Skip to content

Commit

Permalink
Refactor styles
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishiking committed Aug 10, 2021
1 parent a82605f commit 056f4a4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 100 deletions.
2 changes: 0 additions & 2 deletions compiler/src/dotty/tools/dotc/semanticdb/ConstantOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ object ConstantOps:
case DoubleTag => s.DoubleConstant(const.doubleValue)
case StringTag => s.StringConstant(const.stringValue)
case NullTag => s.NullConstant()
// ConstantType(_: Type, ClazzTag) should be converted as it's type
// NoTag => it shouldn't happen
case _ => throw new Error(s"Constant ${const} can't be converted to Semanticdb Constant.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ExtractSemanticDB extends Phase:

/** Extractor of symbol occurrences from trees */
class Extractor extends TreeTraverser:
given builder: s.SemanticSymbolBuilder = s.SemanticSymbolBuilder()
given s.SemanticSymbolBuilder = s.SemanticSymbolBuilder()
val converter = s.TypeOps()

/** The bodies of synthetic locals */
Expand Down Expand Up @@ -328,7 +328,7 @@ class ExtractSemanticDB extends Phase:
else
val content = source.content()
val (start, end) =
if content.lift(span.end - 1).map(_ == '`').getOrElse(false) then
if content.lift(span.end - 1).exists(_ == '`') then
(span.start + 1, span.end - 1)
else (span.start, span.end)
val nameInSource = content.slice(start, end).mkString
Expand Down
157 changes: 79 additions & 78 deletions compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,67 +41,82 @@ object Scala3:
case class TypeParamRefSymbol(owner: Symbol, name: Name, tp: TypeBounds) extends FakeSymbol
case class RefinementSymbol(owner: Symbol, name: Name, tp: Type) extends FakeSymbol
type SemanticSymbol = Symbol | FakeSymbol
extension (sym: SemanticSymbol)
def name(using Context): Name = sym match
case s: Symbol => s.name
case s: WildcardTypeSymbol => nme.WILDCARD
case s: TermParamRefSymbol => s.name
case s: TypeParamRefSymbol => s.name
case s: RefinementSymbol => s.name

def symbolName(using builder: SemanticSymbolBuilder)(using Context): String =
sym match
case s: Symbol => SymbolOps.symbolName(s)
case s: FakeSymbol =>
s.sname.getOrElse {
val sname = builder.symbolName(s)
s.sname = Some(sname)
sname
}

def symbolInfo(symkinds: Set[SymbolKind])(using LinkMode, TypeOps, SemanticSymbolBuilder, Context): SymbolInformation =
sym match
case s: Symbol => SymbolOps.symbolInfo(s)(symkinds)
case s: WildcardTypeSymbol =>
SymbolInformation(
symbol = symbolName,
language = Language.SCALA,
kind = SymbolInformation.Kind.TYPE,
displayName = nme.WILDCARD.show,
signature = s.bounds.toSemanticSig(s.owner),
)
case s: TermParamRefSymbol =>
SymbolInformation(
symbol = symbolName,
language = Language.SCALA,
kind = SymbolInformation.Kind.PARAMETER,
displayName = s.name.show.unescapeUnicode,
signature = s.tp.toSemanticSig(s.owner),
)
case s: TypeParamRefSymbol =>
SymbolInformation(
symbol = symbolName,
language = Language.SCALA,
kind = SymbolInformation.Kind.TYPE_PARAMETER,
displayName = s.name.show.unescapeUnicode,
signature = s.tp.toSemanticSig(s.owner),
)
case s: RefinementSymbol =>
val signature = s.tp.toSemanticSig(s.owner)
val kind = signature match
case _: TypeSignature => SymbolInformation.Kind.TYPE
case _: MethodSignature => SymbolInformation.Kind.METHOD
case _: ValueSignature => SymbolInformation.Kind.FIELD
case _ => SymbolInformation.Kind.UNKNOWN_KIND
SymbolInformation(
symbol = symbolName,
language = Language.SCALA,
kind = kind,
displayName = s.name.show.unescapeUnicode,
properties =
SymbolInformation.Property.ABSTRACT.value,
signature = signature,
)

given SemanticSymbolOps : AnyRef with
extension (sym: SemanticSymbol)
def name(using Context): Name = sym match
case s: Symbol => s.name
case s: WildcardTypeSymbol => nme.WILDCARD
case s: TermParamRefSymbol => s.name
case s: TypeParamRefSymbol => s.name
case s: RefinementSymbol => s.name

def symbolName(using builder: SemanticSymbolBuilder)(using Context): String =
sym match
case s: Symbol => builder.symbolName(s)
case s: FakeSymbol =>
s.sname.getOrElse {
val sname = builder.symbolName(s)
s.sname = Some(sname)
sname
}

def symbolInfo(symkinds: Set[SymbolKind])(using LinkMode, TypeOps, SemanticSymbolBuilder, Context): SymbolInformation =
sym match
case s: Symbol =>
val kind = s.symbolKind(symkinds)
val sname = sym.symbolName
val signature = s.info.toSemanticSig(s)
SymbolInformation(
symbol = sname,
language = Language.SCALA,
kind = kind,
properties = s.symbolProps(symkinds),
displayName = Symbols.displaySymbol(s),
signature = signature,
access = s.symbolAccess(kind),
)
case s: WildcardTypeSymbol =>
SymbolInformation(
symbol = symbolName,
language = Language.SCALA,
kind = SymbolInformation.Kind.TYPE,
displayName = nme.WILDCARD.show,
signature = s.bounds.toSemanticSig(s.owner),
)
case s: TermParamRefSymbol =>
SymbolInformation(
symbol = symbolName,
language = Language.SCALA,
kind = SymbolInformation.Kind.PARAMETER,
displayName = s.name.show.unescapeUnicode,
signature = s.tp.toSemanticSig(s.owner),
)
case s: TypeParamRefSymbol =>
SymbolInformation(
symbol = symbolName,
language = Language.SCALA,
kind = SymbolInformation.Kind.TYPE_PARAMETER,
displayName = s.name.show.unescapeUnicode,
signature = s.tp.toSemanticSig(s.owner),
)
case s: RefinementSymbol =>
val signature = s.tp.toSemanticSig(s.owner)
val kind = signature match
case _: TypeSignature => SymbolInformation.Kind.TYPE
case _: MethodSignature => SymbolInformation.Kind.METHOD
case _: ValueSignature => SymbolInformation.Kind.FIELD
case _ => SymbolInformation.Kind.UNKNOWN_KIND
SymbolInformation(
symbol = symbolName,
language = Language.SCALA,
kind = kind,
displayName = s.name.show.unescapeUnicode,
properties =
SymbolInformation.Property.ABSTRACT.value,
signature = signature,
)
end SemanticSymbolOps

enum SymbolKind derives CanEqual:
kind =>
Expand Down Expand Up @@ -205,28 +220,14 @@ object Scala3:
def isSyntheticWithIdent(using Context): Boolean =
sym.is(Synthetic) && !sym.isAnonymous && !sym.name.isEmptyNumbered

def symbolInfo(symkinds: Set[SymbolKind])(using LinkMode, TypeOps, SemanticSymbolBuilder, Context): SymbolInformation =
val sname = sym.symbolName
val signature = sym.info.toSemanticSig(sym)
val kind = symbolKind(symkinds)
SymbolInformation(
symbol = sname,
language = Language.SCALA,
kind = kind,
properties = sym.symbolProps(symkinds),
displayName = Symbols.displaySymbol(sym),
signature = signature,
access = symbolAccess(kind),
)

/** The semanticdb name of the given symbol */
def symbolName(using builder: SemanticSymbolBuilder)(using Context): String =
builder.symbolName(sym)

def funParamSymbol(using builder: SemanticSymbolBuilder)(using Context): Name => String =
builder.funParamSymbol(sym)

private def symbolKind(symkinds: Set[SymbolKind])(using Context): SymbolInformation.Kind =
def symbolKind(symkinds: Set[SymbolKind])(using Context): SymbolInformation.Kind =
if sym.isTypeParam then
SymbolInformation.Kind.TYPE_PARAMETER
else if sym.is(TermParam) then
Expand Down Expand Up @@ -260,7 +261,7 @@ object Scala3:
else
SymbolInformation.Kind.UNKNOWN_KIND

private def symbolProps(symkinds: Set[SymbolKind])(using Context): Int =
def symbolProps(symkinds: Set[SymbolKind])(using Context): Int =
if sym.is(ModuleClass) then
return sym.sourceModule.symbolProps(symkinds)
var props = 0
Expand Down Expand Up @@ -308,7 +309,7 @@ object Scala3:
props |= SymbolInformation.Property.OPAQUE.value
props

private def symbolAccess(kind: SymbolInformation.Kind)(using Context, SemanticSymbolBuilder): Access =
def symbolAccess(kind: SymbolInformation.Kind)(using Context, SemanticSymbolBuilder): Access =
kind match
case k.LOCAL | k.PARAMETER | k.SELF_PARAMETER | k.TYPE_PARAMETER | k.PACKAGE | k.PACKAGE_OBJECT =>
Access.Empty
Expand Down
25 changes: 7 additions & 18 deletions compiler/src/dotty/tools/dotc/semanticdb/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import core.Flags
import core.Names.Name
import core.StdNames.tpnme
import ast.tpd._
import scala.util.chaining.scalaUtilChainingOps

import collection.mutable

Expand Down Expand Up @@ -153,18 +154,14 @@ class TypeOps:
case mt: MethodType =>
val syms: List[SemanticSymbol] = mt.paramNames.zip(mt.paramInfos).map { (name, info) =>
paramRefSymtab.lookup(mt, name).getOrElse {
val fakeSym = TermParamRefSymbol(sym, name, info)
registerFakeSymbol(fakeSym)
fakeSym
TermParamRefSymbol(sym, name, info).tap(registerFakeSymbol)
}
}
flatten(mt.resType, paramss :+ syms, tparams)
case pt: PolyType =>
val syms: List[SemanticSymbol] = pt.paramNames.zip(pt.paramInfos).map { (name, info) =>
paramRefSymtab.lookup(pt, name).getOrElse {
val fakeSym = TypeParamRefSymbol(sym, name, info)
registerFakeSymbol(fakeSym)
fakeSym
TypeParamRefSymbol(sym, name, info).tap(registerFakeSymbol)
}
}
flatten(pt.resType, paramss, tparams ++ syms)
Expand Down Expand Up @@ -195,14 +192,10 @@ class TypeOps:
val paramSyms: List[SemanticSymbol] = lambda.paramNames.zip(lambda.paramInfos).map { (paramName, bounds) =>
// def x[T[_]] = ???
if paramName.isWildcard then
val fakeSym = WildcardTypeSymbol(sym, bounds)
registerFakeSymbol(fakeSym)
fakeSym
WildcardTypeSymbol(sym, bounds).tap(registerFakeSymbol)
else
paramRefSymtab.lookup(lambda, paramName).getOrElse {
val fakeSym = TypeParamRefSymbol(sym, paramName, bounds)
registerFakeSymbol(fakeSym)
fakeSym
TypeParamRefSymbol(sym, paramName, bounds).tap(registerFakeSymbol)
}
}
(lambda.resType, paramSyms)
Expand Down Expand Up @@ -261,9 +254,7 @@ class TypeOps:
tref.binder.typeParams.find(param => param.paramName == tref.paramName) match
case Some(param) =>
val info = param.paramInfo
val fakeSym = TypeParamRefSymbol(sym, tref.paramName, info)
registerFakeSymbol(fakeSym)
Some(fakeSym)
Some(TypeParamRefSymbol(sym, tref.paramName, info).tap(registerFakeSymbol))
case None =>
symbolNotFound(tref.binder, tref.paramName, sym)
None
Expand Down Expand Up @@ -319,9 +310,7 @@ class TypeOps:

val decls: List[SemanticSymbol] = refinedInfos.map { (name, info) =>
refinementSymtab.lookup(rt, name).getOrElse {
val fakeSym = RefinementSymbol(sym, name, info)
registerFakeSymbol(fakeSym)
fakeSym
RefinementSymbol(sym, name, info).tap(registerFakeSymbol)
}
}
val sdecls = decls.sscopeOpt(using LinkMode.HardlinkChildren)
Expand Down

0 comments on commit 056f4a4

Please sign in to comment.