Emit relation expressions.

This CL updates the WGSL writer to emit the relational expressions.

Bug: tint:4
Change-Id: I2b2f5e40f2091ce2b7715773f3811aed7ed61f6a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17100
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b818392..97c9a32 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -414,6 +414,7 @@
     writer/wgsl/generator_impl_import_test.cc
     writer/wgsl/generator_impl_initializer_test.cc
     writer/wgsl/generator_impl_member_accessor_test.cc
+    writer/wgsl/generator_impl_relational_test.cc
     writer/wgsl/generator_impl_type_test.cc
     writer/wgsl/generator_impl_variable_test.cc
   )
diff --git a/src/ast/relational_expression.h b/src/ast/relational_expression.h
index bc45d4f..a35733d 100644
--- a/src/ast/relational_expression.h
+++ b/src/ast/relational_expression.h
@@ -38,16 +38,6 @@
   kGreaterThan,
   kLessThanEqual,
   kGreaterThanEqual,
-  kUnordGreaterThan,
-  kUnordGreaterThanEqual,
-  kUnordLessThan,
-  kUnordLessThanEqual,
-  kUnordEqual,
-  kUnordNotEqual,
-  kSignedGreaterThan,
-  kSignedGreaterThanEqual,
-  kSignedLessThan,
-  kSignedLessThanEqual,
   kShiftLeft,
   kShiftRight,
   kShiftRightArith,
@@ -158,36 +148,6 @@
     case Relation::kGreaterThanEqual:
       out << "greater_than_equal";
       break;
-    case Relation::kUnordGreaterThan:
-      out << "unord_greater_than";
-      break;
-    case Relation::kUnordGreaterThanEqual:
-      out << "unord_greater_than_equal";
-      break;
-    case Relation::kUnordLessThan:
-      out << "unord_less_than";
-      break;
-    case Relation::kUnordLessThanEqual:
-      out << "unord_less_than_equal";
-      break;
-    case Relation::kUnordEqual:
-      out << "unord_equal";
-      break;
-    case Relation::kUnordNotEqual:
-      out << "unord_not_equal";
-      break;
-    case Relation::kSignedGreaterThan:
-      out << "signed_greateR_than";
-      break;
-    case Relation::kSignedGreaterThanEqual:
-      out << "signed_greater_than_equal";
-      break;
-    case Relation::kSignedLessThan:
-      out << "signed_less_than";
-      break;
-    case Relation::kSignedLessThanEqual:
-      out << "signed_less_than_equal";
-      break;
     case Relation::kShiftLeft:
       out << "shift_left";
       break;
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index b282c9e..f60126e 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -32,6 +32,7 @@
 #include "src/ast/int_literal.h"
 #include "src/ast/location_decoration.h"
 #include "src/ast/member_accessor_expression.h"
+#include "src/ast/relational_expression.h"
 #include "src/ast/set_decoration.h"
 #include "src/ast/struct.h"
 #include "src/ast/struct_member.h"
@@ -140,6 +141,9 @@
   if (expr->IsMemberAccessor()) {
     return EmitMemberAccessor(expr->AsMemberAccessor());
   }
+  if (expr->IsRelational()) {
+    return EmitRelational(expr->AsRelational());
+  }
 
   error_ = "unknown expression type";
   return false;
@@ -451,6 +455,86 @@
   return true;
 }
 
