Skip to content

Commit 90177c6

Browse files
committed
Integrate SymbolInformationOps into Scala3#SymbolOps
1 parent 9c6015e commit 90177c6

File tree

4 files changed

+115
-154
lines changed

4 files changed

+115
-154
lines changed

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import scala.collection.mutable
2222
import scala.annotation.{ threadUnsafe => tu, tailrec }
2323
import scala.PartialFunction.condOpt
2424

25-
import SymbolInformationOps._
2625
import dotty.tools.dotc.semanticdb.SemanticSymbolBuilder
2726

2827
/** Extract symbol references and uses to semanticdb files.
@@ -136,12 +135,12 @@ class ExtractSemanticDB extends Phase:
136135
if !tree.symbol.isAllOf(ModuleValCreationFlags) then
137136
if !excludeDef(tree.symbol)
138137
&& tree.span.hasLength then
139-
registerDefinition(tree.symbol, tree.nameSpan, tree.symbolKinds, tree.source)
138+
registerDefinition(tree.symbol, tree.nameSpan, symbolKinds(tree), tree.source)
140139
val privateWithin = tree.symbol.privateWithin
141140
if privateWithin.exists then
142141
registerUseGuarded(None, privateWithin, spanOfSymbol(privateWithin, tree.span, tree.source), tree.source)
143142
else if !excludeSymbol(tree.symbol) then
144-
registerSymbol(tree.symbol, tree.symbolKinds)
143+
registerSymbol(tree.symbol, symbolKinds(tree))
145144
tree match
146145
case tree: ValDef
147146
if tree.symbol.isAllOf(EnumValue) =>
@@ -332,6 +331,15 @@ class ExtractSemanticDB extends Phase:
332331
val start = if idx >= 0 then idx else span.start
333332
Span(start, start + sym.name.show.length, start)
334333

334+
extension (list: List[List[ValDef]])
335+
private inline def isSingleArg = list match
336+
case (_::Nil)::Nil => true
337+
case _ => false
338+
339+
extension (tree: DefDef)
340+
private def isSetterDef(using Context): Boolean =
341+
tree.name.isSetterName && tree.mods.is(Accessor) && tree.termParamss.isSingleArg
342+
335343
private def findGetters(ctorParams: Set[Names.TermName], body: List[Tree])(using Context): Map[Names.TermName, ValDef] =
336344
if ctorParams.isEmpty || body.isEmpty then
337345
Map.empty
@@ -378,6 +386,28 @@ class ExtractSemanticDB extends Phase:
378386
private inline def matchingMemberType(ctorTypeParam: Symbol, classSym: Symbol)(using Context) =
379387
classSym.info.member(ctorTypeParam.name).symbol
380388

389+
/**Necessary because not all of the eventual flags are propagated from the Tree to the symbol yet.
390+
*/
391+
private def symbolKinds(tree: NamedDefTree)(using Context): Set[SymbolKind] =
392+
if tree.symbol.isSelfSym then
393+
Set.empty
394+
else
395+
val symkinds = mutable.HashSet.empty[SymbolKind]
396+
tree match
397+
case tree: ValDef =>
398+
if !tree.symbol.is(Param) then
399+
symkinds += (if tree.mods is Mutable then SymbolKind.Var else SymbolKind.Val)
400+
if tree.rhs.isEmpty && !tree.symbol.isOneOf(TermParam | CaseAccessor | ParamAccessor) then
401+
symkinds += SymbolKind.Abstract
402+
case tree: DefDef =>
403+
if tree.isSetterDef then
404+
symkinds += SymbolKind.Setter
405+
else if tree.rhs.isEmpty then
406+
symkinds += SymbolKind.Abstract
407+
case tree: Bind =>
408+
symkinds += SymbolKind.Val
409+
case _ =>
410+
symkinds.toSet
381411

382412
private def ctorParams(
383413
vparamss: List[List[ValDef]], body: List[Tree])(using Context): Unit =

compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,86 @@ object Scala3:
126126
def isSyntheticWithIdent(using Context): Boolean =
127127
sym.is(Synthetic) && !sym.isAnonymous && !sym.name.isEmptyNumbered
128128

