LCOV - code coverage report
Current view: top level - gcc/rust/ast - rust-path.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 86.8 % 523 454
Test Date: 2026-11-14 16:39:50 Functions: 85.9 % 142 122
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
       2              : 
       3              : // This file is part of GCC.
       4              : 
       5              : // GCC is free software; you can redistribute it and/or modify it under
       6              : // the terms of the GNU General Public License as published by the Free
       7              : // Software Foundation; either version 3, or (at your option) any later
       8              : // version.
       9              : 
      10              : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11              : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12              : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13              : // for more details.
      14              : 
      15              : // You should have received a copy of the GNU General Public License
      16              : // along with GCC; see the file COPYING3.  If not see
      17              : // <http://www.gnu.org/licenses/>.
      18              : 
      19              : #ifndef RUST_AST_PATH_H
      20              : #define RUST_AST_PATH_H
      21              : /* "Path" (identifier within namespaces, essentially) handling. Required include
      22              :  * for virtually all AST-related functionality. */
      23              : 
      24              : #include "optional.h"
      25              : #include "rust-ast.h"
      26              : #include "rust-hir-map.h"
      27              : #include "rust-mapping-common.h"
      28              : #include "rust-system.h"
      29              : #include "rust-raw-path.h"
      30              : 
      31              : namespace Rust {
      32              : namespace AST {
      33              : 
      34              : // The "identifier" (not generic args) aspect of each path expression segment
      35     14860887 : class PathIdentSegment
      36              : {
      37              :   std::string segment_name;
      38              :   location_t locus;
      39              : 
      40              :   // only allow identifiers, "super", "self", "Self", "crate", or "$crate"
      41              : public:
      42      3647813 :   PathIdentSegment (std::string segment_name, location_t locus)
      43      3698100 :     : segment_name (std::move (segment_name)), locus (locus)
      44              :   {}
      45              : 
      46              :   // Creates an error PathIdentSegment.
      47           11 :   static PathIdentSegment create_error ()
      48              :   {
      49           11 :     return PathIdentSegment ("", UNDEF_LOCATION);
      50              :   }
      51              : 
      52              :   // Returns whether PathIdentSegment is in an error state.
      53      3194045 :   bool is_error () const { return segment_name.empty (); }
      54              : 
      55      7598779 :   std::string as_string () const { return segment_name; }
      56              : 
      57      1558293 :   location_t get_locus () const { return locus; }
      58              : 
      59        44135 :   bool is_super_path_seg () const
      60              :   {
      61        88270 :     return as_string ().compare ("super") == 0;
      62              :   }
      63        44134 :   bool is_crate_path_seg () const
      64              :   {
      65        88268 :     return as_string ().compare ("crate") == 0;
      66              :   }
      67      4344236 :   bool is_lower_self_seg () const { return as_string ().compare ("self") == 0; }
      68      2691916 :   bool is_big_self_seg () const { return as_string ().compare ("Self") == 0; }
      69              : };
      70              : 
      71              : // A binding of an identifier to a type used in generic arguments in paths
      72              : struct GenericArgsBinding
      73              : {
      74              : public:
      75              :   enum class Kind
      76              :   {
      77              :     Equality,
      78              :     Constraint
      79              :   };
      80              : 
      81              : private:
      82              :   Identifier identifier;
      83              :   std::unique_ptr<Type> type;
      84              :   location_t locus;
      85              :   Kind kind;
      86              : 
      87              : public:
      88              :   // Returns whether binding is in an error state.
      89         2868 :   bool is_error () const
      90              :   {
      91         2868 :     return type == nullptr;
      92              :     // and also identifier is empty, but cheaper computation
      93              :   }
      94              : 
      95            0 :   GenericArgsBinding reconstruct () const
      96              :   {
      97            0 :     std::unique_ptr<Type> new_type = nullptr;
      98            0 :     if (type)
      99            0 :       new_type = type->reconstruct ();
     100              : 
     101            0 :     return GenericArgsBinding (identifier, std::move (new_type), locus, kind);
     102            0 :   }
     103              : 
     104              :   // Creates an error state generic args binding.
     105            0 :   static GenericArgsBinding create_error ()
     106              :   {
     107            0 :     return GenericArgsBinding ({""}, nullptr);
     108              :   }
     109              : 
     110              :   // Pointer type for type in constructor to enable polymorphism
     111         2868 :   GenericArgsBinding (Identifier ident, std::unique_ptr<Type> type_ptr,
     112              :                       location_t locus = UNDEF_LOCATION,
     113              :                       Kind kind = Kind::Equality)
     114         2868 :     : identifier (std::move (ident)), type (std::move (type_ptr)),
     115         2868 :       locus (locus), kind (kind)
     116              :   {}
     117              : 
     118              :   // Copy constructor has to deep copy the type as it is a unique pointer
     119        14391 :   GenericArgsBinding (GenericArgsBinding const &other)
     120        14391 :     : identifier (other.identifier), locus (other.locus), kind (other.kind)
     121              :   {
     122              :     // guard to protect from null pointer dereference
     123        14391 :     if (other.type != nullptr)
     124        14391 :       type = other.type->clone_type ();
     125        14391 :   }
     126              : 
     127              :   // default destructor
     128        17316 :   ~GenericArgsBinding () = default;
     129              : 
     130              :   // Overload assignment operator to deep copy the pointed-to type
     131              :   GenericArgsBinding &operator= (GenericArgsBinding const &other)
     132              :   {
     133              :     identifier = other.identifier;
     134              :     locus = other.locus;
     135              :     kind = other.kind;
     136              : 
     137              :     // guard to protect from null pointer dereference
     138              :     if (other.type != nullptr)
     139              :       type = other.type->clone_type ();
     140              :     else
     141              :       type = nullptr;
     142              : 
     143              :     return *this;
     144              :   }
     145              : 
     146              :   // move constructors
     147         2927 :   GenericArgsBinding (GenericArgsBinding &&other) = default;
     148              :   GenericArgsBinding &operator= (GenericArgsBinding &&other) = default;
     149              : 
     150              :   std::string as_string () const;
     151              : 
     152              :   // TODO: is this better? Or is a "vis_pattern" better?
     153       454619 :   Type &get_type ()
     154              :   {
     155       454619 :     rust_assert (type != nullptr);
     156       454619 :     return *type;
     157              :   }
     158              : 
     159       188154 :   std::unique_ptr<Type> &get_type_ptr ()
     160              :   {
     161       188154 :     rust_assert (type != nullptr);
     162       188154 :     return type;
     163              :   }
     164              : 
     165         2925 :   location_t get_locus () const { return locus; }
     166              : 
     167         2925 :   Identifier get_identifier () const { return identifier; }
     168         5736 :   Kind get_kind () const { return kind; }
     169              : };
     170              : 
     171              : /* Class representing a const generic application */
     172              : class GenericArg
     173              : {
     174              : public:
     175              :   /**
     176              :    * const generic arguments cannot always be differentiated with generic type
     177              :    * arguments during parsing, e.g:
     178              :    * ```rust
     179              :    * let a: Foo<N>;
     180              :    * ```
     181              :    *
     182              :    * Is N a type? A constant defined elsewhere? The parser cannot know, and must
     183              :    * not draw any conclusions. We must wait until later passes of the compiler
     184              :    * to decide whether this refers to a constant item or a type.
     185              :    *
     186              :    * On the other hand, simple expressions like literals or block expressions
     187              :    * will always be constant expressions: There is no ambiguity at all.
     188              :    */
     189              :   enum class Kind
     190              :   {
     191              :     Const,  // A const value
     192              :     Type,   // A type argument (not discernable during parsing)
     193              :     Either, // Either a type or a const value, cleared up during resolving
     194              :   };
     195              : 
     196          469 :   static GenericArg create_const (std::unique_ptr<Expr> expression)
     197              :   {
     198          469 :     auto locus = expression->get_locus ();
     199          469 :     return GenericArg (std::move (expression), nullptr, {""}, Kind::Const,
     200          938 :                        locus);
     201              :   }
     202              : 
     203       123278 :   static GenericArg create_type (std::unique_ptr<Type> type)
     204              :   {
     205       123278 :     auto locus = type->get_locus ();
     206       369834 :     return GenericArg (nullptr, std::move (type), {""}, Kind::Type, locus);
     207              :   }
     208              : 
     209        94191 :   static GenericArg create_ambiguous (Identifier path, location_t locus)
     210              :   {
     211       188382 :     return GenericArg (nullptr, nullptr, std::move (path), Kind::Either, locus);
     212              :   }
     213              : 
     214           29 :   GenericArg reconstruct () const
     215              :   {
     216           29 :     switch (kind)
     217              :       {
     218            1 :       case Kind::Type:
     219            1 :         return create_type (type->reconstruct ());
     220            0 :       case Kind::Const:
     221              :         // FIXME: Use reconstruct_expr when available
     222            0 :         return create_const (expression->clone_expr ());
     223           28 :       case Kind::Either:
     224           28 :       default:
     225              :         // For ambiguous or error states, copy constructs are sufficient
     226           28 :         return GenericArg (*this);
     227              :       }
     228              :   }
     229              : 
     230       819842 :   GenericArg (const GenericArg &other)
     231       819842 :     : path (other.path), kind (other.kind), locus (other.locus)
     232              :   {
     233       819842 :     if (other.expression)
     234          535 :       expression = other.expression->clone_expr ();
     235       819842 :     if (other.type)
     236       262283 :       type = other.type->clone_type ();
     237       819842 :   }
     238              : 
     239              :   GenericArg operator= (const GenericArg &other)
     240              :   {
     241              :     kind = other.kind;
     242              :     path = other.path;
     243              :     locus = other.locus;
     244              : 
     245              :     if (other.expression)
     246              :       expression = other.expression->clone_expr ();
     247              :     if (other.type)
     248              :       type = other.type->clone_type ();
     249              : 
     250              :     return *this;
     251              :   }
     252              : 
     253       270922 :   GenericArg (GenericArg &&other) = default;
     254        84471 :   GenericArg &operator= (GenericArg &&other) = default;
     255              : 
     256     30199479 :   Kind get_kind () const { return kind; }
     257            1 :   location_t get_locus () const { return locus; }
     258              : 
     259     16307988 :   void accept_vis (AST::ASTVisitor &visitor)
     260              :   {
     261     16307988 :     switch (get_kind ())
     262              :       {
     263         8126 :       case Kind::Const:
     264         8126 :         get_expression ().accept_vis (visitor);
     265         8126 :         break;
     266      5161146 :       case Kind::Type:
     267      5161146 :         get_type ().accept_vis (visitor);
     268      5161146 :         break;
     269              :       case Kind::Either:
     270              :         break;
     271              :       }
     272     16307988 :   }
     273              : 
     274        10487 :   Expr &get_expression ()
     275              :   {
     276        10487 :     rust_assert (kind == Kind::Const);
     277              : 
     278        10487 :     return *expression;
     279              :   }
     280              : 
     281         1564 :   std::unique_ptr<Expr> &get_expression_ptr ()
     282              :   {
     283         1564 :     rust_assert (kind == Kind::Const);
     284              : 
     285         1564 :     return expression;
     286              :   }
     287              : 
     288      8032450 :   Type &get_type ()
     289              :   {
     290      8032450 :     rust_assert (kind == Kind::Type);
     291              : 
     292      8032450 :     return *type;
     293              :   }
     294              : 
     295      1187331 :   std::unique_ptr<Type> &get_type_ptr ()
     296              :   {
     297      1187331 :     rust_assert (kind == Kind::Type);
     298              : 
     299      1187331 :     return type;
     300              :   }
     301              : 
     302       168996 :   const std::string get_path () const
     303              :   {
     304       168996 :     rust_assert (kind == Kind::Either);
     305              : 
     306       168996 :     return path.as_string ();
     307              :   }
     308              : 
     309            0 :   std::string as_string () const
     310              :   {
     311            0 :     switch (get_kind ())
     312              :       {
     313            0 :       case Kind::Either:
     314            0 :         return "Ambiguous: " + path.as_string ();
     315            0 :       case Kind::Const:
     316            0 :         return "Const: { " + expression->as_string () + " }";
     317            0 :       case Kind::Type:
     318            0 :         return "Type: " + type->as_string ();
     319              :       }
     320              : 
     321            0 :     return "";
     322              :   }
     323              : 
     324              :   /**
     325              :    * Disambiguate an ambiguous generic argument to a const generic argument,
     326              :    * unequivocally
     327              :    */
     328              :   GenericArg disambiguate_to_const () const;
     329              : 
     330              :   /**
     331              :    * Disambiguate an ambiguous generic argument to a type argument,
     332              :    * unequivocally
     333              :    */
     334              :   GenericArg disambiguate_to_type () const;
     335              : 
     336              : private:
     337       217938 :   GenericArg (std::unique_ptr<Expr> expression, std::unique_ptr<Type> type,
     338              :               Identifier path, Kind kind, location_t locus)
     339        96840 :     : expression (std::move (expression)), type (std::move (type)),
     340       217938 :       path (std::move (path)), kind (kind), locus (locus)
     341              :   {}
     342              : 
     343              :   /**
     344              :    * Expression associated with a `Clear` const generic application
     345              :    * A null pointer here is allowed in the case that the const argument is
     346              :    * ambiguous.
     347              :    */
     348              :   std::unique_ptr<Expr> expression;
     349              : 
     350              :   /**
     351              :    * If the argument ends up being a type argument instead. A null pointer will
     352              :    * be present here until the resolving phase.
     353              :    */
     354              :   std::unique_ptr<Type> type;
     355              : 
     356              :   /**
     357              :    * Optional path which cannot be differentiated between a constant item and
     358              :    * a type. Only used for ambiguous const generic arguments, otherwise
     359              :    * empty.
     360              :    */
     361              :   Identifier path;
     362              : 
     363              :   /* Which kind of const generic application are we dealing with */
     364              :   Kind kind;
     365              : 
     366              :   location_t locus;
     367              : };
     368              : 
     369              : /**
     370              :  * Representation of const generic parameters
     371              :  */
     372              : class ConstGenericParam : public GenericParam
     373              : {
     374              :   /* Name of the parameter */
     375              :   Identifier name;
     376              : 
     377              :   /* Mandatory type of the const parameter - a null pointer is an error */
     378              :   std::unique_ptr<AST::Type> type;
     379              : 
     380              :   /**
     381              :    * Default value for the const generic parameter
     382              :    */
     383              :   tl::optional<GenericArg> default_value;
     384              : 
     385              :   AST::AttrVec outer_attrs;
     386              :   location_t locus;
     387              : 
     388              : public:
     389          743 :   ConstGenericParam (Identifier name, std::unique_ptr<AST::Type> type,
     390              :                      tl::optional<GenericArg> default_value,
     391              :                      AST::AttrVec outer_attrs, location_t locus)
     392          743 :     : name (name), type (std::move (type)),
     393          743 :       default_value (std::move (default_value)), outer_attrs (outer_attrs),
     394          743 :       locus (locus)
     395          743 :   {}
     396              : 
     397         1809 :   ConstGenericParam (const ConstGenericParam &other)
     398         1809 :     : GenericParam (), name (other.name), type (other.type->clone_type ()),
     399         1809 :       default_value (other.default_value), outer_attrs (other.outer_attrs),
     400         1809 :       locus (other.locus)
     401         1809 :   {}
     402              : 
     403       116510 :   bool has_type () const { return type != nullptr; }
     404       117169 :   bool has_default_value () const { return default_value.has_value (); }
     405              : 
     406        22081 :   const Identifier &get_name () const { return name; }
     407              : 
     408       116522 :   AST::AttrVec &get_outer_attrs () { return outer_attrs; }
     409              : 
     410        94513 :   AST::Type &get_type ()
     411              :   {
     412        94513 :     rust_assert (has_type ());
     413              : 
     414        94513 :     return *type;
     415              :   }
     416              : 
     417        21997 :   std::unique_ptr<AST::Type> &get_type_ptr ()
     418              :   {
     419        21997 :     rust_assert (has_type ());
     420              : 
     421        21997 :     return type;
     422              :   }
     423              : 
     424          402 :   GenericArg &get_default_value_unchecked ()
     425              :   {
     426          402 :     rust_assert (has_default_value ());
     427              : 
     428          402 :     return default_value.value ();
     429              :   }
     430              : 
     431            0 :   const GenericArg &get_default_value_unchecked () const
     432              :   {
     433            0 :     rust_assert (has_default_value ());
     434              : 
     435            0 :     return default_value.value ();
     436              :   }
     437              : 
     438           42 :   tl::optional<GenericArg> &get_default_value () { return default_value; }
     439              : 
     440              :   const tl::optional<GenericArg> &get_default_value () const
     441              :   {
     442              :     return default_value;
     443              :   }
     444              : 
     445              :   std::string as_string () const override;
     446              : 
     447              :   void accept_vis (ASTVisitor &vis) override;
     448              : 
     449        22895 :   location_t get_locus () const override final { return locus; }
     450              : 
     451         2145 :   Kind get_kind () const override final { return Kind::Const; }
     452              : 
     453              : protected:
     454              :   /* Use covariance to implement clone function as returning this object rather
     455              :    * than base */
     456         1809 :   ConstGenericParam *clone_generic_param_impl () const override
     457              :   {
     458         1809 :     return new ConstGenericParam (*this);
     459              :   }
     460              : };
     461              : 
     462              : // Generic arguments allowed in each path expression segment - inline?
     463              : struct GenericArgs
     464              : {
     465              :   std::vector<Lifetime> lifetime_args;
     466              :   std::vector<GenericArg> generic_args;
     467              :   std::vector<GenericArgsBinding> binding_args;
     468              :   location_t locus;
     469              : 
     470              : public:
     471              :   // Returns true if there are any generic arguments
     472    305990341 :   bool has_generic_args () const
     473              :   {
     474    303022562 :     return !(lifetime_args.empty () && generic_args.empty ()
     475    274545376 :              && binding_args.empty ());
     476              :   }
     477              : 
     478      2944745 :   GenericArgs (std::vector<Lifetime> lifetime_args,
     479              :                std::vector<GenericArg> generic_args,
     480              :                std::vector<GenericArgsBinding> binding_args,
     481              :                location_t locus = UNDEF_LOCATION)
     482      2944745 :     : lifetime_args (std::move (lifetime_args)),
     483      2944745 :       generic_args (std::move (generic_args)),
     484      2944745 :       binding_args (std::move (binding_args)), locus (locus)
     485      2944745 :   {}
     486              : 
     487              :   // copy constructor with vector clone
     488      7085551 :   GenericArgs (GenericArgs const &other)
     489      7085551 :     : lifetime_args (other.lifetime_args), binding_args (other.binding_args),
     490      7085551 :       locus (other.locus)
     491              :   {
     492      7085551 :     generic_args.clear ();
     493      7085551 :     generic_args.reserve (other.generic_args.size ());
     494      7901472 :     for (const auto &arg : other.generic_args)
     495       815921 :       generic_args.emplace_back (arg);
     496      7085551 :   }
     497              : 
     498     15168051 :   ~GenericArgs () = default;
     499              : 
     500          680 :   GenericArgs reconstruct () const
     501              :   {
     502          680 :     std::vector<GenericArg> new_args;
     503          680 :     new_args.reserve (generic_args.size ());
     504          709 :     for (const auto &arg : generic_args)
     505           29 :       new_args.push_back (arg.reconstruct ());
     506              : 
     507          680 :     std::vector<GenericArgsBinding> new_bindings;
     508          680 :     new_bindings.reserve (binding_args.size ());
     509          680 :     for (const auto &binding : binding_args)
     510            0 :       new_bindings.push_back (binding.reconstruct ());
     511              : 
     512              :     // Lifetimes are values, so they can be copied directly
     513          680 :     return GenericArgs (lifetime_args, std::move (new_args),
     514          680 :                         std::move (new_bindings), locus);
     515          680 :   }
     516              : 
     517              :   // overloaded assignment operator to vector clone
     518              :   GenericArgs &operator= (GenericArgs const &other)
     519              :   {
     520              :     lifetime_args = other.lifetime_args;
     521              :     binding_args = other.binding_args;
     522              :     locus = other.locus;
     523              : 
     524              :     generic_args.clear ();
     525              :     generic_args.reserve (other.generic_args.size ());
     526              :     for (const auto &arg : other.generic_args)
     527              :       generic_args.emplace_back (arg);
     528              : 
     529              :     return *this;
     530              :   }
     531              : 
     532              :   // move constructors
     533      6203939 :   GenericArgs (GenericArgs &&other) = default;
     534              :   GenericArgs &operator= (GenericArgs &&other) = default;
     535              : 
     536              :   // Creates an empty GenericArgs (no arguments)
     537      2798961 :   static GenericArgs create_empty () { return GenericArgs ({}, {}, {}); }
     538              : 
     539              :   std::string as_string () const;
     540              : 
     541     31099883 :   std::vector<GenericArg> &get_generic_args () { return generic_args; }
     542              : 
     543     31099912 :   std::vector<GenericArgsBinding> &get_binding_args () { return binding_args; }
     544              : 
     545              :   const std::vector<GenericArgsBinding> &get_binding_args () const
     546              :   {
     547              :     return binding_args;
     548              :   }
     549              : 
     550     12783646 :   std::vector<Lifetime> &get_lifetime_args () { return lifetime_args; };
     551              : 
     552              :   const std::vector<Lifetime> &get_lifetime_args () const
     553              :   {
     554              :     return lifetime_args;
     555              :   };
     556              : 
     557       126436 :   location_t get_locus () const { return locus; }
     558              : };
     559              : 
     560              : /* A segment of a path in expression, including an identifier aspect and maybe
     561              :  * generic args */
     562      9788386 : class PathExprSegment
     563              : { // or should this extend PathIdentSegment?
     564              : private:
     565              :   PathIdentSegment segment_name;
     566              :   GenericArgs generic_args;
     567              :   location_t locus;
     568              :   NodeId node_id;
     569              : 
     570              : public:
     571              :   // Returns true if there are any generic arguments
     572    392695441 :   bool has_generic_args () const { return generic_args.has_generic_args (); }
     573              : 
     574              :   // Constructor for segment (from IdentSegment and GenericArgs)
     575      2804675 :   PathExprSegment (PathIdentSegment segment_name, location_t locus,
     576              :                    GenericArgs generic_args = GenericArgs::create_empty ())
     577      2804675 :     : segment_name (std::move (segment_name)),
     578      2804675 :       generic_args (std::move (generic_args)), locus (locus),
     579      2804675 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     580      2804675 :   {}
     581              : 
     582              :   /* Constructor for segment with generic arguments (from segment name and all
     583              :    * args) */
     584         3983 :   PathExprSegment (std::string segment_name, location_t locus,
     585              :                    std::vector<Lifetime> lifetime_args = {},
     586              :                    std::vector<GenericArg> generic_args = {},
     587              :                    std::vector<GenericArgsBinding> binding_args = {})
     588         3983 :     : segment_name (PathIdentSegment (std::move (segment_name), locus)),
     589         3983 :       generic_args (GenericArgs (std::move (lifetime_args),
     590              :                                  std::move (generic_args),
     591         3983 :                                  std::move (binding_args))),
     592         3983 :       locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
     593         3983 :   {}
     594              : 
     595              :   // Returns whether path expression segment is in an error state.
     596      3190244 :   bool is_error () const { return segment_name.is_error (); }
     597              : 
     598              :   // Creates an error-state path expression segment.
     599           11 :   static PathExprSegment create_error ()
     600              :   {
     601           11 :     return PathExprSegment (PathIdentSegment::create_error (), UNDEF_LOCATION);
     602              :   }
     603              : 
     604              :   std::string as_string () const;
     605              : 
     606      1373985 :   location_t get_locus () const { return locus; }
     607              : 
     608              :   // TODO: is this better? Or is a "vis_pattern" better?
     609       981162 :   GenericArgs &get_generic_args ()
     610              :   {
     611       981162 :     rust_assert (has_generic_args ());
     612       981162 :     return generic_args;
     613              :   }
     614              : 
     615    162042534 :   PathIdentSegment &get_ident_segment () { return segment_name; }
     616      2856858 :   const PathIdentSegment &get_ident_segment () const { return segment_name; }
     617              : 
     618      1719973 :   NodeId get_node_id () const { return node_id; }
     619              : 
     620          644 :   PathExprSegment reconstruct () const
     621              :   {
     622          644 :     return PathExprSegment (segment_name, locus, generic_args.reconstruct ());
     623              :   }
     624              : 
     625              :   bool is_super_path_seg () const
     626              :   {
     627              :     return !has_generic_args () && get_ident_segment ().is_super_path_seg ();
     628              :   }
     629              : 
     630              :   bool is_crate_path_seg () const
     631              :   {
     632              :     return !has_generic_args () && get_ident_segment ().is_crate_path_seg ();
     633              :   }
     634              : 
     635      2131817 :   bool is_lower_self_seg () const
     636              :   {
     637      4261366 :     return !has_generic_args () && get_ident_segment ().is_lower_self_seg ();
     638              :   }
     639              : };
     640              : 
     641              : // AST node representing a pattern that involves a "path" - abstract base
     642              : // class
     643              : class Path : public Pattern, public RawPath<PathExprSegment>
     644              : {
     645              : public:
     646              :   enum class Kind
     647              :   {
     648              :     LangItem,
     649              :     Regular,
     650              :   };
     651              : 
     652      2479923 :   Path (std::vector<PathExprSegment> segments, bool opening_scope_resolution)
     653              :     : RawPath (std::move (segments), opening_scope_resolution),
     654      2479923 :       lang_item (tl::nullopt), kind (Kind::Regular)
     655      2479923 :   {}
     656              : 
     657        13309 :   Path (LangItem::Kind lang_item)
     658        13309 :     : RawPath ({}, false), lang_item (lang_item), kind (Kind::LangItem)
     659        13309 :   {}
     660              : 
     661              :   // Returns whether path has segments.
     662       750969 :   bool has_segments () const
     663              :   {
     664       750969 :     rust_assert (kind == Kind::Regular);
     665       750969 :     return !is_empty ();
     666              :   }
     667              : 
     668              :   /* Converts path segments to their equivalent SimplePath segments if
     669              :    * possible, and creates a SimplePath from them. */
     670              :   SimplePath convert_to_simple_path (bool with_opening_scope_resolution) const;
     671              : 
     672              :   /* Returns whether the path is a single segment (excluding qualified path
     673              :    * initial as segment). */
     674      3537592 :   bool is_single_segment () const
     675              :   {
     676      3537592 :     rust_assert (kind == Kind::Regular);
     677      3537592 :     return get_segments ().size () == 1;
     678              :   }
     679              : 
     680              :   std::string as_string () const override;
     681              : 
     682    152570960 :   bool is_lang_item () const { return kind == Kind::LangItem; }
     683              : 
     684              :   // TODO: this seems kinda dodgy
     685    155063131 :   std::vector<PathExprSegment> &get_segments ()
     686              :   {
     687    155063131 :     rust_assert (kind == Kind::Regular);
     688    155063131 :     return segments;
     689              :   }
     690      3537594 :   const std::vector<PathExprSegment> &get_segments () const
     691              :   {
     692      3537594 :     rust_assert (kind == Kind::Regular);
     693      3537594 :     return segments;
     694              :   }
     695              : 
     696        26778 :   LangItem::Kind get_lang_item () const
     697              :   {
     698        26778 :     rust_assert (kind == Kind::LangItem);
     699        26778 :     return *lang_item;
     700              :   }
     701              : 
     702       177285 :   Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Path; }
     703              :   Path::Kind get_path_kind () { return kind; }
     704              : 
     705              : protected:
     706              :   tl::optional<LangItem::Kind> lang_item;
     707              : 
     708              :   Path::Kind kind;
     709              : };
     710              : 
     711              : /* AST node representing a path-in-expression pattern (path that allows
     712              :  * generic arguments) */
     713              : class PathInExpression : public Path, public ExprWithoutBlock
     714              : {
     715              :   std::vector<Attribute> outer_attrs;
     716              :   location_t locus;
     717              :   NodeId _node_id;
     718              : 
     719              :   bool marked_for_strip;
     720              : 
     721              : public:
     722              :   std::string as_string () const override;
     723              : 
     724              :   // Constructor
     725      2478979 :   PathInExpression (std::vector<PathExprSegment> path_segments,
     726              :                     std::vector<Attribute> outer_attrs, location_t locus,
     727              :                     bool has_opening_scope_resolution = false)
     728              :     : Path (std::move (path_segments), has_opening_scope_resolution),
     729      2478979 :       outer_attrs (std::move (outer_attrs)), locus (locus),
     730      2478979 :       _node_id (Analysis::Mappings::get ().get_next_node_id ()),
     731      2478979 :       marked_for_strip (false)
     732      2478979 :   {}
     733              : 
     734        13309 :   PathInExpression (LangItem::Kind lang_item,
     735              :                     std::vector<Attribute> outer_attrs, location_t locus)
     736        13309 :     : Path (lang_item), outer_attrs (std::move (outer_attrs)), locus (locus),
     737        13309 :       _node_id (Analysis::Mappings::get ().get_next_node_id ()),
     738        13309 :       marked_for_strip (false)
     739        13309 :   {}
     740              : 
     741              :   // Creates an error state path in expression.
     742           11 :   static PathInExpression create_error ()
     743              :   {
     744           11 :     return PathInExpression (std::vector<PathExprSegment> (), {},
     745           22 :                              UNDEF_LOCATION);
     746              :   }
     747              : 
     748              :   // Returns whether path in expression is in an error state.
     749       324972 :   bool is_error () const { return !has_segments (); }
     750              : 
     751              :   /* Converts PathInExpression to SimplePath if possible (i.e. no generic
     752              :    * arguments). Otherwise returns an empty SimplePath. */
     753       425997 :   SimplePath as_simple_path () const
     754              :   {
     755              :     /* delegate to parent class as can't access segments. however,
     756              :      * QualifiedPathInExpression conversion to simple path wouldn't make
     757              :      * sense, so the method in the parent class should be protected, not
     758              :      * public. Have to pass in opening scope resolution as parent class has no
     759              :      * access to it.
     760              :      */
     761       425997 :     return convert_to_simple_path (has_opening_scope_resolution ());
     762              :   }
     763              : 
     764          322 :   std::unique_ptr<PathInExpression> reconstruct () const
     765              :   {
     766          322 :     std::vector<PathExprSegment> new_segments;
     767          322 :     new_segments.reserve (segments.size ());
     768          966 :     for (const auto &seg : segments)
     769         1288 :       new_segments.push_back (seg.reconstruct ());
     770              : 
     771          322 :     auto *new_path
     772          322 :       = new PathInExpression (std::move (new_segments), outer_attrs, locus,
     773          322 :                               has_opening_scope_resolution ());
     774              : 
     775          322 :     return std::unique_ptr<PathInExpression> (new_path);
     776          322 :   }
     777              : 
     778      3010598 :   location_t get_locus () const override final { return locus; }
     779              : 
     780              :   void accept_vis (ASTVisitor &vis) override;
     781              : 
     782            0 :   void mark_for_strip () override { marked_for_strip = true; }
     783     45422611 :   bool is_marked_for_strip () const override { return marked_for_strip; }
     784              : 
     785     20497190 :   NodeId get_node_id () const override { return _node_id; }
     786              : 
     787              :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
     788    179873011 :   std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }
     789              : 
     790       140081 :   void set_outer_attrs (std::vector<Attribute> new_attrs) override
     791              :   {
     792       140081 :     outer_attrs = std::move (new_attrs);
     793            0 :   }
     794              : 
     795       616304 :   Expr::Kind get_expr_kind () const override
     796              :   {
     797       616304 :     return Expr::Kind::PathInExpression;
     798              :   }
     799              : 
     800              : protected:
     801              :   /* Use covariance to implement clone function as returning this object
     802              :    * rather than base */
     803        21058 :   PathInExpression *clone_pattern_impl () const final override
     804              :   {
     805        21058 :     return clone_path_in_expression_impl ();
     806              :   }
     807              : 
     808              :   /* Use covariance to implement clone function as returning this object
     809              :    * rather than base */
     810            0 :   PathInExpression *clone_expr_without_block_impl () const final override
     811              :   {
     812            0 :     return clone_path_in_expression_impl ();
     813              :   }
     814              : 
     815      4229710 :   /*virtual*/ PathInExpression *clone_path_in_expression_impl () const
     816              :   {
     817      4229710 :     return new PathInExpression (*this);
     818              :   }
     819              : };
     820              : 
     821              : /* Base class for segments used in type paths - not abstract (represents an
     822              :  * ident-only segment) */
     823              : class TypePathSegment
     824              : {
     825              : public:
     826              :   enum SegmentType
     827              :   {
     828              :     REG,
     829              :     GENERIC,
     830              :     FUNCTION
     831              :   };
     832              : 
     833              : private:
     834              :   tl::optional<LangItem::Kind> lang_item;
     835              :   PathIdentSegment ident_segment;
     836              :   location_t locus;
     837              : 
     838              : protected:
     839              :   /* This is protected because it is only really used by derived classes, not
     840              :    * the base. */
     841              :   bool has_separating_scope_resolution;
     842              :   NodeId node_id;
     843              : 
     844              : public:
     845              :   // Clone function implementation - not pure virtual as overrided by
     846              :   // subclasses
     847      2813674 :   virtual TypePathSegment *clone_type_path_segment_impl () const
     848              :   {
     849      2813674 :     return new TypePathSegment (*this);
     850              :   }
     851          764 :   virtual TypePathSegment *reconstruct_impl () const
     852              :   {
     853         1528 :     return new TypePathSegment (lang_item, ident_segment,
     854          764 :                                 has_separating_scope_resolution, locus);
     855              :   }
     856              : 
     857              : public:
     858      2894461 :   virtual ~TypePathSegment () {}
     859              : 
     860          225 :   virtual SegmentType get_type () const { return SegmentType::REG; }
     861              : 
     862              :   // Unique pointer custom clone function
     863      3430828 :   std::unique_ptr<TypePathSegment> clone_type_path_segment () const
     864              :   {
     865      3430828 :     return std::unique_ptr<TypePathSegment> (clone_type_path_segment_impl ());
     866              :   }
     867              :   // Unique pointer custom reconstruct function
     868            1 :   std::unique_ptr<TypePathSegment> reconstruct () const
     869              :   {
     870            1 :     return reconstruct_base (this);
     871              :   }
     872              : 
     873       710735 :   TypePathSegment (PathIdentSegment ident_segment,
     874              :                    bool has_separating_scope_resolution, location_t locus)
     875      1421470 :     : lang_item (tl::nullopt), ident_segment (std::move (ident_segment)),
     876       710735 :       locus (locus),
     877       710735 :       has_separating_scope_resolution (has_separating_scope_resolution),
     878       710735 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     879       710735 :   {}
     880              : 
     881         7230 :   TypePathSegment (LangItem::Kind lang_item, PathIdentSegment ident_segment,
     882              :                    location_t locus)
     883        14460 :     : lang_item (lang_item), ident_segment (ident_segment), locus (locus),
     884         7230 :       has_separating_scope_resolution (false),
     885         7230 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     886         7230 :   {}
     887              : 
     888       122019 :   TypePathSegment (std::string segment_name,
     889              :                    bool has_separating_scope_resolution, location_t locus)
     890       122019 :     : lang_item (tl::nullopt),
     891       122019 :       ident_segment (PathIdentSegment (std::move (segment_name), locus)),
     892       122019 :       locus (locus),
     893       122019 :       has_separating_scope_resolution (has_separating_scope_resolution),
     894       244038 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     895       122019 :   {}
     896              : 
     897              :   // General constructor
     898          764 :   TypePathSegment (tl::optional<LangItem::Kind> lang_item,
     899              :                    PathIdentSegment ident_segment,
     900              :                    bool has_separating_scope_resolution, location_t locus)
     901         1528 :     : lang_item (lang_item), ident_segment (ident_segment), locus (locus),
     902          764 :       has_separating_scope_resolution (has_separating_scope_resolution),
     903          764 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     904          764 :   {}
     905              : 
     906      3624737 :   TypePathSegment (TypePathSegment const &other)
     907      3624737 :     : lang_item (other.lang_item), ident_segment (other.ident_segment),
     908      3624737 :       locus (other.locus),
     909      3624737 :       has_separating_scope_resolution (other.has_separating_scope_resolution),
     910      3624737 :       node_id (other.node_id)
     911      3624737 :   {}
     912              : 
     913              :   TypePathSegment &operator= (TypePathSegment const &other)
     914              :   {
     915              :     ident_segment = other.ident_segment;
     916              :     lang_item = other.lang_item;
     917              :     locus = other.locus;
     918              :     has_separating_scope_resolution = other.has_separating_scope_resolution;
     919              :     node_id = other.node_id;
     920              : 
     921              :     return *this;
     922              :   }
     923              : 
     924              :   TypePathSegment (TypePathSegment &&other) = default;
     925              :   TypePathSegment &operator= (TypePathSegment &&other) = default;
     926              : 
     927        18403 :   virtual std::string as_string () const
     928              :   {
     929        18403 :     if (lang_item.has_value ())
     930            0 :       return LangItem::PrettyString (*lang_item);
     931              : 
     932        18403 :     return ident_segment.as_string ();
     933              :   }
     934              : 
     935              :   /* Returns whether the type path segment is in an error state. May be
     936              :    * virtual in future. */
     937         3801 :   bool is_error () const { return ident_segment.is_error (); }
     938              : 
     939              :   /* Returns whether segment is identifier only (as opposed to generic args or
     940              :    * function). Overridden in derived classes with other segments. */
     941         3774 :   virtual bool is_ident_only () const { return true; }
     942              : 
     943      2935955 :   bool is_lang_item () const { return lang_item.has_value (); }
     944              : 
     945       135553 :   location_t get_locus () const { return locus; }
     946              : 
     947              :   // not pure virtual as class not abstract
     948              :   virtual void accept_vis (ASTVisitor &vis);
     949              : 
     950       742269 :   bool get_separating_scope_resolution () const
     951              :   {
     952       742269 :     return has_separating_scope_resolution;
     953              :   }
     954              : 
     955      3771681 :   PathIdentSegment &get_ident_segment () { return ident_segment; };
     956              : 
     957          185 :   const PathIdentSegment &get_ident_segment () const { return ident_segment; };
     958              : 
     959        29309 :   LangItem::Kind get_lang_item () const
     960              :   {
     961        29309 :     rust_assert (is_lang_item ());
     962        29309 :     return *lang_item;
     963              :   }
     964              : 
     965      1482571 :   NodeId get_node_id () const { return node_id; }
     966              : 
     967              :   bool is_crate_path_seg () const
     968              :   {
     969              :     return get_ident_segment ().is_crate_path_seg ();
     970              :   }
     971              :   bool is_super_path_seg () const
     972              :   {
     973              :     return get_ident_segment ().is_super_path_seg ();
     974              :   }
     975              :   bool is_big_self_seg () const
     976              :   {
     977              :     return get_ident_segment ().is_big_self_seg ();
     978              :   }
     979              :   bool is_lower_self_seg () const
     980              :   {
     981              :     return get_ident_segment ().is_lower_self_seg ();
     982              :   }
     983              : };
     984              : 
     985              : // Segment used in type path with generic args
     986              : class TypePathSegmentGeneric : public TypePathSegment
     987              : {
     988              :   GenericArgs generic_args;
     989              : 
     990              : public:
     991           29 :   SegmentType get_type () const override { return SegmentType::GENERIC; }
     992              : 
     993     57021832 :   bool has_generic_args () const { return generic_args.has_generic_args (); }
     994              : 
     995            0 :   bool is_ident_only () const override { return false; }
     996              : 
     997              :   // Constructor with PathIdentSegment and GenericArgs
     998       129656 :   TypePathSegmentGeneric (PathIdentSegment ident_segment,
     999              :                           bool has_separating_scope_resolution,
    1000              :                           GenericArgs generic_args, location_t locus)
    1001              :     : TypePathSegment (std::move (ident_segment),
    1002              :                        has_separating_scope_resolution, locus),
    1003       129656 :       generic_args (std::move (generic_args))
    1004       129656 :   {}
    1005              : 
    1006          541 :   TypePathSegmentGeneric (LangItem::Kind lang_item,
    1007              :                           PathIdentSegment ident_segment,
    1008              :                           GenericArgs generic_args, location_t locus)
    1009              :     : TypePathSegment (lang_item, ident_segment, locus),
    1010          541 :       generic_args (std::move (generic_args))
    1011          541 :   {}
    1012              : 
    1013              :   // Constructor from segment name and all args
    1014              :   TypePathSegmentGeneric (std::string segment_name,
    1015              :                           bool has_separating_scope_resolution,
    1016              :                           std::vector<Lifetime> lifetime_args,
    1017              :                           std::vector<GenericArg> generic_args,
    1018              :                           std::vector<GenericArgsBinding> binding_args,
    1019              :                           location_t locus)
    1020              :     : TypePathSegment (std::move (segment_name),
    1021              :                        has_separating_scope_resolution, locus),
    1022              :       generic_args (GenericArgs (std::move (lifetime_args),
    1023              :                                  std::move (generic_args),
    1024              :                                  std::move (binding_args)))
    1025              :   {}
    1026              : 
    1027              :   // Copy constructor with vector clone
    1028       797714 :   TypePathSegmentGeneric (TypePathSegmentGeneric const &other)
    1029       797714 :     : TypePathSegment (other), generic_args (other.generic_args)
    1030       797714 :   {}
    1031              : 
    1032              :   // Overloaded assignment operator with vector clone
    1033              :   TypePathSegmentGeneric &operator= (TypePathSegmentGeneric const &other)
    1034              :   {
    1035              :     generic_args = other.generic_args;
    1036              : 
    1037              :     return *this;
    1038              :   }
    1039              : 
    1040              :   // move constructors
    1041              :   TypePathSegmentGeneric (TypePathSegmentGeneric &&other) = default;
    1042              :   TypePathSegmentGeneric &operator= (TypePathSegmentGeneric &&other) = default;
    1043              : 
    1044              :   std::string as_string () const override;
    1045              : 
    1046              :   void accept_vis (ASTVisitor &vis) override;
    1047              : 
    1048              :   // TODO: is this better? Or is a "vis_pattern" better?
    1049     30115377 :   GenericArgs &get_generic_args () { return generic_args; }
    1050              : 
    1051              :   // Use covariance to override base class method
    1052       797714 :   TypePathSegmentGeneric *clone_type_path_segment_impl () const override
    1053              :   {
    1054       797714 :     return new TypePathSegmentGeneric (*this);
    1055              :   }
    1056              : 
    1057           36 :   TypePathSegmentGeneric *reconstruct_impl () const override
    1058              :   {
    1059           36 :     return new TypePathSegmentGeneric (get_ident_segment (),
    1060           36 :                                        has_separating_scope_resolution,
    1061          108 :                                        generic_args.reconstruct (),
    1062           36 :                                        get_locus ());
    1063              :   }
    1064              : };
    1065              : 
    1066              : // A function as represented in a type path
    1067              : struct TypePathFunction
    1068              : {
    1069              : private:
    1070              :   // TODO: remove
    1071              :   /*bool has_inputs;
    1072              :   TypePathFnInputs inputs;*/
    1073              :   // inlined from TypePathFnInputs
    1074              :   std::vector<std::unique_ptr<Type>> inputs;
    1075              : 
    1076              :   // bool has_type;
    1077              :   std::unique_ptr<Type> return_type;
    1078              : 
    1079              :   // FIXME: think of better way to mark as invalid than taking up storage
    1080              :   bool is_invalid;
    1081              : 
    1082              :   location_t locus;
    1083              : 
    1084              : protected:
    1085              :   // Constructor only used to create invalid type path functions.
    1086            0 :   TypePathFunction (bool is_invalid, location_t locus)
    1087            0 :     : is_invalid (is_invalid), locus (locus)
    1088              :   {}
    1089              : 
    1090              : public:
    1091              :   // Returns whether the return type of the function has been specified.
    1092      1410131 :   bool has_return_type () const { return return_type != nullptr; }
    1093              : 
    1094              :   // Returns whether the function has inputs.
    1095           36 :   bool has_inputs () const { return !inputs.empty (); }
    1096              : 
    1097              :   // Returns whether function is in an error state.
    1098      1414293 :   bool is_error () const { return is_invalid; }
    1099              : 
    1100              :   // Creates an error state function.
    1101            0 :   static TypePathFunction create_error ()
    1102              :   {
    1103            0 :     return TypePathFunction (true, UNDEF_LOCATION);
    1104              :   }
    1105              : 
    1106              :   // Constructor
    1107         4163 :   TypePathFunction (std::vector<std::unique_ptr<Type>> inputs, location_t locus,
    1108              :                     std::unique_ptr<Type> type = nullptr)
    1109         4163 :     : inputs (std::move (inputs)), return_type (std::move (type)),
    1110         4163 :       is_invalid (false), locus (locus)
    1111              :   {}
    1112              : 
    1113              :   // Copy constructor with clone
    1114        13349 :   TypePathFunction (TypePathFunction const &other)
    1115        13349 :     : is_invalid (other.is_invalid)
    1116              :   {
    1117              :     // guard to protect from null pointer dereference
    1118        13349 :     if (other.return_type != nullptr)
    1119        12830 :       return_type = other.return_type->clone_type ();
    1120              : 
    1121        13349 :     inputs.reserve (other.inputs.size ());
    1122        31945 :     for (const auto &e : other.inputs)
    1123        18596 :       inputs.push_back (e->clone_type ());
    1124        13349 :   }
    1125              : 
    1126         4163 :   ~TypePathFunction () = default;
    1127              : 
    1128              :   // Overloaded assignment operator to clone type
    1129              :   TypePathFunction &operator= (TypePathFunction const &other)
    1130              :   {
    1131              :     is_invalid = other.is_invalid;
    1132              : 
    1133              :     // guard to protect from null pointer dereference
    1134              :     if (other.return_type != nullptr)
    1135              :       return_type = other.return_type->clone_type ();
    1136              :     else
    1137              :       return_type = nullptr;
    1138              : 
    1139              :     inputs.reserve (other.inputs.size ());
    1140              :     for (const auto &e : other.inputs)
    1141              :       inputs.push_back (e->clone_type ());
    1142              : 
    1143              :     return *this;
    1144              :   }
    1145              : 
    1146              :   // move constructors
    1147         4163 :   TypePathFunction (TypePathFunction &&other) = default;
    1148              :   TypePathFunction &operator= (TypePathFunction &&other) = default;
    1149              : 
    1150              :   std::string as_string () const;
    1151              : 
    1152              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    1153              :   const std::vector<std::unique_ptr<Type>> &get_params () const
    1154              :   {
    1155              :     return inputs;
    1156              :   }
    1157      1410130 :   std::vector<std::unique_ptr<Type>> &get_params () { return inputs; }
    1158              : 
    1159              :   // TODO: is this better? Or is a "vis_pattern" better?
    1160      1086199 :   Type &get_return_type ()
    1161              :   {
    1162      1086199 :     rust_assert (has_return_type ());
    1163      1086199 :     return *return_type;
    1164              :   }
    1165              : 
    1166       277029 :   std::unique_ptr<Type> &get_return_type_ptr ()
    1167              :   {
    1168       277029 :     rust_assert (has_return_type ());
    1169       277029 :     return return_type;
    1170              :   }
    1171              : 
    1172            1 :   TypePathFunction reconstruct () const
    1173              :   {
    1174            1 :     std::vector<std::unique_ptr<Type>> new_inputs;
    1175            1 :     new_inputs.reserve (inputs.size ());
    1176            2 :     for (const auto &e : inputs)
    1177            1 :       new_inputs.push_back (e->reconstruct ());
    1178              : 
    1179            1 :     std::unique_ptr<Type> new_ret = nullptr;
    1180            1 :     if (return_type)
    1181            1 :       new_ret = return_type->reconstruct ();
    1182              : 
    1183            1 :     return TypePathFunction (std::move (new_inputs), locus,
    1184            1 :                              std::move (new_ret));
    1185            1 :   }
    1186              : 
    1187           36 :   location_t get_locus () const { return locus; }
    1188              : };
    1189              : 
    1190              : // Segment used in type path with a function argument
    1191              : class TypePathSegmentFunction : public TypePathSegment
    1192              : {
    1193              :   TypePathFunction function_path;
    1194              : 
    1195              : public:
    1196          505 :   SegmentType get_type () const override { return SegmentType::FUNCTION; }
    1197              : 
    1198              :   // Constructor with PathIdentSegment and TypePathFn
    1199         4163 :   TypePathSegmentFunction (PathIdentSegment ident_segment,
    1200              :                            bool has_separating_scope_resolution,
    1201              :                            TypePathFunction function_path, location_t locus)
    1202              :     : TypePathSegment (std::move (ident_segment),
    1203              :                        has_separating_scope_resolution, locus),
    1204         4163 :       function_path (std::move (function_path))
    1205         4163 :   {}
    1206              : 
    1207              :   // Constructor with segment name and TypePathFn
    1208              :   TypePathSegmentFunction (std::string segment_name,
    1209              :                            bool has_separating_scope_resolution,
    1210              :                            TypePathFunction function_path, location_t locus)
    1211              :     : TypePathSegment (std::move (segment_name),
    1212              :                        has_separating_scope_resolution, locus),
    1213              :       function_path (std::move (function_path))
    1214              :   {}
    1215              : 
    1216              :   std::string as_string () const override;
    1217              : 
    1218           36 :   bool is_ident_only () const override { return false; }
    1219              : 
    1220              :   void accept_vis (ASTVisitor &vis) override;
    1221              : 
    1222              :   // TODO: is this better? Or is a "vis_pattern" better?
    1223      1410131 :   TypePathFunction &get_type_path_function ()
    1224              :   {
    1225      1410131 :     rust_assert (!function_path.is_error ());
    1226      1410131 :     return function_path;
    1227              :   }
    1228              : 
    1229              :   // Use covariance to override base class method
    1230        13349 :   TypePathSegmentFunction *clone_type_path_segment_impl () const override
    1231              :   {
    1232        13349 :     return new TypePathSegmentFunction (*this);
    1233              :   }
    1234              : 
    1235            1 :   TypePathSegmentFunction *reconstruct_impl () const override
    1236              :   {
    1237            1 :     return new TypePathSegmentFunction (get_ident_segment (),
    1238            1 :                                         has_separating_scope_resolution,
    1239            2 :                                         function_path.reconstruct (),
    1240            1 :                                         get_locus ());
    1241              :   }
    1242              : };
    1243              : 
    1244      1213510 : class TypePath : public TypeNoBounds,
    1245              :                  public RawPath<std::unique_ptr<TypePathSegment>>
    1246              : {
    1247              :   location_t locus;
    1248              : 
    1249              : protected:
    1250              :   /* Use covariance to implement clone function as returning this object
    1251              :    * rather than base */
    1252      2462027 :   TypePath *clone_type_no_bounds_impl () const override
    1253              :   {
    1254      2462027 :     return new TypePath (*this);
    1255              :   }
    1256          798 :   TypePath *reconstruct_impl () const override
    1257              :   {
    1258         1596 :     return new TypePath (reconstruct_vec (get_segments ()), locus,
    1259          798 :                          has_opening_scope_resolution ());
    1260              :   }
    1261              : 
    1262              : public:
    1263              :   // Returns whether the TypePath is in an invalid state.
    1264     16281909 :   bool is_error () const { return segments.empty (); }
    1265              : 
    1266              :   // Creates an error state TypePath.
    1267        27352 :   static TypePath create_error ()
    1268              :   {
    1269        27352 :     return TypePath (std::vector<std::unique_ptr<TypePathSegment>> (),
    1270        27352 :                      UNDEF_LOCATION);
    1271              :   }
    1272              : 
    1273              :   // Constructor
    1274       800910 :   TypePath (std::vector<std::unique_ptr<TypePathSegment>> segments,
    1275              :             location_t locus, bool has_opening_scope_resolution = false)
    1276              :     : TypeNoBounds (),
    1277              :       RawPath (std::move (segments), has_opening_scope_resolution),
    1278       800910 :       locus (locus)
    1279       800910 :   {}
    1280              : 
    1281            0 :   TypePath (LangItem::Kind lang_item,
    1282              :             std::vector<std::unique_ptr<TypePathSegment>> segments,
    1283              :             location_t locus, bool has_opening_scope_resolution = false)
    1284              :     : TypeNoBounds (),
    1285              :       RawPath (std::move (segments), has_opening_scope_resolution),
    1286            0 :       locus (locus)
    1287            0 :   {}
    1288              : 
    1289              :   // Copy constructor with vector clone
    1290      3241836 :   TypePath (TypePath const &other)
    1291              :     : TypeNoBounds (other), RawPath ({}, other.has_opening_scope_resolution ()),
    1292      3241836 :       locus (other.locus)
    1293              :   {
    1294      3241836 :     segments.reserve (other.segments.size ());
    1295      6672664 :     for (const auto &e : other.segments)
    1296      3430828 :       segments.push_back (e->clone_type_path_segment ());
    1297      3241836 :   }
    1298              : 
    1299              :   // Overloaded assignment operator with clone
    1300              :   TypePath &operator= (TypePath const &other)
    1301              :   {
    1302              :     TypeNoBounds::operator= (other);
    1303              :     opening_scope_resolution = other.has_opening_scope_resolution ();
    1304              :     locus = other.locus;
    1305              : 
    1306              :     segments.reserve (other.segments.size ());
    1307              :     for (const auto &e : other.segments)
    1308              :       segments.push_back (e->clone_type_path_segment ());
    1309              : 
    1310              :     return *this;
    1311              :   }
    1312              : 
    1313              :   // move constructors
    1314       664455 :   TypePath (TypePath &&other) = default;
    1315        26635 :   TypePath &operator= (TypePath &&other) = default;
    1316              : 
    1317              :   std::string as_string () const override;
    1318              : 
    1319              :   std::string make_debug_string () const;
    1320              : 
    1321              :   /* Converts TypePath to SimplePath if possible (i.e. no generic or function
    1322              :    * arguments). Otherwise returns an empty SimplePath. */
    1323              :   SimplePath as_simple_path () const;
    1324              : 
    1325              :   // Creates a trait bound with a clone of this type path as its only element.
    1326              :   TraitBound *to_trait_bound (bool in_parens) const override;
    1327              : 
    1328      1225411 :   location_t get_locus () const override final { return locus; }
    1329     17350826 :   NodeId get_node_id () const override { return node_id; }
    1330              : 
    1331            0 :   void mark_for_strip () override {}
    1332     32234524 :   bool is_marked_for_strip () const override { return false; }
    1333              : 
    1334              :   void accept_vis (ASTVisitor &vis) override;
    1335              : 
    1336              :   size_t get_num_segments () const { return segments.size (); }
    1337              : 
    1338        10060 :   Type::Kind get_type_kind () const override { return Type::Kind::TypePath; }
    1339              : };
    1340              : 
    1341              : struct QualifiedPathType
    1342              : {
    1343              : private:
    1344              :   std::unique_ptr<Type> type_to_invoke_on;
    1345              :   TypePath trait_path;
    1346              :   location_t locus;
    1347              :   NodeId node_id;
    1348              : 
    1349              : public:
    1350              :   // Constructor
    1351        27141 :   QualifiedPathType (std::unique_ptr<Type> invoke_on_type,
    1352              :                      location_t locus = UNDEF_LOCATION,
    1353              :                      TypePath trait_path = TypePath::create_error ())
    1354        27141 :     : type_to_invoke_on (std::move (invoke_on_type)), trait_path (trait_path),
    1355        27141 :       locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
    1356        27141 :   {}
    1357              : 
    1358              :   // Copy constructor uses custom deep copy for Type to preserve polymorphism
    1359       199547 :   QualifiedPathType (QualifiedPathType const &other)
    1360       199547 :     : trait_path (other.trait_path), locus (other.locus)
    1361              :   {
    1362       199547 :     node_id = other.node_id;
    1363              :     // guard to prevent null dereference
    1364       199547 :     if (other.type_to_invoke_on != nullptr)
    1365       199547 :       type_to_invoke_on = other.type_to_invoke_on->clone_type ();
    1366       199547 :   }
    1367              : 
    1368              :   // default destructor
    1369       280770 :   ~QualifiedPathType () = default;
    1370              : 
    1371              :   // overload assignment operator to use custom clone method
    1372              :   QualifiedPathType &operator= (QualifiedPathType const &other)
    1373              :   {
    1374              :     node_id = other.node_id;
    1375              :     trait_path = other.trait_path;
    1376              :     locus = other.locus;
    1377              : 
    1378              :     // guard to prevent null dereference
    1379              :     if (other.type_to_invoke_on != nullptr)
    1380              :       type_to_invoke_on = other.type_to_invoke_on->clone_type ();
    1381              :     else
    1382              :       type_to_invoke_on = nullptr;
    1383              : 
    1384              :     return *this;
    1385              :   }
    1386              : 
    1387              :   // move constructor
    1388        81223 :   QualifiedPathType (QualifiedPathType &&other) = default;
    1389            0 :   QualifiedPathType &operator= (QualifiedPathType &&other) = default;
    1390              : 
    1391            0 :   QualifiedPathType reconstruct () const
    1392              :   {
    1393            0 :     auto new_type = type_to_invoke_on->reconstruct ();
    1394              : 
    1395              :     // trait_path is stored by value, but reconstruct returns a unique_ptr.
    1396              :     // We must dereference it to pass to the constructor.
    1397              :     // This is safe because the constructor makes its own copy/move.
    1398            0 :     auto new_trait_path_ptr = trait_path.reconstruct ();
    1399            0 :     TypePath *concrete_ptr
    1400            0 :       = static_cast<TypePath *> (new_trait_path_ptr.get ());
    1401              : 
    1402            0 :     return QualifiedPathType (std::move (new_type), locus, *concrete_ptr);
    1403            0 :   }
    1404              : 
    1405              :   // Returns whether the qualified path type has a rebind as clause.
    1406      7941461 :   bool has_as_clause () const { return !trait_path.is_error (); }
    1407              : 
    1408              :   // Returns whether the qualified path type is in an error state.
    1409      8048256 :   bool is_error () const { return type_to_invoke_on == nullptr; }
    1410              : 
    1411              :   // Creates an error state qualified path type.
    1412            0 :   static QualifiedPathType create_error ()
    1413              :   {
    1414            0 :     return QualifiedPathType (nullptr);
    1415              :   }
    1416              : 
    1417              :   std::string as_string () const;
    1418              : 
    1419        28311 :   location_t get_locus () const { return locus; }
    1420              : 
    1421              :   // TODO: is this better? Or is a "vis_pattern" better?
    1422      6220758 :   Type &get_type ()
    1423              :   {
    1424      6220758 :     rust_assert (type_to_invoke_on != nullptr);
    1425      6220758 :     return *type_to_invoke_on;
    1426              :   }
    1427              : 
    1428      1720703 :   std::unique_ptr<Type> &get_type_ptr ()
    1429              :   {
    1430      1720703 :     rust_assert (type_to_invoke_on != nullptr);
    1431      1720703 :     return type_to_invoke_on;
    1432              :   }
    1433              : 
    1434              :   // TODO: is this better? Or is a "vis_pattern" better?
    1435      7680343 :   TypePath &get_as_type_path ()
    1436              :   {
    1437      7680343 :     rust_assert (has_as_clause ());
    1438      7680343 :     return trait_path;
    1439              :   }
    1440              : 
    1441        27052 :   NodeId get_node_id () const { return node_id; }
    1442              : };
    1443              : 
    1444              : /* AST node representing a qualified path-in-expression pattern (path that
    1445              :  * allows specifying trait functions) */
    1446              : class QualifiedPathInExpression : public Path, public ExprWithoutBlock
    1447              : {
    1448              :   std::vector<Attribute> outer_attrs;
    1449              :   QualifiedPathType path_type;
    1450              :   location_t locus;
    1451              :   NodeId _node_id;
    1452              : 
    1453              : public:
    1454              :   std::string as_string () const override;
    1455              : 
    1456          944 :   QualifiedPathInExpression (QualifiedPathType qual_path_type,
    1457              :                              std::vector<PathExprSegment> path_segments,
    1458              :                              std::vector<Attribute> outer_attrs,
    1459              :                              location_t locus)
    1460              :     : Path (std::move (path_segments), false),
    1461          944 :       outer_attrs (std::move (outer_attrs)),
    1462          944 :       path_type (std::move (qual_path_type)), locus (locus),
    1463          944 :       _node_id (Analysis::Mappings::get ().get_next_node_id ())
    1464          944 :   {}
    1465              : 
    1466              :   /* TODO: maybe make a shortcut constructor that has QualifiedPathType
    1467              :    * elements as params */
    1468              : 
    1469              :   // Returns whether qualified path in expression is in an error state.
    1470        79754 :   bool is_error () const { return path_type.is_error (); }
    1471              : 
    1472              :   // Creates an error qualified path in expression.
    1473            0 :   static QualifiedPathInExpression create_error ()
    1474              :   {
    1475            0 :     return QualifiedPathInExpression (QualifiedPathType::create_error (), {},
    1476            0 :                                       {}, UNDEF_LOCATION);
    1477              :   }
    1478              : 
    1479         2420 :   location_t get_locus () const override final { return locus; }
    1480              : 
    1481              :   void accept_vis (ASTVisitor &vis) override;
    1482              : 
    1483              :   // Invalid if path_type is error, so base stripping on that.
    1484            0 :   void mark_for_strip () override
    1485              :   {
    1486            0 :     path_type = QualifiedPathType::create_error ();
    1487            0 :   }
    1488        79754 :   bool is_marked_for_strip () const override { return is_error (); }
    1489              : 
    1490              :   // TODO: is this better? Or is a "vis_pattern" better?
    1491       329685 :   QualifiedPathType &get_qualified_path_type ()
    1492              :   {
    1493       329685 :     rust_assert (!path_type.is_error ());
    1494       329685 :     return path_type;
    1495              :   }
    1496              : 
    1497              :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
    1498       394648 :   std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }
    1499              : 
    1500          844 :   void set_outer_attrs (std::vector<Attribute> new_attrs) override
    1501              :   {
    1502          844 :     outer_attrs = std::move (new_attrs);
    1503            0 :   }
    1504              : 
    1505        28287 :   NodeId get_node_id () const override { return _node_id; }
    1506              : 
    1507          944 :   Expr::Kind get_expr_kind () const override
    1508              :   {
    1509          944 :     return Expr::Kind::QualifiedPathInExpression;
    1510              :   }
    1511              : 
    1512              :   std::unique_ptr<QualifiedPathInExpression> reconstruct () const
    1513              :   {
    1514              :     std::vector<PathExprSegment> new_segments;
    1515              :     new_segments.reserve (segments.size ());
    1516              :     for (const auto &seg : segments)
    1517              :       new_segments.push_back (seg.reconstruct ());
    1518              : 
    1519              :     auto *new_path = new QualifiedPathInExpression (path_type.reconstruct (),
    1520              :                                                     std::move (new_segments),
    1521              :                                                     outer_attrs, locus);
    1522              : 
    1523              :     return std::unique_ptr<QualifiedPathInExpression> (new_path);
    1524              :   }
    1525              : 
    1526              : protected:
    1527              :   /* Use covariance to implement clone function as returning this object
    1528              :    * rather than base */
    1529            0 :   QualifiedPathInExpression *clone_pattern_impl () const final override
    1530              :   {
    1531            0 :     return clone_qual_path_in_expression_impl ();
    1532              :   }
    1533              : 
    1534              :   /* Use covariance to implement clone function as returning this object
    1535              :    * rather than base */
    1536              :   QualifiedPathInExpression *
    1537            0 :   clone_expr_without_block_impl () const final override
    1538              :   {
    1539            0 :     return clone_qual_path_in_expression_impl ();
    1540              :   }
    1541              : 
    1542              :   /*virtual*/ QualifiedPathInExpression *
    1543         5538 :   clone_qual_path_in_expression_impl () const
    1544              :   {
    1545         5538 :     return new QualifiedPathInExpression (*this);
    1546              :   }
    1547              : };
    1548              : 
    1549              : /* Represents a qualified path in a type; used for disambiguating trait
    1550              :  * function calls */
    1551              : class QualifiedPathInType : public TypeNoBounds
    1552              : {
    1553              :   QualifiedPathType path_type;
    1554              :   std::unique_ptr<TypePathSegment> associated_segment;
    1555              :   std::vector<std::unique_ptr<TypePathSegment>> segments;
    1556              :   location_t locus;
    1557              : 
    1558              : protected:
    1559              :   /* Use covariance to implement clone function as returning this object
    1560              :    * rather than base */
    1561       193909 :   QualifiedPathInType *clone_type_no_bounds_impl () const override
    1562              :   {
    1563       193909 :     return new QualifiedPathInType (*this);
    1564              :   }
    1565            0 :   QualifiedPathInType *reconstruct_impl () const override
    1566              :   {
    1567            0 :     return new QualifiedPathInType (path_type.reconstruct (),
    1568            0 :                                     associated_segment->reconstruct (),
    1569            0 :                                     reconstruct_vec (segments), locus);
    1570              :   }
    1571              : 
    1572              : public:
    1573        26197 :   QualifiedPathInType (
    1574              :     QualifiedPathType qual_path_type,
    1575              :     std::unique_ptr<TypePathSegment> associated_segment,
    1576              :     std::vector<std::unique_ptr<TypePathSegment>> path_segments,
    1577              :     location_t locus)
    1578        26197 :     : path_type (std::move (qual_path_type)),
    1579        26197 :       associated_segment (std::move (associated_segment)),
    1580        26197 :       segments (std::move (path_segments)), locus (locus)
    1581        26197 :   {}
    1582              : 
    1583              :   // Copy constructor with vector clone
    1584       193909 :   QualifiedPathInType (QualifiedPathInType const &other)
    1585       193909 :     : path_type (other.path_type), locus (other.locus)
    1586              :   {
    1587       193909 :     auto seg = other.associated_segment->clone_type_path_segment_impl ();
    1588       193909 :     associated_segment = std::unique_ptr<TypePathSegment> (seg);
    1589              : 
    1590       193909 :     segments.reserve (other.segments.size ());
    1591       193909 :     for (const auto &e : other.segments)
    1592            0 :       segments.push_back (e->clone_type_path_segment ());
    1593       193909 :   }
    1594              : 
    1595              :   // Overloaded assignment operator with vector clone
    1596              :   QualifiedPathInType &operator= (QualifiedPathInType const &other)
    1597              :   {
    1598              :     auto seg = other.associated_segment->clone_type_path_segment_impl ();
    1599              :     associated_segment = std::unique_ptr<TypePathSegment> (seg);
    1600              : 
    1601              :     path_type = other.path_type;
    1602              :     locus = other.locus;
    1603              : 
    1604              :     segments.reserve (other.segments.size ());
    1605              :     for (const auto &e : other.segments)
    1606              :       segments.push_back (e->clone_type_path_segment ());
    1607              : 
    1608              :     return *this;
    1609              :   }
    1610              : 
    1611              :   // move constructors
    1612        26197 :   QualifiedPathInType (QualifiedPathInType &&other) = default;
    1613              :   QualifiedPathInType &operator= (QualifiedPathInType &&other) = default;
    1614              : 
    1615              :   // Returns whether qualified path in type is in an error state.
    1616        26197 :   bool is_error () const { return path_type.is_error (); }
    1617              : 
    1618              :   // Creates an error state qualified path in type.
    1619            0 :   static QualifiedPathInType create_error ()
    1620              :   {
    1621            0 :     return QualifiedPathInType (
    1622            0 :       QualifiedPathType::create_error (), nullptr,
    1623            0 :       std::vector<std::unique_ptr<TypePathSegment>> (), UNDEF_LOCATION);
    1624              :   }
    1625              : 
    1626              :   std::string as_string () const override;
    1627              : 
    1628              :   void accept_vis (ASTVisitor &vis) override;
    1629              : 
    1630              :   // TODO: is this better? Or is a "vis_pattern" better?
    1631      7690100 :   QualifiedPathType &get_qualified_path_type ()
    1632              :   {
    1633      7690100 :     rust_assert (!path_type.is_error ());
    1634      7690100 :     return path_type;
    1635              :   }
    1636              : 
    1637              :   std::unique_ptr<TypePathSegment> &get_associated_segment ()
    1638              :   {
    1639      5153134 :     return associated_segment;
    1640              :   }
    1641              : 
    1642              :   // TODO: this seems kinda dodgy
    1643              :   std::vector<std::unique_ptr<TypePathSegment>> &get_segments ()
    1644              :   {
    1645      5972623 :     return segments;
    1646              :   }
    1647              :   const std::vector<std::unique_ptr<TypePathSegment>> &get_segments () const
    1648              :   {
    1649              :     return segments;
    1650              :   }
    1651              : 
    1652        26248 :   location_t get_locus () const override final { return locus; }
    1653              : 
    1654            0 :   Type::Kind get_type_kind () const override
    1655              :   {
    1656            0 :     return Type::Kind::QualifiedPathInType;
    1657              :   }
    1658              : };
    1659              : } // namespace AST
    1660              : } // namespace Rust
    1661              : 
    1662              : #endif
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.