diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-02-20 16:19:56 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-02-22 15:17:36 +0000 |
commit | bede8222987f81b10204bf58212813331a630d73 (patch) | |
tree | 32cfb3d36f77e61c3f386ce273fd09c221ddb6e8 /gcc | |
parent | 87aeea25837515cf31c8783238419e6e6936290d (diff) | |
download | gcc-bede8222987f81b10204bf58212813331a630d73.zip gcc-bede8222987f81b10204bf58212813331a630d73.tar.gz gcc-bede8222987f81b10204bf58212813331a630d73.tar.bz2 |
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-base.h | 10 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 4 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower.cc | 79 | ||||
-rw-r--r-- | 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 5a54b30..038b6cf 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 30bd896..55c8e8a 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 aac6ee5..45081af 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 7ab344e..d8df481 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; } |