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 : : #include "rust-hir-path-probe.h"
20 : : #include "rust-hir-trait-resolve.h"
21 : : #include "rust-type-util.h"
22 : : #include "rust-hir-type-bounds.h"
23 : : #include "rust-hir-full.h"
24 : :
25 : : namespace Rust {
26 : : namespace Resolver {
27 : :
28 : : // PathProbeCandidate
29 : :
30 : 71 : PathProbeCandidate::Candidate::Candidate (EnumItemCandidate enum_field)
31 : 71 : : enum_field (enum_field)
32 : 71 : {}
33 : :
34 : 3219 : PathProbeCandidate::Candidate::Candidate (ImplItemCandidate impl) : impl (impl)
35 : 3219 : {}
36 : :
37 : 3634 : PathProbeCandidate::Candidate::Candidate (TraitItemCandidate trait)
38 : 3634 : : trait (trait)
39 : 3634 : {}
40 : :
41 : 71 : PathProbeCandidate::PathProbeCandidate (CandidateType type, TyTy::BaseType *ty,
42 : : location_t locus,
43 : 71 : EnumItemCandidate enum_field)
44 : 71 : : type (type), ty (ty), locus (locus), item (enum_field)
45 : 71 : {}
46 : :
47 : 3219 : PathProbeCandidate::PathProbeCandidate (CandidateType type, TyTy::BaseType *ty,
48 : : location_t locus,
49 : 3219 : ImplItemCandidate impl)
50 : 3219 : : type (type), ty (ty), locus (locus), item (impl)
51 : 3219 : {}
52 : :
53 : 3634 : PathProbeCandidate::PathProbeCandidate (CandidateType type, TyTy::BaseType *ty,
54 : : location_t locus,
55 : 3634 : TraitItemCandidate trait)
56 : 3634 : : type (type), ty (ty), locus (locus), item (trait)
57 : 3634 : {}
58 : :
59 : : std::string
60 : 0 : PathProbeCandidate::as_string () const
61 : : {
62 : 0 : return "PathProbe candidate TODO - as_string";
63 : : }
64 : :
65 : : bool
66 : 2302 : PathProbeCandidate::is_enum_candidate () const
67 : : {
68 : 2302 : return type == ENUM_VARIANT;
69 : : }
70 : :
71 : : bool
72 : 7846 : PathProbeCandidate::is_impl_candidate () const
73 : : {
74 : 7846 : return type == IMPL_CONST || type == IMPL_TYPE_ALIAS || type == IMPL_FUNC;
75 : : }
76 : :
77 : : bool
78 : 0 : PathProbeCandidate::is_trait_candidate () const
79 : : {
80 : 0 : return type == TRAIT_ITEM_CONST || type == TRAIT_TYPE_ALIAS
81 : 0 : || type == TRAIT_FUNC;
82 : : }
83 : :
84 : : bool
85 : 0 : PathProbeCandidate::is_full_trait_item_candidate () const
86 : : {
87 : 0 : return is_trait_candidate () && item.trait.impl == nullptr;
88 : : }
89 : :
90 : : PathProbeCandidate
91 : 0 : PathProbeCandidate::get_error ()
92 : : {
93 : 0 : return PathProbeCandidate (ERROR, nullptr, UNDEF_LOCATION,
94 : 0 : ImplItemCandidate{nullptr, nullptr});
95 : : }
96 : :
97 : : bool
98 : 0 : PathProbeCandidate::is_error () const
99 : : {
100 : 0 : return type == ERROR;
101 : : }
102 : :
103 : : DefId
104 : 6574 : PathProbeCandidate::get_defid () const
105 : : {
106 : 6574 : switch (type)
107 : : {
108 : 0 : case ENUM_VARIANT:
109 : 0 : return item.enum_field.variant->get_defid ();
110 : 1690 : break;
111 : :
112 : 1690 : case IMPL_CONST:
113 : 1690 : case IMPL_TYPE_ALIAS:
114 : 1690 : case IMPL_FUNC:
115 : 1690 : return item.impl.impl_item->get_impl_mappings ().get_defid ();
116 : 4884 : break;
117 : :
118 : 4884 : case TRAIT_ITEM_CONST:
119 : 4884 : case TRAIT_TYPE_ALIAS:
120 : 4884 : case TRAIT_FUNC:
121 : 4884 : return item.trait.item_ref->get_mappings ().get_defid ();
122 : 0 : break;
123 : :
124 : 0 : case ERROR:
125 : 0 : default:
126 : 0 : return UNKNOWN_DEFID;
127 : : }
128 : :
129 : : return UNKNOWN_DEFID;
130 : : }
131 : :
132 : : bool
133 : 504 : PathProbeCandidate::operator< (const PathProbeCandidate &c) const
134 : : {
135 : 504 : return get_defid () < c.get_defid ();
136 : : }
137 : :
138 : : // PathProbeType
139 : :
140 : 4285 : PathProbeType::PathProbeType (const TyTy::BaseType *receiver,
141 : : const HIR::PathIdentSegment &query,
142 : 4285 : DefId specific_trait_id)
143 : 4285 : : TypeCheckBase (), receiver (receiver), search (query),
144 : 4285 : current_impl (nullptr), specific_trait_id (specific_trait_id)
145 : 4285 : {}
146 : :
147 : : std::set<PathProbeCandidate>
148 : 3565 : PathProbeType::Probe (const TyTy::BaseType *receiver,
149 : : const HIR::PathIdentSegment &segment_name,
150 : : bool probe_impls, bool probe_bounds,
151 : : bool ignore_mandatory_trait_items,
152 : : DefId specific_trait_id)
153 : : {
154 : 3565 : PathProbeType probe (receiver, segment_name, specific_trait_id);
155 : 3565 : if (probe_impls)
156 : : {
157 : 1355 : if (receiver->get_kind () == TyTy::TypeKind::ADT)
158 : : {
159 : 706 : const TyTy::ADTType *adt
160 : : = static_cast<const TyTy::ADTType *> (receiver);
161 : 706 : if (adt->is_enum ())
162 : 77 : probe.process_enum_item_for_candiates (adt);
163 : : }
164 : :
165 : 1355 : probe.process_impl_items_for_candidates ();
166 : : }
167 : :
168 : 3565 : if (!probe_bounds)
169 : 2304 : return probe.candidates;
170 : :
171 : 1261 : if (!probe.is_receiver_generic ())
172 : : {
173 : 68 : std::vector<std::pair<TraitReference *, HIR::ImplBlock *>> probed_bounds
174 : 68 : = TypeBoundsProbe::Probe (receiver);
175 : 230 : for (auto &candidate : probed_bounds)
176 : : {
177 : 162 : const TraitReference *trait_ref = candidate.first;
178 : 162 : if (specific_trait_id != UNKNOWN_DEFID)
179 : : {
180 : 0 : if (trait_ref->get_mappings ().get_defid () != specific_trait_id)
181 : 0 : continue;
182 : : }
183 : :
184 : 162 : HIR::ImplBlock *impl = candidate.second;
185 : 162 : probe.process_associated_trait_for_candidates (
186 : : trait_ref, impl, ignore_mandatory_trait_items);
187 : : }
188 : 68 : }
189 : :
190 : 2698 : for (const TyTy::TypeBoundPredicate &predicate :
191 : 2698 : receiver->get_specified_bounds ())
192 : : {
193 : 1437 : const TraitReference *trait_ref = predicate.get ();
194 : 1437 : if (specific_trait_id != UNKNOWN_DEFID)
195 : : {
196 : 0 : if (trait_ref->get_mappings ().get_defid () != specific_trait_id)
197 : 0 : continue;
198 : : }
199 : :
200 : 1437 : probe.process_predicate_for_candidates (predicate,
201 : : ignore_mandatory_trait_items);
202 : : }
203 : :
204 : 1261 : return probe.candidates;
205 : 3565 : }
206 : :
207 : : void
208 : 2158 : PathProbeType::visit (HIR::TypeAlias &alias)
209 : : {
210 : 2158 : Identifier name = alias.get_new_type_name ();
211 : 4316 : if (search.as_string ().compare (name.as_string ()) == 0)
212 : : {
213 : 14 : HirId tyid = alias.get_mappings ().get_hirid ();
214 : 14 : TyTy::BaseType *ty = nullptr;
215 : 14 : if (!query_type (tyid, &ty))
216 : 0 : return;
217 : :
218 : 14 : PathProbeCandidate::ImplItemCandidate impl_item_candidate{&alias,
219 : 14 : current_impl};
220 : 14 : PathProbeCandidate candidate{
221 : : PathProbeCandidate::CandidateType::IMPL_TYPE_ALIAS, ty,
222 : 14 : alias.get_locus (), impl_item_candidate};
223 : 14 : candidates.insert (std::move (candidate));
224 : : }
225 : 2158 : }
226 : :
227 : : void
228 : 58 : PathProbeType::visit (HIR::ConstantItem &constant)
229 : : {
230 : 58 : Identifier name = constant.get_identifier ();
231 : 116 : if (search.as_string ().compare (name.as_string ()) == 0)
232 : : {
233 : 27 : HirId tyid = constant.get_mappings ().get_hirid ();
234 : 27 : TyTy::BaseType *ty = nullptr;
235 : 27 : if (!query_type (tyid, &ty))
236 : 0 : return;
237 : :
238 : 27 : PathProbeCandidate::ImplItemCandidate impl_item_candidate{&constant,
239 : 27 : current_impl};
240 : 27 : PathProbeCandidate candidate{
241 : : PathProbeCandidate::CandidateType::IMPL_CONST, ty,
242 : 27 : constant.get_locus (), impl_item_candidate};
243 : 27 : candidates.insert (std::move (candidate));
244 : : }
245 : 58 : }
246 : :
247 : : void
248 : 7685 : PathProbeType::visit (HIR::Function &function)
249 : : {
250 : 7685 : Identifier name = function.get_function_name ();
251 : 15370 : if (search.as_string ().compare (name.as_string ()) == 0)
252 : : {
253 : 1573 : HirId tyid = function.get_mappings ().get_hirid ();
254 : 1573 : TyTy::BaseType *ty = nullptr;
255 : 1573 : if (!query_type (tyid, &ty))
256 : 0 : return;
257 : :
258 : 1573 : PathProbeCandidate::ImplItemCandidate impl_item_candidate{&function,
259 : 1573 : current_impl};
260 : 1573 : PathProbeCandidate candidate{PathProbeCandidate::CandidateType::IMPL_FUNC,
261 : : ty, function.get_locus (),
262 : 1573 : impl_item_candidate};
263 : 1573 : candidates.insert (std::move (candidate));
264 : : }
265 : 7685 : }
266 : :
267 : : void
268 : 77 : PathProbeType::process_enum_item_for_candiates (const TyTy::ADTType *adt)
269 : : {
270 : 77 : if (specific_trait_id != UNKNOWN_DEFID)
271 : 6 : return;
272 : :
273 : 77 : TyTy::VariantDef *v;
274 : 154 : if (!adt->lookup_variant (search.as_string (), &v))
275 : : return;
276 : :
277 : 71 : PathProbeCandidate::EnumItemCandidate enum_item_candidate{adt, v};
278 : 71 : PathProbeCandidate candidate{PathProbeCandidate::CandidateType::ENUM_VARIANT,
279 : 71 : receiver->clone (),
280 : 71 : mappings.lookup_location (adt->get_ty_ref ()),
281 : 71 : enum_item_candidate};
282 : 71 : candidates.insert (std::move (candidate));
283 : : }
284 : :
285 : : void
286 : 1355 : PathProbeType::process_impl_items_for_candidates ()
287 : : {
288 : 1355 : mappings.iterate_impl_items (
289 : 1355 : [&] (HirId id, HIR::ImplItem *item, HIR::ImplBlock *impl) mutable -> bool {
290 : 55025 : process_impl_item_candidate (id, item, impl);
291 : 55025 : return true;
292 : : });
293 : 1355 : }
294 : :
295 : : void
296 : 64029 : PathProbeType::process_impl_item_candidate (HirId id, HIR::ImplItem *item,
297 : : HIR::ImplBlock *impl)
298 : : {
299 : 64029 : current_impl = impl;
300 : 64029 : HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid ();
301 : 64029 : TyTy::BaseType *impl_block_ty = nullptr;
302 : 64029 : if (!query_type (impl_ty_id, &impl_block_ty))
303 : 54128 : return;
304 : :
305 : 64029 : if (!receiver->can_eq (impl_block_ty, false))
306 : : {
307 : 56575 : if (!impl_block_ty->can_eq (receiver, false))
308 : : return;
309 : : }
310 : :
311 : : // lets visit the impl_item
312 : 9901 : item->accept_vis (*this);
313 : : }
314 : :
315 : : void
316 : 162 : PathProbeType::process_associated_trait_for_candidates (
317 : : const TraitReference *trait_ref, HIR::ImplBlock *impl,
318 : : bool ignore_mandatory_trait_items)
319 : : {
320 : 162 : const TraitItemReference *trait_item_ref = nullptr;
321 : 324 : if (!trait_ref->lookup_trait_item (search.as_string (), &trait_item_ref))
322 : 79 : return;
323 : :
324 : 83 : bool trait_item_needs_implementation = !trait_item_ref->is_optional ();
325 : 83 : if (ignore_mandatory_trait_items && trait_item_needs_implementation)
326 : : return;
327 : :
328 : 83 : PathProbeCandidate::CandidateType candidate_type;
329 : 83 : switch (trait_item_ref->get_trait_item_type ())
330 : : {
331 : : case TraitItemReference::TraitItemType::FN:
332 : : candidate_type = PathProbeCandidate::CandidateType::TRAIT_FUNC;
333 : : break;
334 : : case TraitItemReference::TraitItemType::CONST:
335 : : candidate_type = PathProbeCandidate::CandidateType::TRAIT_ITEM_CONST;
336 : : break;
337 : : case TraitItemReference::TraitItemType::TYPE:
338 : : candidate_type = PathProbeCandidate::CandidateType::TRAIT_TYPE_ALIAS;
339 : : break;
340 : :
341 : 0 : case TraitItemReference::TraitItemType::ERROR:
342 : 0 : default:
343 : 0 : rust_unreachable ();
344 : 83 : break;
345 : : }
346 : :
347 : 83 : const TyTy::TypeBoundPredicate p (*trait_ref, BoundPolarity::RegularBound,
348 : 83 : UNDEF_LOCATION);
349 : 166 : TyTy::TypeBoundPredicateItem item (p, trait_item_ref);
350 : :
351 : 83 : TyTy::BaseType *trait_item_tyty = item.get_raw_item ()->get_tyty ();
352 : 83 : if (receiver->get_kind () != TyTy::DYNAMIC)
353 : 83 : trait_item_tyty = item.get_tyty_for_receiver (receiver);
354 : :
355 : 83 : PathProbeCandidate::TraitItemCandidate trait_item_candidate{trait_ref,
356 : : trait_item_ref,
357 : 83 : impl};
358 : 83 : PathProbeCandidate candidate{candidate_type, trait_item_tyty,
359 : : trait_item_ref->get_locus (),
360 : 83 : trait_item_candidate};
361 : 83 : candidates.insert (std::move (candidate));
362 : : }
363 : :
364 : : void
365 : 1437 : PathProbeType::process_predicate_for_candidates (
366 : : const TyTy::TypeBoundPredicate &predicate, bool ignore_mandatory_trait_items)
367 : : {
368 : 1437 : const TraitReference *trait_ref = predicate.get ();
369 : :
370 : 1437 : TyTy::TypeBoundPredicateItem item
371 : 2874 : = predicate.lookup_associated_item (search.as_string ());
372 : 1437 : if (item.is_error ())
373 : : return;
374 : :
375 : 1193 : if (ignore_mandatory_trait_items && item.needs_implementation ())
376 : : return;
377 : :
378 : 1193 : const TraitItemReference *trait_item_ref = item.get_raw_item ();
379 : 1193 : PathProbeCandidate::CandidateType candidate_type;
380 : 1193 : switch (trait_item_ref->get_trait_item_type ())
381 : : {
382 : : case TraitItemReference::TraitItemType::FN:
383 : : candidate_type = PathProbeCandidate::CandidateType::TRAIT_FUNC;
384 : : break;
385 : : case TraitItemReference::TraitItemType::CONST:
386 : : candidate_type = PathProbeCandidate::CandidateType::TRAIT_ITEM_CONST;
387 : : break;
388 : : case TraitItemReference::TraitItemType::TYPE:
389 : : candidate_type = PathProbeCandidate::CandidateType::TRAIT_TYPE_ALIAS;
390 : : break;
391 : :
392 : 0 : case TraitItemReference::TraitItemType::ERROR:
393 : 0 : default:
394 : 0 : rust_unreachable ();
395 : 1193 : break;
396 : : }
397 : :
398 : 1193 : TyTy::BaseType *trait_item_tyty = item.get_raw_item ()->get_tyty ();
399 : 1193 : if (receiver->get_kind () != TyTy::DYNAMIC)
400 : 949 : trait_item_tyty = item.get_tyty_for_receiver (receiver);
401 : :
402 : 1193 : PathProbeCandidate::TraitItemCandidate trait_item_candidate{trait_ref,
403 : : trait_item_ref,
404 : 1193 : nullptr};
405 : 1193 : PathProbeCandidate candidate{candidate_type, trait_item_tyty,
406 : : trait_item_ref->get_locus (),
407 : 1193 : trait_item_candidate};
408 : 1193 : candidates.insert (std::move (candidate));
409 : 1437 : }
410 : :
411 : : std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>>
412 : 0 : PathProbeType::union_bounds (
413 : : const std::vector<std::pair</*const*/ TraitReference *, HIR::ImplBlock *>> a,
414 : : const std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>> b)
415 : : const
416 : : {
417 : 0 : std::map<DefId, std::pair<const TraitReference *, HIR::ImplBlock *>> mapper;
418 : 0 : for (auto &ref : a)
419 : : {
420 : 0 : mapper.insert ({ref.first->get_mappings ().get_defid (), ref});
421 : : }
422 : 0 : for (auto &ref : b)
423 : : {
424 : 0 : mapper.insert ({ref.first->get_mappings ().get_defid (), ref});
425 : : }
426 : :
427 : 0 : std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>> union_set;
428 : 0 : for (auto it = mapper.begin (); it != mapper.end (); it++)
429 : : {
430 : 0 : union_set.push_back ({it->second.first, it->second.second});
431 : : }
432 : 0 : return union_set;
433 : 0 : }
434 : :
435 : : bool
436 : 1261 : PathProbeType::is_receiver_generic () const
437 : : {
438 : 1261 : const TyTy::BaseType *root = receiver->get_root ();
439 : 1261 : bool receiver_is_type_param = root->get_kind () == TyTy::TypeKind::PARAM;
440 : 1261 : bool receiver_is_dyn = root->get_kind () == TyTy::TypeKind::DYNAMIC;
441 : 1261 : return receiver_is_type_param || receiver_is_dyn;
442 : : }
443 : :
444 : : // PathProbImplTrait
445 : :
446 : 720 : PathProbeImplTrait::PathProbeImplTrait (const TyTy::BaseType *receiver,
447 : : const HIR::PathIdentSegment &query,
448 : 720 : const TraitReference *trait_reference)
449 : : : PathProbeType (receiver, query, UNKNOWN_DEFID),
450 : 720 : trait_reference (trait_reference)
451 : 720 : {}
452 : :
453 : : std::set<PathProbeCandidate>
454 : 720 : PathProbeImplTrait::Probe (const TyTy::BaseType *receiver,
455 : : const HIR::PathIdentSegment &segment_name,
456 : : const TraitReference *trait_reference)
457 : : {
458 : 720 : PathProbeImplTrait probe (receiver, segment_name, trait_reference);
459 : : // iterate all impls for this trait and receiver
460 : : // then search for possible candidates using base class behaviours
461 : 720 : probe.process_trait_impl_items_for_candidates ();
462 : 720 : return probe.candidates;
463 : 720 : }
464 : :
465 : : void
466 : 720 : PathProbeImplTrait::process_trait_impl_items_for_candidates ()
467 : : {
468 : 720 : mappings.iterate_impl_items (
469 : 720 : [&] (HirId id, HIR::ImplItem *item, HIR::ImplBlock *impl) mutable -> bool {
470 : : // just need to check if this is an impl block for this trait the next
471 : : // function checks the receiver
472 : 24072 : if (!impl->has_trait_ref ())
473 : : return true;
474 : :
475 : 16422 : TraitReference *resolved = TraitResolver::Lookup (impl->get_trait_ref ());
476 : 16422 : if (!trait_reference->is_equal (*resolved))
477 : : return true;
478 : :
479 : 9004 : process_impl_item_candidate (id, item, impl);
480 : 9004 : return true;
481 : : });
482 : 720 : }
483 : :
484 : : } // namespace Resolver
485 : : } // namespace Rust
|