com.saxonica.bytecode

## Class ExpressionCompiler

- java.lang.Object
- - com.saxonica.bytecode.ExpressionCompiler

- Direct Known Subclasses:
    ErrorExpressionCompiler, LiteralCompiler, ToBooleanCompiler, ToItemCompiler, ToIteratorCompiler

      public abstract class ExpressionCompiler
      extends Object

  This abstract class represents the compiler (that is, Java bytecode generator) for a particular kind of expression on the expression tree. The expression compiler is called by the `CompilerService` working its way down the expression tree in a top down fashion: there is a one-to-one correspondence between the classes implementing the expression on the expression tree and the compiler object used to generate Java code fragments. Various methods are supplied to compile expressions; exactly one of them is called, depending on the context in which the expression appears.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ExpressionCompiler()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static void` | `allocateStatic(CompilerService compiler, Object value)` Generate code to allocate static field in widget and to push field on stack
    `protected void` | `compileItemFromInt(CompilerService compiler, Expression expression)` Helper method to implement compileFromItem when the class implements compileToPrimitive returning an integer
    `protected void` | `compileItemFromString(CompilerService compiler, Expression expression)` Helper method to implement compileFromItem when the class implements compileToString
    `abstract void` | `compileToBoolean(CompilerService compiler, Expression expression)` Generate bytecode to evaluate the expression as a boolean Precondition: none.
    `abstract void` | `compileToItem(CompilerService compiler, Expression expression)` Generate bytecode to evaluate the expression as an Item Precondition: none.
    `abstract void` | `compileToIterator(CompilerService compiler, Expression expression)` Generate bytecode to evaluate the expression as a `SequenceIterator` Precondition: none.
    `void` | `compileToLoop(CompilerService compiler, Expression expression, LoopBodyGenerator loopBody)` The compileToLoop method compiles this expression in such a way that the code generated by the supplied loopBody argument will be executed once for each item in the result of this expression.
    `void` | `compileToPrimitive(CompilerService compiler, Expression expression, Class requiredClass, OnEmpty onEmpty)` Generate bytecode to evaluate the expression leaving a plain Java value on the stack.
    `abstract void` | `compileToPush(CompilerService compiler, Expression expression)` Generate bytecode to evaluate the expression in push mode Precondition: none.
    `void` | `generateMethod(CompilerService compiler, Expression expression, org.objectweb.asm.ClassVisitor cv)`
    `static void` | `handleEmptyStringResult(OnEmpty onEmpty, Generator ga, GeneratedMethodInfo methodInfo, LabelInfo exit)`
    `static void` | `throwXPathException(CompilerService compiler, MessageBuilder builder, String errorCode, SourceLocator locator, boolean isTypeError)` Generate code to throw an XPath Exception.
    `static void` | `throwXPathException(CompilerService compiler, String message, String errorCode, SourceLocator locator, boolean isTypeError)` Generate code to throw an XPath Exception.
    `void` | `unboxItem(Generator ga, Class requiredClass)` Generate code to extract a primitive value from an Item (actually an Atomic value) given knowledge of the primitive type of the item
    `static void` | `verify(org.objectweb.asm.ClassWriter cw, String objectName, boolean debug)` Verify the generated bytecode, for diagnostic purposes
    `static void` | `visitAnnotation(CompilerService compiler, String message)` Generate annotations in the bytecode, for diagnostic purposes
    `static void` | `visitLineNumber(CompilerService compiler, Generator ga, Expression expr)` Generate line number of expression in byte-code

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ExpressionCompiler

          public ExpressionCompiler()

  - ### Method Detail

    - #### compileToItem

          public abstract void compileToItem(CompilerService compiler,
                           Expression expression)
                                      throws CannotCompileException

      Generate bytecode to evaluate the expression as an Item Precondition: none. Postcondition: at execution time, the stack contains either an item (the result of the expression), or null (representing an empty sequence).
      Parameters:
        `compiler` - the compiler service
        `expression` - the expression to be compiled
      Throws:
        `CannotCompileException`

    - #### compileToIterator

          public abstract void compileToIterator(CompilerService compiler,
                               Expression expression)
                                          throws CannotCompileException

      Generate bytecode to evaluate the expression as a `SequenceIterator` Precondition: none. Postcondition: at execution time, the stack contains a SequenceIterator representing the result of the expression
      Parameters:
        `compiler` - the compiler service
        `expression` - the expression to be compiled
      Throws:
        `CannotCompileException`

    - #### compileToBoolean

          public abstract void compileToBoolean(CompilerService compiler,
                              Expression expression)
                                         throws CannotCompileException

      Generate bytecode to evaluate the expression as a boolean Precondition: none. Postcondition: at execution time, the stack contains an integer (0=false, 1=true) representing the result of the expression
      Parameters:
        `compiler` - the compiler service
        `expression` - the expression to be compiled
      Throws:
        `CannotCompileException`

    - #### compileToPrimitive

          public void compileToPrimitive(CompilerService compiler,
                                Expression expression,
                                Class requiredClass,
                                OnEmpty onEmpty)
                                  throws CannotCompileException

      Generate bytecode to evaluate the expression leaving a plain Java value on the stack. This method must only be called if the static type of the expressionis such that the value is known to be representable by a value of the specified class; the cardinality must be either exactly-one or zero-or-one.
      Parameters:
        `compiler` - the compiler service
        `expression` - the expression to be compiled
        `requiredClass` - the class of the Java value that is to be left on the stack if the generated code exits normally. This must be one of Integer.TYPE, Double.TYPE, Float.TYPE, BigDecimal.class, CharSequence.class.
        `onEmpty` - defines the action to be taken if the expression evaluates to the empty sequence. May be null if the expression is known statically not to evaluate to an empty sequence. If the return class is a primitive type (double, float, etc) this must be an instance of `OnEmpty.UnwindAndJump`
      Throws:
        `CannotCompileException`

    - #### handleEmptyStringResult

          public static void handleEmptyStringResult(OnEmpty onEmpty,
                                     Generator ga,
                                     GeneratedMethodInfo methodInfo,
                                     LabelInfo exit)

    - #### compileToPush

          public abstract void compileToPush(CompilerService compiler,
                           Expression expression)
                                      throws CannotCompileException

      Generate bytecode to evaluate the expression in push mode Precondition: none. Postcondition: at execution time, the stack is unchanged, and the value of the expression has been written to the current receiver
      Parameters:
        `compiler` - the compiler service
        `expression` - the expression to be compiled
      Throws:
        `CannotCompileException`

    - #### compileToLoop

          public void compileToLoop(CompilerService compiler,
                           Expression expression,
                           LoopBodyGenerator loopBody)
                             throws CannotCompileException

      The compileToLoop method compiles this expression in such a way that the code generated by the supplied loopBody argument will be executed once for each item in the result of this expression. The code generated by the loopBody expects to find this item on the top of the bytecode stack, and must remove it.

      Note that the compileToLoop() method must not be used where there is a need to maintain the value of position() or size(), or equivalents such as updating range variables. It is therefore generally used only for compiling aggregate functions such as count() and sum().

      This has proved something of an obstacle. Attempts to compile path expressions as nested loops have failed in the case where there is a dependency on position() or last(): the current dependency mechanism doesn't always allow these cases to be detected (it only tells us when an expression uses position() or last(), not when it needs to maintain position() and last()). Hence this mode of compilation is not widely implemented (other than by the default implementation) and is currently used only when compiling sum() and count().

      Parameters:
        `compiler` - the compiler service
        `expression` - the expression to be compiled
        `loopBody` - a generator which produces code to be executed once for every item in the result of this expression, with that item being on the top of the bytecode stack.
      Throws:
        `CannotCompileException`

    - #### unboxItem

          public void unboxItem(Generator ga,
                       Class requiredClass)

      Generate code to extract a primitive value from an Item (actually an Atomic value) given knowledge of the primitive type of the item
      Parameters:
        `ga` - the generator adapter
        `requiredClass` - the type of value required

    - #### compileItemFromInt

          protected final void compileItemFromInt(CompilerService compiler,
                                Expression expression)
                                           throws CannotCompileException

      Helper method to implement compileFromItem when the class implements compileToPrimitive returning an integer
      Parameters:
        `compiler` -
        `expression` -
      Throws:
        `CannotCompileException`

    - #### compileItemFromString

          protected final void compileItemFromString(CompilerService compiler,
                                   Expression expression)
                                              throws CannotCompileException

      Helper method to implement compileFromItem when the class implements compileToString
      Parameters:
        `compiler` -
        `expression` -
      Throws:
        `CannotCompileException`

    - #### visitAnnotation

          public static void visitAnnotation(CompilerService compiler,
                             String message)

      Generate annotations in the bytecode, for diagnostic purposes
      Parameters:
        `compiler` -
        `message` - the message to be inserted in the bytecode

    - #### allocateStatic

          public static void allocateStatic(CompilerService compiler,
                            Object value)

      Generate code to allocate static field in widget and to push field on stack
      Parameters:
        `compiler` - the CompilerService
        `value` - the Object of the field

    - #### throwXPathException

          public static void throwXPathException(CompilerService compiler,
                                 String message,
                                 String errorCode,
                                 SourceLocator locator,
                                 boolean isTypeError)

      Generate code to throw an XPath Exception. This can only be used if all properties of the exception including the message are known statically

    - #### throwXPathException

          public static void throwXPathException(CompilerService compiler,
                                 MessageBuilder builder,
                                 String errorCode,
                                 SourceLocator locator,
                                 boolean isTypeError)

      Generate code to throw an XPath Exception. The message is constructed at runtime using the MessageBuilder.

    - #### verify

          public static void verify(org.objectweb.asm.ClassWriter cw,
                    String objectName,
                    boolean debug)
                             throws CannotCompileException

      Verify the generated bytecode, for diagnostic purposes
      Parameters:
        `cw` - the ClassWriter
        `objectName` -
      Throws:
        `CannotCompileException`

    - #### visitLineNumber

          public static void visitLineNumber(CompilerService compiler,
                             Generator ga,
                             Expression expr)

      Generate line number of expression in byte-code
      Parameters:
        `compiler` -
        `ga` - the generator adapter
        `expr` - Expression

    - #### generateMethod

          public void generateMethod(CompilerService compiler,
                            Expression expression,
                            org.objectweb.asm.ClassVisitor cv)

Copyright (c) 2004-2013 Saxonica Limited. All rights reserved.