From 7be237aa8f620170cc357f7942eb966a152c018e Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 15 Jun 2020 20:55:09 +0000 Subject: [PATCH] Determine types for module scoped variable constructors. This CL updates the type determination code to determine the types of module scoped variable constructors. Bug: tint:89 Change-Id: Icd5d65409fcf17a0cbf0b34b9919f8d9e1577354 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23220 Reviewed-by: David Neto --- src/type_determiner.cc | 6 ++++++ src/type_determiner_test.cc | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/type_determiner.cc b/src/type_determiner.cc index cd22db21e2..5b1b0297b3 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc @@ -65,6 +65,12 @@ void TypeDeterminer::set_error(const Source& src, const std::string& msg) { bool TypeDeterminer::Determine() { for (const auto& var : mod_->global_variables()) { variable_stack_.set_global(var->name(), var.get()); + + if (var->has_constructor()) { + if (!DetermineResultType(var->constructor())) { + return false; + } + } } for (const auto& func : mod_->functions()) { diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc index 0b607f3af4..1587e9998b 100644 --- a/src/type_determiner_test.cc +++ b/src/type_determiner_test.cc @@ -353,6 +353,21 @@ TEST_F(TypeDeterminerTest, Stmt_VariableDecl) { EXPECT_TRUE(init_ptr->result_type()->IsI32()); } +TEST_F(TypeDeterminerTest, Stmt_VariableDecl_ModuleScope) { + 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(); + + mod()->AddGlobalVariable(std::move(var)); + + EXPECT_TRUE(td()->Determine()); + ASSERT_NE(init_ptr->result_type(), nullptr); + EXPECT_TRUE(init_ptr->result_type()->IsI32()); +} + TEST_F(TypeDeterminerTest, Expr_Error_Unknown) { FakeExpr e; e.set_source(Source{2, 30});