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>
This commit is contained in:
dan sinclair 2020-04-07 12:54:20 +00:00 committed by dan sinclair
parent b7ea6e2f72
commit 6010b29f65
2 changed files with 32 additions and 0 deletions

View File

@ -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 @@ bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) {
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;

View File

@ -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 @@ TEST_F(TypeDeterminerTest, Stmt_Break) {
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(