|
| 1 | +package dotty.tools.dotc.quoted |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.tpd |
| 4 | +import dotty.tools.dotc.core.Contexts.Context |
| 5 | +import dotty.tools.dotc.core.DenotTransformers.SymTransformer |
| 6 | +import dotty.tools.dotc.core.Flags._ |
| 7 | +import dotty.tools.dotc.core.NameKinds.{NumberedInfo, UniqueName} |
| 8 | +import dotty.tools.dotc.core.SymDenotations.SymDenotation |
| 9 | +import dotty.tools.dotc.transform.MegaPhase.MiniPhase |
| 10 | + |
| 11 | +/** Refreshes local names starting from the second use of the name. Intended for readability of the pretty printed code. */ |
| 12 | +class RefreshNames extends MiniPhase with SymTransformer { |
| 13 | + |
| 14 | + def phaseName: String = "RefreshNames" |
| 15 | + |
| 16 | + override def transformValDef(tree: tpd.ValDef)(implicit ctx: Context): tpd.Tree = |
| 17 | + tpd.ValDef(tree.symbol.asTerm, tree.rhs) |
| 18 | + |
| 19 | + override def transformDefDef(tree: tpd.DefDef)(implicit ctx: Context): tpd.Tree = |
| 20 | + tpd.DefDef(tree.symbol.asTerm, tree.rhs) |
| 21 | + |
| 22 | + override def transformTypeDef(tree: tpd.TypeDef)(implicit ctx: Context): tpd.Tree = { |
| 23 | + val newTypeDef = tpd.TypeDef(tree.symbol.asType) |
| 24 | + // keep rhs to keep `type T = ...` instead of `type T >: ... <: ...` |
| 25 | + cpy.TypeDef(newTypeDef)(rhs = tree.rhs) |
| 26 | + } |
| 27 | + |
| 28 | + def transformSym(ref: SymDenotation)(implicit ctx: Context): SymDenotation = { |
| 29 | + if (ref.is(Package) || ref.isClass || ref.owner != ctx.owner || ref.is(Label) || ref.is(Param)) ref |
| 30 | + else { |
| 31 | + val newName = UniqueName.fresh(ref.symbol.name.toTermName) |
| 32 | + newName.info match { |
| 33 | + case info: NumberedInfo if info.num == 1 => ref // Keep the first reference as is to avoid renaming if the code has no duplicated names |
| 34 | + case _ => ref.copySymDenotation(name = if (ref.symbol.isType) newName.toTypeName else newName) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +} |
0 commit comments