[msl-writer] Add emission of if statements.

This Cl adds emission of `if`, `else if` and `else` statements to the
MSL backend.

Bug: tint:8
Change-Id: I8c22d70f2afa0a1d86cf475f5c98127504a6dc0e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23840
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index e90aa05..ccff5dd 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -852,7 +852,6 @@
     "src/writer/wgsl/generator_impl_cast_test.cc",
     "src/writer/wgsl/generator_impl_constructor_test.cc",
     "src/writer/wgsl/generator_impl_continue_test.cc",
-    "src/writer/wgsl/generator_impl_else_test.cc",
     "src/writer/wgsl/generator_impl_entry_point_test.cc",
     "src/writer/wgsl/generator_impl_fallthrough_test.cc",
     "src/writer/wgsl/generator_impl_function_test.cc",
@@ -892,6 +891,7 @@
     "src/writer/msl/generator_impl_constructor_test.cc",
     "src/writer/msl/generator_impl_function_test.cc",
     "src/writer/msl/generator_impl_identifier_test.cc",
+    "src/writer/msl/generator_impl_if_test.cc",
     "src/writer/msl/generator_impl_return_test.cc",
     "src/writer/msl/generator_impl_test.cc",
     "src/writer/msl/generator_impl_type_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e1cd7a4..27d1bfe 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -472,7 +472,6 @@
     writer/wgsl/generator_impl_cast_test.cc
     writer/wgsl/generator_impl_constructor_test.cc
     writer/wgsl/generator_impl_continue_test.cc
-    writer/wgsl/generator_impl_else_test.cc
     writer/wgsl/generator_impl_entry_point_test.cc
     writer/wgsl/generator_impl_fallthrough_test.cc
     writer/wgsl/generator_impl_function_test.cc
@@ -500,6 +499,7 @@
     writer/msl/generator_impl_constructor_test.cc
     writer/msl/generator_impl_function_test.cc
     writer/msl/generator_impl_identifier_test.cc
+    writer/msl/generator_impl_if_test.cc
     writer/msl/generator_impl_return_test.cc
     writer/msl/generator_impl_test.cc
     writer/msl/generator_impl_type_test.cc
diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc
index 252e9029..79f5afb 100644
--- a/src/writer/msl/generator_impl.cc
+++ b/src/writer/msl/generator_impl.cc
@@ -19,9 +19,11 @@
 #include "src/ast/binary_expression.h"
 #include "src/ast/bool_literal.h"
 #include "src/ast/cast_expression.h"
+#include "src/ast/else_statement.h"
 #include "src/ast/float_literal.h"
 #include "src/ast/function.h"
 #include "src/ast/identifier_expression.h"
+#include "src/ast/if_statement.h"
 #include "src/ast/return_statement.h"
 #include "src/ast/sint_literal.h"
 #include "src/ast/type/alias_type.h"
@@ -345,6 +347,42 @@
   return true;
 }
 
+bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
+  if (stmt->HasCondition()) {
+    out_ << " else if (";
+    if (!EmitExpression(stmt->condition())) {
+      return false;
+    }
+    out_ << ")";
+  } else {
+    out_ << " else";
+  }
+
+  return EmitStatementBlock(stmt->body());
+}
+
+bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
+  make_indent();
+
+  out_ << "if (";
+  if (!EmitExpression(stmt->condition())) {
+    return false;
+  }
+  out_ << ")";
+
+  if (!EmitStatementBlock(stmt->body())) {
+    return false;
+  }
+
+  for (const auto& e : stmt->else_statements()) {
+    if (!EmitElse(e.get())) {
+      return false;
+    }
+  }
+  out_ << std::endl;
+
+  return true;
+}
 bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
   make_indent();
 
@@ -390,6 +428,9 @@
   if (stmt->IsAssign()) {
     return EmitAssign(stmt->AsAssign());
   }
+  if (stmt->IsIf()) {
+    return EmitIf(stmt->AsIf());
+  }
   if (stmt->IsReturn()) {
     return EmitReturn(stmt->AsReturn());
   }
diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h
index 3c3b1b5c..9459089 100644
--- a/src/writer/msl/generator_impl.h
+++ b/src/writer/msl/generator_impl.h
@@ -60,6 +60,10 @@
   /// @param expr the constructor expression
   /// @returns true if the expression was emitted
   bool EmitConstructor(ast::ConstructorExpression* expr);
+  /// Handles generating an else statement
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was emitted
+  bool EmitElse(ast::ElseStatement* stmt);
   /// Handles generate an Expression
   /// @param expr the expression
   /// @returns true if the expression was emitted
@@ -72,6 +76,10 @@
   /// @param expr the identifier expression
   /// @returns true if the identifeir was emitted
   bool EmitIdentifier(ast::IdentifierExpression* expr);
+  /// Handles an if statement
+  /// @param stmt the statement to emit
+  /// @returns true if the statement was successfully emitted
+  bool EmitIf(ast::IfStatement* stmt);
   /// Handles a literal
   /// @param lit the literal to emit
   /// @returns true if the literal was successfully emitted
