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-12-05 16:10:02 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      3330699 :   Identifier get_type_representation () const { return type_representation; }
      63              : 
      64              :   // Returns whether the type of the type param has been specified.
      65     23736921 :   bool has_type () const { return type != nullptr; }
      66              : 
      67              :   // Returns whether the type param has type param bounds.
      68       109856 :   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     17149722 :   AST::AttrVec &get_outer_attrs () { return outer_attrs; }
      74              : 
      75       104817 :   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       104817 :     : GenericParam (Analysis::Mappings::get ().get_next_node_id ()),
      81       104817 :       outer_attrs (std::move (outer_attrs)),
      82       104817 :       type_representation (std::move (type_representation)),
      83       104817 :       type_param_bounds (std::move (type_param_bounds)),
      84       104817 :       type (std::move (type)), locus (locus), was_impl_trait (was_impl_trait)
      85       104817 :   {}
      86              : 
      87              :   // Copy constructor uses clone
      88       475888 :   TypeParam (TypeParam const &other)
      89       475888 :     : GenericParam (other.node_id), outer_attrs (other.outer_attrs),
      90       475888 :       type_representation (other.type_representation), locus (other.locus),
      91       951776 :       was_impl_trait (other.was_impl_trait)
      92              :   {
      93              :     // guard to prevent null pointer dereference
      94       475888 :     if (other.type != nullptr)
      95         2116 :       type = other.type->clone_type ();
      96              : 
      97       475888 :     type_param_bounds.reserve (other.type_param_bounds.size ());
      98       596678 :     for (const auto &e : other.type_param_bounds)
      99       120790 :       type_param_bounds.push_back (e->clone_type_param_bound ());
     100       475888 :   }
     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      3327669 :   location_t get_locus () const override final { return locus; }
     131              : 
     132        97297 :   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        69079 :   Type &get_type ()
     138              :   {
     139        69079 :     rust_assert (type != nullptr);
     140        69079 :     return *type;
     141              :   }
     142              : 
     143        25674 :   std::unique_ptr<Type> &get_type_ptr ()
     144              :   {
     145        25674 :     rust_assert (type != nullptr);
     146        25674 :     return type;
     147              :   }
     148              : 
     149         2226 :   std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
     150              :   {
     151     20041679 :     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       104368 :   bool from_impl_trait () const { return was_impl_trait; }
     161              : 
     162              : protected:
     163              :   // Clone function implementation as virtual method
     164       460784 :   TypeParam *clone_generic_param_impl () const override
     165              :   {
     166       460784 :     return new TypeParam (*this);
     167              :   }
     168              : };
     169              : 
     170              : /* "where" clause item base. Abstract - use LifetimeWhereClauseItem,
     171              :  * TypeBoundWhereClauseItem */
     172        65231 : class WhereClauseItem
     173              : {
     174              : public:
     175              :   virtual ~WhereClauseItem () {}
     176              : 
     177              :   // Unique pointer custom clone function
     178        51584 :   std::unique_ptr<WhereClauseItem> clone_where_clause_item () const
     179              :   {
     180        51584 :     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           14 :   LifetimeWhereClauseItem (Lifetime lifetime,
     204              :                            std::vector<Lifetime> lifetime_bounds,
     205              :                            location_t locus)
     206           14 :     : lifetime (std::move (lifetime)),
     207           14 :       lifetime_bounds (std::move (lifetime_bounds)), locus (locus),
     208           14 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     209           14 :   {}
     210              : 
     211              :   std::string as_string () const override;
     212              : 
     213              :   void accept_vis (ASTVisitor &vis) override;
     214              : 
     215           14 :   NodeId get_node_id () const override final { return node_id; }
     216              : 
     217         2668 :   Lifetime &get_lifetime () { return lifetime; }
     218              : 
     219         2668 :   std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
     220              : 
     221           14 :   location_t get_locus () const { return locus; }
     222              : 
     223              : protected:
     224              :   // Clone function implementation as (not pure) virtual method
     225           26 :   LifetimeWhereClauseItem *clone_where_clause_item_impl () const override
     226              :   {
     227           26 :     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      2619839 :   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        13659 :   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        13659 :     : for_lifetimes (std::move (for_lifetimes)),
     254        13659 :       bound_type (std::move (bound_type)),
     255        13659 :       type_param_bounds (std::move (type_param_bounds)),
     256        13659 :       node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
     257        13659 :   {}
     258              : 
     259              :   // Copy constructor requires clone
     260        51558 :   TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other)
     261        51558 :     : for_lifetimes (other.for_lifetimes),
     262        51558 :       bound_type (other.bound_type->clone_type ())
     263              :   {
     264        51558 :     node_id = other.node_id;
     265        51558 :     type_param_bounds.reserve (other.type_param_bounds.size ());
     266       105496 :     for (const auto &e : other.type_param_bounds)
     267        53938 :       type_param_bounds.push_back (e->clone_type_param_bound ());
     268        51558 :   }
     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      2868281 :   Type &get_type ()
     293              :   {
     294      2868281 :     rust_assert (bound_type != nullptr);
     295      2868281 :     return *bound_type;
     296              :   }
     297              : 
     298       907650 :   std::unique_ptr<Type> &get_type_ptr ()
     299              :   {
     300       907650 :     rust_assert (bound_type != nullptr);
     301       907650 :     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      3067209 :     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        13630 :   NodeId get_node_id () const override final { return node_id; }
     317              : 
     318        13630 :   location_t get_locus () const { return locus; }
     319              : 
     320              : protected:
     321              :   // Clone function implementation as (not pure) virtual method
     322        51558 :   TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override
     323              :   {
     324        51558 :     return new TypeBoundWhereClauseItem (*this);
     325              :   }
     326              : };
     327              : 
     328              : // A where clause
     329       676301 : class WhereClause
     330              : {
     331              :   std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items;
     332              :   NodeId node_id;
     333              : 
     334              : public:
     335       339837 :   WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items)
     336       339837 :     : where_clause_items (std::move (where_clause_items)),
     337       339837 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     338       339837 :   {}
     339              : 
     340              :   // copy constructor with vector clone
     341      1652179 :   WhereClause (WhereClause const &other)
     342      1652179 :   {
     343      1652179 :     node_id = other.node_id;
     344      1652179 :     where_clause_items.reserve (other.where_clause_items.size ());
     345      1703763 :     for (const auto &e : other.where_clause_items)
     346        51584 :       where_clause_items.push_back (e->clone_where_clause_item ());
     347      1652179 :   }
     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       352416 :   WhereClause (WhereClause &&other) = default;
     362              :   WhereClause &operator= (WhereClause &&other) = default;
     363              : 
     364              :   // Creates a WhereClause with no items.
     365       329661 :   static WhereClause create_empty ()
     366              :   {
     367       329661 :     return WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> ());
     368              :   }
     369              : 
     370              :   // Returns whether the WhereClause has no items.
     371     64034004 :   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          368 :   std::vector<std::unique_ptr<WhereClauseItem>> &get_items ()
     379              :   {
     380      2494078 :     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       375388 : class Param : public Visitable
     390              : {
     391              : public:
     392      1944007 :   Param (std::vector<Attribute> outer_attrs, location_t locus)
     393      1944007 :     : outer_attrs (std::move (outer_attrs)), locus (locus),
     394      1944007 :       node_id (Analysis::Mappings::get ().get_next_node_id ())
     395      1944007 :   {}
     396              : 
     397              :   virtual ~Param () = default;
     398              : 
     399      1426681 :   std::unique_ptr<Param> clone_param () const
     400              :   {
     401      1426681 :     return std::unique_ptr<Param> (clone_param_impl ());
     402              :   }
     403              : 
     404     10206136 :   virtual bool is_variadic () const { return false; }
     405              : 
     406      9817702 :   virtual bool is_self () const { return false; }
     407              : 
     408       190871 :   NodeId get_node_id () const { return node_id; }
     409              : 
     410       275424 :   location_t get_locus () const { return locus; }
     411              : 
     412       689592 :   std::vector<Attribute> get_outer_attrs () const { return outer_attrs; }
     413              : 
     414     20693011 :   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     30728070 :   bool has_type () const { return type != nullptr; }
     450              : 
     451              :   // Returns whether the self-param has a valid lifetime.
     452     20758308 :   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        50620 :   SelfParam (std::unique_ptr<Type> type, bool is_mut, location_t locus)
     470        50620 :     : Param ({}, locus), has_ref (false), is_mut (is_mut),
     471        50620 :       lifetime (tl::nullopt), type (std::move (type))
     472        50620 :   {}
     473              : 
     474              :   // Lifetime-based self parameter (is ref, no type)
     475        59652 :   SelfParam (tl::optional<Lifetime> lifetime, bool is_mut, location_t locus)
     476        59652 :     : Param ({}, locus), has_ref (true), is_mut (is_mut),
     477        59652 :       lifetime (std::move (lifetime))
     478        59652 :   {}
     479              : 
     480              :   // Copy constructor requires clone
     481       686871 :   SelfParam (SelfParam const &other)
     482       686871 :     : Param (other.get_outer_attrs (), other.get_locus ()),
     483      1373742 :       has_ref (other.has_ref), is_mut (other.is_mut), lifetime (other.lifetime)
     484              :   {
     485       686871 :     node_id = other.node_id;
     486       686871 :     if (other.type != nullptr)
     487          626 :       type = other.type->clone_type ();
     488       686871 :   }
     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       906308 :   location_t get_locus () const { return locus; }
     515              : 
     516      7261412 :   bool is_self () const override { return true; }
     517              : 
     518       119218 :   bool get_has_ref () const { return has_ref; };
     519       115818 :   bool get_is_mut () const { return is_mut; }
     520              : 
     521            0 :   Lifetime get_lifetime () const { return lifetime.value (); }
     522      9500785 :   Lifetime &get_lifetime () { return lifetime.value (); }
     523              : 
     524       219437 :   NodeId get_node_id () const { return node_id; }
     525              : 
     526              :   // TODO: is this better? Or is a "vis_block" better?
     527        41652 :   Type &get_type ()
     528              :   {
     529        41652 :     rust_assert (has_type ());
     530        41652 :     return *type;
     531              :   }
     532              : 
     533        11199 :   std::unique_ptr<Type> &get_type_ptr ()
     534              :   {
     535        11199 :     rust_assert (has_type ());
     536        11199 :     return type;
     537              :   }
     538              : 
     539              :   void accept_vis (ASTVisitor &vis) override;
     540              : 
     541       577167 :   SelfParam *clone_param_impl () const override
     542              :   {
     543       577167 :     return new SelfParam (*this);
     544              :   }
     545              : };
     546              : 
     547              : // Qualifiers for function, i.e. const, unsafe, extern etc.
     548       611853 : 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       200430 :   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       200430 :     : default_status (default_status), async_status (async_status),
     564       200430 :       const_status (const_status), unsafe_status (unsafe_status),
     565       200430 :       has_extern (has_extern), extern_abi (std::move (extern_abi)),
     566       200430 :       locus (locus)
     567              :   {
     568       200430 :     if (!this->extern_abi.empty ())
     569              :       {
     570              :         // having extern is required; not having it is an implementation error
     571         7829 :         rust_assert (has_extern);
     572              :       }
     573       200430 :   }
     574              : 
     575              :   std::string as_string () const;
     576              : 
     577       322960 :   bool is_default () const { return default_status == Default::Yes; }
     578       206703 :   bool is_unsafe () const { return unsafe_status == Unsafety::Unsafe; }
     579       186228 :   bool is_extern () const { return has_extern; }
     580       193067 :   bool is_const () const { return const_status == Const::Yes; }
     581       364713 :   bool is_async () const { return async_status == Async::Yes; }
     582        13944 :   std::string get_extern_abi () const { return extern_abi; }
     583       164884 :   bool has_abi () const { return !extern_abi.empty (); }
     584       164809 :   Const get_const_status () const { return const_status; }
     585       164809 :   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       192385 :   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       192385 :       param_name (std::move (param_name)), type (std::move (param_type))
     669       192385 :   {}
     670              : 
     671              :   // Copy constructor uses clone
     672       950853 :   FunctionParam (FunctionParam const &other)
     673       950853 :     : Param (other.get_outer_attrs (), other.locus)
     674              :   {
     675              :     // guard to prevent nullptr dereference
     676       950853 :     if (other.param_name != nullptr)
     677       950853 :       param_name = other.param_name->clone_pattern ();
     678       950853 :     if (other.type != nullptr)
     679       950853 :       type = other.type->clone_type ();
     680       950853 :   }
     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       187694 :   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     44386019 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
     718       950853 :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
     719              : 
     720              :   // TODO: is this better? Or is a "vis_block" better?
     721     38441571 :   Pattern &get_pattern ()
     722              :   {
     723     38441571 :     rust_assert (param_name != nullptr);
     724     38441571 :     return *param_name;
     725              :   }
     726              : 
     727     12259737 :   std::unique_ptr<Pattern> &get_pattern_ptr ()
     728              :   {
     729     12259737 :     rust_assert (param_name != nullptr);
     730     12259737 :     return param_name;
     731              :   }
     732              : 
     733     35047747 :   bool has_name () const { return param_name != nullptr; }
     734              : 
     735              :   // TODO: is this better? Or is a "vis_block" better?
     736     38544804 :   Type &get_type ()
     737              :   {
     738     38544804 :     rust_assert (type != nullptr);
     739     38544804 :     return *type;
     740              :   }
     741              : 
     742     12261230 :   std::unique_ptr<Type> &get_type_ptr ()
     743              :   {
     744     12261230 :     rust_assert (type != nullptr);
     745     12261230 :     return type;
     746              :   }
     747              : 
     748       847698 :   FunctionParam *clone_param_impl () const override
     749              :   {
     750       847698 :     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       544976 :   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         9796 :   void clone_items (const std::vector<std::unique_ptr<Item>> &other_items)
     793              :   {
     794         9796 :     items.reserve (other_items.size ());
     795       300422 :     for (const auto &e : other_items)
     796       290626 :       items.push_back (e->clone_item ());
     797         9796 :   }
     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         2645 :   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         2645 :       module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED),
     812         2645 :       safety (safety), outer_filename (outer_filename),
     813         2645 :       inner_attrs (std::vector<Attribute> ()),
     814         2645 :       items (std::vector<std::unique_ptr<Item>> ()),
     815         2645 :       module_scope (std::move (module_scope))
     816         2645 :   {}
     817              : 
     818              :   // Loaded module constructor, with items
     819         2646 :   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         2646 :       module_name (name), locus (locus), kind (ModuleKind::LOADED),
     827         2646 :       safety (safety), outer_filename (std::string ()),
     828         2646 :       inner_attrs (std::move (inner_attrs)), items (std::move (items))
     829         2646 :   {}
     830              : 
     831              :   // Copy constructor with vector clone
     832        10633 :   Module (Module const &other)
     833        10633 :     : VisItem (other), module_name (other.module_name), locus (other.locus),
     834        10633 :       kind (other.kind), safety (other.safety), inner_attrs (other.inner_attrs),
     835        10633 :       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        10633 :     if (other.kind == LOADED)
     840         9796 :       clone_items (other.items);
     841              :     else
     842          837 :       items.clear ();
     843        10633 :   }
     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       104784 :   enum ModuleKind get_kind () const { return kind; }
     880              : 
     881         4464 :   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       562844 :   std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
     886              : 
     887              :   const std::vector<std::unique_ptr<Item>> &get_items () const { return items; }
     888       668450 :   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       113496 :   location_t get_locus () const override final { return locus; }
     907              : 
     908              :   // Invalid if name is empty, so base stripping on that.
     909         1632 :   void mark_for_strip () override { module_name = {""}; }
     910       104270 :   bool is_marked_for_strip () const override { return module_name.empty (); }
     911              : 
     912         2478 :   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         6169 :   Module *clone_item_impl () const override { return new Module (*this); }
     918              : 
     919       149433 :   GlobContainer::Kind get_glob_container_kind () const override
     920              :   {
     921       149433 :     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         8656 :   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        54724 :   std::unique_ptr<UseTree> clone_use_tree () const
    1023              :   {
    1024        54724 :     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        11422 :   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        21564 :   UseTree (location_t locus)
    1040        21564 :     : locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
    1041        21564 :   {}
    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         2106 :   UseTreeGlob (PathType glob_type, SimplePath path, location_t locus)
    1061         2106 :     : UseTree (locus), glob_type (glob_type), path (std::move (path))
    1062              :   {
    1063         2106 :     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         2106 :   }
    1072              : 
    1073              :   /* Returns whether has path. Should be made redundant by PathType
    1074              :    * PATH_PREFIXED. */
    1075        46991 :   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        74498 :   Kind get_kind () const override { return Glob; }
    1084              : 
    1085        46969 :   SimplePath get_path () const
    1086              :   {
    1087        46969 :     rust_assert (has_path ());
    1088        46969 :     return path;
    1089              :   }
    1090              : 
    1091        87646 :   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         4672 :   UseTreeGlob *clone_use_tree_impl () const override
    1099              :   {
    1100         4672 :     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         3293 :   UseTreeList (PathType path_type, SimplePath path,
    1123              :                std::vector<std::unique_ptr<UseTree>> trees, location_t locus)
    1124         3293 :     : UseTree (locus), path_type (path_type), path (std::move (path)),
    1125         3293 :       trees (std::move (trees))
    1126              :   {
    1127         3293 :     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         3293 :   }
    1136              : 
    1137              :   // copy constructor with vector clone
    1138         8656 :   UseTreeList (UseTreeList const &other)
    1139         8656 :     : UseTree (other), path_type (other.path_type), path (other.path)
    1140              :   {
    1141         8656 :     trees.reserve (other.trees.size ());
    1142        35756 :     for (const auto &e : other.trees)
    1143        27100 :       trees.push_back (e->clone_use_tree ());
    1144         8656 :   }
    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       184749 :   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       184747 :   Kind get_kind () const override { return List; }
    1177        99143 :   SimplePath get_path () const
    1178              :   {
    1179        99143 :     rust_assert (has_path ());
    1180        99143 :     return path;
    1181              :   }
    1182              : 
    1183       276410 :   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        99147 :     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         8656 :   UseTreeList *clone_use_tree_impl () const override
    1198              :   {
    1199         8656 :     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        16165 :   UseTreeRebind (NewBindType bind_type, SimplePath path, location_t locus,
    1222              :                  Identifier identifier = std::string ())
    1223        16165 :     : UseTree (locus), path (std::move (path)), bind_type (bind_type),
    1224        16165 :       identifier (std::move (identifier))
    1225        16165 :   {}
    1226              : 
    1227              :   // Returns whether has path (this should always be true).
    1228       461573 :   bool has_path () const { return !path.is_empty (); }
    1229              : 
    1230              :   // Returns whether has identifier (or, rather, is allowed to).
    1231         4689 :   bool has_identifier () const { return bind_type == IDENTIFIER; }
    1232              : 
    1233              :   std::string as_string () const override;
    1234              : 
    1235       526992 :   NewBindType get_new_bind_type () const { return bind_type; }
    1236              : 
    1237              :   void accept_vis (ASTVisitor &vis) override;
    1238              : 
    1239       636403 :   Kind get_kind () const override { return Rebind; }
    1240              : 
    1241       461573 :   const SimplePath &get_path () const
    1242              :   {
    1243       461573 :     rust_assert (has_path ());
    1244       461573 :     return path;
    1245              :   }
    1246              : 
    1247       567237 :   SimplePath &get_path () { return path; }
    1248              : 
    1249         4689 :   const Identifier &get_identifier () const
    1250              :   {
    1251         4689 :     rust_assert (has_identifier ());
    1252         4689 :     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        41396 :   virtual UseTreeRebind *clone_use_tree_impl () const override
    1261              :   {
    1262        41396 :     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        11364 :   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        11364 :       use_tree (std::move (use_tree)), locus (locus)
    1279        11364 :   {}
    1280              : 
    1281              :   // Copy constructor with clone
    1282        27624 :   UseDeclaration (UseDeclaration const &other)
    1283        27624 :     : VisItem (other), locus (other.locus)
    1284              :   {
    1285              :     // guard to prevent null dereference (only required if error state)
    1286        27624 :     if (other.use_tree != nullptr)
    1287        27624 :       use_tree = other.use_tree->clone_use_tree ();
    1288        27624 :   }
    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          848 :   location_t get_locus () const override final { return locus; }
    1312              : 
    1313      1691617 :   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          557 :   void mark_for_strip () override { use_tree = nullptr; }
    1321       303317 :   bool is_marked_for_strip () const override { return use_tree == nullptr; }
    1322              : 
    1323         4572 :   Item::Kind get_item_kind () const override
    1324              :   {
    1325         4572 :     return Item::Kind::UseDeclaration;
    1326              :   }
    1327              : 
    1328              : protected:
    1329              :   /* Use covariance to implement clone function as returning this object
    1330              :    * rather than base */
    1331        18186 :   UseDeclaration *clone_item_impl () const override
    1332              :   {
    1333        18186 :     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       192611 :   bool has_generics () const { return !generic_params.empty (); }
    1357              : 
    1358              :   // Returns whether function has regular parameters.
    1359       171407 :   bool has_function_params () const { return !function_params.empty (); }
    1360              : 
    1361              :   // Returns whether function has return type - if not, it is void.
    1362     44456683 :   bool has_return_type () const { return return_type != nullptr; }
    1363              : 
    1364              :   // Returns whether function has a where clause.
    1365     35393022 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    1366              : 
    1367       288084 :   bool has_self_param () const
    1368              :   {
    1369       288084 :     return function_params.size () > 0 && function_params[0]->is_self ();
    1370              :   }
    1371              : 
    1372     50740299 :   bool has_body () const { return function_body.has_value (); }
    1373              : 
    1374       151313 :   bool is_default () const { return qualifiers.is_default (); }
    1375              : 
    1376              :   // Mega-constructor with all possible fields
    1377       192834 :   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       192834 :       ExternalItem (Stmt::node_id), qualifiers (std::move (qualifiers)),
    1386       192834 :       function_name (std::move (function_name)),
    1387       192834 :       generic_params (std::move (generic_params)),
    1388       192834 :       function_params (std::move (function_params)),
    1389       192834 :       return_type (std::move (return_type)),
    1390       192834 :       where_clause (std::move (where_clause)),
    1391       192834 :       function_body (std::move (function_body)), locus (locus),
    1392       192834 :       is_external_function (is_external_function)
    1393       192834 :   {}
    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      5534937 :   location_t get_locus () const override final { return locus; }
    1408              : 
    1409              :   void accept_vis (ASTVisitor &vis) override;
    1410              : 
    1411        15598 :   bool is_variadic () const
    1412              :   {
    1413        15598 :     return function_params.size () != 0
    1414        15598 :            && function_params.back ()->is_variadic ();
    1415              :   }
    1416              : 
    1417       343294 :   bool is_external () const { return is_external_function; }
    1418              : 
    1419              :   // Invalid if block is null, so base stripping on that.
    1420          249 :   void mark_for_strip () override { function_body = nullptr; }
    1421      5219349 :   bool is_marked_for_strip () const override
    1422              :   {
    1423      5219349 :     return function_body == nullptr;
    1424              :   }
    1425              : 
    1426     13939980 :   std::vector<std::unique_ptr<Param>> &get_function_params ()
    1427              :   {
    1428     45091402 :     return function_params;
    1429              :   }
    1430              :   const std::vector<std::unique_ptr<Param>> &get_function_params () const
    1431              :   {
    1432              :     return function_params;
    1433              :   }
    1434              : 
    1435        18643 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    1436              :   {
    1437     35390328 :     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     45773330 :   tl::optional<std::unique_ptr<BlockExpr>> &get_definition ()
    1446              :   {
    1447     45773330 :     return function_body;
    1448              :   }
    1449              : 
    1450              :   const FunctionQualifiers &get_qualifiers () const { return qualifiers; }
    1451              : 
    1452     30696584 :   FunctionQualifiers &get_qualifiers () { return qualifiers; }
    1453              : 
    1454     15761793 :   Identifier get_function_name () const { return function_name; }
    1455              : 
    1456              :   // TODO: is this better? Or is a "vis_block" better?
    1457      1265648 :   WhereClause &get_where_clause () { return where_clause; }
    1458              : 
    1459              :   // TODO: is this better? Or is a "vis_block" better?
    1460     29651692 :   Type &get_return_type ()
    1461              :   {
    1462     29651692 :     rust_assert (has_return_type ());
    1463     29651692 :     return *return_type;
    1464              :   }
    1465              : 
    1466      8886480 :   std::unique_ptr<Type> &get_return_type_ptr ()
    1467              :   {
    1468      8886480 :     rust_assert (has_return_type ());
    1469      8886480 :     return return_type;
    1470              :   }
    1471              : 
    1472       109705 :   Param &get_self_param ()
    1473              :   {
    1474       109705 :     rust_assert (has_self_param ());
    1475       109705 :     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     37189124 :   NodeId get_node_id () const override { return Stmt::node_id; }
    1485              : 
    1486      1278837 :   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        79370 :   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       668563 :   Function *clone_associated_item_impl () const override
    1496              :   {
    1497       668563 :     return new Function (*this);
    1498              :   }
    1499              : 
    1500              :   /* Use covariance to implement clone function as returning this object
    1501              :    * rather than base */
    1502        40782 :   Function *clone_external_item_impl () const override
    1503              :   {
    1504        40782 :     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        34936 :   bool has_generics () const { return !generic_params.empty (); }
    1529              : 
    1530              :   // Returns whether type alias has a where clause.
    1531      7543855 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    1532              : 
    1533              :   // Mega-constructor with all possible fields
    1534        33572 :   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        33572 :       new_type_name (std::move (new_type_name)),
    1541        33572 :       generic_params (std::move (generic_params)),
    1542        33572 :       where_clause (std::move (where_clause)),
    1543        33572 :       existing_type (std::move (existing_type)), locus (locus)
    1544        33572 :   {}
    1545              : 
    1546              :   // Copy constructor
    1547       252517 :   TypeAlias (TypeAlias const &other)
    1548       252517 :     : VisItem (other), new_type_name (other.new_type_name),
    1549       252517 :       where_clause (other.where_clause), locus (other.locus)
    1550              :   {
    1551              :     // guard to prevent null dereference (only required if error state)
    1552       252517 :     if (other.existing_type != nullptr)
    1553       252517 :       existing_type = other.existing_type->clone_type ();
    1554              : 
    1555       252517 :     generic_params.reserve (other.generic_params.size ());
    1556       252591 :     for (const auto &e : other.generic_params)
    1557           74 :       generic_params.push_back (e->clone_generic_param ());
    1558       252517 :   }
    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      1107988 :   location_t get_locus () const override final { return locus; }
    1588              : 
    1589              :   // needed to override AssociatedItem::get_node_id
    1590      6438306 :   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      1073171 :   bool is_marked_for_strip () const override
    1597              :   {
    1598      1073171 :     return existing_type == nullptr;
    1599              :   }
    1600              : 
    1601           48 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    1602              :   {
    1603      7542429 :     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      7540657 :   Type &get_type_aliased ()
    1615              :   {
    1616      7540657 :     rust_assert (existing_type != nullptr);
    1617      7540657 :     return *existing_type;
    1618              :   }
    1619              : 
    1620      2112490 :   std::unique_ptr<Type> &get_type_aliased_ptr ()
    1621              :   {
    1622      2112490 :     rust_assert (existing_type != nullptr);
    1623      2112490 :     return existing_type;
    1624              :   }
    1625              : 
    1626      5399943 :   Identifier get_new_type_name () const { return new_type_name; }
    1627              : 
    1628          930 :   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          393 :   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       218567 :   TypeAlias *clone_associated_item_impl () const override
    1638              :   {
    1639       218567 :     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        13934 :   bool has_generics () const { return !generic_params.empty (); }
    1792              : 
    1793              :   // Returns whether struct has a where clause.
    1794      2888731 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    1795              : 
    1796       517593 :   location_t get_locus () const override final { return locus; }
    1797              : 
    1798              :   // Invalid if name is empty, so base stripping on that.
    1799          126 :   void mark_for_strip () override { struct_name = {""}; }
    1800       734410 :   bool is_marked_for_strip () const override { return struct_name.empty (); }
    1801              : 
    1802      1707758 :   Identifier get_struct_name () const { return struct_name; }
    1803              : 
    1804              :   virtual size_t get_field_count () const = 0;
    1805              : 
    1806        12547 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    1807              :   {
    1808      2898646 :     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        32011 :   WhereClause &get_where_clause () { return where_clause; }
    1817              : 
    1818        19220 :   Identifier get_identifier () const { return struct_name; }
    1819              : 
    1820        89752 :   Item::Kind get_item_kind () const override { return Item::Kind::Struct; }
    1821              : 
    1822              : protected:
    1823        11807 :   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        23614 :       struct_name (std::move (struct_name)),
    1829        11807 :       generic_params (std::move (generic_params)),
    1830        11807 :       where_clause (std::move (where_clause)), locus (locus)
    1831        11807 :   {}
    1832              : 
    1833              :   // Copy constructor with vector clone
    1834        56916 :   Struct (Struct const &other)
    1835        56916 :     : VisItem (other), struct_name (other.struct_name),
    1836        56916 :       where_clause (other.where_clause), locus (other.locus)
    1837              :   {
    1838        56916 :     generic_params.reserve (other.generic_params.size ());
    1839        72316 :     for (const auto &e : other.generic_params)
    1840        15400 :       generic_params.push_back (e->clone_generic_param ());
    1841        56916 :   }
    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         7963 :   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         7963 :     : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
    1890         7963 :       field_name (std::move (field_name)), field_type (std::move (field_type)),
    1891         7963 :       node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
    1892         7963 :   {}
    1893              : 
    1894              :   // Copy constructor
    1895        21768 :   StructField (StructField const &other)
    1896        21768 :     : outer_attrs (other.outer_attrs), visibility (other.visibility),
    1897        21768 :       field_name (other.field_name), node_id (other.node_id),
    1898        21768 :       locus (other.locus)
    1899              :   {
    1900              :     // guard to prevent null dereference
    1901        21768 :     if (other.field_type != nullptr)
    1902        21768 :       field_type = other.field_type->clone_type ();
    1903        21768 :   }
    1904              : 
    1905        33738 :   ~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        11579 :   StructField (StructField &&other) = default;
    1926           24 :   StructField &operator= (StructField &&other) = default;
    1927              : 
    1928              :   // Returns whether struct field is in an error state.
    1929         7190 :   bool is_error () const
    1930              :   {
    1931         7190 :     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      1399570 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
    1946              :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
    1947              : 
    1948        13367 :   Identifier get_field_name () const { return field_name; }
    1949              : 
    1950         9867 :   location_t get_locus () const { return locus; }
    1951              : 
    1952              :   // TODO: is this better? Or is a "vis_block" better?
    1953      1062993 :   Type &get_field_type ()
    1954              :   {
    1955      1062993 :     rust_assert (field_type != nullptr);
    1956      1062993 :     return *field_type;
    1957              :   }
    1958              : 
    1959       344590 :   std::unique_ptr<Type> &get_field_type_ptr ()
    1960              :   {
    1961       344590 :     rust_assert (field_type != nullptr);
    1962       344590 :     return field_type;
    1963              :   }
    1964              : 
    1965      1157529 :   Visibility &get_visibility () { return visibility; }
    1966              :   const Visibility &get_visibility () const { return visibility; }
    1967              : 
    1968         7530 :   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         4136 :   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         4136 :       fields (std::move (fields)), is_unit (is_unit)
    1989         4136 :   {}
    1990              : 
    1991              :   // Unit struct constructor
    1992         5312 :   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         5312 :       is_unit (true)
    2000         5312 :   {}
    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       265268 :   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      3913111 :   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        38305 :   StructStruct *clone_item_impl () const override
    2019              :   {
    2020        38305 :     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         8416 :   TupleField (std::unique_ptr<Type> field_type, Visibility vis,
    2049              :               location_t locus,
    2050              :               std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
    2051         8416 :     : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
    2052         8416 :       field_type (std::move (field_type)),
    2053         8416 :       node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
    2054         8416 :   {}
    2055              : 
    2056              :   // Copy constructor with clone
    2057        43258 :   TupleField (TupleField const &other)
    2058        43258 :     : outer_attrs (other.outer_attrs), visibility (other.visibility),
    2059        43258 :       node_id (other.node_id), locus (other.locus)
    2060              :   {
    2061              :     // guard to prevent null dereference (only required if error)
    2062        43258 :     if (other.field_type != nullptr)
    2063        43258 :       field_type = other.field_type->clone_type ();
    2064        43258 :   }
    2065              : 
    2066        57071 :   ~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        13786 :   TupleField (TupleField &&other) = default;
    2087            0 :   TupleField &operator= (TupleField &&other) = default;
    2088              : 
    2089              :   // Returns whether tuple field is in an error state.
    2090         8416 :   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         8322 :   NodeId get_node_id () const { return node_id; }
    2101              : 
    2102      1302656 :   Visibility &get_visibility () { return visibility; }
    2103              :   const Visibility &get_visibility () const { return visibility; }
    2104              : 
    2105         8322 :   location_t get_locus () const { return locus; }
    2106              : 
    2107              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2108      1528696 :   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      1117346 :   Type &get_field_type ()
    2113              :   {
    2114      1117346 :     rust_assert (field_type != nullptr);
    2115      1117346 :     return *field_type;
    2116              :   }
    2117              : 
    2118       419162 :   std::unique_ptr<Type> &get_field_type_ptr ()
    2119              :   {
    2120       419162 :     rust_assert (field_type != nullptr);
    2121       419162 :     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         2359 :   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         2359 :       fields (std::move (fields))
    2142         2359 :   {}
    2143              : 
    2144              :   void accept_vis (ASTVisitor &vis) override;
    2145              : 
    2146              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2147       337470 :   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         7184 :   TupleStruct *clone_item_impl () const override
    2156              :   {
    2157         7184 :     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        10189 :   virtual ~EnumItem () {}
    2207              : 
    2208         2571 :   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         2571 :       variant_name (std::move (variant_name)), locus (locus)
    2212         2571 :   {}
    2213              : 
    2214         1723 :   virtual Kind get_enum_item_kind () const { return Kind::Identifier; }
    2215              : 
    2216              :   // Unique pointer custom clone function
    2217         6882 :   std::unique_ptr<EnumItem> clone_enum_item () const
    2218              :   {
    2219         6882 :     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        47391 :   location_t get_locus () const override { return locus; }
    2228              : 
    2229       136834 :   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        43668 :   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         3301 :   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          877 :   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          877 :       tuple_fields (std::move (tuple_fields))
    2257          877 :   {}
    2258              : 
    2259         1012 :   EnumItem::Kind get_enum_item_kind () const override
    2260              :   {
    2261         1012 :     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        98543 :   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         2329 :   EnumItemTuple *clone_item_impl () const override
    2278              :   {
    2279         2329 :     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          332 :   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          332 :       expression (std::move (expr))
    2337          332 :   {}
    2338              : 
    2339              :   // Copy constructor with clone
    2340         1037 :   EnumItemDiscriminant (EnumItemDiscriminant const &other)
    2341         1037 :     : EnumItem (other), expression (other.expression->clone_expr ())
    2342         1037 :   {}
    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          117 :   EnumItem::Kind get_enum_item_kind () const override
    2360              :   {
    2361          117 :     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        17174 :   Expr &get_expr ()
    2372              :   {
    2373        17174 :     rust_assert (expression != nullptr);
    2374        17174 :     return *expression;
    2375              :   }
    2376              : 
    2377         4292 :   std::unique_ptr<Expr> &get_expr_ptr ()
    2378              :   {
    2379         4292 :     rust_assert (expression != nullptr);
    2380         4292 :     return expression;
    2381              :   }
    2382              : 
    2383              : protected:
    2384              :   // Clone function implementation as (not pure) virtual method
    2385         1037 :   EnumItemDiscriminant *clone_item_impl () const override
    2386              :   {
    2387         1037 :     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         1582 :   bool has_generics () const { return !generic_params.empty (); }
    2412              : 
    2413              :   // Returns whether "enum" has a where clause.
    2414        95172 :   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         1044 :   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         1044 :       enum_name (std::move (enum_name)),
    2427         1044 :       generic_params (std::move (generic_params)),
    2428         1044 :       where_clause (std::move (where_clause)), items (std::move (items)),
    2429         1044 :       locus (locus)
    2430         1044 :   {}
    2431              : 
    2432              :   // TODO: constructor with less arguments
    2433              : 
    2434              :   // Copy constructor with vector clone
    2435         2774 :   Enum (Enum const &other)
    2436         2774 :     : VisItem (other), enum_name (other.enum_name),
    2437         2774 :       where_clause (other.where_clause), locus (other.locus)
    2438              :   {
    2439         2774 :     generic_params.reserve (other.generic_params.size ());
    2440         4138 :     for (const auto &e : other.generic_params)
    2441         1364 :       generic_params.push_back (e->clone_generic_param ());
    2442              : 
    2443         2774 :     items.reserve (other.items.size ());
    2444         9656 :     for (const auto &e : other.items)
    2445         6882 :       items.push_back (e->clone_enum_item ());
    2446         2774 :   }
    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        17680 :   location_t get_locus () const override final { return locus; }
    2472              : 
    2473              :   void accept_vis (ASTVisitor &vis) override;
    2474              : 
    2475        80514 :   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        14283 :   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       111462 :   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         2064 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    2489              :   {
    2490        96670 :     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         6930 :   Item::Kind get_item_kind () const override { return Item::Kind::Enum; }
    2501              : 
    2502         4950 :   GlobContainer::Kind get_glob_container_kind () const override
    2503              :   {
    2504         4950 :     return GlobContainer::Kind::Enum;
    2505              :   }
    2506              : 
    2507              : protected:
    2508              :   /* Use covariance to implement clone function as returning this object
    2509              :    * rather than base */
    2510         1733 :   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          234 :   bool has_generics () const { return !generic_params.empty (); }
    2534              : 
    2535              :   // Returns whether union has where clause.
    2536         7786 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    2537              : 
    2538          139 :   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          278 :       union_name (std::move (union_name)),
    2544          139 :       generic_params (std::move (generic_params)),
    2545          139 :       where_clause (std::move (where_clause)), variants (std::move (variants)),
    2546          139 :       locus (locus)
    2547          139 :   {}
    2548              : 
    2549              :   // copy constructor with vector clone
    2550          299 :   Union (Union const &other)
    2551          299 :     : VisItem (other), union_name (other.union_name),
    2552          299 :       where_clause (other.where_clause), variants (other.variants),
    2553          299 :       locus (other.locus)
    2554              :   {
    2555          299 :     generic_params.reserve (other.generic_params.size ());
    2556          529 :     for (const auto &e : other.generic_params)
    2557          230 :       generic_params.push_back (e->clone_generic_param ());
    2558          299 :   }
    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         1475 :   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         1079 :   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         9139 :   std::vector<StructField> &get_variants () { return variants; }
    2590              :   const std::vector<StructField> &get_variants () const { return variants; }
    2591              : 
    2592          190 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    2593              :   {
    2594         7875 :     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         6327 :   Identifier get_identifier () const { return union_name; }
    2605              : 
    2606         5246 :   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          160 :   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         9190 :   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         9190 :       identifier (std::move (ident)), type (std::move (type)),
    2636         9190 :       const_expr (std::move (const_expr)), locus (locus)
    2637         9190 :   {}
    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        38147 :   ConstantItem (ConstantItem const &other)
    2647        38147 :     : VisItem (other), identifier (other.identifier), locus (other.locus)
    2648              :   {
    2649              :     // guard to prevent null dereference (only required if error state)
    2650        38147 :     if (other.type != nullptr)
    2651        38147 :       type = other.type->clone_type ();
    2652        38147 :     if (other.const_expr != nullptr)
    2653        36930 :       const_expr = other.const_expr->clone_expr ();
    2654        38147 :   }
    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       313282 :   location_t get_locus () const override final { return locus; }
    2685              : 
    2686              :   // needed to override AssociatedItem::get_node_id
    2687      1228226 :   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           12 :   void mark_for_strip () override
    2693              :   {
    2694           12 :     type = nullptr;
    2695           12 :     const_expr = nullptr;
    2696           12 :   }
    2697       302790 :   bool is_marked_for_strip () const override
    2698              :   {
    2699       302790 :     return type == nullptr && const_expr == nullptr;
    2700              :   }
    2701              : 
    2702      2336014 :   bool has_expr () const { return const_expr != nullptr; }
    2703              : 
    2704              :   // TODO: is this better? Or is a "vis_block" better?
    2705      1672188 :   Expr &get_expr ()
    2706              :   {
    2707      1672188 :     rust_assert (const_expr != nullptr);
    2708      1672188 :     return *const_expr;
    2709              :   }
    2710              : 
    2711       581592 :   std::unique_ptr<Expr> &get_expr_ptr ()
    2712              :   {
    2713       581592 :     rust_assert (const_expr != nullptr);
    2714       581592 :     return const_expr;
    2715              :   }
    2716              : 
    2717              :   // TODO: is this better? Or is a "vis_block" better?
    2718      1730710 :   Type &get_type ()
    2719              :   {
    2720      1730710 :     rust_assert (type != nullptr);
    2721      1730710 :     return *type;
    2722              :   }
    2723              : 
    2724       597959 :   std::unique_ptr<Type> &get_type_ptr ()
    2725              :   {
    2726       597959 :     rust_assert (type != nullptr);
    2727       597959 :     return type;
    2728              :   }
    2729              : 
    2730       920243 :   const Identifier &get_identifier () const { return identifier; }
    2731              : 
    2732        26277 :   Item::Kind get_item_kind () const override
    2733              :   {
    2734        26277 :     return Item::Kind::ConstantItem;
    2735              :   }
    2736              : 
    2737              : protected:
    2738              :   /* Use covariance to implement clone function as returning this object
    2739              :    * rather than base */
    2740        18912 :   ConstantItem *clone_item_impl () const override
    2741              :   {
    2742        18912 :     return new ConstantItem (*this);
    2743              :   }
    2744              : 
    2745              :   /* Use covariance to implement clone function as returning this object
    2746              :    * rather than base */
    2747        10121 :   ConstantItem *clone_associated_item_impl () const override
    2748              :   {
    2749        10121 :     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          518 :   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          518 :     : VisItem (std::move (vis), std::move (outer_attrs)), has_mut (is_mut),
    2770         1036 :       name (std::move (name)), type (std::move (type)), expr (std::move (expr)),
    2771          518 :       locus (locus)
    2772          518 :   {}
    2773              : 
    2774              :   // Copy constructor with clone
    2775         1403 :   StaticItem (StaticItem const &other)
    2776         1403 :     : VisItem (other), has_mut (other.has_mut), name (other.name),
    2777         1403 :       locus (other.locus)
    2778              :   {
    2779              :     // guard to prevent null dereference (only required if error state)
    2780         1403 :     if (other.type != nullptr)
    2781         1403 :       type = other.type->clone_type ();
    2782         1403 :     if (other.expr != nullptr)
    2783         1403 :       expr = other.expr->clone_expr ();
    2784         1403 :   }
    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        15998 :   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        14890 :   bool is_marked_for_strip () const override
    2822              :   {
    2823        14890 :     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        78725 :   Expr &get_expr ()
    2830              :   {
    2831        78725 :     rust_assert (expr != nullptr);
    2832        78725 :     return *expr;
    2833              :   }
    2834              : 
    2835        30289 :   std::unique_ptr<Expr> &get_expr_ptr ()
    2836              :   {
    2837        30289 :     rust_assert (expr != nullptr);
    2838        30289 :     return expr;
    2839              :   }
    2840              : 
    2841              :   // TODO: is this better? Or is a "vis_block" better?
    2842        78725 :   Type &get_type ()
    2843              :   {
    2844        78725 :     rust_assert (type != nullptr);
    2845        78725 :     return *type;
    2846              :   }
    2847              : 
    2848        30289 :   std::unique_ptr<Type> &get_type_ptr ()
    2849              :   {
    2850        30289 :     rust_assert (type != nullptr);
    2851        30289 :     return type;
    2852              :   }
    2853              : 
    2854          566 :   bool is_mutable () const { return has_mut; }
    2855              : 
    2856        46777 :   Identifier get_identifier () const { return name; }
    2857              : 
    2858           93 :   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         1403 :   StaticItem *clone_item_impl () const override
    2864              :   {
    2865         1403 :     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         1328 :   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         1328 :     : TraitItem (vis, locus), outer_attrs (std::move (outer_attrs)),
    2896         1328 :       name (std::move (name)), generic_params (std::move (generic_params)),
    2897         1328 :       type_param_bounds (std::move (type_param_bounds))
    2898         1328 :   {}
    2899              : 
    2900              :   // Copy constructor with vector clone
    2901         3313 :   TraitItemType (TraitItemType const &other)
    2902         6626 :     : TraitItem (other.locus), outer_attrs (other.outer_attrs),
    2903         3313 :       name (other.name)
    2904              :   {
    2905         3313 :     node_id = other.node_id;
    2906         3313 :     generic_params.reserve (other.generic_params.size ());
    2907         3349 :     for (const auto &e : other.generic_params)
    2908           36 :       generic_params.push_back (e->clone_generic_param ());
    2909         3313 :     type_param_bounds.reserve (other.type_param_bounds.size ());
    2910         4094 :     for (const auto &e : other.type_param_bounds)
    2911          781 :       type_param_bounds.push_back (e->clone_type_param_bound ());
    2912         3313 :   }
    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        19131 :   bool is_marked_for_strip () const override { return name.empty (); }
    2944              : 
    2945              :   // TODO: this mutable getter seems really dodgy. Think up better way.
    2946       175143 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
    2947         1326 :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
    2948              : 
    2949              :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    2950              :   {
    2951       137393 :     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       137393 :     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        21255 :   Identifier get_identifier () const { return name; }
    2969              : 
    2970              : protected:
    2971              :   // Clone function implementation as (not pure) virtual method
    2972         3313 :   TraitItemType *clone_associated_item_impl () const override
    2973              :   {
    2974         3313 :     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        10564 :   bool has_generics () const { return !generic_params.empty (); }
    2997              : 
    2998              :   // Returns whether trait has type parameter bounds.
    2999         6314 :   bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
    3000              : 
    3001              :   // Returns whether trait has where clause.
    3002       458964 :   bool has_where_clause () const { return !where_clause.is_empty (); }
    3003              : 
    3004              :   // Returns whether trait has trait items.
    3005           78 :   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       374943 :   Identifier get_identifier () const { return name; }
    3011              : 
    3012         6236 :   bool is_unsafe () const { return has_unsafe; }
    3013        18784 :   bool is_auto () const { return has_auto; }
    3014              : 
    3015              :   // Mega-constructor
    3016         6331 :   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        12662 :       has_unsafe (is_unsafe), has_auto (is_auto), name (std::move (name)),
    3025        12662 :       generic_params (std::move (generic_params)), self_param ({"Self"}, locus),
    3026         6331 :       type_param_bounds (std::move (type_param_bounds)),
    3027         6331 :       where_clause (std::move (where_clause)),
    3028         6331 :       inner_attrs (std::move (inner_attrs)),
    3029         6331 :       trait_items (std::move (trait_items)), locus (locus)
    3030         6331 :   {}
    3031              : 
    3032              :   // Copy constructor with vector clone
    3033        15104 :   Trait (Trait const &other)
    3034        15104 :     : VisItem (other), has_unsafe (other.has_unsafe), has_auto (other.has_auto),
    3035        15104 :       name (other.name), self_param (other.self_param),
    3036        15104 :       where_clause (other.where_clause), inner_attrs (other.inner_attrs),
    3037        30208 :       locus (other.locus)
    3038              :   {
    3039        15104 :     generic_params.reserve (other.generic_params.size ());
    3040        19142 :     for (const auto &e : other.generic_params)
    3041         4038 :       generic_params.push_back (e->clone_generic_param ());
    3042              : 
    3043        15104 :     type_param_bounds.reserve (other.type_param_bounds.size ());
    3044        19837 :     for (const auto &e : other.type_param_bounds)
    3045         4733 :       type_param_bounds.push_back (e->clone_type_param_bound ());
    3046              : 
    3047        15104 :     trait_items.reserve (other.trait_items.size ());
    3048        39126 :     for (const auto &e : other.trait_items)
    3049        24022 :       trait_items.push_back (e->clone_associated_item ());
    3050        15104 :   }
    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        87324 :   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        66577 :   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       458995 :   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       137411 :   std::vector<std::unique_ptr<AssociatedItem>> &get_trait_items ()
    3100              :   {
    3101       536047 :     return trait_items;
    3102              :   }
    3103              : 
    3104         2137 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    3105              :   {
    3106       461101 :     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       466889 :     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       392399 :   AST::TypeParam &get_implicit_self () { return self_param; }
    3126              : 
    3127       121183 :   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         8830 :   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        93603 :   bool has_generics () const { return !generic_params.empty (); }
    3159              : 
    3160              :   // Returns whether impl has where clause.
    3161     17646430 :   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       101527 :   location_t get_locus () const override final { return locus; }
    3167              : 
    3168              :   // Invalid if trait type is null, so base stripping on that.
    3169          218 :   void mark_for_strip () override { trait_type = nullptr; }
    3170      2812381 :   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     20547180 :   std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
    3175              : 
    3176        39517 :   std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
    3177              :   {
    3178     17678283 :     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       926350 :   WhereClause &get_where_clause () { return where_clause; }
    3187              : 
    3188              :   // TODO: is this better? Or is a "vis_block" better?
    3189     17862697 :   Type &get_type ()
    3190              :   {
    3191     17862697 :     rust_assert (trait_type != nullptr);
    3192     17862697 :     return *trait_type;
    3193              :   }
    3194              : 
    3195      5728087 :   std::unique_ptr<Type> &get_type_ptr ()
    3196              :   {
    3197      5728087 :     rust_assert (trait_type != nullptr);
    3198      5728087 :     return trait_type;
    3199              :   }
    3200              : 
    3201       151304 :   Item::Kind get_item_kind () const override { return Item::Kind::Impl; }
    3202              : 
    3203              : protected:
    3204              :   // Mega-constructor
    3205        94062 :   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        94062 :       generic_params (std::move (generic_params)),
    3211        94062 :       trait_type (std::move (trait_type)),
    3212        94062 :       where_clause (std::move (where_clause)),
    3213        94062 :       inner_attrs (std::move (inner_attrs)), locus (locus)
    3214        94062 :   {}
    3215              : 
    3216              :   // Copy constructor
    3217       518374 :   Impl (Impl const &other)
    3218       518374 :     : VisItem (other), where_clause (other.where_clause),
    3219       518374 :       inner_attrs (other.inner_attrs), locus (other.locus)
    3220              :   {
    3221              :     // guard to prevent null dereference (only required if error state)
    3222       518374 :     if (other.trait_type != nullptr)
    3223       518374 :       trait_type = other.trait_type->clone_type ();
    3224              : 
    3225       518374 :     generic_params.reserve (other.generic_params.size ());
    3226       967555 :     for (const auto &e : other.generic_params)
    3227       449181 :       generic_params.push_back (e->clone_generic_param ());
    3228       518374 :   }
    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         4860 :   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         4860 :       impl_items (std::move (impl_items))
    3278         4860 :   {}
    3279              : 
    3280              :   // Copy constructor with vector clone
    3281        17247 :   InherentImpl (InherentImpl const &other) : Impl (other)
    3282              :   {
    3283        17247 :     impl_items.reserve (other.impl_items.size ());
    3284       114319 :     for (const auto &e : other.impl_items)
    3285        97072 :       impl_items.push_back (e->clone_associated_item ());
    3286        17247 :   }
    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       248989 :   std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items ()
    3312              :   {
    3313       913177 :     return impl_items;
    3314              :   }
    3315              : 
    3316              : protected:
    3317              :   /* Use covariance to implement clone function as returning this object
    3318              :    * rather than base */
    3319        12510 :   InherentImpl *clone_item_impl () const override
    3320              :   {
    3321        12510 :     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        89202 :   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        89202 :       has_unsafe (is_unsafe), has_exclam (has_exclam), trait_path (trait_path),
    3352        89202 :       impl_items (std::move (impl_items))
    3353        89202 :   {}
    3354              : 
    3355              :   // Copy constructor with vector clone
    3356       501127 :   TraitImpl (TraitImpl const &other)
    3357       501127 :     : Impl (other), has_unsafe (other.has_unsafe),
    3358       501127 :       has_exclam (other.has_exclam), trait_path (other.trait_path)
    3359              :   {
    3360       501127 :     impl_items.reserve (other.impl_items.size ());
    3361      1231071 :     for (const auto &e : other.impl_items)
    3362       729944 :       impl_items.push_back (e->clone_associated_item ());
    3363       501127 :   }
    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        88896 :   bool is_unsafe () const { return has_unsafe; };
    3387       184515 :   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      5396601 :   std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items ()
    3395              :   {
    3396     19641667 :     return impl_items;
    3397              :   }
    3398              : 
    3399              :   // TODO: is this better? Or is a "vis_block" better?
    3400     19730648 :   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       412160 :   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           18 :   ExternalTypeItem (Identifier item_name, Visibility vis,
    3501              :                     std::vector<Attribute> outer_attrs, location_t locus)
    3502           18 :     : ExternalItem (), outer_attrs (std::move (outer_attrs)), visibility (vis),
    3503           18 :       item_name (std::move (item_name)), locus (locus), marked_for_strip (false)
    3504           18 :   {}
    3505              : 
    3506              :   // copy constructor
    3507           32 :   ExternalTypeItem (ExternalTypeItem const &other)
    3508           32 :     : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs),
    3509           32 :       visibility (other.visibility), item_name (other.item_name),
    3510           32 :       locus (other.locus), marked_for_strip (other.marked_for_strip)
    3511              :   {
    3512           32 :     node_id = other.node_id;
    3513           32 :   }
    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          475 :   location_t get_locus () const { return locus; }
    3542              : 
    3543            0 :   void mark_for_strip () override { marked_for_strip = true; };
    3544          443 :   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         2272 :   std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
    3548              :   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
    3549              : 
    3550          461 :   Identifier get_identifier () const { return item_name; }
    3551              : 
    3552         1861 :   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           32 :   ExternalTypeItem *clone_external_item_impl () const override
    3559              :   {
    3560           32 :     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         8957 :   bool has_abi () const { return !abi.empty (); }
    3701              : 
    3702        13152 :   std::string get_abi () const { return abi; }
    3703              : 
    3704         2394 :   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         2394 :     : VisItem (std::move (vis), std::move (outer_attrs)), abi (std::move (abi)),
    3709         2394 :       inner_attrs (std::move (inner_attrs)),
    3710         2394 :       extern_items (std::move (extern_items)), locus (locus)
    3711         2394 :   {}
    3712              : 
    3713              :   // Copy constructor with vector clone
    3714         5937 :   ExternBlock (ExternBlock const &other)
    3715         5937 :     : VisItem (other), abi (other.abi), inner_attrs (other.inner_attrs),
    3716        11874 :       locus (other.locus), marked_for_strip (other.marked_for_strip)
    3717              :   {
    3718         5937 :     extern_items.reserve (other.extern_items.size ());
    3719        46743 :     for (const auto &e : other.extern_items)
    3720        40806 :       extern_items.push_back (e->clone_external_item ());
    3721         5937 :   }
    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         5673 :   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        22309 :   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        48142 :   std::vector<std::unique_ptr<ExternalItem>> &get_extern_items ()
    3757              :   {
    3758       160688 :     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       158879 :   std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
    3764              : 
    3765          706 :   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         3545 :   ExternBlock *clone_item_impl () const override
    3771              :   {
    3772         3545 :     return new ExternBlock (*this);
    3773              :   }
    3774              : };
    3775              : 
    3776              : class MacroRulesDefinition;
    3777              : } // namespace AST
    3778              : } // namespace Rust
    3779              : 
    3780              : #endif
        

Generated by: LCOV version 2.6-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.