Skip to content

Fix #3914: Add compiler args to quote run and show Settings #3972

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 2 commits into from
Feb 12, 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
19 changes: 10 additions & 9 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import scala.quoted.Expr

import java.net.URLClassLoader

import Runners.{Settings, Run, Show}

class QuoteDriver extends Driver {
import tpd._

def run[T](expr: Expr[T], settings: Runners.RunSettings): T = {
val ctx: Context = initCtx.fresh
ctx.settings.optimise.update(settings.optimise)(ctx)
def run[T](expr: Expr[T], settings: Settings[Run]): T = {
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummy.scala?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setup expects some input file. But it is not required here. Hence I add a dummy file name that is then ignored. It is the _ in val (_, ctx: Context) =


val outDir: AbstractFile = settings.outDir match {
case Some(out) =>
Expand All @@ -39,25 +40,25 @@ class QuoteDriver extends Driver {
method.invoke(instance).asInstanceOf[T]
}

def show(expr: Expr[_]): String = {
def show(expr: Expr[_], settings: Settings[Show]): String = {
def show(tree: Tree, ctx: Context): String = {
val printer = new DecompilerPrinter(ctx)
val pageWidth = ctx.settings.pageWidth.value(ctx)
printer.toText(tree).mkString(pageWidth, false)
}
withTree(expr, show)
withTree(expr, show, settings)
}

def withTree[T](expr: Expr[_], f: (Tree, Context) => T): T = {
val ctx: Context = initCtx.fresh
ctx.settings.color.update("never")(ctx) // TODO support colored show
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: Settings[_]): T = {
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's dummy.scala?


var output: Option[T] = None
def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {
assert(output.isEmpty)
output = Some(f(tree, ctx))
}
new ExprDecompiler(registerTree).newRun(ctx).compileExpr(expr)
output.getOrElse(throw new Exception("Could not extact " + expr))
output.getOrElse(throw new Exception("Could not extract " + expr))
}

override def initCtx: Context = {
Expand Down
69 changes: 51 additions & 18 deletions compiler/src/dotty/tools/dotc/quoted/Runners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ import scala.runtime.quoted._
object Runners {
import tpd._

type Run
type Show

implicit def runner[T]: Runner[T] = new Runner[T] {

def run(expr: Expr[T]): T = Runners.run(expr, RunSettings())
def run(expr: Expr[T]): T = Runners.run(expr, Settings.run())

def show(expr: Expr[T]): String = expr match {
case expr: ConstantExpr[T] =>
implicit val ctx = new QuoteDriver().initCtx
ctx.settings.color.update("never")
val printer = new RefinedPrinter(ctx)
if (expr.value == BoxedUnit.UNIT) "()"
else printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false)
case _ => new QuoteDriver().show(expr)
}
def show(expr: Expr[T]): String = Runners.show(expr, Settings.show())

def toConstantOpt(expr: Expr[T]): Option[T] = {
def toConstantOpt(tree: Tree): Option[T] = tree match {
Expand All @@ -37,21 +32,59 @@ object Runners {
}
expr match {
case expr: ConstantExpr[T] => Some(expr.value)
case _ => new QuoteDriver().withTree(expr, (tree, _) => toConstantOpt(tree))
case _ => new QuoteDriver().withTree(expr, (tree, _) => toConstantOpt(tree), Settings.run())
}
}

}

def run[T](expr: Expr[T], settings: RunSettings): T = expr match {
def run[T](expr: Expr[T], settings: Settings[Run]): T = expr match {
case expr: ConstantExpr[T] => expr.value
case _ => new QuoteDriver().run(expr, settings)
}

case class RunSettings(
/** Enable optimisation when compiling the quoted code */
optimise: Boolean = false,
/** Output directory for the copiled quote. If set to None the output will be in memory */
outDir: Option[String] = None
)
def show[T](expr: Expr[T], settings: Settings[Show]): String = expr match {
case expr: ConstantExpr[T] =>
implicit val ctx = new QuoteDriver().initCtx
if (settings.compilerArgs.contains("-color:never"))
ctx.settings.color.update("never")
val printer = new RefinedPrinter(ctx)
if (expr.value == BoxedUnit.UNIT) "()"
else printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false)
case _ => new QuoteDriver().show(expr, settings)
}

class Settings[T] private (val outDir: Option[String], val compilerArgs: List[String])

object Settings {

/** Quote run settings
* @param optimise Enable optimisation when compiling the quoted code
* @param outDir Output directory for the compiled quote. If set to None the output will be in memory
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
*/
def run(
optimise: Boolean = false,
outDir: Option[String] = None,
compilerArgs: List[String] = Nil
): Settings[Run] = {
var compilerArgs1 = compilerArgs
if (optimise) compilerArgs1 = "-optimise" :: compilerArgs1
new Settings(outDir, compilerArgs1)
}

/** Quote show settings
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
*/
def show(
color: Boolean = false,
compilerArgs: List[String] = Nil
): Settings[Show] = {
var compilerArgs1 = compilerArgs
compilerArgs1 = s"-color:${if (color) "always" else "never"}" :: compilerArgs1
new Settings(None, compilerArgs1)
}

}

}
2 changes: 1 addition & 1 deletion tests/run-with-compiler/quote-run-with-settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object Test {

Files.deleteIfExists(classFile)

val settings = RunSettings(optimise = true, outDir = Some(outDir.toString))
val settings = Settings.run(optimise = true, outDir = Some(outDir.toString))

println(run(expr, settings))
assert(Files.exists(classFile))
Expand Down