From 0984214d8bef3b1a62590a3118b90e0a4c7eb7bf Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Wed, 25 Mar 2020 18:54:01 +0000 Subject: [PATCH] [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 --- .../spirv/parser_impl_convert_type_test.cc | 3 +- src/writer/wgsl/generator_impl.cc | 125 +++++------------- src/writer/wgsl/generator_impl.h | 7 +- src/writer/wgsl/generator_impl_else_test.cc | 10 +- src/writer/wgsl/generator_impl_if_test.cc | 12 +- 5 files changed, 56 insertions(+), 101 deletions(-) diff --git a/src/reader/spirv/parser_impl_convert_type_test.cc b/src/reader/spirv/parser_impl_convert_type_test.cc index f963ae14ec..dd16e0a2a4 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 #include #include @@ -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 213831759b..a74c48e54c 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -363,21 +363,7 @@ bool GeneratorImpl::EmitFunction(ast::Function* func) { 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 @@ bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) { return true; } +bool GeneratorImpl::EmitStatementBlock( + const std::vector>& 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 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { } 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::EmitContinue(ast::ContinueStatement* stmt) { } 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 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { 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 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { 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 @@ bool GeneratorImpl::EmitRegardless(ast::RegardlessStatement* stmt) { 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 @@ bool GeneratorImpl::EmitUnless(ast::UnlessStatement* stmt) { 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 fe920304ae..891073ac12 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -174,7 +174,12 @@ class GeneratorImpl { /// @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>& 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 561babf9ac..0d1d98a4f0 100644 --- a/src/writer/wgsl/generator_impl_else_test.cc +++ b/src/writer/wgsl/generator_impl_else_test.cc @@ -38,9 +38,10 @@ TEST_F(GeneratorImplTest, Emit_Else) { 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 @@ TEST_F(GeneratorImplTest, Emit_ElseWithCondition) { 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 274c7c0d64..cde3640d8e 100644 --- a/src/writer/wgsl/generator_impl_if_test.cc +++ b/src/writer/wgsl/generator_impl_if_test.cc @@ -69,7 +69,8 @@ TEST_F(GeneratorImplTest, Emit_IfWithElseIf) { 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 @@ TEST_F(GeneratorImplTest, Emit_IfWithElse) { ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); EXPECT_EQ(g.result(), R"( if (cond) { kill; - } else { + } + else { kill; } )"); @@ -128,9 +130,11 @@ TEST_F(GeneratorImplTest, Emit_IfWithMultiple) { 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; } )");