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