[hlsl-writer] Emit variable declarations.

This CL adds variable declarations to the HLSL backend.

Bug: tint:7
Change-Id: I5c1e42ca26029f1595bf4f23b3b867a492ddacc1
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25846
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index c2c957b..6401ca3 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1059,6 +1059,7 @@
     "src/writer/hlsl/generator_impl_test.cc",
     "src/writer/hlsl/generator_impl_type_test.cc",
     "src/writer/hlsl/generator_impl_unary_op_test.cc",
+    "src/writer/hlsl/generator_impl_variable_decl_statement_test.cc",
     "src/writer/hlsl/namer_test.cc",
   ]
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 3d5302a..54db20b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -567,6 +567,7 @@
     writer/hlsl/generator_impl_test.cc
     writer/hlsl/generator_impl_type_test.cc
     writer/hlsl/generator_impl_unary_op_test.cc
+    writer/hlsl/generator_impl_variable_decl_statement_test.cc
     writer/hlsl/namer_test.cc
   )
 endif()
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index aec13ec..035b335 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -36,6 +36,7 @@
 #include "src/ast/type/vector_type.h"
 #include "src/ast/uint_literal.h"
 #include "src/ast/unary_op_expression.h"
+#include "src/ast/variable_decl_statement.h"
 
 namespace tint {
 namespace writer {
@@ -544,6 +545,9 @@
   if (stmt->IsSwitch()) {
     return EmitSwitch(stmt->AsSwitch());
   }
+  if (stmt->IsVariableDecl()) {
+    return EmitVariable(stmt->AsVariableDecl()->variable());
+  }
 
   error_ = "unknown statement type: " + stmt->str();
   return false;
@@ -685,6 +689,36 @@
   return true;
 }
 
+bool GeneratorImpl::EmitVariable(ast::Variable* var) {
+  make_indent();
+
+  // TODO(dsinclair): Handle variable decorations
+  if (var->IsDecorated()) {
+    error_ = "Variable decorations are not handled yet";
+    return false;
+  }
+
+  if (var->is_const()) {
+    out_ << "const ";
+  }
+  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 hlsl
 }  // namespace writer
 }  // namespace tint
diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h
index d2cb4f0..8c60eb2 100644
--- a/src/writer/hlsl/generator_impl.h
+++ b/src/writer/hlsl/generator_impl.h
@@ -139,6 +139,10 @@
   /// @param type the type to emit the value for
   /// @returns true if the zero value was successfully emitted.
   bool EmitZeroValue(ast::type::Type* type);
+  /// Handles generating a variable
+  /// @param var the variable to generate
+  /// @returns true if the variable was emitted
+  bool EmitVariable(ast::Variable* var);
 
   /// Checks if the global variable is in an input or output struct
   /// @param var the variable to check
diff --git a/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc b/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc
new file mode 100644
index 0000000..961beef
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc
@@ -0,0 +1,154 @@
+// 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/identifier_expression.h"
+#include "src/ast/module.h"
+#include "src/ast/type/array_type.h"
+#include "src/ast/type/f32_type.h"
+#include "src/ast/type/vector_type.h"
+#include "src/ast/variable.h"
+#include "src/ast/variable_decl_statement.h"
+#include "src/writer/hlsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+
+using HlslGeneratorImplTest = testing::Test;
+
+TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement) {
+  ast::type::F32Type f32;
+  auto var =
+      std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
+
+  ast::VariableDeclStatement stmt(std::move(var));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
+  EXPECT_EQ(g.result(), "  float a;\n");
+}
+
+TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Const) {
+  ast::type::F32Type f32;
+  auto var =
+      std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
+  var->set_is_const(true);
+
+  ast::VariableDeclStatement stmt(std::move(var));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
+  EXPECT_EQ(g.result(), "  const float a;\n");
+}
+
+TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
+  ast::type::F32Type f32;
+  ast::type::ArrayType ary(&f32, 5);
+
+  auto var =
+      std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &ary);
+
+  ast::VariableDeclStatement stmt(std::move(var));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
+  EXPECT_EQ(g.result(), "  float a[5];\n");
+}
+
+TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Function) {
+  ast::type::F32Type f32;
+  auto var =
+      std::make_unique<ast::Variable>("a", ast::StorageClass::kFunction, &f32);
+
+  ast::VariableDeclStatement stmt(std::move(var));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
+  EXPECT_EQ(g.result(), "  float a;\n");
+}
+
+TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
+  ast::type::F32Type f32;
+  auto var =
+      std::make_unique<ast::Variable>("a", ast::StorageClass::kPrivate, &f32);
+
+  ast::VariableDeclStatement stmt(std::move(var));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
+  EXPECT_EQ(g.result(), "  float a;\n");
+}
+
+TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
+  auto ident = std::make_unique<ast::IdentifierExpression>("initializer");
+
+  ast::type::F32Type f32;
+  auto var =
+      std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
+  var->set_constructor(std::move(ident));
+
+  ast::VariableDeclStatement stmt(std::move(var));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
+  EXPECT_EQ(g.result(), R"(float a = initializer;
+)");
+}
+
+TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
+  ast::type::F32Type f32;
+  ast::type::VectorType vec(&f32, 3);
+
+  ast::ExpressionList values;
+  auto zero_vec =
+      std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(values));
+
+  auto var =
+      std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &vec);
+  var->set_constructor(std::move(zero_vec));
+
+  ast::VariableDeclStatement stmt(std::move(var));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
+  EXPECT_EQ(g.result(), R"(vector<float, 3> a = vector<float, 3>(0.0f);
+)");
+}
+
+}  // namespace
+}  // namespace hlsl
+}  // namespace writer
+}  // namespace tint
diff --git a/src/writer/msl/generator_impl_variable_decl_statement_test.cc b/src/writer/msl/generator_impl_variable_decl_statement_test.cc
index 043ef7a..8e14e4d 100644
--- a/src/writer/msl/generator_impl_variable_decl_statement_test.cc
+++ b/src/writer/msl/generator_impl_variable_decl_statement_test.cc
@@ -47,6 +47,22 @@
   EXPECT_EQ(g.result(), "  float a;\n");
 }
 
+TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Const) {
+  ast::type::F32Type f32;
+  auto var =
+      std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
+  var->set_is_const(true);
+
+  ast::VariableDeclStatement stmt(std::move(var));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
+  EXPECT_EQ(g.result(), "  const float a;\n");
+}
+
 TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
   ast::type::F32Type f32;
   ast::type::ArrayType ary(&f32, 5);