1 // Copyright (C) 2020-2023 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-full.h"
24 #include "rust-tyty.h"
25 #include "rust-substitution-mapper.h"
26 #include "rust-hir-type-bounds.h"
31 struct PathProbeCandidate
48 struct EnumItemCandidate
50 const TyTy::ADTType
*parent
;
51 const TyTy::VariantDef
*variant
;
54 struct ImplItemCandidate
56 HIR::ImplItem
*impl_item
;
57 HIR::ImplBlock
*parent
;
60 struct TraitItemCandidate
62 const TraitReference
*trait_ref
;
63 const TraitItemReference
*item_ref
;
72 EnumItemCandidate enum_field
;
73 ImplItemCandidate impl
;
74 TraitItemCandidate trait
;
76 Candidate (EnumItemCandidate enum_field
);
77 Candidate (ImplItemCandidate impl
);
78 Candidate (TraitItemCandidate trait
);
81 PathProbeCandidate (CandidateType type
, TyTy::BaseType
*ty
, Location locus
,
82 EnumItemCandidate enum_field
);
84 PathProbeCandidate (CandidateType type
, TyTy::BaseType
*ty
, Location locus
,
85 ImplItemCandidate impl
);
87 PathProbeCandidate (CandidateType type
, TyTy::BaseType
*ty
, Location locus
,
88 TraitItemCandidate trait
);
90 std::string
as_string () const;
92 bool is_enum_candidate () const;
94 bool is_impl_candidate () const;
96 bool is_trait_candidate () const;
98 bool is_full_trait_item_candidate () const;
100 static PathProbeCandidate
get_error ();
102 bool is_error () const;
104 DefId
get_defid () const;
106 bool operator< (const PathProbeCandidate
&c
) const;
109 class PathProbeType
: public TypeCheckBase
, public HIR::HIRImplVisitor
112 static std::set
<PathProbeCandidate
>
113 Probe (const TyTy::BaseType
*receiver
,
114 const HIR::PathIdentSegment
&segment_name
, bool probe_impls
,
115 bool probe_bounds
, bool ignore_mandatory_trait_items
,
116 DefId specific_trait_id
= UNKNOWN_DEFID
);
118 void visit (HIR::TypeAlias
&alias
) override
;
119 void visit (HIR::ConstantItem
&constant
) override
;
120 void visit (HIR::Function
&function
) override
;
123 void process_enum_item_for_candiates (const TyTy::ADTType
*adt
);
125 void process_impl_items_for_candidates ();
127 void process_impl_item_candidate (HirId id
, HIR::ImplItem
*item
,
128 HIR::ImplBlock
*impl
);
131 process_associated_trait_for_candidates (const TraitReference
*trait_ref
,
132 HIR::ImplBlock
*impl
,
133 bool ignore_mandatory_trait_items
);
136 process_predicate_for_candidates (const TyTy::TypeBoundPredicate
&predicate
,
137 bool ignore_mandatory_trait_items
);
140 PathProbeType (const TyTy::BaseType
*receiver
,
141 const HIR::PathIdentSegment
&query
, DefId specific_trait_id
);
143 std::vector
<std::pair
<const TraitReference
*, HIR::ImplBlock
*>>
145 const std::vector
<std::pair
</*const*/ TraitReference
*, HIR::ImplBlock
*>>
147 const std::vector
<std::pair
<const TraitReference
*, HIR::ImplBlock
*>> b
)
150 bool is_reciever_generic () const;
152 const TyTy::BaseType
*receiver
;
153 const HIR::PathIdentSegment
&search
;
154 std::set
<PathProbeCandidate
> candidates
;
155 HIR::ImplBlock
*current_impl
;
156 DefId specific_trait_id
;
159 class ReportMultipleCandidateError
: private TypeCheckBase
162 static void Report (std::set
<PathProbeCandidate
> &candidates
,
163 const HIR::PathIdentSegment
&query
, Location query_locus
)
165 RichLocation
r (query_locus
);
166 for (auto &c
: candidates
)
167 r
.add_range (c
.locus
);
169 rust_error_at (r
, "multiple applicable items in scope for: %s",
170 query
.as_string ().c_str ());
174 class PathProbeImplTrait
: public PathProbeType
177 static std::set
<PathProbeCandidate
>
178 Probe (const TyTy::BaseType
*receiver
,
179 const HIR::PathIdentSegment
&segment_name
,
180 const TraitReference
*trait_reference
);
183 PathProbeImplTrait (const TyTy::BaseType
*receiver
,
184 const HIR::PathIdentSegment
&query
,
185 const TraitReference
*trait_reference
);
187 void process_trait_impl_items_for_candidates ();
189 const TraitReference
*trait_reference
;
192 } // namespace Resolver
195 #endif // RUST_HIR_PATH_PROBE_H