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)
|
bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode)
|
||||||
{
|
{
|
||||||
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(&_node))
|
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(&_node))
|
||||||
{
|
{
|
||||||
bool success = true;
|
bool success = true;
|
||||||
m_currentScope = m_scopes[contract->scope()].get();
|
setScope(contract->scope());
|
||||||
solAssert(!!m_currentScope, "");
|
solAssert(!!m_currentScope, "");
|
||||||
|
|
||||||
for (ASTPointer<InheritanceSpecifier> const& baseContract: contract->baseContracts())
|
for (ASTPointer<InheritanceSpecifier> const& baseContract: contract->baseContracts())
|
||||||
if (!resolveNamesAndTypes(*baseContract, true))
|
if (!resolveNamesAndTypes(*baseContract, true))
|
||||||
success = false;
|
success = false;
|
||||||
|
|
||||||
m_currentScope = m_scopes[contract].get();
|
setScope(contract);
|
||||||
|
|
||||||
if (success)
|
if (success)
|
||||||
{
|
{
|
||||||
@ -273,7 +278,7 @@ bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _res
|
|||||||
// these can contain code, only resolve parameters for now
|
// these can contain code, only resolve parameters for now
|
||||||
for (ASTPointer<ASTNode> const& node: contract->subNodes())
|
for (ASTPointer<ASTNode> const& node: contract->subNodes())
|
||||||
{
|
{
|
||||||
m_currentScope = m_scopes[contract].get();
|
setScope(contract);
|
||||||
if (!resolveNamesAndTypes(*node, false))
|
if (!resolveNamesAndTypes(*node, false))
|
||||||
{
|
{
|
||||||
success = false;
|
success = false;
|
||||||
@ -287,12 +292,12 @@ bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _res
|
|||||||
if (!_resolveInsideCode)
|
if (!_resolveInsideCode)
|
||||||
return success;
|
return success;
|
||||||
|
|
||||||
m_currentScope = m_scopes[contract].get();
|
setScope(contract);
|
||||||
|
|
||||||
// now resolve references inside the code
|
// now resolve references inside the code
|
||||||
for (ASTPointer<ASTNode> const& node: contract->subNodes())
|
for (ASTPointer<ASTNode> const& node: contract->subNodes())
|
||||||
{
|
{
|
||||||
m_currentScope = m_scopes[contract].get();
|
setScope(contract);
|
||||||
if (!resolveNamesAndTypes(*node, true))
|
if (!resolveNamesAndTypes(*node, true))
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
@ -301,7 +306,7 @@ bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _res
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (m_scopes.count(&_node))
|
if (m_scopes.count(&_node))
|
||||||
m_currentScope = m_scopes[&_node].get();
|
setScope(&_node);
|
||||||
return ReferencesResolver(m_errorReporter, *this, _resolveInsideCode).resolve(_node);
|
return ReferencesResolver(m_errorReporter, *this, _resolveInsideCode).resolve(_node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -632,14 +637,17 @@ void DeclarationRegistrationHelper::endVisit(EventDefinition&)
|
|||||||
closeCurrentScope();
|
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;
|
map<ASTNode const*, shared_ptr<DeclarationContainer>>::iterator iter;
|
||||||
bool newlyAdded;
|
bool newlyAdded;
|
||||||
shared_ptr<DeclarationContainer> container(new DeclarationContainer(m_currentScope, m_scopes[m_currentScope].get()));
|
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.");
|
solAssert(newlyAdded, "Unable to add new scope.");
|
||||||
m_currentScope = &_declaration;
|
m_currentScope = &_subScope;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeclarationRegistrationHelper::closeCurrentScope()
|
void DeclarationRegistrationHelper::closeCurrentScope()
|
||||||
@ -669,9 +677,10 @@ void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaratio
|
|||||||
|
|
||||||
registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, warnAboutShadowing, m_errorReporter);
|
registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, warnAboutShadowing, m_errorReporter);
|
||||||
|
|
||||||
_declaration.setScope(m_currentScope);
|
|
||||||
if (_opensScope)
|
if (_opensScope)
|
||||||
enterNewSubScope(_declaration);
|
enterNewSubScope(_declaration);
|
||||||
|
else
|
||||||
|
_declaration.setScope(m_currentScope);
|
||||||
}
|
}
|
||||||
|
|
||||||
string DeclarationRegistrationHelper::currentCanonicalName() const
|
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.
|
/// @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;
|
std::string similarNameSuggestions(ASTString const& _name) const;
|
||||||
|
|
||||||
|
/// Sets the current scope.
|
||||||
|
void setScope(ASTNode const* _node);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Internal version of @a resolveNamesAndTypes (called from there) throws exceptions on fatal errors.
|
/// Internal version of @a resolveNamesAndTypes (called from there) throws exceptions on fatal errors.
|
||||||
bool resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode = true);
|
bool resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode = true);
|
||||||
@ -169,7 +172,7 @@ private:
|
|||||||
bool visit(EventDefinition& _event) override;
|
bool visit(EventDefinition& _event) override;
|
||||||
void endVisit(EventDefinition& _event) override;
|
void endVisit(EventDefinition& _event) override;
|
||||||
|
|
||||||
void enterNewSubScope(Declaration const& _declaration);
|
void enterNewSubScope(ASTNode& _subScope);
|
||||||
void closeCurrentScope();
|
void closeCurrentScope();
|
||||||
void registerDeclaration(Declaration& _declaration, bool _opensScope);
|
void registerDeclaration(Declaration& _declaration, bool _opensScope);
|
||||||
|
|
||||||
|
@ -98,11 +98,12 @@ set<SourceUnit const*> SourceUnit::referencedSourceUnits(bool _recurse, set<Sour
|
|||||||
|
|
||||||
SourceUnit const& Declaration::sourceUnit() const
|
SourceUnit const& Declaration::sourceUnit() const
|
||||||
{
|
{
|
||||||
solAssert(!!m_scope, "");
|
ASTNode const* s = scope();
|
||||||
ASTNode const* scope = m_scope;
|
solAssert(s, "");
|
||||||
while (dynamic_cast<Declaration const*>(scope) && dynamic_cast<Declaration const*>(scope)->m_scope)
|
// will not always be a declaratoion
|
||||||
scope = dynamic_cast<Declaration const*>(scope)->m_scope;
|
while (dynamic_cast<Scopable const*>(s) && dynamic_cast<Scopable const*>(s)->scope())
|
||||||
return dynamic_cast<SourceUnit const&>(*scope);
|
s = dynamic_cast<Scopable const*>(s)->scope();
|
||||||
|
return dynamic_cast<SourceUnit const&>(*s);
|
||||||
}
|
}
|
||||||
|
|
||||||
string Declaration::sourceUnitName() const
|
string Declaration::sourceUnitName() const
|
||||||
|
@ -139,10 +139,26 @@ private:
|
|||||||
std::vector<ASTPointer<ASTNode>> m_nodes;
|
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).
|
* Abstract AST class for a declaration (contract, function, struct, variable, import directive).
|
||||||
*/
|
*/
|
||||||
class Declaration: public ASTNode
|
class Declaration: public ASTNode, public Scopable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Visibility ordered from restricted to unrestricted.
|
/// Visibility ordered from restricted to unrestricted.
|
||||||
@ -171,7 +187,7 @@ public:
|
|||||||
ASTPointer<ASTString> const& _name,
|
ASTPointer<ASTString> const& _name,
|
||||||
Visibility _visibility = Visibility::Default
|
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.
|
/// @returns the declared name.
|
||||||
ASTString const& name() const { return *m_name; }
|
ASTString const& name() const { return *m_name; }
|
||||||
@ -181,11 +197,6 @@ public:
|
|||||||
virtual bool isVisibleInContract() const { return visibility() != Visibility::External; }
|
virtual bool isVisibleInContract() const { return visibility() != Visibility::External; }
|
||||||
bool isVisibleInDerivedContracts() const { return isVisibleInContract() && visibility() >= Visibility::Internal; }
|
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.
|
/// @returns the source unit this declaration is present in.
|
||||||
SourceUnit const& sourceUnit() const;
|
SourceUnit const& sourceUnit() const;
|
||||||
|
|
||||||
@ -213,7 +224,6 @@ protected:
|
|||||||
private:
|
private:
|
||||||
ASTPointer<ASTString> m_name;
|
ASTPointer<ASTString> m_name;
|
||||||
Visibility m_visibility;
|
Visibility m_visibility;
|
||||||
ASTNode const* m_scope;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user