Add type determination for switch statements.
This CL adds the type determination code for the switch statements. Bug: tint:5 Change-Id: I96efd96016c03137ef471e3f220e21e7f53d11ba Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18838 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
bf0fff8438
commit
18b3285b5b
|
@ -24,6 +24,7 @@
|
||||||
#include "src/ast/regardless_statement.h"
|
#include "src/ast/regardless_statement.h"
|
||||||
#include "src/ast/return_statement.h"
|
#include "src/ast/return_statement.h"
|
||||||
#include "src/ast/scalar_constructor_expression.h"
|
#include "src/ast/scalar_constructor_expression.h"
|
||||||
|
#include "src/ast/switch_statement.h"
|
||||||
#include "src/ast/type_constructor_expression.h"
|
#include "src/ast/type_constructor_expression.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
|
@ -125,6 +126,9 @@ bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) {
|
||||||
return DetermineResultType(l->body()) &&
|
return DetermineResultType(l->body()) &&
|
||||||
DetermineResultType(l->continuing());
|
DetermineResultType(l->continuing());
|
||||||
}
|
}
|
||||||
|
if (stmt->IsNop()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if (stmt->IsRegardless()) {
|
if (stmt->IsRegardless()) {
|
||||||
auto r = stmt->AsRegardless();
|
auto r = stmt->AsRegardless();
|
||||||
return DetermineResultType(r->condition()) &&
|
return DetermineResultType(r->condition()) &&
|
||||||
|
@ -134,7 +138,16 @@ bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) {
|
||||||
auto r = stmt->AsReturn();
|
auto r = stmt->AsReturn();
|
||||||
return DetermineResultType(r->value());
|
return DetermineResultType(r->value());
|
||||||
}
|
}
|
||||||
if (stmt->IsNop()) {
|
if (stmt->IsSwitch()) {
|
||||||
|
auto s = stmt->AsSwitch();
|
||||||
|
if (!DetermineResultType(s->condition())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (const auto& case_stmt : s->body()) {
|
||||||
|
if (!DetermineResultType(case_stmt.get())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include "src/ast/regardless_statement.h"
|
#include "src/ast/regardless_statement.h"
|
||||||
#include "src/ast/return_statement.h"
|
#include "src/ast/return_statement.h"
|
||||||
#include "src/ast/scalar_constructor_expression.h"
|
#include "src/ast/scalar_constructor_expression.h"
|
||||||
|
#include "src/ast/switch_statement.h"
|
||||||
#include "src/ast/type/f32_type.h"
|
#include "src/ast/type/f32_type.h"
|
||||||
#include "src/ast/type/i32_type.h"
|
#include "src/ast/type/i32_type.h"
|
||||||
#include "src/ast/type/vector_type.h"
|
#include "src/ast/type/vector_type.h"
|
||||||
|
@ -293,6 +294,40 @@ TEST_F(TypeDeterminerTest, Stmt_Return) {
|
||||||
EXPECT_TRUE(cond_ptr->result_type()->IsI32());
|
EXPECT_TRUE(cond_ptr->result_type()->IsI32());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(TypeDeterminerTest, Stmt_Switch) {
|
||||||
|
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::CaseStatementList cases;
|
||||||
|
cases.push_back(std::make_unique<ast::CaseStatement>(
|
||||||
|
std::make_unique<ast::IntLiteral>(&i32, 3), std::move(body)));
|
||||||
|
|
||||||
|
ast::SwitchStatement stmt(std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
|
std::make_unique<ast::IntLiteral>(&i32, 2)),
|
||||||
|
std::move(cases));
|
||||||
|
|
||||||
|
EXPECT_TRUE(td()->DetermineResultType(&stmt)) << td()->error();
|
||||||
|
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) {
|
TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) {
|
||||||
ast::type::F32Type f32;
|
ast::type::F32Type f32;
|
||||||
ast::ScalarConstructorExpression s(
|
ast::ScalarConstructorExpression s(
|
||||||
|
|
Loading…
Reference in New Issue