Skip to content

Commit

Permalink
Create base class AssociatedItem
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
powerboat9 authored and CohenArthur committed Oct 18, 2023
1 parent b3f02d9 commit cdd548b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 72 deletions.
63 changes: 31 additions & 32 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1446,82 +1446,81 @@ 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<AssociatedItem> clone_associated_item () const
{
return std::unique_ptr<AssociatedItem> (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)
: node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus)
{}

// 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<TraitItem> clone_trait_item () const
{
return std::unique_ptr<TraitItem> (clone_trait_item_impl ());
return std::unique_ptr<TraitItem> (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<InherentImplItem> clone_inherent_impl_item () const
{
return std::unique_ptr<InherentImplItem> (clone_inherent_impl_item_impl ());
return std::unique_ptr<InherentImplItem> (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<TraitImplItem> clone_trait_impl_item () const
{
return std::unique_ptr<TraitImplItem> (clone_trait_impl_item_impl ());
return std::unique_ptr<TraitImplItem> (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
Expand Down
37 changes: 8 additions & 29 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 1 addition & 11 deletions gcc/rust/ast/rust-macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cdd548b

Please sign in to comment.