Skip to content

Use -from-tasty flag to enable/disable some phases #5609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions compiler/src/dotty/tools/backend/sjs/GenSJSIR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import Phases._
class GenSJSIR extends Phase {
def phaseName: String = "genSJSIR"

def run(implicit ctx: Context): Unit = {
if (ctx.settings.scalajs.value)
new JSCodeGen().run()
}
override def isRunnable(implicit ctx: Context): Boolean =
super.isRunnable && ctx.settings.scalajs.value

def run(implicit ctx: Context): Unit =
new JSCodeGen().run()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.tasty.file.TastyConsumer
class TastyFromClass(consumer: TastyConsumer) extends TASTYCompiler {

override protected def frontendPhases: List[List[Phase]] =
List(new ReadTastyTreesFromClasses) :: // Load classes from tasty
List(new ReadTasty) :: // Load classes from tasty
Nil

override protected def picklerPhases: List[List[Phase]] = Nil
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Phases.scala
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ object Phases {
final def isAfterTyper(phase: Phase): Boolean = phase.id > typerPhase.id
}

trait Phase {
abstract class Phase {

/** A name given to the `Phase` that can be used to debug the compiler. For
* instance, it is possible to print trees after a given phase using:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dotty.tools.dotc.core.Phases.Phase
class TASTYDecompiler extends TASTYCompiler {

override protected def frontendPhases: List[List[Phase]] =
List(new ReadTastyTreesFromClasses) :: // Load classes from tasty
List(new ReadTasty) :: // Load trees from TASTY files
Nil

override protected def picklerPhases: List[List[Phase]] = Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import Decorators._
import Contexts.Context
import Symbols.{Symbol, ClassSymbol}
import SymDenotations.ClassDenotation
import typer.FrontEnd
import NameOps._
import ast.Trees.Tree
import CompilationUnit.mkCompilationUnit
import Phases.Phase

class ReadTastyTreesFromClasses extends FrontEnd {

override def isTyper: Boolean = false
/** Load trees from TASTY files */
class ReadTasty extends Phase {

def phaseName: String = "readTasty"

override def isRunnable(implicit ctx: Context): Boolean =
ctx.settings.fromTasty.value

override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
units.flatMap(readTASTY(_)(ctx.addMode(Mode.ReadPositions)))
Expand Down Expand Up @@ -71,4 +76,6 @@ class ReadTastyTreesFromClasses extends FrontEnd {
case unit =>
Some(unit)
}

def run(implicit ctx: Context): Unit = unsupported("run")
}
5 changes: 1 addition & 4 deletions compiler/src/dotty/tools/dotc/fromtasty/TASTYCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import dotty.tools.dotc.transform._
class TASTYCompiler extends Compiler {

override protected def frontendPhases: List[List[Phase]] =
List(new ReadTastyTreesFromClasses) :: Nil

override protected def picklerPhases: List[List[Phase]] =
super.picklerPhases.map(_.filterNot(_.isInstanceOf[Pickler])) // No need to repickle
List(new ReadTasty) :: Nil

override def newRun(implicit ctx: Context): Run = {
reset()
Expand Down
11 changes: 6 additions & 5 deletions compiler/src/dotty/tools/dotc/quoted/QuoteCompiler.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package quoted

import dotty.tools.dotc.ast.tpd
Expand All @@ -14,7 +15,6 @@ import dotty.tools.dotc.core.Symbols.defn
import dotty.tools.dotc.core.Types.ExprType
import dotty.tools.dotc.core.quoted.PickledQuotes
import dotty.tools.dotc.transform.Staging
import dotty.tools.dotc.typer.FrontEnd
import dotty.tools.dotc.util.Positions.Position
import dotty.tools.dotc.util.SourceFile
import dotty.tools.io.{Path, VirtualFile}
Expand All @@ -40,10 +40,10 @@ class QuoteCompiler extends Compiler {
def outputClassName: TypeName = "Quoted".toTypeName

/** Frontend that receives a scala.quoted.Expr or scala.quoted.Type as input */
class QuotedFrontend(putInClass: Boolean) extends FrontEnd {
class QuotedFrontend(putInClass: Boolean) extends Phase {
import tpd._

override def isTyper: Boolean = false
def phaseName: String = "quotedFrontend"

override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = {
units.map {
Expand Down Expand Up @@ -80,6 +80,8 @@ class QuoteCompiler extends Compiler {
val classTree = ClassDef(cls, DefDef(cls.primaryConstructor.asTerm), run :: Nil)
PackageDef(ref(defn.RootPackage).asInstanceOf[Ident], classTree :: Nil).withPos(pos)
}

def run(implicit ctx: Context): Unit = unsupported("run")
}

class ExprRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) {
Expand All @@ -92,5 +94,4 @@ class QuoteCompiler extends Compiler {
compileUnits(units)
}
}

}
47 changes: 24 additions & 23 deletions compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ import scala.collection.mutable
class ExtractAPI extends Phase {
override def phaseName: String = "sbt-api"

override def isRunnable(implicit ctx: Context): Boolean = {
def forceRun = ctx.settings.YdumpSbtInc.value || ctx.settings.YforceSbtPhases.value
super.isRunnable && (ctx.sbtCallback != null || forceRun)
}

// SuperAccessors need to be part of the API (see the scripted test
// `trait-super` for an example where this matters), this is only the case
// after `PostTyper` (unlike `ExtractDependencies`, the simplication to trees
Expand All @@ -50,30 +55,26 @@ class ExtractAPI extends Phase {

override def run(implicit ctx: Context): Unit = {
val unit = ctx.compilationUnit
val dumpInc = ctx.settings.YdumpSbtInc.value
val forceRun = dumpInc || ctx.settings.YforceSbtPhases.value
if ((ctx.sbtCallback != null || forceRun) && !unit.isJava) {
val sourceFile = unit.source.file
if (ctx.sbtCallback != null)
ctx.sbtCallback.startSource(sourceFile.file)

val apiTraverser = new ExtractAPICollector
val classes = apiTraverser.apiSource(unit.tpdTree)
val mainClasses = apiTraverser.mainClasses

if (dumpInc) {
// Append to existing file that should have been created by ExtractDependencies
val pw = new PrintWriter(File(sourceFile.jpath).changeExtension("inc").toFile
.bufferedWriter(append = true), true)
try {
classes.foreach(source => pw.println(DefaultShowAPI(source)))
} finally pw.close()
}
val sourceFile = unit.source.file
if (ctx.sbtCallback != null)
ctx.sbtCallback.startSource(sourceFile.file)

val apiTraverser = new ExtractAPICollector
val classes = apiTraverser.apiSource(unit.tpdTree)
val mainClasses = apiTraverser.mainClasses

if (ctx.settings.YdumpSbtInc.value) {
// Append to existing file that should have been created by ExtractDependencies
val pw = new PrintWriter(File(sourceFile.jpath).changeExtension("inc").toFile
.bufferedWriter(append = true), true)
try {
classes.foreach(source => pw.println(DefaultShowAPI(source)))
} finally pw.close()
}

if (ctx.sbtCallback != null) {
classes.foreach(ctx.sbtCallback.api(sourceFile.file, _))
mainClasses.foreach(ctx.sbtCallback.mainClass(sourceFile.file, _))
}
if (ctx.sbtCallback != null) {
classes.foreach(ctx.sbtCallback.api(sourceFile.file, _))
mainClasses.foreach(ctx.sbtCallback.mainClass(sourceFile.file, _))
}
}
}
Expand Down
73 changes: 36 additions & 37 deletions compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,52 +49,51 @@ class ExtractDependencies extends Phase {

override def phaseName: String = "sbt-deps"

override def isRunnable(implicit ctx: Context): Boolean = {
def forceRun = ctx.settings.YdumpSbtInc.value || ctx.settings.YforceSbtPhases.value
super.isRunnable && (ctx.sbtCallback != null || forceRun)
}

// This phase should be run directly after `Frontend`, if it is run after
// `PostTyper`, some dependencies will be lost because trees get simplified.
// See the scripted test `constants` for an example where this matters.
// TODO: Add a `Phase#runsBefore` method ?

override def run(implicit ctx: Context): Unit = {
val unit = ctx.compilationUnit
val dumpInc = ctx.settings.YdumpSbtInc.value
val forceRun = dumpInc || ctx.settings.YforceSbtPhases.value
val shouldRun = !unit.isJava && (ctx.sbtCallback != null || forceRun)

if (shouldRun) {
val collector = new ExtractDependenciesCollector
collector.traverse(unit.tpdTree)

if (dumpInc) {
val deps = collector.dependencies.map(_.toString).toArray[Object]
val names = collector.usedNames.map { case (clazz, names) => s"$clazz: $names" }.toArray[Object]
Arrays.sort(deps)
Arrays.sort(names)

val pw = io.File(unit.source.file.jpath).changeExtension("inc").toFile.printWriter()
// val pw = Console.out
try {
pw.println("Used Names:")
pw.println("===========")
names.foreach(pw.println)
pw.println()
pw.println("Dependencies:")
pw.println("=============")
deps.foreach(pw.println)
} finally pw.close()
}

if (ctx.sbtCallback != null) {
collector.usedNames.foreach {
case (clazz, usedNames) =>
val className = classNameAsString(clazz)
usedNames.names.foreach {
case (usedName, scopes) =>
ctx.sbtCallback.usedName(className, usedName.toString, scopes)
}
}
val collector = new ExtractDependenciesCollector
collector.traverse(unit.tpdTree)

if (ctx.settings.YdumpSbtInc.value) {
val deps = collector.dependencies.map(_.toString).toArray[Object]
val names = collector.usedNames.map { case (clazz, names) => s"$clazz: $names" }.toArray[Object]
Arrays.sort(deps)
Arrays.sort(names)

val pw = io.File(unit.source.file.jpath).changeExtension("inc").toFile.printWriter()
// val pw = Console.out
try {
pw.println("Used Names:")
pw.println("===========")
names.foreach(pw.println)
pw.println()
pw.println("Dependencies:")
pw.println("=============")
deps.foreach(pw.println)
} finally pw.close()
}

collector.dependencies.foreach(recordDependency)
if (ctx.sbtCallback != null) {
collector.usedNames.foreach {
case (clazz, usedNames) =>
val className = classNameAsString(clazz)
usedNames.names.foreach {
case (usedName, scopes) =>
ctx.sbtCallback.usedName(className, usedName.toString, scopes)
}
}

collector.dependencies.foreach(recordDependency)
}
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/Pickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class Pickler extends Phase {

override def phaseName: String = Pickler.name

// No need to repickle trees comming from TASTY
override def isRunnable(implicit ctx: Context): Boolean =
super.isRunnable && !ctx.settings.fromTasty.value

private def output(name: String, msg: String) = {
val s = new PrintStream(name)
s.print(msg)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class TreeChecker extends Phase with SymTransformer {
def run(implicit ctx: Context): Unit = {
if (ctx.settings.YtestPickler.value && ctx.phase.prev.isInstanceOf[Pickler])
ctx.echo("Skipping Ycheck after pickling with -Ytest-pickler, the returned tree contains stale symbols")
else
else if (ctx.phase.prev.isCheckable)
check(ctx.base.allPhases, ctx)
}

Expand Down
9 changes: 3 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/FrontEnd.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package typer

import core._
Expand Down Expand Up @@ -90,11 +91,7 @@ class FrontEnd extends Phase {
unitContexts.map(_.compilationUnit).filterNot(discardAfterTyper)
}

override def run(implicit ctx: Context): Unit = {
parse
enterSyms
typeCheck
}
def run(implicit ctx: Context): Unit = unsupported("run")
}

object FrontEnd {
Expand Down
Loading