]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Set traits getter as member function
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fri, 28 Jul 2023 15:55:55 +0000 (17:55 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:04:32 +0000 (19:04 +0100)
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>
gcc/rust/ast/rust-ast.cc
gcc/rust/ast/rust-ast.h
gcc/rust/expand/rust-expand-visitor.cc

index 434c2768bc57b726927dcf2f8ed614136e861d89..076f40e338b7b07c77c36b4e5073e4cdcff17c1f 100644 (file)
@@ -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)
index 08b511f4019abafe94a1c303e087cfbecb98ecf8..32732ffdbd75a5b2087a8002fce3717bee10f889 100644 (file)
@@ -519,6 +519,8 @@ public:
 
   bool is_derive () const;
 
+  std::vector<AST::SimplePath> get_traits_to_derive ();
+
   // default destructor
   ~Attribute () = default;
 
index 77dfdcb6d6a1404e55130136e8ab8dbb8a819553..f75069b2e0a57e1f82578b4e7fdc430250e19bb6 100644 (file)
@@ -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 (