Skip to content

Commit 9a654d7

Browse files
Merge pull request #3830 from dotty-staging/enable-optimise-in-quotes
Enable optimisations in `Expr.run`
2 parents d345609 + 873058b commit 9a654d7

File tree

6 files changed

+78
-20
lines changed

6 files changed

+78
-20
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package quoted
33

44
import dotty.tools.backend.jvm.GenBCode
55
import dotty.tools.dotc.ast.tpd
6-
76
import dotty.tools.dotc.core.Contexts.Context
8-
import dotty.tools.dotc.core.Flags.{EmptyFlags, Method}
9-
import dotty.tools.dotc.core.{Mode, Phases}
7+
import dotty.tools.dotc.core.Decorators._
8+
import dotty.tools.dotc.core.Flags._
9+
import dotty.tools.dotc.core.Mode
10+
import dotty.tools.dotc.core.Names.TypeName
1011
import dotty.tools.dotc.core.Phases.Phase
1112
import dotty.tools.dotc.core.Scopes.{EmptyScope, newScope}
1213
import dotty.tools.dotc.core.StdNames.nme
@@ -17,14 +18,14 @@ import dotty.tools.dotc.transform.ReifyQuotes
1718
import dotty.tools.dotc.typer.FrontEnd
1819
import dotty.tools.dotc.util.Positions.Position
1920
import dotty.tools.dotc.util.SourceFile
20-
import dotty.tools.io.{Path, PlainFile, VirtualDirectory}
21+
import dotty.tools.io.{AbstractFile, Path, PlainFile}
2122

2223
import scala.quoted.Expr
2324

2425
/** Compiler that takes the contents of a quoted expression `expr` and produces
2526
* a class file with `class ' { def apply: Object = expr }`.
2627
*/
27-
class ExprCompiler(directory: VirtualDirectory) extends Compiler {
28+
class ExprCompiler(directory: AbstractFile) extends Compiler {
2829
import tpd._
2930

3031
/** A GenBCode phase that outputs to a virtual directory */
@@ -47,6 +48,8 @@ class ExprCompiler(directory: VirtualDirectory) extends Compiler {
4748
new ExprRun(this, ctx.addMode(Mode.ReadPositions))
4849
}
4950

51+
def outputClassName: TypeName = "Quoted".toTypeName
52+
5053
/** Frontend that receives scala.quoted.Expr as input */
5154
class ExprFrontend(putInClass: Boolean) extends FrontEnd {
5255
import tpd._
@@ -72,7 +75,7 @@ class ExprCompiler(directory: VirtualDirectory) extends Compiler {
7275
val pos = Position(0)
7376
val assocFile = new PlainFile(Path("<quote>"))
7477

75-
val cls = ctx.newCompleteClassSymbol(defn.RootClass, nme.QUOTE.toTypeName, EmptyFlags,
78+
val cls = ctx.newCompleteClassSymbol(defn.RootClass, outputClassName, EmptyFlags,
7679
defn.ObjectType :: Nil, newScope, coord = pos, assocFile = assocFile).entered.asClass
7780
cls.enter(ctx.newDefaultConstructor(cls), EmptyScope)
7881
val meth = ctx.newSymbol(cls, nme.apply, Method, ExprType(defn.AnyType), coord = pos).entered

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.Driver
4-
import dotty.tools.dotc.core.Contexts.{Context, FreshContext}
4+
import dotty.tools.dotc.core.Contexts.Context
55
import dotty.tools.dotc.core.StdNames._
6-
import dotty.tools.io.VirtualDirectory
6+
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
77
import dotty.tools.repl.AbstractFileClassLoader
88

99
import scala.quoted.Expr
@@ -13,18 +13,25 @@ import java.nio.charset.StandardCharsets
1313

1414
class QuoteDriver extends Driver {
1515

16-
def run[T](expr: Expr[T]): T = {
16+
def run[T](expr: Expr[T], settings: Runners.RunSettings): T = {
1717
val ctx: Context = initCtx.fresh
18-
// TODO enable optimisation?
19-
// ctx.settings.optimise.update(true)(ctx)
20-
21-
val outDir = new VirtualDirectory("(memory)", None)
18+
ctx.settings.optimise.update(settings.optimise)(ctx)
19+
20+
val outDir: AbstractFile = settings.outDir match {
21+
case Some(out) =>
22+
val dir = Directory(out)
23+
dir.createDirectory()
24+
new PlainDirectory(Directory(out))
25+
case None =>
26+
new VirtualDirectory("(memory)", None)
27+
}
2228

23-
new ExprCompiler(outDir).newRun(ctx).compileExpr(expr)
29+
val driver = new ExprCompiler(outDir)
30+
driver.newRun(ctx).compileExpr(expr)
2431

2532
val classLoader = new AbstractFileClassLoader(outDir, this.getClass.getClassLoader)
2633

27-
val clazz = classLoader.loadClass(nme.QUOTE.toString)
34+
val clazz = classLoader.loadClass(driver.outputClassName.toString)
2835
val method = clazz.getMethod("apply")
2936
val instance = clazz.newInstance()
3037

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ object Runners {
1313

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

16-
def run(expr: Expr[T]): T = expr match {
17-
case expr: ConstantExpr[T] => expr.value
18-
case _ => new QuoteDriver().run(expr)
19-
}
16+
def run(expr: Expr[T]): T = Runners.run(expr, RunSettings())
2017

2118
def show(expr: Expr[T]): String = expr match {
2219
case expr: ConstantExpr[T] =>
@@ -27,4 +24,16 @@ object Runners {
2724
case _ => new QuoteDriver().show(expr)
2825
}
2926
}
27+
28+
def run[T](expr: Expr[T], settings: RunSettings): T = expr match {
29+
case expr: ConstantExpr[T] => expr.value
30+
case _ => new QuoteDriver().run(expr, settings)
31+
}
32+
33+
case class RunSettings(
34+
/** Enable optimisation when compiling the quoted code */
35+
optimise: Boolean = false,
36+
/** Output directory for the copiled quote. If set to None the output will be in memory */
37+
outDir: Option[String] = None
38+
)
3039
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
val a: Int = 3
3+
println("foo")
4+
2.+(a)
5+
}
6+
foo
7+
5
8+
9+
foo
10+
5
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
import java.nio.file.{Files, Paths}
3+
4+
import dotty.tools.dotc.quoted.Runners._
5+
6+
import scala.quoted._
7+
8+
object Test {
9+
def main(args: Array[String]): Unit = {
10+
val expr = '{
11+
val a = 3
12+
println("foo")
13+
2 + a
14+
}
15+
println(expr.show)
16+
println(expr.run)
17+
println()
18+
19+
val outDir = Paths.get("../out/out-quoted-1")
20+
val classFile = outDir.resolve("Quoted.class")
21+
22+
Files.deleteIfExists(classFile)
23+
24+
val settings = RunSettings(optimise = true, outDir = Some(outDir.toString))
25+
26+
println(run(expr, settings))
27+
assert(Files.exists(classFile))
28+
}
29+
}

tests/run-with-compiler/quote-run.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ lambda(5)
1313
Foo
1414
false
1515
Bar
16-
class '$A$1
16+
class Quoted$A$1

0 commit comments

Comments
 (0)