This CL adds type determination for the variable declaration statements.
This Cl adds variable declaration type determination.
Bug: tint:5
Change-Id: I3dc19af470f9403dadc230f1bf2b597bb1fe4fee
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18840
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/type_determiner.cc b/src/type_determiner.cc
index 88cf4f6..b5b9ff1 100644
--- a/src/type_determiner.cc
+++ b/src/type_determiner.cc
@@ -27,6 +27,7 @@
#include "src/ast/switch_statement.h"
#include "src/ast/type_constructor_expression.h"
#include "src/ast/unless_statement.h"
+#include "src/ast/variable_decl_statement.h"
namespace tint {
@@ -156,6 +157,11 @@
return DetermineResultType(u->condition()) &&
DetermineResultType(u->body());
}
+ if (stmt->IsVariableDecl()) {
+ auto v = stmt->AsVariableDecl();
+ variable_stack_.set(v->variable()->name(), v->variable());
+ return DetermineResultType(v->variable()->constructor());
+ }
error_ = "unknown statement type for type determination";
return false;
diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc
index a683728..da8c7df 100644
--- a/src/type_determiner_test.cc
+++ b/src/type_determiner_test.cc
@@ -36,6 +36,7 @@
#include "src/ast/type/vector_type.h"
#include "src/ast/type_constructor_expression.h"
#include "src/ast/unless_statement.h"
+#include "src/ast/variable_decl_statement.h"
namespace tint {
namespace {
@@ -359,6 +360,21 @@
EXPECT_TRUE(rhs_ptr->result_type()->IsF32());
}
+TEST_F(TypeDeterminerTest, Stmt_VariableDecl) {
+ ast::type::I32Type i32;
+ auto var =
+ std::make_unique<ast::Variable>("my_var", ast::StorageClass::kNone, &i32);
+ var->set_constructor(std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::IntLiteral>(&i32, 2)));
+ auto init_ptr = var->constructor();
+
+ ast::VariableDeclStatement decl(std::move(var));
+
+ EXPECT_TRUE(td()->DetermineResultType(&decl));
+ ASSERT_NE(init_ptr->result_type(), nullptr);
+ EXPECT_TRUE(init_ptr->result_type()->IsI32());
+}
+
TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) {
ast::type::F32Type f32;
ast::ScalarConstructorExpression s(