Skip to content

Commit 3a68a66

Browse files
committed
Make reflect printers depend on quote context
1 parent df52911 commit 3a68a66

File tree

5 files changed

+193
-194
lines changed

5 files changed

+193
-194
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
6565
def pos: Position = self.sourcePos
6666
def symbol: Symbol = self.symbol
6767
def showExtractors: String =
68-
new ExtractorsPrinter[reflect.type](reflect).showTree(self)
68+
new ExtractorsPrinter().showTree(using QuoteContextImpl.this)(self)
6969
def show: String =
7070
self.showWith(SyntaxHighlight.plain)
7171
def showWith(syntaxHighlight: SyntaxHighlight): String =
72-
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showTree(self)
72+
new SourceCodePrinter(syntaxHighlight).showTree(using QuoteContextImpl.this)(self)
7373
def isExpr: Boolean =
7474
self match
7575
case TermTypeTest(self) =>
@@ -1589,13 +1589,13 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
15891589
object TypeMethodsImpl extends TypeMethods:
15901590
extension (self: TypeRepr):
15911591
def showExtractors: String =
1592-
new ExtractorsPrinter[reflect.type](reflect).showType(self)
1592+
new ExtractorsPrinter().showType(using QuoteContextImpl.this)(self)
15931593

15941594
def show: String =
15951595
self.showWith(SyntaxHighlight.plain)
15961596

15971597
def showWith(syntaxHighlight: SyntaxHighlight): String =
1598-
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showType(self)
1598+
new SourceCodePrinter(syntaxHighlight).showType(using QuoteContextImpl.this)(self)
15991599

16001600
def seal: scala.quoted.Type[_] =
16011601
new scala.internal.quoted.Type(Inferred(self), QuoteContextImpl.this.hashCode)
@@ -2172,11 +2172,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21722172
extension (self: Constant):
21732173
def value: Any = self.value
21742174
def showExtractors: String =
2175-
new ExtractorsPrinter[reflect.type](reflect).showConstant(self)
2175+
new ExtractorsPrinter().showConstant(using QuoteContextImpl.this)(self)
21762176
def show: String =
21772177
self.showWith(SyntaxHighlight.plain)
21782178
def showWith(syntaxHighlight: SyntaxHighlight): String =
2179-
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showConstant(self)
2179+
new SourceCodePrinter(syntaxHighlight).showConstant(using QuoteContextImpl.this)(self)
21802180
end extension
21812181
end ConstantMethodsImpl
21822182

@@ -2395,11 +2395,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
23952395
def children: List[Symbol] = self.denot.children
23962396

23972397
def showExtractors: String =
2398-
new ExtractorsPrinter[reflect.type](reflect).showSymbol(self)
2398+
new ExtractorsPrinter().showSymbol(using QuoteContextImpl.this)(self)
23992399
def show: String =
24002400
self.showWith(SyntaxHighlight.plain)
24012401
def showWith(syntaxHighlight: SyntaxHighlight): String =
2402-
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showSymbol(self)
2402+
new SourceCodePrinter(syntaxHighlight).showSymbol(using QuoteContextImpl.this)(self)
24032403

24042404
end extension
24052405

@@ -2531,11 +2531,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
25312531
def |(that: Flags): Flags = dotc.core.Flags.extension_|(self)(that)
25322532
def &(that: Flags): Flags = dotc.core.Flags.extension_&(self)(that)
25332533
def showExtractors: String =
2534-
new ExtractorsPrinter[reflect.type](reflect).showFlags(self)
2534+
new ExtractorsPrinter().showFlags(using QuoteContextImpl.this)(self)
25352535
def show: String =
25362536
self.showWith(SyntaxHighlight.plain)
25372537
def showWith(syntaxHighlight: SyntaxHighlight): String =
2538-
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showFlags(self)
2538+
new SourceCodePrinter(syntaxHighlight).showFlags(using QuoteContextImpl.this)(self)
25392539
end extension
25402540
end FlagsMethodsImpl
25412541

