LCOV - code coverage report
Current view: top level - gcc/rust/typecheck - rust-tyty.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 84.5 % 251 212
Test Date: 2025-11-22 14:42:49 Functions: 75.3 % 81 61
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2025 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_TYTY
      20                 :             : #define RUST_TYTY
      21                 :             : 
      22                 :             : #include "optional.h"
      23                 :             : #include "rust-hir-map.h"
      24                 :             : #include "rust-common.h"
      25                 :             : #include "rust-identifier.h"
      26                 :             : #include "rust-abi.h"
      27                 :             : #include "rust-tyty-bounds.h"
      28                 :             : #include "rust-tyty-util.h"
      29                 :             : #include "rust-tyty-subst.h"
      30                 :             : #include "rust-tyty-region.h"
      31                 :             : #include "rust-system.h"
      32                 :             : #include "rust-hir.h"
      33                 :             : #include "tree.h"
      34                 :             : 
      35                 :             : namespace Rust {
      36                 :             : 
      37                 :             : namespace Resolver {
      38                 :             : class TraitReference;
      39                 :             : 
      40                 :             : class TraitItemReference;
      41                 :             : 
      42                 :             : class AssociatedImplTrait;
      43                 :             : } // namespace Resolver
      44                 :             : 
      45                 :             : namespace TyTy {
      46                 :             : class ClosureType;
      47                 :             : class FnPtr;
      48                 :             : class FnType;
      49                 :             : class CallableTypeInterface;
      50                 :             : 
      51                 :             : // https://rustc-dev-guide.rust-lang.org/type-inference.html#inference-variables
      52                 :             : // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.TyKind.html#variants
      53                 :             : enum TypeKind
      54                 :             : {
      55                 :             :   INFER,
      56                 :             :   ADT,
      57                 :             :   STR,
      58                 :             :   REF,
      59                 :             :   POINTER,
      60                 :             :   PARAM,
      61                 :             :   CONST,
      62                 :             :   ARRAY,
      63                 :             :   SLICE,
      64                 :             :   FNDEF,
      65                 :             :   FNPTR,
      66                 :             :   TUPLE,
      67                 :             :   BOOL,
      68                 :             :   CHAR,
      69                 :             :   INT,
      70                 :             :   UINT,
      71                 :             :   FLOAT,
      72                 :             :   USIZE,
      73                 :             :   ISIZE,
      74                 :             :   NEVER,
      75                 :             :   PLACEHOLDER,
      76                 :             :   PROJECTION,
      77                 :             :   DYNAMIC,
      78                 :             :   CLOSURE,
      79                 :             :   OPAQUE,
      80                 :             :   // there are more to add...
      81                 :             :   ERROR
      82                 :             : };
      83                 :             : 
      84                 :             : extern bool is_primitive_type_kind (TypeKind kind);
      85                 :             : 
      86                 :             : class TypeKindFormat
      87                 :             : {
      88                 :             : public:
      89                 :             :   static std::string to_string (TypeKind kind);
      90                 :             : };
      91                 :             : 
      92                 :             : class TyVisitor;
      93                 :             : class TyConstVisitor;
      94                 :             : class BaseConstType;
      95                 :             : class BaseType : public TypeBoundsMappings
      96                 :             : {
      97                 :             : public:
      98                 :             :   virtual ~BaseType ();
      99                 :             : 
     100                 :             :   HirId get_ref () const;
     101                 :             :   void set_ref (HirId id);
     102                 :             : 
     103                 :             :   HirId get_ty_ref () const;
     104                 :             :   void set_ty_ref (HirId id);
     105                 :             : 
     106                 :             :   HirId get_orig_ref () const;
     107                 :             : 
     108                 :             :   virtual void accept_vis (TyVisitor &vis) = 0;
     109                 :             :   virtual void accept_vis (TyConstVisitor &vis) const = 0;
     110                 :             : 
     111                 :             :   virtual std::string as_string () const = 0;
     112                 :             :   virtual std::string get_name () const = 0;
     113                 :             : 
     114                 :             :   // Check value equality between two ty. Type inference rules are ignored. Two
     115                 :             :   //   ty are considered equal if they're of the same kind, and
     116                 :             :   //     1. (For ADTs, arrays, tuples, refs) have the same underlying ty
     117                 :             :   //     2. (For functions) have the same signature
     118                 :             :   virtual bool is_equal (const BaseType &other) const;
     119                 :             : 
     120                 :             :   bool satisfies_bound (const TypeBoundPredicate &predicate, bool emit_error);
     121                 :             : 
     122                 :             :   bool bounds_compatible (BaseType &other, location_t locus, bool emit_error);
     123                 :             : 
     124                 :             :   void inherit_bounds (const BaseType &other);
     125                 :             : 
     126                 :             :   void inherit_bounds (
     127                 :             :     const std::vector<TyTy::TypeBoundPredicate> &specified_bounds);
     128                 :             : 
     129                 :             :   // contains_infer checks if there is an inference variable inside the type
     130                 :             :   const TyTy::BaseType *contains_infer () const;
     131                 :             : 
     132                 :             :   // is_unit returns whether this is just a unit-struct
     133                 :             :   bool is_unit () const;
     134                 :             : 
     135                 :             :   // is_concrete returns true if the type is fully resolved to concrete
     136                 :             :   // primitives
     137                 :             :   bool is_concrete () const;
     138                 :             : 
     139                 :             :   // return the type-kind
     140                 :             :   TypeKind get_kind () const;
     141                 :             : 
     142                 :             :   // monomorphized clone is a clone which destructures the types to get rid of
     143                 :             :   // generics
     144                 :             :   BaseType *monomorphized_clone () const;
     145                 :             : 
     146                 :             :   // get_combined_refs returns the chain of node refs involved in unification
     147                 :             :   std::set<HirId> get_combined_refs () const;
     148                 :             : 
     149                 :             :   void append_reference (HirId id);
     150                 :             : 
     151                 :             :   std::string mappings_str () const;
     152                 :             : 
     153                 :             :   std::string debug_str () const;
     154                 :             : 
     155                 :             :   void debug () const;
     156                 :             : 
     157                 :             :   BaseType *get_root ();
     158                 :             : 
     159                 :             :   // This will get the monomorphized type from Params, Placeholders or
     160                 :             :   // Projections if available or error
     161                 :             :   BaseType *destructure ();
     162                 :             :   const BaseType *destructure () const;
     163                 :             : 
     164                 :             :   const RustIdent &get_ident () const;
     165                 :             :   location_t get_locus () const;
     166                 :             : 
     167                 :             :   bool has_substitutions_defined () const;
     168                 :             :   bool needs_generic_substitutions () const;
     169                 :             :   const SubstitutionArgumentMappings &get_subst_argument_mappings () const;
     170                 :             : 
     171                 :       29603 :   std::string mangle_string () const
     172                 :             :   {
     173                 :       88809 :     return TypeKindFormat::to_string (get_kind ()) + ":" + as_string () + ":"
     174                 :      118412 :            + mappings_str () + ":" + bounds_as_string ();
     175                 :             :   }
     176                 :             : 
     177                 :             :   /* Returns a pointer to a clone of this. The caller is responsible for
     178                 :             :    * releasing the memory of the returned ty. */
     179                 :             :   virtual BaseType *clone () const = 0;
     180                 :             : 
     181                 :             :   // Check if TyTy::BaseType is of a specific type.
     182                 :   137265919 :   template <typename T> WARN_UNUSED_RESULT bool is () const
     183                 :             :   {
     184                 :             :     static_assert (std::is_base_of<BaseType, T>::value,
     185                 :             :                    "Can only safely cast to TyTy types.");
     186                 :    42621119 :     return this->get_kind () == T::KIND;
     187                 :             :   }
     188                 :             : 
     189                 :      805643 :   template <typename T> T *as () const
     190                 :             :   {
     191                 :             :     static_assert (std::is_base_of<BaseType, T>::value,
     192                 :             :                    "Can only safely cast to TyTy types.");
     193                 :      805643 :     rust_assert (this->is<T> ());
     194                 :      805643 :     return static_cast<T *> (this);
     195                 :             :   }
     196                 :             : 
     197                 :       50410 :   template <typename T> T *as ()
     198                 :             :   {
     199                 :             :     static_assert (std::is_base_of<BaseType, T>::value,
     200                 :             :                    "Can only safely cast to TyTy types.");
     201                 :       50410 :     rust_assert (this->is<T> ());
     202                 :       50410 :     return static_cast<T *> (this);
     203                 :             :   }
     204                 :             : 
     205                 :             :   // Check if TyTy::BaseType is of a specific type and convert it to that type
     206                 :             :   // if so.
     207                 :             :   // Returns nullptr otherwise. Works as a dynamic_cast, but without compiler
     208                 :             :   // RTTI.
     209                 :    78106735 :   template <typename T> T *try_as () const
     210                 :             :   {
     211                 :             :     static_assert (std::is_base_of<BaseType, T>::value,
     212                 :             :                    "Can only safely cast to TyTy types.");
     213                 :    78106735 :     if (!this->is<T> ())
     214                 :             :       return nullptr;
     215                 :             : 
     216                 :             :     return static_cast<T *> (this);
     217                 :             :   }
     218                 :             : 
     219                 :             :   // See above.
     220                 :    15730182 :   template <typename T> T *try_as ()
     221                 :             :   {
     222                 :             :     static_assert (std::is_base_of<BaseType, T>::value,
     223                 :             :                    "Can only safely cast to TyTy types.");
     224                 :    15730182 :     if (!this->is<T> ())
     225                 :           0 :       return nullptr;
     226                 :             : 
     227                 :             :     return static_cast<T *> (this);
     228                 :             :   }
     229                 :             : 
     230                 :             :   // Helper to get BaseConstType interface for CONST types
     231                 :             :   // Overridden by const types that also inherit from BaseConstType
     232                 :           0 :   virtual BaseConstType *as_const_type () { return nullptr; }
     233                 :           0 :   virtual const BaseConstType *as_const_type () const { return nullptr; }
     234                 :             : 
     235                 :             : protected:
     236                 :             :   BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
     237                 :             :             std::set<HirId> refs = std::set<HirId> ());
     238                 :             : 
     239                 :             :   BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
     240                 :             :             std::vector<TypeBoundPredicate> specified_bounds,
     241                 :             :             std::set<HirId> refs = std::set<HirId> ());
     242                 :             : 
     243                 :             :   TypeKind kind;
     244                 :             :   HirId ref;
     245                 :             :   HirId ty_ref;
     246                 :             :   const HirId orig_ref;
     247                 :             :   std::set<HirId> combined;
     248                 :             :   RustIdent ident;
     249                 :             : 
     250                 :             :   Analysis::Mappings &mappings;
     251                 :             : };
     252                 :             : 
     253                 :             : /** Unified interface for all function-like types. */
     254                 :             : class CallableTypeInterface : public BaseType
     255                 :             : {
     256                 :             : public:
     257                 :       54788 :   explicit CallableTypeInterface (HirId ref, HirId ty_ref, TypeKind kind,
     258                 :             :                                   RustIdent ident,
     259                 :             :                                   std::set<HirId> refs = std::set<HirId> ())
     260                 :       54788 :     : BaseType (ref, ty_ref, kind, ident, refs)
     261                 :       54788 :   {}
     262                 :             : 
     263                 :             :   WARN_UNUSED_RESULT virtual size_t get_num_params () const = 0;
     264                 :             :   WARN_UNUSED_RESULT virtual BaseType *get_param_type_at (size_t index) const
     265                 :             :     = 0;
     266                 :             :   WARN_UNUSED_RESULT virtual BaseType *get_return_type () const = 0;
     267                 :             : };
     268                 :             : 
     269                 :             : class InferType : public BaseType
     270                 :             : {
     271                 :             : public:
     272                 :             :   static constexpr auto KIND = TypeKind::INFER;
     273                 :             : 
     274                 :             :   enum InferTypeKind
     275                 :             :   {
     276                 :             :     GENERAL,
     277                 :             :     INTEGRAL,
     278                 :             :     FLOAT
     279                 :             :   };
     280                 :             : 
     281                 :             :   struct TypeHint
     282                 :             :   {
     283                 :             :     enum SignedHint
     284                 :             :     {
     285                 :             :       SIGNED,
     286                 :             :       UNSIGNED,
     287                 :             : 
     288                 :             :       UNKNOWN
     289                 :             :     };
     290                 :             :     enum SizeHint
     291                 :             :     {
     292                 :             :       S8,
     293                 :             :       S16,
     294                 :             :       S32,
     295                 :             :       S64,
     296                 :             :       S128,
     297                 :             :       SUNKNOWN
     298                 :             :     };
     299                 :             : 
     300                 :             :     TyTy::TypeKind kind;
     301                 :             :     SignedHint shint;
     302                 :             :     SizeHint szhint;
     303                 :             : 
     304                 :             :     static TypeHint Default ()
     305                 :             :     {
     306                 :             :       return TypeHint{TypeKind::ERROR, UNKNOWN, SUNKNOWN};
     307                 :             :     }
     308                 :             :   };
     309                 :             : 
     310                 :             :   InferType (HirId ref, InferTypeKind infer_kind, TypeHint hint,
     311                 :             :              location_t locus, std::set<HirId> refs = std::set<HirId> ());
     312                 :             : 
     313                 :             :   InferType (HirId ref, HirId ty_ref, InferTypeKind infer_kind, TypeHint hint,
     314                 :             :              location_t locus, std::set<HirId> refs = std::set<HirId> ());
     315                 :             : 
     316                 :             :   void accept_vis (TyVisitor &vis) override;
     317                 :             : 
     318                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     319                 :             : 
     320                 :             :   std::string as_string () const override;
     321                 :             : 
     322                 :             :   BaseType *clone () const final override;
     323                 :             : 
     324                 :             :   InferTypeKind get_infer_kind () const;
     325                 :             : 
     326                 :             :   std::string get_name () const override final;
     327                 :             : 
     328                 :             :   bool default_type (BaseType **type) const;
     329                 :             : 
     330                 :             :   void apply_primitive_type_hint (const TyTy::BaseType &hint);
     331                 :             : 
     332                 :             : private:
     333                 :             :   InferTypeKind infer_kind;
     334                 :             :   TypeHint default_hint;
     335                 :             : };
     336                 :             : 
     337                 :             : class ErrorType : public BaseType
     338                 :             : {
     339                 :             : public:
     340                 :             :   static constexpr auto KIND = TypeKind::ERROR;
     341                 :             : 
     342                 :             :   ErrorType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
     343                 :             : 
     344                 :             :   ErrorType (HirId ref, HirId ty_ref,
     345                 :             :              std::set<HirId> refs = std::set<HirId> ());
     346                 :             : 
     347                 :             :   void accept_vis (TyVisitor &vis) override;
     348                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     349                 :             : 
     350                 :             :   std::string as_string () const override;
     351                 :             : 
     352                 :             :   BaseType *clone () const final override;
     353                 :             : 
     354                 :             :   std::string get_name () const override final;
     355                 :             : };
     356                 :             : 
     357                 :             : class BaseGeneric : public BaseType
     358                 :             : {
     359                 :             : public:
     360                 :             :   virtual std::string get_symbol () const = 0;
     361                 :             : 
     362                 :             :   virtual bool can_resolve () const = 0;
     363                 :             : 
     364                 :             :   virtual BaseType *resolve () const = 0;
     365                 :             : 
     366                 :             : protected:
     367                 :    85826736 :   BaseGeneric (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
     368                 :             :                std::vector<TypeBoundPredicate> specified_bounds,
     369                 :             :                std::set<HirId> refs = std::set<HirId> ())
     370                 :    85826736 :     : BaseType (ref, ty_ref, kind, ident, specified_bounds, refs)
     371                 :    85826736 :   {}
     372                 :             : };
     373                 :             : 
     374                 :             : class ParamType : public BaseGeneric
     375                 :             : {
     376                 :             : public:
     377                 :             :   static constexpr auto KIND = TypeKind::PARAM;
     378                 :             : 
     379                 :             :   ParamType (std::string symbol, location_t locus, HirId ref,
     380                 :             :              std::vector<TypeBoundPredicate> specified_bounds,
     381                 :             :              std::set<HirId> refs = std::set<HirId> ());
     382                 :             : 
     383                 :             :   ParamType (bool is_trait_self, std::string symbol, location_t locus,
     384                 :             :              HirId ref, HirId ty_ref,
     385                 :             :              std::vector<TypeBoundPredicate> specified_bounds,
     386                 :             :              std::set<HirId> refs = std::set<HirId> ());
     387                 :             : 
     388                 :             :   void accept_vis (TyVisitor &vis) override;
     389                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     390                 :             : 
     391                 :             :   std::string as_string () const override;
     392                 :             : 
     393                 :             :   BaseType *clone () const final override;
     394                 :             : 
     395                 :             :   std::string get_symbol () const override final;
     396                 :             : 
     397                 :             :   bool can_resolve () const override final;
     398                 :             : 
     399                 :             :   BaseType *resolve () const override final;
     400                 :             : 
     401                 :             :   std::string get_name () const override final;
     402                 :             : 
     403                 :             :   bool is_equal (const BaseType &other) const override;
     404                 :             : 
     405                 :             :   ParamType *handle_substitions (SubstitutionArgumentMappings &mappings);
     406                 :             : 
     407                 :             :   void set_implicit_self_trait ();
     408                 :             :   bool is_implicit_self_trait () const;
     409                 :             : 
     410                 :             : private:
     411                 :             :   bool is_trait_self;
     412                 :             :   std::string symbol;
     413                 :             : };
     414                 :             : 
     415                 :        3306 : class BaseConstType
     416                 :             : {
     417                 :             : public:
     418                 :             :   static constexpr auto KIND = TypeKind::CONST;
     419                 :             : 
     420                 :             :   enum ConstKind
     421                 :             :   {
     422                 :             :     Decl,
     423                 :             :     Value,
     424                 :             :     Infer,
     425                 :             :     Error
     426                 :             :   };
     427                 :             : 
     428                 :             :   virtual ConstKind const_kind () const = 0;
     429                 :             : 
     430                 :        2000 :   BaseType *get_specified_type () const { return specified_type; }
     431                 :             : 
     432                 :             :   // Helper to get BaseType interface (all const types also inherit BaseType)
     433                 :             :   // This must be implemented by concrete classes since BaseConstType doesn't
     434                 :             :   // inherit from BaseType, but all concrete const types do.
     435                 :             :   virtual BaseType *as_base_type () = 0;
     436                 :             :   virtual const BaseType *as_base_type () const = 0;
     437                 :             : 
     438                 :             : protected:
     439                 :         807 :   BaseConstType (BaseType *type) : specified_type (type) {}
     440                 :             : 
     441                 :             :   BaseType *specified_type;
     442                 :             : };
     443                 :             : 
     444                 :             : class ConstParamType : public BaseConstType, public BaseGeneric
     445                 :             : {
     446                 :             : public:
     447                 :             :   ConstParamType (std::string symbol, location_t locus, BaseType *type,
     448                 :             :                   HirId ref, HirId ty_ref,
     449                 :             :                   std::set<HirId> refs = std::set<HirId> ());
     450                 :             : 
     451                 :             :   ConstKind const_kind () const override final;
     452                 :             : 
     453                 :             :   std::string get_symbol () const override final;
     454                 :             : 
     455                 :             :   bool can_resolve () const override final;
     456                 :             : 
     457                 :             :   BaseType *resolve () const override final;
     458                 :             : 
     459                 :             :   void accept_vis (TyVisitor &vis) override;
     460                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     461                 :             : 
     462                 :             :   std::string as_string () const override;
     463                 :             : 
     464                 :             :   BaseType *clone () const final override;
     465                 :             :   std::string get_name () const override final;
     466                 :             : 
     467                 :             :   bool is_equal (const BaseType &other) const override;
     468                 :             : 
     469                 :             :   BaseType *handle_substitions (SubstitutionArgumentMappings &mappings);
     470                 :             : 
     471                 :         117 :   BaseType *as_base_type () override { return static_cast<BaseType *> (this); }
     472                 :        2649 :   const BaseType *as_base_type () const override
     473                 :             :   {
     474                 :        2649 :     return static_cast<const BaseType *> (this);
     475                 :             :   }
     476                 :             : 
     477                 :        2270 :   BaseConstType *as_const_type () override { return this; }
     478                 :        2493 :   const BaseConstType *as_const_type () const override { return this; }
     479                 :             : 
     480                 :             : private:
     481                 :             :   std::string symbol;
     482                 :             : };
     483                 :             : 
     484                 :        3306 : class ConstValueType : public BaseType, public BaseConstType
     485                 :             : {
     486                 :             : public:
     487                 :             :   static constexpr auto KIND = TypeKind::CONST;
     488                 :             : 
     489                 :             :   ConstValueType (tree value, BaseType *type, HirId ref, HirId ty_ref,
     490                 :             :                   std::set<HirId> refs = std::set<HirId> ());
     491                 :             : 
     492                 :             :   ConstKind const_kind () const override final;
     493                 :             : 
     494                 :             :   void accept_vis (TyVisitor &vis) override;
     495                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     496                 :             : 
     497                 :             :   std::string as_string () const override;
     498                 :             : 
     499                 :             :   BaseType *clone () const final override;
     500                 :             :   std::string get_name () const override final;
     501                 :             : 
     502                 :             :   bool is_equal (const BaseType &other) const override;
     503                 :             : 
     504                 :             :   tree get_value () const;
     505                 :             : 
     506                 :        5422 :   BaseType *as_base_type () override { return static_cast<BaseType *> (this); }
     507                 :           0 :   const BaseType *as_base_type () const override
     508                 :             :   {
     509                 :           0 :     return static_cast<const BaseType *> (this);
     510                 :             :   }
     511                 :             : 
     512                 :       26296 :   BaseConstType *as_const_type () override { return this; }
     513                 :       36413 :   const BaseConstType *as_const_type () const override { return this; }
     514                 :             : 
     515                 :             : private:
     516                 :             :   tree folded_val;
     517                 :             : };
     518                 :             : 
     519                 :             : class ConstInferType : public BaseType, public BaseConstType
     520                 :             : {
     521                 :             : public:
     522                 :             :   static constexpr auto KIND = TypeKind::CONST;
     523                 :             : 
     524                 :             :   ConstInferType (BaseType *type, HirId ref, HirId ty_ref,
     525                 :             :                   std::set<HirId> refs = std::set<HirId> ());
     526                 :             : 
     527                 :             :   ConstKind const_kind () const override final;
     528                 :             : 
     529                 :             :   void accept_vis (TyVisitor &vis) override;
     530                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     531                 :             : 
     532                 :             :   std::string as_string () const override;
     533                 :             : 
     534                 :             :   BaseType *clone () const final override;
     535                 :             :   std::string get_name () const override final;
     536                 :             : 
     537                 :             :   bool is_equal (const BaseType &other) const override;
     538                 :             : 
     539                 :          10 :   BaseType *as_base_type () override { return static_cast<BaseType *> (this); }
     540                 :           0 :   const BaseType *as_base_type () const override
     541                 :             :   {
     542                 :           0 :     return static_cast<const BaseType *> (this);
     543                 :             :   }
     544                 :             : 
     545                 :         603 :   BaseConstType *as_const_type () override { return this; }
     546                 :          62 :   const BaseConstType *as_const_type () const override { return this; }
     547                 :             : };
     548                 :             : 
     549                 :             : class ConstErrorType : public BaseType, public BaseConstType
     550                 :             : {
     551                 :             : public:
     552                 :             :   static constexpr auto KIND = TypeKind::CONST;
     553                 :             : 
     554                 :             :   ConstErrorType (BaseType *type, HirId ref, HirId ty_ref,
     555                 :             :                   std::set<HirId> refs = std::set<HirId> ());
     556                 :             : 
     557                 :             :   ConstKind const_kind () const override final;
     558                 :             : 
     559                 :             :   void accept_vis (TyVisitor &vis) override;
     560                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     561                 :             : 
     562                 :             :   std::string as_string () const override;
     563                 :             : 
     564                 :             :   BaseType *clone () const final override;
     565                 :             :   std::string get_name () const override final;
     566                 :             : 
     567                 :             :   bool is_equal (const BaseType &other) const override;
     568                 :             : 
     569                 :           1 :   BaseType *as_base_type () override { return static_cast<BaseType *> (this); }
     570                 :           0 :   const BaseType *as_base_type () const override
     571                 :             :   {
     572                 :           0 :     return static_cast<const BaseType *> (this);
     573                 :             :   }
     574                 :             : 
     575                 :           0 :   BaseConstType *as_const_type () override { return this; }
     576                 :           0 :   const BaseConstType *as_const_type () const override { return this; }
     577                 :             : };
     578                 :             : 
     579                 :         168 : class OpaqueType : public BaseType
     580                 :             : {
     581                 :             : public:
     582                 :             :   static constexpr auto KIND = TypeKind::OPAQUE;
     583                 :             : 
     584                 :             :   OpaqueType (location_t locus, HirId ref,
     585                 :             :               std::vector<TypeBoundPredicate> specified_bounds,
     586                 :             :               std::set<HirId> refs = std::set<HirId> ());
     587                 :             : 
     588                 :             :   OpaqueType (location_t locus, HirId ref, HirId ty_ref,
     589                 :             :               std::vector<TypeBoundPredicate> specified_bounds,
     590                 :             :               std::set<HirId> refs = std::set<HirId> ());
     591                 :             : 
     592                 :             :   void accept_vis (TyVisitor &vis) override;
     593                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     594                 :             : 
     595                 :             :   std::string as_string () const override;
     596                 :             : 
     597                 :             :   BaseType *clone () const final override;
     598                 :             : 
     599                 :             :   bool can_resolve () const;
     600                 :             : 
     601                 :             :   BaseType *resolve () const;
     602                 :             : 
     603                 :             :   std::string get_name () const override final;
     604                 :             : 
     605                 :             :   bool is_equal (const BaseType &other) const override;
     606                 :             : };
     607                 :             : 
     608                 :             : class StructFieldType
     609                 :             : {
     610                 :             : public:
     611                 :             :   StructFieldType (HirId ref, std::string name, BaseType *ty, location_t locus);
     612                 :             : 
     613                 :             :   HirId get_ref () const;
     614                 :             : 
     615                 :             :   bool is_equal (const StructFieldType &other) const;
     616                 :             : 
     617                 :             :   std::string get_name () const;
     618                 :             : 
     619                 :             :   BaseType *get_field_type () const;
     620                 :             :   void set_field_type (BaseType *fty);
     621                 :             : 
     622                 :             :   StructFieldType *clone () const;
     623                 :             :   StructFieldType *monomorphized_clone () const;
     624                 :             : 
     625                 :             :   void debug () const;
     626                 :             :   location_t get_locus () const;
     627                 :             :   std::string as_string () const;
     628                 :             : 
     629                 :             : private:
     630                 :             :   HirId ref;
     631                 :             :   std::string name;
     632                 :             :   BaseType *ty;
     633                 :             :   location_t locus;
     634                 :             : };
     635                 :             : 
     636                 :             : class TupleType : public BaseType
     637                 :             : {
     638                 :             : public:
     639                 :             :   static constexpr auto KIND = TypeKind::TUPLE;
     640                 :             : 
     641                 :             :   TupleType (HirId ref, location_t locus,
     642                 :             :              std::vector<TyVar> fields = std::vector<TyVar> (),
     643                 :             :              std::set<HirId> refs = std::set<HirId> ());
     644                 :             : 
     645                 :             :   TupleType (HirId ref, HirId ty_ref, location_t locus,
     646                 :             :              std::vector<TyVar> fields = std::vector<TyVar> (),
     647                 :             :              std::set<HirId> refs = std::set<HirId> ());
     648                 :             : 
     649                 :             :   static TupleType *get_unit_type ();
     650                 :             : 
     651                 :             :   void accept_vis (TyVisitor &vis) override;
     652                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     653                 :             : 
     654                 :             :   std::string as_string () const override;
     655                 :             : 
     656                 :             :   bool is_equal (const BaseType &other) const override;
     657                 :             : 
     658                 :             :   size_t num_fields () const;
     659                 :             : 
     660                 :             :   BaseType *get_field (size_t index) const;
     661                 :             : 
     662                 :             :   BaseType *clone () const final override;
     663                 :             : 
     664                 :             :   const std::vector<TyVar> &get_fields () const;
     665                 :             : 
     666                 :             :   std::string get_name () const override final;
     667                 :             : 
     668                 :             :   TupleType *handle_substitions (SubstitutionArgumentMappings &mappings);
     669                 :             : 
     670                 :             : private:
     671                 :             :   std::vector<TyVar> fields;
     672                 :             : };
     673                 :             : 
     674                 :             : class TypeBoundPredicate : public SubstitutionRef
     675                 :             : {
     676                 :             : public:
     677                 :             :   TypeBoundPredicate (const Resolver::TraitReference &trait_reference,
     678                 :             :                       BoundPolarity polarity, location_t locus);
     679                 :             : 
     680                 :             :   TypeBoundPredicate (DefId reference,
     681                 :             :                       std::vector<SubstitutionParamMapping> substitutions,
     682                 :             :                       BoundPolarity polarity, location_t locus);
     683                 :             : 
     684                 :             :   TypeBoundPredicate (const TypeBoundPredicate &other);
     685                 :             : 
     686                 :    59275743 :   virtual ~TypeBoundPredicate () {}
     687                 :             : 
     688                 :             :   TypeBoundPredicate &operator= (const TypeBoundPredicate &other);
     689                 :             : 
     690                 :             :   static TypeBoundPredicate error ();
     691                 :             : 
     692                 :             :   std::string as_string () const;
     693                 :             : 
     694                 :             :   std::string as_name () const;
     695                 :             : 
     696                 :             :   const Resolver::TraitReference *get () const;
     697                 :             : 
     698                 :       26730 :   location_t get_locus () const { return locus; }
     699                 :             : 
     700                 :             :   std::string get_name () const;
     701                 :             : 
     702                 :             :   // check that this  is object-safe see:
     703                 :             :   // https://doc.rust-lang.org/reference/items/traits.html#object-safety
     704                 :             :   bool is_object_safe (bool emit_error, location_t locus) const;
     705                 :             : 
     706                 :             :   void apply_generic_arguments (HIR::GenericArgs *generic_args,
     707                 :             :                                 bool has_associated_self, bool is_super_trait);
     708                 :             : 
     709                 :             :   void apply_argument_mappings (SubstitutionArgumentMappings &arguments,
     710                 :             :                                 bool is_super_trait);
     711                 :             : 
     712                 :             :   bool contains_item (const std::string &search) const;
     713                 :             : 
     714                 :             :   tl::optional<TypeBoundPredicateItem>
     715                 :             :   lookup_associated_item (const std::string &search) const;
     716                 :             : 
     717                 :             :   tl::optional<TypeBoundPredicateItem>
     718                 :             :   lookup_associated_item (const Resolver::TraitItemReference *ref) const;
     719                 :             : 
     720                 :             :   // WARNING THIS WILL ALWAYS RETURN NULLPTR
     721                 :             :   BaseType *
     722                 :             :   handle_substitions (SubstitutionArgumentMappings &mappings) override final;
     723                 :             : 
     724                 :             :   bool is_error () const;
     725                 :             : 
     726                 :             :   bool requires_generic_args () const;
     727                 :             : 
     728                 :             :   bool contains_associated_types () const;
     729                 :             : 
     730                 :       51849 :   DefId get_id () const { return reference; }
     731                 :             : 
     732                 :       13284 :   BoundPolarity get_polarity () const { return polarity; }
     733                 :             : 
     734                 :             :   std::vector<TypeBoundPredicateItem> get_associated_type_items ();
     735                 :             : 
     736                 :             :   size_t get_num_associated_bindings () const override final;
     737                 :             : 
     738                 :             :   TypeBoundPredicateItem
     739                 :             :   lookup_associated_type (const std::string &search) override final;
     740                 :             : 
     741                 :             :   bool is_equal (const TypeBoundPredicate &other) const;
     742                 :             : 
     743                 :             :   bool validate_type_implements_super_traits (TyTy::BaseType &self,
     744                 :             :                                               HIR::Type &impl_type,
     745                 :             :                                               HIR::Type &trait) const;
     746                 :             : 
     747                 :             :   bool validate_type_implements_this (TyTy::BaseType &self,
     748                 :             :                                       HIR::Type &impl_type,
     749                 :             :                                       HIR::Type &trait) const;
     750                 :             : 
     751                 :             : private:
     752                 :             :   struct mark_is_error
     753                 :             :   {
     754                 :             :   };
     755                 :             : 
     756                 :             :   TypeBoundPredicate (mark_is_error);
     757                 :             : 
     758                 :             :   void get_trait_hierachy (
     759                 :             :     std::function<void (const Resolver::TraitReference &)> callback) const;
     760                 :             : 
     761                 :             :   DefId reference;
     762                 :             :   location_t locus;
     763                 :             :   bool error_flag;
     764                 :             :   BoundPolarity polarity;
     765                 :             :   std::vector<TyTy::TypeBoundPredicate> super_traits;
     766                 :             : };
     767                 :             : 
     768                 :       54694 : class TypeBoundPredicateItem
     769                 :             : {
     770                 :             : public:
     771                 :             :   TypeBoundPredicateItem (const TypeBoundPredicate parent,
     772                 :             :                           const Resolver::TraitItemReference *trait_item_ref);
     773                 :             : 
     774                 :             :   TypeBoundPredicateItem (const TypeBoundPredicateItem &other);
     775                 :             : 
     776                 :             :   TypeBoundPredicateItem &operator= (const TypeBoundPredicateItem &other);
     777                 :             : 
     778                 :             :   static TypeBoundPredicateItem error ();
     779                 :             : 
     780                 :             :   bool is_error () const;
     781                 :             : 
     782                 :             :   BaseType *get_tyty_for_receiver (const TyTy::BaseType *receiver);
     783                 :             : 
     784                 :             :   const Resolver::TraitItemReference *get_raw_item () const;
     785                 :             : 
     786                 :             :   bool needs_implementation () const;
     787                 :             : 
     788                 :             :   const TypeBoundPredicate *get_parent () const;
     789                 :             : 
     790                 :             :   location_t get_locus () const;
     791                 :             : 
     792                 :             : private:
     793                 :             :   TypeBoundPredicate parent;
     794                 :             :   const Resolver::TraitItemReference *trait_item_ref;
     795                 :             : };
     796                 :             : 
     797                 :             : // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.VariantDef.html
     798                 :             : class VariantDef
     799                 :             : {
     800                 :             : public:
     801                 :             :   enum VariantType
     802                 :             :   {
     803                 :             :     NUM,
     804                 :             :     TUPLE,
     805                 :             :     STRUCT
     806                 :             :   };
     807                 :             : 
     808                 :             :   static std::string variant_type_string (VariantType type);
     809                 :             : 
     810                 :             :   VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident,
     811                 :             :               tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant);
     812                 :             : 
     813                 :             :   VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident,
     814                 :             :               VariantType type,
     815                 :             :               tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant,
     816                 :             :               std::vector<StructFieldType *> fields);
     817                 :             : 
     818                 :             :   static VariantDef &get_error_node ();
     819                 :             :   bool is_error () const;
     820                 :             : 
     821                 :             :   HirId get_id () const;
     822                 :             :   DefId get_defid () const;
     823                 :             : 
     824                 :             :   VariantType get_variant_type () const;
     825                 :             :   bool is_data_variant () const;
     826                 :             :   bool is_dataless_variant () const;
     827                 :             : 
     828                 :             :   std::string get_identifier () const;
     829                 :             : 
     830                 :             :   size_t num_fields () const;
     831                 :             :   StructFieldType *get_field_at_index (size_t index);
     832                 :             : 
     833                 :             :   std::vector<StructFieldType *> &get_fields ();
     834                 :             : 
     835                 :             :   bool lookup_field (const std::string &lookup, StructFieldType **field_lookup,
     836                 :             :                      size_t *index) const;
     837                 :             : 
     838                 :             :   bool has_discriminant () const;
     839                 :             : 
     840                 :             :   HIR::Expr &get_discriminant ();
     841                 :             :   const HIR::Expr &get_discriminant () const;
     842                 :             : 
     843                 :             :   std::string as_string () const;
     844                 :             : 
     845                 :             :   bool is_equal (const VariantDef &other) const;
     846                 :             : 
     847                 :             :   VariantDef *clone () const;
     848                 :             : 
     849                 :             :   VariantDef *monomorphized_clone () const;
     850                 :             : 
     851                 :             :   const RustIdent &get_ident () const;
     852                 :             : 
     853                 :             : private:
     854                 :             :   HirId id;
     855                 :             :   DefId defid;
     856                 :             :   std::string identifier;
     857                 :             :   RustIdent ident;
     858                 :             :   VariantType type;
     859                 :             : 
     860                 :             :   // can either be a structure or a discriminant value
     861                 :             :   tl::optional<std::unique_ptr<HIR::Expr>> discriminant;
     862                 :             : 
     863                 :             :   std::vector<StructFieldType *> fields;
     864                 :             : };
     865                 :             : 
     866                 :             : class ADTType : public BaseType, public SubstitutionRef
     867                 :             : {
     868                 :             : public:
     869                 :             :   static constexpr auto KIND = TypeKind::ADT;
     870                 :             : 
     871                 :             :   enum ADTKind
     872                 :             :   {
     873                 :             :     STRUCT_STRUCT,
     874                 :             :     TUPLE_STRUCT,
     875                 :             :     UNION,
     876                 :             :     ENUM
     877                 :             :   };
     878                 :             : 
     879                 :             :   enum ReprKind
     880                 :             :   {
     881                 :             :     RUST,
     882                 :             :     C,
     883                 :             :     INT,
     884                 :             :     ALIGN,
     885                 :             :     PACKED,
     886                 :             :     // TRANSPARENT,
     887                 :             :     // SIMD,
     888                 :             :     // ...
     889                 :             :   };
     890                 :             : 
     891                 :             :   // Representation options, specified via attributes e.g. #[repr(packed)]
     892                 :         100 :   struct ReprOptions
     893                 :             :   {
     894                 :             :     ReprKind repr_kind = ReprKind::RUST;
     895                 :             : 
     896                 :             :     // For align and pack: 0 = unspecified. Nonzero = byte alignment.
     897                 :             :     // It is an error for both to be nonzero, this should be caught when
     898                 :             :     // parsing the #[repr] attribute.
     899                 :             :     unsigned char align = 0;
     900                 :             :     unsigned char pack = 0;
     901                 :             :     BaseType *repr = nullptr;
     902                 :             :   };
     903                 :             : 
     904                 :             :   ADTType (DefId id, HirId ref, std::string identifier, RustIdent ident,
     905                 :             :            ADTKind adt_kind, std::vector<VariantDef *> variants,
     906                 :             :            std::vector<SubstitutionParamMapping> subst_refs,
     907                 :             :            SubstitutionArgumentMappings generic_arguments
     908                 :             :            = SubstitutionArgumentMappings::error (),
     909                 :             :            RegionConstraints region_constraints = RegionConstraints{},
     910                 :             :            std::set<HirId> refs = std::set<HirId> ());
     911                 :             : 
     912                 :             :   ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier,
     913                 :             :            RustIdent ident, ADTKind adt_kind,
     914                 :             :            std::vector<VariantDef *> variants,
     915                 :             :            std::vector<SubstitutionParamMapping> subst_refs,
     916                 :             :            SubstitutionArgumentMappings generic_arguments
     917                 :             :            = SubstitutionArgumentMappings::error (),
     918                 :             :            RegionConstraints region_constraints = RegionConstraints{},
     919                 :             :            std::set<HirId> refs = std::set<HirId> ());
     920                 :             : 
     921                 :             :   ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier,
     922                 :             :            RustIdent ident, ADTKind adt_kind,
     923                 :             :            std::vector<VariantDef *> variants,
     924                 :             :            std::vector<SubstitutionParamMapping> subst_refs, ReprOptions repr,
     925                 :             :            SubstitutionArgumentMappings generic_arguments
     926                 :             :            = SubstitutionArgumentMappings::error (),
     927                 :             :            RegionConstraints region_constraints = RegionConstraints{},
     928                 :             :            std::set<HirId> refs = std::set<HirId> ());
     929                 :             : 
     930                 :      304745 :   ADTKind get_adt_kind () const { return adt_kind; }
     931                 :             : 
     932                 :      185011 :   ReprOptions get_repr_options () const { return repr; }
     933                 :             : 
     934                 :         902 :   bool is_struct_struct () const { return adt_kind == STRUCT_STRUCT; }
     935                 :             : 
     936                 :         746 :   bool is_tuple_struct () const { return adt_kind == TUPLE_STRUCT; }
     937                 :             : 
     938                 :       18764 :   bool is_union () const { return adt_kind == UNION; }
     939                 :             : 
     940                 :      798484 :   bool is_enum () const { return adt_kind == ENUM; }
     941                 :             : 
     942                 :             :   void accept_vis (TyVisitor &vis) override;
     943                 :             : 
     944                 :             :   void accept_vis (TyConstVisitor &vis) const override;
     945                 :             : 
     946                 :             :   std::string as_string () const override;
     947                 :             : 
     948                 :             :   bool is_equal (const BaseType &other) const override;
     949                 :             : 
     950                 :      271762 :   std::string get_identifier () const { return identifier; }
     951                 :             : 
     952                 :     1571692 :   std::string get_name () const override final
     953                 :             :   {
     954                 :     1571692 :     return identifier + subst_as_string ();
     955                 :             :   }
     956                 :             : 
     957                 :             :   DefId get_id () const;
     958                 :             : 
     959                 :             :   BaseType *clone () const final override;
     960                 :             : 
     961                 :      482499 :   size_t number_of_variants () const { return variants.size (); }
     962                 :             : 
     963                 :      127506 :   std::vector<VariantDef *> &get_variants () { return variants; }
     964                 :             : 
     965                 :     1272756 :   const std::vector<VariantDef *> &get_variants () const { return variants; }
     966                 :             : 
     967                 :           6 :   bool lookup_variant (const std::string &lookup,
     968                 :             :                        VariantDef **found_variant) const
     969                 :             :   {
     970                 :          13 :     for (auto &variant : variants)
     971                 :             :       {
     972                 :          10 :         if (variant->get_identifier ().compare (lookup) == 0)
     973                 :             :           {
     974                 :           3 :             *found_variant = variant;
     975                 :           3 :             return true;
     976                 :             :           }
     977                 :             :       }
     978                 :             :     return false;
     979                 :             :   }
     980                 :             : 
     981                 :        8290 :   bool lookup_variant_by_id (HirId id, VariantDef **found_variant,
     982                 :             :                              int *index = nullptr) const
     983                 :             :   {
     984                 :        8290 :     int i = 0;
     985                 :       15708 :     for (auto &variant : variants)
     986                 :             :       {
     987                 :       15708 :         if (variant->get_id () == id)
     988                 :             :           {
     989                 :        8290 :             if (index != nullptr)
     990                 :        4418 :               *index = i;
     991                 :             : 
     992                 :        8290 :             *found_variant = variant;
     993                 :        8290 :             return true;
     994                 :             :           }
     995                 :        7418 :         i++;
     996                 :             :       }
     997                 :             :     return false;
     998                 :             :   }
     999                 :             : 
    1000                 :             :   ADTType *
    1001                 :             :   handle_substitions (SubstitutionArgumentMappings &mappings) override final;
    1002                 :             : 
    1003                 :             : private:
    1004                 :             :   DefId id;
    1005                 :             :   std::string identifier;
    1006                 :             :   std::vector<VariantDef *> variants;
    1007                 :             :   ADTType::ADTKind adt_kind;
    1008                 :             :   ReprOptions repr;
    1009                 :             : };
    1010                 :             : 
    1011                 :       50808 : class FnParam
    1012                 :             : {
    1013                 :             : public:
    1014                 :       77341 :   FnParam (std::unique_ptr<HIR::Pattern> pattern, BaseType *type)
    1015                 :       77341 :     : pattern (std::move (pattern)), type (type)
    1016                 :             :   {}
    1017                 :             : 
    1018                 :             :   FnParam (const FnParam &) = delete;
    1019                 :       50808 :   FnParam (FnParam &&) = default;
    1020                 :             :   FnParam &operator= (FnParam &&) = default;
    1021                 :             : 
    1022                 :       15094 :   HIR::Pattern &get_pattern () { return *pattern; }
    1023                 :      139367 :   const HIR::Pattern &get_pattern () const { return *pattern; }
    1024                 :             : 
    1025                 :       11953 :   bool has_pattern () { return pattern != nullptr; }
    1026                 :      218940 :   BaseType *get_type () const { return type; }
    1027                 :       13010 :   void set_type (BaseType *new_type) { type = new_type; }
    1028                 :             : 
    1029                 :       19378 :   FnParam clone () const
    1030                 :             :   {
    1031                 :       19378 :     return FnParam (pattern->clone_pattern (), type->clone ());
    1032                 :             :   }
    1033                 :             : 
    1034                 :           0 :   FnParam monomorphized_clone () const
    1035                 :             :   {
    1036                 :           0 :     return FnParam (pattern->clone_pattern (), type->monomorphized_clone ());
    1037                 :             :   }
    1038                 :             : 
    1039                 :             : private:
    1040                 :             :   std::unique_ptr<HIR::Pattern> pattern;
    1041                 :             :   BaseType *type;
    1042                 :             : };
    1043                 :             : 
    1044                 :             : class FnType : public CallableTypeInterface, public SubstitutionRef
    1045                 :             : {
    1046                 :             : public:
    1047                 :             :   static constexpr auto KIND = TypeKind::FNDEF;
    1048                 :             : 
    1049                 :             :   static const uint8_t FNTYPE_DEFAULT_FLAGS = 0x00;
    1050                 :             :   static const uint8_t FNTYPE_IS_METHOD_FLAG = 0x01;
    1051                 :             :   static const uint8_t FNTYPE_IS_EXTERN_FLAG = 0x02;
    1052                 :             :   static const uint8_t FNTYPE_IS_VARADIC_FLAG = 0X04;
    1053                 :             :   static const uint8_t FNTYPE_IS_SYN_CONST_FLAG = 0X08;
    1054                 :             : 
    1055                 :       40073 :   FnType (HirId ref, DefId id, std::string identifier, RustIdent ident,
    1056                 :             :           uint8_t flags, ABI abi, std::vector<FnParam> params, BaseType *type,
    1057                 :             :           std::vector<SubstitutionParamMapping> subst_refs,
    1058                 :             :           SubstitutionArgumentMappings substitution_argument_mappings,
    1059                 :             :           RegionConstraints region_constraints,
    1060                 :             :           std::set<HirId> refs = std::set<HirId> ())
    1061                 :       40073 :     : CallableTypeInterface (ref, ref, TypeKind::FNDEF, ident, refs),
    1062                 :             :       SubstitutionRef (std::move (subst_refs), substitution_argument_mappings,
    1063                 :             :                        region_constraints),
    1064                 :       40073 :       params (std::move (params)), type (type), flags (flags),
    1065                 :       80146 :       identifier (identifier), id (id), abi (abi)
    1066                 :             :   {
    1067                 :       40073 :     LocalDefId local_def_id = id.localDefId;
    1068                 :       40073 :     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
    1069                 :       40073 :   }
    1070                 :             : 
    1071                 :       12887 :   FnType (HirId ref, HirId ty_ref, DefId id, std::string identifier,
    1072                 :             :           RustIdent ident, uint8_t flags, ABI abi, std::vector<FnParam> params,
    1073                 :             :           BaseType *type, std::vector<SubstitutionParamMapping> subst_refs,
    1074                 :             :           SubstitutionArgumentMappings substitution_argument_mappings,
    1075                 :             :           RegionConstraints region_constraints,
    1076                 :             :           std::set<HirId> refs = std::set<HirId> ())
    1077                 :       12887 :     : CallableTypeInterface (ref, ty_ref, TypeKind::FNDEF, ident, refs),
    1078                 :             :       SubstitutionRef (std::move (subst_refs), substitution_argument_mappings,
    1079                 :             :                        region_constraints),
    1080                 :       12887 :       params (std::move (params)), type (type), flags (flags),
    1081                 :       25774 :       identifier (identifier), id (id), abi (abi)
    1082                 :             :   {
    1083                 :       12887 :     LocalDefId local_def_id = id.localDefId;
    1084                 :       12887 :     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
    1085                 :       12887 :   }
    1086                 :             : 
    1087                 :             :   FnType (const FnType &) = delete;
    1088                 :             :   FnType (FnType &&) = default;
    1089                 :             : 
    1090                 :             :   void accept_vis (TyVisitor &vis) override;
    1091                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1092                 :             : 
    1093                 :             :   std::string as_string () const override;
    1094                 :             : 
    1095                 :       63386 :   std::string get_name () const override final { return as_string (); }
    1096                 :             : 
    1097                 :       60870 :   std::string get_identifier () const { return identifier; }
    1098                 :             : 
    1099                 :             :   bool is_equal (const BaseType &other) const override;
    1100                 :             : 
    1101                 :       82933 :   size_t num_params () const { return params.size (); }
    1102                 :             : 
    1103                 :       85386 :   bool is_method () const
    1104                 :             :   {
    1105                 :       85386 :     if (num_params () == 0)
    1106                 :             :       return false;
    1107                 :             : 
    1108                 :       47286 :     return (flags & FNTYPE_IS_METHOD_FLAG) != 0;
    1109                 :             :   }
    1110                 :             : 
    1111                 :             :   bool is_extern () const { return (flags & FNTYPE_IS_EXTERN_FLAG) != 0; }
    1112                 :             : 
    1113                 :       25850 :   bool is_variadic () const { return (flags & FNTYPE_IS_VARADIC_FLAG) != 0; }
    1114                 :             : 
    1115                 :        9182 :   bool is_syn_constant () const
    1116                 :             :   {
    1117                 :        9182 :     return (flags & FNTYPE_IS_SYN_CONST_FLAG) != 0;
    1118                 :             :   }
    1119                 :             : 
    1120                 :       55838 :   DefId get_id () const { return id; }
    1121                 :             : 
    1122                 :             :   // get the Self type for the method
    1123                 :       40969 :   BaseType *get_self_type () const
    1124                 :             :   {
    1125                 :       81938 :     rust_assert (is_method ());
    1126                 :       81938 :     return param_at (0).get_type ();
    1127                 :             :   }
    1128                 :             : 
    1129                 :       14425 :   std::vector<FnParam> &get_params () { return params; }
    1130                 :             : 
    1131                 :       24543 :   const std::vector<FnParam> &get_params () const { return params; }
    1132                 :             : 
    1133                 :       28896 :   FnParam &param_at (size_t idx) { return params.at (idx); }
    1134                 :             : 
    1135                 :       57130 :   const FnParam &param_at (size_t idx) const { return params.at (idx); }
    1136                 :             : 
    1137                 :             :   BaseType *clone () const final override;
    1138                 :             : 
    1139                 :             :   FnType *
    1140                 :             :   handle_substitions (SubstitutionArgumentMappings &mappings) override final;
    1141                 :             : 
    1142                 :       29587 :   ABI get_abi () const { return abi; }
    1143                 :           0 :   uint8_t get_flags () const { return flags; }
    1144                 :             : 
    1145                 :          22 :   WARN_UNUSED_RESULT size_t get_num_params () const override
    1146                 :             :   {
    1147                 :          22 :     return params.size ();
    1148                 :             :   }
    1149                 :             : 
    1150                 :          22 :   WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override
    1151                 :             :   {
    1152                 :          22 :     return param_at (index).get_type ();
    1153                 :             :   }
    1154                 :             : 
    1155                 :      113754 :   WARN_UNUSED_RESULT BaseType *get_return_type () const override
    1156                 :             :   {
    1157                 :      113754 :     return type;
    1158                 :             :   }
    1159                 :             : 
    1160                 :             : private:
    1161                 :             :   std::vector<FnParam> params;
    1162                 :             :   BaseType *type;
    1163                 :             :   uint8_t flags;
    1164                 :             :   std::string identifier;
    1165                 :             :   DefId id;
    1166                 :             :   ABI abi;
    1167                 :             : };
    1168                 :             : 
    1169                 :             : class FnPtr : public CallableTypeInterface
    1170                 :             : {
    1171                 :             : public:
    1172                 :             :   static constexpr auto KIND = TypeKind::FNPTR;
    1173                 :             : 
    1174                 :          60 :   FnPtr (HirId ref, location_t locus, std::vector<TyVar> params,
    1175                 :             :          TyVar result_type, ABI abi, Unsafety unsafety,
    1176                 :             :          std::set<HirId> refs = std::set<HirId> ())
    1177                 :          60 :     : CallableTypeInterface (ref, ref, TypeKind::FNPTR,
    1178                 :          60 :                              {Resolver::CanonicalPath::create_empty (), locus},
    1179                 :             :                              refs),
    1180                 :          60 :       params (std::move (params)), result_type (result_type), abi (abi),
    1181                 :         120 :       unsafety (unsafety)
    1182                 :          60 :   {}
    1183                 :             : 
    1184                 :        1384 :   FnPtr (HirId ref, HirId ty_ref, location_t locus, std::vector<TyVar> params,
    1185                 :             :          TyVar result_type, ABI abi, Unsafety unsafety,
    1186                 :             :          std::set<HirId> refs = std::set<HirId> ())
    1187                 :        1384 :     : CallableTypeInterface (ref, ty_ref, TypeKind::FNPTR,
    1188                 :        1384 :                              {Resolver::CanonicalPath::create_empty (), locus},
    1189                 :             :                              refs),
    1190                 :        2768 :       params (params), result_type (result_type), abi (abi), unsafety (unsafety)
    1191                 :        1384 :   {}
    1192                 :             : 
    1193                 :        9247 :   std::string get_name () const override final { return as_string (); }
    1194                 :             : 
    1195                 :           0 :   WARN_UNUSED_RESULT size_t get_num_params () const override
    1196                 :             :   {
    1197                 :           0 :     return params.size ();
    1198                 :             :   }
    1199                 :             : 
    1200                 :         183 :   WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override
    1201                 :             :   {
    1202                 :         183 :     return params.at (index).get_tyty ();
    1203                 :             :   }
    1204                 :             : 
    1205                 :       14438 :   WARN_UNUSED_RESULT BaseType *get_return_type () const override
    1206                 :             :   {
    1207                 :       14438 :     return result_type.get_tyty ();
    1208                 :             :   }
    1209                 :             : 
    1210                 :           0 :   const TyVar &get_var_return_type () const { return result_type; }
    1211                 :             : 
    1212                 :         446 :   size_t num_params () const { return params.size (); }
    1213                 :             : 
    1214                 :             :   void accept_vis (TyVisitor &vis) override;
    1215                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1216                 :             : 
    1217                 :             :   std::string as_string () const override;
    1218                 :             : 
    1219                 :             :   bool is_equal (const BaseType &other) const override;
    1220                 :             : 
    1221                 :             :   BaseType *clone () const final override;
    1222                 :             : 
    1223                 :          20 :   std::vector<TyVar> &get_params () { return params; }
    1224                 :       13726 :   const std::vector<TyVar> &get_params () const { return params; }
    1225                 :             : 
    1226                 :        9618 :   ABI get_abi () const { return abi; }
    1227                 :             : 
    1228                 :       10944 :   Unsafety get_unsafety () const { return unsafety; }
    1229                 :             : 
    1230                 :             :   FnPtr *handle_substitions (SubstitutionArgumentMappings &mappings);
    1231                 :             : 
    1232                 :             : private:
    1233                 :             :   std::vector<TyVar> params;
    1234                 :             :   TyVar result_type;
    1235                 :             :   ABI abi;
    1236                 :             :   Unsafety unsafety;
    1237                 :             : };
    1238                 :             : 
    1239                 :             : class ClosureType : public CallableTypeInterface, public SubstitutionRef
    1240                 :             : {
    1241                 :             : public:
    1242                 :             :   static constexpr auto KIND = TypeKind::CLOSURE;
    1243                 :             : 
    1244                 :          65 :   ClosureType (HirId ref, DefId id, RustIdent ident, TupleType *parameters,
    1245                 :             :                TyVar result_type,
    1246                 :             :                std::vector<SubstitutionParamMapping> subst_refs,
    1247                 :             :                std::set<NodeId> captures,
    1248                 :             :                std::set<HirId> refs = std::set<HirId> (),
    1249                 :             :                std::vector<TypeBoundPredicate> specified_bounds
    1250                 :             :                = std::vector<TypeBoundPredicate> ())
    1251                 :          65 :     : CallableTypeInterface (ref, ref, TypeKind::CLOSURE, ident, refs),
    1252                 :             :       SubstitutionRef (std::move (subst_refs),
    1253                 :         130 :                        SubstitutionArgumentMappings::error (),
    1254                 :             :                        {}), // TODO: check region constraints
    1255                 :          65 :       parameters (parameters), result_type (std::move (result_type)), id (id),
    1256                 :         130 :       captures (captures)
    1257                 :             :   {
    1258                 :          65 :     LocalDefId local_def_id = id.localDefId;
    1259                 :          65 :     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
    1260                 :          65 :     inherit_bounds (specified_bounds);
    1261                 :          65 :   }
    1262                 :             : 
    1263                 :         319 :   ClosureType (HirId ref, HirId ty_ref, RustIdent ident, DefId id,
    1264                 :             :                TupleType *parameters, TyVar result_type,
    1265                 :             :                std::vector<SubstitutionParamMapping> subst_refs,
    1266                 :             :                std::set<NodeId> captures,
    1267                 :             :                std::set<HirId> refs = std::set<HirId> (),
    1268                 :             :                std::vector<TypeBoundPredicate> specified_bounds
    1269                 :             :                = std::vector<TypeBoundPredicate> ())
    1270                 :         319 :     : CallableTypeInterface (ref, ty_ref, TypeKind::CLOSURE, ident, refs),
    1271                 :             :       SubstitutionRef (std::move (subst_refs),
    1272                 :         638 :                        SubstitutionArgumentMappings::error (), {}), // TODO
    1273                 :         319 :       parameters (parameters), result_type (std::move (result_type)), id (id),
    1274                 :         638 :       captures (captures)
    1275                 :             :   {
    1276                 :         319 :     LocalDefId local_def_id = id.localDefId;
    1277                 :         319 :     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
    1278                 :         319 :     inherit_bounds (specified_bounds);
    1279                 :         319 :   }
    1280                 :             : 
    1281                 :             :   void accept_vis (TyVisitor &vis) override;
    1282                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1283                 :             : 
    1284                 :           0 :   WARN_UNUSED_RESULT size_t get_num_params () const override
    1285                 :             :   {
    1286                 :           0 :     return parameters->num_fields ();
    1287                 :             :   }
    1288                 :             : 
    1289                 :           0 :   WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override
    1290                 :             :   {
    1291                 :           0 :     return parameters->get_field (index);
    1292                 :             :   }
    1293                 :             : 
    1294                 :           0 :   WARN_UNUSED_RESULT BaseType *get_return_type () const override
    1295                 :             :   {
    1296                 :           0 :     return result_type.get_tyty ();
    1297                 :             :   }
    1298                 :             : 
    1299                 :             :   std::string as_string () const override;
    1300                 :        2054 :   std::string get_name () const override final { return as_string (); }
    1301                 :             : 
    1302                 :             :   bool is_equal (const BaseType &other) const override;
    1303                 :             : 
    1304                 :             :   BaseType *clone () const final override;
    1305                 :             : 
    1306                 :             :   ClosureType *
    1307                 :             :   handle_substitions (SubstitutionArgumentMappings &mappings) override final;
    1308                 :             : 
    1309                 :         642 :   TyTy::TupleType &get_parameters () const { return *parameters; }
    1310                 :         644 :   TyTy::BaseType &get_result_type () const { return *result_type.get_tyty (); }
    1311                 :             : 
    1312                 :         390 :   DefId get_def_id () const { return id; }
    1313                 :             : 
    1314                 :             :   void setup_fn_once_output () const;
    1315                 :             : 
    1316                 :         365 :   const std::set<NodeId> &get_captures () const { return captures; }
    1317                 :             : 
    1318                 :             : private:
    1319                 :             :   TyTy::TupleType *parameters;
    1320                 :             :   TyVar result_type;
    1321                 :             :   DefId id;
    1322                 :             :   std::set<NodeId> captures;
    1323                 :             : };
    1324                 :             : 
    1325                 :        4704 : class ArrayType : public BaseType
    1326                 :             : {
    1327                 :             : public:
    1328                 :             :   static constexpr auto KIND = TypeKind::ARRAY;
    1329                 :             : 
    1330                 :        1117 :   ArrayType (HirId ref, location_t locus, TyVar capacity, TyVar base,
    1331                 :             :              std::set<HirId> refs = std::set<HirId> ())
    1332                 :        1117 :     : BaseType (ref, ref, TypeKind::ARRAY,
    1333                 :        1117 :                 {Resolver::CanonicalPath::create_empty (), locus}, refs),
    1334                 :        2234 :       element_type (base), capacity (capacity)
    1335                 :        1117 :   {}
    1336                 :             : 
    1337                 :        2450 :   ArrayType (HirId ref, HirId ty_ref, location_t locus, TyVar capacity,
    1338                 :             :              TyVar base, std::set<HirId> refs = std::set<HirId> ())
    1339                 :        2450 :     : BaseType (ref, ty_ref, TypeKind::ARRAY,
    1340                 :        2450 :                 {Resolver::CanonicalPath::create_empty (), locus}, refs),
    1341                 :        4900 :       element_type (base), capacity (capacity)
    1342                 :        2450 :   {}
    1343                 :             : 
    1344                 :             :   void accept_vis (TyVisitor &vis) override;
    1345                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1346                 :             : 
    1347                 :             :   std::string as_string () const override;
    1348                 :             : 
    1349                 :       54143 :   std::string get_name () const override final { return as_string (); }
    1350                 :             : 
    1351                 :             :   bool is_equal (const BaseType &other) const override;
    1352                 :             : 
    1353                 :             :   BaseType *get_element_type () const;
    1354                 :             :   const TyVar &get_var_element_type () const;
    1355                 :             : 
    1356                 :             :   BaseType *clone () const final override;
    1357                 :             : 
    1358                 :             :   BaseType *get_capacity () const;
    1359                 :          80 :   const TyVar &get_capacity_var () const { return capacity; }
    1360                 :             : 
    1361                 :             :   ArrayType *handle_substitions (SubstitutionArgumentMappings &mappings);
    1362                 :             : 
    1363                 :             : private:
    1364                 :             :   TyVar element_type;
    1365                 :             :   TyVar capacity;
    1366                 :             : };
    1367                 :             : 
    1368                 :        7336 : class SliceType : public BaseType
    1369                 :             : {
    1370                 :             : public:
    1371                 :             :   static constexpr auto KIND = TypeKind::SLICE;
    1372                 :             : 
    1373                 :         916 :   SliceType (HirId ref, location_t locus, TyVar base,
    1374                 :             :              std::set<HirId> refs = std::set<HirId> ())
    1375                 :         916 :     : BaseType (ref, ref, TypeKind::SLICE,
    1376                 :         916 :                 {Resolver::CanonicalPath::create_empty (), locus}, refs),
    1377                 :        1832 :       element_type (base)
    1378                 :         916 :   {}
    1379                 :             : 
    1380                 :       50062 :   SliceType (HirId ref, HirId ty_ref, location_t locus, TyVar base,
    1381                 :             :              std::set<HirId> refs = std::set<HirId> ())
    1382                 :       50062 :     : BaseType (ref, ty_ref, TypeKind::SLICE,
    1383                 :       50062 :                 {Resolver::CanonicalPath::create_empty (), locus}, refs),
    1384                 :      100124 :       element_type (base)
    1385                 :       50062 :   {}
    1386                 :             : 
    1387                 :             :   void accept_vis (TyVisitor &vis) override;
    1388                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1389                 :             : 
    1390                 :             :   std::string as_string () const override;
    1391                 :             : 
    1392                 :       43751 :   std::string get_name () const override final { return as_string (); }
    1393                 :             : 
    1394                 :             :   bool is_equal (const BaseType &other) const override;
    1395                 :             : 
    1396                 :             :   BaseType *get_element_type () const;
    1397                 :             :   const TyVar &get_var_element_type () const;
    1398                 :             : 
    1399                 :             :   BaseType *clone () const final override;
    1400                 :             : 
    1401                 :             :   SliceType *handle_substitions (SubstitutionArgumentMappings &mappings);
    1402                 :             : 
    1403                 :             : private:
    1404                 :             :   TyVar element_type;
    1405                 :             : };
    1406                 :             : 
    1407                 :             : class BoolType : public BaseType
    1408                 :             : {
    1409                 :             : public:
    1410                 :             :   static constexpr auto KIND = TypeKind::BOOL;
    1411                 :             : 
    1412                 :             :   BoolType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
    1413                 :             :   BoolType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ());
    1414                 :             : 
    1415                 :             :   void accept_vis (TyVisitor &vis) override;
    1416                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1417                 :             : 
    1418                 :             :   std::string as_string () const override;
    1419                 :             : 
    1420                 :             :   std::string get_name () const override final;
    1421                 :             : 
    1422                 :             :   BaseType *clone () const final override;
    1423                 :             : };
    1424                 :             : 
    1425                 :             : class IntType : public BaseType
    1426                 :             : {
    1427                 :             : public:
    1428                 :             :   enum IntKind
    1429                 :             :   {
    1430                 :             :     I8,
    1431                 :             :     I16,
    1432                 :             :     I32,
    1433                 :             :     I64,
    1434                 :             :     I128
    1435                 :             :   };
    1436                 :             : 
    1437                 :             :   static constexpr auto KIND = TypeKind::INT;
    1438                 :             : 
    1439                 :             :   IntType (HirId ref, IntKind kind, std::set<HirId> refs = std::set<HirId> ());
    1440                 :             :   IntType (HirId ref, HirId ty_ref, IntKind kind,
    1441                 :             :            std::set<HirId> refs = std::set<HirId> ());
    1442                 :             : 
    1443                 :             :   void accept_vis (TyVisitor &vis) override;
    1444                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1445                 :             : 
    1446                 :             :   std::string as_string () const override;
    1447                 :             : 
    1448                 :             :   std::string get_name () const override final;
    1449                 :             : 
    1450                 :             :   IntKind get_int_kind () const;
    1451                 :             : 
    1452                 :             :   BaseType *clone () const final override;
    1453                 :             : 
    1454                 :             :   bool is_equal (const BaseType &other) const override;
    1455                 :             : 
    1456                 :             : private:
    1457                 :             :   IntKind int_kind;
    1458                 :             : };
    1459                 :             : 
    1460                 :             : class UintType : public BaseType
    1461                 :             : {
    1462                 :             : public:
    1463                 :             :   static constexpr auto KIND = TypeKind::UINT;
    1464                 :             : 
    1465                 :             :   enum UintKind
    1466                 :             :   {
    1467                 :             :     U8,
    1468                 :             :     U16,
    1469                 :             :     U32,
    1470                 :             :     U64,
    1471                 :             :     U128
    1472                 :             :   };
    1473                 :             : 
    1474                 :             :   UintType (HirId ref, UintKind kind,
    1475                 :             :             std::set<HirId> refs = std::set<HirId> ());
    1476                 :             :   UintType (HirId ref, HirId ty_ref, UintKind kind,
    1477                 :             :             std::set<HirId> refs = std::set<HirId> ());
    1478                 :             : 
    1479                 :             :   void accept_vis (TyVisitor &vis) override;
    1480                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1481                 :             : 
    1482                 :             :   std::string as_string () const override;
    1483                 :             : 
    1484                 :             :   std::string get_name () const override final;
    1485                 :             : 
    1486                 :             :   UintKind get_uint_kind () const;
    1487                 :             : 
    1488                 :             :   BaseType *clone () const final override;
    1489                 :             : 
    1490                 :             :   bool is_equal (const BaseType &other) const override;
    1491                 :             : 
    1492                 :             : private:
    1493                 :             :   UintKind uint_kind;
    1494                 :             : };
    1495                 :             : 
    1496                 :             : class FloatType : public BaseType
    1497                 :             : {
    1498                 :             : public:
    1499                 :             :   static constexpr auto KIND = TypeKind::FLOAT;
    1500                 :             : 
    1501                 :             :   enum FloatKind
    1502                 :             :   {
    1503                 :             :     F32,
    1504                 :             :     F64
    1505                 :             :   };
    1506                 :             : 
    1507                 :             :   FloatType (HirId ref, FloatKind kind,
    1508                 :             :              std::set<HirId> refs = std::set<HirId> ());
    1509                 :             :   FloatType (HirId ref, HirId ty_ref, FloatKind kind,
    1510                 :             :              std::set<HirId> refs = std::set<HirId> ());
    1511                 :             : 
    1512                 :             :   void accept_vis (TyVisitor &vis) override;
    1513                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1514                 :             : 
    1515                 :             :   std::string as_string () const override;
    1516                 :             :   std::string get_name () const override final;
    1517                 :             : 
    1518                 :             :   FloatKind get_float_kind () const;
    1519                 :             : 
    1520                 :             :   BaseType *clone () const final override;
    1521                 :             : 
    1522                 :             :   bool is_equal (const BaseType &other) const override;
    1523                 :             : 
    1524                 :             : private:
    1525                 :             :   FloatKind float_kind;
    1526                 :             : };
    1527                 :             : 
    1528                 :             : class USizeType : public BaseType
    1529                 :             : {
    1530                 :             : public:
    1531                 :             :   static constexpr auto KIND = TypeKind::USIZE;
    1532                 :             : 
    1533                 :             :   USizeType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
    1534                 :             :   USizeType (HirId ref, HirId ty_ref,
    1535                 :             :              std::set<HirId> refs = std::set<HirId> ());
    1536                 :             : 
    1537                 :             :   void accept_vis (TyVisitor &vis) override;
    1538                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1539                 :             : 
    1540                 :             :   std::string as_string () const override;
    1541                 :             :   std::string get_name () const override final;
    1542                 :             : 
    1543                 :             :   BaseType *clone () const final override;
    1544                 :             : };
    1545                 :             : 
    1546                 :             : class ISizeType : public BaseType
    1547                 :             : {
    1548                 :             : public:
    1549                 :             :   static constexpr auto KIND = TypeKind::ISIZE;
    1550                 :             : 
    1551                 :             :   ISizeType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
    1552                 :             :   ISizeType (HirId ref, HirId ty_ref,
    1553                 :             :              std::set<HirId> refs = std::set<HirId> ());
    1554                 :             : 
    1555                 :             :   void accept_vis (TyVisitor &vis) override;
    1556                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1557                 :             : 
    1558                 :             :   std::string as_string () const override;
    1559                 :             :   std::string get_name () const override final;
    1560                 :             : 
    1561                 :             :   BaseType *clone () const final override;
    1562                 :             : };
    1563                 :             : 
    1564                 :             : class CharType : public BaseType
    1565                 :             : {
    1566                 :             : public:
    1567                 :             :   static constexpr auto KIND = TypeKind::CHAR;
    1568                 :             : 
    1569                 :             :   CharType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
    1570                 :             :   CharType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ());
    1571                 :             : 
    1572                 :             :   void accept_vis (TyVisitor &vis) override;
    1573                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1574                 :             : 
    1575                 :             :   std::string as_string () const override;
    1576                 :             :   std::string get_name () const override final;
    1577                 :             : 
    1578                 :             :   BaseType *clone () const final override;
    1579                 :             : };
    1580                 :             : 
    1581                 :             : class StrType : public BaseType
    1582                 :             : {
    1583                 :             : public:
    1584                 :             :   static constexpr auto KIND = TypeKind::STR;
    1585                 :             : 
    1586                 :             :   StrType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
    1587                 :             :   StrType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ());
    1588                 :             : 
    1589                 :             :   std::string get_name () const override final;
    1590                 :             : 
    1591                 :             :   void accept_vis (TyVisitor &vis) override;
    1592                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1593                 :             : 
    1594                 :             :   std::string as_string () const override;
    1595                 :             : 
    1596                 :             :   bool is_equal (const BaseType &other) const override;
    1597                 :             : 
    1598                 :             :   BaseType *clone () const final override;
    1599                 :             : };
    1600                 :             : 
    1601                 :             : class DynamicObjectType : public BaseType
    1602                 :             : {
    1603                 :             : public:
    1604                 :             :   static constexpr auto KIND = TypeKind::DYNAMIC;
    1605                 :             : 
    1606                 :             :   DynamicObjectType (HirId ref, RustIdent ident,
    1607                 :             :                      std::vector<TypeBoundPredicate> specified_bounds,
    1608                 :             :                      std::set<HirId> refs = std::set<HirId> ());
    1609                 :             : 
    1610                 :             :   DynamicObjectType (HirId ref, HirId ty_ref, RustIdent ident,
    1611                 :             :                      std::vector<TypeBoundPredicate> specified_bounds,
    1612                 :             :                      std::set<HirId> refs = std::set<HirId> ());
    1613                 :             : 
    1614                 :             :   void accept_vis (TyVisitor &vis) override;
    1615                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1616                 :             : 
    1617                 :             :   std::string as_string () const override;
    1618                 :             : 
    1619                 :             :   bool is_equal (const BaseType &other) const override;
    1620                 :             : 
    1621                 :             :   BaseType *clone () const final override;
    1622                 :             : 
    1623                 :             :   std::string get_name () const override final;
    1624                 :             : 
    1625                 :             :   // this returns a flat list of items including super trait bounds
    1626                 :             :   const std::vector<
    1627                 :             :     std::pair<const Resolver::TraitItemReference *, const TypeBoundPredicate *>>
    1628                 :             :   get_object_items () const;
    1629                 :             : };
    1630                 :             : 
    1631                 :       35852 : class ReferenceType : public BaseType
    1632                 :             : {
    1633                 :             : public:
    1634                 :             :   static constexpr auto KIND = REF;
    1635                 :             : 
    1636                 :             :   ReferenceType (HirId ref, TyVar base, Mutability mut,
    1637                 :             :                  Region region = Region::make_anonymous (),
    1638                 :             :                  std::set<HirId> refs = std::set<HirId> ());
    1639                 :             :   ReferenceType (HirId ref, HirId ty_ref, TyVar base, Mutability mut,
    1640                 :             :                  Region region = Region::make_anonymous (),
    1641                 :             :                  std::set<HirId> refs = std::set<HirId> ());
    1642                 :             : 
    1643                 :             :   BaseType *get_base () const;
    1644                 :             :   const TyVar &get_var_element_type () const;
    1645                 :             : 
    1646                 :             :   void accept_vis (TyVisitor &vis) override;
    1647                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1648                 :             : 
    1649                 :             :   std::string as_string () const override;
    1650                 :             : 
    1651                 :             :   std::string get_name () const override final;
    1652                 :             : 
    1653                 :             :   bool is_equal (const BaseType &other) const override;
    1654                 :             : 
    1655                 :             :   BaseType *clone () const final override;
    1656                 :             : 
    1657                 :             :   ReferenceType *handle_substitions (SubstitutionArgumentMappings &mappings);
    1658                 :             : 
    1659                 :             :   Mutability mutability () const;
    1660                 :             :   bool is_mutable () const;
    1661                 :             : 
    1662                 :             :   WARN_UNUSED_RESULT Region get_region () const;
    1663                 :             :   void set_region (Region region);
    1664                 :             : 
    1665                 :             :   bool is_dyn_object () const;
    1666                 :             :   bool is_dyn_slice_type (const TyTy::SliceType **slice = nullptr) const;
    1667                 :             :   bool is_dyn_str_type (const TyTy::StrType **str = nullptr) const;
    1668                 :             :   bool is_dyn_obj_type (const TyTy::DynamicObjectType **dyn = nullptr) const;
    1669                 :             : 
    1670                 :             : private:
    1671                 :             :   TyVar base;
    1672                 :             :   Mutability mut;
    1673                 :             :   Region region;
    1674                 :             : };
    1675                 :             : 
    1676                 :       31494 : class PointerType : public BaseType
    1677                 :             : {
    1678                 :             : public:
    1679                 :             :   static constexpr auto KIND = TypeKind::POINTER;
    1680                 :             : 
    1681                 :             :   PointerType (HirId ref, TyVar base, Mutability mut,
    1682                 :             :                std::set<HirId> refs = std::set<HirId> ());
    1683                 :             :   PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut,
    1684                 :             :                std::set<HirId> refs = std::set<HirId> ());
    1685                 :             : 
    1686                 :             :   BaseType *get_base () const;
    1687                 :             :   const TyVar &get_var_element_type () const;
    1688                 :             : 
    1689                 :             :   void accept_vis (TyVisitor &vis) override;
    1690                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1691                 :             : 
    1692                 :             :   std::string as_string () const override;
    1693                 :             :   std::string get_name () const override final;
    1694                 :             : 
    1695                 :             :   bool is_equal (const BaseType &other) const override;
    1696                 :             : 
    1697                 :             :   BaseType *clone () const final override;
    1698                 :             : 
    1699                 :             :   PointerType *handle_substitions (SubstitutionArgumentMappings &mappings);
    1700                 :             : 
    1701                 :             :   Mutability mutability () const;
    1702                 :             :   bool is_mutable () const;
    1703                 :             :   bool is_const () const;
    1704                 :             :   bool is_dyn_object () const;
    1705                 :             :   bool is_dyn_slice_type (const TyTy::SliceType **slice = nullptr) const;
    1706                 :             :   bool is_dyn_str_type (const TyTy::StrType **str = nullptr) const;
    1707                 :             :   bool is_dyn_obj_type (const TyTy::DynamicObjectType **dyn = nullptr) const;
    1708                 :             : 
    1709                 :             : private:
    1710                 :             :   TyVar base;
    1711                 :             :   Mutability mut;
    1712                 :             : };
    1713                 :             : 
    1714                 :             : // https://doc.rust-lang.org/std/primitive.never.html
    1715                 :             : //
    1716                 :             : // Since the `!` type is really complicated and it is even still unstable
    1717                 :             : // in rustc, only fairly limited support for this type is introduced here.
    1718                 :             : // Unification between `!` and ANY other type (including `<T?>`) is simply
    1719                 :             : // not allowed. If it is needed, it should be handled manually. For example,
    1720                 :             : // unifying `!` with other types is very necessary when resolving types of
    1721                 :             : // `if/else` expressions.
    1722                 :             : //
    1723                 :             : // See related discussion at https://github.com/Rust-GCC/gccrs/pull/364
    1724                 :             : class NeverType : public BaseType
    1725                 :             : {
    1726                 :             : public:
    1727                 :             :   static constexpr auto KIND = TypeKind::NEVER;
    1728                 :             : 
    1729                 :             :   NeverType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
    1730                 :             : 
    1731                 :             :   NeverType (HirId ref, HirId ty_ref,
    1732                 :             :              std::set<HirId> refs = std::set<HirId> ());
    1733                 :             : 
    1734                 :             :   void accept_vis (TyVisitor &vis) override;
    1735                 :             : 
    1736                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1737                 :             : 
    1738                 :             :   std::string as_string () const override;
    1739                 :             : 
    1740                 :             :   BaseType *clone () const final override;
    1741                 :             : 
    1742                 :             :   std::string get_name () const override final;
    1743                 :             : };
    1744                 :             : 
    1745                 :             : // used at the type in associated types in traits
    1746                 :             : // see: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
    1747                 :             : class PlaceholderType : public BaseType
    1748                 :             : {
    1749                 :             : public:
    1750                 :             :   static constexpr auto KIND = TypeKind::PLACEHOLDER;
    1751                 :             : 
    1752                 :             :   PlaceholderType (std::string symbol, DefId id, HirId ref,
    1753                 :             :                    std::set<HirId> refs = std::set<HirId> ());
    1754                 :             :   PlaceholderType (std::string symbol, DefId id, HirId ref, HirId ty_ref,
    1755                 :             :                    std::set<HirId> refs = std::set<HirId> ());
    1756                 :             : 
    1757                 :             :   void accept_vis (TyVisitor &vis) override;
    1758                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1759                 :             : 
    1760                 :             :   std::string as_string () const override;
    1761                 :             : 
    1762                 :             :   BaseType *clone () const final override;
    1763                 :             : 
    1764                 :             :   std::string get_name () const override final;
    1765                 :             : 
    1766                 :             :   std::string get_symbol () const;
    1767                 :             : 
    1768                 :             :   void set_associated_type (HirId ref);
    1769                 :             : 
    1770                 :             :   void clear_associated_type ();
    1771                 :             : 
    1772                 :             :   bool can_resolve () const;
    1773                 :             : 
    1774                 :             :   BaseType *resolve () const;
    1775                 :             : 
    1776                 :             :   bool is_equal (const BaseType &other) const override;
    1777                 :             : 
    1778                 :             :   DefId get_def_id () const;
    1779                 :             : 
    1780                 :             : private:
    1781                 :             :   std::string symbol;
    1782                 :             :   DefId defId;
    1783                 :             : };
    1784                 :             : 
    1785                 :             : class ProjectionType : public BaseType, public SubstitutionRef
    1786                 :             : {
    1787                 :             : public:
    1788                 :             :   static constexpr auto KIND = TypeKind::PROJECTION;
    1789                 :             : 
    1790                 :             :   ProjectionType (HirId ref, BaseType *base,
    1791                 :             :                   const Resolver::TraitReference *trait, DefId item,
    1792                 :             :                   std::vector<SubstitutionParamMapping> subst_refs,
    1793                 :             :                   SubstitutionArgumentMappings generic_arguments
    1794                 :             :                   = SubstitutionArgumentMappings::error (),
    1795                 :             :                   RegionConstraints region_constraints = {},
    1796                 :             :                   std::set<HirId> refs = std::set<HirId> ());
    1797                 :             : 
    1798                 :             :   ProjectionType (HirId ref, HirId ty_ref, BaseType *base,
    1799                 :             :                   const Resolver::TraitReference *trait, DefId item,
    1800                 :             :                   std::vector<SubstitutionParamMapping> subst_refs,
    1801                 :             :                   SubstitutionArgumentMappings generic_arguments
    1802                 :             :                   = SubstitutionArgumentMappings::error (),
    1803                 :             :                   RegionConstraints region_constraints = {},
    1804                 :             :                   std::set<HirId> refs = std::set<HirId> ());
    1805                 :             : 
    1806                 :             :   void accept_vis (TyVisitor &vis) override;
    1807                 :             :   void accept_vis (TyConstVisitor &vis) const override;
    1808                 :             : 
    1809                 :             :   std::string as_string () const override;
    1810                 :             : 
    1811                 :             :   BaseType *clone () const final override;
    1812                 :             : 
    1813                 :             :   std::string get_name () const override final;
    1814                 :             : 
    1815                 :             :   const BaseType *get () const;
    1816                 :             :   BaseType *get ();
    1817                 :             : 
    1818                 :             :   ProjectionType *
    1819                 :             :   handle_substitions (SubstitutionArgumentMappings &mappings) override final;
    1820                 :             : 
    1821                 :             : private:
    1822                 :             :   BaseType *base;
    1823                 :             :   const Resolver::TraitReference *trait;
    1824                 :             :   DefId item;
    1825                 :             : };
    1826                 :             : 
    1827                 :             : template <>
    1828                 :             : WARN_UNUSED_RESULT inline bool
    1829                 :          22 : BaseType::is<CallableTypeInterface> () const
    1830                 :             : {
    1831                 :          22 :   auto kind = this->get_kind ();
    1832                 :          22 :   return kind == FNPTR || kind == FNDEF || kind == CLOSURE;
    1833                 :             : }
    1834                 :             : 
    1835                 :             : template <>
    1836                 :             : WARN_UNUSED_RESULT inline bool
    1837                 :          11 : BaseType::is<const CallableTypeInterface> () const
    1838                 :             : {
    1839                 :          11 :   return this->is<CallableTypeInterface> ();
    1840                 :             : }
    1841                 :             : 
    1842                 :             : template <>
    1843                 :             : WARN_UNUSED_RESULT inline bool
    1844                 :          17 : BaseType::is<SubstitutionRef> () const
    1845                 :             : {
    1846                 :          17 :   auto kind = this->get_kind ();
    1847                 :          34 :   return kind == FNPTR || kind == FNDEF || kind == CLOSURE || kind == ADT
    1848                 :          17 :          || kind == PROJECTION;
    1849                 :             : }
    1850                 :             : 
    1851                 :             : template <>
    1852                 :             : WARN_UNUSED_RESULT inline bool
    1853                 :             : BaseType::is<const SubstitutionRef> () const
    1854                 :             : {
    1855                 :             :   return this->is<SubstitutionRef> ();
    1856                 :             : }
    1857                 :             : 
    1858                 :             : template <>
    1859                 :             : WARN_UNUSED_RESULT inline SubstitutionRef *
    1860                 :          16 : BaseType::as<SubstitutionRef> ()
    1861                 :             : {
    1862                 :          16 :   auto kind = this->get_kind ();
    1863                 :          16 :   switch (kind)
    1864                 :             :     {
    1865                 :           0 :     case FNDEF:
    1866                 :           0 :       return static_cast<FnType *> (this);
    1867                 :           0 :     case CLOSURE:
    1868                 :           0 :       return static_cast<ClosureType *> (this);
    1869                 :          16 :     case ADT:
    1870                 :          16 :       return static_cast<ADTType *> (this);
    1871                 :           0 :     case PROJECTION:
    1872                 :           0 :       return static_cast<ProjectionType *> (this);
    1873                 :           0 :     default:
    1874                 :           0 :       rust_unreachable ();
    1875                 :             :     }
    1876                 :             : }
    1877                 :             : 
    1878                 :             : template <>
    1879                 :             : WARN_UNUSED_RESULT inline const SubstitutionRef *
    1880                 :           5 : BaseType::as<const SubstitutionRef> () const
    1881                 :             : {
    1882                 :           5 :   auto kind = this->get_kind ();
    1883                 :           5 :   switch (kind)
    1884                 :             :     {
    1885                 :           0 :     case FNDEF:
    1886                 :           0 :       return static_cast<const FnType *> (this);
    1887                 :           0 :     case CLOSURE:
    1888                 :           0 :       return static_cast<const ClosureType *> (this);
    1889                 :           5 :     case ADT:
    1890                 :           5 :       return static_cast<const ADTType *> (this);
    1891                 :           0 :     case PROJECTION:
    1892                 :           0 :       return static_cast<const ProjectionType *> (this);
    1893                 :           0 :     default:
    1894                 :           0 :       rust_unreachable ();
    1895                 :             :     }
    1896                 :             : }
    1897                 :             : 
    1898                 :             : template <>
    1899                 :             : WARN_UNUSED_RESULT inline SubstitutionRef *
    1900                 :          17 : BaseType::try_as<SubstitutionRef> ()
    1901                 :             : {
    1902                 :          17 :   if (this->is<SubstitutionRef> ())
    1903                 :             :     {
    1904                 :          16 :       return this->as<SubstitutionRef> ();
    1905                 :             :     }
    1906                 :             :   return nullptr;
    1907                 :             : }
    1908                 :             : 
    1909                 :             : template <>
    1910                 :             : WARN_UNUSED_RESULT inline const SubstitutionRef *
    1911                 :             : BaseType::try_as<const SubstitutionRef> () const
    1912                 :             : {
    1913                 :             :   if (this->is<const SubstitutionRef> ())
    1914                 :             :     {
    1915                 :             :       return this->as<const SubstitutionRef> ();
    1916                 :             :     }
    1917                 :             :   return nullptr;
    1918                 :             : }
    1919                 :             : 
    1920                 :             : } // namespace TyTy
    1921                 :             : } // namespace Rust
    1922                 :             : 
    1923                 :             : #endif // RUST_TYTY
        

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