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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
|
/* NSC -- new Scala compiler
* Copyright 2005-2007 LAMP/EPFL
* @author Martin Odersky
*/
// $Id$
package scala.tools.nsc.ast
import java.io.{PrintWriter, StringWriter}
import scala.tools.nsc.symtab.Flags
import scala.tools.nsc.symtab.Flags._
import scala.tools.nsc.util.{HashSet, Position, SourceFile}
trait Trees requires Global {
//statistics
var nodeCount = 0
case class Modifiers(flags: int, privateWithin: Name, annotations: List[Annotation]) {
def isCovariant = hasFlag(COVARIANT )
def isContravariant = hasFlag(CONTRAVARIANT)
def isPrivate = hasFlag(PRIVATE )
def isProtected = hasFlag(PROTECTED)
def isVariable = hasFlag(MUTABLE )
def isArgument = hasFlag(PARAM )
def isAccessor = hasFlag(ACCESSOR )
def isOverride = hasFlag(OVERRIDE )
def isAbstract = hasFlag(ABSTRACT )
def isDeferred = hasFlag(DEFERRED )
def isCase = hasFlag(CASE )
def isSealed = hasFlag(SEALED )
def isFinal = hasFlag(FINAL )
def isTrait = hasFlag(TRAIT )
def isImplicit = hasFlag(IMPLICIT )
def isPublic = !isPrivate && !isProtected
def hasFlag(flag: int) = (flag & flags) != 0
def & (flag: Int): Modifiers = {
val flags1 = flags & flag
if (flags1 == flags) this
else Modifiers(flags1, privateWithin, annotations)
}
def &~ (flag: Int): Modifiers = {
val flags1 = flags & (~flag)
if (flags1 == flags) this
else Modifiers(flags1, privateWithin, annotations)
}
def | (flag: int): Modifiers = {
val flags1 = flags | flag
if (flags1 == flags) this
else Modifiers(flags1, privateWithin, annotations)
}
def withAnnotations(annots: List[Annotation]) =
if (annots.isEmpty) this
else Modifiers(flags, privateWithin, annotations ::: annots)
}
def Modifiers(flags: int, privateWithin: Name): Modifiers = Modifiers(flags, privateWithin, List())
def Modifiers(flags: int): Modifiers = Modifiers(flags, nme.EMPTY.toTypeName)
def Modifiers(flags: long): Modifiers = Modifiers(flags.asInstanceOf[int])
val NoMods = Modifiers(0)
abstract class Tree {
{
import util.Statistics
if (Statistics.enabled) nodeCount = nodeCount + 1
}
private var rawpos: PositionType = NoPos
def pos = rawpos
var tpe: Type = _
def setPos(pos: PositionType): this.type = { rawpos = pos; this }
def setType(tp: Type): this.type = { tpe = tp; this }
def symbol: Symbol = null
def symbol_=(sym: Symbol): unit =
throw new Error("symbol_= inapplicable for " + this)
def setSymbol(sym: Symbol): this.type = { symbol = sym; this }
def hasSymbol = false
def isDef = false
def isTerm = false
def isType = false
def isEmpty = false
def isErroneous = (tpe ne null) && tpe.isErroneous
override def toString(): String = {
val buffer = new StringWriter()
val printer = treePrinters.create(new PrintWriter(buffer))
printer.print(this); printer.flush
buffer.toString()
}
override def hashCode(): int = super.hashCode()
override def equals(that: Any): boolean = that match {
case t: Tree => this eq t
case _ => false
}
def equalsStructure(that: Tree): Boolean = {
val this0 = this.asInstanceOf[Product];
val that0 = that.asInstanceOf[Product];
if (this == that) return true;
if (this.getClass != that.getClass) return false;
assert(this0.arity == that0.arity);
def equals0(thiz : Any, that : Any) : Boolean = thiz match {
case thiz : Tree =>
thiz.equalsStructure(that.asInstanceOf[Tree]);
case thiz : List[_] =>
val that0 = that.asInstanceOf[List[Any]];
if (thiz.length != that0.length) false;
else {
val results0 = for (val i <- 0.until(thiz.length).toList)
yield equals0(thiz(i), that0(i));
results0.foldLeft(true)((x,y) => x && y);
}
case thiz => thiz == that;
}
val results = for (val i <- 0.until(this0.arity).toList) yield
equals0(this0.element(i), that0.element(i));
val b = results.foldLeft(true)((x,y) => x && y);
b && (if (tpe == null || tpe == NoType) that.tpe == null || that.tpe == NoType
else tpe == that.tpe)
}
def duplicate: this.type =
(duplicator transform this).asInstanceOf[this.type]
def copyAttrs(tree: Tree): this.type = {
rawpos = tree.rawpos
tpe = tree.tpe
if (hasSymbol) symbol = tree.symbol
this
}
}
trait SymTree extends Tree {
override def hasSymbol = true
override var symbol: Symbol = NoSymbol
}
abstract class DefTree extends SymTree {
def name: Name
override def isDef = true
}
trait TermTree extends Tree {
override def isTerm = true
}
/** A tree for a type. Note that not all type trees implement
* this trait; in particular, Ident's are an exception. */
trait TypTree extends Tree {
override def isType = true
}
// ----- auxiliary objects and methods ------------------------------
private val duplicator = new Transformer {
override val copy = new StrictTreeCopier
}
private def syntheticParams(owner: Symbol, mtp: Type): List[List[Symbol]] = {
var cnt = 0
def freshName() = { cnt = cnt + 1; newTermName("x$" + cnt) }
def synthetics(mtp: Type): List[List[Symbol]] = mtp match {
case PolyType(_, restp) =>
synthetics(restp)
case MethodType(formals, restp) =>
(formals map (f => owner.newValueParameter(owner.pos, freshName()).setInfo(f))) ::
synthetics(restp)
case _ =>
List()
}
synthetics(mtp)
}
// def nextPhase = if (phase.id > globalPhase.id) phase else phase.next;
// ----- tree node alternatives --------------------------------------
/** The empty tree */
case object EmptyTree extends TermTree {
tpe = NoType
override def isEmpty = true
}
abstract class MemberDef extends DefTree {
def mods: Modifiers
def keyword: String = this match {
case AliasTypeDef(_, _, _, _) => "type"
case ClassDef(_, _, _, _, _) => "class"
case DefDef(_, _, _, _, _, _) => "def"
case ModuleDef(_, _, _) => "object"
case PackageDef(_, _) => "package"
case ValDef(mods, _, _, _) => if (mods.isVariable) "var" else "val"
case _ => ""
}
final def hasFlag(mask: long): boolean = (mods.flags & mask) != 0
}
/** Package clause */
case class PackageDef(name: Name, stats: List[Tree])
extends MemberDef {
def mods = NoMods
}
def PackageDef(sym: Symbol, stats: List[Tree]): PackageDef =
PackageDef(sym.name, stats) setSymbol sym
abstract class ImplDef extends MemberDef {
def impl: Template
}
/** Class definition */
case class ClassDef(mods: Modifiers, name: Name, tparams: List[AbsTypeDef], self: ValDef, impl: Template)
extends ImplDef
/**
* @param sym the class symbol
* @param impl ...
* @return ...
*/
def ClassDef(sym: Symbol, impl: Template): ClassDef =
posAssigner.atPos(sym.pos) {
ClassDef(Modifiers(sym.flags),
sym.name,
sym.typeParams map AbsTypeDef,
if (sym.thisSym == sym || phase.erasedTypes) emptyValDef else ValDef(sym.thisSym),
impl) setSymbol sym
}
/** Construct class definition with given class symbol, value parameters,
* supercall arguments and template body.
*
* @param sym the class symbol
* @param vparamss the value parameters -- if they have symbols they
* should be owned by `sym'
* @param argss the supercall arguments
* @param body the template statements without primary constructor
* and value parameter fields.
* @return ...
*/
def ClassDef(sym: Symbol, constrMods: Modifiers, vparamss: List[List[ValDef]], argss: List[List[Tree]], body: List[Tree]): ClassDef =
ClassDef(sym, Template(sym.info.parents map TypeTree, constrMods, vparamss, argss, body))
/** Singleton object definition
*
* @param mods
* @param name
* @param impl
*/
case class ModuleDef(mods: Modifiers, name: Name, impl: Template)
extends ImplDef
/**
* @param sym the class symbol
* @param impl ...
* @return ...
*/
def ModuleDef(sym: Symbol, impl: Template): ModuleDef =
posAssigner.atPos(sym.pos) {
ModuleDef(Modifiers(sym.flags), sym.name, impl)
}
abstract class ValOrDefDef extends MemberDef {
def tpt: Tree
def rhs: Tree
}
/** Value definition
*
* @param mods
* @param name
* @param tpt
* @param rhs
*/
case class ValDef(mods: Modifiers, name: Name, tpt: Tree, rhs: Tree)
extends ValOrDefDef {
assert(tpt.isType, tpt)
assert(rhs.isTerm, rhs)
}
def ValDef(sym: Symbol, rhs: Tree): ValDef = {
posAssigner.atPos(sym.pos) {
ValDef(Modifiers(sym.flags), sym.name, TypeTree(sym.tpe), rhs) setSymbol sym
}
}
def ValDef(sym: Symbol): ValDef = ValDef(sym, EmptyTree)
object emptyValDef extends ValDef(Modifiers(PRIVATE), nme.WILDCARD, TypeTree(NoType), EmptyTree)
/** Method definition
*
* @param mods
* @param name
* @param tparams
* @param vparamss
* @param tpt
* @param rhs
*/
case class DefDef(mods: Modifiers, name: Name, tparams: List[AbsTypeDef],
vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree)
extends ValOrDefDef {
assert(tpt.isType)
assert(rhs.isTerm)
}
def DefDef(sym: Symbol, mods: Modifiers, vparamss: List[List[ValDef]], rhs: Tree): DefDef =
posAssigner.atPos(sym.pos) {
assert(sym != NoSymbol)
DefDef(Modifiers(sym.flags),
sym.name,
sym.typeParams map AbsTypeDef,
vparamss,
TypeTree(sym.tpe.finalResultType),
rhs) setSymbol sym
}
def DefDef(sym: Symbol, vparamss: List[List[ValDef]], rhs: Tree): DefDef =
DefDef(sym, Modifiers(sym.flags), vparamss, rhs)
def DefDef(sym: Symbol, mods: Modifiers, rhs: List[List[Symbol]] => Tree): DefDef = {
val vparamss = syntheticParams(sym, sym.tpe)
DefDef(sym, mods, vparamss map (.map(ValDef)), rhs(vparamss))
}
def DefDef(sym: Symbol, rhs: List[List[Symbol]] => Tree): DefDef =
DefDef(sym, Modifiers(sym.flags), rhs)
/** Abstract type or type parameter
*
* @param mods
* @param name
* @param lo
* @param hi
*/
case class AbsTypeDef(mods: Modifiers, name: Name, lo: Tree, hi: Tree)
extends DefTree {
def namePos = pos - name.length
}
def AbsTypeDef(sym: Symbol): AbsTypeDef =
posAssigner.atPos(sym.pos) {
AbsTypeDef(Modifiers(sym.flags), sym.name,
TypeTree(sym.info.bounds.lo), TypeTree(sym.info.bounds.hi))
}
/** Type alias
*
* @param mods
* @param name
* @param tparams
* @param rhs
*/
case class AliasTypeDef(mods: Modifiers, name: Name, tparams: List[AbsTypeDef], rhs: Tree)
extends MemberDef
def AliasTypeDef(sym: Symbol, rhs: Tree): AliasTypeDef =
posAssigner.atPos(sym.pos) {
AliasTypeDef(Modifiers(sym.flags), sym.name, sym.typeParams map AbsTypeDef, rhs)
}
/** <p>
* Labelled expression - the symbols in the array (must be Idents!)
* are those the label takes as argument
* </p>
* <p>
* The symbol that is given to the labeldef should have a MethodType
* (as if it were a nested function)
* </p>
* <p>
* Jumps are apply nodes attributed with label symbol, the arguments
* will get assigned to the idents.
* </p>
* <p>
* Note: on 2005-06-09 Martin, Iuli, Burak agreed to have forward
* jumps within a Block.
* </p>
*/
case class LabelDef(name: Name, params: List[Ident], rhs: Tree)
extends DefTree with TermTree {
assert(rhs.isTerm)
}
/**
* @param sym the class symbol
* @param params ...
* @param rhs ...
* @return ...
*/
def LabelDef(sym: Symbol, params: List[Symbol], rhs: Tree): LabelDef =
posAssigner.atPos(sym.pos) {
LabelDef(sym.name, params map Ident, rhs) setSymbol sym
}
/** Import clause
*
* @param expr
* @param selectors
*/
case class Import(expr: Tree, selectors: List[(Name, Name)])
extends SymTree
/** Annotation application (constructor arguments + name-value pairs) */
case class Annotation(constr: Tree, elements: List[Tree])
extends TermTree
/** Documented definition, eliminated by analyzer */
case class DocDef(comment: String, definition: Tree)
extends Tree {
override def symbol: Symbol = definition.symbol
override def symbol_=(sym: Symbol): unit = { definition.symbol = sym }
}
/** Instantiation template
*
* @param parents
* @param body
*/
case class Template(parents: List[Tree], body: List[Tree])
extends SymTree {
// System.err.println("TEMPLATE: " + parents)
}
/**
* @param parents ...
* @param vparamss ...
* @param argss ...
* @param body ...
* @return ...
*/
def Template(parents: List[Tree], constrMods: Modifiers, vparamss: List[List[ValDef]], argss: List[List[Tree]], body: List[Tree]): Template = {
/** Add constructor to template */
var vparamss1 =
vparamss map (.map (vd => {
val ret = ValDef(Modifiers(vd.mods.flags & IMPLICIT | PARAM) withAnnotations vd.mods.annotations,
vd.name, vd.tpt.duplicate, EmptyTree).setPos(vd.pos)
if (inIDE && vd.symbol != NoSymbol)
ret.symbol = vd.symbol
ret
}));
if (vparamss1.isEmpty ||
!vparamss1.head.isEmpty && (vparamss1.head.head.mods.flags & IMPLICIT) != 0)
vparamss1 = List() :: vparamss1;
val superRef: Tree = Select(Super(nme.EMPTY.toTypeName, nme.EMPTY.toTypeName), nme.CONSTRUCTOR)
val superCall = posAssigner.atPos(parents.head.pos) { (superRef /: argss) (Apply) }
val constr: Tree =
DefDef(constrMods, nme.CONSTRUCTOR, List(), vparamss1, TypeTree(), superCall)
Template(parents, List.flatten(vparamss) ::: constr :: body)
}
/** Block of expressions (semicolon separated expressions) */
case class Block(stats: List[Tree], expr: Tree)
extends TermTree
/** Case clause in a pattern match, eliminated by TransMatch
* (except for occurences in switch statements)
*/
case class CaseDef(pat: Tree, guard: Tree, body: Tree)
extends Tree
/** casedef shorthand */
def CaseDef(pat: Tree, body: Tree): CaseDef = CaseDef(pat, EmptyTree, body)
/** Sequence of patterns (comma separated expressions), eliminated by the
* <code>TransMatch</code> phase.
*/
case class Sequence(trees: List[Tree])
extends TermTree
/** Alternatives of patterns, eliminated by TransMatch, except for
* occurences in encoded Switch stmt (=remaining Match(CaseDef(...))
*/
case class Alternative(trees: List[Tree])
extends TermTree
/** Repetition of pattern, eliminated by TransMatch */
case class Star(elem: Tree)
extends TermTree
/** Bind of a variable to a rhs pattern, eliminated by TransMatch
*
* @param name
* @param body
*/
case class Bind(name: Name, body: Tree)
extends DefTree {
override def isTerm = name.isTermName
override def isType = name.isTypeName
}
def Bind(sym: Symbol, body: Tree): Bind =
Bind(sym.name, body) setSymbol sym
case class UnApply(fun: Tree, args: List[Tree])
extends TermTree
/** Array of expressions, needs to be translated in backend,
*/
case class ArrayValue(elemtpt: Tree, elems: List[Tree])
extends TermTree
/** Anonymous function, eliminated by analyzer */
case class Function(vparams: List[ValDef], body: Tree)
extends TermTree with SymTree
/** Assignment */
case class Assign(lhs: Tree, rhs: Tree)
extends TermTree
/** Conditional expression */
case class If(cond: Tree, thenp: Tree, elsep: Tree)
extends TermTree
/** <p>
* Pattern matching expression (before <code>TransMatch</code>)
* Switch statements (after TransMatch)
* </p>
* <p>
* After <code>TransMatch</code>, cases will satisfy the following
* constraints:
* </p>
* <ul>
* <li>all guards are EmptyTree,</li>
* <li>all patterns will be either <code>Literal(Constant(x:Int))</code>
* or <code>Alternative(lit|...|lit)</code></li>
* <li>except for an "otherwise" branch, which has pattern
* <code>Ident(nme.WILDCARD)</code></li>
* </ul>
*/
case class Match(selector: Tree, cases: List[CaseDef])
extends TermTree
/** Return expression */
case class Return(expr: Tree)
extends TermTree with SymTree
case class Try(block: Tree, catches: List[CaseDef], finalizer: Tree)
extends TermTree
/** Throw expression */
case class Throw(expr: Tree)
extends TermTree
/** Object instantiation
* One should always use factory method below to build a user level new.
*
* @param tpt a class type
*/
case class New(tpt: Tree)
extends TermTree {
assert(tpt.isType)
}
/** Factory method for object creation <code><new tpt(args_1)...(args_n)></code>.
*
* @param tpt ...
* @param argss ...
* @return ...
*/
def New(tpt: Tree, argss: List[List[Tree]]): Tree = {
assert(!argss.isEmpty)
val superRef: Tree = Select(New(tpt), nme.CONSTRUCTOR)
(superRef /: argss) (Apply)
}
/** Type annotation, eliminated by explicit outer */
case class Typed(expr: Tree, tpt: Tree)
extends TermTree
abstract class GenericApply extends TermTree {
val fun: Tree
val args: List[Tree]
}
/** Type application */
case class TypeApply(fun: Tree, args: List[Tree])
extends GenericApply {
override def symbol: Symbol = fun.symbol
override def symbol_=(sym: Symbol): unit = { fun.symbol = sym }
}
/** Value application */
case class Apply(fun: Tree, args: List[Tree])
extends GenericApply {
override def symbol: Symbol = fun.symbol
override def symbol_=(sym: Symbol): unit = { fun.symbol = sym }
}
/** Dynamic value application.
* In a dynamic application q.f(as)
* - q is stored in qual
* - as is stored in args
* - f is stored as the node's symbol field.
*/
case class ApplyDynamic(qual: Tree, args: List[Tree])
extends TermTree with SymTree
/** Super reference */
case class Super(qual: Name, mix: Name)
extends TermTree with SymTree
def Super(sym: Symbol, mix: Name): Tree = Super(sym.name, mix) setSymbol sym
/** Self reference */
case class This(qual: Name)
extends TermTree with SymTree
def This(sym: Symbol): Tree = This(sym.name) setSymbol sym
/** Designator */
case class Select(qualifier: Tree, selector: Name)
extends SymTree {
override def isTerm = selector.isTermName
override def isType = selector.isTypeName
}
def Select(qualifier: Tree, sym: Symbol): Select =
Select(qualifier, sym.name) setSymbol sym
/** Identifier */
case class Ident(name: Name)
extends SymTree {
override def isTerm = name.isTermName
override def isType = name.isTypeName
}
class BackQuotedIdent(name: Name) extends Ident(name)
def Ident(sym: Symbol): Ident =
Ident(sym.name) setSymbol sym
/** Literal */
case class Literal(value: Constant)
extends TermTree {
assert(value ne null)
}
def Literal(value: Any): Literal =
Literal(Constant(value))
/** A synthetic term holding an arbitrary type. Not to be confused with
* with TypTree, the trait for trees that are only used for type trees.
* TypeTree's are inserted in several places, but most notably in
* <code>RefCheck</code>, where the arbitrary type trees are all replaced by
* TypeTree's. */
case class TypeTree() extends TypTree {
var original: Tree = _
def setOriginal(tree: Tree): this.type = {
original = tree
setPos(tree.pos)
}
override def isEmpty = (tpe eq null) || tpe == NoType
}
def TypeTree(tp: Type): TypeTree = TypeTree() setType tp
// def TypeTree(tp: Type, tree : Tree): TypeTree = TypeTree(tree) setType tp
/** A tree that has anP attribute attached to it */
case class Annotated(annot: Annotation, arg: Tree) extends Tree {
override def isType = arg.isType
}
/** Singleton type, eliminated by RefCheck */
case class SingletonTypeTree(ref: Tree)
extends TypTree
/** Type selection, eliminated by RefCheck */
case class SelectFromTypeTree(qualifier: Tree, selector: Name)
extends TypTree with SymTree
/** Intersection type, eliminated by RefCheck */
case class CompoundTypeTree(templ: Template)
extends TypTree
/** Applied type, eliminated by RefCheck */
case class AppliedTypeTree(tpt: Tree, args: List[Tree])
extends TypTree {
override def symbol: Symbol = tpt.symbol
override def symbol_=(sym: Symbol): unit = { tpt.symbol = sym }
}
case class WildcardTypeTree(lo: Tree, hi: Tree)
extends TypTree
/* A standard pattern match
case EmptyTree =>
case PackageDef(name, stats) =>
case ClassDef(mods, name, tparams, self, impl) =>
case ModuleDef(mods, name, impl) => (eliminated by refcheck)
case ValDef(mods, name, tpt, rhs) =>
case DefDef(mods, name, tparams, vparamss, tpt, rhs) =>
case AbsTypeDef(mods, name, lo, hi) => (eliminated by erasure)
case AliasTypeDef(mods, name, tparams, rhs) => (eliminated by erasure)
case LabelDef(name, params, rhs) =>
case Import(expr, selectors) => (eliminated by typecheck)
case Annotation(constr, elements) => (eliminated by typecheck)
case DocDef(comment, definition) => (eliminated by typecheck)
case Template(parents, body) =>
case Block(stats, expr) =>
case CaseDef(pat, guard, body) => (eliminated by transmatch)
case Sequence(trees) => (eliminated by transmatch)
case Alternative(trees) => (eliminated by transmatch)
case Star(elem) => (eliminated by transmatch)
case Bind(name, body) => (eliminated by transmatch)
case UnApply(fun: Tree, args) (introduced by typer, eliminated by transmatch)
case ArrayValue(elemtpt, trees) => (introduced by uncurry)
case Function(vparams, body) => (eliminated by lambdaLift)
case Assign(lhs, rhs) =>
case If(cond, thenp, elsep) =>
case Match(selector, cases) =>
case Return(expr) =>
case Try(block, catches, finalizer) =>
case Throw(expr) =>
case New(tpt) =>
case Typed(expr, tpt) => (eliminated by erasure)
case TypeApply(fun, args) =>
case Apply(fun, args) =>
case ApplyDynamic(qual, args) (introduced by erasure, eliminated by cleanup)
case Super(qual, mix) =>
case This(qual) =>
case Select(qualifier, selector) =>
case Ident(name) =>
case Literal(value) =>
case TypeTree() => (introduced by refcheck)
case Annotated(annot, arg) => (eliminated by typer)
case SingletonTypeTree(ref) => (eliminated by uncurry)
case SelectFromTypeTree(qualifier, selector) => (eliminated by uncurry)
case CompoundTypeTree(templ: Template) => (eliminated by uncurry)
case AppliedTypeTree(tpt, args) => (eliminated by uncurry)
case WildcardTypeTree(lo, hi) => (eliminated by uncurry)
*/
abstract class TreeCopier {
def ClassDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[AbsTypeDef], self: ValDef, impl: Template): ClassDef
def PackageDef(tree: Tree, name: Name, stats: List[Tree]): PackageDef
def ModuleDef(tree: Tree, mods: Modifiers, name: Name, impl: Template): ModuleDef
def ValDef(tree: Tree, mods: Modifiers, name: Name, tpt: Tree, rhs: Tree): ValDef
def DefDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[AbsTypeDef], vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree): DefDef
def AbsTypeDef(tree: Tree, mods: Modifiers, name: Name, lo: Tree, hi: Tree): AbsTypeDef
def AliasTypeDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[AbsTypeDef], rhs: Tree): AliasTypeDef
def LabelDef(tree: Tree, name: Name, params: List[Ident], rhs: Tree): LabelDef
def Import(tree: Tree, expr: Tree, selectors: List[(Name, Name)]): Import
def Annotation(tree: Tree, constr: Tree, elements: List[Tree]): Annotation
def DocDef(tree: Tree, comment: String, definition: Tree): DocDef
def Template(tree: Tree, parents: List[Tree], body: List[Tree]): Template
def Block(tree: Tree, stats: List[Tree], expr: Tree): Block
def CaseDef(tree: Tree, pat: Tree, guard: Tree, body: Tree): CaseDef
def Sequence(tree: Tree, trees: List[Tree]): Sequence
def Alternative(tree: Tree, trees: List[Tree]): Alternative
def Star(tree: Tree, elem: Tree): Star
def Bind(tree: Tree, name: Name, body: Tree): Bind
def UnApply(tree: Tree, fun: Tree, args: List[Tree]): UnApply
def ArrayValue(tree: Tree, elemtpt: Tree, trees: List[Tree]): ArrayValue
def Function(tree: Tree, vparams: List[ValDef], body: Tree): Function
def Assign(tree: Tree, lhs: Tree, rhs: Tree): Assign
def If(tree: Tree, cond: Tree, thenp: Tree, elsep: Tree): If
def Match(tree: Tree, selector: Tree, cases: List[CaseDef]): Match
def Return(tree: Tree, expr: Tree): Return
def Try(tree: Tree, block: Tree, catches: List[CaseDef], finalizer: Tree): Try
def Throw(tree: Tree, expr: Tree): Throw
def New(tree: Tree, tpt: Tree): New
def Typed(tree: Tree, expr: Tree, tpt: Tree): Typed
def TypeApply(tree: Tree, fun: Tree, args: List[Tree]): TypeApply
def Apply(tree: Tree, fun: Tree, args: List[Tree]): Apply
def ApplyDynamic(tree: Tree, qual: Tree, args: List[Tree]): ApplyDynamic
def Super(tree: Tree, qual: Name, mix: Name): Super
def This(tree: Tree, qual: Name): This
def Select(tree: Tree, qualifier: Tree, selector: Name): Select
def Ident(tree: Tree, name: Name): Ident
def Literal(tree: Tree, value: Constant): Literal
def TypeTree(tree: Tree): TypeTree
def Annotated(tree: Tree, annot: Annotation, arg: Tree): Annotated
def SingletonTypeTree(tree: Tree, ref: Tree): SingletonTypeTree
def SelectFromTypeTree(tree: Tree, qualifier: Tree, selector: Name): SelectFromTypeTree
def CompoundTypeTree(tree: Tree, templ: Template): CompoundTypeTree
def AppliedTypeTree(tree: Tree, tpt: Tree, args: List[Tree]): AppliedTypeTree
def WildcardTypeTree(tree: Tree, lo: Tree, hi: Tree): WildcardTypeTree
}
class StrictTreeCopier extends TreeCopier {
def ClassDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[AbsTypeDef], self: ValDef, impl: Template) =
new ClassDef(mods, name, tparams, self, impl).copyAttrs(tree);
def PackageDef(tree: Tree, name: Name, stats: List[Tree]) =
new PackageDef(name, stats).copyAttrs(tree)
def ModuleDef(tree: Tree, mods: Modifiers, name: Name, impl: Template) =
new ModuleDef(mods, name, impl).copyAttrs(tree)
def ValDef(tree: Tree, mods: Modifiers, name: Name, tpt: Tree, rhs: Tree) =
new ValDef(mods, name, tpt, rhs).copyAttrs(tree)
def DefDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[AbsTypeDef], vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree) =
new DefDef(mods, name, tparams, vparamss, tpt, rhs).copyAttrs(tree)
def AbsTypeDef(tree: Tree, mods: Modifiers, name: Name, lo: Tree, hi: Tree) =
new AbsTypeDef(mods, name, lo, hi).copyAttrs(tree)
def AliasTypeDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[AbsTypeDef], rhs: Tree) =
new AliasTypeDef(mods, name, tparams, rhs).copyAttrs(tree)
def LabelDef(tree: Tree, name: Name, params: List[Ident], rhs: Tree) =
new LabelDef(name, params, rhs).copyAttrs(tree)
def Import(tree: Tree, expr: Tree, selectors: List[(Name, Name)]) =
new Import(expr, selectors).copyAttrs(tree)
def Annotation(tree: Tree, constr: Tree, elements: List[Tree]) =
new Annotation(constr, elements).copyAttrs(tree)
def DocDef(tree: Tree, comment: String, definition: Tree) =
new DocDef(comment, definition).copyAttrs(tree)
def Template(tree: Tree, parents: List[Tree], body: List[Tree]) =
new Template(parents, body).copyAttrs(tree)
def Block(tree: Tree, stats: List[Tree], expr: Tree) =
new Block(stats, expr).copyAttrs(tree)
def CaseDef(tree: Tree, pat: Tree, guard: Tree, body: Tree) =
new CaseDef(pat, guard, body).copyAttrs(tree)
def Sequence(tree: Tree, trees: List[Tree]) =
new Sequence(trees).copyAttrs(tree)
def Alternative(tree: Tree, trees: List[Tree]) =
new Alternative(trees).copyAttrs(tree)
def Star(tree: Tree, elem: Tree) =
new Star(elem).copyAttrs(tree)
def Bind(tree: Tree, name: Name, body: Tree) =
new Bind(name, body).copyAttrs(tree)
def UnApply(tree: Tree, fun: Tree, args: List[Tree]) =
new UnApply(fun, args).copyAttrs(tree)
def ArrayValue(tree: Tree, elemtpt: Tree, trees: List[Tree]) =
new ArrayValue(elemtpt, trees).copyAttrs(tree)
def Function(tree: Tree, vparams: List[ValDef], body: Tree) =
new Function(vparams, body).copyAttrs(tree)
def Assign(tree: Tree, lhs: Tree, rhs: Tree) =
new Assign(lhs, rhs).copyAttrs(tree)
def If(tree: Tree, cond: Tree, thenp: Tree, elsep: Tree) =
new If(cond, thenp, elsep).copyAttrs(tree)
def Match(tree: Tree, selector: Tree, cases: List[CaseDef]) =
new Match(selector, cases).copyAttrs(tree)
def Return(tree: Tree, expr: Tree) =
new Return(expr).copyAttrs(tree)
def Try(tree: Tree, block: Tree, catches: List[CaseDef], finalizer: Tree) =
new Try(block, catches, finalizer).copyAttrs(tree)
def Throw(tree: Tree, expr: Tree) =
new Throw(expr).copyAttrs(tree)
def New(tree: Tree, tpt: Tree) =
new New(tpt).copyAttrs(tree)
def Typed(tree: Tree, expr: Tree, tpt: Tree) =
new Typed(expr, tpt).copyAttrs(tree)
def TypeApply(tree: Tree, fun: Tree, args: List[Tree]) =
new TypeApply(fun, args).copyAttrs(tree)
def Apply(tree: Tree, fun: Tree, args: List[Tree]) =
new Apply(fun, args).copyAttrs(tree)
def ApplyDynamic(tree: Tree, qual: Tree, args: List[Tree]) =
new ApplyDynamic(qual, args).copyAttrs(tree)
def Super(tree: Tree, qual: Name, mix: Name) =
new Super(qual, mix).copyAttrs(tree)
def This(tree: Tree, qual: Name) =
new This(qual).copyAttrs(tree)
def Select(tree: Tree, qualifier: Tree, selector: Name) =
new Select(qualifier, selector).copyAttrs(tree)
def Ident(tree: Tree, name: Name) =
new Ident(name).copyAttrs(tree)
def Literal(tree: Tree, value: Constant) =
new Literal(value).copyAttrs(tree)
def TypeTree(tree: Tree) =
new TypeTree().copyAttrs(tree)
def Annotated(tree: Tree, annot: Annotation, arg: Tree) =
new Annotated(annot, arg).copyAttrs(tree)
def SingletonTypeTree(tree: Tree, ref: Tree) =
new SingletonTypeTree(ref).copyAttrs(tree)
def SelectFromTypeTree(tree: Tree, qualifier: Tree, selector: Name) =
new SelectFromTypeTree(qualifier, selector).copyAttrs(tree)
def CompoundTypeTree(tree: Tree, templ: Template) =
new CompoundTypeTree(templ).copyAttrs(tree)
def AppliedTypeTree(tree: Tree, tpt: Tree, args: List[Tree]) =
new AppliedTypeTree(tpt, args).copyAttrs(tree)
def WildcardTypeTree(tree: Tree, lo: Tree, hi: Tree) =
new WildcardTypeTree(lo, hi).copyAttrs(tree)
}
class LazyTreeCopier(copy: TreeCopier) extends TreeCopier {
def this() = this(new StrictTreeCopier)
def ClassDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[AbsTypeDef], self: ValDef, impl: Template) = tree match {
case t @ ClassDef(mods0, name0, tparams0, self0, impl0)
if (mods0 == mods && (name0 == name) && (tparams0 == tparams) && (self0 == self) && (impl0 == impl)) => t
case _ => copy.ClassDef(tree, mods, name, tparams, self, impl)
}
def PackageDef(tree: Tree, name: Name, stats: List[Tree]) = tree match {
case t @ PackageDef(name0, stats0)
if (name0 == name) && (stats0 == stats) => t
case _ => copy.PackageDef(tree, name, stats)
}
def ModuleDef(tree: Tree, mods: Modifiers, name: Name, impl: Template) = tree match {
case t @ ModuleDef(mods0, name0, impl0)
if (mods0 == mods) && (name0 == name) && (impl0 == impl) => t
case _ => copy.ModuleDef(tree, mods, name, impl)
}
def ValDef(tree: Tree, mods: Modifiers, name: Name, tpt: Tree, rhs: Tree) = tree match {
case t @ ValDef(mods0, name0, tpt0, rhs0)
if (mods0 == mods) && (name0 == name) && (tpt0 == tpt) && (rhs0 == rhs) => t
case _ => copy.ValDef(tree, mods, name, tpt, rhs)
}
def DefDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[AbsTypeDef], vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree) = tree match {
case t @ DefDef(mods0, name0, tparams0, vparamss0, tpt0, rhs0)
if (mods0 == mods) && (name0 == name) && (tparams0 == tparams) &&
(vparamss0 == vparamss) && (tpt0 == tpt) && (rhs == rhs0) => t
case _ => copy.DefDef(tree, mods, name, tparams, vparamss, tpt, rhs)
}
def AbsTypeDef(tree: Tree, mods: Modifiers, name: Name, lo: Tree, hi: Tree) = tree match {
case t @ AbsTypeDef(mods0, name0, lo0, hi0)
if (mods0 == mods) && (name0 == name) && (lo0 == lo) && (hi0 == hi) => t
case _ => copy.AbsTypeDef(tree, mods, name, lo, hi)
}
def AliasTypeDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[AbsTypeDef], rhs: Tree) = tree match {
case t @ AliasTypeDef(mods0, name0, tparams0, rhs0)
if (mods0 == mods) && (name0 == name) && (tparams0 == tparams) && (rhs0 == rhs) => t
case _ => copy.AliasTypeDef(tree, mods, name, tparams, rhs)
}
def LabelDef(tree: Tree, name: Name, params: List[Ident], rhs: Tree) = tree match {
case t @ LabelDef(name0, params0, rhs0)
if (name0 == name) && (params0 == params) && (rhs0 == rhs) => t
case _ => copy.LabelDef(tree, name, params, rhs)
}
def Import(tree: Tree, expr: Tree, selectors: List[(Name, Name)]) = tree match {
case t @ Import(expr0, selectors0)
if (expr0 == expr) && (selectors0 == selectors) => t
case _ => copy.Import(tree, expr, selectors)
}
def Annotation(tree: Tree, constr: Tree, elements: List[Tree]) = tree match {
case t @ Annotation(constr0, elements0)
if (constr0 == constr) && (elements0 == elements) => t
case _ => copy.Annotation(tree, constr, elements)
}
def DocDef(tree: Tree, comment: String, definition: Tree) = tree match {
case t @ DocDef(comment0, definition0)
if (comment0 == comment) && (definition0 == definition) => t
case _ => copy.DocDef(tree, comment, definition)
}
def Template(tree: Tree, parents: List[Tree], body: List[Tree]) = tree match {
case t @ Template(parents0, body0)
if (parents0 == parents) && (body0 == body) => t
case _ => copy.Template(tree, parents, body)
}
def Block(tree: Tree, stats: List[Tree], expr: Tree) = tree match {
case t @ Block(stats0, expr0)
if ((stats0 == stats) && (expr0 == expr)) => t
case _ => copy.Block(tree, stats, expr)
}
def CaseDef(tree: Tree, pat: Tree, guard: Tree, body: Tree) = tree match {
case t @ CaseDef(pat0, guard0, body0)
if (pat0 == pat) && (guard0 == guard) && (body0 == body) => t
case _ => copy.CaseDef(tree, pat, guard, body)
}
def Sequence(tree: Tree, trees: List[Tree]) = tree match {
case t @ Sequence(trees0)
if trees0 == trees => t
case _ => copy.Sequence(tree, trees)
}
def Alternative(tree: Tree, trees: List[Tree]) = tree match {
case t @ Alternative(trees0)
if trees0 == trees => t
case _ => copy.Alternative(tree, trees)
}
def Star(tree: Tree, elem: Tree) = tree match {
case t @ Star(elem0)
if elem0 == elem => t
case _ => copy.Star(tree, elem)
}
def Bind(tree: Tree, name: Name, body: Tree) = tree match {
case t @ Bind(name0, body0)
if (name0 == name) && (body0 == body) => t
case _ => copy.Bind(tree, name, body)
}
def UnApply(tree: Tree, fun: Tree, args: List[Tree]) = tree match {
case t @ UnApply(fun0, args0)
if (fun0 == fun) && (args0 == args) => t
case _ => copy.UnApply(tree, fun, args)
}
def ArrayValue(tree: Tree, elemtpt: Tree, trees: List[Tree]) = tree match {
case t @ ArrayValue(elemtpt0, trees0)
if (elemtpt0 == elemtpt) && (trees0 == trees) => t
case _ => copy.ArrayValue(tree, elemtpt, trees)
}
def Function(tree: Tree, vparams: List[ValDef], body: Tree) = tree match {
case t @ Function(vparams0, body0)
if (vparams0 == vparams) && (body0 == body) => t
case _ => copy.Function(tree, vparams, body)
}
def Assign(tree: Tree, lhs: Tree, rhs: Tree) = tree match {
case t @ Assign(lhs0, rhs0)
if (lhs0 == lhs) && (rhs0 == rhs) => t
case _ => copy.Assign(tree, lhs, rhs)
}
def If(tree: Tree, cond: Tree, thenp: Tree, elsep: Tree) = tree match {
case t @ If(cond0, thenp0, elsep0)
if (cond0 == cond) && (thenp0 == thenp) && (elsep0 == elsep) => t
case _ => copy.If(tree, cond, thenp, elsep)
}
def Match(tree: Tree, selector: Tree, cases: List[CaseDef]) = tree match {
case t @ Match(selector0, cases0)
if (selector0 == selector) && (cases0 == cases) => t
case _ => copy.Match(tree, selector, cases)
}
def Return(tree: Tree, expr: Tree) = tree match {
case t @ Return(expr0)
if expr0 == expr => t
case _ => copy.Return(tree, expr)
}
def Try(tree: Tree, block: Tree, catches: List[CaseDef], finalizer: Tree) = tree match {
case t @ Try(block0, catches0, finalizer0)
if (block0 == block) && (catches0 == catches) && (finalizer0 == finalizer) => t
case _ => copy.Try(tree, block, catches, finalizer)
}
def Throw(tree: Tree, expr: Tree) = tree match {
case t @ Throw(expr0)
if expr0 == expr => t
case _ => copy.Throw(tree, expr)
}
def New(tree: Tree, tpt: Tree) = tree match {
case t @ New(tpt0)
if tpt0 == tpt => t
case _ => copy.New(tree, tpt)
}
def Typed(tree: Tree, expr: Tree, tpt: Tree) = tree match {
case t @ Typed(expr0, tpt0)
if (expr0 == expr) && (tpt0 == tpt) => t
case _ => copy.Typed(tree, expr, tpt)
}
def TypeApply(tree: Tree, fun: Tree, args: List[Tree]) = tree match {
case t @ TypeApply(fun0, args0)
if (fun0 == fun) && (args0 == args) => t
case _ => copy.TypeApply(tree, fun, args)
}
def Apply(tree: Tree, fun: Tree, args: List[Tree]) = tree match {
case t @ Apply(fun0, args0)
if (fun0 == fun) && (args0 == args) => t
case _ => copy.Apply(tree, fun, args)
}
def ApplyDynamic(tree: Tree, qual: Tree, args: List[Tree]) = tree match {
case t @ ApplyDynamic(qual0, args0)
if (qual0 == qual) && (args0 == args) => t
case _ => copy.ApplyDynamic(tree, qual, args)
}
def Super(tree: Tree, qual: Name, mix: Name) = tree match {
case t @ Super(qual0, mix0)
if (qual0 == qual) && (mix0 == mix) => t
case _ => copy.Super(tree, qual, mix)
}
def This(tree: Tree, qual: Name) = tree match {
case t @ This(qual0)
if qual0 == qual => t
case _ => copy.This(tree, qual)
}
def Select(tree: Tree, qualifier: Tree, selector: Name) = tree match {
case t @ Select(qualifier0, selector0)
if (qualifier0 == qualifier) && (selector0 == selector) => t
case _ => copy.Select(tree, qualifier, selector)
}
def Ident(tree: Tree, name: Name) = tree match {
case t @ Ident(name0)
if name0 == name => t
case _ => copy.Ident(tree, name)
}
def Literal(tree: Tree, value: Constant) = tree match {
case t @ Literal(value0)
if value0 == value => t
case _ => copy.Literal(tree, value)
}
def TypeTree(tree: Tree) = tree match {
case t @ TypeTree() => t
case _ => copy.TypeTree(tree)
}
def Annotated(tree: Tree, annot: Annotation, arg: Tree) = tree match {
case t @ Annotated(annot0, arg0)
if (annot0==annot) => t
case _ => copy.Annotated(tree, annot, arg)
}
def SingletonTypeTree(tree: Tree, ref: Tree) = tree match {
case t @ SingletonTypeTree(ref0)
if ref0 == ref => t
case _ => copy.SingletonTypeTree(tree, ref)
}
def SelectFromTypeTree(tree: Tree, qualifier: Tree, selector: Name) = tree match {
case t @ SelectFromTypeTree(qualifier0, selector0)
if (qualifier0 == qualifier) && (selector0 == selector) => t
case _ => copy.SelectFromTypeTree(tree, qualifier, selector)
}
def CompoundTypeTree(tree: Tree, templ: Template) = tree match {
case t @ CompoundTypeTree(templ0)
if templ0 == templ => t
case _ => copy.CompoundTypeTree(tree, templ)
}
def AppliedTypeTree(tree: Tree, tpt: Tree, args: List[Tree]) = tree match {
case t @ AppliedTypeTree(tpt0, args0)
if (tpt0 == tpt) && (args0 == args) => t
case _ => copy.AppliedTypeTree(tree, tpt, args)
}
def WildcardTypeTree(tree: Tree, lo: Tree, hi: Tree) = tree match {
case t @ WildcardTypeTree(lo0, hi0)
if (lo0 == lo) && (hi0 == hi) => t
case _ => copy.WildcardTypeTree(tree, lo, hi)
}
}
abstract class Transformer {
val copy: TreeCopier = new LazyTreeCopier
protected var currentOwner: Symbol = definitions.RootClass
protected def currentMethod = currentOwner.enclMethod
protected def currentClass = currentOwner.enclClass
protected def currentPackage = currentOwner.toplevelClass.owner
def transform(tree: Tree): Tree = tree match {
case EmptyTree =>
tree
case PackageDef(name, stats) =>
atOwner(tree.symbol.moduleClass) {
copy.PackageDef(tree, name, transformStats(stats, currentOwner))
}
case ClassDef(mods, name, tparams, self, impl) =>
atOwner(tree.symbol) {
copy.ClassDef(tree, mods, name,
transformAbsTypeDefs(tparams),
if (self ne emptyValDef) transformValDef(self) else self,
transformTemplate(impl))
}
case ModuleDef(mods, name, impl) =>
atOwner(tree.symbol.moduleClass) {
copy.ModuleDef(tree, mods, name, transformTemplate(impl))
}
case ValDef(mods, name, tpt, rhs) =>
atOwner(tree.symbol) {
copy.ValDef(tree, mods, name, transform(tpt), transform(rhs))
}
case DefDef(mods, name, tparams, vparamss, tpt, rhs) =>
atOwner(tree.symbol) {
copy.DefDef(
tree, mods, name, transformAbsTypeDefs(tparams), transformValDefss(vparamss), transform(tpt), transform(rhs))
}
case AbsTypeDef(mods, name, lo, hi) =>
atOwner(tree.symbol) {
copy.AbsTypeDef(tree, mods, name, transform(lo), transform(hi))
}
case AliasTypeDef(mods, name, tparams, rhs) =>
atOwner(tree.symbol) {
copy.AliasTypeDef(tree, mods, name, transformAbsTypeDefs(tparams), transform(rhs))
}
case LabelDef(name, params, rhs) =>
copy.LabelDef(tree, name, transformIdents(params), transform(rhs)) //bq: Martin, once, atOwner(...) works, also change `LamdaLifter.proxy'
case Import(expr, selectors) =>
copy.Import(tree, transform(expr), selectors)
case Annotation(constr, elements) =>
copy.Annotation(tree, transform(constr), transformTrees(elements))
case DocDef(comment, definition) =>
copy.DocDef(tree, comment, transform(definition))
case Template(parents, body) =>
copy.Template(tree, transformTrees(parents), transformStats(body, tree.symbol))
case Block(stats, expr) =>
copy.Block(tree, transformStats(stats, currentOwner), transform(expr))
case CaseDef(pat, guard, body) =>
copy.CaseDef(tree, transform(pat), transform(guard), transform(body))
case Sequence(trees) =>
copy.Sequence(tree, transformTrees(trees))
case Alternative(trees) =>
copy.Alternative(tree, transformTrees(trees))
case Star(elem) =>
copy.Star(tree, transform(elem))
case Bind(name, body) =>
copy.Bind(tree, name, transform(body))
case UnApply(fun, args) =>
copy.UnApply(tree, fun, transformTrees(args)) // bq: see test/.../unapplyContexts2.scala
case ArrayValue(elemtpt, trees) =>
copy.ArrayValue(tree, transform(elemtpt), transformTrees(trees))
case Function(vparams, body) =>
atOwner(tree.symbol) {
copy.Function(tree, transformValDefs(vparams), transform(body))
}
case Assign(lhs, rhs) =>
copy.Assign(tree, transform(lhs), transform(rhs))
case If(cond, thenp, elsep) =>
copy.If(tree, transform(cond), transform(thenp), transform(elsep))
case Match(selector, cases) =>
copy.Match(tree, transform(selector), transformCaseDefs(cases))
case Return(expr) =>
copy.Return(tree, transform(expr))
case Try(block, catches, finalizer) =>
copy.Try(tree, transform(block), transformCaseDefs(catches), transform(finalizer))
case Throw(expr) =>
copy.Throw(tree, transform(expr))
case New(tpt) =>
copy.New(tree, transform(tpt))
case Typed(expr, tpt) =>
copy.Typed(tree, transform(expr), transform(tpt))
case TypeApply(fun, args) =>
copy.TypeApply(tree, transform(fun), transformTrees(args))
case Apply(fun, args) =>
copy.Apply(tree, transform(fun), transformTrees(args))
case ApplyDynamic(qual, args) =>
copy.ApplyDynamic(tree, transform(qual), transformTrees(args))
case Super(qual, mix) =>
copy.Super(tree, qual, mix)
case This(qual) =>
copy.This(tree, qual)
case Select(qualifier, selector) =>
copy.Select(tree, transform(qualifier), selector)
case Ident(name) =>
copy.Ident(tree, name)
case Literal(value) =>
copy.Literal(tree, value)
case TypeTree() =>
copy.TypeTree(tree)
case Annotated(annot, arg) =>
copy.Annotated(tree, transform(annot).asInstanceOf[Annotation], transform(arg))
case SingletonTypeTree(ref) =>
copy.SingletonTypeTree(tree, transform(ref))
case SelectFromTypeTree(qualifier, selector) =>
copy.SelectFromTypeTree(tree, transform(qualifier), selector)
case CompoundTypeTree(templ) =>
copy.CompoundTypeTree(tree, transformTemplate(templ))
case AppliedTypeTree(tpt, args) =>
copy.AppliedTypeTree(tree, transform(tpt), transformTrees(args))
case WildcardTypeTree(lo, hi) =>
copy.WildcardTypeTree(tree, transform(lo), transform(hi))
}
def transformTrees(trees: List[Tree]): List[Tree] =
List.mapConserve(trees)(transform)
def transformTemplate(tree: Template): Template =
transform(tree: Tree).asInstanceOf[Template]
def transformAbsTypeDefs(trees: List[AbsTypeDef]): List[AbsTypeDef] =
List.mapConserve(trees)(tree => transform(tree).asInstanceOf[AbsTypeDef])
def transformValDef(tree: ValDef): ValDef =
transform(tree).asInstanceOf[ValDef]
def transformValDefs(trees: List[ValDef]): List[ValDef] =
List.mapConserve(trees)(transformValDef)
def transformValDefss(treess: List[List[ValDef]]): List[List[ValDef]] =
List.mapConserve(treess)(transformValDefs)
def transformCaseDefs(trees: List[CaseDef]): List[CaseDef] =
List.mapConserve(trees)(tree => transform(tree).asInstanceOf[CaseDef])
def transformIdents(trees: List[Ident]): List[Ident] =
List.mapConserve(trees)(tree => transform(tree).asInstanceOf[Ident])
def transformStats(stats: List[Tree], exprOwner: Symbol): List[Tree] =
List.mapConserve(stats)(stat =>
if (exprOwner != currentOwner && stat.isTerm) atOwner(exprOwner)(transform(stat))
else transform(stat)) filter (EmptyTree !=)
def transformUnit(unit: CompilationUnit): unit = { unit.body = transform(unit.body) }
def atOwner[A](owner: Symbol)(trans: => A): A = {
val prevOwner = currentOwner
currentOwner = owner
val result = trans
currentOwner = prevOwner
result
}
}
class Traverser {
protected var currentOwner: Symbol = definitions.RootClass
def traverse(tree: Tree): unit = tree match {
case EmptyTree =>
;
case PackageDef(name, stats) =>
atOwner(tree.symbol.moduleClass) {
traverseTrees(stats)
}
case ClassDef(mods, name, tparams, self, impl) =>
atOwner(tree.symbol) {
traverseTrees(tparams);
if (self ne emptyValDef) traverse(self);
traverse(impl)
}
case ModuleDef(mods, name, impl) =>
atOwner(tree.symbol.moduleClass) {
traverse(impl)
}
case ValDef(mods, name, tpt, rhs) =>
atOwner(tree.symbol) {
traverse(tpt); traverse(rhs)
}
case DefDef(mods, name, tparams, vparamss, tpt, rhs) =>
atOwner(tree.symbol) {
traverseTrees(tparams); traverseTreess(vparamss); traverse(tpt); traverse(rhs)
}
case AbsTypeDef(mods, name, lo, hi) =>
atOwner(tree.symbol) {
traverse(lo); traverse(hi)
}
case AliasTypeDef(mods, name, tparams, rhs) =>
atOwner(tree.symbol) {
traverseTrees(tparams); traverse(rhs)
}
case LabelDef(name, params, rhs) =>
traverseTrees(params); traverse(rhs)
case Import(expr, selectors) =>
traverse(expr)
case Annotation(constr, elements) =>
traverse(constr); traverseTrees(elements)
case Annotated(annot, arg) =>
traverse(annot); traverse(arg)
case DocDef(comment, definition) =>
traverse(definition)
case Template(parents, body) =>
traverseTrees(parents); traverseStats(body, tree.symbol)
case Block(stats, expr) =>
traverseTrees(stats); traverse(expr)
case CaseDef(pat, guard, body) =>
traverse(pat); traverse(guard); traverse(body)
case Sequence(trees) =>
traverseTrees(trees)
case Alternative(trees) =>
traverseTrees(trees)
case Star(elem) =>
traverse(elem)
case Bind(name, body) =>
traverse(body)
case UnApply(fun, args) =>
traverse(fun); traverseTrees(args)
case ArrayValue(elemtpt, trees) =>
traverse(elemtpt); traverseTrees(trees)
case Function(vparams, body) =>
atOwner(tree.symbol) {
traverseTrees(vparams); traverse(body)
}
case Assign(lhs, rhs) =>
traverse(lhs); traverse(rhs)
case If(cond, thenp, elsep) =>
traverse(cond); traverse(thenp); traverse(elsep)
case Match(selector, cases) =>
traverse(selector); traverseTrees(cases)
case Return(expr) =>
traverse(expr)
case Try(block, catches, finalizer) =>
traverse(block); traverseTrees(catches); traverse(finalizer)
case Throw(expr) =>
traverse(expr)
case New(tpt) =>
traverse(tpt)
case Typed(expr, tpt) =>
traverse(expr); traverse(tpt)
case TypeApply(fun, args) =>
traverse(fun); traverseTrees(args)
case Apply(fun, args) =>
traverse(fun); traverseTrees(args)
case ApplyDynamic(qual, args) =>
traverse(qual); traverseTrees(args)
case Super(_, _) =>
;
case This(_) =>
;
case Select(qualifier, selector) =>
traverse(qualifier)
case Ident(_) =>
;
case Literal(_) =>
;
case TypeTree() =>
;
case SingletonTypeTree(ref) =>
traverse(ref)
case SelectFromTypeTree(qualifier, selector) =>
traverse(qualifier)
case CompoundTypeTree(templ) =>
traverse(templ)
case AppliedTypeTree(tpt, args) =>
traverse(tpt); traverseTrees(args)
case WildcardTypeTree(lo, hi) =>
traverse(lo); traverse(hi)
}
def traverseTrees(trees: List[Tree]): unit =
trees foreach traverse
def traverseTreess(treess: List[List[Tree]]): unit =
treess foreach traverseTrees
def traverseStats(stats: List[Tree], exprOwner: Symbol): unit =
stats foreach (stat =>
if (exprOwner != currentOwner && stat.isTerm) atOwner(exprOwner)(traverse(stat))
else traverse(stat))
def apply[T <: Tree](tree: T): T = { traverse(tree); tree }
def atOwner(owner: Symbol)(traverse: => unit): unit = {
val prevOwner = currentOwner
currentOwner = owner
traverse
currentOwner = prevOwner
}
}
class TreeSubstituter(from: List[Symbol], to: List[Tree]) extends Transformer {
override def transform(tree: Tree): Tree = tree match {
case Ident(_) =>
def subst(from: List[Symbol], to: List[Tree]): Tree =
if (from.isEmpty) tree
else if (tree.symbol == from.head) to.head
else subst(from.tail, to.tail);
subst(from, to)
case _ =>
super.transform(tree)
}
}
class TreeTypeSubstituter(from: List[Symbol], to: List[Type]) extends Traverser {
val typeSubst = new SubstTypeMap(from, to)
override def traverse(tree: Tree): unit = {
if (tree.tpe ne null) tree.tpe = typeSubst(tree.tpe)
super.traverse(tree)
}
override def apply[T <: Tree](tree: T): T = super.apply(tree.duplicate)
}
class TreeSymSubstituter(from: List[Symbol], to: List[Symbol]) extends Traverser {
val symSubst = new SubstSymMap(from, to)
override def traverse(tree: Tree): unit = {
def subst(from: List[Symbol], to: List[Symbol]): unit = {
if (!from.isEmpty)
if (tree.symbol == from.head) tree setSymbol to.head
else subst(from.tail, to.tail)
}
if (tree.tpe ne null) tree.tpe = symSubst(tree.tpe)
if (tree.hasSymbol) subst(from, to)
super.traverse(tree)
}
override def apply[T <: Tree](tree: T): T = super.apply(tree.duplicate)
}
class ChangeOwnerTraverser(val oldowner: Symbol, val newowner: Symbol) extends Traverser {
override def traverse(tree: Tree): unit = {
if ((tree.isDef || tree.isInstanceOf[Function]) &&
tree.symbol != NoSymbol && tree.symbol.owner == oldowner)
tree.symbol.owner = newowner;
super.traverse(tree)
}
}
final class TreeList {
private var trees = List[Tree]()
def append(t: Tree): TreeList = { trees = t :: trees; this }
def append(ts: List[Tree]): TreeList = { trees = ts reverse_::: trees; this }
def toList: List[Tree] = trees.reverse
}
object posAssigner extends Traverser {
private var pos: PositionType = _
override def traverse(t: Tree): unit =
if (t != EmptyTree && t.pos == NoPos) {
t.setPos(pos)
super.traverse(t)
}
def atPos[T <: Tree](pos: PositionType)(tree: T): T = {
this.pos = pos
traverse(tree)
tree
}
}
object resetPos extends Traverser {
override def traverse(t: Tree): unit = {
if (t != EmptyTree) t.setPos(NoPos)
super.traverse(t)
}
}
/** A traverser which resets symbol and tpe fields of all nodes in a given tree
* except for (1) TypeTree nodes, whose <code>.tpe</code> field is kept and
* (2) is a <code>.symbol</code> field refers to a symbol which is defined
* outside the tree, it is also kept.
*/
object resetAttrs extends Traverser {
private val erasedSyms = new HashSet[Symbol](8)
override def traverse(tree: Tree): unit = tree match {
case EmptyTree | TypeTree() =>
;
case Template(parents, body) =>
tree.symbol = NoSymbol
tree.tpe = null
for (val stat <- body)
if (stat.isDef) erasedSyms.addEntry(stat.symbol)
super.traverse(tree)
case _: DefTree =>
erasedSyms.addEntry(tree.symbol)
tree.symbol = NoSymbol
tree.tpe = null
super.traverse(tree)
case _ =>
if (tree.hasSymbol && erasedSyms.contains(tree.symbol)) tree.symbol = NoSymbol
tree.tpe = null
super.traverse(tree)
}
}
}
|