Skip to content

Commit f5f77a0

Browse files
authored
Merge pull request #13276 from ethereum/resolvingFlags
Settings struct for name resolution.
2 parents 454603e + e267500 commit f5f77a0

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

libsolidity/analysis/DeclarationContainer.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,32 +144,30 @@ bool DeclarationContainer::registerDeclaration(
144144

145145
vector<Declaration const*> DeclarationContainer::resolveName(
146146
ASTString const& _name,
147-
bool _recursive,
148-
bool _alsoInvisible,
149-
bool _onlyVisibleAsUnqualifiedNames
147+
ResolvingSettings _settings
150148
) const
151149
{
152150
solAssert(!_name.empty(), "Attempt to resolve empty name.");
153151
vector<Declaration const*> result;
154152

155153
if (m_declarations.count(_name))
156154
{
157-
if (_onlyVisibleAsUnqualifiedNames)
155+
if (_settings.onlyVisibleAsUnqualifiedNames)
158156
result += m_declarations.at(_name) | ranges::views::filter(&Declaration::isVisibleAsUnqualifiedName) | ranges::to_vector;
159157
else
160158
result += m_declarations.at(_name);
161159
}
162160

163-
if (_alsoInvisible && m_invisibleDeclarations.count(_name))
161+
if (_settings.alsoInvisible && m_invisibleDeclarations.count(_name))
164162
{
165-
if (_onlyVisibleAsUnqualifiedNames)
163+
if (_settings.onlyVisibleAsUnqualifiedNames)
166164
result += m_invisibleDeclarations.at(_name) | ranges::views::filter(&Declaration::isVisibleAsUnqualifiedName) | ranges::to_vector;
167165
else
168166
result += m_invisibleDeclarations.at(_name);
169167
}
170168

171-
if (result.empty() && _recursive && m_enclosingContainer)
172-
result = m_enclosingContainer->resolveName(_name, true, _alsoInvisible, _onlyVisibleAsUnqualifiedNames);
169+
if (result.empty() && _settings.recursive && m_enclosingContainer)
170+
result = m_enclosingContainer->resolveName(_name, _settings);
173171

174172
return result;
175173
}
@@ -209,7 +207,10 @@ void DeclarationContainer::populateHomonyms(back_insert_iterator<Homonyms> _it)
209207

210208
for (auto [name, location]: m_homonymCandidates)
211209
{
212-
vector<Declaration const*> const& declarations = m_enclosingContainer->resolveName(name, true, true);
210+
ResolvingSettings settings;
211+
settings.recursive = true;
212+
settings.alsoInvisible = true;
213+
vector<Declaration const*> const& declarations = m_enclosingContainer->resolveName(name, move(settings));
213214
if (!declarations.empty())
214215
_it = make_pair(location, declarations);
215216
}

libsolidity/analysis/DeclarationContainer.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,27 @@
2727
#include <liblangutil/Exceptions.h>
2828
#include <liblangutil/SourceLocation.h>
2929

30+
#include <memory>
31+
3032
namespace solidity::frontend
3133
{
3234

35+
/**
36+
* Settings for how the function DeclarationContainer::resolveName operates.
37+
*/
38+
struct ResolvingSettings
39+
{
40+
/// if true and there are no matching declarations in the current container,
41+
/// recursively searches the enclosing containers as well.
42+
bool recursive = false;
43+
/// if true, include invisible declaration in the results.
44+
bool alsoInvisible = false;
45+
/// if true, do not include declarations which can never actually be referenced using their
46+
/// name alone (without being qualified with the name of scope in which they are declared).
47+
bool onlyVisibleAsUnqualifiedNames = false;
48+
};
49+
50+
3351
/**
3452
* Container that stores mappings between names and declarations. It also contains a link to the
3553
* enclosing scope.
@@ -58,18 +76,8 @@ class DeclarationContainer
5876

5977
/// Finds all declarations that in the current scope can be referred to using specified name.
6078
/// @param _name the name to look for.
61-
/// @param _recursive if true and there are no matching declarations in the current container,
62-
/// recursively searches the enclosing containers as well.
63-
/// @param _alsoInvisible if true, include invisible declaration in the results.
64-
/// @param _onlyVisibleAsUnqualifiedNames if true, do not include declarations which can never
65-
/// actually be referenced using their name alone (without being qualified with the name
66-
/// of scope in which they are declared).
67-
std::vector<Declaration const*> resolveName(
68-
ASTString const& _name,
69-
bool _recursive = false,
70-
bool _alsoInvisible = false,
71-
bool _onlyVisibleAsUnqualifiedNames = false
72-
) const;
79+
/// @param _settings see ResolvingSettings
80+
std::vector<Declaration const*> resolveName(ASTString const& _name, ResolvingSettings _settings = ResolvingSettings{}) const;
7381
ASTNode const* enclosingNode() const { return m_enclosingNode; }
7482
DeclarationContainer const* enclosingContainer() const { return m_enclosingContainer; }
7583
std::map<ASTString, std::vector<Declaration const*>> const& declarations() const { return m_declarations; }

libsolidity/analysis/NameAndTypeResolver.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ bool NameAndTypeResolver::performImports(SourceUnit& _sourceUnit, map<string, So
8383
if (!imp->symbolAliases().empty())
8484
for (auto const& alias: imp->symbolAliases())
8585
{
86-
auto declarations = scope->second->resolveName(alias.symbol->name(), false);
86+
auto declarations = scope->second->resolveName(alias.symbol->name());
8787
if (declarations.empty())
8888
{
8989
m_errorReporter.declarationError(
@@ -176,12 +176,15 @@ vector<Declaration const*> NameAndTypeResolver::resolveName(ASTString const& _na
176176
auto iterator = m_scopes.find(_scope);
177177
if (iterator == end(m_scopes))
178178
return vector<Declaration const*>({});
179-
return iterator->second->resolveName(_name, false);
179+
return iterator->second->resolveName(_name);
180180
}
181181

182182
vector<Declaration const*> NameAndTypeResolver::nameFromCurrentScope(ASTString const& _name, bool _includeInvisibles) const
183183
{
184-
return m_currentScope->resolveName(_name, true, _includeInvisibles);
184+
ResolvingSettings settings;
185+
settings.recursive = true;
186+
settings.alsoInvisible = _includeInvisibles;
187+
return m_currentScope->resolveName(_name, move(settings));
185188
}
186189

187190
Declaration const* NameAndTypeResolver::pathFromCurrentScope(vector<ASTString> const& _path) const
@@ -197,12 +200,11 @@ std::vector<Declaration const*> NameAndTypeResolver::pathFromCurrentScopeWithAll
197200
solAssert(!_path.empty(), "");
198201
vector<Declaration const*> pathDeclarations;
199202

200-
vector<Declaration const*> candidates = m_currentScope->resolveName(
201-
_path.front(),
202-
/* _recursive */ true,
203-
/* _alsoInvisible */ false,
204-
/* _onlyVisibleAsUnqualifiedNames */ true
205-
);
203+
ResolvingSettings settings;
204+
settings.recursive = true;
205+
settings.alsoInvisible = false;
206+
settings.onlyVisibleAsUnqualifiedNames = true;
207+
vector<Declaration const*> candidates = m_currentScope->resolveName(_path.front(), move(settings));
206208

207209
for (size_t i = 1; i < _path.size() && candidates.size() == 1; i++)
208210
{
@@ -211,7 +213,7 @@ std::vector<Declaration const*> NameAndTypeResolver::pathFromCurrentScopeWithAll
211213

212214
pathDeclarations.push_back(candidates.front());
213215

214-
candidates = m_scopes.at(candidates.front())->resolveName(_path[i], false);
216+
candidates = m_scopes.at(candidates.front())->resolveName(_path[i]);
215217
}
216218
if (candidates.size() == 1)
217219
{

0 commit comments

Comments
 (0)