(root)/
gcc-13.2.0/
gcc/
rust/
typecheck/
rust-substitution-mapper.h
       1  // Copyright (C) 2020-2023 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_SUBSTITUTION_MAPPER_H
      20  #define RUST_SUBSTITUTION_MAPPER_H
      21  
      22  #include "rust-tyty.h"
      23  #include "rust-tyty-visitor.h"
      24  
      25  namespace Rust {
      26  namespace Resolver {
      27  
      28  class SubstMapper : public TyTy::TyVisitor
      29  {
      30  public:
      31    static TyTy::BaseType *Resolve (TyTy::BaseType *base, Location locus,
      32  				  HIR::GenericArgs *generics = nullptr);
      33  
      34    static TyTy::BaseType *InferSubst (TyTy::BaseType *base, Location locus);
      35  
      36    bool have_generic_args () const;
      37  
      38    void visit (TyTy::FnType &type) override;
      39    void visit (TyTy::ADTType &type) override;
      40    void visit (TyTy::PlaceholderType &type) override;
      41    void visit (TyTy::ProjectionType &type) override;
      42  
      43    // nothing to do for these
      44    void visit (TyTy::InferType &) override { gcc_unreachable (); }
      45    void visit (TyTy::TupleType &) override { gcc_unreachable (); }
      46    void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
      47    void visit (TyTy::ArrayType &) override { gcc_unreachable (); }
      48    void visit (TyTy::SliceType &) override { gcc_unreachable (); }
      49    void visit (TyTy::BoolType &) override { gcc_unreachable (); }
      50    void visit (TyTy::IntType &) override { gcc_unreachable (); }
      51    void visit (TyTy::UintType &) override { gcc_unreachable (); }
      52    void visit (TyTy::FloatType &) override { gcc_unreachable (); }
      53    void visit (TyTy::USizeType &) override { gcc_unreachable (); }
      54    void visit (TyTy::ISizeType &) override { gcc_unreachable (); }
      55    void visit (TyTy::ErrorType &) override { gcc_unreachable (); }
      56    void visit (TyTy::CharType &) override { gcc_unreachable (); }
      57    void visit (TyTy::ReferenceType &) override { gcc_unreachable (); }
      58    void visit (TyTy::PointerType &) override { gcc_unreachable (); }
      59    void visit (TyTy::ParamType &) override { gcc_unreachable (); }
      60    void visit (TyTy::StrType &) override { gcc_unreachable (); }
      61    void visit (TyTy::NeverType &) override { gcc_unreachable (); }
      62    void visit (TyTy::DynamicObjectType &) override { gcc_unreachable (); }
      63    void visit (TyTy::ClosureType &) override { gcc_unreachable (); }
      64  
      65  private:
      66    SubstMapper (HirId ref, HIR::GenericArgs *generics, Location locus);
      67  
      68    TyTy::BaseType *resolved;
      69    HIR::GenericArgs *generics;
      70    Location locus;
      71  };
      72  
      73  class SubstMapperInternal : public TyTy::TyVisitor
      74  {
      75  public:
      76    static TyTy::BaseType *Resolve (TyTy::BaseType *base,
      77  				  TyTy::SubstitutionArgumentMappings &mappings);
      78  
      79    static bool mappings_are_bound (TyTy::BaseType *ty,
      80  				  TyTy::SubstitutionArgumentMappings &mappings);
      81  
      82    void visit (TyTy::FnType &type) override;
      83    void visit (TyTy::ADTType &type) override;
      84    void visit (TyTy::TupleType &type) override;
      85    void visit (TyTy::ReferenceType &type) override;
      86    void visit (TyTy::PointerType &type) override;
      87    void visit (TyTy::ParamType &type) override;
      88    void visit (TyTy::PlaceholderType &type) override;
      89    void visit (TyTy::ProjectionType &type) override;
      90    void visit (TyTy::ClosureType &type) override;
      91    void visit (TyTy::ArrayType &type) override;
      92    void visit (TyTy::SliceType &type) override;
      93    void visit (TyTy::InferType &type) override;
      94    void visit (TyTy::FnPtr &type) override;
      95    void visit (TyTy::BoolType &type) override;
      96    void visit (TyTy::IntType &type) override;
      97    void visit (TyTy::UintType &type) override;
      98    void visit (TyTy::FloatType &type) override;
      99    void visit (TyTy::USizeType &type) override;
     100    void visit (TyTy::ISizeType &type) override;
     101    void visit (TyTy::ErrorType &type) override;
     102    void visit (TyTy::CharType &type) override;
     103    void visit (TyTy::StrType &type) override;
     104    void visit (TyTy::NeverType &type) override;
     105    void visit (TyTy::DynamicObjectType &type) override;
     106  
     107  private:
     108    SubstMapperInternal (HirId ref, TyTy::SubstitutionArgumentMappings &mappings);
     109  
     110    TyTy::BaseType *resolved;
     111    TyTy::SubstitutionArgumentMappings &mappings;
     112  };
     113  
     114  class SubstMapperFromExisting : public TyTy::TyVisitor
     115  {
     116  public:
     117    static TyTy::BaseType *Resolve (TyTy::BaseType *concrete,
     118  				  TyTy::BaseType *receiver);
     119  
     120    void visit (TyTy::FnType &type) override;
     121    void visit (TyTy::ADTType &type) override;
     122    void visit (TyTy::ClosureType &type) override;
     123  
     124    void visit (TyTy::InferType &) override { gcc_unreachable (); }
     125    void visit (TyTy::TupleType &) override { gcc_unreachable (); }
     126    void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
     127    void visit (TyTy::ArrayType &) override { gcc_unreachable (); }
     128    void visit (TyTy::SliceType &) override { gcc_unreachable (); }
     129    void visit (TyTy::BoolType &) override { gcc_unreachable (); }
     130    void visit (TyTy::IntType &) override { gcc_unreachable (); }
     131    void visit (TyTy::UintType &) override { gcc_unreachable (); }
     132    void visit (TyTy::FloatType &) override { gcc_unreachable (); }
     133    void visit (TyTy::USizeType &) override { gcc_unreachable (); }
     134    void visit (TyTy::ISizeType &) override { gcc_unreachable (); }
     135    void visit (TyTy::ErrorType &) override { gcc_unreachable (); }
     136    void visit (TyTy::CharType &) override { gcc_unreachable (); }
     137    void visit (TyTy::ReferenceType &) override { gcc_unreachable (); }
     138    void visit (TyTy::PointerType &) override { gcc_unreachable (); }
     139    void visit (TyTy::ParamType &) override { gcc_unreachable (); }
     140    void visit (TyTy::StrType &) override { gcc_unreachable (); }
     141    void visit (TyTy::NeverType &) override { gcc_unreachable (); }
     142    void visit (TyTy::PlaceholderType &) override { gcc_unreachable (); }
     143    void visit (TyTy::ProjectionType &) override { gcc_unreachable (); }
     144    void visit (TyTy::DynamicObjectType &) override { gcc_unreachable (); }
     145  
     146  private:
     147    SubstMapperFromExisting (TyTy::BaseType *concrete, TyTy::BaseType *receiver);
     148  
     149    TyTy::BaseType *concrete;
     150    TyTy::BaseType *receiver;
     151    TyTy::BaseType *resolved;
     152  };
     153  
     154  class GetUsedSubstArgs : public TyTy::TyConstVisitor
     155  {
     156  public:
     157    static TyTy::SubstitutionArgumentMappings From (const TyTy::BaseType *from);
     158  
     159    void visit (const TyTy::FnType &type) override;
     160    void visit (const TyTy::ADTType &type) override;
     161    void visit (const TyTy::ClosureType &type) override;
     162  
     163    void visit (const TyTy::InferType &) override {}
     164    void visit (const TyTy::TupleType &) override {}
     165    void visit (const TyTy::FnPtr &) override {}
     166    void visit (const TyTy::ArrayType &) override {}
     167    void visit (const TyTy::SliceType &) override {}
     168    void visit (const TyTy::BoolType &) override {}
     169    void visit (const TyTy::IntType &) override {}
     170    void visit (const TyTy::UintType &) override {}
     171    void visit (const TyTy::FloatType &) override {}
     172    void visit (const TyTy::USizeType &) override {}
     173    void visit (const TyTy::ISizeType &) override {}
     174    void visit (const TyTy::ErrorType &) override {}
     175    void visit (const TyTy::CharType &) override {}
     176    void visit (const TyTy::ReferenceType &) override {}
     177    void visit (const TyTy::PointerType &) override {}
     178    void visit (const TyTy::ParamType &) override {}
     179    void visit (const TyTy::StrType &) override {}
     180    void visit (const TyTy::NeverType &) override {}
     181    void visit (const TyTy::PlaceholderType &) override {}
     182    void visit (const TyTy::ProjectionType &) override {}
     183    void visit (const TyTy::DynamicObjectType &) override {}
     184  
     185  private:
     186    GetUsedSubstArgs ();
     187  
     188    TyTy::SubstitutionArgumentMappings args;
     189  };
     190  
     191  } // namespace Resolver
     192  } // namespace Rust
     193  
     194  #endif // RUST_SUBSTITUTION_MAPPER_H