|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import dotty.tools.dotc.ast.Trees._ |
| 5 | +import dotty.tools.dotc.ast.{tpd, untpd} |
| 6 | +import dotty.tools.dotc.core.Contexts._ |
| 7 | +import dotty.tools.dotc.core.Decorators._ |
| 8 | +import dotty.tools.dotc.core.Flags._ |
| 9 | +import dotty.tools.dotc.core.Phases |
| 10 | +import dotty.tools.dotc.core.Symbols._ |
| 11 | +import dotty.tools.dotc.util.{SourceFile, SourcePosition} |
| 12 | + |
| 13 | +/** Ycheck inlined positions */ |
| 14 | +class YCheckPositions extends Phases.Phase { |
| 15 | + import tpd._ |
| 16 | + |
| 17 | + def phaseName: String = "inlinedPositions" |
| 18 | + |
| 19 | + override def run(implicit ctx: Context): Unit = () // YCheck only |
| 20 | + |
| 21 | + override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = { |
| 22 | + tree match { |
| 23 | + case PackageDef(pid, _) if tree.symbol.owner == defn.RootClass => |
| 24 | + new TreeTraverser { |
| 25 | + private[this] var sources: List[SourceFile] = ctx.source :: Nil |
| 26 | + def traverse(tree: tpd.Tree)(implicit ctx: Context): Unit = { |
| 27 | + |
| 28 | + // Check current context is correct |
| 29 | + assert(ctx.source == sources.head) |
| 30 | + if (!tree.isEmpty && !tree.isInstanceOf[untpd.TypedSplice] && ctx.typerState.isGlobalCommittable) { |
| 31 | + if (!tree.isType) { // TODO also check types, currently we do not add Inlined(EmptyTree, _, _) for types. We should. |
| 32 | + val currentSource = sources.head |
| 33 | + assert(tree.source == currentSource, i"wrong source set for $tree # ${tree.uniqueId} of ${tree.getClass}, set to ${tree.source} but context had $currentSource") |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // Recursivlely check children while keeping track of current source |
| 38 | + tree match { |
| 39 | + case Inlined(EmptyTree, bindings, expansion) => |
| 40 | + assert(bindings.isEmpty) |
| 41 | + val old = sources |
| 42 | + sources = old.tail |
| 43 | + traverse(expansion)(inlineContext(EmptyTree)) |
| 44 | + sources = old |
| 45 | + case Inlined(call, bindings, expansion) => |
| 46 | + bindings.foreach(traverse(_)) |
| 47 | + sources = call.symbol.topLevelClass.source :: sources |
| 48 | + if ( |
| 49 | + !( // FIXME macro implementations can drop Inlined nodes. We should reinsert them after macro expansion based on the positions of the trees |
| 50 | + ((ctx.phase <= ctx.typerPhase.next) && call.symbol.is(Macro)) || |
| 51 | + (!(ctx.phase <= ctx.typerPhase.next) && call.symbol.unforcedDecls.exists(_.is(Macro)) || call.symbol.unforcedDecls.toList.exists(_.is(Macro))) |
| 52 | + ) |
| 53 | + ) traverse(expansion)(inlineContext(call)) |
| 54 | + sources = sources.tail |
| 55 | + case _ => traverseChildren(tree) |
| 56 | + } |
| 57 | + } |
| 58 | + }.traverse(tree) |
| 59 | + case _ => |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments