[hlsl-writer] Emit discard.

This CL adds the discard statement to the HLSL writer.

Bug: tint:7, tint:166
Change-Id: I292e9b97a1246c9b79a9660ec229ff7855aeb2a9
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25842
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 9388949..c47c3d3 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1049,6 +1049,7 @@
     "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_discard_test.cc",
     "src/writer/hlsl/generator_impl_identifier_test.cc",
     "src/writer/hlsl/generator_impl_return_test.cc",
     "src/writer/hlsl/generator_impl_switch_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ab5c4fa..7f07d7c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -557,6 +557,7 @@
     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_discard_test.cc
     writer/hlsl/generator_impl_identifier_test.cc
     writer/hlsl/generator_impl_return_test.cc
     writer/hlsl/generator_impl_switch_test.cc
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index 089c764..126be19 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -325,6 +325,14 @@
   return true;
 }
 
+bool GeneratorImpl::EmitDiscard(ast::DiscardStatement*) {
+  make_indent();
+  // TODO(dsinclair): Verify this is correct when the discard semantics are
+  // defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361)
+  out_ << "discard;" << std::endl;
+  return true;
+}
+
 bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
   if (expr->IsArrayAccessor()) {
     return EmitArrayAccessor(expr->AsArrayAccessor());
@@ -450,6 +458,9 @@
   if (stmt->IsContinue()) {
     return EmitContinue(stmt->AsContinue());
   }
+  if (stmt->IsDiscard()) {
+    return EmitDiscard(stmt->AsDiscard());
+  }
   if (stmt->IsFallthrough()) {
     make_indent();
     out_ << "/* fallthrough */" << std::endl;
diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h
index b8b1743..6d8856e 100644
--- a/src/writer/hlsl/generator_impl.h
+++ b/src/writer/hlsl/generator_impl.h
@@ -66,6 +66,10 @@
   /// @param expr the constructor expression
   /// @returns true if the expression was emitted
   bool EmitConstructor(ast::ConstructorExpression* expr);
+  /// Handles generating a discard statement
+  /// @param stmt the discard statement
+  /// @returns true if the statement was successfully emitted
+  bool EmitDiscard(ast::DiscardStatement* stmt);
   /// Handles generating a scalar constructor
   /// @param expr the scalar constructor expression
   /// @returns true if the scalar constructor is emitted
diff --git a/src/writer/hlsl/generator_impl_discard_test.cc b/src/writer/hlsl/generator_impl_discard_test.cc
new file mode 100644
index 0000000..4dfa099
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_discard_test.cc
@@ -0,0 +1,41 @@
+// 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/discard_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_Discard) {
+  ast::DiscardStatement stmt;
+
+  ast::Module m;
+  GeneratorImpl g(&m);
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
+  EXPECT_EQ(g.result(), "  discard;\n");
+}
+
+}  // namespace
+}  // namespace hlsl
+}  // namespace writer
+}  // namespace tint