From 9a84e5eb47d7e2c592ff1a9187b77064736f3bde Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 7 Apr 2020 12:56:57 +0000 Subject: [PATCH] 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 --- src/type_determiner.cc | 6 ++++++ src/type_determiner_test.cc | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/type_determiner.cc b/src/type_determiner.cc index 96747b60bf..88cf4f6c0c 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 @@ bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) { } 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 ee23762806..a683728715 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 @@ TEST_F(TypeDeterminerTest, Stmt_Switch) { 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( + std::make_unique(&i32, 2)); + auto lhs_ptr = lhs.get(); + + auto rhs = std::make_unique( + std::make_unique(&f32, 2.3f)); + auto rhs_ptr = rhs.get(); + + ast::StatementList body; + body.push_back(std::make_unique(std::move(lhs), + std::move(rhs))); + + ast::UnlessStatement unless( + std::make_unique( + std::make_unique(&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(