|
| 1 | +package dotty.tools.dotc.transform |
| 2 | + |
| 3 | +import dotty.tools.dotc.core.Contexts.Context |
| 4 | +import dotty.tools.dotc.core.Decorators._ |
| 5 | +import dotty.tools.dotc.core.DenotTransformers.SymTransformer |
| 6 | +import dotty.tools.dotc.core.Flags._ |
| 7 | +import dotty.tools.dotc.core.NameKinds._ |
| 8 | +import dotty.tools.dotc.core.Names._ |
| 9 | +import dotty.tools.dotc.core.Phases |
| 10 | +import dotty.tools.dotc.core.SymDenotations.SymDenotation |
| 11 | +import dotty.tools.dotc.core.Symbols._ |
| 12 | +import dotty.tools.dotc.transform.TreeTransforms.MiniPhaseTransform |
| 13 | + |
| 14 | +/** Renames lifted classes to local numbering scheme */ |
| 15 | +class RenameLifted extends MiniPhaseTransform with SymTransformer { thisTransformer => |
| 16 | + |
| 17 | + override def phaseName = "renameLifted" |
| 18 | + |
| 19 | + override def runsAfterGroupsOf: Set[Class[_ <: Phases.Phase]] = Set(classOf[RestoreScopes]) |
| 20 | + |
| 21 | + def transformSym(ref: SymDenotation)(implicit ctx: Context): SymDenotation = |
| 22 | + if (needsRefresh(ref.symbol)) ref.copySymDenotation(name = refreshedName(ref.symbol)) |
| 23 | + else ref |
| 24 | + |
| 25 | + /** If the name of the symbol with a unique name needs to be refreshed |
| 26 | + * - if it is a lifted class |
| 27 | + * - if it is a lifted method |
| 28 | + */ |
| 29 | + private def needsRefresh(sym: Symbol)(implicit ctx: Context): Boolean = |
| 30 | + (sym.isClass || sym.is(Private | Method | JavaStatic)) && sym.name.is(UniqueName) |
| 31 | + |
| 32 | + /** Refreshes the number of the name based on the full name of the symbol */ |
| 33 | + private def refreshedName(sym: Symbol)(implicit ctx: Context): Name = { |
| 34 | + def rewriteUnique: PartialFunction[Name, Name] = { |
| 35 | + case name: DerivedName if name.info.kind == UniqueName => |
| 36 | + val fullName = (sym.owner.fullName.toString + name.underlying).toTermName |
| 37 | + val freshName = UniqueName.fresh(fullName) |
| 38 | + val info = freshName.asInstanceOf[DerivedName].info |
| 39 | + DerivedName(name.underlying.rewrite(rewriteUnique), info) |
| 40 | + case DerivedName(underlying, info: QualifiedInfo) => |
| 41 | + underlying.rewrite(rewriteUnique).derived(info) |
| 42 | + } |
| 43 | + |
| 44 | + sym.name.rewrite(rewriteUnique) |
| 45 | + } |
| 46 | +} |
0 commit comments