[hlsl-writer] Add constructor emission.

This CL adds scalar and type constructor emission to the HLSL backend.

Bug: tint:7
Change-Id: I2b402b7eb66f266c3a111c9b07502ef17cc1a679
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25726
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index fa98013..1c9efe95 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1045,6 +1045,7 @@
     "src/writer/hlsl/generator_impl_binary_test.cc",
     "src/writer/hlsl/generator_impl_break_test.cc",
     "src/writer/hlsl/generator_impl_case_test.cc",
+    "src/writer/hlsl/generator_impl_constructor_test.cc",
     "src/writer/hlsl/generator_impl_continue_test.cc",
     "src/writer/hlsl/generator_impl_identifier_test.cc",
     "src/writer/hlsl/generator_impl_return_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 43932c3..21ac83c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -553,6 +553,7 @@
     writer/hlsl/generator_impl_binary_test.cc
     writer/hlsl/generator_impl_break_test.cc
     writer/hlsl/generator_impl_case_test.cc
+    writer/hlsl/generator_impl_constructor_test.cc
     writer/hlsl/generator_impl_continue_test.cc
     writer/hlsl/generator_impl_identifier_test.cc
     writer/hlsl/generator_impl_return_test.cc
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index 9807979..2eb0a18 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -233,6 +233,56 @@
   return true;
 }
 
+bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) {
+  if (expr->IsScalarConstructor()) {
+    return EmitScalarConstructor(expr->AsScalarConstructor());
+  }
+  return EmitTypeConstructor(expr->AsTypeConstructor());
+}
+
+bool GeneratorImpl::EmitScalarConstructor(
+    ast::ScalarConstructorExpression* expr) {
+  return EmitLiteral(expr->literal());
+}
+
+bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
+  if (expr->type()->IsArray()) {
+    out_ << "{";
+  } else {
+    if (!EmitType(expr->type(), "")) {
+      return false;
+    }
+    out_ << "(";
+  }
+
+  // If the type constructor is empty then we need to construct with the zero
+  // value for all components.
+  if (expr->values().empty()) {
+    if (!EmitZeroValue(expr->type())) {
+      return false;
+    }
+  } else {
+    bool first = true;
+    for (const auto& e : expr->values()) {
+      if (!first) {
+        out_ << ", ";
+      }
+      first = false;
+
+      if (!EmitExpression(e.get())) {
+        return false;
+      }
+    }
+  }
+
+  if (expr->type()->IsArray()) {
+    out_ << "}";
+  } else {
+    out_ << ")";
+  }
+  return true;
+}
+
 bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
   make_indent();
   out_ << "continue;" << std::endl;
@@ -243,6 +293,9 @@
   if (expr->IsBinary()) {
     return EmitBinary(expr->AsBinary());
   }
+  if (expr->IsConstructor()) {
+    return EmitConstructor(expr->AsConstructor());
+  }
   if (expr->IsIdentifier()) {
     return EmitIdentifier(expr->AsIdentifier());
   }
@@ -310,6 +363,24 @@
   return true;
 }
 
+bool GeneratorImpl::EmitZeroValue(ast::type::Type* type) {
+  if (type->IsBool()) {
+    out_ << "false";
+  } else if (type->IsF32()) {
+    out_ << "0.0f";
+  } else if (type->IsI32()) {
+    out_ << "0";
+  } else if (type->IsU32()) {
+    out_ << "0u";
+  } else if (type->IsVector()) {
+    return EmitZeroValue(type->AsVector()->type());
+  } else {
+    error_ = "Invalid type for zero emission: " + type->type_name();
+    return false;
+  }
+  return true;
+}
+
 bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
   make_indent();
 
diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h
index 896bf306..52cf3bb 100644
--- a/src/writer/hlsl/generator_impl.h
+++ b/src/writer/hlsl/generator_impl.h
@@ -17,6 +17,8 @@
 
 #include "src/ast/literal.h"
 #include "src/ast/module.h"
+#include "src/ast/scalar_constructor_expression.h"
+#include "src/ast/type_constructor_expression.h"
 #include "src/scope_stack.h"
 #include "src/writer/hlsl/namer.h"
 #include "src/writer/text_generator.h"
@@ -52,6 +54,18 @@
   /// @param stmt the statement
   /// @returns true if the statment was emitted successfully
   bool EmitCase(ast::CaseStatement* stmt);
