[msl-writer] Add break and continue emission.

This CL adds writting of break and continue statements to the MSL
backend.

Bug: tint:8
Change-Id: If2443c036fa82f54708d209d924192ee7a159e76
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24121
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 5c67ee8..4643fca 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -889,8 +889,10 @@
     "src/writer/msl/generator_impl_as_test.cc",
     "src/writer/msl/generator_impl_assign_test.cc",
     "src/writer/msl/generator_impl_binary_test.cc",
+    "src/writer/msl/generator_impl_break_test.cc",
     "src/writer/msl/generator_impl_cast_test.cc",
     "src/writer/msl/generator_impl_constructor_test.cc",
+    "src/writer/msl/generator_impl_continue_test.cc",
     "src/writer/msl/generator_impl_function_test.cc",
     "src/writer/msl/generator_impl_identifier_test.cc",
     "src/writer/msl/generator_impl_if_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ab01566..5a64fba 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -497,8 +497,10 @@
     writer/msl/generator_impl_as_test.cc
     writer/msl/generator_impl_assign_test.cc
     writer/msl/generator_impl_binary_test.cc
+    writer/msl/generator_impl_break_test.cc
     writer/msl/generator_impl_cast_test.cc
     writer/msl/generator_impl_constructor_test.cc
+    writer/msl/generator_impl_continue_test.cc
     writer/msl/generator_impl_function_test.cc
     writer/msl/generator_impl_identifier_test.cc
     writer/msl/generator_impl_if_test.cc
diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc
index 8d05206..04eba00 100644
--- a/src/writer/msl/generator_impl.cc
+++ b/src/writer/msl/generator_impl.cc
@@ -19,7 +19,9 @@
 #include "src/ast/assignment_statement.h"
 #include "src/ast/binary_expression.h"
 #include "src/ast/bool_literal.h"
+#include "src/ast/break_statement.h"
 #include "src/ast/cast_expression.h"
+#include "src/ast/continue_statement.h"
 #include "src/ast/else_statement.h"
 #include "src/ast/float_literal.h"
 #include "src/ast/function.h"
@@ -213,6 +215,12 @@
   return true;
 }
 
+bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
+  make_indent();
+  out_ << "break;" << std::endl;
+  return true;
+}
+
 bool GeneratorImpl::EmitCast(ast::CastExpression* expr) {
   if (!EmitType(expr->type(), "")) {
     return false;
@@ -233,6 +241,12 @@
   return EmitTypeConstructor(expr->AsTypeConstructor());
 }
 
+bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
+  make_indent();
+  out_ << "continue;" << std::endl;
+  return true;
+}
+
 bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
   if (!EmitType(expr->type(), "")) {
     return false;
@@ -493,6 +507,12 @@
   if (stmt->IsAssign()) {
     return EmitAssign(stmt->AsAssign());
   }
+  if (stmt->IsBreak()) {
+    return EmitBreak(stmt->AsBreak());
+  }
+  if (stmt->IsContinue()) {
+    return EmitContinue(stmt->AsContinue());
+  }
   if (stmt->IsIf()) {
     return EmitIf(stmt->AsIf());
   }
diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h
index e27a071..77391b5 100644
--- a/src/writer/msl/generator_impl.h
+++ b/src/writer/msl/generator_impl.h
@@ -60,6 +60,10 @@
   /// @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 generating a cast expression
   /// @param expr the cast expression
   /// @returns true if the cast was emitted
@@ -68,6 +72,10 @@
   /// @param expr the constructor expression
   /// @returns true if the expression was emitted
   bool EmitConstructor(ast::ConstructorExpression* expr);
+  /// Handles a continue statement
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was emitted successfully
+  bool EmitContinue(ast::ContinueStatement* stmt);
   /// Handles generating an else statement
   /// @param stmt the statement to emit
   /// @returns true if the statement was emitted
diff --git a/src/writer/msl/generator_impl_break_test.cc b/src/writer/msl/generator_impl_break_test.cc
new file mode 100644
index 0000000..8f33da0
--- /dev/null
+++ b/src/writer/msl/generator_impl_break_test.cc
@@ -0,0 +1,42 @@
+// 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/writer/msl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace msl {
+namespace {
+
+using MslGeneratorImplTest = testing::Test;
+
+TEST_F(MslGeneratorImplTest, Emit_Break) {
+  ast::BreakStatement b;
+
+  GeneratorImpl g;
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
+  EXPECT_EQ(g.result(), "  break;\n");
+}
+
+}  // namespace
+}  // namespace msl
+}  // namespace writer
+}  // namespace tint
diff --git a/src/writer/msl/generator_impl_continue_test.cc b/src/writer/msl/generator_impl_continue_test.cc
new file mode 100644
index 0000000..550c29f
--- /dev/null
+++ b/src/writer/msl/generator_impl_continue_test.cc
@@ -0,0 +1,42 @@
+// 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/writer/msl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace msl {
+namespace {
+
+using MslGeneratorImplTest = testing::Test;
+
+TEST_F(MslGeneratorImplTest, Emit_Continue) {
+  ast::ContinueStatement c;
+
+  GeneratorImpl g;
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
+  EXPECT_EQ(g.result(), "  continue;\n");
+}
+
+}  // namespace
+}  // namespace msl
+}  // namespace writer
+}  // namespace tint