Package gnu.expr

Class Compilation

java.lang.Object
gnu.expr.Compilation
All Implemented Interfaces:
SourceLocator, SourceLocator, Locator
Direct Known Subclasses:
Translator

public class Compilation extends Object implements SourceLocator
State for a single expression or module. For each top-level thing (expression or file) we compile or evaluate we create a new Compilation.
  • Field Details

    • mustCompile

      public boolean mustCompile
      True if the form is too complex to evaluate,and we must compile it. This is because it contains a construct we know how to compile, but not evaluate, and it it outside a function (which we always compile). This can be a let scope, or primitive procedure.
    • curClass

      public ClassType curClass
    • mainClass

      public ClassType mainClass
    • moduleClass

      public ClassType moduleClass
      Generated class that extends ModuleBody. Normally same as mainClass.
    • curLambda

      public LambdaExp curLambda
    • mainLambda

      public ModuleExp mainLambda
    • thisDecl

      public Variable thisDecl
    • PROLOG_PARSING

      public static final int PROLOG_PARSING
      State code for initial pre-parse looking for module name.
      See Also:
    • PROLOG_PARSED

      public static final int PROLOG_PARSED
      We have determined the module name and class, but not finished parsing.
      See Also:
    • BODY_PARSED

      public static final int BODY_PARSED
      State code indicating the entire module has been parsed.
      See Also:
    • RESOLVED

      public static final int RESOLVED
      State code for lexical bindings having been resolved.
      See Also:
    • PRE_WALKED

      public static final int PRE_WALKED
      State code when initial tree-walking (PushApply) are done.
      See Also:
    • WALKED

      public static final int WALKED
      State code when various inlining and optimization passes are done.
      See Also:
    • COMPILE_SETUP

      public static final int COMPILE_SETUP
      State code that various compile-only data has been determined.
      See Also:
    • COMPILED

      public static final int COMPILED
      State code indicating the bytecode has been generated.
      See Also:
    • CLASS_WRITTEN

      public static final int CLASS_WRITTEN
      State code indicating that bytecode has been written to its target.
      See Also:
    • ERROR_SEEN

      public static final int ERROR_SEEN
      See Also:
    • lexer

      public Lexer lexer
    • pendingImports

      public Stack<Object> pendingImports
      Stack of quads of (ModuleInfo, ScopeExp, position, formSize).
    • subModuleMap

      public Map<String,ModuleInfo> subModuleMap
    • writeImplicitClasses

      public static boolean writeImplicitClasses
    • debugPrintExpr

      public static boolean debugPrintExpr
      If true, print out expressions after parsing and before optimizations.
    • debugPrintFinalExpr

      public static boolean debugPrintFinalExpr
      If true, print out final expressions after optimizations etc.
    • debugPrintANF

      public static boolean debugPrintANF
    • enableANF

      public static boolean enableANF
    • options

      public static Options options
    • fullTailCallsVariable

      public static Options.OptionInfo fullTailCallsVariable
    • mainMethodVariable

      public static Options.OptionInfo mainMethodVariable
    • warnUnreachable

      public static Options.OptionInfo warnUnreachable
    • warnVoidUsed

      public static Options.OptionInfo warnVoidUsed
    • warnUninitialized

      public static Options.OptionInfo warnUninitialized
    • warnUndefinedVariable

      public static Options.OptionInfo warnUndefinedVariable
    • warnUnknownMember

      public static Options.OptionInfo warnUnknownMember
    • warnInvokeUnknownMethod

      public static Options.OptionInfo warnInvokeUnknownMethod
    • warnUnused

      public static Options.OptionInfo warnUnused
    • warnAsError

      public static Options.OptionInfo warnAsError
    • maxErrors

      public static Options.OptionInfo maxErrors
    • currentOptions

      public Options currentOptions
    • defaultClassFileVersion

      public static int defaultClassFileVersion
    • defaultCallConvention

      public static int defaultCallConvention
      The default calling convention. One of the following CALL_WITH_xxx values.
    • CALL_WITH_UNSPECIFIED

      public static final int CALL_WITH_UNSPECIFIED
      See Also:
    • CALL_WITH_RETURN

      public static final int CALL_WITH_RETURN
      Plain calling convention, using regular Java parameters and returns.
      See Also:
    • CALL_WITH_CONSUMER

      public static final int CALL_WITH_CONSUMER
      Function results are written to the current CallContext's Consumer.
      See Also:
    • CALL_WITH_TAILCALLS

      public static final int CALL_WITH_TAILCALLS
      Like CALL_WITH_CONSUMER, but handle full on-stack-growing tail-calls.
      See Also:
    • CALL_WITH_CONTINUATIONS

      public static final int CALL_WITH_CONTINUATIONS
      Support for full continuations. Not implemented.
      See Also:
    • MODULE_NONSTATIC

      public static final int MODULE_NONSTATIC
      See Also:
    • MODULE_STATIC_DEFAULT

      public static final int MODULE_STATIC_DEFAULT
      See Also:
    • MODULE_STATIC

      public static final int MODULE_STATIC
      See Also:
    • MODULE_STATIC_RUN

      public static final int MODULE_STATIC_RUN
      See Also:
    • moduleStatic

      public static int moduleStatic
      Default for whether a module is static. If moduleStatic > 0, then (module-static #t) is implied by default. If moduleStatic == MODULE_STATIC_RUN, then <clinit> calls run. If moduleStatic < 0, then (module-static #f) is implied by default.
    • immediate

      public boolean immediate
      True if the compiled result will be immediately loaded.
    • explicit

      public boolean explicit
      Compilation was explicitly requested, rather than being a dependency.
    • method

      public Method method
      The current method.
    • typeObject

      public static ClassType typeObject
    • scmBooleanType

      public static ClassType scmBooleanType
    • typeString

      public static ClassType typeString
    • typeCharSequence

      public static ClassType typeCharSequence
    • javaStringType

      public static ClassType javaStringType
    • scmKeywordType

      public static ClassType scmKeywordType
    • scmSequenceType

      public static ClassType scmSequenceType
    • typeList

      public static final ClassType typeList
    • scmListType

      public static ClassType scmListType
    • typePair

      public static ClassType typePair
    • objArrayType

      public static final ArrayType objArrayType
    • typeRunnable

      public static ClassType typeRunnable
    • typeRunnableModule

      public static ClassType typeRunnableModule
    • typeType

      public static ClassType typeType
    • typeObjectType

      public static ClassType typeObjectType
    • typeClass

      public static ClassType typeClass
    • typeClassType

      public static ClassType typeClassType
    • typeProcedure

      public static ClassType typeProcedure
    • typeLanguage

      public static ClassType typeLanguage
    • typeEnvironment

      public static ClassType typeEnvironment
    • typeLocation

      public static ClassType typeLocation
    • typeLocationProc

      public static final ClassType typeLocationProc
    • typeFieldLocation

      public static ClassType typeFieldLocation
    • typeStaticFieldLocation

      public static ClassType typeStaticFieldLocation
    • typeSymbol

      public static ClassType typeSymbol
    • trueConstant

      public static final Field trueConstant
    • falseConstant

      public static final Field falseConstant
    • voidConsumerInstanceField

      public static final Field voidConsumerInstanceField
    • int1Args

      public static final Type[] int1Args
    • string1Arg

      public static final Type[] string1Arg
    • sym1Arg

      public static final Type[] sym1Arg
    • getCurrentEnvironmentMethod

      public static Method getCurrentEnvironmentMethod
    • apply0args

      public static Type[] apply0args
    • apply1args

      public static Type[] apply1args
    • apply2args

      public static Type[] apply2args
    • applyNargs

      public static Type[] applyNargs
    • apply0method

      public static Method apply0method
    • apply1method

      public static Method apply1method
    • apply2method

      public static Method apply2method
    • apply3method

      public static Method apply3method
    • apply4method

      public static Method apply4method
    • applyNmethod

      public static Method applyNmethod
    • applymethods

      public static Method[] applymethods
    • typeProcedure0

      public static ClassType typeProcedure0
    • typeProcedure1

      public static ClassType typeProcedure1
    • typeProcedure2

      public static ClassType typeProcedure2
    • typeProcedure3

      public static ClassType typeProcedure3
    • typeProcedure4

      public static ClassType typeProcedure4
    • typeProcedureN

      public static ClassType typeProcedureN
    • typeModuleBody

      public static ClassType typeModuleBody
    • typeApplet

      public static ClassType typeApplet
    • typeServlet

      public static ClassType typeServlet
    • typeCallContext

      public static ClassType typeCallContext
    • typeConsumer

      public static final ClassType typeConsumer
    • getCallContextInstanceMethod

      public static Method getCallContextInstanceMethod
    • typeValues

      public static ClassType typeValues
    • noArgsField

      public static Field noArgsField
    • pcCallContextField

      public static Field pcCallContextField
    • typeMethodProc

      public static ClassType typeMethodProc
    • typeCompiledProc

      public static ClassType typeCompiledProc
    • argsCallContextField

      public static Field argsCallContextField
    • procCallContextField

      public static Field procCallContextField
    • applyCpsMethod

      public static Method applyCpsMethod
    • typeProcedureArray

      public static ClassType[] typeProcedureArray
    • getNextArgMethod

      public static final Method getNextArgMethod
    • inlineOk

      public static boolean inlineOk
    • classPrefixDefault

      public static String classPrefixDefault
    • classPrefix

      public String classPrefix
      If non-null: a prefix for generateClassName to prepend to names.
    • emitSourceDebugExtAttr

      public static boolean emitSourceDebugExtAttr
    • language

      protected Language language
    • exprStack

      public Stack<Expression> exprStack
      A help vector for building expressions.
    • lexical

      public NameLookup lexical
      Current lexical scope - map name to Declaration.
    • current_scope

      protected ScopeExp current_scope
    • messages

      protected SourceMessages messages
  • Constructor Details

  • Method Details

    • getState

      public int getState()
      Returns a code indicating how far along we are in the parsing/compilation process.
    • setState

      public void setState(int state)
    • isPedantic

      public boolean isPedantic()
    • setPedantic

      public void setPedantic(boolean value)
    • pushPendingImport

      public void pushPendingImport(ModuleInfo info, ScopeExp defs, Translator.FormStack forms, require.DeclSetMapper mapper)
    • generateMainMethod

      public boolean generateMainMethod()
    • warnUnreachable

      public boolean warnUnreachable()
    • warnUndefinedVariable

      public boolean warnUndefinedVariable()
    • warnUnknownMember

      public boolean warnUnknownMember()
    • warnInvokeUnknownMethod

      public boolean warnInvokeUnknownMethod()
    • warnUnused

      public boolean warnUnused()
    • warnUninitialized

      public boolean warnUninitialized()
    • warnVoidUsed

      public boolean warnVoidUsed()
    • warnAsError

      public boolean warnAsError()
    • maxErrors

      public static int maxErrors()
    • getBooleanOption

      public final boolean getBooleanOption(String key, boolean defaultValue)
      Get a named boolean option.
    • getBooleanOption

      public final boolean getBooleanOption(String key)
      Get a named boolean option.
    • currentCallConvention

      public int currentCallConvention()
    • usingCPStyle

      public boolean usingCPStyle()
    • usingTailCalls

      public boolean usingTailCalls()
    • usingCallContext

      public boolean usingCallContext()
    • getCode

      public final CodeAttr getCode()
    • generatingApplet

      public boolean generatingApplet()
      True if we should generate an Applet.
    • generatingServlet

      public boolean generatingServlet()
      True if we should generate a Servlet.
    • sharedModuleDefs

      public boolean sharedModuleDefs()
    • setSharedModuleDefs

      public void setSharedModuleDefs(boolean shared)
    • getModuleType

      public final ClassType getModuleType()
    • compileConstant

      public void compileConstant(Object value)
      Emit code to "evaluate" a compile-time constant. This is the normal external interface.
      Parameters:
      value - the value to be compiled
    • inlineOk

      public boolean inlineOk(Expression proc)
    • inlineOk

      public boolean inlineOk(Procedure proc)
    • isApplyFunction

      public boolean isApplyFunction(Expression exp)
    • isSimpleApplyFunction

      public boolean isSimpleApplyFunction(Expression exp)
      A simple apply function maps actual arguments to formals directly. E.g. no distribution of multiple values.
    • compileConstant

      public void compileConstant(Object value, Target target)
    • emitPushBoolean

      public void emitPushBoolean(boolean value)
      Push language-specific boxed true or false value.
    • emitCoerceToBoolean

      public void emitCoerceToBoolean()
      Generate code to test if an object is considered true. Assume the object has been pushed on the JVM stack. Generate code to push (unboxed) true or false as appropriate.
    • asBooleanValue

      public Type asBooleanValue(ConditionalTarget target, Type stackType)
      Hook for language-specific handling in ConditionalTarget.
      Parameters:
      stackType - Value to be treated as boolean, already pushed.
      Returns:
      null if we've handled the conditional transfer; otherwise type value has been converted to (usually booleanType).
    • findNamedClass

      public ClassType findNamedClass(String name)
      Search this Compilation for a ClassType with a given name.
      Parameters:
      name - the name of the class desired
      Returns:
      the matching ClassType, or null if none is found
    • mangleURI

      public static String mangleURI(String name)
      Map a URI to a package/class name. Similar to the JAXB mangling, and that in the Java language spec.
    • generateClassName

      public String generateClassName(String hint)
      Generate an unused class name.
      Parameters:
      hint - the requested name (or prefix)
      Returns:
      a unique class name.
    • outputClass

      public void outputClass(String directory) throws IOException
      Throws:
      IOException
    • cleanupAfterCompilation

      public void cleanupAfterCompilation()
    • compileToArchive

      public void compileToArchive(ModuleExp mexp, String fname) throws IOException
      Throws:
      IOException
    • addClass

      public void addClass(ClassType new_class)
    • makeRunnable

      public boolean makeRunnable()
    • addMainClass

      public void addMainClass(ModuleExp module)
    • getConstructor

      public final Method getConstructor(LambdaExp lexp)
    • getConstructor

      public static final Method getConstructor(ClassType clas, LambdaExp lexp)
    • generateConstructor

      public final void generateConstructor(LambdaExp lexp)
    • generateConstructor

      public final void generateConstructor(ClassType clas, LambdaExp lexp)
    • generateCheckMethod

      public Method generateCheckMethod(LambdaExp lexp, LambdaExp parent)
    • generateCheckMethods

      public void generateCheckMethods(LambdaExp parent)
    • process

      public void process(int wantedState)
      Parse/visit/compile this module as needed and requested. This method does not process any dependent modules (expect indirectly, such as may be done by a require form).
      Parameters:
      wantedState - the desired value of getState().
    • allocLocalField

      public Field allocLocalField(Type type, String name)
    • loadCallContext

      public final void loadCallContext()
      Generate code to push the current CallContext on the JVM stack.
    • freeLocalField

      public void freeLocalField(Field field)
    • parse

      public Expression parse(Object input)
      This may not make sense, except for Lisp-like languages. For those, 'input' an s-expression from the reader.
    • getLanguage

      public Language getLanguage()
    • currentLambda

      public LambdaExp currentLambda()
    • getModule

      public final ModuleExp getModule()
    • setModule

      public void setModule(ModuleExp mexp)
    • isStatic

      public boolean isStatic()
    • isInteractive

      public boolean isInteractive()
    • currentModule

      public ModuleExp currentModule()
      The same as getModule, until we allow nested modules.
    • mustCompileHere

      public void mustCompileHere()
      Note that we have seen a construct that must be compiled, not evaluated. If we are not inside a lambda (which is always compiled), but only inside the outer-most ModuleExp, note that it must be compiled.
    • currentScope

      public ScopeExp currentScope()
    • setCurrentScope

      public void setCurrentScope(ScopeExp scope)
      Set currentScope(). Also update the nesting object.
    • setPushCurrentScope

      public ScopeExp setPushCurrentScope(ScopeExp scope)
    • setPopCurrentScope

      public void setPopCurrentScope(ScopeExp old)
    • pushNewModule

      public ModuleExp pushNewModule(Lexer lexer)
    • push

      public void push(ScopeExp scope)
    • pushScope

      public final void pushScope(ScopeExp scope)
    • pop

      public void pop(ScopeExp scope)
    • pop

      public final void pop()
    • push

      public void push(Declaration decl)
    • lookup

      public Declaration lookup(Object name, int namespace)
    • usedClass

      public void usedClass(Type type)
      Called for classes referenced in bytecode. Since this only does something when immediate, we only care about classes referenced in the bytecode when immediate. It is used to ensure that we can inherit from classes defined when in immediate mode (in Scheme using define-class or similar).
    • setModuleName

      public void setModuleName(String name)
      Set module name - which sets name of generated class.
    • setInteractiveName

      public void setInteractiveName()
      Generate and set unique module name suitable for an interactive session.
    • setEvalName

      public void setEvalName()
      Generate and set unique module name suitable for a call to eval.
    • getMessages

      public SourceMessages getMessages()
    • setMessages

      public void setMessages(SourceMessages messages)
    • error

      public void error(char severity, String message, SourceLocator location)
    • error

      public void error(char severity, String message)
    • error

      public void error(char severity, Declaration decl, String msg1, String msg2)
    • error

      public void error(char severity, String message, String code, SourceLocator decl)
    • syntaxError

      public ErrorExp syntaxError(String message)
      Handle syntax errors (at rewrite time).
      Parameters:
      message - an error message to print out
      Returns:
      an ErrorExp
    • getLineNumber

      public final int getLineNumber()
      Description copied from interface: SourceLocator
      Return current line number. Normally the same as getStartLine(). The "first" line is line 1; unknown is -1.
      Specified by:
      getLineNumber in interface Locator
      Specified by:
      getLineNumber in interface SourceLocator
      Specified by:
      getLineNumber in interface SourceLocator
    • getColumnNumber

      public final int getColumnNumber()
      Description copied from interface: SourceLocator
      Return current column number. Normally the same as getStartColumn(). The "first" column is column 1; unknown is -1.
      Specified by:
      getColumnNumber in interface Locator
      Specified by:
      getColumnNumber in interface SourceLocator
      Specified by:
      getColumnNumber in interface SourceLocator
    • getStartLine

      public final int getStartLine()
      Description copied from interface: SourceLocator
      Line number (one-origin) of start of range; unknown/unspecified is -1.
      Specified by:
      getStartLine in interface SourceLocator
    • getStartColumn

      public final int getStartColumn()
      Description copied from interface: SourceLocator
      Column (one-origin) of start of range; unknown/unspecified is -1.
      Specified by:
      getStartColumn in interface SourceLocator
    • getEndLine

      public final int getEndLine()
      Description copied from interface: SourceLocator
      Line number (one-origin) of end of range; unknown/unspecified is -1.
      Specified by:
      getEndLine in interface SourceLocator
    • getEndColumn

      public final int getEndColumn()
      Description copied from interface: SourceLocator
      Column (one-origin) of end of range; unknown/unspecified is -1.
      Specified by:
      getEndColumn in interface SourceLocator
    • getFileName

      public final String getFileName()
      Description copied from interface: SourceLocator
      Normally same as getSystemId.
      Specified by:
      getFileName in interface SourceLocator
    • getPublicId

      public String getPublicId()
      Specified by:
      getPublicId in interface Locator
      Specified by:
      getPublicId in interface SourceLocator
      Specified by:
      getPublicId in interface SourceLocator
    • getSystemId

      public String getSystemId()
      Specified by:
      getSystemId in interface Locator
      Specified by:
      getSystemId in interface SourceLocator
      Specified by:
      getSystemId in interface SourceLocator
    • isStableSourceLocation

      public boolean isStableSourceLocation()
      Description copied from interface: SourceLocator
      True if position is unlikely to change. True for an expression but not an input file.
      Specified by:
      isStableSourceLocation in interface SourceLocator
    • setFile

      public void setFile(String filename)
    • setLine

      public void setLine(int line)
    • setColumn

      public void setColumn(int column)
    • setLine

      public final void setLine(Expression position)
    • setLine

      public void setLine(Object location)
    • setLocation

      public final void setLocation(SourceLocator position)
    • setLine

      public void setLine(String filename, int line, int column)
    • getSourceAbsPath

      public gnu.kawa.io.Path getSourceAbsPath()
      Get source filename as an absolute Path, or null. Return null if there is no actual file, such as a <string>. Note the ModuleInfo.getSourceAbsPath() is similar, but this version is not canonicalized.
    • letStart

      public void letStart()
    • letVariable

      public Declaration letVariable(Object name, Type type, Expression init)
    • letVariable

      public void letVariable(Declaration decl, Expression init)
    • letEnter

      public void letEnter()
    • letDone

      public LetExp letDone(Expression body)
    • loopStart

      public LambdaExp loopStart()
      Start a new loop. This provides the functionality of Scheme 'named let'.
    • loopVariable

      public Declaration loopVariable(Object name, Type type, Expression init)
      Add a new loop variable, with initializer.
    • loopEnter

      public void loopEnter()
      Done handling loop variables, and pushes them into the lexical scope. Ready to parse the loop condition.
    • loopCond

      @Deprecated public void loopCond(Expression cond)
      Deprecated.
    • loopBody

      @Deprecated public void loopBody(Expression body)
      Deprecated.
    • loopRepeat

      public Expression loopRepeat(LambdaExp loop, Expression... exps)
      Recurse to next iteration of specified loop.
    • loopDone

      public Expression loopDone(Expression body)
      Finish building a loop and return resulting expression.
    • loopRepeatDone

      public Expression loopRepeatDone(Expression... exps)
      Combine loopRepeat and loopDone. Assume loopCond and loopBody have been called.
    • applyFunction

      public Expression applyFunction(Expression func)
      A language-dependent "apply" function for generic application.
    • makeQuoteExp

      public QuoteExp makeQuoteExp(Object value)
    • makeCoercion

      public static ApplyExp makeCoercion(Expression value, Expression type)
      Convenience method to make an Expression that coerces a value.
      Parameters:
      value - to be coerced
      type - to coerce value to
      Returns:
      expression that coerces value to type
    • makeCoercion

      public static ApplyExp makeCoercion(Expression value, Type type)
      Convenience method to make an Expression that coerces a value.
      Parameters:
      value - to be coerced
      type - to coerce value to
      Returns:
      expression that coerces value to type
    • loadClassRef

      public void loadClassRef(ObjectType clas)
      Generate code to load a named Class without initializing it.
    • getForNameHelper

      public Method getForNameHelper()
      Generate a method to find a named Class without initializing it. Generate a static helper method "class$" like javac generates for 'CLASS.class', but does not initialize CLASS. Also, we don't bother catching exceptions, since the JVM doesn't require us to. I.e. generates: public static class $(String name) { return Class.forName(name, false, Class.forName(THISCLASSNAME).getClassLoader()); } Note that we want the result to use the same ClassLoader as the caller, which is why we generate a static helper method.
    • getGlobalEnvironment

      public Environment getGlobalEnvironment()
    • resolve

      public Object resolve(Object name, boolean function)
    • setupLiterals

      public static void setupLiterals(int key)
      Call-back from compiled code to initialize literals in immediate mode. In non-immediate mode (i.e. generating class files) the compiler emits code to "re-construct" literal values. However, in immediate mode that would be wasteful, plus we would get values that are similar (equals) to but not necessarily identical (eq) to the compile-time literal. So we need to pass the literal values to the compiled code, by using reflection to initialize various static fields. This method does that. It is called at the start of the generated static initializer, which helps makes things more consistent between immediate and non-immediate mode.
    • registerForImmediateLiterals

      public static int registerForImmediateLiterals(Compilation comp)
    • findForImmediateLiterals

      public static Compilation findForImmediateLiterals(int key)
    • getCurrent

      public static Compilation getCurrent()
    • setCurrent

      public static void setCurrent(Compilation comp)
    • setSaveCurrent

      public static Compilation setSaveCurrent(Compilation comp)
    • restoreCurrent

      public static void restoreCurrent(Compilation saved)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getMinfo

      public ModuleInfo getMinfo()