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 class
LambdaExp.Closure
-
Nested classes/interfaces inherited from interface gnu.text.SourceLocator
SourceLocator.Simple
-
-
Field Summary
Fields Modifier and Type Field Description static int
ALLOW_OTHER_KEYWORDS
static MethodHandle
applyToConsumer
static int
ATTEMPT_INLINE
Expression
body
static String
CLOSURE_ENV_NAME
Field
closureEnvField
If non-null, this is a Field that is used for implementing lexical closures.LambdaExp
firstChild
protected static int
HAS_NONTRIVIAL_DEFAULT
True if IS_SUPPLIED_PARAMETER is set on a parameter.protected static int
HAS_NONTRIVIAL_PATTERN
static int
IN_EXPWALKER
LambdaExp
inlineHome
If this lambda gets inlined this is the containing lambda.Keyword[]
keywords
int
max_args
Maximum number of actual arguments; -1 if variable.int
min_args
Minimum number of actual arguments.Declaration
nameDecl
If non-null, a Declaration whose value is (only) this LambdaExp.protected static int
NEXT_AVAIL_FLAG
LambdaExp
nextSibling
static int
NO_FIELD
int
opt_args
Number of optional arguments, not counting keyword arguments.static int
OVERLOADABLE_FIELD
static int
PASSES_TAILCALLS
Treat as inlined in outer lambda when determining tailcalls.static int
PUBLIC_METHOD
True of emitted method should be public.Expression
returnContinuation
The unique call site that calls this lambda.Type
returnType
If non-null, the type of values returned by this function.static int
SEQUENCE_RESULT
Field
staticLinkField
Field 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 Declaration
addParameter(Object name)
void
allocChildClasses(Compilation comp)
void
allocFrame(Compilation comp)
void
apply(CallContext ctx)
Evaluate the expression.protected Type
calculateType()
void
capture(Declaration decl)
ClassType
checkForInitCall(Expression bodyFirst)
Check if argument is a this(...) or super(...) initializtion.void
compile(Compilation comp, Target target)
void
compileBody(Compilation comp)
void
compileEnd(Compilation comp)
Field
compileSetField(Compilation comp)
Variable
declareClosureEnv()
Variable
declareThis(ClassType clas)
void
generateApplyMethods(Compilation comp)
Expression
getBodyFirstExpression()
Get the first expression/statement in the body.int
getCallConvention()
The calling convention used for this function.LambdaExp
getCaller()
For an INLINE_ONLY function, return the function it gets inlined in.boolean
getCanRead()
ClassType
getClassType()
The ClassType generated for this class.protected ClassType
getCompiledClassType(Compilation comp)
Return the ClassType of the Procedure this is being compiled into.protected String
getExpClassName()
ClassType
getHeapFrameType()
boolean
getImportsLexVars()
True iff this lambda "captures" (uses) lexical variables from outside.boolean
getInlineOnly()
True iff this lambda is only "called" inline.Method
getMainMethod()
Get the method that contains the actual body of the procedure.Method
getMethod(int nonSpliceCount, int spliceCount)
Select the method used given an argument count.boolean
getNeedsClosureEnv()
boolean
getNeedsStaticLink()
True if a child lambda uses lexical variables from outside.LambdaExp
getOwningLambda()
Object
getProperty(Object key, Object defaultValue)
Type
getReturnType()
The return type of this function, i.e the type of its returned values.int
incomingArgs()
Number of argument variable actually passed by the caller.boolean
inlinedInCallerOrCheckMethodOnly()
True if no primitive method is created for this procedure.boolean
inlinedInCheckMethod()
boolean
isAbstract()
boolean
isClassMethod()
True if this is a method in an ClassExp.boolean
isHandlingTailCalls()
boolean
isModuleBody()
True iff this is the dummy top-level function of a module body.boolean
isNative()
void
loadHeapFrame(Compilation comp)
Generate code to load heapFrame on the JVM stack.static void
maybeSetReturnType(LambdaExp lexp, Type type)
protected boolean
mustCompile()
LambdaExp
outerLambda()
LambdaExp
outerLambdaNotInline()
Return the closest outer non-inlined LambdaExp.LambdaExp
outerLambdaOrCaller()
void
print(gnu.kawa.io.OutPort out)
void
pushChild(LambdaExp child)
Type
restArgType()
Return the parameter type of the "keyword/rest" parameters.void
reverseChildList()
void
setCallConvention(Compilation comp)
void
setCanRead(boolean read)
void
setClassMethod(boolean isMethod)
void
setCoercedReturnType(Type returnType)
void
setCoercedReturnValue(Expression type, Language language)
Modify LambdaExp so result is coerced to given type.void
setExceptions(Expression[] exceptions)
void
setImportsLexVars()
void
setImportsLexVars(boolean importsLexVars)
void
setInlineOnly(boolean inlineOnly)
void
setInlineOnly(Expression returnContinuation, LambdaExp caller)
Note this function is inlined in a give context.void
setNeedsStaticLink()
void
setNeedsStaticLink(boolean needsStaticLink)
void
setProperty(Object key, Object value)
void
setReturnType(Type returnType)
void
setType(ClassType type)
boolean
side_effects()
True if evaluating may have side-effects.String
toString()
boolean
usingCallContext()
Expression
validateApply(ApplyExp exp, InlineCalls visitor, Type required, Declaration decl)
Apply inlining transformations on a given ApplyExp.boolean
variable_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:
calculateType
in 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:
compile
in 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:
visitChildren
in 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:
mustCompile
in classExpression
-
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 classExpression
- 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 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:
print
in classExpression
-
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 classExpression
-
getProperty
public Object getProperty(Object key, Object defaultValue)
- Overrides:
getProperty
in classPropertySet
-
setProperty
public void setProperty(Object key, Object value)
- Overrides:
setProperty
in 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 intoBeginExp
s. Used to check forinvoke-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.
-
-