Add case type determination
This CL adds type determination for case statements.
Bug: tint:5
Change-Id: I353232bd68a524a09de60fa73256b51d2be51c57
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18830
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/type_determiner.cc b/src/type_determiner.cc
index 8eed309..8418998 100644
--- a/src/type_determiner.cc
+++ b/src/type_determiner.cc
@@ -16,6 +16,7 @@
#include "src/ast/assignment_statement.h"
#include "src/ast/break_statement.h"
+#include "src/ast/case_statement.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/type_constructor_expression.h"
@@ -80,6 +81,10 @@
auto b = stmt->AsBreak();
return DetermineResultType(b->conditional());
}
+ if (stmt->IsCase()) {
+ auto c = stmt->AsCase();
+ return DetermineResultType(c->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 a4c8087..fb244d0 100644
--- a/src/type_determiner_test.cc
+++ b/src/type_determiner_test.cc
@@ -20,6 +20,7 @@
#include "gtest/gtest.h"
#include "src/ast/assignment_statement.h"
#include "src/ast/break_statement.h"
+#include "src/ast/case_statement.h"
#include "src/ast/float_literal.h"
#include "src/ast/int_literal.h"
#include "src/ast/scalar_constructor_expression.h"
@@ -78,6 +79,32 @@
EXPECT_TRUE(cond_ptr->result_type()->IsI32());
}
+TEST_F(TypeDeterminerTest, Stmt_Case) {
+ 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::CaseStatement cse(std::make_unique<ast::IntLiteral>(&i32, 3),
+ std::move(body));
+
+ EXPECT_TRUE(td()->DetermineResultType(&cse));
+ ASSERT_NE(lhs_ptr->result_type(), nullptr);
+ ASSERT_NE(rhs_ptr->result_type(), nullptr);
+ 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(