public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Refactor mapping any lang items to be done during HIR lowering Date: Wed, 8 Jun 2022 12:11:02 +0000 (GMT) [thread overview] Message-ID: <20220608121102.DD5B53817D39@sourceware.org> (raw) https://gcc.gnu.org/g:bede8222987f81b10204bf58212813331a630d73 commit bede8222987f81b10204bf58212813331a630d73 Author: Philip Herron <philip.herron@embecosm.com> 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<AST::AttrInputLiteral &> (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<AST::AttrInputLiteral &> (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; }
reply other threads:[~2022-06-08 12:11 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220608121102.DD5B53817D39@sourceware.org \ --to=tschwinge@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).