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
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
\* */
// $OldId: ExpandMixins.java,v 1.24 2002/11/11 16:08:50 schinz Exp $
// $Id$
package scalac.transformer;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import scalac.Global;
import scalac.ast.Tree;
import scalac.ast.Tree.Template;
import scalac.ast.TreeList;
import scalac.ast.TreeGen;
import scalac.ast.TreeCloner;
import scalac.ast.TreeSymbolCloner;
import scalac.ast.Transformer;
import scalac.symtab.Modifiers;
import scalac.symtab.Scope;
import scalac.symtab.Scope.SymbolIterator;
import scalac.symtab.Symbol;
import scalac.symtab.SymbolCloner;
import scalac.symtab.SymbolSubstTypeMap;
import scalac.symtab.Type;
import scalac.util.Name;
import scalac.util.Debug;
public class ClassExpander {
//########################################################################
// Private Fields
/** The global environment */
private final Global global;
/** A tree generator */
private final TreeGen gen;
/** The expanding class */
private final Symbol clasz;
/** The parents of the expanding class */
private final Type[] parents;
/** The members of the expanding class */
private final Scope members;
/** The template of the expanding class */
private final Template template;
/** The body of the expanding class */
private final TreeList body;
/** The type map to apply to inlined symbols and trees */
private final SymbolSubstTypeMap map;
/** The symbol cloner to clone inlined symbols */
private final SymbolCloner cloner;
/** The state of this class (used to prevent misuses) */
private int state;
//########################################################################
// Public Constructors
public ClassExpander(Global global, Symbol clasz, Template template) {
this.global = global;
this.gen = global.treeGen;
this.clasz = clasz;
this.parents = Type.cloneArray(clasz.parents());
this.members = clasz.members().cloneScope();
this.template = gen.make.Template(template.pos, template.symbol(),
Tree.cloneArray(template.parents), template.body);
this.template.setType(Type.compoundType(parents, members, clasz));
this.body = new TreeList();
this.map = new SymbolSubstTypeMap();
this.cloner = new SymbolCloner(
global.freshNameCreator, new HashMap(), map.getSymbols());
this.state = parents.length;
}
//########################################################################
// Public Methods
public void inlineMixin(int i, Type type, Symbol iface, Template impl) {
assert 0 < i && i < state : "state = " + state + ", i = " + i;
switch (parents[i]) {
case TypeRef(Type prefix, Symbol mixin, Type[] args):
map.insertSymbol(mixin, clasz);
cloner.owners.put(mixin.primaryConstructor(), clasz);
inlineMixinTParams(type);
Tree.Apply constr = (Tree.Apply)template.parents[i];
inlineMixinVParams(mixin.valueParams(), constr.args);
inlineMixinMembers(impl);
parents[i] = Type.TypeRef(prefix, iface, args);
template.parents[i] = gen.mkPrimaryConstr(constr.pos, parents[i]);
state = i;
return;
default:
throw Debug.abort("invalid base class type", parents[i]);
}
}
public Template getTemplate() {
assert 0 < state : "state = " + state;
Transformer superFixer = new Transformer(global) {
public Tree transform(Tree tree) {
switch (tree) {
case Select(Super(_, _), _):
Symbol symbol = map.lookupSymbol(tree.symbol());
if (symbol != null)
return gen.Select(gen.This(tree.pos, clasz), symbol);
}
return super.transform(tree);
}
};
body.append(superFixer.transform(template.body));
template.body = body.toArray();
// !!! *1 fix ExpandMixinsPhase.transformInfo and remove next line
clasz.updateInfo(template.type());
state = 0;
return template;
}
//########################################################################
// Private Methods
private void inlineMixinTParams(Type type) {
switch (type) {
case TypeRef(Type prefix, Symbol symbol, Type[] args):
map.insertType(symbol.typeParams(), args);
// !!! symbols could be equal but args different? need to rebind ?
if (prefix.symbol() != symbol.owner())
inlineMixinTParams(prefix.baseType(symbol.owner()));
return;
default:
throw Debug.abort("illegal case", type);
}
}
private void inlineMixinVParams(Symbol[] params, Tree[] args) {
for (int i = 0; i < params.length; i++) {
Symbol member = cloner.cloneSymbol(params[i], true);
member.flags &= ~Modifiers.PARAM;
member.flags |= Modifiers.PRIVATE;
members.enter(member);
}
// We need two loops because parameters may appear in their types.
for (int i = 0; i < params.length; i++) {
Symbol member = map.lookupSymbol(params[i]);
member.setType(map.apply(member.type()));
body.append(gen.ValDef(member, args[i]));
}
}
private void inlineMixinMembers(Template mixin) {
Scope symbols = mixin.type().members();
// The map names is used to implement an all or nothing
// strategy for overloaded symbols.
Map/*<Name,Name>*/ names = new HashMap();
for (SymbolIterator i = symbols.iterator(true); i.hasNext();) {
Symbol member = i.next();
Name name = (Name)names.get(member.name);
boolean shadowed = name == null &&
members.lookup(member.name) != Symbol.NONE;
Symbol clone = cloner.cloneSymbol(member, shadowed);
if (name != null)
clone.name = name;
else
names.put(member.name, clone.name);
if (clone.name != member.name) clone.flags &= ~Modifiers.OVERRIDE;
clone.setType(clone.type().cloneTypeNoSubst(cloner));
members.enterOrOverload(clone);
}
// We need two loops because members may appear in their types.
for (SymbolIterator i = symbols.iterator(true); i.hasNext();) {
Symbol member = map.lookupSymbol(i.next());
if (member == null) continue;
member.setType(map.applyParams(member.type()));
}
cloner.owners.put(mixin.symbol(), template.symbol());
final Set clones = new HashSet();
TreeSymbolCloner mixinSymbolCloner = new TreeSymbolCloner(cloner) {
public Symbol cloneSymbol(Symbol symbol) {
Symbol clone = super.cloneSymbol(symbol);
clones.add(clone);
return clone;
}
};
TreeCloner mixinTreeCloner = new TreeCloner(global, map) {
public Tree transform(Tree tree) {
switch (tree) {
case New(Template template):
assert template.parents.length == 1 : tree;
assert template.body.length == 0 : tree;
Tree apply = template.parents[0];
switch (apply) {
case Apply(Tree clasz, Tree[] args):
args = transform(args);
apply = gen.Apply(apply.pos, clasz, args);
return gen.New(tree.pos, apply);
default:
throw Debug.abort("illegal case", tree);
}
case Select(Super(_, _), _):
Symbol sym = tree.symbol();
Symbol newSym = sym.overridingSymbol(parents[0]);
if (newSym != Symbol.NONE)
return gen.Select(tree.pos,
gen.Super(tree.pos, newSym.owner()),
newSym);
else
return super.transform(tree);
default:
return super.transform(tree);
}
}
};
for (int i = 0; i < mixin.body.length; i++) {
Tree tree = mixin.body[i];
// Inline local code and members whose symbol has been cloned.
if (!tree.definesSymbol() ||
map.lookupSymbol(tree.symbol()) != null) {
mixinSymbolCloner.traverse(tree);
for (Iterator j = clones.iterator(); j.hasNext();) {
Symbol clone = (Symbol)j.next();
clone.setType(map.apply(clone.type()));
}
clones.clear();
body.append(mixinTreeCloner.transform(tree));
}
}
}
//########################################################################
}
|