2019-04-15 13:33:39 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity 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.
|
|
|
|
|
|
|
|
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libsolidity/ast/Types.h>
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2019-10-28 10:39:30 +00:00
|
|
|
#include <optional>
|
2019-04-15 13:33:39 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API for accessing the Solidity Type System.
|
|
|
|
*
|
|
|
|
* This is the Solidity Compiler's type provider. Use it to request for types. The caller does
|
|
|
|
* <b>not</b> own the types.
|
|
|
|
*
|
|
|
|
* It is not recommended to explicitly instantiate types unless you really know what and why
|
|
|
|
* you are doing it.
|
|
|
|
*/
|
|
|
|
class TypeProvider
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TypeProvider() = default;
|
|
|
|
TypeProvider(TypeProvider&&) = default;
|
|
|
|
TypeProvider(TypeProvider const&) = delete;
|
|
|
|
TypeProvider& operator=(TypeProvider&&) = default;
|
|
|
|
TypeProvider& operator=(TypeProvider const&) = delete;
|
|
|
|
~TypeProvider() = default;
|
|
|
|
|
|
|
|
/// Resets state of this TypeProvider to initial state, wiping all mutable types.
|
|
|
|
/// This invalidates all dangling pointers to types provided by this TypeProvider.
|
|
|
|
static void reset();
|
|
|
|
|
|
|
|
/// @name Factory functions
|
|
|
|
/// Factory functions that convert an AST @ref TypeName to a Type.
|
2019-10-28 13:07:53 +00:00
|
|
|
static Type const* fromElementaryTypeName(ElementaryTypeNameToken const& _type, std::optional<StateMutability> _stateMutability = {});
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// Converts a given elementary type name with optional data location
|
|
|
|
/// suffix " storage", " calldata" or " memory" to a type pointer. If suffix not given, defaults to " storage".
|
2021-03-22 16:12:05 +00:00
|
|
|
static Type const* fromElementaryTypeName(std::string const& _name);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// @returns boolean type.
|
2019-04-17 11:40:50 +00:00
|
|
|
static BoolType const* boolean() noexcept { return &m_boolean; }
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static FixedBytesType const* byte() { return fixedBytes(1); }
|
|
|
|
static FixedBytesType const* fixedBytes(unsigned m) { return m_bytesM.at(m - 1).get(); }
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static ArrayType const* bytesStorage();
|
|
|
|
static ArrayType const* bytesMemory();
|
2019-09-03 16:30:00 +00:00
|
|
|
static ArrayType const* bytesCalldata();
|
2019-04-17 11:40:50 +00:00
|
|
|
static ArrayType const* stringStorage();
|
|
|
|
static ArrayType const* stringMemory();
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// Constructor for a byte array ("bytes") and string.
|
2019-04-17 11:40:50 +00:00
|
|
|
static ArrayType const* array(DataLocation _location, bool _isString = false);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// Constructor for a dynamically sized array type ("type[]")
|
2019-04-17 11:40:50 +00:00
|
|
|
static ArrayType const* array(DataLocation _location, Type const* _baseType);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// Constructor for a fixed-size array type ("type[20]")
|
2019-04-17 11:40:50 +00:00
|
|
|
static ArrayType const* array(DataLocation _location, Type const* _baseType, u256 const& _length);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-09-03 16:30:00 +00:00
|
|
|
static ArraySliceType const* arraySlice(ArrayType const& _arrayType);
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static AddressType const* payableAddress() noexcept { return &m_payableAddress; }
|
|
|
|
static AddressType const* address() noexcept { return &m_address; }
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static IntegerType const* integer(unsigned _bits, IntegerType::Modifier _modifier)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
solAssert((_bits % 8) == 0, "");
|
|
|
|
if (_modifier == IntegerType::Modifier::Unsigned)
|
2019-04-16 16:11:43 +00:00
|
|
|
return m_uintM.at(_bits / 8 - 1).get();
|
2019-04-15 13:33:39 +00:00
|
|
|
else
|
2019-04-16 16:11:43 +00:00
|
|
|
return m_intM.at(_bits / 8 - 1).get();
|
2019-04-15 13:33:39 +00:00
|
|
|
}
|
2019-04-17 11:40:50 +00:00
|
|
|
static IntegerType const* uint(unsigned _bits) { return integer(_bits, IntegerType::Modifier::Unsigned); }
|
2019-04-15 16:10:43 +00:00
|
|
|
|
|
|
|
static IntegerType const* uint256() { return uint(256); }
|
2020-05-19 15:53:39 +00:00
|
|
|
static IntegerType const* int256() { return integer(256, IntegerType::Modifier::Signed); }
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static FixedPointType const* fixedPoint(unsigned m, unsigned n, FixedPointType::Modifier _modifier);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static StringLiteralType const* stringLiteral(std::string const& literal);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// @param members the member types the tuple type must contain. This is passed by value on purspose.
|
|
|
|
/// @returns a tuple type with the given members.
|
2019-04-17 11:40:50 +00:00
|
|
|
static TupleType const* tuple(std::vector<Type const*> members);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static TupleType const* emptyTuple() noexcept { return &m_emptyTuple; }
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
static ReferenceType const* withLocation(ReferenceType const* _type, DataLocation _location, bool _isPointer);
|
|
|
|
|
|
|
|
/// @returns a copy of @a _type having the same location as this (and is not a pointer type)
|
|
|
|
/// if _type is a reference type and an unmodified copy of _type otherwise.
|
|
|
|
/// This function is mostly useful to modify inner types appropriately.
|
2020-06-05 08:37:00 +00:00
|
|
|
static Type const* withLocationIfReference(DataLocation _location, Type const* _type, bool _isPointer = false)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
if (auto refType = dynamic_cast<ReferenceType const*>(_type))
|
2020-06-05 08:37:00 +00:00
|
|
|
return withLocation(refType, _location, _isPointer);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
|
2020-06-05 08:37:00 +00:00
|
|
|
static bool isReferenceWithLocation(Type const* _type, DataLocation _location)
|
|
|
|
{
|
|
|
|
if (auto const* refType = dynamic_cast<ReferenceType const*>(_type))
|
|
|
|
if (refType->location() == _location)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:42:37 +00:00
|
|
|
/// @returns the internally-facing or externally-facing type of a function or the type of a function declaration.
|
|
|
|
static FunctionType const* function(FunctionDefinition const& _function, FunctionType::Kind _kind = FunctionType::Kind::Declaration);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// @returns the accessor function type of a state variable.
|
2019-04-17 11:40:50 +00:00
|
|
|
static FunctionType const* function(VariableDeclaration const& _varDecl);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// @returns the function type of an event.
|
2019-04-17 11:40:50 +00:00
|
|
|
static FunctionType const* function(EventDefinition const& _event);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2021-01-28 11:56:22 +00:00
|
|
|
static FunctionType const* function(ErrorDefinition const& _error);
|
|
|
|
|
2019-04-15 13:33:39 +00:00
|
|
|
/// @returns the type of a function type name.
|
2019-04-17 11:40:50 +00:00
|
|
|
static FunctionType const* function(FunctionTypeName const& _typeName);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// @returns the function type to be used for a plain type (not derived from a declaration).
|
2019-04-17 11:40:50 +00:00
|
|
|
static FunctionType const* function(
|
2019-04-15 13:33:39 +00:00
|
|
|
strings const& _parameterTypes,
|
|
|
|
strings const& _returnParameterTypes,
|
|
|
|
FunctionType::Kind _kind = FunctionType::Kind::Internal,
|
2021-12-20 11:40:43 +00:00
|
|
|
StateMutability _stateMutability = StateMutability::NonPayable,
|
|
|
|
FunctionType::Options _options = {}
|
2019-04-15 13:33:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/// @returns a highly customized FunctionType, use with care.
|
2019-04-17 11:40:50 +00:00
|
|
|
static FunctionType const* function(
|
2019-04-15 13:33:39 +00:00
|
|
|
TypePointers const& _parameterTypes,
|
|
|
|
TypePointers const& _returnParameterTypes,
|
|
|
|
strings _parameterNames = strings{},
|
|
|
|
strings _returnParameterNames = strings{},
|
|
|
|
FunctionType::Kind _kind = FunctionType::Kind::Internal,
|
|
|
|
StateMutability _stateMutability = StateMutability::NonPayable,
|
|
|
|
Declaration const* _declaration = nullptr,
|
2021-12-20 11:40:43 +00:00
|
|
|
FunctionType::Options _options = {}
|
2019-04-15 13:33:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/// Auto-detect the proper type for a literal. @returns an empty pointer if the literal does
|
|
|
|
/// not fit any type.
|
2021-03-22 16:12:05 +00:00
|
|
|
static Type const* forLiteral(Literal const& _literal);
|
2019-04-17 11:40:50 +00:00
|
|
|
static RationalNumberType const* rationalNumber(Literal const& _literal);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static RationalNumberType const* rationalNumber(
|
2019-04-15 13:33:39 +00:00
|
|
|
rational const& _value,
|
|
|
|
Type const* _compatibleBytesType = nullptr
|
|
|
|
);
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static ContractType const* contract(ContractDefinition const& _contract, bool _isSuper = false);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static InaccessibleDynamicType const* inaccessibleDynamic() noexcept { return &m_inaccessibleDynamic; }
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
/// @returns the type of an enum instance for given definition, there is one distinct type per enum definition.
|
|
|
|
static EnumType const* enumType(EnumDefinition const& _enum);
|
|
|
|
|
|
|
|
/// @returns special type for imported modules. These mainly give access to their scope via members.
|
2019-04-17 11:40:50 +00:00
|
|
|
static ModuleType const* module(SourceUnit const& _source);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
static TypeType const* typeType(Type const* _actualType);
|
|
|
|
|
2019-04-15 15:28:32 +00:00
|
|
|
static StructType const* structType(StructDefinition const& _struct, DataLocation _location);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static ModifierType const* modifier(ModifierDefinition const& _modifierDef);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static MagicType const* magic(MagicType::Kind _kind);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static MagicType const* meta(Type const* _type);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static MappingType const* mapping(Type const* _keyType, Type const* _valueType);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
2021-08-12 15:06:38 +00:00
|
|
|
static UserDefinedValueType const* userDefinedValueType(UserDefinedValueTypeDefinition const& _definition);
|
|
|
|
|
2019-04-15 13:33:39 +00:00
|
|
|
private:
|
|
|
|
/// Global TypeProvider instance.
|
|
|
|
static TypeProvider& instance()
|
|
|
|
{
|
|
|
|
static TypeProvider _provider;
|
|
|
|
return _provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
static inline T const* createAndGet(Args&& ... _args);
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
static BoolType const m_boolean;
|
|
|
|
static InaccessibleDynamicType const m_inaccessibleDynamic;
|
2019-04-16 17:45:25 +00:00
|
|
|
|
|
|
|
/// These are lazy-initialized because they depend on `byte` being available.
|
2019-04-17 11:40:50 +00:00
|
|
|
static std::unique_ptr<ArrayType> m_bytesStorage;
|
|
|
|
static std::unique_ptr<ArrayType> m_bytesMemory;
|
2019-09-03 16:30:00 +00:00
|
|
|
static std::unique_ptr<ArrayType> m_bytesCalldata;
|
2019-04-17 11:40:50 +00:00
|
|
|
static std::unique_ptr<ArrayType> m_stringStorage;
|
|
|
|
static std::unique_ptr<ArrayType> m_stringMemory;
|
|
|
|
|
|
|
|
static TupleType const m_emptyTuple;
|
|
|
|
static AddressType const m_payableAddress;
|
|
|
|
static AddressType const m_address;
|
2019-04-16 16:11:43 +00:00
|
|
|
static std::array<std::unique_ptr<IntegerType>, 32> const m_intM;
|
|
|
|
static std::array<std::unique_ptr<IntegerType>, 32> const m_uintM;
|
|
|
|
static std::array<std::unique_ptr<FixedBytesType>, 32> const m_bytesM;
|
2019-04-17 11:40:50 +00:00
|
|
|
static std::array<std::unique_ptr<MagicType>, 4> const m_magics; ///< MagicType's except MetaType
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
std::map<std::pair<unsigned, unsigned>, std::unique_ptr<FixedPointType>> m_ufixedMxN{};
|
|
|
|
std::map<std::pair<unsigned, unsigned>, std::unique_ptr<FixedPointType>> m_fixedMxN{};
|
|
|
|
std::map<std::string, std::unique_ptr<StringLiteralType>> m_stringLiteralTypes{};
|
|
|
|
std::vector<std::unique_ptr<Type>> m_generalTypes{};
|
|
|
|
};
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
}
|