@@ -588,63 +588,97 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
588
588
tree
589
589
}
590
590
591
+ /** A select node with the given selector name and a computed type */
591
592
def select (name : Name )(implicit ctx : Context ): Select =
592
593
Select (tree, name)
593
594
595
+ /** A select node with the given type */
594
596
def select (tp : NamedType )(implicit ctx : Context ): Select =
595
597
untpd.Select (tree, tp.name).withType(tp)
596
598
599
+ /** A select node that selects the given symbol. Note: Need to make sure this
600
+ * is in fact the symbol you would get when you select with the symbol's name,
601
+ * otherwise a data race may occur which would be flagged by -Yno-double-bindings.
602
+ */
597
603
def select (sym : Symbol )(implicit ctx : Context ): Select =
598
604
untpd.Select (tree, sym.name).withType(
599
605
TermRef .withSigAndDenot(tree.tpe, sym.name.asTermName, sym.signature, sym.denot.asSeenFrom(tree.tpe)))
600
606
601
- def selectWithSig (name : Name , sig : Signature )(implicit ctx : Context ) =
607
+ /** A select node with the given selector name and signature and a computed type */
608
+ def selectWithSig (name : Name , sig : Signature )(implicit ctx : Context ): Tree =
602
609
untpd.SelectWithSig (tree, name, sig)
603
610
.withType(TermRef .withSig(tree.tpe, name.asTermName, sig))
604
611
612
+ /** A select node with selector name and signature taken from `sym`.
613
+ * Note: Use this method instead of select(sym) if the referenced symbol
614
+ * might be overridden in the type of the qualifier prefix. See note
615
+ * on select(sym: Symbol).
616
+ */
617
+ def selectWithSig (sym : Symbol )(implicit ctx : Context ): Tree =
618
+ selectWithSig(sym.name, sym.signature)
619
+
620
+ /** A unary apply node with given argument: `tree(arg)` */
605
621
def appliedTo (arg : Tree )(implicit ctx : Context ): Tree =
606
622
appliedToArgs(arg :: Nil )
607
623
624
+ /** An apply node with given arguments: `tree(arg, args0, ..., argsN)` */
608
625
def appliedTo (arg : Tree , args : Tree * )(implicit ctx : Context ): Tree =
609
626
appliedToArgs(arg :: args.toList)
610
627
628
+ /** An apply node with given argument list `tree(args(0), ..., args(args.length - 1))` */
611
629
def appliedToArgs (args : List [Tree ])(implicit ctx : Context ): Apply =
612
630
Apply (tree, args)
613
631
632
+ /** The current tree applied to given argument lists:
633
+ * `tree (argss(0)) ... (argss(argss.length -1))`
634
+ */
614
635
def appliedToArgss (argss : List [List [Tree ]])(implicit ctx : Context ): Tree =
615
636
((tree : Tree ) /: argss)(Apply (_, _))
616
637
638
+ /** The current tree applied to (): `tree()` */
617
639
def appliedToNone (implicit ctx : Context ): Apply = appliedToArgs(Nil )
618
640
641
+ /** The current tree applied to given type argument: `tree[targ]` */
619
642
def appliedToType (targ : Type )(implicit ctx : Context ): Tree =
620
643
appliedToTypes(targ :: Nil )
621
644
645
+ /** The current tree applied to given type arguments: `tree[targ0, ..., targN]` */
622
646
def appliedToTypes (targs : List [Type ])(implicit ctx : Context ): Tree =
623
647
appliedToTypeTrees(targs map (TypeTree (_)))
624
648
649
+ /** The current tree applied to given type argument list: `tree[targs(0), ..., targs(targs.length - 1)]` */
625
650
def appliedToTypeTrees (targs : List [Tree ])(implicit ctx : Context ): Tree =
626
651
if (targs.isEmpty) tree else TypeApply (tree, targs)
627
652
653
+ /** Apply to `()` unless tree's widened type is parameterless */
628
654
def ensureApplied (implicit ctx : Context ): Tree =
629
655
if (tree.tpe.widen.isParameterless) tree else tree.appliedToNone
630
656
657
+ /** `tree.isInstanceOf[tp]` */
631
658
def isInstance (tp : Type )(implicit ctx : Context ): Tree =
632
659
tree.select(defn.Any_isInstanceOf ).appliedToType(tp)
633
660
661
+ /** tree.asInstanceOf[`tp`] */
634
662
def asInstance (tp : Type )(implicit ctx : Context ): Tree = {
635
663
assert(tp.isValueType, i " bad cast: $tree.asInstanceOf[ $tp] " )
636
664
tree.select(defn.Any_asInstanceOf ).appliedToType(tp)
637
665
}
638
666
667
+ /** `tree.asInstanceOf[tp]` unless tree's type already conforms to `tp` */
639
668
def ensureConforms (tp : Type )(implicit ctx : Context ): Tree =
640
669
if (tree.tpe <:< tp) tree else asInstance(tp)
641
670
671
+ /** `this && that`, for boolean trees `this`, `that` */
642
672
def and (that : Tree )(implicit ctx : Context ): Tree =
643
673
tree.select(defn.Boolean_&& ).appliedTo(that)
644
674
675
+ /** `this || that`, for boolean trees `this`, `that` */
645
676
def or (that : Tree )(implicit ctx : Context ): Tree =
646
677
tree.select(defn.Boolean_|| ).appliedTo(that)
647
678
679
+ /** The translation of `tree = rhs`.
680
+ * This is either the tree as an assignment, to a setter call.
681
+ */
648
682
def becomes (rhs : Tree )(implicit ctx : Context ): Tree =
649
683
if (tree.symbol is Method ) {
650
684
val setr = tree match {
@@ -660,20 +694,23 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
660
694
661
695
// --- Higher order traversal methods -------------------------------
662
696
663
- def foreachSubTree (f : Tree => Unit )(implicit ctx : Context ): Unit = { // TODO should go in tpd.
697
+ /** Apply `f` to each subtree of this tree */
698
+ def foreachSubTree (f : Tree => Unit )(implicit ctx : Context ): Unit = {
664
699
val traverser = new TreeTraverser {
665
700
def traverse (tree : Tree )(implicit ctx : Context ) = foldOver(f(tree), tree)
666
701
}
667
702
traverser.traverse(tree)
668
703
}
669
704
705
+ /** Is there a subtree of this tree that satisfies predicate `p`? */
670
706
def existsSubTree (p : Tree => Boolean )(implicit ctx : Context ): Boolean = {
671
707
val acc = new TreeAccumulator [Boolean ] {
672
708
def apply (x : Boolean , t : Tree )(implicit ctx : Context ) = x || p(t) || foldOver(x, t)
673
709
}
674
710
acc(false , tree)
675
711
}
676
712
713
+ /** All subtrees of this tree that satisfy predicate `p`. */
677
714
def filterSubTrees (f : Tree => Boolean )(implicit ctx : Context ): List [Tree ] = {
678
715
val buf = new mutable.ListBuffer [Tree ]
679
716
foreachSubTree { tree => if (f(tree)) buf += tree }
0 commit comments