From 4a5e864cf0b0489fff1101d5ac758861be2acebe Mon Sep 17 00:00:00 2001 From: Om Swaroop Nayak <96killerat96@gmail.com> Date: Tue, 31 Dec 2024 08:06:59 -0800 Subject: [PATCH 1/2] gcc/rust/ChangeLog: * backend/rust-compile-base.cc (get_attributes): "removed checker function" (get_trait_name): "removed checker function" * util/rust-attributes.cc (Attributes::get_attributes): "added checker function" (Attributes::get_trait_name): "added checker function" * util/rust-attributes.h: "added functions definitions in the header file for access" Signed-off-by: Om Swaroop Nayak <96killerat96@gmail.com> --- gcc/rust/backend/rust-compile-base.cc | 63 +-------------------------- gcc/rust/util/rust-attributes.cc | 59 +++++++++++++++++++++++++ gcc/rust/util/rust-attributes.h | 2 + 3 files changed, 63 insertions(+), 61 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index a4d0d062accc..ae166ba788ef 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -158,65 +158,6 @@ HIRCompileBase::handle_attribute_proc_macro_attribute_on_fndecl ( ctx->collect_attribute_proc_macro (fndecl); } -static std::vector -get_attributes (const AST::Attribute &attr) -{ - std::vector result; - - rust_assert (attr.get_attr_input ().get_attr_input_type () - == Rust::AST::AttrInput::TOKEN_TREE); - const auto &tt - = static_cast (attr.get_attr_input ()); - - // TODO: Should we rely on fixed index ? Should we search for the - // attribute tokentree instead ? - - // Derive proc macros have the following format: - // #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))] - // -~~~~~~~~ - ~~~~~~~~~~--------------------- - // ^0 ^1 ^2 ^3 ^4 - // - "attributes" is stored at position 3 in the token tree - // - attribute are stored in the delimited token tree in position 4 - constexpr size_t attr_kw_pos = 3; - constexpr size_t attribute_list_pos = 4; - - if (tt.get_token_trees ().size () > attr_kw_pos) - { - rust_assert (tt.get_token_trees ()[attr_kw_pos]->as_string () - == "attributes"); - - auto attributes = static_cast ( - tt.get_token_trees ()[attribute_list_pos].get ()); - - auto &token_trees = attributes->get_token_trees (); - - for (auto i = token_trees.cbegin () + 1; // Skip opening parenthesis - i < token_trees.cend (); - i += 2) // Skip comma and closing parenthesis - { - result.push_back ((*i)->as_string ()); - } - } - return result; -} - -static std::string -get_trait_name (const AST::Attribute &attr) -{ - // Derive proc macros have the following format: - // #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))] - // -~~~~~~~~ - ~~~~~~~~~~--------------------- - // ^0 ^1 ^2 ^3 ^4 - // - The trait name is stored at position 1 - constexpr size_t trait_name_pos = 1; - - rust_assert (attr.get_attr_input ().get_attr_input_type () - == Rust::AST::AttrInput::TOKEN_TREE); - const auto &tt - = static_cast (attr.get_attr_input ()); - return tt.get_token_trees ()[trait_name_pos]->as_string (); -} - void HIRCompileBase::handle_derive_proc_macro_attribute_on_fndecl ( tree fndecl, const AST::Attribute &attr) @@ -224,8 +165,8 @@ HIRCompileBase::handle_derive_proc_macro_attribute_on_fndecl ( handle_proc_macro_common (fndecl, attr); attr.get_attr_input ().parse_to_meta_item (); - CustomDeriveInfo macro - = {fndecl, get_trait_name (attr), get_attributes (attr)}; + CustomDeriveInfo macro = {fndecl, Analysis::Attributes::get_trait_name (attr), + Analysis::Attributes::get_attributes (attr)}; ctx->collect_derive_proc_macro (macro); } diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index 45ebf8c65461..d6995efd4b05 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -38,6 +38,65 @@ Attributes::is_known (const std::string &attribute_path) return !lookup.is_error (); } +std::vector +Attributes::get_attributes (const AST::Attribute &attr) +{ + std::vector result; + + rust_assert (attr.get_attr_input ().get_attr_input_type () + == Rust::AST::AttrInput::TOKEN_TREE); + const auto &tt + = static_cast (attr.get_attr_input ()); + + // TODO: Should we rely on fixed index ? Should we search for the + // attribute tokentree instead ? + + // Derive proc macros have the following format: + // #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))] + // -~~~~~~~~ - ~~~~~~~~~~--------------------- + // ^0 ^1 ^2 ^3 ^4 + // - "attributes" is stored at position 3 in the token tree + // - attribute are stored in the delimited token tree in position 4 + constexpr size_t attr_kw_pos = 3; + constexpr size_t attribute_list_pos = 4; + + if (tt.get_token_trees ().size () > attr_kw_pos) + { + rust_assert (tt.get_token_trees ()[attr_kw_pos]->as_string () + == "attributes"); + + auto attributes = static_cast ( + tt.get_token_trees ()[attribute_list_pos].get ()); + + auto &token_trees = attributes->get_token_trees (); + + for (auto i = token_trees.cbegin () + 1; // Skip opening parenthesis + i < token_trees.cend (); + i += 2) // Skip comma and closing parenthesis + { + result.push_back ((*i)->as_string ()); + } + } + return result; +} + +std::string +Attributes::get_trait_name (const AST::Attribute &attr) +{ + // Derive proc macros have the following format: + // #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))] + // -~~~~~~~~ - ~~~~~~~~~~--------------------- + // ^0 ^1 ^2 ^3 ^4 + // - The trait name is stored at position 1 + constexpr size_t trait_name_pos = 1; + + rust_assert (attr.get_attr_input ().get_attr_input_type () + == Rust::AST::AttrInput::TOKEN_TREE); + const auto &tt + = static_cast (attr.get_attr_input ()); + return tt.get_token_trees ()[trait_name_pos]->as_string (); +} + 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 c341b3e0a5db..3aecfcbb41bb 100644 --- a/gcc/rust/util/rust-attributes.h +++ b/gcc/rust/util/rust-attributes.h @@ -29,6 +29,8 @@ class Attributes { public: static bool is_known (const std::string &attribute_path); + static std::vector get_attributes (const AST::Attribute &attr); + static std::string get_trait_name (const AST::Attribute &attr); }; enum CompilerPass From 22f70ef6793f9b1a58c0003991cba0063276ef23 Mon Sep 17 00:00:00 2001 From: Om Swaroop Nayak <146124307+saeitoshi-10@users.noreply.github.com> Date: Tue, 31 Dec 2024 21:56:29 +0530 Subject: [PATCH 2/2] Update rust-compile-base.cc --- gcc/rust/backend/rust-compile-base.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index ae166ba788ef..82e07650d741 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -33,6 +33,7 @@ #include "rust-compile-implitem.h" #include "rust-attribute-values.h" #include "rust-immutable-name-resolution-context.h" +#include "rust-attributes.h" #include "fold-const.h" #include "stringpool.h"