Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows implicit clock to double conversions in ternary conditional operators during type checking #61

Merged
merged 15 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/utap/typechecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TypeChecker : public DocumentVisitor, public AbstractStatementVisitor

expression_t checkInitialiser(type_t type, expression_t init);
bool areAssignmentCompatible(type_t lvalue, type_t rvalue, bool init = false) const;
bool areInlineIfCompatible(type_t thenArg, type_t elseArg) const;
bool areInlineIfCompatible(type_t result_type, type_t thenArg, type_t elseArg) const;
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
bool areEqCompatible(type_t t1, type_t t2) const;
bool areEquivalent(type_t, type_t) const;
bool isLValue(expression_t) const;
Expand Down
8 changes: 6 additions & 2 deletions src/ExpressionBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void ExpressionBuilder::expr_unary(kind_t unaryop) // 1 expr
case MINUS:
unaryop = UNARY_MINUS;
/* Fall through! */
default: fragments[0] = expression_t::create_unary(unaryop, fragments[0], position);
default: fragments[0] = expression_t::create_unary(unaryop, fragments[0], position, fragments[0].get_type());
}
}

Expand Down Expand Up @@ -543,7 +543,11 @@ void ExpressionBuilder::expr_inline_if()
expression_t t = fragments[1];
expression_t e = fragments[0];
fragments.pop(3);
fragments.push(expression_t::create_ternary(INLINE_IF, c, t, e, position, t.get_type()));

// Handle special case where clock is implicitly converted to double
type_t type = t.get_type().is_clock() && e.get_type().is_double() ? e.get_type() : t.get_type();

fragments.push(expression_t::create_ternary(INLINE_IF, c, t, e, position, type));
}

void ExpressionBuilder::expr_comma()
Expand Down
15 changes: 8 additions & 7 deletions src/typechecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1515,13 +1515,14 @@ expression_t TypeChecker::checkInitialiser(type_t type, expression_t init)
same size and the subtypes must be compatible. In case of records,
they must have the same type name.
*/
bool TypeChecker::areInlineIfCompatible(type_t t1, type_t t2) const
bool TypeChecker::areInlineIfCompatible(type_t result_type, type_t t1, type_t t2) const
{
if (t1.is_integral() && t2.is_integral()) {
if (t1.is_integral() && t2.is_integral())
return true;
} else {
return areEquivalent(t1, t2);
}
if (result_type.is_double() && (t1.is_double() && t2.is_clock() || t1.is_clock() && t2.is_double()))
return true;

return areEquivalent(t1, t2);
}

/**
Expand Down Expand Up @@ -2084,11 +2085,11 @@ bool TypeChecker::checkExpression(expression_t expr)
handleError(expr, "$First_argument_of_inline_if_must_be_an_integer");
return false;
}
if (!areInlineIfCompatible(expr[1].get_type(), expr[2].get_type())) {
if (!areInlineIfCompatible(expr.get_type(), expr[1].get_type(), expr[2].get_type())) {
handleError(expr, "$Incompatible_arguments_to_inline_if");
return false;
}
type = expr[1].get_type();
type = expr.get_type();
break;

case COMMA:
Expand Down
75 changes: 74 additions & 1 deletion test/test_typechecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ TEST_SUITE("Quantifier forall")
df.add_system_decl("bool b[3] = {1,1,1};");
df.add_system_decl("bool x = forall(i : int[0,2]) b[i];");
auto doc = df.add_default_process().parse();
CHECK(doc->get_warnings().size() == 0);
CHECK_MESSAGE(doc->get_warnings().size() == 0, doc->get_warnings()[0].msg);
const auto errors = doc->get_errors();
REQUIRE(errors.size() == 1);
CHECK(errors[0].msg == "$Must_be_computable_at_compile_time");
Expand Down Expand Up @@ -197,3 +197,76 @@ TEST_SUITE("Error positions for unbound parameters")
CHECK(pos.end != pos.unknown_pos);
}
}

TEST_CASE("Ternary operator with clock and double")
{
auto doc = document_fixture{}
.add_global_decl("clock c; double x; void f() { x = true? c : 1.0; }")
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
.add_default_process()
.parse();
CHECK_MESSAGE(doc->get_errors().size() == 0, doc->get_errors()[0].msg);
CHECK_MESSAGE(doc->get_warnings().size() == 0, doc->get_warnings()[0].msg);
}

TEST_CASE("Ternary operator with double and clock")
{
auto doc = document_fixture{}
.add_global_decl("clock c; double x; void f() { x = true? 1.0 : c; }")
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
.add_default_process()
.parse();
CHECK_MESSAGE(doc->get_errors().size() == 0, doc->get_errors()[0].msg);
CHECK_MESSAGE(doc->get_warnings().size() == 0, doc->get_warnings()[0].msg);
}

TEST_CASE("Ternary operator with clock and integer")
{
auto doc = document_fixture{}
.add_global_decl("clock c; double x; void f() { x = true? c : 1; }")
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
.add_default_process()
.parse();
CHECK(doc->get_errors().size() == 1);
CHECK_MESSAGE(doc->get_warnings().size() == 0, doc->get_warnings()[0].msg);
}

TEST_CASE("Ternary operator with clock and bool")
{
auto doc = document_fixture{}
.add_global_decl("clock c; double x; void f() { x = true? c : true; };")
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
.add_default_process()
.parse();
CHECK(doc->get_errors().size() == 1);
CHECK_MESSAGE(doc->get_warnings().size() == 0, doc->get_warnings()[0].msg);
}

TEST_CASE("Ternary operator with clock and clock")
{
auto doc = document_fixture{}
.add_global_decl("clock c; double x; void f() { x = true? c : c; }")
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
.add_default_process()
.parse();
CHECK_MESSAGE(doc->get_errors().size() == 0, doc->get_errors()[0].msg);
CHECK_MESSAGE(doc->get_warnings().size() == 0, doc->get_warnings()[0].msg);
}

TEST_CASE("Ternary operator with constant double")
{
auto doc = document_fixture{}
.add_global_decl("const double VAL = 2;")
.add_global_decl("double x; void f() { x = true? -VAL : VAL; }")
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
.add_default_process()
.parse();
CHECK_MESSAGE(doc->get_errors().size() == 0, doc->get_errors()[0].msg);
CHECK_MESSAGE(doc->get_warnings().size() == 0, doc->get_warnings()[0].msg);
}

TEST_CASE("Ternary operator with constant double and clock")
{
auto doc = document_fixture{}
.add_global_decl("const double VAL = 2;")
.add_global_decl("clock c;")
.add_global_decl("double x; void f() { x = true? -VAL : c; }")
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
.add_default_process()
.parse();
CHECK_MESSAGE(doc->get_errors().size() == 0, doc->get_errors()[0].msg);
CHECK_MESSAGE(doc->get_warnings().size() == 0, doc->get_warnings()[0].msg);
}
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved