Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix module class logic (now works as in compiler)
  • Loading branch information
nicolasstucki committed Nov 12, 2018
commit 906ad2a1bb5fb03eb47d1cea8ce214849b38bb25
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/SymbolOpsImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {
if (sym.exists) Some(sym.asClass) else None
}

def companionModule(implicit ctx: Context): Option[ValSymbol] = {
val sym = symbol.companionModule
if (sym.exists) Some(sym.asTerm) else None
}

private def isField(sym: Symbol)(implicit ctx: Context): Boolean = sym.isTerm && !sym.is(Flags.Method)
}

Expand All @@ -173,6 +178,11 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {

def ValSymbolDeco(symbol: ValSymbol): ValSymbolAPI = new ValSymbolAPI {
def tree(implicit ctx: Context): ValDef = FromSymbol.valDefFromSym(symbol)

def companionClass(implicit ctx: Context): Option[ClassSymbol] = {
val sym = symbol.companionClass
if (sym.exists) Some(sym.asClass) else None
}
}

object IsBindSymbol extends IsBindSymbolExtractor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ trait TypeOrBoundsTreesOpsImpl extends scala.tasty.reflect.TypeOrBoundsTreeOps w
def TypeTreeDeco(tpt: TypeTree): TypeTreeAPI = new TypeTreeAPI {
def pos(implicit ctx: Context): Position = tpt.pos
def tpe(implicit ctx: Context): Type = tpt.tpe.stripTypeVar
def symbol(implicit ctx: Context): Symbol = {
val sym = tpt.symbol
if (sym.isType) sym else sym.companionClass
}
def symbol(implicit ctx: Context): Symbol = tpt.symbol
}

object IsTypeTree extends IsTypeTreeExtractor {
Expand Down
9 changes: 9 additions & 0 deletions library/src/scala/tasty/reflect/SymbolOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ trait SymbolOps extends Core {
/** Fields of a case class type -- only the ones declared in primary constructor */
def caseFields(implicit ctx: Context): List[ValSymbol]

/** The class symbol of the companion module class */
def companionClass(implicit ctx: Context): Option[ClassSymbol]

/** The symbol of the companion module */
def companionModule(implicit ctx: Context): Option[ValSymbol]

}
implicit def ClassSymbolDeco(symbol: ClassSymbol): ClassSymbolAPI

Expand Down Expand Up @@ -122,6 +127,10 @@ trait SymbolOps extends Core {

trait ValSymbolAPI {
def tree(implicit ctx: Context): ValDef

/** The class symbol of the companion module class */
def companionClass(implicit ctx: Context): Option[ClassSymbol]

}
implicit def ValSymbolDeco(symbol: ValSymbol): ValSymbolAPI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,19 @@ object TypeToolbox {
inline def companion[T1, T2]: Boolean = ~companionImpl('[T1], '[T2])
private def companionImpl(t1: Type[_], t2: Type[_])(implicit reflect: Reflection): Expr[Boolean] = {
import reflect._
val res = t1.reflect.symbol.asClass.companionClass.contains(t2.reflect.symbol)
val res = t1.reflect.symbol.asClass.companionModule.contains(t2.reflect.symbol)
res.toExpr
}

inline def companionName[T1]: String = ~companionNameImpl('[T1])
private def companionNameImpl(tp: Type[_])(implicit reflect: Reflection): Expr[String] = {
import reflect._
tp.reflect.symbol match {
case IsClassSymbol(sym) => sym.companionClass.map(_.fullName).getOrElse("").toExpr
case _ => '("")
val companionClassOpt = tp.reflect.symbol match {
case IsClassSymbol(sym) => sym.companionClass
case IsValSymbol(sym) => sym.companionClass
case _ => None
}
companionClassOpt.map(_.fullName).getOrElse("").toExpr
}

// TODO add to the std lib
Expand Down