|
| 1 | +package dotty.tools.dotc.transform |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.tpd._ |
| 4 | +import dotty.tools.dotc.core.Contexts.Context |
| 5 | +import dotty.tools.dotc.core.Flags._ |
| 6 | +import dotty.tools.dotc.core.Types.{Type, TypeRef} |
| 7 | +import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo} |
| 8 | + |
| 9 | +/** |
| 10 | + * Eliminates syntactic references to Java packages, so that there's no chance |
| 11 | + * they accidentally end up in the backend. |
| 12 | + */ |
| 13 | +class ElimJavaPackages extends MiniPhaseTransform { |
| 14 | + |
| 15 | + override def phaseName: String = "elimJavaPackages" |
| 16 | + |
| 17 | + override def transformSelect(tree: Select)(implicit ctx: Context, info: TransformerInfo): Tree = { |
| 18 | + if (isJavaPackage(tree)) { |
| 19 | + assert(tree.tpe.isInstanceOf[TypeRef], s"Expected tree with type TypeRef, but got ${tree.tpe.show}") |
| 20 | + Ident(tree.tpe.asInstanceOf[TypeRef]) |
| 21 | + } else { |
| 22 | + tree |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = { |
| 27 | + tree match { |
| 28 | + case tree: Select => |
| 29 | + assert(!isJavaPackage(tree), s"Unexpected reference to Java package in ${tree.show}") |
| 30 | + case _ => () |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Is the given tree a syntactic reference to a Java package? |
| 36 | + */ |
| 37 | + private def isJavaPackage(tree: Select)(implicit ctx: Context): Boolean = { |
| 38 | + tree.tpe match { |
| 39 | + case TypeRef(prefix, _) => |
| 40 | + val flags = prefix.termSymbol.flags |
| 41 | + // Testing for each flag separately is more efficient than using FlagConjunction. |
| 42 | + flags.is(Package) && flags.is(JavaDefined) |
| 43 | + case _ => false |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments