[gcc r14-7976] gccrs: Set traits getter as member function

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 18:10:19 GMT 2024


https://gcc.gnu.org/g:de8bc8f9bc03230f124802c4dd0a42f79796a770

commit r14-7976-gde8bc8f9bc03230f124802c4dd0a42f79796a770
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Fri Jul 28 17:55:55 2023 +0200

    gccrs: Set traits getter as member function
    
    This function will be used outside of the expand visitor, making it
    easily accessible is therefore mandatory.
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast.cc (Attribute::get_traits_to_derive): Add
            function as member function.
            * ast/rust-ast.h: Add prototype.
            * expand/rust-expand-visitor.cc (get_traits_to_derive): Remove
            function.
            (ExpandVisitor::expand_inner_stmts): Update function call.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/ast/rust-ast.cc               | 72 ++++++++++++++++++++++++++++++++
 gcc/rust/ast/rust-ast.h                |  2 +
 gcc/rust/expand/rust-expand-visitor.cc | 76 +---------------------------------
 3 files changed, 76 insertions(+), 74 deletions(-)

diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index 434c2768bc5..076f40e338b 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -90,6 +90,78 @@ Attribute::is_derive () const
 	 && get_path () == "derive";
 }
 
+/**
+ * Returns a list of traits to derive from within a given attribute.
+ *
+ * @param attrs The attributes on the item to derive
+ */
+std::vector<AST::SimplePath>
+Attribute::get_traits_to_derive ()
+{
+  std::vector<AST::SimplePath> result;
+  auto &input = get_attr_input ();
+  switch (input.get_attr_input_type ())
+    {
+      case AST::AttrInput::META_ITEM: {
+	auto meta = static_cast<AST::AttrInputMetaItemContainer &> (input);
+	for (auto &current : meta.get_items ())
+	  {
+	    // HACK: Find a better way to achieve the downcast.
+	    switch (current->get_kind ())
+	      {
+		case AST::MetaItemInner::Kind::MetaItem: {
+		  // Let raw pointer go out of scope without freeing, it doesn't
+		  // own the data anyway
+		  auto meta_item
+		    = static_cast<AST::MetaItem *> (current.get ());
+		  switch (meta_item->get_item_kind ())
+		    {
+		      case AST::MetaItem::ItemKind::Path: {
+			auto path
+			  = static_cast<AST::MetaItemPath *> (meta_item);
+			result.push_back (path->get_path ());
+		      }
+		      break;
+		      case AST::MetaItem::ItemKind::Word: {
+			auto word = static_cast<AST::MetaWord *> (meta_item);
+			// Convert current word to path
+			current
+			  = make_unique<AST::MetaItemPath> (AST::MetaItemPath (
+			    AST::SimplePath (word->get_ident ())));
+			auto path
+			  = static_cast<AST::MetaItemPath *> (current.get ());
+
+			result.push_back (path->get_path ());
+		      }
+		      break;
+		    case AST::MetaItem::ItemKind::ListPaths:
+		    case AST::MetaItem::ItemKind::NameValueStr:
+		    case AST::MetaItem::ItemKind::PathLit:
+		    case AST::MetaItem::ItemKind::Seq:
+		    case AST::MetaItem::ItemKind::ListNameValueStr:
+		    default:
+		      gcc_unreachable ();
+		      break;
+		    }
+		}
+		break;
+	      case AST::MetaItemInner::Kind::LitExpr:
+	      default:
+		gcc_unreachable ();
+		break;
+	      }
+	  }
+      }
+      break;
+    case AST::AttrInput::TOKEN_TREE:
+    case AST::AttrInput::LITERAL:
+    case AST::AttrInput::MACRO:
+      rust_unreachable ();
+      break;
+    }
+  return result;
+}
+
 // Copy constructor must deep copy attr_input as unique pointer
 Attribute::Attribute (Attribute const &other)
   : path (other.path), locus (other.locus)
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 08b511f4019..32732ffdbd7 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -519,6 +519,8 @@ public:
 
   bool is_derive () const;
 
+  std::vector<AST::SimplePath> get_traits_to_derive ();
+
   // default destructor
   ~Attribute () = default;
 
diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index 77dfdcb6d6a..f75069b2e0a 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -42,78 +42,6 @@ ExpandVisitor::go (AST::Crate &crate)
   expand_inner_items (crate.items);
 }
 
-/**
- * Returns a list of traits to derive from within a given attribute.
- *
- * @param attrs The attributes on the item to derive
- */
-static std::vector<AST::SimplePath>
-get_traits_to_derive (AST::Attribute &attr)
-{
-  std::vector<AST::SimplePath> result;
-  auto &input = attr.get_attr_input ();
-  switch (input.get_attr_input_type ())
-    {
-      case AST::AttrInput::META_ITEM: {
-	auto meta = static_cast<AST::AttrInputMetaItemContainer &> (input);
-	for (auto &current : meta.get_items ())
-	  {
-	    // HACK: Find a better way to achieve the downcast.
-	    switch (current->get_kind ())
-	      {
-		case AST::MetaItemInner::Kind::MetaItem: {
-		  // Let raw pointer go out of scope without freeing, it doesn't
-		  // own the data anyway
-		  auto meta_item
-		    = static_cast<AST::MetaItem *> (current.get ());
-		  switch (meta_item->get_item_kind ())
-		    {
-		      case AST::MetaItem::ItemKind::Path: {
-			auto path
-			  = static_cast<AST::MetaItemPath *> (meta_item);
-			result.push_back (path->get_path ());
-		      }
-		      break;
-		      case AST::MetaItem::ItemKind::Word: {
-			auto word = static_cast<AST::MetaWord *> (meta_item);
-			// Convert current word to path
-			current
-			  = make_unique<AST::MetaItemPath> (AST::MetaItemPath (
-			    AST::SimplePath (word->get_ident ())));
-			auto path
-			  = static_cast<AST::MetaItemPath *> (current.get ());
-
-			result.push_back (path->get_path ());
-		      }
-		      break;
-		    case AST::MetaItem::ItemKind::ListPaths:
-		    case AST::MetaItem::ItemKind::NameValueStr:
-		    case AST::MetaItem::ItemKind::PathLit:
-		    case AST::MetaItem::ItemKind::Seq:
-		    case AST::MetaItem::ItemKind::ListNameValueStr:
-		    default:
-		      gcc_unreachable ();
-		      break;
-		    }
-		}
-		break;
-	      case AST::MetaItemInner::Kind::LitExpr:
-	      default:
-		gcc_unreachable ();
-		break;
-	      }
-	  }
-      }
-      break;
-    case AST::AttrInput::TOKEN_TREE:
-    case AST::AttrInput::LITERAL:
-    case AST::AttrInput::MACRO:
-      rust_unreachable ();
-      break;
-    }
-  return result;
-}
-
 static std::unique_ptr<AST::Item>
 builtin_derive_item (AST::Item &item, const AST::Attribute &derive,
 		     BuiltinMacro to_derive)
@@ -253,7 +181,7 @@ ExpandVisitor::expand_inner_items (
 		  current.parse_attr_to_meta_item ();
 		  attr_it = attrs.erase (attr_it);
 		  // Get traits to derive in the current attribute
-		  auto traits_to_derive = get_traits_to_derive (current);
+		  auto traits_to_derive = current.get_traits_to_derive ();
 		  for (auto &to_derive : traits_to_derive)
 		    {
 		      auto maybe_builtin = MacroBuiltin::builtins.lookup (
@@ -339,7 +267,7 @@ ExpandVisitor::expand_inner_stmts (AST::BlockExpr &expr)
 		{
 		  attr_it = attrs.erase (attr_it);
 		  // Get traits to derive in the current attribute
-		  auto traits_to_derive = get_traits_to_derive (current);
+		  auto traits_to_derive = current.get_traits_to_derive ();
 		  for (auto &to_derive : traits_to_derive)
 		    {
 		      auto maybe_builtin = MacroBuiltin::builtins.lookup (


More information about the Gcc-cvs mailing list