From 6fd301e0269f73a6fa7d5a067af311d4e1e6a8f1 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Sat, 6 Dec 2025 22:23:00 -0500 Subject: [PATCH 1/2] stash --- gcc/rust/ast/rust-ast-builder.cc | 7 +- gcc/rust/ast/rust-ast.cc | 4 +- gcc/rust/ast/rust-ast.h | 191 +++--------- gcc/rust/ast/rust-expr.h | 58 ++-- gcc/rust/ast/rust-item.h | 139 +++------ gcc/rust/ast/rust-macro.h | 22 +- gcc/rust/ast/rust-path.h | 86 ++--- gcc/rust/ast/rust-pattern.h | 149 +++------ gcc/rust/ast/rust-type.h | 123 ++------ gcc/rust/expand/rust-derive-default.cc | 4 +- gcc/rust/expand/rust-derive-eq.cc | 10 +- gcc/rust/expand/rust-expand-visitor.cc | 6 + gcc/rust/expand/rust-expand-visitor.h | 5 - gcc/rust/expand/rust-macro-expand.cc | 3 + gcc/rust/expand/rust-node-id-visitor.h | 295 ++++++++++++++++++ gcc/rust/hir/rust-ast-lower-item.cc | 2 +- .../resolve/rust-late-name-resolver-2.0.cc | 4 + gcc/rust/resolve/rust-name-resolver.cc | 3 +- .../rust-toplevel-name-resolver-2.0.cc | 24 +- gcc/rust/rust-session-manager.cc | 3 + gcc/rust/util/rust-hir-map.cc | 4 + gcc/rust/util/rust-mapping-common.h | 1 + 22 files changed, 538 insertions(+), 605 deletions(-) create mode 100644 gcc/rust/expand/rust-node-id-visitor.h diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc index 632f9455e3b6..a08fc56edea5 100644 --- a/gcc/rust/ast/rust-ast-builder.cc +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -587,7 +587,7 @@ Builder::new_type_param ( std::unique_ptr type = nullptr; if (param.has_type ()) - type = param.get_type ().reconstruct (); + type = param.get_type ().clone_type (); for (auto &&extra_bound : extra_bounds) type_param_bounds.emplace_back (std::move (extra_bound)); @@ -708,7 +708,7 @@ Builder::new_generic_args (GenericArgs &args) for (auto &binding : args.get_binding_args ()) { Type &t = *binding.get_type_ptr ().get (); - std::unique_ptr ty = t.reconstruct (); + std::unique_ptr ty = t.clone_type (); binding_args.emplace_back (binding.get_identifier (), std::move (ty), binding.get_locus ()); } @@ -720,7 +720,7 @@ Builder::new_generic_args (GenericArgs &args) switch (arg.get_kind ()) { case GenericArg::Kind::Type: - new_arg = GenericArg::create_type (arg.get_type ().reconstruct ()); + new_arg = GenericArg::create_type (arg.get_type ().clone_type ()); break; case GenericArg::Kind::Either: new_arg @@ -729,7 +729,6 @@ Builder::new_generic_args (GenericArgs &args) case GenericArg::Kind::Const: new_arg = GenericArg::create_const (arg.get_expression ().clone_expr ()); - // FIXME: Use `reconstruct()` here, not `clone_expr()` break; } diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 851f7ea4b6f6..1648fa03f1fe 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -1082,7 +1082,7 @@ Union::as_string () const } Function::Function (Function const &other) - : VisItem (other), ExternalItem (other.get_node_id ()), + : VisItem (other), AssociatedItem (other), ExternalItem (other), qualifiers (other.qualifiers), function_name (other.function_name), where_clause (other.where_clause), locus (other.locus), has_default (other.has_default), @@ -1111,6 +1111,8 @@ Function & Function::operator= (Function const &other) { VisItem::operator= (other); + AssociatedItem::operator= (other); + ExternalItem::operator= (other); function_name = other.function_name; qualifiers = other.qualifiers; where_clause = other.where_clause; diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 8610ade830c1..f9601b6acbac 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -33,6 +33,26 @@ typedef int TupleIndex; struct Session; struct MacroExpander; +class HasNodeId +{ + NodeId node_id; + +public: + HasNodeId (): node_id (UNKNOWN_NODEID) {} + + NodeId get_node_id () const + { + rust_assert (node_id != UNKNOWN_NODEID); + return node_id; + } + + void assign_node_id () + { + if (node_id == UNKNOWN_NODEID) + node_id = Analysis::Mappings::get ().get_next_node_id (); + } +}; + class Identifier { public: @@ -83,38 +103,6 @@ class Visitable virtual void accept_vis (ASTVisitor &vis) = 0; }; -/** - * Base function for reconstructing and asserting that the new NodeId is - * different from the old NodeId. It then wraps the given pointer into a unique - * pointer and returns it. - */ -template -std::unique_ptr -reconstruct_base (const T *instance) -{ - auto *reconstructed = instance->reconstruct_impl (); - - rust_assert (reconstructed->get_node_id () != instance->get_node_id ()); - - return std::unique_ptr (reconstructed); -} - -/** - * Reconstruct multiple items in a vector - */ -template -std::vector> -reconstruct_vec (const std::vector> &to_reconstruct) -{ - std::vector> reconstructed; - reconstructed.reserve (to_reconstruct.size ()); - - for (const auto &elt : to_reconstruct) - reconstructed.emplace_back (std::unique_ptr (elt->reconstruct_impl ())); - - return reconstructed; -} - // Delimiter types - used in macros and whatever. enum DelimType { @@ -323,18 +311,16 @@ class PathSegment }; // A segment of a simple path without generic or type arguments -class SimplePathSegment : public PathSegment +class SimplePathSegment : public PathSegment, public HasNodeId { 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 @@ -350,7 +336,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 { @@ -371,12 +356,11 @@ class SimplePathSegment : public PathSegment }; // A simple path without generic or type arguments -class SimplePath +class SimplePath : public HasNodeId { bool opening_scope_resolution; std::vector segments; location_t locus; - NodeId node_id; public: // Constructor @@ -384,15 +368,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) {} explicit 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. @@ -412,7 +394,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 @@ -1054,7 +1035,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 Visitable +class Stmt : public Visitable, public virtual HasNodeId { public: enum class Kind @@ -1081,9 +1062,6 @@ class Stmt : public Visitable virtual void mark_for_strip () = 0; virtual bool is_marked_for_strip () const = 0; - // TODO: put this in a virtual base class? - virtual NodeId get_node_id () const { return node_id; } - virtual Kind get_stmt_kind () = 0; // TODO: Can we remove these two? @@ -1093,12 +1071,10 @@ class Stmt : public Visitable virtual void add_semicolon () {} protected: - Stmt () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} + Stmt () {} // 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) @@ -1175,12 +1151,13 @@ class VisItem : public Item // Visibility copy constructor VisItem (VisItem const &other) - : visibility (other.visibility), outer_attrs (other.outer_attrs) + : Item (other), visibility (other.visibility), outer_attrs (other.outer_attrs) {} // Overload assignment operator to clone VisItem &operator= (VisItem const &other) { + Item::operator= (other); visibility = other.visibility; outer_attrs = other.outer_attrs; @@ -1215,7 +1192,7 @@ class VisItem : public Item class ExprWithoutBlock; // Base expression AST node - abstract -class Expr : public Visitable +class Expr : public Visitable, public virtual HasNodeId { public: enum class Kind @@ -1295,10 +1272,6 @@ class Expr : public Visitable 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; // TODO: think of less hacky way to implement this kind of thing @@ -1307,12 +1280,10 @@ class Expr : public Visitable protected: // Constructor - Expr () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} + Expr () {} // 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 @@ -1400,7 +1371,7 @@ class IdentifierExpr : public ExprWithoutBlock }; // Pattern base AST node -class Pattern : public Visitable +class Pattern : public Visitable, public virtual HasNodeId { public: enum class Kind @@ -1440,7 +1411,6 @@ class Pattern : public Visitable virtual bool is_marked_for_strip () const { return false; } virtual location_t get_locus () const = 0; - virtual NodeId get_node_id () const = 0; protected: // Clone pattern implementation as pure virtual method @@ -1451,7 +1421,7 @@ class Pattern : public Visitable class TraitBound; // Base class for types as represented in AST - abstract -class Type : public Visitable +class Type : public Visitable, public virtual HasNodeId { public: // Unique pointer custom clone function @@ -1460,10 +1430,6 @@ class Type : public Visitable return std::unique_ptr (clone_type_impl ()); } - // Similar to `clone_type`, but generates a new instance of the node with a - // different NodeId - std::unique_ptr reconstruct () const { return reconstruct_base (this); } - // virtual destructor virtual ~Type () {} @@ -1481,18 +1447,11 @@ class Type : public Visitable virtual location_t get_locus () const = 0; - // TODO: put this in a virtual base class? - virtual NodeId get_node_id () const { return node_id; } - virtual Type *reconstruct_impl () const = 0; - protected: - Type () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} - Type (NodeId node_id) : node_id (node_id) {} + Type () {} // Clone and reconstruct function implementations as pure virtual methods virtual Type *clone_type_impl () const = 0; - - NodeId node_id; }; // A type without parentheses? - abstract @@ -1505,13 +1464,6 @@ class TypeNoBounds : public Type return std::unique_ptr (clone_type_no_bounds_impl ()); } - std::unique_ptr reconstruct () const - { - return reconstruct_base (this); - } - - virtual TypeNoBounds *reconstruct_impl () const override = 0; - protected: // Clone function implementation as pure virtual method virtual TypeNoBounds *clone_type_no_bounds_impl () const = 0; @@ -1529,7 +1481,7 @@ class TypeNoBounds : public Type /* Abstract base class representing a type param bound - Lifetime and * TraitBound extends it */ -class TypeParamBound : public Visitable +class TypeParamBound : public Visitable, public HasNodeId { public: enum TypeParamBoundType @@ -1546,30 +1498,17 @@ class TypeParamBound : public Visitable return std::unique_ptr (clone_type_param_bound_impl ()); } - std::unique_ptr reconstruct () const - { - return reconstruct_base (this); - } - virtual std::string as_string () const = 0; - NodeId get_node_id () const { return node_id; } - virtual location_t get_locus () const = 0; virtual TypeParamBoundType get_bound_type () const = 0; - virtual TypeParamBound *reconstruct_impl () const = 0; - protected: // Clone function implementation as pure virtual method virtual TypeParamBound *clone_type_param_bound_impl () const = 0; - TypeParamBound () : node_id (Analysis::Mappings::get ().get_next_node_id ()) - {} - TypeParamBound (NodeId node_id) : node_id (node_id) {} - - NodeId node_id; + TypeParamBound () {} }; // Represents a lifetime (and is also a kind of type param bound) @@ -1587,20 +1526,12 @@ class Lifetime : public TypeParamBound LifetimeType lifetime_type; std::string lifetime_name; location_t locus; - NodeId node_id; public: // Constructor Lifetime (LifetimeType type, std::string name = std::string (), location_t locus = UNDEF_LOCATION) - : TypeParamBound (Analysis::Mappings::get ().get_next_node_id ()), - lifetime_type (type), lifetime_name (std::move (name)), locus (locus) - {} - - Lifetime (NodeId id, LifetimeType type, std::string name = std::string (), - location_t locus = UNDEF_LOCATION) - : TypeParamBound (id), lifetime_type (type), - lifetime_name (std::move (name)), locus (locus) + : lifetime_type (type), lifetime_name (std::move (name)), locus (locus) {} static Lifetime elided () { return Lifetime (WILDCARD, ""); } @@ -1625,10 +1556,6 @@ class Lifetime : public TypeParamBound /* Use covariance to implement clone function as returning this object * rather than base */ Lifetime *clone_type_param_bound_impl () const override - { - return new Lifetime (node_id, lifetime_type, lifetime_name, locus); - } - Lifetime *reconstruct_impl () const override { return new Lifetime (lifetime_type, lifetime_name, locus); } @@ -1636,7 +1563,7 @@ class Lifetime : public TypeParamBound /* Base generic parameter in AST. Abstract - can be represented by a Lifetime * or Type param */ -class GenericParam : public Visitable +class GenericParam : public Visitable, public HasNodeId { public: enum class Kind @@ -1660,16 +1587,11 @@ class GenericParam : public Visitable virtual Kind get_kind () const = 0; - NodeId get_node_id () const { return node_id; } - protected: - GenericParam () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} - GenericParam (NodeId node_id) : node_id (node_id) {} + GenericParam () {} // Clone function implementation as pure virtual method virtual GenericParam *clone_generic_param_impl () const = 0; - - NodeId node_id; }; // A lifetime generic parameter (as opposed to a type generic parameter) @@ -1725,7 +1647,7 @@ class LifetimeParam : public GenericParam } }; -class AssociatedItem : public Visitable +class AssociatedItem : public Visitable, public virtual HasNodeId { protected: // Clone function implementation as pure virtual method @@ -1745,8 +1667,6 @@ class AssociatedItem : public Visitable virtual bool is_marked_for_strip () const = 0; virtual location_t get_locus () const = 0; - - virtual NodeId get_node_id () const = 0; }; // Item used in trait declarations - abstract base class @@ -1754,19 +1674,16 @@ class TraitItem : public AssociatedItem { protected: TraitItem (location_t locus) - : node_id (Analysis::Mappings::get ().get_next_node_id ()), - vis (Visibility::create_private ()), locus (locus) + : vis (Visibility::create_private ()), locus (locus) {} TraitItem (Visibility vis, location_t locus) - : node_id (Analysis::Mappings::get ().get_next_node_id ()), vis (vis), - locus (locus) + : vis (vis), locus (locus) {} // Clone function implementation as pure virtual method virtual TraitItem *clone_associated_item_impl () const override = 0; - NodeId node_id; Visibility vis; location_t locus; @@ -1777,17 +1694,14 @@ class TraitItem : public AssociatedItem return std::unique_ptr (clone_associated_item_impl ()); } - NodeId get_node_id () const override { return node_id; } location_t get_locus () const override { return locus; } }; // Abstract base class for an item used inside an extern block -class ExternalItem : public Visitable +class ExternalItem : public Visitable, public virtual HasNodeId { public: - ExternalItem () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} - - ExternalItem (NodeId node_id) : node_id (node_id) {} + ExternalItem () {} virtual ~ExternalItem () {} @@ -1802,13 +1716,9 @@ class ExternalItem : public Visitable virtual void mark_for_strip () = 0; virtual bool is_marked_for_strip () const = 0; - virtual 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 @@ -2038,7 +1948,7 @@ class SingleASTNode : public Visitable }; // A crate AST object - holds all the data for a single compilation unit -struct Crate +struct Crate : public HasNodeId { std::vector inner_attrs; // dodgy spacing required here @@ -2046,19 +1956,16 @@ struct Crate * top-level one)? */ std::vector> items; - NodeId node_id; - public: // Constructor Crate (std::vector> items, std::vector inner_attrs) - : inner_attrs (std::move (inner_attrs)), items (std::move (items)), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : inner_attrs (std::move (inner_attrs)), items (std::move (items)) {} // Copy constructor with vector clone Crate (Crate const &other) - : inner_attrs (other.inner_attrs), node_id (other.node_id) + : HasNodeId (other), inner_attrs (other.inner_attrs) { items.reserve (other.items.size ()); for (const auto &e : other.items) @@ -2070,8 +1977,9 @@ struct Crate // Overloaded assignment operator with vector clone Crate &operator= (Crate const &other) { + HasNodeId::operator= (other); + inner_attrs = other.inner_attrs; - node_id = other.node_id; items.reserve (other.items.size ()); for (const auto &e : other.items) @@ -2098,7 +2006,6 @@ struct Crate // TODO: is this the best way to do this? } - NodeId get_node_id () const { return node_id; } const std::vector &get_inner_attrs () const { return inner_attrs; } std::vector &get_inner_attrs () { return inner_attrs; } diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 3c36238c6ee6..58909c3db1b5 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -16,27 +16,22 @@ namespace AST { // Loop label expression AST node used with break and continue expressions // TODO: inline? -class LoopLabel /*: public Visitable*/ +class LoopLabel : public HasNodeId /*, public Visitable*/ { Lifetime label; // or type LIFETIME_OR_LABEL location_t locus; - NodeId node_id; - public: std::string as_string () const; LoopLabel (Lifetime loop_label, location_t locus = UNDEF_LOCATION) - : label (std::move (loop_label)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : label (std::move (loop_label)), locus (locus) {} // Returns whether the LoopLabel is in an error state. location_t get_locus () const { return locus; } Lifetime &get_lifetime () { return label; } - - NodeId get_node_id () const { return node_id; } }; // AST node for an expression with an accompanying block - abstract @@ -1194,7 +1189,7 @@ class GroupedExpr : public ExprWithoutBlock // Base array initialisation internal element representation thing (abstract) // aka ArrayElements -class ArrayElems +class ArrayElems : public HasNodeId { public: virtual ~ArrayElems () {} @@ -1209,15 +1204,11 @@ class ArrayElems virtual void accept_vis (ASTVisitor &vis) = 0; - NodeId get_node_id () const { return node_id; } - protected: - ArrayElems () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} + ArrayElems () {} // pure virtual clone implementation virtual ArrayElems *clone_array_elems_impl () const = 0; - - NodeId node_id; }; // Value array elements @@ -1893,7 +1884,7 @@ struct StructBase /* Base AST node for a single struct expression field (in struct instance * creation) - abstract */ -class StructExprField +class StructExprField : public HasNodeId { public: virtual ~StructExprField () {} @@ -1910,8 +1901,6 @@ class StructExprField virtual location_t get_locus () const = 0; - NodeId get_node_id () const { return node_id; } - const std::vector &get_outer_attrs () const { return outer_attrs; @@ -1923,16 +1912,13 @@ class StructExprField // pure virtual clone implementation virtual StructExprField *clone_struct_expr_field_impl () const = 0; - StructExprField () : node_id (Analysis::Mappings::get ().get_next_node_id ()) - {} + StructExprField () {} StructExprField (AST::AttrVec outer_attrs) - : outer_attrs (std::move (outer_attrs)), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : outer_attrs (std::move (outer_attrs)) {} AST::AttrVec outer_attrs; - NodeId node_id; }; // Identifier-only variant of StructExprField AST node @@ -2755,7 +2741,7 @@ class BlockExpr : public ExprWithBlock // Copy constructor with clone BlockExpr (BlockExpr const &other) - : ExprWithBlock (other), outer_attrs (other.outer_attrs), + : HasNodeId (other), ExprWithBlock (other), outer_attrs (other.outer_attrs), inner_attrs (other.inner_attrs), label (other.label), start_locus (other.start_locus), end_locus (other.end_locus), marked_for_strip (other.marked_for_strip) @@ -2906,9 +2892,8 @@ class AnonConst : public ExprWithBlock expr (tl::nullopt) {} - AnonConst (const AnonConst &other) + AnonConst (const AnonConst &other) : HasNodeId (other), ExprWithBlock (other) { - node_id = other.node_id; locus = other.locus; kind = other.kind; @@ -2918,7 +2903,7 @@ class AnonConst : public ExprWithBlock AnonConst operator= (const AnonConst &other) { - node_id = other.node_id; + ExprWithBlock::operator= (other); locus = other.locus; kind = other.kind; @@ -2946,8 +2931,6 @@ class AnonConst : public ExprWithBlock return expr.value (); } - NodeId get_node_id () const override { return node_id; } - /* FIXME: AnonConst are always "internal" and should not have outer attributes * - is that true? Or should we instead call * expr->get_outer_attrs()/expr->set_outer_attrs() */ @@ -2995,8 +2978,9 @@ class ConstBlock : public ExprWithBlock ConstBlock operator= (const ConstBlock &other) { + ExprWithBlock::operator= (other); + expr = other.expr; - node_id = other.node_id; outer_attrs = other.outer_attrs; locus = other.locus; @@ -3954,7 +3938,7 @@ class TryExpr : public ExprWithBlock // Copy constructor with clone TryExpr (TryExpr const &other) - : ExprWithBlock (other), outer_attrs (other.outer_attrs), + : HasNodeId (other), ExprWithBlock (other), outer_attrs (other.outer_attrs), block_expr (other.block_expr->clone_block_expr ()), locus (other.locus), marked_for_strip (other.marked_for_strip) {} @@ -4248,7 +4232,7 @@ class WhileLoopExpr : public BaseLoopExpr // Copy constructor with clone WhileLoopExpr (WhileLoopExpr const &other) - : BaseLoopExpr (other), condition (other.condition->clone_expr ()) + : HasNodeId (other), BaseLoopExpr (other), condition (other.condition->clone_expr ()) {} // Overloaded assignment operator to clone @@ -4321,7 +4305,7 @@ class WhileLetLoopExpr : public BaseLoopExpr // Copy constructor with clone WhileLetLoopExpr (WhileLetLoopExpr const &other) - : BaseLoopExpr (other), + : HasNodeId (other), BaseLoopExpr (other), /*match_arm_patterns(other.match_arm_patterns),*/ scrutinee ( other.scrutinee->clone_expr ()) { @@ -4970,31 +4954,29 @@ struct MatchArm /* A "match case" - a correlated match arm and resulting expression. Not * abstract. */ -struct MatchCase +struct MatchCase : public HasNodeId { private: MatchArm arm; std::unique_ptr expr; - NodeId node_id; /* TODO: does whether trailing comma exists need to be stored? currently * assuming it is only syntactical and has no effect on meaning. */ public: MatchCase (MatchArm arm, std::unique_ptr expr) - : arm (std::move (arm)), expr (std::move (expr)), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : arm (std::move (arm)), expr (std::move (expr)) {} MatchCase (const MatchCase &other) - : arm (other.arm), expr (other.expr->clone_expr ()), node_id (other.node_id) + : HasNodeId (other), arm (other.arm), expr (other.expr->clone_expr ()) {} MatchCase &operator= (const MatchCase &other) { + HasNodeId::operator= (other); arm = other.arm; expr = other.expr->clone_expr (); - node_id = other.node_id; return *this; } @@ -5025,8 +5007,6 @@ struct MatchCase rust_assert (!arm.is_error ()); return arm; } - - NodeId get_node_id () const { return node_id; } }; // Match expression AST node diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 3e3735c3ece6..b7681998877c 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -77,8 +77,7 @@ class TypeParam : public GenericParam = std::vector> (), std::unique_ptr type = nullptr, AST::AttrVec outer_attrs = {}, bool was_impl_trait = false) - : GenericParam (Analysis::Mappings::get ().get_next_node_id ()), - outer_attrs (std::move (outer_attrs)), + : outer_attrs (std::move (outer_attrs)), type_representation (std::move (type_representation)), type_param_bounds (std::move (type_param_bounds)), type (std::move (type)), locus (locus), was_impl_trait (was_impl_trait) @@ -86,7 +85,7 @@ class TypeParam : public GenericParam // Copy constructor uses clone TypeParam (TypeParam const &other) - : GenericParam (other.node_id), outer_attrs (other.outer_attrs), + : GenericParam (other), outer_attrs (other.outer_attrs), type_representation (other.type_representation), locus (other.locus), was_impl_trait (other.was_impl_trait) { @@ -102,10 +101,10 @@ class TypeParam : public GenericParam // Overloaded assignment operator to clone TypeParam &operator= (TypeParam const &other) { + GenericParam::operator= (other); type_representation = other.type_representation; outer_attrs = other.outer_attrs; locus = other.locus; - node_id = other.node_id; was_impl_trait = other.was_impl_trait; // guard to prevent null pointer dereference @@ -169,7 +168,7 @@ class TypeParam : public GenericParam /* "where" clause item base. Abstract - use LifetimeWhereClauseItem, * TypeBoundWhereClauseItem */ -class WhereClauseItem +class WhereClauseItem : public HasNodeId { public: virtual ~WhereClauseItem () {} @@ -184,8 +183,6 @@ class WhereClauseItem virtual void accept_vis (ASTVisitor &vis) = 0; - virtual NodeId get_node_id () const = 0; - protected: // Clone function implementation as pure virtual method virtual WhereClauseItem *clone_where_clause_item_impl () const = 0; @@ -197,23 +194,19 @@ class LifetimeWhereClauseItem : public WhereClauseItem Lifetime lifetime; std::vector lifetime_bounds; location_t locus; - NodeId node_id; public: LifetimeWhereClauseItem (Lifetime lifetime, std::vector lifetime_bounds, location_t locus) : lifetime (std::move (lifetime)), - lifetime_bounds (std::move (lifetime_bounds)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + lifetime_bounds (std::move (lifetime_bounds)), locus (locus) {} std::string as_string () const override; void accept_vis (ASTVisitor &vis) override; - NodeId get_node_id () const override final { return node_id; } - Lifetime &get_lifetime () { return lifetime; } std::vector &get_lifetime_bounds () { return lifetime_bounds; } @@ -234,7 +227,6 @@ class TypeBoundWhereClauseItem : public WhereClauseItem std::vector for_lifetimes; std::unique_ptr bound_type; std::vector> type_param_bounds; - NodeId node_id; location_t locus; public: @@ -253,15 +245,14 @@ class TypeBoundWhereClauseItem : public WhereClauseItem : for_lifetimes (std::move (for_lifetimes)), bound_type (std::move (bound_type)), type_param_bounds (std::move (type_param_bounds)), - node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus) + locus (locus) {} // Copy constructor requires clone TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other) - : for_lifetimes (other.for_lifetimes), + : WhereClauseItem (other), for_lifetimes (other.for_lifetimes), bound_type (other.bound_type->clone_type ()) { - 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 ()); @@ -270,7 +261,7 @@ class TypeBoundWhereClauseItem : public WhereClauseItem // Overload assignment operator to clone TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other) { - node_id = other.node_id; + WhereClauseItem::operator= (other); for_lifetimes = other.for_lifetimes; bound_type = other.bound_type->clone_type (); type_param_bounds.reserve (other.type_param_bounds.size ()); @@ -313,8 +304,6 @@ class TypeBoundWhereClauseItem : public WhereClauseItem return type_param_bounds; } - NodeId get_node_id () const override final { return node_id; } - location_t get_locus () const { return locus; } protected: @@ -326,21 +315,18 @@ class TypeBoundWhereClauseItem : public WhereClauseItem }; // A where clause -class WhereClause +class WhereClause : public HasNodeId { std::vector> where_clause_items; - NodeId node_id; public: WhereClause (std::vector> where_clause_items) - : where_clause_items (std::move (where_clause_items)), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : where_clause_items (std::move (where_clause_items)) {} // copy constructor with vector clone - WhereClause (WhereClause const &other) + WhereClause (WhereClause const &other) : HasNodeId (other) { - node_id = other.node_id; where_clause_items.reserve (other.where_clause_items.size ()); for (const auto &e : other.where_clause_items) where_clause_items.push_back (e->clone_where_clause_item ()); @@ -349,7 +335,7 @@ class WhereClause // overloaded assignment operator with vector clone WhereClause &operator= (WhereClause const &other) { - node_id = other.node_id; + HasNodeId::operator= (other); where_clause_items.reserve (other.where_clause_items.size ()); for (const auto &e : other.where_clause_items) where_clause_items.push_back (e->clone_where_clause_item ()); @@ -372,8 +358,6 @@ class WhereClause std::string as_string () const; - NodeId get_node_id () const { return node_id; } - // TODO: this mutable getter seems kinda dodgy std::vector> &get_items () { @@ -386,12 +370,11 @@ class WhereClause }; // Abstract class Param -class Param : public Visitable +class Param : public Visitable, public HasNodeId { public: Param (std::vector outer_attrs, location_t locus) - : outer_attrs (std::move (outer_attrs)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : outer_attrs (std::move (outer_attrs)), locus (locus) {} virtual ~Param () = default; @@ -405,8 +388,6 @@ class Param : public Visitable virtual bool is_self () const { return false; } - NodeId get_node_id () const { return node_id; } - location_t get_locus () const { return locus; } std::vector get_outer_attrs () const { return outer_attrs; } @@ -420,7 +401,6 @@ class Param : public Visitable protected: std::vector outer_attrs; location_t locus; - NodeId node_id; }; // A self parameter in a method @@ -479,10 +459,9 @@ class SelfParam : public Param // Copy constructor requires clone SelfParam (SelfParam const &other) - : Param (other.get_outer_attrs (), other.get_locus ()), + : Param (other), has_ref (other.has_ref), is_mut (other.is_mut), lifetime (other.lifetime) { - node_id = other.node_id; if (other.type != nullptr) type = other.type->clone_type (); } @@ -490,12 +469,10 @@ class SelfParam : public Param // Overload assignment operator to use clone SelfParam &operator= (SelfParam const &other) { + Param::operator= (other); is_mut = other.is_mut; has_ref = other.has_ref; lifetime = other.lifetime; - locus = other.locus; - node_id = other.node_id; - outer_attrs = other.outer_attrs; if (other.type != nullptr) type = other.type->clone_type (); @@ -521,8 +498,6 @@ class SelfParam : public Param Lifetime get_lifetime () const { return lifetime.value (); } Lifetime &get_lifetime () { return lifetime.value (); } - NodeId get_node_id () const { return node_id; } - // TODO: is this better? Or is a "vis_block" better? Type &get_type () { @@ -599,7 +574,7 @@ class VariadicParam : public Param {} VariadicParam (VariadicParam const &other) - : Param (other.get_outer_attrs (), other.locus) + : Param (other) { if (other.param_name != nullptr) param_name = other.param_name->clone_pattern (); @@ -607,9 +582,7 @@ class VariadicParam : public Param VariadicParam &operator= (VariadicParam const &other) { - outer_attrs = other.outer_attrs; - locus = other.locus; - node_id = other.node_id; + Param::operator= (other); if (other.param_name != nullptr) param_name = other.param_name->clone_pattern (); else @@ -666,7 +639,7 @@ class FunctionParam : public Param // Copy constructor uses clone FunctionParam (FunctionParam const &other) - : Param (other.get_outer_attrs (), other.locus) + : Param (other) { // guard to prevent nullptr dereference if (other.param_name != nullptr) @@ -678,8 +651,7 @@ class FunctionParam : public Param // Overload assignment operator to use clone FunctionParam &operator= (FunctionParam const &other) { - locus = other.locus; - node_id = other.node_id; + Param::operator= (other); // guard to prevent nullptr dereference if (other.param_name != nullptr) @@ -980,10 +952,9 @@ class ExternCrate : public VisItem }; // The path-ish thing referred to in a use declaration - abstract base class -class UseTree +class UseTree : public HasNodeId { location_t locus; - NodeId node_id; public: enum Kind @@ -995,14 +966,7 @@ class UseTree virtual ~UseTree () {} - // Overload assignment operator to clone - UseTree &operator= (UseTree const &other) - { - locus = other.locus; - - return *this; - } - + UseTree &operator= (UseTree const &other) = default; UseTree (const UseTree &other) = default; // move constructors @@ -1019,7 +983,6 @@ class UseTree virtual Kind get_kind () const = 0; location_t get_locus () const { return locus; } - NodeId get_node_id () const { return node_id; } virtual void accept_vis (ASTVisitor &vis) = 0; @@ -1028,7 +991,7 @@ class UseTree virtual UseTree *clone_use_tree_impl () const = 0; UseTree (location_t locus) - : locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ()) + : locus (locus) {} }; @@ -1375,7 +1338,7 @@ class Function : public VisItem, public AssociatedItem, public ExternalItem location_t locus, bool has_default = false, bool is_external_function = false) : VisItem (std::move (vis), std::move (outer_attrs)), - ExternalItem (Stmt::node_id), qualifiers (std::move (qualifiers)), + qualifiers (std::move (qualifiers)), function_name (std::move (function_name)), generic_params (std::move (generic_params)), function_params (std::move (function_params)), @@ -1473,9 +1436,6 @@ class Function : public VisItem, public AssociatedItem, public ExternalItem return *function_params[0]; } - // ExternalItem::node_id is same as Stmt::node_id - NodeId get_node_id () const override { return Stmt::node_id; } - Item::Kind get_item_kind () const override { return Item::Kind::Function; } protected: @@ -1579,9 +1539,6 @@ class TypeAlias : public VisItem, public AssociatedItem location_t get_locus () const override final { return locus; } - // needed to override AssociatedItem::get_node_id - NodeId get_node_id () const override final { return VisItem::get_node_id (); } - void accept_vis (ASTVisitor &vis) override; // Invalid if existing type is null, so base stripping on that. @@ -1723,7 +1680,7 @@ class Struct : public VisItem }; // A single field in a struct -class StructField +class StructField : public HasNodeId { // bool has_outer_attributes; std::vector outer_attrs; @@ -1734,8 +1691,6 @@ class StructField Identifier field_name; std::unique_ptr field_type; - NodeId node_id; - location_t locus; public: @@ -1750,13 +1705,13 @@ class StructField std::vector outer_attrs = std::vector ()) : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)), field_name (std::move (field_name)), field_type (std::move (field_type)), - node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus) + locus (locus) {} // Copy constructor StructField (StructField const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - field_name (other.field_name), node_id (other.node_id), + : HasNodeId (other), outer_attrs (other.outer_attrs), visibility (other.visibility), + field_name (other.field_name), locus (other.locus) { // guard to prevent null dereference @@ -1769,10 +1724,10 @@ class StructField // Overloaded assignment operator to clone StructField &operator= (StructField const &other) { + HasNodeId::operator= (other); field_name = other.field_name; visibility = other.visibility; outer_attrs = other.outer_attrs; - node_id = other.node_id; // guard to prevent null dereference if (other.field_type != nullptr) @@ -1826,8 +1781,6 @@ class StructField Visibility &get_visibility () { return visibility; } const Visibility &get_visibility () const { return visibility; } - - NodeId get_node_id () const { return node_id; } }; // Rust struct declaration with true struct type AST node @@ -1882,7 +1835,7 @@ class StructStruct : public Struct }; // A single field in a tuple -class TupleField +class TupleField : public HasNodeId { // bool has_outer_attributes; std::vector outer_attrs; @@ -1892,8 +1845,6 @@ class TupleField std::unique_ptr field_type; - NodeId node_id; - location_t locus; public: @@ -1910,13 +1861,13 @@ class TupleField std::vector outer_attrs = std::vector ()) : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)), field_type (std::move (field_type)), - node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus) + locus (locus) {} // Copy constructor with clone TupleField (TupleField const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - node_id (other.node_id), locus (other.locus) + : HasNodeId (other), outer_attrs (other.outer_attrs), visibility (other.visibility), + locus (other.locus) { // guard to prevent null dereference (only required if error) if (other.field_type != nullptr) @@ -1928,9 +1879,9 @@ class TupleField // Overloaded assignment operator to clone TupleField &operator= (TupleField const &other) { + HasNodeId::operator= (other); visibility = other.visibility; outer_attrs = other.outer_attrs; - node_id = other.node_id; locus = other.locus; // guard to prevent null dereference (only required if error) @@ -1957,8 +1908,6 @@ class TupleField std::string as_string () const; - NodeId get_node_id () const { return node_id; } - Visibility &get_visibility () { return visibility; } const Visibility &get_visibility () const { return visibility; } @@ -2536,9 +2485,6 @@ class ConstantItem : public VisItem, public AssociatedItem location_t get_locus () const override final { return locus; } - // needed to override AssociatedItem::get_node_id - NodeId get_node_id () const override final { return VisItem::get_node_id (); } - void accept_vis (ASTVisitor &vis) override; // Invalid if type and expression are null, so base stripping on that. @@ -2752,10 +2698,9 @@ class TraitItemType : public TraitItem // Copy constructor with vector clone TraitItemType (TraitItemType const &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), + : TraitItem (other), outer_attrs (other.outer_attrs), name (other.name) { - node_id = other.node_id; generic_params.reserve (other.generic_params.size ()); for (const auto &e : other.generic_params) generic_params.push_back (e->clone_generic_param ()); @@ -2771,7 +2716,6 @@ class TraitItemType : public TraitItem outer_attrs = other.outer_attrs; name = other.name; locus = other.locus; - node_id = other.node_id; generic_params.reserve (other.generic_params.size ()); for (const auto &e : other.generic_params) @@ -3358,16 +3302,14 @@ class ExternalTypeItem : public ExternalItem // copy constructor ExternalTypeItem (ExternalTypeItem const &other) - : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs), + : 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) - { - node_id = other.node_id; - } + {} 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; @@ -3440,11 +3382,10 @@ class ExternalStaticItem : public ExternalItem // Copy constructor ExternalStaticItem (ExternalStaticItem const &other) - : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs), + : 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 (); @@ -3453,7 +3394,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; diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 71de8f022d22..ea77c3cb68ae 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -672,11 +672,6 @@ class MacroInvocation : public TypeNoBounds, outer_attrs = std::move (new_attrs); } - NodeId get_node_id () const override final - { - return ExprWithoutBlock::get_node_id (); - } - NodeId get_macro_node_id () const { return node_id; } MacroInvocData &get_invoc_data () { return invoc_data; } @@ -722,7 +717,8 @@ class MacroInvocation : public TypeNoBounds, {} MacroInvocation (const MacroInvocation &other) - : TraitItem (other.locus), ExternalItem (other.node_id), + : HasNodeId (other), TypeNoBounds (other), Pattern (other), Item (other), + TraitItem (other), ExternalItem (other), ExprWithoutBlock (other), 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), @@ -787,20 +783,6 @@ class MacroInvocation : public TypeNoBounds, return new MacroInvocation (*this); } - std::unique_ptr reconstruct_macro_invocation () const - { - return nullptr; - // return reconstruct (this, - // &MacroInvocation::reconstruct_macro_invocation_impl); - } - - MacroInvocation *reconstruct_impl () const override - { - return new MacroInvocation (kind, builtin_kind, invoc_data, outer_attrs, - locus, is_semi_coloned, - reconstruct_vec (pending_eager_invocs)); - } - void add_semicolon () override { is_semi_coloned = true; } Pattern::Kind get_pattern_kind () override diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h index be04882ed6ad..ef5a89271e33 100644 --- a/gcc/rust/ast/rust-path.h +++ b/gcc/rust/ast/rust-path.h @@ -505,13 +505,12 @@ struct GenericArgs /* A segment of a path in expression, including an identifier aspect and maybe * generic args */ -class PathExprSegment +class PathExprSegment : public HasNodeId { // 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 @@ -521,8 +520,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 @@ -535,7 +533,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. @@ -561,8 +559,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_path_seg (); @@ -657,7 +653,6 @@ class PathInExpression : public Path, public ExprWithoutBlock std::vector outer_attrs; bool has_opening_scope_resolution; location_t locus; - NodeId _node_id; bool marked_for_strip; @@ -670,7 +665,7 @@ class PathInExpression : public Path, public ExprWithoutBlock bool has_opening_scope_resolution = false) : Path (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 ()), + locus (locus), marked_for_strip (false) {} @@ -678,7 +673,6 @@ class PathInExpression : public Path, public ExprWithoutBlock std::vector outer_attrs, location_t locus) : Path (lang_item), outer_attrs (std::move (outer_attrs)), has_opening_scope_resolution (false), locus (locus), - _node_id (Analysis::Mappings::get ().get_next_node_id ()), marked_for_strip (false) {} @@ -717,8 +711,6 @@ class PathInExpression : public Path, public ExprWithoutBlock 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; } @@ -761,7 +753,7 @@ class PathInExpression : public Path, public ExprWithoutBlock /* Base class for segments used in type paths - not abstract (represents an * ident-only segment) */ -class TypePathSegment +class TypePathSegment: public HasNodeId { public: enum SegmentType @@ -780,7 +772,6 @@ class TypePathSegment /* This is protected because it is only really used by derived classes, not * the base. */ bool has_separating_scope_resolution; - NodeId node_id; public: // Clone function implementation - not pure virtual as overrided by @@ -789,11 +780,6 @@ class TypePathSegment { return new TypePathSegment (*this); } - virtual TypePathSegment *reconstruct_impl () const - { - return new TypePathSegment (lang_item, ident_segment, - has_separating_scope_resolution, locus); - } public: virtual ~TypePathSegment () {} @@ -805,24 +791,17 @@ class TypePathSegment { return std::unique_ptr (clone_type_path_segment_impl ()); } - // Unique pointer custom reconstruct function - std::unique_ptr reconstruct () const - { - return reconstruct_base (this); - } TypePathSegment (PathIdentSegment ident_segment, bool has_separating_scope_resolution, location_t locus) : lang_item (tl::nullopt), ident_segment (std::move (ident_segment)), locus (locus), - has_separating_scope_resolution (has_separating_scope_resolution), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + has_separating_scope_resolution (has_separating_scope_resolution) {} TypePathSegment (LangItem::Kind lang_item, location_t locus) : lang_item (lang_item), ident_segment (tl::nullopt), locus (locus), - has_separating_scope_resolution (false), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + has_separating_scope_resolution (false) {} TypePathSegment (std::string segment_name, @@ -830,8 +809,7 @@ class TypePathSegment : lang_item (tl::nullopt), ident_segment (PathIdentSegment (std::move (segment_name), locus)), locus (locus), - has_separating_scope_resolution (has_separating_scope_resolution), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + has_separating_scope_resolution (has_separating_scope_resolution) {} // General constructor @@ -839,24 +817,22 @@ class TypePathSegment tl::optional ident_segment, bool has_separating_scope_resolution, location_t locus) : lang_item (lang_item), ident_segment (ident_segment), locus (locus), - has_separating_scope_resolution (has_separating_scope_resolution), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + has_separating_scope_resolution (has_separating_scope_resolution) {} TypePathSegment (TypePathSegment const &other) - : lang_item (other.lang_item), ident_segment (other.ident_segment), + : HasNodeId (other), lang_item (other.lang_item), ident_segment (other.ident_segment), locus (other.locus), - has_separating_scope_resolution (other.has_separating_scope_resolution), - node_id (other.node_id) + has_separating_scope_resolution (other.has_separating_scope_resolution) {} TypePathSegment &operator= (TypePathSegment const &other) { + HasNodeId::operator= (other); ident_segment = other.ident_segment; lang_item = other.lang_item; locus = other.locus; has_separating_scope_resolution = other.has_separating_scope_resolution; - node_id = other.node_id; return *this; } @@ -914,8 +890,6 @@ class TypePathSegment return *lang_item; } - NodeId get_node_id () const { return node_id; } - bool is_crate_path_seg () const { return get_ident_segment ().is_crate_path_seg (); @@ -983,6 +957,7 @@ class TypePathSegmentGeneric : public TypePathSegment // Overloaded assignment operator with vector clone TypePathSegmentGeneric &operator= (TypePathSegmentGeneric const &other) { + TypePathSegment::operator= (other); generic_args = other.generic_args; return *this; @@ -1174,11 +1149,6 @@ class TypePath : public TypeNoBounds { return new TypePath (*this); } - TypePath *reconstruct_impl () const override - { - return new TypePath (reconstruct_vec (segments), locus, - has_opening_scope_resolution); - } public: /* Returns whether the TypePath has an opening scope resolution operator @@ -1216,10 +1186,9 @@ class TypePath : public TypeNoBounds // Copy constructor with vector clone TypePath (TypePath const &other) - : has_opening_scope_resolution (other.has_opening_scope_resolution), + : HasNodeId (other), 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 ()); @@ -1228,7 +1197,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; @@ -1255,7 +1224,6 @@ class TypePath : public TypeNoBounds TraitBound *to_trait_bound (bool in_parens) const override; location_t get_locus () const override final { return locus; } - NodeId get_node_id () const override { return node_id; } void mark_for_strip () override {} bool is_marked_for_strip () const override { return false; } @@ -1275,13 +1243,12 @@ class TypePath : public TypeNoBounds size_t get_num_segments () const { return segments.size (); } }; -struct QualifiedPathType +struct QualifiedPathType : public HasNodeId { private: std::unique_ptr type_to_invoke_on; TypePath trait_path; location_t locus; - NodeId node_id; public: // Constructor @@ -1289,14 +1256,13 @@ struct QualifiedPathType location_t locus = UNDEF_LOCATION, TypePath trait_path = TypePath::create_error ()) : type_to_invoke_on (std::move (invoke_on_type)), trait_path (trait_path), - locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ()) + locus (locus) {} // Copy constructor uses custom deep copy for Type to preserve polymorphism QualifiedPathType (QualifiedPathType const &other) - : trait_path (other.trait_path), locus (other.locus) + : HasNodeId (other), trait_path (other.trait_path), locus (other.locus) { - node_id = other.node_id; // guard to prevent null dereference if (other.type_to_invoke_on != nullptr) type_to_invoke_on = other.type_to_invoke_on->clone_type (); @@ -1308,7 +1274,7 @@ struct QualifiedPathType // overload assignment operator to use custom clone method QualifiedPathType &operator= (QualifiedPathType const &other) { - node_id = other.node_id; + HasNodeId::operator= (other); trait_path = other.trait_path; locus = other.locus; @@ -1360,8 +1326,6 @@ struct QualifiedPathType rust_assert (has_as_clause ()); return trait_path; } - - NodeId get_node_id () const { return node_id; } }; /* AST node representing a qualified path-in-expression pattern (path that @@ -1371,7 +1335,6 @@ class QualifiedPathInExpression : public Path, public ExprWithoutBlock std::vector outer_attrs; QualifiedPathType path_type; location_t locus; - NodeId _node_id; public: std::string as_string () const override; @@ -1381,8 +1344,7 @@ class QualifiedPathInExpression : public Path, public ExprWithoutBlock std::vector outer_attrs, location_t locus) : Path (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 @@ -1424,8 +1386,6 @@ class QualifiedPathInExpression : public Path, public ExprWithoutBlock outer_attrs = std::move (new_attrs); } - NodeId get_node_id () const override { return _node_id; } - Expr::Kind get_expr_kind () const override { return Expr::Kind::QualifiedPathInExpression; @@ -1470,12 +1430,6 @@ class QualifiedPathInType : public TypeNoBounds { return new QualifiedPathInType (*this); } - QualifiedPathInType *reconstruct_impl () const override - { - return new QualifiedPathInType (path_type, - associated_segment->reconstruct (), - reconstruct_vec (segments), locus); - } public: QualifiedPathInType ( diff --git a/gcc/rust/ast/rust-pattern.h b/gcc/rust/ast/rust-pattern.h index 3b1bd1c29ecf..6ca3d21e81e7 100644 --- a/gcc/rust/ast/rust-pattern.h +++ b/gcc/rust/ast/rust-pattern.h @@ -29,7 +29,6 @@ class LiteralPattern : public Pattern { Literal lit; location_t locus; - NodeId node_id; bool has_minus; public: @@ -38,27 +37,23 @@ class LiteralPattern : public Pattern // Constructor for a literal pattern LiteralPattern (Literal lit, location_t locus) : lit (std::move (lit)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()), has_minus (false) {} LiteralPattern (Literal lit, location_t locus, bool has_minus) : lit (std::move (lit)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()), has_minus (has_minus) {} LiteralPattern (std::string val, Literal::LitType type, location_t locus, PrimitiveCoreType type_hint) : lit (Literal (std::move (val), type, type_hint)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()), has_minus (false) {} LiteralPattern (std::string val, Literal::LitType type, location_t locus, PrimitiveCoreType type_hint, bool has_minus) : lit (Literal (std::move (val), type, type_hint)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()), has_minus (has_minus) {} @@ -68,8 +63,6 @@ class LiteralPattern : public Pattern void accept_vis (ASTVisitor &vis) override; - NodeId get_node_id () const override { return node_id; } - Literal &get_literal () { return lit; } const Literal &get_literal () const { return lit; } @@ -95,7 +88,6 @@ class IdentifierPattern : public Pattern // bool has_pattern; std::unique_ptr subpattern; location_t locus; - NodeId node_id; public: std::string as_string () const override; @@ -108,22 +100,13 @@ class IdentifierPattern : public Pattern bool is_mut = false, std::unique_ptr subpattern = nullptr) : Pattern (), variable_ident (std::move (ident)), is_ref (is_ref), - is_mut (is_mut), subpattern (std::move (subpattern)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) - {} - - IdentifierPattern (NodeId node_id, Identifier ident, location_t locus, - bool is_ref = false, bool is_mut = false, - std::unique_ptr subpattern = nullptr) - : Pattern (), variable_ident (std::move (ident)), is_ref (is_ref), - is_mut (is_mut), subpattern (std::move (subpattern)), locus (locus), - node_id (node_id) + is_mut (is_mut), subpattern (std::move (subpattern)), locus (locus) {} // Copy constructor with clone IdentifierPattern (IdentifierPattern const &other) - : variable_ident (other.variable_ident), is_ref (other.is_ref), - is_mut (other.is_mut), locus (other.locus), node_id (other.node_id) + : HasNodeId (other), Pattern (other), variable_ident (other.variable_ident), is_ref (other.is_ref), + is_mut (other.is_mut), locus (other.locus) { // fix to get prevent null pointer dereference if (other.subpattern != nullptr) @@ -133,11 +116,11 @@ class IdentifierPattern : public Pattern // Overload assignment operator to use clone IdentifierPattern &operator= (IdentifierPattern const &other) { + Pattern::operator= (other); variable_ident = other.variable_ident; is_ref = other.is_ref; is_mut = other.is_mut; locus = other.locus; - node_id = other.node_id; // fix to prevent null pointer dereference if (other.subpattern != nullptr) @@ -173,8 +156,6 @@ class IdentifierPattern : public Pattern bool get_is_mut () const { return is_mut; } bool get_is_ref () const { return is_ref; } - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Identifier; @@ -193,21 +174,18 @@ class IdentifierPattern : public Pattern class WildcardPattern : public Pattern { location_t locus; - NodeId node_id; public: std::string as_string () const override { return std::string (1, '_'); } WildcardPattern (location_t locus) - : locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ()) + : locus (locus) {} location_t get_locus () const override final { return locus; } void accept_vis (ASTVisitor &vis) override; - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Wildcard; } protected: @@ -222,21 +200,18 @@ class WildcardPattern : public Pattern class RestPattern : public Pattern { location_t locus; - NodeId node_id; public: std::string as_string () const override { return ".."; } RestPattern (location_t locus) - : locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ()) + : locus (locus) {} location_t get_locus () const override final { return locus; } void accept_vis (ASTVisitor &vis) override; - NodeId get_node_id () const override final { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Rest; } protected: @@ -411,7 +386,6 @@ class RangePattern : public Pattern /* location only stored to avoid a dereference - lower pattern should give * correct location so maybe change in future */ location_t locus; - NodeId node_id; public: std::string as_string () const override; @@ -421,26 +395,24 @@ class RangePattern : public Pattern std::unique_ptr upper, RangeKind range_kind, location_t locus) : lower (std::move (lower)), upper (std::move (upper)), - range_kind (range_kind), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + range_kind (range_kind), locus (locus) {} // Copy constructor with clone RangePattern (RangePattern const &other) - : lower (other.lower->clone_range_pattern_bound ()), + : HasNodeId (other), Pattern (other), lower (other.lower->clone_range_pattern_bound ()), upper (other.upper->clone_range_pattern_bound ()), - range_kind (other.range_kind), locus (other.locus), - node_id (other.node_id) + range_kind (other.range_kind), locus (other.locus) {} // Overloaded assignment operator to clone RangePattern &operator= (RangePattern const &other) { + Pattern::operator= (other); lower = other.lower->clone_range_pattern_bound (); upper = other.upper->clone_range_pattern_bound (); range_kind = other.range_kind; locus = other.locus; - node_id = other.node_id; return *this; } @@ -477,8 +449,6 @@ class RangePattern : public Pattern return *upper; } - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Range; } protected: @@ -497,7 +467,6 @@ class ReferencePattern : public Pattern bool is_mut; std::unique_ptr pattern; location_t locus; - NodeId node_id; public: std::string as_string () const override; @@ -505,25 +474,23 @@ class ReferencePattern : public Pattern ReferencePattern (std::unique_ptr pattern, bool is_mut_reference, bool ref_has_two_amps, location_t locus) : has_two_amps (ref_has_two_amps), is_mut (is_mut_reference), - pattern (std::move (pattern)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + pattern (std::move (pattern)), locus (locus) {} // Copy constructor requires clone ReferencePattern (ReferencePattern const &other) - : has_two_amps (other.has_two_amps), is_mut (other.is_mut), - pattern (other.pattern->clone_pattern ()), locus (other.locus), - node_id (other.node_id) + : HasNodeId (other), Pattern (other), has_two_amps (other.has_two_amps), is_mut (other.is_mut), + pattern (other.pattern->clone_pattern ()), locus (other.locus) {} // Overload assignment operator to clone ReferencePattern &operator= (ReferencePattern const &other) { + Pattern::operator= (other); pattern = other.pattern->clone_pattern (); is_mut = other.is_mut; has_two_amps = other.has_two_amps; locus = other.locus; - node_id = other.node_id; return *this; } @@ -553,8 +520,6 @@ class ReferencePattern : public Pattern bool get_is_mut () const { return is_mut; } - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Reference; @@ -592,14 +557,11 @@ struct StructPatternEtc #endif // Base class for a single field in a struct pattern - abstract -class StructPatternField +class StructPatternField : public HasNodeId { std::vector outer_attrs; location_t locus; -protected: - NodeId node_id; - public: enum ItemType { @@ -627,16 +589,13 @@ class StructPatternField virtual bool is_marked_for_strip () const = 0; virtual ItemType get_item_type () const = 0; - NodeId get_node_id () const { return node_id; } - // TODO: seems kinda dodgy. Think of better way. std::vector &get_outer_attrs () { return outer_attrs; } const std::vector &get_outer_attrs () const { return outer_attrs; } protected: - StructPatternField (std::vector outer_attribs, location_t locus, - NodeId node_id) - : outer_attrs (std::move (outer_attribs)), locus (locus), node_id (node_id) + StructPatternField (std::vector outer_attribs, location_t locus) + : outer_attrs (std::move (outer_attribs)), locus (locus) {} // Clone function implementation as pure virtual method @@ -654,8 +613,7 @@ class StructPatternFieldTuplePat : public StructPatternField std::unique_ptr tuple_pattern, std::vector outer_attribs, location_t locus) - : StructPatternField (std::move (outer_attribs), locus, - Analysis::Mappings::get ().get_next_node_id ()), + : StructPatternField (std::move (outer_attribs), locus), index (index), tuple_pattern (std::move (tuple_pattern)) {} @@ -664,7 +622,6 @@ class StructPatternFieldTuplePat : public StructPatternField : StructPatternField (other), index (other.index) { // guard to prevent null dereference (only required if error state) - node_id = other.get_node_id (); if (other.tuple_pattern != nullptr) tuple_pattern = other.tuple_pattern->clone_pattern (); } @@ -676,7 +633,6 @@ class StructPatternFieldTuplePat : public StructPatternField StructPatternField::operator= (other); index = other.index; // outer_attrs = other.outer_attrs; - node_id = other.get_node_id (); // guard to prevent null dereference (only required if error state) if (other.tuple_pattern != nullptr) @@ -740,8 +696,7 @@ class StructPatternFieldIdentPat : public StructPatternField std::unique_ptr ident_pattern, std::vector outer_attrs, location_t locus) - : StructPatternField (std::move (outer_attrs), locus, - Analysis::Mappings::get ().get_next_node_id ()), + : StructPatternField (std::move (outer_attrs), locus), ident (std::move (ident)), ident_pattern (std::move (ident_pattern)) {} @@ -750,7 +705,6 @@ class StructPatternFieldIdentPat : public StructPatternField : StructPatternField (other), ident (other.ident) { // guard to prevent null dereference (only required if error state) - node_id = other.get_node_id (); if (other.ident_pattern != nullptr) ident_pattern = other.ident_pattern->clone_pattern (); } @@ -762,7 +716,6 @@ class StructPatternFieldIdentPat : public StructPatternField StructPatternField::operator= (other); ident = other.ident; // outer_attrs = other.outer_attrs; - node_id = other.get_node_id (); // guard to prevent null dereference (only required if error state) if (other.ident_pattern != nullptr) @@ -825,8 +778,7 @@ class StructPatternFieldIdent : public StructPatternField public: StructPatternFieldIdent (Identifier ident, bool is_ref, bool is_mut, std::vector outer_attrs, location_t locus) - : StructPatternField (std::move (outer_attrs), locus, - Analysis::Mappings::get ().get_next_node_id ()), + : StructPatternField (std::move (outer_attrs), locus), has_ref (is_ref), has_mut (is_mut), ident (std::move (ident)) {} @@ -970,7 +922,6 @@ class StructPattern : public Pattern // bool has_struct_pattern_elements; StructPatternElements elems; - NodeId node_id; location_t locus; public: @@ -981,7 +932,7 @@ class StructPattern : public Pattern StructPatternElements elems = StructPatternElements::create_empty ()) : path (std::move (struct_path)), elems (std::move (elems)), - node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus) + locus (locus) {} /* TODO: constructor to construct via elements included in @@ -1005,8 +956,6 @@ class StructPattern : public Pattern PathInExpression &get_path () { return path; } const PathInExpression &get_path () const { return path; } - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Struct; } protected: @@ -1203,7 +1152,6 @@ class TupleStructPattern : public Pattern { PathInExpression path; std::unique_ptr items; - NodeId node_id; /* TOOD: should this store location data? current accessor uses path location * data */ @@ -1213,27 +1161,25 @@ class TupleStructPattern : public Pattern TupleStructPattern (PathInExpression tuple_struct_path, std::unique_ptr items) - : path (std::move (tuple_struct_path)), items (std::move (items)), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : path (std::move (tuple_struct_path)), items (std::move (items)) { rust_assert (this->items != nullptr); } // Copy constructor required to clone - TupleStructPattern (TupleStructPattern const &other) : path (other.path) + TupleStructPattern (TupleStructPattern const &other) : Pattern (other), path (other.path) { // guard to protect from null dereference rust_assert (other.items != nullptr); - node_id = other.node_id; items = other.items->clone_tuple_struct_items (); } // Operator overload assignment operator to clone TupleStructPattern &operator= (TupleStructPattern const &other) { + Pattern::operator= (other); path = other.path; - node_id = other.node_id; // guard to protect from null dereference rust_assert (other.items != nullptr); @@ -1260,8 +1206,6 @@ class TupleStructPattern : public Pattern PathInExpression &get_path () { return path; } const PathInExpression &get_path () const { return path; } - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::TupleStruct; @@ -1434,33 +1378,30 @@ class TuplePattern : public Pattern { std::unique_ptr items; location_t locus; - NodeId node_id; public: std::string as_string () const override; TuplePattern (std::unique_ptr items, location_t locus) - : items (std::move (items)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : items (std::move (items)), locus (locus) { rust_assert (this->items != nullptr); } // Copy constructor requires clone - TuplePattern (TuplePattern const &other) : locus (other.locus) + TuplePattern (TuplePattern const &other) : HasNodeId (other), Pattern (other), locus (other.locus) { // guard to prevent null dereference rust_assert (other.items != nullptr); - node_id = other.node_id; items = other.items->clone_tuple_pattern_items (); } // Overload assignment operator to clone TuplePattern &operator= (TuplePattern const &other) { + Pattern::operator= (other); locus = other.locus; - node_id = other.node_id; // guard to prevent null dereference rust_assert (other.items != nullptr); @@ -1480,8 +1421,6 @@ class TuplePattern : public Pattern return *items; } - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Tuple; } protected: @@ -1498,7 +1437,6 @@ class GroupedPattern : public Pattern { std::unique_ptr pattern_in_parens; location_t locus; - NodeId node_id; public: std::string as_string () const override @@ -1507,22 +1445,21 @@ class GroupedPattern : public Pattern } GroupedPattern (std::unique_ptr pattern_in_parens, location_t locus) - : pattern_in_parens (std::move (pattern_in_parens)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : pattern_in_parens (std::move (pattern_in_parens)), locus (locus) {} // Copy constructor uses clone GroupedPattern (GroupedPattern const &other) - : pattern_in_parens (other.pattern_in_parens->clone_pattern ()), - locus (other.locus), node_id (other.node_id) + : Pattern (other), pattern_in_parens (other.pattern_in_parens->clone_pattern ()), + locus (other.locus) {} // Overload assignment operator to clone GroupedPattern &operator= (GroupedPattern const &other) { + Pattern::operator= (other); pattern_in_parens = other.pattern_in_parens->clone_pattern (); locus = other.locus; - node_id = other.node_id; return *this; } @@ -1548,8 +1485,6 @@ class GroupedPattern : public Pattern return pattern_in_parens; } - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Grouped; } protected: @@ -1719,31 +1654,28 @@ class SlicePattern : public Pattern { std::unique_ptr items; location_t locus; - NodeId node_id; public: std::string as_string () const override; SlicePattern (std::unique_ptr items, location_t locus) - : items (std::move (items)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : items (std::move (items)), locus (locus) {} // Copy constructor requires clone - SlicePattern (SlicePattern const &other) : locus (other.locus) + SlicePattern (SlicePattern const &other) : Pattern (other), locus (other.locus) { // guard to prevent null dereference rust_assert (other.items != nullptr); - node_id = other.node_id; items = other.items->clone_slice_pattern_items (); } // Overloaded assignment operator to clone SlicePattern &operator= (SlicePattern const &other) { + Pattern::operator= (other); locus = other.locus; - node_id = other.node_id; // guard to prevent null dereference rust_assert (other.items != nullptr); @@ -1767,8 +1699,6 @@ class SlicePattern : public Pattern return *items; } - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Slice; } protected: @@ -1786,20 +1716,17 @@ class AltPattern : public Pattern { std::vector> alts; location_t locus; - NodeId node_id; public: std::string as_string () const override; AltPattern (std::vector> alts, location_t locus) - : alts (std::move (alts)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : alts (std::move (alts)), locus (locus) {} // Copy constructor with vector clone - AltPattern (AltPattern const &other) : locus (other.locus) + AltPattern (AltPattern const &other) : Pattern (other), locus (other.locus) { - node_id = other.node_id; alts.reserve (other.alts.size ()); for (const auto &e : other.alts) alts.push_back (e->clone_pattern ()); @@ -1808,8 +1735,8 @@ class AltPattern : public Pattern // Overloaded assignment operator to vector clone AltPattern &operator= (AltPattern const &other) { + Pattern::operator= (other); locus = other.locus; - node_id = other.node_id; alts.clear (); alts.reserve (other.alts.size ()); @@ -1834,8 +1761,6 @@ class AltPattern : public Pattern return alts; } - NodeId get_node_id () const override { return node_id; } - Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Alt; } protected: diff --git a/gcc/rust/ast/rust-type.h b/gcc/rust/ast/rust-type.h index 38a34748130e..23693e540303 100644 --- a/gcc/rust/ast/rust-type.h +++ b/gcc/rust/ast/rust-type.h @@ -59,24 +59,14 @@ class TraitBound : public TypeParamBound bool opening_question_mark = false, std::vector for_lifetimes = std::vector ()) - : TypeParamBound (Analysis::Mappings::get ().get_next_node_id ()), + : TypeParamBound (), in_parens (in_parens), opening_question_mark (opening_question_mark), for_lifetimes (std::move (for_lifetimes)), type_path (std::move (type_path)), locus (locus) {} - TraitBound (NodeId id, TypePath type_path, location_t locus, - bool in_parens = false, bool opening_question_mark = false, - std::vector for_lifetimes - = std::vector ()) - : TypeParamBound (id), in_parens (in_parens), - opening_question_mark (opening_question_mark), - for_lifetimes (std::move (for_lifetimes)), - type_path (std::move (type_path)), locus (locus) - {} - TraitBound (TraitBound const &other) - : TypeParamBound (other.get_node_id ()), in_parens (other.in_parens), + : TypeParamBound (other), in_parens (other.in_parens), opening_question_mark (other.opening_question_mark), for_lifetimes (other.for_lifetimes), type_path (other.type_path), locus (other.locus) @@ -105,13 +95,7 @@ class TraitBound : public TypeParamBound * than base */ TraitBound *clone_type_param_bound_impl () const override { - return new TraitBound (node_id, type_path, locus, in_parens, - opening_question_mark, for_lifetimes); - } - TraitBound *reconstruct_impl () const override - { - return new TraitBound (type_path, locus, in_parens, opening_question_mark, - for_lifetimes); + return new TraitBound (*this); } }; @@ -134,10 +118,6 @@ class ImplTraitType : public Type { return new ImplTraitType (*this); } - ImplTraitType *reconstruct_impl () const override - { - return new ImplTraitType (reconstruct_vec (type_param_bounds), locus); - } public: ImplTraitType ( @@ -148,7 +128,7 @@ class ImplTraitType : public Type // copy constructor with vector clone ImplTraitType (ImplTraitType const &other) - : Type (other.node_id), locus (other.locus) + : HasNodeId (other), Type (other), locus (other.locus) { type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) @@ -158,6 +138,7 @@ class ImplTraitType : public Type // overloaded assignment operator to clone ImplTraitType &operator= (ImplTraitType const &other) { + Type::operator= (other); locus = other.locus; type_param_bounds.reserve (other.type_param_bounds.size ()); @@ -202,11 +183,6 @@ class TraitObjectType : public Type { return new TraitObjectType (*this); } - TraitObjectType *reconstruct_impl () const override - { - return new TraitObjectType (reconstruct_vec (type_param_bounds), locus, - has_dyn); - } public: TraitObjectType ( @@ -218,7 +194,7 @@ class TraitObjectType : public Type // copy constructor with vector clone TraitObjectType (TraitObjectType const &other) - : Type (other.node_id), has_dyn (other.has_dyn), locus (other.locus) + : HasNodeId (other), Type (other), has_dyn (other.has_dyn), locus (other.locus) { type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) @@ -228,6 +204,7 @@ class TraitObjectType : public Type // overloaded assignment operator to clone TraitObjectType &operator= (TraitObjectType const &other) { + Type::operator= (other); has_dyn = other.has_dyn; locus = other.locus; type_param_bounds.reserve (other.type_param_bounds.size ()); @@ -273,10 +250,6 @@ class ParenthesisedType : public TypeNoBounds { return new ParenthesisedType (*this); } - ParenthesisedType *reconstruct_impl () const override - { - return new ParenthesisedType (type_in_parens->reconstruct (), locus); - } public: // Constructor uses Type pointer for polymorphism @@ -287,12 +260,13 @@ class ParenthesisedType : public TypeNoBounds /* Copy constructor uses custom deep copy method for type to preserve * polymorphism */ ParenthesisedType (ParenthesisedType const &other) - : type_in_parens (other.type_in_parens->clone_type ()), locus (other.locus) + : HasNodeId (other), TypeNoBounds (other), type_in_parens (other.type_in_parens->clone_type ()), locus (other.locus) {} // overload assignment operator to use custom clone method ParenthesisedType &operator= (ParenthesisedType const &other) { + TypeNoBounds::operator= (other); type_in_parens = other.type_in_parens->clone_type (); locus = other.locus; return *this; @@ -341,7 +315,7 @@ class ImplTraitTypeOneBound : public TypeNoBounds {} ImplTraitTypeOneBound (ImplTraitTypeOneBound const &other) - : trait_bound (other.trait_bound->clone_type_param_bound ()), + : HasNodeId (other), TypeNoBounds (other), trait_bound (other.trait_bound->clone_type_param_bound ()), locus (other.locus) {} @@ -357,10 +331,6 @@ class ImplTraitTypeOneBound : public TypeNoBounds { return new ImplTraitTypeOneBound (*this); } - TypeNoBounds *reconstruct_impl () const override - { - return new ImplTraitTypeOneBound (trait_bound->reconstruct (), locus); - } }; /* A trait object with a single trait bound. The "trait bound" is really just @@ -378,10 +348,6 @@ class TraitObjectTypeOneBound : public TypeNoBounds { return new TraitObjectTypeOneBound (*this); } - TraitObjectTypeOneBound *reconstruct_impl () const override - { - return new TraitObjectTypeOneBound (trait_bound, locus, has_dyn); - } public: TraitObjectTypeOneBound (TraitBound trait_bound, location_t locus, @@ -432,7 +398,7 @@ class TupleType : public TypeNoBounds {} // copy constructor with vector clone - TupleType (TupleType const &other) : locus (other.locus) + TupleType (TupleType const &other) : HasNodeId (other), TypeNoBounds (other), locus (other.locus) { elems.reserve (other.elems.size ()); for (const auto &e : other.elems) @@ -442,6 +408,7 @@ class TupleType : public TypeNoBounds // overloaded assignment operator to clone TupleType &operator= (TupleType const &other) { + TypeNoBounds::operator= (other); locus = other.locus; elems.reserve (other.elems.size ()); @@ -474,10 +441,6 @@ class TupleType : public TypeNoBounds { return new TupleType (*this); } - TupleType *reconstruct_impl () const override - { - return new TupleType (reconstruct_vec (elems), locus); - } }; /* A type with no values, representing the result of computations that never @@ -494,10 +457,6 @@ class NeverType : public TypeNoBounds { return new NeverType (*this); } - NeverType *reconstruct_impl () const override - { - return new NeverType (locus); - } public: NeverType (location_t locus) : locus (locus) {} @@ -538,13 +497,14 @@ class RawPointerType : public TypeNoBounds // Copy constructor calls custom polymorphic clone function RawPointerType (RawPointerType const &other) - : pointer_type (other.pointer_type), + : HasNodeId (other), TypeNoBounds (other), pointer_type (other.pointer_type), type (other.type->clone_type_no_bounds ()), locus (other.locus) {} // overload assignment operator to use custom clone method RawPointerType &operator= (RawPointerType const &other) { + TypeNoBounds::operator= (other); pointer_type = other.pointer_type; type = other.type->clone_type_no_bounds (); locus = other.locus; @@ -584,10 +544,6 @@ class RawPointerType : public TypeNoBounds { return new RawPointerType (*this); } - RawPointerType *reconstruct_impl () const override - { - return new RawPointerType (pointer_type, type->reconstruct (), locus); - } }; // A type pointing to memory owned by another value @@ -617,13 +573,14 @@ class ReferenceType : public TypeNoBounds // Copy constructor with custom clone method ReferenceType (ReferenceType const &other) - : lifetime (other.lifetime), has_mut (other.has_mut), + : HasNodeId (other), TypeNoBounds (other), lifetime (other.lifetime), has_mut (other.has_mut), type (other.type->clone_type_no_bounds ()), locus (other.locus) {} // Operator overload assignment operator to custom clone the unique pointer ReferenceType &operator= (ReferenceType const &other) { + TypeNoBounds::operator= (other); lifetime = other.lifetime; has_mut = other.has_mut; type = other.type->clone_type_no_bounds (); @@ -672,16 +629,6 @@ class ReferenceType : public TypeNoBounds { return new ReferenceType (*this); } - ReferenceType *reconstruct_impl () const override - { - return new ReferenceType (has_mut, type->reconstruct (), locus, - // TODO: Improve this - it's ugly! - has_lifetime () ? tl::make_optional ( - lifetime->get_lifetime_type (), - lifetime->get_lifetime_name (), - lifetime->get_locus ()) - : tl::nullopt); - } }; // A fixed-size sequence of elements of a specified type @@ -699,13 +646,14 @@ class ArrayType : public TypeNoBounds // Copy constructor requires deep copies of both unique pointers ArrayType (ArrayType const &other) - : elem_type (other.elem_type->clone_type ()), size (other.size), + : HasNodeId (other), TypeNoBounds (other), elem_type (other.elem_type->clone_type ()), size (other.size), locus (other.locus) {} // Overload assignment operator to deep copy pointers ArrayType &operator= (ArrayType const &other) { + TypeNoBounds::operator= (other); elem_type = other.elem_type->clone_type (); size = other.size; locus = other.locus; @@ -752,12 +700,6 @@ class ArrayType : public TypeNoBounds { return new ArrayType (*this); } - ArrayType *reconstruct_impl () const override - { - return new ArrayType (elem_type->reconstruct (), - size /* FIXME: This should be `reconstruct_expr()` */, - locus); - } }; /* A dynamically-sized type representing a "view" into a sequence of elements of @@ -775,12 +717,13 @@ class SliceType : public TypeNoBounds // Copy constructor requires deep copy of Type smart pointer SliceType (SliceType const &other) - : elem_type (other.elem_type->clone_type ()), locus (other.locus) + : HasNodeId (other), TypeNoBounds (other), elem_type (other.elem_type->clone_type ()), locus (other.locus) {} // Overload assignment operator to deep copy SliceType &operator= (SliceType const &other) { + TypeNoBounds::operator= (other); elem_type = other.elem_type->clone_type (); locus = other.locus; @@ -814,10 +757,6 @@ class SliceType : public TypeNoBounds { return new SliceType (*this); } - SliceType *reconstruct_impl () const override - { - return new SliceType (elem_type->reconstruct (), locus); - } }; /* Type used in generic arguments to explicitly request type inference (wildcard @@ -836,13 +775,6 @@ class InferredType : public TypeNoBounds return new InferredType (*this); } - InferredType *reconstruct_impl () const override - { - // This goes through the base constructor which calls the base - // TypeNoBounds constructor, which allocates a new NodeId - return new InferredType (locus); - } - public: InferredType (location_t locus) : locus (locus) {} @@ -1006,7 +938,7 @@ class BareFunctionType : public TypeNoBounds // Copy constructor with clone BareFunctionType (BareFunctionType const &other) - : for_lifetimes (other.for_lifetimes), + : HasNodeId (other), TypeNoBounds (other), for_lifetimes (other.for_lifetimes), function_qualifiers (other.function_qualifiers), params (other.params), _is_variadic (other._is_variadic), variadic_attrs (other.variadic_attrs), locus (other.locus) @@ -1019,6 +951,7 @@ class BareFunctionType : public TypeNoBounds // Overload assignment operator to deep copy BareFunctionType &operator= (BareFunctionType const &other) { + TypeNoBounds::operator= (other); for_lifetimes = other.for_lifetimes; function_qualifiers = other.function_qualifiers; params = other.params; @@ -1067,18 +1000,6 @@ class BareFunctionType : public TypeNoBounds FunctionQualifiers &get_function_qualifiers () { return function_qualifiers; } - BareFunctionType *reconstruct_impl () const override - { - std::unique_ptr ret_type = nullptr; - if (return_type != nullptr) - ret_type = return_type->reconstruct (); - - return new BareFunctionType ( - for_lifetimes, function_qualifiers, params, - /* FIXME: Should params be reconstruct() as well? */ - _is_variadic, variadic_attrs, std::move (ret_type), locus); - } - protected: /* Use covariance to implement clone function as returning this object * rather than base */ diff --git a/gcc/rust/expand/rust-derive-default.cc b/gcc/rust/expand/rust-derive-default.cc index 26ee5461095a..307f5215098d 100644 --- a/gcc/rust/expand/rust-derive-default.cc +++ b/gcc/rust/expand/rust-derive-default.cc @@ -98,7 +98,7 @@ DeriveDefault::visit_struct (StructStruct &item) for (auto &field : item.get_fields ()) { auto name = field.get_field_name ().as_string (); - auto type = field.get_field_type ().reconstruct (); + auto type = field.get_field_type ().clone_type (); auto expr = default_call (std::move (type)); cloned_fields.emplace_back ( @@ -120,7 +120,7 @@ DeriveDefault::visit_tuple (TupleStruct &tuple_item) for (auto &field : tuple_item.get_fields ()) { - auto type = field.get_field_type ().reconstruct (); + auto type = field.get_field_type ().clone_type (); defaulted_fields.emplace_back (default_call (std::move (type))); } diff --git a/gcc/rust/expand/rust-derive-eq.cc b/gcc/rust/expand/rust-derive-eq.cc index 17332a47937e..9c7f762da62c 100644 --- a/gcc/rust/expand/rust-derive-eq.cc +++ b/gcc/rust/expand/rust-derive-eq.cc @@ -146,7 +146,7 @@ DeriveEq::visit_tuple (TupleStruct &item) auto types = std::vector> (); for (auto &field : item.get_fields ()) - types.emplace_back (field.get_field_type ().reconstruct ()); + types.emplace_back (field.get_field_type ().clone_type ()); expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)), item.get_identifier ().as_string (), @@ -159,7 +159,7 @@ DeriveEq::visit_struct (StructStruct &item) auto types = std::vector> (); for (auto &field : item.get_fields ()) - types.emplace_back (field.get_field_type ().reconstruct ()); + types.emplace_back (field.get_field_type ().clone_type ()); expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)), item.get_identifier ().as_string (), @@ -184,7 +184,7 @@ DeriveEq::visit_enum (Enum &item) auto &tuple = static_cast (*variant); for (auto &field : tuple.get_tuple_fields ()) - types.emplace_back (field.get_field_type ().reconstruct ()); + types.emplace_back (field.get_field_type ().clone_type ()); break; } case EnumItem::Kind::Struct: @@ -192,7 +192,7 @@ DeriveEq::visit_enum (Enum &item) auto &tuple = static_cast (*variant); for (auto &field : tuple.get_struct_fields ()) - types.emplace_back (field.get_field_type ().reconstruct ()); + types.emplace_back (field.get_field_type ().clone_type ()); break; } @@ -210,7 +210,7 @@ DeriveEq::visit_union (Union &item) auto types = std::vector> (); for (auto &field : item.get_variants ()) - types.emplace_back (field.get_field_type ().reconstruct ()); + types.emplace_back (field.get_field_type ().clone_type ()); expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)), item.get_identifier ().as_string (), diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc index 6e98a5c3724e..c2fe30492bad 100644 --- a/gcc/rust/expand/rust-expand-visitor.cc +++ b/gcc/rust/expand/rust-expand-visitor.cc @@ -25,6 +25,7 @@ #include "rust-ast.h" #include "rust-type.h" #include "rust-derive.h" +#include "rust-node-id-visitor.h" namespace Rust { @@ -51,7 +52,10 @@ builtin_derive_item (AST::Item &item, const AST::Attribute &derive, { auto items = AST::DeriveVisitor::derive (item, derive, to_derive); for (auto &item : items) + { + NodeIdVisitor::go (*item); Analysis::Mappings::get ().add_derived_node (item->get_node_id ()); + } return items; } @@ -336,6 +340,8 @@ ExpandVisitor::expand_inner_stmts (AST::BlockExpr &expr) void ExpandVisitor::maybe_expand_expr (std::unique_ptr &expr) { + rust_debug_loc (expr->get_locus (), "EXPANDING"); + NodeId old_expect = expr->get_node_id (); std::swap (macro_invoc_expect_id, old_expect); diff --git a/gcc/rust/expand/rust-expand-visitor.h b/gcc/rust/expand/rust-expand-visitor.h index b79eb663c6c3..0f92268eefea 100644 --- a/gcc/rust/expand/rust-expand-visitor.h +++ b/gcc/rust/expand/rust-expand-visitor.h @@ -27,11 +27,6 @@ namespace Rust { -/** - * Whether or not an attribute is a derive attribute - */ -bool is_derive (AST::Attribute &attr); - /** * Whether or not an attribute is builtin */ diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index b47e43afd764..7a98cda0219b 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -29,6 +29,7 @@ #include "rust-cfg-strip.h" #include "rust-proc-macro.h" #include "rust-token-tree-desugar.h" +#include "rust-node-id-visitor.h" namespace Rust { @@ -328,6 +329,8 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc, fragment = expand_decl_macro (invoc.get_locus (), invoc_data, *rdef, semicolon); + NodeIdVisitor::go (fragment); + set_expanded_fragment (std::move (fragment)); } diff --git a/gcc/rust/expand/rust-node-id-visitor.h b/gcc/rust/expand/rust-node-id-visitor.h new file mode 100644 index 000000000000..2f0fdfab63a9 --- /dev/null +++ b/gcc/rust/expand/rust-node-id-visitor.h @@ -0,0 +1,295 @@ +// Copyright (C) 2025 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// . + +#ifndef RUST_NODE_ID_VISITOR_H +#define RUST_NODE_ID_VISITOR_H + +#include "rust-ast-visitor.h" +#include "rust-expand-visitor.h" + +namespace Rust { + +// assigns node ids to instances of HasNodeId +class NodeIdVisitor : public AST::DefaultASTVisitor +{ +public: + NodeIdVisitor () {} + + static void go (AST::Crate &crate) + { + NodeIdVisitor visitor; + visitor.visit (crate); + } + + static void go (AST::Item &item) + { + NodeIdVisitor visitor; + item.accept_vis (visitor); + } + + static void go (AST::SimplePath &path) + { + NodeIdVisitor visitor; + visitor.visit (path); + } + + static void go (AST::GenericArg &arg) + { + NodeIdVisitor visitor; + arg.accept_vis (visitor); + } + + static void go (AST::Fragment &frag) + { + for (auto &node : frag.get_nodes ()) + go (node); + } + + static void go (AST::SingleASTNode &node) + { + NodeIdVisitor visitor; + node.accept_vis (visitor); + } + +#define X(type) \ + void visit (AST::type &node) override \ + { \ + handle (node); \ + if (!has_derive (node)) \ + AST::DefaultASTVisitor::visit (node); \ + } + + void visit (AST::UseDeclaration &node) override + { + handle (node); + // skip handling of import path + } + + X(Crate) + X(Token) + X(DelimTokenTree) + X(AttrInputMetaItemContainer) + X(IdentifierExpr) + X(Lifetime) + X(LifetimeParam) + X(ConstGenericParam) + X(PathInExpression) + X(TypePathSegment) + X(TypePathSegmentGeneric) + X(TypePathSegmentFunction) + X(TypePath) + X(QualifiedPathInExpression) + X(QualifiedPathInType) + X(LiteralExpr) + X(AttrInputLiteral) + X(AttrInputMacro) + X(MetaItemLitExpr) + X(MetaItemPathExpr) + X(BorrowExpr) + X(DereferenceExpr) + X(ErrorPropagationExpr) + X(NegationExpr) + X(ArithmeticOrLogicalExpr) + X(ComparisonExpr) + X(LazyBooleanExpr) + X(TypeCastExpr) + X(AssignmentExpr) + X(CompoundAssignmentExpr) + X(GroupedExpr) + X(ArrayElemsValues) + X(ArrayElemsCopied) + X(ArrayExpr) + X(ArrayIndexExpr) + X(TupleExpr) + X(TupleIndexExpr) + X(StructExprStruct) + X(StructExprFieldIdentifier) + X(StructExprFieldIdentifierValue) + X(StructExprFieldIndexValue) + X(StructExprStructFields) + X(StructExprStructBase) + X(CallExpr) + X(MethodCallExpr) + X(FieldAccessExpr) + X(ClosureExprInner) + X(BlockExpr) + X(AnonConst) + X(ConstBlock) + X(ClosureExprInnerTyped) + X(ContinueExpr) + X(BreakExpr) + X(RangeFromToExpr) + X(RangeFromExpr) + X(RangeToExpr) + X(RangeFullExpr) + X(RangeFromToInclExpr) + X(RangeToInclExpr) + X(ReturnExpr) + X(TryExpr) + X(BoxExpr) + X(UnsafeBlockExpr) + X(LoopExpr) + X(WhileLoopExpr) + X(WhileLetLoopExpr) + X(ForLoopExpr) + X(IfExpr) + X(IfExprConseqElse) + X(IfLetExpr) + X(IfLetExprConseqElse) + X(MatchExpr) + X(AwaitExpr) + X(AsyncBlockExpr) + + X(InlineAsm) + X(LlvmInlineAsm) + + X(TypeParam) + X(LifetimeWhereClauseItem) + X(TypeBoundWhereClauseItem) + X(Module) + X(ExternCrate) + X(UseTreeGlob) + X(UseTreeList) + X(UseTreeRebind) + // X(UseDeclaration) + X(Function) + X(TypeAlias) + X(StructStruct) + X(TupleStruct) + X(EnumItem) + X(EnumItemTuple) + X(EnumItemStruct) + X(EnumItemDiscriminant) + X(Enum) + X(Union) + X(ConstantItem) + X(StaticItem) + X(TraitItemType) + X(Trait) + X(InherentImpl) + X(TraitImpl) + X(ExternalTypeItem) + X(ExternalStaticItem) + X(ExternBlock) + X(MacroMatchFragment) + X(MacroMatchRepetition) + X(MacroMatcher) + X(MacroRulesDefinition) + X(MacroInvocation) + X(MetaItemPath) + X(MetaItemSeq) + X(MetaWord) + X(MetaNameValueStr) + X(MetaListPaths) + X(MetaListNameValueStr) + X(LiteralPattern) + X(IdentifierPattern) + X(WildcardPattern) + X(RestPattern) + X(RangePatternBoundLiteral) + X(RangePatternBoundPath) + X(RangePatternBoundQualPath) + X(RangePattern) + X(ReferencePattern) + X(StructPatternFieldTuplePat) + X(StructPatternFieldIdentPat) + X(StructPatternFieldIdent) + X(StructPattern) + X(TupleStructItemsNoRest) + X(TupleStructItemsHasRest) + X(TupleStructPattern) + X(TuplePatternItemsNoRest) + X(TuplePatternItemsHasRest) + X(TuplePattern) + X(GroupedPattern) + X(SlicePatternItemsNoRest) + X(SlicePatternItemsHasRest) + X(SlicePattern) + X(AltPattern) + X(EmptyStmt) + X(LetStmt) + X(ExprStmt) + X(TraitBound) + X(ImplTraitType) + X(TraitObjectType) + X(ParenthesisedType) + X(ImplTraitTypeOneBound) + X(TraitObjectTypeOneBound) + X(TupleType) + X(NeverType) + X(RawPointerType) + X(ReferenceType) + X(ArrayType) + X(SliceType) + X(InferredType) + X(BareFunctionType) + X(SelfParam) + X(FunctionParam) + X(VariadicParam) + X(FormatArgs) + X(OffsetOf) + + X(GenericArgsBinding) + X(PathExprSegment) + X(GenericArgs) + X(QualifiedPathType) + X(TypePathFunction) + X(PathIdentSegment) + X(SimplePath) + X(SimplePathSegment) + X(StructBase) + X(ClosureParam) + X(LoopLabel) + X(MatchCase) + X(MatchArm) + X(Visibility) + X(FunctionQualifiers) + X(WhereClause) + X(StructField) + X(TupleField) + X(MacroRule) + X(MacroInvocData) + X(MacroTranscriber) + X(StructPatternElements) + X(MaybeNamedParam) + +private: + template ::value && !std::is_base_of::value>::type> + void handle (T&) {} + + void handle (HasNodeId &node) { node.assign_node_id (); } + + bool has_derive (const AST::Item &node) + { + for (auto &attr : node.get_outer_attrs ()) + { + if (attr.is_derive ()) + return true; + } + return false; + } + + template bool has_derive (const T &) + { + return false; + } +}; + +} // namespace Rust + +#endif // RUST_NODE_ID_VISITOR_H diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc index 81815ff22eab..369f77e70e69 100644 --- a/gcc/rust/hir/rust-ast-lower-item.cc +++ b/gcc/rust/hir/rust-ast-lower-item.cc @@ -451,7 +451,7 @@ ASTLoweringItem::visit (AST::Function &function) continue; } - auto param = static_cast (*p); + auto ¶m = static_cast (*p); auto translated_pattern = std::unique_ptr ( ASTLoweringPattern::translate (param.get_pattern ())); diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc index 96b38f48e4e4..13aaca12ab86 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc @@ -31,6 +31,7 @@ #include "rust-hir-type-check.h" #include "rust-ice-finalizer.h" #include "rust-ast.h" +#include "rust-node-id-visitor.h" namespace Rust { namespace Resolver2_0 { @@ -641,6 +642,9 @@ Late::visit (AST::GenericArg &arg) arg = arg.disambiguate_to_const (); else arg = arg.disambiguate_to_type (); + + // make sure to set node ids + NodeIdVisitor::go (arg); } DefaultResolver::visit (arg); diff --git a/gcc/rust/resolve/rust-name-resolver.cc b/gcc/rust/resolve/rust-name-resolver.cc index dddaa0793010..25d9b627a05b 100644 --- a/gcc/rust/resolve/rust-name-resolver.cc +++ b/gcc/rust/resolve/rust-name-resolver.cc @@ -283,7 +283,7 @@ Resolver::Resolver () macro_scope (Scope (mappings.get_current_crate ())), global_type_node_id (UNKNOWN_NODEID), unit_ty_node_id (UNKNOWN_NODEID) { - generate_builtins (); + // generate_builtins (); } Resolver * @@ -459,6 +459,7 @@ Resolver::setup_builtin (const std::string &name, TyTy::BaseType *tyty) auto builtin_type = new AST::TypePath (::std::move (segs), BUILTINS_LOCATION, false); builtins.push_back (builtin_type); + builtin_type->assign_node_id (); tyctx->insert_builtin (tyty->get_ref (), builtin_type->get_node_id (), tyty); mappings.insert_node_to_hir (builtin_type->get_node_id (), tyty->get_ref ()); mappings.insert_canonical_path ( diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc index 0930f966e202..b72208b9a8cd 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc @@ -22,6 +22,7 @@ #include "rust-ast-full.h" #include "rust-hir-map.h" #include "rust-attribute-values.h" +#include "rust-node-id-visitor.h" namespace Rust { namespace Resolver2_0 { @@ -518,17 +519,26 @@ TopLevel::visit (AST::UseDeclaration &use) auto imports = std::vector (); for (auto &&path : paths) - imports.emplace_back ( - ImportKind::Simple (std::move (path), values_rib, types_rib, macros_rib)); + { + NodeIdVisitor::go (path); + imports.emplace_back ( + ImportKind::Simple (std::move (path), values_rib, types_rib, macros_rib)); + } for (auto &&glob : glob_path) - imports.emplace_back ( - ImportKind::Glob (std::move (glob), values_rib, types_rib, macros_rib)); + { + NodeIdVisitor::go (glob); + imports.emplace_back ( + ImportKind::Glob (std::move (glob), values_rib, types_rib, macros_rib)); + } for (auto &&rebind : rebind_path) - imports.emplace_back ( - ImportKind::Rebind (std::move (rebind.first), std::move (rebind.second), - values_rib, types_rib, macros_rib)); + { + NodeIdVisitor::go (rebind.first); + imports.emplace_back ( + ImportKind::Rebind (std::move (rebind.first), std::move (rebind.second), + values_rib, types_rib, macros_rib)); + } imports_to_resolve.insert ({use.get_node_id (), std::move (imports)}); } diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 641811846832..665de280a6d4 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -45,6 +45,7 @@ #include "rust-imports.h" #include "rust-extern-crate.h" #include "rust-attributes.h" +#include "rust-node-id-visitor.h" #include "rust-name-resolution-context.h" #include "rust-early-name-resolver-2.0.h" #include "rust-late-name-resolver-2.0.h" @@ -943,6 +944,8 @@ Session::expansion (AST::Crate &crate, Resolver2_0::NameResolutionContext &ctx) CfgStrip (cfg).go (crate); // Errors might happen during cfg strip pass + NodeIdVisitor::go (crate); + Resolver2_0::Early early (ctx); early.go (crate); macro_errors = early.get_macro_resolve_errors (); diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index a6d323e36144..cb330d8fb9b3 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -201,6 +201,8 @@ NodeId Mappings::get_next_node_id () { auto it = nodeIdIter; + if (it > MAX_NODEID) + rust_fatal_error (UNKNOWN_LOCATION, "out of node ids"); nodeIdIter++; return it; } @@ -262,6 +264,8 @@ AST::Crate & Mappings::insert_ast_crate (std::unique_ptr &&crate, CrateNum crate_num) { + crate->assign_node_id (); + auto it = ast_crate_mappings.find (crate_num); rust_assert (it == ast_crate_mappings.end ()); diff --git a/gcc/rust/util/rust-mapping-common.h b/gcc/rust/util/rust-mapping-common.h index 3cc5a7506c39..bd9fd733d548 100644 --- a/gcc/rust/util/rust-mapping-common.h +++ b/gcc/rust/util/rust-mapping-common.h @@ -63,6 +63,7 @@ struct DefId #define UNKNOWN_CRATENUM ((uint32_t) (UINT32_MAX)) #define UNKNOWN_NODEID ((uint32_t) (UINT32_MAX)) +#define MAX_NODEID (UNKNOWN_NODEID - 1) #define UNKNOWN_HIRID ((uint32_t) (UINT32_MAX)) #define UNKNOWN_LOCAL_DEFID ((uint32_t) (0)) #define UNKNOWN_DEFID (DefId{0, 0}) From 0e87455384dd17402c8de876f402577693947fef Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Sun, 7 Dec 2025 17:22:51 -0500 Subject: [PATCH 2/2] stash 2 --- gcc/rust/ast/rust-ast-visitor.cc | 2 ++ gcc/rust/rust-session-manager.cc | 2 ++ 2 files changed, 4 insertions(+) diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc index afdd2b19dac4..a9e2f053a011 100644 --- a/gcc/rust/ast/rust-ast-visitor.cc +++ b/gcc/rust/ast/rust-ast-visitor.cc @@ -1029,6 +1029,8 @@ void DefaultASTVisitor::visit (AST::TraitItemType &item) { visit_outer_attrs (item); + for (auto &generic : item.get_generic_params ()) + visit (generic); for (auto &bound : item.get_type_param_bounds ()) visit (bound); } diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 665de280a6d4..036bdcb755ec 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -987,6 +987,8 @@ Session::expansion (AST::Crate &crate, Resolver2_0::NameResolutionContext &ctx) AST::DesugarApit ().go (crate); + NodeIdVisitor::go (crate); + // HACK: we may need a final TopLevel pass // however, this should not count towards the recursion limit // and we don't need a full Early pass