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
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
\* */
// $Id$
package scalac.ast;
import scalac.Global;
import scalac.Unit;
{#Imports#}
/**
* A default transformer class. This class traverses the abstract
* syntax tree but does not do any transformations.
*/
public class Transformer {
//########################################################################
// Public Fields
/** The global environment */
public final Global global;
/** The tree factory */
public final TreeFactory make;
/** The tree copier */
public final TreeCopier copy;
/** A tree generator */
public final TreeGen gen;
//########################################################################
// Public Constructors
public Transformer(Global global) {
this(global, global.make);
}
public Transformer(Global global, TreeFactory make) {
this(global, make, new LazyTreeCopier(make));
}
public Transformer(Global global, TreeFactory make, TreeCopier copy) {
this.global = global;
this.make = make;
this.copy = copy;
this.gen = global.treeGen;
}
//########################################################################
// Public Methods
public void apply(Unit[] units) {
for (int i = 0; i < units.length; i++) apply(units[i]);
}
public void apply(Unit unit) {
unit.global.log("transforming " + unit);
unit.body = transform(unit.body);
}
public Tree transform(Tree tree) {
{#TreeSwitch#}
}
public Template transform(Template tree) {
return (Template)transform((Tree)tree);
}
{#TransformArrays#}
//########################################################################
}
public class GenTransformer {
//########################################################################
// Public Fields
/** The global environment */
public final Global global;
/** A tree generator */
public final TreeGen gen;
//########################################################################
// Public Constructors
/** Initializes this instance. */
public GenTransformer(Global global) {
this.global = global;
this.gen = global.treeGen;
}
//########################################################################
// Public Methods
/** Transforms the given units. */
public void apply(Unit[] units) {
for (int i = 0; i < units.length; i++) apply(units[i]);
}
/** Transforms the given unit. */
public void apply(Unit unit) {
unit.global.log("transforming " + unit);
unit.body = transform(unit.body);
}
/** Transforms the given tree. */
public Tree transform(Tree tree) {
switch (tree) {
// case Bad():
case Empty:
return tree;
case ClassDef(_, _, _, _, _, Template impl):
Symbol clasz = tree.symbol();
Tree[] parents = transform(impl.parents);
Tree[] body = transform(impl.body);
return gen.ClassDef(clasz, parents, impl.symbol(), body);
case PackageDef(Tree packaged, Template(Tree[] parents, Tree[] body)):
assert parents.length == 0: tree;
return gen.PackageDef(packaged.symbol(), transform(body));
// case ModuleDef(_, _, _, Template impl):
case ValDef(_, _, _, Tree rhs):
return gen.ValDef(tree.symbol(), transform(rhs));
// case PatDef(int mods, Tree pat, Tree rhs):
case DefDef(_, _, _, _, _, Tree rhs):
return gen.DefDef(tree.symbol(), transform(rhs));
// case AbsTypeDef(_, _, Tree rhs, Tree lobound):
// case AliasTypeDef(_, _, AbsTypeDef[] tparams, Tree rhs):
// case Import(Tree expr, Name[] selectors):
// case CaseDef(Tree pat, Tree guard, Tree body):
// case Template(Tree[] parents, Tree[] body):
case LabelDef(_, Ident[] params, Tree rhs):
Symbol label = tree.symbol();
return gen.LabelDef(label, Tree.symbolOf(params), transform(rhs));
case Block(Tree[] stats):
return gen.Block(tree.pos, transform(stats));
// case Sequence(Tree[] trees):
// case Alternative(Tree[] trees):
// case Bind(Name name, Tree rhs):
// case Visitor(CaseDef[] cases):
// case Function(ValDef[] vparams, Tree body):
case Assign(Tree lhs, Tree rhs):
return gen.Assign(tree.pos, transform(lhs), transform(rhs));
case If(Tree cond, Tree thenp, Tree elsep):
cond = transform(cond);
thenp = transform(thenp);
elsep = transform(elsep);
return gen.If(tree.pos, cond, thenp, elsep);
case Switch(Tree test, int[] tags, Tree[] bodies, Tree otherwise):
test = transform(test);
bodies = transform(bodies);
otherwise = transform(otherwise);
return gen.Switch(tree.pos, test, tags, bodies, otherwise);
case Return(Tree expr):
return gen.Return(tree.pos, tree.symbol(), expr);
// case Throw(Tree expr):
case New(Template(Tree[] base, Tree[] body)):
assert base.length == 1 && body.length == 0: tree;
return gen.New(tree.pos, transform(base[0]));
// case Typed(Tree expr, Tree tpe):
case TypeApply(Tree fun, Tree[] args):
return gen.TypeApply(transform(fun), transform(args));
case Apply(Tree fun, Tree[] args):
return gen.Apply(transform(fun), transform(args));
case Super(_, _):
return gen.Super(tree.pos, tree.symbol());
case This(_):
return gen.This(tree.pos, tree.symbol());
case Select(Tree qualifier, _):
return gen.Select(tree.pos, transform(qualifier), tree.symbol());
case Ident(_):
return gen.Ident(tree.pos, tree.symbol());
case Literal(Object value):
return gen.mkLit(tree.pos, value);
case TypeTerm():
return tree;
// case SingletonType(Tree ref):
// case SelectFromType(Tree qualifier, Name selector):
// case FunType(Tree[] argtpes, Tree restpe):
// case CompoundType(Tree[] parents, Tree[] refinements):
// case AppliedType(Tree tpe, Tree[] args):
// case Try(Tree block, Tree catcher, Tree finalizer):
default:
throw Debug.abort("illegal case", tree);
}
}
{#TransformArrays#}
//########################################################################
}
|