Skip to content

Commit 126edfb

Browse files
committed
Add type and term symbols
1 parent b600803 commit 126edfb

File tree

4 files changed

+79
-35
lines changed

4 files changed

+79
-35
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,11 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
14351435
def PackageDefSymbol_tree(self: PackageDefSymbol)(implicit ctx: Context): PackageDef =
14361436
FromSymbol.packageDefFromSym(self)
14371437

1438+
type TypeSymbol = core.Symbols.TypeSymbol
1439+
1440+
def matchTypeSymbol(symbol: Symbol)(implicit ctx: Context): Option[TypeSymbol] =
1441+
if (symbol.isType) Some(symbol.asType) else None
1442+
14381443
type ClassDefSymbol = core.Symbols.ClassSymbol
14391444

14401445
def matchClassDefSymbol(symbol: Symbol)(implicit ctx: Context): Option[ClassDefSymbol] =
@@ -1522,6 +1527,11 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
15221527
def TypeBindSymbol_tree(self: TypeBindSymbol)(implicit ctx: Context): TypeTree_TypeBind =
15231528
FromSymbol.typeBindFromSym(self)
15241529

1530+
type TermSymbol = core.Symbols.TermSymbol
1531+
1532+
def matchTermSymbol(symbol: Symbol)(implicit ctx: Context): Option[TermSymbol] =
1533+
if (symbol.isTerm) Some(symbol.asTerm) else None
1534+
15251535
type DefDefSymbol = core.Symbols.TermSymbol
15261536

