From 8aff0ed684cd140a6dd010b95ceac1decb734f74 Mon Sep 17 00:00:00 2001 From: James Price Date: Mon, 2 May 2022 14:53:36 +0000 Subject: [PATCH] tint: Add ProgramBuilder::Else() helper This aids readability when building chains of if-else statements. Change-Id: I77ed5a16421bd52302db61f2776d55971838e122 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/88366 Reviewed-by: Ben Clayton Kokoro: Kokoro --- src/tint/ast/if_statement_test.cc | 4 +- src/tint/program_builder.h | 25 ++++++-- src/tint/resolver/compound_statement_test.cc | 2 +- src/tint/resolver/dependency_graph_test.cc | 10 ++-- src/tint/resolver/resolver_behavior_test.cc | 6 +- src/tint/resolver/resolver_test.cc | 2 +- src/tint/resolver/validation_test.cc | 58 +++++++++---------- .../transform/multiplanar_external_texture.cc | 4 +- .../transform/utils/hoist_to_decl_before.cc | 2 +- .../utils/hoist_to_decl_before_test.cc | 16 ++--- .../writer/glsl/generator_impl_binary_test.cc | 4 +- .../glsl/generator_impl_continue_test.cc | 3 +- .../writer/glsl/generator_impl_if_test.cc | 6 +- .../writer/hlsl/generator_impl_binary_test.cc | 4 +- .../writer/hlsl/generator_impl_if_test.cc | 6 +- src/tint/writer/msl/generator_impl_if_test.cc | 7 ++- src/tint/writer/spirv/builder_if_test.cc | 24 ++++---- src/tint/writer/spirv/builder_loop_test.cc | 8 +-- .../writer/wgsl/generator_impl_if_test.cc | 6 +- 19 files changed, 107 insertions(+), 90 deletions(-) diff --git a/src/tint/ast/if_statement_test.cc b/src/tint/ast/if_statement_test.cc index 57d0b86ed6..9115cb76e8 100644 --- a/src/tint/ast/if_statement_test.cc +++ b/src/tint/ast/if_statement_test.cc @@ -58,7 +58,7 @@ TEST_F(IfStatementTest, Assert_InvalidElse) { EXPECT_FATAL_FAILURE( { ProgramBuilder b; - b.If(b.Expr(true), b.Block(), b.CallStmt(b.Call("foo"))); + b.If(b.Expr(true), b.Block(), b.Else(b.CallStmt(b.Call("foo")))); }, "internal compiler error"); } @@ -88,7 +88,7 @@ TEST_F(IfStatementTest, Assert_DifferentProgramID_ElseStatement) { { ProgramBuilder b1; ProgramBuilder b2; - b1.If(b1.Expr(true), b1.Block(), b2.If(b2.Expr("ident"), b2.Block())); + b1.If(b1.Expr(true), b1.Block(), b2.Else(b2.If(b2.Expr("ident"), b2.Block()))); }, "internal compiler error"); } diff --git a/src/tint/program_builder.h b/src/tint/program_builder.h index 0b26ede324..5c3feee42b 100644 --- a/src/tint/program_builder.h +++ b/src/tint/program_builder.h @@ -2080,6 +2080,17 @@ class ProgramBuilder { ast::StatementList{std::forward(statements)...}); } + /// A wrapper type for the Else statement used to create If statements. + struct ElseStmt { + /// Default constructor - no else statement. + ElseStmt() : stmt(nullptr) {} + /// Constructor + /// @param s The else statement + explicit ElseStmt(const ast::Statement* s) : stmt(s) {} + /// The else statement, or nullptr. + const ast::Statement* stmt; + }; + /// Creates a ast::IfStatement with input condition, body, and optional /// else statement /// @param source the source information for the if statement @@ -2091,9 +2102,9 @@ class ProgramBuilder { const ast::IfStatement* If(const Source& source, CONDITION&& condition, const ast::BlockStatement* body, - const ast::Statement* else_stmt = nullptr) { + const ElseStmt else_stmt = ElseStmt()) { return create(source, Expr(std::forward(condition)), body, - else_stmt); + else_stmt.stmt); } /// Creates a ast::IfStatement with input condition, body, and optional @@ -2105,10 +2116,16 @@ class ProgramBuilder { template const ast::IfStatement* If(CONDITION&& condition, const ast::BlockStatement* body, - const ast::Statement* else_stmt = nullptr) { - return create(Expr(std::forward(condition)), body, else_stmt); + const ElseStmt else_stmt = ElseStmt()) { + return create(Expr(std::forward(condition)), body, + else_stmt.stmt); } + /// Creates an Else object. + /// @param stmt else statement + /// @returns the Else object + ElseStmt Else(const ast::Statement* stmt) { return ElseStmt(stmt); } + /// Creates a ast::AssignmentStatement with input lhs and rhs expressions /// @param source the source information /// @param lhs the left hand side expression initializer diff --git a/src/tint/resolver/compound_statement_test.cc b/src/tint/resolver/compound_statement_test.cc index e3aec7405d..1358b383b1 100644 --- a/src/tint/resolver/compound_statement_test.cc +++ b/src/tint/resolver/compound_statement_test.cc @@ -224,7 +224,7 @@ TEST_F(ResolverCompoundStatementTest, If) { auto* cond_b = Expr(true); auto* stmt_b = Ignore(1); auto* stmt_c = Ignore(1); - auto* if_stmt = If(cond_a, Block(stmt_a), If(cond_b, Block(stmt_b), Block(stmt_c))); + auto* if_stmt = If(cond_a, Block(stmt_a), Else(If(cond_b, Block(stmt_b), Else(Block(stmt_c))))); WrapInFunction(if_stmt); ASSERT_TRUE(r()->Resolve()) << r()->error(); diff --git a/src/tint/resolver/dependency_graph_test.cc b/src/tint/resolver/dependency_graph_test.cc index a02d08c91e..3a11a8dd12 100644 --- a/src/tint/resolver/dependency_graph_test.cc +++ b/src/tint/resolver/dependency_graph_test.cc @@ -1147,9 +1147,9 @@ TEST_P(ResolverDependencyShadowTest, Test) { helper.Add(inner_kind, symbol, Source{{56, 78}}); auto* inner_var = helper.nested_statements.size() ? helper.nested_statements[0]->As()->variable - : helper.statements.size() - ? helper.statements[0]->As()->variable - : helper.parameters[0]; + : helper.statements.size() + ? helper.statements[0]->As()->variable + : helper.parameters[0]; helper.Build(); EXPECT_EQ(Build().shadows[inner_var], outer); @@ -1218,8 +1218,8 @@ TEST_F(ResolverDependencyGraphTraversalTest, SymbolsReached) { Assign(V, V)), // If(V, // Block(Assign(V, V)), // - If(V, // - Block(Assign(V, V)))), // + Else(If(V, // + Block(Assign(V, V))))), // Ignore(Bitcast(T, V)), // For(Decl(Var(Sym(), T, V)), // Equal(V, V), // diff --git a/src/tint/resolver/resolver_behavior_test.cc b/src/tint/resolver/resolver_behavior_test.cc index 4bcd4ffda1..03729c581f 100644 --- a/src/tint/resolver/resolver_behavior_test.cc +++ b/src/tint/resolver/resolver_behavior_test.cc @@ -332,7 +332,7 @@ TEST_F(ResolverBehaviorTest, StmtIfTrue_ThenDiscard) { } TEST_F(ResolverBehaviorTest, StmtIfTrue_ThenEmptyBlock_ElseDiscard) { - auto* stmt = If(true, Block(), Block(Discard())); + auto* stmt = If(true, Block(), Else(Block(Discard()))); WrapInFunction(stmt); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -342,7 +342,7 @@ TEST_F(ResolverBehaviorTest, StmtIfTrue_ThenEmptyBlock_ElseDiscard) { } TEST_F(ResolverBehaviorTest, StmtIfTrue_ThenDiscard_ElseDiscard) { - auto* stmt = If(true, Block(Discard()), Block(Discard())); + auto* stmt = If(true, Block(Discard()), Else(Block(Discard()))); WrapInFunction(stmt); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -363,7 +363,7 @@ TEST_F(ResolverBehaviorTest, StmtIfCallFuncMayDiscard_ThenEmptyBlock) { TEST_F(ResolverBehaviorTest, StmtIfTrue_ThenEmptyBlock_ElseCallFuncMayDiscard) { auto* stmt = If(true, Block(), // - If(Equal(Call("DiscardOrNext"), 1), Block())); + Else(If(Equal(Call("DiscardOrNext"), 1), Block()))); WrapInFunction(stmt); ASSERT_TRUE(r()->Resolve()) << r()->error(); diff --git a/src/tint/resolver/resolver_test.cc b/src/tint/resolver/resolver_test.cc index 439d92930e..7c06fab785 100644 --- a/src/tint/resolver/resolver_test.cc +++ b/src/tint/resolver/resolver_test.cc @@ -168,7 +168,7 @@ TEST_F(ResolverTest, Stmt_If) { auto* assign = Assign(lhs, rhs); auto* body = Block(assign); auto* cond = Expr(true); - auto* stmt = If(cond, body, else_stmt); + auto* stmt = If(cond, body, Else(else_stmt)); WrapInFunction(v, stmt); EXPECT_TRUE(r()->Resolve()) << r()->error(); diff --git a/src/tint/resolver/validation_test.cc b/src/tint/resolver/validation_test.cc index 94349d8d01..0e7cffff1f 100644 --- a/src/tint/resolver/validation_test.cc +++ b/src/tint/resolver/validation_test.cc @@ -124,7 +124,7 @@ TEST_F(ResolverValidationTest, Stmt_If_NonBool) { TEST_F(ResolverValidationTest, Stmt_ElseIf_NonBool) { // else if (1.23f) {} - WrapInFunction(If(Expr(true), Block(), If(Expr(Source{{12, 34}}, 1.23f), Block()))); + WrapInFunction(If(Expr(true), Block(), Else(If(Expr(Source{{12, 34}}, 1.23f), Block())))); EXPECT_FALSE(r()->Resolve()); @@ -1007,12 +1007,12 @@ TEST_F(ResolverValidationTest, Stmt_BreakInIfTrueInContinuing) { } TEST_F(ResolverValidationTest, Stmt_BreakInIfElseInContinuing) { - auto* cont = Block( // continuing { - If(true, Block(), // if(true) { - Block( // } else { - Break(Source{{12, 34}})))); // break; - // } - // } + auto* cont = Block( // continuing { + If(true, Block(), // if(true) { + Else(Block( // } else { + Break(Source{{12, 34}}))))); // break; + // } + // } WrapInFunction(Loop(Block(), cont)); EXPECT_TRUE(r()->Resolve()) << r()->error(); } @@ -1065,13 +1065,13 @@ TEST_F(ResolverValidationTest, Stmt_BreakInIfTrueMultipleStmtsInContinuing) { } TEST_F(ResolverValidationTest, Stmt_BreakInIfElseMultipleStmtsInContinuing) { - auto* cont = Block( // continuing { - If(true, Block(), // if(true) { - Block(Source{{56, 78}}, // } else { - Assign(Phony(), 1), // _ = 1; - Break(Source{{12, 34}})))); // break; - // } - // } + auto* cont = Block( // continuing { + If(true, Block(), // if(true) { + Else(Block(Source{{56, 78}}, // } else { + Assign(Phony(), 1), // _ = 1; + Break(Source{{12, 34}}))))); // break; + // } + // } WrapInFunction(Loop(Block(), cont)); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -1082,12 +1082,12 @@ TEST_F(ResolverValidationTest, Stmt_BreakInIfElseMultipleStmtsInContinuing) { } TEST_F(ResolverValidationTest, Stmt_BreakInIfElseIfInContinuing) { - auto* cont = Block( // continuing { - If(true, Block(), // if(true) { - If(Source{{56, 78}}, Expr(true), // } else if (true) { - Block(Break(Source{{12, 34}}))))); // break; - // } - // } + auto* cont = Block( // continuing { + If(true, Block(), // if(true) { + Else(If(Source{{56, 78}}, Expr(true), // } else if (true) { + Block(Break(Source{{12, 34}})))))); // break; + // } + // } WrapInFunction(Loop(Block(), cont)); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -1098,13 +1098,13 @@ TEST_F(ResolverValidationTest, Stmt_BreakInIfElseIfInContinuing) { } TEST_F(ResolverValidationTest, Stmt_BreakInIfNonEmptyElseInContinuing) { - auto* cont = Block( // continuing { - If(true, // if(true) { - Block(Break(Source{{12, 34}})), // break; - Block(Source{{56, 78}}, // } else { - Assign(Phony(), 1)))); // _ = 1; - // } - // } + auto* cont = Block( // continuing { + If(true, // if(true) { + Block(Break(Source{{12, 34}})), // break; + Else(Block(Source{{56, 78}}, // } else { + Assign(Phony(), 1))))); // _ = 1; + // } + // } WrapInFunction(Loop(Block(), cont)); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -1118,8 +1118,8 @@ TEST_F(ResolverValidationTest, Stmt_BreakInIfElseNonEmptyTrueInContinuing) { auto* cont = Block( // continuing { If(true, // if(true) { Block(Source{{56, 78}}, Assign(Phony(), 1)), // _ = 1; - Block( // } else { - Break(Source{{12, 34}})))); // break; + Else(Block( // } else { + Break(Source{{12, 34}}))))); // break; // } // } WrapInFunction(Loop(Block(), cont)); diff --git a/src/tint/transform/multiplanar_external_texture.cc b/src/tint/transform/multiplanar_external_texture.cc index 93af4caa09..671c6c1e61 100644 --- a/src/tint/transform/multiplanar_external_texture.cc +++ b/src/tint/transform/multiplanar_external_texture.cc @@ -329,13 +329,13 @@ struct MultiplanarExternalTexture::State { b.Block( // color = textureLoad(plane0, coord, 0).rgb; b.Assign("color", b.MemberAccessor(single_plane_call, "rgb"))), - b.Block( + b.Else(b.Block( // color = vec4(plane_0_call.r, plane_1_call.rg, 1.0) * // params.yuvToRgbConversionMatrix; b.Assign("color", b.Mul(b.vec4(b.MemberAccessor(plane_0_call, "r"), b.MemberAccessor(plane_1_call, "rg"), 1.0f), - b.MemberAccessor("params", "yuvToRgbConversionMatrix"))))), + b.MemberAccessor("params", "yuvToRgbConversionMatrix")))))), // color = gammaConversion(color, gammaDecodeParams); b.Assign("color", b.Call("gammaCorrection", "color", b.MemberAccessor("params", "gammaDecodeParams"))), diff --git a/src/tint/transform/utils/hoist_to_decl_before.cc b/src/tint/transform/utils/hoist_to_decl_before.cc index 9917412460..450a2e8a92 100644 --- a/src/tint/transform/utils/hoist_to_decl_before.cc +++ b/src/tint/transform/utils/hoist_to_decl_before.cc @@ -123,7 +123,7 @@ class HoistToDeclBefore::State { // Move the 'else-if' into the new `else` block as a plain 'if'. auto* cond = ctx.Clone(else_if->condition); auto* body = ctx.Clone(else_if->body); - auto* new_if = b.If(cond, body, ctx.Clone(else_if->else_statement)); + auto* new_if = b.If(cond, body, b.Else(ctx.Clone(else_if->else_statement))); body_stmts.emplace_back(new_if); // Replace the 'else-if' with the new 'else' block. diff --git a/src/tint/transform/utils/hoist_to_decl_before_test.cc b/src/tint/transform/utils/hoist_to_decl_before_test.cc index bce27df101..e675fac1aa 100644 --- a/src/tint/transform/utils/hoist_to_decl_before_test.cc +++ b/src/tint/transform/utils/hoist_to_decl_before_test.cc @@ -184,9 +184,9 @@ TEST_F(HoistToDeclBeforeTest, ElseIf) { ProgramBuilder b; auto* var = b.Decl(b.Var("a", b.ty.bool_())); auto* expr = b.Expr("a"); - auto* s = b.If(b.Expr(true), b.Block(), // - b.If(expr, b.Block(), // - b.Block())); + auto* s = b.If(b.Expr(true), b.Block(), // + b.Else(b.If(expr, b.Block(), // + b.Else(b.Block())))); b.Func("f", {}, b.ty.void_(), {var, s}); Program original(std::move(b)); @@ -378,9 +378,9 @@ TEST_F(HoistToDeclBeforeTest, Prepare_ElseIf) { ProgramBuilder b; auto* var = b.Decl(b.Var("a", b.ty.bool_())); auto* expr = b.Expr("a"); - auto* s = b.If(b.Expr(true), b.Block(), // - b.If(expr, b.Block(), // - b.Block())); + auto* s = b.If(b.Expr(true), b.Block(), // + b.Else(b.If(expr, b.Block(), // + b.Else(b.Block())))); b.Func("f", {}, b.ty.void_(), {var, s}); Program original(std::move(b)); @@ -552,9 +552,9 @@ TEST_F(HoistToDeclBeforeTest, InsertBefore_ElseIf) { ProgramBuilder b; b.Func("foo", {}, b.ty.void_(), {}); auto* var = b.Decl(b.Var("a", b.ty.bool_())); - auto* elseif = b.If(b.Expr("a"), b.Block(), b.Block()); + auto* elseif = b.If(b.Expr("a"), b.Block(), b.Else(b.Block())); auto* s = b.If(b.Expr(true), b.Block(), // - elseif); + b.Else(elseif)); b.Func("f", {}, b.ty.void_(), {var, s}); Program original(std::move(b)); diff --git a/src/tint/writer/glsl/generator_impl_binary_test.cc b/src/tint/writer/glsl/generator_impl_binary_test.cc index 46dd8427c5..a38918409b 100644 --- a/src/tint/writer/glsl/generator_impl_binary_test.cc +++ b/src/tint/writer/glsl/generator_impl_binary_test.cc @@ -343,8 +343,8 @@ TEST_F(GlslGeneratorImplTest_Binary, If_WithLogical) { auto* expr = If(create(ast::BinaryOp::kLogicalAnd, Expr("a"), Expr("b")), Block(Return(1)), - If(create(ast::BinaryOp::kLogicalOr, Expr("b"), Expr("c")), - Block(Return(2)), Block(Return(3)))); + Else(If(create(ast::BinaryOp::kLogicalOr, Expr("b"), Expr("c")), + Block(Return(2)), Else(Block(Return(3)))))); Func("func", {}, ty.i32(), {WrapInStatement(expr)}); GeneratorImpl& gen = Build(); diff --git a/src/tint/writer/glsl/generator_impl_continue_test.cc b/src/tint/writer/glsl/generator_impl_continue_test.cc index 4f77281547..331ce795de 100644 --- a/src/tint/writer/glsl/generator_impl_continue_test.cc +++ b/src/tint/writer/glsl/generator_impl_continue_test.cc @@ -20,8 +20,7 @@ namespace { using GlslGeneratorImplTest_Continue = TestHelper; TEST_F(GlslGeneratorImplTest_Continue, Emit_Continue) { - auto* loop = Loop(Block(If(false, Block(Break())), // - Continue())); + auto* loop = Loop(Block(If(false, Block(Break())), Continue())); WrapInFunction(loop); GeneratorImpl& gen = Build(); diff --git a/src/tint/writer/glsl/generator_impl_if_test.cc b/src/tint/writer/glsl/generator_impl_if_test.cc index a5fcbc5dae..4b0b7bbe7a 100644 --- a/src/tint/writer/glsl/generator_impl_if_test.cc +++ b/src/tint/writer/glsl/generator_impl_if_test.cc @@ -46,7 +46,7 @@ TEST_F(GlslGeneratorImplTest_If, Emit_IfWithElseIf) { auto* cond = Expr("cond"); auto* body = Block(Return()); - auto* i = If(cond, body, If(else_cond, else_body)); + auto* i = If(cond, body, Else(If(else_cond, else_body))); WrapInFunction(i); GeneratorImpl& gen = Build(); @@ -71,7 +71,7 @@ TEST_F(GlslGeneratorImplTest_If, Emit_IfWithElse) { auto* cond = Expr("cond"); auto* body = Block(Return()); - auto* i = If(cond, body, else_body); + auto* i = If(cond, body, Else(else_body)); WrapInFunction(i); GeneratorImpl& gen = Build(); @@ -99,7 +99,7 @@ TEST_F(GlslGeneratorImplTest_If, Emit_IfWithMultiple) { auto* cond = Expr("cond"); auto* body = Block(Return()); - auto* i = If(cond, body, If(else_cond, else_body, else_body_2)); + auto* i = If(cond, body, Else(If(else_cond, else_body, Else(else_body_2)))); WrapInFunction(i); GeneratorImpl& gen = Build(); diff --git a/src/tint/writer/hlsl/generator_impl_binary_test.cc b/src/tint/writer/hlsl/generator_impl_binary_test.cc index 230c72abf0..6299e84347 100644 --- a/src/tint/writer/hlsl/generator_impl_binary_test.cc +++ b/src/tint/writer/hlsl/generator_impl_binary_test.cc @@ -333,8 +333,8 @@ TEST_F(HlslGeneratorImplTest_Binary, If_WithLogical) { auto* expr = If(create(ast::BinaryOp::kLogicalAnd, Expr("a"), Expr("b")), Block(Return(1)), - If(create(ast::BinaryOp::kLogicalOr, Expr("b"), Expr("c")), - Block(Return(2)), Block(Return(3)))); + Else(If(create(ast::BinaryOp::kLogicalOr, Expr("b"), Expr("c")), + Block(Return(2)), Else(Block(Return(3)))))); Func("func", {}, ty.i32(), {WrapInStatement(expr)}); GeneratorImpl& gen = Build(); diff --git a/src/tint/writer/hlsl/generator_impl_if_test.cc b/src/tint/writer/hlsl/generator_impl_if_test.cc index c3b2721e58..1668d71cb8 100644 --- a/src/tint/writer/hlsl/generator_impl_if_test.cc +++ b/src/tint/writer/hlsl/generator_impl_if_test.cc @@ -46,7 +46,7 @@ TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElseIf) { auto* cond = Expr("cond"); auto* body = Block(Return()); - auto* i = If(cond, body, If(else_cond, else_body)); + auto* i = If(cond, body, Else(If(else_cond, else_body))); WrapInFunction(i); GeneratorImpl& gen = Build(); @@ -71,7 +71,7 @@ TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElse) { auto* cond = Expr("cond"); auto* body = Block(Return()); - auto* i = If(cond, body, else_body); + auto* i = If(cond, body, Else(else_body)); WrapInFunction(i); GeneratorImpl& gen = Build(); @@ -99,7 +99,7 @@ TEST_F(HlslGeneratorImplTest_If, Emit_IfWithMultiple) { auto* cond = Expr("cond"); auto* body = Block(Return()); - auto* i = If(cond, body, If(else_cond, else_body, else_body_2)); + auto* i = If(cond, body, Else(If(else_cond, else_body, Else(else_body_2)))); WrapInFunction(i); GeneratorImpl& gen = Build(); diff --git a/src/tint/writer/msl/generator_impl_if_test.cc b/src/tint/writer/msl/generator_impl_if_test.cc index 1af0bed4ab..5138dce172 100644 --- a/src/tint/writer/msl/generator_impl_if_test.cc +++ b/src/tint/writer/msl/generator_impl_if_test.cc @@ -38,7 +38,7 @@ TEST_F(MslGeneratorImplTest, Emit_If) { TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) { auto* cond = Var("cond", ty.bool_()); auto* else_cond = Var("else_cond", ty.bool_()); - auto* i = If(cond, Block(Return()), If(else_cond, Block(Return()))); + auto* i = If(cond, Block(Return()), Else(If(else_cond, Block(Return())))); WrapInFunction(cond, else_cond, i); GeneratorImpl& gen = Build(); @@ -58,7 +58,7 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) { TEST_F(MslGeneratorImplTest, Emit_IfWithElse) { auto* cond = Var("cond", ty.bool_()); - auto* i = If(cond, Block(Return()), Block(Return())); + auto* i = If(cond, Block(Return()), Else(Block(Return()))); WrapInFunction(cond, i); GeneratorImpl& gen = Build(); @@ -77,7 +77,8 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithElse) { TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) { auto* cond = Var("cond", ty.bool_()); auto* else_cond = Var("else_cond", ty.bool_()); - auto* i = If(cond, Block(Return()), If(else_cond, Block(Return()), Block(Return()))); + auto* i = + If(cond, Block(Return()), Else(If(else_cond, Block(Return()), Else(Block(Return()))))); WrapInFunction(cond, else_cond, i); GeneratorImpl& gen = Build(); diff --git a/src/tint/writer/spirv/builder_if_test.cc b/src/tint/writer/spirv/builder_if_test.cc index 26b58ae53c..17a2f50802 100644 --- a/src/tint/writer/spirv/builder_if_test.cc +++ b/src/tint/writer/spirv/builder_if_test.cc @@ -106,7 +106,7 @@ TEST_F(BuilderTest, If_WithElse) { auto* body = Block(Assign("v", 2)); auto* else_body = Block(Assign("v", 3)); - auto* expr = If(true, body, else_body); + auto* expr = If(true, body, Else(else_body)); WrapInFunction(expr); spirv::Builder& b = Build(); @@ -148,7 +148,7 @@ TEST_F(BuilderTest, If_WithElseIf) { auto* body = Block(Assign("v", 2)); auto* else_body = Block(Assign("v", 3)); - auto* expr = If(true, body, If(true, else_body)); + auto* expr = If(true, body, Else(If(true, else_body))); WrapInFunction(expr); spirv::Builder& b = Build(); @@ -201,10 +201,10 @@ TEST_F(BuilderTest, If_WithMultiple) { auto* elseif_2_body = Block(Assign("v", 4)); auto* else_body = Block(Assign("v", 5)); - auto* expr = If(true, body, // - If(true, elseif_1_body, // - If(false, elseif_2_body, // - else_body))); + auto* expr = If(true, body, // + Else(If(true, elseif_1_body, // + Else(If(false, elseif_2_body, // + Else(else_body)))))); WrapInFunction(expr); spirv::Builder& b = Build(); @@ -305,7 +305,7 @@ TEST_F(BuilderTest, If_WithElseBreak) { // } auto* else_body = Block(Break()); - auto* if_stmt = If(true, Block(), else_body); + auto* if_stmt = If(true, Block(), Else(else_body)); auto* loop_body = Block(if_stmt); @@ -349,7 +349,7 @@ TEST_F(BuilderTest, If_WithContinueAndBreak) { // } // } - auto* if_stmt = If(true, Block(Continue()), Block(Break())); + auto* if_stmt = If(true, Block(Continue()), Else(Block(Break()))); auto* expr = Loop(Block(if_stmt), Block()); WrapInFunction(expr); @@ -392,7 +392,7 @@ TEST_F(BuilderTest, If_WithElseContinue) { // } auto* else_body = Block(create()); - auto* if_stmt = If(true, Block(), else_body); + auto* if_stmt = If(true, Block(), Else(else_body)); auto* loop_body = Block(if_stmt, Break()); @@ -493,7 +493,7 @@ TEST_F(BuilderTest, IfElse_BothReturn) { { If(true, // Block(Return(true)), // - Block(Return(true))), + Else(Block(Return(true)))), }); spirv::Builder& b = Build(); @@ -589,7 +589,7 @@ TEST_F(BuilderTest, If_ElseIf_WithReturn) { // return; // } - auto* if_stmt = If(false, Block(), If(true, Block(Return()))); + auto* if_stmt = If(false, Block(), Else(If(true, Block(Return())))); auto* fn = Func("f", {}, ty.void_(), {if_stmt}); spirv::Builder& b = Build(); @@ -627,7 +627,7 @@ TEST_F(BuilderTest, Loop_If_ElseIf_WithBreak) { // } // } - auto* if_stmt = If(false, Block(), If(true, Block(Break()))); + auto* if_stmt = If(false, Block(), Else(If(true, Block(Break())))); auto* fn = Func("f", {}, ty.void_(), {Loop(Block(if_stmt))}); spirv::Builder& b = Build(); diff --git a/src/tint/writer/spirv/builder_loop_test.cc b/src/tint/writer/spirv/builder_loop_test.cc index ebce6783f0..5532f6ad86 100644 --- a/src/tint/writer/spirv/builder_loop_test.cc +++ b/src/tint/writer/spirv/builder_loop_test.cc @@ -268,7 +268,7 @@ TEST_F(BuilderTest, Loop_WithContinuing_BreakUnless) { // if (true) {} else { break; } // } // } - auto* if_stmt = If(Expr(true), Block(), Block(Break())); + auto* if_stmt = If(Expr(true), Block(), Else(Block(Break()))); auto* continuing = Block(if_stmt); auto* loop = Loop(Block(), continuing); WrapInFunction(loop); @@ -341,7 +341,7 @@ TEST_F(BuilderTest, Loop_WithContinuing_BreakUnless_ConditionIsVar) { // } // } auto* cond_var = Decl(Var("cond", nullptr, Expr(true))); - auto* if_stmt = If(Expr("cond"), Block(), Block(Break())); + auto* if_stmt = If(Expr("cond"), Block(), Else(Block(Break()))); auto* continuing = Block(cond_var, if_stmt); auto* loop = Loop(Block(), continuing); WrapInFunction(loop); @@ -437,11 +437,11 @@ TEST_F(BuilderTest, Loop_WithContinuing_BreakUnless_Nested) { // } // } - auto* inner_if_stmt = If(Expr(true), Block(), Block(Break())); + auto* inner_if_stmt = If(Expr(true), Block(), Else(Block(Break()))); auto* inner_continuing = Block(inner_if_stmt); auto* inner_loop = Loop(Block(), inner_continuing); - auto* outer_if_stmt = If(Expr(true), Block(), Block(Break())); + auto* outer_if_stmt = If(Expr(true), Block(), Else(Block(Break()))); auto* outer_continuing = Block(inner_loop, outer_if_stmt); auto* outer_loop = Loop(Block(), outer_continuing); diff --git a/src/tint/writer/wgsl/generator_impl_if_test.cc b/src/tint/writer/wgsl/generator_impl_if_test.cc index bf85df672f..18c0ffddc4 100644 --- a/src/tint/writer/wgsl/generator_impl_if_test.cc +++ b/src/tint/writer/wgsl/generator_impl_if_test.cc @@ -47,7 +47,7 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) { auto* cond = Expr("cond"); auto* body = Block(Return()); - auto* i = If(cond, body, If(else_cond, else_body)); + auto* i = If(cond, body, Else(If(else_cond, else_body))); WrapInFunction(i); GeneratorImpl& gen = Build(); @@ -70,7 +70,7 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) { auto* cond = Expr("cond"); auto* body = Block(Return()); - auto* i = If(cond, body, else_body); + auto* i = If(cond, body, Else(else_body)); WrapInFunction(i); GeneratorImpl& gen = Build(); @@ -98,7 +98,7 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithMultiple) { auto* cond = Expr("cond"); auto* body = Block(Return()); - auto* i = If(cond, body, If(else_cond, else_body, else_body_2)); + auto* i = If(cond, body, Else(If(else_cond, else_body, Else(else_body_2)))); WrapInFunction(i); GeneratorImpl& gen = Build();