[hlsl-writer] Add BlockStatement support.

This CL adds support for BlockStatement to the HLSL backend.

Bug: tint:7
Change-Id: I953a1b85a05cd84e8e296d677204ae9b5a3ae669
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25860
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-29 19:06:46 +00:00 committed by dan sinclair
parent cda5af0fa2
commit d450a920d5
5 changed files with 113 additions and 1 deletions

View File

@ -1045,6 +1045,7 @@ source_set("tint_unittests_hlsl_writer_src") {
"src/writer/hlsl/generator_impl_array_accessor_test.cc",
"src/writer/hlsl/generator_impl_assign_test.cc",
"src/writer/hlsl/generator_impl_binary_test.cc",
"src/writer/hlsl/generator_impl_block_test.cc",
"src/writer/hlsl/generator_impl_break_test.cc",
"src/writer/hlsl/generator_impl_case_test.cc",
"src/writer/hlsl/generator_impl_constructor_test.cc",

View File

@ -553,6 +553,7 @@ if (${TINT_BUILD_HLSL_WRITER})
writer/hlsl/generator_impl_array_accessor_test.cc
writer/hlsl/generator_impl_assign_test.cc
writer/hlsl/generator_impl_binary_test.cc
writer/hlsl/generator_impl_block_test.cc
writer/hlsl/generator_impl_break_test.cc
writer/hlsl/generator_impl_case_test.cc
writer/hlsl/generator_impl_constructor_test.cc

View File

@ -1,4 +1,4 @@
// Copyright 2020 The Tint Authors.
/// 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.
@ -219,6 +219,40 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) {
return true;
}
bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) {
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(const ast::BlockStatement* stmt) {
const bool result = EmitBlock(stmt);
if (result) {
out_ << std::endl;
}
return result;
}
bool GeneratorImpl::EmitIndentedBlockAndNewline(ast::BlockStatement* stmt) {
make_indent();
const bool result = EmitBlock(stmt);
if (result) {
out_ << std::endl;
}
return result;
}
bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
make_indent();
out_ << "break;" << std::endl;
@ -452,6 +486,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsAssign()) {
return EmitAssign(stmt->AsAssign());
}
if (stmt->IsBlock()) {
return EmitIndentedBlockAndNewline(stmt->AsBlock());
}
if (stmt->IsBreak()) {
return EmitBreak(stmt->AsBreak());
}

View File

@ -54,6 +54,18 @@ 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(const 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 EmitIndentedBlockAndNewline(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(const 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,61 @@
// 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/hlsl/generator_impl.h"
namespace tint {
namespace writer {
namespace hlsl {
namespace {
using HlslGeneratorImplTest = testing::Test;
TEST_F(HlslGeneratorImplTest, Emit_Block) {
ast::BlockStatement b;
b.append(std::make_unique<ast::DiscardStatement>());
ast::Module m;
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
EXPECT_EQ(g.result(), R"( {
discard;
}
)");
}
TEST_F(HlslGeneratorImplTest, Emit_Block_WithoutNewline) {
ast::BlockStatement b;
b.append(std::make_unique<ast::DiscardStatement>());
ast::Module m;
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitBlock(&b)) << g.error();
EXPECT_EQ(g.result(), R"({
discard;
})");
}
} // namespace
} // namespace hlsl
} // namespace writer
} // namespace tint