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 : : 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;
44 : :
45 : : // nothing to do for these
46 : 0 : void visit (TyTy::InferType &) override { rust_unreachable (); }
47 : 0 : void visit (TyTy::TupleType &) override { rust_unreachable (); }
48 : 0 : void visit (TyTy::FnPtr &) override { rust_unreachable (); }
49 : 0 : void visit (TyTy::ArrayType &) override { rust_unreachable (); }
50 : 0 : void visit (TyTy::SliceType &) override { rust_unreachable (); }
51 : 0 : void visit (TyTy::BoolType &) override { rust_unreachable (); }
52 : 0 : void visit (TyTy::IntType &) override { rust_unreachable (); }
53 : 0 : void visit (TyTy::UintType &) override { rust_unreachable (); }
54 : 0 : void visit (TyTy::FloatType &) override { rust_unreachable (); }
55 : 0 : void visit (TyTy::USizeType &) override { rust_unreachable (); }
56 : 0 : void visit (TyTy::ISizeType &) override { rust_unreachable (); }
57 : 0 : void visit (TyTy::ErrorType &) override { rust_unreachable (); }
58 : 0 : void visit (TyTy::CharType &) override { rust_unreachable (); }
59 : 0 : void visit (TyTy::ReferenceType &) override { rust_unreachable (); }
60 : 0 : void visit (TyTy::PointerType &) override { rust_unreachable (); }
61 : 0 : void visit (TyTy::ParamType &) override { rust_unreachable (); }
62 : 0 : void visit (TyTy::StrType &) override { rust_unreachable (); }
63 : 0 : void visit (TyTy::NeverType &) override { rust_unreachable (); }
64 : 0 : void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }
65 : 0 : void visit (TyTy::ClosureType &) override { rust_unreachable (); }
66 : :
67 : : private:
68 : : SubstMapper (HirId ref, HIR::GenericArgs *generics,
69 : : const std::vector<TyTy::Region> ®ions, location_t locus);
70 : :
71 : : TyTy::BaseType *resolved;
72 : : HIR::GenericArgs *generics;
73 : : const std::vector<TyTy::Region> ®ions;
74 : : location_t locus;
75 : : };
76 : :
77 : : class SubstMapperInternal : public TyTy::TyVisitor
78 : : {
79 : : public:
80 : : static TyTy::BaseType *Resolve (TyTy::BaseType *base,
81 : : TyTy::SubstitutionArgumentMappings &mappings);
82 : :
83 : : static bool mappings_are_bound (TyTy::BaseType *ty,
84 : : TyTy::SubstitutionArgumentMappings &mappings);
85 : :
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;
110 : :
111 : : private:
112 : : SubstMapperInternal (HirId ref, TyTy::SubstitutionArgumentMappings &mappings);
113 : :
114 : : TyTy::BaseType *resolved;
115 : : TyTy::SubstitutionArgumentMappings &mappings;
116 : : };
117 : :
118 : : class SubstMapperFromExisting : public TyTy::TyVisitor
119 : : {
120 : : public:
121 : : static TyTy::BaseType *Resolve (TyTy::BaseType *concrete,
122 : : TyTy::BaseType *receiver);
123 : :
124 : : void visit (TyTy::FnType &type) override;
125 : : void visit (TyTy::ADTType &type) override;
126 : : void visit (TyTy::ClosureType &type) override;
127 : :
128 : 0 : void visit (TyTy::InferType &) override { rust_unreachable (); }
129 : 0 : void visit (TyTy::TupleType &) override { rust_unreachable (); }
130 : 0 : void visit (TyTy::FnPtr &) override { rust_unreachable (); }
131 : 0 : void visit (TyTy::ArrayType &) override { rust_unreachable (); }
132 : 0 : void visit (TyTy::SliceType &) override { rust_unreachable (); }
133 : 0 : void visit (TyTy::BoolType &) override { rust_unreachable (); }
134 : 0 : void visit (TyTy::IntType &) override { rust_unreachable (); }
135 : 0 : void visit (TyTy::UintType &) override { rust_unreachable (); }
136 : 0 : void visit (TyTy::FloatType &) override { rust_unreachable (); }
137 : 0 : void visit (TyTy::USizeType &) override { rust_unreachable (); }
138 : 0 : void visit (TyTy::ISizeType &) override { rust_unreachable (); }
139 : 0 : void visit (TyTy::ErrorType &) override { rust_unreachable (); }
140 : 0 : void visit (TyTy::CharType &) override { rust_unreachable (); }
141 : 0 : void visit (TyTy::ReferenceType &) override { rust_unreachable (); }
142 : 0 : void visit (TyTy::PointerType &) override { rust_unreachable (); }
143 : 0 : void visit (TyTy::ParamType &) override { rust_unreachable (); }
144 : 0 : void visit (TyTy::StrType &) override { rust_unreachable (); }
145 : 0 : void visit (TyTy::NeverType &) override { rust_unreachable (); }
146 : 0 : void visit (TyTy::PlaceholderType &) override { rust_unreachable (); }
147 : 0 : void visit (TyTy::ProjectionType &) override { rust_unreachable (); }
148 : 0 : void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }
149 : :
150 : : private:
151 : : SubstMapperFromExisting (TyTy::BaseType *concrete, TyTy::BaseType *receiver);
152 : :
153 : : TyTy::BaseType *concrete;
154 : : TyTy::BaseType *receiver;
155 : : TyTy::BaseType *resolved;
156 : : };
157 : :
158 : 12368 : class GetUsedSubstArgs : public TyTy::TyConstVisitor
159 : : {
160 : : public:
161 : : static TyTy::SubstitutionArgumentMappings From (const TyTy::BaseType *from);
162 : :
163 : : void visit (const TyTy::FnType &type) override;
164 : : void visit (const TyTy::ADTType &type) override;
165 : : void visit (const TyTy::ClosureType &type) override;
166 : :
167 : 0 : void visit (const TyTy::InferType &) override {}
168 : 0 : void visit (const TyTy::TupleType &) override {}
169 : 0 : void visit (const TyTy::FnPtr &) override {}
170 : 0 : void visit (const TyTy::ArrayType &) override {}
171 : 448 : void visit (const TyTy::SliceType &) override {}
172 : 184 : void visit (const TyTy::BoolType &) override {}
173 : 1333 : void visit (const TyTy::IntType &) override {}
174 : 2119 : void visit (const TyTy::UintType &) override {}
175 : 802 : void visit (const TyTy::FloatType &) override {}
176 : 852 : void visit (const TyTy::USizeType &) override {}
177 : 208 : void visit (const TyTy::ISizeType &) override {}
178 : 1 : void visit (const TyTy::ErrorType &) override {}
179 : 187 : void visit (const TyTy::CharType &) override {}
180 : 1213 : void visit (const TyTy::ReferenceType &) override {}
181 : 692 : void visit (const TyTy::PointerType &) override {}
182 : 137 : void visit (const TyTy::ParamType &) override {}
183 : 63 : void visit (const TyTy::StrType &) override {}
184 : 0 : void visit (const TyTy::NeverType &) override {}
185 : 0 : void visit (const TyTy::PlaceholderType &) override {}
186 : 0 : void visit (const TyTy::ProjectionType &) override {}
187 : 14 : void visit (const TyTy::DynamicObjectType &) override {}
188 : :
189 : : private:
190 : : GetUsedSubstArgs ();
191 : :
192 : : TyTy::SubstitutionArgumentMappings args;
193 : : };
194 : :
195 : : } // namespace Resolver
196 : : } // namespace Rust
197 : :
198 : : #endif // RUST_SUBSTITUTION_MAPPER_H
|