From 23305133817a0985e7439e4908e4e503c7a1bb80 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Wed, 11 Oct 2023 12:58:27 -0400 Subject: [PATCH] Unify cloning of associated items gcc/rust/ChangeLog: * ast/rust-ast.h (class AssociatedItem): New class with virtual cloning function. (class TraitItem): Inherit from AssociatedItem, rename cloning function to match. (class InherentImplItem): Likewise. (class TraitImplItem): Likewise. * ast/rust-item.h (class Method): Update cloning functions. (class Function): Likewise. (class TypeAlias): Likewise. (class ConstantItem): Likewise. (class TraitItemFunc): Likewise. (class TraitItemMethod): Likewise. (class TraitItemConst): Likewise. (class TraitItemType): Likewise. * ast/rust-macro.h (class MacroInvocation): Likewise. --- gcc/rust/ast/rust-ast.h | 25 ++++++++++++++++--------- gcc/rust/ast/rust-item.h | 37 ++++++++----------------------------- gcc/rust/ast/rust-macro.h | 12 +----------- 3 files changed, 25 insertions(+), 49 deletions(-) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index db55f011faac..d39c63300b0d 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1446,8 +1446,15 @@ class LifetimeParam : public GenericParam } }; +class AssociatedItem : public Visitable +{ +protected: + // Clone function implementation as pure virtual method + virtual AssociatedItem *clone_associated_item_impl () const = 0; +}; + // Item used in trait declarations - abstract base class -class TraitItem : public Visitable +class TraitItem : public AssociatedItem { protected: TraitItem (location_t locus) @@ -1455,7 +1462,7 @@ class TraitItem : public Visitable {} // Clone function implementation as pure virtual method - virtual TraitItem *clone_trait_item_impl () const = 0; + virtual TraitItem *clone_associated_item_impl () const override = 0; NodeId node_id; location_t locus; @@ -1466,7 +1473,7 @@ class TraitItem : public Visitable // Unique pointer custom clone function std::unique_ptr clone_trait_item () const { - return std::unique_ptr (clone_trait_item_impl ()); + return std::unique_ptr (clone_associated_item_impl ()); } virtual std::string as_string () const = 0; @@ -1480,11 +1487,11 @@ class TraitItem : public Visitable /* Abstract base class for items used within an inherent impl block (the impl * name {} one) */ -class InherentImplItem : public Visitable +class InherentImplItem : public AssociatedItem { protected: // Clone function implementation as pure virtual method - virtual InherentImplItem *clone_inherent_impl_item_impl () const = 0; + virtual InherentImplItem *clone_associated_item_impl () const override = 0; public: virtual ~InherentImplItem () {} @@ -1492,7 +1499,7 @@ class InherentImplItem : public Visitable // Unique pointer custom clone function std::unique_ptr clone_inherent_impl_item () const { - return std::unique_ptr (clone_inherent_impl_item_impl ()); + return std::unique_ptr (clone_associated_item_impl ()); } virtual std::string as_string () const = 0; @@ -1504,10 +1511,10 @@ class InherentImplItem : public Visitable }; // Abstract base class for items used in a trait impl -class TraitImplItem : public Visitable +class TraitImplItem : public AssociatedItem { protected: - virtual TraitImplItem *clone_trait_impl_item_impl () const = 0; + virtual TraitImplItem *clone_associated_item_impl () const override = 0; public: virtual ~TraitImplItem (){}; @@ -1515,7 +1522,7 @@ class TraitImplItem : public Visitable // Unique pointer custom clone function std::unique_ptr clone_trait_impl_item () const { - return std::unique_ptr (clone_trait_impl_item_impl ()); + return std::unique_ptr (clone_associated_item_impl ()); } virtual std::string as_string () const = 0; diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index aae054969633..e842f98655cb 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -923,14 +923,7 @@ class Method : public InherentImplItem, public TraitImplItem protected: /* Use covariance to implement clone function as returning this object * rather than base */ - Method *clone_inherent_impl_item_impl () const final override - { - return clone_method_impl (); - } - - /* Use covariance to implement clone function as returning this object - * rather than base */ - Method *clone_trait_impl_item_impl () const final override + Method *clone_associated_item_impl () const final override { return clone_method_impl (); } @@ -1703,14 +1696,7 @@ class Function : public VisItem, public InherentImplItem, public TraitImplItem /* Use covariance to implement clone function as returning this object * rather than base */ - Function *clone_inherent_impl_item_impl () const override - { - return new Function (*this); - } - - /* Use covariance to implement clone function as returning this object - * rather than base */ - Function *clone_trait_impl_item_impl () const override + Function *clone_associated_item_impl () const override { return new Function (*this); } @@ -1834,7 +1820,7 @@ class TypeAlias : public VisItem, public TraitImplItem /* Use covariance to implement clone function as returning this object * rather than base */ - TypeAlias *clone_trait_impl_item_impl () const override + TypeAlias *clone_associated_item_impl () const override { return new TypeAlias (*this); } @@ -2699,14 +2685,7 @@ class ConstantItem : public VisItem, /* Use covariance to implement clone function as returning this object * rather than base */ - ConstantItem *clone_inherent_impl_item_impl () const override - { - return new ConstantItem (*this); - } - - /* Use covariance to implement clone function as returning this object - * rather than base */ - ConstantItem *clone_trait_impl_item_impl () const override + ConstantItem *clone_associated_item_impl () const override { return new ConstantItem (*this); } @@ -3016,7 +2995,7 @@ class TraitItemFunc : public TraitItem protected: // Clone function implementation as (not pure) virtual method - TraitItemFunc *clone_trait_item_impl () const override + TraitItemFunc *clone_associated_item_impl () const override { return new TraitItemFunc (*this); } @@ -3232,7 +3211,7 @@ class TraitItemMethod : public TraitItem protected: // Clone function implementation as (not pure) virtual method - TraitItemMethod *clone_trait_item_impl () const override + TraitItemMethod *clone_associated_item_impl () const override { return new TraitItemMethod (*this); } @@ -3337,7 +3316,7 @@ class TraitItemConst : public TraitItem protected: // Clone function implementation as (not pure) virtual method - TraitItemConst *clone_trait_item_impl () const override + TraitItemConst *clone_associated_item_impl () const override { return new TraitItemConst (*this); } @@ -3424,7 +3403,7 @@ class TraitItemType : public TraitItem protected: // Clone function implementation as (not pure) virtual method - TraitItemType *clone_trait_item_impl () const override + TraitItemType *clone_associated_item_impl () const override { return new TraitItemType (*this); } diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 27d3134ec2a1..d5621eb2c961 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -803,20 +803,10 @@ class MacroInvocation : public TypeNoBounds, bool is_item () const override { return !has_semicolon (); } - TraitItem *clone_trait_item_impl () const override + MacroInvocation *clone_associated_item_impl () const override { return clone_macro_invocation_impl (); }; - - TraitImplItem *clone_trait_impl_item_impl () const override - { - return clone_macro_invocation_impl (); - }; - - InherentImplItem *clone_inherent_impl_item_impl () const override - { - return clone_macro_invocation_impl (); - } }; // more generic meta item path-only form