[wgsl-writer] Add statement writer helper.

This CL adds a helper to emit a block of statements from the WGSL
writer.

Change-Id: I6fe62d894882c0a0fdc8865967bfa4887f3a65a3
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17761
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/reader/spirv/parser_impl_convert_type_test.cc b/src/reader/spirv/parser_impl_convert_type_test.cc
index f963ae1..dd16e0a 100644
--- a/src/reader/spirv/parser_impl_convert_type_test.cc
+++ b/src/reader/spirv/parser_impl_convert_type_test.cc
@@ -12,8 +12,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "src/reader/spirv/parser_impl.h"
-
 #include <cstdint>
 #include <string>
 #include <vector>
@@ -21,6 +19,7 @@
 #include "gmock/gmock.h"
 #include "src/ast/type/matrix_type.h"
 #include "src/ast/type/vector_type.h"
+#include "src/reader/spirv/parser_impl.h"
 #include "src/reader/spirv/spirv_tools_helpers_test.h"
 #include "src/type_manager.h"
 
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index 2138317..a74c48e 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -363,21 +363,7 @@
     return false;
   }
 
-  out_ << " {" << std::endl;
-
-  increment_indent();
-
-  for (const auto& s : func->body()) {
-    if (!EmitStatement(s.get())) {
-      return false;
-    }
-  }
-
-  decrement_indent();
-  make_indent();
-  out_ << "}" << std::endl;
-
-  return true;
+  return EmitStatementBlock(func->body());
 }
 
 bool GeneratorImpl::EmitType(ast::type::Type* type) {
@@ -704,6 +690,25 @@
   return true;
 }
 
+bool GeneratorImpl::EmitStatementBlock(
+    const std::vector<std::unique_ptr<ast::Statement>>& statements) {
+  out_ << " {" << std::endl;
+
+  increment_indent();
+
+  for (const auto& s : statements) {
+    if (!EmitStatement(s.get())) {
+      return false;
+    }
+  }
+
+  decrement_indent();
+  make_indent();
+  out_ << "}" << std::endl;
+
+  return true;
+}
+
 bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
   if (stmt->IsAssign()) {
     return EmitAssign(stmt->AsAssign());
@@ -805,20 +810,8 @@
     }
     out_ << ":";
   }
-  out_ << " {" << std::endl;
-  increment_indent();
 
-  for (const auto& b : stmt->body()) {
-    if (!EmitStatement(b.get())) {
-      return false;
-    }
-  }
-  decrement_indent();
-
-  make_indent();
-  out_ << "}" << std::endl;
-
-  return true;
+  return EmitStatementBlock(stmt->body());
 }
 
 bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
@@ -847,31 +840,18 @@
 }
 
 bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
+  make_indent();
   if (stmt->HasCondition()) {
-    out_ << " elseif (";
+    out_ << "elseif (";
     if (!EmitExpression(stmt->condition())) {
       return false;
     }
     out_ << ")";
   } else {
-    out_ << " else";
-  }
-  out_ << " {" << std::endl;
-
-  increment_indent();
-
-  for (const auto& s : stmt->body()) {
-    if (!EmitStatement(s.get())) {
-      return false;
-    }
+    out_ << "else";
   }
 
-  decrement_indent();
-
-  make_indent();
-  out_ << "}";
-
-  return true;
+  return EmitStatementBlock(stmt->body());
 }
 
 bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) {
@@ -887,28 +867,18 @@
   if (!EmitExpression(stmt->condition())) {
     return false;
   }
-  out_ << ") {" << std::endl;
+  out_ << ")";
 
-  increment_indent();
-
-  for (const auto& b : stmt->body()) {
-    if (!EmitStatement(b.get())) {
-      return false;
-    }
+  if (!EmitStatementBlock(stmt->body())) {
+    return false;
   }
 
-  decrement_indent();
-  make_indent();
-  out_ << "}";
-
   for (const auto& e : stmt->else_statements()) {
     if (!EmitElse(e.get())) {
       return false;
     }
   }
 
-  out_ << std::endl;
-
   return true;
 }
 
@@ -934,18 +904,11 @@
     out_ << std::endl;
 
     make_indent();
-    out_ << "continuing {" << std::endl;
+    out_ << "continuing";
 
-    increment_indent();
-    for (const auto& s : stmt->continuing()) {
-      if (!EmitStatement(s.get())) {
-        return false;
-      }
+    if (!EmitStatementBlock(stmt->continuing())) {
+      return false;
     }
-
-    decrement_indent();
-    make_indent();
-    out_ << "}" << std::endl;
   }
 
   decrement_indent();
@@ -968,18 +931,9 @@
   if (!EmitExpression(stmt->condition())) {
     return false;
   }
-  out_ << ") {" << std::endl;
+  out_ << ")";
 
-  increment_indent();
-  for (const auto& b : stmt->body()) {
-    if (!EmitStatement(b.get())) {
-      return false;
-    }
-  }
-  decrement_indent();
-  make_indent();
-  out_ << "}" << std::endl;
-  return true;
+  return EmitStatementBlock(stmt->body());
 }
 
 bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
