aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/transform/MoveStatics.scala
diff options
context:
space:
mode:
authorDmitry Petrashko <dmitry.petrashko@gmail.com>2016-04-20 14:01:11 +0200
committerDmitry Petrashko <dmitry.petrashko@gmail.com>2016-06-07 14:18:27 +0200
commitf2cfac5771b1b19c733e7be9b3d05dae411bc9e3 (patch)
tree1b9e4afbc6e6bd01ad1c2d20ca4fc6034389e4ea /src/dotty/tools/dotc/transform/MoveStatics.scala
parent990e962d50d5d688eee72fd64293e5fc8ce70617 (diff)
downloaddotty-f2cfac5771b1b19c733e7be9b3d05dae411bc9e3.tar.gz
dotty-f2cfac5771b1b19c733e7be9b3d05dae411bc9e3.tar.bz2
dotty-f2cfac5771b1b19c733e7be9b3d05dae411bc9e3.zip
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.
Diffstat (limited to 'src/dotty/tools/dotc/transform/MoveStatics.scala')
-rw-r--r--src/dotty/tools/dotc/transform/MoveStatics.scala38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/dotty/tools/dotc/transform/MoveStatics.scala b/src/dotty/tools/dotc/transform/MoveStatics.scala
index 23f19379b..7b9d27c2b 100644
--- a/src/dotty/tools/dotc/transform/MoveStatics.scala
+++ b/src/dotty/tools/dotc/transform/MoveStatics.scala
@@ -1,6 +1,7 @@
package dotty.tools.dotc.transform
import dotty.tools.dotc.ast.{Trees, tpd}
+import dotty.tools.dotc.core.Annotations.Annotation
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.DenotTransformers.{InfoTransformer, SymTransformer}
import dotty.tools.dotc.core.SymDenotations.SymDenotation
@@ -20,7 +21,7 @@ class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransform
def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation = {
- if (sym.hasAnnotation(defn.ScalaStaticAnnot) && sym.owner.is(Flags.Module)) {
+ if (sym.hasAnnotation(defn.ScalaStaticAnnot) && sym.owner.is(Flags.Module) && sym.owner.companionClass.exists) {
sym.owner.asClass.delete(sym.symbol)
sym.owner.companionClass.asClass.enter(sym.symbol)
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
val (classes, others) = trees.partition(x => x.isInstanceOf[TypeDef] && x.symbol.isClass)
val pairs = classes.groupBy(_.symbol.name.stripModuleClassSuffix).asInstanceOf[Map[Name, List[TypeDef]]]
- def move(companion: TypeDef, module: TypeDef): Thicket = {
- if (companion.symbol.is(Flags.Module)) move(module, companion)
+ def move(module: TypeDef, companion: TypeDef): List[Tree] = {
+ if (!module.symbol.is(Flags.Module)) move(companion, module)
else {
- val allMembers = companion.rhs.asInstanceOf[Template].body ++ module.rhs.asInstanceOf[Template].body
- val (newCompanionBody, newModuleBody) = allMembers.partition(x => {assert(x.symbol.exists); x.symbol.owner == companion.symbol})
- def rebuild(orig: TypeDef, newBody: List[Tree]) = {
- val oldTemplate = orig.rhs.asInstanceOf[Template]
- val statics = newBody.filter(x => x.isInstanceOf[ValDef] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot)).asInstanceOf[List[ValDef]]
+ val allMembers =
+ if(companion ne null) {companion.rhs.asInstanceOf[Template].body} else Nil ++
+ module.rhs.asInstanceOf[Template].body
+ val (newModuleBody, newCompanionBody) = allMembers.partition(x => {assert(x.symbol.exists); x.symbol.owner == module.symbol})
+ def rebuild(orig: TypeDef, newBody: List[Tree]): Tree = {
+ if (orig eq null) return EmptyTree
+
+ val staticFields = newBody.filter(x => x.isInstanceOf[ValDef] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot)).asInstanceOf[List[ValDef]]
val newBodyWithStaticConstr =
- if (statics.nonEmpty) {
- val staticCostructor = ctx.newSymbol(orig.symbol, Names.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.JavaStatic | Flags.Method, MethodType(Nil, defn.UnitType))
- val staticAssigns = statics.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))
+ if (staticFields.nonEmpty) {
+ val staticCostructor = ctx.newSymbol(orig.symbol, Names.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.Method, MethodType(Nil, defn.UnitType))
+ staticCostructor.addAnnotation(Annotation(defn.ScalaStaticAnnot))
+ staticCostructor.entered
+
+ val staticAssigns = staticFields.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))
tpd.DefDef(staticCostructor, Block(staticAssigns, tpd.unitLiteral)) :: newBody
} else newBody
+ val oldTemplate = orig.rhs.asInstanceOf[Template]
cpy.TypeDef(orig)(rhs = cpy.Template(orig.rhs)(oldTemplate.constr, oldTemplate.parents, oldTemplate.self, newBodyWithStaticConstr))
}
- Thicket(rebuild(companion, newCompanionBody), rebuild(module, newModuleBody))
+ Trees.flatten(rebuild(companion, newCompanionBody) :: rebuild(module, newModuleBody) :: Nil)
}
}
val newPairs =
for ((name, classes) <- pairs)
yield
- if (classes.tail.isEmpty) classes.head
+ if (classes.tail.isEmpty)
+ if (classes.head.symbol.is(Flags.Module)) move(classes.head, null)
+ else List(classes.head)
else move(classes.head, classes.tail.head)
- Trees.flatten(newPairs.toList ++ others)
+ Trees.flatten(newPairs.toList.flatten ++ others)
} else trees
}
}