class
compiler::ClosureToImmutable
sys::Obj compiler::CompilerSupport compiler::CompilerStep compiler::ClosureToImmutable
ClosureToImmutable processes each closure to determine its immutability. At this point, all the enclosed variables have been mapped to fields by ClosureVars. So we have three cases:
- If every field is known const, then the function is always immutable, and we can just override isImmutable to return true.
- If any field is known to never be const, then the function can never be immutable, and we just use Func defaults for isImmutable and toImmutable.
- In the last case we have fields like Obj or List which require us calling toImmutable. In this case we generate a toImmutable method which constructs a new closure instance by calling toImmutable on each field.
Slots
-
private Void genIsImmutable(TypeDef cls, Expr result)
Generate:
isImmutable() { return result }
-
private Void genToImmutable(TypeDef cls)
Generate toImmutable by attempting to construct a copy of this closure with toImmutable called on every field along with a flag to keep track of which state we are in.
Obj toImmutable() { r := make( (T1)f1.toImmutable, ... ) r.isImmutable$ = true return true } Bool isImmutable() { immutable } private Bool immutable
- isAlwaysImmutableSource
-
Bool isAlwaysImmutable(TypeDef cls)
Are all the fields known to be const types?
- isNeverImmutableSource
-
Bool isNeverImmutable(TypeDef cls)
Are any of the fields known to never be immutable?
- makeSource
-
new make(Compiler compiler)
-
private Void process(ClosureExpr closure)
- runSource
-
override Void run()
Overrides compiler::CompilerStep.run
Doc inherited from compiler::CompilerStep.run
Run the step
- setAllFieldsConstSource
-
Void setAllFieldsConst(TypeDef cls)
Set const flag on every field def.