1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #ifndef RUST_SUBSTITUTION_MAPPER_H
20 #define RUST_SUBSTITUTION_MAPPER_H
22 #include "rust-tyty.h"
23 #include "rust-tyty-visitor.h"
28 class SubstMapper
: public TyTy::TyVisitor
31 static TyTy::BaseType
*Resolve (TyTy::BaseType
*base
, location_t locus
,
32 HIR::GenericArgs
*generics
= nullptr,
33 const std::vector
<TyTy::Region
> ®ions
36 static TyTy::BaseType
*InferSubst (TyTy::BaseType
*base
, location_t locus
);
38 bool have_generic_args () const;
40 void visit (TyTy::FnType
&type
) override
;
41 void visit (TyTy::ADTType
&type
) override
;
42 void visit (TyTy::PlaceholderType
&type
) override
;
43 void visit (TyTy::ProjectionType
&type
) override
;
45 // nothing to do for these
46 void visit (TyTy::InferType
&) override
{ rust_unreachable (); }
47 void visit (TyTy::TupleType
&) override
{ rust_unreachable (); }
48 void visit (TyTy::FnPtr
&) override
{ rust_unreachable (); }
49 void visit (TyTy::ArrayType
&) override
{ rust_unreachable (); }
50 void visit (TyTy::SliceType
&) override
{ rust_unreachable (); }
51 void visit (TyTy::BoolType
&) override
{ rust_unreachable (); }
52 void visit (TyTy::IntType
&) override
{ rust_unreachable (); }
53 void visit (TyTy::UintType
&) override
{ rust_unreachable (); }
54 void visit (TyTy::FloatType
&) override
{ rust_unreachable (); }
55 void visit (TyTy::USizeType
&) override
{ rust_unreachable (); }
56 void visit (TyTy::ISizeType
&) override
{ rust_unreachable (); }
57 void visit (TyTy::ErrorType
&) override
{ rust_unreachable (); }
58 void visit (TyTy::CharType
&) override
{ rust_unreachable (); }
59 void visit (TyTy::ReferenceType
&) override
{ rust_unreachable (); }
60 void visit (TyTy::PointerType
&) override
{ rust_unreachable (); }
61 void visit (TyTy::ParamType
&) override
{ rust_unreachable (); }
62 void visit (TyTy::StrType
&) override
{ rust_unreachable (); }
63 void visit (TyTy::NeverType
&) override
{ rust_unreachable (); }
64 void visit (TyTy::DynamicObjectType
&) override
{ rust_unreachable (); }
65 void visit (TyTy::ClosureType
&) override
{ rust_unreachable (); }
68 SubstMapper (HirId ref
, HIR::GenericArgs
*generics
,
69 const std::vector
<TyTy::Region
> ®ions
, location_t locus
);
71 TyTy::BaseType
*resolved
;
72 HIR::GenericArgs
*generics
;
73 const std::vector
<TyTy::Region
> ®ions
;
77 class SubstMapperInternal
: public TyTy::TyVisitor
80 static TyTy::BaseType
*Resolve (TyTy::BaseType
*base
,
81 TyTy::SubstitutionArgumentMappings
&mappings
);
83 static bool mappings_are_bound (TyTy::BaseType
*ty
,
84 TyTy::SubstitutionArgumentMappings
&mappings
);
86 void visit (TyTy::FnType
&type
) override
;
87 void visit (TyTy::ADTType
&type
) override
;
88 void visit (TyTy::TupleType
&type
) override
;
89 void visit (TyTy::ReferenceType
&type
) override
;
90 void visit (TyTy::PointerType
&type
) override
;
91 void visit (TyTy::ParamType
&type
) override
;
92 void visit (TyTy::PlaceholderType
&type
) override
;
93 void visit (TyTy::ProjectionType
&type
) override
;
94 void visit (TyTy::ClosureType
&type
) override
;
95 void visit (TyTy::ArrayType
&type
) override
;
96 void visit (TyTy::SliceType
&type
) override
;
97 void visit (TyTy::InferType
&type
) override
;
98 void visit (TyTy::FnPtr
&type
) override
;
99 void visit (TyTy::BoolType
&type
) override
;
100 void visit (TyTy::IntType
&type
) override
;
101 void visit (TyTy::UintType
&type
) override
;
102 void visit (TyTy::FloatType
&type
) override
;
103 void visit (TyTy::USizeType
&type
) override
;
104 void visit (TyTy::ISizeType
&type
) override
;
105 void visit (TyTy::ErrorType
&type
) override
;
106 void visit (TyTy::CharType
&type
) override
;
107 void visit (TyTy::StrType
&type
) override
;
108 void visit (TyTy::NeverType
&type
) override
;
109 void visit (TyTy::DynamicObjectType
&type
) override
;
112 SubstMapperInternal (HirId ref
, TyTy::SubstitutionArgumentMappings
&mappings
);
114 TyTy::BaseType
*resolved
;
115 TyTy::SubstitutionArgumentMappings
&mappings
;
118 class SubstMapperFromExisting
: public TyTy::TyVisitor
121 static TyTy::BaseType
*Resolve (TyTy::BaseType
*concrete
,
122 TyTy::BaseType
*receiver
);
124 void visit (TyTy::FnType
&type
) override
;
125 void visit (TyTy::ADTType
&type
) override
;
126 void visit (TyTy::ClosureType
&type
) override
;
128 void visit (TyTy::InferType
&) override
{ rust_unreachable (); }
129 void visit (TyTy::TupleType
&) override
{ rust_unreachable (); }
130 void visit (TyTy::FnPtr
&) override
{ rust_unreachable (); }
131 void visit (TyTy::ArrayType
&) override
{ rust_unreachable (); }
132 void visit (TyTy::SliceType
&) override
{ rust_unreachable (); }
133 void visit (TyTy::BoolType
&) override
{ rust_unreachable (); }
134 void visit (TyTy::IntType
&) override
{ rust_unreachable (); }
135 void visit (TyTy::UintType
&) override
{ rust_unreachable (); }
136 void visit (TyTy::FloatType
&) override
{ rust_unreachable (); }
137 void visit (TyTy::USizeType
&) override
{ rust_unreachable (); }
138 void visit (TyTy::ISizeType
&) override
{ rust_unreachable (); }
139 void visit (TyTy::ErrorType
&) override
{ rust_unreachable (); }
140 void visit (TyTy::CharType
&) override
{ rust_unreachable (); }
141 void visit (TyTy::ReferenceType
&) override
{ rust_unreachable (); }
142 void visit (TyTy::PointerType
&) override
{ rust_unreachable (); }
143 void visit (TyTy::ParamType
&) override
{ rust_unreachable (); }
144 void visit (TyTy::StrType
&) override
{ rust_unreachable (); }
145 void visit (TyTy::NeverType
&) override
{ rust_unreachable (); }
146 void visit (TyTy::PlaceholderType
&) override
{ rust_unreachable (); }
147 void visit (TyTy::ProjectionType
&) override
{ rust_unreachable (); }
148 void visit (TyTy::DynamicObjectType
&) override
{ rust_unreachable (); }
151 SubstMapperFromExisting (TyTy::BaseType
*concrete
, TyTy::BaseType
*receiver
);
153 TyTy::BaseType
*concrete
;
154 TyTy::BaseType
*receiver
;
155 TyTy::BaseType
*resolved
;
158 class GetUsedSubstArgs
: public TyTy::TyConstVisitor
161 static TyTy::SubstitutionArgumentMappings
From (const TyTy::BaseType
*from
);
163 void visit (const TyTy::FnType
&type
) override
;
164 void visit (const TyTy::ADTType
&type
) override
;
165 void visit (const TyTy::ClosureType
&type
) override
;
167 void visit (const TyTy::InferType
&) override
{}
168 void visit (const TyTy::TupleType
&) override
{}
169 void visit (const TyTy::FnPtr
&) override
{}
170 void visit (const TyTy::ArrayType
&) override
{}
171 void visit (const TyTy::SliceType
&) override
{}
172 void visit (const TyTy::BoolType
&) override
{}
173 void visit (const TyTy::IntType
&) override
{}
174 void visit (const TyTy::UintType
&) override
{}
175 void visit (const TyTy::FloatType
&) override
{}
176 void visit (const TyTy::USizeType
&) override
{}
177 void visit (const TyTy::ISizeType
&) override
{}
178 void visit (const TyTy::ErrorType
&) override
{}
179 void visit (const TyTy::CharType
&) override
{}
180 void visit (const TyTy::ReferenceType
&) override
{}
181 void visit (const TyTy::PointerType
&) override
{}
182 void visit (const TyTy::ParamType
&) override
{}
183 void visit (const TyTy::StrType
&) override
{}
184 void visit (const TyTy::NeverType
&) override
{}
185 void visit (const TyTy::PlaceholderType
&) override
{}
186 void visit (const TyTy::ProjectionType
&) override
{}
187 void visit (const TyTy::DynamicObjectType
&) override
{}
192 TyTy::SubstitutionArgumentMappings args
;
195 } // namespace Resolver
198 #endif // RUST_SUBSTITUTION_MAPPER_H