Skip to content

Commit d86190f

Browse files
author
EnzeXing
committed
Adding ValueElement and refining type for thisV
1 parent 889c208 commit d86190f

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ object Objects:
6969
sealed abstract class Value:
7070
def show(using Context): String
7171

72+
sealed abstract class ValueElement extends Value
7273

7374
/**
7475
* A reference caches the values for outers and immutable fields.
@@ -77,7 +78,7 @@ object Objects:
7778
valsMap: mutable.Map[Symbol, Value],
7879
varsMap: mutable.Map[Symbol, Heap.Addr],
7980
outersMap: mutable.Map[ClassSymbol, Value])
80-
extends Value:
81+
extends ValueElement:
8182
protected val vals: mutable.Map[Symbol, Value] = valsMap
8283
protected val vars: mutable.Map[Symbol, Heap.Addr] = varsMap
8384
protected val outers: mutable.Map[ClassSymbol, Value] = outersMap
@@ -170,7 +171,7 @@ object Objects:
170171
/**
171172
* Represents a lambda expression
172173
*/
173-
case class Fun(code: Tree, thisV: Value, klass: ClassSymbol, env: Env.Data) extends Value:
174+
case class Fun(code: Tree, thisV: ValueElement, klass: ClassSymbol, env: Env.Data) extends Value:
174175
def show(using Context) = "Fun(" + code.show + ", " + thisV.show + ", " + klass.show + ")"
175176

176177
/**
@@ -183,7 +184,7 @@ object Objects:
183184
def show(using Context) = refs.map(_.show).mkString("[", ",", "]")
184185

185186
/** A cold alias which should not be used during initialization. */
186-
case object Cold extends Value:
187+
case object Cold extends ValueElement:
187188
def show(using Context) = "Cold"
188189

189190
val Bottom = RefSet(ListSet.empty)
@@ -240,7 +241,7 @@ object Objects:
240241
obj
241242
end doCheckObject
242243

243-
def checkObjectAccess(clazz: ClassSymbol)(using data: Data, ctx: Context, pendingTrace: Trace): Value =
244+
def checkObjectAccess(clazz: ClassSymbol)(using data: Data, ctx: Context, pendingTrace: Trace): ObjectRef =
244245
val index = data.checkingObjects.indexOf(ObjectRef(clazz))
245246

246247
if index != -1 then
@@ -466,7 +467,7 @@ object Objects:
466467
val config = Config(thisV, summon[Env.Data], Heap.getHeapData())
467468
super.get(config, expr).map(_.value)
468469

