2015-09-16 14:56:30 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-09-16 14:56:30 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-09-16 14:56:30 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-09-16 14:56:30 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Type analyzer and checker.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-02-22 15:16:27 +00:00
|
|
|
#include <libsolidity/interface/EVMVersion.h>
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/Types.h>
|
|
|
|
#include <libsolidity/ast/ASTAnnotations.h>
|
|
|
|
#include <libsolidity/ast/ASTForward.h>
|
|
|
|
#include <libsolidity/ast/ASTVisitor.h>
|
2015-09-16 14:56:30 +00:00
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
|
2017-05-11 13:26:35 +00:00
|
|
|
class ErrorReporter;
|
2015-09-16 14:56:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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:
|
2017-11-15 15:28:41 +00:00
|
|
|
/// @param _errorReporter provides the error logging functionality.
|
2018-02-22 15:16:27 +00:00
|
|
|
TypeChecker(EVMVersion _evmVersion, ErrorReporter& _errorReporter):
|
|
|
|
m_evmVersion(_evmVersion),
|
|
|
|
m_errorReporter(_errorReporter)
|
|
|
|
{}
|
2015-10-14 18:37:41 +00:00
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
/// Performs type checking on the given contract and all of its sub-nodes.
|
2015-09-29 16:22:02 +00:00
|
|
|
/// @returns true iff all checks passed. Note even if all checks passed, errors() can still contain warnings
|
2017-01-27 22:29:03 +00:00
|
|
|
bool checkTypeRequirements(ASTNode const& _contract);
|
2015-09-16 14:56:30 +00:00
|
|
|
|
|
|
|
/// @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;
|
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
private:
|
2015-09-16 14:56:30 +00:00
|
|
|
|
|
|
|
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);
|
2017-09-29 21:45:17 +00:00
|
|
|
void checkContractDuplicateEvents(ContractDefinition const& _contract);
|
2015-09-16 14:56:30 +00:00
|
|
|
void checkContractIllegalOverrides(ContractDefinition const& _contract);
|
2018-07-10 07:18:59 +00:00
|
|
|
/// Reports a type error with an appropriate message if overridden function signature differs.
|
2017-08-29 15:07:32 +00:00
|
|
|
/// Also stores the direct super function in the AST annotations.
|
2017-07-19 17:32:26 +00:00
|
|
|
void checkFunctionOverride(FunctionDefinition const& function, FunctionDefinition const& super);
|
2017-07-19 22:22:36 +00:00
|
|
|
void overrideError(FunctionDefinition const& function, FunctionDefinition const& super, std::string message);
|
2015-09-16 14:56:30 +00:00
|
|
|
void checkContractAbstractFunctions(ContractDefinition const& _contract);
|
2018-04-05 14:25:20 +00:00
|
|
|
void checkContractBaseConstructorArguments(ContractDefinition const& _contract);
|
|
|
|
void annotateBaseConstructorArguments(
|
|
|
|
ContractDefinition const& _currentContract,
|
|
|
|
FunctionDefinition const* _baseConstructor,
|
|
|
|
ASTNode const* _argumentNode
|
|
|
|
);
|
2015-09-16 14:56:30 +00:00
|
|
|
/// 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);
|
2017-06-21 17:32:56 +00:00
|
|
|
/// Checks (and warns) if a tuple assignment might cause unexpected overwrites in storage.
|
|
|
|
/// Should only be called if the left hand side is tuple-typed.
|
|
|
|
void checkDoubleStorageAssignment(Assignment const& _assignment);
|
2018-08-03 12:32:37 +00:00
|
|
|
// Checks whether the expression @arg _expression can be assigned from type @arg _type
|
|
|
|
// and reports an error, if not.
|
|
|
|
void checkExpressionAssignment(Type const& _type, Expression const& _expression);
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2018-06-30 16:09:13 +00:00
|
|
|
/// Performs type checks for ``abi.decode(bytes memory, (...))`` and returns the
|
2018-09-04 14:24:21 +00:00
|
|
|
/// vector of return types (which is basically the second argument) if successful. It returns
|
|
|
|
/// the empty vector on error.
|
|
|
|
TypePointers typeCheckABIDecodeAndRetrieveReturnType(FunctionCall const& _functionCall, bool _abiEncoderV2);
|
2018-06-30 16:09:13 +00:00
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
virtual void endVisit(InheritanceSpecifier const& _inheritance) override;
|
2015-11-22 19:39:10 +00:00
|
|
|
virtual void endVisit(UsingForDirective const& _usingFor) override;
|
2015-09-16 14:56:30 +00:00
|
|
|
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;
|
2016-11-11 11:07:30 +00:00
|
|
|
virtual void endVisit(FunctionTypeName const& _funType) override;
|
2016-03-01 21:56:39 +00:00
|
|
|
virtual bool visit(InlineAssembly const& _inlineAssembly) override;
|
2015-09-16 14:56:30 +00:00
|
|
|
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;
|
2018-02-16 16:32:41 +00:00
|
|
|
virtual bool visit(EmitStatement const&) override { m_insideEmitStatement = true; return true; }
|
2018-02-16 15:55:21 +00:00
|
|
|
virtual void endVisit(EmitStatement const& _emit) override;
|
2015-10-08 16:01:12 +00:00
|
|
|
virtual bool visit(VariableDeclarationStatement const& _variable) override;
|
2015-09-16 14:56:30 +00:00
|
|
|
virtual void endVisit(ExpressionStatement const& _statement) override;
|
2016-01-07 09:01:46 +00:00
|
|
|
virtual bool visit(Conditional const& _conditional) override;
|
2015-09-16 14:56:30 +00:00
|
|
|
virtual bool visit(Assignment const& _assignment) override;
|
2015-10-12 21:02:35 +00:00
|
|
|
virtual bool visit(TupleExpression const& _tuple) override;
|
2015-09-16 14:56:30 +00:00
|
|
|
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;
|
|
|
|
|
2017-09-29 22:45:56 +00:00
|
|
|
template <class T>
|
|
|
|
void findDuplicateDefinitions(std::map<std::string, std::vector<T>> const& _definitions, std::string _message);
|
|
|
|
|
2015-10-07 13:57:17 +00:00
|
|
|
bool contractDependenciesAreCyclic(
|
|
|
|
ContractDefinition const& _contract,
|
|
|
|
std::set<ContractDefinition const*> const& _seenContracts = std::set<ContractDefinition const*>()
|
|
|
|
) const;
|
|
|
|
|
2015-09-16 14:56:30 +00:00
|
|
|
/// @returns the referenced declaration and throws on error.
|
2015-11-24 15:34:02 +00:00
|
|
|
Declaration const& dereference(Identifier const& _identifier) const;
|
2015-11-16 23:06:57 +00:00
|
|
|
/// @returns the referenced declaration and throws on error.
|
2015-11-24 15:34:02 +00:00
|
|
|
Declaration const& dereference(UserDefinedTypeName const& _typeName) const;
|
2015-09-16 14:56:30 +00:00
|
|
|
|
|
|
|
/// Runs type checks on @a _expression to infer its type and then checks that it is implicitly
|
|
|
|
/// convertible to @a _expectedType.
|
2018-10-10 17:33:45 +00:00
|
|
|
bool expectType(Expression const& _expression, Type const& _expectedType);
|
2015-09-16 14:56:30 +00:00
|
|
|
/// Runs type checks on @a _expression to infer its type and then checks that it is an LValue.
|
|
|
|
void requireLValue(Expression const& _expression);
|
|
|
|
|
2015-11-19 17:02:04 +00:00
|
|
|
ContractDefinition const* m_scope = nullptr;
|
|
|
|
|
2018-02-22 15:16:27 +00:00
|
|
|
EVMVersion m_evmVersion;
|
|
|
|
|
2018-02-16 16:32:41 +00:00
|
|
|
/// Flag indicating whether we are currently inside an EmitStatement.
|
|
|
|
bool m_insideEmitStatement = false;
|
|
|
|
|
2018-08-10 09:51:41 +00:00
|
|
|
/// Flag indicating whether we are currently inside a StructDefinition.
|
|
|
|
bool m_insideStruct = false;
|
|
|
|
|
2017-05-11 13:26:35 +00:00
|
|
|
ErrorReporter& m_errorReporter;
|
2015-09-16 14:56:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|