Line data Source code
1 : // Copyright (C) 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-path-probe-impl-trait.h"
20 : #include "rust-hir-item.h"
21 : #include "rust-type-util.h"
22 :
23 : namespace Rust {
24 : namespace Resolver {
25 :
26 1280 : PathProbeImplTrait::PathProbeImplTrait (TyTy::BaseType *receiver,
27 : const HIR::PathIdentSegment &query,
28 : const TraitReference *trait_reference)
29 1280 : : TypeCheckBase (), mappings (Analysis::Mappings::get ()),
30 1280 : receiver (receiver), search (query), trait_reference (trait_reference),
31 1280 : current_impl (nullptr)
32 1280 : {}
33 :
34 : std::set<PathProbeCandidate>
35 1280 : PathProbeImplTrait::Probe (TyTy::BaseType *receiver,
36 : const HIR::PathIdentSegment &segment_name,
37 : const TraitReference *trait_reference)
38 : {
39 1280 : PathProbeImplTrait probe (receiver, segment_name, trait_reference);
40 1280 : probe.process_trait_impl_items_for_candidates ();
41 1280 : return probe.candidates;
42 1280 : }
43 :
44 : void
45 1280 : PathProbeImplTrait::process_trait_impl_items_for_candidates ()
46 : {
47 1280 : DefId trait_id = trait_reference->get_mappings ().get_defid ();
48 1280 : mappings.iterate_trait_impl_items (trait_id,
49 1280 : [this] (HirId id, HIR::ImplItem *item,
50 : HIR::ImplBlock *impl) -> bool {
51 9881 : process_impl_item_candidate (id, item,
52 : impl);
53 9881 : return true;
54 : });
55 1280 : }
56 :
57 : void
58 9881 : PathProbeImplTrait::process_impl_item_candidate (HirId id, HIR::ImplItem *item,
59 : HIR::ImplBlock *impl)
60 : {
61 9881 : current_impl = impl;
62 9881 : HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid ();
63 9881 : TyTy::BaseType *impl_block_ty = nullptr;
64 9881 : if (!query_type (impl_ty_id, &impl_block_ty))
65 7521 : return;
66 :
67 9881 : if (!types_compatable (TyTy::TyWithLocation (receiver),
68 9881 : TyTy::TyWithLocation (impl_block_ty),
69 : impl->get_locus (), false))
70 : return;
71 :
72 2360 : item->accept_vis (*this);
73 : }
74 :
75 : void
76 362 : PathProbeImplTrait::visit (HIR::TypeAlias &alias)
77 : {
78 724 : if (search.to_string () != alias.get_new_type_name ().as_string ())
79 362 : return;
80 :
81 0 : TyTy::BaseType *ty = nullptr;
82 0 : if (!query_type (alias.get_mappings ().get_hirid (), &ty))
83 : return;
84 :
85 0 : PathProbeCandidate::ImplItemCandidate item{&alias, current_impl};
86 0 : candidates.insert (
87 : {PathProbeCandidate::IMPL_TYPE_ALIAS, ty, alias.get_locus (), item});
88 : }
89 :
90 : void
91 0 : PathProbeImplTrait::visit (HIR::ConstantItem &constant)
92 : {
93 0 : if (search.to_string () != constant.get_identifier ().as_string ())
94 0 : return;
95 :
96 0 : TyTy::BaseType *ty = nullptr;
97 0 : if (!query_type (constant.get_mappings ().get_hirid (), &ty))
98 : return;
99 :
100 0 : PathProbeCandidate::ImplItemCandidate item{&constant, current_impl};
101 0 : candidates.insert (
102 : {PathProbeCandidate::IMPL_CONST, ty, constant.get_locus (), item});
103 : }
104 :
105 : void
106 1998 : PathProbeImplTrait::visit (HIR::Function &function)
107 : {
108 3996 : if (search.to_string () != function.get_function_name ().as_string ())
109 919 : return;
110 :
111 1079 : TyTy::BaseType *ty = nullptr;
112 1079 : if (!query_type (function.get_mappings ().get_hirid (), &ty))
113 : return;
114 :
115 1079 : PathProbeCandidate::ImplItemCandidate item{&function, current_impl};
116 1079 : candidates.insert (
117 : {PathProbeCandidate::IMPL_FUNC, ty, function.get_locus (), item});
118 : }
119 :
120 : } // namespace Resolver
121 : } // namespace Rust
|