Convert IfStatement and ElseStatement to use BlockStatement.
This CL converts the IfStatement and ElseStatement classes to using a BlockStatement internally. All usages have been updated execept for the two readers. Bug: tint:130 Change-Id: I59a5b92f22f35f27f17e005aa9b266f4a2b0ef25 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25722 Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
parent
7c2fa1e7bc
commit
77040b85d9
|
@ -17,21 +17,45 @@
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace ast {
|
namespace ast {
|
||||||
|
|
||||||
ElseStatement::ElseStatement() : Statement() {}
|
ElseStatement::ElseStatement()
|
||||||
|
: Statement(), body_(std::make_unique<ast::BlockStatement>()) {}
|
||||||
|
|
||||||
ElseStatement::ElseStatement(StatementList body)
|
ElseStatement::ElseStatement(std::unique_ptr<BlockStatement> body)
|
||||||
: Statement(), body_(std::move(body)) {}
|
: Statement(), body_(std::move(body)) {}
|
||||||
|
|
||||||
ElseStatement::ElseStatement(std::unique_ptr<Expression> condition,
|
ElseStatement::ElseStatement(std::unique_ptr<Expression> condition,
|
||||||
StatementList body)
|
StatementList body)
|
||||||
|
: Statement(),
|
||||||
|
condition_(std::move(condition)),
|
||||||
|
body_(std::make_unique<BlockStatement>()) {
|
||||||
|
set_body(std::move(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
ElseStatement::ElseStatement(std::unique_ptr<Expression> condition,
|
||||||
|
std::unique_ptr<BlockStatement> body)
|
||||||
: Statement(), condition_(std::move(condition)), body_(std::move(body)) {}
|
: Statement(), condition_(std::move(condition)), body_(std::move(body)) {}
|
||||||
|
|
||||||
ElseStatement::ElseStatement(const Source& source, StatementList body)
|
ElseStatement::ElseStatement(const Source& source, StatementList body)
|
||||||
|
: Statement(source), body_(std::make_unique<BlockStatement>()) {
|
||||||
|
set_body(std::move(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
ElseStatement::ElseStatement(const Source& source,
|
||||||
|
std::unique_ptr<BlockStatement> body)
|
||||||
: Statement(source), body_(std::move(body)) {}
|
: Statement(source), body_(std::move(body)) {}
|
||||||
|
|
||||||
ElseStatement::ElseStatement(const Source& source,
|
ElseStatement::ElseStatement(const Source& source,
|
||||||
std::unique_ptr<Expression> condition,
|
std::unique_ptr<Expression> condition,
|
||||||
StatementList body)
|
StatementList body)
|
||||||
|
: Statement(source),
|
||||||
|
condition_(std::move(condition)),
|
||||||
|
body_(std::make_unique<BlockStatement>()) {
|
||||||
|
set_body(std::move(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
ElseStatement::ElseStatement(const Source& source,
|
||||||
|
std::unique_ptr<Expression> condition,
|
||||||
|
std::unique_ptr<BlockStatement> body)
|
||||||
: Statement(source),
|
: Statement(source),
|
||||||
condition_(std::move(condition)),
|
condition_(std::move(condition)),
|
||||||
body_(std::move(body)) {}
|
body_(std::move(body)) {}
|
||||||
|
@ -40,19 +64,21 @@ ElseStatement::ElseStatement(ElseStatement&&) = default;
|
||||||
|
|
||||||
ElseStatement::~ElseStatement() = default;
|
ElseStatement::~ElseStatement() = default;
|
||||||
|
|
||||||
|
void ElseStatement::set_body(StatementList body) {
|
||||||
|
for (auto& stmt : body) {
|
||||||
|
body_->append(std::move(stmt));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool ElseStatement::IsElse() const {
|
bool ElseStatement::IsElse() const {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ElseStatement::IsValid() const {
|
bool ElseStatement::IsValid() const {
|
||||||
for (const auto& stmt : body_) {
|
if (body_ == nullptr || !body_->IsValid()) {
|
||||||
if (stmt == nullptr || !stmt->IsValid())
|
return false;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
if (condition_)
|
return condition_ == nullptr || condition_->IsValid();
|
||||||
return condition_->IsValid();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ElseStatement::to_str(std::ostream& out, size_t indent) const {
|
void ElseStatement::to_str(std::ostream& out, size_t indent) const {
|
||||||
|
@ -71,8 +97,11 @@ void ElseStatement::to_str(std::ostream& out, size_t indent) const {
|
||||||
make_indent(out, indent + 2);
|
make_indent(out, indent + 2);
|
||||||
out << "{" << std::endl;
|
out << "{" << std::endl;
|
||||||
|
|
||||||
for (const auto& stmt : body_)
|
if (body_ != nullptr) {
|
||||||
stmt->to_str(out, indent + 4);
|
for (const auto& stmt : *body_) {
|
||||||
|
stmt->to_str(out, indent + 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
make_indent(out, indent + 2);
|
make_indent(out, indent + 2);
|
||||||
out << "}" << std::endl;
|
out << "}" << std::endl;
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "src/ast/block_statement.h"
|
||||||
#include "src/ast/expression.h"
|
#include "src/ast/expression.h"
|
||||||
#include "src/ast/statement.h"
|
#include "src/ast/statement.h"
|
||||||
|
|
||||||
|
@ -32,22 +33,38 @@ class ElseStatement : public Statement {
|
||||||
ElseStatement();
|
ElseStatement();
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// @param body the else body
|
/// @param body the else body
|
||||||
explicit ElseStatement(StatementList body);
|
explicit ElseStatement(std::unique_ptr<BlockStatement> body);
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// @param condition the else condition
|
/// @param condition the else condition
|
||||||
/// @param body the else body
|
/// @param body the else body
|
||||||
ElseStatement(std::unique_ptr<Expression> condition, StatementList body);
|
ElseStatement(std::unique_ptr<Expression> condition, StatementList body);
|
||||||
/// Constructor
|
/// Constructor
|
||||||
|
/// @param condition the else condition
|
||||||
|
/// @param body the else body
|
||||||
|
ElseStatement(std::unique_ptr<Expression> condition,
|
||||||
|
std::unique_ptr<BlockStatement> body);
|
||||||
|
/// Constructor
|
||||||
/// @param source the source information
|
/// @param source the source information
|
||||||
/// @param body the else body
|
/// @param body the else body
|
||||||
ElseStatement(const Source& source, StatementList body);
|
ElseStatement(const Source& source, StatementList body);
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// @param source the source information
|
/// @param source the source information
|
||||||
|
/// @param body the else body
|
||||||
|
ElseStatement(const Source& source, std::unique_ptr<BlockStatement> body);
|
||||||
|
/// Constructor
|
||||||
|
/// @param source the source information
|
||||||
/// @param condition the else condition
|
/// @param condition the else condition
|
||||||
/// @param body the else body
|
/// @param body the else body
|
||||||
ElseStatement(const Source& source,
|
ElseStatement(const Source& source,
|
||||||
std::unique_ptr<Expression> condition,
|
std::unique_ptr<Expression> condition,
|
||||||
StatementList body);
|
StatementList body);
|
||||||
|
/// Constructor
|
||||||
|
/// @param source the source information
|
||||||
|
/// @param condition the else condition
|
||||||
|
/// @param body the else body
|
||||||
|
ElseStatement(const Source& source,
|
||||||
|
std::unique_ptr<Expression> condition,
|
||||||
|
std::unique_ptr<BlockStatement> body);
|
||||||
/// Move constructor
|
/// Move constructor
|
||||||
ElseStatement(ElseStatement&&);
|
ElseStatement(ElseStatement&&);
|
||||||
~ElseStatement() override;
|
~ElseStatement() override;
|
||||||
|
@ -64,9 +81,11 @@ class ElseStatement : public Statement {
|
||||||
|
|
||||||
/// Sets the else body
|
/// Sets the else body
|
||||||
/// @param body the else body
|
/// @param body the else body
|
||||||
void set_body(StatementList body) { body_ = std::move(body); }
|
void set_body(std::unique_ptr<BlockStatement> body) {
|
||||||
|
body_ = std::move(body);
|
||||||
|
}
|
||||||
/// @returns the else body
|
/// @returns the else body
|
||||||
const StatementList& body() const { return body_; }
|
const BlockStatement* body() const { return body_.get(); }
|
||||||
|
|
||||||
/// @returns true if this is a else statement
|
/// @returns true if this is a else statement
|
||||||
bool IsElse() const override;
|
bool IsElse() const override;
|
||||||
|
@ -82,8 +101,12 @@ class ElseStatement : public Statement {
|
||||||
private:
|
private:
|
||||||
ElseStatement(const ElseStatement&) = delete;
|
ElseStatement(const ElseStatement&) = delete;
|
||||||
|
|
||||||
|
/// Sets the else body
|
||||||
|
/// @param body the else body
|
||||||
|
void set_body(StatementList body);
|
||||||
|
|
||||||
std::unique_ptr<Expression> condition_;
|
std::unique_ptr<Expression> condition_;
|
||||||
StatementList body_;
|
std::unique_ptr<BlockStatement> body_;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A list of unique else statements
|
/// A list of unique else statements
|
||||||
|
|
|
@ -31,20 +31,20 @@ TEST_F(ElseStatementTest, Creation) {
|
||||||
ast::type::BoolType bool_type;
|
ast::type::BoolType bool_type;
|
||||||
auto cond = std::make_unique<ScalarConstructorExpression>(
|
auto cond = std::make_unique<ScalarConstructorExpression>(
|
||||||
std::make_unique<BoolLiteral>(&bool_type, true));
|
std::make_unique<BoolLiteral>(&bool_type, true));
|
||||||
StatementList body;
|
auto body = std::make_unique<BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
auto* cond_ptr = cond.get();
|
auto* cond_ptr = cond.get();
|
||||||
auto* discard_ptr = body[0].get();
|
auto* discard_ptr = body->get(0);
|
||||||
|
|
||||||
ElseStatement e(std::move(cond), std::move(body));
|
ElseStatement e(std::move(cond), std::move(body));
|
||||||
EXPECT_EQ(e.condition(), cond_ptr);
|
EXPECT_EQ(e.condition(), cond_ptr);
|
||||||
ASSERT_EQ(e.body().size(), 1u);
|
ASSERT_EQ(e.body()->size(), 1u);
|
||||||
EXPECT_EQ(e.body()[0].get(), discard_ptr);
|
EXPECT_EQ(e.body()->get(0), discard_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ElseStatementTest, Creation_WithSource) {
|
TEST_F(ElseStatementTest, Creation_WithSource) {
|
||||||
ElseStatement e(Source{20, 2}, {});
|
ElseStatement e(Source{20, 2}, std::make_unique<BlockStatement>());
|
||||||
auto src = e.source();
|
auto src = e.source();
|
||||||
EXPECT_EQ(src.line, 20u);
|
EXPECT_EQ(src.line, 20u);
|
||||||
EXPECT_EQ(src.column, 2u);
|
EXPECT_EQ(src.column, 2u);
|
||||||
|
@ -59,7 +59,7 @@ TEST_F(ElseStatementTest, HasCondition) {
|
||||||
ast::type::BoolType bool_type;
|
ast::type::BoolType bool_type;
|
||||||
auto cond = std::make_unique<ScalarConstructorExpression>(
|
auto cond = std::make_unique<ScalarConstructorExpression>(
|
||||||
std::make_unique<BoolLiteral>(&bool_type, true));
|
std::make_unique<BoolLiteral>(&bool_type, true));
|
||||||
ElseStatement e(std::move(cond), {});
|
ElseStatement e(std::move(cond), std::make_unique<BlockStatement>());
|
||||||
EXPECT_TRUE(e.HasCondition());
|
EXPECT_TRUE(e.HasCondition());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,17 +74,17 @@ TEST_F(ElseStatementTest, IsValid) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ElseStatementTest, IsValid_WithBody) {
|
TEST_F(ElseStatementTest, IsValid_WithBody) {
|
||||||
StatementList body;
|
auto body = std::make_unique<BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
ElseStatement e(std::move(body));
|
ElseStatement e(std::move(body));
|
||||||
EXPECT_TRUE(e.IsValid());
|
EXPECT_TRUE(e.IsValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ElseStatementTest, IsValid_WithNullBodyStatement) {
|
TEST_F(ElseStatementTest, IsValid_WithNullBodyStatement) {
|
||||||
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);
|
||||||
|
|
||||||
ElseStatement e(std::move(body));
|
ElseStatement e(std::move(body));
|
||||||
EXPECT_FALSE(e.IsValid());
|
EXPECT_FALSE(e.IsValid());
|
||||||
|
@ -92,13 +92,13 @@ TEST_F(ElseStatementTest, IsValid_WithNullBodyStatement) {
|
||||||
|
|
||||||
TEST_F(ElseStatementTest, IsValid_InvalidCondition) {
|
TEST_F(ElseStatementTest, IsValid_InvalidCondition) {
|
||||||
auto cond = std::make_unique<ScalarConstructorExpression>();
|
auto cond = std::make_unique<ScalarConstructorExpression>();
|
||||||
ElseStatement e(std::move(cond), {});
|
ElseStatement e(std::move(cond), std::make_unique<BlockStatement>());
|
||||||
EXPECT_FALSE(e.IsValid());
|
EXPECT_FALSE(e.IsValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ElseStatementTest, IsValid_InvalidBodyStatement) {
|
TEST_F(ElseStatementTest, IsValid_InvalidBodyStatement) {
|
||||||
StatementList body;
|
auto body = std::make_unique<BlockStatement>();
|
||||||
body.push_back(std::make_unique<IfStatement>());
|
body->append(std::make_unique<IfStatement>());
|
||||||
|
|
||||||
ElseStatement e(std::move(body));
|
ElseStatement e(std::move(body));
|
||||||
EXPECT_FALSE(e.IsValid());
|
EXPECT_FALSE(e.IsValid());
|
||||||
|
@ -108,8 +108,8 @@ TEST_F(ElseStatementTest, ToStr) {
|
||||||
ast::type::BoolType bool_type;
|
ast::type::BoolType bool_type;
|
||||||
auto cond = std::make_unique<ScalarConstructorExpression>(
|
auto cond = std::make_unique<ScalarConstructorExpression>(
|
||||||
std::make_unique<BoolLiteral>(&bool_type, true));
|
std::make_unique<BoolLiteral>(&bool_type, true));
|
||||||
StatementList body;
|
auto body = std::make_unique<BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
ElseStatement e(std::move(cond), std::move(body));
|
ElseStatement e(std::move(cond), std::move(body));
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
|
@ -126,8 +126,8 @@ TEST_F(ElseStatementTest, ToStr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ElseStatementTest, ToStr_NoCondition) {
|
TEST_F(ElseStatementTest, ToStr_NoCondition) {
|
||||||
StatementList body;
|
auto body = std::make_unique<BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
ElseStatement e(std::move(body));
|
ElseStatement e(std::move(body));
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
|
|
|
@ -19,15 +19,25 @@
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace ast {
|
namespace ast {
|
||||||
|
|
||||||
IfStatement::IfStatement() : Statement() {}
|
IfStatement::IfStatement()
|
||||||
|
: Statement(), body_(std::make_unique<BlockStatement>()) {}
|
||||||
|
|
||||||
IfStatement::IfStatement(std::unique_ptr<Expression> condition,
|
IfStatement::IfStatement(std::unique_ptr<Expression> condition,
|
||||||
StatementList body)
|
std::unique_ptr<BlockStatement> body)
|
||||||
: Statement(), condition_(std::move(condition)), body_(std::move(body)) {}
|
: Statement(), condition_(std::move(condition)), body_(std::move(body)) {}
|
||||||
|
|
||||||
IfStatement::IfStatement(const Source& source,
|
IfStatement::IfStatement(const Source& source,
|
||||||
std::unique_ptr<Expression> condition,
|
std::unique_ptr<Expression> condition,
|
||||||
StatementList body)
|
StatementList body)
|
||||||
|
: Statement(source),
|
||||||
|
condition_(std::move(condition)),
|
||||||
|
body_(std::make_unique<BlockStatement>()) {
|
||||||
|
set_body(std::move(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
IfStatement::IfStatement(const Source& source,
|
||||||
|
std::unique_ptr<Expression> condition,
|
||||||
|
std::unique_ptr<BlockStatement> body)
|
||||||
: Statement(source),
|
: Statement(source),
|
||||||
condition_(std::move(condition)),
|
condition_(std::move(condition)),
|
||||||
body_(std::move(body)) {}
|
body_(std::move(body)) {}
|
||||||
|
@ -36,17 +46,22 @@ IfStatement::IfStatement(IfStatement&&) = default;
|
||||||
|
|
||||||
IfStatement::~IfStatement() = default;
|
IfStatement::~IfStatement() = default;
|
||||||
|
|
||||||
|
void IfStatement::set_body(StatementList body) {
|
||||||
|
for (auto& stmt : body) {
|
||||||
|
body_->append(std::move(stmt));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool IfStatement::IsIf() const {
|
bool IfStatement::IsIf() const {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IfStatement::IsValid() const {
|
bool IfStatement::IsValid() const {
|
||||||
if (condition_ == nullptr || !condition_->IsValid())
|
if (condition_ == nullptr || !condition_->IsValid()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (body_ == nullptr || !body_->IsValid()) {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (const auto& stmt : body_) {
|
|
||||||
if (stmt == nullptr || !stmt->IsValid())
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool found_else = false;
|
bool found_else = false;
|
||||||
|
@ -83,8 +98,11 @@ void IfStatement::to_str(std::ostream& out, size_t indent) const {
|
||||||
make_indent(out, indent + 2);
|
make_indent(out, indent + 2);
|
||||||
out << "{" << std::endl;
|
out << "{" << std::endl;
|
||||||
|
|
||||||
for (const auto& stmt : body_)
|
if (body_ != nullptr) {
|
||||||
stmt->to_str(out, indent + 4);
|
for (const auto& stmt : *body_) {
|
||||||
|
stmt->to_str(out, indent + 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Close the if body
|
// Close the if body
|
||||||
make_indent(out, indent + 2);
|
make_indent(out, indent + 2);
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "src/ast/block_statement.h"
|
||||||
#include "src/ast/else_statement.h"
|
#include "src/ast/else_statement.h"
|
||||||
#include "src/ast/expression.h"
|
#include "src/ast/expression.h"
|
||||||
#include "src/ast/statement.h"
|
#include "src/ast/statement.h"
|
||||||
|
@ -33,7 +34,8 @@ class IfStatement : public Statement {
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// @param condition the if condition
|
/// @param condition the if condition
|
||||||
/// @param body the if body
|
/// @param body the if body
|
||||||
IfStatement(std::unique_ptr<Expression> condition, StatementList body);
|
IfStatement(std::unique_ptr<Expression> condition,
|
||||||
|
std::unique_ptr<BlockStatement> body);
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// @param source the source information
|
/// @param source the source information
|
||||||
/// @param condition the if condition
|
/// @param condition the if condition
|
||||||
|
@ -41,6 +43,13 @@ class IfStatement : public Statement {
|
||||||
IfStatement(const Source& source,
|
IfStatement(const Source& source,
|
||||||
std::unique_ptr<Expression> condition,
|
std::unique_ptr<Expression> condition,
|
||||||
StatementList body);
|
StatementList body);
|
||||||
|
/// Constructor
|
||||||
|
/// @param source the source information
|
||||||
|
/// @param condition the if condition
|
||||||
|
/// @param body the if body
|
||||||
|
IfStatement(const Source& source,
|
||||||
|
std::unique_ptr<Expression> condition,
|
||||||
|
std::unique_ptr<BlockStatement> body);
|
||||||
/// Move constructor
|
/// Move constructor
|
||||||
IfStatement(IfStatement&&);
|
IfStatement(IfStatement&&);
|
||||||
~IfStatement() override;
|
~IfStatement() override;
|
||||||
|
@ -55,9 +64,14 @@ class IfStatement : public Statement {
|
||||||
|
|
||||||
/// Sets the if body
|
/// Sets the if body
|
||||||
/// @param body the if body
|
/// @param body the if body
|
||||||
void set_body(StatementList body) { body_ = std::move(body); }
|
void set_body(StatementList body);
|
||||||
|
/// Sets the if body
|
||||||
|
/// @param body the if body
|
||||||
|
void set_body(std::unique_ptr<BlockStatement> body) {
|
||||||
|
body_ = std::move(body);
|
||||||
|
}
|
||||||
/// @returns the if body
|
/// @returns the if body
|
||||||
const StatementList& body() const { return body_; }
|
const BlockStatement* body() const { return body_.get(); }
|
||||||
|
|
||||||
/// Sets the else statements
|
/// Sets the else statements
|
||||||
/// @param else_statements the else statements to set
|
/// @param else_statements the else statements to set
|
||||||
|
@ -84,7 +98,7 @@ class IfStatement : public Statement {
|
||||||
IfStatement(const IfStatement&) = delete;
|
IfStatement(const IfStatement&) = delete;
|
||||||
|
|
||||||
std::unique_ptr<Expression> condition_;
|
std::unique_ptr<Expression> condition_;
|
||||||
StatementList body_;
|
std::unique_ptr<BlockStatement> body_;
|
||||||
ElseStatementList else_statements_;
|
ElseStatementList else_statements_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,22 +26,22 @@ using IfStatementTest = testing::Test;
|
||||||
|
|
||||||
TEST_F(IfStatementTest, Creation) {
|
TEST_F(IfStatementTest, Creation) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
auto* cond_ptr = cond.get();
|
auto* cond_ptr = cond.get();
|
||||||
auto* stmt_ptr = body[0].get();
|
auto* stmt_ptr = body->get(0);
|
||||||
|
|
||||||
IfStatement stmt(std::move(cond), std::move(body));
|
IfStatement stmt(std::move(cond), std::move(body));
|
||||||
EXPECT_EQ(stmt.condition(), cond_ptr);
|
EXPECT_EQ(stmt.condition(), cond_ptr);
|
||||||
ASSERT_EQ(stmt.body().size(), 1u);
|
ASSERT_EQ(stmt.body()->size(), 1u);
|
||||||
EXPECT_EQ(stmt.body()[0].get(), stmt_ptr);
|
EXPECT_EQ(stmt.body()->get(0), stmt_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(IfStatementTest, Creation_WithSource) {
|
TEST_F(IfStatementTest, Creation_WithSource) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
IfStatement stmt(Source{20, 2}, std::move(cond), std::move(body));
|
IfStatement stmt(Source{20, 2}, std::move(cond), std::move(body));
|
||||||
auto src = stmt.source();
|
auto src = stmt.source();
|
||||||
|
@ -56,8 +56,8 @@ TEST_F(IfStatementTest, IsIf) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid) {
|
TEST_F(IfStatementTest, IsValid) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
IfStatement stmt(std::move(cond), std::move(body));
|
IfStatement stmt(std::move(cond), std::move(body));
|
||||||
EXPECT_TRUE(stmt.IsValid());
|
EXPECT_TRUE(stmt.IsValid());
|
||||||
|
@ -65,8 +65,8 @@ TEST_F(IfStatementTest, IsValid) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid_WithElseStatements) {
|
TEST_F(IfStatementTest, IsValid_WithElseStatements) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
ElseStatementList else_stmts;
|
ElseStatementList else_stmts;
|
||||||
else_stmts.push_back(std::make_unique<ElseStatement>());
|
else_stmts.push_back(std::make_unique<ElseStatement>());
|
||||||
|
@ -79,8 +79,8 @@ TEST_F(IfStatementTest, IsValid_WithElseStatements) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid_MissingCondition) {
|
TEST_F(IfStatementTest, IsValid_MissingCondition) {
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
IfStatement stmt(nullptr, std::move(body));
|
IfStatement stmt(nullptr, std::move(body));
|
||||||
EXPECT_FALSE(stmt.IsValid());
|
EXPECT_FALSE(stmt.IsValid());
|
||||||
|
@ -88,8 +88,8 @@ TEST_F(IfStatementTest, IsValid_MissingCondition) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid_InvalidCondition) {
|
TEST_F(IfStatementTest, IsValid_InvalidCondition) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("");
|
auto cond = std::make_unique<IdentifierExpression>("");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
IfStatement stmt(std::move(cond), std::move(body));
|
IfStatement stmt(std::move(cond), std::move(body));
|
||||||
EXPECT_FALSE(stmt.IsValid());
|
EXPECT_FALSE(stmt.IsValid());
|
||||||
|
@ -97,9 +97,9 @@ TEST_F(IfStatementTest, IsValid_InvalidCondition) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid_NullBodyStatement) {
|
TEST_F(IfStatementTest, IsValid_NullBodyStatement) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
body.push_back(nullptr);
|
body->append(nullptr);
|
||||||
|
|
||||||
IfStatement stmt(std::move(cond), std::move(body));
|
IfStatement stmt(std::move(cond), std::move(body));
|
||||||
EXPECT_FALSE(stmt.IsValid());
|
EXPECT_FALSE(stmt.IsValid());
|
||||||
|
@ -107,9 +107,9 @@ TEST_F(IfStatementTest, IsValid_NullBodyStatement) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) {
|
TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::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>());
|
||||||
|
|
||||||
IfStatement stmt(std::move(cond), std::move(body));
|
IfStatement stmt(std::move(cond), std::move(body));
|
||||||
EXPECT_FALSE(stmt.IsValid());
|
EXPECT_FALSE(stmt.IsValid());
|
||||||
|
@ -117,8 +117,8 @@ TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid_NullElseStatement) {
|
TEST_F(IfStatementTest, IsValid_NullElseStatement) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
ElseStatementList else_stmts;
|
ElseStatementList else_stmts;
|
||||||
else_stmts.push_back(std::make_unique<ElseStatement>());
|
else_stmts.push_back(std::make_unique<ElseStatement>());
|
||||||
|
@ -133,8 +133,8 @@ TEST_F(IfStatementTest, IsValid_NullElseStatement) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid_InvalidElseStatement) {
|
TEST_F(IfStatementTest, IsValid_InvalidElseStatement) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
ElseStatementList else_stmts;
|
ElseStatementList else_stmts;
|
||||||
else_stmts.push_back(std::make_unique<ElseStatement>());
|
else_stmts.push_back(std::make_unique<ElseStatement>());
|
||||||
|
@ -147,8 +147,8 @@ TEST_F(IfStatementTest, IsValid_InvalidElseStatement) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) {
|
TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
ElseStatementList else_stmts;
|
ElseStatementList else_stmts;
|
||||||
else_stmts.push_back(std::make_unique<ElseStatement>());
|
else_stmts.push_back(std::make_unique<ElseStatement>());
|
||||||
|
@ -161,8 +161,8 @@ TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, IsValid_ElseNotLast) {
|
TEST_F(IfStatementTest, IsValid_ElseNotLast) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
ElseStatementList else_stmts;
|
ElseStatementList else_stmts;
|
||||||
else_stmts.push_back(std::make_unique<ElseStatement>());
|
else_stmts.push_back(std::make_unique<ElseStatement>());
|
||||||
|
@ -176,8 +176,8 @@ TEST_F(IfStatementTest, IsValid_ElseNotLast) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, ToStr) {
|
TEST_F(IfStatementTest, ToStr) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
IfStatement stmt(std::move(cond), std::move(body));
|
IfStatement stmt(std::move(cond), std::move(body));
|
||||||
|
|
||||||
|
@ -196,15 +196,15 @@ TEST_F(IfStatementTest, ToStr) {
|
||||||
|
|
||||||
TEST_F(IfStatementTest, ToStr_WithElseStatements) {
|
TEST_F(IfStatementTest, ToStr_WithElseStatements) {
|
||||||
auto cond = std::make_unique<IdentifierExpression>("cond");
|
auto cond = std::make_unique<IdentifierExpression>("cond");
|
||||||
StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<DiscardStatement>());
|
body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
StatementList else_if_body;
|
auto else_if_body = std::make_unique<BlockStatement>();
|
||||||
else_if_body.push_back(std::make_unique<DiscardStatement>());
|
else_if_body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
StatementList else_body;
|
auto else_body = std::make_unique<BlockStatement>();
|
||||||
else_body.push_back(std::make_unique<DiscardStatement>());
|
else_body->append(std::make_unique<DiscardStatement>());
|
||||||
else_body.push_back(std::make_unique<DiscardStatement>());
|
else_body->append(std::make_unique<DiscardStatement>());
|
||||||
|
|
||||||
ElseStatementList else_stmts;
|
ElseStatementList else_stmts;
|
||||||
else_stmts.push_back(std::make_unique<ElseStatement>());
|
else_stmts.push_back(std::make_unique<ElseStatement>());
|
||||||
|
|
|
@ -29,7 +29,7 @@ TEST_F(ParserImplTest, ElseStmt) {
|
||||||
ASSERT_NE(e, nullptr);
|
ASSERT_NE(e, nullptr);
|
||||||
ASSERT_TRUE(e->IsElse());
|
ASSERT_TRUE(e->IsElse());
|
||||||
ASSERT_EQ(e->condition(), nullptr);
|
ASSERT_EQ(e->condition(), nullptr);
|
||||||
EXPECT_EQ(e->body().size(), 2u);
|
EXPECT_EQ(e->body()->size(), 2u);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ParserImplTest, ElseStmt_InvalidBody) {
|
TEST_F(ParserImplTest, ElseStmt_InvalidBody) {
|
||||||
|
|
|
@ -31,7 +31,7 @@ TEST_F(ParserImplTest, ElseIfStmt) {
|
||||||
ASSERT_TRUE(e[0]->IsElse());
|
ASSERT_TRUE(e[0]->IsElse());
|
||||||
ASSERT_NE(e[0]->condition(), nullptr);
|
ASSERT_NE(e[0]->condition(), nullptr);
|
||||||
ASSERT_TRUE(e[0]->condition()->IsBinary());
|
ASSERT_TRUE(e[0]->condition()->IsBinary());
|
||||||
EXPECT_EQ(e[0]->body().size(), 2u);
|
EXPECT_EQ(e[0]->body()->size(), 2u);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ParserImplTest, ElseIfStmt_Multiple) {
|
TEST_F(ParserImplTest, ElseIfStmt_Multiple) {
|
||||||
|
@ -43,12 +43,12 @@ TEST_F(ParserImplTest, ElseIfStmt_Multiple) {
|
||||||
ASSERT_TRUE(e[0]->IsElse());
|
ASSERT_TRUE(e[0]->IsElse());
|
||||||
ASSERT_NE(e[0]->condition(), nullptr);
|
ASSERT_NE(e[0]->condition(), nullptr);
|
||||||
ASSERT_TRUE(e[0]->condition()->IsBinary());
|
ASSERT_TRUE(e[0]->condition()->IsBinary());
|
||||||
EXPECT_EQ(e[0]->body().size(), 2u);
|
EXPECT_EQ(e[0]->body()->size(), 2u);
|
||||||
|
|
||||||
ASSERT_TRUE(e[1]->IsElse());
|
ASSERT_TRUE(e[1]->IsElse());
|
||||||
ASSERT_NE(e[1]->condition(), nullptr);
|
ASSERT_NE(e[1]->condition(), nullptr);
|
||||||
ASSERT_TRUE(e[1]->condition()->IsIdentifier());
|
ASSERT_TRUE(e[1]->condition()->IsIdentifier());
|
||||||
EXPECT_EQ(e[1]->body().size(), 1u);
|
EXPECT_EQ(e[1]->body()->size(), 1u);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ParserImplTest, ElseIfStmt_InvalidBody) {
|
TEST_F(ParserImplTest, ElseIfStmt_InvalidBody) {
|
||||||
|
|
|
@ -32,7 +32,7 @@ TEST_F(ParserImplTest, IfStmt) {
|
||||||
ASSERT_TRUE(e->IsIf());
|
ASSERT_TRUE(e->IsIf());
|
||||||
ASSERT_NE(e->condition(), nullptr);
|
ASSERT_NE(e->condition(), nullptr);
|
||||||
ASSERT_TRUE(e->condition()->IsBinary());
|
ASSERT_TRUE(e->condition()->IsBinary());
|
||||||
EXPECT_EQ(e->body().size(), 2u);
|
EXPECT_EQ(e->body()->size(), 2u);
|
||||||
EXPECT_EQ(e->else_statements().size(), 0u);
|
EXPECT_EQ(e->else_statements().size(), 0u);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,15 +46,15 @@ TEST_F(ParserImplTest, IfStmt_WithElse) {
|
||||||
ASSERT_TRUE(e->IsIf());
|
ASSERT_TRUE(e->IsIf());
|
||||||
ASSERT_NE(e->condition(), nullptr);
|
ASSERT_NE(e->condition(), nullptr);
|
||||||
ASSERT_TRUE(e->condition()->IsBinary());
|
ASSERT_TRUE(e->condition()->IsBinary());
|
||||||
EXPECT_EQ(e->body().size(), 2u);
|
EXPECT_EQ(e->body()->size(), 2u);
|
||||||
|
|
||||||
ASSERT_EQ(e->else_statements().size(), 2u);
|
ASSERT_EQ(e->else_statements().size(), 2u);
|
||||||
ASSERT_NE(e->else_statements()[0]->condition(), nullptr);
|
ASSERT_NE(e->else_statements()[0]->condition(), nullptr);
|
||||||
ASSERT_TRUE(e->else_statements()[0]->condition()->IsIdentifier());
|
ASSERT_TRUE(e->else_statements()[0]->condition()->IsIdentifier());
|
||||||
EXPECT_EQ(e->else_statements()[0]->body().size(), 1u);
|
EXPECT_EQ(e->else_statements()[0]->body()->size(), 1u);
|
||||||
|
|
||||||
ASSERT_EQ(e->else_statements()[1]->condition(), nullptr);
|
ASSERT_EQ(e->else_statements()[1]->condition(), nullptr);
|
||||||
EXPECT_EQ(e->else_statements()[1]->body().size(), 0u);
|
EXPECT_EQ(e->else_statements()[1]->body()->size(), 0u);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ParserImplTest, IfStmt_InvalidCondition) {
|
TEST_F(ParserImplTest, IfStmt_InvalidCondition) {
|
||||||
|
|
|
@ -195,9 +195,9 @@ TEST_F(TypeDeterminerTest, Stmt_Else) {
|
||||||
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
||||||
auto* rhs_ptr = rhs.get();
|
auto* rhs_ptr = rhs.get();
|
||||||
|
|
||||||
ast::StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<ast::AssignmentStatement>(std::move(lhs),
|
body->append(std::make_unique<ast::AssignmentStatement>(std::move(lhs),
|
||||||
std::move(rhs)));
|
std::move(rhs)));
|
||||||
|
|
||||||
ast::ElseStatement stmt(std::make_unique<ast::ScalarConstructorExpression>(
|
ast::ElseStatement stmt(std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::SintLiteral>(&i32, 3)),
|
std::make_unique<ast::SintLiteral>(&i32, 3)),
|
||||||
|
@ -224,8 +224,8 @@ TEST_F(TypeDeterminerTest, Stmt_If) {
|
||||||
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
||||||
auto* else_rhs_ptr = else_rhs.get();
|
auto* else_rhs_ptr = else_rhs.get();
|
||||||
|
|
||||||
ast::StatementList else_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
else_body.push_back(std::make_unique<ast::AssignmentStatement>(
|
else_body->append(std::make_unique<ast::AssignmentStatement>(
|
||||||
std::move(else_lhs), std::move(else_rhs)));
|
std::move(else_lhs), std::move(else_rhs)));
|
||||||
|
|
||||||
auto else_stmt = std::make_unique<ast::ElseStatement>(
|
auto else_stmt = std::make_unique<ast::ElseStatement>(
|
||||||
|
@ -244,9 +244,9 @@ TEST_F(TypeDeterminerTest, Stmt_If) {
|
||||||
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
|
||||||
auto* rhs_ptr = rhs.get();
|
auto* rhs_ptr = rhs.get();
|
||||||
|
|
||||||
ast::StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<ast::AssignmentStatement>(std::move(lhs),
|
body->append(std::make_unique<ast::AssignmentStatement>(std::move(lhs),
|
||||||
std::move(rhs)));
|
std::move(rhs)));
|
||||||
|
|
||||||
ast::IfStatement stmt(std::make_unique<ast::ScalarConstructorExpression>(
|
ast::IfStatement stmt(std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::SintLiteral>(&i32, 3)),
|
std::make_unique<ast::SintLiteral>(&i32, 3)),
|
||||||
|
|
|
@ -1516,12 +1516,12 @@ bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
|
||||||
if (!EmitExpression(stmt->condition())) {
|
if (!EmitExpression(stmt->condition())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
out_ << ")";
|
out_ << ") ";
|
||||||
} else {
|
} else {
|
||||||
out_ << " else";
|
out_ << " else ";
|
||||||
}
|
}
|
||||||
|
|
||||||
return EmitStatementBlock(stmt->body());
|
return EmitBlock(stmt->body());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
|
bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
|
||||||
|
@ -1531,9 +1531,9 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
|
||||||
if (!EmitExpression(stmt->condition())) {
|
if (!EmitExpression(stmt->condition())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
out_ << ")";
|
out_ << ") ";
|
||||||
|
|
||||||
if (!EmitStatementBlock(stmt->body())) {
|
if (!EmitBlock(stmt->body())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1577,7 +1577,7 @@ bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitBlock(ast::BlockStatement* stmt) {
|
bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) {
|
||||||
out_ << "{" << std::endl;
|
out_ << "{" << std::endl;
|
||||||
increment_indent();
|
increment_indent();
|
||||||
|
|
||||||
|
|
|
@ -76,7 +76,7 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// Handles a block statement
|
/// Handles a block 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
|
||||||
bool EmitBlock(ast::BlockStatement* stmt);
|
bool EmitBlock(const ast::BlockStatement* stmt);
|
||||||
/// 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
|
||||||
|
|
|
@ -971,8 +971,9 @@ TEST_F(MslGeneratorImplTest,
|
||||||
std::make_unique<ast::ScalarConstructorExpression>(
|
std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::FloatLiteral>(&f32, 1.0f))));
|
std::make_unique<ast::FloatLiteral>(&f32, 1.0f))));
|
||||||
|
|
||||||
ast::StatementList list;
|
auto list = std::make_unique<ast::BlockStatement>();
|
||||||
list.push_back(std::make_unique<ast::ReturnStatement>());
|
list->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
body->append(std::make_unique<ast::IfStatement>(
|
body->append(std::make_unique<ast::IfStatement>(
|
||||||
std::make_unique<ast::BinaryExpression>(
|
std::make_unique<ast::BinaryExpression>(
|
||||||
ast::BinaryOp::kEqual,
|
ast::BinaryOp::kEqual,
|
||||||
|
|
|
@ -29,8 +29,8 @@ using MslGeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
TEST_F(MslGeneratorImplTest, Emit_If) {
|
TEST_F(MslGeneratorImplTest, Emit_If) {
|
||||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||||
ast::StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<ast::ReturnStatement>());
|
body->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
ast::IfStatement i(std::move(cond), std::move(body));
|
ast::IfStatement i(std::move(cond), std::move(body));
|
||||||
|
|
||||||
|
@ -47,17 +47,16 @@ TEST_F(MslGeneratorImplTest, Emit_If) {
|
||||||
|
|
||||||
TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) {
|
TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) {
|
||||||
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
||||||
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
ast::StatementList else_body;
|
else_body->append(std::make_unique<ast::ReturnStatement>());
|
||||||
else_body.push_back(std::make_unique<ast::ReturnStatement>());
|
|
||||||
|
|
||||||
ast::ElseStatementList elses;
|
ast::ElseStatementList elses;
|
||||||
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
|
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
|
||||||
std::move(else_body)));
|
std::move(else_body)));
|
||||||
|
|
||||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||||
ast::StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<ast::ReturnStatement>());
|
body->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
ast::IfStatement i(std::move(cond), std::move(body));
|
ast::IfStatement i(std::move(cond), std::move(body));
|
||||||
i.set_else_statements(std::move(elses));
|
i.set_else_statements(std::move(elses));
|
||||||
|
@ -76,15 +75,15 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(MslGeneratorImplTest, Emit_IfWithElse) {
|
TEST_F(MslGeneratorImplTest, Emit_IfWithElse) {
|
||||||
ast::StatementList else_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
else_body.push_back(std::make_unique<ast::ReturnStatement>());
|
else_body->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
ast::ElseStatementList elses;
|
ast::ElseStatementList elses;
|
||||||
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body)));
|
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body)));
|
||||||
|
|
||||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||||
ast::StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<ast::ReturnStatement>());
|
body->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
ast::IfStatement i(std::move(cond), std::move(body));
|
ast::IfStatement i(std::move(cond), std::move(body));
|
||||||
i.set_else_statements(std::move(elses));
|
i.set_else_statements(std::move(elses));
|
||||||
|
@ -105,11 +104,11 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithElse) {
|
||||||
TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) {
|
TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) {
|
||||||
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
||||||
|
|
||||||
ast::StatementList else_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
else_body.push_back(std::make_unique<ast::ReturnStatement>());
|
else_body->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
ast::StatementList else_body_2;
|
auto else_body_2 = std::make_unique<ast::BlockStatement>();
|
||||||
else_body_2.push_back(std::make_unique<ast::ReturnStatement>());
|
else_body_2->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
ast::ElseStatementList elses;
|
ast::ElseStatementList elses;
|
||||||
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
|
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
|
||||||
|
@ -117,8 +116,8 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) {
|
||||||
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body_2)));
|
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body_2)));
|
||||||
|
|
||||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||||
ast::StatementList body;
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
body.push_back(std::make_unique<ast::ReturnStatement>());
|
body->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
ast::IfStatement i(std::move(cond), std::move(body));
|
ast::IfStatement i(std::move(cond), std::move(body));
|
||||||
i.set_else_statements(std::move(elses));
|
i.set_else_statements(std::move(elses));
|
||||||
|
|
|
@ -1600,7 +1600,7 @@ uint32_t Builder::GenerateCastExpression(ast::CastExpression* cast) {
|
||||||
|
|
||||||
bool Builder::GenerateConditionalBlock(
|
bool Builder::GenerateConditionalBlock(
|
||||||
ast::Expression* cond,
|
ast::Expression* cond,
|
||||||
const ast::StatementList& true_body,
|
const ast::BlockStatement* true_body,
|
||||||
size_t cur_else_idx,
|
size_t cur_else_idx,
|
||||||
const ast::ElseStatementList& else_stmts) {
|
const ast::ElseStatementList& else_stmts) {
|
||||||
auto cond_id = GenerateExpression(cond);
|
auto cond_id = GenerateExpression(cond);
|
||||||
|
@ -1629,7 +1629,7 @@ bool Builder::GenerateConditionalBlock(
|
||||||
|
|
||||||
// Output true block
|
// Output true block
|
||||||
GenerateLabel(true_block_id);
|
GenerateLabel(true_block_id);
|
||||||
if (!GenerateStatementList(true_body)) {
|
if (!GenerateBlockStatement(true_body)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// We only branch if the last element of the body didn't already branch.
|
// We only branch if the last element of the body didn't already branch.
|
||||||
|
@ -1644,7 +1644,7 @@ bool Builder::GenerateConditionalBlock(
|
||||||
auto* else_stmt = else_stmts[cur_else_idx].get();
|
auto* else_stmt = else_stmts[cur_else_idx].get();
|
||||||
// Handle the else case by just outputting the statements.
|
// Handle the else case by just outputting the statements.
|
||||||
if (!else_stmt->HasCondition()) {
|
if (!else_stmt->HasCondition()) {
|
||||||
if (!GenerateStatementList(else_stmt->body())) {
|
if (!GenerateBlockStatement(else_stmt->body())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -308,7 +308,7 @@ class Builder {
|
||||||
/// @param else_stmts the list of all else statements
|
/// @param else_stmts the list of all else statements
|
||||||
/// @returns true on success, false on failure
|
/// @returns true on success, false on failure
|
||||||
bool GenerateConditionalBlock(ast::Expression* cond,
|
bool GenerateConditionalBlock(ast::Expression* cond,
|
||||||
const ast::StatementList& true_body,
|
const ast::BlockStatement* true_body,
|
||||||
size_t cur_else_idx,
|
size_t cur_else_idx,
|
||||||
const ast::ElseStatementList& else_stmts);
|
const ast::ElseStatementList& else_stmts);
|
||||||
/// Generates a statement
|
/// Generates a statement
|
||||||
|
|
|
@ -48,7 +48,8 @@ TEST_F(BuilderTest, If_Empty) {
|
||||||
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
||||||
|
|
||||||
ast::IfStatement expr(std::move(cond), ast::StatementList{});
|
ast::IfStatement expr(std::move(cond),
|
||||||
|
std::make_unique<ast::BlockStatement>());
|
||||||
|
|
||||||
Context ctx;
|
Context ctx;
|
||||||
ast::Module mod;
|
ast::Module mod;
|
||||||
|
@ -81,8 +82,8 @@ TEST_F(BuilderTest, If_WithStatements) {
|
||||||
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))));
|
||||||
|
@ -133,14 +134,14 @@ TEST_F(BuilderTest, If_WithElse) {
|
||||||
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 else_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
else_body.push_back(std::make_unique<ast::AssignmentStatement>(
|
else_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, 3))));
|
std::make_unique<ast::SintLiteral>(&i32, 3))));
|
||||||
|
@ -201,14 +202,14 @@ TEST_F(BuilderTest, If_WithElseIf) {
|
||||||
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 else_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
else_body.push_back(std::make_unique<ast::AssignmentStatement>(
|
else_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, 3))));
|
std::make_unique<ast::SintLiteral>(&i32, 3))));
|
||||||
|
@ -281,23 +282,23 @@ TEST_F(BuilderTest, If_WithMultiple) {
|
||||||
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 elseif_1_body;
|
auto elseif_1_body = std::make_unique<ast::BlockStatement>();
|
||||||
elseif_1_body.push_back(std::make_unique<ast::AssignmentStatement>(
|
elseif_1_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, 3))));
|
std::make_unique<ast::SintLiteral>(&i32, 3))));
|
||||||
ast::StatementList elseif_2_body;
|
auto elseif_2_body = std::make_unique<ast::BlockStatement>();
|
||||||
elseif_2_body.push_back(std::make_unique<ast::AssignmentStatement>(
|
elseif_2_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, 4))));
|
std::make_unique<ast::SintLiteral>(&i32, 4))));
|
||||||
ast::StatementList else_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
else_body.push_back(std::make_unique<ast::AssignmentStatement>(
|
else_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, 5))));
|
std::make_unique<ast::SintLiteral>(&i32, 5))));
|
||||||
|
@ -384,8 +385,8 @@ TEST_F(BuilderTest, If_WithBreak) {
|
||||||
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
||||||
|
|
||||||
ast::StatementList if_body;
|
auto if_body = std::make_unique<ast::BlockStatement>();
|
||||||
if_body.push_back(std::make_unique<ast::BreakStatement>());
|
if_body->append(std::make_unique<ast::BreakStatement>());
|
||||||
|
|
||||||
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));
|
||||||
|
@ -437,16 +438,15 @@ TEST_F(BuilderTest, If_WithElseBreak) {
|
||||||
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
||||||
|
|
||||||
ast::StatementList if_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
ast::StatementList else_body;
|
else_body->append(std::make_unique<ast::BreakStatement>());
|
||||||
else_body.push_back(std::make_unique<ast::BreakStatement>());
|
|
||||||
|
|
||||||
ast::ElseStatementList else_stmts;
|
ast::ElseStatementList else_stmts;
|
||||||
else_stmts.push_back(
|
else_stmts.push_back(
|
||||||
std::make_unique<ast::ElseStatement>(std::move(else_body)));
|
std::make_unique<ast::ElseStatement>(std::move(else_body)));
|
||||||
|
|
||||||
auto if_stmt =
|
auto if_stmt = std::make_unique<ast::IfStatement>(
|
||||||
std::make_unique<ast::IfStatement>(std::move(cond), std::move(if_body));
|
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;
|
ast::StatementList loop_body;
|
||||||
|
@ -497,8 +497,8 @@ TEST_F(BuilderTest, If_WithContinue) {
|
||||||
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
||||||
|
|
||||||
ast::StatementList if_body;
|
auto if_body = std::make_unique<ast::BlockStatement>();
|
||||||
if_body.push_back(std::make_unique<ast::ContinueStatement>());
|
if_body->append(std::make_unique<ast::ContinueStatement>());
|
||||||
|
|
||||||
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));
|
||||||
|
@ -550,16 +550,15 @@ TEST_F(BuilderTest, If_WithElseContinue) {
|
||||||
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
||||||
|
|
||||||
ast::StatementList if_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
ast::StatementList else_body;
|
else_body->append(std::make_unique<ast::ContinueStatement>());
|
||||||
else_body.push_back(std::make_unique<ast::ContinueStatement>());
|
|
||||||
|
|
||||||
ast::ElseStatementList else_stmts;
|
ast::ElseStatementList else_stmts;
|
||||||
else_stmts.push_back(
|
else_stmts.push_back(
|
||||||
std::make_unique<ast::ElseStatement>(std::move(else_body)));
|
std::make_unique<ast::ElseStatement>(std::move(else_body)));
|
||||||
|
|
||||||
auto if_stmt =
|
auto if_stmt = std::make_unique<ast::IfStatement>(
|
||||||
std::make_unique<ast::IfStatement>(std::move(cond), std::move(if_body));
|
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;
|
ast::StatementList loop_body;
|
||||||
|
@ -608,8 +607,8 @@ TEST_F(BuilderTest, If_WithReturn) {
|
||||||
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
auto cond = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
std::make_unique<ast::BoolLiteral>(&bool_type, true));
|
||||||
|
|
||||||
ast::StatementList if_body;
|
auto if_body = std::make_unique<ast::BlockStatement>();
|
||||||
if_body.push_back(std::make_unique<ast::ReturnStatement>());
|
if_body->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
ast::IfStatement expr(std::move(cond), std::move(if_body));
|
ast::IfStatement expr(std::move(cond), std::move(if_body));
|
||||||
|
|
||||||
|
@ -645,8 +644,8 @@ TEST_F(BuilderTest, If_WithReturnValue) {
|
||||||
auto cond2 = std::make_unique<ast::ScalarConstructorExpression>(
|
auto cond2 = std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
std::make_unique<ast::BoolLiteral>(&bool_type, false));
|
std::make_unique<ast::BoolLiteral>(&bool_type, false));
|
||||||
|
|
||||||
ast::StatementList if_body;
|
auto if_body = std::make_unique<ast::BlockStatement>();
|
||||||
if_body.push_back(std::make_unique<ast::ReturnStatement>(std::move(cond2)));
|
if_body->append(std::make_unique<ast::ReturnStatement>(std::move(cond2)));
|
||||||
|
|
||||||
ast::IfStatement expr(std::move(cond), std::move(if_body));
|
ast::IfStatement expr(std::move(cond), std::move(if_body));
|
||||||
|
|
||||||
|
|
|
@ -489,8 +489,8 @@ TEST_F(BuilderTest, Switch_WithNestedBreak) {
|
||||||
auto a =
|
auto a =
|
||||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kPrivate, &i32);
|
std::make_unique<ast::Variable>("a", ast::StorageClass::kPrivate, &i32);
|
||||||
|
|
||||||
ast::StatementList if_body;
|
auto if_body = std::make_unique<ast::BlockStatement>();
|
||||||
if_body.push_back(std::make_unique<ast::BreakStatement>());
|
if_body->append(std::make_unique<ast::BreakStatement>());
|
||||||
|
|
||||||
auto case_1_body = std::make_unique<ast::BlockStatement>();
|
auto case_1_body = std::make_unique<ast::BlockStatement>();
|
||||||
case_1_body->append(std::make_unique<ast::IfStatement>(
|
case_1_body->append(std::make_unique<ast::IfStatement>(
|
||||||
|
|
|
@ -771,12 +771,12 @@ bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
|
||||||
if (!EmitExpression(stmt->condition())) {
|
if (!EmitExpression(stmt->condition())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
out_ << ")";
|
out_ << ") ";
|
||||||
} else {
|
} else {
|
||||||
out_ << " else";
|
out_ << " else ";
|
||||||
}
|
}
|
||||||
|
|
||||||
return EmitStatementBlock(stmt->body());
|
return EmitBlock(stmt->body());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) {
|
bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) {
|
||||||
|
@ -792,9 +792,9 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
|
||||||
if (!EmitExpression(stmt->condition())) {
|
if (!EmitExpression(stmt->condition())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
out_ << ")";
|
out_ << ") ";
|
||||||
|
|
||||||
if (!EmitStatementBlock(stmt->body())) {
|
if (!EmitBlock(stmt->body())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,8 +28,8 @@ using WgslGeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
TEST_F(WgslGeneratorImplTest, Emit_If) {
|
TEST_F(WgslGeneratorImplTest, Emit_If) {
|
||||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||||
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::IfStatement i(std::move(cond), std::move(body));
|
ast::IfStatement i(std::move(cond), std::move(body));
|
||||||
|
|
||||||
|
@ -45,17 +45,16 @@ TEST_F(WgslGeneratorImplTest, Emit_If) {
|
||||||
|
|
||||||
TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) {
|
TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) {
|
||||||
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
||||||
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
ast::StatementList else_body;
|
else_body->append(std::make_unique<ast::DiscardStatement>());
|
||||||
else_body.push_back(std::make_unique<ast::DiscardStatement>());
|
|
||||||
|
|
||||||
ast::ElseStatementList elses;
|
ast::ElseStatementList elses;
|
||||||
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
|
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
|
||||||
std::move(else_body)));
|
std::move(else_body)));
|
||||||
|
|
||||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||||
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::IfStatement i(std::move(cond), std::move(body));
|
ast::IfStatement i(std::move(cond), std::move(body));
|
||||||
i.set_else_statements(std::move(elses));
|
i.set_else_statements(std::move(elses));
|
||||||
|
@ -73,15 +72,15 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) {
|
TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) {
|
||||||
ast::StatementList else_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
else_body.push_back(std::make_unique<ast::DiscardStatement>());
|
else_body->append(std::make_unique<ast::DiscardStatement>());
|
||||||
|
|
||||||
ast::ElseStatementList elses;
|
ast::ElseStatementList elses;
|
||||||
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body)));
|
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body)));
|
||||||
|
|
||||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||||
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::IfStatement i(std::move(cond), std::move(body));
|
ast::IfStatement i(std::move(cond), std::move(body));
|
||||||
i.set_else_statements(std::move(elses));
|
i.set_else_statements(std::move(elses));
|
||||||
|
@ -101,11 +100,11 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) {
|
||||||
TEST_F(WgslGeneratorImplTest, Emit_IfWithMultiple) {
|
TEST_F(WgslGeneratorImplTest, Emit_IfWithMultiple) {
|
||||||
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
||||||
|
|
||||||
ast::StatementList else_body;
|
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||||
else_body.push_back(std::make_unique<ast::DiscardStatement>());
|
else_body->append(std::make_unique<ast::DiscardStatement>());
|
||||||
|
|
||||||
ast::StatementList else_body_2;
|
auto else_body_2 = std::make_unique<ast::BlockStatement>();
|
||||||
else_body_2.push_back(std::make_unique<ast::DiscardStatement>());
|
else_body_2->append(std::make_unique<ast::DiscardStatement>());
|
||||||
|
|
||||||
ast::ElseStatementList elses;
|
ast::ElseStatementList elses;
|
||||||
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
|
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
|
||||||
|
@ -113,8 +112,8 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithMultiple) {
|
||||||
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body_2)));
|
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body_2)));
|
||||||
|
|
||||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||||
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::IfStatement i(std::move(cond), std::move(body));
|
ast::IfStatement i(std::move(cond), std::move(body));
|
||||||
i.set_else_statements(std::move(elses));
|
i.set_else_statements(std::move(elses));
|
||||||
|
|
Loading…
Reference in New Issue