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 : 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 : 18 : Builder::literal_string (std::string &&content) const
45 : : {
46 : 18 : return std::unique_ptr<Expr> (
47 : : new AST::LiteralExpr (std::move (content), Literal::LitType::STRING,
48 : 18 : 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 : 447 : Builder::call (std::unique_ptr<Expr> &&path,
64 : : std::vector<std::unique_ptr<Expr>> &&args) const
65 : : {
66 : 447 : return std::unique_ptr<Expr> (
67 : 447 : 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 : 4 : Builder::array (std::vector<std::unique_ptr<Expr>> &&members) const
81 : : {
82 : 4 : auto elts = std::make_unique<ArrayElemsValues> (std::move (members), loc);
83 : :
84 : 4 : return std::unique_ptr<Expr> (new ArrayExpr (std::move (elts), {}, {}, loc));
85 : 4 : }
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 : 1200 : Builder::identifier (std::string name) const
110 : : {
111 : 3600 : return std::unique_ptr<Expr> (new IdentifierExpr (name, {}, loc));
112 : : }
113 : :
114 : : std::unique_ptr<Pattern>
115 : 684 : Builder::identifier_pattern (std::string name, bool mut) const
116 : : {
117 : 684 : return std::unique_ptr<Pattern> (
118 : 2052 : 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 : 218 : Builder::self_ref_param (bool mutability) const
137 : : {
138 : 218 : return std::make_unique<SelfParam> (tl::nullopt, mutability, loc);
139 : : }
140 : :
141 : : std::unique_ptr<Param>
142 : 173 : Builder::function_param (std::unique_ptr<Pattern> &&pattern,
143 : : std::unique_ptr<Type> &&type) const
144 : : {
145 : 173 : return std::unique_ptr<FunctionParam> (
146 : 173 : 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 : 233 : 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 : 233 : return std::unique_ptr<Function> (
165 : 466 : new Function (function_name, qualifiers, std::move (generic_params),
166 : : std::move (params), std::move (return_type), where_clause,
167 : 932 : std::move (block), visibility, {}, loc));
168 : : }
169 : :
170 : : PathExprSegment
171 : 1936 : Builder::path_segment (std::string seg) const
172 : : {
173 : 3872 : return PathExprSegment (PathIdentSegment (seg, loc), loc);
174 : : }
175 : :
176 : : std::unique_ptr<TypePathSegment>
177 : 2084 : Builder::type_path_segment (std::string seg) const
178 : : {
179 : 2084 : return std::unique_ptr<TypePathSegment> (
180 : 4168 : new TypePathSegment (seg, false, loc));
181 : : }
182 : :
183 : : std::unique_ptr<TypePathSegment>
184 : 384 : Builder::type_path_segment (LangItem::Kind lang_item) const
185 : : {
186 : 384 : return std::unique_ptr<TypePathSegment> (
187 : 384 : 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 : 47 : Builder::type_path_segment_generic (LangItem::Kind lang_item,
199 : : GenericArgs args) const
200 : : {
201 : 47 : return std::unique_ptr<TypePathSegment> (
202 : 47 : new TypePathSegmentGeneric (lang_item, args, loc));
203 : : }
204 : :
205 : : std::unique_ptr<Type>
206 : 602 : Builder::single_type_path (std::string type) const
207 : : {
208 : 602 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
209 : 1204 : segments.emplace_back (type_path_segment (type));
210 : :
211 : 602 : return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
212 : 602 : }
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 : 47 : Builder::single_generic_type_path (LangItem::Kind lang_item,
231 : : GenericArgs args) const
232 : : {
233 : 47 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
234 : 47 : segments.emplace_back (type_path_segment_generic (lang_item, args));
235 : :
236 : 47 : return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
237 : 47 : }
238 : :
239 : : TypePath
240 : 607 : Builder::type_path (std::vector<std::unique_ptr<TypePathSegment>> &&segments,
241 : : bool opening_scope) const
242 : : {
243 : 607 : return TypePath (std::move (segments), loc, opening_scope);
244 : : }
245 : :
246 : : TypePath
247 : 407 : Builder::type_path (std::vector<std::string> &&segments,
248 : : bool opening_scope) const
249 : : {
250 : 407 : auto type_segments = std::vector<std::unique_ptr<TypePathSegment>> ();
251 : :
252 : 1628 : for (auto &&segment : segments)
253 : 2442 : type_segments.emplace_back (type_path_segment (segment));
254 : :
255 : 407 : return TypePath (std::move (type_segments), loc, opening_scope);
256 : 407 : }
257 : :
258 : : TypePath
259 : 569 : Builder::type_path (std::unique_ptr<TypePathSegment> &&segment) const
260 : : {
261 : 569 : auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
262 : 569 : segments.emplace_back (std::move (segment));
263 : :
264 : 569 : return type_path (std::move (segments));
265 : 569 : }
266 : :
267 : : TypePath
268 : 185 : Builder::type_path (std::string type) const
269 : : {
270 : 370 : return type_path (type_path_segment (type));
271 : : }
272 : :
273 : : TypePath
274 : 384 : Builder::type_path (LangItem::Kind lang_item) const
275 : : {
276 : 384 : return type_path (type_path_segment (lang_item));
277 : : }
278 : :
279 : : std::unique_ptr<Type>
280 : 173 : Builder::reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
281 : : bool mutability) const
282 : : {
283 : 173 : return std::make_unique<ReferenceType> (mutability, std::move (inner_type),
284 : 173 : loc);
285 : : }
286 : :
287 : : PathInExpression
288 : 476 : Builder::path_in_expression (std::vector<std::string> &&segments,
289 : : bool opening_scope) const
290 : : {
291 : 476 : auto path_segments = std::vector<PathExprSegment> ();
292 : 2067 : for (auto &seg : segments)
293 : 4773 : path_segments.emplace_back (path_segment (seg));
294 : :
295 : 476 : return PathInExpression (std::move (path_segments), {}, loc, opening_scope);
296 : 476 : }
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 : 140 : Builder::block (tl::optional<std::unique_ptr<Stmt>> &&stmt,
314 : : std::unique_ptr<Expr> &&tail_expr) const
315 : : {
316 : 140 : auto stmts = std::vector<std::unique_ptr<Stmt>> ();
317 : :
318 : 140 : if (stmt)
319 : 16 : stmts.emplace_back (std::move (*stmt));
320 : :
321 : 140 : return block (std::move (stmts), std::move (tail_expr));
322 : 140 : }
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 : 124 : Builder::block (std::unique_ptr<Expr> &&tail_expr) const
334 : : {
335 : 124 : return block (tl::nullopt, std::move (tail_expr));
336 : : }
337 : :
338 : : std::unique_ptr<BlockExpr>
339 : 210 : Builder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
340 : : std::unique_ptr<Expr> &&tail_expr) const
341 : : {
342 : 210 : return std::unique_ptr<BlockExpr> (new BlockExpr (std::move (stmts),
343 : : std::move (tail_expr), {},
344 : 210 : {}, 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 : 1 : new ReturnExpr (std::move (to_return), {}, loc));
352 : : }
353 : :
354 : : std::unique_ptr<Stmt>
355 : 215 : Builder::let (std::unique_ptr<Pattern> &&pattern, std::unique_ptr<Type> &&type,
356 : : std::unique_ptr<Expr> &&init) const
357 : : {
358 : 215 : return std::unique_ptr<Stmt> (new LetStmt (std::move (pattern),
359 : : std::move (init), std::move (type),
360 : 215 : tl::nullopt, {}, loc));
361 : : }
362 : :
363 : : std::unique_ptr<Expr>
364 : 473 : Builder::ref (std::unique_ptr<Expr> &&of, bool mut) const
365 : : {
366 : 473 : auto mutability = mut ? Mutability::Mut : Mutability::Imm;
367 : 473 : return std::unique_ptr<Expr> (
368 : : new BorrowExpr (std::move (of), mutability,
369 : 473 : /* 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 : 189 : Builder::comparison_expr (std::unique_ptr<Expr> &&lhs,
380 : : std::unique_ptr<Expr> &&rhs,
381 : : ComparisonOperator op) const
382 : : {
383 : 189 : return std::make_unique<ComparisonExpr> (std::move (lhs), std::move (rhs), op,
384 : 189 : 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 : 47 : Builder::struct_struct (std::string struct_name,
398 : : std::vector<std::unique_ptr<GenericParam>> &&generics,
399 : : std::vector<StructField> &&fields)
400 : : {
401 : 47 : auto is_unit = fields.empty ();
402 : :
403 : 47 : return std::unique_ptr<Stmt> (
404 : 141 : new StructStruct (std::move (fields), struct_name, std::move (generics),
405 : 94 : WhereClause::create_empty (), is_unit,
406 : 188 : 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 : 423 : Builder::field_access (std::unique_ptr<Expr> &&instance,
443 : : std::string field) const
444 : : {
445 : 423 : return std::unique_ptr<Expr> (
446 : 1269 : 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 : 125 : Builder::wildcard () const
459 : : {
460 : 125 : 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 : auto patterns = std::vector<std::unique_ptr<Pattern>> ();
488 : 365 : patterns.emplace_back (std::move (pattern));
489 : :
490 : 365 : return MatchArm (std::move (patterns), loc);
491 : 365 : }
492 : :
493 : : MatchCase
494 : 157 : Builder::match_case (std::unique_ptr<Pattern> &&pattern,
495 : : std::unique_ptr<Expr> &&expr)
496 : : {
497 : 157 : return match_case (match_arm (std::move (pattern)), std::move (expr));
498 : : }
499 : :
500 : : MatchCase
501 : 329 : Builder::match_case (MatchArm &&arm, std::unique_ptr<Expr> &&expr)
502 : : {
503 : 329 : return MatchCase (std::move (arm), std::move (expr));
504 : : }
505 : : std::unique_ptr<Expr>
506 : 18 : Builder::loop (std::vector<std::unique_ptr<Stmt>> &&stmts)
507 : : {
508 : 18 : auto block_expr = block (std::move (stmts), nullptr);
509 : :
510 : 18 : return std::unique_ptr<Expr> (new LoopExpr (std::move (block_expr), loc));
511 : 18 : }
512 : :
513 : : std::unique_ptr<TypeParamBound>
514 : 289 : Builder::trait_bound (TypePath bound)
515 : : {
516 : 289 : return std::make_unique<TraitBound> (bound, loc);
517 : : }
518 : :
519 : : std::unique_ptr<Item>
520 : 423 : 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 : 423 : 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 : 423 : visibility, {}, {}, loc));
530 : : }
531 : :
532 : : std::unique_ptr<GenericParam>
533 : 3 : 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 : 3 : return std::make_unique<TypeParam> (type_representation, loc,
539 : : std::move (bounds), std::move (type),
540 : 3 : std::vector<Attribute> ());
541 : : }
542 : :
543 : : std::unique_ptr<Stmt>
544 : 89 : Builder::discriminant_value (std::string binding_name, std::string instance)
545 : : {
546 : 89 : auto intrinsic = ptrify (
547 : 89 : path_in_expression ({"core", "intrinsics", "discriminant_value"}, true));
548 : :
549 : 356 : return let (identifier_pattern (binding_name), nullptr,
550 : 356 : call (std::move (intrinsic), identifier (instance)));
551 : 89 : }
552 : :
553 : : std::unique_ptr<GenericParam>
554 : 0 : Builder::new_lifetime_param (LifetimeParam ¶m)
555 : : {
556 : 0 : Lifetime l = new_lifetime (param.get_lifetime ());
557 : 0 : std::vector<Lifetime> lifetime_bounds;
558 : 0 : for (auto b : param.get_lifetime_bounds ())
559 : : {
560 : 0 : Lifetime bl = new_lifetime (b);
561 : 0 : lifetime_bounds.push_back (bl);
562 : 0 : }
563 : :
564 : 0 : auto p = new LifetimeParam (l, std::move (lifetime_bounds),
565 : 0 : param.get_outer_attrs (), param.get_locus ());
566 : 0 : return std::unique_ptr<GenericParam> (p);
567 : 0 : }
568 : :
569 : : std::unique_ptr<GenericParam>
570 : 0 : Builder::new_const_param (ConstGenericParam ¶m) const
571 : : {
572 : 0 : return std::make_unique<ConstGenericParam> (param.get_name (),
573 : 0 : param.get_type ().clone_type (),
574 : : param.get_default_value (),
575 : 0 : param.get_outer_attrs (),
576 : 0 : param.get_locus ());
577 : : }
578 : :
579 : : std::unique_ptr<GenericParam>
580 : 2 : Builder::new_type_param (
581 : : TypeParam ¶m, std::vector<std::unique_ptr<TypeParamBound>> extra_bounds)
582 : : {
583 : 2 : location_t locus = param.get_locus ();
584 : 2 : AST::AttrVec outer_attrs = param.get_outer_attrs ();
585 : 2 : Identifier type_representation = param.get_type_representation ();
586 : 2 : std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
587 : 2 : std::unique_ptr<Type> type = nullptr;
588 : :
589 : 2 : if (param.has_type ())
590 : 0 : type = param.get_type ().reconstruct ();
591 : :
592 : 4 : for (auto &&extra_bound : extra_bounds)
593 : 2 : type_param_bounds.emplace_back (std::move (extra_bound));
594 : :
595 : 4 : for (const auto &b : param.get_type_param_bounds ())
596 : : {
597 : 2 : switch (b->get_bound_type ())
598 : : {
599 : 2 : case TypeParamBound::TypeParamBoundType::TRAIT:
600 : 2 : {
601 : 2 : const TraitBound &tb = (const TraitBound &) *b.get ();
602 : 2 : const TypePath &path = tb.get_type_path ();
603 : :
604 : 2 : std::vector<LifetimeParam> for_lifetimes;
605 : 2 : for (const auto &lifetime : tb.get_for_lifetimes ())
606 : : {
607 : 0 : std::vector<Lifetime> lifetime_bounds;
608 : 0 : for (const auto &b : lifetime.get_lifetime_bounds ())
609 : : {
610 : 0 : Lifetime bl = new_lifetime (b);
611 : 0 : lifetime_bounds.push_back (std::move (bl));
612 : 0 : }
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 : 2 : std::vector<std::unique_ptr<TypePathSegment>> segments;
621 : 4 : for (auto &seg : path.get_segments ())
622 : : {
623 : 2 : switch (seg->get_type ())
624 : : {
625 : 2 : case TypePathSegment::REG:
626 : 2 : {
627 : 2 : const TypePathSegment &segment
628 : 2 : = (const TypePathSegment &) (*seg.get ());
629 : 2 : TypePathSegment *s = new TypePathSegment (
630 : : segment.get_ident_segment (),
631 : 2 : segment.get_separating_scope_resolution (),
632 : 2 : segment.get_locus ());
633 : 2 : std::unique_ptr<TypePathSegment> sg (s);
634 : 2 : segments.push_back (std::move (sg));
635 : 2 : }
636 : 2 : break;
637 : :
638 : 0 : case TypePathSegment::GENERIC:
639 : 0 : {
640 : 0 : TypePathSegmentGeneric &generic
641 : 0 : = (TypePathSegmentGeneric &) (*seg.get ());
642 : :
643 : 0 : GenericArgs args
644 : 0 : = new_generic_args (generic.get_generic_args ());
645 : 0 : TypePathSegmentGeneric *s = new TypePathSegmentGeneric (
646 : 0 : generic.get_ident_segment (), false, std::move (args),
647 : 0 : generic.get_locus ());
648 : 0 : std::unique_ptr<TypePathSegment> sg (s);
649 : 0 : segments.push_back (std::move (sg));
650 : 0 : }
651 : 0 : break;
652 : :
653 : 0 : case TypePathSegment::FUNCTION:
654 : 0 : {
655 : 0 : rust_unreachable ();
656 : : // TODO
657 : : // const TypePathSegmentFunction &fn
658 : : // = (const TypePathSegmentFunction &) (*seg.get ());
659 : : }
660 : 2 : break;
661 : : }
662 : : }
663 : :
664 : 2 : TypePath p (std::move (segments), path.get_locus (),
665 : 2 : path.has_opening_scope_resolution_op ());
666 : :
667 : 2 : TraitBound *b = new TraitBound (std::move (p), tb.get_locus (),
668 : 2 : tb.is_in_parens (),
669 : 2 : tb.has_opening_question_mark (),
670 : 2 : std::move (for_lifetimes));
671 : 4 : std::unique_ptr<TypeParamBound> bound (b);
672 : 2 : type_param_bounds.push_back (std::move (bound));
673 : 2 : }
674 : 2 : break;
675 : :
676 : 0 : case TypeParamBound::TypeParamBoundType::LIFETIME:
677 : 0 : {
678 : 0 : const Lifetime &l = (const Lifetime &) *b.get ();
679 : :
680 : 0 : auto bl = new Lifetime (l.get_lifetime_type (),
681 : 0 : l.get_lifetime_name (), l.get_locus ());
682 : 0 : std::unique_ptr<TypeParamBound> bound (bl);
683 : 0 : type_param_bounds.push_back (std::move (bound));
684 : 0 : }
685 : 0 : break;
686 : : }
687 : : }
688 : :
689 : 2 : auto type_param
690 : : = new TypeParam (type_representation, locus, std::move (type_param_bounds),
691 : 2 : std::move (type), std::move (outer_attrs));
692 : :
693 : 2 : return std::unique_ptr<GenericParam> (type_param);
694 : 2 : }
695 : :
696 : : Lifetime
697 : 0 : Builder::new_lifetime (const Lifetime &lifetime)
698 : : {
699 : 0 : return Lifetime (lifetime.get_lifetime_type (), lifetime.get_lifetime_name (),
700 : 0 : lifetime.get_locus ());
701 : : }
702 : :
703 : : GenericArgs
704 : 0 : Builder::new_generic_args (GenericArgs &args)
705 : : {
706 : 0 : std::vector<Lifetime> lifetime_args;
707 : 0 : std::vector<GenericArg> generic_args;
708 : 0 : std::vector<GenericArgsBinding> binding_args;
709 : 0 : location_t locus = args.get_locus ();
710 : :
711 : 0 : for (const auto &lifetime : args.get_lifetime_args ())
712 : : {
713 : 0 : Lifetime l = new_lifetime (lifetime);
714 : 0 : lifetime_args.push_back (std::move (l));
715 : 0 : }
716 : :
717 : 0 : for (auto &binding : args.get_binding_args ())
718 : : {
719 : 0 : Type &t = *binding.get_type_ptr ().get ();
720 : 0 : std::unique_ptr<Type> ty = t.reconstruct ();
721 : 0 : GenericArgsBinding b (binding.get_identifier (), std::move (ty),
722 : 0 : binding.get_locus ());
723 : 0 : binding_args.push_back (std::move (b));
724 : 0 : }
725 : :
726 : 0 : for (auto &arg : args.get_generic_args ())
727 : : {
728 : 0 : tl::optional<GenericArg> new_arg = tl::nullopt;
729 : :
730 : 0 : switch (arg.get_kind ())
731 : : {
732 : 0 : case GenericArg::Kind::Type:
733 : 0 : new_arg = GenericArg::create_type (arg.get_type ().reconstruct ());
734 : 0 : break;
735 : 0 : case GenericArg::Kind::Either:
736 : 0 : new_arg
737 : 0 : = GenericArg::create_ambiguous (arg.get_path (), arg.get_locus ());
738 : 0 : break;
739 : 0 : case GenericArg::Kind::Const:
740 : 0 : new_arg
741 : 0 : = GenericArg::create_const (arg.get_expression ().clone_expr ());
742 : : // FIXME: Use `reconstruct()` here, not `clone_expr()`
743 : 0 : break;
744 : : }
745 : :
746 : 0 : generic_args.emplace_back (*new_arg);
747 : 0 : }
748 : :
749 : 0 : return GenericArgs (std::move (lifetime_args), std::move (generic_args),
750 : 0 : std::move (binding_args), locus);
751 : 0 : }
752 : :
753 : : } // namespace AST
754 : : } // namespace Rust
|