Skip to content

Commit 627b558

Browse files
committed
Refactor way we handle hole contents and tags while pickling quotes
1 parent c27f870 commit 627b558

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ import scala.annotation.constructorOnly
3333
* val x1: U1 = ???
3434
* val x2: U2 = ???
3535
* ...
36-
* {{{ 3 | x1 | contents0 | T0 }}} // hole
36+
* {{{ 3 | x1 | holeContents0 | T0 }}} // hole
3737
* ...
38-
* {{{ 4 | x2 | contents1 | T1 }}} // hole
38+
* {{{ 4 | x2 | holeContents1 | T1 }}} // hole
3939
* ...
40-
* {{{ 5 | x1, x2 | contents2 | T2 }}} // hole
40+
* {{{ 5 | x1, x2 | holeContents2 | T2 }}} // hole
4141
* ...
4242
* }
4343
* ```
@@ -93,25 +93,25 @@ class PickleQuotes extends MacroTransform {
9393
override def transform(tree: tpd.Tree)(using Context): tpd.Tree =
9494
tree match
9595
case Apply(Select(quote: Quote, nme.apply), List(quotes)) =>
96-
val (contents, quote1) = makeHoles(quote)
96+
val (holeContents, quote1) = extractHolesContents(quote)
9797
val quote2 = encodeTypeArgs(quote1)
98-
val contents1 = contents.map(transform(_)) ::: quote.tags
99-
PickleQuotes.pickle(quote2, quotes, contents1)
98+
val holeContents1 = holeContents.map(transform(_))
99+
PickleQuotes.pickle(quote2, quotes, holeContents1)
100100
case tree: DefDef if !tree.rhs.isEmpty && tree.symbol.isInlineMethod =>
101101
tree
102102
case _ =>
103103
super.transform(tree)
104104
}
105105

106-
private def makeHoles(quote: tpd.Quote)(using Context): (List[Tree], tpd.Quote) =
106+
private def extractHolesContents(quote: tpd.Quote)(using Context): (List[Tree], tpd.Quote) =
107107
class HoleContentExtractor extends Transformer:
108-
private val contents = List.newBuilder[Tree]
108+
private val holeContents = List.newBuilder[Tree]
109109
override def transform(tree: tpd.Tree)(using Context): tpd.Tree =
110110
tree match
111111
case tree @ Hole(isTerm, _, _, content) =>
112112
assert(isTerm)
113113
assert(!content.isEmpty)
114-
contents += content
114+
holeContents += content
115115
val holeType = getTermHoleType(tree.tpe)
116116
val hole = untpd.cpy.Hole(tree)(content = EmptyTree).withType(holeType)
117117
cpy.Inlined(tree)(EmptyTree, Nil, hole)
@@ -147,10 +147,10 @@ class PickleQuotes extends MacroTransform {
147147
mapOver(tp)
148148
}
149149

150-
/** Get the contents of the transformed tree */
150+
/** Get the holeContents of the transformed tree */
151151
def getContents() =
152-
val res = contents.result
153-
contents.clear()
152+
val res = holeContents.result
153+
holeContents.clear()
154154
res
155155
end HoleContentExtractor
156156

@@ -159,7 +159,7 @@ class PickleQuotes extends MacroTransform {
159159
val quote1 = cpy.Quote(quote)(body1, quote.tags)
160160

161161
(holeMaker.getContents(), quote1)
162-
end makeHoles
162+
end extractHolesContents
163163

164164
/** Encode quote tags as holes in the quote body.
165165
*
@@ -236,7 +236,7 @@ object PickleQuotes {
236236
val name: String = "pickleQuotes"
237237
val description: String = "turn quoted trees into explicit run-time data structures"
238238

239-
def pickle(quote: Quote, quotes: Tree, contents: List[Tree])(using Context) = {
239+
def pickle(quote: Quote, quotes: Tree, holeContents: List[Tree])(using Context) = {
240240
val body = quote.body
241241
val bodyType = quote.bodyType
242242

@@ -334,27 +334,22 @@ object PickleQuotes {
334334
case x :: Nil => Literal(Constant(x))
335335
case xs => tpd.mkList(xs.map(x => Literal(Constant(x))), TypeTree(defn.StringType))
336336

337-
// TODO split holes earlier into types and terms. This all holes in each category can have consecutive indices
338-
val (typeSplices, termSplices) = contents.zipWithIndex.partition {
339-
_._1.tpe.derivesFrom(defn.QuotedTypeClass)
340-
}
341-
342337
// This and all closures in typeSplices are removed by the BetaReduce phase
343338
val types =
344-
if typeSplices.isEmpty then Literal(Constant(null)) // keep pickled quote without contents as small as possible
345-
else SeqLiteral(typeSplices.map(_._1), TypeTree(defn.QuotedTypeClass.typeRef.appliedTo(WildcardType)))
339+
if quote.tags.isEmpty then Literal(Constant(null)) // keep pickled quote without holeContents as small as possible
340+
else SeqLiteral(quote.tags, TypeTree(defn.QuotedTypeClass.typeRef.appliedTo(WildcardType)))
346341

347342
// This and all closures in termSplices are removed by the BetaReduce phase
348343
val termHoles =
349-
if termSplices.isEmpty then Literal(Constant(null)) // keep pickled quote without contents as small as possible
344+
if holeContents.isEmpty then Literal(Constant(null)) // keep pickled quote without holeContents as small as possible
350345
else
351346
Lambda(
352347
MethodType(
353348
List(nme.idx, nme.contents, nme.quotes).map(name => UniqueName.fresh(name).toTermName),
354349
List(defn.IntType, defn.SeqType.appliedTo(defn.AnyType), defn.QuotesClass.typeRef),
355350
defn.QuotedExprClass.typeRef.appliedTo(defn.AnyType)),
356351
args =>
357-
val cases = termSplices.map { case (splice, idx) =>
352+
val cases = holeContents.zipWithIndex.map { case (splice, idx) =>
358353
val defn.FunctionOf(argTypes, defn.FunctionOf(quotesType :: _, _, _), _) = splice.tpe: @unchecked
359354
val rhs = {
360355
val spliceArgs = argTypes.zipWithIndex.map { (argType, i) =>
@@ -413,7 +408,7 @@ object PickleQuotes {
413408
case _ => None
414409

415410
if body.isType then
416-
if contents.isEmpty && body.symbol.isPrimitiveValueClass then taggedType()
411+
if holeContents.isEmpty && body.symbol.isPrimitiveValueClass then taggedType()
417412
else pickleAsTasty()
418413
else
419414
getLiteral(body) match

0 commit comments

Comments
 (0)