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 : #ifndef RUST_HIR_DOT_OPERATOR
20 : #define RUST_HIR_DOT_OPERATOR
21 :
22 : #include "rust-hir-path-probe.h"
23 :
24 : namespace Rust {
25 : namespace Resolver {
26 :
27 43909 : struct MethodCandidate
28 : {
29 : PathProbeCandidate candidate;
30 : std::vector<Adjustment> adjustments;
31 :
32 : static MethodCandidate get_error ()
33 : {
34 : return {PathProbeCandidate::get_error (), {}};
35 : }
36 :
37 : bool is_error () const { return candidate.is_error (); }
38 :
39 9368 : DefId get_defid () const { return candidate.get_defid (); }
40 :
41 4684 : bool operator< (const MethodCandidate &c) const
42 : {
43 4684 : return get_defid () < c.get_defid ();
44 : }
45 : };
46 :
47 : class MethodResolver : private TypeCheckBase, protected AutoderefCycle
48 : {
49 : public:
50 1338 : struct predicate_candidate
51 : {
52 1338 : predicate_candidate (TyTy::TypeBoundPredicateItem lookup,
53 : TyTy::FnType *fntype)
54 1338 : : lookup (lookup), fntype (fntype)
55 : {}
56 : TyTy::TypeBoundPredicateItem lookup;
57 : TyTy::FnType *fntype;
58 : };
59 :
60 : static std::set<MethodCandidate>
61 : Probe (TyTy::BaseType *receiver, const HIR::PathIdentSegment &segment_name,
62 : bool autoderef_flag = false,
63 : const HIR::Trait *specified_trait = nullptr);
64 :
65 : static std::set<MethodCandidate>
66 : Select (std::set<MethodCandidate> &candidates, TyTy::BaseType *receiver,
67 : std::vector<TyTy::BaseType *> arguments,
68 : TyTy::BaseType *result_type = nullptr);
69 :
70 : static std::vector<predicate_candidate> get_predicate_items (
71 : const HIR::PathIdentSegment &segment_name, const TyTy::BaseType &receiver,
72 : const std::vector<TyTy::TypeBoundPredicate> &specified_bounds);
73 :
74 : struct impl_item_candidate
75 : {
76 22329 : impl_item_candidate (HIR::Function *item, HIR::ImplBlock *impl_block,
77 : TyTy::FnType *ty)
78 22329 : : item (item), impl_block (impl_block), ty (ty)
79 : {}
80 :
81 : HIR::Function *item;
82 : HIR::ImplBlock *impl_block;
83 : TyTy::FnType *ty;
84 : };
85 :
86 : struct trait_item_candidate
87 : {
88 1446 : trait_item_candidate (const HIR::TraitItemFunc *item,
89 : const HIR::Trait *trait, TyTy::FnType *ty,
90 : const TraitReference *reference,
91 : const TraitItemReference *item_ref)
92 1446 : : item (item), trait (trait), ty (ty), reference (reference),
93 1446 : item_ref (item_ref)
94 : {}
95 : const HIR::TraitItemFunc *item;
96 : const HIR::Trait *trait;
97 : TyTy::FnType *ty;
98 : const TraitReference *reference;
99 : const TraitItemReference *item_ref;
100 : };
101 :
102 : protected:
103 : MethodResolver (bool autoderef_flag,
104 : const HIR::PathIdentSegment &segment_name,
105 : const HIR::Trait *specified_trait);
106 :
107 : void try_hook (const TyTy::BaseType &r) override;
108 :
109 : bool select (TyTy::BaseType &receiver) override;
110 :
111 : private:
112 : std::vector<Adjustment>
113 : append_adjustments (const std::vector<Adjustment> &adjustments) const;
114 :
115 : std::vector<impl_item_candidate>
116 : assemble_inherent_impl_candidates (const TyTy::BaseType &receiver);
117 :
118 : void assemble_trait_impl_candidates (
119 : const TyTy::BaseType &receiver,
120 : std::vector<impl_item_candidate> &impl_candidates,
121 : std::vector<trait_item_candidate> &trait_candidates);
122 :
123 : bool try_select_predicate_candidates (TyTy::BaseType &receiver);
124 :
125 : bool try_select_inherent_impl_candidates (
126 : TyTy::BaseType &receiver,
127 : const std::vector<impl_item_candidate> &candidates,
128 : bool trait_impl_blocks_only);
129 :
130 : bool try_select_trait_impl_candidates (
131 : TyTy::BaseType &receiver,
132 : const std::vector<trait_item_candidate> &candidates);
133 :
134 : private:
135 : // search
136 : const HIR::PathIdentSegment &segment_name;
137 : const HIR::Trait *specified_trait;
138 : std::vector<MethodResolver::predicate_candidate> predicate_items;
139 :
140 : // mutable fields
141 : std::set<MethodCandidate> result;
142 : };
143 :
144 : } // namespace Resolver
145 : } // namespace Rust
146 :
147 : #endif // RUST_HIR_DOT_OPERATOR
|