blob: 82bea1813fcfaca31f5423386c1d0a82d42d3d8f (
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 transform
import core._
import Types._
import Contexts._
import Symbols._
import Decorators._
import Names._
import StdNames._
import NameOps._
import Flags._
import language.implicitConversions
object SymUtils {
implicit def decorateSymUtils(sym: Symbol): SymUtils = new SymUtils(sym)
}
/** A decorator that provides methods on symbols
* that are needed in the transformer pipeline.
*/
class SymUtils(val self: Symbol) extends AnyVal {
import SymUtils._
def isTypeTestOrCast(implicit ctx: Context): Boolean =
self == defn.Any_asInstanceOf || self == defn.Any_isInstanceOf
def isVolatile(implicit ctx: Context) = self.hasAnnotation(defn.VolatileAnnot)
/** If this is a constructor, its owner: otherwise this. */
final def skipConstructor(implicit ctx: Context): Symbol =
if (self.isConstructor) self.owner else self
final def isAnonymousFunction(implicit ctx: Context): Boolean =
self.is(Method) && (self.denot.initial.asSymDenotation.name startsWith nme.ANON_FUN)
/** The logically enclosing method or class for this symbol.
* Instead of constructors one always picks the enclosing class.
*/
final def enclosure(implicit ctx: Context) = self.owner.enclosingMethod.skipConstructor
/** Apply symbol/symbol substitution to this symbol */
def subst(from: List[Symbol], to: List[Symbol]): Symbol = {
def loop(from: List[Symbol], to: List[Symbol]): Symbol =
if (from.isEmpty) self
else if (self eq from.head) to.head
else loop(from.tail, to.tail)
loop(from, to)
}
def accessorNamed(name: TermName)(implicit ctx: Context): Symbol =
self.owner.info.decl(name).suchThat(_ is Accessor).symbol
def getter(implicit ctx: Context): Symbol =
if (self.isGetter) self else accessorNamed(self.asTerm.name.getterName)
def setter(implicit ctx: Context): Symbol =
if (self.isSetter) self
else accessorNamed(self.asTerm.name.setterName) orElse
accessorNamed(self.asTerm.name.traitSetterName)
def field(implicit ctx: Context): Symbol =
self.owner.info.decl(self.asTerm.name.fieldName).suchThat(!_.is(Method)).symbol
}
|