@@ -1027,18 +981,9 @@
   if (!EmitExpression(stmt->condition())) {
     return false;
   }
-  out_ << ") {" << std::endl;
+  out_ << ")";
 
-  increment_indent();
-  for (const auto& b : stmt->body()) {
-    if (!EmitStatement(b.get())) {
-      return false;
-    }
-  }
-  decrement_indent();
-  make_indent();
-  out_ << "}" << std::endl;
-  return true;
+  return EmitStatementBlock(stmt->body());
 }
 
 }  // namespace wgsl
diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h
index fe92030..891073a 100644
--- a/src/writer/wgsl/generator_impl.h
+++ b/src/writer/wgsl/generator_impl.h
@@ -174,7 +174,12 @@
   /// @param stmt the statement to emit
   /// @returns true if the statement was successfully emitted
   bool EmitReturn(ast::ReturnStatement* stmt);
-  /// Handles statements
+  /// Handles a list of statements
+  /// @param statements the statements to output
+  /// @returns true if the statements were emitted
+  bool EmitStatementBlock(
+      const std::vector<std::unique_ptr<ast::Statement>>& statements);
+  /// Handles statement
   /// @param stmt the statement to emit
   /// @returns true if the statement was emitted
   bool EmitStatement(ast::Statement* stmt);
diff --git a/src/writer/wgsl/generator_impl_else_test.cc b/src/writer/wgsl/generator_impl_else_test.cc
index 561babf..0d1d98a 100644
--- a/src/writer/wgsl/generator_impl_else_test.cc
+++ b/src/writer/wgsl/generator_impl_else_test.cc
@@ -38,9 +38,10 @@
   g.increment_indent();
 
   ASSERT_TRUE(g.EmitElse(&e)) << g.error();
-  EXPECT_EQ(g.result(), R"( else {
+  EXPECT_EQ(g.result(), R"(  else {
     kill;
-  })");
+  }
+)");
 }
 
 TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
@@ -55,9 +56,10 @@
   g.increment_indent();
 
   ASSERT_TRUE(g.EmitElse(&e)) << g.error();
-  EXPECT_EQ(g.result(), R"( elseif (cond) {
+  EXPECT_EQ(g.result(), R"(  elseif (cond) {
     kill;
-  })");
+  }
+)");
 }
 
 }  // namespace
diff --git a/src/writer/wgsl/generator_impl_if_test.cc b/src/writer/wgsl/generator_impl_if_test.cc
index 274c7c0..cde3640 100644
--- a/src/writer/wgsl/generator_impl_if_test.cc
+++ b/src/writer/wgsl/generator_impl_if_test.cc
@@ -69,7 +69,8 @@
   ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
   EXPECT_EQ(g.result(), R"(  if (cond) {
     kill;
-  } elseif (else_cond) {
+  }
+  elseif (else_cond) {
     kill;
   }
 )");
@@ -95,7 +96,8 @@
   ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
   EXPECT_EQ(g.result(), R"(  if (cond) {
     kill;
-  } else {
+  }
+  else {
     kill;
   }
 )");
@@ -128,9 +130,11 @@
   ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
   EXPECT_EQ(g.result(), R"(  if (cond) {
     kill;
-  } elseif (else_cond) {
+  }
+  elseif (else_cond) {
     kill;
-  } else {
+  }
+  else {
     kill;
   }
 )");