[hlsl-writer] Add BlockStatement support.

This CL adds support for BlockStatement to the HLSL backend.

Bug: tint:7
Change-Id: I953a1b85a05cd84e8e296d677204ae9b5a3ae669
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25860
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index c47c3d3..bd007cc 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1045,6 +1045,7 @@
     "src/writer/hlsl/generator_impl_array_accessor_test.cc",
     "src/writer/hlsl/generator_impl_assign_test.cc",
     "src/writer/hlsl/generator_impl_binary_test.cc",
+    "src/writer/hlsl/generator_impl_block_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",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7f07d7c..7af9c12 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -553,6 +553,7 @@
     writer/hlsl/generator_impl_array_accessor_test.cc
     writer/hlsl/generator_impl_assign_test.cc
     writer/hlsl/generator_impl_binary_test.cc
+    writer/hlsl/generator_impl_block_test.cc
     writer/hlsl/generator_impl_break_test.cc
     writer/hlsl/generator_impl_case_test.cc
     writer/hlsl/generator_impl_constructor_test.cc
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index 126be19..a431001 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -1,4 +1,4 @@
-// Copyright 2020 The Tint Authors.
+/// 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.
@@ -219,6 +219,40 @@
   return true;
 }
 
+bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) {
+  out_ << "{" << std::endl;
+  increment_indent();
+
+  for (const auto& s : *stmt) {
+    if (!EmitStatement(s.get())) {
+      return false;
+    }
+  }
+
+  decrement_indent();
+  make_indent();
+  out_ << "}";
+
+  return true;
+}
+
+bool GeneratorImpl::EmitBlockAndNewline(const ast::BlockStatement* stmt) {
+  const bool result = EmitBlock(stmt);
+  if (result) {
+    out_ << std::endl;
+  }
+  return result;
+}
+
+bool GeneratorImpl::EmitIndentedBlockAndNewline(ast::BlockStatement* stmt) {
+  make_indent();
+  const bool result = EmitBlock(stmt);
+  if (result) {
+    out_ << std::endl;
+  }
+  return result;
+}
+
 bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
   make_indent();
   out_ << "break;" << std::endl;
@@ -452,6 +486,9 @@
   if (stmt->IsAssign()) {
     return EmitAssign(stmt->AsAssign());
   }
+  if (stmt->IsBlock()) {
+    return EmitIndentedBlockAndNewline(stmt->AsBlock());
+  }
   if (stmt->IsBreak()) {
     return EmitBreak(stmt->AsBreak());
   }
diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h
index 6d8856e..24fadb8 100644
--- a/src/writer/hlsl/generator_impl.h
+++ b/src/writer/hlsl/generator_impl.h
@@ -54,6 +54,18 @@
   /// @param expr the binary expression
   /// @returns true if the expression was emitted, false otherwise
   bool EmitBinary(ast::BinaryExpression* expr);
+  /// Handles a block statement
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was emitted successfully
+  bool EmitBlock(const ast::BlockStatement* stmt);
+  /// Handles a block statement with a newline at the end
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was emitted successfully
+  bool EmitIndentedBlockAndNewline(ast::BlockStatement* stmt);
+  /// Handles a block statement with a newline at the end
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was emitted successfully
+  bool EmitBlockAndNewline(const ast::BlockStatement* stmt);
   /// Handles a break statement
   /// @param stmt the statement to emit
   /// @returns true if the statement was emitted successfully
diff --git a/src/writer/hlsl/generator_impl_block_test.cc b/src/writer/hlsl/generator_impl_block_test.cc
new file mode 100644
index 0000000..772274f
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_block_test.cc
@@ -0,0 +1,61 @@
+// 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 "gtest/gtest.h"
+#include "src/ast/block_statement.h"
+#include "src/ast/discard_statement.h"
+#include "src/writer/hlsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+
+using HlslGeneratorImplTest = testing::Test;
+
+TEST_F(HlslGeneratorImplTest, Emit_Block) {
+  ast::BlockStatement b;
+  b.append(std::make_unique<ast::DiscardStatement>());
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
+  EXPECT_EQ(g.result(), R"(  {
+    discard;
+  }
+)");
+}
+
+TEST_F(HlslGeneratorImplTest, Emit_Block_WithoutNewline) {
+  ast::BlockStatement b;
+  b.append(std::make_unique<ast::DiscardStatement>());
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitBlock(&b)) << g.error();
+  EXPECT_EQ(g.result(), R"({
+    discard;
+  })");
+}
+
+}  // namespace
+}  // namespace hlsl
+}  // namespace writer
+}  // namespace tint