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>
This commit is contained in:
dan sinclair 2020-04-07 12:56:57 +00:00 committed by dan sinclair
parent 18b3285b5b
commit 9a84e5eb47
2 changed files with 37 additions and 0 deletions

View File

@ -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;

View File

@ -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<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(