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 <dneto@google.com>
diff --git a/src/type_determiner.cc b/src/type_determiner.cc
index cd22db2..5b1b029 100644
--- a/src/type_determiner.cc
+++ b/src/type_determiner.cc
@@ -65,6 +65,12 @@
 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 0b607f3..1587e99 100644
--- a/src/type_determiner_test.cc
+++ b/src/type_determiner_test.cc
@@ -353,6 +353,21 @@
   EXPECT_TRUE(init_ptr->result_type()->IsI32());
 }
 
+TEST_F(TypeDeterminerTest, Stmt_VariableDecl_ModuleScope) {
+  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::SintLiteral>(&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});