From cdd548b64918100e07c78dc7467d68383977a7d2 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Wed, 11 Oct 2023 12:58:27 -0400 Subject: [PATCH] Create base class AssociatedItem gcc/rust/ChangeLog: * ast/rust-ast.h (class AssociatedItem): New, based on TraitItem, InherentImplItem, and TraitImplItem classes. (class TraitItem): Inherit from AssociatedItem. (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. Signed-off-by: Owen Avery --- gcc/rust/ast/rust-ast.h | 63 +++++++++++++++++++-------------------- gcc/rust/ast/rust-item.h | 37 +++++------------------ gcc/rust/ast/rust-macro.h | 12 +------- 3 files changed, 40 insertions(+), 72 deletions(-) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 35c3f7a9dbb9..e5cadb673148 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1446,8 +1446,30 @@ class LifetimeParam : public GenericParam } }; +class AssociatedItem : public Visitable +{ +protected: + // Clone function implementation as pure virtual method + virtual AssociatedItem *clone_associated_item_impl () const = 0; + +public: + virtual ~AssociatedItem () {} + + std::unique_ptr clone_associated_item () const + { + return std::unique_ptr (clone_associated_item_impl ()); + } + + virtual std::string as_string () const = 0; + + virtual void mark_for_strip () = 0; + virtual bool is_marked_for_strip () const = 0; + + virtual location_t get_locus () const = 0; +}; + // Item used in trait declarations - abstract base class -class TraitItem : public Visitable +class TraitItem : virtual public AssociatedItem { protected: TraitItem (location_t locus) @@ -1455,73 +1477,50 @@ 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; public: - virtual ~TraitItem () {} - // 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; - - virtual void mark_for_strip () = 0; - virtual bool is_marked_for_strip () const = 0; - NodeId get_node_id () const { return node_id; } location_t get_locus () const { return locus; } }; /* Abstract base class for items used within an inherent impl block (the impl * name {} one) */ -class InherentImplItem : public Visitable +class InherentImplItem : virtual 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 () {} - // 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; - - virtual void mark_for_strip () = 0; - virtual bool is_marked_for_strip () const = 0; - - virtual location_t get_locus () const = 0; }; // Abstract base class for items used in a trait impl -class TraitImplItem : public Visitable +class TraitImplItem : virtual public AssociatedItem { protected: - virtual TraitImplItem *clone_trait_impl_item_impl () const = 0; + virtual TraitImplItem *clone_associated_item_impl () const override = 0; public: - virtual ~TraitImplItem (){}; - // 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; - - virtual void mark_for_strip () = 0; - virtual bool is_marked_for_strip () const = 0; }; // Abstract base class for an item used inside an extern block 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 c7f9025acf80..07991649a936 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -804,20 +804,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