@@ -410,6 +410,7 @@ class Objects(using Context @constructorOnly):
410
410
new LocalEnv (argMap, meth, outer)(valsMap = mutable.Map .empty, varsMap = mutable.Map .empty)
411
411
412
412
def ofDefDef (ddef : DefDef , args : List [Value ], outer : Data )(using Context ): Data =
413
+ // println(ddef.show)
413
414
val params = ddef.termParamss.flatten.map(_.symbol)
414
415
assert(args.size == params.size, " arguments = " + args.size + " , params = " + params.size)
415
416
assert(ddef.symbol.owner.isClass ^ (outer != NoEnv ), " ddef.owner = " + ddef.symbol.owner.show + " , outer = " + outer + " , " + ddef.source)
@@ -452,17 +453,43 @@ class Objects(using Context @constructorOnly):
452
453
*
453
454
* @return the environment and value for `this` owned by the given method.
454
455
*/
455
- def resolveEnv (meth : Symbol , thisV : ThisValue , env : Data )(using Context ): Option [(ThisValue , Data )] = log(" Resolving env for " + meth.show + " , this = " + thisV.show + " , env = " + env.show, printer) {
456
+ def _resolveEnvByValue (target : Symbol , thisV : ThisValue , env : Data )(using Context ): Option [(ThisValue , Data )] = log(" Resolving env for " + target.show + " , this = " + thisV.show + " , env = " + env.show, printer) {
457
+ env match
458
+ case localEnv : LocalEnv =>
459
+ if localEnv.getVal(target).isDefined then Some (thisV -> localEnv)
460
+ else if localEnv.getVar(target).isDefined then Some (thisV -> localEnv)
461
+ else _resolveEnvByValue(target, thisV, localEnv.outer)
462
+ case NoEnv =>
463
+ thisV match
464
+ case ref : OfClass =>
465
+ ref.outer match
466
+ case outer : ThisValue =>
467
+ _resolveEnvByValue(target, outer, ref.env)
468
+ case _ =>
469
+ // TODO: properly handle the case where ref.outer is ValueSet
470
+ None
471
+ case _ =>
472
+ None
473
+ }
474
+
475
+ def resolveEnvByValue (target : Symbol , thisV : ThisValue , env : Data )(using Context ): Option [(ThisValue , Data )] =
476
+ // println("the target: " + target.show + "its owner: " + target.owner.show + " the env: " + env.show)
477
+ val e = _resolveEnvByValue(target, thisV, env)
478
+ if e.isDefined then println(e.get._2.show)
479
+ else println(" NONE" )
480
+ e
481
+
482
+ def resolveEnvByOwner (meth : Symbol , thisV : ThisValue , env : Data )(using Context ): Option [(ThisValue , Data )] = log(" Resolving env for " + meth.show + " , this = " + thisV.show + " , env = " + env.show, printer) {
456
483
env match
457
484
case localEnv : LocalEnv =>
458
485
if localEnv.meth == meth then Some (thisV -> env)
459
- else resolveEnv (meth, thisV, localEnv.outer)
486
+ else resolveEnvByOwner (meth, thisV, localEnv.outer)
460
487
case NoEnv =>
461
488
thisV match
462
489
case ref : OfClass =>
463
490
ref.outer match
464
491
case outer : ThisValue =>
465
- resolveEnv (meth, outer, ref.env)
492
+ resolveEnvByOwner (meth, outer, ref.env)
466
493
case _ =>
467
494
// TODO: properly handle the case where ref.outer is ValueSet
468
495
None
@@ -725,7 +752,8 @@ class Objects(using Context @constructorOnly):
725
752
if meth.owner.isClass then
726
753
(ref, Env .NoEnv )
727
754
else
728
- Env .resolveEnv(meth.owner.enclosingMethod, ref, summon[Env .Data ]).getOrElse(Cold -> Env .NoEnv )
755
+ // println("1: " + meth.show + " env : " + summon[Env.Data].show)
756
+ Env .resolveEnvByOwner(meth.owner.enclosingMethod, ref, summon[Env .Data ]).getOrElse(Cold -> Env .NoEnv )
729
757
730
758
val env2 = Env .ofDefDef(ddef, args.map(_.value), outerEnv)
731
759
extendTrace(ddef) {
@@ -963,7 +991,8 @@ class Objects(using Context @constructorOnly):
963
991
(thisV.widenRefOrCold(1 ), Env .NoEnv )
964
992
else
965
993
// klass.enclosingMethod returns its primary constructor
966
- Env .resolveEnv(klass.owner.enclosingMethod, thisV, summon[Env .Data ]).getOrElse(Cold -> Env .NoEnv )
994
+ // println("callsite 2: " + klass.show + " env : " + summon[Env.Data].show)
995
+ Env .resolveEnvByOwner(klass.owner.enclosingMethod, thisV, summon[Env .Data ]).getOrElse(Cold -> Env .NoEnv )
967
996
968
997
val instance = OfClass (klass, outerWidened, ctor, args.map(_.value), envWidened)
969
998
callConstructor(instance, ctor, args)
@@ -983,7 +1012,6 @@ class Objects(using Context @constructorOnly):
983
1012
Env .setLocalVar(sym, addr)
984
1013
Heap .writeJoin(addr, value)
985
1014
else
986
- System .out.println(" initLocal sym: " + sym.show + " val: " + value.show + " data: " + data.show)
987
1015
Env .setLocalVal(sym, value)
988
1016
}
989
1017
@@ -994,7 +1022,8 @@ class Objects(using Context @constructorOnly):
994
1022
*/
995
1023
def readLocal (thisV : ThisValue , sym : Symbol ): Contextual [Value ] = log(" reading local " + sym.show, printer, (_ : Value ).show) {
996
1024
def isByNameParam (sym : Symbol ) = sym.is(Flags .Param ) && sym.info.isInstanceOf [ExprType ]
997
- Env .resolveEnv(sym.enclosingMethod, thisV, summon[Env .Data ]) match
1025
+ // println("callsite 3: " + sym.show + " env : " + summon[Env.Data].show)
1026
+ Env .resolveEnvByValue(sym, thisV, summon[Env .Data ]) match
998
1027
case Some (thisV -> env) =>
999
1028
if sym.is(Flags .Mutable ) then
1000
1029
// Assume forward reference check is doing a good job
@@ -1017,15 +1046,12 @@ class Objects(using Context @constructorOnly):
1017
1046
val rhs = sym.defTree.asInstanceOf [ValDef ].rhs
1018
1047
eval(rhs, thisV, sym.enclosingClass.asClass, cacheResult = true )
1019
1048
else
1020
- System .out.println(" env: " + env.show)
1021
- System .out.println(" sym: " + sym.show)
1022
1049
// Assume forward reference check is doing a good job
1023
1050
val value = Env .valValue(sym)
1024
1051
if isByNameParam(sym) then
1025
1052
value match
1026
1053
case fun : Fun =>
1027
1054
given Env .Data = Env .ofByName(sym, fun.env)
1028
- System .out.println(" created new env " + fun.code.show)
1029
1055
eval(fun.code, fun.thisV, fun.klass)
1030
1056
case Cold =>
1031
1057
report.warning(" Calling cold by-name alias. " + Trace .show, Trace .position)
@@ -1052,8 +1078,8 @@ class Objects(using Context @constructorOnly):
1052
1078
*/
1053
1079
def writeLocal (thisV : ThisValue , sym : Symbol , value : Value ): Contextual [Value ] = log(" write local " + sym.show + " with " + value.show, printer, (_ : Value ).show) {
1054
1080
assert(sym.is(Flags .Mutable ), " Writing to immutable variable " + sym.show)
1055
-
1056
- Env .resolveEnv (sym.enclosingMethod , thisV, summon[Env .Data ]) match
1081
+ // println("callsite 4: " + sym.show + " env : " + summon[Env.Data].show)
1082
+ Env .resolveEnvByValue (sym, thisV, summon[Env .Data ]) match
1057
1083
case Some (thisV -> env) =>
1058
1084
given Env .Data = env
1059
1085
Env .getVar(sym) match
@@ -1189,6 +1215,7 @@ class Objects(using Context @constructorOnly):
1189
1215
withTrace(trace2) { call(receiver, ref.symbol, args, receiver = qual.tpe, superType = NoType ) }
1190
1216
1191
1217
case id : Ident =>
1218
+ // println("ID : " + id.show)
1192
1219
id.tpe match
1193
1220
case TermRef (NoPrefix , _) =>
1194
1221
// resolve this for the local method
@@ -1258,6 +1285,7 @@ class Objects(using Context @constructorOnly):
1258
1285
Fun (ddef, thisV, klass, summon[Env .Data ])
1259
1286
1260
1287
case Block (stats, expr) =>
1288
+ // println("BLOCK DATA : " + expr.show)
1261
1289
evalExprs(stats, thisV, klass)
1262
1290
eval(expr, thisV, klass)
1263
1291
0 commit comments