Add type determination for the unless statement
This CL adds type determination for Unless statements.
Bug: tint:5
Change-Id: I0e8721d3c282ca8dcf9e7954f309c14e437b5272
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18839
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/type_determiner.cc b/src/type_determiner.cc
index 96747b6..88cf4f6 100644
--- a/src/type_determiner.cc
+++ b/src/type_determiner.cc
@@ -26,6 +26,7 @@
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/switch_statement.h"
#include "src/ast/type_constructor_expression.h"
+#include "src/ast/unless_statement.h"
namespace tint {
@@ -150,6 +151,11 @@
}
return true;
}
+ if (stmt->IsUnless()) {
+ auto u = stmt->AsUnless();
+ return DetermineResultType(u->condition()) &&
+ DetermineResultType(u->body());
+ }
error_ = "unknown statement type for type determination";
return false;
diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc
index ee23762..a683728 100644
--- a/src/type_determiner_test.cc
+++ b/src/type_determiner_test.cc
@@ -35,6 +35,7 @@
#include "src/ast/type/i32_type.h"
#include "src/ast/type/vector_type.h"
#include "src/ast/type_constructor_expression.h"
+#include "src/ast/unless_statement.h"
namespace tint {
namespace {
@@ -328,6 +329,36 @@
EXPECT_TRUE(rhs_ptr->result_type()->IsF32());
}
+TEST_F(TypeDeterminerTest, Stmt_Unless) {
+ ast::type::I32Type i32;
+ ast::type::F32Type f32;
+
+ auto lhs = std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::IntLiteral>(&i32, 2));
+ auto lhs_ptr = lhs.get();
+
+ auto rhs = std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
+ auto rhs_ptr = rhs.get();
+
+ ast::StatementList body;
+ body.push_back(std::make_unique<ast::AssignmentStatement>(std::move(lhs),
+ std::move(rhs)));
+
+ ast::UnlessStatement unless(
+ std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::IntLiteral>(&i32, 3)),
+ std::move(body));
+
+ EXPECT_TRUE(td()->DetermineResultType(&unless));
+ ASSERT_NE(unless.condition()->result_type(), nullptr);
+ ASSERT_NE(lhs_ptr->result_type(), nullptr);
+ ASSERT_NE(rhs_ptr->result_type(), nullptr);
+ EXPECT_TRUE(unless.condition()->result_type()->IsI32());
+ EXPECT_TRUE(lhs_ptr->result_type()->IsI32());
+ EXPECT_TRUE(rhs_ptr->result_type()->IsF32());
+}
+
TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) {
ast::type::F32Type f32;
ast::ScalarConstructorExpression s(