From ca893e3200e7b7f9b893f2f893829a097cd59b85 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 7 Apr 2020 12:57:12 +0000 Subject: [PATCH] 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 --- src/type_determiner.cc | 6 ++++++ src/type_determiner_test.cc | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/type_determiner.cc b/src/type_determiner.cc index 88cf4f6c0c..b5b9ff1fc7 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 @@ bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) { 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 a683728715..da8c7dff9b 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 @@ TEST_F(TypeDeterminerTest, Stmt_Unless) { EXPECT_TRUE(rhs_ptr->result_type()->IsF32()); } +TEST_F(TypeDeterminerTest, Stmt_VariableDecl) { + ast::type::I32Type i32; + auto var = + std::make_unique("my_var", ast::StorageClass::kNone, &i32); + var->set_constructor(std::make_unique( + std::make_unique(&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(