From: Pierre-Emmanuel Patry Date: Fri, 28 Jul 2023 15:55:55 +0000 (+0200) Subject: gccrs: Set traits getter as member function X-Git-Tag: basepoints/gcc-15~2163 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=de8bc8f9bc03230f124802c4dd0a42f79796a770;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 434c2768bc57..076f40e338b7 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 +Attribute::get_traits_to_derive () +{ + std::vector result; + auto &input = get_attr_input (); + switch (input.get_attr_input_type ()) + { + case AST::AttrInput::META_ITEM: { + auto meta = static_cast (input); + for (auto ¤t : 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 (current.get ()); + switch (meta_item->get_item_kind ()) + { + case AST::MetaItem::ItemKind::Path: { + auto path + = static_cast (meta_item); + result.push_back (path->get_path ()); + } + break; + case AST::MetaItem::ItemKind::Word: { + auto word = static_cast (meta_item); + // Convert current word to path + current + = make_unique (AST::MetaItemPath ( + AST::SimplePath (word->get_ident ()))); + auto path + = static_cast (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 08b511f4019a..32732ffdbd75 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 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 77dfdcb6d6a1..f75069b2e0a5 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 -get_traits_to_derive (AST::Attribute &attr) -{ - std::vector result; - auto &input = attr.get_attr_input (); - switch (input.get_attr_input_type ()) - { - case AST::AttrInput::META_ITEM: { - auto meta = static_cast (input); - for (auto ¤t : 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 (current.get ()); - switch (meta_item->get_item_kind ()) - { - case AST::MetaItem::ItemKind::Path: { - auto path - = static_cast (meta_item); - result.push_back (path->get_path ()); - } - break; - case AST::MetaItem::ItemKind::Word: { - auto word = static_cast (meta_item); - // Convert current word to path - current - = make_unique (AST::MetaItemPath ( - AST::SimplePath (word->get_ident ()))); - auto path - = static_cast (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 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 (