@@ -44,15 +44,32 @@ class FirstTransform extends MiniPhaseTransform with IdentityDenotTransformer wi
44
44
this
45
45
}
46
46
47
- def transformInfo (tp : Type , sym : Symbol )(implicit ctx : Context ): Type = tp
48
-
49
- override def checkPostCondition (tree : Tree )(implicit ctx : Context ): Unit = tree match {
50
- case Select (qual, _) if tree.symbol.exists =>
51
- assert(qual.tpe derivesFrom tree.symbol.owner, i " non member selection of ${tree.symbol.showLocated} from ${qual.tpe}" )
52
- case _ : TypeTree =>
53
- case _ : Import | _ : NamedArg | _ : TypTree =>
54
- assert(false , i " illegal tree: $tree" )
55
- case _ =>
47
+ def transformInfo (tp : Type , sym : Symbol )(implicit ctx : Context ): Type = {
48
+ tp match {
49
+ // create companions for value classes that are not from currently compiled source file
50
+ case tp@ ClassInfo (_, cls, _, decls, _)
51
+ if (ValueClasses .isDerivedValueClass(cls)) &&
52
+ ! sym.isDefinedInCurrentRun && sym.scalacLinkedClass == NoSymbol =>
53
+ println(s " needsCompanion for: $sym" )
54
+ val newDecls = decls.cloneScope
55
+ val (modul, mcMethod, symMethod) = newCompanion(sym.name.toTermName, sym)
56
+ modul.entered
57
+ mcMethod.entered
58
+ newDecls.enter(symMethod)
59
+ tp.derivedClassInfo(decls = newDecls)
60
+ case _ => tp
61
+ }
62
+ }
63
+
64
+ override def checkPostCondition (tree : Tree )(implicit ctx : Context ): Unit = {
65
+ tree match {
66
+ case Select (qual, _) if tree.symbol.exists =>
67
+ assert(qual.tpe derivesFrom tree.symbol.owner, i " non member selection of ${tree.symbol.showLocated} from ${qual.tpe}" )
68
+ case _ : TypeTree =>
69
+ case _ : Import | _ : NamedArg | _ : TypTree =>
70
+ assert(false , i " illegal tree: $tree" )
71
+ case _ =>
72
+ }
56
73
}
57
74
58
75
/** Reorder statements so that module classes always come after their companion classes, add missing companion classes */
@@ -81,14 +98,12 @@ class FirstTransform extends MiniPhaseTransform with IdentityDenotTransformer wi
81
98
case Nil => Nil
82
99
}
83
100
84
- def newCompanion (name : TermName , forClass : Symbol ): Thicket = {
85
- val modul = ctx.newCompleteModuleSymbol(ctx.owner, name, Synthetic , Synthetic ,
86
- defn.ObjectType :: Nil , Scopes .newScope)
87
- val mc = modul.moduleClass
101
+ def registerCompanion (name : TermName , forClass : Symbol ): TermSymbol = {
102
+ val (modul, mcCompanion, classCompanion) = newCompanion(name, forClass)
88
103
if (ctx.owner.isClass) modul.enteredAfter(thisTransformer)
89
- ctx.synthesizeCompanionMethod(nme. COMPANION_CLASS_METHOD , forClass, mc) .enteredAfter(thisTransformer)
90
- ctx.synthesizeCompanionMethod(nme. COMPANION_MODULE_METHOD , mc, forClass) .enteredAfter(thisTransformer)
91
- ModuleDef ( modul, Nil )
104
+ mcCompanion .enteredAfter(thisTransformer)
105
+ classCompanion .enteredAfter(thisTransformer)
106
+ modul
92
107
}
93
108
94
109
def addMissingCompanions (stats : List [Tree ]): List [Tree ] = stats map {
@@ -101,13 +116,23 @@ class FirstTransform extends MiniPhaseTransform with IdentityDenotTransformer wi
101
116
false
102
117
}
103
118
val uniqueName = if (nameClash) objName.avoidClashName else objName
104
- Thicket (stat :: newCompanion( uniqueName, stat.symbol).trees)
119
+ Thicket (stat :: ModuleDef (registerCompanion( uniqueName, stat.symbol), Nil ).trees)
105
120
case stat => stat
106
121
}
107
122
108
123
addMissingCompanions(reorder(stats))
109
124
}
110
125
126
+ private def newCompanion (name : TermName , forClass : Symbol )(implicit ctx : Context ) = {
127
+ val modul = ctx.newCompleteModuleSymbol(forClass.owner, name, Synthetic , Synthetic ,
128
+ defn.ObjectType :: Nil , Scopes .newScope)
129
+ val mc = modul.moduleClass
130
+
131
+ val mcComp = ctx.synthesizeCompanionMethod(nme.COMPANION_CLASS_METHOD , forClass, mc)
132
+ val classComp = ctx.synthesizeCompanionMethod(nme.COMPANION_MODULE_METHOD , mc, forClass)
133
+ (modul, mcComp, classComp)
134
+ }
135
+
111
136
override def transformDefDef (ddef : DefDef )(implicit ctx : Context , info : TransformerInfo ) = {
112
137
if (ddef.symbol.hasAnnotation(defn.NativeAnnot )) {
113
138
ddef.symbol.resetFlag(Deferred )
0 commit comments