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-ast-builder.h"
20 : #include "optional.h"
21 : #include "rust-ast.h"
22 : #include "rust-common.h"
23 : #include "rust-expr.h"
24 : #include "rust-keyword-values.h"
25 : #include "rust-path.h"
26 : #include "rust-item.h"
27 : #include "rust-path.h"
28 : #include "rust-pattern.h"
29 : #include "rust-system.h"
30 : #include "rust-token.h"
31 :
32 : namespace Rust {
33 : namespace AST {
34 :
35 : std::unique_ptr<Stmt>
36 3737 : Builder::statementify (std::unique_ptr<Expr> &&value,
37 : bool semicolon_followed) const
38 : {
39 3737 : return std::make_unique<ExprStmt> (std::move (value), loc,
40 3737 : semicolon_followed);
41 : }
42 :
43 : std::unique_ptr<Expr>
44 1195 : Builder::literal_string (std::string &&content) const
45 : {
46 1195 : return std::unique_ptr<Expr> (
47 : new AST::LiteralExpr (std::move (content), Literal::LitType::STRING,
48 1195 : PrimitiveCoreType::CORETYPE_STR, {}, loc));
49 : }
50 :
51 : std::unique_ptr<Expr>
52 88 : Builder::literal_bool (bool b) const
53 : {
54 176 : auto str
55 88 : = b ? Values::Keywords::TRUE_LITERAL : Values::Keywords::FALSE_LITERAL;
56 :
57 88 : return std::unique_ptr<Expr> (
58 176 : new AST::LiteralExpr (std::move (str), Literal::LitType::BOOL,
59 88 : PrimitiveCoreType::CORETYPE_BOOL, {}, loc));
60 : }
61 :
62 : std::unique_ptr<Expr>
63 17653 : Builder::call (std::unique_ptr<Expr> &&path,
64 : std::vector<std::unique_ptr<Expr>> &&args) const
65 : {
66 17653 : return std::unique_ptr<Expr> (
67 17653 : new CallExpr (std::move (path), std::move (args), {}, loc));
68 : }
69 :
70 : std::unique_ptr<Expr>
71 8844 : Builder::call (std::unique_ptr<Expr> &&path, std::unique_ptr<Expr> &&arg) const
72 : {
73 8844 : auto args = std::vector<std::unique_ptr<Expr>> ();
74 8844 : args.emplace_back (std::move (arg));
75 :
76 8844 : return call (std::move (path), std::move (args));
77 8844 : }
78 :
79 : std::unique_ptr<Expr>
80 1174 : Builder::array (std::vector<std::unique_ptr<Expr>> &&members) const
81 : {
82 1174 : auto elts = std::make_unique<ArrayElemsValues> (std::move (members), loc);
83 :
84 1174 : return std::unique_ptr<Expr> (new ArrayExpr (std::move (elts), {}, {}, loc));
85 1174 : }
86 :
87 : std::unique_ptr<Expr>
88 100 : Builder::qualified_path_in_expression (std::unique_ptr<Type> &&type,
89 : TypePath trait,
90 : PathExprSegment segment) const
91 : {
92 300 : auto segments = {segment};
93 :
94 100 : return qualified_path_in_expression (std::move (type), trait, segments);
95 200 : }
96 :
97 : std::unique_ptr<Expr>
98 100 : Builder::qualified_path_in_expression (
99 : std::unique_ptr<Type> &&type, TypePath trait,
100 : std::vector<PathExprSegment> &&segments) const
101 : {
102 100 : auto qual_type = QualifiedPathType (std::move (type), loc, trait);
103 :
104 100 : return std::unique_ptr<QualifiedPathInExpression> (
105 100 : new QualifiedPathInExpression (qual_type, std::move (segments), {}, loc));
106 100 : }
107 :
108 : std::unique_ptr<Expr>
109 24200 : Builder::identifier (std::string name) const
110 : {
111 72600 : return std::unique_ptr<Expr> (new IdentifierExpr (name, {}, loc));
112 : }
113 :
114 : std::unique_ptr<Pattern>
115 11856 : Builder::identifier_pattern (std::string name, bool mut) const
116 : {
117 11856 : return std::unique_ptr<Pattern> (
118 35568 : new IdentifierPattern (name, loc, false, mut));
119 : }
120 :
121 : std::unique_ptr<Expr>
122 9763 : Builder::tuple_idx (std::string receiver, int idx) const
123 : {
124 9763 : return std::unique_ptr<Expr> (
125 19526 : new TupleIndexExpr (identifier (receiver), idx, {}, loc));
126 : }
127 :
128 : std::unique_ptr<Expr>
129 1943 : Builder::tuple (std::vector<std::unique_ptr<Expr>> &&values) const
130 : {
131 1943 : return std::unique_ptr<TupleExpr> (
132 1943 : new TupleExpr (std::move (values), {}, {}, loc));
133 : }
134 :
135 : std::unique_ptr<Param>
136 3766 : Builder::self_ref_param (bool mutability) const
137 : {
138 3766 : return std::make_unique<SelfParam> (tl::nullopt, mutability, loc);
139 : }
140 :
141 : std::unique_ptr<Param>
142 3227 : Builder::function_param (std::unique_ptr<Pattern> &&pattern,
143 : std::unique_ptr<Type> &&type) const
144 : {
145 3227 : return std::unique_ptr<FunctionParam> (
146 3227 : new FunctionParam (std::move (pattern), std::move (type), {}, loc));
147 : }
148 :
149 : FunctionQualifiers
150 1742 : Builder::fn_qualifiers () const
151 : {
152 1742 : return FunctionQualifiers (loc, Default::No, Async::No, Const::No,
153 1742 : Unsafety::Normal);
154 : }
155 :
156 : std::unique_ptr<Function>
157 3882 : Builder::function (std::string function_name,
158 : std::vector<std::unique_ptr<Param>> params,
159 : std::unique_ptr<Type> return_type,
160 : std::unique_ptr<BlockExpr> block,
161 : std::vector<std::unique_ptr<GenericParam>> generic_params,
162 : FunctionQualifiers qualifiers, WhereClause where_clause,
163 : Visibility visibility) const
164 : {
165 3882 : return std::unique_ptr<Function> (
166 7764 : new Function (function_name, qualifiers, std::move (generic_params),
167 : std::move (params), std::move (return_type), where_clause,
168 15528 : std::move (block), visibility, {}, loc));
169 : }
170 :
171 : PathExprSegment
172 41633 : Builder::path_segment (std::string seg) const
173 : {
174 83266 : return PathExprSegment (PathIdentSegment (seg, loc), loc);
175 : }
176 :
177 : std::unique_ptr<TypePathSegment>
178 37852 : Builder::type_path_segment (std::string seg) const
179 : {
180 37852 : return std::unique_ptr<TypePathSegment> (
181 75704 : new TypePathSegment (seg, false, loc));
182 : }
183 :
184 : std::unique_ptr<TypePathSegment>
185 6689 : Builder::type_path_segment (LangItem::Kind lang_item) const
186 : {
187 6689 : auto &mappings = Analysis::Mappings::get ();
188 6689 : auto name = mappings.get_lang_item_identifier (lang_item);
189 6689 : return std::unique_ptr<TypePathSegment> (
190 13378 : new TypePathSegment (lang_item, PathIdentSegment (name, loc), loc));
191 6689 : }
192 :
193 : std::unique_ptr<TypePathSegment>
194 2514 : Builder::type_path_segment_generic (std::string seg, GenericArgs args) const
195 : {
196 2514 : return std::unique_ptr<TypePathSegment> (
197 5028 : new TypePathSegmentGeneric (PathIdentSegment (seg, loc), false, args, loc));
198 : }
199 :
200 : std::unique_ptr<TypePathSegment>
201 541 : Builder::type_path_segment_generic (LangItem::Kind lang_item,
202 : GenericArgs args) const
203 : {
204 541 : auto &mappings = Analysis::Mappings::get ();
205 541 : auto name = mappings.get_lang_item_identifier (lang_item);
206 541 : return std::unique_ptr<TypePathSegment> (
207 1623 : new TypePathSegmentGeneric (lang_item, PathIdentSegment (name, loc), args,
208 1623 : loc));
209 541 : }
210 :
211 : std::unique_ptr<Type>
212 11238 : Builder::single_type_path (std::string type) const
213 : {
214 11238 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
215 22476 : segments.emplace_back (type_path_segment (type));
216 :
217 11238 : return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
218 11238 : }
219 :
220 : std::unique_ptr<Type>
221 0 : Builder::single_type_path (LangItem::Kind lang_item) const
222 : {
223 0 : return std::unique_ptr<Type> (new TypePath (lang_item, {}, loc));
224 : }
225 :
226 : std::unique_ptr<Type>
227 2266 : Builder::single_generic_type_path (std::string type, GenericArgs args) const
228 : {
229 2266 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
230 4532 : segments.emplace_back (type_path_segment_generic (type, args));
231 :
232 2266 : return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
233 2266 : }
234 :
235 : std::unique_ptr<Type>
236 541 : Builder::single_generic_type_path (LangItem::Kind lang_item,
237 : GenericArgs args) const
238 : {
239 541 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
240 541 : segments.emplace_back (type_path_segment_generic (lang_item, args));
241 :
242 541 : return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
243 541 : }
244 :
245 : TypePath
246 8848 : Builder::type_path (std::vector<std::unique_ptr<TypePathSegment>> &&segments,
247 : bool opening_scope) const
248 : {
249 8848 : return TypePath (std::move (segments), loc, opening_scope);
250 : }
251 :
252 : TypePath
253 8069 : Builder::type_path (std::vector<std::string> &&segments,
254 : bool opening_scope) const
255 : {
256 8069 : auto type_segments = std::vector<std::unique_ptr<TypePathSegment>> ();
257 :
258 32276 : for (auto &&segment : segments)
259 48414 : type_segments.emplace_back (type_path_segment (segment));
260 :
261 8069 : return TypePath (std::move (type_segments), loc, opening_scope);
262 8069 : }
263 :
264 : TypePath
265 8600 : Builder::type_path (std::unique_ptr<TypePathSegment> &&segment) const
266 : {
267 8600 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
268 8600 : segments.emplace_back (std::move (segment));
269 :
270 8600 : return type_path (std::move (segments));
271 8600 : }
272 :
273 : TypePath
274 1911 : Builder::type_path (std::string type) const
275 : {
276 3822 : return type_path (type_path_segment (type));
277 : }
278 :
279 : TypePath
280 6689 : Builder::type_path (LangItem::Kind lang_item) const
281 : {
282 6689 : return type_path (type_path_segment (lang_item));
283 : }
284 :
285 : std::unique_ptr<Type>
286 3227 : Builder::reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
287 : bool mutability) const
288 : {
289 3227 : return std::make_unique<ReferenceType> (mutability, std::move (inner_type),
290 3227 : loc);
291 : }
292 :
293 : PathInExpression
294 11190 : Builder::path_in_expression (std::vector<std::string> &&segments,
295 : bool opening_scope) const
296 : {
297 11190 : auto path_segments = std::vector<PathExprSegment> ();
298 50435 : for (auto &seg : segments)
299 117735 : path_segments.emplace_back (path_segment (seg));
300 :
301 11190 : return PathInExpression (std::move (path_segments), {}, loc, opening_scope);
302 11190 : }
303 :
304 : PathInExpression
305 13293 : Builder::path_in_expression (LangItem::Kind lang_item) const
306 : {
307 13293 : return PathInExpression (lang_item, {}, loc);
308 : }
309 :
310 : PathInExpression
311 1144 : Builder::variant_path (const std::string &enum_path,
312 : const std::string &variant) const
313 : {
314 1144 : return PathInExpression ({path_segment (enum_path), path_segment (variant)},
315 6864 : {}, loc, false);
316 : }
317 :
318 : std::unique_ptr<BlockExpr>
319 2357 : Builder::block (tl::optional<std::unique_ptr<Stmt>> &&stmt,
320 : std::unique_ptr<Expr> &&tail_expr) const
321 : {
322 2357 : auto stmts = std::vector<std::unique_ptr<Stmt>> ();
323 :
324 2357 : if (stmt)
325 883 : stmts.emplace_back (std::move (*stmt));
326 :
327 2357 : return block (std::move (stmts), std::move (tail_expr));
328 2357 : }
329 :
330 : std::unique_ptr<BlockExpr>
331 64 : Builder::block () const
332 : {
333 64 : auto stmts = std::vector<std::unique_ptr<Stmt>> ();
334 :
335 64 : return block (std::move (stmts));
336 64 : }
337 :
338 : std::unique_ptr<BlockExpr>
339 1474 : Builder::block (std::unique_ptr<Expr> &&tail_expr) const
340 : {
341 1474 : return block (tl::nullopt, std::move (tail_expr));
342 : }
343 :
344 : std::unique_ptr<BlockExpr>
345 5528 : Builder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
346 : std::unique_ptr<Expr> &&tail_expr) const
347 : {
348 5528 : return std::unique_ptr<BlockExpr> (new BlockExpr (std::move (stmts),
349 : std::move (tail_expr), {},
350 5528 : {}, tl::nullopt, loc, loc));
351 : }
352 :
353 : std::unique_ptr<Expr>
354 1592 : Builder::return_expr (std::unique_ptr<Expr> &&to_return)
355 : {
356 1592 : return std::unique_ptr<Expr> (
357 3184 : new ReturnExpr (std::move (to_return), {}, loc));
358 : }
359 :
360 : std::unique_ptr<Stmt>
361 3946 : Builder::let (std::unique_ptr<Pattern> &&pattern, std::unique_ptr<Type> &&type,
362 : std::unique_ptr<Expr> &&init) const
363 : {
364 3946 : return std::unique_ptr<Stmt> (new LetStmt (std::move (pattern),
365 : std::move (init), std::move (type),
366 3946 : tl::nullopt, {}, loc));
367 : }
368 :
369 : std::unique_ptr<Expr>
370 10028 : Builder::ref (std::unique_ptr<Expr> &&of, bool mut) const
371 : {
372 10028 : auto mutability = mut ? Mutability::Mut : Mutability::Imm;
373 10028 : return std::unique_ptr<Expr> (
374 : new BorrowExpr (std::move (of), mutability,
375 10028 : /* raw */ false, /* is double */ false, {}, loc));
376 : }
377 :
378 : std::unique_ptr<Expr>
379 2 : Builder::deref (std::unique_ptr<Expr> &&of) const
380 : {
381 2 : return std::unique_ptr<Expr> (new DereferenceExpr (std::move (of), {}, loc));
382 : }
383 :
384 : std::unique_ptr<Expr>
385 3779 : Builder::comparison_expr (std::unique_ptr<Expr> &&lhs,
386 : std::unique_ptr<Expr> &&rhs,
387 : ComparisonOperator op) const
388 : {
389 3779 : return std::make_unique<ComparisonExpr> (std::move (lhs), std::move (rhs), op,
390 3779 : loc);
391 : }
392 :
393 : std::unique_ptr<Expr>
394 2675 : Builder::boolean_operation (std::unique_ptr<Expr> &&lhs,
395 : std::unique_ptr<Expr> &&rhs,
396 : LazyBooleanOperator op) const
397 : {
398 2675 : return std::make_unique<LazyBooleanExpr> (std::move (lhs), std::move (rhs),
399 2675 : op, loc);
400 : }
401 :
402 : std::unique_ptr<Stmt>
403 541 : Builder::struct_struct (std::string struct_name,
404 : std::vector<std::unique_ptr<GenericParam>> &&generics,
405 : std::vector<StructField> &&fields)
406 : {
407 541 : auto is_unit = fields.empty ();
408 :
409 541 : return std::unique_ptr<Stmt> (
410 1623 : new StructStruct (std::move (fields), struct_name, std::move (generics),
411 1082 : WhereClause::create_empty (), is_unit,
412 2705 : Visibility::create_private (), {}, loc));
413 : }
414 :
415 : std::unique_ptr<Expr>
416 214 : Builder::struct_expr_struct (std::string struct_name) const
417 : {
418 214 : return std::unique_ptr<Expr> (
419 642 : new StructExprStruct (path_in_expression ({struct_name}), {}, {}, loc));
420 : }
421 :
422 : std::unique_ptr<Expr>
423 757 : Builder::struct_expr (
424 : std::string struct_name,
425 : std::vector<std::unique_ptr<StructExprField>> &&fields) const
426 : {
427 2271 : return struct_expr (path_in_expression ({struct_name}), std::move (fields));
428 : }
429 :
430 : std::unique_ptr<Expr>
431 757 : Builder::struct_expr (
432 : PathInExpression struct_name,
433 : std::vector<std::unique_ptr<StructExprField>> &&fields) const
434 : {
435 757 : return std::unique_ptr<Expr> (
436 757 : new StructExprStructFields (struct_name, std::move (fields), loc));
437 : }
438 :
439 : std::unique_ptr<StructExprField>
440 1491 : Builder::struct_expr_field (std::string field_name,
441 : std::unique_ptr<Expr> &&value) const
442 : {
443 1491 : return std::unique_ptr<StructExprField> (
444 4473 : new StructExprFieldIdentifierValue (field_name, std::move (value), loc));
445 : }
446 :
447 : std::unique_ptr<Expr>
448 3097 : Builder::field_access (std::unique_ptr<Expr> &&instance,
449 : std::string field) const
450 : {
451 3097 : return std::unique_ptr<Expr> (
452 9291 : new FieldAccessExpr (std::move (instance), field, {}, loc));
453 : }
454 :
455 : std::unique_ptr<StructPatternField>
456 96 : Builder::struct_pattern_ident_pattern (std::string field_name,
457 : std::unique_ptr<Pattern> &&pattern)
458 : {
459 96 : return std::make_unique<StructPatternFieldIdentPat> (
460 96 : field_name, std::move (pattern), std::vector<Attribute> (), loc);
461 : }
462 :
463 : std::unique_ptr<Pattern>
464 1235 : Builder::wildcard () const
465 : {
466 1235 : return std::unique_ptr<Pattern> (new WildcardPattern (loc));
467 : }
468 :
469 : std::unique_ptr<Pattern>
470 0 : Builder::ref_pattern (std::unique_ptr<Pattern> &&inner) const
471 : {
472 0 : return std::make_unique<ReferencePattern> (std::move (inner), false, false,
473 0 : loc);
474 : }
475 :
476 : std::unique_ptr<Path>
477 0 : Builder::lang_item_path (LangItem::Kind kind) const
478 : {
479 0 : return std::unique_ptr<Path> (new PathInExpression (kind, {}, loc));
480 : }
481 :
482 : std::unique_ptr<Expr>
483 2678 : Builder::match (std::unique_ptr<Expr> &&scrutinee,
484 : std::vector<MatchCase> &&cases)
485 : {
486 2678 : return std::unique_ptr<Expr> (
487 2678 : new MatchExpr (std::move (scrutinee), std::move (cases), {}, {}, loc));
488 : }
489 :
490 : MatchArm
491 4807 : Builder::match_arm (std::unique_ptr<Pattern> &&pattern)
492 : {
493 4807 : return MatchArm (std::move (pattern), loc);
494 : }
495 :
496 : MatchCase
497 2261 : Builder::match_case (std::unique_ptr<Pattern> &&pattern,
498 : std::unique_ptr<Expr> &&expr)
499 : {
500 2261 : return match_case (match_arm (std::move (pattern)), std::move (expr));
501 : }
502 :
503 : MatchCase
504 2625 : Builder::match_case (MatchArm &&arm, std::unique_ptr<Expr> &&expr)
505 : {
506 2625 : return MatchCase (std::move (arm), std::move (expr));
507 : }
508 : std::unique_ptr<Expr>
509 1091 : Builder::loop (std::vector<std::unique_ptr<Stmt>> &&stmts)
510 : {
511 1091 : auto block_expr = block (std::move (stmts), nullptr);
512 :
513 1091 : return std::unique_ptr<Expr> (new LoopExpr (std::move (block_expr), loc));
514 1091 : }
515 :
516 : std::unique_ptr<TypeParamBound>
517 2061 : Builder::trait_bound (TypePath bound)
518 : {
519 2061 : return std::make_unique<TraitBound> (std::move (bound), loc);
520 : }
521 :
522 : std::unique_ptr<Item>
523 8156 : Builder::trait_impl (TypePath trait_path, std::unique_ptr<Type> target,
524 : std::vector<std::unique_ptr<AssociatedItem>> trait_items,
525 : std::vector<std::unique_ptr<GenericParam>> generics,
526 : WhereClause where_clause, Visibility visibility) const
527 : {
528 8156 : return std::unique_ptr<Item> (
529 : new TraitImpl (std::move (trait_path), /* unsafe */ false,
530 : /* exclam */ false, std::move (trait_items),
531 : std::move (generics), std::move (target), where_clause,
532 8156 : visibility, {}, {}, loc));
533 : }
534 :
535 : std::unique_ptr<GenericParam>
536 292 : Builder::generic_type_param (
537 : std::string type_representation,
538 : std::vector<std::unique_ptr<TypeParamBound>> &&bounds,
539 : std::unique_ptr<Type> &&type)
540 : {
541 292 : return std::make_unique<TypeParam> (type_representation, loc,
542 : std::move (bounds), std::move (type),
543 292 : std::vector<Attribute> ());
544 : }
545 :
546 : std::unique_ptr<Stmt>
547 604 : Builder::discriminant_value (std::string binding_name, std::string instance)
548 : {
549 604 : auto intrinsic = ptrify (
550 3020 : path_in_expression ({get_path_start (), "intrinsics", "discriminant_value"},
551 604 : has_opening_scope ()));
552 :
553 2416 : return let (identifier_pattern (binding_name), nullptr,
554 2416 : call (std::move (intrinsic), identifier (instance)));
555 604 : }
556 :
557 : std::unique_ptr<GenericParam>
558 781 : Builder::new_lifetime_param (LifetimeParam ¶m)
559 : {
560 781 : Lifetime l = new_lifetime (param.get_lifetime ());
561 :
562 781 : std::vector<Lifetime> lifetime_bounds;
563 781 : lifetime_bounds.reserve (param.get_lifetime_bounds ().size ());
564 :
565 789 : for (auto b : param.get_lifetime_bounds ())
566 8 : lifetime_bounds.emplace_back (new_lifetime (b));
567 :
568 781 : auto p = new LifetimeParam (l, std::move (lifetime_bounds),
569 781 : param.get_outer_attrs (), param.get_locus ());
570 781 : return std::unique_ptr<GenericParam> (p);
571 781 : }
572 :
573 : std::unique_ptr<GenericParam>
574 42 : Builder::new_const_param (ConstGenericParam ¶m) const
575 : {
576 84 : return std::make_unique<ConstGenericParam> (param.get_name (),
577 84 : param.get_type ().reconstruct (),
578 : param.get_default_value (),
579 42 : param.get_outer_attrs (),
580 84 : param.get_locus ());
581 : }
582 :
583 : std::unique_ptr<GenericParam>
584 2055 : Builder::new_type_param (
585 : TypeParam ¶m, std::vector<std::unique_ptr<TypeParamBound>> extra_bounds,
586 : DefaultParamGen default_param_generation)
587 : {
588 2055 : location_t locus = param.get_locus ();
589 2055 : AST::AttrVec outer_attrs = param.get_outer_attrs ();
590 2055 : Identifier type_representation = param.get_type_representation ();
591 2055 : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
592 2055 : std::unique_ptr<Type> type = nullptr;
593 :
594 2055 : if (default_param_generation == DefaultParamGen::Keep && param.has_type ())
595 0 : type = param.get_type ().reconstruct ();
596 :
597 3824 : for (auto &&extra_bound : extra_bounds)
598 1769 : type_param_bounds.emplace_back (std::move (extra_bound));
599 :
600 2362 : for (const auto &b : param.get_type_param_bounds ())
601 : {
602 307 : switch (b->get_bound_type ())
603 : {
604 148 : case TypeParamBound::TypeParamBoundType::TRAIT:
605 148 : {
606 148 : const TraitBound &tb = (const TraitBound &) *b.get ();
607 148 : const TypePath &path = tb.get_type_path ();
608 :
609 148 : std::vector<LifetimeParam> for_lifetimes;
610 148 : for (const auto &lifetime : tb.get_for_lifetimes ())
611 : {
612 0 : std::vector<Lifetime> lifetime_bounds;
613 0 : lifetime_bounds.reserve (
614 0 : lifetime.get_lifetime_bounds ().size ());
615 :
616 0 : for (const auto &b : lifetime.get_lifetime_bounds ())
617 0 : lifetime_bounds.emplace_back (new_lifetime (b));
618 :
619 0 : Lifetime nl = new_lifetime (lifetime.get_lifetime ());
620 0 : LifetimeParam p (std::move (nl), std::move (lifetime_bounds),
621 0 : {}, lifetime.get_locus ());
622 0 : for_lifetimes.push_back (std::move (p));
623 0 : }
624 :
625 148 : std::vector<std::unique_ptr<TypePathSegment>> segments;
626 297 : for (auto &seg : path.get_segments ())
627 : {
628 149 : switch (seg->get_type ())
629 : {
630 148 : case TypePathSegment::REG:
631 148 : {
632 148 : const TypePathSegment &segment
633 148 : = (const TypePathSegment &) (*seg.get ());
634 :
635 148 : segments.emplace_back (new TypePathSegment (
636 : segment.get_ident_segment (),
637 : segment.get_separating_scope_resolution (),
638 148 : segment.get_locus ()));
639 : }
640 148 : break;
641 :
642 0 : case TypePathSegment::GENERIC:
643 0 : {
644 0 : TypePathSegmentGeneric &generic
645 0 : = (TypePathSegmentGeneric &) (*seg.get ());
646 :
647 0 : GenericArgs args
648 0 : = new_generic_args (generic.get_generic_args ());
649 :
650 0 : segments.emplace_back (new TypePathSegmentGeneric (
651 0 : generic.get_ident_segment (), false, std::move (args),
652 0 : generic.get_locus ()));
653 0 : }
654 0 : break;
655 :
656 1 : case TypePathSegment::FUNCTION:
657 1 : segments.emplace_back (seg->reconstruct ());
658 1 : break;
659 : }
660 : }
661 :
662 148 : TypePath p (std::move (segments), path.get_locus (),
663 148 : path.has_opening_scope_resolution ());
664 :
665 148 : type_param_bounds.emplace_back (new TraitBound (
666 : std::move (p), tb.get_locus (), tb.is_in_parens (),
667 296 : tb.has_opening_question_mark (), std::move (for_lifetimes)));
668 148 : }
669 148 : break;
670 :
671 159 : case TypeParamBound::TypeParamBoundType::LIFETIME:
672 159 : {
673 159 : const Lifetime &l = (const Lifetime &) *b.get ();
674 :
675 159 : type_param_bounds.emplace_back (
676 159 : new Lifetime (l.get_lifetime_type (), l.get_lifetime_name (),
677 318 : l.get_locus ()));
678 : }
679 159 : break;
680 : }
681 : }
682 :
683 2055 : auto type_param
684 : = new TypeParam (type_representation, locus, std::move (type_param_bounds),
685 2055 : std::move (type), std::move (outer_attrs));
686 :
687 2055 : return std::unique_ptr<GenericParam> (type_param);
688 2055 : }
689 :
690 : Lifetime
691 1570 : Builder::new_lifetime (const Lifetime &lifetime)
692 : {
693 3140 : return Lifetime (lifetime.get_lifetime_type (), lifetime.get_lifetime_name (),
694 3140 : lifetime.get_locus ());
695 : }
696 :
697 : GenericArgs
698 0 : Builder::new_generic_args (GenericArgs &args)
699 : {
700 0 : std::vector<Lifetime> lifetime_args;
701 0 : std::vector<GenericArg> generic_args;
702 0 : std::vector<GenericArgsBinding> binding_args;
703 0 : location_t locus = args.get_locus ();
704 :
705 0 : for (const auto &lifetime : args.get_lifetime_args ())
706 0 : lifetime_args.push_back (new_lifetime (lifetime));
707 :
708 0 : for (auto &binding : args.get_binding_args ())
709 : {
710 0 : Type &t = *binding.get_type_ptr ().get ();
711 0 : std::unique_ptr<Type> ty = t.reconstruct ();
712 0 : binding_args.emplace_back (binding.get_identifier (), std::move (ty),
713 0 : binding.get_locus ());
714 0 : }
715 :
716 0 : for (auto &arg : args.get_generic_args ())
717 : {
718 0 : tl::optional<GenericArg> new_arg = tl::nullopt;
719 :
720 0 : switch (arg.get_kind ())
721 : {
722 0 : case GenericArg::Kind::Type:
723 0 : new_arg = GenericArg::create_type (arg.get_type ().reconstruct ());
724 0 : break;
725 0 : case GenericArg::Kind::Either:
726 0 : new_arg
727 0 : = GenericArg::create_ambiguous (arg.get_path (), arg.get_locus ());
728 0 : break;
729 0 : case GenericArg::Kind::Const:
730 0 : new_arg
731 0 : = GenericArg::create_const (arg.get_expression ().clone_expr ());
732 : // FIXME: Use `reconstruct()` here, not `clone_expr()`
733 0 : break;
734 : }
735 :
736 0 : generic_args.emplace_back (*new_arg);
737 0 : }
738 :
739 0 : return GenericArgs (std::move (lifetime_args), std::move (generic_args),
740 0 : std::move (binding_args), locus);
741 0 : }
742 :
743 : std::unique_ptr<Expr>
744 5132 : Builder::qualified_call (std::vector<std::string> &&segments,
745 : bool has_opening_scope,
746 : std::vector<std::unique_ptr<Expr>> &&args) const
747 : {
748 5132 : auto path = std::unique_ptr<Expr> (new PathInExpression (
749 5132 : path_in_expression (std::move (segments), has_opening_scope)));
750 :
751 5132 : return call (std::move (path), std::move (args));
752 5132 : }
753 :
754 : } // namespace AST
755 : } // namespace Rust
|