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 <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/tint/program_builder.h b/src/tint/program_builder.h
index 0b26ede..5c3feee 100644
--- a/src/tint/program_builder.h
+++ b/src/tint/program_builder.h
@@ -2080,6 +2080,17 @@
ast::StatementList{std::forward<STATEMENTS>(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 @@
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<ast::IfStatement>(source, Expr(std::forward<CONDITION>(condition)), body,
- else_stmt);
+ else_stmt.stmt);
}
/// Creates a ast::IfStatement with input condition, body, and optional
@@ -2105,10 +2116,16 @@
template <typename CONDITION>
const ast::IfStatement* If(CONDITION&& condition,
const ast::BlockStatement* body,
- const ast::Statement* else_stmt = nullptr) {
- return create<ast::IfStatement>(Expr(std::forward<CONDITION>(condition)), body, else_stmt);
+ const ElseStmt else_stmt = ElseStmt()) {
+ return create<ast::IfStatement>(Expr(std::forward<CONDITION>(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