+  /// Handles generating constructor expressions
+  /// @param expr the constructor expression
+  /// @returns true if the expression was emitted
+  bool EmitConstructor(ast::ConstructorExpression* expr);
+  /// Handles generating a scalar constructor
+  /// @param expr the scalar constructor expression
+  /// @returns true if the scalar constructor is emitted
+  bool EmitScalarConstructor(ast::ScalarConstructorExpression* expr);
+  /// Handles emitting a type constructor
+  /// @param expr the type constructor expression
+  /// @returns true if the constructor is emitted
+  bool EmitTypeConstructor(ast::TypeConstructorExpression* expr);
   /// Handles a continue statement
   /// @param stmt the statement to emit
   /// @returns true if the statement was emitted successfully
@@ -89,6 +103,10 @@
   /// @param expr the expression to emit
   /// @returns true if the expression was emitted
   bool EmitUnaryOp(ast::UnaryOpExpression* expr);
+  /// 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.
+  bool EmitZeroValue(ast::type::Type* type);
 
   /// 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_constructor_test.cc b/src/writer/hlsl/generator_impl_constructor_test.cc
new file mode 100644
index 0000000..b269bfd
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_constructor_test.cc
@@ -0,0 +1,273 @@
+// 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 "gtest/gtest.h"
+#include "src/ast/bool_literal.h"
+#include "src/ast/float_literal.h"
+#include "src/ast/module.h"
+#include "src/ast/scalar_constructor_expression.h"
+#include "src/ast/sint_literal.h"
+#include "src/ast/type/array_type.h"
+#include "src/ast/type/bool_type.h"
+#include "src/ast/type/f32_type.h"
+#include "src/ast/type/i32_type.h"
+#include "src/ast/type/matrix_type.h"
+#include "src/ast/type/u32_type.h"
+#include "src/ast/type/vector_type.h"
+#include "src/ast/type_constructor_expression.h"
+#include "src/ast/uint_literal.h"
+#include "src/writer/hlsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+
+using HlslGeneratorImplTest = testing::Test;
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Bool) {
+  ast::type::BoolType bool_type;
+  auto lit = std::make_unique<ast::BoolLiteral>(&bool_type, false);
+  ast::ScalarConstructorExpression expr(std::move(lit));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(), "false");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Int) {
+  ast::type::I32Type i32;
+  auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345);
+  ast::ScalarConstructorExpression expr(std::move(lit));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(), "-12345");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_UInt) {
+  ast::type::U32Type u32;
+  auto lit = std::make_unique<ast::UintLiteral>(&u32, 56779);
+  ast::ScalarConstructorExpression expr(std::move(lit));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(), "56779u");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Float) {
+  ast::type::F32Type f32;
+  auto lit = std::make_unique<ast::FloatLiteral>(&f32, 1.5e27);
+  ast::ScalarConstructorExpression expr(std::move(lit));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(), "1.49999995e+27f");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Float) {
+  ast::type::F32Type f32;
+
+  auto lit = std::make_unique<ast::FloatLiteral>(&f32, -1.2e-5);
+  ast::ExpressionList values;
+  values.push_back(
+      std::make_unique<ast::ScalarConstructorExpression>(std::move(lit)));
+
+  ast::TypeConstructorExpression expr(&f32, std::move(values));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(), "float(-1.20000004e-05f)");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Bool) {
+  ast::type::BoolType b;
+
+  auto lit = std::make_unique<ast::BoolLiteral>(&b, true);
+  ast::ExpressionList values;
+  values.push_back(
+      std::make_unique<ast::ScalarConstructorExpression>(std::move(lit)));
+
+  ast::TypeConstructorExpression expr(&b, std::move(values));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(), "bool(true)");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Int) {
+  ast::type::I32Type i32;
+
+  auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345);
+  ast::ExpressionList values;
+  values.push_back(
+      std::make_unique<ast::ScalarConstructorExpression>(std::move(lit)));
+
+  ast::TypeConstructorExpression expr(&i32, std::move(values));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(), "int(-12345)");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Uint) {
+  ast::type::U32Type u32;
+
+  auto lit = std::make_unique<ast::UintLiteral>(&u32, 12345);
+  ast::ExpressionList values;
+  values.push_back(
+      std::make_unique<ast::ScalarConstructorExpression>(std::move(lit)));
+
+  ast::TypeConstructorExpression expr(&u32, std::move(values));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(), "uint(12345u)");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Vec) {
+  ast::type::F32Type f32;
+  ast::type::VectorType vec(&f32, 3);
+
+  auto lit1 = std::make_unique<ast::FloatLiteral>(&f32, 1.f);
+  auto lit2 = std::make_unique<ast::FloatLiteral>(&f32, 2.f);
+  auto lit3 = std::make_unique<ast::FloatLiteral>(&f32, 3.f);
+  ast::ExpressionList values;
+  values.push_back(
+      std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
+  values.push_back(
+      std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
+  values.push_back(
+      std::make_unique<ast::ScalarConstructorExpression>(std::move(lit3)));
+
+  ast::TypeConstructorExpression expr(&vec, std::move(values));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(),
+            "vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f)");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Vec_Empty) {
+  ast::type::F32Type f32;
+  ast::type::VectorType vec(&f32, 3);
+
+  ast::ExpressionList values;
+  ast::TypeConstructorExpression expr(&vec, std::move(values));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(), "vector<float, 3>(0.0f)");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Mat) {
+  ast::type::F32Type f32;
+  ast::type::MatrixType mat(&f32, 3, 2);  // 3 ROWS, 2 COLUMNS
+  ast::type::VectorType vec(&f32, 3);
+
+  // WGSL matrix is mat2x3 (it flips for AST, sigh). With a type constructor
+  // of <vec3, vec3>
+
+  ast::ExpressionList mat_values;
+
+  for (size_t i = 0; i < 2; i++) {
+    auto lit1 = std::make_unique<ast::FloatLiteral>(
+        &f32, static_cast<float>(1 + (i * 2)));
+    auto lit2 = std::make_unique<ast::FloatLiteral>(
+        &f32, static_cast<float>(2 + (i * 2)));
+    auto lit3 = std::make_unique<ast::FloatLiteral>(
+        &f32, static_cast<float>(3 + (i * 2)));
+
+    ast::ExpressionList values;
+    values.push_back(
+        std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
+    values.push_back(
+        std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
+    values.push_back(
+        std::make_unique<ast::ScalarConstructorExpression>(std::move(lit3)));
+
+    mat_values.push_back(std::make_unique<ast::TypeConstructorExpression>(
+        &vec, std::move(values)));
+  }
+
+  ast::TypeConstructorExpression expr(&mat, std::move(mat_values));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+
+  // A matrix of type T with n columns and m rows can also be constructed from
+  // n vectors of type T with m components.
+  EXPECT_EQ(g.result(),
+            std::string("matrix<float, 3, 2>(vector<float, 3>(1.00000000f, "
+                        "2.00000000f, 3.00000000f), ") +
+                "vector<float, 3>(3.00000000f, 4.00000000f, 5.00000000f))");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Array) {
+  ast::type::F32Type f32;
+  ast::type::VectorType vec(&f32, 3);
+  ast::type::ArrayType ary(&vec, 3);
+
+  ast::ExpressionList ary_values;
+
+  for (size_t i = 0; i < 3; i++) {
+    auto lit1 = std::make_unique<ast::FloatLiteral>(
+        &f32, static_cast<float>(1 + (i * 3)));
+    auto lit2 = std::make_unique<ast::FloatLiteral>(
+        &f32, static_cast<float>(2 + (i * 3)));
+    auto lit3 = std::make_unique<ast::FloatLiteral>(
+        &f32, static_cast<float>(3 + (i * 3)));
+
+    ast::ExpressionList values;
+    values.push_back(
+        std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
+    values.push_back(
+        std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
+    values.push_back(
+        std::make_unique<ast::ScalarConstructorExpression>(std::move(lit3)));
+
+    ary_values.push_back(std::make_unique<ast::TypeConstructorExpression>(
+        &vec, std::move(values)));
+  }
+
+  ast::TypeConstructorExpression expr(&ary, std::move(ary_values));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
+  EXPECT_EQ(g.result(),
+            std::string("{") +
+                "vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), " +
+                "vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f), " +
+                "vector<float, 3>(7.00000000f, 8.00000000f, 9.00000000f)}");
+}
+
+// TODO(dsinclair): Add struct constructor test.
+TEST_F(HlslGeneratorImplTest, DISABLED_EmitConstructor_Type_Struct) {}
+
+}  // namespace
+}  // namespace hlsl
+}  // namespace writer
+}  // namespace tint