[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 <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-25 18:54:01 +00:00 committed by dan sinclair
parent 235ceebe37
commit 0984214d8b
5 changed files with 56 additions and 101 deletions

View File

@ -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 <cstdint>
#include <string>
#include <vector>
@ -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"

View File

@ -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<std::unique_ptr<ast::Statement>>& 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,6 +840,7 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
}
bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
make_indent();
if (stmt->HasCondition()) {
out_ << "elseif (";
if (!EmitExpression(stmt->condition())) {
@ -856,22 +850,8 @@ bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
} else {
out_ << "else";
}
out_ << " {" << std::endl;
increment_indent();
for (const auto& s : stmt->body()) {
if (!EmitStatement(s.get())) {
return false;
}
}
decrement_indent();
make_indent();
out_ << "}";
return true;
return EmitStatementBlock(stmt->body());
}
bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) {
@ -887,19 +867,11 @@ 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())) {
if (!EmitStatementBlock(stmt->body())) {
return false;
}
}
decrement_indent();
make_indent();
out_ << "}";
for (const auto& e : stmt->else_statements()) {
if (!EmitElse(e.get())) {
@ -907,8 +879,6 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
}
}
out_ << std::endl;
return true;
}
@ -934,11 +904,9 @@ 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())) {
if (!EmitStatementBlock(stmt->continuing())) {
return false;
}
}
@ -946,11 +914,6 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
decrement_indent();
make_indent();
out_ << "}" << std::endl;
}
decrement_indent();
make_indent();
out_ << "}" << std::endl;
return true;
}
@ -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

View File

@ -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<std::unique_ptr<ast::Statement>>& statements);
/// Handles statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitStatement(ast::Statement* stmt);

View File

@ -40,7 +40,8 @@ TEST_F(GeneratorImplTest, Emit_Else) {
ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( else {
kill;
})");
}
)");
}
TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
@ -57,7 +58,8 @@ TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( elseif (cond) {
kill;
})");
}
)");
}
} // namespace

View File

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