Skip to content

Commit 303934f

Browse files
committed
Implement TASTy reflect constructors
1 parent f256a73 commit 303934f

File tree

11 files changed

+493
-27
lines changed

11 files changed

+493
-27
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -974,13 +974,13 @@ object Trees {
974974
* so that they selectively retype themselves. Retyping needs a context.
975975
*/
976976
abstract class TreeCopier {
977-
def postProcess(tree: Tree, copied: untpd.Tree): copied.ThisTree[T]
978-
def postProcess(tree: Tree, copied: untpd.MemberDef): copied.ThisTree[T]
977+
protected def postProcess(tree: Tree, copied: untpd.Tree): copied.ThisTree[T]
978+
protected def postProcess(tree: Tree, copied: untpd.MemberDef): copied.ThisTree[T]
979979

980-
def finalize(tree: Tree, copied: untpd.Tree): copied.ThisTree[T] =
980+
protected def finalize(tree: Tree, copied: untpd.Tree): copied.ThisTree[T] =
981981
postProcess(tree, copied.withPos(tree.pos).withAttachmentsFrom(tree))
982982

983-
def finalize(tree: Tree, copied: untpd.MemberDef): copied.ThisTree[T] =
983+
protected def finalize(tree: Tree, copied: untpd.MemberDef): copied.ThisTree[T] =
984984
postProcess(tree, copied.withPos(tree.pos).withAttachmentsFrom(tree))
985985

986986
def Ident(tree: Tree)(name: Name): Ident = tree match {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ trait CaseDefOpsImpl extends scala.tasty.reflect.CaseDefOps with CoreImpl with H
1010
}
1111

1212
object CaseDef extends CaseDefModule {
13+
def apply(pat: Pattern, guard: Option[Term], body: Term)(implicit ctx: Context): CaseDef = ???
14+
15+
def copy(original: CaseDef)(pat: Pattern, guard: Option[Term], body: Term)(implicit ctx: Context): CaseDef = ???
1316
def unapply(x: CaseDef): Some[(Pattern, Option[Term], Term)] = Some(x.pat, optional(x.guard), x.body)
1417
}
1518

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ trait CoreImpl extends scala.tasty.reflect.Core {
2626
type Term = tpd.Tree
2727
val Term: TermCoreModuleImpl
2828
trait TermCoreModuleImpl extends TermCoreModule {
29-
type Ident = tpd.Ident
30-
type Select = tpd.Select
29+
type Ref = tpd.RefTree
30+
type Ident = tpd.Ident
31+
type Select = tpd.Select
3132
type Literal = tpd.Literal
3233
type This = tpd.This
3334
type New = tpd.New

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

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
2121
}
2222

2323
object PackageClause extends PackageClauseModule {
24-
def unapply(tree: Tree)(implicit ctx: Context): Option[(Term, List[Tree])] = tree match {
24+
def apply(pid: Ref, stats: List[Tree])(implicit ctx: Context): PackageClause =
25+
tpd.PackageDef(pid.asInstanceOf[tpd.RefTree], stats)
26+
27+
def unapply(tree: Tree)(implicit ctx: Context): Option[(Ref, List[Tree])] = tree match {
2528
case x: tpd.PackageDef => Some((x.pid, x.stats))
2629
case _ => None
2730
}
@@ -34,6 +37,9 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
3437
// ----- Statements -----------------------------------------------
3538

3639
object Import extends ImportModule {
40+
def apply(expr: Term, selectors: List[ImportSelector])(implicit ctx: Context): Import =
41+
tpd.Import(expr, selectors)
42+
3743
def unapply(x: Tree)(implicit ctx: Context): Option[(Term, List[ImportSelector])] = x match {
3844
case x: tpd.Import => Some((x.expr, x.selectors))
3945
case _ => None
@@ -69,6 +75,10 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
6975
}
7076

7177
object ClassDef extends ClassDefModule {
78+
def apply(name: String, constr: DefDef, parents: List[TermOrTypeTree], selfOpt: Option[ValDef], body: List[Statement])(implicit ctx: Context): ClassDef = {
79+
???
80+
}
81+
7282
def unapply(tree: Tree)(implicit ctx: Context): Option[(String, DefDef, List[TermOrTypeTree], Option[ValDef], List[Statement])] = tree match {
7383
case Trees.TypeDef(name, impl: tpd.Template) =>
7484
Some((name.toString, impl.constr, impl.parents, optional(impl.self), impl.body))
@@ -95,6 +105,9 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
95105
}
96106

97107
object DefDef extends DefDefModule {
108+
def apply(symbol: DefSymbol, rhsFn: List[Type] => List[List[Term]] => Option[Term])(implicit ctx: Context): DefDef =
109+
tpd.polyDefDef(symbol, tparams => vparamss => rhsFn(tparams)(vparamss).getOrElse(tpd.EmptyTree))
110+
98111
def unapply(tree: Tree)(implicit ctx: Context): Option[(String, List[TypeDef], List[List[ValDef]], TypeTree, Option[Term])] = tree match {
99112
case x: tpd.DefDef =>
100113
Some((x.name.toString, x.tparams, x.vparamss, x.tpt, optional(x.rhs)))
@@ -120,6 +133,9 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
120133
}
121134

122135
object ValDef extends ValDefModule {
136+
def apply(symbol: ValSymbol, rhs: Option[Term])(implicit ctx: Context): ValDef =
137+
tpd.ValDef(symbol, rhs.getOrElse(tpd.EmptyTree))
138+
123139
def unapply(tree: Tree)(implicit ctx: Context): Option[(String, TypeTree, Option[Term])] = tree match {
124140
case x: tpd.ValDef =>
125141
Some((x.name.toString, x.tpt, optional(x.rhs)))
@@ -143,6 +159,8 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
143159
}
144160

145161
object TypeDef extends TypeDefModule {
162+
def apply(symbol: TypeSymbol)(implicit ctx: Context): TypeDef = tpd.TypeDef(symbol)
163+
146164
def unapply(tree: Tree)(implicit ctx: Context): Option[(String, TypeOrBoundsTree /* TypeTree | TypeBoundsTree */)] = tree match {
147165
case x: tpd.TypeDef if !x.symbol.isClass => Some((x.name.toString, x.rhs))
148166
case _ => None
@@ -213,7 +231,16 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
213231
def name(implicit ctx: Context): String = x.name.show
214232
}
215233

234+
object Ref extends RefModule {
235+
def apply(sym: Symbol)(implicit ctx: Context): Ref = tpd.ref(sym).asInstanceOf[tpd.RefTree]
236+
def copy(original: Tree)(name: String)(implicit ctx: Context): Ref = ???
237+
}
238+
216239
object Ident extends IdentModule {
240+
def apply(name: String)(implicit ctx: Context): Ident = ???
241+
242+
def copy(original: Tree)(name: String)(implicit ctx: Context): Ident = ???
243+
217244
def unapply(x: Term)(implicit ctx: Context): Option[String] = x match {
218245
case x: tpd.Ident if x.isTerm => Some(x.name.show)
219246
case _ => None
@@ -236,6 +263,10 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
236263
}
237264

238265
object Select extends SelectModule {
266+
def apply(qualifier: Term, name: String, signature: Option[Signature])(implicit ctx: Context): Select = ???
267+
268+
def copy(original: Tree)(qualifier: Term, name: String)(implicit ctx: Context): Select = ???
269+
239270
def unapply(x: Term)(implicit ctx: Context): Option[(Term, String)] = x match {
240271
case x: tpd.Select if x.isTerm => Some((x.qualifier, x.name.toString))
241272
case _ => None
@@ -255,6 +286,12 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
255286
}
256287

257288
object Literal extends LiteralModule {
289+
290+
def apply(constant: Constant)(implicit ctx: Context): Literal =
291+
tpd.Literal(constant)
292+
293+
def copy(original: Tree)(constant: Constant)(implicit ctx: Context): Literal = ???
294+
258295
def unapply(x: Term)(implicit ctx: Context): Option[Constant] = x match {
259296
case Trees.Literal(const) => Some(const)
260297
case _ => None
@@ -273,6 +310,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
273310
}
274311

275312
object This extends ThisModule {
313+
314+
def apply(cls: ClassSymbol)(implicit ctx: Context): This = tpd.This(cls)
315+
316+
def copy(original: Tree)(qual: Option[Id]): This = ???
317+
276318
def unapply(x: Term)(implicit ctx: Context): Option[Option[Id]] = x match {
277319
case Trees.This(qual) => Some(optional(qual))
278320
case _ => None
@@ -292,6 +334,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
292334
}
293335

294336
object New extends NewModule {
337+
338+
def apply(tpt: TypeTree)(implicit ctx: Context): New = tpd.New(tpt)
339+
340+
def copy(original: Tree)(tpt: TypeTree)(implicit ctx: Context): New = ???
341+
295342
def unapply(x: Term)(implicit ctx: Context): Option[TypeTree] = x match {
296343
case x: tpd.New => Some(x.tpt)
297344
case _ => None
@@ -311,6 +358,12 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
311358
}
312359

313360
object NamedArg extends NamedArgModule {
361+
362+
def apply(name: String, arg: Term)(implicit ctx: Context): NamedArg =
363+
tpd.NamedArg(name.toTermName, arg)
364+
365+
def copy(tree: NamedArg)(name: String, arg: Term)(implicit ctx: Context): NamedArg = ???
366+
314367
def unapply(x: Term)(implicit ctx: Context): Option[(String, Term)] = x match {
315368
case x: tpd.NamedArg if x.name.isInstanceOf[Names.TermName] => Some((x.name.toString, x.arg))
316369
case _ => None
@@ -330,6 +383,12 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
330383
}
331384

332385
object Apply extends ApplyModule {
386+
387+
def apply(fn: Term, args: List[Term])(implicit ctx: Context): Apply =
388+
tpd.Apply(fn, args)
389+
390+
def copy(original: Tree)(fun: Term, args: List[Term])(implicit ctx: Context): Apply = ???
391+
333392
def unapply(x: Term)(implicit ctx: Context): Option[(Term, List[Term])] = x match {
334393
case x: tpd.Apply => Some((x.fun, x.args))
335394
case _ => None
@@ -349,6 +408,12 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
349408
}
350409

351410
object TypeApply extends TypeApplyModule {
411+
412+
def apply(fn: Term, args: List[TypeTree])(implicit ctx: Context): TypeApply =
413+
tpd.TypeApply(fn, args)
414+
415+
def copy(original: Tree)(fun: Term, args: List[TypeTree])(implicit ctx: Context): TypeApply = ???
416+
352417
def unapply(x: Term)(implicit ctx: Context): Option[(Term, List[TypeTree])] = x match {
353418
case x: tpd.TypeApply => Some((x.fun, x.args))
354419
case _ => None
@@ -368,6 +433,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
368433
}
369434

370435
object Super extends SuperModule {
436+
def apply(qual: Term, mix: Option[Id])(implicit ctx: Context): Super =
437+
tpd.Super(qual, mix.getOrElse(untpd.EmptyTypeIdent), ???, ???)
438+
439+
def copy(original: Tree)(qual: Term, mix: Option[Id]): Super = ???
440+
371441
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Option[Id])] = x match {
372442
case x: tpd.Super => Some((x.qual, if (x.mix.isEmpty) None else Some(x.mix)))
373443
case _ => None
@@ -387,6 +457,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
387457
}
388458

389459
object Typed extends TypedModule {
460+
def apply(expr: Term, tpt: TypeTree)(implicit ctx: Context): Typed =
461+
tpd.Typed(expr, tpt)
462+
463+
def copy(original: Tree)(expr: Term, tpt: TypeTree)(implicit ctx: Context): Typed = ???
464+
390465
def unapply(x: Term)(implicit ctx: Context): Option[(Term, TypeTree)] = x match {
391466
case x: tpd.Typed => Some((x.expr, x.tpt))
392467
case _ => None
@@ -406,6 +481,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
406481
}
407482

408483
object Assign extends AssignModule {
484+
def apply(lhs: Term, rhs: Term)(implicit ctx: Context): Assign =
485+
tpd.Assign(lhs, rhs)
486+
487+
def copy(original: Tree)(lhs: Term, rhs: Term)(implicit ctx: Context): Assign = ???
488+
409489
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Term)] = x match {
410490
case x: tpd.Assign => Some((x.lhs, x.rhs))
411491
case _ => None
@@ -454,6 +534,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
454534
}
455535

456536
object Block extends BlockModule {
537+
def apply(stats: List[Statement], expr: Term)(implicit ctx: Context): Block =
538+
tpd.Block(stats, expr)
539+
540+
def copy(original: Tree)(stats: List[Statement], expr: Term)(implicit ctx: Context): Block = ???
541+
457542
def unapply(x: Term)(implicit ctx: Context): Option[(List[Statement], Term)] = x match {
458543
case IsBlock(x) => Some((x.stats, x.expr))
459544
case _ => None
@@ -474,6 +559,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
474559
}
475560

476561
object Inlined extends InlinedModule {
562+
def apply(call: Option[TermOrTypeTree], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined =
563+
tpd.Inlined(call.getOrElse(tpd.EmptyTree), bindings.map { case b: tpd.MemberDef => b }, expansion)
564+
565+
def copy(original: Tree)(call: Option[TermOrTypeTree], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined = ???
566+
477567
def unapply(x: Term)(implicit ctx: Context): Option[(Option[TermOrTypeTree], List[Statement], Term)] = x match {
478568
case x: tpd.Inlined =>
479569
Some((optional(x.call), x.bindings, x.expansion))
@@ -494,6 +584,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
494584
}
495585

496586
object Lambda extends LambdaModule {
587+
def apply(meth: Term, tpt: Option[TypeTree])(implicit ctx: Context): Lambda =
588+
tpd.Closure(Nil, meth, tpt.getOrElse(tpd.EmptyTree))
589+
590+
def copy(original: Tree)(meth: Tree, tpt: Tree)(implicit ctx: Context): Lambda = ???
591+
497592
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Option[TypeTree])] = x match {
498593
case x: tpd.Closure => Some((x.meth, optional(x.tpt)))
499594
case _ => None
@@ -514,6 +609,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
514609
}
515610

516611
object If extends IfModule {
612+
def apply(cond: Term, thenp: Term, elsep: Term)(implicit ctx: Context): If =
613+
tpd.If(cond, thenp, elsep)
614+
615+
def copy(original: Tree)(cond: Term, thenp: Term, elsep: Term)(implicit ctx: Context): If = ???
616+
517617
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Term, Term)] = x match {
518618
case x: tpd.If => Some((x.cond, x.thenp, x.elsep))
519619
case _ => None
@@ -533,6 +633,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
533633
}
534634

535635
object Match extends MatchModule {
636+
def apply(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match =
637+
tpd.Match(selector, cases)
638+
639+
def copy(original: Tree)(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match = ???
640+
536641
def unapply(x: Term)(implicit ctx: Context): Option[(Term, List[CaseDef])] = x match {
537642
case x: tpd.Match => Some((x.selector, x.cases))
538643
case _ => None
@@ -553,6 +658,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
553658
}
554659

555660
object Try extends TryModule {
661+
def apply(expr: Term, cases: List[CaseDef], finalizer: Option[Term])(implicit ctx: Context): Try =
662+
tpd.Try(expr, cases, finalizer.getOrElse(tpd.EmptyTree))
663+
664+
def copy(original: Tree)(expr: Term, cases: List[CaseDef], finalizer: Option[Term])(implicit ctx: Context): Try = ???
665+
556666
def unapply(x: Term)(implicit ctx: Context): Option[(Term, List[CaseDef], Option[Term])] = x match {
557667
case x: tpd.Try => Some((x.expr, x.cases, optional(x.finalizer)))
558668
case _ => None
@@ -571,6 +681,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
571681
}
572682

573683
object Return extends ReturnModule {
684+
def apply(expr: Term)(implicit ctx: Context): Return =
685+
tpd.Return(expr, ctx.owner)
686+
687+
def copy(original: Tree)(expr: Term)(implicit ctx: Context): Return = ???
688+
574689
def unapply(x: Term)(implicit ctx: Context): Option[Term] = x match {
575690
case x: tpd.Return => Some(x.expr)
576691
case _ => None
@@ -589,6 +704,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
589704
}
590705

591706
object Repeated extends RepeatedModule {
707+
def apply(elems: List[Term])(implicit ctx: Context): Repeated =
708+
tpd.SeqLiteral(elems, ???)
709+
710+
def copy(original: Tree)(elems: List[Term])(implicit ctx: Context): Repeated = ???
711+
592712
def unapply(x: Term)(implicit ctx: Context): Option[List[Term]] = x match {
593713
case x: tpd.SeqLiteral => Some(x.elems)
594714
case _ => None
@@ -616,6 +736,12 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
616736
}
617737

618738
object SelectOuter extends SelectOuterModule {
739+
740+
def apply(call: Option[TermOrTypeTree], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined =
741+
tpd.Inlined(call.getOrElse(tpd.EmptyTree), bindings.map { case b: tpd.MemberDef => b }, expansion)
742+
743+
def copy(original: Tree)(call: Option[TermOrTypeTree], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined = ???
744+
619745
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Int, Type)] = x match {
620746
case x: tpd.Select =>
621747
x.name match {
@@ -639,6 +765,11 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
639765
}
640766

641767
object While extends WhileModule {
768+
def apply(cond: Term, body: Term)(implicit ctx: Context): While =
769+
tpd.WhileDo(cond, body)
770+
771+
def copy(original: Tree)(cond: Term, body: Term)(implicit ctx: Context): While = ???
772+
642773
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Term)] = x match {
643774
case x: tpd.WhileDo => Some((x.cond, x.body))
644775
case _ => None

library/src/scala/tasty/reflect/CaseDefOps.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ trait CaseDefOps extends Core {
1313

1414
val CaseDef: CaseDefModule
1515
abstract class CaseDefModule {
16+
17+
def apply(pat: Pattern, guard: Option[Term], body: Term)(implicit ctx: Context): CaseDef
18+
19+
def copy(original: CaseDef)(pat: Pattern, guard: Option[Term], body: Term)(implicit ctx: Context): CaseDef
20+
1621
def unapply(x: CaseDef): Option[(Pattern, Option[Term], Term)]
1722
}
1823

0 commit comments

Comments
 (0)