Skip to content

Commit 503b952

Browse files
committed
Fixes to localIdx and more comments
1 parent 6017117 commit 503b952

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,30 @@ class ExtractSemanticDB extends Phase {
2828
// Check not needed since it does not transform trees
2929
override def isCheckable: Boolean = false
3030

31-
//def genInfo(unit: CompilationUnit, occurrences: List[SymbolOccurrence])
32-
3331
override def run(implicit ctx: Context): Unit = {
3432
val extract = Extractor()
3533
val unit = ctx.compilationUnit
3634
extract.traverse(unit.tpdTree)
3735
ExtractSemanticDB.write(unit.source, extract.occurrences.toList)
3836
}
3937

38+
/** Extractor of symbol occurrences from trees */
4039
class Extractor extends TreeTraverser {
4140

42-
private val locals = mutable.HashMap[Symbol, Int]()
41+
private var nextLocalIdx: Int = 0
4342

44-
val occurrences = new mutable.ListBuffer[SymbolOccurrence]()
43+
/** The index of a local symbol */
44+
private val locals = mutable.HashMap[Symbol, Int]()
4545

46-
val localIndex = mutable.HashMap[Int, Int]()
46+
/** The local symbol(s) starting at given offset */
47+
private val symsAtOffset = new mutable.HashMap[Int, Set[Symbol]]() {
48+
override def default(key: Int) = Set[Symbol]()
49+
}
4750

48-
private var myLocalIdx: Int = -1
49-
private def nextLocalIdx() =
50-
myLocalIdx += 1
51-
myLocalIdx
51+
/** The extracted symbol occurrences */
52+
val occurrences = new mutable.ListBuffer[SymbolOccurrence]()
5253

54+
/** The semanticdb name of the given symbol */
5355
private def symbolName(sym: Symbol)(given ctx: Context): String =
5456

5557
def isJavaIdent(str: String) =
@@ -59,6 +61,7 @@ class ExtractSemanticDB extends Phase {
5961
val str = name.toString
6062
if isJavaIdent(str) then str else "`" + str + "`"
6163

64+
/** Is symbol global? Non-global symbols get localX names */
6265
def isGlobal(sym: Symbol): Boolean =
6366
sym.is(Package)
6467
|| (sym.is(Param) || sym.owner.isClass) && isGlobal(sym.owner)
@@ -87,8 +90,21 @@ class ExtractSemanticDB extends Phase {
8790
else if sym.isTerm then str + "."
8891
else throw new AssertionError(i"unhandled symbol: $sym: ${sym.info} with ${sym.flagsString}")
8992

90-
def localIdx(sym: Symbol)(given Context): Int =
91-
localIndex.getOrElseUpdate(sym.span.start, nextLocalIdx())
93+
/** The index of local symbol `sym`. Symbols with the same name and
94+
* the same starting position have the same index.
95+
*/
96+
def localIdx(sym: Symbol)(given Context): Int = {
97+
def computeLocalIdx(): Int =
98+
symsAtOffset(sym.span.start).find(_.name == sym.name) match
99+
case Some(other) => localIdx(other)
100+
case None =>
101+
val idx = nextLocalIdx
102+
nextLocalIdx += 1
103+
locals(sym) = idx
104+
symsAtOffset(sym.span.start) += sym
105+
idx
106+
locals.getOrElseUpdate(sym, computeLocalIdx())
107+
}
92108

93109
if sym.isRoot then "_root_"
94110
else if sym.isEmptyPackage then "_empty_"
@@ -108,7 +124,6 @@ class ExtractSemanticDB extends Phase {
108124

109125
private def registerOccurrence(sym: Symbol, pos: SourcePosition, role: SymbolOccurrence.Role)(given Context): Unit =
110126
if !excluded(sym) then
111-
//println(i"register: ${symbolName(sym)}")
112127
occurrences += SymbolOccurrence(symbolName(sym), range(pos), role)
113128

114129
private def registerUse(sym: Symbol, pos: SourcePosition)(given Context) =

0 commit comments

Comments
 (0)