Skip to content

Commit 6cc453b

Browse files
committed
Implement toTasty on quoted.Type
1 parent bc67630 commit 6cc453b

File tree

13 files changed

+109
-28
lines changed

13 files changed

+109
-28
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ object PickledQuotes {
4141
case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
4242
case value=> Literal(Constant(value))
4343
}
44-
case expr: TreeExpr @unchecked =>
44+
case expr: TreeExpr =>
4545
dotty.tools.dotc.tasty.internal.Term.tree(expr.term)
4646
case expr: FunctionAppliedTo[_, _] =>
4747
functionAppliedTo(quotedExprToTree(expr.f), quotedExprToTree(expr.x))
@@ -51,7 +51,7 @@ object PickledQuotes {
5151
def quotedTypeToTree(expr: quoted.Type[_])(implicit ctx: Context): Tree = expr match {
5252
case expr: TastyType[_] => unpickleType(expr)
5353
case expr: TaggedType[_] => classTagToTypeTree(expr.ct)
54-
case expr: TreeType[Tree] @unchecked => expr.tree
54+
case expr: TreeType => dotty.tools.dotc.tasty.internal.TypeTree.tree(expr.tpe)
5555
}
5656

5757
/** Unpickle the tree contained in the TastyExpr */

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ class TreeUnpickler(reader: TastyReader,
11421142
val splice = splices(idx)
11431143
def wrap(arg: Tree) =
11441144
if (arg.isTerm) new TreeExpr(dotty.tools.dotc.tasty.internal.Term(arg))
1145-
else new TreeType(arg) // TODO use tasty type
1145+
else new TreeType(dotty.tools.dotc.tasty.internal.TypeTree(arg))
11461146
val reifiedArgs = args.map(wrap)
11471147
if (isType) {
11481148
val quotedType =

compiler/src/dotty/tools/dotc/quoted/ExprCompiler.scala renamed to compiler/src/dotty/tools/dotc/quoted/QuoteCompiler.scala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import dotty.tools.dotc.util.Positions.Position
2020
import dotty.tools.dotc.util.SourceFile
2121
import dotty.tools.io.{AbstractFile, Path, PlainFile}
2222

23-
import scala.quoted.Expr
23+
import scala.quoted.{Expr, Type}
2424

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

3131
/** A GenBCode phase that outputs to a virtual directory */
@@ -35,7 +35,7 @@ class ExprCompiler(directory: AbstractFile) extends Compiler {
3535
}
3636

3737
override protected def frontendPhases: List[List[Phase]] =
38-
List(List(new ExprFrontend(putInClass = true)))
38+
List(List(new QuotedFrontend(putInClass = true)))
3939

4040
override protected def picklerPhases: List[List[Phase]] =
4141
List(List(new ReifyQuotes))
@@ -50,8 +50,8 @@ class ExprCompiler(directory: AbstractFile) extends Compiler {
5050

5151
def outputClassName: TypeName = "Quoted".toTypeName
5252

53-
/** Frontend that receives scala.quoted.Expr as input */
54-
class ExprFrontend(putInClass: Boolean) extends FrontEnd {
53+
/** Frontend that receives a scala.quoted.Expr or scala.quoted.Type as input */
54+
class QuotedFrontend(putInClass: Boolean) extends FrontEnd {
5555
import tpd._
5656

5757
override def isTyper = false
@@ -64,6 +64,11 @@ class ExprCompiler(directory: AbstractFile) extends Compiler {
6464
else PickledQuotes.quotedExprToTree(exprUnit.expr)
6565
val source = new SourceFile("", Seq())
6666
CompilationUnit.mkCompilationUnit(source, tree, forceTrees = true)
67+
case typeUnit: TypeCompilationUnit =>
68+
assert(!putInClass)
69+
val tree = PickledQuotes.quotedTypeToTree(typeUnit.tpe)
70+
val source = new SourceFile("", Seq())
71+
CompilationUnit.mkCompilationUnit(source, tree, forceTrees = true)
6772
}
6873
}
6974

@@ -93,6 +98,10 @@ class ExprCompiler(directory: AbstractFile) extends Compiler {
9398
val units = new ExprCompilationUnit(expr) :: Nil
9499
compileUnits(units)
95100
}
101+
def compileType(tpe: Type[_]): Unit = {
102+
val units = new TypeCompilationUnit(tpe) :: Nil
103+
compileUnits(units)
104+
}
96105
}
97106

98107
}

compiler/src/dotty/tools/dotc/quoted/ExprDecompiler.scala renamed to compiler/src/dotty/tools/dotc/quoted/QuoteDecompiler.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import dotty.tools.dotc.ast.tpd
44
import dotty.tools.dotc.core.Contexts.Context
55
import dotty.tools.dotc.core.Phases.Phase
66

7-
/** Compiler that takes the contents of a quoted expression `expr` and outputs it's tree. */
8-
class ExprDecompiler(output: tpd.Tree => Context => Unit) extends ExprCompiler(null) {
7+
/** Compiler that takes the contents of a quoted expression (or type) and outputs it's tree. */
8+
class QuoteDecompiler(output: tpd.Tree => Context => Unit) extends QuoteCompiler(null) {
99
override def phases: List[List[Phase]] = List(
10-
List(new ExprFrontend(putInClass = false)), // Create class from Expr
10+
List(new QuotedFrontend(putInClass = false)), // Create class from Expr
1111
List(new QuoteTreeOutput(output))
1212
)
1313

1414
class QuoteTreeOutput(output: tpd.Tree => Context => Unit) extends Phase {
15-
override def phaseName: String = "quotePrinter"
15+
override def phaseName: String = "quoteOutput"
1616
override def run(implicit ctx: Context): Unit = output(ctx.compilationUnit.tpdTree)(ctx)
1717
}
1818
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory
77
import dotty.tools.repl.AbstractFileClassLoader
88
import dotty.tools.dotc.printing.DecompilerPrinter
99

10-
import scala.quoted.Expr
10+
import scala.quoted.{Expr, Type}
1111

1212
import java.net.URLClassLoader
1313

@@ -28,7 +28,7 @@ class QuoteDriver extends Driver {
2828
new VirtualDirectory("(memory)", None)
2929
}
3030

31-
val driver = new ExprCompiler(outDir)
31+
val driver = new QuoteCompiler(outDir)
3232
driver.newRun(ctx).compileExpr(expr)
3333

3434
val classLoader = new AbstractFileClassLoader(outDir, this.getClass.getClassLoader)
@@ -58,10 +58,22 @@ class QuoteDriver extends Driver {
5858
assert(output.isEmpty)
5959
output = Some(f(tree, ctx))
6060
}
61-
new ExprDecompiler(registerTree).newRun(ctx).compileExpr(expr)
61+
new QuoteDecompiler(registerTree).newRun(ctx).compileExpr(expr)
6262
output.getOrElse(throw new Exception("Could not extract " + expr))
6363
}
6464

65+
def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: Settings[_]): T = {
66+
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
67+
68+
var output: Option[T] = None
69+
def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {
70+
assert(output.isEmpty)
71+
output = Some(f(tree.asInstanceOf[TypTree], ctx))
72+
}
73+
new QuoteDecompiler(registerTree).newRun(ctx).compileType(tpe)
74+
output.getOrElse(throw new Exception("Could not extract " + tpe))
75+
}
76+
6577
override def initCtx: Context = {
6678
val ictx = super.initCtx.fresh
6779
var classpath = System.getProperty("java.class.path")

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package dotty.tools.dotc.quoted
22

3-
import dotty.tools.dotc.ast.Trees
4-
import dotty.tools.dotc.ast.Trees._
53
import dotty.tools.dotc.ast.tpd
64
import dotty.tools.dotc.core.Constants._
75
import dotty.tools.dotc.printing.RefinedPrinter
8-
import dotty.tools.dotc.tasty.internal.Term
6+
import dotty.tools.dotc.tasty.internal
97

10-
import scala.quoted.Expr
11-
import scala.runtime.BoxedUnit
8+
import scala.quoted.{Expr, Type}
129
import scala.quoted.Exprs.{LiftedExpr, TreeExpr}
10+
import scala.quoted.Types.TreeType
11+
import scala.runtime.BoxedUnit
1312
import scala.runtime.quoted._
14-
import scala.tasty.trees.Term
13+
import scala.tasty.trees
1514

1615
/** Default runners for quoted expressions */
1716
object Toolbox {
@@ -45,11 +44,15 @@ object Toolbox {
4544
case _ => new QuoteDriver().show(expr, showSettings)
4645
}
4746

48-
def toTasty(expr: Expr[T]): Term = expr match {
47+
def toTasty(expr: Expr[T]): trees.Term = expr match {
4948
case expr: TreeExpr => expr.term
50-
case _ => new QuoteDriver().withTree(expr, (tree, ctx) => Term(tree)(ctx), Settings.run())
49+
case _ => new QuoteDriver().withTree(expr, (tree, ctx) => internal.Term(tree)(ctx), Settings.run())
5150
}
5251

52+
def toTasty(tpe: Type[T]): trees.TypeTree = tpe match {
53+
case expr: TreeType => expr.tpe
54+
case _ => new QuoteDriver().withTypeTree(tpe, (tpt, ctx) => internal.TypeTree(tpt)(ctx), Settings.run())
55+
}
5356
}
5457

5558
class Settings[T] private (val outDir: Option[String], val rawTree: Boolean, val compilerArgs: List[String])
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dotty.tools.dotc.quoted
2+
3+
import dotty.tools.dotc.CompilationUnit
4+
import dotty.tools.dotc.util.NoSource
5+
6+
import scala.quoted.Type
7+
8+
/* Compilation unit containing the contents of a quoted type */
9+
class TypeCompilationUnit(val tpe: Type[_]) extends CompilationUnit(NoSource) {
10+
override def toString = s"Type($tpe)"
11+
}

compiler/src/dotty/tools/dotc/tasty/internal/TypeTree.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ object TypeTree {
7979
case _ => None
8080
}
8181

82+
def tree(tpe: trees.TypeTree): tpd.Tree = tpe.asInstanceOf[Impl].tree
83+
8284
private[tasty] class Impl(val tree: tpd.Tree)(implicit val ctx: Context) extends trees.TypeTree with Positioned {
8385

8486
assert(!tree.isInstanceOf[Trees.TypeBoundsTree[_]])

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ object Splicer {
6565
}
6666
args1 ::: liftArgs(tp.resType, args.tail)
6767
case tp: PolyType =>
68-
val args1 = args.head.map(tp => new scala.quoted.Types.TreeType(tp))
68+
val args1 = args.head.map(tp => new scala.quoted.Types.TreeType(dotty.tools.dotc.tasty.internal.TypeTree(tp)))
6969
args1 ::: liftArgs(tp.resType, args.tail)
7070
case _ => Nil
7171
}

library/src/scala/quoted/Type.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package scala.quoted
22

33
import scala.quoted.Types.TaggedType
44
import scala.reflect.ClassTag
5+
import scala.runtime.quoted.Toolbox
56
import scala.runtime.quoted.Unpickler.Pickled
67

78
sealed abstract class Type[T] {
89
type unary_~ = T
10+
final def toTasty(implicit toolbox: Toolbox[T]): scala.tasty.trees.TypeTree = toolbox.toTasty(this)
911
}
1012

1113
/** Some basic type tags, currently incomplete */
@@ -36,7 +38,7 @@ object Types {
3638
}
3739

3840
/** An Type backed by a tree */
39-
final class TreeType[Tree](val tree: Tree) extends quoted.Type[Any] {
41+
final class TreeType(val tpe: scala.tasty.trees.TypeTree) extends quoted.Type[Any] {
4042
override def toString: String = s"Type(<raw>)"
4143
}
4244
}

0 commit comments

Comments
 (0)