Skip to content

Commit c61f081

Browse files
committed
Move other extractors to objects
1 parent 6a30fa4 commit c61f081

File tree

8 files changed

+714
-688
lines changed

8 files changed

+714
-688
lines changed

compiler/src/dotty/tools/dotc/tasty/TastyImpl.scala

Lines changed: 350 additions & 338 deletions
Large diffs are not rendered by default.

library/src/scala/tasty/Tasty.scala

Lines changed: 265 additions & 250 deletions
Large diffs are not rendered by default.

library/src/scala/tasty/util/ConstantExtractor.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class ConstantExtractor[T <: Tasty with Singleton](val tasty: T) {
2020

2121
def unapply[T](expr: Expr[T])(implicit ctx: Context): Option[T] = {
2222
def const(tree: Term): Option[T] = tree match {
23-
case Literal(c) => Some(c.value.asInstanceOf[T])
24-
case Block(Nil, e) => const(e)
25-
case Inlined(_, Nil, e) => const(e)
23+
case Term.Literal(c) => Some(c.value.asInstanceOf[T])
24+
case Term.Block(Nil, e) => const(e)
25+
case Term.Inlined(_, Nil, e) => const(e)
2626
case _ => None
2727
}
2828
const(expr.toTasty)

library/src/scala/tasty/util/TastyPrinter.scala

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,43 @@ class TastyPrinter[T <: Tasty with Singleton](val tasty: T) {
2727
def result(): String = sb.result()
2828

2929
def visitTree(x: Tree): Buffer = x match {
30-
case Ident(name) =>
30+
case Term.Ident(name) =>
3131
this += "Ident(" += name += ")"
32-
case Select(qualifier, name, signature) =>
32+
case Term.Select(qualifier, name, signature) =>
3333
this += "Select(" += qualifier += ", " += name += ", " += signature += ")"
34-
case This(qual) =>
34+
case Term.This(qual) =>
3535
this += "This(" += qual += ")"
36-
case Super(qual, mix) =>
36+
case Term.Super(qual, mix) =>
3737
this += "TypeApply(" += qual += ", " += mix += ")"
38-
case Apply(fun, args) =>
38+
case Term.Apply(fun, args) =>
3939
this += "Apply(" += fun += ", " ++= args += ")"
40-
case TypeApply(fun, args) =>
40+
case Term.TypeApply(fun, args) =>
4141
this += "TypeApply(" += fun += ", " ++= args += ")"
42-
case Literal(const) =>
42+
case Term.Literal(const) =>
4343
this += "Literal(" += const += ")"
44-
case New(tpt) =>
44+
case Term.New(tpt) =>
4545
this += "New(" += tpt += ")"
46-
case Typed(expr, tpt) =>
46+
case Term.Typed(expr, tpt) =>
4747
this += "Typed(" += expr += ", " += tpt += ")"
48-
case NamedArg(name, arg) =>
48+
case Term.NamedArg(name, arg) =>
4949
this += "NamedArg(" += name += ", " += arg += ")"
50-
case Assign(lhs, rhs) =>
50+
case Term.Assign(lhs, rhs) =>
5151
this += "Assign(" += lhs += ", " += rhs += ")"
52-
case Block(stats, expr) =>
52+
case Term.Block(stats, expr) =>
5353
this += "Block(" ++= stats += ", " += expr += ")"
54-
case If(cond, thenp, elsep) =>
54+
case Term.If(cond, thenp, elsep) =>
5555
this += "If(" += cond += ", " += thenp += ", " += elsep += ")"
56-
case Lambda(meth, tpt) =>
56+
case Term.Lambda(meth, tpt) =>
5757
this += "Lambda(" += meth += ", " += tpt += ")"
58-
case Match(selector, cases) =>
58+
case Term.Match(selector, cases) =>
5959
this += "Match(" += selector += ", " ++= cases += ")"
60-
case Return(expr) =>
60+
case Term.Return(expr) =>
6161
this += "Return(" += expr += ")"
62-
case Try(block, handlers, finalizer) =>
62+
case Term.Try(block, handlers, finalizer) =>
6363
this += "Try(" += block += ", " ++= handlers += ", " += finalizer += ")"
64-
case Repeated(elems) =>
64+
case Term.Repeated(elems) =>
6565
this += "Repeated(" ++= elems += ")"
66-
case Inlined(call, bindings, expansion) =>
66+
case Term.Inlined(call, bindings, expansion) =>
6767
this += "Inlined(" += call += ", " ++= bindings += ", " += expansion += ")"
6868
case ValDef(name, tpt, rhs) =>
6969
this += "ValDef(" += name += ", " += tpt += ", " += rhs += ")"
@@ -87,25 +87,25 @@ class TastyPrinter[T <: Tasty with Singleton](val tasty: T) {
8787
}
8888

8989
def visitTypeTree(x: TypeOrBoundsTree): Buffer = x match {
90-
case Synthetic() =>
90+
case TypeTree.Synthetic() =>
9191
this += "Synthetic()"
92-
case TypeIdent(name) =>
92+
case TypeTree.TypeIdent(name) =>
9393
this += "TypeIdent(" += name += ")"
94-
case TypeSelect(qualifier, name) =>
94+
case TypeTree.TypeSelect(qualifier, name) =>
9595
this += "TypeSelect(" += qualifier += ", " += name += ")"
96-
case Singleton(ref) =>
96+
case TypeTree.Singleton(ref) =>
9797
this += "Singleton(" += ref += ")"
98-
case And(left, right) =>
98+
case TypeTree.And(left, right) =>
9999
this += "And(" += left += ", " += right += ")"
100-
case Or(left, right) =>
100+
case TypeTree.Or(left, right) =>
101101
this += "Or(" += left += ", " += right += ")"
102-
case Refined(tpt, refinements) =>
102+
case TypeTree.Refined(tpt, refinements) =>
103103
this += "Refined(" += tpt += ", " ++= refinements += ")"
104-
case Applied(tpt, args) =>
104+
case TypeTree.Applied(tpt, args) =>
105105
this += "Applied(" += tpt += ", " ++= args += ")"
106-
case ByName(result) =>
106+
case TypeTree.ByName(result) =>
107107
this += "ByName(" += result += ")"
108-
case Annotated(arg, annot) =>
108+
case TypeTree.Annotated(arg, annot) =>
109109
this += "Annotated(" += arg += ", " += annot += ")"
110110
case TypeBoundsTree(lo, hi) =>
111111
this += "TypeBoundsTree(" += lo += ", " += hi += ")"
@@ -117,15 +117,15 @@ class TastyPrinter[T <: Tasty with Singleton](val tasty: T) {
117117
}
118118

119119
def visitPattern(x: Pattern): Buffer = x match {
120-
case Value(v) =>
120+
case Pattern.Value(v) =>
121121
this += "Value(" += v += ")"
122-
case Bind(name, body) =>
122+
case Pattern.Bind(name, body) =>
123123
this += "Bind(" += name += ", " += body += ")"
124-
case Unapply(fun, implicits, patterns) =>
124+
case Pattern.Unapply(fun, implicits, patterns) =>
125125
this += "Unapply(" += fun += ", " ++= implicits += ", " ++= patterns += ")"
126-
case Alternative(patterns) =>
126+
case Pattern.Alternative(patterns) =>
127127
this += "Alternative(" ++= patterns += ")"
128-
case TypeTest(tpt) =>
128+
case Pattern.TypeTest(tpt) =>
129129
this += "TypeTest(" += tpt += ")"
130130
}
131131

@@ -144,9 +144,9 @@ class TastyPrinter[T <: Tasty with Singleton](val tasty: T) {
144144
}
145145

146146
def visitType(x: TypeOrBounds): Buffer = x match {
147-
case ConstantType(value) =>
147+
case Type.ConstantType(value) =>
148148
this += "ConstantType(" += value += ")"
149-
case SymRef(sym, qual) =>
149+
case Type.SymRef(sym, qual) =>
150150
def visitName(sym: Definition): Buffer = sym match {
151151
case ValDef(name, _, _) => this += name
152152
case DefDef(name, _, _, _, _) => this += name
@@ -158,33 +158,33 @@ class TastyPrinter[T <: Tasty with Singleton](val tasty: T) {
158158
this += "SymRef("
159159
visitName(sym)
160160
this += ", " += qual += ")"
161-
case TermRef(name, qual) =>
161+
case Type.TermRef(name, qual) =>
162162
this += "TermRef(" += name += ", " += qual += ")"
163-
case TypeRef(name, qual) =>
163+
case Type.TypeRef(name, qual) =>
164164
this += "TypeRef(" += name += ", " += qual += ")"
165-
case Refinement(parent, name, info) =>
165+
case Type.Refinement(parent, name, info) =>
166166
this += "Refinement(" += parent += ", " += name += ", " += info += ")"
167-
case AppliedType(tycon, args) =>
167+
case Type.AppliedType(tycon, args) =>
168168
this += "AppliedType(" += tycon += ", " ++= args += ")"
169-
case AnnotatedType(underlying, annot) =>
169+
case Type.AnnotatedType(underlying, annot) =>
170170
this += "AnnotatedType(" += underlying += ", " += annot += ")"
171-
case AndType(left, right) =>
171+
case Type.AndType(left, right) =>
172172
this += "AndType(" += left += ", " += right += ")"
173-
case OrType(left, right) =>
173+
case Type.OrType(left, right) =>
174174
this += "OrType(" += left += ", " += right += ")"
175-
case ByNameType(underlying) =>
175+
case Type.ByNameType(underlying) =>
176176
this += "ByNameType(" += underlying += ")"
177-
case ParamRef(binder, idx) =>
177+
case Type.ParamRef(binder, idx) =>
178178
this += "ParamRef(" += binder+= ", " += idx += ")"
179-
case ThisType(tp) =>
179+
case Type.ThisType(tp) =>
180180
this += "ThisType(" += tp += ")"
181-
case RecursiveThis(binder) =>
181+
case Type.RecursiveThis(binder) =>
182182
this += "RecursiveThis(" += binder += ")"
183-
case MethodType(argNames, argTypes, resType) =>
183+
case Type.MethodType(argNames, argTypes, resType) =>
184184
this += "MethodType(" ++= argNames += ", " ++= argTypes += ", " += resType += ")"
185-
case PolyType(argNames, argBounds, resType) =>
185+
case Type.PolyType(argNames, argBounds, resType) =>
186186
this += "PolyType(" ++= argNames += ", " ++= argBounds += ", " += resType += ")"
187-
case TypeLambda(argNames, argBounds, resType) =>
187+
case Type.TypeLambda(argNames, argBounds, resType) =>
188188
this += "TypeLambda(" ++= argNames += ", " ++= argBounds += ", " += resType += ")"
189189
case TypeBounds(lo, hi) =>
190190
this += "TypeBounds(" += lo += ", " += hi += ")"
@@ -193,10 +193,10 @@ class TastyPrinter[T <: Tasty with Singleton](val tasty: T) {
193193
}
194194

195195
def visitModifier(x: Modifier): Buffer = x match {
196-
case Flags(flags) => this += "Flags(" += flags.toString += ")"
197-
case QualifiedPrivate(tp) => this += "QualifiedPrivate(" += tp += ")"
198-
case QualifiedProtected(tp) => this += "QualifiedProtected(" += tp += ")"
199-
case Annotation(tree) => this += "Annotation(" += tree += ")"
196+
case Modifier.Flags(flags) => this += "Flags(" += flags.toString += ")"
197+
case Modifier.QualifiedPrivate(tp) => this += "QualifiedPrivate(" += tp += ")"
198+
case Modifier.QualifiedProtected(tp) => this += "QualifiedProtected(" += tp += ")"
199+
case Modifier.Annotation(tree) => this += "Annotation(" += tree += ")"
200200
}
201201

202202
def visitId(x: Id): Buffer = {

library/src/scala/tasty/util/TreeAccumulator.scala

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,45 @@ abstract class TreeAccumulator[X, T <: Tasty with Singleton](val tasty: T) {
2020
def foldOverTree(x: X, tree: Tree)(implicit ctx: Context): X = {
2121
def localCtx(definition: Definition): Context = definition.localContext
2222
tree match {
23-
case Ident(_) =>
23+
case Term.Ident(_) =>
2424
x
25-
case Select(qualifier, _, _) =>
25+
case Term.Select(qualifier, _, _) =>
2626
foldTree(x, qualifier)
27-
case This(qual) =>
27+
case Term.This(qual) =>
2828
x
29-
case Super(qual, _) =>
29+
case Term.Super(qual, _) =>
3030
foldTree(x, qual)
31-
case Apply(fun, args) =>
31+
case Term.Apply(fun, args) =>
3232
foldTrees(foldTree(x, fun), args)
33-
case TypeApply(fun, args) =>
33+
case Term.TypeApply(fun, args) =>
3434
foldTypeTrees(foldTree(x, fun), args)
35-
case Literal(const) =>
35+
case Term.Literal(const) =>
3636
x
37-
case New(tpt) =>
37+
case Term.New(tpt) =>
3838
foldTypeTree(x, tpt)
39-
case Typed(expr, tpt) =>
39+
case Term.Typed(expr, tpt) =>
4040
foldTypeTree(foldTree(x, expr), tpt)
41-
case NamedArg(_, arg) =>
41+
case Term.NamedArg(_, arg) =>
4242
foldTree(x, arg)
43-
case Assign(lhs, rhs) =>
43+
case Term.Assign(lhs, rhs) =>
4444
foldTree(foldTree(x, lhs), rhs)
45-
case Block(stats, expr) =>
45+
case Term.Block(stats, expr) =>
4646
foldTree(foldTrees(x, stats), expr)
47-
case If(cond, thenp, elsep) =>
47+
case Term.If(cond, thenp, elsep) =>
4848
foldTree(foldTree(foldTree(x, cond), thenp), elsep)
49-
case Lambda(meth, tpt) =>
49+
case Term.Lambda(meth, tpt) =>
5050
val a = foldTree(x, meth)
5151
tpt.fold(a)(b => foldTypeTree(a, b))
52-
case Match(selector, cases) =>
52+
case Term.Match(selector, cases) =>
5353
foldCaseDefs(foldTree(x, selector), cases)
54-
case Return(expr) =>
54+
case Term.Return(expr) =>
5555
foldTree(x, expr)
56-
case Try(block, handler, finalizer) =>
56+
case Term.Try(block, handler, finalizer) =>
5757
foldTrees(foldCaseDefs(foldTree(x, block), handler), finalizer)
58-
case Repeated(elems) =>
58+
case Term.Repeated(elems) =>
5959
foldTrees(x, elems)
60-
case Inlined(call, bindings, expansion) =>
60+
case Term.Inlined(call, bindings, expansion) =>
6161
foldTree(foldTrees(x, bindings), expansion)
62-
6362
case vdef @ ValDef(_, tpt, rhs) =>
6463
implicit val ctx = localCtx(vdef)
6564
foldTrees(foldTypeTree(x, tpt), rhs)
@@ -80,16 +79,16 @@ abstract class TreeAccumulator[X, T <: Tasty with Singleton](val tasty: T) {
8079
}
8180

8281
def foldOverTypeTree(x: X, tree: TypeOrBoundsTree)(implicit ctx: Context): X = tree match {
83-
case Synthetic() => x
84-
case TypeIdent(_) => x
85-
case TypeSelect(qualifier, _) => foldTree(x, qualifier)
86-
case Singleton(ref) => foldTree(x, ref)
87-
case And(left, right) => foldTypeTree(foldTypeTree(x, left), right)
88-
case Or(left, right) => foldTypeTree(foldTypeTree(x, left), right)
89-
case Refined(tpt, refinements) => foldTrees(foldTypeTree(x, tpt), refinements)
90-
case Applied(tpt, args) => foldTypeTrees(foldTypeTree(x, tpt), args)
91-
case ByName(result) => foldTypeTree(x, result)
92-
case Annotated(arg, annot) => foldTree(foldTypeTree(x, arg), annot)
82+
case TypeTree.Synthetic() => x
83+
case TypeTree.TypeIdent(_) => x
84+
case TypeTree.TypeSelect(qualifier, _) => foldTree(x, qualifier)
85+
case TypeTree.Singleton(ref) => foldTree(x, ref)
86+
case TypeTree.And(left, right) => foldTypeTree(foldTypeTree(x, left), right)
87+
case TypeTree.Or(left, right) => foldTypeTree(foldTypeTree(x, left), right)
88+
case TypeTree.Refined(tpt, refinements) => foldTrees(foldTypeTree(x, tpt), refinements)
89+
case TypeTree.Applied(tpt, args) => foldTypeTrees(foldTypeTree(x, tpt), args)
90+
case TypeTree.ByName(result) => foldTypeTree(x, result)
91+
case TypeTree.Annotated(arg, annot) => foldTree(foldTypeTree(x, arg), annot)
9392
case TypeBoundsTree(lo, hi) => foldTypeTree(foldTypeTree(x, lo), hi)
9493
}
9594

@@ -98,11 +97,11 @@ abstract class TreeAccumulator[X, T <: Tasty with Singleton](val tasty: T) {
9897
}
9998

10099
def foldOverPattern(x: X, tree: Pattern)(implicit ctx: Context): X = tree match {
101-
case Value(v) => foldTree(x, v)
102-
case Bind(_, body) => foldPattern(x, body)
103-
case Unapply(fun, implicits, patterns) => foldPatterns(foldTrees(foldTree(x, fun), implicits), patterns)
104-
case Alternative(patterns) => foldPatterns(x, patterns)
105-
case TypeTest(tpt) => foldTypeTree(x, tpt)
100+
case Pattern.Value(v) => foldTree(x, v)
101+
case Pattern.Bind(_, body) => foldPattern(x, body)
102+
case Pattern.Unapply(fun, implicits, patterns) => foldPatterns(foldTrees(foldTree(x, fun), implicits), patterns)
103+
case Pattern.Alternative(patterns) => foldPatterns(x, patterns)
104+
case Pattern.TypeTest(tpt) => foldTypeTree(x, tpt)
106105
}
107106

108107
private def foldOverParent(x: X, tree: Parent)(implicit ctx: Context): X = tree match {

tests/run/tasty-eval/quoted_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ object Macros {
2323
import u._
2424
import u.tasty._
2525
e.toTasty.tpe match {
26-
case SymRef(ValDef(_, tpt, _), pre) =>
26+
case Type.SymRef(ValDef(_, tpt, _), pre) =>
2727
tpt.tpe match {
28-
case ConstantType(Constant.Int(i)) => Some(i)
28+
case Type.ConstantType(Constant.Int(i)) => Some(i)
2929
case _ => None
3030
}
31-
case ConstantType(Constant.Int(i)) => Some(i)
31+
case Type.ConstantType(Constant.Int(i)) => Some(i)
3232
case _ => None
3333
}
3434
}

tests/run/tasty-indexed-map/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ object Index {
3232
import u.tasty._
3333

3434
def name(tp: TypeOrBounds): String = tp match {
35-
case ConstantType(Constant.String(str)) => str
35+
case Type.ConstantType(Constant.String(str)) => str
3636
}
3737

3838
def names(tp: TypeOrBounds): List[String] = tp match {
39-
case AppliedType(_, x1 :: x2 :: Nil) => name(x1) :: names(x2)
39+
case Type.AppliedType(_, x1 :: x2 :: Nil) => name(x1) :: names(x2)
4040
case _ => Nil
4141
}
4242

tests/run/tasty-macro-assert/quoted_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ object Asserts {
2323
val tree = cond.toTasty
2424

2525
def isOps(tpe: TypeOrBounds): Boolean = tpe match {
26-
case SymRef(DefDef("Ops", _, _, _, _), _) => true // TODO check that the parent is Asserts
26+
case Type.SymRef(DefDef("Ops", _, _, _, _), _) => true // TODO check that the parent is Asserts
2727
case _ => false
2828
}
2929

3030
object OpsTree {
3131
def unapply(arg: Term): Option[Term] = arg match {
32-
case Apply(TypeApply(term, _), left :: Nil) if isOps(term.tpe) =>
32+
case Term.Apply(Term.TypeApply(term, _), left :: Nil) if isOps(term.tpe) =>
3333
Some(left)
3434
case _ => None
3535
}
3636
}
3737

3838
tree match {
39-
case Apply(Select(OpsTree(left), op, _), right :: Nil) =>
39+
case Term.Apply(Term.Select(OpsTree(left), op, _), right :: Nil) =>
4040
// FIXME splice the threes directly
4141
val printer = new TastyPrinter(tasty)
4242
val lExpr = printer.stringOfTree(left).toExpr

0 commit comments

Comments
 (0)