@@ -17,24 +17,22 @@ import dotty.tools.dotc.core.tasty.DottyUnpickler
17
17
import dotty .tools .dotc .core .tasty .TreeUnpickler .UnpickleMode
18
18
import dotty .tools .dotc .report
19
19
20
- import dotty .tools .tasty .TastyString
21
-
22
20
import scala .reflect .ClassTag
23
21
24
- import scala .internal .quoted .Unpickler . _
22
+ import scala .internal .quoted .PickledQuote
25
23
import scala .quoted .QuoteContext
26
24
import scala .collection .mutable
27
25
28
26
object PickledQuotes {
29
27
import tpd ._
30
28
31
29
/** Pickle the tree of the quote into strings */
32
- def pickleQuote (tree : Tree )(using Context ): PickledQuote =
30
+ def pickleQuote (tree : Tree )(using Context ): List [ String ] =
33
31
if (ctx.reporter.hasErrors) Nil
34
32
else {
35
33
assert(! tree.isInstanceOf [Hole ]) // Should not be pickled as it represents `'{$x}` which should be optimized to `x`
36
34
val pickled = pickle(tree)
37
- TastyString .pickle(pickled)
35
+ scala.internal.quoted. TastyString .pickle(pickled)
38
36
}
39
37
40
38
/** Transform the expression into its fully spliced Tree */
@@ -52,27 +50,25 @@ object PickledQuotes {
52
50
}
53
51
54
52
/** Unpickle the tree contained in the TastyExpr */
55
- def unpickleExpr (tasty : PickledQuote , splices : PickledArgs )(using Context ): Tree = {
56
- val tastyBytes = TastyString .unpickle(tasty)
53
+ def unpickleTerm (pickledQuote : PickledQuote )(using Context ): Tree = {
57
54
val unpickled = withMode(Mode .ReadPositions )(
58
- unpickle(tastyBytes, splices , isType = false ))
55
+ unpickle(pickledQuote , isType = false ))
59
56
val Inlined (call, Nil , expnasion) = unpickled
60
57
val inlineCtx = inlineContext(call)
61
- val expansion1 = spliceTypes(expnasion, splices )(using inlineCtx)
62
- val expansion2 = spliceTerms(expansion1, splices )(using inlineCtx)
58
+ val expansion1 = spliceTypes(expnasion, pickledQuote )(using inlineCtx)
59
+ val expansion2 = spliceTerms(expansion1, pickledQuote )(using inlineCtx)
63
60
cpy.Inlined (unpickled)(call, Nil , expansion2)
64
61
}
65
62
66
63
/** Unpickle the tree contained in the TastyType */
67
- def unpickleType (tasty : PickledQuote , args : PickledArgs )(using Context ): Tree = {
68
- val tastyBytes = TastyString .unpickle(tasty)
64
+ def unpickleTypeTree (pickledQuote : PickledQuote )(using Context ): Tree = {
69
65
val unpickled = withMode(Mode .ReadPositions )(
70
- unpickle(tastyBytes, args , isType = true ))
71
- spliceTypes(unpickled, args )
66
+ unpickle(pickledQuote , isType = true ))
67
+ spliceTypes(unpickled, pickledQuote )
72
68
}
73
69
74
70
/** Replace all term holes with the spliced terms */
75
- private def spliceTerms (tree : Tree , splices : PickledArgs )(using Context ): Tree = {
71
+ private def spliceTerms (tree : Tree , pickledQuote : PickledQuote )(using Context ): Tree = {
76
72
val evaluateHoles = new TreeMap {
77
73
override def transform (tree : tpd.Tree )(using Context ): tpd.Tree = tree match {
78
74
case Hole (isTerm, idx, args) =>
@@ -81,8 +77,7 @@ object PickledQuotes {
81
77
else new scala.internal.quoted.Type (arg, QuoteContextImpl .scopeId)
82
78
}
83
79
if isTerm then
84
- val splice1 = splices(idx).asInstanceOf [Seq [Any ] => QuoteContext ?=> quoted.Expr [? ]]
85
- val quotedExpr = splice1(reifiedArgs)(using dotty.tools.dotc.quoted.QuoteContextImpl ())
80
+ val quotedExpr = pickledQuote.exprSplice(idx)(reifiedArgs)(dotty.tools.dotc.quoted.QuoteContextImpl ())
86
81
val filled = PickledQuotes .quotedExprToTree(quotedExpr)
87
82
88
83
// We need to make sure a hole is created with the source file of the surrounding context, even if
@@ -92,7 +87,7 @@ object PickledQuotes {
92
87
else
93
88
// Replaces type holes generated by ReifyQuotes (non-spliced types).
94
89
// These are types defined in a quote and used at the same level in a nested quote.
95
- val quotedType = splices (idx). asInstanceOf [ Seq [ Any ] => quoted. Type [ ? ]] (reifiedArgs)
90
+ val quotedType = pickledQuote.typeSplice (idx)(reifiedArgs)
96
91
PickledQuotes .quotedTypeToTree(quotedType)
97
92
case tree : Select =>
98
93
// Retain selected members
@@ -127,15 +122,15 @@ object PickledQuotes {
127
122
}
128
123
129
124
/** Replace all type holes generated with the spliced types */
130
- private def spliceTypes (tree : Tree , splices : PickledArgs )(using Context ): Tree = {
125
+ private def spliceTypes (tree : Tree , pickledQuote : PickledQuote )(using Context ): Tree = {
131
126
tree match
132
127
case Block (stat :: rest, expr1) if stat.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ) =>
133
128
val typeSpliceMap = (stat :: rest).iterator.map {
134
129
case tdef : TypeDef =>
135
130
assert(tdef.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ))
136
131
val tree = tdef.rhs match
137
132
case TypeBoundsTree (_, Hole (_, idx, args), _) =>
138
- val quotedType = splices (idx). asInstanceOf [ Seq [ Any ] => quoted. Type [ ? ]] (args)
133
+ val quotedType = pickledQuote.typeSplice (idx)(args)
139
134
PickledQuotes .quotedTypeToTree(quotedType)
140
135
case TypeBoundsTree (_, tpt, _) =>
141
136
tpt
@@ -181,7 +176,8 @@ object PickledQuotes {
181
176
}
182
177
183
178
/** Unpickle TASTY bytes into it's tree */
184
- private def unpickle (bytes : Array [Byte ], splices : Seq [Any ], isType : Boolean )(using Context ): Tree = {
179
+ private def unpickle (pickledQuote : PickledQuote , isType : Boolean )(using Context ): Tree = {
180
+ val bytes = pickledQuote.bytes()
185
181
quotePickling.println(s " **** unpickling quote from TASTY \n ${new TastyPrinter (bytes).printContents()}" )
186
182
187
183
val mode = if (isType) UnpickleMode .TypeTree else UnpickleMode .Term
0 commit comments