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