129+
def symbolInfo(symkinds: Set[SymbolKind])(using LinkMode, Context, SemanticSymbolBuilder): SymbolInformation =
130+
import TypeOps._
131+
val sname = sym.symbolName
132+
val signature = sym.info.toSemanticSig(sym)
133+
SymbolInformation(
134+
symbol = sname,
135+
language = Language.SCALA,
136+
kind = symbolKind(symkinds),
137+
properties = sym.symbolProps(symkinds),
138+
displayName = Symbols.displaySymbol(sym),
139+
signature = signature,
140+
)
141+
142+
private def symbolKind(symkinds: Set[SymbolKind])(using Context): SymbolInformation.Kind =
143+
if sym.isTypeParam then
144+
SymbolInformation.Kind.TYPE_PARAMETER
145+
else if sym.is(TermParam) then
146+
SymbolInformation.Kind.PARAMETER
147+
else if sym.isTerm && sym.owner.isTerm then
148+
SymbolInformation.Kind.LOCAL
149+
else if sym.isInlineMethod || sym.is(Macro) then
150+
SymbolInformation.Kind.MACRO
151+
else if sym.isConstructor then
152+
SymbolInformation.Kind.CONSTRUCTOR
153+
else if sym.isSelfSym then
154+
SymbolInformation.Kind.SELF_PARAMETER
155+
else if sym.isOneOf(Method) || symkinds.exists(_.isVarOrVal) then
156+
SymbolInformation.Kind.METHOD
157+
else if sym.isPackageObject then
158+
SymbolInformation.Kind.PACKAGE_OBJECT
159+
else if sym.is(Module) then
160+
SymbolInformation.Kind.OBJECT
161+
else if sym.is(Package) then
162+
SymbolInformation.Kind.PACKAGE
163+
else if sym.isAllOf(JavaInterface) then
164+
SymbolInformation.Kind.INTERFACE
165+
else if sym.is(Trait) then
166+
SymbolInformation.Kind.TRAIT
167+
else if sym.isClass then
168+
SymbolInformation.Kind.CLASS
169+
else if sym.isType then
170+
SymbolInformation.Kind.TYPE
171+
else if sym.is(ParamAccessor) then
172+
SymbolInformation.Kind.FIELD
173+
else
174+
SymbolInformation.Kind.UNKNOWN_KIND
175+
176+
private def symbolProps(symkinds: Set[SymbolKind])(using Context): Int =
177+
if sym.is(ModuleClass) then
178+
return sym.sourceModule.symbolProps(symkinds)
179+
var props = 0
180+
if sym.isPrimaryConstructor then
181+
props |= SymbolInformation.Property.PRIMARY.value
182+
if sym.is(Abstract) || symkinds.contains(SymbolKind.Abstract) then
183+
props |= SymbolInformation.Property.ABSTRACT.value
184+
if sym.is(Final) then
185+
props |= SymbolInformation.Property.FINAL.value
186+
if sym.is(Sealed) then
187+
props |= SymbolInformation.Property.SEALED.value
188+
if sym.isOneOf(GivenOrImplicit) then
189+
props |= SymbolInformation.Property.IMPLICIT.value
190+
if sym.is(Lazy, butNot=Module) then
191+
props |= SymbolInformation.Property.LAZY.value
192+
if sym.isAllOf(Case | Module) || sym.is(CaseClass) || sym.isAllOf(EnumCase) then
193+
props |= SymbolInformation.Property.CASE.value
194+
if sym.is(Covariant) then
195+
props |= SymbolInformation.Property.COVARIANT.value
196+
if sym.is(Contravariant) then
197+
props |= SymbolInformation.Property.CONTRAVARIANT.value
198+
if sym.isAllOf(DefaultMethod | JavaDefined) || sym.is(Accessor) && sym.name.is(NameKinds.DefaultGetterName) then
199+
props |= SymbolInformation.Property.DEFAULT.value
200+
if symkinds.exists(_.isVal) then
201+
props |= SymbolInformation.Property.VAL.value
202+
if symkinds.exists(_.isVar) then
203+
props |= SymbolInformation.Property.VAR.value
204+
if sym.is(JavaStatic) then
205+
props |= SymbolInformation.Property.STATIC.value
206+
if sym.is(Enum) then
207+
props |= SymbolInformation.Property.ENUM.value
208+
props
129209
end SymbolOps
130210

131211
object LocalSymbol:

compiler/src/dotty/tools/dotc/semanticdb/SymbolInformationOps.scala

Lines changed: 0 additions & 136 deletions
This file was deleted.

compiler/src/dotty/tools/dotc/semanticdb/SymbolOps.scala renamed to compiler/src/dotty/tools/dotc/semanticdb/TypeOps.scala

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@ import ast.tpd._
1111

1212
import dotty.tools.dotc.{semanticdb => s}
1313

14-
object SymbolOps:
15-
extension (sym: Symbol)
16-
def sig(using LinkMode, Context, SemanticSymbolBuilder): s.Signature =
17-
import TypeOps._
18-
val sig = sym.info.toSemanticSig(sym)
19-
// println("")
20-
// println(sym.toString)
21-
// println(s"=========sym.info================")
22-
// pprint.pprintln(sym.info)
23-
// println(s"=========sig================")
24-
// pprint.pprintln(sig)
25-
sig
26-
2714
object TypeOps:
2815
import SymbolScopeOps._
2916
extension (tpe: Type)
@@ -244,12 +231,12 @@ object TypeOps:
244231
}
245232

246233
object SymbolScopeOps:
247-
import SymbolInformationOps._
234+
import Scala3.given
248235
extension (syms: List[Symbol])
249236
def sscope(using linkMode: LinkMode)(using SemanticSymbolBuilder, Context): s.Scope =
250237
linkMode match {
251238
case LinkMode.SymlinkChildren =>
252239
s.Scope(symlinks = syms.map(_.symbolName))
253240
case LinkMode.HardlinkChildren =>
254-
s.Scope(hardlinks = syms.map(_.toSymbolInformation))
241+
s.Scope(hardlinks = syms.map(_.symbolInfo(Set.empty)))
255242
}

0 commit comments

Comments
 (0)