-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement Expr.{run|show} #3685
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
db47f14
Fix compiling quote in file with no splices
nicolasstucki 385a87f
Implement run/show for quoted expressions
nicolasstucki 3df0b19
Simplify compiler classpath for dotr
nicolasstucki 3e5625c
Add -with-compiler to dotc
nicolasstucki 9e54e73
Make staged interpreter a run test
nicolasstucki 7d9c6e4
Simplify Runners
nicolasstucki efff78b
Avoid running the compiler for ConstantExpr
nicolasstucki bb5b114
Fix typo
nicolasstucki 1cfc136
Update quote.Expr API in docs
nicolasstucki 98c7228
Fix format of ConstantExpr.show
nicolasstucki 71d8c78
Move ExprFrontend and ExprRun into ExprCompiler
nicolasstucki d8d38be
Do not overide phases directly
nicolasstucki 88b7f46
Use isQuote
nicolasstucki 610ad9c
Fix compiling quote in file with no splices
nicolasstucki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
compiler/src/dotty/tools/dotc/quoted/ExprCompilationUnit.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dotty.tools.dotc.quoted | ||
|
||
import dotty.tools.dotc.CompilationUnit | ||
import dotty.tools.dotc.util.NoSource | ||
|
||
import scala.quoted.Expr | ||
|
||
/* Compilation unit containing the contents of a quoted expression */ | ||
class ExprCompilationUnit(val expr: Expr[_]) extends CompilationUnit(NoSource) { | ||
override def toString = s"Expr($expr)" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package dotty.tools.dotc | ||
package quoted | ||
|
||
import dotty.tools.backend.jvm.GenBCode | ||
import dotty.tools.dotc.ast.tpd | ||
|
||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.Flags.{EmptyFlags, Method} | ||
import dotty.tools.dotc.core.{Mode, Phases} | ||
import dotty.tools.dotc.core.Phases.Phase | ||
import dotty.tools.dotc.core.Scopes.{EmptyScope, newScope} | ||
import dotty.tools.dotc.core.StdNames.nme | ||
import dotty.tools.dotc.core.Symbols.defn | ||
import dotty.tools.dotc.core.Types.ExprType | ||
import dotty.tools.dotc.core.quoted.PickledQuotes | ||
import dotty.tools.dotc.transform.ReifyQuotes | ||
import dotty.tools.dotc.typer.FrontEnd | ||
import dotty.tools.dotc.util.Positions.Position | ||
import dotty.tools.dotc.util.SourceFile | ||
import dotty.tools.io.{Path, PlainFile, VirtualDirectory} | ||
|
||
import scala.quoted.Expr | ||
|
||
/** Compiler that takes the contents of a quoted expression `expr` and produces | ||
* a class file with `class ' { def apply: Object = expr }`. | ||
*/ | ||
class ExprCompiler(directory: VirtualDirectory) extends Compiler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since these are all small files, should ExprRun and ExprFrontEnd be made part of ExprCompiler? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. Done. |
||
import tpd._ | ||
|
||
/** A GenBCode phase that outputs to a virtual directory */ | ||
private class ExprGenBCode extends GenBCode { | ||
override def phaseName = "genBCode" | ||
override def outputDir(implicit ctx: Context) = directory | ||
} | ||
|
||
override protected def frontendPhases: List[List[Phase]] = | ||
List(List(new ExprFrontend(putInClass = true))) | ||
|
||
override protected def picklerPhases: List[List[Phase]] = | ||
List(List(new ReifyQuotes)) | ||
|
||
override protected def backendPhases: List[List[Phase]] = | ||
List(List(new ExprGenBCode)) | ||
|
||
override def newRun(implicit ctx: Context): ExprRun = { | ||
reset() | ||
new ExprRun(this, ctx.addMode(Mode.ReadPositions)) | ||
} | ||
|
||
/** Frontend that receives scala.quoted.Expr as input */ | ||
class ExprFrontend(putInClass: Boolean) extends FrontEnd { | ||
import tpd._ | ||
|
||
override def isTyper = false | ||
|
||
override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = { | ||
units.map { | ||
case exprUnit: ExprCompilationUnit => | ||
val tree = | ||
if (putInClass) inClass(exprUnit.expr) | ||
else PickledQuotes.quotedToTree(exprUnit.expr) | ||
val source = new SourceFile("", Seq()) | ||
CompilationUnit.mkCompilationUnit(source, tree, forceTrees = true) | ||
} | ||
} | ||
|
||
/** Places the contents of expr in a compilable tree for a class | ||
* with the following format. | ||
* `package __root__ { class ' { def apply: Any = <expr> } }` | ||
*/ | ||
private def inClass(expr: Expr[_])(implicit ctx: Context): Tree = { | ||
val pos = Position(0) | ||
val assocFile = new PlainFile(Path("<quote>")) | ||
|
||
val cls = ctx.newCompleteClassSymbol(defn.RootClass, nme.QUOTE.toTypeName, EmptyFlags, | ||
defn.ObjectType :: Nil, newScope, coord = pos, assocFile = assocFile).entered.asClass | ||
cls.enter(ctx.newDefaultConstructor(cls), EmptyScope) | ||
val meth = ctx.newSymbol(cls, nme.apply, Method, ExprType(defn.AnyType), coord = pos).entered | ||
|
||
val quoted = PickledQuotes.quotedToTree(expr)(ctx.withOwner(meth)) | ||
|
||
val run = DefDef(meth, quoted) | ||
val classTree = ClassDef(cls, DefDef(cls.primaryConstructor.asTerm), run :: Nil) | ||
PackageDef(ref(defn.RootPackage).asInstanceOf[Ident], classTree :: Nil).withPos(pos) | ||
} | ||
} | ||
|
||
class ExprRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) { | ||
def compileExpr(expr: Expr[_]): Unit = { | ||
val units = new ExprCompilationUnit(expr) :: Nil | ||
compileUnits(units) | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dotty.tools.dotc.quoted | ||
|
||
import java.io.PrintStream | ||
|
||
import dotty.tools.dotc.core.Phases.Phase | ||
|
||
/** Compiler that takes the contents of a quoted expression `expr` and produces outputs | ||
* the pretty printed code. | ||
*/ | ||
class ExprDecompiler(out: PrintStream) extends ExprCompiler(null) { | ||
override def phases: List[List[Phase]] = List( | ||
List(new ExprFrontend(putInClass = false)), // Create class from Expr | ||
List(new QuotePrinter(out)) // Print all loaded classes | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package dotty.tools.dotc.quoted | ||
|
||
import dotty.tools.dotc.Driver | ||
import dotty.tools.dotc.core.Contexts.{Context, FreshContext} | ||
import dotty.tools.dotc.core.StdNames._ | ||
import dotty.tools.io.VirtualDirectory | ||
import dotty.tools.repl.AbstractFileClassLoader | ||
|
||
import scala.quoted.Expr | ||
import java.io.ByteArrayOutputStream | ||
import java.io.PrintStream | ||
import java.nio.charset.StandardCharsets | ||
|
||
class QuoteDriver extends Driver { | ||
|
||
def run[T](expr: Expr[T]): T = { | ||
val ctx: Context = initCtx.fresh | ||
// TODO enable optimisation? | ||
// ctx.settings.optimise.update(true)(ctx) | ||
|
||
val outDir = new VirtualDirectory("(memory)", None) | ||
|
||
new ExprCompiler(outDir).newRun(ctx).compileExpr(expr) | ||
|
||
val classLoader = new AbstractFileClassLoader(outDir, this.getClass.getClassLoader) | ||
|
||
val clazz = classLoader.loadClass(nme.QUOTE.toString) | ||
val method = clazz.getMethod("apply") | ||
val instance = clazz.newInstance() | ||
|
||
method.invoke(instance).asInstanceOf[T] | ||
} | ||
|
||
def show(expr: Expr[_]): String = { | ||
val ctx: Context = initCtx.fresh | ||
ctx.settings.color.update("never")(ctx) // TODO support colored show | ||
val baos = new ByteArrayOutputStream | ||
var ps: PrintStream = null | ||
try { | ||
ps = new PrintStream(baos, true, "utf-8") | ||
|
||
new ExprDecompiler(ps).newRun(ctx).compileExpr(expr) | ||
|
||
new String(baos.toByteArray, StandardCharsets.UTF_8) | ||
} | ||
finally if (ps != null) ps.close() | ||
} | ||
|
||
override def initCtx: Context = { | ||
val ictx = super.initCtx.fresh | ||
val classpath = System.getProperty("java.class.path") | ||
ictx.settings.classpath.update(classpath)(ictx) | ||
ictx | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dotty.tools.dotc.quoted | ||
|
||
import java.io.PrintStream | ||
|
||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.core.Phases.Phase | ||
|
||
/** Pretty prints the compilation unit to an output stream */ | ||
class QuotePrinter(out: PrintStream) extends Phase { | ||
|
||
override def phaseName: String = "quotePrinter" | ||
|
||
override def run(implicit ctx: Context): Unit = { | ||
val unit = ctx.compilationUnit | ||
out.print(unit.tpdTree.show) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package dotty.tools.dotc.quoted | ||
|
||
import dotty.tools.dotc.ast.Trees.Literal | ||
import dotty.tools.dotc.core.Constants.Constant | ||
import dotty.tools.dotc.printing.RefinedPrinter | ||
|
||
import scala.quoted.Expr | ||
import scala.quoted.Liftable.ConstantExpr | ||
import scala.runtime.quoted._ | ||
|
||
/** Default runners for quoted expressions */ | ||
object Runners { | ||
|
||
implicit def runner[T]: Runner[T] = new Runner[T] { | ||
|
||
def run(expr: Expr[T]): T = expr match { | ||
case expr: ConstantExpr[T] => expr.value | ||
case _ => new QuoteDriver().run(expr) | ||
} | ||
|
||
def show(expr: Expr[T]): String = expr match { | ||
case expr: ConstantExpr[T] => | ||
val ctx = new QuoteDriver().initCtx | ||
ctx.settings.color.update("never")(ctx) | ||
val printer = new RefinedPrinter(ctx) | ||
printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false) | ||
case _ => new QuoteDriver().show(expr) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why the change here is necessary.
forceTrees
is true only in the IDE,, right? But in the IDE we never get to ReifyQuotes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, force tree is also used when compiling from TASTY.
This covers the case when we recompile from TASTY a file with a quote and no splices.