From 9b740b03ffd6b7b604498cc7df8a191f5055f147 Mon Sep 17 00:00:00 2001 From: a3d4 Date: Sun, 27 Sep 2020 00:00:56 +0200 Subject: [PATCH] Simplify DeclarationRegistrationHelper --- libsolidity/analysis/NameAndTypeResolver.cpp | 25 +++++++------------- libsolidity/analysis/NameAndTypeResolver.h | 1 - libsolidity/ast/AST.cpp | 23 ++++++++++++++---- libsolidity/ast/AST.h | 12 ++++++---- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/libsolidity/analysis/NameAndTypeResolver.cpp b/libsolidity/analysis/NameAndTypeResolver.cpp index 49f8b6d7c..b84972f24 100644 --- a/libsolidity/analysis/NameAndTypeResolver.cpp +++ b/libsolidity/analysis/NameAndTypeResolver.cpp @@ -109,7 +109,7 @@ bool NameAndTypeResolver::performImports(SourceUnit& _sourceUnit, mapsecond->declarations()) for (auto const& declaration: nameAndDeclaration.second) if (!DeclarationRegistrationHelper::registerDeclaration( - target, *declaration, &nameAndDeclaration.first, &imp->location(), true, false, m_errorReporter + target, *declaration, &nameAndDeclaration.first, &imp->location(), false, m_errorReporter )) error = true; } @@ -446,7 +446,6 @@ bool DeclarationRegistrationHelper::registerDeclaration( Declaration const& _declaration, string const* _name, SourceLocation const* _errorLocation, - bool _warnOnShadow, bool _inactive, ErrorReporter& _errorReporter ) @@ -456,7 +455,11 @@ bool DeclarationRegistrationHelper::registerDeclaration( string name = _name ? *_name : _declaration.name(); Declaration const* shadowedDeclaration = nullptr; - if (_warnOnShadow && !name.empty() && _container.enclosingContainer()) + // Do not warn about shadowing for structs and enums because their members are + // not accessible without prefixes. Also do not warn about event parameters + // because they do not participate in any proper scope. + bool warnOnShadow = !_declaration.isStructMember() && !_declaration.isEnumValue() && !_declaration.isEventParameter(); + if (warnOnShadow && !name.empty() && _container.enclosingContainer()) for (auto const* decl: _container.enclosingContainer()->resolveName(name, true, true)) shadowedDeclaration = decl; @@ -623,23 +626,13 @@ void DeclarationRegistrationHelper::closeCurrentScope() void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaration) { solAssert(m_currentScope && m_scopes.count(m_currentScope), "No current scope."); - - bool warnAboutShadowing = true; - // Do not warn about shadowing for structs and enums because their members are - // not accessible without prefixes. Also do not warn about event parameters - // because they don't participate in any proper scope. - if ( - dynamic_cast(m_currentScope) || - dynamic_cast(m_currentScope) || - dynamic_cast(m_currentScope) - ) - warnAboutShadowing = false; + solAssert(m_currentScope == _declaration.scope(), "Unexpected current scope."); // Register declaration as inactive if we are in block scope. bool inactive = (dynamic_cast(m_currentScope) || dynamic_cast(m_currentScope)); - registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, warnAboutShadowing, inactive, m_errorReporter); + registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, inactive, m_errorReporter); solAssert(_declaration.annotation().scope == m_currentScope, ""); solAssert(_declaration.annotation().contract == m_currentContract, ""); diff --git a/libsolidity/analysis/NameAndTypeResolver.h b/libsolidity/analysis/NameAndTypeResolver.h index a5a178a83..3412ce2b7 100644 --- a/libsolidity/analysis/NameAndTypeResolver.h +++ b/libsolidity/analysis/NameAndTypeResolver.h @@ -152,7 +152,6 @@ public: Declaration const& _declaration, std::string const* _name, langutil::SourceLocation const* _errorLocation, - bool _warnOnShadow, bool _inactive, langutil::ErrorReporter& _errorReporter ); diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 7094ccf83..31129feab 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -495,6 +495,24 @@ string Scopable::sourceUnitName() const return *sourceUnit().annotation().path; } +bool Declaration::isEnumValue() const +{ + solAssert(scope(), ""); + return dynamic_cast(scope()); +} + +bool Declaration::isStructMember() const +{ + solAssert(scope(), ""); + return dynamic_cast(scope()); +} + +bool Declaration::isEventParameter() const +{ + solAssert(scope(), ""); + return dynamic_cast(scope()); +} + DeclarationAnnotation& Declaration::annotation() const { return initAnnotation(); @@ -617,11 +635,6 @@ bool VariableDeclaration::isLibraryFunctionParameter() const return false; } -bool VariableDeclaration::isEventParameter() const -{ - return dynamic_cast(scope()) != nullptr; -} - bool VariableDeclaration::hasReferenceOrMappingType() const { solAssert(typeName().annotation().type, "Can only be called after reference resolution"); diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 4b1ecd2ca..7a986225d 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -258,10 +258,16 @@ public: bool isVisibleAsLibraryMember() const { return visibility() >= Visibility::Internal; } virtual bool isVisibleViaContractTypeAccess() const { return false; } - virtual bool isLValue() const { return false; } virtual bool isPartOfExternalInterface() const { return false; } + /// @returns true if this is a declaration of an enum member. + bool isEnumValue() const; + /// @returns true if this is a declaration of a struct member. + bool isStructMember() const; + /// @returns true if this is a declaration of a parameter of an event. + bool isEventParameter() const; + /// @returns the type of expressions referencing this declaration. /// This can only be called once types of variable declarations have already been resolved. virtual TypePointer type() const = 0; @@ -952,10 +958,6 @@ public: bool isConstructorParameter() const; /// @returns true iff this variable is a parameter(or return parameter of a library function bool isLibraryFunctionParameter() const; - /// @returns true if the type of the variable does not need to be specified, i.e. it is declared - /// in the body of a function or modifier. - /// @returns true if this variable is a parameter of an event. - bool isEventParameter() const; /// @returns true if the type of the variable is a reference or mapping type, i.e. /// array, struct or mapping. These types can take a data location (and often require it). /// Can only be called after reference resolution.