1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #ifndef RUST_HIR_PATH_PROBE_H
20 #define RUST_HIR_PATH_PROBE_H
22 #include "rust-hir-type-check-base.h"
23 #include "rust-hir-visitor.h"
24 #include "rust-tyty.h"
29 struct PathProbeCandidate
46 struct EnumItemCandidate
48 const TyTy::ADTType
*parent
;
49 const TyTy::VariantDef
*variant
;
52 struct ImplItemCandidate
54 HIR::ImplItem
*impl_item
;
55 HIR::ImplBlock
*parent
;
58 struct TraitItemCandidate
60 const TraitReference
*trait_ref
;
61 const TraitItemReference
*item_ref
;
70 EnumItemCandidate enum_field
;
71 ImplItemCandidate impl
;
72 TraitItemCandidate trait
;
74 Candidate (EnumItemCandidate enum_field
);
75 Candidate (ImplItemCandidate impl
);
76 Candidate (TraitItemCandidate trait
);
79 PathProbeCandidate (CandidateType type
, TyTy::BaseType
*ty
, location_t locus
,
80 EnumItemCandidate enum_field
);
82 PathProbeCandidate (CandidateType type
, TyTy::BaseType
*ty
, location_t locus
,
83 ImplItemCandidate impl
);
85 PathProbeCandidate (CandidateType type
, TyTy::BaseType
*ty
, location_t locus
,
86 TraitItemCandidate trait
);
88 std::string
as_string () const;
90 bool is_enum_candidate () const;
92 bool is_impl_candidate () const;
94 bool is_trait_candidate () const;
96 bool is_full_trait_item_candidate () const;
98 static PathProbeCandidate
get_error ();
100 bool is_error () const;
102 DefId
get_defid () const;
104 bool operator< (const PathProbeCandidate
&c
) const;
107 class PathProbeType
: public TypeCheckBase
, public HIR::HIRImplVisitor
110 static std::set
<PathProbeCandidate
>
111 Probe (const TyTy::BaseType
*receiver
,
112 const HIR::PathIdentSegment
&segment_name
, bool probe_impls
,
113 bool probe_bounds
, bool ignore_mandatory_trait_items
,
114 DefId specific_trait_id
= UNKNOWN_DEFID
);
116 void visit (HIR::TypeAlias
&alias
) override
;
117 void visit (HIR::ConstantItem
&constant
) override
;
118 void visit (HIR::Function
&function
) override
;
121 void process_enum_item_for_candiates (const TyTy::ADTType
*adt
);
123 void process_impl_items_for_candidates ();
125 void process_impl_item_candidate (HirId id
, HIR::ImplItem
*item
,
126 HIR::ImplBlock
*impl
);
129 process_associated_trait_for_candidates (const TraitReference
*trait_ref
,
130 HIR::ImplBlock
*impl
,
131 bool ignore_mandatory_trait_items
);
134 process_predicate_for_candidates (const TyTy::TypeBoundPredicate
&predicate
,
135 bool ignore_mandatory_trait_items
);
138 PathProbeType (const TyTy::BaseType
*receiver
,
139 const HIR::PathIdentSegment
&query
, DefId specific_trait_id
);
141 std::vector
<std::pair
<const TraitReference
*, HIR::ImplBlock
*>>
143 const std::vector
<std::pair
</*const*/ TraitReference
*, HIR::ImplBlock
*>>
145 const std::vector
<std::pair
<const TraitReference
*, HIR::ImplBlock
*>> b
)
148 bool is_reciever_generic () const;
150 const TyTy::BaseType
*receiver
;
151 const HIR::PathIdentSegment
&search
;
152 std::set
<PathProbeCandidate
> candidates
;
153 HIR::ImplBlock
*current_impl
;
154 DefId specific_trait_id
;
157 class ReportMultipleCandidateError
: private TypeCheckBase
160 static void Report (std::set
<PathProbeCandidate
> &candidates
,
161 const HIR::PathIdentSegment
&query
,
162 location_t query_locus
)
164 rich_location
r (line_table
, query_locus
);
165 for (auto &c
: candidates
)
166 r
.add_range (c
.locus
);
168 std::string rich_msg
= "multiple " + query
.as_string () + " found";
169 r
.add_fixit_replace (rich_msg
.c_str ());
171 rust_error_at (r
, ErrorCode::E0034
,
172 "multiple applicable items in scope for: %qs",
173 query
.as_string ().c_str ());
177 class PathProbeImplTrait
: public PathProbeType
180 static std::set
<PathProbeCandidate
>
181 Probe (const TyTy::BaseType
*receiver
,
182 const HIR::PathIdentSegment
&segment_name
,
183 const TraitReference
*trait_reference
);
186 PathProbeImplTrait (const TyTy::BaseType
*receiver
,
187 const HIR::PathIdentSegment
&query
,
188 const TraitReference
*trait_reference
);
190 void process_trait_impl_items_for_candidates ();
192 const TraitReference
*trait_reference
;
195 } // namespace Resolver
198 #endif // RUST_HIR_PATH_PROBE_H