Blocks and for loops can be scopes.

This commit is contained in:
chriseth 2018-02-09 16:53:52 +01:00
parent 5f20129e65
commit e6d48bb72a
6 changed files with 78 additions and 6 deletions

View File

@ -610,6 +610,30 @@ void DeclarationRegistrationHelper::endVisit(ModifierDefinition&)
closeCurrentScope();
}
bool DeclarationRegistrationHelper::visit(Block& _block)
{
enterNewSubScope(_block);
return true;
}
void DeclarationRegistrationHelper::endVisit(Block&)
{
closeCurrentScope();
}
bool DeclarationRegistrationHelper::visit(ForStatement& _for)
{
// TODO special scoping rules for the init statement - if it is a block, then it should
// not open its own scope.
enterNewSubScope(_for);
return true;
}
void DeclarationRegistrationHelper::endVisit(ForStatement&)
{
closeCurrentScope();
}
void DeclarationRegistrationHelper::endVisit(VariableDeclarationStatement& _variableDeclarationStatement)
{
// Register the local variables with the function

View File

@ -167,6 +167,10 @@ private:
void endVisit(FunctionDefinition& _function) override;
bool visit(ModifierDefinition& _modifier) override;
void endVisit(ModifierDefinition& _modifier) override;
bool visit(Block& _block) override;
void endVisit(Block& _block) override;
bool visit(ForStatement& _forLoop) override;
void endVisit(ForStatement& _forLoop) override;
void endVisit(VariableDeclarationStatement& _variableDeclarationStatement) override;
bool visit(VariableDeclaration& _declaration) override;
bool visit(EventDefinition& _event) override;

View File

@ -43,6 +43,37 @@ bool ReferencesResolver::resolve(ASTNode const& _root)
return !m_errorOccurred;
}
bool ReferencesResolver::visit(Block const& _block)
{
if (!m_resolveInsideCode)
return false;
m_resolver.setScope(&_block);
return true;
}
void ReferencesResolver::endVisit(Block const& _block)
{
if (!m_resolveInsideCode)
return;
m_resolver.setScope(_block.scope());
}
bool ReferencesResolver::visit(ForStatement const& _for)
{
if (!m_resolveInsideCode)
return false;
m_resolver.setScope(&_for);
return true;
}
void ReferencesResolver::endVisit(ForStatement const& _for)
{
if (!m_resolveInsideCode)
return;
m_resolver.setScope(_for.scope());
}
bool ReferencesResolver::visit(Identifier const& _identifier)
{
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name());

View File

@ -57,7 +57,10 @@ public:
bool resolve(ASTNode const& _root);
private:
virtual bool visit(Block const&) override { return m_resolveInsideCode; }
virtual bool visit(Block const& _block) override;
virtual void endVisit(Block const& _block) override;
virtual bool visit(ForStatement const& _for) override;
virtual void endVisit(ForStatement const& _for) override;
virtual bool visit(Identifier const& _identifier) override;
virtual bool visit(ElementaryTypeName const& _typeName) override;
virtual bool visit(FunctionDefinition const& _functionDefinition) override;

View File

@ -415,6 +415,15 @@ bool VariableDeclaration::isLValue() const
return !isExternalCallableParameter() && !m_isConstant;
}
bool VariableDeclaration::isLocalVariable() const
{
auto s = scope();
return
dynamic_cast<CallableDeclaration const*>(s) ||
dynamic_cast<Block const*>(s) ||
dynamic_cast<ForStatement const*>(s);
}
bool VariableDeclaration::isCallableParameter() const
{
auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
@ -460,8 +469,7 @@ bool VariableDeclaration::isExternalCallableParameter() const
bool VariableDeclaration::canHaveAutoType() const
{
auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
return (!!callable && !isCallableParameter());
return isLocalVariable() && !isCallableParameter();
}
TypePointer VariableDeclaration::type() const

View File

@ -299,6 +299,8 @@ private:
/**
* Abstract class that is added to each AST node that can store local variables.
* Local variables in functions are always added to functions, even though they are not
* in scope for the whole function.
*/
class VariableScope
{
@ -672,7 +674,7 @@ public:
virtual bool isLValue() const override;
virtual bool isPartOfExternalInterface() const override { return isPublic(); }
bool isLocalVariable() const { return !!dynamic_cast<CallableDeclaration const*>(scope()); }
bool isLocalVariable() const;
/// @returns true if this variable is a parameter or return parameter of a function.
bool isCallableParameter() const;
/// @returns true if this variable is a return parameter of a function.
@ -1014,7 +1016,7 @@ private:
/**
* Brace-enclosed block containing zero or more statements.
*/
class Block: public Statement
class Block: public Statement, public Scopable
{
public:
Block(
@ -1121,7 +1123,7 @@ private:
/**
* For loop statement
*/
class ForStatement: public BreakableStatement
class ForStatement: public BreakableStatement, public Scopable
{
public:
ForStatement(