469-
def cachedEval(thisV: Value, expr: Tree, cacheResult: Boolean)(fun: Tree => Value)(using Heap.MutableData, Env.Data): Value =
470+
def cachedEval(thisV: ValueElement, expr: Tree, cacheResult: Boolean)(fun: Tree => Value)(using Heap.MutableData, Env.Data): Value =
470471
val config = Config(thisV, summon[Env.Data], Heap.getHeapData())
471472
val result = super.cachedEval(config, expr, cacheResult, default = Res(Bottom, Heap.getHeapData())) { expr =>
472473
Res(fun(expr), Heap.getHeapData())
@@ -542,7 +543,7 @@ object Objects:
542543
refs.map(ref => ref.widen(height)).join
543544

544545
case Fun(code, thisV, klass, env) =>
545-
Fun(code, thisV.widen(height), klass, env.widen(height))
546+
Fun(code, thisV.widen(height).asInstanceOf[ValueElement], klass, env.widen(height))
546547

547548
case ref @ OfClass(klass, outer, _, args, env) =>
548549
val outer2 = outer.widen(height - 1)
@@ -619,13 +620,14 @@ object Objects:
619620
else
620621
Env.resolveEnv(meth.owner.enclosingMethod, ref, summon[Env.Data]).getOrElse(Cold -> Env.NoEnv)
621622

623+
assert(thisV.isInstanceOf[ValueElement], "thisV = " + thisV)
622624
val env2 = Env.of(ddef, args.map(_.value), outerEnv)
623625
extendTrace(ddef) {
624626
given Env.Data = env2
625627
// eval(ddef.rhs, ref, cls, cacheResult = true)
626628
cache.cachedEval(ref, ddef.rhs, cacheResult = true) { expr =>
627629
Returns.installHandler(meth)
628-
val res = cases(expr, thisV, cls)
630+
val res = cases(expr, thisV.asInstanceOf[ValueElement], cls)
629631
val returns = Returns.popHandler(meth)
630632
res.join(returns)
631633
}
@@ -699,8 +701,8 @@ object Objects:
699701
* @param receiver The type of the receiver.
700702
* @param needResolve Whether the target of the selection needs resolution?
701703
*/
702-
def select(thisV: Value, field: Symbol, receiver: Type, needResolve: Boolean = true): Contextual[Value] = log("select " + field.show + ", this = " + thisV.show, printer, (_: Value).show) {
703-
thisV match
704+
def select(value: Value, field: Symbol, receiver: Type, needResolve: Boolean = true): Contextual[Value] = log("select " + field.show + ", this = " + value.show, printer, (_: Value).show) {
705+
value match
704706
case Cold =>
705707
report.warning("Using cold alias", Trace.position)
706708
Bottom
@@ -814,11 +816,11 @@ object Objects:
814816
else
815817
// Widen the outer to finitize the domain. Arguments already widened in `evalArgs`.
816818
val (outerWidened, envWidened) =
817-
if klass.owner.isClass then
819+
if klass.owner.isClass then // For top-level classes, klass.owner is the enclosing package, which is a class
818820
(outer.widen(1), Env.NoEnv)
819821
else
820822
// klass.enclosingMethod returns its primary constructor
821-
Env.resolveEnv(klass.owner.enclosingMethod, outer, summon[Env.Data]).getOrElse(Cold -> Env.NoEnv)
823+
Env.resolveEnv(klass.owner.enclosingMethod, outer.asInstanceOf[ValueElement], summon[Env.Data]).getOrElse(Cold -> Env.NoEnv)
822824

823825
val instance = OfClass(klass, outerWidened, ctor, args.map(_.value), envWidened)
824826
callConstructor(instance, ctor, args)
@@ -848,7 +850,7 @@ object Objects:
848850
* @param thisV The value for `this` where the variable is used.
849851
* @param sym The symbol of the variable.
850852
*/
851-
def readLocal(thisV: Value, sym: Symbol): Contextual[Value] = log("reading local " + sym.show, printer, (_: Value).show) {
853+
def readLocal(thisV: ValueElement, sym: Symbol): Contextual[Value] = log("reading local " + sym.show, printer, (_: Value).show) {
852854
def isByNameParam(sym: Symbol) = sym.is(Flags.Param) && sym.info.isInstanceOf[ExprType]
853855
Env.resolveEnv(sym.enclosingMethod, thisV, summon[Env.Data]) match
854856
case Some(thisV -> env) =>
@@ -902,7 +904,7 @@ object Objects:
902904
* @param sym The symbol of the variable.
903905
* @param value The value of the rhs of the assignment.
904906
*/
905-
def writeLocal(thisV: Value, sym: Symbol, value: Value): Contextual[Value] = log("write local " + sym.show + " with " + value.show, printer, (_: Value).show) {
907+
def writeLocal(thisV: ValueElement, sym: Symbol, value: Value): Contextual[Value] = log("write local " + sym.show + " with " + value.show, printer, (_: Value).show) {
906908

907909
assert(sym.is(Flags.Mutable), "Writing to immutable variable " + sym.show)
908910
Env.resolveEnv(sym.enclosingMethod, thisV, summon[Env.Data]) match
@@ -960,13 +962,14 @@ object Objects:
960962
* @param klass The enclosing class where the expression is located.
961963
* @param cacheResult It is used to reduce the size of the cache.
962964
*/
963-
def eval(expr: Tree, thisV: Value, klass: ClassSymbol, cacheResult: Boolean = false): Contextual[Value] = log("evaluating " + expr.show + ", this = " + thisV.show + ", regions = " + Regions.show + " in " + klass.show, printer, (_: Value).show) {
965+
def eval(expr: Tree, thisV: ValueElement, klass: ClassSymbol, cacheResult: Boolean = false): Contextual[Value] = log("evaluating " + expr.show + ", this = " + thisV.show + ", regions = " + Regions.show + " in " + klass.show, printer, (_: Value).show) {
966+
// assert(thisV.isInstanceOf[ValueElement], "thisV is not ValueElement, " + "evaluating " + expr.show + ", this = " + thisV.show)
964967
cache.cachedEval(thisV, expr, cacheResult) { expr => cases(expr, thisV, klass) }
965968
}
966969

967970

968971
/** Evaluate a list of expressions */
969-
def evalExprs(exprs: List[Tree], thisV: Value, klass: ClassSymbol): Contextual[List[Value]] =
972+
def evalExprs(exprs: List[Tree], thisV: ValueElement, klass: ClassSymbol): Contextual[List[Value]] =
970973
exprs.map { expr => eval(expr, thisV, klass) }
971974

972975
/** Handles the evaluation of different expressions
@@ -977,7 +980,7 @@ object Objects:
977980
* @param thisV The value for `C.this` where `C` is represented by the parameter `klass`.
978981
* @param klass The enclosing class where the expression `expr` is located.
979982
*/
980-
def cases(expr: Tree, thisV: Value, klass: ClassSymbol): Contextual[Value] = log("evaluating " + expr.show + ", this = " + thisV.show + " in " + klass.show, printer, (_: Value).show) {
983+
def cases(expr: Tree, thisV: ValueElement, klass: ClassSymbol): Contextual[Value] = log("evaluating " + expr.show + ", this = " + thisV.show + " in " + klass.show, printer, (_: Value).show) {
981984
val trace2 = trace.add(expr)
982985

983986
expr match
@@ -1179,7 +1182,7 @@ object Objects:
11791182
* Object access elission happens when the object access is used as a prefix
11801183
* in `new o.C` and `C` does not need an outer.
11811184
*/
1182-
def evalType(tp: Type, thisV: Value, klass: ClassSymbol, elideObjectAccess: Boolean = false): Contextual[Value] = log("evaluating " + tp.show, printer, (_: Value).show) {
1185+
def evalType(tp: Type, thisV: ValueElement, klass: ClassSymbol, elideObjectAccess: Boolean = false): Contextual[Value] = log("evaluating " + tp.show, printer, (_: Value).show) {
11831186
tp match
11841187
case _: ConstantType =>
11851188
Bottom
@@ -1229,7 +1232,7 @@ object Objects:
12291232
}
12301233

12311234
/** Evaluate arguments of methods and constructors */
1232-
def evalArgs(args: List[Arg], thisV: Value, klass: ClassSymbol): Contextual[List[ArgInfo]] =
1235+
def evalArgs(args: List[Arg], thisV: ValueElement, klass: ClassSymbol): Contextual[List[ArgInfo]] =
12331236
val argInfos = new mutable.ArrayBuffer[ArgInfo]
12341237
args.foreach { arg =>
12351238
val res =
@@ -1429,7 +1432,7 @@ object Objects:
14291432
* @param thisV The value for `C.this` where `C` is represented by the parameter `klass`.
14301433
* @param klass The enclosing class where the type `tref` is located.
14311434
*/
1432-
def outerValue(tref: TypeRef, thisV: Value, klass: ClassSymbol): Contextual[Value] =
1435+
def outerValue(tref: TypeRef, thisV: ValueElement, klass: ClassSymbol): Contextual[Value] =
14331436
val cls = tref.classSymbol.asClass
14341437
if tref.prefix == NoPrefix then
14351438
val enclosing = cls.owner.lexicallyEnclosingClass.asClass

0 commit comments

Comments
 (0)