|
| 1 | +package dotty.tools.repl.transform |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.tpd |
| 4 | +import dotty.tools.dotc.ast.untpd |
| 5 | +import dotty.tools.dotc.core.Contexts._ |
| 6 | +import dotty.tools.dotc.core.Names.Name |
| 7 | +import dotty.tools.dotc.core.Phases.Phase |
| 8 | +import dotty.tools.dotc.core.StdNames.ScalaTermNames |
| 9 | + |
| 10 | + |
| 11 | +/** This phase collects and transforms top-level Import trees to handle definition shadowing. |
| 12 | + * |
| 13 | + * This is used by repl to handle new run contexts and allowing |
| 14 | + * definitions to be shadowed by imports in the same run. |
| 15 | + * |
| 16 | + * Import transformation is necessary for excluding its members when they are shadowed in the same run. |
| 17 | + * This is done by finding all members defined after the Import clause calculating |
| 18 | + * their intersection with available members from selectors |
| 19 | + * |
| 20 | + * This step is necessary for proper new run initialization since we need to import the previous run |
| 21 | + * into Context. It is accomplished in the following order: |
| 22 | + * 1. Previous wrapper object for a given run |
| 23 | + * 2. Previous imports for a given run |
| 24 | + * |
| 25 | + * This phase uses typed trees thus after the Typer phase. |
| 26 | + */ |
| 27 | +class CollectTopLevelImports extends Phase { |
| 28 | + import tpd._ |
| 29 | + |
| 30 | + def phaseName: String = "collectTopLevelImports" |
| 31 | + |
| 32 | + private var gatheredImports: List[Import] = _ |
| 33 | + |
| 34 | + def imports: List[Import] = gatheredImports |
| 35 | + |
| 36 | + def run(using Context): Unit = |
| 37 | + val PackageDef(_, _ :: TypeDef(_, rhs: Template) :: _) = ctx.compilationUnit.tpdTree: @unchecked |
| 38 | + gatheredImports = transformTopLevelImports(rhs.body) |
| 39 | + |
| 40 | + /** Transforms top-level imports to exclude intersecting members declared after the Import clause. |
| 41 | + * To properly handle imports such as: `import A.f; def f = 3` consequently making sure that original selectors are |
| 42 | + * filtered to eliminate potential duplications that would result in compilation error. |
| 43 | + */ |
| 44 | + private def transformTopLevelImports(trees: List[Tree])(using Context): List[Import] = |
| 45 | + val definitions = collectTopLevelMemberDefs(trees) |
| 46 | + |
| 47 | + trees.collect { |
| 48 | + case tree @ Import(expr, selectors) => |
| 49 | + val definitionsAfterImport = definitions.filter(_._2 > tree.endPos.end).map(_._1) |
| 50 | + val membersIntersection = expr.tpe.allMembers.map(_.name).intersect(definitionsAfterImport) |
| 51 | + |
| 52 | + val transformedSelectors = membersIntersection.map(collidingMember => { |
| 53 | + untpd.ImportSelector(untpd.Ident(collidingMember), untpd.Ident(CollectTopLevelImports.nme.WILDCARD)) |
| 54 | + }).toList |
| 55 | + |
| 56 | + val filteredSelectors = selectors.filterNot(importSelector => membersIntersection.contains(importSelector.imported.name)) |
| 57 | + |
| 58 | + Import(expr, transformedSelectors.toList ::: filteredSelectors) |
| 59 | + } |
| 60 | + |
| 61 | + private def collectTopLevelMemberDefs(trees: List[Tree])(using Context): List[(Name, Int)] = |
| 62 | + trees.collect { |
| 63 | + case tree: ValDef => tree.name -> tree.endPos.end |
| 64 | + case tree: DefDef => tree.name -> tree.endPos.end |
| 65 | + case tree: TypeDef => tree.name -> tree.endPos.end |
| 66 | + } |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +object CollectTopLevelImports { |
| 71 | + private lazy val nme: ScalaTermNames = new ScalaTermNames |
| 72 | +} |
| 73 | + |
0 commit comments