1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/* NSC -- new scala compiler
* Copyright 2005 LAMP/EPFL
* @author
*/
// $Id$
package scala.tools.nsc.transform;
import symtab._;
import Flags._;
import util.ListBuffer;
import collection.mutable.HashMap;
abstract class AddInterfaces extends InfoTransform {
import global._; // the global environment
import definitions._; // standard classes and methods
import posAssigner.atPos; // for filling in tree positions
override def phaseNewFlags: long = lateDEFERRED | lateINTERFACE;
// Type transformation
def erasedTypeRef(sym: Symbol): Type;
private val implClassMap = new HashMap[Symbol, Symbol];
private val implMethodMap = new HashMap[Symbol, Symbol];
override def newPhase(prev: scala.tools.nsc.Phase): StdPhase = {
implClassMap.clear;
implMethodMap.clear;
super.newPhase(prev)
}
private def needsImplMethod(sym: Symbol): boolean =
sym.isMethod && isInterfaceMember(sym) &&
(!(sym hasFlag (DEFERRED | SUPERACCESSOR)) || (sym hasFlag lateDEFERRED));
private def isInterfaceMember(sym: Symbol): boolean = {
sym.info; // to set lateMETHOD flag if necessary
sym.isType ||
sym.isMethod && !(sym hasFlag (PRIVATE | BRIDGE | LABEL)) && !sym.isConstructor
}
def implClass(iface: Symbol): Symbol = implClassMap.get(iface) match {
case Some(c) => c
case None =>
atPhase(currentRun.erasurePhase) {
val implName = nme.implClassName(iface.name);
var impl = if (iface.owner.isClass) iface.owner.info.decl(implName) else NoSymbol;
if (impl == NoSymbol) {
impl = iface.cloneSymbolImpl(iface.owner);
impl.name = implName;
if (iface.owner.isClass) iface.owner.info.decls enter impl
}
impl setPos iface.pos;
impl.flags = iface.flags & ~(INTERFACE | lateINTERFACE);
impl setInfo new LazyImplClassType(iface);
implClassMap(iface) = impl;
if (settings.debug.value) log("generating impl class " + impl + " in " + iface.owner);//debug
impl
}
}
private class LazyImplClassType(iface: Symbol) extends LazyType {
def implDecls(implClass: Symbol, ifaceDecls: Scope): Scope = {
val decls = new Scope();
for (val sym <- ifaceDecls.elements) {
if (isInterfaceMember(sym)) {
if (needsImplMethod(sym)) {
val impl = sym.cloneSymbol(implClass).setInfo(sym.info).resetFlag(lateDEFERRED);
if (!impl.isExternal) implMethodMap(sym) = impl;
decls enter impl;
sym setFlag lateDEFERRED
}
} else {
if (sym.name == newTermName("definitions"))
System.out.println("definitions to impl class " + sym.isMethod + " " + sym.getFlag(PRIVATE | BRIDGE | LABEL));
sym.owner = implClass;
decls enter sym;
}
}
decls
}
override def complete(sym: Symbol): unit = {
def implType(tp: Type): Type = tp match {
case ClassInfoType(parents, decls, _) =>
//ClassInfoType(traitToImplClass(parents) ::: List(iface.tpe), implDecls(sym, decls), sym)
ClassInfoType(
ObjectClass.tpe :: (parents.tail map traitToImplClass) ::: List(iface.tpe),
implDecls(sym, decls),
sym)
case PolyType(tparams, restpe) =>
PolyType(tparams, implType(restpe))
}
sym.setInfo(atPhase(currentRun.erasurePhase)(implType(iface.info)));
}
override def load(clazz: Symbol): unit = complete(clazz)
}
private def traitToImplClass(tp: Type): Type = tp match {
case TypeRef(pre, sym, args) if (sym.needsImplClass) =>
typeRef(pre, implClass(sym), args)
case _ =>
tp
}
def transformTraitInfo(tp: Type): Type = tp match {
case ClassInfoType(parents, decls, clazz) =>
if (clazz.needsImplClass) {
clazz setFlag lateINTERFACE;
implClass(clazz) // generate an impl class
}
val parents1 =
if (parents.isEmpty) List()
else {
assert(!parents.head.symbol.isTrait || clazz == RepeatedParamClass, clazz);
if (clazz hasFlag INTERFACE) erasedTypeRef(ObjectClass) :: parents.tail
else if (clazz.isImplClass || clazz == ArrayClass) parents
else parents map traitToImplClass
}
val decls1 = if (clazz hasFlag INTERFACE) new Scope(decls.toList filter isInterfaceMember)
else decls;
if ((parents1 eq parents) && (decls1 eq decls)) tp
else ClassInfoType(parents1, decls1, clazz)
case _ =>
tp
}
// Tree transformation --------------------------------------------------------------
private class ChangeOwnerAndReturnTraverser(oldowner: Symbol, newowner: Symbol)
extends ChangeOwnerTraverser(oldowner, newowner) {
override def traverse(tree: Tree): unit = {
tree match {
case Return(expr) =>
if (tree.symbol == oldowner) tree.symbol = newowner;
case _ =>
}
super.traverse(tree)
}
}
private def ifaceMemberDef(tree: Tree): Tree =
if (!tree.isDef || !isInterfaceMember(tree.symbol)) EmptyTree
else if (needsImplMethod(tree.symbol)) DefDef(tree.symbol, vparamss => EmptyTree)
else tree;
private def ifaceTemplate(templ: Template): Template =
copy.Template(templ, templ.parents, templ.body map ifaceMemberDef);
private def implMethodDef(tree: Tree, ifaceMethod: Symbol): Tree =
implMethodMap.get(ifaceMethod) match {
case Some(implMethod) =>
tree.symbol = implMethod;
new ChangeOwnerAndReturnTraverser(ifaceMethod, implMethod)(tree)
case None =>
throw new Error("implMethod missing for " + ifaceMethod)
}
private def implMemberDef(tree: Tree): Tree =
if (!tree.isDef || !isInterfaceMember(tree.symbol)) tree
else if (needsImplMethod(tree.symbol)) implMethodDef(tree, tree.symbol)
else EmptyTree;
private def implTemplate(clazz: Symbol, templ: Template): Template = atPos(templ.pos){
val templ1 = Template(templ.parents, templ.body map implMemberDef)
setPos templ.pos
setSymbol clazz.newLocalDummy(templ.pos);
new ChangeOwnerTraverser(templ.symbol.owner, clazz)(
new ChangeOwnerTraverser(templ.symbol, templ1.symbol)(templ1))
}
def implClassDefs(trees: List[Tree]): List[Tree] = {
val buf = new ListBuffer[Tree];
for (val tree <- trees)
tree match {
case ClassDef(_, _, _, _, impl) =>
if (tree.symbol.needsImplClass)
buf += {
val clazz = implClass(tree.symbol).initialize;
ClassDef(clazz, implTemplate(clazz, impl))
}
case _ =>
}
buf.toList
}
protected val traitTransformer = new Transformer {
override def transformStats(stats: List[Tree], exprOwner: Symbol): List[Tree] =
super.transformStats(stats, exprOwner) :::
super.transformStats(implClassDefs(stats), exprOwner);
override def transform(tree: Tree): Tree = {
val tree1 = tree match {
case ClassDef(mods, name, tparams, tpt, impl) =>
if (tree.symbol.needsImplClass) {
implClass(tree.symbol).initialize; // to force lateDEFERRED flags
copy.ClassDef(tree, mods | INTERFACE, name, tparams, tpt, ifaceTemplate(impl))
}
else tree
case Template(parents, body) =>
val parents1 = tree.symbol.owner.info.parents map (t => TypeTree(t) setPos tree.pos);
copy.Template(tree, parents1, body)
case This(_) =>
if (tree.symbol.needsImplClass) {
val impl = implClass(tree.symbol);
var owner = currentOwner;
while (owner != tree.symbol && owner != impl) owner = owner.owner;
if (owner == impl) This(impl) setPos tree.pos
else tree
} else tree
case Super(qual, mix) =>
val mix1 =
if (mix == nme.EMPTY.toTypeName) mix
else {
val ps = atPhase(currentRun.erasurePhase) {
tree.symbol.info.parents dropWhile (p => p.symbol.name != mix)
}
assert(!ps.isEmpty, tree);
if (ps.head.symbol.needsImplClass) implClass(ps.head.symbol).name
else mix
}
if (tree.symbol.needsImplClass) Super(implClass(tree.symbol), mix1) setPos tree.pos
else copy.Super(tree, qual, mix1)
case _ =>
tree
}
super.transform(tree1)
}
}
}
/*
val ensureNoEscapes = new TypeTraverser {
def ensureNoEscape(sym: Symbol): unit = {
if (sym.hasFlag(PRIVATE)) {
var o = currentOwner;
while (o != NoSymbol && o != sym.owner && !o.isLocal && !o.hasFlag(PRIVATE))
o = o.owner;
if (o == sym.owner) sym.makeNotPrivate(base);
}
}
def traverse(t: Type): TypeTraverser = {
t match {
case TypeRef(qual, sym, args) =>
ensureNoEscape(sym);
mapOver(t);
case ClassInfoType(parents, decls, clazz) =>
parents foreach { p => traverse; () }
traverse(t.typeOfThis);
case _ =>
mapOver(t)
}
this
}
}
*/
|