/
Trees.scala
2991 lines (2562 loc) · 99.5 KB
/
Trees.scala
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
/* NSC -- new Scala compiler
* Copyright 2005-2013 LAMP/EPFL
* @author Martin Odersky
*/
package scala.reflect
package api
/**
* <span class="badge badge-red" style="float: right;">EXPERIMENTAL</span>
*
* This trait defines the node types used in Scala abstract syntax trees (AST) and operations on them.
*
* Trees are the basis for Scala's abstract syntax that is used to represent programs. They are also called
* abstract syntax trees and commonly abbreviated as ASTs.
*
* In Scala reflection, APIs that produce or use `Tree`s are:
*
* - '''Annotations''' which use trees to represent their arguments, exposed in [[scala.reflect.api.Annotations#scalaArgs Annotation.scalaArgs]].
* - '''[[scala.reflect.api.Universe#reify reify]]''', a special method on [[scala.reflect.api.Universe]] that takes an expression and returns an AST which represents the expression.
* - '''Macros and runtime compilation with toolboxes''' which both use trees as their program representation medium.
*
* Trees are immutable, except for three fields
* [[Trees#TreeApi.pos pos]], [[Trees#TreeApi.symbol symbol]], and [[Trees#TreeApi.tpe tpe]], which are assigned when a tree is typechecked
* to attribute it with the information gathered by the typechecker.
*
* === Examples ===
*
* The following creates an AST representing a literal 5 in Scala source code:
* {{{
* Literal(Constant(5))
* }}}
*
* The following creates an AST representing `print("Hello World")`:
* {{{
* Apply(Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("print")), List(Literal(Constant("Hello World"))))
* }}}
*
* The following creates an AST from a literal 5, and then uses `showRaw` to print it in a readable format.
* {{{
* import scala.reflect.runtime.universe.{ reify, showRaw }
* print( showRaw( reify{5}.tree ) )` // prints Literal(Constant(5))
* }}}
*
* For more information about `Tree`s, see the [[http://docs.scala-lang.org/overviews/reflection/symbols-trees-types.html Reflection Guide: Symbols, Trees, Types]].
*
* @groupname Traversal Tree Traversal and Transformation
* @groupprio Traversal 1
* @groupprio Factories 1
* @groupname Copying Tree Copying
* @groupprio Copying 1
*
* @contentDiagram hideNodes "*Api"
* @group ReflectionAPI
*/
trait Trees { self: Universe =>
/** The type of Scala abstract syntax trees.
* @group Trees
* @template
*/
type Tree >: Null <: TreeApi
/** A tag that preserves the identity of the `Tree` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val TreeTag: ClassTag[Tree]
/** The API that all trees support.
* The main source of information about trees is the [[scala.reflect.api.Trees]] page.
* @group API
*/
trait TreeApi extends Product { this: Tree =>
/** Does this tree represent a definition? (of a method, of a class, etc) */
def isDef: Boolean
/** Is this tree one of the empty trees?
* Empty trees are: the `EmptyTree` null object, `TypeTree` instances that don't carry a type
* and the special `emptyValDef` singleton.
*/
def isEmpty: Boolean
/** The canonical way to test if a Tree represents a term.
*/
def isTerm: Boolean
/** The canonical way to test if a Tree represents a type.
*/
def isType: Boolean
/** Position of the tree. */
def pos: Position
/** Type of the tree.
*
* Upon creation most trees have their `tpe` set to `null`.
* Types are typically assigned to trees during typechecking.
* Some node factory methods set `tpe` immediately after creation.
*
* When the typechecker encounters a tree with a non-null tpe,
* it will assume it to be correct and not check it again. This means one has
* to be careful not to erase the `tpe` field of subtrees.
*/
def tpe: Type
/** Symbol of the tree.
*
* For most trees symbol is `null`. In `SymTree`s,
* it is overridden and implemented with a var, initialized to `NoSymbol`.
*
* Trees which are not `SymTree`s but which carry symbols do so by
* overriding `def symbol` to forward it elsewhere. Examples:
*
* - `Super(qual, _)` has `qual`'s symbol,
* - `Apply(fun, args)` has `fun`'s symbol,
* - `TypeApply(fun, args)` has `fun`'s symbol,
* - `AppliedTypeTree(tpt, args)` has `tpt`'s symbol,
* - `TypeTree(tpe)` has `tpe`'s `typeSymbol`, if `tpe != null`.
*/
def symbol: Symbol
/** Provides an alternate if tree is empty
* @param alt The alternate tree
* @return If this tree is non empty, this tree, otherwise `alt`.
*/
def orElse(alt: => Tree): Tree
/** Apply `f` to each subtree */
def foreach(f: Tree => Unit): Unit
/** Find all subtrees matching predicate `p`. Same as `filter` */
def withFilter(f: Tree => Boolean): List[Tree]
/** Find all subtrees matching predicate `p`. Same as `withFilter` */
def filter(f: Tree => Boolean): List[Tree]
/** Apply `pf' to each subtree on which the function is defined and collect the results.
*/
def collect[T](pf: PartialFunction[Tree, T]): List[T]
/** Returns optionally first tree (in a preorder traversal) which satisfies predicate `p`,
* or None if none exists.
*/
def find(p: Tree => Boolean): Option[Tree]
/** Is there exists a part of this tree which satisfies predicate `p`? */
def exists(p: Tree => Boolean): Boolean
/** Do all parts of this tree satisfy predicate `p`? */
def forAll(p: Tree => Boolean): Boolean
/** Tests whether two trees are structurall equal.
* Note that `==` on trees is reference equality.
*/
def equalsStructure(that : Tree): Boolean
/** The direct child trees of this tree.
* EmptyTrees are always omitted. Lists are flattened.
*/
def children: List[Tree]
/** Extracts free term symbols from a tree that is reified or contains reified subtrees.
*/
def freeTerms: List[FreeTermSymbol]
/** Extracts free type symbols from a tree that is reified or contains reified subtrees.
*/
def freeTypes: List[FreeTypeSymbol]
/** Substitute symbols in `to` for corresponding occurrences of references to
* symbols `from` in this type.
*/
def substituteSymbols(from: List[Symbol], to: List[Symbol]): Tree
/** Substitute types in `to` for corresponding occurrences of references to
* symbols `from` in this tree.
*/
def substituteTypes(from: List[Symbol], to: List[Type]): Tree
/** Substitute given tree `to` for occurrences of nodes that represent
* `C.this`, where `C` referes to the given class `clazz`.
*/
def substituteThis(clazz: Symbol, to: Tree): Tree
/** Make a copy of this tree, keeping all attributes,
* except that all positions are focused (so nothing
* in this tree will be found when searching by position).
*/
def duplicate: this.type
/** Obtains string representation of a tree */
override def toString: String = treeToString(this)
}
/** Obtains string representation of a tree
* @group Trees
*/
protected def treeToString(tree: Tree): String
/** The empty tree
* @group Trees
*/
val EmptyTree: Tree
/** A tree for a term. Not all trees representing terms are TermTrees; use isTerm
* to reliably identify terms.
* @group Trees
* @template
*/
type TermTree >: Null <: AnyRef with Tree with TermTreeApi
/** A tag that preserves the identity of the `TermTree` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val TermTreeTag: ClassTag[TermTree]
/** The API that all term trees support
* @group API
*/
trait TermTreeApi extends TreeApi { this: TermTree =>
}
/** A tree for a type. Not all trees representing types are TypTrees; use isType
* to reliably identify types.
* @group Trees
* @template
*/
type TypTree >: Null <: AnyRef with Tree with TypTreeApi
/** A tag that preserves the identity of the `TypTree` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val TypTreeTag: ClassTag[TypTree]
/** The API that all typ trees support
* @group API
*/
trait TypTreeApi extends TreeApi { this: TypTree =>
}
/** A tree with a mutable symbol field, initialized to NoSymbol.
* @group Trees
* @template
*/
type SymTree >: Null <: AnyRef with Tree with SymTreeApi
/** A tag that preserves the identity of the `SymTree` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val SymTreeTag: ClassTag[SymTree]
/** The API that all sym trees support
* @group API
*/
trait SymTreeApi extends TreeApi { this: SymTree =>
/** @inheritdoc */
def symbol: Symbol
}
/** A tree with a name - effectively, a DefTree or RefTree.
* @group Trees
* @template
*/
type NameTree >: Null <: AnyRef with Tree with NameTreeApi
/** A tag that preserves the identity of the `NameTree` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val NameTreeTag: ClassTag[NameTree]
/** The API that all name trees support
* @group API
*/
trait NameTreeApi extends TreeApi { this: NameTree =>
/** The underlying name.
* For example, the `<List>` part of `Ident("List": TermName)`.
*/
def name: Name
}
/** A tree which references a symbol-carrying entity.
* References one, as opposed to defining one; definitions
* are in DefTrees.
* @group Trees
* @template
*/
type RefTree >: Null <: SymTree with NameTree with RefTreeApi
/** A tag that preserves the identity of the `RefTree` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val RefTreeTag: ClassTag[RefTree]
/** The API that all ref trees support
* @group API
*/
trait RefTreeApi extends SymTreeApi with NameTreeApi { this: RefTree =>
/** The qualifier of the reference.
* For example, the `<scala>` part of `Select("scala": TermName, "List": TermName)`.
* `EmptyTree` for `Ident` instances.
*/
def qualifier: Tree
/** @inheritdoc */
def name: Name
}
/** A tree which defines a symbol-carrying entity.
* @group Trees
* @template
*/
type DefTree >: Null <: SymTree with NameTree with DefTreeApi
/** A tag that preserves the identity of the `DefTree` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val DefTreeTag: ClassTag[DefTree]
/** The API that all def trees support
* @group API
*/
trait DefTreeApi extends SymTreeApi with NameTreeApi { this: DefTree =>
/** @inheritdoc */
def name: Name
}
/** Common base class for all member definitions: types, classes,
* objects, packages, vals and vars, defs.
* @group Trees
* @template
*/
type MemberDef >: Null <: DefTree with MemberDefApi
/** A tag that preserves the identity of the `MemberDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val MemberDefTag: ClassTag[MemberDef]
/** The API that all member defs support
* @group API
*/
trait MemberDefApi extends DefTreeApi { this: MemberDef =>
/** Modifiers of the declared member. */
def mods: Modifiers
}
/** A packaging, such as `package pid { stats }`
* @group Trees
* @template
*/
type PackageDef >: Null <: MemberDef with PackageDefApi
/** A tag that preserves the identity of the `PackageDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val PackageDefTag: ClassTag[PackageDef]
/** The constructor/extractor for `PackageDef` instances.
* @group Extractors
*/
val PackageDef: PackageDefExtractor
/** An extractor class to create and pattern match with syntax `PackageDef(pid, stats)`.
* This AST node corresponds to the following Scala code:
*
* `package` pid { stats }
* @group Extractors
*/
abstract class PackageDefExtractor {
def apply(pid: RefTree, stats: List[Tree]): PackageDef
def unapply(packageDef: PackageDef): Option[(RefTree, List[Tree])]
}
/** The API that all package defs support
* @group API
*/
trait PackageDefApi extends MemberDefApi { this: PackageDef =>
/** The (possibly, fully-qualified) name of the package. */
def pid: RefTree
/** Body of the package definition. */
def stats: List[Tree]
}
/** A common base class for class and object definitions.
* @group Trees
* @template
*/
type ImplDef >: Null <: MemberDef with ImplDefApi
/** A tag that preserves the identity of the `ImplDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val ImplDefTag: ClassTag[ImplDef]
/** The API that all impl defs support
* @group API
*/
trait ImplDefApi extends MemberDefApi { this: ImplDef =>
/** The body of the definition. */
def impl: Template
}
/** A class definition.
* @group Trees
* @template
*/
type ClassDef >: Null <: ImplDef with ClassDefApi
/** A tag that preserves the identity of the `ClassDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val ClassDefTag: ClassTag[ClassDef]
/** The constructor/extractor for `ClassDef` instances.
* @group Extractors
*/
val ClassDef: ClassDefExtractor
/** An extractor class to create and pattern match with syntax `ClassDef(mods, name, tparams, impl)`.
* This AST node corresponds to the following Scala code:
*
* mods `class` name [tparams] impl
*
* Where impl stands for:
*
* `extends` parents { defs }
* @group Extractors
*/
abstract class ClassDefExtractor {
def apply(mods: Modifiers, name: TypeName, tparams: List[TypeDef], impl: Template): ClassDef
def unapply(classDef: ClassDef): Option[(Modifiers, TypeName, List[TypeDef], Template)]
}
/** The API that all class defs support
* @group API
*/
trait ClassDefApi extends ImplDefApi { this: ClassDef =>
/** @inheritdoc */
def mods: Modifiers
/** The name of the class. */
def name: TypeName
/** The type parameters of the class. */
def tparams: List[TypeDef]
/** @inheritdoc */
def impl: Template
}
/** An object definition, e.g. `object Foo`. Internally, objects are
* quite frequently called modules to reduce ambiguity.
* Eliminated by compiler phase refcheck.
* @group Trees
* @template
*/
type ModuleDef >: Null <: ImplDef with ModuleDefApi
/** A tag that preserves the identity of the `ModuleDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val ModuleDefTag: ClassTag[ModuleDef]
/** The constructor/extractor for `ModuleDef` instances.
* @group Extractors
*/
val ModuleDef: ModuleDefExtractor
/** An extractor class to create and pattern match with syntax `ModuleDef(mods, name, impl)`.
* This AST node corresponds to the following Scala code:
*
* mods `object` name impl
*
* Where impl stands for:
*
* `extends` parents { defs }
* @group Extractors
*/
abstract class ModuleDefExtractor {
def apply(mods: Modifiers, name: TermName, impl: Template): ModuleDef
def unapply(moduleDef: ModuleDef): Option[(Modifiers, TermName, Template)]
}
/** The API that all module defs support
* @group API
*/
trait ModuleDefApi extends ImplDefApi { this: ModuleDef =>
/** @inheritdoc */
def mods: Modifiers
/** The name of the module. */
def name: TermName
/** @inheritdoc */
def impl: Template
}
/** A common base class for ValDefs and DefDefs.
* @group Trees
* @template
*/
type ValOrDefDef >: Null <: MemberDef with ValOrDefDefApi
/** A tag that preserves the identity of the `ValOrDefDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val ValOrDefDefTag: ClassTag[ValOrDefDef]
/** The API that all val defs and def defs support
* @group API
*/
trait ValOrDefDefApi extends MemberDefApi { this: ValOrDefDef =>
/** @inheritdoc */
def name: Name // can't be a TermName because macros can be type names.
/** The type ascribed to the definition.
* An empty `TypeTree` if the type hasn't been specified explicitly
* and is supposed to be inferred.
*/
def tpt: Tree
/** The body of the definition.
* The `EmptyTree` is the body is empty (e.g. for abstract members).
*/
def rhs: Tree
}
/** Broadly speaking, a value definition. All these are encoded as ValDefs:
*
* - immutable values, e.g. "val x"
* - mutable values, e.g. "var x" - the MUTABLE flag set in mods
* - lazy values, e.g. "lazy val x" - the LAZY flag set in mods
* - method parameters, see vparamss in [[scala.reflect.api.Trees#DefDef]] - the PARAM flag is set in mods
* - explicit self-types, e.g. class A { self: Bar => }
* @group Trees
* @template
*/
type ValDef >: Null <: ValOrDefDef with ValDefApi
/** A tag that preserves the identity of the `ValDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val ValDefTag: ClassTag[ValDef]
/** The constructor/extractor for `ValDef` instances.
* @group Extractors
*/
val ValDef: ValDefExtractor
/** An extractor class to create and pattern match with syntax `ValDef(mods, name, tpt, rhs)`.
* This AST node corresponds to any of the following Scala code:
*
* mods `val` name: tpt = rhs
*
* mods `var` name: tpt = rhs
*
* mods name: tpt = rhs // in signatures of function and method definitions
*
* self: Bar => // self-types
*
* If the type of a value is not specified explicitly (i.e. is meant to be inferred),
* this is expressed by having `tpt` set to `TypeTree()` (but not to an `EmptyTree`!).
* @group Extractors
*/
abstract class ValDefExtractor {
def apply(mods: Modifiers, name: TermName, tpt: Tree, rhs: Tree): ValDef
def unapply(valDef: ValDef): Option[(Modifiers, TermName, Tree, Tree)]
}
/** The API that all val defs support
* @group API
*/
trait ValDefApi extends ValOrDefDefApi { this: ValDef =>
/** @inheritdoc */
def mods: Modifiers
/** @inheritdoc */
def name: TermName
/** @inheritdoc */
def tpt: Tree
/** @inheritdoc */
def rhs: Tree
}
/** A method or macro definition.
* @param name The name of the method or macro. Can be a type name in case this is a type macro
* @group Trees
* @template
*/
type DefDef >: Null <: ValOrDefDef with DefDefApi
/** A tag that preserves the identity of the `DefDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val DefDefTag: ClassTag[DefDef]
/** The constructor/extractor for `DefDef` instances.
* @group Extractors
*/
val DefDef: DefDefExtractor
/** An extractor class to create and pattern match with syntax `DefDef(mods, name, tparams, vparamss, tpt, rhs)`.
* This AST node corresponds to the following Scala code:
*
* mods `def` name[tparams](vparams_1)...(vparams_n): tpt = rhs
*
* If the return type is not specified explicitly (i.e. is meant to be inferred),
* this is expressed by having `tpt` set to `TypeTree()` (but not to an `EmptyTree`!).
* @group Extractors
*/
abstract class DefDefExtractor {
def apply(mods: Modifiers, name: Name, tparams: List[TypeDef], vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree): DefDef
def unapply(defDef: DefDef): Option[(Modifiers, Name, List[TypeDef], List[List[ValDef]], Tree, Tree)]
}
/** The API that all def defs support
* @group API
*/
trait DefDefApi extends ValOrDefDefApi { this: DefDef =>
/** @inheritdoc */
def mods: Modifiers
/** @inheritdoc */
def name: Name
/** The type parameters of the method. */
def tparams: List[TypeDef]
/** The parameter lists of the method. */
def vparamss: List[List[ValDef]]
/** @inheritdoc */
def tpt: Tree
/** @inheritdoc */
def rhs: Tree
}
/** An abstract type, a type parameter, or a type alias.
* Eliminated by erasure.
* @group Trees
* @template
*/
type TypeDef >: Null <: MemberDef with TypeDefApi
/** A tag that preserves the identity of the `TypeDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val TypeDefTag: ClassTag[TypeDef]
/** The constructor/extractor for `TypeDef` instances.
* @group Extractors
*/
val TypeDef: TypeDefExtractor
/** An extractor class to create and pattern match with syntax `TypeDef(mods, name, tparams, rhs)`.
* This AST node corresponds to the following Scala code:
*
* mods `type` name[tparams] = rhs
*
* mods `type` name[tparams] >: lo <: hi
*
* First usage illustrates `TypeDefs` representing type aliases and type parameters.
* Second usage illustrates `TypeDefs` representing abstract types,
* where lo and hi are both `TypeBoundsTrees` and `Modifier.deferred` is set in mods.
* @group Extractors
*/
abstract class TypeDefExtractor {
def apply(mods: Modifiers, name: TypeName, tparams: List[TypeDef], rhs: Tree): TypeDef
def unapply(typeDef: TypeDef): Option[(Modifiers, TypeName, List[TypeDef], Tree)]
}
/** The API that all type defs support
* @group API
*/
trait TypeDefApi extends MemberDefApi { this: TypeDef =>
/** @inheritdoc */
def mods: Modifiers
/** @inheritdoc */
def name: TypeName
/** The type parameters of this type definition. */
def tparams: List[TypeDef]
/** The body of the definition.
* The `EmptyTree` is the body is empty (e.g. for abstract type members).
*/
def rhs: Tree
}
/** A labelled expression. Not expressible in language syntax, but
* generated by the compiler to simulate while/do-while loops, and
* also by the pattern matcher.
*
* The label acts much like a nested function, where `params` represents
* the incoming parameters. The symbol given to the LabelDef should have
* a MethodType, as if it were a nested function.
*
* Jumps are apply nodes attributed with a label's symbol. The
* arguments from the apply node will be passed to the label and
* assigned to the Idents.
*
* Forward jumps within a block are allowed.
* @group Trees
* @template
*/
type LabelDef >: Null <: DefTree with TermTree with LabelDefApi
/** A tag that preserves the identity of the `LabelDef` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val LabelDefTag: ClassTag[LabelDef]
/** The constructor/extractor for `LabelDef` instances.
* @group Extractors
*/
val LabelDef: LabelDefExtractor
/** An extractor class to create and pattern match with syntax `LabelDef(name, params, rhs)`.
*
* This AST node does not have direct correspondence to Scala code.
* It is used for tailcalls and like.
* For example, while/do are desugared to label defs as follows:
* {{{
* while (cond) body ==> LabelDef(\$L, List(), if (cond) { body; L\$() } else ())
* }}}
* {{{
* do body while (cond) ==> LabelDef(\$L, List(), body; if (cond) L\$() else ())
* }}}
* @group Extractors
*/
abstract class LabelDefExtractor {
def apply(name: TermName, params: List[Ident], rhs: Tree): LabelDef
def unapply(labelDef: LabelDef): Option[(TermName, List[Ident], Tree)]
}
/** The API that all label defs support
* @group API
*/
trait LabelDefApi extends DefTreeApi with TermTreeApi { this: LabelDef =>
/** @inheritdoc */
def name: TermName
/** Label's parameters - names that can be used in the body of the label.
* See the example for [[scala.reflect.api.Trees#LabelDefExtractor]].
*/
def params: List[Ident]
/** The body of the label.
* See the example for [[scala.reflect.api.Trees#LabelDefExtractor]].
*/
def rhs: Tree
}
/** Import selector
*
* Representation of an imported name its optional rename and their optional positions
*
* Eliminated by typecheck.
*
* @param name the imported name
* @param namePos its position or -1 if undefined
* @param rename the name the import is renamed to (== name if no renaming)
* @param renamePos the position of the rename or -1 if undefined
* @group Trees
* @template
*/
type ImportSelector >: Null <: AnyRef with ImportSelectorApi
/** A tag that preserves the identity of the `ImportSelector` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val ImportSelectorTag: ClassTag[ImportSelector]
/** The constructor/extractor for `ImportSelector` instances.
* @group Extractors
*/
val ImportSelector: ImportSelectorExtractor
/** An extractor class to create and pattern match with syntax `ImportSelector(name:, namePos, rename, renamePos)`.
* This is not an AST node, it is used as a part of the `Import` node.
* @group Extractors
*/
abstract class ImportSelectorExtractor {
def apply(name: Name, namePos: Int, rename: Name, renamePos: Int): ImportSelector
def unapply(importSelector: ImportSelector): Option[(Name, Int, Name, Int)]
}
/** The API that all import selectors support
* @group API
*/
trait ImportSelectorApi { this: ImportSelector =>
/** The imported name. */
def name: Name
/** Offset of the position of the importing part of the selector in the source file.
* Is equal to -1 is the position is unknown.
*/
def namePos: Int
/** The name the import is renamed to.
* Is equal to `name` if it's not a renaming import.
*/
def rename: Name
/** Offset of the position of the renaming part of the selector in the source file.
* Is equal to -1 is the position is unknown.
*/
def renamePos: Int
}
/** Import clause
*
* @param expr
* @param selectors
* @group Trees
* @template
*/
type Import >: Null <: SymTree with ImportApi
/** A tag that preserves the identity of the `Import` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val ImportTag: ClassTag[Import]
/** The constructor/extractor for `Import` instances.
* @group Extractors
*/
val Import: ImportExtractor
/** An extractor class to create and pattern match with syntax `Import(expr, selectors)`.
* This AST node corresponds to the following Scala code:
*
* import expr.{selectors}
*
* Selectors are a list of ImportSelectors, which conceptually are pairs of names (from, to).
* The last (and maybe only name) may be a nme.WILDCARD. For instance:
*
* import qual.{x, y => z, _}
*
* Would be represented as:
*
* Import(qual, List(("x", "x"), ("y", "z"), (WILDCARD, null)))
*
* The symbol of an `Import` is an import symbol @see Symbol.newImport.
* It's used primarily as a marker to check that the import has been typechecked.
* @group Extractors
*/
abstract class ImportExtractor {
def apply(expr: Tree, selectors: List[ImportSelector]): Import
def unapply(import_ : Import): Option[(Tree, List[ImportSelector])]
}
/** The API that all imports support
* @group API
*/
trait ImportApi extends SymTreeApi { this: Import =>
/** The qualifier of the import.
* See the example for [[scala.reflect.api.Trees#ImportExtractor]].
*/
def expr: Tree
/** The selectors of the import.
* See the example for [[scala.reflect.api.Trees#ImportExtractor]].
*/
def selectors: List[ImportSelector]
}
/** Instantiation template of a class or trait
*
* @param parents
* @param body
* @group Trees
* @template
*/
type Template >: Null <: SymTree with TemplateApi
/** A tag that preserves the identity of the `Template` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val TemplateTag: ClassTag[Template]
/** The constructor/extractor for `Template` instances.
* @group Extractors
*/
val Template: TemplateExtractor
/** An extractor class to create and pattern match with syntax `Template(parents, self, body)`.
* This AST node corresponds to the following Scala code:
*
* `extends` parents { self => body }
*
* In case when the self-type annotation is missing, it is represented as
* an empty value definition with nme.WILDCARD as name and NoType as type.
*
* The symbol of a template is a local dummy. @see Symbol.newLocalDummy
* The owner of the local dummy is the enclosing trait or class.
* The local dummy is itself the owner of any local blocks. For example:
*
* class C {
* def foo { // owner is C
* def bar // owner is local dummy
* }
* }
* @group Extractors
*/
abstract class TemplateExtractor {
def apply(parents: List[Tree], self: ValDef, body: List[Tree]): Template
def unapply(template: Template): Option[(List[Tree], ValDef, List[Tree])]
}
/** The API that all templates support
* @group API
*/
trait TemplateApi extends SymTreeApi { this: Template =>
/** Superclasses of the template. */
def parents: List[Tree]
/** Self type of the template.
* Is equal to `emptyValDef` if the self type is not specified.
*/
def self: ValDef
/** Body of the template.
*/
def body: List[Tree]
}
/** Block of expressions (semicolon separated expressions)
* @group Trees
* @template
*/
type Block >: Null <: TermTree with BlockApi
/** A tag that preserves the identity of the `Block` abstract type from erasure.
* Can be used for pattern matching, instance tests, serialization and likes.
* @group Tags
*/
implicit val BlockTag: ClassTag[Block]
/** The constructor/extractor for `Block` instances.
* @group Extractors
*/
val Block: BlockExtractor
/** An extractor class to create and pattern match with syntax `Block(stats, expr)`.
* This AST node corresponds to the following Scala code:
*
* { stats; expr }
*
* If the block is empty, the `expr` is set to `Literal(Constant(()))`.
* @group Extractors
*/
abstract class BlockExtractor {
def apply(stats: List[Tree], expr: Tree): Block
def unapply(block: Block): Option[(List[Tree], Tree)]
}
/** The API that all blocks support
* @group API
*/
trait BlockApi extends TermTreeApi { this: Block =>
/** All, but the last, expressions in the block.
* Can very well be an empty list.
*/
def stats: List[Tree]
/** The last expression in the block. */
def expr: Tree
}
/** Case clause in a pattern match.
* (except for occurrences in switch statements).
* Eliminated by compiler phases patmat (in the new pattern matcher of 2.10) or explicitouter (in the old pre-2.10 pattern matcher)
* @group Trees
* @template
*/