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
411
412
413
414
415
416
417
418
419
|
/* NSC -- new Scala compiler
* Copyright 2005-2011 LAMP/EPFL
* @author Martin Odersky
*/
package scala.tools.nsc
package models
import scala.tools.nsc.Global
/** This abstract class ...
*
* @author Sean McDirmid
* @version 1.0
*/
abstract class Models {
val global: Global
import global._
def acceptPrivate = true
object Kinds extends Enumeration {
type Kind = Value
val CONSTRUCTOR = Value("Constructor")
val OBJECT = Value("Object")
val CLASS = Value("Class")
val TRAIT = Value("Trait")
val DEF = Value("Def")
val VAL = Value("Val")
val VAR = Value("Var")
val ARG = Value("Arg")
val TPARAM = Value("Type")
}
import Kinds._
def KINDS = List(CLASS, TRAIT, OBJECT, CONSTRUCTOR, TPARAM, VAL, VAR, DEF)
def labelFor(kind: Kind): String = kind.toString
def stringsFor(mods: Modifiers) = {
var modString: List[String] = Nil
if (mods.isPrivate ) modString = "private" :: modString
if (mods.isProtected) modString = "protected" :: modString
if (mods.isOverride ) modString = "override" :: modString
if (mods.isAbstract ) modString = "abstract" :: modString
if (mods.isDeferred ) modString = "abstract" :: modString
if (mods.isCase ) modString = "case" :: modString
if (mods.isSealed ) modString = "sealed" :: modString
if (mods.isFinal ) modString = "final" :: modString
if (mods.isImplicit ) modString = "implicit" :: modString
modString
}
def codeFor(kind: Kind): String = kind match {
case CONSTRUCTOR => codeFor(DEF)
case _ => labelFor(kind).toLowerCase()
}
def pluralFor(kind: Kind): String = kind match {
case CLASS => "Classes"
case _ => labelFor(kind) + "s"
}
def kindOf(tree: Tree) = {
val term0 = tree.symbol;
if (term0 != NoSymbol) {
if (term0.isVariable) VAR
else if (term0.isValueParameter) ARG
else if (term0.isMethod) {
if (term0.nameString.equals("this")) CONSTRUCTOR
else DEF
}
else if (term0.isClass) {
if (tree.asInstanceOf[MemberDef].mods.isTrait) TRAIT
else CLASS
}
else if (term0.isModule) OBJECT
else if (term0.isValue) VAL
else if (term0.isTypeParameter) TPARAM
else if (term0.isType) TPARAM
else {
// Console.err.println("UNRECOGNIZED SYMBOL: " + term0 + " " + name);
null
}
} else {
val ddef = tree.asInstanceOf[ValOrDefDef];
if (ddef.mods.isMutable) VAR;
else VAL;
}
}
abstract class Model
// def textFor(tp : AbsTypeDef) : String = tp.toString()
/**
* @param tree ...
* @return ...
*/
def textFor(tree: Tree): String = {
var ret = ""
if (tree.symbol != NoSymbol) tree.symbol.name.toString()
if (ret.equals("<init>")) ret = "this"
tree match {
case cdef: ClassDef =>
ret = ret + "[" +
(for (tparam <- cdef.tparams) yield textFor(tparam)) + "]";
cdef.mods
case vdef: ValOrDefDef =>
vdef match {
case ddef: DefDef =>
ret = ret + "[" +
(for (tparam <- ddef.tparams) yield textFor(tparam)) + "]";
for (vparams <- ddef.vparamss) {
ret = ret + "(" +
(for (vparam <- vparams) yield textFor(vparam)) + ")";
}
case _ =>
}
ret = ret + " : " + textFor(vdef.tpt)
/* Martin to Sean: Please check whether this can be dropped or does it need to be adapted?
case atd: AbsTypeDef =>
ret = ret + "[" + (for (tparam <- atd.tparams) yield textFor(tparam)) + "]" +
((if(atd.hi ne null) " <: " + textFor(atd.hi) else "") +
(if(atd.lo ne null) " >: " + textFor(atd.lo) else ""));
*/
case _ =>
ret = ret + tree.toString()
}
ret
}
def mods1(tree: Tree) = tree match {
case mdef: MemberDef => mdef.mods
case _ => NoMods
}
abstract class HasTree(val parent: Composite) extends Model with Ordered[HasTree] {
var tree : Tree = _
def update(tree0: Tree): Boolean = {
tree = tree0
false
}
def replacedBy(tree0: Tree): Boolean = true
def text: String = textFor(tree)
var mods0 = NoMods
def mods = if (mods0 != NoMods) mods0 else mods1(tree)
override def toString(): String = tree.toString()
def compare(that: HasTree): Int = {
val idx = KINDS.indexOf(kind)
val jdx = KINDS.indexOf(that.kind)
if (idx != jdx) return idx - jdx
val result = tree.symbol.nameString.compare(that.tree.symbol.nameString)
if (result != 0) result
else toString().compare(that.toString())
}
def compare [b >: HasTree <% Ordered[b]](that: b): Int = {
if (that.isInstanceOf[HasTree])
compare(that.asInstanceOf[HasTree])
else -1
}
def kind = kindOf(tree)
//override def add(from: Composite, model: HasTree): Unit = { parent.add(from, model) }
//override def remove(from: Composite, model: HasTree): Unit = { parent.remove(from, model) }
}
class ImportMod(parent0: Composite) extends HasTree(parent0) {
def treex = tree.asInstanceOf[Import]
override def replacedBy(tree0: Tree): Boolean =
if (super.replacedBy(tree0) && tree0.isInstanceOf[Import]) {
val tree1 = tree0.asInstanceOf[Import]
tree1.tpe == treex.tpe
} else
false
}
class PackageMod(parent0: Composite) extends HasTree(parent0) {
def treex = tree.asInstanceOf[PackageDef]
}
trait Composite extends Model {
import scala.collection.mutable._
class Members extends HashSet[HasTree]
// val members = new Members
object members extends Members
def isMember(tree: Tree): Boolean = tree.isInstanceOf[Import] // imports welcome anywhere.
def member(tree: Tree, members: List[Tree]): Tree = tree
def update0(members1: List[Tree]): Boolean = {
// Console.err.println("update0 " + this + " " + members1)
// Martin: This is rather ugly code. We should use pattern matching here!
if (members1.length == 1 && members1.head.isInstanceOf[PackageDef])
return update0(members1.head.asInstanceOf[PackageDef].stats)
val marked = new HashSet[HasTree]
var updated = false
for (mmbr1 <- members1) if (mmbr1.isInstanceOf[PackageDef]) {
Console.err.println("PACKAGE: " + mmbr1.symbol + " " + members1.length)
} else if (isMember(mmbr1)) {
val mmbr2 = member(mmbr1, members1)
if (mmbr2 ne null) {
var found = false
for (mmbr <- members) if (!found && mmbr.replacedBy(mmbr2)) {
//Console.err.println("REPLACE: " + mmbr + " with " + mmbr2)
mmbr.mods0 = mods1(mmbr1)
found = true
updated = mmbr.update(mmbr2) || updated
marked += mmbr
}
if (!found) {
updated = true
val add = modelFor(mmbr2, this)
add.update(mmbr2)
add.mods0 = mods1(mmbr1) &
~symtab.Flags.ACCESSOR & ~symtab.Flags.SYNTHETIC
val sz = members.size
members += (add)
assert(members.size == sz + 1)
marked += add
}
}
// Console.err.println("update1 " + this + " " + members + " " + marked)
}
val sz = members.size
members.intersect(marked)
updated = updated || sz < members.size
// check if anything was removed!
updated
}
}
abstract class MemberMod(parent0: Composite) extends HasTree(parent0) {
def treex = tree.asInstanceOf[MemberDef]
def name: Name = treex.name
override def replacedBy(tree0: Tree): Boolean =
if (super.replacedBy(tree0) && tree0.isInstanceOf[MemberDef]) {
val tree1 = tree0.asInstanceOf[MemberDef]
treex.toString().equals(tree1.toString())
} else false
override def update(tree0: Tree): Boolean = {
val updated = (tree eq null) || (treex.mods != tree0.asInstanceOf[MemberDef].mods)
super.update(tree0) || updated;
}
}
abstract class MemberComposite(parent0: Composite) extends MemberMod(parent0) with Composite
trait HasClassObjects extends Composite {
override def isMember(tree: Tree): Boolean =
super.isMember(tree) || tree.isInstanceOf[ImplDef]
}
abstract class ValOrDefMod(parent0: Composite) extends MemberComposite(parent0) with HasClassObjects {
override def replacedBy(tree0: Tree): Boolean =
super.replacedBy(tree0) && tree0.isInstanceOf[ValOrDefDef]
override def update(tree0: Tree): Boolean = {
val tree1 = tree0.asInstanceOf[ValOrDefDef]
val updated = (tree eq null) || treex.tpe != tree1.tpe
update0(flatten(tree1.rhs, (tree2: Tree) => isMember(tree2)))
super.update(tree0) || updated
}
}
class ValMod(parent0: Composite) extends ValOrDefMod(parent0) {
def treez = tree.asInstanceOf[ValDef]
override def replacedBy(tree0: Tree): Boolean =
super.replacedBy(tree0) && tree0.isInstanceOf[ValDef]
}
class DefMod(parent0: Composite) extends ValOrDefMod(parent0) {
def treez = tree.asInstanceOf[DefDef]
override def replacedBy(tree0: Tree) : Boolean =
if (super.replacedBy(tree0) && tree0.isInstanceOf[DefDef]) {
val tree1 = tree0.asInstanceOf[DefDef]
if (tree1.vparamss.length == treez.vparamss.length) {
val tpz = for (vd <- treez.vparamss) yield for (xd <- vd) yield xd.tpe;
val tp1 = for (vd <- tree1.vparamss) yield for (xd <- vd) yield xd.tpe;
tpz == tp1
} else false
} else false
}
abstract class ImplMod(parent0: Composite)
extends MemberComposite(parent0) with HasClassObjects {
override def replacedBy(tree0: Tree): Boolean =
super.replacedBy(tree0) && tree0.isInstanceOf[ImplDef]
override def isMember(tree: Tree): Boolean = (super.isMember(tree) ||
(tree.isInstanceOf[ValOrDefDef] &&
(acceptPrivate || !tree.asInstanceOf[ValOrDefDef].mods.isPrivate)
/* && !tree.asInstanceOf[ValOrDefDef].mods.isPrivate */
/* && !tree.asInstanceOf[ValOrDefDef].mods.isAccessor */) ||
treeInfo.isAliasTypeDef(tree))
override def member(tree: Tree, members: List[Tree]): Tree = {
val tree0 = if (tree.isInstanceOf[DefDef]) {
val ddef = tree.asInstanceOf[DefDef]
ddef.mods
if (ddef.mods.isAccessor && (ddef.symbol ne null)) {
val sym0 = ddef.symbol;
if (sym0.isSetter) return null;
assert(sym0.isGetter);
val sym = sym0.accessed
val ret = if (sym == NoSymbol) {
val sym = analyzer.underlying(sym0)
//val name = nme.getterToSetter(sym0.name)
//val setter = sym0.owner.info.decl(name);
val isVar = sym.isVariable;
val mods = (ddef.mods |
(if (isVar) symtab.Flags.MUTABLE else 0) | symtab.Flags.DEFERRED) &
~symtab.Flags.ACCESSOR & ~symtab.Flags.SYNTHETIC
val tree =
ValDef(mods, ddef.name, ddef.tpt, ddef.rhs).setPos(ddef.pos).setSymbol(sym);
tree :: Nil;
} else for (member <- members if member.symbol == sym) yield member
if (ret.isEmpty) null
else ret.head
} else tree
} else super.member(tree, members)
def sym = tree0.symbol
if ((tree0 eq null) || tree0.pos == NoPosition) null
else if (!acceptPrivate &&
tree0.isInstanceOf[ValOrDefDef] &&
tree.asInstanceOf[ValOrDefDef].mods.isPrivate) null
else tree0
}
def children(tree0: Tree): List[Tree] =
tree0.asInstanceOf[ImplDef].impl.body
override def update(tree0: Tree): Boolean = {
var updated = update0(children(tree0))
super.update(tree0) || updated
}
}
class ClassMod(parent0: Composite) extends ImplMod(parent0) {
def treez = tree.asInstanceOf[ClassDef]
override def replacedBy(tree0: Tree): Boolean =
super.replacedBy(tree0) && tree0.isInstanceOf[ClassDef]
}
class ObjectMod(parent0: Composite) extends ImplMod(parent0) {
def treez = tree.asInstanceOf[ModuleDef]
override def replacedBy(tree0: Tree): Boolean =
super.replacedBy(tree0) && tree0.isInstanceOf[ModuleDef]
}
class TypeMod(parent0: Composite) extends MemberMod(parent0) {
override def replacedBy(tree0 : Tree) : Boolean = (super.replacedBy(tree0) && tree0.isInstanceOf[TypeDef]);
}
def SourceMod(original: CompilationUnit) = new SourceMod(original)
class SourceMod(val original: CompilationUnit) extends Composite with HasClassObjects {
update(original)
//var listener : Listener = null;
def update(unit: CompilationUnit) = unit.body match {
case pdef: PackageDef => try {
update0(pdef.stats)
} catch {
case e: Error => members.clear; update0(pdef.stats)
}
case _ =>
}
override def isMember(tree: Tree): Boolean =
super.isMember(tree) || tree.isInstanceOf[Import]
}
def flatten0(exprs: List[Tree], filter: (Tree) => Boolean): List[Tree] =
for (expr <- exprs; t: Tree <- flatten(expr,filter)) yield t
def flatten(expr: Tree, filter: (Tree) => Boolean): List[Tree] =
if (filter(expr)) expr :: Nil; else expr match {
case Block(stats, last) =>
flatten0(stats, filter) ::: flatten(last, filter)
case If(cond, thenp, elsep) =>
flatten(cond,filter) ::: flatten(thenp,filter) ::: flatten(elsep,filter);
case Assign(lhs, rhs) =>
flatten(rhs, filter)
case CaseDef(pat, guard, body) =>
flatten(body, filter)
case Return(expr0) =>
flatten(expr0, filter)
case Throw(expr0) =>
flatten(expr0,filter)
case Try(block, catches, finalizer) =>
flatten(block, filter) ::: flatten(finalizer, filter) ::: flatten0(catches, filter)
case Match(selector, cases) =>
flatten(selector, filter) ::: flatten0(cases, filter)
case Apply(fun, args) =>
flatten(fun, filter) ::: flatten0(args, filter)
case TypeApply(fun, args) =>
flatten(fun, filter) ::: flatten0(args, filter)
case _ =>
Nil
}
def modelFor(tree: Tree, parent: Composite): HasTree = tree match {
case _: ValDef => new ValMod(parent)
case _: DefDef => new DefMod(parent)
case _: ClassDef => new ClassMod(parent)
case _: ModuleDef => new ObjectMod(parent)
case _: TypeDef => new TypeMod(parent)
case _: Import => new ImportMod(parent)
}
}
|