reader/spirv: Add bool operator to TypedExpression

Use this to check that type and expr are both not nullptr.

Cleans up the random mix of checking `expr`, `type` or both.

Change-Id: I84eb4a16e0dc22e18d6868f62c0314effe9858a2
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51184
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-05-17 15:00:17 +00:00 committed by Commit Bot service account
parent c88fbe0fb2
commit bcd909efa4
3 changed files with 26 additions and 23 deletions

View File

@ -2662,7 +2662,7 @@ bool FunctionEmitter::EmitSwitchStart(const BlockInfo& block_info) {
const auto selector_id = branch->GetSingleWordInOperand(0); const auto selector_id = branch->GetSingleWordInOperand(0);
// Generate the code for the selector. // Generate the code for the selector.
auto selector = MakeExpression(selector_id); auto selector = MakeExpression(selector_id);
if (!selector.expr) { if (!selector) {
return false; return false;
} }
// First, push the statement block for the entire switch. // First, push the statement block for the entire switch.
@ -2800,7 +2800,7 @@ bool FunctionEmitter::EmitNormalTerminator(const BlockInfo& block_info) {
return true; return true;
case SpvOpReturnValue: { case SpvOpReturnValue: {
auto value = MakeExpression(terminator.GetSingleWordInOperand(0)); auto value = MakeExpression(terminator.GetSingleWordInOperand(0));
if (!value.expr) { if (!value) {
return false; return false;
} }
AddStatement(create<ast::ReturnStatement>(Source{}, value.expr)); AddStatement(create<ast::ReturnStatement>(Source{}, value.expr));
@ -3114,7 +3114,7 @@ bool FunctionEmitter::EmitStatementsInBasicBlock(const BlockInfo& block_info,
for (auto assignment : block_info.phi_assignments) { for (auto assignment : block_info.phi_assignments) {
const auto var_name = GetDefInfo(assignment.phi_id)->phi_var; const auto var_name = GetDefInfo(assignment.phi_id)->phi_var;
auto expr = MakeExpression(assignment.value); auto expr = MakeExpression(assignment.value);
if (!expr.expr) { if (!expr) {
return false; return false;
} }
AddStatement(create<ast::AssignmentStatement>( AddStatement(create<ast::AssignmentStatement>(
@ -3132,7 +3132,7 @@ bool FunctionEmitter::EmitStatementsInBasicBlock(const BlockInfo& block_info,
bool FunctionEmitter::EmitConstDefinition( bool FunctionEmitter::EmitConstDefinition(
const spvtools::opt::Instruction& inst, const spvtools::opt::Instruction& inst,
TypedExpression ast_expr) { TypedExpression ast_expr) {
if (!ast_expr.expr) { if (!ast_expr) {
return false; return false;
} }
auto* ast_const = parser_impl_.MakeVariable( auto* ast_const = parser_impl_.MakeVariable(
@ -3242,7 +3242,7 @@ bool FunctionEmitter::EmitStatement(const spvtools::opt::Instruction& inst) {
} }
TypedExpression rhs = MakeExpression(value_id); TypedExpression rhs = MakeExpression(value_id);
if (!rhs.expr) { if (!rhs) {
return false; return false;
} }
@ -3279,7 +3279,7 @@ bool FunctionEmitter::EmitStatement(const spvtools::opt::Instruction& inst) {
// Handle an ordinary store as an assignment. // Handle an ordinary store as an assignment.
auto lhs = MakeExpression(ptr_id); auto lhs = MakeExpression(ptr_id);
if (!lhs.expr) { if (!lhs) {
return false; return false;
} }
@ -3339,7 +3339,7 @@ bool FunctionEmitter::EmitStatement(const spvtools::opt::Instruction& inst) {
break; break;
} }
auto expr = MakeExpression(ptr_id); auto expr = MakeExpression(ptr_id);
if (!expr.expr) { if (!expr) {
return false; return false;
} }
@ -3383,7 +3383,7 @@ bool FunctionEmitter::EmitStatement(const spvtools::opt::Instruction& inst) {
return true; return true;
} }
auto expr = MakeExpression(value_id); auto expr = MakeExpression(value_id);
if (!expr.type || !expr.expr) { if (!expr) {
return false; return false;
} }
expr.type = RemapStorageClass(expr.type, result_id); expr.type = RemapStorageClass(expr.type, result_id);
@ -3456,7 +3456,7 @@ TypedExpression FunctionEmitter::MakeOperand(
const spvtools::opt::Instruction& inst, const spvtools::opt::Instruction& inst,
uint32_t operand_index) { uint32_t operand_index) {
auto expr = this->MakeExpression(inst.GetSingleWordInOperand(operand_index)); auto expr = this->MakeExpression(inst.GetSingleWordInOperand(operand_index));
if (!expr.expr) { if (!expr) {
return {}; return {};
} }
return parser_impl_.RectifyOperandSignedness(inst, std::move(expr)); return parser_impl_.RectifyOperandSignedness(inst, std::move(expr));
@ -3790,7 +3790,7 @@ TypedExpression FunctionEmitter::MakeAccessChain(
// walking down into composites. The Tint AST represents this as // walking down into composites. The Tint AST represents this as
// ever-deeper nested indexing expressions. Start off with an expression // ever-deeper nested indexing expressions. Start off with an expression
// for the base, and then bury that inside nested indexing expressions. // for the base, and then bury that inside nested indexing expressions.
if (!current_expr.expr) { if (!current_expr) {
current_expr = MakeOperand(inst, 0); current_expr = MakeOperand(inst, 0);
} }
const auto constants = constant_mgr_->GetOperandConstants(&inst); const auto constants = constant_mgr_->GetOperandConstants(&inst);
@ -4091,7 +4091,7 @@ TypedExpression FunctionEmitter::MakeVectorShuffle(
const auto index = inst.GetSingleWordInOperand(i); const auto index = inst.GetSingleWordInOperand(i);
if (index < vec0_len) { if (index < vec0_len) {
auto expr = MakeExpression(vec0_id); auto expr = MakeExpression(vec0_id);
if (!expr.expr) { if (!expr) {
return {}; return {};
} }
values.emplace_back(create<ast::MemberAccessorExpression>( values.emplace_back(create<ast::MemberAccessorExpression>(
@ -4100,7 +4100,7 @@ TypedExpression FunctionEmitter::MakeVectorShuffle(
const auto sub_index = index - vec0_len; const auto sub_index = index - vec0_len;
TINT_ASSERT(sub_index < kMaxVectorLen); TINT_ASSERT(sub_index < kMaxVectorLen);
auto expr = MakeExpression(vec1_id); auto expr = MakeExpression(vec1_id);
if (!expr.expr) { if (!expr) {
return {}; return {};
} }
values.emplace_back(create<ast::MemberAccessorExpression>( values.emplace_back(create<ast::MemberAccessorExpression>(
@ -4424,7 +4424,7 @@ TypedExpression FunctionEmitter::MakeNumericConversion(
const auto opcode = inst.opcode(); const auto opcode = inst.opcode();
auto* requested_type = parser_impl_.ConvertType(inst.type_id()); auto* requested_type = parser_impl_.ConvertType(inst.type_id());
auto arg_expr = MakeOperand(inst, 0); auto arg_expr = MakeOperand(inst, 0);
if (!arg_expr.expr || !arg_expr.type) { if (!arg_expr) {
return {}; return {};
} }
@ -5039,7 +5039,7 @@ ast::ExpressionList FunctionEmitter::MakeCoordinateOperandsForImageAccess(
// The coordinates parameter is always in position 1. // The coordinates parameter is always in position 1.
TypedExpression raw_coords(MakeOperand(inst, 1)); TypedExpression raw_coords(MakeOperand(inst, 1));
if (!raw_coords.type) { if (!raw_coords) {
return {}; return {};
} }
const Texture* texture_type = GetImageType(*image); const Texture* texture_type = GetImageType(*image);
@ -5192,7 +5192,7 @@ ast::Expression* FunctionEmitter::ConvertTexelForStorage(
} }
TypedExpression FunctionEmitter::ToI32(TypedExpression value) { TypedExpression FunctionEmitter::ToI32(TypedExpression value) {
if (!value.type || value.type->Is<I32>()) { if (!value || value.type->Is<I32>()) {
return value; return value;
} }
return {ty_.I32(), return {ty_.I32(),
@ -5201,7 +5201,7 @@ TypedExpression FunctionEmitter::ToI32(TypedExpression value) {
} }
TypedExpression FunctionEmitter::ToSignedIfUnsigned(TypedExpression value) { TypedExpression FunctionEmitter::ToSignedIfUnsigned(TypedExpression value) {
if (!value.type || !value.type->IsUnsignedScalarOrVector()) { if (!value || !value.type->IsUnsignedScalarOrVector()) {
return value; return value;
} }
if (auto* vec_type = value.type->As<Vector>()) { if (auto* vec_type = value.type->As<Vector>()) {
@ -5237,7 +5237,7 @@ TypedExpression FunctionEmitter::MakeArrayLength(
auto* member_ident = create<ast::IdentifierExpression>( auto* member_ident = create<ast::IdentifierExpression>(
Source{}, builder_.Symbols().Register(field_name)); Source{}, builder_.Symbols().Register(field_name));
auto member_expr = MakeExpression(struct_ptr_id); auto member_expr = MakeExpression(struct_ptr_id);
if (!member_expr.expr) { if (!member_expr) {
return {}; return {};
} }
auto* member_access = create<ast::MemberAccessorExpression>( auto* member_access = create<ast::MemberAccessorExpression>(
@ -5382,7 +5382,7 @@ bool FunctionEmitter::MakeCompositeInsert(
// The left-hand side of the assignment *looks* like a decomposition. // The left-hand side of the assignment *looks* like a decomposition.
TypedExpression lhs = TypedExpression lhs =
MakeCompositeValueDecomposition(inst, seed_expr, inst.type_id(), 2); MakeCompositeValueDecomposition(inst, seed_expr, inst.type_id(), 2);
if (!lhs.expr) { if (!lhs) {
return false; return false;
} }

View File

@ -1673,7 +1673,7 @@ TypedExpression ParserImpl::RectifyOperandSignedness(
// No conversion is required, assuming our tables are complete. // No conversion is required, assuming our tables are complete.
return std::move(expr); return std::move(expr);
} }
if (!expr.expr) { if (!expr) {
Fail() << "internal error: RectifyOperandSignedness given a null expr\n"; Fail() << "internal error: RectifyOperandSignedness given a null expr\n";
return {}; return {};
} }

View File

@ -67,15 +67,18 @@ struct TypedExpression {
/// Copy constructor /// Copy constructor
TypedExpression(const TypedExpression&); TypedExpression(const TypedExpression&);
/// Assignment operator
/// @returns this TypedExpression
TypedExpression& operator=(const TypedExpression&);
/// Constructor /// Constructor
/// @param type_in the type of the expression /// @param type_in the type of the expression
/// @param expr_in the expression /// @param expr_in the expression
TypedExpression(const Type* type_in, ast::Expression* expr_in); TypedExpression(const Type* type_in, ast::Expression* expr_in);
/// Assignment operator
/// @returns this TypedExpression
TypedExpression& operator=(const TypedExpression&);
/// @returns true if both type and expr are not nullptr
operator bool() const { return type && expr; }
/// The type /// The type
Type const* type = nullptr; Type const* type = nullptr;
/// The expression /// The expression