Skip to content

Commit 624dc40

Browse files
committed
Add documentation to code
1 parent b4d39d0 commit 624dc40

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

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

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ object Objects:
132132
valsMap: mutable.Map[Symbol, Value], varsMap: mutable.Map[Symbol, Heap.Addr], outersMap: mutable.Map[ClassSymbol, Value])
133133
extends Ref(valsMap, varsMap, outersMap):
134134
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)
136136

137137
def show(using Context) =
138138
val valFields = vals.map(_.show + " -> " + _.show)
@@ -379,6 +379,8 @@ object Objects:
379379
* @param meth The method which owns the environment
380380
* @param thisV The value for `this` of the enclosing class where the local variable is referenced.
381381
* @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.
382384
*/
383385
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) {
384386
env match
@@ -528,6 +530,15 @@ object Objects:
528530

529531
def widen(height: Int): Contextual[List[Value]] = values.map(_.widen(height)).toList
530532

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+
*/
531542
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) {
532543
value match
533544
case Cold =>
@@ -618,6 +629,12 @@ object Objects:
618629
vs.map(v => call(v, meth, args, receiver, superType)).join
619630
}
620631

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+
*/
621638
def callConstructor(thisV: Value, ctor: Symbol, args: List[ArgInfo]): Contextual[Value] = log("call " + ctor.show + ", args = " + args.map(_.value.show), printer, (_: Value).show) {
622639

623640
thisV match
@@ -643,6 +660,13 @@ object Objects:
643660
Bottom
644661
}
645662

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+
*/
646670
def select(thisV: Value, field: Symbol, receiver: Type, needResolve: Boolean = true): Contextual[Value] = log("select " + field.show + ", this = " + thisV.show, printer, (_: Value).show) {
647671
thisV match
648672
case Cold =>
@@ -700,8 +724,15 @@ object Objects:
700724
refs.map(ref => select(ref, field, receiver)).join
701725
}
702726

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
705736
case fun: Fun =>
706737
report.error("[Internal error] unexpected tree in assignment, fun = " + fun.code.show + Trace.show, Trace.position)
707738

@@ -727,6 +758,13 @@ object Objects:
727758
Bottom
728759
}
729760

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+
*/
730768
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) {
731769
outer match
732770

@@ -758,6 +796,12 @@ object Objects:
758796
refs.map(ref => instantiate(ref, klass, ctor, args)).join
759797
}
760798

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+
*/
761805
def initLocal(ref: Ref, sym: Symbol, value: Value): Contextual[Unit] = log("initialize local " + sym.show + " with " + value.show, printer) {
762806
if sym.is(Flags.Mutable) then
763807
val addr = Heap.localVarAddr(summon[Regions.Data], sym, State.currentObject)
@@ -767,6 +811,11 @@ object Objects:
767811
Env.setLocalVal(sym, value)
768812
}
769813

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+
*/
770819
def readLocal(thisV: Value, sym: Symbol): Contextual[Value] = log("reading local " + sym.show, printer, (_: Value).show) {
771820
Env.resolveEnv(sym.enclosingMethod, thisV, summon[Env.Data]) match
772821
case Some(thisV -> env) =>
@@ -814,6 +863,12 @@ object Objects:
814863
Cold
815864
}
816865

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+
*/
817872
def writeLocal(thisV: Value, sym: Symbol, value: Value): Contextual[Value] = log("write local " + sym.show + " with " + value.show, printer, (_: Value).show) {
818873

819874
assert(sym.is(Flags.Mutable), "Writing to immutable variable " + sym.show)
@@ -1010,7 +1065,8 @@ object Objects:
10101065
eval(expr, thisV, klass)
10111066

10121067
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
10141070

10151071
case Annotated(arg, annot) =>
10161072
if expr.tpe.hasAnnotation(defn.UncheckedAnnot) then
@@ -1309,7 +1365,7 @@ object Objects:
13091365
Bottom
13101366
else if target.isStaticObject then
13111367
val res = ObjectRef(target.moduleClass.asClass)
1312-
if target == klass || elideObjectAccess then res
1368+
if elideObjectAccess then res
13131369
else accessObject(target)
13141370
else
13151371
thisV match

library/src/scala/annotation/init.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ object init:
1414
* class A(x: Int):
1515
* def square(): Int = x*x
1616
*
17+
* class C(val a: A)
18+
*
1719
* object B:
18-
* val a = build(new A(10): @init.expose) // <-- usage
20+
* val a = build(new C(new A(10)): @widen(2)) // <-- usage
1921
*
20-
* def build(o: A) = new A(o.square()) // calling methods on parameter
22+
* def build(c: C) = new A(c.a.square()) // calling methods on parameter
2123
*
22-
* By default, method and constructor arguments are widened to height 1.
24+
* By default, method and constructor arguments are widened to height 1. In
25+
* the code above, without using `@widen(2)` we will have the abstract value
26+
* `C(a = Cold)` for the argument `c` of the method `build`. Consequently,
27+
* the checker will issue a warning for the method call `c.a.square()` because
28+
* it is forbidden to call methods or access fields on cold values.
2329
*/
2430
final class widen(height: Int) extends StaticAnnotation
2531

0 commit comments

Comments
 (0)