1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
// Copyright (C) 2020-2023 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#ifndef RUST_HIR_PATH_PROBE_H
#define RUST_HIR_PATH_PROBE_H
#include "rust-hir-type-check-base.h"
#include "rust-hir-full.h"
#include "rust-tyty.h"
#include "rust-substitution-mapper.h"
#include "rust-hir-type-bounds.h"
namespace Rust {
namespace Resolver {
struct PathProbeCandidate
{
enum CandidateType
{
ERROR,
ENUM_VARIANT,
IMPL_CONST,
IMPL_TYPE_ALIAS,
IMPL_FUNC,
TRAIT_ITEM_CONST,
TRAIT_TYPE_ALIAS,
TRAIT_FUNC,
};
struct EnumItemCandidate
{
const TyTy::ADTType *parent;
const TyTy::VariantDef *variant;
};
struct ImplItemCandidate
{
HIR::ImplItem *impl_item;
HIR::ImplBlock *parent;
};
struct TraitItemCandidate
{
const TraitReference *trait_ref;
const TraitItemReference *item_ref;
HIR::ImplBlock *impl;
};
CandidateType type;
TyTy::BaseType *ty;
Location locus;
union Candidate
{
EnumItemCandidate enum_field;
ImplItemCandidate impl;
TraitItemCandidate trait;
Candidate (EnumItemCandidate enum_field);
Candidate (ImplItemCandidate impl);
Candidate (TraitItemCandidate trait);
} item;
PathProbeCandidate (CandidateType type, TyTy::BaseType *ty, Location locus,
EnumItemCandidate enum_field);
PathProbeCandidate (CandidateType type, TyTy::BaseType *ty, Location locus,
ImplItemCandidate impl);
PathProbeCandidate (CandidateType type, TyTy::BaseType *ty, Location locus,
TraitItemCandidate trait);
std::string as_string () const;
bool is_enum_candidate () const;
bool is_impl_candidate () const;
bool is_trait_candidate () const;
bool is_full_trait_item_candidate () const;
static PathProbeCandidate get_error ();
bool is_error () const;
DefId get_defid () const;
bool operator< (const PathProbeCandidate &c) const;
};
class PathProbeType : public TypeCheckBase, public HIR::HIRImplVisitor
{
public:
static std::set<PathProbeCandidate>
Probe (const TyTy::BaseType *receiver,
const HIR::PathIdentSegment &segment_name, bool probe_impls,
bool probe_bounds, bool ignore_mandatory_trait_items,
DefId specific_trait_id = UNKNOWN_DEFID);
void visit (HIR::TypeAlias &alias) override;
void visit (HIR::ConstantItem &constant) override;
void visit (HIR::Function &function) override;
protected:
void process_enum_item_for_candiates (const TyTy::ADTType *adt);
void process_impl_items_for_candidates ();
void process_impl_item_candidate (HirId id, HIR::ImplItem *item,
HIR::ImplBlock *impl);
void
process_associated_trait_for_candidates (const TraitReference *trait_ref,
HIR::ImplBlock *impl,
bool ignore_mandatory_trait_items);
void
process_predicate_for_candidates (const TyTy::TypeBoundPredicate &predicate,
bool ignore_mandatory_trait_items);
protected:
PathProbeType (const TyTy::BaseType *receiver,
const HIR::PathIdentSegment &query, DefId specific_trait_id);
std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>>
union_bounds (
const std::vector<std::pair</*const*/ TraitReference *, HIR::ImplBlock *>>
a,
const std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>> b)
const;
bool is_reciever_generic () const;
const TyTy::BaseType *receiver;
const HIR::PathIdentSegment &search;
std::set<PathProbeCandidate> candidates;
HIR::ImplBlock *current_impl;
DefId specific_trait_id;
};
class ReportMultipleCandidateError : private TypeCheckBase
{
public:
static void Report (std::set<PathProbeCandidate> &candidates,
const HIR::PathIdentSegment &query, Location query_locus)
{
RichLocation r (query_locus);
for (auto &c : candidates)
r.add_range (c.locus);
rust_error_at (r, "multiple applicable items in scope for: %s",
query.as_string ().c_str ());
}
};
class PathProbeImplTrait : public PathProbeType
{
public:
static std::set<PathProbeCandidate>
Probe (const TyTy::BaseType *receiver,
const HIR::PathIdentSegment &segment_name,
const TraitReference *trait_reference);
private:
PathProbeImplTrait (const TyTy::BaseType *receiver,
const HIR::PathIdentSegment &query,
const TraitReference *trait_reference);
void process_trait_impl_items_for_candidates ();
const TraitReference *trait_reference;
};
} // namespace Resolver
} // namespace Rust
#endif // RUST_HIR_PATH_PROBE_H
|