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 : #include "rust-hir-trait-reference.h"
20 : #include "rust-hir-type-check.h"
21 :
22 : namespace Rust {
23 : namespace Resolver {
24 :
25 : std::string
26 291 : TraitItemReference::as_string () const
27 : {
28 1164 : return "(" + trait_item_type_as_string (type) + " " + identifier + " " + ")";
29 : }
30 :
31 : bool
32 41731 : TraitItemReference::is_error () const
33 : {
34 41731 : return type == ERROR;
35 : }
36 :
37 : bool
38 14165 : TraitItemReference::is_optional () const
39 : {
40 14165 : return optional_flag;
41 : }
42 :
43 : std::string
44 152605 : TraitItemReference::get_identifier () const
45 : {
46 152605 : return identifier;
47 : }
48 :
49 : TraitItemReference::TraitItemType
50 154134 : TraitItemReference::get_trait_item_type () const
51 : {
52 154134 : return type;
53 : }
54 :
55 : HIR::TraitItem *
56 45267 : TraitItemReference::get_hir_trait_item () const
57 : {
58 45267 : return hir_trait_item;
59 : }
60 :
61 : location_t
62 2835 : TraitItemReference::get_locus () const
63 : {
64 2835 : return locus;
65 : }
66 :
67 : const Analysis::NodeMapping
68 122178 : TraitItemReference::get_mappings () const
69 : {
70 122178 : return hir_trait_item->get_mappings ();
71 : }
72 :
73 : TyTy::BaseType *
74 74680 : TraitItemReference::get_tyty () const
75 : {
76 74680 : rust_assert (hir_trait_item != nullptr);
77 :
78 74680 : TyTy::BaseType *resolved = nullptr;
79 74680 : if (type == FN
80 74680 : && context->lookup_type (get_mappings ().get_hirid (), &resolved))
81 30787 : return resolved;
82 :
83 43893 : switch (type)
84 : {
85 32 : case CONST:
86 32 : return get_type_from_constant (
87 32 : static_cast</*const*/ HIR::TraitItemConst &> (*hir_trait_item));
88 39090 : break;
89 :
90 39090 : case TYPE:
91 39090 : return get_type_from_typealias (
92 39090 : static_cast</*const*/ HIR::TraitItemType &> (*hir_trait_item));
93 :
94 4771 : case FN:
95 4771 : return get_type_from_fn (
96 4771 : static_cast</*const*/ HIR::TraitItemFunc &> (*hir_trait_item));
97 0 : break;
98 :
99 0 : default:
100 0 : return get_error ();
101 : }
102 :
103 : rust_unreachable ();
104 : return get_error ();
105 : }
106 :
107 : TyTy::ErrorType *
108 2 : TraitItemReference::get_error () const
109 : {
110 2 : return new TyTy::ErrorType (get_mappings ().get_hirid ());
111 : }
112 :
113 7627 : TraitReference::TraitReference (
114 : const HIR::Trait *hir_trait_ref, std::vector<TraitItemReference> item_refs,
115 : std::vector<TyTy::TypeBoundPredicate> super_traits,
116 : std::vector<TyTy::SubstitutionParamMapping> substs)
117 7627 : : hir_trait_ref (hir_trait_ref), item_refs (item_refs),
118 7627 : super_traits (super_traits)
119 : {
120 7627 : trait_substs.clear ();
121 7627 : trait_substs.reserve (substs.size ());
122 14101 : for (const auto &p : substs)
123 6474 : trait_substs.push_back (p.clone ());
124 7627 : }
125 :
126 0 : TraitReference::TraitReference (TraitReference const &other)
127 0 : : hir_trait_ref (other.hir_trait_ref), item_refs (other.item_refs),
128 0 : super_traits (other.super_traits)
129 : {
130 0 : trait_substs.clear ();
131 0 : trait_substs.reserve (other.trait_substs.size ());
132 0 : for (const auto &p : other.trait_substs)
133 0 : trait_substs.push_back (p.clone ());
134 0 : }
135 :
136 : TraitReference &
137 0 : TraitReference::operator= (TraitReference const &other)
138 : {
139 0 : hir_trait_ref = other.hir_trait_ref;
140 0 : item_refs = other.item_refs;
141 0 : super_traits = other.super_traits;
142 :
143 0 : trait_substs.clear ();
144 0 : trait_substs.reserve (other.trait_substs.size ());
145 0 : for (const auto &p : other.trait_substs)
146 0 : trait_substs.push_back (p.clone ());
147 :
148 0 : return *this;
149 : }
150 :
151 : bool
152 321178 : TraitReference::is_error () const
153 : {
154 321178 : return hir_trait_ref == nullptr;
155 : }
156 :
157 : location_t
158 818 : TraitReference::get_locus () const
159 : {
160 818 : return hir_trait_ref->get_locus ();
161 : }
162 :
163 : std::string
164 8575 : TraitReference::get_name () const
165 : {
166 8575 : rust_assert (!is_error ());
167 17150 : return hir_trait_ref->get_name ().as_string ();
168 : }
169 :
170 : std::string
171 227 : TraitReference::as_string () const
172 : {
173 227 : if (is_error ())
174 0 : return "<trait-ref-error-node>";
175 :
176 227 : std::string item_buf;
177 518 : for (auto &item : item_refs)
178 : {
179 873 : item_buf += item.as_string () + ", ";
180 : }
181 454 : return "HIR Trait: " + get_name () + "->"
182 1135 : + hir_trait_ref->get_mappings ().as_string () + " [" + item_buf + "]";
183 227 : }
184 :
185 : const HIR::Trait *
186 264398 : TraitReference::get_hir_trait_ref () const
187 : {
188 264398 : return hir_trait_ref;
189 : }
190 :
191 : const Analysis::NodeMapping &
192 1512988 : TraitReference::get_mappings () const
193 : {
194 1512988 : return hir_trait_ref->get_mappings ();
195 : }
196 :
197 : DefId
198 1553 : TraitReference::get_defid () const
199 : {
200 1553 : return get_mappings ().get_defid ();
201 : }
202 :
203 : bool
204 290 : TraitReference::lookup_hir_trait_item (const HIR::TraitItem &item,
205 : TraitItemReference **ref)
206 : {
207 290 : return lookup_trait_item (item.trait_identifier (), ref);
208 : }
209 :
210 : bool
211 290 : TraitReference::lookup_trait_item (const std::string &ident,
212 : TraitItemReference **ref)
213 : {
214 667 : for (auto &item : item_refs)
215 : {
216 667 : if (ident.compare (item.get_identifier ()) == 0)
217 : {
218 290 : *ref = &item;
219 290 : return true;
220 : }
221 : }
222 : return false;
223 : }
224 :
225 : bool
226 0 : TraitReference::lookup_trait_item_by_type (
227 : const std::string &ident, TraitItemReference::TraitItemType type,
228 : TraitItemReference **ref)
229 : {
230 0 : for (auto &item : item_refs)
231 : {
232 0 : if (item.get_trait_item_type () != type)
233 0 : continue;
234 :
235 0 : if (ident.compare (item.get_identifier ()) == 0)
236 : {
237 0 : *ref = &item;
238 0 : return true;
239 : }
240 : }
241 : return false;
242 : }
243 :
244 : bool
245 21678 : TraitReference::lookup_trait_item_by_type (
246 : const std::string &ident, TraitItemReference::TraitItemType type,
247 : const TraitItemReference **ref) const
248 : {
249 26456 : for (auto &item : item_refs)
250 : {
251 26452 : if (item.get_trait_item_type () != type)
252 2705 : continue;
253 :
254 23747 : if (ident.compare (item.get_identifier ()) == 0)
255 : {
256 21674 : *ref = &item;
257 21674 : return true;
258 : }
259 : }
260 : return false;
261 : }
262 :
263 : bool
264 0 : TraitReference::lookup_hir_trait_item (const HIR::TraitItem &item,
265 : const TraitItemReference **ref) const
266 : {
267 0 : return lookup_trait_item (item.trait_identifier (), ref);
268 : }
269 :
270 : bool
271 58911 : TraitReference::lookup_trait_item (const std::string &ident,
272 : const TraitItemReference **ref,
273 : bool lookup_supers) const
274 : {
275 72229 : for (auto &item : item_refs)
276 : {
277 65956 : if (ident.compare (item.get_identifier ()) == 0)
278 : {
279 52638 : *ref = &item;
280 52638 : return true;
281 : }
282 : }
283 :
284 6273 : if (!lookup_supers)
285 : return false;
286 :
287 : // lookup super traits
288 91 : for (const auto &super_trait : super_traits)
289 : {
290 14 : bool found = super_trait.get ()->lookup_trait_item (ident, ref);
291 14 : if (found)
292 58911 : return true;
293 : }
294 :
295 : return false;
296 : }
297 :
298 : const TraitItemReference *
299 20057 : TraitReference::lookup_trait_item (const std::string &ident,
300 : TraitItemReference::TraitItemType type) const
301 : {
302 41646 : for (auto &item : item_refs)
303 : {
304 40974 : if (item.get_trait_item_type () != type)
305 17997 : continue;
306 :
307 22977 : if (ident.compare (item.get_identifier ()) == 0)
308 20057 : return &item;
309 : }
310 :
311 : // lookup super traits
312 1008 : for (const auto &super_trait : super_traits)
313 : {
314 336 : const TraitItemReference *res
315 336 : = super_trait.get ()->lookup_trait_item (ident, type);
316 336 : if (!res->is_error ())
317 20057 : return res;
318 : }
319 :
320 672 : return &TraitItemReference::error_node ();
321 : }
322 :
323 : size_t
324 8166 : TraitReference::size () const
325 : {
326 8166 : return item_refs.size ();
327 : }
328 :
329 : const std::vector<TraitItemReference> &
330 20256 : TraitReference::get_trait_items () const
331 : {
332 20256 : return item_refs;
333 : }
334 :
335 : void
336 1502 : TraitReference::get_trait_items_and_supers (
337 : std::vector<const TraitItemReference *> &result) const
338 : {
339 3073 : for (const auto &item : item_refs)
340 1571 : result.push_back (&item);
341 :
342 1873 : for (const auto &super_trait : super_traits)
343 371 : super_trait.get ()->get_trait_items_and_supers (result);
344 1502 : }
345 :
346 : void
347 5359 : TraitReference::on_resolved ()
348 : {
349 11379 : for (auto &item : item_refs)
350 : {
351 6020 : if (item.get_trait_item_type ()
352 : == TraitItemReference::TraitItemType::TYPE)
353 1076 : item.on_resolved (this);
354 : }
355 11379 : for (auto &item : item_refs)
356 : {
357 6020 : if (item.get_trait_item_type ()
358 : != TraitItemReference::TraitItemType::TYPE)
359 4944 : item.on_resolved (this);
360 : }
361 5359 : }
362 :
363 : void
364 4500 : TraitReference::resolve_default_function_bodies ()
365 : {
366 8276 : for (auto &item : item_refs)
367 3776 : item.resolve_default_function_body (this);
368 4500 : }
369 :
370 : bool
371 631467 : TraitReference::is_equal (const TraitReference &other) const
372 : {
373 631467 : DefId this_id = get_mappings ().get_defid ();
374 631467 : DefId other_id = other.get_mappings ().get_defid ();
375 631467 : return this_id == other_id;
376 : }
377 :
378 : std::vector<TyTy::TypeBoundPredicate>
379 238172 : TraitReference::get_super_traits () const
380 : {
381 238172 : return super_traits;
382 : }
383 :
384 : bool
385 1192 : TraitReference::is_object_safe (bool emit_error, location_t locus) const
386 : {
387 : // https: // doc.rust-lang.org/reference/items/traits.html#object-safety
388 1192 : std::vector<const TraitReference *> non_object_super_traits;
389 1591 : for (auto &super_trait : super_traits)
390 : {
391 399 : if (!super_trait.get ()->is_object_safe (false, UNDEF_LOCATION))
392 1 : non_object_super_traits.push_back (super_trait.get ());
393 : }
394 :
395 1192 : std::vector<const Resolver::TraitItemReference *> non_object_safe_items;
396 3706 : for (auto &item : get_trait_items ())
397 : {
398 2514 : if (!item.is_object_safe ())
399 11 : non_object_safe_items.push_back (&item);
400 : }
401 :
402 1192 : bool is_safe
403 1192 : = non_object_super_traits.empty () && non_object_safe_items.empty ();
404 1192 : if (emit_error && !is_safe)
405 : {
406 9 : rich_location r (line_table, locus);
407 10 : for (auto &item : non_object_super_traits)
408 1 : r.add_range (item->get_locus ());
409 18 : for (auto &item : non_object_safe_items)
410 9 : r.add_range (item->get_locus ());
411 :
412 9 : rust_error_at (r, "trait bound is not object safe");
413 9 : }
414 :
415 1192 : return is_safe;
416 1192 : }
417 :
418 : bool
419 0 : TraitReference::trait_has_generics () const
420 : {
421 0 : return !trait_substs.empty ();
422 : }
423 :
424 : std::vector<TyTy::SubstitutionParamMapping> &
425 0 : TraitReference::get_trait_substs ()
426 : {
427 0 : return trait_substs;
428 : }
429 :
430 : const std::vector<TyTy::SubstitutionParamMapping> &
431 517969 : TraitReference::get_trait_substs () const
432 : {
433 517969 : return trait_substs;
434 : }
435 :
436 : bool
437 246396 : TraitReference::satisfies_bound (const TraitReference &reference) const
438 : {
439 246396 : if (is_equal (reference))
440 : return true;
441 :
442 16259 : for (const auto &super_trait : super_traits)
443 : {
444 2851 : if (super_trait.get ()->satisfies_bound (reference))
445 246396 : return true;
446 : }
447 :
448 : return false;
449 : }
450 :
451 8167 : AssociatedImplTrait::AssociatedImplTrait (TraitReference *trait,
452 : TyTy::TypeBoundPredicate predicate,
453 : HIR::ImplBlock *impl,
454 : TyTy::BaseType *self,
455 : ImplTraitContextFrame frame)
456 8167 : : trait (trait), predicate (predicate), impl (impl), self (self),
457 8167 : context (TypeCheckContext::get ()), frame (frame)
458 8167 : {}
459 :
460 : TyTy::TypeBoundPredicate &
461 3100 : AssociatedImplTrait::get_predicate ()
462 : {
463 3100 : return predicate;
464 : }
465 :
466 : HIR::ImplBlock *
467 53 : AssociatedImplTrait::get_impl_block ()
468 : {
469 53 : return impl;
470 : }
471 :
472 : TyTy::BaseType *
473 0 : AssociatedImplTrait::get_self ()
474 : {
475 0 : return self;
476 : }
477 :
478 : const TyTy::BaseType *
479 0 : AssociatedImplTrait::get_self () const
480 : {
481 0 : return self;
482 : }
483 :
484 : ImplTraitContextFrame
485 7543 : AssociatedImplTrait::get_frame () const
486 : {
487 7543 : return frame;
488 : }
489 :
490 : } // namespace Resolver
491 : } // namespace Rust
|