Skip to content

Commit 6a71449

Browse files
committed
Simplify Tasty trees
Several changes mean that quite a bit of boilerplate can be removed. Also, some fixes.
1 parent 7fdddd4 commit 6a71449

File tree

1 file changed

+99
-128
lines changed

1 file changed

+99
-128
lines changed

tests/pos/TastyADT.scala

Lines changed: 99 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ object tasty {
1212
case DefaultGetter(methodName: TermName, idx: String) // s"$methodName${"$default$"}${idx+1}"
1313
case Variant(underlying: TermName, covariant: Boolean) // s"${if (covariant) "+" else "-"}$underlying"
1414
case SuperAccessor(underlying: TermName) // s"${"super$"}$underlying"
15-
case ProtectedAccessor(underlying: TermName) // s"${"protectded$"}$underlying"
16-
case ProtectedSetter(underlying: TermName) // s"${"protectded$set"}$underlying"
15+
case protectedAccessor(underlying: TermName) // s"${"protectded$"}$underlying"
16+
case protectecSetter(underlying: TermName) // s"${"protectded$set"}$underlying"
1717
case ObjectClass(underlying: TermName) // s"$underlying${"$"}"
1818

1919
case Expanded(prefix: TermName, selector: String) // s"$prefix${"$$"}$name" , used only for symbols coming from Scala 2
@@ -34,83 +34,11 @@ object tasty {
3434

3535
// ------ Statements ---------------------------------
3636

37-
// Note: Definitions are written as extractors, because they may be referred to
38-
// recursively from some of their arguments (since we equate symbols with definitions)
39-
4037
trait TopLevelStatement extends Positioned
41-
4238
trait Statement extends TopLevelStatement
4339

4440
case class Package(pkg: Term, body: List[TopLevelStatement]) extends TopLevelStatement
4541

46-
trait Definition extends Statement {
47-
def tpe: Type = Type.SymRef(this, ???)
48-
}
49-
50-
class ValDef(
51-
val name: TermName,
52-
val tpt: Term,
53-
rhsExp: ValDef => Term | Empty,
54-
val mods: List[Modifier])
55-
extends Definition {
56-
lazy val rhs = rhsExp(this)
57-
}
58-
object ValDef {
59-
def apply(name: TermName, tpt: Term, rhs: Term | Empty, mods: List[Modifier] = Nil) =
60-
new ValDef(name, tpt, _ => rhs, mods)
61-
def unapply(vdef: ValDef) = Some((vdef.name, vdef.tpt, vdef.rhs, vdef.mods))
62-
}
63-
64-
class DefDef(
65-
val name: TermName,
66-
typeParamsExp: DefDef => List[TypeDef],
67-
paramssExp: DefDef => List[List[ValDef]],
68-
returnTptExp: DefDef => Term,
69-
rhsExp: DefDef => Term | Empty,
70-
val mods: List[Modifier])
71-
extends Definition {
72-
val typeParams = typeParamsExp(this)
73-
val paramss = paramssExp(this)
74-
val returnTpt = returnTptExp(this)
75-
lazy val rhs = rhsExp(this)
76-
}
77-
object DefDef {
78-
def apply(name: TermName, typeParams: List[TypeDef], paramss: List[List[ValDef]], returnTpt: Term, rhs: Term | Empty, mods: List[Modifier] = Nil) =
79-
new DefDef(name, _ => typeParams, _ => paramss, _ => returnTpt, _ => rhs, mods)
80-
def unapply(ddef: DefDef) = Some((ddef.name, ddef.typeParams, ddef.paramss, ddef.returnTpt, ddef.rhs, ddef.mods))
81-
}
82-
83-
class TypeDef(
84-
val name: TypeName,
85-
rhsExp: TypeDef => Term,
86-
val mods: List[Modifier])
87-
extends Definition {
88-
val rhs = rhsExp(this),
89-
}
90-
object TypeDef {
91-
def apply(name: TypeName, rhs: Term, mods: List[Modifier] = Nil) = new TypeDef(name, _ => rhs, mods)
92-
def unapply(tdef: TypeDef) = Some((tdef.name, tdef.rhs, tdef.mods))
93-
}
94-
95-
class ClassDef(
96-
val name: TypeName,
97-
rhsExp: ClassDef => Template,
98-
val mods: List[Modifier])
99-
extends Definition {
100-
val rhs = rhsExp(this)
101-
}
102-
object ClassDef {
103-
def apply(name: TypeName, rhs: Template, mods: List[Modifier] = Nil) = new ClassDef(name, _ => rhs, mods)
104-
def unapply(tdef: ClassDef) = Some((tdef.name, tdef.rhs, tdef.mods))
105-
}
106-
107-
case class Template(
108-
typeParams: List[TypeDef],
109-
paramss: List[List[ValDef]],
110-
parents: List[Term],
111-
self: ValDef | Empty,
112-
body: List[Statement])
113-
11442
case class Import(expr: Term, selector: List[ImportSelector]) extends Statement
11543

11644
enum ImportSelector {
@@ -121,6 +49,25 @@ object tasty {
12149

12250
case class Id(name: String) extends Positioned // untyped ident
12351

52+
// ------ Definitions ---------------------------------
53+
54+
class Symbol {
55+
def owner: Symbol = ???
56+
def definition: Definition = ???
57+
}
58+
object NoSymbol extends Symbol
59+
60+
trait Definition {
61+
def sym: Symbol = ???
62+
}
63+
64+
case class ValDef(name: TermName, tpt: Term, rhs: Term | Empty, mods: List[Modifier]) extends Definition
65+
case class DefDef(name: TermName, typeParams: List[TypeDef], paramss: List[List[ValDef]],
66+
returnTpt: Term, rhs: Term | Empty, mods: List[Modifier]) extends Definition
67+
case class TypeDef(name: TypeName, rhs: Term, mods: List[Modifier]) extends Definition
68+
case class ClassDef(name: TypeName, constructor: DefDef, parents: List[Term],
69+
self: ValDef | Empty, body: List[Statement], mods: List[Modifier]) extends Definition
70+
12471
// ------ Terms ---------------------------------
12572

12673
/** Trees denoting terms */
@@ -177,83 +124,82 @@ object tasty {
177124

178125
case class CaseDef(pat: Pattern, guard: Term | Empty, rhs: Term) extends Positioned
179126

180-
sealed trait Type
181-
182127
// ------ Types ---------------------------------
183128

129+
sealed trait Type
130+
184131
object Type {
132+
private val PlaceHolder = SymRef(NoSymbol, Empty)
133+
185134
case class ConstantType(value: Constant) extends Type
186-
case class SymRef(sym: Definition, qualifier: Type | Empty = Empty) extends Type
135+
case class SymRef(sym: Symbol, qualifier: Type | Empty = Empty) extends Type
187136
case class NameRef(name: Name, qualifier: Type | Empty = Empty) extends Type // Empty means: select from _root_
188137
case class SuperType(thistp: Type, underlying: Type) extends Type
189138
case class Refinement(underlying: Type, name: Name, tpe: Type) extends Type
190-
case class AppliedType(tycon: Type, args: Type | TypeBounds) extends Type
139+
case class AppliedType(tycon: Type, args: List[Type | TypeBounds]) extends Type
191140
case class AnnotatedType(underlying: Type, annotation: Term) extends Type
192141
case class AndType(left: Type, right: Type) extends Type
193142
case class OrType(left: Type, right: Type) extends Type
194143
case class ByNameType(underlying: Type) extends Type
195-
case class ParamRef(binder: LambdaType, idx: Int) extends Type
196-
case class RecThis(binder: RecursiveType) extends Type
197-
198-
// The following types are all expressed by extractors because they may be referred
199-
// to from some of their arguments
144+
case class ParamRef(binder: LambdaType[_, _, _], idx: Int) extends Type
145+
case class RecursiveThis(binder: RecursiveType) extends Type
200146

201-
class RecursiveType(underlyingExp: RecursiveType => Type) extends Type {
202-
val underlying = underlyingExp(this)
147+
abstract case class RecursiveType(private var _underlying: Type) extends Type {
148+
def underlying = _underlying
203149
}
204150
object RecursiveType {
205-
def unapply(tp: RecursiveType): Option[Type] = Some(tp.underlying)
151+
def apply(underlyingExp: RecursiveType => Type) = {
152+
val rt = new RecursiveType(PlaceHolder) {}
153+
rt._underlying = underlyingExp(rt)
154+
rt
155+
}
206156
}
207157

208-
trait LambdaType extends Type {
209-
type ParamName
210-
type ParamInfo
158+
abstract class LambdaType[ParamName, ParamInfo, This <: LambdaType[ParamName, ParamInfo, This]](
159+
val companion: LambdaTypeCompanion[ParamName, ParamInfo, This]
160+
) {
161+
private[Type] var _pinfos: List[ParamInfo]
162+
private[Type] var _restpe: Type
163+
211164
def paramNames: List[ParamName]
212-
def paramInfos: List[ParamInfo]
213-
def resultType: Type
165+
def paramInfos: List[ParamInfo] = _pinfos
166+
def resultType: Type = _restpe
214167
}
215168

216-
class MethodType(val paramNames: List[TermName], paramTypesExp: MethodType => List[Type],
217-
resultTypeExp: MethodType => Type, val mods: List[Modifier]) extends LambdaType {
218-
type ParamName = TermName
219-
type ParamInfo = Type
220-
val paramTypes = paramTypesExp(this)
221-
val resultType = resultTypeExp(this)
222-
def paramInfos = paramTypes
223-
}
224-
object MethodType {
225-
def apply(paramNames: List[TermName], paramTypes: List[Type], resultType: Type, mods: List[Modifier] = Nil) =
226-
new MethodType(paramNames, _ => paramTypes, _ => resultType, mods)
227-
def unapply(tp: MethodType) = Some((tp.paramNames, tp.paramTypes, tp.resultType, tp.mods))
228-
}
169+
abstract class LambdaTypeCompanion[ParamName, ParamInfo, This <: LambdaType[ParamName, ParamInfo, This]] {
170+
def apply(pnames: List[ParamName], ptypes: List[ParamInfo], restpe: Type): This
229171

230-
class PolyType(val paramNames: List[TypeName], paramBoundsExp: PolyType => List[TypeBounds],
231-
resultTypeExp: PolyType => Type) extends LambdaType {
232-
type ParamName = TypeName
233-
type ParamInfo = TypeBounds
234-
val paramBounds = paramBoundsExp(this)
235-
val resultType = resultTypeExp(this)
236-
def paramInfos = paramBounds
237-
}
238-
object PolyType {
239-
def apply(paramNames: List[TypeName], paramBounds: List[TypeBounds], resultType: Type) =
240-
new PolyType(paramNames, _ => paramBounds, _ => resultType)
241-
def unapply(tp: PolyType) = Some((tp.paramNames, tp.paramBounds, tp.resultType))
172+
def apply(pnames: List[ParamName], ptypesExp: This => List[ParamInfo], restpeExp: This => Type): This = {
173+
val lambda = apply(pnames, Nil, PlaceHolder)
174+
lambda._pinfos = ptypesExp(lambda)
175+
lambda._restpe = restpeExp(lambda)
176+
lambda
177+
}
242178
}
243179

244-
class TypeLambda(val paramNames: List[TypeName], paramBoundsExp: TypeLambda => List[TypeBounds],
245-
resultTypeExp: TypeLambda => Type) extends LambdaType {
246-
type ParamName = TypeName
247-
type ParamInfo = TypeBounds
248-
val paramBounds = paramBoundsExp(this)
249-
val resultType = resultTypeExp(this)
250-
def paramInfos = paramBounds
180+
case class MethodType(paramNames: List[TermName], private[Type] var _pinfos: List[Type], private[Type] var _restpe: Type)
181+
extends LambdaType[TermName, Type, MethodType](MethodType) {
182+
def isImplicit = (companion `eq` ImplicitMethodType) || (companion `eq` ErasedImplicitMethodType)
183+
def isErased = (companion `eq` ErasedMethodType) || (companion `eq` ErasedImplicitMethodType)
251184
}
252-
object TypeLambda {
253-
def apply(paramNames: List[TypeName], paramBounds: List[TypeBounds], resultType: Type) =
254-
new TypeLambda(paramNames, _ => paramBounds, _ => resultType)
255-
def unapply(tp: TypeLambda) = Some((tp.paramNames, tp.paramBounds, tp.resultType))
185+
186+
case class PolyType(paramNames: List[TypeName], private[Type] var _pinfos: List[TypeBounds], private[Type] var _restpe: Type)
187+
extends LambdaType[TypeName, TypeBounds, PolyType](PolyType)
188+
189+
case class TypeLambda(paramNames: List[TypeName], private[Type] var _pinfos: List[TypeBounds], private[Type] var _restpe: Type)
190+
extends LambdaType[TypeName, TypeBounds, TypeLambda](TypeLambda)
191+
192+
object TypeLambda extends LambdaTypeCompanion[TypeName, TypeBounds, TypeLambda]
193+
object PolyType extends LambdaTypeCompanion[TypeName, TypeBounds, PolyType]
194+
object MethodType extends LambdaTypeCompanion[TermName, Type, MethodType]
195+
196+
class SpecializedMethodTypeCompanion extends LambdaTypeCompanion[TermName, Type, MethodType] { self =>
197+
def apply(pnames: List[TermName], ptypes: List[Type], restpe: Type): MethodType =
198+
new MethodType(pnames, ptypes, restpe) { override val companion = self }
256199
}
200+
object ImplicitMethodType extends SpecializedMethodTypeCompanion
201+
object ErasedMethodType extends SpecializedMethodTypeCompanion
202+
object ErasedImplicitMethodType extends SpecializedMethodTypeCompanion
257203

258204
case class TypeBounds(loBound: Type, hiBound: Type)
259205
}
@@ -266,7 +212,7 @@ object tasty {
266212
Static, // mapped to static Java member
267213
Object, // an object or its class (used for a ValDef or a ClassDef, respectively)
268214
Trait, // a trait (used for a ClassDef)
269-
Local, // used in conjunction with Private/Protected to mean private[this], proctected[this]
215+
Local, // used in conjunction with Private/private[Type] to mean private[this], proctected[this]
270216
Synthetic, // generated by Scala compiler
271217
Artifact, // to be tagged Java Synthetic
272218
Mutable, // when used on a ValDef: a var
@@ -305,4 +251,29 @@ object tasty {
305251

306252
sealed class Empty()
307253
object Empty extends Empty
254+
}
255+
256+
object Test {
257+
import tasty._
258+
import Type._
259+
260+
def show(tp: Type) = tp match {
261+
case ConstantType(value) => ???
262+
case SymRef(sym, Empty) => ???
263+
case SymRef(sym, qual) => ???
264+
case NameRef(name: Name, qualifier) => ???
265+
case SuperType(thistp: Type, underlying: Type) => ???
266+
case Refinement(underlying: Type, name: Name, tpe: Type) => ???
267+
case AppliedType(tycon, args) => ???
268+
case AnnotatedType(underlying: Type, annotation: Term) => ???
269+
case AndType(left: Type, right: Type) => ???
270+
case OrType(left: Type, right: Type) => ???
271+
case ByNameType(underlying: Type) => ???
272+
case ParamRef(binder, idx) => ???
273+
case RecursiveThis(binder: RecursiveType) => ???
274+
case RecursiveType(tp) => ???
275+
case MethodType(pnames, ptypes, resType) => ???
276+
case PolyType(pnames, ptypes, resType) => ???
277+
case TypeLambda(pnames, ptypes, resType) => ???
278+
}
308279
}

0 commit comments

Comments
 (0)