Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : /* Template implementation for Rust::Parser. Previously in rust-parse.cc (before
20 : * Parser was template). Separated from rust-parse.h for readability. */
21 :
22 : /* DO NOT INCLUDE ANYWHERE - this is automatically included
23 : * by rust-parse-impl-*.cc
24 : * This is also the reason why there are no include guards. */
25 :
26 : #include "expected.h"
27 : #include "rust-ast.h"
28 : #include "rust-common.h"
29 : #include "rust-expr.h"
30 : #include "rust-item.h"
31 : #include "rust-common.h"
32 : #include "rust-parse.h"
33 : #include "rust-token.h"
34 : #define INCLUDE_ALGORITHM
35 : #include "rust-diagnostics.h"
36 : #include "rust-dir-owner.h"
37 : #include "rust-keyword-values.h"
38 : #include "rust-edition.h"
39 : #include "rust-parse-error.h"
40 :
41 : #include "optional.h"
42 :
43 : namespace Rust {
44 :
45 : /* HACK-y special handling for skipping a right angle token at the end of
46 : * generic arguments.
47 : * Currently, this replaces the "current token" with one that is identical
48 : * except has the leading '>' removed (e.g. '>>' becomes '>'). This is bad
49 : * for several reasons - it modifies the token stream to something that
50 : * actually doesn't make syntactic sense, it may not worked if the token
51 : * has already been skipped, etc. It was done because it would not
52 : * actually require inserting new items into the token stream (which I
53 : * thought would take more work to not mess up) and because I wasn't sure
54 : * if the "already seen right angle" flag in the parser would work
55 : * correctly.
56 : * Those two other approaches listed are in my opinion actually better
57 : * long-term - insertion is probably best as it reflects syntactically
58 : * what occurs. On the other hand, I need to do a code audit to make sure
59 : * that insertion doesn't mess anything up. So that's a FIXME. */
60 : template <typename ManagedTokenSource>
61 : bool
62 242543 : Parser<ManagedTokenSource>::skip_generics_right_angle ()
63 : {
64 : /* OK, new great idea. Have a lexer method called
65 : * "split_current_token(TokenType newLeft, TokenType newRight)", which is
66 : * called here with whatever arguments are appropriate. That lexer method
67 : * handles "replacing" the current token with the "newLeft" and "inserting"
68 : * the next token with the "newRight" (and creating a location, etc. for it)
69 : */
70 :
71 : /* HACK: special handling for right shift '>>', greater or equal '>=', and
72 : * right shift assig */
73 : // '>>='
74 242543 : const_TokenPtr tok = lexer.peek_token ();
75 242543 : switch (tok->get_id ())
76 : {
77 200723 : case RIGHT_ANGLE:
78 : // this is good - skip token
79 200723 : lexer.skip_token ();
80 200723 : return true;
81 41819 : case RIGHT_SHIFT:
82 : {
83 : // new implementation that should be better
84 41819 : lexer.split_current_token (RIGHT_ANGLE, RIGHT_ANGLE);
85 41819 : lexer.skip_token ();
86 41819 : return true;
87 : }
88 0 : case GREATER_OR_EQUAL:
89 : {
90 : // new implementation that should be better
91 0 : lexer.split_current_token (RIGHT_ANGLE, EQUAL);
92 0 : lexer.skip_token ();
93 0 : return true;
94 : }
95 0 : case RIGHT_SHIFT_EQ:
96 : {
97 : // new implementation that should be better
98 0 : lexer.split_current_token (RIGHT_ANGLE, GREATER_OR_EQUAL);
99 0 : lexer.skip_token ();
100 0 : return true;
101 : }
102 1 : default:
103 1 : add_error (Error (tok->get_locus (),
104 : "expected %<>%> at end of generic argument - found %qs",
105 : tok->get_token_description ()));
106 1 : return false;
107 : }
108 242543 : }
109 :
110 : /* Gets left binding power for specified token.
111 : * Not suitable for use at the moment or possibly ever because binding power
112 : * cannot be purely determined from operator token with Rust grammar - e.g.
113 : * method call and field access have
114 : * different left binding powers but the same operator token. */
115 : template <typename ManagedTokenSource>
116 : int
117 9266581 : Parser<ManagedTokenSource>::left_binding_power (const_TokenPtr token)
118 : {
119 : // HACK: called with "peek_token()", so lookahead is "peek_token(1)"
120 9266581 : switch (token->get_id ())
121 : {
122 : /* TODO: issue here - distinguish between method calls and field access
123 : * somehow? Also would have to distinguish between paths and function
124 : * calls (:: operator), maybe more stuff. */
125 : /* Current plan for tackling LBP - don't do it based on token, use
126 : * lookahead. Or alternatively, only use Pratt parsing for OperatorExpr
127 : * and handle other expressions without it. rustc only considers
128 : * arithmetic, logical/relational, 'as',
129 : * '?=', ranges, colons, and assignment to have operator precedence and
130 : * associativity rules applicable. It then has
131 : * a separate "ExprPrecedence" that also includes binary operators. */
132 :
133 : // TODO: handle operator overloading - have a function replace the
134 : // operator?
135 :
136 : /*case DOT:
137 : return LBP_DOT;*/
138 :
139 0 : case SCOPE_RESOLUTION:
140 0 : rust_debug (
141 : "possible error - looked up LBP of scope resolution operator. should "
142 : "be handled elsewhere.");
143 : return LBP_PATH;
144 :
145 : /* Resolved by lookahead HACK that should work with current code. If next
146 : * token is identifier and token after that isn't parenthesised expression
147 : * list, it is a field reference. */
148 465572 : case DOT:
149 931144 : if (lexer.peek_token (1)->get_id () == IDENTIFIER
150 833259 : && lexer.peek_token (2)->get_id () != LEFT_PAREN)
151 : {
152 : return LBP_FIELD_EXPR;
153 : }
154 : return LBP_METHOD_CALL;
155 :
156 : case LEFT_PAREN:
157 : return LBP_FUNCTION_CALL;
158 :
159 : case LEFT_SQUARE:
160 : return LBP_ARRAY_REF;
161 :
162 : // postfix question mark (i.e. error propagation expression)
163 2472 : case QUESTION_MARK:
164 2472 : return LBP_QUESTION_MARK;
165 :
166 62834 : case AS:
167 62834 : return LBP_AS;
168 :
169 : case ASTERISK:
170 : return LBP_MUL;
171 : case DIV:
172 : return LBP_DIV;
173 : case PERCENT:
174 : return LBP_MOD;
175 :
176 : case PLUS:
177 : return LBP_PLUS;
178 : case MINUS:
179 : return LBP_MINUS;
180 :
181 : case LEFT_SHIFT:
182 : return LBP_L_SHIFT;
183 : case RIGHT_SHIFT:
184 : return LBP_R_SHIFT;
185 :
186 : // binary & operator
187 51911 : case AMP:
188 51911 : return LBP_AMP;
189 :
190 : // binary ^ operator
191 1066 : case CARET:
192 1066 : return LBP_CARET;
193 :
194 : // binary | operator
195 38460 : case PIPE:
196 38460 : return LBP_PIPE;
197 :
198 : case EQUAL_EQUAL:
199 : return LBP_EQUAL;
200 : case NOT_EQUAL:
201 : return LBP_NOT_EQUAL;
202 : case RIGHT_ANGLE:
203 : return LBP_GREATER_THAN;
204 : case GREATER_OR_EQUAL:
205 : return LBP_GREATER_EQUAL;
206 : case LEFT_ANGLE:
207 : return LBP_SMALLER_THAN;
208 : case LESS_OR_EQUAL:
209 : return LBP_SMALLER_EQUAL;
210 :
211 8707 : case LOGICAL_AND:
212 8707 : return LBP_LOGICAL_AND;
213 :
214 6802 : case OR:
215 6802 : return LBP_LOGICAL_OR;
216 :
217 : case DOT_DOT:
218 : return LBP_DOT_DOT;
219 :
220 : case DOT_DOT_EQ:
221 : return LBP_DOT_DOT_EQ;
222 :
223 : case EQUAL:
224 : return LBP_ASSIG;
225 : case PLUS_EQ:
226 : return LBP_PLUS_ASSIG;
227 : case MINUS_EQ:
228 : return LBP_MINUS_ASSIG;
229 : case ASTERISK_EQ:
230 : return LBP_MULT_ASSIG;
231 : case DIV_EQ:
232 : return LBP_DIV_ASSIG;
233 : case PERCENT_EQ:
234 : return LBP_MOD_ASSIG;
235 : case AMP_EQ:
236 : return LBP_AMP_ASSIG;
237 : case PIPE_EQ:
238 : return LBP_PIPE_ASSIG;
239 : case CARET_EQ:
240 : return LBP_CARET_ASSIG;
241 : case LEFT_SHIFT_EQ:
242 : return LBP_L_SHIFT_ASSIG;
243 : case RIGHT_SHIFT_EQ:
244 : return LBP_R_SHIFT_ASSIG;
245 :
246 : /* HACK: float literal due to lexer misidentifying a dot then an integer as
247 : * a float */
248 : case FLOAT_LITERAL:
249 : return LBP_FIELD_EXPR;
250 : // field expr is same as tuple expr in precedence, i imagine
251 : // TODO: is this needed anymore? lexer shouldn't do that anymore
252 :
253 : // anything that can't appear in an infix position is given lowest priority
254 8321817 : default:
255 8321817 : return LBP_LOWEST;
256 : }
257 : }
258 :
259 : // Returns true when current token is EOF.
260 : template <typename ManagedTokenSource>
261 : bool
262 0 : Parser<ManagedTokenSource>::done_end_of_file ()
263 : {
264 0 : return lexer.peek_token ()->get_id () == END_OF_FILE;
265 : }
266 :
267 : // Parses a sequence of items within a module or the implicit top-level module
268 : // in a crate
269 : template <typename ManagedTokenSource>
270 : tl::expected<std::vector<std::unique_ptr<AST::Item>>, Parse::Error::Items>
271 7982 : Parser<ManagedTokenSource>::parse_items ()
272 : {
273 7982 : std::vector<std::unique_ptr<AST::Item>> items;
274 :
275 7982 : const_TokenPtr t = lexer.peek_token ();
276 96042 : while (t->get_id () != END_OF_FILE)
277 : {
278 88060 : auto item = parse_item (false);
279 88060 : if (!item)
280 87 : return Parse::Error::Items::make_malformed (std::move (items));
281 :
282 87973 : items.push_back (std::move (item.value ()));
283 :
284 87973 : t = lexer.peek_token ();
285 : }
286 :
287 : // GCC 5->7 bug doesn't threat lvalue as an rvalue for the overload
288 : #if __GNUC__ <= 7
289 : return std::move (items);
290 : #else
291 7895 : return items;
292 : #endif
293 7982 : }
294 :
295 : // Parses a crate (compilation unit) - entry point
296 : template <typename ManagedTokenSource>
297 : std::unique_ptr<AST::Crate>
298 5482 : Parser<ManagedTokenSource>::parse_crate ()
299 : {
300 : // parse inner attributes
301 5482 : AST::AttrVec inner_attrs = parse_inner_attributes ();
302 :
303 : // parse items
304 10964 : auto items
305 5482 : = parse_items ().value_or (std::vector<std::unique_ptr<AST::Item>>{});
306 :
307 : // emit all errors
308 5671 : for (const auto &error : error_table)
309 189 : error.emit ();
310 :
311 : return std::unique_ptr<AST::Crate> (
312 5482 : new AST::Crate (std::move (items), std::move (inner_attrs)));
313 5482 : }
314 :
315 : // Parses an identifier/keyword as a Token
316 : template <typename ManagedTokenSource>
317 : tl::expected<std::unique_ptr<AST::Token>, Parse::Error::Node>
318 154656 : Parser<ManagedTokenSource>::parse_identifier_or_keyword_token ()
319 : {
320 154656 : const_TokenPtr t = lexer.peek_token ();
321 :
322 154656 : if (t->get_id () == IDENTIFIER || token_id_is_keyword (t->get_id ()))
323 : {
324 154630 : lexer.skip_token ();
325 154630 : return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
326 : }
327 : else
328 : {
329 26 : add_error (Error (t->get_locus (), "expected keyword or identifier"));
330 26 : return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
331 : }
332 154656 : }
333 :
334 : template <typename ManagedTokenSource>
335 : bool
336 46182 : Parser<ManagedTokenSource>::is_macro_rules_def (const_TokenPtr t)
337 : {
338 46182 : auto macro_name = lexer.peek_token (2)->get_id ();
339 :
340 46182 : bool allowed_macro_name = (macro_name == IDENTIFIER || macro_name == TRY);
341 :
342 46182 : return t->get_str () == Values::WeakKeywords::MACRO_RULES
343 46256 : && lexer.peek_token (1)->get_id () == EXCLAM && allowed_macro_name;
344 : }
345 :
346 : // Parses a single item
347 : template <typename ManagedTokenSource>
348 : tl::expected<std::unique_ptr<AST::Item>, Parse::Error::Item>
349 220044 : Parser<ManagedTokenSource>::parse_item (bool called_from_statement)
350 : {
351 : // has a "called_from_statement" parameter for better error message handling
352 :
353 : // TODO: GCC 5 does not handle implicit return type correctly so we're forced
354 : // to specify it almost every time until the baseline GCC gets bumped.
355 : // Since this type is quite long and the code is dense we use an alias.
356 : //
357 : // When support for GCC 5 stops: remove this alias as well as the explicit
358 : // ctor calls.
359 : using RType = tl::expected<std::unique_ptr<AST::Item>, Parse::Error::Item>;
360 :
361 : // parse outer attributes for item
362 220044 : AST::AttrVec outer_attrs = parse_outer_attributes ();
363 220044 : const_TokenPtr t = lexer.peek_token ();
364 :
365 220044 : switch (t->get_id ())
366 : {
367 7 : case END_OF_FILE:
368 : // not necessarily an error, unless we just read outer
369 : // attributes which needs to be attached
370 7 : if (!outer_attrs.empty ())
371 : {
372 0 : Rust::AST::Attribute attr = outer_attrs.back ();
373 0 : Error error (attr.get_locus (),
374 : "expected item after outer attribute or doc comment");
375 0 : add_error (std::move (error));
376 0 : }
377 7 : return Parse::Error::Item::make_end_of_file ();
378 :
379 174269 : case ASYNC:
380 : case PUB:
381 : case MOD:
382 : case EXTERN_KW:
383 : case USE:
384 : case FN_KW:
385 : case TYPE:
386 : case STRUCT_KW:
387 : case ENUM_KW:
388 : case CONST:
389 : case STATIC_KW:
390 : case AUTO:
391 : case TRAIT:
392 : case IMPL:
393 : case MACRO:
394 : /* TODO: implement union keyword but not really because of
395 : * context-dependence crappy hack way to parse a union written below to
396 : * separate it from the good code. */
397 : // case UNION:
398 : case UNSAFE: // maybe - unsafe traits are a thing
399 : // if any of these (should be all possible VisItem prefixes), parse a
400 : // VisItem
401 : {
402 174269 : auto errors_before = error_table.size ();
403 174269 : auto vis_item = parse_vis_item (std::move (outer_attrs));
404 174269 : if (!vis_item)
405 : {
406 : // e.g. `pub` followed by something that isn't an item
407 63 : if (error_table.size () == errors_before)
408 : {
409 1 : const_TokenPtr t = lexer.peek_token ();
410 1 : add_error (Error (t->get_locus (), "expected item, found %qs",
411 : t->get_token_description ()));
412 1 : }
413 63 : return Parse::Error::Item::make_malformed ();
414 : }
415 174206 : return RType{std::move (vis_item)};
416 174269 : }
417 4 : case SUPER:
418 : case SELF:
419 : case CRATE:
420 : case DOLLAR_SIGN:
421 : // almost certainly macro invocation semi
422 : {
423 4 : auto macro_invoc_semi
424 4 : = parse_macro_invocation_semi (std::move (outer_attrs));
425 4 : if (!macro_invoc_semi)
426 1 : return Parse::Error::Item::make_malformed ();
427 3 : return RType{std::move (macro_invoc_semi)};
428 4 : }
429 : // crappy hack to do union "keyword"
430 45764 : case IDENTIFIER:
431 : // TODO: ensure std::string and literal comparison works
432 45764 : if (t->get_str () == Values::WeakKeywords::UNION
433 45832 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
434 : {
435 68 : auto vis_item = parse_vis_item (std::move (outer_attrs));
436 68 : if (!vis_item)
437 0 : return Parse::Error::Item::make_malformed ();
438 68 : return RType{std::move (vis_item)};
439 : // or should this go straight to parsing union?
440 68 : }
441 45696 : else if (t->get_str () == Values::WeakKeywords::DEFAULT
442 45698 : && lexer.peek_token (1)->get_id () != EXCLAM)
443 : {
444 : // parse normal functions with `default` qualifier
445 : // they will be rejected in ASTValidation pass
446 1 : return parse_vis_item (std::move (outer_attrs));
447 : }
448 91390 : else if (is_macro_rules_def (t))
449 : {
450 : // macro_rules! macro item
451 3471 : auto macro_rule_def = parse_macro_rules_def (std::move (outer_attrs));
452 3471 : if (!macro_rule_def)
453 13 : return Parse::Error::Item::make_malformed ();
454 3458 : return RType{std::move (macro_rule_def)};
455 3471 : }
456 84448 : else if (lexer.peek_token (1)->get_id () == SCOPE_RESOLUTION
457 84447 : || lexer.peek_token (1)->get_id () == EXCLAM)
458 : {
459 : /* path (probably) or macro invocation, so probably a macro invocation
460 : * semi */
461 42221 : auto macro_invocation_semi
462 42221 : = parse_macro_invocation_semi (std::move (outer_attrs));
463 42221 : if (!macro_invocation_semi)
464 2 : return Parse::Error::Item::make_malformed ();
465 42219 : return RType{std::move (macro_invocation_semi)};
466 42221 : }
467 : gcc_fallthrough ();
468 : default:
469 : // otherwise unrecognised
470 6 : add_error (Error (t->get_locus (),
471 : "unrecognised token %qs for start of %s",
472 : t->get_token_description (),
473 : called_from_statement ? "statement" : "item"));
474 :
475 : // skip somewhere?
476 3 : return Parse::Error::Item::make_malformed ();
477 : break;
478 : }
479 220044 : }
480 :
481 : // Parses a VisItem (item that can have non-default visibility).
482 : template <typename ManagedTokenSource>
483 : std::unique_ptr<AST::VisItem>
484 180999 : Parser<ManagedTokenSource>::parse_vis_item (AST::AttrVec outer_attrs)
485 : {
486 : // parse visibility, which may or may not exist
487 180999 : auto vis_res = parse_visibility ();
488 180999 : if (!vis_res)
489 0 : return nullptr;
490 180999 : auto vis = vis_res.value ();
491 :
492 : // select VisItem to create depending on keyword
493 180999 : const_TokenPtr t = lexer.peek_token ();
494 :
495 180999 : switch (t->get_id ())
496 : {
497 5291 : case MOD:
498 5291 : return parse_module (std::move (vis), std::move (outer_attrs));
499 2467 : case EXTERN_KW:
500 : // lookahead to resolve syntactical production
501 2467 : t = lexer.peek_token (1);
502 :
503 2467 : switch (t->get_id ())
504 : {
505 70 : case CRATE:
506 70 : return parse_extern_crate (std::move (vis), std::move (outer_attrs));
507 0 : case FN_KW: // extern function
508 0 : return parse_function (std::move (vis), std::move (outer_attrs));
509 1 : case LEFT_CURLY: // extern block
510 1 : return parse_extern_block (std::move (vis), std::move (outer_attrs));
511 2396 : case STRING_LITERAL: // for specifying extern ABI
512 : // could be extern block or extern function, so more lookahead
513 2396 : t = lexer.peek_token (2);
514 :
515 2396 : switch (t->get_id ())
516 : {
517 2 : case FN_KW:
518 2 : return parse_function (std::move (vis), std::move (outer_attrs));
519 2394 : case LEFT_CURLY:
520 2394 : return parse_extern_block (std::move (vis),
521 2394 : std::move (outer_attrs));
522 0 : default:
523 0 : add_error (
524 0 : Error (t->get_locus (),
525 : "unexpected token %qs in some sort of extern production",
526 : t->get_token_description ()));
527 :
528 0 : lexer.skip_token (2); // TODO: is this right thing to do?
529 0 : return nullptr;
530 : }
531 0 : default:
532 0 : add_error (
533 0 : Error (t->get_locus (),
534 : "unexpected token %qs in some sort of extern production",
535 : t->get_token_description ()));
536 :
537 0 : lexer.skip_token (1); // TODO: is this right thing to do?
538 0 : return nullptr;
539 : }
540 11365 : case USE:
541 11365 : return parse_use_decl (std::move (vis), std::move (outer_attrs));
542 10904 : case FN_KW:
543 10904 : return parse_function (std::move (vis), std::move (outer_attrs));
544 234 : case TYPE:
545 234 : return parse_type_alias (std::move (vis), std::move (outer_attrs));
546 11037 : case STRUCT_KW:
547 11037 : return parse_struct (std::move (vis), std::move (outer_attrs));
548 1046 : case ENUM_KW:
549 1046 : return parse_enum (std::move (vis), std::move (outer_attrs));
550 : // TODO: implement union keyword but not really because of
551 : // context-dependence case UNION: crappy hack to do union "keyword"
552 140 : case IDENTIFIER:
553 140 : if (t->get_str () == Values::WeakKeywords::UNION
554 279 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
555 : {
556 139 : return parse_union (std::move (vis), std::move (outer_attrs));
557 : // or should item switch go straight to parsing union?
558 : }
559 1 : else if (t->get_str () == Values::WeakKeywords::DEFAULT)
560 : {
561 : // parse normal functions with `default` qualifier they will be
562 : // rejected in ASTValidation pass
563 1 : return parse_function (std::move (vis), std::move (outer_attrs));
564 : }
565 : break;
566 7528 : case CONST:
567 : // lookahead to resolve syntactical production
568 7528 : t = lexer.peek_token (1);
569 :
570 7528 : switch (t->get_id ())
571 : {
572 7151 : case IDENTIFIER:
573 : case UNDERSCORE:
574 7151 : return parse_const_item (std::move (vis), std::move (outer_attrs));
575 1 : case ASYNC:
576 1 : return parse_async_item (std::move (vis), std::move (outer_attrs));
577 375 : case UNSAFE:
578 : case EXTERN_KW:
579 : case FN_KW:
580 375 : return parse_function (std::move (vis), std::move (outer_attrs));
581 1 : default:
582 1 : add_error (
583 1 : Error (t->get_locus (),
584 : "unexpected token %qs in some sort of const production",
585 : t->get_token_description ()));
586 :
587 1 : lexer.skip_token (1); // TODO: is this right thing to do?
588 1 : return nullptr;
589 : }
590 : // for async functions
591 6 : case ASYNC:
592 6 : return parse_async_item (std::move (vis), std::move (outer_attrs));
593 :
594 521 : case STATIC_KW:
595 521 : return parse_static_item (std::move (vis), std::move (outer_attrs));
596 6068 : case AUTO:
597 : case TRAIT:
598 6068 : return parse_trait (std::move (vis), std::move (outer_attrs));
599 80255 : case IMPL:
600 80255 : return parse_impl (std::move (vis), std::move (outer_attrs));
601 43853 : case UNSAFE: // unsafe traits, unsafe functions, unsafe impls (trait impls),
602 : // lookahead to resolve syntactical production
603 43853 : t = lexer.peek_token (1);
604 :
605 43853 : switch (t->get_id ())
606 : {
607 265 : case AUTO:
608 : case TRAIT:
609 265 : return parse_trait (std::move (vis), std::move (outer_attrs));
610 41617 : case EXTERN_KW:
611 : case FN_KW:
612 41617 : return parse_function (std::move (vis), std::move (outer_attrs));
613 1969 : case IMPL:
614 1969 : return parse_impl (std::move (vis), std::move (outer_attrs));
615 1 : case MOD:
616 1 : return parse_module (std::move (vis), std::move (outer_attrs));
617 1 : default:
618 1 : add_error (
619 1 : Error (t->get_locus (),
620 : "unexpected token %qs in some sort of unsafe production",
621 : t->get_token_description ()));
622 :
623 1 : lexer.skip_token (1); // TODO: is this right thing to do?
624 1 : return nullptr;
625 : }
626 282 : case MACRO:
627 282 : return parse_decl_macro_def (std::move (vis), std::move (outer_attrs));
628 : default:
629 : // otherwise vis item clearly doesn't exist, which is not an error
630 : // has a catch-all post-switch return to allow other breaks to occur
631 : break;
632 : }
633 2 : return nullptr;
634 180999 : }
635 :
636 : template <typename ManagedTokenSource>
637 : std::unique_ptr<AST::Function>
638 8 : Parser<ManagedTokenSource>::parse_async_item (AST::Visibility vis,
639 : AST::AttrVec outer_attrs)
640 : {
641 8 : auto offset = (lexer.peek_token ()->get_id () == CONST) ? 1 : 0;
642 8 : const_TokenPtr t = lexer.peek_token (offset);
643 :
644 8 : if (get_rust_edition () == Edition::E2015)
645 : {
646 1 : add_error (Error (t->get_locus (), ErrorCode::E0670,
647 : "%<async fn%> is not permitted in Rust 2015"));
648 1 : add_error (
649 2 : Error::Hint (t->get_locus (),
650 : "to use %<async fn%>, switch to Rust 2018 or later"));
651 : }
652 :
653 8 : t = lexer.peek_token (offset + 1);
654 :
655 8 : switch (t->get_id ())
656 : {
657 8 : case UNSAFE:
658 : case FN_KW:
659 8 : return parse_function (std::move (vis), std::move (outer_attrs));
660 :
661 0 : default:
662 0 : add_error (
663 0 : Error (t->get_locus (), "expected item, found keyword %<async%>"));
664 :
665 0 : lexer.skip_token (1);
666 0 : return nullptr;
667 : }
668 8 : }
669 :
670 : // Parses a macro rules definition syntax extension whatever thing.
671 : template <typename ManagedTokenSource>
672 : std::unique_ptr<AST::MacroRulesDefinition>
673 9589 : Parser<ManagedTokenSource>::parse_macro_rules_def (AST::AttrVec outer_attrs)
674 : {
675 : // ensure that first token is identifier saying "macro_rules"
676 9589 : const_TokenPtr t = lexer.peek_token ();
677 9589 : if (t->get_id () != IDENTIFIER
678 9589 : || t->get_str () != Values::WeakKeywords::MACRO_RULES)
679 : {
680 0 : Error error (
681 : t->get_locus (),
682 : "macro rules definition does not start with %<macro_rules%>");
683 0 : add_error (std::move (error));
684 :
685 : // skip after somewhere?
686 0 : return nullptr;
687 0 : }
688 9589 : lexer.skip_token ();
689 9589 : location_t macro_locus = t->get_locus ();
690 :
691 9589 : if (!skip_token (EXCLAM))
692 : {
693 : // skip after somewhere?
694 0 : return nullptr;
695 : }
696 :
697 : // parse macro name
698 9589 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
699 9589 : if (ident_tok == nullptr)
700 : {
701 1 : return nullptr;
702 : }
703 19176 : Identifier rule_name{ident_tok};
704 :
705 : // DEBUG
706 9588 : rust_debug ("in macro rules def, about to parse parens.");
707 :
708 : // save delim type to ensure it is reused later
709 9588 : AST::DelimType delim_type = AST::PARENS;
710 :
711 : // Map tokens to DelimType
712 9588 : t = lexer.peek_token ();
713 9588 : switch (t->get_id ())
714 : {
715 : case LEFT_PAREN:
716 : delim_type = AST::PARENS;
717 : break;
718 0 : case LEFT_SQUARE:
719 0 : delim_type = AST::SQUARE;
720 0 : break;
721 9587 : case LEFT_CURLY:
722 9587 : delim_type = AST::CURLY;
723 9587 : break;
724 0 : default:
725 0 : add_error (Error (t->get_locus (),
726 : "unexpected token %qs - expecting delimiters (for a "
727 : "macro rules definition)",
728 : t->get_token_description ()));
729 :
730 0 : return nullptr;
731 : }
732 9588 : lexer.skip_token ();
733 :
734 : // parse actual macro rules
735 9588 : std::vector<AST::MacroRule> macro_rules;
736 :
737 : // must be at least one macro rule, so parse it
738 9588 : AST::MacroRule initial_rule = parse_macro_rule ();
739 9588 : if (initial_rule.is_error ())
740 : {
741 12 : Error error (lexer.peek_token ()->get_locus (),
742 : "required first macro rule in macro rules definition "
743 : "could not be parsed");
744 12 : add_error (std::move (error));
745 :
746 : // skip after somewhere?
747 12 : return nullptr;
748 12 : }
749 9576 : macro_rules.push_back (std::move (initial_rule));
750 :
751 : // DEBUG
752 9576 : rust_debug ("successfully pushed back initial macro rule");
753 :
754 9576 : t = lexer.peek_token ();
755 : // parse macro rules
756 10177 : while (t->get_id () == SEMICOLON)
757 : {
758 : // skip semicolon
759 8458 : lexer.skip_token ();
760 :
761 : // don't parse if end of macro rules
762 16916 : if (Parse::Utils::token_id_matches_delims (lexer.peek_token ()->get_id (),
763 : delim_type))
764 : {
765 : // DEBUG
766 7857 : rust_debug (
767 : "broke out of parsing macro rules loop due to finding delim");
768 :
769 7857 : break;
770 : }
771 :
772 : // try to parse next rule
773 601 : AST::MacroRule rule = parse_macro_rule ();
774 601 : if (rule.is_error ())
775 : {
776 0 : Error error (lexer.peek_token ()->get_locus (),
777 : "failed to parse macro rule in macro rules definition");
778 0 : add_error (std::move (error));
779 :
780 0 : return nullptr;
781 0 : }
782 :
783 601 : macro_rules.push_back (std::move (rule));
784 :
785 : // DEBUG
786 601 : rust_debug ("successfully pushed back another macro rule");
787 :
788 601 : t = lexer.peek_token ();
789 : }
790 :
791 : // parse end delimiters
792 9576 : t = lexer.peek_token ();
793 9576 : if (Parse::Utils::token_id_matches_delims (t->get_id (), delim_type))
794 : {
795 : // tokens match opening delimiter, so skip.
796 9576 : lexer.skip_token ();
797 :
798 9576 : if (delim_type != AST::CURLY)
799 : {
800 : // skip semicolon at end of non-curly macro definitions
801 1 : if (!skip_token (SEMICOLON))
802 : {
803 : // as this is the end, allow recovery (probably) - may change
804 : return std::unique_ptr<AST::MacroRulesDefinition> (
805 0 : AST::MacroRulesDefinition::mbe (
806 : std::move (rule_name), delim_type, std::move (macro_rules),
807 0 : std::move (outer_attrs), macro_locus));
808 : }
809 : }
810 :
811 : return std::unique_ptr<AST::MacroRulesDefinition> (
812 19152 : AST::MacroRulesDefinition::mbe (std::move (rule_name), delim_type,
813 : std::move (macro_rules),
814 9576 : std::move (outer_attrs), macro_locus));
815 : }
816 : else
817 : {
818 : // tokens don't match opening delimiters, so produce error
819 0 : Error error (t->get_locus (),
820 : "unexpected token %qs - expecting closing delimiter %qs "
821 : "(for a macro rules definition)",
822 : t->get_token_description (),
823 : (delim_type == AST::PARENS
824 : ? ")"
825 : : (delim_type == AST::SQUARE ? "]" : "}")));
826 0 : add_error (std::move (error));
827 :
828 : /* return empty macro definiton despite possibly parsing mostly valid one
829 : * - TODO is this a good idea? */
830 0 : return nullptr;
831 0 : }
832 28765 : }
833 :
834 : // Parses a declarative macro 2.0 definition.
835 : template <typename ManagedTokenSource>
836 : std::unique_ptr<AST::MacroRulesDefinition>
837 282 : Parser<ManagedTokenSource>::parse_decl_macro_def (AST::Visibility vis,
838 : AST::AttrVec outer_attrs)
839 : {
840 : // ensure that first token is identifier saying "macro"
841 282 : const_TokenPtr t = lexer.peek_token ();
842 282 : if (t->get_id () != MACRO)
843 : {
844 0 : Error error (
845 : t->get_locus (),
846 : "declarative macro definition does not start with %<macro%>");
847 0 : add_error (std::move (error));
848 :
849 : // skip after somewhere?
850 0 : return nullptr;
851 0 : }
852 282 : lexer.skip_token ();
853 282 : location_t macro_locus = t->get_locus ();
854 :
855 : // parse macro name
856 282 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
857 282 : if (ident_tok == nullptr)
858 : {
859 0 : return nullptr;
860 : }
861 564 : Identifier rule_name{ident_tok};
862 :
863 282 : t = lexer.peek_token ();
864 282 : if (t->get_id () == LEFT_PAREN)
865 : {
866 : // single definiton of macro rule
867 : // e.g. `macro foo($e:expr) {}`
868 :
869 : // parse macro matcher
870 257 : location_t locus = lexer.peek_token ()->get_locus ();
871 257 : AST::MacroMatcher matcher = parse_macro_matcher ();
872 257 : if (matcher.is_error ())
873 0 : return nullptr;
874 :
875 : // check delimiter of macro matcher
876 257 : if (matcher.get_delim_type () != AST::DelimType::PARENS)
877 : {
878 0 : Error error (locus, "only parenthesis can be used for a macro "
879 : "matcher in declarative macro definition");
880 0 : add_error (std::move (error));
881 0 : return nullptr;
882 0 : }
883 :
884 257 : location_t transcriber_loc = lexer.peek_token ()->get_locus ();
885 257 : auto delim_tok_tree = parse_delim_token_tree ();
886 257 : if (!delim_tok_tree)
887 0 : return nullptr;
888 :
889 257 : AST::MacroTranscriber transcriber (delim_tok_tree.value (),
890 : transcriber_loc);
891 :
892 257 : if (transcriber.get_token_tree ().get_delim_type ()
893 : != AST::DelimType::CURLY)
894 : {
895 1 : Error error (transcriber_loc,
896 : "only braces can be used for a macro transcriber "
897 : "in declarative macro definition");
898 1 : add_error (std::move (error));
899 1 : return nullptr;
900 1 : }
901 :
902 256 : std::vector<AST::MacroRule> macro_rules;
903 256 : macro_rules.emplace_back (std::move (matcher), std::move (transcriber),
904 : locus);
905 :
906 : return std::unique_ptr<AST::MacroRulesDefinition> (
907 512 : AST::MacroRulesDefinition::decl_macro (std::move (rule_name),
908 : macro_rules,
909 : std::move (outer_attrs),
910 256 : macro_locus, vis));
911 770 : }
912 25 : else if (t->get_id () == LEFT_CURLY)
913 : {
914 : // multiple definitions of macro rule separated by comma
915 : // e.g. `macro foo { () => {}, ($e:expr) => {}, }`
916 :
917 : // parse left curly
918 25 : const_TokenPtr left_curly = expect_token (LEFT_CURLY);
919 25 : if (left_curly == nullptr)
920 : {
921 0 : return nullptr;
922 : }
923 :
924 : // parse actual macro rules
925 25 : std::vector<AST::MacroRule> macro_rules;
926 :
927 : // must be at least one macro rule, so parse it
928 25 : AST::MacroRule initial_rule = parse_macro_rule ();
929 25 : if (initial_rule.is_error ())
930 : {
931 1 : Error error (
932 1 : lexer.peek_token ()->get_locus (),
933 : "required first macro rule in declarative macro definition "
934 : "could not be parsed");
935 1 : add_error (std::move (error));
936 :
937 : // skip after somewhere?
938 1 : return nullptr;
939 1 : }
940 24 : macro_rules.push_back (std::move (initial_rule));
941 :
942 24 : t = lexer.peek_token ();
943 : // parse macro rules
944 40 : while (t->get_id () == COMMA)
945 : {
946 : // skip comma
947 32 : lexer.skip_token ();
948 :
949 : // don't parse if end of macro rules
950 32 : if (Parse::Utils::token_id_matches_delims (
951 64 : lexer.peek_token ()->get_id (), AST::CURLY))
952 : {
953 : break;
954 : }
955 :
956 : // try to parse next rule
957 16 : AST::MacroRule rule = parse_macro_rule ();
958 16 : if (rule.is_error ())
959 : {
960 0 : Error error (
961 0 : lexer.peek_token ()->get_locus (),
962 : "failed to parse macro rule in declarative macro definition");
963 0 : add_error (std::move (error));
964 :
965 0 : return nullptr;
966 0 : }
967 :
968 16 : macro_rules.push_back (std::move (rule));
969 :
970 16 : t = lexer.peek_token ();
971 : }
972 :
973 : // parse right curly
974 24 : const_TokenPtr right_curly = expect_token (RIGHT_CURLY);
975 24 : if (right_curly == nullptr)
976 : {
977 0 : return nullptr;
978 : }
979 :
980 : return std::unique_ptr<AST::MacroRulesDefinition> (
981 48 : AST::MacroRulesDefinition::decl_macro (std::move (rule_name),
982 : std::move (macro_rules),
983 : std::move (outer_attrs),
984 24 : macro_locus, vis));
985 50 : }
986 : else
987 : {
988 0 : add_error (Error (t->get_locus (),
989 : "unexpected token %qs - expecting delimiters "
990 : "(for a declarative macro definiton)",
991 : t->get_token_description ()));
992 0 : return nullptr;
993 : }
994 564 : }
995 :
996 : /* Parses a visibility syntactical production (i.e. creating a non-default
997 : * visibility) */
998 : template <typename ManagedTokenSource>
999 : tl::expected<AST::Visibility, Parse::Error::Visibility>
1000 366507 : Parser<ManagedTokenSource>::parse_visibility ()
1001 : {
1002 : // check for no visibility
1003 733014 : if (lexer.peek_token ()->get_id () != PUB)
1004 : {
1005 283030 : return AST::Visibility::create_private ();
1006 : }
1007 :
1008 83477 : auto vis_loc = lexer.peek_token ()->get_locus ();
1009 83477 : lexer.skip_token ();
1010 :
1011 : // create simple pub visibility if
1012 : // - found no parentheses
1013 : // - found unit type `()`
1014 166954 : if (lexer.peek_token ()->get_id () != LEFT_PAREN
1015 87347 : || lexer.peek_token (1)->get_id () == RIGHT_PAREN)
1016 : {
1017 79608 : return AST::Visibility::create_public (vis_loc);
1018 : // or whatever
1019 : }
1020 :
1021 3869 : lexer.skip_token ();
1022 :
1023 3869 : const_TokenPtr t = lexer.peek_token ();
1024 3869 : auto path_loc = t->get_locus ();
1025 :
1026 3869 : switch (t->get_id ())
1027 : {
1028 2623 : case CRATE:
1029 2623 : lexer.skip_token ();
1030 :
1031 2623 : skip_token (RIGHT_PAREN);
1032 :
1033 2623 : return AST::Visibility::create_crate (path_loc, vis_loc);
1034 0 : case SELF:
1035 0 : lexer.skip_token ();
1036 :
1037 0 : skip_token (RIGHT_PAREN);
1038 :
1039 0 : return AST::Visibility::create_self (path_loc, vis_loc);
1040 1160 : case SUPER:
1041 1160 : lexer.skip_token ();
1042 :
1043 1160 : skip_token (RIGHT_PAREN);
1044 :
1045 1160 : return AST::Visibility::create_super (path_loc, vis_loc);
1046 86 : case IN:
1047 : {
1048 86 : lexer.skip_token ();
1049 :
1050 : // parse the "in" path as well
1051 86 : auto path = parse_simple_path ();
1052 86 : if (!path)
1053 : {
1054 0 : Error error (lexer.peek_token ()->get_locus (),
1055 : "missing path in pub(in path) visibility");
1056 0 : add_error (std::move (error));
1057 :
1058 : // skip after somewhere?
1059 0 : return Parse::Error::Visibility::make_missing_path ();
1060 0 : }
1061 :
1062 86 : skip_token (RIGHT_PAREN);
1063 :
1064 172 : return AST::Visibility::create_in_path (std::move (path.value ()),
1065 86 : vis_loc);
1066 86 : }
1067 0 : default:
1068 0 : add_error (Error (t->get_locus (), "unexpected token %qs in visibility",
1069 : t->get_token_description ()));
1070 :
1071 0 : lexer.skip_token ();
1072 0 : return Parse::Error::Visibility::make_malformed ();
1073 : }
1074 3869 : }
1075 :
1076 : // Parses a module - either a bodied module or a module defined in another file.
1077 : template <typename ManagedTokenSource>
1078 : std::unique_ptr<AST::Module>
1079 5292 : Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
1080 : AST::AttrVec outer_attrs)
1081 : {
1082 5292 : location_t locus = lexer.peek_token ()->get_locus ();
1083 :
1084 5292 : Unsafety safety = Unsafety::Normal;
1085 10584 : if (lexer.peek_token ()->get_id () == UNSAFE)
1086 : {
1087 1 : safety = Unsafety::Unsafe;
1088 1 : skip_token (UNSAFE);
1089 : }
1090 :
1091 5292 : skip_token (MOD);
1092 :
1093 5292 : const_TokenPtr module_name = expect_token (IDENTIFIER);
1094 5292 : if (module_name == nullptr)
1095 : {
1096 0 : return nullptr;
1097 : }
1098 10584 : Identifier name{module_name};
1099 :
1100 5292 : const_TokenPtr t = lexer.peek_token ();
1101 :
1102 5292 : switch (t->get_id ())
1103 : {
1104 2645 : case SEMICOLON:
1105 2645 : lexer.skip_token ();
1106 :
1107 : // Construct an external module
1108 : return std::unique_ptr<AST::Module> (
1109 7935 : new AST::Module (std::move (name), std::move (vis),
1110 : std::move (outer_attrs), locus, safety,
1111 7935 : lexer.get_filename (), inline_module_stack));
1112 2647 : case LEFT_CURLY:
1113 : {
1114 2647 : lexer.skip_token ();
1115 :
1116 : // parse inner attributes
1117 2647 : AST::AttrVec inner_attrs = parse_inner_attributes ();
1118 :
1119 2647 : std::string default_path = name.as_string ();
1120 :
1121 2647 : if (inline_module_stack.empty ())
1122 : {
1123 2081 : std::string filename = lexer.get_filename ();
1124 2081 : auto slash_idx = filename.rfind (file_separator);
1125 2081 : if (slash_idx == std::string::npos)
1126 : slash_idx = 0;
1127 : else
1128 2074 : slash_idx++;
1129 2081 : filename = filename.substr (slash_idx);
1130 :
1131 2081 : std::string subdir;
1132 2081 : if (get_file_subdir (filename, subdir))
1133 1966 : default_path = subdir + file_separator + name.as_string ();
1134 2081 : }
1135 :
1136 2647 : std::string module_path_name
1137 : = extract_module_path (inner_attrs, outer_attrs, default_path);
1138 2647 : InlineModuleStackScope scope (*this, std::move (module_path_name));
1139 :
1140 : // parse items
1141 2647 : std::vector<std::unique_ptr<AST::Item>> items;
1142 2647 : const_TokenPtr tok = lexer.peek_token ();
1143 31706 : while (tok->get_id () != RIGHT_CURLY)
1144 : {
1145 29059 : auto item = parse_item (false);
1146 29059 : if (!item)
1147 : {
1148 1 : Error error (tok->get_locus (),
1149 : "failed to parse item in module");
1150 1 : add_error (std::move (error));
1151 :
1152 1 : return nullptr;
1153 1 : }
1154 :
1155 29058 : items.push_back (std::move (item.value ()));
1156 :
1157 29058 : tok = lexer.peek_token ();
1158 : }
1159 :
1160 2646 : if (!skip_token (RIGHT_CURLY))
1161 : {
1162 : // skip somewhere?
1163 0 : return nullptr;
1164 : }
1165 :
1166 : return std::unique_ptr<AST::Module> (
1167 2646 : new AST::Module (std::move (name), locus, std::move (items),
1168 : std::move (vis), safety, std::move (inner_attrs),
1169 2646 : std::move (outer_attrs))); // module name?
1170 2647 : }
1171 0 : default:
1172 0 : add_error (
1173 0 : Error (t->get_locus (),
1174 : "unexpected token %qs in module declaration/definition item",
1175 : t->get_token_description ()));
1176 :
1177 0 : lexer.skip_token ();
1178 0 : return nullptr;
1179 : }
1180 5292 : }
1181 :
1182 : // Parses an extern crate declaration (dependency on external crate)
1183 : template <typename ManagedTokenSource>
1184 : std::unique_ptr<AST::ExternCrate>
1185 70 : Parser<ManagedTokenSource>::parse_extern_crate (AST::Visibility vis,
1186 : AST::AttrVec outer_attrs)
1187 : {
1188 70 : location_t locus = lexer.peek_token ()->get_locus ();
1189 70 : if (!skip_token (EXTERN_KW))
1190 : {
1191 0 : skip_after_semicolon ();
1192 0 : return nullptr;
1193 : }
1194 :
1195 70 : if (!skip_token (CRATE))
1196 : {
1197 0 : skip_after_semicolon ();
1198 0 : return nullptr;
1199 : }
1200 :
1201 : /* parse crate reference name - this has its own syntactical rule in reference
1202 : * but seems to not be used elsewhere, so i'm putting it here */
1203 70 : const_TokenPtr crate_name_tok = lexer.peek_token ();
1204 70 : std::string crate_name;
1205 :
1206 70 : switch (crate_name_tok->get_id ())
1207 : {
1208 69 : case IDENTIFIER:
1209 69 : crate_name = crate_name_tok->get_str ();
1210 69 : lexer.skip_token ();
1211 69 : break;
1212 1 : case SELF:
1213 1 : crate_name = Values::Keywords::SELF;
1214 1 : lexer.skip_token ();
1215 1 : break;
1216 0 : default:
1217 0 : add_error (
1218 0 : Error (crate_name_tok->get_locus (),
1219 : "expecting crate name (identifier or %<self%>), found %qs",
1220 : crate_name_tok->get_token_description ()));
1221 :
1222 0 : skip_after_semicolon ();
1223 0 : return nullptr;
1224 : }
1225 :
1226 : // don't parse as clause if it doesn't exist
1227 140 : if (lexer.peek_token ()->get_id () == SEMICOLON)
1228 : {
1229 61 : lexer.skip_token ();
1230 :
1231 : return std::unique_ptr<AST::ExternCrate> (
1232 61 : new AST::ExternCrate (std::move (crate_name), std::move (vis),
1233 61 : std::move (outer_attrs), locus));
1234 : }
1235 :
1236 : /* parse as clause - this also has its own syntactical rule in reference and
1237 : * also seems to not be used elsewhere, so including here again. */
1238 9 : if (!skip_token (AS))
1239 : {
1240 0 : skip_after_semicolon ();
1241 0 : return nullptr;
1242 : }
1243 :
1244 9 : const_TokenPtr as_name_tok = lexer.peek_token ();
1245 9 : std::string as_name;
1246 :
1247 9 : switch (as_name_tok->get_id ())
1248 : {
1249 9 : case IDENTIFIER:
1250 9 : as_name = as_name_tok->get_str ();
1251 9 : lexer.skip_token ();
1252 9 : break;
1253 0 : case UNDERSCORE:
1254 0 : as_name = Values::Keywords::UNDERSCORE;
1255 0 : lexer.skip_token ();
1256 0 : break;
1257 0 : default:
1258 0 : add_error (
1259 0 : Error (as_name_tok->get_locus (),
1260 : "expecting as clause name (identifier or %<_%>), found %qs",
1261 : as_name_tok->get_token_description ()));
1262 :
1263 0 : skip_after_semicolon ();
1264 0 : return nullptr;
1265 : }
1266 :
1267 9 : if (!skip_token (SEMICOLON))
1268 : {
1269 0 : skip_after_semicolon ();
1270 0 : return nullptr;
1271 : }
1272 :
1273 : return std::unique_ptr<AST::ExternCrate> (
1274 9 : new AST::ExternCrate (std::move (crate_name), std::move (vis),
1275 9 : std::move (outer_attrs), locus, std::move (as_name)));
1276 158 : }
1277 :
1278 : // Parses a use declaration.
1279 : template <typename ManagedTokenSource>
1280 : std::unique_ptr<AST::UseDeclaration>
1281 11365 : Parser<ManagedTokenSource>::parse_use_decl (AST::Visibility vis,
1282 : AST::AttrVec outer_attrs)
1283 : {
1284 11365 : location_t locus = lexer.peek_token ()->get_locus ();
1285 11365 : if (!skip_token (USE))
1286 : {
1287 0 : skip_after_semicolon ();
1288 0 : return nullptr;
1289 : }
1290 :
1291 : // parse use tree, which is required
1292 11365 : std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
1293 11365 : if (use_tree == nullptr)
1294 : {
1295 1 : Error error (lexer.peek_token ()->get_locus (),
1296 : "could not parse use tree in use declaration");
1297 1 : add_error (std::move (error));
1298 :
1299 1 : skip_after_semicolon ();
1300 1 : return nullptr;
1301 1 : }
1302 :
1303 11364 : if (!skip_token (SEMICOLON))
1304 : {
1305 0 : skip_after_semicolon ();
1306 0 : return nullptr;
1307 : }
1308 :
1309 : return std::unique_ptr<AST::UseDeclaration> (
1310 11364 : new AST::UseDeclaration (std::move (use_tree), std::move (vis),
1311 11364 : std::move (outer_attrs), locus));
1312 11365 : }
1313 :
1314 : // Parses a use tree (which can be recursive and is actually a base class).
1315 : template <typename ManagedTokenSource>
1316 : std::unique_ptr<AST::UseTree>
1317 21565 : Parser<ManagedTokenSource>::parse_use_tree ()
1318 : {
1319 : /* potential syntax definitions in attempt to get algorithm:
1320 : * Glob:
1321 : * <- SimplePath :: *
1322 : * <- :: *
1323 : * <- *
1324 : * Nested tree thing:
1325 : * <- SimplePath :: { COMPLICATED_INNER_TREE_THING }
1326 : * <- :: COMPLICATED_INNER_TREE_THING }
1327 : * <- { COMPLICATED_INNER_TREE_THING }
1328 : * Rebind thing:
1329 : * <- SimplePath as IDENTIFIER
1330 : * <- SimplePath as _
1331 : * <- SimplePath
1332 : */
1333 :
1334 : /* current plan of attack: try to parse SimplePath first - if fails, one of
1335 : * top two then try parse :: - if fails, one of top two. Next is deciding
1336 : * character for top two. */
1337 :
1338 : /* Thus, parsing smaller parts of use tree may require feeding into function
1339 : * via parameters (or could handle all in this single function because other
1340 : * use tree types aren't recognised as separate in the spec) */
1341 :
1342 : // TODO: I think this function is too complex, probably should split it
1343 :
1344 21565 : location_t locus = lexer.peek_token ()->get_locus ();
1345 :
1346 : // bool has_path = false;
1347 21565 : auto path = parse_simple_path ();
1348 :
1349 21565 : if (!path)
1350 : {
1351 : // has no path, so must be glob or nested tree UseTree type
1352 :
1353 7 : bool is_global = false;
1354 :
1355 : // check for global scope resolution operator
1356 14 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
1357 : {
1358 1 : lexer.skip_token ();
1359 1 : is_global = true;
1360 : }
1361 :
1362 7 : const_TokenPtr t = lexer.peek_token ();
1363 7 : switch (t->get_id ())
1364 : {
1365 5 : case ASTERISK:
1366 : // glob UseTree type
1367 5 : lexer.skip_token ();
1368 :
1369 5 : if (is_global)
1370 1 : return std::unique_ptr<AST::UseTreeGlob> (
1371 2 : new AST::UseTreeGlob (AST::UseTreeGlob::GLOBAL,
1372 3 : AST::SimplePath::create_empty (), locus));
1373 : else
1374 4 : return std::unique_ptr<AST::UseTreeGlob> (
1375 8 : new AST::UseTreeGlob (AST::UseTreeGlob::NO_PATH,
1376 12 : AST::SimplePath::create_empty (), locus));
1377 2 : case LEFT_CURLY:
1378 : {
1379 : // nested tree UseTree type
1380 2 : lexer.skip_token ();
1381 :
1382 2 : std::vector<std::unique_ptr<AST::UseTree>> use_trees;
1383 :
1384 2 : const_TokenPtr t = lexer.peek_token ();
1385 5 : while (t->get_id () != RIGHT_CURLY)
1386 : {
1387 3 : std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
1388 3 : if (use_tree == nullptr)
1389 : {
1390 : break;
1391 : }
1392 :
1393 3 : use_trees.push_back (std::move (use_tree));
1394 :
1395 6 : if (lexer.peek_token ()->get_id () != COMMA)
1396 : break;
1397 :
1398 1 : lexer.skip_token ();
1399 1 : t = lexer.peek_token ();
1400 : }
1401 :
1402 : // skip end curly delimiter
1403 2 : if (!skip_token (RIGHT_CURLY))
1404 : {
1405 : // skip after somewhere?
1406 0 : return nullptr;
1407 : }
1408 :
1409 2 : if (is_global)
1410 0 : return std::unique_ptr<AST::UseTreeList> (
1411 0 : new AST::UseTreeList (AST::UseTreeList::GLOBAL,
1412 0 : AST::SimplePath::create_empty (),
1413 0 : std::move (use_trees), locus));
1414 : else
1415 2 : return std::unique_ptr<AST::UseTreeList> (
1416 4 : new AST::UseTreeList (AST::UseTreeList::NO_PATH,
1417 4 : AST::SimplePath::create_empty (),
1418 2 : std::move (use_trees), locus));
1419 2 : }
1420 0 : case AS:
1421 : // this is not allowed
1422 0 : add_error (Error (
1423 : t->get_locus (),
1424 : "use declaration with rebind %<as%> requires a valid simple path - "
1425 : "none found"));
1426 :
1427 0 : skip_after_semicolon ();
1428 0 : return nullptr;
1429 0 : default:
1430 0 : add_error (Error (t->get_locus (),
1431 : "unexpected token %qs in use tree with "
1432 : "no valid simple path (i.e. list"
1433 : " or glob use tree)",
1434 : t->get_token_description ()));
1435 :
1436 0 : skip_after_semicolon ();
1437 0 : return nullptr;
1438 : }
1439 7 : }
1440 : else
1441 : {
1442 21558 : const_TokenPtr t = lexer.peek_token ();
1443 :
1444 : switch (t->get_id ())
1445 : {
1446 201 : case AS:
1447 : {
1448 : // rebind UseTree type
1449 201 : lexer.skip_token ();
1450 :
1451 201 : const_TokenPtr t = lexer.peek_token ();
1452 201 : switch (t->get_id ())
1453 : {
1454 199 : case IDENTIFIER:
1455 : // skip lexer token
1456 199 : lexer.skip_token ();
1457 :
1458 199 : return std::unique_ptr<AST::UseTreeRebind> (
1459 597 : new AST::UseTreeRebind (AST::UseTreeRebind::IDENTIFIER,
1460 597 : std::move (path.value ()), locus, t));
1461 2 : case UNDERSCORE:
1462 : // skip lexer token
1463 2 : lexer.skip_token ();
1464 :
1465 2 : return std::unique_ptr<AST::UseTreeRebind> (
1466 6 : new AST::UseTreeRebind (AST::UseTreeRebind::WILDCARD,
1467 2 : std::move (path.value ()), locus,
1468 : {Values::Keywords::UNDERSCORE,
1469 2 : t->get_locus ()}));
1470 0 : default:
1471 0 : add_error (Error (
1472 : t->get_locus (),
1473 : "unexpected token %qs in use tree with as clause - expected "
1474 : "identifier or %<_%>",
1475 : t->get_token_description ()));
1476 :
1477 0 : skip_after_semicolon ();
1478 0 : return nullptr;
1479 : }
1480 201 : }
1481 15964 : case SEMICOLON:
1482 : // rebind UseTree type without rebinding - path only
1483 :
1484 : // don't skip semicolon - handled in parse_use_tree
1485 : // lexer.skip_token();
1486 : case COMMA:
1487 : case RIGHT_CURLY:
1488 : // this may occur in recursive calls - assume it is ok and ignore it
1489 15964 : return std::unique_ptr<AST::UseTreeRebind> (
1490 47892 : new AST::UseTreeRebind (AST::UseTreeRebind::NONE,
1491 15964 : std::move (path.value ()), locus));
1492 : case SCOPE_RESOLUTION:
1493 : // keep going
1494 : break;
1495 1 : default:
1496 1 : add_error (Error (t->get_locus (),
1497 : "unexpected token %qs in use tree with valid path",
1498 : t->get_token_description ()));
1499 1 : return nullptr;
1500 : }
1501 :
1502 5392 : skip_token ();
1503 5392 : t = lexer.peek_token ();
1504 :
1505 5392 : switch (t->get_id ())
1506 : {
1507 2101 : case ASTERISK:
1508 : // glob UseTree type
1509 2101 : lexer.skip_token ();
1510 :
1511 2101 : return std::unique_ptr<AST::UseTreeGlob> (
1512 6303 : new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
1513 2101 : std::move (path.value ()), locus));
1514 3291 : case LEFT_CURLY:
1515 : {
1516 : // nested tree UseTree type
1517 3291 : lexer.skip_token ();
1518 :
1519 3291 : std::vector<std::unique_ptr<AST::UseTree>> use_trees;
1520 :
1521 : // TODO: think of better control structure
1522 3291 : const_TokenPtr t = lexer.peek_token ();
1523 13488 : while (t->get_id () != RIGHT_CURLY)
1524 : {
1525 10197 : std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
1526 10197 : if (use_tree == nullptr)
1527 : {
1528 : break;
1529 : }
1530 :
1531 10197 : use_trees.push_back (std::move (use_tree));
1532 :
1533 20394 : if (lexer.peek_token ()->get_id () != COMMA)
1534 : break;
1535 :
1536 7284 : lexer.skip_token ();
1537 7284 : t = lexer.peek_token ();
1538 : }
1539 :
1540 : // skip end curly delimiter
1541 3291 : if (!skip_token (RIGHT_CURLY))
1542 : {
1543 : // skip after somewhere?
1544 0 : return nullptr;
1545 : }
1546 :
1547 3291 : return std::unique_ptr<AST::UseTreeList> (
1548 6582 : new AST::UseTreeList (AST::UseTreeList::PATH_PREFIXED,
1549 3291 : std::move (path.value ()),
1550 3291 : std::move (use_trees), locus));
1551 3291 : }
1552 0 : default:
1553 0 : add_error (Error (t->get_locus (),
1554 : "unexpected token %qs in use tree with valid path",
1555 : t->get_token_description ()));
1556 :
1557 : // skip_after_semicolon();
1558 0 : return nullptr;
1559 : }
1560 21558 : }
1561 21565 : }
1562 :
1563 : // Parses a function (not a method).
1564 : template <typename ManagedTokenSource>
1565 : std::unique_ptr<AST::Function>
1566 72930 : Parser<ManagedTokenSource>::parse_function (AST::Visibility vis,
1567 : AST::AttrVec outer_attrs,
1568 : bool is_external)
1569 : {
1570 72930 : location_t locus = lexer.peek_token ()->get_locus ();
1571 : // Get qualifiers for function if they exist
1572 72930 : auto qualifiers = parse_function_qualifiers ();
1573 72930 : if (!qualifiers)
1574 1 : return nullptr;
1575 :
1576 72929 : skip_token (FN_KW);
1577 :
1578 : // Save function name token
1579 72929 : const_TokenPtr function_name_tok = expect_token (IDENTIFIER);
1580 72929 : if (function_name_tok == nullptr)
1581 : {
1582 0 : skip_after_next_block ();
1583 0 : return nullptr;
1584 : }
1585 145858 : Identifier function_name{function_name_tok};
1586 :
1587 : // parse generic params - if exist
1588 72929 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
1589 : = parse_generic_params_in_angles ();
1590 :
1591 72929 : if (!skip_token (LEFT_PAREN))
1592 : {
1593 0 : Error error (lexer.peek_token ()->get_locus (),
1594 : "function declaration missing opening parentheses before "
1595 : "parameter list");
1596 0 : add_error (std::move (error));
1597 :
1598 0 : skip_after_next_block ();
1599 0 : return nullptr;
1600 0 : }
1601 :
1602 72929 : auto initial_param = parse_self_param ();
1603 :
1604 72929 : if (!initial_param.has_value ()
1605 72929 : && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
1606 0 : return nullptr;
1607 :
1608 79014 : if (initial_param.has_value () && lexer.peek_token ()->get_id () == COMMA)
1609 3641 : skip_token ();
1610 :
1611 : // parse function parameters (only if next token isn't right paren)
1612 72929 : std::vector<std::unique_ptr<AST::Param>> function_params;
1613 :
1614 145858 : if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
1615 : function_params
1616 42563 : = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
1617 :
1618 72929 : if (initial_param.has_value ())
1619 6085 : function_params.insert (function_params.begin (),
1620 6085 : std::move (*initial_param));
1621 :
1622 72929 : if (!skip_token (RIGHT_PAREN))
1623 : {
1624 1 : Error error (lexer.peek_token ()->get_locus (),
1625 : "function declaration missing closing parentheses after "
1626 : "parameter list");
1627 1 : add_error (std::move (error));
1628 :
1629 1 : skip_after_next_block ();
1630 1 : return nullptr;
1631 1 : }
1632 :
1633 : // parse function return type - if exists
1634 72928 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
1635 :
1636 : // parse where clause - if exists
1637 72928 : AST::WhereClause where_clause = parse_where_clause ();
1638 :
1639 72928 : tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
1640 145856 : if (lexer.peek_token ()->get_id () == SEMICOLON)
1641 17223 : lexer.skip_token ();
1642 : else
1643 : {
1644 55705 : auto block_expr = parse_block_expr ();
1645 55705 : if (!block_expr)
1646 30 : return nullptr;
1647 55675 : body = std::move (block_expr.value ());
1648 55705 : }
1649 :
1650 201471 : return std::unique_ptr<AST::Function> (new AST::Function (
1651 72898 : std::move (function_name), std::move (qualifiers.value ()),
1652 : std::move (generic_params), std::move (function_params),
1653 : std::move (return_type), std::move (where_clause), std::move (body),
1654 72898 : std::move (vis), std::move (outer_attrs), locus, is_external));
1655 218787 : }
1656 :
1657 : // Parses function or method qualifiers (i.e. const, unsafe, and extern).
1658 : template <typename ManagedTokenSource>
1659 : tl::expected<AST::FunctionQualifiers, Parse::Error::Node>
1660 192253 : Parser<ManagedTokenSource>::parse_function_qualifiers ()
1661 : {
1662 192253 : location_t locus = lexer.peek_token ()->get_locus ();
1663 :
1664 192253 : auto parsed = parse_function_qualifiers_raw (locus);
1665 192253 : if (!parsed)
1666 3 : return tl::unexpected<Parse::Error::Node> (parsed.error ());
1667 :
1668 384500 : return function_qualifiers_from_keywords (locus, std::move (parsed->first),
1669 384500 : std::move (parsed->second));
1670 192253 : }
1671 :
1672 : // Take the list of parsed function qualifiers and convert it to
1673 : // the corrresponding flags to pass to the AST item constructor.
1674 : //
1675 : // This assumes ``keywords`` contains only those tokens that
1676 : // map to qualifiers.
1677 : template <typename ManagedTokenSource>
1678 : tl::expected<AST::FunctionQualifiers, Parse::Error::Node>
1679 192250 : Parser<ManagedTokenSource>::function_qualifiers_from_keywords (
1680 : location_t locus, const std::vector<TokenId> keywords, std::string abi)
1681 : {
1682 192250 : Default default_status = Default::No;
1683 192250 : Async async_status = Async::No;
1684 192250 : Const const_status = Const::No;
1685 192250 : Unsafety unsafe_status = Unsafety::Normal;
1686 192250 : bool has_extern = false;
1687 :
1688 266606 : for (auto qualifier : keywords)
1689 : {
1690 74356 : switch (qualifier)
1691 : {
1692 382 : case IDENTIFIER:
1693 : // only "default" is valid in this context
1694 382 : default_status = Default::Yes;
1695 382 : continue;
1696 17039 : case CONST:
1697 17039 : const_status = Const::Yes;
1698 17039 : continue;
1699 11 : case ASYNC:
1700 11 : async_status = Async::Yes;
1701 11 : continue;
1702 49095 : case UNSAFE:
1703 49095 : unsafe_status = Unsafety::Unsafe;
1704 49095 : continue;
1705 7829 : case EXTERN_KW:
1706 7829 : has_extern = true;
1707 7829 : continue;
1708 0 : default:
1709 : // non-qualifier token in input
1710 0 : rust_unreachable ();
1711 : }
1712 : }
1713 :
1714 384500 : return AST::FunctionQualifiers (locus, default_status, async_status,
1715 : const_status, unsafe_status, has_extern,
1716 192250 : std::move (abi));
1717 : }
1718 :
1719 : // this consumes as many function qualifier tokens while ensuring
1720 : // uniqueness.
1721 : template <typename ManagedTokenSource>
1722 : tl::expected<std::pair<std::vector<TokenId>, std::string>, Parse::Error::Node>
1723 192253 : Parser<ManagedTokenSource>::parse_function_qualifiers_raw (location_t locus)
1724 : {
1725 192253 : std::vector<TokenId> found_order;
1726 192253 : std::string abi;
1727 :
1728 : // this will terminate on duplicates or the first non-qualifier token
1729 74366 : while (true)
1730 : {
1731 266618 : auto token = lexer.peek_token ();
1732 266618 : const TokenId token_id = token->get_id ();
1733 266618 : location_t locus = lexer.peek_token ()->get_locus ();
1734 :
1735 266618 : switch (token_id)
1736 : {
1737 384 : case IDENTIFIER:
1738 384 : if (token->get_str () != Values::WeakKeywords::DEFAULT)
1739 : {
1740 : // only "default" is valid in this context, so this must
1741 : // be a non-qualifier keyword
1742 0 : goto done;
1743 : }
1744 : // fallthrough
1745 : case CONST:
1746 : case ASYNC:
1747 : case UNSAFE:
1748 66536 : found_order.push_back (token_id);
1749 66536 : lexer.skip_token ();
1750 66536 : break;
1751 7830 : case EXTERN_KW:
1752 : {
1753 7830 : found_order.push_back (token_id);
1754 7830 : lexer.skip_token ();
1755 :
1756 : // detect optional abi name
1757 7830 : const_TokenPtr next_tok = lexer.peek_token ();
1758 7830 : if (next_tok->get_id () == STRING_LITERAL)
1759 : {
1760 7830 : abi = next_tok->get_str ();
1761 7830 : lexer.skip_token ();
1762 : }
1763 7830 : }
1764 7830 : break;
1765 192252 : default:
1766 : // non-qualifier keyword
1767 192252 : goto done;
1768 : }
1769 :
1770 148732 : if (std::count (found_order.cbegin (), found_order.cend (), token_id) > 1)
1771 : {
1772 : // qualifiers mustn't appear twice
1773 1 : Error error (locus, "encountered duplicate function qualifier %qs",
1774 : token->get_token_description ());
1775 1 : add_error (std::move (error));
1776 :
1777 : return tl::unexpected<Parse::Error::Node> (
1778 1 : Parse::Error::Node::MALFORMED);
1779 1 : }
1780 : }
1781 192252 : done:
1782 :
1783 192252 : if (!ensure_function_qualifier_order (locus, found_order))
1784 2 : return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
1785 :
1786 384500 : return make_pair (found_order, abi);
1787 192253 : }
1788 :
1789 : // Validate the order of the list of function qualifiers; this assumes that
1790 : // ``found_order`` consists only of function qualifier tokens.
1791 : //
1792 : // If the order is illegal, the generated error message gives both the wrong
1793 : // order as found in the source and the correct order according to Rust syntax
1794 : // rules.
1795 : template <typename ManagedTokenSource>
1796 : bool
1797 192252 : Parser<ManagedTokenSource>::ensure_function_qualifier_order (
1798 : location_t locus, const std::vector<TokenId> &found_order)
1799 : {
1800 : // Check in order of default, const, async, unsafe, extern
1801 74363 : auto token_priority = [] (const TokenId id) {
1802 74363 : switch (id)
1803 : {
1804 : case IDENTIFIER: // "default"; the only "weak" keyword considered here
1805 : return 1;
1806 17040 : case CONST:
1807 17040 : return 2;
1808 12 : case ASYNC:
1809 12 : return 3;
1810 49097 : case UNSAFE:
1811 49097 : return 4;
1812 7830 : case EXTERN_KW:
1813 7830 : return 5;
1814 0 : default:
1815 0 : rust_unreachable ();
1816 : };
1817 : };
1818 :
1819 192252 : size_t last_priority = 0;
1820 266613 : for (auto token_id : found_order)
1821 : {
1822 74363 : const size_t priority = token_priority (token_id);
1823 74363 : if (priority <= last_priority)
1824 : {
1825 2 : emit_function_qualifier_order_error_msg (locus, found_order);
1826 2 : return false;
1827 : }
1828 :
1829 74361 : last_priority = priority;
1830 : }
1831 :
1832 : return true;
1833 : }
1834 :
1835 : template <typename ManagedTokenSource>
1836 : void
1837 2 : Parser<ManagedTokenSource>::emit_function_qualifier_order_error_msg (
1838 : location_t locus, const std::vector<TokenId> &found_order)
1839 : {
1840 2 : std::vector<TokenId> expected_order
1841 : = {IDENTIFIER, CONST, ASYNC, UNSAFE, EXTERN_KW};
1842 :
1843 : // we only keep the qualifiers actually used in the offending code
1844 2 : std::vector<TokenId>::iterator token_id = expected_order.begin ();
1845 12 : while (token_id != expected_order.end ())
1846 : {
1847 20 : if (std::find (found_order.cbegin (), found_order.cend (), *token_id)
1848 20 : == found_order.cend ())
1849 : {
1850 3 : token_id = expected_order.erase (token_id);
1851 : }
1852 : else
1853 : {
1854 7 : ++token_id;
1855 : }
1856 : }
1857 :
1858 4 : auto qualifiers_to_str = [] (const std::vector<TokenId> &token_ids) {
1859 4 : std::ostringstream ss;
1860 :
1861 18 : for (auto id : token_ids)
1862 : {
1863 14 : if (ss.tellp () != 0)
1864 10 : ss << ' ';
1865 :
1866 14 : if (id == IDENTIFIER)
1867 4 : ss << Values::WeakKeywords::DEFAULT;
1868 : else
1869 10 : ss << token_id_keyword_string (id);
1870 : }
1871 :
1872 8 : return ss.str ();
1873 4 : };
1874 :
1875 2 : const std::string found_qualifiers = qualifiers_to_str (found_order);
1876 2 : const std::string expected_qualifiers = qualifiers_to_str (expected_order);
1877 :
1878 : location_t error_locus
1879 2 : = make_location (locus, locus, lexer.peek_token ()->get_locus ());
1880 2 : Error error (error_locus,
1881 : "invalid order of function qualifiers; found %qs, expected %qs",
1882 : found_qualifiers.c_str (), expected_qualifiers.c_str ());
1883 2 : add_error (std::move (error));
1884 2 : }
1885 :
1886 : // Parses generic (lifetime or type) params inside angle brackets (optional).
1887 : template <typename ManagedTokenSource>
1888 : std::vector<std::unique_ptr<AST::GenericParam>>
1889 319055 : Parser<ManagedTokenSource>::parse_generic_params_in_angles ()
1890 : {
1891 638110 : if (lexer.peek_token ()->get_id () != LEFT_ANGLE)
1892 : {
1893 : // seems to be no generic params, so exit with empty vector
1894 270195 : return std::vector<std::unique_ptr<AST::GenericParam>> ();
1895 : }
1896 48860 : lexer.skip_token ();
1897 :
1898 : // DEBUG:
1899 48860 : rust_debug ("skipped left angle in generic param");
1900 :
1901 48860 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
1902 : = parse_generic_params (Parse::Utils::is_right_angle_tok);
1903 :
1904 : // DEBUG:
1905 48860 : rust_debug ("finished parsing actual generic params (i.e. inside angles)");
1906 :
1907 48860 : if (!skip_generics_right_angle ())
1908 : {
1909 : // DEBUG
1910 1 : rust_debug ("failed to skip generics right angle - returning empty "
1911 : "generic params");
1912 :
1913 1 : return std::vector<std::unique_ptr<AST::GenericParam>> ();
1914 : }
1915 :
1916 48859 : return generic_params;
1917 48860 : }
1918 :
1919 : template <typename ManagedTokenSource>
1920 : template <typename EndTokenPred>
1921 : std::unique_ptr<AST::GenericParam>
1922 108297 : Parser<ManagedTokenSource>::parse_generic_param (EndTokenPred is_end_token)
1923 : {
1924 108297 : auto outer_attrs = parse_outer_attributes ();
1925 108297 : std::unique_ptr<AST::GenericParam> param;
1926 108297 : auto token = lexer.peek_token ();
1927 :
1928 108297 : switch (token->get_id ())
1929 : {
1930 13893 : case LIFETIME:
1931 : {
1932 13893 : auto lifetime = parse_lifetime (false);
1933 13893 : if (!lifetime)
1934 : {
1935 0 : Error error (token->get_locus (),
1936 : "failed to parse lifetime in generic parameter list");
1937 0 : add_error (std::move (error));
1938 :
1939 0 : return nullptr;
1940 0 : }
1941 :
1942 13893 : std::vector<AST::Lifetime> lifetime_bounds;
1943 27786 : if (lexer.peek_token ()->get_id () == COLON)
1944 : {
1945 223 : lexer.skip_token ();
1946 : // parse required bounds
1947 : lifetime_bounds
1948 223 : = parse_lifetime_bounds ([is_end_token] (TokenId id) {
1949 235 : return is_end_token (id) || id == COMMA;
1950 : });
1951 : }
1952 :
1953 27786 : param = std::unique_ptr<AST::LifetimeParam> (new AST::LifetimeParam (
1954 13893 : std::move (lifetime.value ()), std::move (lifetime_bounds),
1955 13893 : std::move (outer_attrs), token->get_locus ()));
1956 : break;
1957 27786 : }
1958 93419 : case IDENTIFIER:
1959 : {
1960 93419 : auto type_ident = token->get_str ();
1961 93419 : lexer.skip_token ();
1962 :
1963 93419 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
1964 186838 : if (lexer.peek_token ()->get_id () == COLON)
1965 : {
1966 21942 : lexer.skip_token ();
1967 :
1968 : // parse optional type param bounds
1969 21942 : type_param_bounds = parse_type_param_bounds ();
1970 : }
1971 :
1972 93419 : std::unique_ptr<AST::Type> type = nullptr;
1973 186838 : if (lexer.peek_token ()->get_id () == EQUAL)
1974 : {
1975 750 : lexer.skip_token ();
1976 :
1977 : // parse required type
1978 750 : type = parse_type ();
1979 750 : if (!type)
1980 : {
1981 0 : Error error (
1982 0 : lexer.peek_token ()->get_locus (),
1983 : "failed to parse type in type param in generic params");
1984 0 : add_error (std::move (error));
1985 :
1986 0 : return nullptr;
1987 0 : }
1988 : }
1989 :
1990 93419 : param = std::unique_ptr<AST::TypeParam> (
1991 280257 : new AST::TypeParam (std::move (type_ident), token->get_locus (),
1992 : std::move (type_param_bounds), std::move (type),
1993 93419 : std::move (outer_attrs)));
1994 : break;
1995 93419 : }
1996 984 : case CONST:
1997 : {
1998 984 : lexer.skip_token ();
1999 :
2000 984 : auto name_token = expect_token (IDENTIFIER);
2001 :
2002 1968 : if (!name_token || !expect_token (COLON))
2003 1 : return nullptr;
2004 :
2005 983 : auto type = parse_type ();
2006 983 : if (!type)
2007 1 : return nullptr;
2008 :
2009 : // optional default value
2010 982 : tl::optional<AST::GenericArg> default_expr = tl::nullopt;
2011 1964 : if (lexer.peek_token ()->get_id () == EQUAL)
2012 : {
2013 24 : lexer.skip_token ();
2014 24 : auto tok = lexer.peek_token ();
2015 47 : default_expr = parse_generic_arg ();
2016 :
2017 24 : if (!default_expr)
2018 : {
2019 1 : Error error (tok->get_locus (),
2020 : "invalid token for start of default value for "
2021 : "const generic parameter: expected %<block%>, "
2022 : "%<identifier%> or %<literal%>, got %qs",
2023 : token_id_to_str (tok->get_id ()));
2024 :
2025 1 : add_error (std::move (error));
2026 1 : return nullptr;
2027 1 : }
2028 :
2029 : // At this point, we *know* that we are parsing a const
2030 : // expression
2031 23 : if (default_expr.value ().get_kind ()
2032 : == AST::GenericArg::Kind::Either)
2033 1 : default_expr = default_expr.value ().disambiguate_to_const ();
2034 22 : else if (default_expr.value ().get_kind ()
2035 : != AST::GenericArg::Kind::Const)
2036 : {
2037 1 : Error error (
2038 1 : default_expr.value ().get_locus (),
2039 : "expressions must be enclosed in braces to be used as const "
2040 : "generic arguments");
2041 1 : add_error (std::move (error));
2042 1 : default_expr = tl::nullopt;
2043 1 : }
2044 24 : }
2045 :
2046 981 : param = std::unique_ptr<AST::ConstGenericParam> (
2047 3946 : new AST::ConstGenericParam (name_token->get_str (), std::move (type),
2048 : default_expr, std::move (outer_attrs),
2049 981 : token->get_locus ()));
2050 :
2051 : break;
2052 1967 : }
2053 1 : default:
2054 : // FIXME: Can we clean this last call with a method call?
2055 1 : Error error (token->get_locus (),
2056 : "unexpected token when parsing generic parameters: %qs",
2057 1 : token->as_string ().c_str ());
2058 1 : add_error (std::move (error));
2059 :
2060 1 : return nullptr;
2061 1 : }
2062 :
2063 108293 : return param;
2064 108297 : }
2065 :
2066 : /* Parse generic (lifetime or type) params NOT INSIDE ANGLE BRACKETS!!! Almost
2067 : * always parse_generic_params_in_angles is what is wanted. */
2068 : template <typename ManagedTokenSource>
2069 : template <typename EndTokenPred>
2070 : std::vector<std::unique_ptr<AST::GenericParam>>
2071 48860 : Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
2072 : {
2073 48860 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params;
2074 :
2075 : /* can't parse lifetime and type params separately due to lookahead issues
2076 : * thus, parse them all here */
2077 :
2078 : /* HACK: used to retain attribute data if a lifetime param is tentatively
2079 : * parsed but it turns out to be type param */
2080 48860 : AST::Attribute parsed_outer_attr = AST::Attribute::create_empty ();
2081 :
2082 : // Did we parse a generic type param yet
2083 48860 : auto type_seen = false;
2084 : // Did we parse a const param with a default value yet
2085 48860 : auto const_with_default_seen = false;
2086 : // Did the user write a lifetime parameter after a type one
2087 48860 : auto order_error = false;
2088 : // Did the user write a const param with a default value after a type one
2089 48860 : auto const_with_default_order_error = false;
2090 :
2091 : // parse lifetime params
2092 422603 : while (!is_end_token (lexer.peek_token ()->get_id ()))
2093 : {
2094 108297 : auto param = parse_generic_param (is_end_token);
2095 108297 : if (param)
2096 : {
2097 108293 : if (param->get_kind () == AST::GenericParam::Kind::Type)
2098 : {
2099 93419 : type_seen = true;
2100 93419 : if (const_with_default_seen)
2101 108293 : const_with_default_order_error = true;
2102 : }
2103 14874 : else if (param->get_kind () == AST::GenericParam::Kind::Lifetime
2104 14874 : && type_seen)
2105 : {
2106 2 : order_error = true;
2107 2 : if (const_with_default_seen)
2108 0 : const_with_default_order_error = true;
2109 : }
2110 14872 : else if (param->get_kind () == AST::GenericParam::Kind::Const)
2111 : {
2112 981 : type_seen = true;
2113 : AST::ConstGenericParam *const_param
2114 981 : = static_cast<AST::ConstGenericParam *> (param.get ());
2115 981 : if (const_param->has_default_value ())
2116 : const_with_default_seen = true;
2117 959 : else if (const_with_default_seen)
2118 108293 : const_with_default_order_error = true;
2119 : }
2120 :
2121 108293 : generic_params.emplace_back (std::move (param));
2122 108293 : maybe_skip_token (COMMA);
2123 : }
2124 : else
2125 : break;
2126 : }
2127 :
2128 : // FIXME: Add reordering hint
2129 48860 : if (order_error)
2130 : {
2131 2 : Error error (generic_params.front ()->get_locus (),
2132 : "invalid order for generic parameters: lifetime parameters "
2133 : "must be declared prior to type and const parameters");
2134 2 : add_error (std::move (error));
2135 2 : }
2136 48860 : if (const_with_default_order_error)
2137 : {
2138 2 : Error error (generic_params.front ()->get_locus (),
2139 : "invalid order for generic parameters: generic parameters "
2140 : "with a default must be trailing");
2141 2 : add_error (std::move (error));
2142 2 : }
2143 :
2144 48860 : generic_params.shrink_to_fit ();
2145 48860 : return generic_params;
2146 48860 : }
2147 :
2148 : /* Parses lifetime generic parameters (pointers). Will also consume any
2149 : * trailing comma. No extra checks for end token. */
2150 : template <typename ManagedTokenSource>
2151 : std::vector<std::unique_ptr<AST::LifetimeParam>>
2152 160 : Parser<ManagedTokenSource>::parse_lifetime_params ()
2153 : {
2154 160 : std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
2155 :
2156 528 : while (lexer.peek_token ()->get_id () != END_OF_FILE)
2157 : {
2158 184 : auto lifetime_param = parse_lifetime_param ();
2159 :
2160 184 : if (!lifetime_param)
2161 : {
2162 : // can't treat as error as only way to get out with trailing comma
2163 : break;
2164 : }
2165 :
2166 74 : lifetime_params.emplace_back (
2167 74 : new AST::LifetimeParam (std::move (lifetime_param.value ())));
2168 :
2169 148 : if (lexer.peek_token ()->get_id () != COMMA)
2170 : break;
2171 :
2172 : // skip commas, including trailing commas
2173 24 : lexer.skip_token ();
2174 : }
2175 :
2176 160 : lifetime_params.shrink_to_fit ();
2177 :
2178 160 : return lifetime_params;
2179 : }
2180 :
2181 : /* Parses lifetime generic parameters (pointers). Will also consume any
2182 : * trailing comma. Has extra is_end_token predicate checking. */
2183 : template <typename ManagedTokenSource>
2184 : template <typename EndTokenPred>
2185 : std::vector<std::unique_ptr<AST::LifetimeParam>>
2186 : Parser<ManagedTokenSource>::parse_lifetime_params (EndTokenPred is_end_token)
2187 : {
2188 : std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
2189 :
2190 : // if end_token is not specified, it defaults to EOF, so should work fine
2191 : while (!is_end_token (lexer.peek_token ()->get_id ()))
2192 : {
2193 : auto lifetime_param = parse_lifetime_param ();
2194 :
2195 : if (!lifetime_param)
2196 : {
2197 : /* TODO: is it worth throwing away all lifetime params just because
2198 : * one failed? */
2199 : Error error (lexer.peek_token ()->get_locus (),
2200 : "failed to parse lifetime param in lifetime params");
2201 : add_error (std::move (error));
2202 :
2203 : return {};
2204 : }
2205 :
2206 : lifetime_params.emplace_back (
2207 : new AST::LifetimeParam (std::move (lifetime_param)));
2208 :
2209 : if (lexer.peek_token ()->get_id () != COMMA)
2210 : break;
2211 :
2212 : // skip commas, including trailing commas
2213 : lexer.skip_token ();
2214 : }
2215 :
2216 : lifetime_params.shrink_to_fit ();
2217 :
2218 : return lifetime_params;
2219 : }
2220 :
2221 : /* Parses lifetime generic parameters (objects). Will also consume any
2222 : * trailing comma. No extra checks for end token.
2223 : * TODO: is this best solution? implements most of the same algorithm.
2224 : * TODO: seems to be unused, remove? */
2225 : template <typename ManagedTokenSource>
2226 : std::vector<AST::LifetimeParam>
2227 0 : Parser<ManagedTokenSource>::parse_lifetime_params_objs ()
2228 : {
2229 0 : std::vector<AST::LifetimeParam> lifetime_params;
2230 :
2231 : // bad control structure as end token cannot be guaranteed
2232 0 : while (true)
2233 : {
2234 0 : auto lifetime_param = parse_lifetime_param ();
2235 :
2236 0 : if (!lifetime_param)
2237 : {
2238 : // not an error as only way to exit if trailing comma
2239 : break;
2240 : }
2241 :
2242 0 : lifetime_params.push_back (std::move (lifetime_param.value ()));
2243 :
2244 0 : if (lexer.peek_token ()->get_id () != COMMA)
2245 : break;
2246 :
2247 : // skip commas, including trailing commas
2248 0 : lexer.skip_token ();
2249 : }
2250 :
2251 0 : lifetime_params.shrink_to_fit ();
2252 :
2253 0 : return lifetime_params;
2254 : }
2255 :
2256 : /* Parses lifetime generic parameters (objects). Will also consume any
2257 : * trailing comma. Has extra is_end_token predicate checking.
2258 : * TODO: is this best solution? implements most of the same algorithm. */
2259 : template <typename ManagedTokenSource>
2260 : template <typename EndTokenPred>
2261 : std::vector<AST::LifetimeParam>
2262 44 : Parser<ManagedTokenSource>::parse_lifetime_params_objs (
2263 : EndTokenPred is_end_token)
2264 : {
2265 44 : std::vector<AST::LifetimeParam> lifetime_params;
2266 :
2267 132 : while (!is_end_token (lexer.peek_token ()->get_id ()))
2268 : {
2269 44 : auto lifetime_param = parse_lifetime_param ();
2270 :
2271 44 : if (!lifetime_param)
2272 : {
2273 : /* TODO: is it worth throwing away all lifetime params just because
2274 : * one failed? */
2275 0 : Error error (lexer.peek_token ()->get_locus (),
2276 : "failed to parse lifetime param in lifetime params");
2277 0 : add_error (std::move (error));
2278 :
2279 0 : return {};
2280 0 : }
2281 :
2282 44 : lifetime_params.push_back (std::move (lifetime_param.value ()));
2283 :
2284 88 : if (lexer.peek_token ()->get_id () != COMMA)
2285 : break;
2286 :
2287 : // skip commas, including trailing commas
2288 0 : lexer.skip_token ();
2289 : }
2290 :
2291 44 : lifetime_params.shrink_to_fit ();
2292 :
2293 44 : return lifetime_params;
2294 44 : }
2295 :
2296 : /* Parses a sequence of a certain grammar rule in object form (not pointer or
2297 : * smart pointer), delimited by commas and ending when 'is_end_token' is
2298 : * satisfied (templated). Will also consume any trailing comma.
2299 : * FIXME: this cannot be used due to member function pointer problems (i.e.
2300 : * parsing_function cannot be specified properly) */
2301 : template <typename ManagedTokenSource>
2302 : template <typename ParseFunction, typename EndTokenPred>
2303 : auto
2304 : Parser<ManagedTokenSource>::parse_non_ptr_sequence (
2305 : ParseFunction parsing_function, EndTokenPred is_end_token,
2306 : std::string error_msg) -> std::vector<decltype (parsing_function ())>
2307 : {
2308 : std::vector<decltype (parsing_function ())> params;
2309 :
2310 : while (!is_end_token (lexer.peek_token ()->get_id ()))
2311 : {
2312 : auto param = parsing_function ();
2313 :
2314 : if (param.is_error ())
2315 : {
2316 : // TODO: is it worth throwing away all params just because one
2317 : // failed?
2318 : Error error (lexer.peek_token ()->get_locus (),
2319 : std::move (error_msg));
2320 : add_error (std::move (error));
2321 :
2322 : return {};
2323 : }
2324 :
2325 : params.push_back (std::move (param));
2326 :
2327 : if (lexer.peek_token ()->get_id () != COMMA)
2328 : break;
2329 :
2330 : // skip commas, including trailing commas
2331 : lexer.skip_token ();
2332 : }
2333 :
2334 : params.shrink_to_fit ();
2335 :
2336 : return params;
2337 : }
2338 :
2339 : /* Parses a single lifetime generic parameter (not including comma). */
2340 : template <typename ManagedTokenSource>
2341 : tl::expected<AST::LifetimeParam, Parse::Error::LifetimeParam>
2342 228 : Parser<ManagedTokenSource>::parse_lifetime_param ()
2343 : {
2344 : // parse outer attributes, which are optional and may not exist
2345 228 : auto outer_attrs = parse_outer_attributes ();
2346 :
2347 : // save lifetime token - required
2348 228 : const_TokenPtr lifetime_tok = lexer.peek_token ();
2349 228 : if (lifetime_tok->get_id () != LIFETIME)
2350 : {
2351 : // if lifetime is missing, must not be a lifetime param, so return error
2352 110 : return Parse::Error::LifetimeParam::make_not_a_lifetime_param ();
2353 : }
2354 118 : lexer.skip_token ();
2355 236 : AST::Lifetime lifetime (AST::Lifetime::NAMED, lifetime_tok->get_str (),
2356 : lifetime_tok->get_locus ());
2357 :
2358 : // parse lifetime bounds, if it exists
2359 118 : std::vector<AST::Lifetime> lifetime_bounds;
2360 236 : if (lexer.peek_token ()->get_id () == COLON)
2361 : {
2362 : // parse lifetime bounds
2363 0 : lifetime_bounds = parse_lifetime_bounds ();
2364 : // TODO: have end token passed in?
2365 : }
2366 :
2367 236 : return AST::LifetimeParam (std::move (lifetime), std::move (lifetime_bounds),
2368 : std::move (outer_attrs),
2369 118 : lifetime_tok->get_locus ());
2370 346 : }
2371 :
2372 : // Parses type generic parameters. Will also consume any trailing comma.
2373 : template <typename ManagedTokenSource>
2374 : std::vector<std::unique_ptr<AST::TypeParam>>
2375 0 : Parser<ManagedTokenSource>::parse_type_params ()
2376 : {
2377 0 : std::vector<std::unique_ptr<AST::TypeParam>> type_params;
2378 :
2379 : // infinite loop with break on failure as no info on ending token
2380 0 : while (true)
2381 : {
2382 0 : std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
2383 :
2384 0 : if (type_param == nullptr)
2385 : {
2386 : // break if fails to parse
2387 : break;
2388 : }
2389 :
2390 0 : type_params.push_back (std::move (type_param));
2391 :
2392 0 : if (lexer.peek_token ()->get_id () != COMMA)
2393 : break;
2394 :
2395 : // skip commas, including trailing commas
2396 0 : lexer.skip_token ();
2397 : }
2398 :
2399 0 : type_params.shrink_to_fit ();
2400 0 : return type_params;
2401 : }
2402 :
2403 : // Parses type generic parameters. Will also consume any trailing comma.
2404 : template <typename ManagedTokenSource>
2405 : template <typename EndTokenPred>
2406 : std::vector<std::unique_ptr<AST::TypeParam>>
2407 : Parser<ManagedTokenSource>::parse_type_params (EndTokenPred is_end_token)
2408 : {
2409 : std::vector<std::unique_ptr<AST::TypeParam>> type_params;
2410 :
2411 : while (!is_end_token (lexer.peek_token ()->get_id ()))
2412 : {
2413 : std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
2414 :
2415 : if (type_param == nullptr)
2416 : {
2417 : Error error (lexer.peek_token ()->get_locus (),
2418 : "failed to parse type param in type params");
2419 : add_error (std::move (error));
2420 :
2421 : return {};
2422 : }
2423 :
2424 : type_params.push_back (std::move (type_param));
2425 :
2426 : if (lexer.peek_token ()->get_id () != COMMA)
2427 : break;
2428 :
2429 : // skip commas, including trailing commas
2430 : lexer.skip_token ();
2431 : }
2432 :
2433 : type_params.shrink_to_fit ();
2434 : return type_params;
2435 : /* TODO: this shares most code with parse_lifetime_params - good place to
2436 : * use template (i.e. parse_non_ptr_sequence if doable) */
2437 : }
2438 :
2439 : /* Parses a single type (generic) parameter, not including commas. May change
2440 : * to return value. */
2441 : template <typename ManagedTokenSource>
2442 : std::unique_ptr<AST::TypeParam>
2443 0 : Parser<ManagedTokenSource>::parse_type_param ()
2444 : {
2445 : // parse outer attributes, which are optional and may not exist
2446 0 : auto outer_attrs = parse_outer_attributes ();
2447 :
2448 0 : const_TokenPtr identifier_tok = lexer.peek_token ();
2449 0 : if (identifier_tok->get_id () != IDENTIFIER)
2450 : {
2451 : // return null as type param can't exist without this required
2452 : // identifier
2453 0 : return nullptr;
2454 : }
2455 0 : Identifier ident{identifier_tok};
2456 0 : lexer.skip_token ();
2457 :
2458 : // parse type param bounds (if they exist)
2459 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
2460 0 : if (lexer.peek_token ()->get_id () == COLON)
2461 : {
2462 0 : lexer.skip_token ();
2463 :
2464 : // parse type param bounds, which may or may not exist
2465 0 : type_param_bounds = parse_type_param_bounds ();
2466 : }
2467 :
2468 : // parse type (if it exists)
2469 0 : std::unique_ptr<AST::Type> type = nullptr;
2470 0 : if (lexer.peek_token ()->get_id () == EQUAL)
2471 : {
2472 0 : lexer.skip_token ();
2473 :
2474 : // parse type (now required)
2475 0 : type = parse_type ();
2476 0 : if (type == nullptr)
2477 : {
2478 0 : Error error (lexer.peek_token ()->get_locus (),
2479 : "failed to parse type in type param");
2480 0 : add_error (std::move (error));
2481 :
2482 0 : return nullptr;
2483 0 : }
2484 : }
2485 :
2486 : return std::unique_ptr<AST::TypeParam> (
2487 0 : new AST::TypeParam (std::move (ident), identifier_tok->get_locus (),
2488 : std::move (type_param_bounds), std::move (type),
2489 0 : std::move (outer_attrs)));
2490 0 : }
2491 :
2492 : /* Parses regular (i.e. non-generic) parameters in functions or methods. Also
2493 : * has end token handling. */
2494 : template <typename ManagedTokenSource>
2495 : template <typename EndTokenPred>
2496 : std::vector<std::unique_ptr<AST::Param>>
2497 140215 : Parser<ManagedTokenSource>::parse_function_params (EndTokenPred is_end_token)
2498 : {
2499 140215 : std::vector<std::unique_ptr<AST::Param>> params;
2500 :
2501 280430 : if (is_end_token (lexer.peek_token ()->get_id ()))
2502 11701 : return params;
2503 :
2504 128514 : auto initial_param = parse_function_param ();
2505 :
2506 : // Return empty parameter list if no parameter there
2507 128514 : if (initial_param == nullptr)
2508 : {
2509 : // TODO: is this an error?
2510 1 : return params;
2511 : }
2512 :
2513 128513 : params.push_back (std::move (initial_param));
2514 :
2515 : // maybe think of a better control structure here - do-while with an initial
2516 : // error state? basically, loop through parameter list until can't find any
2517 : // more params
2518 128513 : const_TokenPtr t = lexer.peek_token ();
2519 188599 : while (t->get_id () == COMMA)
2520 : {
2521 : // skip comma if applies
2522 63566 : lexer.skip_token ();
2523 :
2524 : // TODO: strictly speaking, shouldn't there be no trailing comma?
2525 127132 : if (is_end_token (lexer.peek_token ()->get_id ()))
2526 : break;
2527 :
2528 : // now, as right paren would break, function param is required
2529 60086 : auto param = parse_function_param ();
2530 60086 : if (param == nullptr)
2531 : {
2532 0 : Error error (lexer.peek_token ()->get_locus (),
2533 : "failed to parse function param (in function params)");
2534 0 : add_error (std::move (error));
2535 :
2536 : // skip somewhere?
2537 0 : return std::vector<std::unique_ptr<AST::Param>> ();
2538 0 : }
2539 :
2540 60086 : params.push_back (std::move (param));
2541 :
2542 60086 : t = lexer.peek_token ();
2543 : }
2544 :
2545 128513 : params.shrink_to_fit ();
2546 128513 : return params;
2547 140215 : }
2548 :
2549 : /* Parses a single regular (i.e. non-generic) parameter in a function or
2550 : * method, i.e. the "name: type" bit. Also handles it not existing. */
2551 : template <typename ManagedTokenSource>
2552 : std::unique_ptr<AST::Param>
2553 188600 : Parser<ManagedTokenSource>::parse_function_param ()
2554 : {
2555 : // parse outer attributes if they exist
2556 188600 : AST::AttrVec outer_attrs = parse_outer_attributes ();
2557 :
2558 : // TODO: should saved location be at start of outer attributes or pattern?
2559 188600 : location_t locus = lexer.peek_token ()->get_locus ();
2560 :
2561 377200 : if (lexer.peek_token ()->get_id () == ELLIPSIS) // Unnamed variadic
2562 : {
2563 894 : lexer.skip_token (); // Skip ellipsis
2564 894 : return std::make_unique<AST::VariadicParam> (
2565 1788 : AST::VariadicParam (std::move (outer_attrs), locus));
2566 : }
2567 :
2568 187706 : std::unique_ptr<AST::Pattern> param_pattern = parse_pattern ();
2569 :
2570 : // create error function param if it doesn't exist
2571 187706 : if (param_pattern == nullptr)
2572 : {
2573 : // skip after something
2574 1 : return nullptr;
2575 : }
2576 :
2577 187705 : if (!skip_token (COLON))
2578 : {
2579 : // skip after something
2580 0 : return nullptr;
2581 : }
2582 :
2583 375410 : if (lexer.peek_token ()->get_id () == ELLIPSIS) // Named variadic
2584 : {
2585 11 : lexer.skip_token (); // Skip ellipsis
2586 11 : return std::make_unique<AST::VariadicParam> (
2587 22 : AST::VariadicParam (std::move (param_pattern), std::move (outer_attrs),
2588 11 : locus));
2589 : }
2590 : else
2591 : {
2592 187694 : std::unique_ptr<AST::Type> param_type = parse_type ();
2593 187694 : if (param_type == nullptr)
2594 : {
2595 0 : return nullptr;
2596 : }
2597 187694 : return std::make_unique<AST::FunctionParam> (
2598 375388 : AST::FunctionParam (std::move (param_pattern), std::move (param_type),
2599 187694 : std::move (outer_attrs), locus));
2600 187694 : }
2601 188600 : }
2602 :
2603 : /* Parses a function or method return type syntactical construction. Also
2604 : * handles a function return type not existing. */
2605 : template <typename ManagedTokenSource>
2606 : std::unique_ptr<AST::Type>
2607 190871 : Parser<ManagedTokenSource>::parse_function_return_type ()
2608 : {
2609 381742 : if (lexer.peek_token ()->get_id () != RETURN_TYPE)
2610 45241 : return nullptr;
2611 :
2612 : // skip return type, as it now obviously exists
2613 145630 : lexer.skip_token ();
2614 :
2615 145630 : std::unique_ptr<AST::Type> type = parse_type ();
2616 :
2617 145630 : return type;
2618 145630 : }
2619 :
2620 : /* Parses a "where clause" (in a function, struct, method, etc.). Also handles
2621 : * a where clause not existing, in which it will return
2622 : * WhereClause::create_empty(), which can be checked via
2623 : * WhereClause::is_empty(). */
2624 : template <typename ManagedTokenSource>
2625 : AST::WhereClause
2626 319032 : Parser<ManagedTokenSource>::parse_where_clause ()
2627 : {
2628 319032 : const_TokenPtr where_tok = lexer.peek_token ();
2629 319032 : if (where_tok->get_id () != WHERE)
2630 : {
2631 : // where clause doesn't exist, so create empty one
2632 308856 : return AST::WhereClause::create_empty ();
2633 : }
2634 :
2635 10176 : lexer.skip_token ();
2636 :
2637 : /* parse where clause items - this is not a separate rule in the reference
2638 : * so won't be here */
2639 10176 : std::vector<std::unique_ptr<AST::WhereClauseItem>> where_clause_items;
2640 :
2641 10176 : std::vector<AST::LifetimeParam> for_lifetimes;
2642 20352 : if (lexer.peek_token ()->get_id () == FOR)
2643 1 : for_lifetimes = parse_for_lifetimes ();
2644 :
2645 : /* HACK: where clauses end with a right curly or semicolon or equals in all
2646 : * uses currently */
2647 10176 : const_TokenPtr t = lexer.peek_token ();
2648 23801 : while (t->get_id () != LEFT_CURLY && t->get_id () != SEMICOLON
2649 21747 : && t->get_id () != EQUAL)
2650 : {
2651 13625 : std::unique_ptr<AST::WhereClauseItem> where_clause_item
2652 : = parse_where_clause_item (for_lifetimes);
2653 :
2654 13625 : if (where_clause_item == nullptr)
2655 : {
2656 0 : Error error (t->get_locus (), "failed to parse where clause item");
2657 0 : add_error (std::move (error));
2658 :
2659 0 : return AST::WhereClause::create_empty ();
2660 0 : }
2661 :
2662 13625 : where_clause_items.push_back (std::move (where_clause_item));
2663 :
2664 : // also skip comma if it exists
2665 27250 : if (lexer.peek_token ()->get_id () != COMMA)
2666 : break;
2667 :
2668 11571 : lexer.skip_token ();
2669 11571 : t = lexer.peek_token ();
2670 : }
2671 :
2672 10176 : where_clause_items.shrink_to_fit ();
2673 10176 : return AST::WhereClause (std::move (where_clause_items));
2674 10176 : }
2675 :
2676 : /* Parses a where clause item (lifetime or type bound). Does not parse any
2677 : * commas. */
2678 : template <typename ManagedTokenSource>
2679 : std::unique_ptr<AST::WhereClauseItem>
2680 13625 : Parser<ManagedTokenSource>::parse_where_clause_item (
2681 : const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
2682 : {
2683 : // shitty cheat way of determining lifetime or type bound - test for
2684 : // lifetime
2685 13625 : const_TokenPtr t = lexer.peek_token ();
2686 :
2687 13625 : if (t->get_id () == LIFETIME)
2688 14 : return parse_lifetime_where_clause_item ();
2689 : else
2690 13611 : return parse_type_bound_where_clause_item (outer_for_lifetimes);
2691 13625 : }
2692 :
2693 : // Parses a lifetime where clause item.
2694 : template <typename ManagedTokenSource>
2695 : std::unique_ptr<AST::LifetimeWhereClauseItem>
2696 14 : Parser<ManagedTokenSource>::parse_lifetime_where_clause_item ()
2697 : {
2698 14 : auto parsed_lifetime = parse_lifetime (false);
2699 14 : if (!parsed_lifetime)
2700 : {
2701 : // TODO: error here?
2702 0 : return nullptr;
2703 : }
2704 14 : auto lifetime = parsed_lifetime.value ();
2705 :
2706 14 : if (!skip_token (COLON))
2707 : {
2708 : // TODO: skip after somewhere
2709 0 : return nullptr;
2710 : }
2711 :
2712 14 : std::vector<AST::Lifetime> lifetime_bounds = parse_lifetime_bounds ();
2713 : // TODO: have end token passed in?
2714 :
2715 14 : location_t locus = lifetime.get_locus ();
2716 :
2717 : return std::unique_ptr<AST::LifetimeWhereClauseItem> (
2718 14 : new AST::LifetimeWhereClauseItem (std::move (lifetime),
2719 14 : std::move (lifetime_bounds), locus));
2720 28 : }
2721 :
2722 : // Parses a type bound where clause item.
2723 : template <typename ManagedTokenSource>
2724 : std::unique_ptr<AST::TypeBoundWhereClauseItem>
2725 13611 : Parser<ManagedTokenSource>::parse_type_bound_where_clause_item (
2726 : const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
2727 : {
2728 13611 : std::vector<AST::LifetimeParam> for_lifetimes = outer_for_lifetimes;
2729 :
2730 13611 : std::unique_ptr<AST::Type> type = parse_type ();
2731 13611 : if (type == nullptr)
2732 : {
2733 0 : return nullptr;
2734 : }
2735 :
2736 13611 : if (!skip_token (COLON))
2737 : {
2738 : // TODO: skip after somewhere
2739 0 : return nullptr;
2740 : }
2741 :
2742 27222 : if (lexer.peek_token ()->get_id () == FOR)
2743 : {
2744 20 : auto for_lifetimes_inner = parse_for_lifetimes ();
2745 20 : for_lifetimes.insert (for_lifetimes.end (), for_lifetimes_inner.begin (),
2746 : for_lifetimes_inner.end ());
2747 20 : }
2748 :
2749 : // parse type param bounds if they exist
2750 13611 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds
2751 : = parse_type_param_bounds ();
2752 :
2753 13611 : location_t locus = lexer.peek_token ()->get_locus ();
2754 :
2755 : return std::unique_ptr<AST::TypeBoundWhereClauseItem> (
2756 13611 : new AST::TypeBoundWhereClauseItem (std::move (for_lifetimes),
2757 : std::move (type),
2758 13611 : std::move (type_param_bounds), locus));
2759 13611 : }
2760 :
2761 : // Parses a for lifetimes clause, including the for keyword and angle
2762 : // brackets.
2763 : template <typename ManagedTokenSource>
2764 : std::vector<AST::LifetimeParam>
2765 44 : Parser<ManagedTokenSource>::parse_for_lifetimes ()
2766 : {
2767 44 : std::vector<AST::LifetimeParam> params;
2768 :
2769 44 : if (!skip_token (FOR))
2770 : {
2771 : // skip after somewhere?
2772 : return params;
2773 : }
2774 :
2775 44 : if (!skip_token (LEFT_ANGLE))
2776 : {
2777 : // skip after somewhere?
2778 : return params;
2779 : }
2780 :
2781 : /* cannot specify end token due to parsing problems with '>' tokens being
2782 : * nested */
2783 44 : params = parse_lifetime_params_objs (Parse::Utils::is_right_angle_tok);
2784 :
2785 44 : if (!skip_generics_right_angle ())
2786 : {
2787 : // DEBUG
2788 0 : rust_debug ("failed to skip generics right angle after (supposedly) "
2789 : "finished parsing where clause items");
2790 : // ok, well this gets called.
2791 :
2792 : // skip after somewhere?
2793 : return params;
2794 : }
2795 :
2796 : return params;
2797 : }
2798 :
2799 : // Parses type parameter bounds in where clause or generic arguments.
2800 : template <typename ManagedTokenSource>
2801 : std::vector<std::unique_ptr<AST::TypeParamBound>>
2802 36506 : Parser<ManagedTokenSource>::parse_type_param_bounds ()
2803 : {
2804 36506 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
2805 :
2806 36506 : std::unique_ptr<AST::TypeParamBound> initial_bound
2807 : = parse_type_param_bound ();
2808 :
2809 : // quick exit if null
2810 36506 : if (initial_bound == nullptr)
2811 : {
2812 : /* error? type param bounds must have at least one term, but are bounds
2813 : * optional? */
2814 : return type_param_bounds;
2815 : }
2816 36506 : type_param_bounds.push_back (std::move (initial_bound));
2817 :
2818 78288 : while (lexer.peek_token ()->get_id () == PLUS)
2819 : {
2820 2638 : lexer.skip_token ();
2821 :
2822 2638 : std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
2823 2638 : if (bound == nullptr)
2824 : {
2825 : /* not an error: bound is allowed to be null as trailing plus is
2826 : * allowed */
2827 : return type_param_bounds;
2828 : }
2829 :
2830 2638 : type_param_bounds.push_back (std::move (bound));
2831 : }
2832 :
2833 36506 : type_param_bounds.shrink_to_fit ();
2834 : return type_param_bounds;
2835 36506 : }
2836 :
2837 : /* Parses type parameter bounds in where clause or generic arguments, with end
2838 : * token handling. */
2839 : template <typename ManagedTokenSource>
2840 : template <typename EndTokenPred>
2841 : std::vector<std::unique_ptr<AST::TypeParamBound>>
2842 1551 : Parser<ManagedTokenSource>::parse_type_param_bounds (EndTokenPred is_end_token)
2843 : {
2844 1551 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
2845 :
2846 1551 : std::unique_ptr<AST::TypeParamBound> initial_bound
2847 : = parse_type_param_bound ();
2848 :
2849 : // quick exit if null
2850 1551 : if (initial_bound == nullptr)
2851 : {
2852 : /* error? type param bounds must have at least one term, but are bounds
2853 : * optional? */
2854 0 : return type_param_bounds;
2855 : }
2856 1551 : type_param_bounds.push_back (std::move (initial_bound));
2857 :
2858 3928 : while (lexer.peek_token ()->get_id () == PLUS)
2859 : {
2860 413 : lexer.skip_token ();
2861 :
2862 : // break if end token character
2863 826 : if (is_end_token (lexer.peek_token ()->get_id ()))
2864 : break;
2865 :
2866 413 : std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
2867 413 : if (bound == nullptr)
2868 : {
2869 : // TODO how wise is it to ditch all bounds if only one failed?
2870 0 : Error error (lexer.peek_token ()->get_locus (),
2871 : "failed to parse type param bound in type param bounds");
2872 0 : add_error (std::move (error));
2873 :
2874 0 : return {};
2875 0 : }
2876 :
2877 413 : type_param_bounds.push_back (std::move (bound));
2878 : }
2879 :
2880 1551 : type_param_bounds.shrink_to_fit ();
2881 1551 : return type_param_bounds;
2882 1551 : }
2883 :
2884 : /* Parses a single type parameter bound in a where clause or generic argument.
2885 : * Does not parse the '+' between arguments. */
2886 : template <typename ManagedTokenSource>
2887 : std::unique_ptr<AST::TypeParamBound>
2888 41688 : Parser<ManagedTokenSource>::parse_type_param_bound ()
2889 : {
2890 : // shitty cheat way of determining lifetime or trait bound - test for
2891 : // lifetime
2892 41688 : const_TokenPtr t = lexer.peek_token ();
2893 41688 : switch (t->get_id ())
2894 : {
2895 1404 : case LIFETIME:
2896 1404 : return std::unique_ptr<AST::Lifetime> (
2897 2808 : new AST::Lifetime (parse_lifetime (false).value ()));
2898 40284 : case LEFT_PAREN:
2899 : case QUESTION_MARK:
2900 : case FOR:
2901 : case IDENTIFIER:
2902 : case SUPER:
2903 : case SELF:
2904 : case SELF_ALIAS:
2905 : case CRATE:
2906 : case DOLLAR_SIGN:
2907 : case SCOPE_RESOLUTION:
2908 40284 : return parse_trait_bound ();
2909 0 : default:
2910 : // don't error - assume this is fine TODO
2911 0 : return nullptr;
2912 : }
2913 41688 : }
2914 :
2915 : // Parses a trait bound type param bound.
2916 : template <typename ManagedTokenSource>
2917 : std::unique_ptr<AST::TraitBound>
2918 42561 : Parser<ManagedTokenSource>::parse_trait_bound ()
2919 : {
2920 42561 : bool has_parens = false;
2921 42561 : bool has_question_mark = false;
2922 :
2923 42561 : location_t locus = lexer.peek_token ()->get_locus ();
2924 :
2925 : /* parse optional `for lifetimes`. */
2926 42561 : std::vector<AST::LifetimeParam> for_lifetimes;
2927 85122 : if (lexer.peek_token ()->get_id () == FOR)
2928 7 : for_lifetimes = parse_for_lifetimes ();
2929 :
2930 : // handle trait bound being in parentheses
2931 85122 : if (lexer.peek_token ()->get_id () == LEFT_PAREN)
2932 : {
2933 0 : has_parens = true;
2934 0 : lexer.skip_token ();
2935 : }
2936 :
2937 : // handle having question mark (optional)
2938 85122 : if (lexer.peek_token ()->get_id () == QUESTION_MARK)
2939 : {
2940 4779 : has_question_mark = true;
2941 4779 : lexer.skip_token ();
2942 : }
2943 :
2944 : // handle TypePath
2945 42561 : AST::TypePath type_path = parse_type_path ();
2946 42561 : if (type_path.is_error ())
2947 2 : return nullptr;
2948 :
2949 : // handle closing parentheses
2950 42559 : if (has_parens)
2951 : {
2952 0 : if (!skip_token (RIGHT_PAREN))
2953 : {
2954 0 : return nullptr;
2955 : }
2956 : }
2957 :
2958 : return std::unique_ptr<AST::TraitBound> (
2959 42559 : new AST::TraitBound (std::move (type_path), locus, has_parens,
2960 42559 : has_question_mark, std::move (for_lifetimes)));
2961 42561 : }
2962 :
2963 : // Parses lifetime bounds.
2964 : template <typename ManagedTokenSource>
2965 : std::vector<AST::Lifetime>
2966 14 : Parser<ManagedTokenSource>::parse_lifetime_bounds ()
2967 : {
2968 14 : std::vector<AST::Lifetime> lifetime_bounds;
2969 :
2970 14 : while (true)
2971 : {
2972 14 : auto lifetime = parse_lifetime (false);
2973 :
2974 : // quick exit for parsing failure
2975 14 : if (!lifetime)
2976 : break;
2977 :
2978 14 : lifetime_bounds.push_back (std::move (lifetime.value ()));
2979 :
2980 : /* plus is maybe not allowed at end - spec defines it weirdly, so
2981 : * assuming allowed at end */
2982 28 : if (lexer.peek_token ()->get_id () != PLUS)
2983 : break;
2984 :
2985 0 : lexer.skip_token ();
2986 : }
2987 :
2988 14 : lifetime_bounds.shrink_to_fit ();
2989 14 : return lifetime_bounds;
2990 : }
2991 :
2992 : // Parses lifetime bounds, with added check for ending token.
2993 : template <typename ManagedTokenSource>
2994 : template <typename EndTokenPred>
2995 : std::vector<AST::Lifetime>
2996 223 : Parser<ManagedTokenSource>::parse_lifetime_bounds (EndTokenPred is_end_token)
2997 : {
2998 223 : std::vector<AST::Lifetime> lifetime_bounds;
2999 :
3000 928 : while (!is_end_token (lexer.peek_token ()->get_id ()))
3001 : {
3002 235 : auto lifetime = parse_lifetime (false);
3003 :
3004 235 : if (!lifetime)
3005 : {
3006 : /* TODO: is it worth throwing away all lifetime bound info just
3007 : * because one failed? */
3008 0 : Error error (lexer.peek_token ()->get_locus (),
3009 : "failed to parse lifetime in lifetime bounds");
3010 0 : add_error (std::move (error));
3011 :
3012 0 : return {};
3013 0 : }
3014 :
3015 235 : lifetime_bounds.push_back (std::move (lifetime.value ()));
3016 :
3017 : /* plus is maybe not allowed at end - spec defines it weirdly, so
3018 : * assuming allowed at end */
3019 470 : if (lexer.peek_token ()->get_id () != PLUS)
3020 : break;
3021 :
3022 12 : lexer.skip_token ();
3023 : }
3024 :
3025 223 : lifetime_bounds.shrink_to_fit ();
3026 223 : return lifetime_bounds;
3027 223 : }
3028 :
3029 : /* Parses a lifetime token (named, 'static, or '_). Also handles lifetime not
3030 : * existing. */
3031 : template <typename ManagedTokenSource>
3032 : tl::expected<AST::Lifetime, Parse::Error::Lifetime>
3033 232590 : Parser<ManagedTokenSource>::parse_lifetime (bool allow_elided)
3034 : {
3035 232590 : const_TokenPtr lifetime_tok = lexer.peek_token ();
3036 232590 : if (lifetime_tok->get_id () != LIFETIME)
3037 : {
3038 183423 : if (allow_elided)
3039 : {
3040 0 : return AST::Lifetime::elided ();
3041 : }
3042 : else
3043 : {
3044 183423 : return tl::make_unexpected<Parse::Error::Lifetime> ({});
3045 : }
3046 : }
3047 49167 : lexer.skip_token ();
3048 :
3049 98334 : return lifetime_from_token (lifetime_tok);
3050 232590 : }
3051 :
3052 : template <typename ManagedTokenSource>
3053 : AST::Lifetime
3054 49250 : Parser<ManagedTokenSource>::lifetime_from_token (const_TokenPtr tok)
3055 : {
3056 49250 : location_t locus = tok->get_locus ();
3057 49250 : std::string lifetime_ident = tok->get_str ();
3058 :
3059 49250 : if (lifetime_ident == "static")
3060 : {
3061 549 : return AST::Lifetime (AST::Lifetime::STATIC, "", locus);
3062 : }
3063 48701 : else if (lifetime_ident == "_")
3064 : {
3065 : // Explicitly and implicitly elided lifetimes follow the same rules.
3066 9169 : return AST::Lifetime (AST::Lifetime::WILDCARD, "", locus);
3067 : }
3068 : else
3069 : {
3070 79064 : return AST::Lifetime (AST::Lifetime::NAMED, std::move (lifetime_ident),
3071 39532 : locus);
3072 : }
3073 49250 : }
3074 :
3075 : template <typename ManagedTokenSource>
3076 : std::unique_ptr<AST::ExternalTypeItem>
3077 19 : Parser<ManagedTokenSource>::parse_external_type_item (AST::Visibility vis,
3078 : AST::AttrVec outer_attrs)
3079 : {
3080 19 : location_t locus = lexer.peek_token ()->get_locus ();
3081 19 : skip_token (TYPE);
3082 :
3083 19 : const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
3084 19 : if (alias_name_tok == nullptr)
3085 : {
3086 0 : Error error (lexer.peek_token ()->get_locus (),
3087 : "could not parse identifier in external opaque type");
3088 0 : add_error (std::move (error));
3089 :
3090 0 : skip_after_semicolon ();
3091 0 : return nullptr;
3092 0 : }
3093 :
3094 19 : if (!skip_token (SEMICOLON))
3095 1 : return nullptr;
3096 :
3097 : return std::unique_ptr<AST::ExternalTypeItem> (
3098 54 : new AST::ExternalTypeItem (alias_name_tok->get_str (), std::move (vis),
3099 18 : std::move (outer_attrs), std::move (locus)));
3100 19 : }
3101 :
3102 : // Parses a "type alias" (typedef) item.
3103 : template <typename ManagedTokenSource>
3104 : std::unique_ptr<AST::TypeAlias>
3105 33572 : Parser<ManagedTokenSource>::parse_type_alias (AST::Visibility vis,
3106 : AST::AttrVec outer_attrs)
3107 : {
3108 33572 : location_t locus = lexer.peek_token ()->get_locus ();
3109 33572 : skip_token (TYPE);
3110 :
3111 : // TODO: use this token for identifier when finished that
3112 33572 : const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
3113 33572 : if (alias_name_tok == nullptr)
3114 : {
3115 0 : Error error (lexer.peek_token ()->get_locus (),
3116 : "could not parse identifier in type alias");
3117 0 : add_error (std::move (error));
3118 :
3119 0 : skip_after_semicolon ();
3120 0 : return nullptr;
3121 0 : }
3122 67144 : Identifier alias_name{alias_name_tok};
3123 :
3124 : // parse generic params, which may not exist
3125 33572 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
3126 : = parse_generic_params_in_angles ();
3127 :
3128 : // parse where clause, which may not exist
3129 33572 : AST::WhereClause where_clause = parse_where_clause ();
3130 :
3131 33572 : if (!skip_token (EQUAL))
3132 : {
3133 0 : skip_after_semicolon ();
3134 0 : return nullptr;
3135 : }
3136 :
3137 33572 : std::unique_ptr<AST::Type> type_to_alias = parse_type ();
3138 :
3139 33572 : if (!skip_token (SEMICOLON))
3140 : {
3141 : // should be skipping past this, not the next line
3142 0 : return nullptr;
3143 : }
3144 :
3145 : return std::unique_ptr<AST::TypeAlias> (
3146 33572 : new AST::TypeAlias (std::move (alias_name), std::move (generic_params),
3147 : std::move (where_clause), std::move (type_to_alias),
3148 33572 : std::move (vis), std::move (outer_attrs), locus));
3149 67144 : }
3150 :
3151 : // Parse a struct item AST node.
3152 : template <typename ManagedTokenSource>
3153 : std::unique_ptr<AST::Struct>
3154 11037 : Parser<ManagedTokenSource>::parse_struct (AST::Visibility vis,
3155 : AST::AttrVec outer_attrs)
3156 : {
3157 : /* TODO: determine best way to parse the proper struct vs tuple struct -
3158 : * share most of initial constructs so lookahead might be impossible, and if
3159 : * not probably too expensive. Best way is probably unified parsing for the
3160 : * initial parts and then pass them in as params to more derived functions.
3161 : * Alternatively, just parse everything in this one function - do this if
3162 : * function not too long. */
3163 :
3164 : /* Proper struct <- 'struct' IDENTIFIER generic_params? where_clause? ( '{'
3165 : * struct_fields? '}' | ';' ) */
3166 : /* Tuple struct <- 'struct' IDENTIFIER generic_params? '(' tuple_fields? ')'
3167 : * where_clause? ';' */
3168 11037 : location_t locus = lexer.peek_token ()->get_locus ();
3169 11037 : skip_token (STRUCT_KW);
3170 :
3171 : // parse struct name
3172 11037 : const_TokenPtr name_tok = expect_token (IDENTIFIER);
3173 11037 : if (name_tok == nullptr)
3174 : {
3175 : // skip after somewhere?
3176 1 : return nullptr;
3177 : }
3178 22072 : Identifier struct_name{name_tok};
3179 :
3180 : // parse generic params, which may or may not exist
3181 11036 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
3182 : = parse_generic_params_in_angles ();
3183 :
3184 : // branch on next token - determines whether proper struct or tuple struct
3185 22072 : if (lexer.peek_token ()->get_id () == LEFT_PAREN)
3186 : {
3187 : // tuple struct
3188 :
3189 : // skip left parenthesis
3190 2360 : lexer.skip_token ();
3191 :
3192 : // parse tuple fields
3193 2360 : std::vector<AST::TupleField> tuple_fields;
3194 : // Might be empty tuple for unit tuple struct.
3195 4720 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
3196 24 : tuple_fields = std::vector<AST::TupleField> ();
3197 : else
3198 2336 : tuple_fields = parse_tuple_fields ();
3199 :
3200 : // tuple parameters must have closing parenthesis
3201 2360 : if (!skip_token (RIGHT_PAREN))
3202 : {
3203 1 : skip_after_semicolon ();
3204 1 : return nullptr;
3205 : }
3206 :
3207 : // parse where clause, which is optional
3208 2359 : AST::WhereClause where_clause = parse_where_clause ();
3209 :
3210 2359 : if (!skip_token (SEMICOLON))
3211 : {
3212 : // can't skip after semicolon because it's meant to be here
3213 0 : return nullptr;
3214 : }
3215 :
3216 2359 : return std::unique_ptr<AST::TupleStruct> (
3217 2359 : new AST::TupleStruct (std::move (tuple_fields), std::move (struct_name),
3218 : std::move (generic_params),
3219 : std::move (where_clause), std::move (vis),
3220 2359 : std::move (outer_attrs), locus));
3221 2360 : }
3222 :
3223 : // assume it is a proper struct being parsed and continue outside of switch
3224 : // - label only here to suppress warning
3225 :
3226 : // parse where clause, which is optional
3227 8676 : AST::WhereClause where_clause = parse_where_clause ();
3228 :
3229 : // branch on next token - determines whether struct is a unit struct
3230 8676 : const_TokenPtr t = lexer.peek_token ();
3231 8676 : switch (t->get_id ())
3232 : {
3233 3363 : case LEFT_CURLY:
3234 : {
3235 : // struct with body
3236 :
3237 : // skip curly bracket
3238 3363 : lexer.skip_token ();
3239 :
3240 : // parse struct fields, if any
3241 3363 : std::vector<AST::StructField> struct_fields
3242 : = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
3243 :
3244 3363 : if (!skip_token (RIGHT_CURLY))
3245 : {
3246 : // skip somewhere?
3247 0 : return nullptr;
3248 : }
3249 :
3250 3363 : return std::unique_ptr<AST::StructStruct> (new AST::StructStruct (
3251 : std::move (struct_fields), std::move (struct_name),
3252 : std::move (generic_params), std::move (where_clause), false,
3253 3363 : std::move (vis), std::move (outer_attrs), locus));
3254 3363 : }
3255 5312 : case SEMICOLON:
3256 : // unit struct declaration
3257 :
3258 5312 : lexer.skip_token ();
3259 :
3260 5312 : return std::unique_ptr<AST::StructStruct> (
3261 10624 : new AST::StructStruct (std::move (struct_name),
3262 : std::move (generic_params),
3263 : std::move (where_clause), std::move (vis),
3264 5312 : std::move (outer_attrs), locus));
3265 1 : default:
3266 1 : add_error (Error (t->get_locus (),
3267 : "unexpected token %qs in struct declaration",
3268 : t->get_token_description ()));
3269 :
3270 : // skip somewhere?
3271 1 : return nullptr;
3272 : }
3273 11036 : }
3274 :
3275 : // Parses struct fields in struct declarations.
3276 : template <typename ManagedTokenSource>
3277 : std::vector<AST::StructField>
3278 0 : Parser<ManagedTokenSource>::parse_struct_fields ()
3279 : {
3280 0 : std::vector<AST::StructField> fields;
3281 :
3282 0 : AST::StructField initial_field = parse_struct_field ();
3283 :
3284 : // Return empty field list if no field there
3285 0 : if (initial_field.is_error ())
3286 : return fields;
3287 :
3288 0 : fields.push_back (std::move (initial_field));
3289 :
3290 0 : while (lexer.peek_token ()->get_id () == COMMA)
3291 : {
3292 0 : lexer.skip_token ();
3293 :
3294 0 : AST::StructField field = parse_struct_field ();
3295 :
3296 0 : if (field.is_error ())
3297 : {
3298 : // would occur with trailing comma, so allowed
3299 : break;
3300 : }
3301 :
3302 0 : fields.push_back (std::move (field));
3303 : }
3304 :
3305 0 : fields.shrink_to_fit ();
3306 : return fields;
3307 : // TODO: template if possible (parse_non_ptr_seq)
3308 0 : }
3309 :
3310 : // Parses struct fields in struct declarations.
3311 : template <typename ManagedTokenSource>
3312 : template <typename EndTokenPred>
3313 : std::vector<AST::StructField>
3314 3605 : Parser<ManagedTokenSource>::parse_struct_fields (EndTokenPred is_end_tok)
3315 : {
3316 3605 : std::vector<AST::StructField> fields;
3317 :
3318 3605 : AST::StructField initial_field = parse_struct_field ();
3319 :
3320 : // Return empty field list if no field there
3321 3605 : if (initial_field.is_error ())
3322 60 : return fields;
3323 :
3324 3545 : fields.push_back (std::move (initial_field));
3325 :
3326 14260 : while (lexer.peek_token ()->get_id () == COMMA)
3327 : {
3328 6918 : lexer.skip_token ();
3329 :
3330 13836 : if (is_end_tok (lexer.peek_token ()->get_id ()))
3331 : break;
3332 :
3333 3585 : AST::StructField field = parse_struct_field ();
3334 3585 : if (field.is_error ())
3335 : {
3336 : /* TODO: should every field be ditched just because one couldn't be
3337 : * parsed? */
3338 0 : Error error (lexer.peek_token ()->get_locus (),
3339 : "failed to parse struct field in struct fields");
3340 0 : add_error (std::move (error));
3341 :
3342 0 : return {};
3343 0 : }
3344 :
3345 3585 : fields.push_back (std::move (field));
3346 : }
3347 :
3348 3545 : fields.shrink_to_fit ();
3349 3545 : return fields;
3350 : // TODO: template if possible (parse_non_ptr_seq)
3351 3605 : }
3352 :
3353 : // Parses a single struct field (in a struct definition). Does not parse
3354 : // commas.
3355 : template <typename ManagedTokenSource>
3356 : AST::StructField
3357 7190 : Parser<ManagedTokenSource>::parse_struct_field ()
3358 : {
3359 : // parse outer attributes, if they exist
3360 7190 : AST::AttrVec outer_attrs = parse_outer_attributes ();
3361 :
3362 : // parse visibility, if it exists
3363 7190 : auto vis = parse_visibility ();
3364 7190 : if (!vis)
3365 0 : return AST::StructField::create_error ();
3366 :
3367 7190 : location_t locus = lexer.peek_token ()->get_locus ();
3368 :
3369 : // parse field name
3370 7190 : const_TokenPtr field_name_tok = lexer.peek_token ();
3371 7190 : if (field_name_tok->get_id () != IDENTIFIER)
3372 : {
3373 : // if not identifier, assumes there is no struct field and exits - not
3374 : // necessarily error
3375 60 : return AST::StructField::create_error ();
3376 : }
3377 14260 : Identifier field_name{field_name_tok};
3378 7130 : lexer.skip_token ();
3379 :
3380 7130 : if (!skip_token (COLON))
3381 : {
3382 : // skip after somewhere?
3383 0 : return AST::StructField::create_error ();
3384 : }
3385 :
3386 : // parse field type - this is required
3387 7130 : std::unique_ptr<AST::Type> field_type = parse_type ();
3388 7130 : if (field_type == nullptr)
3389 : {
3390 0 : Error error (lexer.peek_token ()->get_locus (),
3391 : "could not parse type in struct field definition");
3392 0 : add_error (std::move (error));
3393 :
3394 : // skip after somewhere
3395 0 : return AST::StructField::create_error ();
3396 0 : }
3397 :
3398 14260 : return AST::StructField (std::move (field_name), std::move (field_type),
3399 7130 : std::move (vis.value ()), locus,
3400 7130 : std::move (outer_attrs));
3401 28640 : }
3402 :
3403 : // Parses tuple fields in tuple/tuple struct declarations.
3404 : template <typename ManagedTokenSource>
3405 : std::vector<AST::TupleField>
3406 3190 : Parser<ManagedTokenSource>::parse_tuple_fields ()
3407 : {
3408 3190 : std::vector<AST::TupleField> fields;
3409 :
3410 3190 : AST::TupleField initial_field = parse_tuple_field ();
3411 :
3412 : // Return empty field list if no field there
3413 3190 : if (initial_field.is_error ())
3414 : {
3415 1 : return fields;
3416 : }
3417 :
3418 3189 : fields.push_back (std::move (initial_field));
3419 :
3420 : // maybe think of a better control structure here - do-while with an initial
3421 : // error state? basically, loop through field list until can't find any more
3422 : // params HACK: all current syntax uses of tuple fields have them ending
3423 : // with a right paren token
3424 3189 : const_TokenPtr t = lexer.peek_token ();
3425 8415 : while (t->get_id () == COMMA)
3426 : {
3427 : // skip comma if applies - e.g. trailing comma
3428 5240 : lexer.skip_token ();
3429 :
3430 : // break out due to right paren if it exists
3431 10480 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
3432 : {
3433 : break;
3434 : }
3435 :
3436 5226 : AST::TupleField field = parse_tuple_field ();
3437 5226 : if (field.is_error ())
3438 : {
3439 0 : Error error (lexer.peek_token ()->get_locus (),
3440 : "failed to parse tuple field in tuple fields");
3441 0 : add_error (std::move (error));
3442 :
3443 0 : return std::vector<AST::TupleField> ();
3444 0 : }
3445 :
3446 5226 : fields.push_back (std::move (field));
3447 :
3448 5226 : t = lexer.peek_token ();
3449 : }
3450 :
3451 3189 : fields.shrink_to_fit ();
3452 3189 : return fields;
3453 :
3454 : // TODO: this shares basically all code with function params and struct
3455 : // fields
3456 : // - templates?
3457 3190 : }
3458 :
3459 : /* Parses a single tuple struct field in a tuple struct definition. Does not
3460 : * parse commas. */
3461 : template <typename ManagedTokenSource>
3462 : AST::TupleField
3463 8416 : Parser<ManagedTokenSource>::parse_tuple_field ()
3464 : {
3465 : // parse outer attributes if they exist
3466 8416 : AST::AttrVec outer_attrs = parse_outer_attributes ();
3467 :
3468 : // parse visibility if it exists
3469 8416 : auto visibility = parse_visibility ();
3470 8416 : if (!visibility)
3471 0 : return AST::TupleField::create_error ();
3472 :
3473 8416 : location_t locus = lexer.peek_token ()->get_locus ();
3474 :
3475 : // parse type, which is required
3476 8416 : std::unique_ptr<AST::Type> field_type = parse_type ();
3477 8416 : if (field_type == nullptr)
3478 : {
3479 : // error if null
3480 1 : Error error (lexer.peek_token ()->get_locus (),
3481 : "could not parse type in tuple struct field");
3482 1 : add_error (std::move (error));
3483 :
3484 : // skip after something
3485 1 : return AST::TupleField::create_error ();
3486 1 : }
3487 :
3488 8415 : return AST::TupleField (std::move (field_type),
3489 8415 : std::move (visibility.value ()), locus,
3490 8415 : std::move (outer_attrs));
3491 16832 : }
3492 :
3493 : // Parses a Rust "enum" tagged union item definition.
3494 : template <typename ManagedTokenSource>
3495 : std::unique_ptr<AST::Enum>
3496 1046 : Parser<ManagedTokenSource>::parse_enum (AST::Visibility vis,
3497 : AST::AttrVec outer_attrs)
3498 : {
3499 1046 : location_t locus = lexer.peek_token ()->get_locus ();
3500 1046 : skip_token (ENUM_KW);
3501 :
3502 : // parse enum name
3503 1046 : const_TokenPtr enum_name_tok = expect_token (IDENTIFIER);
3504 1046 : if (enum_name_tok == nullptr)
3505 1 : return nullptr;
3506 :
3507 2090 : Identifier enum_name = {enum_name_tok};
3508 :
3509 : // parse generic params (of enum container, not enum variants) if they exist
3510 1045 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
3511 : = parse_generic_params_in_angles ();
3512 :
3513 : // parse where clause if it exists
3514 1045 : AST::WhereClause where_clause = parse_where_clause ();
3515 :
3516 1045 : if (!skip_token (LEFT_CURLY))
3517 : {
3518 0 : skip_after_end_block ();
3519 0 : return nullptr;
3520 : }
3521 :
3522 : // parse actual enum variant definitions
3523 1045 : std::vector<std::unique_ptr<AST::EnumItem>> enum_items
3524 : = parse_enum_items ([] (TokenId id) { return id == RIGHT_CURLY; });
3525 :
3526 1045 : if (!skip_token (RIGHT_CURLY))
3527 : {
3528 1 : skip_after_end_block ();
3529 1 : return nullptr;
3530 : }
3531 :
3532 : return std::unique_ptr<AST::Enum> (
3533 1044 : new AST::Enum (std::move (enum_name), std::move (vis),
3534 : std::move (generic_params), std::move (where_clause),
3535 1044 : std::move (enum_items), std::move (outer_attrs), locus));
3536 2090 : }
3537 :
3538 : // Parses the enum variants inside an enum definiton.
3539 : template <typename ManagedTokenSource>
3540 : std::vector<std::unique_ptr<AST::EnumItem>>
3541 0 : Parser<ManagedTokenSource>::parse_enum_items ()
3542 : {
3543 0 : std::vector<std::unique_ptr<AST::EnumItem>> items;
3544 :
3545 0 : auto initial_item = parse_enum_item ();
3546 :
3547 : // Return empty item list if no field there
3548 0 : if (!initial_item)
3549 : return items;
3550 :
3551 0 : items.push_back (std::move (initial_item.value ()));
3552 :
3553 0 : while (lexer.peek_token ()->get_id () == COMMA)
3554 : {
3555 0 : lexer.skip_token ();
3556 :
3557 0 : auto item = parse_enum_item ();
3558 0 : if (!item)
3559 : {
3560 : // this would occur with a trailing comma, which is allowed
3561 : break;
3562 : }
3563 :
3564 0 : items.push_back (std::move (item.value ()));
3565 : }
3566 :
3567 0 : items.shrink_to_fit ();
3568 : return items;
3569 :
3570 : /* TODO: use template if doable (parse_non_ptr_sequence) */
3571 0 : }
3572 :
3573 : // Parses the enum variants inside an enum definiton.
3574 : template <typename ManagedTokenSource>
3575 : template <typename EndTokenPred>
3576 : std::vector<std::unique_ptr<AST::EnumItem>>
3577 1045 : Parser<ManagedTokenSource>::parse_enum_items (EndTokenPred is_end_tok)
3578 : {
3579 1045 : std::vector<std::unique_ptr<AST::EnumItem>> items;
3580 :
3581 1045 : auto initial_item = parse_enum_item ();
3582 :
3583 : // Return empty item list if no field there
3584 1045 : if (!initial_item)
3585 82 : return items;
3586 :
3587 963 : items.push_back (std::move (initial_item.value ()));
3588 :
3589 5142 : while (lexer.peek_token ()->get_id () == COMMA)
3590 : {
3591 2541 : lexer.skip_token ();
3592 :
3593 5082 : if (is_end_tok (lexer.peek_token ()->get_id ()))
3594 : break;
3595 :
3596 1608 : auto item = parse_enum_item ();
3597 1608 : if (!item)
3598 : {
3599 : /* TODO should this ignore all successfully parsed enum items just
3600 : * because one failed? */
3601 0 : Error error (lexer.peek_token ()->get_locus (),
3602 : "failed to parse enum item in enum items");
3603 0 : add_error (std::move (error));
3604 :
3605 0 : return {};
3606 0 : }
3607 :
3608 1608 : items.push_back (std::move (item.value ()));
3609 : }
3610 :
3611 963 : items.shrink_to_fit ();
3612 963 : return items;
3613 :
3614 : /* TODO: use template if doable (parse_non_ptr_sequence) */
3615 1045 : }
3616 :
3617 : /* Parses a single enum variant item in an enum definition. Does not parse
3618 : * commas. */
3619 : template <typename ManagedTokenSource>
3620 : tl::expected<std::unique_ptr<AST::EnumItem>, Parse::Error::EnumVariant>
3621 2653 : Parser<ManagedTokenSource>::parse_enum_item ()
3622 : {
3623 : // parse outer attributes if they exist
3624 2653 : AST::AttrVec outer_attrs = parse_outer_attributes ();
3625 :
3626 : // parse visibility, which may or may not exist
3627 2653 : auto vis_res = parse_visibility ();
3628 2653 : if (!vis_res)
3629 0 : return Parse::Error::EnumVariant::make_child_error ();
3630 2653 : auto vis = vis_res.value ();
3631 :
3632 : // parse name for enum item, which is required
3633 2653 : const_TokenPtr item_name_tok = lexer.peek_token ();
3634 2653 : if (item_name_tok->get_id () != IDENTIFIER)
3635 : {
3636 : // this may not be an error but it means there is no enum item here
3637 164 : return Parse::Error::EnumVariant::make_not_identifier (item_name_tok);
3638 : }
3639 2571 : lexer.skip_token ();
3640 5142 : Identifier item_name{item_name_tok};
3641 :
3642 : // branch based on next token
3643 2571 : const_TokenPtr t = lexer.peek_token ();
3644 2571 : switch (t->get_id ())
3645 : {
3646 877 : case LEFT_PAREN:
3647 : {
3648 : // tuple enum item
3649 877 : lexer.skip_token ();
3650 :
3651 877 : std::vector<AST::TupleField> tuple_fields;
3652 : // Might be empty tuple for unit tuple enum variant.
3653 1754 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
3654 23 : tuple_fields = std::vector<AST::TupleField> ();
3655 : else
3656 854 : tuple_fields = parse_tuple_fields ();
3657 :
3658 877 : if (!skip_token (RIGHT_PAREN))
3659 : {
3660 : // skip after somewhere
3661 0 : return Parse::Error::EnumVariant::make_unfinished_tuple_variant ();
3662 : }
3663 :
3664 877 : return std::unique_ptr<AST::EnumItemTuple> (new AST::EnumItemTuple (
3665 : std::move (item_name), std::move (vis), std::move (tuple_fields),
3666 877 : std::move (outer_attrs), item_name_tok->get_locus ()));
3667 877 : }
3668 103 : case LEFT_CURLY:
3669 : {
3670 : // struct enum item
3671 103 : lexer.skip_token ();
3672 :
3673 103 : std::vector<AST::StructField> struct_fields
3674 : = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
3675 :
3676 103 : if (!skip_token (RIGHT_CURLY))
3677 : {
3678 : // skip after somewhere
3679 0 : return Parse::Error::EnumVariant::make_unfinished_tuple_variant ();
3680 : }
3681 :
3682 103 : return std::unique_ptr<AST::EnumItemStruct> (new AST::EnumItemStruct (
3683 : std::move (item_name), std::move (vis), std::move (struct_fields),
3684 103 : std::move (outer_attrs), item_name_tok->get_locus ()));
3685 103 : }
3686 332 : case EQUAL:
3687 : {
3688 : // discriminant enum item
3689 332 : lexer.skip_token ();
3690 :
3691 332 : auto discriminant_expr = parse_expr ();
3692 332 : if (!discriminant_expr)
3693 0 : return Parse::Error::EnumVariant::make_child_error ();
3694 :
3695 332 : return std::make_unique<AST::EnumItemDiscriminant> (
3696 : std::move (item_name), std::move (vis),
3697 332 : std::move (discriminant_expr.value ()), std::move (outer_attrs),
3698 664 : item_name_tok->get_locus ());
3699 332 : }
3700 1259 : default:
3701 : // regular enum with just an identifier
3702 1259 : return std::make_unique<AST::EnumItem> (std::move (item_name),
3703 : std::move (vis),
3704 : std::move (outer_attrs),
3705 1259 : item_name_tok->get_locus ());
3706 : }
3707 7877 : }
3708 :
3709 : // Parses a C-style (and C-compat) untagged union declaration.
3710 : template <typename ManagedTokenSource>
3711 : std::unique_ptr<AST::Union>
3712 139 : Parser<ManagedTokenSource>::parse_union (AST::Visibility vis,
3713 : AST::AttrVec outer_attrs)
3714 : {
3715 : /* hack - "weak keyword" by finding identifier called "union" (lookahead in
3716 : * item switch) */
3717 139 : const_TokenPtr union_keyword = expect_token (IDENTIFIER);
3718 139 : rust_assert (union_keyword->get_str () == Values::WeakKeywords::UNION);
3719 139 : location_t locus = union_keyword->get_locus ();
3720 :
3721 : // parse actual union name
3722 139 : const_TokenPtr union_name_tok = expect_token (IDENTIFIER);
3723 139 : if (union_name_tok == nullptr)
3724 : {
3725 0 : skip_after_next_block ();
3726 0 : return nullptr;
3727 : }
3728 278 : Identifier union_name{union_name_tok};
3729 :
3730 : // parse optional generic parameters
3731 139 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
3732 : = parse_generic_params_in_angles ();
3733 :
3734 : // parse optional where clause
3735 139 : AST::WhereClause where_clause = parse_where_clause ();
3736 :
3737 139 : if (!skip_token (LEFT_CURLY))
3738 : {
3739 0 : skip_after_end_block ();
3740 0 : return nullptr;
3741 : }
3742 :
3743 : /* parse union inner items as "struct fields" because hey, syntax reuse.
3744 : * Spec said so. */
3745 139 : std::vector<AST::StructField> union_fields
3746 : = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
3747 :
3748 139 : if (!skip_token (RIGHT_CURLY))
3749 : {
3750 : // skip after somewhere
3751 0 : return nullptr;
3752 : }
3753 :
3754 : return std::unique_ptr<AST::Union> (
3755 139 : new AST::Union (std::move (union_name), std::move (vis),
3756 : std::move (generic_params), std::move (where_clause),
3757 139 : std::move (union_fields), std::move (outer_attrs), locus));
3758 417 : }
3759 :
3760 : /* Parses a "constant item" (compile-time constant to maybe "inline"
3761 : * throughout the program - like constexpr). */
3762 : template <typename ManagedTokenSource>
3763 : std::unique_ptr<AST::ConstantItem>
3764 8925 : Parser<ManagedTokenSource>::parse_const_item (AST::Visibility vis,
3765 : AST::AttrVec outer_attrs)
3766 : {
3767 8925 : location_t locus = lexer.peek_token ()->get_locus ();
3768 8925 : skip_token (CONST);
3769 :
3770 : /* get constant identifier - this is either a proper identifier or the _
3771 : * wildcard */
3772 8925 : const_TokenPtr ident_tok = lexer.peek_token ();
3773 : // make default identifier the underscore wildcard one
3774 8925 : std::string ident (Values::Keywords::UNDERSCORE);
3775 8925 : switch (ident_tok->get_id ())
3776 : {
3777 8917 : case IDENTIFIER:
3778 8917 : ident = ident_tok->get_str ();
3779 8917 : lexer.skip_token ();
3780 8917 : break;
3781 8 : case UNDERSCORE:
3782 : // do nothing - identifier is already "_"
3783 8 : lexer.skip_token ();
3784 8 : break;
3785 0 : default:
3786 0 : add_error (
3787 0 : Error (ident_tok->get_locus (),
3788 : "expected item name (identifier or %<_%>) in constant item "
3789 : "declaration - found %qs",
3790 : ident_tok->get_token_description ()));
3791 :
3792 0 : skip_after_semicolon ();
3793 0 : return nullptr;
3794 : }
3795 :
3796 8925 : if (!skip_token (COLON))
3797 : {
3798 0 : skip_after_semicolon ();
3799 0 : return nullptr;
3800 : }
3801 :
3802 : // parse constant type (required)
3803 8925 : std::unique_ptr<AST::Type> type = parse_type ();
3804 :
3805 : // A const with no given expression value
3806 17850 : if (lexer.peek_token ()->get_id () == SEMICOLON)
3807 : {
3808 4 : lexer.skip_token ();
3809 : return std::unique_ptr<AST::ConstantItem> (
3810 8 : new AST::ConstantItem (std::move (ident), std::move (vis),
3811 : std::move (type), std::move (outer_attrs),
3812 4 : locus));
3813 : }
3814 :
3815 8921 : if (!skip_token (EQUAL))
3816 : {
3817 0 : skip_after_semicolon ();
3818 0 : return nullptr;
3819 : }
3820 :
3821 : // parse constant expression (required)
3822 8921 : auto expr = parse_expr ();
3823 8921 : if (!expr)
3824 1 : return nullptr;
3825 :
3826 8920 : if (!skip_token (SEMICOLON))
3827 : {
3828 : // skip somewhere?
3829 0 : return nullptr;
3830 : }
3831 :
3832 : return std::unique_ptr<AST::ConstantItem> (
3833 17840 : new AST::ConstantItem (std::move (ident), std::move (vis), std::move (type),
3834 8920 : std::move (expr.value ()), std::move (outer_attrs),
3835 8920 : locus));
3836 17850 : }
3837 :
3838 : // Parses a "static item" (static storage item, with 'static lifetime).
3839 : template <typename ManagedTokenSource>
3840 : std::unique_ptr<AST::StaticItem>
3841 521 : Parser<ManagedTokenSource>::parse_static_item (AST::Visibility vis,
3842 : AST::AttrVec outer_attrs)
3843 : {
3844 521 : location_t locus = lexer.peek_token ()->get_locus ();
3845 521 : skip_token (STATIC_KW);
3846 :
3847 : // determine whether static item is mutable
3848 521 : bool is_mut = false;
3849 1042 : if (lexer.peek_token ()->get_id () == MUT)
3850 : {
3851 19 : is_mut = true;
3852 19 : lexer.skip_token ();
3853 : }
3854 :
3855 521 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
3856 521 : if (ident_tok == nullptr)
3857 1 : return nullptr;
3858 :
3859 1040 : Identifier ident{ident_tok};
3860 :
3861 520 : if (!skip_token (COLON))
3862 : {
3863 1 : skip_after_semicolon ();
3864 1 : return nullptr;
3865 : }
3866 :
3867 : // parse static item type (required)
3868 519 : std::unique_ptr<AST::Type> type = parse_type ();
3869 :
3870 519 : if (!skip_token (EQUAL))
3871 : {
3872 0 : skip_after_semicolon ();
3873 0 : return nullptr;
3874 : }
3875 :
3876 : // parse static item expression (required)
3877 519 : auto expr = parse_expr ();
3878 519 : if (!expr)
3879 1 : return nullptr;
3880 :
3881 518 : if (!skip_token (SEMICOLON))
3882 : {
3883 : // skip after somewhere
3884 0 : return nullptr;
3885 : }
3886 :
3887 : return std::unique_ptr<AST::StaticItem> (
3888 1036 : new AST::StaticItem (std::move (ident), is_mut, std::move (type),
3889 518 : std::move (expr.value ()), std::move (vis),
3890 518 : std::move (outer_attrs), locus));
3891 1039 : }
3892 :
3893 : // Parses a trait definition item, including unsafe ones.
3894 : // also handles trait aliases
3895 : template <typename ManagedTokenSource>
3896 : std::unique_ptr<AST::VisItem>
3897 6333 : Parser<ManagedTokenSource>::parse_trait (AST::Visibility vis,
3898 : AST::AttrVec outer_attrs)
3899 : {
3900 6333 : location_t locus = lexer.peek_token ()->get_locus ();
3901 6333 : bool is_unsafe = false;
3902 6333 : bool is_auto_trait = false;
3903 6333 : bool is_alias = false;
3904 :
3905 12666 : if (lexer.peek_token ()->get_id () == UNSAFE)
3906 : {
3907 265 : is_unsafe = true;
3908 265 : lexer.skip_token ();
3909 : }
3910 :
3911 12666 : if (lexer.peek_token ()->get_id () == AUTO)
3912 : {
3913 78 : is_auto_trait = true;
3914 78 : lexer.skip_token ();
3915 : }
3916 :
3917 6333 : skip_token (TRAIT);
3918 :
3919 : // parse trait name
3920 6333 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
3921 6333 : if (ident_tok == nullptr)
3922 0 : return nullptr;
3923 :
3924 12666 : Identifier ident{ident_tok};
3925 :
3926 : // parse generic parameters (if they exist)
3927 6333 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
3928 : = parse_generic_params_in_angles ();
3929 :
3930 : // create placeholder type param bounds in case they don't exist
3931 6333 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
3932 :
3933 : // this might be a trait alias
3934 12666 : if (lexer.peek_token ()->get_id () == EQUAL)
3935 : {
3936 2 : lexer.skip_token ();
3937 :
3938 2 : if (is_auto_trait)
3939 0 : rust_error_at (locus, "trait aliases cannot be %<auto%>");
3940 2 : if (is_unsafe)
3941 0 : rust_error_at (locus, "trait aliases cannot be %<unsafe%>");
3942 :
3943 2 : type_param_bounds = parse_type_param_bounds (
3944 0 : [] (TokenId id) { return id == WHERE || id == SEMICOLON; });
3945 :
3946 2 : is_alias = true;
3947 : }
3948 : // otherwise, parse type param bounds (if they exist)
3949 12662 : else if (lexer.peek_token ()->get_id () == COLON)
3950 : {
3951 1370 : lexer.skip_token ();
3952 :
3953 1370 : type_param_bounds = parse_type_param_bounds (
3954 293 : [] (TokenId id) { return id == WHERE || id == LEFT_CURLY; });
3955 : // type_param_bounds = parse_type_param_bounds ();
3956 : }
3957 :
3958 : // parse where clause (if it exists)
3959 6333 : AST::WhereClause where_clause = parse_where_clause ();
3960 :
3961 : // if this was a trait alias, finish up
3962 6333 : if (is_alias)
3963 : {
3964 2 : if (!skip_token (SEMICOLON))
3965 0 : return nullptr;
3966 :
3967 2 : return std::make_unique<AST::TraitAlias> (std::move (ident),
3968 : std::move (generic_params),
3969 : std::move (where_clause),
3970 : std::move (type_param_bounds),
3971 : std::move (vis),
3972 2 : std::move (outer_attrs), locus);
3973 : }
3974 :
3975 6331 : if (!skip_token (LEFT_CURLY))
3976 : {
3977 0 : skip_after_end_block ();
3978 0 : return nullptr;
3979 : }
3980 :
3981 : // parse inner attrs (if they exist)
3982 6331 : AST::AttrVec inner_attrs = parse_inner_attributes ();
3983 :
3984 : // parse trait items
3985 6331 : std::vector<std::unique_ptr<AST::AssociatedItem>> trait_items;
3986 :
3987 6331 : const_TokenPtr t = lexer.peek_token ();
3988 14831 : while (t->get_id () != RIGHT_CURLY)
3989 : {
3990 8500 : std::unique_ptr<AST::AssociatedItem> trait_item = parse_trait_item ();
3991 :
3992 8500 : if (trait_item == nullptr)
3993 : {
3994 0 : Error error (lexer.peek_token ()->get_locus (),
3995 : "failed to parse trait item in trait");
3996 0 : add_error (std::move (error));
3997 :
3998 0 : return nullptr;
3999 0 : }
4000 8500 : trait_items.push_back (std::move (trait_item));
4001 :
4002 8500 : t = lexer.peek_token ();
4003 : }
4004 :
4005 6331 : if (!skip_token (RIGHT_CURLY))
4006 : {
4007 : // skip after something
4008 0 : return nullptr;
4009 : }
4010 :
4011 6331 : trait_items.shrink_to_fit ();
4012 6331 : return std::unique_ptr<AST::Trait> (
4013 12662 : new AST::Trait (std::move (ident), is_unsafe, is_auto_trait,
4014 : std::move (generic_params), std::move (type_param_bounds),
4015 : std::move (where_clause), std::move (trait_items),
4016 : std::move (vis), std::move (outer_attrs),
4017 6331 : std::move (inner_attrs), locus));
4018 12664 : }
4019 :
4020 : // Parses a trait item used inside traits (not trait, the Item).
4021 : template <typename ManagedTokenSource>
4022 : std::unique_ptr<AST::AssociatedItem>
4023 8502 : Parser<ManagedTokenSource>::parse_trait_item ()
4024 : {
4025 : // parse outer attributes (if they exist)
4026 8502 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4027 :
4028 8502 : auto vis_res = parse_visibility ();
4029 8502 : if (!vis_res)
4030 0 : return nullptr;
4031 :
4032 8502 : auto vis = vis_res.value ();
4033 :
4034 : // lookahead to determine what type of trait item to parse
4035 8502 : const_TokenPtr tok = lexer.peek_token ();
4036 8502 : switch (tok->get_id ())
4037 : {
4038 0 : case SUPER:
4039 : case SELF:
4040 : case CRATE:
4041 : case DOLLAR_SIGN:
4042 : // these seem to be SimplePath tokens, so this is a macro invocation
4043 : // semi
4044 0 : return parse_macro_invocation_semi (std::move (outer_attrs));
4045 1 : case IDENTIFIER:
4046 2 : if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
4047 0 : return parse_function (std::move (vis), std::move (outer_attrs));
4048 : else
4049 2 : return parse_macro_invocation_semi (std::move (outer_attrs));
4050 1328 : case TYPE:
4051 1328 : return parse_trait_type (std::move (outer_attrs), vis);
4052 271 : case CONST:
4053 : // disambiguate with function qualifier
4054 542 : if (lexer.peek_token (1)->get_id () == IDENTIFIER)
4055 : {
4056 540 : return parse_trait_const (std::move (outer_attrs));
4057 : }
4058 : // else, fallthrough to function
4059 : // TODO: find out how to disable gcc "implicit fallthrough" error
4060 : gcc_fallthrough ();
4061 : case ASYNC:
4062 : case UNSAFE:
4063 : case EXTERN_KW:
4064 : case FN_KW:
4065 13806 : return parse_function (std::move (vis), std::move (outer_attrs));
4066 : default:
4067 : break;
4068 : }
4069 0 : add_error (Error (tok->get_locus (),
4070 : "unrecognised token %qs for item in trait",
4071 : tok->get_token_description ()));
4072 : // skip?
4073 0 : return nullptr;
4074 17004 : }
4075 :
4076 : // Parse a typedef trait item.
4077 : template <typename ManagedTokenSource>
4078 : std::unique_ptr<AST::TraitItemType>
4079 1328 : Parser<ManagedTokenSource>::parse_trait_type (AST::AttrVec outer_attrs,
4080 : AST::Visibility vis)
4081 : {
4082 1328 : location_t locus = lexer.peek_token ()->get_locus ();
4083 1328 : skip_token (TYPE);
4084 :
4085 1328 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
4086 1328 : if (ident_tok == nullptr)
4087 0 : return nullptr;
4088 :
4089 2656 : Identifier ident{ident_tok};
4090 :
4091 : // Parse optional generic parameters for GATs (Generic Associated Types)
4092 1328 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params;
4093 2656 : if (lexer.peek_token ()->get_id () == LEFT_ANGLE)
4094 : {
4095 17 : generic_params = parse_generic_params_in_angles ();
4096 : }
4097 :
4098 1328 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
4099 :
4100 : // parse optional colon
4101 2656 : if (lexer.peek_token ()->get_id () == COLON)
4102 : {
4103 179 : lexer.skip_token ();
4104 :
4105 : // parse optional type param bounds
4106 : bounds
4107 179 : = parse_type_param_bounds ([] (TokenId id) { return id == SEMICOLON; });
4108 : // bounds = parse_type_param_bounds ();
4109 : }
4110 :
4111 1328 : if (!skip_token (SEMICOLON))
4112 : {
4113 : // skip?
4114 0 : return nullptr;
4115 : }
4116 :
4117 : return std::unique_ptr<AST::TraitItemType> (
4118 1328 : new AST::TraitItemType (std::move (ident), std::move (generic_params),
4119 : std::move (bounds), std::move (outer_attrs), vis,
4120 1328 : locus));
4121 1328 : }
4122 :
4123 : // Parses a constant trait item.
4124 : template <typename ManagedTokenSource>
4125 : std::unique_ptr<AST::ConstantItem>
4126 270 : Parser<ManagedTokenSource>::parse_trait_const (AST::AttrVec outer_attrs)
4127 : {
4128 270 : location_t locus = lexer.peek_token ()->get_locus ();
4129 270 : skip_token (CONST);
4130 :
4131 : // parse constant item name
4132 270 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
4133 270 : if (ident_tok == nullptr)
4134 0 : return nullptr;
4135 :
4136 540 : Identifier ident{ident_tok};
4137 :
4138 270 : if (!skip_token (COLON))
4139 : {
4140 0 : skip_after_semicolon ();
4141 0 : return nullptr;
4142 : }
4143 :
4144 : // parse constant trait item type
4145 270 : std::unique_ptr<AST::Type> type = parse_type ();
4146 :
4147 : // parse constant trait body expression, if it exists
4148 270 : std::unique_ptr<AST::Expr> const_body = nullptr;
4149 540 : if (lexer.peek_token ()->get_id () == EQUAL)
4150 : {
4151 12 : lexer.skip_token ();
4152 :
4153 : // expression must exist, so parse it
4154 12 : auto expr = parse_expr ();
4155 12 : if (!expr)
4156 0 : return nullptr;
4157 12 : const_body = std::move (expr.value ());
4158 12 : }
4159 :
4160 270 : if (!skip_token (SEMICOLON))
4161 : {
4162 : // skip after something?
4163 0 : return nullptr;
4164 : }
4165 :
4166 810 : return std::unique_ptr<AST::ConstantItem> (new AST::ConstantItem (
4167 540 : std::move (ident), AST::Visibility::create_private (), std::move (type),
4168 270 : std::move (const_body), std::move (outer_attrs), locus));
4169 540 : }
4170 :
4171 : /* Parses a struct "impl" item (both inherent impl and trait impl can be
4172 : * parsed here), */
4173 : template <typename ManagedTokenSource>
4174 : std::unique_ptr<AST::Impl>
4175 82224 : Parser<ManagedTokenSource>::parse_impl (AST::Visibility vis,
4176 : AST::AttrVec outer_attrs)
4177 : {
4178 : /* Note that only trait impls are allowed to be unsafe. So if unsafe, it
4179 : * must be a trait impl. However, this isn't enough for full disambiguation,
4180 : * so don't branch here. */
4181 82224 : location_t locus = lexer.peek_token ()->get_locus ();
4182 82224 : bool is_unsafe = false;
4183 164448 : if (lexer.peek_token ()->get_id () == UNSAFE)
4184 : {
4185 1969 : lexer.skip_token ();
4186 1969 : is_unsafe = true;
4187 : }
4188 :
4189 82224 : if (!skip_token (IMPL))
4190 : {
4191 0 : skip_after_next_block ();
4192 0 : return nullptr;
4193 : }
4194 :
4195 : // parse generic params (shared by trait and inherent impls)
4196 82224 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4197 : = parse_generic_params_in_angles ();
4198 :
4199 : // Again, trait impl-only feature, but optional one, so can be used for
4200 : // branching yet.
4201 82224 : bool has_exclam = false;
4202 164448 : if (lexer.peek_token ()->get_id () == EXCLAM)
4203 : {
4204 181 : lexer.skip_token ();
4205 181 : has_exclam = true;
4206 : }
4207 :
4208 : /* FIXME: code that doesn't look shit for TypePath. Also, make sure this
4209 : * doesn't parse too much and not work. */
4210 82224 : AST::TypePath type_path = parse_type_path ();
4211 164099 : if (type_path.is_error () || lexer.peek_token ()->get_id () != FOR)
4212 : {
4213 : /* cannot parse type path (or not for token next, at least), so must be
4214 : * inherent impl */
4215 :
4216 : // hacky conversion of TypePath stack object to Type pointer
4217 4871 : std::unique_ptr<AST::Type> type = nullptr;
4218 4871 : if (!type_path.is_error ())
4219 4522 : type = std::unique_ptr<AST::TypePath> (
4220 4522 : new AST::TypePath (std::move (type_path)));
4221 : else
4222 349 : type = parse_type ();
4223 :
4224 : // Type is required, so error if null
4225 4871 : if (type == nullptr)
4226 : {
4227 1 : Error error (lexer.peek_token ()->get_locus (),
4228 : "could not parse type in inherent impl");
4229 1 : add_error (std::move (error));
4230 :
4231 1 : skip_after_next_block ();
4232 1 : return nullptr;
4233 1 : }
4234 :
4235 : // parse optional where clause
4236 4870 : AST::WhereClause where_clause = parse_where_clause ();
4237 :
4238 4870 : if (!skip_token (LEFT_CURLY))
4239 : {
4240 : // TODO: does this still skip properly?
4241 0 : skip_after_end_block ();
4242 0 : return nullptr;
4243 : }
4244 :
4245 : // parse inner attributes (optional)
4246 4870 : AST::AttrVec inner_attrs = parse_inner_attributes ();
4247 :
4248 : // parse inherent impl items
4249 4870 : std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
4250 :
4251 4870 : const_TokenPtr t = lexer.peek_token ();
4252 24407 : while (t->get_id () != RIGHT_CURLY)
4253 : {
4254 19547 : std::unique_ptr<AST::AssociatedItem> impl_item
4255 : = parse_inherent_impl_item ();
4256 :
4257 19547 : if (impl_item == nullptr)
4258 : {
4259 10 : Error error (
4260 10 : lexer.peek_token ()->get_locus (),
4261 : "failed to parse inherent impl item in inherent impl");
4262 10 : add_error (std::move (error));
4263 :
4264 10 : return nullptr;
4265 10 : }
4266 :
4267 19537 : impl_items.push_back (std::move (impl_item));
4268 :
4269 19537 : t = lexer.peek_token ();
4270 : }
4271 :
4272 4860 : if (!skip_token (RIGHT_CURLY))
4273 : {
4274 : // skip somewhere
4275 0 : return nullptr;
4276 : }
4277 :
4278 : // DEBUG
4279 4860 : rust_debug ("successfully parsed inherent impl");
4280 :
4281 4860 : impl_items.shrink_to_fit ();
4282 :
4283 4860 : return std::unique_ptr<AST::InherentImpl> (new AST::InherentImpl (
4284 : std::move (impl_items), std::move (generic_params), std::move (type),
4285 : std::move (where_clause), std::move (vis), std::move (inner_attrs),
4286 4860 : std::move (outer_attrs), locus));
4287 4871 : }
4288 : else
4289 : {
4290 : // type path must both be valid and next token is for, so trait impl
4291 77353 : if (!skip_token (FOR))
4292 : {
4293 0 : skip_after_next_block ();
4294 0 : return nullptr;
4295 : }
4296 :
4297 : // parse type
4298 77353 : std::unique_ptr<AST::Type> type = parse_type ();
4299 : // ensure type is included as it is required
4300 77353 : if (type == nullptr)
4301 : {
4302 0 : Error error (lexer.peek_token ()->get_locus (),
4303 : "could not parse type in trait impl");
4304 0 : add_error (std::move (error));
4305 :
4306 0 : skip_after_next_block ();
4307 0 : return nullptr;
4308 0 : }
4309 :
4310 : // parse optional where clause
4311 77353 : AST::WhereClause where_clause = parse_where_clause ();
4312 :
4313 77353 : if (!skip_token (LEFT_CURLY))
4314 : {
4315 : // TODO: does this still skip properly?
4316 0 : skip_after_end_block ();
4317 0 : return nullptr;
4318 : }
4319 :
4320 : // parse inner attributes (optional)
4321 77353 : AST::AttrVec inner_attrs = parse_inner_attributes ();
4322 :
4323 : // parse trait impl items
4324 77353 : std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
4325 :
4326 77353 : const_TokenPtr t = lexer.peek_token ();
4327 192612 : while (t->get_id () != RIGHT_CURLY)
4328 : {
4329 115262 : std::unique_ptr<AST::AssociatedItem> impl_item
4330 : = parse_trait_impl_item ();
4331 :
4332 115262 : if (impl_item == nullptr)
4333 : {
4334 3 : Error error (lexer.peek_token ()->get_locus (),
4335 : "failed to parse trait impl item in trait impl");
4336 3 : add_error (std::move (error));
4337 :
4338 3 : return nullptr;
4339 3 : }
4340 :
4341 115259 : impl_items.push_back (std::move (impl_item));
4342 :
4343 115259 : t = lexer.peek_token ();
4344 :
4345 : // DEBUG
4346 115259 : rust_debug ("successfully parsed a trait impl item");
4347 : }
4348 : // DEBUG
4349 77350 : rust_debug ("successfully finished trait impl items");
4350 :
4351 77350 : if (!skip_token (RIGHT_CURLY))
4352 : {
4353 : // skip somewhere
4354 0 : return nullptr;
4355 : }
4356 :
4357 : // DEBUG
4358 77350 : rust_debug ("successfully parsed trait impl");
4359 :
4360 77350 : impl_items.shrink_to_fit ();
4361 :
4362 77350 : return std::unique_ptr<AST::TraitImpl> (
4363 154700 : new AST::TraitImpl (std::move (type_path), is_unsafe, has_exclam,
4364 : std::move (impl_items), std::move (generic_params),
4365 : std::move (type), std::move (where_clause),
4366 : std::move (vis), std::move (inner_attrs),
4367 77350 : std::move (outer_attrs), locus));
4368 77353 : }
4369 82224 : }
4370 :
4371 : // Parses a single inherent impl item (item inside an inherent impl block).
4372 : template <typename ManagedTokenSource>
4373 : std::unique_ptr<AST::AssociatedItem>
4374 43597 : Parser<ManagedTokenSource>::parse_inherent_impl_item ()
4375 : {
4376 : // parse outer attributes (if they exist)
4377 43597 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4378 :
4379 : // TODO: cleanup - currently an unreadable mess
4380 :
4381 : // branch on next token:
4382 43597 : const_TokenPtr t = lexer.peek_token ();
4383 43597 : switch (t->get_id ())
4384 : {
4385 13081 : case IDENTIFIER:
4386 : // FIXME: Arthur: Do we need to some lookahead here?
4387 26162 : return parse_macro_invocation_semi (outer_attrs);
4388 28803 : case SUPER:
4389 : case SELF:
4390 : case CRATE:
4391 : case PUB:
4392 : {
4393 : // visibility, so not a macro invocation semi - must be constant,
4394 : // function, or method
4395 28803 : auto vis_res = parse_visibility ();
4396 28803 : if (!vis_res)
4397 0 : return nullptr;
4398 28803 : auto vis = vis_res.value ();
4399 :
4400 : // TODO: is a recursive call to parse_inherent_impl_item better?
4401 57606 : switch (lexer.peek_token ()->get_id ())
4402 : {
4403 11095 : case EXTERN_KW:
4404 : case UNSAFE:
4405 : case FN_KW:
4406 : // function or method
4407 22190 : return parse_inherent_impl_function_or_method (std::move (vis),
4408 : std::move (
4409 11095 : outer_attrs));
4410 17708 : case CONST:
4411 : // lookahead to resolve production - could be function/method or
4412 : // const item
4413 17708 : t = lexer.peek_token (1);
4414 :
4415 17708 : switch (t->get_id ())
4416 : {
4417 1192 : case IDENTIFIER:
4418 : case UNDERSCORE:
4419 2384 : return parse_const_item (std::move (vis),
4420 1192 : std::move (outer_attrs));
4421 16516 : case UNSAFE:
4422 : case EXTERN_KW:
4423 : case FN_KW:
4424 33032 : return parse_inherent_impl_function_or_method (std::move (vis),
4425 : std::move (
4426 16516 : outer_attrs));
4427 0 : default:
4428 0 : add_error (Error (t->get_locus (),
4429 : "unexpected token %qs in some sort of const "
4430 : "item in inherent impl",
4431 : t->get_token_description ()));
4432 :
4433 0 : lexer.skip_token (1); // TODO: is this right thing to do?
4434 0 : return nullptr;
4435 : }
4436 0 : default:
4437 0 : add_error (
4438 0 : Error (t->get_locus (),
4439 : "unrecognised token %qs for item in inherent impl",
4440 : t->get_token_description ()));
4441 : // skip?
4442 0 : return nullptr;
4443 : }
4444 57606 : }
4445 1523 : case ASYNC:
4446 : case EXTERN_KW:
4447 : case UNSAFE:
4448 : case FN_KW:
4449 : // function or method
4450 1523 : return parse_inherent_impl_function_or_method (
4451 1523 : AST::Visibility::create_private (), std::move (outer_attrs));
4452 190 : case CONST:
4453 : /* lookahead to resolve production - could be function/method or const
4454 : * item */
4455 190 : t = lexer.peek_token (1);
4456 :
4457 190 : switch (t->get_id ())
4458 : {
4459 45 : case IDENTIFIER:
4460 : case UNDERSCORE:
4461 90 : return parse_const_item (AST::Visibility::create_private (),
4462 45 : std::move (outer_attrs));
4463 145 : case UNSAFE:
4464 : case EXTERN_KW:
4465 : case FN_KW:
4466 145 : return parse_inherent_impl_function_or_method (
4467 145 : AST::Visibility::create_private (), std::move (outer_attrs));
4468 0 : default:
4469 0 : add_error (Error (t->get_locus (),
4470 : "unexpected token %qs in some sort of const item "
4471 : "in inherent impl",
4472 : t->get_token_description ()));
4473 :
4474 0 : lexer.skip_token (1); // TODO: is this right thing to do?
4475 0 : return nullptr;
4476 : }
4477 : rust_unreachable ();
4478 0 : default:
4479 0 : add_error (Error (t->get_locus (),
4480 : "unrecognised token %qs for item in inherent impl",
4481 : t->get_token_description ()));
4482 :
4483 : // skip?
4484 0 : return nullptr;
4485 : }
4486 43597 : }
4487 :
4488 : /* For internal use only by parse_inherent_impl_item() - splits giant method
4489 : * into smaller ones and prevents duplication of logic. Strictly, this parses
4490 : * a function or method item inside an inherent impl item block. */
4491 : // TODO: make this a templated function with "return type" as type param -
4492 : // InherentImplItem is this specialisation of the template while TraitImplItem
4493 : // will be the other.
4494 : template <typename ManagedTokenSource>
4495 : std::unique_ptr<AST::AssociatedItem>
4496 29279 : Parser<ManagedTokenSource>::parse_inherent_impl_function_or_method (
4497 : AST::Visibility vis, AST::AttrVec outer_attrs)
4498 : {
4499 29279 : location_t locus = lexer.peek_token ()->get_locus ();
4500 : // parse function or method qualifiers
4501 29279 : auto qualifiers = parse_function_qualifiers ();
4502 29279 : if (!qualifiers)
4503 0 : return nullptr;
4504 :
4505 29279 : skip_token (FN_KW);
4506 :
4507 : // parse function or method name
4508 29279 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
4509 29279 : if (ident_tok == nullptr)
4510 7 : return nullptr;
4511 :
4512 58544 : Identifier ident{ident_tok};
4513 :
4514 : // parse generic params
4515 29272 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4516 : = parse_generic_params_in_angles ();
4517 :
4518 29272 : if (!skip_token (LEFT_PAREN))
4519 : {
4520 : // skip after somewhere?
4521 0 : return nullptr;
4522 : }
4523 :
4524 : // now for function vs method disambiguation - method has opening "self"
4525 : // param
4526 29272 : auto initial_param = parse_self_param ();
4527 :
4528 29272 : if (!initial_param.has_value ()
4529 29272 : && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
4530 3 : return nullptr;
4531 :
4532 : /* FIXME: ensure that self param doesn't accidently consume tokens for a
4533 : * function one idea is to lookahead up to 4 tokens to see whether self is
4534 : * one of them */
4535 29269 : bool is_method = false;
4536 29269 : if (initial_param.has_value ())
4537 : {
4538 23389 : if ((*initial_param)->is_self ())
4539 : is_method = true;
4540 :
4541 : /* skip comma so function and method regular params can be parsed in
4542 : * same way */
4543 46778 : if (lexer.peek_token ()->get_id () == COMMA)
4544 12361 : lexer.skip_token ();
4545 : }
4546 :
4547 : // parse trait function params
4548 29269 : std::vector<std::unique_ptr<AST::Param>> function_params
4549 : = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
4550 :
4551 29269 : if (initial_param.has_value ())
4552 23389 : function_params.insert (function_params.begin (),
4553 23389 : std::move (*initial_param));
4554 :
4555 29269 : if (!skip_token (RIGHT_PAREN))
4556 : {
4557 0 : skip_after_end_block ();
4558 0 : return nullptr;
4559 : }
4560 :
4561 : // parse return type (optional)
4562 29269 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
4563 :
4564 : // parse where clause (optional)
4565 29269 : AST::WhereClause where_clause = parse_where_clause ();
4566 :
4567 29269 : tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
4568 58538 : if (lexer.peek_token ()->get_id () == SEMICOLON)
4569 2 : lexer.skip_token ();
4570 : else
4571 : {
4572 29267 : auto result = parse_block_expr ();
4573 :
4574 29267 : if (!result)
4575 : {
4576 0 : Error error (
4577 0 : lexer.peek_token ()->get_locus (),
4578 : "could not parse definition in inherent impl %s definition",
4579 : is_method ? "method" : "function");
4580 0 : add_error (std::move (error));
4581 :
4582 0 : skip_after_end_block ();
4583 0 : return nullptr;
4584 0 : }
4585 29267 : body = std::move (result.value ());
4586 29267 : }
4587 :
4588 29269 : return std::unique_ptr<AST::Function> (
4589 117074 : new AST::Function (std::move (ident), std::move (qualifiers.value ()),
4590 : std::move (generic_params), std::move (function_params),
4591 : std::move (return_type), std::move (where_clause),
4592 : std::move (body), std::move (vis),
4593 29269 : std::move (outer_attrs), locus));
4594 87820 : }
4595 :
4596 : // Parses a single trait impl item (item inside a trait impl block).
4597 : template <typename ManagedTokenSource>
4598 : std::unique_ptr<AST::AssociatedItem>
4599 116802 : Parser<ManagedTokenSource>::parse_trait_impl_item ()
4600 : {
4601 : // parse outer attributes (if they exist)
4602 116802 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4603 :
4604 116802 : auto vis_res = parse_visibility ();
4605 116802 : if (!vis_res)
4606 0 : return nullptr;
4607 116802 : auto visibility = vis_res.value ();
4608 :
4609 : // branch on next token:
4610 116802 : const_TokenPtr t = lexer.peek_token ();
4611 116802 : switch (t->get_id ())
4612 : {
4613 0 : case SUPER:
4614 : case SELF:
4615 : case CRATE:
4616 : case DOLLAR_SIGN:
4617 : // these seem to be SimplePath tokens, so this is a macro invocation
4618 : // semi
4619 0 : return parse_macro_invocation_semi (std::move (outer_attrs));
4620 818 : case IDENTIFIER:
4621 1636 : if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
4622 764 : return parse_trait_impl_function_or_method (visibility,
4623 382 : std::move (outer_attrs));
4624 : else
4625 872 : return parse_macro_invocation_semi (std::move (outer_attrs));
4626 33338 : case TYPE:
4627 66676 : return parse_type_alias (visibility, std::move (outer_attrs));
4628 82107 : case EXTERN_KW:
4629 : case UNSAFE:
4630 : case FN_KW:
4631 : // function or method
4632 164214 : return parse_trait_impl_function_or_method (visibility,
4633 82107 : std::move (outer_attrs));
4634 1 : case ASYNC:
4635 2 : return parse_async_item (visibility, std::move (outer_attrs));
4636 538 : case CONST:
4637 : // lookahead to resolve production - could be function/method or const
4638 : // item
4639 538 : t = lexer.peek_token (1);
4640 :
4641 538 : switch (t->get_id ())
4642 : {
4643 537 : case IDENTIFIER:
4644 : case UNDERSCORE:
4645 1074 : return parse_const_item (visibility, std::move (outer_attrs));
4646 1 : case UNSAFE:
4647 : case EXTERN_KW:
4648 : case FN_KW:
4649 2 : return parse_trait_impl_function_or_method (visibility,
4650 1 : std::move (outer_attrs));
4651 0 : default:
4652 0 : add_error (Error (
4653 : t->get_locus (),
4654 : "unexpected token %qs in some sort of const item in trait impl",
4655 : t->get_token_description ()));
4656 :
4657 0 : lexer.skip_token (1); // TODO: is this right thing to do?
4658 0 : return nullptr;
4659 : }
4660 : rust_unreachable ();
4661 : default:
4662 : break;
4663 : }
4664 0 : add_error (Error (t->get_locus (),
4665 : "unrecognised token %qs for item in trait impl",
4666 : t->get_token_description ()));
4667 :
4668 : // skip?
4669 0 : return nullptr;
4670 233604 : }
4671 :
4672 : /* For internal use only by parse_trait_impl_item() - splits giant method into
4673 : * smaller ones and prevents duplication of logic. Strictly, this parses a
4674 : * function or method item inside a trait impl item block. */
4675 : template <typename ManagedTokenSource>
4676 : std::unique_ptr<AST::AssociatedItem>
4677 82490 : Parser<ManagedTokenSource>::parse_trait_impl_function_or_method (
4678 : AST::Visibility vis, AST::AttrVec outer_attrs)
4679 : {
4680 : // this shares virtually all logic with
4681 : // parse_inherent_impl_function_or_method
4682 : // - template?
4683 82490 : location_t locus = lexer.peek_token ()->get_locus ();
4684 :
4685 : // parse function or method qualifiers
4686 82490 : auto qualifiers = parse_function_qualifiers ();
4687 82490 : if (!qualifiers)
4688 2 : return nullptr;
4689 :
4690 82488 : skip_token (FN_KW);
4691 :
4692 : // parse function or method name
4693 82488 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
4694 82488 : if (ident_tok == nullptr)
4695 : {
4696 0 : return nullptr;
4697 : }
4698 164976 : Identifier ident{ident_tok};
4699 :
4700 : // DEBUG:
4701 82488 : rust_debug (
4702 : "about to start parsing generic params in trait impl function or method");
4703 :
4704 : // parse generic params
4705 82488 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4706 : = parse_generic_params_in_angles ();
4707 :
4708 : // DEBUG:
4709 82488 : rust_debug (
4710 : "finished parsing generic params in trait impl function or method");
4711 :
4712 82488 : if (!skip_token (LEFT_PAREN))
4713 : {
4714 : // skip after somewhere?
4715 0 : return nullptr;
4716 : }
4717 :
4718 : // now for function vs method disambiguation - method has opening "self"
4719 : // param
4720 82488 : auto initial_param = parse_self_param ();
4721 :
4722 82488 : if (!initial_param.has_value ()
4723 82488 : && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
4724 0 : return nullptr;
4725 :
4726 : // FIXME: ensure that self param doesn't accidently consume tokens for a
4727 : // function
4728 82488 : bool is_method = false;
4729 82488 : if (initial_param.has_value ())
4730 : {
4731 72782 : if ((*initial_param)->is_self ())
4732 : is_method = true;
4733 :
4734 : // skip comma so function and method regular params can be parsed in
4735 : // same way
4736 145564 : if (lexer.peek_token ()->get_id () == COMMA)
4737 : {
4738 60448 : lexer.skip_token ();
4739 : }
4740 :
4741 : // DEBUG
4742 72782 : rust_debug ("successfully parsed self param in method trait impl item");
4743 : }
4744 :
4745 : // DEBUG
4746 82488 : rust_debug (
4747 : "started to parse function params in function or method trait impl item");
4748 :
4749 : // parse trait function params (only if next token isn't right paren)
4750 82488 : std::vector<std::unique_ptr<AST::Param>> function_params;
4751 164976 : if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
4752 : {
4753 : function_params
4754 68383 : = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
4755 :
4756 68383 : if (function_params.empty ())
4757 : {
4758 0 : Error error (
4759 0 : lexer.peek_token ()->get_locus (),
4760 : "failed to parse function params in trait impl %s definition",
4761 : is_method ? "method" : "function");
4762 0 : add_error (std::move (error));
4763 :
4764 0 : skip_after_next_block ();
4765 0 : return nullptr;
4766 0 : }
4767 : }
4768 :
4769 82488 : if (initial_param.has_value ())
4770 72782 : function_params.insert (function_params.begin (),
4771 72782 : std::move (*initial_param));
4772 :
4773 : // DEBUG
4774 82488 : rust_debug ("successfully parsed function params in function or method "
4775 : "trait impl item");
4776 :
4777 82488 : if (!skip_token (RIGHT_PAREN))
4778 : {
4779 0 : skip_after_next_block ();
4780 0 : return nullptr;
4781 : }
4782 :
4783 : // parse return type (optional)
4784 82488 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
4785 :
4786 : // DEBUG
4787 82488 : rust_debug (
4788 : "successfully parsed return type in function or method trait impl item");
4789 :
4790 : // parse where clause (optional)
4791 82488 : AST::WhereClause where_clause = parse_where_clause ();
4792 :
4793 : // DEBUG
4794 82488 : rust_debug (
4795 : "successfully parsed where clause in function or method trait impl item");
4796 :
4797 : // parse function definition (in block) - semicolon not allowed
4798 82488 : tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
4799 :
4800 164976 : if (lexer.peek_token ()->get_id () == SEMICOLON)
4801 1 : lexer.skip_token ();
4802 : else
4803 : {
4804 82487 : auto result = parse_block_expr ();
4805 82487 : if (!result)
4806 : {
4807 1 : Error error (lexer.peek_token ()->get_locus (),
4808 : "could not parse definition in trait impl %s definition",
4809 : is_method ? "method" : "function");
4810 1 : add_error (std::move (error));
4811 :
4812 1 : skip_after_end_block ();
4813 1 : return nullptr;
4814 1 : }
4815 82486 : body = std::move (result.value ());
4816 82487 : }
4817 :
4818 82487 : return std::unique_ptr<AST::Function> (
4819 329947 : new AST::Function (std::move (ident), std::move (qualifiers.value ()),
4820 : std::move (generic_params), std::move (function_params),
4821 : std::move (return_type), std::move (where_clause),
4822 : std::move (body), std::move (vis),
4823 82487 : std::move (outer_attrs), locus));
4824 247464 : }
4825 :
4826 : // Parses an extern block of declarations.
4827 : template <typename ManagedTokenSource>
4828 : std::unique_ptr<AST::ExternBlock>
4829 2395 : Parser<ManagedTokenSource>::parse_extern_block (AST::Visibility vis,
4830 : AST::AttrVec outer_attrs)
4831 : {
4832 2395 : location_t locus = lexer.peek_token ()->get_locus ();
4833 2395 : skip_token (EXTERN_KW);
4834 :
4835 : // detect optional abi name
4836 2395 : std::string abi;
4837 2395 : const_TokenPtr next_tok = lexer.peek_token ();
4838 2395 : if (next_tok->get_id () == STRING_LITERAL)
4839 : {
4840 2394 : lexer.skip_token ();
4841 2394 : abi = next_tok->get_str ();
4842 : }
4843 :
4844 2395 : if (!skip_token (LEFT_CURLY))
4845 : {
4846 0 : skip_after_end_block ();
4847 0 : return nullptr;
4848 : }
4849 :
4850 2395 : AST::AttrVec inner_attrs = parse_inner_attributes ();
4851 :
4852 : // parse declarations inside extern block
4853 2395 : std::vector<std::unique_ptr<AST::ExternalItem>> extern_items;
4854 :
4855 2395 : const_TokenPtr t = lexer.peek_token ();
4856 15533 : while (t->get_id () != RIGHT_CURLY)
4857 : {
4858 13139 : std::unique_ptr<AST::ExternalItem> extern_item = parse_external_item ();
4859 :
4860 13139 : if (extern_item == nullptr)
4861 : {
4862 1 : Error error (t->get_locus (),
4863 : "failed to parse external item despite not reaching "
4864 : "end of extern block");
4865 1 : add_error (std::move (error));
4866 :
4867 1 : return nullptr;
4868 1 : }
4869 :
4870 13138 : extern_items.push_back (std::move (extern_item));
4871 :
4872 13138 : t = lexer.peek_token ();
4873 : }
4874 :
4875 2394 : if (!skip_token (RIGHT_CURLY))
4876 : {
4877 : // skip somewhere
4878 0 : return nullptr;
4879 : }
4880 :
4881 2394 : extern_items.shrink_to_fit ();
4882 :
4883 : return std::unique_ptr<AST::ExternBlock> (
4884 2394 : new AST::ExternBlock (std::move (abi), std::move (extern_items),
4885 : std::move (vis), std::move (inner_attrs),
4886 2394 : std::move (outer_attrs), locus));
4887 4790 : }
4888 :
4889 : // Parses a single extern block item (static or function declaration).
4890 : template <typename ManagedTokenSource>
4891 : std::unique_ptr<AST::ExternalItem>
4892 13142 : Parser<ManagedTokenSource>::parse_external_item ()
4893 : {
4894 : // parse optional outer attributes
4895 13142 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4896 :
4897 13142 : location_t locus = lexer.peek_token ()->get_locus ();
4898 :
4899 : // parse optional visibility
4900 13142 : auto vis_res = parse_visibility ();
4901 13142 : if (!vis_res)
4902 0 : return nullptr;
4903 13142 : auto vis = vis_res.value ();
4904 :
4905 13142 : const_TokenPtr t = lexer.peek_token ();
4906 13142 : switch (t->get_id ())
4907 : {
4908 2 : case IDENTIFIER:
4909 4 : return parse_macro_invocation_semi (outer_attrs);
4910 1 : case STATIC_KW:
4911 : {
4912 : // parse extern static item
4913 1 : lexer.skip_token ();
4914 :
4915 : // parse mut (optional)
4916 1 : bool has_mut = false;
4917 2 : if (lexer.peek_token ()->get_id () == MUT)
4918 : {
4919 0 : lexer.skip_token ();
4920 0 : has_mut = true;
4921 : }
4922 :
4923 : // parse identifier
4924 1 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
4925 1 : if (ident_tok == nullptr)
4926 : {
4927 0 : skip_after_semicolon ();
4928 0 : return nullptr;
4929 : }
4930 2 : Identifier ident{ident_tok};
4931 :
4932 1 : if (!skip_token (COLON))
4933 : {
4934 0 : skip_after_semicolon ();
4935 0 : return nullptr;
4936 : }
4937 :
4938 : // parse type (required)
4939 1 : std::unique_ptr<AST::Type> type = parse_type ();
4940 1 : if (type == nullptr)
4941 : {
4942 0 : Error error (lexer.peek_token ()->get_locus (),
4943 : "failed to parse type in external static item");
4944 0 : add_error (std::move (error));
4945 :
4946 0 : skip_after_semicolon ();
4947 0 : return nullptr;
4948 0 : }
4949 :
4950 1 : if (!skip_token (SEMICOLON))
4951 : {
4952 : // skip after somewhere?
4953 0 : return nullptr;
4954 : }
4955 :
4956 1 : return std::unique_ptr<AST::ExternalStaticItem> (
4957 2 : new AST::ExternalStaticItem (std::move (ident), std::move (type),
4958 : has_mut, std::move (vis),
4959 1 : std::move (outer_attrs), locus));
4960 3 : }
4961 13120 : case FN_KW:
4962 26240 : return parse_function (std::move (vis), std::move (outer_attrs), true);
4963 :
4964 19 : case TYPE:
4965 19 : return parse_external_type_item (std::move (vis),
4966 19 : std::move (outer_attrs));
4967 0 : default:
4968 : // error
4969 0 : add_error (
4970 0 : Error (t->get_locus (),
4971 : "unrecognised token %qs in extern block item declaration",
4972 : t->get_token_description ()));
4973 :
4974 0 : skip_after_semicolon ();
4975 0 : return nullptr;
4976 : }
4977 26284 : }
4978 :
4979 : // Parses a statement (will further disambiguate any statement).
4980 : template <typename ManagedTokenSource>
4981 : std::unique_ptr<AST::Stmt>
4982 2836 : Parser<ManagedTokenSource>::parse_stmt (ParseRestrictions restrictions)
4983 : {
4984 : // quick exit for empty statement
4985 : // FIXME: Can we have empty statements without semicolons? Just nothing?
4986 2836 : const_TokenPtr t = lexer.peek_token ();
4987 2836 : if (t->get_id () == SEMICOLON)
4988 : {
4989 30 : lexer.skip_token ();
4990 30 : return std::unique_ptr<AST::EmptyStmt> (
4991 30 : new AST::EmptyStmt (t->get_locus ()));
4992 : }
4993 :
4994 : // parse outer attributes
4995 2806 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4996 :
4997 : // parsing this will be annoying because of the many different possibilities
4998 : /* best may be just to copy paste in parse_item switch, and failing that try
4999 : * to parse outer attributes, and then pass them in to either a let
5000 : * statement or (fallback) expression statement. */
5001 : // FIXME: think of a way to do this without such a large switch?
5002 2806 : t = lexer.peek_token ();
5003 2806 : switch (t->get_id ())
5004 : {
5005 227 : case LET:
5006 : // let statement
5007 227 : return parse_let_stmt (std::move (outer_attrs), restrictions);
5008 186 : case PUB:
5009 : case MOD:
5010 : case EXTERN_KW:
5011 : case USE:
5012 : case FN_KW:
5013 : case TYPE:
5014 : case STRUCT_KW:
5015 : case ENUM_KW:
5016 : case CONST:
5017 : case STATIC_KW:
5018 : case AUTO:
5019 : case TRAIT:
5020 : case IMPL:
5021 : case MACRO:
5022 : /* TODO: implement union keyword but not really because of
5023 : * context-dependence crappy hack way to parse a union written below to
5024 : * separate it from the good code. */
5025 : // case UNION:
5026 : case UNSAFE: // maybe - unsafe traits are a thing
5027 : /* if any of these (should be all possible VisItem prefixes), parse a
5028 : * VisItem can't parse item because would require reparsing outer
5029 : * attributes */
5030 : // may also be unsafe block
5031 372 : if (lexer.peek_token (1)->get_id () == LEFT_CURLY)
5032 : {
5033 1 : return parse_expr_stmt (std::move (outer_attrs), restrictions);
5034 : }
5035 : else
5036 : {
5037 185 : return parse_vis_item (std::move (outer_attrs));
5038 : }
5039 : break;
5040 : // crappy hack to do union "keyword"
5041 487 : case IDENTIFIER:
5042 487 : if (t->get_str () == Values::WeakKeywords::UNION
5043 487 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
5044 : {
5045 0 : return parse_vis_item (std::move (outer_attrs));
5046 : // or should this go straight to parsing union?
5047 : }
5048 974 : else if (is_macro_rules_def (t))
5049 : {
5050 : // macro_rules! macro item
5051 2 : return parse_macro_rules_def (std::move (outer_attrs));
5052 : }
5053 : gcc_fallthrough ();
5054 : // TODO: find out how to disable gcc "implicit fallthrough" warning
5055 : default:
5056 : // fallback: expression statement
5057 2391 : return parse_expr_stmt (std::move (outer_attrs), restrictions);
5058 : break;
5059 : }
5060 2806 : }
5061 :
5062 : // Parses a let statement.
5063 : template <typename ManagedTokenSource>
5064 : std::unique_ptr<AST::LetStmt>
5065 133937 : Parser<ManagedTokenSource>::parse_let_stmt (AST::AttrVec outer_attrs,
5066 : ParseRestrictions restrictions)
5067 : {
5068 133937 : location_t locus = lexer.peek_token ()->get_locus ();
5069 133937 : skip_token (LET);
5070 :
5071 : // parse pattern (required)
5072 133937 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
5073 133937 : if (pattern == nullptr)
5074 : {
5075 0 : Error error (lexer.peek_token ()->get_locus (),
5076 : "failed to parse pattern in let statement");
5077 0 : add_error (std::move (error));
5078 :
5079 0 : skip_after_semicolon ();
5080 0 : return nullptr;
5081 0 : }
5082 :
5083 : // parse type declaration (optional)
5084 133937 : std::unique_ptr<AST::Type> type = nullptr;
5085 267874 : if (lexer.peek_token ()->get_id () == COLON)
5086 : {
5087 : // must have a type declaration
5088 7953 : lexer.skip_token ();
5089 :
5090 7953 : type = parse_type ();
5091 7953 : if (type == nullptr)
5092 : {
5093 0 : Error error (lexer.peek_token ()->get_locus (),
5094 : "failed to parse type in let statement");
5095 0 : add_error (std::move (error));
5096 :
5097 0 : skip_after_semicolon ();
5098 0 : return nullptr;
5099 0 : }
5100 : }
5101 :
5102 : // parse expression to set variable to (optional)
5103 133937 : std::unique_ptr<AST::Expr> expr = nullptr;
5104 267874 : if (lexer.peek_token ()->get_id () == EQUAL)
5105 : {
5106 : // must have an expression
5107 132339 : lexer.skip_token ();
5108 :
5109 132339 : auto expr_res = parse_expr ();
5110 132339 : if (!expr_res)
5111 : {
5112 22 : skip_after_semicolon ();
5113 22 : return nullptr;
5114 : }
5115 132317 : expr = std::move (expr_res.value ());
5116 132339 : }
5117 :
5118 133915 : tl::optional<std::unique_ptr<AST::Expr>> else_expr = tl::nullopt;
5119 133915 : if (maybe_skip_token (ELSE))
5120 : {
5121 5 : auto block_expr = parse_block_expr ();
5122 5 : if (block_expr)
5123 : else_expr = tl::optional<std::unique_ptr<AST::Expr>>{
5124 10 : std::move (block_expr.value ())};
5125 : else
5126 : else_expr = tl::nullopt;
5127 5 : }
5128 :
5129 133915 : if (restrictions.consume_semi)
5130 : {
5131 : // `stmt` macro variables are parsed without a semicolon, but should be
5132 : // parsed as a full statement when interpolated. This should be handled
5133 : // by having the interpolated statement be distinguishable from normal
5134 : // tokens, e.g. by NT tokens.
5135 133770 : if (restrictions.allow_close_after_expr_stmt)
5136 82 : maybe_skip_token (SEMICOLON);
5137 133688 : else if (!skip_token (SEMICOLON))
5138 1 : return nullptr;
5139 : }
5140 :
5141 : return std::unique_ptr<AST::LetStmt> (
5142 267833 : new AST::LetStmt (std::move (pattern), std::move (expr), std::move (type),
5143 133914 : std::move (else_expr), std::move (outer_attrs), locus));
5144 133937 : }
5145 :
5146 : template <typename ManagedTokenSource>
5147 : tl::optional<AST::GenericArg>
5148 190780 : Parser<ManagedTokenSource>::parse_generic_arg ()
5149 : {
5150 190780 : auto tok = lexer.peek_token ();
5151 190780 : std::unique_ptr<AST::Expr> expr = nullptr;
5152 :
5153 190780 : switch (tok->get_id ())
5154 : {
5155 149394 : case IDENTIFIER:
5156 : {
5157 : // This is a bit of a weird situation: With an identifier token, we
5158 : // could either have a valid type or a macro (FIXME: anything else?). So
5159 : // we need one bit of lookahead to differentiate if this is really
5160 149394 : auto next_tok = lexer.peek_token (1);
5161 149394 : if (next_tok->get_id () == LEFT_ANGLE
5162 139930 : || next_tok->get_id () == SCOPE_RESOLUTION
5163 288069 : || next_tok->get_id () == EXCLAM)
5164 : {
5165 10727 : auto type = parse_type ();
5166 10727 : if (type)
5167 10727 : return AST::GenericArg::create_type (std::move (type));
5168 : else
5169 0 : return tl::nullopt;
5170 10727 : }
5171 138667 : else if (next_tok->get_id () == COLON)
5172 : {
5173 0 : lexer.skip_token (); // skip ident
5174 0 : lexer.skip_token (); // skip colon
5175 :
5176 0 : auto tok = lexer.peek_token ();
5177 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
5178 : = parse_type_param_bounds ();
5179 :
5180 0 : auto type = std::unique_ptr<AST::TraitObjectType> (
5181 0 : new AST::TraitObjectType (std::move (bounds), tok->get_locus (),
5182 : false));
5183 0 : if (type)
5184 0 : return AST::GenericArg::create_type (std::move (type));
5185 : else
5186 : return tl::nullopt;
5187 0 : }
5188 138667 : lexer.skip_token ();
5189 554668 : return AST::GenericArg::create_ambiguous (tok->get_str (),
5190 138667 : tok->get_locus ());
5191 159839 : }
5192 23 : case LEFT_CURLY:
5193 : {
5194 23 : auto res = parse_block_expr ();
5195 23 : if (res)
5196 23 : expr = std::move (res.value ());
5197 : else
5198 0 : return tl::nullopt;
5199 0 : }
5200 23 : break;
5201 100 : case MINUS:
5202 : case STRING_LITERAL:
5203 : case CHAR_LITERAL:
5204 : case INT_LITERAL:
5205 : case FLOAT_LITERAL:
5206 : case TRUE_LITERAL:
5207 : case FALSE_LITERAL:
5208 : {
5209 100 : auto res = parse_literal_expr ();
5210 100 : if (res)
5211 100 : expr = std::move (res.value ());
5212 : else
5213 0 : return tl::nullopt;
5214 0 : }
5215 100 : break;
5216 : // FIXME: Because of this, error reporting is garbage for const generic
5217 : // parameter's default values
5218 41263 : default:
5219 : {
5220 41263 : auto type = parse_type ();
5221 : // FIXME: Find a better way to do this?
5222 41263 : if (type)
5223 41262 : return AST::GenericArg::create_type (std::move (type));
5224 : else
5225 1 : return tl::nullopt;
5226 41263 : }
5227 : }
5228 :
5229 123 : if (!expr)
5230 0 : return tl::nullopt;
5231 :
5232 123 : return AST::GenericArg::create_const (std::move (expr));
5233 190780 : }
5234 :
5235 : // Parses the generic arguments in each path segment.
5236 : template <typename ManagedTokenSource>
5237 : AST::GenericArgs
5238 193639 : Parser<ManagedTokenSource>::parse_path_generic_args ()
5239 : {
5240 387278 : if (lexer.peek_token ()->get_id () == LEFT_SHIFT)
5241 175 : lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
5242 :
5243 193639 : if (!skip_token (LEFT_ANGLE))
5244 : {
5245 : // skip after somewhere?
5246 0 : return AST::GenericArgs::create_empty ();
5247 : }
5248 :
5249 : // We need to parse all lifetimes, then parse types and const generics in
5250 : // any order.
5251 :
5252 : // try to parse lifetimes first
5253 193639 : std::vector<AST::Lifetime> lifetime_args;
5254 :
5255 193639 : const_TokenPtr t = lexer.peek_token ();
5256 193639 : location_t locus = t->get_locus ();
5257 393611 : while (!Parse::Utils::is_right_angle_tok (t->get_id ()))
5258 : {
5259 199972 : auto lifetime = parse_lifetime (false);
5260 199972 : if (!lifetime)
5261 : {
5262 : // not necessarily an error
5263 : break;
5264 : }
5265 :
5266 17138 : lifetime_args.push_back (std::move (lifetime.value ()));
5267 :
5268 : // if next token isn't comma, then it must be end of list
5269 34276 : if (lexer.peek_token ()->get_id () != COMMA)
5270 : {
5271 : break;
5272 : }
5273 : // skip comma
5274 6334 : lexer.skip_token ();
5275 :
5276 6334 : t = lexer.peek_token ();
5277 : }
5278 :
5279 : // try to parse types and const generics second
5280 193639 : std::vector<AST::GenericArg> generic_args;
5281 :
5282 : // TODO: think of better control structure
5283 193639 : t = lexer.peek_token ();
5284 388520 : while (!Parse::Utils::is_right_angle_tok (t->get_id ()))
5285 : {
5286 : // FIXME: Is it fine to break if there is one binding? Can't there be
5287 : // bindings in between types?
5288 :
5289 : // ensure not binding being parsed as type accidently
5290 194881 : if (t->get_id () == IDENTIFIER
5291 389762 : && (lexer.peek_token (1)->get_id () == EQUAL
5292 345226 : || lexer.peek_token (1)->get_id () == COLON))
5293 : break;
5294 :
5295 190756 : auto arg = parse_generic_arg ();
5296 190756 : if (arg)
5297 : {
5298 190756 : generic_args.emplace_back (std::move (arg.value ()));
5299 : }
5300 :
5301 : // FIXME: Do we need to break if we encounter an error?
5302 :
5303 : // if next token isn't comma, then it must be end of list
5304 381512 : if (lexer.peek_token ()->get_id () != COMMA)
5305 : break;
5306 :
5307 : // skip comma
5308 12067 : lexer.skip_token ();
5309 12067 : t = lexer.peek_token ();
5310 : }
5311 :
5312 : // try to parse bindings third
5313 193639 : std::vector<AST::GenericArgsBinding> binding_args;
5314 :
5315 : // TODO: think of better control structure
5316 193639 : t = lexer.peek_token ();
5317 197851 : while (!Parse::Utils::is_right_angle_tok (t->get_id ()))
5318 : {
5319 4212 : AST::GenericArgsBinding binding = parse_generic_args_binding ();
5320 4212 : if (binding.is_error ())
5321 : {
5322 : // not necessarily an error
5323 : break;
5324 : }
5325 :
5326 4212 : binding_args.push_back (std::move (binding));
5327 :
5328 : // if next token isn't comma, then it must be end of list
5329 8424 : if (lexer.peek_token ()->get_id () != COMMA)
5330 : {
5331 : break;
5332 : }
5333 : // skip comma
5334 87 : lexer.skip_token ();
5335 :
5336 87 : t = lexer.peek_token ();
5337 : }
5338 :
5339 : // skip any trailing commas
5340 387278 : if (lexer.peek_token ()->get_id () == COMMA)
5341 0 : lexer.skip_token ();
5342 :
5343 193639 : if (!skip_generics_right_angle ())
5344 0 : return AST::GenericArgs::create_empty ();
5345 :
5346 193639 : lifetime_args.shrink_to_fit ();
5347 193639 : generic_args.shrink_to_fit ();
5348 193639 : binding_args.shrink_to_fit ();
5349 :
5350 193639 : return AST::GenericArgs (std::move (lifetime_args), std::move (generic_args),
5351 193639 : std::move (binding_args), locus);
5352 387278 : }
5353 :
5354 : // Parses a binding in a generic args path segment.
5355 : template <typename ManagedTokenSource>
5356 : AST::GenericArgsBinding
5357 4212 : Parser<ManagedTokenSource>::parse_generic_args_binding ()
5358 : {
5359 4212 : const_TokenPtr ident_tok = lexer.peek_token ();
5360 4212 : if (ident_tok->get_id () != IDENTIFIER)
5361 : {
5362 : // allow non error-inducing use
5363 : // skip somewhere?
5364 0 : return AST::GenericArgsBinding::create_error ();
5365 : }
5366 4212 : lexer.skip_token ();
5367 8424 : Identifier ident{ident_tok};
5368 :
5369 8424 : if (lexer.peek_token ()->get_id () == COLON)
5370 : {
5371 953 : lexer.skip_token ();
5372 953 : auto bounds_locus = lexer.peek_token ()->get_locus ();
5373 953 : auto bounds = parse_type_param_bounds ();
5374 953 : auto type = std::unique_ptr<AST::Type> (
5375 953 : new AST::ImplTraitType (std::move (bounds), bounds_locus));
5376 1906 : return AST::GenericArgsBinding (
5377 : std::move (ident), std::move (type), ident_tok->get_locus (),
5378 1906 : AST::GenericArgsBinding::Kind::Constraint);
5379 953 : }
5380 :
5381 3259 : if (!skip_token (EQUAL))
5382 0 : return AST::GenericArgsBinding::create_error ();
5383 :
5384 : // parse type (required)
5385 3259 : std::unique_ptr<AST::Type> type = parse_type ();
5386 3259 : if (type == nullptr)
5387 : {
5388 : // skip somewhere?
5389 0 : return AST::GenericArgsBinding::create_error ();
5390 : }
5391 :
5392 6518 : return AST::GenericArgsBinding (std::move (ident), std::move (type),
5393 6518 : ident_tok->get_locus ());
5394 7471 : }
5395 :
5396 : // Parses a self param. Also handles self param not existing.
5397 : template <typename ManagedTokenSource>
5398 : tl::expected<std::unique_ptr<AST::Param>, Parse::Error::Self>
5399 184689 : Parser<ManagedTokenSource>::parse_self_param ()
5400 : {
5401 184689 : bool has_reference = false;
5402 184689 : AST::Lifetime lifetime = AST::Lifetime::elided ();
5403 :
5404 184689 : location_t locus = lexer.peek_token ()->get_locus ();
5405 :
5406 : // TODO: Feels off, find a better way to clearly express this
5407 738756 : std::vector<std::vector<TokenId>> ptrs
5408 : = {{ASTERISK, SELF} /* *self */,
5409 : {ASTERISK, CONST, SELF} /* *const self */,
5410 : {ASTERISK, MUT, SELF} /* *mut self */};
5411 :
5412 738750 : for (auto &s : ptrs)
5413 : {
5414 : size_t i = 0;
5415 554075 : for (i = 0; i < s.size (); i++)
5416 1108144 : if (lexer.peek_token (i)->get_id () != s[i])
5417 : break;
5418 554064 : if (i == s.size ())
5419 : {
5420 3 : Error error (lexer.peek_token ()->get_locus (),
5421 : "cannot pass %<self%> by raw pointer");
5422 3 : add_error (std::move (error));
5423 3 : return Parse::Error::Self::make_self_raw_pointer ();
5424 3 : }
5425 : }
5426 :
5427 : // Trying to find those patterns:
5428 : //
5429 : // &'lifetime mut self
5430 : // &'lifetime self
5431 : // & mut self
5432 : // & self
5433 : // mut self
5434 : // self
5435 : //
5436 : // If not found, it is probably a function, exit and let function parsing
5437 : // handle it.
5438 : bool is_self = false;
5439 1108116 : for (size_t i = 0; i < 5; i++)
5440 1846860 : if (lexer.peek_token (i)->get_id () == SELF)
5441 102265 : is_self = true;
5442 :
5443 184686 : if (!is_self)
5444 82426 : return Parse::Error::Self::make_not_self ();
5445 :
5446 : // test if self is a reference parameter
5447 204520 : if (lexer.peek_token ()->get_id () == AMP)
5448 : {
5449 51636 : has_reference = true;
5450 51636 : lexer.skip_token ();
5451 :
5452 : // now test whether it has a lifetime
5453 103272 : if (lexer.peek_token ()->get_id () == LIFETIME)
5454 : {
5455 : // something went wrong somehow
5456 1036 : if (auto parsed_lifetime = parse_lifetime (true))
5457 : {
5458 518 : lifetime = parsed_lifetime.value ();
5459 : }
5460 : else
5461 : {
5462 0 : Error error (lexer.peek_token ()->get_locus (),
5463 : "failed to parse lifetime in self param");
5464 0 : add_error (std::move (error));
5465 :
5466 : // skip after somewhere?
5467 0 : return Parse::Error::Self::make_parsing_error ();
5468 0 : }
5469 : }
5470 : }
5471 :
5472 : // test for mut
5473 102260 : bool has_mut = false;
5474 204520 : if (lexer.peek_token ()->get_id () == MUT)
5475 : {
5476 22856 : has_mut = true;
5477 22856 : lexer.skip_token ();
5478 : }
5479 :
5480 : // skip self token
5481 102260 : const_TokenPtr self_tok = lexer.peek_token ();
5482 102260 : if (self_tok->get_id () != SELF)
5483 : {
5484 : // skip after somewhere?
5485 4 : return Parse::Error::Self::make_not_self ();
5486 : }
5487 102256 : lexer.skip_token ();
5488 :
5489 : // parse optional type
5490 102256 : std::unique_ptr<AST::Type> type = nullptr;
5491 204512 : if (lexer.peek_token ()->get_id () == COLON)
5492 : {
5493 171 : lexer.skip_token ();
5494 :
5495 : // type is now required
5496 171 : type = parse_type ();
5497 171 : if (type == nullptr)
5498 : {
5499 0 : Error error (lexer.peek_token ()->get_locus (),
5500 : "could not parse type in self param");
5501 0 : add_error (std::move (error));
5502 :
5503 : // skip after somewhere?
5504 0 : return Parse::Error::Self::make_parsing_error ();
5505 0 : }
5506 : }
5507 :
5508 : // ensure that cannot have both type and reference
5509 102256 : if (type && has_reference)
5510 : {
5511 0 : Error error (
5512 0 : lexer.peek_token ()->get_locus (),
5513 : "cannot have both a reference and a type specified in a self param");
5514 0 : add_error (std::move (error));
5515 :
5516 : // skip after somewhere?
5517 0 : return Parse::Error::Self::make_parsing_error ();
5518 0 : }
5519 :
5520 102256 : if (has_reference)
5521 : {
5522 51636 : return std::make_unique<AST::SelfParam> (std::move (lifetime), has_mut,
5523 51636 : locus);
5524 : }
5525 : else
5526 : {
5527 : // note that type may be nullptr here and that's fine
5528 50620 : return std::make_unique<AST::SelfParam> (std::move (type), has_mut,
5529 50620 : locus);
5530 : }
5531 286945 : }
5532 :
5533 : /* Parses an expression or macro statement. */
5534 : template <typename ManagedTokenSource>
5535 : std::unique_ptr<AST::Stmt>
5536 2392 : Parser<ManagedTokenSource>::parse_expr_stmt (AST::AttrVec outer_attrs,
5537 : ParseRestrictions restrictions)
5538 : {
5539 2392 : location_t locus = lexer.peek_token ()->get_locus ();
5540 :
5541 2392 : tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr> expr;
5542 :
5543 4784 : switch (lexer.peek_token ()->get_id ())
5544 : {
5545 535 : case IDENTIFIER:
5546 : case CRATE:
5547 : case SUPER:
5548 : case SELF:
5549 : case SELF_ALIAS:
5550 : case DOLLAR_SIGN:
5551 : case SCOPE_RESOLUTION:
5552 : {
5553 535 : AST::PathInExpression path = parse_path_in_expression ();
5554 : tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
5555 535 : null_denotation;
5556 :
5557 1070 : if (lexer.peek_token ()->get_id () == EXCLAM)
5558 : {
5559 61 : std::unique_ptr<AST::MacroInvocation> invoc
5560 122 : = parse_macro_invocation_partial (std::move (path),
5561 : std::move (outer_attrs));
5562 :
5563 61 : if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
5564 : {
5565 59 : invoc->add_semicolon ();
5566 : // Macro invocation with semicolon.
5567 59 : return invoc;
5568 : }
5569 :
5570 2 : TokenId after_macro = lexer.peek_token ()->get_id ();
5571 :
5572 2 : if (restrictions.allow_close_after_expr_stmt
5573 2 : && (after_macro == RIGHT_PAREN || after_macro == RIGHT_CURLY
5574 : || after_macro == RIGHT_SQUARE))
5575 2 : return invoc;
5576 :
5577 0 : if (invoc->get_invoc_data ().get_delim_tok_tree ().get_delim_type ()
5578 : == AST::CURLY
5579 0 : && after_macro != DOT && after_macro != QUESTION_MARK)
5580 : {
5581 0 : rust_debug ("braced macro statement");
5582 0 : return invoc;
5583 : }
5584 :
5585 0 : null_denotation = std::move (invoc);
5586 61 : }
5587 : else
5588 : {
5589 : null_denotation
5590 948 : = null_denotation_path (std::move (path), {}, restrictions);
5591 : }
5592 :
5593 1896 : expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
5594 : std::move (outer_attrs), restrictions);
5595 : break;
5596 535 : }
5597 1857 : default:
5598 1857 : restrictions.expr_can_be_stmt = true;
5599 3714 : expr = parse_expr (std::move (outer_attrs), restrictions);
5600 1857 : break;
5601 : }
5602 :
5603 2331 : if (!expr)
5604 : {
5605 : // expr is required, error
5606 0 : Error error (lexer.peek_token ()->get_locus (),
5607 : "failed to parse expr in expr statement");
5608 0 : add_error (std::move (error));
5609 :
5610 0 : skip_after_semicolon ();
5611 0 : return nullptr;
5612 0 : }
5613 :
5614 2331 : bool has_semi = false;
5615 :
5616 2331 : if (restrictions.consume_semi)
5617 : {
5618 2315 : if (maybe_skip_token (SEMICOLON))
5619 : {
5620 210 : has_semi = true;
5621 : }
5622 2105 : else if (expr.value ()->is_expr_without_block ())
5623 : {
5624 254 : if (restrictions.allow_close_after_expr_stmt)
5625 : {
5626 254 : TokenId id = lexer.peek_token ()->get_id ();
5627 254 : if (id != RIGHT_PAREN && id != RIGHT_CURLY && id != RIGHT_SQUARE)
5628 : {
5629 3 : expect_token (SEMICOLON);
5630 3 : return nullptr;
5631 : }
5632 : }
5633 : else
5634 : {
5635 0 : expect_token (SEMICOLON);
5636 0 : return nullptr;
5637 : }
5638 : }
5639 : }
5640 :
5641 2328 : return std::make_unique<AST::ExprStmt> (std::move (expr.value ()), locus,
5642 2328 : has_semi);
5643 2392 : }
5644 :
5645 : // Parses a loop label used in loop expressions.
5646 : template <typename ManagedTokenSource>
5647 : tl::expected<AST::LoopLabel, Parse::Error::LoopLabel>
5648 83 : Parser<ManagedTokenSource>::parse_loop_label (const_TokenPtr tok)
5649 : {
5650 : // parse lifetime - if doesn't exist, assume no label
5651 83 : if (tok->get_id () != LIFETIME)
5652 : {
5653 : // not necessarily an error
5654 0 : return Parse::Error::LoopLabel::make_not_loop_label ();
5655 : }
5656 : /* FIXME: check for named lifetime requirement here? or check in semantic
5657 : * analysis phase? */
5658 166 : AST::Lifetime label = lifetime_from_token (tok);
5659 :
5660 83 : if (!skip_token (COLON))
5661 : {
5662 : // skip somewhere?
5663 83 : Parse::Error::LoopLabel::make_missing_colon ();
5664 : }
5665 :
5666 : return tl::expected<AST::LoopLabel, Parse::Error::LoopLabel> (
5667 83 : AST::LoopLabel (std::move (label), tok->get_locus ()));
5668 83 : }
5669 :
5670 : // Parses the "pattern" part of the match arm (the 'case x:' equivalent).
5671 : template <typename ManagedTokenSource>
5672 : AST::MatchArm
5673 576105 : Parser<ManagedTokenSource>::parse_match_arm ()
5674 : {
5675 : // parse optional outer attributes
5676 576105 : AST::AttrVec outer_attrs = parse_outer_attributes ();
5677 :
5678 : // DEBUG
5679 576105 : rust_debug ("about to start parsing match arm patterns");
5680 :
5681 : // break early if find right curly
5682 1152210 : if (lexer.peek_token ()->get_id () == RIGHT_CURLY)
5683 : {
5684 : // not an error
5685 0 : return AST::MatchArm::create_error ();
5686 : }
5687 :
5688 : // parse match arm patterns - at least 1 is required
5689 576105 : std::unique_ptr<AST::Pattern> match_arm_pattern
5690 : = parse_match_arm_pattern (RIGHT_CURLY);
5691 576105 : if (match_arm_pattern == nullptr)
5692 : {
5693 0 : Error error (lexer.peek_token ()->get_locus (),
5694 : "failed to parse any patterns in match arm");
5695 0 : add_error (std::move (error));
5696 :
5697 : // skip somewhere?
5698 0 : return AST::MatchArm::create_error ();
5699 0 : }
5700 :
5701 : // DEBUG
5702 576105 : rust_debug ("successfully parsed match arm patterns");
5703 :
5704 : // parse match arm guard expr if it exists
5705 576105 : std::unique_ptr<AST::Expr> guard_expr = nullptr;
5706 1152210 : if (lexer.peek_token ()->get_id () == IF)
5707 : {
5708 467 : lexer.skip_token ();
5709 :
5710 467 : auto guard_expr_res = parse_expr ();
5711 467 : if (!guard_expr_res)
5712 : {
5713 0 : Error error (lexer.peek_token ()->get_locus (),
5714 : "failed to parse guard expression in match arm");
5715 0 : add_error (std::move (error));
5716 :
5717 : // skip somewhere?
5718 0 : return AST::MatchArm::create_error ();
5719 0 : }
5720 467 : guard_expr = std::move (guard_expr_res.value ());
5721 467 : }
5722 :
5723 : // DEBUG
5724 576105 : rust_debug ("successfully parsed match arm");
5725 :
5726 1152210 : return AST::MatchArm (std::move (match_arm_pattern),
5727 1152210 : lexer.peek_token ()->get_locus (),
5728 576105 : std::move (guard_expr), std::move (outer_attrs));
5729 576105 : }
5730 :
5731 : /* Parses the patterns used in a match arm. End token id is the id of the
5732 : * token that would exist after the patterns are done (e.g. '}' for match
5733 : * expr, '=' for if let and while let). */
5734 : template <typename ManagedTokenSource>
5735 : std::unique_ptr<AST::Pattern>
5736 577596 : Parser<ManagedTokenSource>::parse_match_arm_pattern (TokenId end_token_id)
5737 : {
5738 : // skip optional leading '|'
5739 1155192 : if (lexer.peek_token ()->get_id () == PIPE)
5740 0 : lexer.skip_token ();
5741 : /* TODO: do I even need to store the result of this? can't be used.
5742 : * If semantically different, I need a wrapped "match arm patterns" object
5743 : * for this. */
5744 :
5745 577596 : std::unique_ptr<AST::Pattern> pattern;
5746 :
5747 : // quick break out if end_token_id
5748 1155192 : if (lexer.peek_token ()->get_id () == end_token_id)
5749 1 : return pattern;
5750 :
5751 : // parse required pattern - if doesn't exist, return empty
5752 577595 : std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
5753 577595 : if (initial_pattern == nullptr)
5754 : {
5755 : // FIXME: should this be an error?
5756 0 : return pattern;
5757 : }
5758 :
5759 577595 : return initial_pattern;
5760 577596 : }
5761 :
5762 : // Parses a single parameter used in a closure definition.
5763 : template <typename ManagedTokenSource>
5764 : AST::ClosureParam
5765 4659 : Parser<ManagedTokenSource>::parse_closure_param ()
5766 : {
5767 4659 : AST::AttrVec outer_attrs = parse_outer_attributes ();
5768 :
5769 : // parse pattern (which is required)
5770 4659 : std::unique_ptr<AST::Pattern> pattern = parse_pattern_no_alt ();
5771 4659 : if (pattern == nullptr)
5772 : {
5773 : // not necessarily an error
5774 0 : return AST::ClosureParam::create_error ();
5775 : }
5776 :
5777 : // parse optional type of param
5778 4659 : std::unique_ptr<AST::Type> type = nullptr;
5779 9318 : if (lexer.peek_token ()->get_id () == COLON)
5780 : {
5781 275 : lexer.skip_token ();
5782 :
5783 : // parse type, which is now required
5784 275 : type = parse_type ();
5785 275 : if (type == nullptr)
5786 : {
5787 0 : Error error (lexer.peek_token ()->get_locus (),
5788 : "failed to parse type in closure parameter");
5789 0 : add_error (std::move (error));
5790 :
5791 : // skip somewhere?
5792 0 : return AST::ClosureParam::create_error ();
5793 0 : }
5794 : }
5795 :
5796 4659 : location_t loc = pattern->get_locus ();
5797 4659 : return AST::ClosureParam (std::move (pattern), loc, std::move (type),
5798 4659 : std::move (outer_attrs));
5799 4659 : }
5800 :
5801 : // Parses a type (will further disambiguate any type).
5802 : template <typename ManagedTokenSource>
5803 : std::unique_ptr<AST::Type>
5804 801735 : Parser<ManagedTokenSource>::parse_type (bool save_errors)
5805 : {
5806 : /* rules for all types:
5807 : * NeverType: '!'
5808 : * SliceType: '[' Type ']'
5809 : * InferredType: '_'
5810 : * MacroInvocation: SimplePath '!' DelimTokenTree
5811 : * ParenthesisedType: '(' Type ')'
5812 : * ImplTraitType: 'impl' TypeParamBounds
5813 : * TypeParamBounds (not type) TypeParamBound ( '+' TypeParamBound )* '+'?
5814 : * TypeParamBound Lifetime | TraitBound
5815 : * ImplTraitTypeOneBound: 'impl' TraitBound
5816 : * TraitObjectType: 'dyn'? TypeParamBounds
5817 : * TraitObjectTypeOneBound: 'dyn'? TraitBound
5818 : * TraitBound '?'? ForLifetimes? TypePath | '(' '?'?
5819 : * ForLifetimes? TypePath ')' BareFunctionType: ForLifetimes?
5820 : * FunctionQualifiers 'fn' etc. ForLifetimes (not type) 'for' '<'
5821 : * LifetimeParams '>' FunctionQualifiers ( 'async' | 'const' )?
5822 : * 'unsafe'?
5823 : * ('extern' abi?)? QualifiedPathInType: '<' Type ( 'as' TypePath )? '>'
5824 : * (
5825 : * '::' TypePathSegment )+ TypePath: '::'? TypePathSegment (
5826 : * '::' TypePathSegment)* ArrayType: '[' Type ';' Expr ']'
5827 : * ReferenceType: '&' Lifetime? 'mut'? TypeNoBounds
5828 : * RawPointerType: '*' ( 'mut' | 'const' ) TypeNoBounds
5829 : * TupleType: '(' Type etc. - regular tuple stuff. Also
5830 : * regular tuple vs parenthesised precedence
5831 : *
5832 : * Disambiguate between macro and type path via type path being parsed, and
5833 : * then if '!' found, convert type path to simple path for macro. Usual
5834 : * disambiguation for tuple vs parenthesised. For ImplTraitType and
5835 : * TraitObjectType individual disambiguations, they seem more like "special
5836 : * cases", so probably just try to parse the more general ImplTraitType or
5837 : * TraitObjectType and return OneBound versions if they satisfy those
5838 : * criteria. */
5839 :
5840 801735 : const_TokenPtr t = lexer.peek_token ();
5841 801735 : switch (t->get_id ())
5842 : {
5843 804 : case EXCLAM:
5844 : // never type - can't be macro as no path beforehand
5845 804 : lexer.skip_token ();
5846 804 : return std::unique_ptr<AST::NeverType> (
5847 804 : new AST::NeverType (t->get_locus ()));
5848 5172 : case LEFT_SQUARE:
5849 : // slice type or array type - requires further disambiguation
5850 5172 : return parse_slice_or_array_type ();
5851 39079 : case LEFT_SHIFT:
5852 : case LEFT_ANGLE:
5853 : {
5854 : // qualified path in type
5855 39079 : AST::QualifiedPathInType path = parse_qualified_path_in_type ();
5856 39079 : if (path.is_error ())
5857 : {
5858 0 : if (save_errors)
5859 : {
5860 0 : Error error (t->get_locus (),
5861 : "failed to parse qualified path in type");
5862 0 : add_error (std::move (error));
5863 0 : }
5864 :
5865 0 : return nullptr;
5866 : }
5867 39079 : return std::unique_ptr<AST::QualifiedPathInType> (
5868 39079 : new AST::QualifiedPathInType (std::move (path)));
5869 39079 : }
5870 2658 : case UNDERSCORE:
5871 : // inferred type
5872 2658 : lexer.skip_token ();
5873 2658 : return std::unique_ptr<AST::InferredType> (
5874 2658 : new AST::InferredType (t->get_locus ()));
5875 11866 : case ASTERISK:
5876 : // raw pointer type
5877 11866 : return parse_raw_pointer_type ();
5878 93719 : case AMP: // does this also include AMP_AMP?
5879 : case LOGICAL_AND:
5880 : // reference type
5881 93719 : return parse_reference_type ();
5882 0 : case LIFETIME:
5883 : {
5884 : /* probably a lifetime bound, so probably type param bounds in
5885 : * TraitObjectType */
5886 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
5887 : = parse_type_param_bounds ();
5888 :
5889 0 : return std::unique_ptr<AST::TraitObjectType> (
5890 0 : new AST::TraitObjectType (std::move (bounds), t->get_locus (),
5891 0 : false));
5892 0 : }
5893 630609 : case IDENTIFIER:
5894 : case SUPER:
5895 : case SELF:
5896 : case SELF_ALIAS:
5897 : case CRATE:
5898 : case DOLLAR_SIGN:
5899 : case SCOPE_RESOLUTION:
5900 : {
5901 : // macro invocation or type path - requires further disambiguation.
5902 : /* for parsing path component of each rule, perhaps parse it as a
5903 : * typepath and attempt conversion to simplepath if a trailing '!' is
5904 : * found */
5905 : /* Type path also includes TraitObjectTypeOneBound BUT if it starts
5906 : * with it, it is exactly the same as a TypePath syntactically, so
5907 : * this is a syntactical ambiguity. As such, the parser will parse it
5908 : * as a TypePath. This, however, does not prevent TraitObjectType from
5909 : * starting with a typepath. */
5910 :
5911 : // parse path as type path
5912 630609 : AST::TypePath path = parse_type_path ();
5913 630609 : if (path.is_error ())
5914 : {
5915 0 : if (save_errors)
5916 : {
5917 0 : Error error (t->get_locus (),
5918 : "failed to parse path as first component of type");
5919 0 : add_error (std::move (error));
5920 0 : }
5921 :
5922 0 : return nullptr;
5923 : }
5924 630609 : location_t locus = path.get_locus ();
5925 :
5926 : // branch on next token
5927 630609 : t = lexer.peek_token ();
5928 630609 : switch (t->get_id ())
5929 : {
5930 5645 : case EXCLAM:
5931 : {
5932 : // macro invocation
5933 : // convert to simple path
5934 5645 : AST::SimplePath macro_path = path.as_simple_path ();
5935 5645 : if (macro_path.is_empty ())
5936 : {
5937 0 : if (save_errors)
5938 : {
5939 0 : Error error (t->get_locus (),
5940 : "failed to parse simple path in macro "
5941 : "invocation (for type)");
5942 0 : add_error (std::move (error));
5943 0 : }
5944 :
5945 0 : return nullptr;
5946 : }
5947 :
5948 5645 : lexer.skip_token ();
5949 :
5950 5645 : auto tok_tree = parse_delim_token_tree ();
5951 5645 : if (!tok_tree)
5952 0 : return nullptr;
5953 :
5954 11290 : return AST::MacroInvocation::Regular (
5955 11290 : AST::MacroInvocData (std::move (macro_path),
5956 5645 : std::move (tok_tree.value ())),
5957 5645 : {}, locus);
5958 11290 : }
5959 348 : case PLUS:
5960 : {
5961 : // type param bounds
5962 348 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
5963 :
5964 : // convert type path to trait bound
5965 348 : std::unique_ptr<AST::TraitBound> path_bound (
5966 348 : new AST::TraitBound (std::move (path), locus, false, false));
5967 348 : bounds.push_back (std::move (path_bound));
5968 :
5969 : /* parse rest of bounds - FIXME: better way to find when to stop
5970 : * parsing */
5971 696 : while (t->get_id () == PLUS)
5972 : {
5973 348 : lexer.skip_token ();
5974 :
5975 : // parse bound if it exists - if not, assume end of sequence
5976 348 : std::unique_ptr<AST::TypeParamBound> bound
5977 : = parse_type_param_bound ();
5978 348 : if (bound == nullptr)
5979 : {
5980 : break;
5981 : }
5982 348 : bounds.push_back (std::move (bound));
5983 :
5984 348 : t = lexer.peek_token ();
5985 : }
5986 :
5987 348 : return std::unique_ptr<AST::TraitObjectType> (
5988 348 : new AST::TraitObjectType (std::move (bounds), locus, false));
5989 348 : }
5990 624616 : default:
5991 : // assume that this is a type path and not an error
5992 624616 : return std::unique_ptr<AST::TypePath> (
5993 624616 : new AST::TypePath (std::move (path)));
5994 : }
5995 630609 : }
5996 8614 : case LEFT_PAREN:
5997 : /* tuple type or parenthesised type - requires further disambiguation
5998 : * (the usual). ok apparently can be a parenthesised TraitBound too, so
5999 : * could be TraitObjectTypeOneBound or TraitObjectType */
6000 8614 : return parse_paren_prefixed_type ();
6001 16 : case FOR:
6002 : // TraitObjectTypeOneBound or BareFunctionType
6003 16 : return parse_for_prefixed_type ();
6004 7548 : case ASYNC:
6005 : case CONST:
6006 : case UNSAFE:
6007 : case EXTERN_KW:
6008 : case FN_KW:
6009 : // bare function type (with no for lifetimes)
6010 7548 : return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
6011 1409 : case IMPL:
6012 1409 : lexer.skip_token ();
6013 2818 : if (lexer.peek_token ()->get_id () == LIFETIME)
6014 : {
6015 : /* cannot be one bound because lifetime prevents it from being
6016 : * traitbound */
6017 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
6018 : = parse_type_param_bounds ();
6019 :
6020 0 : return std::unique_ptr<AST::ImplTraitType> (
6021 0 : new AST::ImplTraitType (std::move (bounds), t->get_locus ()));
6022 0 : }
6023 : else
6024 : {
6025 : // should be trait bound, so parse trait bound
6026 1409 : std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
6027 1409 : if (initial_bound == nullptr)
6028 : {
6029 0 : if (save_errors)
6030 : {
6031 0 : Error error (lexer.peek_token ()->get_locus (),
6032 : "failed to parse ImplTraitType initial bound");
6033 0 : add_error (std::move (error));
6034 0 : }
6035 :
6036 0 : return nullptr;
6037 : }
6038 :
6039 1409 : location_t locus = t->get_locus ();
6040 :
6041 : // short cut if next token isn't '+'
6042 1409 : t = lexer.peek_token ();
6043 1409 : if (t->get_id () != PLUS)
6044 : {
6045 1383 : return std::unique_ptr<AST::ImplTraitTypeOneBound> (
6046 1383 : new AST::ImplTraitTypeOneBound (std::move (initial_bound),
6047 1383 : locus));
6048 : }
6049 :
6050 : // parse additional type param bounds
6051 26 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
6052 26 : bounds.push_back (std::move (initial_bound));
6053 52 : while (t->get_id () == PLUS)
6054 : {
6055 26 : lexer.skip_token ();
6056 :
6057 : // parse bound if it exists
6058 26 : std::unique_ptr<AST::TypeParamBound> bound
6059 : = parse_type_param_bound ();
6060 26 : if (bound == nullptr)
6061 : {
6062 : // not an error as trailing plus may exist
6063 : break;
6064 : }
6065 26 : bounds.push_back (std::move (bound));
6066 :
6067 26 : t = lexer.peek_token ();
6068 : }
6069 :
6070 26 : return std::unique_ptr<AST::ImplTraitType> (
6071 26 : new AST::ImplTraitType (std::move (bounds), locus));
6072 1409 : }
6073 225 : case DYN:
6074 : case QUESTION_MARK:
6075 : {
6076 : // either TraitObjectType or TraitObjectTypeOneBound
6077 225 : bool has_dyn = false;
6078 225 : if (t->get_id () == DYN)
6079 : {
6080 225 : lexer.skip_token ();
6081 225 : has_dyn = true;
6082 : }
6083 :
6084 450 : if (lexer.peek_token ()->get_id () == LIFETIME)
6085 : {
6086 : /* cannot be one bound because lifetime prevents it from being
6087 : * traitbound */
6088 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
6089 : = parse_type_param_bounds ();
6090 :
6091 0 : return std::unique_ptr<AST::TraitObjectType> (
6092 0 : new AST::TraitObjectType (std::move (bounds), t->get_locus (),
6093 0 : has_dyn));
6094 0 : }
6095 : else
6096 : {
6097 : // should be trait bound, so parse trait bound
6098 225 : std::unique_ptr<AST::TraitBound> initial_bound
6099 : = parse_trait_bound ();
6100 225 : if (initial_bound == nullptr)
6101 : {
6102 2 : if (save_errors)
6103 : {
6104 2 : Error error (
6105 2 : lexer.peek_token ()->get_locus (),
6106 : "failed to parse TraitObjectType initial bound");
6107 2 : add_error (std::move (error));
6108 2 : }
6109 :
6110 2 : return nullptr;
6111 : }
6112 :
6113 : // short cut if next token isn't '+'
6114 223 : t = lexer.peek_token ();
6115 223 : if (t->get_id () != PLUS)
6116 : {
6117 : // convert trait bound to value object
6118 49 : AST::TraitBound value_bound (*initial_bound);
6119 :
6120 : // DEBUG: removed as unique ptr, so should auto delete
6121 : // delete initial_bound;
6122 :
6123 49 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
6124 98 : new AST::TraitObjectTypeOneBound (std::move (value_bound),
6125 49 : t->get_locus (), has_dyn));
6126 49 : }
6127 :
6128 : // parse additional type param bounds
6129 174 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
6130 174 : bounds.push_back (std::move (initial_bound));
6131 380 : while (t->get_id () == PLUS)
6132 : {
6133 206 : lexer.skip_token ();
6134 :
6135 : // parse bound if it exists
6136 206 : std::unique_ptr<AST::TypeParamBound> bound
6137 : = parse_type_param_bound ();
6138 206 : if (bound == nullptr)
6139 : {
6140 : // not an error as trailing plus may exist
6141 : break;
6142 : }
6143 206 : bounds.push_back (std::move (bound));
6144 :
6145 206 : t = lexer.peek_token ();
6146 : }
6147 :
6148 174 : return std::unique_ptr<AST::TraitObjectType> (
6149 174 : new AST::TraitObjectType (std::move (bounds), t->get_locus (),
6150 174 : has_dyn));
6151 225 : }
6152 : }
6153 16 : default:
6154 16 : if (save_errors)
6155 16 : add_error (Error (t->get_locus (), "unrecognised token %qs in type",
6156 : t->get_token_description ()));
6157 :
6158 16 : return nullptr;
6159 : }
6160 801735 : }
6161 :
6162 : /* Parses a type that has '(' as its first character. Returns a tuple type,
6163 : * parenthesised type, TraitObjectTypeOneBound, or TraitObjectType depending
6164 : * on following characters. */
6165 : template <typename ManagedTokenSource>
6166 : std::unique_ptr<AST::Type>
6167 8614 : Parser<ManagedTokenSource>::parse_paren_prefixed_type ()
6168 : {
6169 : /* NOTE: Syntactical ambiguity of a parenthesised trait bound is considered
6170 : * a trait bound, not a parenthesised type, so that it can still be used in
6171 : * type param bounds. */
6172 :
6173 : /* NOTE: this implementation is really shit but I couldn't think of a better
6174 : * one. It requires essentially breaking polymorphism and downcasting via
6175 : * virtual method abuse, as it was copied from the rustc implementation (in
6176 : * which types are reified due to tagged union), after a more OOP attempt by
6177 : * me failed. */
6178 8614 : location_t left_delim_locus = lexer.peek_token ()->get_locus ();
6179 :
6180 : // skip left delim
6181 8614 : lexer.skip_token ();
6182 : /* while next token isn't close delim, parse comma-separated types, saving
6183 : * whether trailing comma happens */
6184 8614 : const_TokenPtr t = lexer.peek_token ();
6185 8614 : bool trailing_comma = true;
6186 8614 : std::vector<std::unique_ptr<AST::Type>> types;
6187 :
6188 22697 : while (t->get_id () != RIGHT_PAREN)
6189 : {
6190 19766 : std::unique_ptr<AST::Type> type = parse_type ();
6191 19766 : if (type == nullptr)
6192 : {
6193 0 : Error error (t->get_locus (),
6194 : "failed to parse type inside parentheses (probably "
6195 : "tuple or parenthesised)");
6196 0 : add_error (std::move (error));
6197 :
6198 0 : return nullptr;
6199 0 : }
6200 19766 : types.push_back (std::move (type));
6201 :
6202 19766 : t = lexer.peek_token ();
6203 19766 : if (t->get_id () != COMMA)
6204 : {
6205 5683 : trailing_comma = false;
6206 : break;
6207 : }
6208 14083 : lexer.skip_token ();
6209 :
6210 14083 : t = lexer.peek_token ();
6211 : }
6212 :
6213 8614 : if (!skip_token (RIGHT_PAREN))
6214 : {
6215 0 : return nullptr;
6216 : }
6217 :
6218 : // if only one type and no trailing comma, then not a tuple type
6219 8614 : if (types.size () == 1 && !trailing_comma)
6220 : {
6221 : // must be a TraitObjectType (with more than one bound)
6222 10 : if (lexer.peek_token ()->get_id () == PLUS)
6223 : {
6224 : // create type param bounds vector
6225 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
6226 :
6227 : // HACK: convert type to traitbound and add to bounds
6228 0 : std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
6229 0 : std::unique_ptr<AST::TraitBound> converted_bound (
6230 0 : released_ptr->to_trait_bound (true));
6231 0 : if (converted_bound == nullptr)
6232 : {
6233 0 : Error error (
6234 0 : lexer.peek_token ()->get_locus (),
6235 : "failed to hackily converted parsed type to trait bound");
6236 0 : add_error (std::move (error));
6237 :
6238 0 : return nullptr;
6239 0 : }
6240 0 : bounds.push_back (std::move (converted_bound));
6241 :
6242 0 : t = lexer.peek_token ();
6243 0 : while (t->get_id () == PLUS)
6244 : {
6245 0 : lexer.skip_token ();
6246 :
6247 : // attempt to parse typeparambound
6248 0 : std::unique_ptr<AST::TypeParamBound> bound
6249 : = parse_type_param_bound ();
6250 0 : if (bound == nullptr)
6251 : {
6252 : // not an error if null
6253 : break;
6254 : }
6255 0 : bounds.push_back (std::move (bound));
6256 :
6257 0 : t = lexer.peek_token ();
6258 : }
6259 :
6260 0 : return std::unique_ptr<AST::TraitObjectType> (
6261 0 : new AST::TraitObjectType (std::move (bounds), left_delim_locus,
6262 0 : false));
6263 0 : }
6264 : else
6265 : {
6266 : // release vector pointer
6267 5 : std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
6268 : /* HACK: attempt to convert to trait bound. if fails, parenthesised
6269 : * type */
6270 5 : std::unique_ptr<AST::TraitBound> converted_bound (
6271 5 : released_ptr->to_trait_bound (true));
6272 5 : if (converted_bound == nullptr)
6273 : {
6274 : // parenthesised type
6275 5 : return std::unique_ptr<AST::ParenthesisedType> (
6276 5 : new AST::ParenthesisedType (std::move (released_ptr),
6277 5 : left_delim_locus));
6278 : }
6279 : else
6280 : {
6281 : // trait object type (one bound)
6282 :
6283 : // get value semantics trait bound
6284 0 : AST::TraitBound value_bound (*converted_bound);
6285 :
6286 0 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
6287 0 : new AST::TraitObjectTypeOneBound (value_bound,
6288 0 : left_delim_locus));
6289 0 : }
6290 5 : }
6291 : }
6292 : else
6293 : {
6294 8609 : return std::unique_ptr<AST::TupleType> (
6295 8609 : new AST::TupleType (std::move (types), left_delim_locus));
6296 : }
6297 : /* TODO: ensure that this ensures that dynamic dispatch for traits is not
6298 : * lost somehow */
6299 8614 : }
6300 :
6301 : /* Parses a type that has 'for' as its first character. This means it has a
6302 : * "for lifetimes", so returns either a BareFunctionType, TraitObjectType, or
6303 : * TraitObjectTypeOneBound depending on following characters. */
6304 : template <typename ManagedTokenSource>
6305 : std::unique_ptr<AST::Type>
6306 16 : Parser<ManagedTokenSource>::parse_for_prefixed_type ()
6307 : {
6308 16 : location_t for_locus = lexer.peek_token ()->get_locus ();
6309 : // parse for lifetimes in type
6310 16 : std::vector<AST::LifetimeParam> for_lifetimes = parse_for_lifetimes ();
6311 :
6312 : // branch on next token - either function or a trait type
6313 16 : const_TokenPtr t = lexer.peek_token ();
6314 16 : switch (t->get_id ())
6315 : {
6316 4 : case ASYNC:
6317 : case CONST:
6318 : case UNSAFE:
6319 : case EXTERN_KW:
6320 : case FN_KW:
6321 4 : return parse_bare_function_type (std::move (for_lifetimes));
6322 12 : case SCOPE_RESOLUTION:
6323 : case IDENTIFIER:
6324 : case SUPER:
6325 : case SELF:
6326 : case SELF_ALIAS:
6327 : case CRATE:
6328 : case DOLLAR_SIGN:
6329 : {
6330 : // path, so trait type
6331 :
6332 : // parse type path to finish parsing trait bound
6333 12 : AST::TypePath path = parse_type_path ();
6334 :
6335 12 : t = lexer.peek_token ();
6336 12 : if (t->get_id () != PLUS)
6337 : {
6338 : // must be one-bound trait type
6339 : // create trait bound value object
6340 12 : AST::TraitBound bound (std::move (path), for_locus, false, false,
6341 : std::move (for_lifetimes));
6342 :
6343 12 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
6344 24 : new AST::TraitObjectTypeOneBound (std::move (bound), for_locus));
6345 12 : }
6346 :
6347 : /* more than one bound trait type (or at least parsed as it - could be
6348 : * trailing '+') create trait bound pointer and bounds */
6349 0 : std::unique_ptr<AST::TraitBound> initial_bound (
6350 0 : new AST::TraitBound (std::move (path), for_locus, false, false,
6351 : std::move (for_lifetimes)));
6352 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
6353 0 : bounds.push_back (std::move (initial_bound));
6354 :
6355 0 : while (t->get_id () == PLUS)
6356 : {
6357 0 : lexer.skip_token ();
6358 :
6359 : // parse type param bound if it exists
6360 0 : std::unique_ptr<AST::TypeParamBound> bound
6361 : = parse_type_param_bound ();
6362 0 : if (bound == nullptr)
6363 : {
6364 : // not an error - e.g. trailing plus
6365 0 : return nullptr;
6366 : }
6367 0 : bounds.push_back (std::move (bound));
6368 :
6369 0 : t = lexer.peek_token ();
6370 : }
6371 :
6372 0 : return std::unique_ptr<AST::TraitObjectType> (
6373 0 : new AST::TraitObjectType (std::move (bounds), for_locus, false));
6374 12 : }
6375 0 : default:
6376 : // error
6377 0 : add_error (Error (t->get_locus (),
6378 : "unrecognised token %qs in bare function type or trait "
6379 : "object type or trait object type one bound",
6380 : t->get_token_description ()));
6381 :
6382 0 : return nullptr;
6383 : }
6384 16 : }
6385 :
6386 : // Parses a maybe named param used in bare function types.
6387 : template <typename ManagedTokenSource>
6388 : AST::MaybeNamedParam
6389 45206 : Parser<ManagedTokenSource>::parse_maybe_named_param (AST::AttrVec outer_attrs)
6390 : {
6391 : /* Basically guess that param is named if first token is identifier or
6392 : * underscore and second token is semicolon. This should probably have no
6393 : * exceptions. rustc uses backtracking to parse these, but at the time of
6394 : * writing gccrs has no backtracking capabilities. */
6395 45206 : const_TokenPtr current = lexer.peek_token ();
6396 45206 : const_TokenPtr next = lexer.peek_token (1);
6397 :
6398 45206 : Identifier name;
6399 45206 : AST::MaybeNamedParam::ParamKind kind = AST::MaybeNamedParam::UNNAMED;
6400 :
6401 45206 : if (current->get_id () == IDENTIFIER && next->get_id () == COLON)
6402 : {
6403 : // named param
6404 1 : name = {current};
6405 1 : kind = AST::MaybeNamedParam::IDENTIFIER;
6406 1 : lexer.skip_token (1);
6407 : }
6408 45205 : else if (current->get_id () == UNDERSCORE && next->get_id () == COLON)
6409 : {
6410 : // wildcard param
6411 12 : name = {Values::Keywords::UNDERSCORE, current->get_locus ()};
6412 6 : kind = AST::MaybeNamedParam::WILDCARD;
6413 6 : lexer.skip_token (1);
6414 : }
6415 :
6416 : // parse type (required)
6417 45206 : std::unique_ptr<AST::Type> type = parse_type ();
6418 45206 : if (type == nullptr)
6419 : {
6420 0 : Error error (lexer.peek_token ()->get_locus (),
6421 : "failed to parse type in maybe named param");
6422 0 : add_error (std::move (error));
6423 :
6424 0 : return AST::MaybeNamedParam::create_error ();
6425 0 : }
6426 :
6427 90412 : return AST::MaybeNamedParam (std::move (name), kind, std::move (type),
6428 45206 : std::move (outer_attrs), current->get_locus ());
6429 90412 : }
6430 :
6431 : /* Parses a bare function type (with the given for lifetimes for convenience -
6432 : * does not parse them itself). */
6433 : template <typename ManagedTokenSource>
6434 : std::unique_ptr<AST::BareFunctionType>
6435 7554 : Parser<ManagedTokenSource>::parse_bare_function_type (
6436 : std::vector<AST::LifetimeParam> for_lifetimes)
6437 : {
6438 : // TODO: pass in for lifetime location as param
6439 7554 : location_t best_try_locus = lexer.peek_token ()->get_locus ();
6440 :
6441 7554 : auto qualifiers = parse_function_qualifiers ();
6442 7554 : if (!qualifiers)
6443 0 : return nullptr;
6444 :
6445 7554 : if (!skip_token (FN_KW))
6446 0 : return nullptr;
6447 :
6448 7554 : if (!skip_token (LEFT_PAREN))
6449 0 : return nullptr;
6450 :
6451 : // parse function params, if they exist
6452 7554 : std::vector<AST::MaybeNamedParam> params;
6453 7554 : bool is_variadic = false;
6454 7554 : AST::AttrVec variadic_attrs;
6455 :
6456 7554 : const_TokenPtr t = lexer.peek_token ();
6457 95440 : while (t->get_id () != RIGHT_PAREN)
6458 : {
6459 47510 : AST::AttrVec temp_attrs = parse_outer_attributes ();
6460 :
6461 95020 : if (lexer.peek_token ()->get_id () == ELLIPSIS)
6462 : {
6463 2304 : lexer.skip_token ();
6464 2304 : is_variadic = true;
6465 2304 : variadic_attrs = std::move (temp_attrs);
6466 :
6467 2304 : t = lexer.peek_token ();
6468 :
6469 2304 : if (t->get_id () != RIGHT_PAREN)
6470 : {
6471 0 : Error error (t->get_locus (),
6472 : "expected right parentheses after variadic in maybe "
6473 : "named function "
6474 : "parameters, found %qs",
6475 : t->get_token_description ());
6476 0 : add_error (std::move (error));
6477 :
6478 0 : return nullptr;
6479 0 : }
6480 :
6481 : break;
6482 : }
6483 :
6484 45206 : AST::MaybeNamedParam param
6485 45206 : = parse_maybe_named_param (std::move (temp_attrs));
6486 45206 : if (param.is_error ())
6487 : {
6488 0 : Error error (
6489 0 : lexer.peek_token ()->get_locus (),
6490 : "failed to parse maybe named param in bare function type");
6491 0 : add_error (std::move (error));
6492 :
6493 0 : return nullptr;
6494 0 : }
6495 45206 : params.push_back (std::move (param));
6496 :
6497 90412 : if (lexer.peek_token ()->get_id () != COMMA)
6498 : break;
6499 :
6500 40376 : lexer.skip_token ();
6501 40376 : t = lexer.peek_token ();
6502 : }
6503 :
6504 7554 : if (!skip_token (RIGHT_PAREN))
6505 0 : return nullptr;
6506 :
6507 : // bare function return type, if exists
6508 7554 : std::unique_ptr<AST::TypeNoBounds> return_type = nullptr;
6509 15108 : if (lexer.peek_token ()->get_id () == RETURN_TYPE)
6510 : {
6511 7437 : lexer.skip_token ();
6512 :
6513 : // parse required TypeNoBounds
6514 7437 : return_type = parse_type_no_bounds ();
6515 7437 : if (return_type == nullptr)
6516 : {
6517 0 : Error error (lexer.peek_token ()->get_locus (),
6518 : "failed to parse return type (type no bounds) in bare "
6519 : "function type");
6520 0 : add_error (std::move (error));
6521 :
6522 0 : return nullptr;
6523 0 : }
6524 : }
6525 :
6526 7554 : return std::unique_ptr<AST::BareFunctionType> (new AST::BareFunctionType (
6527 7554 : std::move (for_lifetimes), std::move (qualifiers.value ()),
6528 : std::move (params), is_variadic, std::move (variadic_attrs),
6529 7554 : std::move (return_type), best_try_locus));
6530 15108 : }
6531 :
6532 : template <typename ManagedTokenSource>
6533 : std::unique_ptr<AST::ReferenceType>
6534 94016 : Parser<ManagedTokenSource>::parse_reference_type_inner (location_t locus)
6535 : {
6536 : // parse optional lifetime
6537 94016 : AST::Lifetime lifetime = AST::Lifetime::elided ();
6538 188032 : if (lexer.peek_token ()->get_id () == LIFETIME)
6539 : {
6540 15834 : auto parsed_lifetime = parse_lifetime (true);
6541 15834 : if (parsed_lifetime)
6542 : {
6543 15834 : lifetime = parsed_lifetime.value ();
6544 : }
6545 : else
6546 : {
6547 0 : Error error (lexer.peek_token ()->get_locus (),
6548 : "failed to parse lifetime in reference type");
6549 0 : add_error (std::move (error));
6550 :
6551 0 : return nullptr;
6552 0 : }
6553 15834 : }
6554 :
6555 94016 : bool is_mut = false;
6556 188032 : if (lexer.peek_token ()->get_id () == MUT)
6557 : {
6558 15931 : lexer.skip_token ();
6559 15931 : is_mut = true;
6560 : }
6561 :
6562 : // parse type no bounds, which is required
6563 94016 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
6564 94016 : if (type == nullptr)
6565 : {
6566 0 : Error error (lexer.peek_token ()->get_locus (),
6567 : "failed to parse referenced type in reference type");
6568 0 : add_error (std::move (error));
6569 :
6570 0 : return nullptr;
6571 0 : }
6572 :
6573 : return std::unique_ptr<AST::ReferenceType> (
6574 282048 : new AST::ReferenceType (is_mut, std::move (type), locus,
6575 94016 : std::move (lifetime)));
6576 94016 : }
6577 :
6578 : // Parses a reference type (mutable or immutable, with given lifetime).
6579 : template <typename ManagedTokenSource>
6580 : std::unique_ptr<AST::ReferenceType>
6581 94016 : Parser<ManagedTokenSource>::parse_reference_type ()
6582 : {
6583 94016 : auto t = lexer.peek_token ();
6584 94016 : auto locus = t->get_locus ();
6585 :
6586 94016 : switch (t->get_id ())
6587 : {
6588 93693 : case AMP:
6589 93693 : skip_token (AMP);
6590 93693 : return parse_reference_type_inner (locus);
6591 323 : case LOGICAL_AND:
6592 323 : skip_token (LOGICAL_AND);
6593 : return std::unique_ptr<AST::ReferenceType> (
6594 969 : new AST::ReferenceType (false, parse_reference_type_inner (locus),
6595 323 : locus));
6596 0 : default:
6597 0 : rust_unreachable ();
6598 : }
6599 94016 : }
6600 :
6601 : // Parses a raw (unsafe) pointer type.
6602 : template <typename ManagedTokenSource>
6603 : std::unique_ptr<AST::RawPointerType>
6604 24008 : Parser<ManagedTokenSource>::parse_raw_pointer_type ()
6605 : {
6606 24008 : location_t locus = lexer.peek_token ()->get_locus ();
6607 24008 : skip_token (ASTERISK);
6608 :
6609 24008 : AST::RawPointerType::PointerType kind = AST::RawPointerType::CONST;
6610 :
6611 : // branch on next token for pointer kind info
6612 24008 : const_TokenPtr t = lexer.peek_token ();
6613 24008 : switch (t->get_id ())
6614 : {
6615 9150 : case MUT:
6616 9150 : kind = AST::RawPointerType::MUT;
6617 9150 : lexer.skip_token ();
6618 9150 : break;
6619 14858 : case CONST:
6620 14858 : kind = AST::RawPointerType::CONST;
6621 14858 : lexer.skip_token ();
6622 14858 : break;
6623 0 : default:
6624 0 : add_error (Error (t->get_locus (),
6625 : "unrecognised token %qs in raw pointer type",
6626 : t->get_token_description ()));
6627 :
6628 0 : return nullptr;
6629 : }
6630 :
6631 : // parse type no bounds (required)
6632 24008 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
6633 24008 : if (type == nullptr)
6634 : {
6635 0 : Error error (lexer.peek_token ()->get_locus (),
6636 : "failed to parse pointed type of raw pointer type");
6637 0 : add_error (std::move (error));
6638 :
6639 0 : return nullptr;
6640 0 : }
6641 :
6642 : return std::unique_ptr<AST::RawPointerType> (
6643 24008 : new AST::RawPointerType (kind, std::move (type), locus));
6644 24008 : }
6645 :
6646 : /* Parses a slice or array type, depending on following arguments (as
6647 : * lookahead is not possible). */
6648 : template <typename ManagedTokenSource>
6649 : std::unique_ptr<AST::TypeNoBounds>
6650 12974 : Parser<ManagedTokenSource>::parse_slice_or_array_type ()
6651 : {
6652 12974 : location_t locus = lexer.peek_token ()->get_locus ();
6653 12974 : skip_token (LEFT_SQUARE);
6654 :
6655 : // parse inner type (required)
6656 12974 : std::unique_ptr<AST::Type> inner_type = parse_type ();
6657 12974 : if (inner_type == nullptr)
6658 : {
6659 0 : Error error (lexer.peek_token ()->get_locus (),
6660 : "failed to parse inner type in slice or array type");
6661 0 : add_error (std::move (error));
6662 :
6663 0 : return nullptr;
6664 0 : }
6665 :
6666 : // branch on next token
6667 12974 : const_TokenPtr t = lexer.peek_token ();
6668 12974 : switch (t->get_id ())
6669 : {
6670 8134 : case RIGHT_SQUARE:
6671 : // slice type
6672 8134 : lexer.skip_token ();
6673 :
6674 8134 : return std::unique_ptr<AST::SliceType> (
6675 8134 : new AST::SliceType (std::move (inner_type), locus));
6676 4840 : case SEMICOLON:
6677 : {
6678 : // array type
6679 4840 : lexer.skip_token ();
6680 :
6681 : // parse required array size expression
6682 4840 : auto size = parse_anon_const ();
6683 :
6684 4840 : if (!size)
6685 : {
6686 1 : Error error (lexer.peek_token ()->get_locus (),
6687 : "failed to parse size expression in array type");
6688 1 : add_error (std::move (error));
6689 :
6690 1 : return nullptr;
6691 1 : }
6692 :
6693 4839 : if (!skip_token (RIGHT_SQUARE))
6694 : {
6695 0 : return nullptr;
6696 : }
6697 :
6698 4839 : return std::unique_ptr<AST::ArrayType> (
6699 9665 : new AST::ArrayType (std::move (inner_type), std::move (*size),
6700 4839 : locus));
6701 4840 : }
6702 0 : default:
6703 : // error
6704 0 : add_error (
6705 0 : Error (t->get_locus (),
6706 : "unrecognised token %qs in slice or array type after inner type",
6707 : t->get_token_description ()));
6708 :
6709 0 : return nullptr;
6710 : }
6711 12974 : }
6712 :
6713 : // Parses a type, taking into account type boundary disambiguation.
6714 : template <typename ManagedTokenSource>
6715 : std::unique_ptr<AST::TypeNoBounds>
6716 177030 : Parser<ManagedTokenSource>::parse_type_no_bounds ()
6717 : {
6718 177030 : const_TokenPtr t = lexer.peek_token ();
6719 177030 : switch (t->get_id ())
6720 : {
6721 38 : case EXCLAM:
6722 : // never type - can't be macro as no path beforehand
6723 38 : lexer.skip_token ();
6724 38 : return std::unique_ptr<AST::NeverType> (
6725 38 : new AST::NeverType (t->get_locus ()));
6726 7802 : case LEFT_SQUARE:
6727 : // slice type or array type - requires further disambiguation
6728 7802 : return parse_slice_or_array_type ();
6729 34 : case LEFT_SHIFT:
6730 : case LEFT_ANGLE:
6731 : {
6732 : // qualified path in type
6733 34 : AST::QualifiedPathInType path = parse_qualified_path_in_type ();
6734 34 : if (path.is_error ())
6735 : {
6736 0 : Error error (t->get_locus (),
6737 : "failed to parse qualified path in type");
6738 0 : add_error (std::move (error));
6739 :
6740 0 : return nullptr;
6741 0 : }
6742 34 : return std::unique_ptr<AST::QualifiedPathInType> (
6743 34 : new AST::QualifiedPathInType (std::move (path)));
6744 34 : }
6745 1160 : case UNDERSCORE:
6746 : // inferred type
6747 1160 : lexer.skip_token ();
6748 1160 : return std::unique_ptr<AST::InferredType> (
6749 1160 : new AST::InferredType (t->get_locus ()));
6750 12142 : case ASTERISK:
6751 : // raw pointer type
6752 12142 : return parse_raw_pointer_type ();
6753 297 : case AMP: // does this also include AMP_AMP? Yes! Which is... LOGICAL_AND?
6754 : case LOGICAL_AND:
6755 : // reference type
6756 297 : return parse_reference_type ();
6757 0 : case LIFETIME:
6758 : /* probably a lifetime bound, so probably type param bounds in
6759 : * TraitObjectType. this is not allowed, but detection here for error
6760 : * message */
6761 0 : add_error (Error (t->get_locus (),
6762 : "lifetime bounds (i.e. in type param bounds, in "
6763 : "TraitObjectType) are not allowed as TypeNoBounds"));
6764 :
6765 0 : return nullptr;
6766 151445 : case IDENTIFIER:
6767 : case SUPER:
6768 : case SELF:
6769 : case SELF_ALIAS:
6770 : case CRATE:
6771 : case DOLLAR_SIGN:
6772 : case SCOPE_RESOLUTION:
6773 : {
6774 : // macro invocation or type path - requires further disambiguation.
6775 : /* for parsing path component of each rule, perhaps parse it as a
6776 : * typepath and attempt conversion to simplepath if a trailing '!' is
6777 : * found */
6778 : /* Type path also includes TraitObjectTypeOneBound BUT if it starts
6779 : * with it, it is exactly the same as a TypePath syntactically, so
6780 : * this is a syntactical ambiguity. As such, the parser will parse it
6781 : * as a TypePath. This, however, does not prevent TraitObjectType from
6782 : * starting with a typepath. */
6783 :
6784 : // parse path as type path
6785 151445 : AST::TypePath path = parse_type_path ();
6786 151445 : if (path.is_error ())
6787 : {
6788 0 : Error error (
6789 : t->get_locus (),
6790 : "failed to parse path as first component of type no bounds");
6791 0 : add_error (std::move (error));
6792 :
6793 0 : return nullptr;
6794 0 : }
6795 151445 : location_t locus = path.get_locus ();
6796 :
6797 : // branch on next token
6798 151445 : t = lexer.peek_token ();
6799 151445 : switch (t->get_id ())
6800 : {
6801 1 : case EXCLAM:
6802 : {
6803 : // macro invocation
6804 : // convert to simple path
6805 1 : AST::SimplePath macro_path = path.as_simple_path ();
6806 1 : if (macro_path.is_empty ())
6807 : {
6808 0 : Error error (t->get_locus (),
6809 : "failed to parse simple path in macro "
6810 : "invocation (for type)");
6811 0 : add_error (std::move (error));
6812 :
6813 0 : return nullptr;
6814 0 : }
6815 :
6816 1 : lexer.skip_token ();
6817 :
6818 1 : auto tok_tree = parse_delim_token_tree ();
6819 1 : if (!tok_tree)
6820 0 : return nullptr;
6821 :
6822 2 : return AST::MacroInvocation::Regular (
6823 2 : AST::MacroInvocData (std::move (macro_path),
6824 1 : std::move (tok_tree.value ())),
6825 1 : {}, locus);
6826 2 : }
6827 151444 : default:
6828 : // assume that this is a type path and not an error
6829 151444 : return std::unique_ptr<AST::TypePath> (
6830 151444 : new AST::TypePath (std::move (path)));
6831 : }
6832 151445 : }
6833 3467 : case LEFT_PAREN:
6834 : /* tuple type or parenthesised type - requires further disambiguation
6835 : * (the usual). ok apparently can be a parenthesised TraitBound too, so
6836 : * could be TraitObjectTypeOneBound */
6837 3467 : return parse_paren_prefixed_type_no_bounds ();
6838 2 : case FOR:
6839 : case ASYNC:
6840 : case CONST:
6841 : case UNSAFE:
6842 : case EXTERN_KW:
6843 : case FN_KW:
6844 : // bare function type (with no for lifetimes)
6845 2 : return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
6846 206 : case IMPL:
6847 206 : lexer.skip_token ();
6848 412 : if (lexer.peek_token ()->get_id () == LIFETIME)
6849 : {
6850 : /* cannot be one bound because lifetime prevents it from being
6851 : * traitbound not allowed as type no bounds, only here for error
6852 : * message */
6853 0 : Error error (
6854 0 : lexer.peek_token ()->get_locus (),
6855 : "lifetime (probably lifetime bound, in type param "
6856 : "bounds, in ImplTraitType) is not allowed in TypeNoBounds");
6857 0 : add_error (std::move (error));
6858 :
6859 0 : return nullptr;
6860 0 : }
6861 : else
6862 : {
6863 : // should be trait bound, so parse trait bound
6864 206 : std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
6865 206 : if (initial_bound == nullptr)
6866 : {
6867 0 : Error error (lexer.peek_token ()->get_locus (),
6868 : "failed to parse ImplTraitTypeOneBound bound");
6869 0 : add_error (std::move (error));
6870 :
6871 0 : return nullptr;
6872 0 : }
6873 :
6874 206 : location_t locus = t->get_locus ();
6875 :
6876 : // ensure not a trait with multiple bounds
6877 206 : t = lexer.peek_token ();
6878 206 : if (t->get_id () == PLUS)
6879 : {
6880 0 : Error error (t->get_locus (),
6881 : "plus after trait bound means an ImplTraitType, "
6882 : "which is not allowed as a TypeNoBounds");
6883 0 : add_error (std::move (error));
6884 :
6885 0 : return nullptr;
6886 0 : }
6887 :
6888 206 : return std::unique_ptr<AST::ImplTraitTypeOneBound> (
6889 206 : new AST::ImplTraitTypeOneBound (std::move (initial_bound), locus));
6890 206 : }
6891 437 : case DYN:
6892 : case QUESTION_MARK:
6893 : {
6894 : // either TraitObjectTypeOneBound
6895 437 : bool has_dyn = false;
6896 437 : if (t->get_id () == DYN)
6897 : {
6898 437 : lexer.skip_token ();
6899 437 : has_dyn = true;
6900 : }
6901 :
6902 874 : if (lexer.peek_token ()->get_id () == LIFETIME)
6903 : {
6904 : /* means that cannot be TraitObjectTypeOneBound - so here for
6905 : * error message */
6906 0 : Error error (lexer.peek_token ()->get_locus (),
6907 : "lifetime as bound in TraitObjectTypeOneBound "
6908 : "is not allowed, so cannot be TypeNoBounds");
6909 0 : add_error (std::move (error));
6910 :
6911 0 : return nullptr;
6912 0 : }
6913 :
6914 : // should be trait bound, so parse trait bound
6915 437 : std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
6916 437 : if (initial_bound == nullptr)
6917 : {
6918 0 : Error error (
6919 0 : lexer.peek_token ()->get_locus (),
6920 : "failed to parse TraitObjectTypeOneBound initial bound");
6921 0 : add_error (std::move (error));
6922 :
6923 0 : return nullptr;
6924 0 : }
6925 :
6926 437 : location_t locus = t->get_locus ();
6927 :
6928 : // detect error with plus as next token
6929 437 : t = lexer.peek_token ();
6930 437 : if (t->get_id () == PLUS)
6931 : {
6932 0 : Error error (t->get_locus (),
6933 : "plus after trait bound means a TraitObjectType, "
6934 : "which is not allowed as a TypeNoBounds");
6935 0 : add_error (std::move (error));
6936 :
6937 0 : return nullptr;
6938 0 : }
6939 :
6940 : // convert trait bound to value object
6941 437 : AST::TraitBound value_bound (*initial_bound);
6942 :
6943 437 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
6944 874 : new AST::TraitObjectTypeOneBound (std::move (value_bound), locus,
6945 437 : has_dyn));
6946 437 : }
6947 0 : default:
6948 0 : add_error (Error (t->get_locus (),
6949 : "unrecognised token %qs in type no bounds",
6950 : t->get_token_description ()));
6951 :
6952 0 : return nullptr;
6953 : }
6954 177030 : }
6955 :
6956 : // Parses a type no bounds beginning with '('.
6957 : template <typename ManagedTokenSource>
6958 : std::unique_ptr<AST::TypeNoBounds>
6959 3467 : Parser<ManagedTokenSource>::parse_paren_prefixed_type_no_bounds ()
6960 : {
6961 : /* NOTE: this could probably be parsed without the HACK solution of
6962 : * parse_paren_prefixed_type, but I was lazy. So FIXME for future.*/
6963 :
6964 : /* NOTE: again, syntactical ambiguity of a parenthesised trait bound is
6965 : * considered a trait bound, not a parenthesised type, so that it can still
6966 : * be used in type param bounds. */
6967 :
6968 3467 : location_t left_paren_locus = lexer.peek_token ()->get_locus ();
6969 :
6970 : // skip left delim
6971 3467 : lexer.skip_token ();
6972 : /* while next token isn't close delim, parse comma-separated types, saving
6973 : * whether trailing comma happens */
6974 3467 : const_TokenPtr t = lexer.peek_token ();
6975 3467 : bool trailing_comma = true;
6976 3467 : std::vector<std::unique_ptr<AST::Type>> types;
6977 :
6978 11032 : while (t->get_id () != RIGHT_PAREN)
6979 : {
6980 7754 : std::unique_ptr<AST::Type> type = parse_type ();
6981 7754 : if (type == nullptr)
6982 : {
6983 0 : Error error (t->get_locus (),
6984 : "failed to parse type inside parentheses (probably "
6985 : "tuple or parenthesised)");
6986 0 : add_error (std::move (error));
6987 :
6988 0 : return nullptr;
6989 0 : }
6990 7754 : types.push_back (std::move (type));
6991 :
6992 7754 : t = lexer.peek_token ();
6993 7754 : if (t->get_id () != COMMA)
6994 : {
6995 189 : trailing_comma = false;
6996 : break;
6997 : }
6998 7565 : lexer.skip_token ();
6999 :
7000 7565 : t = lexer.peek_token ();
7001 : }
7002 :
7003 3467 : if (!skip_token (RIGHT_PAREN))
7004 : {
7005 0 : return nullptr;
7006 : }
7007 :
7008 : // if only one type and no trailing comma, then not a tuple type
7009 3467 : if (types.size () == 1 && !trailing_comma)
7010 : {
7011 : // must be a TraitObjectType (with more than one bound)
7012 224 : if (lexer.peek_token ()->get_id () == PLUS)
7013 : {
7014 : // error - this is not allowed for type no bounds
7015 0 : Error error (lexer.peek_token ()->get_locus (),
7016 : "plus (implying TraitObjectType as type param "
7017 : "bounds) is not allowed in type no bounds");
7018 0 : add_error (std::move (error));
7019 :
7020 0 : return nullptr;
7021 0 : }
7022 : else
7023 : {
7024 : // release vector pointer
7025 112 : std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
7026 : /* HACK: attempt to convert to trait bound. if fails, parenthesised
7027 : * type */
7028 112 : std::unique_ptr<AST::TraitBound> converted_bound (
7029 112 : released_ptr->to_trait_bound (true));
7030 112 : if (converted_bound == nullptr)
7031 : {
7032 : // parenthesised type
7033 112 : return std::unique_ptr<AST::ParenthesisedType> (
7034 112 : new AST::ParenthesisedType (std::move (released_ptr),
7035 112 : left_paren_locus));
7036 : }
7037 : else
7038 : {
7039 : // trait object type (one bound)
7040 :
7041 : // get value semantics trait bound
7042 0 : AST::TraitBound value_bound (*converted_bound);
7043 :
7044 0 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
7045 0 : new AST::TraitObjectTypeOneBound (value_bound,
7046 0 : left_paren_locus));
7047 0 : }
7048 112 : }
7049 : }
7050 : else
7051 : {
7052 3355 : return std::unique_ptr<AST::TupleType> (
7053 3355 : new AST::TupleType (std::move (types), left_paren_locus));
7054 : }
7055 : /* TODO: ensure that this ensures that dynamic dispatch for traits is not
7056 : * lost somehow */
7057 3467 : }
7058 :
7059 : // Parses tuple struct items if they exist. Does not parse parentheses.
7060 : template <typename ManagedTokenSource>
7061 : std::unique_ptr<AST::TupleStructItems>
7062 10203 : Parser<ManagedTokenSource>::parse_tuple_struct_items ()
7063 : {
7064 10203 : std::vector<std::unique_ptr<AST::Pattern>> lower_patterns;
7065 :
7066 : // DEBUG
7067 10203 : rust_debug ("started parsing tuple struct items");
7068 :
7069 : // check for '..' at front
7070 20406 : if (lexer.peek_token ()->get_id () == DOT_DOT)
7071 : {
7072 : // only parse upper patterns
7073 160 : lexer.skip_token ();
7074 :
7075 : // DEBUG
7076 160 : rust_debug ("'..' at front in tuple struct items detected");
7077 :
7078 160 : std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
7079 :
7080 160 : const_TokenPtr t = lexer.peek_token ();
7081 192 : while (t->get_id () == COMMA)
7082 : {
7083 32 : lexer.skip_token ();
7084 :
7085 : // break if right paren
7086 64 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
7087 : break;
7088 :
7089 : // parse pattern, which is now required
7090 32 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
7091 32 : if (pattern == nullptr)
7092 : {
7093 0 : Error error (lexer.peek_token ()->get_locus (),
7094 : "failed to parse pattern in tuple struct items");
7095 0 : add_error (std::move (error));
7096 :
7097 0 : return nullptr;
7098 0 : }
7099 32 : upper_patterns.push_back (std::move (pattern));
7100 :
7101 32 : t = lexer.peek_token ();
7102 : }
7103 :
7104 : // DEBUG
7105 160 : rust_debug (
7106 : "finished parsing tuple struct items ranged (upper/none only)");
7107 :
7108 160 : return std::unique_ptr<AST::TupleStructItemsHasRest> (
7109 160 : new AST::TupleStructItemsHasRest (std::move (lower_patterns),
7110 160 : std::move (upper_patterns)));
7111 160 : }
7112 :
7113 : // has at least some lower patterns
7114 10043 : const_TokenPtr t = lexer.peek_token ();
7115 10439 : while (t->get_id () != RIGHT_PAREN && t->get_id () != DOT_DOT)
7116 : {
7117 : // DEBUG
7118 10358 : rust_debug ("about to parse pattern in tuple struct items");
7119 :
7120 : // parse pattern, which is required
7121 10358 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
7122 10358 : if (pattern == nullptr)
7123 : {
7124 0 : Error error (t->get_locus (),
7125 : "failed to parse pattern in tuple struct items");
7126 0 : add_error (std::move (error));
7127 :
7128 0 : return nullptr;
7129 0 : }
7130 10358 : lower_patterns.push_back (std::move (pattern));
7131 :
7132 : // DEBUG
7133 10358 : rust_debug ("successfully parsed pattern in tuple struct items");
7134 :
7135 20716 : if (lexer.peek_token ()->get_id () != COMMA)
7136 : {
7137 : // DEBUG
7138 9962 : rust_debug ("broke out of parsing patterns in tuple struct "
7139 : "items as no comma");
7140 :
7141 : break;
7142 : }
7143 396 : lexer.skip_token ();
7144 396 : t = lexer.peek_token ();
7145 : }
7146 :
7147 : // branch on next token
7148 10043 : t = lexer.peek_token ();
7149 10043 : switch (t->get_id ())
7150 : {
7151 9990 : case RIGHT_PAREN:
7152 9990 : return std::unique_ptr<AST::TupleStructItemsNoRest> (
7153 9990 : new AST::TupleStructItemsNoRest (std::move (lower_patterns)));
7154 52 : case DOT_DOT:
7155 : {
7156 : // has an upper range that must be parsed separately
7157 52 : lexer.skip_token ();
7158 :
7159 52 : std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
7160 :
7161 52 : t = lexer.peek_token ();
7162 66 : while (t->get_id () == COMMA)
7163 : {
7164 14 : lexer.skip_token ();
7165 :
7166 : // break if next token is right paren
7167 28 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
7168 : break;
7169 :
7170 : // parse pattern, which is required
7171 14 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
7172 14 : if (pattern == nullptr)
7173 : {
7174 0 : Error error (lexer.peek_token ()->get_locus (),
7175 : "failed to parse pattern in tuple struct items");
7176 0 : add_error (std::move (error));
7177 :
7178 0 : return nullptr;
7179 0 : }
7180 14 : upper_patterns.push_back (std::move (pattern));
7181 :
7182 14 : t = lexer.peek_token ();
7183 : }
7184 :
7185 52 : return std::unique_ptr<AST::TupleStructItemsHasRest> (
7186 52 : new AST::TupleStructItemsHasRest (std::move (lower_patterns),
7187 52 : std::move (upper_patterns)));
7188 52 : }
7189 1 : default:
7190 : // error
7191 1 : add_error (Error (t->get_locus (),
7192 : "unexpected token %qs in tuple struct items",
7193 : t->get_token_description ()));
7194 :
7195 1 : return nullptr;
7196 : }
7197 10203 : }
7198 :
7199 : /* Parses a statement or expression (depending on whether a trailing semicolon
7200 : * exists). Useful for block expressions where it cannot be determined through
7201 : * lookahead whether it is a statement or expression to be parsed. */
7202 : template <typename ManagedTokenSource>
7203 : tl::expected<ExprOrStmt, Parse::Error::Node>
7204 436002 : Parser<ManagedTokenSource>::parse_stmt_or_expr ()
7205 : {
7206 : // quick exit for empty statement
7207 436002 : const_TokenPtr t = lexer.peek_token ();
7208 436002 : if (t->get_id () == SEMICOLON)
7209 : {
7210 18 : lexer.skip_token ();
7211 18 : std::unique_ptr<AST::EmptyStmt> stmt (
7212 18 : new AST::EmptyStmt (t->get_locus ()));
7213 36 : return ExprOrStmt (std::move (stmt));
7214 18 : }
7215 :
7216 : // parse outer attributes
7217 435984 : AST::AttrVec outer_attrs = parse_outer_attributes ();
7218 435984 : ParseRestrictions restrictions;
7219 435984 : restrictions.expr_can_be_stmt = true;
7220 :
7221 : // Defered child error checking: we need to check for a semicolon
7222 435984 : tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr> expr;
7223 :
7224 : // parsing this will be annoying because of the many different possibilities
7225 : /* best may be just to copy paste in parse_item switch, and failing that try
7226 : * to parse outer attributes, and then pass them in to either a let
7227 : * statement or (fallback) expression statement. */
7228 : // FIXME: think of a way to do this without such a large switch?
7229 :
7230 : /* FIXME: for expressions at least, the only way that they can really be
7231 : * parsed properly in this way is if they don't support operators on them.
7232 : * They must be pratt-parsed otherwise. As such due to composability, only
7233 : * explicit statements will have special cases here. This should roughly
7234 : * correspond to "expr-with-block", but this warning is here in case it
7235 : * isn't the case. */
7236 435984 : t = lexer.peek_token ();
7237 435984 : switch (t->get_id ())
7238 : {
7239 133710 : case LET:
7240 : {
7241 : // let statement
7242 133710 : std::unique_ptr<AST::LetStmt> stmt (
7243 133710 : parse_let_stmt (std::move (outer_attrs)));
7244 267420 : return ExprOrStmt (std::move (stmt));
7245 133710 : }
7246 500 : case CONST:
7247 1000 : if (lexer.peek_token (1)->get_id () == LEFT_CURLY)
7248 : {
7249 : // const block: parse as expression
7250 50 : expr = parse_expr (std::move (outer_attrs), restrictions);
7251 25 : break;
7252 : }
7253 : gcc_fallthrough ();
7254 : case PUB:
7255 : case MOD:
7256 : case EXTERN_KW:
7257 : case USE:
7258 : case FN_KW:
7259 : case TYPE:
7260 : case STRUCT_KW:
7261 : case ENUM_KW:
7262 : case STATIC_KW:
7263 : case AUTO:
7264 : case TRAIT:
7265 : case IMPL:
7266 : {
7267 6419 : std::unique_ptr<AST::VisItem> item (
7268 6419 : parse_vis_item (std::move (outer_attrs)));
7269 12838 : return ExprOrStmt (std::move (item));
7270 6419 : }
7271 : /* TODO: implement union keyword but not really because of
7272 : * context-dependence crappy hack way to parse a union written below to
7273 : * separate it from the good code. */
7274 : // case UNION:
7275 13727 : case UNSAFE:
7276 : { // maybe - unsafe traits are a thing
7277 : /* if any of these (should be all possible VisItem prefixes), parse a
7278 : * VisItem - can't parse item because would require reparsing outer
7279 : * attributes */
7280 13727 : const_TokenPtr t2 = lexer.peek_token (1);
7281 13727 : switch (t2->get_id ())
7282 : {
7283 13671 : case LEFT_CURLY:
7284 : {
7285 : // unsafe block: parse as expression
7286 27342 : expr = parse_expr (std::move (outer_attrs), restrictions);
7287 : break;
7288 : }
7289 0 : case AUTO:
7290 : case TRAIT:
7291 : {
7292 : // unsafe trait
7293 0 : std::unique_ptr<AST::VisItem> item (
7294 0 : parse_vis_item (std::move (outer_attrs)));
7295 0 : return ExprOrStmt (std::move (item));
7296 0 : }
7297 56 : case EXTERN_KW:
7298 : case FN_KW:
7299 : {
7300 : // unsafe function
7301 56 : std::unique_ptr<AST::VisItem> item (
7302 56 : parse_vis_item (std::move (outer_attrs)));
7303 112 : return ExprOrStmt (std::move (item));
7304 56 : }
7305 0 : case IMPL:
7306 : {
7307 : // unsafe trait impl
7308 0 : std::unique_ptr<AST::VisItem> item (
7309 0 : parse_vis_item (std::move (outer_attrs)));
7310 0 : return ExprOrStmt (std::move (item));
7311 0 : }
7312 0 : default:
7313 0 : add_error (Error (t2->get_locus (),
7314 : "unrecognised token %qs after parsing unsafe - "
7315 : "expected beginning of expression or statement",
7316 : t->get_token_description ()));
7317 :
7318 : // skip somewhere?
7319 : return tl::unexpected<Parse::Error::Node> (
7320 0 : Parse::Error::Node::MALFORMED);
7321 : }
7322 : break;
7323 292785 : }
7324 : /* FIXME: this is either a macro invocation or macro invocation semi.
7325 : * start parsing to determine which one it is. */
7326 : // FIXME: old code there
7327 :
7328 : // crappy hack to do union "keyword"
7329 180721 : case IDENTIFIER:
7330 180721 : if (t->get_str () == Values::WeakKeywords::UNION
7331 180757 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
7332 : {
7333 1 : std::unique_ptr<AST::VisItem> item (
7334 1 : parse_vis_item (std::move (outer_attrs)));
7335 2 : return ExprOrStmt (std::move (item));
7336 : // or should this go straight to parsing union?
7337 1 : }
7338 180720 : else if (t->get_str () == Values::WeakKeywords::MACRO_RULES
7339 186836 : && lexer.peek_token (1)->get_id () == EXCLAM)
7340 : {
7341 : // macro_rules! macro item
7342 6116 : std::unique_ptr<AST::Item> item (
7343 6116 : parse_macro_rules_def (std::move (outer_attrs)));
7344 12232 : return ExprOrStmt (std::move (item));
7345 6116 : }
7346 : gcc_fallthrough ();
7347 : case SUPER:
7348 : case SELF:
7349 : case SELF_ALIAS:
7350 : case CRATE:
7351 : case SCOPE_RESOLUTION:
7352 : case DOLLAR_SIGN:
7353 : {
7354 207927 : AST::PathInExpression path = parse_path_in_expression ();
7355 : tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
7356 207927 : null_denotation;
7357 :
7358 415854 : if (lexer.peek_token ()->get_id () == EXCLAM)
7359 : {
7360 24074 : std::unique_ptr<AST::MacroInvocation> invoc
7361 48148 : = parse_macro_invocation_partial (std::move (path),
7362 : std::move (outer_attrs));
7363 24074 : if (invoc == nullptr)
7364 : return tl::unexpected<Parse::Error::Node> (
7365 0 : Parse::Error::Node::CHILD_ERROR);
7366 :
7367 24074 : if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
7368 : {
7369 12150 : invoc->add_semicolon ();
7370 : // Macro invocation with semicolon.
7371 12150 : return ExprOrStmt (
7372 24300 : std::unique_ptr<AST::Stmt> (std::move (invoc)));
7373 : }
7374 :
7375 23848 : TokenId after_macro = lexer.peek_token ()->get_id ();
7376 :
7377 11924 : AST::DelimType delim_type = invoc->get_invoc_data ()
7378 11924 : .get_delim_tok_tree ()
7379 11924 : .get_delim_type ();
7380 :
7381 11924 : if (delim_type == AST::CURLY && after_macro != DOT
7382 20 : && after_macro != QUESTION_MARK)
7383 : {
7384 20 : rust_debug ("braced macro statement");
7385 20 : return ExprOrStmt (
7386 40 : std::unique_ptr<AST::Stmt> (std::move (invoc)));
7387 : }
7388 :
7389 11904 : null_denotation = std::move (invoc);
7390 24074 : }
7391 : else
7392 : {
7393 : null_denotation
7394 367705 : = null_denotation_path (std::move (path), {}, restrictions);
7395 : }
7396 :
7397 783022 : expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
7398 : std::move (outer_attrs), restrictions);
7399 : break;
7400 207927 : }
7401 68059 : default:
7402 : /* expression statement or expression itself - parse
7403 : * expression then make it statement if semi afterwards */
7404 136088 : expr = parse_expr (std::move (outer_attrs), restrictions);
7405 68059 : break;
7406 : }
7407 :
7408 277512 : const_TokenPtr after_expr = lexer.peek_token ();
7409 277512 : if (after_expr->get_id () == SEMICOLON)
7410 : {
7411 : // must be expression statement
7412 69372 : lexer.skip_token ();
7413 :
7414 69372 : if (expr)
7415 : {
7416 69371 : return ExprOrStmt (
7417 138742 : std::make_unique<AST::ExprStmt> (std::move (expr.value ()),
7418 208113 : t->get_locus (), true));
7419 : }
7420 : else
7421 : {
7422 : return tl::unexpected<Parse::Error::Node> (
7423 1 : Parse::Error::Node::CHILD_ERROR);
7424 : }
7425 : }
7426 :
7427 208140 : if (expr)
7428 : {
7429 : // block expression statement.
7430 208108 : if (!expr.value ()->is_expr_without_block ()
7431 208108 : && after_expr->get_id () != RIGHT_CURLY)
7432 14431 : return ExprOrStmt (
7433 28862 : std::make_unique<AST::ExprStmt> (std::move (expr.value ()),
7434 43293 : t->get_locus (), false));
7435 :
7436 : // Check if expr_without_block is properly terminated
7437 193677 : if (expr.value ()->is_expr_without_block ()
7438 193677 : && after_expr->get_id () != RIGHT_CURLY)
7439 : {
7440 : // expr_without_block must be followed by ';' or '}'
7441 1 : Error error (after_expr->get_locus (),
7442 : "expected %<;%> or %<}%> after expression, found %qs",
7443 : after_expr->get_token_description ());
7444 1 : add_error (std::move (error));
7445 : return tl::unexpected<Parse::Error::Node> (
7446 1 : Parse::Error::Node::MALFORMED);
7447 1 : }
7448 : }
7449 :
7450 : // return expression
7451 193708 : if (expr)
7452 387352 : return ExprOrStmt (std::move (expr.value ()));
7453 : else
7454 32 : return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::CHILD_ERROR);
7455 713496 : }
7456 :
7457 : } // namespace Rust
7458 :
7459 : #include "rust-parse-impl-utils.hxx"
7460 : #include "rust-parse-impl-attribute.hxx"
7461 : #include "rust-parse-impl-ttree.hxx"
7462 : #include "rust-parse-impl-macro.hxx"
7463 : #include "rust-parse-impl-path.hxx"
7464 : #include "rust-parse-impl-pattern.hxx"
7465 : #include "rust-parse-impl-expr.hxx"
|