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