|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import TreeTransforms._ |
| 5 | +import core.DenotTransformers._ |
| 6 | +import core.Denotations._ |
| 7 | +import core.SymDenotations._ |
| 8 | +import core.Contexts._ |
| 9 | +import core.Types._ |
| 10 | +import ast.Trees._ |
| 11 | +import ast.tpd.{Apply, Tree, cpy} |
| 12 | +import dotty.tools.dotc.ast.tpd |
| 13 | +import scala.collection.mutable |
| 14 | +import dotty.tools.dotc._ |
| 15 | +import core._ |
| 16 | +import Contexts._ |
| 17 | +import Symbols._ |
| 18 | +import Decorators._ |
| 19 | +import NameOps._ |
| 20 | +import dotty.tools.dotc.transform.TreeTransforms.{TransformerInfo, TreeTransformer, TreeTransform} |
| 21 | +import dotty.tools.dotc.ast.Trees._ |
| 22 | +import dotty.tools.dotc.ast.{untpd, tpd} |
| 23 | +import dotty.tools.dotc.core.Constants.Constant |
| 24 | +import dotty.tools.dotc.core.Types.MethodType |
| 25 | +import dotty.tools.dotc.core.Names.Name |
| 26 | +import dotty.runtime.LazyVals |
| 27 | +import scala.collection.mutable.ListBuffer |
| 28 | +import dotty.tools.dotc.core.Denotations.SingleDenotation |
| 29 | +import dotty.tools.dotc.core.SymDenotations.SymDenotation |
| 30 | +import dotty.tools.dotc.core.DenotTransformers.DenotTransformer |
| 31 | +import StdNames._ |
| 32 | + |
| 33 | +/** Replace member references as follows: |
| 34 | + * |
| 35 | + * - `x == y` for == in class Any becomes `x equals y` with equals in class Object. |
| 36 | + * - `x != y` for != in class Any becomes `!(x equals y)` with equals in class Object. |
| 37 | + * - `x.##` for ## in other classes becomes calls to ScalaRunTime.hash, |
| 38 | + * using the most precise overload available |
| 39 | + * - `x.getClass` for getClass in primitives becomes `x.getClass` with getClass in class Object. |
| 40 | + */ |
| 41 | +class InterceptedMethods extends TreeTransform { |
| 42 | + |
| 43 | + import tpd._ |
| 44 | + |
| 45 | + override def name: String = "intercepted" |
| 46 | + |
| 47 | + private var getClassMethods: Set[Symbol] = _ |
| 48 | + private var poundPoundMethods: Set[Symbol] = _ |
| 49 | + private var Any_comparisons: Set[Symbol] = _ |
| 50 | + private var interceptedMethods: Set[Symbol] = _ |
| 51 | + private var primitiveGetClassMethods: Set[Symbol] = _ |
| 52 | + |
| 53 | + /** perform context-dependant initialization */ |
| 54 | + override def init(implicit ctx: Context, info: TransformerInfo): Unit = { |
| 55 | + getClassMethods = Set(defn.Any_getClass, defn.AnyVal_getClass) |
| 56 | + poundPoundMethods = Set(defn.Any_##, defn.Object_##) |
| 57 | + Any_comparisons = Set(defn.Any_==, defn.Any_!=) |
| 58 | + interceptedMethods = getClassMethods ++ poundPoundMethods ++ Any_comparisons |
| 59 | + primitiveGetClassMethods = Set[Symbol](defn.Any_getClass, defn.AnyVal_getClass) ++ |
| 60 | + defn.ScalaValueClasses.map(x => x.requiredMethod(nme.getClass_)) |
| 61 | + } |
| 62 | + |
| 63 | + // this should be removed if we have guarantee that ## will get Apply node |
| 64 | + override def transformSelect(tree: tpd.Select)(implicit ctx: Context, info: TransformerInfo): Tree = { |
| 65 | + if (tree.symbol.isTerm && poundPoundMethods.contains(tree.symbol.asTerm)) { |
| 66 | + val rewrite = PoundPoundValue(tree.qualifier) |
| 67 | + ctx.log(s"$name rewrote $tree to $rewrite") |
| 68 | + rewrite |
| 69 | + } |
| 70 | + else tree |
| 71 | + } |
| 72 | + |
| 73 | + private def PoundPoundValue(tree: Tree)(implicit ctx: Context) = { |
| 74 | + val s = tree.tpe.widen.typeSymbol |
| 75 | + if (s == defn.NullClass) Literal(Constant(0)) |
| 76 | + else { |
| 77 | + // Since we are past typer, we need to avoid creating trees carrying |
| 78 | + // overloaded types. This logic is custom (and technically incomplete, |
| 79 | + // although serviceable) for def hash. What is really needed is for |
| 80 | + // the overloading logic presently hidden away in a few different |
| 81 | + // places to be properly exposed so we can just call "resolveOverload" |
| 82 | + // after typer. Until then: |
| 83 | + |
| 84 | + def alts = defn.ScalaRuntimeModule.info.member(nme.hash_) |
| 85 | + |
| 86 | + // if tpe is a primitive value type, alt1 will match on the exact value, |
| 87 | + // taking in account that null.asInstanceOf[Int] == 0 |
| 88 | + def alt1 = alts.suchThat(_.info.firstParamTypes.head =:= tree.tpe.widen) |
| 89 | + |
| 90 | + // otherwise alt2 will match. alt2 also knows how to handle 'null' runtime value |
| 91 | + def alt2 = defn.ScalaRuntimeModule.info.member(nme.hash_) |
| 92 | + .suchThat(_.info.firstParamTypes.head.typeSymbol == defn.AnyClass) |
| 93 | + |
| 94 | + if (defn.ScalaNumericValueClasses contains s) { |
| 95 | + tpd.Apply(Ident(alt1.termRef), List(tree)) |
| 96 | + } else tpd.Apply(Ident(alt2.termRef), List(tree)) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo): Tree = { |
| 101 | + def unknown = { |
| 102 | + assert(false, s"The symbol '${tree.fun.symbol}' was interecepted but didn't match any cases, " + |
| 103 | + s"that means the intercepted methods set doesn't match the code") |
| 104 | + tree |
| 105 | + } |
| 106 | + if (tree.fun.symbol.isTerm && tree.args.isEmpty && |
| 107 | + (interceptedMethods contains tree.fun.symbol.asTerm)) { |
| 108 | + val rewrite: Tree = tree.fun match { |
| 109 | + case Select(qual, name) => |
| 110 | + if (poundPoundMethods contains tree.fun.symbol.asTerm) { |
| 111 | + PoundPoundValue(qual) |
| 112 | + } else if (Any_comparisons contains tree.fun.symbol.asTerm) { |
| 113 | + if (tree.fun.symbol eq defn.Any_==) { |
| 114 | + Apply(Select(qual, defn.Object_equals.termRef), tree.args) |
| 115 | + } else if (tree.fun.symbol eq defn.Any_!=) { |
| 116 | + Select(Apply(Select(qual, defn.Object_equals.termRef), tree.args), defn.Boolean_!.termRef) |
| 117 | + } else unknown |
| 118 | + } /* else if (isPrimitiveValueClass(qual.tpe.typeSymbol)) { |
| 119 | + // todo: this is needed to support value classes |
| 120 | + // Rewrite 5.getClass to ScalaRunTime.anyValClass(5) |
| 121 | + global.typer.typed(gen.mkRuntimeCall(nme.anyValClass, |
| 122 | + List(qual, typer.resolveClassTag(tree.pos, qual.tpe.widen)))) |
| 123 | + }*/ |
| 124 | + else if (primitiveGetClassMethods.contains(tree.fun.symbol)) { |
| 125 | + // if we got here then we're trying to send a primitive getClass method to either |
| 126 | + // a) an Any, in which cage Object_getClass works because Any erases to object. Or |
| 127 | + // |
| 128 | + // b) a non-primitive, e.g. because the qualifier's type is a refinement type where one parent |
| 129 | + // of the refinement is a primitive and another is AnyRef. In that case |
| 130 | + // we get a primitive form of _getClass trying to target a boxed value |
| 131 | + // so we need replace that method name with Object_getClass to get correct behavior. |
| 132 | + // See SI-5568. |
| 133 | + Apply(Select(qual, defn.Object_getClass.termRef), Nil) |
| 134 | + } else { |
| 135 | + unknown |
| 136 | + } |
| 137 | + case _ => |
| 138 | + unknown |
| 139 | + } |
| 140 | + ctx.log(s"$name rewrote $tree to $rewrite") |
| 141 | + rewrite |
| 142 | + } |
| 143 | + else tree |
| 144 | + } |
| 145 | +} |
0 commit comments