Debian Codegraph @

gcc/rust/ast/rust-ast-collector.cc

raw
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-collector.h"
20 #include "rust-ast.h"
21 #include "rust-diagnostics.h"
22 #include "rust-expr.h"
23 #include "rust-item.h"
24 #include "rust-keyword-values.h"
25 #include "rust-location.h"
26 #include "rust-path.h"
27 #include "rust-system.h"
28 #include "rust-token.h"
29
30 namespace Rust {
31 namespace AST {
32
33 std::vector<TokenPtr>
34 TokenCollector::collect_tokens () const
35 {
36 std::vector<TokenPtr> result;
37 for (auto item : tokens)
38 {
39 if (item.get_kind () == CollectItem::Kind::Token)
40 {
41 result.emplace_back (item.get_token ());
42 }
43 }
44 return result;
45 }
46
47 std::vector<CollectItem>
48 TokenCollector::collect () const
49 {
50 return tokens;
51 }
52
53 void
54 TokenCollector::visit (AST::Crate &crate)
55 {
56 visit_items_as_lines (crate.inner_attrs);
57 visit_items_as_lines (crate.items);
58 }
59
60 void
61 TokenCollector::visit (AST::Item &item)
62 {
63 item.accept_vis (*this);
64 }
65
66 void
67 TokenCollector::trailing_comma ()
68 {
69 if (output_trailing_commas)
70 {
71 push (Rust::Token::make (COMMA, UNDEF_LOCATION));
72 }
73 }
74
75 void
76 TokenCollector::newline ()
77 {
78 tokens.push_back ({CollectItem::Kind::Newline});
79 }
80
81 void
82 TokenCollector::indentation ()
83 {
84 tokens.push_back ({indent_level});
85 }
86
87 void
88 TokenCollector::increment_indentation ()
89 {
90 indent_level++;
91 }
92
93 void
94 TokenCollector::decrement_indentation ()
95 {
96 rust_assert (indent_level != 0);
97 indent_level--;
98 }
99
100 void
101 TokenCollector::comment (std::string comment)
102 {
103 tokens.push_back ({comment});
104 }
105
106 void
107 TokenCollector::visit (Visitable &v)
108 {
109 v.accept_vis (*this);
110 }
111
112 void
113 TokenCollector::visit (FunctionParam &param)
114 {
115 visit_items_as_lines (param.get_outer_attrs ());
116 if (!param.is_variadic ())
117 {
118 visit (param.get_pattern ());
119 push (Rust::Token::make (COLON, UNDEF_LOCATION));
120 visit (param.get_type ());
121 }
122 else
123 {
124 if (param.has_name ())
125 {
126 visit (param.get_pattern ());
127 push (Rust::Token::make (COLON, UNDEF_LOCATION));
128 }
129 push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION));
130 }
131 }
132
133 void
134 TokenCollector::visit (VariadicParam &param)
135 {
136 if (param.has_pattern ())
137 {
138 visit (param.get_pattern ());
139 push (Rust::Token::make (COLON, UNDEF_LOCATION));
140 }
141 push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION));
142 }
143
144 void
145 TokenCollector::visit (Attribute &attrib)
146 {
147 push (Rust::Token::make (HASH, attrib.get_locus ()));
148 if (attrib.is_inner_attribute ())
149 push (Rust::Token::make (EXCLAM, UNDEF_LOCATION));
150 push (Rust::Token::make (LEFT_SQUARE, UNDEF_LOCATION));
151 visit (attrib.get_path ());
152
153 if (attrib.has_attr_input ())
154 {
155 switch (attrib.get_attr_input ().get_attr_input_type ())
156 {
157 case AST::AttrInput::AttrInputType::LITERAL: {
158 visit (static_cast<AttrInputLiteral &> (attrib.get_attr_input ()));
159 break;
160 }
161 case AST::AttrInput::AttrInputType::MACRO: {
162 visit (static_cast<AttrInputMacro &> (attrib.get_attr_input ()));
163 break;
164 }
165 case AST::AttrInput::AttrInputType::META_ITEM: {
166 visit (static_cast<AttrInputMetaItemContainer &> (
167 attrib.get_attr_input ()));
168 break;
169 }
170 case AST::AttrInput::AttrInputType::TOKEN_TREE: {
171 visit (static_cast<DelimTokenTree &> (attrib.get_attr_input ()));
172 break;
173 }
174 default:
175 rust_unreachable ();
176 }
177 }
178 push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
179 }
180
181 void
182 TokenCollector::visit (SimplePath &path)
183 {
184 if (path.has_opening_scope_resolution ())
185 {
186 push (Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
187 }
188 visit_items_joined_by_separator (path.get_segments (), SCOPE_RESOLUTION);
189 }
190
191 void
192 TokenCollector::visit (SimplePathSegment &segment)
193 {
194 auto name = segment.get_segment_name ();
195 if (segment.is_crate_path_seg ())
196 {
197 push (Rust::Token::make (CRATE, segment.get_locus ()));
198 }
199 else if (segment.is_super_path_seg ())
200 {
201 push (Rust::Token::make (SUPER, segment.get_locus ()));
202 }
203 else if (segment.is_lower_self_seg ())
204 {
205 push (Rust::Token::make (SELF, segment.get_locus ()));
206 }
207 else if (segment.is_big_self ())
208 {
209 push (Rust::Token::make (SELF_ALIAS, segment.get_locus ()));
210 }
211 else
212 {
213 push (
214 Rust::Token::make_identifier (segment.get_locus (), std::move (name)));
215 }
216 }
217
218 void
219 TokenCollector::visit (Visibility &vis)
220 {
221 switch (vis.get_vis_type ())
222 {
223 case Visibility::PUB:
224 push (Rust::Token::make (PUB, vis.get_locus ()));
225 break;
226 case Visibility::PUB_CRATE:
227 push (Rust::Token::make (PUB, vis.get_locus ()));
228 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
229 push (Rust::Token::make (CRATE, UNDEF_LOCATION));
230 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
231 break;
232 case Visibility::PUB_SELF:
233 push (Rust::Token::make (PUB, vis.get_locus ()));
234 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
235 push (Rust::Token::make (SELF, UNDEF_LOCATION));
236 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
237 break;
238 case Visibility::PUB_SUPER:
239 push (Rust::Token::make (PUB, vis.get_locus ()));
240 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
241 push (Rust::Token::make (SUPER, UNDEF_LOCATION));
242 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
243 break;
244 case Visibility::PUB_IN_PATH:
245 push (Rust::Token::make (PUB, vis.get_locus ()));
246 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
247 push (Rust::Token::make (IN, UNDEF_LOCATION));
248 visit (vis.get_path ());
249 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
250 break;
251 case Visibility::PRIV:
252 break;
253 }
254 }
255
256 void
257 TokenCollector::visit (std::vector<std::unique_ptr<GenericParam>> &params)
258 {
259 push (Rust::Token::make (LEFT_ANGLE, UNDEF_LOCATION));
260 visit_items_joined_by_separator (params, COMMA);
261 push (Rust::Token::make (RIGHT_ANGLE, UNDEF_LOCATION));
262 }
263
264 void
265 TokenCollector::visit (TupleField &field)
266 {
267 for (auto attr : field.get_outer_attrs ())
268 {
269 visit (attr);
270 }
271 visit (field.get_visibility ());
272 visit (field.get_field_type ());
273 }
274
275 void
276 TokenCollector::visit (StructField &field)
277 {
278 for (auto attr : field.get_outer_attrs ())
279 {
280 visit (attr);
281 }
282 visit (field.get_visibility ());
283 auto name = field.get_field_name ().as_string ();
284 push (Rust::Token::make_identifier (field.get_locus (), std::move (name)));
285 push (Rust::Token::make (COLON, UNDEF_LOCATION));
286 visit (field.get_field_type ());
287 }
288
289 void
290 TokenCollector::visit (std::vector<LifetimeParam> &for_lifetimes)
291 {
292 push (Rust::Token::make (FOR, UNDEF_LOCATION));
293 push (Rust::Token::make (LEFT_ANGLE, UNDEF_LOCATION));
294 visit_items_joined_by_separator (for_lifetimes, COMMA);
295 push (Rust::Token::make (RIGHT_ANGLE, UNDEF_LOCATION));
296 }
297
298 void
299 TokenCollector::visit (FunctionQualifiers &qualifiers)
300 {
301 // Syntax:
302 // `const`? `async`? `unsafe`? (`extern` Abi?)?
303 // unsafe? (extern Abi?)?
304
305 if (qualifiers.is_async ())
306 push (Rust::Token::make (ASYNC, qualifiers.get_locus ()));
307 if (qualifiers.is_const ())
308 push (Rust::Token::make (CONST, qualifiers.get_locus ()));
309 if (qualifiers.is_unsafe ())
310 push (Rust::Token::make (UNSAFE, qualifiers.get_locus ()));
311 if (qualifiers.is_extern ())
312 {
313 push (Rust::Token::make (EXTERN_KW, qualifiers.get_locus ()));
314 if (qualifiers.has_abi ())
315 {
316 push (Rust::Token::make_string (UNDEF_LOCATION,
317 qualifiers.get_extern_abi ()));
318 }
319 }
320 }
321
322 void
323 TokenCollector::visit (MaybeNamedParam &param)
324 {
325 // Syntax:
326 // OuterAttribute* ( ( IDENTIFIER | _ ) : )? Type
327
328 for (auto attr : param.get_outer_attrs ())
329 {
330 visit (attr);
331 }
332 auto param_name = param.get_name ().as_string ();
333 switch (param.get_param_kind ())
334 {
335 case MaybeNamedParam::UNNAMED:
336 break;
337 case MaybeNamedParam::IDENTIFIER:
338 push (
339 Rust::Token::make_identifier (UNDEF_LOCATION, std::move (param_name)));
340 push (Rust::Token::make (COLON, UNDEF_LOCATION));
341 break;
342 case MaybeNamedParam::WILDCARD:
343 push (Rust::Token::make (UNDERSCORE, UNDEF_LOCATION));
344 push (Rust::Token::make (COLON, UNDEF_LOCATION));
345 break;
346 }
347 visit (param.get_type ());
348 }
349
350 void
351 TokenCollector::visit (Token &tok)
352 {
353 std::string data = tok.get_tok_ptr ()->has_str () ? tok.get_str () : "";
354 switch (tok.get_id ())
355 {
356 case IDENTIFIER:
357 push (Rust::Token::make_identifier (tok.get_locus (), std::move (data)));
358 break;
359 case INT_LITERAL:
360 push (Rust::Token::make_int (tok.get_locus (), std::move (data),
361 tok.get_type_hint ()));
362 break;
363 case FLOAT_LITERAL:
364 push (Rust::Token::make_float (tok.get_locus (), std::move (data),
365 tok.get_type_hint ()));
366 break;
367 case STRING_LITERAL:
368 push (Rust::Token::make_string (tok.get_locus (), std::move (data)));
369 break;
370 case CHAR_LITERAL:
371 push (Rust::Token::make_char (
372 tok.get_locus (),
373 // FIXME: This need to be fixed to properly support UTF-8
374 static_cast<uint32_t> (data[0])));
375 break;
376 case BYTE_CHAR_LITERAL:
377 push (Rust::Token::make_byte_char (tok.get_locus (), data[0]));
378 break;
379 case BYTE_STRING_LITERAL:
380 push (Rust::Token::make_byte_string (tok.get_locus (), std::move (data)));
381 break;
382 case RAW_STRING_LITERAL:
383 push (Rust::Token::make_raw_string (tok.get_locus (), std::move (data)));
384 break;
385 case INNER_DOC_COMMENT:
386 push (Rust::Token::make_inner_doc_comment (tok.get_locus (),
387 std::move (data)));
388 break;
389 case OUTER_DOC_COMMENT:
390 push (Rust::Token::make_outer_doc_comment (tok.get_locus (),
391 std::move (data)));
392 break;
393 case LIFETIME:
394 push (Rust::Token::make_lifetime (tok.get_locus (), std::move (data)));
395 break;
396 default:
397 push (Rust::Token::make (tok.get_id (), tok.get_locus ()));
398 }
399 }
400
401 void
402 TokenCollector::visit (DelimTokenTree &delim_tok_tree)
403 {
404 for (auto &token : delim_tok_tree.to_token_stream ())
405 {
406 visit (token);
407 }
408 }
409
410 void
411 TokenCollector::visit (AttrInputMetaItemContainer &container)
412 {
413 for (auto &item : container.get_items ())
414 {
415 visit (item);
416 }
417 }
418
419 void
420 TokenCollector::visit (IdentifierExpr &ident_expr)
421 {
422 auto ident = ident_expr.get_ident ().as_string ();
423 push (
424 Rust::Token::make_identifier (ident_expr.get_locus (), std::move (ident)));
425 }
426
427 void
428 TokenCollector::visit (Lifetime &lifetime)
429 {
430 // Syntax:
431 // Lifetime :
432 // LIFETIME_OR_LABEL
433 // | 'static
434 // | '_
435
436 auto name = lifetime.get_lifetime_name ();
437 switch (lifetime.get_lifetime_type ())
438 {
439 case Lifetime::LifetimeType::NAMED:
440 push (
441 Rust::Token::make_lifetime (lifetime.get_locus (), std::move (name)));
442 break;
443 case Lifetime::LifetimeType::STATIC:
444 push (Rust::Token::make_lifetime (lifetime.get_locus (),
445 Values::Keywords::STATIC_KW));
446 break;
447 case Lifetime::LifetimeType::WILDCARD:
448 push (Rust::Token::make_lifetime (lifetime.get_locus (),
449 Values::Keywords::UNDERSCORE));
450 break;
451 }
452 }
453
454 void
455 TokenCollector::visit (LifetimeParam &lifetime_param)
456 {
457 // Syntax:
458 // LIFETIME_OR_LABEL ( : LifetimeBounds )?
459 // LifetimeBounds :
460 // ( Lifetime + )* Lifetime?
461
462 // TODO what to do with outer attr? They are not mentioned in the reference.
463
464 visit_items_as_lines (lifetime_param.get_outer_attrs ());
465 auto lifetime = lifetime_param.get_lifetime ();
466 visit (lifetime);
467
468 if (lifetime_param.has_lifetime_bounds ())
469 {
470 push (Rust::Token::make (COLON, UNDEF_LOCATION));
471 for (auto &bound : lifetime_param.get_lifetime_bounds ())
472 {
473 visit (bound);
474 }
475 }
476 }
477
478 void
479 TokenCollector::visit (ConstGenericParam &param)
480 {
481 // Syntax:
482 // const IDENTIFIER : Type ( = Block | IDENTIFIER | -?LITERAL )?
483
484 visit_items_as_lines (param.get_outer_attrs ());
485 push (Rust::Token::make (CONST, param.get_locus ()));
486 auto id = param.get_name ().as_string ();
487 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
488 push (Rust::Token::make (COLON, UNDEF_LOCATION));
489 if (param.has_type ())
490 visit (param.get_type ());
491 if (param.has_default_value ())
492 {
493 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
494 visit (param.get_default_value ());
495 }
496 }
497
498 void
499 TokenCollector::visit (PathExprSegment &segment)
500 {
501 visit (segment.get_ident_segment ());
502 if (segment.has_generic_args ())
503 {
504 auto generics = segment.get_generic_args ();
505 push (Rust::Token::make (SCOPE_RESOLUTION, segment.get_locus ()));
506 push (Rust::Token::make (LEFT_ANGLE, generics.get_locus ()));
507
508 auto &lifetime_args = generics.get_lifetime_args ();
509 auto &generic_args = generics.get_generic_args ();
510 auto &binding_args = generics.get_binding_args ();
511
512 visit_items_joined_by_separator (generic_args, COMMA);
513
514 if (!lifetime_args.empty ()
515 && (!generic_args.empty () || !binding_args.empty ()))
516 {
517 push (Rust::Token::make (COMMA, UNDEF_LOCATION));
518 }
519
520 visit_items_joined_by_separator (binding_args, COMMA);
521
522 if (!generic_args.empty () && !binding_args.empty ())
523 {
524 push (Rust::Token::make (COMMA, UNDEF_LOCATION));
525 }
526
527 visit_items_joined_by_separator (lifetime_args, COMMA);
528
529 push (Rust::Token::make (RIGHT_ANGLE, UNDEF_LOCATION));
530 }
531 }
532
533 void
534 TokenCollector::visit (PathInExpression &path)
535 {
536 if (path.is_lang_item ())
537 {
538 push (Rust::Token::make (TokenId::HASH, path.get_locus ()));
539 push (Rust::Token::make (TokenId::LEFT_SQUARE, path.get_locus ()));
540 push (Rust::Token::make_identifier (path.get_locus (), "lang"));
541 push (Rust::Token::make (TokenId::EQUAL, path.get_locus ()));
542 push (
543 Rust::Token::make_string (path.get_locus (),
544 LangItem::ToString (path.get_lang_item ())));
545 push (Rust::Token::make (TokenId::RIGHT_SQUARE, path.get_locus ()));
546
547 return;
548 }
549
550 if (path.opening_scope_resolution ())
551 push (Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
552
553 visit_items_joined_by_separator (path.get_segments (), SCOPE_RESOLUTION);
554 }
555
556 void
557 TokenCollector::visit (TypePathSegment &segment)
558 {
559 // Syntax:
560 // PathIdentSegment
561
562 auto locus = segment.is_lang_item ()
563 ? segment.get_locus ()
564 : segment.get_ident_segment ().get_locus ();
565 auto segment_string = segment.is_lang_item ()
566 ? LangItem::PrettyString (segment.get_lang_item ())
567 : segment.get_ident_segment ().as_string ();
568 push (Rust::Token::make_identifier (locus, std::move (segment_string)));
569 }
570
571 void
572 TokenCollector::visit (TypePathSegmentGeneric &segment)
573 {
574 // Syntax:
575 // PathIdentSegment `::`? (GenericArgs)?
576 // GenericArgs :
577 // `<` `>`
578 // | `<` ( GenericArg `,` )* GenericArg `,`? `>`
579
580 auto locus = segment.is_lang_item ()
581 ? segment.get_locus ()
582 : segment.get_ident_segment ().get_locus ();
583 auto segment_string = segment.is_lang_item ()
584 ? LangItem::PrettyString (segment.get_lang_item ())
585 : segment.get_ident_segment ().as_string ();
586 push (Rust::Token::make_identifier (locus, std::move (segment_string)));
587
588 if (segment.get_separating_scope_resolution ())
589 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
590
591 push (Rust::Token::make (LEFT_ANGLE, UNDEF_LOCATION));
592
593 {
594 auto &lifetime_args = segment.get_generic_args ().get_lifetime_args ();
595 auto &generic_args = segment.get_generic_args ().get_generic_args ();
596 auto &binding_args = segment.get_generic_args ().get_binding_args ();
597
598 visit_items_joined_by_separator (lifetime_args, COMMA);
599 if (!lifetime_args.empty ()
600 && (!generic_args.empty () || !binding_args.empty ()))
601 push (Rust::Token::make (COMMA, UNDEF_LOCATION));
602 visit_items_joined_by_separator (generic_args, COMMA);
603 if (!generic_args.empty () && !binding_args.empty ())
604 push (Rust::Token::make (COMMA, UNDEF_LOCATION));
605 visit_items_joined_by_separator (binding_args, COMMA);
606 }
607
608 push (Rust::Token::make (RIGHT_ANGLE, UNDEF_LOCATION));
609 }
610
611 void
612 TokenCollector::visit (GenericArgsBinding &binding)
613 {
614 // Syntax:
615 // IDENTIFIER `=` Type
616 auto identifier = binding.get_identifier ().as_string ();
617 push (Rust::Token::make_identifier (binding.get_locus (),
618 std::move (identifier)));
619
620 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
621 visit (binding.get_type ());
622 }
623
624 void
625 TokenCollector::visit (GenericArg &arg)
626 {
627 // `GenericArg` implements `accept_vis` but it is not useful for this case as
628 // it ignores unresolved cases (`Kind::Either`).
629 switch (arg.get_kind ())
630 {
631 case GenericArg::Kind::Const:
632 visit (arg.get_expression ());
633 break;
634 case GenericArg::Kind::Type:
635 visit (arg.get_type ());
636 break;
637 case GenericArg::Kind::Either: {
638 auto path = arg.get_path ();
639 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (path)));
640 }
641 break;
642 case GenericArg::Kind::Error:
643 rust_unreachable ();
644 }
645 }
646
647 void
648 TokenCollector::visit (TypePathSegmentFunction &segment)
649 {
650 // Syntax:
651 // PathIdentSegment `::`? (TypePathFn)?
652
653 auto ident_segment = segment.get_ident_segment ();
654 auto id = ident_segment.as_string ();
655 push (
656 Rust::Token::make_identifier (ident_segment.get_locus (), std::move (id)));
657
658 if (segment.get_separating_scope_resolution ())
659 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
660
661 if (!segment.is_ident_only ())
662 visit (segment.get_type_path_function ());
663 }
664
665 void
666 TokenCollector::visit (TypePathFunction &type_path_fn)
667 {
668 // Syntax:
669 // `(` TypePathFnInputs? `)` (`->` Type)?
670 // TypePathFnInputs :
671 // Type (`,` Type)* `,`?
672
673 push (Rust::Token::make (LEFT_PAREN, type_path_fn.get_locus ()));
674 if (type_path_fn.has_inputs ())
675 visit_items_joined_by_separator (type_path_fn.get_params (), COMMA);
676 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
677
678 if (type_path_fn.has_return_type ())
679 {
680 push (Rust::Token::make (RETURN_TYPE, UNDEF_LOCATION));
681 visit (type_path_fn.get_return_type ());
682 }
683 }
684
685 void
686 TokenCollector::visit (TypePath &path)
687 {
688 // Syntax:
689 // `::`? TypePathSegment (`::` TypePathSegment)*
690
691 if (path.has_opening_scope_resolution_op ())
692 push (Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
693
694 visit_items_joined_by_separator (path.get_segments (), SCOPE_RESOLUTION);
695 }
696
697 void
698 TokenCollector::visit (PathIdentSegment &segment)
699 {
700 if (segment.is_super_path_seg ())
701 {
702 push (Rust::Token::make (SUPER, segment.get_locus ()));
703 }
704 else if (segment.is_crate_path_seg ())
705 {
706 push (Rust::Token::make (CRATE, segment.get_locus ()));
707 }
708 else if (segment.is_lower_self_seg ())
709 {
710 push (Rust::Token::make (SELF, segment.get_locus ()));
711 }
712 else if (segment.is_big_self_seg ())
713 {
714 push (Rust::Token::make (SELF_ALIAS, segment.get_locus ()));
715 }
716 else
717 {
718 auto id = segment.as_string ();
719 push (
720 Rust::Token::make_identifier (segment.get_locus (), std::move (id)));
721 }
722 }
723
724 void
725 TokenCollector::visit (QualifiedPathInExpression &path)
726 {
727 visit (path.get_qualified_path_type ());
728 for (auto &segment : path.get_segments ())
729 {
730 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
731 visit (segment);
732 }
733 }
734
735 void
736 TokenCollector::visit (QualifiedPathType &path)
737 {
738 push (Rust::Token::make (LEFT_ANGLE, path.get_locus ()));
739 visit (path.get_type ());
740 if (path.has_as_clause ())
741 {
742 push (Rust::Token::make (AS, UNDEF_LOCATION));
743 visit (path.get_as_type_path ());
744 }
745 push (Rust::Token::make (RIGHT_ANGLE, UNDEF_LOCATION));
746 }
747
748 void
749 TokenCollector::visit (QualifiedPathInType &path)
750 {
751 visit (path.get_qualified_path_type ());
752
753 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
754 visit (path.get_associated_segment ());
755 for (auto &segment : path.get_segments ())
756 {
757 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
758 visit (segment);
759 }
760 }
761
762 void
763 TokenCollector::visit (Literal &lit, location_t locus)
764 {
765 auto value = lit.as_string ();
766 switch (lit.get_lit_type ())
767 {
768 case Literal::LitType::CHAR:
769 push (
770 Rust::Token::make_char (locus,
771 // TODO: Change this to support utf-8 properly
772 Codepoint (static_cast<uint32_t> (value[0]))));
773 break;
774 case Literal::LitType::STRING:
775 push (Rust::Token::make_string (locus, std::move (value)));
776 break;
777 case Literal::LitType::BYTE:
778 push (Rust::Token::make_byte_char (locus, value[0]));
779 break;
780 case Literal::LitType::BYTE_STRING:
781 push (Rust::Token::make_byte_string (locus, std::move (value)));
782 break;
783 case Literal::LitType::RAW_STRING:
784 push (Rust::Token::make_raw_string (locus, std::move (value)));
785 break;
786 case Literal::LitType::INT:
787 push (
788 Rust::Token::make_int (locus, std::move (value), lit.get_type_hint ()));
789 break;
790 case Literal::LitType::FLOAT:
791 push (Rust::Token::make_float (locus, std::move (value),
792 lit.get_type_hint ()));
793 break;
794 case Literal::LitType::BOOL: {
795 if (value == Values::Keywords::FALSE_LITERAL)
796 push (Rust::Token::make (FALSE_LITERAL, locus));
797 else if (value == Values::Keywords::TRUE_LITERAL)
798 push (Rust::Token::make (TRUE_LITERAL, locus));
799 else
800 rust_unreachable (); // Not a boolean
801 break;
802 }
803 case Literal::LitType::ERROR:
804 rust_unreachable ();
805 break;
806 }
807 }
808
809 void
810 TokenCollector::visit (LiteralExpr &expr)
811 {
812 auto lit = expr.get_literal ();
813 visit (lit, expr.get_locus ());
814 }
815
816 void
817 TokenCollector::visit (AttrInputLiteral &literal)
818 {
819 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
820 visit (literal.get_literal ());
821 }
822
823 void
824 TokenCollector::visit (AttrInputMacro &macro)
825 {
826 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
827 visit (macro.get_macro ());
828 }
829
830 void
831 TokenCollector::visit (MetaItemLitExpr &item)
832 {
833 auto lit = item.get_literal ();
834 visit (lit);
835 }
836
837 void
838 TokenCollector::visit (MetaItemPathLit &item)
839 {
840 auto path = item.get_path ();
841 auto lit = item.get_literal ();
842 visit (path);
843 push (Rust::Token::make (COLON, item.get_locus ()));
844 visit (lit);
845 }
846
847 void
848 TokenCollector::visit (BorrowExpr &expr)
849 {
850 push (Rust::Token::make (AMP, expr.get_locus ()));
851 if (expr.get_is_double_borrow ())
852 push (Rust::Token::make (AMP, UNDEF_LOCATION));
853
854 if (expr.is_raw_borrow ())
855 {
856 push (Rust::Token::make_identifier (expr.get_locus (),
857 Values::WeakKeywords::RAW));
858 if (expr.get_is_mut ())
859 push (Rust::Token::make (MUT, UNDEF_LOCATION));
860 else
861 push (Rust::Token::make (CONST, UNDEF_LOCATION));
862 }
863 else
864 {
865 if (expr.get_is_mut ())
866 push (Rust::Token::make (MUT, UNDEF_LOCATION));
867 }
868
869 visit (expr.get_borrowed_expr ());
870 }
871
872 void
873 TokenCollector::visit (DereferenceExpr &expr)
874 {
875 push (Rust::Token::make (ASTERISK, expr.get_locus ()));
876 visit (expr.get_dereferenced_expr ());
877 }
878
879 void
880 TokenCollector::visit (ErrorPropagationExpr &expr)
881 {
882 visit (expr.get_propagating_expr ());
883 push (Rust::Token::make (QUESTION_MARK, expr.get_locus ()));
884 }
885
886 void
887 TokenCollector::visit (NegationExpr &expr)
888 {
889 switch (expr.get_expr_type ())
890 {
891 case NegationOperator::NEGATE:
892 push (Rust::Token::make (MINUS, expr.get_locus ()));
893 break;
894 case NegationOperator::NOT:
895 push (Rust::Token::make (EXCLAM, expr.get_locus ()));
896 break;
897 }
898 visit (expr.get_negated_expr ());
899 }
900
901 void
902 TokenCollector::visit (ArithmeticOrLogicalExpr &expr)
903 {
904 visit (expr.get_left_expr ());
905 switch (expr.get_expr_type ())
906 {
907 case ArithmeticOrLogicalOperator::ADD:
908 push (Rust::Token::make (PLUS, expr.get_locus ()));
909 break;
910
911 case ArithmeticOrLogicalOperator::SUBTRACT:
912 push (Rust::Token::make (MINUS, expr.get_locus ()));
913 break;
914
915 case ArithmeticOrLogicalOperator::MULTIPLY:
916 push (Rust::Token::make (ASTERISK, expr.get_locus ()));
917 break;
918
919 case ArithmeticOrLogicalOperator::DIVIDE:
920 push (Rust::Token::make (DIV, expr.get_locus ()));
921 break;
922
923 case ArithmeticOrLogicalOperator::MODULUS:
924 push (Rust::Token::make (PERCENT, expr.get_locus ()));
925 break;
926
927 case ArithmeticOrLogicalOperator::BITWISE_AND:
928 push (Rust::Token::make (AMP, expr.get_locus ()));
929 break;
930
931 case ArithmeticOrLogicalOperator::BITWISE_OR:
932 push (Rust::Token::make (PIPE, expr.get_locus ()));
933 break;
934
935 case ArithmeticOrLogicalOperator::BITWISE_XOR:
936 push (Rust::Token::make (CARET, expr.get_locus ()));
937 break;
938
939 case ArithmeticOrLogicalOperator::LEFT_SHIFT:
940 push (Rust::Token::make (LEFT_SHIFT, expr.get_locus ()));
941 break;
942
943 case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
944 push (Rust::Token::make (RIGHT_SHIFT, expr.get_locus ()));
945 break;
946 }
947
948 visit (expr.get_right_expr ());
949 }
950
951 void
952 TokenCollector::visit (ComparisonExpr &expr)
953 {
954 visit (expr.get_left_expr ());
955
956 switch (expr.get_expr_type ())
957 {
958 case ComparisonOperator::EQUAL:
959 push (Rust::Token::make (EQUAL_EQUAL, expr.get_locus ()));
960 break;
961 case ComparisonOperator::NOT_EQUAL:
962 push (Rust::Token::make (NOT_EQUAL, expr.get_locus ()));
963 break;
964 case ComparisonOperator::GREATER_THAN:
965 push (Rust::Token::make (RIGHT_ANGLE, expr.get_locus ()));
966 break;
967 case ComparisonOperator::LESS_THAN:
968 push (Rust::Token::make (LEFT_ANGLE, expr.get_locus ()));
969 break;
970 case ComparisonOperator::GREATER_OR_EQUAL:
971 push (Rust::Token::make (GREATER_OR_EQUAL, expr.get_locus ()));
972 break;
973
974 case ComparisonOperator::LESS_OR_EQUAL:
975 push (Rust::Token::make (LESS_OR_EQUAL, expr.get_locus ()));
976 break;
977 }
978 visit (expr.get_right_expr ());
979 }
980
981 void
982 TokenCollector::visit (LazyBooleanExpr &expr)
983 {
984 visit (expr.get_left_expr ());
985
986 switch (expr.get_expr_type ())
987 {
988 case LazyBooleanOperator::LOGICAL_AND:
989 push (Rust::Token::make (LOGICAL_AND, expr.get_locus ()));
990 break;
991 case LazyBooleanOperator::LOGICAL_OR:
992 push (Rust::Token::make (OR, expr.get_locus ()));
993 break;
994 }
995
996 visit (expr.get_right_expr ());
997 }
998
999 void
1000 TokenCollector::visit (TypeCastExpr &expr)
1001 {
1002 visit (expr.get_casted_expr ());
1003 push (Rust::Token::make (AS, expr.get_locus ()));
1004 visit (expr.get_type_to_cast_to ());
1005 }
1006
1007 void
1008 TokenCollector::visit (AssignmentExpr &expr)
1009 {
1010 expr.visit_lhs (*this);
1011 push (Rust::Token::make (EQUAL, expr.get_locus ()));
1012 expr.visit_rhs (*this);
1013 }
1014
1015 void
1016 TokenCollector::visit (CompoundAssignmentExpr &expr)
1017 {
1018 visit (expr.get_left_expr ());
1019
1020 switch (expr.get_expr_type ())
1021 {
1022 case CompoundAssignmentOperator::ADD:
1023 push (Rust::Token::make (PLUS_EQ, expr.get_locus ()));
1024 break;
1025 case CompoundAssignmentOperator::SUBTRACT:
1026 push (Rust::Token::make (MINUS_EQ, expr.get_locus ()));
1027 break;
1028 case CompoundAssignmentOperator::MULTIPLY:
1029 push (Rust::Token::make (ASTERISK_EQ, expr.get_locus ()));
1030 break;
1031 case CompoundAssignmentOperator::DIVIDE:
1032 push (Rust::Token::make (DIV_EQ, expr.get_locus ()));
1033 break;
1034 case CompoundAssignmentOperator::MODULUS:
1035 push (Rust::Token::make (PERCENT_EQ, expr.get_locus ()));
1036 break;
1037 case CompoundAssignmentOperator::BITWISE_AND:
1038 push (Rust::Token::make (AMP_EQ, expr.get_locus ()));
1039 break;
1040 case CompoundAssignmentOperator::BITWISE_OR:
1041 push (Rust::Token::make (PIPE_EQ, expr.get_locus ()));
1042 break;
1043 case CompoundAssignmentOperator::BITWISE_XOR:
1044 push (Rust::Token::make (CARET_EQ, expr.get_locus ()));
1045 break;
1046 case CompoundAssignmentOperator::LEFT_SHIFT:
1047 push (Rust::Token::make (LEFT_SHIFT_EQ, expr.get_locus ()));
1048 break;
1049 case CompoundAssignmentOperator::RIGHT_SHIFT:
1050 push (Rust::Token::make (RIGHT_SHIFT_EQ, expr.get_locus ()));
1051 break;
1052 }
1053 visit (expr.get_right_expr ());
1054 }
1055
1056 void
1057 TokenCollector::visit (GroupedExpr &expr)
1058 {
1059 push (Rust::Token::make (LEFT_PAREN, expr.get_locus ()));
1060 visit (expr.get_expr_in_parens ());
1061 push (Rust::Token::make (RIGHT_PAREN, expr.get_locus ()));
1062 }
1063
1064 void
1065 TokenCollector::visit (ArrayElemsValues &elems)
1066 {
1067 visit_items_joined_by_separator (elems.get_values (), COMMA);
1068 }
1069
1070 void
1071 TokenCollector::visit (ArrayElemsCopied &elems)
1072 {
1073 visit (elems.get_elem_to_copy ());
1074 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1075 visit (elems.get_num_copies ());
1076 }
1077
1078 void
1079 TokenCollector::visit (ArrayExpr &expr)
1080 {
1081 push (Rust::Token::make (LEFT_SQUARE, expr.get_locus ()));
1082 visit (expr.get_array_elems ());
1083 push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
1084 }
1085
1086 void
1087 TokenCollector::visit (ArrayIndexExpr &expr)
1088 {
1089 visit (expr.get_array_expr ());
1090 push (Rust::Token::make (LEFT_SQUARE, expr.get_locus ()));
1091 visit (expr.get_index_expr ());
1092 push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
1093 }
1094
1095 void
1096 TokenCollector::visit (TupleExpr &expr)
1097 {
1098 visit_items_as_lines (expr.get_outer_attrs ());
1099 push (Rust::Token::make (LEFT_PAREN, expr.get_locus ()));
1100 visit_items_joined_by_separator (expr.get_tuple_elems (), COMMA);
1101 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
1102 }
1103
1104 void
1105 TokenCollector::visit (TupleIndexExpr &expr)
1106 {
1107 visit (expr.get_tuple_expr ());
1108 push (Rust::Token::make (DOT, expr.get_locus ()));
1109 push (Rust::Token::make_int (UNDEF_LOCATION,
1110 std::to_string (expr.get_tuple_index ())));
1111 }
1112
1113 void
1114 TokenCollector::visit (StructExprStruct &expr)
1115 {
1116 visit (expr.get_struct_name ());
1117 }
1118
1119 void
1120 TokenCollector::visit (StructExprFieldIdentifier &expr)
1121 {
1122 visit_items_as_lines (expr.get_outer_attrs ());
1123 auto id = expr.get_field_name ().as_string ();
1124 push (Rust::Token::make_identifier (expr.get_locus (), std::move (id)));
1125 }
1126
1127 void
1128 TokenCollector::visit (StructExprFieldIdentifierValue &expr)
1129 {
1130 visit_items_as_lines (expr.get_outer_attrs ());
1131 auto id = expr.get_field_name ();
1132 push (Rust::Token::make_identifier (expr.get_locus (), std::move (id)));
1133 push (Rust::Token::make (COLON, UNDEF_LOCATION));
1134 visit (expr.get_value ());
1135 }
1136
1137 void
1138 TokenCollector::visit (StructExprFieldIndexValue &expr)
1139 {
1140 visit_items_as_lines (expr.get_outer_attrs ());
1141 push (Rust::Token::make_int (expr.get_locus (),
1142 std::to_string (expr.get_index ())));
1143 push (Rust::Token::make (COLON, UNDEF_LOCATION));
1144 visit (expr.get_value ());
1145 }
1146
1147 void
1148 TokenCollector::visit (StructBase &base)
1149 {
1150 push (Rust::Token::make (DOT_DOT, UNDEF_LOCATION));
1151 visit (base.get_base_struct ());
1152 }
1153
1154 void
1155 TokenCollector::visit (StructExprStructFields &expr)
1156 {
1157 visit (expr.get_struct_name ());
1158 push (Rust::Token::make (LEFT_CURLY, expr.get_locus ()));
1159 visit_items_joined_by_separator (expr.get_fields (), COMMA);
1160 if (expr.has_struct_base ())
1161 {
1162 push (Rust::Token::make (COMMA, UNDEF_LOCATION));
1163 visit (expr.get_struct_base ());
1164 }
1165 else
1166 {
1167 trailing_comma ();
1168 }
1169 push (Rust::Token::make (RIGHT_CURLY, expr.get_locus ()));
1170 }
1171
1172 void
1173 TokenCollector::visit (StructExprStructBase &)
1174 {
1175 // FIXME: Implement this node
1176 rust_unreachable ();
1177 }
1178
1179 void
1180 TokenCollector::visit (CallExpr &expr)
1181 {
1182 visit (expr.get_function_expr ());
1183
1184 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
1185
1186 visit_items_joined_by_separator (expr.get_params (), COMMA);
1187
1188 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
1189 }
1190
1191 void
1192 TokenCollector::visit (MethodCallExpr &expr)
1193 {
1194 visit (expr.get_receiver_expr ());
1195 push (Rust::Token::make (DOT, expr.get_locus ()));
1196 visit (expr.get_method_name ());
1197 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
1198 visit_items_joined_by_separator (expr.get_params (), COMMA);
1199 trailing_comma ();
1200 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
1201 }
1202
1203 void
1204 TokenCollector::visit (FieldAccessExpr &expr)
1205 {
1206 visit (expr.get_receiver_expr ());
1207 push (Rust::Token::make (DOT, expr.get_locus ()));
1208 auto field_name = expr.get_field_name ().as_string ();
1209 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (field_name)));
1210 }
1211
1212 void
1213 TokenCollector::visit (ClosureParam &param)
1214 {
1215 visit_items_as_lines (param.get_outer_attrs ());
1216 visit (param.get_pattern ());
1217 if (param.has_type_given ())
1218 {
1219 push (Rust::Token::make (COLON, param.get_locus ()));
1220 visit (param.get_type ());
1221 }
1222 }
1223
1224 void
1225 TokenCollector::visit_closure_common (ClosureExpr &expr)
1226 {
1227 if (expr.get_has_move ())
1228 {
1229 push (Rust::Token::make (MOVE, expr.get_locus ()));
1230 }
1231 push (Rust::Token::make (PIPE, UNDEF_LOCATION));
1232 visit_items_joined_by_separator (expr.get_params (), COMMA);
1233 push (Rust::Token::make (PIPE, UNDEF_LOCATION));
1234 }
1235
1236 void
1237 TokenCollector::visit (ClosureExprInner &expr)
1238 {
1239 visit_closure_common (expr);
1240 visit (expr.get_definition_expr ());
1241 }
1242
1243 void
1244 TokenCollector::visit (BlockExpr &expr)
1245 {
1246 visit_items_as_lines (expr.get_outer_attrs ());
1247 push (Rust::Token::make (LEFT_CURLY, expr.get_locus ()));
1248 newline ();
1249 increment_indentation ();
1250 visit_items_as_lines (expr.get_inner_attrs ());
1251
1252 visit_items_as_lines (expr.get_statements (), {});
1253
1254 if (expr.has_tail_expr ())
1255 {
1256 indentation ();
1257 visit (expr.get_tail_expr ());
1258 comment ("tail expr");
1259 newline ();
1260 }
1261
1262 decrement_indentation ();
1263 indentation ();
1264 push (Rust::Token::make (RIGHT_CURLY, expr.get_locus ()));
1265 newline ();
1266 }
1267
1268 void
1269 TokenCollector::visit (ClosureExprInnerTyped &expr)
1270 {
1271 visit_closure_common (expr);
1272 push (Rust::Token::make (RETURN_TYPE, expr.get_locus ()));
1273 visit (expr.get_return_type ());
1274 visit (expr.get_definition_block ());
1275 }
1276
1277 void
1278 TokenCollector::visit (ContinueExpr &expr)
1279 {
1280 push (Rust::Token::make (CONTINUE, expr.get_locus ()));
1281 if (expr.has_label ())
1282 visit (expr.get_label_unchecked ());
1283 }
1284
1285 void
1286 TokenCollector::visit (BreakExpr &expr)
1287 {
1288 push (Rust::Token::make (BREAK, expr.get_locus ()));
1289 if (expr.has_label ())
1290 visit (expr.get_label_unchecked ());
1291 if (expr.has_break_expr ())
1292 visit (expr.get_break_expr ());
1293 }
1294
1295 void
1296 TokenCollector::visit (RangeFromToExpr &expr)
1297 {
1298 visit (expr.get_from_expr ());
1299 push (Rust::Token::make (DOT_DOT, expr.get_locus ()));
1300 visit (expr.get_to_expr ());
1301 }
1302
1303 void
1304 TokenCollector::visit (RangeFromExpr &expr)
1305 {
1306 visit (expr.get_from_expr ());
1307 push (Rust::Token::make (DOT_DOT, expr.get_locus ()));
1308 }
1309
1310 void
1311 TokenCollector::visit (RangeToExpr &expr)
1312 {
1313 push (Rust::Token::make (DOT_DOT, expr.get_locus ()));
1314 visit (expr.get_to_expr ());
1315 }
1316
1317 void
1318 TokenCollector::visit (RangeFullExpr &expr)
1319 {
1320 push (Rust::Token::make (DOT_DOT, expr.get_locus ()));
1321 }
1322
1323 void
1324 TokenCollector::visit (RangeFromToInclExpr &expr)
1325 {
1326 visit (expr.get_from_expr ());
1327 push (Rust::Token::make (DOT_DOT_EQ, expr.get_locus ()));
1328 visit (expr.get_to_expr ());
1329 }
1330
1331 void
1332 TokenCollector::visit (RangeToInclExpr &expr)
1333 {
1334 push (Rust::Token::make (DOT_DOT_EQ, expr.get_locus ()));
1335 visit (expr.get_to_expr ());
1336 }
1337
1338 void
1339 TokenCollector::visit (BoxExpr &expr)
1340 {
1341 push (Rust::Token::make (BOX, expr.get_locus ()));
1342 visit (expr.get_boxed_expr ());
1343 }
1344
1345 void
1346 TokenCollector::visit (ReturnExpr &expr)
1347 {
1348 push (Rust::Token::make (RETURN_KW, expr.get_locus ()));
1349 if (expr.has_returned_expr ())
1350 visit (expr.get_returned_expr ());
1351 }
1352
1353 void
1354 TokenCollector::visit (UnsafeBlockExpr &expr)
1355 {
1356 push (Rust::Token::make (UNSAFE, expr.get_locus ()));
1357 visit (expr.get_block_expr ());
1358 }
1359
1360 void
1361 TokenCollector::visit (LoopLabel &label)
1362 {
1363 visit (label.get_lifetime ());
1364 push (Rust::Token::make (COLON, label.get_locus ()));
1365 }
1366
1367 void
1368 TokenCollector::visit_loop_common (BaseLoopExpr &expr)
1369 {
1370 if (expr.has_loop_label ())
1371 visit (expr.get_loop_label ());
1372 }
1373
1374 void
1375 TokenCollector::visit (LoopExpr &expr)
1376 {
1377 visit_loop_common (expr);
1378 push (Rust::Token::make (LOOP, expr.get_locus ()));
1379 visit (expr.get_loop_block ());
1380 }
1381
1382 void
1383 TokenCollector::visit (WhileLoopExpr &expr)
1384 {
1385 visit_loop_common (expr);
1386 push (Rust::Token::make (WHILE, expr.get_locus ()));
1387 visit (expr.get_predicate_expr ());
1388 visit (expr.get_loop_block ());
1389 }
1390
1391 void
1392 TokenCollector::visit (WhileLetLoopExpr &expr)
1393 {
1394 visit_loop_common (expr);
1395 push (Rust::Token::make (WHILE, expr.get_locus ()));
1396 push (Rust::Token::make (LET, UNDEF_LOCATION));
1397 // TODO: The reference mention only one Pattern
1398 for (auto &item : expr.get_patterns ())
1399 {
1400 visit (item);
1401 }
1402 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
1403 visit (expr.get_scrutinee_expr ());
1404 visit (expr.get_loop_block ());
1405 }
1406
1407 void
1408 TokenCollector::visit (ForLoopExpr &expr)
1409 {
1410 visit_loop_common (expr);
1411 push (Rust::Token::make (FOR, expr.get_locus ()));
1412 visit (expr.get_pattern ());
1413 push (Rust::Token::make (IN, UNDEF_LOCATION));
1414 visit (expr.get_iterator_expr ());
1415 visit (expr.get_loop_block ());
1416 }
1417
1418 void
1419 TokenCollector::visit (IfExpr &expr)
1420 {
1421 push (Rust::Token::make (IF, expr.get_locus ()));
1422 visit (expr.get_condition_expr ());
1423 visit (expr.get_if_block ());
1424 }
1425
1426 void
1427 TokenCollector::visit (IfExprConseqElse &expr)
1428 {
1429 visit (static_cast<IfExpr &> (expr));
1430 indentation ();
1431 push (Rust::Token::make (ELSE, expr.get_locus ()));
1432 visit (expr.get_else_block ());
1433 }
1434
1435 void
1436 TokenCollector::visit (IfLetExpr &expr)
1437 {
1438 push (Rust::Token::make (IF, expr.get_locus ()));
1439 push (Rust::Token::make (LET, UNDEF_LOCATION));
1440 for (auto &pattern : expr.get_patterns ())
1441 {
1442 visit (pattern);
1443 }
1444 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
1445 visit (expr.get_value_expr ());
1446 visit (expr.get_if_block ());
1447 }
1448
1449 void
1450 TokenCollector::visit (IfLetExprConseqElse &expr)
1451 {
1452 visit (static_cast<IfLetExpr &> (expr));
1453 indentation ();
1454 push (Rust::Token::make (ELSE, expr.get_locus ()));
1455 visit (expr.get_else_block ());
1456 }
1457
1458 void
1459 TokenCollector::visit (MatchArm &arm)
1460 {
1461 visit_items_as_lines (arm.get_outer_attrs ());
1462 for (auto &pattern : arm.get_patterns ())
1463 {
1464 visit (pattern);
1465 }
1466 if (arm.has_match_arm_guard ())
1467 {
1468 push (Rust::Token::make (IF, UNDEF_LOCATION));
1469 visit (arm.get_guard_expr ());
1470 }
1471 }
1472
1473 void
1474 TokenCollector::visit (MatchCase &match_case)
1475 {
1476 indentation ();
1477 visit (match_case.get_arm ());
1478 push (Rust::Token::make (MATCH_ARROW, UNDEF_LOCATION));
1479 visit (match_case.get_expr ());
1480 indentation ();
1481 push (Rust::Token::make (COMMA, UNDEF_LOCATION));
1482 newline ();
1483 }
1484
1485 void
1486 TokenCollector::visit (MatchExpr &expr)
1487 {
1488 push (Rust::Token::make (MATCH_KW, expr.get_locus ()));
1489 visit (expr.get_scrutinee_expr ());
1490 push (Rust::Token::make (LEFT_CURLY, UNDEF_LOCATION));
1491 newline ();
1492 increment_indentation ();
1493 visit_items_as_lines (expr.get_inner_attrs ());
1494 for (auto &arm : expr.get_match_cases ())
1495 {
1496 visit (arm);
1497 }
1498 decrement_indentation ();
1499 indentation ();
1500 push (Rust::Token::make (RIGHT_CURLY, UNDEF_LOCATION));
1501 }
1502
1503 void
1504 TokenCollector::visit (AwaitExpr &expr)
1505 {
1506 visit (expr.get_awaited_expr ());
1507 push (Rust::Token::make (DOT, expr.get_locus ()));
1508 // TODO: Check status of await keyword (Context dependant ?)
1509 push (Rust::Token::make_identifier (UNDEF_LOCATION, Values::Keywords::AWAIT));
1510 }
1511
1512 void
1513 TokenCollector::visit (AsyncBlockExpr &expr)
1514 {
1515 push (Rust::Token::make (ASYNC, expr.get_locus ()));
1516 if (expr.get_has_move ())
1517 push (Rust::Token::make (MOVE, UNDEF_LOCATION));
1518 visit (expr.get_block_expr ());
1519 }
1520
1521 void
1522 TokenCollector::visit (InlineAsm &expr)
1523 {}
1524
1525 // rust-item.h
1526
1527 void
1528 TokenCollector::visit (TypeParam &param)
1529 {
1530 // Syntax:
1531 // IDENTIFIER( : TypeParamBounds? )? ( = Type )?
1532 // TypeParamBounds :
1533 // TypeParamBound ( + TypeParamBound )* +?
1534
1535 visit_items_as_lines (param.get_outer_attrs ());
1536 auto id = param.get_type_representation ().as_string ();
1537 push (Rust::Token::make_identifier (param.get_locus (), std::move (id)));
1538 if (param.has_type_param_bounds ())
1539 {
1540 push (Rust::Token::make (COLON, UNDEF_LOCATION));
1541 visit_items_joined_by_separator (param.get_type_param_bounds (), PLUS);
1542 }
1543 if (param.has_type ())
1544 {
1545 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
1546 visit (param.get_type ());
1547 }
1548 }
1549
1550 void
1551 TokenCollector::visit (WhereClause &rule)
1552 {
1553 // Syntax:
1554 // where ( WhereClauseItem , )* WhereClauseItem ?
1555 // WhereClauseItem :
1556 // LifetimeWhereClauseItem
1557 // | TypeBoundWhereClauseItem
1558
1559 push (Rust::Token::make (WHERE, UNDEF_LOCATION));
1560 newline ();
1561 increment_indentation ();
1562 visit_items_joined_by_separator (rule.get_items (), COMMA);
1563 decrement_indentation ();
1564 }
1565
1566 void
1567 TokenCollector::visit (LifetimeWhereClauseItem &item)
1568 {
1569 // Syntax:
1570 // Lifetime : LifetimeBounds
1571 // LifetimeBounds :
1572 // ( Lifetime + )* Lifetime?
1573
1574 visit (item.get_lifetime ());
1575 push (Rust::Token::make (COLON, UNDEF_LOCATION));
1576 visit_items_joined_by_separator (item.get_lifetime_bounds (), PLUS);
1577 }
1578
1579 void
1580 TokenCollector::visit (TypeBoundWhereClauseItem &item)
1581 {
1582 // Syntax:
1583 // ForLifetimes? Type : TypeParamBounds?
1584 // TypeParamBounds :
1585 // TypeParamBound ( + TypeParamBound )* +?
1586 // TypeParamBound :
1587 // Lifetime | TraitBound
1588
1589 if (item.has_for_lifetimes ())
1590 visit (item.get_for_lifetimes ());
1591
1592 visit (item.get_type ());
1593
1594 push (Rust::Token::make (COLON, UNDEF_LOCATION));
1595 visit_items_joined_by_separator (item.get_type_param_bounds (), PLUS);
1596 }
1597
1598 void
1599 TokenCollector::visit (Module &module)
1600 {
1601 // Syntax:
1602 // mod IDENTIFIER ;
1603 // | mod IDENTIFIER {
1604 // InnerAttribute*
1605 // Item*
1606 // }
1607
1608 visit_items_as_lines (module.get_outer_attrs ());
1609 visit (module.get_visibility ());
1610 auto name = module.get_name ().as_string ();
1611 push (Rust::Token::make (MOD, module.get_locus ()));
1612 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (name)));
1613
1614 if (module.get_kind () == Module::UNLOADED)
1615 {
1616 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1617 newline ();
1618 }
1619 else /* Module::LOADED */
1620 {
1621 push (Rust::Token::make (LEFT_CURLY, UNDEF_LOCATION));
1622 newline ();
1623 increment_indentation ();
1624
1625 visit_items_as_lines (module.get_inner_attrs ());
1626 visit_items_as_lines (module.get_items ());
1627
1628 decrement_indentation ();
1629
1630 push (Rust::Token::make (RIGHT_CURLY, UNDEF_LOCATION));
1631 newline ();
1632 }
1633 }
1634
1635 void
1636 TokenCollector::visit (ExternCrate &crate)
1637 {
1638 visit_items_as_lines (crate.get_outer_attrs ());
1639 push (Rust::Token::make (EXTERN_KW, crate.get_locus ()));
1640 push (Rust::Token::make (CRATE, UNDEF_LOCATION));
1641 auto ref = crate.get_referenced_crate ();
1642 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (ref)));
1643 if (crate.has_as_clause ())
1644 {
1645 auto as_clause = crate.get_as_clause ();
1646 push (Rust::Token::make (AS, UNDEF_LOCATION));
1647 push (
1648 Rust::Token::make_identifier (UNDEF_LOCATION, std::move (as_clause)));
1649 }
1650 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1651 newline ();
1652 }
1653
1654 void
1655 TokenCollector::visit (UseTreeGlob &use_tree)
1656 {
1657 switch (use_tree.get_glob_type ())
1658 {
1659 case UseTreeGlob::PathType::PATH_PREFIXED: {
1660 auto path = use_tree.get_path ();
1661 visit (path);
1662 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
1663 }
1664 break;
1665 case UseTreeGlob::PathType::NO_PATH:
1666 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
1667 break;
1668 case UseTreeGlob::PathType::GLOBAL:
1669 break;
1670 }
1671 push (Rust::Token::make (ASTERISK, UNDEF_LOCATION));
1672 }
1673
1674 void
1675 TokenCollector::visit (UseTreeList &use_tree)
1676 {
1677 switch (use_tree.get_path_type ())
1678 {
1679 case UseTreeList::PathType::PATH_PREFIXED: {
1680 auto path = use_tree.get_path ();
1681 visit (path);
1682 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
1683 }
1684 break;
1685 case UseTreeList::PathType::NO_PATH:
1686 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
1687 break;
1688 case UseTreeList::PathType::GLOBAL:
1689 break;
1690 }
1691
1692 push (Rust::Token::make (LEFT_CURLY, UNDEF_LOCATION));
1693 if (use_tree.has_trees ())
1694 {
1695 visit_items_joined_by_separator (use_tree.get_trees (), COMMA);
1696 }
1697 push (Rust::Token::make (RIGHT_CURLY, UNDEF_LOCATION));
1698 }
1699
1700 void
1701 TokenCollector::visit (UseTreeRebind &use_tree)
1702 {
1703 auto path = use_tree.get_path ();
1704 visit (path);
1705 switch (use_tree.get_new_bind_type ())
1706 {
1707 case UseTreeRebind::NewBindType::IDENTIFIER: {
1708 push (Rust::Token::make (AS, UNDEF_LOCATION));
1709 auto id = use_tree.get_identifier ().as_string ();
1710 push (
1711 Rust::Token::make_identifier (use_tree.get_locus (), std::move (id)));
1712 }
1713 break;
1714 case UseTreeRebind::NewBindType::WILDCARD:
1715 push (Rust::Token::make (AS, UNDEF_LOCATION));
1716 push (Rust::Token::make (UNDERSCORE, use_tree.get_locus ()));
1717 break;
1718 case UseTreeRebind::NewBindType::NONE:
1719 break;
1720 }
1721 }
1722
1723 void
1724 TokenCollector::visit (UseDeclaration &decl)
1725 {
1726 visit_items_as_lines (decl.get_outer_attrs ());
1727 push (Rust::Token::make (USE, decl.get_locus ()));
1728 visit (*decl.get_tree ());
1729 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1730 newline ();
1731 }
1732
1733 void
1734 TokenCollector::visit (Function &function)
1735 {
1736 // Syntax:
1737 // FunctionQualifiers fn IDENTIFIER GenericParams?
1738 // ( FunctionParameters? )
1739 // FunctionReturnType? WhereClause?
1740 // ( BlockExpression | ; )
1741 visit_items_as_lines (function.get_outer_attrs ());
1742
1743 visit (function.get_visibility ());
1744 auto qualifiers = function.get_qualifiers ();
1745 visit (qualifiers);
1746
1747 push (Rust::Token::make (FN_KW, function.get_locus ()));
1748 auto name = function.get_function_name ().as_string ();
1749 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (name)));
1750 if (function.has_generics ())
1751 visit (function.get_generic_params ());
1752
1753 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
1754
1755 visit_items_joined_by_separator (function.get_function_params ());
1756 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
1757
1758 if (function.has_return_type ())
1759 {
1760 push (Rust::Token::make (RETURN_TYPE, UNDEF_LOCATION));
1761 visit (function.get_return_type ());
1762 }
1763
1764 if (function.has_where_clause ())
1765 visit (function.get_where_clause ());
1766
1767 if (function.has_body ())
1768 visit (*function.get_definition ());
1769 else
1770 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1771 newline ();
1772 }
1773
1774 void
1775 TokenCollector::visit (TypeAlias &type_alias)
1776 {
1777 // Syntax:
1778 // Visibility? type IDENTIFIER GenericParams? WhereClause? = Type;
1779
1780 // Note: Associated types are handled by `AST::TraitItemType`.
1781
1782 visit_items_as_lines (type_alias.get_outer_attrs ());
1783 if (type_alias.has_visibility ())
1784 visit (type_alias.get_visibility ());
1785 auto alias_name = type_alias.get_new_type_name ().as_string ();
1786 push (Rust::Token::make (TYPE, type_alias.get_locus ()));
1787 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (alias_name)));
1788
1789 if (type_alias.has_generics ())
1790 visit (type_alias.get_generic_params ());
1791
1792 if (type_alias.has_where_clause ())
1793 visit (type_alias.get_where_clause ());
1794
1795 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
1796 visit (type_alias.get_type_aliased ());
1797 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1798 }
1799
1800 void
1801 TokenCollector::visit (StructStruct &struct_item)
1802 {
1803 visit_items_as_lines (struct_item.get_outer_attrs ());
1804 if (struct_item.has_visibility ())
1805 visit (struct_item.get_visibility ());
1806 auto struct_name = struct_item.get_identifier ().as_string ();
1807 push (Rust::Token::make (STRUCT_KW, struct_item.get_locus ()));
1808 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (struct_name)));
1809
1810 if (struct_item.has_generics ())
1811 visit (struct_item.get_generic_params ());
1812 if (struct_item.has_where_clause ())
1813 visit (struct_item.get_where_clause ());
1814 if (struct_item.is_unit_struct ())
1815 {
1816 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1817 newline ();
1818 }
1819 else
1820 visit_items_as_block (struct_item.get_fields (),
1821 {Rust::Token::make (COMMA, UNDEF_LOCATION)});
1822 }
1823
1824 void
1825 TokenCollector::visit (TupleStruct &tuple_struct)
1826 {
1827 visit_items_as_lines (tuple_struct.get_outer_attrs ());
1828 auto struct_name = tuple_struct.get_identifier ().as_string ();
1829 push (Rust::Token::make (STRUCT_KW, tuple_struct.get_locus ()));
1830 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (struct_name)));
1831 if (tuple_struct.has_generics ())
1832 visit (tuple_struct.get_generic_params ());
1833 if (tuple_struct.has_where_clause ())
1834 visit (tuple_struct.get_where_clause ());
1835
1836 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
1837 visit_items_joined_by_separator (tuple_struct.get_fields (), COMMA);
1838 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
1839 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1840 newline ();
1841 }
1842
1843 void
1844 TokenCollector::visit (EnumItem &item)
1845 {
1846 visit_items_as_lines (item.get_outer_attrs ());
1847 auto id = item.get_identifier ().as_string ();
1848 push (Rust::Token::make_identifier (item.get_locus (), std::move (id)));
1849 }
1850
1851 void
1852 TokenCollector::visit (EnumItemTuple &item)
1853 {
1854 auto id = item.get_identifier ().as_string ();
1855 push (Rust::Token::make_identifier (item.get_locus (), std::move (id)));
1856 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
1857 visit_items_joined_by_separator (item.get_tuple_fields (), COMMA);
1858 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
1859 }
1860
1861 void
1862 TokenCollector::visit (EnumItemStruct &item)
1863 {
1864 auto id = item.get_identifier ().as_string ();
1865 push (Rust::Token::make_identifier (item.get_locus (), std::move (id)));
1866 visit_items_as_block (item.get_struct_fields (),
1867 {Rust::Token::make (COMMA, UNDEF_LOCATION)});
1868 }
1869
1870 void
1871 TokenCollector::visit (EnumItemDiscriminant &item)
1872 {
1873 auto id = item.get_identifier ().as_string ();
1874 push (Rust::Token::make_identifier (item.get_locus (), std::move (id)));
1875 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
1876 visit (item.get_expr ());
1877 }
1878
1879 void
1880 TokenCollector::visit (Enum &enumeration)
1881 {
1882 visit_items_as_lines (enumeration.get_outer_attrs ());
1883 if (enumeration.has_visibility ())
1884 visit (enumeration.get_visibility ());
1885 push (Rust::Token::make (ENUM_KW, enumeration.get_locus ()));
1886 auto id = enumeration.get_identifier ().as_string ();
1887 push (
1888 Rust::Token::make_identifier (enumeration.get_locus (), std::move (id)));
1889 if (enumeration.has_generics ())
1890 visit (enumeration.get_generic_params ());
1891 if (enumeration.has_where_clause ())
1892 visit (enumeration.get_where_clause ());
1893
1894 visit_items_as_block (enumeration.get_variants (),
1895 {Rust::Token::make (COMMA, UNDEF_LOCATION)});
1896 }
1897
1898 void
1899 TokenCollector::visit (Union &union_item)
1900 {
1901 visit_items_as_lines (union_item.get_outer_attrs ());
1902 auto id = union_item.get_identifier ().as_string ();
1903 push (Rust::Token::make_identifier (union_item.get_locus (),
1904 Values::WeakKeywords::UNION));
1905 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
1906
1907 if (union_item.has_generics ())
1908 visit (union_item.get_generic_params ());
1909
1910 if (union_item.has_where_clause ())
1911 visit (union_item.get_where_clause ());
1912
1913 visit_items_as_block (union_item.get_variants (),
1914 {Rust::Token::make (COMMA, UNDEF_LOCATION)});
1915 }
1916
1917 void
1918 TokenCollector::visit (ConstantItem &item)
1919 {
1920 visit_items_as_lines (item.get_outer_attrs ());
1921 push (Rust::Token::make (CONST, item.get_locus ()));
1922 if (item.is_unnamed ())
1923 {
1924 push (Rust::Token::make (UNDERSCORE, UNDEF_LOCATION));
1925 }
1926 else
1927 {
1928 auto id = item.get_identifier ();
1929 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
1930 }
1931 push (Rust::Token::make (COLON, UNDEF_LOCATION));
1932 visit (item.get_type ());
1933 if (item.has_expr ())
1934 {
1935 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
1936 visit (item.get_expr ());
1937 }
1938 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1939 }
1940
1941 void
1942 TokenCollector::visit (StaticItem &item)
1943 {
1944 visit_items_as_lines (item.get_outer_attrs ());
1945 push (Rust::Token::make (STATIC_KW, item.get_locus ()));
1946 if (item.is_mutable ())
1947 push (Rust::Token::make (MUT, UNDEF_LOCATION));
1948
1949 auto id = item.get_identifier ().as_string ();
1950 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
1951 push (Rust::Token::make (COLON, UNDEF_LOCATION));
1952
1953 visit (item.get_type ());
1954
1955 if (item.has_expr ())
1956 {
1957 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
1958 visit (item.get_expr ());
1959 }
1960 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1961 }
1962
1963 void
1964 TokenCollector::visit_function_common (std::unique_ptr<Type> &return_type,
1965 std::unique_ptr<BlockExpr> &block)
1966 {
1967 // FIXME: This should format the `<vis> fn <name> ( [args] )` as well
1968 if (return_type)
1969 {
1970 push (Rust::Token::make (RETURN_TYPE, UNDEF_LOCATION));
1971 visit (return_type);
1972 }
1973
1974 if (block)
1975 {
1976 visit (block);
1977 }
1978 else
1979 {
1980 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
1981 newline ();
1982 }
1983 }
1984
1985 void
1986 TokenCollector::visit (SelfParam &param)
1987 {
1988 if (param.get_has_ref ())
1989 {
1990 push (Rust::Token::make (AMP, UNDEF_LOCATION));
1991 if (param.has_lifetime ())
1992 {
1993 auto lifetime = param.get_lifetime ();
1994 visit (lifetime);
1995 }
1996 if (param.get_is_mut ())
1997 push (Rust::Token::make (MUT, UNDEF_LOCATION));
1998 }
1999 push (Rust::Token::make (SELF, UNDEF_LOCATION));
2000 if (param.has_type ())
2001 {
2002 push (Rust::Token::make (COLON, UNDEF_LOCATION));
2003 visit (param.get_type ());
2004 }
2005 }
2006
2007 void
2008 TokenCollector::visit (TraitItemConst &item)
2009 {
2010 auto id = item.get_identifier ().as_string ();
2011 indentation ();
2012 push (Rust::Token::make (CONST, item.get_locus ()));
2013 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
2014 push (Rust::Token::make (COLON, UNDEF_LOCATION));
2015 visit (item.get_type ());
2016 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
2017 newline ();
2018 }
2019
2020 void
2021 TokenCollector::visit (TraitItemType &item)
2022 {
2023 visit_items_as_lines (item.get_outer_attrs ());
2024 auto id = item.get_identifier ().as_string ();
2025 indentation ();
2026
2027 push (Rust::Token::make (TYPE, item.get_locus ()));
2028 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
2029 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
2030 newline ();
2031 }
2032
2033 void
2034 TokenCollector::visit (Trait &trait)
2035 {
2036 for (auto &attr : trait.get_outer_attrs ())
2037 {
2038 visit (attr);
2039 newline ();
2040 indentation ();
2041 }
2042
2043 visit (trait.get_visibility ());
2044
2045 auto id = trait.get_identifier ().as_string ();
2046 push (Rust::Token::make (TRAIT, trait.get_locus ()));
2047 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
2048
2049 visit (trait.get_generic_params ());
2050
2051 visit_items_as_block (trait.get_trait_items (), {});
2052 }
2053
2054 void
2055 TokenCollector::visit (InherentImpl &impl)
2056 {
2057 visit_items_as_lines (impl.get_outer_attrs ());
2058 push (Rust::Token::make (IMPL, impl.get_locus ()));
2059 visit (impl.get_generic_params ());
2060
2061 visit (impl.get_type ());
2062
2063 if (impl.has_where_clause ())
2064 visit (impl.get_where_clause ());
2065
2066 // FIXME: Handle inner attributes
2067
2068 visit_items_as_block (impl.get_impl_items (), {});
2069 }
2070
2071 void
2072 TokenCollector::visit (TraitImpl &impl)
2073 {
2074 visit_items_as_lines (impl.get_outer_attrs ());
2075 push (Rust::Token::make (IMPL, impl.get_locus ()));
2076 visit (impl.get_generic_params ());
2077 if (impl.is_exclam ())
2078 push (Rust::Token::make (EXCLAM, UNDEF_LOCATION));
2079 visit (impl.get_trait_path ());
2080 push (Rust::Token::make (FOR, UNDEF_LOCATION));
2081 visit (impl.get_type ());
2082
2083 if (impl.has_where_clause ())
2084 visit (impl.get_where_clause ());
2085
2086 visit_items_as_block (impl.get_impl_items ());
2087 }
2088
2089 void
2090 TokenCollector::visit (ExternalTypeItem &type)
2091 {
2092 visit (type.get_visibility ());
2093
2094 auto id = type.get_identifier ().as_string ();
2095
2096 push (Rust::Token::make (TYPE, UNDEF_LOCATION));
2097 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
2098 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
2099 }
2100
2101 void
2102 TokenCollector::visit (ExternalStaticItem &item)
2103 {
2104 auto id = item.get_identifier ().as_string ();
2105 visit_items_as_lines (item.get_outer_attrs ());
2106 if (item.has_visibility ())
2107 visit (item.get_visibility ());
2108 push (Rust::Token::make (STATIC_KW, item.get_locus ()));
2109 if (item.is_mut ())
2110 push (Rust::Token::make (MUT, UNDEF_LOCATION));
2111 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
2112 push (Rust::Token::make (COLON, UNDEF_LOCATION));
2113 visit (item.get_type ());
2114 // TODO: No expr ? The "(= Expression)?" part from the reference seems missing
2115 // in the ast.
2116 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
2117 }
2118
2119 void
2120 TokenCollector::visit (ExternBlock &block)
2121 {
2122 visit_items_as_lines (block.get_outer_attrs ());
2123 push (Rust::Token::make (EXTERN_KW, block.get_locus ()));
2124
2125 if (block.has_abi ())
2126 {
2127 auto abi = block.get_abi ();
2128 push (Rust::Token::make_string (UNDEF_LOCATION, std::move (abi)));
2129 }
2130
2131 visit_items_as_block (block.get_extern_items (), {});
2132 }
2133
2134 static std::pair<TokenId, TokenId>
2135 get_delimiters (DelimType delim)
2136 {
2137 switch (delim)
2138 {
2139 case PARENS:
2140 return {LEFT_PAREN, RIGHT_PAREN};
2141 case SQUARE:
2142 return {LEFT_SQUARE, RIGHT_SQUARE};
2143 case CURLY:
2144 return {LEFT_CURLY, RIGHT_CURLY};
2145 default:
2146 rust_unreachable ();
2147 }
2148 }
2149
2150 void
2151 TokenCollector::visit (MacroMatchFragment &match)
2152 {
2153 auto id = match.get_ident ().as_string ();
2154 auto frag_spec = match.get_frag_spec ().as_string ();
2155 push (Rust::Token::make (DOLLAR_SIGN, UNDEF_LOCATION));
2156 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
2157 push (Rust::Token::make (COLON, UNDEF_LOCATION));
2158 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (frag_spec)));
2159 }
2160
2161 void
2162 TokenCollector::visit (MacroMatchRepetition &repetition)
2163 {
2164 push (Rust::Token::make (DOLLAR_SIGN, UNDEF_LOCATION));
2165 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
2166
2167 for (auto &match : repetition.get_matches ())
2168 {
2169 visit (match);
2170 }
2171
2172 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2173
2174 if (repetition.has_sep ())
2175 {
2176 push (Rust::Token::make (repetition.get_sep ()->get_id (),
2177 repetition.get_sep ()->get_locus ()));
2178 }
2179 switch (repetition.get_op ())
2180 {
2181 case MacroMatchRepetition::ANY:
2182 push (Rust::Token::make (ASTERISK, UNDEF_LOCATION));
2183 break;
2184 case MacroMatchRepetition::ONE_OR_MORE:
2185 push (Rust::Token::make (PLUS, UNDEF_LOCATION));
2186 break;
2187 case MacroMatchRepetition::ZERO_OR_ONE:
2188 push (Rust::Token::make (QUESTION_MARK, UNDEF_LOCATION));
2189 break;
2190 case MacroMatchRepetition::NONE:
2191 break;
2192 }
2193 }
2194
2195 void
2196 TokenCollector::visit (MacroMatcher &matcher)
2197 {
2198 auto delimiters = get_delimiters (matcher.get_delim_type ());
2199
2200 push (Rust::Token::make (delimiters.first, UNDEF_LOCATION));
2201
2202 for (auto &item : matcher.get_matches ())
2203 {
2204 visit (item);
2205 }
2206
2207 push (Rust::Token::make (delimiters.second, UNDEF_LOCATION));
2208 }
2209
2210 void
2211 TokenCollector::visit (MacroRule &rule)
2212 {
2213 visit (rule.get_matcher ());
2214 push (Rust::Token::make (MATCH_ARROW, rule.get_locus ()));
2215 visit (rule.get_transcriber ().get_token_tree ());
2216 }
2217
2218 void
2219 TokenCollector::visit (MacroRulesDefinition &rules_def)
2220 {
2221 for (auto &outer_attr : rules_def.get_outer_attrs ())
2222 visit (outer_attr);
2223
2224 auto rule_name = rules_def.get_rule_name ().as_string ();
2225
2226 push (Rust::Token::make_identifier (rules_def.get_locus (),
2227 Values::WeakKeywords::MACRO_RULES));
2228 push (Rust::Token::make (EXCLAM, UNDEF_LOCATION));
2229
2230 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (rule_name)));
2231
2232 visit_items_as_block (rules_def.get_rules (),
2233 {Rust::Token::make (SEMICOLON, UNDEF_LOCATION)});
2234 }
2235
2236 void
2237 TokenCollector::visit (MacroInvocation &invocation)
2238 {
2239 auto data = invocation.get_invoc_data ();
2240 visit (data.get_path ());
2241 push (Rust::Token::make (EXCLAM, UNDEF_LOCATION));
2242 visit (data.get_delim_tok_tree ());
2243 if (invocation.has_semicolon ())
2244 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
2245 }
2246
2247 void
2248 TokenCollector::visit (MetaItemPath &item)
2249 {
2250 auto path = item.to_path_item ();
2251 visit (path);
2252 }
2253
2254 void
2255 TokenCollector::visit (MetaItemSeq &item)
2256 {
2257 visit (item.get_path ());
2258 // TODO: Double check this, there is probably a mistake.
2259 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
2260 visit_items_joined_by_separator (item.get_seq (), COMMA);
2261 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2262 }
2263
2264 void
2265 TokenCollector::visit (MetaWord &word)
2266 {
2267 auto id = word.get_ident ().as_string ();
2268
2269 push (Rust::Token::make_identifier (word.get_locus (), std::move (id)));
2270 }
2271
2272 void
2273 TokenCollector::visit (MetaNameValueStr &name)
2274 {
2275 auto pair = name.get_name_value_pair ();
2276 auto id = std::get<0> (pair).as_string ();
2277 auto value = std::get<1> (pair);
2278
2279 push (Rust::Token::make_identifier (name.get_locus (), std::move (id)));
2280 push (Rust::Token::make (EQUAL, name.get_locus ()));
2281 push (Rust::Token::make (DOUBLE_QUOTE, UNDEF_LOCATION));
2282 push (Rust::Token::make_identifier (name.get_locus (), std::move (value)));
2283 push (Rust::Token::make (DOUBLE_QUOTE, UNDEF_LOCATION));
2284 }
2285
2286 void
2287 TokenCollector::visit (MetaListPaths &list)
2288 {
2289 auto id = list.get_ident ().as_string ();
2290
2291 push (Rust::Token::make_identifier (list.get_locus (), std::move (id)));
2292 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
2293
2294 visit_items_joined_by_separator (list.get_paths (), COMMA);
2295
2296 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2297 }
2298
2299 void
2300 TokenCollector::visit (MetaListNameValueStr &list)
2301 {
2302 auto id = list.get_ident ().as_string ();
2303
2304 push (Rust::Token::make_identifier (list.get_locus (), std::move (id)));
2305 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
2306
2307 visit_items_joined_by_separator (list.get_values (), COMMA);
2308
2309 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2310 }
2311
2312 // rust-pattern.h
2313 void
2314 TokenCollector::visit (LiteralPattern &pattern)
2315 {
2316 visit (pattern.get_literal (), pattern.get_locus ());
2317 }
2318
2319 void
2320 TokenCollector::visit (IdentifierPattern &pattern)
2321 {
2322 if (pattern.get_is_ref ())
2323 {
2324 push (Rust::Token::make (REF, pattern.get_locus ()));
2325 }
2326 if (pattern.get_is_mut ())
2327 {
2328 push (Rust::Token::make (MUT, UNDEF_LOCATION));
2329 }
2330
2331 auto id = pattern.get_ident ().as_string ();
2332 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
2333
2334 if (pattern.has_pattern_to_bind ())
2335 {
2336 push (Rust::Token::make (PATTERN_BIND, UNDEF_LOCATION));
2337 visit (pattern.get_pattern_to_bind ());
2338 }
2339 }
2340
2341 void
2342 TokenCollector::visit (WildcardPattern &pattern)
2343 {
2344 push (Rust::Token::make (UNDERSCORE, pattern.get_locus ()));
2345 }
2346
2347 void
2348 TokenCollector::visit (RestPattern &pattern)
2349 {
2350 push (Rust::Token::make (DOT_DOT, pattern.get_locus ()));
2351 }
2352
2353 // void TokenCollector::visit(RangePatternBound& ){}
2354
2355 void
2356 TokenCollector::visit (RangePatternBoundLiteral &pattern)
2357 {
2358 if (pattern.get_has_minus ())
2359 {
2360 push (Rust::Token::make (MINUS, pattern.get_locus ()));
2361 }
2362 auto literal = pattern.get_literal ();
2363 visit (literal);
2364 }
2365
2366 void
2367 TokenCollector::visit (RangePatternBoundPath &pattern)
2368 {
2369 visit (pattern.get_path ());
2370 }
2371
2372 void
2373 TokenCollector::visit (RangePatternBoundQualPath &pattern)
2374 {
2375 visit (pattern.get_qualified_path ());
2376 }
2377
2378 void
2379 TokenCollector::visit (RangePattern &pattern)
2380 {
2381 if (pattern.get_has_lower_bound () && pattern.get_has_upper_bound ())
2382 {
2383 visit (pattern.get_lower_bound ());
2384 if (pattern.get_has_ellipsis_syntax ())
2385 push (Rust::Token::make (ELLIPSIS, pattern.get_locus ()));
2386 else
2387 push (Rust::Token::make (DOT_DOT_EQ, pattern.get_locus ()));
2388 visit (pattern.get_upper_bound ());
2389 }
2390 else if (pattern.get_has_lower_bound ())
2391 {
2392 visit (pattern.get_lower_bound ());
2393 push (Rust::Token::make (DOT_DOT, pattern.get_locus ()));
2394 }
2395 else
2396 {
2397 push (Rust::Token::make (DOT_DOT_EQ, pattern.get_locus ()));
2398 visit (pattern.get_upper_bound ());
2399 }
2400 }
2401
2402 void
2403 TokenCollector::visit (ReferencePattern &pattern)
2404 {
2405 if (pattern.is_double_reference ())
2406 {
2407 push (Rust::Token::make (LOGICAL_AND, pattern.get_locus ()));
2408 }
2409 else
2410 {
2411 push (Rust::Token::make (AMP, pattern.get_locus ()));
2412 }
2413
2414 if (pattern.get_is_mut ())
2415 {
2416 push (Rust::Token::make (MUT, UNDEF_LOCATION));
2417 }
2418
2419 visit (pattern.get_referenced_pattern ());
2420 }
2421
2422 // void TokenCollector::visit(StructPatternField& ){}
2423
2424 void
2425 TokenCollector::visit (StructPatternFieldTuplePat &pattern)
2426 {
2427 visit_items_as_lines (pattern.get_outer_attrs ());
2428 push (Rust::Token::make_int (pattern.get_locus (),
2429 std::to_string (pattern.get_index ())));
2430 push (Rust::Token::make (COLON, pattern.get_locus ()));
2431 visit (pattern.get_index_pattern ());
2432 }
2433
2434 void
2435 TokenCollector::visit (StructPatternFieldIdentPat &pattern)
2436 {
2437 visit_items_as_lines (pattern.get_outer_attrs ());
2438
2439 auto id = pattern.get_identifier ().as_string ();
2440 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
2441
2442 push (Rust::Token::make (COLON, pattern.get_locus ()));
2443
2444 visit (pattern.get_ident_pattern ());
2445 }
2446
2447 void
2448 TokenCollector::visit (StructPatternFieldIdent &pattern)
2449 {
2450 visit_items_as_lines (pattern.get_outer_attrs ());
2451 if (pattern.is_ref ())
2452 push (Rust::Token::make (REF, UNDEF_LOCATION));
2453 if (pattern.is_mut ())
2454 push (Rust::Token::make (MUT, UNDEF_LOCATION));
2455
2456 auto id = pattern.get_identifier ().as_string ();
2457 push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
2458 }
2459
2460 void
2461 TokenCollector::visit (StructPattern &pattern)
2462 {
2463 visit (pattern.get_path ());
2464 push (Rust::Token::make (LEFT_CURLY, pattern.get_locus ()));
2465 auto elems = pattern.get_struct_pattern_elems ();
2466 if (elems.has_struct_pattern_fields ())
2467 {
2468 visit_items_joined_by_separator (elems.get_struct_pattern_fields ());
2469 if (elems.has_etc ())
2470 {
2471 push (Rust::Token::make (COMMA, UNDEF_LOCATION));
2472 visit_items_as_lines (elems.get_etc_outer_attrs ());
2473 }
2474 }
2475 else
2476 {
2477 visit_items_as_lines (elems.get_etc_outer_attrs ());
2478 }
2479
2480 push (Rust::Token::make (RIGHT_CURLY, UNDEF_LOCATION));
2481 }
2482
2483 // void TokenCollector::visit(TupleStructItems& ){}
2484
2485 void
2486 TokenCollector::visit (TupleStructItemsNoRange &pattern)
2487 {
2488 visit_items_joined_by_separator (pattern.get_patterns ());
2489 }
2490
2491 void
2492 TokenCollector::visit (TupleStructItemsRange &pattern)
2493 {
2494 for (auto &lower : pattern.get_lower_patterns ())
2495 {
2496 visit (lower);
2497 }
2498 push (Rust::Token::make (DOT_DOT, UNDEF_LOCATION));
2499 for (auto &upper : pattern.get_lower_patterns ())
2500 {
2501 visit (upper);
2502 }
2503 }
2504
2505 void
2506 TokenCollector::visit (TupleStructPattern &pattern)
2507 {
2508 visit (pattern.get_path ());
2509 push (Rust::Token::make (LEFT_PAREN, pattern.get_locus ()));
2510 visit (pattern.get_items ());
2511 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2512 }
2513
2514 // void
2515 // TokenCollector::visit (TuplePatternItems &)
2516 // {}
2517
2518 void
2519 TokenCollector::visit (TuplePatternItemsMultiple &pattern)
2520 {
2521 visit_items_joined_by_separator (pattern.get_patterns (), COMMA);
2522 }
2523
2524 void
2525 TokenCollector::visit (TuplePatternItemsRanged &pattern)
2526 {
2527 for (auto &lower : pattern.get_lower_patterns ())
2528 {
2529 visit (lower);
2530 }
2531 push (Rust::Token::make (DOT_DOT, UNDEF_LOCATION));
2532 for (auto &upper : pattern.get_lower_patterns ())
2533 {
2534 visit (upper);
2535 }
2536 }
2537
2538 void
2539 TokenCollector::visit (TuplePattern &pattern)
2540 {
2541 push (Rust::Token::make (LEFT_PAREN, pattern.get_locus ()));
2542 visit (pattern.get_items ());
2543 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2544 }
2545
2546 void
2547 TokenCollector::visit (GroupedPattern &pattern)
2548 {
2549 push (Rust::Token::make (LEFT_PAREN, pattern.get_locus ()));
2550 visit (pattern.get_pattern_in_parens ());
2551 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2552 }
2553
2554 void
2555 TokenCollector::visit (SlicePattern &pattern)
2556 {
2557 push (Rust::Token::make (LEFT_SQUARE, pattern.get_locus ()));
2558 visit_items_joined_by_separator (pattern.get_items (), COMMA);
2559 push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
2560 }
2561
2562 void
2563 TokenCollector::visit (AltPattern &pattern)
2564 {
2565 visit_items_joined_by_separator (pattern.get_alts (), PIPE);
2566 }
2567
2568 // rust-stmt.h
2569 void
2570 TokenCollector::visit (EmptyStmt &)
2571 {}
2572
2573 void
2574 TokenCollector::visit (LetStmt &stmt)
2575 {
2576 push (Rust::Token::make (LET, stmt.get_locus ()));
2577 auto &pattern = stmt.get_pattern ();
2578 visit (pattern);
2579
2580 if (stmt.has_type ())
2581 {
2582 push (Rust::Token::make (COLON, UNDEF_LOCATION));
2583 visit (stmt.get_type ());
2584 }
2585
2586 if (stmt.has_init_expr ())
2587 {
2588 push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
2589 visit (stmt.get_init_expr ());
2590 }
2591
2592 if (stmt.has_else_expr ())
2593 {
2594 push (Rust::Token::make (ELSE, UNDEF_LOCATION));
2595 visit (stmt.get_else_expr ());
2596 }
2597
2598 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
2599 }
2600
2601 void
2602 TokenCollector::visit (ExprStmt &stmt)
2603 {
2604 visit (stmt.get_expr ());
2605 if (stmt.is_semicolon_followed ())
2606 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
2607 }
2608
2609 // rust-type.h
2610 void
2611 TokenCollector::visit (TraitBound &bound)
2612 {
2613 // Syntax:
2614 // ?? ForLifetimes? TypePath
2615 // | ( ?? ForLifetimes? TypePath )
2616
2617 if (bound.has_opening_question_mark ())
2618 push (Rust::Token::make (QUESTION_MARK, bound.get_locus ()));
2619
2620 if (bound.has_for_lifetimes ())
2621 visit (bound.get_for_lifetimes ());
2622
2623 visit (bound.get_type_path ());
2624 }
2625
2626 void
2627 TokenCollector::visit (ImplTraitType &type)
2628 {
2629 // Syntax:
2630 // impl TypeParamBounds
2631 // TypeParamBounds :
2632 // TypeParamBound ( + TypeParamBound )* +?
2633
2634 push (Rust::Token::make (IMPL, type.get_locus ()));
2635 visit_items_joined_by_separator (type.get_type_param_bounds (), PLUS);
2636 }
2637
2638 void
2639 TokenCollector::visit (TraitObjectType &type)
2640 {
2641 // Syntax:
2642 // dyn? TypeParamBounds
2643 // TypeParamBounds :
2644 // TypeParamBound ( + TypeParamBound )* +?
2645
2646 if (type.is_dyn ())
2647 push (Rust::Token::make (DYN, type.get_locus ()));
2648 visit_items_joined_by_separator (type.get_type_param_bounds (), PLUS);
2649 }
2650
2651 void
2652 TokenCollector::visit (ParenthesisedType &type)
2653 {
2654 // Syntax:
2655 // ( Type )
2656
2657 push (Rust::Token::make (LEFT_PAREN, type.get_locus ()));
2658 visit (type.get_type_in_parens ());
2659 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2660 }
2661
2662 void
2663 TokenCollector::visit (ImplTraitTypeOneBound &type)
2664 {
2665 // Syntax:
2666 // impl TraitBound
2667
2668 push (Rust::Token::make (IMPL, type.get_locus ()));
2669 visit (type.get_trait_bound ());
2670 }
2671
2672 void
2673 TokenCollector::visit (TraitObjectTypeOneBound &type)
2674 {
2675 // Syntax:
2676 // dyn? TraitBound
2677
2678 if (type.is_dyn ())
2679 push (Rust::Token::make (DYN, type.get_locus ()));
2680 visit (type.get_trait_bound ());
2681 }
2682
2683 void
2684 TokenCollector::visit (TupleType &type)
2685 {
2686 // Syntax:
2687 // ( )
2688 // | ( ( Type , )+ Type? )
2689
2690 push (Rust::Token::make (LEFT_PAREN, type.get_locus ()));
2691 visit_items_joined_by_separator (type.get_elems (), COMMA);
2692 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2693 }
2694
2695 void
2696 TokenCollector::visit (NeverType &type)
2697 {
2698 // Syntax:
2699 // !
2700
2701 push (Rust::Token::make (EXCLAM, type.get_locus ()));
2702 }
2703
2704 void
2705 TokenCollector::visit (RawPointerType &type)
2706 {
2707 // Syntax:
2708 // * ( mut | const ) TypeNoBounds
2709
2710 push (Rust::Token::make (ASTERISK, type.get_locus ()));
2711 if (type.get_pointer_type () == RawPointerType::MUT)
2712 push (Rust::Token::make (MUT, UNDEF_LOCATION));
2713 else /* RawPointerType::CONST */
2714 push (Rust::Token::make (CONST, UNDEF_LOCATION));
2715
2716 visit (type.get_type_pointed_to ());
2717 }
2718
2719 void
2720 TokenCollector::visit (ReferenceType &type)
2721 {
2722 // Syntax:
2723 // & Lifetime? mut? TypeNoBounds
2724
2725 push (Rust::Token::make (AMP, type.get_locus ()));
2726
2727 if (type.has_lifetime ())
2728 {
2729 visit (type.get_lifetime ());
2730 }
2731
2732 if (type.get_has_mut ())
2733 push (Rust::Token::make (MUT, UNDEF_LOCATION));
2734
2735 visit (type.get_type_referenced ());
2736 }
2737
2738 void
2739 TokenCollector::visit (ArrayType &type)
2740 {
2741 // Syntax:
2742 // [ Type ; Expression ]
2743
2744 push (Rust::Token::make (LEFT_SQUARE, type.get_locus ()));
2745 visit (type.get_elem_type ());
2746 push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
2747 visit (type.get_size_expr ());
2748 push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
2749 }
2750
2751 void
2752 TokenCollector::visit (SliceType &type)
2753 {
2754 // Syntax:
2755 // [ Type ]
2756
2757 push (Rust::Token::make (LEFT_SQUARE, type.get_locus ()));
2758 visit (type.get_elem_type ());
2759 push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
2760 }
2761
2762 void
2763 TokenCollector::visit (InferredType &type)
2764 {
2765 // Syntax:
2766 // _
2767
2768 push (Rust::Token::make (UNDERSCORE, type.get_locus ()));
2769 }
2770
2771 void
2772 TokenCollector::visit (BareFunctionType &type)
2773 {
2774 // Syntax:
2775 // ForLifetimes? FunctionTypeQualifiers fn
2776 // ( FunctionParametersMaybeNamedVariadic? ) BareFunctionReturnType?
2777 //
2778 // BareFunctionReturnType:
2779 // -> TypeNoBounds
2780 //
2781 // FunctionParametersMaybeNamedVariadic :
2782 // MaybeNamedFunctionParameters | MaybeNamedFunctionParametersVariadic
2783 //
2784 // MaybeNamedFunctionParameters :
2785 // MaybeNamedParam ( , MaybeNamedParam )* ,?
2786 //
2787 // MaybeNamedFunctionParametersVariadic :
2788 // ( MaybeNamedParam , )* MaybeNamedParam , OuterAttribute* ...
2789
2790 if (type.has_for_lifetimes ())
2791 visit (type.get_for_lifetimes ());
2792
2793 visit (type.get_function_qualifiers ());
2794
2795 push (Rust::Token::make (FN_KW, type.get_locus ()));
2796 push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
2797
2798 visit_items_joined_by_separator (type.get_function_params (), COMMA);
2799
2800 if (type.is_variadic ())
2801 {
2802 push (Rust::Token::make (COMMA, UNDEF_LOCATION));
2803 for (auto &item : type.get_variadic_attr ())
2804 {
2805 visit (item);
2806 }
2807 push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION));
2808 }
2809
2810 push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
2811
2812 if (type.has_return_type ())
2813 {
2814 push (Rust::Token::make (RETURN_TYPE, UNDEF_LOCATION));
2815 visit (type.get_return_type ());
2816 }
2817 }
2818
2819 void
2820 TokenCollector::visit (AST::FormatArgs &fmt)
2821 {
2822 rust_sorry_at (fmt.get_locus (), "%s:%u: unimplemented FormatArgs visitor",
2823 __FILE__, __LINE__);
2824 }
2825
2826 } // namespace AST
2827 } // namespace Rust