Skip to content

Commit 9826edf

Browse files
committed
Integrate ExtractSsemanticDB in Compiler
1 parent 51edd7e commit 9826edf

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Compiler {
4040
List(new YCheckPositions) :: // YCheck positions
4141
List(new Staging) :: // Check PCP, heal quoted types and expand macros
4242
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
43+
List(new semanticdb.ExtractSemanticDB) ::
4344
List(new PostTyper) :: // Additional checks and cleanups after type checking
4445
List(new sbt.ExtractAPI) :: // Sends a representation of the API of classes to sbt via callbacks
4546
List(new SetRootTree) :: // Set the `rootTreeOrProvider` on class symbols

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import collection.mutable
1717
import java.lang.Character.{isJavaIdentifierPart, isJavaIdentifierStart}
1818
import java.nio.file.Paths
1919

20-
abstract class ExtractSemanticDB extends Phase {
20+
class ExtractSemanticDB extends Phase {
2121
import ast.tpd._
2222

2323
override val phaseName: String = ExtractSemanticDB.name
2424

2525
override def isRunnable(implicit ctx: Context) =
26-
super.isRunnable && ctx.settings.YretainTrees.value
26+
true || super.isRunnable && ctx.settings.YretainTrees.value
2727

2828
// Check not needed since it does not transform trees
2929
override def isCheckable: Boolean = false
@@ -49,9 +49,12 @@ abstract class ExtractSemanticDB extends Phase {
4949

5050
class Extractor extends TreeTraverser {
5151

52-
private val locals = mutable.HashMap[Symbol, java.lang.Integer]()
52+
private val locals = mutable.HashMap[Symbol, Int]()
53+
5354
val occurrences = new mutable.ListBuffer[SymbolOccurrence]()
5455

56+
val localIndex = mutable.HashMap[Int, Int]()
57+
5558
private var myLocalIdx: Int = -1
5659
private def nextLocalIdx() =
5760
myLocalIdx += 1
@@ -61,6 +64,8 @@ abstract class ExtractSemanticDB extends Phase {
6164

6265
def isJavaIdent(str: String) =
6366
isJavaIdentifierStart(str.head) && str.tail.forall(isJavaIdentifierPart)
67+
|| str == nme.CONSTRUCTOR.toString
68+
|| str == nme.STATIC_CONSTRUCTOR.toString
6469

6570
def nameToString(name: Name) =
6671
val str = name.toString
@@ -71,7 +76,7 @@ abstract class ExtractSemanticDB extends Phase {
7176
|| (sym.is(Param) || sym.owner.isClass) && isGlobal(sym.owner)
7277

7378
def ownerString(owner: Symbol): String =
74-
if owner.isRoot then "" else symbolName(owner) + "/"
79+
if owner.isRoot || owner.isEmptyPackage then "" else symbolName(owner) + "/"
7580

7681
def overloadIdx(sym: Symbol): String =
7782
val alts = sym.owner.info.decls.lookupAll(sym.name).toList
@@ -87,15 +92,15 @@ abstract class ExtractSemanticDB extends Phase {
8792
else
8893
val str = nameToString(sym.name)
8994
if sym.is(Package) then str
90-
else if sym.is(Module) || sym.isGetter && !sym.is(Mutable) then str + "."
9195
else if sym.isType then str + "#"
92-
else if sym.is(Method) then str + "(" + overloadIdx(sym) + ")"
93-
else if sym.is(TermParam) then "(" + str + ")"
96+
else if sym.isRealMethod then str + "(" + overloadIdx(sym) + ")"
97+
else if sym.is(TermParam) || sym.is(ParamAccessor) then "(" + str + ")"
9498
else if sym.is(TypeParam) then "[" + str + "]"
95-
else throw new AssertionError(i"unhandled symbol: $sym")
99+
else if sym.isTerm then str + "."
100+
else throw new AssertionError(i"unhandled symbol: $sym: ${sym.info} with ${sym.flagsString}")
96101

97-
def localIdx(sym: Symbol): Int =
98-
locals.getOrElseUpdate(sym, nextLocalIdx())
102+
def localIdx(sym: Symbol)(given Context): Int =
103+
localIndex.getOrElseUpdate(sym.span.start, nextLocalIdx())
99104

100105
if sym.isRoot then "_root_"
101106
else if sym.isEmptyPackage then "_empty_"
@@ -110,8 +115,13 @@ abstract class ExtractSemanticDB extends Phase {
110115
val (endLine, endCol) = lineCol(pos.span.end)
111116
Some(Range(startLine, startCol, endLine, endCol))
112117

118+
private def excluded(sym: Symbol)(given Context): Boolean =
119+
!sym.exists || sym.isLocalDummy
120+
113121
private def registerOccurrence(sym: Symbol, pos: SourcePosition, role: SymbolOccurrence.Role)(given Context): Unit =
114-
occurrences += SymbolOccurrence(symbolName(sym), range(pos), role)
122+
if !excluded(sym) then
123+
//println(i"register: ${symbolName(sym)}")
124+
occurrences += SymbolOccurrence(symbolName(sym), range(pos), role)
115125

116126
private def registerUse(sym: Symbol, pos: SourcePosition)(given Context) =
117127
registerOccurrence(sym, pos, SymbolOccurrence.Role.REFERENCE)

0 commit comments

Comments
 (0)