Skip to content

Commit

Permalink
Unify storage of associated items in SingleASTNode
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* ast/rust-ast-fragment.cc
	(Fragment::assert_single_fragment): Update.

	* ast/rust-ast.h
	(class TraitImplItem): Move definition before that of TraitItem.
	(class TraitItem):
	Inherit from TraitImplItem instead of AssociatedItem.

	(class SingleASTNode): Unify handling of associated items.
	(SingleASTNode::take_assoc_item): Move from...
	(SingleASTNode::take_impl_item): ...here, but leave stub calling
	take_assoc_item behind.
	(SingleASTNode::take_trait_item):
	Cast associated item to TraitItem.
	(SingleASTNode::take_trait_impl_item):
	Cast associated item to TraitImplItem.

	* ast/rust-ast.cc
	(SingleASTNode::SingleASTNode):
	Unify handling of associated items.
	(SingleASTNode::operator=): Likewise.
	(SingleASTNode::accept_vis): Likewise.
	(SingleASTNode::is_error): Likewise.
	(SingleASTNode::as_string): Likewise.

	* ast/rust-item.h
	(class Function): Remove direct inheritence from AssociatedItem.
	(class ConstantItem): Likewise.
	* ast/rust-macro.h
	(class MacroInvocation):
	Remove direct inheritence from AssociatedItem and TraitImplItem.
  • Loading branch information
powerboat9 authored and P-E-P committed Jan 18, 2024
1 parent 972a4f0 commit 43bb497
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 87 deletions.
4 changes: 1 addition & 3 deletions gcc/rust/ast/rust-ast-fragment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,12 @@ void
Fragment::assert_single_fragment (SingleASTNode::NodeType expected) const
{
static const std::map<SingleASTNode::NodeType, const char *> str_map = {
{SingleASTNode::NodeType::IMPL, "impl"},
{SingleASTNode::NodeType::ASSOC_ITEM, "associated item"},
{SingleASTNode::NodeType::ITEM, "item"},
{SingleASTNode::NodeType::TYPE, "type"},
{SingleASTNode::NodeType::EXPRESSION, "expr"},
{SingleASTNode::NodeType::STMT, "stmt"},
{SingleASTNode::NodeType::EXTERN, "extern"},
{SingleASTNode::NodeType::TRAIT, "trait"},
{SingleASTNode::NodeType::TRAIT_IMPL, "trait impl"},
};

auto actual = nodes[0].get_kind ();
Expand Down
52 changes: 10 additions & 42 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,8 @@ SingleASTNode::SingleASTNode (SingleASTNode const &other)
external_item = other.external_item->clone_external_item ();
break;

case TRAIT:
trait_item = other.trait_item->clone_trait_item ();
break;

case IMPL:
impl_item = other.impl_item->clone_associated_item ();
break;

case TRAIT_IMPL:
trait_impl_item = other.trait_impl_item->clone_trait_impl_item ();
case ASSOC_ITEM:
assoc_item = other.assoc_item->clone_associated_item ();
break;

case TYPE:
Expand Down Expand Up @@ -100,16 +92,8 @@ SingleASTNode::operator= (SingleASTNode const &other)
external_item = other.external_item->clone_external_item ();
break;

case TRAIT:
trait_item = other.trait_item->clone_trait_item ();
break;

case IMPL:
impl_item = other.impl_item->clone_associated_item ();
break;

case TRAIT_IMPL:
trait_impl_item = other.trait_impl_item->clone_trait_impl_item ();
case ASSOC_ITEM:
assoc_item = other.assoc_item->clone_associated_item ();
break;

case TYPE:
Expand Down Expand Up @@ -140,16 +124,8 @@ SingleASTNode::accept_vis (ASTVisitor &vis)
external_item->accept_vis (vis);
break;

case TRAIT:
trait_item->accept_vis (vis);
break;

case IMPL:
impl_item->accept_vis (vis);
break;

case TRAIT_IMPL:
trait_impl_item->accept_vis (vis);
case ASSOC_ITEM:
assoc_item->accept_vis (vis);
break;

case TYPE:
Expand All @@ -171,12 +147,8 @@ SingleASTNode::is_error ()
return stmt == nullptr;
case EXTERN:
return external_item == nullptr;
case TRAIT:
return trait_item == nullptr;
case IMPL:
return impl_item == nullptr;
case TRAIT_IMPL:
return trait_impl_item == nullptr;
case ASSOC_ITEM:
return assoc_item == nullptr;
case TYPE:
return type == nullptr;
}
Expand All @@ -198,12 +170,8 @@ SingleASTNode::as_string () const
return "Stmt: " + stmt->as_string ();
case EXTERN:
return "External Item: " + external_item->as_string ();
case TRAIT:
return "Trait Item: " + trait_item->as_string ();
case IMPL:
return "Impl Item: " + impl_item->as_string ();
case TRAIT_IMPL:
return "Trait Impl Item: " + trait_impl_item->as_string ();
case ASSOC_ITEM:
return "Associated Item: " + assoc_item->as_string ();
case TYPE:
return "Type: " + type->as_string ();
}
Expand Down
63 changes: 29 additions & 34 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1657,8 +1657,22 @@ 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 : virtual public AssociatedItem
class TraitItem : public TraitImplItem
{
protected:
TraitItem (location_t locus)
Expand Down Expand Up @@ -1689,20 +1703,6 @@ class TraitItem : virtual public AssociatedItem
location_t get_locus () const override { return locus; }
};

