Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add access information to SymbolInformation #12964

Merged
merged 3 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 21 additions & 2 deletions compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import NameOps._
import util.Spans.Span
import util.{SourceFile, SourcePosition}
import transform.SymUtils._
import SymbolInformation.{Kind => k}

import scala.jdk.CollectionConverters._
import scala.collection.mutable
Expand Down Expand Up @@ -450,13 +451,31 @@ class ExtractSemanticDB extends Phase:
props |= SymbolInformation.Property.ENUM.value
props

private def symbolAccess(sym: Symbol, kind: SymbolInformation.Kind)(using Context): Access =
kind match
case k.LOCAL | k.PARAMETER | k.SELF_PARAMETER | k.TYPE_PARAMETER | k.PACKAGE | k.PACKAGE_OBJECT =>
Access.Empty
case _ =>
if (sym.privateWithin == NoSymbol)
if (sym.isAllOf(PrivateLocal)) PrivateThisAccess()
else if (sym.is(Private)) PrivateAccess()
else if (sym.isAllOf(ProtectedLocal)) ProtectedThisAccess()
else if (sym.is(Protected)) ProtectedAccess()
else PublicAccess()
else
val ssym = symbolName(sym.privateWithin)
if (sym.is(Protected)) ProtectedWithinAccess(ssym)
else PrivateWithinAccess(ssym)

private def symbolInfo(sym: Symbol, symbolName: String, symkinds: Set[SymbolKind])(using Context): SymbolInformation =
val kind = symbolKind(sym, symkinds)
SymbolInformation(
symbol = symbolName,
language = Language.SCALA,
kind = symbolKind(sym, symkinds),
kind = kind,
properties = symbolProps(sym, symkinds),
displayName = Symbols.displaySymbol(sym)
displayName = Symbols.displaySymbol(sym),
access = symbolAccess(sym, kind),
)

private def registerSymbol(sym: Symbol, symbolName: String, symkinds: Set[SymbolKind])(using Context): Unit =
Expand Down
15 changes: 15 additions & 0 deletions compiler/src/dotty/tools/dotc/semanticdb/Tools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,24 @@ object Tools:
case UNKNOWN_LANGUAGE | Unrecognized(_) => "unknown"
end languageString

private def accessString(access: Access): String =
access match
case Access.Empty => ""
case _: PublicAccess => ""
case _: PrivateAccess => "private "
case _: ProtectedAccess => "protected "
case _: PrivateThisAccess => "private[this] "
case _: ProtectedThisAccess => "protected[this] "
case PrivateWithinAccess(ssym) =>
s"private[${ssym}] "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth noting here that the official metap will print the displayName of the associated SymbolInformation, and we have been trying to replicate its output style as much as possible

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the situation will be better with #12885 but I was just a bit lazy to duplicate the pretty printer to this PR... Can I go with this and prettify it in the future?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, this is fine for now

case ProtectedWithinAccess(ssym) =>
s"protected[${ssym}] "


private def processSymbol(info: SymbolInformation)(using sb: StringBuilder): Unit =
import SymbolInformation.Kind._
sb.append(info.symbol).append(" => ")
sb.append(accessString(info.access))
if info.isAbstract then sb.append("abstract ")
if info.isFinal then sb.append("final ")
if info.isSealed then sb.append("sealed ")
Expand Down
96 changes: 48 additions & 48 deletions tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Occurrences => 19 entries
Symbols:
example/Access# => class Access
example/Access#`<init>`(). => primary ctor <init>
example/Access#m1(). => method m1
example/Access#m2(). => method m2
example/Access#m3(). => method m3
example/Access#m4(). => method m4
example/Access#m5(). => method m5
example/Access#m6(). => method m6
example/Access#m1(). => private[this] method m1
example/Access#m2(). => private[this] method m2
example/Access#m3(). => private[example/Access#] method m3
example/Access#m4(). => protected method m4
example/Access#m5(). => protected[this] method m5
example/Access#m6(). => protected[example/] method m6
example/Access#m7(). => method m7

