mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Arbitrary precision integer constants.
This commit is contained in:
parent
be1e89da42
commit
be623273f3
20
AST.cpp
20
AST.cpp
@ -174,7 +174,15 @@ void VariableDefinition::checkTypeRequirements()
|
|||||||
{
|
{
|
||||||
// no type declared and no previous assignment, infer the type
|
// no type declared and no previous assignment, infer the type
|
||||||
m_value->checkTypeRequirements();
|
m_value->checkTypeRequirements();
|
||||||
m_variable->setType(m_value->getType());
|
TypePointer type = m_value->getType();
|
||||||
|
if (type->getCategory() == Type::Category::INTEGER_CONSTANT)
|
||||||
|
{
|
||||||
|
auto intType = dynamic_pointer_cast<IntegerConstantType const>(type)->getIntegerType();
|
||||||
|
if (!intType)
|
||||||
|
BOOST_THROW_EXCEPTION(m_value->createTypeError("Invalid integer constant " + type->toString()));
|
||||||
|
type = intType;
|
||||||
|
}
|
||||||
|
m_variable->setType(type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -196,13 +204,19 @@ void Assignment::checkTypeRequirements()
|
|||||||
TypePointer resultType = m_type->binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator),
|
TypePointer resultType = m_type->binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator),
|
||||||
m_rightHandSide->getType());
|
m_rightHandSide->getType());
|
||||||
if (!resultType || *resultType != *m_type)
|
if (!resultType || *resultType != *m_type)
|
||||||
BOOST_THROW_EXCEPTION(createTypeError("Operator not compatible with type."));
|
BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_assigmentOperator)) +
|
||||||
|
" not compatible with types " +
|
||||||
|
m_type->toString() + " and " +
|
||||||
|
m_rightHandSide->getType()->toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExpressionStatement::checkTypeRequirements()
|
void ExpressionStatement::checkTypeRequirements()
|
||||||
{
|
{
|
||||||
m_expression->checkTypeRequirements();
|
m_expression->checkTypeRequirements();
|
||||||
|
if (m_expression->getType()->getCategory() == Type::Category::INTEGER_CONSTANT)
|
||||||
|
if (!dynamic_pointer_cast<IntegerConstantType const>(m_expression->getType())->getIntegerType())
|
||||||
|
BOOST_THROW_EXCEPTION(m_expression->createTypeError("Invalid integer constant."));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Expression::expectType(Type const& _expectedType)
|
void Expression::expectType(Type const& _expectedType)
|
||||||
@ -388,7 +402,7 @@ void Literal::checkTypeRequirements()
|
|||||||
{
|
{
|
||||||
m_type = Type::forLiteral(*this);
|
m_type = Type::forLiteral(*this);
|
||||||
if (!m_type)
|
if (!m_type)
|
||||||
BOOST_THROW_EXCEPTION(createTypeError("Literal value too large."));
|
BOOST_THROW_EXCEPTION(createTypeError("Invalid literal value."));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -71,12 +71,20 @@ bool ExpressionCompiler::visit(Assignment const& _assignment)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExpressionCompiler::endVisit(UnaryOperation const& _unaryOperation)
|
bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
|
||||||
{
|
{
|
||||||
//@todo type checking and creating code for an operator should be in the same place:
|
//@todo type checking and creating code for an operator should be in the same place:
|
||||||
// the operator should know how to convert itself and to which types it applies, so
|
// the operator should know how to convert itself and to which types it applies, so
|
||||||
// put this code together with "Type::acceptsBinary/UnaryOperator" into a class that
|
// put this code together with "Type::acceptsBinary/UnaryOperator" into a class that
|
||||||
// represents the operator
|
// represents the operator
|
||||||
|
if (_unaryOperation.getType()->getCategory() == Type::Category::INTEGER_CONSTANT)
|
||||||
|
{
|
||||||
|
m_context << _unaryOperation.getType()->literalValue(nullptr);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_unaryOperation.getSubExpression().accept(*this);
|
||||||
|
|
||||||
switch (_unaryOperation.getOperator())
|
switch (_unaryOperation.getOperator())
|
||||||
{
|
{
|
||||||
case Token::NOT: // !
|
case Token::NOT: // !
|
||||||
@ -128,6 +136,7 @@ void ExpressionCompiler::endVisit(UnaryOperation const& _unaryOperation)
|
|||||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid unary operator: " +
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid unary operator: " +
|
||||||
string(Token::toString(_unaryOperation.getOperator()))));
|
string(Token::toString(_unaryOperation.getOperator()))));
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
|
bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
|
||||||
@ -139,17 +148,19 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
|
|||||||
|
|
||||||
if (op == Token::AND || op == Token::OR) // special case: short-circuiting
|
if (op == Token::AND || op == Token::OR) // special case: short-circuiting
|
||||||
appendAndOrOperatorCode(_binaryOperation);
|
appendAndOrOperatorCode(_binaryOperation);
|
||||||
|
else if (commonType.getCategory() == Type::Category::INTEGER_CONSTANT)
|
||||||
|
m_context << commonType.literalValue(nullptr);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bool cleanupNeeded = false;
|
bool cleanupNeeded = commonType.getCategory() == Type::Category::INTEGER &&
|
||||||
if (commonType.getCategory() == Type::Category::INTEGER)
|
(Token::isCompareOp(op) || op == Token::DIV || op == Token::MOD);
|
||||||
if (Token::isCompareOp(op) || op == Token::DIV || op == Token::MOD)
|
|
||||||
cleanupNeeded = true;
|
|
||||||
|
|
||||||
// for commutative operators, push the literal as late as possible to allow improved optimization
|
// for commutative operators, push the literal as late as possible to allow improved optimization
|
||||||
//@todo this has to be extended for literal expressions
|
auto isLiteral = [](Expression const& _e)
|
||||||
bool swap = (m_optimize && Token::isCommutativeOp(op) && dynamic_cast<Literal const*>(&rightExpression)
|
{
|
||||||
&& !dynamic_cast<Literal const*>(&leftExpression));
|
return dynamic_cast<Literal const*>(&_e) || _e.getType()->getCategory() == Type::Category::INTEGER_CONSTANT;
|
||||||
|
};
|
||||||
|
bool swap = m_optimize && Token::isCommutativeOp(op) && isLiteral(rightExpression) && !isLiteral(leftExpression);
|
||||||
if (swap)
|
if (swap)
|
||||||
{
|
{
|
||||||
leftExpression.accept(*this);
|
leftExpression.accept(*this);
|
||||||
@ -478,10 +489,10 @@ void ExpressionCompiler::endVisit(Literal const& _literal)
|
|||||||
{
|
{
|
||||||
switch (_literal.getType()->getCategory())
|
switch (_literal.getType()->getCategory())
|
||||||
{
|
{
|
||||||
case Type::Category::INTEGER:
|
case Type::Category::INTEGER_CONSTANT:
|
||||||
case Type::Category::BOOL:
|
case Type::Category::BOOL:
|
||||||
case Type::Category::STRING:
|
case Type::Category::STRING:
|
||||||
m_context << _literal.getType()->literalValue(_literal);
|
m_context << _literal.getType()->literalValue(&_literal);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer, boolean and string literals implemented for now."));
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer, boolean and string literals implemented for now."));
|
||||||
@ -617,9 +628,16 @@ void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type con
|
|||||||
|
|
||||||
if (_typeOnStack == _targetType && !_cleanupNeeded)
|
if (_typeOnStack == _targetType && !_cleanupNeeded)
|
||||||
return;
|
return;
|
||||||
if (_typeOnStack.getCategory() == Type::Category::INTEGER)
|
Type::Category stackTypeCategory = _typeOnStack.getCategory();
|
||||||
|
Type::Category targetTypeCategory = _targetType.getCategory();
|
||||||
|
if (stackTypeCategory == Type::Category::INTEGER)
|
||||||
|
{
|
||||||
|
solAssert(targetTypeCategory == Type::Category::INTEGER || targetTypeCategory == Type::Category::CONTRACT, "");
|
||||||
appendHighBitsCleanup(dynamic_cast<IntegerType const&>(_typeOnStack));
|
appendHighBitsCleanup(dynamic_cast<IntegerType const&>(_typeOnStack));
|
||||||
else if (_typeOnStack.getCategory() == Type::Category::STRING)
|
}
|
||||||
|
else if (stackTypeCategory == Type::Category::INTEGER_CONSTANT)
|
||||||
|
solAssert(targetTypeCategory == Type::Category::INTEGER || targetTypeCategory == Type::Category::CONTRACT, "");
|
||||||
|
else if (stackTypeCategory == Type::Category::STRING)
|
||||||
{
|
{
|
||||||
// nothing to do, strings are high-order-bit-aligned
|
// nothing to do, strings are high-order-bit-aligned
|
||||||
//@todo clear lower-order bytes if we allow explicit conversion to shorter strings
|
//@todo clear lower-order bytes if we allow explicit conversion to shorter strings
|
||||||
|
@ -58,7 +58,7 @@ private:
|
|||||||
m_optimize(_optimize), m_context(_compilerContext), m_currentLValue(m_context) {}
|
m_optimize(_optimize), m_context(_compilerContext), m_currentLValue(m_context) {}
|
||||||
|
|
||||||
virtual bool visit(Assignment const& _assignment) override;
|
virtual bool visit(Assignment const& _assignment) override;
|
||||||
virtual void endVisit(UnaryOperation const& _unaryOperation) override;
|
virtual bool visit(UnaryOperation const& _unaryOperation) override;
|
||||||
virtual bool visit(BinaryOperation const& _binaryOperation) override;
|
virtual bool visit(BinaryOperation const& _binaryOperation) override;
|
||||||
virtual bool visit(FunctionCall const& _functionCall) override;
|
virtual bool visit(FunctionCall const& _functionCall) override;
|
||||||
virtual bool visit(NewExpression const& _newExpression) override;
|
virtual bool visit(NewExpression const& _newExpression) override;
|
||||||
|
@ -455,7 +455,7 @@ void Scanner::scanToken()
|
|||||||
token = Token::ADD;
|
token = Token::ADD;
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
// - -- -= Number
|
// - -- -=
|
||||||
advance();
|
advance();
|
||||||
if (m_char == '-')
|
if (m_char == '-')
|
||||||
{
|
{
|
||||||
@ -464,8 +464,6 @@ void Scanner::scanToken()
|
|||||||
}
|
}
|
||||||
else if (m_char == '=')
|
else if (m_char == '=')
|
||||||
token = selectToken(Token::ASSIGN_SUB);
|
token = selectToken(Token::ASSIGN_SUB);
|
||||||
else if (m_char == '.' || isDecimalDigit(m_char))
|
|
||||||
token = scanNumber('-');
|
|
||||||
else
|
else
|
||||||
token = Token::SUB;
|
token = Token::SUB;
|
||||||
break;
|
break;
|
||||||
@ -650,8 +648,7 @@ Token::Value Scanner::scanNumber(char _charSeen)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_charSeen == '-')
|
solAssert(_charSeen == 0, "");
|
||||||
addLiteralChar('-');
|
|
||||||
// if the first character is '0' we must check for octals and hex
|
// if the first character is '0' we must check for octals and hex
|
||||||
if (m_char == '0')
|
if (m_char == '0')
|
||||||
{
|
{
|
||||||
|
172
Types.cpp
172
Types.cpp
@ -91,7 +91,7 @@ shared_ptr<Type const> Type::forLiteral(Literal const& _literal)
|
|||||||
case Token::FALSE_LITERAL:
|
case Token::FALSE_LITERAL:
|
||||||
return make_shared<BoolType const>();
|
return make_shared<BoolType const>();
|
||||||
case Token::NUMBER:
|
case Token::NUMBER:
|
||||||
return IntegerType::smallestTypeForLiteral(_literal.getValue());
|
return IntegerConstantType::fromLiteral(_literal.getValue());
|
||||||
case Token::STRING_LITERAL:
|
case Token::STRING_LITERAL:
|
||||||
//@todo put larger strings into dynamic strings
|
//@todo put larger strings into dynamic strings
|
||||||
return StaticStringType::smallestTypeForLiteral(_literal.getValue());
|
return StaticStringType::smallestTypeForLiteral(_literal.getValue());
|
||||||
@ -112,19 +112,6 @@ TypePointer Type::commonType(TypePointer const& _a, TypePointer const& _b)
|
|||||||
|
|
||||||
const MemberList Type::EmptyMemberList = MemberList();
|
const MemberList Type::EmptyMemberList = MemberList();
|
||||||
|
|
||||||
shared_ptr<IntegerType const> IntegerType::smallestTypeForLiteral(string const& _literal)
|
|
||||||
{
|
|
||||||
bigint value(_literal);
|
|
||||||
bool isSigned = value < 0 || (!_literal.empty() && _literal.front() == '-');
|
|
||||||
if (isSigned)
|
|
||||||
// convert to positive number of same bit requirements
|
|
||||||
value = ((-value) - 1) << 1;
|
|
||||||
unsigned bytes = max(bytesRequired(value), 1u);
|
|
||||||
if (bytes > 32)
|
|
||||||
return shared_ptr<IntegerType const>();
|
|
||||||
return make_shared<IntegerType const>(bytes * 8, isSigned ? Modifier::SIGNED : Modifier::UNSIGNED);
|
|
||||||
}
|
|
||||||
|
|
||||||
IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier):
|
IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier):
|
||||||
m_bits(_bits), m_modifier(_modifier)
|
m_bits(_bits), m_modifier(_modifier)
|
||||||
{
|
{
|
||||||
@ -194,15 +181,9 @@ string IntegerType::toString() const
|
|||||||
return prefix + dev::toString(m_bits);
|
return prefix + dev::toString(m_bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
u256 IntegerType::literalValue(Literal const& _literal) const
|
|
||||||
{
|
|
||||||
bigint value(_literal.getValue());
|
|
||||||
return u256(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
|
TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
|
||||||
{
|
{
|
||||||
if (getCategory() != _other->getCategory())
|
if (_other->getCategory() != Category::INTEGER_CONSTANT && _other->getCategory() != getCategory())
|
||||||
return TypePointer();
|
return TypePointer();
|
||||||
auto commonType = dynamic_pointer_cast<IntegerType const>(Type::commonType(shared_from_this(), _other));
|
auto commonType = dynamic_pointer_cast<IntegerType const>(Type::commonType(shared_from_this(), _other));
|
||||||
|
|
||||||
@ -236,6 +217,141 @@ const MemberList IntegerType::AddressMemberList =
|
|||||||
make_shared<FunctionType const>(TypePointers({make_shared<IntegerType const>(256)}),
|
make_shared<FunctionType const>(TypePointers({make_shared<IntegerType const>(256)}),
|
||||||
TypePointers(), FunctionType::Location::SEND)}});
|
TypePointers(), FunctionType::Location::SEND)}});
|
||||||
|
|
||||||
|
shared_ptr<IntegerConstantType const> IntegerConstantType::fromLiteral(string const& _literal)
|
||||||
|
{
|
||||||
|
return make_shared<IntegerConstantType const>(bigint(_literal));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const
|
||||||
|
{
|
||||||
|
TypePointer integerType = getIntegerType();
|
||||||
|
return integerType && integerType->isImplicitlyConvertibleTo(_convertTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IntegerConstantType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
||||||
|
{
|
||||||
|
TypePointer integerType = getIntegerType();
|
||||||
|
return integerType && integerType->isExplicitlyConvertibleTo(_convertTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
TypePointer IntegerConstantType::unaryOperatorResult(Token::Value _operator) const
|
||||||
|
{
|
||||||
|
bigint value;
|
||||||
|
switch (_operator)
|
||||||
|
{
|
||||||
|
case Token::BIT_NOT:
|
||||||
|
value = ~m_value;
|
||||||
|
break;
|
||||||
|
case Token::ADD:
|
||||||
|
value = m_value;
|
||||||
|
break;
|
||||||
|
case Token::SUB:
|
||||||
|
value = -m_value;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return TypePointer();
|
||||||
|
}
|
||||||
|
return make_shared<IntegerConstantType const>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
TypePointer IntegerConstantType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
|
||||||
|
{
|
||||||
|
if (_other->getCategory() == Category::INTEGER)
|
||||||
|
{
|
||||||
|
shared_ptr<IntegerType const> integerType = getIntegerType();
|
||||||
|
if (!integerType)
|
||||||
|
return TypePointer();
|
||||||
|
return integerType->binaryOperatorResult(_operator, _other);
|
||||||
|
}
|
||||||
|
else if (_other->getCategory() != getCategory())
|
||||||
|
return TypePointer();
|
||||||
|
|
||||||
|
IntegerConstantType const& other = dynamic_cast<IntegerConstantType const&>(*_other);
|
||||||
|
if (Token::isCompareOp(_operator))
|
||||||
|
{
|
||||||
|
shared_ptr<IntegerType const> thisIntegerType = getIntegerType();
|
||||||
|
shared_ptr<IntegerType const> otherIntegerType = other.getIntegerType();
|
||||||
|
if (!thisIntegerType || !otherIntegerType)
|
||||||
|
return TypePointer();
|
||||||
|
return thisIntegerType->binaryOperatorResult(_operator, otherIntegerType);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bigint value;
|
||||||
|
switch (_operator)
|
||||||
|
{
|
||||||
|
case Token::BIT_OR:
|
||||||
|
value = m_value | other.m_value;
|
||||||
|
break;
|
||||||
|
case Token::BIT_XOR:
|
||||||
|
value = m_value ^ other.m_value;
|
||||||
|
break;
|
||||||
|
case Token::BIT_AND:
|
||||||
|
value = m_value & other.m_value;
|
||||||
|
break;
|
||||||
|
case Token::ADD:
|
||||||
|
value = m_value + other.m_value;
|
||||||
|
break;
|
||||||
|
case Token::SUB:
|
||||||
|
value = m_value - other.m_value;
|
||||||
|
break;
|
||||||
|
case Token::MUL:
|
||||||
|
value = m_value * other.m_value;
|
||||||
|
break;
|
||||||
|
case Token::DIV:
|
||||||
|
if (other.m_value == 0)
|
||||||
|
return TypePointer();
|
||||||
|
value = m_value / other.m_value;
|
||||||
|
break;
|
||||||
|
case Token::MOD:
|
||||||
|
if (other.m_value == 0)
|
||||||
|
return TypePointer();
|
||||||
|
value = m_value % other.m_value;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return TypePointer();
|
||||||
|
}
|
||||||
|
return make_shared<IntegerConstantType const>(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IntegerConstantType::operator==(Type const& _other) const
|
||||||
|
{
|
||||||
|
if (_other.getCategory() != getCategory())
|
||||||
|
return false;
|
||||||
|
return m_value == dynamic_cast<IntegerConstantType const&>(_other).m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
string IntegerConstantType::toString() const
|
||||||
|
{
|
||||||
|
return "int_const " + m_value.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
u256 IntegerConstantType::literalValue(Literal const*) const
|
||||||
|
{
|
||||||
|
// we ignore the literal and hope that the type was correctly determined
|
||||||
|
solAssert(m_value <= u256(-1), "Integer constant too large.");
|
||||||
|
solAssert(m_value >= -(bigint(1) << 255), "Integer constant too small.");
|
||||||
|
if (m_value >= 0)
|
||||||
|
return u256(m_value);
|
||||||
|
else
|
||||||
|
return s2u(s256(m_value));
|
||||||
|
}
|
||||||
|
|
||||||
|
shared_ptr<IntegerType const> IntegerConstantType::getIntegerType() const
|
||||||
|
{
|
||||||
|
bigint value = m_value;
|
||||||
|
bool negative = (value < 0);
|
||||||
|
if (negative) // convert to positive number of same bit requirements
|
||||||
|
value = ((-value) - 1) << 1;
|
||||||
|
if (value > u256(-1))
|
||||||
|
return shared_ptr<IntegerType const>();
|
||||||
|
else
|
||||||
|
return make_shared<IntegerType const>(max(bytesRequired(value), 1u) * 8,
|
||||||
|
negative ? IntegerType::Modifier::SIGNED
|
||||||
|
: IntegerType::Modifier::UNSIGNED);
|
||||||
|
}
|
||||||
|
|
||||||
shared_ptr<StaticStringType> StaticStringType::smallestTypeForLiteral(string const& _literal)
|
shared_ptr<StaticStringType> StaticStringType::smallestTypeForLiteral(string const& _literal)
|
||||||
{
|
{
|
||||||
if (_literal.length() <= 32)
|
if (_literal.length() <= 32)
|
||||||
@ -265,12 +381,13 @@ bool StaticStringType::operator==(Type const& _other) const
|
|||||||
return other.m_bytes == m_bytes;
|
return other.m_bytes == m_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
u256 StaticStringType::literalValue(const Literal& _literal) const
|
u256 StaticStringType::literalValue(const Literal* _literal) const
|
||||||
{
|
{
|
||||||
|
solAssert(_literal, "");
|
||||||
u256 value = 0;
|
u256 value = 0;
|
||||||
for (char c: _literal.getValue())
|
for (char c: _literal->getValue())
|
||||||
value = (value << 8) | byte(c);
|
value = (value << 8) | byte(c);
|
||||||
return value << ((32 - _literal.getValue().length()) * 8);
|
return value << ((32 - _literal->getValue().length()) * 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
||||||
@ -286,11 +403,12 @@ bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
|||||||
return isImplicitlyConvertibleTo(_convertTo);
|
return isImplicitlyConvertibleTo(_convertTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
u256 BoolType::literalValue(Literal const& _literal) const
|
u256 BoolType::literalValue(Literal const* _literal) const
|
||||||
{
|
{
|
||||||
if (_literal.getToken() == Token::TRUE_LITERAL)
|
solAssert(_literal, "");
|
||||||
|
if (_literal->getToken() == Token::TRUE_LITERAL)
|
||||||
return u256(1);
|
return u256(1);
|
||||||
else if (_literal.getToken() == Token::FALSE_LITERAL)
|
else if (_literal->getToken() == Token::FALSE_LITERAL)
|
||||||
return u256(0);
|
return u256(0);
|
||||||
else
|
else
|
||||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Bool type constructed from non-boolean literal."));
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Bool type constructed from non-boolean literal."));
|
||||||
|
47
Types.h
47
Types.h
@ -75,7 +75,7 @@ class Type: private boost::noncopyable, public std::enable_shared_from_this<Type
|
|||||||
public:
|
public:
|
||||||
enum class Category
|
enum class Category
|
||||||
{
|
{
|
||||||
INTEGER, BOOL, REAL, STRING, CONTRACT, STRUCT, FUNCTION, MAPPING, VOID, TYPE, MAGIC
|
INTEGER, INTEGER_CONSTANT, BOOL, REAL, STRING, CONTRACT, STRUCT, FUNCTION, MAPPING, VOID, TYPE, MAGIC
|
||||||
};
|
};
|
||||||
|
|
||||||
///@{
|
///@{
|
||||||
@ -135,7 +135,7 @@ public:
|
|||||||
TypePointer getMemberType(std::string const& _name) const { return getMembers().getMemberType(_name); }
|
TypePointer getMemberType(std::string const& _name) const { return getMembers().getMemberType(_name); }
|
||||||
|
|
||||||
virtual std::string toString() const = 0;
|
virtual std::string toString() const = 0;
|
||||||
virtual u256 literalValue(Literal const&) const
|
virtual u256 literalValue(Literal const*) const
|
||||||
{
|
{
|
||||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Literal value requested "
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Literal value requested "
|
||||||
"for type without literals."));
|
"for type without literals."));
|
||||||
@ -158,10 +158,6 @@ public:
|
|||||||
};
|
};
|
||||||
virtual Category getCategory() const override { return Category::INTEGER; }
|
virtual Category getCategory() const override { return Category::INTEGER; }
|
||||||
|
|
||||||
/// @returns the smallest integer type for the given literal or an empty pointer
|
|
||||||
/// if no type fits.
|
|
||||||
static std::shared_ptr<IntegerType const> smallestTypeForLiteral(std::string const& _literal);
|
|
||||||
|
|
||||||
explicit IntegerType(int _bits, Modifier _modifier = Modifier::UNSIGNED);
|
explicit IntegerType(int _bits, Modifier _modifier = Modifier::UNSIGNED);
|
||||||
|
|
||||||
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
|
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
|
||||||
@ -177,7 +173,6 @@ public:
|
|||||||
virtual MemberList const& getMembers() const { return isAddress() ? AddressMemberList : EmptyMemberList; }
|
virtual MemberList const& getMembers() const { return isAddress() ? AddressMemberList : EmptyMemberList; }
|
||||||
|
|
||||||
virtual std::string toString() const override;
|
virtual std::string toString() const override;
|
||||||
virtual u256 literalValue(Literal const& _literal) const override;
|
|
||||||
|
|
||||||
int getNumBits() const { return m_bits; }
|
int getNumBits() const { return m_bits; }
|
||||||
bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; }
|
bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; }
|
||||||
@ -190,6 +185,40 @@ private:
|
|||||||
static const MemberList AddressMemberList;
|
static const MemberList AddressMemberList;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integer constants either literals or computed. Example expressions: 2, 2+10, ~10.
|
||||||
|
* There is one distinct type per value.
|
||||||
|
*/
|
||||||
|
class IntegerConstantType: public Type
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual Category getCategory() const override { return Category::INTEGER_CONSTANT; }
|
||||||
|
|
||||||
|
static std::shared_ptr<IntegerConstantType const> fromLiteral(std::string const& _literal);
|
||||||
|
|
||||||
|
explicit IntegerConstantType(bigint _value): m_value(_value) {}
|
||||||
|
|
||||||
|
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
|
||||||
|
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
|
||||||
|
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override;
|
||||||
|
virtual TypePointer binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const override;
|
||||||
|
|
||||||
|
virtual bool operator==(Type const& _other) const override;
|
||||||
|
|
||||||
|
virtual bool canBeStored() const override { return false; }
|
||||||
|
virtual bool canLiveOutsideStorage() const override { return false; }
|
||||||
|
virtual unsigned getSizeOnStack() const override { return 1; }
|
||||||
|
|
||||||
|
virtual std::string toString() const override;
|
||||||
|
virtual u256 literalValue(Literal const* _literal) const override;
|
||||||
|
|
||||||
|
/// @returns the smallest integer type that can hold the value or an empty pointer if not possible.
|
||||||
|
std::shared_ptr<IntegerType const> getIntegerType() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bigint m_value;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String type with fixed length, up to 32 bytes.
|
* String type with fixed length, up to 32 bytes.
|
||||||
*/
|
*/
|
||||||
@ -211,7 +240,7 @@ public:
|
|||||||
virtual bool isValueType() const override { return true; }
|
virtual bool isValueType() const override { return true; }
|
||||||
|
|
||||||
virtual std::string toString() const override { return "string" + dev::toString(m_bytes); }
|
virtual std::string toString() const override { return "string" + dev::toString(m_bytes); }
|
||||||
virtual u256 literalValue(Literal const& _literal) const override;
|
virtual u256 literalValue(Literal const* _literal) const override;
|
||||||
|
|
||||||
int getNumBytes() const { return m_bytes; }
|
int getNumBytes() const { return m_bytes; }
|
||||||
|
|
||||||
@ -238,7 +267,7 @@ public:
|
|||||||
virtual bool isValueType() const override { return true; }
|
virtual bool isValueType() const override { return true; }
|
||||||
|
|
||||||
virtual std::string toString() const override { return "bool"; }
|
virtual std::string toString() const override { return "bool"; }
|
||||||
virtual u256 literalValue(Literal const& _literal) const override;
|
virtual u256 literalValue(Literal const* _literal) const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user