Validate binary operations

This change validates that the operand types and result type of every
binary operation is valid.

* Added two unit tests which test all valid and invalid param combos. I
also removed the old tests, many of which failed once I added this
validation, and the rest are obviated by the new tests.

* Fixed VertexPulling transform, as well as many tests, that were using
invalid operand types for binary operations.

Fixed: tint:354
Change-Id: Ia3f48384256993da61b341f17ba5583741011819
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/44341
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Antonio Maiorano
2021-03-16 13:26:03 +00:00
committed by Commit Bot service account
parent 1691401179
commit be0fc4e929
11 changed files with 534 additions and 259 deletions

View File

@@ -23,11 +23,11 @@ namespace ast {
/// The operator type
enum class BinaryOp {
kNone = 0,
kAnd,
kOr,
kAnd, // &
kOr, // |
kXor,
kLogicalAnd,
kLogicalOr,
kLogicalAnd, // &&
kLogicalOr, // ||
kEqual,
kNotEqual,
kLessThan,
@@ -98,6 +98,14 @@ class BinaryExpression : public Castable<BinaryExpression, Expression> {
bool IsDivide() const { return op_ == BinaryOp::kDivide; }
/// @returns true if the op is modulo
bool IsModulo() const { return op_ == BinaryOp::kModulo; }
/// @returns true if the op is an arithmetic operation
bool IsArithmetic() const;
/// @returns true if the op is a comparison operation
bool IsComparison() const;
/// @returns true if the op is a bitwise operation
bool IsBitwise() const;
/// @returns true if the op is a bit shift operation
bool IsBitshift() const;
/// @returns the left side expression
Expression* lhs() const { return lhs_; }
@@ -126,6 +134,54 @@ class BinaryExpression : public Castable<BinaryExpression, Expression> {
Expression* const rhs_;
};
inline bool BinaryExpression::IsArithmetic() const {
switch (op_) {
case ast::BinaryOp::kAdd:
case ast::BinaryOp::kSubtract:
case ast::BinaryOp::kMultiply:
case ast::BinaryOp::kDivide:
case ast::BinaryOp::kModulo:
return true;
default:
return false;
}
}
inline bool BinaryExpression::IsComparison() const {
switch (op_) {
case ast::BinaryOp::kEqual:
case ast::BinaryOp::kNotEqual:
case ast::BinaryOp::kLessThan:
case ast::BinaryOp::kLessThanEqual:
case ast::BinaryOp::kGreaterThan:
case ast::BinaryOp::kGreaterThanEqual:
return true;
default:
return false;
}
}
inline bool BinaryExpression::IsBitwise() const {
switch (op_) {
case ast::BinaryOp::kAnd:
case ast::BinaryOp::kOr:
case ast::BinaryOp::kXor:
return true;
default:
return false;
}
}
inline bool BinaryExpression::IsBitshift() const {
switch (op_) {
case ast::BinaryOp::kShiftLeft:
case ast::BinaryOp::kShiftRight:
return true;
default:
return false;
}
}
inline std::ostream& operator<<(std::ostream& out, BinaryOp op) {
switch (op) {
case BinaryOp::kNone: