aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-07-28 17:55:55 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2024-01-16 19:04:32 +0100
commitde8bc8f9bc03230f124802c4dd0a42f79796a770 (patch)
treed999cb83bced9055bad0346d72dcd2f1b146981b /gcc
parentad1f51084946125ceb33a0086d42fac0f42460be (diff)
downloadgcc-de8bc8f9bc03230f124802c4dd0a42f79796a770.zip
gcc-de8bc8f9bc03230f124802c4dd0a42f79796a770.tar.gz
gcc-de8bc8f9bc03230f124802c4dd0a42f79796a770.tar.bz2
gccrs: Set traits getter as member function
This function will be used outside of the expand visitor, making it easily accessible is therefore mandatory. gcc/rust/ChangeLog: * ast/rust-ast.cc (Attribute::get_traits_to_derive): Add function as member function. * ast/rust-ast.h: Add prototype. * expand/rust-expand-visitor.cc (get_traits_to_derive): Remove function. (ExpandVisitor::expand_inner_stmts): Update function call. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-ast.cc72
-rw-r--r--gcc/rust/ast/rust-ast.h2
-rw-r--r--gcc/rust/expand/rust-expand-visitor.cc76
3 files changed, 76 insertions, 74 deletions
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index 434c276..076f40e 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -90,6 +90,78 @@ Attribute::is_derive () const
&& get_path () == "derive";
}
+/**
+ * Returns a list of traits to derive from within a given attribute.
+ *
+ * @param attrs The attributes on the item to derive
+ */
+std::vector<AST::SimplePath>
+Attribute::get_traits_to_derive ()
+{
+ std::vector<AST::SimplePath> result;
+ auto &input = get_attr_input ();
+ switch (input.get_attr_input_type ())
+ {
+ case AST::AttrInput::META_ITEM: {
+ auto meta = static_cast<AST::AttrInputMetaItemContainer &> (input);
+ for (auto &current : meta.get_items ())
+ {
+ // HACK: Find a better way to achieve the downcast.
+ switch (current->get_kind ())
+ {
+ case AST::MetaItemInner::Kind::MetaItem: {
+ // Let raw pointer go out of scope without freeing, it doesn't
+ // own the data anyway
+ auto meta_item
+ = static_cast<AST::MetaItem *> (current.get ());
+ switch (meta_item->get_item_kind ())
+ {
+ case AST::MetaItem::ItemKind::Path: {
+ auto path
+ = static_cast<AST::MetaItemPath *> (meta_item);
+ result.push_back (path->get_path ());
+ }
+ break;
+ case AST::MetaItem::ItemKind::Word: {
+ auto word = static_cast<AST::MetaWord *> (meta_item);
+ // Convert current word to path
+ current
+ = make_unique<AST::MetaItemPath> (AST::MetaItemPath (
+ AST::SimplePath (word->get_ident ())));
+ auto path
+ = static_cast<AST::MetaItemPath *> (current.get ());
+
+ result.push_back (path->get_path ());
+ }
+ break;
+ case AST::MetaItem::ItemKind::ListPaths:
+ case AST::MetaItem::ItemKind::NameValueStr:
+ case AST::MetaItem::ItemKind::PathLit:
+ case AST::MetaItem::ItemKind::Seq:
+ case AST::MetaItem::ItemKind::ListNameValueStr:
+ default:
+ gcc_unreachable ();
+ break;
+ }
+ }
+ break;
+ case AST::MetaItemInner::Kind::LitExpr:
+ default:
+ gcc_unreachable ();
+ break;
+ }
+ }
+ }
+ break;
+ case AST::AttrInput::TOKEN_TREE:
+ case AST::AttrInput::LITERAL:
+ case AST::AttrInput::MACRO:
+ rust_unreachable ();
+ break;
+ }
+ return result;
+}
+
// Copy constructor must deep copy attr_input as unique pointer
Attribute::Attribute (Attribute const &other)
: path (other.path), locus (other.locus)
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 08b511f..32732ff 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -519,6 +519,8 @@ public:
bool is_derive () const;
+ std::vector<AST::SimplePath> get_traits_to_derive ();
+
// default destructor
~Attribute () = default;
diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index 77dfdcb..f75069b 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -42,78 +42,6 @@ ExpandVisitor::go (AST::Crate &crate)
expand_inner_items (crate.items);
}
-/**
- * Returns a list of traits to derive from within a given attribute.
- *
- * @param attrs The attributes on the item to derive
- */
-static std::vector<AST::SimplePath>
-get_traits_to_derive (AST::Attribute &attr)
-{
- std::vector<AST::SimplePath> result;
- auto &input = attr.get_attr_input ();
- switch (input.get_attr_input_type ())
- {
- case AST::AttrInput::META_ITEM: {
- auto meta = static_cast<AST::AttrInputMetaItemContainer &> (input);
- for (auto &current : meta.get_items ())
- {
- // HACK: Find a better way to achieve the downcast.
- switch (current->get_kind ())
- {
- case AST::MetaItemInner::Kind::MetaItem: {
- // Let raw pointer go out of scope without freeing, it doesn't
- // own the data anyway
- auto meta_item
- = static_cast<AST::MetaItem *> (current.get ());
- switch (meta_item->get_item_kind ())
- {
- case AST::MetaItem::ItemKind::Path: {
- auto path
- = static_cast<AST::MetaItemPath *> (meta_item);
- result.push_back (path->get_path ());
- }
- break;
- case AST::MetaItem::ItemKind::Word: {
- auto word = static_cast<AST::MetaWord *> (meta_item);
- // Convert current word to path
- current
- = make_unique<AST::MetaItemPath> (AST::MetaItemPath (
- AST::SimplePath (word->get_ident ())));
- auto path
- = static_cast<AST::MetaItemPath *> (current.get ());
-
- result.push_back (path->get_path ());
- }
- break;
- case AST::MetaItem::ItemKind::ListPaths:
- case AST::MetaItem::ItemKind::NameValueStr:
- case AST::MetaItem::ItemKind::PathLit:
- case AST::MetaItem::ItemKind::Seq:
- case AST::MetaItem::ItemKind::ListNameValueStr:
- default:
- gcc_unreachable ();
- break;
- }
- }
- break;
- case AST::MetaItemInner::Kind::LitExpr:
- default:
- gcc_unreachable ();
- break;
- }
- }
- }
- break;
- case AST::AttrInput::TOKEN_TREE:
- case AST::AttrInput::LITERAL:
- case AST::AttrInput::MACRO:
- rust_unreachable ();
- break;
- }
- return result;
-}
-
static std::unique_ptr<AST::Item>
builtin_derive_item (AST::Item &item, const AST::Attribute &derive,
BuiltinMacro to_derive)
@@ -253,7 +181,7 @@ ExpandVisitor::expand_inner_items (
current.parse_attr_to_meta_item ();
attr_it = attrs.erase (attr_it);
// Get traits to derive in the current attribute
- auto traits_to_derive = get_traits_to_derive (current);
+ auto traits_to_derive = current.get_traits_to_derive ();
for (auto &to_derive : traits_to_derive)
{
auto maybe_builtin = MacroBuiltin::builtins.lookup (
@@ -339,7 +267,7 @@ ExpandVisitor::expand_inner_stmts (AST::BlockExpr &expr)
{
attr_it = attrs.erase (attr_it);
// Get traits to derive in the current attribute
- auto traits_to_derive = get_traits_to_derive (current);
+ auto traits_to_derive = current.get_traits_to_derive ();
for (auto &to_derive : traits_to_derive)
{
auto maybe_builtin = MacroBuiltin::builtins.lookup (