|
| 1 | +package dotty.tools.dotc.transform.phantom |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.tpd |
| 4 | +import dotty.tools.dotc.core.Contexts._ |
| 5 | +import dotty.tools.dotc.core.Decorators._ |
| 6 | +import dotty.tools.dotc.core.Types._ |
| 7 | +import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo} |
| 8 | + |
| 9 | +class PhantomTermEval extends MiniPhaseTransform { |
| 10 | + import tpd._ |
| 11 | + |
| 12 | + override def phaseName: String = "phantomTermEval" |
| 13 | + |
| 14 | + /** Check what the phase achieves, to be called at any point after it is finished. */ |
| 15 | + override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = { |
| 16 | + // TODO |
| 17 | + } |
| 18 | + |
| 19 | + /* Tree transform */ |
| 20 | + |
| 21 | + override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo): Tree = tree.tpe match { |
| 22 | + case _: MethodType => tree |
| 23 | + case _ if tree.args.forall(!_.tpe.isPhantom) => tree |
| 24 | + case _ => |
| 25 | + val argsVals = tree.args.map(toSynthVal) |
| 26 | + |
| 27 | + def evalArgsInBlock() = Block(argsVals, cpy.Apply(tree)(tree.fun, refs(argsVals))) |
| 28 | + def evalFunAndArgsInBlock(fun: Select) = { |
| 29 | + val qualVal = toSynthVal(fun.qualifier) |
| 30 | + val app = cpy.Apply(tree)(Select(ref(qualVal.symbol), fun.name), refs(argsVals)) |
| 31 | + Block(qualVal :: argsVals, app) |
| 32 | + } |
| 33 | + |
| 34 | + tree.fun match { |
| 35 | + case fun: Select => |
| 36 | + if (fun.qualifier.isInstanceOf[New]) evalArgsInBlock() |
| 37 | + else evalFunAndArgsInBlock(fun) |
| 38 | + case _ => evalArgsInBlock() |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /* private methods */ |
| 43 | + |
| 44 | + private def toSynthVal(t: Tree)(implicit ctx: Context) = |
| 45 | + SyntheticValDef(ctx.freshName("ev$").toTermName, t) |
| 46 | + |
| 47 | + private def refs(vals: List[Tree])(implicit ctx: Context) = |
| 48 | + vals.map(x => ref(x.symbol)) |
| 49 | + |
| 50 | +} |
0 commit comments