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
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-09-16 14:56:30 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Type analyzer and checker.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/EVMVersion.h>
|
2018-02-22 15:16:27 +00:00
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/ASTAnnotations.h>
|
|
|
|
#include <libsolidity/ast/ASTForward.h>
|
|
|
|
#include <libsolidity/ast/ASTVisitor.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
#include <libsolidity/ast/Types.h>
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::langutil
|
2018-11-14 16:11:55 +00:00
|
|
|
{
|
|
|
|
class ErrorReporter;
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend
|
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.
|
2019-02-25 14:29:57 +00:00
|
|
|
TypeChecker(langutil::EVMVersion _evmVersion, langutil::ErrorReporter& _errorReporter):
|
2018-02-22 15:16:27 +00:00
|
|
|
m_evmVersion(_evmVersion),
|
|
|
|
m_errorReporter(_errorReporter)
|
|
|
|
{}
|
2015-10-14 18:37:41 +00:00
|
|
|
|
2020-06-11 15:17:07 +00:00
|
|
|
/// Performs type checking on the given source 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
|
2020-06-11 15:17:07 +00:00
|
|
|
bool checkTypeRequirements(SourceUnit const& _source);
|
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;
|
|
|
|
|
2019-03-04 13:33:46 +00:00
|
|
|
static bool typeSupportedByOldABIEncoder(Type const& _type, bool _isLibraryCall);
|
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
private:
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2018-11-16 01:09:04 +00:00
|
|
|
bool visit(ContractDefinition const& _contract) override;
|
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.
|
2018-10-18 21:53:59 +00:00
|
|
|
TypePointers typeCheckABIDecodeAndRetrieveReturnType(
|
|
|
|
FunctionCall const& _functionCall,
|
|
|
|
bool _abiEncoderV2
|
|
|
|
);
|
|
|
|
|
2019-01-10 15:28:39 +00:00
|
|
|
TypePointers typeCheckMetaTypeFunctionAndRetrieveReturnType(FunctionCall const& _functionCall);
|
|
|
|
|
2018-10-18 21:53:59 +00:00
|
|
|
/// Performs type checks and determines result types for type conversion FunctionCall nodes.
|
|
|
|
TypePointer typeCheckTypeConversionAndRetrieveReturnType(
|
|
|
|
FunctionCall const& _functionCall
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Performs type checks on function call and struct ctor FunctionCall nodes (except for kind ABIDecode).
|
|
|
|
void typeCheckFunctionCall(
|
|
|
|
FunctionCall const& _functionCall,
|
|
|
|
FunctionTypePointer _functionType
|
|
|
|
);
|
|
|
|
|
2019-09-09 16:22:02 +00:00
|
|
|
void typeCheckFallbackFunction(FunctionDefinition const& _function);
|
|
|
|
void typeCheckReceiveFunction(FunctionDefinition const& _function);
|
|
|
|
void typeCheckConstructor(FunctionDefinition const& _function);
|
|
|
|
|
2018-10-18 21:53:59 +00:00
|
|
|
/// Performs general number and type checks of arguments against function call and struct ctor FunctionCall node parameters.
|
|
|
|
void typeCheckFunctionGeneralChecks(
|
|
|
|
FunctionCall const& _functionCall,
|
|
|
|
FunctionTypePointer _functionType
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Performs general checks and checks specific to ABI encode functions
|
|
|
|
void typeCheckABIEncodeFunctions(
|
|
|
|
FunctionCall const& _functionCall,
|
|
|
|
FunctionTypePointer _functionType
|
|
|
|
);
|
2018-06-30 16:09:13 +00:00
|
|
|
|
2018-11-16 01:09:04 +00:00
|
|
|
void endVisit(InheritanceSpecifier const& _inheritance) override;
|
2020-04-15 10:42:15 +00:00
|
|
|
void endVisit(ModifierDefinition const& _modifier) override;
|
2018-11-16 01:09:04 +00:00
|
|
|
bool visit(FunctionDefinition const& _function) override;
|
|
|
|
bool visit(VariableDeclaration const& _variable) override;
|
2015-09-16 14:56:30 +00:00
|
|
|
/// 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);
|
2018-11-16 01:09:04 +00:00
|
|
|
bool visit(EventDefinition const& _eventDef) override;
|
|
|
|
void endVisit(FunctionTypeName const& _funType) override;
|
|
|
|
bool visit(InlineAssembly const& _inlineAssembly) override;
|
|
|
|
bool visit(IfStatement const& _ifStatement) override;
|
2019-09-05 18:02:34 +00:00
|
|
|
void endVisit(TryStatement const& _tryStatement) override;
|
2018-11-16 01:09:04 +00:00
|
|
|
bool visit(WhileStatement const& _whileStatement) override;
|
|
|
|
bool visit(ForStatement const& _forStatement) override;
|
|
|
|
void endVisit(Return const& _return) override;
|
|
|
|
void endVisit(EmitStatement const& _emit) override;
|
|
|
|
bool visit(VariableDeclarationStatement const& _variable) override;
|
|
|
|
void endVisit(ExpressionStatement const& _statement) override;
|
|
|
|
bool visit(Conditional const& _conditional) override;
|
|
|
|
bool visit(Assignment const& _assignment) override;
|
|
|
|
bool visit(TupleExpression const& _tuple) override;
|
|
|
|
void endVisit(BinaryOperation const& _operation) override;
|
|
|
|
bool visit(UnaryOperation const& _operation) override;
|
|
|
|
bool visit(FunctionCall const& _functionCall) override;
|
2020-01-22 14:42:50 +00:00
|
|
|
bool visit(FunctionCallOptions const& _functionCallOptions) override;
|
2018-11-16 01:09:04 +00:00
|
|
|
void endVisit(NewExpression const& _newExpression) override;
|
|
|
|
bool visit(MemberAccess const& _memberAccess) override;
|
|
|
|
bool visit(IndexAccess const& _indexAccess) override;
|
2019-09-03 16:30:00 +00:00
|
|
|
bool visit(IndexRangeAccess const& _indexRangeAccess) override;
|
2018-11-16 01:09:04 +00:00
|
|
|
bool visit(Identifier const& _identifier) override;
|
|
|
|
void endVisit(ElementaryTypeNameExpression const& _expr) override;
|
|
|
|
void endVisit(Literal const& _literal) override;
|
2020-08-27 12:39:52 +00:00
|
|
|
void endVisit(UsingForDirective const& _usingForDirective) override;
|
2015-09-16 14:56:30 +00:00
|
|
|
|
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
|
|
|
|
2020-04-07 17:31:48 +00:00
|
|
|
std::vector<Declaration const*> cleanOverloadedDeclarations(
|
|
|
|
Identifier const& _reference,
|
|
|
|
std::vector<Declaration const*> const& _candidates
|
|
|
|
);
|
|
|
|
|
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.
|
2020-03-10 17:15:50 +00:00
|
|
|
void requireLValue(Expression const& _expression, bool _ordinaryAssignment);
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2020-10-22 17:19:14 +00:00
|
|
|
bool useABICoderV2() const;
|
2020-06-11 22:16:24 +00:00
|
|
|
|
2020-05-04 16:38:00 +00:00
|
|
|
/// @returns the current scope that can have function or type definitions.
|
|
|
|
/// This is either a contract or a source unit.
|
|
|
|
ASTNode const* currentDefinitionScope() const
|
|
|
|
{
|
|
|
|
if (m_currentContract)
|
|
|
|
return m_currentContract;
|
|
|
|
else
|
|
|
|
return m_currentSourceUnit;
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:16:24 +00:00
|
|
|
SourceUnit const* m_currentSourceUnit = nullptr;
|
2020-06-09 16:17:58 +00:00
|
|
|
ContractDefinition const* m_currentContract = nullptr;
|
2015-11-19 17:02:04 +00:00
|
|
|
|
2019-02-25 14:29:57 +00:00
|
|
|
langutil::EVMVersion m_evmVersion;
|
2018-02-22 15:16:27 +00:00
|
|
|
|
2018-11-14 16:11:55 +00:00
|
|
|
langutil::ErrorReporter& m_errorReporter;
|
2015-09-16 14:56:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|