Skip to content

Commit 02f73e8

Browse files
committed
MoveStatics: survive absence of companions.
Now moveStatics can correctly create static constructors for objects. Those static constructors would later be merged with synthetic module initialisers by GenBCode. This is a bit of magic, it would be good to move all this into this phase.
1 parent 1358e97 commit 02f73e8

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/dotty/tools/dotc/transform/MoveStatics.scala

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dotty.tools.dotc.transform
22

33
import dotty.tools.dotc.ast.{Trees, tpd}
4+
import dotty.tools.dotc.core.Annotations.Annotation
45
import dotty.tools.dotc.core.Contexts.Context
56
import dotty.tools.dotc.core.DenotTransformers.{InfoTransformer, SymTransformer}
67
import dotty.tools.dotc.core.SymDenotations.SymDenotation
@@ -20,7 +21,7 @@ class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransform
2021

2122

2223
def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation = {
23-
if (sym.hasAnnotation(defn.ScalaStaticAnnot) && sym.owner.is(Flags.Module)) {
24+
if (sym.hasAnnotation(defn.ScalaStaticAnnot) && sym.owner.is(Flags.Module) && sym.owner.companionClass.exists) {
2425
sym.owner.asClass.delete(sym.symbol)
2526
sym.owner.companionClass.asClass.enter(sym.symbol)
2627
val flags = if (sym.is(Flags.Method)) sym.flags else sym.flags | Flags.Mutable
@@ -34,32 +35,41 @@ class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransform
3435
val (classes, others) = trees.partition(x => x.isInstanceOf[TypeDef] && x.symbol.isClass)
3536
val pairs = classes.groupBy(_.symbol.name.stripModuleClassSuffix).asInstanceOf[Map[Name, List[TypeDef]]]
3637

37-
def move(companion: TypeDef, module: TypeDef): Thicket = {
38-
if (companion.symbol.is(Flags.Module)) move(module, companion)
38+
def move(module: TypeDef, companion: TypeDef): List[Tree] = {
39+
if (!module.symbol.is(Flags.Module)) move(companion, module)
3940
else {
40-
val allMembers = companion.rhs.asInstanceOf[Template].body ++ module.rhs.asInstanceOf[Template].body
41-
val (newCompanionBody, newModuleBody) = allMembers.partition(x => {assert(x.symbol.exists); x.symbol.owner == companion.symbol})
42-
def rebuild(orig: TypeDef, newBody: List[Tree]) = {
43-
val oldTemplate = orig.rhs.asInstanceOf[Template]
44-
val statics = newBody.filter(x => x.isInstanceOf[ValDef] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot)).asInstanceOf[List[ValDef]]
41+
val allMembers =
42+
if(companion ne null) {companion.rhs.asInstanceOf[Template].body} else Nil ++
43+
module.rhs.asInstanceOf[Template].body
44+
val (newModuleBody, newCompanionBody) = allMembers.partition(x => {assert(x.symbol.exists); x.symbol.owner == module.symbol})
45+
def rebuild(orig: TypeDef, newBody: List[Tree]): Tree = {
46+
if (orig eq null) return EmptyTree
47+
48+
val staticFields = newBody.filter(x => x.isInstanceOf[ValDef] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot)).asInstanceOf[List[ValDef]]
4549
val newBodyWithStaticConstr =
46-
if (statics.nonEmpty) {
47-
val staticCostructor = ctx.newSymbol(orig.symbol, Names.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.JavaStatic | Flags.Method, MethodType(Nil, defn.UnitType))
48-
val staticAssigns = statics.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))
50+
if (staticFields.nonEmpty) {
51+
val staticCostructor = ctx.newSymbol(orig.symbol, Names.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.Method, MethodType(Nil, defn.UnitType))
52+
staticCostructor.addAnnotation(Annotation(defn.ScalaStaticAnnot))
53+
staticCostructor.entered
54+
55+
val staticAssigns = staticFields.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))
4956
tpd.DefDef(staticCostructor, Block(staticAssigns, tpd.unitLiteral)) :: newBody
5057
} else newBody
5158

59+
val oldTemplate = orig.rhs.asInstanceOf[Template]
5260
cpy.TypeDef(orig)(rhs = cpy.Template(orig.rhs)(oldTemplate.constr, oldTemplate.parents, oldTemplate.self, newBodyWithStaticConstr))
5361
}
54-
Thicket(rebuild(companion, newCompanionBody), rebuild(module, newModuleBody))
62+
Trees.flatten(rebuild(companion, newCompanionBody) :: rebuild(module, newModuleBody) :: Nil)
5563
}
5664
}
5765
val newPairs =
5866
for ((name, classes) <- pairs)
5967
yield
60-
if (classes.tail.isEmpty) classes.head
68+
if (classes.tail.isEmpty)
69+
if (classes.head.symbol.is(Flags.Module)) move(classes.head, null)
70+
else List(classes.head)
6171
else move(classes.head, classes.tail.head)
62-
Trees.flatten(newPairs.toList ++ others)
72+
Trees.flatten(newPairs.toList.flatten ++ others)
6373
} else trees
6474
}
6575
}

0 commit comments

Comments
 (0)