Package gnu.expr
Class LambdaExp
- java.lang.Object
-
- gnu.mapping.PropertySet
-
- gnu.mapping.Procedure
-
- gnu.expr.Expression
-
- gnu.expr.ScopeExp
-
- gnu.expr.LambdaExp
-
- All Implemented Interfaces:
gnu.kawa.format.Printable,Named,SourceLocator,SourceLocator,Locator
public class LambdaExp extends ScopeExp
Class used to implement Scheme lambda expressions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLambdaExp.Closure-
Nested classes/interfaces inherited from interface gnu.text.SourceLocator
SourceLocator.Simple
-
-
Field Summary
Fields Modifier and Type Field Description static intALLOW_OTHER_KEYWORDSstatic MethodHandleapplyToConsumerstatic intATTEMPT_INLINEExpressionbodystatic StringCLOSURE_ENV_NAMEFieldclosureEnvFieldIf non-null, this is a Field that is used for implementing lexical closures.LambdaExpfirstChildprotected static intHAS_NONTRIVIAL_DEFAULTTrue if IS_SUPPLIED_PARAMETER is set on a parameter.protected static intHAS_NONTRIVIAL_PATTERNstatic intIN_EXPWALKERLambdaExpinlineHomeIf this lambda gets inlined this is the containing lambda.Keyword[]keywordsintmax_argsMaximum number of actual arguments; -1 if variable.intmin_argsMinimum number of actual arguments.DeclarationnameDeclIf non-null, a Declaration whose value is (only) this LambdaExp.protected static intNEXT_AVAIL_FLAGLambdaExpnextSiblingstatic intNO_FIELDintopt_argsNumber of optional arguments, not counting keyword arguments.static intOVERLOADABLE_FIELDstatic intPASSES_TAILCALLSTreat as inlined in outer lambda when determining tailcalls.static intPUBLIC_METHODTrue of emitted method should be public.ExpressionreturnContinuationThe unique call site that calls this lambda.TypereturnTypeIf non-null, the type of values returned by this function.static intSEQUENCE_RESULTFieldstaticLinkFieldField in heapFrame.getType() that contains the static link.-
Fields inherited from class gnu.expr.Expression
applyMethodExpression, flags, noExpressions, type, VALIDATED
-
Fields inherited from class gnu.mapping.Procedure
applyMethodType, applyToConsumerDefault, applyToConsumerMethod, applyToObjectDefault, applyToObjectMethod, compilerKey, compilerXKey, inlineIfConstantSymbol, validateApplyKey, validateXApplyKey
-
Fields inherited from class gnu.mapping.PropertySet
nameKey
-
-
Constructor Summary
Constructors Constructor Description LambdaExp()LambdaExp(int args)LambdaExp(Expression body)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DeclarationaddParameter(Object name)voidallocChildClasses(Compilation comp)voidallocFrame(Compilation comp)voidapply(CallContext ctx)Evaluate the expression.protected TypecalculateType()voidcapture(Declaration decl)ClassTypecheckForInitCall(Expression bodyFirst)Check if argument is a this(...) or super(...) initializtion.voidcompile(Compilation comp, Target target)voidcompileBody(Compilation comp)voidcompileEnd(Compilation comp)FieldcompileSetField(Compilation comp)VariabledeclareClosureEnv()VariabledeclareThis(ClassType clas)voidgenerateApplyMethods(Compilation comp)ExpressiongetBodyFirstExpression()Get the first expression/statement in the body.intgetCallConvention()The calling convention used for this function.LambdaExpgetCaller()For an INLINE_ONLY function, return the function it gets inlined in.booleangetCanRead()ClassTypegetClassType()The ClassType generated for this class.protected ClassTypegetCompiledClassType(Compilation comp)Return the ClassType of the Procedure this is being compiled into.protected StringgetExpClassName()ClassTypegetHeapFrameType()booleangetImportsLexVars()True iff this lambda "captures" (uses) lexical variables from outside.booleangetInlineOnly()True iff this lambda is only "called" inline.MethodgetMainMethod()Get the method that contains the actual body of the procedure.MethodgetMethod(int nonSpliceCount, int spliceCount)Select the method used given an argument count.booleangetNeedsClosureEnv()booleangetNeedsStaticLink()True if a child lambda uses lexical variables from outside.LambdaExpgetOwningLambda()ObjectgetProperty(Object key, Object defaultValue)TypegetReturnType()The return type of this function, i.e the type of its returned values.intincomingArgs()Number of argument variable actually passed by the caller.booleaninlinedInCallerOrCheckMethodOnly()True if no primitive method is created for this procedure.booleaninlinedInCheckMethod()booleanisAbstract()booleanisClassMethod()True if this is a method in an ClassExp.booleanisHandlingTailCalls()booleanisModuleBody()True iff this is the dummy top-level function of a module body.booleanisNative()voidloadHeapFrame(Compilation comp)Generate code to load heapFrame on the JVM stack.static voidmaybeSetReturnType(LambdaExp lexp, Type type)protected booleanmustCompile()LambdaExpouterLambda()LambdaExpouterLambdaNotInline()Return the closest outer non-inlined LambdaExp.LambdaExpouterLambdaOrCaller()voidprint(gnu.kawa.io.OutPort out)voidpushChild(LambdaExp child)TyperestArgType()Return the parameter type of the "keyword/rest" parameters.voidreverseChildList()voidsetCallConvention(Compilation comp)voidsetCanRead(boolean read)voidsetClassMethod(boolean isMethod)voidsetCoercedReturnType(Type returnType)voidsetCoercedReturnValue(Expression type, Language language)Modify LambdaExp so result is coerced to given type.voidsetExceptions(Expression[] exceptions)voidsetImportsLexVars()voidsetImportsLexVars(boolean importsLexVars)voidsetInlineOnly(boolean inlineOnly)voidsetInlineOnly(Expression returnContinuation, LambdaExp caller)Note this function is inlined in a give context.voidsetNeedsStaticLink()voidsetNeedsStaticLink(boolean needsStaticLink)voidsetProperty(Object key, Object value)voidsetReturnType(Type returnType)voidsetType(ClassType type)booleanside_effects()True if evaluating may have side-effects.StringtoString()booleanusingCallContext()ExpressionvalidateApply(ApplyExp exp, InlineCalls visitor, Type required, Declaration decl)Apply inlining transformations on a given ApplyExp.booleanvariable_args()protected <R,D>
Rvisit(ExpVisitor<R,D> visitor, D d)protected <R,D>
voidvisitChildren(ExpVisitor<R,D> visitor, D d)protected <R,D>
voidvisitChildrenOnly(ExpVisitor<R,D> visitor, D d)protected <R,D>
voidvisitProperties(ExpVisitor<R,D> visitor, D d)-
Methods inherited from class gnu.expr.ScopeExp
add, add, addDeclaration, addDeclaration, addDeclaration, clearCallList, countDecls, currentLambda, currentModule, duplicateDeclarationError, firstDecl, getDefine, getNoDefine, getOuter, getVarScope, isClassGenerated, lastDecl, lookup, lookup, nestedIn, nesting, popScope, remove, remove, replaceFollowing, setIndexes, setOuter, topLevel
-
Methods inherited from class gnu.expr.Expression
applyMethodExpression, checkLiteralKeyword, compile, compile, compileWithPosition, compileWithPosition, deepCopy, deepCopy, deepCopy, deepCopy, eval, eval, getColumnNumber, getEndColumn, getEndLine, getFileName, getFlag, getFlags, getLineNumber, getPublicId, getStartColumn, getStartLine, getSystemId, getType, getTypeRaw, isSingleValue, isStableSourceLocation, makeWhile, maybeSetLine, neverReturns, numArgs, print, printLineColumn, setFile, setFlag, setFlag, setLine, setLine, setLine, setLine, setLine, setLocation, setType, valueIfConstant
-
Methods inherited from class gnu.mapping.Procedure
apply0, apply1, apply2, apply3, apply4, applyL, applyN, applyToConsumerDefault, applyToObjectDefault, checkArgCount, checkBadCode, getApplyMethod, getApplyToConsumerMethod, getApplyToObjectMethod, getReturnType, getSetter, getSourceLocation, isSideEffectFree, lookupApplyHandle, maxArgs, maxArgs, minArgs, minArgs, set0, set1, setN, setSetter, setSourceLocation
-
Methods inherited from class gnu.mapping.PropertySet
getName, getSymbol, removeProperty, setName, setProperty, setSymbol
-
-
-
-
Field Detail
-
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:
- Constant Field Values
-
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:
- Constant Field Values
-
SEQUENCE_RESULT
public static final int SEQUENCE_RESULT
- See Also:
- Constant Field Values
-
OVERLOADABLE_FIELD
public static final int OVERLOADABLE_FIELD
- See Also:
- Constant Field Values
-
ATTEMPT_INLINE
public static final int ATTEMPT_INLINE
- See Also:
- Constant Field Values
-
IN_EXPWALKER
public static final int IN_EXPWALKER
- See Also:
- Constant Field Values
-
PASSES_TAILCALLS
public static final int PASSES_TAILCALLS
Treat as inlined in outer lambda when determining tailcalls.- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
ALLOW_OTHER_KEYWORDS
public static final int ALLOW_OTHER_KEYWORDS
- See Also:
- Constant Field Values
-
HAS_NONTRIVIAL_PATTERN
protected static final int HAS_NONTRIVIAL_PATTERN
- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
NEXT_AVAIL_FLAG
protected static final int NEXT_AVAIL_FLAG
- See Also:
- Constant Field Values
-
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 Detail
-
LambdaExp
public LambdaExp()
-
LambdaExp
public LambdaExp(int args)
-
LambdaExp
public LambdaExp(Expression body)
-
-
Method Detail
-
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 ifgetInlineOnly() || 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:
calculateTypein classExpression
-
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.
-
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:
compilein classExpression
-
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)
-
visitChildren
protected <R,D> void visitChildren(ExpVisitor<R,D> visitor, D d)
- Overrides:
visitChildrenin classExpression
-
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:
mustCompilein classExpression
-
apply
public void apply(CallContext ctx) throws Throwable
Description copied from class:ExpressionEvaluate 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:
applyin classExpression- Throws:
Throwable
-
validateApply
public Expression validateApply(ApplyExp exp, InlineCalls visitor, Type required, Declaration decl)
Description copied from class:ExpressionApply inlining transformations on a given ApplyExp. Assumes the ApplyExp's function is this expression, or can be optimized to this expression.- Overrides:
validateApplyin classExpression- Parameters:
exp- an application whose function expression can be simplified to this expression.visitor- the context for the current inlining passdecl- 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:
printin classExpression
-
getExpClassName
protected final String getExpClassName()
-
side_effects
public boolean side_effects()
Description copied from class:ExpressionTrue if evaluating may have side-effects.- Overrides:
side_effectsin classExpression
-
getProperty
public Object getProperty(Object key, Object defaultValue)
- Overrides:
getPropertyin classPropertySet
-
setProperty
public void setProperty(Object key, Object value)
- Overrides:
setPropertyin classPropertySet
-
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)
-
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 intoBeginExps. Used to check forinvoke-specialcalls in@initmethods.
-
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.
-
-