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>
This commit is contained in:
dan sinclair 2023-02-21 18:49:40 +00:00 committed by Dawn LUCI CQ
parent b92ff39724
commit a79c6603b2
10 changed files with 113 additions and 36 deletions

View File

@ -490,6 +490,7 @@ libtint_source_set("libtint_ast_hdrs") {
"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 @@ libtint_source_set("libtint_ast_src") {
"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 @@ if (tint_build_unittests) {
"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",

View File

@ -174,6 +174,8 @@ list(APPEND TINT_LIB_SRCS
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 @@ if(TINT_BUILD_TESTS)
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

View File

@ -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

View File

@ -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

View File

@ -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_

View File

@ -800,6 +800,9 @@ bool BuilderImpl::EmitAttribute(const ast::Attribute* attr) {
// [&](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 "

View File

@ -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 @@ class ProgramBuilder {
/// @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

View File

@ -85,12 +85,12 @@ bool is_reserved(const Token& t) {
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" ||

View File

@ -152,7 +152,6 @@ INSTANTIATE_TEST_SUITE_P(ParserImplReservedKeywordTest,
"inout",
"instanceof",
"interface",
"invariant",
"layout",
"lowp",
"macro",

View File

@ -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 @@ class DependencyScanner {
}
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;
}