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
|
package scala.tools.nsc.transform
import scala.tools.nsc.symtab.SymbolTable
import scala.reflect
import collection.mutable.HashMap
/** Functions to reify (and un-reify) symbols, types, and trees.
* These can be used with only a symbol table; they do not
* need a full compiler.
*
* @author Gilles Dubochet, Lex Spoon
*/
trait Reifiers {
val symbols: SymbolTable
import symbols._
private def mkGlobalSymbol(fullname: String, sym: Symbol): reflect.Symbol =
if (sym.isClass) reflect.Class(fullname)
else if (sym.isType) reflect.TypeField(fullname, reify(sym.info))
else if (sym.isMethod) reflect.Method(fullname, reify(sym.info))
else reflect.Field(fullname, reify(sym.info));
def reify(sym: Symbol): reflect.Symbol = {
if (sym.isRoot || sym.isRootPackage || sym.isEmptyPackageClass || sym.isEmptyPackage)
reflect.RootSymbol
else if (sym.owner.isTerm)
reflect.NoSymbol
else reify(sym.owner) match {
case reflect.NoSymbol =>
reflect.NoSymbol;
case reflect.RootSymbol =>
mkGlobalSymbol(sym.name.toString(), sym)
case reflect.Class(ownername) =>
mkGlobalSymbol(ownername + "." + sym.name, sym)
case _ =>
reflect.NoSymbol
}
}
var _log_reify_type_ = false
def reify(tp: Type): reflect.Type = tp match {
case ErrorType =>
reflect.NoType
case WildcardType =>
if (_log_reify_type_) println("cannot handle WildcardType")
reflect.NoType
case NoType =>
reflect.NoType
case NoPrefix =>
reflect.NoType
case ThisType(sym) =>
val rsym = reify(sym)
reflect.ThisType(rsym)
case SingleType(pre, sym) =>
reflect.SingleType(reify(pre), reify(sym))
case ConstantType(value) =>
reify(value.tpe)
case TypeRef(pre, sym, args) =>
val rpre = reify(pre)
val rsym = reify(sym)
val rargs = args map reify
val beforeArgs = reflect.PrefixedType(rpre, rsym)
if (rargs.isEmpty)
beforeArgs
else if (rpre == reflect.NoType || rsym == reflect.NoSymbol)
beforeArgs
else
reflect.AppliedType(beforeArgs, rargs)
case TypeBounds(lo, hi) =>
reflect.TypeBounds(reify(lo), reify(hi))
case RefinedType(parents, defs) =>
if (_log_reify_type_) println("cannot handle RefinedType "+tp); reflect.NoType
case ClassInfoType(parents, defs, clazz) =>
if (_log_reify_type_) println("cannot handle ClassInfoType "+tp); reflect.NoType
case MethodType(params, result) =>
reflect.MethodType(params.map(reify), reify(result))
case PolyType(tparams, result) =>
val boundss =
for {
param <- tparams
TypeBounds(lo,hi) = param.info.bounds
} yield (reify(lo), reify(hi))
reflect.PolyType(
tparams.map(reify),
boundss,
reify(result))
//todo: treat ExistentialType
case AnnotatedType(annots, tp, _) =>
reify(tp)
case _ =>
println("could not reify: " + tp)
reflect.NoType
}
/** This is woefully incomplete. It is barely enough
* to process the types of Constant's .
*/
def unreify(tpe: reflect.Type): Type =
tpe match {
case reflect.NoPrefix => NoPrefix
case reflect.NoType => NoType
case reflect.NamedType(fullname) =>
//NamedType(fullname)
println("NamedType: " + fullname)
NoType
case reflect.PrefixedType(_, reflect.Class("scala.Array")) =>
definitions.ArrayClass.tpe
case reflect.PrefixedType(_, reflect.Class("java.lang.String")) =>
definitions.StringClass.tpe
case reflect.PrefixedType(_, reflect.Class("scala.Unit")) =>
definitions.UnitClass.tpe
case reflect.PrefixedType(_, reflect.Class("scala.Boolean")) =>
definitions.BooleanClass.tpe
case reflect.PrefixedType(_, reflect.Class("scala.Byte")) =>
definitions.ByteClass.tpe
case reflect.PrefixedType(_, reflect.Class("scala.Short")) =>
definitions.ShortClass.tpe
case reflect.PrefixedType(_, reflect.Class("scala.Int")) =>
definitions.IntClass.tpe
case reflect.PrefixedType(_, reflect.Class("scala.Long")) =>
definitions.LongClass.tpe
case reflect.PrefixedType(_, reflect.Class("scala.Float")) =>
definitions.FloatClass.tpe
case reflect.PrefixedType(_, reflect.Class("scala.Double")) =>
definitions.DoubleClass.tpe
case reflect.PrefixedType(pre, sym) =>
NoType
case reflect.SingleType(pre, sym) =>
SingleType(unreify(pre), unreify(sym))
case reflect.ThisType(clazz) =>
ThisType(unreify(clazz))
case reflect.AppliedType(tpe, args) =>
val untpe = unreify(tpe)
if (untpe == NoType)
NoType
else
appliedType(untpe, args.map(unreify))
case reflect.TypeBounds(lo, hi) =>
TypeBounds(unreify(lo), unreify(hi))
case reflect.MethodType(params, restpe) =>
MethodType(params.map(unreify), unreify(restpe))
case reflect.PolyType(typeParams, typeBounds, resultType) =>
PolyType(typeParams.map(unreify), unreify(resultType))
//todo: treat ExistentialType
case _ => NoType
}
/** This is woefully incomplete. It is barely enough
* to process the types of Constant's .
*/
def unreify(symbol: reflect.Symbol): Symbol =
symbol match {
case reflect.Class(fullname) =>
fullname match {
case "scala.Unit" => definitions.UnitClass
case "scala.Boolean" => definitions.BooleanClass
case "scala.Byte" => definitions.ByteClass
case "scala.Short" => definitions.ShortClass
case "scala.Int" => definitions.IntClass
case "scala.Long" => definitions.LongClass
case "scala.Float" => definitions.FloatClass
case "scala.Double" => definitions.DoubleClass
case "scala.Array" => definitions.ArrayClass
case _ => NoSymbol
}
case _ => NoSymbol
}
case class FreeValue(tree: Tree) extends reflect.Tree
class ReifyEnvironment extends HashMap[Symbol, reflect.Symbol] {
var targets = new HashMap[String, Option[reflect.LabelSymbol]]()
def addTarget(name: String, target: reflect.LabelSymbol): Unit =
targets.update(name, Some(target))
def getTarget(name: String): Option[reflect.LabelSymbol] =
targets.get(name) match {
case None =>
targets.update(name, None)
None
//case Some(None) => None //bq:redundant
case Some(tgt) => tgt
}
def hasAllTargets: Boolean =
targets.iterator.map(_._2).forall {
case Some(_) => true
case None => false
}
override def update(sym: Symbol, rsym: reflect.Symbol) =
super.update(sym,rsym)
}
class Reifier(env: ReifyEnvironment, currentOwner: reflect.Symbol)
{
def reify(tree: Tree): reflect.Tree = tree match {
case Ident(_) =>
val rsym = reify(tree.symbol);
//Console.println("LiftCode: seen ident")
if (rsym == reflect.NoSymbol) {
//Console.println(" free = "+tree)
FreeValue(tree)
} else {
//Console.println(" rsym = "+rsym)
reflect.Ident(rsym)
}
case Select(qual, _) =>
val rsym = reify(tree.symbol);
if (rsym == reflect.NoSymbol) throw new TypeError("cannot reify symbol: " + tree.symbol)
else reflect.Select(reify(qual), reify(tree.symbol))
case _ : StubTree => reflect.Literal(0)
case Literal(constant) =>
reflect.Literal(constant.value)
case Apply(name, args) if name.toString().startsWith("label$") =>
env.getTarget(name.toString()) match {
case None => throw new TypeError("cannot reify tree (no forward jumps allowed): " + tree)
case Some(label) => reflect.Goto(label)
}
case Apply(fun, args) =>
reflect.Apply(reify(fun), args map reify)
case TypeApply(fun, args) =>
reflect.TypeApply(reify(fun), args map (_.tpe) map reify)
case Function(vparams, body) =>
var env1 = env
for (vparam <- vparams) {
val local = reflect.LocalValue(
currentOwner, vparam.symbol.name.toString(), reify(vparam.symbol.tpe));
env1.update(vparam.symbol, local);
}
reflect.Function(vparams map (_.symbol) map env1,
new Reifier(env1, currentOwner).reify(body))
case tree@This(_) if tree.symbol.isModule =>
// there is no reflect node for a module's this, so
// represent it as a selection of the module
reify(
Select(This(tree.symbol.owner), tree.symbol.name))
case This(_) =>
reflect.This(reify(tree.symbol))
case Block(stats, expr) =>
reflect.Block(stats.map(reify), reify(expr))
case New(clazz) if (clazz.isType) =>
val reifiedSymbol = reify(clazz.symbol)
reflect.New(reflect.Ident(reifiedSymbol))
case New(clazz) =>
val reifiedClass = reify(clazz)
reflect.New(reifiedClass)
case Typed(t, _) =>
reify(t)
case If(cond, thenp, elsep) =>
reflect.If(reify(cond), reify(thenp), reify(elsep))
case Assign(lhs, rhs) =>
reflect.Assign(reify(lhs), reify(rhs))
case LabelDef(name, Nil, body) =>
val sym = new reflect.LabelSymbol(name.toString())
env.addTarget(name.toString(), sym)
val res = reflect.Target(sym, reify(body))
res
case vd @ ValDef(mods, name, tpt, rhs) =>
val rtpe = reify(vd.tpe) // will return null, currently?!
val sym = reflect.LocalValue(currentOwner, name.toString(), rtpe)
env(vd.symbol) = sym // bq: despite Scala's scoping rules, this should work because references to vd.symbol were type checked.
val rhs_ = reify(rhs)
reflect.ValDef(sym, rhs_)
case cd @ ClassDef(mods, name, tparams, impl) =>
if(!tparams.isEmpty)
throw new TypeError("cannot handle polymorphic ClassDef ("+name+"): " + tparams)
val rsym = reify(cd.symbol)
val rimp = reify(impl)
val rtpe = reify(impl.self.tpt.tpe) //todo: update
reflect.ClassDef(rsym, rtpe, rimp.asInstanceOf[reflect.Template])
case tmpl @ Template(parents, self, body) =>
val rparents = for (p <- parents) yield { reify(p.tpe) }
//todo: add self to reified templates
reflect.Template(rparents, body.map(reify))
case dd @ DefDef(mods, name, tparams, vparamss, tpt, rhs) =>
if(!tparams.isEmpty)
throw new TypeError("cannot handle polymorphic DefDef ("+name+"): " + tparams)
val rsym = reify(dd.symbol)
val rparss = vparamss map { x => x map (reify) }
val rret = reify(tpt.tpe)
val rrhs = reify(rhs)
reflect.DefDef(rsym, rparss, rret, rrhs)
case sp @ Super(qual: Name, mix: Name) =>
val rsym = reify(sp.symbol)
reflect.Super(rsym)
case _ =>
throw new TypeError("cannot reify tree ("+tree.getClass()+"): " + tree)
}
def reify(sym: Symbol): reflect.Symbol =
env.get(sym) match {
case Some(rsym) =>
rsym
case None =>
Reifiers.this.reify(sym)
}
def reify(tpe: Type): reflect.Type =
Reifiers.this.reify(tpe)
}
def reify(tree: Tree): reflect.Tree =
new Reifier(new ReifyEnvironment(), reflect.NoSymbol).reify(tree)
}
|