|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import core.Phases._ |
| 5 | +import core.DenotTransformers._ |
| 6 | +import core.Symbols._ |
| 7 | +import core.Contexts._ |
| 8 | +import core.Types._ |
| 9 | +import core.Decorators._ |
| 10 | +import dotty.tools.dotc.ast.{Trees, tpd} |
| 11 | +import ast.Trees._ |
| 12 | +import dotty.tools.dotc.core.StdNames._ |
| 13 | +import dotty.tools.dotc.core.Flags |
| 14 | +import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo, TreeTransform} |
| 15 | + |
| 16 | +import scala.annotation.tailrec |
| 17 | + |
| 18 | +class PhantomDeclErasure extends MiniPhaseTransform with InfoTransformer { |
| 19 | + thisTransformer => |
| 20 | + |
| 21 | + import dotty.tools.dotc.ast.tpd._ |
| 22 | + |
| 23 | + override def phaseName: String = "phantomDeclErasure" |
| 24 | + |
| 25 | + /** List of names of phases that should precede this phase */ |
| 26 | + override def runsAfter: Set[Class[_ <: Phase]] = |
| 27 | + Set(classOf[PhantomRefErasure]) |
| 28 | + |
| 29 | + /** Check what the phase achieves, to be called at any point after it is finished. |
| 30 | + */ |
| 31 | + override def checkPostCondition(tree: tpd.Tree)(implicit ctx: Context): Unit = { |
| 32 | + def assertNotPhantom(tpe: Type): Unit = |
| 33 | + assert(!tpe.derivesFrom(defn.PhantomAnyClass), "All phantom type declarations should be erased in " + tree) |
| 34 | + |
| 35 | + tree match { |
| 36 | + case _: TypeTree => |
| 37 | + case ValDef(_, tpt, _) => assertNotPhantom(tpt.typeOpt) |
| 38 | + case DefDef(_, _, _, tpt, _) => assertNotPhantom(tpt.typeOpt) |
| 39 | + case tree: Trees.TypeDef[_] => |
| 40 | + tree.symbol match { |
| 41 | + case sym: ClassSymbol => |
| 42 | + assertNotPhantom(sym.info) |
| 43 | + assert(!sym.classInfo.decls.exists(sym2 => isPhantomMethodType(sym2.info)), |
| 44 | + "All phantom type declarations should be erased in " + sym.classInfo) |
| 45 | + case _ => |
| 46 | + } |
| 47 | + case _ => |
| 48 | + } |
| 49 | + |
| 50 | + super.checkPostCondition(tree) |
| 51 | + } |
| 52 | + |
| 53 | + /* Tree transform */ |
| 54 | + |
| 55 | + override def transformStats(trees: List[tpd.Tree])(implicit ctx: Context, info: TransformerInfo): List[tpd.Tree] = { |
| 56 | + val newTrees = trees.filter { |
| 57 | + case tree: ValOrDefDef => !tree.tpt.tpe.derivesFrom(defn.PhantomAnyClass) |
| 58 | + case tree: TypeDef => !tree.tpe.derivesFrom(defn.PhantomAnyClass) |
| 59 | + case _ => true |
| 60 | + } |
| 61 | + super.transformStats(newTrees) |
| 62 | + } |
| 63 | + |
| 64 | + /* Symbol transform */ |
| 65 | + |
| 66 | + def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context): Type = erasedPhantoms(tp) |
| 67 | + |
| 68 | + private def erasedPhantoms(tp: Type)(implicit ctx: Context): Type = { |
| 69 | + val flags = tp.typeSymbol.flags |
| 70 | + tp match { |
| 71 | + case tp: ClassInfo if !flags.is(Flags.Package) && !tp.typeRef.derivesFrom(defn.PhantomAnyClass) => |
| 72 | + val newDecls = tp.decls.filteredScope(sym => !isPhantomMethodType(sym.info) && !isPhantomClassType(sym.info)) |
| 73 | + ClassInfo(tp.prefix, tp.cls, tp.classParents, newDecls, tp.selfInfo) |
| 74 | + |
| 75 | + case _ => tp |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @tailrec private def isPhantomMethodType(tpe: Type)(implicit ctx: Context): Boolean = tpe match { |
| 80 | + case tpe: MethodicType => tpe.resultType.derivesFrom(defn.PhantomAnyClass) || isPhantomMethodType(tpe.resultType) |
| 81 | + case _ => false |
| 82 | + } |
| 83 | + |
| 84 | + private def isPhantomClassType(tp: Type)(implicit ctx: Context): Boolean = tp match { |
| 85 | + case tp: ClassInfo => tp.cls.derivesFrom(defn.PhantomAnyClass) |
| 86 | + case _ => false |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments