compiler::ClosureVars
sys::Obj
compiler::CompilerSupport
compiler::CompilerStep
compiler::ClosureVars
ClosureVars (cvars) is used to pull local variables used by closures into an auto-generated anonymous class so that they can be used outside and inside the closure as well as have a lifetime once the method returns:
1) scan for methods with have locals used by their closures 3) define the cvars class 4) remove method vars which are stored in cvars 5) walk the method body a) remap local var access to cvars field access b) accumulate all the ClosureExprs 6) walk accumlated ClosureExprs in method body a) add $cvars field to closure implementation class b) add $cvars parameter to implementation class constructor c) pass $cvars arg from method body to implementation constructor d) walk implementation class code and remap local var access 7) decide if closure is thread-safe or not and mark isConst
Note: this same process is used to process nested closure doCall methods
too; but they do things a bit differently since they always share the outmost method's cvars.
Slots
-
private ClosureExpr[] closures
-
private TypeDef cvars
-
private MethodDef cvarsCtor
-
private MethodVar cvarsLocal
-
private Void defineCvarsClass()
Walk the current method and create the cvars class, a default constructor, and a field for every local variable used inside closures. If this is a closure itself, then we just reuse the cvar class of the outer most method.
-
static const private FieldExpr fieldExpr(Location loc, Expr target, CField field)
-
private Bool inClosure
-
private Void insertCvarsInit()
-
private Location location
- make
-
new make(Compiler compiler)
- makeOuterThisField
-
static const CField makeOuterThisField(ClosureExpr closure)
This method is called by ClosureExpr to auto-generate the implicit outer "this" field in the Closure's implementation class:
- add $this field to closure's anonymous class
- add $this param to closure's make constructor
- set field from param in constructor
- update substitute to make sure this is passed to ctor
-
static const private Void markMutable(ClosureExpr c)
-
private MethodDef method
- remapLocalVar
-
Expr remapLocalVar(LocalVarExpr local)
-
private Void remapVarInClosure(ClosureExpr closure)
-
private Void remapVarsInClosures()
-
private Void remapVarsInMethod()
-
private Void reorderVars()
Once all the variables of a method body have been processed into cvars fields, this method strips out any non-parameter locals and optimally reorders them. We return the local variable to use for the cvar reference itself.
- run
-
virtual Void run()
-
static const private Int syntheticFieldFlags
-
static const private Str toCvarsTypeName(TypeDef t, MethodDef m)