Convert LoopStatement to use BlockStatement.
This CL converts the LoopStatement class to using a BlockStatement internally. All usages have been updated execept for the two readers. Bug: tint:130 Change-Id: Ied4b82f0ef49c7b92caa040bcf73050093022df7 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25723 Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
parent
77040b85d9
commit
37c0970d51
|
@ -17,14 +17,28 @@
|
|||
namespace tint {
|
||||
namespace ast {
|
||||
|
||||
LoopStatement::LoopStatement() : Statement() {}
|
||||
LoopStatement::LoopStatement()
|
||||
: Statement(),
|
||||
body_(std::make_unique<BlockStatement>()),
|
||||
continuing_(std::make_unique<BlockStatement>()) {}
|
||||
|
||||
LoopStatement::LoopStatement(StatementList body, StatementList continuing)
|
||||
LoopStatement::LoopStatement(std::unique_ptr<BlockStatement> body,
|
||||
std::unique_ptr<BlockStatement> continuing)
|
||||
: Statement(), body_(std::move(body)), continuing_(std::move(continuing)) {}
|
||||
|
||||
LoopStatement::LoopStatement(const Source& source,
|
||||
StatementList body,
|
||||
StatementList continuing)
|
||||
: Statement(source),
|
||||
body_(std::make_unique<BlockStatement>()),
|
||||
continuing_(std::make_unique<BlockStatement>()) {
|
||||
set_body(std::move(body));
|
||||
set_continuing(std::move(continuing));
|
||||
}
|
||||
|
||||
LoopStatement::LoopStatement(const Source& source,
|
||||
std::unique_ptr<BlockStatement> body,
|
||||
std::unique_ptr<BlockStatement> continuing)
|
||||
: Statement(source),
|
||||
body_(std::move(body)),
|
||||
continuing_(std::move(continuing)) {}
|
||||
|
@ -33,18 +47,28 @@ LoopStatement::LoopStatement(LoopStatement&&) = default;
|
|||
|
||||
LoopStatement::~LoopStatement() = default;
|
||||
|
||||
void LoopStatement::set_body(StatementList body) {
|
||||
for (auto& stmt : body) {
|
||||
body_->append(std::move(stmt));
|
||||
}
|
||||
}
|
||||
|
||||
void LoopStatement::set_continuing(StatementList continuing) {
|
||||
for (auto& stmt : continuing) {
|
||||
continuing_->append(std::move(stmt));
|
||||
}
|
||||
}
|
||||
|
||||
bool LoopStatement::IsLoop() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LoopStatement::IsValid() const {
|
||||
for (const auto& stmt : body_) {
|
||||
if (stmt == nullptr || !stmt->IsValid())
|
||||
return false;
|
||||
if (body_ == nullptr || !body_->IsValid()) {
|
||||
return false;
|
||||
}
|
||||
for (const auto& stmt : continuing_) {
|
||||
if (stmt == nullptr || !stmt->IsValid())
|
||||
return false;
|
||||
if (continuing_ == nullptr || !continuing_->IsValid()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -53,15 +77,19 @@ void LoopStatement::to_str(std::ostream& out, size_t indent) const {
|
|||
make_indent(out, indent);
|
||||
out << "Loop{" << std::endl;
|
||||
|
||||
for (const auto& stmt : body_)
|
||||
stmt->to_str(out, indent + 2);
|
||||
if (body_ != nullptr) {
|
||||
for (const auto& stmt : *body_) {
|
||||
stmt->to_str(out, indent + 2);
|
||||
}
|
||||
}
|
||||
|
||||
if (continuing_.size() > 0) {
|
||||
if (continuing_ != nullptr && continuing_->size() > 0) {
|
||||
make_indent(out, indent + 2);
|
||||
out << "continuing {" << std::endl;
|
||||
|
||||
for (const auto& stmt : continuing_)
|
||||
for (const auto& stmt : *continuing_) {
|
||||
stmt->to_str(out, indent + 4);
|
||||
}
|
||||
|
||||
make_indent(out, indent + 2);
|
||||
out << "}" << std::endl;
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include <utility>
|
||||
|
||||
#include "src/ast/block_statement.h"
|
||||
#include "src/ast/statement.h"
|
||||
|
||||
namespace tint {
|
||||
|
@ -30,7 +31,8 @@ class LoopStatement : public Statement {
|
|||
/// Constructor
|
||||
/// @param body the body statements
|
||||
/// @param continuing the continuing statements
|
||||
LoopStatement(StatementList body, StatementList continuing);
|
||||
LoopStatement(std::unique_ptr<BlockStatement> body,
|
||||
std::unique_ptr<BlockStatement> continuing);
|
||||
/// Constructor
|
||||
/// @param source the loop statement source
|
||||
/// @param body the body statements
|
||||
|
@ -38,25 +40,42 @@ class LoopStatement : public Statement {
|
|||
LoopStatement(const Source& source,
|
||||
StatementList body,
|
||||
StatementList continuing);
|
||||
/// Constructor
|
||||
/// @param source the loop statement source
|
||||
/// @param body the body statements
|
||||
/// @param continuing the continuing statements
|
||||
LoopStatement(const Source& source,
|
||||
std::unique_ptr<BlockStatement> body,
|
||||
std::unique_ptr<BlockStatement> continuing);
|
||||
/// Move constructor
|
||||
LoopStatement(LoopStatement&&);
|
||||
~LoopStatement() override;
|
||||
|
||||
/// Sets the body statements
|
||||
/// @param body the body statements
|
||||
void set_body(StatementList body) { body_ = std::move(body); }
|
||||
void set_body(std::unique_ptr<BlockStatement> body) {
|
||||
body_ = std::move(body);
|
||||
}
|
||||
/// Sets the body statements
|
||||
/// @param body the body statements
|
||||
void set_body(StatementList body);
|
||||
/// @returns the body statements
|
||||
const StatementList& body() const { return body_; }
|
||||
const BlockStatement* body() const { return body_.get(); }
|
||||
|
||||
/// Sets the continuing statements
|
||||
/// @param continuing the continuing statements
|
||||
void set_continuing(StatementList continuing) {
|
||||
void set_continuing(std::unique_ptr<BlockStatement> continuing) {
|
||||
continuing_ = std::move(continuing);
|
||||
}
|
||||
/// Sets the continuing statements
|
||||
/// @param continuing the continuing statements
|
||||
void set_continuing(StatementList continuing);
|
||||
/// @returns the continuing statements
|
||||
const StatementList& continuing() const { return continuing_; }
|
||||
const BlockStatement* continuing() const { return continuing_.get(); }
|
||||
/// @returns true if there are continuing statements in the loop
|
||||
bool has_continuing() const { return !continuing_.empty(); }
|
||||
bool has_continuing() const {
|
||||
return continuing_ != nullptr && !continuing_->empty();
|
||||
}
|
||||
|
||||
/// @returns true if this is a loop statement
|
||||
bool IsLoop() const override;
|
||||
|
@ -72,8 +91,8 @@ class LoopStatement : public Statement {
|
|||
private:
|
||||
LoopStatement(const LoopStatement&) = delete;
|
||||
|
||||
StatementList body_;
|
||||
StatementList continuing_;
|
||||
std::unique_ptr<BlockStatement> body_;
|
||||
std::unique_ptr<BlockStatement> continuing_;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
|
|
@ -28,27 +28,27 @@ namespace {
|
|||
using LoopStatementTest = testing::Test;
|
||||
|
||||
TEST_F(LoopStatementTest, Creation) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto* b_ptr = body[0].get();
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
auto* b_ptr = body->last();
|
||||
|
||||
StatementList continuing;
|
||||
continuing.push_back(std::make_unique<DiscardStatement>());
|
||||
auto* c_ptr = continuing[0].get();
|
||||
auto continuing = std::make_unique<BlockStatement>();
|
||||
continuing->append(std::make_unique<DiscardStatement>());
|
||||
auto* c_ptr = continuing->last();
|
||||
|
||||
LoopStatement l(std::move(body), std::move(continuing));
|
||||
ASSERT_EQ(l.body().size(), 1u);
|
||||
EXPECT_EQ(l.body()[0].get(), b_ptr);
|
||||
ASSERT_EQ(l.continuing().size(), 1u);
|
||||
EXPECT_EQ(l.continuing()[0].get(), c_ptr);
|
||||
ASSERT_EQ(l.body()->size(), 1u);
|
||||
EXPECT_EQ(l.body()->get(0), b_ptr);
|
||||
ASSERT_EQ(l.continuing()->size(), 1u);
|
||||
EXPECT_EQ(l.continuing()->get(0), c_ptr);
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, Creation_WithSource) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
StatementList continuing;
|
||||
continuing.push_back(std::make_unique<DiscardStatement>());
|
||||
auto continuing = std::make_unique<BlockStatement>();
|
||||
continuing->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
LoopStatement l(Source{20, 2}, std::move(body), std::move(continuing));
|
||||
auto src = l.source();
|
||||
|
@ -62,99 +62,100 @@ TEST_F(LoopStatementTest, IsLoop) {
|
|||
}
|
||||
|
||||
TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), {});
|
||||
EXPECT_FALSE(l.has_continuing());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, HasContinuing_WithContinuing) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
StatementList continuing;
|
||||
continuing.push_back(std::make_unique<DiscardStatement>());
|
||||
auto continuing = std::make_unique<BlockStatement>();
|
||||
continuing->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), std::move(continuing));
|
||||
EXPECT_TRUE(l.has_continuing());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, IsValid) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
StatementList continuing;
|
||||
continuing.push_back(std::make_unique<DiscardStatement>());
|
||||
auto continuing = std::make_unique<BlockStatement>();
|
||||
continuing->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), std::move(continuing));
|
||||
EXPECT_TRUE(l.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, IsValid_WithoutContinuing) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), {});
|
||||
LoopStatement l(std::move(body), std::make_unique<BlockStatement>());
|
||||
EXPECT_TRUE(l.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, IsValid_WithoutBody) {
|
||||
LoopStatement l({}, {});
|
||||
LoopStatement l(std::make_unique<BlockStatement>(),
|
||||
std::make_unique<BlockStatement>());
|
||||
EXPECT_TRUE(l.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, IsValid_NullBodyStatement) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
body.push_back(nullptr);
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
body->append(nullptr);
|
||||
|
||||
StatementList continuing;
|
||||
continuing.push_back(std::make_unique<DiscardStatement>());
|
||||
auto continuing = std::make_unique<BlockStatement>();
|
||||
continuing->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), std::move(continuing));
|
||||
EXPECT_FALSE(l.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, IsValid_InvalidBodyStatement) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
body.push_back(std::make_unique<IfStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
body->append(std::make_unique<IfStatement>());
|
||||
|
||||
StatementList continuing;
|
||||
continuing.push_back(std::make_unique<DiscardStatement>());
|
||||
auto continuing = std::make_unique<BlockStatement>();
|
||||
continuing->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), std::move(continuing));
|
||||
EXPECT_FALSE(l.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, IsValid_NullContinuingStatement) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
StatementList continuing;
|
||||
continuing.push_back(std::make_unique<DiscardStatement>());
|
||||
continuing.push_back(nullptr);
|
||||
auto continuing = std::make_unique<BlockStatement>();
|
||||
continuing->append(std::make_unique<DiscardStatement>());
|
||||
continuing->append(nullptr);
|
||||
|
||||
LoopStatement l(std::move(body), std::move(continuing));
|
||||
EXPECT_FALSE(l.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, IsValid_InvalidContinuingStatement) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
StatementList continuing;
|
||||
continuing.push_back(std::make_unique<DiscardStatement>());
|
||||
continuing.push_back(std::make_unique<IfStatement>());
|
||||
auto continuing = std::make_unique<BlockStatement>();
|
||||
continuing->append(std::make_unique<DiscardStatement>());
|
||||
continuing->append(std::make_unique<IfStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), std::move(continuing));
|
||||
EXPECT_FALSE(l.IsValid());
|
||||
}
|
||||
|
||||
TEST_F(LoopStatementTest, ToStr) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), {});
|
||||
std::ostringstream out;
|
||||
|
@ -166,11 +167,11 @@ TEST_F(LoopStatementTest, ToStr) {
|
|||
}
|
||||
|
||||
TEST_F(LoopStatementTest, ToStr_WithContinuing) {
|
||||
StatementList body;
|
||||
body.push_back(std::make_unique<DiscardStatement>());
|
||||
auto body = std::make_unique<BlockStatement>();
|
||||
body->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
StatementList continuing;
|
||||
continuing.push_back(std::make_unique<DiscardStatement>());
|
||||
auto continuing = std::make_unique<BlockStatement>();
|
||||
continuing->append(std::make_unique<DiscardStatement>());
|
||||
|
||||
LoopStatement l(std::move(body), std::move(continuing));
|
||||
std::ostringstream out;
|
||||
|
|
|
@ -27,10 +27,10 @@ TEST_F(ParserImplTest, LoopStmt_BodyNoContinuing) {
|
|||
ASSERT_FALSE(p->has_error()) << p->error();
|
||||
ASSERT_NE(e, nullptr);
|
||||
|
||||
ASSERT_EQ(e->body().size(), 1u);
|
||||
EXPECT_TRUE(e->body()[0]->IsDiscard());
|
||||
ASSERT_EQ(e->body()->size(), 1u);
|
||||
EXPECT_TRUE(e->body()->get(0)->IsDiscard());
|
||||
|
||||
EXPECT_EQ(e->continuing().size(), 0u);
|
||||
EXPECT_EQ(e->continuing()->size(), 0u);
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, LoopStmt_BodyWithContinuing) {
|
||||
|
@ -39,11 +39,11 @@ TEST_F(ParserImplTest, LoopStmt_BodyWithContinuing) {
|
|||
ASSERT_FALSE(p->has_error()) << p->error();
|
||||
ASSERT_NE(e, nullptr);
|
||||
|
||||
ASSERT_EQ(e->body().size(), 1u);
|
||||
EXPECT_TRUE(e->body()[0]->IsDiscard());
|
||||
ASSERT_EQ(e->body()->size(), 1u);
|
||||
EXPECT_TRUE(e->body()->get(0)->IsDiscard());
|
||||
|
||||
EXPECT_EQ(e->continuing().size(), 1u);
|
||||
EXPECT_TRUE(e->continuing()[0]->IsDiscard());
|
||||
EXPECT_EQ(e->continuing()->size(), 1u);
|
||||
EXPECT_TRUE(e->continuing()->get(0)->IsDiscard());
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, LoopStmt_NoBodyNoContinuing) {
|
||||
|
@ -51,8 +51,8 @@ TEST_F(ParserImplTest, LoopStmt_NoBodyNoContinuing) {
|
|||
auto e = p->loop_stmt();
|
||||
ASSERT_FALSE(p->has_error()) << p->error();
|
||||
ASSERT_NE(e, nullptr);
|
||||
ASSERT_EQ(e->body().size(), 0u);
|
||||
ASSERT_EQ(e->continuing().size(), 0u);
|
||||
ASSERT_EQ(e->body()->size(), 0u);
|
||||
ASSERT_EQ(e->continuing()->size(), 0u);
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, LoopStmt_NoBodyWithContinuing) {
|
||||
|
@ -60,9 +60,9 @@ TEST_F(ParserImplTest, LoopStmt_NoBodyWithContinuing) {
|
|||
auto e = p->loop_stmt();
|
||||
ASSERT_FALSE(p->has_error()) << p->error();
|
||||
ASSERT_NE(e, nullptr);
|
||||
ASSERT_EQ(e->body().size(), 0u);
|
||||
ASSERT_EQ(e->continuing().size(), 1u);
|
||||
EXPECT_TRUE(e->continuing()[0]->IsDiscard());
|
||||
ASSERT_EQ(e->body()->size(), 0u);
|
||||
ASSERT_EQ(e->continuing()->size(), 1u);
|
||||
EXPECT_TRUE(e->continuing()->get(0)->IsDiscard());
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, LoopStmt_MissingBracketLeft) {
|
||||
|
|
|
@ -253,19 +253,6 @@ bool TypeDeterminer::DetermineStatements(const ast::BlockStatement* stmts) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool TypeDeterminer::DetermineStatements(const ast::StatementList& stmts) {
|
||||
for (const auto& stmt : stmts) {
|
||||
if (!DetermineVariableStorageClass(stmt.get())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!DetermineResultType(stmt.get())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TypeDeterminer::DetermineVariableStorageClass(ast::Statement* stmt) {
|
||||
if (!stmt->IsVariableDecl()) {
|
||||
return true;
|
||||
|
|
|
@ -65,10 +65,6 @@ class TypeDeterminer {
|
|||
/// @param stmts the statements to check
|
||||
/// @returns true if the determination was successful
|
||||
bool DetermineStatements(const ast::BlockStatement* stmts);
|
||||
/// Determines type information for a set of statements
|
||||
/// @param stmts the statements to check
|
||||
/// @returns true if the determination was successful
|
||||
bool DetermineStatements(const ast::StatementList& stmts);
|
||||
/// Determines type information for a statement
|
||||
/// @param stmt the statement to check
|
||||
/// @returns true if the determination was successful
|
||||
|
|
|
@ -278,9 +278,9 @@ TEST_F(TypeDeterminerTest, Stmt_Loop) {
|
|||
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
||||
auto* body_rhs_ptr = body_rhs.get();
|
||||
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::AssignmentStatement>(
|
||||
std::move(body_lhs), std::move(body_rhs)));
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::AssignmentStatement>(std::move(body_lhs),
|
||||
std::move(body_rhs)));
|
||||
|
||||
auto continuing_lhs = std::make_unique<ast::ScalarConstructorExpression>(
|
||||
std::make_unique<ast::SintLiteral>(&i32, 2));
|
||||
|
@ -290,8 +290,8 @@ TEST_F(TypeDeterminerTest, Stmt_Loop) {
|
|||
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
||||
auto* continuing_rhs_ptr = continuing_rhs.get();
|
||||
|
||||
ast::StatementList continuing;
|
||||
continuing.push_back(std::make_unique<ast::AssignmentStatement>(
|
||||
auto continuing = std::make_unique<ast::BlockStatement>();
|
||||
continuing->append(std::make_unique<ast::AssignmentStatement>(
|
||||
std::move(continuing_lhs), std::move(continuing_rhs)));
|
||||
|
||||
ast::LoopStatement stmt(std::move(body), std::move(continuing));
|
||||
|
|
|
@ -1471,9 +1471,9 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
|
|||
|
||||
if (stmt->has_continuing()) {
|
||||
make_indent();
|
||||
out_ << "if (!" << guard << ")";
|
||||
out_ << "if (!" << guard << ") ";
|
||||
|
||||
if (!EmitStatementBlockAndNewline(stmt->continuing())) {
|
||||
if (!EmitBlockAndNewline(stmt->continuing())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1482,7 +1482,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
|
|||
out_ << std::endl;
|
||||
}
|
||||
|
||||
for (const auto& s : stmt->body()) {
|
||||
for (const auto& s : *(stmt->body())) {
|
||||
if (!EmitStatement(s.get())) {
|
||||
return false;
|
||||
}
|
||||
|
@ -1594,7 +1594,7 @@ bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitBlockAndNewline(ast::BlockStatement* stmt) {
|
||||
bool GeneratorImpl::EmitBlockAndNewline(const ast::BlockStatement* stmt) {
|
||||
const bool result = EmitBlock(stmt);
|
||||
if (result) {
|
||||
out_ << std::endl;
|
||||
|
@ -1611,33 +1611,6 @@ bool GeneratorImpl::EmitIndentedBlockAndNewline(ast::BlockStatement* stmt) {
|
|||
return result;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitStatementBlock(const ast::StatementList& statements) {
|
||||
out_ << " {" << std::endl;
|
||||
|
||||
increment_indent();
|
||||
|
||||
for (const auto& s : statements) {
|
||||
if (!EmitStatement(s.get())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
decrement_indent();
|
||||
make_indent();
|
||||
out_ << "}";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitStatementBlockAndNewline(
|
||||
const ast::StatementList& statements) {
|
||||
const bool result = EmitStatementBlock(statements);
|
||||
if (result) {
|
||||
out_ << std::endl;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||
if (stmt->IsAssign()) {
|
||||
return EmitAssign(stmt->AsAssign());
|
||||
|
|
|
@ -84,7 +84,7 @@ class GeneratorImpl : public TextGenerator {
|
|||
/// 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);
|
||||
bool EmitBlockAndNewline(const ast::BlockStatement* stmt);
|
||||
/// Handles a break statement
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted successfully
|
||||
|
@ -177,14 +177,6 @@ class GeneratorImpl : public TextGenerator {
|
|||
/// Handles emitting a pipeline stage name
|
||||
/// @param stage the stage to emit
|
||||
void EmitStage(ast::PipelineStage stage);
|
||||
/// Handles a brace-enclosed list of statements.
|
||||
/// @param statements the statements to output
|
||||
/// @returns true if the statements were emitted
|
||||
bool EmitStatementBlock(const ast::StatementList& statements);
|
||||
/// Handles a brace-enclosed list of statements and trailing newline.
|
||||
/// @param statements the statements to output
|
||||
/// @returns true if the statements were emitted
|
||||
bool EmitStatementBlockAndNewline(const ast::StatementList& statements);
|
||||
/// Handles statement
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted
|
||||
|
|
|
@ -35,8 +35,8 @@ namespace {
|
|||
using MslGeneratorImplTest = testing::Test;
|
||||
|
||||
TEST_F(MslGeneratorImplTest, Emit_Loop) {
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::DiscardStatement>());
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
ast::LoopStatement l(std::move(body), {});
|
||||
|
||||
|
@ -52,11 +52,11 @@ TEST_F(MslGeneratorImplTest, Emit_Loop) {
|
|||
}
|
||||
|
||||
TEST_F(MslGeneratorImplTest, Emit_LoopWithContinuing) {
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::DiscardStatement>());
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
ast::StatementList continuing;
|
||||
continuing.push_back(std::make_unique<ast::ReturnStatement>());
|
||||
auto continuing = std::make_unique<ast::BlockStatement>();
|
||||
continuing->append(std::make_unique<ast::ReturnStatement>());
|
||||
|
||||
ast::LoopStatement l(std::move(body), std::move(continuing));
|
||||
|
||||
|
@ -82,20 +82,23 @@ TEST_F(MslGeneratorImplTest, Emit_LoopWithContinuing) {
|
|||
TEST_F(MslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
|
||||
ast::type::F32Type f32;
|
||||
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::DiscardStatement>());
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
ast::StatementList continuing;
|
||||
continuing.push_back(std::make_unique<ast::ReturnStatement>());
|
||||
auto continuing = std::make_unique<ast::BlockStatement>();
|
||||
continuing->append(std::make_unique<ast::ReturnStatement>());
|
||||
|
||||
auto inner = std::make_unique<ast::LoopStatement>(std::move(body),
|
||||
std::move(continuing));
|
||||
|
||||
body.push_back(std::move(inner));
|
||||
body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::move(inner));
|
||||
|
||||
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
|
||||
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
|
||||
continuing.push_back(std::make_unique<ast::AssignmentStatement>(
|
||||
|
||||
continuing = std::make_unique<ast::BlockStatement>();
|
||||
continuing->append(std::make_unique<ast::AssignmentStatement>(
|
||||
std::move(lhs), std::move(rhs)));
|
||||
|
||||
ast::LoopStatement outer(std::move(body), std::move(continuing));
|
||||
|
@ -138,16 +141,17 @@ TEST_F(MslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) {
|
|||
var->set_constructor(std::make_unique<ast::ScalarConstructorExpression>(
|
||||
std::make_unique<ast::FloatLiteral>(&f32, 2.4)));
|
||||
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::VariableDeclStatement>(std::move(var)));
|
||||
body.push_back(std::make_unique<ast::VariableDeclStatement>(
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::VariableDeclStatement>(std::move(var)));
|
||||
body->append(std::make_unique<ast::VariableDeclStatement>(
|
||||
std::make_unique<ast::Variable>("other", ast::StorageClass::kFunction,
|
||||
&f32)));
|
||||
|
||||
ast::StatementList continuing;
|
||||
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
|
||||
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
|
||||
continuing.push_back(std::make_unique<ast::AssignmentStatement>(
|
||||
|
||||
auto continuing = std::make_unique<ast::BlockStatement>();
|
||||
continuing->append(std::make_unique<ast::AssignmentStatement>(
|
||||
std::move(lhs), std::move(rhs)));
|
||||
|
||||
ast::LoopStatement outer(std::move(body), std::move(continuing));
|
||||
|
@ -172,6 +176,7 @@ TEST_F(MslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) {
|
|||
}
|
||||
)");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace msl
|
||||
} // namespace writer
|
||||
|
|
|
@ -110,16 +110,6 @@ bool LastIsTerminator(const ast::BlockStatement* stmts) {
|
|||
last->IsReturn() || last->IsFallthrough();
|
||||
}
|
||||
|
||||
bool LastIsTerminator(const ast::StatementList& stmts) {
|
||||
if (stmts.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto* last = stmts.back().get();
|
||||
return last->IsBreak() || last->IsContinue() || last->IsDiscard() ||
|
||||
last->IsReturn() || last->IsFallthrough();
|
||||
}
|
||||
|
||||
uint32_t IndexFromName(char name) {
|
||||
switch (name) {
|
||||
case 'x':
|
||||
|
@ -1794,7 +1784,7 @@ bool Builder::GenerateLoopStatement(ast::LoopStatement* stmt) {
|
|||
|
||||
push_function_inst(spv::Op::OpBranch, {Operand::Int(body_block_id)});
|
||||
GenerateLabel(body_block_id);
|
||||
if (!GenerateStatementList(stmt->body())) {
|
||||
if (!GenerateBlockStatement(stmt->body())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1804,7 +1794,7 @@ bool Builder::GenerateLoopStatement(ast::LoopStatement* stmt) {
|
|||
}
|
||||
|
||||
GenerateLabel(continue_block_id);
|
||||
if (!GenerateStatementList(stmt->continuing())) {
|
||||
if (!GenerateBlockStatement(stmt->continuing())) {
|
||||
return false;
|
||||
}
|
||||
push_function_inst(spv::Op::OpBranch, {Operand::Int(loop_header_id)});
|
||||
|
@ -1817,15 +1807,6 @@ bool Builder::GenerateLoopStatement(ast::LoopStatement* stmt) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool Builder::GenerateStatementList(const ast::StatementList& list) {
|
||||
for (const auto& inst : list) {
|
||||
if (!GenerateStatement(inst.get())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Builder::GenerateStatement(ast::Statement* stmt) {
|
||||
if (stmt->IsAssign()) {
|
||||
return GenerateAssignStatement(stmt->AsAssign());
|
||||
|
|
|
@ -315,10 +315,6 @@ class Builder {
|
|||
/// @param stmt the statement to generate
|
||||
/// @returns true if the statement was generated
|
||||
bool GenerateStatement(ast::Statement* stmt);
|
||||
/// Generates a list of statements
|
||||
/// @param list the statement list to generate
|
||||
/// @returns true on successful generation
|
||||
bool GenerateStatementList(const ast::StatementList& list);
|
||||
/// Geneates an OpLoad
|
||||
/// @param type the type to load
|
||||
/// @param id the variable id to load
|
||||
|
|
|
@ -391,10 +391,11 @@ TEST_F(BuilderTest, If_WithBreak) {
|
|||
auto if_stmt =
|
||||
std::make_unique<ast::IfStatement>(std::move(cond), std::move(if_body));
|
||||
|
||||
ast::StatementList loop_body;
|
||||
loop_body.push_back(std::move(if_stmt));
|
||||
auto loop_body = std::make_unique<ast::BlockStatement>();
|
||||
loop_body->append(std::move(if_stmt));
|
||||
|
||||
ast::LoopStatement expr(std::move(loop_body), {});
|
||||
ast::LoopStatement expr(std::move(loop_body),
|
||||
std::make_unique<ast::BlockStatement>());
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
|
@ -449,10 +450,11 @@ TEST_F(BuilderTest, If_WithElseBreak) {
|
|||
std::move(cond), std::make_unique<ast::BlockStatement>());
|
||||
if_stmt->set_else_statements(std::move(else_stmts));
|
||||
|
||||
ast::StatementList loop_body;
|
||||
loop_body.push_back(std::move(if_stmt));
|
||||
auto loop_body = std::make_unique<ast::BlockStatement>();
|
||||
loop_body->append(std::move(if_stmt));
|
||||
|
||||
ast::LoopStatement expr(std::move(loop_body), {});
|
||||
ast::LoopStatement expr(std::move(loop_body),
|
||||
std::make_unique<ast::BlockStatement>());
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
|
@ -503,10 +505,11 @@ TEST_F(BuilderTest, If_WithContinue) {
|
|||
auto if_stmt =
|
||||
std::make_unique<ast::IfStatement>(std::move(cond), std::move(if_body));
|
||||
|
||||
ast::StatementList loop_body;
|
||||
loop_body.push_back(std::move(if_stmt));
|
||||
auto loop_body = std::make_unique<ast::BlockStatement>();
|
||||
loop_body->append(std::move(if_stmt));
|
||||
|
||||
ast::LoopStatement expr(std::move(loop_body), {});
|
||||
ast::LoopStatement expr(std::move(loop_body),
|
||||
std::make_unique<ast::BlockStatement>());
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
|
@ -561,10 +564,11 @@ TEST_F(BuilderTest, If_WithElseContinue) {
|
|||
std::move(cond), std::make_unique<ast::BlockStatement>());
|
||||
if_stmt->set_else_statements(std::move(else_stmts));
|
||||
|
||||
ast::StatementList loop_body;
|
||||
loop_body.push_back(std::move(if_stmt));
|
||||
auto loop_body = std::make_unique<ast::BlockStatement>();
|
||||
loop_body->append(std::move(if_stmt));
|
||||
|
||||
ast::LoopStatement expr(std::move(loop_body), {});
|
||||
ast::LoopStatement expr(std::move(loop_body),
|
||||
std::make_unique<ast::BlockStatement>());
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
|
|
|
@ -72,14 +72,14 @@ TEST_F(BuilderTest, Loop_WithoutContinuing) {
|
|||
auto var =
|
||||
std::make_unique<ast::Variable>("v", ast::StorageClass::kPrivate, &i32);
|
||||
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::AssignmentStatement>(
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::AssignmentStatement>(
|
||||
std::make_unique<ast::IdentifierExpression>("v"),
|
||||
std::make_unique<ast::ScalarConstructorExpression>(
|
||||
std::make_unique<ast::SintLiteral>(&i32, 2))));
|
||||
|
||||
ast::StatementList continuing;
|
||||
ast::LoopStatement expr(std::move(body), std::move(continuing));
|
||||
ast::LoopStatement expr(std::move(body),
|
||||
std::make_unique<ast::BlockStatement>());
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
|
@ -124,14 +124,14 @@ TEST_F(BuilderTest, Loop_WithContinuing) {
|
|||
auto var =
|
||||
std::make_unique<ast::Variable>("v", ast::StorageClass::kPrivate, &i32);
|
||||
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::AssignmentStatement>(
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::AssignmentStatement>(
|
||||
std::make_unique<ast::IdentifierExpression>("v"),
|
||||
std::make_unique<ast::ScalarConstructorExpression>(
|
||||
std::make_unique<ast::SintLiteral>(&i32, 2))));
|
||||
|
||||
ast::StatementList continuing;
|
||||
continuing.push_back(std::make_unique<ast::AssignmentStatement>(
|
||||
auto continuing = std::make_unique<ast::BlockStatement>();
|
||||
continuing->append(std::make_unique<ast::AssignmentStatement>(
|
||||
std::make_unique<ast::IdentifierExpression>("v"),
|
||||
std::make_unique<ast::ScalarConstructorExpression>(
|
||||
std::make_unique<ast::SintLiteral>(&i32, 3))));
|
||||
|
@ -174,11 +174,11 @@ TEST_F(BuilderTest, Loop_WithContinue) {
|
|||
// loop {
|
||||
// continue;
|
||||
// }
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::ContinueStatement>());
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::ContinueStatement>());
|
||||
|
||||
ast::StatementList continuing;
|
||||
ast::LoopStatement expr(std::move(body), std::move(continuing));
|
||||
ast::LoopStatement expr(std::move(body),
|
||||
std::make_unique<ast::BlockStatement>());
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
|
@ -206,11 +206,11 @@ TEST_F(BuilderTest, Loop_WithBreak) {
|
|||
// loop {
|
||||
// break;
|
||||
// }
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::BreakStatement>());
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::BreakStatement>());
|
||||
|
||||
ast::StatementList continuing;
|
||||
ast::LoopStatement expr(std::move(body), std::move(continuing));
|
||||
ast::LoopStatement expr(std::move(body),
|
||||
std::make_unique<ast::BlockStatement>());
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
|
|
|
@ -635,33 +635,6 @@ bool GeneratorImpl::EmitBlockAndNewline(const ast::BlockStatement* stmt) {
|
|||
return result;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitStatementBlock(const ast::StatementList& statements) {
|
||||
out_ << " {" << std::endl;
|
||||
|
||||
increment_indent();
|
||||
|
||||
for (const auto& s : statements) {
|
||||
if (!EmitStatement(s.get())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
decrement_indent();
|
||||
make_indent();
|
||||
out_ << "}";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitStatementBlockAndNewline(
|
||||
const ast::StatementList& statements) {
|
||||
const bool result = EmitStatementBlock(statements);
|
||||
if (result) {
|
||||
out_ << std::endl;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||
if (stmt->IsAssign()) {
|
||||
return EmitAssign(stmt->AsAssign());
|
||||
|
@ -820,7 +793,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
|
|||
out_ << "loop {" << std::endl;
|
||||
increment_indent();
|
||||
|
||||
for (const auto& s : stmt->body()) {
|
||||
for (const auto& s : *(stmt->body())) {
|
||||
if (!EmitStatement(s.get())) {
|
||||
return false;
|
||||
}
|
||||
|
@ -830,9 +803,9 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
|
|||
out_ << std::endl;
|
||||
|
||||
make_indent();
|
||||
out_ << "continuing";
|
||||
out_ << "continuing ";
|
||||
|
||||
if (!EmitStatementBlockAndNewline(stmt->continuing())) {
|
||||
if (!EmitBlockAndNewline(stmt->continuing())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -159,14 +159,6 @@ class GeneratorImpl : public TextGenerator {
|
|||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was successfully emitted
|
||||
bool EmitReturn(ast::ReturnStatement* stmt);
|
||||
/// Handles a brace-enclosed list of statements.
|
||||
/// @param statements the statements to output
|
||||
/// @returns true if the statements were emitted
|
||||
bool EmitStatementBlock(const ast::StatementList& statements);
|
||||
/// Handles a brace-enclosed list of statements and trailing newline.
|
||||
/// @param statements the statements to output
|
||||
/// @returns true if the statements were emitted
|
||||
bool EmitStatementBlockAndNewline(const ast::StatementList& statements);
|
||||
/// Handles statement
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted
|
||||
|
|
|
@ -27,9 +27,8 @@ namespace {
|
|||
using WgslGeneratorImplTest = testing::Test;
|
||||
|
||||
TEST_F(WgslGeneratorImplTest, Emit_Loop) {
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::DiscardStatement>());
|
||||
ast::LoopStatement l(std::move(body), {});
|
||||
|
||||
GeneratorImpl g;
|
||||
|
@ -43,11 +42,11 @@ TEST_F(WgslGeneratorImplTest, Emit_Loop) {
|
|||
}
|
||||
|
||||
TEST_F(WgslGeneratorImplTest, Emit_LoopWithContinuing) {
|
||||
ast::StatementList body;
|
||||
body.push_back(std::make_unique<ast::DiscardStatement>());
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
ast::StatementList continuing;
|
||||
continuing.push_back(std::make_unique<ast::DiscardStatement>());
|
||||
auto continuing = std::make_unique<ast::BlockStatement>();
|
||||
continuing->append(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
ast::LoopStatement l(std::move(body), std::move(continuing));
|
||||
|
||||
|
|
Loading…
Reference in New Issue