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 636 : Builder::statementify (std::unique_ptr<Expr> &&value,
37 : bool semicolon_followed) const
38 : {
39 636 : return std::make_unique<ExprStmt> (std::move (value), loc,
40 636 : semicolon_followed);
41 : }
42 :
43 : std::unique_ptr<Expr>
44 166 : Builder::literal_string (std::string &&content) const
45 : {
46 166 : return std::unique_ptr<Expr> (
47 : new AST::LiteralExpr (std::move (content), Literal::LitType::STRING,
48 166 : PrimitiveCoreType::CORETYPE_STR, {}, loc));
49 : }
50 :
51 : std::unique_ptr<Expr>
52 25 : Builder::literal_bool (bool b) const
53 : {
54 50 : auto str
55 25 : = b ? Values::Keywords::TRUE_LITERAL : Values::Keywords::FALSE_LITERAL;
56 :
57 25 : return std::unique_ptr<Expr> (
58 50 : new AST::LiteralExpr (std::move (str), Literal::LitType::BOOL,
59 25 : PrimitiveCoreType::CORETYPE_BOOL, {}, loc));
60 : }
61 :
62 : std::unique_ptr<Expr>
63 3037 : Builder::call (std::unique_ptr<Expr> &&path,
64 : std::vector<std::unique_ptr<Expr>> &&args) const
65 : {
66 3037 : return std::unique_ptr<Expr> (
67 3037 : new CallExpr (std::move (path), std::move (args), {}, loc));
68 : }
69 :
70 : std::unique_ptr<Expr>
71 1515 : Builder::call (std::unique_ptr<Expr> &&path, std::unique_ptr<Expr> &&arg) const
72 : {
73 1515 : auto args = std::vector<std::unique_ptr<Expr>> ();
74 1515 : args.emplace_back (std::move (arg));
75 :
76 1515 : return call (std::move (path), std::move (args));
77 1515 : }
78 :
79 : std::unique_ptr<Expr>
80 152 : Builder::array (std::vector<std::unique_ptr<Expr>> &&members) const
81 : {
82 152 : auto elts = std::make_unique<ArrayElemsValues> (std::move (members), loc);
83 :
84 152 : return std::unique_ptr<Expr> (new ArrayExpr (std::move (elts), {}, {}, loc));
85 152 : }
86 :
87 : std::unique_ptr<Expr>
88 30 : Builder::qualified_path_in_expression (std::unique_ptr<Type> &&type,
89 : TypePath trait,
90 : PathExprSegment segment) const
91 : {
92 90 : auto segments = {segment};
93 :
94 30 : return qualified_path_in_expression (std::move (type), trait, segments);
95 60 : }
96 :
97 : std::unique_ptr<Expr>
98 30 : Builder::qualified_path_in_expression (
99 : std::unique_ptr<Type> &&type, TypePath trait,
100 : std::vector<PathExprSegment> &&segments) const
101 : {
102 30 : auto qual_type = QualifiedPathType (std::move (type), loc, trait);
103 :
104 30 : return std::unique_ptr<QualifiedPathInExpression> (
105 30 : new QualifiedPathInExpression (qual_type, std::move (segments), {}, loc));
106 30 : }
107 :
108 : std::unique_ptr<Expr>
109 4733 : Builder::identifier (std::string name) const
110 : {
111 14199 : return std::unique_ptr<Expr> (new IdentifierExpr (name, {}, loc));
112 : }
113 :
114 : std::unique_ptr<Pattern>
115 2553 : Builder::identifier_pattern (std::string name, bool mut) const
116 : {
117 2553 : return std::unique_ptr<Pattern> (
118 7659 : new IdentifierPattern (name, loc, false, mut));
119 : }
120 :
121 : std::unique_ptr<Expr>
122 1258 : Builder::tuple_idx (std::string receiver, int idx) const
123 : {
124 1258 : return std::unique_ptr<Expr> (
125 2516 : new TupleIndexExpr (identifier (receiver), idx, {}, loc));
126 : }
127 :
128 : std::unique_ptr<Expr>
129 347 : Builder::tuple (std::vector<std::unique_ptr<Expr>> &&values) const
130 : {
131 347 : return std::unique_ptr<TupleExpr> (
132 347 : new TupleExpr (std::move (values), {}, {}, loc));
133 : }
134 :
135 : std::unique_ptr<Param>
136 798 : Builder::self_ref_param (bool mutability) const
137 : {
138 798 : return std::make_unique<SelfParam> (tl::nullopt, mutability, loc);
139 : }
140 :
141 : std::unique_ptr<Param>
142 665 : Builder::function_param (std::unique_ptr<Pattern> &&pattern,
143 : std::unique_ptr<Type> &&type) const
144 : {
145 665 : return std::unique_ptr<FunctionParam> (
146 665 : new FunctionParam (std::move (pattern), std::move (type), {}, loc));
147 : }
148 :
149 : FunctionQualifiers
150 314 : Builder::fn_qualifiers () const
151 : {
152 314 : return FunctionQualifiers (loc, Default::No, Async::No, Const::No,
153 314 : Unsafety::Normal);
154 : }
155 :
156 : std::unique_ptr<Function>
157 830 : 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 830 : return std::unique_ptr<Function> (
166 1660 : new Function (function_name, qualifiers, std::move (generic_params),
167 : std::move (params), std::move (return_type), where_clause,
168 3320 : std::move (block), visibility, {}, loc));
169 : }
170 :
171 : PathExprSegment
172 7704 : Builder::path_segment (std::string seg) const
173 : {
174 15408 : return PathExprSegment (PathIdentSegment (seg, loc), loc);
175 : }
176 :
177 : std::unique_ptr<TypePathSegment>
178 7736 : Builder::type_path_segment (std::string seg) const
179 : {
180 7736 : return std::unique_ptr<TypePathSegment> (
181 15472 : new TypePathSegment (seg, false, loc));
182 : }
183 :
184 : std::unique_ptr<TypePathSegment>
185 1387 : Builder::type_path_segment (LangItem::Kind lang_item) const
186 : {
187 1387 : auto &mappings = Analysis::Mappings::get ();
188 1387 : auto name = mappings.get_lang_item_identifier (lang_item);
189 1387 : return std::unique_ptr<TypePathSegment> (
190 2774 : new TypePathSegment (lang_item, PathIdentSegment (name, loc), loc));
191 1387 : }
192 :
193 : std::unique_ptr<TypePathSegment>
194 511 : Builder::type_path_segment_generic (std::string seg, GenericArgs args) const
195 : {
196 511 : return std::unique_ptr<TypePathSegment> (
197 1022 : new TypePathSegmentGeneric (PathIdentSegment (seg, loc), false, args, loc));
198 : }
199 :
200 : std::unique_ptr<TypePathSegment>
201 135 : Builder::type_path_segment_generic (LangItem::Kind lang_item,
202 : GenericArgs args) const
203 : {
204 135 : auto &mappings = Analysis::Mappings::get ();
205 135 : auto name = mappings.get_lang_item_identifier (lang_item);
206 135 : return std::unique_ptr<TypePathSegment> (
207 405 : new TypePathSegmentGeneric (lang_item, PathIdentSegment (name, loc), args,
208 405 : loc));
209 135 : }
210 :
211 : std::unique_ptr<Type>
212 2339 : Builder::single_type_path (std::string type) const
213 : {
214 2339 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
215 4678 : segments.emplace_back (type_path_segment (type));
216 :
217 2339 : return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
218 2339 : }
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 438 : Builder::single_generic_type_path (std::string type, GenericArgs args) const
228 : {
229 438 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
230 876 : segments.emplace_back (type_path_segment_generic (type, args));
231 :
232 438 : return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
233 438 : }
234 :
235 : std::unique_ptr<Type>
236 135 : Builder::single_generic_type_path (LangItem::Kind lang_item,
237 : GenericArgs args) const
238 : {
239 135 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
240 135 : segments.emplace_back (type_path_segment_generic (lang_item, args));
241 :
242 135 : return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
243 135 : }
244 :
245 : TypePath
246 1929 : Builder::type_path (std::vector<std::unique_ptr<TypePathSegment>> &&segments,
247 : bool opening_scope) const
248 : {
249 1929 : return TypePath (std::move (segments), loc, opening_scope);
250 : }
251 :
252 : TypePath
253 1594 : Builder::type_path (std::vector<std::string> &&segments,
254 : bool opening_scope) const
255 : {
256 1594 : auto type_segments = std::vector<std::unique_ptr<TypePathSegment>> ();
257 :
258 6376 : for (auto &&segment : segments)
259 9564 : type_segments.emplace_back (type_path_segment (segment));
260 :
261 1594 : return TypePath (std::move (type_segments), loc, opening_scope);
262 1594 : }
263 :
264 : TypePath
265 1856 : Builder::type_path (std::unique_ptr<TypePathSegment> &&segment) const
266 : {
267 1856 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
268 1856 : segments.emplace_back (std::move (segment));
269 :
270 1856 : return type_path (std::move (segments));
271 1856 : }
272 :
273 : TypePath
274 469 : Builder::type_path (std::string type) const
275 : {
276 938 : return type_path (type_path_segment (type));
277 : }
278 :
279 : TypePath
280 1387 : Builder::type_path (LangItem::Kind lang_item) const
281 : {
282 1387 : return type_path (type_path_segment (lang_item));
283 : }
284 :
285 : std::unique_ptr<Type>
286 665 : Builder::reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
287 : bool mutability) const
288 : {
289 665 : return std::make_unique<ReferenceType> (mutability, std::move (inner_type),
290 665 : loc);
291 : }
292 :
293 : PathInExpression
294 2041 : Builder::path_in_expression (std::vector<std::string> &&segments,
295 : bool opening_scope) const
296 : {
297 2041 : auto path_segments = std::vector<PathExprSegment> ();
298 9135 : for (auto &seg : segments)
299 21282 : path_segments.emplace_back (path_segment (seg));
300 :
301 2041 : return PathInExpression (std::move (path_segments), {}, loc, opening_scope);
302 2041 : }
303 :
304 : PathInExpression
305 2205 : Builder::path_in_expression (LangItem::Kind lang_item) const
306 : {
307 2205 : return PathInExpression (lang_item, {}, loc);
308 : }
309 :
310 : PathInExpression
311 290 : Builder::variant_path (const std::string &enum_path,
312 : const std::string &variant) const
313 : {
314 290 : return PathInExpression ({path_segment (enum_path), path_segment (variant)},
315 1740 : {}, loc, false);
316 : }
317 :
318 : std::unique_ptr<BlockExpr>
319 495 : Builder::block (tl::optional<std::unique_ptr<Stmt>> &&stmt,
320 : std::unique_ptr<Expr> &&tail_expr) const
321 : {
322 495 : auto stmts = std::vector<std::unique_ptr<Stmt>> ();
323 :
324 495 : if (stmt)
325 169 : stmts.emplace_back (std::move (*stmt));
326 :
327 495 : return block (std::move (stmts), std::move (tail_expr));
328 495 : }
329 :
330 : std::unique_ptr<BlockExpr>
331 15 : Builder::block () const
332 : {
333 15 : auto stmts = std::vector<std::unique_ptr<Stmt>> ();
334 :
335 15 : return block (std::move (stmts));
336 15 : }
337 :
338 : std::unique_ptr<BlockExpr>
339 326 : Builder::block (std::unique_ptr<Expr> &&tail_expr) const
340 : {
341 326 : return block (tl::nullopt, std::move (tail_expr));
342 : }
343 :
344 : std::unique_ptr<BlockExpr>
345 1062 : Builder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
346 : std::unique_ptr<Expr> &&tail_expr) const
347 : {
348 1062 : return std::unique_ptr<BlockExpr> (new BlockExpr (std::move (stmts),
349 : std::move (tail_expr), {},
350 1062 : {}, tl::nullopt, loc, loc));
351 : }
352 :
353 : std::unique_ptr<Expr>
354 241 : Builder::return_expr (std::unique_ptr<Expr> &&to_return)
355 : {
356 241 : return std::unique_ptr<Expr> (
357 482 : new ReturnExpr (std::move (to_return), {}, loc));
358 : }
359 :
360 : std::unique_ptr<Stmt>
361 887 : Builder::let (std::unique_ptr<Pattern> &&pattern, std::unique_ptr<Type> &&type,
362 : std::unique_ptr<Expr> &&init) const
363 : {
364 887 : return std::unique_ptr<Stmt> (new LetStmt (std::move (pattern),
365 : std::move (init), std::move (type),
366 887 : tl::nullopt, {}, loc));
367 : }
368 :
369 : std::unique_ptr<Expr>
370 1866 : Builder::ref (std::unique_ptr<Expr> &&of, bool mut) const
371 : {
372 1866 : auto mutability = mut ? Mutability::Mut : Mutability::Imm;
373 1866 : return std::unique_ptr<Expr> (
374 : new BorrowExpr (std::move (of), mutability,
375 1866 : /* 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 685 : Builder::comparison_expr (std::unique_ptr<Expr> &&lhs,
386 : std::unique_ptr<Expr> &&rhs,
387 : ComparisonOperator op) const
388 : {
389 685 : return std::make_unique<ComparisonExpr> (std::move (lhs), std::move (rhs), op,
390 685 : loc);
391 : }
392 :
393 : std::unique_ptr<Expr>
394 407 : Builder::boolean_operation (std::unique_ptr<Expr> &&lhs,
395 : std::unique_ptr<Expr> &&rhs,
396 : LazyBooleanOperator op) const
397 : {
398 407 : return std::make_unique<LazyBooleanExpr> (std::move (lhs), std::move (rhs),
399 407 : op, loc);
400 : }
401 :
402 : std::unique_ptr<Stmt>
403 135 : Builder::struct_struct (std::string struct_name,
404 : std::vector<std::unique_ptr<GenericParam>> &&generics,
405 : std::vector<StructField> &&fields)
406 : {
407 135 : auto is_unit = fields.empty ();
408 :
409 135 : return std::unique_ptr<Stmt> (
410 405 : new StructStruct (std::move (fields), struct_name, std::move (generics),
411 270 : WhereClause::create_empty (), is_unit,
412 675 : Visibility::create_private (), {}, loc));
413 : }
414 :
415 : std::unique_ptr<Expr>
416 46 : Builder::struct_expr_struct (std::string struct_name) const
417 : {
418 46 : return std::unique_ptr<Expr> (
419 138 : new StructExprStruct (path_in_expression ({struct_name}), {}, {}, loc));
420 : }
421 :
422 : std::unique_ptr<Expr>
423 155 : Builder::struct_expr (
424 : std::string struct_name,
425 : std::vector<std::unique_ptr<StructExprField>> &&fields) const
426 : {
427 465 : return struct_expr (path_in_expression ({struct_name}), std::move (fields));
428 : }
429 :
430 : std::unique_ptr<Expr>
431 155 : Builder::struct_expr (
432 : PathInExpression struct_name,
433 : std::vector<std::unique_ptr<StructExprField>> &&fields) const
434 : {
435 155 : return std::unique_ptr<Expr> (
436 155 : new StructExprStructFields (struct_name, std::move (fields), loc));
437 : }
438 :
439 : std::unique_ptr<StructExprField>
440 280 : Builder::struct_expr_field (std::string field_name,
441 : std::unique_ptr<Expr> &&value) const
442 : {
443 280 : return std::unique_ptr<StructExprField> (
444 840 : new StructExprFieldIdentifierValue (field_name, std::move (value), loc));
445 : }
446 :
447 : std::unique_ptr<Expr>
448 892 : Builder::field_access (std::unique_ptr<Expr> &&instance,
449 : std::string field) const
450 : {
451 892 : return std::unique_ptr<Expr> (
452 2676 : 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 339 : Builder::wildcard () const
465 : {
466 339 : 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 627 : Builder::match (std::unique_ptr<Expr> &&scrutinee,
484 : std::vector<MatchCase> &&cases)
485 : {
486 627 : return std::unique_ptr<Expr> (
487 627 : new MatchExpr (std::move (scrutinee), std::move (cases), {}, {}, loc));
488 : }
489 :
490 : MatchArm
491 1167 : Builder::match_arm (std::unique_ptr<Pattern> &&pattern)
492 : {
493 1167 : return MatchArm (std::move (pattern), loc);
494 : }
495 :
496 : MatchCase
497 539 : Builder::match_case (std::unique_ptr<Pattern> &&pattern,
498 : std::unique_ptr<Expr> &&expr)
499 : {
500 539 : return match_case (match_arm (std::move (pattern)), std::move (expr));
501 : }
502 :
503 : MatchCase
504 735 : Builder::match_case (MatchArm &&arm, std::unique_ptr<Expr> &&expr)
505 : {
506 735 : return MatchCase (std::move (arm), std::move (expr));
507 : }
508 : std::unique_ptr<Expr>
509 216 : Builder::loop (std::vector<std::unique_ptr<Stmt>> &&stmts)
510 : {
511 216 : auto block_expr = block (std::move (stmts), nullptr);
512 :
513 216 : return std::unique_ptr<Expr> (new LoopExpr (std::move (block_expr), loc));
514 216 : }
515 :
516 : std::unique_ptr<TypeParamBound>
517 400 : Builder::trait_bound (TypePath bound)
518 : {
519 400 : return std::make_unique<TraitBound> (std::move (bound), loc);
520 : }
521 :
522 : std::unique_ptr<Item>
523 1680 : 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 1680 : 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 1680 : visibility, {}, {}, loc));
533 : }
534 :
535 : std::unique_ptr<GenericParam>
536 54 : 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 54 : return std::make_unique<TypeParam> (type_representation, loc,
542 : std::move (bounds), std::move (type),
543 54 : std::vector<Attribute> ());
544 : }
545 :
546 : std::unique_ptr<Stmt>
547 191 : Builder::discriminant_value (std::string binding_name, std::string instance)
548 : {
549 191 : auto intrinsic = ptrify (
550 955 : path_in_expression ({get_path_start (), "intrinsics", "discriminant_value"},
551 191 : true));
552 :
553 764 : return let (identifier_pattern (binding_name), nullptr,
554 764 : call (std::move (intrinsic), identifier (instance)));
555 191 : }
556 :
557 : std::unique_ptr<GenericParam>
558 130 : Builder::new_lifetime_param (LifetimeParam ¶m)
559 : {
560 130 : Lifetime l = new_lifetime (param.get_lifetime ());
561 :
562 130 : std::vector<Lifetime> lifetime_bounds;
563 130 : lifetime_bounds.reserve (param.get_lifetime_bounds ().size ());
564 :
565 131 : for (auto b : param.get_lifetime_bounds ())
566 1 : lifetime_bounds.emplace_back (new_lifetime (b));
567 :
568 130 : auto p = new LifetimeParam (l, std::move (lifetime_bounds),
569 130 : param.get_outer_attrs (), param.get_locus ());
570 130 : return std::unique_ptr<GenericParam> (p);
571 130 : }
572 :
573 : std::unique_ptr<GenericParam>
574 7 : Builder::new_const_param (ConstGenericParam ¶m) const
575 : {
576 14 : return std::make_unique<ConstGenericParam> (param.get_name (),
577 14 : param.get_type ().reconstruct (),
578 : param.get_default_value (),
579 7 : param.get_outer_attrs (),
580 14 : param.get_locus ());
581 : }
582 :
583 : std::unique_ptr<GenericParam>
584 408 : Builder::new_type_param (
585 : TypeParam ¶m, std::vector<std::unique_ptr<TypeParamBound>> extra_bounds,
586 : DefaultParamGen default_param_generation)
587 : {
588 408 : location_t locus = param.get_locus ();
589 408 : AST::AttrVec outer_attrs = param.get_outer_attrs ();
590 408 : Identifier type_representation = param.get_type_representation ();
591 408 : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
592 408 : std::unique_ptr<Type> type = nullptr;
593 :
594 408 : if (default_param_generation == DefaultParamGen::Keep && param.has_type ())
595 0 : type = param.get_type ().reconstruct ();
596 :
597 754 : for (auto &&extra_bound : extra_bounds)
598 346 : type_param_bounds.emplace_back (std::move (extra_bound));
599 :
600 469 : for (const auto &b : param.get_type_param_bounds ())
601 : {
602 61 : switch (b->get_bound_type ())
603 : {
604 35 : case TypeParamBound::TypeParamBoundType::TRAIT:
605 35 : {
606 35 : const TraitBound &tb = (const TraitBound &) *b.get ();
607 35 : const TypePath &path = tb.get_type_path ();
608 :
609 35 : std::vector<LifetimeParam> for_lifetimes;
610 35 : 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 35 : std::vector<std::unique_ptr<TypePathSegment>> segments;
626 71 : for (auto &seg : path.get_segments ())
627 : {
628 36 : switch (seg->get_type ())
629 : {
630 36 : case TypePathSegment::REG:
631 36 : {
632 36 : const TypePathSegment &segment
633 36 : = (const TypePathSegment &) (*seg.get ());
634 :
635 36 : segments.emplace_back (new TypePathSegment (
636 : segment.get_ident_segment (),
637 : segment.get_separating_scope_resolution (),
638 36 : segment.get_locus ()));
639 : }
640 36 : 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 0 : case TypePathSegment::FUNCTION:
657 0 : {
658 0 : rust_unreachable ();
659 : // TODO
660 : // const TypePathSegmentFunction &fn
661 : // = (const TypePathSegmentFunction &) (*seg.get ());
662 : }
663 36 : break;
664 : }
665 : }
666 :
667 35 : TypePath p (std::move (segments), path.get_locus (),
668 35 : path.has_opening_scope_resolution_op ());
669 :
670 35 : type_param_bounds.emplace_back (new TraitBound (
671 : std::move (p), tb.get_locus (), tb.is_in_parens (),
672 70 : tb.has_opening_question_mark (), std::move (for_lifetimes)));
673 35 : }
674 35 : break;
675 :
676 26 : case TypeParamBound::TypeParamBoundType::LIFETIME:
677 26 : {
678 26 : const Lifetime &l = (const Lifetime &) *b.get ();
679 :
680 26 : type_param_bounds.emplace_back (
681 26 : new Lifetime (l.get_lifetime_type (), l.get_lifetime_name (),
682 52 : l.get_locus ()));
683 : }
684 26 : break;
685 : }
686 : }
687 :
688 408 : auto type_param
689 : = new TypeParam (type_representation, locus, std::move (type_param_bounds),
690 408 : std::move (type), std::move (outer_attrs));
691 :
692 408 : return std::unique_ptr<GenericParam> (type_param);
693 408 : }
694 :
695 : Lifetime
696 261 : Builder::new_lifetime (const Lifetime &lifetime)
697 : {
698 522 : return Lifetime (lifetime.get_lifetime_type (), lifetime.get_lifetime_name (),
699 522 : lifetime.get_locus ());
700 : }
701 :
702 : GenericArgs
703 0 : Builder::new_generic_args (GenericArgs &args)
704 : {
705 0 : std::vector<Lifetime> lifetime_args;
706 0 : std::vector<GenericArg> generic_args;
707 0 : std::vector<GenericArgsBinding> binding_args;
708 0 : location_t locus = args.get_locus ();
709 :
710 0 : for (const auto &lifetime : args.get_lifetime_args ())
711 0 : lifetime_args.push_back (new_lifetime (lifetime));
712 :
713 0 : for (auto &binding : args.get_binding_args ())
714 : {
715 0 : Type &t = *binding.get_type_ptr ().get ();
716 0 : std::unique_ptr<Type> ty = t.reconstruct ();
717 0 : binding_args.emplace_back (binding.get_identifier (), std::move (ty),
718 0 : binding.get_locus ());
719 0 : }
720 :
721 0 : for (auto &arg : args.get_generic_args ())
722 : {
723 0 : tl::optional<GenericArg> new_arg = tl::nullopt;
724 :
725 0 : switch (arg.get_kind ())
726 : {
727 0 : case GenericArg::Kind::Type:
728 0 : new_arg = GenericArg::create_type (arg.get_type ().reconstruct ());
729 0 : break;
730 0 : case GenericArg::Kind::Either:
731 0 : new_arg
732 0 : = GenericArg::create_ambiguous (arg.get_path (), arg.get_locus ());
733 0 : break;
734 0 : case GenericArg::Kind::Const:
735 0 : new_arg
736 0 : = GenericArg::create_const (arg.get_expression ().clone_expr ());
737 : // FIXME: Use `reconstruct()` here, not `clone_expr()`
738 0 : break;
739 : }
740 :
741 0 : generic_args.emplace_back (*new_arg);
742 0 : }
743 :
744 0 : return GenericArgs (std::move (lifetime_args), std::move (generic_args),
745 0 : std::move (binding_args), locus);
746 0 : }
747 :
748 : std::unique_ptr<Expr>
749 771 : Builder::qualified_call (std::vector<std::string> &&segments,
750 : std::vector<std::unique_ptr<Expr>> &&args) const
751 : {
752 771 : auto path = std::unique_ptr<Expr> (
753 771 : new PathInExpression (path_in_expression (std::move (segments))));
754 :
755 771 : return call (std::move (path), std::move (args));
756 771 : }
757 :
758 : } // namespace AST
759 : } // namespace Rust
|