@@ -49,8 +49,9 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
49
49
50
50
type Id = untpd.Ident
51
51
52
- def IdDeco (x : Id ): IdAPI = new IdAPI {
53
- def pos (implicit ctx : Context ): Position = x.pos
52
+ def IdDeco (id : Id ): IdAPI = new IdAPI {
53
+ def pos (implicit ctx : Context ): Position = id.pos
54
+ def name (implicit ctx : Context ): String = id.name.toString
54
55
}
55
56
56
57
def idClassTag : ClassTag [Id ] = implicitly[ClassTag [Id ]]
@@ -82,8 +83,8 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
82
83
}
83
84
}
84
85
85
- def PackageClauseDeco (x : PackageClause ): PackageClauseAPI = new PackageClauseAPI {
86
- def definition (implicit ctx : Context ): Definition = FromSymbol .packageDef(x .symbol)
86
+ def PackageClauseDeco (pack : PackageClause ): PackageClauseAPI = new PackageClauseAPI {
87
+ def definition (implicit ctx : Context ): Definition = FromSymbol .packageDef(pack .symbol)
87
88
}
88
89
89
90
// ----- Statements -----------------------------------------------
@@ -101,6 +102,11 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
101
102
}
102
103
}
103
104
105
+ def ImportDeco (imp : Import ): ImportAPI = new ImportAPI {
106
+ def expr (implicit ctx : Context ): Tree = imp.expr
107
+ def selector (implicit ctx : Context ): List [ImportSelector ] = imp.selectors
108
+ }
109
+
104
110
type ImportSelector = untpd.Tree
105
111
106
112
def importSelectorClassTag : ClassTag [ImportSelector ] = implicitly[ClassTag [ImportSelector ]]
@@ -135,34 +141,36 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
135
141
x.isInstanceOf [Trees .MemberDef [_]]
136
142
}
137
143
138
- def DefinitionDeco (x : Definition ): DefinitionAPI = new DefinitionAPI {
144
+ def DefinitionDeco (definition : Definition ): DefinitionAPI = new DefinitionAPI {
139
145
140
- def owner (implicit ctx : Context ): Definition = FromSymbol .definition(x.symbol.owner)
146
+ def name (implicit ctx : Context ): String = definition.symbol.name.toString
147
+
148
+ def owner (implicit ctx : Context ): Definition = FromSymbol .definition(definition.symbol.owner)
141
149
142
150
def flags (implicit ctx : Context ): FlagSet =
143
- new FlagSet (x .symbol.flags)
151
+ new FlagSet (definition .symbol.flags)
144
152
145
153
def privateWithin (implicit ctx : Context ): Option [Type ] = {
146
- val within = x .symbol.privateWithin
147
- if (within.exists && ! x .symbol.is(core.Flags .Protected )) Some (within.typeRef)
154
+ val within = definition .symbol.privateWithin
155
+ if (within.exists && ! definition .symbol.is(core.Flags .Protected )) Some (within.typeRef)
148
156
else None
149
157
}
150
158
151
159
def protectedWithin (implicit ctx : Context ): Option [Type ] = {
152
- val within = x .symbol.privateWithin
153
- if (within.exists && x .symbol.is(core.Flags .Protected )) Some (within.typeRef)
160
+ val within = definition .symbol.privateWithin
161
+ if (within.exists && definition .symbol.is(core.Flags .Protected )) Some (within.typeRef)
154
162
else None
155
163
}
156
164
157
165
def annots (implicit ctx : Context ): List [Term ] = {
158
- x .symbol.annotations.flatMap {
166
+ definition .symbol.annotations.flatMap {
159
167
case _ : core.Annotations .LazyBodyAnnotation => Nil
160
168
case annot => annot.tree :: Nil
161
169
}
162
170
}
163
171
164
172
def localContext (implicit ctx : Context ): Context =
165
- if (x .hasType && x .symbol.exists) ctx.withOwner(x .symbol)
173
+ if (definition .hasType && definition .symbol.exists) ctx.withOwner(definition .symbol)
166
174
else ctx
167
175
}
168
176
@@ -177,13 +185,20 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
177
185
object ClassDef extends ClassDefExtractor {
178
186
def unapply (x : ClassDef )(implicit ctx : Context ): Option [(String , DefDef , List [Parent ], Option [ValDef ], List [Statement ])] = x match {
179
187
case x : tpd.TypeDef @ unchecked if x.isClassDef =>
180
- val temp @ Trees .Template (constr, parents, self, _) = x.rhs
181
- val selfVal = if (self.isEmpty) None else Some (self)
182
- Some ((x.name.toString, constr, parents, selfVal, temp.body))
188
+ val deco = ClassDefDeco (x)
189
+ Some ((x.name.toString, deco.constructor, deco.parents, deco.self, deco.body))
183
190
case _ => None
184
191
}
185
192
}
186
193
194
+ def ClassDefDeco (cdef : ClassDef ): ClassDefAPI = new ClassDefAPI {
195
+ private [this ] val rhs = cdef.rhs.asInstanceOf [tpd.Template ]
196
+ def constructor (implicit ctx : Context ): DefDef = rhs.constr
197
+ def parents (implicit ctx : Context ): List [tpd.Tree ] = rhs.parents
198
+ def self (implicit ctx : Context ): Option [tpd.ValDef ] = optional(rhs.self)
199
+ def body (implicit ctx : Context ): List [tpd.Tree ] = rhs.body
200
+ }
201
+
187
202
// DefDef
188
203
189
204
type DefDef = tpd.DefDef
@@ -193,11 +208,18 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
193
208
object DefDef extends DefDefExtractor {
194
209
def unapply (x : DefDef )(implicit ctx : Context ): Option [(String , List [TypeDef ], List [List [ValDef ]], TypeTree , Option [Term ])] = x match {
195
210
case x : tpd.DefDef @ unchecked =>
196
- Some ((x.name.toString, x.tparams, x.vparamss, x.tpt, if (x.rhs.isEmpty) None else Some (x.rhs)))
211
+ Some ((x.name.toString, x.tparams, x.vparamss, x.tpt, optional (x.rhs)))
197
212
case _ => None
198
213
}
199
214
}
200
215
216
+ def DefDefDeco (ddef : DefDef ): DefDefAPI = new DefDefAPI {
217
+ def typeParams (implicit ctx : Context ): List [TypeDef ] = ddef.tparams
218
+ def paramss (implicit ctx : Context ): List [List [ValDef ]] = ddef.vparamss
219
+ def returnTpt (implicit ctx : Context ): TypeTree = ddef.tpt
220
+ def rhs (implicit ctx : Context ): Option [Tree ] = optional(ddef.rhs)
221
+ }
222
+
201
223
// ValDef
202
224
203
225
type ValDef = tpd.ValDef
@@ -207,11 +229,16 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
207
229
object ValDef extends ValDefExtractor {
208
230
def unapply (x : ValDef )(implicit ctx : Context ): Option [(String , TypeTree , Option [Term ])] = x match {
209
231
case x : tpd.ValDef @ unchecked =>
210
- Some ((x.name.toString, x.tpt, if (x.rhs.isEmpty) None else Some (x.rhs)))
232
+ Some ((x.name.toString, x.tpt, optional (x.rhs)))
211
233
case _ => None
212
234
}
213
235
}
214
236
237
+ def ValDefDeco (vdef : ValDef ): ValDefAPI = new ValDefAPI {
238
+ def tpt (implicit ctx : Context ): TypeTree = vdef.tpt
239
+ def rhs (implicit ctx : Context ): Option [Tree ] = optional(vdef.rhs)
240
+ }
241
+
215
242
// TypeDef
216
243
217
244
type TypeDef = tpd.TypeDef
@@ -225,12 +252,21 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
225
252
}
226
253
}
227
254
255
+ def TypeDefDeco (tdef : TypeDef ): TypeDefAPI = new TypeDefAPI {
256
+ def rhs (implicit ctx : Context ): TypeOrBoundsTree = tdef.rhs
257
+ }
258
+
259
+ // PackageDef
260
+
228
261
type PackageDef = PackageDefinition
229
262
230
- def PackageDefDeco (t : PackageDef ): PackageDefAPI = new PackageDefAPI {
263
+ def PackageDefDeco (pdef : PackageDef ): PackageDefAPI = new PackageDefAPI {
264
+
265
+ def owner (implicit ctx : Context ): PackageDefinition = FromSymbol .packageDef(pdef.symbol.owner)
266
+
231
267
def members (implicit ctx : Context ): List [Statement ] = {
232
- if (t .symbol.is(core.Flags .JavaDefined )) Nil // FIXME should also support java packages
233
- else t .symbol.info.decls.iterator.map(FromSymbol .definition).toList
268
+ if (pdef .symbol.is(core.Flags .JavaDefined )) Nil // FIXME should also support java packages
269
+ else pdef .symbol.info.decls.iterator.map(FromSymbol .definition).toList
234
270
}
235
271
}
236
272
@@ -252,15 +288,15 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
252
288
253
289
type Term = tpd.Tree
254
290
255
- def TermDeco (tree : Term ): TermAPI = new TermAPI {
291
+ def TermDeco (term : Term ): TermAPI = new TermAPI {
256
292
257
- def pos (implicit ctx : Context ): Position = tree .pos
293
+ def pos (implicit ctx : Context ): Position = term .pos
258
294
259
- def tpe (implicit ctx : Context ): Types .Type = tree .tpe
295
+ def tpe (implicit ctx : Context ): Types .Type = term .tpe
260
296
261
297
def toExpr [T : quoted.Type ](implicit ctx : Context ): quoted.Expr [T ] = {
262
298
typecheck(ctx)
263
- new quoted.Exprs .TastyTreeExpr (tree ).asInstanceOf [quoted.Expr [T ]]
299
+ new quoted.Exprs .TastyTreeExpr (term ).asInstanceOf [quoted.Expr [T ]]
264
300
}
265
301
266
302
private def typecheck [T : quoted.Type ](ctx : Contexts .Context ): Unit = {
@@ -270,15 +306,15 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
270
306
def doReport (m : MessageContainer )(implicit ctx : Contexts .Context ): Unit = ()
271
307
})
272
308
val tp = QuotedTypeDeco (implicitly[quoted.Type [T ]]).toTasty
273
- ctx0.typer.typed(tree , tp.tpe)
309
+ ctx0.typer.typed(term , tp.tpe)
274
310
if (ctx0.reporter.hasErrors) {
275
311
val stack = new Exception ().getStackTrace
276
312
def filter (elem : StackTraceElement ) =
277
313
elem.getClassName.startsWith(" dotty.tools.dotc.tasty.TastyImpl" ) ||
278
314
! elem.getClassName.startsWith(" dotty.tools.dotc" )
279
315
throw new scala.tasty.TastyTypecheckError (
280
316
s """ Error during tasty reflection while typing term
281
- |term: ${tree .show}
317
+ |term: ${term .show}
282
318
|with expected type: ${tp.tpe.show}
283
319
|
284
320
| ${stack.takeWhile(filter).mkString(" \n " )}
@@ -321,7 +357,7 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
321
357
322
358
object This extends ThisExtractor {
323
359
def unapply (x : Term )(implicit ctx : Context ): Option [Option [Id ]] = x match {
324
- case Trees .This (qual) => Some (if (qual.isEmpty) None else Some (qual))
360
+ case Trees .This (qual) => Some (optional (qual))
325
361
case _ => None
326
362
}
327
363
}
@@ -414,7 +450,7 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
414
450
415
451
object Lambda extends LambdaExtractor {
416
452
def unapply (x : Term )(implicit ctx : Context ): Option [(Term , Option [TypeTree ])] = x match {
417
- case x : tpd.Closure @ unchecked => Some ((x.meth, if (x.tpt.isEmpty) None else Some (x.tpt)))
453
+ case x : tpd.Closure @ unchecked => Some ((x.meth, optional (x.tpt)))
418
454
case _ => None
419
455
}
420
456
}
@@ -435,7 +471,7 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
435
471
436
472
object Try extends TryExtractor {
437
473
def unapply (x : Term )(implicit ctx : Context ): Option [(Term , List [CaseDef ], Option [Term ])] = x match {
438
- case x : tpd.Try @ unchecked => Some ((x.expr, x.cases, if (x.finalizer.isEmpty) None else Some (x.finalizer)))
474
+ case x : tpd.Try @ unchecked => Some ((x.expr, x.cases, optional (x.finalizer)))
439
475
case _ => None
440
476
}
441
477
}
@@ -497,12 +533,15 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
497
533
498
534
def CaseDefDeco (caseDef : CaseDef ): CaseDefAPI = new CaseDefAPI {
499
535
def show (implicit ctx : Context , s : Show [TastyImpl .this .type ]): String = s.showCaseDef(caseDef)
536
+ def pattern (implicit ctx : Context ): Pattern = caseDef.pat
537
+ def guard (implicit ctx : Context ): Option [Term ] = optional(caseDef.guard)
538
+ def rhs (implicit ctx : Context ): Term = caseDef.body
500
539
}
501
540
502
541
object CaseDef extends CaseDefExtractor {
503
542
def unapply (x : CaseDef ): Option [(Pattern , Option [Term ], Term )] = x match {
504
543
case x : tpd.CaseDef @ unchecked =>
505
- Some (x.pat, if (x.guard.isEmpty) None else Some (x.guard), x.body)
544
+ Some (x.pat, optional (x.guard), x.body)
506
545
case _ => None
507
546
}
508
547
}
@@ -575,9 +614,9 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
575
614
576
615
type TypeTree = tpd.Tree
577
616
578
- def TypeTreeDeco (x : TypeTree ): TypeTreeAPI = new TypeTreeAPI {
579
- def pos (implicit ctx : Context ): Position = x .pos
580
- def tpe (implicit ctx : Context ): Types .Type = x .tpe.stripTypeVar
617
+ def TypeTreeDeco (tpt : TypeTree ): TypeTreeAPI = new TypeTreeAPI {
618
+ def pos (implicit ctx : Context ): Position = tpt .pos
619
+ def tpe (implicit ctx : Context ): Types .Type = tpt .tpe.stripTypeVar
581
620
}
582
621
583
622
def typeTreeClassTag : ClassTag [TypeTree ] = implicitly[ClassTag [TypeTree ]]
@@ -682,8 +721,10 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
682
721
683
722
type TypeBoundsTree = tpd.Tree
684
723
685
- def TypeBoundsTreeDeco (x : TypeBoundsTree ): TypeBoundsTreeAPI = new TypeBoundsTreeAPI {
686
- def tpe (implicit ctx : Context ): TypeBounds = x.tpe.asInstanceOf [Types .TypeBounds ]
724
+ def TypeBoundsTreeDeco (bounds : TypeBoundsTree ): TypeBoundsTreeAPI = new TypeBoundsTreeAPI {
725
+ def tpe (implicit ctx : Context ): TypeBounds = bounds.tpe.asInstanceOf [Types .TypeBounds ]
726
+ def low (implicit ctx : Context ): TypeTree = bounds.asInstanceOf [tpd.TypeBoundsTree ].lo
727
+ def hi (implicit ctx : Context ): TypeTree = bounds.asInstanceOf [tpd.TypeBoundsTree ].hi
687
728
}
688
729
689
730
def typeBoundsTreeClassTag : ClassTag [TypeBoundsTree ] = implicitly[ClassTag [TypeBoundsTree ]]
@@ -726,9 +767,24 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
726
767
def polyTypeClassTag : ClassTag [PolyType ] = implicitly[ClassTag [PolyType ]]
727
768
def typeLambdaClassTag : ClassTag [TypeLambda ] = implicitly[ClassTag [TypeLambda ]]
728
769
729
- def MethodTypeDeco (x : MethodType ): MethodTypeAPI = new MethodTypeAPI {
730
- def isErased : Boolean = x.isErasedMethod
731
- def isImplicit : Boolean = x.isImplicitMethod
770
+ def MethodTypeDeco (tpe : MethodType ): MethodTypeAPI = new MethodTypeAPI {
771
+ def isErased : Boolean = tpe.isErasedMethod
772
+ def isImplicit : Boolean = tpe.isImplicitMethod
773
+ def paramNames (implicit ctx : Context ): List [String ] = tpe.paramNames.map(_.toString)
774
+ def paramTypes (implicit ctx : Context ): List [Type ] = tpe.paramInfos
775
+ def resultTpe (implicit ctx : Context ): Type = tpe.resType
776
+ }
777
+
778
+ def PolyTypeDeco (tpe : Types .PolyType ): PolyTypeAPI = new PolyTypeAPI {
779
+ def paramNames (implicit ctx : Context ): List [String ] = tpe.paramNames.map(_.toString)
780
+ def paramTypes (implicit ctx : Context ): List [TypeBounds ] = tpe.paramInfos
781
+ def resultTpe (implicit ctx : Context ): Type = tpe.resType
782
+ }
783
+
784
+ def TypeLambdaDeco (tpe : Types .TypeLambda ): TypeLambdaAPI = new TypeLambdaAPI {
785
+ def paramNames (implicit ctx : Context ): List [String ] = tpe.paramNames.map(_.toString)
786
+ def paramTypes (implicit ctx : Context ): List [TypeBounds ] = tpe.paramInfos
787
+ def resultTpe (implicit ctx : Context ): Type = tpe.resType
732
788
}
733
789
734
790
object Type extends TypeModule {
@@ -894,6 +950,11 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
894
950
}
895
951
}
896
952
953
+ def TypeBoundsDeco (tpe : TypeBounds ): TypeBoundsAPI = new TypeBoundsAPI {
954
+ def low (implicit ctx : Context ): Type = tpe.lo
955
+ def hi (implicit ctx : Context ): Type = tpe.hi
956
+ }
957
+
897
958
// ----- NoPrefix --------------------------------------------------
898
959
899
960
type NoPrefix = Types .NoPrefix .type
@@ -1015,6 +1076,11 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
1015
1076
}
1016
1077
}
1017
1078
1079
+ def SignatureDeco (sig : Signature ): SignatureAPI = new SignatureAPI {
1080
+ def paramSigs : List [String ] = sig.paramsSig.map(_.toString)
1081
+ def resultSig : String = sig.resSig.toString
1082
+ }
1083
+
1018
1084
// ===== Positions ================================================
1019
1085
1020
1086
type Position = SourcePosition
@@ -1032,4 +1098,9 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
1032
1098
def endColumn = pos.endColumn
1033
1099
}
1034
1100
1101
+ // ===== Helpers ==================================================
1102
+
1103
+ private def optional [T <: Trees .Tree [_]](tree : T ): Option [tree.type ] =
1104
+ if (tree.isEmpty) None else Some (tree)
1105
+
1035
1106
}
0 commit comments