Package gnu.expr

Class LambdaExp

All Implemented Interfaces:
gnu.kawa.format.Printable, Named, SourceLocator, SourceLocator, Locator
Direct Known Subclasses:
ClassExp, ModuleExp

public class LambdaExp extends ScopeExp
Class used to implement Scheme lambda expressions.
  • Field Details

    • body

      public Expression body
    • min_args

      public int min_args
      Minimum number of actual arguments. Does not count implicit isThisParameter().
    • max_args

      public int max_args
      Maximum number of actual arguments; -1 if variable. Does not count keyword arguments.
    • opt_args

      public int opt_args
      Number of optional arguments, not counting keyword arguments.
    • keywords

      public Keyword[] keywords
    • firstChild

      public LambdaExp firstChild
    • nextSibling

      public LambdaExp nextSibling
    • returnContinuation

      public Expression returnContinuation
      The unique call site that calls this lambda. The value is null if no callers have been seen. A value of unknownContinuation means there are multiple call sites. Tail-recursive calls do not count as multiple call sites. This is used to see if we can inline the function at its unique call site. Usually this is an ApplyExp, but it can also be the "tail position" for some outer expression, such as an IfExp. This allows inlining f in the call 'if (cond) f(x) else f(y)' since both calls have the same return point.
    • inlineHome

      public LambdaExp inlineHome
      If this lambda gets inlined this is the containing lambda. Otherwise this is null.
    • nameDecl

      public Declaration nameDecl
      If non-null, a Declaration whose value is (only) this LambdaExp.
    • CLOSURE_ENV_NAME

      public static final String CLOSURE_ENV_NAME
      See Also:
    • closureEnvField

      public Field closureEnvField
      If non-null, this is a Field that is used for implementing lexical closures. If getName() is CLOSURE_ENV_NAME, it is our parent's heapFrame, which is an instance of one of our siblings. (Otherwise, we use "this" as the implicit "closureEnv" field.)
    • staticLinkField

      public Field staticLinkField
      Field in heapFrame.getType() that contains the static link. It is used by child functions to get to outer environments. Its value is this function's closureEnv value.
    • NO_FIELD

      public static final int NO_FIELD
      See Also:
    • SEQUENCE_RESULT

      public static final int SEQUENCE_RESULT
      See Also:
    • OVERLOADABLE_FIELD

      public static final int OVERLOADABLE_FIELD
      See Also:
    • ATTEMPT_INLINE

      public static final int ATTEMPT_INLINE
      See Also:
    • IN_EXPWALKER

      public static final int IN_EXPWALKER
      See Also:
    • PASSES_TAILCALLS

      public static final int PASSES_TAILCALLS
      Treat as inlined in outer lambda when determining tailcalls.
      See Also:
    • PUBLIC_METHOD

      public static final int PUBLIC_METHOD
      True of emitted method should be public. Needed if PrimProcedure.getMethodFor shold be able to find it.
      See Also:
    • ALLOW_OTHER_KEYWORDS

      public static final int ALLOW_OTHER_KEYWORDS
      See Also:
    • HAS_NONTRIVIAL_PATTERN

      protected static final int HAS_NONTRIVIAL_PATTERN
      See Also:
    • HAS_NONTRIVIAL_DEFAULT

      protected static final int HAS_NONTRIVIAL_DEFAULT
      True if IS_SUPPLIED_PARAMETER is set on a parameter. In that case we generate an extra foo$P method for the actual body. (It might make sense to also do this whenever a default expression is non-literal, but we current don't do that.)
      See Also:
    • NEXT_AVAIL_FLAG

      protected static final int NEXT_AVAIL_FLAG
      See Also:
    • returnType

      public Type returnType
      If non-null, the type of values returned by this function. If null, the return type has not been set or calculated yet.
    • applyToConsumer

      public static final MethodHandle applyToConsumer
  • Constructor Details

    • LambdaExp

      public LambdaExp()
    • LambdaExp

      public LambdaExp(int args)
    • LambdaExp

      public LambdaExp(Expression body)
  • Method Details

    • capture

      public void capture(Declaration decl)
    • addParameter

      public Declaration addParameter(Object name)
    • setExceptions

      public void setExceptions(Expression[] exceptions)
    • getInlineOnly

      public final boolean getInlineOnly()
      True iff this lambda is only "called" inline.
    • setInlineOnly

      public final void setInlineOnly(boolean inlineOnly)
    • inlinedInCheckMethod

      public final boolean inlinedInCheckMethod()
    • inlinedInCallerOrCheckMethodOnly

      public boolean inlinedInCallerOrCheckMethodOnly()
      True if no primitive method is created for this procedure. Specifically if getInlineOnly() || inlinedInCheckMethod() .
    • setInlineOnly

      public final void setInlineOnly(Expression returnContinuation, LambdaExp caller)
      Note this function is inlined in a give context. This is meant to be used during validate-apply processing, for procedures that will be inlined in a compile method.
    • getNeedsClosureEnv

      public final boolean getNeedsClosureEnv()
    • getNeedsStaticLink

      public final boolean getNeedsStaticLink()
      True if a child lambda uses lexical variables from outside. Hence, a child heapFrame needs a staticLink to outer frames.
    • setNeedsStaticLink

      public final void setNeedsStaticLink(boolean needsStaticLink)
    • getImportsLexVars

      public final boolean getImportsLexVars()
      True iff this lambda "captures" (uses) lexical variables from outside.
    • setImportsLexVars

      public final void setImportsLexVars(boolean importsLexVars)
    • setImportsLexVars

      public final void setImportsLexVars()
    • setNeedsStaticLink

      public final void setNeedsStaticLink()
    • getCanRead

      public final boolean getCanRead()
    • setCanRead

      public final void setCanRead(boolean read)
    • isClassMethod

      public final boolean isClassMethod()
      True if this is a method in an ClassExp.
    • setClassMethod

      public final void setClassMethod(boolean isMethod)
    • isModuleBody

      public final boolean isModuleBody()
      True iff this is the dummy top-level function of a module body.
    • isAbstract

      public boolean isAbstract()
    • isNative

      public boolean isNative()
    • getCallConvention

      public int getCallConvention()
      The calling convention used for this function. It is derived from Compilation's currentCallConvention.
      Returns:
      One of the CALL_WITH_xxx values in Compilation.
    • setCallConvention

      public void setCallConvention(Compilation comp)
    • usingCallContext

      public boolean usingCallContext()
    • isHandlingTailCalls

      public final boolean isHandlingTailCalls()
    • variable_args

      public final boolean variable_args()
    • getCompiledClassType

      protected ClassType getCompiledClassType(Compilation comp)
      Return the ClassType of the Procedure this is being compiled into.
    • calculateType

      protected Type calculateType()
      Overrides:
      calculateType in class Expression
    • getClassType

      public ClassType getClassType()
      The ClassType generated for this class. Only used for ClassExp (which overrides this method) or ModuleExp.
    • setType

      public void setType(ClassType type)
    • incomingArgs

      public int incomingArgs()
      Number of argument variable actually passed by the caller. For functions that accept more than 4 argument, or take a variable number, this is 1, since in that all arguments are passed in a single array.
    • getMethod

      public final Method getMethod(int nonSpliceCount, int spliceCount)
      Select the method used given an argument count.
    • getMainMethod

      public final Method getMainMethod()
      Get the method that contains the actual body of the procedure. (The other methods are just stubs that call that method.)
    • restArgType

      public final Type restArgType()
      Return the parameter type of the "keyword/rest" parameters.
    • outerLambda

      public LambdaExp outerLambda()
    • outerLambdaOrCaller

      public LambdaExp outerLambdaOrCaller()
    • outerLambdaNotInline

      public LambdaExp outerLambdaNotInline()
      Return the closest outer non-inlined LambdaExp.
    • getCaller

      public LambdaExp getCaller()
      For an INLINE_ONLY function, return the function it gets inlined in.
    • declareThis

      public Variable declareThis(ClassType clas)
    • declareClosureEnv

      public Variable declareClosureEnv()
    • loadHeapFrame

      public void loadHeapFrame(Compilation comp)
      Generate code to load heapFrame on the JVM stack.
    • compileEnd

      public void compileEnd(Compilation comp)
    • generateApplyMethods

      public void generateApplyMethods(Compilation comp)
    • compileSetField

      public Field compileSetField(Compilation comp)
    • compile

      public void compile(Compilation comp, Target target)
      Specified by:
      compile in class Expression
    • getHeapFrameType

      public ClassType getHeapFrameType()
    • getOwningLambda

      public LambdaExp getOwningLambda()
    • allocChildClasses

      public void allocChildClasses(Compilation comp)
    • pushChild

      public void pushChild(LambdaExp child)
    • reverseChildList

      public void reverseChildList()
    • allocFrame

      public void allocFrame(Compilation comp)
    • compileBody

      public void compileBody(Compilation comp)
    • visit

      protected <R, D> R visit(ExpVisitor<R,D> visitor, D d)
      Overrides:
      visit in class ScopeExp
    • visitChildren

      protected <R, D> void visitChildren(ExpVisitor<R,D> visitor, D d)
      Overrides:
      visitChildren in class Expression
    • visitChildrenOnly

      protected final <R, D> void visitChildrenOnly(ExpVisitor<R,D> visitor, D d)
    • visitProperties

      protected final <R, D> void visitProperties(ExpVisitor<R,D> visitor, D d)
    • mustCompile

      protected boolean mustCompile()
      Specified by:
      mustCompile in class Expression
    • apply

      public void apply(CallContext ctx) throws Throwable
      Description copied from class: Expression
      Evaluate the expression. This is named apply rather than eval so it is compatible with the full-tail-call calling convention, and we can stash an Expression in CallContext's proc field. FIXME - are we making use of this?
      Overrides:
      apply in class Expression
      Throws:
      Throwable
    • validateApply

      public Expression validateApply(ApplyExp exp, InlineCalls visitor, Type required, Declaration decl)
      Description copied from class: Expression
      Apply inlining transformations on a given ApplyExp. Assumes the ApplyExp's function is this expression, or can be optimized to this expression.
      Overrides:
      validateApply in class Expression
      Parameters:
      exp - an application whose function expression can be simplified to this expression.
      visitor - the context for the current inlining pass
      decl - if non-null, a Declaration bound to this expression.
      Returns:
      an Expression equivalent to the passed-in exp.
    • print

      public void print(gnu.kawa.io.OutPort out)
      Specified by:
      print in class Expression
    • getExpClassName

      protected final String getExpClassName()
    • side_effects

      public boolean side_effects()
      Description copied from class: Expression
      True if evaluating may have side-effects.
      Overrides:
      side_effects in class Expression
    • toString

      public String toString()
      Overrides:
      toString in class ScopeExp
    • getProperty

      public Object getProperty(Object key, Object defaultValue)
      Overrides:
      getProperty in class PropertySet
    • setProperty

      public void setProperty(Object key, Object value)
      Overrides:
      setProperty in class PropertySet
    • getReturnType

      public final Type getReturnType()
      The return type of this function, i.e the type of its returned values.
    • setReturnType

      public final void setReturnType(Type returnType)
    • setCoercedReturnType

      public final void setCoercedReturnType(Type returnType)
    • maybeSetReturnType

      public static void maybeSetReturnType(LambdaExp lexp, Type type)
    • setCoercedReturnValue

      public final void setCoercedReturnValue(Expression type, Language language)
      Modify LambdaExp so result is coerced to given type.
    • getBodyFirstExpression

      public Expression getBodyFirstExpression()
      Get the first expression/statement in the body. It dives down into BeginExps. Used to check for invoke-special calls in @init methods.
    • checkForInitCall

      public ClassType checkForInitCall(Expression bodyFirst)
      Check if argument is a this(...) or super(...) initializtion. If so, return return the corresponding this or super class.