|
| 1 | +package dotty.tools.dotc |
| 2 | +package quoted |
| 3 | + |
| 4 | +import dotty.tools.backend.jvm.GenBCode |
| 5 | +import dotty.tools.dotc.ast.tpd |
| 6 | + |
| 7 | +import dotty.tools.dotc.core.Contexts.Context |
| 8 | +import dotty.tools.dotc.core.Flags.{EmptyFlags, Method} |
| 9 | +import dotty.tools.dotc.core.{Mode, Phases} |
| 10 | +import dotty.tools.dotc.core.Phases.Phase |
| 11 | +import dotty.tools.dotc.core.Scopes.{EmptyScope, newScope} |
| 12 | +import dotty.tools.dotc.core.StdNames.nme |
| 13 | +import dotty.tools.dotc.core.Symbols.defn |
| 14 | +import dotty.tools.dotc.core.Types.ExprType |
| 15 | +import dotty.tools.dotc.core.quoted.PickledQuotes |
| 16 | +import dotty.tools.dotc.transform.ReifyQuotes |
| 17 | +import dotty.tools.dotc.typer.FrontEnd |
| 18 | +import dotty.tools.dotc.util.Positions.Position |
| 19 | +import dotty.tools.dotc.util.SourceFile |
| 20 | +import dotty.tools.io.{Path, PlainFile, VirtualDirectory} |
| 21 | + |
| 22 | +import scala.quoted.Expr |
| 23 | + |
| 24 | +/** Compiler that takes the contents of a quoted expression `expr` and produces |
| 25 | + * a class file with `class ' { def apply: Object = expr }`. |
| 26 | + */ |
| 27 | +class ExprCompiler(directory: VirtualDirectory) extends Compiler { |
| 28 | + import tpd._ |
| 29 | + |
| 30 | + /** A GenBCode phase that outputs to a virtual directory */ |
| 31 | + private class ExprGenBCode extends GenBCode { |
| 32 | + override def phaseName = "genBCode" |
| 33 | + override def outputDir(implicit ctx: Context) = directory |
| 34 | + } |
| 35 | + |
| 36 | + override protected def frontendPhases: List[List[Phase]] = |
| 37 | + List(List(new ExprFrontend(putInClass = true))) |
| 38 | + |
| 39 | + override protected def picklerPhases: List[List[Phase]] = |
| 40 | + List(List(new ReifyQuotes)) |
| 41 | + |
| 42 | + override protected def backendPhases: List[List[Phase]] = |
| 43 | + List(List(new ExprGenBCode)) |
| 44 | + |
| 45 | + override def newRun(implicit ctx: Context): ExprRun = { |
| 46 | + reset() |
| 47 | + new ExprRun(this, ctx.addMode(Mode.ReadPositions)) |
| 48 | + } |
| 49 | + |
| 50 | + /** Frontend that receives scala.quoted.Expr as input */ |
| 51 | + class ExprFrontend(putInClass: Boolean) extends FrontEnd { |
| 52 | + import tpd._ |
| 53 | + |
| 54 | + override def isTyper = false |
| 55 | + |
| 56 | + override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = { |
| 57 | + units.map { |
| 58 | + case exprUnit: ExprCompilationUnit => |
| 59 | + val tree = |
| 60 | + if (putInClass) inClass(exprUnit.expr) |
| 61 | + else PickledQuotes.quotedToTree(exprUnit.expr) |
| 62 | + val source = new SourceFile("", Seq()) |
| 63 | + CompilationUnit.mkCompilationUnit(source, tree, forceTrees = true) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** Places the contents of expr in a compilable tree for a class |
| 68 | + * with the following format. |
| 69 | + * `package __root__ { class ' { def apply: Any = <expr> } }` |
| 70 | + */ |
| 71 | + private def inClass(expr: Expr[_])(implicit ctx: Context): Tree = { |
| 72 | + val pos = Position(0) |
| 73 | + val assocFile = new PlainFile(Path("<quote>")) |
| 74 | + |
| 75 | + val cls = ctx.newCompleteClassSymbol(defn.RootClass, nme.QUOTE.toTypeName, EmptyFlags, |
| 76 | + defn.ObjectType :: Nil, newScope, coord = pos, assocFile = assocFile).entered.asClass |
| 77 | + cls.enter(ctx.newDefaultConstructor(cls), EmptyScope) |
| 78 | + val meth = ctx.newSymbol(cls, nme.apply, Method, ExprType(defn.AnyType), coord = pos).entered |
| 79 | + |
| 80 | + val quoted = PickledQuotes.quotedToTree(expr)(ctx.withOwner(meth)) |
| 81 | + |
| 82 | + val run = DefDef(meth, quoted) |
| 83 | + val classTree = ClassDef(cls, DefDef(cls.primaryConstructor.asTerm), run :: Nil) |
| 84 | + PackageDef(ref(defn.RootPackage).asInstanceOf[Ident], classTree :: Nil).withPos(pos) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + class ExprRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) { |
| 89 | + def compileExpr(expr: Expr[_]): Unit = { |
| 90 | + val units = new ExprCompilationUnit(expr) :: Nil |
| 91 | + compileUnits(units) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments