[wgsl-writer] Add emission of BlockStatement.

This CL adds BlockStatement writting to the WGSL writer.

Bug: tint:131
Change-Id: I6d1d286134311cea13e19c7381ed344da8205199
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25607
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
dan sinclair 2020-07-27 15:25:00 +00:00 committed by Sarah Mashayekhi
parent 775cb51794
commit 21f8e253a0
5 changed files with 100 additions and 0 deletions

View File

@ -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",

View File

@ -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

View File

@ -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());
}

View File

@ -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

View File

@ -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 <memory>
#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<ast::DiscardStatement>());
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<ast::DiscardStatement>());
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