From 8fca34546b51f5f2206c62aa1492b1c8567a3058 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 21 Oct 2021 18:33:14 +0000 Subject: [PATCH] ast: Add PhonyExpression Represents the `_` in a phony assignment `_ = ` Bug: tint:1213 Change-Id: Ifb070277b7872aa5ae405e467e28191e625c4c5a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/67062 Kokoro: Kokoro Commit-Queue: Ben Clayton Reviewed-by: David Neto Reviewed-by: James Price --- src/BUILD.gn | 2 ++ src/CMakeLists.txt | 3 +++ src/ast/phony_expression.cc | 38 +++++++++++++++++++++++++++ src/ast/phony_expression.h | 45 ++++++++++++++++++++++++++++++++ src/ast/phony_expression_test.cc | 42 +++++++++++++++++++++++++++++ src/program_builder.h | 10 +++++++ test/BUILD.gn | 1 + 7 files changed, 141 insertions(+) create mode 100644 src/ast/phony_expression.cc create mode 100644 src/ast/phony_expression.h create mode 100644 src/ast/phony_expression_test.cc diff --git a/src/BUILD.gn b/src/BUILD.gn index fa51b86ff3..aa4a13d23b 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -274,6 +274,8 @@ libtint_source_set("libtint_core_all_src") { "ast/node.h", "ast/override_decoration.cc", "ast/override_decoration.h", + "ast/phony_expression.cc", + "ast/phony_expression.h", "ast/pipeline_stage.cc", "ast/pipeline_stage.h", "ast/pointer.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f188da1488..53def54ea2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -138,6 +138,8 @@ set(TINT_LIB_SRCS ast/node.h ast/override_decoration.cc ast/override_decoration.h + ast/phony_expression.cc + ast/phony_expression.h ast/pipeline_stage.cc ast/pipeline_stage.h ast/pointer.cc @@ -619,6 +621,7 @@ if(${TINT_BUILD_TESTS}) ast/module_test.cc ast/multisampled_texture_test.cc ast/override_decoration_test.cc + ast/phony_expression_test.cc ast/pointer_test.cc ast/return_statement_test.cc ast/sampled_texture_test.cc diff --git a/src/ast/phony_expression.cc b/src/ast/phony_expression.cc new file mode 100644 index 0000000000..dc01f03e20 --- /dev/null +++ b/src/ast/phony_expression.cc @@ -0,0 +1,38 @@ +// Copyright 2021 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/phony_expression.h" + +#include "src/program_builder.h" + +TINT_INSTANTIATE_TYPEINFO(tint::ast::PhonyExpression); + +namespace tint { +namespace ast { + +PhonyExpression::PhonyExpression(ProgramID pid, const Source& src) + : Base(pid, src) {} + +PhonyExpression::PhonyExpression(PhonyExpression&&) = default; + +PhonyExpression::~PhonyExpression() = default; + +const PhonyExpression* PhonyExpression::Clone(CloneContext* ctx) const { + // Clone arguments outside of create() call to have deterministic ordering + auto src = ctx->Clone(source); + return ctx->dst->create(src); +} + +} // namespace ast +} // namespace tint diff --git a/src/ast/phony_expression.h b/src/ast/phony_expression.h new file mode 100644 index 0000000000..edfc7d001e --- /dev/null +++ b/src/ast/phony_expression.h @@ -0,0 +1,45 @@ +// Copyright 2021 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_PHONY_EXPRESSION_H_ +#define SRC_AST_PHONY_EXPRESSION_H_ + +#include "src/ast/expression.h" + +namespace tint { +namespace ast { + +/// Represents the `_` of a phony assignment `_ = ` +/// @see https://www.w3.org/TR/WGSL/#phony-assignment-section +class PhonyExpression : public Castable { + public: + /// Constructor + /// @param pid the identifier of the program that owns this node + /// @param src the source of this node + PhonyExpression(ProgramID pid, const Source& src); + /// Move constructor + PhonyExpression(PhonyExpression&&); + ~PhonyExpression() override; + + /// Clones this node and all transitive child nodes using the `CloneContext` + /// `ctx`. + /// @param ctx the clone context + /// @return the newly cloned node + const PhonyExpression* Clone(CloneContext* ctx) const override; +}; + +} // namespace ast +} // namespace tint + +#endif // SRC_AST_PHONY_EXPRESSION_H_ diff --git a/src/ast/phony_expression_test.cc b/src/ast/phony_expression_test.cc new file mode 100644 index 0000000000..15ec654e8e --- /dev/null +++ b/src/ast/phony_expression_test.cc @@ -0,0 +1,42 @@ +// Copyright 2021 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/test_helper.h" + +namespace tint { +namespace ast { +namespace { + +using IdentifierExpressionTest = TestHelper; + +TEST_F(IdentifierExpressionTest, Creation) { + EXPECT_NE(Phony(), nullptr); +} + +TEST_F(IdentifierExpressionTest, Creation_WithSource) { + auto* p = Phony(Source{{20, 2}}); + + auto src = p->source; + EXPECT_EQ(src.range.begin.line, 20u); + EXPECT_EQ(src.range.begin.column, 2u); +} + +TEST_F(IdentifierExpressionTest, IsPhony) { + auto* p = Phony(); + EXPECT_TRUE(p->Is()); +} + +} // namespace +} // namespace ast +} // namespace tint diff --git a/src/program_builder.h b/src/program_builder.h index 0928b8ec21..cf9103a439 100644 --- a/src/program_builder.h +++ b/src/program_builder.h @@ -49,6 +49,7 @@ #include "src/ast/module.h" #include "src/ast/multisampled_texture.h" #include "src/ast/override_decoration.h" +#include "src/ast/phony_expression.h" #include "src/ast/pointer.h" #include "src/ast/return_statement.h" #include "src/ast/sampled_texture.h" @@ -1618,6 +1619,15 @@ class ProgramBuilder { ExprList(std::forward(args)...)); } + /// @param source the source information + /// @returns a `ast::PhonyExpression` + const ast::PhonyExpression* Phony(const Source& source) { + return create(source); + } + + /// @returns a `ast::PhonyExpression` + const ast::PhonyExpression* Phony() { return create(); } + /// @param expr the expression to ignore /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is /// passed the single `expr` argument diff --git a/test/BUILD.gn b/test/BUILD.gn index cefec250d7..53dc747e5a 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -189,6 +189,7 @@ tint_unittests_source_set("tint_unittests_core_src") { "../src/ast/module_test.cc", "../src/ast/multisampled_texture_test.cc", "../src/ast/override_decoration_test.cc", + "../src/ast/phony_expression_test.cc", "../src/ast/pointer_test.cc", "../src/ast/return_statement_test.cc", "../src/ast/sampled_texture_test.cc",