library/src/scala/tasty/reflect/ExtractorsPrinter.scala

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package scala.tasty
22
package reflect
33

4-
class ExtractorsPrinter[R <: Reflection & Singleton](val reflect: R) extends Printer[R] {
5-
import reflect._
4+
import scala.quoted._
65

7-
def showTree(tree: Tree): String =
8-
new Buffer().visitTree(tree).result()
6+
class ExtractorsPrinter extends Printer {
97

10-
def showType(tpe: TypeRepr): String =
11-
new Buffer().visitType(tpe).result()
8+
def showTree(using QuoteContext)(tree: qctx.reflect.Tree): String =
9+
new Buffer[qctx.type]().visitTree(tree).result()
1210

13-
def showConstant(const: Constant): String =
14-
new Buffer().visitConstant(const).result()
11+
def showType(using QuoteContext)(tpe: qctx.reflect.TypeRepr): String =
12+
new Buffer[qctx.type]().visitType(tpe).result()
1513

16-
def showSymbol(symbol: Symbol): String =
17-
new Buffer().visitSymbol(symbol).result()
14+
def showConstant(using QuoteContext)(const: qctx.reflect.Constant): String =
15+
new Buffer[qctx.type]().visitConstant(const).result()
1816

19-
def showFlags(flags: Flags): String = {
17+
def showSymbol(using QuoteContext)(symbol: qctx.reflect.Symbol): String =
18+
new Buffer[qctx.type]().visitSymbol(symbol).result()
19+
20+
def showFlags(using QuoteContext)(flags: qctx.reflect.Flags): String = {
21+
import qctx.reflect._
2022
val flagList = List.newBuilder[String]
2123
if (flags.is(Flags.Abstract)) flagList += "Flags.Abstract"
2224
if (flags.is(Flags.Artifact)) flagList += "Flags.Artifact"
@@ -55,13 +57,14 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val reflect: R) extends Pri
5557
flagList.result().mkString(" | ")
5658
}
5759

58-
private class Buffer { self =>
60+
private class Buffer[QCtx <: QuoteContext & Singleton](using val qctx: QCtx) { self =>
61+
import qctx.reflect._
5962

6063
private val sb: StringBuilder = new StringBuilder
6164

6265
def result(): String = sb.result()
6366

64-
def visitTree(x: Tree): Buffer = x match {
67+
def visitTree(x: Tree): this.type = x match {
6568
case Ident(name) =>
6669
this += "Ident(\"" += name += "\")"
6770
case Select(qualifier, name) =>
@@ -164,7 +167,7 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val reflect: R) extends Pri
164167
this += "Alternative(" ++= patterns += ")"
165168
}
166169

167-
def visitConstant(x: Constant): Buffer = x match {
170+
def visitConstant(x: Constant): this.type = x match {
168171
case Constant.Unit() => this += "Constant.Unit()"
169172
case Constant.Null() => this += "Constant.Null()"
170173
case Constant.Boolean(value) => this += "Constant.Boolean(" += value += ")"
@@ -181,7 +184,7 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val reflect: R) extends Pri
181184
visitType(value) += ")"
182185
}
183186

184-
def visitType(x: TypeRepr): Buffer = x match {
187+
def visitType(x: TypeRepr): this.type = x match {
185188
case ConstantType(value) =>
186189
this += "ConstantType(" += value += ")"
187190
case TermRef(qual, name) =>
@@ -225,71 +228,71 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val reflect: R) extends Pri
225228
this += "NoPrefix()"
226229
}
227230

228-
def visitSignature(sig: Signature): Buffer = {
231+
def visitSignature(sig: Signature): this.type = {
229232
val Signature(params, res) = sig
230233
this += "Signature(" ++= params.map(_.toString) += ", " += res += ")"
231234
}
232235

233-
def visitImportSelector(sel: ImportSelector): Buffer = sel match {
236+
def visitImportSelector(sel: ImportSelector): this.type = sel match {
234237
case SimpleSelector(id) => this += "SimpleSelector(" += id += ")"
235238
case RenameSelector(id1, id2) => this += "RenameSelector(" += id1 += ", " += id2 += ")"
236239
case OmitSelector(id) => this += "OmitSelector(" += id += ")"
237240
}
238241

239-
def visitSymbol(x: Symbol): Buffer =
242+
def visitSymbol(x: Symbol): this.type =
240243
if x.isPackageDef then this += "IsPackageDefSymbol(<" += x.fullName += ">)"
241244
else if x.isClassDef then this += "IsClassDefSymbol(<" += x.fullName += ">)"
242245
else if x.isDefDef then this += "IsDefDefSymbol(<" += x.fullName += ">)"
243246
else if x.isValDef then this += "IsValDefSymbol(<" += x.fullName += ">)"
244247
else if x.isTypeDef then this += "IsTypeDefSymbol(<" += x.fullName += ">)"
245248
else { assert(x.isNoSymbol); this += "NoSymbol()" }
246249

247-
def +=(x: Boolean): Buffer = { sb.append(x); this }
248-
def +=(x: Byte): Buffer = { sb.append(x); this }
249-
def +=(x: Short): Buffer = { sb.append(x); this }
250-
def +=(x: Int): Buffer = { sb.append(x); this }
251-
def +=(x: Long): Buffer = { sb.append(x); this }
252-
def +=(x: Float): Buffer = { sb.append(x); this }
253-
def +=(x: Double): Buffer = { sb.append(x); this }
254-
def +=(x: Char): Buffer = { sb.append(x); this }
255-
def +=(x: String): Buffer = { sb.append(x); this }
250+
def +=(x: Boolean): this.type = { sb.append(x); this }
251+
def +=(x: Byte): this.type = { sb.append(x); this }
252+
def +=(x: Short): this.type = { sb.append(x); this }
253+
def +=(x: Int): this.type = { sb.append(x); this }
254+
def +=(x: Long): this.type = { sb.append(x); this }
255+
def +=(x: Float): this.type = { sb.append(x); this }
256+
def +=(x: Double): this.type = { sb.append(x); this }
257+
def +=(x: Char): this.type = { sb.append(x); this }
258+
def +=(x: String): this.type = { sb.append(x); this }
256259

257-
def ++=(xs: List[String]): Buffer = visitList[String](xs, +=)
260+
def ++=(xs: List[String]): this.type = visitList[String](xs, +=)
258261

259-
private implicit class StringOps(buff: Buffer) {
260-
def +=(x: Option[String]): Buffer = { visitOption(x, y => buff += "\"" += y += "\""); buff }
262+
private implicit class StringOps(buff: self.type) {
263+
def +=(x: Option[String]): self.type = { visitOption(x, y => buff += "\"" += y += "\""); buff }
261264
}
262265

263-
private implicit class TreeOps(buff: Buffer) {
264-
def +=(x: Tree): Buffer = { visitTree(x); buff }
265-
def +=(x: Option[Tree]): Buffer = { visitOption(x, visitTree); buff }
266-
def ++=(x: List[Tree]): Buffer = { visitList(x, visitTree); buff }
267-
def +++=(x: List[List[Tree]]): Buffer = { visitList(x, ++=); buff }
266+
private implicit class TreeOps(buff: self.type) {
267+
def +=(x: Tree): self.type = { visitTree(x); buff }
268+
def +=(x: Option[Tree]): self.type = { visitOption(x, visitTree); buff }
269+
def ++=(x: List[Tree]): self.type = { visitList(x, visitTree); buff }
270+
def +++=(x: List[List[Tree]]): self.type = { visitList(x, ++=); buff }
268271
}
269272

270-
private implicit class ConstantOps(buff: Buffer) {
271-
def +=(x: Constant): Buffer = { visitConstant(x); buff }
273+
private implicit class ConstantOps(buff: self.type) {
274+
def +=(x: Constant): self.type = { visitConstant(x); buff }
272275
}
273276

274-
private implicit class TypeOps(buff: Buffer) {
275-
def +=(x: TypeRepr): Buffer = { visitType(x); buff }
276-
def +=(x: Option[TypeRepr]): Buffer = { visitOption(x, visitType); buff }
277-
def ++=(x: List[TypeRepr]): Buffer = { visitList(x, visitType); buff }
277+
private implicit class TypeOps(buff: self.type) {
278+
def +=(x: TypeRepr): self.type = { visitType(x); buff }
279+
def +=(x: Option[TypeRepr]): self.type = { visitOption(x, visitType); buff }
280+
def ++=(x: List[TypeRepr]): self.type = { visitList(x, visitType); buff }
278281
}
279282

280-
private implicit class SignatureOps(buff: Buffer) {
281-
def +=(x: Option[Signature]): Buffer = { visitOption(x, visitSignature); buff }
283+
private implicit class SignatureOps(buff: self.type) {
284+
def +=(x: Option[Signature]): self.type = { visitOption(x, visitSignature); buff }
282285
}
283286

284-
private implicit class ImportSelectorOps(buff: Buffer) {
285-
def ++=(x: List[ImportSelector]): Buffer = { visitList(x, visitImportSelector); buff }
287+
private implicit class ImportSelectorOps(buff: self.type) {
288+
def ++=(x: List[ImportSelector]): self.type = { visitList(x, visitImportSelector); buff }
286289
}
287290

288-
private implicit class SymbolOps(buff: Buffer) {
289-
def +=(x: Symbol): Buffer = { visitSymbol(x); buff }
291+
private implicit class SymbolOps(buff: self.type) {
292+
def +=(x: Symbol): self.type = { visitSymbol(x); buff }
290293
}
291294

292-
private def visitOption[U](opt: Option[U], visit: U => Buffer): Buffer = opt match {
295+
private def visitOption[U](opt: Option[U], visit: U => this.type): this.type = opt match {
293296
case Some(x) =>
294297
this += "Some("
295298
visit(x)
@@ -298,7 +301,7 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val reflect: R) extends Pri
298301
this += "None"
299302
}
300303

301-
private def visitList[U](list: List[U], visit: U => Buffer): Buffer = list match {
304+
private def visitList[U](list: List[U], visit: U => this.type): this.type = list match {
302305
case x0 :: xs =>
303306
this += "List("
304307
visit(x0)
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
package scala.tasty
22
package reflect
33

4-
// TODO use QuoteContext instead of Reflection
5-
trait Printer[R <: Reflection & Singleton] {
6-
7-
/** Instance of reflection interface */
8-
val reflect: R
9-
import reflect._
4+
import scala.quoted._
105

6+
trait Printer {
117
/** Show a String representation of a reflect.Tree */
12-
def showTree(tree: Tree): String
8+
def showTree(using QuoteContext)(tree: qctx.reflect.Tree): String
139

1410
/** Show a String representation of a reflect.Type */
15-
def showType(tpe: TypeRepr): String
11+
def showType(using QuoteContext)(tpe: qctx.reflect.TypeRepr): String
1612

1713
/** Show a String representation of a reflect.Constant */
18-
def showConstant(const: Constant): String
14+
def showConstant(using QuoteContext)(const: qctx.reflect.Constant): String
1915

2016
/** Show a String representation of a reflect.Symbol */
21-
def showSymbol(symbol: Symbol): String
17+
def showSymbol(using QuoteContext)(symbol: qctx.reflect.Symbol): String
2218

2319
/** Show a String representation of a reflect.Flags */
24-
def showFlags(flags: Flags): String
20+
def showFlags(using QuoteContext)(flags: qctx.reflect.Flags): String
2521
}

0 commit comments

Comments
 (0)