diff --git a/src/writer/msl/generator_impl_if_test.cc b/src/writer/msl/generator_impl_if_test.cc
new file mode 100644
index 0000000..b7a78b7
--- /dev/null
+++ b/src/writer/msl/generator_impl_if_test.cc
@@ -0,0 +1,139 @@
+// 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/else_statement.h"
+#include "src/ast/identifier_expression.h"
+#include "src/ast/if_statement.h"
+#include "src/ast/return_statement.h"
+#include "src/writer/msl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace msl {
+namespace {
+
+using MslGeneratorImplTest = testing::Test;
+
+TEST_F(MslGeneratorImplTest, Emit_If) {
+  auto cond = std::make_unique<ast::IdentifierExpression>("cond");
+  ast::StatementList body;
+  body.push_back(std::make_unique<ast::ReturnStatement>());
+
+  ast::IfStatement i(std::move(cond), std::move(body));
+
+  GeneratorImpl g;
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
+  EXPECT_EQ(g.result(), R"(  if (cond) {
+    return;
+  }
+)");
+}
+
+TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) {
+  auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
+
+  ast::StatementList else_body;
+  else_body.push_back(std::make_unique<ast::ReturnStatement>());
+
+  ast::ElseStatementList elses;
+  elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
+                                                       std::move(else_body)));
+
+  auto cond = std::make_unique<ast::IdentifierExpression>("cond");
+  ast::StatementList body;
+  body.push_back(std::make_unique<ast::ReturnStatement>());
+
+  ast::IfStatement i(std::move(cond), std::move(body));
+  i.set_else_statements(std::move(elses));
+
+  GeneratorImpl g;
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
+  EXPECT_EQ(g.result(), R"(  if (cond) {
+    return;
+  } else if (else_cond) {
+    return;
+  }
+)");
+}
+
+TEST_F(MslGeneratorImplTest, Emit_IfWithElse) {
+  ast::StatementList else_body;
+  else_body.push_back(std::make_unique<ast::ReturnStatement>());
+
+  ast::ElseStatementList elses;
+  elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body)));
+
+  auto cond = std::make_unique<ast::IdentifierExpression>("cond");
+  ast::StatementList body;
+  body.push_back(std::make_unique<ast::ReturnStatement>());
+
+  ast::IfStatement i(std::move(cond), std::move(body));
+  i.set_else_statements(std::move(elses));
+
+  GeneratorImpl g;
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
+  EXPECT_EQ(g.result(), R"(  if (cond) {
+    return;
+  } else {
+    return;
+  }
+)");
+}
+
+TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) {
+  auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
+
+  ast::StatementList else_body;
+  else_body.push_back(std::make_unique<ast::ReturnStatement>());
+
+  ast::StatementList else_body_2;
+  else_body_2.push_back(std::make_unique<ast::ReturnStatement>());
+
+  ast::ElseStatementList elses;
+  elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
+                                                       std::move(else_body)));
+  elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body_2)));
+
+  auto cond = std::make_unique<ast::IdentifierExpression>("cond");
+  ast::StatementList body;
+  body.push_back(std::make_unique<ast::ReturnStatement>());
+
+  ast::IfStatement i(std::move(cond), std::move(body));
+  i.set_else_statements(std::move(elses));
+
+  GeneratorImpl g;
+  g.increment_indent();
+
+  ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
+  EXPECT_EQ(g.result(), R"(  if (cond) {
+    return;
+  } else if (else_cond) {
+    return;
+  } else {
+    return;
+  }
+)");
+}
+
+}  // namespace
+}  // namespace msl
+}  // namespace writer
+}  // namespace tint
diff --git a/src/writer/wgsl/generator_impl_else_test.cc b/src/writer/wgsl/generator_impl_else_test.cc
deleted file mode 100644
index 47869ba..0000000
--- a/src/writer/wgsl/generator_impl_else_test.cc
+++ /dev/null
@@ -1,65 +0,0 @@
-// 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/else_statement.h"
-#include "src/ast/identifier_expression.h"
-#include "src/ast/kill_statement.h"
-#include "src/writer/wgsl/generator_impl.h"
-
-namespace tint {
-namespace writer {
-namespace wgsl {
-namespace {
-
-using WgslGeneratorImplTest = testing::Test;
-
-TEST_F(WgslGeneratorImplTest, Emit_Else) {
-  ast::StatementList body;
-  body.push_back(std::make_unique<ast::KillStatement>());
-
-  ast::ElseStatement e(std::move(body));
-
-  GeneratorImpl g;
-  g.increment_indent();
-
-  ASSERT_TRUE(g.EmitElse(&e)) << g.error();
-  EXPECT_EQ(g.result(), R"( else {
-    kill;
-  })");
-}
-
-TEST_F(WgslGeneratorImplTest, Emit_ElseWithCondition) {
-  auto cond = std::make_unique<ast::IdentifierExpression>("cond");
-
-  ast::StatementList body;
-  body.push_back(std::make_unique<ast::KillStatement>());
-
-  ast::ElseStatement e(std::move(cond), std::move(body));
-
-  GeneratorImpl g;
-  g.increment_indent();
-
-  ASSERT_TRUE(g.EmitElse(&e)) << g.error();
-  EXPECT_EQ(g.result(), R"( elseif (cond) {
-    kill;
-  })");
-}
-
-}  // namespace
-}  // namespace wgsl
-}  // namespace writer
-}  // namespace tint