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