[msl-writer] Add cast conversion.

This CL adds conversion of casts to MSL.

Bug: tint:8
Change-Id: Iecfb9a5b413b1d10372b4d2fec31c0956b1475a0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23822
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index ae14e27..e90aa05 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -888,6 +888,7 @@
     "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_cast_test.cc",
     "src/writer/msl/generator_impl_constructor_test.cc",
     "src/writer/msl/generator_impl_function_test.cc",
     "src/writer/msl/generator_impl_identifier_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b16315c..e1cd7a4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -496,6 +496,7 @@
     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_cast_test.cc
     writer/msl/generator_impl_constructor_test.cc
     writer/msl/generator_impl_function_test.cc
     writer/msl/generator_impl_identifier_test.cc
diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc
index b82090a..252e902 100644
--- a/src/writer/msl/generator_impl.cc
+++ b/src/writer/msl/generator_impl.cc
@@ -18,6 +18,7 @@
 #include "src/ast/assignment_statement.h"
 #include "src/ast/binary_expression.h"
 #include "src/ast/bool_literal.h"
+#include "src/ast/cast_expression.h"
 #include "src/ast/float_literal.h"
 #include "src/ast/function.h"
 #include "src/ast/identifier_expression.h"
@@ -141,8 +142,9 @@
       // implementation-defined behaviour for negative LHS.  We may have to
       // generate extra code to implement WGSL-specified behaviour for negative
       // LHS.
-      out_ << ">>";
+      out_ << R"(>>)";
       break;
+
     case ast::BinaryOp::kAdd:
       out_ << "+";
       break;
@@ -172,6 +174,19 @@
   return true;
 }
 
+bool GeneratorImpl::EmitCast(ast::CastExpression* expr) {
+  if (!EmitType(expr->type(), "")) {
+    return false;
+  }
+
+  out_ << "(";
+  if (!EmitExpression(expr->expr())) {
+    return false;
+  }
+  out_ << ")";
+  return true;
+}
+
 bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) {
   if (expr->IsScalarConstructor()) {
     return EmitScalarConstructor(expr->AsScalarConstructor());
@@ -239,6 +254,9 @@
   if (expr->IsBinary()) {
     return EmitBinary(expr->AsBinary());
   }
+  if (expr->IsCast()) {
+    return EmitCast(expr->AsCast());
+  }
   if (expr->IsConstructor()) {
     return EmitConstructor(expr->AsConstructor());
   }
diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h
index 3f1cba7..3c3b1b5c 100644
--- a/src/writer/msl/generator_impl.h
+++ b/src/writer/msl/generator_impl.h
@@ -52,6 +52,10 @@
   /// @param expr the binary expression
   /// @returns true if the expression was emitted, false otherwise
   bool EmitBinary(ast::BinaryExpression* expr);
+  /// Handles generating a cast expression
+  /// @param expr the cast expression
+  /// @returns true if the cast was emitted
+  bool EmitCast(ast::CastExpression* expr);
   /// Handles generating constructor expressions
   /// @param expr the constructor expression
   /// @returns true if the expression was emitted
diff --git a/src/writer/msl/generator_impl_cast_test.cc b/src/writer/msl/generator_impl_cast_test.cc
new file mode 100644
index 0000000..04dece4
--- /dev/null
+++ b/src/writer/msl/generator_impl_cast_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 "gtest/gtest.h"
+#include "src/ast/cast_expression.h"
+#include "src/ast/identifier_expression.h"
+#include "src/ast/type/f32_type.h"
+#include "src/ast/type/vector_type.h"
+#include "src/writer/msl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace msl {
+namespace {
+
+using MslGeneratorImplTest = testing::Test;
+
+TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Scalar) {
+  ast::type::F32Type f32;
+  auto id = std::make_unique<ast::IdentifierExpression>("id");
+  ast::CastExpression cast(&f32, std::move(id));
+
+  GeneratorImpl g;
+  ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
+  EXPECT_EQ(g.result(), "float(id)");
+}
+
+TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Vector) {
+  ast::type::F32Type f32;
+  ast::type::VectorType vec3(&f32, 3);
+
+  auto id = std::make_unique<ast::IdentifierExpression>("id");
+  ast::CastExpression cast(&vec3, std::move(id));
+
+  GeneratorImpl g;
+  ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
+  EXPECT_EQ(g.result(), "float3(id)");
+}
+
+}  // namespace
+}  // namespace msl
+}  // namespace writer
+}  // namespace tint