diff --git a/gcc/rust/ast/rust-ast-full-decls.h b/gcc/rust/ast/rust-ast-full-decls.h index 418edd27cbc7..8cd397db3b6c 100644 --- a/gcc/rust/ast/rust-ast-full-decls.h +++ b/gcc/rust/ast/rust-ast-full-decls.h @@ -51,7 +51,6 @@ class Lifetime; class GenericParam; class LifetimeParam; class ConstGenericParam; -class TraitItem; class AssociatedItem; struct Crate; class PathExpr; diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 0f1a13282e08..15acba3225d7 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -3395,14 +3395,14 @@ Module::process_file_path () bool no_candidates_found = !file_mod_found && !dir_mod_found; if (multiple_candidates_found) - rust_error_at (locus, + rust_error_at (get_locus (), "two candidates found for module %s: %s.rs and %s%smod.rs", module_name.as_string ().c_str (), module_name.as_string ().c_str (), module_name.as_string ().c_str (), file_separator); if (no_candidates_found) - rust_error_at (locus, "no candidate found for module %s", + rust_error_at (get_locus (), "no candidate found for module %s", module_name.as_string ().c_str ()); if (no_candidates_found || multiple_candidates_found) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 9581dd57b739..29aaa9d2e3ac 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -75,6 +75,66 @@ namespace AST { class ASTVisitor; using AttrVec = std::vector; +class Located +{ +public: + virtual location_t get_locus () const = 0; +}; + +class LocatedImpl : virtual public Located +{ +private: + location_t locus; + +protected: + LocatedImpl (location_t locus) : locus (locus) {} + +public: + location_t get_locus () const override final { return locus; } +}; + +class NodeIdStore +{ + NodeId node_id; + +public: + NodeIdStore () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} + + NodeId get_node_id () const { return node_id; } + + friend class Expr; +}; + +class Located +{ +public: + virtual location_t get_locus () const = 0; +}; + +class LocatedImpl : virtual public Located +{ +private: + location_t locus; + +protected: + LocatedImpl (location_t locus) : locus (locus) {} + +public: + location_t get_locus () const override final { return locus; } +}; + +class NodeIdStore +{ + NodeId node_id; + +public: + NodeIdStore () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} + + NodeId get_node_id () const { return node_id; } + + friend class Expr; +}; + class Visitable { public: @@ -346,18 +406,16 @@ class PathSegment }; // A segment of a simple path without generic or type arguments -class SimplePathSegment : public PathSegment +class SimplePathSegment : public PathSegment, virtual public NodeIdStore { std::string segment_name; location_t locus; - NodeId node_id; // only allow identifiers, "super", "self", "crate", or "$crate" public: // TODO: put checks in constructor to enforce this rule? SimplePathSegment (std::string segment_name, location_t locus) - : segment_name (std::move (segment_name)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + : segment_name (std::move (segment_name)), locus (locus) {} /* Returns whether simple path segment is in an invalid state (currently, if @@ -373,7 +431,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 { @@ -394,12 +451,11 @@ class SimplePathSegment : public PathSegment }; // A simple path without generic or type arguments -class SimplePath +class SimplePath : public NodeIdStore { bool opening_scope_resolution; std::vector segments; location_t locus; - NodeId node_id; public: // Constructor @@ -407,15 +463,13 @@ class SimplePath bool has_opening_scope_resolution = false, location_t locus = UNDEF_LOCATION) : opening_scope_resolution (has_opening_scope_resolution), - segments (std::move (path_segments)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + segments (std::move (path_segments)), locus (locus) {} SimplePath (Identifier ident) : opening_scope_resolution (false), segments ({SimplePathSegment (ident.as_string (), ident.get_locus ())}), - locus (ident.get_locus ()), - node_id (Analysis::Mappings::get ().get_next_node_id ()) + locus (ident.get_locus ()) {} // Creates an empty SimplePath. @@ -435,7 +489,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 @@ -1077,7 +1130,9 @@ 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, + virtual public Located, + virtual public NodeIdStore { public: enum class Kind @@ -1099,11 +1154,8 @@ class Stmt : public Visitable virtual std::string as_string () const = 0; - virtual location_t get_locus () const = 0; - virtual void mark_for_strip () = 0; virtual bool is_marked_for_strip () const = 0; - NodeId get_node_id () const { return node_id; } virtual Kind get_stmt_kind () = 0; @@ -1114,12 +1166,8 @@ class Stmt : public Visitable virtual void add_semicolon () {} protected: - Stmt () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} - // Clone function implementation as pure virtual method virtual Stmt *clone_stmt_impl () const = 0; - - NodeId node_id; }; // Rust "item" AST node (declaration of top-level/module-level allowed stuff) @@ -1236,7 +1284,9 @@ class VisItem : public Item class ExprWithoutBlock; // Base expression AST node - abstract -class Expr : public Visitable +class Expr : public Visitable, + virtual public Located, + virtual public NodeIdStore { public: enum class Kind @@ -1302,8 +1352,6 @@ class Expr : public Visitable virtual ~Expr () {} - virtual location_t get_locus () const = 0; - virtual bool is_literal () const { return false; } // HACK: strictly not needed, but faster than full downcast clone @@ -1312,8 +1360,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; @@ -1323,13 +1369,8 @@ class Expr : public Visitable virtual void set_outer_attrs (std::vector) = 0; protected: - // Constructor - Expr () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} - // Clone function implementation as pure virtual method virtual Expr *clone_expr_impl () const = 0; - - NodeId node_id; }; // AST node for an expression without an accompanying block - abstract @@ -1417,7 +1458,9 @@ class IdentifierExpr : public ExprWithoutBlock }; // Pattern base AST node -class Pattern : public Visitable +class Pattern : public Visitable, + virtual public Located, + virtual public NodeIdStore { public: enum class Kind @@ -1456,9 +1499,6 @@ class Pattern : public Visitable virtual void mark_for_strip () {} virtual bool is_marked_for_strip () const { return false; } - virtual location_t get_locus () const = 0; - virtual NodeId get_node_id () const = 0; - protected: // Clone pattern implementation as pure virtual method virtual Pattern *clone_pattern_impl () const = 0; @@ -1468,7 +1508,9 @@ class Pattern : public Visitable class TraitBound; // Base class for types as represented in AST - abstract -class Type : public Visitable +class Type : public Visitable, + virtual public Located, + virtual public NodeIdStore { public: // Unique pointer custom clone function @@ -1492,17 +1534,9 @@ class Type : public Visitable virtual void mark_for_strip () {} virtual bool is_marked_for_strip () const { return false; } - virtual location_t get_locus () const = 0; - - NodeId get_node_id () const { return node_id; } - protected: - Type () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} - // Clone function implementation as pure virtual method virtual Type *clone_type_impl () const = 0; - - NodeId node_id; }; // A type without parentheses? - abstract @@ -1715,7 +1749,8 @@ class LifetimeParam : public GenericParam } }; -class AssociatedItem : public Visitable +// Abstract base class for items used within an impl block +class AssociatedItem : public Visitable, virtual public Located { protected: // Clone function implementation as pure virtual method @@ -1724,6 +1759,7 @@ class AssociatedItem : public Visitable public: virtual ~AssociatedItem () {} + // Unique pointer custom clone function std::unique_ptr clone_associated_item () const { return std::unique_ptr (clone_associated_item_impl ()); @@ -1733,50 +1769,12 @@ class AssociatedItem : public Visitable virtual void mark_for_strip () = 0; virtual bool is_marked_for_strip () const = 0; - - virtual location_t get_locus () const = 0; -}; - -// Item used in trait declarations - abstract base class -class TraitItem : public AssociatedItem -{ -protected: - TraitItem (location_t locus) - : node_id (Analysis::Mappings::get ().get_next_node_id ()), - 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) - {} - - // Clone function implementation as pure virtual method - virtual TraitItem *clone_associated_item_impl () const override = 0; - - NodeId node_id; - Visibility vis; - location_t locus; - -public: - // Unique pointer custom clone function - std::unique_ptr clone_trait_item () const - { - return std::unique_ptr (clone_associated_item_impl ()); - } - - NodeId get_node_id () const { 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, virtual public NodeIdStore { public: - ExternalItem () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {} - - ExternalItem (NodeId node_id) : node_id (node_id) {} - virtual ~ExternalItem () {} // Unique pointer custom clone function @@ -1790,13 +1788,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 diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 2f53f8d117e4..c8153fe6db48 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -86,7 +86,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 +102,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 @@ -738,7 +738,7 @@ class FunctionParam : public Param }; // Rust module item - abstract base class -class Module : public VisItem +class Module : public VisItem, public LocatedImpl { public: // Type of the current module. A module can be either loaded or unloaded, @@ -756,7 +756,6 @@ class Module : public VisItem private: Identifier module_name; - location_t locus; ModuleKind kind; Unsafety safety; @@ -792,9 +791,9 @@ class Module : public VisItem std::vector outer_attrs, location_t locus, Unsafety safety, std::string outer_filename, std::vector module_scope) : VisItem (std::move (visibility), std::move (outer_attrs)), - module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED), - safety (safety), outer_filename (outer_filename), - inner_attrs (std::vector ()), + LocatedImpl (locus), module_name (module_name), + kind (ModuleKind::UNLOADED), safety (safety), + outer_filename (outer_filename), inner_attrs (std::vector ()), items (std::vector> ()), module_scope (std::move (module_scope)) {} @@ -807,14 +806,14 @@ class Module : public VisItem std::vector inner_attrs = std::vector (), std::vector outer_attrs = std::vector ()) : VisItem (std::move (visibility), std::move (outer_attrs)), - module_name (name), locus (locus), kind (ModuleKind::LOADED), + LocatedImpl (locus), module_name (name), kind (ModuleKind::LOADED), safety (safety), outer_filename (std::string ()), inner_attrs (std::move (inner_attrs)), items (std::move (items)) {} // Copy constructor with vector clone Module (Module const &other) - : VisItem (other), module_name (other.module_name), locus (other.locus), + : VisItem (other), LocatedImpl (other), module_name (other.module_name), kind (other.kind), safety (other.safety), inner_attrs (other.inner_attrs), module_scope (other.module_scope) { @@ -830,9 +829,9 @@ class Module : public VisItem Module &operator= (Module const &other) { VisItem::operator= (other); + LocatedImpl::operator= (other); module_name = other.module_name; - locus = other.locus; kind = other.kind; inner_attrs = other.inner_attrs; module_scope = other.module_scope; @@ -887,8 +886,6 @@ class Module : public VisItem std::string as_string () const override; - location_t get_locus () const override final { return locus; } - // Invalid if name is empty, so base stripping on that. void mark_for_strip () override { module_name = {""}; } bool is_marked_for_strip () const override { return module_name.empty (); } @@ -1487,7 +1484,9 @@ class Function : public VisItem, public AssociatedItem, public ExternalItem }; // Rust type alias (i.e. typedef) AST node -class TypeAlias : public VisItem, public AssociatedItem +class TypeAlias : public VisItem, + public AssociatedItem, + virtual public LocatedImpl { Identifier new_type_name; @@ -1500,8 +1499,6 @@ class TypeAlias : public VisItem, public AssociatedItem std::unique_ptr existing_type; - location_t locus; - public: std::string as_string () const override; @@ -1517,17 +1514,17 @@ class TypeAlias : public VisItem, public AssociatedItem WhereClause where_clause, std::unique_ptr existing_type, Visibility vis, std::vector outer_attrs, location_t locus) - : VisItem (std::move (vis), std::move (outer_attrs)), + : LocatedImpl (locus), VisItem (std::move (vis), std::move (outer_attrs)), new_type_name (std::move (new_type_name)), generic_params (std::move (generic_params)), where_clause (std::move (where_clause)), - existing_type (std::move (existing_type)), locus (locus) + existing_type (std::move (existing_type)) {} // Copy constructor TypeAlias (TypeAlias const &other) - : VisItem (other), new_type_name (other.new_type_name), - where_clause (other.where_clause), locus (other.locus) + : LocatedImpl (other), VisItem (other), new_type_name (other.new_type_name), + where_clause (other.where_clause) { // guard to prevent null dereference (only required if error state) if (other.existing_type != nullptr) @@ -1542,11 +1539,11 @@ class TypeAlias : public VisItem, public AssociatedItem TypeAlias &operator= (TypeAlias const &other) { VisItem::operator= (other); + LocatedImpl::operator= (other); new_type_name = other.new_type_name; where_clause = other.where_clause; // visibility = other.visibility->clone_visibility(); // outer_attrs = other.outer_attrs; - locus = other.locus; // guard to prevent null dereference (only required if error state) if (other.existing_type != nullptr) @@ -1565,8 +1562,6 @@ class TypeAlias : public VisItem, public AssociatedItem TypeAlias (TypeAlias &&other) = default; TypeAlias &operator= (TypeAlias &&other) = default; - location_t get_locus () const override final { return locus; } - void accept_vis (ASTVisitor &vis) override; // Invalid if existing type is null, so base stripping on that. @@ -2513,7 +2508,7 @@ class ConstantItem : public VisItem, public AssociatedItem * as identifier) constant. */ bool is_unnamed () const { return identifier == "_"; } - location_t get_locus () const override final { return locus; } + location_t get_locus () const { return locus; } void accept_vis (ASTVisitor &vis) override; @@ -2696,7 +2691,9 @@ class StaticItem : public VisItem }; // Constant item within traits -class TraitItemConst : public TraitItem +class TraitItemConst : public AssociatedItem, + public LocatedImpl, + public NodeIdStore { std::vector outer_attrs; Identifier name; @@ -2712,17 +2709,15 @@ class TraitItemConst : public TraitItem TraitItemConst (Identifier name, std::unique_ptr type, std::unique_ptr expr, std::vector outer_attrs, location_t locus) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), + : LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), name (std::move (name)), type (std::move (type)), expr (std::move (expr)) {} // Copy constructor with clones TraitItemConst (TraitItemConst const &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), + : LocatedImpl (other), NodeIdStore (other), outer_attrs (other.outer_attrs), name (other.name) { - node_id = other.node_id; - // guard to prevent null dereference if (other.expr != nullptr) expr = other.expr->clone_expr (); @@ -2735,11 +2730,10 @@ class TraitItemConst : public TraitItem // Overloaded assignment operator to clone TraitItemConst &operator= (TraitItemConst const &other) { - TraitItem::operator= (other); + LocatedImpl::operator= (other); + NodeIdStore::operator= (other); outer_attrs = other.outer_attrs; name = other.name; - locus = other.locus; - node_id = other.node_id; // guard to prevent null dereference if (other.expr != nullptr) @@ -2762,8 +2756,6 @@ class TraitItemConst : public TraitItem std::string as_string () const override; - location_t get_locus () const override { return locus; } - void accept_vis (ASTVisitor &vis) override; // Invalid if type is null, so base stripping on that. @@ -2813,7 +2805,9 @@ class TraitItemConst : public TraitItem }; // Type items within traits -class TraitItemType : public TraitItem +class TraitItemType : public AssociatedItem, + public LocatedImpl, + public NodeIdStore { std::vector outer_attrs; @@ -2832,16 +2826,15 @@ class TraitItemType : public TraitItem std::vector> type_param_bounds, std::vector outer_attrs, Visibility vis, location_t locus) - : TraitItem (vis, locus), outer_attrs (std::move (outer_attrs)), + : LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), name (std::move (name)), type_param_bounds (std::move (type_param_bounds)) {} // Copy constructor with vector clone TraitItemType (TraitItemType const &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), + : LocatedImpl (other), NodeIdStore (other), outer_attrs (other.outer_attrs), name (other.name) { - node_id = other.node_id; type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) type_param_bounds.push_back (e->clone_type_param_bound ()); @@ -2850,11 +2843,10 @@ class TraitItemType : public TraitItem // Overloaded assignment operator with vector clone TraitItemType &operator= (TraitItemType const &other) { - TraitItem::operator= (other); + LocatedImpl::operator= (other); + NodeIdStore::operator= (other); outer_attrs = other.outer_attrs; name = other.name; - locus = other.locus; - node_id = other.node_id; type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) @@ -3424,22 +3416,20 @@ class ExternalTypeItem : public ExternalItem public: ExternalTypeItem (Identifier item_name, Visibility vis, std::vector outer_attrs, location_t locus) - : ExternalItem (), outer_attrs (std::move (outer_attrs)), visibility (vis), + : outer_attrs (std::move (outer_attrs)), visibility (vis), item_name (std::move (item_name)), locus (locus), marked_for_strip (false) {} // 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; @@ -3505,18 +3495,17 @@ class ExternalStaticItem : public ExternalItem ExternalStaticItem (Identifier item_name, std::unique_ptr item_type, bool is_mut, Visibility vis, std::vector outer_attrs, location_t locus) - : ExternalItem (), outer_attrs (std::move (outer_attrs)), - visibility (std::move (vis)), item_name (std::move (item_name)), - locus (locus), has_mut (is_mut), item_type (std::move (item_type)) + : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)), + item_name (std::move (item_name)), locus (locus), has_mut (is_mut), + item_type (std::move (item_type)) {} // Copy constructor ExternalStaticItem (ExternalStaticItem const &other) - : 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 (); @@ -3525,7 +3514,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 575cd231af79..a04e10882852 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -448,7 +448,7 @@ struct MacroRule }; // A macro rules definition item AST node -class MacroRulesDefinition : public VisItem +class MacroRulesDefinition : public VisItem, public LocatedImpl { public: enum MacroKind @@ -467,7 +467,6 @@ class MacroRulesDefinition : public VisItem DelimType delim_type; // MacroRules rules; std::vector rules; // inlined form - location_t locus; MacroTranscriberFunc associated_transcriber; @@ -497,9 +496,9 @@ class MacroRulesDefinition : public VisItem std::vector rules, std::vector outer_attrs, location_t locus, MacroKind kind, Visibility vis) - : VisItem (std::move (vis), outer_attrs), + : VisItem (std::move (vis), outer_attrs), LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), rule_name (std::move (rule_name)), - delim_type (delim_type), rules (std::move (rules)), locus (locus), + delim_type (delim_type), rules (std::move (rules)), associated_transcriber (dummy_builtin), is_builtin_rule (false), kind (kind) {} @@ -508,10 +507,11 @@ class MacroRulesDefinition : public VisItem MacroTranscriberFunc associated_transcriber, MacroKind kind, Visibility vis) : VisItem (std::move (vis), std::vector ()), - outer_attrs (std::vector ()), rule_name (builtin_name), - delim_type (delim_type), rules (std::vector ()), - locus (UNDEF_LOCATION), associated_transcriber (associated_transcriber), - is_builtin_rule (true), kind (kind) + LocatedImpl (UNDEF_LOCATION), outer_attrs (std::vector ()), + rule_name (builtin_name), delim_type (delim_type), + rules (std::vector ()), + associated_transcriber (associated_transcriber), is_builtin_rule (true), + kind (kind) {} public: @@ -553,8 +553,6 @@ class MacroRulesDefinition : public VisItem std::vector &get_macro_rules () { return rules; } const std::vector &get_macro_rules () const { return rules; } - location_t get_locus () const override final { return locus; } - Identifier get_rule_name () const { return rule_name; } std::vector &get_rules () { return rules; } @@ -597,7 +595,8 @@ class MacroRulesDefinition : public VisItem class MacroInvocation : public TypeNoBounds, public Pattern, public Item, - public TraitItem, + public AssociatedItem, + virtual public LocatedImpl, public ExternalItem, public ExprWithoutBlock { @@ -645,8 +644,6 @@ class MacroInvocation : public TypeNoBounds, std::move (pending_eager_invocations))); } - location_t get_locus () const override final { return locus; } - void accept_vis (ASTVisitor &vis) override; // Invalid if path is empty, so base stripping on that. @@ -667,12 +664,7 @@ 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; } + NodeId get_macro_node_id () const { return macro_node_id; } MacroInvocData &get_invoc_data () { return invoc_data; } @@ -709,19 +701,18 @@ class MacroInvocation : public TypeNoBounds, MacroInvocData invoc_data, std::vector outer_attrs, location_t locus, bool is_semi_coloned, std::vector> &&pending_eager_invocs) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), locus (locus), - node_id (Analysis::Mappings::get ().get_next_node_id ()), + : LocatedImpl (locus), outer_attrs (std::move (outer_attrs)), + macro_node_id (Analysis::Mappings::get ().get_next_node_id ()), invoc_data (std::move (invoc_data)), is_semi_coloned (is_semi_coloned), kind (kind), builtin_kind (builtin_kind), pending_eager_invocs (std::move (pending_eager_invocs)) {} MacroInvocation (const MacroInvocation &other) - : TraitItem (other.locus), ExternalItem (other.node_id), - outer_attrs (other.outer_attrs), locus (other.locus), - node_id (other.node_id), invoc_data (other.invoc_data), - is_semi_coloned (other.is_semi_coloned), kind (other.kind), - builtin_kind (other.builtin_kind) + : NodeIdStore (other), ExternalItem (other), LocatedImpl (other), + outer_attrs (other.outer_attrs), macro_node_id (other.macro_node_id), + invoc_data (other.invoc_data), is_semi_coloned (other.is_semi_coloned), + kind (other.kind), builtin_kind (other.builtin_kind) { if (other.kind == InvocKind::Builtin) for (auto &pending : other.pending_eager_invocs) @@ -730,8 +721,7 @@ class MacroInvocation : public TypeNoBounds, } std::vector outer_attrs; - location_t locus; - NodeId node_id; + NodeId macro_node_id; /* The data given to the macro invocation */ MacroInvocData invoc_data; diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h index 6f53188d9a37..f6ea974feffc 100644 --- a/gcc/rust/ast/rust-path.h +++ b/gcc/rust/ast/rust-path.h @@ -495,13 +495,12 @@ struct GenericArgs /* A segment of a path in expression, including an identifier aspect and maybe * generic args */ -class PathExprSegment +class PathExprSegment : virtual public NodeIdStore { // or should this extend PathIdentSegment? private: PathIdentSegment segment_name; GenericArgs generic_args; location_t locus; - NodeId node_id; public: // Returns true if there are any generic arguments @@ -511,8 +510,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 @@ -525,7 +523,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. @@ -551,8 +549,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 (); @@ -642,12 +638,13 @@ class Path : public Pattern /* AST node representing a path-in-expression pattern (path that allows * generic arguments) */ -class PathInExpression : public Path, public ExprWithoutBlock +class PathInExpression : public Path, + public ExprWithoutBlock, + virtual public NodeIdStore { std::vector outer_attrs; bool has_opening_scope_resolution; location_t locus; - NodeId _node_id; bool marked_for_strip; @@ -660,16 +657,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 ()), - marked_for_strip (false) - {} - - PathInExpression (LangItem::Kind lang_item, - 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) + locus (locus), marked_for_strip (false) {} // Creates an error state path in expression. @@ -707,8 +695,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; } @@ -1167,25 +1153,23 @@ class TypePath : public TypeNoBounds // Constructor TypePath (std::vector> segments, location_t locus, bool has_opening_scope_resolution = false) - : TypeNoBounds (), - has_opening_scope_resolution (has_opening_scope_resolution), + : has_opening_scope_resolution (has_opening_scope_resolution), segments (std::move (segments)), locus (locus) {} TypePath (LangItem::Kind lang_item, std::vector> segments, location_t locus, bool has_opening_scope_resolution = false) - : TypeNoBounds (), - has_opening_scope_resolution (has_opening_scope_resolution), + : has_opening_scope_resolution (has_opening_scope_resolution), segments (std::move (segments)), locus (locus) {} // Copy constructor with vector clone TypePath (TypePath const &other) - : has_opening_scope_resolution (other.has_opening_scope_resolution), + : TypeNoBounds (other), + has_opening_scope_resolution (other.has_opening_scope_resolution), locus (other.locus) { - node_id = other.node_id; segments.reserve (other.segments.size ()); for (const auto &e : other.segments) segments.push_back (e->clone_type_path_segment ()); @@ -1194,7 +1178,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; @@ -1337,7 +1321,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; @@ -1347,8 +1330,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 @@ -1390,8 +1372,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; diff --git a/gcc/rust/ast/rust-pattern.h b/gcc/rust/ast/rust-pattern.h index 6810732fe219..71aa05d93b20 100644 --- a/gcc/rust/ast/rust-pattern.h +++ b/gcc/rust/ast/rust-pattern.h @@ -211,8 +211,6 @@ class RestPattern : public Pattern 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: diff --git a/gcc/rust/ast/rust-stmt.h b/gcc/rust/ast/rust-stmt.h index e64fbe4acd0a..c9a5f6985530 100644 --- a/gcc/rust/ast/rust-stmt.h +++ b/gcc/rust/ast/rust-stmt.h @@ -223,15 +223,12 @@ class LetStmt : public Stmt }; // Expression statements (statements containing an expression) -class ExprStmt : public Stmt +class ExprStmt : public Stmt, public LocatedImpl { std::unique_ptr expr; - location_t locus; bool semicolon_followed; public: - location_t get_locus () const override final { return locus; } - bool is_item () const override final { return false; } bool is_expr () const override final { return true; } @@ -248,13 +245,13 @@ class ExprStmt : public Stmt ExprStmt (std::unique_ptr &&expr, location_t locus, bool semicolon_followed) - : expr (std::move (expr)), locus (locus), + : LocatedImpl (locus), expr (std::move (expr)), semicolon_followed (semicolon_followed) {} // Copy constructor with clone ExprStmt (ExprStmt const &other) - : locus (other.locus), semicolon_followed (other.semicolon_followed) + : LocatedImpl (other), semicolon_followed (other.semicolon_followed) { // guard to prevent null dereference (only required if error state) if (other.expr != nullptr) @@ -265,6 +262,7 @@ class ExprStmt : public Stmt ExprStmt &operator= (ExprStmt const &other) { Stmt::operator= (other); + LocatedImpl::operator= (other); // guard to prevent null dereference (only required if error state) if (other.expr != nullptr) @@ -272,7 +270,6 @@ class ExprStmt : public Stmt else expr = nullptr; - locus = other.locus; semicolon_followed = other.semicolon_followed; return *this; diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index e165998e2cb9..0057aeadaaef 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -5307,7 +5307,7 @@ Parser::parse_impl (AST::Visibility vis, // parse inner attributes (optional) AST::AttrVec inner_attrs = parse_inner_attributes (); - // parse inherent impl items + // parse associated items std::vector> impl_items; const_TokenPtr t = lexer.peek_token ();