Skip to content

Commit

Permalink
Add type and term symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Mar 21, 2019
1 parent b600803 commit 126edfb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 35 deletions.
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,11 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
def PackageDefSymbol_tree(self: PackageDefSymbol)(implicit ctx: Context): PackageDef =
FromSymbol.packageDefFromSym(self)

type TypeSymbol = core.Symbols.TypeSymbol

def matchTypeSymbol(symbol: Symbol)(implicit ctx: Context): Option[TypeSymbol] =
if (symbol.isType) Some(symbol.asType) else None

type ClassDefSymbol = core.Symbols.ClassSymbol

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

type TermSymbol = core.Symbols.TermSymbol

def matchTermSymbol(symbol: Symbol)(implicit ctx: Context): Option[TermSymbol] =
if (symbol.isTerm) Some(symbol.asTerm) else None

type DefDefSymbol = core.Symbols.TermSymbol

def matchDefDefSymbol(symbol: Symbol)(implicit ctx: Context): Option[DefDefSymbol] =
Expand Down
45 changes: 27 additions & 18 deletions library/src/scala/tasty/reflect/Core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ package scala.tasty.reflect
* +- Constant
*
* +- Symbol --+- PackageDefSymbol
* +- ClassDefSymbol
* +- TypeDefSymbol
* +- TypeBindSymbol
* +- DefDefSymbol
* +- ValDefSymbol
* +- BindSymbol
* |
* +- TypeSymbol -+- ClassDefSymbol
* | +- TypeDefSymbol
* | +- TypeBindSymbol
* |
* +- TermSymbol -+- DefDefSymbol
* | +- ValDefSymbol
* | +- BindSymbol
* |
* +- NoSymbol
*
* +- Flags
Expand Down Expand Up @@ -421,23 +424,29 @@ trait Core {
/** Symbol of a package definition */
type PackageDefSymbol = kernel.PackageDefSymbol

/** Symbol of a class definition. This includes anonymous class definitions and the class of a module object. */
type ClassDefSymbol = kernel.ClassDefSymbol
/** Symbol representing a type definition. */
type TypeSymbol = kernel.TypeSymbol

/** Symbol of a type (parameter or member) definition. */
type TypeDefSymbol = kernel.TypeDefSymbol
/** Symbol of a class definition. This includes anonymous class definitions and the class of a module object. */
type ClassDefSymbol = kernel.ClassDefSymbol

/** Symbol representing a type bind definition. */
type TypeBindSymbol = kernel.TypeBindSymbol
/** Symbol of a type (parameter or member) definition. */
type TypeDefSymbol = kernel.TypeDefSymbol

/** Symbol representing a method definition. */
type DefDefSymbol = kernel.DefDefSymbol
/** Symbol representing a type bind definition. */
type TypeBindSymbol = kernel.TypeBindSymbol

/** Symbol representing a value definition. This includes `val`, `lazy val`, `var`, `object` and parameter definitions. */
type ValDefSymbol = kernel.ValDefSymbol
/** Symbol representing a term definition. */
type TermSymbol = kernel.TermSymbol

/** Symbol representing a bind definition. */
type BindSymbol = kernel.BindSymbol
/** Symbol representing a method definition. */
type DefDefSymbol = kernel.DefDefSymbol

/** Symbol representing a value definition. This includes `val`, `lazy val`, `var`, `object` and parameter definitions. */
type ValDefSymbol = kernel.ValDefSymbol

/** Symbol representing a bind definition. */
type BindSymbol = kernel.BindSymbol

/** No symbol available. */
type NoSymbol = kernel.NoSymbol
Expand Down
45 changes: 28 additions & 17 deletions library/src/scala/tasty/reflect/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ package scala.tasty.reflect
* +- Constant
*
* +- Symbol --+- PackageDefSymbol
* +- ClassDefSymbol
* +- TypeDefSymbol
* +- TypeBindSymbol
* +- DefDefSymbol
* +- ValDefSymbol
* +- BindSymbol
* |
* +- TypeSymbol -+- ClassDefSymbol
* | +- TypeDefSymbol
* | +- TypeBindSymbol
* |
* +- TermSymbol -+- DefDefSymbol
* | +- ValDefSymbol
* | +- BindSymbol
* |
* +- NoSymbol
*
* +- Flags
Expand Down Expand Up @@ -249,7 +252,7 @@ trait Kernel {
def DefDef_apply(symbol: DefDefSymbol, rhsFn: List[Type] => List[List[Term]] => Option[Term])(implicit ctx: Context): DefDef
def DefDef_copy(original: DefDef)(name: String, typeParams: List[TypeDef], paramss: List[List[ValDef]], tpt: TypeTree, rhs: Option[Term])(implicit ctx: Context): DefDef

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

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

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

type TypeSymbol <: Symbol

def matchTypeSymbol(symbol: Symbol)(implicit ctx: Context): Option[TypeSymbol]

/** Symbol of a class definition. This includes anonymous class definitions and the class of a module object. */
type ClassDefSymbol <: Symbol
type ClassDefSymbol <: TypeSymbol

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

/** ClassDef tree of this defintion */
/** ClassDef tree of this definition */
def ClassDefSymbol_tree(self: ClassDefSymbol)(implicit ctx: Context): ClassDef

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

/** Symbol of a type (parameter or member) definition. */
type TypeDefSymbol <: Symbol
type TypeDefSymbol <: TypeSymbol

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

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

/** Symbol representing a bind definition. */
type TypeBindSymbol <: Symbol
type TypeBindSymbol <: TypeSymbol

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

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

type TermSymbol <: Symbol

def matchTermSymbol(symbol: Symbol)(implicit ctx: Context): Option[TermSymbol]

/** Symbol representing a method definition. */
type DefDefSymbol <: Symbol
type DefDefSymbol <: TermSymbol

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

/** DefDef tree of this defintion */
/** DefDef tree of this definition */
def DefDefSymbol_tree(self: DefDefSymbol)(implicit ctx: Context): DefDef

/** Signature of this defintion */
/** Signature of this definition */
def DefDefSymbol_signature(self: DefDefSymbol)(implicit ctx: Context): Signature

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

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

/** ValDef tree of this defintion */
/** ValDef tree of this definition */
def ValDefSymbol_tree(self: ValDefSymbol)(implicit ctx: Context): ValDef

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

/** Symbol representing a bind definition. */
type BindSymbol <: Symbol
type BindSymbol <: TermSymbol

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

Expand Down
14 changes: 14 additions & 0 deletions library/src/scala/tasty/reflect/SymbolOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ trait SymbolOps extends Core {
kernel.PackageDefSymbol_tree(self)
}

// TypeSymbol

object IsTypeSymbol {
def unapply(symbol: Symbol)(implicit ctx: Context): Option[TypeSymbol] =
kernel.matchTypeSymbol(symbol)
}

// ClassSymbol

object IsClassDefSymbol {
Expand Down Expand Up @@ -184,6 +191,13 @@ trait SymbolOps extends Core {
kernel.TypeBindSymbol_tree(self)
}

// TermSymbol

object IsTermSymbol {
def unapply(symbol: Symbol)(implicit ctx: Context): Option[TermSymbol] =
kernel.matchTermSymbol(symbol)
}

// DefSymbol

object IsDefDefSymbol {
Expand Down

0 comments on commit 126edfb

Please sign in to comment.