@@ -12,13 +12,17 @@ import SymDenotations.SymDenotation
1212import config .Printers .inlining
1313import ErrorReporting .errorTree
1414import dotty .tools .dotc .util .{SourceFile , SourcePosition , SrcPos }
15+ import dotty .tools .dotc .transform .*
16+ import dotty .tools .dotc .transform .MegaPhase
17+ import dotty .tools .dotc .transform .MegaPhase .MiniPhase
1518import parsing .Parsers .Parser
1619import transform .{PostTyper , Inlining , CrossVersionChecks }
1720import staging .StagingLevel
1821
1922import collection .mutable
2023import reporting .{NotConstant , trace }
2124import util .Spans .Span
25+ import dotty .tools .dotc .core .Periods .PhaseId
2226
2327/** Support for querying inlineable methods and for inlining calls to such methods */
2428object Inlines :
@@ -343,10 +347,53 @@ object Inlines:
343347 if Inlines .isInlineable(codeArg1.symbol) then stripTyped(Inlines .inlineCall(codeArg1))
344348 else codeArg1
345349
350+ class MegaPhaseWithCustomPhaseId (miniPhases : Array [MiniPhase ], startId : PhaseId , endId : PhaseId )
351+ extends MegaPhase (miniPhases) {
352+ override def start : Int = startId
353+ override def end : Int = endId
354+ }
355+
356+ // Let's reconstruct necessary transform MegaPhases, without anything
357+ // that could cause problems here (like `CrossVersionChecks`).
358+ // The individiual lists here should line up with Compiler.scala, i.e
359+ // separate chunks there should also be kept separate here.
360+ // For now we create a single MegaPhase, since there does not seem to
361+ // be any important checks later (e.g. ForwardDepChecks could be applicable here,
362+ // but the equivalent is also not run in the scala 2's `ctx.typechecks`,
363+ // so let's leave it out for now).
364+ lazy val reconstructedTransformPhases =
365+ val transformPhases : List [List [(Class [? ], () => MiniPhase )]] = List (
366+ List (
367+ (classOf [InlineVals ], () => new InlineVals ),
368+ (classOf [ElimRepeated ], () => new ElimRepeated ),
369+ (classOf [RefChecks ], () => new RefChecks ),
370+ ),
371+ )
372+
373+ transformPhases.flatMap( (megaPhaseList : List [(Class [? ], () => MiniPhase )]) =>
374+ val (newMegaPhasePhases, phaseIds) =
375+ megaPhaseList.flatMap {
376+ case (filteredPhaseClass, miniphaseConstructor) =>
377+ ctx.base.phases
378+ .find(phase => filteredPhaseClass.isInstance(phase))
379+ .map(phase => (miniphaseConstructor(), phase.id))
380+ }
381+ .unzip
382+ if newMegaPhasePhases.isEmpty then None
383+ else Some (MegaPhaseWithCustomPhaseId (newMegaPhasePhases.toArray, phaseIds.head, phaseIds.last))
384+ )
385+
346386 ConstFold (underlyingCodeArg).tpe.widenTermRefExpr match {
347387 case ConstantType (Constant (code : String )) =>
348- val source2 = SourceFile .virtual(" tasty-reflect" , code)
349- inContext(ctx.fresh.setNewTyperState().setTyper(new Typer (ctx.nestingLevel + 1 )).setSource(source2)) {
388+ val unitName = " tasty-reflect"
389+ val source2 = SourceFile .virtual(unitName, code)
390+ // We need a dummy owner, as the actual one does not have a computed denotation yet,
391+ // but might be inspected in a transform phase, leading to cyclic errors
392+ val dummyOwner = newSymbol(ctx.owner, " $dummySymbol$" .toTermName, Private , defn.AnyType , NoSymbol )
393+ val newContext =
394+ ctx.fresh.setNewTyperState().setTyper(new Typer (ctx.nestingLevel + 1 )).setSource(source2)
395+
396+ inContext(newContext) {
350397 val tree2 = new Parser (source2).block()
351398 if ctx.reporter.allErrors.nonEmpty then
352399 ctx.reporter.allErrors.map((ErrorKind .Parser , _))
@@ -355,10 +402,23 @@ object Inlines:
355402 ctx.base.postTyperPhase match
356403 case postTyper : PostTyper if ctx.reporter.allErrors.isEmpty =>
357404 val tree4 = atPhase(postTyper) { postTyper.newTransformer.transform(tree3) }
358- ctx.base.inliningPhase match
359- case inlining : Inlining if ctx.reporter.allErrors.isEmpty =>
360- atPhase(inlining) { inlining.newTransformer.transform(tree4) }
361- case _ =>
405+ ctx.base.setRootTreePhase match
406+ case setRootTree =>
407+ val tree5 =
408+ val compilationUnit = CompilationUnit (unitName, code)
409+ compilationUnit.tpdTree = tree4
410+ compilationUnit.untpdTree = tree2
411+ var units = List (compilationUnit)
412+ atPhase(setRootTree)(setRootTree.runOn(units).head.tpdTree)
413+ ctx.base.inliningPhase match
414+ case inlining : Inlining if ctx.reporter.allErrors.isEmpty =>
415+ val tree6 = atPhase(inlining) { inlining.newTransformer.transform(tree5) }
416+ if ctx.reporter.allErrors.isEmpty && reconstructedTransformPhases.nonEmpty then
417+ var transformTree = tree6
418+ for phase <- reconstructedTransformPhases do
419+ if ctx.reporter.allErrors.isEmpty then
420+ transformTree = atPhase(phase.end + 1 )(phase.transformUnit(transformTree))
421+ case _ =>
362422 case _ =>
363423 ctx.reporter.allErrors.map((ErrorKind .Typer , _))
364424 }
0 commit comments