mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Scopes do not have to be declarations.
This commit is contained in:
parent
53289e15a2
commit
5f20129e65
@ -244,19 +244,24 @@ void NameAndTypeResolver::warnVariablesNamedLikeInstructions()
|
||||
}
|
||||
}
|
||||
|
||||
void NameAndTypeResolver::setScope(ASTNode const* _node)
|
||||
{
|
||||
m_currentScope = m_scopes[_node].get();
|
||||
}
|
||||
|
||||
bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode)
|
||||
{
|
||||
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(&_node))
|
||||
{
|
||||
bool success = true;
|
||||
m_currentScope = m_scopes[contract->scope()].get();
|
||||
setScope(contract->scope());
|
||||
solAssert(!!m_currentScope, "");
|
||||
|
||||
for (ASTPointer<InheritanceSpecifier> const& baseContract: contract->baseContracts())
|
||||
if (!resolveNamesAndTypes(*baseContract, true))
|
||||
success = false;
|
||||
|
||||
m_currentScope = m_scopes[contract].get();
|
||||
setScope(contract);
|
||||
|
||||
if (success)
|
||||
{
|
||||
@ -273,7 +278,7 @@ bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _res
|
||||
// these can contain code, only resolve parameters for now
|
||||
for (ASTPointer<ASTNode> const& node: contract->subNodes())
|
||||
{
|
||||
m_currentScope = m_scopes[contract].get();
|
||||
setScope(contract);
|
||||
if (!resolveNamesAndTypes(*node, false))
|
||||
{
|
||||
success = false;
|
||||
@ -287,12 +292,12 @@ bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _res
|
||||
if (!_resolveInsideCode)
|
||||
return success;
|
||||
|
||||
m_currentScope = m_scopes[contract].get();
|
||||
setScope(contract);
|
||||
|
||||
// now resolve references inside the code
|
||||
for (ASTPointer<ASTNode> const& node: contract->subNodes())
|
||||
{
|
||||
m_currentScope = m_scopes[contract].get();
|
||||
setScope(contract);
|
||||
if (!resolveNamesAndTypes(*node, true))
|
||||
success = false;
|
||||
}
|
||||
@ -301,7 +306,7 @@ bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _res
|
||||
else
|
||||
{
|
||||
if (m_scopes.count(&_node))
|
||||
m_currentScope = m_scopes[&_node].get();
|
||||
setScope(&_node);
|
||||
return ReferencesResolver(m_errorReporter, *this, _resolveInsideCode).resolve(_node);
|
||||
}
|
||||
}
|
||||
@ -632,14 +637,17 @@ void DeclarationRegistrationHelper::endVisit(EventDefinition&)
|
||||
closeCurrentScope();
|
||||
}
|
||||
|
||||
void DeclarationRegistrationHelper::enterNewSubScope(Declaration const& _declaration)
|
||||
void DeclarationRegistrationHelper::enterNewSubScope(ASTNode& _subScope)
|
||||
{
|
||||
if (auto s = dynamic_cast<Scopable*>(&_subScope))
|
||||
s->setScope(m_currentScope);
|
||||
|
||||
map<ASTNode const*, shared_ptr<DeclarationContainer>>::iterator iter;
|
||||
bool newlyAdded;
|
||||
shared_ptr<DeclarationContainer> container(new DeclarationContainer(m_currentScope, m_scopes[m_currentScope].get()));
|
||||
tie(iter, newlyAdded) = m_scopes.emplace(&_declaration, move(container));
|
||||
tie(iter, newlyAdded) = m_scopes.emplace(&_subScope, move(container));
|
||||
solAssert(newlyAdded, "Unable to add new scope.");
|
||||
m_currentScope = &_declaration;
|
||||
m_currentScope = &_subScope;
|
||||
}
|
||||
|
||||
void DeclarationRegistrationHelper::closeCurrentScope()
|
||||
@ -669,9 +677,10 @@ void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaratio
|
||||
|
||||
registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, warnAboutShadowing, m_errorReporter);
|
||||
|
||||
_declaration.setScope(m_currentScope);
|
||||
if (_opensScope)
|
||||
enterNewSubScope(_declaration);
|
||||
else
|
||||
_declaration.setScope(m_currentScope);
|
||||
}
|
||||
|
||||
string DeclarationRegistrationHelper::currentCanonicalName() const
|
||||
|
@ -97,6 +97,9 @@ public:
|
||||
/// @returns a list of similar identifiers in the current and enclosing scopes. May return empty string if no suggestions.
|
||||
std::string similarNameSuggestions(ASTString const& _name) const;
|
||||
|
||||
/// Sets the current scope.
|
||||
void setScope(ASTNode const* _node);
|
||||
|
||||
private:
|
||||
/// Internal version of @a resolveNamesAndTypes (called from there) throws exceptions on fatal errors.
|
||||
bool resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode = true);
|
||||
@ -169,7 +172,7 @@ private:
|
||||
bool visit(EventDefinition& _event) override;
|
||||
void endVisit(EventDefinition& _event) override;
|
||||
|
||||
void enterNewSubScope(Declaration const& _declaration);
|
||||
void enterNewSubScope(ASTNode& _subScope);
|
||||
void closeCurrentScope();
|
||||
void registerDeclaration(Declaration& _declaration, bool _opensScope);
|
||||
|
||||
|
@ -98,11 +98,12 @@ set<SourceUnit const*> SourceUnit::referencedSourceUnits(bool _recurse, set<Sour
|
||||
|
||||
SourceUnit const& Declaration::sourceUnit() const
|
||||
{
|
||||
solAssert(!!m_scope, "");
|
||||
ASTNode const* scope = m_scope;
|
||||
while (dynamic_cast<Declaration const*>(scope) && dynamic_cast<Declaration const*>(scope)->m_scope)
|
||||
scope = dynamic_cast<Declaration const*>(scope)->m_scope;
|
||||
return dynamic_cast<SourceUnit const&>(*scope);
|
||||
ASTNode const* s = scope();
|
||||
solAssert(s, "");
|
||||
// will not always be a declaratoion
|
||||
while (dynamic_cast<Scopable const*>(s) && dynamic_cast<Scopable const*>(s)->scope())
|
||||
s = dynamic_cast<Scopable const*>(s)->scope();
|
||||
return dynamic_cast<SourceUnit const&>(*s);
|
||||
}
|
||||
|
||||
string Declaration::sourceUnitName() const
|
||||
|
@ -139,10 +139,26 @@ private:
|
||||
std::vector<ASTPointer<ASTNode>> m_nodes;
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract class that is added to each AST node that is stored inside a scope
|
||||
* (including scopes).
|
||||
*/
|
||||
class Scopable
|
||||
{
|
||||
public:
|
||||
/// @returns the scope this declaration resides in. Can be nullptr if it is the global scope.
|
||||
/// Available only after name and type resolution step.
|
||||
ASTNode const* scope() const { return m_scope; }
|
||||
void setScope(ASTNode const* _scope) { m_scope = _scope; }
|
||||
|
||||
protected:
|
||||
ASTNode const* m_scope = nullptr;
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract AST class for a declaration (contract, function, struct, variable, import directive).
|
||||
*/
|
||||
class Declaration: public ASTNode
|
||||
class Declaration: public ASTNode, public Scopable
|
||||
{
|
||||
public:
|
||||
/// Visibility ordered from restricted to unrestricted.
|
||||
@ -171,7 +187,7 @@ public:
|
||||
ASTPointer<ASTString> const& _name,
|
||||
Visibility _visibility = Visibility::Default
|
||||
):
|
||||
ASTNode(_location), m_name(_name), m_visibility(_visibility), m_scope(nullptr) {}
|
||||
ASTNode(_location), m_name(_name), m_visibility(_visibility) {}
|
||||
|
||||
/// @returns the declared name.
|
||||
ASTString const& name() const { return *m_name; }
|
||||
@ -181,11 +197,6 @@ public:
|
||||
virtual bool isVisibleInContract() const { return visibility() != Visibility::External; }
|
||||
bool isVisibleInDerivedContracts() const { return isVisibleInContract() && visibility() >= Visibility::Internal; }
|
||||
|
||||
/// @returns the scope this declaration resides in. Can be nullptr if it is the global scope.
|
||||
/// Available only after name and type resolution step.
|
||||
ASTNode const* scope() const { return m_scope; }
|
||||
void setScope(ASTNode const* _scope) { m_scope = _scope; }
|
||||
|
||||
/// @returns the source unit this declaration is present in.
|
||||
SourceUnit const& sourceUnit() const;
|
||||
|
||||
@ -213,7 +224,6 @@ protected:
|
||||
private:
|
||||
ASTPointer<ASTString> m_name;
|
||||
Visibility m_visibility;
|
||||
ASTNode const* m_scope;
|
||||
};
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user