Skip to content

Commit a440779

Browse files
committed
wip
1 parent d82b651 commit a440779

File tree

8 files changed

+68
-60
lines changed

8 files changed

+68
-60
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.ast.tpd
44
import dotty.tools.dotc.Driver
5-
import dotty.tools.dotc.core.Contexts.Context
5+
import dotty.tools.dotc.core.Contexts.{Context, ContextBase}
66
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
77
import dotty.tools.repl.AbstractFileClassLoader
88

99
import scala.quoted.{Expr, Type}
1010
import java.net.URLClassLoader
1111

12-
import Toolbox.{Run, Settings, Show}
1312
import dotty.tools.dotc.tastyreflect.TastyImpl
1413

1514
class QuoteDriver extends Driver {
1615
import tpd._
1716

18-
def run[T](expr: Expr[T], settings: Settings[Run]): T = {
17+
private[this] val contextBase: ContextBase = new ContextBase
18+
19+
def run[T](expr: Expr[T], settings: ToolboxSettings): T = {
1920
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
2021

2122
val outDir: AbstractFile = settings.outDir match {
@@ -39,15 +40,15 @@ class QuoteDriver extends Driver {
3940
method.invoke(instance).asInstanceOf[T]
4041
}
4142

42-
def show(expr: Expr[_], settings: Settings[Show]): String = {
43+
def show(expr: Expr[_], settings: ToolboxSettings): String = {
4344
def show(tree: Tree, ctx: Context): String = {
4445
val tree1 = if (settings.rawTree) tree else (new TreeCleaner).transform(tree)(ctx)
4546
TastyImpl.showSourceCode.showTree(tree1)(ctx)
4647
}
4748
withTree(expr, show, settings)
4849
}
4950

50-
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: Settings[_]): T = {
51+
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: ToolboxSettings): T = {
5152
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
5253

5354
var output: Option[T] = None
@@ -59,7 +60,7 @@ class QuoteDriver extends Driver {
5960
output.getOrElse(throw new Exception("Could not extract " + expr))
6061
}
6162

62-
def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: Settings[_]): T = {
63+
def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: ToolboxSettings): T = {
6364
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
6465

6566
var output: Option[T] = None
@@ -72,7 +73,7 @@ class QuoteDriver extends Driver {
7273
}
7374

7475
override def initCtx: Context = {
75-
val ictx = super.initCtx.fresh
76+
val ictx = contextBase.initialCtx
7677
var classpath = System.getProperty("java.class.path")
7778
this.getClass.getClassLoader match {
7879
case cl: URLClassLoader =>

compiler/src/dotty/tools/dotc/quoted/Toolbox.scala

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,20 @@ import scala.runtime.quoted._
1010
object Toolbox {
1111
import tpd._
1212

13-
type Run
14-
type Show
13+
implicit def make(implicit settings: ToolboxSettings): Toolbox = new Toolbox {
1514

16-
implicit def toolbox[T](implicit
17-
runSettings: Settings[Run] = Settings.run(),
18-
showSettings: Settings[Show] = Settings.show()
19-
): Toolbox[T] = new Toolbox[T] {
15+
private[this] val driver: QuoteDriver = new QuoteDriver()
2016

21-
def run(expr: Expr[T]): T = expr match {
17+
def run[T](expr: Expr[T]): T = expr match {
2218
case expr: LiftedExpr[T] =>
2319
expr.value
2420
case expr: TastyTreeExpr[Tree] @unchecked =>
2521
throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from an inline macro argument.")
2622
case _ =>
27-
new QuoteDriver().run(expr, runSettings)
23+
driver.run(expr, settings)
2824
}
2925

30-
def show(expr: Expr[T]): String = new QuoteDriver().show(expr, showSettings)
26+
def show[T](expr: Expr[T]): String = driver.show(expr, settings)
3127

3228
}
33-
34-
class Settings[T] private (val outDir: Option[String], val rawTree: Boolean, val compilerArgs: List[String])
35-
36-
object Settings {
37-
38-
/** Quote run settings
39-
* @param optimise Enable optimisation when compiling the quoted code
40-
* @param outDir Output directory for the compiled quote. If set to None the output will be in memory
41-
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
42-
*/
43-
def run(
44-
optimise: Boolean = false,
45-
outDir: Option[String] = None,
46-
compilerArgs: List[String] = Nil
47-
): Settings[Run] = {
48-
var compilerArgs1 = compilerArgs
49-
if (optimise) compilerArgs1 = "-optimise" :: compilerArgs1
50-
new Settings(outDir, false, compilerArgs1)
51-
}
52-
53-
/** Quote show settings
54-
* @param color Print output with colors
55-
* @param rawTree Do not remove quote tree artifacts
56-
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
57-
*/
58-
def show(
59-
color: Boolean = false,
60-
rawTree: Boolean = false,
61-
compilerArgs: List[String] = Nil
62-
): Settings[Show] = {
63-
var compilerArgs1 = compilerArgs
64-
compilerArgs1 = s"-color:${if (color) "always" else "never"}" :: compilerArgs1
65-
new Settings(None, rawTree, compilerArgs1)
66-
}
67-
68-
}
69-
7029
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dotty.tools.dotc.quoted
2+
3+
class ToolboxSettings private (val outDir: Option[String], val rawTree: Boolean, val compilerArgs: List[String])
4+
5+
object ToolboxSettings {
6+
7+
implicit def default: ToolboxSettings = make()
8+
9+
/** Make toolbox settings
10+
* @param optimise Enable optimisation when compiling the quoted code
11+
* @param outDir Output directory for the compiled quote. If set to None the output will be in memory
12+
* @param color Print output with colors
13+
* @param rawTree Do not remove quote tree artifacts
14+
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
15+
*/
16+
def make(
17+
optimise: Boolean = false,
18+
color: Boolean = false,
19+
rawTree: Boolean = false,
20+
outDir: Option[String] = None,
21+
compilerArgs: List[String] = Nil
22+
): ToolboxSettings = {
23+
var compilerArgs1 = compilerArgs
24+
if (optimise) compilerArgs1 = "-optimise" :: compilerArgs1
25+
compilerArgs1 = s"-color:${if (color) "always" else "never"}" :: compilerArgs1
26+
new ToolboxSettings(outDir, rawTree, compilerArgs1)
27+
}
28+
29+
}

library/src/scala/quoted/Expr.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ sealed abstract class Expr[T] {
1010
*
1111
* May throw a FreeVariableError on expressions that came from an inline macro.
1212
*/
13-
final def run(implicit toolbox: Toolbox[T]): T = toolbox.run(this)
13+
final def run(implicit toolbox: Toolbox): T = toolbox.run(this)
1414

1515
/** Show a source code like representation of this expression */
16-
final def show(implicit toolbox: Toolbox[T]): String = toolbox.show(this)
16+
final def show(implicit toolbox: Toolbox): String = toolbox.show(this)
1717
}
1818

1919
object Expr {

library/src/scala/runtime/quoted/Toolbox.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.annotation.implicitNotFound
44
import scala.quoted.Expr
55

66
@implicitNotFound("Could not find implicit quoted.Toolbox. Default toolbox can be imported with `import dotty.tools.dotc.quoted.Toolbox._`")
7-
trait Toolbox[T] {
8-
def run(expr: Expr[T]): T
9-
def show(expr: Expr[T]): String
7+
trait Toolbox {
8+
def run[T](expr: Expr[T]): T
9+
def show[T](expr: Expr[T]): String
1010
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import scala.quoted._
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
implicit val toolbox: scala.runtime.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
6+
def expr(i: Int) = '{
7+
val a = 3 + ~i.toExpr
8+
2 + a
9+
}
10+
for (i <- 0 to 200) {
11+
val t0 = System.nanoTime()
12+
val res = expr(i).run
13+
val t = (System.nanoTime() - t0) / 1000000
14+
println(s"$i = $res in ${t}ms")
15+
}
16+
}
17+
}

tests/run-with-compiler/quote-run-with-settings.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import java.nio.file.{Files, Paths}
33

44
import dotty.tools.dotc.quoted.Toolbox._
5+
import dotty.tools.dotc.quoted.ToolboxSettings
56

67
import scala.quoted._
78

@@ -22,7 +23,7 @@ object Test {
2223
Files.deleteIfExists(classFile)
2324

2425
{
25-
implicit val settings = Settings.run(optimise = true, outDir = Some(outDir.toString))
26+
implicit val settings = ToolboxSettings.make(optimise = true, outDir = Some(outDir.toString))
2627
println(expr.run)
2728
assert(Files.exists(classFile))
2829
}

tests/run-with-compiler/quote-show-blocks-raw.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

22
import dotty.tools.dotc.quoted.Toolbox._
3+
import dotty.tools.dotc.quoted.ToolboxSettings
34

45
import scala.quoted._
56

67
object Test {
78
def main(args: Array[String]): Unit = {
8-
implicit val settings = Settings.show(rawTree = true)
9+
implicit val settings = ToolboxSettings.make(rawTree = true)
910

1011
def a(n: Int, x: Expr[Unit]): Expr[Unit] =
1112
if (n == 0) x

0 commit comments

Comments
 (0)