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