diff options
author | Christian <c@ethdev.com> | 2014-12-06 09:19:10 +0800 |
---|---|---|
committer | Christian <c@ethdev.com> | 2014-12-08 19:53:56 +0800 |
commit | 13640d7db82562592958fcce2d7ce8155cc7ea35 (patch) | |
tree | a27fc38d8fb642ad602d875e3218542adb533de5 | |
parent | c78c330634997e5d8e2ec27db9e3d9cf50bae230 (diff) | |
download | dexon-solidity-13640d7db82562592958fcce2d7ce8155cc7ea35.tar dexon-solidity-13640d7db82562592958fcce2d7ce8155cc7ea35.tar.gz dexon-solidity-13640d7db82562592958fcce2d7ce8155cc7ea35.tar.bz2 dexon-solidity-13640d7db82562592958fcce2d7ce8155cc7ea35.tar.lz dexon-solidity-13640d7db82562592958fcce2d7ce8155cc7ea35.tar.xz dexon-solidity-13640d7db82562592958fcce2d7ce8155cc7ea35.tar.zst dexon-solidity-13640d7db82562592958fcce2d7ce8155cc7ea35.zip |
Clear separation between ASTVisitor and ASTConstVisitor and more const specifiers.
-rw-r--r-- | AST.cpp | 73 | ||||
-rw-r--r-- | AST.h | 119 | ||||
-rw-r--r-- | ASTPrinter.h | 2 | ||||
-rw-r--r-- | ASTVisitor.h | 72 | ||||
-rw-r--r-- | Compiler.cpp | 6 | ||||
-rw-r--r-- | Compiler.h | 2 | ||||
-rw-r--r-- | ExpressionCompiler.cpp | 2 | ||||
-rw-r--r-- | ExpressionCompiler.h | 2 | ||||
-rw-r--r-- | Types.cpp | 36 | ||||
-rw-r--r-- | Types.h | 12 |
10 files changed, 173 insertions, 153 deletions
@@ -40,12 +40,25 @@ void SourceUnit::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } +void SourceUnit::accept(ASTConstVisitor& _visitor) const +{ + if (_visitor.visit(*this)) + listAccept(m_nodes, _visitor); + _visitor.endVisit(*this); +} + void ImportDirective::accept(ASTVisitor& _visitor) { _visitor.visit(*this); _visitor.endVisit(*this); } +void ImportDirective::accept(ASTConstVisitor& _visitor) const +{ + _visitor.visit(*this); + _visitor.endVisit(*this); +} + void ContractDefinition::accept(ASTVisitor& _visitor) { if (_visitor.visit(*this)) @@ -57,7 +70,7 @@ void ContractDefinition::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void ContractDefinition::accept(ASTVisitor& _visitor) const +void ContractDefinition::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -75,14 +88,14 @@ void StructDefinition::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void StructDefinition::accept(ASTVisitor& _visitor) const +void StructDefinition::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) listAccept(m_members, _visitor); _visitor.endVisit(*this); } -void StructDefinition::checkValidityOfMembers() +void StructDefinition::checkValidityOfMembers() const { checkMemberTypes(); checkRecursion(); @@ -95,7 +108,7 @@ void ParameterList::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void ParameterList::accept(ASTVisitor& _visitor) const +void ParameterList::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) listAccept(m_parameters, _visitor); @@ -114,7 +127,7 @@ void FunctionDefinition::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void FunctionDefinition::accept(ASTVisitor& _visitor) const +void FunctionDefinition::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -134,7 +147,7 @@ void VariableDeclaration::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void VariableDeclaration::accept(ASTVisitor& _visitor) const +void VariableDeclaration::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) if (m_typeName) @@ -148,7 +161,7 @@ void TypeName::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void TypeName::accept(ASTVisitor& _visitor) const +void TypeName::accept(ASTConstVisitor& _visitor) const { _visitor.visit(*this); _visitor.endVisit(*this); @@ -160,7 +173,7 @@ void ElementaryTypeName::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void ElementaryTypeName::accept(ASTVisitor& _visitor) const +void ElementaryTypeName::accept(ASTConstVisitor& _visitor) const { _visitor.visit(*this); _visitor.endVisit(*this); @@ -172,7 +185,7 @@ void UserDefinedTypeName::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void UserDefinedTypeName::accept(ASTVisitor& _visitor) const +void UserDefinedTypeName::accept(ASTConstVisitor& _visitor) const { _visitor.visit(*this); _visitor.endVisit(*this); @@ -188,7 +201,7 @@ void Mapping::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void Mapping::accept(ASTVisitor& _visitor) const +void Mapping::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -205,7 +218,7 @@ void Block::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void Block::accept(ASTVisitor& _visitor) const +void Block::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) listAccept(m_statements, _visitor); @@ -224,7 +237,7 @@ void IfStatement::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void IfStatement::accept(ASTVisitor& _visitor) const +void IfStatement::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -246,7 +259,7 @@ void WhileStatement::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void WhileStatement::accept(ASTVisitor& _visitor) const +void WhileStatement::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -262,7 +275,7 @@ void Continue::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void Continue::accept(ASTVisitor& _visitor) const +void Continue::accept(ASTConstVisitor& _visitor) const { _visitor.visit(*this); _visitor.endVisit(*this); @@ -274,7 +287,7 @@ void Break::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void Break::accept(ASTVisitor& _visitor) const +void Break::accept(ASTConstVisitor& _visitor) const { _visitor.visit(*this); _visitor.endVisit(*this); @@ -288,7 +301,7 @@ void Return::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void Return::accept(ASTVisitor& _visitor) const +void Return::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) if (m_expression) @@ -304,7 +317,7 @@ void ExpressionStatement::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void ExpressionStatement::accept(ASTVisitor& _visitor) const +void ExpressionStatement::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) if (m_expression) @@ -323,7 +336,7 @@ void VariableDefinition::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void VariableDefinition::accept(ASTVisitor& _visitor) const +void VariableDefinition::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -344,7 +357,7 @@ void Assignment::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void Assignment::accept(ASTVisitor& _visitor) const +void Assignment::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -361,7 +374,7 @@ void UnaryOperation::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void UnaryOperation::accept(ASTVisitor& _visitor) const +void UnaryOperation::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) m_subExpression->accept(_visitor); @@ -378,7 +391,7 @@ void BinaryOperation::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void BinaryOperation::accept(ASTVisitor& _visitor) const +void BinaryOperation::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -398,7 +411,7 @@ void FunctionCall::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void FunctionCall::accept(ASTVisitor& _visitor) const +void FunctionCall::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -415,7 +428,7 @@ void MemberAccess::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void MemberAccess::accept(ASTVisitor& _visitor) const +void MemberAccess::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) m_expression->accept(_visitor); @@ -432,7 +445,7 @@ void IndexAccess::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void IndexAccess::accept(ASTVisitor& _visitor) const +void IndexAccess::accept(ASTConstVisitor& _visitor) const { if (_visitor.visit(*this)) { @@ -448,7 +461,7 @@ void Identifier::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void Identifier::accept(ASTVisitor& _visitor) const +void Identifier::accept(ASTConstVisitor& _visitor) const { _visitor.visit(*this); _visitor.endVisit(*this); @@ -460,7 +473,7 @@ void ElementaryTypeNameExpression::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void ElementaryTypeNameExpression::accept(ASTVisitor& _visitor) const +void ElementaryTypeNameExpression::accept(ASTConstVisitor& _visitor) const { _visitor.visit(*this); _visitor.endVisit(*this); @@ -472,7 +485,7 @@ void Literal::accept(ASTVisitor& _visitor) _visitor.endVisit(*this); } -void Literal::accept(ASTVisitor& _visitor) const +void Literal::accept(ASTConstVisitor& _visitor) const { _visitor.visit(*this); _visitor.endVisit(*this); @@ -498,14 +511,14 @@ vector<FunctionDefinition const*> ContractDefinition::getInterfaceFunctions() co return exportedFunctions; } -void StructDefinition::checkMemberTypes() +void StructDefinition::checkMemberTypes() const { for (ASTPointer<VariableDeclaration> const& member: getMembers()) if (!member->getType()->canBeStored()) BOOST_THROW_EXCEPTION(member->createTypeError("Type cannot be used in struct.")); } -void StructDefinition::checkRecursion() +void StructDefinition::checkRecursion() const { set<StructDefinition const*> definitionsSeen; vector<StructDefinition const*> queue = {this}; @@ -520,7 +533,7 @@ void StructDefinition::checkRecursion() for (ASTPointer<VariableDeclaration> const& member: def->getMembers()) if (member->getType()->getCategory() == Type::Category::STRUCT) { - UserDefinedTypeName const& typeName = dynamic_cast<UserDefinedTypeName&>(*member->getTypeName()); + UserDefinedTypeName const& typeName = dynamic_cast<UserDefinedTypeName const&>(*member->getTypeName()); queue.push_back(&dynamic_cast<StructDefinition const&>(*typeName.getReferencedDeclaration())); } } @@ -39,6 +39,7 @@ namespace solidity { class ASTVisitor; +class ASTConstVisitor; /** @@ -54,7 +55,7 @@ public: virtual ~ASTNode() {} virtual void accept(ASTVisitor& _visitor) = 0; - virtual void accept(ASTVisitor& _visitor) const = 0; + virtual void accept(ASTConstVisitor& _visitor) const = 0; template <class T> static void listAccept(std::vector<ASTPointer<T>>& _list, ASTVisitor& _visitor) { @@ -62,9 +63,9 @@ public: element->accept(_visitor); } template <class T> - static void listAccept(std::vector<ASTPointer<T>> const& _list, ASTVisitor& _visitor) + static void listAccept(std::vector<ASTPointer<T>> const& _list, ASTConstVisitor& _visitor) { - for (ASTPointer<T const> const& element: _list) + for (ASTPointer<T> const& element: _list) element->accept(_visitor); } @@ -96,6 +97,7 @@ public: ASTNode(_location), m_nodes(_nodes) {} virtual void accept(ASTVisitor& _visitor) override; + virtual void accept(ASTConstVisitor& _visitor) const override; std::vector<ASTPointer<ASTNode>> getNodes() const { return m_nodes; } @@ -115,6 +117,7 @@ public: ASTNode(_location), m_identifier(_identifier) {} virtual void accept(ASTVisitor& _visitor) override; + virtual void accept(ASTConstVisitor& _visitor) const override; ASTString const& getIdentifier() const { return *m_identifier; } @@ -135,7 +138,7 @@ public: 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* getScope() const { return m_scope; } + Declaration const* getScope() const { return m_scope; } void setScope(Declaration* const& _scope) { m_scope = _scope; } private: @@ -163,7 +166,7 @@ public: {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; std::vector<ASTPointer<StructDefinition>> const& getDefinedStructs() const { return m_definedStructs; } std::vector<ASTPointer<VariableDeclaration>> const& getStateVariables() const { return m_stateVariables; } @@ -186,17 +189,17 @@ public: std::vector<ASTPointer<VariableDeclaration>> const& _members): Declaration(_location, _name), m_members(_members) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; std::vector<ASTPointer<VariableDeclaration>> const& getMembers() const { return m_members; } /// Checks that the members do not include any recursive structs and have valid types /// (e.g. no functions). - void checkValidityOfMembers(); + void checkValidityOfMembers() const; private: - void checkMemberTypes(); - void checkRecursion(); + void checkMemberTypes() const; + void checkRecursion() const; std::vector<ASTPointer<VariableDeclaration>> m_members; }; @@ -213,7 +216,7 @@ public: std::vector<ASTPointer<VariableDeclaration>> const& _parameters): ASTNode(_location), m_parameters(_parameters) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; std::vector<ASTPointer<VariableDeclaration>> const& getParameters() const { return m_parameters; } @@ -240,7 +243,7 @@ public: {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; bool isPublic() const { return m_isPublic; } bool isDeclaredConst() const { return m_isDeclaredConst; } @@ -281,16 +284,16 @@ public: ASTPointer<ASTString> const& _name): Declaration(_location, _name), m_typeName(_type) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; - TypeName* getTypeName() const { return m_typeName.get(); } + TypeName const* getTypeName() const { return m_typeName.get(); } /// Returns the declared or inferred type. Can be an empty pointer if no type was explicitly /// declared and there is no assignment to the variable that fixes the type. std::shared_ptr<Type const> const& getType() const { return m_type; } void setType(std::shared_ptr<Type const> const& _type) { m_type = _type; } - bool isLocalVariable() const { return !!dynamic_cast<FunctionDefinition*>(getScope()); } + bool isLocalVariable() const { return !!dynamic_cast<FunctionDefinition const*>(getScope()); } private: ASTPointer<TypeName> m_typeName; ///< can be empty ("var") @@ -309,7 +312,7 @@ public: Declaration(Location(), std::make_shared<ASTString>(_name)), m_type(_type) {} virtual void accept(ASTVisitor&) override { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("MagicVariableDeclaration used inside real AST.")); } - virtual void accept(ASTVisitor&) const override { BOOST_THROW_EXCEPTION(InternalCompilerError() + virtual void accept(ASTConstVisitor&) const override { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("MagicVariableDeclaration used inside real AST.")); } std::shared_ptr<Type const> const& getType() const { return m_type; } @@ -329,12 +332,12 @@ class TypeName: public ASTNode public: explicit TypeName(Location const& _location): ASTNode(_location) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; /// Retrieve the element of the type hierarchy this node refers to. Can return an empty shared /// pointer until the types have been resolved using the @ref NameAndTypeResolver. /// If it returns an empty shared pointer after that, this indicates that the type was not found. - virtual std::shared_ptr<Type> toType() const = 0; + virtual std::shared_ptr<Type const> toType() const = 0; }; /** @@ -350,8 +353,8 @@ public: if (asserts(Token::isElementaryTypeName(_type))) BOOST_THROW_EXCEPTION(InternalCompilerError()); } virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; - virtual std::shared_ptr<Type> toType() const override { return Type::fromElementaryTypeName(m_type); } + virtual void accept(ASTConstVisitor& _visitor) const override; + virtual std::shared_ptr<Type const> toType() const override { return Type::fromElementaryTypeName(m_type); } Token::Value getTypeName() const { return m_type; } @@ -368,8 +371,8 @@ public: UserDefinedTypeName(Location const& _location, ASTPointer<ASTString> const& _name): TypeName(_location), m_name(_name) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; - virtual std::shared_ptr<Type> toType() const override { return Type::fromUserDefinedTypeName(*this); } + virtual void accept(ASTConstVisitor& _visitor) const override; + virtual std::shared_ptr<Type const> toType() const override { return Type::fromUserDefinedTypeName(*this); } ASTString const& getName() const { return *m_name; } void setReferencedDeclaration(Declaration& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; } @@ -391,8 +394,8 @@ public: ASTPointer<TypeName> const& _valueType): TypeName(_location), m_keyType(_keyType), m_valueType(_valueType) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; - virtual std::shared_ptr<Type> toType() const override { return Type::fromMapping(*this); } + virtual void accept(ASTConstVisitor& _visitor) const override; + virtual std::shared_ptr<Type const> toType() const override { return Type::fromMapping(*this); } ElementaryTypeName const& getKeyType() const { return *m_keyType; } TypeName const& getValueType() const { return *m_valueType; } @@ -431,7 +434,7 @@ public: Block(Location const& _location, std::vector<ASTPointer<Statement>> const& _statements): Statement(_location), m_statements(_statements) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; @@ -451,13 +454,13 @@ public: Statement(_location), m_condition(_condition), m_trueBody(_trueBody), m_falseBody(_falseBody) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; - Expression& getCondition() const { return *m_condition; } - Statement& getTrueStatement() const { return *m_trueBody; } + Expression const& getCondition() const { return *m_condition; } + Statement const& getTrueStatement() const { return *m_trueBody; } /// @returns the "else" part of the if statement or nullptr if there is no "else" part. - Statement* getFalseStatement() const { return m_falseBody.get(); } + Statement const* getFalseStatement() const { return m_falseBody.get(); } private: ASTPointer<Expression> m_condition; @@ -481,11 +484,11 @@ public: ASTPointer<Statement> const& _body): BreakableStatement(_location), m_condition(_condition), m_body(_body) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; - Expression& getCondition() const { return *m_condition; } - Statement& getBody() const { return *m_body; } + Expression const& getCondition() const { return *m_condition; } + Statement const& getBody() const { return *m_body; } private: ASTPointer<Expression> m_condition; @@ -497,7 +500,7 @@ class Continue: public Statement public: Continue(Location const& _location): Statement(_location) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override {} }; @@ -506,7 +509,7 @@ class Break: public Statement public: Break(Location const& _location): Statement(_location) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override {} }; @@ -516,7 +519,7 @@ public: Return(Location const& _location, ASTPointer<Expression> _expression): Statement(_location), m_expression(_expression) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; void setFunctionReturnParameters(ParameterList& _parameters) { m_returnParameters = &_parameters; } @@ -526,7 +529,7 @@ public: BOOST_THROW_EXCEPTION(InternalCompilerError()); return *m_returnParameters; } - Expression* getExpression() const { return m_expression.get(); } + Expression const* getExpression() const { return m_expression.get(); } private: ASTPointer<Expression> m_expression; ///< value to return, optional @@ -547,11 +550,11 @@ public: ASTPointer<Expression> _value): Statement(_location), m_variable(_variable), m_value(_value) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; VariableDeclaration const& getDeclaration() const { return *m_variable; } - Expression* getExpression() const { return m_value.get(); } + Expression const* getExpression() const { return m_value.get(); } private: ASTPointer<VariableDeclaration> m_variable; @@ -567,10 +570,10 @@ public: ExpressionStatement(Location const& _location, ASTPointer<Expression> _expression): Statement(_location), m_expression(_expression) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; - Expression& getExpression() const { return *m_expression; } + Expression const& getExpression() const { return *m_expression; } private: ASTPointer<Expression> m_expression; @@ -632,12 +635,12 @@ public: if (asserts(Token::isAssignmentOp(_assignmentOperator))) BOOST_THROW_EXCEPTION(InternalCompilerError()); } virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; - Expression& getLeftHandSide() const { return *m_leftHandSide; } + Expression const& getLeftHandSide() const { return *m_leftHandSide; } Token::Value getAssignmentOperator() const { return m_assigmentOperator; } - Expression& getRightHandSide() const { return *m_rightHandSide; } + Expression const& getRightHandSide() const { return *m_rightHandSide; } private: ASTPointer<Expression> m_leftHandSide; @@ -660,12 +663,12 @@ public: if (asserts(Token::isUnaryOp(_operator))) BOOST_THROW_EXCEPTION(InternalCompilerError()); } virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; Token::Value getOperator() const { return m_operator; } bool isPrefixOperation() const { return m_isPrefix; } - Expression& getSubExpression() const { return *m_subExpression; } + Expression const& getSubExpression() const { return *m_subExpression; } private: Token::Value m_operator; @@ -687,11 +690,11 @@ public: if (asserts(Token::isBinaryOp(_operator) || Token::isCompareOp(_operator))) BOOST_THROW_EXCEPTION(InternalCompilerError()); } virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; - Expression& getLeftExpression() const { return *m_left; } - Expression& getRightExpression() const { return *m_right; } + Expression const& getLeftExpression() const { return *m_left; } + Expression const& getRightExpression() const { return *m_right; } Token::Value getOperator() const { return m_operator; } Type const& getCommonType() const { return *m_commonType; } @@ -715,11 +718,11 @@ public: std::vector<ASTPointer<Expression>> const& _arguments): Expression(_location), m_expression(_expression), m_arguments(_arguments) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; - Expression& getExpression() const { return *m_expression; } - std::vector<ASTPointer<Expression>> const& getArguments() const { return m_arguments; } + Expression const& getExpression() const { return *m_expression; } + std::vector<ASTPointer<Expression const>> getArguments() const { return {m_arguments.begin(), m_arguments.end()}; } /// Returns true if this is not an actual function call, but an explicit type conversion /// or constructor call. @@ -740,8 +743,8 @@ public: ASTPointer<ASTString> const& _memberName): Expression(_location), m_expression(_expression), m_memberName(_memberName) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; - Expression& getExpression() const { return *m_expression; } + virtual void accept(ASTConstVisitor& _visitor) const override; + Expression const& getExpression() const { return *m_expression; } ASTString const& getMemberName() const { return *m_memberName; } virtual void checkTypeRequirements() override; @@ -760,11 +763,11 @@ public: ASTPointer<Expression> const& _index): Expression(_location), m_base(_base), m_index(_index) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; - Expression& getBaseExpression() const { return *m_base; } - Expression& getIndexExpression() const { return *m_index; } + Expression const& getBaseExpression() const { return *m_base; } + Expression const& getIndexExpression() const { return *m_index; } private: ASTPointer<Expression> m_base; @@ -790,7 +793,7 @@ public: Identifier(Location const& _location, ASTPointer<ASTString> const& _name): PrimaryExpression(_location), m_name(_name) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; ASTString const& getName() const { return *m_name; } @@ -819,7 +822,7 @@ public: if (asserts(Token::isElementaryTypeName(_typeToken))) BOOST_THROW_EXCEPTION(InternalCompilerError()); } virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; Token::Value getTypeToken() const { return m_typeToken; } @@ -837,7 +840,7 @@ public: Literal(Location const& _location, Token::Value _token, ASTPointer<ASTString> const& _value): PrimaryExpression(_location), m_token(_token), m_value(_value) {} virtual void accept(ASTVisitor& _visitor) override; - virtual void accept(ASTVisitor& _visitor) const override; + virtual void accept(ASTConstVisitor& _visitor) const override; virtual void checkTypeRequirements() override; Token::Value getToken() const { return m_token; } diff --git a/ASTPrinter.h b/ASTPrinter.h index 8092dbd5..5a918715 100644 --- a/ASTPrinter.h +++ b/ASTPrinter.h @@ -33,7 +33,7 @@ namespace solidity /** * Pretty-printer for the abstract syntax tree (the "pretty" is arguable) for debugging purposes. */ -class ASTPrinter: public ASTVisitor +class ASTPrinter: public ASTConstVisitor { public: /// Create a printer for the given abstract syntax tree. If the source is specified, diff --git a/ASTVisitor.h b/ASTVisitor.h index c731588d..4e1a4945 100644 --- a/ASTVisitor.h +++ b/ASTVisitor.h @@ -75,40 +75,6 @@ public: virtual bool visit(ElementaryTypeNameExpression&) { return true; } virtual bool visit(Literal&) { return true; } - virtual bool visit(ASTNode const&) { return true; } - virtual bool visit(SourceUnit const&) { return true; } - virtual bool visit(ImportDirective const&) { return true; } - virtual bool visit(ContractDefinition const&) { return true; } - virtual bool visit(StructDefinition const&) { return true; } - virtual bool visit(ParameterList const&) { return true; } - virtual bool visit(FunctionDefinition const&) { return true; } - virtual bool visit(VariableDeclaration const&) { return true; } - virtual bool visit(TypeName const&) { return true; } - virtual bool visit(ElementaryTypeName const&) { return true; } - virtual bool visit(UserDefinedTypeName const&) { return true; } - virtual bool visit(Mapping const&) { return true; } - virtual bool visit(Statement const&) { return true; } - virtual bool visit(Block const&) { return true; } - virtual bool visit(IfStatement const&) { return true; } - virtual bool visit(BreakableStatement const&) { return true; } - virtual bool visit(WhileStatement const&) { return true; } - virtual bool visit(Continue const&) { return true; } - virtual bool visit(Break const&) { return true; } - virtual bool visit(Return const&) { return true; } - virtual bool visit(VariableDefinition const&) { return true; } - virtual bool visit(ExpressionStatement const&) { return true; } - virtual bool visit(Expression const&) { return true; } - virtual bool visit(Assignment const&) { return true; } - virtual bool visit(UnaryOperation const&) { return true; } - virtual bool visit(BinaryOperation const&) { return true; } - virtual bool visit(FunctionCall const&) { return true; } - virtual bool visit(MemberAccess const&) { return true; } - virtual bool visit(IndexAccess const&) { return true; } - virtual bool visit(PrimaryExpression const&) { return true; } - virtual bool visit(Identifier const&) { return true; } - virtual bool visit(ElementaryTypeNameExpression const&) { return true; } - virtual bool visit(Literal const&) { return true; } - virtual void endVisit(ASTNode&) { } virtual void endVisit(SourceUnit&) { } virtual void endVisit(ImportDirective&) { } @@ -142,6 +108,44 @@ public: virtual void endVisit(Identifier&) { } virtual void endVisit(ElementaryTypeNameExpression&) { } virtual void endVisit(Literal&) { } +}; + +class ASTConstVisitor +{ +public: + virtual bool visit(ASTNode const&) { return true; } + virtual bool visit(SourceUnit const&) { return true; } + virtual bool visit(ImportDirective const&) { return true; } + virtual bool visit(ContractDefinition const&) { return true; } + virtual bool visit(StructDefinition const&) { return true; } + virtual bool visit(ParameterList const&) { return true; } + virtual bool visit(FunctionDefinition const&) { return true; } + virtual bool visit(VariableDeclaration const&) { return true; } + virtual bool visit(TypeName const&) { return true; } + virtual bool visit(ElementaryTypeName const&) { return true; } + virtual bool visit(UserDefinedTypeName const&) { return true; } + virtual bool visit(Mapping const&) { return true; } + virtual bool visit(Statement const&) { return true; } + virtual bool visit(Block const&) { return true; } + virtual bool visit(IfStatement const&) { return true; } + virtual bool visit(BreakableStatement const&) { return true; } + virtual bool visit(WhileStatement const&) { return true; } + virtual bool visit(Continue const&) { return true; } + virtual bool visit(Break const&) { return true; } + virtual bool visit(Return const&) { return true; } + virtual bool visit(VariableDefinition const&) { return true; } + virtual bool visit(ExpressionStatement const&) { return true; } + virtual bool visit(Expression const&) { return true; } + virtual bool visit(Assignment const&) { return true; } + virtual bool visit(UnaryOperation const&) { return true; } + virtual bool visit(BinaryOperation const&) { return true; } + virtual bool visit(FunctionCall const&) { return true; } + virtual bool visit(MemberAccess const&) { return true; } + virtual bool visit(IndexAccess const&) { return true; } + virtual bool visit(PrimaryExpression const&) { return true; } + virtual bool visit(Identifier const&) { return true; } + virtual bool visit(ElementaryTypeNameExpression const&) { return true; } + virtual bool visit(Literal const&) { return true; } virtual void endVisit(ASTNode const&) { } virtual void endVisit(SourceUnit const&) { } diff --git a/Compiler.cpp b/Compiler.cpp index 8c5d26f1..601a679a 100644 --- a/Compiler.cpp +++ b/Compiler.cpp @@ -292,7 +292,7 @@ bool Compiler::visit(Break const&) bool Compiler::visit(Return const& _return) { //@todo modifications are needed to make this work with functions returning multiple values - if (Expression* expression = _return.getExpression()) + if (Expression const* expression = _return.getExpression()) { ExpressionCompiler::compileExpression(m_context, *expression); VariableDeclaration const& firstVariable = *_return.getFunctionReturnParameters().getParameters().front(); @@ -307,7 +307,7 @@ bool Compiler::visit(Return const& _return) bool Compiler::visit(VariableDefinition const& _variableDefinition) { - if (Expression* expression = _variableDefinition.getExpression()) + if (Expression const* expression = _variableDefinition.getExpression()) { ExpressionCompiler::compileExpression(m_context, *expression); ExpressionCompiler::appendTypeConversion(m_context, @@ -322,7 +322,7 @@ bool Compiler::visit(VariableDefinition const& _variableDefinition) bool Compiler::visit(ExpressionStatement const& _expressionStatement) { - Expression& expression = _expressionStatement.getExpression(); + Expression const& expression = _expressionStatement.getExpression(); ExpressionCompiler::compileExpression(m_context, expression); // Type::Category category = expression.getType()->getCategory(); for (unsigned i = 0; i < expression.getType()->getSizeOnStack(); ++i) @@ -27,7 +27,7 @@ namespace dev { namespace solidity { -class Compiler: private ASTVisitor +class Compiler: private ASTConstVisitor { public: Compiler(): m_returnTag(m_context.newTag()) {} diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index 4cf962fa..ac972935 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -181,7 +181,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) else { FunctionType const& function = dynamic_cast<FunctionType const&>(*_functionCall.getExpression().getType()); - std::vector<ASTPointer<Expression>> const& arguments = _functionCall.getArguments(); + std::vector<ASTPointer<Expression const>> arguments = _functionCall.getArguments(); if (asserts(arguments.size() == function.getParameterTypes().size())) BOOST_THROW_EXCEPTION(InternalCompilerError()); diff --git a/ExpressionCompiler.h b/ExpressionCompiler.h index 5d4eec37..e93d3280 100644 --- a/ExpressionCompiler.h +++ b/ExpressionCompiler.h @@ -40,7 +40,7 @@ class IntegerType; // forward * of EVM instructions. It needs a compiler context that is the same for the whole compilation * unit. */ -class ExpressionCompiler: private ASTVisitor +class ExpressionCompiler: private ASTConstVisitor { public: /// Compile the given @a _expression into the @a _context. @@ -32,7 +32,7 @@ namespace dev namespace solidity { -shared_ptr<Type> Type::fromElementaryTypeName(Token::Value _typeToken) +shared_ptr<Type const> Type::fromElementaryTypeName(Token::Value _typeToken) { if (asserts(Token::isElementaryTypeName(_typeToken))) BOOST_THROW_EXCEPTION(InternalCompilerError()); @@ -44,33 +44,33 @@ shared_ptr<Type> Type::fromElementaryTypeName(Token::Value _typeToken) if (bytes == 0) bytes = 32; int modifier = offset / 33; - return make_shared<IntegerType>(bytes * 8, + return make_shared<IntegerType const>(bytes * 8, modifier == 0 ? IntegerType::Modifier::SIGNED : modifier == 1 ? IntegerType::Modifier::UNSIGNED : IntegerType::Modifier::HASH); } else if (_typeToken == Token::ADDRESS) - return make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS); + return make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS); else if (_typeToken == Token::BOOL) - return make_shared<BoolType>(); + return make_shared<BoolType const>(); else BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unable to convert elementary typename " + std::string(Token::toString(_typeToken)) + " to type.")); } -shared_ptr<Type> Type::fromUserDefinedTypeName(UserDefinedTypeName const& _typeName) +shared_ptr<Type const> Type::fromUserDefinedTypeName(UserDefinedTypeName const& _typeName) { Declaration const* declaration = _typeName.getReferencedDeclaration(); if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration)) - return make_shared<StructType>(*structDef); + return make_shared<StructType const>(*structDef); else if (FunctionDefinition const* function = dynamic_cast<FunctionDefinition const*>(declaration)) - return make_shared<FunctionType>(*function); + return make_shared<FunctionType const>(*function); else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration)) - return make_shared<ContractType>(*contract); - return shared_ptr<Type>(); + return make_shared<ContractType const>(*contract); + return shared_ptr<Type const>(); } -shared_ptr<Type> Type::fromMapping(Mapping const& _typeName) +shared_ptr<Type const> Type::fromMapping(Mapping const& _typeName) { shared_ptr<Type const> keyType = _typeName.getKeyType().toType(); if (!keyType) @@ -78,28 +78,28 @@ shared_ptr<Type> Type::fromMapping(Mapping const& _typeName) shared_ptr<Type const> valueType = _typeName.getValueType().toType(); if (!valueType) BOOST_THROW_EXCEPTION(_typeName.getValueType().createTypeError("Invalid type name")); - return make_shared<MappingType>(keyType, valueType); + return make_shared<MappingType const>(keyType, valueType); } -shared_ptr<Type> Type::forLiteral(Literal const& _literal) +shared_ptr<Type const> Type::forLiteral(Literal const& _literal) { switch (_literal.getToken()) { case Token::TRUE_LITERAL: case Token::FALSE_LITERAL: - return make_shared<BoolType>(); + return make_shared<BoolType const>(); case Token::NUMBER: return IntegerType::smallestTypeForLiteral(_literal.getValue()); case Token::STRING_LITERAL: - return shared_ptr<Type>(); // @todo add string literals + return shared_ptr<Type const>(); // @todo add string literals default: - return shared_ptr<Type>(); + return shared_ptr<Type const>(); } } const MemberList Type::EmptyMemberList = MemberList(); -shared_ptr<IntegerType> IntegerType::smallestTypeForLiteral(string const& _literal) +shared_ptr<IntegerType const> IntegerType::smallestTypeForLiteral(string const& _literal) { bigint value(_literal); bool isSigned = value < 0 || (!_literal.empty() && _literal.front() == '-'); @@ -108,8 +108,8 @@ shared_ptr<IntegerType> IntegerType::smallestTypeForLiteral(string const& _liter value = ((-value) - 1) << 1; unsigned bytes = max(bytesRequired(value), 1u); if (bytes > 32) - return shared_ptr<IntegerType>(); - return make_shared<IntegerType>(bytes * 8, isSigned ? Modifier::SIGNED : Modifier::UNSIGNED); + return shared_ptr<IntegerType const>(); + return make_shared<IntegerType const>(bytes * 8, isSigned ? Modifier::SIGNED : Modifier::UNSIGNED); } IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier): @@ -80,15 +80,15 @@ public: ///@{ ///@name Factory functions /// Factory functions that convert an AST @ref TypeName to a Type. - static std::shared_ptr<Type> fromElementaryTypeName(Token::Value _typeToken); - static std::shared_ptr<Type> fromUserDefinedTypeName(UserDefinedTypeName const& _typeName); - static std::shared_ptr<Type> fromMapping(Mapping const& _typeName); - static std::shared_ptr<Type> fromFunction(FunctionDefinition const& _function); + static std::shared_ptr<Type const> fromElementaryTypeName(Token::Value _typeToken); + static std::shared_ptr<Type const> fromUserDefinedTypeName(UserDefinedTypeName const& _typeName); + static std::shared_ptr<Type const> fromMapping(Mapping const& _typeName); + static std::shared_ptr<Type const> fromFunction(FunctionDefinition const& _function); /// @} /// Auto-detect the proper type for a literal. @returns an empty pointer if the literal does /// not fit any type. - static std::shared_ptr<Type> forLiteral(Literal const& _literal); + static std::shared_ptr<Type const> forLiteral(Literal const& _literal); virtual Category getCategory() const = 0; virtual bool isImplicitlyConvertibleTo(Type const& _other) const { return *this == _other; } @@ -148,7 +148,7 @@ public: /// @returns the smallest integer type for the given literal or an empty pointer /// if no type fits. - static std::shared_ptr<IntegerType> smallestTypeForLiteral(std::string const& _literal); + static std::shared_ptr<IntegerType const> smallestTypeForLiteral(std::string const& _literal); explicit IntegerType(int _bits, Modifier _modifier = Modifier::UNSIGNED); |