Debian Codegraph @

gcc/rust/hir/tree/rust-hir-path.cc

raw
1 // Copyright (C) 2020-2024 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.h"
20 #include "optional.h"
21 #include "rust-hir-bound.h"
22
23 namespace Rust {
24 namespace HIR {
25
26 GenericArgsBinding::GenericArgsBinding (Identifier ident,
27 std::unique_ptr<Type> type_ptr,
28 location_t locus)
29 : identifier (std::move (ident)), type (std::move (type_ptr)), locus (locus)
30 {}
31
32 GenericArgsBinding::GenericArgsBinding (GenericArgsBinding const &other)
33 : identifier (other.identifier), type (other.type->clone_type ()),
34 locus (other.locus)
35 {}
36
37 GenericArgsBinding &
38 GenericArgsBinding::operator= (GenericArgsBinding const &other)
39 {
40 identifier = other.identifier;
41 type = other.type->clone_type ();
42 locus = other.locus;
43 return *this;
44 }
45
46 ConstGenericArg::ConstGenericArg (std::unique_ptr<Expr> expression,
47 location_t locus)
48 : expression (std::move (expression)), locus (locus)
49 {}
50
51 ConstGenericArg::ConstGenericArg (const ConstGenericArg &other)
52 : locus (other.locus)
53 {
54 expression = other.expression->clone_expr ();
55 }
56
57 ConstGenericArg
58 ConstGenericArg::operator= (const ConstGenericArg &other)
59 {
60 expression = other.expression->clone_expr ();
61 locus = other.locus;
62
63 return *this;
64 }
65
66 GenericArgs &
67 GenericArgs::operator= (GenericArgs const &other)
68 {
69 lifetime_args = other.lifetime_args;
70 binding_args = other.binding_args;
71 const_args = other.const_args;
72 locus = other.locus;
73
74 type_args.clear ();
75 type_args.reserve (other.type_args.size ());
76 for (const auto &e : other.type_args)
77 type_args.push_back (e->clone_type ());
78
79 return *this;
80 }
81
82 GenericArgs::GenericArgs (std::vector<Lifetime> lifetime_args,
83 std::vector<std::unique_ptr<Type> > type_args,
84 std::vector<GenericArgsBinding> binding_args,
85 std::vector<ConstGenericArg> const_args,
86 location_t locus)
87 : lifetime_args (std::move (lifetime_args)),
88 type_args (std::move (type_args)), binding_args (std::move (binding_args)),
89 const_args (std::move (const_args)), locus (locus)
90 {}
91
92 GenericArgs::GenericArgs (GenericArgs const &other)
93 : lifetime_args (other.lifetime_args), binding_args (other.binding_args),
94 const_args (other.const_args), locus (other.locus)
95 {
96 type_args.clear ();
97 type_args.reserve (other.type_args.size ());
98
99 for (const auto &e : other.type_args)
100 type_args.push_back (e->clone_type ());
101 }
102
103 bool
104 GenericArgs::is_empty () const
105 {
106 return lifetime_args.size () == 0 && type_args.size () == 0
107 && binding_args.size () == 0;
108 }
109
110 PathExprSegment::PathExprSegment (Analysis::NodeMapping mappings,
111 PathIdentSegment segment_name,
112 location_t locus, GenericArgs generic_args)
113 : mappings (std::move (mappings)), segment_name (std::move (segment_name)),
114 generic_args (std::move (generic_args)), locus (locus)
115 {}
116
117 PathExprSegment::PathExprSegment (PathExprSegment const &other)
118 : mappings (other.mappings), segment_name (other.segment_name),
119 generic_args (other.generic_args), locus (other.locus)
120 {}
121
122 PathExprSegment &
123 PathExprSegment::operator= (PathExprSegment const &other)
124 {
125 mappings = other.mappings;
126 segment_name = other.segment_name;
127 generic_args = other.generic_args;
128 locus = other.locus;
129
130 return *this;
131 }
132
133 void
134 PathPattern::iterate_path_segments (std::function<bool (PathExprSegment &)> cb)
135 {
136 rust_assert (kind == Kind::Segmented);
137
138 for (auto it = segments.begin (); it != segments.end (); it++)
139 {
140 if (!cb (*it))
141 return;
142 }
143 }
144
145 PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
146 std::vector<PathExprSegment> path_segments,
147 location_t locus,
148 bool has_opening_scope_resolution,
149 std::vector<AST::Attribute> outer_attrs)
150 : PathPattern (std::move (path_segments)),
151 PathExpr (std::move (mappings), std::move (outer_attrs)),
152 has_opening_scope_resolution (has_opening_scope_resolution), locus (locus)
153 {}
154
155 PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
156 LangItem::Kind lang_item, location_t locus,
157 bool has_opening_scope_resolution,
158 std::vector<AST::Attribute> outer_attrs)
159 : PathPattern (lang_item),
160 PathExpr (std::move (mappings), std::move (outer_attrs)),
161 has_opening_scope_resolution (has_opening_scope_resolution), locus (locus)
162 {}
163
164 bool
165 PathInExpression::is_self () const
166
167 {
168 if (!is_single_segment ())
169 return false;
170
171 return get_final_segment ().get_segment ().as_string ().compare ("self") == 0;
172 }
173
174 TypePathSegment::TypePathSegment (Analysis::NodeMapping mappings,
175 PathIdentSegment ident_segment,
176 bool has_separating_scope_resolution,
177 location_t locus)
178 : mappings (std::move (mappings)), ident_segment (std::move (ident_segment)),
179 lang_item (tl::nullopt), locus (locus),
180 has_separating_scope_resolution (has_separating_scope_resolution),
181 type (SegmentType::REG)
182 {}
183
184 TypePathSegment::TypePathSegment (Analysis::NodeMapping mappings,
185 LangItem::Kind lang_item, location_t locus)
186 : mappings (std::move (mappings)), ident_segment (tl::nullopt),
187 lang_item (lang_item), locus (locus),
188 has_separating_scope_resolution (false), type (SegmentType::REG)
189 {}
190
191 TypePathSegment::TypePathSegment (Analysis::NodeMapping mappings,
192 std::string segment_name,
193 bool has_separating_scope_resolution,
194 location_t locus)
195 : mappings (std::move (mappings)),
196 ident_segment (PathIdentSegment (std::move (segment_name))),
197 lang_item (tl::nullopt), locus (locus),
198 has_separating_scope_resolution (has_separating_scope_resolution),
199 type (SegmentType::REG)
200 {}
201
202 TypePathSegmentGeneric::TypePathSegmentGeneric (
203 Analysis::NodeMapping mappings, PathIdentSegment ident_segment,
204 bool has_separating_scope_resolution, GenericArgs generic_args,
205 location_t locus)
206 : TypePathSegment (std::move (mappings), std::move (ident_segment),
207 has_separating_scope_resolution, locus),
208 generic_args (std::move (generic_args))
209 {}
210
211 TypePathSegmentGeneric::TypePathSegmentGeneric (Analysis::NodeMapping mappings,
212 LangItem::Kind lang_item,
213 GenericArgs generic_args,
214 location_t locus)
215 : TypePathSegment (std::move (mappings), lang_item, locus),
216 generic_args (std::move (generic_args))
217 {}
218
219 TypePathSegmentGeneric::TypePathSegmentGeneric (
220 Analysis::NodeMapping mappings, std::string segment_name,
221 bool has_separating_scope_resolution, std::vector<Lifetime> lifetime_args,
222 std::vector<std::unique_ptr<Type> > type_args,
223 std::vector<GenericArgsBinding> binding_args,
224 std::vector<ConstGenericArg> const_args, location_t locus)
225 : TypePathSegment (std::move (mappings), std::move (segment_name),
226 has_separating_scope_resolution, locus),
227 generic_args (GenericArgs (std::move (lifetime_args), std::move (type_args),
228 std::move (binding_args), std::move (const_args),
229 locus))
230 {}
231
232 TypePathFunction::TypePathFunction (std::vector<std::unique_ptr<Type> > inputs,
233 std::unique_ptr<Type> type)
234 : inputs (std::move (inputs)), return_type (std::move (type))
235 {}
236
237 TypePathFunction::TypePathFunction (TypePathFunction const &other)
238 {
239 return_type = other.has_return_type ()
240 ? other.get_return_type ().clone_type ()
241 : nullptr;
242
243 inputs.reserve (other.inputs.size ());
244 for (const auto &e : other.inputs)
245 inputs.push_back (e->clone_type ());
246 }
247
248 TypePathFunction &
249 TypePathFunction::operator= (TypePathFunction const &other)
250 {
251 return_type = other.has_return_type ()
252 ? other.get_return_type ().clone_type ()
253 : nullptr;
254
255 inputs.reserve (other.inputs.size ());
256 for (const auto &e : other.inputs)
257 inputs.push_back (e->clone_type ());
258
259 return *this;
260 }
261
262 TypePathSegmentFunction::TypePathSegmentFunction (
263 Analysis::NodeMapping mappings, PathIdentSegment ident_segment,
264 bool has_separating_scope_resolution, TypePathFunction function_path,
265 location_t locus)
266 : TypePathSegment (std::move (mappings), std::move (ident_segment),
267 has_separating_scope_resolution, locus),
268 function_path (std::move (function_path))
269 {}
270
271 TypePathSegmentFunction::TypePathSegmentFunction (
272 Analysis::NodeMapping mappings, std::string segment_name,
273 bool has_separating_scope_resolution, TypePathFunction function_path,
274 location_t locus)
275 : TypePathSegment (std::move (mappings), std::move (segment_name),
276 has_separating_scope_resolution, locus),
277 function_path (std::move (function_path))
278 {}
279
280 TypePath::TypePath (Analysis::NodeMapping mappings,
281 std::vector<std::unique_ptr<TypePathSegment> > segments,
282 location_t locus, bool has_opening_scope_resolution)
283 : TypeNoBounds (mappings, locus),
284 has_opening_scope_resolution (has_opening_scope_resolution),
285 segments (std::move (segments))
286 {}
287
288 TypePath::TypePath (TypePath const &other)
289 : TypeNoBounds (other.mappings, other.locus),
290 has_opening_scope_resolution (other.has_opening_scope_resolution)
291 {
292 segments.reserve (other.segments.size ());
293 for (const auto &e : other.segments)
294 segments.push_back (e->clone_type_path_segment ());
295 }
296
297 TypePath &
298 TypePath::operator= (TypePath const &other)
299 {
300 has_opening_scope_resolution = other.has_opening_scope_resolution;
301 locus = other.locus;
302 mappings = other.mappings;
303
304 segments.reserve (other.segments.size ());
305 for (const auto &e : other.segments)
306 segments.push_back (e->clone_type_path_segment ());
307
308 return *this;
309 }
310
311 QualifiedPathType::QualifiedPathType (Analysis::NodeMapping mappings,
312 std::unique_ptr<Type> type,
313 std::unique_ptr<TypePath> trait,
314 location_t locus)
315 : type (std::move (type)), trait (std::move (trait)), locus (locus),
316 mappings (mappings)
317 {}
318
319 QualifiedPathType::QualifiedPathType (QualifiedPathType const &other)
320 : type (other.type->clone_type ()),
321 trait (other.has_as_clause ()
322 ? std::unique_ptr<HIR::TypePath> (new HIR::TypePath (*other.trait))
323 : nullptr),
324 locus (other.locus), mappings (other.mappings)
325 {}
326
327 QualifiedPathType &
328 QualifiedPathType::operator= (QualifiedPathType const &other)
329 {
330 type = other.type->clone_type ();
331 locus = other.locus;
332 mappings = other.mappings;
333 trait = other.has_as_clause ()
334 ? std::unique_ptr<HIR::TypePath> (new HIR::TypePath (*other.trait))
335 : nullptr;
336
337 return *this;
338 }
339
340 bool
341 QualifiedPathType::trait_has_generic_args () const
342 {
343 rust_assert (has_as_clause ());
344 bool is_generic_seg = trait->get_final_segment ().get_type ()
345 == TypePathSegment::SegmentType::GENERIC;
346 if (!is_generic_seg)
347 return false;
348
349 auto &seg
350 = static_cast<TypePathSegmentGeneric &> (trait->get_final_segment ());
351 return seg.has_generic_args ();
352 }
353
354 GenericArgs &
355 QualifiedPathType::get_trait_generic_args ()
356 {
357 rust_assert (trait_has_generic_args ());
358 auto &seg
359 = static_cast<TypePathSegmentGeneric &> (trait->get_final_segment ());
360 return seg.get_generic_args ();
361 }
362
363 QualifiedPathInExpression::QualifiedPathInExpression (
364 Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
365 std::vector<PathExprSegment> path_segments, location_t locus,
366 std::vector<AST::Attribute> outer_attrs)
367 : PathPattern (std::move (path_segments)),
368 PathExpr (std::move (mappings), std::move (outer_attrs)),
369 path_type (std::move (qual_path_type)), locus (locus)
370 {}
371
372 QualifiedPathInExpression::QualifiedPathInExpression (
373 Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
374 LangItem::Kind lang_item, location_t locus,
375 std::vector<AST::Attribute> outer_attrs)
376 : PathPattern (lang_item),
377 PathExpr (std::move (mappings), std::move (outer_attrs)),
378 path_type (std::move (qual_path_type)), locus (locus)
379 {}
380
381 QualifiedPathInType::QualifiedPathInType (
382 Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
383 std::unique_ptr<TypePathSegment> associated_segment,
384 std::vector<std::unique_ptr<TypePathSegment> > path_segments,
385 location_t locus)
386 : TypeNoBounds (mappings, locus), path_type (std::move (qual_path_type)),
387 associated_segment (std::move (associated_segment)),
388 segments (std::move (path_segments))
389 {}
390
391 QualifiedPathInType::QualifiedPathInType (QualifiedPathInType const &other)
392 : TypeNoBounds (other.mappings, other.locus), path_type (other.path_type)
393 {
394 auto seg = other.associated_segment->clone_type_path_segment_impl ();
395 associated_segment = std::unique_ptr<TypePathSegment> (seg);
396
397 segments.reserve (other.segments.size ());
398 for (const auto &e : other.segments)
399 segments.push_back (e->clone_type_path_segment ());
400 }
401
402 QualifiedPathInType &
403 QualifiedPathInType::operator= (QualifiedPathInType const &other)
404 {
405 auto seg = other.associated_segment->clone_type_path_segment_impl ();
406 associated_segment = std::unique_ptr<TypePathSegment> (seg);
407
408 path_type = other.path_type;
409 locus = other.locus;
410 mappings = other.mappings;
411
412 segments.reserve (other.segments.size ());
413 for (const auto &e : other.segments)
414 segments.push_back (e->clone_type_path_segment ());
415
416 return *this;
417 }
418
419 } // namespace HIR
420 } // namespace Rust