15271537
def matchDefDefSymbol(symbol: Symbol)(implicit ctx: Context): Option[DefDefSymbol] =

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,15 @@ package scala.tasty.reflect
101101
* +- Constant
102102
*
103103
* +- Symbol --+- PackageDefSymbol
104-
* +- ClassDefSymbol
105-
* +- TypeDefSymbol
106-
* +- TypeBindSymbol
107-
* +- DefDefSymbol
108-
* +- ValDefSymbol
109-
* +- BindSymbol
104+
* |
105+
* +- TypeSymbol -+- ClassDefSymbol
106+
* | +- TypeDefSymbol
107+
* | +- TypeBindSymbol
108+
* |
109+
* +- TermSymbol -+- DefDefSymbol
110+
* | +- ValDefSymbol
111+
* | +- BindSymbol
112+
* |
110113
* +- NoSymbol
111114
*
112115
* +- Flags
@@ -421,23 +424,29 @@ trait Core {
421424
/** Symbol of a package definition */
422425
type PackageDefSymbol = kernel.PackageDefSymbol
423426

424-
/** Symbol of a class definition. This includes anonymous class definitions and the class of a module object. */
425-
type ClassDefSymbol = kernel.ClassDefSymbol
427+
/** Symbol representing a type definition. */
428+
type TypeSymbol = kernel.TypeSymbol
426429

427-
/** Symbol of a type (parameter or member) definition. */
428-
type TypeDefSymbol = kernel.TypeDefSymbol
430+
/** Symbol of a class definition. This includes anonymous class definitions and the class of a module object. */
431+
type ClassDefSymbol = kernel.ClassDefSymbol
429432

430-
/** Symbol representing a type bind definition. */
431-
type TypeBindSymbol = kernel.TypeBindSymbol
433+
/** Symbol of a type (parameter or member) definition. */
434+
type TypeDefSymbol = kernel.TypeDefSymbol
432435

433-
/** Symbol representing a method definition. */
434-
type DefDefSymbol = kernel.DefDefSymbol
436+
/** Symbol representing a type bind definition. */
437+
type TypeBindSymbol = kernel.TypeBindSymbol
435438

436-
/** Symbol representing a value definition. This includes `val`, `lazy val`, `var`, `object` and parameter definitions. */
437-
type ValDefSymbol = kernel.ValDefSymbol
439+
/** Symbol representing a term definition. */
440+
type TermSymbol = kernel.TermSymbol
438441

439-
/** Symbol representing a bind definition. */
440-
type BindSymbol = kernel.BindSymbol
442+
/** Symbol representing a method definition. */
443+
type DefDefSymbol = kernel.DefDefSymbol
444+
445+
/** Symbol representing a value definition. This includes `val`, `lazy val`, `var`, `object` and parameter definitions. */
446+
type ValDefSymbol = kernel.ValDefSymbol
447+
448+
/** Symbol representing a bind definition. */
449+
type BindSymbol = kernel.BindSymbol
441450

442451
/** No symbol available. */
443452
type NoSymbol = kernel.NoSymbol

library/src/scala/tasty/reflect/Kernel.scala

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,15 @@ package scala.tasty.reflect
100100
* +- Constant
101101
*
102102
* +- Symbol --+- PackageDefSymbol
103-
* +- ClassDefSymbol
104-
* +- TypeDefSymbol
105-
* +- TypeBindSymbol
106-
* +- DefDefSymbol
107-
* +- ValDefSymbol
108-
* +- BindSymbol
103+
* |
104+
* +- TypeSymbol -+- ClassDefSymbol
105+
* | +- TypeDefSymbol
106+
* | +- TypeBindSymbol
107+
* |
108+
* +- TermSymbol -+- DefDefSymbol
109+
* | +- ValDefSymbol
110+
* | +- BindSymbol
111+
* |
109112
* +- NoSymbol
110113
*
111114
* +- Flags
@@ -249,7 +252,7 @@ trait Kernel {
249252
def DefDef_apply(symbol: DefDefSymbol, rhsFn: List[Type] => List[List[Term]] => Option[Term])(implicit ctx: Context): DefDef
250253
def DefDef_copy(original: DefDef)(name: String, typeParams: List[TypeDef], paramss: List[List[ValDef]], tpt: TypeTree, rhs: Option[Term])(implicit ctx: Context): DefDef
251254

252-
/** Tree representing a value definition in the source code This inclues `val`, `lazy val`, `var`, `object` and parameter defintions. */
255+
/** Tree representing a value definition in the source code This inclues `val`, `lazy val`, `var`, `object` and parameter definitions. */
253256
type ValDef <: Definition
254257

255258
def matchValDef(tree: Tree)(implicit ctx: Context): Option[ValDef]
@@ -1173,12 +1176,16 @@ trait Kernel {
11731176

11741177
def PackageDefSymbol_tree(self: PackageDefSymbol)(implicit ctx: Context): PackageDef
11751178

1179+
type TypeSymbol <: Symbol
1180+
1181+
def matchTypeSymbol(symbol: Symbol)(implicit ctx: Context): Option[TypeSymbol]
1182+
11761183
/** Symbol of a class definition. This includes anonymous class definitions and the class of a module object. */
1177-
type ClassDefSymbol <: Symbol
1184+
type ClassDefSymbol <: TypeSymbol
11781185

11791186
def matchClassDefSymbol(symbol: Symbol)(implicit ctx: Context): Option[ClassDefSymbol]
11801187

1181-
/** ClassDef tree of this defintion */
1188+
/** ClassDef tree of this definition */
11821189
def ClassDefSymbol_tree(self: ClassDefSymbol)(implicit ctx: Context): ClassDef
11831190

11841191
/** Fields directly declared in the class */
@@ -1214,7 +1221,7 @@ trait Kernel {
12141221
def ClassDefSymbol_of(fullName: String)(implicit ctx: Context): ClassDefSymbol
12151222

12161223
/** Symbol of a type (parameter or member) definition. */
1217-
type TypeDefSymbol <: Symbol
1224+
type TypeDefSymbol <: TypeSymbol
12181225

12191226
def matchTypeDefSymbol(symbol: Symbol)(implicit ctx: Context): Option[TypeDefSymbol]
12201227

@@ -1224,30 +1231,34 @@ trait Kernel {
12241231
def TypeDefSymbol_tree(self: TypeDefSymbol)(implicit ctx: Context): TypeDef
12251232

12261233
/** Symbol representing a bind definition. */
1227-
type TypeBindSymbol <: Symbol
1234+
type TypeBindSymbol <: TypeSymbol
12281235

12291236
def matchTypeBindSymbol(symbol: Symbol)(implicit ctx: Context): Option[TypeBindSymbol]
12301237

12311238
/** TypeBind pattern of this definition */
12321239
def TypeBindSymbol_tree(self: TypeBindSymbol)(implicit ctx: Context): TypeTree_TypeBind
12331240

1241+
type TermSymbol <: Symbol
1242+
1243+
def matchTermSymbol(symbol: Symbol)(implicit ctx: Context): Option[TermSymbol]
1244+
12341245
/** Symbol representing a method definition. */
1235-
type DefDefSymbol <: Symbol
1246+
type DefDefSymbol <: TermSymbol
12361247

12371248
def matchDefDefSymbol(symbol: Symbol)(implicit ctx: Context): Option[DefDefSymbol]
12381249

1239-
/** DefDef tree of this defintion */
1250+
/** DefDef tree of this definition */
12401251
def DefDefSymbol_tree(self: DefDefSymbol)(implicit ctx: Context): DefDef
12411252

1242-
/** Signature of this defintion */
1253+
/** Signature of this definition */
12431254
def DefDefSymbol_signature(self: DefDefSymbol)(implicit ctx: Context): Signature
12441255

12451256
/** Symbol representing a value definition. This includes `val`, `lazy val`, `var`, `object` and parameter definitions. */
1246-
type ValDefSymbol <: Symbol
1257+
type ValDefSymbol <: TermSymbol
12471258

12481259
def matchValDefSymbol(symbol: Symbol)(implicit ctx: Context): Option[ValDefSymbol]
12491260

1250-
/** ValDef tree of this defintion */
1261+
/** ValDef tree of this definition */
12511262
def ValDefSymbol_tree(self: ValDefSymbol)(implicit ctx: Context): ValDef
12521263

12531264
/** The class symbol of the companion module class */
@@ -1256,7 +1267,7 @@ trait Kernel {
12561267
def ValDefSymbol_companionClass(self: ValDefSymbol)(implicit ctx: Context): Option[ClassDefSymbol]
12571268

12581269
/** Symbol representing a bind definition. */
1259-
type BindSymbol <: Symbol
1270+
type BindSymbol <: TermSymbol
12601271

12611272
def matchBindSymbol(symbol: Symbol)(implicit ctx: Context): Option[BindSymbol]
12621273

library/src/scala/tasty/reflect/SymbolOps.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ trait SymbolOps extends Core {
9696
kernel.PackageDefSymbol_tree(self)
9797
}
9898

99+
// TypeSymbol
100+
101+
object IsTypeSymbol {
102+
def unapply(symbol: Symbol)(implicit ctx: Context): Option[TypeSymbol] =
103+
kernel.matchTypeSymbol(symbol)
104+
}
105+
99106
// ClassSymbol
100107

101108
object IsClassDefSymbol {
@@ -184,6 +191,13 @@ trait SymbolOps extends Core {
184191
kernel.TypeBindSymbol_tree(self)
185192
}
186193

194+
// TermSymbol
195+
196+
object IsTermSymbol {
197+
def unapply(symbol: Symbol)(implicit ctx: Context): Option[TermSymbol] =
198+
kernel.matchTermSymbol(symbol)
199+
}
200+
187201
// DefSymbol
188202

189203
object IsDefDefSymbol {

0 commit comments

Comments
 (0)