diff options
author | chriseth <c@ethdev.com> | 2015-09-16 22:56:30 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-09-22 02:03:05 +0800 |
commit | 34a81fd60e0218ef93813ffce22fb6d0ed4955e2 (patch) | |
tree | e15e3f2c224688c43859d4c4588743c8fd07adc1 /libsolidity/TypeChecker.h | |
parent | 352c196eb37744b8054909a9801d55c672d0eb1c (diff) | |
download | dexon-solidity-34a81fd60e0218ef93813ffce22fb6d0ed4955e2.tar dexon-solidity-34a81fd60e0218ef93813ffce22fb6d0ed4955e2.tar.gz dexon-solidity-34a81fd60e0218ef93813ffce22fb6d0ed4955e2.tar.bz2 dexon-solidity-34a81fd60e0218ef93813ffce22fb6d0ed4955e2.tar.lz dexon-solidity-34a81fd60e0218ef93813ffce22fb6d0ed4955e2.tar.xz dexon-solidity-34a81fd60e0218ef93813ffce22fb6d0ed4955e2.tar.zst dexon-solidity-34a81fd60e0218ef93813ffce22fb6d0ed4955e2.zip |
Refactoring: Check types outside of AST and recover from some errors.
Diffstat (limited to 'libsolidity/TypeChecker.h')
-rw-r--r-- | libsolidity/TypeChecker.h | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/libsolidity/TypeChecker.h b/libsolidity/TypeChecker.h new file mode 100644 index 00000000..90e2a01b --- /dev/null +++ b/libsolidity/TypeChecker.h @@ -0,0 +1,114 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. +*/ +/** + * @author Christian <c@ethdev.com> + * @date 2015 + * Type analyzer and checker. + */ + +#pragma once + +#include <libsolidity/TypeChecker.h> +#include <libsolidity/Types.h> +#include <libsolidity/ASTAnnotations.h> +#include <libsolidity/ASTForward.h> +#include <libsolidity/ASTVisitor.h> + +namespace dev +{ +namespace solidity +{ + + +/** + * The module that performs type analysis on the AST, checks the applicability of operations on + * those types and stores errors for invalid operations. + * Provides a way to retrieve the type of an AST node. + */ +class TypeChecker: private ASTConstVisitor +{ +public: + /// Performs type checking on the given contract and all of its sub-nodes. + /// @returns true iff all checks passed. + bool checkTypeRequirements(ContractDefinition const& _contract); + + /// @returns the list of errors found during type checking. + std::vector<std::shared_ptr<Exception const>> const& errors() const { return m_errors; } + + /// @returns the type of an expression and asserts that it is present. + TypePointer const& type(Expression const& _expression) const; + /// @returns the type of the given variable and throws if the type is not present + /// (this can happen for variables with non-explicit types before their types are resolved) + TypePointer const& type(VariableDeclaration const& _variable) const; + + /// Adds a new error to the list of errors. + void typeError(ASTNode const& _node, std::string const& _description); + /// Adds a new error to the list of errors and throws to abort type checking. + void fatalTypeError(ASTNode const& _node, std::string const& _description); + +private: + virtual bool visit(ContractDefinition const& _contract) override; + /// Checks that two functions defined in this contract with the same name have different + /// arguments and that there is at most one constructor. + void checkContractDuplicateFunctions(ContractDefinition const& _contract); + void checkContractIllegalOverrides(ContractDefinition const& _contract); + void checkContractAbstractFunctions(ContractDefinition const& _contract); + void checkContractAbstractConstructors(ContractDefinition const& _contract); + /// Checks that different functions with external visibility end up having different + /// external argument types (i.e. different signature). + void checkContractExternalTypeClashes(ContractDefinition const& _contract); + /// Checks that all requirements for a library are fulfilled if this is a library. + void checkLibraryRequirements(ContractDefinition const& _contract); + + virtual void endVisit(InheritanceSpecifier const& _inheritance) override; + virtual bool visit(StructDefinition const& _struct) override; + virtual bool visit(FunctionDefinition const& _function) override; + virtual bool visit(VariableDeclaration const& _variable) override; + /// We need to do this manually because we want to pass the bases of the current contract in + /// case this is a base constructor call. + void visitManually(ModifierInvocation const& _modifier, std::vector<ContractDefinition const*> const& _bases); + virtual bool visit(EventDefinition const& _eventDef) override; + virtual bool visit(IfStatement const& _ifStatement) override; + virtual bool visit(WhileStatement const& _whileStatement) override; + virtual bool visit(ForStatement const& _forStatement) override; + virtual void endVisit(Return const& _return) override; + virtual void endVisit(ExpressionStatement const& _statement) override; + virtual bool visit(Assignment const& _assignment) override; + virtual void endVisit(BinaryOperation const& _operation) override; + virtual bool visit(UnaryOperation const& _operation) override; + virtual bool visit(FunctionCall const& _functionCall) override; + virtual void endVisit(NewExpression const& _newExpression) override; + virtual bool visit(MemberAccess const& _memberAccess) override; + virtual bool visit(IndexAccess const& _indexAccess) override; + virtual bool visit(Identifier const& _identifier) override; + virtual void endVisit(ElementaryTypeNameExpression const& _expr) override; + virtual void endVisit(Literal const& _literal) override; + + /// @returns the referenced declaration and throws on error. + Declaration const& dereference(Identifier const& _identifier); + + /// Runs type checks on @a _expression to infer its type and then checks that it is implicitly + /// convertible to @a _expectedType. + void expectType(Expression const& _expression, Type const& _expectedType); + /// Runs type checks on @a _expression to infer its type and then checks that it is an LValue. + void requireLValue(Expression const& _expression); + + std::vector<std::shared_ptr<Exception const>> m_errors; +}; + +} +} |