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::StrType &) override { rust_unreachable (); }
65 : 0 : void visit (TyTy::NeverType &) override { rust_unreachable (); }
66 : 0 : void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }
67 : 0 : void visit (TyTy::ClosureType &) override { rust_unreachable (); }
68 : 0 : void visit (TyTy::OpaqueType &) override { rust_unreachable (); }
69 : :
70 : : private:
71 : : SubstMapper (HirId ref, HIR::GenericArgs *generics,
72 : : const std::vector<TyTy::Region> ®ions, location_t locus);
73 : :
74 : : TyTy::BaseType *resolved;
75 : : HIR::GenericArgs *generics;
76 : : const std::vector<TyTy::Region> ®ions;
77 : : location_t locus;
78 : : };
79 : :
80 : : class SubstMapperInternal : public TyTy::TyVisitor
81 : : {
82 : : public:
83 : : static TyTy::BaseType *Resolve (TyTy::BaseType *base,
84 : : TyTy::SubstitutionArgumentMappings &mappings);
85 : :
86 : : static bool mappings_are_bound (TyTy::BaseType *ty,
87 : : TyTy::SubstitutionArgumentMappings &mappings);
88 : :
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;
114 : :
115 : : private:
116 : : SubstMapperInternal (HirId ref, TyTy::SubstitutionArgumentMappings &mappings);
117 : :
118 : : TyTy::BaseType *resolved;
119 : : TyTy::SubstitutionArgumentMappings &mappings;
120 : : };
121 : :
122 : : class SubstMapperFromExisting : public TyTy::TyVisitor
123 : : {
124 : : public:
125 : : static TyTy::BaseType *Resolve (TyTy::BaseType *concrete,
126 : : TyTy::BaseType *receiver);
127 : :
128 : : void visit (TyTy::FnType &type) override;
129 : : void visit (TyTy::ADTType &type) override;
130 : : void visit (TyTy::ClosureType &type) override;
131 : :
132 : 0 : void visit (TyTy::InferType &) override { rust_unreachable (); }
133 : 0 : void visit (TyTy::TupleType &) override { rust_unreachable (); }
134 : 0 : void visit (TyTy::FnPtr &) override { rust_unreachable (); }
135 : 0 : void visit (TyTy::ArrayType &) override { rust_unreachable (); }
136 : 0 : void visit (TyTy::SliceType &) override { rust_unreachable (); }
137 : 0 : void visit (TyTy::BoolType &) override { rust_unreachable (); }
138 : 0 : void visit (TyTy::IntType &) override { rust_unreachable (); }
139 : 0 : void visit (TyTy::UintType &) override { rust_unreachable (); }
140 : 0 : void visit (TyTy::FloatType &) override { rust_unreachable (); }
141 : 0 : void visit (TyTy::USizeType &) override { rust_unreachable (); }
142 : 0 : void visit (TyTy::ISizeType &) override { rust_unreachable (); }
143 : 0 : void visit (TyTy::ErrorType &) override { rust_unreachable (); }
144 : 0 : void visit (TyTy::CharType &) override { rust_unreachable (); }
145 : 0 : void visit (TyTy::ReferenceType &) override { rust_unreachable (); }
146 : 0 : void visit (TyTy::PointerType &) override { rust_unreachable (); }
147 : 0 : void visit (TyTy::ParamType &) override { rust_unreachable (); }
148 : 0 : void visit (TyTy::StrType &) override { rust_unreachable (); }
149 : 0 : void visit (TyTy::NeverType &) override { rust_unreachable (); }
150 : 0 : void visit (TyTy::PlaceholderType &) override { rust_unreachable (); }
151 : 0 : void visit (TyTy::ProjectionType &) override { rust_unreachable (); }
152 : 0 : void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }
153 : 0 : void visit (TyTy::OpaqueType &type) override { rust_unreachable (); }
154 : :
155 : : private:
156 : : SubstMapperFromExisting (TyTy::BaseType *concrete, TyTy::BaseType *receiver);
157 : :
158 : : TyTy::BaseType *concrete;
159 : : TyTy::BaseType *receiver;
160 : : TyTy::BaseType *resolved;
161 : : };
162 : :
163 : 22132 : class GetUsedSubstArgs : public TyTy::TyConstVisitor
164 : : {
165 : : public:
166 : : static TyTy::SubstitutionArgumentMappings From (const TyTy::BaseType *from);
167 : :
168 : : void visit (const TyTy::FnType &type) override;
169 : : void visit (const TyTy::ADTType &type) override;
170 : : void visit (const TyTy::ClosureType &type) override;
171 : :
172 : 0 : void visit (const TyTy::InferType &) override {}
173 : 5 : void visit (const TyTy::TupleType &) override {}
174 : 0 : void visit (const TyTy::FnPtr &) override {}
175 : 0 : void visit (const TyTy::ArrayType &) override {}
176 : 475 : void visit (const TyTy::SliceType &) override {}
177 : 379 : void visit (const TyTy::BoolType &) override {}
178 : 2166 : void visit (const TyTy::IntType &) override {}
179 : 5429 : void visit (const TyTy::UintType &) override {}
180 : 2026 : void visit (const TyTy::FloatType &) override {}
181 : 1771 : void visit (const TyTy::USizeType &) override {}
182 : 389 : void visit (const TyTy::ISizeType &) override {}
183 : 0 : void visit (const TyTy::ErrorType &) override {}
184 : 343 : void visit (const TyTy::CharType &) override {}
185 : 1670 : void visit (const TyTy::ReferenceType &) override {}
186 : 1017 : void visit (const TyTy::PointerType &) override {}
187 : 368 : void visit (const TyTy::ParamType &) override {}
188 : 63 : void visit (const TyTy::StrType &) override {}
189 : 8 : void visit (const TyTy::NeverType &) override {}
190 : 0 : void visit (const TyTy::PlaceholderType &) override {}
191 : 0 : void visit (const TyTy::ProjectionType &) override {}
192 : 11 : void visit (const TyTy::DynamicObjectType &) override {}
193 : 0 : void visit (const TyTy::OpaqueType &type) override {}
194 : :
195 : : private:
196 : : GetUsedSubstArgs ();
197 : :
198 : : TyTy::SubstitutionArgumentMappings args;
199 : : };
200 : :
201 : : } // namespace Resolver
202 : : } // namespace Rust
203 : :
204 : : #endif // RUST_SUBSTITUTION_MAPPER_H
|