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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
\* */
// $Id$
package scalac.transformer;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import scalac.Global;
import scalac.Phase;
import scalac.PhaseDescriptor;
import scalac.Unit;
import scalac.ast.Transformer;
import scalac.ast.Tree;
import scalac.ast.Tree.Template;
import scalac.symtab.Modifiers;
import scalac.symtab.Symbol;
import scalac.symtab.TermSymbol;
import scalac.symtab.Type;
import scalac.util.Debug;
import scalac.util.Name;
import scalac.util.Names;
/**
* This phase does the following:
*
* - In every nested class, adds to each of its constructor a new
* value parameter that contains a link to the outer class.
*
* - In every nested class, adds to each of its constructor a new type
* parameter for every type parameter appearing in outer classes.
*
* - In every class, adds a forwarding "super" method for every method
* that is accessed via "super" in a nested class.
*
* - Adds all missing qualifiers.
*/
public class ExplicitOuterClassesPhase extends Phase {
//########################################################################
// Public Constructors
/** Initializes this instance. */
public ExplicitOuterClassesPhase(Global global,PhaseDescriptor descriptor){
super(global, descriptor);
}
//########################################################################
// Public Methods
/** Applies this phase to the given type for the given symbol. */
public Type transformInfo(Symbol symbol, Type type) {
if (symbol.isJava()) return type;
//System.out.println("!!! debug1 = " + Debug.show(symbol));
//if (symbol.name.toString().equals("x")) new Error("!!!").printStackTrace();
type = typeTransformer.apply(type);
if (symbol.isConstructor()) {
Symbol[] tparams = type.typeParams();
Symbol[] vparams = type.valueParams();
Type result = type.resultType();
// Add outer value link
if (hasOuterValueLink(symbol)) {
Name name = Names.OUTER(symbol);
Symbol vlink = new TermSymbol(symbol.pos, name, symbol, 0);
vlink.setInfo(getOuterClass(symbol).typeOfThis()); // !!!
vparams = Symbol.cloneArray(1, vparams);
vparams[0] = vlink;
}
// Add outer type links
if (hasOuterTypeLinks(symbol)) {
Symbol[] oldtparams = getOuterTypeParams(symbol);
Symbol[] tlinks = Symbol.cloneArray(oldtparams);
for (int i = 0; i < tlinks.length; i++) {
tlinks[i] = oldtparams[i].cloneSymbol(symbol);
tlinks[i].name = Names.OUTER(symbol, oldtparams[i]);
}
tparams = Symbol.concat(tlinks, tparams);
result = Type.getSubst(oldtparams, tlinks, true).apply(result);
}
type = Type.MethodType(vparams, result);
if (tparams.length != 0) type = Type.PolyType(tparams, type);
} else {
Symbol owner = symbol.owner().isConstructor()
? symbol.owner()
: symbol.enclClass();
//System.out.println("!!! debug2 = " + Debug.show(symbol) + " - " + Debug.show(owner) + " --- " + (owner == Symbol.NONE) + " -- #" + owner.name + "#");
//if (onwer.isJava() && owner != Symbol.NONE && owner.name.length() > 0) // !!!
if (owner.isClass() || owner.isConstructor())
type = getOuterTypeSubst(owner, true).apply(type);
}
/*
String s1 = Debug.show(symbol);
String s2 = symbol.info().toString();
//global.nextPhase();
String s3 = type.toString();
//global.prevPhase();
System.out.println("!!! symbol = " + s1);
System.out.println("!!! type = " + s2 + " -- " + System.identityHashCode(s2));
System.out.println("!!! new = " + s3 + " -- " + System.identityHashCode(s3));
System.out.println("!!!");
*/
return type;
}
/** Applies this phase to the given compilation units. */
public void apply(Unit[] units) {
treeTransformer.apply(units);
}
//########################################################################
// Private Methods - Outer class
/** Returns the outer class of the given class or constructor. */
private Symbol getOuterClass(Symbol symbol) {
assert symbol.isClass() || symbol.isConstructor(): Debug.show(symbol);
return symbol.owner();
}
/** Has the given class or constructor an outer value link? */
private boolean hasOuterValueLink(Symbol symbol) {
assert symbol.isClass() || symbol.isConstructor(): Debug.show(symbol);
return !symbol.isJava() && getOuterClass(symbol).isClass();
}
/** Returns the outer link of the given class or constructor. */
private Symbol getOuterValueLink(Symbol symbol) {
if (!hasOuterValueLink(symbol)) return Symbol.NONE;
if (symbol.isClass()) symbol = symbol.primaryConstructor();
return nextValueParams(symbol)[0];
}
/** Has the given class or constructor outer type links? */
private boolean hasOuterTypeLinks(Symbol symbol) {
assert symbol.isClass() || symbol.isConstructor(): Debug.show(symbol);
if (symbol.isJava()) return false;
Symbol outer = getOuterClass(symbol);
return outer.isClass() && nextTypeParams(outer).length != 0;
}
/** Returns the type substitution for the given class or constructor. */
private Type.Map getOuterTypeSubst(Symbol symbol, boolean update) {
if (!hasOuterTypeLinks(symbol)) return Type.IdMap;
Symbol[] oldtparams = getOuterTypeParams(symbol);
Symbol[] newtparams = nextTypeParams(symbol);
Symbol[] tlinks = new Symbol[oldtparams.length];
for (int i = 0; i < tlinks.length; i++) tlinks[i] = newtparams[i];
return Type.getSubst(oldtparams, tlinks, update);
}
/** Returns the outer type parameters of the given class or constructor. */
private Symbol[] getOuterTypeParams(Symbol symbol) {
Symbol outer = getOuterClass(symbol);
Symbol[] tparams = Symbol.cloneArray(nextTypeParams(outer));
for (int i = tparams.length; i != 0; outer = getOuterClass(outer)) {
Symbol[] symbols = outer.typeParams();
for (int j = symbols.length; j != 0; ) tparams[--i] = symbols[--j];
}
return tparams;
}
/**
* Extracts from given prefix the outer type arguments for the
* given class or constructor.
*/
private Type[] getOuterTypeArgs(Type prefix, Symbol symbol) {
if (!hasOuterTypeLinks(symbol)) return Type.EMPTY_ARRAY;
Symbol outer = getOuterClass(symbol);
global.nextPhase();
Type[] targs = prefix.baseType(outer).widen().typeArgs();
global.prevPhase();
assert targs.length == nextTypeParams(outer).length:
"\nsymbol = " + Debug.show(symbol) +
"\nprefix = " + prefix;
return targs;
}
//########################################################################
// Private Methods - Helper methods
/** Returns the given symbol's type parameters in next phase. */
private Symbol[] nextTypeParams(Symbol symbol) {
global.nextPhase();
Symbol[] tparams = symbol.typeParams();
global.prevPhase();
return tparams;
}
/** Returns the given symbol's value parameters in next phase. */
private Symbol[] nextValueParams(Symbol symbol) {
global.nextPhase();
Symbol[] vparams = symbol.valueParams();
global.prevPhase();
return vparams;
}
//########################################################################
// Private Class - Type transformer
/** The type transformer */
private final Type.Map typeTransformer = new Type.MapOnlyTypes() {
public Type apply(Type type) {
switch (type) {
case TypeRef(Type prefix, Symbol symbol, Type[] targs):
if (!symbol.isClass()) break;
prefix = apply(prefix);
targs = Type.concat(getOuterTypeArgs(prefix, symbol), targs);
return Type.TypeRef(prefix, symbol, targs);
}
return map(type);
}
};
//########################################################################
// Private Class - Tree transformer
/** The tree transformer */
private final Transformer treeTransformer = new Transformer(global) {
/** The current context */
private Context context;
/** Transforms the given tree. */
public Tree transform(Tree tree) {
switch (tree) {
case ClassDef(_, _, _, _, _, Template impl):
Symbol clasz = tree.symbol();
context = new Context(context, clasz, new HashMap());
Tree[] parents = transform(impl.parents);
Tree[] members = transform(impl.body);
members = Tree.concat(members, genSuperMethods());
context = context.outer;
if (context != null) clasz.flags |= Modifiers.STATIC;
return gen.ClassDef(clasz, parents, impl.symbol(), members);
case DefDef(_, _, _, _, _, Tree rhs):
Symbol symbol = tree.symbol();
if (!symbol.isConstructor()) break;
Context backup = context;
context = context.getConstructorContext(symbol);
rhs = transform(rhs);
context = backup;
return gen.DefDef(tree.pos, symbol, rhs);
case This(_):
return genOuterRef(tree.pos, tree.symbol());
case Ident(_):
Symbol symbol = tree.symbol();
Symbol owner = symbol.owner();
if (owner.isClass()) {
// !!! A this node is missing here. This should
// never happen if all trees were correct.
Tree qualifier = genOuterRef(tree.pos, owner);
return gen.mkStable(gen.Select(qualifier, symbol));
}
if (!owner.isConstructor()) break;
Symbol clasz = owner.constructorClass();
if (clasz == context.clasz) break;
Tree qualifier = genOuterRef(tree.pos, clasz);
return gen.mkStable(gen.Select(qualifier, symbol));
case Select(Super(_, _), _):
Tree qualifier = ((Tree.Select)tree).qualifier;
Symbol clasz = qualifier.symbol();
if (clasz == context.clasz) break;
Symbol symbol = getSuperMethod(clasz, tree.symbol());
qualifier = genOuterRef(qualifier.pos, clasz);
return gen.mkStable(gen.Select(tree.pos, qualifier, symbol));
case Apply(Tree vfun, Tree[] vargs):
switch (vfun) {
case TypeApply(Tree tfun, Tree[] targs):
if (!tfun.symbol().isConstructor()) break;
return transform(tree, vargs, vfun, targs, tfun);
default:
if (!vfun.symbol().isConstructor()) break;
return transform(tree, vargs, vfun, Tree.EMPTY_ARRAY,vfun);
}
}
Type type = typeTransformer.apply(tree.type());
if (context != null) type = context.subst.apply(type);
return super.transform(tree).setType(type);
}
/* Add outer type and value arguments to constructor calls. */
private Tree transform(Tree vapply, Tree[] vargs, Tree tapply,
Tree[] targs, Tree tree)
{
Symbol symbol = tree.symbol();
targs = transform(targs);
vargs = transform(vargs);
switch (transform(tree)) {
case Select(Tree qualifier, _):
if (hasOuterValueLink(symbol)) {
vargs = Tree.cloneArray(1, vargs);
vargs[0] = qualifier;
}
Type[] types = getOuterTypeArgs(qualifier.type(), symbol);
if (types.length != 0) {
targs = Tree.cloneArray(types.length, targs);
for (int i = 0; i < types.length; i++)
targs[i] = gen.mkType(tapply.pos, types[i]);
}
}
tree = gen.Ident(tree.pos, symbol);
if (targs.length != 0) tree = gen.TypeApply(tapply.pos,tree,targs);
return gen.Apply(vapply.pos, tree, vargs);
}
/**
* Returns the forwarding "super" method of the given class
* that invokes the given method. If the symbol does not yet
* exist, it is created and added to the class members.
*/
private Symbol getSuperMethod(Symbol clasz, Symbol method) {
Context context = this.context;
for (; context.clasz != clasz; context = context.outer)
assert context.outer != null : Debug.show(clasz);
Symbol forward = (Symbol)context.supers.get(method);
if (forward == null) {
Name name = Names.SUPER(method);
int flags = Modifiers.PRIVATE | Modifiers.FINAL;
forward = new TermSymbol(method.pos, name, clasz, flags);
forward.setInfo(method.nextType().cloneType(method, forward));
context.supers.put(method, forward);
clasz.nextInfo().members().enter(forward);
assert Debug.log("created forwarding method: ", forward);
}
return forward;
}
/** Generates the trees of the forwarding "super" methods. */
private Tree[] genSuperMethods() {
if (context.supers.size() == 0) return Tree.EMPTY_ARRAY;
Tree[] trees = new Tree[context.supers.size()];
Iterator entries = context.supers.entrySet().iterator();
for (int i = 0; i < trees.length; i++) {
Map.Entry entry = (Map.Entry)entries.next();
Symbol method = (Symbol)entry.getKey();
Symbol forward = (Symbol)entry.getValue();
int pos = forward.pos;
Tree[] targs = gen.mkTypeIdents(pos, nextTypeParams(forward));
Tree[] vargs = gen.mkIdents(pos, nextValueParams(forward));
Tree fun = gen.Select(gen.Super(pos, context.clasz), method);
trees[i] = gen.DefDef(forward, gen.mkApply(fun, targs, vargs));
}
return trees;
}
/** Returns a tree referencing the given outer class. */
private Tree genOuterRef(int pos, Symbol clasz) {
if (context.clasz == clasz) return gen.This(pos, clasz);
Tree tree = gen.mkStable(gen.Ident(pos, context.link));
for (Context context = this.context.outer;;context =context.outer){
assert context != null: Debug.show(clasz);
if (context.clasz == clasz) return tree;
tree = gen.mkStable(gen.Select(tree, context.link));
}
}
};
//########################################################################
// Private Class - Tree transformer context
/** This class represents the tree transformation context. */
private class Context {
/** The outer context */
public final Context outer;
/** The class symbol */
public final Symbol clasz;
/** The super methods (maps invoked to forwarding methods) */
public final Map/*<Symbol,Symbol>*/ supers;
/** The outer type parameter substitution */
public final Type.Map subst;
/** The link to the outer class */
public final Symbol link;
/** Initializes this instance. */
public Context(Context outer, Symbol symbol, Map supers) {
this.outer = outer;
this.clasz = symbol.constructorClass();
this.supers = supers;
this.subst = getOuterTypeSubst(symbol, false);
this.link = getOuterValueLink(symbol);
}
/** Returns a context for the given constructor. */
public Context getConstructorContext(Symbol constructor) {
assert constructor.constructorClass() == clasz;
return new Context(outer, constructor, supers);
}
}
//########################################################################
}
|