aboutsummaryrefslogtreecommitdiff
path: root/src/dotty
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2014-09-21 14:48:10 +0200
committerDmitry Petrashko <dmitry.petrashko@gmail.com>2014-10-11 08:24:36 +0200
commitd32ff05d101f860dc1b290330d34caa9aea6e1df (patch)
tree23aad3d7ee22a375f99f772e37dc0b09ce5397c1 /src/dotty
parentd288981f42cd8fde42340264a73d30e037bffec5 (diff)
downloaddotty-d32ff05d101f860dc1b290330d34caa9aea6e1df.tar.gz
dotty-d32ff05d101f860dc1b290330d34caa9aea6e1df.tar.bz2
dotty-d32ff05d101f860dc1b290330d34caa9aea6e1df.zip
New utility methods in SymUtils
Diffstat (limited to 'src/dotty')
-rw-r--r--src/dotty/tools/dotc/ast/TreeTypeMap.scala9
-rw-r--r--src/dotty/tools/dotc/transform/SymUtils.scala24
2 files changed, 26 insertions, 7 deletions
diff --git a/src/dotty/tools/dotc/ast/TreeTypeMap.scala b/src/dotty/tools/dotc/ast/TreeTypeMap.scala
index 56602cca9..33be848c1 100644
--- a/src/dotty/tools/dotc/ast/TreeTypeMap.scala
+++ b/src/dotty/tools/dotc/ast/TreeTypeMap.scala
@@ -6,6 +6,7 @@ import core._
import Types._, Contexts._, Constants._, Names._, Flags._
import SymDenotations._, Symbols._, Annotations._, Trees._, Symbols._
import Denotations._, Decorators._
+import dotty.tools.dotc.transform.SymUtils._
/** A map that applies three functions and a substitution together to a tree and
* makes sure they are coordinated so that the result is well-typed. The functions are
@@ -42,13 +43,7 @@ final class TreeTypeMap(
import tpd._
/** If `sym` is one of `oldOwners`, replace by corresponding symbol in `newOwners` */
- def mapOwner(sym: Symbol) = {
- def loop(from: List[Symbol], to: List[Symbol]): Symbol =
- if (from.isEmpty) sym
- else if (sym eq from.head) to.head
- else loop(from.tail, to.tail)
- loop(oldOwners, newOwners)
- }
+ def mapOwner(sym: Symbol) = sym.subst(oldOwners, newOwners)
/** Replace occurrences of `This(oldOwner)` in some prefix of a type
* by the corresponding `This(newOwner)`.
diff --git a/src/dotty/tools/dotc/transform/SymUtils.scala b/src/dotty/tools/dotc/transform/SymUtils.scala
index df0ac59ae..5ae9aba63 100644
--- a/src/dotty/tools/dotc/transform/SymUtils.scala
+++ b/src/dotty/tools/dotc/transform/SymUtils.scala
@@ -7,6 +7,8 @@ import Contexts._
import Symbols._
import Decorators._
import Names._
+import StdNames._
+import Flags._
import language.implicitConversions
object SymUtils {
@@ -17,9 +19,31 @@ object SymUtils {
* 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)
+ }
}