Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

AST Transformations


[ 188 ]


  • Conversion: In this phase, the token tree is converted into an AST. This is
    the very first time that we have access to an AST. In the AST viewer, we
    will see an AST representation of the code for the first time when we select
    this phase.

  • Semantic analysis: The compiler performs consistency checks and validity
    checks on the AST that cannot be detected by the grammar. This is generally
    the first phase at which we would normally consider attaching our own AST
    transformation. If we were to hook a transform into conversion for instance,
    we risk operating on code that may fail the consistency and validity checks.
    This is the first point at which we can be confident that the code being
    compiled is syntactically correct. This is also the phase at which outside
    references are resolved to classes, static imports and the like.

  • Canonicalization: Access from inner classes to surrounding scope is
    resolved. This also completes what are considered the frontend phases of the
    compiler. The following phases are called the backend phases and are more
    involved in generating the final byte code.

  • Instruction selection: This is the point at which the instruction set is
    selected. Java class files are only forward compatible. This means that
    while a class compiled for a specific version of JVM will still work on later
    versions the converse is not necessarily true. For instance, early prerelease
    versions of JDK 5 implemented varargs, enums, and generics using the JDK
    4 bytecode; however, the release version of the JDK implemented new class
    file attributes, so classes compiled for JDK 5, which use these features, are
    no longer backward compatible. This is the phase of the compilation process
    that selects which bytecode instruction set to use. You will note in the AST
    viewer that no new AST nodes are added at this point.

  • Class generation: This is the point at which the final Java version of
    the Groovy class is generated. In the AST viewer, you will see the AST
    augmented with Groovy bean methods for property getter and setter
    methods. You will also see the introduction of the metaClass property and
    the invokeMethod method. It's also the first phase at which we can observe
    the generation of the bytecode. In the AST viewer, select a method or
    constructor and look at the bytecode tab.

  • Output: As the name suggests, this is the phase at which the .class files are
    written out to disk with the bytecode that has been generated.

  • Finalization: This is the bookend to the initialization phase. Files are closed,
    resources released, and other general housekeeping is performed. Nothing
    new is added to the AST at this point.


http://www.ebook3000.com
Free download pdf