Skip to content

Commit 8e5b76d

Browse files
committed
Add instace checks for TASTy relfect Tree.Terms
1 parent 189e78e commit 8e5b76d

File tree

2 files changed

+302
-25
lines changed

2 files changed

+302
-25
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/TreeOpsImpl.scala

Lines changed: 160 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
1313
def symbol(implicit ctx: Context): Symbol = tree.symbol
1414
}
1515

16-
object IsPackageClause extends IsPackageClauseExtractor {
16+
object IsPackageClause extends IsPackageClauseModule {
1717
def unapply(tree: Tree)(implicit ctx: Context): Option[PackageClause] = tree match {
1818
case x: tpd.PackageDef => Some(x)
1919
case _ => None
@@ -47,7 +47,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
4747

4848
// ----- Definitions ----------------------------------------------
4949

50-
object IsDefinition extends IsDefinitionExtractor {
50+
object IsDefinition extends IsDefinitionModule {
5151
def unapply(tree: Tree)(implicit ctx: Context): Option[Definition] = tree match {
5252
case tree: tpd.MemberDef => Some(tree)
5353
case tree: PackageDefinition => Some(tree)
@@ -61,7 +61,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
6161

6262
// ClassDef
6363

64-
object IsClassDef extends IsClassDefExtractor {
64+
object IsClassDef extends IsClassDefModule {
6565
def unapply(tree: Tree)(implicit ctx: Context): Option[ClassDef] = tree match {
6666
case x: tpd.TypeDef if x.isClassDef => Some(x)
6767
case _ => None
@@ -87,7 +87,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
8787

8888
// DefDef
8989

90-
object IsDefDef extends IsDefDefExtractor {
90+
object IsDefDef extends IsDefDefModule {
9191
def unapply(tree: Tree)(implicit ctx: Context): Option[DefDef] = tree match {
9292
case x: tpd.DefDef => Some(x)
9393
case _ => None
@@ -112,7 +112,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
112112

113113
// ValDef
114114

115-
object IsValDef extends IsValDefExtractor {
115+
object IsValDef extends IsValDefModule {
116116
def unapply(tree: Tree)(implicit ctx: Context): Option[ValDef] = tree match {
117117
case x: tpd.ValDef => Some(x)
118118
case _ => None
@@ -135,7 +135,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
135135

136136
// TypeDef
137137

138-
object IsTypeDef extends IsTypeDefExtractor {
138+
object IsTypeDef extends IsTypeDefModule {
139139
def unapply(tree: Tree)(implicit ctx: Context): Option[TypeDef] = tree match {
140140
case x: tpd.TypeDef if !x.symbol.isClass => Some(x)
141141
case _ => None
@@ -168,7 +168,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
168168
def symbol(implicit ctx: Context): PackageSymbol = pdef.symbol
169169
}
170170

171-
object IsPackageDef extends IsPackageDefExtractor {
171+
object IsPackageDef extends IsPackageDefModule {
172172
def unapply(tree: Tree)(implicit ctx: Context): Option[PackageDef] = tree match {
173173
case x: PackageDefinition => Some(x)
174174
case _ => None
@@ -193,7 +193,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
193193
def underlying(implicit ctx: Context): Term = term.underlying
194194
}
195195

196-
object IsTerm extends IsTermExtractor {
196+
object IsTerm extends IsTermModule {
197197
def unapply(tree: Tree)(implicit ctx: Context): Option[Term] =
198198
if (tree.isTerm) Some(tree) else None
199199
def unapply(termOrTypeTree: TermOrTypeTree)(implicit ctx: Context, dummy: DummyImplicit): Option[Term] =
@@ -202,13 +202,27 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
202202

203203
object Term extends TermModule with TermCoreModuleImpl {
204204

205+
object IsIdent extends IsIdentModule {
206+
def unapply(x: Term)(implicit ctx: Context): Option[Ident] = x match {
207+
case x: tpd.Ident if x.isTerm => Some(x)
208+
case _ => None
209+
}
210+
}
211+
205212
object Ident extends IdentExtractor {
206213
def unapply(x: Term)(implicit ctx: Context): Option[String] = x match {
207214
case x: tpd.Ident if x.isTerm => Some(x.name.show)
208215
case _ => None
209216
}
210217
}
211218

219+
object IsSelect extends IsSelectModule {
220+
def unapply(x: Term)(implicit ctx: Context): Option[Select] = x match {
221+
case x: tpd.Select if x.isTerm => Some(x)
222+
case _ => None
223+
}
224+
}
225+
212226
object Select extends SelectExtractor {
213227
def unapply(x: Term)(implicit ctx: Context): Option[(Term, String, Option[Signature])] = x match {
214228
case x: tpd.Select if x.isTerm =>
@@ -220,69 +234,139 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
220234
}
221235
}
222236

237+
object IsLiteral extends IsLiteralModule {
238+
def unapply(x: Term)(implicit ctx: Context): Option[Literal] = x match {
239+
case x: tpd.Literal => Some(x)
240+
case _ => None
241+
}
242+
}
243+
223244
object Literal extends LiteralExtractor {
224245
def unapply(x: Term)(implicit ctx: Context): Option[Constant] = x match {
225246
case Trees.Literal(const) => Some(const)
226247
case _ => None
227248
}
228249
}
229250

251+
object IsThis extends IsThisModule {
252+
def unapply(x: Term)(implicit ctx: Context): Option[This] = x match {
253+
case x: tpd.This => Some(x)
254+
case _ => None
255+
}
256+
}
257+
230258
object This extends ThisExtractor {
231259
def unapply(x: Term)(implicit ctx: Context): Option[Option[Id]] = x match {
232260
case Trees.This(qual) => Some(optional(qual))
233261
case _ => None
234262
}
235263
}
236264

265+
object IsNew extends IsNewModule {
266+
def unapply(x: Term)(implicit ctx: Context): Option[New] = x match {
267+
case x: tpd.New => Some(x)
268+
case _ => None
269+
}
270+
}
271+
237272
object New extends NewExtractor {
238273
def unapply(x: Term)(implicit ctx: Context): Option[TypeTree] = x match {
239274
case x: tpd.New => Some(x.tpt)
240275
case _ => None
241276
}
242277
}
243278

279+
object IsNamedArg extends IsNamedArgModule {
280+
def unapply(x: Term)(implicit ctx: Context): Option[NamedArg] = x match {
281+
case x: tpd.NamedArg if x.name.isInstanceOf[Names.TermName] => Some(x) // TODO: Now, the name should alwas be a term name
282+
case _ => None
283+
}
284+
}
285+
244286
object NamedArg extends NamedArgExtractor {
245287
def unapply(x: Term)(implicit ctx: Context): Option[(String, Term)] = x match {
246288
case x: tpd.NamedArg if x.name.isInstanceOf[Names.TermName] => Some((x.name.toString, x.arg))
247289
case _ => None
248290
}
249291
}
250292

293+
object IsApply extends IsApplyModule {
294+
def unapply(x: Term)(implicit ctx: Context): Option[Apply] = x match {
295+
case x: tpd.Apply => Some(x)
296+
case _ => None
297+
}
298+
}
299+
251300
object Apply extends ApplyExtractor {
252301
def unapply(x: Term)(implicit ctx: Context): Option[(Term, List[Term])] = x match {
253302
case x: tpd.Apply => Some((x.fun, x.args))
254303
case _ => None
255304
}
256305
}
257306

307+
object IsTypeApply extends IsTypeApplyModule {
308+
def unapply(x: Term)(implicit ctx: Context): Option[TypeApply] = x match {
309+
case x: tpd.TypeApply => Some(x)
310+
case _ => None
311+
}
312+
}
313+
258314
object TypeApply extends TypeApplyExtractor {
259315
def unapply(x: Term)(implicit ctx: Context): Option[(Term, List[TypeTree])] = x match {
260316
case x: tpd.TypeApply => Some((x.fun, x.args))
261317
case _ => None
262318
}
263319
}
264320

321+
object IsSuper extends IsSuperModule {
322+
def unapply(x: Term)(implicit ctx: Context): Option[Super] = x match {
323+
case x: tpd.Super => Some(x)
324+
case _ => None
325+
}
326+
}
327+
265328
object Super extends SuperExtractor {
266329
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Option[Id])] = x match {
267330
case x: tpd.Super => Some((x.qual, if (x.mix.isEmpty) None else Some(x.mix)))
268331
case _ => None
269332
}
270333
}
271334

335+
object IsTyped extends IsTypedModule {
336+
def unapply(x: Term)(implicit ctx: Context): Option[Typed] = x match {
337+
case x: tpd.Typed => Some(x)
338+
case _ => None
339+
}
340+
}
341+
272342
object Typed extends TypedExtractor {
273343
def unapply(x: Term)(implicit ctx: Context): Option[(Term, TypeTree)] = x match {
274344
case x: tpd.Typed => Some((x.expr, x.tpt))
275345
case _ => None
276346
}
277347
}
278348

349+
object IsAssign extends IsAssignModule {
350+
def unapply(x: Term)(implicit ctx: Context): Option[Assign] = x match {
351+
case x: tpd.Assign => Some(x)
352+
case _ => None
353+
}
354+
}
355+
279356
object Assign extends AssignExtractor {
280357
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Term)] = x match {
281358
case x: tpd.Assign => Some((x.lhs, x.rhs))
282359
case _ => None
283360
}
284361
}
285362

363+
object IsBlock extends IsBlockModule {
364+
def unapply(x: Term)(implicit ctx: Context): Option[Block] = Block.normalizedLoops(x) match {
365+
case x: tpd.Block => Some(x)
366+
case _ => None
367+
}
368+
}
369+
286370
object Block extends BlockExtractor {
287371
def unapply(x: Term)(implicit ctx: Context): Option[(List[Statement], Term)] = normalizedLoops(x) match {
288372
case Trees.Block(stats, expr) => Some((stats, expr))
@@ -292,7 +376,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
292376
* i) Put `while` and `doWhile` loops in their own blocks: `{ def while$() = ...; while$() }`
293377
* ii) Put closures in their own blocks: `{ def anon$() = ...; closure(anon$, ...) }`
294378
*/
295-
private def normalizedLoops(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
379+
private[Term] def normalizedLoops(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
296380
case block: tpd.Block if block.stats.size > 1 =>
297381
def normalizeInnerLoops(stats: List[tpd.Tree]): List[tpd.Tree] = stats match {
298382
case (x: tpd.DefDef) :: y :: xs if needsNormalization(y) =>
@@ -318,6 +402,13 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
318402
}
319403
}
320404

405+
object IsInlined extends IsInlinedModule {
406+
def unapply(x: Term)(implicit ctx: Context): Option[Inlined] = x match {
407+
case x: tpd.Inlined => Some(x)
408+
case _ => None
409+
}
410+
}
411+
321412
object Inlined extends InlinedExtractor {
322413
def unapply(x: Term)(implicit ctx: Context): Option[(Option[TermOrTypeTree], List[Statement], Term)] = x match {
323414
case x: tpd.Inlined =>
@@ -326,48 +417,101 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
326417
}
327418
}
328419

420+
object IsLambda extends IsLambdaModule {
421+
def unapply(x: Term)(implicit ctx: Context): Option[Lambda] = x match {
422+
case x: tpd.Closure => Some(x)
423+
case _ => None
424+
}
425+
}
426+
329427
object Lambda extends LambdaExtractor {
330428
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Option[TypeTree])] = x match {
331429
case x: tpd.Closure => Some((x.meth, optional(x.tpt)))
332430
case _ => None
333431
}
334432
}
335433

434+
object IsIf extends IsIfModule {
435+
def unapply(x: Term)(implicit ctx: Context): Option[If] = x match {
436+
case x: tpd.If => Some(x)
437+
case _ => None
438+
}
439+
}
440+
336441
object If extends IfExtractor {
337442
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Term, Term)] = x match {
338443
case x: tpd.If => Some((x.cond, x.thenp, x.elsep))
339444
case _ => None
340445
}
341446
}
342447

448+
object IsMatch extends IsMatchModule {
449+
def unapply(x: Term)(implicit ctx: Context): Option[Match] = x match {
450+
case x: tpd.Match => Some(x)
451+
case _ => None
452+
}
453+
}
454+
343455
object Match extends MatchExtractor {
344456
def unapply(x: Term)(implicit ctx: Context): Option[(Term, List[CaseDef])] = x match {
345457
case x: tpd.Match => Some((x.selector, x.cases))
346458
case _ => None
347459
}
348460
}
349461

462+
object IsTry extends IsTryModule {
463+
def unapply(x: Term)(implicit ctx: Context): Option[Try] = x match {
464+
case x: tpd.Try => Some(x)
465+
case _ => None
466+
}
467+
}
468+
350469
object Try extends TryExtractor {
351470
def unapply(x: Term)(implicit ctx: Context): Option[(Term, List[CaseDef], Option[Term])] = x match {
352471
case x: tpd.Try => Some((x.expr, x.cases, optional(x.finalizer)))
353472
case _ => None
354473
}
355474
}
356475

476+
object IsReturn extends IsReturnModule {
477+
def unapply(x: Term)(implicit ctx: Context): Option[Return] = x match {
478+
case x: tpd.Return => Some(x)
479+
case _ => None
480+
}
481+
}
482+
357483
object Return extends ReturnExtractor {
358484
def unapply(x: Term)(implicit ctx: Context): Option[Term] = x match {
359485
case x: tpd.Return => Some(x.expr)
360486
case _ => None
361487
}
362488
}
363489

490+
object IsRepeated extends IsRepeatedModule {
491+
def unapply(x: Term)(implicit ctx: Context): Option[Repeated] = x match {
492+
case x: tpd.SeqLiteral => Some(x)
493+
case _ => None
494+
}
495+
}
496+
364497
object Repeated extends RepeatedExtractor {
365498
def unapply(x: Term)(implicit ctx: Context): Option[List[Term]] = x match {
366499
case x: tpd.SeqLiteral => Some(x.elems)
367500
case _ => None
368501
}
369502
}
370503

504+
object IsSelectOuter extends IsSelectOuterModule {
505+
def unapply(x: Term)(implicit ctx: Context): Option[SelectOuter] = x match {
506+
case x: tpd.Select =>
507+
x.name match {
508+
case NameKinds.OuterSelectName(_, _) => Some(x)
509+
case _ => None
510+
}
511+
case _ => None
512+
}
513+
}
514+
371515
object SelectOuter extends SelectOuterExtractor {
372516
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Int, Type)] = x match {
373517
case x: tpd.Select =>
@@ -379,6 +523,13 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
379523
}
380524
}
381525

526+
object IsWhile extends IsWhileModule {
527+
def unapply(x: Term)(implicit ctx: Context): Option[While] = x match {
528+
case x: tpd.WhileDo => Some(x)
529+
case _ => None
530+
}
531+
}
532+
382533
object While extends WhileExtractor {
383534
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Term)] = x match {
384535
case x: tpd.WhileDo => Some((x.cond, x.body))

0 commit comments

Comments
 (0)