ast: Remove no-arg constructor for ast::IfStatement

In a near-future change, AST nodes, such as ast::BlockStatement will no longer
be std::unique_ptrs, and will have to be constructed and owned by an external
class. This means AST nodes can no longer allocate default child nodes.

Bug: tint:322
Change-Id: I36a1cf55c31a1dabccde272b2be415f98c16b18f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32677
Commit-Queue: Ben Clayton <bclayton@google.com>
Commit-Queue: David Neto <dneto@google.com>
Reviewed-by: David Neto <dneto@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-16 15:15:37 +00:00 committed by Commit Bot service account
parent aa57015db4
commit eeac0c5f63
8 changed files with 39 additions and 37 deletions

View File

@ -91,7 +91,7 @@ TEST_F(BlockStatementTest, IsValid_NullBodyStatement) {
TEST_F(BlockStatementTest, IsValid_InvalidBodyStatement) {
BlockStatement b;
b.append(create<IfStatement>());
b.append(create<IfStatement>(nullptr, create<BlockStatement>()));
EXPECT_FALSE(b.IsValid());
}

View File

@ -129,7 +129,7 @@ TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) {
b.push_back(create<SintLiteral>(&i32, 2));
auto body = create<BlockStatement>();
body->append(create<IfStatement>());
body->append(create<IfStatement>(nullptr, create<ast::BlockStatement>()));
CaseStatement c({std::move(b)}, std::move(body));
EXPECT_FALSE(c.IsValid());

View File

@ -98,7 +98,7 @@ TEST_F(ElseStatementTest, IsValid_InvalidCondition) {
TEST_F(ElseStatementTest, IsValid_InvalidBodyStatement) {
auto body = create<BlockStatement>();
body->append(create<IfStatement>());
body->append(create<IfStatement>(nullptr, create<ast::BlockStatement>()));
ElseStatement e(std::move(body));
EXPECT_FALSE(e.IsValid());

View File

@ -19,9 +19,6 @@
namespace tint {
namespace ast {
IfStatement::IfStatement()
: Statement(), body_(std::make_unique<BlockStatement>()) {}
IfStatement::IfStatement(std::unique_ptr<Expression> condition,
std::unique_ptr<BlockStatement> body)
: Statement(), condition_(std::move(condition)), body_(std::move(body)) {}

View File

@ -29,8 +29,6 @@ namespace ast {
/// An if statement
class IfStatement : public Statement {
public:
/// Constructor
IfStatement();
/// Constructor
/// @param condition the if condition
/// @param body the if body

View File

@ -26,7 +26,7 @@ using IfStatementTest = TestHelper;
TEST_F(IfStatementTest, Creation) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
auto* cond_ptr = cond.get();
@ -40,7 +40,7 @@ TEST_F(IfStatementTest, Creation) {
TEST_F(IfStatementTest, Creation_WithSource) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
IfStatement stmt(Source{Source::Location{20, 2}}, std::move(cond),
@ -51,13 +51,13 @@ TEST_F(IfStatementTest, Creation_WithSource) {
}
TEST_F(IfStatementTest, IsIf) {
IfStatement stmt;
IfStatement stmt(nullptr, create<BlockStatement>());
EXPECT_TRUE(stmt.IsIf());
}
TEST_F(IfStatementTest, IsValid) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
IfStatement stmt(std::move(cond), std::move(body));
@ -66,7 +66,7 @@ TEST_F(IfStatementTest, IsValid) {
TEST_F(IfStatementTest, IsValid_WithElseStatements) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
ElseStatementList else_stmts;
@ -80,7 +80,7 @@ TEST_F(IfStatementTest, IsValid_WithElseStatements) {
}
TEST_F(IfStatementTest, IsValid_MissingCondition) {
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
IfStatement stmt(nullptr, std::move(body));
@ -89,7 +89,7 @@ TEST_F(IfStatementTest, IsValid_MissingCondition) {
TEST_F(IfStatementTest, IsValid_InvalidCondition) {
auto cond = create<IdentifierExpression>("");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
IfStatement stmt(std::move(cond), std::move(body));
@ -98,7 +98,7 @@ TEST_F(IfStatementTest, IsValid_InvalidCondition) {
TEST_F(IfStatementTest, IsValid_NullBodyStatement) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
body->append(nullptr);
@ -108,9 +108,9 @@ TEST_F(IfStatementTest, IsValid_NullBodyStatement) {
TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
body->append(create<IfStatement>());
body->append(create<IfStatement>(nullptr, create<BlockStatement>()));
IfStatement stmt(std::move(cond), std::move(body));
EXPECT_FALSE(stmt.IsValid());
@ -118,7 +118,7 @@ TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) {
TEST_F(IfStatementTest, IsValid_NullElseStatement) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
ElseStatementList else_stmts;
@ -134,7 +134,7 @@ TEST_F(IfStatementTest, IsValid_NullElseStatement) {
TEST_F(IfStatementTest, IsValid_InvalidElseStatement) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
ElseStatementList else_stmts;
@ -148,7 +148,7 @@ TEST_F(IfStatementTest, IsValid_InvalidElseStatement) {
TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
ElseStatementList else_stmts;
@ -162,7 +162,7 @@ TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) {
TEST_F(IfStatementTest, IsValid_ElseNotLast) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
ElseStatementList else_stmts;
@ -177,7 +177,7 @@ TEST_F(IfStatementTest, IsValid_ElseNotLast) {
TEST_F(IfStatementTest, ToStr) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
IfStatement stmt(std::move(cond), std::move(body));
@ -197,7 +197,7 @@ TEST_F(IfStatementTest, ToStr) {
TEST_F(IfStatementTest, ToStr_WithElseStatements) {
auto cond = create<IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
auto else_if_body = create<BlockStatement>();

View File

@ -120,7 +120,7 @@ TEST_F(LoopStatementTest, IsValid_NullBodyStatement) {
TEST_F(LoopStatementTest, IsValid_InvalidBodyStatement) {
auto body = create<BlockStatement>();
body->append(create<DiscardStatement>());
body->append(create<IfStatement>());
body->append(create<IfStatement>(nullptr, create<BlockStatement>()));
auto continuing = create<BlockStatement>();
continuing->append(create<DiscardStatement>());
@ -147,7 +147,7 @@ TEST_F(LoopStatementTest, IsValid_InvalidContinuingStatement) {
auto continuing = create<BlockStatement>();
continuing->append(create<DiscardStatement>());
continuing->append(create<IfStatement>());
continuing->append(create<IfStatement>(nullptr, create<BlockStatement>()));
LoopStatement l(std::move(body), std::move(continuing));
EXPECT_FALSE(l.IsValid());

View File

@ -515,10 +515,11 @@ void FunctionEmitter::PushGuard(const std::string& guard_name,
// if-selection with a then-clause ending at the same block
// as the statement block at the top of the stack.
const auto& top = statements_stack_.back();
auto* const guard_stmt =
AddStatement(std::make_unique<ast::IfStatement>())->AsIf();
guard_stmt->set_condition(
std::make_unique<ast::IdentifierExpression>(guard_name));
auto cond = std::make_unique<ast::IdentifierExpression>(guard_name);
auto body = std::make_unique<ast::BlockStatement>();
auto* const guard_stmt = AddStatement(std::make_unique<ast::IfStatement>(
std::move(cond), std::move(body)))
->AsIf();
PushNewStatementBlock(top.construct_, end_id,
[guard_stmt](StatementBlock* s) {
guard_stmt->set_body(std::move(s->statements_));
@ -528,8 +529,11 @@ void FunctionEmitter::PushGuard(const std::string& guard_name,
void FunctionEmitter::PushTrueGuard(uint32_t end_id) {
assert(!statements_stack_.empty());
const auto& top = statements_stack_.back();
auto* const guard_stmt =
AddStatement(std::make_unique<ast::IfStatement>())->AsIf();
auto cond = MakeTrue();
auto body = std::make_unique<ast::BlockStatement>();
auto* const guard_stmt = AddStatement(std::make_unique<ast::IfStatement>(
std::move(cond), std::move(body)))
->AsIf();
guard_stmt->set_condition(MakeTrue());
PushNewStatementBlock(top.construct_, end_id,
[guard_stmt](StatementBlock* s) {
@ -1977,12 +1981,15 @@ bool FunctionEmitter::EmitIfStart(const BlockInfo& block_info) {
AddStatement(std::move(guard_decl));
}
auto* const if_stmt =
AddStatement(std::make_unique<ast::IfStatement>())->AsIf();
const auto condition_id =
block_info.basic_block->terminator()->GetSingleWordInOperand(0);
auto cond = MakeExpression(condition_id).expr;
auto body = std::make_unique<ast::BlockStatement>();
auto* const if_stmt = AddStatement(std::make_unique<ast::IfStatement>(
std::move(cond), std::move(body)))
->AsIf();
// Generate the code for the condition.
if_stmt->set_condition(std::move(MakeExpression(condition_id).expr));
// Compute the block IDs that should end the then-clause and the else-clause.
@ -2413,8 +2420,8 @@ std::unique_ptr<ast::Statement> FunctionEmitter::MakeSimpleIf(
if ((then_stmt == nullptr) && (else_stmt == nullptr)) {
return nullptr;
}
auto if_stmt = std::make_unique<ast::IfStatement>();
if_stmt->set_condition(std::move(condition));
auto if_stmt = std::make_unique<ast::IfStatement>(
std::move(condition), std::make_unique<ast::BlockStatement>());
if (then_stmt != nullptr) {
auto stmts = std::make_unique<ast::BlockStatement>();
stmts->append(std::move(then_stmt));