[COMMITTED 100/101] gccrs: Remove TraitImplItem

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Tue Jan 30 12:07:56 GMT 2024


From: Owen Avery <powerboat9.gamer@gmail.com>

gcc/rust/ChangeLog:

	* ast/rust-ast-full-decls.h
	(class TraitImplItem): Remove forward declaration.
	(class AssociatedItem): Add forward declaration.
	* ast/rust-ast.h
	(class TraitImplItem): Remove.
	(class TraitItem): Inherit from AssociatedItem.
	(SingleASTNode::take_trait_impl_item):
	Return std::unique_ptr<AssociatedItem> instead of
	std::unique_ptr<TraitImplItem>.
	* ast/rust-item.h
	(class Function): Inherit from AssociatedItem instead of
	TraitImplItem.
	(class TypeAlias): Likewise.
	(class ConstantItem): Likewise.
	(class TraitImpl): Store items as AssociatedItem.
	* expand/rust-derive-clone.cc
	(DeriveClone::clone_fn): Return std::unique_ptr<AssociatedItem>.
	(DeriveClone::clone_impl): Take std::unique_ptr<AssociatedItem>.
	* expand/rust-derive-clone.h
	(DeriveClone::clone_fn): Return std::unique_ptr<AssociatedItem>.
	(DeriveClone::clone_impl): Take std::unique_ptr<AssociatedItem>.
	* expand/rust-expand-visitor.cc
	(ExpandVisitor::visit): Handle changes to
	SingleASTNode::take_trait_impl_item.
	* parse/rust-parse-impl.h
	(Parser::parse_impl): Parse TraitImpl as containing AssociatedItem.
	(Parser::parse_trait_impl_item): Return
	std::unique_ptr<AssociatedItem>.
	(Parser::parse_trait_impl_function_or_method): Likewise.
	* parse/rust-parse.h
	(Parser::parse_trait_impl_item): Return
	std::unique_ptr<AssociatedItem>.
	(Parser::parse_trait_impl_function_or_method): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
---
 gcc/rust/ast/rust-ast-full-decls.h     |  2 +-
 gcc/rust/ast/rust-ast.h                | 22 +++-------------------
 gcc/rust/ast/rust-item.h               | 18 +++++++++---------
 gcc/rust/expand/rust-derive-clone.cc   |  8 ++++----
 gcc/rust/expand/rust-derive-clone.h    |  4 ++--
 gcc/rust/expand/rust-expand-visitor.cc |  2 +-
 gcc/rust/parse/rust-parse-impl.h       |  8 ++++----
 gcc/rust/parse/rust-parse.h            |  4 ++--
 8 files changed, 26 insertions(+), 42 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-full-decls.h b/gcc/rust/ast/rust-ast-full-decls.h
index 5bfaaa8ba3c..0f95149a214 100644
--- a/gcc/rust/ast/rust-ast-full-decls.h
+++ b/gcc/rust/ast/rust-ast-full-decls.h
@@ -52,7 +52,7 @@ class GenericParam;
 class LifetimeParam;
 class ConstGenericParam;
 class TraitItem;
-class TraitImplItem;
+class AssociatedItem;
 struct Crate;
 class PathExpr;
 
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 256a527118a..e96ac349733 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -1657,22 +1657,8 @@ public:
   virtual location_t get_locus () const = 0;
 };
 
-// Abstract base class for items used in a trait impl
-class TraitImplItem : public AssociatedItem
-{
-protected:
-  virtual TraitImplItem *clone_associated_item_impl () const override = 0;
-
-public:
-  // Unique pointer custom clone function
-  std::unique_ptr<TraitImplItem> clone_trait_impl_item () const
-  {
-    return std::unique_ptr<TraitImplItem> (clone_associated_item_impl ());
-  }
-};
-
 // Item used in trait declarations - abstract base class
-class TraitItem : public TraitImplItem
+class TraitItem : public AssociatedItem
 {
 protected:
   TraitItem (location_t locus)
@@ -1945,11 +1931,9 @@ public:
     return take_assoc_item ();
   }
 
-  std::unique_ptr<TraitImplItem> take_trait_impl_item ()
+  std::unique_ptr<AssociatedItem> take_trait_impl_item ()
   {
-    rust_assert (!is_error ());
-    return std::unique_ptr<TraitImplItem> (
-      static_cast<TraitImplItem *> (assoc_item.release ()));
+    return take_assoc_item ();
   }
 
   std::unique_ptr<Type> take_type ()
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 5b9eb64560d..1553f29ad2d 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -1289,7 +1289,7 @@ protected:
 class LetStmt;
 
 // Rust function declaration AST node