Occurrences:
Expand Down Expand Up @@ -201,12 +201,12 @@ annot/Annotations#`<init>`(). => primary ctor <init>
annot/Annotations#`<init>`().(x) => param x
annot/Annotations#field. => val method field
annot/Annotations#method(). => method method
annot/Annotations#x. => val method x
annot/Annotations#x. => private[this] val method x
annot/B# => class B
annot/B#`<init>`(). => primary ctor <init>
annot/B#`<init>`().(x) => param x
annot/B#`<init>`(+1). => ctor <init>
annot/B#x. => val method x
annot/B#x. => private[this] val method x
annot/M. => final object M
annot/M.m(). => macro m
annot/M.m().[TT] => typeparam TT
Expand Down Expand Up @@ -411,7 +411,7 @@ classes/C6#`<init>`().(x) => param x
classes/C6#copy$default$1(). => method copy$default$1
classes/C6#copy(). => method copy
classes/C6#copy().(x) => param x
classes/C6#x. => val method x
classes/C6#x. => private val method x
classes/C6. => final object C6
classes/C6.apply(). => method apply
classes/C6.apply().(x) => param x
Expand All @@ -421,19 +421,19 @@ classes/C6.unapply().(x$1) => param x$1
classes/C7# => class C7
classes/C7#`<init>`(). => primary ctor <init>
classes/C7#`<init>`().(x) => param x
classes/C7#x. => val method x
classes/C7#x. => private[this] val method x
classes/C8# => class C8
classes/C8#`<init>`(). => primary ctor <init>
classes/C8#`<init>`().(x) => param x
classes/C8#x. => val method x
classes/C8#x. => private[this] val method x
classes/C9# => class C9
classes/C9#`<init>`(). => primary ctor <init>
classes/C9#`<init>`().(x) => param x
classes/C9#x(). => var method x
classes/C9#x(). => private[this] var method x
classes/C10# => class C10
classes/C10#`<init>`(). => primary ctor <init>
classes/C10#`<init>`().(s) => param s
classes/C10#s. => val method s
classes/C10#s. => private[this] val method s
classes/C11# => class C11
classes/C11#`<init>`(). => primary ctor <init>
classes/C11#foo(). => macro foo
Expand Down Expand Up @@ -465,7 +465,7 @@ classes/M. => final object M
classes/M.C5# => class C5
classes/M.C5#`<init>`(). => primary ctor <init>
classes/M.C5#`<init>`().(x) => param x
classes/M.C5#x. => val method x
classes/M.C5#x. => private[this] val method x
classes/M.C5(). => final implicit method C5
classes/M.C5().(x) => param x
classes/N. => final object N
Expand Down Expand Up @@ -779,7 +779,7 @@ enumVal/Color#`<init>`(). => primary ctor <init>
enumVal/Color#`<init>`().(rgb) => val param rgb
enumVal/Color#rgb. => val method rgb
enumVal/Color. => final object Color
enumVal/Color.$values. => val method $values
enumVal/Color.$values. => private[this] val method $values
enumVal/Color.Blue. => case val static enum method Blue
enumVal/Color.Green. => case val static enum method Green
enumVal/Color.Red. => case val static enum method Red
Expand Down Expand Up @@ -828,9 +828,9 @@ _empty_/Enums. => final object Enums
_empty_/Enums.Coin# => abstract sealed enum class Coin
_empty_/Enums.Coin#`<init>`(). => primary ctor <init>
_empty_/Enums.Coin#`<init>`().(value) => param value
_empty_/Enums.Coin#value. => val method value
_empty_/Enums.Coin#value. => private[this] val method value
_empty_/Enums.Coin. => final object Coin
_empty_/Enums.Coin.$values. => val method $values
_empty_/Enums.Coin.$values. => private[this] val method $values
_empty_/Enums.Coin.Dime. => case val static enum method Dime
_empty_/Enums.Coin.Dollar. => case val static enum method Dollar
_empty_/Enums.Coin.Nickel. => case val static enum method Nickel
Expand All @@ -844,10 +844,10 @@ _empty_/Enums.Coin.values(). => method values
_empty_/Enums.Colour# => abstract sealed enum class Colour
_empty_/Enums.Colour#`<init>`(). => primary ctor <init>
_empty_/Enums.Colour. => final object Colour
_empty_/Enums.Colour.$new(). => method $new
_empty_/Enums.Colour.$new(). => private[this] method $new
_empty_/Enums.Colour.$new().($name) => param $name
_empty_/Enums.Colour.$new().(_$ordinal) => param _$ordinal
_empty_/Enums.Colour.$values. => val method $values
_empty_/Enums.Colour.$values. => private[this] val method $values
_empty_/Enums.Colour.Blue. => case val static enum method Blue
_empty_/Enums.Colour.Green. => case val static enum method Green
_empty_/Enums.Colour.Red. => case val static enum method Red
Expand All @@ -859,10 +859,10 @@ _empty_/Enums.Colour.values(). => method values
_empty_/Enums.Directions# => abstract sealed enum class Directions
_empty_/Enums.Directions#`<init>`(). => primary ctor <init>
_empty_/Enums.Directions. => final object Directions
_empty_/Enums.Directions.$new(). => method $new
_empty_/Enums.Directions.$new(). => private[this] method $new
_empty_/Enums.Directions.$new().($name) => param $name
_empty_/Enums.Directions.$new().(_$ordinal) => param _$ordinal
_empty_/Enums.Directions.$values. => val method $values
_empty_/Enums.Directions.$values. => private[this] val method $values
_empty_/Enums.Directions.East. => case val static enum method East
_empty_/Enums.Directions.North. => case val static enum method North
_empty_/Enums.Directions.South. => case val static enum method South
Expand Down Expand Up @@ -900,17 +900,17 @@ _empty_/Enums.Maybe.None. => case val static enum method None
_empty_/Enums.Maybe.fromOrdinal(). => method fromOrdinal
_empty_/Enums.Maybe.fromOrdinal().(ordinal) => param ordinal
_empty_/Enums.Planet# => abstract sealed enum class Planet
_empty_/Enums.Planet#G. => final val method G
_empty_/Enums.Planet#G. => private[this] final val method G
_empty_/Enums.Planet#`<init>`(). => primary ctor <init>
_empty_/Enums.Planet#`<init>`().(mass) => param mass
_empty_/Enums.Planet#`<init>`().(radius) => param radius
_empty_/Enums.Planet#mass. => val method mass
_empty_/Enums.Planet#radius. => val method radius
_empty_/Enums.Planet#mass. => private[this] val method mass
_empty_/Enums.Planet#radius. => private[this] val method radius
_empty_/Enums.Planet#surfaceGravity(). => method surfaceGravity
_empty_/Enums.Planet#surfaceWeight(). => method surfaceWeight
_empty_/Enums.Planet#surfaceWeight().(otherMass) => param otherMass
_empty_/Enums.Planet. => final object Planet
_empty_/Enums.Planet.$values. => val method $values
_empty_/Enums.Planet.$values. => private[this] val method $values
_empty_/Enums.Planet.Earth. => case val static enum method Earth
_empty_/Enums.Planet.Jupiter. => case val static enum method Jupiter
_empty_/Enums.Planet.Mars. => case val static enum method Mars
Expand All @@ -927,10 +927,10 @@ _empty_/Enums.Planet.values(). => method values
_empty_/Enums.Suits# => abstract sealed enum class Suits
_empty_/Enums.Suits#`<init>`(). => primary ctor <init>
_empty_/Enums.Suits. => final object Suits
_empty_/Enums.Suits.$new(). => method $new
_empty_/Enums.Suits.$new(). => private[this] method $new
_empty_/Enums.Suits.$new().($name) => param $name
_empty_/Enums.Suits.$new().(_$ordinal) => param _$ordinal
_empty_/Enums.Suits.$values. => val method $values
_empty_/Enums.Suits.$values. => private[this] val method $values
_empty_/Enums.Suits.Clubs. => case val static enum method Clubs
_empty_/Enums.Suits.Diamonds. => case val static enum method Diamonds
_empty_/Enums.Suits.Hearts. => case val static enum method Hearts
Expand All @@ -949,7 +949,7 @@ _empty_/Enums.Tag# => abstract sealed enum class Tag
_empty_/Enums.Tag#[A] => typeparam A
_empty_/Enums.Tag#`<init>`(). => primary ctor <init>
_empty_/Enums.Tag. => final object Tag
_empty_/Enums.Tag.$values. => val method $values
_empty_/Enums.Tag.$values. => private[this] val method $values
_empty_/Enums.Tag.BooleanTag. => case val static enum method BooleanTag
_empty_/Enums.Tag.IntTag. => case val static enum method IntTag
_empty_/Enums.Tag.fromOrdinal(). => method fromOrdinal
Expand All @@ -960,10 +960,10 @@ _empty_/Enums.Tag.values(). => method values
_empty_/Enums.WeekDays# => abstract sealed enum class WeekDays
_empty_/Enums.WeekDays#`<init>`(). => primary ctor <init>
_empty_/Enums.WeekDays. => final object WeekDays
_empty_/Enums.WeekDays.$new(). => method $new
_empty_/Enums.WeekDays.$new(). => private[this] method $new
_empty_/Enums.WeekDays.$new().($name) => param $name
_empty_/Enums.WeekDays.$new().(_$ordinal) => param _$ordinal
_empty_/Enums.WeekDays.$values. => val method $values
_empty_/Enums.WeekDays.$values. => private[this] val method $values
_empty_/Enums.WeekDays.Friday. => case val static enum method Friday
_empty_/Enums.WeekDays.Monday. => case val static enum method Monday
_empty_/Enums.WeekDays.Saturday. => case val static enum method Saturday
Expand Down Expand Up @@ -1568,7 +1568,7 @@ example/ImplicitConversion.newAny2stringadd#`+`(). => method +
example/ImplicitConversion.newAny2stringadd#`+`().(other) => param other
example/ImplicitConversion.newAny2stringadd#`<init>`(). => primary ctor <init>
example/ImplicitConversion.newAny2stringadd#`<init>`().(self) => param self
example/ImplicitConversion.newAny2stringadd#self. => val method self
example/ImplicitConversion.newAny2stringadd#self. => private val method self
example/ImplicitConversion.newAny2stringadd(). => final implicit method newAny2stringadd
example/ImplicitConversion.newAny2stringadd().(self) => param self
example/ImplicitConversion.newAny2stringadd().[A] => typeparam A
Expand Down Expand Up @@ -1769,7 +1769,7 @@ givens/InventedNames$package.given_Y# => class given_Y
givens/InventedNames$package.given_Y#`<init>`(). => primary ctor <init>
givens/InventedNames$package.given_Y#`<init>`().(x$1) => implicit val param x$1
givens/InventedNames$package.given_Y#doY(). => method doY
givens/InventedNames$package.given_Y#x$1. => implicit val method x$1
givens/InventedNames$package.given_Y#x$1. => protected implicit val method x$1
givens/InventedNames$package.given_Y(). => final implicit method given_Y
givens/InventedNames$package.given_Y().(x$1) => implicit param x$1
givens/InventedNames$package.given_Z_T# => class given_Z_T
Expand Down Expand Up @@ -3179,7 +3179,7 @@ Symbols:
example/ValUsages. => final object ValUsages
example/ValUsages.v. => val method v
example/Vals# => abstract class Vals
example/Vals#_explicitSetter(). => var method _explicitSetter
example/Vals#_explicitSetter(). => private[this] var method _explicitSetter
example/Vals#`<init>`(). => primary ctor <init>
example/Vals#`<init>`().(p) => param p
example/Vals#`<init>`().(xp) => val param xp
Expand All @@ -3198,19 +3198,19 @@ example/Vals#`yp_=`(). => var method yp_=
example/Vals#`yp_=`().(x$1) => param x$1
example/Vals#explicitSetter(). => method explicitSetter
example/Vals#m(). => method m
example/Vals#p. => val method p
example/Vals#p. => private[this] val method p
example/Vals#xam. => abstract val method xam
example/Vals#xfm. => final val method xfm
example/Vals#xim. => implicit val method xim
example/Vals#xlm. => val method xlm
example/Vals#xlm. => private[this] val method xlm
example/Vals#xm. => val method xm
example/Vals#xp. => val method xp
example/Vals#xzlm. => lazy val method xzlm
example/Vals#xzlm. => private[this] lazy val method xzlm
example/Vals#xzm. => lazy val method xzm
example/Vals#yam(). => abstract var method yam
example/Vals#yfm(). => final var method yfm
example/Vals#yim(). => implicit var method yim
example/Vals#ylm(). => var method ylm
example/Vals#ylm(). => private[this] var method ylm
example/Vals#ym(). => var method ym
example/Vals#yp(). => var method yp
local0 => val local xl
Expand Down Expand Up @@ -3401,10 +3401,10 @@ exports/example/Codec#`<init>`().(decode) => param decode
exports/example/Codec#`<init>`().(encode) => param encode
exports/example/Codec#decode(). => final method decode
exports/example/Codec#decode().(a) => param a
exports/example/Codec#decode. => val method decode
exports/example/Codec#decode. => private[this] val method decode
exports/example/Codec#encode(). => final method encode
exports/example/Codec#encode().(t) => param t
exports/example/Codec#encode. => val method encode
exports/example/Codec#encode. => private[this] val method encode
exports/example/Decoder# => trait Decoder
exports/example/Decoder#[T] => covariant typeparam T
exports/example/Decoder#`<init>`(). => primary ctor <init>
Expand Down Expand Up @@ -3516,7 +3516,7 @@ Symbols:
i9727/Test# => class Test
i9727/Test#`<init>`(). => primary ctor <init>
i9727/Test#`<init>`().(a) => param a
i9727/Test#a. => val method a
i9727/Test#a. => private[this] val method a
i9727/i9727$package. => final package object i9727
i9727/i9727$package.a. => val method a
i9727/i9727$package.b. => val method b
Expand Down Expand Up @@ -3777,7 +3777,7 @@ flags/p/package.AA#`<init>`().(y) => val param y
flags/p/package.AA#`<init>`().(z) => var param z
flags/p/package.AA#`z_=`(). => var method z_=
flags/p/package.AA#`z_=`().(x$1) => param x$1
flags/p/package.AA#x. => val method x
flags/p/package.AA#x. => private[this] val method x
flags/p/package.AA#y. => val method y
flags/p/package.AA#z(). => var method z
flags/p/package.C# => abstract class C
Expand All @@ -3792,9 +3792,9 @@ flags/p/package.C#`<init>`(+1). => ctor <init>
flags/p/package.C#`<init>`(+2). => ctor <init>
flags/p/package.C#`<init>`(+2).(t) => param t
flags/p/package.C#w(). => abstract method w
flags/p/package.C#x. => val method x
flags/p/package.C#y. => val method y
flags/p/package.C#z. => val method z
flags/p/package.C#x. => private[this] val method x
flags/p/package.C#y. => private[this] val method y
flags/p/package.C#z. => private[this] val method z
flags/p/package.S# => class S
flags/p/package.S#[T] => typeparam T
flags/p/package.S#`<init>`(). => primary ctor <init>
Expand All @@ -3808,13 +3808,13 @@ flags/p/package.Y# => final class Y
flags/p/package.Y#`<init>`(). => primary ctor <init>
flags/p/package.Z# => sealed trait Z
flags/p/package.Z#`<init>`(). => primary ctor <init>
flags/p/package.`y_=`(). => var method y_=
flags/p/package.`y_=`(). => protected var method y_=
flags/p/package.`y_=`().(x$1) => param x$1
flags/p/package.m(). => macro m
flags/p/package.m().[TT] => typeparam TT
flags/p/package.x. => lazy val method x
flags/p/package.x. => private[flags/p/] lazy val method x
flags/p/package.xs1. => val method xs1
flags/p/package.y(). => implicit var method y
flags/p/package.y(). => protected implicit var method y
flags/p/package.z(). => method z
flags/p/package.z().(pp) => param pp
local0 => val local xs2
Expand Down Expand Up @@ -4053,7 +4053,7 @@ types/ann# => class ann
types/ann#[T] => typeparam T
types/ann#`<init>`(). => primary ctor <init>
types/ann#`<init>`().(x) => param x
types/ann#x. => val method x
types/ann#x. => private[this] val method x
types/ann1# => class ann1
types/ann1#`<init>`(). => primary ctor <init>
types/ann2# => class ann2
Expand Down