Rename RelationalExpression to BinaryExpression. Match the more common usage for the expression type. Bug: tint:37 Change-Id: Ia5d48a0444742ec4e304ea1036e499b3d7cad682 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18981 Reviewed-by: Ryan Harrison <rharrison@chromium.org>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f6fcaa5..102f26a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt
@@ -43,6 +43,8 @@ ast/as_expression.h ast/assignment_statement.cc ast/assignment_statement.h + ast/binary_expression.cc + ast/binary_expression.h ast/binding_decoration.cc ast/binding_decoration.h ast/bool_literal.h @@ -107,8 +109,6 @@ ast/pipeline_stage.h ast/regardless_statement.cc ast/regardless_statement.h - ast/relational_expression.cc - ast/relational_expression.h ast/return_statement.cc ast/return_statement.h ast/scalar_constructor_expression.cc @@ -281,7 +281,7 @@ ast/module_test.cc ast/nop_statement_test.cc ast/regardless_statement_test.cc - ast/relational_expression_test.cc + ast/binary_expression_test.cc ast/return_statement_test.cc ast/scalar_constructor_expression_test.cc ast/set_decoration_test.cc
diff --git a/src/ast/binary_expression.cc b/src/ast/binary_expression.cc new file mode 100644 index 0000000..115b41f --- /dev/null +++ b/src/ast/binary_expression.cc
@@ -0,0 +1,59 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/ast/binary_expression.h" + +namespace tint { +namespace ast { + +BinaryExpression::BinaryExpression() : Expression() {} + +BinaryExpression::BinaryExpression(BinaryOp op, + std::unique_ptr<Expression> lhs, + std::unique_ptr<Expression> rhs) + : Expression(), op_(op), lhs_(std::move(lhs)), rhs_(std::move(rhs)) {} + +BinaryExpression::BinaryExpression(const Source& source, + BinaryOp op, + std::unique_ptr<Expression> lhs, + std::unique_ptr<Expression> rhs) + : Expression(source), op_(op), lhs_(std::move(lhs)), rhs_(std::move(rhs)) {} + +BinaryExpression::~BinaryExpression() = default; + +bool BinaryExpression::IsValid() const { + if (lhs_ == nullptr || !lhs_->IsValid()) { + return false; + } + if (rhs_ == nullptr || !rhs_->IsValid()) { + return false; + } + return op_ != BinaryOp::kNone; +} + +void BinaryExpression::to_str(std::ostream& out, size_t indent) const { + make_indent(out, indent); + out << "Binary{" << std::endl; + lhs_->to_str(out, indent + 2); + + make_indent(out, indent + 2); + out << op_ << std::endl; + + rhs_->to_str(out, indent + 2); + make_indent(out, indent); + out << "}" << std::endl; +} + +} // namespace ast +} // namespace tint
diff --git a/src/ast/binary_expression.h b/src/ast/binary_expression.h new file mode 100644 index 0000000..0af2b6e --- /dev/null +++ b/src/ast/binary_expression.h
@@ -0,0 +1,221 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef SRC_AST_BINARY_EXPRESSION_H_ +#define SRC_AST_BINARY_EXPRESSION_H_ + +#include <memory> +#include <utility> + +#include "src/ast/expression.h" +#include "src/ast/literal.h" + +namespace tint { +namespace ast { + +/// The operator type +enum class BinaryOp { + kNone = 0, + kAnd, + kOr, + kXor, + kLogicalAnd, + kLogicalOr, + kEqual, + kNotEqual, + kLessThan, + kGreaterThan, + kLessThanEqual, + kGreaterThanEqual, + kShiftLeft, + kShiftRight, + kShiftRightArith, + kAdd, + kSubtract, + kMultiply, + kDivide, + kModulo, +}; + +/// An binary expression +class BinaryExpression : public Expression { + public: + /// Constructor + BinaryExpression(); + /// Constructor + /// @param op the operation type + /// @param lhs the left side of the expression + /// @param rhs the right side of the expression + BinaryExpression(BinaryOp op, + std::unique_ptr<Expression> lhs, + std::unique_ptr<Expression> rhs); + /// Constructor + /// @param source the binary expression source + /// @param op the operation type + /// @param lhs the left side of the expression + /// @param rhs the right side of the expression + BinaryExpression(const Source& source, + BinaryOp op, + std::unique_ptr<Expression> lhs, + std::unique_ptr<Expression> rhs); + /// Move constructor + BinaryExpression(BinaryExpression&&) = default; + ~BinaryExpression() override; + + /// Sets the binary op type + /// @param op the binary op type + void set_op(BinaryOp op) { op_ = op; } + /// @returns the binary op type + BinaryOp op() const { return op_; } + + /// @returns true if the op is and + bool IsAnd() const { return op_ == BinaryOp::kAnd; } + /// @returns true if the op is or + bool IsOr() const { return op_ == BinaryOp::kOr; } + /// @returns true if the op is xor + bool IsXor() const { return op_ == BinaryOp::kXor; } + /// @returns true if the op is logical and + bool IsLogicalAnd() const { return op_ == BinaryOp::kLogicalAnd; } + /// @returns true if the op is logical or + bool IsLogicalOr() const { return op_ == BinaryOp::kLogicalOr; } + /// @returns true if the op is equal + bool IsEqual() const { return op_ == BinaryOp::kEqual; } + /// @returns true if the op is not equal + bool IsNotEqual() const { return op_ == BinaryOp::kNotEqual; } + /// @returns true if the op is less than + bool IsLessThan() const { return op_ == BinaryOp::kLessThan; } + /// @returns true if the op is greater than + bool IsGreaterThan() const { return op_ == BinaryOp::kGreaterThan; } + /// @returns true if the op is less than equal + bool IsLessThanEqual() const { return op_ == BinaryOp::kLessThanEqual; } + /// @returns true if the op is greater than equal + bool IsGreaterThanEqual() const { return op_ == BinaryOp::kGreaterThanEqual; } + /// @returns true if the op is shift left + bool IsShiftLeft() const { return op_ == BinaryOp::kShiftLeft; } + /// @returns true if the op is shift right + bool IsShiftRight() const { return op_ == BinaryOp::kShiftRight; } + /// @returns true if the op is shift right arith + bool IsShiftRightArith() const { return op_ == BinaryOp::kShiftRightArith; } + /// @returns true if the op is add + bool IsAdd() const { return op_ == BinaryOp::kAdd; } + /// @returns true if the op is subtract + bool IsSubtract() const { return op_ == BinaryOp::kSubtract; } + /// @returns true if the op is multiply + bool IsMultiply() const { return op_ == BinaryOp::kMultiply; } + /// @returns true if the op is divide + bool IsDivide() const { return op_ == BinaryOp::kDivide; } + /// @returns true if the op is modulo + bool IsModulo() const { return op_ == BinaryOp::kModulo; } + + /// Sets the left side of the expression + /// @param lhs the left side to set + void set_lhs(std::unique_ptr<Expression> lhs) { lhs_ = std::move(lhs); } + /// @returns the left side expression + Expression* lhs() const { return lhs_.get(); } + + /// Sets the right side of the expression + /// @param rhs the right side to set + void set_rhs(std::unique_ptr<Expression> rhs) { rhs_ = std::move(rhs); } + /// @returns the right side expression + Expression* rhs() const { return rhs_.get(); } + + /// @returns true if this is a op expression + bool IsBinary() const override { return true; } + + /// @returns true if the node is valid + bool IsValid() const override; + + /// Writes a representation of the node to the output stream + /// @param out the stream to write to + /// @param indent number of spaces to indent the node when writing + void to_str(std::ostream& out, size_t indent) const override; + + private: + BinaryExpression(const BinaryExpression&) = delete; + + BinaryOp op_ = BinaryOp::kNone; + std::unique_ptr<Expression> lhs_; + std::unique_ptr<Expression> rhs_; +}; + +inline std::ostream& operator<<(std::ostream& out, BinaryOp op) { + switch (op) { + case BinaryOp::kNone: + out << "none"; + break; + case BinaryOp::kAnd: + out << "and"; + break; + case BinaryOp::kOr: + out << "or"; + break; + case BinaryOp::kXor: + out << "xor"; + break; + case BinaryOp::kLogicalAnd: + out << "logical_and"; + break; + case BinaryOp::kLogicalOr: + out << "logical_or"; + break; + case BinaryOp::kEqual: + out << "equal"; + break; + case BinaryOp::kNotEqual: + out << "not_equal"; + break; + case BinaryOp::kLessThan: + out << "less_than"; + break; + case BinaryOp::kGreaterThan: + out << "greater_than"; + break; + case BinaryOp::kLessThanEqual: + out << "less_than_equal"; + break; + case BinaryOp::kGreaterThanEqual: + out << "greater_than_equal"; + break; + case BinaryOp::kShiftLeft: + out << "shift_left"; + break; + case BinaryOp::kShiftRight: + out << "shift_right"; + break; + case BinaryOp::kShiftRightArith: + out << "shift_right_arith"; + break; + case BinaryOp::kAdd: + out << "add"; + break; + case BinaryOp::kSubtract: + out << "subtract"; + break; + case BinaryOp::kMultiply: + out << "multiply"; + break; + case BinaryOp::kDivide: + out << "divide"; + break; + case BinaryOp::kModulo: + out << "modulo"; + break; + } + return out; +} + +} // namespace ast +} // namespace tint + +#endif // SRC_AST_BINARY_EXPRESSION_H_
diff --git a/src/ast/relational_expression_test.cc b/src/ast/binary_expression_test.cc similarity index 61% rename from src/ast/relational_expression_test.cc rename to src/ast/binary_expression_test.cc index b014448..0ca73aa 100644 --- a/src/ast/relational_expression_test.cc +++ b/src/ast/binary_expression_test.cc
@@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "src/ast/relational_expression.h" +#include "src/ast/binary_expression.h" #include <sstream> @@ -23,95 +23,95 @@ namespace ast { namespace { -using RelationalExpressionTest = testing::Test; +using BinaryExpressionTest = testing::Test; -TEST_F(RelationalExpressionTest, Creation) { +TEST_F(BinaryExpressionTest, Creation) { auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto rhs = std::make_unique<IdentifierExpression>("rhs"); auto lhs_ptr = lhs.get(); auto rhs_ptr = rhs.get(); - RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs)); + BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs)); EXPECT_EQ(r.lhs(), lhs_ptr); EXPECT_EQ(r.rhs(), rhs_ptr); - EXPECT_EQ(r.relation(), Relation::kEqual); + EXPECT_EQ(r.op(), BinaryOp::kEqual); } -TEST_F(RelationalExpressionTest, Creation_WithSource) { +TEST_F(BinaryExpressionTest, Creation_WithSource) { auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto rhs = std::make_unique<IdentifierExpression>("rhs"); - RelationalExpression r(Source{20, 2}, Relation::kEqual, std::move(lhs), - std::move(rhs)); + BinaryExpression r(Source{20, 2}, BinaryOp::kEqual, std::move(lhs), + std::move(rhs)); auto src = r.source(); EXPECT_EQ(src.line, 20); EXPECT_EQ(src.column, 2); } -TEST_F(RelationalExpressionTest, IsRelational) { - RelationalExpression r; - EXPECT_TRUE(r.IsRelational()); +TEST_F(BinaryExpressionTest, IsBinaryal) { + BinaryExpression r; + EXPECT_TRUE(r.IsBinary()); } -TEST_F(RelationalExpressionTest, IsValid) { +TEST_F(BinaryExpressionTest, IsValid) { auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto rhs = std::make_unique<IdentifierExpression>("rhs"); - RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs)); + BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs)); EXPECT_TRUE(r.IsValid()); } -TEST_F(RelationalExpressionTest, IsValid_Null_LHS) { +TEST_F(BinaryExpressionTest, IsValid_Null_LHS) { auto rhs = std::make_unique<IdentifierExpression>("rhs"); - RelationalExpression r; - r.set_relation(Relation::kEqual); + BinaryExpression r; + r.set_op(BinaryOp::kEqual); r.set_rhs(std::move(rhs)); EXPECT_FALSE(r.IsValid()); } -TEST_F(RelationalExpressionTest, IsValid_Invalid_LHS) { +TEST_F(BinaryExpressionTest, IsValid_Invalid_LHS) { auto lhs = std::make_unique<IdentifierExpression>(""); auto rhs = std::make_unique<IdentifierExpression>("rhs"); - RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs)); + BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs)); EXPECT_FALSE(r.IsValid()); } -TEST_F(RelationalExpressionTest, IsValid_Null_RHS) { +TEST_F(BinaryExpressionTest, IsValid_Null_RHS) { auto lhs = std::make_unique<IdentifierExpression>("lhs"); - RelationalExpression r; - r.set_relation(Relation::kEqual); + BinaryExpression r; + r.set_op(BinaryOp::kEqual); r.set_lhs(std::move(lhs)); EXPECT_FALSE(r.IsValid()); } -TEST_F(RelationalExpressionTest, IsValid_Invalid_RHS) { +TEST_F(BinaryExpressionTest, IsValid_Invalid_RHS) { auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto rhs = std::make_unique<IdentifierExpression>(""); - RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs)); + BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs)); EXPECT_FALSE(r.IsValid()); } -TEST_F(RelationalExpressionTest, IsValid_Relation_None) { +TEST_F(BinaryExpressionTest, IsValid_Binary_None) { auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto rhs = std::make_unique<IdentifierExpression>("rhs"); - RelationalExpression r(Relation::kNone, std::move(lhs), std::move(rhs)); + BinaryExpression r(BinaryOp::kNone, std::move(lhs), std::move(rhs)); EXPECT_FALSE(r.IsValid()); } -TEST_F(RelationalExpressionTest, ToStr) { +TEST_F(BinaryExpressionTest, ToStr) { auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto rhs = std::make_unique<IdentifierExpression>("rhs"); - RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs)); + BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs)); std::ostringstream out; r.to_str(out, 2); - EXPECT_EQ(out.str(), R"( Relation{ + EXPECT_EQ(out.str(), R"( Binary{ Identifier{lhs} equal Identifier{rhs}
diff --git a/src/ast/expression.cc b/src/ast/expression.cc index 08e536e..6e34ef9 100644 --- a/src/ast/expression.cc +++ b/src/ast/expression.cc
@@ -18,12 +18,12 @@ #include "src/ast/array_accessor_expression.h" #include "src/ast/as_expression.h" +#include "src/ast/binary_expression.h" #include "src/ast/call_expression.h" #include "src/ast/cast_expression.h" #include "src/ast/constructor_expression.h" #include "src/ast/identifier_expression.h" #include "src/ast/member_accessor_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/unary_derivative_expression.h" #include "src/ast/unary_method_expression.h" #include "src/ast/unary_op_expression.h" @@ -47,6 +47,11 @@ return static_cast<AsExpression*>(this); } +BinaryExpression* Expression::AsBinary() { + assert(IsBinary()); + return static_cast<BinaryExpression*>(this); +} + CallExpression* Expression::AsCall() { assert(IsCall()); return static_cast<CallExpression*>(this); @@ -57,26 +62,21 @@ return static_cast<CastExpression*>(this); } -IdentifierExpression* Expression::AsIdentifier() { - assert(IsIdentifier()); - return static_cast<IdentifierExpression*>(this); -} - ConstructorExpression* Expression::AsConstructor() { assert(IsConstructor()); return static_cast<ConstructorExpression*>(this); } +IdentifierExpression* Expression::AsIdentifier() { + assert(IsIdentifier()); + return static_cast<IdentifierExpression*>(this); +} + MemberAccessorExpression* Expression::AsMemberAccessor() { assert(IsMemberAccessor()); return static_cast<MemberAccessorExpression*>(this); } -RelationalExpression* Expression::AsRelational() { - assert(IsRelational()); - return static_cast<RelationalExpression*>(this); -} - UnaryDerivativeExpression* Expression::AsUnaryDerivative() { assert(IsUnaryDerivative()); return static_cast<UnaryDerivativeExpression*>(this);
diff --git a/src/ast/expression.h b/src/ast/expression.h index 065755f..abd7661 100644 --- a/src/ast/expression.h +++ b/src/ast/expression.h
@@ -26,12 +26,12 @@ class ArrayAccessorExpression; class AsExpression; +class BinaryExpression; class CallExpression; class CastExpression; class IdentifierExpression; class ConstructorExpression; class MemberAccessorExpression; -class RelationalExpression; class UnaryDerivativeExpression; class UnaryMethodExpression; class UnaryOpExpression; @@ -61,8 +61,8 @@ virtual bool IsConstructor() const { return false; } /// @returns true if this is a member accessor expression virtual bool IsMemberAccessor() const { return false; } - /// @returns true if this is a relational expression - virtual bool IsRelational() const { return false; } + /// @returns true if this is a binary expression + virtual bool IsBinary() const { return false; } /// @returns true if this is a unary derivative expression virtual bool IsUnaryDerivative() const { return false; } /// @returns true if this is a unary method expression @@ -84,8 +84,8 @@ ConstructorExpression* AsConstructor(); /// @returns the expression as a member accessor MemberAccessorExpression* AsMemberAccessor(); - /// @returns the expression as a relational expression - RelationalExpression* AsRelational(); + /// @returns the expression as a binary expression + BinaryExpression* AsBinary(); /// @returns the expression as a unary derivative expression UnaryDerivativeExpression* AsUnaryDerivative(); /// @returns the expression as a unary method expression
diff --git a/src/ast/relational_expression.cc b/src/ast/relational_expression.cc deleted file mode 100644 index a5e3863..0000000 --- a/src/ast/relational_expression.cc +++ /dev/null
@@ -1,65 +0,0 @@ -// Copyright 2020 The Tint Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "src/ast/relational_expression.h" - -namespace tint { -namespace ast { - -RelationalExpression::RelationalExpression() : Expression() {} - -RelationalExpression::RelationalExpression(Relation relation, - std::unique_ptr<Expression> lhs, - std::unique_ptr<Expression> rhs) - : Expression(), - relation_(relation), - lhs_(std::move(lhs)), - rhs_(std::move(rhs)) {} - -RelationalExpression::RelationalExpression(const Source& source, - Relation relation, - std::unique_ptr<Expression> lhs, - std::unique_ptr<Expression> rhs) - : Expression(source), - relation_(relation), - lhs_(std::move(lhs)), - rhs_(std::move(rhs)) {} - -RelationalExpression::~RelationalExpression() = default; - -bool RelationalExpression::IsValid() const { - if (lhs_ == nullptr || !lhs_->IsValid()) { - return false; - } - if (rhs_ == nullptr || !rhs_->IsValid()) { - return false; - } - return relation_ != Relation::kNone; -} - -void RelationalExpression::to_str(std::ostream& out, size_t indent) const { - make_indent(out, indent); - out << "Relation{" << std::endl; - lhs_->to_str(out, indent + 2); - - make_indent(out, indent + 2); - out << relation_ << std::endl; - - rhs_->to_str(out, indent + 2); - make_indent(out, indent); - out << "}" << std::endl; -} - -} // namespace ast -} // namespace tint
diff --git a/src/ast/relational_expression.h b/src/ast/relational_expression.h deleted file mode 100644 index f5bd944..0000000 --- a/src/ast/relational_expression.h +++ /dev/null
@@ -1,225 +0,0 @@ -// Copyright 2020 The Tint Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef SRC_AST_RELATIONAL_EXPRESSION_H_ -#define SRC_AST_RELATIONAL_EXPRESSION_H_ - -#include <memory> -#include <utility> - -#include "src/ast/expression.h" -#include "src/ast/literal.h" - -namespace tint { -namespace ast { - -/// The relation type -enum class Relation { - kNone = 0, - kAnd, - kOr, - kXor, - kLogicalAnd, - kLogicalOr, - kEqual, - kNotEqual, - kLessThan, - kGreaterThan, - kLessThanEqual, - kGreaterThanEqual, - kShiftLeft, - kShiftRight, - kShiftRightArith, - kAdd, - kSubtract, - kMultiply, - kDivide, - kModulo, -}; - -/// An xor expression -class RelationalExpression : public Expression { - public: - /// Constructor - RelationalExpression(); - /// Constructor - /// @param relation the relation type - /// @param lhs the left side of the expression - /// @param rhs the right side of the expression - RelationalExpression(Relation relation, - std::unique_ptr<Expression> lhs, - std::unique_ptr<Expression> rhs); - /// Constructor - /// @param source the relational expression source - /// @param relation the relation type - /// @param lhs the left side of the expression - /// @param rhs the right side of the expression - RelationalExpression(const Source& source, - Relation relation, - std::unique_ptr<Expression> lhs, - std::unique_ptr<Expression> rhs); - /// Move constructor - RelationalExpression(RelationalExpression&&) = default; - ~RelationalExpression() override; - - /// Sets the relation type - /// @param relation the relation type - void set_relation(Relation relation) { relation_ = relation; } - /// @returns the relation - Relation relation() const { return relation_; } - - /// @returns true if the relation is and - bool IsAnd() const { return relation_ == Relation::kAnd; } - /// @returns true if the relation is or - bool IsOr() const { return relation_ == Relation::kOr; } - /// @returns true if the relation is xor - bool IsXor() const { return relation_ == Relation::kXor; } - /// @returns true if the relation is logical and - bool IsLogicalAnd() const { return relation_ == Relation::kLogicalAnd; } - /// @returns true if the relation is logical or - bool IsLogicalOr() const { return relation_ == Relation::kLogicalOr; } - /// @returns true if the relation is equal - bool IsEqual() const { return relation_ == Relation::kEqual; } - /// @returns true if the relation is not equal - bool IsNotEqual() const { return relation_ == Relation::kNotEqual; } - /// @returns true if the relation is less than - bool IsLessThan() const { return relation_ == Relation::kLessThan; } - /// @returns true if the relation is greater than - bool IsGreaterThan() const { return relation_ == Relation::kGreaterThan; } - /// @returns true if the relation is less than equal - bool IsLessThanEqual() const { return relation_ == Relation::kLessThanEqual; } - /// @returns true if the relation is greater than equal - bool IsGreaterThanEqual() const { - return relation_ == Relation::kGreaterThanEqual; - } - /// @returns true if the relation is shift left - bool IsShiftLeft() const { return relation_ == Relation::kShiftLeft; } - /// @returns true if the relation is shift right - bool IsShiftRight() const { return relation_ == Relation::kShiftRight; } - /// @returns true if the relation is shift right arith - bool IsShiftRightArith() const { - return relation_ == Relation::kShiftRightArith; - } - /// @returns true if the relation is add - bool IsAdd() const { return relation_ == Relation::kAdd; } - /// @returns true if the relation is subtract - bool IsSubtract() const { return relation_ == Relation::kSubtract; } - /// @returns true if the relation is multiply - bool IsMultiply() const { return relation_ == Relation::kMultiply; } - /// @returns true if the relation is divide - bool IsDivide() const { return relation_ == Relation::kDivide; } - /// @returns true if the relation is modulo - bool IsModulo() const { return relation_ == Relation::kModulo; } - - /// Sets the left side of the expression - /// @param lhs the left side to set - void set_lhs(std::unique_ptr<Expression> lhs) { lhs_ = std::move(lhs); } - /// @returns the left side expression - Expression* lhs() const { return lhs_.get(); } - - /// Sets the right side of the expression - /// @param rhs the right side to set - void set_rhs(std::unique_ptr<Expression> rhs) { rhs_ = std::move(rhs); } - /// @returns the right side expression - Expression* rhs() const { return rhs_.get(); } - - /// @returns true if this is a relational expression - bool IsRelational() const override { return true; } - - /// @returns true if the node is valid - bool IsValid() const override; - - /// Writes a representation of the node to the output stream - /// @param out the stream to write to - /// @param indent number of spaces to indent the node when writing - void to_str(std::ostream& out, size_t indent) const override; - - private: - RelationalExpression(const RelationalExpression&) = delete; - - Relation relation_ = Relation::kNone; - std::unique_ptr<Expression> lhs_; - std::unique_ptr<Expression> rhs_; -}; - -inline std::ostream& operator<<(std::ostream& out, Relation relation) { - switch (relation) { - case Relation::kNone: - out << "none"; - break; - case Relation::kAnd: - out << "and"; - break; - case Relation::kOr: - out << "or"; - break; - case Relation::kXor: - out << "xor"; - break; - case Relation::kLogicalAnd: - out << "logical_and"; - break; - case Relation::kLogicalOr: - out << "logical_or"; - break; - case Relation::kEqual: - out << "equal"; - break; - case Relation::kNotEqual: - out << "not_equal"; - break; - case Relation::kLessThan: - out << "less_than"; - break; - case Relation::kGreaterThan: - out << "greater_than"; - break; - case Relation::kLessThanEqual: - out << "less_than_equal"; - break; - case Relation::kGreaterThanEqual: - out << "greater_than_equal"; - break; - case Relation::kShiftLeft: - out << "shift_left"; - break; - case Relation::kShiftRight: - out << "shift_right"; - break; - case Relation::kShiftRightArith: - out << "shift_right_arith"; - break; - case Relation::kAdd: - out << "add"; - break; - case Relation::kSubtract: - out << "subtract"; - break; - case Relation::kMultiply: - out << "multiply"; - break; - case Relation::kDivide: - out << "divide"; - break; - case Relation::kModulo: - out << "modulo"; - break; - } - return out; -} - -} // namespace ast -} // namespace tint - -#endif // SRC_AST_RELATIONAL_EXPRESSION_H_
diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index dcc7229..5f86f56 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc
@@ -19,6 +19,7 @@ #include "src/ast/array_accessor_expression.h" #include "src/ast/as_expression.h" +#include "src/ast/binary_expression.h" #include "src/ast/binding_decoration.h" #include "src/ast/bool_literal.h" #include "src/ast/break_statement.h" @@ -38,7 +39,6 @@ #include "src/ast/location_decoration.h" #include "src/ast/member_accessor_expression.h" #include "src/ast/nop_statement.h" -#include "src/ast/relational_expression.h" #include "src/ast/return_statement.h" #include "src/ast/scalar_constructor_expression.h" #include "src/ast/set_decoration.h" @@ -2633,13 +2633,13 @@ std::unique_ptr<ast::Expression> lhs) { auto t = peek(); - ast::Relation relation = ast::Relation::kNone; + ast::BinaryOp op = ast::BinaryOp::kNone; if (t.IsStar()) - relation = ast::Relation::kMultiply; + op = ast::BinaryOp::kMultiply; else if (t.IsForwardSlash()) - relation = ast::Relation::kDivide; + op = ast::BinaryOp::kDivide; else if (t.IsMod()) - relation = ast::Relation::kModulo; + op = ast::BinaryOp::kModulo; else return lhs; @@ -2654,8 +2654,8 @@ set_error(peek(), "unable to parse right side of " + name + " expression"); return nullptr; } - return multiplicative_expr(std::make_unique<ast::RelationalExpression>( - source, relation, std::move(lhs), std::move(rhs))); + return multiplicative_expr(std::make_unique<ast::BinaryExpression>( + source, op, std::move(lhs), std::move(rhs))); } // multiplicative_expression @@ -2678,11 +2678,11 @@ std::unique_ptr<ast::Expression> lhs) { auto t = peek(); - ast::Relation relation = ast::Relation::kNone; + ast::BinaryOp op = ast::BinaryOp::kNone; if (t.IsPlus()) - relation = ast::Relation::kAdd; + op = ast::BinaryOp::kAdd; else if (t.IsMinus()) - relation = ast::Relation::kSubtract; + op = ast::BinaryOp::kSubtract; else return lhs; @@ -2696,8 +2696,8 @@ set_error(peek(), "unable to parse right side of + expression"); return nullptr; } - return additive_expr(std::make_unique<ast::RelationalExpression>( - source, relation, std::move(lhs), std::move(rhs))); + return additive_expr(std::make_unique<ast::BinaryExpression>( + source, op, std::move(lhs), std::move(rhs))); } // additive_expression @@ -2725,22 +2725,22 @@ auto t3 = peek(2); auto name = ""; - ast::Relation relation = ast::Relation::kNone; + ast::BinaryOp op = ast::BinaryOp::kNone; if (t.IsLessThan() && t2.IsLessThan()) { next(); // Consume the t peek next(); // Consume the t2 peek - relation = ast::Relation::kShiftLeft; + op = ast::BinaryOp::kShiftLeft; name = "<<"; } else if (t.IsGreaterThan() && t2.IsGreaterThan() && t3.IsGreaterThan()) { next(); // Consume the t peek next(); // Consume the t2 peek next(); // Consume the t3 peek - relation = ast::Relation::kShiftRightArith; + op = ast::BinaryOp::kShiftRightArith; name = ">>>"; } else if (t.IsGreaterThan() && t2.IsGreaterThan()) { next(); // Consume the t peek next(); // Consume the t2 peek - relation = ast::Relation::kShiftRight; + op = ast::BinaryOp::kShiftRight; name = ">>"; } else { return lhs; @@ -2754,8 +2754,8 @@ " expression"); return nullptr; } - return shift_expr(std::make_unique<ast::RelationalExpression>( - source, relation, std::move(lhs), std::move(rhs))); + return shift_expr(std::make_unique<ast::BinaryExpression>( + source, op, std::move(lhs), std::move(rhs))); } // shift_expression @@ -2779,15 +2779,15 @@ std::unique_ptr<ast::Expression> ParserImpl::relational_expr( std::unique_ptr<ast::Expression> lhs) { auto t = peek(); - ast::Relation relation = ast::Relation::kNone; + ast::BinaryOp op = ast::BinaryOp::kNone; if (t.IsLessThan()) - relation = ast::Relation::kLessThan; + op = ast::BinaryOp::kLessThan; else if (t.IsGreaterThan()) - relation = ast::Relation::kGreaterThan; + op = ast::BinaryOp::kGreaterThan; else if (t.IsLessThanEqual()) - relation = ast::Relation::kLessThanEqual; + op = ast::BinaryOp::kLessThanEqual; else if (t.IsGreaterThanEqual()) - relation = ast::Relation::kGreaterThanEqual; + op = ast::BinaryOp::kGreaterThanEqual; else return lhs; @@ -2802,8 +2802,8 @@ set_error(peek(), "unable to parse right side of " + name + " expression"); return nullptr; } - return relational_expr(std::make_unique<ast::RelationalExpression>( - source, relation, std::move(lhs), std::move(rhs))); + return relational_expr(std::make_unique<ast::BinaryExpression>( + source, op, std::move(lhs), std::move(rhs))); } // relational_expression @@ -2825,11 +2825,11 @@ std::unique_ptr<ast::Expression> ParserImpl::equality_expr( std::unique_ptr<ast::Expression> lhs) { auto t = peek(); - ast::Relation relation = ast::Relation::kNone; + ast::BinaryOp op = ast::BinaryOp::kNone; if (t.IsEqualEqual()) - relation = ast::Relation::kEqual; + op = ast::BinaryOp::kEqual; else if (t.IsNotEqual()) - relation = ast::Relation::kNotEqual; + op = ast::BinaryOp::kNotEqual; else return lhs; @@ -2844,8 +2844,8 @@ set_error(peek(), "unable to parse right side of " + name + " expression"); return nullptr; } - return equality_expr(std::make_unique<ast::RelationalExpression>( - source, relation, std::move(lhs), std::move(rhs))); + return equality_expr(std::make_unique<ast::BinaryExpression>( + source, op, std::move(lhs), std::move(rhs))); } // equality_expression @@ -2879,8 +2879,8 @@ set_error(peek(), "unable to parse right side of & expression"); return nullptr; } - return and_expr(std::make_unique<ast::RelationalExpression>( - source, ast::Relation::kAnd, std::move(lhs), std::move(rhs))); + return and_expr(std::make_unique<ast::BinaryExpression>( + source, ast::BinaryOp::kAnd, std::move(lhs), std::move(rhs))); } // and_expression @@ -2914,8 +2914,8 @@ set_error(peek(), "unable to parse right side of ^ expression"); return nullptr; } - return exclusive_or_expr(std::make_unique<ast::RelationalExpression>( - source, ast::Relation::kXor, std::move(lhs), std::move(rhs))); + return exclusive_or_expr(std::make_unique<ast::BinaryExpression>( + source, ast::BinaryOp::kXor, std::move(lhs), std::move(rhs))); } // exclusive_or_expression @@ -2949,8 +2949,8 @@ set_error(peek(), "unable to parse right side of | expression"); return nullptr; } - return inclusive_or_expr(std::make_unique<ast::RelationalExpression>( - source, ast::Relation::kOr, std::move(lhs), std::move(rhs))); + return inclusive_or_expr(std::make_unique<ast::BinaryExpression>( + source, ast::BinaryOp::kOr, std::move(lhs), std::move(rhs))); } // inclusive_or_expression @@ -2984,8 +2984,8 @@ set_error(peek(), "unable to parse right side of && expression"); return nullptr; } - return logical_and_expr(std::make_unique<ast::RelationalExpression>( - source, ast::Relation::kLogicalAnd, std::move(lhs), std::move(rhs))); + return logical_and_expr(std::make_unique<ast::BinaryExpression>( + source, ast::BinaryOp::kLogicalAnd, std::move(lhs), std::move(rhs))); } // logical_and_expression @@ -3019,8 +3019,8 @@ set_error(peek(), "unable to parse right side of || expression"); return nullptr; } - return logical_or_expr(std::make_unique<ast::RelationalExpression>( - source, ast::Relation::kLogicalOr, std::move(lhs), std::move(rhs))); + return logical_or_expr(std::make_unique<ast::BinaryExpression>( + source, ast::BinaryOp::kLogicalOr, std::move(lhs), std::move(rhs))); } // logical_or_expression
diff --git a/src/reader/wgsl/parser_impl_additive_expression_test.cc b/src/reader/wgsl/parser_impl_additive_expression_test.cc index db4b4a2..38d92ea 100644 --- a/src/reader/wgsl/parser_impl_additive_expression_test.cc +++ b/src/reader/wgsl/parser_impl_additive_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kAdd, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kAdd, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier(); @@ -53,9 +53,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kSubtract, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kSubtract, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_and_expression_test.cc b/src/reader/wgsl/parser_impl_and_expression_test.cc index 4f55afe..49ef84a 100644 --- a/src/reader/wgsl/parser_impl_and_expression_test.cc +++ b/src/reader/wgsl/parser_impl_and_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kAnd, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kAnd, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_argument_expression_list_test.cc b/src/reader/wgsl/parser_impl_argument_expression_list_test.cc index 3155ee8..650ff96 100644 --- a/src/reader/wgsl/parser_impl_argument_expression_list_test.cc +++ b/src/reader/wgsl/parser_impl_argument_expression_list_test.cc
@@ -45,7 +45,7 @@ ASSERT_EQ(e.size(), 3); ASSERT_TRUE(e[0]->IsIdentifier()); ASSERT_TRUE(e[1]->IsConstructor()); - ASSERT_TRUE(e[2]->IsRelational()); + ASSERT_TRUE(e[2]->IsBinary()); } TEST_F(ParserImplTest, ArgumentExpressionList_HandlesMissingExpression) {
diff --git a/src/reader/wgsl/parser_impl_break_stmt_test.cc b/src/reader/wgsl/parser_impl_break_stmt_test.cc index df1b901..688c5b8 100644 --- a/src/reader/wgsl/parser_impl_break_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_break_stmt_test.cc
@@ -42,7 +42,7 @@ ASSERT_TRUE(e->IsBreak()); EXPECT_EQ(e->condition(), ast::StatementCondition::kIf); ASSERT_NE(e->conditional(), nullptr); - EXPECT_TRUE(e->conditional()->IsRelational()); + EXPECT_TRUE(e->conditional()->IsBinary()); } TEST_F(ParserImplTest, BreakStmt_WithUnless) { @@ -53,7 +53,7 @@ ASSERT_TRUE(e->IsBreak()); EXPECT_EQ(e->condition(), ast::StatementCondition::kUnless); ASSERT_NE(e->conditional(), nullptr); - EXPECT_TRUE(e->conditional()->IsRelational()); + EXPECT_TRUE(e->conditional()->IsBinary()); } TEST_F(ParserImplTest, BreakStmt_InvalidRHS) {
diff --git a/src/reader/wgsl/parser_impl_continue_stmt_test.cc b/src/reader/wgsl/parser_impl_continue_stmt_test.cc index db2171d..f1e05c2 100644 --- a/src/reader/wgsl/parser_impl_continue_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_continue_stmt_test.cc
@@ -42,7 +42,7 @@ ASSERT_TRUE(e->IsContinue()); EXPECT_EQ(e->condition(), ast::StatementCondition::kIf); ASSERT_NE(e->conditional(), nullptr); - EXPECT_TRUE(e->conditional()->IsRelational()); + EXPECT_TRUE(e->conditional()->IsBinary()); } TEST_F(ParserImplTest, ContinueStmt_WithUnless) { @@ -53,7 +53,7 @@ ASSERT_TRUE(e->IsContinue()); EXPECT_EQ(e->condition(), ast::StatementCondition::kUnless); ASSERT_NE(e->conditional(), nullptr); - EXPECT_TRUE(e->conditional()->IsRelational()); + EXPECT_TRUE(e->conditional()->IsBinary()); } TEST_F(ParserImplTest, ContinueStmt_InvalidRHS) {
diff --git a/src/reader/wgsl/parser_impl_elseif_stmt_test.cc b/src/reader/wgsl/parser_impl_elseif_stmt_test.cc index e5b5f52..147b3d4 100644 --- a/src/reader/wgsl/parser_impl_elseif_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_elseif_stmt_test.cc
@@ -30,7 +30,7 @@ ASSERT_TRUE(e[0]->IsElse()); ASSERT_NE(e[0]->condition(), nullptr); - ASSERT_TRUE(e[0]->condition()->IsRelational()); + ASSERT_TRUE(e[0]->condition()->IsBinary()); EXPECT_EQ(e[0]->body().size(), 2); } @@ -42,7 +42,7 @@ ASSERT_TRUE(e[0]->IsElse()); ASSERT_NE(e[0]->condition(), nullptr); - ASSERT_TRUE(e[0]->condition()->IsRelational()); + ASSERT_TRUE(e[0]->condition()->IsBinary()); EXPECT_EQ(e[0]->body().size(), 2); ASSERT_TRUE(e[1]->IsElse());
diff --git a/src/reader/wgsl/parser_impl_equality_expression_test.cc b/src/reader/wgsl/parser_impl_equality_expression_test.cc index 7193229..39a83b6 100644 --- a/src/reader/wgsl/parser_impl_equality_expression_test.cc +++ b/src/reader/wgsl/parser_impl_equality_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kEqual, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kEqual, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier(); @@ -53,9 +53,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kNotEqual, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kNotEqual, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc b/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc index 626a9ac..bb4d21b 100644 --- a/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc +++ b/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kXor, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kXor, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_if_stmt_test.cc b/src/reader/wgsl/parser_impl_if_stmt_test.cc index 43dcecc..40a6661 100644 --- a/src/reader/wgsl/parser_impl_if_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_if_stmt_test.cc
@@ -31,7 +31,7 @@ ASSERT_TRUE(e->IsIf()); ASSERT_NE(e->condition(), nullptr); - ASSERT_TRUE(e->condition()->IsRelational()); + ASSERT_TRUE(e->condition()->IsBinary()); EXPECT_EQ(e->body().size(), 2); EXPECT_EQ(e->else_statements().size(), 0); EXPECT_EQ(e->premerge().size(), 0); @@ -45,7 +45,7 @@ ASSERT_TRUE(e->IsIf()); ASSERT_NE(e->condition(), nullptr); - ASSERT_TRUE(e->condition()->IsRelational()); + ASSERT_TRUE(e->condition()->IsBinary()); EXPECT_EQ(e->body().size(), 2); ASSERT_EQ(e->else_statements().size(), 2); @@ -72,7 +72,7 @@ ASSERT_TRUE(e->IsIf()); ASSERT_NE(e->condition(), nullptr); - ASSERT_TRUE(e->condition()->IsRelational()); + ASSERT_TRUE(e->condition()->IsBinary()); EXPECT_EQ(e->body().size(), 2); ASSERT_EQ(e->else_statements().size(), 1);
diff --git a/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc b/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc index 4bd9c44..bbbe778 100644 --- a/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc +++ b/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kOr, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kOr, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_logical_and_expression_test.cc b/src/reader/wgsl/parser_impl_logical_and_expression_test.cc index 9483649..9c7029e 100644 --- a/src/reader/wgsl/parser_impl_logical_and_expression_test.cc +++ b/src/reader/wgsl/parser_impl_logical_and_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kLogicalAnd, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kLogicalAnd, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_logical_or_expression_test.cc b/src/reader/wgsl/parser_impl_logical_or_expression_test.cc index 3fb303a..bc19f61 100644 --- a/src/reader/wgsl/parser_impl_logical_or_expression_test.cc +++ b/src/reader/wgsl/parser_impl_logical_or_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kLogicalOr, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kLogicalOr, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc b/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc index 9d6e2f7..cba8fc1 100644 --- a/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc +++ b/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kMultiply, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kMultiply, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier(); @@ -53,9 +53,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kDivide, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kDivide, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier(); @@ -75,9 +75,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kModulo, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kModulo, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_paren_rhs_stmt_test.cc b/src/reader/wgsl/parser_impl_paren_rhs_stmt_test.cc index 2955815..fde1394 100644 --- a/src/reader/wgsl/parser_impl_paren_rhs_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_paren_rhs_stmt_test.cc
@@ -26,7 +26,7 @@ auto e = p->paren_rhs_stmt(); ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); + ASSERT_TRUE(e->IsBinary()); } TEST_F(ParserImplTest, ParenRhsStmt_MissingLeftParen) {
diff --git a/src/reader/wgsl/parser_impl_postfix_expression_test.cc b/src/reader/wgsl/parser_impl_postfix_expression_test.cc index a9c63e9..66380cb 100644 --- a/src/reader/wgsl/parser_impl_postfix_expression_test.cc +++ b/src/reader/wgsl/parser_impl_postfix_expression_test.cc
@@ -65,7 +65,7 @@ ASSERT_EQ(ident->name().size(), 1); EXPECT_EQ(ident->name()[0], "a"); - ASSERT_TRUE(ary->idx_expr()->IsRelational()); + ASSERT_TRUE(ary->idx_expr()->IsBinary()); } TEST_F(ParserImplTest, PostfixExpression_Array_MissingIndex) { @@ -127,7 +127,7 @@ EXPECT_EQ(c->params().size(), 3); EXPECT_TRUE(c->params()[0]->IsConstructor()); EXPECT_TRUE(c->params()[1]->IsIdentifier()); - EXPECT_TRUE(c->params()[2]->IsRelational()); + EXPECT_TRUE(c->params()[2]->IsBinary()); } TEST_F(ParserImplTest, PostfixExpression_Call_InvalidArg) {
diff --git a/src/reader/wgsl/parser_impl_primary_expression_test.cc b/src/reader/wgsl/parser_impl_primary_expression_test.cc index 0ce6138..ae0c9c3 100644 --- a/src/reader/wgsl/parser_impl_primary_expression_test.cc +++ b/src/reader/wgsl/parser_impl_primary_expression_test.cc
@@ -153,7 +153,7 @@ auto e = p->primary_expression(); ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); + ASSERT_TRUE(e->IsBinary()); } TEST_F(ParserImplTest, PrimaryExpression_ParenExpr_MissingRightParen) {
diff --git a/src/reader/wgsl/parser_impl_relational_expression_test.cc b/src/reader/wgsl/parser_impl_relational_expression_test.cc index 5673535..1b4ab29 100644 --- a/src/reader/wgsl/parser_impl_relational_expression_test.cc +++ b/src/reader/wgsl/parser_impl_relational_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kLessThan, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kLessThan, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier(); @@ -53,9 +53,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kGreaterThan, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kGreaterThan, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier(); @@ -75,9 +75,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kLessThanEqual, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kLessThanEqual, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier(); @@ -97,9 +97,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kGreaterThanEqual, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kGreaterThanEqual, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_shift_expression_test.cc b/src/reader/wgsl/parser_impl_shift_expression_test.cc index 76f5270..60c99ad 100644 --- a/src/reader/wgsl/parser_impl_shift_expression_test.cc +++ b/src/reader/wgsl/parser_impl_shift_expression_test.cc
@@ -13,9 +13,9 @@ // limitations under the License. #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/reader/wgsl/parser_impl.h" #include "src/reader/wgsl/parser_impl_test_helper.h" @@ -31,9 +31,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kShiftLeft, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kShiftLeft, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier(); @@ -53,9 +53,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kShiftRight, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kShiftRight, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier(); @@ -75,9 +75,9 @@ ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e, nullptr); - ASSERT_TRUE(e->IsRelational()); - auto rel = e->AsRelational(); - EXPECT_EQ(ast::Relation::kShiftRightArith, rel->relation()); + ASSERT_TRUE(e->IsBinary()); + auto rel = e->AsBinary(); + EXPECT_EQ(ast::BinaryOp::kShiftRightArith, rel->op()); ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto ident = rel->lhs()->AsIdentifier();
diff --git a/src/reader/wgsl/parser_impl_statement_test.cc b/src/reader/wgsl/parser_impl_statement_test.cc index bc047f3..25a1017 100644 --- a/src/reader/wgsl/parser_impl_statement_test.cc +++ b/src/reader/wgsl/parser_impl_statement_test.cc
@@ -58,7 +58,7 @@ ASSERT_TRUE(e->IsReturn()); auto ret = e->AsReturn(); ASSERT_NE(ret->value(), nullptr); - EXPECT_TRUE(ret->value()->IsRelational()); + EXPECT_TRUE(ret->value()->IsBinary()); } TEST_F(ParserImplTest, Statement_Return_MissingSemi) {
diff --git a/src/type_determiner.cc b/src/type_determiner.cc index abb1c9f..aa78f04 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc
@@ -19,6 +19,7 @@ #include "src/ast/array_accessor_expression.h" #include "src/ast/as_expression.h" #include "src/ast/assignment_statement.h" +#include "src/ast/binary_expression.h" #include "src/ast/break_statement.h" #include "src/ast/call_expression.h" #include "src/ast/case_statement.h" @@ -30,7 +31,6 @@ #include "src/ast/loop_statement.h" #include "src/ast/member_accessor_expression.h" #include "src/ast/regardless_statement.h" -#include "src/ast/relational_expression.h" #include "src/ast/return_statement.h" #include "src/ast/scalar_constructor_expression.h" #include "src/ast/switch_statement.h" @@ -197,6 +197,9 @@ if (expr->IsAs()) { return DetermineAs(expr->AsAs()); } + if (expr->IsBinary()) { + return DetermineBinary(expr->AsBinary()); + } if (expr->IsCall()) { return DetermineCall(expr->AsCall()); } @@ -212,9 +215,6 @@ if (expr->IsMemberAccessor()) { return DetermineMemberAccessor(expr->AsMemberAccessor()); } - if (expr->IsRelational()) { - return DetermineRelational(expr->AsRelational()); - } if (expr->IsUnaryDerivative()) { return DetermineUnaryDerivative(expr->AsUnaryDerivative()); } @@ -339,7 +339,7 @@ return false; } -bool TypeDeterminer::DetermineRelational(ast::RelationalExpression* expr) { +bool TypeDeterminer::DetermineBinary(ast::BinaryExpression* expr) { if (!DetermineResultType(expr->lhs()) || !DetermineResultType(expr->rhs())) { return false; }
diff --git a/src/type_determiner.h b/src/type_determiner.h index 0b72b59..b61799b 100644 --- a/src/type_determiner.h +++ b/src/type_determiner.h
@@ -27,13 +27,13 @@ class ArrayAccessorExpression; class AsExpression; +class BinaryExpression; class CallExpression; class CastExpression; class ConstructorExpression; class Function; class IdentifierExpression; class MemberAccessorExpression; -class RelationalExpression; class UnaryDerivativeExpression; class UnaryMethodExpression; class UnaryOpExpression; @@ -80,12 +80,12 @@ private: bool DetermineArrayAccessor(ast::ArrayAccessorExpression* expr); bool DetermineAs(ast::AsExpression* expr); + bool DetermineBinary(ast::BinaryExpression* expr); bool DetermineCall(ast::CallExpression* expr); bool DetermineCast(ast::CastExpression* expr); bool DetermineConstructor(ast::ConstructorExpression* expr); bool DetermineIdentifier(ast::IdentifierExpression* expr); bool DetermineMemberAccessor(ast::MemberAccessorExpression* expr); - bool DetermineRelational(ast::RelationalExpression* expr); bool DetermineUnaryDerivative(ast::UnaryDerivativeExpression* expr); bool DetermineUnaryMethod(ast::UnaryMethodExpression* expr); bool DetermineUnaryOp(ast::UnaryOpExpression* expr);
diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc index 8275406..dcdd657 100644 --- a/src/type_determiner_test.cc +++ b/src/type_determiner_test.cc
@@ -22,6 +22,7 @@ #include "src/ast/array_accessor_expression.h" #include "src/ast/as_expression.h" #include "src/ast/assignment_statement.h" +#include "src/ast/binary_expression.h" #include "src/ast/break_statement.h" #include "src/ast/call_expression.h" #include "src/ast/case_statement.h" @@ -35,7 +36,6 @@ #include "src/ast/loop_statement.h" #include "src/ast/member_accessor_expression.h" #include "src/ast/regardless_statement.h" -#include "src/ast/relational_expression.h" #include "src/ast/return_statement.h" #include "src/ast/scalar_constructor_expression.h" #include "src/ast/struct.h" @@ -778,8 +778,8 @@ EXPECT_EQ(mem.result_type()->AsVector()->size(), 2); } -using Expr_Relational_BitwiseTest = testing::TestWithParam<ast::Relation>; -TEST_P(Expr_Relational_BitwiseTest, Scalar) { +using Expr_Binary_BitwiseTest = testing::TestWithParam<ast::BinaryOp>; +TEST_P(Expr_Binary_BitwiseTest, Scalar) { auto op = GetParam(); ast::type::I32Type i32; @@ -796,7 +796,7 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( + ast::BinaryExpression expr( op, std::make_unique<ast::IdentifierExpression>("val"), std::make_unique<ast::IdentifierExpression>("val")); @@ -805,7 +805,7 @@ EXPECT_TRUE(expr.result_type()->IsI32()); } -TEST_P(Expr_Relational_BitwiseTest, Vector) { +TEST_P(Expr_Binary_BitwiseTest, Vector) { auto op = GetParam(); ast::type::I32Type i32; @@ -823,7 +823,7 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( + ast::BinaryExpression expr( op, std::make_unique<ast::IdentifierExpression>("val"), std::make_unique<ast::IdentifierExpression>("val")); @@ -834,20 +834,20 @@ EXPECT_EQ(expr.result_type()->AsVector()->size(), 3); } INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, - Expr_Relational_BitwiseTest, - testing::Values(ast::Relation::kAnd, - ast::Relation::kOr, - ast::Relation::kXor, - ast::Relation::kShiftLeft, - ast::Relation::kShiftRight, - ast::Relation::kShiftRightArith, - ast::Relation::kAdd, - ast::Relation::kSubtract, - ast::Relation::kDivide, - ast::Relation::kModulo)); + Expr_Binary_BitwiseTest, + testing::Values(ast::BinaryOp::kAnd, + ast::BinaryOp::kOr, + ast::BinaryOp::kXor, + ast::BinaryOp::kShiftLeft, + ast::BinaryOp::kShiftRight, + ast::BinaryOp::kShiftRightArith, + ast::BinaryOp::kAdd, + ast::BinaryOp::kSubtract, + ast::BinaryOp::kDivide, + ast::BinaryOp::kModulo)); -using Expr_Relational_LogicalTest = testing::TestWithParam<ast::Relation>; -TEST_P(Expr_Relational_LogicalTest, Scalar) { +using Expr_Binary_LogicalTest = testing::TestWithParam<ast::BinaryOp>; +TEST_P(Expr_Binary_LogicalTest, Scalar) { auto op = GetParam(); ast::type::BoolType bool_type; @@ -864,7 +864,7 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( + ast::BinaryExpression expr( op, std::make_unique<ast::IdentifierExpression>("val"), std::make_unique<ast::IdentifierExpression>("val")); @@ -873,7 +873,7 @@ EXPECT_TRUE(expr.result_type()->IsBool()); } -TEST_P(Expr_Relational_LogicalTest, Vector) { +TEST_P(Expr_Binary_LogicalTest, Vector) { auto op = GetParam(); ast::type::BoolType bool_type; @@ -891,7 +891,7 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( + ast::BinaryExpression expr( op, std::make_unique<ast::IdentifierExpression>("val"), std::make_unique<ast::IdentifierExpression>("val")); @@ -902,12 +902,12 @@ EXPECT_EQ(expr.result_type()->AsVector()->size(), 3); } INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, - Expr_Relational_LogicalTest, - testing::Values(ast::Relation::kLogicalAnd, - ast::Relation::kLogicalOr)); + Expr_Binary_LogicalTest, + testing::Values(ast::BinaryOp::kLogicalAnd, + ast::BinaryOp::kLogicalOr)); -using Expr_Relational_CompareTest = testing::TestWithParam<ast::Relation>; -TEST_P(Expr_Relational_CompareTest, Scalar) { +using Expr_Binary_CompareTest = testing::TestWithParam<ast::BinaryOp>; +TEST_P(Expr_Binary_CompareTest, Scalar) { auto op = GetParam(); ast::type::I32Type i32; @@ -924,7 +924,7 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( + ast::BinaryExpression expr( op, std::make_unique<ast::IdentifierExpression>("val"), std::make_unique<ast::IdentifierExpression>("val")); @@ -933,7 +933,7 @@ EXPECT_TRUE(expr.result_type()->IsBool()); } -TEST_P(Expr_Relational_CompareTest, Vector) { +TEST_P(Expr_Binary_CompareTest, Vector) { auto op = GetParam(); ast::type::I32Type i32; @@ -951,7 +951,7 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( + ast::BinaryExpression expr( op, std::make_unique<ast::IdentifierExpression>("val"), std::make_unique<ast::IdentifierExpression>("val")); @@ -962,15 +962,15 @@ EXPECT_EQ(expr.result_type()->AsVector()->size(), 3); } INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, - Expr_Relational_CompareTest, - testing::Values(ast::Relation::kEqual, - ast::Relation::kNotEqual, - ast::Relation::kLessThan, - ast::Relation::kGreaterThan, - ast::Relation::kLessThanEqual, - ast::Relation::kGreaterThanEqual)); + Expr_Binary_CompareTest, + testing::Values(ast::BinaryOp::kEqual, + ast::BinaryOp::kNotEqual, + ast::BinaryOp::kLessThan, + ast::BinaryOp::kGreaterThan, + ast::BinaryOp::kLessThanEqual, + ast::BinaryOp::kGreaterThanEqual)); -TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Scalar_Scalar) { +TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Scalar) { ast::type::I32Type i32; auto var = @@ -985,8 +985,8 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( - ast::Relation::kMultiply, + ast::BinaryExpression expr( + ast::BinaryOp::kMultiply, std::make_unique<ast::IdentifierExpression>("val"), std::make_unique<ast::IdentifierExpression>("val")); @@ -995,7 +995,7 @@ EXPECT_TRUE(expr.result_type()->IsI32()); } -TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Vector_Scalar) { +TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Scalar) { ast::type::F32Type f32; ast::type::VectorType vec3(&f32, 3); @@ -1014,8 +1014,8 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( - ast::Relation::kMultiply, + ast::BinaryExpression expr( + ast::BinaryOp::kMultiply, std::make_unique<ast::IdentifierExpression>("vector"), std::make_unique<ast::IdentifierExpression>("scalar")); @@ -1026,7 +1026,7 @@ EXPECT_EQ(expr.result_type()->AsVector()->size(), 3); } -TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Scalar_Vector) { +TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Vector) { ast::type::F32Type f32; ast::type::VectorType vec3(&f32, 3); @@ -1045,8 +1045,8 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( - ast::Relation::kMultiply, + ast::BinaryExpression expr( + ast::BinaryOp::kMultiply, std::make_unique<ast::IdentifierExpression>("scalar"), std::make_unique<ast::IdentifierExpression>("vector")); @@ -1057,7 +1057,7 @@ EXPECT_EQ(expr.result_type()->AsVector()->size(), 3); } -TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Vector_Vector) { +TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Vector) { ast::type::F32Type f32; ast::type::VectorType vec3(&f32, 3); @@ -1073,8 +1073,8 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( - ast::Relation::kMultiply, + ast::BinaryExpression expr( + ast::BinaryOp::kMultiply, std::make_unique<ast::IdentifierExpression>("vector"), std::make_unique<ast::IdentifierExpression>("vector")); @@ -1085,7 +1085,7 @@ EXPECT_EQ(expr.result_type()->AsVector()->size(), 3); } -TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Matrix_Scalar) { +TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Scalar) { ast::type::F32Type f32; ast::type::MatrixType mat3x2(&f32, 3, 2); @@ -1104,8 +1104,8 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( - ast::Relation::kMultiply, + ast::BinaryExpression expr( + ast::BinaryOp::kMultiply, std::make_unique<ast::IdentifierExpression>("matrix"), std::make_unique<ast::IdentifierExpression>("scalar")); @@ -1119,7 +1119,7 @@ EXPECT_EQ(mat->columns(), 2); } -TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Scalar_Matrix) { +TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Matrix) { ast::type::F32Type f32; ast::type::MatrixType mat3x2(&f32, 3, 2); @@ -1138,8 +1138,8 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( - ast::Relation::kMultiply, + ast::BinaryExpression expr( + ast::BinaryOp::kMultiply, std::make_unique<ast::IdentifierExpression>("scalar"), std::make_unique<ast::IdentifierExpression>("matrix")); @@ -1153,7 +1153,7 @@ EXPECT_EQ(mat->columns(), 2); } -TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Matrix_Vector) { +TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Vector) { ast::type::F32Type f32; ast::type::VectorType vec3(&f32, 2); ast::type::MatrixType mat3x2(&f32, 3, 2); @@ -1173,8 +1173,8 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( - ast::Relation::kMultiply, + ast::BinaryExpression expr( + ast::BinaryOp::kMultiply, std::make_unique<ast::IdentifierExpression>("matrix"), std::make_unique<ast::IdentifierExpression>("vector")); @@ -1185,7 +1185,7 @@ EXPECT_EQ(expr.result_type()->AsVector()->size(), 3); } -TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Vector_Matrix) { +TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Matrix) { ast::type::F32Type f32; ast::type::VectorType vec3(&f32, 3); ast::type::MatrixType mat3x2(&f32, 3, 2); @@ -1205,8 +1205,8 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( - ast::Relation::kMultiply, + ast::BinaryExpression expr( + ast::BinaryOp::kMultiply, std::make_unique<ast::IdentifierExpression>("vector"), std::make_unique<ast::IdentifierExpression>("matrix")); @@ -1217,7 +1217,7 @@ EXPECT_EQ(expr.result_type()->AsVector()->size(), 2); } -TEST_F(TypeDeterminerTest, Expr_Relational_Multiply_Matrix_Matrix) { +TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Matrix) { ast::type::F32Type f32; ast::type::MatrixType mat4x3(&f32, 4, 3); ast::type::MatrixType mat3x4(&f32, 3, 4); @@ -1237,8 +1237,8 @@ // Register the global ASSERT_TRUE(td.Determine(&m)) << td.error(); - ast::RelationalExpression expr( - ast::Relation::kMultiply, + ast::BinaryExpression expr( + ast::BinaryOp::kMultiply, std::make_unique<ast::IdentifierExpression>("mat4x3"), std::make_unique<ast::IdentifierExpression>("mat3x4"));
diff --git a/src/writer/spirv/builder_constructor_expression_test.cc b/src/writer/spirv/builder_constructor_expression_test.cc index 8e8d86e..49cb27d 100644 --- a/src/writer/spirv/builder_constructor_expression_test.cc +++ b/src/writer/spirv/builder_constructor_expression_test.cc
@@ -17,8 +17,8 @@ #include "gtest/gtest.h" #include "spirv/unified1/spirv.h" #include "spirv/unified1/spirv.hpp11" +#include "src/ast/binary_expression.h" #include "src/ast/float_literal.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/ast/type/f32_type.h" #include "src/ast/type/vector_type.h" @@ -96,8 +96,8 @@ TEST_F(BuilderTest, Constructor_NonConst_Type_Fails) { ast::type::F32Type f32; ast::type::VectorType vec(&f32, 2); - auto rel = std::make_unique<ast::RelationalExpression>( - ast::Relation::kAdd, + auto rel = std::make_unique<ast::BinaryExpression>( + ast::BinaryOp::kAdd, std::make_unique<ast::ScalarConstructorExpression>( std::make_unique<ast::FloatLiteral>(&f32, 3.0f)), std::make_unique<ast::ScalarConstructorExpression>(
diff --git a/src/writer/spirv/builder_function_variable_test.cc b/src/writer/spirv/builder_function_variable_test.cc index 108d585..286470d 100644 --- a/src/writer/spirv/builder_function_variable_test.cc +++ b/src/writer/spirv/builder_function_variable_test.cc
@@ -15,13 +15,13 @@ #include <memory> #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/binding_decoration.h" #include "src/ast/builtin.h" #include "src/ast/builtin_decoration.h" #include "src/ast/decorated_variable.h" #include "src/ast/float_literal.h" #include "src/ast/location_decoration.h" -#include "src/ast/relational_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/ast/set_decoration.h" #include "src/ast/storage_class.h" @@ -97,13 +97,13 @@ )"); } -// DISABLED until we have RelationalExpression Output +// DISABLED until we have BinaryExpression Output TEST_F(BuilderTest, DISABLED_FunctionVar_WithNonConstantConstructor) { ast::type::F32Type f32; ast::type::VectorType vec(&f32, 2); - auto rel = std::make_unique<ast::RelationalExpression>( - ast::Relation::kAdd, + auto rel = std::make_unique<ast::BinaryExpression>( + ast::BinaryOp::kAdd, std::make_unique<ast::ScalarConstructorExpression>( std::make_unique<ast::FloatLiteral>(&f32, 3.0f)), std::make_unique<ast::ScalarConstructorExpression>(
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index f16a358..9eb1b36 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc
@@ -20,6 +20,7 @@ #include "src/ast/array_accessor_expression.h" #include "src/ast/as_expression.h" #include "src/ast/assignment_statement.h" +#include "src/ast/binary_expression.h" #include "src/ast/binding_decoration.h" #include "src/ast/bool_literal.h" #include "src/ast/break_statement.h" @@ -39,7 +40,6 @@ #include "src/ast/loop_statement.h" #include "src/ast/member_accessor_expression.h" #include "src/ast/regardless_statement.h" -#include "src/ast/relational_expression.h" #include "src/ast/return_statement.h" #include "src/ast/scalar_constructor_expression.h" #include "src/ast/set_decoration.h" @@ -149,6 +149,9 @@ if (expr->IsAs()) { return EmitAs(expr->AsAs()); } + if (expr->IsBinary()) { + return EmitBinary(expr->AsBinary()); + } if (expr->IsCall()) { return EmitCall(expr->AsCall()); } @@ -164,9 +167,6 @@ if (expr->IsMemberAccessor()) { return EmitMemberAccessor(expr->AsMemberAccessor()); } - if (expr->IsRelational()) { - return EmitRelational(expr->AsRelational()); - } if (expr->IsUnaryDerivative()) { return EmitUnaryDerivative(expr->AsUnaryDerivative()); } @@ -518,7 +518,7 @@ return true; } -bool GeneratorImpl::EmitRelational(ast::RelationalExpression* expr) { +bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) { out_ << "("; if (!EmitExpression(expr->lhs())) { @@ -526,66 +526,66 @@ } out_ << " "; - switch (expr->relation()) { - case ast::Relation::kAnd: + switch (expr->op()) { + case ast::BinaryOp::kAnd: out_ << "&"; break; - case ast::Relation::kOr: + case ast::BinaryOp::kOr: out_ << "|"; break; - case ast::Relation::kXor: + case ast::BinaryOp::kXor: out_ << "^"; break; - case ast::Relation::kLogicalAnd: + case ast::BinaryOp::kLogicalAnd: out_ << "&&"; break; - case ast::Relation::kLogicalOr: + case ast::BinaryOp::kLogicalOr: out_ << "||"; break; - case ast::Relation::kEqual: + case ast::BinaryOp::kEqual: out_ << "=="; break; - case ast::Relation::kNotEqual: + case ast::BinaryOp::kNotEqual: out_ << "!="; break; - case ast::Relation::kLessThan: + case ast::BinaryOp::kLessThan: out_ << "<"; break; - case ast::Relation::kGreaterThan: + case ast::BinaryOp::kGreaterThan: out_ << ">"; break; - case ast::Relation::kLessThanEqual: + case ast::BinaryOp::kLessThanEqual: out_ << "<="; break; - case ast::Relation::kGreaterThanEqual: + case ast::BinaryOp::kGreaterThanEqual: out_ << ">="; break; - case ast::Relation::kShiftLeft: + case ast::BinaryOp::kShiftLeft: out_ << "<<"; break; - case ast::Relation::kShiftRight: + case ast::BinaryOp::kShiftRight: out_ << ">>"; break; - case ast::Relation::kShiftRightArith: + case ast::BinaryOp::kShiftRightArith: out_ << ">>>"; break; - case ast::Relation::kAdd: + case ast::BinaryOp::kAdd: out_ << "+"; break; - case ast::Relation::kSubtract: + case ast::BinaryOp::kSubtract: out_ << "-"; break; - case ast::Relation::kMultiply: + case ast::BinaryOp::kMultiply: out_ << "*"; break; - case ast::Relation::kDivide: + case ast::BinaryOp::kDivide: out_ << "/"; break; - case ast::Relation::kModulo: + case ast::BinaryOp::kModulo: out_ << "%"; break; - case ast::Relation::kNone: - error_ = "missing relation type"; + case ast::BinaryOp::kNone: + error_ = "missing binary operation type"; return false; } out_ << " ";
diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index dca64ad..532f154 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h
@@ -82,6 +82,10 @@ /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully bool EmitAssign(ast::AssignmentStatement* stmt); + /// Handles generating a binary expression + /// @param expr the binary expression + /// @returns true if the expression was emitted, false otherwise + bool EmitBinary(ast::BinaryExpression* expr); /// Handles a break statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully @@ -166,10 +170,6 @@ /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted bool EmitRegardless(ast::RegardlessStatement* stmt); - /// Handles generating a relational expression - /// @param expr the relational expression - /// @returns true if the expression was emitted, false otherwise - bool EmitRelational(ast::RelationalExpression* expr); /// Handles return statements /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted
diff --git a/src/writer/wgsl/generator_impl_relational_test.cc b/src/writer/wgsl/generator_impl_relational_test.cc index 27d7370..983b29c 100644 --- a/src/writer/wgsl/generator_impl_relational_test.cc +++ b/src/writer/wgsl/generator_impl_relational_test.cc
@@ -15,8 +15,8 @@ #include <memory> #include "gtest/gtest.h" +#include "src/ast/binary_expression.h" #include "src/ast/identifier_expression.h" -#include "src/ast/relational_expression.h" #include "src/writer/wgsl/generator_impl.h" namespace tint { @@ -24,23 +24,22 @@ namespace wgsl { namespace { -struct RelationData { +struct BinaryData { const char* result; - ast::Relation relation; + ast::BinaryOp op; }; -inline std::ostream& operator<<(std::ostream& out, RelationData data) { - out << data.relation; +inline std::ostream& operator<<(std::ostream& out, BinaryData data) { + out << data.op; return out; } -using RelationTest = testing::TestWithParam<RelationData>; -TEST_P(RelationTest, Emit) { +using BinaryTest = testing::TestWithParam<BinaryData>; +TEST_P(BinaryTest, Emit) { auto params = GetParam(); auto left = std::make_unique<ast::IdentifierExpression>("left"); auto right = std::make_unique<ast::IdentifierExpression>("right"); - ast::RelationalExpression expr(params.relation, std::move(left), - std::move(right)); + ast::BinaryExpression expr(params.op, std::move(left), std::move(right)); GeneratorImpl g; ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); @@ -48,27 +47,27 @@ } INSTANTIATE_TEST_SUITE_P( GeneratorImplTest, - RelationTest, + BinaryTest, testing::Values( - RelationData{"(left & right)", ast::Relation::kAnd}, - RelationData{"(left | right)", ast::Relation::kOr}, - RelationData{"(left ^ right)", ast::Relation::kXor}, - RelationData{"(left && right)", ast::Relation::kLogicalAnd}, - RelationData{"(left || right)", ast::Relation::kLogicalOr}, - RelationData{"(left == right)", ast::Relation::kEqual}, - RelationData{"(left != right)", ast::Relation::kNotEqual}, - RelationData{"(left < right)", ast::Relation::kLessThan}, - RelationData{"(left > right)", ast::Relation::kGreaterThan}, - RelationData{"(left <= right)", ast::Relation::kLessThanEqual}, - RelationData{"(left >= right)", ast::Relation::kGreaterThanEqual}, - RelationData{"(left << right)", ast::Relation::kShiftLeft}, - RelationData{"(left >> right)", ast::Relation::kShiftRight}, - RelationData{"(left >>> right)", ast::Relation::kShiftRightArith}, - RelationData{"(left + right)", ast::Relation::kAdd}, - RelationData{"(left - right)", ast::Relation::kSubtract}, - RelationData{"(left * right)", ast::Relation::kMultiply}, - RelationData{"(left / right)", ast::Relation::kDivide}, - RelationData{"(left % right)", ast::Relation::kModulo})); + BinaryData{"(left & right)", ast::BinaryOp::kAnd}, + BinaryData{"(left | right)", ast::BinaryOp::kOr}, + BinaryData{"(left ^ right)", ast::BinaryOp::kXor}, + BinaryData{"(left && right)", ast::BinaryOp::kLogicalAnd}, + BinaryData{"(left || right)", ast::BinaryOp::kLogicalOr}, + BinaryData{"(left == right)", ast::BinaryOp::kEqual}, + BinaryData{"(left != right)", ast::BinaryOp::kNotEqual}, + BinaryData{"(left < right)", ast::BinaryOp::kLessThan}, + BinaryData{"(left > right)", ast::BinaryOp::kGreaterThan}, + BinaryData{"(left <= right)", ast::BinaryOp::kLessThanEqual}, + BinaryData{"(left >= right)", ast::BinaryOp::kGreaterThanEqual}, + BinaryData{"(left << right)", ast::BinaryOp::kShiftLeft}, + BinaryData{"(left >> right)", ast::BinaryOp::kShiftRight}, + BinaryData{"(left >>> right)", ast::BinaryOp::kShiftRightArith}, + BinaryData{"(left + right)", ast::BinaryOp::kAdd}, + BinaryData{"(left - right)", ast::BinaryOp::kSubtract}, + BinaryData{"(left * right)", ast::BinaryOp::kMultiply}, + BinaryData{"(left / right)", ast::BinaryOp::kDivide}, + BinaryData{"(left % right)", ast::BinaryOp::kModulo})); } // namespace } // namespace wgsl