aboutsummaryrefslogtreecommitdiff
path: root/src/dotty
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2014-03-31 17:20:02 +0200
committerDmitry Petrashko <dmitry.petrashko@gmail.com>2014-04-08 16:55:56 +0200
commitdd5341106d9b57a99316a26a1f0c7e195b6debf1 (patch)
tree2a555b335e2d661a828014e35e91e308fec4798e /src/dotty
parenteb3df0db4a57d89c0d7370a9180742273db166b2 (diff)
downloaddotty-dd5341106d9b57a99316a26a1f0c7e195b6debf1.tar.gz
dotty-dd5341106d9b57a99316a26a1f0c7e195b6debf1.tar.bz2
dotty-dd5341106d9b57a99316a26a1f0c7e195b6debf1.zip
Fleshed out Splitter phase
Implemented splitting operations As a side effect, this contains a test ruling out structural term member dispatch. Tests 0586 and 0625 which used structural dispatch got moved to neg.
Diffstat (limited to 'src/dotty')
-rw-r--r--src/dotty/tools/dotc/Run.scala2
-rw-r--r--src/dotty/tools/dotc/transform/Splitter.scala106
2 files changed, 104 insertions, 4 deletions
diff --git a/src/dotty/tools/dotc/Run.scala b/src/dotty/tools/dotc/Run.scala
index 264373baf..247fa4336 100644
--- a/src/dotty/tools/dotc/Run.scala
+++ b/src/dotty/tools/dotc/Run.scala
@@ -51,7 +51,7 @@ class Run(comp: Compiler)(implicit ctx: Context) {
private def printTree(ctx: Context) = {
val unit = ctx.compilationUnit
- println(s"result of $unit after ${ctx.phase}:")
+ println(s"result of $unit after ${ctx.phase.prev}:")
println(unit.tpdTree.show(ctx))
}
diff --git a/src/dotty/tools/dotc/transform/Splitter.scala b/src/dotty/tools/dotc/transform/Splitter.scala
index 43a91a058..978a9cce4 100644
--- a/src/dotty/tools/dotc/transform/Splitter.scala
+++ b/src/dotty/tools/dotc/transform/Splitter.scala
@@ -3,10 +3,10 @@ package transform
import TreeTransforms._
import ast.Trees._
-import core.Contexts._
-import core.Types._
+import core._
+import Contexts._, Types._, Decorators._, Denotations._, Symbols._, SymDenotations._, Names._
-/** This transform makes usre every identifier and select node
+/** This transform makes sure every identifier and select node
* carries a symbol. To do this, certain qualifiers with a union type
* have to be "splitted" with a type test.
*
@@ -24,4 +24,104 @@ class Splitter extends TreeTransform {
This(cls) withPos tree.pos
case _ => tree
}
+
+ /** If we select a name, make sure the node has a symbol.
+ * If necessary, split the qualifier with type tests.
+ * Example: Assume:
+ *
+ * class A { def f(x: S): T }
+ * class B { def f(x: S): T }
+ * def p(): A | B
+ *
+ * Then p().f(a) translates to
+ *
+ * val ev$1 = p()
+ * if (ev$1.isInstanceOf[A]) ev$1.asInstanceOf[A].f(a)
+ * else ev$1.asInstanceOf[B].f(a)
+ */
+ override def transformSelect(tree: Select)(implicit ctx: Context, info: TransformerInfo) = {
+ val Select(qual, name) = tree
+
+ def memberDenot(tp: Type): SingleDenotation = {
+ val mbr = tp.member(name)
+ if (!mbr.isOverloaded) mbr.asSingleDenotation
+ else tree.tpe match {
+ case tref: TermRefWithSignature => mbr.atSignature(tref.sig)
+ case _ => ctx.error(s"cannot disambiguate overloaded member $mbr"); NoDenotation
+ }
+ }
+
+ def candidates(tp: Type): List[Symbol] = {
+ val mbr = memberDenot(tp)
+ if (mbr.symbol.exists) mbr.symbol :: Nil
+ else tp.widen match {
+ case tref: TypeRef =>
+ tref.info match {
+ case TypeBounds(_, hi) => candidates(hi)
+ case _ => Nil
+ }
+ case OrType(tp1, tp2) =>
+ candidates(tp1) | candidates(tp2)
+ case AndType(tp1, tp2) =>
+ candidates(tp1) & candidates(tp2)
+ case tpw =>
+ Nil
+ }
+ }
+
+ def isStructuralSelect(tp: Type): Boolean = tp.stripTypeVar match {
+ case tp: RefinedType => tp.refinedName == name || isStructuralSelect(tp)
+ case tp: TypeProxy => isStructuralSelect(tp.underlying)
+ case AndType(tp1, tp2) => isStructuralSelect(tp1) || isStructuralSelect(tp2)
+ case _ => false
+ }
+
+ if (tree.symbol.exists) tree
+ else {
+ def choose(qual: Tree, syms: List[Symbol]): Tree = {
+ def testOrCast(which: Symbol, mbr: Symbol) =
+ TypeApply(Select(qual, which), TypeTree(mbr.owner.typeRef) :: Nil)
+ def select(sym: Symbol) = {
+ val qual1 =
+ if (qual.tpe derivesFrom sym.owner) qual
+ else testOrCast(defn.Any_asInstanceOf, sym)
+ Select(qual1, sym) withPos tree.pos
+ }
+ syms match {
+ case Nil =>
+ def msg =
+ if (isStructuralSelect(qual.tpe))
+ s"cannot access member '$name' from structural type ${qual.tpe.widen.show}; use Dynamic instead"
+ else
+ s"no candidate symbols for ${tree.tpe.show} found in ${qual.tpe.show}"
+ ctx.error(msg, tree.pos)
+ tree
+ case sym :: Nil =>
+ select(sym)
+ case sym :: syms1 =>
+ If(testOrCast(defn.Any_isInstanceOf, sym), select(sym), choose(qual, syms1))
+ }
+ }
+ evalOnce(qual)(qual => choose(qual, candidates(qual.tpe)))
+ }
+ }
+
+ /** Distribute arguments among splitted branches */
+ def distribute(tree: GenericApply[Type], rebuild: (Tree, List[Tree]) => Context => Tree)(implicit ctx: Context) = {
+ def recur(fn: Tree): Tree = fn match {
+ case Block(stats, expr) => Block(stats, recur(expr))
+ case If(cond, thenp, elsep) => If(cond, recur(thenp), recur(elsep))
+ case _ => rebuild(fn, tree.args)(ctx) withPos tree.pos
+ }
+ recur(tree.fun)
+ }
+
+ override def transformTypeApply(tree: TypeApply)(implicit ctx: Context, info: TransformerInfo) =
+ distribute(tree, typeApply)
+
+ override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo) =
+ distribute(tree, apply)
+
+ private val typeApply = (fn: Tree, args: List[Tree]) => (ctx: Context) => TypeApply(fn, args)(ctx)
+ private val apply = (fn: Tree, args: List[Tree]) => (ctx: Context) => Apply(fn, args)(ctx)
} \ No newline at end of file