|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import core._ |
| 5 | +import DenotTransformers.SymTransformer |
| 6 | +import Phases.Phase |
| 7 | +import Contexts.Context |
| 8 | +import Flags._ |
| 9 | +import Symbols._ |
| 10 | +import SymDenotations.SymDenotation |
| 11 | +import ast.Trees._ |
| 12 | +import collection.mutable |
| 13 | +import Decorators._ |
| 14 | +import NameOps._ |
| 15 | +import TreeTransforms.MiniPhaseTransform |
| 16 | +import dotty.tools.dotc.transform.TreeTransforms.TransformerInfo |
| 17 | + |
| 18 | +/** Remove companion objects that are empty |
| 19 | + * Lots of constraints here: |
| 20 | + * 1. It's impractical to place DropEmptyCompanions before lambda lift because dropped |
| 21 | + * modules can be anywhere and have hard to trace references. |
| 22 | + * 2. DropEmptyCompanions cannot be interleaved with LambdaLift or Flatten because |
| 23 | + * they put things in liftedDefs sets which cause them to surface later. So |
| 24 | + * removed modules resurface. |
| 25 | + * 3. DropEmptyCompanions has to be before RestoreScopes. |
| 26 | + * The solution to the constraints is to put DropEmptyCompanions between Flatten |
| 27 | + * and RestoreScopes and to only start working once we are back on PackageDef |
| 28 | + * level, so we know that all objects moved by LambdaLift and Flatten have arrived |
| 29 | + * at their destination. |
| 30 | + */ |
| 31 | +class DropEmptyCompanions extends MiniPhaseTransform { thisTransform => |
| 32 | + import ast.tpd._ |
| 33 | + override def phaseName = "dropEmpty" |
| 34 | + override def runsAfter: Set[Class[_ <: Phase]] = Set(classOf[Flatten]) |
| 35 | + |
| 36 | + override def transformPackageDef(pdef: PackageDef)(implicit ctx: Context, info: TransformerInfo) = { |
| 37 | + |
| 38 | + /** Is `tree` an empty companion object? */ |
| 39 | + def isEmptyCompanion(tree: Tree) = tree match { |
| 40 | + case TypeDef(_, impl: Template) if tree.symbol.is(SyntheticModule) && |
| 41 | + tree.symbol.companionClass.exists && |
| 42 | + impl.body.forall(_.symbol.isPrimaryConstructor) => |
| 43 | + println(i"removing ${tree.symbol}") |
| 44 | + true |
| 45 | + case _ => |
| 46 | + false |
| 47 | + } |
| 48 | + |
| 49 | + val dropped = pdef.stats.filter(isEmptyCompanion).map(_.symbol).toSet |
| 50 | + |
| 51 | + /** Symbol is a $lzy field representing a module */ |
| 52 | + def isLazyModuleVar(sym: Symbol) = |
| 53 | + sym.name.isLazyLocal && |
| 54 | + sym.owner.info.decl(sym.name.asTermName.nonLazyName).symbol.is(Module) |
| 55 | + |
| 56 | + /** Symbol should be dropped together with a dropped companion object. |
| 57 | + * Such symbols are: |
| 58 | + * - lzy fields pointing to modules, |
| 59 | + * - vals and getters representing modules. |
| 60 | + */ |
| 61 | + def symIsDropped(sym: Symbol): Boolean = |
| 62 | + (sym.is(Module) || isLazyModuleVar(sym)) && |
| 63 | + dropped.contains(sym.info.resultType.typeSymbol) |
| 64 | + |
| 65 | + /** Tree should be dropped because it (is associated with) an empty |
| 66 | + * companion object. Such trees are |
| 67 | + * - module classes of empty companion objects |
| 68 | + * - definitions of lazy module variables or assignments to them. |
| 69 | + * - vals and getters for empty companion objects |
| 70 | + */ |
| 71 | + def toDrop(stat: Tree): Boolean = stat match { |
| 72 | + case stat: TypeDef => dropped.contains(stat.symbol) |
| 73 | + case stat: ValOrDefDef => symIsDropped(stat.symbol) |
| 74 | + case stat: Assign => symIsDropped(stat.lhs.symbol) |
| 75 | + case _ => false |
| 76 | + } |
| 77 | + |
| 78 | + def prune(tree: Tree): Tree = tree match { |
| 79 | + case tree @ TypeDef(name, impl @ Template(constr, _, _, _)) => |
| 80 | + cpy.TypeDef(tree)( |
| 81 | + rhs = cpy.Template(impl)( |
| 82 | + constr = cpy.DefDef(constr)(rhs = pruneLocals(constr.rhs)), |
| 83 | + body = pruneStats(impl.body))) |
| 84 | + case _ => |
| 85 | + tree |
| 86 | + } |
| 87 | + |
| 88 | + def pruneStats(stats: List[Tree]) = |
| 89 | + stats.filterConserve(!toDrop(_)).mapConserve(prune) |
| 90 | + |
| 91 | + def pruneLocals(expr: Tree) = expr match { |
| 92 | + case Block(stats, expr) => cpy.Block(expr)(pruneStats(stats), expr) |
| 93 | + case _ => expr |
| 94 | + } |
| 95 | + |
| 96 | + cpy.PackageDef(pdef)(pdef.pid, pruneStats(pdef.stats)) |
| 97 | + } |
| 98 | +} |
0 commit comments