// Abstract base class for items used in a trait impl
class TraitImplItem : virtual 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 ());
}
};

// Abstract base class for an item used inside an extern block
class ExternalItem : public Visitable
{
Expand Down Expand Up @@ -1831,9 +1831,7 @@ class SingleASTNode : public Visitable
ITEM,
STMT,
EXTERN,
TRAIT,
IMPL,
TRAIT_IMPL,
ASSOC_ITEM,
TYPE,
};

Expand All @@ -1845,9 +1843,7 @@ class SingleASTNode : public Visitable
std::unique_ptr<Item> item;
std::unique_ptr<Stmt> stmt;
std::unique_ptr<ExternalItem> external_item;
std::unique_ptr<TraitItem> trait_item;
std::unique_ptr<AssociatedItem> impl_item;
std::unique_ptr<TraitImplItem> trait_impl_item;
std::unique_ptr<AssociatedItem> assoc_item;
std::unique_ptr<Type> type;

public:
Expand All @@ -1867,16 +1863,8 @@ class SingleASTNode : public Visitable
: kind (EXTERN), external_item (std::move (item))
{}

SingleASTNode (std::unique_ptr<TraitItem> item)
: kind (TRAIT), trait_item (std::move (item))
{}

SingleASTNode (std::unique_ptr<AssociatedItem> item)
: kind (IMPL), impl_item (std::move (item))
{}

SingleASTNode (std::unique_ptr<TraitImplItem> trait_impl_item)
: kind (TRAIT_IMPL), trait_impl_item (std::move (trait_impl_item))
: kind (ASSOC_ITEM), assoc_item (std::move (item))
{}

SingleASTNode (std::unique_ptr<Type> type)
Expand Down Expand Up @@ -1936,7 +1924,8 @@ class SingleASTNode : public Visitable
std::unique_ptr<TraitItem> take_trait_item ()
{
rust_assert (!is_error ());
return std::move (trait_item);
return std::unique_ptr<TraitItem> (
static_cast<TraitItem *> (assoc_item.release ()));
}

std::unique_ptr<ExternalItem> take_external_item ()
Expand All @@ -1945,16 +1934,22 @@ class SingleASTNode : public Visitable
return std::move (external_item);
}

std::unique_ptr<AssociatedItem> take_impl_item ()
std::unique_ptr<AssociatedItem> take_assoc_item ()
{
rust_assert (!is_error ());
return std::move (impl_item);
return std::move (assoc_item);
}

std::unique_ptr<AssociatedItem> take_impl_item ()
{
return take_assoc_item ();
}

std::unique_ptr<TraitImplItem> take_trait_impl_item ()
{
rust_assert (!is_error ());
return std::move (trait_impl_item);
return std::unique_ptr<TraitImplItem> (
static_cast<TraitImplItem *> (assoc_item.release ()));
}

std::unique_ptr<Type> take_type ()
Expand Down
8 changes: 2 additions & 6 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1289,9 +1289,7 @@ class UseDeclaration : public VisItem
class LetStmt;

// Rust function declaration AST node
class Function : public VisItem,
virtual public AssociatedItem,
public TraitImplItem
class Function : public VisItem, public TraitImplItem
{
FunctionQualifiers qualifiers;
Identifier function_name;
Expand Down Expand Up @@ -2314,9 +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,
virtual public AssociatedItem,
public TraitImplItem
class ConstantItem : public VisItem, public TraitImplItem
{
// either has an identifier or "_" - maybe handle in identifier?
// bool identifier_is_underscore;
Expand Down
2 changes: 0 additions & 2 deletions gcc/rust/ast/rust-macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,6 @@ class MacroInvocation : public TypeNoBounds,
public Pattern,
public Item,
public TraitItem,
public TraitImplItem,
virtual public AssociatedItem,
public ExternalItem,
public ExprWithoutBlock
{
Expand Down

0 comments on commit 43bb497

Please sign in to comment.