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 3898 : GenericArgsBinding::GenericArgsBinding (Identifier ident,
27 : std::unique_ptr<Type> type_ptr,
28 : location_t locus, Kind kind)
29 3898 : : identifier (std::move (ident)), type (std::move (type_ptr)), locus (locus),
30 3898 : kind (kind)
31 3898 : {}
32 :
33 11512 : GenericArgsBinding::GenericArgsBinding (GenericArgsBinding const &other)
34 11512 : : identifier (other.identifier), type (other.type->clone_type ()),
35 11512 : locus (other.locus), kind (other.kind)
36 11512 : {}
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 442 : ConstGenericArg::ConstGenericArg (std::unique_ptr<Expr> expression,
49 : location_t locus)
50 442 : : expression (std::move (expression)), locus (locus)
51 442 : {}
52 :
53 799 : ConstGenericArg::ConstGenericArg (const ConstGenericArg &other)
54 799 : : locus (other.locus)
55 : {
56 799 : expression = other.expression->clone_expr ();
57 799 : }
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 43310 : GenericArgs::operator= (GenericArgs const &other)
70 : {
71 43310 : lifetime_args = other.lifetime_args;
72 43310 : binding_args = other.binding_args;
73 43310 : const_args = other.const_args;
74 43310 : locus = other.locus;
75 :
76 43310 : type_args.clear ();
77 43310 : type_args.reserve (other.type_args.size ());
78 84541 : for (const auto &e : other.type_args)
79 41231 : type_args.push_back (e->clone_type ());
80 :
81 43310 : return *this;
82 : }
83 :
84 2252419 : 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 2252419 : : lifetime_args (std::move (lifetime_args)),
90 2252419 : type_args (std::move (type_args)), binding_args (std::move (binding_args)),
91 2252419 : const_args (std::move (const_args)), locus (locus)
92 2252419 : {}
93 :
94 5275765 : GenericArgs::GenericArgs (GenericArgs const &other)
95 5275765 : : lifetime_args (other.lifetime_args), binding_args (other.binding_args),
96 5275765 : const_args (other.const_args), locus (other.locus)
97 : {
98 5275765 : type_args.clear ();
99 5275765 : type_args.reserve (other.type_args.size ());
100 :
101 5458644 : for (const auto &e : other.type_args)
102 182879 : type_args.push_back (e->clone_type ());
103 5275765 : }
104 :
105 : bool
106 88400 : GenericArgs::is_empty () const
107 : {
108 175668 : return lifetime_args.size () == 0 && type_args.size () == 0
109 90065 : && binding_args.size () == 0;
110 : }
111 :
112 1954793 : PathExprSegment::PathExprSegment (Analysis::NodeMapping mappings,
113 : PathIdentSegment segment_name,
114 : location_t locus, GenericArgs generic_args)
115 1954793 : : mappings (std::move (mappings)), segment_name (std::move (segment_name)),
116 1954793 : generic_args (std::move (generic_args)), locus (locus)
117 1954793 : {}
118 :
119 5091430 : PathExprSegment::PathExprSegment (PathExprSegment const &other)
120 5091430 : : mappings (other.mappings), segment_name (other.segment_name),
121 5091430 : generic_args (other.generic_args), locus (other.locus)
122 5091430 : {}
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 51037 : PathPattern::iterate_path_segments (std::function<bool (PathExprSegment &)> cb)
137 : {
138 51037 : rust_assert (kind == Kind::Segmented);
139 :
140 105764 : for (auto it = segments.begin (); it != segments.end (); it++)
141 : {
142 55603 : if (!cb (*it))
143 51037 : return;
144 : }
145 : }
146 :
147 1637789 : PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
148 : RawPath<PathExprSegment> path,
149 : location_t locus,
150 : std::vector<AST::Attribute> outer_attrs)
151 : : PathPattern (path),
152 1637789 : PathExpr (std::move (mappings), std::move (outer_attrs)), locus (locus)
153 1637789 : {}
154 :
155 13365 : PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
156 : LangItem::Kind lang_item, location_t locus,
157 : std::vector<AST::Attribute> outer_attrs)
158 : : PathPattern (lang_item),
159 13365 : PathExpr (std::move (mappings), std::move (outer_attrs)), locus (locus)
160 13365 : {}
161 :
162 : bool
163 0 : PathInExpression::is_self () const
164 :
165 : {
166 0 : if (!is_single_segment ())
167 : return false;
168 :
169 0 : return get_final_segment ().get_segment ().to_string ().compare (
170 : Rust::Values::Keywords::SELF)
171 0 : == 0;
172 : }
173 :
174 742085 : TypePathSegment::TypePathSegment (Analysis::NodeMapping mappings,
175 : PathIdentSegment ident_segment,
176 : bool has_separating_scope_resolution,
177 : location_t locus)
178 1484170 : : mappings (std::move (mappings)), ident_segment (std::move (ident_segment)),
179 742085 : lang_item (tl::nullopt), locus (locus),
180 742085 : has_separating_scope_resolution (has_separating_scope_resolution),
181 742085 : type (SegmentType::REG)
182 742085 : {}
183 :
184 7226 : TypePathSegment::TypePathSegment (Analysis::NodeMapping mappings,
185 : LangItem::Kind lang_item, location_t locus)
186 7226 : : mappings (std::move (mappings)), ident_segment (tl::nullopt),
187 7226 : lang_item (lang_item), locus (locus),
188 7226 : has_separating_scope_resolution (false), type (SegmentType::REG)
189 7226 : {}
190 :
191 0 : TypePathSegment::TypePathSegment (Analysis::NodeMapping mappings,
192 : std::string segment_name,
193 : bool has_separating_scope_resolution,
194 : location_t locus)
195 0 : : mappings (std::move (mappings)),
196 0 : ident_segment (PathIdentSegment (std::move (segment_name))),
197 0 : lang_item (tl::nullopt), locus (locus),
198 0 : has_separating_scope_resolution (has_separating_scope_resolution),
199 0 : type (SegmentType::REG)
200 0 : {}
201 :
202 120013 : 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 240026 : generic_args (std::move (generic_args))
209 120013 : {}
210 :
211 541 : 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 541 : generic_args (std::move (generic_args))
217 541 : {}
218 :
219 0 : 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 0 : generic_args (GenericArgs (std::move (lifetime_args), std::move (type_args),
228 : std::move (binding_args), std::move (const_args),
229 0 : locus))
230 0 : {}
231 :
232 4147 : TypePathFunction::TypePathFunction (std::vector<std::unique_ptr<Type>> inputs,
233 : std::unique_ptr<Type> type)
234 4147 : : inputs (std::move (inputs)), return_type (std::move (type))
235 4147 : {}
236 :
237 4910 : TypePathFunction::TypePathFunction (TypePathFunction const &other)
238 : {
239 4910 : return_type = other.has_return_type ()
240 4910 : ? other.get_return_type ().clone_type ()
241 4910 : : nullptr;
242 :
243 4910 : inputs.reserve (other.inputs.size ());
244 11433 : for (const auto &e : other.inputs)
245 6523 : inputs.push_back (e->clone_type ());
246 4910 : }
247 :
248 : TypePathFunction &
249 0 : TypePathFunction::operator= (TypePathFunction const &other)
250 : {
251 0 : return_type = other.has_return_type ()
252 0 : ? other.get_return_type ().clone_type ()
253 0 : : nullptr;
254 :
255 0 : inputs.reserve (other.inputs.size ());
256 0 : for (const auto &e : other.inputs)
257 0 : inputs.push_back (e->clone_type ());
258 :
259 0 : return *this;
260 : }
261 :
262 4147 : 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 8294 : function_path (std::move (function_path))
269 4147 : {}
270 :
271 0 : 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 0 : function_path (std::move (function_path))
278 0 : {}
279 :
280 691456 : TypePath::TypePath (Analysis::NodeMapping mappings,
281 : RawPath<std::unique_ptr<TypePathSegment>> path,
282 : location_t locus)
283 691456 : : TypeNoBounds (mappings, locus), RawPath (std::move (path))
284 691456 : {}
285 :
286 478229 : TypePath::TypePath (TypePath const &other)
287 478229 : : TypeNoBounds (other.mappings, other.locus),
288 478229 : RawPath ({}, other.has_opening_scope_resolution ())
289 : {
290 478229 : segments.reserve (other.segments.size ());
291 965868 : for (const auto &e : other.segments)
292 487639 : segments.push_back (e->clone_type_path_segment ());
293 478229 : }
294 :
295 : TypePath &
296 0 : TypePath::operator= (TypePath const &other)
297 : {
298 0 : opening_scope_resolution = other.has_opening_scope_resolution ();
299 0 : locus = other.locus;
300 0 : mappings = other.mappings;
301 :
302 0 : segments.reserve (other.segments.size ());
303 0 : for (const auto &e : other.segments)
304 0 : segments.push_back (e->clone_type_path_segment ());
305 :
306 0 : return *this;
307 : }
308 :
309 27052 : QualifiedPathType::QualifiedPathType (Analysis::NodeMapping mappings,
310 : std::unique_ptr<Type> type,
311 : std::unique_ptr<TypePath> trait,
312 : location_t locus)
313 27052 : : type (std::move (type)), trait (std::move (trait)), locus (locus),
314 27052 : mappings (mappings)
315 27052 : {}
316 :
317 18452 : QualifiedPathType::QualifiedPathType (QualifiedPathType const &other)
318 18452 : : type (other.type->clone_type ()),
319 18452 : trait (other.has_as_clause ()
320 18452 : ? std::unique_ptr<HIR::TypePath> (new HIR::TypePath (*other.trait))
321 : : nullptr),
322 18452 : locus (other.locus), mappings (other.mappings)
323 18452 : {}
324 :
325 : QualifiedPathType &
326 0 : QualifiedPathType::operator= (QualifiedPathType const &other)
327 : {
328 0 : type = other.type->clone_type ();
329 0 : locus = other.locus;
330 0 : mappings = other.mappings;
331 0 : trait = other.has_as_clause ()
332 0 : ? std::unique_ptr<HIR::TypePath> (new HIR::TypePath (*other.trait))
333 0 : : nullptr;
334 :
335 0 : return *this;
336 : }
337 :
338 : bool
339 0 : QualifiedPathType::trait_has_generic_args () const
340 : {
341 0 : rust_assert (has_as_clause ());
342 0 : bool is_generic_seg = trait->get_final_segment ().get_type ()
343 0 : == TypePathSegment::SegmentType::GENERIC;
344 0 : if (!is_generic_seg)
345 : return false;
346 :
347 0 : auto &seg
348 0 : = static_cast<TypePathSegmentGeneric &> (trait->get_final_segment ());
349 0 : return seg.has_generic_args ();
350 : }
351 :
352 : GenericArgs &
353 0 : QualifiedPathType::get_trait_generic_args ()
354 : {
355 0 : rust_assert (trait_has_generic_args ());
356 0 : auto &seg
357 0 : = static_cast<TypePathSegmentGeneric &> (trait->get_final_segment ());
358 0 : return seg.get_generic_args ();
359 : }
360 :
361 944 : QualifiedPathInExpression::QualifiedPathInExpression (
362 : Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
363 : RawPath<PathExprSegment> path, location_t locus,
364 : std::vector<AST::Attribute> outer_attrs)
365 : : PathPattern (std::move (path)),
366 : PathExpr (std::move (mappings), std::move (outer_attrs)),
367 944 : path_type (std::move (qual_path_type)), locus (locus)
368 944 : {}
369 :
370 0 : QualifiedPathInExpression::QualifiedPathInExpression (
371 : Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
372 : LangItem::Kind lang_item, location_t locus,
373 : std::vector<AST::Attribute> outer_attrs)
374 : : PathPattern (lang_item),
375 : PathExpr (std::move (mappings), std::move (outer_attrs)),
376 0 : path_type (std::move (qual_path_type)), locus (locus)
377 0 : {}
378 :
379 26108 : QualifiedPathInType::QualifiedPathInType (
380 : Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
381 : std::unique_ptr<TypePathSegment> associated_segment,
382 : std::vector<std::unique_ptr<TypePathSegment>> path_segments, location_t locus)
383 26108 : : TypeNoBounds (mappings, locus), path_type (std::move (qual_path_type)),
384 26108 : associated_segment (std::move (associated_segment)),
385 26108 : segments (std::move (path_segments))
386 26108 : {}
387 :
388 190 : QualifiedPathInType::QualifiedPathInType (QualifiedPathInType const &other)
389 190 : : TypeNoBounds (other.mappings, other.locus), path_type (other.path_type)
390 : {
391 190 : auto seg = other.associated_segment->clone_type_path_segment_impl ();
392 190 : associated_segment = std::unique_ptr<TypePathSegment> (seg);
393 :
394 190 : segments.reserve (other.segments.size ());
395 190 : for (const auto &e : other.segments)
396 0 : segments.push_back (e->clone_type_path_segment ());
397 190 : }
398 :
399 : QualifiedPathInType &
400 0 : QualifiedPathInType::operator= (QualifiedPathInType const &other)
401 : {
402 0 : auto seg = other.associated_segment->clone_type_path_segment_impl ();
403 0 : associated_segment = std::unique_ptr<TypePathSegment> (seg);
404 :
405 0 : path_type = other.path_type;
406 0 : locus = other.locus;
407 0 : mappings = other.mappings;
408 :
409 0 : segments.reserve (other.segments.size ());
410 0 : for (const auto &e : other.segments)
411 0 : segments.push_back (e->clone_type_path_segment ());
412 :
413 0 : return *this;
414 : }
415 :
416 : } // namespace HIR
417 : } // namespace Rust
|