Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #ifndef RUST_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_t locus,
32 : HIR::GenericArgs *generics = nullptr,
33 : const std::vector<TyTy::Region> ®ions
34 : = {});
35 :
36 : static TyTy::BaseType *InferSubst (TyTy::BaseType *base, location_t locus);
37 :
38 : bool have_generic_args () const;
39 :
40 : static bool valid_type (TyTy::BaseType *base);
41 :
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;
46 :
47 : // nothing to do for these
48 0 : void visit (TyTy::InferType &) override { rust_unreachable (); }
49 0 : void visit (TyTy::TupleType &) override { rust_unreachable (); }
50 0 : void visit (TyTy::FnPtr &) override { rust_unreachable (); }
51 0 : void visit (TyTy::ArrayType &) override { rust_unreachable (); }
52 0 : void visit (TyTy::SliceType &) override { rust_unreachable (); }
53 0 : void visit (TyTy::BoolType &) override { rust_unreachable (); }
54 0 : void visit (TyTy::IntType &) override { rust_unreachable (); }
55 0 : void visit (TyTy::UintType &) override { rust_unreachable (); }
56 0 : void visit (TyTy::FloatType &) override { rust_unreachable (); }
57 0 : void visit (TyTy::USizeType &) override { rust_unreachable (); }
58 0 : void visit (TyTy::ISizeType &) override { rust_unreachable (); }
59 0 : void visit (TyTy::ErrorType &) override { rust_unreachable (); }
60 0 : void visit (TyTy::CharType &) override { rust_unreachable (); }
61 0 : void visit (TyTy::ReferenceType &) override { rust_unreachable (); }
62 0 : void visit (TyTy::PointerType &) override { rust_unreachable (); }
63 0 : void visit (TyTy::ParamType &) override { rust_unreachable (); }
64 0 : void visit (TyTy::ConstParamType &) override { rust_unreachable (); }
65 0 : void visit (TyTy::ConstValueType &) override { rust_unreachable (); }
66 0 : void visit (TyTy::ConstInferType &) override { rust_unreachable (); }
67 0 : void visit (TyTy::ConstErrorType &) override { rust_unreachable (); }
68 0 : void visit (TyTy::StrType &) override { rust_unreachable (); }
69 0 : void visit (TyTy::NeverType &) override { rust_unreachable (); }
70 0 : void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }
71 0 : void visit (TyTy::ClosureType &) override { rust_unreachable (); }
72 0 : void visit (TyTy::OpaqueType &) override { rust_unreachable (); }
73 :
74 : private:
75 : SubstMapper (HirId ref, HIR::GenericArgs *generics,
76 : const std::vector<TyTy::Region> ®ions, location_t locus);
77 :
78 : TyTy::BaseType *resolved;
79 : HIR::GenericArgs *generics;
80 : const std::vector<TyTy::Region> ®ions;
81 : location_t locus;
82 : };
83 :
84 : class SubstMapperInternal : public TyTy::TyVisitor
85 : {
86 : public:
87 : static TyTy::BaseType *Resolve (TyTy::BaseType *base,
88 : TyTy::SubstitutionArgumentMappings &mappings);
89 :
90 : static bool mappings_are_bound (TyTy::BaseType *ty,
91 : TyTy::SubstitutionArgumentMappings &mappings);
92 :
93 : void visit (TyTy::FnType &type) override;
94 : void visit (TyTy::ADTType &type) override;
95 : void visit (TyTy::TupleType &type) override;
96 : void visit (TyTy::ReferenceType &type) override;
97 : void visit (TyTy::PointerType &type) override;
98 : void visit (TyTy::ParamType &type) override;
99 : void visit (TyTy::ConstParamType &type) override;
100 : void visit (TyTy::ConstValueType &type) override;
101 : void visit (TyTy::ConstInferType &type) override;
102 : void visit (TyTy::ConstErrorType &type) override;
103 : void visit (TyTy::PlaceholderType &type) override;
104 : void visit (TyTy::ProjectionType &type) override;
105 : void visit (TyTy::ClosureType &type) override;
106 : void visit (TyTy::ArrayType &type) override;
107 : void visit (TyTy::SliceType &type) override;
108 : void visit (TyTy::InferType &type) override;
109 : void visit (TyTy::FnPtr &type) override;
110 : void visit (TyTy::BoolType &type) override;
111 : void visit (TyTy::IntType &type) override;
112 : void visit (TyTy::UintType &type) override;
113 : void visit (TyTy::FloatType &type) override;
114 : void visit (TyTy::USizeType &type) override;
115 : void visit (TyTy::ISizeType &type) override;
116 : void visit (TyTy::ErrorType &type) override;
117 : void visit (TyTy::CharType &type) override;
118 : void visit (TyTy::StrType &type) override;
119 : void visit (TyTy::NeverType &type) override;
120 : void visit (TyTy::DynamicObjectType &type) override;
121 : void visit (TyTy::OpaqueType &type) override;
122 :
123 : private:
124 : SubstMapperInternal (HirId ref, TyTy::SubstitutionArgumentMappings &mappings);
125 :
126 : TyTy::BaseType *resolved;
127 : TyTy::SubstitutionArgumentMappings &mappings;
128 : };
129 :
130 : class SubstMapperFromExisting : public TyTy::TyVisitor
131 : {
132 : public:
133 : static TyTy::BaseType *Resolve (TyTy::BaseType *concrete,
134 : TyTy::BaseType *receiver);
135 :
136 : void visit (TyTy::FnType &type) override;
137 : void visit (TyTy::ADTType &type) override;
138 : void visit (TyTy::ClosureType &type) override;
139 :
140 0 : void visit (TyTy::InferType &) override { rust_unreachable (); }
141 0 : void visit (TyTy::TupleType &) override { rust_unreachable (); }
142 0 : void visit (TyTy::FnPtr &) override { rust_unreachable (); }
143 0 : void visit (TyTy::ArrayType &) override { rust_unreachable (); }
144 0 : void visit (TyTy::SliceType &) override { rust_unreachable (); }
145 0 : void visit (TyTy::BoolType &) override { rust_unreachable (); }
146 0 : void visit (TyTy::IntType &) override { rust_unreachable (); }
147 0 : void visit (TyTy::UintType &) override { rust_unreachable (); }
148 0 : void visit (TyTy::FloatType &) override { rust_unreachable (); }
149 0 : void visit (TyTy::USizeType &) override { rust_unreachable (); }
150 0 : void visit (TyTy::ISizeType &) override { rust_unreachable (); }
151 0 : void visit (TyTy::ErrorType &) override { rust_unreachable (); }
152 0 : void visit (TyTy::CharType &) override { rust_unreachable (); }
153 0 : void visit (TyTy::ReferenceType &) override { rust_unreachable (); }
154 0 : void visit (TyTy::PointerType &) override { rust_unreachable (); }
155 0 : void visit (TyTy::ParamType &) override { rust_unreachable (); }
156 0 : void visit (TyTy::ConstParamType &) override { rust_unreachable (); }
157 0 : void visit (TyTy::ConstValueType &) override { rust_unreachable (); }
158 0 : void visit (TyTy::ConstInferType &) override { rust_unreachable (); }
159 0 : void visit (TyTy::ConstErrorType &) override { rust_unreachable (); }
160 0 : void visit (TyTy::StrType &) override { rust_unreachable (); }
161 0 : void visit (TyTy::NeverType &) override { rust_unreachable (); }
162 0 : void visit (TyTy::PlaceholderType &) override { rust_unreachable (); }
163 0 : void visit (TyTy::ProjectionType &) override { rust_unreachable (); }
164 0 : void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }
165 0 : void visit (TyTy::OpaqueType &) override { rust_unreachable (); }
166 :
167 : private:
168 : SubstMapperFromExisting (TyTy::BaseType *concrete, TyTy::BaseType *receiver);
169 :
170 : TyTy::BaseType *concrete;
171 : TyTy::BaseType *receiver;
172 : TyTy::BaseType *resolved;
173 : };
174 :
175 42965 : class GetUsedSubstArgs : public TyTy::TyConstVisitor
176 : {
177 : public:
178 : static TyTy::SubstitutionArgumentMappings From (const TyTy::BaseType *from);
179 :
180 : void visit (const TyTy::FnType &type) override;
181 : void visit (const TyTy::ADTType &type) override;
182 : void visit (const TyTy::ClosureType &type) override;
183 :
184 0 : void visit (const TyTy::InferType &) override {}
185 2 : void visit (const TyTy::TupleType &) override {}
186 92 : void visit (const TyTy::FnPtr &) override {}
187 0 : void visit (const TyTy::ArrayType &) override {}
188 500 : void visit (const TyTy::SliceType &) override {}
189 343 : void visit (const TyTy::BoolType &) override {}
190 7897 : void visit (const TyTy::IntType &) override {}
191 11050 : void visit (const TyTy::UintType &) override {}
192 2192 : void visit (const TyTy::FloatType &) override {}
193 2742 : void visit (const TyTy::USizeType &) override {}
194 3980 : void visit (const TyTy::ISizeType &) override {}
195 0 : void visit (const TyTy::ErrorType &) override {}
196 304 : void visit (const TyTy::CharType &) override {}
197 1283 : void visit (const TyTy::ReferenceType &) override {}
198 1147 : void visit (const TyTy::PointerType &) override {}
199 330 : void visit (const TyTy::ParamType &) override {}
200 0 : void visit (const TyTy::ConstParamType &) override {}
201 0 : void visit (const TyTy::ConstValueType &) override {}
202 0 : void visit (const TyTy::ConstInferType &) override {}
203 0 : void visit (const TyTy::ConstErrorType &) override {}
204 63 : void visit (const TyTy::StrType &) override {}
205 4 : void visit (const TyTy::NeverType &) override {}
206 0 : void visit (const TyTy::PlaceholderType &) override {}
207 0 : void visit (const TyTy::ProjectionType &) override {}
208 10 : void visit (const TyTy::DynamicObjectType &) override {}
209 0 : void visit (const TyTy::OpaqueType &) override {}
210 :
211 : private:
212 : GetUsedSubstArgs ();
213 :
214 : TyTy::SubstitutionArgumentMappings args;
215 : };
216 :
217 : } // namespace Resolver
218 : } // namespace Rust
219 :
220 : #endif // RUST_SUBSTITUTION_MAPPER_H
|