Skip to content

Commit 2397f09

Browse files
authored
Merge pull request #13182 from ethereum/identifierPath_declarations
Add all path declarations in IdentifierPath annotation
2 parents 9d72842 + 03409be commit 2397f09

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

libsolidity/analysis/NameAndTypeResolver.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,18 @@ vector<Declaration const*> NameAndTypeResolver::nameFromCurrentScope(ASTString c
185185
}
186186

187187
Declaration const* NameAndTypeResolver::pathFromCurrentScope(vector<ASTString> const& _path) const
188+
{
189+
if (auto declarations = pathFromCurrentScopeWithAllDeclarations(_path); !declarations.empty())
190+
return declarations.back();
191+
192+
return nullptr;
193+
}
194+
195+
std::vector<Declaration const*> NameAndTypeResolver::pathFromCurrentScopeWithAllDeclarations(std::vector<ASTString> const& _path) const
188196
{
189197
solAssert(!_path.empty(), "");
198+
vector<Declaration const*> pathDeclarations;
199+
190200
vector<Declaration const*> candidates = m_currentScope->resolveName(
191201
_path.front(),
192202
/* _recursive */ true,
@@ -197,13 +207,19 @@ Declaration const* NameAndTypeResolver::pathFromCurrentScope(vector<ASTString> c
197207
for (size_t i = 1; i < _path.size() && candidates.size() == 1; i++)
198208
{
199209
if (!m_scopes.count(candidates.front()))
200-
return nullptr;
210+
return {};
211+
212+
pathDeclarations.push_back(candidates.front());
213+
201214
candidates = m_scopes.at(candidates.front())->resolveName(_path[i], false);
202215
}
203216
if (candidates.size() == 1)
204-
return candidates.front();
217+
{
218+
pathDeclarations.push_back(candidates.front());
219+
return pathDeclarations;
220+
}
205221
else
206-
return nullptr;
222+
return {};
207223
}
208224

209225
void NameAndTypeResolver::warnHomonymDeclarations() const

libsolidity/analysis/NameAndTypeResolver.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class NameAndTypeResolver
9393
/// Should only be called during the initial resolving phase.
9494
/// @note Returns a null pointer if any component in the path was not unique or not found.
9595
Declaration const* pathFromCurrentScope(std::vector<ASTString> const& _path) const;
96+
/// Resolves a path starting from the "current" scope, but also searches parent scopes.
97+
/// Should only be called during the initial resolving phase.
98+
/// @note Returns an empty vector if any component in the path was non-unique or not found. Otherwise, all declarations along the path are returned.
99+
std::vector<Declaration const*> pathFromCurrentScopeWithAllDeclarations(std::vector<ASTString> const& _path) const;
96100

97101
/// Generate and store warnings about declarations with the same name.
98102
void warnHomonymDeclarations() const;

libsolidity/analysis/ReferencesResolver.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,15 @@ void ReferencesResolver::endVisit(ModifierDefinition const&)
173173

174174
void ReferencesResolver::endVisit(IdentifierPath const& _path)
175175
{
176-
Declaration const* declaration = m_resolver.pathFromCurrentScope(_path.path());
177-
if (!declaration)
176+
std::vector<Declaration const*> declarations = m_resolver.pathFromCurrentScopeWithAllDeclarations(_path.path());
177+
if (declarations.empty())
178178
{
179179
m_errorReporter.fatalDeclarationError(7920_error, _path.location(), "Identifier not found or not unique.");
180180
return;
181181
}
182182

183-
_path.annotation().referencedDeclaration = declaration;
183+
_path.annotation().referencedDeclaration = declarations.back();
184+
_path.annotation().pathDeclarations = std::move(declarations);
184185
}
185186

186187
bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)

libsolidity/ast/ASTAnnotations.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ struct IdentifierPathAnnotation: ASTAnnotation
256256
Declaration const* referencedDeclaration = nullptr;
257257
/// What kind of lookup needs to be done (static, virtual, super) find the declaration.
258258
util::SetOnce<VirtualLookup> requiredLookup;
259+
260+
/// Declaration of each path element.
261+
std::vector<Declaration const*> pathDeclarations;
259262
};
260263

261264
struct ExpressionAnnotation: ASTAnnotation

0 commit comments

Comments
 (0)