This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: no class-of ?


On 09/22/2017 11:28 AM, Jamison Hope wrote:
On a somewhat related note, is there a way to define a macro that can
determine the type of an expression at macro expansion time, without
actually evaluating the expression?

Note that expressions do not have types at macro-expansion time - that happens
at a later stage.

What you can do is write a procedure and associate either a 'validate'
or a 'compile' property with it.

There are lots of examples in kawa/lib.  Look for the search string 'validate-apply:'.
For example in characters.scm:

(define (char->integer ch::character) ::int
  validate-apply: "kawa.lib.compile_misc:charToIntegerValidateApply"
  (as int ch))

You'll find charToIntegerValidateApply in compile-misc.scm:

(define-validate charToIntegerValidateApply (exp required proc)
  ((exp:isSimple 1 1)
   (let ((e0 (visit-exp (exp:getArg 0) character)))
     (apply-exp as int
                (apply-exp gnu.kawa.functions.Convert:cast character e0)))))

This mean is exp is a simple application (no splices or keywords) with one argument,
then apply the following transformation.  This is evaluated during the
InlineCall phase of the compilation.

When it comes to charToIntegerValidateApply it first "validates" argument 0
in the context of the "required type" character (which is a 32-bit "primitive"
character type).  It then wraps the result in a call to the low-lever Convert:cast
procedure.  Assuming the types are correct, this will be a no-op.
Then that result is cast to an int.

If all goes well and the incoming value is a 32-bit unboxed character or a 16-bit unboxed char,
no code actually needs to be generated - which is the reason
for using the charToIntegerValidateApply transformer in the first place.  Otherwise,
you either get a compile-time error of the appropriate run-time conversion.

You can use (exp:getType) to get the type of the expression.

There is also the compile-apply property:

(define (values #!rest (args :: <Object[]>))
  validate-apply: "kawa.lib.compile_misc:valuesValidateApply"
  compile-apply: "kawa.lib.compile_misc:valuesCompile"
  (invoke-static <gnu.mapping.Values> 'make args))

This causes the valuesCompile method in class kawa.lib.compile_misc
to be called at code-generation time.  This is more low-level,
and most of the code-generation are actually written in Java to
generate custom byte-code depending on the argument expressions
and their types.

(If I wanted to get the type of a standalone expression, I'd
look at the implementation of eval (see kawa/lang/Eval.java), and do the same,
but stop before code-generation.  Specifically,you would probably want to do
compilation.process(Compilation.WALKED). Then you'd do something like
compilation.getModule().body.getType().

However, that doesn't give you the type on an expression *in a lexical context*.)
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]