Emit WGSL continue statement.

This Cl adds support for emitting the WGSL continue statement.

Bug: tint:4
Change-Id: I20f87d46e707bdd7ae2a517983e779988892c445
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17200
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index df4045d..7d68e42 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -412,6 +412,7 @@
     writer/wgsl/generator_impl_call_test.cc
     writer/wgsl/generator_impl_case_test.cc
     writer/wgsl/generator_impl_cast_test.cc
+    writer/wgsl/generator_impl_continue_test.cc
     writer/wgsl/generator_impl_entry_point_test.cc
     writer/wgsl/generator_impl_identifier_test.cc
     writer/wgsl/generator_impl_import_test.cc
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index 4f58742..d0a3fb6 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -28,6 +28,7 @@
 #include "src/ast/case_statement.h"
 #include "src/ast/cast_expression.h"
 #include "src/ast/const_initializer_expression.h"
+#include "src/ast/continue_statement.h"
 #include "src/ast/decorated_variable.h"
 #include "src/ast/float_literal.h"
 #include "src/ast/identifier_expression.h"
@@ -656,6 +657,9 @@
   if (stmt->IsCase()) {
     return EmitCase(stmt->AsCase());
   }
+  if (stmt->IsContinue()) {
+    return EmitContinue(stmt->AsContinue());
+  }
 
   error_ = "unknown statement type";
   return false;
@@ -733,6 +737,31 @@
   return true;
 }
 
+bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
+  make_indent();
+
+  out_ << "continue";
+
+  if (stmt->condition() != ast::StatementCondition::kNone) {
+    out_ << " ";
+    if (stmt->condition() == ast::StatementCondition::kIf) {
+      out_ << "if";
+    } else {
+      out_ << "unless";
+    }
+
+    out_ << " (";
+    if (!EmitExpression(stmt->conditional())) {
+      return false;
+    }
+    out_ << ")";
+  }
+
+  out_ << ";" << std::endl;
+
+  return true;
+}
+
 }  // namespace wgsl
 }  // namespace writer
 }  // namespace tint
diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h
index 7b456a9..d69c32e 100644
--- a/src/writer/wgsl/generator_impl.h
+++ b/src/writer/wgsl/generator_impl.h
@@ -102,6 +102,10 @@
   /// @param expr the const initializer expression
   /// @returns true if the initializer is emitted
   bool EmitConstInitializer(ast::ConstInitializerExpression* 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 entry_point command
   /// @param ep the entry point
   /// @returns true if the entry point was emitted
diff --git a/src/writer/wgsl/generator_impl_continue_test.cc b/src/writer/wgsl/generator_impl_continue_test.cc
new file mode 100644
index 0000000..cb5b81b
--- /dev/null
+++ b/src/writer/wgsl/generator_impl_continue_test.cc
@@ -0,0 +1,65 @@
+// 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/identifier_expression.h"
+#include "src/writer/wgsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace wgsl {
+namespace {
+
+using GeneratorImplTest = testing::Test;
+
+TEST_F(GeneratorImplTest, Emit_Continue) {
+  ast::ContinueStatement c;
+
+  GeneratorImpl g;
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
+  EXPECT_EQ(g.result(), "  continue;\n");
+}
+
+TEST_F(GeneratorImplTest, Emit_ContinueWithIf) {
+  auto expr = std::make_unique<ast::IdentifierExpression>("expr");
+  ast::ContinueStatement c(ast::StatementCondition::kIf, std::move(expr));
+
+  GeneratorImpl g;
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
+  EXPECT_EQ(g.result(), "  continue if (expr);\n");
+}
+
+TEST_F(GeneratorImplTest, Emit_ContinueWithUnless) {
+  auto expr = std::make_unique<ast::IdentifierExpression>("expr");
+  ast::ContinueStatement c(ast::StatementCondition::kUnless, std::move(expr));
+
+  GeneratorImpl g;
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
+  EXPECT_EQ(g.result(), "  continue unless (expr);\n");
+}
+
+}  // namespace
+}  // namespace wgsl
+}  // namespace writer
+}  // namespace tint