public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Refactor mapping any lang items to be done during HIR lowering
@ 2022-06-08 12:11 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:11 UTC (permalink / raw)
  To: gcc-cvs

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;
   }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:11 [gcc/devel/rust/master] Refactor mapping any lang items to be done during HIR lowering Thomas Schwinge

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).