From 983876859984e875af971367d27465388d085ee6 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 7 Dec 2020 20:11:24 +0000 Subject: [PATCH] ast: Remove IfStatement::set_else_statements() Move it to the constructor. Bug: tint:390 Change-Id: Ib4ac1a1c83aa59963472ac7c14c9e0cbcf2734e6 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35007 Commit-Queue: Ben Clayton Reviewed-by: dan sinclair --- src/ast/block_statement_test.cc | 3 +- src/ast/case_statement_test.cc | 3 +- src/ast/else_statement_test.cc | 3 +- src/ast/if_statement.cc | 15 +-- src/ast/if_statement.h | 13 +- src/ast/if_statement_test.cc | 112 ++++++++---------- src/ast/loop_statement_test.cc | 6 +- src/reader/spirv/function.cc | 70 +++++++---- src/reader/wgsl/parser_impl.cc | 14 +-- src/type_determiner_test.cc | 9 +- src/validator/validator_test.cc | 12 +- src/writer/hlsl/generator_impl_binary_test.cc | 12 +- .../hlsl/generator_impl_function_test.cc | 3 +- src/writer/hlsl/generator_impl_if_test.cc | 27 ++--- .../msl/generator_impl_function_test.cc | 3 +- src/writer/msl/generator_impl_if_test.cc | 27 ++--- src/writer/spirv/builder_if_test.cc | 66 +++++------ src/writer/spirv/builder_switch_test.cc | 5 +- src/writer/wgsl/generator_impl_if_test.cc | 27 ++--- 19 files changed, 210 insertions(+), 220 deletions(-) diff --git a/src/ast/block_statement_test.cc b/src/ast/block_statement_test.cc index f0941b7e5b..fc2cb92364 100644 --- a/src/ast/block_statement_test.cc +++ b/src/ast/block_statement_test.cc @@ -88,7 +88,8 @@ TEST_F(BlockStatementTest, IsValid_NullBodyStatement) { TEST_F(BlockStatementTest, IsValid_InvalidBodyStatement) { BlockStatement b; - b.append(create(nullptr, create())); + b.append(create(Source{}, nullptr, create(), + ElseStatementList{})); EXPECT_FALSE(b.IsValid()); } diff --git a/src/ast/case_statement_test.cc b/src/ast/case_statement_test.cc index ba62191549..37617a8083 100644 --- a/src/ast/case_statement_test.cc +++ b/src/ast/case_statement_test.cc @@ -124,7 +124,8 @@ TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) { b.push_back(create(&i32, 2)); auto* body = create(); - body->append(create(nullptr, create())); + body->append(create(Source{}, nullptr, create(), + ElseStatementList{})); CaseStatement c({b}, body); EXPECT_FALSE(c.IsValid()); diff --git a/src/ast/else_statement_test.cc b/src/ast/else_statement_test.cc index 166d805a1e..98dfd71446 100644 --- a/src/ast/else_statement_test.cc +++ b/src/ast/else_statement_test.cc @@ -97,7 +97,8 @@ TEST_F(ElseStatementTest, IsValid_InvalidCondition) { TEST_F(ElseStatementTest, IsValid_InvalidBodyStatement) { auto* body = create(); - body->append(create(nullptr, create())); + body->append(create(Source{}, nullptr, create(), + ElseStatementList{})); ElseStatement e(body); EXPECT_FALSE(e.IsValid()); diff --git a/src/ast/if_statement.cc b/src/ast/if_statement.cc index 451c123eb2..aac56a89c2 100644 --- a/src/ast/if_statement.cc +++ b/src/ast/if_statement.cc @@ -23,13 +23,14 @@ TINT_INSTANTIATE_CLASS_ID(tint::ast::IfStatement); namespace tint { namespace ast { -IfStatement::IfStatement(Expression* condition, BlockStatement* body) - : Base(), condition_(condition), body_(body) {} - IfStatement::IfStatement(const Source& source, Expression* condition, - BlockStatement* body) - : Base(source), condition_(condition), body_(body) {} + BlockStatement* body, + ElseStatementList else_stmts) + : Base(source), + condition_(condition), + body_(body), + else_statements_(std::move(else_stmts)) {} IfStatement::IfStatement(IfStatement&&) = default; @@ -37,8 +38,8 @@ IfStatement::~IfStatement() = default; IfStatement* IfStatement::Clone(CloneContext* ctx) const { auto* cloned = ctx->mod->create( - ctx->Clone(source()), ctx->Clone(condition_), ctx->Clone(body_)); - cloned->else_statements_ = ctx->Clone(else_statements_); + ctx->Clone(source()), ctx->Clone(condition_), ctx->Clone(body_), + ctx->Clone(else_statements_)); return cloned; } diff --git a/src/ast/if_statement.h b/src/ast/if_statement.h index 32a4d31150..f489c4a930 100644 --- a/src/ast/if_statement.h +++ b/src/ast/if_statement.h @@ -29,17 +29,15 @@ namespace ast { /// An if statement class IfStatement : public Castable { public: - /// Constructor - /// @param condition the if condition - /// @param body the if body - IfStatement(Expression* condition, BlockStatement* body); /// Constructor /// @param source the source information /// @param condition the if condition /// @param body the if body + /// @param else_stmts the else statements IfStatement(const Source& source, Expression* condition, - BlockStatement* body); + BlockStatement* body, + ElseStatementList else_stmts); /// Move constructor IfStatement(IfStatement&&); ~IfStatement() override; @@ -51,11 +49,6 @@ class IfStatement : public Castable { /// @returns the if body BlockStatement* body() { return body_; } - /// Sets the else statements - /// @param else_statements the else statements to set - void set_else_statements(ElseStatementList else_statements) { - else_statements_ = std::move(else_statements); - } /// @returns the else statements const ElseStatementList& else_statements() const { return else_statements_; } /// @returns the else statements diff --git a/src/ast/if_statement_test.cc b/src/ast/if_statement_test.cc index a87930a5dd..e3117cded1 100644 --- a/src/ast/if_statement_test.cc +++ b/src/ast/if_statement_test.cc @@ -25,30 +25,20 @@ namespace { using IfStatementTest = TestHelper; TEST_F(IfStatementTest, Creation) { - auto* cond = create("cond"); - auto* body = create(); - auto* discard = create(); - body->append(discard); - - IfStatement stmt(cond, body); - EXPECT_EQ(stmt.condition(), cond); - ASSERT_EQ(stmt.body()->size(), 1u); - EXPECT_EQ(stmt.body()->get(0), discard); -} - -TEST_F(IfStatementTest, Creation_WithSource) { auto* cond = create("cond"); auto* body = create(); body->append(create()); - IfStatement stmt(Source{Source::Location{20, 2}}, cond, body); + IfStatement stmt(Source{Source::Location{20, 2}}, cond, body, + ElseStatementList{}); auto src = stmt.source(); EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.column, 2u); } TEST_F(IfStatementTest, IsIf) { - IfStatement stmt(nullptr, create()); + IfStatement stmt(Source{}, nullptr, create(), + ElseStatementList{}); EXPECT_TRUE(stmt.Is()); } @@ -57,7 +47,7 @@ TEST_F(IfStatementTest, IsValid) { auto* body = create(); body->append(create()); - IfStatement stmt(cond, body); + IfStatement stmt(Source{}, cond, body, ElseStatementList{}); EXPECT_TRUE(stmt.IsValid()); } @@ -66,13 +56,13 @@ TEST_F(IfStatementTest, IsValid_WithElseStatements) { auto* body = create(); body->append(create()); - ElseStatementList else_stmts; - else_stmts.push_back(create( - create("Ident"), create())); - else_stmts.push_back(create(create())); - - IfStatement stmt(cond, body); - stmt.set_else_statements(else_stmts); + IfStatement stmt( + Source{}, cond, body, + { + create(create("Ident"), + create()), + create(create()), + }); EXPECT_TRUE(stmt.IsValid()); } @@ -80,7 +70,7 @@ TEST_F(IfStatementTest, IsValid_MissingCondition) { auto* body = create(); body->append(create()); - IfStatement stmt(nullptr, body); + IfStatement stmt(Source{}, nullptr, body, ElseStatementList{}); EXPECT_FALSE(stmt.IsValid()); } @@ -89,7 +79,7 @@ TEST_F(IfStatementTest, IsValid_InvalidCondition) { auto* body = create(); body->append(create()); - IfStatement stmt(cond, body); + IfStatement stmt(Source{}, cond, body, ElseStatementList{}); EXPECT_FALSE(stmt.IsValid()); } @@ -99,7 +89,7 @@ TEST_F(IfStatementTest, IsValid_NullBodyStatement) { body->append(create()); body->append(nullptr); - IfStatement stmt(cond, body); + IfStatement stmt(Source{}, cond, body, ElseStatementList{}); EXPECT_FALSE(stmt.IsValid()); } @@ -107,9 +97,10 @@ TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) { auto* cond = create("cond"); auto* body = create(); body->append(create()); - body->append(create(nullptr, create())); + body->append(create(Source{}, nullptr, create(), + ast::ElseStatementList{})); - IfStatement stmt(cond, body); + IfStatement stmt(Source{}, cond, body, ElseStatementList{}); EXPECT_FALSE(stmt.IsValid()); } @@ -118,14 +109,14 @@ TEST_F(IfStatementTest, IsValid_NullElseStatement) { auto* body = create(); body->append(create()); - ElseStatementList else_stmts; - else_stmts.push_back(create( - create("Ident"), create())); - else_stmts.push_back(create(create())); - else_stmts.push_back(nullptr); - - IfStatement stmt(cond, body); - stmt.set_else_statements(else_stmts); + IfStatement stmt( + Source{}, cond, body, + { + create(create("Ident"), + create()), + create(create()), + nullptr, + }); EXPECT_FALSE(stmt.IsValid()); } @@ -134,12 +125,11 @@ TEST_F(IfStatementTest, IsValid_InvalidElseStatement) { auto* body = create(); body->append(create()); - ElseStatementList else_stmts; - else_stmts.push_back(create(create(""), - create())); - - IfStatement stmt(cond, body); - stmt.set_else_statements(else_stmts); + IfStatement stmt(Source{}, cond, body, + { + create(create(""), + create()), + }); EXPECT_FALSE(stmt.IsValid()); } @@ -148,12 +138,11 @@ TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) { auto* body = create(); body->append(create()); - ElseStatementList else_stmts; - else_stmts.push_back(create(create())); - else_stmts.push_back(create(create())); - - IfStatement stmt(cond, body); - stmt.set_else_statements(else_stmts); + IfStatement stmt(Source{}, cond, body, + { + create(create()), + create(create()), + }); EXPECT_FALSE(stmt.IsValid()); } @@ -162,13 +151,13 @@ TEST_F(IfStatementTest, IsValid_ElseNotLast) { auto* body = create(); body->append(create()); - ElseStatementList else_stmts; - else_stmts.push_back(create(create())); - else_stmts.push_back(create( - create("ident"), create())); - - IfStatement stmt(cond, body); - stmt.set_else_statements(else_stmts); + IfStatement stmt( + Source{}, cond, body, + { + create(create()), + create(create("ident"), + create()), + }); EXPECT_FALSE(stmt.IsValid()); } @@ -177,7 +166,7 @@ TEST_F(IfStatementTest, ToStr) { auto* body = create(); body->append(create()); - IfStatement stmt(cond, body); + IfStatement stmt(Source{}, cond, body, ElseStatementList{}); std::ostringstream out; stmt.to_str(out, 2); @@ -204,13 +193,12 @@ TEST_F(IfStatementTest, ToStr_WithElseStatements) { else_body->append(create()); else_body->append(create()); - ElseStatementList else_stmts; - else_stmts.push_back(create( - create("ident"), else_if_body)); - else_stmts.push_back(create(else_body)); - - IfStatement stmt(cond, body); - stmt.set_else_statements(else_stmts); + IfStatement stmt(Source{}, cond, body, + { + create( + create("ident"), else_if_body), + create(else_body), + }); std::ostringstream out; stmt.to_str(out, 2); diff --git a/src/ast/loop_statement_test.cc b/src/ast/loop_statement_test.cc index 12001decb9..63d4e13509 100644 --- a/src/ast/loop_statement_test.cc +++ b/src/ast/loop_statement_test.cc @@ -118,7 +118,8 @@ TEST_F(LoopStatementTest, IsValid_NullBodyStatement) { TEST_F(LoopStatementTest, IsValid_InvalidBodyStatement) { auto* body = create(); body->append(create()); - body->append(create(nullptr, create())); + body->append(create(Source{}, nullptr, create(), + ElseStatementList{})); auto* continuing = create(); continuing->append(create()); @@ -145,7 +146,8 @@ TEST_F(LoopStatementTest, IsValid_InvalidContinuingStatement) { auto* continuing = create(); continuing->append(create()); - continuing->append(create(nullptr, create())); + continuing->append(create( + Source{}, nullptr, create(), ElseStatementList{})); LoopStatement l(body, continuing); EXPECT_FALSE(l.IsValid()); diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc index 5d25d78f20..26e65808b0 100644 --- a/src/reader/spirv/function.cc +++ b/src/reader/spirv/function.cc @@ -572,7 +572,8 @@ void FunctionEmitter::PushGuard(const std::string& guard_name, auto* cond = create(guard_name); auto* body = create(); - AddStatement(create(cond, body)); + AddStatement( + create(Source{}, cond, body, ast::ElseStatementList{})); PushNewStatementBlock(top.construct_, end_id, body, nullptr, nullptr); } @@ -582,7 +583,8 @@ void FunctionEmitter::PushTrueGuard(uint32_t end_id) { auto* cond = MakeTrue(); auto* body = create(); - AddStatement(create(cond, body)); + AddStatement( + create(Source{}, cond, body, ast::ElseStatementList{})); PushNewStatementBlock(top.construct_, end_id, body, nullptr, nullptr); } @@ -2028,10 +2030,41 @@ bool FunctionEmitter::EmitIfStart(const BlockInfo& block_info) { block_info.basic_block->terminator()->GetSingleWordInOperand(0); auto* cond = MakeExpression(condition_id).expr; auto* body = create(); - auto* const if_stmt = AddStatement(create(cond, body)) - ->As(); // Generate the code for the condition. + // Use the IfBuilder to create the if-statement. The IfBuilder is constructed + // as a std::shared_ptr and is captured by the then and else clause + // CompletionAction lambdas, and so will only be destructed when the last + // block is completed. The IfBuilder destructor constructs the IfStatement, + // inserting it at the current insertion point in the current + // ast::BlockStatement. + struct IfBuilder { + IfBuilder(ast::Module* mod, + StatementBlock& statement_block, + tint::ast::Expression* cond, + ast::BlockStatement* body) + : mod_(mod), + dst_block_(statement_block.statements_), + dst_block_insertion_point_(statement_block.statements_->size()), + cond_(cond), + body_(body) {} + + ~IfBuilder() { + dst_block_->insert( + dst_block_insertion_point_, + mod_->create(Source{}, cond_, body_, else_stmts_)); + } + + ast::Module* mod_; + ast::BlockStatement* dst_block_; + size_t dst_block_insertion_point_; + tint::ast::Expression* cond_; + ast::BlockStatement* body_; + ast::ElseStatementList else_stmts_; + }; + + auto if_builder = std::make_shared( + &ast_module_, statements_stack_.back(), cond, body); // Compute the block IDs that should end the then-clause and the else-clause. @@ -2069,20 +2102,18 @@ bool FunctionEmitter::EmitIfStart(const BlockInfo& block_info) { // Push statement blocks for the then-clause and the else-clause. // But make sure we do it in the right order. - - auto push_else = [this, if_stmt, else_end, construct]() { + auto push_else = [this, if_builder, else_end, construct]() { // Push the else clause onto the stack first. auto* else_body = create(); PushNewStatementBlock( - construct, else_end, else_body, nullptr, [this, if_stmt, else_body]() { + construct, else_end, else_body, nullptr, + [this, if_builder, else_body]() { // Only set the else-clause if there are statements to fill it. if (!else_body->empty()) { // The "else" consists of the statement list from the top of - // statments stack, without an elseif condition. - ast::ElseStatementList else_stmts; - else_stmts.emplace_back( + // statements stack, without an elseif condition. + if_builder->else_stmts_.emplace_back( create(nullptr, else_body)); - if_stmt->set_else_statements(else_stmts); } }); }; @@ -2121,7 +2152,7 @@ bool FunctionEmitter::EmitIfStart(const BlockInfo& block_info) { } // Push the then clause onto the stack. - PushNewStatementBlock(construct, then_end, body, nullptr, nullptr); + PushNewStatementBlock(construct, then_end, body, nullptr, [if_builder] {}); } return success(); @@ -2439,18 +2470,17 @@ ast::Statement* FunctionEmitter::MakeSimpleIf(ast::Expression* condition, if ((then_stmt == nullptr) && (else_stmt == nullptr)) { return nullptr; } - auto* if_block = create(); - auto* if_stmt = create(condition, if_block); - if (then_stmt != nullptr) { - if_block->append(then_stmt); - } + ast::ElseStatementList else_stmts; if (else_stmt != nullptr) { auto* stmts = create(); stmts->append(else_stmt); - - ast::ElseStatementList else_stmts; else_stmts.emplace_back(create(nullptr, stmts)); - if_stmt->set_else_statements(else_stmts); + } + auto* if_block = create(); + auto* if_stmt = + create(Source{}, condition, if_block, else_stmts); + if (then_stmt != nullptr) { + if_block->append(then_stmt); } return if_stmt; } diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 0e7e9d752c..ec4a74827d 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -1647,14 +1647,11 @@ Maybe ParserImpl::if_stmt() { auto el = else_stmt(); if (el.errored) return Failure::kErrored; - - auto* stmt = create(source, condition.value, body.value); - if (el.matched) { + if (el.matched) elseif.value.push_back(el.value); - } - stmt->set_else_statements(elseif.value); - return stmt; + return create(source, condition.value, body.value, + elseif.value); } // elseif_stmt @@ -1945,8 +1942,9 @@ Maybe ParserImpl::for_stmt() { auto* break_body = create(not_condition->source()); break_body->append(break_stmt); // if (!condition) { break; } - auto* break_if_not_condition = create( - not_condition->source(), not_condition, break_body); + auto* break_if_not_condition = + create(not_condition->source(), not_condition, + break_body, ast::ElseStatementList{}); body->insert(0, break_if_not_condition); } diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc index af76cee1ef..fc1df28fa0 100644 --- a/src/type_determiner_test.cc +++ b/src/type_determiner_test.cc @@ -225,9 +225,6 @@ TEST_F(TypeDeterminerTest, Stmt_If) { create(&i32, 3)), else_body); - ast::ElseStatementList else_stmts; - else_stmts.push_back(else_stmt); - auto* lhs = create( create(&i32, 2)); auto* rhs = create( @@ -236,10 +233,10 @@ TEST_F(TypeDeterminerTest, Stmt_If) { auto* body = create(); body->append(create(lhs, rhs)); - ast::IfStatement stmt(create( + ast::IfStatement stmt(Source{}, + create( create(&i32, 3)), - body); - stmt.set_else_statements(else_stmts); + body, ast::ElseStatementList{else_stmt}); EXPECT_TRUE(td()->DetermineResultType(&stmt)); ASSERT_NE(stmt.condition()->result_type(), nullptr); diff --git a/src/validator/validator_test.cc b/src/validator/validator_test.cc index e15a5e0dfc..1978a18161 100644 --- a/src/validator/validator_test.cc +++ b/src/validator/validator_test.cc @@ -353,7 +353,8 @@ TEST_F(ValidatorTest, UsingUndefinedVariableInnerScope_Fail) { create(&f32, 3.14f)); auto* outer_body = create(); - outer_body->append(create(cond, body)); + outer_body->append( + create(Source{}, cond, body, ast::ElseStatementList{})); outer_body->append(create( Source{Source::Location{12, 34}}, lhs, rhs)); @@ -388,7 +389,8 @@ TEST_F(ValidatorTest, UsingUndefinedVariableOuterScope_Pass) { auto* outer_body = create(); outer_body->append(create(var)); - outer_body->append(create(cond, body)); + outer_body->append( + create(Source{}, cond, body, ast::ElseStatementList{})); EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error(); ASSERT_NE(lhs->result_type(), nullptr); ASSERT_NE(rhs->result_type(), nullptr); @@ -554,7 +556,8 @@ TEST_F(ValidatorTest, RedeclaredIdentifierInnerScope_Pass) { create(&f32, 3.14))); auto* outer_body = create(); - outer_body->append(create(cond, body)); + outer_body->append( + create(Source{}, cond, body, ast::ElseStatementList{})); outer_body->append(create( Source{Source::Location{12, 34}}, var_a_float)); @@ -588,7 +591,8 @@ TEST_F(ValidatorTest, DISABLED_RedeclaredIdentifierInnerScope_False) { auto* outer_body = create(); outer_body->append(create(var_a_float)); - outer_body->append(create(cond, body)); + outer_body->append( + create(Source{}, cond, body, ast::ElseStatementList{})); EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error(); EXPECT_FALSE(v()->ValidateStatements(outer_body)); diff --git a/src/writer/hlsl/generator_impl_binary_test.cc b/src/writer/hlsl/generator_impl_binary_test.cc index 18c46f3b5e..52e5fff36c 100644 --- a/src/writer/hlsl/generator_impl_binary_test.cc +++ b/src/writer/hlsl/generator_impl_binary_test.cc @@ -388,21 +388,21 @@ TEST_F(HlslGeneratorImplTest_Binary, If_WithLogical) { create("c")), body); - ast::ElseStatementList else_stmts; - else_stmts.push_back(else_if_stmt); - else_stmts.push_back(else_stmt); - body = create(); body->append( create(create( create(&i32, 1)))); ast::IfStatement expr( + Source{}, create(ast::BinaryOp::kLogicalAnd, create("a"), create("b")), - body); - expr.set_else_statements(else_stmts); + body, + { + else_if_stmt, + else_stmt, + }); ASSERT_TRUE(gen.EmitStatement(out, &expr)) << gen.error(); EXPECT_EQ(result(), R"(bool _tint_tmp = a; diff --git a/src/writer/hlsl/generator_impl_function_test.cc b/src/writer/hlsl/generator_impl_function_test.cc index 476f720896..b3bb0e7979 100644 --- a/src/writer/hlsl/generator_impl_function_test.cc +++ b/src/writer/hlsl/generator_impl_function_test.cc @@ -889,12 +889,13 @@ TEST_F(HlslGeneratorImplTest_Function, list->append(create()); body->append(create( + Source{}, create(ast::BinaryOp::kEqual, create( create(&i32, 1)), create( create(&i32, 1))), - list)); + list, ast::ElseStatementList{})); body->append(create()); auto* func_1 = create("ep_1", params, &void_type, body); diff --git a/src/writer/hlsl/generator_impl_if_test.cc b/src/writer/hlsl/generator_impl_if_test.cc index 6e9bd7a362..0ce0fed6fc 100644 --- a/src/writer/hlsl/generator_impl_if_test.cc +++ b/src/writer/hlsl/generator_impl_if_test.cc @@ -31,7 +31,7 @@ TEST_F(HlslGeneratorImplTest_If, Emit_If) { auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); + ast::IfStatement i(Source{}, cond, body, ast::ElseStatementList{}); gen.increment_indent(); ASSERT_TRUE(gen.EmitStatement(out, &i)) << gen.error(); @@ -46,15 +46,12 @@ TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElseIf) { auto* else_body = create(); else_body->append(create()); - ast::ElseStatementList elses; - elses.push_back(create(else_cond, else_body)); - auto* cond = create("cond"); auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); - i.set_else_statements(elses); + ast::IfStatement i(Source{}, cond, body, + {create(else_cond, else_body)}); gen.increment_indent(); @@ -73,15 +70,12 @@ TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElse) { auto* else_body = create(); else_body->append(create()); - ast::ElseStatementList elses; - elses.push_back(create(else_body)); - auto* cond = create("cond"); auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); - i.set_else_statements(elses); + ast::IfStatement i(Source{}, cond, body, + {create(else_body)}); gen.increment_indent(); @@ -103,16 +97,15 @@ TEST_F(HlslGeneratorImplTest_If, Emit_IfWithMultiple) { auto* else_body_2 = create(); else_body_2->append(create()); - ast::ElseStatementList elses; - elses.push_back(create(else_cond, else_body)); - elses.push_back(create(else_body_2)); - auto* cond = create("cond"); auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); - i.set_else_statements(elses); + ast::IfStatement i(Source{}, cond, body, + { + create(else_cond, else_body), + create(else_body_2), + }); gen.increment_indent(); diff --git a/src/writer/msl/generator_impl_function_test.cc b/src/writer/msl/generator_impl_function_test.cc index 53378d457b..088455a6c8 100644 --- a/src/writer/msl/generator_impl_function_test.cc +++ b/src/writer/msl/generator_impl_function_test.cc @@ -911,12 +911,13 @@ TEST_F(MslGeneratorImplTest, list->append(create()); body->append(create( + Source{}, create(ast::BinaryOp::kEqual, create( create(&i32, 1)), create( create(&i32, 1))), - list)); + list, ast::ElseStatementList{})); body->append(create()); diff --git a/src/writer/msl/generator_impl_if_test.cc b/src/writer/msl/generator_impl_if_test.cc index 71428a83c2..dd34be7ad9 100644 --- a/src/writer/msl/generator_impl_if_test.cc +++ b/src/writer/msl/generator_impl_if_test.cc @@ -33,7 +33,7 @@ TEST_F(MslGeneratorImplTest, Emit_If) { auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); + ast::IfStatement i(Source{}, cond, body, ast::ElseStatementList{}); gen.increment_indent(); @@ -49,15 +49,12 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) { auto* else_body = create(); else_body->append(create()); - ast::ElseStatementList elses; - elses.push_back(create(else_cond, else_body)); - auto* cond = create("cond"); auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); - i.set_else_statements(elses); + ast::IfStatement i(Source{}, cond, body, + {create(else_cond, else_body)}); gen.increment_indent(); @@ -74,15 +71,12 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithElse) { auto* else_body = create(); else_body->append(create()); - ast::ElseStatementList elses; - elses.push_back(create(else_body)); - auto* cond = create("cond"); auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); - i.set_else_statements(elses); + ast::IfStatement i(Source{}, cond, body, + {create(else_body)}); gen.increment_indent(); @@ -104,16 +98,15 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) { auto* else_body_2 = create(); else_body_2->append(create()); - ast::ElseStatementList elses; - elses.push_back(create(else_cond, else_body)); - elses.push_back(create(else_body_2)); - auto* cond = create("cond"); auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); - i.set_else_statements(elses); + ast::IfStatement i(Source{}, cond, body, + { + create(else_cond, else_body), + create(else_body_2), + }); gen.increment_indent(); diff --git a/src/writer/spirv/builder_if_test.cc b/src/writer/spirv/builder_if_test.cc index 71115c36eb..768604abd4 100644 --- a/src/writer/spirv/builder_if_test.cc +++ b/src/writer/spirv/builder_if_test.cc @@ -48,7 +48,8 @@ TEST_F(BuilderTest, If_Empty) { auto* cond = create( create(&bool_type, true)); - ast::IfStatement expr(cond, create()); + ast::IfStatement expr(Source{}, cond, create(), + ast::ElseStatementList{}); ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); @@ -85,7 +86,7 @@ TEST_F(BuilderTest, If_WithStatements) { auto* cond = create( create(&bool_type, true)); - ast::IfStatement expr(cond, body); + ast::IfStatement expr(Source{}, cond, body, ast::ElseStatementList{}); td.RegisterVariableForTesting(var); ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); @@ -135,14 +136,11 @@ TEST_F(BuilderTest, If_WithElse) { create( create(&i32, 3)))); - ast::ElseStatementList else_stmts; - else_stmts.push_back(create(else_body)); - auto* cond = create( create(&bool_type, true)); - ast::IfStatement expr(cond, body); - expr.set_else_statements(else_stmts); + ast::IfStatement expr(Source{}, cond, body, + {create(else_body)}); td.RegisterVariableForTesting(var); @@ -200,14 +198,11 @@ TEST_F(BuilderTest, If_WithElseIf) { auto* else_cond = create( create(&bool_type, true)); - ast::ElseStatementList else_stmts; - else_stmts.push_back(create(else_cond, else_body)); - auto* cond = create( create(&bool_type, true)); - ast::IfStatement expr(cond, body); - expr.set_else_statements(else_stmts); + ast::IfStatement expr(Source{}, cond, body, + {create(else_cond, else_body)}); td.RegisterVariableForTesting(var); @@ -285,18 +280,16 @@ TEST_F(BuilderTest, If_WithMultiple) { auto* elseif_2_cond = create( create(&bool_type, false)); - ast::ElseStatementList else_stmts; - else_stmts.push_back( - create(elseif_1_cond, elseif_1_body)); - else_stmts.push_back( - create(elseif_2_cond, elseif_2_body)); - else_stmts.push_back(create(else_body)); - auto* cond = create( create(&bool_type, true)); - ast::IfStatement expr(cond, body); - expr.set_else_statements(else_stmts); + ast::IfStatement expr( + Source{}, cond, body, + { + create(elseif_1_cond, elseif_1_body), + create(elseif_2_cond, elseif_2_body), + create(else_body), + }); td.RegisterVariableForTesting(var); @@ -360,7 +353,8 @@ TEST_F(BuilderTest, If_WithBreak) { auto* if_body = create(); if_body->append(create()); - auto* if_stmt = create(cond, if_body); + auto* if_stmt = create(Source{}, cond, if_body, + ast::ElseStatementList{}); auto* loop_body = create(); loop_body->append(if_stmt); @@ -407,11 +401,9 @@ TEST_F(BuilderTest, If_WithElseBreak) { auto* else_body = create(); else_body->append(create()); - ast::ElseStatementList else_stmts; - else_stmts.push_back(create(else_body)); - - auto* if_stmt = create(cond, create()); - if_stmt->set_else_statements(else_stmts); + auto* if_stmt = create( + Source{}, cond, create(), + ast::ElseStatementList{create(else_body)}); auto* loop_body = create(); loop_body->append(if_stmt); @@ -459,7 +451,8 @@ TEST_F(BuilderTest, If_WithContinue) { auto* if_body = create(); if_body->append(create()); - auto* if_stmt = create(cond, if_body); + auto* if_stmt = create(Source{}, cond, if_body, + ast::ElseStatementList{}); auto* loop_body = create(); loop_body->append(if_stmt); @@ -506,11 +499,9 @@ TEST_F(BuilderTest, If_WithElseContinue) { auto* else_body = create(); else_body->append(create()); - ast::ElseStatementList else_stmts; - else_stmts.push_back(create(else_body)); - - auto* if_stmt = create(cond, create()); - if_stmt->set_else_statements(else_stmts); + auto* if_stmt = create( + Source{}, cond, create(), + ast::ElseStatementList{create(else_body)}); auto* loop_body = create(); loop_body->append(if_stmt); @@ -556,7 +547,7 @@ TEST_F(BuilderTest, If_WithReturn) { auto* if_body = create(); if_body->append(create()); - ast::IfStatement expr(cond, if_body); + ast::IfStatement expr(Source{}, cond, if_body, ast::ElseStatementList{}); ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); @@ -588,7 +579,7 @@ TEST_F(BuilderTest, If_WithReturnValue) { auto* if_body = create(); if_body->append(create(cond2)); - ast::IfStatement expr(cond, if_body); + ast::IfStatement expr(Source{}, cond, if_body, ast::ElseStatementList{}); ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); @@ -618,8 +609,9 @@ TEST_F(BuilderTest, If_WithLoad_Bug327) { create("a", ast::StorageClass::kFunction, &bool_type); td.RegisterVariableForTesting(var); - ast::IfStatement expr(create("a"), - create()); + ast::IfStatement expr(Source{}, create("a"), + create(), + ast::ElseStatementList{}); ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); diff --git a/src/writer/spirv/builder_switch_test.cc b/src/writer/spirv/builder_switch_test.cc index add53b844e..aeb872485c 100644 --- a/src/writer/spirv/builder_switch_test.cc +++ b/src/writer/spirv/builder_switch_test.cc @@ -447,9 +447,10 @@ TEST_F(BuilderTest, Switch_WithNestedBreak) { auto* case_1_body = create(); case_1_body->append( - create(create( + create(Source{}, + create( create(&bool_type, true)), - if_body)); + if_body, ast::ElseStatementList{})); case_1_body->append( create(create("v"), diff --git a/src/writer/wgsl/generator_impl_if_test.cc b/src/writer/wgsl/generator_impl_if_test.cc index b0c68b826f..b2e22059d8 100644 --- a/src/writer/wgsl/generator_impl_if_test.cc +++ b/src/writer/wgsl/generator_impl_if_test.cc @@ -32,7 +32,7 @@ TEST_F(WgslGeneratorImplTest, Emit_If) { auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); + ast::IfStatement i(Source{}, cond, body, ast::ElseStatementList{}); gen.increment_indent(); @@ -48,15 +48,12 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) { auto* else_body = create(); else_body->append(create()); - ast::ElseStatementList elses; - elses.push_back(create(else_cond, else_body)); - auto* cond = create("cond"); auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); - i.set_else_statements(elses); + ast::IfStatement i(Source{}, cond, body, + {create(else_cond, else_body)}); gen.increment_indent(); @@ -73,15 +70,12 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) { auto* else_body = create(); else_body->append(create()); - ast::ElseStatementList elses; - elses.push_back(create(else_body)); - auto* cond = create("cond"); auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); - i.set_else_statements(elses); + ast::IfStatement i(Source{}, cond, body, + {create(else_body)}); gen.increment_indent(); @@ -103,16 +97,15 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithMultiple) { auto* else_body_2 = create(); else_body_2->append(create()); - ast::ElseStatementList elses; - elses.push_back(create(else_cond, else_body)); - elses.push_back(create(else_body_2)); - auto* cond = create("cond"); auto* body = create(); body->append(create()); - ast::IfStatement i(cond, body); - i.set_else_statements(elses); + ast::IfStatement i(Source{}, cond, body, + { + create(else_cond, else_body), + create(else_body_2), + }); gen.increment_indent();