ast: Add a CompoundAssignmentStatement node

Bug: tint:1325
Change-Id: I6b5024fd81cd02119d9520e72ab4bdf14eafc3c2
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/85280
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
James Price 2022-03-31 22:30:10 +00:00
parent daea034bd1
commit 4924186bbd
6 changed files with 222 additions and 0 deletions

View File

@ -210,6 +210,8 @@ libtint_source_set("libtint_core_all_src") {
"ast/call_statement.h",
"ast/case_statement.cc",
"ast/case_statement.h",
"ast/compound_assignment_statement.cc",
"ast/compound_assignment_statement.h",
"ast/continue_statement.cc",
"ast/continue_statement.h",
"ast/depth_multisampled_texture.cc",

View File

@ -98,6 +98,8 @@ set(TINT_LIB_SRCS
ast/call_statement.h
ast/case_statement.cc
ast/case_statement.h
ast/compound_assignment_statement.cc
ast/compound_assignment_statement.h
ast/continue_statement.cc
ast/continue_statement.h
ast/depth_multisampled_texture.cc
@ -658,6 +660,7 @@ if(TINT_BUILD_TESTS)
ast/call_expression_test.cc
ast/call_statement_test.cc
ast/case_statement_test.cc
ast/compound_assignment_statement_test.cc
ast/continue_statement_test.cc
ast/depth_multisampled_texture_test.cc
ast/depth_texture_test.cc

View File

@ -0,0 +1,51 @@
// Copyright 2022 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/tint/ast/compound_assignment_statement.h"
#include "src/tint/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::ast::CompoundAssignmentStatement);
namespace tint {
namespace ast {
CompoundAssignmentStatement::CompoundAssignmentStatement(ProgramID pid,
const Source& src,
const Expression* l,
const Expression* r,
BinaryOp o)
: Base(pid, src), lhs(l), rhs(r), op(o) {
TINT_ASSERT(AST, lhs);
TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, lhs, program_id);
TINT_ASSERT(AST, rhs);
TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, rhs, program_id);
}
CompoundAssignmentStatement::CompoundAssignmentStatement(
CompoundAssignmentStatement&&) = default;
CompoundAssignmentStatement::~CompoundAssignmentStatement() = default;
const CompoundAssignmentStatement* CompoundAssignmentStatement::Clone(
CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source);
auto* l = ctx->Clone(lhs);
auto* r = ctx->Clone(rhs);
return ctx->dst->create<CompoundAssignmentStatement>(src, l, r, op);
}
} // namespace ast
} // namespace tint

View File

@ -0,0 +1,63 @@
// Copyright 2022 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_TINT_AST_COMPOUND_ASSIGNMENT_STATEMENT_H_
#define SRC_TINT_AST_COMPOUND_ASSIGNMENT_STATEMENT_H_
#include "src/tint/ast/binary_expression.h"
#include "src/tint/ast/expression.h"
#include "src/tint/ast/statement.h"
namespace tint {
namespace ast {
/// A compound assignment statement
class CompoundAssignmentStatement final
: public Castable<CompoundAssignmentStatement, Statement> {
public:
/// Constructor
/// @param program_id the identifier of the program that owns this node
/// @param source the compound assignment statement source
/// @param lhs the left side of the expression
/// @param rhs the right side of the expression
/// @param op the binary operator
CompoundAssignmentStatement(ProgramID program_id,
const Source& source,
const Expression* lhs,
const Expression* rhs,
BinaryOp op);
/// Move constructor
CompoundAssignmentStatement(CompoundAssignmentStatement&&);
~CompoundAssignmentStatement() override;
/// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`.
/// @param ctx the clone context
/// @return the newly cloned node
const CompoundAssignmentStatement* Clone(CloneContext* ctx) const override;
/// left side expression
const Expression* const lhs;
/// right side expression
const Expression* const rhs;
/// the binary operator
const BinaryOp op;
};
} // namespace ast
} // namespace tint
#endif // SRC_TINT_AST_COMPOUND_ASSIGNMENT_STATEMENT_H_

View File

@ -0,0 +1,102 @@
// Copyright 2022 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/tint/ast/compound_assignment_statement.h"
#include "gtest/gtest-spi.h"
#include "src/tint/ast/test_helper.h"
namespace tint {
namespace ast {
namespace {
using CompoundAssignmentStatementTest = TestHelper;
TEST_F(CompoundAssignmentStatementTest, Creation) {
auto* lhs = Expr("lhs");
auto* rhs = Expr("rhs");
auto op = BinaryOp::kAdd;
auto* stmt = create<CompoundAssignmentStatement>(lhs, rhs, op);
EXPECT_EQ(stmt->lhs, lhs);
EXPECT_EQ(stmt->rhs, rhs);
EXPECT_EQ(stmt->op, op);
}
TEST_F(CompoundAssignmentStatementTest, CreationWithSource) {
auto* lhs = Expr("lhs");
auto* rhs = Expr("rhs");
auto op = BinaryOp::kMultiply;
auto* stmt = create<CompoundAssignmentStatement>(
Source{Source::Location{20, 2}}, lhs, rhs, op);
auto src = stmt->source;
EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u);
}
TEST_F(CompoundAssignmentStatementTest, IsCompoundAssign) {
auto* lhs = Expr("lhs");
auto* rhs = Expr("rhs");
auto op = BinaryOp::kSubtract;
auto* stmt = create<CompoundAssignmentStatement>(lhs, rhs, op);
EXPECT_TRUE(stmt->Is<CompoundAssignmentStatement>());
}
TEST_F(CompoundAssignmentStatementTest, Assert_Null_LHS) {
EXPECT_FATAL_FAILURE(
{
ProgramBuilder b;
b.create<CompoundAssignmentStatement>(nullptr, b.Expr(1),
BinaryOp::kAdd);
},
"internal compiler error");
}
TEST_F(CompoundAssignmentStatementTest, Assert_Null_RHS) {
EXPECT_FATAL_FAILURE(
{
ProgramBuilder b;
b.create<CompoundAssignmentStatement>(b.Expr(1), nullptr,
BinaryOp::kAdd);
},
"internal compiler error");
}
TEST_F(CompoundAssignmentStatementTest, Assert_DifferentProgramID_LHS) {
EXPECT_FATAL_FAILURE(
{
ProgramBuilder b1;
ProgramBuilder b2;
b1.create<CompoundAssignmentStatement>(b2.Expr("lhs"), b1.Expr("rhs"),
BinaryOp::kAdd);
},
"internal compiler error");
}
TEST_F(CompoundAssignmentStatementTest, Assert_DifferentProgramID_RHS) {
EXPECT_FATAL_FAILURE(
{
ProgramBuilder b1;
ProgramBuilder b2;
b1.create<CompoundAssignmentStatement>(b1.Expr("lhs"), b2.Expr("rhs"),
BinaryOp::kAdd);
},
"internal compiler error");
}
} // namespace
} // namespace ast
} // namespace tint

View File

@ -158,6 +158,7 @@ tint_unittests_source_set("tint_unittests_ast_src") {
"../../src/tint/ast/call_expression_test.cc",
"../../src/tint/ast/call_statement_test.cc",
"../../src/tint/ast/case_statement_test.cc",
"../../src/tint/ast/compound_assignment_statement_test.cc",
"../../src/tint/ast/continue_statement_test.cc",
"../../src/tint/ast/depth_multisampled_texture_test.cc",
"../../src/tint/ast/depth_texture_test.cc",