Skip to content

Commit 168e90e

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

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ 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
1010
import dotty.tools.dotc.core.Phases.Phase
1111
import dotty.tools.dotc.core.Scopes.{EmptyScope, newScope}
1212
import dotty.tools.dotc.core.StdNames.nme
@@ -17,14 +17,14 @@ import dotty.tools.dotc.transform.ReifyQuotes
1717
import dotty.tools.dotc.typer.FrontEnd
1818
import dotty.tools.dotc.util.Positions.Position
1919
import dotty.tools.dotc.util.SourceFile
20-
import dotty.tools.io.{Path, PlainFile, VirtualDirectory}
20+
import dotty.tools.io.{AbstractFile, Path, PlainFile}
2121

2222
import scala.quoted.Expr
2323

2424
/** Compiler that takes the contents of a quoted expression `expr` and produces
2525
* a class file with `class ' { def apply: Object = expr }`.
2626
*/
27-
class ExprCompiler(directory: VirtualDirectory) extends Compiler {
27+
class ExprCompiler(directory: AbstractFile) extends Compiler {
2828
import tpd._
2929

3030
/** A GenBCode phase that outputs to a virtual directory */
@@ -72,7 +72,7 @@ class ExprCompiler(directory: VirtualDirectory) extends Compiler {
7272
val pos = Position(0)
7373
val assocFile = new PlainFile(Path("<quote>"))
7474

75-
val cls = ctx.newCompleteClassSymbol(defn.RootClass, nme.QUOTE.toTypeName, EmptyFlags,
75+
val cls = ctx.newCompleteClassSymbol(defn.RootClass, "Quoted".toTypeName, EmptyFlags,
7676
defn.ObjectType :: Nil, newScope, coord = pos, assocFile = assocFile).entered.asClass
7777
cls.enter(ctx.newDefaultConstructor(cls), EmptyScope)
7878
val meth = ctx.newSymbol(cls, nme.apply, Method, ExprType(defn.AnyType), coord = pos).entered

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

Lines changed: 11 additions & 4 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,20 @@ 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

2229
new ExprCompiler(outDir).newRun(ctx).compileExpr(expr)
2330

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

26-
val clazz = classLoader.loadClass(nme.QUOTE.toString)
33+
val clazz = classLoader.loadClass("Quoted")
2734
val method = clazz.getMethod("apply")
2835
val instance = clazz.newInstance()
2936

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

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

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

16-
def run(expr: Expr[T]): T = Runners.run(expr, RunSettings())
16+
def run(expr: Expr[T]): T = Runners.run(expr, RunSettings(outDir = Some("aa")))
1717

1818
def show(expr: Expr[T]): String = expr match {
1919
case expr: ConstantExpr[T] =>
@@ -31,6 +31,7 @@ object Runners {
3131
}
3232

3333
case class RunSettings(
34-
optimise: Boolean = false
34+
optimise: Boolean = false,
35+
outDir: Option[String] = None
3536
)
3637
}

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
}

0 commit comments

Comments
 (0)