Skip to content

Commit 4090212

Browse files
committed
Move RawQuoted to library
1 parent 991c112 commit 4090212

File tree

7 files changed

+61
-50
lines changed

7 files changed

+61
-50
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import dotty.tools.dotc.core.Flags._
1010
import dotty.tools.dotc.core.StdNames._
1111
import dotty.tools.dotc.core.Symbols._
1212
import dotty.tools.dotc.core.tasty.{TastyPickler, TastyPrinter, TastyString}
13-
import dotty.tools.dotc.interpreter.RawQuoted
1413

1514
import scala.quoted.Quoted._
1615

@@ -34,22 +33,17 @@ object PickledQuotes {
3433
}
3534

3635
/** Transform the expression into its fully spliced Tree */
37-
def quotedToTree(expr: quoted.Quoted)(implicit ctx: Context): Tree = expr match {
38-
case expr: TastyQuoted => unpickleQuote(expr)
36+
def quotedExprToTree(expr: quoted.Expr[_])(implicit ctx: Context): Tree = expr match {
37+
case expr: TastyExpr[_] => unpickleQuote(expr)
3938
case expr: ConstantExpr[_] => Literal(Constant(expr.value))
40-
case expr: TaggedType[_] =>
41-
val tpe = expr.ct match {
42-
case ClassTag.Unit => defn.UnitType
43-
case ClassTag.Byte => defn.ByteType
44-
case ClassTag.Char => defn.CharType
45-
case ClassTag.Short => defn.ShortType
46-
case ClassTag.Int => defn.IntType
47-
case ClassTag.Long => defn.LongType
48-
case ClassTag.Float => defn.FloatType
49-
case ClassTag.Double => defn.FloatType
50-
}
51-
TypeTree(tpe)
52-
case expr: RawQuoted => expr.tree
39+
case expr: RawExpr[Tree] @unchecked => expr.tree
40+
}
41+
42+
/** Transform the expression into its fully spliced TypeTree */
43+
def quotedTypeToTree(expr: quoted.Type[_])(implicit ctx: Context): Tree = expr match {
44+
case expr: TastyType[_] => unpickleQuote(expr)
45+
case expr: TaggedType[_] => classTagToTypeTree(expr.ct)
46+
case expr: RawType[Tree] @unchecked => expr.tree
5347
}
5448

5549
/** Unpickle the tree contained in the TastyQuoted */
@@ -113,4 +107,18 @@ object PickledQuotes {
113107
}
114108
tree
115109
}
110+
111+
private def classTagToTypeTree(ct: ClassTag[_])(implicit ctx: Context): TypeTree = {
112+
val tpe = ct match {
113+
case ClassTag.Unit => defn.UnitType
114+
case ClassTag.Byte => defn.ByteType
115+
case ClassTag.Char => defn.CharType
116+
case ClassTag.Short => defn.ShortType
117+
case ClassTag.Int => defn.IntType
118+
case ClassTag.Long => defn.LongType
119+
case ClassTag.Float => defn.FloatType
120+
case ClassTag.Double => defn.FloatType
121+
}
122+
TypeTree(tpe)
123+
}
116124
}

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
// Implementations of Type[T]
3041

3142
/** A Type backed by a pickled TASTY tree */
@@ -38,4 +49,7 @@ object Quoted {
3849
override def toString: String = s"Type($ct)"
3950
}
4051

52+
/** An Type backed by a tree */
53+
final class RawType[Tree](val tree: Tree) extends quoted.Type[Any] with RawQuoted[Tree]
54+
4155
}

0 commit comments

Comments
 (0)