[hlsl-writer] Emit break, return and continue.

This CL updates the HLSL writer to emit break, return and continue
statements.

Bug: tint:7
Change-Id: I03eafc343e57e9e8d1efaf930023099d6f85fc57
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25220
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 2ecae8b..372d8c3 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1019,7 +1019,10 @@
 source_set("tint_unittests_hlsl_writer_src") {
   sources = [
     "src/writer/hlsl/generator_impl_binary_test.cc",
+    "src/writer/hlsl/generator_impl_break_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",
     "src/writer/hlsl/generator_impl_test.cc",
     "src/writer/hlsl/namer_test.cc",
   ]
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e79bb7f..ca0f3ec 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -543,7 +543,10 @@
 if (${TINT_BUILD_HLSL_WRITER})
   list(APPEND TINT_TEST_SRCS
     writer/hlsl/generator_impl_binary_test.cc
+    writer/hlsl/generator_impl_break_test.cc
+    writer/hlsl/generator_impl_continue_test.cc
     writer/hlsl/generator_impl_identifier_test.cc
+    writer/hlsl/generator_impl_return_test.cc
     writer/hlsl/generator_impl_test.cc
     writer/hlsl/namer_test.cc
   )
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index 3b0af4e..076aa7b 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -16,6 +16,7 @@
 
 #include "src/ast/binary_expression.h"
 #include "src/ast/identifier_expression.h"
+#include "src/ast/return_statement.h"
 
 namespace tint {
 namespace writer {
@@ -137,6 +138,18 @@
   return true;
 }
 
+bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
+  make_indent();
+  out_ << "break;" << std::endl;
+  return true;
+}
+
+bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
+  make_indent();
+  out_ << "continue;" << std::endl;
+  return true;
+}
+
 bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
   if (expr->IsBinary()) {
     return EmitBinary(expr->AsBinary());
@@ -180,6 +193,41 @@
   return true;
 }
 
+bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
+  make_indent();
+
+  out_ << "return";
+
+  if (generating_entry_point_) {
+    auto out_data = ep_name_to_out_data_.find(current_ep_name_);
+    if (out_data != ep_name_to_out_data_.end()) {
+      out_ << " " << out_data->second.var_name;
+    }
+  } else if (stmt->has_value()) {
+    out_ << " ";
+    if (!EmitExpression(stmt->value())) {
+      return false;
+    }
+  }
+  out_ << ";" << std::endl;
+  return true;
+}
+
+bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
+  if (stmt->IsBreak()) {
+    return EmitBreak(stmt->AsBreak());
+  }
+  if (stmt->IsContinue()) {
+    return EmitContinue(stmt->AsContinue());
+  }
+  if (stmt->IsReturn()) {
+    return EmitReturn(stmt->AsReturn());
+  }
+
+  error_ = "unknown statement type: " + stmt->str();
+  return false;
+}
+
 }  // namespace hlsl
 }  // namespace writer
 }  // namespace tint
diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h
index 3f0e394..b6565ea 100644
--- a/src/writer/hlsl/generator_impl.h
+++ b/src/writer/hlsl/generator_impl.h
@@ -39,6 +39,14 @@
   /// @param expr the binary expression
   /// @returns true if the expression was emitted, false otherwise
   bool EmitBinary(ast::BinaryExpression* expr);
+  /// Handles a break statement
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was emitted successfully
+  bool EmitBreak(ast::BreakStatement* stmt);
+  /// Handles a continue statement
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was emitted successfully
+  bool EmitContinue(ast::ContinueStatement* stmt);
   /// Handles generate an Expression
   /// @param expr the expression
   /// @returns true if the expression was emitted
@@ -47,6 +55,14 @@
   /// @param expr the identifier expression
   /// @returns true if the identifeir was emitted
   bool EmitIdentifier(ast::IdentifierExpression* expr);
+  /// Handles return statements
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was successfully emitted
+  bool EmitReturn(ast::ReturnStatement* stmt);
+  /// Handles statement
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was emitted
+  bool EmitStatement(ast::Statement* stmt);
 
   /// Checks if the global variable is in an input or output struct
   /// @param var the variable to check
@@ -66,6 +82,7 @@
   Namer namer_;
   ast::Module* module_ = nullptr;
   std::string current_ep_name_;
+  bool generating_entry_point_ = false;
   ScopeStack<ast::Variable*> global_variables_;
   std::unordered_map<std::string, EntryPointData> ep_name_to_in_data_;
   std::unordered_map<std::string, EntryPointData> ep_name_to_out_data_;
diff --git a/src/writer/hlsl/generator_impl_break_test.cc b/src/writer/hlsl/generator_impl_break_test.cc
new file mode 100644
index 0000000..ad53672
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_break_test.cc
@@ -0,0 +1,44 @@
+// 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/break_statement.h"
+#include "src/ast/module.h"
+#include "src/writer/hlsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+
+using HlslGeneratorImplTest = testing::Test;
+
+TEST_F(HlslGeneratorImplTest, Emit_Break) {
+  ast::BreakStatement b;
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
+  EXPECT_EQ(g.result(), "  break;\n");
+}
+
+}  // namespace
+}  // namespace hlsl
+}  // namespace writer
+}  // namespace tint
diff --git a/src/writer/hlsl/generator_impl_continue_test.cc b/src/writer/hlsl/generator_impl_continue_test.cc
new file mode 100644
index 0000000..1aa0238
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_continue_test.cc
@@ -0,0 +1,44 @@
+// 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/continue_statement.h"
+#include "src/ast/module.h"
+#include "src/writer/hlsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+
+using HlslGeneratorImplTest = testing::Test;
+
+TEST_F(HlslGeneratorImplTest, Emit_Continue) {
+  ast::ContinueStatement c;
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
+  EXPECT_EQ(g.result(), "  continue;\n");
+}
+
+}  // namespace
+}  // namespace hlsl
+}  // namespace writer
+}  // namespace tint
diff --git a/src/writer/hlsl/generator_impl_return_test.cc b/src/writer/hlsl/generator_impl_return_test.cc
new file mode 100644
index 0000000..f160d3b
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_return_test.cc
@@ -0,0 +1,57 @@
+// 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/return_statement.h"
+#include "src/writer/hlsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+
+using HlslGeneratorImplTest = testing::Test;
+
+TEST_F(HlslGeneratorImplTest, Emit_Return) {
+  ast::ReturnStatement r;
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
+  EXPECT_EQ(g.result(), "  return;\n");
+}
+
+TEST_F(HlslGeneratorImplTest, Emit_ReturnWithValue) {
+  auto expr = std::make_unique<ast::IdentifierExpression>("expr");
+  ast::ReturnStatement r(std::move(expr));
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
+  EXPECT_EQ(g.result(), "  return expr;\n");
+}
+
+}  // namespace
+}  // namespace hlsl
+}  // namespace writer
+}  // namespace tint