diff --git a/gcc/rust/ast/rust-ast-fragment.cc b/gcc/rust/ast/rust-ast-fragment.cc index 80f10d54dc55..5b2181c49e5a 100644 --- a/gcc/rust/ast/rust-ast-fragment.cc +++ b/gcc/rust/ast/rust-ast-fragment.cc @@ -153,14 +153,12 @@ void Fragment::assert_single_fragment (SingleASTNode::NodeType expected) const { static const std::map str_map = { - {SingleASTNode::NodeType::IMPL, "impl"}, + {SingleASTNode::NodeType::ASSOC_ITEM, "assoc 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 (); diff --git a/gcc/rust/ast/rust-ast-full-decls.h b/gcc/rust/ast/rust-ast-full-decls.h index 64b9b3d5f056..3a32e3095bbf 100644 --- a/gcc/rust/ast/rust-ast-full-decls.h +++ b/gcc/rust/ast/rust-ast-full-decls.h @@ -51,9 +51,7 @@ class Lifetime; class GenericParam; class LifetimeParam; class ConstGenericParam; -class TraitItem; -class InherentImplItem; -class TraitImplItem; +class AssociatedItem; struct Crate; class PathExpr; diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 1f9a0ef31e6d..48412a26703f 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -3458,14 +3458,14 @@ Module::process_file_path () bool no_candidates_found = !file_mod_found && !dir_mod_found; if (multiple_candidates_found) - rust_error_at (locus, + rust_error_at (get_locus (), "two candidates found for module %s: %s.rs and %s%smod.rs", module_name.as_string ().c_str (), module_name.as_string ().c_str (), module_name.as_string ().c_str (), file_separator); if (no_candidates_found) - rust_error_at (locus, "no candidate found for module %s", + rust_error_at (get_locus (), "no candidate found for module %s", module_name.as_string ().c_str ()); if (no_candidates_found || multiple_candidates_found) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index db55f011faac..fc5ccfc8d858 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -79,6 +79,36 @@ enum class Kind IDENTIFIER, }; +class Located +{ +public: + virtual location_t get_locus () const = 0; +}; + +class LocatedImpl : virtual public Located +{ +private: + location_t locus; + +protected: + LocatedImpl (location_t locus) : locus (locus) {} + +public: + location_t get_locus () const override final { return locus; } +}; + +class NodeIdStore +{ + NodeId node_id; + +public: + NodeIdStore () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} + + NodeId get_node_id () const { return node_id; } + + friend class Expr; +}; + class Visitable { public: @@ -362,18 +392,16 @@ class PathSegment }; // A segment of a simple path without generic or type arguments -class SimplePathSegment : public PathSegment +class SimplePathSegment : public PathSegment, virtual public NodeIdStore { std::string segment_name; location_t locus; - NodeId node_id; // only allow identifiers, "super", "self", "crate", or "$crate" public: // TODO: put checks in constructor to enforce this rule? SimplePathSegment (std::string segment_name, location_t locus) - : segment_name (std::move (segment_name)), locus (locus), - node_id (Analysis::Mappings::get ()->get_next_node_id ()) + : segment_name (std::move (segment_name)), locus (locus) {} /* Returns whether simple path segment is in an invalid state (currently, if @@ -389,7 +417,6 @@ class SimplePathSegment : public PathSegment std::string as_string () const override; location_t get_locus () const { return locus; } - NodeId get_node_id () const { return node_id; } const std::string &get_segment_name () const { return segment_name; } bool is_super_path_seg () const { @@ -404,12 +431,11 @@ class SimplePathSegment : public PathSegment }; // A simple path without generic or type arguments -class SimplePath +class SimplePath : public NodeIdStore { bool opening_scope_resolution; std::vector segments; location_t locus; - NodeId node_id; public: // Constructor @@ -417,15 +443,13 @@ class SimplePath bool has_opening_scope_resolution = false, location_t locus = UNDEF_LOCATION) : opening_scope_resolution (has_opening_scope_resolution), - segments (std::move (path_segments)), locus (locus), - node_id (Analysis::Mappings::get ()->get_next_node_id ()) + segments (std::move (path_segments)), locus (locus) {} SimplePath (Identifier ident) : opening_scope_resolution (false), segments ({SimplePathSegment (ident.as_string (), ident.get_locus ())}), - locus (ident.get_locus ()), - node_id (Analysis::Mappings::get ()->get_next_node_id ()) + locus (ident.get_locus ()) {} // Creates an empty SimplePath. @@ -445,7 +469,6 @@ class SimplePath } location_t get_locus () const { return locus; } - NodeId get_node_id () const { return node_id; } // does this need visitor if not polymorphic? probably not @@ -955,7 +978,7 @@ class MetaListNameValueStr; /* Base statement abstract class. Note that most "statements" are not allowed * in top-level module scope - only a subclass of statements called "items" * are. */ -class Stmt : public Node +class Stmt : public Node, virtual public Located, virtual public NodeIdStore { public: enum class Kind @@ -977,11 +1000,8 @@ class Stmt : public Node virtual std::string as_string () const = 0; - virtual location_t get_locus () const = 0; - virtual void mark_for_strip () = 0; virtual bool is_marked_for_strip () const = 0; - NodeId get_node_id () const { return node_id; } virtual Kind get_stmt_kind () = 0; @@ -992,12 +1012,8 @@ class Stmt : public Node virtual void add_semicolon () {} protected: - Stmt () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} - // Clone function implementation as pure virtual method virtual Stmt *clone_stmt_impl () const = 0; - - NodeId node_id; }; // Rust "item" AST node (declaration of top-level/module-level allowed stuff) @@ -1041,7 +1057,7 @@ class Item : public Stmt class ExprWithoutBlock; // Base expression AST node - abstract -class Expr : public Node +class Expr : public Node, virtual public Located, virtual public NodeIdStore { public: // Unique pointer custom clone function @@ -1059,8 +1075,6 @@ class Expr : public Node virtual ~Expr () {} - virtual location_t get_locus () const = 0; - virtual bool is_literal () const { return false; } // HACK: strictly not needed, but faster than full downcast clone @@ -1069,8 +1083,6 @@ class Expr : public Node virtual void mark_for_strip () = 0; virtual bool is_marked_for_strip () const = 0; - virtual NodeId get_node_id () const { return node_id; } - virtual void set_node_id (NodeId id) { node_id = id; } virtual std::vector &get_outer_attrs () = 0; @@ -1080,13 +1092,8 @@ class Expr : public Node virtual void set_outer_attrs (std::vector) = 0; protected: - // Constructor - Expr () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} - // Clone function implementation as pure virtual method virtual Expr *clone_expr_impl () const = 0; - - NodeId node_id; }; // AST node for an expression without an accompanying block - abstract @@ -1174,7 +1181,9 @@ class IdentifierExpr : public ExprWithoutBlock }; // Pattern base AST node -class Pattern : public Visitable +class Pattern : public Visitable, + virtual public Located, + virtual public NodeIdStore { public: // Unique pointer custom clone function @@ -1193,9 +1202,6 @@ class Pattern : public Visitable virtual void mark_for_strip () {} virtual bool is_marked_for_strip () const { return false; } - virtual location_t get_locus () const = 0; - virtual NodeId get_pattern_node_id () const = 0; - protected: // Clone pattern implementation as pure virtual method virtual Pattern *clone_pattern_impl () const = 0; @@ -1205,7 +1211,7 @@ class Pattern : public Visitable class TraitBound; // Base class for types as represented in AST - abstract -class Type : public Node +class Type : public Node, virtual public Located, virtual public NodeIdStore { public: // Unique pointer custom clone function @@ -1229,17 +1235,9 @@ class Type : public Node virtual void mark_for_strip () {} virtual bool is_marked_for_strip () const { return false; } - virtual location_t get_locus () const = 0; - - NodeId get_node_id () const { return node_id; } - protected: - Type () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} - // Clone function implementation as pure virtual method virtual Type *clone_type_impl () const = 0; - - NodeId node_id; }; // A type without parentheses? - abstract @@ -1446,76 +1444,20 @@ class LifetimeParam : public GenericParam } }; -// Item used in trait declarations - abstract base class -class TraitItem : public Visitable +// Abstract base class for items used within an impl block +class AssociatedItem : public Visitable, virtual public Located { protected: - TraitItem (location_t locus) - : node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus) - {} - // Clone function implementation as pure virtual method - virtual TraitItem *clone_trait_item_impl () const = 0; - - NodeId node_id; - location_t locus; + virtual AssociatedItem *clone_associated_item_impl () const = 0; public: - virtual ~TraitItem () {} + virtual ~AssociatedItem () {} // Unique pointer custom clone function - std::unique_ptr clone_trait_item () const + std::unique_ptr clone_associated_item () const { - return std::unique_ptr (clone_trait_item_impl ()); - } - - virtual std::string as_string () const = 0; - - virtual void mark_for_strip () = 0; - virtual bool is_marked_for_strip () const = 0; - - NodeId get_node_id () const { return node_id; } - location_t get_locus () const { return locus; } -}; - -/* Abstract base class for items used within an inherent impl block (the impl - * name {} one) */ -class InherentImplItem : public Visitable -{ -protected: - // Clone function implementation as pure virtual method - virtual InherentImplItem *clone_inherent_impl_item_impl () const = 0; - -public: - virtual ~InherentImplItem () {} - - // Unique pointer custom clone function - std::unique_ptr clone_inherent_impl_item () const - { - return std::unique_ptr (clone_inherent_impl_item_impl ()); - } - - virtual std::string as_string () const = 0; - - virtual void mark_for_strip () = 0; - virtual bool is_marked_for_strip () const = 0; - - virtual location_t get_locus () const = 0; -}; - -// Abstract base class for items used in a trait impl -class TraitImplItem : public Visitable -{ -protected: - virtual TraitImplItem *clone_trait_impl_item_impl () const = 0; - -public: - virtual ~TraitImplItem (){}; - - // Unique pointer custom clone function - std::unique_ptr clone_trait_impl_item () const - { - return std::unique_ptr (clone_trait_impl_item_impl ()); + return std::unique_ptr (clone_associated_item_impl ()); } virtual std::string as_string () const = 0; @@ -1525,11 +1467,9 @@ class TraitImplItem : public Visitable }; // Abstract base class for an item used inside an extern block -class ExternalItem : public Visitable +class ExternalItem : public Visitable, virtual public NodeIdStore { public: - ExternalItem () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} - virtual ~ExternalItem () {} // Unique pointer custom clone function @@ -1543,13 +1483,9 @@ class ExternalItem : public Visitable virtual void mark_for_strip () = 0; virtual bool is_marked_for_strip () const = 0; - NodeId get_node_id () const { return node_id; } - protected: // Clone function implementation as pure virtual method virtual ExternalItem *clone_external_item_impl () const = 0; - - NodeId node_id; }; /* Data structure to store the data used in macro invocations and macro @@ -1652,9 +1588,7 @@ class SingleASTNode : public Visitable ITEM, STMT, EXTERN, - TRAIT, - IMPL, - TRAIT_IMPL, + ASSOC_ITEM, TYPE, }; @@ -1666,9 +1600,7 @@ class SingleASTNode : public Visitable std::unique_ptr item; std::unique_ptr stmt; std::unique_ptr external_item; - std::unique_ptr trait_item; - std::unique_ptr impl_item; - std::unique_ptr trait_impl_item; + std::unique_ptr assoc_item; std::unique_ptr type; public: @@ -1688,16 +1620,8 @@ class SingleASTNode : public Visitable : kind (EXTERN), external_item (std::move (item)) {} - SingleASTNode (std::unique_ptr item) - : kind (TRAIT), trait_item (std::move (item)) - {} - - SingleASTNode (std::unique_ptr item) - : kind (IMPL), impl_item (std::move (item)) - {} - - SingleASTNode (std::unique_ptr trait_impl_item) - : kind (TRAIT_IMPL), trait_impl_item (std::move (trait_impl_item)) + SingleASTNode (std::unique_ptr item) + : kind (ASSOC_ITEM), assoc_item (std::move (item)) {} SingleASTNode (std::unique_ptr type) @@ -1725,16 +1649,8 @@ class SingleASTNode : public Visitable 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_inherent_impl_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: @@ -1764,16 +1680,8 @@ class SingleASTNode : public Visitable 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_inherent_impl_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: @@ -1829,28 +1737,16 @@ class SingleASTNode : public Visitable return std::move (item); } - std::unique_ptr take_trait_item () - { - rust_assert (!is_error ()); - return std::move (trait_item); - } - std::unique_ptr take_external_item () { rust_assert (!is_error ()); return std::move (external_item); } - std::unique_ptr take_impl_item () + std::unique_ptr take_assoc_item () { rust_assert (!is_error ()); - return std::move (impl_item); - } - - std::unique_ptr take_trait_impl_item () - { - rust_assert (!is_error ()); - return std::move (trait_impl_item); + return std::move (assoc_item); } std::unique_ptr take_type () @@ -1879,16 +1775,8 @@ class SingleASTNode : public Visitable 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: @@ -1909,12 +1797,8 @@ class SingleASTNode : public Visitable 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; } @@ -1935,12 +1819,8 @@ class SingleASTNode : public Visitable 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 (); } diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index aae054969633..d44cdbbd931c 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -91,7 +91,7 @@ class TypeParam : public GenericParam // Copy constructor uses clone TypeParam (TypeParam const &other) - : GenericParam (other.node_id), outer_attr (other.outer_attr), + : GenericParam (other), outer_attr (other.outer_attr), type_representation (other.type_representation), locus (other.locus) { // guard to prevent null pointer dereference @@ -106,10 +106,10 @@ class TypeParam : public GenericParam // Overloaded assignment operator to clone TypeParam &operator= (TypeParam const &other) { + GenericParam::operator= (other); type_representation = other.type_representation; outer_attr = other.outer_attr; locus = other.locus; - node_id = other.node_id; // guard to prevent null pointer dereference if (other.type != nullptr) @@ -726,7 +726,7 @@ struct Visibility }; // A method (function belonging to a type) -class Method : public InherentImplItem, public TraitImplItem +class Method : public AssociatedItem { std::vector outer_attrs; Visibility vis; @@ -913,7 +913,7 @@ class Method : public InherentImplItem, public TraitImplItem NodeId get_node_id () const { return node_id; } - location_t get_locus () const override final { return locus; } + location_t get_locus () const { return locus; } FunctionQualifiers get_qualifiers () { return qualifiers; } @@ -923,14 +923,7 @@ class Method : public InherentImplItem, public TraitImplItem protected: /* Use covariance to implement clone function as returning this object * rather than base */ - Method *clone_inherent_impl_item_impl () const final override - { - return clone_method_impl (); - } - - /* Use covariance to implement clone function as returning this object - * rather than base */ - Method *clone_trait_impl_item_impl () const final override + Method *clone_associated_item_impl () const final override { return clone_method_impl (); } @@ -988,7 +981,7 @@ class VisItem : public Item }; // Rust module item - abstract base class -class Module : public VisItem +class Module : public VisItem, public LocatedImpl { public: // Type of the current module. A module can be either loaded or unloaded, @@ -1008,7 +1001,6 @@ class Module : public VisItem private: Identifier module_name; - location_t locus; ModuleKind kind; // Name of the file including the module @@ -1043,8 +1035,9 @@ class Module : public VisItem std::vector outer_attrs, location_t locus, std::string outer_filename, std::vector module_scope) : VisItem (std::move (visibility), std::move (outer_attrs)), - module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED), - outer_filename (outer_filename), inner_attrs (std::vector ()), + LocatedImpl (locus), module_name (module_name), + kind (ModuleKind::UNLOADED), outer_filename (outer_filename), + inner_attrs (std::vector ()), items (std::vector> ()), module_scope (std::move (module_scope)) {} @@ -1056,14 +1049,14 @@ class Module : public VisItem std::vector inner_attrs = std::vector (), std::vector outer_attrs = std::vector ()) : VisItem (std::move (visibility), std::move (outer_attrs)), - module_name (name), locus (locus), kind (ModuleKind::LOADED), + LocatedImpl (locus), module_name (name), kind (ModuleKind::LOADED), outer_filename (std::string ()), inner_attrs (std::move (inner_attrs)), items (std::move (items)) {} // Copy constructor with vector clone Module (Module const &other) - : VisItem (other), module_name (other.module_name), locus (other.locus), + : VisItem (other), LocatedImpl (other), module_name (other.module_name), kind (other.kind), inner_attrs (other.inner_attrs), module_scope (other.module_scope) { @@ -1079,9 +1072,9 @@ class Module : public VisItem Module &operator= (Module const &other) { VisItem::operator= (other); + LocatedImpl::operator= (other); module_name = other.module_name; - locus = other.locus; kind = other.kind; inner_attrs = other.inner_attrs; module_scope = other.module_scope; @@ -1134,8 +1127,6 @@ class Module : public VisItem std::string as_string () const override; - location_t get_locus () const override final { return locus; } - // Invalid if name is empty, so base stripping on that. void mark_for_strip () override { module_name = {""}; } bool is_marked_for_strip () const override { return module_name.empty (); } @@ -1544,7 +1535,7 @@ class UseDeclaration : public VisItem class LetStmt; // Rust function declaration AST node -class Function : public VisItem, public InherentImplItem, public TraitImplItem +class Function : public VisItem, public AssociatedItem { FunctionQualifiers qualifiers; Identifier function_name; @@ -1703,21 +1694,16 @@ class Function : public VisItem, public InherentImplItem, public TraitImplItem /* Use covariance to implement clone function as returning this object * rather than base */ - Function *clone_inherent_impl_item_impl () const override - { - return new Function (*this); - } - - /* Use covariance to implement clone function as returning this object - * rather than base */ - Function *clone_trait_impl_item_impl () const override + Function *clone_associated_item_impl () const override { return new Function (*this); } }; // Rust type alias (i.e. typedef) AST node -class TypeAlias : public VisItem, public TraitImplItem +class TypeAlias : public VisItem, + public AssociatedItem, + virtual public LocatedImpl { Identifier new_type_name; @@ -1730,8 +1716,6 @@ class TypeAlias : public VisItem, public TraitImplItem std::unique_ptr existing_type; - location_t locus; - public: std::string as_string () const override; @@ -1747,17 +1731,17 @@ class TypeAlias : public VisItem, public TraitImplItem WhereClause where_clause, std::unique_ptr existing_type, Visibility vis, std::vector outer_attrs, location_t locus) - : VisItem (std::move (vis), std::move (outer_attrs)), + : LocatedImpl (locus), VisItem (std::move (vis), std::move (outer_attrs)), new_type_name (std::move (new_type_name)), generic_params (std::move (generic_params)), where_clause (std::move (where_clause)), - existing_type (std::move (existing_type)), locus (locus) + existing_type (std::move (existing_type)) {} // Copy constructor TypeAlias (TypeAlias const &other) - : VisItem (other), new_type_name (other.new_type_name), - where_clause (other.where_clause), locus (other.locus) + : LocatedImpl (other), VisItem (other), new_type_name (other.new_type_name), + where_clause (other.where_clause) { // guard to prevent null dereference (only required if error state) if (other.existing_type != nullptr) @@ -1772,11 +1756,11 @@ class TypeAlias : public VisItem, public TraitImplItem TypeAlias &operator= (TypeAlias const &other) { VisItem::operator= (other); + LocatedImpl::operator= (other); new_type_name = other.new_type_name; where_clause = other.where_clause; // visibility = other.visibility->clone_visibility(); // outer_attrs = other.outer_attrs; - locus = other.locus; // guard to prevent null dereference (only required if error state) if (other.existing_type != nullptr) @@ -1795,8 +1779,6 @@ class TypeAlias : public VisItem, public TraitImplItem TypeAlias (TypeAlias &&other) = default; TypeAlias &operator= (TypeAlias &&other) = default; - location_t get_locus () const override final { return locus; } - void accept_vis (ASTVisitor &vis) override; // Invalid if existing type is null, so base stripping on that. @@ -1834,7 +1816,7 @@ class TypeAlias : public VisItem, public TraitImplItem /* Use covariance to implement clone function as returning this object * rather than base */ - TypeAlias *clone_trait_impl_item_impl () const override + TypeAlias *clone_associated_item_impl () const override { return new TypeAlias (*this); } @@ -2593,9 +2575,7 @@ class Union : public VisItem /* "Constant item" AST node - used for constant, compile-time expressions * within module scope (like constexpr) */ -class ConstantItem : public VisItem, - public InherentImplItem, - public TraitImplItem +class ConstantItem : public VisItem, public AssociatedItem { // either has an identifier or "_" - maybe handle in identifier? // bool identifier_is_underscore; @@ -2656,7 +2636,7 @@ class ConstantItem : public VisItem, * as identifier) constant. */ bool is_unnamed () const { return identifier == "_"; } - location_t get_locus () const override final { return locus; } + location_t get_locus () const { return locus; } void accept_vis (ASTVisitor &vis) override; @@ -2699,14 +2679,7 @@ class ConstantItem : public VisItem, /* Use covariance to implement clone function as returning this object * rather than base */ - ConstantItem *clone_inherent_impl_item_impl () const override - { - return new ConstantItem (*this); - } - - /* Use covariance to implement clone function as returning this object - * rather than base */ - ConstantItem *clone_trait_impl_item_impl () const override + ConstantItem *clone_associated_item_impl () const override { return new ConstantItem (*this); } @@ -2939,7 +2912,9 @@ class TraitFunctionDecl }; // Actual trait item function declaration within traits -class TraitItemFunc : public TraitItem +class TraitItemFunc : public AssociatedItem, + public LocatedImpl, + public NodeIdStore { std::vector outer_attrs; TraitFunctionDecl decl; @@ -2951,17 +2926,15 @@ class TraitItemFunc : public TraitItem TraitItemFunc (TraitFunctionDecl decl, std::unique_ptr block_expr, std::vector outer_attrs, location_t locus) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), + : LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), decl (std::move (decl)), block_expr (std::move (block_expr)) {} // Copy constructor with clone TraitItemFunc (TraitItemFunc const &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), + : LocatedImpl (other), NodeIdStore (other), outer_attrs (other.outer_attrs), decl (other.decl) { - node_id = other.node_id; - // guard to prevent null dereference if (other.block_expr != nullptr) block_expr = other.block_expr->clone_block_expr (); @@ -2970,11 +2943,10 @@ class TraitItemFunc : public TraitItem // Overloaded assignment operator to clone TraitItemFunc &operator= (TraitItemFunc const &other) { - TraitItem::operator= (other); + LocatedImpl::operator= (other); + NodeIdStore::operator= (other); outer_attrs = other.outer_attrs; decl = other.decl; - locus = other.locus; - node_id = other.node_id; // guard to prevent null dereference if (other.block_expr != nullptr) @@ -3016,7 +2988,7 @@ class TraitItemFunc : public TraitItem protected: // Clone function implementation as (not pure) virtual method - TraitItemFunc *clone_trait_item_impl () const override + TraitItemFunc *clone_associated_item_impl () const override { return new TraitItemFunc (*this); } @@ -3155,7 +3127,9 @@ class TraitMethodDecl }; // Actual trait item method declaration within traits -class TraitItemMethod : public TraitItem +class TraitItemMethod : public AssociatedItem, + public LocatedImpl, + public NodeIdStore { std::vector outer_attrs; TraitMethodDecl decl; @@ -3167,17 +3141,15 @@ class TraitItemMethod : public TraitItem TraitItemMethod (TraitMethodDecl decl, std::unique_ptr block_expr, std::vector outer_attrs, location_t locus) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), + : LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), decl (std::move (decl)), block_expr (std::move (block_expr)) {} // Copy constructor with clone TraitItemMethod (TraitItemMethod const &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), + : LocatedImpl (other), NodeIdStore (other), outer_attrs (other.outer_attrs), decl (other.decl) { - node_id = other.node_id; - // guard to prevent null dereference if (other.block_expr != nullptr) block_expr = other.block_expr->clone_block_expr (); @@ -3186,11 +3158,10 @@ class TraitItemMethod : public TraitItem // Overloaded assignment operator to clone TraitItemMethod &operator= (TraitItemMethod const &other) { - TraitItem::operator= (other); + LocatedImpl::operator= (other); + NodeIdStore::operator= (other); outer_attrs = other.outer_attrs; decl = other.decl; - locus = other.locus; - node_id = other.node_id; // guard to prevent null dereference if (other.block_expr != nullptr) @@ -3232,14 +3203,16 @@ class TraitItemMethod : public TraitItem protected: // Clone function implementation as (not pure) virtual method - TraitItemMethod *clone_trait_item_impl () const override + TraitItemMethod *clone_associated_item_impl () const override { return new TraitItemMethod (*this); } }; // Constant item within traits -class TraitItemConst : public TraitItem +class TraitItemConst : public AssociatedItem, + public LocatedImpl, + public NodeIdStore { std::vector outer_attrs; Identifier name; @@ -3255,17 +3228,15 @@ class TraitItemConst : public TraitItem TraitItemConst (Identifier name, std::unique_ptr type, std::unique_ptr expr, std::vector outer_attrs, location_t locus) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), + : LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), name (std::move (name)), type (std::move (type)), expr (std::move (expr)) {} // Copy constructor with clones TraitItemConst (TraitItemConst const &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), + : LocatedImpl (other), NodeIdStore (other), outer_attrs (other.outer_attrs), name (other.name) { - node_id = other.node_id; - // guard to prevent null dereference if (other.expr != nullptr) expr = other.expr->clone_expr (); @@ -3278,11 +3249,10 @@ class TraitItemConst : public TraitItem // Overloaded assignment operator to clone TraitItemConst &operator= (TraitItemConst const &other) { - TraitItem::operator= (other); + LocatedImpl::operator= (other); + NodeIdStore::operator= (other); outer_attrs = other.outer_attrs; name = other.name; - locus = other.locus; - node_id = other.node_id; // guard to prevent null dereference if (other.expr != nullptr) @@ -3305,8 +3275,6 @@ class TraitItemConst : public TraitItem std::string as_string () const override; - location_t get_locus () const { return locus; } - void accept_vis (ASTVisitor &vis) override; // Invalid if type is null, so base stripping on that. @@ -3337,14 +3305,16 @@ class TraitItemConst : public TraitItem protected: // Clone function implementation as (not pure) virtual method - TraitItemConst *clone_trait_item_impl () const override + TraitItemConst *clone_associated_item_impl () const override { return new TraitItemConst (*this); } }; // Type items within traits -class TraitItemType : public TraitItem +class TraitItemType : public AssociatedItem, + public LocatedImpl, + public NodeIdStore { std::vector outer_attrs; @@ -3362,16 +3332,15 @@ class TraitItemType : public TraitItem TraitItemType (Identifier name, std::vector> type_param_bounds, std::vector outer_attrs, location_t locus) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), + : LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), name (std::move (name)), type_param_bounds (std::move (type_param_bounds)) {} // Copy constructor with vector clone TraitItemType (TraitItemType const &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), + : LocatedImpl (other), NodeIdStore (other), outer_attrs (other.outer_attrs), name (other.name) { - node_id = other.node_id; type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) type_param_bounds.push_back (e->clone_type_param_bound ()); @@ -3380,11 +3349,10 @@ class TraitItemType : public TraitItem // Overloaded assignment operator with vector clone TraitItemType &operator= (TraitItemType const &other) { - TraitItem::operator= (other); + LocatedImpl::operator= (other); + NodeIdStore::operator= (other); outer_attrs = other.outer_attrs; name = other.name; - locus = other.locus; - node_id = other.node_id; type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) @@ -3424,7 +3392,7 @@ class TraitItemType : public TraitItem protected: // Clone function implementation as (not pure) virtual method - TraitItemType *clone_trait_item_impl () const override + TraitItemType *clone_associated_item_impl () const override { return new TraitItemType (*this); } @@ -3440,7 +3408,7 @@ class Trait : public VisItem std::vector> type_param_bounds; WhereClause where_clause; std::vector inner_attrs; - std::vector> trait_items; + std::vector> trait_items; location_t locus; public: @@ -3471,9 +3439,9 @@ class Trait : public VisItem std::vector> generic_params, std::vector> type_param_bounds, WhereClause where_clause, - std::vector> trait_items, Visibility vis, - std::vector outer_attrs, std::vector inner_attrs, - location_t locus) + std::vector> trait_items, + Visibility vis, std::vector outer_attrs, + std::vector inner_attrs, location_t locus) : VisItem (std::move (vis), std::move (outer_attrs)), has_unsafe (is_unsafe), has_auto (is_auto), name (std::move (name)), generic_params (std::move (generic_params)), @@ -3499,7 +3467,7 @@ class Trait : public VisItem trait_items.reserve (other.trait_items.size ()); for (const auto &e : other.trait_items) - trait_items.push_back (e->clone_trait_item ()); + trait_items.push_back (e->clone_associated_item ()); } // Overloaded assignment operator with vector clone @@ -3523,7 +3491,7 @@ class Trait : public VisItem trait_items.reserve (other.trait_items.size ()); for (const auto &e : other.trait_items) - trait_items.push_back (e->clone_trait_item ()); + trait_items.push_back (e->clone_associated_item ()); return *this; } @@ -3544,11 +3512,11 @@ class Trait : public VisItem const std::vector &get_inner_attrs () const { return inner_attrs; } std::vector &get_inner_attrs () { return inner_attrs; } - const std::vector> &get_trait_items () const + const std::vector> &get_trait_items () const { return trait_items; } - std::vector> &get_trait_items () + std::vector> &get_trait_items () { return trait_items; } @@ -3711,7 +3679,7 @@ class Impl : public VisItem class InherentImpl : public Impl { // bool has_impl_items; - std::vector> impl_items; + std::vector> impl_items; public: std::string as_string () const override; @@ -3720,7 +3688,7 @@ class InherentImpl : public Impl bool has_impl_items () const { return !impl_items.empty (); } // Mega-constructor - InherentImpl (std::vector> impl_items, + InherentImpl (std::vector> impl_items, std::vector> generic_params, std::unique_ptr trait_type, WhereClause where_clause, Visibility vis, std::vector inner_attrs, @@ -3736,7 +3704,7 @@ class InherentImpl : public Impl { impl_items.reserve (other.impl_items.size ()); for (const auto &e : other.impl_items) - impl_items.push_back (e->clone_inherent_impl_item ()); + impl_items.push_back (e->clone_associated_item ()); } // Overloaded assignment operator with vector clone @@ -3746,7 +3714,7 @@ class InherentImpl : public Impl impl_items.reserve (other.impl_items.size ()); for (const auto &e : other.impl_items) - impl_items.push_back (e->clone_inherent_impl_item ()); + impl_items.push_back (e->clone_associated_item ()); return *this; } @@ -3758,11 +3726,11 @@ class InherentImpl : public Impl void accept_vis (ASTVisitor &vis) override; // TODO: think of better way to do this - const std::vector> &get_impl_items () const + const std::vector> &get_impl_items () const { return impl_items; } - std::vector> &get_impl_items () + std::vector> &get_impl_items () { return impl_items; } @@ -3784,7 +3752,7 @@ class TraitImpl : public Impl TypePath trait_path; // bool has_impl_items; - std::vector> impl_items; + std::vector> impl_items; public: std::string as_string () const override; @@ -3794,7 +3762,7 @@ class TraitImpl : public Impl // Mega-constructor TraitImpl (TypePath trait_path, bool is_unsafe, bool has_exclam, - std::vector> impl_items, + std::vector> impl_items, std::vector> generic_params, std::unique_ptr trait_type, WhereClause where_clause, Visibility vis, std::vector inner_attrs, @@ -3813,7 +3781,7 @@ class TraitImpl : public Impl { impl_items.reserve (other.impl_items.size ()); for (const auto &e : other.impl_items) - impl_items.push_back (e->clone_trait_impl_item ()); + impl_items.push_back (e->clone_associated_item ()); } // Overloaded assignment operator with vector clone @@ -3826,7 +3794,7 @@ class TraitImpl : public Impl impl_items.reserve (other.impl_items.size ()); for (const auto &e : other.impl_items) - impl_items.push_back (e->clone_trait_impl_item ()); + impl_items.push_back (e->clone_associated_item ()); return *this; } @@ -3841,11 +3809,11 @@ class TraitImpl : public Impl bool is_exclam () const { return has_exclam; } // TODO: think of better way to do this - const std::vector> &get_impl_items () const + const std::vector> &get_impl_items () const { return impl_items; } - std::vector> &get_impl_items () + std::vector> &get_impl_items () { return impl_items; } @@ -3957,21 +3925,19 @@ class ExternalTypeItem : public ExternalItem public: ExternalTypeItem (Identifier item_name, Visibility vis, std::vector outer_attrs, location_t locus) - : ExternalItem (), outer_attrs (std::move (outer_attrs)), visibility (vis), + : outer_attrs (std::move (outer_attrs)), visibility (vis), item_name (std::move (item_name)), locus (locus), marked_for_strip (false) {} ExternalTypeItem (ExternalTypeItem const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - item_name (other.item_name), locus (other.locus), - marked_for_strip (other.marked_for_strip) - { - node_id = other.node_id; - } + : ExternalItem (other), outer_attrs (other.outer_attrs), + visibility (other.visibility), item_name (other.item_name), + locus (other.locus), marked_for_strip (other.marked_for_strip) + {} ExternalTypeItem &operator= (ExternalTypeItem const &other) { - node_id = other.node_id; + ExternalItem::operator= (other); outer_attrs = other.outer_attrs; visibility = other.visibility; item_name = other.item_name; @@ -4037,17 +4003,17 @@ class ExternalStaticItem : public ExternalItem ExternalStaticItem (Identifier item_name, std::unique_ptr item_type, bool is_mut, Visibility vis, std::vector outer_attrs, location_t locus) - : ExternalItem (), outer_attrs (std::move (outer_attrs)), - visibility (std::move (vis)), item_name (std::move (item_name)), - locus (locus), has_mut (is_mut), item_type (std::move (item_type)) + : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)), + item_name (std::move (item_name)), locus (locus), has_mut (is_mut), + item_type (std::move (item_type)) {} // Copy constructor ExternalStaticItem (ExternalStaticItem const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - item_name (other.item_name), locus (other.locus), has_mut (other.has_mut) + : ExternalItem (other), outer_attrs (other.outer_attrs), + visibility (other.visibility), item_name (other.item_name), + locus (other.locus), has_mut (other.has_mut) { - node_id = other.node_id; // guard to prevent null dereference (only required if error state) if (other.item_type != nullptr) item_type = other.item_type->clone_type (); @@ -4056,7 +4022,7 @@ class ExternalStaticItem : public ExternalItem // Overloaded assignment operator to clone ExternalStaticItem &operator= (ExternalStaticItem const &other) { - node_id = other.node_id; + ExternalItem::operator= (other); outer_attrs = other.outer_attrs; visibility = other.visibility; item_name = other.item_name; @@ -4282,9 +4248,9 @@ class ExternalFunctionItem : public ExternalItem std::vector function_params, bool has_variadics, std::vector variadic_outer_attrs, Visibility vis, std::vector outer_attrs, location_t locus) - : ExternalItem (), outer_attrs (std::move (outer_attrs)), - visibility (std::move (vis)), item_name (std::move (item_name)), - locus (locus), generic_params (std::move (generic_params)), + : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)), + item_name (std::move (item_name)), locus (locus), + generic_params (std::move (generic_params)), return_type (std::move (return_type)), where_clause (std::move (where_clause)), function_params (std::move (function_params)), @@ -4297,14 +4263,13 @@ class ExternalFunctionItem : public ExternalItem // Copy constructor with clone ExternalFunctionItem (ExternalFunctionItem const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - item_name (other.item_name), locus (other.locus), - where_clause (other.where_clause), + : ExternalItem (other), outer_attrs (other.outer_attrs), + visibility (other.visibility), item_name (other.item_name), + locus (other.locus), where_clause (other.where_clause), function_params (other.function_params), has_variadics (other.has_variadics), variadic_outer_attrs (other.variadic_outer_attrs) { - node_id = other.node_id; // guard to prevent null pointer dereference if (other.return_type != nullptr) return_type = other.return_type->clone_type (); @@ -4317,6 +4282,7 @@ class ExternalFunctionItem : public ExternalItem // Overloaded assignment operator with clone ExternalFunctionItem &operator= (ExternalFunctionItem const &other) { + ExternalItem::operator= (other); outer_attrs = other.outer_attrs; visibility = other.visibility; item_name = other.item_name; @@ -4325,7 +4291,6 @@ class ExternalFunctionItem : public ExternalItem function_params = other.function_params; has_variadics = other.has_variadics; variadic_outer_attrs = other.variadic_outer_attrs; - node_id = other.node_id; // guard to prevent null pointer dereference if (other.return_type != nullptr) diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 27d3134ec2a1..ecf168c9d9f4 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -447,7 +447,7 @@ struct MacroRule }; // A macro rules definition item AST node -class MacroRulesDefinition : public VisItem +class MacroRulesDefinition : public VisItem, public LocatedImpl { public: enum MacroKind @@ -466,7 +466,6 @@ class MacroRulesDefinition : public VisItem DelimType delim_type; // MacroRules rules; std::vector rules; // inlined form - location_t locus; MacroTranscriberFunc associated_transcriber; @@ -496,9 +495,9 @@ class MacroRulesDefinition : public VisItem std::vector rules, std::vector outer_attrs, location_t locus, MacroKind kind, Visibility vis) - : VisItem (std::move (vis), outer_attrs), + : VisItem (std::move (vis), outer_attrs), LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), rule_name (std::move (rule_name)), - delim_type (delim_type), rules (std::move (rules)), locus (locus), + delim_type (delim_type), rules (std::move (rules)), associated_transcriber (dummy_builtin), is_builtin_rule (false), kind (kind) {} @@ -507,10 +506,11 @@ class MacroRulesDefinition : public VisItem MacroTranscriberFunc associated_transcriber, MacroKind kind, Visibility vis) : VisItem (std::move (vis), std::vector ()), - outer_attrs (std::vector ()), rule_name (builtin_name), - delim_type (delim_type), rules (std::vector ()), - locus (UNDEF_LOCATION), associated_transcriber (associated_transcriber), - is_builtin_rule (true), kind (kind) + LocatedImpl (UNDEF_LOCATION), outer_attrs (std::vector ()), + rule_name (builtin_name), delim_type (delim_type), + rules (std::vector ()), + associated_transcriber (associated_transcriber), is_builtin_rule (true), + kind (kind) {} public: @@ -552,8 +552,6 @@ class MacroRulesDefinition : public VisItem std::vector &get_macro_rules () { return rules; } const std::vector &get_macro_rules () const { return rules; } - location_t get_locus () const override final { return locus; } - Identifier get_rule_name () const { return rule_name; } std::vector &get_rules () { return rules; } @@ -578,6 +576,11 @@ class MacroRulesDefinition : public VisItem MacroKind get_kind () const { return kind; } + std::unique_ptr clone_macro_rules_def () const + { + return std::unique_ptr (clone_item_impl ()); + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -596,9 +599,8 @@ class MacroRulesDefinition : public VisItem class MacroInvocation : public TypeNoBounds, public Pattern, public Item, - public TraitItem, - public TraitImplItem, - public InherentImplItem, + public AssociatedItem, + virtual public LocatedImpl, public ExternalItem, public ExprWithoutBlock { @@ -646,8 +648,6 @@ class MacroInvocation : public TypeNoBounds, std::move (pending_eager_invocations))); } - location_t get_locus () const override final { return locus; } - void accept_vis (ASTVisitor &vis) override; // Invalid if path is empty, so base stripping on that. @@ -668,17 +668,12 @@ class MacroInvocation : public TypeNoBounds, outer_attrs = std::move (new_attrs); } - NodeId get_pattern_node_id () const override final - { - return ExprWithoutBlock::get_node_id (); - } - AST::Kind get_ast_kind () const override { return AST::Kind::MACRO_INVOCATION; } - NodeId get_macro_node_id () const { return node_id; } + NodeId get_macro_node_id () const { return macro_node_id; } MacroInvocData &get_invoc_data () { return invoc_data; } @@ -715,18 +710,18 @@ class MacroInvocation : public TypeNoBounds, MacroInvocData invoc_data, std::vector outer_attrs, location_t locus, bool is_semi_coloned, std::vector> &&pending_eager_invocs) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), locus (locus), - node_id (Analysis::Mappings::get ()->get_next_node_id ()), + : LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), + macro_node_id (Analysis::Mappings::get ()->get_next_node_id ()), invoc_data (std::move (invoc_data)), is_semi_coloned (is_semi_coloned), kind (kind), builtin_kind (builtin_kind), pending_eager_invocs (std::move (pending_eager_invocs)) {} MacroInvocation (const MacroInvocation &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), - locus (other.locus), node_id (other.node_id), - invoc_data (other.invoc_data), is_semi_coloned (other.is_semi_coloned), - kind (other.kind), builtin_kind (other.builtin_kind) + : NodeIdStore (other), LocatedImpl (other), outer_attrs (other.outer_attrs), + macro_node_id (other.macro_node_id), invoc_data (other.invoc_data), + is_semi_coloned (other.is_semi_coloned), kind (other.kind), + builtin_kind (other.builtin_kind) { if (other.kind == InvocKind::Builtin) for (auto &pending : other.pending_eager_invocs) @@ -735,8 +730,7 @@ class MacroInvocation : public TypeNoBounds, } std::vector outer_attrs; - location_t locus; - NodeId node_id; + NodeId macro_node_id; /* The data given to the macro invocation */ MacroInvocData invoc_data; @@ -803,20 +797,10 @@ class MacroInvocation : public TypeNoBounds, bool is_item () const override { return !has_semicolon (); } - TraitItem *clone_trait_item_impl () const override - { - return clone_macro_invocation_impl (); - }; - - TraitImplItem *clone_trait_impl_item_impl () const override + AssociatedItem *clone_associated_item_impl () const override { return clone_macro_invocation_impl (); }; - - InherentImplItem *clone_inherent_impl_item_impl () const override - { - return clone_macro_invocation_impl (); - } }; // more generic meta item path-only form diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h index 912a64183857..c6fbff50174e 100644 --- a/gcc/rust/ast/rust-path.h +++ b/gcc/rust/ast/rust-path.h @@ -467,13 +467,12 @@ struct GenericArgs /* A segment of a path in expression, including an identifier aspect and maybe * generic args */ -class PathExprSegment +class PathExprSegment : virtual public NodeIdStore { // or should this extend PathIdentSegment? private: PathIdentSegment segment_name; GenericArgs generic_args; location_t locus; - NodeId node_id; public: // Returns true if there are any generic arguments @@ -483,8 +482,7 @@ class PathExprSegment PathExprSegment (PathIdentSegment segment_name, location_t locus, GenericArgs generic_args = GenericArgs::create_empty ()) : segment_name (std::move (segment_name)), - generic_args (std::move (generic_args)), locus (locus), - node_id (Analysis::Mappings::get ()->get_next_node_id ()) + generic_args (std::move (generic_args)), locus (locus) {} /* Constructor for segment with generic arguments (from segment name and all @@ -497,7 +495,7 @@ class PathExprSegment generic_args (GenericArgs (std::move (lifetime_args), std::move (generic_args), std::move (binding_args))), - locus (locus), node_id (Analysis::Mappings::get ()->get_next_node_id ()) + locus (locus) {} // Returns whether path expression segment is in an error state. @@ -523,8 +521,6 @@ class PathExprSegment PathIdentSegment &get_ident_segment () { return segment_name; } const PathIdentSegment &get_ident_segment () const { return segment_name; } - NodeId get_node_id () const { return node_id; } - bool is_super_path_seg () const { return !has_generic_args () && get_ident_segment ().is_super_segment (); @@ -579,12 +575,13 @@ class PathPattern : public Pattern /* AST node representing a path-in-expression pattern (path that allows * generic arguments) */ -class PathInExpression : public PathPattern, public PathExpr +class PathInExpression : public PathPattern, + public PathExpr, + virtual public NodeIdStore { std::vector outer_attrs; bool has_opening_scope_resolution; location_t locus; - NodeId _node_id; public: std::string as_string () const override; @@ -595,8 +592,7 @@ class PathInExpression : public PathPattern, public PathExpr bool has_opening_scope_resolution = false) : PathPattern (std::move (path_segments)), outer_attrs (std::move (outer_attrs)), - has_opening_scope_resolution (has_opening_scope_resolution), - locus (locus), _node_id (Analysis::Mappings::get ()->get_next_node_id ()) + has_opening_scope_resolution (has_opening_scope_resolution), locus (locus) {} // Creates an error state path in expression. @@ -634,8 +630,6 @@ class PathInExpression : public PathPattern, public PathExpr return has_opening_scope_resolution; } - NodeId get_node_id () const override { return _node_id; } - const std::vector &get_outer_attrs () const { return outer_attrs; } std::vector &get_outer_attrs () override { return outer_attrs; } @@ -644,8 +638,6 @@ class PathInExpression : public PathPattern, public PathExpr outer_attrs = std::move (new_attrs); } - NodeId get_pattern_node_id () const override final { return get_node_id (); } - protected: /* Use covariance to implement clone function as returning this object * rather than base */ @@ -1037,17 +1029,16 @@ class TypePath : public TypeNoBounds // Constructor TypePath (std::vector > segments, location_t locus, bool has_opening_scope_resolution = false) - : TypeNoBounds (), - has_opening_scope_resolution (has_opening_scope_resolution), + : has_opening_scope_resolution (has_opening_scope_resolution), segments (std::move (segments)), locus (locus) {} // Copy constructor with vector clone TypePath (TypePath const &other) - : has_opening_scope_resolution (other.has_opening_scope_resolution), + : TypeNoBounds (other), + has_opening_scope_resolution (other.has_opening_scope_resolution), locus (other.locus) { - node_id = other.node_id; segments.reserve (other.segments.size ()); for (const auto &e : other.segments) segments.push_back (e->clone_type_path_segment ()); @@ -1056,7 +1047,7 @@ class TypePath : public TypeNoBounds // Overloaded assignment operator with clone TypePath &operator= (TypePath const &other) { - node_id = other.node_id; + TypeNoBounds::operator= (other); has_opening_scope_resolution = other.has_opening_scope_resolution; locus = other.locus; @@ -1188,7 +1179,6 @@ class QualifiedPathInExpression : public PathPattern, public PathExpr std::vector outer_attrs; QualifiedPathType path_type; location_t locus; - NodeId _node_id; public: std::string as_string () const override; @@ -1199,8 +1189,7 @@ class QualifiedPathInExpression : public PathPattern, public PathExpr location_t locus) : PathPattern (std::move (path_segments)), outer_attrs (std::move (outer_attrs)), - path_type (std::move (qual_path_type)), locus (locus), - _node_id (Analysis::Mappings::get ()->get_next_node_id ()) + path_type (std::move (qual_path_type)), locus (locus) {} /* TODO: maybe make a shortcut constructor that has QualifiedPathType @@ -1242,10 +1231,6 @@ class QualifiedPathInExpression : public PathPattern, public PathExpr outer_attrs = std::move (new_attrs); } - NodeId get_node_id () const override { return _node_id; } - - NodeId get_pattern_node_id () const override final { return get_node_id (); } - protected: /* Use covariance to implement clone function as returning this object * rather than base */ diff --git a/gcc/rust/ast/rust-pattern.h b/gcc/rust/ast/rust-pattern.h index 85ab627697cd..79eef6782434 100644 --- a/gcc/rust/ast/rust-pattern.h +++ b/gcc/rust/ast/rust-pattern.h @@ -51,8 +51,6 @@ class LiteralPattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - Literal &get_literal () { return lit; } const Literal &get_literal () const { return lit; } @@ -151,8 +149,6 @@ class IdentifierPattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -181,8 +177,6 @@ class WildcardPattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -208,8 +202,6 @@ class RestPattern : public Pattern void accept_vis (ASTVisitor &vis) override; - NodeId get_pattern_node_id () const override final { return node_id; } - protected: RestPattern *clone_pattern_impl () const override { @@ -437,8 +429,6 @@ class RangePattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -507,8 +497,6 @@ class ReferencePattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -944,8 +932,6 @@ class StructPattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1183,8 +1169,6 @@ class TupleStructPattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1425,8 +1409,6 @@ class TuplePattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1487,8 +1469,6 @@ class GroupedPattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1553,8 +1533,6 @@ class SlicePattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1620,8 +1598,6 @@ class AltPattern : public Pattern NodeId get_node_id () const { return node_id; } - NodeId get_pattern_node_id () const override final { return node_id; } - protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/ast/rust-stmt.h b/gcc/rust/ast/rust-stmt.h index c97dd7b6f929..4d5ded7b6ecd 100644 --- a/gcc/rust/ast/rust-stmt.h +++ b/gcc/rust/ast/rust-stmt.h @@ -183,15 +183,12 @@ class LetStmt : public Stmt }; // Expression statements (statements containing an expression) -class ExprStmt : public Stmt +class ExprStmt : public Stmt, public LocatedImpl { std::unique_ptr expr; - location_t locus; bool semicolon_followed; public: - location_t get_locus () const override final { return locus; } - bool is_item () const override final { return false; } bool is_expr () const override final { return true; } @@ -208,13 +205,13 @@ class ExprStmt : public Stmt ExprStmt (std::unique_ptr &&expr, location_t locus, bool semicolon_followed) - : expr (std::move (expr)), locus (locus), + : LocatedImpl (locus), expr (std::move (expr)), semicolon_followed (semicolon_followed) {} // Copy constructor with clone ExprStmt (ExprStmt const &other) - : locus (other.locus), semicolon_followed (other.semicolon_followed) + : LocatedImpl (other), semicolon_followed (other.semicolon_followed) { // guard to prevent null dereference (only required if error state) if (other.expr != nullptr) @@ -225,6 +222,7 @@ class ExprStmt : public Stmt ExprStmt &operator= (ExprStmt const &other) { Stmt::operator= (other); + LocatedImpl::operator= (other); // guard to prevent null dereference (only required if error state) if (other.expr != nullptr) @@ -232,7 +230,6 @@ class ExprStmt : public Stmt else expr = nullptr; - locus = other.locus; semicolon_followed = other.semicolon_followed; return *this; diff --git a/gcc/rust/expand/rust-derive-clone.cc b/gcc/rust/expand/rust-derive-clone.cc index 964602b98b9e..f1cac103957f 100644 --- a/gcc/rust/expand/rust-derive-clone.cc +++ b/gcc/rust/expand/rust-derive-clone.cc @@ -42,14 +42,14 @@ DeriveClone::clone_call (std::unique_ptr &&to_clone) * fn clone(&self) -> Self { } * */ -std::unique_ptr +std::unique_ptr DeriveClone::clone_fn (std::unique_ptr &&clone_expr) { auto block = std::unique_ptr ( new BlockExpr ({}, std::move (clone_expr), {}, {}, loc, loc)); auto big_self_type = builder.single_type_path ("Self"); - return std::unique_ptr ( + return std::unique_ptr ( new Method ({"clone"}, builder.fn_qualifiers (), /* generics */ {}, SelfParam (Lifetime::error (), /* is_mut */ false, loc), /* function params */ {}, std::move (big_self_type), @@ -66,7 +66,7 @@ DeriveClone::clone_fn (std::unique_ptr &&clone_expr) * */ std::unique_ptr -DeriveClone::clone_impl (std::unique_ptr &&clone_fn, +DeriveClone::clone_impl (std::unique_ptr &&clone_fn, std::string name) { // should that be `$crate::core::clone::Clone` instead? @@ -74,7 +74,7 @@ DeriveClone::clone_impl (std::unique_ptr &&clone_fn, segments.emplace_back (builder.type_path_segment ("Clone")); auto clone = TypePath (std::move (segments), loc); - auto trait_items = std::vector> (); + auto trait_items = std::vector> (); trait_items.emplace_back (std::move (clone_fn)); return std::unique_ptr ( diff --git a/gcc/rust/expand/rust-derive-clone.h b/gcc/rust/expand/rust-derive-clone.h index dcb88f9468ab..1009247e8826 100644 --- a/gcc/rust/expand/rust-derive-clone.h +++ b/gcc/rust/expand/rust-derive-clone.h @@ -49,7 +49,7 @@ class DeriveClone : DeriveVisitor * fn clone(&self) -> Self { } * */ - std::unique_ptr clone_fn (std::unique_ptr &&clone_expr); + std::unique_ptr clone_fn (std::unique_ptr &&clone_expr); /** * Create the Clone trait implementation for a type @@ -59,7 +59,7 @@ class DeriveClone : DeriveVisitor * } * */ - std::unique_ptr clone_impl (std::unique_ptr &&clone_fn, + std::unique_ptr clone_impl (std::unique_ptr &&clone_fn, std::string name); virtual void visit_struct (StructStruct &item); diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc index 55d5de0a04ec..e33fd4e3a77a 100644 --- a/gcc/rust/expand/rust-expand-visitor.cc +++ b/gcc/rust/expand/rust-expand-visitor.cc @@ -1186,8 +1186,9 @@ ExpandVisitor::visit (AST::Trait &trait) expander.push_context (MacroExpander::ContextType::TRAIT); - std::function (AST::SingleASTNode)> extractor - = [] (AST::SingleASTNode node) { return node.take_trait_item (); }; + std::function (AST::SingleASTNode)> + extractor + = [] (AST::SingleASTNode node) { return node.take_assoc_item (); }; expand_macro_children (MacroExpander::ContextType::TRAIT, trait.get_trait_items (), extractor); @@ -1213,8 +1214,9 @@ ExpandVisitor::visit (AST::InherentImpl &impl) if (impl.has_where_clause ()) expand_where_clause (impl.get_where_clause ()); - std::function (AST::SingleASTNode)> - extractor = [] (AST::SingleASTNode node) { return node.take_impl_item (); }; + std::function (AST::SingleASTNode)> + extractor + = [] (AST::SingleASTNode node) { return node.take_assoc_item (); }; expand_macro_children (MacroExpander::ContextType::IMPL, impl.get_impl_items (), extractor); @@ -1240,9 +1242,9 @@ ExpandVisitor::visit (AST::TraitImpl &impl) if (impl.has_where_clause ()) expand_where_clause (impl.get_where_clause ()); - std::function (AST::SingleASTNode)> + std::function (AST::SingleASTNode)> extractor - = [] (AST::SingleASTNode node) { return node.take_trait_impl_item (); }; + = [] (AST::SingleASTNode node) { return node.take_assoc_item (); }; expand_macro_children (MacroExpander::ContextType::TRAIT_IMPL, impl.get_impl_items (), extractor); diff --git a/gcc/rust/hir/rust-ast-lower-implitem.h b/gcc/rust/hir/rust-ast-lower-implitem.h index c8f64402c2f1..abb0227426a1 100644 --- a/gcc/rust/hir/rust-ast-lower-implitem.h +++ b/gcc/rust/hir/rust-ast-lower-implitem.h @@ -32,31 +32,7 @@ class ASTLowerImplItem : public ASTLoweringBase using Rust::HIR::ASTLoweringBase::visit; public: - static HIR::ImplItem *translate (AST::InherentImplItem *item, - HirId parent_impl_id) - { - ASTLowerImplItem resolver; - item->accept_vis (resolver); - - if (resolver.translated != nullptr) - { - rust_assert (resolver.item_cast != nullptr); - - auto id = resolver.translated->get_impl_mappings ().get_hirid (); - auto defid = resolver.translated->get_impl_mappings ().get_defid (); - auto locus = resolver.translated->get_locus (); - - resolver.handle_outer_attributes (*resolver.item_cast); - resolver.mappings->insert_hir_implitem (parent_impl_id, - resolver.translated); - resolver.mappings->insert_location (id, locus); - resolver.mappings->insert_defid_mapping (defid, resolver.item_cast); - } - - return resolver.translated; - } - - static HIR::ImplItem *translate (AST::TraitImplItem *item, + static HIR::ImplItem *translate (AST::AssociatedItem *item, HirId parent_impl_id) { ASTLowerImplItem resolver; @@ -312,7 +288,7 @@ class ASTLowerTraitItem : public ASTLoweringBase using Rust::HIR::ASTLoweringBase::visit; public: - static HIR::TraitItem *translate (AST::TraitItem *item) + static HIR::TraitItem *translate (AST::AssociatedItem *item) { ASTLowerTraitItem resolver; item->accept_vis (resolver); diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index be3a7b8f3fbf..84b479a009c7 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -4899,12 +4899,12 @@ Parser::parse_trait (AST::Visibility vis, AST::AttrVec inner_attrs = parse_inner_attributes (); // parse trait items - std::vector> trait_items; + std::vector> trait_items; const_TokenPtr t = lexer.peek_token (); while (t->get_id () != RIGHT_CURLY) { - std::unique_ptr trait_item = parse_trait_item (); + std::unique_ptr trait_item = parse_trait_item (); if (trait_item == nullptr) { @@ -4948,7 +4948,7 @@ Parser::parse_trait (AST::Visibility vis, // Parses a trait item used inside traits (not trait, the Item). template -std::unique_ptr +std::unique_ptr Parser::parse_trait_item () { // parse outer attributes (if they exist) @@ -5093,7 +5093,7 @@ Parser::parse_trait_item () } default: { // TODO: try and parse macro invocation semi - if fails, maybe error. - std::unique_ptr macro_invoc + std::unique_ptr macro_invoc = parse_macro_invocation_semi (outer_attrs); if (macro_invoc == nullptr) @@ -5272,13 +5272,13 @@ Parser::parse_impl (AST::Visibility vis, // parse inner attributes (optional) AST::AttrVec inner_attrs = parse_inner_attributes (); - // parse inherent impl items - std::vector> impl_items; + // parse associated items + std::vector> impl_items; const_TokenPtr t = lexer.peek_token (); while (t->get_id () != RIGHT_CURLY) { - std::unique_ptr impl_item + std::unique_ptr impl_item = parse_inherent_impl_item (); if (impl_item == nullptr) @@ -5348,12 +5348,12 @@ Parser::parse_impl (AST::Visibility vis, AST::AttrVec inner_attrs = parse_inner_attributes (); // parse trait impl items - std::vector> impl_items; + std::vector> impl_items; const_TokenPtr t = lexer.peek_token (); while (t->get_id () != RIGHT_CURLY) { - std::unique_ptr impl_item + std::unique_ptr impl_item = parse_trait_impl_item (); if (impl_item == nullptr) @@ -5397,7 +5397,7 @@ Parser::parse_impl (AST::Visibility vis, // Parses a single inherent impl item (item inside an inherent impl block). template -std::unique_ptr +std::unique_ptr Parser::parse_inherent_impl_item () { // parse outer attributes (if they exist) @@ -5514,7 +5514,7 @@ Parser::parse_inherent_impl_item () // InherentImplItem is this specialisation of the template while TraitImplItem // will be the other. template -std::unique_ptr +std::unique_ptr Parser::parse_inherent_impl_function_or_method ( AST::Visibility vis, AST::AttrVec outer_attrs) { @@ -5621,7 +5621,7 @@ Parser::parse_inherent_impl_function_or_method ( // Parses a single trait impl item (item inside a trait impl block). template -std::unique_ptr +std::unique_ptr Parser::parse_trait_impl_item () { // parse outer attributes (if they exist) @@ -5696,7 +5696,7 @@ Parser::parse_trait_impl_item () * smaller ones and prevents duplication of logic. Strictly, this parses a * function or method item inside a trait impl item block. */ template -std::unique_ptr +std::unique_ptr Parser::parse_trait_impl_function_or_method ( AST::Visibility vis, AST::AttrVec outer_attrs) { diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 904b5028a755..06ab850c5c90 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -158,9 +158,9 @@ template class Parser = ParseRestrictions ()); std::unique_ptr parse_type (bool save_errors = true); std::unique_ptr parse_external_item (); - std::unique_ptr parse_trait_item (); - std::unique_ptr parse_inherent_impl_item (); - std::unique_ptr parse_trait_impl_item (); + std::unique_ptr parse_trait_item (); + std::unique_ptr parse_inherent_impl_item (); + std::unique_ptr parse_trait_impl_item (); AST::PathInExpression parse_path_in_expression (); std::vector > parse_lifetime_params (); AST::Visibility parse_visibility (); @@ -328,10 +328,10 @@ template class Parser AST::SelfParam parse_self_param (); std::unique_ptr parse_impl (AST::Visibility vis, AST::AttrVec outer_attrs); - std::unique_ptr + std::unique_ptr parse_inherent_impl_function_or_method (AST::Visibility vis, AST::AttrVec outer_attrs); - std::unique_ptr + std::unique_ptr parse_trait_impl_function_or_method (AST::Visibility vis, AST::AttrVec outer_attrs); std::unique_ptr diff --git a/gcc/rust/resolve/rust-ast-resolve-implitem.h b/gcc/rust/resolve/rust-ast-resolve-implitem.h index 9a34aa83889c..aa7ffe3d796b 100644 --- a/gcc/rust/resolve/rust-ast-resolve-implitem.h +++ b/gcc/rust/resolve/rust-ast-resolve-implitem.h @@ -31,16 +31,7 @@ class ResolveToplevelImplItem : public ResolverBase using Rust::Resolver::ResolverBase::visit; public: - static void go (AST::InherentImplItem *item, const CanonicalPath &prefix) - { - if (item->is_marked_for_strip ()) - return; - - ResolveToplevelImplItem resolver (prefix); - item->accept_vis (resolver); - } - - static void go (AST::TraitImplItem *item, const CanonicalPath &prefix) + static void go (AST::AssociatedItem *item, const CanonicalPath &prefix) { if (item->is_marked_for_strip ()) return; @@ -128,7 +119,7 @@ class ResolveTopLevelTraitItems : public ResolverBase using Rust::Resolver::ResolverBase::visit; public: - static void go (AST::TraitItem *item, const CanonicalPath &prefix, + static void go (AST::AssociatedItem *item, const CanonicalPath &prefix, const CanonicalPath &canonical_prefix) { ResolveTopLevelTraitItems resolver (prefix, canonical_prefix); diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc index baab62209994..3b730924c277 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.cc +++ b/gcc/rust/resolve/rust-ast-resolve-item.cc @@ -33,7 +33,7 @@ ResolveTraitItems::ResolveTraitItems (const CanonicalPath &prefix, {} void -ResolveTraitItems::go (AST::TraitItem *item, const CanonicalPath &prefix, +ResolveTraitItems::go (AST::AssociatedItem *item, const CanonicalPath &prefix, const CanonicalPath &canonical_prefix) { if (item->is_marked_for_strip ()) @@ -881,15 +881,7 @@ ResolveItem::visit (AST::ExternBlock &extern_block) } void -ResolveItem::resolve_impl_item (AST::TraitImplItem *item, - const CanonicalPath &prefix, - const CanonicalPath &canonical_prefix) -{ - ResolveImplItems::go (item, prefix, canonical_prefix); -} - -void -ResolveItem::resolve_impl_item (AST::InherentImplItem *item, +ResolveItem::resolve_impl_item (AST::AssociatedItem *item, const CanonicalPath &prefix, const CanonicalPath &canonical_prefix) { @@ -1062,18 +1054,7 @@ ResolveImplItems::ResolveImplItems (const CanonicalPath &prefix, {} void -ResolveImplItems::go (AST::InherentImplItem *item, const CanonicalPath &prefix, - const CanonicalPath &canonical_prefix) -{ - if (item->is_marked_for_strip ()) - return; - - ResolveImplItems resolver (prefix, canonical_prefix); - item->accept_vis (resolver); -} - -void -ResolveImplItems::go (AST::TraitImplItem *item, const CanonicalPath &prefix, +ResolveImplItems::go (AST::AssociatedItem *item, const CanonicalPath &prefix, const CanonicalPath &canonical_prefix) { if (item->is_marked_for_strip ()) diff --git a/gcc/rust/resolve/rust-ast-resolve-item.h b/gcc/rust/resolve/rust-ast-resolve-item.h index db47df5713a5..273d5c1c9efd 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.h +++ b/gcc/rust/resolve/rust-ast-resolve-item.h @@ -32,7 +32,7 @@ class ResolveTraitItems : public ResolverBase using Rust::Resolver::ResolverBase::visit; public: - static void go (AST::TraitItem *item, const CanonicalPath &prefix, + static void go (AST::AssociatedItem *item, const CanonicalPath &prefix, const CanonicalPath &canonical_prefix); void visit (AST::TraitItemType &type) override; @@ -78,9 +78,7 @@ class ResolveItem : public ResolverBase void visit (AST::UseDeclaration &) override; protected: - void resolve_impl_item (AST::TraitImplItem *item, const CanonicalPath &prefix, - const CanonicalPath &canonical_prefix); - void resolve_impl_item (AST::InherentImplItem *item, + void resolve_impl_item (AST::AssociatedItem *item, const CanonicalPath &prefix, const CanonicalPath &canonical_prefix); void resolve_extern_item (AST::ExternalItem *item); @@ -97,9 +95,7 @@ class ResolveImplItems : public ResolveItem using Rust::Resolver::ResolveItem::visit; public: - static void go (AST::InherentImplItem *item, const CanonicalPath &prefix, - const CanonicalPath &canonical_prefix); - static void go (AST::TraitImplItem *item, const CanonicalPath &prefix, + static void go (AST::AssociatedItem *item, const CanonicalPath &prefix, const CanonicalPath &canonical_prefix); void visit (AST::TypeAlias &alias) override; diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc b/gcc/rust/resolve/rust-early-name-resolver.cc index dc7ad16039fd..32522d52cac0 100644 --- a/gcc/rust/resolve/rust-early-name-resolver.cc +++ b/gcc/rust/resolve/rust-early-name-resolver.cc @@ -953,7 +953,7 @@ EarlyNameResolver::visit (AST::MacroInvocation &invoc) if (has_semicolon) source_node = invoc.get_macro_node_id (); else - source_node = invoc.get_pattern_node_id (); + source_node = invoc.get_node_id (); auto seg = CanonicalPath::new_seg (source_node, invoc_data.get_path ().as_string ()); diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index a15d310ce6d2..1f76ef77574d 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -917,7 +917,8 @@ Mappings::insert_macro_invocation (AST::MacroInvocation &invoc, auto it = macroInvocations.find (invoc.get_macro_node_id ()); rust_assert (it == macroInvocations.end ()); - macroInvocations[invoc.get_macro_node_id ()] = def; + // TODO: remove hack that converts use-after-free into memory leak + macroInvocations[invoc.get_macro_node_id ()] = def->clone_macro_rules_def ().release (); } bool