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_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::ConstType &) override { rust_unreachable (); }
65 : 0 : void visit (TyTy::StrType &) override { rust_unreachable (); }
66 : 0 : void visit (TyTy::NeverType &) override { rust_unreachable (); }
67 : 0 : void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }
68 : 0 : void visit (TyTy::ClosureType &) override { rust_unreachable (); }
69 : 0 : void visit (TyTy::OpaqueType &) override { rust_unreachable (); }
70 : :
71 : : private:
72 : : SubstMapper (HirId ref, HIR::GenericArgs *generics,
73 : : const std::vector<TyTy::Region> ®ions, location_t locus);
74 : :
75 : : TyTy::BaseType *resolved;
76 : : HIR::GenericArgs *generics;
77 : : const std::vector<TyTy::Region> ®ions;
78 : : location_t locus;
79 : : };
80 : :
81 : : class SubstMapperInternal : public TyTy::TyVisitor
82 : : {
83 : : public:
84 : : static TyTy::BaseType *Resolve (TyTy::BaseType *base,
85 : : TyTy::SubstitutionArgumentMappings &mappings);
86 : :
87 : : static bool mappings_are_bound (TyTy::BaseType *ty,
88 : : TyTy::SubstitutionArgumentMappings &mappings);
89 : :
90 : : void visit (TyTy::FnType &type) override;
91 : : void visit (TyTy::ADTType &type) override;
92 : : void visit (TyTy::TupleType &type) override;
93 : : void visit (TyTy::ReferenceType &type) override;
94 : : void visit (TyTy::PointerType &type) override;
95 : : void visit (TyTy::ParamType &type) override;
96 : : void visit (TyTy::ConstType &type) override;
97 : : void visit (TyTy::PlaceholderType &type) override;
98 : : void visit (TyTy::ProjectionType &type) override;
99 : : void visit (TyTy::ClosureType &type) override;
100 : : void visit (TyTy::ArrayType &type) override;
101 : : void visit (TyTy::SliceType &type) override;
102 : : void visit (TyTy::InferType &type) override;
103 : : void visit (TyTy::FnPtr &type) override;
104 : : void visit (TyTy::BoolType &type) override;
105 : : void visit (TyTy::IntType &type) override;
106 : : void visit (TyTy::UintType &type) override;
107 : : void visit (TyTy::FloatType &type) override;
108 : : void visit (TyTy::USizeType &type) override;
109 : : void visit (TyTy::ISizeType &type) override;
110 : : void visit (TyTy::ErrorType &type) override;
111 : : void visit (TyTy::CharType &type) override;
112 : : void visit (TyTy::StrType &type) override;
113 : : void visit (TyTy::NeverType &type) override;
114 : : void visit (TyTy::DynamicObjectType &type) override;
115 : : void visit (TyTy::OpaqueType &type) override;
116 : :
117 : : private:
118 : : SubstMapperInternal (HirId ref, TyTy::SubstitutionArgumentMappings &mappings);
119 : :
120 : : TyTy::BaseType *resolved;
121 : : TyTy::SubstitutionArgumentMappings &mappings;
122 : : };
123 : :
124 : : class SubstMapperFromExisting : public TyTy::TyVisitor
125 : : {
126 : : public:
127 : : static TyTy::BaseType *Resolve (TyTy::BaseType *concrete,
128 : : TyTy::BaseType *receiver);
129 : :
130 : : void visit (TyTy::FnType &type) override;
131 : : void visit (TyTy::ADTType &type) override;
132 : : void visit (TyTy::ClosureType &type) override;
133 : :
134 : 0 : void visit (TyTy::InferType &) override { rust_unreachable (); }
135 : 0 : void visit (TyTy::TupleType &) override { rust_unreachable (); }
136 : 0 : void visit (TyTy::FnPtr &) override { rust_unreachable (); }
137 : 0 : void visit (TyTy::ArrayType &) override { rust_unreachable (); }
138 : 0 : void visit (TyTy::SliceType &) override { rust_unreachable (); }
139 : 0 : void visit (TyTy::BoolType &) override { rust_unreachable (); }
140 : 0 : void visit (TyTy::IntType &) override { rust_unreachable (); }
141 : 0 : void visit (TyTy::UintType &) override { rust_unreachable (); }
142 : 0 : void visit (TyTy::FloatType &) override { rust_unreachable (); }
143 : 0 : void visit (TyTy::USizeType &) override { rust_unreachable (); }
144 : 0 : void visit (TyTy::ISizeType &) override { rust_unreachable (); }
145 : 0 : void visit (TyTy::ErrorType &) override { rust_unreachable (); }
146 : 0 : void visit (TyTy::CharType &) override { rust_unreachable (); }
147 : 0 : void visit (TyTy::ReferenceType &) override { rust_unreachable (); }
148 : 0 : void visit (TyTy::PointerType &) override { rust_unreachable (); }
149 : 0 : void visit (TyTy::ParamType &) override { rust_unreachable (); }
150 : 0 : void visit (TyTy::ConstType &) override { rust_unreachable (); }
151 : 0 : void visit (TyTy::StrType &) override { rust_unreachable (); }
152 : 0 : void visit (TyTy::NeverType &) override { rust_unreachable (); }
153 : 0 : void visit (TyTy::PlaceholderType &) override { rust_unreachable (); }
154 : 0 : void visit (TyTy::ProjectionType &) override { rust_unreachable (); }
155 : 0 : void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }
156 : 0 : void visit (TyTy::OpaqueType &type) override { rust_unreachable (); }
157 : :
158 : : private:
159 : : SubstMapperFromExisting (TyTy::BaseType *concrete, TyTy::BaseType *receiver);
160 : :
161 : : TyTy::BaseType *concrete;
162 : : TyTy::BaseType *receiver;
163 : : TyTy::BaseType *resolved;
164 : : };
165 : :
166 : 42097 : class GetUsedSubstArgs : public TyTy::TyConstVisitor
167 : : {
168 : : public:
169 : : static TyTy::SubstitutionArgumentMappings From (const TyTy::BaseType *from);
170 : :
171 : : void visit (const TyTy::FnType &type) override;
172 : : void visit (const TyTy::ADTType &type) override;
173 : : void visit (const TyTy::ClosureType &type) override;
174 : :
175 : 0 : void visit (const TyTy::InferType &) override {}
176 : 2 : void visit (const TyTy::TupleType &) override {}
177 : 0 : void visit (const TyTy::FnPtr &) override {}
178 : 0 : void visit (const TyTy::ArrayType &) override {}
179 : 549 : void visit (const TyTy::SliceType &) override {}
180 : 343 : void visit (const TyTy::BoolType &) override {}
181 : 8372 : void visit (const TyTy::IntType &) override {}
182 : 10866 : void visit (const TyTy::UintType &) override {}
183 : 2192 : void visit (const TyTy::FloatType &) override {}
184 : 2805 : void visit (const TyTy::USizeType &) override {}
185 : 4123 : void visit (const TyTy::ISizeType &) override {}
186 : 0 : void visit (const TyTy::ErrorType &) override {}
187 : 304 : void visit (const TyTy::CharType &) override {}
188 : 1318 : void visit (const TyTy::ReferenceType &) override {}
189 : 1147 : void visit (const TyTy::PointerType &) override {}
190 : 330 : void visit (const TyTy::ParamType &) override {}
191 : 0 : void visit (const TyTy::ConstType &) override {}
192 : 63 : void visit (const TyTy::StrType &) override {}
193 : 4 : void visit (const TyTy::NeverType &) override {}
194 : 0 : void visit (const TyTy::PlaceholderType &) override {}
195 : 0 : void visit (const TyTy::ProjectionType &) override {}
196 : 9 : void visit (const TyTy::DynamicObjectType &) override {}
197 : 0 : void visit (const TyTy::OpaqueType &) override {}
198 : :
199 : : private:
200 : : GetUsedSubstArgs ();
201 : :
202 : : TyTy::SubstitutionArgumentMappings args;
203 : : };
204 : :
205 : : } // namespace Resolver
206 : : } // namespace Rust
207 : :
208 : : #endif // RUST_SUBSTITUTION_MAPPER_H
|