Skip to content

Commit 29b52d7

Browse files
committed
Move RawQuoted to library
1 parent c29bbe1 commit 29b52d7

File tree

7 files changed

+50
-46
lines changed

7 files changed

+50
-46
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import dotty.tools.dotc.core.Constants.Constant
77
import dotty.tools.dotc.core.Contexts._
88
import dotty.tools.dotc.core.Decorators._
99
import dotty.tools.dotc.core.Flags._
10-
import dotty.tools.dotc.core.NameKinds
1110
import dotty.tools.dotc.core.StdNames._
11+
import dotty.tools.dotc.core.NameKinds
1212
import dotty.tools.dotc.core.Symbols._
1313
import dotty.tools.dotc.core.tasty.{TastyPickler, TastyPrinter, TastyString}
14-
import dotty.tools.dotc.interpreter.RawQuoted
1514

1615
import scala.quoted.Quoted._
1716

@@ -35,17 +34,19 @@ object PickledQuotes {
3534
}
3635

3736
/** Transform the expression into its fully spliced Tree */
38-
def quotedToTree(expr: quoted.Quoted)(implicit ctx: Context): Tree = expr match {
39-
case expr: TastyQuoted =>
40-
unpickleQuote(expr)
41-
case expr: ConstantExpr[_] =>
42-
Literal(Constant(expr.value))
37+
def quotedExprToTree(expr: quoted.Expr[_])(implicit ctx: Context): Tree = expr match {
38+
case expr: TastyExpr[_] => unpickleQuote(expr)
39+
case expr: ConstantExpr[_] => Literal(Constant(expr.value))
40+
case expr: RawExpr[Tree] @unchecked => expr.tree
4341
case expr: FunctionAppliedTo[_, _] =>
44-
functionAppliedTo(quotedToTree(expr.f), quotedToTree(expr.x))
45-
case expr: TaggedType[_] =>
46-
classTagToTypeTree(expr.ct)
47-
case expr: RawQuoted =>
48-
expr.tree
42+
functionAppliedTo(quotedExprToTree(expr.f), quotedExprToTree(expr.x))
43+
}
44+
45+
/** Transform the expression into its fully spliced TypeTree */
46+
def quotedTypeToTree(expr: quoted.Type[_])(implicit ctx: Context): Tree = expr match {
47+
case expr: TastyType[_] => unpickleQuote(expr)
48+
case expr: TaggedType[_] => classTagToTypeTree(expr.ct)
49+
case expr: RawType[Tree] @unchecked => expr.tree
4950
}
5051

5152
/** Unpickle the tree contained in the TastyQuoted */

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import config.Printers.pickling
2020
import typer.Checking
2121
import config.Config
2222
import dotty.tools.dotc.core.quoted.PickledQuotes
23-
import dotty.tools.dotc.interpreter.RawQuoted
2423
import scala.quoted
24+
import scala.quoted.Quoted.{RawExpr, RawType}
2525

2626
/** Unpickler for typed trees
2727
* @param reader the reader from which to unpickle
@@ -288,7 +288,7 @@ class TreeUnpickler(reader: TastyReader,
288288
case ENUMconst =>
289289
ConstantType(Constant(readTermRef().termSymbol))
290290
case HOLE =>
291-
readHole(end).tpe
291+
readHole(end, isType = true).tpe
292292
}
293293
assert(currentAddr == end, s"$start $currentAddr $end ${astTagToString(tag)}")
294294
result
@@ -1040,7 +1040,7 @@ class TreeUnpickler(reader: TastyReader,
10401040
case TYPEBOUNDStpt =>
10411041
TypeBoundsTree(readTpt(), readTpt())
10421042
case HOLE =>
1043-
readHole(end)
1043+
readHole(end, isType = false)
10441044
case _ =>
10451045
readPathTerm()
10461046
}
@@ -1091,14 +1091,23 @@ class TreeUnpickler(reader: TastyReader,
10911091
new LazyReader(localReader, op)
10921092
}
10931093

1094-
def readHole(end: Addr)(implicit ctx: Context): Tree = {
1094+
def readHole(end: Addr, isType: Boolean)(implicit ctx: Context): Tree = {
10951095
val idx = readNat()
10961096
val args = until(end)(readTerm())
10971097
val splice = splices(idx)
1098-
val quotedType =
1099-
if (args.isEmpty) splice.asInstanceOf[quoted.Quoted]
1100-
else splice.asInstanceOf[Seq[Any] => quoted.Quoted](args.map(RawQuoted.apply))
1101-
PickledQuotes.quotedToTree(quotedType)
1098+
1099+
if (isType) {
1100+
val quotedType =
1101+
if (args.isEmpty) splice.asInstanceOf[quoted.Type[_]]
1102+
else splice.asInstanceOf[Seq[Any] => quoted.Type[_]](args.map(tree => new RawType(tree)))
1103+
PickledQuotes.quotedTypeToTree(quotedType)
1104+
} else {
1105+
val quotedExpr =
1106+
if (args.isEmpty) splice.asInstanceOf[quoted.Expr[_]]
1107+
else splice.asInstanceOf[Seq[Any] => quoted.Expr[_]](args.map(tree => new RawExpr(tree)))
1108+
PickledQuotes.quotedExprToTree(quotedExpr)
1109+
}
1110+
11021111
}
11031112

11041113
// ------ Setting positions ------------------------------------------------

compiler/src/dotty/tools/dotc/interpreter/Interpreter.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ class Interpreter(implicit ctx: Context) {
7171
implicit val pos: Position = tree.pos
7272

7373
tree match {
74-
case Quoted(quotedTree) => RawQuoted(quotedTree)
74+
case Quoted(quotedTree) =>
75+
if (tree.isTerm) new scala.quoted.Quoted.RawExpr(quotedTree)
76+
else new scala.quoted.Quoted.RawType(quotedTree)
7577

7678
case Literal(Constant(c)) => c.asInstanceOf[Object]
7779

compiler/src/dotty/tools/dotc/interpreter/RawQuoted.scala

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ExprCompiler(directory: AbstractFile) extends Compiler {
6161
case exprUnit: ExprCompilationUnit =>
6262
val tree =
6363
if (putInClass) inClass(exprUnit.expr)
64-
else PickledQuotes.quotedToTree(exprUnit.expr)
64+
else PickledQuotes.quotedExprToTree(exprUnit.expr)
6565
val source = new SourceFile("", Seq())
6666
CompilationUnit.mkCompilationUnit(source, tree, forceTrees = true)
6767
}
@@ -80,7 +80,7 @@ class ExprCompiler(directory: AbstractFile) extends Compiler {
8080
cls.enter(ctx.newDefaultConstructor(cls), EmptyScope)
8181
val meth = ctx.newSymbol(cls, nme.apply, Method, ExprType(defn.AnyType), coord = pos).entered
8282

83-
val quoted = PickledQuotes.quotedToTree(expr)(ctx.withOwner(meth))
83+
val quoted = PickledQuotes.quotedExprToTree(expr)(ctx.withOwner(meth))
8484

8585
val run = DefDef(meth, quoted)
8686
val classTree = ClassDef(cls, DefDef(cls.primaryConstructor.asTerm), run :: Nil)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object Splicer {
2424
/** Splice the Tree for a Quoted expression which is constructed via a reflective call to the given method */
2525
private def reflectiveSplice(tree: Tree)(implicit ctx: Context): Tree = {
2626
val interpreter = new Interpreter
27-
interpreter.interpretTree[scala.quoted.Expr[_]](tree).map(PickledQuotes.quotedToTree(_)).getOrElse(tree)
27+
interpreter.interpretTree[scala.quoted.Expr[_]](tree).map(PickledQuotes.quotedExprToTree).getOrElse(tree)
2828
}
2929

3030
}