-class Function : public VisItem, public TraitImplItem
+class Function : public VisItem, public AssociatedItem
 {
   FunctionQualifiers qualifiers;
   Identifier function_name;
@@ -1436,7 +1436,7 @@ protected:
 };
 
 // Rust type alias (i.e. typedef) AST node
-class TypeAlias : public VisItem, public TraitImplItem
+class TypeAlias : public VisItem, public AssociatedItem
 {
   Identifier new_type_name;
 
@@ -2312,7 +2312,7 @@ protected:
 
 /* "Constant item" AST node - used for constant, compile-time expressions
  * within module scope (like constexpr) */
-class ConstantItem : public VisItem, public TraitImplItem
+class ConstantItem : public VisItem, public AssociatedItem
 {
   // either has an identifier or "_" - maybe handle in identifier?
   // bool identifier_is_underscore;
@@ -3484,7 +3484,7 @@ class TraitImpl : public Impl
   TypePath trait_path;
 
   // bool has_impl_items;
-  std::vector<std::unique_ptr<TraitImplItem>> impl_items;
+  std::vector<std::unique_ptr<AssociatedItem>> impl_items;
 
 public:
   std::string as_string () const override;
@@ -3494,7 +3494,7 @@ public:
 
   // Mega-constructor
   TraitImpl (TypePath trait_path, bool is_unsafe, bool has_exclam,
-	     std::vector<std::unique_ptr<TraitImplItem>> impl_items,
+	     std::vector<std::unique_ptr<AssociatedItem>> impl_items,
 	     std::vector<std::unique_ptr<GenericParam>> generic_params,
 	     std::unique_ptr<Type> trait_type, WhereClause where_clause,
 	     Visibility vis, std::vector<Attribute> inner_attrs,
@@ -3513,7 +3513,7 @@ public:
   {
     impl_items.reserve (other.impl_items.size ());
     for (const auto &e : other.impl_items)
-      impl_items.push_back (e->clone_trait_impl_item ());
+      impl_items.push_back (e->clone_associated_item ());
   }
 
   // Overloaded assignment operator with vector clone
@@ -3526,7 +3526,7 @@ public:
 
     impl_items.reserve (other.impl_items.size ());
     for (const auto &e : other.impl_items)
-      impl_items.push_back (e->clone_trait_impl_item ());
+      impl_items.push_back (e->clone_associated_item ());
 
     return *this;
   }
@@ -3541,11 +3541,11 @@ public:
   bool is_exclam () const { return has_exclam; }
 
   // TODO: think of better way to do this
-  const std::vector<std::unique_ptr<TraitImplItem>> &get_impl_items () const
+  const std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items () const
   {
     return impl_items;
   }
-  std::vector<std::unique_ptr<TraitImplItem>> &get_impl_items ()
+  std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items ()
   {
     return impl_items;
   }
diff --git a/gcc/rust/expand/rust-derive-clone.cc b/gcc/rust/expand/rust-derive-clone.cc
index cc198eecf5d..01226e4f913 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -43,7 +43,7 @@ DeriveClone::clone_call (std::unique_ptr<Expr> &&to_clone)
  * fn clone(&self) -> Self { <clone_expr> }
  *
  */
-std::unique_ptr<TraitImplItem>
+std::unique_ptr<AssociatedItem>
 DeriveClone::clone_fn (std::unique_ptr<Expr> &&clone_expr)
 {
   auto block = std::unique_ptr<BlockExpr> (
@@ -57,7 +57,7 @@ DeriveClone::clone_fn (std::unique_ptr<Expr> &&clone_expr)
   std::vector<std::unique_ptr<Param>> params;
   params.push_back (std::move (self));
 
-  return std::unique_ptr<TraitImplItem> (
+  return std::unique_ptr<AssociatedItem> (
     new Function ({"clone"}, builder.fn_qualifiers (), /* generics */ {},
 		  /* function params */ std::move (params),
 		  std::move (big_self_type), WhereClause::create_empty (),
@@ -73,7 +73,7 @@ DeriveClone::clone_fn (std::unique_ptr<Expr> &&clone_expr)
  *
  */
 std::unique_ptr<Item>
-DeriveClone::clone_impl (std::unique_ptr<TraitImplItem> &&clone_fn,
+DeriveClone::clone_impl (std::unique_ptr<AssociatedItem> &&clone_fn,
 			 std::string name)
 {
   // should that be `$crate::core::clone::Clone` instead?
@@ -81,7 +81,7 @@ DeriveClone::clone_impl (std::unique_ptr<TraitImplItem> &&clone_fn,
   segments.emplace_back (builder.type_path_segment ("Clone"));
   auto clone = TypePath (std::move (segments), loc);
 
-  auto trait_items = std::vector<std::unique_ptr<TraitImplItem>> ();
+  auto trait_items = std::vector<std::unique_ptr<AssociatedItem>> ();
   trait_items.emplace_back (std::move (clone_fn));
 
   return std::unique_ptr<Item> (
diff --git a/gcc/rust/expand/rust-derive-clone.h b/gcc/rust/expand/rust-derive-clone.h
index dcb88f9468a..1009247e882 100644
--- a/gcc/rust/expand/rust-derive-clone.h
+++ b/gcc/rust/expand/rust-derive-clone.h
@@ -49,7 +49,7 @@ private:
    * fn clone(&self) -> Self { <clone_expr> }
    *
    */
-  std::unique_ptr<TraitImplItem> clone_fn (std::unique_ptr<Expr> &&clone_expr);
+  std::unique_ptr<AssociatedItem> clone_fn (std::unique_ptr<Expr> &&clone_expr);
 
   /**
    * Create the Clone trait implementation for a type
@@ -59,7 +59,7 @@ private:
    * }
    *
    */
-  std::unique_ptr<Item> clone_impl (std::unique_ptr<TraitImplItem> &&clone_fn,
+  std::unique_ptr<Item> clone_impl (std::unique_ptr<AssociatedItem> &&clone_fn,
 				    std::string name);
 
   virtual void visit_struct (StructStruct &item);
diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index a60c4728926..dad2417eac4 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -957,7 +957,7 @@ ExpandVisitor::visit (AST::TraitImpl &impl)
   if (impl.has_where_clause ())
     expand_where_clause (impl.get_where_clause ());
 
-  std::function<std::unique_ptr<AST::TraitImplItem> (AST::SingleASTNode)>
+  std::function<std::unique_ptr<AST::AssociatedItem> (AST::SingleASTNode)>
     extractor
     = [] (AST::SingleASTNode node) { return node.take_trait_impl_item (); };
 
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 7ea7276dc94..c622ed7bc97 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -5492,12 +5492,12 @@ Parser<ManagedTokenSource>::parse_impl (AST::Visibility vis,
       AST::AttrVec inner_attrs = parse_inner_attributes ();
 
       // parse trait impl items
-      std::vector<std::unique_ptr<AST::TraitImplItem>> impl_items;
+      std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
 
       const_TokenPtr t = lexer.peek_token ();
       while (t->get_id () != RIGHT_CURLY)
 	{
-	  std::unique_ptr<AST::TraitImplItem> impl_item
+	  std::unique_ptr<AST::AssociatedItem> impl_item
 	    = parse_trait_impl_item ();
 
 	  if (impl_item == nullptr)
@@ -5759,7 +5759,7 @@ Parser<ManagedTokenSource>::parse_inherent_impl_function_or_method (
 
 // Parses a single trait impl item (item inside a trait impl block).
 template <typename ManagedTokenSource>
-std::unique_ptr<AST::TraitImplItem>
+std::unique_ptr<AST::AssociatedItem>
 Parser<ManagedTokenSource>::parse_trait_impl_item ()
 {
   // parse outer attributes (if they exist)
@@ -5836,7 +5836,7 @@ Parser<ManagedTokenSource>::parse_trait_impl_item ()
  * smaller ones and prevents duplication of logic. Strictly, this parses a
  * function or method item inside a trait impl item block. */
 template <typename ManagedTokenSource>
-std::unique_ptr<AST::TraitImplItem>
+std::unique_ptr<AST::AssociatedItem>
 Parser<ManagedTokenSource>::parse_trait_impl_function_or_method (
   AST::Visibility vis, AST::AttrVec outer_attrs)
 {
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index 02f90217fc2..34397540321 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -171,7 +171,7 @@ public:
   std::unique_ptr<AST::ExternalItem> parse_external_item ();
   std::unique_ptr<AST::TraitItem> parse_trait_item ();
   std::unique_ptr<AST::AssociatedItem> parse_inherent_impl_item ();
-  std::unique_ptr<AST::TraitImplItem> parse_trait_impl_item ();
+  std::unique_ptr<AST::AssociatedItem> parse_trait_impl_item ();
   AST::PathInExpression parse_path_in_expression ();
   std::vector<std::unique_ptr<AST::LifetimeParam>> parse_lifetime_params ();
   AST::Visibility parse_visibility ();
@@ -353,7 +353,7 @@ private:
   std::unique_ptr<AST::AssociatedItem>
   parse_inherent_impl_function_or_method (AST::Visibility vis,
 					  AST::AttrVec outer_attrs);
-  std::unique_ptr<AST::TraitImplItem>
+  std::unique_ptr<AST::AssociatedItem>
   parse_trait_impl_function_or_method (AST::Visibility vis,
 				       AST::AttrVec outer_attrs);
   std::unique_ptr<AST::ExternBlock>
-- 
2.42.1



More information about the Gcc-rust mailing list