@@ -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 :
@@ -345,10 +349,58 @@ object Inlines:
345349 // We should not be rewriting tested strings
346350 val noRewriteSettings = ctx.settings.rewrite.updateIn(ctx.settingsState.reinitializedCopy(), None )
347351
352+ class MegaPhaseWithCustomPhaseId (miniPhases : Array [MiniPhase ], startId : PhaseId , endId : PhaseId )
353+ extends MegaPhase (miniPhases) {
354+ override def start : Int = startId
355+ override def end : Int = endId
356+ }
357+
348358 ConstFold (underlyingCodeArg).tpe.widenTermRefExpr match {
349359 case ConstantType (Constant (code : String )) =>
350- val source2 = SourceFile .virtual(" tasty-reflect" , code)
351- inContext(ctx.fresh.setSettings(noRewriteSettings).setNewTyperState().setTyper(new Typer (ctx.nestingLevel + 1 )).setSource(source2)) {
360+ val unitName = " tasty-reflect"
361+ val source2 = SourceFile .virtual(unitName, code)
362+ // We need a dummy owner, as the actual one does not have a computed denotation yet,
363+ // but might be inspected in a transform phase, leading to cyclic errors
364+ val dummyOwner = newSymbol(ctx.owner, " $dummySymbol$" .toTermName, Private , defn.AnyType , NoSymbol )
365+ val newContext =
366+ ctx.fresh
367+ .setSettings(noRewriteSettings)
368+ .setNewTyperState()
369+ .setTyper(new Typer (ctx.nestingLevel + 1 ))
370+ .setSource(source2)
371+ .withOwner(dummyOwner)
372+
373+ inContext(newContext) {
374+ // Let's reconstruct necessary transform MegaPhases, without anything
375+ // that could cause problems here (like `CrossVersionChecks`).
376+ // The individiual lists here should line up with Compiler.scala, i.e
377+ // separate chunks there should also be kept separate here.
378+ // For now we create a single MegaPhase, since there does not seem to
379+ // be any important checks later (e.g. ForwardDepChecks could be applicable here,
380+ // but the equivalent is also not run in the scala 2's `ctx.typechecks`,
381+ // so let's leave it out for now).
382+ val transformPhases : List [List [(Class [? ], () => MiniPhase )]] = List (
383+ List (
384+ (classOf [InlineVals ], () => new InlineVals ),
385+ (classOf [ElimRepeated ], () => new ElimRepeated ),
386+ (classOf [RefChecks ], () => new RefChecks ),
387+ ),
388+ )
389+
390+ val mergedTransformPhases =
391+ transformPhases.flatMap( (megaPhaseList : List [(Class [? ], () => MiniPhase )]) =>
392+ val (newMegaPhasePhases, phaseIds) =
393+ megaPhaseList
394+ .flatMap { filteredPhase =>
395+ ctx.base.phases.find(phase => filteredPhase._1.isInstance(phase)).map { a =>
396+ (filteredPhase._2(), a.id)
397+ }
398+ }
399+ .unzip
400+ if newMegaPhasePhases.isEmpty then None
401+ else Some (MegaPhaseWithCustomPhaseId (newMegaPhasePhases.toArray, phaseIds.head, phaseIds.last))
402+ )
403+
352404 val tree2 = new Parser (source2).block()
353405 if ctx.reporter.allErrors.nonEmpty then
354406 ctx.reporter.allErrors.map((ErrorKind .Parser , _))
@@ -357,10 +409,24 @@ object Inlines:
357409 ctx.base.postTyperPhase match
358410 case postTyper : PostTyper if ctx.reporter.allErrors.isEmpty =>
359411 val tree4 = atPhase(postTyper) { postTyper.newTransformer.transform(tree3) }
360- ctx.base.inliningPhase match
361- case inlining : Inlining if ctx.reporter.allErrors.isEmpty =>
362- atPhase(inlining) { inlining.newTransformer.transform(tree4) }
363- case _ =>
412+ ctx.base.setRootTreePhase match
413+ case setRootTree =>
414+ val tree5 =
415+ val compilationUnit = CompilationUnit (unitName, code)
416+ compilationUnit.tpdTree = tree4
417+ compilationUnit.untpdTree = tree2
418+ var units = List (compilationUnit)
419+ atPhase(setRootTree)(setRootTree.runOn(units).head.tpdTree)
420+
421+ ctx.base.inliningPhase match
422+ case inlining : Inlining if ctx.reporter.allErrors.isEmpty =>
423+ val tree6 = atPhase(inlining) { inlining.newTransformer.transform(tree5) }
424+ if mergedTransformPhases.nonEmpty then
425+ var transformTree = tree6
426+ for (phase <- mergedTransformPhases if ctx.reporter.allErrors.isEmpty) {
427+ transformTree = atPhase(phase.end + 1 )(phase.transformUnit(transformTree))
428+ }
429+ case _ =>
364430 case _ =>
365431 ctx.reporter.allErrors.map((ErrorKind .Typer , _))
366432 }
0 commit comments