Skip to content

Commit

Permalink
Remove TraitImplItem
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
powerboat9 authored and P-E-P committed Jan 24, 2024
1 parent 7b203f6 commit 9a62272
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 42 deletions.
2 changes: 1 addition & 1 deletion gcc/rust/ast/rust-ast-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class GenericParam;
class LifetimeParam;
class ConstGenericParam;
class TraitItem;
class TraitImplItem;
class AssociatedItem;
struct Crate;
class PathExpr;

Expand Down
22 changes: 3 additions & 19 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1657,22 +1657,8 @@ class AssociatedItem : public Visitable
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)
Expand Down Expand Up @@ -1945,11 +1931,9 @@ class SingleASTNode : public Visitable
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 ()
Expand Down
18 changes: 9 additions & 9 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ class UseDeclaration : public VisItem
class LetStmt;

// Rust function declaration AST node
class Function : public VisItem, public TraitImplItem
class Function : public VisItem, public AssociatedItem
{
FunctionQualifiers qualifiers;
Identifier function_name;
Expand Down Expand Up @@ -1436,7 +1436,7 @@ class Function : public VisItem, public TraitImplItem
};

// Rust type alias (i.e. typedef) AST node
class TypeAlias : public VisItem, public TraitImplItem
class TypeAlias : public VisItem, public AssociatedItem
{
Identifier new_type_name;

Expand Down Expand Up @@ -2312,7 +2312,7 @@ class Union : public VisItem

/* "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;
Expand Down Expand Up @@ -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;
Expand All @@ -3494,7 +3494,7 @@ class TraitImpl : public Impl

// 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,
Expand All @@ -3513,7 +3513,7 @@ class TraitImpl : public Impl
{
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
Expand All @@ -3526,7 +3526,7 @@ class TraitImpl : public Impl

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;
}
Expand All @@ -3541,11 +3541,11 @@ class TraitImpl : public Impl
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;
}
Expand Down
8 changes: 4 additions & 4 deletions gcc/rust/expand/rust-derive-clone.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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> (
Expand All @@ -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 (),
Expand All @@ -73,15 +73,15 @@ 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?
auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
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> (
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/expand/rust-derive-clone.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DeriveClone : DeriveVisitor
* 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
Expand All @@ -59,7 +59,7 @@ class DeriveClone : DeriveVisitor
* }
*
*/
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);
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/expand/rust-expand-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 (); };

Expand Down
8 changes: 4 additions & 4 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/parse/rust-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ template <typename ManagedTokenSource> class Parser
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 ();
Expand Down Expand Up @@ -353,7 +353,7 @@ template <typename ManagedTokenSource> class Parser
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>
Expand Down

0 comments on commit 9a62272

Please sign in to comment.