diff --git a/BUILD.gn b/BUILD.gn index 24a19b0706..4ec9d1035d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -937,6 +937,7 @@ source_set("tint_unittests_wgsl_writer_src") { "src/writer/wgsl/generator_impl_as_test.cc", "src/writer/wgsl/generator_impl_assign_test.cc", "src/writer/wgsl/generator_impl_binary_test.cc", + "src/writer/wgsl/generator_impl_block_test.cc", "src/writer/wgsl/generator_impl_break_test.cc", "src/writer/wgsl/generator_impl_call_test.cc", "src/writer/wgsl/generator_impl_case_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a5bd4b9e41..046adcae24 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -487,6 +487,7 @@ if(${TINT_BUILD_WGSL_WRITER}) writer/wgsl/generator_impl_as_test.cc writer/wgsl/generator_impl_assign_test.cc writer/wgsl/generator_impl_binary_test.cc + writer/wgsl/generator_impl_block_test.cc writer/wgsl/generator_impl_break_test.cc writer/wgsl/generator_impl_call_test.cc writer/wgsl/generator_impl_case_test.cc diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 036306b973..373ef4081c 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -22,6 +22,7 @@ #include "src/ast/assignment_statement.h" #include "src/ast/binary_expression.h" #include "src/ast/binding_decoration.h" +#include "src/ast/block_statement.h" #include "src/ast/bool_literal.h" #include "src/ast/break_statement.h" #include "src/ast/builtin_decoration.h" @@ -598,6 +599,33 @@ bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) { return true; } +bool GeneratorImpl::EmitBlock(ast::BlockStatement* stmt) { + make_indent(); + + out_ << "{" << std::endl; + increment_indent(); + + for (const auto& s : *stmt) { + if (!EmitStatement(s.get())) { + return false; + } + } + + decrement_indent(); + make_indent(); + out_ << "}"; + + return true; +} + +bool GeneratorImpl::EmitBlockAndNewline(ast::BlockStatement* stmt) { + const bool result = EmitBlock(stmt); + if (result) { + out_ << std::endl; + } + return result; +} + bool GeneratorImpl::EmitStatementBlock(const ast::StatementList& statements) { out_ << " {" << std::endl; @@ -629,6 +657,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsAssign()) { return EmitAssign(stmt->AsAssign()); } + if (stmt->IsBlock()) { + return EmitBlockAndNewline(stmt->AsBlock()); + } if (stmt->IsBreak()) { return EmitBreak(stmt->AsBreak()); } diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index 7df7262f95..6c211cf47e 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -67,6 +67,14 @@ class GeneratorImpl : public TextGenerator { /// @param expr the binary expression /// @returns true if the expression was emitted, false otherwise bool EmitBinary(ast::BinaryExpression* expr); + /// Handles a block statement + /// @param stmt the statement to emit + /// @returns true if the statement was emitted successfully + bool EmitBlock(ast::BlockStatement* stmt); + /// Handles a block statement with a newline at the end + /// @param stmt the statement to emit + /// @returns true if the statement was emitted successfully + bool EmitBlockAndNewline(ast::BlockStatement* stmt); /// Handles a break statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully diff --git a/src/writer/wgsl/generator_impl_block_test.cc b/src/writer/wgsl/generator_impl_block_test.cc new file mode 100644 index 0000000000..bc822738b2 --- /dev/null +++ b/src/writer/wgsl/generator_impl_block_test.cc @@ -0,0 +1,59 @@ +// 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 + +#include "gtest/gtest.h" +#include "src/ast/block_statement.h" +#include "src/ast/discard_statement.h" +#include "src/writer/wgsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace wgsl { +namespace { + +using WgslGeneratorImplTest = testing::Test; + +TEST_F(WgslGeneratorImplTest, Emit_Block) { + ast::BlockStatement b; + b.append(std::make_unique()); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&b)) << g.error(); + EXPECT_EQ(g.result(), R"( { + discard; + } +)"); +} + +TEST_F(WgslGeneratorImplTest, Emit_Block_WithoutNewline) { + ast::BlockStatement b; + b.append(std::make_unique()); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitBlock(&b)) << g.error(); + EXPECT_EQ(g.result(), R"( { + discard; + })"); +} + +} // namespace +} // namespace wgsl +} // namespace writer +} // namespace tint