solidity/Types.h

226 lines
7.1 KiB
C
Raw Normal View History

2014-10-13 16:22:15 +00:00
/*
2014-10-16 12:08:54 +00:00
This file is part of cpp-ethereum.
2014-10-13 16:22:15 +00:00
2014-10-16 12:08:54 +00:00
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.
2014-10-13 16:22:15 +00:00
2014-10-16 12:08:54 +00:00
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.
2014-10-13 16:22:15 +00:00
2014-10-16 12:08:54 +00:00
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
2014-10-13 16:22:15 +00:00
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Solidity data types
*/
#pragma once
#include <memory>
#include <string>
#include <boost/noncopyable.hpp>
2014-10-20 10:41:56 +00:00
#include <libdevcore/Common.h>
#include <libsolidity/ASTForward.h>
2014-10-13 16:22:15 +00:00
#include <libsolidity/Token.h>
2014-10-16 12:08:54 +00:00
namespace dev
{
namespace solidity
{
2014-10-13 16:22:15 +00:00
// @todo realMxN, string<N>, mapping
2014-10-23 15:06:12 +00:00
/// Abstract base class that forms the root of the type hierarchy.
2014-10-16 21:49:45 +00:00
class Type: private boost::noncopyable
2014-10-13 16:22:15 +00:00
{
public:
2014-10-16 12:08:54 +00:00
enum class Category
{
2014-10-13 16:22:15 +00:00
INTEGER, BOOL, REAL, STRING, CONTRACT, STRUCT, FUNCTION, MAPPING, VOID, TYPE
};
2014-10-23 15:06:12 +00:00
///@{
///@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);
2014-10-23 15:06:12 +00:00
/// @}
2014-10-13 16:22:15 +00:00
2014-10-23 15:06:12 +00:00
/// Auto-detect the proper type for a literal
static std::shared_ptr<Type> forLiteral(Literal const& _literal);
2014-10-13 16:22:15 +00:00
virtual Category getCategory() const = 0;
2014-10-20 10:41:56 +00:00
virtual bool isImplicitlyConvertibleTo(Type const& _other) const { return *this == _other; }
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const
2014-10-16 12:08:54 +00:00
{
return isImplicitlyConvertibleTo(_convertTo);
}
2014-10-15 16:50:15 +00:00
virtual bool acceptsBinaryOperator(Token::Value) const { return false; }
virtual bool acceptsUnaryOperator(Token::Value) const { return false; }
2014-10-16 15:57:27 +00:00
2014-10-20 10:41:56 +00:00
virtual bool operator==(Type const& _other) const { return getCategory() == _other.getCategory(); }
virtual bool operator!=(Type const& _other) const { return !this->operator ==(_other); }
2014-10-16 15:57:27 +00:00
virtual std::string toString() const = 0;
2014-10-20 10:41:56 +00:00
virtual bytes literalToBigEndian(Literal const&) const { return NullBytes; }
2014-10-13 16:22:15 +00:00
};
2014-10-23 15:06:12 +00:00
/// Any kind of integer type including hash and address.
2014-10-16 21:49:45 +00:00
class IntegerType: public Type
2014-10-13 16:22:15 +00:00
{
public:
2014-10-16 12:08:54 +00:00
enum class Modifier
{
2014-10-13 16:22:15 +00:00
UNSIGNED, SIGNED, HASH, ADDRESS
};
2014-10-16 15:57:27 +00:00
virtual Category getCategory() const override { return Category::INTEGER; }
2014-10-13 16:22:15 +00:00
static std::shared_ptr<IntegerType> smallestTypeForLiteral(std::string const& _literal);
2014-10-13 16:22:15 +00:00
explicit IntegerType(int _bits, Modifier _modifier = Modifier::UNSIGNED);
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
2014-10-13 16:22:15 +00:00
virtual bool acceptsBinaryOperator(Token::Value _operator) const override;
virtual bool acceptsUnaryOperator(Token::Value _operator) const override;
2014-10-20 10:41:56 +00:00
virtual bool operator==(Type const& _other) const override;
2014-10-16 15:57:27 +00:00
virtual std::string toString() const override;
2014-10-20 10:41:56 +00:00
virtual bytes literalToBigEndian(Literal const& _literal) const override;
2014-10-16 15:57:27 +00:00
2014-10-13 16:22:15 +00:00
int getNumBits() const { return m_bits; }
bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; }
bool isAddress() const { return m_modifier == Modifier::ADDRESS; }
int isSigned() const { return m_modifier == Modifier::SIGNED; }
2014-10-13 16:22:15 +00:00
private:
int m_bits;
Modifier m_modifier;
};
2014-10-23 15:06:12 +00:00
/// The boolean type.
2014-10-16 21:49:45 +00:00
class BoolType: public Type
2014-10-13 16:22:15 +00:00
{
public:
virtual Category getCategory() const { return Category::BOOL; }
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
2014-10-13 16:22:15 +00:00
virtual bool acceptsBinaryOperator(Token::Value _operator) const override
2014-10-16 12:08:54 +00:00
{
return _operator == Token::AND || _operator == Token::OR;
}
2014-10-13 16:22:15 +00:00
virtual bool acceptsUnaryOperator(Token::Value _operator) const override
2014-10-16 12:08:54 +00:00
{
return _operator == Token::NOT || _operator == Token::DELETE;
}
2014-10-20 10:41:56 +00:00
2014-10-16 15:57:27 +00:00
virtual std::string toString() const override { return "bool"; }
2014-10-20 10:41:56 +00:00
virtual bytes literalToBigEndian(Literal const& _literal) const override;
2014-10-13 16:22:15 +00:00
};
2014-10-23 15:06:12 +00:00
/// The type of a contract instance, there is one distinct type for each contract definition.
2014-10-16 21:49:45 +00:00
class ContractType: public Type
2014-10-13 16:22:15 +00:00
{
public:
2014-10-16 15:57:27 +00:00
virtual Category getCategory() const override { return Category::CONTRACT; }
2014-10-16 21:49:45 +00:00
ContractType(ContractDefinition const& _contract): m_contract(_contract) {}
2014-10-20 10:41:56 +00:00
virtual bool operator==(Type const& _other) const override;
2014-10-16 15:57:27 +00:00
virtual std::string toString() const override { return "contract{...}"; }
2014-10-13 16:22:15 +00:00
private:
ContractDefinition const& m_contract;
};
2014-10-23 15:06:12 +00:00
/// The type of a struct instance, there is one distinct type per struct definition.
2014-10-16 21:49:45 +00:00
class StructType: public Type
2014-10-13 16:22:15 +00:00
{
public:
2014-10-16 15:57:27 +00:00
virtual Category getCategory() const override { return Category::STRUCT; }
2014-10-16 21:49:45 +00:00
StructType(StructDefinition const& _struct): m_struct(_struct) {}
2014-10-13 16:22:15 +00:00
virtual bool acceptsUnaryOperator(Token::Value _operator) const override
2014-10-16 12:08:54 +00:00
{
return _operator == Token::DELETE;
}
2014-10-20 10:41:56 +00:00
virtual bool operator==(Type const& _other) const override;
2014-10-16 15:57:27 +00:00
virtual std::string toString() const override { return "struct{...}"; }
2014-10-13 16:22:15 +00:00
private:
StructDefinition const& m_struct;
};
2014-10-23 15:06:12 +00:00
/// The type of a function, there is one distinct type per function definition.
2014-10-16 21:49:45 +00:00
class FunctionType: public Type
2014-10-13 16:22:15 +00:00
{
public:
2014-10-16 15:57:27 +00:00
virtual Category getCategory() const override { return Category::FUNCTION; }
2014-10-16 21:49:45 +00:00
FunctionType(FunctionDefinition const& _function): m_function(_function) {}
2014-10-13 16:22:15 +00:00
FunctionDefinition const& getFunction() const { return m_function; }
2014-10-16 15:57:27 +00:00
virtual std::string toString() const override { return "function(...)returns(...)"; }
2014-10-20 10:41:56 +00:00
virtual bool operator==(Type const& _other) const override;
2014-10-13 16:22:15 +00:00
private:
FunctionDefinition const& m_function;
};
2014-10-23 15:06:12 +00:00
/// The type of a mapping, there is one distinct type per key/value type pair.
2014-10-16 21:49:45 +00:00
class MappingType: public Type
2014-10-13 16:22:15 +00:00
{
public:
2014-10-16 15:57:27 +00:00
virtual Category getCategory() const override { return Category::MAPPING; }
2014-10-13 16:22:15 +00:00
MappingType() {}
2014-10-16 15:57:27 +00:00
virtual std::string toString() const override { return "mapping(...=>...)"; }
2014-10-20 10:41:56 +00:00
virtual bool operator==(Type const& _other) const override;
2014-10-13 16:22:15 +00:00
private:
2014-10-20 10:41:56 +00:00
std::shared_ptr<Type const> m_keyType;
std::shared_ptr<Type const> m_valueType;
2014-10-13 16:22:15 +00:00
};
2014-10-23 15:06:12 +00:00
/// The void type, can only be implicitly used as the type that is returned by functions without
/// return parameters.
2014-10-16 21:49:45 +00:00
class VoidType: public Type
2014-10-13 16:22:15 +00:00
{
public:
2014-10-16 15:57:27 +00:00
virtual Category getCategory() const override { return Category::VOID; }
2014-10-13 16:22:15 +00:00
VoidType() {}
2014-10-20 10:41:56 +00:00
2014-10-16 15:57:27 +00:00
virtual std::string toString() const override { return "void"; }
2014-10-13 16:22:15 +00:00
};
2014-10-23 15:06:12 +00:00
/// The type of a type reference. The type of "uint32" when used in "a = uint32(2)" is an example
/// of a TypeType.
2014-10-16 21:49:45 +00:00
class TypeType: public Type
2014-10-13 16:22:15 +00:00
{
public:
2014-10-16 15:57:27 +00:00
virtual Category getCategory() const override { return Category::TYPE; }
TypeType(std::shared_ptr<Type const> const& _actualType): m_actualType(_actualType) {}
std::shared_ptr<Type const> const& getActualType() const { return m_actualType; }
2014-10-13 16:22:15 +00:00
2014-10-20 10:41:56 +00:00
virtual bool operator==(Type const& _other) const override;
2014-10-16 15:57:27 +00:00
virtual std::string toString() const override { return "type(" + m_actualType->toString() + ")"; }
2014-10-13 16:22:15 +00:00
private:
std::shared_ptr<Type const> m_actualType;
2014-10-13 16:22:15 +00:00
};
2014-10-16 12:08:54 +00:00
}
}