Add type helper function.

This commit is contained in:
chriseth 2019-05-06 11:09:50 +02:00
parent 7c62193524
commit 3365cb9b4a
2 changed files with 17 additions and 9 deletions

View File

@ -116,7 +116,7 @@ bool IRGeneratorForStatements::visit(Assignment const& _assignment)
_assignment.rightHandSide().accept(*this); _assignment.rightHandSide().accept(*this);
Type const* intermediateType = _assignment.rightHandSide().annotation().type->closestTemporaryType( Type const* intermediateType = _assignment.rightHandSide().annotation().type->closestTemporaryType(
_assignment.leftHandSide().annotation().type &type(_assignment.leftHandSide())
); );
string intermediateValue = m_context.newYulVariable(); string intermediateValue = m_context.newYulVariable();
m_code << "let " << intermediateValue << " := " << expressionAsType(_assignment.rightHandSide(), *intermediateType) << "\n"; m_code << "let " << intermediateValue << " := " << expressionAsType(_assignment.rightHandSide(), *intermediateType) << "\n";
@ -228,21 +228,21 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
"This type of function call is not yet implemented" "This type of function call is not yet implemented"
); );
TypePointer const funcType = _functionCall.expression().annotation().type; Type const& funcType = type(_functionCall.expression());
if (_functionCall.annotation().kind == FunctionCallKind::TypeConversion) if (_functionCall.annotation().kind == FunctionCallKind::TypeConversion)
{ {
solAssert(funcType->category() == Type::Category::TypeType, "Expected category to be TypeType"); solAssert(funcType.category() == Type::Category::TypeType, "Expected category to be TypeType");
solAssert(_functionCall.arguments().size() == 1, "Expected one argument for type conversion"); solAssert(_functionCall.arguments().size() == 1, "Expected one argument for type conversion");
defineExpression(_functionCall) << defineExpression(_functionCall) <<
expressionAsType(*_functionCall.arguments().front(), *_functionCall.annotation().type) << expressionAsType(*_functionCall.arguments().front(), type(_functionCall)) <<
"\n"; "\n";
return; return;
} }
FunctionTypePointer functionType = dynamic_cast<FunctionType const*>(funcType); FunctionTypePointer functionType = dynamic_cast<FunctionType const*>(&funcType);
TypePointers parameterTypes = functionType->parameterTypes(); TypePointers parameterTypes = functionType->parameterTypes();
vector<ASTPointer<Expression const>> const& callArguments = _functionCall.arguments(); vector<ASTPointer<Expression const>> const& callArguments = _functionCall.arguments();
@ -347,14 +347,14 @@ bool IRGeneratorForStatements::visit(Identifier const& _identifier)
bool IRGeneratorForStatements::visit(Literal const& _literal) bool IRGeneratorForStatements::visit(Literal const& _literal)
{ {
TypePointer type = _literal.annotation().type; Type const& literalType = type(_literal);
switch (type->category()) switch (literalType.category())
{ {
case Type::Category::RationalNumber: case Type::Category::RationalNumber:
case Type::Category::Bool: case Type::Category::Bool:
case Type::Category::Address: case Type::Category::Address:
defineExpression(_literal) << toCompactHexWithPrefix(type->literalValue(&_literal)) << "\n"; defineExpression(_literal) << toCompactHexWithPrefix(literalType.literalValue(&_literal)) << "\n";
break; break;
case Type::Category::StringLiteral: case Type::Category::StringLiteral:
solUnimplemented(""); solUnimplemented("");
@ -367,7 +367,7 @@ bool IRGeneratorForStatements::visit(Literal const& _literal)
string IRGeneratorForStatements::expressionAsType(Expression const& _expression, Type const& _to) string IRGeneratorForStatements::expressionAsType(Expression const& _expression, Type const& _to)
{ {
Type const& from = *_expression.annotation().type; Type const& from = type(_expression);
string varName = m_context.variable(_expression); string varName = m_context.variable(_expression);
if (from == _to) if (from == _to)
@ -391,3 +391,9 @@ void IRGeneratorForStatements::setLValue(Expression const& _expression, unique_p
else else
defineExpression(_expression) << _lvalue->retrieveValue() << "\n"; defineExpression(_expression) << _lvalue->retrieveValue() << "\n";
} }
Type const& IRGeneratorForStatements::type(Expression const& _expression)
{
solAssert(_expression.annotation().type, "Type of expression not set.");
return *_expression.annotation().type;
}

View File

@ -65,6 +65,8 @@ private:
void setLValue(Expression const& _expression, std::unique_ptr<IRLValue> _lvalue); void setLValue(Expression const& _expression, std::unique_ptr<IRLValue> _lvalue);
static Type const& type(Expression const& _expression);
std::ostringstream m_code; std::ostringstream m_code;
IRGenerationContext& m_context; IRGenerationContext& m_context;
YulUtilFunctions& m_utils; YulUtilFunctions& m_utils;