blob: 87473ca338f45b6ba5383bedacb4fb7a427ebb35 (
plain) (
blame)
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
|
package dotty.tools.dotc
package core
import annotation.tailrec
import Symbols._
import Contexts._, Names._, Phases._
/** This object provides useful implicit decorators for types defined elsewhere */
object Decorators {
/** Turns Strings into PreNames, adding toType/TermName methods */
implicit class StringDecorator(val s: String) extends AnyVal with PreName {
def toTypeName: TypeName = typeName(s)
def toTermName: TermName = termName(s)
def toEncodedTypeName = encodedTypeName(s)
def toEncodedTermName = encodedTermName(s)
}
/** Implements a findSymbol method on iterators of Symbols that
* works like find but avoids Option, replacing None with NoSymbol.
*/
implicit class SymbolIteratorDecorator(val it: Iterator[Symbol]) extends AnyVal {
final def findSymbol(p: Symbol => Boolean): Symbol = {
while (it.hasNext) {
val sym = it.next
if (p(sym)) return sym
}
NoSymbol
}
}
final val MaxFilterRecursions = 1000
/** Implements a filterConserve method on lists that avoids
* duplication of list nodes if all tests yield true. Works for lists
* up to `MaxFilterRecursions` length.
*/
implicit class ListDecorator[T](val xs: List[T]) extends AnyVal {
def filterConserve(p: T => Boolean): List[T] = {
def loop(xs: List[T], nrec: Int): List[T] = xs match {
case Nil => xs
case x :: xs1 =>
if (nrec < MaxFilterRecursions) {
val ys1 = loop(xs1, nrec + 1)
if (p(x))
if (ys1 eq xs1) xs else x :: ys1
else
ys1
} else xs filter p
}
loop(xs, 0)
}
}
/** Implements a test whether a list of strings representing phases contains
* a given phase. The test returns true if the given phase starts with
* one of the names in the list of strings.
*/
implicit class PhaseListDecorator(val names: List[String]) extends AnyVal {
def containsPhase(phase: Phase) =
names exists (phase.name.startsWith)
}
}
|