library/src/scala/quoted/Quoted.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ object Quoted {
1414
def args: Seq[Any]
1515
}
1616

17+
/** Quoted for which its internal representation is its tree.
18+
* - Used for trees that cannot be serialized, such as references to local symbols that will be spliced in.
19+
* - Used for trees that do not need to be serialized to avoid the overhead of serialization/deserialization.
20+
*/
21+
trait RawQuoted[Tree] extends quoted.Quoted {
22+
def tree: Tree
23+
}
24+
1725
// Implementations of Expr[T]
1826

1927
/** An Expr backed by a pickled TASTY tree */
@@ -26,6 +34,9 @@ object Quoted {
2634
override def toString: String = s"Expr($value)"
2735
}
2836

37+
/** An Expr backed by a tree */
38+
final class RawExpr[Tree](val tree: Tree) extends quoted.Expr[Any] with RawQuoted[Tree]
39+
2940
/** An Expr representing `'{(~f).apply(~x)}` but it is beta-reduced when the closure is known */
3041
final class FunctionAppliedTo[T, U](val f: Expr[T => U], val x: Expr[T]) extends Expr[U] {
3142
override def toString: String = s"Expr($f <applied to> $x)"
@@ -43,4 +54,7 @@ object Quoted {
4354
override def toString: String = s"Type($ct)"
4455
}
4556

57+
/** An Type backed by a tree */
58+
final class RawType[Tree](val tree: Tree) extends quoted.Type[Any] with RawQuoted[Tree]
59+
4660
}

0 commit comments

Comments
 (0)