1/* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22#define INCLUDE_UNIQUE_PTR
23#include "system.h"
24#include "coretypes.h"
25#include "cp-tree.h"
26#include "c-family/c-common.h"
27#include "timevar.h"
28#include "stringpool.h"
29#include "cgraph.h"
30#include "print-tree.h"
31#include "attribs.h"
32#include "trans-mem.h"
33#include "intl.h"
34#include "decl.h"
35#include "c-family/c-objc.h"
36#include "plugin.h"
37#include "tree-pretty-print.h"
38#include "parser.h"
39#include "gomp-constants.h"
40#include "omp-general.h"
41#include "omp-offload.h"
42#include "c-family/c-indentation.h"
43#include "context.h"
44#include "gcc-rich-location.h"
45#include "tree-iterator.h"
46#include "c-family/name-hint.h"
47
48
49/* The lexer. */
50
51/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
53
54static cp_token eof_token =
55{
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
57};
58
59/* The various kinds of non integral constant we encounter. */
60enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116};
117
118/* The various kinds of errors about name-lookup failing. */
119enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128};
129
130/* The various kinds of required token */
131enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
153
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
175
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184};
185
186/* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
188
189class type_id_in_expr_sentinel
190{
191 cp_parser *parser;
192 bool saved;
193public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
200};
201
202/* Prototypes. */
203
204static cp_lexer *cp_lexer_new_main
205 (void);
206static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208static void cp_lexer_destroy
209 (cp_lexer *);
210static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228static void cp_lexer_purge_token
229 (cp_lexer *);
230static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232static void cp_lexer_save_tokens
233 (cp_lexer *);
234static void cp_lexer_commit_tokens
235 (cp_lexer *);
236static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238static void cp_lexer_print_token
239 (FILE *, cp_token *);
240static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246
247static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
249
250static void cp_parser_initial_pragma
251 (cp_token *);
252
253static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
257
258/* Manifest constants. */
259#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260#define CP_SAVED_TOKEN_STACK 5
261
262/* Variables. */
263
264/* The stream to which debugging output should be written. */
265static FILE *cp_lexer_debug_stream;
266
267/* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269int cp_unevaluated_operand;
270
271/* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
276
277static void
278cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
281{
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
285
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
287
288 if (buffer == NULL)
289 return;
290
291 if (num == 0)
292 num = buffer->length ();
293
294 if (start_token == NULL)
295 start_token = buffer->address ();
296
297 if (start_token > buffer->address ())
298 {
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
301 }
302
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
306 {
307 if (token == start_token)
308 do_print = true;
309
310 if (!do_print)
311 continue;
312
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
316
317 cp_lexer_print_token (file, token);
318
319 if (token == curr_token)
320 fprintf (file, "]]");
321
322 switch (token->type)
323 {
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
330
331 default:
332 fputc (' ', file);
333 }
334 }
335
336 if (i == num && i < buffer->length ())
337 {
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
340 }
341
342 fprintf (file, "\n");
343}
344
345
346/* Dump all tokens in BUFFER to stderr. */
347
348void
349cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
350{
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
352}
353
354DEBUG_FUNCTION void
355debug (vec<cp_token, va_gc> &ref)
356{
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
358}
359
360DEBUG_FUNCTION void
361debug (vec<cp_token, va_gc> *ptr)
362{
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
367}
368
369
370/* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
372
373static void
374cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
375{
376 if (t)
377 {
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
380 }
381}
382
383
384/* Dump parser context C to FILE. */
385
386static void
387cp_debug_print_context (FILE *file, cp_parser_context *c)
388{
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
393}
394
395
396/* Print the stack of parsing contexts to FILE starting with FIRST. */
397
398static void
399cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
400{
401 unsigned i;
402 cp_parser_context *c;
403
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
406 {
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
409 }
410}
411
412
413/* Print the value of FLAG to FILE. DESC is a string describing the flag. */
414
415static void
416cp_debug_print_flag (FILE *file, const char *desc, bool flag)
417{
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
420}
421
422
423/* Print an unparsed function entry UF to FILE. */
424
425static void
426cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
427{
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
431
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
436 {
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
442 }
443
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
447 {
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
450 }
451 fprintf (file, "\n");
452
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
456 {
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
459 }
460 fprintf (file, "\n");
461}
462
463
464/* Print the stack of unparsed member functions S to FILE. */
465
466static void
467cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
469{
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
472
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
475 {
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
478 }
479}
480
481
482/* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
484
485static void
486cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
487{
488 cp_token *next_token, *first_token, *start_token;
489
490 if (file == NULL)
491 file = stderr;
492
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
500}
501
502
503/* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
505
506void
507cp_debug_parser (FILE *file, cp_parser *parser)
508{
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
512
513 if (file == NULL)
514 file = stderr;
515
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
587}
588
589DEBUG_FUNCTION void
590debug (cp_parser &ref)
591{
592 cp_debug_parser (stderr, &ref);
593}
594
595DEBUG_FUNCTION void
596debug (cp_parser *ptr)
597{
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
602}
603
604/* Allocate memory for a new lexer object and return it. */
605
606static cp_lexer *
607cp_lexer_alloc (void)
608{
609 cp_lexer *lexer;
610
611 c_common_no_more_pch ();
612
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
615
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
618
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623
624 return lexer;
625}
626
627
628/* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
630
631static cp_lexer *
632cp_lexer_new_main (void)
633{
634 cp_lexer *lexer;
635 cp_token token;
636
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
641
642 lexer = cp_lexer_alloc ();
643
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
646
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
649 {
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
652 }
653
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
660
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
664
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
667}
668
669/* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
671
672static cp_lexer *
673cp_lexer_new_from_tokens (cp_token_cache *cache)
674{
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
683
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
688
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
691}
692
693/* Frees all resources associated with LEXER. */
694
695static void
696cp_lexer_destroy (cp_lexer *lexer)
697{
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
701}
702
703/* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
707
708#define LEXER_DEBUGGING_ENABLED_P false
709
710/* Returns nonzero if debugging information should be output. */
711
712static inline bool
713cp_lexer_debugging_p (cp_lexer *lexer)
714{
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
717
718 return lexer->debugging_p;
719}
720
721
722static inline cp_token_position
723cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724{
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
726
727 return lexer->next_token - previous_p;
728}
729
730static inline cp_token *
731cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732{
733 return pos;
734}
735
736static inline void
737cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738{
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
740}
741
742static inline cp_token_position
743cp_lexer_previous_token_position (cp_lexer *lexer)
744{
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
749}
750
751static inline cp_token *
752cp_lexer_previous_token (cp_lexer *lexer)
753{
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
755
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
758 {
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
761 }
762
763 return cp_lexer_token_at (lexer, tp);
764}
765
766/* nonzero if we are presently saving tokens. */
767
768static inline int
769cp_lexer_saving_tokens (const cp_lexer* lexer)
770{
771 return lexer->saved_tokens.length () != 0;
772}
773
774/* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
778
779static void
780cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
781{
782 static int is_extern_c = 0;
783
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
791
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
798
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
801 {
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
803 {
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
808 }
809 else
810 {
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
814 {
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
820
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
824 }
825
826 token->keyword = RID_MAX;
827 }
828 }
829 else if (token->type == CPP_AT_NAME)
830 {
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
834 {
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
842 */
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
852 }
853 }
854}
855
856/* Update the globals input_location and the input file stack from TOKEN. */
857static inline void
858cp_lexer_set_source_position_from_token (cp_token *token)
859{
860 if (token->type != CPP_EOF)
861 {
862 input_location = token->location;
863 }
864}
865
866/* Update the globals input_location and the input file stack from LEXER. */
867static inline void
868cp_lexer_set_source_position (cp_lexer *lexer)
869{
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
872}
873
874/* Return a pointer to the next token in the token stream, but do not
875 consume it. */
876
877static inline cp_token *
878cp_lexer_peek_token (cp_lexer *lexer)
879{
880 if (cp_lexer_debugging_p (lexer))
881 {
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
885 }
886 return lexer->next_token;
887}
888
889/* Return true if the next token has the indicated TYPE. */
890
891static inline bool
892cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893{
894 return cp_lexer_peek_token (lexer)->type == type;
895}
896
897/* Return true if the next token does not have the indicated TYPE. */
898
899static inline bool
900cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901{
902 return !cp_lexer_next_token_is (lexer, type);
903}
904
905/* Return true if the next token is the indicated KEYWORD. */
906
907static inline bool
908cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909{
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
911}
912
913static inline bool
914cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915{
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
917}
918
919static inline bool
920cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921{
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
923}
924
925/* Return true if the next token is not the indicated KEYWORD. */
926
927static inline bool
928cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
929{
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
931}
932
933/* Return true if KEYWORD can start a decl-specifier. */
934
935bool
936cp_keyword_starts_decl_specifier_p (enum rid keyword)
937{
938 switch (keyword)
939 {
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
977
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
984 }
985}
986
987/* Return true if the next token is a keyword for a decl-specifier. */
988
989static bool
990cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
991{
992 cp_token *token;
993
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
996}
997
998/* Returns TRUE iff the token T begins a decltype type. */
999
1000static bool
1001token_is_decltype (cp_token *t)
1002{
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1005}
1006
1007/* Returns TRUE iff the next token begins a decltype type. */
1008
1009static bool
1010cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1011{
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1014}
1015
1016/* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1018
1019static tree
1020saved_checks_value (struct tree_check *check_value)
1021{
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1027 {
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1033 }
1034 /* Return the stored value. */
1035 return check_value->value;
1036}
1037
1038/* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1043
1044static cp_token *
1045cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1046{
1047 cp_token *token;
1048
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1051
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1055
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1060 {
1061 ++token;
1062 if (token == lexer->last_token)
1063 {
1064 token = &eof_token;
1065 break;
1066 }
1067
1068 if (!token->purged_p)
1069 --n;
1070 }
1071
1072 if (cp_lexer_debugging_p (lexer))
1073 {
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1076 }
1077
1078 return token;
1079}
1080
1081/* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1083
1084static cp_token *
1085cp_lexer_consume_token (cp_lexer* lexer)
1086{
1087 cp_token *token = lexer->next_token;
1088
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1091
1092 do
1093 {
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1096 {
1097 lexer->next_token = &eof_token;
1098 break;
1099 }
1100
1101 }
1102 while (lexer->next_token->purged_p);
1103
1104 cp_lexer_set_source_position_from_token (token);
1105
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1108 {
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1112 }
1113
1114 return token;
1115}
1116
1117/* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1120
1121static void
1122cp_lexer_purge_token (cp_lexer *lexer)
1123{
1124 cp_token *tok = lexer->next_token;
1125
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1131
1132 do
1133 {
1134 tok++;
1135 if (tok == lexer->last_token)
1136 {
1137 tok = &eof_token;
1138 break;
1139 }
1140 }
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1143}
1144
1145/* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1148
1149static void
1150cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1151{
1152 cp_token *peek = lexer->next_token;
1153
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1156
1157 gcc_assert (tok < peek);
1158
1159 for ( tok += 1; tok != peek; tok += 1)
1160 {
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1165 }
1166}
1167
1168/* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1170
1171static void
1172cp_lexer_save_tokens (cp_lexer* lexer)
1173{
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1177
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1179}
1180
1181/* Commit to the portion of the token stream most recently saved. */
1182
1183static void
1184cp_lexer_commit_tokens (cp_lexer* lexer)
1185{
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1189
1190 lexer->saved_tokens.pop ();
1191}
1192
1193/* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1195
1196static void
1197cp_lexer_rollback_tokens (cp_lexer* lexer)
1198{
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1202
1203 lexer->next_token = lexer->saved_tokens.pop ();
1204}
1205
1206/* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1210
1211struct saved_token_sentinel
1212{
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1217 {
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1220 }
1221 void rollback ()
1222 {
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1225 }
1226 ~saved_token_sentinel()
1227 {
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1231 }
1232};
1233
1234/* Print a representation of the TOKEN on the STREAM. */
1235
1236static void
1237cp_lexer_print_token (FILE * stream, cp_token *token)
1238{
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243#define OP(e, s) #e,
1244#define TK(e, s) #e,
1245 TTYPE_TABLE
1246#undef OP
1247#undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1252 };
1253
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1256 {
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1266
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1274
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1278
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1287 }
1288}
1289
1290DEBUG_FUNCTION void
1291debug (cp_token &ref)
1292{
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1295}
1296
1297DEBUG_FUNCTION void
1298debug (cp_token *ptr)
1299{
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1304}
1305
1306
1307/* Start emitting debugging information. */
1308
1309static void
1310cp_lexer_start_debugging (cp_lexer* lexer)
1311{
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1315
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1318}
1319
1320/* Stop emitting debugging information. */
1321
1322static void
1323cp_lexer_stop_debugging (cp_lexer* lexer)
1324{
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1328
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1331}
1332
1333/* Create a new cp_token_cache, representing a range of tokens. */
1334
1335static cp_token_cache *
1336cp_token_cache_new (cp_token *first, cp_token *last)
1337{
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1342}
1343
1344/* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1346
1347static inline void
1348cp_ensure_no_omp_declare_simd (cp_parser *parser)
1349{
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1351 {
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1355 }
1356}
1357
1358/* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1360
1361static inline void
1362cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1363{
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1365 {
1366 if (fndecl == error_mark_node)
1367 {
1368 parser->omp_declare_simd = NULL;
1369 return;
1370 }
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1372 {
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1375 }
1376 }
1377}
1378
1379/* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1381
1382static inline void
1383cp_ensure_no_oacc_routine (cp_parser *parser)
1384{
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1386 {
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1391 }
1392}
1393
1394/* Decl-specifiers. */
1395
1396/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1397
1398static void
1399clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1400{
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1402}
1403
1404/* Declarators. */
1405
1406/* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1410
1411static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1421
1422/* An erroneous declarator. */
1423static cp_declarator *cp_error_declarator;
1424
1425/* The obstack on which declarators and related data structures are
1426 allocated. */
1427static struct obstack declarator_obstack;
1428
1429/* Alloc BYTES from the declarator memory pool. */
1430
1431static inline void *
1432alloc_declarator (size_t bytes)
1433{
1434 return obstack_alloc (&declarator_obstack, bytes);
1435}
1436
1437/* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1439
1440static cp_declarator *
1441make_declarator (cp_declarator_kind kind)
1442{
1443 cp_declarator *declarator;
1444
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1453
1454 return declarator;
1455}
1456
1457/* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1462
1463static cp_declarator *
1464make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1466{
1467 cp_declarator *declarator;
1468
1469 /* It is valid to write:
1470
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1474
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1481
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1485
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1490
1491 return declarator;
1492}
1493
1494/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1498
1499cp_declarator *
1500make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1502{
1503 cp_declarator *declarator;
1504
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1510 {
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1514 }
1515 else
1516 declarator->parameter_pack_p = false;
1517
1518 declarator->std_attributes = attributes;
1519
1520 return declarator;
1521}
1522
1523/* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1526
1527cp_declarator *
1528make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1530{
1531 cp_declarator *declarator;
1532
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1538 {
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1542 }
1543 else
1544 declarator->parameter_pack_p = false;
1545
1546 declarator->std_attributes = attributes;
1547
1548 return declarator;
1549}
1550
1551/* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1554
1555cp_declarator *
1556make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1559{
1560 cp_declarator *declarator;
1561
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1566
1567 if (pointee)
1568 {
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1571 }
1572 else
1573 declarator->parameter_pack_p = false;
1574
1575 declarator->std_attributes = attributes;
1576
1577 return declarator;
1578}
1579
1580/* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1584
1585cp_declarator *
1586make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1595{
1596 cp_declarator *declarator;
1597
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1609 {
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1613 }
1614 else
1615 declarator->parameter_pack_p = false;
1616
1617 return declarator;
1618}
1619
1620/* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1622
1623cp_declarator *
1624make_array_declarator (cp_declarator *element, tree bounds)
1625{
1626 cp_declarator *declarator;
1627
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1632 {
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1636 }
1637 else
1638 declarator->parameter_pack_p = false;
1639
1640 return declarator;
1641}
1642
1643/* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645static bool
1646declarator_can_be_parameter_pack (cp_declarator *declarator)
1647{
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1651
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1658 {
1659 switch ((int)declarator->kind)
1660 {
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1666
1667 case cdk_error:
1668 return true;
1669
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1673 }
1674 }
1675
1676 return !found;
1677}
1678
1679cp_parameter_declarator *no_parameters;
1680
1681/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1683
1684cp_parameter_declarator *
1685make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1690{
1691 cp_parameter_declarator *parameter;
1692
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1704
1705 return parameter;
1706}
1707
1708/* Returns true iff DECLARATOR is a declaration for a function. */
1709
1710static bool
1711function_declarator_p (const cp_declarator *declarator)
1712{
1713 while (declarator)
1714 {
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1723 }
1724 return false;
1725}
1726
1727/* The parser. */
1728
1729/* Overview
1730 --------
1731
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1739
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1743
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1752
1753 Methodology
1754 -----------
1755
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1772
1773 Future Improvements
1774 -------------------
1775
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1781
1782/* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1784
1785enum
1786{
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1803};
1804
1805/* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807typedef int cp_parser_flags;
1808
1809/* The different kinds of declarators we want to parse. */
1810
1811enum cp_parser_declarator_kind
1812{
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1819};
1820
1821/* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1824
1825enum cp_parser_prec
1826{
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840};
1841
1842/* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1844
1845struct cp_parser_binary_operations_map_node
1846{
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1853};
1854
1855struct cp_parser_expression_stack_entry
1856{
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1869};
1870
1871/* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1876
1877/* Prototypes. */
1878
1879/* Constructors and destructors. */
1880
1881static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1883
1884/* Class variables. */
1885
1886static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1887
1888/* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1891
1892static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1895
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1899
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1902
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1905
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1910
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1913
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1915
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1917
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1919
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1921
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923};
1924
1925/* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1929
1930/* Constructors and destructors. */
1931
1932/* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1934
1935static cp_parser_context *
1936cp_parser_context_new (cp_parser_context* next)
1937{
1938 cp_parser_context *context;
1939
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1942 {
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1947 }
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1950
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1956 {
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1962 }
1963
1964 return context;
1965}
1966
1967/* Managing the unparsed function queues. */
1968
1969#define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971#define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973#define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975#define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1977
1978static void
1979push_unparsed_function_queues (cp_parser *parser)
1980{
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1983}
1984
1985static void
1986pop_unparsed_function_queues (cp_parser *parser)
1987{
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1990}
1991
1992/* Prototypes. */
1993
1994/* Constructors and destructors. */
1995
1996static cp_parser *cp_parser_new
1997 (void);
1998
1999/* Routines to parse various constructs.
2000
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2010
2011/* Lexical conventions [gram.lex] */
2012
2013static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019static tree cp_parser_userdef_string_literal
2020 (tree);
2021static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2023
2024/* Basic concepts [gram.basic] */
2025
2026static bool cp_parser_translation_unit
2027 (cp_parser *);
2028
2029/* Expressions [gram.expr] */
2030
2031static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2051/* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2052enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2053static void cp_parser_pseudo_destructor_name
2054 (cp_parser *, tree, tree *, tree *);
2055static cp_expr cp_parser_unary_expression
2056 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2057static enum tree_code cp_parser_unary_operator
2058 (cp_token *);
2059static tree cp_parser_new_expression
2060 (cp_parser *);
2061static vec<tree, va_gc> *cp_parser_new_placement
2062 (cp_parser *);
2063static tree cp_parser_new_type_id
2064 (cp_parser *, tree *);
2065static cp_declarator *cp_parser_new_declarator_opt
2066 (cp_parser *);
2067static cp_declarator *cp_parser_direct_new_declarator
2068 (cp_parser *);
2069static vec<tree, va_gc> *cp_parser_new_initializer
2070 (cp_parser *);
2071static tree cp_parser_delete_expression
2072 (cp_parser *);
2073static cp_expr cp_parser_cast_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075static cp_expr cp_parser_binary_expression
2076 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2077static tree cp_parser_question_colon_clause
2078 (cp_parser *, cp_expr);
2079static cp_expr cp_parser_assignment_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081static enum tree_code cp_parser_assignment_operator_opt
2082 (cp_parser *);
2083static cp_expr cp_parser_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085static cp_expr cp_parser_constant_expression
2086 (cp_parser *, bool = false, bool * = NULL, bool = false);
2087static cp_expr cp_parser_builtin_offsetof
2088 (cp_parser *);
2089static cp_expr cp_parser_lambda_expression
2090 (cp_parser *);
2091static void cp_parser_lambda_introducer
2092 (cp_parser *, tree);
2093static bool cp_parser_lambda_declarator_opt
2094 (cp_parser *, tree);
2095static void cp_parser_lambda_body
2096 (cp_parser *, tree);
2097
2098/* Statements [gram.stmt.stmt] */
2099
2100static void cp_parser_statement
2101 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2102static void cp_parser_label_for_labeled_statement
2103(cp_parser *, tree);
2104static tree cp_parser_expression_statement
2105 (cp_parser *, tree);
2106static tree cp_parser_compound_statement
2107 (cp_parser *, tree, int, bool);
2108static void cp_parser_statement_seq_opt
2109 (cp_parser *, tree);
2110static tree cp_parser_selection_statement
2111 (cp_parser *, bool *, vec<tree> *);
2112static tree cp_parser_condition
2113 (cp_parser *);
2114static tree cp_parser_iteration_statement
2115 (cp_parser *, bool *, bool);
2116static bool cp_parser_init_statement
2117 (cp_parser *, tree *decl);
2118static tree cp_parser_for
2119 (cp_parser *, bool);
2120static tree cp_parser_c_for
2121 (cp_parser *, tree, tree, bool);
2122static tree cp_parser_range_for
2123 (cp_parser *, tree, tree, tree, bool);
2124static void do_range_for_auto_deduction
2125 (tree, tree);
2126static tree cp_parser_perform_range_for_lookup
2127 (tree, tree *, tree *);
2128static tree cp_parser_range_for_member_function
2129 (tree, tree);
2130static tree cp_parser_jump_statement
2131 (cp_parser *);
2132static void cp_parser_declaration_statement
2133 (cp_parser *);
2134
2135static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2137static void cp_parser_already_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &);
2139
2140/* Declarations [gram.dcl.dcl] */
2141
2142static void cp_parser_declaration_seq_opt
2143 (cp_parser *);
2144static void cp_parser_declaration
2145 (cp_parser *);
2146static void cp_parser_block_declaration
2147 (cp_parser *, bool);
2148static void cp_parser_simple_declaration
2149 (cp_parser *, bool, tree *);
2150static void cp_parser_decl_specifier_seq
2151 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2152static tree cp_parser_storage_class_specifier_opt
2153 (cp_parser *);
2154static tree cp_parser_function_specifier_opt
2155 (cp_parser *, cp_decl_specifier_seq *);
2156static tree cp_parser_type_specifier
2157 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2158 int *, bool *);
2159static tree cp_parser_simple_type_specifier
2160 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2161static tree cp_parser_type_name
2162 (cp_parser *, bool);
2163static tree cp_parser_type_name
2164 (cp_parser *);
2165static tree cp_parser_nonclass_name
2166 (cp_parser* parser);
2167static tree cp_parser_elaborated_type_specifier
2168 (cp_parser *, bool, bool);
2169static tree cp_parser_enum_specifier
2170 (cp_parser *);
2171static void cp_parser_enumerator_list
2172 (cp_parser *, tree);
2173static void cp_parser_enumerator_definition
2174 (cp_parser *, tree);
2175static tree cp_parser_namespace_name
2176 (cp_parser *);
2177static void cp_parser_namespace_definition
2178 (cp_parser *);
2179static void cp_parser_namespace_body
2180 (cp_parser *);
2181static tree cp_parser_qualified_namespace_specifier
2182 (cp_parser *);
2183static void cp_parser_namespace_alias_definition
2184 (cp_parser *);
2185static bool cp_parser_using_declaration
2186 (cp_parser *, bool);
2187static void cp_parser_using_directive
2188 (cp_parser *);
2189static tree cp_parser_alias_declaration
2190 (cp_parser *);
2191static void cp_parser_asm_definition
2192 (cp_parser *);
2193static void cp_parser_linkage_specification
2194 (cp_parser *);
2195static void cp_parser_static_assert
2196 (cp_parser *, bool);
2197static tree cp_parser_decltype
2198 (cp_parser *);
2199static tree cp_parser_decomposition_declaration
2200 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2201
2202/* Declarators [gram.dcl.decl] */
2203
2204static tree cp_parser_init_declarator
2205 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2206 bool, bool, int, bool *, tree *, location_t *, tree *);
2207static cp_declarator *cp_parser_declarator
2208 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2209static cp_declarator *cp_parser_direct_declarator
2210 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2211static enum tree_code cp_parser_ptr_operator
2212 (cp_parser *, tree *, cp_cv_quals *, tree *);
2213static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2214 (cp_parser *);
2215static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2216 (cp_parser *);
2217static cp_ref_qualifier cp_parser_ref_qualifier_opt
2218 (cp_parser *);
2219static tree cp_parser_tx_qualifier_opt
2220 (cp_parser *);
2221static tree cp_parser_late_return_type_opt
2222 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2223static tree cp_parser_declarator_id
2224 (cp_parser *, bool);
2225static tree cp_parser_type_id
2226 (cp_parser *);
2227static tree cp_parser_template_type_arg
2228 (cp_parser *);
2229static tree cp_parser_trailing_type_id (cp_parser *);
2230static tree cp_parser_type_id_1
2231 (cp_parser *, bool, bool);
2232static void cp_parser_type_specifier_seq
2233 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2234static tree cp_parser_parameter_declaration_clause
2235 (cp_parser *);
2236static tree cp_parser_parameter_declaration_list
2237 (cp_parser *, bool *);
2238static cp_parameter_declarator *cp_parser_parameter_declaration
2239 (cp_parser *, bool, bool *);
2240static tree cp_parser_default_argument
2241 (cp_parser *, bool);
2242static void cp_parser_function_body
2243 (cp_parser *, bool);
2244static tree cp_parser_initializer
2245 (cp_parser *, bool *, bool *);
2246static cp_expr cp_parser_initializer_clause
2247 (cp_parser *, bool *);
2248static cp_expr cp_parser_braced_list
2249 (cp_parser*, bool*);
2250static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2251 (cp_parser *, bool *);
2252
2253static void cp_parser_ctor_initializer_opt_and_function_body
2254 (cp_parser *, bool);
2255
2256static tree cp_parser_late_parsing_omp_declare_simd
2257 (cp_parser *, tree);
2258
2259static tree cp_parser_late_parsing_oacc_routine
2260 (cp_parser *, tree);
2261
2262static tree synthesize_implicit_template_parm
2263 (cp_parser *, tree);
2264static tree finish_fully_implicit_template
2265 (cp_parser *, tree);
2266
2267/* Classes [gram.class] */
2268
2269static tree cp_parser_class_name
2270 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2271static tree cp_parser_class_specifier
2272 (cp_parser *);
2273static tree cp_parser_class_head
2274 (cp_parser *, bool *);
2275static enum tag_types cp_parser_class_key
2276 (cp_parser *);
2277static void cp_parser_type_parameter_key
2278 (cp_parser* parser);
2279static void cp_parser_member_specification_opt
2280 (cp_parser *);
2281static void cp_parser_member_declaration
2282 (cp_parser *);
2283static tree cp_parser_pure_specifier
2284 (cp_parser *);
2285static tree cp_parser_constant_initializer
2286 (cp_parser *);
2287
2288/* Derived classes [gram.class.derived] */
2289
2290static tree cp_parser_base_clause
2291 (cp_parser *);
2292static tree cp_parser_base_specifier
2293 (cp_parser *);
2294
2295/* Special member functions [gram.special] */
2296
2297static tree cp_parser_conversion_function_id
2298 (cp_parser *);
2299static tree cp_parser_conversion_type_id
2300 (cp_parser *);
2301static cp_declarator *cp_parser_conversion_declarator_opt
2302 (cp_parser *);
2303static void cp_parser_ctor_initializer_opt
2304 (cp_parser *);
2305static void cp_parser_mem_initializer_list
2306 (cp_parser *);
2307static tree cp_parser_mem_initializer
2308 (cp_parser *);
2309static tree cp_parser_mem_initializer_id
2310 (cp_parser *);
2311
2312/* Overloading [gram.over] */
2313
2314static cp_expr cp_parser_operator_function_id
2315 (cp_parser *);
2316static cp_expr cp_parser_operator
2317 (cp_parser *);
2318
2319/* Templates [gram.temp] */
2320
2321static void cp_parser_template_declaration
2322 (cp_parser *, bool);
2323static tree cp_parser_template_parameter_list
2324 (cp_parser *);
2325static tree cp_parser_template_parameter
2326 (cp_parser *, bool *, bool *);
2327static tree cp_parser_type_parameter
2328 (cp_parser *, bool *);
2329static tree cp_parser_template_id
2330 (cp_parser *, bool, bool, enum tag_types, bool);
2331static tree cp_parser_template_name
2332 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2333static tree cp_parser_template_argument_list
2334 (cp_parser *);
2335static tree cp_parser_template_argument
2336 (cp_parser *);
2337static void cp_parser_explicit_instantiation
2338 (cp_parser *);
2339static void cp_parser_explicit_specialization
2340 (cp_parser *);
2341
2342/* Exception handling [gram.exception] */
2343
2344static tree cp_parser_try_block
2345 (cp_parser *);
2346static void cp_parser_function_try_block
2347 (cp_parser *);
2348static void cp_parser_handler_seq
2349 (cp_parser *);
2350static void cp_parser_handler
2351 (cp_parser *);
2352static tree cp_parser_exception_declaration
2353 (cp_parser *);
2354static tree cp_parser_throw_expression
2355 (cp_parser *);
2356static tree cp_parser_exception_specification_opt
2357 (cp_parser *);
2358static tree cp_parser_type_id_list
2359 (cp_parser *);
2360
2361/* GNU Extensions */
2362
2363static tree cp_parser_asm_specification_opt
2364 (cp_parser *);
2365static tree cp_parser_asm_operand_list
2366 (cp_parser *);
2367static tree cp_parser_asm_clobber_list
2368 (cp_parser *);
2369static tree cp_parser_asm_label_list
2370 (cp_parser *);
2371static bool cp_next_tokens_can_be_attribute_p
2372 (cp_parser *);
2373static bool cp_next_tokens_can_be_gnu_attribute_p
2374 (cp_parser *);
2375static bool cp_next_tokens_can_be_std_attribute_p
2376 (cp_parser *);
2377static bool cp_nth_tokens_can_be_std_attribute_p
2378 (cp_parser *, size_t);
2379static bool cp_nth_tokens_can_be_gnu_attribute_p
2380 (cp_parser *, size_t);
2381static bool cp_nth_tokens_can_be_attribute_p
2382 (cp_parser *, size_t);
2383static tree cp_parser_attributes_opt
2384 (cp_parser *);
2385static tree cp_parser_gnu_attributes_opt
2386 (cp_parser *);
2387static tree cp_parser_gnu_attribute_list
2388 (cp_parser *);
2389static tree cp_parser_std_attribute
2390 (cp_parser *, tree);
2391static tree cp_parser_std_attribute_spec
2392 (cp_parser *);
2393static tree cp_parser_std_attribute_spec_seq
2394 (cp_parser *);
2395static bool cp_parser_extension_opt
2396 (cp_parser *, int *);
2397static void cp_parser_label_declaration
2398 (cp_parser *);
2399
2400/* Concept Extensions */
2401
2402static tree cp_parser_requires_clause
2403 (cp_parser *);
2404static tree cp_parser_requires_clause_opt
2405 (cp_parser *);
2406static tree cp_parser_requires_expression
2407 (cp_parser *);
2408static tree cp_parser_requirement_parameter_list
2409 (cp_parser *);
2410static tree cp_parser_requirement_body
2411 (cp_parser *);
2412static tree cp_parser_requirement_list
2413 (cp_parser *);
2414static tree cp_parser_requirement
2415 (cp_parser *);
2416static tree cp_parser_simple_requirement
2417 (cp_parser *);
2418static tree cp_parser_compound_requirement
2419 (cp_parser *);
2420static tree cp_parser_type_requirement
2421 (cp_parser *);
2422static tree cp_parser_nested_requirement
2423 (cp_parser *);
2424
2425/* Transactional Memory Extensions */
2426
2427static tree cp_parser_transaction
2428 (cp_parser *, cp_token *);
2429static tree cp_parser_transaction_expression
2430 (cp_parser *, enum rid);
2431static void cp_parser_function_transaction
2432 (cp_parser *, enum rid);
2433static tree cp_parser_transaction_cancel
2434 (cp_parser *);
2435
2436enum pragma_context {
2437 pragma_external,
2438 pragma_member,
2439 pragma_objc_icode,
2440 pragma_stmt,
2441 pragma_compound
2442};
2443static bool cp_parser_pragma
2444 (cp_parser *, enum pragma_context, bool *);
2445
2446/* Objective-C++ Productions */
2447
2448static tree cp_parser_objc_message_receiver
2449 (cp_parser *);
2450static tree cp_parser_objc_message_args
2451 (cp_parser *);
2452static tree cp_parser_objc_message_expression
2453 (cp_parser *);
2454static cp_expr cp_parser_objc_encode_expression
2455 (cp_parser *);
2456static tree cp_parser_objc_defs_expression
2457 (cp_parser *);
2458static tree cp_parser_objc_protocol_expression
2459 (cp_parser *);
2460static tree cp_parser_objc_selector_expression
2461 (cp_parser *);
2462static cp_expr cp_parser_objc_expression
2463 (cp_parser *);
2464static bool cp_parser_objc_selector_p
2465 (enum cpp_ttype);
2466static tree cp_parser_objc_selector
2467 (cp_parser *);
2468static tree cp_parser_objc_protocol_refs_opt
2469 (cp_parser *);
2470static void cp_parser_objc_declaration
2471 (cp_parser *, tree);
2472static tree cp_parser_objc_statement
2473 (cp_parser *);
2474static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser *, tree *);
2476static void cp_parser_objc_at_property_declaration
2477 (cp_parser *) ;
2478static void cp_parser_objc_at_synthesize_declaration
2479 (cp_parser *) ;
2480static void cp_parser_objc_at_dynamic_declaration
2481 (cp_parser *) ;
2482static tree cp_parser_objc_struct_declaration
2483 (cp_parser *) ;
2484
2485/* Utility Routines */
2486
2487static cp_expr cp_parser_lookup_name
2488 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2489static tree cp_parser_lookup_name_simple
2490 (cp_parser *, tree, location_t);
2491static tree cp_parser_maybe_treat_template_as_class
2492 (tree, bool);
2493static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser *, cp_declarator *, location_t);
2495static bool cp_parser_check_template_parameters
2496 (cp_parser *, unsigned, location_t, cp_declarator *);
2497static cp_expr cp_parser_simple_cast_expression
2498 (cp_parser *);
2499static tree cp_parser_global_scope_opt
2500 (cp_parser *, bool);
2501static bool cp_parser_constructor_declarator_p
2502 (cp_parser *, bool);
2503static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2505static tree cp_parser_function_definition_after_declarator
2506 (cp_parser *, bool);
2507static bool cp_parser_template_declaration_after_export
2508 (cp_parser *, bool);
2509static void cp_parser_perform_template_parameter_access_checks
2510 (vec<deferred_access_check, va_gc> *);
2511static tree cp_parser_single_declaration
2512 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2513static cp_expr cp_parser_functional_cast
2514 (cp_parser *, tree);
2515static tree cp_parser_save_member_function_body
2516 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2517static tree cp_parser_save_nsdmi
2518 (cp_parser *);
2519static tree cp_parser_enclosed_template_argument_list
2520 (cp_parser *);
2521static void cp_parser_save_default_args
2522 (cp_parser *, tree);
2523static void cp_parser_late_parsing_for_member
2524 (cp_parser *, tree);
2525static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser *, tree, tree, tree);
2527static void cp_parser_late_parsing_nsdmi
2528 (cp_parser *, tree);
2529static void cp_parser_late_parsing_default_args
2530 (cp_parser *, tree);
2531static tree cp_parser_sizeof_operand
2532 (cp_parser *, enum rid);
2533static cp_expr cp_parser_trait_expr
2534 (cp_parser *, enum rid);
2535static bool cp_parser_declares_only_class_p
2536 (cp_parser *);
2537static void cp_parser_set_storage_class
2538 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2539static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2541static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq *decl_specs,
2543 cp_decl_spec ds, cp_token *);
2544static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq *);
2546static void cp_parser_required_error
2547 (cp_parser *, required_token, bool, location_t);
2548static cp_token *cp_parser_require
2549 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2550static cp_token *cp_parser_require_keyword
2551 (cp_parser *, enum rid, required_token);
2552static bool cp_parser_token_starts_function_definition_p
2553 (cp_token *);
2554static bool cp_parser_next_token_starts_class_definition_p
2555 (cp_parser *);
2556static bool cp_parser_next_token_ends_template_argument_p
2557 (cp_parser *);
2558static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser *, size_t);
2560static enum tag_types cp_parser_token_is_class_key
2561 (cp_token *);
2562static enum tag_types cp_parser_token_is_type_parameter_key
2563 (cp_token *);
2564static void cp_parser_check_class_key
2565 (enum tag_types, tree type);
2566static void cp_parser_check_access_in_redeclaration
2567 (tree type, location_t location);
2568static bool cp_parser_optional_template_keyword
2569 (cp_parser *);
2570static void cp_parser_pre_parsed_nested_name_specifier
2571 (cp_parser *);
2572static bool cp_parser_cache_group
2573 (cp_parser *, enum cpp_ttype, unsigned);
2574static tree cp_parser_cache_defarg
2575 (cp_parser *parser, bool nsdmi);
2576static void cp_parser_parse_tentatively
2577 (cp_parser *);
2578static void cp_parser_commit_to_tentative_parse
2579 (cp_parser *);
2580static void cp_parser_commit_to_topmost_tentative_parse
2581 (cp_parser *);
2582static void cp_parser_abort_tentative_parse
2583 (cp_parser *);
2584static bool cp_parser_parse_definitely
2585 (cp_parser *);
2586static inline bool cp_parser_parsing_tentatively
2587 (cp_parser *);
2588static bool cp_parser_uncommitted_to_tentative_parse_p
2589 (cp_parser *);
2590static void cp_parser_error
2591 (cp_parser *, const char *);
2592static void cp_parser_name_lookup_error
2593 (cp_parser *, tree, tree, name_lookup_error, location_t);
2594static bool cp_parser_simulate_error
2595 (cp_parser *);
2596static bool cp_parser_check_type_definition
2597 (cp_parser *);
2598static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator *, tree, location_t type_location);
2600static void cp_parser_check_for_invalid_template_id
2601 (cp_parser *, tree, enum tag_types, location_t location);
2602static bool cp_parser_non_integral_constant_expression
2603 (cp_parser *, non_integral_constant);
2604static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser *, tree, location_t);
2606static bool cp_parser_parse_and_diagnose_invalid_type_name
2607 (cp_parser *);
2608static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser *, bool, bool, bool);
2610static void cp_parser_skip_to_end_of_statement
2611 (cp_parser *);
2612static void cp_parser_consume_semicolon_at_end_of_statement
2613 (cp_parser *);
2614static void cp_parser_skip_to_end_of_block_or_statement
2615 (cp_parser *);
2616static bool cp_parser_skip_to_closing_brace
2617 (cp_parser *);
2618static void cp_parser_skip_to_end_of_template_parameter_list
2619 (cp_parser *);
2620static void cp_parser_skip_to_pragma_eol
2621 (cp_parser*, cp_token *);
2622static bool cp_parser_error_occurred
2623 (cp_parser *);
2624static bool cp_parser_allow_gnu_extensions_p
2625 (cp_parser *);
2626static bool cp_parser_is_pure_string_literal
2627 (cp_token *);
2628static bool cp_parser_is_string_literal
2629 (cp_token *);
2630static bool cp_parser_is_keyword
2631 (cp_token *, enum rid);
2632static tree cp_parser_make_typename_type
2633 (cp_parser *, tree, location_t location);
2634static cp_declarator * cp_parser_make_indirect_declarator
2635 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2636static bool cp_parser_compound_literal_p
2637 (cp_parser *);
2638static bool cp_parser_array_designator_p
2639 (cp_parser *);
2640static bool cp_parser_init_statement_p
2641 (cp_parser *);
2642static bool cp_parser_skip_to_closing_square_bracket
2643 (cp_parser *);
2644
2645/* Concept-related syntactic transformations */
2646
2647static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2648static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2649
2650// -------------------------------------------------------------------------- //
2651// Unevaluated Operand Guard
2652//
2653// Implementation of an RAII helper for unevaluated operand parsing.
2654cp_unevaluated::cp_unevaluated ()
2655{
2656 ++cp_unevaluated_operand;
2657 ++c_inhibit_evaluation_warnings;
2658}
2659
2660cp_unevaluated::~cp_unevaluated ()
2661{
2662 --c_inhibit_evaluation_warnings;
2663 --cp_unevaluated_operand;
2664}
2665
2666// -------------------------------------------------------------------------- //
2667// Tentative Parsing
2668
2669/* Returns nonzero if we are parsing tentatively. */
2670
2671static inline bool
2672cp_parser_parsing_tentatively (cp_parser* parser)
2673{
2674 return parser->context->next != NULL;
2675}
2676
2677/* Returns nonzero if TOKEN is a string literal. */
2678
2679static bool
2680cp_parser_is_pure_string_literal (cp_token* token)
2681{
2682 return (token->type == CPP_STRING ||
2683 token->type == CPP_STRING16 ||
2684 token->type == CPP_STRING32 ||
2685 token->type == CPP_WSTRING ||
2686 token->type == CPP_UTF8STRING);
2687}
2688
2689/* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2691
2692static bool
2693cp_parser_is_string_literal (cp_token* token)
2694{
2695 return (cp_parser_is_pure_string_literal (token) ||
2696 token->type == CPP_STRING_USERDEF ||
2697 token->type == CPP_STRING16_USERDEF ||
2698 token->type == CPP_STRING32_USERDEF ||
2699 token->type == CPP_WSTRING_USERDEF ||
2700 token->type == CPP_UTF8STRING_USERDEF);
2701}
2702
2703/* Returns nonzero if TOKEN is the indicated KEYWORD. */
2704
2705static bool
2706cp_parser_is_keyword (cp_token* token, enum rid keyword)
2707{
2708 return token->keyword == keyword;
2709}
2710
2711/* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2712 PRAGMA_NONE. */
2713
2714static enum pragma_kind
2715cp_parser_pragma_kind (cp_token *token)
2716{
2717 if (token->type != CPP_PRAGMA)
2718 return PRAGMA_NONE;
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2721}
2722
2723/* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2732 of the marker. */
2733
2734static bool
2735cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2736 location_t *out_loc)
2737{
2738 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2739 if (token2->type != tok1_kind)
2740 return false;
2741 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2742 if (token3->type != tok1_kind)
2743 return false;
2744 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2745 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2746 return false;
2747
2748 /* It must be at the start of the line. */
2749 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2750 if (LOCATION_COLUMN (start_loc) != 1)
2751 return false;
2752
2753 /* We have a conflict marker. Construct a location of the form:
2754 <<<<<<<
2755 ^~~~~~~
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc = get_finish (token4->location);
2758 *out_loc = make_location (start_loc, start_loc, finish_loc);
2759
2760 return true;
2761}
2762
2763/* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2764 RT_CLOSE_PAREN. */
2765
2766static const char *
2767get_matching_symbol (required_token token_desc)
2768{
2769 switch (token_desc)
2770 {
2771 default:
2772 gcc_unreachable ();
2773 return "";
2774 case RT_CLOSE_BRACE:
2775 return "{";
2776 case RT_CLOSE_PAREN:
2777 return "(";
2778 }
2779}
2780
2781/* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2783
2784static enum cpp_ttype
2785get_required_cpp_ttype (required_token token_desc)
2786{
2787 switch (token_desc)
2788 {
2789 case RT_SEMICOLON:
2790 return CPP_SEMICOLON;
2791 case RT_OPEN_PAREN:
2792 return CPP_OPEN_PAREN;
2793 case RT_CLOSE_BRACE:
2794 return CPP_CLOSE_BRACE;
2795 case RT_OPEN_BRACE:
2796 return CPP_OPEN_BRACE;
2797 case RT_CLOSE_SQUARE:
2798 return CPP_CLOSE_SQUARE;
2799 case RT_OPEN_SQUARE:
2800 return CPP_OPEN_SQUARE;
2801 case RT_COMMA:
2802 return CPP_COMMA;
2803 case RT_COLON:
2804 return CPP_COLON;
2805 case RT_CLOSE_PAREN:
2806 return CPP_CLOSE_PAREN;
2807
2808 default:
2809 /* Use CPP_EOF as a "no completions possible" code. */
2810 return CPP_EOF;
2811 }
2812}
2813
2814
2815/* Subroutine of cp_parser_error and cp_parser_required_error.
2816
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2821 OTHER-TOKEN".
2822
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2825
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2828
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2831 location. */
2832
2833static void
2834cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2835 required_token missing_token_desc,
2836 location_t matching_location)
2837{
2838 cp_token *token = cp_lexer_peek_token (parser->lexer);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token);
2842
2843 if (token->type == CPP_PRAGMA)
2844 {
2845 error_at (token->location,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser, token);
2848 return;
2849 }
2850
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token->type == CPP_LSHIFT
2853 || token->type == CPP_RSHIFT
2854 || token->type == CPP_EQ_EQ)
2855 {
2856 location_t loc;
2857 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2858 {
2859 error_at (loc, "version control conflict marker in file");
2860 return;
2861 }
2862 }
2863
2864 gcc_rich_location richloc (input_location);
2865
2866 bool added_matching_location = false;
2867
2868 if (missing_token_desc != RT_NONE)
2869 {
2870 /* Potentially supply a fix-it hint, suggesting to add the
2871 missing token immediately after the *previous* token.
2872 This may move the primary location within richloc. */
2873 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2874 location_t prev_token_loc
2875 = cp_lexer_previous_token (parser->lexer)->location;
2876 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2877
2878 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2879 Attempt to consolidate diagnostics by printing it as a
2880 secondary range within the main diagnostic. */
2881 if (matching_location != UNKNOWN_LOCATION)
2882 added_matching_location
2883 = richloc.add_location_if_nearby (matching_location);
2884 }
2885
2886 /* Actually emit the error. */
2887 c_parse_error (gmsgid,
2888 /* Because c_parser_error does not understand
2889 CPP_KEYWORD, keywords are treated like
2890 identifiers. */
2891 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2892 token->u.value, token->flags, &richloc);
2893
2894 if (missing_token_desc != RT_NONE)
2895 {
2896 /* If we weren't able to consolidate matching_location, then
2897 print it as a secondary diagnostic. */
2898 if (matching_location != UNKNOWN_LOCATION
2899 && !added_matching_location)
2900 inform (matching_location, "to match this %qs",
2901 get_matching_symbol (missing_token_desc));
2902 }
2903}
2904
2905/* If not parsing tentatively, issue a diagnostic of the form
2906 FILE:LINE: MESSAGE before TOKEN
2907 where TOKEN is the next token in the input stream. MESSAGE
2908 (specified by the caller) is usually of the form "expected
2909 OTHER-TOKEN". */
2910
2911static void
2912cp_parser_error (cp_parser* parser, const char* gmsgid)
2913{
2914 if (!cp_parser_simulate_error (parser))
2915 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2916}
2917
2918/* Issue an error about name-lookup failing. NAME is the
2919 IDENTIFIER_NODE DECL is the result of
2920 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2921 the thing that we hoped to find. */
2922
2923static void
2924cp_parser_name_lookup_error (cp_parser* parser,
2925 tree name,
2926 tree decl,
2927 name_lookup_error desired,
2928 location_t location)
2929{
2930 /* If name lookup completely failed, tell the user that NAME was not
2931 declared. */
2932 if (decl == error_mark_node)
2933 {
2934 if (parser->scope && parser->scope != global_namespace)
2935 error_at (location, "%<%E::%E%> has not been declared",
2936 parser->scope, name);
2937 else if (parser->scope == global_namespace)
2938 error_at (location, "%<::%E%> has not been declared", name);
2939 else if (parser->object_scope
2940 && !CLASS_TYPE_P (parser->object_scope))
2941 error_at (location, "request for member %qE in non-class type %qT",
2942 name, parser->object_scope);
2943 else if (parser->object_scope)
2944 error_at (location, "%<%T::%E%> has not been declared",
2945 parser->object_scope, name);
2946 else
2947 error_at (location, "%qE has not been declared", name);
2948 }
2949 else if (parser->scope && parser->scope != global_namespace)
2950 {
2951 switch (desired)
2952 {
2953 case NLE_TYPE:
2954 error_at (location, "%<%E::%E%> is not a type",
2955 parser->scope, name);
2956 break;
2957 case NLE_CXX98:
2958 error_at (location, "%<%E::%E%> is not a class or namespace",
2959 parser->scope, name);
2960 break;
2961 case NLE_NOT_CXX98:
2962 error_at (location,
2963 "%<%E::%E%> is not a class, namespace, or enumeration",
2964 parser->scope, name);
2965 break;
2966 default:
2967 gcc_unreachable ();
2968
2969 }
2970 }
2971 else if (parser->scope == global_namespace)
2972 {
2973 switch (desired)
2974 {
2975 case NLE_TYPE:
2976 error_at (location, "%<::%E%> is not a type", name);
2977 break;
2978 case NLE_CXX98:
2979 error_at (location, "%<::%E%> is not a class or namespace", name);
2980 break;
2981 case NLE_NOT_CXX98:
2982 error_at (location,
2983 "%<::%E%> is not a class, namespace, or enumeration",
2984 name);
2985 break;
2986 default:
2987 gcc_unreachable ();
2988 }
2989 }
2990 else
2991 {
2992 switch (desired)
2993 {
2994 case NLE_TYPE:
2995 error_at (location, "%qE is not a type", name);
2996 break;
2997 case NLE_CXX98:
2998 error_at (location, "%qE is not a class or namespace", name);
2999 break;
3000 case NLE_NOT_CXX98:
3001 error_at (location,
3002 "%qE is not a class, namespace, or enumeration", name);
3003 break;
3004 default:
3005 gcc_unreachable ();
3006 }
3007 }
3008}
3009
3010/* If we are parsing tentatively, remember that an error has occurred
3011 during this tentative parse. Returns true if the error was
3012 simulated; false if a message should be issued by the caller. */
3013
3014static bool
3015cp_parser_simulate_error (cp_parser* parser)
3016{
3017 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3018 {
3019 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3020 return true;
3021 }
3022 return false;
3023}
3024
3025/* This function is called when a type is defined. If type
3026 definitions are forbidden at this point, an error message is
3027 issued. */
3028
3029static bool
3030cp_parser_check_type_definition (cp_parser* parser)
3031{
3032 /* If types are forbidden here, issue a message. */
3033 if (parser->type_definition_forbidden_message)
3034 {
3035 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3036 in the message need to be interpreted. */
3037 error (parser->type_definition_forbidden_message);
3038 return false;
3039 }
3040 return true;
3041}
3042
3043/* This function is called when the DECLARATOR is processed. The TYPE
3044 was a type defined in the decl-specifiers. If it is invalid to
3045 define a type in the decl-specifiers for DECLARATOR, an error is
3046 issued. TYPE_LOCATION is the location of TYPE and is used
3047 for error reporting. */
3048
3049static void
3050cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3051 tree type, location_t type_location)
3052{
3053 /* [dcl.fct] forbids type definitions in return types.
3054 Unfortunately, it's not easy to know whether or not we are
3055 processing a return type until after the fact. */
3056 while (declarator
3057 && (declarator->kind == cdk_pointer
3058 || declarator->kind == cdk_reference
3059 || declarator->kind == cdk_ptrmem))
3060 declarator = declarator->declarator;
3061 if (declarator
3062 && declarator->kind == cdk_function)
3063 {
3064 error_at (type_location,
3065 "new types may not be defined in a return type");
3066 inform (type_location,
3067 "(perhaps a semicolon is missing after the definition of %qT)",
3068 type);
3069 }
3070}
3071
3072/* A type-specifier (TYPE) has been parsed which cannot be followed by
3073 "<" in any valid C++ program. If the next token is indeed "<",
3074 issue a message warning the user about what appears to be an
3075 invalid attempt to form a template-id. LOCATION is the location
3076 of the type-specifier (TYPE) */
3077
3078static void
3079cp_parser_check_for_invalid_template_id (cp_parser* parser,
3080 tree type,
3081 enum tag_types tag_type,
3082 location_t location)
3083{
3084 cp_token_position start = 0;
3085
3086 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3087 {
3088 if (TREE_CODE (type) == TYPE_DECL)
3089 type = TREE_TYPE (type);
3090 if (TYPE_P (type) && !template_placeholder_p (type))
3091 error_at (location, "%qT is not a template", type);
3092 else if (identifier_p (type))
3093 {
3094 if (tag_type != none_type)
3095 error_at (location, "%qE is not a class template", type);
3096 else
3097 error_at (location, "%qE is not a template", type);
3098 }
3099 else
3100 error_at (location, "invalid template-id");
3101 /* Remember the location of the invalid "<". */
3102 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3103 start = cp_lexer_token_position (parser->lexer, true);
3104 /* Consume the "<". */
3105 cp_lexer_consume_token (parser->lexer);
3106 /* Parse the template arguments. */
3107 cp_parser_enclosed_template_argument_list (parser);
3108 /* Permanently remove the invalid template arguments so that
3109 this error message is not issued again. */
3110 if (start)
3111 cp_lexer_purge_tokens_after (parser->lexer, start);
3112 }
3113}
3114
3115/* If parsing an integral constant-expression, issue an error message
3116 about the fact that THING appeared and return true. Otherwise,
3117 return false. In either case, set
3118 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3119
3120static bool
3121cp_parser_non_integral_constant_expression (cp_parser *parser,
3122 non_integral_constant thing)
3123{
3124 parser->non_integral_constant_expression_p = true;
3125 if (parser->integral_constant_expression_p)
3126 {
3127 if (!parser->allow_non_integral_constant_expression_p)
3128 {
3129 const char *msg = NULL;
3130 switch (thing)
3131 {
3132 case NIC_FLOAT:
3133 pedwarn (input_location, OPT_Wpedantic,
3134 "ISO C++ forbids using a floating-point literal "
3135 "in a constant-expression");
3136 return true;
3137 case NIC_CAST:
3138 error ("a cast to a type other than an integral or "
3139 "enumeration type cannot appear in a "
3140 "constant-expression");
3141 return true;
3142 case NIC_TYPEID:
3143 error ("%<typeid%> operator "
3144 "cannot appear in a constant-expression");
3145 return true;
3146 case NIC_NCC:
3147 error ("non-constant compound literals "
3148 "cannot appear in a constant-expression");
3149 return true;
3150 case NIC_FUNC_CALL:
3151 error ("a function call "
3152 "cannot appear in a constant-expression");
3153 return true;
3154 case NIC_INC:
3155 error ("an increment "
3156 "cannot appear in a constant-expression");
3157 return true;
3158 case NIC_DEC:
3159 error ("an decrement "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_ARRAY_REF:
3163 error ("an array reference "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_ADDR_LABEL:
3167 error ("the address of a label "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_OVERLOADED:
3171 error ("calls to overloaded operators "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_ASSIGNMENT:
3175 error ("an assignment cannot appear in a constant-expression");
3176 return true;
3177 case NIC_COMMA:
3178 error ("a comma operator "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_CONSTRUCTOR:
3182 error ("a call to a constructor "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_TRANSACTION:
3186 error ("a transaction expression "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_THIS:
3190 msg = "this";
3191 break;
3192 case NIC_FUNC_NAME:
3193 msg = "__FUNCTION__";
3194 break;
3195 case NIC_PRETTY_FUNC:
3196 msg = "__PRETTY_FUNCTION__";
3197 break;
3198 case NIC_C99_FUNC:
3199 msg = "__func__";
3200 break;
3201 case NIC_VA_ARG:
3202 msg = "va_arg";
3203 break;
3204 case NIC_ARROW:
3205 msg = "->";
3206 break;
3207 case NIC_POINT:
3208 msg = ".";
3209 break;
3210 case NIC_STAR:
3211 msg = "*";
3212 break;
3213 case NIC_ADDR:
3214 msg = "&";
3215 break;
3216 case NIC_PREINCREMENT:
3217 msg = "++";
3218 break;
3219 case NIC_PREDECREMENT:
3220 msg = "--";
3221 break;
3222 case NIC_NEW:
3223 msg = "new";
3224 break;
3225 case NIC_DEL:
3226 msg = "delete";
3227 break;
3228 default:
3229 gcc_unreachable ();
3230 }
3231 if (msg)
3232 error ("%qs cannot appear in a constant-expression", msg);
3233 return true;
3234 }
3235 }
3236 return false;
3237}
3238
3239/* Emit a diagnostic for an invalid type name. This function commits
3240 to the current active tentative parse, if any. (Otherwise, the
3241 problematic construct might be encountered again later, resulting
3242 in duplicate error messages.) LOCATION is the location of ID. */
3243
3244static void
3245cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3246 location_t location)
3247{
3248 tree decl, ambiguous_decls;
3249 cp_parser_commit_to_tentative_parse (parser);
3250 /* Try to lookup the identifier. */
3251 decl = cp_parser_lookup_name (parser, id, none_type,
3252 /*is_template=*/false,
3253 /*is_namespace=*/false,
3254 /*check_dependency=*/true,
3255 &ambiguous_decls, location);
3256 if (ambiguous_decls)
3257 /* If the lookup was ambiguous, an error will already have
3258 been issued. */
3259 return;
3260 /* If the lookup found a template-name, it means that the user forgot
3261 to specify an argument list. Emit a useful error message. */
3262 if (DECL_TYPE_TEMPLATE_P (decl))
3263 {
3264 error_at (location,
3265 "invalid use of template-name %qE without an argument list",
3266 decl);
3267 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3268 inform (location, "class template argument deduction is only available "
3269 "with -std=c++17 or -std=gnu++17");
3270 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3271 }
3272 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3273 error_at (location, "invalid use of destructor %qD as a type", id);
3274 else if (TREE_CODE (decl) == TYPE_DECL)
3275 /* Something like 'unsigned A a;' */
3276 error_at (location, "invalid combination of multiple type-specifiers");
3277 else if (!parser->scope)
3278 {
3279 /* Issue an error message. */
3280 name_hint hint;
3281 if (TREE_CODE (id) == IDENTIFIER_NODE)
3282 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3283 if (hint)
3284 {
3285 gcc_rich_location richloc (location);
3286 richloc.add_fixit_replace (hint.suggestion ());
3287 error_at (&richloc,
3288 "%qE does not name a type; did you mean %qs?",
3289 id, hint.suggestion ());
3290 }
3291 else
3292 error_at (location, "%qE does not name a type", id);
3293 /* If we're in a template class, it's possible that the user was
3294 referring to a type from a base class. For example:
3295
3296 template <typename T> struct A { typedef T X; };
3297 template <typename T> struct B : public A<T> { X x; };
3298
3299 The user should have said "typename A<T>::X". */
3300 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3301 inform (location, "C++11 %<constexpr%> only available with "
3302 "-std=c++11 or -std=gnu++11");
3303 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3304 inform (location, "C++11 %<noexcept%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect < cxx11
3307 && TREE_CODE (id) == IDENTIFIER_NODE
3308 && id_equal (id, "thread_local"))
3309 inform (location, "C++11 %<thread_local%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3312 inform (location, "%<concept%> only available with -fconcepts");
3313 else if (processing_template_decl && current_class_type
3314 && TYPE_BINFO (current_class_type))
3315 {
3316 tree b;
3317
3318 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3319 b;
3320 b = TREE_CHAIN (b))
3321 {
3322 tree base_type = BINFO_TYPE (b);
3323 if (CLASS_TYPE_P (base_type)
3324 && dependent_type_p (base_type))
3325 {
3326 tree field;
3327 /* Go from a particular instantiation of the
3328 template (which will have an empty TYPE_FIELDs),
3329 to the main version. */
3330 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3331 for (field = TYPE_FIELDS (base_type);
3332 field;
3333 field = DECL_CHAIN (field))
3334 if (TREE_CODE (field) == TYPE_DECL
3335 && DECL_NAME (field) == id)
3336 {
3337 inform (location,
3338 "(perhaps %<typename %T::%E%> was intended)",
3339 BINFO_TYPE (b), id);
3340 break;
3341 }
3342 if (field)
3343 break;
3344 }
3345 }
3346 }
3347 }
3348 /* Here we diagnose qualified-ids where the scope is actually correct,
3349 but the identifier does not resolve to a valid type name. */
3350 else if (parser->scope != error_mark_node)
3351 {
3352 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3353 {
3354 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3355 error_at (location_of (id),
3356 "%qE in namespace %qE does not name a template type",
3357 id, parser->scope);
3358 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3359 error_at (location_of (id),
3360 "%qE in namespace %qE does not name a template type",
3361 TREE_OPERAND (id, 0), parser->scope);
3362 else
3363 error_at (location_of (id),
3364 "%qE in namespace %qE does not name a type",
3365 id, parser->scope);
3366 if (DECL_P (decl))
3367 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3368 else if (decl == error_mark_node)
3369 suggest_alternative_in_explicit_scope (location, id,
3370 parser->scope);
3371 }
3372 else if (CLASS_TYPE_P (parser->scope)
3373 && constructor_name_p (id, parser->scope))
3374 {
3375 /* A<T>::A<T>() */
3376 error_at (location, "%<%T::%E%> names the constructor, not"
3377 " the type", parser->scope, id);
3378 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3379 error_at (location, "and %qT has no template constructors",
3380 parser->scope);
3381 }
3382 else if (TYPE_P (parser->scope)
3383 && dependent_scope_p (parser->scope))
3384 {
3385 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3386 error_at (location,
3387 "need %<typename%> before %<%T::%D::%E%> because "
3388 "%<%T::%D%> is a dependent scope",
3389 TYPE_CONTEXT (parser->scope),
3390 TYPENAME_TYPE_FULLNAME (parser->scope),
3391 id,
3392 TYPE_CONTEXT (parser->scope),
3393 TYPENAME_TYPE_FULLNAME (parser->scope));
3394 else
3395 error_at (location, "need %<typename%> before %<%T::%E%> because "
3396 "%qT is a dependent scope",
3397 parser->scope, id, parser->scope);
3398 }
3399 else if (TYPE_P (parser->scope))
3400 {
3401 if (!COMPLETE_TYPE_P (parser->scope))
3402 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3403 parser->scope);
3404 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3405 error_at (location_of (id),
3406 "%qE in %q#T does not name a template type",
3407 id, parser->scope);
3408 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3409 error_at (location_of (id),
3410 "%qE in %q#T does not name a template type",
3411 TREE_OPERAND (id, 0), parser->scope);
3412 else
3413 error_at (location_of (id),
3414 "%qE in %q#T does not name a type",
3415 id, parser->scope);
3416 if (DECL_P (decl))
3417 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3418 }
3419 else
3420 gcc_unreachable ();
3421 }
3422}
3423
3424/* Check for a common situation where a type-name should be present,
3425 but is not, and issue a sensible error message. Returns true if an
3426 invalid type-name was detected.
3427
3428 The situation handled by this function are variable declarations of the
3429 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3430 Usually, `ID' should name a type, but if we got here it means that it
3431 does not. We try to emit the best possible error message depending on
3432 how exactly the id-expression looks like. */
3433
3434static bool
3435cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3436{
3437 tree id;
3438 cp_token *token = cp_lexer_peek_token (parser->lexer);
3439
3440 /* Avoid duplicate error about ambiguous lookup. */
3441 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3442 {
3443 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3444 if (next->type == CPP_NAME && next->error_reported)
3445 goto out;
3446 }
3447
3448 cp_parser_parse_tentatively (parser);
3449 id = cp_parser_id_expression (parser,
3450 /*template_keyword_p=*/false,
3451 /*check_dependency_p=*/true,
3452 /*template_p=*/NULL,
3453 /*declarator_p=*/true,
3454 /*optional_p=*/false);
3455 /* If the next token is a (, this is a function with no explicit return
3456 type, i.e. constructor, destructor or conversion op. */
3457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3458 || TREE_CODE (id) == TYPE_DECL)
3459 {
3460 cp_parser_abort_tentative_parse (parser);
3461 return false;
3462 }
3463 if (!cp_parser_parse_definitely (parser))
3464 return false;
3465
3466 /* Emit a diagnostic for the invalid type. */
3467 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3468 out:
3469 /* If we aren't in the middle of a declarator (i.e. in a
3470 parameter-declaration-clause), skip to the end of the declaration;
3471 there's no point in trying to process it. */
3472 if (!parser->in_declarator_p)
3473 cp_parser_skip_to_end_of_block_or_statement (parser);
3474 return true;
3475}
3476
3477/* Consume tokens up to, and including, the next non-nested closing `)'.
3478 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3479 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3480 found an unnested token of that type. */
3481
3482static int
3483cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3484 bool recovering,
3485 cpp_ttype or_ttype,
3486 bool consume_paren)
3487{
3488 unsigned paren_depth = 0;
3489 unsigned brace_depth = 0;
3490 unsigned square_depth = 0;
3491
3492 if (recovering && or_ttype == CPP_EOF
3493 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3494 return 0;
3495
3496 while (true)
3497 {
3498 cp_token * token = cp_lexer_peek_token (parser->lexer);
3499
3500 /* Have we found what we're looking for before the closing paren? */
3501 if (token->type == or_ttype && or_ttype != CPP_EOF
3502 && !brace_depth && !paren_depth && !square_depth)
3503 return -1;
3504
3505 switch (token->type)
3506 {
3507 case CPP_EOF:
3508 case CPP_PRAGMA_EOL:
3509 /* If we've run out of tokens, then there is no closing `)'. */
3510 return 0;
3511
3512 /* This is good for lambda expression capture-lists. */
3513 case CPP_OPEN_SQUARE:
3514 ++square_depth;
3515 break;
3516 case CPP_CLOSE_SQUARE:
3517 if (!square_depth--)
3518 return 0;
3519 break;
3520
3521 case CPP_SEMICOLON:
3522 /* This matches the processing in skip_to_end_of_statement. */
3523 if (!brace_depth)
3524 return 0;
3525 break;
3526
3527 case CPP_OPEN_BRACE:
3528 ++brace_depth;
3529 break;
3530 case CPP_CLOSE_BRACE:
3531 if (!brace_depth--)
3532 return 0;
3533 break;
3534
3535 case CPP_OPEN_PAREN:
3536 if (!brace_depth)
3537 ++paren_depth;
3538 break;
3539
3540 case CPP_CLOSE_PAREN:
3541 if (!brace_depth && !paren_depth--)
3542 {
3543 if (consume_paren)
3544 cp_lexer_consume_token (parser->lexer);
3545 return 1;
3546 }
3547 break;
3548
3549 default:
3550 break;
3551 }
3552
3553 /* Consume the token. */
3554 cp_lexer_consume_token (parser->lexer);
3555 }
3556}
3557
3558/* Consume tokens up to, and including, the next non-nested closing `)'.
3559 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3560 are doing error recovery. Returns -1 if OR_COMMA is true and we
3561 found an unnested token of that type. */
3562
3563static int
3564cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3565 bool recovering,
3566 bool or_comma,
3567 bool consume_paren)
3568{
3569 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3570 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3571 ttype, consume_paren);
3572}
3573
3574/* Consume tokens until we reach the end of the current statement.
3575 Normally, that will be just before consuming a `;'. However, if a
3576 non-nested `}' comes first, then we stop before consuming that. */
3577
3578static void
3579cp_parser_skip_to_end_of_statement (cp_parser* parser)
3580{
3581 unsigned nesting_depth = 0;
3582
3583 /* Unwind generic function template scope if necessary. */
3584 if (parser->fully_implicit_function_template_p)
3585 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3586
3587 while (true)
3588 {
3589 cp_token *token = cp_lexer_peek_token (parser->lexer);
3590
3591 switch (token->type)
3592 {
3593 case CPP_EOF:
3594 case CPP_PRAGMA_EOL:
3595 /* If we've run out of tokens, stop. */
3596 return;
3597
3598 case CPP_SEMICOLON:
3599 /* If the next token is a `;', we have reached the end of the
3600 statement. */
3601 if (!nesting_depth)
3602 return;
3603 break;
3604
3605 case CPP_CLOSE_BRACE:
3606 /* If this is a non-nested '}', stop before consuming it.
3607 That way, when confronted with something like:
3608
3609 { 3 + }
3610
3611 we stop before consuming the closing '}', even though we
3612 have not yet reached a `;'. */
3613 if (nesting_depth == 0)
3614 return;
3615
3616 /* If it is the closing '}' for a block that we have
3617 scanned, stop -- but only after consuming the token.
3618 That way given:
3619
3620 void f g () { ... }
3621 typedef int I;
3622
3623 we will stop after the body of the erroneously declared
3624 function, but before consuming the following `typedef'
3625 declaration. */
3626 if (--nesting_depth == 0)
3627 {
3628 cp_lexer_consume_token (parser->lexer);
3629 return;
3630 }
3631 break;
3632
3633 case CPP_OPEN_BRACE:
3634 ++nesting_depth;
3635 break;
3636
3637 default:
3638 break;
3639 }
3640
3641 /* Consume the token. */
3642 cp_lexer_consume_token (parser->lexer);
3643 }
3644}
3645
3646/* This function is called at the end of a statement or declaration.
3647 If the next token is a semicolon, it is consumed; otherwise, error
3648 recovery is attempted. */
3649
3650static void
3651cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3652{
3653 /* Look for the trailing `;'. */
3654 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3655 {
3656 /* If there is additional (erroneous) input, skip to the end of
3657 the statement. */
3658 cp_parser_skip_to_end_of_statement (parser);
3659 /* If the next token is now a `;', consume it. */
3660 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3661 cp_lexer_consume_token (parser->lexer);
3662 }
3663}
3664
3665/* Skip tokens until we have consumed an entire block, or until we
3666 have consumed a non-nested `;'. */
3667
3668static void
3669cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3670{
3671 int nesting_depth = 0;
3672
3673 /* Unwind generic function template scope if necessary. */
3674 if (parser->fully_implicit_function_template_p)
3675 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3676
3677 while (nesting_depth >= 0)
3678 {
3679 cp_token *token = cp_lexer_peek_token (parser->lexer);
3680
3681 switch (token->type)
3682 {
3683 case CPP_EOF:
3684 case CPP_PRAGMA_EOL:
3685 /* If we've run out of tokens, stop. */
3686 return;
3687
3688 case CPP_SEMICOLON:
3689 /* Stop if this is an unnested ';'. */
3690 if (!nesting_depth)
3691 nesting_depth = -1;
3692 break;
3693
3694 case CPP_CLOSE_BRACE:
3695 /* Stop if this is an unnested '}', or closes the outermost
3696 nesting level. */
3697 nesting_depth--;
3698 if (nesting_depth < 0)
3699 return;
3700 if (!nesting_depth)
3701 nesting_depth = -1;
3702 break;
3703
3704 case CPP_OPEN_BRACE:
3705 /* Nest. */
3706 nesting_depth++;
3707 break;
3708
3709 default:
3710 break;
3711 }
3712
3713 /* Consume the token. */
3714 cp_lexer_consume_token (parser->lexer);
3715 }
3716}
3717
3718/* Skip tokens until a non-nested closing curly brace is the next
3719 token, or there are no more tokens. Return true in the first case,
3720 false otherwise. */
3721
3722static bool
3723cp_parser_skip_to_closing_brace (cp_parser *parser)
3724{
3725 unsigned nesting_depth = 0;
3726
3727 while (true)
3728 {
3729 cp_token *token = cp_lexer_peek_token (parser->lexer);
3730
3731 switch (token->type)
3732 {
3733 case CPP_EOF:
3734 case CPP_PRAGMA_EOL:
3735 /* If we've run out of tokens, stop. */
3736 return false;
3737
3738 case CPP_CLOSE_BRACE:
3739 /* If the next token is a non-nested `}', then we have reached
3740 the end of the current block. */
3741 if (nesting_depth-- == 0)
3742 return true;
3743 break;
3744
3745 case CPP_OPEN_BRACE:
3746 /* If it the next token is a `{', then we are entering a new
3747 block. Consume the entire block. */
3748 ++nesting_depth;
3749 break;
3750
3751 default:
3752 break;
3753 }
3754
3755 /* Consume the token. */
3756 cp_lexer_consume_token (parser->lexer);
3757 }
3758}
3759
3760/* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3761 parameter is the PRAGMA token, allowing us to purge the entire pragma
3762 sequence. */
3763
3764static void
3765cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3766{
3767 cp_token *token;
3768
3769 parser->lexer->in_pragma = false;
3770
3771 do
3772 token = cp_lexer_consume_token (parser->lexer);
3773 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3774
3775 /* Ensure that the pragma is not parsed again. */
3776 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3777}
3778
3779/* Require pragma end of line, resyncing with it as necessary. The
3780 arguments are as for cp_parser_skip_to_pragma_eol. */
3781
3782static void
3783cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3784{
3785 parser->lexer->in_pragma = false;
3786 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3787 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3788}
3789
3790/* This is a simple wrapper around make_typename_type. When the id is
3791 an unresolved identifier node, we can provide a superior diagnostic
3792 using cp_parser_diagnose_invalid_type_name. */
3793
3794static tree
3795cp_parser_make_typename_type (cp_parser *parser, tree id,
3796 location_t id_location)
3797{
3798 tree result;
3799 if (identifier_p (id))
3800 {
3801 result = make_typename_type (parser->scope, id, typename_type,
3802 /*complain=*/tf_none);
3803 if (result == error_mark_node)
3804 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3805 return result;
3806 }
3807 return make_typename_type (parser->scope, id, typename_type, tf_error);
3808}
3809
3810/* This is a wrapper around the
3811 make_{pointer,ptrmem,reference}_declarator functions that decides
3812 which one to call based on the CODE and CLASS_TYPE arguments. The
3813 CODE argument should be one of the values returned by
3814 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3815 appertain to the pointer or reference. */
3816
3817static cp_declarator *
3818cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3819 cp_cv_quals cv_qualifiers,
3820 cp_declarator *target,
3821 tree attributes)
3822{
3823 if (code == ERROR_MARK)
3824 return cp_error_declarator;
3825
3826 if (code == INDIRECT_REF)
3827 if (class_type == NULL_TREE)
3828 return make_pointer_declarator (cv_qualifiers, target, attributes);
3829 else
3830 return make_ptrmem_declarator (cv_qualifiers, class_type,
3831 target, attributes);
3832 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3833 return make_reference_declarator (cv_qualifiers, target,
3834 false, attributes);
3835 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3836 return make_reference_declarator (cv_qualifiers, target,
3837 true, attributes);
3838 gcc_unreachable ();
3839}
3840
3841/* Create a new C++ parser. */
3842
3843static cp_parser *
3844cp_parser_new (void)
3845{
3846 cp_parser *parser;
3847 cp_lexer *lexer;
3848 unsigned i;
3849
3850 /* cp_lexer_new_main is called before doing GC allocation because
3851 cp_lexer_new_main might load a PCH file. */
3852 lexer = cp_lexer_new_main ();
3853
3854 /* Initialize the binops_by_token so that we can get the tree
3855 directly from the token. */
3856 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3857 binops_by_token[binops[i].token_type] = binops[i];
3858
3859 parser = ggc_cleared_alloc<cp_parser> ();
3860 parser->lexer = lexer;
3861 parser->context = cp_parser_context_new (NULL);
3862
3863 /* For now, we always accept GNU extensions. */
3864 parser->allow_gnu_extensions_p = 1;
3865
3866 /* The `>' token is a greater-than operator, not the end of a
3867 template-id. */
3868 parser->greater_than_is_operator_p = true;
3869
3870 parser->default_arg_ok_p = true;
3871
3872 /* We are not parsing a constant-expression. */
3873 parser->integral_constant_expression_p = false;
3874 parser->allow_non_integral_constant_expression_p = false;
3875 parser->non_integral_constant_expression_p = false;
3876
3877 /* Local variable names are not forbidden. */
3878 parser->local_variables_forbidden_p = false;
3879
3880 /* We are not processing an `extern "C"' declaration. */
3881 parser->in_unbraced_linkage_specification_p = false;
3882
3883 /* We are not processing a declarator. */
3884 parser->in_declarator_p = false;
3885
3886 /* We are not processing a template-argument-list. */
3887 parser->in_template_argument_list_p = false;
3888
3889 /* We are not in an iteration statement. */
3890 parser->in_statement = 0;
3891
3892 /* We are not in a switch statement. */
3893 parser->in_switch_statement_p = false;
3894
3895 /* We are not parsing a type-id inside an expression. */
3896 parser->in_type_id_in_expr_p = false;
3897
3898 /* Declarations aren't implicitly extern "C". */
3899 parser->implicit_extern_c = false;
3900
3901 /* String literals should be translated to the execution character set. */
3902 parser->translate_strings_p = true;
3903
3904 /* We are not parsing a function body. */
3905 parser->in_function_body = false;
3906
3907 /* We can correct until told otherwise. */
3908 parser->colon_corrects_to_scope_p = true;
3909
3910 /* The unparsed function queue is empty. */
3911 push_unparsed_function_queues (parser);
3912
3913 /* There are no classes being defined. */
3914 parser->num_classes_being_defined = 0;
3915
3916 /* No template parameters apply. */
3917 parser->num_template_parameter_lists = 0;
3918
3919 /* Special parsing data structures. */
3920 parser->omp_declare_simd = NULL;
3921 parser->oacc_routine = NULL;
3922
3923 /* Not declaring an implicit function template. */
3924 parser->auto_is_implicit_function_template_parm_p = false;
3925 parser->fully_implicit_function_template_p = false;
3926 parser->implicit_template_parms = 0;
3927 parser->implicit_template_scope = 0;
3928
3929 /* Allow constrained-type-specifiers. */
3930 parser->prevent_constrained_type_specifiers = 0;
3931
3932 /* We haven't yet seen an 'extern "C"'. */
3933 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3934
3935 return parser;
3936}
3937
3938/* Create a cp_lexer structure which will emit the tokens in CACHE
3939 and push it onto the parser's lexer stack. This is used for delayed
3940 parsing of in-class method bodies and default arguments, and should
3941 not be confused with tentative parsing. */
3942static void
3943cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3944{
3945 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3946 lexer->next = parser->lexer;
3947 parser->lexer = lexer;
3948
3949 /* Move the current source position to that of the first token in the
3950 new lexer. */
3951 cp_lexer_set_source_position_from_token (lexer->next_token);
3952}
3953
3954/* Pop the top lexer off the parser stack. This is never used for the
3955 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3956static void
3957cp_parser_pop_lexer (cp_parser *parser)
3958{
3959 cp_lexer *lexer = parser->lexer;
3960 parser->lexer = lexer->next;
3961 cp_lexer_destroy (lexer);
3962
3963 /* Put the current source position back where it was before this
3964 lexer was pushed. */
3965 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3966}
3967
3968/* Lexical conventions [gram.lex] */
3969
3970/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3971 identifier. */
3972
3973static cp_expr
3974cp_parser_identifier (cp_parser* parser)
3975{
3976 cp_token *token;
3977
3978 /* Look for the identifier. */
3979 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3980 /* Return the value. */
3981 if (token)
3982 return cp_expr (token->u.value, token->location);
3983 else
3984 return error_mark_node;
3985}
3986
3987/* Parse a sequence of adjacent string constants. Returns a
3988 TREE_STRING representing the combined, nul-terminated string
3989 constant. If TRANSLATE is true, translate the string to the
3990 execution character set. If WIDE_OK is true, a wide string is
3991 invalid here.
3992
3993 C++98 [lex.string] says that if a narrow string literal token is
3994 adjacent to a wide string literal token, the behavior is undefined.
3995 However, C99 6.4.5p4 says that this results in a wide string literal.
3996 We follow C99 here, for consistency with the C front end.
3997
3998 This code is largely lifted from lex_string() in c-lex.c.
3999
4000 FUTURE: ObjC++ will need to handle @-strings here. */
4001static cp_expr
4002cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4003 bool lookup_udlit = true)
4004{
4005 tree value;
4006 size_t count;
4007 struct obstack str_ob;
4008 cpp_string str, istr, *strs;
4009 cp_token *tok;
4010 enum cpp_ttype type, curr_type;
4011 int have_suffix_p = 0;
4012 tree string_tree;
4013 tree suffix_id = NULL_TREE;
4014 bool curr_tok_is_userdef_p = false;
4015
4016 tok = cp_lexer_peek_token (parser->lexer);
4017 if (!cp_parser_is_string_literal (tok))
4018 {
4019 cp_parser_error (parser, "expected string-literal");
4020 return error_mark_node;
4021 }
4022
4023 location_t loc = tok->location;
4024
4025 if (cpp_userdef_string_p (tok->type))
4026 {
4027 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4028 curr_type = cpp_userdef_string_remove_type (tok->type);
4029 curr_tok_is_userdef_p = true;
4030 }
4031 else
4032 {
4033 string_tree = tok->u.value;
4034 curr_type = tok->type;
4035 }
4036 type = curr_type;
4037
4038 /* Try to avoid the overhead of creating and destroying an obstack
4039 for the common case of just one string. */
4040 if (!cp_parser_is_string_literal
4041 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4042 {
4043 cp_lexer_consume_token (parser->lexer);
4044
4045 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4046 str.len = TREE_STRING_LENGTH (string_tree);
4047 count = 1;
4048
4049 if (curr_tok_is_userdef_p)
4050 {
4051 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4052 have_suffix_p = 1;
4053 curr_type = cpp_userdef_string_remove_type (tok->type);
4054 }
4055 else
4056 curr_type = tok->type;
4057
4058 strs = &str;
4059 }
4060 else
4061 {
4062 location_t last_tok_loc = tok->location;
4063 gcc_obstack_init (&str_ob);
4064 count = 0;
4065
4066 do
4067 {
4068 cp_lexer_consume_token (parser->lexer);
4069 count++;
4070 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4071 str.len = TREE_STRING_LENGTH (string_tree);
4072
4073 if (curr_tok_is_userdef_p)
4074 {
4075 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4076 if (have_suffix_p == 0)
4077 {
4078 suffix_id = curr_suffix_id;
4079 have_suffix_p = 1;
4080 }
4081 else if (have_suffix_p == 1
4082 && curr_suffix_id != suffix_id)
4083 {
4084 error ("inconsistent user-defined literal suffixes"
4085 " %qD and %qD in string literal",
4086 suffix_id, curr_suffix_id);
4087 have_suffix_p = -1;
4088 }
4089 curr_type = cpp_userdef_string_remove_type (tok->type);
4090 }
4091 else
4092 curr_type = tok->type;
4093
4094 if (type != curr_type)
4095 {
4096 if (type == CPP_STRING)
4097 type = curr_type;
4098 else if (curr_type != CPP_STRING)
4099 {
4100 rich_location rich_loc (line_table, tok->location);
4101 rich_loc.add_range (last_tok_loc, false);
4102 error_at (&rich_loc,
4103 "unsupported non-standard concatenation "
4104 "of string literals");
4105 }
4106 }
4107
4108 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4109
4110 last_tok_loc = tok->location;
4111
4112 tok = cp_lexer_peek_token (parser->lexer);
4113 if (cpp_userdef_string_p (tok->type))
4114 {
4115 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4116 curr_type = cpp_userdef_string_remove_type (tok->type);
4117 curr_tok_is_userdef_p = true;
4118 }
4119 else
4120 {
4121 string_tree = tok->u.value;
4122 curr_type = tok->type;
4123 curr_tok_is_userdef_p = false;
4124 }
4125 }
4126 while (cp_parser_is_string_literal (tok));
4127
4128 /* A string literal built by concatenation has its caret=start at
4129 the start of the initial string, and its finish at the finish of
4130 the final string literal. */
4131 loc = make_location (loc, loc, get_finish (last_tok_loc));
4132
4133 strs = (cpp_string *) obstack_finish (&str_ob);
4134 }
4135
4136 if (type != CPP_STRING && !wide_ok)
4137 {
4138 cp_parser_error (parser, "a wide string is invalid in this context");
4139 type = CPP_STRING;
4140 }
4141
4142 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4143 (parse_in, strs, count, &istr, type))
4144 {
4145 value = build_string (istr.len, (const char *)istr.text);
4146 free (CONST_CAST (unsigned char *, istr.text));
4147
4148 switch (type)
4149 {
4150 default:
4151 case CPP_STRING:
4152 case CPP_UTF8STRING:
4153 TREE_TYPE (value) = char_array_type_node;
4154 break;
4155 case CPP_STRING16:
4156 TREE_TYPE (value) = char16_array_type_node;
4157 break;
4158 case CPP_STRING32:
4159 TREE_TYPE (value) = char32_array_type_node;
4160 break;
4161 case CPP_WSTRING:
4162 TREE_TYPE (value) = wchar_array_type_node;
4163 break;
4164 }
4165
4166 value = fix_string_type (value);
4167
4168 if (have_suffix_p)
4169 {
4170 tree literal = build_userdef_literal (suffix_id, value,
4171 OT_NONE, NULL_TREE);
4172 if (lookup_udlit)
4173 value = cp_parser_userdef_string_literal (literal);
4174 else
4175 value = literal;
4176 }
4177 }
4178 else
4179 /* cpp_interpret_string has issued an error. */
4180 value = error_mark_node;
4181
4182 if (count > 1)
4183 obstack_free (&str_ob, 0);
4184
4185 return cp_expr (value, loc);
4186}
4187
4188/* Look up a literal operator with the name and the exact arguments. */
4189
4190static tree
4191lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4192{
4193 tree decl;
4194 decl = lookup_name (name);
4195 if (!decl || !is_overloaded_fn (decl))
4196 return error_mark_node;
4197
4198 for (lkp_iterator iter (decl); iter; ++iter)
4199 {
4200 unsigned int ix;
4201 bool found = true;
4202 tree fn = *iter;
4203 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4204 if (parmtypes != NULL_TREE)
4205 {
4206 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4207 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4208 {
4209 tree tparm = TREE_VALUE (parmtypes);
4210 tree targ = TREE_TYPE ((*args)[ix]);
4211 bool ptr = TYPE_PTR_P (tparm);
4212 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4213 if ((ptr || arr || !same_type_p (tparm, targ))
4214 && (!ptr || !arr
4215 || !same_type_p (TREE_TYPE (tparm),
4216 TREE_TYPE (targ))))
4217 found = false;
4218 }
4219 if (found
4220 && ix == vec_safe_length (args)
4221 /* May be this should be sufficient_parms_p instead,
4222 depending on how exactly should user-defined literals
4223 work in presence of default arguments on the literal
4224 operator parameters. */
4225 && parmtypes == void_list_node)
4226 return decl;
4227 }
4228 }
4229
4230 return error_mark_node;
4231}
4232
4233/* Parse a user-defined char constant. Returns a call to a user-defined
4234 literal operator taking the character as an argument. */
4235
4236static cp_expr
4237cp_parser_userdef_char_literal (cp_parser *parser)
4238{
4239 cp_token *token = cp_lexer_consume_token (parser->lexer);
4240 tree literal = token->u.value;
4241 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4242 tree value = USERDEF_LITERAL_VALUE (literal);
4243 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4244 tree decl, result;
4245
4246 /* Build up a call to the user-defined operator */
4247 /* Lookup the name we got back from the id-expression. */
4248 vec<tree, va_gc> *args = make_tree_vector ();
4249 vec_safe_push (args, value);
4250 decl = lookup_literal_operator (name, args);
4251 if (!decl || decl == error_mark_node)
4252 {
4253 error ("unable to find character literal operator %qD with %qT argument",
4254 name, TREE_TYPE (value));
4255 release_tree_vector (args);
4256 return error_mark_node;
4257 }
4258 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4259 release_tree_vector (args);
4260 return result;
4261}
4262
4263/* A subroutine of cp_parser_userdef_numeric_literal to
4264 create a char... template parameter pack from a string node. */
4265
4266static tree
4267make_char_string_pack (tree value)
4268{
4269 tree charvec;
4270 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4271 const char *str = TREE_STRING_POINTER (value);
4272 int i, len = TREE_STRING_LENGTH (value) - 1;
4273 tree argvec = make_tree_vec (1);
4274
4275 /* Fill in CHARVEC with all of the parameters. */
4276 charvec = make_tree_vec (len);
4277 for (i = 0; i < len; ++i)
4278 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4279
4280 /* Build the argument packs. */
4281 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4282
4283 TREE_VEC_ELT (argvec, 0) = argpack;
4284
4285 return argvec;
4286}
4287
4288/* A subroutine of cp_parser_userdef_numeric_literal to
4289 create a char... template parameter pack from a string node. */
4290
4291static tree
4292make_string_pack (tree value)
4293{
4294 tree charvec;
4295 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4296 const unsigned char *str
4297 = (const unsigned char *) TREE_STRING_POINTER (value);
4298 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4299 int len = TREE_STRING_LENGTH (value) / sz - 1;
4300 tree argvec = make_tree_vec (2);
4301
4302 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4303 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4304
4305 /* First template parm is character type. */
4306 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4307
4308 /* Fill in CHARVEC with all of the parameters. */
4309 charvec = make_tree_vec (len);
4310 for (int i = 0; i < len; ++i)
4311 TREE_VEC_ELT (charvec, i)
4312 = double_int_to_tree (str_char_type_node,
4313 double_int::from_buffer (str + i * sz, sz));
4314
4315 /* Build the argument packs. */
4316 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4317
4318 TREE_VEC_ELT (argvec, 1) = argpack;
4319
4320 return argvec;
4321}
4322
4323/* Parse a user-defined numeric constant. returns a call to a user-defined
4324 literal operator. */
4325
4326static cp_expr
4327cp_parser_userdef_numeric_literal (cp_parser *parser)
4328{
4329 cp_token *token = cp_lexer_consume_token (parser->lexer);
4330 tree literal = token->u.value;
4331 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4332 tree value = USERDEF_LITERAL_VALUE (literal);
4333 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4334 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4335 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4336 tree decl, result;
4337 vec<tree, va_gc> *args;
4338
4339 /* Look for a literal operator taking the exact type of numeric argument
4340 as the literal value. */
4341 args = make_tree_vector ();
4342 vec_safe_push (args, value);
4343 decl = lookup_literal_operator (name, args);
4344 if (decl && decl != error_mark_node)
4345 {
4346 result = finish_call_expr (decl, &args, false, true,
4347 tf_warning_or_error);
4348
4349 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4350 {
4351 warning_at (token->location, OPT_Woverflow,
4352 "integer literal exceeds range of %qT type",
4353 long_long_unsigned_type_node);
4354 }
4355 else
4356 {
4357 if (overflow > 0)
4358 warning_at (token->location, OPT_Woverflow,
4359 "floating literal exceeds range of %qT type",
4360 long_double_type_node);
4361 else if (overflow < 0)
4362 warning_at (token->location, OPT_Woverflow,
4363 "floating literal truncated to zero");
4364 }
4365
4366 release_tree_vector (args);
4367 return result;
4368 }
4369 release_tree_vector (args);
4370
4371 /* If the numeric argument didn't work, look for a raw literal
4372 operator taking a const char* argument consisting of the number
4373 in string format. */
4374 args = make_tree_vector ();
4375 vec_safe_push (args, num_string);
4376 decl = lookup_literal_operator (name, args);
4377 if (decl && decl != error_mark_node)
4378 {
4379 result = finish_call_expr (decl, &args, false, true,
4380 tf_warning_or_error);
4381 release_tree_vector (args);
4382 return result;
4383 }
4384 release_tree_vector (args);
4385
4386 /* If the raw literal didn't work, look for a non-type template
4387 function with parameter pack char.... Call the function with
4388 template parameter characters representing the number. */
4389 args = make_tree_vector ();
4390 decl = lookup_literal_operator (name, args);
4391 if (decl && decl != error_mark_node)
4392 {
4393 tree tmpl_args = make_char_string_pack (num_string);
4394 decl = lookup_template_function (decl, tmpl_args);
4395 result = finish_call_expr (decl, &args, false, true,
4396 tf_warning_or_error);
4397 release_tree_vector (args);
4398 return result;
4399 }
4400
4401 release_tree_vector (args);
4402
4403 /* In C++14 the standard library defines complex number suffixes that
4404 conflict with GNU extensions. Prefer them if <complex> is #included. */
4405 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4406 bool i14 = (cxx_dialect > cxx11
4407 && (id_equal (suffix_id, "i")
4408 || id_equal (suffix_id, "if")
4409 || id_equal (suffix_id, "il")));
4410 diagnostic_t kind = DK_ERROR;
4411 int opt = 0;
4412
4413 if (i14 && ext)
4414 {
4415 tree cxlit = lookup_qualified_name (std_node,
4416 get_identifier ("complex_literals"),
4417 0, false, false);
4418 if (cxlit == error_mark_node)
4419 {
4420 /* No <complex>, so pedwarn and use GNU semantics. */
4421 kind = DK_PEDWARN;
4422 opt = OPT_Wpedantic;
4423 }
4424 }
4425
4426 bool complained
4427 = emit_diagnostic (kind, input_location, opt,
4428 "unable to find numeric literal operator %qD", name);
4429
4430 if (!complained)
4431 /* Don't inform either. */;
4432 else if (i14)
4433 {
4434 inform (token->location, "add %<using namespace std::complex_literals%> "
4435 "(from <complex>) to enable the C++14 user-defined literal "
4436 "suffixes");
4437 if (ext)
4438 inform (token->location, "or use %<j%> instead of %<i%> for the "
4439 "GNU built-in suffix");
4440 }
4441 else if (!ext)
4442 inform (token->location, "use -fext-numeric-literals "
4443 "to enable more built-in suffixes");
4444
4445 if (kind == DK_ERROR)
4446 value = error_mark_node;
4447 else
4448 {
4449 /* Use the built-in semantics. */
4450 tree type;
4451 if (id_equal (suffix_id, "i"))
4452 {
4453 if (TREE_CODE (value) == INTEGER_CST)
4454 type = integer_type_node;
4455 else
4456 type = double_type_node;
4457 }
4458 else if (id_equal (suffix_id, "if"))
4459 type = float_type_node;
4460 else /* if (id_equal (suffix_id, "il")) */
4461 type = long_double_type_node;
4462
4463 value = build_complex (build_complex_type (type),
4464 fold_convert (type, integer_zero_node),
4465 fold_convert (type, value));
4466 }
4467
4468 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4469 /* Avoid repeated diagnostics. */
4470 token->u.value = value;
4471 return value;
4472}
4473
4474/* Parse a user-defined string constant. Returns a call to a user-defined
4475 literal operator taking a character pointer and the length of the string
4476 as arguments. */
4477
4478static tree
4479cp_parser_userdef_string_literal (tree literal)
4480{
4481 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4482 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4483 tree value = USERDEF_LITERAL_VALUE (literal);
4484 int len = TREE_STRING_LENGTH (value)
4485 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4486 tree decl, result;
4487 vec<tree, va_gc> *args;
4488
4489 /* Build up a call to the user-defined operator. */
4490 /* Lookup the name we got back from the id-expression. */
4491 args = make_tree_vector ();
4492 vec_safe_push (args, value);
4493 vec_safe_push (args, build_int_cst (size_type_node, len));
4494 decl = lookup_literal_operator (name, args);
4495
4496 if (decl && decl != error_mark_node)
4497 {
4498 result = finish_call_expr (decl, &args, false, true,
4499 tf_warning_or_error);
4500 release_tree_vector (args);
4501 return result;
4502 }
4503 release_tree_vector (args);
4504
4505 /* Look for a template function with typename parameter CharT
4506 and parameter pack CharT... Call the function with
4507 template parameter characters representing the string. */
4508 args = make_tree_vector ();
4509 decl = lookup_literal_operator (name, args);
4510 if (decl && decl != error_mark_node)
4511 {
4512 tree tmpl_args = make_string_pack (