[msl-writer] Emitting of program constants.

This CL adds code to emit program constants in the MSL backend.

Bug: tint:8
Change-Id: I63e40983253349d2e293904fbe9b6f543b885b34
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24940
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 533f899..29fae26 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -863,6 +863,7 @@
     "src/writer/wgsl/generator_impl_kill_test.cc",
     "src/writer/wgsl/generator_impl_loop_test.cc",
     "src/writer/wgsl/generator_impl_member_accessor_test.cc",
+    "src/writer/wgsl/generator_impl_module_constant_test.cc",
     "src/writer/wgsl/generator_impl_return_test.cc",
     "src/writer/wgsl/generator_impl_switch_test.cc",
     "src/writer/wgsl/generator_impl_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d45e6cd..43033f9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -512,6 +512,7 @@
     writer/msl/generator_impl_kill_test.cc
     writer/msl/generator_impl_loop_test.cc
     writer/msl/generator_impl_member_accessor_test.cc
+    writer/msl/generator_impl_module_constant_test.cc
     writer/msl/generator_impl_return_test.cc
     writer/msl/generator_impl_switch_test.cc
     writer/msl/generator_impl_test.cc
diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc
index c68d12e..46ce013 100644
--- a/src/writer/msl/generator_impl.cc
+++ b/src/writer/msl/generator_impl.cc
@@ -118,6 +118,15 @@
     out_ << std::endl;
   }
 
+  for (const auto& var : module.global_variables()) {
+    if (!var->is_const()) {
+      continue;
+    }
+    if (!EmitProgramConstVariable(var.get())) {
+      return false;
+    }
+  }
+
   for (const auto& ep : module.entry_points()) {
     if (!EmitEntryPointData(ep.get())) {
       return false;
@@ -1467,6 +1476,37 @@
   return true;
 }
 
+bool GeneratorImpl::EmitProgramConstVariable(const ast::Variable* var) {
+  make_indent();
+
+  if (var->IsDecorated()) {
+    error_ = "Decorated const values not valid";
+    return false;
+  }
+  if (!var->is_const()) {
+    error_ = "Expected a const value";
+    return false;
+  }
+
+  out_ << "constant ";
+  if (!EmitType(var->type(), var->name())) {
+    return false;
+  }
+  if (!var->type()->IsArray()) {
+    out_ << " " << var->name();
+  }
+
+  if (var->constructor() != nullptr) {
+    out_ << " = ";
+    if (!EmitExpression(var->constructor())) {
+      return false;
+    }
+  }
+  out_ << ";" << std::endl;
+
+  return true;
+}
+
 }  // namespace msl
 }  // namespace writer
 }  // namespace tint
diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h
index e499392..df90697 100644
--- a/src/writer/msl/generator_impl.h
+++ b/src/writer/msl/generator_impl.h
@@ -195,6 +195,10 @@
   /// @param var the variable to generate
   /// @returns true if the variable was emitted
   bool EmitVariable(ast::Variable* var);
+  /// Handles generating a program scope constant variable
+  /// @param var the variable to emit
+  /// @returns true if the variable was emitted
+  bool EmitProgramConstVariable(const ast::Variable* var);
   /// Emits the zero value for the given type
   /// @param type the type to emit the value for
   /// @returns true if the zero value was successfully emitted.
diff --git a/src/writer/msl/generator_impl_module_constant_test.cc b/src/writer/msl/generator_impl_module_constant_test.cc
new file mode 100644
index 0000000..7f38a34
--- /dev/null
+++ b/src/writer/msl/generator_impl_module_constant_test.cc
@@ -0,0 +1,62 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <memory>
+#include <vector>
+
+#include "gtest/gtest.h"
+#include "src/ast/float_literal.h"
+#include "src/ast/scalar_constructor_expression.h"
+#include "src/ast/type/array_type.h"
+#include "src/ast/type/f32_type.h"
+#include "src/ast/type_constructor_expression.h"
+#include "src/ast/variable.h"
+#include "src/writer/msl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace msl {
+namespace {
+
+using MslGeneratorImplTest = testing::Test;
+
+TEST_F(MslGeneratorImplTest, Emit_ModuleConstant) {
+  ast::type::F32Type f32;
+  ast::type::ArrayType ary(&f32, 3);
+
+  ast::ExpressionList exprs;
+  exprs.push_back(std::make_unique<ast::ScalarConstructorExpression>(
+      std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
+  exprs.push_back(std::make_unique<ast::ScalarConstructorExpression>(
+      std::make_unique<ast::FloatLiteral>(&f32, 2.0f)));
+  exprs.push_back(std::make_unique<ast::ScalarConstructorExpression>(
+      std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
+
+  auto var =
+      std::make_unique<ast::Variable>("pos", ast::StorageClass::kNone, &ary);
+  var->set_is_const(true);
+  var->set_constructor(
+      std::make_unique<ast::TypeConstructorExpression>(&ary, std::move(exprs)));
+
+  GeneratorImpl g;
+  ASSERT_TRUE(g.EmitProgramConstVariable(var.get())) << g.error();
+  EXPECT_EQ(
+      g.result(),
+      "constant float pos[3] = {1.00000000f, 2.00000000f, 3.00000000f};\n");
+}
+
+}  // namespace
+}  // namespace msl
+}  // namespace writer
+}  // namespace tint