LCOV - code coverage report
Current view: top level - gcc/rust/ast - rust-item.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.0 % 961 913
Test Date: 2026-11-14 16:39:50 Functions: 93.3 % 255 238
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_ITEM_H
      20              : #define RUST_AST_ITEM_H
      21              : 
      22              : #include "rust-ast.h"
      23              : #include "rust-hir-map.h"
      24              : #include "rust-mapping-common.h"
      25              : #include "rust-path.h"
      26              : #include "rust-common.h"
      27              : #include "rust-expr.h"
      28              : 
      29              : namespace Rust {
      30              : namespace AST {
      31              : // forward decls
      32              : class TypePath;
      33              : 
      34              : // TODO: inline?
      35              : /*struct AbiName {
      36              :     std::string abi_name;
      37              :     // Technically is meant to be STRING_LITERAL
      38              : 
      39              :   public:
      40              :     // Returns whether abi name is empty, i.e. doesn't exist.
      41              :     bool is_empty() const {
      42              :         return abi_name.empty();
      43              :     }
      44              : 
      45              :     AbiName(std::string name) : abi_name(std::move(name)) {}
      46              : 
      47              :     // Empty AbiName constructor
      48              :     AbiName() {}
      49              : };*/
      50              : 
      51              : // A type generic parameter (as opposed to a lifetime generic parameter)
      52              : class TypeParam : public GenericParam
      53              : {
      54              :   AST::AttrVec outer_attrs;
      55              :   Identifier type_representation;
      56              :   std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
      57              :   std::unique_ptr<Type> type;
      58              :   location_t locus;
      59              :   bool was_impl_trait;
      60              : 
      61              : public:
      62      2241095 :   Identifier get_type_representation () const { return type_representation; }
      63              : 
      64              :   // Returns whether the type of the type param has been specified.
      65     15938429 :   bool has_type () const { return type != nullptr; }
      66              : 
      67              :   // Returns whether the type param has type param bounds.
      68        79012 :   bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
      69              : 
      70              :   // Returns whether the type param has an outer attribute.
      71            0 :   bool has_outer_attribute () const { return !outer_attrs.empty (); }
      72              : 
      73     11532614 :   AST::AttrVec &get_outer_attrs () { return outer_attrs; }
      74              : 
      75        73933 :   TypeParam (Identifier type_representation, location_t locus = UNDEF_LOCATION,
      76              :              std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds
      77              :              = std::vector<std::unique_ptr<TypeParamBound>> (),
      78              :              std::unique_ptr<Type> type = nullptr,
      79              :              AST::AttrVec outer_attrs = {}, bool was_impl_trait = false)
      80        73933 :     : GenericParam (Analysis::Mappings::get ().get_next_node_id ()),
      81        73933 :       outer_attrs (std::move (outer_attrs)),
      82        73933 :       type_representation (std::move (type_representation)),
      83        73933 :       type_param_bounds (std::move (type_param_bounds)),
      84        73933 :       type (std::move (type)), locus (locus), was_impl_trait (was_impl_trait)
      85        73933 :   {}
      86              : 
      87              :   // Copy constructor uses clone
      88       326868 :   TypeParam (TypeParam const &other)
      89       326868 :     : GenericParam (other.node_id), outer_attrs (other.outer_attrs),
      90       326868 :       type_representation (other.type_representation), locus (other.locus),
      91       653736 :       was_impl_trait (other.was_impl_trait)
      92              :   {
      93              :     // guard to prevent null pointer dereference
      94       326868 :     if (other.type != nullptr)
      95         1796 :       type = other.type->clone_type ();
      96              : 
      97       326868 :     type_param_bounds.reserve (other.type_param_bounds.size ());
      98       409362 :     for (const auto &e : other.type_param_bounds)
      99        82494 :       type_param_bounds.push_back (e->clone_type_param_bound ());
     100       326868 :   }
     101              : 
     102              :   // Overloaded assignment operator to clone
     103              :   TypeParam &operator= (TypeParam const &other)
     104              :   {
     105              :     type_representation = other.type_representation;
     106              :     outer_attrs = other.outer_attrs;
     107              :     locus = other.locus;
     108              :     node_id = other.node_id;
     109              :     was_impl_trait = other.was_impl_trait;
     110              : 
     111              :     // guard to prevent null pointer dereference
     112              :     if (other.type != nullptr)
     113              :       type = other.type->clone_type ();
     114              :     else
     115              :       type = nullptr;
     116              : 
     117              :     type_param_bounds.reserve (other.type_param_bounds.size ());
     118              :     for (const auto &e : other.type_param_bounds)
     119              :       type_param_bounds.push_back (e->clone_type_param_bound ());
     120              : 
     121              :     return *this;
     122              :   }
     123              : 
     124              :   // move constructors
     125              :   TypeParam (TypeParam &&other) = default;
     126              :   TypeParam &operator= (TypeParam &&other) = default;
     127              : 
     128              :   std::string as_string () const override;
     129              : 
     130      2239013 :   location_t get_locus () const override final { return locus; }
     131              : 
     132        67309 :   Kind get_kind () const override final { return Kind::Type; }
     133              : 
     134              :   void accept_vis (ASTVisitor &vis) override;
     135              : 
     136              :   // TODO: is this better? Or is a "vis_block" better?
     137        49315 :   Type &get_type ()
     138              :   {
     139        49315 :     rust_assert (type != nullptr);
     140        49315 :     return *type;
     141              :   }
     142              : 
     143        18006 :   std::unique_ptr<Type> &get_type_ptr ()
     144              :   {
     145        18006 :     rust_assert (type != nullptr);
     146        18006 :     return type;
     147              :   }
     148              : 
     149         1918 :   std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
     150              :   {
     151     13459783 :     return type_param_bounds;
     152              :   }
     153              : 
     154              :   const std::vector<std::unique_ptr<TypeParamBound>> &
     155              :   get_type_param_bounds () const
     156              :   {
     157              :     return type_param_bounds;
     158              :   }
     159              : 
     160        73580 :   bool from_impl_trait () const { return was_impl_trait; }
     161              : 
     162              : protected:
     163              :   // Clone function implementation as virtual method
     164       313260 :   TypeParam *clone_generic_param_impl () const override
     165              :   {
     166       313260 :     return new TypeParam (*this);
     167              :   }
     168              : };
     169              : 
     170              : /* "where" clause item base. Abstract - use LifetimeWhereClauseItem,
     171              :  * TypeBoundWhereClauseItem */
     172        44251 : class WhereClauseItem
     173              : {
     174              : public:
     175              :   virtual ~WhereClauseItem () {}
     176              : 
     177              :   // Unique pointer custom clone function
     178        34952 :   std::unique_ptr<WhereClauseItem> clone_where_clause_item () const
     179              :   {
     180        34952 :     return std::unique_ptr<WhereClauseItem> (clone_where_clause_item_impl ());
     181              :   }
     182              : 
     183              :   virtual std::string as_string () const = 0;
     184              : 
     185              :   virtual void accept_vis (ASTVisitor &vis) = 0;
     186              : 
     187              :   virtual NodeId get_node_id () const = 0;
     188              : 
     189              : protected:
     190              :   // Clone function implementation as pure virtual method
     191              :   virtual WhereClauseItem *clone_where_clause_item_impl () const = 0;
     192              : };
     193              : 
     194              : // A lifetime where clause item
     195              : class LifetimeWhereClauseItem : public WhereClauseItem
     196              : {
     197              :   Lifetime lifetime;
     198              :   std::vector<Lifetime> lifetime_bounds;
     199              :   location_t locus;
     200              :   NodeId node_id;
     201              : 
     202              : public:
     203           10 :   LifetimeWhereClauseItem (Lifetime lifetime,
     204              :                            std::vector<Lifetime> lifetime_bounds,
     205              :                            location_t locus)
     206           10 :     : lifetime (std::move (lifetime)),
     207           10 :       lifetime_bounds (std::move (lifetime_bounds)), locus (locus),
     208           10 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     209           10 :   {}
     210              : 
     211              :   std::string as_string () const override;
     212              : 
     213              :   void accept_vis (ASTVisitor &vis) override;
     214              : 
     215           10 :   NodeId get_node_id () const override final { return node_id; }
     216              : 
     217         1792 :   Lifetime &get_lifetime () { return lifetime; }
     218              : 
     219         1792 :   std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
     220              : 
     221           10 :   location_t get_locus () const { return locus; }
     222              : 
     223              : protected:
     224              :   // Clone function implementation as (not pure) virtual method
     225           18 :   LifetimeWhereClauseItem *clone_where_clause_item_impl () const override
     226              :   {
     227           18 :     return new LifetimeWhereClauseItem (*this);
     228              :   }
     229              : };
     230              : 
     231              : // A type bound where clause item
     232              : class TypeBoundWhereClauseItem : public WhereClauseItem
     233              : {
     234              :   std::vector<LifetimeParam> for_lifetimes;
     235              :   std::unique_ptr<Type> bound_type;
     236              :   std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
     237              :   NodeId node_id;
     238              :   location_t locus;
     239              : 
     240              : public:
     241              :   // Returns whether the item has ForLifetimes
     242          345 :   bool has_for_lifetimes () const { return !for_lifetimes.empty (); }
     243              : 
     244      1751823 :   std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; }
     245              : 
     246              :   // Returns whether the item has type param bounds
     247              :   bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
     248              : 
     249         9307 :   TypeBoundWhereClauseItem (
     250              :     std::vector<LifetimeParam> for_lifetimes, std::unique_ptr<Type> bound_type,
     251              :     std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
     252              :     location_t locus)
     253         9307 :     : for_lifetimes (std::move (for_lifetimes)),
     254         9307 :       bound_type (std::move (bound_type)),
     255         9307 :       type_param_bounds (std::move (type_param_bounds)),
     256         9307 :       node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
     257         9307 :   {}
     258              : 
     259              :   // Copy constructor requires clone
     260        34934 :   TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other)
     261        34934 :     : for_lifetimes (other.for_lifetimes),
     262        34934 :       bound_type (other.bound_type->clone_type ())
     263              :   {
     264        34934 :     node_id = other.node_id;
     265        34934 :     type_param_bounds.reserve (other.type_param_bounds.size ());
     266        71480 :     for (const auto &e : other.type_param_bounds)
     267        36546 :       type_param_bounds.push_back (e->clone_type_param_bound ());
     268        34934 :   }
     269              : 
     270              :   // Overload assignment operator to clone
     271              :   TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other)
     272              :   {
     273              :     node_id = other.node_id;
     274              :     for_lifetimes = other.for_lifetimes;
     275              :     bound_type = other.bound_type->clone_type ();
     276              :     type_param_bounds.reserve (other.type_param_bounds.size ());
     277              :     for (const auto &e : other.type_param_bounds)
     278              :       type_param_bounds.push_back (e->clone_type_param_bound ());
     279              : 
     280              :     return *this;
     281              :   }
     282              : 
     283              :   // move constructors
     284              :   TypeBoundWhereClauseItem (TypeBoundWhereClauseItem &&other) = default;
     285              :   TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem &&other)
     286              :     = default;
     287              : 
     288              :   std::string as_string () const override;
     289              : 
     290              :   void accept_vis (ASTVisitor &vis) override;
     291              : 
     292      1917713 :   Type &get_type ()
     293              :   {
     294      1917713 :     rust_assert (bound_type != nullptr);
     295      1917713 :     return *bound_type;
     296              :   }
     297              : 
     298       606498 :   std::unique_ptr<Type> &get_type_ptr ()
     299              :   {
     300       606498 :     rust_assert (bound_type != nullptr);
     301       606498 :     return bound_type;
     302              :   }
     303              : 
     304              :   // TODO: this mutable getter seems really dodgy. Think up better way.
     305          345 :   std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
     306              :   {
     307      2050785 :     return type_param_bounds;
     308              :   }
     309              : 
     310              :   const std::vector<std::unique_ptr<TypeParamBound>> &
     311              :   get_type_param_bounds () const
     312              :   {
     313              :     return type_param_bounds;
     314              :   }
     315              : 
     316         9286 :   NodeId get_node_id () const override final { return node_id; }
     317              : 
     318         9286 :   location_t get_locus () const { return locus; }
     319              : 
     320              : protected:
     321              :   // Clone function implementation as (not pure) virtual method
     322        34934 :   TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override
     323              :   {
     324        34934 :     return new TypeBoundWhereClauseItem (*this);
     325              :   }
     326              : };
     327              : 
     328              : // A where clause
     329       480601 : class WhereClause
     330              : {
     331              :   std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items;
     332              :   NodeId node_id;
     333              : 
     334              : public:
     335       241465 :   WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items)
     336       241465 :     : where_clause_items (std::move (where_clause_items)),
     337       241465 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     338       241465 :   {}
     339              : 
     340              :   // copy constructor with vector clone
     341      1144355 :   WhereClause (WhereClause const &other)
     342      1144355 :   {
     343      1144355 :     node_id = other.node_id;
     344      1144355 :     where_clause_items.reserve (other.where_clause_items.size ());
     345      1179307 :     for (const auto &e : other.where_clause_items)
     346        34952 :       where_clause_items.push_back (e->clone_where_clause_item ());
     347      1144355 :   }
     348              : 
     349              :   // overloaded assignment operator with vector clone
     350            0 :   WhereClause &operator= (WhereClause const &other)
     351              :   {
     352            0 :     node_id = other.node_id;
     353            0 :     where_clause_items.reserve (other.where_clause_items.size ());
     354            0 :     for (const auto &e : other.where_clause_items)
     355            0 :       where_clause_items.push_back (e->clone_where_clause_item ());
     356              : 
     357            0 :     return *this;
     358              :   }
     359              : 
     360              :   // move constructors
     361       250116 :   WhereClause (WhereClause &&other) = default;
     362              :   WhereClause &operator= (WhereClause &&other) = default;
     363              : 
     364              :   // Creates a WhereClause with no items.
     365       234513 :   static WhereClause create_empty ()
     366              :   {
     367       234513 :     return WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> ());
     368              :   }
     369              : 
     370              :   // Returns whether the WhereClause has no items.
     371     43063164 :   bool is_empty () const { return where_clause_items.empty (); }
     372              : 
     373              :   std::string as_string () const;
     374              : 
     375              :   NodeId get_node_id () const { return node_id; }
     376              : 
     377              :   // TODO: this mutable getter seems kinda dodgy
     378          360 :   std::vector<std::unique_ptr<WhereClauseItem>> &get_items ()
     379              :   {
     380      1680642 :     return where_clause_items;
     381              :   }
     382              :   const std::vector<std::unique_ptr<WhereClauseItem>> &get_items () const
     383              :   {
     384              :     return where_clause_items;
     385              :   }
     386              : };
     387              : 
     388              : // Abstract class Param
     389       259780 : class Param : public Visitable
     390              : {
     391              : public:
     392      1339143 :   Param (std::vector<Attribute> outer_attrs, location_t locus)
     393      1339143 :     : outer_attrs (std::move (outer_attrs)), locus (locus),
     394      1339143 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     395      1339143 :   {}
     396              : 
     397              :   virtual ~Param () = default;
     398              : 
     399       978365 :   std::unique_ptr<Param> clone_param () const
     400              :   {
     401       978365 :     return std::unique_ptr<Param> (clone_param_impl ());
     402              :   }
     403              : 
     404      6854624 :   virtual bool is_variadic () const { return false; }
     405              : 
     406      6580162 :   virtual bool is_self () const { return false; }
     407              : 
     408       132043 :   NodeId get_node_id () const { return node_id; }
     409              : 
     410       191032 :   location_t get_locus () const { return locus; }
     411              : 
     412       478132 :   std::vector<Attribute> get_outer_attrs () const { return outer_attrs; }
     413              : 
     414     13889707 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
     415              : 
     416              :   virtual Param *clone_param_impl () const = 0;
     417              : 
     418              :   virtual std::string as_string () const = 0;
     419              : 
     420              : protected:
     421              :   std::vector<Attribute> outer_attrs;
     422              :   location_t locus;
     423              :   NodeId node_id;
     424              : };
     425              : 
     426              : // A self parameter in a method
     427              : class SelfParam : public Param
     428              : {
     429              :   bool has_ref;
     430              :   bool is_mut;
     431              :   // bool has_lifetime; // only possible if also ref
     432              :   tl::optional<Lifetime> lifetime;
     433              : 
     434              :   // bool has_type; // only possible if not ref
     435              :   std::unique_ptr<Type> type;
     436              : 
     437              :   // Unrestricted constructor used for error state
     438              :   SelfParam (tl::optional<Lifetime> lifetime, bool has_ref, bool is_mut,
     439              :              Type *type)
     440              :     : Param ({}, UNDEF_LOCATION), has_ref (has_ref), is_mut (is_mut),
     441              :       lifetime (std::move (lifetime)), type (type)
     442              :   {}
     443              :   // this is ok as no outside classes can ever call this
     444              : 
     445              :   // TODO: self param can have outer attributes
     446              : 
     447              : public:
     448              :   // Returns whether the self-param has a type field.
     449     20617774 :   bool has_type () const { return type != nullptr; }
     450              : 
     451              :   // Returns whether the self-param has a valid lifetime.
     452     13937820 :   bool has_lifetime () const { return lifetime.has_value (); }
     453              : 
     454              :   // Returns whether the self-param is in an error state.
     455            0 :   bool is_error () const
     456              :   {
     457            0 :     return (has_type () && has_lifetime ()) || (has_lifetime () && !has_ref);
     458              :     // not having either is not an error
     459              :   }
     460              : 
     461              :   // Creates an error state self-param.
     462              :   static SelfParam create_error ()
     463              :   {
     464              :     // cannot have no ref but have a lifetime at the same time
     465              :     return SelfParam (Lifetime (Lifetime::STATIC), false, false, nullptr);
     466              :   }
     467              : 
     468              :   // Type-based self parameter (not ref, no lifetime)
     469        35080 :   SelfParam (std::unique_ptr<Type> type, bool is_mut, location_t locus)
     470        35080 :     : Param ({}, locus), has_ref (false), is_mut (is_mut),
     471        35080 :       lifetime (tl::nullopt), type (std::move (type))
     472        35080 :   {}
     473              : 
     474              :   // Lifetime-based self parameter (is ref, no type)
     475        42324 :   SelfParam (tl::optional<Lifetime> lifetime, bool is_mut, location_t locus)
     476        42324 :     : Param ({}, locus), has_ref (true), is_mut (is_mut),
     477        42324 :       lifetime (std::move (lifetime))
     478        42324 :   {}
     479              : 
     480              :   // Copy constructor requires clone
     481       475411 :   SelfParam (SelfParam const &other)
     482       475411 :     : Param (other.get_outer_attrs (), other.get_locus ()),
     483       950822 :       has_ref (other.has_ref), is_mut (other.is_mut), lifetime (other.lifetime)
     484              :   {
     485       475411 :     node_id = other.node_id;
     486       475411 :     if (other.type != nullptr)
     487          434 :       type = other.type->clone_type ();
     488       475411 :   }
     489              : 
     490              :   // Overload assignment operator to use clone
     491              :   SelfParam &operator= (SelfParam const &other)
     492              :   {
     493              :     is_mut = other.is_mut;
     494              :     has_ref = other.has_ref;
     495              :     lifetime = other.lifetime;
     496              :     locus = other.locus;
     497              :     node_id = other.node_id;
     498              :     outer_attrs = other.outer_attrs;
     499              : 
     500              :     if (other.type != nullptr)
     501              :       type = other.type->clone_type ();
     502              :     else
     503              :       type = nullptr;
     504              : 
     505              :     return *this;
     506              :   }
     507              : 
     508              :   // move constructors
     509              :   SelfParam (SelfParam &&other) = default;
     510              :   SelfParam &operator= (SelfParam &&other) = default;
     511              : 
     512              :   std::string as_string () const override;
     513              : 
     514       629416 :   location_t get_locus () const { return locus; }
     515              : 
     516      4885104 :   bool is_self () const override { return true; }
     517              : 
     518        86554 :   bool get_has_ref () const { return has_ref; };
     519        83102 :   bool get_is_mut () const { return is_mut; }
     520              : 
     521            0 :   Lifetime get_lifetime () const { return lifetime.value (); }
     522      6396325 :   Lifetime &get_lifetime () { return lifetime.value (); }
     523              : 
     524       154005 :   NodeId get_node_id () const { return node_id; }
     525              : 
     526              :   // TODO: is this better? Or is a "vis_block" better?
     527        27936 :   Type &get_type ()
     528              :   {
     529        27936 :     rust_assert (has_type ());
     530        27936 :     return *type;
     531              :   }
     532              : 
     533         7507 :   std::unique_ptr<Type> &get_type_ptr ()
     534              :   {
     535         7507 :     rust_assert (has_type ());
     536         7507 :     return type;
     537              :   }
     538              : 
     539              :   void accept_vis (ASTVisitor &vis) override;
     540              : 
     541       398423 :   SelfParam *clone_param_impl () const override
     542              :   {
     543       398423 :     return new SelfParam (*this);
     544              :   }
     545              : };
     546              : 
     547              : // Qualifiers for function, i.e. const, unsafe, extern etc.
     548       439497 : class FunctionQualifiers
     549              : {
     550              :   Default default_status;
     551              :   Async async_status;
     552              :   Const const_status;
     553              :   Unsafety unsafe_status;
     554              :   bool has_extern;
     555              :   std::string extern_abi;
     556              :   location_t locus;
     557              : 
     558              : public:
     559       141874 :   FunctionQualifiers (location_t locus, Default default_status,
     560              :                       Async async_status, Const const_status,
     561              :                       Unsafety unsafe_status, bool has_extern = false,
     562              :                       std::string extern_abi = std::string ())
     563       141874 :     : default_status (default_status), async_status (async_status),
     564       141874 :       const_status (const_status), unsafe_status (unsafe_status),
     565       141874 :       has_extern (has_extern), extern_abi (std::move (extern_abi)),
     566       141874 :       locus (locus)
     567              :   {
     568       141874 :     if (!this->extern_abi.empty ())
     569              :       {
     570              :         // having extern is required; not having it is an implementation error
     571         5257 :         rust_assert (has_extern);
     572              :       }
     573       141874 :   }
     574              : 
     575              :   std::string as_string () const;
     576              : 
     577       229436 :   bool is_default () const { return default_status == Default::Yes; }
     578       151899 :   bool is_unsafe () const { return unsafe_status == Unsafety::Unsafe; }
     579       138248 :   bool is_extern () const { return has_extern; }
     580       143959 :   bool is_const () const { return const_status == Const::Yes; }
     581       266553 :   bool is_async () const { return async_status == Async::Yes; }
     582         9408 :   std::string get_extern_abi () const { return extern_abi; }
     583       116960 :   bool has_abi () const { return !extern_abi.empty (); }
     584       116885 :   Const get_const_status () const { return const_status; }
     585       116885 :   Async get_async_status () const { return async_status; }
     586              : 
     587         1639 :   location_t get_locus () const { return locus; }
     588              : };
     589              : 
     590              : class VariadicParam : public Param
     591              : {
     592              :   std::unique_ptr<Pattern> param_name;
     593              : 
     594              : public:
     595           11 :   VariadicParam (std::unique_ptr<Pattern> param_name,
     596              :                  std::vector<Attribute> outer_attrs, location_t locus)
     597              :     : Param (std::move (outer_attrs), std::move (locus)),
     598           11 :       param_name (std::move (param_name))
     599           11 :   {}
     600              : 
     601          894 :   VariadicParam (std::vector<Attribute> outer_attrs, location_t locus)
     602          894 :     : Param (std::move (outer_attrs), std::move (locus)), param_name (nullptr)
     603          894 :   {}
     604              : 
     605         2721 :   VariadicParam (VariadicParam const &other)
     606         2721 :     : Param (other.get_outer_attrs (), other.locus)
     607              :   {
     608         2721 :     if (other.param_name != nullptr)
     609           33 :       param_name = other.param_name->clone_pattern ();
     610         2721 :   }
     611              : 
     612              :   VariadicParam &operator= (VariadicParam const &other)
     613              :   {
     614              :     outer_attrs = other.outer_attrs;
     615              :     locus = other.locus;
     616              :     node_id = other.node_id;
     617              :     if (other.param_name != nullptr)
     618              :       param_name = other.param_name->clone_pattern ();
     619              :     else
     620              :       param_name = nullptr;
     621              : 
     622              :     return *this;
     623              :   }
     624              : 
     625         5536 :   bool is_variadic () const override { return true; }
     626              : 
     627         1816 :   VariadicParam *clone_param_impl () const override
     628              :   {
     629         1816 :     return new VariadicParam (*this);
     630              :   }
     631              : 
     632          185 :   Pattern &get_pattern ()
     633              :   {
     634          185 :     rust_assert (param_name != nullptr);
     635          185 :     return *param_name;
     636              :   }
     637              : 
     638           55 :   std::unique_ptr<Pattern> &get_pattern_ptr ()
     639              :   {
     640           55 :     rust_assert (param_name != nullptr);
     641           55 :     return param_name;
     642              :   }
     643              : 
     644            0 :   const Pattern &get_pattern () const
     645              :   {
     646            0 :     rust_assert (param_name != nullptr);
     647            0 :     return *param_name;
     648              :   }
     649              : 
     650        20533 :   bool has_pattern () const { return param_name != nullptr; }
     651              : 
     652              :   void accept_vis (ASTVisitor &vis) override;
     653              : 
     654              :   std::string as_string () const override;
     655              : };
     656              : 
     657              : // A function parameter
     658              : class FunctionParam : public Param
     659              : {
     660              :   std::unique_ptr<Pattern> param_name;
     661              :   std::unique_ptr<Type> type;
     662              : 
     663              : public:
     664       133117 :   FunctionParam (std::unique_ptr<Pattern> param_name,
     665              :                  std::unique_ptr<Type> param_type,
     666              :                  std::vector<Attribute> outer_attrs, location_t locus)
     667              :     : Param (std::move (outer_attrs), locus),
     668       133117 :       param_name (std::move (param_name)), type (std::move (param_type))
     669       133117 :   {}
     670              : 
     671              :   // Copy constructor uses clone
     672       649585 :   FunctionParam (FunctionParam const &other)
     673       649585 :     : Param (other.get_outer_attrs (), other.locus)
     674              :   {
     675              :     // guard to prevent nullptr dereference
     676       649585 :     if (other.param_name != nullptr)
     677       649585 :       param_name = other.param_name->clone_pattern ();
     678       649585 :     if (other.type != nullptr)
     679       649585 :       type = other.type->clone_type ();
     680       649585 :   }
     681              : 
     682              :   // Overload assignment operator to use clone
     683              :   FunctionParam &operator= (FunctionParam const &other)
     684              :   {
     685              :     locus = other.locus;
     686              :     node_id = other.node_id;
     687              : 
     688              :     // guard to prevent nullptr dereference
     689              :     if (other.param_name != nullptr)
     690              :       param_name = other.param_name->clone_pattern ();
     691              :     else
     692              :       param_name = nullptr;
     693              :     if (other.type != nullptr)
     694              :       type = other.type->clone_type ();
     695              :     else
     696              :       type = nullptr;
     697              : 
     698              :     return *this;
     699              :   }
     700              : 
     701              :   // move constructors
     702       129890 :   FunctionParam (FunctionParam &&other) = default;
     703              :   FunctionParam &operator= (FunctionParam &&other) = default;
     704              : 
     705              :   // Returns whether FunctionParam is in an invalid state.
     706              :   bool is_error () const { return param_name == nullptr || type == nullptr; }
     707              : 
     708              :   // Creates an error FunctionParam.
     709              :   static FunctionParam create_error ()
     710              :   {
     711              :     return FunctionParam (nullptr, nullptr, {}, UNDEF_LOCATION);
     712              :   }
     713              : 
     714              :   std::string as_string () const override;
     715              : 
     716              :   // TODO: seems kinda dodgy. Think of better way.
     717     29730659 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
     718       649585 :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
     719              : 
     720              :   // TODO: is this better? Or is a "vis_block" better?
     721     25756711 :   Pattern &get_pattern ()
     722              :   {
     723     25756711 :     rust_assert (param_name != nullptr);
     724     25756711 :     return *param_name;
     725              :   }
     726              : 
     727      8204181 :   std::unique_ptr<Pattern> &get_pattern_ptr ()
     728              :   {
     729      8204181 :     rust_assert (param_name != nullptr);
     730      8204181 :     return param_name;
     731              :   }
     732              : 
     733     23481847 :   bool has_name () const { return param_name != nullptr; }
     734              : 
     735              :   // TODO: is this better? Or is a "vis_block" better?
     736     25828248 :   Type &get_type ()
     737              :   {
     738     25828248 :     rust_assert (type != nullptr);
     739     25828248 :     return *type;
     740              :   }
     741              : 
     742      8205234 :   std::unique_ptr<Type> &get_type_ptr ()
     743              :   {
     744      8205234 :     rust_assert (type != nullptr);
     745      8205234 :     return type;
     746              :   }
     747              : 
     748       578126 :   FunctionParam *clone_param_impl () const override
     749              :   {
     750       578126 :     return new FunctionParam (*this);
     751              :   }
     752              : 
     753              :   void accept_vis (ASTVisitor &vis) override;
     754              : };
     755              : 
     756              : // Rust module item - abstract base class
     757              : class Module : public VisItem, public GlobContainer
     758              : {
     759              : public:
     760              :   // Type of the current module. A module can be either loaded or unloaded,
     761              :   // meaning that the items of the module can already be present or not. For
     762              :   // example, the following module would be loaded: `mod foo { fn bar() {} }`.
     763              :   // However, the module would be unloaded if it refers to an external file (i.e
     764              :   // `mod foo;`) and then become loaded upon expansion.
     765              :   enum ModuleKind
     766              :   {
     767              :     LOADED,
     768              :     UNLOADED,
     769              :   };
     770              : 
     771       374112 :   Identifier get_name () const { return module_name; }
     772              : 
     773              : private:
     774              :   Identifier module_name;
     775              :   location_t locus;
     776              :   ModuleKind kind;
     777              :   Unsafety safety;
     778              : 
     779              :   // Name of the file including the module
     780              :   std::string outer_filename;
     781              :   // bool has_inner_attrs;
     782              :   std::vector<Attribute> inner_attrs;
     783              :   // bool has_items;
     784              :   std::vector<std::unique_ptr<Item>> items;
     785              :   // Names of including inline modules (immediate parent is last in the list)
     786              :   std::vector<std::string> module_scope;
     787              : 
     788              :   // Filename the module refers to. Empty string on LOADED modules or if an
     789              :   // error occured when dealing with UNLOADED modules
     790              :   std::string module_file;
     791              : 
     792         7768 :   void clone_items (const std::vector<std::unique_ptr<Item>> &other_items)
     793              :   {
     794         7768 :     items.reserve (other_items.size ());
     795       206630 :     for (const auto &e : other_items)
     796       198862 :       items.push_back (e->clone_item ());
     797         7768 :   }
     798              : 
     799              : public:
     800              :   // Returns whether the module has items in its body.
     801              :   bool has_items () const { return !items.empty (); }
     802              : 
     803              :   // Returns whether the module has any inner attributes.
     804              :   bool has_inner_attrs () const { return !inner_attrs.empty (); }
     805              : 
     806              :   // Unloaded module constructor
     807         1805 :   Module (Identifier module_name, Visibility visibility,
     808              :           std::vector<Attribute> outer_attrs, location_t locus, Unsafety safety,
     809              :           std::string outer_filename, std::vector<std::string> module_scope)
     810              :     : VisItem (std::move (visibility), std::move (outer_attrs)),
     811         1805 :       module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED),
     812         1805 :       safety (safety), outer_filename (outer_filename),
     813         1805 :       inner_attrs (std::vector<Attribute> ()),
     814         1805 :       items (std::vector<std::unique_ptr<Item>> ()),
     815         1805 :       module_scope (std::move (module_scope))
     816         1805 :   {}
     817              : 
     818              :   // Loaded module constructor, with items
     819         2274 :   Module (Identifier name, location_t locus,
     820              :           std::vector<std::unique_ptr<Item>> items,
     821              :           Visibility visibility = Visibility::create_private (),
     822              :           Unsafety safety = Unsafety::Normal,
     823              :           std::vector<Attribute> inner_attrs = std::vector<Attribute> (),
     824              :           std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
     825              :     : VisItem (std::move (visibility), std::move (outer_attrs)),
     826         2274 :       module_name (name), locus (locus), kind (ModuleKind::LOADED),
     827         2274 :       safety (safety), outer_filename (std::string ()),
     828         2274 :       inner_attrs (std::move (inner_attrs)), items (std::move (items))
     829         2274 :   {}
     830              : 
     831              :   // Copy constructor with vector clone
     832         8369 :   Module (Module const &other)
     833         8369 :     : VisItem (other), module_name (other.module_name), locus (other.locus),
     834         8369 :       kind (other.kind), safety (other.safety), inner_attrs (other.inner_attrs),
     835         8369 :       module_scope (other.module_scope)
     836              :   {
     837              :     // We need to check whether we are copying a loaded module or an unloaded
     838              :     // one. In the second case, clear the `items` vector.
     839         8369 :     if (other.kind == LOADED)
     840         7768 :       clone_items (other.items);
     841              :     else
     842          601 :       items.clear ();
     843         8369 :   }
     844              : 
     845              :   // Overloaded assignment operator with vector clone
     846              :   Module &operator= (Module const &other)
     847              :   {
     848              :     VisItem::operator= (other);
     849              : 
     850              :     module_name = other.module_name;
     851              :     locus = other.locus;
     852              :     kind = other.kind;
     853              :     inner_attrs = other.inner_attrs;
     854              :     module_scope = other.module_scope;
     855              : 
     856              :     // Likewise, we need to clear the `items` vector in case the other module is
     857              :     // unloaded
     858              :     if (kind == LOADED)
     859              :       clone_items (other.items);
     860              :     else
     861              :       items.clear ();
     862              : 
     863              :     return *this;
     864              :   }
     865              : 
     866              :   // Search for the filename associated with an external module, storing it in
     867              :   // module_file
     868              :   void process_file_path ();
     869              :   // Load the items contained in an external module
     870              :   void load_items ();
     871              : 
     872              :   void accept_vis (ASTVisitor &vis) override;
     873              : 
     874              :   /* Override that runs the function recursively on all items contained within
     875              :    * the module. */
     876              :   void add_crate_name (std::vector<std::string> &names) const override;
     877              : 
     878              :   // Returns the kind of the module
     879        71744 :   enum ModuleKind get_kind () const { return kind; }
     880              : 
     881         3520 :   Unsafety get_unsafety () const { return safety; }
     882              : 
     883              :   // TODO: think of better way to do this - mutable getter seems dodgy
     884              :   const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
     885       388676 :   std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
     886              : 
     887              :   const std::vector<std::unique_ptr<Item>> &get_items () const { return items; }
     888       461006 :   std::vector<std::unique_ptr<Item>> &get_items () { return items; }
     889              : 
     890              :   std::vector<std::unique_ptr<AST::Item>> take_items ()
     891              :   {
     892              :     return std::move (items);
     893              :   }
     894              : 
     895              :   void set_items (std::vector<std::unique_ptr<AST::Item>> &&new_items)
     896              :   {
     897              :     items = std::move (new_items);
     898              :   }
     899              : 
     900              :   // move constructors
     901              :   Module (Module &&other) = default;
     902              :   Module &operator= (Module &&other) = default;
     903              : 
     904              :   std::string as_string () const override;
     905              : 
     906        78568 :   location_t get_locus () const override final { return locus; }
     907              : 
     908              :   // Invalid if name is empty, so base stripping on that.
     909         1096 :   void mark_for_strip () override { module_name = {""}; }
     910        70962 :   bool is_marked_for_strip () const override { return module_name.empty (); }
     911              : 
     912         1774 :   Item::Kind get_item_kind () const override { return Item::Kind::Module; }
     913              : 
     914              : protected:
     915              :   /* Use covariance to implement clone function as returning this object
     916              :    * rather than base */
     917         4849 :   Module *clone_item_impl () const override { return new Module (*this); }
     918              : 
     919       100265 :   GlobContainer::Kind get_glob_container_kind () const override
     920              :   {
     921       100265 :     return GlobContainer::Kind::Module;
     922              :   }
     923              : };
     924              : 
     925              : // Rust extern crate declaration AST node
     926              : class ExternCrate : public VisItem
     927              : {
     928              :   // this is either an identifier or "self", with self parsed to string
     929              :   std::string referenced_crate;
     930              :   // bool has_as_clause;
     931              :   // AsClause as_clause;
     932              :   // this is either an identifier or "_", with _ parsed to string
     933              :   std::string as_clause_name;
     934              : 
     935              :   location_t locus;
     936              : 
     937              :   /* e.g.
     938              :       "extern crate foo as _"
     939              :       "extern crate foo"
     940              :       "extern crate std as cool_std"  */
     941              : public:
     942              :   std::string as_string () const override;
     943              : 
     944              :   // Returns whether extern crate declaration has an as clause.
     945          754 :   bool has_as_clause () const { return !as_clause_name.empty (); }
     946              : 
     947              :   /* Returns whether extern crate declaration references the current crate
     948              :    * (i.e. self). */
     949          229 :   bool references_self () const { return referenced_crate == "self"; }
     950              : 
     951              :   // Constructor
     952          133 :   ExternCrate (std::string referenced_crate, Visibility visibility,
     953              :                std::vector<Attribute> outer_attrs, location_t locus,
     954              :                std::string as_clause_name = std::string ())
     955              :     : VisItem (std::move (visibility), std::move (outer_attrs)),
     956          133 :       referenced_crate (std::move (referenced_crate)),
     957          133 :       as_clause_name (std::move (as_clause_name)), locus (locus)
     958          133 :   {}
     959              : 
     960          489 :   location_t get_locus () const override final { return locus; }
     961              : 
     962              :   void accept_vis (ASTVisitor &vis) override;
     963              : 
     964         1549 :   const std::string &get_referenced_crate () const { return referenced_crate; }
     965           82 :   const std::string &get_as_clause () const { return as_clause_name; }
     966              : 
     967              :   // Override that adds extern crate name in decl to passed list of names.
     968            0 :   void add_crate_name (std::vector<std::string> &names) const override
     969              :   {
     970            0 :     names.push_back (referenced_crate);
     971            0 :   }
     972              : 
     973              :   // Invalid if crate name is empty, so base stripping on that.
     974            2 :   void mark_for_strip () override { referenced_crate = ""; }
     975          177 :   bool is_marked_for_strip () const override
     976              :   {
     977          177 :     return referenced_crate.empty ();
     978              :   }
     979              : 
     980            6 :   Item::Kind get_item_kind () const override { return Item::Kind::ExternCrate; }
     981              : 
     982              : protected:
     983              :   /* Use covariance to implement clone function as returning this object
     984              :    * rather than base */
     985           88 :   ExternCrate *clone_item_impl () const override
     986              :   {
     987           88 :     return new ExternCrate (*this);
     988              :   }
     989              : };
     990              : 
     991              : // The path-ish thing referred to in a use declaration - abstract base class
     992              : class UseTree
     993              : {
     994              :   location_t locus;
     995              :   NodeId node_id;
     996              : 
     997              : public:
     998              :   enum Kind
     999              :   {
    1000              :     Glob,
    1001              :     Rebind,
    1002              :     List,
    1003              :   };
    1004              : 
    1005              :   virtual ~UseTree () {}
    1006              : 
    1007              :   // Overload assignment operator to clone
    1008              :   UseTree &operator= (UseTree const &other)
    1009              :   {
    1010              :     locus = other.locus;
    1011              : 
    1012              :     return *this;
    1013              :   }
    1014              : 
    1015         6036 :   UseTree (const UseTree &other) = default;
    1016              : 
    1017              :   // move constructors
    1018              :   UseTree (UseTree &&other) = default;
    1019              :   UseTree &operator= (UseTree &&other) = default;
    1020              : 
    1021              :   // Unique pointer custom clone function
    1022        38556 :   std::unique_ptr<UseTree> clone_use_tree () const
    1023              :   {
    1024        38556 :     return std::unique_ptr<UseTree> (clone_use_tree_impl ());
    1025              :   }
    1026              : 
    1027              :   virtual std::string as_string () const = 0;
    1028              :   virtual Kind get_kind () const = 0;
    1029              : 
    1030           26 :   location_t get_locus () const { return locus; }
    1031         7622 :   NodeId get_node_id () const { return node_id; }
    1032              : 
    1033              :   virtual void accept_vis (ASTVisitor &vis) = 0;
    1034              : 
    1035              : protected:
    1036              :   // Clone function implementation as pure virtual method
    1037              :   virtual UseTree *clone_use_tree_impl () const = 0;
    1038              : 
    1039        15196 :   UseTree (location_t locus)
    1040        15196 :     : locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
    1041        15196 :   {}
    1042              : };
    1043              : 
    1044              : // Use tree with a glob (wildcard) operator
    1045              : class UseTreeGlob : public UseTree
    1046              : {
    1047              : public:
    1048              :   enum PathType
    1049              :   {
    1050              :     NO_PATH,
    1051              :     GLOBAL,
    1052              :     PATH_PREFIXED
    1053              :   };
    1054              : 
    1055              : private:
    1056              :   PathType glob_type;
    1057              :   SimplePath path;
    1058              : 
    1059              : public:
    1060         1430 :   UseTreeGlob (PathType glob_type, SimplePath path, location_t locus)
    1061         1430 :     : UseTree (locus), glob_type (glob_type), path (std::move (path))
    1062              :   {
    1063         1430 :     if (this->glob_type != PATH_PREFIXED)
    1064              :       {
    1065              :         // compiler implementation error if there is a path with a
    1066              :         // non-path-prefixed use tree glob
    1067            0 :         rust_assert (!has_path ());
    1068              :       }
    1069              :     // TODO: do path-prefixed paths also have to have a path? If so, have an
    1070              :     // assert for that too.
    1071         1430 :   }
    1072              : 
    1073              :   /* Returns whether has path. Should be made redundant by PathType
    1074              :    * PATH_PREFIXED. */
    1075        31439 :   bool has_path () const { return !path.is_empty (); }
    1076              : 
    1077              :   std::string as_string () const override;
    1078              : 
    1079              :   void accept_vis (ASTVisitor &vis) override;
    1080              : 
    1081           37 :   PathType get_glob_type () const { return glob_type; }
    1082              : 
    1083        49846 :   Kind get_kind () const override { return Glob; }
    1084              : 
    1085        31417 :   SimplePath get_path () const
    1086              :   {
    1087        31417 :     rust_assert (has_path ());
    1088        31417 :     return path;
    1089              :   }
    1090              : 
    1091        58782 :   SimplePath &get_path () { return path; }
    1092              : 
    1093              :   /* TODO: find way to ensure only PATH_PREFIXED glob_type has path - factory
    1094              :    * methods? */
    1095              : protected:
    1096              :   /* Use covariance to implement clone function as returning this object
    1097              :    * rather than base */
    1098         3192 :   UseTreeGlob *clone_use_tree_impl () const override
    1099              :   {
    1100         3192 :     return new UseTreeGlob (*this);
    1101              :   }
    1102              : };
    1103              : 
    1104              : // Use tree with a list of paths with a common prefix
    1105              : class UseTreeList : public UseTree
    1106              : {
    1107              : public:
    1108              :   enum PathType
    1109              :   {
    1110              :     NO_PATH,
    1111              :     GLOBAL,
    1112              :     PATH_PREFIXED
    1113              :   };
    1114              : 
    1115              : private:
    1116              :   PathType path_type;
    1117              :   SimplePath path;
    1118              : 
    1119              :   std::vector<std::unique_ptr<UseTree>> trees;
    1120              : 
    1121              : public:
    1122         2309 :   UseTreeList (PathType path_type, SimplePath path,
    1123              :                std::vector<std::unique_ptr<UseTree>> trees, location_t locus)
    1124         2309 :     : UseTree (locus), path_type (path_type), path (std::move (path)),
    1125         2309 :       trees (std::move (trees))
    1126              :   {
    1127         2309 :     if (this->path_type != PATH_PREFIXED)
    1128              :       {
    1129              :         // compiler implementation error if there is a path with a
    1130              :         // non-path-prefixed use tree glob
    1131            0 :         rust_assert (!has_path ());
    1132              :       }
    1133              :     // TODO: do path-prefixed paths also have to have a path? If so, have an
    1134              :     // assert for that too.
    1135         2309 :   }
    1136              : 
    1137              :   // copy constructor with vector clone
    1138         6036 :   UseTreeList (UseTreeList const &other)
    1139         6036 :     : UseTree (other), path_type (other.path_type), path (other.path)
    1140              :   {
    1141         6036 :     trees.reserve (other.trees.size ());
    1142        24960 :     for (const auto &e : other.trees)
    1143        18924 :       trees.push_back (e->clone_use_tree ());
    1144         6036 :   }
    1145              : 
    1146              :   // overloaded assignment operator with vector clone
    1147              :   UseTreeList &operator= (UseTreeList const &other)
    1148              :   {
    1149              :     UseTree::operator= (other);
    1150              :     path_type = other.path_type;
    1151              :     path = other.path;
    1152              : 
    1153              :     trees.reserve (other.trees.size ());
    1154              :     for (const auto &e : other.trees)
    1155              :       trees.push_back (e->clone_use_tree ());
    1156              : 
    1157              :     return *this;
    1158              :   }
    1159              : 
    1160              :   // move constructors
    1161              :   UseTreeList (UseTreeList &&other) = default;
    1162              :   UseTreeList &operator= (UseTreeList &&other) = default;
    1163              : 
    1164              :   // Returns whether has path. Should be made redundant by path_type.
    1165       124065 :   bool has_path () const { return !path.is_empty (); }
    1166              : 
    1167              :   // Returns whether has inner tree elements.
    1168          171 :   bool has_trees () const { return !trees.empty (); }
    1169              : 
    1170              :   std::string as_string () const override;
    1171              : 
    1172          171 :   PathType get_path_type () { return path_type; }
    1173              : 
    1174              :   void accept_vis (ASTVisitor &vis) override;
    1175              : 
    1176       124063 :   Kind get_kind () const override { return List; }
    1177        66599 :   SimplePath get_path () const
    1178              :   {
    1179        66599 :     rust_assert (has_path ());
    1180        66599 :     return path;
    1181              :   }
    1182              : 
    1183       186362 :   SimplePath &get_path () { return path; }
    1184              : 
    1185          175 :   std::vector<std::unique_ptr<UseTree>> &get_trees () { return trees; }
    1186              : 
    1187              :   const std::vector<std::unique_ptr<UseTree>> &get_trees () const
    1188              :   {
    1189        66603 :     return trees;
    1190              :   }
    1191              : 
    1192              :   // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
    1193              :   // methods?
    1194              : protected:
    1195              :   /* Use covariance to implement clone function as returning this object
    1196              :    * rather than base */
    1197         6036 :   UseTreeList *clone_use_tree_impl () const override
    1198              :   {
    1199         6036 :     return new UseTreeList (*this);
    1200              :   }
    1201              : };
    1202              : 
    1203              : // Use tree where it rebinds the module name as something else
    1204              : class UseTreeRebind : public UseTree
    1205              : {
    1206              : public:
    1207              :   enum NewBindType
    1208              :   {
    1209              :     NONE,
    1210              :     IDENTIFIER,
    1211              :     WILDCARD
    1212              :   };
    1213              : 
    1214              : private:
    1215              :   SimplePath path;
    1216              : 
    1217              :   NewBindType bind_type;
    1218              :   Identifier identifier; // only if NewBindType is IDENTIFIER
    1219              : 
    1220              : public:
    1221        11457 :   UseTreeRebind (NewBindType bind_type, SimplePath path, location_t locus,
    1222              :                  Identifier identifier = std::string ())
    1223        11457 :     : UseTree (locus), path (std::move (path)), bind_type (bind_type),
    1224        11457 :       identifier (std::move (identifier))
    1225        11457 :   {}
    1226              : 
    1227              :   // Returns whether has path (this should always be true).
    1228       310605 :   bool has_path () const { return !path.is_empty (); }
    1229              : 
    1230              :   // Returns whether has identifier (or, rather, is allowed to).
    1231         3129 :   bool has_identifier () const { return bind_type == IDENTIFIER; }
    1232              : 
    1233              :   std::string as_string () const override;
    1234              : 
    1235       354728 :   NewBindType get_new_bind_type () const { return bind_type; }
    1236              : 
    1237              :   void accept_vis (ASTVisitor &vis) override;
    1238              : 
    1239       428123 :   Kind get_kind () const override { return Rebind; }
    1240              : 
    1241       310605 :   const SimplePath &get_path () const
    1242              :   {
    1243       310605 :     rust_assert (has_path ());
    1244       310605 :     return path;
    1245              :   }
    1246              : 
    1247       383817 :   SimplePath &get_path () { return path; }
    1248              : 
    1249         3129 :   const Identifier &get_identifier () const
    1250              :   {
    1251         3129 :     rust_assert (has_identifier ());
    1252         3129 :     return identifier;
    1253              :   }
    1254              : 
    1255              :   // TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
    1256              :   // methods?
    1257              : protected:
    1258              :   /* Use covariance to implement clone function as returning this object
    1259              :    * rather than base */
    1260        29328 :   virtual UseTreeRebind *clone_use_tree_impl () const override
    1261              :   {
    1262        29328 :     return new UseTreeRebind (*this);
    1263              :   }
    1264              : };
    1265              : 
    1266              : // Rust use declaration (i.e. for modules) AST node
    1267              : class UseDeclaration : public VisItem
    1268              : {
    1269              :   std::unique_ptr<UseTree> use_tree;
    1270              :   location_t locus;
    1271              : 
    1272              : public:
    1273              :   std::string as_string () const override;
    1274              : 
    1275         8020 :   UseDeclaration (std::unique_ptr<UseTree> use_tree, Visibility visibility,
    1276              :                   std::vector<Attribute> outer_attrs, location_t locus)
    1277              :     : VisItem (std::move (visibility), std::move (outer_attrs)),
    1278         8020 :       use_tree (std::move (use_tree)), locus (locus)
    1279         8020 :   {}
    1280              : 
    1281              :   // Copy constructor with clone
    1282        19632 :   UseDeclaration (UseDeclaration const &other)
    1283        19632 :     : VisItem (other), locus (other.locus)
    1284              :   {
    1285              :     // guard to prevent null dereference (only required if error state)
    1286        19632 :     if (other.use_tree != nullptr)
    1287        19632 :       use_tree = other.use_tree->clone_use_tree ();
    1288        19632 :   }
    1289              : 
    1290              :   // Overloaded assignment operator to clone
    1291              :   UseDeclaration &operator= (UseDeclaration const &other)
    1292              :   {
    1293              :     VisItem::operator= (other);
    1294              :     // visibility = other.visibility->clone_visibility();
    1295              :     // outer_attrs = other.outer_attrs;
    1296              :     locus = other.locus;
    1297              : 
    1298              :     // guard to prevent null dereference (only required if error state)
    1299              :     if (other.use_tree != nullptr)
    1300              :       use_tree = other.use_tree->clone_use_tree ();
    1301              :     else
    1302              :       use_tree = nullptr;
    1303              : 
    1304              :     return *this;
    1305              :   }
    1306              : 
    1307              :   // move constructors
    1308              :   UseDeclaration (UseDeclaration &&other) = default;
    1309              :   UseDeclaration &operator= (UseDeclaration &&other) = default;
    1310              : 
    1311          844 :   location_t get_locus () const override final { return locus; }
    1312              : 
    1313      1139905 :   std::unique_ptr<UseTree> &get_tree () { return use_tree; }
    1314              : 
    1315              :   const std::unique_ptr<UseTree> &get_tree () const { return use_tree; }
    1316              : 
    1317              :   void accept_vis (ASTVisitor &vis) override;
    1318              : 
    1319              :   // Invalid if use tree is null, so base stripping on that.
    1320          373 :   void mark_for_strip () override { use_tree = nullptr; }
    1321       203645 :   bool is_marked_for_strip () const override { return use_tree == nullptr; }
    1322              : 
    1323         3092 :   Item::Kind get_item_kind () const override
    1324              :   {
    1325         3092 :     return Item::Kind::UseDeclaration;
    1326              :   }
    1327              : 
    1328              : protected:
    1329              :   /* Use covariance to implement clone function as returning this object
    1330              :    * rather than base */
    1331        12898 :   UseDeclaration *clone_item_impl () const override
    1332              :   {
    1333        12898 :     return new UseDeclaration (*this);
    1334              :   }
    1335              : };
    1336              : 
    1337              : class LetStmt;
    1338              : 
    1339              : // Rust function declaration AST node
    1340              : class Function : public VisItem, public AssociatedItem, public ExternalItem
    1341              : {
    1342              :   FunctionQualifiers qualifiers;
    1343              :   Identifier function_name;
    1344              :   std::vector<std::unique_ptr<GenericParam>> generic_params;
    1345              :   std::vector<std::unique_ptr<Param>> function_params;
    1346              :   std::unique_ptr<Type> return_type;
    1347              :   WhereClause where_clause;
    1348              :   tl::optional<std::unique_ptr<BlockExpr>> function_body;
    1349              :   location_t locus;
    1350              :   bool is_external_function;
    1351              : 
    1352              : public:
    1353              :   std::string as_string () const override;
    1354              : 
    1355              :   // Returns whether function has generic parameters.
    1356       143503 :   bool has_generics () const { return !generic_params.empty (); }
    1357              : 
    1358              :   // Returns whether function has regular parameters.
    1359       122355 :   bool has_function_params () const { return !function_params.empty (); }
    1360              : 
    1361              :   // Returns whether function has return type - if not, it is void.
    1362     29890455 :   bool has_return_type () const { return return_type != nullptr; }
    1363              : 
    1364              :   // Returns whether function has a where clause.
    1365     23807126 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    1366              : 
    1367       205052 :   bool has_self_param () const
    1368              :   {
    1369       205052 :     return function_params.size () > 0 && function_params[0]->is_self ();
    1370              :   }
    1371              : 
    1372     34142599 :   bool has_body () const { return function_body.has_value (); }
    1373              : 
    1374       106841 :   bool is_default () const { return qualifiers.is_default (); }
    1375              : 
    1376              :   // Mega-constructor with all possible fields
    1377       136770 :   Function (Identifier function_name, FunctionQualifiers qualifiers,
    1378              :             std::vector<std::unique_ptr<GenericParam>> generic_params,
    1379              :             std::vector<std::unique_ptr<Param>> function_params,
    1380              :             std::unique_ptr<Type> return_type, WhereClause where_clause,
    1381              :             tl::optional<std::unique_ptr<BlockExpr>> function_body,
    1382              :             Visibility vis, std::vector<Attribute> outer_attrs,
    1383              :             location_t locus, bool is_external_function = false)
    1384              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    1385       136770 :       ExternalItem (Stmt::node_id), qualifiers (std::move (qualifiers)),
    1386       136770 :       function_name (std::move (function_name)),
    1387       136770 :       generic_params (std::move (generic_params)),
    1388       136770 :       function_params (std::move (function_params)),
    1389       136770 :       return_type (std::move (return_type)),
    1390       136770 :       where_clause (std::move (where_clause)),
    1391       136770 :       function_body (std::move (function_body)), locus (locus),
    1392       136770 :       is_external_function (is_external_function)
    1393       136770 :   {}
    1394              : 
    1395              :   // TODO: add constructor with less fields
    1396              : 
    1397              :   // Copy constructor with clone
    1398              :   Function (Function const &other);
    1399              : 
    1400              :   // Overloaded assignment operator to clone
    1401              :   Function &operator= (Function const &other);
    1402              : 
    1403              :   // move constructors
    1404              :   Function (Function &&other) = default;
    1405              :   Function &operator= (Function &&other) = default;
    1406              : 
    1407      3740169 :   location_t get_locus () const override final { return locus; }
    1408              : 
    1409              :   void accept_vis (ASTVisitor &vis) override;
    1410              : 
    1411        11718 :   bool is_variadic () const
    1412              :   {
    1413        11718 :     return function_params.size () != 0
    1414        11718 :            && function_params.back ()->is_variadic ();
    1415              :   }
    1416              : 
    1417       245190 :   bool is_external () const { return is_external_function; }
    1418              : 
    1419              :   // Invalid if block is null, so base stripping on that.
    1420          181 :   void mark_for_strip () override { function_body = nullptr; }
    1421      3508301 :   bool is_marked_for_strip () const override
    1422              :   {
    1423      3508301 :     return function_body == nullptr;
    1424              :   }
    1425              : 
    1426      9353952 :   std::vector<std::unique_ptr<Param>> &get_function_params ()
    1427              :   {
    1428     30336674 :     return function_params;
    1429              :   }
    1430              :   const std::vector<std::unique_ptr<Param>> &get_function_params () const
    1431              :   {
    1432              :     return function_params;
    1433              :   }
    1434              : 
    1435        14047 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    1436              :   {
    1437     23799892 :     return generic_params;
    1438              :   }
    1439              :   const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
    1440              :   {
    1441              :     return generic_params;
    1442              :   }
    1443              : 
    1444              :   // TODO: is this better? Or is a "vis_block" better?
    1445     30734390 :   tl::optional<std::unique_ptr<BlockExpr>> &get_definition ()
    1446              :   {
    1447     30734390 :     return function_body;
    1448              :   }
    1449              : 
    1450              :   const FunctionQualifiers &get_qualifiers () const { return qualifiers; }
    1451              : 
    1452     20670592 :   FunctionQualifiers &get_qualifiers () { return qualifiers; }
    1453              : 
    1454     10609121 :   Identifier get_function_name () const { return function_name; }
    1455              : 
    1456              :   // TODO: is this better? Or is a "vis_block" better?
    1457       846888 :   WhereClause &get_where_clause () { return where_clause; }
    1458              : 
    1459              :   // TODO: is this better? Or is a "vis_block" better?
    1460     19922736 :   Type &get_return_type ()
    1461              :   {
    1462     19922736 :     rust_assert (has_return_type ());
    1463     19922736 :     return *return_type;
    1464              :   }
    1465              : 
    1466      5961536 :   std::unique_ptr<Type> &get_return_type_ptr ()
    1467              :   {
    1468      5961536 :     rust_assert (has_return_type ());
    1469      5961536 :     return return_type;
    1470              :   }
    1471              : 
    1472        76989 :   Param &get_self_param ()
    1473              :   {
    1474        76989 :     rust_assert (has_self_param ());
    1475        76989 :     return *function_params[0];
    1476              :   }
    1477              :   const Param &get_self_param () const
    1478              :   {
    1479              :     rust_assert (has_self_param ());
    1480              :     return *function_params[0];
    1481              :   }
    1482              : 
    1483              :   // ExternalItem::node_id is same as Stmt::node_id
    1484     25019716 :   NodeId get_node_id () const override { return Stmt::node_id; }
    1485              : 
    1486       866285 :   Item::Kind get_item_kind () const override { return Item::Kind::Function; }
    1487              : 
    1488              : protected:
    1489              :   /* Use covariance to implement clone function as returning this object
    1490              :    * rather than base */
    1491        55694 :   Function *clone_item_impl () const override { return new Function (*this); }
    1492              : 
    1493              :   /* Use covariance to implement clone function as returning this object
    1494              :    * rather than base */
    1495       461375 :   Function *clone_associated_item_impl () const override
    1496              :   {
    1497       461375 :     return new Function (*this);
    1498              :   }
    1499              : 
    1500              :   /* Use covariance to implement clone function as returning this object
    1501              :    * rather than base */
    1502        29558 :   Function *clone_external_item_impl () const override
    1503              :   {
    1504        29558 :     return new Function (*this);
    1505              :   }
    1506              : };
    1507              : 
    1508              : // Rust type alias (i.e. typedef) AST node
    1509              : class TypeAlias : public VisItem, public AssociatedItem
    1510              : {
    1511              :   Identifier new_type_name;
    1512              : 
    1513              :   // bool has_generics;
    1514              :   // Generics generic_params;
    1515              :   std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
    1516              : 
    1517              :   // bool has_where_clause;
    1518              :   WhereClause where_clause;
    1519              : 
    1520              :   std::unique_ptr<Type> existing_type;
    1521              : 
    1522              :   location_t locus;
    1523              : 
    1524              : public:
    1525              :   std::string as_string () const override;
    1526              : 
    1527              :   // Returns whether type alias has generic parameters.
    1528        24328 :   bool has_generics () const { return !generic_params.empty (); }
    1529              : 
    1530              :   // Returns whether type alias has a where clause.
    1531      5044823 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    1532              : 
    1533              :   // Mega-constructor with all possible fields
    1534        22956 :   TypeAlias (Identifier new_type_name,
    1535              :              std::vector<std::unique_ptr<GenericParam>> generic_params,
    1536              :              WhereClause where_clause, std::unique_ptr<Type> existing_type,
    1537              :              Visibility vis, std::vector<Attribute> outer_attrs,
    1538              :              location_t locus)
    1539              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    1540        22956 :       new_type_name (std::move (new_type_name)),
    1541        22956 :       generic_params (std::move (generic_params)),
    1542        22956 :       where_clause (std::move (where_clause)),
    1543        22956 :       existing_type (std::move (existing_type)), locus (locus)
    1544        22956 :   {}
    1545              : 
    1546              :   // Copy constructor
    1547       170873 :   TypeAlias (TypeAlias const &other)
    1548       170873 :     : VisItem (other), new_type_name (other.new_type_name),
    1549       170873 :       where_clause (other.where_clause), locus (other.locus)
    1550              :   {
    1551              :     // guard to prevent null dereference (only required if error state)
    1552       170873 :     if (other.existing_type != nullptr)
    1553       170873 :       existing_type = other.existing_type->clone_type ();
    1554              : 
    1555       170873 :     generic_params.reserve (other.generic_params.size ());
    1556       170947 :     for (const auto &e : other.generic_params)
    1557           74 :       generic_params.push_back (e->clone_generic_param ());
    1558       170873 :   }
    1559              : 
    1560              :   // Overloaded assignment operator to clone
    1561              :   TypeAlias &operator= (TypeAlias const &other)
    1562              :   {
    1563              :     VisItem::operator= (other);
    1564              :     new_type_name = other.new_type_name;
    1565              :     where_clause = other.where_clause;
    1566              :     // visibility = other.visibility->clone_visibility();
    1567              :     // outer_attrs = other.outer_attrs;
    1568              :     locus = other.locus;
    1569              : 
    1570              :     // guard to prevent null dereference (only required if error state)
    1571              :     if (other.existing_type != nullptr)
    1572              :       existing_type = other.existing_type->clone_type ();
    1573              :     else
    1574              :       existing_type = nullptr;
    1575              : 
    1576              :     generic_params.reserve (other.generic_params.size ());
    1577              :     for (const auto &e : other.generic_params)
    1578              :       generic_params.push_back (e->clone_generic_param ());
    1579              : 
    1580              :     return *this;
    1581              :   }
    1582              : 
    1583              :   // move constructors
    1584              :   TypeAlias (TypeAlias &&other) = default;
    1585              :   TypeAlias &operator= (TypeAlias &&other) = default;
    1586              : 
    1587       741752 :   location_t get_locus () const override final { return locus; }
    1588              : 
    1589              :   // needed to override AssociatedItem::get_node_id
    1590      4304474 :   NodeId get_node_id () const override final { return VisItem::get_node_id (); }
    1591              : 
    1592              :   void accept_vis (ASTVisitor &vis) override;
    1593              : 
    1594              :   // Invalid if existing type is null, so base stripping on that.
    1595            0 :   void mark_for_strip () override { existing_type = nullptr; }
    1596       717447 :   bool is_marked_for_strip () const override
    1597              :   {
    1598       717447 :     return existing_type == nullptr;
    1599              :   }
    1600              : 
    1601           48 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    1602              :   {
    1603      5043397 :     return generic_params;
    1604              :   }
    1605              :   const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
    1606              :   {
    1607              :     return generic_params;
    1608              :   }
    1609              : 
    1610              :   // TODO: is this better? Or is a "vis_block" better?
    1611            0 :   WhereClause &get_where_clause () { return where_clause; }
    1612              : 
    1613              :   // TODO: is this better? Or is a "vis_block" better?
    1614      5042613 :   Type &get_type_aliased ()
    1615              :   {
    1616      5042613 :     rust_assert (existing_type != nullptr);
    1617      5042613 :     return *existing_type;
    1618              :   }
    1619              : 
    1620      1411850 :   std::unique_ptr<Type> &get_type_aliased_ptr ()
    1621              :   {
    1622      1411850 :     rust_assert (existing_type != nullptr);
    1623      1411850 :     return existing_type;
    1624              :   }
    1625              : 
    1626      3611195 :   Identifier get_new_type_name () const { return new_type_name; }
    1627              : 
    1628          626 :   Item::Kind get_item_kind () const override { return Item::Kind::TypeAlias; }
    1629              : 
    1630              : protected:
    1631              :   /* Use covariance to implement clone function as returning this object
    1632              :    * rather than base */
    1633          313 :   TypeAlias *clone_item_impl () const override { return new TypeAlias (*this); }
    1634              : 
    1635              :   /* Use covariance to implement clone function as returning this object
    1636              :    * rather than base */
    1637       147619 :   TypeAlias *clone_associated_item_impl () const override
    1638              :   {
    1639       147619 :     return new TypeAlias (*this);
    1640              :   }
    1641              : };
    1642              : 
    1643              : // Rust trait alias AST node
    1644              : // like a type alias, but for traits
    1645              : // unstable but used in core
    1646              : class TraitAlias : public VisItem
    1647              : {
    1648              :   Identifier alias_name;
    1649              : 
    1650              :   // bool has_generics;
    1651              :   // Generics generic_params;
    1652              :   std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
    1653              : 
    1654              :   // bool has_where_clause;
    1655              :   WhereClause where_clause;
    1656              : 
    1657              :   std::vector<std::unique_ptr<TypeParamBound>> alias_bounds;
    1658              : 
    1659              :   location_t locus;
    1660              : 
    1661              :   bool m_mark_for_strip;
    1662              : 
    1663              : public:
    1664              :   std::string as_string () const override;
    1665              : 
    1666              :   // Returns whether type alias has generic parameters.
    1667            0 :   bool has_generics () const { return !generic_params.empty (); }
    1668              : 
    1669              :   // Returns whether type alias has a where clause.
    1670           44 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    1671              : 
    1672              :   // Mega-constructor with all possible fields
    1673            2 :   TraitAlias (Identifier alias_name,
    1674              :               std::vector<std::unique_ptr<GenericParam>> generic_params,
    1675              :               WhereClause where_clause,
    1676              :               std::vector<std::unique_ptr<TypeParamBound>> alias_bounds,
    1677              :               Visibility vis, std::vector<Attribute> outer_attrs,
    1678              :               location_t locus)
    1679              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    1680            4 :       alias_name (std::move (alias_name)),
    1681            2 :       generic_params (std::move (generic_params)),
    1682            2 :       where_clause (std::move (where_clause)),
    1683            2 :       alias_bounds (std::move (alias_bounds)), locus (locus),
    1684            2 :       m_mark_for_strip (false)
    1685            2 :   {}
    1686              : 
    1687              :   // Copy constructor
    1688            2 :   TraitAlias (TraitAlias const &other)
    1689            2 :     : VisItem (other), alias_name (other.alias_name),
    1690            2 :       where_clause (other.where_clause), locus (other.locus)
    1691              :   {
    1692            2 :     generic_params.reserve (other.generic_params.size ());
    1693            2 :     for (const auto &e : other.generic_params)
    1694            0 :       generic_params.push_back (e->clone_generic_param ());
    1695              : 
    1696            2 :     alias_bounds.reserve (other.alias_bounds.size ());
    1697            4 :     for (const auto &b : other.alias_bounds)
    1698            2 :       alias_bounds.push_back (b->clone_type_param_bound ());
    1699            2 :   }
    1700              : 
    1701              :   // Overloaded assignment operator to clone
    1702              :   TraitAlias &operator= (TraitAlias const &other)
    1703              :   {
    1704              :     VisItem::operator= (other);
    1705              :     alias_name = other.alias_name;
    1706              :     where_clause = other.where_clause;
    1707              :     // visibility = other.visibility->clone_visibility();
    1708              :     // outer_attrs = other.outer_attrs;
    1709              :     locus = other.locus;
    1710              : 
    1711              :     generic_params.clear ();
    1712              :     generic_params.reserve (other.generic_params.size ());
    1713              :     for (const auto &e : other.generic_params)
    1714              :       generic_params.push_back (e->clone_generic_param ());
    1715              : 
    1716              :     alias_bounds.clear ();
    1717              :     alias_bounds.reserve (other.alias_bounds.size ());
    1718              :     for (const auto &b : other.alias_bounds)
    1719              :       alias_bounds.push_back (b->clone_type_param_bound ());
    1720              : 
    1721              :     return *this;
    1722              :   }
    1723              : 
    1724              :   // move constructors
    1725              :   TraitAlias (TraitAlias &&other) = default;
    1726              :   TraitAlias &operator= (TraitAlias &&other) = default;
    1727              : 
    1728            8 :   location_t get_locus () const override final { return locus; }
    1729              : 
    1730              :   // needed to override AssociatedItem::get_node_id
    1731           22 :   NodeId get_node_id () const override final { return VisItem::get_node_id (); }
    1732              : 
    1733              :   void accept_vis (ASTVisitor &vis) override;
    1734              : 
    1735            0 :   void mark_for_strip () override { m_mark_for_strip = true; }
    1736            4 :   bool is_marked_for_strip () const override { return m_mark_for_strip; }
    1737              : 
    1738            0 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    1739              :   {
    1740           44 :     return generic_params;
    1741              :   }
    1742              :   const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
    1743              :   {
    1744              :     return generic_params;
    1745              :   }
    1746              : 
    1747              :   // TODO: is this better? Or is a "vis_block" better?
    1748            0 :   WhereClause &get_where_clause () { return where_clause; }
    1749              : 
    1750            0 :   std::vector<std::unique_ptr<TypeParamBound>> &get_alias_bounds ()
    1751              :   {
    1752           44 :     return alias_bounds;
    1753              :   }
    1754              :   const std::vector<std::unique_ptr<TypeParamBound>> &get_alias_bounds () const
    1755              :   {
    1756              :     return alias_bounds;
    1757              :   }
    1758              : 
    1759           30 :   const Identifier &get_alias_name () const { return alias_name; }
    1760              : 
    1761            0 :   Item::Kind get_item_kind () const override { return Item::Kind::TraitAlias; }
    1762              : 
    1763              : protected:
    1764              :   /* Use covariance to implement clone function as returning this object
    1765              :    * rather than base */
    1766            2 :   TraitAlias *clone_item_impl () const override
    1767              :   {
    1768            2 :     return new TraitAlias (*this);
    1769              :   }
    1770              : };
    1771              : 
    1772              : // Rust base struct declaration AST node - abstract base class
    1773              : class Struct : public VisItem
    1774              : {
    1775              : protected:
    1776              :   // protected to enable access by derived classes - allows better as_string
    1777              :   Identifier struct_name;
    1778              : 
    1779              :   // bool has_generics;
    1780              :   // Generics generic_params;
    1781              :   std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
    1782              : 
    1783              :   // bool has_where_clause;
    1784              :   WhereClause where_clause;
    1785              : 
    1786              : private:
    1787              :   location_t locus;
    1788              : 
    1789              : public:
    1790              :   // Returns whether struct has generic parameters.
    1791        11198 :   bool has_generics () const { return !generic_params.empty (); }
    1792              : 
    1793              :   // Returns whether struct has a where clause.
    1794      1952083 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    1795              : 
    1796       352681 :   location_t get_locus () const override final { return locus; }
    1797              : 
    1798              :   // Invalid if name is empty, so base stripping on that.
    1799           86 :   void mark_for_strip () override { struct_name = {""}; }
    1800       492162 :   bool is_marked_for_strip () const override { return struct_name.empty (); }
    1801              : 
    1802      1158326 :   Identifier get_struct_name () const { return struct_name; }
    1803              : 
    1804              :   virtual size_t get_field_count () const = 0;
    1805              : 
    1806         9139 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    1807              :   {
    1808      1958590 :     return generic_params;
    1809              :   }
    1810              :   const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
    1811              :   {
    1812            8 :     return generic_params;
    1813              :   }
    1814              : 
    1815              :   // TODO: is this better? Or is a "vis_block" better?
    1816        21547 :   WhereClause &get_where_clause () { return where_clause; }
    1817              : 
    1818        14788 :   Identifier get_identifier () const { return struct_name; }
    1819              : 
    1820        61400 :   Item::Kind get_item_kind () const override { return Item::Kind::Struct; }
    1821              : 
    1822              : protected:
    1823         9007 :   Struct (Identifier struct_name,
    1824              :           std::vector<std::unique_ptr<GenericParam>> generic_params,
    1825              :           WhereClause where_clause, Visibility vis, location_t locus,
    1826              :           std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
    1827              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    1828        18014 :       struct_name (std::move (struct_name)),
    1829         9007 :       generic_params (std::move (generic_params)),
    1830         9007 :       where_clause (std::move (where_clause)), locus (locus)
    1831         9007 :   {}
    1832              : 
    1833              :   // Copy constructor with vector clone
    1834        40436 :   Struct (Struct const &other)
    1835        40436 :     : VisItem (other), struct_name (other.struct_name),
    1836        40436 :       where_clause (other.where_clause), locus (other.locus)
    1837              :   {
    1838        40436 :     generic_params.reserve (other.generic_params.size ());
    1839        51844 :     for (const auto &e : other.generic_params)
    1840        11408 :       generic_params.push_back (e->clone_generic_param ());
    1841        40436 :   }
    1842              : 
    1843              :   // Overloaded assignment operator with vector clone
    1844              :   Struct &operator= (Struct const &other)
    1845              :   {
    1846              :     VisItem::operator= (other);
    1847              :     struct_name = other.struct_name;
    1848              :     where_clause = other.where_clause;
    1849              :     locus = other.locus;
    1850              : 
    1851              :     generic_params.reserve (other.generic_params.size ());
    1852              :     for (const auto &e : other.generic_params)
    1853              :       generic_params.push_back (e->clone_generic_param ());
    1854              : 
    1855              :     return *this;
    1856              :   }
    1857              : 
    1858              :   // move constructors
    1859              :   Struct (Struct &&other) = default;
    1860              :   Struct &operator= (Struct &&other) = default;
    1861              : };
    1862              : 
    1863              : // A single field in a struct
    1864              : class StructField
    1865              : {
    1866              :   // bool has_outer_attributes;
    1867              :   std::vector<Attribute> outer_attrs;
    1868              : 
    1869              :   // bool has_visibility;
    1870              :   Visibility visibility;
    1871              : 
    1872              :   Identifier field_name;
    1873              :   std::unique_ptr<Type> field_type;
    1874              : 
    1875              :   NodeId node_id;
    1876              : 
    1877              :   location_t locus;
    1878              : 
    1879              : public:
    1880              :   // Returns whether struct field has any outer attributes.
    1881              :   bool has_outer_attributes () const { return !outer_attrs.empty (); }
    1882              : 
    1883              :   // Returns whether struct field has a non-private (non-default) visibility.
    1884              :   bool has_visibility () const { return true; }
    1885              : 
    1886         6311 :   StructField (Identifier field_name, std::unique_ptr<Type> field_type,
    1887              :                Visibility vis, location_t locus,
    1888              :                std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
    1889         6311 :     : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
    1890         6311 :       field_name (std::move (field_name)), field_type (std::move (field_type)),
    1891         6311 :       node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
    1892         6311 :   {}
    1893              : 
    1894              :   // Copy constructor
    1895        16760 :   StructField (StructField const &other)
    1896        16760 :     : outer_attrs (other.outer_attrs), visibility (other.visibility),
    1897        16760 :       field_name (other.field_name), node_id (other.node_id),
    1898        16760 :       locus (other.locus)
    1899              :   {
    1900              :     // guard to prevent null dereference
    1901        16760 :     if (other.field_type != nullptr)
    1902        16760 :       field_type = other.field_type->clone_type ();
    1903        16760 :   }
    1904              : 
    1905        26270 :   ~StructField () = default;
    1906              : 
    1907              :   // Overloaded assignment operator to clone
    1908              :   StructField &operator= (StructField const &other)
    1909              :   {
    1910              :     field_name = other.field_name;
    1911              :     visibility = other.visibility;
    1912              :     outer_attrs = other.outer_attrs;
    1913              :     node_id = other.node_id;
    1914              : 
    1915              :     // guard to prevent null dereference
    1916              :     if (other.field_type != nullptr)
    1917              :       field_type = other.field_type->clone_type ();
    1918              :     else
    1919              :       field_type = nullptr;
    1920              : 
    1921              :     return *this;
    1922              :   }
    1923              : 
    1924              :   // move constructors
    1925         9227 :   StructField (StructField &&other) = default;
    1926           16 :   StructField &operator= (StructField &&other) = default;
    1927              : 
    1928              :   // Returns whether struct field is in an error state.
    1929         5770 :   bool is_error () const
    1930              :   {
    1931         5770 :     return field_name.empty () && field_type == nullptr;
    1932              :     // this should really be an or since neither are allowed
    1933              :   }
    1934              : 
    1935              :   // Creates an error state struct field.
    1936           60 :   static StructField create_error ()
    1937              :   {
    1938          120 :     return StructField (std::string (""), nullptr,
    1939          180 :                         Visibility::create_private (), UNDEF_LOCATION);
    1940              :   }
    1941              : 
    1942              :   std::string as_string () const;
    1943              : 
    1944              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    1945       961130 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
    1946              :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
    1947              : 
    1948        10807 :   Identifier get_field_name () const { return field_name; }
    1949              : 
    1950         8323 :   location_t get_locus () const { return locus; }
    1951              : 
    1952              :   // TODO: is this better? Or is a "vis_block" better?
    1953       731997 :   Type &get_field_type ()
    1954              :   {
    1955       731997 :     rust_assert (field_type != nullptr);
    1956       731997 :     return *field_type;
    1957              :   }
    1958              : 
    1959       235422 :   std::unique_ptr<Type> &get_field_type_ptr ()
    1960              :   {
    1961       235422 :     rust_assert (field_type != nullptr);
    1962       235422 :     return field_type;
    1963              :   }
    1964              : 
    1965       797005 :   Visibility &get_visibility () { return visibility; }
    1966              :   const Visibility &get_visibility () const { return visibility; }
    1967              : 
    1968         5986 :   NodeId get_node_id () const { return node_id; }
    1969              : };
    1970              : 
    1971              : // Rust struct declaration with true struct type AST node
    1972              : class StructStruct : public Struct
    1973              : {
    1974              :   std::vector<StructField> fields;
    1975              :   bool is_unit;
    1976              : 
    1977              : public:
    1978              :   std::string as_string () const override;
    1979              : 
    1980              :   // Mega-constructor with all possible fields
    1981         3216 :   StructStruct (std::vector<StructField> fields, Identifier struct_name,
    1982              :                 std::vector<std::unique_ptr<GenericParam>> generic_params,
    1983              :                 WhereClause where_clause, bool is_unit, Visibility vis,
    1984              :                 std::vector<Attribute> outer_attrs, location_t locus)
    1985              :     : Struct (std::move (struct_name), std::move (generic_params),
    1986              :               std::move (where_clause), std::move (vis), locus,
    1987              :               std::move (outer_attrs)),
    1988         3216 :       fields (std::move (fields)), is_unit (is_unit)
    1989         3216 :   {}
    1990              : 
    1991              :   // Unit struct constructor
    1992         3844 :   StructStruct (Identifier struct_name,
    1993              :                 std::vector<std::unique_ptr<GenericParam>> generic_params,
    1994              :                 WhereClause where_clause, Visibility vis,
    1995              :                 std::vector<Attribute> outer_attrs, location_t locus)
    1996              :     : Struct (std::move (struct_name), std::move (generic_params),
    1997              :               std::move (where_clause), std::move (vis), locus,
    1998              :               std::move (outer_attrs)),
    1999         3844 :       is_unit (true)
    2000         3844 :   {}
    2001              : 
    2002              :   /* Returns whether the struct is a unit struct - struct defined without
    2003              :    * fields. This is important because it also means an implicit constant of its
    2004              :    * type is defined. */
    2005       180444 :   bool is_unit_struct () const { return is_unit; }
    2006              : 
    2007              :   void accept_vis (ASTVisitor &vis) override;
    2008              : 
    2009              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2010      2628867 :   std::vector<StructField> &get_fields () { return fields; }
    2011              :   const std::vector<StructField> &get_fields () const { return fields; }
    2012              : 
    2013            6 :   size_t get_field_count () const override { return fields.size (); }
    2014              : 
    2015              : protected:
    2016              :   /* Use covariance to implement clone function as returning this object
    2017              :    * rather than base */
    2018        26533 :   StructStruct *clone_item_impl () const override
    2019              :   {
    2020        26533 :     return new StructStruct (*this);
    2021              :   }
    2022              : };
    2023              : 
    2024              : // A single field in a tuple
    2025              : class TupleField
    2026              : {
    2027              :   // bool has_outer_attributes;
    2028              :   std::vector<Attribute> outer_attrs;
    2029              : 
    2030              :   // bool has_visibility;
    2031              :   Visibility visibility;
    2032              : 
    2033              :   std::unique_ptr<Type> field_type;
    2034              : 
    2035              :   NodeId node_id;
    2036              : 
    2037              :   location_t locus;
    2038              : 
    2039              : public:
    2040              :   // Returns whether tuple field has outer attributes.
    2041              :   bool has_outer_attributes () const { return !outer_attrs.empty (); }
    2042              : 
    2043              :   /* Returns whether tuple field has a non-default visibility (i.e. a public
    2044              :    * one) */
    2045              :   bool has_visibility () const { return true; }
    2046              : 
    2047              :   // Complete constructor
    2048         6464 :   TupleField (std::unique_ptr<Type> field_type, Visibility vis,
    2049              :               location_t locus,
    2050              :               std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
    2051         6464 :     : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
    2052         6464 :       field_type (std::move (field_type)),
    2053         6464 :       node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
    2054         6464 :   {}
    2055              : 
    2056              :   // Copy constructor with clone
    2057        30762 :   TupleField (TupleField const &other)
    2058        30762 :     : outer_attrs (other.outer_attrs), visibility (other.visibility),
    2059        30762 :       node_id (other.node_id), locus (other.locus)
    2060              :   {
    2061              :     // guard to prevent null dereference (only required if error)
    2062        30762 :     if (other.field_type != nullptr)
    2063        30762 :       field_type = other.field_type->clone_type ();
    2064        30762 :   }
    2065              : 
    2066        41183 :   ~TupleField () = default;
    2067              : 
    2068              :   // Overloaded assignment operator to clone
    2069              :   TupleField &operator= (TupleField const &other)
    2070              :   {
    2071              :     visibility = other.visibility;
    2072              :     outer_attrs = other.outer_attrs;
    2073              :     node_id = other.node_id;
    2074              :     locus = other.locus;
    2075              : 
    2076              :     // guard to prevent null dereference (only required if error)
    2077              :     if (other.field_type != nullptr)
    2078              :       field_type = other.field_type->clone_type ();
    2079              :     else
    2080              :       field_type = nullptr;
    2081              : 
    2082              :     return *this;
    2083              :   }
    2084              : 
    2085              :   // move constructors
    2086        10398 :   TupleField (TupleField &&other) = default;
    2087            0 :   TupleField &operator= (TupleField &&other) = default;
    2088              : 
    2089              :   // Returns whether tuple field is in an error state.
    2090         6464 :   bool is_error () const { return field_type == nullptr; }
    2091              : 
    2092              :   // Creates an error state tuple field.
    2093            1 :   static TupleField create_error ()
    2094              :   {
    2095            1 :     return TupleField (nullptr, Visibility::create_private (), UNDEF_LOCATION);
    2096              :   }
    2097              : 
    2098              :   std::string as_string () const;
    2099              : 
    2100         6374 :   NodeId get_node_id () const { return node_id; }
    2101              : 
    2102       889680 :   Visibility &get_visibility () { return visibility; }
    2103              :   const Visibility &get_visibility () const { return visibility; }
    2104              : 
    2105         6374 :   location_t get_locus () const { return locus; }
    2106              : 
    2107              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2108      1043536 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
    2109              :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
    2110              : 
    2111              :   // TODO: is this better? Or is a "vis_block" better?
    2112       765058 :   Type &get_field_type ()
    2113              :   {
    2114       765058 :     rust_assert (field_type != nullptr);
    2115       765058 :     return *field_type;
    2116              :   }
    2117              : 
    2118       284326 :   std::unique_ptr<Type> &get_field_type_ptr ()
    2119              :   {
    2120       284326 :     rust_assert (field_type != nullptr);
    2121       284326 :     return field_type;
    2122              :   }
    2123              : };
    2124              : 
    2125              : // Rust tuple declared using struct keyword AST node
    2126              : class TupleStruct : public Struct
    2127              : {
    2128              :   std::vector<TupleField> fields;
    2129              : 
    2130              : public:
    2131              :   std::string as_string () const override;
    2132              : 
    2133              :   // Mega-constructor with all possible fields
    2134         1947 :   TupleStruct (std::vector<TupleField> fields, Identifier struct_name,
    2135              :                std::vector<std::unique_ptr<GenericParam>> generic_params,
    2136              :                WhereClause where_clause, Visibility vis,
    2137              :                std::vector<Attribute> outer_attrs, location_t locus)
    2138              :     : Struct (std::move (struct_name), std::move (generic_params),
    2139              :               std::move (where_clause), std::move (vis), locus,
    2140              :               std::move (outer_attrs)),
    2141         1947 :       fields (std::move (fields))
    2142         1947 :   {}
    2143              : 
    2144              :   void accept_vis (ASTVisitor &vis) override;
    2145              : 
    2146              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2147       235014 :   std::vector<TupleField> &get_fields () { return fields; }
    2148              :   const std::vector<TupleField> &get_fields () const { return fields; }
    2149              : 
    2150            2 :   size_t get_field_count () const override { return fields.size (); }
    2151              : 
    2152              : protected:
    2153              :   /* Use covariance to implement clone function as returning this object
    2154              :    * rather than base */
    2155         5212 :   TupleStruct *clone_item_impl () const override
    2156              :   {
    2157         5212 :     return new TupleStruct (*this);
    2158              :   }
    2159              : };
    2160              : 
    2161              : /* An item used in an "enum" tagged union - not abstract: base represents a
    2162              :  * name-only enum. EnumItems (variants) syntactically allow a Visibility
    2163              :  * annotation. */
    2164              : class EnumItem : public VisItem
    2165              : {
    2166              :   Identifier variant_name;
    2167              : 
    2168              :   location_t locus;
    2169              : 
    2170              : public:
    2171              :   enum class Kind
    2172              :   {
    2173              :     Identifier,
    2174              :     Tuple,
    2175              :     Struct,
    2176              : 
    2177              :     // FIXME: In the future, we'll need to remove this possibility as well as
    2178              :     // remove the EnumItemDiscriminant class. The feature for arbitrary
    2179              :     // discriminants on all kinds of variants has been stabilized, and a
    2180              :     // "discriminant" is no longer an enum item variant - it's simply an
    2181              :     // optional part of all variants.
    2182              :     //
    2183              :     // Per the reference:
    2184              :     //
    2185              :     // EnumItem :
    2186              :     //    OuterAttribute* Visibility?
    2187              :     //    IDENTIFIER ( EnumItemTuple | EnumItemStruct )? EnumItemDiscriminant?
    2188              :     //
    2189              :     // EnumItemTuple :
    2190              :     //    ( TupleFields? )
    2191              :     //
    2192              :     // EnumItemStruct :
    2193              :     //    { StructFields? }
    2194              :     //
    2195              :     // EnumItemDiscriminant :
    2196              :     //    = Expression
    2197              :     //
    2198              :     // So we instead need to remove the class, and add an optional expression to
    2199              :     // the base EnumItem class
    2200              :     //
    2201              :     // gccrs#3340
    2202              : 
    2203              :     Discriminant,
    2204              :   };
    2205              : 
    2206         8541 :   virtual ~EnumItem () {}
    2207              : 
    2208         2215 :   EnumItem (Identifier variant_name, Visibility vis,
    2209              :             std::vector<Attribute> outer_attrs, location_t locus)
    2210              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    2211         2215 :       variant_name (std::move (variant_name)), locus (locus)
    2212         2215 :   {}
    2213              : 
    2214         1187 :   virtual Kind get_enum_item_kind () const { return Kind::Identifier; }
    2215              : 
    2216              :   // Unique pointer custom clone function
    2217         5894 :   std::unique_ptr<EnumItem> clone_enum_item () const
    2218              :   {
    2219         5894 :     return std::unique_ptr<EnumItem> (clone_item_impl ());
    2220              :   }
    2221              : 
    2222              :   virtual std::string as_string () const override;
    2223              : 
    2224              :   // not pure virtual as not abstract
    2225              :   virtual void accept_vis (ASTVisitor &vis) override;
    2226              : 
    2227        34219 :   location_t get_locus () const override { return locus; }
    2228              : 
    2229        97366 :   Identifier get_identifier () const { return variant_name; }
    2230              : 
    2231              :   // Based on idea that name is never empty.
    2232            0 :   void mark_for_strip () override { variant_name = {""}; }
    2233        30852 :   bool is_marked_for_strip () const override { return variant_name.empty (); }
    2234              : 
    2235            0 :   Item::Kind get_item_kind () const override { return Item::Kind::EnumItem; }
    2236              : 
    2237              : protected:
    2238         2641 :   EnumItem *clone_item_impl () const override { return new EnumItem (*this); }
    2239              : };
    2240              : 
    2241              : // A tuple item used in an "enum" tagged union
    2242              : class EnumItemTuple : public EnumItem
    2243              : {
    2244              :   // bool has_tuple_fields;
    2245              :   std::vector<TupleField> tuple_fields;
    2246              : 
    2247              : public:
    2248              :   // Returns whether tuple enum item has tuple fields.
    2249            0 :   bool has_tuple_fields () const { return !tuple_fields.empty (); }
    2250              : 
    2251          769 :   EnumItemTuple (Identifier variant_name, Visibility vis,
    2252              :                  std::vector<TupleField> tuple_fields,
    2253              :                  std::vector<Attribute> outer_attrs, location_t locus)
    2254              :     : EnumItem (std::move (variant_name), std::move (vis),
    2255              :                 std::move (outer_attrs), locus),
    2256          769 :       tuple_fields (std::move (tuple_fields))
    2257          769 :   {}
    2258              : 
    2259          712 :   EnumItem::Kind get_enum_item_kind () const override
    2260              :   {
    2261          712 :     return EnumItem::Kind::Tuple;
    2262              :   }
    2263              : 
    2264              :   std::string as_string () const override;
    2265              : 
    2266              :   void accept_vis (ASTVisitor &vis) override;
    2267              : 
    2268              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2269        71051 :   std::vector<TupleField> &get_tuple_fields () { return tuple_fields; }
    2270              :   const std::vector<TupleField> &get_tuple_fields () const
    2271              :   {
    2272              :     return tuple_fields;
    2273              :   }
    2274              : 
    2275              : protected:
    2276              :   // Clone function implementation as (not pure) virtual method
    2277         2025 :   EnumItemTuple *clone_item_impl () const override
    2278              :   {
    2279         2025 :     return new EnumItemTuple (*this);
    2280              :   }
    2281              : };
    2282              : 
    2283              : // A struct item used in an "enum" tagged union
    2284              : class EnumItemStruct : public EnumItem
    2285              : {
    2286              :   // bool has_struct_fields;
    2287              :   std::vector<StructField> struct_fields;
    2288              : 
    2289              : public:
    2290              :   // Returns whether struct enum item has struct fields.
    2291            0 :   bool has_struct_fields () const { return !struct_fields.empty (); }
    2292              : 
    2293          103 :   EnumItemStruct (Identifier variant_name, Visibility vis,
    2294              :                   std::vector<StructField> struct_fields,
    2295              :                   std::vector<Attribute> outer_attrs, location_t locus)
    2296              :     : EnumItem (std::move (variant_name), std::move (vis),
    2297              :                 std::move (outer_attrs), locus),
    2298          103 :       struct_fields (std::move (struct_fields))
    2299          103 :   {}
    2300              : 
    2301           36 :   EnumItem::Kind get_enum_item_kind () const override
    2302              :   {
    2303           36 :     return EnumItem::Kind::Struct;
    2304              :   }
    2305              : 
    2306              :   std::string as_string () const override;
    2307              : 
    2308              :   void accept_vis (ASTVisitor &vis) override;
    2309              : 
    2310              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2311         2802 :   std::vector<StructField> &get_struct_fields () { return struct_fields; }
    2312              :   const std::vector<StructField> &get_struct_fields () const
    2313              :   {
    2314           11 :     return struct_fields;
    2315              :   }
    2316              : 
    2317              : protected:
    2318              :   // Clone function implementation as (not pure) virtual method
    2319          215 :   EnumItemStruct *clone_item_impl () const override
    2320              :   {
    2321          215 :     return new EnumItemStruct (*this);
    2322              :   }
    2323              : };
    2324              : 
    2325              : // A discriminant (numbered enum) item used in an "enum" tagged union
    2326              : class EnumItemDiscriminant : public EnumItem
    2327              : {
    2328              :   std::unique_ptr<Expr> expression;
    2329              : 
    2330              : public:
    2331          320 :   EnumItemDiscriminant (Identifier variant_name, Visibility vis,
    2332              :                         std::unique_ptr<Expr> expr,
    2333              :                         std::vector<Attribute> outer_attrs, location_t locus)
    2334              :     : EnumItem (std::move (variant_name), std::move (vis),
    2335              :                 std::move (outer_attrs), locus),
    2336          320 :       expression (std::move (expr))
    2337          320 :   {}
    2338              : 
    2339              :   // Copy constructor with clone
    2340         1013 :   EnumItemDiscriminant (EnumItemDiscriminant const &other)
    2341         1013 :     : EnumItem (other), expression (other.expression->clone_expr ())
    2342         1013 :   {}
    2343              : 
    2344              :   // Overloaded assignment operator to clone
    2345              :   EnumItemDiscriminant &operator= (EnumItemDiscriminant const &other)
    2346              :   {
    2347              :     EnumItem::operator= (other);
    2348              :     expression = other.expression->clone_expr ();
    2349              :     // variant_name = other.variant_name;
    2350              :     // outer_attrs = other.outer_attrs;
    2351              : 
    2352              :     return *this;
    2353              :   }
    2354              : 
    2355              :   // move constructors
    2356              :   EnumItemDiscriminant (EnumItemDiscriminant &&other) = default;
    2357              :   EnumItemDiscriminant &operator= (EnumItemDiscriminant &&other) = default;
    2358              : 
    2359           81 :   EnumItem::Kind get_enum_item_kind () const override
    2360              :   {
    2361           81 :     return EnumItem::Kind::Discriminant;
    2362              :   }
    2363              : 
    2364              :   std::string as_string () const override;
    2365              : 
    2366              :   void accept_vis (ASTVisitor &vis) override;
    2367              : 
    2368              :   bool has_expr () { return expression != nullptr; }
    2369              : 
    2370              :   // TODO: is this better? Or is a "vis_block" better?
    2371        14138 :   Expr &get_expr ()
    2372              :   {
    2373        14138 :     rust_assert (expression != nullptr);
    2374        14138 :     return *expression;
    2375              :   }
    2376              : 
    2377         3440 :   std::unique_ptr<Expr> &get_expr_ptr ()
    2378              :   {
    2379         3440 :     rust_assert (expression != nullptr);
    2380         3440 :     return expression;
    2381              :   }
    2382              : 
    2383              : protected:
    2384              :   // Clone function implementation as (not pure) virtual method
    2385         1013 :   EnumItemDiscriminant *clone_item_impl () const override
    2386              :   {
    2387         1013 :     return new EnumItemDiscriminant (*this);
    2388              :   }
    2389              : };
    2390              : 
    2391              : // AST node for Rust "enum" - tagged union
    2392              : class Enum : public VisItem, public GlobContainer
    2393              : {
    2394              :   Identifier enum_name;
    2395              : 
    2396              :   // bool has_generics;
    2397              :   // Generics generic_params;
    2398              :   std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
    2399              : 
    2400              :   // bool has_where_clause;
    2401              :   WhereClause where_clause;
    2402              : 
    2403              :   std::vector<std::unique_ptr<EnumItem>> items;
    2404              : 
    2405              :   location_t locus;
    2406              : 
    2407              : public:
    2408              :   std::string as_string () const override;
    2409              : 
    2410              :   // Returns whether "enum" has generic parameters.
    2411         1462 :   bool has_generics () const { return !generic_params.empty (); }
    2412              : 
    2413              :   // Returns whether "enum" has a where clause.
    2414        69012 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    2415              : 
    2416              :   /* Returns whether enum is a "zero-variant" (no possible variant) enum,
    2417              :    * which cannot be instantiated. */
    2418              :   bool is_zero_variant () const { return items.empty (); }
    2419              : 
    2420              :   // Mega-constructor
    2421          924 :   Enum (Identifier enum_name, Visibility vis,
    2422              :         std::vector<std::unique_ptr<GenericParam>> generic_params,
    2423              :         WhereClause where_clause, std::vector<std::unique_ptr<EnumItem>> items,
    2424              :         std::vector<Attribute> outer_attrs, location_t locus)
    2425              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    2426          924 :       enum_name (std::move (enum_name)),
    2427          924 :       generic_params (std::move (generic_params)),
    2428          924 :       where_clause (std::move (where_clause)), items (std::move (items)),
    2429          924 :       locus (locus)
    2430          924 :   {}
    2431              : 
    2432              :   // TODO: constructor with less arguments
    2433              : 
    2434              :   // Copy constructor with vector clone
    2435         2438 :   Enum (Enum const &other)
    2436         2438 :     : VisItem (other), enum_name (other.enum_name),
    2437         2438 :       where_clause (other.where_clause), locus (other.locus)
    2438              :   {
    2439         2438 :     generic_params.reserve (other.generic_params.size ());
    2440         3678 :     for (const auto &e : other.generic_params)
    2441         1240 :       generic_params.push_back (e->clone_generic_param ());
    2442              : 
    2443         2438 :     items.reserve (other.items.size ());
    2444         8332 :     for (const auto &e : other.items)
    2445         5894 :       items.push_back (e->clone_enum_item ());
    2446         2438 :   }
    2447              : 
    2448              :   // Overloaded assignment operator with vector clone
    2449              :   Enum &operator= (Enum const &other)
    2450              :   {
    2451              :     VisItem::operator= (other);
    2452              :     enum_name = other.enum_name;
    2453              :     where_clause = other.where_clause;
    2454              :     locus = other.locus;
    2455              : 
    2456              :     generic_params.reserve (other.generic_params.size ());
    2457              :     for (const auto &e : other.generic_params)
    2458              :       generic_params.push_back (e->clone_generic_param ());
    2459              : 
    2460              :     items.reserve (other.items.size ());
    2461              :     for (const auto &e : other.items)
    2462              :       items.push_back (e->clone_enum_item ());
    2463              : 
    2464              :     return *this;
    2465              :   }
    2466              : 
    2467              :   // Move constructors
    2468              :   Enum (Enum &&other) = default;
    2469              :   Enum &operator= (Enum &&other) = default;
    2470              : 
    2471        13240 :   location_t get_locus () const override final { return locus; }
    2472              : 
    2473              :   void accept_vis (ASTVisitor &vis) override;
    2474              : 
    2475        58054 :   Identifier get_identifier () const { return enum_name; }
    2476              : 
    2477              :   // Invalid if name is empty, so base stripping on that.
    2478            0 :   void mark_for_strip () override { enum_name = {""}; }
    2479        10083 :   bool is_marked_for_strip () const override { return enum_name.empty (); }
    2480              : 
    2481              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2482        80702 :   std::vector<std::unique_ptr<EnumItem>> &get_variants () { return items; }
    2483              :   const std::vector<std::unique_ptr<EnumItem>> &get_variants () const
    2484              :   {
    2485              :     return items;
    2486              :   }
    2487              : 
    2488         1588 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    2489              :   {
    2490        70034 :     return generic_params;
    2491              :   }
    2492              :   const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
    2493              :   {
    2494              :     return generic_params;
    2495              :   }
    2496              : 
    2497              :   // TODO: is this better? Or is a "vis_block" better?
    2498           32 :   WhereClause &get_where_clause () { return where_clause; }
    2499              : 
    2500         4810 :   Item::Kind get_item_kind () const override { return Item::Kind::Enum; }
    2501              : 
    2502         3674 :   GlobContainer::Kind get_glob_container_kind () const override
    2503              :   {
    2504         3674 :     return GlobContainer::Kind::Enum;
    2505              :   }
    2506              : 
    2507              : protected:
    2508              :   /* Use covariance to implement clone function as returning this object
    2509              :    * rather than base */
    2510         1517 :   Enum *clone_item_impl () const override { return new Enum (*this); }
    2511              : };
    2512              : 
    2513              : // Rust untagged union used for C compat AST node
    2514              : class Union : public VisItem
    2515              : {
    2516              :   Identifier union_name;
    2517              : 
    2518              :   // bool has_generics;
    2519              :   // Generics generic_params;
    2520              :   std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
    2521              : 
    2522              :   // bool has_where_clause;
    2523              :   WhereClause where_clause;
    2524              : 
    2525              :   std::vector<StructField> variants;
    2526              : 
    2527              :   location_t locus;
    2528              : 
    2529              : public:
    2530              :   std::string as_string () const override;
    2531              : 
    2532              :   // Returns whether union has generic params.
    2533          226 :   bool has_generics () const { return !generic_params.empty (); }
    2534              : 
    2535              :   // Returns whether union has where clause.
    2536         6042 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    2537              : 
    2538          131 :   Union (Identifier union_name, Visibility vis,
    2539              :          std::vector<std::unique_ptr<GenericParam>> generic_params,
    2540              :          WhereClause where_clause, std::vector<StructField> variants,
    2541              :          std::vector<Attribute> outer_attrs, location_t locus)
    2542              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    2543          262 :       union_name (std::move (union_name)),
    2544          131 :       generic_params (std::move (generic_params)),
    2545          131 :       where_clause (std::move (where_clause)), variants (std::move (variants)),
    2546          131 :       locus (locus)
    2547          131 :   {}
    2548              : 
    2549              :   // copy constructor with vector clone
    2550          279 :   Union (Union const &other)
    2551          279 :     : VisItem (other), union_name (other.union_name),
    2552          279 :       where_clause (other.where_clause), variants (other.variants),
    2553          279 :       locus (other.locus)
    2554              :   {
    2555          279 :     generic_params.reserve (other.generic_params.size ());
    2556          489 :     for (const auto &e : other.generic_params)
    2557          210 :       generic_params.push_back (e->clone_generic_param ());
    2558          279 :   }
    2559              : 
    2560              :   // overloaded assignment operator with vector clone
    2561              :   Union &operator= (Union const &other)
    2562              :   {
    2563              :     VisItem::operator= (other);
    2564              :     union_name = other.union_name;
    2565              :     where_clause = other.where_clause;
    2566              :     variants = other.variants;
    2567              :     locus = other.locus;
    2568              : 
    2569              :     generic_params.reserve (other.generic_params.size ());
    2570              :     for (const auto &e : other.generic_params)
    2571              :       generic_params.push_back (e->clone_generic_param ());
    2572              : 
    2573              :     return *this;
    2574              :   }
    2575              : 
    2576              :   // move constructors
    2577              :   Union (Union &&other) = default;
    2578              :   Union &operator= (Union &&other) = default;
    2579              : 
    2580         1179 :   location_t get_locus () const override final { return locus; }
    2581              : 
    2582              :   void accept_vis (ASTVisitor &vis) override;
    2583              : 
    2584              :   // Invalid if name is empty, so base stripping on that.
    2585            0 :   void mark_for_strip () override { union_name = {""}; }
    2586          799 :   bool is_marked_for_strip () const override { return union_name.empty (); }
    2587              : 
    2588              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2589         7099 :   std::vector<StructField> &get_variants () { return variants; }
    2590              :   const std::vector<StructField> &get_variants () const { return variants; }
    2591              : 
    2592          178 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    2593              :   {
    2594         6119 :     return generic_params;
    2595              :   }
    2596              :   const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
    2597              :   {
    2598              :     return generic_params;
    2599              :   }
    2600              : 
    2601              :   // TODO: is this better? Or is a "vis_block" better?
    2602            0 :   WhereClause &get_where_clause () { return where_clause; }
    2603              : 
    2604         4875 :   Identifier get_identifier () const { return union_name; }
    2605              : 
    2606         3522 :   Item::Kind get_item_kind () const override { return Item::Kind::Union; }
    2607              : 
    2608              : protected:
    2609              :   /* Use covariance to implement clone function as returning this object
    2610              :    * rather than base */
    2611          148 :   Union *clone_item_impl () const override { return new Union (*this); }
    2612              : };
    2613              : 
    2614              : /* "Constant item" AST node - used for constant, compile-time expressions
    2615              :  * within module scope (like constexpr) */
    2616              : class ConstantItem : public VisItem, public AssociatedItem
    2617              : {
    2618              :   // either has an identifier or "_" - maybe handle in identifier?
    2619              :   // bool identifier_is_underscore;
    2620              :   // if no identifier declared, identifier will be "_"
    2621              :   Identifier identifier;
    2622              : 
    2623              :   std::unique_ptr<Type> type;
    2624              :   std::unique_ptr<Expr> const_expr;
    2625              : 
    2626              :   location_t locus;
    2627              : 
    2628              : public:
    2629              :   std::string as_string () const override;
    2630              : 
    2631         6350 :   ConstantItem (Identifier ident, Visibility vis, std::unique_ptr<Type> type,
    2632              :                 std::unique_ptr<Expr> const_expr,
    2633              :                 std::vector<Attribute> outer_attrs, location_t locus)
    2634              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    2635         6350 :       identifier (std::move (ident)), type (std::move (type)),
    2636         6350 :       const_expr (std::move (const_expr)), locus (locus)
    2637         6350 :   {}
    2638              : 
    2639            4 :   ConstantItem (Identifier ident, Visibility vis, std::unique_ptr<Type> type,
    2640              :                 std::vector<Attribute> outer_attrs, location_t locus)
    2641              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    2642            4 :       identifier (std::move (ident)), type (std::move (type)),
    2643            4 :       const_expr (nullptr), locus (locus)
    2644            4 :   {}
    2645              : 
    2646        25935 :   ConstantItem (ConstantItem const &other)
    2647        25935 :     : VisItem (other), identifier (other.identifier), locus (other.locus)
    2648              :   {
    2649              :     // guard to prevent null dereference (only required if error state)
    2650        25935 :     if (other.type != nullptr)
    2651        25935 :       type = other.type->clone_type ();
    2652        25935 :     if (other.const_expr != nullptr)
    2653        25090 :       const_expr = other.const_expr->clone_expr ();
    2654        25935 :   }
    2655              : 
    2656              :   // Overload assignment operator to clone
    2657              :   ConstantItem &operator= (ConstantItem const &other)
    2658              :   {
    2659              :     VisItem::operator= (other);
    2660              :     identifier = other.identifier;
    2661              :     locus = other.locus;
    2662              : 
    2663              :     // guard to prevent null dereference (only required if error state)
    2664              :     if (other.type != nullptr)
    2665              :       type = other.type->clone_type ();
    2666              :     else
    2667              :       type = nullptr;
    2668              :     if (other.const_expr != nullptr)
    2669              :       const_expr = other.const_expr->clone_expr ();
    2670              :     else
    2671              :       const_expr = nullptr;
    2672              : 
    2673              :     return *this;
    2674              :   }
    2675              : 
    2676              :   // move constructors
    2677              :   ConstantItem (ConstantItem &&other) = default;
    2678              :   ConstantItem &operator= (ConstantItem &&other) = default;
    2679              : 
    2680              :   /* Returns whether constant item is an "unnamed" (wildcard underscore used
    2681              :    * as identifier) constant. */
    2682          561 :   bool is_unnamed () const { return identifier.as_string () == "_"; }
    2683              : 
    2684       209962 :   location_t get_locus () const override final { return locus; }
    2685              : 
    2686              :   // needed to override AssociatedItem::get_node_id
    2687       821858 :   NodeId get_node_id () const override final { return VisItem::get_node_id (); }
    2688              : 
    2689              :   void accept_vis (ASTVisitor &vis) override;
    2690              : 
    2691              :   // Invalid if type and expression are null, so base stripping on that.
    2692            8 :   void mark_for_strip () override
    2693              :   {
    2694            8 :     type = nullptr;
    2695            8 :     const_expr = nullptr;
    2696            8 :   }
    2697       202358 :   bool is_marked_for_strip () const override
    2698              :   {
    2699       202358 :     return type == nullptr && const_expr == nullptr;
    2700              :   }
    2701              : 
    2702      1563362 :   bool has_expr () const { return const_expr != nullptr; }
    2703              : 
    2704              :   // TODO: is this better? Or is a "vis_block" better?
    2705      1119208 :   Expr &get_expr ()
    2706              :   {
    2707      1119208 :     rust_assert (const_expr != nullptr);
    2708      1119208 :     return *const_expr;
    2709              :   }
    2710              : 
    2711       388796 :   std::unique_ptr<Expr> &get_expr_ptr ()
    2712              :   {
    2713       388796 :     rust_assert (const_expr != nullptr);
    2714       388796 :     return const_expr;
    2715              :   }
    2716              : 
    2717              :   // TODO: is this better? Or is a "vis_block" better?
    2718      1158502 :   Type &get_type ()
    2719              :   {
    2720      1158502 :     rust_assert (type != nullptr);
    2721      1158502 :     return *type;
    2722              :   }
    2723              : 
    2724       399767 :   std::unique_ptr<Type> &get_type_ptr ()
    2725              :   {
    2726       399767 :     rust_assert (type != nullptr);
    2727       399767 :     return type;
    2728              :   }
    2729              : 
    2730       615915 :   const Identifier &get_identifier () const { return identifier; }
    2731              : 
    2732        17533 :   Item::Kind get_item_kind () const override
    2733              :   {
    2734        17533 :     return Item::Kind::ConstantItem;
    2735              :   }
    2736              : 
    2737              : protected:
    2738              :   /* Use covariance to implement clone function as returning this object
    2739              :    * rather than base */
    2740        12800 :   ConstantItem *clone_item_impl () const override
    2741              :   {
    2742        12800 :     return new ConstantItem (*this);
    2743              :   }
    2744              : 
    2745              :   /* Use covariance to implement clone function as returning this object
    2746              :    * rather than base */
    2747         6837 :   ConstantItem *clone_associated_item_impl () const override
    2748              :   {
    2749         6837 :     return new ConstantItem (*this);
    2750              :   }
    2751              : };
    2752              : 
    2753              : /* Static item AST node - items within module scope with fixed storage
    2754              :  * duration? */
    2755              : class StaticItem : public VisItem
    2756              : {
    2757              :   bool has_mut;
    2758              :   Identifier name;
    2759              :   std::unique_ptr<Type> type;
    2760              :   std::unique_ptr<Expr> expr;
    2761              :   location_t locus;
    2762              : 
    2763              : public:
    2764              :   std::string as_string () const override;
    2765              : 
    2766          378 :   StaticItem (Identifier name, bool is_mut, std::unique_ptr<Type> type,
    2767              :               std::unique_ptr<Expr> expr, Visibility vis,
    2768              :               std::vector<Attribute> outer_attrs, location_t locus)
    2769          378 :     : VisItem (std::move (vis), std::move (outer_attrs)), has_mut (is_mut),
    2770          756 :       name (std::move (name)), type (std::move (type)), expr (std::move (expr)),
    2771          378 :       locus (locus)
    2772          378 :   {}
    2773              : 
    2774              :   // Copy constructor with clone
    2775          967 :   StaticItem (StaticItem const &other)
    2776          967 :     : VisItem (other), has_mut (other.has_mut), name (other.name),
    2777          967 :       locus (other.locus)
    2778              :   {
    2779              :     // guard to prevent null dereference (only required if error state)
    2780          967 :     if (other.type != nullptr)
    2781          967 :       type = other.type->clone_type ();
    2782          967 :     if (other.expr != nullptr)
    2783          967 :       expr = other.expr->clone_expr ();
    2784          967 :   }
    2785              : 
    2786              :   // Overloaded assignment operator to clone
    2787              :   StaticItem &operator= (StaticItem const &other)
    2788              :   {
    2789              :     VisItem::operator= (other);
    2790              :     name = other.name;
    2791              :     has_mut = other.has_mut;
    2792              :     locus = other.locus;
    2793              : 
    2794              :     // guard to prevent null dereference (only required if error state)
    2795              :     if (other.type != nullptr)
    2796              :       type = other.type->clone_type ();
    2797              :     else
    2798              :       type = nullptr;
    2799              :     if (other.expr != nullptr)
    2800              :       expr = other.expr->clone_expr ();
    2801              :     else
    2802              :       expr = nullptr;
    2803              : 
    2804              :     return *this;
    2805              :   }
    2806              : 
    2807              :   // move constructors
    2808              :   StaticItem (StaticItem &&other) = default;
    2809              :   StaticItem &operator= (StaticItem &&other) = default;
    2810              : 
    2811        10818 :   location_t get_locus () const override final { return locus; }
    2812              : 
    2813              :   void accept_vis (ASTVisitor &vis) override;
    2814              : 
    2815              :   // Invalid if type or expression are null, so base stripping on that.
    2816            0 :   void mark_for_strip () override
    2817              :   {
    2818            0 :     type = nullptr;
    2819            0 :     expr = nullptr;
    2820            0 :   }
    2821         9990 :   bool is_marked_for_strip () const override
    2822              :   {
    2823         9990 :     return type == nullptr && expr == nullptr;
    2824              :   }
    2825              : 
    2826           70 :   bool has_expr () { return expr != nullptr; }
    2827              : 
    2828              :   // TODO: is this better? Or is a "vis_block" better?
    2829        53105 :   Expr &get_expr ()
    2830              :   {
    2831        53105 :     rust_assert (expr != nullptr);
    2832        53105 :     return *expr;
    2833              :   }
    2834              : 
    2835        20349 :   std::unique_ptr<Expr> &get_expr_ptr ()
    2836              :   {
    2837        20349 :     rust_assert (expr != nullptr);
    2838        20349 :     return expr;
    2839              :   }
    2840              : 
    2841              :   // TODO: is this better? Or is a "vis_block" better?
    2842        53105 :   Type &get_type ()
    2843              :   {
    2844        53105 :     rust_assert (type != nullptr);
    2845        53105 :     return *type;
    2846              :   }
    2847              : 
    2848        20349 :   std::unique_ptr<Type> &get_type_ptr ()
    2849              :   {
    2850        20349 :     rust_assert (type != nullptr);
    2851        20349 :     return type;
    2852              :   }
    2853              : 
    2854          426 :   bool is_mutable () const { return has_mut; }
    2855              : 
    2856        31517 :   Identifier get_identifier () const { return name; }
    2857              : 
    2858           77 :   Item::Kind get_item_kind () const override { return Item::Kind::StaticItem; }
    2859              : 
    2860              : protected:
    2861              :   /* Use covariance to implement clone function as returning this object
    2862              :    * rather than base */
    2863          967 :   StaticItem *clone_item_impl () const override
    2864              :   {
    2865          967 :     return new StaticItem (*this);
    2866              :   }
    2867              : };
    2868              : 
    2869              : // Type items within traits
    2870              : class TraitItemType : public TraitItem
    2871              : {
    2872              :   std::vector<Attribute> outer_attrs;
    2873              : 
    2874              :   Identifier name;
    2875              : 
    2876              :   // Generic parameters for GATs (Generic Associated Types)
    2877              :   std::vector<std::unique_ptr<GenericParam>> generic_params;
    2878              : 
    2879              :   // bool has_type_param_bounds;
    2880              :   // TypeParamBounds type_param_bounds;
    2881              :   std::vector<std::unique_ptr<TypeParamBound>>
    2882              :     type_param_bounds; // inlined form
    2883              : 
    2884              : public:
    2885            0 :   bool has_generics () const { return !generic_params.empty (); }
    2886              : 
    2887              :   // Returns whether trait item type has type param bounds.
    2888            0 :   bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
    2889              : 
    2890         1180 :   TraitItemType (Identifier name,
    2891              :                  std::vector<std::unique_ptr<GenericParam>> generic_params,
    2892              :                  std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
    2893              :                  std::vector<Attribute> outer_attrs, Visibility vis,
    2894              :                  location_t locus)
    2895         1180 :     : TraitItem (vis, locus), outer_attrs (std::move (outer_attrs)),
    2896         1180 :       name (std::move (name)), generic_params (std::move (generic_params)),
    2897         1180 :       type_param_bounds (std::move (type_param_bounds))
    2898         1180 :   {}
    2899              : 
    2900              :   // Copy constructor with vector clone
    2901         2861 :   TraitItemType (TraitItemType const &other)
    2902         5722 :     : TraitItem (other.locus), outer_attrs (other.outer_attrs),
    2903         2861 :       name (other.name)
    2904              :   {
    2905         2861 :     node_id = other.node_id;
    2906         2861 :     generic_params.reserve (other.generic_params.size ());
    2907         2897 :     for (const auto &e : other.generic_params)
    2908           36 :       generic_params.push_back (e->clone_generic_param ());
    2909         2861 :     type_param_bounds.reserve (other.type_param_bounds.size ());
    2910         3426 :     for (const auto &e : other.type_param_bounds)
    2911          565 :       type_param_bounds.push_back (e->clone_type_param_bound ());
    2912         2861 :   }
    2913              : 
    2914              :   // Overloaded assignment operator with vector clone
    2915              :   TraitItemType &operator= (TraitItemType const &other)
    2916              :   {
    2917              :     TraitItem::operator= (other);
    2918              :     outer_attrs = other.outer_attrs;
    2919              :     name = other.name;
    2920              :     locus = other.locus;
    2921              :     node_id = other.node_id;
    2922              : 
    2923              :     generic_params.reserve (other.generic_params.size ());
    2924              :     for (const auto &e : other.generic_params)
    2925              :       generic_params.push_back (e->clone_generic_param ());
    2926              :     type_param_bounds.reserve (other.type_param_bounds.size ());
    2927              :     for (const auto &e : other.type_param_bounds)
    2928              :       type_param_bounds.push_back (e->clone_type_param_bound ());
    2929              : 
    2930              :     return *this;
    2931              :   }
    2932              : 
    2933              :   // default move constructors
    2934              :   TraitItemType (TraitItemType &&other) = default;
    2935              :   TraitItemType &operator= (TraitItemType &&other) = default;
    2936              : 
    2937              :   std::string as_string () const override;
    2938              : 
    2939              :   void accept_vis (ASTVisitor &vis) override;
    2940              : 
    2941              :   // Invalid if name is empty, so base stripping on that.
    2942            0 :   void mark_for_strip () override { name = {""}; }
    2943        13803 :   bool is_marked_for_strip () const override { return name.empty (); }
    2944              : 
    2945              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2946       127043 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
    2947         1178 :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
    2948              : 
    2949              :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    2950              :   {
    2951        99801 :     return generic_params;
    2952              :   }
    2953              :   const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
    2954              :   {
    2955              :     return generic_params;
    2956              :   }
    2957              : 
    2958              :   std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
    2959              :   {
    2960        99801 :     return type_param_bounds;
    2961              :   }
    2962              :   const std::vector<std::unique_ptr<TypeParamBound>> &
    2963              :   get_type_param_bounds () const
    2964              :   {
    2965              :     return type_param_bounds;
    2966              :   }
    2967              : 
    2968        15779 :   Identifier get_identifier () const { return name; }
    2969              : 
    2970              : protected:
    2971              :   // Clone function implementation as (not pure) virtual method
    2972         2861 :   TraitItemType *clone_associated_item_impl () const override
    2973              :   {
    2974         2861 :     return new TraitItemType (*this);
    2975              :   }
    2976              : };
    2977              : 
    2978              : // Rust trait item declaration AST node
    2979              : class Trait : public VisItem
    2980              : {
    2981              :   bool has_unsafe;
    2982              :   bool has_auto;
    2983              :   Identifier name;
    2984              :   std::vector<std::unique_ptr<GenericParam>> generic_params;
    2985              :   TypeParam self_param;
    2986              :   std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
    2987              :   WhereClause where_clause;
    2988              :   std::vector<Attribute> inner_attrs;
    2989              :   std::vector<std::unique_ptr<AssociatedItem>> trait_items;
    2990              :   location_t locus;
    2991              : 
    2992              : public:
    2993              :   std::string as_string () const override;
    2994              : 
    2995              :   // Returns whether trait has generic parameters.
    2996        10020 :   bool has_generics () const { return !generic_params.empty (); }
    2997              : 
    2998              :   // Returns whether trait has type parameter bounds.
    2999         5770 :   bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
    3000              : 
    3001              :   // Returns whether trait has where clause.
    3002       343860 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    3003              : 
    3004              :   // Returns whether trait has trait items.
    3005           62 :   bool has_trait_items () const { return !trait_items.empty (); }
    3006              : 
    3007              :   // Returns whether trait has inner attributes.
    3008              :   bool has_inner_attrs () const { return !inner_attrs.empty (); }
    3009              : 
    3010       279375 :   Identifier get_identifier () const { return name; }
    3011              : 
    3012         5708 :   bool is_unsafe () const { return has_unsafe; }
    3013        17200 :   bool is_auto () const { return has_auto; }
    3014              : 
    3015              :   // Mega-constructor
    3016         5803 :   Trait (Identifier name, bool is_unsafe, bool is_auto,
    3017              :          std::vector<std::unique_ptr<GenericParam>> generic_params,
    3018              :          std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
    3019              :          WhereClause where_clause,
    3020              :          std::vector<std::unique_ptr<AssociatedItem>> trait_items,
    3021              :          Visibility vis, std::vector<Attribute> outer_attrs,
    3022              :          std::vector<Attribute> inner_attrs, location_t locus)
    3023              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    3024        11606 :       has_unsafe (is_unsafe), has_auto (is_auto), name (std::move (name)),
    3025        11606 :       generic_params (std::move (generic_params)), self_param ({"Self"}, locus),
    3026         5803 :       type_param_bounds (std::move (type_param_bounds)),
    3027         5803 :       where_clause (std::move (where_clause)),
    3028         5803 :       inner_attrs (std::move (inner_attrs)),
    3029         5803 :       trait_items (std::move (trait_items)), locus (locus)
    3030         5803 :   {}
    3031              : 
    3032              :   // Copy constructor with vector clone
    3033        13608 :   Trait (Trait const &other)
    3034        13608 :     : VisItem (other), has_unsafe (other.has_unsafe), has_auto (other.has_auto),
    3035        13608 :       name (other.name), self_param (other.self_param),
    3036        13608 :       where_clause (other.where_clause), inner_attrs (other.inner_attrs),
    3037        27216 :       locus (other.locus)
    3038              :   {
    3039        13608 :     generic_params.reserve (other.generic_params.size ());
    3040        16950 :     for (const auto &e : other.generic_params)
    3041         3342 :       generic_params.push_back (e->clone_generic_param ());
    3042              : 
    3043        13608 :     type_param_bounds.reserve (other.type_param_bounds.size ());
    3044        17477 :     for (const auto &e : other.type_param_bounds)
    3045         3869 :       type_param_bounds.push_back (e->clone_type_param_bound ());
    3046              : 
    3047        13608 :     trait_items.reserve (other.trait_items.size ());
    3048        32902 :     for (const auto &e : other.trait_items)
    3049        19294 :       trait_items.push_back (e->clone_associated_item ());
    3050        13608 :   }
    3051              : 
    3052              :   // Overloaded assignment operator with vector clone
    3053              :   Trait &operator= (Trait const &other)
    3054              :   {
    3055              :     VisItem::operator= (other);
    3056              :     name = other.name;
    3057              :     self_param = other.self_param;
    3058              :     has_unsafe = other.has_unsafe;
    3059              :     has_auto = other.has_auto;
    3060              :     where_clause = other.where_clause;
    3061              :     inner_attrs = other.inner_attrs;
    3062              :     locus = other.locus;
    3063              : 
    3064              :     generic_params.reserve (other.generic_params.size ());
    3065              :     for (const auto &e : other.generic_params)
    3066              :       generic_params.push_back (e->clone_generic_param ());
    3067              : 
    3068              :     type_param_bounds.reserve (other.type_param_bounds.size ());
    3069              :     for (const auto &e : other.type_param_bounds)
    3070              :       type_param_bounds.push_back (e->clone_type_param_bound ());
    3071              : 
    3072              :     trait_items.reserve (other.trait_items.size ());
    3073              :     for (const auto &e : other.trait_items)
    3074              :       trait_items.push_back (e->clone_associated_item ());
    3075              : 
    3076              :     return *this;
    3077              :   }
    3078              : 
    3079              :   // default move constructors
    3080              :   Trait (Trait &&other) = default;
    3081              :   Trait &operator= (Trait &&other) = default;
    3082              : 
    3083        67772 :   location_t get_locus () const override final { return locus; }
    3084              : 
    3085              :   void accept_vis (ASTVisitor &vis) override;
    3086              : 
    3087              :   // Invalid if trait name is empty, so base stripping on that.
    3088            2 :   void mark_for_strip () override { name = {""}; }
    3089        48097 :   bool is_marked_for_strip () const override { return name.empty (); }
    3090              : 
    3091              :   // TODO: think of better way to do this
    3092              :   const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
    3093       343891 :   std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
    3094              : 
    3095              :   const std::vector<std::unique_ptr<AssociatedItem>> &get_trait_items () const
    3096              :   {
    3097              :     return trait_items;
    3098              :   }
    3099       100451 :   std::vector<std::unique_ptr<AssociatedItem>> &get_trait_items ()
    3100              :   {
    3101       401935 :     return trait_items;
    3102              :   }
    3103              : 
    3104         1909 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    3105              :   {
    3106       345769 :     return generic_params;
    3107              :   }
    3108              :   const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
    3109              :   {
    3110              :     return generic_params;
    3111              :   }
    3112              : 
    3113         4250 :   std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
    3114              :   {
    3115       351289 :     return type_param_bounds;
    3116              :   }
    3117              :   const std::vector<std::unique_ptr<TypeParamBound>> &
    3118              :   get_type_param_bounds () const
    3119              :   {
    3120              :     return type_param_bounds;
    3121              :   }
    3122              : 
    3123          196 :   WhereClause &get_where_clause () { return where_clause; }
    3124              : 
    3125       295775 :   AST::TypeParam &get_implicit_self () { return self_param; }
    3126              : 
    3127        83839 :   Item::Kind get_item_kind () const override { return Item::Kind::Trait; }
    3128              : 
    3129              : protected:
    3130              :   /* Use covariance to implement clone function as returning this object
    3131              :    * rather than base */
    3132         7862 :   Trait *clone_item_impl () const override { return new Trait (*this); }
    3133              : };
    3134              : 
    3135              : // Implementation item declaration AST node - abstract base class
    3136              : class Impl : public VisItem
    3137              : {
    3138              :   // must be protected to allow subclasses to access them properly
    3139              : protected:
    3140              :   // bool has_generics;
    3141              :   // Generics generic_params;
    3142              :   std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
    3143              : 
    3144              :   std::unique_ptr<Type> trait_type;
    3145              : 
    3146              :   // bool has_where_clause;
    3147              :   WhereClause where_clause;
    3148              : 
    3149              :   // bool has_inner_attrs;
    3150              :   std::vector<Attribute> inner_attrs;
    3151              : 
    3152              : private:
    3153              :   // doesn't really need to be protected as write access probably not needed
    3154              :   location_t locus;
    3155              : 
    3156              : public:
    3157              :   // Returns whether impl has generic parameters.
    3158        65479 :   bool has_generics () const { return !generic_params.empty (); }
    3159              : 
    3160              :   // Returns whether impl has where clause.
    3161     11840174 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    3162              : 
    3163              :   // Returns whether impl has inner attributes.
    3164              :   bool has_inner_attrs () const { return !inner_attrs.empty (); }
    3165              : 
    3166        73347 :   location_t get_locus () const override final { return locus; }
    3167              : 
    3168              :   // Invalid if trait type is null, so base stripping on that.
    3169          146 :   void mark_for_strip () override { trait_type = nullptr; }
    3170      1882621 :   bool is_marked_for_strip () const override { return trait_type == nullptr; }
    3171              : 
    3172              :   // TODO: think of better way to do this
    3173              :   const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
    3174     13782272 :   std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
    3175              : 
    3176        29569 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    3177              :   {
    3178     11862079 :     return generic_params;
    3179              :   }
    3180              :   const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
    3181              :   {
    3182              :     return generic_params;
    3183              :   }
    3184              : 
    3185              :   // TODO: is this better? Or is a "vis_block" better?
    3186       619182 :   WhereClause &get_where_clause () { return where_clause; }
    3187              : 
    3188              :   // TODO: is this better? Or is a "vis_block" better?
    3189     11990961 :   Type &get_type ()
    3190              :   {
    3191     11990961 :     rust_assert (trait_type != nullptr);
    3192     11990961 :     return *trait_type;
    3193              :   }
    3194              : 
    3195      3837451 :   std::unique_ptr<Type> &get_type_ptr ()
    3196              :   {
    3197      3837451 :     rust_assert (trait_type != nullptr);
    3198      3837451 :     return trait_type;
    3199              :   }
    3200              : 
    3201       101544 :   Item::Kind get_item_kind () const override { return Item::Kind::Impl; }
    3202              : 
    3203              : protected:
    3204              :   // Mega-constructor
    3205        65826 :   Impl (std::vector<std::unique_ptr<GenericParam>> generic_params,
    3206              :         std::unique_ptr<Type> trait_type, WhereClause where_clause,
    3207              :         Visibility vis, std::vector<Attribute> inner_attrs,
    3208              :         std::vector<Attribute> outer_attrs, location_t locus)
    3209              :     : VisItem (std::move (vis), std::move (outer_attrs)),
    3210        65826 :       generic_params (std::move (generic_params)),
    3211        65826 :       trait_type (std::move (trait_type)),
    3212        65826 :       where_clause (std::move (where_clause)),
    3213        65826 :       inner_attrs (std::move (inner_attrs)), locus (locus)
    3214        65826 :   {}
    3215              : 
    3216              :   // Copy constructor
    3217       358054 :   Impl (Impl const &other)
    3218       358054 :     : VisItem (other), where_clause (other.where_clause),
    3219       358054 :       inner_attrs (other.inner_attrs), locus (other.locus)
    3220              :   {
    3221              :     // guard to prevent null dereference (only required if error state)
    3222       358054 :     if (other.trait_type != nullptr)
    3223       358054 :       trait_type = other.trait_type->clone_type ();
    3224              : 
    3225       358054 :     generic_params.reserve (other.generic_params.size ());
    3226       660191 :     for (const auto &e : other.generic_params)
    3227       302137 :       generic_params.push_back (e->clone_generic_param ());
    3228       358054 :   }
    3229              : 
    3230              :   // Assignment operator overload with cloning
    3231              :   Impl &operator= (Impl const &other)
    3232              :   {
    3233              :     VisItem::operator= (other);
    3234              :     where_clause = other.where_clause;
    3235              :     inner_attrs = other.inner_attrs;
    3236              :     locus = other.locus;
    3237              : 
    3238              :     // guard to prevent null dereference (only required if error state)
    3239              :     if (other.trait_type != nullptr)
    3240              :       trait_type = other.trait_type->clone_type ();
    3241              :     else
    3242              :       trait_type = nullptr;
    3243              : 
    3244              :     generic_params.reserve (other.generic_params.size ());
    3245              :     for (const auto &e : other.generic_params)
    3246              :       generic_params.push_back (e->clone_generic_param ());
    3247              : 
    3248              :     return *this;
    3249              :   }
    3250              : 
    3251              :   // move constructors
    3252              :   Impl (Impl &&other) = default;
    3253              :   Impl &operator= (Impl &&other) = default;
    3254              : };
    3255              : 
    3256              : // Regular "impl foo" impl block declaration AST node
    3257              : class InherentImpl : public Impl
    3258              : {
    3259              :   // bool has_impl_items;
    3260              :   std::vector<std::unique_ptr<AssociatedItem>> impl_items;
    3261              : 
    3262              : public:
    3263              :   std::string as_string () const override;
    3264              : 
    3265              :   // Returns whether inherent impl block has inherent impl items.
    3266            0 :   bool has_impl_items () const { return !impl_items.empty (); }
    3267              : 
    3268              :   // Mega-constructor
    3269         3660 :   InherentImpl (std::vector<std::unique_ptr<AssociatedItem>> impl_items,
    3270              :                 std::vector<std::unique_ptr<GenericParam>> generic_params,
    3271              :                 std::unique_ptr<Type> trait_type, WhereClause where_clause,
    3272              :                 Visibility vis, std::vector<Attribute> inner_attrs,
    3273              :                 std::vector<Attribute> outer_attrs, location_t locus)
    3274              :     : Impl (std::move (generic_params), std::move (trait_type),
    3275              :             std::move (where_clause), std::move (vis), std::move (inner_attrs),
    3276              :             std::move (outer_attrs), locus),
    3277         3660 :       impl_items (std::move (impl_items))
    3278         3660 :   {}
    3279              : 
    3280              :   // Copy constructor with vector clone
    3281        12723 :   InherentImpl (InherentImpl const &other) : Impl (other)
    3282              :   {
    3283        12723 :     impl_items.reserve (other.impl_items.size ());
    3284        82395 :     for (const auto &e : other.impl_items)
    3285        69672 :       impl_items.push_back (e->clone_associated_item ());
    3286        12723 :   }
    3287              : 
    3288              :   // Overloaded assignment operator with vector clone
    3289              :   InherentImpl &operator= (InherentImpl const &other)
    3290              :   {
    3291              :     Impl::operator= (other);
    3292              : 
    3293              :     impl_items.reserve (other.impl_items.size ());
    3294              :     for (const auto &e : other.impl_items)
    3295              :       impl_items.push_back (e->clone_associated_item ());
    3296              : 
    3297              :     return *this;
    3298              :   }
    3299              : 
    3300              :   // default move constructors
    3301              :   InherentImpl (InherentImpl &&other) = default;
    3302              :   InherentImpl &operator= (InherentImpl &&other) = default;
    3303              : 
    3304              :   void accept_vis (ASTVisitor &vis) override;
    3305              : 
    3306              :   // TODO: think of better way to do this
    3307              :   const std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items () const
    3308              :   {
    3309              :     return impl_items;
    3310              :   }
    3311       168461 :   std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items ()
    3312              :   {
    3313       620889 :     return impl_items;
    3314              :   }
    3315              : 
    3316              : protected:
    3317              :   /* Use covariance to implement clone function as returning this object
    3318              :    * rather than base */
    3319         9146 :   InherentImpl *clone_item_impl () const override
    3320              :   {
    3321         9146 :     return new InherentImpl (*this);
    3322              :   }
    3323              : };
    3324              : 
    3325              : // The "impl footrait for foo" impl block declaration AST node
    3326              : class TraitImpl : public Impl
    3327              : {
    3328              :   bool has_unsafe;
    3329              :   bool has_exclam;
    3330              :   TypePath trait_path;
    3331              : 
    3332              :   // bool has_impl_items;
    3333              :   std::vector<std::unique_ptr<AssociatedItem>> impl_items;
    3334              : 
    3335              : public:
    3336              :   std::string as_string () const override;
    3337              : 
    3338              :   // Returns whether trait impl has impl items.
    3339            0 :   bool has_impl_items () const { return !impl_items.empty (); }
    3340              : 
    3341              :   // Mega-constructor
    3342        62166 :   TraitImpl (TypePath trait_path, bool is_unsafe, bool has_exclam,
    3343              :              std::vector<std::unique_ptr<AssociatedItem>> impl_items,
    3344              :              std::vector<std::unique_ptr<GenericParam>> generic_params,
    3345              :              std::unique_ptr<Type> trait_type, WhereClause where_clause,
    3346              :              Visibility vis, std::vector<Attribute> inner_attrs,
    3347              :              std::vector<Attribute> outer_attrs, location_t locus)
    3348              :     : Impl (std::move (generic_params), std::move (trait_type),
    3349              :             std::move (where_clause), std::move (vis), std::move (inner_attrs),
    3350              :             std::move (outer_attrs), locus),
    3351        62166 :       has_unsafe (is_unsafe), has_exclam (has_exclam), trait_path (trait_path),
    3352        62166 :       impl_items (std::move (impl_items))
    3353        62166 :   {}
    3354              : 
    3355              :   // Copy constructor with vector clone
    3356       345331 :   TraitImpl (TraitImpl const &other)
    3357       345331 :     : Impl (other), has_unsafe (other.has_unsafe),
    3358       345331 :       has_exclam (other.has_exclam), trait_path (other.trait_path)
    3359              :   {
    3360       345331 :     impl_items.reserve (other.impl_items.size ());
    3361       841899 :     for (const auto &e : other.impl_items)
    3362       496568 :       impl_items.push_back (e->clone_associated_item ());
    3363       345331 :   }
    3364              : 
    3365              :   // Overloaded assignment operator with vector clone
    3366              :   TraitImpl &operator= (TraitImpl const &other)
    3367              :   {
    3368              :     Impl::operator= (other);
    3369              :     trait_path = other.trait_path;
    3370              :     has_unsafe = other.has_unsafe;
    3371              :     has_exclam = other.has_exclam;
    3372              : 
    3373              :     impl_items.reserve (other.impl_items.size ());
    3374              :     for (const auto &e : other.impl_items)
    3375              :       impl_items.push_back (e->clone_associated_item ());
    3376              : 
    3377              :     return *this;
    3378              :   }
    3379              : 
    3380              :   // move constructors
    3381              :   TraitImpl (TraitImpl &&other) = default;
    3382              :   TraitImpl &operator= (TraitImpl &&other) = default;
    3383              : 
    3384              :   void accept_vis (ASTVisitor &vis) override;
    3385              : 
    3386        61932 :   bool is_unsafe () const { return has_unsafe; };
    3387       130587 :   bool is_exclam () const { return has_exclam; }
    3388              : 
    3389              :   // TODO: think of better way to do this
    3390              :   const std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items () const
    3391              :   {
    3392              :     return impl_items;
    3393              :   }
    3394      3613497 :   std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items ()
    3395              :   {
    3396     13169047 :     return impl_items;
    3397              :   }
    3398              : 
    3399              :   // TODO: is this better? Or is a "vis_block" better?
    3400     13231064 :   TypePath &get_trait_path () { return trait_path; }
    3401              : 
    3402              : protected:
    3403              :   /* Use covariance to implement clone function as returning this object
    3404              :    * rather than base */
    3405       283328 :   TraitImpl *clone_item_impl () const override { return new TraitImpl (*this); }
    3406              : };
    3407              : 
    3408              : #if 0
    3409              : // Abstract base class for an item used inside an extern block
    3410              : class ExternalItem
    3411              : {
    3412              :   // bool has_outer_attrs;
    3413              :   std::vector<Attribute> outer_attrs;
    3414              : 
    3415              :   // bool has_visibility;
    3416              :   Visibility visibility;
    3417              : 
    3418              :   Identifier item_name;
    3419              :   location_t locus;
    3420              : 
    3421              : public:
    3422              :   virtual ~ExternalItem () {}
    3423              : 
    3424              :   /* TODO: spec syntax rules state that "MacroInvocationSemi" can be used as
    3425              :    * ExternalItem, but text body isn't so clear. Adding MacroInvocationSemi
    3426              :    * support would require a lot of refactoring. */
    3427              : 
    3428              :   // Returns whether item has outer attributes.
    3429              :   bool has_outer_attrs () const { return !outer_attrs.empty (); }
    3430              : 
    3431              :   // Returns whether item has non-default visibility.
    3432              :   bool has_visibility () const { return !visibility.is_error (); }
    3433              : 
    3434              :   // Unique pointer custom clone function
    3435              :   std::unique_ptr<ExternalItem> clone_external_item () const
    3436              :   {
    3437              :     return std::unique_ptr<ExternalItem> (clone_external_item_impl ());
    3438              :   }
    3439              : 
    3440              :   virtual std::string as_string () const;
    3441              : 
    3442              :   location_t get_locus () const override final { return locus; }
    3443              : 
    3444              :   virtual void accept_vis (ASTVisitor &vis) = 0;
    3445              : 
    3446              :   // TODO: make virtual? Would be more flexible.
    3447              :   // Based on idea that name should never be empty.
    3448              :   void mark_for_strip () { item_name = ""; };
    3449              :   bool is_marked_for_strip () const { return item_name.empty (); };
    3450              : 
    3451              : protected:
    3452              :   ExternalItem (Identifier item_name, Visibility vis,
    3453              :                 std::vector<Attribute> outer_attrs, location_t locus)
    3454              :     : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
    3455              :       item_name (std::move (item_name)), locus (locus)
    3456              :   {}
    3457              : 
    3458              :   // Copy constructor
    3459              :   ExternalItem (ExternalItem const &other)
    3460              :     : outer_attrs (other.outer_attrs), visibility (other.visibility),
    3461              :       item_name (other.item_name), locus (other.locus)
    3462              :   {}
    3463              : 
    3464              :   // Overloaded assignment operator to clone
    3465              :   ExternalItem &operator= (ExternalItem const &other)
    3466              :   {
    3467              :     item_name = other.item_name;
    3468              :     visibility = other.visibility;
    3469              :     outer_attrs = other.outer_attrs;
    3470              :     locus = other.locus;
    3471              : 
    3472              :     return *this;
    3473              :   }
    3474              : 
    3475              :   // move constructors
    3476              :   ExternalItem (ExternalItem &&other) = default;
    3477              :   ExternalItem &operator= (ExternalItem &&other) = default;
    3478              : 
    3479              :   // Clone function implementation as pure virtual method
    3480              :   virtual ExternalItem *clone_external_item_impl () const = 0;
    3481              : 
    3482              :   // possibly make this public if required
    3483              :   std::string get_item_name () const { return item_name; }
    3484              : };
    3485              : #endif
    3486              : 
    3487              : // A foreign type defined outside the current crate.
    3488              : // https://rust-lang.github.io/rfcs/1861-extern-types.html
    3489              : class ExternalTypeItem : public ExternalItem
    3490              : {
    3491              :   std::vector<Attribute> outer_attrs;
    3492              : 
    3493              :   Visibility visibility;
    3494              :   Identifier item_name;
    3495              :   location_t locus;
    3496              : 
    3497              :   bool marked_for_strip;
    3498              : 
    3499              : public:
    3500           14 :   ExternalTypeItem (Identifier item_name, Visibility vis,
    3501              :                     std::vector<Attribute> outer_attrs, location_t locus)
    3502           14 :     : ExternalItem (), outer_attrs (std::move (outer_attrs)), visibility (vis),
    3503           14 :       item_name (std::move (item_name)), locus (locus), marked_for_strip (false)
    3504           14 :   {}
    3505              : 
    3506              :   // copy constructor
    3507           24 :   ExternalTypeItem (ExternalTypeItem const &other)
    3508           24 :     : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs),
    3509           24 :       visibility (other.visibility), item_name (other.item_name),
    3510           24 :       locus (other.locus), marked_for_strip (other.marked_for_strip)
    3511              :   {
    3512           24 :     node_id = other.node_id;
    3513           24 :   }
    3514              : 
    3515              :   ExternalTypeItem &operator= (ExternalTypeItem const &other)
    3516              :   {
    3517              :     node_id = other.node_id;
    3518              :     outer_attrs = other.outer_attrs;
    3519              :     visibility = other.visibility;
    3520              :     item_name = other.item_name;
    3521              :     locus = other.locus;
    3522              :     marked_for_strip = other.marked_for_strip;
    3523              : 
    3524              :     return *this;
    3525              :   }
    3526              : 
    3527              :   // move constructors
    3528              :   ExternalTypeItem (ExternalTypeItem &&other) = default;
    3529              :   ExternalTypeItem &operator= (ExternalTypeItem &&other) = default;
    3530              : 
    3531              :   std::string as_string () const override;
    3532              : 
    3533              :   void accept_vis (ASTVisitor &vis) override;
    3534              : 
    3535              :   // Returns whether item has outer attributes.
    3536              :   bool has_outer_attrs () const { return !outer_attrs.empty (); }
    3537              : 
    3538              :   // Returns whether item has non-default visibility.
    3539              :   bool has_visibility () const { return true; }
    3540              : 
    3541          323 :   location_t get_locus () const { return locus; }
    3542              : 
    3543            0 :   void mark_for_strip () override { marked_for_strip = true; };
    3544          299 :   bool is_marked_for_strip () const override { return marked_for_strip; };
    3545              : 
    3546              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    3547         1540 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
    3548              :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
    3549              : 
    3550          313 :   Identifier get_identifier () const { return item_name; }
    3551              : 
    3552         1265 :   Visibility &get_visibility () { return visibility; }
    3553              :   const Visibility &get_visibility () const { return visibility; }
    3554              : 
    3555              : protected:
    3556              :   /* Use covariance to implement clone function as returning this object
    3557              :    * rather than base */
    3558           24 :   ExternalTypeItem *clone_external_item_impl () const override
    3559              :   {
    3560           24 :     return new ExternalTypeItem (*this);
    3561              :   }
    3562              : };
    3563              : 
    3564              : // A static item used in an extern block
    3565              : class ExternalStaticItem : public ExternalItem
    3566              : {
    3567              :   // bool has_outer_attrs;
    3568              :   std::vector<Attribute> outer_attrs;
    3569              : 
    3570              :   // bool has_visibility;
    3571              :   Visibility visibility;
    3572              : 
    3573              :   Identifier item_name;
    3574              :   location_t locus;
    3575              : 
    3576              :   bool has_mut;
    3577              :   std::unique_ptr<Type> item_type;
    3578              : 
    3579              : public:
    3580            1 :   ExternalStaticItem (Identifier item_name, std::unique_ptr<Type> item_type,
    3581              :                       bool is_mut, Visibility vis,
    3582              :                       std::vector<Attribute> outer_attrs, location_t locus)
    3583            1 :     : ExternalItem (), outer_attrs (std::move (outer_attrs)),
    3584            1 :       visibility (std::move (vis)), item_name (std::move (item_name)),
    3585            1 :       locus (locus), has_mut (is_mut), item_type (std::move (item_type))
    3586            1 :   {}
    3587              : 
    3588              :   // Copy constructor
    3589            2 :   ExternalStaticItem (ExternalStaticItem const &other)
    3590            2 :     : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs),
    3591            2 :       visibility (other.visibility), item_name (other.item_name),
    3592            4 :       locus (other.locus), has_mut (other.has_mut)
    3593              :   {
    3594            2 :     node_id = other.node_id;
    3595              :     // guard to prevent null dereference (only required if error state)
    3596            2 :     if (other.item_type != nullptr)
    3597            2 :       item_type = other.item_type->clone_type ();
    3598            2 :   }
    3599              : 
    3600              :   // Overloaded assignment operator to clone
    3601              :   ExternalStaticItem &operator= (ExternalStaticItem const &other)
    3602              :   {
    3603              :     node_id = other.node_id;
    3604              :     outer_attrs = other.outer_attrs;
    3605              :     visibility = other.visibility;
    3606              :     item_name = other.item_name;
    3607              :     locus = other.locus;
    3608              :     has_mut = other.has_mut;
    3609              : 
    3610              :     // guard to prevent null dereference (only required if error state)
    3611              :     if (other.item_type != nullptr)
    3612              :       item_type = other.item_type->clone_type ();
    3613              :     else
    3614              :       item_type = nullptr;
    3615              : 
    3616              :     return *this;
    3617              :   }
    3618              : 
    3619              :   // move constructors
    3620              :   ExternalStaticItem (ExternalStaticItem &&other) = default;
    3621              :   ExternalStaticItem &operator= (ExternalStaticItem &&other) = default;
    3622              : 
    3623              :   std::string as_string () const override;
    3624              : 
    3625              :   void accept_vis (ASTVisitor &vis) override;
    3626              : 
    3627              :   // Returns whether item has outer attributes.
    3628              :   bool has_outer_attrs () const { return !outer_attrs.empty (); }
    3629              : 
    3630              :   // Returns whether item has non-default visibility.
    3631              :   bool has_visibility () const { return true; }
    3632              : 
    3633            4 :   location_t get_locus () const { return locus; }
    3634              : 
    3635              :   // Based on idea that type should never be null.
    3636            0 :   void mark_for_strip () override { item_type = nullptr; };
    3637            3 :   bool is_marked_for_strip () const override { return item_type == nullptr; };
    3638              : 
    3639              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    3640           24 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
    3641              :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
    3642              : 
    3643              :   // TODO: is this better? Or is a "vis_block" better?
    3644           19 :   Type &get_type ()
    3645              :   {
    3646           19 :     rust_assert (item_type != nullptr);
    3647           19 :     return *item_type;
    3648              :   }
    3649              : 
    3650            5 :   std::unique_ptr<Type> &get_type_ptr ()
    3651              :   {
    3652            5 :     rust_assert (item_type != nullptr);
    3653            5 :     return item_type;
    3654              :   }
    3655              : 
    3656            4 :   Identifier get_identifier () const { return item_name; }
    3657              : 
    3658           22 :   Visibility &get_visibility () { return visibility; }
    3659              : 
    3660              :   const Visibility &get_visibility () const { return visibility; }
    3661              : 
    3662            1 :   bool is_mut () const { return has_mut; }
    3663              : 
    3664              : protected:
    3665              :   /* Use covariance to implement clone function as returning this object
    3666              :    * rather than base */
    3667            2 :   ExternalStaticItem *clone_external_item_impl () const override
    3668              :   {
    3669            2 :     return new ExternalStaticItem (*this);
    3670              :   }
    3671              : };
    3672              : 
    3673              : // An extern block AST node
    3674              : class ExternBlock : public VisItem
    3675              : {
    3676              :   // bool has_abi;
    3677              :   std::string abi;
    3678              : 
    3679              :   // bool has_inner_attrs;
    3680              :   std::vector<Attribute> inner_attrs;
    3681              : 
    3682              :   // bool has_extern_items;
    3683              :   std::vector<std::unique_ptr<ExternalItem>> extern_items;
    3684              : 
    3685              :   location_t locus;
    3686              : 
    3687              :   // TODO: find another way to store this to save memory?
    3688              :   bool marked_for_strip = false;
    3689              : 
    3690              : public:
    3691              :   std::string as_string () const override;
    3692              : 
    3693              :   // Returns whether extern block has inner attributes.
    3694              :   bool has_inner_attrs () const { return !inner_attrs.empty (); }
    3695              : 
    3696              :   // Returns whether extern block has extern items.
    3697            0 :   bool has_extern_items () const { return !extern_items.empty (); }
    3698              : 
    3699              :   // Returns whether extern block has ABI name.
    3700         8441 :   bool has_abi () const { return !abi.empty (); }
    3701              : 
    3702        12464 :   std::string get_abi () const { return abi; }
    3703              : 
    3704         2222 :   ExternBlock (std::string abi,
    3705              :                std::vector<std::unique_ptr<ExternalItem>> extern_items,
    3706              :                Visibility vis, std::vector<Attribute> inner_attrs,
    3707              :                std::vector<Attribute> outer_attrs, location_t locus)
    3708         2222 :     : VisItem (std::move (vis), std::move (outer_attrs)), abi (std::move (abi)),
    3709         2222 :       inner_attrs (std::move (inner_attrs)),
    3710         2222 :       extern_items (std::move (extern_items)), locus (locus)
    3711         2222 :   {}
    3712              : 
    3713              :   // Copy constructor with vector clone
    3714         5321 :   ExternBlock (ExternBlock const &other)
    3715         5321 :     : VisItem (other), abi (other.abi), inner_attrs (other.inner_attrs),
    3716        10642 :       locus (other.locus), marked_for_strip (other.marked_for_strip)
    3717              :   {
    3718         5321 :     extern_items.reserve (other.extern_items.size ());
    3719        34895 :     for (const auto &e : other.extern_items)
    3720        29574 :       extern_items.push_back (e->clone_external_item ());
    3721         5321 :   }
    3722              : 
    3723              :   // Overloaded assignment operator with vector clone
    3724              :   ExternBlock &operator= (ExternBlock const &other)
    3725              :   {
    3726              :     VisItem::operator= (other);
    3727              :     abi = other.abi;
    3728              :     inner_attrs = other.inner_attrs;
    3729              :     locus = other.locus;
    3730              :     marked_for_strip = other.marked_for_strip;
    3731              : 
    3732              :     extern_items.reserve (other.extern_items.size ());
    3733              :     for (const auto &e : other.extern_items)
    3734              :       extern_items.push_back (e->clone_external_item ());
    3735              : 
    3736              :     return *this;
    3737              :   }
    3738              : 
    3739              :   // move constructors
    3740              :   ExternBlock (ExternBlock &&other) = default;
    3741              :   ExternBlock &operator= (ExternBlock &&other) = default;
    3742              : 
    3743         5477 :   location_t get_locus () const override final { return locus; }
    3744              : 
    3745              :   void accept_vis (ASTVisitor &vis) override;
    3746              : 
    3747              :   // Can't think of any invalid invariants, so store boolean.
    3748            0 :   void mark_for_strip () override { marked_for_strip = true; }
    3749        16289 :   bool is_marked_for_strip () const override { return marked_for_strip; }
    3750              : 
    3751              :   // TODO: think of better way to do this
    3752              :   const std::vector<std::unique_ptr<ExternalItem>> &get_extern_items () const
    3753              :   {
    3754              :     return extern_items;
    3755              :   }
    3756        35542 :   std::vector<std::unique_ptr<ExternalItem>> &get_extern_items ()
    3757              :   {
    3758       122460 :     return extern_items;
    3759              :   }
    3760              : 
    3761              :   // TODO: think of better way to do this
    3762              :   const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
    3763       120651 :   std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
    3764              : 
    3765          474 :   Item::Kind get_item_kind () const override { return Item::Kind::ExternBlock; }
    3766              : 
    3767              : protected:
    3768              :   /* Use covariance to implement clone function as returning this object
    3769              :    * rather than base */
    3770         3101 :   ExternBlock *clone_item_impl () const override
    3771              :   {
    3772         3101 :     return new ExternBlock (*this);
    3773              :   }
    3774              : };
    3775              : 
    3776              : class MacroRulesDefinition;
    3777              : } // namespace AST
    3778              : } // namespace Rust
    3779              : 
    3780              : #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.