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