+bool GeneratorImpl::EmitRelational(ast::RelationalExpression* expr) {
+  out_ << "(";
+
+  if (!EmitExpression(expr->lhs())) {
+    return false;
+  }
+  out_ << " ";
+
+  switch (expr->relation()) {
+    case ast::Relation::kAnd:
+      out_ << "&";
+      break;
+    case ast::Relation::kOr:
+      out_ << "|";
+      break;
+    case ast::Relation::kXor:
+      out_ << "^";
+      break;
+    case ast::Relation::kLogicalAnd:
+      out_ << "&&";
+      break;
+    case ast::Relation::kLogicalOr:
+      out_ << "||";
+      break;
+    case ast::Relation::kEqual:
+      out_ << "==";
+      break;
+    case ast::Relation::kNotEqual:
+      out_ << "!=";
+      break;
+    case ast::Relation::kLessThan:
+      out_ << "<";
+      break;
+    case ast::Relation::kGreaterThan:
+      out_ << ">";
+      break;
+    case ast::Relation::kLessThanEqual:
+      out_ << "<=";
+      break;
+    case ast::Relation::kGreaterThanEqual:
+      out_ << ">=";
+      break;
+    case ast::Relation::kShiftLeft:
+      out_ << "<<";
+      break;
+    case ast::Relation::kShiftRight:
+      out_ << ">>";
+      break;
+    case ast::Relation::kShiftRightArith:
+      out_ << ">>>";
+      break;
+    case ast::Relation::kAdd:
+      out_ << "+";
+      break;
+    case ast::Relation::kSubtract:
+      out_ << "-";
+      break;
+    case ast::Relation::kMultiply:
+      out_ << "*";
+      break;
+    case ast::Relation::kDivide:
+      out_ << "/";
+      break;
+    case ast::Relation::kModulo:
+      out_ << "%";
+      break;
+    case ast::Relation::kNone:
+      error_ = "missing relation type";
+      return false;
+  }
+  out_ << " ";
+
+  if (!EmitExpression(expr->rhs())) {
+    return false;
+  }
+
+  out_ << ")";
+  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 f84ff84..698de59 100644
--- a/src/writer/wgsl/generator_impl.h
+++ b/src/writer/wgsl/generator_impl.h
@@ -114,6 +114,10 @@
   /// @param expr the member accessor expression
   /// @returns true if the member accessor was emitted
   bool EmitMemberAccessor(ast::MemberAccessorExpression* expr);
+  /// Handles generating a relational expression
+  /// @param expr the relational expression
+  /// @returns true if the expression was emitted, false otherwise
+  bool EmitRelational(ast::RelationalExpression* expr);
   /// Handles generating type
   /// @param type the type to generate
   /// @returns true if the type is emitted
diff --git a/src/writer/wgsl/generator_impl_relational_test.cc b/src/writer/wgsl/generator_impl_relational_test.cc
new file mode 100644
index 0000000..27d7370
--- /dev/null
+++ b/src/writer/wgsl/generator_impl_relational_test.cc
@@ -0,0 +1,76 @@
+// 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/identifier_expression.h"
+#include "src/ast/relational_expression.h"
+#include "src/writer/wgsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace wgsl {
+namespace {
+
+struct RelationData {
+  const char* result;
+  ast::Relation relation;
+};
+inline std::ostream& operator<<(std::ostream& out, RelationData data) {
+  out << data.relation;
+  return out;
+}
+using RelationTest = testing::TestWithParam<RelationData>;
+TEST_P(RelationTest, Emit) {
+  auto params = GetParam();
+
+  auto left = std::make_unique<ast::IdentifierExpression>("left");
+  auto right = std::make_unique<ast::IdentifierExpression>("right");
+
+  ast::RelationalExpression expr(params.relation, std::move(left),
+                                 std::move(right));
+
+  GeneratorImpl g;
+  ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
+  EXPECT_EQ(g.result(), params.result);
+}
+INSTANTIATE_TEST_SUITE_P(
+    GeneratorImplTest,
+    RelationTest,
+    testing::Values(
+        RelationData{"(left & right)", ast::Relation::kAnd},
+        RelationData{"(left | right)", ast::Relation::kOr},
+        RelationData{"(left ^ right)", ast::Relation::kXor},
+        RelationData{"(left && right)", ast::Relation::kLogicalAnd},
+        RelationData{"(left || right)", ast::Relation::kLogicalOr},
+        RelationData{"(left == right)", ast::Relation::kEqual},
+        RelationData{"(left != right)", ast::Relation::kNotEqual},
+        RelationData{"(left < right)", ast::Relation::kLessThan},
+        RelationData{"(left > right)", ast::Relation::kGreaterThan},
+        RelationData{"(left <= right)", ast::Relation::kLessThanEqual},
+        RelationData{"(left >= right)", ast::Relation::kGreaterThanEqual},
+        RelationData{"(left << right)", ast::Relation::kShiftLeft},
+        RelationData{"(left >> right)", ast::Relation::kShiftRight},
+        RelationData{"(left >>> right)", ast::Relation::kShiftRightArith},
+        RelationData{"(left + right)", ast::Relation::kAdd},
+        RelationData{"(left - right)", ast::Relation::kSubtract},
+        RelationData{"(left * right)", ast::Relation::kMultiply},
+        RelationData{"(left / right)", ast::Relation::kDivide},
+        RelationData{"(left % right)", ast::Relation::kModulo}));
+
+}  // namespace
+}  // namespace wgsl
+}  // namespace writer
+}  // namespace tint