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>
This commit is contained in:
dan sinclair 2020-04-07 12:54:37 +00:00 committed by dan sinclair
parent aec965e4a1
commit 0cf685f728
2 changed files with 36 additions and 0 deletions

View File

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

View File

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