Add else statement type determination.
This CL adds type determination for else statements.
Bug: tint:5
Change-Id: Ie859edbc08c191da0a04407c5f56b413a48f1791
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18832
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/type_determiner.cc b/src/type_determiner.cc
index 2e8ffbd..8cc2b8b 100644
--- a/src/type_determiner.cc
+++ b/src/type_determiner.cc
@@ -18,6 +18,7 @@
#include "src/ast/break_statement.h"
#include "src/ast/case_statement.h"
#include "src/ast/continue_statement.h"
+#include "src/ast/else_statement.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/type_constructor_expression.h"
@@ -90,6 +91,11 @@
auto c = stmt->AsContinue();
return DetermineResultType(c->conditional());
}
+ if (stmt->IsElse()) {
+ auto e = stmt->AsElse();
+ return DetermineResultType(e->condition()) &&
+ DetermineResultType(e->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 f5c54f1..9fe15dd 100644
--- a/src/type_determiner_test.cc
+++ b/src/type_determiner_test.cc
@@ -22,6 +22,7 @@
#include "src/ast/break_statement.h"
#include "src/ast/case_statement.h"
#include "src/ast/continue_statement.h"
+#include "src/ast/else_statement.h"
#include "src/ast/float_literal.h"
#include "src/ast/int_literal.h"
#include "src/ast/scalar_constructor_expression.h"
@@ -120,6 +121,35 @@
EXPECT_TRUE(cond_ptr->result_type()->IsI32());
}
+TEST_F(TypeDeterminerTest, Stmt_Else) {
+ 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::ElseStatement stmt(std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::IntLiteral>(&i32, 3)),
+ std::move(body));
+
+ EXPECT_TRUE(td()->DetermineResultType(&stmt));
+ ASSERT_NE(stmt.condition()->result_type(), nullptr);
+ ASSERT_NE(lhs_ptr->result_type(), nullptr);
+ ASSERT_NE(rhs_ptr->result_type(), nullptr);
+ EXPECT_TRUE(stmt.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(