Skip to content

Commit

Permalink
ast: Add EnumItem::Kind
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* ast/rust-item.h: Add EnumItem::Kind for differentiating all variants that may be
	used inside an enum declaration.
  • Loading branch information
CohenArthur committed Jan 2, 2025
1 parent db11825 commit 766f587
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,41 @@ class EnumItem : public VisItem
location_t locus;

public:
enum class Kind
{
Identifier,
Tuple,
Struct,

// FIXME: In the future, we'll need to remove this possibility as well as
// remove the EnumItemDiscriminant class. The feature for arbitrary
// discriminants on all kinds of variants has been stabilized, and a
// "discriminant" is no longer an enum item variant - it's simply an
// optional part of all variants.
//
// Per the reference:
//
// EnumItem :
// OuterAttribute* Visibility?
// IDENTIFIER ( EnumItemTuple | EnumItemStruct )? EnumItemDiscriminant?
//
// EnumItemTuple :
// ( TupleFields? )
//
// EnumItemStruct :
// { StructFields? }
//
// EnumItemDiscriminant :
// = Expression
//
// So we instead need to remove the class, and add an optional expression to
// the base EnumItem class
//
// gccrs#3340

Discriminant,
};

virtual ~EnumItem () {}

EnumItem (Identifier variant_name, Visibility vis,
Expand All @@ -2002,6 +2037,8 @@ class EnumItem : public VisItem
variant_name (std::move (variant_name)), locus (locus)
{}

virtual Kind get_enum_item_kind () const { return Kind::Identifier; }

// Unique pointer custom clone function
std::unique_ptr<EnumItem> clone_enum_item () const
{
Expand Down Expand Up @@ -2043,6 +2080,11 @@ class EnumItemTuple : public EnumItem
tuple_fields (std::move (tuple_fields))
{}

EnumItem::Kind get_enum_item_kind () const override
{
return EnumItem::Kind::Tuple;
}

std::string as_string () const override;

void accept_vis (ASTVisitor &vis) override;
Expand Down Expand Up @@ -2080,6 +2122,11 @@ class EnumItemStruct : public EnumItem
struct_fields (std::move (struct_fields))
{}

EnumItem::Kind get_enum_item_kind () const override
{
return EnumItem::Kind::Struct;
}

std::string as_string () const override;

void accept_vis (ASTVisitor &vis) override;
Expand Down Expand Up @@ -2133,6 +2180,11 @@ class EnumItemDiscriminant : public EnumItem
EnumItemDiscriminant (EnumItemDiscriminant &&other) = default;
EnumItemDiscriminant &operator= (EnumItemDiscriminant &&other) = default;

EnumItem::Kind get_enum_item_kind () const override
{
return EnumItem::Kind::Discriminant;
}

std::string as_string () const override;

void accept_vis (ASTVisitor &vis) override;
Expand Down

0 comments on commit 766f587

Please sign in to comment.