-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Extractors.scala
351 lines (321 loc) · 15.8 KB
/
Extractors.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package scala.quoted
package runtime.impl.printers
import scala.quoted.*
object Extractors {
def showTree(using Quotes)(tree: quotes.reflect.Tree): String =
new ExtractorsPrinter[quotes.type]().visitTree(tree).result()
def showType(using Quotes)(tpe: quotes.reflect.TypeRepr): String =
new ExtractorsPrinter[quotes.type]().visitType(tpe).result()
def showConstant(using Quotes)(const: quotes.reflect.Constant): String =
new ExtractorsPrinter[quotes.type]().visitConstant(const).result()
def showSymbol(using Quotes)(symbol: quotes.reflect.Symbol): String =
new ExtractorsPrinter[quotes.type]().visitSymbol(symbol).result()
def showFlags(using Quotes)(flags: quotes.reflect.Flags): String = {
import quotes.reflect.*
val flagList = List.newBuilder[String]
if (flags.is(Flags.Abstract)) flagList += "Flags.Abstract"
if (flags.is(Flags.Artifact)) flagList += "Flags.Artifact"
if (flags.is(Flags.Case)) flagList += "Flags.Case"
if (flags.is(Flags.CaseAccessor)) flagList += "Flags.CaseAccessor"
if (flags.is(Flags.Contravariant)) flagList += "Flags.Contravariant"
if (flags.is(Flags.Covariant)) flagList += "Flags.Covariant"
if (flags.is(Flags.Deferred)) flagList += "Flags.Deferred"
if (flags.is(Flags.Enum)) flagList += "Flags.Enum"
if (flags.is(Flags.Erased)) flagList += "Flags.Erased"
if (flags.is(Flags.Exported)) flagList += "Flags.Exported"
if (flags.is(Flags.ExtensionMethod)) flagList += "Flags.ExtensionMethod"
if (flags.is(Flags.FieldAccessor)) flagList += "Flags.FieldAccessor"
if (flags.is(Flags.Final)) flagList += "Flags.Final"
if (flags.is(Flags.Given)) flagList += "Flags.Given"
if (flags.is(Flags.HasDefault)) flagList += "Flags.HasDefault"
if (flags.is(Flags.Implicit)) flagList += "Flags.Implicit"
if (flags.is(Flags.Infix)) flagList += "Flags.Infix"
if (flags.is(Flags.Inline)) flagList += "Flags.Inline"
if (flags.is(Flags.JavaDefined)) flagList += "Flags.JavaDefined"
if (flags.is(Flags.JavaStatic)) flagList += "Flags.JavaStatic"
if (flags.is(Flags.Lazy)) flagList += "Flags.Lazy"
if (flags.is(Flags.Local)) flagList += "Flags.Local"
if (flags.is(Flags.Macro)) flagList += "Flags.Macro"
if (flags.is(Flags.Method)) flagList += "Flags.Method"
if (flags.is(Flags.Module)) flagList += "Flags.Module"
if (flags.is(Flags.Mutable)) flagList += "Flags.Mutable"
if (flags.is(Flags.NoInits)) flagList += "Flags.NoInits"
if (flags.is(Flags.Override)) flagList += "Flags.Override"
if (flags.is(Flags.Package)) flagList += "Flags.Package"
if (flags.is(Flags.Param)) flagList += "Flags.Param"
if (flags.is(Flags.ParamAccessor)) flagList += "Flags.ParamAccessor"
if (flags.is(Flags.Private)) flagList += "Flags.Private"
if (flags.is(Flags.PrivateLocal)) flagList += "Flags.PrivateLocal"
if (flags.is(Flags.Protected)) flagList += "Flags.Protected"
if (flags.is(Flags.Scala2x)) flagList += "Flags.Scala2x"
if (flags.is(Flags.Sealed)) flagList += "Flags.Sealed"
if (flags.is(Flags.StableRealizable)) flagList += "Flags.StableRealizable"
if (flags.is(Flags.Synthetic)) flagList += "Flags.Synthetic"
if (flags.is(Flags.Trait)) flagList += "Flags.Trait"
if (flags.is(Flags.Transparent)) flagList += "Flags.Transparent"
flagList.result().mkString(" | ")
}
private class ExtractorsPrinter[Q <: Quotes & Singleton](using val quotes: Q) { self =>
import quotes.reflect.*
private val sb: StringBuilder = new StringBuilder
def result(): String = sb.result()
def visitTree(x: Tree): this.type = x match {
case tree: Ref =>
tree match
case Wildcard() =>
this += "Wildcard()"
case Ident(name) =>
this += "Ident(\"" += name += "\")"
case Select(qualifier, name) =>
this += "Select(" += qualifier += ", \"" += name += "\")"
case This(qual) =>
this += "This(" += qual += ")"
case Super(qual, mix) =>
this += "Super(" += qual += ", " += mix += ")"
case Apply(fun, args) =>
this += "Apply(" += fun += ", " ++= args += ")"
case TypeApply(fun, args) =>
this += "TypeApply(" += fun += ", " ++= args += ")"
case Literal(const) =>
this += "Literal(" += const += ")"
case New(tpt) =>
this += "New(" += tpt += ")"
case Typed(expr, tpt) =>
this += "Typed(" += expr += ", " += tpt += ")"
case NamedArg(name, arg) =>
this += "NamedArg(\"" += name += "\", " += arg += ")"
case Assign(lhs, rhs) =>
this += "Assign(" += lhs += ", " += rhs += ")"
case Block(stats, expr) =>
this += "Block(" ++= stats += ", " += expr += ")"
case If(cond, thenp, elsep) =>
this += "If(" += cond += ", " += thenp += ", " += elsep += ")"
case Closure(meth, tpt) =>
this += "Closure(" += meth += ", " += tpt += ")"
case Match(selector, cases) =>
this += "Match(" += selector += ", " ++= cases += ")"
case SummonFrom(cases) =>
this += "SummonFrom(" ++= cases += ")"
case Return(expr, from) =>
this += "Return(" += expr += ", " += from += ")"
case While(cond, body) =>
this += "While(" += cond += ", " += body += ")"
case Try(block, handlers, finalizer) =>
this += "Try(" += block += ", " ++= handlers += ", " += finalizer += ")"
case Repeated(elems, elemtpt) =>
this += "Repeated(" ++= elems += ", " += elemtpt += ")"
case Inlined(call, bindings, expansion) =>
this += "Inlined("
visitOption(call, visitTree)
this += ", " ++= bindings += ", " += expansion += ")"
case ValDef(name, tpt, rhs) =>
this += "ValDef(\"" += name += "\", " += tpt += ", " += rhs += ")"
case DefDef(name, paramsClauses, returnTpt, rhs) =>
this += "DefDef(\"" += name += "\", " ++= paramsClauses += ", " += returnTpt += ", " += rhs += ")"
case TypeDef(name, rhs) =>
this += "TypeDef(\"" += name += "\", " += rhs += ")"
case ClassDef(name, constr, parents, self, body) =>
this += "ClassDef(\"" += name += "\", " += constr += ", "
visitList[Tree](parents, visitTree)
this += ", " += self += ", " ++= body += ")"
case Import(expr, selectors) =>
this += "Import(" += expr += ", " ++= selectors += ")"
case Export(expr, selectors) =>
this += "Export(" += expr += ", " ++= selectors += ")"
case PackageClause(pid, stats) =>
this += "PackageClause(" += pid += ", " ++= stats += ")"
case Inferred() =>
this += "Inferred()"
case TypeIdent(name) =>
this += "TypeIdent(\"" += name += "\")"
case TypeSelect(qualifier, name) =>
this += "TypeSelect(" += qualifier += ", \"" += name += "\")"
case TypeProjection(qualifier, name) =>
this += "Projection(" += qualifier += ", \"" += name += "\")"
case Singleton(ref) =>
this += "Singleton(" += ref += ")"
case Refined(tpt, refinements) =>
this += "Refined(" += tpt += ", " ++= refinements += ")"
case Applied(tpt, args) =>
this += "Applied(" += tpt += ", " ++= args += ")"
case ByName(result) =>
this += "ByName(" += result += ")"
case Annotated(arg, annot) =>
this += "Annotated(" += arg += ", " += annot += ")"
case LambdaTypeTree(tparams, body) =>
this += "LambdaTypeTree(" ++= tparams += ", " += body += ")"
case TypeBind(name, bounds) =>
this += "TypeBind(" += name += ", " += bounds += ")"
case TypeBlock(aliases, tpt) =>
this += "TypeBlock(" ++= aliases += ", " += tpt += ")"
case TypeBoundsTree(lo, hi) =>
this += "TypeBoundsTree(" += lo += ", " += hi += ")"
case WildcardTypeTree() =>
this += s"WildcardTypeTree()"
case MatchTypeTree(bound, selector, cases) =>
this += "MatchTypeTree(" += bound += ", " += selector += ", " ++= cases += ")"
case CaseDef(pat, guard, body) =>
this += "CaseDef(" += pat += ", " += guard += ", " += body += ")"
case TypeCaseDef(pat, body) =>
this += "TypeCaseDef(" += pat += ", " += body += ")"
case Bind(name, body) =>
this += "Bind(\"" += name += "\", " += body += ")"
case Unapply(fun, implicits, patterns) =>
this += "Unapply(" += fun += ", " ++= implicits += ", " ++= patterns += ")"
case Alternatives(patterns) =>
this += "Alternatives(" ++= patterns += ")"
case TypedOrTest(tree, tpt) =>
this += "TypedOrTest(" += tree += ", " += tpt += ")"
case tree =>
this += s"<Internal compiler AST $tree does not have a corresponding reflect extractor>"
}
def visitConstant(x: Constant): this.type = x match {
case UnitConstant() => this += "UnitConstant()"
case NullConstant() => this += "NullConstant()"
case BooleanConstant(value) => this += "BooleanConstant(" += value += ")"
case ByteConstant(value) => this += "ByteConstant(" += value += ")"
case ShortConstant(value) => this += "ShortConstant(" += value += ")"
case IntConstant(value) => this += "IntConstant(" += value += ")"
case LongConstant(value) => this += "LongConstant(" += value += "L)"
case FloatConstant(value) => this += "FloatConstant(" += value += "f)"
case DoubleConstant(value) => this += "DoubleConstant(" += value += "d)"
case CharConstant(value) => this += "CharConstant('" += value += "')"
case StringConstant(value) => this += "StringConstant(\"" += value += "\")"
case ClassOfConstant(value) =>
this += "ClassOfConstant("
visitType(value) += ")"
}
def visitType(x: TypeRepr): this.type = x match {
case ConstantType(value) =>
this += "ConstantType(" += value += ")"
case TermRef(qual, name) =>
this += "TermRef(" += qual+= ", \"" += name += "\")"
case TypeRef(qual, name) =>
this += "TypeRef(" += qual += ", \"" += name += "\")"
case Refinement(parent, name, info) =>
this += "Refinement(" += parent += ", \"" += name += "\", " += info += ")"
case AppliedType(tycon, args) =>
this += "AppliedType(" += tycon += ", " ++= args += ")"
case AnnotatedType(underlying, annot) =>
this += "AnnotatedType(" += underlying += ", " += annot += ")"
case AndType(left, right) =>
this += "AndType(" += left += ", " += right += ")"
case OrType(left, right) =>
this += "OrType(" += left += ", " += right += ")"
case MatchType(bound, scrutinee, cases) =>
this += "MatchType(" += bound += ", " += scrutinee += ", " ++= cases += ")"
case ByNameType(underlying) =>
this += "ByNameType(" += underlying += ")"
case ParamRef(binder, idx) =>
this += "ParamRef(binder, " += idx += ")"
case ThisType(tp) =>
this += "ThisType(" += tp += ")"
case SuperType(thistpe, supertpe) =>
this += "SuperType(" += thistpe += ", " += supertpe += ")"
case RecursiveThis(binder) =>
this += "RecursiveThis(" += binder += ")"
case RecursiveType(underlying) =>
this += "RecursiveType(" += underlying += ")"
case MethodType(argNames, argTypes, resType) =>
this += "MethodType(" ++= argNames += ", " ++= argTypes += ", " += resType += ")"
case PolyType(argNames, argBounds, resType) =>
this += "PolyType(" ++= argNames += ", " ++= argBounds += ", " += resType += ")"
case TypeLambda(argNames, argBounds, resType) =>
this += "TypeLambda(" ++= argNames += ", " ++= argBounds += ", " += resType += ")"
case TypeBounds(lo, hi) =>
this += "TypeBounds(" += lo += ", " += hi += ")"
case NoPrefix() =>
this += "NoPrefix()"
case MatchCase(pat, rhs) =>
this += "MatchCase(" += pat += ", " += rhs += ")"
case FlexibleType(tp) =>
this += "FlexibleType(" += tp += ")"
case tp =>
this += s"<Internal compiler type $tp does not have a corresponding reflect extractor>"
}
def visitSignature(sig: Signature): this.type = {
val Signature(params, res) = sig
this += "Signature(" ++= params.map(_.toString) += ", " += res += ")"
}
def visitSelector(sel: Selector): this.type = sel match {
case SimpleSelector(id) => this += "SimpleSelector(" += id += ")"
case RenameSelector(id1, id2) => this += "RenameSelector(" += id1 += ", " += id2 += ")"
case OmitSelector(id) => this += "OmitSelector(" += id += ")"
case GivenSelector(bound) => this += "GivenSelector(" += bound += ")"
}
def visitSymbol(x: Symbol): this.type =
if x.isPackageDef then this += "IsPackageDefSymbol(<" += x.fullName += ">)"
else if x.isClassDef then this += "IsClassDefSymbol(<" += x.fullName += ">)"
else if x.isDefDef then this += "IsDefDefSymbol(<" += x.fullName += ">)"
else if x.isValDef then this += "IsValDefSymbol(<" += x.fullName += ">)"
else if x.isTypeDef then this += "IsTypeDefSymbol(<" += x.fullName += ">)"
else { assert(x.isNoSymbol); this += "NoSymbol()" }
def visitParamClause(x: ParamClause): this.type =
x match
case TermParamClause(params) => this += "TermParamClause(" ++= params += ")"
case TypeParamClause(params) => this += "TypeParamClause(" ++= params += ")"
def +=(x: Boolean): this.type = { sb.append(x); this }
def +=(x: Byte): this.type = { sb.append(x); this }
def +=(x: Short): this.type = { sb.append(x); this }
def +=(x: Int): this.type = { sb.append(x); this }
def +=(x: Long): this.type = { sb.append(x); this }
def +=(x: Float): this.type = { sb.append(x); this }
def +=(x: Double): this.type = { sb.append(x); this }
def +=(x: Char): this.type = { sb.append(x); this }
def +=(x: String): this.type = { sb.append(x); this }
def ++=(xs: List[String]): this.type = visitList[String](xs, +=)
private implicit class StringOps(buff: self.type) {
def +=(x: Option[String]): self.type = { visitOption(x, y => buff += "\"" += y += "\""); buff }
}
private implicit class TreeOps(buff: self.type) {
def +=(x: Tree): self.type = { visitTree(x); buff }
def +=(x: Option[Tree]): self.type = { visitOption(x, visitTree); buff }
def ++=(x: List[Tree]): self.type = { visitList(x, visitTree); buff }
def +++=(x: List[List[Tree]]): self.type = { visitList(x, ++=); buff }
}
private implicit class ConstantOps(buff: self.type) {
def +=(x: Constant): self.type = { visitConstant(x); buff }
}
private implicit class TypeOps(buff: self.type) {
def +=(x: TypeRepr): self.type = { visitType(x); buff }
def +=(x: Option[TypeRepr]): self.type = { visitOption(x, visitType); buff }
def ++=(x: List[TypeRepr]): self.type = { visitList(x, visitType); buff }
}
private implicit class SignatureOps(buff: self.type) {
def +=(x: Option[Signature]): self.type = { visitOption(x, visitSignature); buff }
}
private implicit class SelectorOps(buff: self.type) {
def ++=(x: List[Selector]): self.type = { visitList(x, visitSelector); buff }
}
private implicit class SymbolOps(buff: self.type) {
def +=(x: Symbol): self.type = { visitSymbol(x); buff }
}
private implicit class ParamClauseOps(buff: self.type) {
def ++=(x: List[ParamClause]): self.type = { visitList(x, visitParamClause); buff }
}
private def visitOption[U](opt: Option[U], visit: U => this.type): this.type = opt match {
case Some(x) =>
this += "Some("
visit(x)
this += ")"
case _ =>
this += "None"
}
private def visitList[U](list: List[U], visit: U => this.type): this.type = list match {
case x0 :: xs =>
this += "List("
visit(x0)
def visitNext(xs: List[U]): Unit = xs match {
case y :: ys =>
this += ", "
visit(y)
visitNext(ys)
case Nil =>
}
visitNext(xs)
this += ")"
case Nil =>
this += "Nil"
}
}
}