Skip to content

Commit 7b16047

Browse files
committed
Implement TASTy reflect constructors
1 parent fcc35d2 commit 7b16047

16 files changed

+842
-95
lines changed

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

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

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

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

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools.dotc.tastyreflect
22

3+
import dotty.tools.dotc.ast.tpd
34

45
trait CaseDefOpsImpl extends scala.tasty.reflect.CaseDefOps with CoreImpl with Helpers {
56

@@ -10,6 +11,11 @@ trait CaseDefOpsImpl extends scala.tasty.reflect.CaseDefOps with CoreImpl with H
1011
}
1112

1213
object CaseDef extends CaseDefModule {
14+
def apply(pat: Pattern, guard: Option[Term], body: Term)(implicit ctx: Context): CaseDef = ???
15+
16+
def copy(original: CaseDef)(pat: Pattern, guard: Option[Term], body: Term)(implicit ctx: Context): CaseDef =
17+
tpd.cpy.CaseDef(original)(pat, guard.getOrElse(tpd.EmptyTree), body)
18+
1319
def unapply(x: CaseDef): Some[(Pattern, Option[Term], Term)] = Some(x.pat, optional(x.guard), x.body)
1420
}
1521

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

Lines changed: 7 additions & 6 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
@@ -56,8 +57,8 @@ trait CoreImpl extends scala.tasty.reflect.Core {
5657
type Pattern = tpd.Tree
5758
type Value = tpd.Tree
5859
type Bind = tpd.Bind
59-
type Unapply = tpd.Tree
60-
type Alternative = tpd.Alternative
60+
type Unapply = tpd.UnApply
61+
type Alternatives = tpd.Alternative
6162
type TypeTest = tpd.Typed
6263

6364
type TypeOrBoundsTree = tpd.Tree
@@ -77,8 +78,8 @@ trait CoreImpl extends scala.tasty.reflect.Core {
7778
type MatchType = tpd.MatchTypeTree
7879
type ByName = tpd.ByNameTypeTree
7980
type LambdaTypeTree = tpd.LambdaTypeTree
80-
type Bind = tpd.Bind
81-
type Block = tpd.Block
81+
type TypeBind = tpd.Bind
82+
type TypeBlock = tpd.Block
8283
}
8384
type TypeBoundsTree = tpd.TypeBoundsTree
8485
type WildcardType = tpd.TypeTree

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

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
package dotty.tools.dotc.tastyreflect
22

33
import dotty.tools.dotc.ast.{Trees, tpd}
4+
import dotty.tools.dotc.core.Contexts
45
import dotty.tools.dotc.core.Decorators._
56

67
trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {
78

9+
def ValueDeco(value: Value): Pattern.ValueAPI = new Pattern.ValueAPI {
10+
def value(implicit ctx: Context): Term = ???
11+
}
12+
def BindDeco(bind: Bind): Pattern.BindAPI = new Pattern.BindAPI {
13+
def name(implicit ctx: Context): String = ???
14+
def pattern(implicit ctx: Context): Pattern = ???
15+
}
16+
def UnapplyDeco(unapply: Unapply): Pattern.UnapplyAPI = new Pattern.UnapplyAPI {
17+
def fun(implicit ctx: Context): Term = unapply.fun
18+
def implicits(implicit ctx: Context): List[Term] = unapply.implicits
19+
def patterns(implicit ctx: Context): List[Pattern] = effectivePatterns(unapply.patterns)
20+
21+
private def effectivePatterns(patterns: List[Pattern]): List[Pattern] = patterns match {
22+
case patterns0 :+ Trees.SeqLiteral(elems, _) => patterns0 ::: elems
23+
case _ => patterns
24+
}
25+
}
26+
def AlternativeDeco(alternatives: Alternatives): Pattern.AlternativesAPI = new Pattern.AlternativesAPI {
27+
def patterns(implicit ctx: Context): List[Pattern] = alternatives.trees
28+
}
29+
def TypeTestDeco(typeTest: TypeTest): Pattern.TypeTestAPI = new Pattern.TypeTestAPI {
30+
def tpt(implicit ctx: Context): TypeTree = typeTest.tpt
31+
}
32+
833
// ----- Patterns -------------------------------------------------
934

1035
def PatternDeco(pattern: Pattern): PatternAPI = new PatternAPI {
@@ -14,42 +39,79 @@ trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {
1439

1540
object Pattern extends PatternModule {
1641

17-
object Value extends ValueModule {
18-
def unapply(x: Pattern)(implicit ctx: Context): Option[Term] = x match {
42+
object IsValue extends IsValueModule {
43+
def unapply(pattern: Pattern)(implicit ctx: Context): Option[Value] = pattern match {
1944
case lit: tpd.Literal => Some(lit)
2045
case ref: tpd.RefTree if ref.isTerm => Some(ref)
2146
case ths: tpd.This => Some(ths)
2247
case _ => None
2348
}
2449
}
2550

51+
object Value extends ValueModule {
52+
def apply(tpt: Term): Value = ???
53+
def copy(original: Value)(tpt: Term): Value = ???
54+
def unapply(x: Pattern)(implicit ctx: Context): Option[Term] = IsValue.unapply(x)
55+
}
56+
57+
object IsBind extends IsBindModule {
58+
def unapply(x: Pattern)(implicit ctx: Context): Option[Bind] = x match {
59+
case x: tpd.Bind if x.name.isTermName => Some(x)
60+
case _ => None
61+
}
62+
}
63+
2664
object Bind extends BindModule {
27-
def unapply(x: Pattern)(implicit ctx: Context): Option[(String, Pattern)] = x match {
28-
case x: tpd.Bind if x.name.isTermName => Some(x.name.toString, x.body)
65+
def apply(name: String, pattern: Pattern): Bind = ???
66+
def copy(original: Bind)(name: String, pattern: Pattern): Bind = ???
67+
def unapply(pattern: Pattern)(implicit ctx: Context): Option[(String, Pattern)] = pattern match {
68+
case IsBind(pattern) => Some((pattern.name.toString, pattern.body))
69+
case _ => None
70+
}
71+
}
72+
73+
object IsUnapply extends IsUnapplyModule {
74+
def unapply(pattern: Pattern)(implicit ctx: Context): Option[Unapply] = pattern match {
75+
case pattern @ Trees.UnApply(_, _, _) => Some(pattern)
76+
case Trees.Typed(pattern @ Trees.UnApply(_, _, _), _) => Some(pattern)
2977
case _ => None
3078
}
3179
}
3280

3381
object Unapply extends UnapplyModule {
82+
def apply(fun: Term, implicits: List[Term], patterns: List[Pattern]): Unapply = ???
83+
def copy(original: Unapply)(fun: Term, implicits: List[Term], patterns: List[Pattern]): Unapply =
84+
tpd.cpy.UnApply(original)(fun, implicits, patterns)
3485
def unapply(x: Pattern)(implicit ctx: Context): Option[(Term, List[Term], List[Pattern])] = x match {
35-
case Trees.UnApply(fun, implicits, patterns) => Some((fun, implicits, effectivePatterns(patterns)))
36-
case Trees.Typed(Trees.UnApply(fun, implicits, patterns), _) => Some((fun, implicits, effectivePatterns(patterns)))
86+
case IsUnapply(x) => Some((x.fun, x.implicits, UnapplyDeco(x).patterns))
3787
case _ => None
3888
}
39-
private def effectivePatterns(patterns: List[Pattern]): List[Pattern] = patterns match {
40-
case patterns0 :+ Trees.SeqLiteral(elems, _) => patterns0 ::: elems
41-
case _ => patterns
89+
}
90+
91+
object IsAlternatives extends IsAlternativesModule {
92+
def unapply(pattern: Pattern)(implicit ctx: Context): Option[Alternatives] = pattern match {
93+
case pattern: tpd.Alternative => Some(pattern)
94+
case _ => None
4295
}
4396
}
4497

45-
object Alternative extends AlternativeModule {
98+
object Alternatives extends AlternativesModule {
99+
def apply(patterns: List[Pattern]): Alternatives = ???
100+
def copy(original: Alternatives)(patterns: List[Pattern]): Alternatives =
101+
tpd.cpy.Alternative(original)(patterns)
46102
def unapply(x: Pattern)(implicit ctx: Context): Option[List[Pattern]] = x match {
47103
case x: tpd.Alternative => Some(x.trees)
48104
case _ => None
49105
}
50106
}
51107

108+
object IsTypeTest extends IsTypeTestModule {
109+
def unapply(pattern: Pattern)(implicit ctx: Context): Option[TypeTest] = ???
110+
}
111+
52112
object TypeTest extends TypeTestModule {
113+
def apply(tpt: List[TypeTree]): TypeTest = ???
114+
def copy(original: TypeTest)(tpt: List[TypeTree]): TypeTest = ???
53115
def unapply(x: Pattern)(implicit ctx: Context): Option[TypeTree] = x match {
54116
case Trees.Typed(Trees.UnApply(_, _, _), _) => None
55117
case Trees.Typed(_, tpt) => Some(tpt)

0 commit comments

Comments
 (0)