From 72081086d6b8228f7db6526e39e41bea2eae244c Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Wed, 4 Dec 2024 13:53:11 +0000 Subject: [PATCH] attributes: Add class for sharing methods on attributes. gcc/rust/ChangeLog: * util/rust-attributes.h (class Attributes): New. * util/rust-attributes.cc: Implement Attributes::is_known(). * ast/rust-collect-lang-items.cc (is_known_attribute): Remove. (get_lang_item_attr): Call Attributes::is_known() instead. * hir/rust-ast-lower-base.cc (ASTLoweringBase::handle_outer_attributes): Likewise. (ASTLoweringBase::is_known_attribute): Remove. --- gcc/rust/ast/rust-collect-lang-items.cc | 13 +------------ gcc/rust/hir/rust-ast-lower-base.cc | 10 ++-------- gcc/rust/util/rust-attributes.cc | 9 +++++++++ gcc/rust/util/rust-attributes.h | 6 ++++++ 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/gcc/rust/ast/rust-collect-lang-items.cc b/gcc/rust/ast/rust-collect-lang-items.cc index 11a30aa37a74..308720ae69ab 100644 --- a/gcc/rust/ast/rust-collect-lang-items.cc +++ b/gcc/rust/ast/rust-collect-lang-items.cc @@ -27,17 +27,6 @@ namespace Rust { namespace AST { -// FIXME: Before merging: De-duplicate with function in rust-ast-lower-base.cc -bool -is_known_attribute (const std::string &attribute_path) -{ - const auto &lookup - = Analysis::BuiltinAttributeMappings::get ()->lookup_builtin ( - attribute_path); - - return !lookup.is_error (); -} - template tl::optional get_lang_item_attr (const T &maybe_lang_item) @@ -45,7 +34,7 @@ get_lang_item_attr (const T &maybe_lang_item) for (const auto &attr : maybe_lang_item.get_outer_attrs ()) { const auto &str_path = attr.get_path ().as_string (); - if (!is_known_attribute (str_path)) + if (!Analysis::Attributes::is_known (str_path)) { rust_error_at (attr.get_locus (), "unknown attribute"); continue; diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index 272de0f87172..eac2cba5c752 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -25,6 +25,7 @@ #include "rust-diagnostics.h" #include "rust-item.h" #include "rust-system.h" +#include "rust-attributes.h" namespace Rust { namespace HIR { @@ -751,7 +752,7 @@ ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item) for (const auto &attr : item.get_outer_attrs ()) { const auto &str_path = attr.get_path ().as_string (); - if (!is_known_attribute (str_path)) + if (!Analysis::Attributes::is_known (str_path)) { rust_error_at (attr.get_locus (), "unknown attribute"); continue; @@ -814,13 +815,6 @@ ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item, rust_error_at (attr.get_locus (), "unknown lang item"); } -bool -ASTLoweringBase::is_known_attribute (const std::string &attribute_path) const -{ - const auto &lookup = attr_mappings->lookup_builtin (attribute_path); - return !lookup.is_error (); -} - bool ASTLoweringBase::attribute_handled_in_another_pass ( const std::string &attribute_path) const diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index 14f00bd3c526..45ebf8c65461 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -29,6 +29,15 @@ namespace Rust { namespace Analysis { +bool +Attributes::is_known (const std::string &attribute_path) +{ + const auto &lookup + = BuiltinAttributeMappings::get ()->lookup_builtin (attribute_path); + + return !lookup.is_error (); +} + using Attrs = Values::Attributes; // https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248 diff --git a/gcc/rust/util/rust-attributes.h b/gcc/rust/util/rust-attributes.h index f557b2d46b38..c341b3e0a5db 100644 --- a/gcc/rust/util/rust-attributes.h +++ b/gcc/rust/util/rust-attributes.h @@ -25,6 +25,12 @@ namespace Rust { namespace Analysis { +class Attributes +{ +public: + static bool is_known (const std::string &attribute_path); +}; + enum CompilerPass { UNKNOWN,