From e6d48bb72a82e2b3140d5a98da2961e401430a25 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 9 Feb 2018 16:53:52 +0100 Subject: Blocks and for loops can be scopes. --- libsolidity/ast/AST.cpp | 12 ++++++++++-- libsolidity/ast/AST.h | 8 +++++--- 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'libsolidity/ast') diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index af007908..60a15aeb 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -415,6 +415,15 @@ bool VariableDeclaration::isLValue() const return !isExternalCallableParameter() && !m_isConstant; } +bool VariableDeclaration::isLocalVariable() const +{ + auto s = scope(); + return + dynamic_cast(s) || + dynamic_cast(s) || + dynamic_cast(s); +} + bool VariableDeclaration::isCallableParameter() const { auto const* callable = dynamic_cast(scope()); @@ -460,8 +469,7 @@ bool VariableDeclaration::isExternalCallableParameter() const bool VariableDeclaration::canHaveAutoType() const { - auto const* callable = dynamic_cast(scope()); - return (!!callable && !isCallableParameter()); + return isLocalVariable() && !isCallableParameter(); } TypePointer VariableDeclaration::type() const diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index a0089d64..1e4c6591 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -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(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( -- cgit v1.2.3