[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:
parent
cda5af0fa2
commit
d450a920d5
1
BUILD.gn
1
BUILD.gn
|
@ -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_array_accessor_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_assign_test.cc",
|
"src/writer/hlsl/generator_impl_assign_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_binary_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_break_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_case_test.cc",
|
"src/writer/hlsl/generator_impl_case_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_constructor_test.cc",
|
"src/writer/hlsl/generator_impl_constructor_test.cc",
|
||||||
|
|
|
@ -553,6 +553,7 @@ if (${TINT_BUILD_HLSL_WRITER})
|
||||||
writer/hlsl/generator_impl_array_accessor_test.cc
|
writer/hlsl/generator_impl_array_accessor_test.cc
|
||||||
writer/hlsl/generator_impl_assign_test.cc
|
writer/hlsl/generator_impl_assign_test.cc
|
||||||
writer/hlsl/generator_impl_binary_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_break_test.cc
|
||||||
writer/hlsl/generator_impl_case_test.cc
|
writer/hlsl/generator_impl_case_test.cc
|
||||||
writer/hlsl/generator_impl_constructor_test.cc
|
writer/hlsl/generator_impl_constructor_test.cc
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2020 The Tint Authors.
|
/// Copyright 2020 The Tint Authors.
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with 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;
|
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*) {
|
bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
|
||||||
make_indent();
|
make_indent();
|
||||||
out_ << "break;" << std::endl;
|
out_ << "break;" << std::endl;
|
||||||
|
@ -452,6 +486,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||||
if (stmt->IsAssign()) {
|
if (stmt->IsAssign()) {
|
||||||
return EmitAssign(stmt->AsAssign());
|
return EmitAssign(stmt->AsAssign());
|
||||||
}
|
}
|
||||||
|
if (stmt->IsBlock()) {
|
||||||
|
return EmitIndentedBlockAndNewline(stmt->AsBlock());
|
||||||
|
}
|
||||||
if (stmt->IsBreak()) {
|
if (stmt->IsBreak()) {
|
||||||
return EmitBreak(stmt->AsBreak());
|
return EmitBreak(stmt->AsBreak());
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,18 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @param expr the binary expression
|
/// @param expr the binary expression
|
||||||
/// @returns true if the expression was emitted, false otherwise
|
/// @returns true if the expression was emitted, false otherwise
|
||||||
bool EmitBinary(ast::BinaryExpression* expr);
|
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
|
/// Handles a break statement
|
||||||
/// @param stmt the statement to emit
|
/// @param stmt the statement to emit
|
||||||
/// @returns true if the statement was emitted successfully
|
/// @returns true if the statement was emitted successfully
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue