[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 // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "src/reader/spirv/parser_impl.h"
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
@ -21,6 +19,7 @@
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "src/ast/type/matrix_type.h" #include "src/ast/type/matrix_type.h"
#include "src/ast/type/vector_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/reader/spirv/spirv_tools_helpers_test.h"
#include "src/type_manager.h" #include "src/type_manager.h"

View File

@ -363,21 +363,7 @@ bool GeneratorImpl::EmitFunction(ast::Function* func) {
return false; return false;
} }
out_ << " {" << std::endl; return EmitStatementBlock(func->body());
increment_indent();
for (const auto& s : func->body()) {
if (!EmitStatement(s.get())) {
return false;
}
}
decrement_indent();
make_indent();
out_ << "}" << std::endl;
return true;
} }
bool GeneratorImpl::EmitType(ast::type::Type* type) { bool GeneratorImpl::EmitType(ast::type::Type* type) {
@ -704,6 +690,25 @@ bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) {
return true; 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) { bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsAssign()) { if (stmt->IsAssign()) {
return EmitAssign(stmt->AsAssign()); return EmitAssign(stmt->AsAssign());
@ -805,20 +810,8 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) {
} }
out_ << ":"; out_ << ":";
} }
out_ << " {" << std::endl;
increment_indent();
for (const auto& b : stmt->body()) { return EmitStatementBlock(stmt->body());
if (!EmitStatement(b.get())) {
return false;
}
}
decrement_indent();
make_indent();
out_ << "}" << std::endl;
return true;
} }
bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) { bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
@ -847,31 +840,18 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
} }
bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) { bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
make_indent();
if (stmt->HasCondition()) { if (stmt->HasCondition()) {
out_ << " elseif ("; out_ << "elseif (";
if (!EmitExpression(stmt->condition())) { if (!EmitExpression(stmt->condition())) {
return false; return false;
} }
out_ << ")"; out_ << ")";
} else { } else {
out_ << " else"; out_ << "else";
}
out_ << " {" << std::endl;
increment_indent();
for (const auto& s : stmt->body()) {
if (!EmitStatement(s.get())) {
return false;
}
} }
decrement_indent(); return EmitStatementBlock(stmt->body());
make_indent();
out_ << "}";
return true;
} }
bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) { bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) {
@ -887,28 +867,18 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
if (!EmitExpression(stmt->condition())) { if (!EmitExpression(stmt->condition())) {
return false; return false;
} }
out_ << ") {" << std::endl; out_ << ")";
increment_indent(); if (!EmitStatementBlock(stmt->body())) {
return false;
for (const auto& b : stmt->body()) {
if (!EmitStatement(b.get())) {
return false;
}
} }
decrement_indent();
make_indent();
out_ << "}";
for (const auto& e : stmt->else_statements()) { for (const auto& e : stmt->else_statements()) {
if (!EmitElse(e.get())) { if (!EmitElse(e.get())) {
return false; return false;
} }
} }
out_ << std::endl;
return true; return true;
} }
@ -934,18 +904,11 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
out_ << std::endl; out_ << std::endl;
make_indent(); make_indent();
out_ << "continuing {" << std::endl; out_ << "continuing";
increment_indent(); if (!EmitStatementBlock(stmt->continuing())) {
for (const auto& s : stmt->continuing()) { return false;
if (!EmitStatement(s.get())) {
return false;
}
} }
decrement_indent();
make_indent();
out_ << "}" << std::endl;
} }
decrement_indent(); decrement_indent();
@ -968,18 +931,9 @@ bool GeneratorImpl::EmitRegardless(ast::RegardlessStatement* stmt) {
if (!EmitExpression(stmt->condition())) { if (!EmitExpression(stmt->condition())) {
return false; return false;
} }
out_ << ") {" << std::endl; out_ << ")";
increment_indent(); return EmitStatementBlock(stmt->body());
for (const auto& b : stmt->body()) {
if (!EmitStatement(b.get())) {
return false;
}
}
decrement_indent();
make_indent();
out_ << "}" << std::endl;
return true;
} }
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
@ -1027,18 +981,9 @@ bool GeneratorImpl::EmitUnless(ast::UnlessStatement* stmt) {
if (!EmitExpression(stmt->condition())) { if (!EmitExpression(stmt->condition())) {
return false; return false;
} }
out_ << ") {" << std::endl; out_ << ")";
increment_indent(); return EmitStatementBlock(stmt->body());
for (const auto& b : stmt->body()) {
if (!EmitStatement(b.get())) {
return false;
}
}
decrement_indent();
make_indent();
out_ << "}" << std::endl;
return true;
} }
} // namespace wgsl } // namespace wgsl

View File

@ -174,7 +174,12 @@ class GeneratorImpl {
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted /// @returns true if the statement was successfully emitted
bool EmitReturn(ast::ReturnStatement* stmt); 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 /// @param stmt the statement to emit
/// @returns true if the statement was emitted /// @returns true if the statement was emitted
bool EmitStatement(ast::Statement* stmt); bool EmitStatement(ast::Statement* stmt);

View File

@ -38,9 +38,10 @@ TEST_F(GeneratorImplTest, Emit_Else) {
g.increment_indent(); g.increment_indent();
ASSERT_TRUE(g.EmitElse(&e)) << g.error(); ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( else { EXPECT_EQ(g.result(), R"( else {
kill; kill;
})"); }
)");
} }
TEST_F(GeneratorImplTest, Emit_ElseWithCondition) { TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
@ -55,9 +56,10 @@ TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
g.increment_indent(); g.increment_indent();
ASSERT_TRUE(g.EmitElse(&e)) << g.error(); ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( elseif (cond) { EXPECT_EQ(g.result(), R"( elseif (cond) {
kill; kill;
})"); }
)");
} }
} // namespace } // namespace

View File

@ -69,7 +69,8 @@ TEST_F(GeneratorImplTest, Emit_IfWithElseIf) {
ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) { EXPECT_EQ(g.result(), R"( if (cond) {
kill; kill;
} elseif (else_cond) { }
elseif (else_cond) {
kill; kill;
} }
)"); )");
@ -95,7 +96,8 @@ TEST_F(GeneratorImplTest, Emit_IfWithElse) {
ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) { EXPECT_EQ(g.result(), R"( if (cond) {
kill; kill;
} else { }
else {
kill; kill;
} }
)"); )");
@ -128,9 +130,11 @@ TEST_F(GeneratorImplTest, Emit_IfWithMultiple) {
ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) { EXPECT_EQ(g.result(), R"( if (cond) {
kill; kill;
} elseif (else_cond) { }
elseif (else_cond) {
kill; kill;
} else { }
else {
kill; kill;
} }
)"); )");