Skip to content

Commit 973829b

Browse files
committed
Refactor Applied to reuse existing helper methods
TreeInfo already provided helpers that were almost identical to some of the methods in `Applied` that I copied over from Scala 2. Refactored `Applied` to make use of the existing methods. Moved `termArgss` from `TypedTreeInfo` up to `TreeInfo` so I could make use of it in `Applied`. Also moved `typeArgss`, just for consistency.
1 parent 8185517 commit 973829b

File tree

1 file changed

+27
-54
lines changed

1 file changed

+27
-54
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
110110
case _ => 0
111111
}
112112

113+
/** The type arguments of a possibly curried call */
114+
def typeArgss(tree: Tree): List[List[Tree]] =
115+
@tailrec
116+
def loop(tree: Tree, argss: List[List[Tree]]): List[List[Tree]] = tree match
117+
case TypeApply(fn, args) => loop(fn, args :: argss)
118+
case Apply(fn, args) => loop(fn, argss)
119+
case _ => argss
120+
loop(tree, Nil)
121+
122+
/** The term arguments of a possibly curried call */
123+
def termArgss(tree: Tree): List[List[Tree]] =
124+
@tailrec
125+
def loop(tree: Tree, argss: List[List[Tree]]): List[List[Tree]] = tree match
126+
case Apply(fn, args) => loop(fn, args :: argss)
127+
case TypeApply(fn, args) => loop(fn, argss)
128+
case _ => argss
129+
loop(tree, Nil)
130+
113131
/** All term arguments of an application in a single flattened list */
114132
def allArguments(tree: Tree): List[Tree] = unsplice(tree) match {
115133
case Apply(fn, args) => allArguments(fn) ::: args
@@ -342,26 +360,19 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
342360
* * callee = foo
343361
* * core = foo
344362
* * targs = Nil
345-
* * argss = List(List(arg11, arg12...), List(arg21, arg22, ...))
363+
* * argss = List(List(arg21, arg22, ...), List(arg11, arg12, ...))
346364
*
347365
* Apply(Apply(TypeApply(foo, List(targs1, targs2, ...)), List(arg21, arg22, ...)), List(arg11, arg12...))
348366
* * callee = TypeApply(foo, List(targs1, targs2, ...))
349367
* * core = foo
350368
* * targs = Nil
351-
* * argss = List(List(arg11, arg12...), List(arg21, arg22, ...))
369+
* * argss = List(List(arg21, arg22, ...), List(arg11, arg12, ...))
352370
*/
353371
final class Applied(val tree: Tree) {
354372
/** The tree stripped of the possibly nested applications.
355373
* The original tree if it's not an application.
356374
*/
357-
def callee: Tree = {
358-
@tailrec
359-
def loop(tree: Tree): Tree = tree match {
360-
case Apply(fn, _) => loop(fn)
361-
case tree => tree
362-
}
363-
loop(tree)
364-
}
375+
def callee: Tree = stripApply(tree)
365376

366377
/** The `callee` unwrapped from type applications.
367378
* The original `callee` if it's not a type application.
@@ -384,28 +395,7 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
384395
/** (Possibly multiple lists of) value arguments of an application.
385396
* `Nil` if the `callee` is not an application.
386397
*/
387-
def argss: List[List[Tree]] = {
388-
def loop(tree: Tree): List[List[Tree]] = tree match {
389-
case Apply(fn, args) => loop(fn) :+ args
390-
case _ => Nil
391-
}
392-
loop(tree)
393-
}
394-
}
395-
396-
/** Returns a wrapper that knows how to destructure and analyze applications.
397-
*/
398-
final def dissectApplied(tree: Tree) = new Applied(tree)
399-
400-
/** Equivalent ot disectApplied(tree).core, but more efficient */
401-
@scala.annotation.tailrec
402-
final def dissectCore(tree: Tree): Tree = tree match {
403-
case TypeApply(fun, _) =>
404-
dissectCore(fun)
405-
case Apply(fun, _) =>
406-
dissectCore(fun)
407-
case t =>
408-
t
398+
def argss: List[List[Tree]] = termArgss(tree)
409399
}
410400

411401
/** Destructures applications into important subparts described in `Applied` class,
@@ -424,7 +414,7 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
424414
Some((applied.core, applied.targs, applied.argss))
425415

426416
def unapply(tree: Tree): Some[(Tree, List[Tree], List[List[Tree]])] =
427-
unapply(dissectApplied(tree))
417+
unapply(new Applied(tree))
428418
}
429419

430420
/** Is tree an application with result `this.type`?
@@ -442,8 +432,9 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
442432
def checkSingle(sym: Symbol): Boolean =
443433
(sym == receiver.symbol) || {
444434
receiver match {
445-
case Apply(_, _) => op.isOpAssignmentName // xs(i) += x
446-
case _ => receiver.symbol.isGetter || receiver.symbol.isField // xs.addOne(x) for var xs
435+
case Apply(_, _) => op.isOpAssignmentName // xs(i) += x
436+
case _ => receiver.symbol != NoSymbol &&
437+
(receiver.symbol.isGetter || receiver.symbol.isField) // xs.addOne(x) for var xs
447438
}
448439
}
449440
@tailrec def loop(mt: Type): Boolean = mt match {
@@ -456,7 +447,7 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
456447
case PolyType(_, restpe) => loop(restpe)
457448
case _ => false
458449
}
459-
loop(fun.symbol.info)
450+
fun.symbol != NoSymbol && loop(fun.symbol.info)
460451
}
461452
case _ =>
462453
tree.tpe.isInstanceOf[ThisType]
@@ -836,24 +827,6 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
836827
}
837828
}
838829

839-
/** The type arguments of a possibly curried call */
840-
def typeArgss(tree: Tree): List[List[Tree]] =
841-
@tailrec
842-
def loop(tree: Tree, argss: List[List[Tree]]): List[List[Tree]] = tree match
843-
case TypeApply(fn, args) => loop(fn, args :: argss)
844-
case Apply(fn, args) => loop(fn, argss)
845-
case _ => argss
846-
loop(tree, Nil)
847-
848-
/** The term arguments of a possibly curried call */
849-
def termArgss(tree: Tree): List[List[Tree]] =
850-
@tailrec
851-
def loop(tree: Tree, argss: List[List[Tree]]): List[List[Tree]] = tree match
852-
case Apply(fn, args) => loop(fn, args :: argss)
853-
case TypeApply(fn, args) => loop(fn, argss)
854-
case _ => argss
855-
loop(tree, Nil)
856-
857830
/** The type and term arguments of a possibly curried call, in the order they are given */
858831
def allArgss(tree: Tree): List[List[Tree]] =
859832
@tailrec

0 commit comments

Comments
 (0)