Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed EnumItemDiscriminant and associated functions #3355

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1881,15 +1881,6 @@ TokenCollector::visit (EnumItemStruct &item)
{Rust::Token::make (COMMA, UNDEF_LOCATION)});
}

void
TokenCollector::visit (EnumItemDiscriminant &item)
{
auto id = item.get_identifier ().as_string ();
push (Rust::Token::make_identifier (item.get_locus (), std::move (id)));
push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
visit (item.get_expr ());
}

void
TokenCollector::visit (Enum &enumeration)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ class TokenCollector : public ASTVisitor
void visit (EnumItem &item);
void visit (EnumItemTuple &item);
void visit (EnumItemStruct &item);
void visit (EnumItemDiscriminant &item);
void visit (Enum &enumeration);
void visit (Union &union_item);
void visit (ConstantItem &const_item);
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/ast/rust-ast-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ class TupleStruct;
class EnumItem;
class EnumItemTuple;
class EnumItemStruct;
class EnumItemDiscriminant;
class Enum;
class Union;
class ConstantItem;
Expand Down
7 changes: 0 additions & 7 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -920,13 +920,6 @@ DefaultASTVisitor::visit (AST::EnumItemStruct &item)
visit (field);
}

void
DefaultASTVisitor::visit (AST::EnumItemDiscriminant &item)
{
visit (reinterpret_cast<EnumItem &> (item));
visit (item.get_expr ());
}

void
DefaultASTVisitor::visit (AST::Enum &enum_item)
{
Expand Down
2 changes: 0 additions & 2 deletions gcc/rust/ast/rust-ast-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ class ASTVisitor
virtual void visit (EnumItem &item) = 0;
virtual void visit (EnumItemTuple &item) = 0;
virtual void visit (EnumItemStruct &item) = 0;
virtual void visit (EnumItemDiscriminant &item) = 0;
virtual void visit (Enum &enum_item) = 0;
virtual void visit (Union &union_item) = 0;
virtual void visit (ConstantItem &const_item) = 0;
Expand Down Expand Up @@ -335,7 +334,6 @@ class DefaultASTVisitor : public ASTVisitor
virtual void visit (AST::EnumItem &item) override;
virtual void visit (AST::EnumItemTuple &item) override;
virtual void visit (AST::EnumItemStruct &item) override;
virtual void visit (AST::EnumItemDiscriminant &item) override;
virtual void visit (AST::Enum &enum_item) override;
virtual void visit (AST::Union &union_item) override;
virtual void visit (AST::ConstantItem &const_item) override;
Expand Down
18 changes: 0 additions & 18 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2952,18 +2952,6 @@ StructField::as_string () const
return str;
}

std::string
EnumItemDiscriminant::as_string () const
{
// TODO: rewrite to work with non-linearisable exprs
std::string str = EnumItem::as_string ();

// add equal and expression
str += " = " + expression->as_string ();

return str;
}

std::string
ExternalTypeItem::as_string () const
{
Expand Down Expand Up @@ -4765,12 +4753,6 @@ EnumItemStruct::accept_vis (ASTVisitor &vis)
vis.visit (*this);
}

void
EnumItemDiscriminant::accept_vis (ASTVisitor &vis)
{
vis.visit (*this);
}

void
Enum::accept_vis (ASTVisitor &vis)
{
Expand Down
96 changes: 29 additions & 67 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,8 @@ class EnumItem : public VisItem

location_t locus;

std::unique_ptr<Expr> expression;

Comment on lines +2009 to +2010
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to add handling for this just yet because we're not handling it properly and parsing it for all enum items yet. So I would say remove this

public:
enum class Kind
{
Expand Down Expand Up @@ -2039,7 +2041,6 @@ class EnumItem : public VisItem
//
// gccrs#3340

Discriminant,
};

virtual ~EnumItem () {}
Expand All @@ -2050,6 +2051,19 @@ class EnumItem : public VisItem
variant_name (std::move (variant_name)), locus (locus)
{}

EnumItem (Identifier variant_name, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus,
std::unique_ptr<AST::Expr> Discriminant)
: VisItem (std::move (vis), std::move (outer_attrs)),
variant_name (std::move (variant_name)), locus (locus),
expression (Discriminant->clone_expr ())
{}

EnumItem (EnumItem const &other)
: VisItem (other), variant_name (other.variant_name), locus (other.locus),
expression (other.expression->clone_expr ())
{}

Comment on lines +2054 to +2066
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove these constructors

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

// Unique pointer custom clone function
Expand All @@ -2073,6 +2087,20 @@ class EnumItem : public VisItem

Item::Kind get_item_kind () const override { return Item::Kind::EnumItem; }

bool has_expr () { return expression != nullptr; }

Expr &get_expr ()
{
rust_assert (expression != nullptr);
return *expression;
}

std::unique_ptr<Expr> &get_expr_ptr ()
{
rust_assert (expression != nullptr);
return expression;
}

Comment on lines +2090 to +2103
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need these as well

protected:
EnumItem *clone_item_impl () const override { return new EnumItem (*this); }
};
Expand Down Expand Up @@ -2161,72 +2189,6 @@ class EnumItemStruct : public EnumItem
}
};

// A discriminant (numbered enum) item used in an "enum" tagged union
class EnumItemDiscriminant : public EnumItem
{
std::unique_ptr<Expr> expression;

public:
EnumItemDiscriminant (Identifier variant_name, Visibility vis,
std::unique_ptr<Expr> expr,
std::vector<Attribute> outer_attrs, location_t locus)
: EnumItem (std::move (variant_name), std::move (vis),
std::move (outer_attrs), locus),
expression (std::move (expr))
{}

// Copy constructor with clone
EnumItemDiscriminant (EnumItemDiscriminant const &other)
: EnumItem (other), expression (other.expression->clone_expr ())
{}

// Overloaded assignment operator to clone
EnumItemDiscriminant &operator= (EnumItemDiscriminant const &other)
{
EnumItem::operator= (other);
expression = other.expression->clone_expr ();
// variant_name = other.variant_name;
// outer_attrs = other.outer_attrs;

return *this;
}

// move constructors
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;

bool has_expr () { return expression != nullptr; }

// TODO: is this better? Or is a "vis_block" better?
Expr &get_expr ()
{
rust_assert (expression != nullptr);
return *expression;
}

std::unique_ptr<Expr> &get_expr_ptr ()
{
rust_assert (expression != nullptr);
return expression;
}

protected:
// Clone function implementation as (not pure) virtual method
EnumItemDiscriminant *clone_item_impl () const override
{
return new EnumItemDiscriminant (*this);
}
};

// AST node for Rust "enum" - tagged union
class Enum : public VisItem
{
Expand Down
21 changes: 0 additions & 21 deletions gcc/rust/expand/rust-cfg-strip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1940,27 +1940,6 @@ CfgStrip::visit (AST::EnumItemStruct &item)
maybe_strip_struct_fields (item.get_struct_fields ());
}

void
CfgStrip::visit (AST::EnumItemDiscriminant &item)
{
// initial test based on outer attrs
expand_cfg_attrs (item.get_outer_attrs ());
if (fails_cfg_with_expand (item.get_outer_attrs ()))
{
item.mark_for_strip ();
return;
}

AST::DefaultASTVisitor::visit (item);
/* strip any internal sub-expressions - expression itself isn't
* allowed to have external attributes in this position so can't be
* stripped. */
auto &expr = item.get_expr ();
if (expr.is_marked_for_strip ())
rust_error_at (expr.get_locus (),
"cannot strip expression in this position - outer "
"attributes not allowed");
}
void
CfgStrip::visit (AST::Enum &enum_item)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/expand/rust-cfg-strip.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ class CfgStrip : public AST::DefaultASTVisitor
void visit (AST::EnumItem &item) override;
void visit (AST::EnumItemTuple &item) override;
void visit (AST::EnumItemStruct &item) override;
void visit (AST::EnumItemDiscriminant &item) override;
void visit (AST::Enum &enum_item) override;
void visit (AST::Union &union_item) override;
void visit (AST::ConstantItem &const_item) override;
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/expand/rust-derive.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ class DeriveVisitor : public AST::ASTVisitor
virtual void visit (EnumItem &item) override final{};
virtual void visit (EnumItemTuple &item) override final{};
virtual void visit (EnumItemStruct &item) override final{};
virtual void visit (EnumItemDiscriminant &item) override final{};
virtual void visit (ConstantItem &const_item) override final{};
virtual void visit (StaticItem &static_item) override final{};
virtual void visit (TraitItemConst &item) override final{};
Expand Down
6 changes: 0 additions & 6 deletions gcc/rust/expand/rust-expand-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -798,12 +798,6 @@ ExpandVisitor::visit (AST::EnumItemStruct &item)
expand_struct_fields (item.get_struct_fields ());
}

void
ExpandVisitor::visit (AST::EnumItemDiscriminant &item)
{
maybe_expand_expr (item.get_expr_ptr ());
}

