@@ -132,7 +132,7 @@ object Objects:
132
132
valsMap : mutable.Map [Symbol , Value ], varsMap : mutable.Map [Symbol , Heap .Addr ], outersMap : mutable.Map [ClassSymbol , Value ])
133
133
extends Ref (valsMap, varsMap, outersMap):
134
134
def widenedCopy (outer : Value , args : List [Value ], env : Env .Data ): OfClass =
135
- new OfClass (klass, outer, ctor, args, env)(this .valsMap, this .varsMap, outersMap)
135
+ new OfClass (klass, outer, ctor, args, env)(this .valsMap, this .varsMap, this . outersMap)
136
136
137
137
def show (using Context ) =
138
138
val valFields = vals.map(_.show + " -> " + _.show)
@@ -379,6 +379,8 @@ object Objects:
379
379
* @param meth The method which owns the environment
380
380
* @param thisV The value for `this` of the enclosing class where the local variable is referenced.
381
381
* @param env The local environment where the local variable is referenced.
382
+ *
383
+ * @return the environment and value for `this` owned by the given method.
382
384
*/
383
385
def resolveEnv (meth : Symbol , thisV : Value , env : Data )(using Context ): Option [(Value , Data )] = log(" Resolving env for " + meth.show + " , this = " + thisV.show + " , env = " + env.show, printer) {
384
386
env match
@@ -528,6 +530,15 @@ object Objects:
528
530
529
531
def widen (height : Int ): Contextual [List [Value ]] = values.map(_.widen(height)).toList
530
532
533
+ /** Handle method calls `e.m(args)`.
534
+ *
535
+ * @param value The value for the receiver.
536
+ * @param meth The symbol of the target method (could be virtual or abstract method).
537
+ * @param args Arguments of the method call (all parameter blocks flatten to a list).
538
+ * @param receiver The type of the receiver.
539
+ * @param superType The type of the super in a super call. NoType for non-super calls.
540
+ * @param needResolve Whether the target of the call needs resolution?
541
+ */
531
542
def call (value : Value , meth : Symbol , args : List [ArgInfo ], receiver : Type , superType : Type , needResolve : Boolean = true ): Contextual [Value ] = log(" call " + meth.show + " , args = " + args.map(_.value.show), printer, (_ : Value ).show) {
532
543
value match
533
544
case Cold =>
@@ -618,6 +629,12 @@ object Objects:
618
629
vs.map(v => call(v, meth, args, receiver, superType)).join
619
630
}
620
631
632
+ /** Handle constructor calls `<init>(args)`.
633
+ *
634
+ * @param thisV The value for the receiver.
635
+ * @param ctor The symbol of the target method.
636
+ * @param args Arguments of the constructor call (all parameter blocks flatten to a list).
637
+ */
621
638
def callConstructor (thisV : Value , ctor : Symbol , args : List [ArgInfo ]): Contextual [Value ] = log(" call " + ctor.show + " , args = " + args.map(_.value.show), printer, (_ : Value ).show) {
622
639
623
640
thisV match
@@ -643,6 +660,13 @@ object Objects:
643
660
Bottom
644
661
}
645
662
663
+ /** Handle selection `e.f`.
664
+ *
665
+ * @param value The value for the receiver.
666
+ * @param field The symbol of the target field (could be virtual or abstract).
667
+ * @param receiver The type of the receiver.
668
+ * @param needResolve Whether the target of the selection needs resolution?
669
+ */
646
670
def select (thisV : Value , field : Symbol , receiver : Type , needResolve : Boolean = true ): Contextual [Value ] = log(" select " + field.show + " , this = " + thisV.show, printer, (_ : Value ).show) {
647
671
thisV match
648
672
case Cold =>
@@ -700,8 +724,15 @@ object Objects:
700
724
refs.map(ref => select(ref, field, receiver)).join
701
725
}
702
726
703
- def assign (receiver : Value , field : Symbol , rhs : Value , rhsTyp : Type ): Contextual [Value ] = log(" Assign" + field.show + " of " + receiver.show + " , rhs = " + rhs.show, printer, (_ : Value ).show) {
704
- receiver match
727
+ /** Handle assignment `lhs.f = rhs`.
728
+ *
729
+ * @param lhs The value of the object to be mutated.
730
+ * @param field The symbol of the target field.
731
+ * @param lhs The value to be assigned.
732
+ * @param rhsTyp The type of the right-hand side.
733
+ */
734
+ def assign (lhs : Value , field : Symbol , rhs : Value , rhsTyp : Type ): Contextual [Value ] = log(" Assign" + field.show + " of " + lhs.show + " , rhs = " + rhs.show, printer, (_ : Value ).show) {
735
+ lhs match
705
736
case fun : Fun =>
706
737
report.error(" [Internal error] unexpected tree in assignment, fun = " + fun.code.show + Trace .show, Trace .position)
707
738
@@ -727,6 +758,13 @@ object Objects:
727
758
Bottom
728
759
}
729
760
761
+ /** Handle new expression `new p.C(args)`.
762
+ *
763
+ * @param outer The value for `p`.
764
+ * @param klass The symbol of the class `C`.
765
+ * @param ctor The symbol of the target constructor.
766
+ * @param args The arguments passsed to the constructor.
767
+ */
730
768
def instantiate (outer : Value , klass : ClassSymbol , ctor : Symbol , args : List [ArgInfo ]): Contextual [Value ] = log(" instantiating " + klass.show + " , outer = " + outer + " , args = " + args.map(_.value.show), printer, (_ : Value ).show) {
731
769
outer match
732
770
@@ -758,6 +796,12 @@ object Objects:
758
796
refs.map(ref => instantiate(ref, klass, ctor, args)).join
759
797
}
760
798
799
+ /** Handle local variable definition, `val x = e` or `var x = e`.
800
+ *
801
+ * @param ref The value for `this` where the variable is defined.
802
+ * @param sym The symbol of the variable.
803
+ * @param value The value of the initializer.
804
+ */
761
805
def initLocal (ref : Ref , sym : Symbol , value : Value ): Contextual [Unit ] = log(" initialize local " + sym.show + " with " + value.show, printer) {
762
806
if sym.is(Flags .Mutable ) then
763
807
val addr = Heap .localVarAddr(summon[Regions .Data ], sym, State .currentObject)
@@ -767,6 +811,11 @@ object Objects:
767
811
Env .setLocalVal(sym, value)
768
812
}
769
813
814
+ /** Read local variable `x`.
815
+ *
816
+ * @param thisV The value for `this` where the variable is used.
817
+ * @param sym The symbol of the variable.
818
+ */
770
819
def readLocal (thisV : Value , sym : Symbol ): Contextual [Value ] = log(" reading local " + sym.show, printer, (_ : Value ).show) {
771
820
Env .resolveEnv(sym.enclosingMethod, thisV, summon[Env .Data ]) match
772
821
case Some (thisV -> env) =>
@@ -814,6 +863,12 @@ object Objects:
814
863
Cold
815
864
}
816
865
866
+ /** Handle local variable assignmenbt, `x = e`.
867
+ *
868
+ * @param thisV The value for `this` where the assignment locates.
869
+ * @param sym The symbol of the variable.
870
+ * @param value The value of the rhs of the assignment.
871
+ */
817
872
def writeLocal (thisV : Value , sym : Symbol , value : Value ): Contextual [Value ] = log(" write local " + sym.show + " with " + value.show, printer, (_ : Value ).show) {
818
873
819
874
assert(sym.is(Flags .Mutable ), " Writing to immutable variable " + sym.show)
@@ -1010,7 +1065,8 @@ object Objects:
1010
1065
eval(expr, thisV, klass)
1011
1066
1012
1067
case If (cond, thenp, elsep) =>
1013
- evalExprs(cond :: thenp :: elsep :: Nil , thisV, klass).join
1068
+ eval(cond, thisV, klass)
1069
+ evalExprs(thenp :: elsep :: Nil , thisV, klass).join
1014
1070
1015
1071
case Annotated (arg, annot) =>
1016
1072
if expr.tpe.hasAnnotation(defn.UncheckedAnnot ) then
@@ -1309,7 +1365,7 @@ object Objects:
1309
1365
Bottom
1310
1366
else if target.isStaticObject then
1311
1367
val res = ObjectRef (target.moduleClass.asClass)
1312
- if target == klass || elideObjectAccess then res
1368
+ if elideObjectAccess then res
1313
1369
else accessObject(target)
1314
1370
else
1315
1371
thisV match
0 commit comments