Skip to content

Commit d927985

Browse files
committed
emit SymbolInfo for ctor params
1 parent ba9efc3 commit d927985

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import collection.mutable
1818
import java.lang.Character.{isJavaIdentifierPart, isJavaIdentifierStart}
1919
import java.nio.file.Paths
2020

21+
import scala.annotation.{ threadUnsafe => tu }
22+
2123
/** Extract symbol references and uses to semanticdb files.
2224
* See https://scalameta.org/docs/semanticdb/specification.html#symbol-1
2325
* for a description of the format.
@@ -98,6 +100,15 @@ class ExtractSemanticDB extends Phase {
98100
kind.isVar
99101
|| kind.isVal
100102

103+
private object SymbolKind with
104+
val SingletonVal = Set(Val)
105+
val SingletonVar = Set(Var)
106+
107+
private def isWildcard(name: Name)(given ctx: Context) = name match
108+
case nme.WILDCARD | WILDCARDTypeName => true
109+
case _ if name.is(NameKinds.WildcardParamName) => true
110+
case _ => false
111+
101112
/** Add semanticdb name of the given symbol to string builder */
102113
private def addSymName(b: StringBuilder, sym: Symbol)(given ctx: Context): Unit =
103114

@@ -199,11 +210,6 @@ class ExtractSemanticDB extends Phase {
199210

200211
private val WILDCARDTypeName = nme.WILDCARD.toTypeName
201212

202-
private def isWildcard(name: Name)(given ctx: Context) = name match
203-
case nme.WILDCARD | WILDCARDTypeName => true
204-
case _ if name.is(NameKinds.WildcardParamName) => true
205-
case _ => false
206-
207213
/** Definitions of this symbol should be excluded from semanticdb */
208214
private def excludeDef(sym: Symbol)(given Context): Boolean =
209215
!sym.exists
@@ -328,6 +334,19 @@ class ExtractSemanticDB extends Phase {
328334
def (tree: DefDef) isSetterDef(given Context): Boolean =
329335
tree.name.isSetterName && tree.mods.is(Accessor) && tree.vparamss.flatten.length == 1
330336

337+
def findGetters(ctorParams: List[ValDef], body: List[ast.tpd.Tree])(given Context): Map[Names.TermName, ValDef] =
338+
if ctorParams.isEmpty then
339+
Map.empty
340+
else
341+
val ctorParamNames = ctorParams.map(_.name).toSet
342+
body.collect({
343+
case tree: ValDef
344+
if ctorParamNames.contains(tree.name)
345+
&& !tree.symbol.isPrivate =>
346+
tree.name -> tree
347+
}).toMap
348+
end findGetters
349+
331350
override def traverse(tree: Tree)(given Context): Unit =
332351
for annot <- tree.symbol.annotations do
333352
if annot.tree.span.exists
@@ -370,7 +389,16 @@ class ExtractSemanticDB extends Phase {
370389
case tree: Template =>
371390
if !excludeDef(tree.constr.symbol)
372391
registerDefinition(tree.constr.symbol, tree.constr.span, Set.empty)
373-
tree.constr.vparamss.flatten.foreach(vparam => traverse(vparam.tpt)) // the accessor symbol is traversed in the body
392+
val ctorParams = tree.constr.vparamss.flatten
393+
@tu lazy val getters = findGetters(ctorParams, tree.body)
394+
for vparam <- ctorParams do
395+
if !isWildcard(vparam.name)
396+
val symkinds =
397+
getters.get(vparam.name).fold(
398+
Set.empty[SymbolKind])(
399+
getter => if getter.mods.is(Mutable) then SymbolKind.SingletonVar else SymbolKind.SingletonVal)
400+
registerSymbol(vparam.symbol, symbolName(vparam.symbol), symkinds)
401+
traverse(vparam.tpt)
374402
for parent <- tree.parentsOrDerived do
375403
if
376404
parent.symbol != defn.ObjectClass.primaryConstructor

tests/semanticdb/metac.expect

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Schema => SemanticDB v4
194194
Uri => Annotations.scala
195195
Text => empty
196196
Language => Scala
197-
Symbols => 20 entries
197+
Symbols => 22 entries
198198
Occurrences => 51 entries
199199

200200
Symbols:
@@ -204,11 +204,13 @@ annot/Annotations# => class Annotations
204204
annot/Annotations#S# => type S
205205
annot/Annotations#[T] => typeparam T
206206
annot/Annotations#`<init>`(). => primary ctor <init>
207+
annot/Annotations#`<init>`().(x) => param x
207208
annot/Annotations#field. => val method field
208209
annot/Annotations#method(). => method method
209210
annot/Annotations#x. => val method x
210211
annot/B# => class B
211212
annot/B#`<init>`(). => primary ctor <init>
213+
annot/B#`<init>`().(x) => param x
212214
annot/B#`<init>`(+1). => ctor <init>
213215
annot/B#x. => val method x
214216
annot/M. => final object M
@@ -342,39 +344,48 @@ Schema => SemanticDB v4
342344
Uri => Classes.scala
343345
Text => empty
344346
Language => Scala
345-
Symbols => 35 entries
347+
Symbols => 44 entries
346348
Occurrences => 58 entries
347349

348350
Symbols:
349351
classes/C1# => final class C1
350352
classes/C1#`<init>`(). => primary ctor <init>
353+
classes/C1#`<init>`().(x1) => val param x1
351354
classes/C1#x1. => val method x1
352355
classes/C2# => final class C2
353356
classes/C2#`<init>`(). => primary ctor <init>
357+
classes/C2#`<init>`().(x2) => val param x2
354358
classes/C2#x2. => val method x2
355359
classes/C2. => final object C2
356360
classes/C3# => case class C3
357361
classes/C3#`<init>`(). => primary ctor <init>
362+
classes/C3#`<init>`().(x) => val param x
358363
classes/C3#x. => val method x
359364
classes/C4# => case class C4
360365
classes/C4#`<init>`(). => primary ctor <init>
366+
classes/C4#`<init>`().(x) => val param x
361367
classes/C4#x. => val method x
362368
classes/C4. => final object C4
363369
classes/C6# => case class C6
364370
classes/C6#`<init>`(). => primary ctor <init>
371+
classes/C6#`<init>`().(x) => param x
365372
classes/C6#x. => val method x
366373
classes/C7# => class C7
367374
classes/C7#`<init>`(). => primary ctor <init>
375+
classes/C7#`<init>`().(x) => param x
368376
classes/C7#x. => val method x
369377
classes/C8# => class C8
370378
classes/C8#`<init>`(). => primary ctor <init>
379+
classes/C8#`<init>`().(x) => param x
371380
classes/C8#x. => val method x
372381
classes/C9# => class C9
373382
classes/C9#`<init>`(). => primary ctor <init>
383+
classes/C9#`<init>`().(x) => param x
374384
classes/C9#x(). => var method x
375385
classes/M. => final object M
376386
classes/M.C5# => class C5
377387
classes/M.C5#`<init>`(). => primary ctor <init>
388+
classes/M.C5#`<init>`().(x) => param x
378389
classes/M.C5#x. => val method x
379390
classes/N. => final object N
380391
classes/N.anonClass. => val method anonClass
@@ -534,7 +545,7 @@ Schema => SemanticDB v4
534545
Uri => Enums.scala
535546
Text => empty
536547
Language => Scala
537-
Symbols => 66 entries
548+
Symbols => 69 entries
538549
Occurrences => 156 entries
539550

540551
Symbols:
@@ -549,11 +560,14 @@ _empty_/Enums.Maybe#[A] => covariant typeparam A
549560
_empty_/Enums.Maybe#`<init>`(). => primary ctor <init>
550561
_empty_/Enums.Maybe.Just# => final case enum class Just
551562
_empty_/Enums.Maybe.Just#`<init>`(). => primary ctor <init>
563+
_empty_/Enums.Maybe.Just#`<init>`().(value) => val param value
552564
_empty_/Enums.Maybe.Just#value. => val method value
553565
_empty_/Enums.Maybe.None. => case val static enum method None
554566
_empty_/Enums.Planet# => abstract sealed enum class Planet
555567
_empty_/Enums.Planet#G. => final val method G
556568
_empty_/Enums.Planet#`<init>`(). => primary ctor <init>
569+
_empty_/Enums.Planet#`<init>`().(mass) => param mass
570+
_empty_/Enums.Planet#`<init>`().(radius) => param radius
557571
_empty_/Enums.Planet#mass. => val method mass
558572
_empty_/Enums.Planet#radius. => val method radius
559573
_empty_/Enums.Planet#surfaceGravity(). => method surfaceGravity
@@ -845,13 +859,16 @@ Schema => SemanticDB v4
845859
Uri => Flags.scala
846860
Text => empty
847861
Language => Scala
848-
Symbols => 44 entries
862+
Symbols => 50 entries
849863
Occurrences => 84 entries
850864

851865
Symbols:
852866
flags/p/package. => final package object p
853867
flags/p/package.AA# => class AA
854868
flags/p/package.AA#`<init>`(). => primary ctor <init>
869+
flags/p/package.AA#`<init>`().(x) => param x
870+
flags/p/package.AA#`<init>`().(y) => val param y
871+
flags/p/package.AA#`<init>`().(z) => var param z
855872
flags/p/package.AA#`z_=`(). => var method z_=
856873
flags/p/package.AA#x. => val method x
857874
flags/p/package.AA#y. => val method y
@@ -861,6 +878,9 @@ flags/p/package.C#[T] => covariant typeparam T
861878
flags/p/package.C#[U] => contravariant typeparam U
862879
flags/p/package.C#[V] => typeparam V
863880
flags/p/package.C#`<init>`(). => primary ctor <init>
881+
flags/p/package.C#`<init>`().(x) => param x
882+
flags/p/package.C#`<init>`().(y) => param y
883+
flags/p/package.C#`<init>`().(z) => param z
864884
flags/p/package.C#`<init>`(+1). => ctor <init>
865885
flags/p/package.C#`<init>`(+1).[T] => typeparam T
866886
flags/p/package.C#`<init>`(+1).[U] => typeparam U
@@ -1157,7 +1177,7 @@ Schema => SemanticDB v4
11571177
Uri => ImplicitConversion.scala
11581178
Text => empty
11591179
Language => Scala
1160-
Symbols => 18 entries
1180+
Symbols => 19 entries
11611181
Occurrences => 58 entries
11621182

11631183
Symbols:
@@ -1178,6 +1198,7 @@ example/ImplicitConversion.newAny2stringadd#[A] => typeparam A
11781198
example/ImplicitConversion.newAny2stringadd#`+`(). => method +
11791199
example/ImplicitConversion.newAny2stringadd#`+`().(other) => val param other
11801200
example/ImplicitConversion.newAny2stringadd#`<init>`(). => primary ctor <init>
1201+
example/ImplicitConversion.newAny2stringadd#`<init>`().(self) => param self
11811202
example/ImplicitConversion.newAny2stringadd#self. => val method self
11821203

11831204
Occurrences:
@@ -1864,13 +1885,16 @@ Schema => SemanticDB v4
18641885
Uri => NamedApplyBlock.scala
18651886
Text => empty
18661887
Language => Scala
1867-
Symbols => 16 entries
1888+
Symbols => 19 entries
18681889
Occurrences => 35 entries
18691890

18701891
Symbols:
18711892
example/NamedApplyBlockCaseClassConstruction. => final object NamedApplyBlockCaseClassConstruction
18721893
example/NamedApplyBlockCaseClassConstruction.Msg# => case class Msg
18731894
example/NamedApplyBlockCaseClassConstruction.Msg#`<init>`(). => primary ctor <init>
1895+
example/NamedApplyBlockCaseClassConstruction.Msg#`<init>`().(body) => val param body
1896+
example/NamedApplyBlockCaseClassConstruction.Msg#`<init>`().(head) => val param head
1897+
example/NamedApplyBlockCaseClassConstruction.Msg#`<init>`().(tail) => val param tail
18741898
example/NamedApplyBlockCaseClassConstruction.Msg#body. => val method body
18751899
example/NamedApplyBlockCaseClassConstruction.Msg#head. => val method head
18761900
example/NamedApplyBlockCaseClassConstruction.Msg#tail. => val method tail
@@ -1930,13 +1954,14 @@ Schema => SemanticDB v4
19301954
Uri => NamedArguments.scala
19311955
Text => empty
19321956
Language => Scala
1933-
Symbols => 5 entries
1957+
Symbols => 6 entries
19341958
Occurrences => 10 entries
19351959

19361960
Symbols:
19371961
example/NamedArguments# => class NamedArguments
19381962
example/NamedArguments#User# => case class User
19391963
example/NamedArguments#User#`<init>`(). => primary ctor <init>
1964+
example/NamedArguments#User#`<init>`().(name) => val param name
19401965
example/NamedArguments#User#name. => val method name
19411966
example/NamedArguments#`<init>`(). => primary ctor <init>
19421967

@@ -2171,7 +2196,7 @@ Schema => SemanticDB v4
21712196
Uri => Synthetic.scala
21722197
Text => empty
21732198
Language => Scala
2174-
Symbols => 21 entries
2199+
Symbols => 22 entries
21752200
Occurrences => 142 entries
21762201

21772202
Symbols:
@@ -2181,6 +2206,7 @@ example/Synthetic#F#`<init>`(). => primary ctor <init>
21812206
example/Synthetic#J# => class J
21822207
example/Synthetic#J#[T] => typeparam T
21832208
example/Synthetic#J#`<init>`(). => primary ctor <init>
2209+
example/Synthetic#J#`<init>`().(evidence$2) => implicit param evidence$2
21842210
example/Synthetic#J#arr. => val method arr
21852211
example/Synthetic#J#evidence$2. => implicit val method evidence$2
21862212
example/Synthetic#Name. => val method Name
@@ -2395,7 +2421,7 @@ Schema => SemanticDB v4
23952421
Uri => Types.scala
23962422
Text => empty
23972423
Language => Scala
2398-
Symbols => 117 entries
2424+
Symbols => 119 entries
23992425
Occurrences => 249 entries
24002426

24012427
Symbols:
@@ -2451,6 +2477,7 @@ types/Test.C#MethodType.x1(). => method x1
24512477
types/Test.C#MethodType.x2(). => method x2
24522478
types/Test.C#RepeatedType# => case class RepeatedType
24532479
types/Test.C#RepeatedType#`<init>`(). => primary ctor <init>
2480+
types/Test.C#RepeatedType#`<init>`().(s) => val param s
24542481
types/Test.C#RepeatedType#m1(). => method m1
24552482
types/Test.C#RepeatedType#m1().(x) => val param x
24562483
types/Test.C#RepeatedType#s. => val method s
@@ -2511,6 +2538,7 @@ types/Test.N#n(). => method n
25112538
types/ann# => class ann
25122539
types/ann#[T] => typeparam T
25132540
types/ann#`<init>`(). => primary ctor <init>
2541+
types/ann#`<init>`().(x) => param x
25142542
types/ann#x. => val method x
25152543
types/ann1# => class ann1
25162544
types/ann1#`<init>`(). => primary ctor <init>
@@ -2869,14 +2897,17 @@ Schema => SemanticDB v4
28692897
Uri => Vals.scala
28702898
Text => empty
28712899
Language => Scala
2872-
Symbols => 30 entries
2900+
Symbols => 33 entries
28732901
Occurrences => 122 entries
28742902

28752903
Symbols:
28762904
example/ValUsages. => final object ValUsages
28772905
example/ValUsages.v. => val method v
28782906
example/Vals# => abstract class Vals
28792907
example/Vals#`<init>`(). => primary ctor <init>
2908+
example/Vals#`<init>`().(p) => param p
2909+
example/Vals#`<init>`().(xp) => val param xp
2910+
example/Vals#`<init>`().(yp) => var param yp
28802911
example/Vals#`yam_=`(). => var method yam_=
28812912
example/Vals#`yfm_=`(). => final var method yfm_=
28822913
example/Vals#`yim_=`(). => var method yim_=
@@ -3113,13 +3144,14 @@ Schema => SemanticDB v4
31133144
Uri => semanticdb-extract.scala
31143145
Text => empty
31153146
Language => Scala
3116-
Symbols => 7 entries
3147+
Symbols => 8 entries
31173148
Occurrences => 23 entries
31183149

31193150
Symbols:
31203151
_empty_/AnObject. => final object AnObject
31213152
_empty_/AnObject.Foo# => case class Foo
31223153
_empty_/AnObject.Foo#`<init>`(). => primary ctor <init>
3154+
_empty_/AnObject.Foo#`<init>`().(x) => val param x
31233155
_empty_/AnObject.Foo#x. => val method x
31243156
_empty_/AnObject.foo(). => method foo
31253157
_empty_/AnObject.foo().(x) => val param x

0 commit comments

Comments
 (0)