From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id DD5B53817D39; Wed, 8 Jun 2022 12:11:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD5B53817D39 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Refactor mapping any lang items to be done during HIR lowering X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 87aeea25837515cf31c8783238419e6e6936290d X-Git-Newrev: bede8222987f81b10204bf58212813331a630d73 Message-Id: <20220608121102.DD5B53817D39@sourceware.org> Date: Wed, 8 Jun 2022 12:11:02 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:11:03 -0000 https://gcc.gnu.org/g:bede8222987f81b10204bf58212813331a630d73 commit bede8222987f81b10204bf58212813331a630d73 Author: Philip Herron Date: Sun Feb 20 16:19:56 2022 +0000 Refactor mapping any lang items to be done during HIR lowering This extracts a common way of handling outer attributes on Items to improve error handling and make lang item mappings more generic. Diff: --- gcc/rust/hir/rust-ast-lower-base.h | 10 ++++ gcc/rust/hir/rust-ast-lower-item.h | 4 ++ gcc/rust/hir/rust-ast-lower.cc | 79 +++++++++++++++++++++++++++++ gcc/rust/typecheck/rust-hir-trait-resolve.h | 25 --------- 4 files changed, 93 insertions(+), 25 deletions(-) diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h index 5a54b308abe..038b6cf3c2f 100644 --- a/gcc/rust/hir/rust-ast-lower-base.h +++ b/gcc/rust/hir/rust-ast-lower-base.h @@ -281,6 +281,16 @@ protected: HIR::FunctionQualifiers lower_qualifiers (const AST::FunctionQualifiers &qualifiers); + + void handle_outer_attributes (const HIR::Item &item); + + void handle_lang_item_attribute (const HIR::Item &item, + const AST::Attribute &attr); + + static bool is_known_attribute (const std::string &attribute_path); + + static bool + attribute_handled_in_another_pass (const std::string &attribute_path); }; } // namespace HIR diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index 30bd896a797..55c8e8ae775 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -44,6 +44,10 @@ public: { ASTLoweringItem resolver; item->accept_vis (resolver); + + if (resolver.translated != nullptr) + resolver.handle_outer_attributes (*resolver.translated); + return resolver.translated; } diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index aac6ee5f1f8..45081afce21 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -609,5 +609,84 @@ ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers) has_extern, extern_abi); } +void +ASTLoweringBase::handle_outer_attributes (const HIR::Item &item) +{ + for (const auto &attr : item.get_outer_attrs ()) + { + const auto &str_path = attr.get_path ().as_string (); + if (!is_known_attribute (str_path)) + { + rust_error_at (attr.get_locus (), "unknown attribute"); + continue; + } + + bool is_lang_item = str_path.compare ("lang") == 0 + && attr.has_attr_input () + && attr.get_attr_input ().get_attr_input_type () + == AST::AttrInput::AttrInputType::LITERAL; + + if (is_lang_item) + handle_lang_item_attribute (item, attr); + else if (!attribute_handled_in_another_pass (str_path)) + { + rust_error_at (attr.get_locus (), "unhandled attribute: [%s]", + attr.get_path ().as_string ().c_str ()); + } + } +} + +void +ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item, + const AST::Attribute &attr) +{ + auto &literal = static_cast (attr.get_attr_input ()); + const auto &lang_item_type_str = literal.get_literal ().as_string (); + auto lang_item_type = Analysis::RustLangItem::Parse (lang_item_type_str); + if (lang_item_type == Analysis::RustLangItem::ItemType::UNKNOWN) + { + rust_error_at (attr.get_locus (), "unknown lang item"); + return; + } + mappings->insert_lang_item (lang_item_type, + item.get_mappings ().get_defid ()); +} + +bool +ASTLoweringBase::is_known_attribute (const std::string &attribute_path) +{ + if (attribute_path.compare ("inline") == 0) + return true; + else if (attribute_path.compare ("cfg") == 0) + return true; + else if (attribute_path.compare ("cfg_attr") == 0) + return true; + else if (attribute_path.compare ("allow") == 0) + return true; + else if (attribute_path.compare ("lang") == 0) + return true; + + return false; +} + +bool +ASTLoweringBase::attribute_handled_in_another_pass ( + const std::string &attribute_path) +{ + // handled during code-generation + if (attribute_path.compare ("inline") == 0) + return true; + + // handled during previous expansion pass + else if (attribute_path.compare ("cfg") == 0) + return true; + else if (attribute_path.compare ("cfg_attr") == 0) + return true; + else if (attribute_path.compare ("allow") == 0) + return true; + + return false; +} + } // namespace HIR } // namespace Rust diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.h b/gcc/rust/typecheck/rust-hir-trait-resolve.h index 7ab344e761b..d8df481076c 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.h +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.h @@ -207,31 +207,6 @@ private: // loop of trying to resolve traits as required by the types tref->on_resolved (); - // does this have any lang-item attributes? - for (auto &attr : trait_reference->get_outer_attrs ()) - { - bool is_lang_item = attr.get_path ().as_string ().compare ("lang") == 0 - && attr.has_attr_input () - && attr.get_attr_input ().get_attr_input_type () - == AST::AttrInput::AttrInputType::LITERAL; - if (is_lang_item) - { - auto &literal - = static_cast (attr.get_attr_input ()); - const auto &lang_item_type_str - = literal.get_literal ().as_string (); - auto lang_item_type - = Analysis::RustLangItem::Parse (lang_item_type_str); - if (lang_item_type == Analysis::RustLangItem::ItemType::UNKNOWN) - { - rust_error_at (attr.get_locus (), "unknown lang item"); - return tref; - } - mappings->insert_lang_item ( - lang_item_type, trait_reference->get_mappings ().get_defid ()); - } - } - return tref; }