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