@@ -19,11 +19,13 @@ import dotty.tools.dotc.tastyreflect.ReflectionImpl
1919import scala .internal .quoted ._
2020import scala .reflect .ClassTag
2121
22+ import scala .runtime .quoted .Unpickler ._
23+
2224object PickledQuotes {
2325 import tpd ._
2426
2527 /** Pickle the tree of the quote into strings */
26- def pickleQuote (tree : Tree )(implicit ctx : Context ): scala.runtime.quoted. Unpickler . Pickled = {
28+ def pickleQuote (tree : Tree )(implicit ctx : Context ): PickledQuote = {
2729 if (ctx.reporter.hasErrors) Nil
2830 else {
2931 assert(! tree.isInstanceOf [Hole ]) // Should not be pickled as it represents `'{$x}` which should be optimized to `x`
@@ -33,33 +35,12 @@ object PickledQuotes {
3335 }
3436
3537 /** Transform the expression into its fully spliced Tree */
36- def quotedExprToTree [T ](expr : quoted.Expr [T ])(implicit ctx : Context ): Tree = expr match {
37- case expr : TastyExpr [_] =>
38- val unpickled = unpickleExpr(expr)
39- /** Force unpickling of the tree, removes the spliced type `@quotedTypeTag type` definitions and dealiases references to `@quotedTypeTag type` */
40- val forceAndCleanArtefacts = new TreeMap {
41- override def transform (tree : tpd.Tree )(implicit ctx : Context ): tpd.Tree = tree match {
42- case Block (stat :: rest, expr1) if stat.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ) =>
43- assert(rest.forall { case tdef : TypeDef => tdef.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ) })
44- transform(expr1)
45- case tree => super .transform(tree).withType(dealiasTypeTags(tree.tpe))
46- }
47- }
48- forceAndCleanArtefacts.transform(unpickled)
49- case expr : TastyTreeExpr [Tree ] @ unchecked => healOwner(expr.tree)
50- }
38+ def quotedExprToTree [T ](expr : quoted.Expr [T ])(implicit ctx : Context ): Tree =
39+ healOwner(expr.asInstanceOf [TastyTreeExpr [Tree ]].tree)
5140
5241 /** Transform the expression into its fully spliced TypeTree */
53- def quotedTypeToTree (expr : quoted.Type [_])(implicit ctx : Context ): Tree = expr match {
54- case expr : TastyType [_] =>
55- unpickleType(expr) match {
56- case Block (aliases, tpt) =>
57- // `@quoteTypeTag type` aliasses are not required after unpickling
58- tpt
59- case tpt => tpt
60- }
61- case expr : TreeType [Tree ] @ unchecked => healOwner(expr.typeTree)
62- }
42+ def quotedTypeToTree (expr : quoted.Type [_])(implicit ctx : Context ): Tree =
43+ healOwner(expr.asInstanceOf [TreeType [Tree ]].typeTree)
6344
6445 private def dealiasTypeTags (tp : Type )(implicit ctx : Context ): Type = new TypeMap () {
6546 override def apply (tp : Type ): Type = {
@@ -72,15 +53,31 @@ object PickledQuotes {
7253 }.apply(tp)
7354
7455 /** Unpickle the tree contained in the TastyExpr */
75- private def unpickleExpr (expr : TastyExpr [_])(implicit ctx : Context ): Tree = {
76- val tastyBytes = TastyString .unpickle(expr.tasty)
77- unpickle(tastyBytes, expr.args, isType = false )(ctx.addMode(Mode .ReadPositions ))
56+ def unpickleExpr (tasty : PickledQuote , args : PickledExprArgs )(implicit ctx : Context ): Tree = {
57+ val tastyBytes = TastyString .unpickle(tasty)
58+ val unpickled = unpickle(tastyBytes, args, isType = false )(ctx.addMode(Mode .ReadPositions ))
59+ /** Force unpickling of the tree, removes the spliced type `@quotedTypeTag type` definitions and dealiases references to `@quotedTypeTag type` */
60+ val forceAndCleanArtefacts = new TreeMap {
61+ override def transform (tree : tpd.Tree )(implicit ctx : Context ): tpd.Tree = tree match {
62+ case Block (stat :: rest, expr1) if stat.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ) =>
63+ assert(rest.forall { case tdef : TypeDef => tdef.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ) })
64+ transform(expr1)
65+ case tree => super .transform(tree).withType(dealiasTypeTags(tree.tpe))
66+ }
67+ }
68+ forceAndCleanArtefacts.transform(unpickled)
7869 }
7970
8071 /** Unpickle the tree contained in the TastyType */
81- private def unpickleType (ttpe : TastyType [_])(implicit ctx : Context ): Tree = {
82- val tastyBytes = TastyString .unpickle(ttpe.tasty)
83- unpickle(tastyBytes, ttpe.args, isType = true )(ctx.addMode(Mode .ReadPositions ))
72+ def unpickleType (tasty : PickledQuote , args : PickledTypeArgs )(implicit ctx : Context ): Tree = {
73+ val tastyBytes = TastyString .unpickle(tasty)
74+ val unpickled = unpickle(tastyBytes, args, isType = true )(ctx.addMode(Mode .ReadPositions ))
75+ unpickled match {
76+ case Block (aliases, tpt) =>
77+ // `@quoteTypeTag type` aliasses are not required after unpickling
78+ tpt
79+ case tpt => tpt
80+ }
8481 }
8582
8683 // TASTY picklingtests/pos/quoteTest.scala
0 commit comments