Skip to content

Commit 963719e

Browse files
Merge pull request #6974 from dotty-staging/refactor-type-and-term-ref-api
Refactor TASTy reflect TermRef, TypeRef, SymRef
2 parents 13da159 + ef1d699 commit 963719e

File tree

17 files changed

+271
-321
lines changed

17 files changed

+271
-321
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionInternal.scala renamed to compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
282282

283283
def Ident_name(self: Ident) given Context: String = self.name.show
284284

285-
def Ident_apply(tmref: TermRef) given Context: Term =
285+
def Ident_apply(tmref: NamedTermRef) given Context: Term =
286286
withDefaultPos(tpd.ref(tmref).asInstanceOf[Term])
287287

288288
def Ident_copy(original: Tree)(name: String) given Context: Ident =
@@ -1145,32 +1145,53 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
11451145

11461146
def ConstantType_constant(self: ConstantType) given Context: Constant = self.value
11471147

1148-
type SymRef = Types.NamedType
1148+
type TermRef = Types.NamedType
11491149

1150-
def matchSymRef(tpe: TypeOrBounds) given Context: Option[SymRef] = tpe match {
1150+
def matchTermRef(tpe: TypeOrBounds) given Context: Option[TermRef] = tpe match {
11511151
case tp: Types.NamedType =>
11521152
tp.designator match {
1153-
case sym: Symbol => Some(tp)
1153+
case sym: Symbol if sym.isTerm => Some(tp)
11541154
case _ => None
11551155
}
11561156
case _ => None
11571157
}
11581158

1159-
def SymRef_qualifier(self: SymRef) given Context: TypeOrBounds = self.prefix
1159+
def TermRef_qualifier(self: TermRef) given Context: TypeOrBounds = self.prefix
11601160

1161-
// TODO remove this method. May require splitting SymRef into TypeSymRef and TermSymRef
1162-
def matchSymRef_unapply(tpe: TypeOrBounds) given Context: Option[(Symbol, Type | NoPrefix)] = tpe match {
1161+
def matchTermRef_unapply(tpe: TypeOrBounds) given Context: Option[(Symbol, Type | NoPrefix)] = tpe match {
11631162
case tpe: Types.NamedType =>
11641163
tpe.designator match {
1165-
case sym: Symbol => Some((sym, tpe.prefix))
1164+
case sym: Symbol if sym.isTerm => Some((sym, tpe.prefix))
11661165
case _ => None
11671166
}
11681167
case _ => None
11691168
}
11701169

1171-
type TermRef = Types.NamedType
1170+
type TypeRef = Types.NamedType
11721171

1173-
def matchTermRef(tpe: TypeOrBounds) given Context: Option[TermRef] = tpe match {
1172+
def matchTypeRef(tpe: TypeOrBounds) given Context: Option[TypeRef] = tpe match {
1173+
case tp: Types.NamedType =>
1174+
tp.designator match {
1175+
case sym: Symbol if sym.isType => Some(tp)
1176+
case _ => None
1177+
}
1178+
case _ => None
1179+
}
1180+
1181+
def TypeRef_qualifier(self: TypeRef) given Context: TypeOrBounds = self.prefix
1182+
1183+
def matchTypeRef_unapply(tpe: TypeOrBounds) given Context: Option[(Symbol, Type | NoPrefix)] = tpe match {
1184+
case tpe: Types.NamedType =>
1185+
tpe.designator match {
1186+
case sym: Symbol if sym.isType => Some((sym, tpe.prefix))
1187+
case _ => None
1188+
}
1189+
case _ => None
1190+
}
1191+
1192+
type NamedTermRef = Types.NamedType
1193+
1194+
def matchNamedTermRef(tpe: TypeOrBounds) given Context: Option[NamedTermRef] = tpe match {
11741195
case tpe: Types.NamedType =>
11751196
tpe.designator match {
11761197
case name: Names.TermName => Some(tpe)
@@ -1179,15 +1200,15 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
11791200
case _ => None
11801201
}
11811202

1182-
def TermRef_name(self: TermRef) given Context: String = self.name.toString
1183-
def TermRef_qualifier(self: TermRef) given Context: TypeOrBounds = self.prefix
1203+
def NamedTermRef_name(self: NamedTermRef) given Context: String = self.name.toString
1204+
def NamedTermRef_qualifier(self: NamedTermRef) given Context: TypeOrBounds = self.prefix
11841205

1185-
def TermRef_apply(qual: TypeOrBounds, name: String) given Context: TermRef =
1206+
def NamedTermRef_apply(qual: TypeOrBounds, name: String) given Context: NamedTermRef =
11861207
Types.TermRef(qual, name.toTermName)
11871208

1188-
type TypeRef = Types.NamedType
1209+
type NamedTypeRef = Types.NamedType
11891210

1190-
def matchTypeRef(tpe: TypeOrBounds) given Context: Option[TypeRef] = tpe match {
1211+
def matchNamedTypeRef(tpe: TypeOrBounds) given Context: Option[NamedTypeRef] = tpe match {
11911212
case tpe: Types.NamedType =>
11921213
tpe.designator match {
11931214
case name: Names.TypeName => Some(tpe)
@@ -1196,8 +1217,8 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
11961217
case _ => None
11971218
}
11981219

1199-
def TypeRef_name(self: TypeRef) given Context: String = self.name.toString
1200-
def TypeRef_qualifier(self: TypeRef) given Context: TypeOrBounds = self.prefix
1220+
def NamedTypeRef_name(self: NamedTypeRef) given Context: String = self.name.toString
1221+
def NamedTypeRef_qualifier(self: NamedTypeRef) given Context: TypeOrBounds = self.prefix
12011222

12021223
type SuperType = Types.SuperType
12031224

docs/docs/reference/metaprogramming/tasty-reflect.md

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -151,116 +151,6 @@ def let(rhs: Term)(body: Ident => Term): Term = ...
151151
def lets(terms: List[Term])(body: List[Term] => Term): Term = ...
152152
```
153153

154-
155-
## TASTy Reflect API
156-
157-
TASTy Reflect provides the following types:
158-
159-
```none
160-
+- Tree -+- PackageClause
161-
+- Import
162-
+- Statement -+- Definition --+- PackageDef
163-
| | +- ClassDef
164-
| | +- TypeDef
165-
| | +- DefDef
166-
| | +- ValDef
167-
| |
168-
| +- Term --------+- Ref -+- Ident
169-
| | +- Select
170-
| |
171-
| +- Literal
172-
| +- This
173-
| +- New
174-
| +- NamedArg
175-
| +- Apply
176-
| +- TypeApply
177-
| +- Super
178-
| +- Typed
179-
| +- Assign
180-
| +- Block
181-
| +- Lambda
182-
| +- If
183-
| +- Match
184-
| +- ImplicitMatch
185-
| +- Try
186-
| +- Return
187-
| +- Repeated
188-
| +- Inlined
189-
| +- SelectOuter
190-
| +- While
191-
|
192-
|
193-
+- TypeTree ----+- Inferred
194-
| +- TypeIdent
195-
| +- TypeSelect
196-
| +- Projection
197-
| +- Singleton
198-
| +- Refined
199-
| +- Applied
200-
| +- Annotated
201-
| +- MatchTypeTree
202-
| +- ByName
203-
| +- LambdaTypeTree
204-
| +- TypeBind
205-
| +- TypeBlock
206-
|
207-
+- TypeBoundsTree
208-
+- WildcardTypeTree
209-
+- CaseDef
210-
+- TypeCaseDef
211-
212-
+- Pattern --+- Value
213-
+- Bind
214-
+- Unapply
215-
+- Alternatives
216-
+- TypeTest
217-
+- WildcardPattern
218-
219-
+- NoPrefix
220-
+- TypeOrBounds -+- TypeBounds
221-
|
222-
+- Type -------+- ConstantType
223-
+- SymRef
224-
+- TermRef
225-
+- TypeRef
226-
+- SuperType
227-
+- Refinement
228-
+- AppliedType
229-
+- AnnotatedType
230-
+- AndType
231-
+- OrType
232-
+- MatchType
233-
+- ByNameType
234-
+- ParamRef
235-
+- ThisType
236-
+- RecursiveThis
237-
+- RecursiveType
238-
+- LambdaType[ParamInfo <: TypeOrBounds] -+- MethodType
239-
+- PolyType
240-
+- TypeLambda
241-
+- ImportSelector -+- SimpleSelector
242-
+- RenameSelector
243-
+- OmitSelector
244-
+- Id
245-
+- Signature
246-
+- Position
247-
+- Comment
248-
+- Constant
249-
+- Symbol --+- PackageDefSymbol
250-
|
251-
+- TypeSymbol -+- ClassDefSymbol
252-
| +- TypeDefSymbol
253-
| +- TypeBindSymbol
254-
|
255-
+- TermSymbol -+- DefDefSymbol
256-
| +- ValDefSymbol
257-
| +- BindSymbol
258-
|
259-
+- NoSymbol
260-
+- Flags
261-
262-
```
263-
264154
## More Examples
265155

266156
* Start experimenting with TASTy Reflect ([link](https://github.com/nicolasstucki/tasty-reflection-exercise))

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ import scala.runtime.quoted.Unpickler
7272
* +- TypeOrBounds -+- TypeBounds
7373
* |
7474
* +- Type -------+- ConstantType
75-
* +- SymRef
7675
* +- TermRef
7776
* +- TypeRef
77+
* +- NamedTermRef
78+
* +- NamedTypeRef
7879
* +- SuperType
7980
* +- Refinement
8081
* +- AppliedType
@@ -329,7 +330,7 @@ trait CompilerInterface {
329330

330331
def Ident_name(self: Ident) given (ctx: Context): String
331332

332-
def Ident_apply(tmref: TermRef) given (ctx: Context): Term
333+
def Ident_apply(tmref: NamedTermRef) given (ctx: Context): Term
333334
def Ident_copy(original: Tree)(name: String) given (ctx: Context): Ident
334335

335336
/** Tree representing a selection of definition with a given name on a given prefix */
@@ -925,34 +926,42 @@ trait CompilerInterface {
925926

926927
def ConstantType_constant(self: ConstantType) given (ctx: Context): Constant
927928

928-
/** Type of a reference to a symbol */
929-
type SymRef <: Type
930-
931-
def matchSymRef(tpe: TypeOrBounds) given (ctx: Context): Option[SymRef]
932-
933-
// TODO remove this method. May require splitting SymRef into TypeSymRef and TermSymRef
934-
def matchSymRef_unapply(tpe: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)]
935-
936-
def SymRef_qualifier(self: SymRef) given (ctx: Context): TypeOrBounds
937-
938-
/** Type of a reference to a term */
929+
/** Type of a reference to a term symbol */
939930
type TermRef <: Type
940931

941932
def matchTermRef(tpe: TypeOrBounds) given (ctx: Context): Option[TermRef]
942933

943-
def TermRef_name(self: TermRef) given (ctx: Context): String
944-
def TermRef_qualifier(self: TermRef) given (ctx: Context): TypeOrBounds
934+
def matchTermRef_unapply(tpe: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)]
945935

946-
def TermRef_apply(qual: TypeOrBounds, name: String) given (ctx: Context): TermRef
936+
def TermRef_qualifier(self: TermRef) given (ctx: Context): TypeOrBounds
947937

948-
/** Type of a reference to a type */
938+
/** Type of a reference to a type symbol */
949939
type TypeRef <: Type
950940

951941
def matchTypeRef(tpe: TypeOrBounds) given (ctx: Context): Option[TypeRef]
952942

953-
def TypeRef_name(self: TypeRef) given (ctx: Context): String
943+
def matchTypeRef_unapply(tpe: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)]
944+
954945
def TypeRef_qualifier(self: TypeRef) given (ctx: Context): TypeOrBounds
955946

947+
/** Type of a reference to a term by it's name */
948+
type NamedTermRef <: Type
949+
950+
def matchNamedTermRef(tpe: TypeOrBounds) given (ctx: Context): Option[NamedTermRef]
951+
952+
def NamedTermRef_name(self: NamedTermRef) given (ctx: Context): String
953+
def NamedTermRef_qualifier(self: NamedTermRef) given (ctx: Context): TypeOrBounds
954+
955+
def NamedTermRef_apply(qual: TypeOrBounds, name: String) given (ctx: Context): NamedTermRef
956+
957+
/** Type of a reference to a type by it's name */
958+
type NamedTypeRef <: Type
959+
960+
def matchNamedTypeRef(tpe: TypeOrBounds) given (ctx: Context): Option[NamedTypeRef]
961+
962+
def NamedTypeRef_name(self: NamedTypeRef) given (ctx: Context): String
963+
def NamedTypeRef_qualifier(self: NamedTypeRef) given (ctx: Context): TypeOrBounds
964+
956965
/** Type of a `super` refernce */
957966
type SuperType <: Type
958967

library/src/scala/tasty/reflect/Core.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ package scala.tasty.reflect
6969
* +- TypeOrBounds -+- TypeBounds
7070
* |
7171
* +- Type -------+- ConstantType
72-
* +- SymRef
7372
* +- TermRef
7473
* +- TypeRef
74+
* +- NamedTermRef
75+
* +- NamedTypeRef
7576
* +- SuperType
7677
* +- Refinement
7778
* +- AppliedType
@@ -331,14 +332,17 @@ trait Core {
331332
/** A singleton type representing a known constant value */
332333
type ConstantType = internal.ConstantType
333334

334-
/** Type of a reference to a symbol */
335-
type SymRef = internal.SymRef
335+
/** Type of a reference to a term symbol */
336+
type TermRef = internal.TermRef
337+
338+
/** Type of a reference to a type symbol */
339+
type TypeRef = internal.TypeRef
336340

337341
/** Type of a reference to a term */
338-
type TermRef = internal.TermRef
342+
type NamedTermRef = internal.NamedTermRef
339343

340344
/** Type of a reference to a type */
341-
type TypeRef = internal.TypeRef
345+
type NamedTypeRef = internal.NamedTypeRef
342346

343347
/** Type of a `super` refernce */
344348
type SuperType = internal.SuperType

0 commit comments

Comments
 (0)