diff --git a/AST.cpp b/AST.cpp index a71f8c71f..a4655765b 100644 --- a/AST.cpp +++ b/AST.cpp @@ -746,7 +746,7 @@ void Identifier::checkTypeRequirements() if (asserts(m_referencedDeclaration)) BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Identifier not resolved.")); - VariableDeclaration* variable = dynamic_cast(m_referencedDeclaration); + VariableDeclaration const* variable = dynamic_cast(m_referencedDeclaration); if (variable) { if (!variable->getType()) @@ -756,29 +756,29 @@ void Identifier::checkTypeRequirements() return; } //@todo can we unify these with TypeName::toType()? - StructDefinition* structDef = dynamic_cast(m_referencedDeclaration); + StructDefinition const* structDef = dynamic_cast(m_referencedDeclaration); if (structDef) { // note that we do not have a struct type here - m_type = make_shared(make_shared(*structDef)); + m_type = make_shared(make_shared(*structDef)); return; } - FunctionDefinition* functionDef = dynamic_cast(m_referencedDeclaration); + FunctionDefinition const* functionDef = dynamic_cast(m_referencedDeclaration); if (functionDef) { // a function reference is not a TypeType, because calling a TypeType converts to the type. // Calling a function (e.g. function(12), otherContract.function(34)) does not do a type // conversion. - m_type = make_shared(*functionDef); + m_type = make_shared(*functionDef); return; } - ContractDefinition* contractDef = dynamic_cast(m_referencedDeclaration); + ContractDefinition const* contractDef = dynamic_cast(m_referencedDeclaration); if (contractDef) { - m_type = make_shared(make_shared(*contractDef)); + m_type = make_shared(make_shared(*contractDef)); return; } - MagicVariableDeclaration* magicVariable = dynamic_cast(m_referencedDeclaration); + MagicVariableDeclaration const* magicVariable = dynamic_cast(m_referencedDeclaration); if (magicVariable) { m_type = magicVariable->getType(); @@ -789,7 +789,7 @@ void Identifier::checkTypeRequirements() void ElementaryTypeNameExpression::checkTypeRequirements() { - m_type = make_shared(Type::fromElementaryTypeName(m_typeToken)); + m_type = make_shared(Type::fromElementaryTypeName(m_typeToken)); } void Literal::checkTypeRequirements() diff --git a/AST.h b/AST.h index f0071b952..0faea3fb9 100644 --- a/AST.h +++ b/AST.h @@ -132,18 +132,18 @@ class Declaration: public ASTNode { public: Declaration(Location const& _location, ASTPointer const& _name): - ASTNode(_location), m_name(_name) {} + ASTNode(_location), m_name(_name), m_scope(nullptr) {} /// @returns the declared name. ASTString const& getName() const { return *m_name; } /// @returns the scope this declaration resides in. Can be nullptr if it is the global scope. /// Available only after name and type resolution step. Declaration const* getScope() const { return m_scope; } - void setScope(Declaration* const& _scope) { m_scope = _scope; } + void setScope(Declaration const* _scope) { m_scope = _scope; } private: ASTPointer m_name; - Declaration* m_scope; + Declaration const* m_scope; }; /** @@ -369,19 +369,19 @@ class UserDefinedTypeName: public TypeName { public: UserDefinedTypeName(Location const& _location, ASTPointer const& _name): - TypeName(_location), m_name(_name) {} + TypeName(_location), m_name(_name), m_referencedDeclaration(nullptr) {} virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTConstVisitor& _visitor) const override; virtual std::shared_ptr toType() const override { return Type::fromUserDefinedTypeName(*this); } ASTString const& getName() const { return *m_name; } - void setReferencedDeclaration(Declaration& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; } + void setReferencedDeclaration(Declaration const& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; } Declaration const* getReferencedDeclaration() const { return m_referencedDeclaration; } private: ASTPointer m_name; - Declaration* m_referencedDeclaration; + Declaration const* m_referencedDeclaration; }; /** @@ -517,7 +517,7 @@ class Return: public Statement { public: Return(Location const& _location, ASTPointer _expression): - Statement(_location), m_expression(_expression) {} + Statement(_location), m_expression(_expression), m_returnParameters(nullptr) {} virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; @@ -791,21 +791,21 @@ class Identifier: public PrimaryExpression { public: Identifier(Location const& _location, ASTPointer const& _name): - PrimaryExpression(_location), m_name(_name) {} + PrimaryExpression(_location), m_name(_name), m_referencedDeclaration(nullptr) {} virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; ASTString const& getName() const { return *m_name; } - void setReferencedDeclaration(Declaration& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; } + void setReferencedDeclaration(Declaration const& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; } Declaration const* getReferencedDeclaration() const { return m_referencedDeclaration; } private: ASTPointer m_name; /// Declaration the name refers to. - Declaration* m_referencedDeclaration; + Declaration const* m_referencedDeclaration; }; /** diff --git a/DeclarationContainer.cpp b/DeclarationContainer.cpp index c0dea757a..c7081bc78 100644 --- a/DeclarationContainer.cpp +++ b/DeclarationContainer.cpp @@ -28,7 +28,7 @@ namespace dev namespace solidity { -bool DeclarationContainer::registerDeclaration(Declaration& _declaration, bool _update) +bool DeclarationContainer::registerDeclaration(Declaration const& _declaration, bool _update) { if (!_update && m_declarations.find(_declaration.getName()) != m_declarations.end()) return false; @@ -36,7 +36,7 @@ bool DeclarationContainer::registerDeclaration(Declaration& _declaration, bool _ return true; } -Declaration* DeclarationContainer::resolveName(ASTString const& _name, bool _recursive) const +Declaration const* DeclarationContainer::resolveName(ASTString const& _name, bool _recursive) const { auto result = m_declarations.find(_name); if (result != m_declarations.end()) diff --git a/DeclarationContainer.h b/DeclarationContainer.h index e1b363e04..c0a0b42c7 100644 --- a/DeclarationContainer.h +++ b/DeclarationContainer.h @@ -39,18 +39,19 @@ namespace solidity class DeclarationContainer { public: - explicit DeclarationContainer(Declaration* _enclosingDeclaration = nullptr, DeclarationContainer* _enclosingContainer = nullptr): + explicit DeclarationContainer(Declaration const* _enclosingDeclaration = nullptr, + DeclarationContainer const* _enclosingContainer = nullptr): m_enclosingDeclaration(_enclosingDeclaration), m_enclosingContainer(_enclosingContainer) {} /// Registers the declaration in the scope unless its name is already declared. Returns true iff /// it was not yet declared. - bool registerDeclaration(Declaration& _declaration, bool _update = false); - Declaration* resolveName(ASTString const& _name, bool _recursive = false) const; - Declaration* getEnclosingDeclaration() const { return m_enclosingDeclaration; } + bool registerDeclaration(Declaration const& _declaration, bool _update = false); + Declaration const* resolveName(ASTString const& _name, bool _recursive = false) const; + Declaration const* getEnclosingDeclaration() const { return m_enclosingDeclaration; } private: - Declaration* m_enclosingDeclaration; - DeclarationContainer* m_enclosingContainer; - std::map m_declarations; + Declaration const* m_enclosingDeclaration; + DeclarationContainer const* m_enclosingContainer; + std::map m_declarations; }; } diff --git a/GlobalContext.cpp b/GlobalContext.cpp index 45242bb1f..915d06bf2 100644 --- a/GlobalContext.cpp +++ b/GlobalContext.cpp @@ -68,16 +68,16 @@ void GlobalContext::setCurrentContract(ContractDefinition const& _contract) m_currentContract = &_contract; } -vector GlobalContext::getDeclarations() const +vector GlobalContext::getDeclarations() const { - vector declarations; + vector declarations; declarations.reserve(m_magicVariables.size() + 1); - for (ASTPointer const& variable: m_magicVariables) + for (ASTPointer const& variable: m_magicVariables) declarations.push_back(variable.get()); return declarations; } -MagicVariableDeclaration* GlobalContext::getCurrentThis() const +MagicVariableDeclaration const* GlobalContext::getCurrentThis() const { if (!m_thisPointer[m_currentContract]) m_thisPointer[m_currentContract] = make_shared( diff --git a/GlobalContext.h b/GlobalContext.h index ddbd049c2..50a21f702 100644 --- a/GlobalContext.h +++ b/GlobalContext.h @@ -47,17 +47,17 @@ class GlobalContext: private boost::noncopyable public: GlobalContext(); void setCurrentContract(ContractDefinition const& _contract); - MagicVariableDeclaration* getCurrentThis() const; + MagicVariableDeclaration const* getCurrentThis() const; /// @returns all magic variables. std::vector getMagicVariables() const; /// @returns a vector of all implicit global declarations excluding "this". - std::vector getDeclarations() const; + std::vector getDeclarations() const; private: - std::vector> m_magicVariables; + std::vector> m_magicVariables; ContractDefinition const* m_currentContract; - std::map> mutable m_thisPointer; + std::map> mutable m_thisPointer; }; } diff --git a/NameAndTypeResolver.cpp b/NameAndTypeResolver.cpp index 3715df6ad..540b066eb 100644 --- a/NameAndTypeResolver.cpp +++ b/NameAndTypeResolver.cpp @@ -32,9 +32,9 @@ namespace solidity { -NameAndTypeResolver::NameAndTypeResolver(std::vector const& _globals) +NameAndTypeResolver::NameAndTypeResolver(std::vector const& _globals) { - for (Declaration* declaration: _globals) + for (Declaration const* declaration: _globals) m_scopes[nullptr].registerDeclaration(*declaration); } @@ -70,13 +70,14 @@ void NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract) m_currentScope = &m_scopes[nullptr]; } -void NameAndTypeResolver::updateDeclaration(Declaration& _declaration) +void NameAndTypeResolver::updateDeclaration(Declaration const& _declaration) { m_scopes[nullptr].registerDeclaration(_declaration, true); - _declaration.setScope(nullptr); + if (asserts(_declaration.getScope() == nullptr)) + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Updated declaration outside global scope.")); } -Declaration* NameAndTypeResolver::resolveName(ASTString const& _name, Declaration const* _scope) const +Declaration const* NameAndTypeResolver::resolveName(ASTString const& _name, Declaration const* _scope) const { auto iterator = m_scopes.find(_scope); if (iterator == end(m_scopes)) @@ -84,7 +85,7 @@ Declaration* NameAndTypeResolver::resolveName(ASTString const& _name, Declaratio return iterator->second.resolveName(_name, false); } -Declaration* NameAndTypeResolver::getNameFromCurrentScope(ASTString const& _name, bool _recursive) +Declaration const* NameAndTypeResolver::getNameFromCurrentScope(ASTString const& _name, bool _recursive) { return m_currentScope->resolveName(_name, _recursive); } @@ -146,7 +147,7 @@ bool DeclarationRegistrationHelper::visit(VariableDeclaration& _declaration) return true; } -void DeclarationRegistrationHelper::enterNewSubScope(Declaration& _declaration) +void DeclarationRegistrationHelper::enterNewSubScope(Declaration const& _declaration) { map::iterator iter; bool newlyAdded; @@ -204,15 +205,14 @@ bool ReferencesResolver::visit(Return& _return) return true; } -bool ReferencesResolver::visit(Mapping& _mapping) +bool ReferencesResolver::visit(Mapping&) { - (void)_mapping; return true; } bool ReferencesResolver::visit(UserDefinedTypeName& _typeName) { - Declaration* declaration = m_resolver.getNameFromCurrentScope(_typeName.getName()); + Declaration const* declaration = m_resolver.getNameFromCurrentScope(_typeName.getName()); if (!declaration) BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_typeName.getLocation()) << errinfo_comment("Undeclared identifier.")); @@ -222,7 +222,7 @@ bool ReferencesResolver::visit(UserDefinedTypeName& _typeName) bool ReferencesResolver::visit(Identifier& _identifier) { - Declaration* declaration = m_resolver.getNameFromCurrentScope(_identifier.getName()); + Declaration const* declaration = m_resolver.getNameFromCurrentScope(_identifier.getName()); if (!declaration) BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_identifier.getLocation()) << errinfo_comment("Undeclared identifier.")); diff --git a/NameAndTypeResolver.h b/NameAndTypeResolver.h index 816d8006f..190cf6fa8 100644 --- a/NameAndTypeResolver.h +++ b/NameAndTypeResolver.h @@ -41,23 +41,23 @@ namespace solidity class NameAndTypeResolver: private boost::noncopyable { public: - explicit NameAndTypeResolver(std::vector const& _globals); + explicit NameAndTypeResolver(std::vector const& _globals); /// Registers all declarations found in the source unit. void registerDeclarations(SourceUnit& _sourceUnit); /// Resolves all names and types referenced from the given contract. void resolveNamesAndTypes(ContractDefinition& _contract); /// Updates the given global declaration (used for "this"). Not to be used with declarations /// that create their own scope. - void updateDeclaration(Declaration& _declaration); + void updateDeclaration(Declaration const& _declaration); /// Resolves the given @a _name inside the scope @a _scope. If @a _scope is omitted, /// the global scope is used (i.e. the one containing only the contract). /// @returns a pointer to the declaration on success or nullptr on failure. - Declaration* resolveName(ASTString const& _name, Declaration const* _scope = nullptr) const; + Declaration const* resolveName(ASTString const& _name, Declaration const* _scope = nullptr) const; /// Resolves a name in the "current" scope. Should only be called during the initial /// resolving phase. - Declaration* getNameFromCurrentScope(ASTString const& _name, bool _recursive = true); + Declaration const* getNameFromCurrentScope(ASTString const& _name, bool _recursive = true); private: /// Throws if @a _struct contains a recursive loop. Note that recursion via mappings is fine. @@ -91,12 +91,12 @@ private: void endVisit(VariableDefinition& _variableDefinition); bool visit(VariableDeclaration& _declaration); - void enterNewSubScope(Declaration& _declaration); + void enterNewSubScope(Declaration const& _declaration); void closeCurrentScope(); void registerDeclaration(Declaration& _declaration, bool _opensScope); std::map& m_scopes; - Declaration* m_currentScope; + Declaration const* m_currentScope; FunctionDefinition* m_currentFunction; };