[msl-writer] Add unary op emission.

This CL adds emission of the unary `-` and `!` operators.

Bug: tint:8
Change-Id: I9dda066111cc8f115b593127cf070c6ca37bdc66
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23842
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 4c1a7b2..69cf8b8 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -896,6 +896,7 @@
     "src/writer/msl/generator_impl_return_test.cc",
     "src/writer/msl/generator_impl_test.cc",
     "src/writer/msl/generator_impl_type_test.cc",
+    "src/writer/msl/generator_impl_unary_op_test.cc",
   ]
 
   configs += [
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 75136f2..fbf8a75 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -504,6 +504,7 @@
     writer/msl/generator_impl_return_test.cc
     writer/msl/generator_impl_test.cc
     writer/msl/generator_impl_type_test.cc
+    writer/msl/generator_impl_unary_op_test.cc
   )
 endif()
 
diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc
index 23171dc..7935c46 100644
--- a/src/writer/msl/generator_impl.cc
+++ b/src/writer/msl/generator_impl.cc
@@ -38,6 +38,7 @@
 #include "src/ast/type/vector_type.h"
 #include "src/ast/type/void_type.h"
 #include "src/ast/uint_literal.h"
+#include "src/ast/unary_op_expression.h"
 
 namespace tint {
 namespace writer {
@@ -265,6 +266,9 @@
   if (expr->IsIdentifier()) {
     return EmitIdentifier(expr->AsIdentifier());
   }
+  if (expr->IsUnaryOp()) {
+    return EmitUnaryOp(expr->AsUnaryOp());
+  }
 
   error_ = "unknown expression type";
   return false;
@@ -535,6 +539,26 @@
   return true;
 }
 
+bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) {
+  switch (expr->op()) {
+    case ast::UnaryOp::kNot:
+      out_ << "!";
+      break;
+    case ast::UnaryOp::kNegation:
+      out_ << "-";
+      break;
+  }
+  out_ << "(";
+
+  if (!EmitExpression(expr->expr())) {
+    return false;
+  }
+
+  out_ << ")";
+
+  return true;
+}
+
 }  // namespace msl
 }  // namespace writer
 }  // namespace tint
diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h
index 0a3fc76..319690e 100644
--- a/src/writer/msl/generator_impl.h
+++ b/src/writer/msl/generator_impl.h
@@ -120,6 +120,10 @@
   /// @param expr the type constructor expression
   /// @returns true if the constructor is emitted
   bool EmitTypeConstructor(ast::TypeConstructorExpression* expr);
+  /// Handles a unary op expression
+  /// @param expr the expression to emit
+  /// @returns true if the expression was emitted
+  bool EmitUnaryOp(ast::UnaryOpExpression* expr);
 
  private:
   const ast::Module* module_ = nullptr;
diff --git a/src/writer/msl/generator_impl_unary_op_test.cc b/src/writer/msl/generator_impl_unary_op_test.cc
new file mode 100644
index 0000000..6a01597
--- /dev/null
+++ b/src/writer/msl/generator_impl_unary_op_test.cc
@@ -0,0 +1,56 @@
+// 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/unary_op_expression.h"
+#include "src/writer/msl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace msl {
+namespace {
+
+struct UnaryOpData {
+  const char* name;
+  ast::UnaryOp op;
+};
+inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
+  out << data.op;
+  return out;
+}
+using MslUnaryOpTest = testing::TestWithParam<UnaryOpData>;
+TEST_P(MslUnaryOpTest, Emit) {
+  auto params = GetParam();
+
+  auto expr = std::make_unique<ast::IdentifierExpression>("expr");
+  ast::UnaryOpExpression op(params.op, std::move(expr));
+
+  GeneratorImpl g;
+  ASSERT_TRUE(g.EmitExpression(&op)) << g.error();
+  EXPECT_EQ(g.result(), std::string(params.name) + "(expr)");
+}
+INSTANTIATE_TEST_SUITE_P(MslGeneratorImplTest,
+                         MslUnaryOpTest,
+                         testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
+                                         UnaryOpData{"-",
+                                                     ast::UnaryOp::kNegation}));
+
+}  // namespace
+}  // namespace msl
+}  // namespace writer
+}  // namespace tint
diff --git a/src/writer/wgsl/generator_impl_unary_op_test.cc b/src/writer/wgsl/generator_impl_unary_op_test.cc
index 73d835d..44bc4a2 100644
--- a/src/writer/wgsl/generator_impl_unary_op_test.cc
+++ b/src/writer/wgsl/generator_impl_unary_op_test.cc
@@ -33,8 +33,8 @@
   out << data.op;
   return out;
 }
-using UnaryOpTest = testing::TestWithParam<UnaryOpData>;
-TEST_P(UnaryOpTest, Emit) {
+using WgslUnaryOpTest = testing::TestWithParam<UnaryOpData>;
+TEST_P(WgslUnaryOpTest, Emit) {
   auto params = GetParam();
 
   auto expr = std::make_unique<ast::IdentifierExpression>("expr");
@@ -45,7 +45,7 @@
   EXPECT_EQ(g.result(), std::string(params.name) + "(expr)");
 }
 INSTANTIATE_TEST_SUITE_P(WgslGeneratorImplTest,
-                         UnaryOpTest,
+                         WgslUnaryOpTest,
                          testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
                                          UnaryOpData{"-",
                                                      ast::UnaryOp::kNegation}));