Skip to content

Add QuoteContext #6700

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 12 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ class Definitions {
@threadUnsafe lazy val InternalQuoted_patternHoleR: TermRef = InternalQuotedModule.requiredMethodRef("patternHole")
def InternalQuoted_patternHole(implicit ctx: Context): Symbol = InternalQuoted_patternHoleR.symbol
@threadUnsafe lazy val InternalQuoted_patternBindHoleAnnot: ClassSymbol = InternalQuotedModule.requiredClass("patternBindHole")
@threadUnsafe lazy val InternalQuoted_QuoteTypeTagAnnot: ClassSymbol = InternalQuotedModule.requiredClass("quoteTypeTag")

@threadUnsafe lazy val InternalQuotedMatcherModuleRef: TermRef = ctx.requiredModuleRef("scala.internal.quoted.Matcher")
def InternalQuotedMatcherModule(implicit ctx: Context): Symbol = InternalQuotedMatcherModuleRef.symbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package dotty.tools.dotc.quoted
import dotty.tools.dotc.CompilationUnit
import dotty.tools.dotc.util.NoSource

import scala.quoted.Expr
import scala.quoted._

/* Compilation unit containing the contents of a quoted expression */
class ExprCompilationUnit(val expr: Expr[_]) extends CompilationUnit(NoSource) {
override def toString: String = s"Expr($expr)"
}
class ExprCompilationUnit(val exprBuilder: QuoteContext => Expr[_]) extends CompilationUnit(NoSource)
91 changes: 53 additions & 38 deletions compiler/src/dotty/tools/dotc/quoted/QuoteCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ 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.tastyreflect.ReflectionImpl
import dotty.tools.dotc.transform.Staging
import dotty.tools.dotc.util.Spans.Span
import dotty.tools.dotc.util.SourceFile
import dotty.tools.io.{Path, VirtualFile}

import scala.quoted.{Expr, Type}
import scala.annotation.tailrec
import scala.concurrent.Promise
import scala.quoted.{Expr, QuoteContext, Type}

/** Compiler that takes the contents of a quoted expression `expr` and produces
* a class file with `class ' { def apply: Object = expr }`.
*/
class QuoteCompiler extends Compiler {

/** Either `Left` with name of the classfile generated or `Right` with the value contained in the expression */
private[this] var result: Either[String, Any] = null

override protected def frontendPhases: List[List[Phase]] =
List(List(new QuotedFrontend(putInClass = true)))
List(List(new QuotedFrontend))

override protected def picklerPhases: List[List[Phase]] =
List(List(new Staging))
Expand All @@ -40,58 +46,67 @@ class QuoteCompiler extends Compiler {
def outputClassName: TypeName = "Generated$Code$From$Quoted".toTypeName

/** Frontend that receives a scala.quoted.Expr or scala.quoted.Type as input */
class QuotedFrontend(putInClass: Boolean) extends Phase {
class QuotedFrontend extends Phase {
import tpd._


def phaseName: String = "quotedFrontend"

override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = {
units.map {
units.flatMap {
case exprUnit: ExprCompilationUnit =>
val tree =
if (putInClass) inClass(exprUnit.expr)
else PickledQuotes.quotedExprToTree(exprUnit.expr)
val source = SourceFile.virtual("<quoted.Expr>", "")
CompilationUnit(source, tree, forceTrees = true)
case typeUnit: TypeCompilationUnit =>
assert(!putInClass)
val tree = PickledQuotes.quotedTypeToTree(typeUnit.tpe)
val source = SourceFile.virtual("<quoted.Type>", "")
CompilationUnit(source, tree, forceTrees = true)
val pos = Span(0)
val assocFile = new VirtualFile("<quote>")

// Places the contents of expr in a compilable tree for a class with the following format.
// `package __root__ { class ' { def apply: Any = <expr> } }`
val cls = ctx.newCompleteClassSymbol(defn.RootClass, outputClassName, 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.quotedExprToTree(checkValidRunExpr(exprUnit.exprBuilder.apply(new QuoteContext(ReflectionImpl(ctx)))))(ctx.withOwner(meth))

getLiteral(quoted) match {
case Some(value) =>
result = Right(value)
None // Stop copilation here we already have the result
case None =>
val run = DefDef(meth, quoted)
val classTree = ClassDef(cls, DefDef(cls.primaryConstructor.asTerm), run :: Nil)
val tree = PackageDef(ref(defn.RootPackage).asInstanceOf[Ident], classTree :: Nil).withSpan(pos)
val source = SourceFile.virtual("<quoted.Expr>", "")
result = Left(outputClassName.toString)
Some(CompilationUnit(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 = Span(0)
val assocFile = new VirtualFile("<quote>")

val cls = ctx.newCompleteClassSymbol(defn.RootClass, outputClassName, 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.quotedExprToTree(expr)(ctx.withOwner(meth))
private def checkValidRunExpr(expr: Expr[_]): Expr[_] = expr match {
case expr: scala.internal.quoted.TastyTreeExpr[Tree] @unchecked =>
throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from a macro argument.")
case _ => expr
}

val run = DefDef(meth, quoted)
val classTree = ClassDef(cls, DefDef(cls.primaryConstructor.asTerm), run :: Nil)
PackageDef(ref(defn.RootPackage).asInstanceOf[Ident], classTree :: Nil).withSpan(pos)
/** Get the literal value if this tree only contains a literal tree */
@tailrec private def getLiteral(tree: Tree): Option[Any] = tree match {
case Literal(lit) => Some(lit.value)
case Block(Nil, expr) => getLiteral(expr)
case Inlined(_, Nil, expr) => getLiteral(expr)
case _ => None
}

def run(implicit ctx: Context): Unit = unsupported("run")
}

class ExprRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) {
def compileExpr(expr: Expr[_]): Unit = {
val units = new ExprCompilationUnit(expr) :: Nil
compileUnits(units)
}
def compileType(tpe: Type[_]): Unit = {
val units = new TypeCompilationUnit(tpe) :: Nil
class ExprRun(comp: QuoteCompiler, ictx: Context) extends Run(comp, ictx) {
/** Unpickle and optionally compile the expression.
* Returns either `Left` with name of the classfile generated or `Right` with the value contained in the expression.
*/
def compileExpr(exprBuilder: QuoteContext => Expr[_]): Either[String, Any] = {
val units = new ExprCompilationUnit(exprBuilder) :: Nil
compileUnits(units)
result
}
}
}
19 changes: 0 additions & 19 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDecompiler.scala

This file was deleted.

60 changes: 12 additions & 48 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dotty.tools.dotc.tastyreflect.ReflectionImpl
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
import dotty.tools.repl.AbstractFileClassLoader
import dotty.tools.dotc.reporting._
import scala.quoted.{Expr, Type}
import scala.quoted._
import scala.quoted.Toolbox
import java.net.URLClassLoader

Expand All @@ -20,7 +20,7 @@ class QuoteDriver(appClassloader: ClassLoader) extends Driver {

private[this] val contextBase: ContextBase = new ContextBase

def run[T](expr: Expr[T], settings: Toolbox.Settings): T = {
def run[T](exprBuilder: QuoteContext => Expr[T], settings: Toolbox.Settings): T = {
val outDir: AbstractFile = settings.outDir match {
case Some(out) =>
val dir = Directory(out)
Expand All @@ -33,56 +33,21 @@ class QuoteDriver(appClassloader: ClassLoader) extends Driver {
val (_, ctx0: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
val ctx = setToolboxSettings(ctx0.fresh.setSetting(ctx0.settings.outputDir, outDir), settings)

val driver = new QuoteCompiler
driver.newRun(ctx).compileExpr(expr)
new QuoteCompiler().newRun(ctx).compileExpr(exprBuilder) match {
case Right(value) =>
value.asInstanceOf[T]

assert(!ctx.reporter.hasErrors)
case Left(classname) =>
assert(!ctx.reporter.hasErrors)

val classLoader = new AbstractFileClassLoader(outDir, appClassloader)
val classLoader = new AbstractFileClassLoader(outDir, appClassloader)

val clazz = classLoader.loadClass(driver.outputClassName.toString)
val method = clazz.getMethod("apply")
val inst = clazz.getConstructor().newInstance()
val clazz = classLoader.loadClass(classname)
val method = clazz.getMethod("apply")
val inst = clazz.getConstructor().newInstance()

method.invoke(inst).asInstanceOf[T]
}

private def doShow(tree: Tree, ctx: Context): String = {
implicit val c: Context = ctx
val tree1 =
if (ctx.settings.YshowRawQuoteTrees.value) tree
else (new TreeCleaner).transform(tree)
ReflectionImpl.showTree(tree1)
}

def show(expr: Expr[_], settings: Toolbox.Settings): String =
withTree(expr, doShow, settings)

def show(tpe: Type[_], settings: Toolbox.Settings): String =
withTypeTree(tpe, doShow, settings)

def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: Toolbox.Settings): T = {
val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)

var output: Option[T] = None
def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {
assert(output.isEmpty)
output = Some(f(tree, ctx))
}
new QuoteDecompiler(registerTree).newRun(ctx).compileExpr(expr)
output.getOrElse(throw new Exception("Could not extract " + expr))
}

def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: Toolbox.Settings): T = {
val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)

var output: Option[T] = None
def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {
assert(output.isEmpty)
output = Some(f(tree.asInstanceOf[TypTree], ctx))
method.invoke(inst).asInstanceOf[T]
}
new QuoteDecompiler(registerTree).newRun(ctx).compileType(tpe)
output.getOrElse(throw new Exception("Could not extract " + tpe))
}

override def initCtx: Context = {
Expand All @@ -92,7 +57,6 @@ class QuoteDriver(appClassloader: ClassLoader) extends Driver {
}

private def setToolboxSettings(ctx: FreshContext, settings: Toolbox.Settings): ctx.type = {
ctx.setSetting(ctx.settings.color, if (settings.color) "always" else "never")
ctx.setSetting(ctx.settings.YshowRawQuoteTrees, settings.showRawTree)
// An error in the generated code is a bug in the compiler
// Setting the throwing reporter however will report any exception
Expand Down
38 changes: 0 additions & 38 deletions compiler/src/dotty/tools/dotc/quoted/RefreshNames.scala

This file was deleted.

16 changes: 2 additions & 14 deletions compiler/src/dotty/tools/dotc/quoted/ToolboxImpl.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.tpd

import scala.quoted._
import scala.internal.quoted.{LiftedExpr, TastyTreeExpr}

/** Default runners for quoted expressions */
object ToolboxImpl {
import tpd._

/** Create a new instance of the toolbox using the the classloader of the application.
*
Expand All @@ -19,18 +15,10 @@ object ToolboxImpl {

private[this] val driver: QuoteDriver = new QuoteDriver(appClassloader)

def run[T](expr: Expr[T]): T = expr match {
case expr: LiftedExpr[T] =>
expr.value
case expr: TastyTreeExpr[Tree] @unchecked =>
throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from a macro argument.")
case _ =>
synchronized(driver.run(expr, settings))
def run[T](exprBuilder: QuoteContext => Expr[T]): T = synchronized {
driver.run(exprBuilder, settings)
}

def show[T](expr: Expr[T]): String = synchronized(driver.show(expr, settings))

def show[T](tpe: Type[T]): String = synchronized(driver.show(tpe, settings))
}

}
49 changes: 0 additions & 49 deletions compiler/src/dotty/tools/dotc/quoted/TreeCleaner.scala

This file was deleted.

Loading