1818#include " slang/ast/Lookup.h"
1919#include " slang/ast/Scope.h"
2020#include " slang/ast/Symbol.h"
21+ #include " slang/ast/Scope.h"
2122#include " slang/ast/symbols/CompilationUnitSymbols.h"
2223#include " slang/diagnostics/Diagnostics.h"
2324#include " slang/syntax/AllSyntax.h"
@@ -39,7 +40,6 @@ struct DefIndex {
3940 std::unordered_map<std::string, slang::SourceLocation> byName;
4041};
4142
42-
4343static void indexDeclarators (const slang::syntax::SyntaxNode &node,
4444 DefIndex &out) {
4545 using namespace slang ::syntax;
@@ -60,12 +60,22 @@ static void indexDeclarators(const slang::syntax::SyntaxNode &node,
6060
6161// / Walk the syntax tree from its root down to collect all declarators;
6262// / declarators contain symbol definitions we can lookup later.
63- static void walk (const slang::syntax::SyntaxNode &node, DefIndex &out) {
64- indexDeclarators (node, out);
63+ static void walk (const slang::syntax::SyntaxNode &root, DefIndex &out) {
64+ llvm::SmallVector<const slang::syntax::SyntaxNode *, 32 > worklist;
65+ worklist.push_back (&root);
66+
67+ while (!worklist.empty ()) {
68+ const slang::syntax::SyntaxNode *node = worklist.pop_back_val ();
6569
66- for (size_t i = 0 , e = node.getChildCount (); i < e; ++i)
67- if (auto *c = node.childNode (i))
68- walk (*c, out);
70+ // Collect declarators at this node.
71+ indexDeclarators (*node, out);
72+
73+ // Push children to the stack.
74+ for (size_t i = 0 , e = node->getChildCount (); i < e; ++i) {
75+ if (auto *c = node->childNode (i))
76+ worklist.push_back (c);
77+ }
78+ }
6979}
7080
7181static bool isLocationInRange (slang::SourceRange r, slang::SourceLocation loc) {
@@ -84,7 +94,7 @@ struct DefinitionVisitor : public slang::syntax::SyntaxVisitor<
8494
8595 DefinitionVisitor (const slang::ast::Compilation &comp,
8696 const slang::ast::Scope &rootScope,
87- slang::SourceLocation queryLoc, DefIndex &defIndex)
97+ slang::SourceLocation queryLoc, const DefIndex &defIndex)
8898 : comp(comp), rootScope(rootScope), queryLoc(queryLoc),
8999 defIndex (defIndex) {}
90100
@@ -129,7 +139,7 @@ struct DefinitionVisitor : public slang::syntax::SyntaxVisitor<
129139 const slang::ast::Compilation ∁
130140 const slang::ast::Scope &rootScope;
131141 slang::SourceLocation queryLoc;
132- DefIndex &defIndex;
142+ const DefIndex &defIndex;
133143};
134144
135145} // namespace
0 commit comments