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 static bool valid_type (TyTy::BaseType
*base
);
42 void visit (TyTy::FnType
&type
) override
;
43 void visit (TyTy::ADTType
&type
) override
;
44 void visit (TyTy::PlaceholderType
&type
) override
;
45 void visit (TyTy::ProjectionType
&type
) override
;
47 // nothing to do for these
48 void visit (TyTy::InferType
&) override
{ rust_unreachable (); }
49 void visit (TyTy::TupleType
&) override
{ rust_unreachable (); }
50 void visit (TyTy::FnPtr
&) override
{ rust_unreachable (); }
51 void visit (TyTy::ArrayType
&) override
{ rust_unreachable (); }
52 void visit (TyTy::SliceType
&) override
{ rust_unreachable (); }
53 void visit (TyTy::BoolType
&) override
{ rust_unreachable (); }
54 void visit (TyTy::IntType
&) override
{ rust_unreachable (); }
55 void visit (TyTy::UintType
&) override
{ rust_unreachable (); }
56 void visit (TyTy::FloatType
&) override
{ rust_unreachable (); }
57 void visit (TyTy::USizeType
&) override
{ rust_unreachable (); }
58 void visit (TyTy::ISizeType
&) override
{ rust_unreachable (); }
59 void visit (TyTy::ErrorType
&) override
{ rust_unreachable (); }
60 void visit (TyTy::CharType
&) override
{ rust_unreachable (); }
61 void visit (TyTy::ReferenceType
&) override
{ rust_unreachable (); }
62 void visit (TyTy::PointerType
&) override
{ rust_unreachable (); }
63 void visit (TyTy::ParamType
&) override
{ rust_unreachable (); }
64 void visit (TyTy::StrType
&) override
{ rust_unreachable (); }
65 void visit (TyTy::NeverType
&) override
{ rust_unreachable (); }
66 void visit (TyTy::DynamicObjectType
&) override
{ rust_unreachable (); }
67 void visit (TyTy::ClosureType
&) override
{ rust_unreachable (); }
68 void visit (TyTy::OpaqueType
&) override
{ rust_unreachable (); }
71 SubstMapper (HirId ref
, HIR::GenericArgs
*generics
,
72 const std::vector
<TyTy::Region
> ®ions
, location_t locus
);
74 TyTy::BaseType
*resolved
;
75 HIR::GenericArgs
*generics
;
76 const std::vector
<TyTy::Region
> ®ions
;
80 class SubstMapperInternal
: public TyTy::TyVisitor
83 static TyTy::BaseType
*Resolve (TyTy::BaseType
*base
,
84 TyTy::SubstitutionArgumentMappings
&mappings
);
86 static bool mappings_are_bound (TyTy::BaseType
*ty
,
87 TyTy::SubstitutionArgumentMappings
&mappings
);
89 void visit (TyTy::FnType
&type
) override
;
90 void visit (TyTy::ADTType
&type
) override
;
91 void visit (TyTy::TupleType
&type
) override
;
92 void visit (TyTy::ReferenceType
&type
) override
;
93 void visit (TyTy::PointerType
&type
) override
;
94 void visit (TyTy::ParamType
&type
) override
;
95 void visit (TyTy::PlaceholderType
&type
) override
;
96 void visit (TyTy::ProjectionType
&type
) override
;
97 void visit (TyTy::ClosureType
&type
) override
;
98 void visit (TyTy::ArrayType
&type
) override
;
99 void visit (TyTy::SliceType
&type
) override
;
100 void visit (TyTy::InferType
&type
) override
;
101 void visit (TyTy::FnPtr
&type
) override
;
102 void visit (TyTy::BoolType
&type
) override
;
103 void visit (TyTy::IntType
&type
) override
;
104 void visit (TyTy::UintType
&type
) override
;
105 void visit (TyTy::FloatType
&type
) override
;
106 void visit (TyTy::USizeType
&type
) override
;
107 void visit (TyTy::ISizeType
&type
) override
;
108 void visit (TyTy::ErrorType
&type
) override
;
109 void visit (TyTy::CharType
&type
) override
;
110 void visit (TyTy::StrType
&type
) override
;
111 void visit (TyTy::NeverType
&type
) override
;
112 void visit (TyTy::DynamicObjectType
&type
) override
;
113 void visit (TyTy::OpaqueType
&type
) override
;
116 SubstMapperInternal (HirId ref
, TyTy::SubstitutionArgumentMappings
&mappings
);
118 TyTy::BaseType
*resolved
;
119 TyTy::SubstitutionArgumentMappings
&mappings
;
122 class SubstMapperFromExisting
: public TyTy::TyVisitor
125 static TyTy::BaseType
*Resolve (TyTy::BaseType
*concrete
,
126 TyTy::BaseType
*receiver
);
128 void visit (TyTy::FnType
&type
) override
;
129 void visit (TyTy::ADTType
&type
) override
;
130 void visit (TyTy::ClosureType
&type
) override
;
132 void visit (TyTy::InferType
&) override
{ rust_unreachable (); }
133 void visit (TyTy::TupleType
&) override
{ rust_unreachable (); }
134 void visit (TyTy::FnPtr
&) override
{ rust_unreachable (); }
135 void visit (TyTy::ArrayType
&) override
{ rust_unreachable (); }
136 void visit (TyTy::SliceType
&) override
{ rust_unreachable (); }
137 void visit (TyTy::BoolType
&) override
{ rust_unreachable (); }
138 void visit (TyTy::IntType
&) override
{ rust_unreachable (); }
139 void visit (TyTy::UintType
&) override
{ rust_unreachable (); }
140 void visit (TyTy::FloatType
&) override
{ rust_unreachable (); }
141 void visit (TyTy::USizeType
&) override
{ rust_unreachable (); }
142 void visit (TyTy::ISizeType
&) override
{ rust_unreachable (); }
143 void visit (TyTy::ErrorType
&) override
{ rust_unreachable (); }
144 void visit (TyTy::CharType
&) override
{ rust_unreachable (); }
145 void visit (TyTy::ReferenceType
&) override
{ rust_unreachable (); }
146 void visit (TyTy::PointerType
&) override
{ rust_unreachable (); }
147 void visit (TyTy::ParamType
&) override
{ rust_unreachable (); }
148 void visit (TyTy::StrType
&) override
{ rust_unreachable (); }
149 void visit (TyTy::NeverType
&) override
{ rust_unreachable (); }
150 void visit (TyTy::PlaceholderType
&) override
{ rust_unreachable (); }
151 void visit (TyTy::ProjectionType
&) override
{ rust_unreachable (); }
152 void visit (TyTy::DynamicObjectType
&) override
{ rust_unreachable (); }
153 void visit (TyTy::OpaqueType
&type
) override
{ rust_unreachable (); }
156 SubstMapperFromExisting (TyTy::BaseType
*concrete
, TyTy::BaseType
*receiver
);
158 TyTy::BaseType
*concrete
;
159 TyTy::BaseType
*receiver
;
160 TyTy::BaseType
*resolved
;
163 class GetUsedSubstArgs
: public TyTy::TyConstVisitor
166 static TyTy::SubstitutionArgumentMappings
From (const TyTy::BaseType
*from
);
168 void visit (const TyTy::FnType
&type
) override
;
169 void visit (const TyTy::ADTType
&type
) override
;
170 void visit (const TyTy::ClosureType
&type
) override
;
172 void visit (const TyTy::InferType
&) override
{}
173 void visit (const TyTy::TupleType
&) override
{}
174 void visit (const TyTy::FnPtr
&) override
{}
175 void visit (const TyTy::ArrayType
&) override
{}
176 void visit (const TyTy::SliceType
&) override
{}
177 void visit (const TyTy::BoolType
&) override
{}
178 void visit (const TyTy::IntType
&) override
{}
179 void visit (const TyTy::UintType
&) override
{}
180 void visit (const TyTy::FloatType
&) override
{}
181 void visit (const TyTy::USizeType
&) override
{}
182 void visit (const TyTy::ISizeType
&) override
{}
183 void visit (const TyTy::ErrorType
&) override
{}
184 void visit (const TyTy::CharType
&) override
{}
185 void visit (const TyTy::ReferenceType
&) override
{}
186 void visit (const TyTy::PointerType
&) override
{}
187 void visit (const TyTy::ParamType
&) override
{}
188 void visit (const TyTy::StrType
&) override
{}
189 void visit (const TyTy::NeverType
&) override
{}
190 void visit (const TyTy::PlaceholderType
&) override
{}
191 void visit (const TyTy::ProjectionType
&) override
{}
192 void visit (const TyTy::DynamicObjectType
&) override
{}
193 void visit (const TyTy::OpaqueType
&type
) override
{}
198 TyTy::SubstitutionArgumentMappings args
;
201 } // namespace Resolver
204 #endif // RUST_SUBSTITUTION_MAPPER_H