void
ExpandVisitor::visit (AST::Union &union_item)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/expand/rust-expand-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ class ExpandVisitor : public AST::DefaultASTVisitor
void visit (AST::EnumItem &item) override;
void visit (AST::EnumItemTuple &item) override;
void visit (AST::EnumItemStruct &item) override;
void visit (AST::EnumItemDiscriminant &item) override;
void visit (AST::Union &union_item) override;
void visit (AST::ConstantItem &const_item) override;
void visit (AST::StaticItem &static_item) override;
Expand Down
3 changes: 0 additions & 3 deletions gcc/rust/hir/rust-ast-lower-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,6 @@ void
ASTLoweringBase::visit (AST::EnumItemStruct &)
{}
void
ASTLoweringBase::visit (AST::EnumItemDiscriminant &)
{}
void
ASTLoweringBase::visit (AST::Enum &)
{}
void
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/hir/rust-ast-lower-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class ASTLoweringBase : public AST::ASTVisitor
virtual void visit (AST::EnumItem &item);
virtual void visit (AST::EnumItemTuple &item);
virtual void visit (AST::EnumItemStruct &item);
virtual void visit (AST::EnumItemDiscriminant &item);
virtual void visit (AST::Enum &enum_item);
virtual void visit (AST::Union &union_item);
virtual void visit (AST::ConstantItem &const_item);
Expand Down
20 changes: 0 additions & 20 deletions gcc/rust/hir/rust-ast-lower-enumitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,6 @@ class ASTLoweringEnumItem : public ASTLoweringBase
item.get_locus ());
}

void visit (AST::EnumItemDiscriminant &item) override
{
auto crate_num = mappings.get_current_crate ();
Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
mappings.get_next_hir_id (crate_num),
mappings.get_next_localdef_id (crate_num));

if (item.has_visibility ())
rust_error_at (item.get_locus (),
"visibility qualifier %qs not allowed on enum item",
item.get_visibility ().as_string ().c_str ());

HIR::Expr *expr = ASTLoweringExpr::translate (item.get_expr ());
translated
= new HIR::EnumItemDiscriminant (mapping, item.get_identifier (),
std::unique_ptr<HIR::Expr> (expr),
item.get_outer_attrs (),
item.get_locus ());
}

private:
ASTLoweringEnumItem () : translated (nullptr) {}

Expand Down
8 changes: 3 additions & 5 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4793,11 +4793,9 @@ Parser<ManagedTokenSource>::parse_enum_item ()

std::unique_ptr<AST::Expr> discriminant_expr = parse_expr ();

return std::unique_ptr<AST::EnumItemDiscriminant> (
new AST::EnumItemDiscriminant (std::move (item_name), std::move (vis),
std::move (discriminant_expr),
std::move (outer_attrs),
item_name_tok->get_locus ()));
return std::unique_ptr<AST::EnumItem> (new AST::EnumItem (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we need to add a comment saying that this part needs to be reworked to properly handle enum discriminants, and link to issue #3340

something like:

// FIXME: We need to add special handling for parsing discriminant expressions for all possible enum items, not just enum item identifiers (gccrs#3340)

std::move (item_name), std::move (vis), std::move (outer_attrs),
item_name_tok->get_locus (), discriminant_expr->clone_expr ()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to not use the discriminant_expr here as we can't do anything with it yet. So use the regular constructor.

Suggested change
item_name_tok->get_locus (), discriminant_expr->clone_expr ()));
item_name_tok->get_locus ()));

}
default:
// regular enum with just an identifier
Expand Down
4 changes: 0 additions & 4 deletions gcc/rust/resolve/rust-ast-resolve-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,6 @@ void
ResolverBase::visit (AST::EnumItemStruct &)
{}

void
ResolverBase::visit (AST::EnumItemDiscriminant &)
{}

void
ResolverBase::visit (AST::Enum &)
{}
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/resolve/rust-ast-resolve-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class ResolverBase : public AST::ASTVisitor
void visit (AST::EnumItem &);
void visit (AST::EnumItemTuple &);
void visit (AST::EnumItemStruct &);
void visit (AST::EnumItemDiscriminant &);
void visit (AST::Enum &);
void visit (AST::Union &);
void visit (AST::ConstantItem &);
Expand Down
11 changes: 0 additions & 11 deletions gcc/rust/resolve/rust-ast-resolve-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,6 @@ ResolveItem::visit (AST::EnumItemStruct &item)
}
}

void
ResolveItem::visit (AST::EnumItemDiscriminant &item)
{
auto decl = CanonicalPath::new_seg (item.get_node_id (),
item.get_identifier ().as_string ());
auto path = prefix.append (decl);
auto cpath = canonical_prefix.append (decl);

mappings.insert_canonical_path (item.get_node_id (), cpath);
}

void
ResolveItem::visit (AST::StructStruct &struct_decl)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/resolve/rust-ast-resolve-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class ResolveItem : public ResolverBase
void visit (AST::EnumItem &item) override;
void visit (AST::EnumItemTuple &item) override;
void visit (AST::EnumItemStruct &item) override;
void visit (AST::EnumItemDiscriminant &item) override;
void visit (AST::StructStruct &struct_decl) override;
void visit (AST::Union &union_decl) override;
void visit (AST::StaticItem &var) override;
Expand Down
Loading
Loading