logo

class

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

make

new make(Compiler compiler)

Source

makeOuterThisField

static 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

Source

remapLocalVar

Expr remapLocalVar(LocalVarExpr local)

Source

run

override Void run()

Run the step

Source