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