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