solidity/Types.h

198 lines
5.8 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>
#include <boost/assert.hpp>
#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-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
};
//! factory functions that convert an AST 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-13 16:22:15 +00:00
static std::shared_ptr<Type> forLiteral(Literal const& _literal);
2014-10-13 16:22:15 +00:00
virtual Category getCategory() const = 0;
virtual bool isImplicitlyConvertibleTo(Type const&) const { return false; }
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
virtual std::string toString() const = 0;
2014-10-13 16:22:15 +00:00
};
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-16 15:57:27 +00:00
virtual std::string toString() const override;
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-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 isImplicitlyConvertibleTo(Type const& _convertTo) const override
2014-10-16 12:08:54 +00:00
{
return _convertTo.getCategory() == 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-16 15:57:27 +00:00
virtual std::string toString() const override { return "bool"; }
2014-10-13 16:22:15 +00:00
};
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-13 16:22:15 +00:00
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const;
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-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 isImplicitlyConvertibleTo(Type const& _convertTo) const;
virtual bool acceptsUnaryOperator(Token::Value _operator) const override
2014-10-16 12:08:54 +00:00
{
return _operator == Token::DELETE;
}
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-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-13 16:22:15 +00:00
private:
FunctionDefinition const& m_function;
};
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-13 16:22:15 +00:00
private:
//@todo
};
//@todo should be changed into "empty anonymous struct"
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-16 15:57:27 +00:00
virtual std::string toString() const override { return "void"; }
2014-10-13 16:22:15 +00:00
};
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-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
}
}