Add `@must_use` AST node.
This CL adds the `@must_use` ast node and hooks it into the
program_builder.
Bug: tint:1844
Change-Id: I2ffe99bbf29ec287446788253aa8cf63cc4da50b
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/120820
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn
index 3207ebb..50b468c 100644
--- a/src/tint/BUILD.gn
+++ b/src/tint/BUILD.gn
@@ -490,6 +490,7 @@
"ast/loop_statement.h",
"ast/member_accessor_expression.h",
"ast/module.h",
+ "ast/must_use_attribute.h",
"ast/node.h",
"ast/node_id.h",
"ast/override.h",
@@ -575,6 +576,7 @@
"ast/loop_statement.cc",
"ast/member_accessor_expression.cc",
"ast/module.cc",
+ "ast/must_use_attribute.cc",
"ast/node.cc",
"ast/override.cc",
"ast/parameter.cc",
@@ -1268,7 +1270,6 @@
"ast/index_accessor_expression_test.cc",
"ast/int_literal_expression_test.cc",
"ast/interpolate_attribute_test.cc",
- "ast/invariant_attribute_test.cc",
"ast/location_attribute_test.cc",
"ast/loop_statement_test.cc",
"ast/member_accessor_expression_test.cc",
diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt
index 79f19a4..91c0851 100644
--- a/src/tint/CMakeLists.txt
+++ b/src/tint/CMakeLists.txt
@@ -174,6 +174,8 @@
ast/member_accessor_expression.h
ast/module.cc
ast/module.h
+ ast/must_use_attribute.cc
+ ast/must_use_attribute.h
ast/node_id.h
ast/node.cc
ast/node.h
@@ -831,7 +833,6 @@
ast/index_accessor_expression_test.cc
ast/int_literal_expression_test.cc
ast/interpolate_attribute_test.cc
- ast/invariant_attribute_test.cc
ast/location_attribute_test.cc
ast/loop_statement_test.cc
ast/member_accessor_expression_test.cc
diff --git a/src/tint/ast/invariant_attribute_test.cc b/src/tint/ast/invariant_attribute_test.cc
deleted file mode 100644
index edc7498..0000000
--- a/src/tint/ast/invariant_attribute_test.cc
+++ /dev/null
@@ -1,25 +0,0 @@
-// 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/tint/ast/invariant_attribute.h"
-
-#include "src/tint/ast/test_helper.h"
-
-namespace tint::ast {
-namespace {
-
-using InvariantAttributeTest = TestHelper;
-
-} // namespace
-} // namespace tint::ast
diff --git a/src/tint/ast/must_use_attribute.cc b/src/tint/ast/must_use_attribute.cc
new file mode 100644
index 0000000..dc1af16
--- /dev/null
+++ b/src/tint/ast/must_use_attribute.cc
@@ -0,0 +1,38 @@
+// Copyright 2023 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/must_use_attribute.h"
+
+#include "src/tint/program_builder.h"
+
+TINT_INSTANTIATE_TYPEINFO(tint::ast::MustUseAttribute);
+
+namespace tint::ast {
+
+MustUseAttribute::MustUseAttribute(ProgramID pid, NodeID nid, const Source& src)
+ : Base(pid, nid, src) {}
+
+MustUseAttribute::~MustUseAttribute() = default;
+
+std::string MustUseAttribute::Name() const {
+ return "must_use";
+}
+
+const MustUseAttribute* MustUseAttribute::Clone(CloneContext* ctx) const {
+ // Clone arguments outside of create() call to have deterministic ordering
+ auto src = ctx->Clone(source);
+ return ctx->dst->create<MustUseAttribute>(src);
+}
+
+} // namespace tint::ast
diff --git a/src/tint/ast/must_use_attribute.h b/src/tint/ast/must_use_attribute.h
new file mode 100644
index 0000000..6befa54
--- /dev/null
+++ b/src/tint/ast/must_use_attribute.h
@@ -0,0 +1,46 @@
+// Copyright 2023 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_MUST_USE_ATTRIBUTE_H_
+#define SRC_TINT_AST_MUST_USE_ATTRIBUTE_H_
+
+#include <string>
+
+#include "src/tint/ast/attribute.h"
+
+namespace tint::ast {
+
+/// The must_use attribute
+class MustUseAttribute final : public Castable<MustUseAttribute, Attribute> {
+ public:
+ /// constructor
+ /// @param pid the identifier of the program that owns this node
+ /// @param nid the unique node identifier
+ /// @param src the source of this node
+ MustUseAttribute(ProgramID pid, NodeID nid, const Source& src);
+ ~MustUseAttribute() override;
+
+ /// @returns the WGSL name for the attribute
+ std::string Name() const override;
+
+ /// Clones this node and all transitive child nodes using the `CloneContext`
+ /// `ctx`.
+ /// @param ctx the clone context
+ /// @return the newly cloned node
+ const MustUseAttribute* Clone(CloneContext* ctx) const override;
+};
+
+} // namespace tint::ast
+
+#endif // SRC_TINT_AST_MUST_USE_ATTRIBUTE_H_
diff --git a/src/tint/ir/builder_impl.cc b/src/tint/ir/builder_impl.cc
index cbc7165..d3f8d53 100644
--- a/src/tint/ir/builder_impl.cc
+++ b/src/tint/ir/builder_impl.cc
@@ -800,6 +800,9 @@
// [&](const ast::InvariantAttribute* i) {
// TODO(dsinclair): Implement
// },
+ // [&](const ast::MustUseAttribute* i) {
+ // TODO(dsinclair): Implement
+ // },
[&](const ast::IdAttribute*) {
diagnostics_.add_warning(tint::diag::System::IR,
"found an `Id` attribute. The SubstituteOverrides transform "
diff --git a/src/tint/program_builder.h b/src/tint/program_builder.h
index 03cc315..44c2330 100644
--- a/src/tint/program_builder.h
+++ b/src/tint/program_builder.h
@@ -56,6 +56,7 @@
#include "src/tint/ast/loop_statement.h"
#include "src/tint/ast/member_accessor_expression.h"
#include "src/tint/ast/module.h"
+#include "src/tint/ast/must_use_attribute.h"
#include "src/tint/ast/override.h"
#include "src/tint/ast/parameter.h"
#include "src/tint/ast/phony_expression.h"
@@ -3504,6 +3505,17 @@
/// @returns the invariant attribute pointer
const ast::InvariantAttribute* Invariant() { return create<ast::InvariantAttribute>(source_); }
+ /// Creates an ast::MustUseAttribute
+ /// @param source the source information
+ /// @returns the invariant attribute pointer
+ const ast::MustUseAttribute* MustUse(const Source& source) {
+ return create<ast::MustUseAttribute>(source);
+ }
+
+ /// Creates an ast::MustUseAttribute
+ /// @returns the invariant attribute pointer
+ const ast::MustUseAttribute* MustUse() { return create<ast::MustUseAttribute>(source_); }
+
/// Creates an ast::LocationAttribute
/// @param source the source information
/// @param location the location value expression
diff --git a/src/tint/reader/wgsl/parser_impl.cc b/src/tint/reader/wgsl/parser_impl.cc
index 3317ab4..d80a039 100644
--- a/src/tint/reader/wgsl/parser_impl.cc
+++ b/src/tint/reader/wgsl/parser_impl.cc
@@ -85,12 +85,12 @@
t == "finally" || t == "friend" || t == "from" || t == "fxgroup" || t == "get" ||
t == "goto" || t == "groupshared" || t == "handle" || t == "highp" || t == "impl" ||
t == "implements" || t == "import" || t == "inline" || t == "inout" ||
- t == "instanceof" || t == "interface" || t == "invariant" || t == "layout" ||
- t == "lowp" || t == "macro" || t == "macro_rules" || t == "match" || t == "mediump" ||
- t == "meta" || t == "mod" || t == "module" || t == "move" || t == "mut" ||
- t == "mutable" || t == "namespace" || t == "new" || t == "nil" || t == "noexcept" ||
- t == "noinline" || t == "nointerpolation" || t == "noperspective" || t == "null" ||
- t == "nullptr" || t == "of" || t == "operator" || t == "package" || t == "packoffset" ||
+ t == "instanceof" || t == "interface" || t == "layout" || t == "lowp" || t == "macro" ||
+ t == "macro_rules" || t == "match" || t == "mediump" || t == "meta" || t == "mod" ||
+ t == "module" || t == "move" || t == "mut" || t == "mutable" || t == "namespace" ||
+ t == "new" || t == "nil" || t == "noexcept" || t == "noinline" ||
+ t == "nointerpolation" || t == "noperspective" || t == "null" || t == "nullptr" ||
+ t == "of" || t == "operator" || t == "package" || t == "packoffset" ||
t == "partition" || t == "pass" || t == "patch" || t == "pixelfragment" ||
t == "precise" || t == "precision" || t == "premerge" || t == "priv" ||
t == "protected" || t == "pub" || t == "public" || t == "readonly" || t == "ref" ||
diff --git a/src/tint/reader/wgsl/parser_impl_reserved_keyword_test.cc b/src/tint/reader/wgsl/parser_impl_reserved_keyword_test.cc
index 57429db..bf91ece 100644
--- a/src/tint/reader/wgsl/parser_impl_reserved_keyword_test.cc
+++ b/src/tint/reader/wgsl/parser_impl_reserved_keyword_test.cc
@@ -152,7 +152,6 @@
"inout",
"instanceof",
"interface",
- "invariant",
"layout",
"lowp",
"macro",
diff --git a/src/tint/resolver/dependency_graph.cc b/src/tint/resolver/dependency_graph.cc
index 2100f6b..a764596 100644
--- a/src/tint/resolver/dependency_graph.cc
+++ b/src/tint/resolver/dependency_graph.cc
@@ -39,6 +39,7 @@
#include "src/tint/ast/invariant_attribute.h"
#include "src/tint/ast/location_attribute.h"
#include "src/tint/ast/loop_statement.h"
+#include "src/tint/ast/must_use_attribute.h"
#include "src/tint/ast/override.h"
#include "src/tint/ast/return_statement.h"
#include "src/tint/ast/stage_attribute.h"
@@ -460,8 +461,9 @@
}
if (attr->IsAnyOf<ast::BuiltinAttribute, ast::DiagnosticAttribute, ast::InternalAttribute,
- ast::InterpolateAttribute, ast::InvariantAttribute, ast::StageAttribute,
- ast::StrideAttribute, ast::StructMemberOffsetAttribute>()) {
+ ast::InterpolateAttribute, ast::InvariantAttribute, ast::MustUseAttribute,
+ ast::StageAttribute, ast::StrideAttribute,
+ ast::StructMemberOffsetAttribute>()) {
return;
}