Skip to content

Commit 873058b

Browse files
committed
Allow custom output directory for class generated by run
1 parent f2a4126 commit 873058b

File tree

6 files changed

+41
-14
lines changed

6 files changed

+41
-14
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: 13 additions & 5 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
@@ -17,13 +17,21 @@ class QuoteDriver extends Driver {
1717
val ctx: Context = initCtx.fresh
1818
ctx.settings.optimise.update(settings.optimise)(ctx)
1919

20-
val outDir = new VirtualDirectory("(memory)", None)
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+
}
2128

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

2432
val classLoader = new AbstractFileClassLoader(outDir, this.getClass.getClassLoader)
2533

26-
val clazz = classLoader.loadClass(nme.QUOTE.toString)
34+
val clazz = classLoader.loadClass(driver.outputClassName.toString)
2735
val method = clazz.getMethod("apply")
2836
val instance = clazz.newInstance()
2937

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ object Runners {
3131
}
3232

3333
case class RunSettings(
34-
optimise: Boolean = false
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
3538
)
3639
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
}
66
foo
77
5
8+
89
foo
910
5

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import java.nio.file.{Files, Paths}
3+
24
import dotty.tools.dotc.quoted.Runners._
35

46
import scala.quoted._
@@ -11,7 +13,17 @@ object Test {
1113
2 + a
1214
}
1315
println(expr.show)
14-
println(run(expr.run, RunSettings(optimise = true)))
1516
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))
1628
}
1729
}

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)