]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
gcc/ada/
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000-2013 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
17 You should have received a copy of the GNU General Public License
18 along 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 "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "target.h"
35 #include "cgraph.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "plugin.h"
39 #include "tree-pretty-print.h"
40 #include "parser.h"
41 #include "type-utils.h"
42 #include "omp-low.h"
43
44 \f
45 /* The lexer. */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
49
50 static cp_token eof_token =
51 {
52 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57 NIC_NONE,
58 /* floating-point literal */
59 NIC_FLOAT,
60 /* %<this%> */
61 NIC_THIS,
62 /* %<__FUNCTION__%> */
63 NIC_FUNC_NAME,
64 /* %<__PRETTY_FUNCTION__%> */
65 NIC_PRETTY_FUNC,
66 /* %<__func__%> */
67 NIC_C99_FUNC,
68 /* "%<va_arg%> */
69 NIC_VA_ARG,
70 /* a cast */
71 NIC_CAST,
72 /* %<typeid%> operator */
73 NIC_TYPEID,
74 /* non-constant compound literals */
75 NIC_NCC,
76 /* a function call */
77 NIC_FUNC_CALL,
78 /* an increment */
79 NIC_INC,
80 /* an decrement */
81 NIC_DEC,
82 /* an array reference */
83 NIC_ARRAY_REF,
84 /* %<->%> */
85 NIC_ARROW,
86 /* %<.%> */
87 NIC_POINT,
88 /* the address of a label */
89 NIC_ADDR_LABEL,
90 /* %<*%> */
91 NIC_STAR,
92 /* %<&%> */
93 NIC_ADDR,
94 /* %<++%> */
95 NIC_PREINCREMENT,
96 /* %<--%> */
97 NIC_PREDECREMENT,
98 /* %<new%> */
99 NIC_NEW,
100 /* %<delete%> */
101 NIC_DEL,
102 /* calls to overloaded operators */
103 NIC_OVERLOADED,
104 /* an assignment */
105 NIC_ASSIGNMENT,
106 /* a comma operator */
107 NIC_COMMA,
108 /* a call to a constructor */
109 NIC_CONSTRUCTOR,
110 /* a transaction expression */
111 NIC_TRANSACTION
112 } non_integral_constant;
113
114 /* The various kinds of errors about name-lookup failing. */
115 typedef enum name_lookup_error {
116 /* NULL */
117 NLE_NULL,
118 /* is not a type */
119 NLE_TYPE,
120 /* is not a class or namespace */
121 NLE_CXX98,
122 /* is not a class, namespace, or enumeration */
123 NLE_NOT_CXX98
124 } name_lookup_error;
125
126 /* The various kinds of required token */
127 typedef enum required_token {
128 RT_NONE,
129 RT_SEMICOLON, /* ';' */
130 RT_OPEN_PAREN, /* '(' */
131 RT_CLOSE_BRACE, /* '}' */
132 RT_OPEN_BRACE, /* '{' */
133 RT_CLOSE_SQUARE, /* ']' */
134 RT_OPEN_SQUARE, /* '[' */
135 RT_COMMA, /* ',' */
136 RT_SCOPE, /* '::' */
137 RT_LESS, /* '<' */
138 RT_GREATER, /* '>' */
139 RT_EQ, /* '=' */
140 RT_ELLIPSIS, /* '...' */
141 RT_MULT, /* '*' */
142 RT_COMPL, /* '~' */
143 RT_COLON, /* ':' */
144 RT_COLON_SCOPE, /* ':' or '::' */
145 RT_CLOSE_PAREN, /* ')' */
146 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147 RT_PRAGMA_EOL, /* end of line */
148 RT_NAME, /* identifier */
149
150 /* The type is CPP_KEYWORD */
151 RT_NEW, /* new */
152 RT_DELETE, /* delete */
153 RT_RETURN, /* return */
154 RT_WHILE, /* while */
155 RT_EXTERN, /* extern */
156 RT_STATIC_ASSERT, /* static_assert */
157 RT_DECLTYPE, /* decltype */
158 RT_OPERATOR, /* operator */
159 RT_CLASS, /* class */
160 RT_TEMPLATE, /* template */
161 RT_NAMESPACE, /* namespace */
162 RT_USING, /* using */
163 RT_ASM, /* asm */
164 RT_TRY, /* try */
165 RT_CATCH, /* catch */
166 RT_THROW, /* throw */
167 RT_LABEL, /* __label__ */
168 RT_AT_TRY, /* @try */
169 RT_AT_SYNCHRONIZED, /* @synchronized */
170 RT_AT_THROW, /* @throw */
171
172 RT_SELECT, /* selection-statement */
173 RT_INTERATION, /* iteration-statement */
174 RT_JUMP, /* jump-statement */
175 RT_CLASS_KEY, /* class-key */
176 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179 RT_TRANSACTION_CANCEL /* __transaction_cancel */
180 } required_token;
181
182 /* Prototypes. */
183
184 static cp_lexer *cp_lexer_new_main
185 (void);
186 static cp_lexer *cp_lexer_new_from_tokens
187 (cp_token_cache *tokens);
188 static void cp_lexer_destroy
189 (cp_lexer *);
190 static int cp_lexer_saving_tokens
191 (const cp_lexer *);
192 static cp_token *cp_lexer_token_at
193 (cp_lexer *, cp_token_position);
194 static void cp_lexer_get_preprocessor_token
195 (cp_lexer *, cp_token *);
196 static inline cp_token *cp_lexer_peek_token
197 (cp_lexer *);
198 static cp_token *cp_lexer_peek_nth_token
199 (cp_lexer *, size_t);
200 static inline bool cp_lexer_next_token_is
201 (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_not
203 (cp_lexer *, enum cpp_ttype);
204 static bool cp_lexer_next_token_is_keyword
205 (cp_lexer *, enum rid);
206 static cp_token *cp_lexer_consume_token
207 (cp_lexer *);
208 static void cp_lexer_purge_token
209 (cp_lexer *);
210 static void cp_lexer_purge_tokens_after
211 (cp_lexer *, cp_token_position);
212 static void cp_lexer_save_tokens
213 (cp_lexer *);
214 static void cp_lexer_commit_tokens
215 (cp_lexer *);
216 static void cp_lexer_rollback_tokens
217 (cp_lexer *);
218 static void cp_lexer_print_token
219 (FILE *, cp_token *);
220 static inline bool cp_lexer_debugging_p
221 (cp_lexer *);
222 static void cp_lexer_start_debugging
223 (cp_lexer *) ATTRIBUTE_UNUSED;
224 static void cp_lexer_stop_debugging
225 (cp_lexer *) ATTRIBUTE_UNUSED;
226
227 static cp_token_cache *cp_token_cache_new
228 (cp_token *, cp_token *);
229
230 static void cp_parser_initial_pragma
231 (cp_token *);
232
233 static tree cp_literal_operator_id
234 (const char *);
235
236 static void cp_parser_cilk_simd
237 (cp_parser *, cp_token *);
238 static bool cp_parser_omp_declare_reduction_exprs
239 (tree, cp_parser *);
240
241 /* Manifest constants. */
242 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
243 #define CP_SAVED_TOKEN_STACK 5
244
245 /* Variables. */
246
247 /* The stream to which debugging output should be written. */
248 static FILE *cp_lexer_debug_stream;
249
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand;
253
254 /* Dump up to NUM tokens in BUFFER to FILE starting with token
255 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
256 first token in BUFFER. If NUM is 0, dump all the tokens. If
257 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
258 highlighted by surrounding it in [[ ]]. */
259
260 static void
261 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
262 cp_token *start_token, unsigned num,
263 cp_token *curr_token)
264 {
265 unsigned i, nprinted;
266 cp_token *token;
267 bool do_print;
268
269 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
270
271 if (buffer == NULL)
272 return;
273
274 if (num == 0)
275 num = buffer->length ();
276
277 if (start_token == NULL)
278 start_token = buffer->address ();
279
280 if (start_token > buffer->address ())
281 {
282 cp_lexer_print_token (file, &(*buffer)[0]);
283 fprintf (file, " ... ");
284 }
285
286 do_print = false;
287 nprinted = 0;
288 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
289 {
290 if (token == start_token)
291 do_print = true;
292
293 if (!do_print)
294 continue;
295
296 nprinted++;
297 if (token == curr_token)
298 fprintf (file, "[[");
299
300 cp_lexer_print_token (file, token);
301
302 if (token == curr_token)
303 fprintf (file, "]]");
304
305 switch (token->type)
306 {
307 case CPP_SEMICOLON:
308 case CPP_OPEN_BRACE:
309 case CPP_CLOSE_BRACE:
310 case CPP_EOF:
311 fputc ('\n', file);
312 break;
313
314 default:
315 fputc (' ', file);
316 }
317 }
318
319 if (i == num && i < buffer->length ())
320 {
321 fprintf (file, " ... ");
322 cp_lexer_print_token (file, &buffer->last ());
323 }
324
325 fprintf (file, "\n");
326 }
327
328
329 /* Dump all tokens in BUFFER to stderr. */
330
331 void
332 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
333 {
334 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
335 }
336
337 DEBUG_FUNCTION void
338 debug (vec<cp_token, va_gc> &ref)
339 {
340 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
341 }
342
343 DEBUG_FUNCTION void
344 debug (vec<cp_token, va_gc> *ptr)
345 {
346 if (ptr)
347 debug (*ptr);
348 else
349 fprintf (stderr, "<nil>\n");
350 }
351
352
353 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
354 description for T. */
355
356 static void
357 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
358 {
359 if (t)
360 {
361 fprintf (file, "%s: ", desc);
362 print_node_brief (file, "", t, 0);
363 }
364 }
365
366
367 /* Dump parser context C to FILE. */
368
369 static void
370 cp_debug_print_context (FILE *file, cp_parser_context *c)
371 {
372 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
373 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
374 print_node_brief (file, "", c->object_type, 0);
375 fprintf (file, "}\n");
376 }
377
378
379 /* Print the stack of parsing contexts to FILE starting with FIRST. */
380
381 static void
382 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
383 {
384 unsigned i;
385 cp_parser_context *c;
386
387 fprintf (file, "Parsing context stack:\n");
388 for (i = 0, c = first; c; c = c->next, i++)
389 {
390 fprintf (file, "\t#%u: ", i);
391 cp_debug_print_context (file, c);
392 }
393 }
394
395
396 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
397
398 static void
399 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
400 {
401 if (flag)
402 fprintf (file, "%s: true\n", desc);
403 }
404
405
406 /* Print an unparsed function entry UF to FILE. */
407
408 static void
409 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
410 {
411 unsigned i;
412 cp_default_arg_entry *default_arg_fn;
413 tree fn;
414
415 fprintf (file, "\tFunctions with default args:\n");
416 for (i = 0;
417 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
418 i++)
419 {
420 fprintf (file, "\t\tClass type: ");
421 print_node_brief (file, "", default_arg_fn->class_type, 0);
422 fprintf (file, "\t\tDeclaration: ");
423 print_node_brief (file, "", default_arg_fn->decl, 0);
424 fprintf (file, "\n");
425 }
426
427 fprintf (file, "\n\tFunctions with definitions that require "
428 "post-processing\n\t\t");
429 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
430 {
431 print_node_brief (file, "", fn, 0);
432 fprintf (file, " ");
433 }
434 fprintf (file, "\n");
435
436 fprintf (file, "\n\tNon-static data members with initializers that require "
437 "post-processing\n\t\t");
438 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
439 {
440 print_node_brief (file, "", fn, 0);
441 fprintf (file, " ");
442 }
443 fprintf (file, "\n");
444 }
445
446
447 /* Print the stack of unparsed member functions S to FILE. */
448
449 static void
450 cp_debug_print_unparsed_queues (FILE *file,
451 vec<cp_unparsed_functions_entry, va_gc> *s)
452 {
453 unsigned i;
454 cp_unparsed_functions_entry *uf;
455
456 fprintf (file, "Unparsed functions\n");
457 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
458 {
459 fprintf (file, "#%u:\n", i);
460 cp_debug_print_unparsed_function (file, uf);
461 }
462 }
463
464
465 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
466 the given PARSER. If FILE is NULL, the output is printed on stderr. */
467
468 static void
469 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
470 {
471 cp_token *next_token, *first_token, *start_token;
472
473 if (file == NULL)
474 file = stderr;
475
476 next_token = parser->lexer->next_token;
477 first_token = parser->lexer->buffer->address ();
478 start_token = (next_token > first_token + window_size / 2)
479 ? next_token - window_size / 2
480 : first_token;
481 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
482 next_token);
483 }
484
485
486 /* Dump debugging information for the given PARSER. If FILE is NULL,
487 the output is printed on stderr. */
488
489 void
490 cp_debug_parser (FILE *file, cp_parser *parser)
491 {
492 const size_t window_size = 20;
493 cp_token *token;
494 expanded_location eloc;
495
496 if (file == NULL)
497 file = stderr;
498
499 fprintf (file, "Parser state\n\n");
500 fprintf (file, "Number of tokens: %u\n",
501 vec_safe_length (parser->lexer->buffer));
502 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
503 cp_debug_print_tree_if_set (file, "Object scope",
504 parser->object_scope);
505 cp_debug_print_tree_if_set (file, "Qualifying scope",
506 parser->qualifying_scope);
507 cp_debug_print_context_stack (file, parser->context);
508 cp_debug_print_flag (file, "Allow GNU extensions",
509 parser->allow_gnu_extensions_p);
510 cp_debug_print_flag (file, "'>' token is greater-than",
511 parser->greater_than_is_operator_p);
512 cp_debug_print_flag (file, "Default args allowed in current "
513 "parameter list", parser->default_arg_ok_p);
514 cp_debug_print_flag (file, "Parsing integral constant-expression",
515 parser->integral_constant_expression_p);
516 cp_debug_print_flag (file, "Allow non-constant expression in current "
517 "constant-expression",
518 parser->allow_non_integral_constant_expression_p);
519 cp_debug_print_flag (file, "Seen non-constant expression",
520 parser->non_integral_constant_expression_p);
521 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
522 "current context",
523 parser->local_variables_forbidden_p);
524 cp_debug_print_flag (file, "In unbraced linkage specification",
525 parser->in_unbraced_linkage_specification_p);
526 cp_debug_print_flag (file, "Parsing a declarator",
527 parser->in_declarator_p);
528 cp_debug_print_flag (file, "In template argument list",
529 parser->in_template_argument_list_p);
530 cp_debug_print_flag (file, "Parsing an iteration statement",
531 parser->in_statement & IN_ITERATION_STMT);
532 cp_debug_print_flag (file, "Parsing a switch statement",
533 parser->in_statement & IN_SWITCH_STMT);
534 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
535 parser->in_statement & IN_OMP_BLOCK);
536 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
537 parser->in_statement & IN_CILK_SIMD_FOR);
538 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
539 parser->in_statement & IN_OMP_FOR);
540 cp_debug_print_flag (file, "Parsing an if statement",
541 parser->in_statement & IN_IF_STMT);
542 cp_debug_print_flag (file, "Parsing a type-id in an expression "
543 "context", parser->in_type_id_in_expr_p);
544 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
545 parser->implicit_extern_c);
546 cp_debug_print_flag (file, "String expressions should be translated "
547 "to execution character set",
548 parser->translate_strings_p);
549 cp_debug_print_flag (file, "Parsing function body outside of a "
550 "local class", parser->in_function_body);
551 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
552 parser->colon_corrects_to_scope_p);
553 cp_debug_print_flag (file, "Colon doesn't start a class definition",
554 parser->colon_doesnt_start_class_def_p);
555 if (parser->type_definition_forbidden_message)
556 fprintf (file, "Error message for forbidden type definitions: %s\n",
557 parser->type_definition_forbidden_message);
558 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
559 fprintf (file, "Number of class definitions in progress: %u\n",
560 parser->num_classes_being_defined);
561 fprintf (file, "Number of template parameter lists for the current "
562 "declaration: %u\n", parser->num_template_parameter_lists);
563 cp_debug_parser_tokens (file, parser, window_size);
564 token = parser->lexer->next_token;
565 fprintf (file, "Next token to parse:\n");
566 fprintf (file, "\tToken: ");
567 cp_lexer_print_token (file, token);
568 eloc = expand_location (token->location);
569 fprintf (file, "\n\tFile: %s\n", eloc.file);
570 fprintf (file, "\tLine: %d\n", eloc.line);
571 fprintf (file, "\tColumn: %d\n", eloc.column);
572 }
573
574 DEBUG_FUNCTION void
575 debug (cp_parser &ref)
576 {
577 cp_debug_parser (stderr, &ref);
578 }
579
580 DEBUG_FUNCTION void
581 debug (cp_parser *ptr)
582 {
583 if (ptr)
584 debug (*ptr);
585 else
586 fprintf (stderr, "<nil>\n");
587 }
588
589 /* Allocate memory for a new lexer object and return it. */
590
591 static cp_lexer *
592 cp_lexer_alloc (void)
593 {
594 cp_lexer *lexer;
595
596 c_common_no_more_pch ();
597
598 /* Allocate the memory. */
599 lexer = ggc_alloc_cleared_cp_lexer ();
600
601 /* Initially we are not debugging. */
602 lexer->debugging_p = false;
603
604 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
605
606 /* Create the buffer. */
607 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
608
609 return lexer;
610 }
611
612
613 /* Create a new main C++ lexer, the lexer that gets tokens from the
614 preprocessor. */
615
616 static cp_lexer *
617 cp_lexer_new_main (void)
618 {
619 cp_lexer *lexer;
620 cp_token token;
621
622 /* It's possible that parsing the first pragma will load a PCH file,
623 which is a GC collection point. So we have to do that before
624 allocating any memory. */
625 cp_parser_initial_pragma (&token);
626
627 lexer = cp_lexer_alloc ();
628
629 /* Put the first token in the buffer. */
630 lexer->buffer->quick_push (token);
631
632 /* Get the remaining tokens from the preprocessor. */
633 while (token.type != CPP_EOF)
634 {
635 cp_lexer_get_preprocessor_token (lexer, &token);
636 vec_safe_push (lexer->buffer, token);
637 }
638
639 lexer->last_token = lexer->buffer->address ()
640 + lexer->buffer->length ()
641 - 1;
642 lexer->next_token = lexer->buffer->length ()
643 ? lexer->buffer->address ()
644 : &eof_token;
645
646 /* Subsequent preprocessor diagnostics should use compiler
647 diagnostic functions to get the compiler source location. */
648 done_lexing = true;
649
650 gcc_assert (!lexer->next_token->purged_p);
651 return lexer;
652 }
653
654 /* Create a new lexer whose token stream is primed with the tokens in
655 CACHE. When these tokens are exhausted, no new tokens will be read. */
656
657 static cp_lexer *
658 cp_lexer_new_from_tokens (cp_token_cache *cache)
659 {
660 cp_token *first = cache->first;
661 cp_token *last = cache->last;
662 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
663
664 /* We do not own the buffer. */
665 lexer->buffer = NULL;
666 lexer->next_token = first == last ? &eof_token : first;
667 lexer->last_token = last;
668
669 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
670
671 /* Initially we are not debugging. */
672 lexer->debugging_p = false;
673
674 gcc_assert (!lexer->next_token->purged_p);
675 return lexer;
676 }
677
678 /* Frees all resources associated with LEXER. */
679
680 static void
681 cp_lexer_destroy (cp_lexer *lexer)
682 {
683 vec_free (lexer->buffer);
684 lexer->saved_tokens.release ();
685 ggc_free (lexer);
686 }
687
688 /* Returns nonzero if debugging information should be output. */
689
690 static inline bool
691 cp_lexer_debugging_p (cp_lexer *lexer)
692 {
693 return lexer->debugging_p;
694 }
695
696
697 static inline cp_token_position
698 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
699 {
700 gcc_assert (!previous_p || lexer->next_token != &eof_token);
701
702 return lexer->next_token - previous_p;
703 }
704
705 static inline cp_token *
706 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
707 {
708 return pos;
709 }
710
711 static inline void
712 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
713 {
714 lexer->next_token = cp_lexer_token_at (lexer, pos);
715 }
716
717 static inline cp_token_position
718 cp_lexer_previous_token_position (cp_lexer *lexer)
719 {
720 if (lexer->next_token == &eof_token)
721 return lexer->last_token - 1;
722 else
723 return cp_lexer_token_position (lexer, true);
724 }
725
726 static inline cp_token *
727 cp_lexer_previous_token (cp_lexer *lexer)
728 {
729 cp_token_position tp = cp_lexer_previous_token_position (lexer);
730
731 return cp_lexer_token_at (lexer, tp);
732 }
733
734 /* nonzero if we are presently saving tokens. */
735
736 static inline int
737 cp_lexer_saving_tokens (const cp_lexer* lexer)
738 {
739 return lexer->saved_tokens.length () != 0;
740 }
741
742 /* Store the next token from the preprocessor in *TOKEN. Return true
743 if we reach EOF. If LEXER is NULL, assume we are handling an
744 initial #pragma pch_preprocess, and thus want the lexer to return
745 processed strings. */
746
747 static void
748 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
749 {
750 static int is_extern_c = 0;
751
752 /* Get a new token from the preprocessor. */
753 token->type
754 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
755 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
756 token->keyword = RID_MAX;
757 token->pragma_kind = PRAGMA_NONE;
758 token->purged_p = false;
759
760 /* On some systems, some header files are surrounded by an
761 implicit extern "C" block. Set a flag in the token if it
762 comes from such a header. */
763 is_extern_c += pending_lang_change;
764 pending_lang_change = 0;
765 token->implicit_extern_c = is_extern_c > 0;
766
767 /* Check to see if this token is a keyword. */
768 if (token->type == CPP_NAME)
769 {
770 if (C_IS_RESERVED_WORD (token->u.value))
771 {
772 /* Mark this token as a keyword. */
773 token->type = CPP_KEYWORD;
774 /* Record which keyword. */
775 token->keyword = C_RID_CODE (token->u.value);
776 }
777 else
778 {
779 if (warn_cxx0x_compat
780 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
781 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
782 {
783 /* Warn about the C++0x keyword (but still treat it as
784 an identifier). */
785 warning (OPT_Wc__0x_compat,
786 "identifier %qE is a keyword in C++11",
787 token->u.value);
788
789 /* Clear out the C_RID_CODE so we don't warn about this
790 particular identifier-turned-keyword again. */
791 C_SET_RID_CODE (token->u.value, RID_MAX);
792 }
793
794 token->ambiguous_p = false;
795 token->keyword = RID_MAX;
796 }
797 }
798 else if (token->type == CPP_AT_NAME)
799 {
800 /* This only happens in Objective-C++; it must be a keyword. */
801 token->type = CPP_KEYWORD;
802 switch (C_RID_CODE (token->u.value))
803 {
804 /* Replace 'class' with '@class', 'private' with '@private',
805 etc. This prevents confusion with the C++ keyword
806 'class', and makes the tokens consistent with other
807 Objective-C 'AT' keywords. For example '@class' is
808 reported as RID_AT_CLASS which is consistent with
809 '@synchronized', which is reported as
810 RID_AT_SYNCHRONIZED.
811 */
812 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
813 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
814 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
815 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
816 case RID_THROW: token->keyword = RID_AT_THROW; break;
817 case RID_TRY: token->keyword = RID_AT_TRY; break;
818 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
819 default: token->keyword = C_RID_CODE (token->u.value);
820 }
821 }
822 else if (token->type == CPP_PRAGMA)
823 {
824 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
825 token->pragma_kind = ((enum pragma_kind)
826 TREE_INT_CST_LOW (token->u.value));
827 token->u.value = NULL_TREE;
828 }
829 }
830
831 /* Update the globals input_location and the input file stack from TOKEN. */
832 static inline void
833 cp_lexer_set_source_position_from_token (cp_token *token)
834 {
835 if (token->type != CPP_EOF)
836 {
837 input_location = token->location;
838 }
839 }
840
841 /* Return a pointer to the next token in the token stream, but do not
842 consume it. */
843
844 static inline cp_token *
845 cp_lexer_peek_token (cp_lexer *lexer)
846 {
847 if (cp_lexer_debugging_p (lexer))
848 {
849 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
850 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
851 putc ('\n', cp_lexer_debug_stream);
852 }
853 return lexer->next_token;
854 }
855
856 /* Return true if the next token has the indicated TYPE. */
857
858 static inline bool
859 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
860 {
861 return cp_lexer_peek_token (lexer)->type == type;
862 }
863
864 /* Return true if the next token does not have the indicated TYPE. */
865
866 static inline bool
867 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
868 {
869 return !cp_lexer_next_token_is (lexer, type);
870 }
871
872 /* Return true if the next token is the indicated KEYWORD. */
873
874 static inline bool
875 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
876 {
877 return cp_lexer_peek_token (lexer)->keyword == keyword;
878 }
879
880 static inline bool
881 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
882 {
883 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
884 }
885
886 /* Return true if the next token is not the indicated KEYWORD. */
887
888 static inline bool
889 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
890 {
891 return cp_lexer_peek_token (lexer)->keyword != keyword;
892 }
893
894 /* Return true if the next token is a keyword for a decl-specifier. */
895
896 static bool
897 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
898 {
899 cp_token *token;
900
901 token = cp_lexer_peek_token (lexer);
902 switch (token->keyword)
903 {
904 /* auto specifier: storage-class-specifier in C++,
905 simple-type-specifier in C++0x. */
906 case RID_AUTO:
907 /* Storage classes. */
908 case RID_REGISTER:
909 case RID_STATIC:
910 case RID_EXTERN:
911 case RID_MUTABLE:
912 case RID_THREAD:
913 /* Elaborated type specifiers. */
914 case RID_ENUM:
915 case RID_CLASS:
916 case RID_STRUCT:
917 case RID_UNION:
918 case RID_TYPENAME:
919 /* Simple type specifiers. */
920 case RID_CHAR:
921 case RID_CHAR16:
922 case RID_CHAR32:
923 case RID_WCHAR:
924 case RID_BOOL:
925 case RID_SHORT:
926 case RID_INT:
927 case RID_LONG:
928 case RID_INT128:
929 case RID_SIGNED:
930 case RID_UNSIGNED:
931 case RID_FLOAT:
932 case RID_DOUBLE:
933 case RID_VOID:
934 /* GNU extensions. */
935 case RID_ATTRIBUTE:
936 case RID_TYPEOF:
937 /* C++0x extensions. */
938 case RID_DECLTYPE:
939 case RID_UNDERLYING_TYPE:
940 return true;
941
942 default:
943 return false;
944 }
945 }
946
947 /* Returns TRUE iff the token T begins a decltype type. */
948
949 static bool
950 token_is_decltype (cp_token *t)
951 {
952 return (t->keyword == RID_DECLTYPE
953 || t->type == CPP_DECLTYPE);
954 }
955
956 /* Returns TRUE iff the next token begins a decltype type. */
957
958 static bool
959 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
960 {
961 cp_token *t = cp_lexer_peek_token (lexer);
962 return token_is_decltype (t);
963 }
964
965 /* Return a pointer to the Nth token in the token stream. If N is 1,
966 then this is precisely equivalent to cp_lexer_peek_token (except
967 that it is not inline). One would like to disallow that case, but
968 there is one case (cp_parser_nth_token_starts_template_id) where
969 the caller passes a variable for N and it might be 1. */
970
971 static cp_token *
972 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
973 {
974 cp_token *token;
975
976 /* N is 1-based, not zero-based. */
977 gcc_assert (n > 0);
978
979 if (cp_lexer_debugging_p (lexer))
980 fprintf (cp_lexer_debug_stream,
981 "cp_lexer: peeking ahead %ld at token: ", (long)n);
982
983 --n;
984 token = lexer->next_token;
985 gcc_assert (!n || token != &eof_token);
986 while (n != 0)
987 {
988 ++token;
989 if (token == lexer->last_token)
990 {
991 token = &eof_token;
992 break;
993 }
994
995 if (!token->purged_p)
996 --n;
997 }
998
999 if (cp_lexer_debugging_p (lexer))
1000 {
1001 cp_lexer_print_token (cp_lexer_debug_stream, token);
1002 putc ('\n', cp_lexer_debug_stream);
1003 }
1004
1005 return token;
1006 }
1007
1008 /* Return the next token, and advance the lexer's next_token pointer
1009 to point to the next non-purged token. */
1010
1011 static cp_token *
1012 cp_lexer_consume_token (cp_lexer* lexer)
1013 {
1014 cp_token *token = lexer->next_token;
1015
1016 gcc_assert (token != &eof_token);
1017 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1018
1019 do
1020 {
1021 lexer->next_token++;
1022 if (lexer->next_token == lexer->last_token)
1023 {
1024 lexer->next_token = &eof_token;
1025 break;
1026 }
1027
1028 }
1029 while (lexer->next_token->purged_p);
1030
1031 cp_lexer_set_source_position_from_token (token);
1032
1033 /* Provide debugging output. */
1034 if (cp_lexer_debugging_p (lexer))
1035 {
1036 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1037 cp_lexer_print_token (cp_lexer_debug_stream, token);
1038 putc ('\n', cp_lexer_debug_stream);
1039 }
1040
1041 return token;
1042 }
1043
1044 /* Permanently remove the next token from the token stream, and
1045 advance the next_token pointer to refer to the next non-purged
1046 token. */
1047
1048 static void
1049 cp_lexer_purge_token (cp_lexer *lexer)
1050 {
1051 cp_token *tok = lexer->next_token;
1052
1053 gcc_assert (tok != &eof_token);
1054 tok->purged_p = true;
1055 tok->location = UNKNOWN_LOCATION;
1056 tok->u.value = NULL_TREE;
1057 tok->keyword = RID_MAX;
1058
1059 do
1060 {
1061 tok++;
1062 if (tok == lexer->last_token)
1063 {
1064 tok = &eof_token;
1065 break;
1066 }
1067 }
1068 while (tok->purged_p);
1069 lexer->next_token = tok;
1070 }
1071
1072 /* Permanently remove all tokens after TOK, up to, but not
1073 including, the token that will be returned next by
1074 cp_lexer_peek_token. */
1075
1076 static void
1077 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1078 {
1079 cp_token *peek = lexer->next_token;
1080
1081 if (peek == &eof_token)
1082 peek = lexer->last_token;
1083
1084 gcc_assert (tok < peek);
1085
1086 for ( tok += 1; tok != peek; tok += 1)
1087 {
1088 tok->purged_p = true;
1089 tok->location = UNKNOWN_LOCATION;
1090 tok->u.value = NULL_TREE;
1091 tok->keyword = RID_MAX;
1092 }
1093 }
1094
1095 /* Begin saving tokens. All tokens consumed after this point will be
1096 preserved. */
1097
1098 static void
1099 cp_lexer_save_tokens (cp_lexer* lexer)
1100 {
1101 /* Provide debugging output. */
1102 if (cp_lexer_debugging_p (lexer))
1103 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1104
1105 lexer->saved_tokens.safe_push (lexer->next_token);
1106 }
1107
1108 /* Commit to the portion of the token stream most recently saved. */
1109
1110 static void
1111 cp_lexer_commit_tokens (cp_lexer* lexer)
1112 {
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1115 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1116
1117 lexer->saved_tokens.pop ();
1118 }
1119
1120 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1121 to the token stream. Stop saving tokens. */
1122
1123 static void
1124 cp_lexer_rollback_tokens (cp_lexer* lexer)
1125 {
1126 /* Provide debugging output. */
1127 if (cp_lexer_debugging_p (lexer))
1128 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1129
1130 lexer->next_token = lexer->saved_tokens.pop ();
1131 }
1132
1133 /* Print a representation of the TOKEN on the STREAM. */
1134
1135 static void
1136 cp_lexer_print_token (FILE * stream, cp_token *token)
1137 {
1138 /* We don't use cpp_type2name here because the parser defines
1139 a few tokens of its own. */
1140 static const char *const token_names[] = {
1141 /* cpplib-defined token types */
1142 #define OP(e, s) #e,
1143 #define TK(e, s) #e,
1144 TTYPE_TABLE
1145 #undef OP
1146 #undef TK
1147 /* C++ parser token types - see "Manifest constants", above. */
1148 "KEYWORD",
1149 "TEMPLATE_ID",
1150 "NESTED_NAME_SPECIFIER",
1151 };
1152
1153 /* For some tokens, print the associated data. */
1154 switch (token->type)
1155 {
1156 case CPP_KEYWORD:
1157 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1158 For example, `struct' is mapped to an INTEGER_CST. */
1159 if (!identifier_p (token->u.value))
1160 break;
1161 /* else fall through */
1162 case CPP_NAME:
1163 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1164 break;
1165
1166 case CPP_STRING:
1167 case CPP_STRING16:
1168 case CPP_STRING32:
1169 case CPP_WSTRING:
1170 case CPP_UTF8STRING:
1171 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1172 break;
1173
1174 case CPP_NUMBER:
1175 print_generic_expr (stream, token->u.value, 0);
1176 break;
1177
1178 default:
1179 /* If we have a name for the token, print it out. Otherwise, we
1180 simply give the numeric code. */
1181 if (token->type < ARRAY_SIZE(token_names))
1182 fputs (token_names[token->type], stream);
1183 else
1184 fprintf (stream, "[%d]", token->type);
1185 break;
1186 }
1187 }
1188
1189 DEBUG_FUNCTION void
1190 debug (cp_token &ref)
1191 {
1192 cp_lexer_print_token (stderr, &ref);
1193 fprintf (stderr, "\n");
1194 }
1195
1196 DEBUG_FUNCTION void
1197 debug (cp_token *ptr)
1198 {
1199 if (ptr)
1200 debug (*ptr);
1201 else
1202 fprintf (stderr, "<nil>\n");
1203 }
1204
1205
1206 /* Start emitting debugging information. */
1207
1208 static void
1209 cp_lexer_start_debugging (cp_lexer* lexer)
1210 {
1211 lexer->debugging_p = true;
1212 cp_lexer_debug_stream = stderr;
1213 }
1214
1215 /* Stop emitting debugging information. */
1216
1217 static void
1218 cp_lexer_stop_debugging (cp_lexer* lexer)
1219 {
1220 lexer->debugging_p = false;
1221 cp_lexer_debug_stream = NULL;
1222 }
1223
1224 /* Create a new cp_token_cache, representing a range of tokens. */
1225
1226 static cp_token_cache *
1227 cp_token_cache_new (cp_token *first, cp_token *last)
1228 {
1229 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1230 cache->first = first;
1231 cache->last = last;
1232 return cache;
1233 }
1234
1235 /* Diagnose if #pragma omp declare simd isn't followed immediately
1236 by function declaration or definition. */
1237
1238 static inline void
1239 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1240 {
1241 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1242 {
1243 error ("%<#pragma omp declare simd%> not immediately followed by "
1244 "function declaration or definition");
1245 parser->omp_declare_simd = NULL;
1246 }
1247 }
1248
1249 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1250 and put that into "omp declare simd" attribute. */
1251
1252 static inline void
1253 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1254 {
1255 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1256 {
1257 if (fndecl == error_mark_node)
1258 {
1259 parser->omp_declare_simd = NULL;
1260 return;
1261 }
1262 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1263 {
1264 cp_ensure_no_omp_declare_simd (parser);
1265 return;
1266 }
1267 }
1268 }
1269 \f
1270 /* Decl-specifiers. */
1271
1272 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1273
1274 static void
1275 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1276 {
1277 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1278 }
1279
1280 /* Declarators. */
1281
1282 /* Nothing other than the parser should be creating declarators;
1283 declarators are a semi-syntactic representation of C++ entities.
1284 Other parts of the front end that need to create entities (like
1285 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1286
1287 static cp_declarator *make_call_declarator
1288 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1289 static cp_declarator *make_array_declarator
1290 (cp_declarator *, tree);
1291 static cp_declarator *make_pointer_declarator
1292 (cp_cv_quals, cp_declarator *, tree);
1293 static cp_declarator *make_reference_declarator
1294 (cp_cv_quals, cp_declarator *, bool, tree);
1295 static cp_parameter_declarator *make_parameter_declarator
1296 (cp_decl_specifier_seq *, cp_declarator *, tree);
1297 static cp_declarator *make_ptrmem_declarator
1298 (cp_cv_quals, tree, cp_declarator *, tree);
1299
1300 /* An erroneous declarator. */
1301 static cp_declarator *cp_error_declarator;
1302
1303 /* The obstack on which declarators and related data structures are
1304 allocated. */
1305 static struct obstack declarator_obstack;
1306
1307 /* Alloc BYTES from the declarator memory pool. */
1308
1309 static inline void *
1310 alloc_declarator (size_t bytes)
1311 {
1312 return obstack_alloc (&declarator_obstack, bytes);
1313 }
1314
1315 /* Allocate a declarator of the indicated KIND. Clear fields that are
1316 common to all declarators. */
1317
1318 static cp_declarator *
1319 make_declarator (cp_declarator_kind kind)
1320 {
1321 cp_declarator *declarator;
1322
1323 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1324 declarator->kind = kind;
1325 declarator->attributes = NULL_TREE;
1326 declarator->std_attributes = NULL_TREE;
1327 declarator->declarator = NULL;
1328 declarator->parameter_pack_p = false;
1329 declarator->id_loc = UNKNOWN_LOCATION;
1330
1331 return declarator;
1332 }
1333
1334 /* Make a declarator for a generalized identifier. If
1335 QUALIFYING_SCOPE is non-NULL, the identifier is
1336 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1337 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1338 is, if any. */
1339
1340 static cp_declarator *
1341 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1342 special_function_kind sfk)
1343 {
1344 cp_declarator *declarator;
1345
1346 /* It is valid to write:
1347
1348 class C { void f(); };
1349 typedef C D;
1350 void D::f();
1351
1352 The standard is not clear about whether `typedef const C D' is
1353 legal; as of 2002-09-15 the committee is considering that
1354 question. EDG 3.0 allows that syntax. Therefore, we do as
1355 well. */
1356 if (qualifying_scope && TYPE_P (qualifying_scope))
1357 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1358
1359 gcc_assert (identifier_p (unqualified_name)
1360 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1361 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1362
1363 declarator = make_declarator (cdk_id);
1364 declarator->u.id.qualifying_scope = qualifying_scope;
1365 declarator->u.id.unqualified_name = unqualified_name;
1366 declarator->u.id.sfk = sfk;
1367
1368 return declarator;
1369 }
1370
1371 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1372 of modifiers such as const or volatile to apply to the pointer
1373 type, represented as identifiers. ATTRIBUTES represent the attributes that
1374 appertain to the pointer or reference. */
1375
1376 cp_declarator *
1377 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1378 tree attributes)
1379 {
1380 cp_declarator *declarator;
1381
1382 declarator = make_declarator (cdk_pointer);
1383 declarator->declarator = target;
1384 declarator->u.pointer.qualifiers = cv_qualifiers;
1385 declarator->u.pointer.class_type = NULL_TREE;
1386 if (target)
1387 {
1388 declarator->id_loc = target->id_loc;
1389 declarator->parameter_pack_p = target->parameter_pack_p;
1390 target->parameter_pack_p = false;
1391 }
1392 else
1393 declarator->parameter_pack_p = false;
1394
1395 declarator->std_attributes = attributes;
1396
1397 return declarator;
1398 }
1399
1400 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1401 represent the attributes that appertain to the pointer or
1402 reference. */
1403
1404 cp_declarator *
1405 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1406 bool rvalue_ref, tree attributes)
1407 {
1408 cp_declarator *declarator;
1409
1410 declarator = make_declarator (cdk_reference);
1411 declarator->declarator = target;
1412 declarator->u.reference.qualifiers = cv_qualifiers;
1413 declarator->u.reference.rvalue_ref = rvalue_ref;
1414 if (target)
1415 {
1416 declarator->id_loc = target->id_loc;
1417 declarator->parameter_pack_p = target->parameter_pack_p;
1418 target->parameter_pack_p = false;
1419 }
1420 else
1421 declarator->parameter_pack_p = false;
1422
1423 declarator->std_attributes = attributes;
1424
1425 return declarator;
1426 }
1427
1428 /* Like make_pointer_declarator -- but for a pointer to a non-static
1429 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1430 appertain to the pointer or reference. */
1431
1432 cp_declarator *
1433 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1434 cp_declarator *pointee,
1435 tree attributes)
1436 {
1437 cp_declarator *declarator;
1438
1439 declarator = make_declarator (cdk_ptrmem);
1440 declarator->declarator = pointee;
1441 declarator->u.pointer.qualifiers = cv_qualifiers;
1442 declarator->u.pointer.class_type = class_type;
1443
1444 if (pointee)
1445 {
1446 declarator->parameter_pack_p = pointee->parameter_pack_p;
1447 pointee->parameter_pack_p = false;
1448 }
1449 else
1450 declarator->parameter_pack_p = false;
1451
1452 declarator->std_attributes = attributes;
1453
1454 return declarator;
1455 }
1456
1457 /* Make a declarator for the function given by TARGET, with the
1458 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1459 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1460 indicates what exceptions can be thrown. */
1461
1462 cp_declarator *
1463 make_call_declarator (cp_declarator *target,
1464 tree parms,
1465 cp_cv_quals cv_qualifiers,
1466 cp_virt_specifiers virt_specifiers,
1467 cp_ref_qualifier ref_qualifier,
1468 tree exception_specification,
1469 tree late_return_type)
1470 {
1471 cp_declarator *declarator;
1472
1473 declarator = make_declarator (cdk_function);
1474 declarator->declarator = target;
1475 declarator->u.function.parameters = parms;
1476 declarator->u.function.qualifiers = cv_qualifiers;
1477 declarator->u.function.virt_specifiers = virt_specifiers;
1478 declarator->u.function.ref_qualifier = ref_qualifier;
1479 declarator->u.function.exception_specification = exception_specification;
1480 declarator->u.function.late_return_type = late_return_type;
1481 if (target)
1482 {
1483 declarator->id_loc = target->id_loc;
1484 declarator->parameter_pack_p = target->parameter_pack_p;
1485 target->parameter_pack_p = false;
1486 }
1487 else
1488 declarator->parameter_pack_p = false;
1489
1490 return declarator;
1491 }
1492
1493 /* Make a declarator for an array of BOUNDS elements, each of which is
1494 defined by ELEMENT. */
1495
1496 cp_declarator *
1497 make_array_declarator (cp_declarator *element, tree bounds)
1498 {
1499 cp_declarator *declarator;
1500
1501 declarator = make_declarator (cdk_array);
1502 declarator->declarator = element;
1503 declarator->u.array.bounds = bounds;
1504 if (element)
1505 {
1506 declarator->id_loc = element->id_loc;
1507 declarator->parameter_pack_p = element->parameter_pack_p;
1508 element->parameter_pack_p = false;
1509 }
1510 else
1511 declarator->parameter_pack_p = false;
1512
1513 return declarator;
1514 }
1515
1516 /* Determine whether the declarator we've seen so far can be a
1517 parameter pack, when followed by an ellipsis. */
1518 static bool
1519 declarator_can_be_parameter_pack (cp_declarator *declarator)
1520 {
1521 /* Search for a declarator name, or any other declarator that goes
1522 after the point where the ellipsis could appear in a parameter
1523 pack. If we find any of these, then this declarator can not be
1524 made into a parameter pack. */
1525 bool found = false;
1526 while (declarator && !found)
1527 {
1528 switch ((int)declarator->kind)
1529 {
1530 case cdk_id:
1531 case cdk_array:
1532 found = true;
1533 break;
1534
1535 case cdk_error:
1536 return true;
1537
1538 default:
1539 declarator = declarator->declarator;
1540 break;
1541 }
1542 }
1543
1544 return !found;
1545 }
1546
1547 cp_parameter_declarator *no_parameters;
1548
1549 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1550 DECLARATOR and DEFAULT_ARGUMENT. */
1551
1552 cp_parameter_declarator *
1553 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1554 cp_declarator *declarator,
1555 tree default_argument)
1556 {
1557 cp_parameter_declarator *parameter;
1558
1559 parameter = ((cp_parameter_declarator *)
1560 alloc_declarator (sizeof (cp_parameter_declarator)));
1561 parameter->next = NULL;
1562 if (decl_specifiers)
1563 parameter->decl_specifiers = *decl_specifiers;
1564 else
1565 clear_decl_specs (&parameter->decl_specifiers);
1566 parameter->declarator = declarator;
1567 parameter->default_argument = default_argument;
1568 parameter->ellipsis_p = false;
1569
1570 return parameter;
1571 }
1572
1573 /* Returns true iff DECLARATOR is a declaration for a function. */
1574
1575 static bool
1576 function_declarator_p (const cp_declarator *declarator)
1577 {
1578 while (declarator)
1579 {
1580 if (declarator->kind == cdk_function
1581 && declarator->declarator->kind == cdk_id)
1582 return true;
1583 if (declarator->kind == cdk_id
1584 || declarator->kind == cdk_error)
1585 return false;
1586 declarator = declarator->declarator;
1587 }
1588 return false;
1589 }
1590
1591 /* The parser. */
1592
1593 /* Overview
1594 --------
1595
1596 A cp_parser parses the token stream as specified by the C++
1597 grammar. Its job is purely parsing, not semantic analysis. For
1598 example, the parser breaks the token stream into declarators,
1599 expressions, statements, and other similar syntactic constructs.
1600 It does not check that the types of the expressions on either side
1601 of an assignment-statement are compatible, or that a function is
1602 not declared with a parameter of type `void'.
1603
1604 The parser invokes routines elsewhere in the compiler to perform
1605 semantic analysis and to build up the abstract syntax tree for the
1606 code processed.
1607
1608 The parser (and the template instantiation code, which is, in a
1609 way, a close relative of parsing) are the only parts of the
1610 compiler that should be calling push_scope and pop_scope, or
1611 related functions. The parser (and template instantiation code)
1612 keeps track of what scope is presently active; everything else
1613 should simply honor that. (The code that generates static
1614 initializers may also need to set the scope, in order to check
1615 access control correctly when emitting the initializers.)
1616
1617 Methodology
1618 -----------
1619
1620 The parser is of the standard recursive-descent variety. Upcoming
1621 tokens in the token stream are examined in order to determine which
1622 production to use when parsing a non-terminal. Some C++ constructs
1623 require arbitrary look ahead to disambiguate. For example, it is
1624 impossible, in the general case, to tell whether a statement is an
1625 expression or declaration without scanning the entire statement.
1626 Therefore, the parser is capable of "parsing tentatively." When the
1627 parser is not sure what construct comes next, it enters this mode.
1628 Then, while we attempt to parse the construct, the parser queues up
1629 error messages, rather than issuing them immediately, and saves the
1630 tokens it consumes. If the construct is parsed successfully, the
1631 parser "commits", i.e., it issues any queued error messages and
1632 the tokens that were being preserved are permanently discarded.
1633 If, however, the construct is not parsed successfully, the parser
1634 rolls back its state completely so that it can resume parsing using
1635 a different alternative.
1636
1637 Future Improvements
1638 -------------------
1639
1640 The performance of the parser could probably be improved substantially.
1641 We could often eliminate the need to parse tentatively by looking ahead
1642 a little bit. In some places, this approach might not entirely eliminate
1643 the need to parse tentatively, but it might still speed up the average
1644 case. */
1645
1646 /* Flags that are passed to some parsing functions. These values can
1647 be bitwise-ored together. */
1648
1649 enum
1650 {
1651 /* No flags. */
1652 CP_PARSER_FLAGS_NONE = 0x0,
1653 /* The construct is optional. If it is not present, then no error
1654 should be issued. */
1655 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1656 /* When parsing a type-specifier, treat user-defined type-names
1657 as non-type identifiers. */
1658 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1659 /* When parsing a type-specifier, do not try to parse a class-specifier
1660 or enum-specifier. */
1661 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1662 /* When parsing a decl-specifier-seq, only allow type-specifier or
1663 constexpr. */
1664 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1665 };
1666
1667 /* This type is used for parameters and variables which hold
1668 combinations of the above flags. */
1669 typedef int cp_parser_flags;
1670
1671 /* The different kinds of declarators we want to parse. */
1672
1673 typedef enum cp_parser_declarator_kind
1674 {
1675 /* We want an abstract declarator. */
1676 CP_PARSER_DECLARATOR_ABSTRACT,
1677 /* We want a named declarator. */
1678 CP_PARSER_DECLARATOR_NAMED,
1679 /* We don't mind, but the name must be an unqualified-id. */
1680 CP_PARSER_DECLARATOR_EITHER
1681 } cp_parser_declarator_kind;
1682
1683 /* The precedence values used to parse binary expressions. The minimum value
1684 of PREC must be 1, because zero is reserved to quickly discriminate
1685 binary operators from other tokens. */
1686
1687 enum cp_parser_prec
1688 {
1689 PREC_NOT_OPERATOR,
1690 PREC_LOGICAL_OR_EXPRESSION,
1691 PREC_LOGICAL_AND_EXPRESSION,
1692 PREC_INCLUSIVE_OR_EXPRESSION,
1693 PREC_EXCLUSIVE_OR_EXPRESSION,
1694 PREC_AND_EXPRESSION,
1695 PREC_EQUALITY_EXPRESSION,
1696 PREC_RELATIONAL_EXPRESSION,
1697 PREC_SHIFT_EXPRESSION,
1698 PREC_ADDITIVE_EXPRESSION,
1699 PREC_MULTIPLICATIVE_EXPRESSION,
1700 PREC_PM_EXPRESSION,
1701 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1702 };
1703
1704 /* A mapping from a token type to a corresponding tree node type, with a
1705 precedence value. */
1706
1707 typedef struct cp_parser_binary_operations_map_node
1708 {
1709 /* The token type. */
1710 enum cpp_ttype token_type;
1711 /* The corresponding tree code. */
1712 enum tree_code tree_type;
1713 /* The precedence of this operator. */
1714 enum cp_parser_prec prec;
1715 } cp_parser_binary_operations_map_node;
1716
1717 typedef struct cp_parser_expression_stack_entry
1718 {
1719 /* Left hand side of the binary operation we are currently
1720 parsing. */
1721 tree lhs;
1722 /* Original tree code for left hand side, if it was a binary
1723 expression itself (used for -Wparentheses). */
1724 enum tree_code lhs_type;
1725 /* Tree code for the binary operation we are parsing. */
1726 enum tree_code tree_type;
1727 /* Precedence of the binary operation we are parsing. */
1728 enum cp_parser_prec prec;
1729 /* Location of the binary operation we are parsing. */
1730 location_t loc;
1731 } cp_parser_expression_stack_entry;
1732
1733 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1734 entries because precedence levels on the stack are monotonically
1735 increasing. */
1736 typedef struct cp_parser_expression_stack_entry
1737 cp_parser_expression_stack[NUM_PREC_VALUES];
1738
1739 /* Prototypes. */
1740
1741 /* Constructors and destructors. */
1742
1743 static cp_parser_context *cp_parser_context_new
1744 (cp_parser_context *);
1745
1746 /* Class variables. */
1747
1748 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1749
1750 /* The operator-precedence table used by cp_parser_binary_expression.
1751 Transformed into an associative array (binops_by_token) by
1752 cp_parser_new. */
1753
1754 static const cp_parser_binary_operations_map_node binops[] = {
1755 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1756 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1757
1758 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1759 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1760 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1761
1762 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1763 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1764
1765 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1766 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1767
1768 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1769 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1770 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1771 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1772
1773 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1774 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1775
1776 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1777
1778 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1779
1780 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1781
1782 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1783
1784 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1785 };
1786
1787 /* The same as binops, but initialized by cp_parser_new so that
1788 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1789 for speed. */
1790 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1791
1792 /* Constructors and destructors. */
1793
1794 /* Construct a new context. The context below this one on the stack
1795 is given by NEXT. */
1796
1797 static cp_parser_context *
1798 cp_parser_context_new (cp_parser_context* next)
1799 {
1800 cp_parser_context *context;
1801
1802 /* Allocate the storage. */
1803 if (cp_parser_context_free_list != NULL)
1804 {
1805 /* Pull the first entry from the free list. */
1806 context = cp_parser_context_free_list;
1807 cp_parser_context_free_list = context->next;
1808 memset (context, 0, sizeof (*context));
1809 }
1810 else
1811 context = ggc_alloc_cleared_cp_parser_context ();
1812
1813 /* No errors have occurred yet in this context. */
1814 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1815 /* If this is not the bottommost context, copy information that we
1816 need from the previous context. */
1817 if (next)
1818 {
1819 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1820 expression, then we are parsing one in this context, too. */
1821 context->object_type = next->object_type;
1822 /* Thread the stack. */
1823 context->next = next;
1824 }
1825
1826 return context;
1827 }
1828
1829 /* Managing the unparsed function queues. */
1830
1831 #define unparsed_funs_with_default_args \
1832 parser->unparsed_queues->last ().funs_with_default_args
1833 #define unparsed_funs_with_definitions \
1834 parser->unparsed_queues->last ().funs_with_definitions
1835 #define unparsed_nsdmis \
1836 parser->unparsed_queues->last ().nsdmis
1837
1838 static void
1839 push_unparsed_function_queues (cp_parser *parser)
1840 {
1841 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1842 vec_safe_push (parser->unparsed_queues, e);
1843 }
1844
1845 static void
1846 pop_unparsed_function_queues (cp_parser *parser)
1847 {
1848 release_tree_vector (unparsed_funs_with_definitions);
1849 parser->unparsed_queues->pop ();
1850 }
1851
1852 /* Prototypes. */
1853
1854 /* Constructors and destructors. */
1855
1856 static cp_parser *cp_parser_new
1857 (void);
1858
1859 /* Routines to parse various constructs.
1860
1861 Those that return `tree' will return the error_mark_node (rather
1862 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1863 Sometimes, they will return an ordinary node if error-recovery was
1864 attempted, even though a parse error occurred. So, to check
1865 whether or not a parse error occurred, you should always use
1866 cp_parser_error_occurred. If the construct is optional (indicated
1867 either by an `_opt' in the name of the function that does the
1868 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1869 the construct is not present. */
1870
1871 /* Lexical conventions [gram.lex] */
1872
1873 static tree cp_parser_identifier
1874 (cp_parser *);
1875 static tree cp_parser_string_literal
1876 (cp_parser *, bool, bool);
1877 static tree cp_parser_userdef_char_literal
1878 (cp_parser *);
1879 static tree cp_parser_userdef_string_literal
1880 (cp_token *);
1881 static tree cp_parser_userdef_numeric_literal
1882 (cp_parser *);
1883
1884 /* Basic concepts [gram.basic] */
1885
1886 static bool cp_parser_translation_unit
1887 (cp_parser *);
1888
1889 /* Expressions [gram.expr] */
1890
1891 static tree cp_parser_primary_expression
1892 (cp_parser *, bool, bool, bool, cp_id_kind *);
1893 static tree cp_parser_id_expression
1894 (cp_parser *, bool, bool, bool *, bool, bool);
1895 static tree cp_parser_unqualified_id
1896 (cp_parser *, bool, bool, bool, bool);
1897 static tree cp_parser_nested_name_specifier_opt
1898 (cp_parser *, bool, bool, bool, bool);
1899 static tree cp_parser_nested_name_specifier
1900 (cp_parser *, bool, bool, bool, bool);
1901 static tree cp_parser_qualifying_entity
1902 (cp_parser *, bool, bool, bool, bool, bool);
1903 static tree cp_parser_postfix_expression
1904 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1905 static tree cp_parser_postfix_open_square_expression
1906 (cp_parser *, tree, bool, bool);
1907 static tree cp_parser_postfix_dot_deref_expression
1908 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1909 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1910 (cp_parser *, int, bool, bool, bool *);
1911 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1912 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1913 static void cp_parser_pseudo_destructor_name
1914 (cp_parser *, tree, tree *, tree *);
1915 static tree cp_parser_unary_expression
1916 (cp_parser *, bool, bool, cp_id_kind *);
1917 static enum tree_code cp_parser_unary_operator
1918 (cp_token *);
1919 static tree cp_parser_new_expression
1920 (cp_parser *);
1921 static vec<tree, va_gc> *cp_parser_new_placement
1922 (cp_parser *);
1923 static tree cp_parser_new_type_id
1924 (cp_parser *, tree *);
1925 static cp_declarator *cp_parser_new_declarator_opt
1926 (cp_parser *);
1927 static cp_declarator *cp_parser_direct_new_declarator
1928 (cp_parser *);
1929 static vec<tree, va_gc> *cp_parser_new_initializer
1930 (cp_parser *);
1931 static tree cp_parser_delete_expression
1932 (cp_parser *);
1933 static tree cp_parser_cast_expression
1934 (cp_parser *, bool, bool, bool, cp_id_kind *);
1935 static tree cp_parser_binary_expression
1936 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1937 static tree cp_parser_question_colon_clause
1938 (cp_parser *, tree);
1939 static tree cp_parser_assignment_expression
1940 (cp_parser *, bool, cp_id_kind *);
1941 static enum tree_code cp_parser_assignment_operator_opt
1942 (cp_parser *);
1943 static tree cp_parser_expression
1944 (cp_parser *, bool, cp_id_kind *);
1945 static tree cp_parser_expression
1946 (cp_parser *, bool, bool, cp_id_kind *);
1947 static tree cp_parser_constant_expression
1948 (cp_parser *, bool, bool *);
1949 static tree cp_parser_builtin_offsetof
1950 (cp_parser *);
1951 static tree cp_parser_lambda_expression
1952 (cp_parser *);
1953 static void cp_parser_lambda_introducer
1954 (cp_parser *, tree);
1955 static bool cp_parser_lambda_declarator_opt
1956 (cp_parser *, tree);
1957 static void cp_parser_lambda_body
1958 (cp_parser *, tree);
1959
1960 /* Statements [gram.stmt.stmt] */
1961
1962 static void cp_parser_statement
1963 (cp_parser *, tree, bool, bool *);
1964 static void cp_parser_label_for_labeled_statement
1965 (cp_parser *, tree);
1966 static tree cp_parser_expression_statement
1967 (cp_parser *, tree);
1968 static tree cp_parser_compound_statement
1969 (cp_parser *, tree, bool, bool);
1970 static void cp_parser_statement_seq_opt
1971 (cp_parser *, tree);
1972 static tree cp_parser_selection_statement
1973 (cp_parser *, bool *);
1974 static tree cp_parser_condition
1975 (cp_parser *);
1976 static tree cp_parser_iteration_statement
1977 (cp_parser *, bool);
1978 static bool cp_parser_for_init_statement
1979 (cp_parser *, tree *decl);
1980 static tree cp_parser_for
1981 (cp_parser *, bool);
1982 static tree cp_parser_c_for
1983 (cp_parser *, tree, tree, bool);
1984 static tree cp_parser_range_for
1985 (cp_parser *, tree, tree, tree, bool);
1986 static void do_range_for_auto_deduction
1987 (tree, tree);
1988 static tree cp_parser_perform_range_for_lookup
1989 (tree, tree *, tree *);
1990 static tree cp_parser_range_for_member_function
1991 (tree, tree);
1992 static tree cp_parser_jump_statement
1993 (cp_parser *);
1994 static void cp_parser_declaration_statement
1995 (cp_parser *);
1996
1997 static tree cp_parser_implicitly_scoped_statement
1998 (cp_parser *, bool *);
1999 static void cp_parser_already_scoped_statement
2000 (cp_parser *);
2001
2002 /* Declarations [gram.dcl.dcl] */
2003
2004 static void cp_parser_declaration_seq_opt
2005 (cp_parser *);
2006 static void cp_parser_declaration
2007 (cp_parser *);
2008 static void cp_parser_block_declaration
2009 (cp_parser *, bool);
2010 static void cp_parser_simple_declaration
2011 (cp_parser *, bool, tree *);
2012 static void cp_parser_decl_specifier_seq
2013 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2014 static tree cp_parser_storage_class_specifier_opt
2015 (cp_parser *);
2016 static tree cp_parser_function_specifier_opt
2017 (cp_parser *, cp_decl_specifier_seq *);
2018 static tree cp_parser_type_specifier
2019 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2020 int *, bool *);
2021 static tree cp_parser_simple_type_specifier
2022 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2023 static tree cp_parser_type_name
2024 (cp_parser *);
2025 static tree cp_parser_nonclass_name
2026 (cp_parser* parser);
2027 static tree cp_parser_elaborated_type_specifier
2028 (cp_parser *, bool, bool);
2029 static tree cp_parser_enum_specifier
2030 (cp_parser *);
2031 static void cp_parser_enumerator_list
2032 (cp_parser *, tree);
2033 static void cp_parser_enumerator_definition
2034 (cp_parser *, tree);
2035 static tree cp_parser_namespace_name
2036 (cp_parser *);
2037 static void cp_parser_namespace_definition
2038 (cp_parser *);
2039 static void cp_parser_namespace_body
2040 (cp_parser *);
2041 static tree cp_parser_qualified_namespace_specifier
2042 (cp_parser *);
2043 static void cp_parser_namespace_alias_definition
2044 (cp_parser *);
2045 static bool cp_parser_using_declaration
2046 (cp_parser *, bool);
2047 static void cp_parser_using_directive
2048 (cp_parser *);
2049 static tree cp_parser_alias_declaration
2050 (cp_parser *);
2051 static void cp_parser_asm_definition
2052 (cp_parser *);
2053 static void cp_parser_linkage_specification
2054 (cp_parser *);
2055 static void cp_parser_static_assert
2056 (cp_parser *, bool);
2057 static tree cp_parser_decltype
2058 (cp_parser *);
2059
2060 /* Declarators [gram.dcl.decl] */
2061
2062 static tree cp_parser_init_declarator
2063 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2064 static cp_declarator *cp_parser_declarator
2065 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2066 static cp_declarator *cp_parser_direct_declarator
2067 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2068 static enum tree_code cp_parser_ptr_operator
2069 (cp_parser *, tree *, cp_cv_quals *, tree *);
2070 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2071 (cp_parser *);
2072 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2073 (cp_parser *);
2074 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2075 (cp_parser *);
2076 static tree cp_parser_late_return_type_opt
2077 (cp_parser *, cp_declarator *, cp_cv_quals);
2078 static tree cp_parser_declarator_id
2079 (cp_parser *, bool);
2080 static tree cp_parser_type_id
2081 (cp_parser *);
2082 static tree cp_parser_template_type_arg
2083 (cp_parser *);
2084 static tree cp_parser_trailing_type_id (cp_parser *);
2085 static tree cp_parser_type_id_1
2086 (cp_parser *, bool, bool);
2087 static void cp_parser_type_specifier_seq
2088 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2089 static tree cp_parser_parameter_declaration_clause
2090 (cp_parser *);
2091 static tree cp_parser_parameter_declaration_list
2092 (cp_parser *, bool *);
2093 static cp_parameter_declarator *cp_parser_parameter_declaration
2094 (cp_parser *, bool, bool *);
2095 static tree cp_parser_default_argument
2096 (cp_parser *, bool);
2097 static void cp_parser_function_body
2098 (cp_parser *, bool);
2099 static tree cp_parser_initializer
2100 (cp_parser *, bool *, bool *);
2101 static tree cp_parser_initializer_clause
2102 (cp_parser *, bool *);
2103 static tree cp_parser_braced_list
2104 (cp_parser*, bool*);
2105 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2106 (cp_parser *, bool *);
2107
2108 static bool cp_parser_ctor_initializer_opt_and_function_body
2109 (cp_parser *, bool);
2110
2111 static tree cp_parser_late_parsing_omp_declare_simd
2112 (cp_parser *, tree);
2113
2114 static tree synthesize_implicit_template_parm
2115 (cp_parser *);
2116 static tree finish_fully_implicit_template
2117 (cp_parser *, tree);
2118
2119 /* Classes [gram.class] */
2120
2121 static tree cp_parser_class_name
2122 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2123 static tree cp_parser_class_specifier
2124 (cp_parser *);
2125 static tree cp_parser_class_head
2126 (cp_parser *, bool *);
2127 static enum tag_types cp_parser_class_key
2128 (cp_parser *);
2129 static void cp_parser_member_specification_opt
2130 (cp_parser *);
2131 static void cp_parser_member_declaration
2132 (cp_parser *);
2133 static tree cp_parser_pure_specifier
2134 (cp_parser *);
2135 static tree cp_parser_constant_initializer
2136 (cp_parser *);
2137
2138 /* Derived classes [gram.class.derived] */
2139
2140 static tree cp_parser_base_clause
2141 (cp_parser *);
2142 static tree cp_parser_base_specifier
2143 (cp_parser *);
2144
2145 /* Special member functions [gram.special] */
2146
2147 static tree cp_parser_conversion_function_id
2148 (cp_parser *);
2149 static tree cp_parser_conversion_type_id
2150 (cp_parser *);
2151 static cp_declarator *cp_parser_conversion_declarator_opt
2152 (cp_parser *);
2153 static bool cp_parser_ctor_initializer_opt
2154 (cp_parser *);
2155 static void cp_parser_mem_initializer_list
2156 (cp_parser *);
2157 static tree cp_parser_mem_initializer
2158 (cp_parser *);
2159 static tree cp_parser_mem_initializer_id
2160 (cp_parser *);
2161
2162 /* Overloading [gram.over] */
2163
2164 static tree cp_parser_operator_function_id
2165 (cp_parser *);
2166 static tree cp_parser_operator
2167 (cp_parser *);
2168
2169 /* Templates [gram.temp] */
2170
2171 static void cp_parser_template_declaration
2172 (cp_parser *, bool);
2173 static tree cp_parser_template_parameter_list
2174 (cp_parser *);
2175 static tree cp_parser_template_parameter
2176 (cp_parser *, bool *, bool *);
2177 static tree cp_parser_type_parameter
2178 (cp_parser *, bool *);
2179 static tree cp_parser_template_id
2180 (cp_parser *, bool, bool, enum tag_types, bool);
2181 static tree cp_parser_template_name
2182 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2183 static tree cp_parser_template_argument_list
2184 (cp_parser *);
2185 static tree cp_parser_template_argument
2186 (cp_parser *);
2187 static void cp_parser_explicit_instantiation
2188 (cp_parser *);
2189 static void cp_parser_explicit_specialization
2190 (cp_parser *);
2191
2192 /* Exception handling [gram.exception] */
2193
2194 static tree cp_parser_try_block
2195 (cp_parser *);
2196 static bool cp_parser_function_try_block
2197 (cp_parser *);
2198 static void cp_parser_handler_seq
2199 (cp_parser *);
2200 static void cp_parser_handler
2201 (cp_parser *);
2202 static tree cp_parser_exception_declaration
2203 (cp_parser *);
2204 static tree cp_parser_throw_expression
2205 (cp_parser *);
2206 static tree cp_parser_exception_specification_opt
2207 (cp_parser *);
2208 static tree cp_parser_type_id_list
2209 (cp_parser *);
2210
2211 /* GNU Extensions */
2212
2213 static tree cp_parser_asm_specification_opt
2214 (cp_parser *);
2215 static tree cp_parser_asm_operand_list
2216 (cp_parser *);
2217 static tree cp_parser_asm_clobber_list
2218 (cp_parser *);
2219 static tree cp_parser_asm_label_list
2220 (cp_parser *);
2221 static bool cp_next_tokens_can_be_attribute_p
2222 (cp_parser *);
2223 static bool cp_next_tokens_can_be_gnu_attribute_p
2224 (cp_parser *);
2225 static bool cp_next_tokens_can_be_std_attribute_p
2226 (cp_parser *);
2227 static bool cp_nth_tokens_can_be_std_attribute_p
2228 (cp_parser *, size_t);
2229 static bool cp_nth_tokens_can_be_gnu_attribute_p
2230 (cp_parser *, size_t);
2231 static bool cp_nth_tokens_can_be_attribute_p
2232 (cp_parser *, size_t);
2233 static tree cp_parser_attributes_opt
2234 (cp_parser *);
2235 static tree cp_parser_gnu_attributes_opt
2236 (cp_parser *);
2237 static tree cp_parser_gnu_attribute_list
2238 (cp_parser *);
2239 static tree cp_parser_std_attribute
2240 (cp_parser *);
2241 static tree cp_parser_std_attribute_spec
2242 (cp_parser *);
2243 static tree cp_parser_std_attribute_spec_seq
2244 (cp_parser *);
2245 static bool cp_parser_extension_opt
2246 (cp_parser *, int *);
2247 static void cp_parser_label_declaration
2248 (cp_parser *);
2249
2250 /* Transactional Memory Extensions */
2251
2252 static tree cp_parser_transaction
2253 (cp_parser *, enum rid);
2254 static tree cp_parser_transaction_expression
2255 (cp_parser *, enum rid);
2256 static bool cp_parser_function_transaction
2257 (cp_parser *, enum rid);
2258 static tree cp_parser_transaction_cancel
2259 (cp_parser *);
2260
2261 enum pragma_context {
2262 pragma_external,
2263 pragma_member,
2264 pragma_objc_icode,
2265 pragma_stmt,
2266 pragma_compound
2267 };
2268 static bool cp_parser_pragma
2269 (cp_parser *, enum pragma_context);
2270
2271 /* Objective-C++ Productions */
2272
2273 static tree cp_parser_objc_message_receiver
2274 (cp_parser *);
2275 static tree cp_parser_objc_message_args
2276 (cp_parser *);
2277 static tree cp_parser_objc_message_expression
2278 (cp_parser *);
2279 static tree cp_parser_objc_encode_expression
2280 (cp_parser *);
2281 static tree cp_parser_objc_defs_expression
2282 (cp_parser *);
2283 static tree cp_parser_objc_protocol_expression
2284 (cp_parser *);
2285 static tree cp_parser_objc_selector_expression
2286 (cp_parser *);
2287 static tree cp_parser_objc_expression
2288 (cp_parser *);
2289 static bool cp_parser_objc_selector_p
2290 (enum cpp_ttype);
2291 static tree cp_parser_objc_selector
2292 (cp_parser *);
2293 static tree cp_parser_objc_protocol_refs_opt
2294 (cp_parser *);
2295 static void cp_parser_objc_declaration
2296 (cp_parser *, tree);
2297 static tree cp_parser_objc_statement
2298 (cp_parser *);
2299 static bool cp_parser_objc_valid_prefix_attributes
2300 (cp_parser *, tree *);
2301 static void cp_parser_objc_at_property_declaration
2302 (cp_parser *) ;
2303 static void cp_parser_objc_at_synthesize_declaration
2304 (cp_parser *) ;
2305 static void cp_parser_objc_at_dynamic_declaration
2306 (cp_parser *) ;
2307 static tree cp_parser_objc_struct_declaration
2308 (cp_parser *) ;
2309
2310 /* Utility Routines */
2311
2312 static tree cp_parser_lookup_name
2313 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2314 static tree cp_parser_lookup_name_simple
2315 (cp_parser *, tree, location_t);
2316 static tree cp_parser_maybe_treat_template_as_class
2317 (tree, bool);
2318 static bool cp_parser_check_declarator_template_parameters
2319 (cp_parser *, cp_declarator *, location_t);
2320 static bool cp_parser_check_template_parameters
2321 (cp_parser *, unsigned, location_t, cp_declarator *);
2322 static tree cp_parser_simple_cast_expression
2323 (cp_parser *);
2324 static tree cp_parser_global_scope_opt
2325 (cp_parser *, bool);
2326 static bool cp_parser_constructor_declarator_p
2327 (cp_parser *, bool);
2328 static tree cp_parser_function_definition_from_specifiers_and_declarator
2329 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2330 static tree cp_parser_function_definition_after_declarator
2331 (cp_parser *, bool);
2332 static void cp_parser_template_declaration_after_export
2333 (cp_parser *, bool);
2334 static void cp_parser_perform_template_parameter_access_checks
2335 (vec<deferred_access_check, va_gc> *);
2336 static tree cp_parser_single_declaration
2337 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2338 static tree cp_parser_functional_cast
2339 (cp_parser *, tree);
2340 static tree cp_parser_save_member_function_body
2341 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2342 static tree cp_parser_save_nsdmi
2343 (cp_parser *);
2344 static tree cp_parser_enclosed_template_argument_list
2345 (cp_parser *);
2346 static void cp_parser_save_default_args
2347 (cp_parser *, tree);
2348 static void cp_parser_late_parsing_for_member
2349 (cp_parser *, tree);
2350 static tree cp_parser_late_parse_one_default_arg
2351 (cp_parser *, tree, tree, tree);
2352 static void cp_parser_late_parsing_nsdmi
2353 (cp_parser *, tree);
2354 static void cp_parser_late_parsing_default_args
2355 (cp_parser *, tree);
2356 static tree cp_parser_sizeof_operand
2357 (cp_parser *, enum rid);
2358 static tree cp_parser_trait_expr
2359 (cp_parser *, enum rid);
2360 static bool cp_parser_declares_only_class_p
2361 (cp_parser *);
2362 static void cp_parser_set_storage_class
2363 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2364 static void cp_parser_set_decl_spec_type
2365 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2366 static void set_and_check_decl_spec_loc
2367 (cp_decl_specifier_seq *decl_specs,
2368 cp_decl_spec ds, cp_token *);
2369 static bool cp_parser_friend_p
2370 (const cp_decl_specifier_seq *);
2371 static void cp_parser_required_error
2372 (cp_parser *, required_token, bool);
2373 static cp_token *cp_parser_require
2374 (cp_parser *, enum cpp_ttype, required_token);
2375 static cp_token *cp_parser_require_keyword
2376 (cp_parser *, enum rid, required_token);
2377 static bool cp_parser_token_starts_function_definition_p
2378 (cp_token *);
2379 static bool cp_parser_next_token_starts_class_definition_p
2380 (cp_parser *);
2381 static bool cp_parser_next_token_ends_template_argument_p
2382 (cp_parser *);
2383 static bool cp_parser_nth_token_starts_template_argument_list_p
2384 (cp_parser *, size_t);
2385 static enum tag_types cp_parser_token_is_class_key
2386 (cp_token *);
2387 static void cp_parser_check_class_key
2388 (enum tag_types, tree type);
2389 static void cp_parser_check_access_in_redeclaration
2390 (tree type, location_t location);
2391 static bool cp_parser_optional_template_keyword
2392 (cp_parser *);
2393 static void cp_parser_pre_parsed_nested_name_specifier
2394 (cp_parser *);
2395 static bool cp_parser_cache_group
2396 (cp_parser *, enum cpp_ttype, unsigned);
2397 static tree cp_parser_cache_defarg
2398 (cp_parser *parser, bool nsdmi);
2399 static void cp_parser_parse_tentatively
2400 (cp_parser *);
2401 static void cp_parser_commit_to_tentative_parse
2402 (cp_parser *);
2403 static void cp_parser_commit_to_topmost_tentative_parse
2404 (cp_parser *);
2405 static void cp_parser_abort_tentative_parse
2406 (cp_parser *);
2407 static bool cp_parser_parse_definitely
2408 (cp_parser *);
2409 static inline bool cp_parser_parsing_tentatively
2410 (cp_parser *);
2411 static bool cp_parser_uncommitted_to_tentative_parse_p
2412 (cp_parser *);
2413 static void cp_parser_error
2414 (cp_parser *, const char *);
2415 static void cp_parser_name_lookup_error
2416 (cp_parser *, tree, tree, name_lookup_error, location_t);
2417 static bool cp_parser_simulate_error
2418 (cp_parser *);
2419 static bool cp_parser_check_type_definition
2420 (cp_parser *);
2421 static void cp_parser_check_for_definition_in_return_type
2422 (cp_declarator *, tree, location_t type_location);
2423 static void cp_parser_check_for_invalid_template_id
2424 (cp_parser *, tree, enum tag_types, location_t location);
2425 static bool cp_parser_non_integral_constant_expression
2426 (cp_parser *, non_integral_constant);
2427 static void cp_parser_diagnose_invalid_type_name
2428 (cp_parser *, tree, tree, location_t);
2429 static bool cp_parser_parse_and_diagnose_invalid_type_name
2430 (cp_parser *);
2431 static int cp_parser_skip_to_closing_parenthesis
2432 (cp_parser *, bool, bool, bool);
2433 static void cp_parser_skip_to_end_of_statement
2434 (cp_parser *);
2435 static void cp_parser_consume_semicolon_at_end_of_statement
2436 (cp_parser *);
2437 static void cp_parser_skip_to_end_of_block_or_statement
2438 (cp_parser *);
2439 static bool cp_parser_skip_to_closing_brace
2440 (cp_parser *);
2441 static void cp_parser_skip_to_end_of_template_parameter_list
2442 (cp_parser *);
2443 static void cp_parser_skip_to_pragma_eol
2444 (cp_parser*, cp_token *);
2445 static bool cp_parser_error_occurred
2446 (cp_parser *);
2447 static bool cp_parser_allow_gnu_extensions_p
2448 (cp_parser *);
2449 static bool cp_parser_is_pure_string_literal
2450 (cp_token *);
2451 static bool cp_parser_is_string_literal
2452 (cp_token *);
2453 static bool cp_parser_is_keyword
2454 (cp_token *, enum rid);
2455 static tree cp_parser_make_typename_type
2456 (cp_parser *, tree, tree, location_t location);
2457 static cp_declarator * cp_parser_make_indirect_declarator
2458 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2459
2460 /* Returns nonzero if we are parsing tentatively. */
2461
2462 static inline bool
2463 cp_parser_parsing_tentatively (cp_parser* parser)
2464 {
2465 return parser->context->next != NULL;
2466 }
2467
2468 /* Returns nonzero if TOKEN is a string literal. */
2469
2470 static bool
2471 cp_parser_is_pure_string_literal (cp_token* token)
2472 {
2473 return (token->type == CPP_STRING ||
2474 token->type == CPP_STRING16 ||
2475 token->type == CPP_STRING32 ||
2476 token->type == CPP_WSTRING ||
2477 token->type == CPP_UTF8STRING);
2478 }
2479
2480 /* Returns nonzero if TOKEN is a string literal
2481 of a user-defined string literal. */
2482
2483 static bool
2484 cp_parser_is_string_literal (cp_token* token)
2485 {
2486 return (cp_parser_is_pure_string_literal (token) ||
2487 token->type == CPP_STRING_USERDEF ||
2488 token->type == CPP_STRING16_USERDEF ||
2489 token->type == CPP_STRING32_USERDEF ||
2490 token->type == CPP_WSTRING_USERDEF ||
2491 token->type == CPP_UTF8STRING_USERDEF);
2492 }
2493
2494 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2495
2496 static bool
2497 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2498 {
2499 return token->keyword == keyword;
2500 }
2501
2502 /* If not parsing tentatively, issue a diagnostic of the form
2503 FILE:LINE: MESSAGE before TOKEN
2504 where TOKEN is the next token in the input stream. MESSAGE
2505 (specified by the caller) is usually of the form "expected
2506 OTHER-TOKEN". */
2507
2508 static void
2509 cp_parser_error (cp_parser* parser, const char* gmsgid)
2510 {
2511 if (!cp_parser_simulate_error (parser))
2512 {
2513 cp_token *token = cp_lexer_peek_token (parser->lexer);
2514 /* This diagnostic makes more sense if it is tagged to the line
2515 of the token we just peeked at. */
2516 cp_lexer_set_source_position_from_token (token);
2517
2518 if (token->type == CPP_PRAGMA)
2519 {
2520 error_at (token->location,
2521 "%<#pragma%> is not allowed here");
2522 cp_parser_skip_to_pragma_eol (parser, token);
2523 return;
2524 }
2525
2526 c_parse_error (gmsgid,
2527 /* Because c_parser_error does not understand
2528 CPP_KEYWORD, keywords are treated like
2529 identifiers. */
2530 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2531 token->u.value, token->flags);
2532 }
2533 }
2534
2535 /* Issue an error about name-lookup failing. NAME is the
2536 IDENTIFIER_NODE DECL is the result of
2537 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2538 the thing that we hoped to find. */
2539
2540 static void
2541 cp_parser_name_lookup_error (cp_parser* parser,
2542 tree name,
2543 tree decl,
2544 name_lookup_error desired,
2545 location_t location)
2546 {
2547 /* If name lookup completely failed, tell the user that NAME was not
2548 declared. */
2549 if (decl == error_mark_node)
2550 {
2551 if (parser->scope && parser->scope != global_namespace)
2552 error_at (location, "%<%E::%E%> has not been declared",
2553 parser->scope, name);
2554 else if (parser->scope == global_namespace)
2555 error_at (location, "%<::%E%> has not been declared", name);
2556 else if (parser->object_scope
2557 && !CLASS_TYPE_P (parser->object_scope))
2558 error_at (location, "request for member %qE in non-class type %qT",
2559 name, parser->object_scope);
2560 else if (parser->object_scope)
2561 error_at (location, "%<%T::%E%> has not been declared",
2562 parser->object_scope, name);
2563 else
2564 error_at (location, "%qE has not been declared", name);
2565 }
2566 else if (parser->scope && parser->scope != global_namespace)
2567 {
2568 switch (desired)
2569 {
2570 case NLE_TYPE:
2571 error_at (location, "%<%E::%E%> is not a type",
2572 parser->scope, name);
2573 break;
2574 case NLE_CXX98:
2575 error_at (location, "%<%E::%E%> is not a class or namespace",
2576 parser->scope, name);
2577 break;
2578 case NLE_NOT_CXX98:
2579 error_at (location,
2580 "%<%E::%E%> is not a class, namespace, or enumeration",
2581 parser->scope, name);
2582 break;
2583 default:
2584 gcc_unreachable ();
2585
2586 }
2587 }
2588 else if (parser->scope == global_namespace)
2589 {
2590 switch (desired)
2591 {
2592 case NLE_TYPE:
2593 error_at (location, "%<::%E%> is not a type", name);
2594 break;
2595 case NLE_CXX98:
2596 error_at (location, "%<::%E%> is not a class or namespace", name);
2597 break;
2598 case NLE_NOT_CXX98:
2599 error_at (location,
2600 "%<::%E%> is not a class, namespace, or enumeration",
2601 name);
2602 break;
2603 default:
2604 gcc_unreachable ();
2605 }
2606 }
2607 else
2608 {
2609 switch (desired)
2610 {
2611 case NLE_TYPE:
2612 error_at (location, "%qE is not a type", name);
2613 break;
2614 case NLE_CXX98:
2615 error_at (location, "%qE is not a class or namespace", name);
2616 break;
2617 case NLE_NOT_CXX98:
2618 error_at (location,
2619 "%qE is not a class, namespace, or enumeration", name);
2620 break;
2621 default:
2622 gcc_unreachable ();
2623 }
2624 }
2625 }
2626
2627 /* If we are parsing tentatively, remember that an error has occurred
2628 during this tentative parse. Returns true if the error was
2629 simulated; false if a message should be issued by the caller. */
2630
2631 static bool
2632 cp_parser_simulate_error (cp_parser* parser)
2633 {
2634 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2635 {
2636 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2637 return true;
2638 }
2639 return false;
2640 }
2641
2642 /* This function is called when a type is defined. If type
2643 definitions are forbidden at this point, an error message is
2644 issued. */
2645
2646 static bool
2647 cp_parser_check_type_definition (cp_parser* parser)
2648 {
2649 /* If types are forbidden here, issue a message. */
2650 if (parser->type_definition_forbidden_message)
2651 {
2652 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2653 in the message need to be interpreted. */
2654 error (parser->type_definition_forbidden_message);
2655 return false;
2656 }
2657 return true;
2658 }
2659
2660 /* This function is called when the DECLARATOR is processed. The TYPE
2661 was a type defined in the decl-specifiers. If it is invalid to
2662 define a type in the decl-specifiers for DECLARATOR, an error is
2663 issued. TYPE_LOCATION is the location of TYPE and is used
2664 for error reporting. */
2665
2666 static void
2667 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2668 tree type, location_t type_location)
2669 {
2670 /* [dcl.fct] forbids type definitions in return types.
2671 Unfortunately, it's not easy to know whether or not we are
2672 processing a return type until after the fact. */
2673 while (declarator
2674 && (declarator->kind == cdk_pointer
2675 || declarator->kind == cdk_reference
2676 || declarator->kind == cdk_ptrmem))
2677 declarator = declarator->declarator;
2678 if (declarator
2679 && declarator->kind == cdk_function)
2680 {
2681 error_at (type_location,
2682 "new types may not be defined in a return type");
2683 inform (type_location,
2684 "(perhaps a semicolon is missing after the definition of %qT)",
2685 type);
2686 }
2687 }
2688
2689 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2690 "<" in any valid C++ program. If the next token is indeed "<",
2691 issue a message warning the user about what appears to be an
2692 invalid attempt to form a template-id. LOCATION is the location
2693 of the type-specifier (TYPE) */
2694
2695 static void
2696 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2697 tree type,
2698 enum tag_types tag_type,
2699 location_t location)
2700 {
2701 cp_token_position start = 0;
2702
2703 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2704 {
2705 if (TYPE_P (type))
2706 error_at (location, "%qT is not a template", type);
2707 else if (identifier_p (type))
2708 {
2709 if (tag_type != none_type)
2710 error_at (location, "%qE is not a class template", type);
2711 else
2712 error_at (location, "%qE is not a template", type);
2713 }
2714 else
2715 error_at (location, "invalid template-id");
2716 /* Remember the location of the invalid "<". */
2717 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2718 start = cp_lexer_token_position (parser->lexer, true);
2719 /* Consume the "<". */
2720 cp_lexer_consume_token (parser->lexer);
2721 /* Parse the template arguments. */
2722 cp_parser_enclosed_template_argument_list (parser);
2723 /* Permanently remove the invalid template arguments so that
2724 this error message is not issued again. */
2725 if (start)
2726 cp_lexer_purge_tokens_after (parser->lexer, start);
2727 }
2728 }
2729
2730 /* If parsing an integral constant-expression, issue an error message
2731 about the fact that THING appeared and return true. Otherwise,
2732 return false. In either case, set
2733 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2734
2735 static bool
2736 cp_parser_non_integral_constant_expression (cp_parser *parser,
2737 non_integral_constant thing)
2738 {
2739 parser->non_integral_constant_expression_p = true;
2740 if (parser->integral_constant_expression_p)
2741 {
2742 if (!parser->allow_non_integral_constant_expression_p)
2743 {
2744 const char *msg = NULL;
2745 switch (thing)
2746 {
2747 case NIC_FLOAT:
2748 error ("floating-point literal "
2749 "cannot appear in a constant-expression");
2750 return true;
2751 case NIC_CAST:
2752 error ("a cast to a type other than an integral or "
2753 "enumeration type cannot appear in a "
2754 "constant-expression");
2755 return true;
2756 case NIC_TYPEID:
2757 error ("%<typeid%> operator "
2758 "cannot appear in a constant-expression");
2759 return true;
2760 case NIC_NCC:
2761 error ("non-constant compound literals "
2762 "cannot appear in a constant-expression");
2763 return true;
2764 case NIC_FUNC_CALL:
2765 error ("a function call "
2766 "cannot appear in a constant-expression");
2767 return true;
2768 case NIC_INC:
2769 error ("an increment "
2770 "cannot appear in a constant-expression");
2771 return true;
2772 case NIC_DEC:
2773 error ("an decrement "
2774 "cannot appear in a constant-expression");
2775 return true;
2776 case NIC_ARRAY_REF:
2777 error ("an array reference "
2778 "cannot appear in a constant-expression");
2779 return true;
2780 case NIC_ADDR_LABEL:
2781 error ("the address of a label "
2782 "cannot appear in a constant-expression");
2783 return true;
2784 case NIC_OVERLOADED:
2785 error ("calls to overloaded operators "
2786 "cannot appear in a constant-expression");
2787 return true;
2788 case NIC_ASSIGNMENT:
2789 error ("an assignment cannot appear in a constant-expression");
2790 return true;
2791 case NIC_COMMA:
2792 error ("a comma operator "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_CONSTRUCTOR:
2796 error ("a call to a constructor "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_TRANSACTION:
2800 error ("a transaction expression "
2801 "cannot appear in a constant-expression");
2802 return true;
2803 case NIC_THIS:
2804 msg = "this";
2805 break;
2806 case NIC_FUNC_NAME:
2807 msg = "__FUNCTION__";
2808 break;
2809 case NIC_PRETTY_FUNC:
2810 msg = "__PRETTY_FUNCTION__";
2811 break;
2812 case NIC_C99_FUNC:
2813 msg = "__func__";
2814 break;
2815 case NIC_VA_ARG:
2816 msg = "va_arg";
2817 break;
2818 case NIC_ARROW:
2819 msg = "->";
2820 break;
2821 case NIC_POINT:
2822 msg = ".";
2823 break;
2824 case NIC_STAR:
2825 msg = "*";
2826 break;
2827 case NIC_ADDR:
2828 msg = "&";
2829 break;
2830 case NIC_PREINCREMENT:
2831 msg = "++";
2832 break;
2833 case NIC_PREDECREMENT:
2834 msg = "--";
2835 break;
2836 case NIC_NEW:
2837 msg = "new";
2838 break;
2839 case NIC_DEL:
2840 msg = "delete";
2841 break;
2842 default:
2843 gcc_unreachable ();
2844 }
2845 if (msg)
2846 error ("%qs cannot appear in a constant-expression", msg);
2847 return true;
2848 }
2849 }
2850 return false;
2851 }
2852
2853 /* Emit a diagnostic for an invalid type name. SCOPE is the
2854 qualifying scope (or NULL, if none) for ID. This function commits
2855 to the current active tentative parse, if any. (Otherwise, the
2856 problematic construct might be encountered again later, resulting
2857 in duplicate error messages.) LOCATION is the location of ID. */
2858
2859 static void
2860 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2861 tree scope, tree id,
2862 location_t location)
2863 {
2864 tree decl, old_scope;
2865 cp_parser_commit_to_tentative_parse (parser);
2866 /* Try to lookup the identifier. */
2867 old_scope = parser->scope;
2868 parser->scope = scope;
2869 decl = cp_parser_lookup_name_simple (parser, id, location);
2870 parser->scope = old_scope;
2871 /* If the lookup found a template-name, it means that the user forgot
2872 to specify an argument list. Emit a useful error message. */
2873 if (TREE_CODE (decl) == TEMPLATE_DECL)
2874 error_at (location,
2875 "invalid use of template-name %qE without an argument list",
2876 decl);
2877 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2878 error_at (location, "invalid use of destructor %qD as a type", id);
2879 else if (TREE_CODE (decl) == TYPE_DECL)
2880 /* Something like 'unsigned A a;' */
2881 error_at (location, "invalid combination of multiple type-specifiers");
2882 else if (!parser->scope)
2883 {
2884 /* Issue an error message. */
2885 error_at (location, "%qE does not name a type", id);
2886 /* If we're in a template class, it's possible that the user was
2887 referring to a type from a base class. For example:
2888
2889 template <typename T> struct A { typedef T X; };
2890 template <typename T> struct B : public A<T> { X x; };
2891
2892 The user should have said "typename A<T>::X". */
2893 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2894 inform (location, "C++11 %<constexpr%> only available with "
2895 "-std=c++11 or -std=gnu++11");
2896 else if (processing_template_decl && current_class_type
2897 && TYPE_BINFO (current_class_type))
2898 {
2899 tree b;
2900
2901 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2902 b;
2903 b = TREE_CHAIN (b))
2904 {
2905 tree base_type = BINFO_TYPE (b);
2906 if (CLASS_TYPE_P (base_type)
2907 && dependent_type_p (base_type))
2908 {
2909 tree field;
2910 /* Go from a particular instantiation of the
2911 template (which will have an empty TYPE_FIELDs),
2912 to the main version. */
2913 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2914 for (field = TYPE_FIELDS (base_type);
2915 field;
2916 field = DECL_CHAIN (field))
2917 if (TREE_CODE (field) == TYPE_DECL
2918 && DECL_NAME (field) == id)
2919 {
2920 inform (location,
2921 "(perhaps %<typename %T::%E%> was intended)",
2922 BINFO_TYPE (b), id);
2923 break;
2924 }
2925 if (field)
2926 break;
2927 }
2928 }
2929 }
2930 }
2931 /* Here we diagnose qualified-ids where the scope is actually correct,
2932 but the identifier does not resolve to a valid type name. */
2933 else if (parser->scope != error_mark_node)
2934 {
2935 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2936 {
2937 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2938 error_at (location_of (id),
2939 "%qE in namespace %qE does not name a template type",
2940 id, parser->scope);
2941 else
2942 error_at (location_of (id),
2943 "%qE in namespace %qE does not name a type",
2944 id, parser->scope);
2945 }
2946 else if (CLASS_TYPE_P (parser->scope)
2947 && constructor_name_p (id, parser->scope))
2948 {
2949 /* A<T>::A<T>() */
2950 error_at (location, "%<%T::%E%> names the constructor, not"
2951 " the type", parser->scope, id);
2952 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2953 error_at (location, "and %qT has no template constructors",
2954 parser->scope);
2955 }
2956 else if (TYPE_P (parser->scope)
2957 && dependent_scope_p (parser->scope))
2958 error_at (location, "need %<typename%> before %<%T::%E%> because "
2959 "%qT is a dependent scope",
2960 parser->scope, id, parser->scope);
2961 else if (TYPE_P (parser->scope))
2962 {
2963 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2964 error_at (location_of (id),
2965 "%qE in %q#T does not name a template type",
2966 id, parser->scope);
2967 else
2968 error_at (location_of (id),
2969 "%qE in %q#T does not name a type",
2970 id, parser->scope);
2971 }
2972 else
2973 gcc_unreachable ();
2974 }
2975 }
2976
2977 /* Check for a common situation where a type-name should be present,
2978 but is not, and issue a sensible error message. Returns true if an
2979 invalid type-name was detected.
2980
2981 The situation handled by this function are variable declarations of the
2982 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2983 Usually, `ID' should name a type, but if we got here it means that it
2984 does not. We try to emit the best possible error message depending on
2985 how exactly the id-expression looks like. */
2986
2987 static bool
2988 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2989 {
2990 tree id;
2991 cp_token *token = cp_lexer_peek_token (parser->lexer);
2992
2993 /* Avoid duplicate error about ambiguous lookup. */
2994 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2995 {
2996 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2997 if (next->type == CPP_NAME && next->ambiguous_p)
2998 goto out;
2999 }
3000
3001 cp_parser_parse_tentatively (parser);
3002 id = cp_parser_id_expression (parser,
3003 /*template_keyword_p=*/false,
3004 /*check_dependency_p=*/true,
3005 /*template_p=*/NULL,
3006 /*declarator_p=*/true,
3007 /*optional_p=*/false);
3008 /* If the next token is a (, this is a function with no explicit return
3009 type, i.e. constructor, destructor or conversion op. */
3010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3011 || TREE_CODE (id) == TYPE_DECL)
3012 {
3013 cp_parser_abort_tentative_parse (parser);
3014 return false;
3015 }
3016 if (!cp_parser_parse_definitely (parser))
3017 return false;
3018
3019 /* Emit a diagnostic for the invalid type. */
3020 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3021 id, token->location);
3022 out:
3023 /* If we aren't in the middle of a declarator (i.e. in a
3024 parameter-declaration-clause), skip to the end of the declaration;
3025 there's no point in trying to process it. */
3026 if (!parser->in_declarator_p)
3027 cp_parser_skip_to_end_of_block_or_statement (parser);
3028 return true;
3029 }
3030
3031 /* Consume tokens up to, and including, the next non-nested closing `)'.
3032 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3033 are doing error recovery. Returns -1 if OR_COMMA is true and we
3034 found an unnested comma. */
3035
3036 static int
3037 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3038 bool recovering,
3039 bool or_comma,
3040 bool consume_paren)
3041 {
3042 unsigned paren_depth = 0;
3043 unsigned brace_depth = 0;
3044 unsigned square_depth = 0;
3045
3046 if (recovering && !or_comma
3047 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3048 return 0;
3049
3050 while (true)
3051 {
3052 cp_token * token = cp_lexer_peek_token (parser->lexer);
3053
3054 switch (token->type)
3055 {
3056 case CPP_EOF:
3057 case CPP_PRAGMA_EOL:
3058 /* If we've run out of tokens, then there is no closing `)'. */
3059 return 0;
3060
3061 /* This is good for lambda expression capture-lists. */
3062 case CPP_OPEN_SQUARE:
3063 ++square_depth;
3064 break;
3065 case CPP_CLOSE_SQUARE:
3066 if (!square_depth--)
3067 return 0;
3068 break;
3069
3070 case CPP_SEMICOLON:
3071 /* This matches the processing in skip_to_end_of_statement. */
3072 if (!brace_depth)
3073 return 0;
3074 break;
3075
3076 case CPP_OPEN_BRACE:
3077 ++brace_depth;
3078 break;
3079 case CPP_CLOSE_BRACE:
3080 if (!brace_depth--)
3081 return 0;
3082 break;
3083
3084 case CPP_COMMA:
3085 if (recovering && or_comma && !brace_depth && !paren_depth
3086 && !square_depth)
3087 return -1;
3088 break;
3089
3090 case CPP_OPEN_PAREN:
3091 if (!brace_depth)
3092 ++paren_depth;
3093 break;
3094
3095 case CPP_CLOSE_PAREN:
3096 if (!brace_depth && !paren_depth--)
3097 {
3098 if (consume_paren)
3099 cp_lexer_consume_token (parser->lexer);
3100 return 1;
3101 }
3102 break;
3103
3104 default:
3105 break;
3106 }
3107
3108 /* Consume the token. */
3109 cp_lexer_consume_token (parser->lexer);
3110 }
3111 }
3112
3113 /* Consume tokens until we reach the end of the current statement.
3114 Normally, that will be just before consuming a `;'. However, if a
3115 non-nested `}' comes first, then we stop before consuming that. */
3116
3117 static void
3118 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3119 {
3120 unsigned nesting_depth = 0;
3121
3122 while (true)
3123 {
3124 cp_token *token = cp_lexer_peek_token (parser->lexer);
3125
3126 switch (token->type)
3127 {
3128 case CPP_EOF:
3129 case CPP_PRAGMA_EOL:
3130 /* If we've run out of tokens, stop. */
3131 return;
3132
3133 case CPP_SEMICOLON:
3134 /* If the next token is a `;', we have reached the end of the
3135 statement. */
3136 if (!nesting_depth)
3137 return;
3138 break;
3139
3140 case CPP_CLOSE_BRACE:
3141 /* If this is a non-nested '}', stop before consuming it.
3142 That way, when confronted with something like:
3143
3144 { 3 + }
3145
3146 we stop before consuming the closing '}', even though we
3147 have not yet reached a `;'. */
3148 if (nesting_depth == 0)
3149 return;
3150
3151 /* If it is the closing '}' for a block that we have
3152 scanned, stop -- but only after consuming the token.
3153 That way given:
3154
3155 void f g () { ... }
3156 typedef int I;
3157
3158 we will stop after the body of the erroneously declared
3159 function, but before consuming the following `typedef'
3160 declaration. */
3161 if (--nesting_depth == 0)
3162 {
3163 cp_lexer_consume_token (parser->lexer);
3164 return;
3165 }
3166
3167 case CPP_OPEN_BRACE:
3168 ++nesting_depth;
3169 break;
3170
3171 default:
3172 break;
3173 }
3174
3175 /* Consume the token. */
3176 cp_lexer_consume_token (parser->lexer);
3177 }
3178 }
3179
3180 /* This function is called at the end of a statement or declaration.
3181 If the next token is a semicolon, it is consumed; otherwise, error
3182 recovery is attempted. */
3183
3184 static void
3185 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3186 {
3187 /* Look for the trailing `;'. */
3188 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3189 {
3190 /* If there is additional (erroneous) input, skip to the end of
3191 the statement. */
3192 cp_parser_skip_to_end_of_statement (parser);
3193 /* If the next token is now a `;', consume it. */
3194 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3195 cp_lexer_consume_token (parser->lexer);
3196 }
3197 }
3198
3199 /* Skip tokens until we have consumed an entire block, or until we
3200 have consumed a non-nested `;'. */
3201
3202 static void
3203 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3204 {
3205 int nesting_depth = 0;
3206
3207 while (nesting_depth >= 0)
3208 {
3209 cp_token *token = cp_lexer_peek_token (parser->lexer);
3210
3211 switch (token->type)
3212 {
3213 case CPP_EOF:
3214 case CPP_PRAGMA_EOL:
3215 /* If we've run out of tokens, stop. */
3216 return;
3217
3218 case CPP_SEMICOLON:
3219 /* Stop if this is an unnested ';'. */
3220 if (!nesting_depth)
3221 nesting_depth = -1;
3222 break;
3223
3224 case CPP_CLOSE_BRACE:
3225 /* Stop if this is an unnested '}', or closes the outermost
3226 nesting level. */
3227 nesting_depth--;
3228 if (nesting_depth < 0)
3229 return;
3230 if (!nesting_depth)
3231 nesting_depth = -1;
3232 break;
3233
3234 case CPP_OPEN_BRACE:
3235 /* Nest. */
3236 nesting_depth++;
3237 break;
3238
3239 default:
3240 break;
3241 }
3242
3243 /* Consume the token. */
3244 cp_lexer_consume_token (parser->lexer);
3245 }
3246 }
3247
3248 /* Skip tokens until a non-nested closing curly brace is the next
3249 token, or there are no more tokens. Return true in the first case,
3250 false otherwise. */
3251
3252 static bool
3253 cp_parser_skip_to_closing_brace (cp_parser *parser)
3254 {
3255 unsigned nesting_depth = 0;
3256
3257 while (true)
3258 {
3259 cp_token *token = cp_lexer_peek_token (parser->lexer);
3260
3261 switch (token->type)
3262 {
3263 case CPP_EOF:
3264 case CPP_PRAGMA_EOL:
3265 /* If we've run out of tokens, stop. */
3266 return false;
3267
3268 case CPP_CLOSE_BRACE:
3269 /* If the next token is a non-nested `}', then we have reached
3270 the end of the current block. */
3271 if (nesting_depth-- == 0)
3272 return true;
3273 break;
3274
3275 case CPP_OPEN_BRACE:
3276 /* If it the next token is a `{', then we are entering a new
3277 block. Consume the entire block. */
3278 ++nesting_depth;
3279 break;
3280
3281 default:
3282 break;
3283 }
3284
3285 /* Consume the token. */
3286 cp_lexer_consume_token (parser->lexer);
3287 }
3288 }
3289
3290 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3291 parameter is the PRAGMA token, allowing us to purge the entire pragma
3292 sequence. */
3293
3294 static void
3295 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3296 {
3297 cp_token *token;
3298
3299 parser->lexer->in_pragma = false;
3300
3301 do
3302 token = cp_lexer_consume_token (parser->lexer);
3303 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3304
3305 /* Ensure that the pragma is not parsed again. */
3306 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3307 }
3308
3309 /* Require pragma end of line, resyncing with it as necessary. The
3310 arguments are as for cp_parser_skip_to_pragma_eol. */
3311
3312 static void
3313 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3314 {
3315 parser->lexer->in_pragma = false;
3316 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3317 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3318 }
3319
3320 /* This is a simple wrapper around make_typename_type. When the id is
3321 an unresolved identifier node, we can provide a superior diagnostic
3322 using cp_parser_diagnose_invalid_type_name. */
3323
3324 static tree
3325 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3326 tree id, location_t id_location)
3327 {
3328 tree result;
3329 if (identifier_p (id))
3330 {
3331 result = make_typename_type (scope, id, typename_type,
3332 /*complain=*/tf_none);
3333 if (result == error_mark_node)
3334 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3335 return result;
3336 }
3337 return make_typename_type (scope, id, typename_type, tf_error);
3338 }
3339
3340 /* This is a wrapper around the
3341 make_{pointer,ptrmem,reference}_declarator functions that decides
3342 which one to call based on the CODE and CLASS_TYPE arguments. The
3343 CODE argument should be one of the values returned by
3344 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3345 appertain to the pointer or reference. */
3346
3347 static cp_declarator *
3348 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3349 cp_cv_quals cv_qualifiers,
3350 cp_declarator *target,
3351 tree attributes)
3352 {
3353 if (code == ERROR_MARK)
3354 return cp_error_declarator;
3355
3356 if (code == INDIRECT_REF)
3357 if (class_type == NULL_TREE)
3358 return make_pointer_declarator (cv_qualifiers, target, attributes);
3359 else
3360 return make_ptrmem_declarator (cv_qualifiers, class_type,
3361 target, attributes);
3362 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3363 return make_reference_declarator (cv_qualifiers, target,
3364 false, attributes);
3365 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3366 return make_reference_declarator (cv_qualifiers, target,
3367 true, attributes);
3368 gcc_unreachable ();
3369 }
3370
3371 /* Create a new C++ parser. */
3372
3373 static cp_parser *
3374 cp_parser_new (void)
3375 {
3376 cp_parser *parser;
3377 cp_lexer *lexer;
3378 unsigned i;
3379
3380 /* cp_lexer_new_main is called before doing GC allocation because
3381 cp_lexer_new_main might load a PCH file. */
3382 lexer = cp_lexer_new_main ();
3383
3384 /* Initialize the binops_by_token so that we can get the tree
3385 directly from the token. */
3386 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3387 binops_by_token[binops[i].token_type] = binops[i];
3388
3389 parser = ggc_alloc_cleared_cp_parser ();
3390 parser->lexer = lexer;
3391 parser->context = cp_parser_context_new (NULL);
3392
3393 /* For now, we always accept GNU extensions. */
3394 parser->allow_gnu_extensions_p = 1;
3395
3396 /* The `>' token is a greater-than operator, not the end of a
3397 template-id. */
3398 parser->greater_than_is_operator_p = true;
3399
3400 parser->default_arg_ok_p = true;
3401
3402 /* We are not parsing a constant-expression. */
3403 parser->integral_constant_expression_p = false;
3404 parser->allow_non_integral_constant_expression_p = false;
3405 parser->non_integral_constant_expression_p = false;
3406
3407 /* Local variable names are not forbidden. */
3408 parser->local_variables_forbidden_p = false;
3409
3410 /* We are not processing an `extern "C"' declaration. */
3411 parser->in_unbraced_linkage_specification_p = false;
3412
3413 /* We are not processing a declarator. */
3414 parser->in_declarator_p = false;
3415
3416 /* We are not processing a template-argument-list. */
3417 parser->in_template_argument_list_p = false;
3418
3419 /* We are not in an iteration statement. */
3420 parser->in_statement = 0;
3421
3422 /* We are not in a switch statement. */
3423 parser->in_switch_statement_p = false;
3424
3425 /* We are not parsing a type-id inside an expression. */
3426 parser->in_type_id_in_expr_p = false;
3427
3428 /* Declarations aren't implicitly extern "C". */
3429 parser->implicit_extern_c = false;
3430
3431 /* String literals should be translated to the execution character set. */
3432 parser->translate_strings_p = true;
3433
3434 /* We are not parsing a function body. */
3435 parser->in_function_body = false;
3436
3437 /* We can correct until told otherwise. */
3438 parser->colon_corrects_to_scope_p = true;
3439
3440 /* The unparsed function queue is empty. */
3441 push_unparsed_function_queues (parser);
3442
3443 /* There are no classes being defined. */
3444 parser->num_classes_being_defined = 0;
3445
3446 /* No template parameters apply. */
3447 parser->num_template_parameter_lists = 0;
3448
3449 /* Not declaring an implicit function template. */
3450 parser->auto_is_implicit_function_template_parm_p = false;
3451 parser->fully_implicit_function_template_p = false;
3452 parser->implicit_template_parms = 0;
3453 parser->implicit_template_scope = 0;
3454
3455 return parser;
3456 }
3457
3458 /* Create a cp_lexer structure which will emit the tokens in CACHE
3459 and push it onto the parser's lexer stack. This is used for delayed
3460 parsing of in-class method bodies and default arguments, and should
3461 not be confused with tentative parsing. */
3462 static void
3463 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3464 {
3465 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3466 lexer->next = parser->lexer;
3467 parser->lexer = lexer;
3468
3469 /* Move the current source position to that of the first token in the
3470 new lexer. */
3471 cp_lexer_set_source_position_from_token (lexer->next_token);
3472 }
3473
3474 /* Pop the top lexer off the parser stack. This is never used for the
3475 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3476 static void
3477 cp_parser_pop_lexer (cp_parser *parser)
3478 {
3479 cp_lexer *lexer = parser->lexer;
3480 parser->lexer = lexer->next;
3481 cp_lexer_destroy (lexer);
3482
3483 /* Put the current source position back where it was before this
3484 lexer was pushed. */
3485 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3486 }
3487
3488 /* Lexical conventions [gram.lex] */
3489
3490 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3491 identifier. */
3492
3493 static tree
3494 cp_parser_identifier (cp_parser* parser)
3495 {
3496 cp_token *token;
3497
3498 /* Look for the identifier. */
3499 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3500 /* Return the value. */
3501 return token ? token->u.value : error_mark_node;
3502 }
3503
3504 /* Parse a sequence of adjacent string constants. Returns a
3505 TREE_STRING representing the combined, nul-terminated string
3506 constant. If TRANSLATE is true, translate the string to the
3507 execution character set. If WIDE_OK is true, a wide string is
3508 invalid here.
3509
3510 C++98 [lex.string] says that if a narrow string literal token is
3511 adjacent to a wide string literal token, the behavior is undefined.
3512 However, C99 6.4.5p4 says that this results in a wide string literal.
3513 We follow C99 here, for consistency with the C front end.
3514
3515 This code is largely lifted from lex_string() in c-lex.c.
3516
3517 FUTURE: ObjC++ will need to handle @-strings here. */
3518 static tree
3519 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3520 {
3521 tree value;
3522 size_t count;
3523 struct obstack str_ob;
3524 cpp_string str, istr, *strs;
3525 cp_token *tok;
3526 enum cpp_ttype type, curr_type;
3527 int have_suffix_p = 0;
3528 tree string_tree;
3529 tree suffix_id = NULL_TREE;
3530 bool curr_tok_is_userdef_p = false;
3531
3532 tok = cp_lexer_peek_token (parser->lexer);
3533 if (!cp_parser_is_string_literal (tok))
3534 {
3535 cp_parser_error (parser, "expected string-literal");
3536 return error_mark_node;
3537 }
3538
3539 if (cpp_userdef_string_p (tok->type))
3540 {
3541 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3542 curr_type = cpp_userdef_string_remove_type (tok->type);
3543 curr_tok_is_userdef_p = true;
3544 }
3545 else
3546 {
3547 string_tree = tok->u.value;
3548 curr_type = tok->type;
3549 }
3550 type = curr_type;
3551
3552 /* Try to avoid the overhead of creating and destroying an obstack
3553 for the common case of just one string. */
3554 if (!cp_parser_is_string_literal
3555 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3556 {
3557 cp_lexer_consume_token (parser->lexer);
3558
3559 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3560 str.len = TREE_STRING_LENGTH (string_tree);
3561 count = 1;
3562
3563 if (curr_tok_is_userdef_p)
3564 {
3565 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3566 have_suffix_p = 1;
3567 curr_type = cpp_userdef_string_remove_type (tok->type);
3568 }
3569 else
3570 curr_type = tok->type;
3571
3572 strs = &str;
3573 }
3574 else
3575 {
3576 gcc_obstack_init (&str_ob);
3577 count = 0;
3578
3579 do
3580 {
3581 cp_lexer_consume_token (parser->lexer);
3582 count++;
3583 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3584 str.len = TREE_STRING_LENGTH (string_tree);
3585
3586 if (curr_tok_is_userdef_p)
3587 {
3588 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3589 if (have_suffix_p == 0)
3590 {
3591 suffix_id = curr_suffix_id;
3592 have_suffix_p = 1;
3593 }
3594 else if (have_suffix_p == 1
3595 && curr_suffix_id != suffix_id)
3596 {
3597 error ("inconsistent user-defined literal suffixes"
3598 " %qD and %qD in string literal",
3599 suffix_id, curr_suffix_id);
3600 have_suffix_p = -1;
3601 }
3602 curr_type = cpp_userdef_string_remove_type (tok->type);
3603 }
3604 else
3605 curr_type = tok->type;
3606
3607 if (type != curr_type)
3608 {
3609 if (type == CPP_STRING)
3610 type = curr_type;
3611 else if (curr_type != CPP_STRING)
3612 error_at (tok->location,
3613 "unsupported non-standard concatenation "
3614 "of string literals");
3615 }
3616
3617 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3618
3619 tok = cp_lexer_peek_token (parser->lexer);
3620 if (cpp_userdef_string_p (tok->type))
3621 {
3622 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3623 curr_type = cpp_userdef_string_remove_type (tok->type);
3624 curr_tok_is_userdef_p = true;
3625 }
3626 else
3627 {
3628 string_tree = tok->u.value;
3629 curr_type = tok->type;
3630 curr_tok_is_userdef_p = false;
3631 }
3632 }
3633 while (cp_parser_is_string_literal (tok));
3634
3635 strs = (cpp_string *) obstack_finish (&str_ob);
3636 }
3637
3638 if (type != CPP_STRING && !wide_ok)
3639 {
3640 cp_parser_error (parser, "a wide string is invalid in this context");
3641 type = CPP_STRING;
3642 }
3643
3644 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3645 (parse_in, strs, count, &istr, type))
3646 {
3647 value = build_string (istr.len, (const char *)istr.text);
3648 free (CONST_CAST (unsigned char *, istr.text));
3649
3650 switch (type)
3651 {
3652 default:
3653 case CPP_STRING:
3654 case CPP_UTF8STRING:
3655 TREE_TYPE (value) = char_array_type_node;
3656 break;
3657 case CPP_STRING16:
3658 TREE_TYPE (value) = char16_array_type_node;
3659 break;
3660 case CPP_STRING32:
3661 TREE_TYPE (value) = char32_array_type_node;
3662 break;
3663 case CPP_WSTRING:
3664 TREE_TYPE (value) = wchar_array_type_node;
3665 break;
3666 }
3667
3668 value = fix_string_type (value);
3669
3670 if (have_suffix_p)
3671 {
3672 tree literal = build_userdef_literal (suffix_id, value,
3673 OT_NONE, NULL_TREE);
3674 tok->u.value = literal;
3675 return cp_parser_userdef_string_literal (tok);
3676 }
3677 }
3678 else
3679 /* cpp_interpret_string has issued an error. */
3680 value = error_mark_node;
3681
3682 if (count > 1)
3683 obstack_free (&str_ob, 0);
3684
3685 return value;
3686 }
3687
3688 /* Look up a literal operator with the name and the exact arguments. */
3689
3690 static tree
3691 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3692 {
3693 tree decl, fns;
3694 decl = lookup_name (name);
3695 if (!decl || !is_overloaded_fn (decl))
3696 return error_mark_node;
3697
3698 for (fns = decl; fns; fns = OVL_NEXT (fns))
3699 {
3700 unsigned int ix;
3701 bool found = true;
3702 tree fn = OVL_CURRENT (fns);
3703 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3704 if (parmtypes != NULL_TREE)
3705 {
3706 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3707 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3708 {
3709 tree tparm = TREE_VALUE (parmtypes);
3710 tree targ = TREE_TYPE ((*args)[ix]);
3711 bool ptr = TYPE_PTR_P (tparm);
3712 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3713 if ((ptr || arr || !same_type_p (tparm, targ))
3714 && (!ptr || !arr
3715 || !same_type_p (TREE_TYPE (tparm),
3716 TREE_TYPE (targ))))
3717 found = false;
3718 }
3719 if (found
3720 && ix == vec_safe_length (args)
3721 /* May be this should be sufficient_parms_p instead,
3722 depending on how exactly should user-defined literals
3723 work in presence of default arguments on the literal
3724 operator parameters. */
3725 && parmtypes == void_list_node)
3726 return fn;
3727 }
3728 }
3729
3730 return error_mark_node;
3731 }
3732
3733 /* Parse a user-defined char constant. Returns a call to a user-defined
3734 literal operator taking the character as an argument. */
3735
3736 static tree
3737 cp_parser_userdef_char_literal (cp_parser *parser)
3738 {
3739 cp_token *token = cp_lexer_consume_token (parser->lexer);
3740 tree literal = token->u.value;
3741 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3742 tree value = USERDEF_LITERAL_VALUE (literal);
3743 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3744 tree decl, result;
3745
3746 /* Build up a call to the user-defined operator */
3747 /* Lookup the name we got back from the id-expression. */
3748 vec<tree, va_gc> *args = make_tree_vector ();
3749 vec_safe_push (args, value);
3750 decl = lookup_literal_operator (name, args);
3751 if (!decl || decl == error_mark_node)
3752 {
3753 error ("unable to find character literal operator %qD with %qT argument",
3754 name, TREE_TYPE (value));
3755 release_tree_vector (args);
3756 return error_mark_node;
3757 }
3758 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3759 release_tree_vector (args);
3760 if (result != error_mark_node)
3761 return result;
3762
3763 error ("unable to find character literal operator %qD with %qT argument",
3764 name, TREE_TYPE (value));
3765 return error_mark_node;
3766 }
3767
3768 /* A subroutine of cp_parser_userdef_numeric_literal to
3769 create a char... template parameter pack from a string node. */
3770
3771 static tree
3772 make_char_string_pack (tree value)
3773 {
3774 tree charvec;
3775 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3776 const char *str = TREE_STRING_POINTER (value);
3777 int i, len = TREE_STRING_LENGTH (value) - 1;
3778 tree argvec = make_tree_vec (1);
3779
3780 /* Fill in CHARVEC with all of the parameters. */
3781 charvec = make_tree_vec (len);
3782 for (i = 0; i < len; ++i)
3783 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3784
3785 /* Build the argument packs. */
3786 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3787 TREE_TYPE (argpack) = char_type_node;
3788
3789 TREE_VEC_ELT (argvec, 0) = argpack;
3790
3791 return argvec;
3792 }
3793
3794 /* A subroutine of cp_parser_userdef_numeric_literal to
3795 create a char... template parameter pack from a string node. */
3796
3797 static tree
3798 make_string_pack (tree value)
3799 {
3800 tree charvec;
3801 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3802 const char *str = TREE_STRING_POINTER (value);
3803 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3804 int len = TREE_STRING_LENGTH (value) / sz - 1;
3805 tree argvec = make_tree_vec (2);
3806
3807 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3808 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3809
3810 /* First template parm is character type. */
3811 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3812
3813 /* Fill in CHARVEC with all of the parameters. */
3814 charvec = make_tree_vec (len);
3815 if (sz == 1)
3816 {
3817 for (int i = 0; i < len; ++i)
3818 TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, str[i]);
3819 }
3820 else if (sz == 2)
3821 {
3822 const uint16_t *num = (const uint16_t *)str;
3823 for (int i = 0; i < len; ++i)
3824 TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]);
3825 }
3826 else if (sz == 4)
3827 {
3828 const uint32_t *num = (const uint32_t *)str;
3829 for (int i = 0; i < len; ++i)
3830 TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]);
3831 }
3832
3833 /* Build the argument packs. */
3834 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3835 TREE_TYPE (argpack) = str_char_type_node;
3836
3837 TREE_VEC_ELT (argvec, 1) = argpack;
3838
3839 return argvec;
3840 }
3841
3842 /* Parse a user-defined numeric constant. returns a call to a user-defined
3843 literal operator. */
3844
3845 static tree
3846 cp_parser_userdef_numeric_literal (cp_parser *parser)
3847 {
3848 cp_token *token = cp_lexer_consume_token (parser->lexer);
3849 tree literal = token->u.value;
3850 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3851 tree value = USERDEF_LITERAL_VALUE (literal);
3852 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3853 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3854 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3855 tree decl, result;
3856 vec<tree, va_gc> *args;
3857
3858 /* Look for a literal operator taking the exact type of numeric argument
3859 as the literal value. */
3860 args = make_tree_vector ();
3861 vec_safe_push (args, value);
3862 decl = lookup_literal_operator (name, args);
3863 if (decl && decl != error_mark_node)
3864 {
3865 result = finish_call_expr (decl, &args, false, true, tf_none);
3866 if (result != error_mark_node)
3867 {
3868 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3869 warning_at (token->location, OPT_Woverflow,
3870 "integer literal exceeds range of %qT type",
3871 long_long_unsigned_type_node);
3872 else
3873 {
3874 if (overflow > 0)
3875 warning_at (token->location, OPT_Woverflow,
3876 "floating literal exceeds range of %qT type",
3877 long_double_type_node);
3878 else if (overflow < 0)
3879 warning_at (token->location, OPT_Woverflow,
3880 "floating literal truncated to zero");
3881 }
3882 release_tree_vector (args);
3883 return result;
3884 }
3885 }
3886 release_tree_vector (args);
3887
3888 /* If the numeric argument didn't work, look for a raw literal
3889 operator taking a const char* argument consisting of the number
3890 in string format. */
3891 args = make_tree_vector ();
3892 vec_safe_push (args, num_string);
3893 decl = lookup_literal_operator (name, args);
3894 if (decl && decl != error_mark_node)
3895 {
3896 result = finish_call_expr (decl, &args, false, true, tf_none);
3897 if (result != error_mark_node)
3898 {
3899 release_tree_vector (args);
3900 return result;
3901 }
3902 }
3903 release_tree_vector (args);
3904
3905 /* If the raw literal didn't work, look for a non-type template
3906 function with parameter pack char.... Call the function with
3907 template parameter characters representing the number. */
3908 args = make_tree_vector ();
3909 decl = lookup_literal_operator (name, args);
3910 if (decl && decl != error_mark_node)
3911 {
3912 tree tmpl_args = make_char_string_pack (num_string);
3913 decl = lookup_template_function (decl, tmpl_args);
3914 result = finish_call_expr (decl, &args, false, true, tf_none);
3915 if (result != error_mark_node)
3916 {
3917 release_tree_vector (args);
3918 return result;
3919 }
3920 }
3921 release_tree_vector (args);
3922
3923 error ("unable to find numeric literal operator %qD", name);
3924 return error_mark_node;
3925 }
3926
3927 /* Parse a user-defined string constant. Returns a call to a user-defined
3928 literal operator taking a character pointer and the length of the string
3929 as arguments. */
3930
3931 static tree
3932 cp_parser_userdef_string_literal (cp_token *token)
3933 {
3934 tree literal = token->u.value;
3935 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3936 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3937 tree value = USERDEF_LITERAL_VALUE (literal);
3938 int len = TREE_STRING_LENGTH (value)
3939 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3940 tree decl, result;
3941 vec<tree, va_gc> *args;
3942
3943 /* Look for a template function with typename parameter CharT
3944 and parameter pack CharT... Call the function with
3945 template parameter characters representing the string. */
3946 args = make_tree_vector ();
3947 decl = lookup_literal_operator (name, args);
3948 if (decl && decl != error_mark_node)
3949 {
3950 tree tmpl_args = make_string_pack (value);
3951 decl = lookup_template_function (decl, tmpl_args);
3952 result = finish_call_expr (decl, &args, false, true, tf_none);
3953 if (result != error_mark_node)
3954 {
3955 release_tree_vector (args);
3956 return result;
3957 }
3958 }
3959 release_tree_vector (args);
3960
3961 /* Build up a call to the user-defined operator */
3962 /* Lookup the name we got back from the id-expression. */
3963 args = make_tree_vector ();
3964 vec_safe_push (args, value);
3965 vec_safe_push (args, build_int_cst (size_type_node, len));
3966 decl = lookup_name (name);
3967 if (!decl || decl == error_mark_node)
3968 {
3969 error ("unable to find string literal operator %qD", name);
3970 release_tree_vector (args);
3971 return error_mark_node;
3972 }
3973 result = finish_call_expr (decl, &args, false, true, tf_none);
3974 release_tree_vector (args);
3975 if (result != error_mark_node)
3976 return result;
3977
3978 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3979 name, TREE_TYPE (value), size_type_node);
3980 return error_mark_node;
3981 }
3982
3983
3984 /* Basic concepts [gram.basic] */
3985
3986 /* Parse a translation-unit.
3987
3988 translation-unit:
3989 declaration-seq [opt]
3990
3991 Returns TRUE if all went well. */
3992
3993 static bool
3994 cp_parser_translation_unit (cp_parser* parser)
3995 {
3996 /* The address of the first non-permanent object on the declarator
3997 obstack. */
3998 static void *declarator_obstack_base;
3999
4000 bool success;
4001
4002 /* Create the declarator obstack, if necessary. */
4003 if (!cp_error_declarator)
4004 {
4005 gcc_obstack_init (&declarator_obstack);
4006 /* Create the error declarator. */
4007 cp_error_declarator = make_declarator (cdk_error);
4008 /* Create the empty parameter list. */
4009 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4010 /* Remember where the base of the declarator obstack lies. */
4011 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4012 }
4013
4014 cp_parser_declaration_seq_opt (parser);
4015
4016 /* If there are no tokens left then all went well. */
4017 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4018 {
4019 /* Get rid of the token array; we don't need it any more. */
4020 cp_lexer_destroy (parser->lexer);
4021 parser->lexer = NULL;
4022
4023 /* This file might have been a context that's implicitly extern
4024 "C". If so, pop the lang context. (Only relevant for PCH.) */
4025 if (parser->implicit_extern_c)
4026 {
4027 pop_lang_context ();
4028 parser->implicit_extern_c = false;
4029 }
4030
4031 /* Finish up. */
4032 finish_translation_unit ();
4033
4034 success = true;
4035 }
4036 else
4037 {
4038 cp_parser_error (parser, "expected declaration");
4039 success = false;
4040 }
4041
4042 /* Make sure the declarator obstack was fully cleaned up. */
4043 gcc_assert (obstack_next_free (&declarator_obstack)
4044 == declarator_obstack_base);
4045
4046 /* All went well. */
4047 return success;
4048 }
4049
4050 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4051 decltype context. */
4052
4053 static inline tsubst_flags_t
4054 complain_flags (bool decltype_p)
4055 {
4056 tsubst_flags_t complain = tf_warning_or_error;
4057 if (decltype_p)
4058 complain |= tf_decltype;
4059 return complain;
4060 }
4061
4062 /* Expressions [gram.expr] */
4063
4064 /* Parse a primary-expression.
4065
4066 primary-expression:
4067 literal
4068 this
4069 ( expression )
4070 id-expression
4071
4072 GNU Extensions:
4073
4074 primary-expression:
4075 ( compound-statement )
4076 __builtin_va_arg ( assignment-expression , type-id )
4077 __builtin_offsetof ( type-id , offsetof-expression )
4078
4079 C++ Extensions:
4080 __has_nothrow_assign ( type-id )
4081 __has_nothrow_constructor ( type-id )
4082 __has_nothrow_copy ( type-id )
4083 __has_trivial_assign ( type-id )
4084 __has_trivial_constructor ( type-id )
4085 __has_trivial_copy ( type-id )
4086 __has_trivial_destructor ( type-id )
4087 __has_virtual_destructor ( type-id )
4088 __is_abstract ( type-id )
4089 __is_base_of ( type-id , type-id )
4090 __is_class ( type-id )
4091 __is_convertible_to ( type-id , type-id )
4092 __is_empty ( type-id )
4093 __is_enum ( type-id )
4094 __is_final ( type-id )
4095 __is_literal_type ( type-id )
4096 __is_pod ( type-id )
4097 __is_polymorphic ( type-id )
4098 __is_std_layout ( type-id )
4099 __is_trivial ( type-id )
4100 __is_union ( type-id )
4101
4102 Objective-C++ Extension:
4103
4104 primary-expression:
4105 objc-expression
4106
4107 literal:
4108 __null
4109
4110 ADDRESS_P is true iff this expression was immediately preceded by
4111 "&" and therefore might denote a pointer-to-member. CAST_P is true
4112 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4113 true iff this expression is a template argument.
4114
4115 Returns a representation of the expression. Upon return, *IDK
4116 indicates what kind of id-expression (if any) was present. */
4117
4118 static tree
4119 cp_parser_primary_expression (cp_parser *parser,
4120 bool address_p,
4121 bool cast_p,
4122 bool template_arg_p,
4123 bool decltype_p,
4124 cp_id_kind *idk)
4125 {
4126 cp_token *token = NULL;
4127
4128 /* Assume the primary expression is not an id-expression. */
4129 *idk = CP_ID_KIND_NONE;
4130
4131 /* Peek at the next token. */
4132 token = cp_lexer_peek_token (parser->lexer);
4133 switch (token->type)
4134 {
4135 /* literal:
4136 integer-literal
4137 character-literal
4138 floating-literal
4139 string-literal
4140 boolean-literal
4141 pointer-literal
4142 user-defined-literal */
4143 case CPP_CHAR:
4144 case CPP_CHAR16:
4145 case CPP_CHAR32:
4146 case CPP_WCHAR:
4147 case CPP_NUMBER:
4148 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4149 return cp_parser_userdef_numeric_literal (parser);
4150 token = cp_lexer_consume_token (parser->lexer);
4151 if (TREE_CODE (token->u.value) == FIXED_CST)
4152 {
4153 error_at (token->location,
4154 "fixed-point types not supported in C++");
4155 return error_mark_node;
4156 }
4157 /* Floating-point literals are only allowed in an integral
4158 constant expression if they are cast to an integral or
4159 enumeration type. */
4160 if (TREE_CODE (token->u.value) == REAL_CST
4161 && parser->integral_constant_expression_p
4162 && pedantic)
4163 {
4164 /* CAST_P will be set even in invalid code like "int(2.7 +
4165 ...)". Therefore, we have to check that the next token
4166 is sure to end the cast. */
4167 if (cast_p)
4168 {
4169 cp_token *next_token;
4170
4171 next_token = cp_lexer_peek_token (parser->lexer);
4172 if (/* The comma at the end of an
4173 enumerator-definition. */
4174 next_token->type != CPP_COMMA
4175 /* The curly brace at the end of an enum-specifier. */
4176 && next_token->type != CPP_CLOSE_BRACE
4177 /* The end of a statement. */
4178 && next_token->type != CPP_SEMICOLON
4179 /* The end of the cast-expression. */
4180 && next_token->type != CPP_CLOSE_PAREN
4181 /* The end of an array bound. */
4182 && next_token->type != CPP_CLOSE_SQUARE
4183 /* The closing ">" in a template-argument-list. */
4184 && (next_token->type != CPP_GREATER
4185 || parser->greater_than_is_operator_p)
4186 /* C++0x only: A ">>" treated like two ">" tokens,
4187 in a template-argument-list. */
4188 && (next_token->type != CPP_RSHIFT
4189 || (cxx_dialect == cxx98)
4190 || parser->greater_than_is_operator_p))
4191 cast_p = false;
4192 }
4193
4194 /* If we are within a cast, then the constraint that the
4195 cast is to an integral or enumeration type will be
4196 checked at that point. If we are not within a cast, then
4197 this code is invalid. */
4198 if (!cast_p)
4199 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4200 }
4201 return token->u.value;
4202
4203 case CPP_CHAR_USERDEF:
4204 case CPP_CHAR16_USERDEF:
4205 case CPP_CHAR32_USERDEF:
4206 case CPP_WCHAR_USERDEF:
4207 return cp_parser_userdef_char_literal (parser);
4208
4209 case CPP_STRING:
4210 case CPP_STRING16:
4211 case CPP_STRING32:
4212 case CPP_WSTRING:
4213 case CPP_UTF8STRING:
4214 case CPP_STRING_USERDEF:
4215 case CPP_STRING16_USERDEF:
4216 case CPP_STRING32_USERDEF:
4217 case CPP_WSTRING_USERDEF:
4218 case CPP_UTF8STRING_USERDEF:
4219 /* ??? Should wide strings be allowed when parser->translate_strings_p
4220 is false (i.e. in attributes)? If not, we can kill the third
4221 argument to cp_parser_string_literal. */
4222 return cp_parser_string_literal (parser,
4223 parser->translate_strings_p,
4224 true);
4225
4226 case CPP_OPEN_PAREN:
4227 {
4228 tree expr;
4229 bool saved_greater_than_is_operator_p;
4230
4231 /* Consume the `('. */
4232 cp_lexer_consume_token (parser->lexer);
4233 /* Within a parenthesized expression, a `>' token is always
4234 the greater-than operator. */
4235 saved_greater_than_is_operator_p
4236 = parser->greater_than_is_operator_p;
4237 parser->greater_than_is_operator_p = true;
4238 /* If we see `( { ' then we are looking at the beginning of
4239 a GNU statement-expression. */
4240 if (cp_parser_allow_gnu_extensions_p (parser)
4241 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4242 {
4243 /* Statement-expressions are not allowed by the standard. */
4244 pedwarn (token->location, OPT_Wpedantic,
4245 "ISO C++ forbids braced-groups within expressions");
4246
4247 /* And they're not allowed outside of a function-body; you
4248 cannot, for example, write:
4249
4250 int i = ({ int j = 3; j + 1; });
4251
4252 at class or namespace scope. */
4253 if (!parser->in_function_body
4254 || parser->in_template_argument_list_p)
4255 {
4256 error_at (token->location,
4257 "statement-expressions are not allowed outside "
4258 "functions nor in template-argument lists");
4259 cp_parser_skip_to_end_of_block_or_statement (parser);
4260 expr = error_mark_node;
4261 }
4262 else
4263 {
4264 /* Start the statement-expression. */
4265 expr = begin_stmt_expr ();
4266 /* Parse the compound-statement. */
4267 cp_parser_compound_statement (parser, expr, false, false);
4268 /* Finish up. */
4269 expr = finish_stmt_expr (expr, false);
4270 }
4271 }
4272 else
4273 {
4274 /* Parse the parenthesized expression. */
4275 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4276 /* Let the front end know that this expression was
4277 enclosed in parentheses. This matters in case, for
4278 example, the expression is of the form `A::B', since
4279 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4280 not. */
4281 expr = finish_parenthesized_expr (expr);
4282 /* DR 705: Wrapping an unqualified name in parentheses
4283 suppresses arg-dependent lookup. We want to pass back
4284 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4285 (c++/37862), but none of the others. */
4286 if (*idk != CP_ID_KIND_QUALIFIED)
4287 *idk = CP_ID_KIND_NONE;
4288 }
4289 /* The `>' token might be the end of a template-id or
4290 template-parameter-list now. */
4291 parser->greater_than_is_operator_p
4292 = saved_greater_than_is_operator_p;
4293 /* Consume the `)'. */
4294 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4295 cp_parser_skip_to_end_of_statement (parser);
4296
4297 return expr;
4298 }
4299
4300 case CPP_OPEN_SQUARE:
4301 if (c_dialect_objc ())
4302 /* We have an Objective-C++ message. */
4303 return cp_parser_objc_expression (parser);
4304 {
4305 tree lam = cp_parser_lambda_expression (parser);
4306 /* Don't warn about a failed tentative parse. */
4307 if (cp_parser_error_occurred (parser))
4308 return error_mark_node;
4309 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4310 return lam;
4311 }
4312
4313 case CPP_OBJC_STRING:
4314 if (c_dialect_objc ())
4315 /* We have an Objective-C++ string literal. */
4316 return cp_parser_objc_expression (parser);
4317 cp_parser_error (parser, "expected primary-expression");
4318 return error_mark_node;
4319
4320 case CPP_KEYWORD:
4321 switch (token->keyword)
4322 {
4323 /* These two are the boolean literals. */
4324 case RID_TRUE:
4325 cp_lexer_consume_token (parser->lexer);
4326 return boolean_true_node;
4327 case RID_FALSE:
4328 cp_lexer_consume_token (parser->lexer);
4329 return boolean_false_node;
4330
4331 /* The `__null' literal. */
4332 case RID_NULL:
4333 cp_lexer_consume_token (parser->lexer);
4334 return null_node;
4335
4336 /* The `nullptr' literal. */
4337 case RID_NULLPTR:
4338 cp_lexer_consume_token (parser->lexer);
4339 return nullptr_node;
4340
4341 /* Recognize the `this' keyword. */
4342 case RID_THIS:
4343 cp_lexer_consume_token (parser->lexer);
4344 if (parser->local_variables_forbidden_p)
4345 {
4346 error_at (token->location,
4347 "%<this%> may not be used in this context");
4348 return error_mark_node;
4349 }
4350 /* Pointers cannot appear in constant-expressions. */
4351 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4352 return error_mark_node;
4353 return finish_this_expr ();
4354
4355 /* The `operator' keyword can be the beginning of an
4356 id-expression. */
4357 case RID_OPERATOR:
4358 goto id_expression;
4359
4360 case RID_FUNCTION_NAME:
4361 case RID_PRETTY_FUNCTION_NAME:
4362 case RID_C99_FUNCTION_NAME:
4363 {
4364 non_integral_constant name;
4365
4366 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4367 __func__ are the names of variables -- but they are
4368 treated specially. Therefore, they are handled here,
4369 rather than relying on the generic id-expression logic
4370 below. Grammatically, these names are id-expressions.
4371
4372 Consume the token. */
4373 token = cp_lexer_consume_token (parser->lexer);
4374
4375 switch (token->keyword)
4376 {
4377 case RID_FUNCTION_NAME:
4378 name = NIC_FUNC_NAME;
4379 break;
4380 case RID_PRETTY_FUNCTION_NAME:
4381 name = NIC_PRETTY_FUNC;
4382 break;
4383 case RID_C99_FUNCTION_NAME:
4384 name = NIC_C99_FUNC;
4385 break;
4386 default:
4387 gcc_unreachable ();
4388 }
4389
4390 if (cp_parser_non_integral_constant_expression (parser, name))
4391 return error_mark_node;
4392
4393 /* Look up the name. */
4394 return finish_fname (token->u.value);
4395 }
4396
4397 case RID_VA_ARG:
4398 {
4399 tree expression;
4400 tree type;
4401 source_location type_location;
4402
4403 /* The `__builtin_va_arg' construct is used to handle
4404 `va_arg'. Consume the `__builtin_va_arg' token. */
4405 cp_lexer_consume_token (parser->lexer);
4406 /* Look for the opening `('. */
4407 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4408 /* Now, parse the assignment-expression. */
4409 expression = cp_parser_assignment_expression (parser,
4410 /*cast_p=*/false, NULL);
4411 /* Look for the `,'. */
4412 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4413 type_location = cp_lexer_peek_token (parser->lexer)->location;
4414 /* Parse the type-id. */
4415 type = cp_parser_type_id (parser);
4416 /* Look for the closing `)'. */
4417 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4418 /* Using `va_arg' in a constant-expression is not
4419 allowed. */
4420 if (cp_parser_non_integral_constant_expression (parser,
4421 NIC_VA_ARG))
4422 return error_mark_node;
4423 return build_x_va_arg (type_location, expression, type);
4424 }
4425
4426 case RID_OFFSETOF:
4427 return cp_parser_builtin_offsetof (parser);
4428
4429 case RID_HAS_NOTHROW_ASSIGN:
4430 case RID_HAS_NOTHROW_CONSTRUCTOR:
4431 case RID_HAS_NOTHROW_COPY:
4432 case RID_HAS_TRIVIAL_ASSIGN:
4433 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4434 case RID_HAS_TRIVIAL_COPY:
4435 case RID_HAS_TRIVIAL_DESTRUCTOR:
4436 case RID_HAS_VIRTUAL_DESTRUCTOR:
4437 case RID_IS_ABSTRACT:
4438 case RID_IS_BASE_OF:
4439 case RID_IS_CLASS:
4440 case RID_IS_CONVERTIBLE_TO:
4441 case RID_IS_EMPTY:
4442 case RID_IS_ENUM:
4443 case RID_IS_FINAL:
4444 case RID_IS_LITERAL_TYPE:
4445 case RID_IS_POD:
4446 case RID_IS_POLYMORPHIC:
4447 case RID_IS_STD_LAYOUT:
4448 case RID_IS_TRIVIAL:
4449 case RID_IS_UNION:
4450 return cp_parser_trait_expr (parser, token->keyword);
4451
4452 /* Objective-C++ expressions. */
4453 case RID_AT_ENCODE:
4454 case RID_AT_PROTOCOL:
4455 case RID_AT_SELECTOR:
4456 return cp_parser_objc_expression (parser);
4457
4458 case RID_TEMPLATE:
4459 if (parser->in_function_body
4460 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4461 == CPP_LESS))
4462 {
4463 error_at (token->location,
4464 "a template declaration cannot appear at block scope");
4465 cp_parser_skip_to_end_of_block_or_statement (parser);
4466 return error_mark_node;
4467 }
4468 default:
4469 cp_parser_error (parser, "expected primary-expression");
4470 return error_mark_node;
4471 }
4472
4473 /* An id-expression can start with either an identifier, a
4474 `::' as the beginning of a qualified-id, or the "operator"
4475 keyword. */
4476 case CPP_NAME:
4477 case CPP_SCOPE:
4478 case CPP_TEMPLATE_ID:
4479 case CPP_NESTED_NAME_SPECIFIER:
4480 {
4481 tree id_expression;
4482 tree decl;
4483 const char *error_msg;
4484 bool template_p;
4485 bool done;
4486 cp_token *id_expr_token;
4487
4488 id_expression:
4489 /* Parse the id-expression. */
4490 id_expression
4491 = cp_parser_id_expression (parser,
4492 /*template_keyword_p=*/false,
4493 /*check_dependency_p=*/true,
4494 &template_p,
4495 /*declarator_p=*/false,
4496 /*optional_p=*/false);
4497 if (id_expression == error_mark_node)
4498 return error_mark_node;
4499 id_expr_token = token;
4500 token = cp_lexer_peek_token (parser->lexer);
4501 done = (token->type != CPP_OPEN_SQUARE
4502 && token->type != CPP_OPEN_PAREN
4503 && token->type != CPP_DOT
4504 && token->type != CPP_DEREF
4505 && token->type != CPP_PLUS_PLUS
4506 && token->type != CPP_MINUS_MINUS);
4507 /* If we have a template-id, then no further lookup is
4508 required. If the template-id was for a template-class, we
4509 will sometimes have a TYPE_DECL at this point. */
4510 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4511 || TREE_CODE (id_expression) == TYPE_DECL)
4512 decl = id_expression;
4513 /* Look up the name. */
4514 else
4515 {
4516 tree ambiguous_decls;
4517
4518 /* If we already know that this lookup is ambiguous, then
4519 we've already issued an error message; there's no reason
4520 to check again. */
4521 if (id_expr_token->type == CPP_NAME
4522 && id_expr_token->ambiguous_p)
4523 {
4524 cp_parser_simulate_error (parser);
4525 return error_mark_node;
4526 }
4527
4528 decl = cp_parser_lookup_name (parser, id_expression,
4529 none_type,
4530 template_p,
4531 /*is_namespace=*/false,
4532 /*check_dependency=*/true,
4533 &ambiguous_decls,
4534 id_expr_token->location);
4535 /* If the lookup was ambiguous, an error will already have
4536 been issued. */
4537 if (ambiguous_decls)
4538 return error_mark_node;
4539
4540 /* In Objective-C++, we may have an Objective-C 2.0
4541 dot-syntax for classes here. */
4542 if (c_dialect_objc ()
4543 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4544 && TREE_CODE (decl) == TYPE_DECL
4545 && objc_is_class_name (decl))
4546 {
4547 tree component;
4548 cp_lexer_consume_token (parser->lexer);
4549 component = cp_parser_identifier (parser);
4550 if (component == error_mark_node)
4551 return error_mark_node;
4552
4553 return objc_build_class_component_ref (id_expression, component);
4554 }
4555
4556 /* In Objective-C++, an instance variable (ivar) may be preferred
4557 to whatever cp_parser_lookup_name() found. */
4558 decl = objc_lookup_ivar (decl, id_expression);
4559
4560 /* If name lookup gives us a SCOPE_REF, then the
4561 qualifying scope was dependent. */
4562 if (TREE_CODE (decl) == SCOPE_REF)
4563 {
4564 /* At this point, we do not know if DECL is a valid
4565 integral constant expression. We assume that it is
4566 in fact such an expression, so that code like:
4567
4568 template <int N> struct A {
4569 int a[B<N>::i];
4570 };
4571
4572 is accepted. At template-instantiation time, we
4573 will check that B<N>::i is actually a constant. */
4574 return decl;
4575 }
4576 /* Check to see if DECL is a local variable in a context
4577 where that is forbidden. */
4578 if (parser->local_variables_forbidden_p
4579 && local_variable_p (decl))
4580 {
4581 /* It might be that we only found DECL because we are
4582 trying to be generous with pre-ISO scoping rules.
4583 For example, consider:
4584
4585 int i;
4586 void g() {
4587 for (int i = 0; i < 10; ++i) {}
4588 extern void f(int j = i);
4589 }
4590
4591 Here, name look up will originally find the out
4592 of scope `i'. We need to issue a warning message,
4593 but then use the global `i'. */
4594 decl = check_for_out_of_scope_variable (decl);
4595 if (local_variable_p (decl))
4596 {
4597 error_at (id_expr_token->location,
4598 "local variable %qD may not appear in this context",
4599 decl);
4600 return error_mark_node;
4601 }
4602 }
4603 }
4604
4605 decl = (finish_id_expression
4606 (id_expression, decl, parser->scope,
4607 idk,
4608 parser->integral_constant_expression_p,
4609 parser->allow_non_integral_constant_expression_p,
4610 &parser->non_integral_constant_expression_p,
4611 template_p, done, address_p,
4612 template_arg_p,
4613 &error_msg,
4614 id_expr_token->location));
4615 if (error_msg)
4616 cp_parser_error (parser, error_msg);
4617 return decl;
4618 }
4619
4620 /* Anything else is an error. */
4621 default:
4622 cp_parser_error (parser, "expected primary-expression");
4623 return error_mark_node;
4624 }
4625 }
4626
4627 static inline tree
4628 cp_parser_primary_expression (cp_parser *parser,
4629 bool address_p,
4630 bool cast_p,
4631 bool template_arg_p,
4632 cp_id_kind *idk)
4633 {
4634 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4635 /*decltype*/false, idk);
4636 }
4637
4638 /* Parse an id-expression.
4639
4640 id-expression:
4641 unqualified-id
4642 qualified-id
4643
4644 qualified-id:
4645 :: [opt] nested-name-specifier template [opt] unqualified-id
4646 :: identifier
4647 :: operator-function-id
4648 :: template-id
4649
4650 Return a representation of the unqualified portion of the
4651 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4652 a `::' or nested-name-specifier.
4653
4654 Often, if the id-expression was a qualified-id, the caller will
4655 want to make a SCOPE_REF to represent the qualified-id. This
4656 function does not do this in order to avoid wastefully creating
4657 SCOPE_REFs when they are not required.
4658
4659 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4660 `template' keyword.
4661
4662 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4663 uninstantiated templates.
4664
4665 If *TEMPLATE_P is non-NULL, it is set to true iff the
4666 `template' keyword is used to explicitly indicate that the entity
4667 named is a template.
4668
4669 If DECLARATOR_P is true, the id-expression is appearing as part of
4670 a declarator, rather than as part of an expression. */
4671
4672 static tree
4673 cp_parser_id_expression (cp_parser *parser,
4674 bool template_keyword_p,
4675 bool check_dependency_p,
4676 bool *template_p,
4677 bool declarator_p,
4678 bool optional_p)
4679 {
4680 bool global_scope_p;
4681 bool nested_name_specifier_p;
4682
4683 /* Assume the `template' keyword was not used. */
4684 if (template_p)
4685 *template_p = template_keyword_p;
4686
4687 /* Look for the optional `::' operator. */
4688 global_scope_p
4689 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4690 != NULL_TREE);
4691 /* Look for the optional nested-name-specifier. */
4692 nested_name_specifier_p
4693 = (cp_parser_nested_name_specifier_opt (parser,
4694 /*typename_keyword_p=*/false,
4695 check_dependency_p,
4696 /*type_p=*/false,
4697 declarator_p)
4698 != NULL_TREE);
4699 /* If there is a nested-name-specifier, then we are looking at
4700 the first qualified-id production. */
4701 if (nested_name_specifier_p)
4702 {
4703 tree saved_scope;
4704 tree saved_object_scope;
4705 tree saved_qualifying_scope;
4706 tree unqualified_id;
4707 bool is_template;
4708
4709 /* See if the next token is the `template' keyword. */
4710 if (!template_p)
4711 template_p = &is_template;
4712 *template_p = cp_parser_optional_template_keyword (parser);
4713 /* Name lookup we do during the processing of the
4714 unqualified-id might obliterate SCOPE. */
4715 saved_scope = parser->scope;
4716 saved_object_scope = parser->object_scope;
4717 saved_qualifying_scope = parser->qualifying_scope;
4718 /* Process the final unqualified-id. */
4719 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4720 check_dependency_p,
4721 declarator_p,
4722 /*optional_p=*/false);
4723 /* Restore the SAVED_SCOPE for our caller. */
4724 parser->scope = saved_scope;
4725 parser->object_scope = saved_object_scope;
4726 parser->qualifying_scope = saved_qualifying_scope;
4727
4728 return unqualified_id;
4729 }
4730 /* Otherwise, if we are in global scope, then we are looking at one
4731 of the other qualified-id productions. */
4732 else if (global_scope_p)
4733 {
4734 cp_token *token;
4735 tree id;
4736
4737 /* Peek at the next token. */
4738 token = cp_lexer_peek_token (parser->lexer);
4739
4740 /* If it's an identifier, and the next token is not a "<", then
4741 we can avoid the template-id case. This is an optimization
4742 for this common case. */
4743 if (token->type == CPP_NAME
4744 && !cp_parser_nth_token_starts_template_argument_list_p
4745 (parser, 2))
4746 return cp_parser_identifier (parser);
4747
4748 cp_parser_parse_tentatively (parser);
4749 /* Try a template-id. */
4750 id = cp_parser_template_id (parser,
4751 /*template_keyword_p=*/false,
4752 /*check_dependency_p=*/true,
4753 none_type,
4754 declarator_p);
4755 /* If that worked, we're done. */
4756 if (cp_parser_parse_definitely (parser))
4757 return id;
4758
4759 /* Peek at the next token. (Changes in the token buffer may
4760 have invalidated the pointer obtained above.) */
4761 token = cp_lexer_peek_token (parser->lexer);
4762
4763 switch (token->type)
4764 {
4765 case CPP_NAME:
4766 return cp_parser_identifier (parser);
4767
4768 case CPP_KEYWORD:
4769 if (token->keyword == RID_OPERATOR)
4770 return cp_parser_operator_function_id (parser);
4771 /* Fall through. */
4772
4773 default:
4774 cp_parser_error (parser, "expected id-expression");
4775 return error_mark_node;
4776 }
4777 }
4778 else
4779 return cp_parser_unqualified_id (parser, template_keyword_p,
4780 /*check_dependency_p=*/true,
4781 declarator_p,
4782 optional_p);
4783 }
4784
4785 /* Parse an unqualified-id.
4786
4787 unqualified-id:
4788 identifier
4789 operator-function-id
4790 conversion-function-id
4791 ~ class-name
4792 template-id
4793
4794 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4795 keyword, in a construct like `A::template ...'.
4796
4797 Returns a representation of unqualified-id. For the `identifier'
4798 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4799 production a BIT_NOT_EXPR is returned; the operand of the
4800 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4801 other productions, see the documentation accompanying the
4802 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4803 names are looked up in uninstantiated templates. If DECLARATOR_P
4804 is true, the unqualified-id is appearing as part of a declarator,
4805 rather than as part of an expression. */
4806
4807 static tree
4808 cp_parser_unqualified_id (cp_parser* parser,
4809 bool template_keyword_p,
4810 bool check_dependency_p,
4811 bool declarator_p,
4812 bool optional_p)
4813 {
4814 cp_token *token;
4815
4816 /* Peek at the next token. */
4817 token = cp_lexer_peek_token (parser->lexer);
4818
4819 switch (token->type)
4820 {
4821 case CPP_NAME:
4822 {
4823 tree id;
4824
4825 /* We don't know yet whether or not this will be a
4826 template-id. */
4827 cp_parser_parse_tentatively (parser);
4828 /* Try a template-id. */
4829 id = cp_parser_template_id (parser, template_keyword_p,
4830 check_dependency_p,
4831 none_type,
4832 declarator_p);
4833 /* If it worked, we're done. */
4834 if (cp_parser_parse_definitely (parser))
4835 return id;
4836 /* Otherwise, it's an ordinary identifier. */
4837 return cp_parser_identifier (parser);
4838 }
4839
4840 case CPP_TEMPLATE_ID:
4841 return cp_parser_template_id (parser, template_keyword_p,
4842 check_dependency_p,
4843 none_type,
4844 declarator_p);
4845
4846 case CPP_COMPL:
4847 {
4848 tree type_decl;
4849 tree qualifying_scope;
4850 tree object_scope;
4851 tree scope;
4852 bool done;
4853
4854 /* Consume the `~' token. */
4855 cp_lexer_consume_token (parser->lexer);
4856 /* Parse the class-name. The standard, as written, seems to
4857 say that:
4858
4859 template <typename T> struct S { ~S (); };
4860 template <typename T> S<T>::~S() {}
4861
4862 is invalid, since `~' must be followed by a class-name, but
4863 `S<T>' is dependent, and so not known to be a class.
4864 That's not right; we need to look in uninstantiated
4865 templates. A further complication arises from:
4866
4867 template <typename T> void f(T t) {
4868 t.T::~T();
4869 }
4870
4871 Here, it is not possible to look up `T' in the scope of `T'
4872 itself. We must look in both the current scope, and the
4873 scope of the containing complete expression.
4874
4875 Yet another issue is:
4876
4877 struct S {
4878 int S;
4879 ~S();
4880 };
4881
4882 S::~S() {}
4883
4884 The standard does not seem to say that the `S' in `~S'
4885 should refer to the type `S' and not the data member
4886 `S::S'. */
4887
4888 /* DR 244 says that we look up the name after the "~" in the
4889 same scope as we looked up the qualifying name. That idea
4890 isn't fully worked out; it's more complicated than that. */
4891 scope = parser->scope;
4892 object_scope = parser->object_scope;
4893 qualifying_scope = parser->qualifying_scope;
4894
4895 /* Check for invalid scopes. */
4896 if (scope == error_mark_node)
4897 {
4898 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4899 cp_lexer_consume_token (parser->lexer);
4900 return error_mark_node;
4901 }
4902 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4903 {
4904 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4905 error_at (token->location,
4906 "scope %qT before %<~%> is not a class-name",
4907 scope);
4908 cp_parser_simulate_error (parser);
4909 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4910 cp_lexer_consume_token (parser->lexer);
4911 return error_mark_node;
4912 }
4913 gcc_assert (!scope || TYPE_P (scope));
4914
4915 /* If the name is of the form "X::~X" it's OK even if X is a
4916 typedef. */
4917 token = cp_lexer_peek_token (parser->lexer);
4918 if (scope
4919 && token->type == CPP_NAME
4920 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4921 != CPP_LESS)
4922 && (token->u.value == TYPE_IDENTIFIER (scope)
4923 || (CLASS_TYPE_P (scope)
4924 && constructor_name_p (token->u.value, scope))))
4925 {
4926 cp_lexer_consume_token (parser->lexer);
4927 return build_nt (BIT_NOT_EXPR, scope);
4928 }
4929
4930 /* ~auto means the destructor of whatever the object is. */
4931 if (cp_parser_is_keyword (token, RID_AUTO))
4932 {
4933 if (cxx_dialect < cxx1y)
4934 pedwarn (input_location, 0,
4935 "%<~auto%> only available with "
4936 "-std=c++1y or -std=gnu++1y");
4937 cp_lexer_consume_token (parser->lexer);
4938 return build_nt (BIT_NOT_EXPR, make_auto ());
4939 }
4940
4941 /* If there was an explicit qualification (S::~T), first look
4942 in the scope given by the qualification (i.e., S).
4943
4944 Note: in the calls to cp_parser_class_name below we pass
4945 typename_type so that lookup finds the injected-class-name
4946 rather than the constructor. */
4947 done = false;
4948 type_decl = NULL_TREE;
4949 if (scope)
4950 {
4951 cp_parser_parse_tentatively (parser);
4952 type_decl = cp_parser_class_name (parser,
4953 /*typename_keyword_p=*/false,
4954 /*template_keyword_p=*/false,
4955 typename_type,
4956 /*check_dependency=*/false,
4957 /*class_head_p=*/false,
4958 declarator_p);
4959 if (cp_parser_parse_definitely (parser))
4960 done = true;
4961 }
4962 /* In "N::S::~S", look in "N" as well. */
4963 if (!done && scope && qualifying_scope)
4964 {
4965 cp_parser_parse_tentatively (parser);
4966 parser->scope = qualifying_scope;
4967 parser->object_scope = NULL_TREE;
4968 parser->qualifying_scope = NULL_TREE;
4969 type_decl
4970 = cp_parser_class_name (parser,
4971 /*typename_keyword_p=*/false,
4972 /*template_keyword_p=*/false,
4973 typename_type,
4974 /*check_dependency=*/false,
4975 /*class_head_p=*/false,
4976 declarator_p);
4977 if (cp_parser_parse_definitely (parser))
4978 done = true;
4979 }
4980 /* In "p->S::~T", look in the scope given by "*p" as well. */
4981 else if (!done && object_scope)
4982 {
4983 cp_parser_parse_tentatively (parser);
4984 parser->scope = object_scope;
4985 parser->object_scope = NULL_TREE;
4986 parser->qualifying_scope = NULL_TREE;
4987 type_decl
4988 = cp_parser_class_name (parser,
4989 /*typename_keyword_p=*/false,
4990 /*template_keyword_p=*/false,
4991 typename_type,
4992 /*check_dependency=*/false,
4993 /*class_head_p=*/false,
4994 declarator_p);
4995 if (cp_parser_parse_definitely (parser))
4996 done = true;
4997 }
4998 /* Look in the surrounding context. */
4999 if (!done)
5000 {
5001 parser->scope = NULL_TREE;
5002 parser->object_scope = NULL_TREE;
5003 parser->qualifying_scope = NULL_TREE;
5004 if (processing_template_decl)
5005 cp_parser_parse_tentatively (parser);
5006 type_decl
5007 = cp_parser_class_name (parser,
5008 /*typename_keyword_p=*/false,
5009 /*template_keyword_p=*/false,
5010 typename_type,
5011 /*check_dependency=*/false,
5012 /*class_head_p=*/false,
5013 declarator_p);
5014 if (processing_template_decl
5015 && ! cp_parser_parse_definitely (parser))
5016 {
5017 /* We couldn't find a type with this name, so just accept
5018 it and check for a match at instantiation time. */
5019 type_decl = cp_parser_identifier (parser);
5020 if (type_decl != error_mark_node)
5021 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5022 return type_decl;
5023 }
5024 }
5025 /* If an error occurred, assume that the name of the
5026 destructor is the same as the name of the qualifying
5027 class. That allows us to keep parsing after running
5028 into ill-formed destructor names. */
5029 if (type_decl == error_mark_node && scope)
5030 return build_nt (BIT_NOT_EXPR, scope);
5031 else if (type_decl == error_mark_node)
5032 return error_mark_node;
5033
5034 /* Check that destructor name and scope match. */
5035 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5036 {
5037 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5038 error_at (token->location,
5039 "declaration of %<~%T%> as member of %qT",
5040 type_decl, scope);
5041 cp_parser_simulate_error (parser);
5042 return error_mark_node;
5043 }
5044
5045 /* [class.dtor]
5046
5047 A typedef-name that names a class shall not be used as the
5048 identifier in the declarator for a destructor declaration. */
5049 if (declarator_p
5050 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5051 && !DECL_SELF_REFERENCE_P (type_decl)
5052 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5053 error_at (token->location,
5054 "typedef-name %qD used as destructor declarator",
5055 type_decl);
5056
5057 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5058 }
5059
5060 case CPP_KEYWORD:
5061 if (token->keyword == RID_OPERATOR)
5062 {
5063 tree id;
5064
5065 /* This could be a template-id, so we try that first. */
5066 cp_parser_parse_tentatively (parser);
5067 /* Try a template-id. */
5068 id = cp_parser_template_id (parser, template_keyword_p,
5069 /*check_dependency_p=*/true,
5070 none_type,
5071 declarator_p);
5072 /* If that worked, we're done. */
5073 if (cp_parser_parse_definitely (parser))
5074 return id;
5075 /* We still don't know whether we're looking at an
5076 operator-function-id or a conversion-function-id. */
5077 cp_parser_parse_tentatively (parser);
5078 /* Try an operator-function-id. */
5079 id = cp_parser_operator_function_id (parser);
5080 /* If that didn't work, try a conversion-function-id. */
5081 if (!cp_parser_parse_definitely (parser))
5082 id = cp_parser_conversion_function_id (parser);
5083 else if (UDLIT_OPER_P (id))
5084 {
5085 /* 17.6.3.3.5 */
5086 const char *name = UDLIT_OP_SUFFIX (id);
5087 if (name[0] != '_' && !in_system_header && declarator_p)
5088 warning (0, "literal operator suffixes not preceded by %<_%>"
5089 " are reserved for future standardization");
5090 }
5091
5092 return id;
5093 }
5094 /* Fall through. */
5095
5096 default:
5097 if (optional_p)
5098 return NULL_TREE;
5099 cp_parser_error (parser, "expected unqualified-id");
5100 return error_mark_node;
5101 }
5102 }
5103
5104 /* Parse an (optional) nested-name-specifier.
5105
5106 nested-name-specifier: [C++98]
5107 class-or-namespace-name :: nested-name-specifier [opt]
5108 class-or-namespace-name :: template nested-name-specifier [opt]
5109
5110 nested-name-specifier: [C++0x]
5111 type-name ::
5112 namespace-name ::
5113 nested-name-specifier identifier ::
5114 nested-name-specifier template [opt] simple-template-id ::
5115
5116 PARSER->SCOPE should be set appropriately before this function is
5117 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5118 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5119 in name lookups.
5120
5121 Sets PARSER->SCOPE to the class (TYPE) or namespace
5122 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5123 it unchanged if there is no nested-name-specifier. Returns the new
5124 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5125
5126 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5127 part of a declaration and/or decl-specifier. */
5128
5129 static tree
5130 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5131 bool typename_keyword_p,
5132 bool check_dependency_p,
5133 bool type_p,
5134 bool is_declaration)
5135 {
5136 bool success = false;
5137 cp_token_position start = 0;
5138 cp_token *token;
5139
5140 /* Remember where the nested-name-specifier starts. */
5141 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5142 {
5143 start = cp_lexer_token_position (parser->lexer, false);
5144 push_deferring_access_checks (dk_deferred);
5145 }
5146
5147 while (true)
5148 {
5149 tree new_scope;
5150 tree old_scope;
5151 tree saved_qualifying_scope;
5152 bool template_keyword_p;
5153
5154 /* Spot cases that cannot be the beginning of a
5155 nested-name-specifier. */
5156 token = cp_lexer_peek_token (parser->lexer);
5157
5158 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5159 the already parsed nested-name-specifier. */
5160 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5161 {
5162 /* Grab the nested-name-specifier and continue the loop. */
5163 cp_parser_pre_parsed_nested_name_specifier (parser);
5164 /* If we originally encountered this nested-name-specifier
5165 with IS_DECLARATION set to false, we will not have
5166 resolved TYPENAME_TYPEs, so we must do so here. */
5167 if (is_declaration
5168 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5169 {
5170 new_scope = resolve_typename_type (parser->scope,
5171 /*only_current_p=*/false);
5172 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5173 parser->scope = new_scope;
5174 }
5175 success = true;
5176 continue;
5177 }
5178
5179 /* Spot cases that cannot be the beginning of a
5180 nested-name-specifier. On the second and subsequent times
5181 through the loop, we look for the `template' keyword. */
5182 if (success && token->keyword == RID_TEMPLATE)
5183 ;
5184 /* A template-id can start a nested-name-specifier. */
5185 else if (token->type == CPP_TEMPLATE_ID)
5186 ;
5187 /* DR 743: decltype can be used in a nested-name-specifier. */
5188 else if (token_is_decltype (token))
5189 ;
5190 else
5191 {
5192 /* If the next token is not an identifier, then it is
5193 definitely not a type-name or namespace-name. */
5194 if (token->type != CPP_NAME)
5195 break;
5196 /* If the following token is neither a `<' (to begin a
5197 template-id), nor a `::', then we are not looking at a
5198 nested-name-specifier. */
5199 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5200
5201 if (token->type == CPP_COLON
5202 && parser->colon_corrects_to_scope_p
5203 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5204 {
5205 error_at (token->location,
5206 "found %<:%> in nested-name-specifier, expected %<::%>");
5207 token->type = CPP_SCOPE;
5208 }
5209
5210 if (token->type != CPP_SCOPE
5211 && !cp_parser_nth_token_starts_template_argument_list_p
5212 (parser, 2))
5213 break;
5214 }
5215
5216 /* The nested-name-specifier is optional, so we parse
5217 tentatively. */
5218 cp_parser_parse_tentatively (parser);
5219
5220 /* Look for the optional `template' keyword, if this isn't the
5221 first time through the loop. */
5222 if (success)
5223 template_keyword_p = cp_parser_optional_template_keyword (parser);
5224 else
5225 template_keyword_p = false;
5226
5227 /* Save the old scope since the name lookup we are about to do
5228 might destroy it. */
5229 old_scope = parser->scope;
5230 saved_qualifying_scope = parser->qualifying_scope;
5231 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5232 look up names in "X<T>::I" in order to determine that "Y" is
5233 a template. So, if we have a typename at this point, we make
5234 an effort to look through it. */
5235 if (is_declaration
5236 && !typename_keyword_p
5237 && parser->scope
5238 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5239 parser->scope = resolve_typename_type (parser->scope,
5240 /*only_current_p=*/false);
5241 /* Parse the qualifying entity. */
5242 new_scope
5243 = cp_parser_qualifying_entity (parser,
5244 typename_keyword_p,
5245 template_keyword_p,
5246 check_dependency_p,
5247 type_p,
5248 is_declaration);
5249 /* Look for the `::' token. */
5250 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5251
5252 /* If we found what we wanted, we keep going; otherwise, we're
5253 done. */
5254 if (!cp_parser_parse_definitely (parser))
5255 {
5256 bool error_p = false;
5257
5258 /* Restore the OLD_SCOPE since it was valid before the
5259 failed attempt at finding the last
5260 class-or-namespace-name. */
5261 parser->scope = old_scope;
5262 parser->qualifying_scope = saved_qualifying_scope;
5263
5264 /* If the next token is a decltype, and the one after that is a
5265 `::', then the decltype has failed to resolve to a class or
5266 enumeration type. Give this error even when parsing
5267 tentatively since it can't possibly be valid--and we're going
5268 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5269 won't get another chance.*/
5270 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5271 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5272 == CPP_SCOPE))
5273 {
5274 token = cp_lexer_consume_token (parser->lexer);
5275 error_at (token->location, "decltype evaluates to %qT, "
5276 "which is not a class or enumeration type",
5277 token->u.value);
5278 parser->scope = error_mark_node;
5279 error_p = true;
5280 /* As below. */
5281 success = true;
5282 cp_lexer_consume_token (parser->lexer);
5283 }
5284
5285 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5286 break;
5287 /* If the next token is an identifier, and the one after
5288 that is a `::', then any valid interpretation would have
5289 found a class-or-namespace-name. */
5290 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5291 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5292 == CPP_SCOPE)
5293 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5294 != CPP_COMPL))
5295 {
5296 token = cp_lexer_consume_token (parser->lexer);
5297 if (!error_p)
5298 {
5299 if (!token->ambiguous_p)
5300 {
5301 tree decl;
5302 tree ambiguous_decls;
5303
5304 decl = cp_parser_lookup_name (parser, token->u.value,
5305 none_type,
5306 /*is_template=*/false,
5307 /*is_namespace=*/false,
5308 /*check_dependency=*/true,
5309 &ambiguous_decls,
5310 token->location);
5311 if (TREE_CODE (decl) == TEMPLATE_DECL)
5312 error_at (token->location,
5313 "%qD used without template parameters",
5314 decl);
5315 else if (ambiguous_decls)
5316 {
5317 // cp_parser_lookup_name has the same diagnostic,
5318 // thus make sure to emit it at most once.
5319 if (cp_parser_uncommitted_to_tentative_parse_p
5320 (parser))
5321 {
5322 error_at (token->location,
5323 "reference to %qD is ambiguous",
5324 token->u.value);
5325 print_candidates (ambiguous_decls);
5326 }
5327 decl = error_mark_node;
5328 }
5329 else
5330 {
5331 if (cxx_dialect != cxx98)
5332 cp_parser_name_lookup_error
5333 (parser, token->u.value, decl, NLE_NOT_CXX98,
5334 token->location);
5335 else
5336 cp_parser_name_lookup_error
5337 (parser, token->u.value, decl, NLE_CXX98,
5338 token->location);
5339 }
5340 }
5341 parser->scope = error_mark_node;
5342 error_p = true;
5343 /* Treat this as a successful nested-name-specifier
5344 due to:
5345
5346 [basic.lookup.qual]
5347
5348 If the name found is not a class-name (clause
5349 _class_) or namespace-name (_namespace.def_), the
5350 program is ill-formed. */
5351 success = true;
5352 }
5353 cp_lexer_consume_token (parser->lexer);
5354 }
5355 break;
5356 }
5357 /* We've found one valid nested-name-specifier. */
5358 success = true;
5359 /* Name lookup always gives us a DECL. */
5360 if (TREE_CODE (new_scope) == TYPE_DECL)
5361 new_scope = TREE_TYPE (new_scope);
5362 /* Uses of "template" must be followed by actual templates. */
5363 if (template_keyword_p
5364 && !(CLASS_TYPE_P (new_scope)
5365 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5366 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5367 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5368 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5369 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5370 == TEMPLATE_ID_EXPR)))
5371 permerror (input_location, TYPE_P (new_scope)
5372 ? G_("%qT is not a template")
5373 : G_("%qD is not a template"),
5374 new_scope);
5375 /* If it is a class scope, try to complete it; we are about to
5376 be looking up names inside the class. */
5377 if (TYPE_P (new_scope)
5378 /* Since checking types for dependency can be expensive,
5379 avoid doing it if the type is already complete. */
5380 && !COMPLETE_TYPE_P (new_scope)
5381 /* Do not try to complete dependent types. */
5382 && !dependent_type_p (new_scope))
5383 {
5384 new_scope = complete_type (new_scope);
5385 /* If it is a typedef to current class, use the current
5386 class instead, as the typedef won't have any names inside
5387 it yet. */
5388 if (!COMPLETE_TYPE_P (new_scope)
5389 && currently_open_class (new_scope))
5390 new_scope = TYPE_MAIN_VARIANT (new_scope);
5391 }
5392 /* Make sure we look in the right scope the next time through
5393 the loop. */
5394 parser->scope = new_scope;
5395 }
5396
5397 /* If parsing tentatively, replace the sequence of tokens that makes
5398 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5399 token. That way, should we re-parse the token stream, we will
5400 not have to repeat the effort required to do the parse, nor will
5401 we issue duplicate error messages. */
5402 if (success && start)
5403 {
5404 cp_token *token;
5405
5406 token = cp_lexer_token_at (parser->lexer, start);
5407 /* Reset the contents of the START token. */
5408 token->type = CPP_NESTED_NAME_SPECIFIER;
5409 /* Retrieve any deferred checks. Do not pop this access checks yet
5410 so the memory will not be reclaimed during token replacing below. */
5411 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5412 token->u.tree_check_value->value = parser->scope;
5413 token->u.tree_check_value->checks = get_deferred_access_checks ();
5414 token->u.tree_check_value->qualifying_scope =
5415 parser->qualifying_scope;
5416 token->keyword = RID_MAX;
5417
5418 /* Purge all subsequent tokens. */
5419 cp_lexer_purge_tokens_after (parser->lexer, start);
5420 }
5421
5422 if (start)
5423 pop_to_parent_deferring_access_checks ();
5424
5425 return success ? parser->scope : NULL_TREE;
5426 }
5427
5428 /* Parse a nested-name-specifier. See
5429 cp_parser_nested_name_specifier_opt for details. This function
5430 behaves identically, except that it will an issue an error if no
5431 nested-name-specifier is present. */
5432
5433 static tree
5434 cp_parser_nested_name_specifier (cp_parser *parser,
5435 bool typename_keyword_p,
5436 bool check_dependency_p,
5437 bool type_p,
5438 bool is_declaration)
5439 {
5440 tree scope;
5441
5442 /* Look for the nested-name-specifier. */
5443 scope = cp_parser_nested_name_specifier_opt (parser,
5444 typename_keyword_p,
5445 check_dependency_p,
5446 type_p,
5447 is_declaration);
5448 /* If it was not present, issue an error message. */
5449 if (!scope)
5450 {
5451 cp_parser_error (parser, "expected nested-name-specifier");
5452 parser->scope = NULL_TREE;
5453 }
5454
5455 return scope;
5456 }
5457
5458 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5459 this is either a class-name or a namespace-name (which corresponds
5460 to the class-or-namespace-name production in the grammar). For
5461 C++0x, it can also be a type-name that refers to an enumeration
5462 type or a simple-template-id.
5463
5464 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5465 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5466 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5467 TYPE_P is TRUE iff the next name should be taken as a class-name,
5468 even the same name is declared to be another entity in the same
5469 scope.
5470
5471 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5472 specified by the class-or-namespace-name. If neither is found the
5473 ERROR_MARK_NODE is returned. */
5474
5475 static tree
5476 cp_parser_qualifying_entity (cp_parser *parser,
5477 bool typename_keyword_p,
5478 bool template_keyword_p,
5479 bool check_dependency_p,
5480 bool type_p,
5481 bool is_declaration)
5482 {
5483 tree saved_scope;
5484 tree saved_qualifying_scope;
5485 tree saved_object_scope;
5486 tree scope;
5487 bool only_class_p;
5488 bool successful_parse_p;
5489
5490 /* DR 743: decltype can appear in a nested-name-specifier. */
5491 if (cp_lexer_next_token_is_decltype (parser->lexer))
5492 {
5493 scope = cp_parser_decltype (parser);
5494 if (TREE_CODE (scope) != ENUMERAL_TYPE
5495 && !MAYBE_CLASS_TYPE_P (scope))
5496 {
5497 cp_parser_simulate_error (parser);
5498 return error_mark_node;
5499 }
5500 if (TYPE_NAME (scope))
5501 scope = TYPE_NAME (scope);
5502 return scope;
5503 }
5504
5505 /* Before we try to parse the class-name, we must save away the
5506 current PARSER->SCOPE since cp_parser_class_name will destroy
5507 it. */
5508 saved_scope = parser->scope;
5509 saved_qualifying_scope = parser->qualifying_scope;
5510 saved_object_scope = parser->object_scope;
5511 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5512 there is no need to look for a namespace-name. */
5513 only_class_p = template_keyword_p
5514 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5515 if (!only_class_p)
5516 cp_parser_parse_tentatively (parser);
5517 scope = cp_parser_class_name (parser,
5518 typename_keyword_p,
5519 template_keyword_p,
5520 type_p ? class_type : none_type,
5521 check_dependency_p,
5522 /*class_head_p=*/false,
5523 is_declaration);
5524 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5525 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5526 if (!only_class_p
5527 && cxx_dialect != cxx98
5528 && !successful_parse_p)
5529 {
5530 /* Restore the saved scope. */
5531 parser->scope = saved_scope;
5532 parser->qualifying_scope = saved_qualifying_scope;
5533 parser->object_scope = saved_object_scope;
5534
5535 /* Parse tentatively. */
5536 cp_parser_parse_tentatively (parser);
5537
5538 /* Parse a type-name */
5539 scope = cp_parser_type_name (parser);
5540
5541 /* "If the name found does not designate a namespace or a class,
5542 enumeration, or dependent type, the program is ill-formed."
5543
5544 We cover classes and dependent types above and namespaces below,
5545 so this code is only looking for enums. */
5546 if (!scope || TREE_CODE (scope) != TYPE_DECL
5547 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5548 cp_parser_simulate_error (parser);
5549
5550 successful_parse_p = cp_parser_parse_definitely (parser);
5551 }
5552 /* If that didn't work, try for a namespace-name. */
5553 if (!only_class_p && !successful_parse_p)
5554 {
5555 /* Restore the saved scope. */
5556 parser->scope = saved_scope;
5557 parser->qualifying_scope = saved_qualifying_scope;
5558 parser->object_scope = saved_object_scope;
5559 /* If we are not looking at an identifier followed by the scope
5560 resolution operator, then this is not part of a
5561 nested-name-specifier. (Note that this function is only used
5562 to parse the components of a nested-name-specifier.) */
5563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5564 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5565 return error_mark_node;
5566 scope = cp_parser_namespace_name (parser);
5567 }
5568
5569 return scope;
5570 }
5571
5572 /* Parse a postfix-expression.
5573
5574 postfix-expression:
5575 primary-expression
5576 postfix-expression [ expression ]
5577 postfix-expression ( expression-list [opt] )
5578 simple-type-specifier ( expression-list [opt] )
5579 typename :: [opt] nested-name-specifier identifier
5580 ( expression-list [opt] )
5581 typename :: [opt] nested-name-specifier template [opt] template-id
5582 ( expression-list [opt] )
5583 postfix-expression . template [opt] id-expression
5584 postfix-expression -> template [opt] id-expression
5585 postfix-expression . pseudo-destructor-name
5586 postfix-expression -> pseudo-destructor-name
5587 postfix-expression ++
5588 postfix-expression --
5589 dynamic_cast < type-id > ( expression )
5590 static_cast < type-id > ( expression )
5591 reinterpret_cast < type-id > ( expression )
5592 const_cast < type-id > ( expression )
5593 typeid ( expression )
5594 typeid ( type-id )
5595
5596 GNU Extension:
5597
5598 postfix-expression:
5599 ( type-id ) { initializer-list , [opt] }
5600
5601 This extension is a GNU version of the C99 compound-literal
5602 construct. (The C99 grammar uses `type-name' instead of `type-id',
5603 but they are essentially the same concept.)
5604
5605 If ADDRESS_P is true, the postfix expression is the operand of the
5606 `&' operator. CAST_P is true if this expression is the target of a
5607 cast.
5608
5609 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5610 class member access expressions [expr.ref].
5611
5612 Returns a representation of the expression. */
5613
5614 static tree
5615 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5616 bool member_access_only_p, bool decltype_p,
5617 cp_id_kind * pidk_return)
5618 {
5619 cp_token *token;
5620 location_t loc;
5621 enum rid keyword;
5622 cp_id_kind idk = CP_ID_KIND_NONE;
5623 tree postfix_expression = NULL_TREE;
5624 bool is_member_access = false;
5625
5626 /* Peek at the next token. */
5627 token = cp_lexer_peek_token (parser->lexer);
5628 loc = token->location;
5629 /* Some of the productions are determined by keywords. */
5630 keyword = token->keyword;
5631 switch (keyword)
5632 {
5633 case RID_DYNCAST:
5634 case RID_STATCAST:
5635 case RID_REINTCAST:
5636 case RID_CONSTCAST:
5637 {
5638 tree type;
5639 tree expression;
5640 const char *saved_message;
5641 bool saved_in_type_id_in_expr_p;
5642
5643 /* All of these can be handled in the same way from the point
5644 of view of parsing. Begin by consuming the token
5645 identifying the cast. */
5646 cp_lexer_consume_token (parser->lexer);
5647
5648 /* New types cannot be defined in the cast. */
5649 saved_message = parser->type_definition_forbidden_message;
5650 parser->type_definition_forbidden_message
5651 = G_("types may not be defined in casts");
5652
5653 /* Look for the opening `<'. */
5654 cp_parser_require (parser, CPP_LESS, RT_LESS);
5655 /* Parse the type to which we are casting. */
5656 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5657 parser->in_type_id_in_expr_p = true;
5658 type = cp_parser_type_id (parser);
5659 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5660 /* Look for the closing `>'. */
5661 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5662 /* Restore the old message. */
5663 parser->type_definition_forbidden_message = saved_message;
5664
5665 bool saved_greater_than_is_operator_p
5666 = parser->greater_than_is_operator_p;
5667 parser->greater_than_is_operator_p = true;
5668
5669 /* And the expression which is being cast. */
5670 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5671 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5672 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5673
5674 parser->greater_than_is_operator_p
5675 = saved_greater_than_is_operator_p;
5676
5677 /* Only type conversions to integral or enumeration types
5678 can be used in constant-expressions. */
5679 if (!cast_valid_in_integral_constant_expression_p (type)
5680 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5681 return error_mark_node;
5682
5683 switch (keyword)
5684 {
5685 case RID_DYNCAST:
5686 postfix_expression
5687 = build_dynamic_cast (type, expression, tf_warning_or_error);
5688 break;
5689 case RID_STATCAST:
5690 postfix_expression
5691 = build_static_cast (type, expression, tf_warning_or_error);
5692 break;
5693 case RID_REINTCAST:
5694 postfix_expression
5695 = build_reinterpret_cast (type, expression,
5696 tf_warning_or_error);
5697 break;
5698 case RID_CONSTCAST:
5699 postfix_expression
5700 = build_const_cast (type, expression, tf_warning_or_error);
5701 break;
5702 default:
5703 gcc_unreachable ();
5704 }
5705 }
5706 break;
5707
5708 case RID_TYPEID:
5709 {
5710 tree type;
5711 const char *saved_message;
5712 bool saved_in_type_id_in_expr_p;
5713
5714 /* Consume the `typeid' token. */
5715 cp_lexer_consume_token (parser->lexer);
5716 /* Look for the `(' token. */
5717 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5718 /* Types cannot be defined in a `typeid' expression. */
5719 saved_message = parser->type_definition_forbidden_message;
5720 parser->type_definition_forbidden_message
5721 = G_("types may not be defined in a %<typeid%> expression");
5722 /* We can't be sure yet whether we're looking at a type-id or an
5723 expression. */
5724 cp_parser_parse_tentatively (parser);
5725 /* Try a type-id first. */
5726 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5727 parser->in_type_id_in_expr_p = true;
5728 type = cp_parser_type_id (parser);
5729 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5730 /* Look for the `)' token. Otherwise, we can't be sure that
5731 we're not looking at an expression: consider `typeid (int
5732 (3))', for example. */
5733 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5734 /* If all went well, simply lookup the type-id. */
5735 if (cp_parser_parse_definitely (parser))
5736 postfix_expression = get_typeid (type, tf_warning_or_error);
5737 /* Otherwise, fall back to the expression variant. */
5738 else
5739 {
5740 tree expression;
5741
5742 /* Look for an expression. */
5743 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5744 /* Compute its typeid. */
5745 postfix_expression = build_typeid (expression, tf_warning_or_error);
5746 /* Look for the `)' token. */
5747 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5748 }
5749 /* Restore the saved message. */
5750 parser->type_definition_forbidden_message = saved_message;
5751 /* `typeid' may not appear in an integral constant expression. */
5752 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5753 return error_mark_node;
5754 }
5755 break;
5756
5757 case RID_TYPENAME:
5758 {
5759 tree type;
5760 /* The syntax permitted here is the same permitted for an
5761 elaborated-type-specifier. */
5762 type = cp_parser_elaborated_type_specifier (parser,
5763 /*is_friend=*/false,
5764 /*is_declaration=*/false);
5765 postfix_expression = cp_parser_functional_cast (parser, type);
5766 }
5767 break;
5768
5769 case RID_BUILTIN_SHUFFLE:
5770 {
5771 vec<tree, va_gc> *vec;
5772 unsigned int i;
5773 tree p;
5774
5775 cp_lexer_consume_token (parser->lexer);
5776 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5777 /*cast_p=*/false, /*allow_expansion_p=*/true,
5778 /*non_constant_p=*/NULL);
5779 if (vec == NULL)
5780 return error_mark_node;
5781
5782 FOR_EACH_VEC_ELT (*vec, i, p)
5783 mark_exp_read (p);
5784
5785 if (vec->length () == 2)
5786 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5787 tf_warning_or_error);
5788 else if (vec->length () == 3)
5789 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5790 tf_warning_or_error);
5791 else
5792 {
5793 error_at (loc, "wrong number of arguments to "
5794 "%<__builtin_shuffle%>");
5795 return error_mark_node;
5796 }
5797 break;
5798 }
5799
5800 default:
5801 {
5802 tree type;
5803
5804 /* If the next thing is a simple-type-specifier, we may be
5805 looking at a functional cast. We could also be looking at
5806 an id-expression. So, we try the functional cast, and if
5807 that doesn't work we fall back to the primary-expression. */
5808 cp_parser_parse_tentatively (parser);
5809 /* Look for the simple-type-specifier. */
5810 type = cp_parser_simple_type_specifier (parser,
5811 /*decl_specs=*/NULL,
5812 CP_PARSER_FLAGS_NONE);
5813 /* Parse the cast itself. */
5814 if (!cp_parser_error_occurred (parser))
5815 postfix_expression
5816 = cp_parser_functional_cast (parser, type);
5817 /* If that worked, we're done. */
5818 if (cp_parser_parse_definitely (parser))
5819 break;
5820
5821 /* If the functional-cast didn't work out, try a
5822 compound-literal. */
5823 if (cp_parser_allow_gnu_extensions_p (parser)
5824 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5825 {
5826 tree initializer = NULL_TREE;
5827 bool compound_literal_p;
5828
5829 cp_parser_parse_tentatively (parser);
5830 /* Consume the `('. */
5831 cp_lexer_consume_token (parser->lexer);
5832
5833 /* Avoid calling cp_parser_type_id pointlessly, see comment
5834 in cp_parser_cast_expression about c++/29234. */
5835 cp_lexer_save_tokens (parser->lexer);
5836
5837 compound_literal_p
5838 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5839 /*consume_paren=*/true)
5840 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5841
5842 /* Roll back the tokens we skipped. */
5843 cp_lexer_rollback_tokens (parser->lexer);
5844
5845 if (!compound_literal_p)
5846 cp_parser_simulate_error (parser);
5847 else
5848 {
5849 /* Parse the type. */
5850 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5851 parser->in_type_id_in_expr_p = true;
5852 type = cp_parser_type_id (parser);
5853 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5854 /* Look for the `)'. */
5855 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5856 }
5857
5858 /* If things aren't going well, there's no need to
5859 keep going. */
5860 if (!cp_parser_error_occurred (parser))
5861 {
5862 bool non_constant_p;
5863 /* Parse the brace-enclosed initializer list. */
5864 initializer = cp_parser_braced_list (parser,
5865 &non_constant_p);
5866 }
5867 /* If that worked, we're definitely looking at a
5868 compound-literal expression. */
5869 if (cp_parser_parse_definitely (parser))
5870 {
5871 /* Warn the user that a compound literal is not
5872 allowed in standard C++. */
5873 pedwarn (input_location, OPT_Wpedantic,
5874 "ISO C++ forbids compound-literals");
5875 /* For simplicity, we disallow compound literals in
5876 constant-expressions. We could
5877 allow compound literals of integer type, whose
5878 initializer was a constant, in constant
5879 expressions. Permitting that usage, as a further
5880 extension, would not change the meaning of any
5881 currently accepted programs. (Of course, as
5882 compound literals are not part of ISO C++, the
5883 standard has nothing to say.) */
5884 if (cp_parser_non_integral_constant_expression (parser,
5885 NIC_NCC))
5886 {
5887 postfix_expression = error_mark_node;
5888 break;
5889 }
5890 /* Form the representation of the compound-literal. */
5891 postfix_expression
5892 = finish_compound_literal (type, initializer,
5893 tf_warning_or_error);
5894 break;
5895 }
5896 }
5897
5898 /* It must be a primary-expression. */
5899 postfix_expression
5900 = cp_parser_primary_expression (parser, address_p, cast_p,
5901 /*template_arg_p=*/false,
5902 decltype_p,
5903 &idk);
5904 }
5905 break;
5906 }
5907
5908 /* Note that we don't need to worry about calling build_cplus_new on a
5909 class-valued CALL_EXPR in decltype when it isn't the end of the
5910 postfix-expression; unary_complex_lvalue will take care of that for
5911 all these cases. */
5912
5913 /* Keep looping until the postfix-expression is complete. */
5914 while (true)
5915 {
5916 if (idk == CP_ID_KIND_UNQUALIFIED
5917 && identifier_p (postfix_expression)
5918 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5919 /* It is not a Koenig lookup function call. */
5920 postfix_expression
5921 = unqualified_name_lookup_error (postfix_expression);
5922
5923 /* Peek at the next token. */
5924 token = cp_lexer_peek_token (parser->lexer);
5925
5926 switch (token->type)
5927 {
5928 case CPP_OPEN_SQUARE:
5929 if (cp_next_tokens_can_be_std_attribute_p (parser))
5930 {
5931 cp_parser_error (parser,
5932 "two consecutive %<[%> shall "
5933 "only introduce an attribute");
5934 return error_mark_node;
5935 }
5936 postfix_expression
5937 = cp_parser_postfix_open_square_expression (parser,
5938 postfix_expression,
5939 false,
5940 decltype_p);
5941 idk = CP_ID_KIND_NONE;
5942 is_member_access = false;
5943 break;
5944
5945 case CPP_OPEN_PAREN:
5946 /* postfix-expression ( expression-list [opt] ) */
5947 {
5948 bool koenig_p;
5949 bool is_builtin_constant_p;
5950 bool saved_integral_constant_expression_p = false;
5951 bool saved_non_integral_constant_expression_p = false;
5952 tsubst_flags_t complain = complain_flags (decltype_p);
5953 vec<tree, va_gc> *args;
5954
5955 is_member_access = false;
5956
5957 is_builtin_constant_p
5958 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5959 if (is_builtin_constant_p)
5960 {
5961 /* The whole point of __builtin_constant_p is to allow
5962 non-constant expressions to appear as arguments. */
5963 saved_integral_constant_expression_p
5964 = parser->integral_constant_expression_p;
5965 saved_non_integral_constant_expression_p
5966 = parser->non_integral_constant_expression_p;
5967 parser->integral_constant_expression_p = false;
5968 }
5969 args = (cp_parser_parenthesized_expression_list
5970 (parser, non_attr,
5971 /*cast_p=*/false, /*allow_expansion_p=*/true,
5972 /*non_constant_p=*/NULL));
5973 if (is_builtin_constant_p)
5974 {
5975 parser->integral_constant_expression_p
5976 = saved_integral_constant_expression_p;
5977 parser->non_integral_constant_expression_p
5978 = saved_non_integral_constant_expression_p;
5979 }
5980
5981 if (args == NULL)
5982 {
5983 postfix_expression = error_mark_node;
5984 break;
5985 }
5986
5987 /* Function calls are not permitted in
5988 constant-expressions. */
5989 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5990 && cp_parser_non_integral_constant_expression (parser,
5991 NIC_FUNC_CALL))
5992 {
5993 postfix_expression = error_mark_node;
5994 release_tree_vector (args);
5995 break;
5996 }
5997
5998 koenig_p = false;
5999 if (idk == CP_ID_KIND_UNQUALIFIED
6000 || idk == CP_ID_KIND_TEMPLATE_ID)
6001 {
6002 if (identifier_p (postfix_expression))
6003 {
6004 if (!args->is_empty ())
6005 {
6006 koenig_p = true;
6007 if (!any_type_dependent_arguments_p (args))
6008 postfix_expression
6009 = perform_koenig_lookup (postfix_expression, args,
6010 /*include_std=*/false,
6011 complain);
6012 }
6013 else
6014 postfix_expression
6015 = unqualified_fn_lookup_error (postfix_expression);
6016 }
6017 /* We do not perform argument-dependent lookup if
6018 normal lookup finds a non-function, in accordance
6019 with the expected resolution of DR 218. */
6020 else if (!args->is_empty ()
6021 && is_overloaded_fn (postfix_expression))
6022 {
6023 tree fn = get_first_fn (postfix_expression);
6024 fn = STRIP_TEMPLATE (fn);
6025
6026 /* Do not do argument dependent lookup if regular
6027 lookup finds a member function or a block-scope
6028 function declaration. [basic.lookup.argdep]/3 */
6029 if (!DECL_FUNCTION_MEMBER_P (fn)
6030 && !DECL_LOCAL_FUNCTION_P (fn))
6031 {
6032 koenig_p = true;
6033 if (!any_type_dependent_arguments_p (args))
6034 postfix_expression
6035 = perform_koenig_lookup (postfix_expression, args,
6036 /*include_std=*/false,
6037 complain);
6038 }
6039 }
6040 }
6041
6042 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6043 {
6044 tree instance = TREE_OPERAND (postfix_expression, 0);
6045 tree fn = TREE_OPERAND (postfix_expression, 1);
6046
6047 if (processing_template_decl
6048 && (type_dependent_expression_p (instance)
6049 || (!BASELINK_P (fn)
6050 && TREE_CODE (fn) != FIELD_DECL)
6051 || type_dependent_expression_p (fn)
6052 || any_type_dependent_arguments_p (args)))
6053 {
6054 postfix_expression
6055 = build_nt_call_vec (postfix_expression, args);
6056 release_tree_vector (args);
6057 break;
6058 }
6059
6060 if (BASELINK_P (fn))
6061 {
6062 postfix_expression
6063 = (build_new_method_call
6064 (instance, fn, &args, NULL_TREE,
6065 (idk == CP_ID_KIND_QUALIFIED
6066 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6067 : LOOKUP_NORMAL),
6068 /*fn_p=*/NULL,
6069 complain));
6070 }
6071 else
6072 postfix_expression
6073 = finish_call_expr (postfix_expression, &args,
6074 /*disallow_virtual=*/false,
6075 /*koenig_p=*/false,
6076 complain);
6077 }
6078 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6079 || TREE_CODE (postfix_expression) == MEMBER_REF
6080 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6081 postfix_expression = (build_offset_ref_call_from_tree
6082 (postfix_expression, &args,
6083 complain));
6084 else if (idk == CP_ID_KIND_QUALIFIED)
6085 /* A call to a static class member, or a namespace-scope
6086 function. */
6087 postfix_expression
6088 = finish_call_expr (postfix_expression, &args,
6089 /*disallow_virtual=*/true,
6090 koenig_p,
6091 complain);
6092 else
6093 /* All other function calls. */
6094 postfix_expression
6095 = finish_call_expr (postfix_expression, &args,
6096 /*disallow_virtual=*/false,
6097 koenig_p,
6098 complain);
6099
6100 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6101 idk = CP_ID_KIND_NONE;
6102
6103 release_tree_vector (args);
6104 }
6105 break;
6106
6107 case CPP_DOT:
6108 case CPP_DEREF:
6109 /* postfix-expression . template [opt] id-expression
6110 postfix-expression . pseudo-destructor-name
6111 postfix-expression -> template [opt] id-expression
6112 postfix-expression -> pseudo-destructor-name */
6113
6114 /* Consume the `.' or `->' operator. */
6115 cp_lexer_consume_token (parser->lexer);
6116
6117 postfix_expression
6118 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6119 postfix_expression,
6120 false, &idk, loc);
6121
6122 is_member_access = true;
6123 break;
6124
6125 case CPP_PLUS_PLUS:
6126 /* postfix-expression ++ */
6127 /* Consume the `++' token. */
6128 cp_lexer_consume_token (parser->lexer);
6129 /* Generate a representation for the complete expression. */
6130 postfix_expression
6131 = finish_increment_expr (postfix_expression,
6132 POSTINCREMENT_EXPR);
6133 /* Increments may not appear in constant-expressions. */
6134 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6135 postfix_expression = error_mark_node;
6136 idk = CP_ID_KIND_NONE;
6137 is_member_access = false;
6138 break;
6139
6140 case CPP_MINUS_MINUS:
6141 /* postfix-expression -- */
6142 /* Consume the `--' token. */
6143 cp_lexer_consume_token (parser->lexer);
6144 /* Generate a representation for the complete expression. */
6145 postfix_expression
6146 = finish_increment_expr (postfix_expression,
6147 POSTDECREMENT_EXPR);
6148 /* Decrements may not appear in constant-expressions. */
6149 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6150 postfix_expression = error_mark_node;
6151 idk = CP_ID_KIND_NONE;
6152 is_member_access = false;
6153 break;
6154
6155 default:
6156 if (pidk_return != NULL)
6157 * pidk_return = idk;
6158 if (member_access_only_p)
6159 return is_member_access? postfix_expression : error_mark_node;
6160 else
6161 return postfix_expression;
6162 }
6163 }
6164
6165 /* We should never get here. */
6166 gcc_unreachable ();
6167 return error_mark_node;
6168 }
6169
6170 /* This function parses Cilk Plus array notations. If a normal array expr. is
6171 parsed then the array index is passed back to the caller through *INIT_INDEX
6172 and the function returns a NULL_TREE. If array notation expr. is parsed,
6173 then *INIT_INDEX is ignored by the caller and the function returns
6174 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6175 error_mark_node. */
6176
6177 static tree
6178 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6179 tree array_value)
6180 {
6181 cp_token *token = NULL;
6182 tree length_index, stride = NULL_TREE, value_tree, array_type;
6183 if (!array_value || array_value == error_mark_node)
6184 {
6185 cp_parser_skip_to_end_of_statement (parser);
6186 return error_mark_node;
6187 }
6188
6189 array_type = TREE_TYPE (array_value);
6190
6191 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6192 parser->colon_corrects_to_scope_p = false;
6193 token = cp_lexer_peek_token (parser->lexer);
6194
6195 if (!token)
6196 {
6197 cp_parser_error (parser, "expected %<:%> or numeral");
6198 return error_mark_node;
6199 }
6200 else if (token->type == CPP_COLON)
6201 {
6202 /* Consume the ':'. */
6203 cp_lexer_consume_token (parser->lexer);
6204
6205 /* If we are here, then we have a case like this A[:]. */
6206 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6207 {
6208 cp_parser_error (parser, "expected %<]%>");
6209 cp_parser_skip_to_end_of_statement (parser);
6210 return error_mark_node;
6211 }
6212 *init_index = NULL_TREE;
6213 stride = NULL_TREE;
6214 length_index = NULL_TREE;
6215 }
6216 else
6217 {
6218 /* If we are here, then there are three valid possibilities:
6219 1. ARRAY [ EXP ]
6220 2. ARRAY [ EXP : EXP ]
6221 3. ARRAY [ EXP : EXP : EXP ] */
6222
6223 *init_index = cp_parser_expression (parser, false, NULL);
6224 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6225 {
6226 /* This indicates that we have a normal array expression. */
6227 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6228 return NULL_TREE;
6229 }
6230
6231 /* Consume the ':'. */
6232 cp_lexer_consume_token (parser->lexer);
6233 length_index = cp_parser_expression (parser, false, NULL);
6234 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6235 {
6236 cp_lexer_consume_token (parser->lexer);
6237 stride = cp_parser_expression (parser, false, NULL);
6238 }
6239 }
6240 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6241
6242 if (*init_index == error_mark_node || length_index == error_mark_node
6243 || stride == error_mark_node)
6244 {
6245 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6246 cp_lexer_consume_token (parser->lexer);
6247 return error_mark_node;
6248 }
6249 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6250
6251 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6252 length_index, stride, array_type);
6253 return value_tree;
6254 }
6255
6256 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6257 by cp_parser_builtin_offsetof. We're looking for
6258
6259 postfix-expression [ expression ]
6260 postfix-expression [ braced-init-list ] (C++11)
6261
6262 FOR_OFFSETOF is set if we're being called in that context, which
6263 changes how we deal with integer constant expressions. */
6264
6265 static tree
6266 cp_parser_postfix_open_square_expression (cp_parser *parser,
6267 tree postfix_expression,
6268 bool for_offsetof,
6269 bool decltype_p)
6270 {
6271 tree index = NULL_TREE;
6272 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6273 bool saved_greater_than_is_operator_p;
6274
6275 /* Consume the `[' token. */
6276 cp_lexer_consume_token (parser->lexer);
6277
6278 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6279 parser->greater_than_is_operator_p = true;
6280
6281 /* Parse the index expression. */
6282 /* ??? For offsetof, there is a question of what to allow here. If
6283 offsetof is not being used in an integral constant expression context,
6284 then we *could* get the right answer by computing the value at runtime.
6285 If we are in an integral constant expression context, then we might
6286 could accept any constant expression; hard to say without analysis.
6287 Rather than open the barn door too wide right away, allow only integer
6288 constant expressions here. */
6289 if (for_offsetof)
6290 index = cp_parser_constant_expression (parser, false, NULL);
6291 else
6292 {
6293 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6294 {
6295 bool expr_nonconst_p;
6296 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6297 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6298 if (flag_enable_cilkplus
6299 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6300 {
6301 error_at (cp_lexer_peek_token (parser->lexer)->location,
6302 "braced list index is not allowed with array "
6303 "notation");
6304 cp_parser_skip_to_end_of_statement (parser);
6305 return error_mark_node;
6306 }
6307 }
6308 else if (flag_enable_cilkplus)
6309 {
6310 /* Here are have these two options:
6311 ARRAY[EXP : EXP] - Array notation expr with default
6312 stride of 1.
6313 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6314 stride. */
6315 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6316 postfix_expression);
6317 if (an_exp)
6318 return an_exp;
6319 }
6320 else
6321 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6322 }
6323
6324 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6325
6326 /* Look for the closing `]'. */
6327 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6328
6329 /* Build the ARRAY_REF. */
6330 postfix_expression = grok_array_decl (loc, postfix_expression,
6331 index, decltype_p);
6332
6333 /* When not doing offsetof, array references are not permitted in
6334 constant-expressions. */
6335 if (!for_offsetof
6336 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6337 postfix_expression = error_mark_node;
6338
6339 return postfix_expression;
6340 }
6341
6342 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6343 by cp_parser_builtin_offsetof. We're looking for
6344
6345 postfix-expression . template [opt] id-expression
6346 postfix-expression . pseudo-destructor-name
6347 postfix-expression -> template [opt] id-expression
6348 postfix-expression -> pseudo-destructor-name
6349
6350 FOR_OFFSETOF is set if we're being called in that context. That sorta
6351 limits what of the above we'll actually accept, but nevermind.
6352 TOKEN_TYPE is the "." or "->" token, which will already have been
6353 removed from the stream. */
6354
6355 static tree
6356 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6357 enum cpp_ttype token_type,
6358 tree postfix_expression,
6359 bool for_offsetof, cp_id_kind *idk,
6360 location_t location)
6361 {
6362 tree name;
6363 bool dependent_p;
6364 bool pseudo_destructor_p;
6365 tree scope = NULL_TREE;
6366
6367 /* If this is a `->' operator, dereference the pointer. */
6368 if (token_type == CPP_DEREF)
6369 postfix_expression = build_x_arrow (location, postfix_expression,
6370 tf_warning_or_error);
6371 /* Check to see whether or not the expression is type-dependent. */
6372 dependent_p = type_dependent_expression_p (postfix_expression);
6373 /* The identifier following the `->' or `.' is not qualified. */
6374 parser->scope = NULL_TREE;
6375 parser->qualifying_scope = NULL_TREE;
6376 parser->object_scope = NULL_TREE;
6377 *idk = CP_ID_KIND_NONE;
6378
6379 /* Enter the scope corresponding to the type of the object
6380 given by the POSTFIX_EXPRESSION. */
6381 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6382 {
6383 scope = TREE_TYPE (postfix_expression);
6384 /* According to the standard, no expression should ever have
6385 reference type. Unfortunately, we do not currently match
6386 the standard in this respect in that our internal representation
6387 of an expression may have reference type even when the standard
6388 says it does not. Therefore, we have to manually obtain the
6389 underlying type here. */
6390 scope = non_reference (scope);
6391 /* The type of the POSTFIX_EXPRESSION must be complete. */
6392 if (scope == unknown_type_node)
6393 {
6394 error_at (location, "%qE does not have class type",
6395 postfix_expression);
6396 scope = NULL_TREE;
6397 }
6398 /* Unlike the object expression in other contexts, *this is not
6399 required to be of complete type for purposes of class member
6400 access (5.2.5) outside the member function body. */
6401 else if (postfix_expression != current_class_ref
6402 && !(processing_template_decl && scope == current_class_type))
6403 scope = complete_type_or_else (scope, NULL_TREE);
6404 /* Let the name lookup machinery know that we are processing a
6405 class member access expression. */
6406 parser->context->object_type = scope;
6407 /* If something went wrong, we want to be able to discern that case,
6408 as opposed to the case where there was no SCOPE due to the type
6409 of expression being dependent. */
6410 if (!scope)
6411 scope = error_mark_node;
6412 /* If the SCOPE was erroneous, make the various semantic analysis
6413 functions exit quickly -- and without issuing additional error
6414 messages. */
6415 if (scope == error_mark_node)
6416 postfix_expression = error_mark_node;
6417 }
6418
6419 /* Assume this expression is not a pseudo-destructor access. */
6420 pseudo_destructor_p = false;
6421
6422 /* If the SCOPE is a scalar type, then, if this is a valid program,
6423 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6424 is type dependent, it can be pseudo-destructor-name or something else.
6425 Try to parse it as pseudo-destructor-name first. */
6426 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6427 {
6428 tree s;
6429 tree type;
6430
6431 cp_parser_parse_tentatively (parser);
6432 /* Parse the pseudo-destructor-name. */
6433 s = NULL_TREE;
6434 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6435 &s, &type);
6436 if (dependent_p
6437 && (cp_parser_error_occurred (parser)
6438 || !SCALAR_TYPE_P (type)))
6439 cp_parser_abort_tentative_parse (parser);
6440 else if (cp_parser_parse_definitely (parser))
6441 {
6442 pseudo_destructor_p = true;
6443 postfix_expression
6444 = finish_pseudo_destructor_expr (postfix_expression,
6445 s, type, location);
6446 }
6447 }
6448
6449 if (!pseudo_destructor_p)
6450 {
6451 /* If the SCOPE is not a scalar type, we are looking at an
6452 ordinary class member access expression, rather than a
6453 pseudo-destructor-name. */
6454 bool template_p;
6455 cp_token *token = cp_lexer_peek_token (parser->lexer);
6456 /* Parse the id-expression. */
6457 name = (cp_parser_id_expression
6458 (parser,
6459 cp_parser_optional_template_keyword (parser),
6460 /*check_dependency_p=*/true,
6461 &template_p,
6462 /*declarator_p=*/false,
6463 /*optional_p=*/false));
6464 /* In general, build a SCOPE_REF if the member name is qualified.
6465 However, if the name was not dependent and has already been
6466 resolved; there is no need to build the SCOPE_REF. For example;
6467
6468 struct X { void f(); };
6469 template <typename T> void f(T* t) { t->X::f(); }
6470
6471 Even though "t" is dependent, "X::f" is not and has been resolved
6472 to a BASELINK; there is no need to include scope information. */
6473
6474 /* But we do need to remember that there was an explicit scope for
6475 virtual function calls. */
6476 if (parser->scope)
6477 *idk = CP_ID_KIND_QUALIFIED;
6478
6479 /* If the name is a template-id that names a type, we will get a
6480 TYPE_DECL here. That is invalid code. */
6481 if (TREE_CODE (name) == TYPE_DECL)
6482 {
6483 error_at (token->location, "invalid use of %qD", name);
6484 postfix_expression = error_mark_node;
6485 }
6486 else
6487 {
6488 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6489 {
6490 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6491 {
6492 error_at (token->location, "%<%D::%D%> is not a class member",
6493 parser->scope, name);
6494 postfix_expression = error_mark_node;
6495 }
6496 else
6497 name = build_qualified_name (/*type=*/NULL_TREE,
6498 parser->scope,
6499 name,
6500 template_p);
6501 parser->scope = NULL_TREE;
6502 parser->qualifying_scope = NULL_TREE;
6503 parser->object_scope = NULL_TREE;
6504 }
6505 if (parser->scope && name && BASELINK_P (name))
6506 adjust_result_of_qualified_name_lookup
6507 (name, parser->scope, scope);
6508 postfix_expression
6509 = finish_class_member_access_expr (postfix_expression, name,
6510 template_p,
6511 tf_warning_or_error);
6512 }
6513 }
6514
6515 /* We no longer need to look up names in the scope of the object on
6516 the left-hand side of the `.' or `->' operator. */
6517 parser->context->object_type = NULL_TREE;
6518
6519 /* Outside of offsetof, these operators may not appear in
6520 constant-expressions. */
6521 if (!for_offsetof
6522 && (cp_parser_non_integral_constant_expression
6523 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6524 postfix_expression = error_mark_node;
6525
6526 return postfix_expression;
6527 }
6528
6529 /* Parse a parenthesized expression-list.
6530
6531 expression-list:
6532 assignment-expression
6533 expression-list, assignment-expression
6534
6535 attribute-list:
6536 expression-list
6537 identifier
6538 identifier, expression-list
6539
6540 CAST_P is true if this expression is the target of a cast.
6541
6542 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6543 argument pack.
6544
6545 Returns a vector of trees. Each element is a representation of an
6546 assignment-expression. NULL is returned if the ( and or ) are
6547 missing. An empty, but allocated, vector is returned on no
6548 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6549 if we are parsing an attribute list for an attribute that wants a
6550 plain identifier argument, normal_attr for an attribute that wants
6551 an expression, or non_attr if we aren't parsing an attribute list. If
6552 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6553 not all of the expressions in the list were constant. */
6554
6555 static vec<tree, va_gc> *
6556 cp_parser_parenthesized_expression_list (cp_parser* parser,
6557 int is_attribute_list,
6558 bool cast_p,
6559 bool allow_expansion_p,
6560 bool *non_constant_p)
6561 {
6562 vec<tree, va_gc> *expression_list;
6563 bool fold_expr_p = is_attribute_list != non_attr;
6564 tree identifier = NULL_TREE;
6565 bool saved_greater_than_is_operator_p;
6566
6567 /* Assume all the expressions will be constant. */
6568 if (non_constant_p)
6569 *non_constant_p = false;
6570
6571 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6572 return NULL;
6573
6574 expression_list = make_tree_vector ();
6575
6576 /* Within a parenthesized expression, a `>' token is always
6577 the greater-than operator. */
6578 saved_greater_than_is_operator_p
6579 = parser->greater_than_is_operator_p;
6580 parser->greater_than_is_operator_p = true;
6581
6582 /* Consume expressions until there are no more. */
6583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6584 while (true)
6585 {
6586 tree expr;
6587
6588 /* At the beginning of attribute lists, check to see if the
6589 next token is an identifier. */
6590 if (is_attribute_list == id_attr
6591 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6592 {
6593 cp_token *token;
6594
6595 /* Consume the identifier. */
6596 token = cp_lexer_consume_token (parser->lexer);
6597 /* Save the identifier. */
6598 identifier = token->u.value;
6599 }
6600 else
6601 {
6602 bool expr_non_constant_p;
6603
6604 /* Parse the next assignment-expression. */
6605 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6606 {
6607 /* A braced-init-list. */
6608 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6609 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6610 if (non_constant_p && expr_non_constant_p)
6611 *non_constant_p = true;
6612 }
6613 else if (non_constant_p)
6614 {
6615 expr = (cp_parser_constant_expression
6616 (parser, /*allow_non_constant_p=*/true,
6617 &expr_non_constant_p));
6618 if (expr_non_constant_p)
6619 *non_constant_p = true;
6620 }
6621 else
6622 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6623
6624 if (fold_expr_p)
6625 expr = fold_non_dependent_expr (expr);
6626
6627 /* If we have an ellipsis, then this is an expression
6628 expansion. */
6629 if (allow_expansion_p
6630 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6631 {
6632 /* Consume the `...'. */
6633 cp_lexer_consume_token (parser->lexer);
6634
6635 /* Build the argument pack. */
6636 expr = make_pack_expansion (expr);
6637 }
6638
6639 /* Add it to the list. We add error_mark_node
6640 expressions to the list, so that we can still tell if
6641 the correct form for a parenthesized expression-list
6642 is found. That gives better errors. */
6643 vec_safe_push (expression_list, expr);
6644
6645 if (expr == error_mark_node)
6646 goto skip_comma;
6647 }
6648
6649 /* After the first item, attribute lists look the same as
6650 expression lists. */
6651 is_attribute_list = non_attr;
6652
6653 get_comma:;
6654 /* If the next token isn't a `,', then we are done. */
6655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6656 break;
6657
6658 /* Otherwise, consume the `,' and keep going. */
6659 cp_lexer_consume_token (parser->lexer);
6660 }
6661
6662 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6663 {
6664 int ending;
6665
6666 skip_comma:;
6667 /* We try and resync to an unnested comma, as that will give the
6668 user better diagnostics. */
6669 ending = cp_parser_skip_to_closing_parenthesis (parser,
6670 /*recovering=*/true,
6671 /*or_comma=*/true,
6672 /*consume_paren=*/true);
6673 if (ending < 0)
6674 goto get_comma;
6675 if (!ending)
6676 {
6677 parser->greater_than_is_operator_p
6678 = saved_greater_than_is_operator_p;
6679 return NULL;
6680 }
6681 }
6682
6683 parser->greater_than_is_operator_p
6684 = saved_greater_than_is_operator_p;
6685
6686 if (identifier)
6687 vec_safe_insert (expression_list, 0, identifier);
6688
6689 return expression_list;
6690 }
6691
6692 /* Parse a pseudo-destructor-name.
6693
6694 pseudo-destructor-name:
6695 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6696 :: [opt] nested-name-specifier template template-id :: ~ type-name
6697 :: [opt] nested-name-specifier [opt] ~ type-name
6698
6699 If either of the first two productions is used, sets *SCOPE to the
6700 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6701 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6702 or ERROR_MARK_NODE if the parse fails. */
6703
6704 static void
6705 cp_parser_pseudo_destructor_name (cp_parser* parser,
6706 tree object,
6707 tree* scope,
6708 tree* type)
6709 {
6710 bool nested_name_specifier_p;
6711
6712 /* Handle ~auto. */
6713 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6714 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6715 && !type_dependent_expression_p (object))
6716 {
6717 if (cxx_dialect < cxx1y)
6718 pedwarn (input_location, 0,
6719 "%<~auto%> only available with "
6720 "-std=c++1y or -std=gnu++1y");
6721 cp_lexer_consume_token (parser->lexer);
6722 cp_lexer_consume_token (parser->lexer);
6723 *scope = NULL_TREE;
6724 *type = TREE_TYPE (object);
6725 return;
6726 }
6727
6728 /* Assume that things will not work out. */
6729 *type = error_mark_node;
6730
6731 /* Look for the optional `::' operator. */
6732 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6733 /* Look for the optional nested-name-specifier. */
6734 nested_name_specifier_p
6735 = (cp_parser_nested_name_specifier_opt (parser,
6736 /*typename_keyword_p=*/false,
6737 /*check_dependency_p=*/true,
6738 /*type_p=*/false,
6739 /*is_declaration=*/false)
6740 != NULL_TREE);
6741 /* Now, if we saw a nested-name-specifier, we might be doing the
6742 second production. */
6743 if (nested_name_specifier_p
6744 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6745 {
6746 /* Consume the `template' keyword. */
6747 cp_lexer_consume_token (parser->lexer);
6748 /* Parse the template-id. */
6749 cp_parser_template_id (parser,
6750 /*template_keyword_p=*/true,
6751 /*check_dependency_p=*/false,
6752 class_type,
6753 /*is_declaration=*/true);
6754 /* Look for the `::' token. */
6755 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6756 }
6757 /* If the next token is not a `~', then there might be some
6758 additional qualification. */
6759 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6760 {
6761 /* At this point, we're looking for "type-name :: ~". The type-name
6762 must not be a class-name, since this is a pseudo-destructor. So,
6763 it must be either an enum-name, or a typedef-name -- both of which
6764 are just identifiers. So, we peek ahead to check that the "::"
6765 and "~" tokens are present; if they are not, then we can avoid
6766 calling type_name. */
6767 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6768 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6769 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6770 {
6771 cp_parser_error (parser, "non-scalar type");
6772 return;
6773 }
6774
6775 /* Look for the type-name. */
6776 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6777 if (*scope == error_mark_node)
6778 return;
6779
6780 /* Look for the `::' token. */
6781 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6782 }
6783 else
6784 *scope = NULL_TREE;
6785
6786 /* Look for the `~'. */
6787 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6788
6789 /* Once we see the ~, this has to be a pseudo-destructor. */
6790 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6791 cp_parser_commit_to_topmost_tentative_parse (parser);
6792
6793 /* Look for the type-name again. We are not responsible for
6794 checking that it matches the first type-name. */
6795 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6796 }
6797
6798 /* Parse a unary-expression.
6799
6800 unary-expression:
6801 postfix-expression
6802 ++ cast-expression
6803 -- cast-expression
6804 unary-operator cast-expression
6805 sizeof unary-expression
6806 sizeof ( type-id )
6807 alignof ( type-id ) [C++0x]
6808 new-expression
6809 delete-expression
6810
6811 GNU Extensions:
6812
6813 unary-expression:
6814 __extension__ cast-expression
6815 __alignof__ unary-expression
6816 __alignof__ ( type-id )
6817 alignof unary-expression [C++0x]
6818 __real__ cast-expression
6819 __imag__ cast-expression
6820 && identifier
6821 sizeof ( type-id ) { initializer-list , [opt] }
6822 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6823 __alignof__ ( type-id ) { initializer-list , [opt] }
6824
6825 ADDRESS_P is true iff the unary-expression is appearing as the
6826 operand of the `&' operator. CAST_P is true if this expression is
6827 the target of a cast.
6828
6829 Returns a representation of the expression. */
6830
6831 static tree
6832 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6833 bool decltype_p, cp_id_kind * pidk)
6834 {
6835 cp_token *token;
6836 enum tree_code unary_operator;
6837
6838 /* Peek at the next token. */
6839 token = cp_lexer_peek_token (parser->lexer);
6840 /* Some keywords give away the kind of expression. */
6841 if (token->type == CPP_KEYWORD)
6842 {
6843 enum rid keyword = token->keyword;
6844
6845 switch (keyword)
6846 {
6847 case RID_ALIGNOF:
6848 case RID_SIZEOF:
6849 {
6850 tree operand, ret;
6851 enum tree_code op;
6852 location_t first_loc;
6853
6854 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6855 /* Consume the token. */
6856 cp_lexer_consume_token (parser->lexer);
6857 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6858 /* Parse the operand. */
6859 operand = cp_parser_sizeof_operand (parser, keyword);
6860
6861 if (TYPE_P (operand))
6862 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6863 else
6864 {
6865 /* ISO C++ defines alignof only with types, not with
6866 expressions. So pedwarn if alignof is used with a non-
6867 type expression. However, __alignof__ is ok. */
6868 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6869 pedwarn (token->location, OPT_Wpedantic,
6870 "ISO C++ does not allow %<alignof%> "
6871 "with a non-type");
6872
6873 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6874 }
6875 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6876 SIZEOF_EXPR with the original operand. */
6877 if (op == SIZEOF_EXPR && ret != error_mark_node)
6878 {
6879 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6880 {
6881 if (!processing_template_decl && TYPE_P (operand))
6882 {
6883 ret = build_min (SIZEOF_EXPR, size_type_node,
6884 build1 (NOP_EXPR, operand,
6885 error_mark_node));
6886 SIZEOF_EXPR_TYPE_P (ret) = 1;
6887 }
6888 else
6889 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6890 TREE_SIDE_EFFECTS (ret) = 0;
6891 TREE_READONLY (ret) = 1;
6892 }
6893 SET_EXPR_LOCATION (ret, first_loc);
6894 }
6895 return ret;
6896 }
6897
6898 case RID_NEW:
6899 return cp_parser_new_expression (parser);
6900
6901 case RID_DELETE:
6902 return cp_parser_delete_expression (parser);
6903
6904 case RID_EXTENSION:
6905 {
6906 /* The saved value of the PEDANTIC flag. */
6907 int saved_pedantic;
6908 tree expr;
6909
6910 /* Save away the PEDANTIC flag. */
6911 cp_parser_extension_opt (parser, &saved_pedantic);
6912 /* Parse the cast-expression. */
6913 expr = cp_parser_simple_cast_expression (parser);
6914 /* Restore the PEDANTIC flag. */
6915 pedantic = saved_pedantic;
6916
6917 return expr;
6918 }
6919
6920 case RID_REALPART:
6921 case RID_IMAGPART:
6922 {
6923 tree expression;
6924
6925 /* Consume the `__real__' or `__imag__' token. */
6926 cp_lexer_consume_token (parser->lexer);
6927 /* Parse the cast-expression. */
6928 expression = cp_parser_simple_cast_expression (parser);
6929 /* Create the complete representation. */
6930 return build_x_unary_op (token->location,
6931 (keyword == RID_REALPART
6932 ? REALPART_EXPR : IMAGPART_EXPR),
6933 expression,
6934 tf_warning_or_error);
6935 }
6936 break;
6937
6938 case RID_TRANSACTION_ATOMIC:
6939 case RID_TRANSACTION_RELAXED:
6940 return cp_parser_transaction_expression (parser, keyword);
6941
6942 case RID_NOEXCEPT:
6943 {
6944 tree expr;
6945 const char *saved_message;
6946 bool saved_integral_constant_expression_p;
6947 bool saved_non_integral_constant_expression_p;
6948 bool saved_greater_than_is_operator_p;
6949
6950 cp_lexer_consume_token (parser->lexer);
6951 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6952
6953 saved_message = parser->type_definition_forbidden_message;
6954 parser->type_definition_forbidden_message
6955 = G_("types may not be defined in %<noexcept%> expressions");
6956
6957 saved_integral_constant_expression_p
6958 = parser->integral_constant_expression_p;
6959 saved_non_integral_constant_expression_p
6960 = parser->non_integral_constant_expression_p;
6961 parser->integral_constant_expression_p = false;
6962
6963 saved_greater_than_is_operator_p
6964 = parser->greater_than_is_operator_p;
6965 parser->greater_than_is_operator_p = true;
6966
6967 ++cp_unevaluated_operand;
6968 ++c_inhibit_evaluation_warnings;
6969 expr = cp_parser_expression (parser, false, NULL);
6970 --c_inhibit_evaluation_warnings;
6971 --cp_unevaluated_operand;
6972
6973 parser->greater_than_is_operator_p
6974 = saved_greater_than_is_operator_p;
6975
6976 parser->integral_constant_expression_p
6977 = saved_integral_constant_expression_p;
6978 parser->non_integral_constant_expression_p
6979 = saved_non_integral_constant_expression_p;
6980
6981 parser->type_definition_forbidden_message = saved_message;
6982
6983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6984 return finish_noexcept_expr (expr, tf_warning_or_error);
6985 }
6986
6987 default:
6988 break;
6989 }
6990 }
6991
6992 /* Look for the `:: new' and `:: delete', which also signal the
6993 beginning of a new-expression, or delete-expression,
6994 respectively. If the next token is `::', then it might be one of
6995 these. */
6996 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6997 {
6998 enum rid keyword;
6999
7000 /* See if the token after the `::' is one of the keywords in
7001 which we're interested. */
7002 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7003 /* If it's `new', we have a new-expression. */
7004 if (keyword == RID_NEW)
7005 return cp_parser_new_expression (parser);
7006 /* Similarly, for `delete'. */
7007 else if (keyword == RID_DELETE)
7008 return cp_parser_delete_expression (parser);
7009 }
7010
7011 /* Look for a unary operator. */
7012 unary_operator = cp_parser_unary_operator (token);
7013 /* The `++' and `--' operators can be handled similarly, even though
7014 they are not technically unary-operators in the grammar. */
7015 if (unary_operator == ERROR_MARK)
7016 {
7017 if (token->type == CPP_PLUS_PLUS)
7018 unary_operator = PREINCREMENT_EXPR;
7019 else if (token->type == CPP_MINUS_MINUS)
7020 unary_operator = PREDECREMENT_EXPR;
7021 /* Handle the GNU address-of-label extension. */
7022 else if (cp_parser_allow_gnu_extensions_p (parser)
7023 && token->type == CPP_AND_AND)
7024 {
7025 tree identifier;
7026 tree expression;
7027 location_t loc = token->location;
7028
7029 /* Consume the '&&' token. */
7030 cp_lexer_consume_token (parser->lexer);
7031 /* Look for the identifier. */
7032 identifier = cp_parser_identifier (parser);
7033 /* Create an expression representing the address. */
7034 expression = finish_label_address_expr (identifier, loc);
7035 if (cp_parser_non_integral_constant_expression (parser,
7036 NIC_ADDR_LABEL))
7037 expression = error_mark_node;
7038 return expression;
7039 }
7040 }
7041 if (unary_operator != ERROR_MARK)
7042 {
7043 tree cast_expression;
7044 tree expression = error_mark_node;
7045 non_integral_constant non_constant_p = NIC_NONE;
7046 location_t loc = token->location;
7047 tsubst_flags_t complain = complain_flags (decltype_p);
7048
7049 /* Consume the operator token. */
7050 token = cp_lexer_consume_token (parser->lexer);
7051 /* Parse the cast-expression. */
7052 cast_expression
7053 = cp_parser_cast_expression (parser,
7054 unary_operator == ADDR_EXPR,
7055 /*cast_p=*/false,
7056 /*decltype*/false,
7057 pidk);
7058 /* Now, build an appropriate representation. */
7059 switch (unary_operator)
7060 {
7061 case INDIRECT_REF:
7062 non_constant_p = NIC_STAR;
7063 expression = build_x_indirect_ref (loc, cast_expression,
7064 RO_UNARY_STAR,
7065 complain);
7066 break;
7067
7068 case ADDR_EXPR:
7069 non_constant_p = NIC_ADDR;
7070 /* Fall through. */
7071 case BIT_NOT_EXPR:
7072 expression = build_x_unary_op (loc, unary_operator,
7073 cast_expression,
7074 complain);
7075 break;
7076
7077 case PREINCREMENT_EXPR:
7078 case PREDECREMENT_EXPR:
7079 non_constant_p = unary_operator == PREINCREMENT_EXPR
7080 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7081 /* Fall through. */
7082 case UNARY_PLUS_EXPR:
7083 case NEGATE_EXPR:
7084 case TRUTH_NOT_EXPR:
7085 expression = finish_unary_op_expr (loc, unary_operator,
7086 cast_expression, complain);
7087 break;
7088
7089 default:
7090 gcc_unreachable ();
7091 }
7092
7093 if (non_constant_p != NIC_NONE
7094 && cp_parser_non_integral_constant_expression (parser,
7095 non_constant_p))
7096 expression = error_mark_node;
7097
7098 return expression;
7099 }
7100
7101 return cp_parser_postfix_expression (parser, address_p, cast_p,
7102 /*member_access_only_p=*/false,
7103 decltype_p,
7104 pidk);
7105 }
7106
7107 static inline tree
7108 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7109 cp_id_kind * pidk)
7110 {
7111 return cp_parser_unary_expression (parser, address_p, cast_p,
7112 /*decltype*/false, pidk);
7113 }
7114
7115 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7116 unary-operator, the corresponding tree code is returned. */
7117
7118 static enum tree_code
7119 cp_parser_unary_operator (cp_token* token)
7120 {
7121 switch (token->type)
7122 {
7123 case CPP_MULT:
7124 return INDIRECT_REF;
7125
7126 case CPP_AND:
7127 return ADDR_EXPR;
7128
7129 case CPP_PLUS:
7130 return UNARY_PLUS_EXPR;
7131
7132 case CPP_MINUS:
7133 return NEGATE_EXPR;
7134
7135 case CPP_NOT:
7136 return TRUTH_NOT_EXPR;
7137
7138 case CPP_COMPL:
7139 return BIT_NOT_EXPR;
7140
7141 default:
7142 return ERROR_MARK;
7143 }
7144 }
7145
7146 /* Parse a new-expression.
7147
7148 new-expression:
7149 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7150 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7151
7152 Returns a representation of the expression. */
7153
7154 static tree
7155 cp_parser_new_expression (cp_parser* parser)
7156 {
7157 bool global_scope_p;
7158 vec<tree, va_gc> *placement;
7159 tree type;
7160 vec<tree, va_gc> *initializer;
7161 tree nelts = NULL_TREE;
7162 tree ret;
7163
7164 /* Look for the optional `::' operator. */
7165 global_scope_p
7166 = (cp_parser_global_scope_opt (parser,
7167 /*current_scope_valid_p=*/false)
7168 != NULL_TREE);
7169 /* Look for the `new' operator. */
7170 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7171 /* There's no easy way to tell a new-placement from the
7172 `( type-id )' construct. */
7173 cp_parser_parse_tentatively (parser);
7174 /* Look for a new-placement. */
7175 placement = cp_parser_new_placement (parser);
7176 /* If that didn't work out, there's no new-placement. */
7177 if (!cp_parser_parse_definitely (parser))
7178 {
7179 if (placement != NULL)
7180 release_tree_vector (placement);
7181 placement = NULL;
7182 }
7183
7184 /* If the next token is a `(', then we have a parenthesized
7185 type-id. */
7186 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7187 {
7188 cp_token *token;
7189 const char *saved_message = parser->type_definition_forbidden_message;
7190
7191 /* Consume the `('. */
7192 cp_lexer_consume_token (parser->lexer);
7193
7194 /* Parse the type-id. */
7195 parser->type_definition_forbidden_message
7196 = G_("types may not be defined in a new-expression");
7197 type = cp_parser_type_id (parser);
7198 parser->type_definition_forbidden_message = saved_message;
7199
7200 /* Look for the closing `)'. */
7201 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7202 token = cp_lexer_peek_token (parser->lexer);
7203 /* There should not be a direct-new-declarator in this production,
7204 but GCC used to allowed this, so we check and emit a sensible error
7205 message for this case. */
7206 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7207 {
7208 error_at (token->location,
7209 "array bound forbidden after parenthesized type-id");
7210 inform (token->location,
7211 "try removing the parentheses around the type-id");
7212 cp_parser_direct_new_declarator (parser);
7213 }
7214 }
7215 /* Otherwise, there must be a new-type-id. */
7216 else
7217 type = cp_parser_new_type_id (parser, &nelts);
7218
7219 /* If the next token is a `(' or '{', then we have a new-initializer. */
7220 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7221 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7222 initializer = cp_parser_new_initializer (parser);
7223 else
7224 initializer = NULL;
7225
7226 /* A new-expression may not appear in an integral constant
7227 expression. */
7228 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7229 ret = error_mark_node;
7230 else
7231 {
7232 /* Create a representation of the new-expression. */
7233 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7234 tf_warning_or_error);
7235 }
7236
7237 if (placement != NULL)
7238 release_tree_vector (placement);
7239 if (initializer != NULL)
7240 release_tree_vector (initializer);
7241
7242 return ret;
7243 }
7244
7245 /* Parse a new-placement.
7246
7247 new-placement:
7248 ( expression-list )
7249
7250 Returns the same representation as for an expression-list. */
7251
7252 static vec<tree, va_gc> *
7253 cp_parser_new_placement (cp_parser* parser)
7254 {
7255 vec<tree, va_gc> *expression_list;
7256
7257 /* Parse the expression-list. */
7258 expression_list = (cp_parser_parenthesized_expression_list
7259 (parser, non_attr, /*cast_p=*/false,
7260 /*allow_expansion_p=*/true,
7261 /*non_constant_p=*/NULL));
7262
7263 return expression_list;
7264 }
7265
7266 /* Parse a new-type-id.
7267
7268 new-type-id:
7269 type-specifier-seq new-declarator [opt]
7270
7271 Returns the TYPE allocated. If the new-type-id indicates an array
7272 type, *NELTS is set to the number of elements in the last array
7273 bound; the TYPE will not include the last array bound. */
7274
7275 static tree
7276 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7277 {
7278 cp_decl_specifier_seq type_specifier_seq;
7279 cp_declarator *new_declarator;
7280 cp_declarator *declarator;
7281 cp_declarator *outer_declarator;
7282 const char *saved_message;
7283
7284 /* The type-specifier sequence must not contain type definitions.
7285 (It cannot contain declarations of new types either, but if they
7286 are not definitions we will catch that because they are not
7287 complete.) */
7288 saved_message = parser->type_definition_forbidden_message;
7289 parser->type_definition_forbidden_message
7290 = G_("types may not be defined in a new-type-id");
7291 /* Parse the type-specifier-seq. */
7292 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7293 /*is_trailing_return=*/false,
7294 &type_specifier_seq);
7295 /* Restore the old message. */
7296 parser->type_definition_forbidden_message = saved_message;
7297
7298 if (type_specifier_seq.type == error_mark_node)
7299 return error_mark_node;
7300
7301 /* Parse the new-declarator. */
7302 new_declarator = cp_parser_new_declarator_opt (parser);
7303
7304 /* Determine the number of elements in the last array dimension, if
7305 any. */
7306 *nelts = NULL_TREE;
7307 /* Skip down to the last array dimension. */
7308 declarator = new_declarator;
7309 outer_declarator = NULL;
7310 while (declarator && (declarator->kind == cdk_pointer
7311 || declarator->kind == cdk_ptrmem))
7312 {
7313 outer_declarator = declarator;
7314 declarator = declarator->declarator;
7315 }
7316 while (declarator
7317 && declarator->kind == cdk_array
7318 && declarator->declarator
7319 && declarator->declarator->kind == cdk_array)
7320 {
7321 outer_declarator = declarator;
7322 declarator = declarator->declarator;
7323 }
7324
7325 if (declarator && declarator->kind == cdk_array)
7326 {
7327 *nelts = declarator->u.array.bounds;
7328 if (*nelts == error_mark_node)
7329 *nelts = integer_one_node;
7330
7331 if (outer_declarator)
7332 outer_declarator->declarator = declarator->declarator;
7333 else
7334 new_declarator = NULL;
7335 }
7336
7337 return groktypename (&type_specifier_seq, new_declarator, false);
7338 }
7339
7340 /* Parse an (optional) new-declarator.
7341
7342 new-declarator:
7343 ptr-operator new-declarator [opt]
7344 direct-new-declarator
7345
7346 Returns the declarator. */
7347
7348 static cp_declarator *
7349 cp_parser_new_declarator_opt (cp_parser* parser)
7350 {
7351 enum tree_code code;
7352 tree type, std_attributes = NULL_TREE;
7353 cp_cv_quals cv_quals;
7354
7355 /* We don't know if there's a ptr-operator next, or not. */
7356 cp_parser_parse_tentatively (parser);
7357 /* Look for a ptr-operator. */
7358 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7359 /* If that worked, look for more new-declarators. */
7360 if (cp_parser_parse_definitely (parser))
7361 {
7362 cp_declarator *declarator;
7363
7364 /* Parse another optional declarator. */
7365 declarator = cp_parser_new_declarator_opt (parser);
7366
7367 declarator = cp_parser_make_indirect_declarator
7368 (code, type, cv_quals, declarator, std_attributes);
7369
7370 return declarator;
7371 }
7372
7373 /* If the next token is a `[', there is a direct-new-declarator. */
7374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7375 return cp_parser_direct_new_declarator (parser);
7376
7377 return NULL;
7378 }
7379
7380 /* Parse a direct-new-declarator.
7381
7382 direct-new-declarator:
7383 [ expression ]
7384 direct-new-declarator [constant-expression]
7385
7386 */
7387
7388 static cp_declarator *
7389 cp_parser_direct_new_declarator (cp_parser* parser)
7390 {
7391 cp_declarator *declarator = NULL;
7392
7393 while (true)
7394 {
7395 tree expression;
7396 cp_token *token;
7397
7398 /* Look for the opening `['. */
7399 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7400
7401 token = cp_lexer_peek_token (parser->lexer);
7402 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7403 /* The standard requires that the expression have integral
7404 type. DR 74 adds enumeration types. We believe that the
7405 real intent is that these expressions be handled like the
7406 expression in a `switch' condition, which also allows
7407 classes with a single conversion to integral or
7408 enumeration type. */
7409 if (!processing_template_decl)
7410 {
7411 expression
7412 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7413 expression,
7414 /*complain=*/true);
7415 if (!expression)
7416 {
7417 error_at (token->location,
7418 "expression in new-declarator must have integral "
7419 "or enumeration type");
7420 expression = error_mark_node;
7421 }
7422 }
7423
7424 /* Look for the closing `]'. */
7425 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7426
7427 /* Add this bound to the declarator. */
7428 declarator = make_array_declarator (declarator, expression);
7429
7430 /* If the next token is not a `[', then there are no more
7431 bounds. */
7432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7433 break;
7434 }
7435
7436 return declarator;
7437 }
7438
7439 /* Parse a new-initializer.
7440
7441 new-initializer:
7442 ( expression-list [opt] )
7443 braced-init-list
7444
7445 Returns a representation of the expression-list. */
7446
7447 static vec<tree, va_gc> *
7448 cp_parser_new_initializer (cp_parser* parser)
7449 {
7450 vec<tree, va_gc> *expression_list;
7451
7452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7453 {
7454 tree t;
7455 bool expr_non_constant_p;
7456 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7457 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7458 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7459 expression_list = make_tree_vector_single (t);
7460 }
7461 else
7462 expression_list = (cp_parser_parenthesized_expression_list
7463 (parser, non_attr, /*cast_p=*/false,
7464 /*allow_expansion_p=*/true,
7465 /*non_constant_p=*/NULL));
7466
7467 return expression_list;
7468 }
7469
7470 /* Parse a delete-expression.
7471
7472 delete-expression:
7473 :: [opt] delete cast-expression
7474 :: [opt] delete [ ] cast-expression
7475
7476 Returns a representation of the expression. */
7477
7478 static tree
7479 cp_parser_delete_expression (cp_parser* parser)
7480 {
7481 bool global_scope_p;
7482 bool array_p;
7483 tree expression;
7484
7485 /* Look for the optional `::' operator. */
7486 global_scope_p
7487 = (cp_parser_global_scope_opt (parser,
7488 /*current_scope_valid_p=*/false)
7489 != NULL_TREE);
7490 /* Look for the `delete' keyword. */
7491 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7492 /* See if the array syntax is in use. */
7493 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7494 {
7495 /* Consume the `[' token. */
7496 cp_lexer_consume_token (parser->lexer);
7497 /* Look for the `]' token. */
7498 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7499 /* Remember that this is the `[]' construct. */
7500 array_p = true;
7501 }
7502 else
7503 array_p = false;
7504
7505 /* Parse the cast-expression. */
7506 expression = cp_parser_simple_cast_expression (parser);
7507
7508 /* A delete-expression may not appear in an integral constant
7509 expression. */
7510 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7511 return error_mark_node;
7512
7513 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7514 tf_warning_or_error);
7515 }
7516
7517 /* Returns true if TOKEN may start a cast-expression and false
7518 otherwise. */
7519
7520 static bool
7521 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7522 {
7523 cp_token *token = cp_lexer_peek_token (parser->lexer);
7524 switch (token->type)
7525 {
7526 case CPP_COMMA:
7527 case CPP_SEMICOLON:
7528 case CPP_QUERY:
7529 case CPP_COLON:
7530 case CPP_CLOSE_SQUARE:
7531 case CPP_CLOSE_PAREN:
7532 case CPP_CLOSE_BRACE:
7533 case CPP_OPEN_BRACE:
7534 case CPP_DOT:
7535 case CPP_DOT_STAR:
7536 case CPP_DEREF:
7537 case CPP_DEREF_STAR:
7538 case CPP_DIV:
7539 case CPP_MOD:
7540 case CPP_LSHIFT:
7541 case CPP_RSHIFT:
7542 case CPP_LESS:
7543 case CPP_GREATER:
7544 case CPP_LESS_EQ:
7545 case CPP_GREATER_EQ:
7546 case CPP_EQ_EQ:
7547 case CPP_NOT_EQ:
7548 case CPP_EQ:
7549 case CPP_MULT_EQ:
7550 case CPP_DIV_EQ:
7551 case CPP_MOD_EQ:
7552 case CPP_PLUS_EQ:
7553 case CPP_MINUS_EQ:
7554 case CPP_RSHIFT_EQ:
7555 case CPP_LSHIFT_EQ:
7556 case CPP_AND_EQ:
7557 case CPP_XOR_EQ:
7558 case CPP_OR_EQ:
7559 case CPP_XOR:
7560 case CPP_OR:
7561 case CPP_OR_OR:
7562 case CPP_EOF:
7563 return false;
7564
7565 case CPP_OPEN_PAREN:
7566 /* In ((type ()) () the last () isn't a valid cast-expression,
7567 so the whole must be parsed as postfix-expression. */
7568 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7569 != CPP_CLOSE_PAREN;
7570
7571 /* '[' may start a primary-expression in obj-c++. */
7572 case CPP_OPEN_SQUARE:
7573 return c_dialect_objc ();
7574
7575 default:
7576 return true;
7577 }
7578 }
7579
7580 /* Parse a cast-expression.
7581
7582 cast-expression:
7583 unary-expression
7584 ( type-id ) cast-expression
7585
7586 ADDRESS_P is true iff the unary-expression is appearing as the
7587 operand of the `&' operator. CAST_P is true if this expression is
7588 the target of a cast.
7589
7590 Returns a representation of the expression. */
7591
7592 static tree
7593 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7594 bool decltype_p, cp_id_kind * pidk)
7595 {
7596 /* If it's a `(', then we might be looking at a cast. */
7597 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7598 {
7599 tree type = NULL_TREE;
7600 tree expr = NULL_TREE;
7601 bool cast_expression_p;
7602 const char *saved_message;
7603
7604 /* There's no way to know yet whether or not this is a cast.
7605 For example, `(int (3))' is a unary-expression, while `(int)
7606 3' is a cast. So, we resort to parsing tentatively. */
7607 cp_parser_parse_tentatively (parser);
7608 /* Types may not be defined in a cast. */
7609 saved_message = parser->type_definition_forbidden_message;
7610 parser->type_definition_forbidden_message
7611 = G_("types may not be defined in casts");
7612 /* Consume the `('. */
7613 cp_lexer_consume_token (parser->lexer);
7614 /* A very tricky bit is that `(struct S) { 3 }' is a
7615 compound-literal (which we permit in C++ as an extension).
7616 But, that construct is not a cast-expression -- it is a
7617 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7618 is legal; if the compound-literal were a cast-expression,
7619 you'd need an extra set of parentheses.) But, if we parse
7620 the type-id, and it happens to be a class-specifier, then we
7621 will commit to the parse at that point, because we cannot
7622 undo the action that is done when creating a new class. So,
7623 then we cannot back up and do a postfix-expression.
7624 Another tricky case is the following (c++/29234):
7625
7626 struct S { void operator () (); };
7627
7628 void foo ()
7629 {
7630 ( S()() );
7631 }
7632
7633 As a type-id we parse the parenthesized S()() as a function
7634 returning a function, groktypename complains and we cannot
7635 back up in this case either.
7636
7637 Therefore, we scan ahead to the closing `)', and check to see
7638 if the tokens after the `)' can start a cast-expression. Otherwise
7639 we are dealing with an unary-expression, a postfix-expression
7640 or something else.
7641
7642 Save tokens so that we can put them back. */
7643 cp_lexer_save_tokens (parser->lexer);
7644
7645 /* We may be looking at a cast-expression. */
7646 cast_expression_p
7647 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7648 /*consume_paren=*/true)
7649 && cp_parser_tokens_start_cast_expression (parser));
7650
7651 /* Roll back the tokens we skipped. */
7652 cp_lexer_rollback_tokens (parser->lexer);
7653 /* If we aren't looking at a cast-expression, simulate an error so
7654 that the call to cp_parser_parse_definitely below will fail. */
7655 if (!cast_expression_p)
7656 cp_parser_simulate_error (parser);
7657 else
7658 {
7659 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7660 parser->in_type_id_in_expr_p = true;
7661 /* Look for the type-id. */
7662 type = cp_parser_type_id (parser);
7663 /* Look for the closing `)'. */
7664 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7665 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7666 }
7667
7668 /* Restore the saved message. */
7669 parser->type_definition_forbidden_message = saved_message;
7670
7671 /* At this point this can only be either a cast or a
7672 parenthesized ctor such as `(T ())' that looks like a cast to
7673 function returning T. */
7674 if (!cp_parser_error_occurred (parser))
7675 {
7676 cp_parser_parse_definitely (parser);
7677 expr = cp_parser_cast_expression (parser,
7678 /*address_p=*/false,
7679 /*cast_p=*/true,
7680 /*decltype_p=*/false,
7681 pidk);
7682
7683 /* Warn about old-style casts, if so requested. */
7684 if (warn_old_style_cast
7685 && !in_system_header
7686 && !VOID_TYPE_P (type)
7687 && current_lang_name != lang_name_c)
7688 warning (OPT_Wold_style_cast, "use of old-style cast");
7689
7690 /* Only type conversions to integral or enumeration types
7691 can be used in constant-expressions. */
7692 if (!cast_valid_in_integral_constant_expression_p (type)
7693 && cp_parser_non_integral_constant_expression (parser,
7694 NIC_CAST))
7695 return error_mark_node;
7696
7697 /* Perform the cast. */
7698 expr = build_c_cast (input_location, type, expr);
7699 return expr;
7700 }
7701 else
7702 cp_parser_abort_tentative_parse (parser);
7703 }
7704
7705 /* If we get here, then it's not a cast, so it must be a
7706 unary-expression. */
7707 return cp_parser_unary_expression (parser, address_p, cast_p,
7708 decltype_p, pidk);
7709 }
7710
7711 /* Parse a binary expression of the general form:
7712
7713 pm-expression:
7714 cast-expression
7715 pm-expression .* cast-expression
7716 pm-expression ->* cast-expression
7717
7718 multiplicative-expression:
7719 pm-expression
7720 multiplicative-expression * pm-expression
7721 multiplicative-expression / pm-expression
7722 multiplicative-expression % pm-expression
7723
7724 additive-expression:
7725 multiplicative-expression
7726 additive-expression + multiplicative-expression
7727 additive-expression - multiplicative-expression
7728
7729 shift-expression:
7730 additive-expression
7731 shift-expression << additive-expression
7732 shift-expression >> additive-expression
7733
7734 relational-expression:
7735 shift-expression
7736 relational-expression < shift-expression
7737 relational-expression > shift-expression
7738 relational-expression <= shift-expression
7739 relational-expression >= shift-expression
7740
7741 GNU Extension:
7742
7743 relational-expression:
7744 relational-expression <? shift-expression
7745 relational-expression >? shift-expression
7746
7747 equality-expression:
7748 relational-expression
7749 equality-expression == relational-expression
7750 equality-expression != relational-expression
7751
7752 and-expression:
7753 equality-expression
7754 and-expression & equality-expression
7755
7756 exclusive-or-expression:
7757 and-expression
7758 exclusive-or-expression ^ and-expression
7759
7760 inclusive-or-expression:
7761 exclusive-or-expression
7762 inclusive-or-expression | exclusive-or-expression
7763
7764 logical-and-expression:
7765 inclusive-or-expression
7766 logical-and-expression && inclusive-or-expression
7767
7768 logical-or-expression:
7769 logical-and-expression
7770 logical-or-expression || logical-and-expression
7771
7772 All these are implemented with a single function like:
7773
7774 binary-expression:
7775 simple-cast-expression
7776 binary-expression <token> binary-expression
7777
7778 CAST_P is true if this expression is the target of a cast.
7779
7780 The binops_by_token map is used to get the tree codes for each <token> type.
7781 binary-expressions are associated according to a precedence table. */
7782
7783 #define TOKEN_PRECEDENCE(token) \
7784 (((token->type == CPP_GREATER \
7785 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7786 && !parser->greater_than_is_operator_p) \
7787 ? PREC_NOT_OPERATOR \
7788 : binops_by_token[token->type].prec)
7789
7790 static tree
7791 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7792 bool no_toplevel_fold_p,
7793 bool decltype_p,
7794 enum cp_parser_prec prec,
7795 cp_id_kind * pidk)
7796 {
7797 cp_parser_expression_stack stack;
7798 cp_parser_expression_stack_entry *sp = &stack[0];
7799 cp_parser_expression_stack_entry current;
7800 tree rhs;
7801 cp_token *token;
7802 enum tree_code rhs_type;
7803 enum cp_parser_prec new_prec, lookahead_prec;
7804 tree overload;
7805
7806 /* Parse the first expression. */
7807 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7808 cast_p, decltype_p, pidk);
7809 current.lhs_type = ERROR_MARK;
7810 current.prec = prec;
7811
7812 if (cp_parser_error_occurred (parser))
7813 return error_mark_node;
7814
7815 for (;;)
7816 {
7817 /* Get an operator token. */
7818 token = cp_lexer_peek_token (parser->lexer);
7819
7820 if (warn_cxx0x_compat
7821 && token->type == CPP_RSHIFT
7822 && !parser->greater_than_is_operator_p)
7823 {
7824 if (warning_at (token->location, OPT_Wc__0x_compat,
7825 "%<>>%> operator is treated"
7826 " as two right angle brackets in C++11"))
7827 inform (token->location,
7828 "suggest parentheses around %<>>%> expression");
7829 }
7830
7831 new_prec = TOKEN_PRECEDENCE (token);
7832
7833 /* Popping an entry off the stack means we completed a subexpression:
7834 - either we found a token which is not an operator (`>' where it is not
7835 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7836 will happen repeatedly;
7837 - or, we found an operator which has lower priority. This is the case
7838 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7839 parsing `3 * 4'. */
7840 if (new_prec <= current.prec)
7841 {
7842 if (sp == stack)
7843 break;
7844 else
7845 goto pop;
7846 }
7847
7848 get_rhs:
7849 current.tree_type = binops_by_token[token->type].tree_type;
7850 current.loc = token->location;
7851
7852 /* We used the operator token. */
7853 cp_lexer_consume_token (parser->lexer);
7854
7855 /* For "false && x" or "true || x", x will never be executed;
7856 disable warnings while evaluating it. */
7857 if (current.tree_type == TRUTH_ANDIF_EXPR)
7858 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7859 else if (current.tree_type == TRUTH_ORIF_EXPR)
7860 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7861
7862 /* Extract another operand. It may be the RHS of this expression
7863 or the LHS of a new, higher priority expression. */
7864 rhs = cp_parser_simple_cast_expression (parser);
7865 rhs_type = ERROR_MARK;
7866
7867 /* Get another operator token. Look up its precedence to avoid
7868 building a useless (immediately popped) stack entry for common
7869 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7870 token = cp_lexer_peek_token (parser->lexer);
7871 lookahead_prec = TOKEN_PRECEDENCE (token);
7872 if (lookahead_prec > new_prec)
7873 {
7874 /* ... and prepare to parse the RHS of the new, higher priority
7875 expression. Since precedence levels on the stack are
7876 monotonically increasing, we do not have to care about
7877 stack overflows. */
7878 *sp = current;
7879 ++sp;
7880 current.lhs = rhs;
7881 current.lhs_type = rhs_type;
7882 current.prec = new_prec;
7883 new_prec = lookahead_prec;
7884 goto get_rhs;
7885
7886 pop:
7887 lookahead_prec = new_prec;
7888 /* If the stack is not empty, we have parsed into LHS the right side
7889 (`4' in the example above) of an expression we had suspended.
7890 We can use the information on the stack to recover the LHS (`3')
7891 from the stack together with the tree code (`MULT_EXPR'), and
7892 the precedence of the higher level subexpression
7893 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7894 which will be used to actually build the additive expression. */
7895 rhs = current.lhs;
7896 rhs_type = current.lhs_type;
7897 --sp;
7898 current = *sp;
7899 }
7900
7901 /* Undo the disabling of warnings done above. */
7902 if (current.tree_type == TRUTH_ANDIF_EXPR)
7903 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7904 else if (current.tree_type == TRUTH_ORIF_EXPR)
7905 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7906
7907 overload = NULL;
7908 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7909 ERROR_MARK for everything that is not a binary expression.
7910 This makes warn_about_parentheses miss some warnings that
7911 involve unary operators. For unary expressions we should
7912 pass the correct tree_code unless the unary expression was
7913 surrounded by parentheses.
7914 */
7915 if (no_toplevel_fold_p
7916 && lookahead_prec <= current.prec
7917 && sp == stack)
7918 current.lhs = build2 (current.tree_type,
7919 TREE_CODE_CLASS (current.tree_type)
7920 == tcc_comparison
7921 ? boolean_type_node : TREE_TYPE (current.lhs),
7922 current.lhs, rhs);
7923 else
7924 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7925 current.lhs, current.lhs_type,
7926 rhs, rhs_type, &overload,
7927 complain_flags (decltype_p));
7928 current.lhs_type = current.tree_type;
7929 if (EXPR_P (current.lhs))
7930 SET_EXPR_LOCATION (current.lhs, current.loc);
7931
7932 /* If the binary operator required the use of an overloaded operator,
7933 then this expression cannot be an integral constant-expression.
7934 An overloaded operator can be used even if both operands are
7935 otherwise permissible in an integral constant-expression if at
7936 least one of the operands is of enumeration type. */
7937
7938 if (overload
7939 && cp_parser_non_integral_constant_expression (parser,
7940 NIC_OVERLOADED))
7941 return error_mark_node;
7942 }
7943
7944 return current.lhs;
7945 }
7946
7947 static tree
7948 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7949 bool no_toplevel_fold_p,
7950 enum cp_parser_prec prec,
7951 cp_id_kind * pidk)
7952 {
7953 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7954 /*decltype*/false, prec, pidk);
7955 }
7956
7957 /* Parse the `? expression : assignment-expression' part of a
7958 conditional-expression. The LOGICAL_OR_EXPR is the
7959 logical-or-expression that started the conditional-expression.
7960 Returns a representation of the entire conditional-expression.
7961
7962 This routine is used by cp_parser_assignment_expression.
7963
7964 ? expression : assignment-expression
7965
7966 GNU Extensions:
7967
7968 ? : assignment-expression */
7969
7970 static tree
7971 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7972 {
7973 tree expr;
7974 tree assignment_expr;
7975 struct cp_token *token;
7976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7977
7978 /* Consume the `?' token. */
7979 cp_lexer_consume_token (parser->lexer);
7980 token = cp_lexer_peek_token (parser->lexer);
7981 if (cp_parser_allow_gnu_extensions_p (parser)
7982 && token->type == CPP_COLON)
7983 {
7984 pedwarn (token->location, OPT_Wpedantic,
7985 "ISO C++ does not allow ?: with omitted middle operand");
7986 /* Implicit true clause. */
7987 expr = NULL_TREE;
7988 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7989 warn_for_omitted_condop (token->location, logical_or_expr);
7990 }
7991 else
7992 {
7993 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7994 parser->colon_corrects_to_scope_p = false;
7995 /* Parse the expression. */
7996 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7997 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7998 c_inhibit_evaluation_warnings +=
7999 ((logical_or_expr == truthvalue_true_node)
8000 - (logical_or_expr == truthvalue_false_node));
8001 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8002 }
8003
8004 /* The next token should be a `:'. */
8005 cp_parser_require (parser, CPP_COLON, RT_COLON);
8006 /* Parse the assignment-expression. */
8007 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8008 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8009
8010 /* Build the conditional-expression. */
8011 return build_x_conditional_expr (loc, logical_or_expr,
8012 expr,
8013 assignment_expr,
8014 tf_warning_or_error);
8015 }
8016
8017 /* Parse an assignment-expression.
8018
8019 assignment-expression:
8020 conditional-expression
8021 logical-or-expression assignment-operator assignment_expression
8022 throw-expression
8023
8024 CAST_P is true if this expression is the target of a cast.
8025 DECLTYPE_P is true if this expression is the operand of decltype.
8026
8027 Returns a representation for the expression. */
8028
8029 static tree
8030 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8031 bool decltype_p, cp_id_kind * pidk)
8032 {
8033 tree expr;
8034
8035 /* If the next token is the `throw' keyword, then we're looking at
8036 a throw-expression. */
8037 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8038 expr = cp_parser_throw_expression (parser);
8039 /* Otherwise, it must be that we are looking at a
8040 logical-or-expression. */
8041 else
8042 {
8043 /* Parse the binary expressions (logical-or-expression). */
8044 expr = cp_parser_binary_expression (parser, cast_p, false,
8045 decltype_p,
8046 PREC_NOT_OPERATOR, pidk);
8047 /* If the next token is a `?' then we're actually looking at a
8048 conditional-expression. */
8049 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8050 return cp_parser_question_colon_clause (parser, expr);
8051 else
8052 {
8053 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8054
8055 /* If it's an assignment-operator, we're using the second
8056 production. */
8057 enum tree_code assignment_operator
8058 = cp_parser_assignment_operator_opt (parser);
8059 if (assignment_operator != ERROR_MARK)
8060 {
8061 bool non_constant_p;
8062 location_t saved_input_location;
8063
8064 /* Parse the right-hand side of the assignment. */
8065 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8066
8067 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8068 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8069
8070 /* An assignment may not appear in a
8071 constant-expression. */
8072 if (cp_parser_non_integral_constant_expression (parser,
8073 NIC_ASSIGNMENT))
8074 return error_mark_node;
8075 /* Build the assignment expression. Its default
8076 location is the location of the '=' token. */
8077 saved_input_location = input_location;
8078 input_location = loc;
8079 expr = build_x_modify_expr (loc, expr,
8080 assignment_operator,
8081 rhs,
8082 complain_flags (decltype_p));
8083 input_location = saved_input_location;
8084 }
8085 }
8086 }
8087
8088 return expr;
8089 }
8090
8091 static tree
8092 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8093 cp_id_kind * pidk)
8094 {
8095 return cp_parser_assignment_expression (parser, cast_p,
8096 /*decltype*/false, pidk);
8097 }
8098
8099 /* Parse an (optional) assignment-operator.
8100
8101 assignment-operator: one of
8102 = *= /= %= += -= >>= <<= &= ^= |=
8103
8104 GNU Extension:
8105
8106 assignment-operator: one of
8107 <?= >?=
8108
8109 If the next token is an assignment operator, the corresponding tree
8110 code is returned, and the token is consumed. For example, for
8111 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8112 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8113 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8114 operator, ERROR_MARK is returned. */
8115
8116 static enum tree_code
8117 cp_parser_assignment_operator_opt (cp_parser* parser)
8118 {
8119 enum tree_code op;
8120 cp_token *token;
8121
8122 /* Peek at the next token. */
8123 token = cp_lexer_peek_token (parser->lexer);
8124
8125 switch (token->type)
8126 {
8127 case CPP_EQ:
8128 op = NOP_EXPR;
8129 break;
8130
8131 case CPP_MULT_EQ:
8132 op = MULT_EXPR;
8133 break;
8134
8135 case CPP_DIV_EQ:
8136 op = TRUNC_DIV_EXPR;
8137 break;
8138
8139 case CPP_MOD_EQ:
8140 op = TRUNC_MOD_EXPR;
8141 break;
8142
8143 case CPP_PLUS_EQ:
8144 op = PLUS_EXPR;
8145 break;
8146
8147 case CPP_MINUS_EQ:
8148 op = MINUS_EXPR;
8149 break;
8150
8151 case CPP_RSHIFT_EQ:
8152 op = RSHIFT_EXPR;
8153 break;
8154
8155 case CPP_LSHIFT_EQ:
8156 op = LSHIFT_EXPR;
8157 break;
8158
8159 case CPP_AND_EQ:
8160 op = BIT_AND_EXPR;
8161 break;
8162
8163 case CPP_XOR_EQ:
8164 op = BIT_XOR_EXPR;
8165 break;
8166
8167 case CPP_OR_EQ:
8168 op = BIT_IOR_EXPR;
8169 break;
8170
8171 default:
8172 /* Nothing else is an assignment operator. */
8173 op = ERROR_MARK;
8174 }
8175
8176 /* If it was an assignment operator, consume it. */
8177 if (op != ERROR_MARK)
8178 cp_lexer_consume_token (parser->lexer);
8179
8180 return op;
8181 }
8182
8183 /* Parse an expression.
8184
8185 expression:
8186 assignment-expression
8187 expression , assignment-expression
8188
8189 CAST_P is true if this expression is the target of a cast.
8190 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8191 except possibly parenthesized or on the RHS of a comma (N3276).
8192
8193 Returns a representation of the expression. */
8194
8195 static tree
8196 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8197 cp_id_kind * pidk)
8198 {
8199 tree expression = NULL_TREE;
8200 location_t loc = UNKNOWN_LOCATION;
8201
8202 while (true)
8203 {
8204 tree assignment_expression;
8205
8206 /* Parse the next assignment-expression. */
8207 assignment_expression
8208 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8209
8210 /* We don't create a temporary for a call that is the immediate operand
8211 of decltype or on the RHS of a comma. But when we see a comma, we
8212 need to create a temporary for a call on the LHS. */
8213 if (decltype_p && !processing_template_decl
8214 && TREE_CODE (assignment_expression) == CALL_EXPR
8215 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8216 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8217 assignment_expression
8218 = build_cplus_new (TREE_TYPE (assignment_expression),
8219 assignment_expression, tf_warning_or_error);
8220
8221 /* If this is the first assignment-expression, we can just
8222 save it away. */
8223 if (!expression)
8224 expression = assignment_expression;
8225 else
8226 expression = build_x_compound_expr (loc, expression,
8227 assignment_expression,
8228 complain_flags (decltype_p));
8229 /* If the next token is not a comma, then we are done with the
8230 expression. */
8231 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8232 break;
8233 /* Consume the `,'. */
8234 loc = cp_lexer_peek_token (parser->lexer)->location;
8235 cp_lexer_consume_token (parser->lexer);
8236 /* A comma operator cannot appear in a constant-expression. */
8237 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8238 expression = error_mark_node;
8239 }
8240
8241 return expression;
8242 }
8243
8244 static inline tree
8245 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8246 {
8247 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8248 }
8249
8250 /* Parse a constant-expression.
8251
8252 constant-expression:
8253 conditional-expression
8254
8255 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8256 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8257 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8258 is false, NON_CONSTANT_P should be NULL. */
8259
8260 static tree
8261 cp_parser_constant_expression (cp_parser* parser,
8262 bool allow_non_constant_p,
8263 bool *non_constant_p)
8264 {
8265 bool saved_integral_constant_expression_p;
8266 bool saved_allow_non_integral_constant_expression_p;
8267 bool saved_non_integral_constant_expression_p;
8268 tree expression;
8269
8270 /* It might seem that we could simply parse the
8271 conditional-expression, and then check to see if it were
8272 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8273 one that the compiler can figure out is constant, possibly after
8274 doing some simplifications or optimizations. The standard has a
8275 precise definition of constant-expression, and we must honor
8276 that, even though it is somewhat more restrictive.
8277
8278 For example:
8279
8280 int i[(2, 3)];
8281
8282 is not a legal declaration, because `(2, 3)' is not a
8283 constant-expression. The `,' operator is forbidden in a
8284 constant-expression. However, GCC's constant-folding machinery
8285 will fold this operation to an INTEGER_CST for `3'. */
8286
8287 /* Save the old settings. */
8288 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8289 saved_allow_non_integral_constant_expression_p
8290 = parser->allow_non_integral_constant_expression_p;
8291 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8292 /* We are now parsing a constant-expression. */
8293 parser->integral_constant_expression_p = true;
8294 parser->allow_non_integral_constant_expression_p
8295 = (allow_non_constant_p || cxx_dialect >= cxx11);
8296 parser->non_integral_constant_expression_p = false;
8297 /* Although the grammar says "conditional-expression", we parse an
8298 "assignment-expression", which also permits "throw-expression"
8299 and the use of assignment operators. In the case that
8300 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8301 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8302 actually essential that we look for an assignment-expression.
8303 For example, cp_parser_initializer_clauses uses this function to
8304 determine whether a particular assignment-expression is in fact
8305 constant. */
8306 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8307 /* Restore the old settings. */
8308 parser->integral_constant_expression_p
8309 = saved_integral_constant_expression_p;
8310 parser->allow_non_integral_constant_expression_p
8311 = saved_allow_non_integral_constant_expression_p;
8312 if (cxx_dialect >= cxx11)
8313 {
8314 /* Require an rvalue constant expression here; that's what our
8315 callers expect. Reference constant expressions are handled
8316 separately in e.g. cp_parser_template_argument. */
8317 bool is_const = potential_rvalue_constant_expression (expression);
8318 parser->non_integral_constant_expression_p = !is_const;
8319 if (!is_const && !allow_non_constant_p)
8320 require_potential_rvalue_constant_expression (expression);
8321 }
8322 if (allow_non_constant_p)
8323 *non_constant_p = parser->non_integral_constant_expression_p;
8324 parser->non_integral_constant_expression_p
8325 = saved_non_integral_constant_expression_p;
8326
8327 return expression;
8328 }
8329
8330 /* Parse __builtin_offsetof.
8331
8332 offsetof-expression:
8333 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8334
8335 offsetof-member-designator:
8336 id-expression
8337 | offsetof-member-designator "." id-expression
8338 | offsetof-member-designator "[" expression "]"
8339 | offsetof-member-designator "->" id-expression */
8340
8341 static tree
8342 cp_parser_builtin_offsetof (cp_parser *parser)
8343 {
8344 int save_ice_p, save_non_ice_p;
8345 tree type, expr;
8346 cp_id_kind dummy;
8347 cp_token *token;
8348
8349 /* We're about to accept non-integral-constant things, but will
8350 definitely yield an integral constant expression. Save and
8351 restore these values around our local parsing. */
8352 save_ice_p = parser->integral_constant_expression_p;
8353 save_non_ice_p = parser->non_integral_constant_expression_p;
8354
8355 /* Consume the "__builtin_offsetof" token. */
8356 cp_lexer_consume_token (parser->lexer);
8357 /* Consume the opening `('. */
8358 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8359 /* Parse the type-id. */
8360 type = cp_parser_type_id (parser);
8361 /* Look for the `,'. */
8362 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8363 token = cp_lexer_peek_token (parser->lexer);
8364
8365 /* Build the (type *)null that begins the traditional offsetof macro. */
8366 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8367 tf_warning_or_error);
8368
8369 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8370 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8371 true, &dummy, token->location);
8372 while (true)
8373 {
8374 token = cp_lexer_peek_token (parser->lexer);
8375 switch (token->type)
8376 {
8377 case CPP_OPEN_SQUARE:
8378 /* offsetof-member-designator "[" expression "]" */
8379 expr = cp_parser_postfix_open_square_expression (parser, expr,
8380 true, false);
8381 break;
8382
8383 case CPP_DEREF:
8384 /* offsetof-member-designator "->" identifier */
8385 expr = grok_array_decl (token->location, expr,
8386 integer_zero_node, false);
8387 /* FALLTHRU */
8388
8389 case CPP_DOT:
8390 /* offsetof-member-designator "." identifier */
8391 cp_lexer_consume_token (parser->lexer);
8392 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8393 expr, true, &dummy,
8394 token->location);
8395 break;
8396
8397 case CPP_CLOSE_PAREN:
8398 /* Consume the ")" token. */
8399 cp_lexer_consume_token (parser->lexer);
8400 goto success;
8401
8402 default:
8403 /* Error. We know the following require will fail, but
8404 that gives the proper error message. */
8405 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8406 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8407 expr = error_mark_node;
8408 goto failure;
8409 }
8410 }
8411
8412 success:
8413 /* If we're processing a template, we can't finish the semantics yet.
8414 Otherwise we can fold the entire expression now. */
8415 if (processing_template_decl)
8416 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8417 else
8418 expr = finish_offsetof (expr);
8419
8420 failure:
8421 parser->integral_constant_expression_p = save_ice_p;
8422 parser->non_integral_constant_expression_p = save_non_ice_p;
8423
8424 return expr;
8425 }
8426
8427 /* Parse a trait expression.
8428
8429 Returns a representation of the expression, the underlying type
8430 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8431
8432 static tree
8433 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8434 {
8435 cp_trait_kind kind;
8436 tree type1, type2 = NULL_TREE;
8437 bool binary = false;
8438 cp_decl_specifier_seq decl_specs;
8439
8440 switch (keyword)
8441 {
8442 case RID_HAS_NOTHROW_ASSIGN:
8443 kind = CPTK_HAS_NOTHROW_ASSIGN;
8444 break;
8445 case RID_HAS_NOTHROW_CONSTRUCTOR:
8446 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8447 break;
8448 case RID_HAS_NOTHROW_COPY:
8449 kind = CPTK_HAS_NOTHROW_COPY;
8450 break;
8451 case RID_HAS_TRIVIAL_ASSIGN:
8452 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8453 break;
8454 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8455 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8456 break;
8457 case RID_HAS_TRIVIAL_COPY:
8458 kind = CPTK_HAS_TRIVIAL_COPY;
8459 break;
8460 case RID_HAS_TRIVIAL_DESTRUCTOR:
8461 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8462 break;
8463 case RID_HAS_VIRTUAL_DESTRUCTOR:
8464 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8465 break;
8466 case RID_IS_ABSTRACT:
8467 kind = CPTK_IS_ABSTRACT;
8468 break;
8469 case RID_IS_BASE_OF:
8470 kind = CPTK_IS_BASE_OF;
8471 binary = true;
8472 break;
8473 case RID_IS_CLASS:
8474 kind = CPTK_IS_CLASS;
8475 break;
8476 case RID_IS_CONVERTIBLE_TO:
8477 kind = CPTK_IS_CONVERTIBLE_TO;
8478 binary = true;
8479 break;
8480 case RID_IS_EMPTY:
8481 kind = CPTK_IS_EMPTY;
8482 break;
8483 case RID_IS_ENUM:
8484 kind = CPTK_IS_ENUM;
8485 break;
8486 case RID_IS_FINAL:
8487 kind = CPTK_IS_FINAL;
8488 break;
8489 case RID_IS_LITERAL_TYPE:
8490 kind = CPTK_IS_LITERAL_TYPE;
8491 break;
8492 case RID_IS_POD:
8493 kind = CPTK_IS_POD;
8494 break;
8495 case RID_IS_POLYMORPHIC:
8496 kind = CPTK_IS_POLYMORPHIC;
8497 break;
8498 case RID_IS_STD_LAYOUT:
8499 kind = CPTK_IS_STD_LAYOUT;
8500 break;
8501 case RID_IS_TRIVIAL:
8502 kind = CPTK_IS_TRIVIAL;
8503 break;
8504 case RID_IS_UNION:
8505 kind = CPTK_IS_UNION;
8506 break;
8507 case RID_UNDERLYING_TYPE:
8508 kind = CPTK_UNDERLYING_TYPE;
8509 break;
8510 case RID_BASES:
8511 kind = CPTK_BASES;
8512 break;
8513 case RID_DIRECT_BASES:
8514 kind = CPTK_DIRECT_BASES;
8515 break;
8516 default:
8517 gcc_unreachable ();
8518 }
8519
8520 /* Consume the token. */
8521 cp_lexer_consume_token (parser->lexer);
8522
8523 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8524
8525 type1 = cp_parser_type_id (parser);
8526
8527 if (type1 == error_mark_node)
8528 return error_mark_node;
8529
8530 /* Build a trivial decl-specifier-seq. */
8531 clear_decl_specs (&decl_specs);
8532 decl_specs.type = type1;
8533
8534 /* Call grokdeclarator to figure out what type this is. */
8535 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8536 /*initialized=*/0, /*attrlist=*/NULL);
8537
8538 if (binary)
8539 {
8540 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8541
8542 type2 = cp_parser_type_id (parser);
8543
8544 if (type2 == error_mark_node)
8545 return error_mark_node;
8546
8547 /* Build a trivial decl-specifier-seq. */
8548 clear_decl_specs (&decl_specs);
8549 decl_specs.type = type2;
8550
8551 /* Call grokdeclarator to figure out what type this is. */
8552 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8553 /*initialized=*/0, /*attrlist=*/NULL);
8554 }
8555
8556 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8557
8558 /* Complete the trait expression, which may mean either processing
8559 the trait expr now or saving it for template instantiation. */
8560 switch(kind)
8561 {
8562 case CPTK_UNDERLYING_TYPE:
8563 return finish_underlying_type (type1);
8564 case CPTK_BASES:
8565 return finish_bases (type1, false);
8566 case CPTK_DIRECT_BASES:
8567 return finish_bases (type1, true);
8568 default:
8569 return finish_trait_expr (kind, type1, type2);
8570 }
8571 }
8572
8573 /* Lambdas that appear in variable initializer or default argument scope
8574 get that in their mangling, so we need to record it. We might as well
8575 use the count for function and namespace scopes as well. */
8576 static GTY(()) tree lambda_scope;
8577 static GTY(()) int lambda_count;
8578 typedef struct GTY(()) tree_int
8579 {
8580 tree t;
8581 int i;
8582 } tree_int;
8583 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8584
8585 static void
8586 start_lambda_scope (tree decl)
8587 {
8588 tree_int ti;
8589 gcc_assert (decl);
8590 /* Once we're inside a function, we ignore other scopes and just push
8591 the function again so that popping works properly. */
8592 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8593 decl = current_function_decl;
8594 ti.t = lambda_scope;
8595 ti.i = lambda_count;
8596 vec_safe_push (lambda_scope_stack, ti);
8597 if (lambda_scope != decl)
8598 {
8599 /* Don't reset the count if we're still in the same function. */
8600 lambda_scope = decl;
8601 lambda_count = 0;
8602 }
8603 }
8604
8605 static void
8606 record_lambda_scope (tree lambda)
8607 {
8608 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8609 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8610 }
8611
8612 static void
8613 finish_lambda_scope (void)
8614 {
8615 tree_int *p = &lambda_scope_stack->last ();
8616 if (lambda_scope != p->t)
8617 {
8618 lambda_scope = p->t;
8619 lambda_count = p->i;
8620 }
8621 lambda_scope_stack->pop ();
8622 }
8623
8624 /* Parse a lambda expression.
8625
8626 lambda-expression:
8627 lambda-introducer lambda-declarator [opt] compound-statement
8628
8629 Returns a representation of the expression. */
8630
8631 static tree
8632 cp_parser_lambda_expression (cp_parser* parser)
8633 {
8634 tree lambda_expr = build_lambda_expr ();
8635 tree type;
8636 bool ok;
8637
8638 LAMBDA_EXPR_LOCATION (lambda_expr)
8639 = cp_lexer_peek_token (parser->lexer)->location;
8640
8641 if (cp_unevaluated_operand)
8642 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8643 "lambda-expression in unevaluated context");
8644
8645 /* We may be in the middle of deferred access check. Disable
8646 it now. */
8647 push_deferring_access_checks (dk_no_deferred);
8648
8649 cp_parser_lambda_introducer (parser, lambda_expr);
8650
8651 type = begin_lambda_type (lambda_expr);
8652 if (type == error_mark_node)
8653 return error_mark_node;
8654
8655 record_lambda_scope (lambda_expr);
8656
8657 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8658 determine_visibility (TYPE_NAME (type));
8659
8660 /* Now that we've started the type, add the capture fields for any
8661 explicit captures. */
8662 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8663
8664 {
8665 /* Inside the class, surrounding template-parameter-lists do not apply. */
8666 unsigned int saved_num_template_parameter_lists
8667 = parser->num_template_parameter_lists;
8668 unsigned char in_statement = parser->in_statement;
8669 bool in_switch_statement_p = parser->in_switch_statement_p;
8670 bool fully_implicit_function_template_p
8671 = parser->fully_implicit_function_template_p;
8672 tree implicit_template_parms = parser->implicit_template_parms;
8673 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8674
8675 parser->num_template_parameter_lists = 0;
8676 parser->in_statement = 0;
8677 parser->in_switch_statement_p = false;
8678 parser->fully_implicit_function_template_p = false;
8679 parser->implicit_template_parms = 0;
8680 parser->implicit_template_scope = 0;
8681
8682 /* By virtue of defining a local class, a lambda expression has access to
8683 the private variables of enclosing classes. */
8684
8685 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8686
8687 if (ok)
8688 cp_parser_lambda_body (parser, lambda_expr);
8689 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8690 cp_parser_skip_to_end_of_block_or_statement (parser);
8691
8692 /* The capture list was built up in reverse order; fix that now. */
8693 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8694 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8695
8696 if (ok)
8697 maybe_add_lambda_conv_op (type);
8698
8699 type = finish_struct (type, /*attributes=*/NULL_TREE);
8700
8701 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8702 parser->in_statement = in_statement;
8703 parser->in_switch_statement_p = in_switch_statement_p;
8704 parser->fully_implicit_function_template_p
8705 = fully_implicit_function_template_p;
8706 parser->implicit_template_parms = implicit_template_parms;
8707 parser->implicit_template_scope = implicit_template_scope;
8708 }
8709
8710 pop_deferring_access_checks ();
8711
8712 /* This field is only used during parsing of the lambda. */
8713 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8714
8715 /* This lambda shouldn't have any proxies left at this point. */
8716 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8717 /* And now that we're done, push proxies for an enclosing lambda. */
8718 insert_pending_capture_proxies ();
8719
8720 if (ok)
8721 return build_lambda_object (lambda_expr);
8722 else
8723 return error_mark_node;
8724 }
8725
8726 /* Parse the beginning of a lambda expression.
8727
8728 lambda-introducer:
8729 [ lambda-capture [opt] ]
8730
8731 LAMBDA_EXPR is the current representation of the lambda expression. */
8732
8733 static void
8734 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8735 {
8736 /* Need commas after the first capture. */
8737 bool first = true;
8738
8739 /* Eat the leading `['. */
8740 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8741
8742 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8743 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8744 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8745 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8746 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8747 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8748
8749 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8750 {
8751 cp_lexer_consume_token (parser->lexer);
8752 first = false;
8753 }
8754
8755 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8756 {
8757 cp_token* capture_token;
8758 tree capture_id;
8759 tree capture_init_expr;
8760 cp_id_kind idk = CP_ID_KIND_NONE;
8761 bool explicit_init_p = false;
8762
8763 enum capture_kind_type
8764 {
8765 BY_COPY,
8766 BY_REFERENCE
8767 };
8768 enum capture_kind_type capture_kind = BY_COPY;
8769
8770 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8771 {
8772 error ("expected end of capture-list");
8773 return;
8774 }
8775
8776 if (first)
8777 first = false;
8778 else
8779 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8780
8781 /* Possibly capture `this'. */
8782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8783 {
8784 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8785 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8786 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8787 "with by-copy capture default");
8788 cp_lexer_consume_token (parser->lexer);
8789 add_capture (lambda_expr,
8790 /*id=*/this_identifier,
8791 /*initializer=*/finish_this_expr(),
8792 /*by_reference_p=*/false,
8793 explicit_init_p);
8794 continue;
8795 }
8796
8797 /* Remember whether we want to capture as a reference or not. */
8798 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8799 {
8800 capture_kind = BY_REFERENCE;
8801 cp_lexer_consume_token (parser->lexer);
8802 }
8803
8804 /* Get the identifier. */
8805 capture_token = cp_lexer_peek_token (parser->lexer);
8806 capture_id = cp_parser_identifier (parser);
8807
8808 if (capture_id == error_mark_node)
8809 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8810 delimiters, but I modified this to stop on unnested ']' as well. It
8811 was already changed to stop on unnested '}', so the
8812 "closing_parenthesis" name is no more misleading with my change. */
8813 {
8814 cp_parser_skip_to_closing_parenthesis (parser,
8815 /*recovering=*/true,
8816 /*or_comma=*/true,
8817 /*consume_paren=*/true);
8818 break;
8819 }
8820
8821 /* Find the initializer for this capture. */
8822 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8823 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8824 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8825 {
8826 bool direct, non_constant;
8827 /* An explicit initializer exists. */
8828 if (cxx_dialect < cxx1y)
8829 pedwarn (input_location, 0,
8830 "lambda capture initializers "
8831 "only available with -std=c++1y or -std=gnu++1y");
8832 capture_init_expr = cp_parser_initializer (parser, &direct,
8833 &non_constant);
8834 explicit_init_p = true;
8835 }
8836 else
8837 {
8838 const char* error_msg;
8839
8840 /* Turn the identifier into an id-expression. */
8841 capture_init_expr
8842 = cp_parser_lookup_name_simple (parser, capture_id,
8843 capture_token->location);
8844
8845 if (capture_init_expr == error_mark_node)
8846 {
8847 unqualified_name_lookup_error (capture_id);
8848 continue;
8849 }
8850 else if (DECL_P (capture_init_expr)
8851 && (!VAR_P (capture_init_expr)
8852 && TREE_CODE (capture_init_expr) != PARM_DECL))
8853 {
8854 error_at (capture_token->location,
8855 "capture of non-variable %qD ",
8856 capture_init_expr);
8857 inform (0, "%q+#D declared here", capture_init_expr);
8858 continue;
8859 }
8860 if (VAR_P (capture_init_expr)
8861 && decl_storage_duration (capture_init_expr) != dk_auto)
8862 {
8863 pedwarn (capture_token->location, 0, "capture of variable "
8864 "%qD with non-automatic storage duration",
8865 capture_init_expr);
8866 inform (0, "%q+#D declared here", capture_init_expr);
8867 continue;
8868 }
8869
8870 capture_init_expr
8871 = finish_id_expression
8872 (capture_id,
8873 capture_init_expr,
8874 parser->scope,
8875 &idk,
8876 /*integral_constant_expression_p=*/false,
8877 /*allow_non_integral_constant_expression_p=*/false,
8878 /*non_integral_constant_expression_p=*/NULL,
8879 /*template_p=*/false,
8880 /*done=*/true,
8881 /*address_p=*/false,
8882 /*template_arg_p=*/false,
8883 &error_msg,
8884 capture_token->location);
8885
8886 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8887 {
8888 cp_lexer_consume_token (parser->lexer);
8889 capture_init_expr = make_pack_expansion (capture_init_expr);
8890 }
8891 else
8892 check_for_bare_parameter_packs (capture_init_expr);
8893 }
8894
8895 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8896 && !explicit_init_p)
8897 {
8898 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8899 && capture_kind == BY_COPY)
8900 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8901 "of %qD redundant with by-copy capture default",
8902 capture_id);
8903 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8904 && capture_kind == BY_REFERENCE)
8905 pedwarn (capture_token->location, 0, "explicit by-reference "
8906 "capture of %qD redundant with by-reference capture "
8907 "default", capture_id);
8908 }
8909
8910 add_capture (lambda_expr,
8911 capture_id,
8912 capture_init_expr,
8913 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8914 explicit_init_p);
8915 }
8916
8917 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8918 }
8919
8920 /* Parse the (optional) middle of a lambda expression.
8921
8922 lambda-declarator:
8923 < template-parameter-list [opt] >
8924 ( parameter-declaration-clause [opt] )
8925 attribute-specifier [opt]
8926 mutable [opt]
8927 exception-specification [opt]
8928 lambda-return-type-clause [opt]
8929
8930 LAMBDA_EXPR is the current representation of the lambda expression. */
8931
8932 static bool
8933 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8934 {
8935 /* 5.1.1.4 of the standard says:
8936 If a lambda-expression does not include a lambda-declarator, it is as if
8937 the lambda-declarator were ().
8938 This means an empty parameter list, no attributes, and no exception
8939 specification. */
8940 tree param_list = void_list_node;
8941 tree attributes = NULL_TREE;
8942 tree exception_spec = NULL_TREE;
8943 tree template_param_list = NULL_TREE;
8944
8945 /* The template-parameter-list is optional, but must begin with
8946 an opening angle if present. */
8947 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8948 {
8949 if (cxx_dialect < cxx1y)
8950 pedwarn (parser->lexer->next_token->location, 0,
8951 "lambda templates are only available with "
8952 "-std=c++1y or -std=gnu++1y");
8953
8954 cp_lexer_consume_token (parser->lexer);
8955
8956 template_param_list = cp_parser_template_parameter_list (parser);
8957
8958 cp_parser_skip_to_end_of_template_parameter_list (parser);
8959
8960 /* We just processed one more parameter list. */
8961 ++parser->num_template_parameter_lists;
8962 }
8963
8964 /* The parameter-declaration-clause is optional (unless
8965 template-parameter-list was given), but must begin with an
8966 opening parenthesis if present. */
8967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8968 {
8969 cp_lexer_consume_token (parser->lexer);
8970
8971 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8972
8973 /* Parse parameters. */
8974 param_list = cp_parser_parameter_declaration_clause (parser);
8975
8976 /* Default arguments shall not be specified in the
8977 parameter-declaration-clause of a lambda-declarator. */
8978 for (tree t = param_list; t; t = TREE_CHAIN (t))
8979 if (TREE_PURPOSE (t))
8980 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8981 "default argument specified for lambda parameter");
8982
8983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8984
8985 attributes = cp_parser_attributes_opt (parser);
8986
8987 /* Parse optional `mutable' keyword. */
8988 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8989 {
8990 cp_lexer_consume_token (parser->lexer);
8991 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8992 }
8993
8994 /* Parse optional exception specification. */
8995 exception_spec = cp_parser_exception_specification_opt (parser);
8996
8997 /* Parse optional trailing return type. */
8998 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8999 {
9000 cp_lexer_consume_token (parser->lexer);
9001 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9002 = cp_parser_trailing_type_id (parser);
9003 }
9004
9005 /* The function parameters must be in scope all the way until after the
9006 trailing-return-type in case of decltype. */
9007 pop_bindings_and_leave_scope ();
9008 }
9009 else if (template_param_list != NULL_TREE) // generate diagnostic
9010 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9011
9012 /* Create the function call operator.
9013
9014 Messing with declarators like this is no uglier than building up the
9015 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9016 other code. */
9017 {
9018 cp_decl_specifier_seq return_type_specs;
9019 cp_declarator* declarator;
9020 tree fco;
9021 int quals;
9022 void *p;
9023
9024 clear_decl_specs (&return_type_specs);
9025 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9026 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9027 else
9028 /* Maybe we will deduce the return type later. */
9029 return_type_specs.type = make_auto ();
9030
9031 p = obstack_alloc (&declarator_obstack, 0);
9032
9033 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9034 sfk_none);
9035
9036 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9037 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9038 declarator = make_call_declarator (declarator, param_list, quals,
9039 VIRT_SPEC_UNSPECIFIED,
9040 REF_QUAL_NONE,
9041 exception_spec,
9042 /*late_return_type=*/NULL_TREE);
9043 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9044
9045 fco = grokmethod (&return_type_specs,
9046 declarator,
9047 attributes);
9048 if (fco != error_mark_node)
9049 {
9050 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9051 DECL_ARTIFICIAL (fco) = 1;
9052 /* Give the object parameter a different name. */
9053 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9054 if (template_param_list)
9055 {
9056 fco = finish_member_template_decl (fco);
9057 finish_template_decl (template_param_list);
9058 --parser->num_template_parameter_lists;
9059 }
9060 else if (parser->fully_implicit_function_template_p)
9061 fco = finish_fully_implicit_template (parser, fco);
9062 }
9063
9064 finish_member_declaration (fco);
9065
9066 obstack_free (&declarator_obstack, p);
9067
9068 return (fco != error_mark_node);
9069 }
9070 }
9071
9072 /* Parse the body of a lambda expression, which is simply
9073
9074 compound-statement
9075
9076 but which requires special handling.
9077 LAMBDA_EXPR is the current representation of the lambda expression. */
9078
9079 static void
9080 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9081 {
9082 bool nested = (current_function_decl != NULL_TREE);
9083 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9084 if (nested)
9085 push_function_context ();
9086 else
9087 /* Still increment function_depth so that we don't GC in the
9088 middle of an expression. */
9089 ++function_depth;
9090 /* Clear this in case we're in the middle of a default argument. */
9091 parser->local_variables_forbidden_p = false;
9092
9093 /* Finish the function call operator
9094 - class_specifier
9095 + late_parsing_for_member
9096 + function_definition_after_declarator
9097 + ctor_initializer_opt_and_function_body */
9098 {
9099 tree fco = lambda_function (lambda_expr);
9100 tree body;
9101 bool done = false;
9102 tree compound_stmt;
9103 tree cap;
9104
9105 /* Let the front end know that we are going to be defining this
9106 function. */
9107 start_preparsed_function (fco,
9108 NULL_TREE,
9109 SF_PRE_PARSED | SF_INCLASS_INLINE);
9110
9111 start_lambda_scope (fco);
9112 body = begin_function_body ();
9113
9114 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9115 goto out;
9116
9117 /* Push the proxies for any explicit captures. */
9118 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9119 cap = TREE_CHAIN (cap))
9120 build_capture_proxy (TREE_PURPOSE (cap));
9121
9122 compound_stmt = begin_compound_stmt (0);
9123
9124 /* 5.1.1.4 of the standard says:
9125 If a lambda-expression does not include a trailing-return-type, it
9126 is as if the trailing-return-type denotes the following type:
9127 * if the compound-statement is of the form
9128 { return attribute-specifier [opt] expression ; }
9129 the type of the returned expression after lvalue-to-rvalue
9130 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9131 (_conv.array_ 4.2), and function-to-pointer conversion
9132 (_conv.func_ 4.3);
9133 * otherwise, void. */
9134
9135 /* In a lambda that has neither a lambda-return-type-clause
9136 nor a deducible form, errors should be reported for return statements
9137 in the body. Since we used void as the placeholder return type, parsing
9138 the body as usual will give such desired behavior. */
9139 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9140 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9141 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9142 {
9143 tree expr = NULL_TREE;
9144 cp_id_kind idk = CP_ID_KIND_NONE;
9145
9146 /* Parse tentatively in case there's more after the initial return
9147 statement. */
9148 cp_parser_parse_tentatively (parser);
9149
9150 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9151
9152 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9153
9154 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9155 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9156
9157 if (cp_parser_parse_definitely (parser))
9158 {
9159 if (!processing_template_decl)
9160 apply_deduced_return_type (fco, lambda_return_type (expr));
9161
9162 /* Will get error here if type not deduced yet. */
9163 finish_return_stmt (expr);
9164
9165 done = true;
9166 }
9167 }
9168
9169 if (!done)
9170 {
9171 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9172 cp_parser_label_declaration (parser);
9173 cp_parser_statement_seq_opt (parser, NULL_TREE);
9174 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9175 }
9176
9177 finish_compound_stmt (compound_stmt);
9178
9179 out:
9180 finish_function_body (body);
9181 finish_lambda_scope ();
9182
9183 /* Finish the function and generate code for it if necessary. */
9184 tree fn = finish_function (/*inline*/2);
9185
9186 /* Only expand if the call op is not a template. */
9187 if (!DECL_TEMPLATE_INFO (fco))
9188 expand_or_defer_fn (fn);
9189 }
9190
9191 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9192 if (nested)
9193 pop_function_context();
9194 else
9195 --function_depth;
9196 }
9197
9198 /* Statements [gram.stmt.stmt] */
9199
9200 /* Parse a statement.
9201
9202 statement:
9203 labeled-statement
9204 expression-statement
9205 compound-statement
9206 selection-statement
9207 iteration-statement
9208 jump-statement
9209 declaration-statement
9210 try-block
9211
9212 C++11:
9213
9214 statement:
9215 labeled-statement
9216 attribute-specifier-seq (opt) expression-statement
9217 attribute-specifier-seq (opt) compound-statement
9218 attribute-specifier-seq (opt) selection-statement
9219 attribute-specifier-seq (opt) iteration-statement
9220 attribute-specifier-seq (opt) jump-statement
9221 declaration-statement
9222 attribute-specifier-seq (opt) try-block
9223
9224 TM Extension:
9225
9226 statement:
9227 atomic-statement
9228
9229 IN_COMPOUND is true when the statement is nested inside a
9230 cp_parser_compound_statement; this matters for certain pragmas.
9231
9232 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9233 is a (possibly labeled) if statement which is not enclosed in braces
9234 and has an else clause. This is used to implement -Wparentheses. */
9235
9236 static void
9237 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9238 bool in_compound, bool *if_p)
9239 {
9240 tree statement, std_attrs = NULL_TREE;
9241 cp_token *token;
9242 location_t statement_location, attrs_location;
9243
9244 restart:
9245 if (if_p != NULL)
9246 *if_p = false;
9247 /* There is no statement yet. */
9248 statement = NULL_TREE;
9249
9250 cp_lexer_save_tokens (parser->lexer);
9251 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9252 if (c_dialect_objc ())
9253 /* In obj-c++, seeing '[[' might be the either the beginning of
9254 c++11 attributes, or a nested objc-message-expression. So
9255 let's parse the c++11 attributes tentatively. */
9256 cp_parser_parse_tentatively (parser);
9257 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9258 if (c_dialect_objc ())
9259 {
9260 if (!cp_parser_parse_definitely (parser))
9261 std_attrs = NULL_TREE;
9262 }
9263
9264 /* Peek at the next token. */
9265 token = cp_lexer_peek_token (parser->lexer);
9266 /* Remember the location of the first token in the statement. */
9267 statement_location = token->location;
9268 /* If this is a keyword, then that will often determine what kind of
9269 statement we have. */
9270 if (token->type == CPP_KEYWORD)
9271 {
9272 enum rid keyword = token->keyword;
9273
9274 switch (keyword)
9275 {
9276 case RID_CASE:
9277 case RID_DEFAULT:
9278 /* Looks like a labeled-statement with a case label.
9279 Parse the label, and then use tail recursion to parse
9280 the statement. */
9281 cp_parser_label_for_labeled_statement (parser, std_attrs);
9282 goto restart;
9283
9284 case RID_IF:
9285 case RID_SWITCH:
9286 statement = cp_parser_selection_statement (parser, if_p);
9287 break;
9288
9289 case RID_WHILE:
9290 case RID_DO:
9291 case RID_FOR:
9292 statement = cp_parser_iteration_statement (parser, false);
9293 break;
9294
9295 case RID_BREAK:
9296 case RID_CONTINUE:
9297 case RID_RETURN:
9298 case RID_GOTO:
9299 statement = cp_parser_jump_statement (parser);
9300 break;
9301
9302 /* Objective-C++ exception-handling constructs. */
9303 case RID_AT_TRY:
9304 case RID_AT_CATCH:
9305 case RID_AT_FINALLY:
9306 case RID_AT_SYNCHRONIZED:
9307 case RID_AT_THROW:
9308 statement = cp_parser_objc_statement (parser);
9309 break;
9310
9311 case RID_TRY:
9312 statement = cp_parser_try_block (parser);
9313 break;
9314
9315 case RID_NAMESPACE:
9316 /* This must be a namespace alias definition. */
9317 cp_parser_declaration_statement (parser);
9318 return;
9319
9320 case RID_TRANSACTION_ATOMIC:
9321 case RID_TRANSACTION_RELAXED:
9322 statement = cp_parser_transaction (parser, keyword);
9323 break;
9324 case RID_TRANSACTION_CANCEL:
9325 statement = cp_parser_transaction_cancel (parser);
9326 break;
9327
9328 default:
9329 /* It might be a keyword like `int' that can start a
9330 declaration-statement. */
9331 break;
9332 }
9333 }
9334 else if (token->type == CPP_NAME)
9335 {
9336 /* If the next token is a `:', then we are looking at a
9337 labeled-statement. */
9338 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9339 if (token->type == CPP_COLON)
9340 {
9341 /* Looks like a labeled-statement with an ordinary label.
9342 Parse the label, and then use tail recursion to parse
9343 the statement. */
9344
9345 cp_parser_label_for_labeled_statement (parser, std_attrs);
9346 goto restart;
9347 }
9348 }
9349 /* Anything that starts with a `{' must be a compound-statement. */
9350 else if (token->type == CPP_OPEN_BRACE)
9351 statement = cp_parser_compound_statement (parser, NULL, false, false);
9352 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9353 a statement all its own. */
9354 else if (token->type == CPP_PRAGMA)
9355 {
9356 /* Only certain OpenMP pragmas are attached to statements, and thus
9357 are considered statements themselves. All others are not. In
9358 the context of a compound, accept the pragma as a "statement" and
9359 return so that we can check for a close brace. Otherwise we
9360 require a real statement and must go back and read one. */
9361 if (in_compound)
9362 cp_parser_pragma (parser, pragma_compound);
9363 else if (!cp_parser_pragma (parser, pragma_stmt))
9364 goto restart;
9365 return;
9366 }
9367 else if (token->type == CPP_EOF)
9368 {
9369 cp_parser_error (parser, "expected statement");
9370 return;
9371 }
9372
9373 /* Everything else must be a declaration-statement or an
9374 expression-statement. Try for the declaration-statement
9375 first, unless we are looking at a `;', in which case we know that
9376 we have an expression-statement. */
9377 if (!statement)
9378 {
9379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9380 {
9381 if (std_attrs != NULL_TREE)
9382 {
9383 /* Attributes should be parsed as part of the the
9384 declaration, so let's un-parse them. */
9385 cp_lexer_rollback_tokens (parser->lexer);
9386 std_attrs = NULL_TREE;
9387 }
9388
9389 cp_parser_parse_tentatively (parser);
9390 /* Try to parse the declaration-statement. */
9391 cp_parser_declaration_statement (parser);
9392 /* If that worked, we're done. */
9393 if (cp_parser_parse_definitely (parser))
9394 return;
9395 }
9396 /* Look for an expression-statement instead. */
9397 statement = cp_parser_expression_statement (parser, in_statement_expr);
9398 }
9399
9400 /* Set the line number for the statement. */
9401 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9402 SET_EXPR_LOCATION (statement, statement_location);
9403
9404 /* Note that for now, we don't do anything with c++11 statements
9405 parsed at this level. */
9406 if (std_attrs != NULL_TREE)
9407 warning_at (attrs_location,
9408 OPT_Wattributes,
9409 "attributes at the beginning of statement are ignored");
9410 }
9411
9412 /* Parse the label for a labeled-statement, i.e.
9413
9414 identifier :
9415 case constant-expression :
9416 default :
9417
9418 GNU Extension:
9419 case constant-expression ... constant-expression : statement
9420
9421 When a label is parsed without errors, the label is added to the
9422 parse tree by the finish_* functions, so this function doesn't
9423 have to return the label. */
9424
9425 static void
9426 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9427 {
9428 cp_token *token;
9429 tree label = NULL_TREE;
9430 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9431
9432 /* The next token should be an identifier. */
9433 token = cp_lexer_peek_token (parser->lexer);
9434 if (token->type != CPP_NAME
9435 && token->type != CPP_KEYWORD)
9436 {
9437 cp_parser_error (parser, "expected labeled-statement");
9438 return;
9439 }
9440
9441 parser->colon_corrects_to_scope_p = false;
9442 switch (token->keyword)
9443 {
9444 case RID_CASE:
9445 {
9446 tree expr, expr_hi;
9447 cp_token *ellipsis;
9448
9449 /* Consume the `case' token. */
9450 cp_lexer_consume_token (parser->lexer);
9451 /* Parse the constant-expression. */
9452 expr = cp_parser_constant_expression (parser,
9453 /*allow_non_constant_p=*/false,
9454 NULL);
9455
9456 ellipsis = cp_lexer_peek_token (parser->lexer);
9457 if (ellipsis->type == CPP_ELLIPSIS)
9458 {
9459 /* Consume the `...' token. */
9460 cp_lexer_consume_token (parser->lexer);
9461 expr_hi =
9462 cp_parser_constant_expression (parser,
9463 /*allow_non_constant_p=*/false,
9464 NULL);
9465 /* We don't need to emit warnings here, as the common code
9466 will do this for us. */
9467 }
9468 else
9469 expr_hi = NULL_TREE;
9470
9471 if (parser->in_switch_statement_p)
9472 finish_case_label (token->location, expr, expr_hi);
9473 else
9474 error_at (token->location,
9475 "case label %qE not within a switch statement",
9476 expr);
9477 }
9478 break;
9479
9480 case RID_DEFAULT:
9481 /* Consume the `default' token. */
9482 cp_lexer_consume_token (parser->lexer);
9483
9484 if (parser->in_switch_statement_p)
9485 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9486 else
9487 error_at (token->location, "case label not within a switch statement");
9488 break;
9489
9490 default:
9491 /* Anything else must be an ordinary label. */
9492 label = finish_label_stmt (cp_parser_identifier (parser));
9493 break;
9494 }
9495
9496 /* Require the `:' token. */
9497 cp_parser_require (parser, CPP_COLON, RT_COLON);
9498
9499 /* An ordinary label may optionally be followed by attributes.
9500 However, this is only permitted if the attributes are then
9501 followed by a semicolon. This is because, for backward
9502 compatibility, when parsing
9503 lab: __attribute__ ((unused)) int i;
9504 we want the attribute to attach to "i", not "lab". */
9505 if (label != NULL_TREE
9506 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9507 {
9508 tree attrs;
9509 cp_parser_parse_tentatively (parser);
9510 attrs = cp_parser_gnu_attributes_opt (parser);
9511 if (attrs == NULL_TREE
9512 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9513 cp_parser_abort_tentative_parse (parser);
9514 else if (!cp_parser_parse_definitely (parser))
9515 ;
9516 else
9517 attributes = chainon (attributes, attrs);
9518 }
9519
9520 if (attributes != NULL_TREE)
9521 cplus_decl_attributes (&label, attributes, 0);
9522
9523 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9524 }
9525
9526 /* Parse an expression-statement.
9527
9528 expression-statement:
9529 expression [opt] ;
9530
9531 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9532 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9533 indicates whether this expression-statement is part of an
9534 expression statement. */
9535
9536 static tree
9537 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9538 {
9539 tree statement = NULL_TREE;
9540 cp_token *token = cp_lexer_peek_token (parser->lexer);
9541
9542 /* If the next token is a ';', then there is no expression
9543 statement. */
9544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9545 {
9546 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9547 if (statement == error_mark_node
9548 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9549 {
9550 cp_parser_skip_to_end_of_block_or_statement (parser);
9551 return error_mark_node;
9552 }
9553 }
9554
9555 /* Give a helpful message for "A<T>::type t;" and the like. */
9556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9557 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9558 {
9559 if (TREE_CODE (statement) == SCOPE_REF)
9560 error_at (token->location, "need %<typename%> before %qE because "
9561 "%qT is a dependent scope",
9562 statement, TREE_OPERAND (statement, 0));
9563 else if (is_overloaded_fn (statement)
9564 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9565 {
9566 /* A::A a; */
9567 tree fn = get_first_fn (statement);
9568 error_at (token->location,
9569 "%<%T::%D%> names the constructor, not the type",
9570 DECL_CONTEXT (fn), DECL_NAME (fn));
9571 }
9572 }
9573
9574 /* Consume the final `;'. */
9575 cp_parser_consume_semicolon_at_end_of_statement (parser);
9576
9577 if (in_statement_expr
9578 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9579 /* This is the final expression statement of a statement
9580 expression. */
9581 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9582 else if (statement)
9583 statement = finish_expr_stmt (statement);
9584
9585 return statement;
9586 }
9587
9588 /* Parse a compound-statement.
9589
9590 compound-statement:
9591 { statement-seq [opt] }
9592
9593 GNU extension:
9594
9595 compound-statement:
9596 { label-declaration-seq [opt] statement-seq [opt] }
9597
9598 label-declaration-seq:
9599 label-declaration
9600 label-declaration-seq label-declaration
9601
9602 Returns a tree representing the statement. */
9603
9604 static tree
9605 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9606 bool in_try, bool function_body)
9607 {
9608 tree compound_stmt;
9609
9610 /* Consume the `{'. */
9611 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9612 return error_mark_node;
9613 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9614 && !function_body)
9615 pedwarn (input_location, OPT_Wpedantic,
9616 "compound-statement in constexpr function");
9617 /* Begin the compound-statement. */
9618 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9619 /* If the next keyword is `__label__' we have a label declaration. */
9620 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9621 cp_parser_label_declaration (parser);
9622 /* Parse an (optional) statement-seq. */
9623 cp_parser_statement_seq_opt (parser, in_statement_expr);
9624 /* Finish the compound-statement. */
9625 finish_compound_stmt (compound_stmt);
9626 /* Consume the `}'. */
9627 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9628
9629 return compound_stmt;
9630 }
9631
9632 /* Parse an (optional) statement-seq.
9633
9634 statement-seq:
9635 statement
9636 statement-seq [opt] statement */
9637
9638 static void
9639 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9640 {
9641 /* Scan statements until there aren't any more. */
9642 while (true)
9643 {
9644 cp_token *token = cp_lexer_peek_token (parser->lexer);
9645
9646 /* If we are looking at a `}', then we have run out of
9647 statements; the same is true if we have reached the end
9648 of file, or have stumbled upon a stray '@end'. */
9649 if (token->type == CPP_CLOSE_BRACE
9650 || token->type == CPP_EOF
9651 || token->type == CPP_PRAGMA_EOL
9652 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9653 break;
9654
9655 /* If we are in a compound statement and find 'else' then
9656 something went wrong. */
9657 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9658 {
9659 if (parser->in_statement & IN_IF_STMT)
9660 break;
9661 else
9662 {
9663 token = cp_lexer_consume_token (parser->lexer);
9664 error_at (token->location, "%<else%> without a previous %<if%>");
9665 }
9666 }
9667
9668 /* Parse the statement. */
9669 cp_parser_statement (parser, in_statement_expr, true, NULL);
9670 }
9671 }
9672
9673 /* Parse a selection-statement.
9674
9675 selection-statement:
9676 if ( condition ) statement
9677 if ( condition ) statement else statement
9678 switch ( condition ) statement
9679
9680 Returns the new IF_STMT or SWITCH_STMT.
9681
9682 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9683 is a (possibly labeled) if statement which is not enclosed in
9684 braces and has an else clause. This is used to implement
9685 -Wparentheses. */
9686
9687 static tree
9688 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9689 {
9690 cp_token *token;
9691 enum rid keyword;
9692
9693 if (if_p != NULL)
9694 *if_p = false;
9695
9696 /* Peek at the next token. */
9697 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9698
9699 /* See what kind of keyword it is. */
9700 keyword = token->keyword;
9701 switch (keyword)
9702 {
9703 case RID_IF:
9704 case RID_SWITCH:
9705 {
9706 tree statement;
9707 tree condition;
9708
9709 /* Look for the `('. */
9710 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9711 {
9712 cp_parser_skip_to_end_of_statement (parser);
9713 return error_mark_node;
9714 }
9715
9716 /* Begin the selection-statement. */
9717 if (keyword == RID_IF)
9718 statement = begin_if_stmt ();
9719 else
9720 statement = begin_switch_stmt ();
9721
9722 /* Parse the condition. */
9723 condition = cp_parser_condition (parser);
9724 /* Look for the `)'. */
9725 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9726 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9727 /*consume_paren=*/true);
9728
9729 if (keyword == RID_IF)
9730 {
9731 bool nested_if;
9732 unsigned char in_statement;
9733
9734 /* Add the condition. */
9735 finish_if_stmt_cond (condition, statement);
9736
9737 /* Parse the then-clause. */
9738 in_statement = parser->in_statement;
9739 parser->in_statement |= IN_IF_STMT;
9740 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9741 {
9742 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9743 add_stmt (build_empty_stmt (loc));
9744 cp_lexer_consume_token (parser->lexer);
9745 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9746 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9747 "empty body in an %<if%> statement");
9748 nested_if = false;
9749 }
9750 else
9751 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9752 parser->in_statement = in_statement;
9753
9754 finish_then_clause (statement);
9755
9756 /* If the next token is `else', parse the else-clause. */
9757 if (cp_lexer_next_token_is_keyword (parser->lexer,
9758 RID_ELSE))
9759 {
9760 /* Consume the `else' keyword. */
9761 cp_lexer_consume_token (parser->lexer);
9762 begin_else_clause (statement);
9763 /* Parse the else-clause. */
9764 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9765 {
9766 location_t loc;
9767 loc = cp_lexer_peek_token (parser->lexer)->location;
9768 warning_at (loc,
9769 OPT_Wempty_body, "suggest braces around "
9770 "empty body in an %<else%> statement");
9771 add_stmt (build_empty_stmt (loc));
9772 cp_lexer_consume_token (parser->lexer);
9773 }
9774 else
9775 cp_parser_implicitly_scoped_statement (parser, NULL);
9776
9777 finish_else_clause (statement);
9778
9779 /* If we are currently parsing a then-clause, then
9780 IF_P will not be NULL. We set it to true to
9781 indicate that this if statement has an else clause.
9782 This may trigger the Wparentheses warning below
9783 when we get back up to the parent if statement. */
9784 if (if_p != NULL)
9785 *if_p = true;
9786 }
9787 else
9788 {
9789 /* This if statement does not have an else clause. If
9790 NESTED_IF is true, then the then-clause is an if
9791 statement which does have an else clause. We warn
9792 about the potential ambiguity. */
9793 if (nested_if)
9794 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9795 "suggest explicit braces to avoid ambiguous"
9796 " %<else%>");
9797 }
9798
9799 /* Now we're all done with the if-statement. */
9800 finish_if_stmt (statement);
9801 }
9802 else
9803 {
9804 bool in_switch_statement_p;
9805 unsigned char in_statement;
9806
9807 /* Add the condition. */
9808 finish_switch_cond (condition, statement);
9809
9810 /* Parse the body of the switch-statement. */
9811 in_switch_statement_p = parser->in_switch_statement_p;
9812 in_statement = parser->in_statement;
9813 parser->in_switch_statement_p = true;
9814 parser->in_statement |= IN_SWITCH_STMT;
9815 cp_parser_implicitly_scoped_statement (parser, NULL);
9816 parser->in_switch_statement_p = in_switch_statement_p;
9817 parser->in_statement = in_statement;
9818
9819 /* Now we're all done with the switch-statement. */
9820 finish_switch_stmt (statement);
9821 }
9822
9823 return statement;
9824 }
9825 break;
9826
9827 default:
9828 cp_parser_error (parser, "expected selection-statement");
9829 return error_mark_node;
9830 }
9831 }
9832
9833 /* Parse a condition.
9834
9835 condition:
9836 expression
9837 type-specifier-seq declarator = initializer-clause
9838 type-specifier-seq declarator braced-init-list
9839
9840 GNU Extension:
9841
9842 condition:
9843 type-specifier-seq declarator asm-specification [opt]
9844 attributes [opt] = assignment-expression
9845
9846 Returns the expression that should be tested. */
9847
9848 static tree
9849 cp_parser_condition (cp_parser* parser)
9850 {
9851 cp_decl_specifier_seq type_specifiers;
9852 const char *saved_message;
9853 int declares_class_or_enum;
9854
9855 /* Try the declaration first. */
9856 cp_parser_parse_tentatively (parser);
9857 /* New types are not allowed in the type-specifier-seq for a
9858 condition. */
9859 saved_message = parser->type_definition_forbidden_message;
9860 parser->type_definition_forbidden_message
9861 = G_("types may not be defined in conditions");
9862 /* Parse the type-specifier-seq. */
9863 cp_parser_decl_specifier_seq (parser,
9864 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9865 &type_specifiers,
9866 &declares_class_or_enum);
9867 /* Restore the saved message. */
9868 parser->type_definition_forbidden_message = saved_message;
9869 /* If all is well, we might be looking at a declaration. */
9870 if (!cp_parser_error_occurred (parser))
9871 {
9872 tree decl;
9873 tree asm_specification;
9874 tree attributes;
9875 cp_declarator *declarator;
9876 tree initializer = NULL_TREE;
9877
9878 /* Parse the declarator. */
9879 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9880 /*ctor_dtor_or_conv_p=*/NULL,
9881 /*parenthesized_p=*/NULL,
9882 /*member_p=*/false);
9883 /* Parse the attributes. */
9884 attributes = cp_parser_attributes_opt (parser);
9885 /* Parse the asm-specification. */
9886 asm_specification = cp_parser_asm_specification_opt (parser);
9887 /* If the next token is not an `=' or '{', then we might still be
9888 looking at an expression. For example:
9889
9890 if (A(a).x)
9891
9892 looks like a decl-specifier-seq and a declarator -- but then
9893 there is no `=', so this is an expression. */
9894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9895 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9896 cp_parser_simulate_error (parser);
9897
9898 /* If we did see an `=' or '{', then we are looking at a declaration
9899 for sure. */
9900 if (cp_parser_parse_definitely (parser))
9901 {
9902 tree pushed_scope;
9903 bool non_constant_p;
9904 bool flags = LOOKUP_ONLYCONVERTING;
9905
9906 /* Create the declaration. */
9907 decl = start_decl (declarator, &type_specifiers,
9908 /*initialized_p=*/true,
9909 attributes, /*prefix_attributes=*/NULL_TREE,
9910 &pushed_scope);
9911
9912 /* Parse the initializer. */
9913 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9914 {
9915 initializer = cp_parser_braced_list (parser, &non_constant_p);
9916 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9917 flags = 0;
9918 }
9919 else
9920 {
9921 /* Consume the `='. */
9922 cp_parser_require (parser, CPP_EQ, RT_EQ);
9923 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9924 }
9925 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9926 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9927
9928 /* Process the initializer. */
9929 cp_finish_decl (decl,
9930 initializer, !non_constant_p,
9931 asm_specification,
9932 flags);
9933
9934 if (pushed_scope)
9935 pop_scope (pushed_scope);
9936
9937 return convert_from_reference (decl);
9938 }
9939 }
9940 /* If we didn't even get past the declarator successfully, we are
9941 definitely not looking at a declaration. */
9942 else
9943 cp_parser_abort_tentative_parse (parser);
9944
9945 /* Otherwise, we are looking at an expression. */
9946 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9947 }
9948
9949 /* Parses a for-statement or range-for-statement until the closing ')',
9950 not included. */
9951
9952 static tree
9953 cp_parser_for (cp_parser *parser, bool ivdep)
9954 {
9955 tree init, scope, decl;
9956 bool is_range_for;
9957
9958 /* Begin the for-statement. */
9959 scope = begin_for_scope (&init);
9960
9961 /* Parse the initialization. */
9962 is_range_for = cp_parser_for_init_statement (parser, &decl);
9963
9964 if (is_range_for)
9965 return cp_parser_range_for (parser, scope, init, decl, ivdep);
9966 else
9967 return cp_parser_c_for (parser, scope, init, ivdep);
9968 }
9969
9970 static tree
9971 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
9972 {
9973 /* Normal for loop */
9974 tree condition = NULL_TREE;
9975 tree expression = NULL_TREE;
9976 tree stmt;
9977
9978 stmt = begin_for_stmt (scope, init);
9979 /* The for-init-statement has already been parsed in
9980 cp_parser_for_init_statement, so no work is needed here. */
9981 finish_for_init_stmt (stmt);
9982
9983 /* If there's a condition, process it. */
9984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9985 condition = cp_parser_condition (parser);
9986 else if (ivdep)
9987 {
9988 cp_parser_error (parser, "missing loop condition in loop with "
9989 "%<GCC ivdep%> pragma");
9990 condition = error_mark_node;
9991 }
9992 finish_for_cond (condition, stmt, ivdep);
9993 /* Look for the `;'. */
9994 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9995
9996 /* If there's an expression, process it. */
9997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9998 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9999 finish_for_expr (expression, stmt);
10000
10001 return stmt;
10002 }
10003
10004 /* Tries to parse a range-based for-statement:
10005
10006 range-based-for:
10007 decl-specifier-seq declarator : expression
10008
10009 The decl-specifier-seq declarator and the `:' are already parsed by
10010 cp_parser_for_init_statement. If processing_template_decl it returns a
10011 newly created RANGE_FOR_STMT; if not, it is converted to a
10012 regular FOR_STMT. */
10013
10014 static tree
10015 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10016 bool ivdep)
10017 {
10018 tree stmt, range_expr;
10019
10020 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10021 {
10022 bool expr_non_constant_p;
10023 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10024 }
10025 else
10026 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10027
10028 /* If in template, STMT is converted to a normal for-statement
10029 at instantiation. If not, it is done just ahead. */
10030 if (processing_template_decl)
10031 {
10032 if (check_for_bare_parameter_packs (range_expr))
10033 range_expr = error_mark_node;
10034 stmt = begin_range_for_stmt (scope, init);
10035 if (ivdep)
10036 RANGE_FOR_IVDEP (stmt) = 1;
10037 finish_range_for_decl (stmt, range_decl, range_expr);
10038 if (!type_dependent_expression_p (range_expr)
10039 /* do_auto_deduction doesn't mess with template init-lists. */
10040 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10041 do_range_for_auto_deduction (range_decl, range_expr);
10042 }
10043 else
10044 {
10045 stmt = begin_for_stmt (scope, init);
10046 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10047 }
10048 return stmt;
10049 }
10050
10051 /* Subroutine of cp_convert_range_for: given the initializer expression,
10052 builds up the range temporary. */
10053
10054 static tree
10055 build_range_temp (tree range_expr)
10056 {
10057 tree range_type, range_temp;
10058
10059 /* Find out the type deduced by the declaration
10060 `auto &&__range = range_expr'. */
10061 range_type = cp_build_reference_type (make_auto (), true);
10062 range_type = do_auto_deduction (range_type, range_expr,
10063 type_uses_auto (range_type));
10064
10065 /* Create the __range variable. */
10066 range_temp = build_decl (input_location, VAR_DECL,
10067 get_identifier ("__for_range"), range_type);
10068 TREE_USED (range_temp) = 1;
10069 DECL_ARTIFICIAL (range_temp) = 1;
10070
10071 return range_temp;
10072 }
10073
10074 /* Used by cp_parser_range_for in template context: we aren't going to
10075 do a full conversion yet, but we still need to resolve auto in the
10076 type of the for-range-declaration if present. This is basically
10077 a shortcut version of cp_convert_range_for. */
10078
10079 static void
10080 do_range_for_auto_deduction (tree decl, tree range_expr)
10081 {
10082 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10083 if (auto_node)
10084 {
10085 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10086 range_temp = convert_from_reference (build_range_temp (range_expr));
10087 iter_type = (cp_parser_perform_range_for_lookup
10088 (range_temp, &begin_dummy, &end_dummy));
10089 if (iter_type)
10090 {
10091 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10092 iter_type);
10093 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10094 tf_warning_or_error);
10095 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10096 iter_decl, auto_node);
10097 }
10098 }
10099 }
10100
10101 /* Converts a range-based for-statement into a normal
10102 for-statement, as per the definition.
10103
10104 for (RANGE_DECL : RANGE_EXPR)
10105 BLOCK
10106
10107 should be equivalent to:
10108
10109 {
10110 auto &&__range = RANGE_EXPR;
10111 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10112 __begin != __end;
10113 ++__begin)
10114 {
10115 RANGE_DECL = *__begin;
10116 BLOCK
10117 }
10118 }
10119
10120 If RANGE_EXPR is an array:
10121 BEGIN_EXPR = __range
10122 END_EXPR = __range + ARRAY_SIZE(__range)
10123 Else if RANGE_EXPR has a member 'begin' or 'end':
10124 BEGIN_EXPR = __range.begin()
10125 END_EXPR = __range.end()
10126 Else:
10127 BEGIN_EXPR = begin(__range)
10128 END_EXPR = end(__range);
10129
10130 If __range has a member 'begin' but not 'end', or vice versa, we must
10131 still use the second alternative (it will surely fail, however).
10132 When calling begin()/end() in the third alternative we must use
10133 argument dependent lookup, but always considering 'std' as an associated
10134 namespace. */
10135
10136 tree
10137 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10138 bool ivdep)
10139 {
10140 tree begin, end;
10141 tree iter_type, begin_expr, end_expr;
10142 tree condition, expression;
10143
10144 if (range_decl == error_mark_node || range_expr == error_mark_node)
10145 /* If an error happened previously do nothing or else a lot of
10146 unhelpful errors would be issued. */
10147 begin_expr = end_expr = iter_type = error_mark_node;
10148 else
10149 {
10150 tree range_temp;
10151
10152 if (TREE_CODE (range_expr) == VAR_DECL
10153 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10154 /* Can't bind a reference to an array of runtime bound. */
10155 range_temp = range_expr;
10156 else
10157 {
10158 range_temp = build_range_temp (range_expr);
10159 pushdecl (range_temp);
10160 cp_finish_decl (range_temp, range_expr,
10161 /*is_constant_init*/false, NULL_TREE,
10162 LOOKUP_ONLYCONVERTING);
10163 range_temp = convert_from_reference (range_temp);
10164 }
10165 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10166 &begin_expr, &end_expr);
10167 }
10168
10169 /* The new for initialization statement. */
10170 begin = build_decl (input_location, VAR_DECL,
10171 get_identifier ("__for_begin"), iter_type);
10172 TREE_USED (begin) = 1;
10173 DECL_ARTIFICIAL (begin) = 1;
10174 pushdecl (begin);
10175 cp_finish_decl (begin, begin_expr,
10176 /*is_constant_init*/false, NULL_TREE,
10177 LOOKUP_ONLYCONVERTING);
10178
10179 end = build_decl (input_location, VAR_DECL,
10180 get_identifier ("__for_end"), iter_type);
10181 TREE_USED (end) = 1;
10182 DECL_ARTIFICIAL (end) = 1;
10183 pushdecl (end);
10184 cp_finish_decl (end, end_expr,
10185 /*is_constant_init*/false, NULL_TREE,
10186 LOOKUP_ONLYCONVERTING);
10187
10188 finish_for_init_stmt (statement);
10189
10190 /* The new for condition. */
10191 condition = build_x_binary_op (input_location, NE_EXPR,
10192 begin, ERROR_MARK,
10193 end, ERROR_MARK,
10194 NULL, tf_warning_or_error);
10195 finish_for_cond (condition, statement, ivdep);
10196
10197 /* The new increment expression. */
10198 expression = finish_unary_op_expr (input_location,
10199 PREINCREMENT_EXPR, begin,
10200 tf_warning_or_error);
10201 finish_for_expr (expression, statement);
10202
10203 /* The declaration is initialized with *__begin inside the loop body. */
10204 cp_finish_decl (range_decl,
10205 build_x_indirect_ref (input_location, begin, RO_NULL,
10206 tf_warning_or_error),
10207 /*is_constant_init*/false, NULL_TREE,
10208 LOOKUP_ONLYCONVERTING);
10209
10210 return statement;
10211 }
10212
10213 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10214 We need to solve both at the same time because the method used
10215 depends on the existence of members begin or end.
10216 Returns the type deduced for the iterator expression. */
10217
10218 static tree
10219 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10220 {
10221 if (error_operand_p (range))
10222 {
10223 *begin = *end = error_mark_node;
10224 return error_mark_node;
10225 }
10226
10227 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10228 {
10229 error ("range-based %<for%> expression of type %qT "
10230 "has incomplete type", TREE_TYPE (range));
10231 *begin = *end = error_mark_node;
10232 return error_mark_node;
10233 }
10234 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10235 {
10236 /* If RANGE is an array, we will use pointer arithmetic. */
10237 *begin = range;
10238 *end = build_binary_op (input_location, PLUS_EXPR,
10239 range,
10240 array_type_nelts_top (TREE_TYPE (range)),
10241 0);
10242 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10243 }
10244 else
10245 {
10246 /* If it is not an array, we must do a bit of magic. */
10247 tree id_begin, id_end;
10248 tree member_begin, member_end;
10249
10250 *begin = *end = error_mark_node;
10251
10252 id_begin = get_identifier ("begin");
10253 id_end = get_identifier ("end");
10254 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10255 /*protect=*/2, /*want_type=*/false,
10256 tf_warning_or_error);
10257 member_end = lookup_member (TREE_TYPE (range), id_end,
10258 /*protect=*/2, /*want_type=*/false,
10259 tf_warning_or_error);
10260
10261 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10262 {
10263 /* Use the member functions. */
10264 if (member_begin != NULL_TREE)
10265 *begin = cp_parser_range_for_member_function (range, id_begin);
10266 else
10267 error ("range-based %<for%> expression of type %qT has an "
10268 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10269
10270 if (member_end != NULL_TREE)
10271 *end = cp_parser_range_for_member_function (range, id_end);
10272 else
10273 error ("range-based %<for%> expression of type %qT has a "
10274 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10275 }
10276 else
10277 {
10278 /* Use global functions with ADL. */
10279 vec<tree, va_gc> *vec;
10280 vec = make_tree_vector ();
10281
10282 vec_safe_push (vec, range);
10283
10284 member_begin = perform_koenig_lookup (id_begin, vec,
10285 /*include_std=*/true,
10286 tf_warning_or_error);
10287 *begin = finish_call_expr (member_begin, &vec, false, true,
10288 tf_warning_or_error);
10289 member_end = perform_koenig_lookup (id_end, vec,
10290 /*include_std=*/true,
10291 tf_warning_or_error);
10292 *end = finish_call_expr (member_end, &vec, false, true,
10293 tf_warning_or_error);
10294
10295 release_tree_vector (vec);
10296 }
10297
10298 /* Last common checks. */
10299 if (*begin == error_mark_node || *end == error_mark_node)
10300 {
10301 /* If one of the expressions is an error do no more checks. */
10302 *begin = *end = error_mark_node;
10303 return error_mark_node;
10304 }
10305 else if (type_dependent_expression_p (*begin)
10306 || type_dependent_expression_p (*end))
10307 /* Can happen, when, eg, in a template context, Koenig lookup
10308 can't resolve begin/end (c++/58503). */
10309 return NULL_TREE;
10310 else
10311 {
10312 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10313 /* The unqualified type of the __begin and __end temporaries should
10314 be the same, as required by the multiple auto declaration. */
10315 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10316 error ("inconsistent begin/end types in range-based %<for%> "
10317 "statement: %qT and %qT",
10318 TREE_TYPE (*begin), TREE_TYPE (*end));
10319 return iter_type;
10320 }
10321 }
10322 }
10323
10324 /* Helper function for cp_parser_perform_range_for_lookup.
10325 Builds a tree for RANGE.IDENTIFIER(). */
10326
10327 static tree
10328 cp_parser_range_for_member_function (tree range, tree identifier)
10329 {
10330 tree member, res;
10331 vec<tree, va_gc> *vec;
10332
10333 member = finish_class_member_access_expr (range, identifier,
10334 false, tf_warning_or_error);
10335 if (member == error_mark_node)
10336 return error_mark_node;
10337
10338 vec = make_tree_vector ();
10339 res = finish_call_expr (member, &vec,
10340 /*disallow_virtual=*/false,
10341 /*koenig_p=*/false,
10342 tf_warning_or_error);
10343 release_tree_vector (vec);
10344 return res;
10345 }
10346
10347 /* Parse an iteration-statement.
10348
10349 iteration-statement:
10350 while ( condition ) statement
10351 do statement while ( expression ) ;
10352 for ( for-init-statement condition [opt] ; expression [opt] )
10353 statement
10354
10355 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10356
10357 static tree
10358 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10359 {
10360 cp_token *token;
10361 enum rid keyword;
10362 tree statement;
10363 unsigned char in_statement;
10364
10365 /* Peek at the next token. */
10366 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10367 if (!token)
10368 return error_mark_node;
10369
10370 /* Remember whether or not we are already within an iteration
10371 statement. */
10372 in_statement = parser->in_statement;
10373
10374 /* See what kind of keyword it is. */
10375 keyword = token->keyword;
10376 switch (keyword)
10377 {
10378 case RID_WHILE:
10379 {
10380 tree condition;
10381
10382 /* Begin the while-statement. */
10383 statement = begin_while_stmt ();
10384 /* Look for the `('. */
10385 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10386 /* Parse the condition. */
10387 condition = cp_parser_condition (parser);
10388 finish_while_stmt_cond (condition, statement, ivdep);
10389 /* Look for the `)'. */
10390 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10391 /* Parse the dependent statement. */
10392 parser->in_statement = IN_ITERATION_STMT;
10393 cp_parser_already_scoped_statement (parser);
10394 parser->in_statement = in_statement;
10395 /* We're done with the while-statement. */
10396 finish_while_stmt (statement);
10397 }
10398 break;
10399
10400 case RID_DO:
10401 {
10402 tree expression;
10403
10404 /* Begin the do-statement. */
10405 statement = begin_do_stmt ();
10406 /* Parse the body of the do-statement. */
10407 parser->in_statement = IN_ITERATION_STMT;
10408 cp_parser_implicitly_scoped_statement (parser, NULL);
10409 parser->in_statement = in_statement;
10410 finish_do_body (statement);
10411 /* Look for the `while' keyword. */
10412 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10413 /* Look for the `('. */
10414 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10415 /* Parse the expression. */
10416 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10417 /* We're done with the do-statement. */
10418 finish_do_stmt (expression, statement, ivdep);
10419 /* Look for the `)'. */
10420 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10421 /* Look for the `;'. */
10422 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10423 }
10424 break;
10425
10426 case RID_FOR:
10427 {
10428 /* Look for the `('. */
10429 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10430
10431 statement = cp_parser_for (parser, ivdep);
10432
10433 /* Look for the `)'. */
10434 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10435
10436 /* Parse the body of the for-statement. */
10437 parser->in_statement = IN_ITERATION_STMT;
10438 cp_parser_already_scoped_statement (parser);
10439 parser->in_statement = in_statement;
10440
10441 /* We're done with the for-statement. */
10442 finish_for_stmt (statement);
10443 }
10444 break;
10445
10446 default:
10447 cp_parser_error (parser, "expected iteration-statement");
10448 statement = error_mark_node;
10449 break;
10450 }
10451
10452 return statement;
10453 }
10454
10455 /* Parse a for-init-statement or the declarator of a range-based-for.
10456 Returns true if a range-based-for declaration is seen.
10457
10458 for-init-statement:
10459 expression-statement
10460 simple-declaration */
10461
10462 static bool
10463 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10464 {
10465 /* If the next token is a `;', then we have an empty
10466 expression-statement. Grammatically, this is also a
10467 simple-declaration, but an invalid one, because it does not
10468 declare anything. Therefore, if we did not handle this case
10469 specially, we would issue an error message about an invalid
10470 declaration. */
10471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10472 {
10473 bool is_range_for = false;
10474 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10475
10476 parser->colon_corrects_to_scope_p = false;
10477
10478 /* We're going to speculatively look for a declaration, falling back
10479 to an expression, if necessary. */
10480 cp_parser_parse_tentatively (parser);
10481 /* Parse the declaration. */
10482 cp_parser_simple_declaration (parser,
10483 /*function_definition_allowed_p=*/false,
10484 decl);
10485 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10486 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10487 {
10488 /* It is a range-for, consume the ':' */
10489 cp_lexer_consume_token (parser->lexer);
10490 is_range_for = true;
10491 if (cxx_dialect < cxx11)
10492 {
10493 error_at (cp_lexer_peek_token (parser->lexer)->location,
10494 "range-based %<for%> loops are not allowed "
10495 "in C++98 mode");
10496 *decl = error_mark_node;
10497 }
10498 }
10499 else
10500 /* The ';' is not consumed yet because we told
10501 cp_parser_simple_declaration not to. */
10502 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10503
10504 if (cp_parser_parse_definitely (parser))
10505 return is_range_for;
10506 /* If the tentative parse failed, then we shall need to look for an
10507 expression-statement. */
10508 }
10509 /* If we are here, it is an expression-statement. */
10510 cp_parser_expression_statement (parser, NULL_TREE);
10511 return false;
10512 }
10513
10514 /* Parse a jump-statement.
10515
10516 jump-statement:
10517 break ;
10518 continue ;
10519 return expression [opt] ;
10520 return braced-init-list ;
10521 goto identifier ;
10522
10523 GNU extension:
10524
10525 jump-statement:
10526 goto * expression ;
10527
10528 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10529
10530 static tree
10531 cp_parser_jump_statement (cp_parser* parser)
10532 {
10533 tree statement = error_mark_node;
10534 cp_token *token;
10535 enum rid keyword;
10536 unsigned char in_statement;
10537
10538 /* Peek at the next token. */
10539 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10540 if (!token)
10541 return error_mark_node;
10542
10543 /* See what kind of keyword it is. */
10544 keyword = token->keyword;
10545 switch (keyword)
10546 {
10547 case RID_BREAK:
10548 in_statement = parser->in_statement & ~IN_IF_STMT;
10549 switch (in_statement)
10550 {
10551 case 0:
10552 error_at (token->location, "break statement not within loop or switch");
10553 break;
10554 default:
10555 gcc_assert ((in_statement & IN_SWITCH_STMT)
10556 || in_statement == IN_ITERATION_STMT);
10557 statement = finish_break_stmt ();
10558 break;
10559 case IN_OMP_BLOCK:
10560 error_at (token->location, "invalid exit from OpenMP structured block");
10561 break;
10562 case IN_OMP_FOR:
10563 error_at (token->location, "break statement used with OpenMP for loop");
10564 break;
10565 case IN_CILK_SIMD_FOR:
10566 error_at (token->location, "break statement used with Cilk Plus for loop");
10567 break;
10568 }
10569 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10570 break;
10571
10572 case RID_CONTINUE:
10573 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10574 {
10575 case 0:
10576 error_at (token->location, "continue statement not within a loop");
10577 break;
10578 case IN_CILK_SIMD_FOR:
10579 error_at (token->location,
10580 "continue statement within %<#pragma simd%> loop body");
10581 /* Fall through. */
10582 case IN_ITERATION_STMT:
10583 case IN_OMP_FOR:
10584 statement = finish_continue_stmt ();
10585 break;
10586 case IN_OMP_BLOCK:
10587 error_at (token->location, "invalid exit from OpenMP structured block");
10588 break;
10589 default:
10590 gcc_unreachable ();
10591 }
10592 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10593 break;
10594
10595 case RID_RETURN:
10596 {
10597 tree expr;
10598 bool expr_non_constant_p;
10599
10600 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10601 {
10602 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10603 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10604 }
10605 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10606 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10607 else
10608 /* If the next token is a `;', then there is no
10609 expression. */
10610 expr = NULL_TREE;
10611 /* Build the return-statement. */
10612 statement = finish_return_stmt (expr);
10613 /* Look for the final `;'. */
10614 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10615 }
10616 break;
10617
10618 case RID_GOTO:
10619 /* Create the goto-statement. */
10620 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10621 {
10622 /* Issue a warning about this use of a GNU extension. */
10623 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10624 /* Consume the '*' token. */
10625 cp_lexer_consume_token (parser->lexer);
10626 /* Parse the dependent expression. */
10627 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10628 }
10629 else
10630 finish_goto_stmt (cp_parser_identifier (parser));
10631 /* Look for the final `;'. */
10632 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10633 break;
10634
10635 default:
10636 cp_parser_error (parser, "expected jump-statement");
10637 break;
10638 }
10639
10640 return statement;
10641 }
10642
10643 /* Parse a declaration-statement.
10644
10645 declaration-statement:
10646 block-declaration */
10647
10648 static void
10649 cp_parser_declaration_statement (cp_parser* parser)
10650 {
10651 void *p;
10652
10653 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10654 p = obstack_alloc (&declarator_obstack, 0);
10655
10656 /* Parse the block-declaration. */
10657 cp_parser_block_declaration (parser, /*statement_p=*/true);
10658
10659 /* Free any declarators allocated. */
10660 obstack_free (&declarator_obstack, p);
10661 }
10662
10663 /* Some dependent statements (like `if (cond) statement'), are
10664 implicitly in their own scope. In other words, if the statement is
10665 a single statement (as opposed to a compound-statement), it is
10666 none-the-less treated as if it were enclosed in braces. Any
10667 declarations appearing in the dependent statement are out of scope
10668 after control passes that point. This function parses a statement,
10669 but ensures that is in its own scope, even if it is not a
10670 compound-statement.
10671
10672 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10673 is a (possibly labeled) if statement which is not enclosed in
10674 braces and has an else clause. This is used to implement
10675 -Wparentheses.
10676
10677 Returns the new statement. */
10678
10679 static tree
10680 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10681 {
10682 tree statement;
10683
10684 if (if_p != NULL)
10685 *if_p = false;
10686
10687 /* Mark if () ; with a special NOP_EXPR. */
10688 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10689 {
10690 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10691 cp_lexer_consume_token (parser->lexer);
10692 statement = add_stmt (build_empty_stmt (loc));
10693 }
10694 /* if a compound is opened, we simply parse the statement directly. */
10695 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10696 statement = cp_parser_compound_statement (parser, NULL, false, false);
10697 /* If the token is not a `{', then we must take special action. */
10698 else
10699 {
10700 /* Create a compound-statement. */
10701 statement = begin_compound_stmt (0);
10702 /* Parse the dependent-statement. */
10703 cp_parser_statement (parser, NULL_TREE, false, if_p);
10704 /* Finish the dummy compound-statement. */
10705 finish_compound_stmt (statement);
10706 }
10707
10708 /* Return the statement. */
10709 return statement;
10710 }
10711
10712 /* For some dependent statements (like `while (cond) statement'), we
10713 have already created a scope. Therefore, even if the dependent
10714 statement is a compound-statement, we do not want to create another
10715 scope. */
10716
10717 static void
10718 cp_parser_already_scoped_statement (cp_parser* parser)
10719 {
10720 /* If the token is a `{', then we must take special action. */
10721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10722 cp_parser_statement (parser, NULL_TREE, false, NULL);
10723 else
10724 {
10725 /* Avoid calling cp_parser_compound_statement, so that we
10726 don't create a new scope. Do everything else by hand. */
10727 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10728 /* If the next keyword is `__label__' we have a label declaration. */
10729 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10730 cp_parser_label_declaration (parser);
10731 /* Parse an (optional) statement-seq. */
10732 cp_parser_statement_seq_opt (parser, NULL_TREE);
10733 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10734 }
10735 }
10736
10737 /* Declarations [gram.dcl.dcl] */
10738
10739 /* Parse an optional declaration-sequence.
10740
10741 declaration-seq:
10742 declaration
10743 declaration-seq declaration */
10744
10745 static void
10746 cp_parser_declaration_seq_opt (cp_parser* parser)
10747 {
10748 while (true)
10749 {
10750 cp_token *token;
10751
10752 token = cp_lexer_peek_token (parser->lexer);
10753
10754 if (token->type == CPP_CLOSE_BRACE
10755 || token->type == CPP_EOF
10756 || token->type == CPP_PRAGMA_EOL)
10757 break;
10758
10759 if (token->type == CPP_SEMICOLON)
10760 {
10761 /* A declaration consisting of a single semicolon is
10762 invalid. Allow it unless we're being pedantic. */
10763 cp_lexer_consume_token (parser->lexer);
10764 if (!in_system_header)
10765 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10766 continue;
10767 }
10768
10769 /* If we're entering or exiting a region that's implicitly
10770 extern "C", modify the lang context appropriately. */
10771 if (!parser->implicit_extern_c && token->implicit_extern_c)
10772 {
10773 push_lang_context (lang_name_c);
10774 parser->implicit_extern_c = true;
10775 }
10776 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10777 {
10778 pop_lang_context ();
10779 parser->implicit_extern_c = false;
10780 }
10781
10782 if (token->type == CPP_PRAGMA)
10783 {
10784 /* A top-level declaration can consist solely of a #pragma.
10785 A nested declaration cannot, so this is done here and not
10786 in cp_parser_declaration. (A #pragma at block scope is
10787 handled in cp_parser_statement.) */
10788 cp_parser_pragma (parser, pragma_external);
10789 continue;
10790 }
10791
10792 /* Parse the declaration itself. */
10793 cp_parser_declaration (parser);
10794 }
10795 }
10796
10797 /* Parse a declaration.
10798
10799 declaration:
10800 block-declaration
10801 function-definition
10802 template-declaration
10803 explicit-instantiation
10804 explicit-specialization
10805 linkage-specification
10806 namespace-definition
10807
10808 GNU extension:
10809
10810 declaration:
10811 __extension__ declaration */
10812
10813 static void
10814 cp_parser_declaration (cp_parser* parser)
10815 {
10816 cp_token token1;
10817 cp_token token2;
10818 int saved_pedantic;
10819 void *p;
10820 tree attributes = NULL_TREE;
10821
10822 /* Check for the `__extension__' keyword. */
10823 if (cp_parser_extension_opt (parser, &saved_pedantic))
10824 {
10825 /* Parse the qualified declaration. */
10826 cp_parser_declaration (parser);
10827 /* Restore the PEDANTIC flag. */
10828 pedantic = saved_pedantic;
10829
10830 return;
10831 }
10832
10833 /* Try to figure out what kind of declaration is present. */
10834 token1 = *cp_lexer_peek_token (parser->lexer);
10835
10836 if (token1.type != CPP_EOF)
10837 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10838 else
10839 {
10840 token2.type = CPP_EOF;
10841 token2.keyword = RID_MAX;
10842 }
10843
10844 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10845 p = obstack_alloc (&declarator_obstack, 0);
10846
10847 /* If the next token is `extern' and the following token is a string
10848 literal, then we have a linkage specification. */
10849 if (token1.keyword == RID_EXTERN
10850 && cp_parser_is_pure_string_literal (&token2))
10851 cp_parser_linkage_specification (parser);
10852 /* If the next token is `template', then we have either a template
10853 declaration, an explicit instantiation, or an explicit
10854 specialization. */
10855 else if (token1.keyword == RID_TEMPLATE)
10856 {
10857 /* `template <>' indicates a template specialization. */
10858 if (token2.type == CPP_LESS
10859 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10860 cp_parser_explicit_specialization (parser);
10861 /* `template <' indicates a template declaration. */
10862 else if (token2.type == CPP_LESS)
10863 cp_parser_template_declaration (parser, /*member_p=*/false);
10864 /* Anything else must be an explicit instantiation. */
10865 else
10866 cp_parser_explicit_instantiation (parser);
10867 }
10868 /* If the next token is `export', then we have a template
10869 declaration. */
10870 else if (token1.keyword == RID_EXPORT)
10871 cp_parser_template_declaration (parser, /*member_p=*/false);
10872 /* If the next token is `extern', 'static' or 'inline' and the one
10873 after that is `template', we have a GNU extended explicit
10874 instantiation directive. */
10875 else if (cp_parser_allow_gnu_extensions_p (parser)
10876 && (token1.keyword == RID_EXTERN
10877 || token1.keyword == RID_STATIC
10878 || token1.keyword == RID_INLINE)
10879 && token2.keyword == RID_TEMPLATE)
10880 cp_parser_explicit_instantiation (parser);
10881 /* If the next token is `namespace', check for a named or unnamed
10882 namespace definition. */
10883 else if (token1.keyword == RID_NAMESPACE
10884 && (/* A named namespace definition. */
10885 (token2.type == CPP_NAME
10886 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10887 != CPP_EQ))
10888 /* An unnamed namespace definition. */
10889 || token2.type == CPP_OPEN_BRACE
10890 || token2.keyword == RID_ATTRIBUTE))
10891 cp_parser_namespace_definition (parser);
10892 /* An inline (associated) namespace definition. */
10893 else if (token1.keyword == RID_INLINE
10894 && token2.keyword == RID_NAMESPACE)
10895 cp_parser_namespace_definition (parser);
10896 /* Objective-C++ declaration/definition. */
10897 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10898 cp_parser_objc_declaration (parser, NULL_TREE);
10899 else if (c_dialect_objc ()
10900 && token1.keyword == RID_ATTRIBUTE
10901 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10902 cp_parser_objc_declaration (parser, attributes);
10903 /* We must have either a block declaration or a function
10904 definition. */
10905 else
10906 /* Try to parse a block-declaration, or a function-definition. */
10907 cp_parser_block_declaration (parser, /*statement_p=*/false);
10908
10909 /* Free any declarators allocated. */
10910 obstack_free (&declarator_obstack, p);
10911 }
10912
10913 /* Parse a block-declaration.
10914
10915 block-declaration:
10916 simple-declaration
10917 asm-definition
10918 namespace-alias-definition
10919 using-declaration
10920 using-directive
10921
10922 GNU Extension:
10923
10924 block-declaration:
10925 __extension__ block-declaration
10926
10927 C++0x Extension:
10928
10929 block-declaration:
10930 static_assert-declaration
10931
10932 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10933 part of a declaration-statement. */
10934
10935 static void
10936 cp_parser_block_declaration (cp_parser *parser,
10937 bool statement_p)
10938 {
10939 cp_token *token1;
10940 int saved_pedantic;
10941
10942 /* Check for the `__extension__' keyword. */
10943 if (cp_parser_extension_opt (parser, &saved_pedantic))
10944 {
10945 /* Parse the qualified declaration. */
10946 cp_parser_block_declaration (parser, statement_p);
10947 /* Restore the PEDANTIC flag. */
10948 pedantic = saved_pedantic;
10949
10950 return;
10951 }
10952
10953 /* Peek at the next token to figure out which kind of declaration is
10954 present. */
10955 token1 = cp_lexer_peek_token (parser->lexer);
10956
10957 /* If the next keyword is `asm', we have an asm-definition. */
10958 if (token1->keyword == RID_ASM)
10959 {
10960 if (statement_p)
10961 cp_parser_commit_to_tentative_parse (parser);
10962 cp_parser_asm_definition (parser);
10963 }
10964 /* If the next keyword is `namespace', we have a
10965 namespace-alias-definition. */
10966 else if (token1->keyword == RID_NAMESPACE)
10967 cp_parser_namespace_alias_definition (parser);
10968 /* If the next keyword is `using', we have a
10969 using-declaration, a using-directive, or an alias-declaration. */
10970 else if (token1->keyword == RID_USING)
10971 {
10972 cp_token *token2;
10973
10974 if (statement_p)
10975 cp_parser_commit_to_tentative_parse (parser);
10976 /* If the token after `using' is `namespace', then we have a
10977 using-directive. */
10978 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10979 if (token2->keyword == RID_NAMESPACE)
10980 cp_parser_using_directive (parser);
10981 /* If the second token after 'using' is '=', then we have an
10982 alias-declaration. */
10983 else if (cxx_dialect >= cxx11
10984 && token2->type == CPP_NAME
10985 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10986 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10987 cp_parser_alias_declaration (parser);
10988 /* Otherwise, it's a using-declaration. */
10989 else
10990 cp_parser_using_declaration (parser,
10991 /*access_declaration_p=*/false);
10992 }
10993 /* If the next keyword is `__label__' we have a misplaced label
10994 declaration. */
10995 else if (token1->keyword == RID_LABEL)
10996 {
10997 cp_lexer_consume_token (parser->lexer);
10998 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10999 cp_parser_skip_to_end_of_statement (parser);
11000 /* If the next token is now a `;', consume it. */
11001 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11002 cp_lexer_consume_token (parser->lexer);
11003 }
11004 /* If the next token is `static_assert' we have a static assertion. */
11005 else if (token1->keyword == RID_STATIC_ASSERT)
11006 cp_parser_static_assert (parser, /*member_p=*/false);
11007 /* Anything else must be a simple-declaration. */
11008 else
11009 cp_parser_simple_declaration (parser, !statement_p,
11010 /*maybe_range_for_decl*/NULL);
11011 }
11012
11013 /* Parse a simple-declaration.
11014
11015 simple-declaration:
11016 decl-specifier-seq [opt] init-declarator-list [opt] ;
11017
11018 init-declarator-list:
11019 init-declarator
11020 init-declarator-list , init-declarator
11021
11022 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11023 function-definition as a simple-declaration.
11024
11025 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11026 parsed declaration if it is an uninitialized single declarator not followed
11027 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11028 if present, will not be consumed. */
11029
11030 static void
11031 cp_parser_simple_declaration (cp_parser* parser,
11032 bool function_definition_allowed_p,
11033 tree *maybe_range_for_decl)
11034 {
11035 cp_decl_specifier_seq decl_specifiers;
11036 int declares_class_or_enum;
11037 bool saw_declarator;
11038
11039 if (maybe_range_for_decl)
11040 *maybe_range_for_decl = NULL_TREE;
11041
11042 /* Defer access checks until we know what is being declared; the
11043 checks for names appearing in the decl-specifier-seq should be
11044 done as if we were in the scope of the thing being declared. */
11045 push_deferring_access_checks (dk_deferred);
11046
11047 /* Parse the decl-specifier-seq. We have to keep track of whether
11048 or not the decl-specifier-seq declares a named class or
11049 enumeration type, since that is the only case in which the
11050 init-declarator-list is allowed to be empty.
11051
11052 [dcl.dcl]
11053
11054 In a simple-declaration, the optional init-declarator-list can be
11055 omitted only when declaring a class or enumeration, that is when
11056 the decl-specifier-seq contains either a class-specifier, an
11057 elaborated-type-specifier, or an enum-specifier. */
11058 cp_parser_decl_specifier_seq (parser,
11059 CP_PARSER_FLAGS_OPTIONAL,
11060 &decl_specifiers,
11061 &declares_class_or_enum);
11062 /* We no longer need to defer access checks. */
11063 stop_deferring_access_checks ();
11064
11065 /* In a block scope, a valid declaration must always have a
11066 decl-specifier-seq. By not trying to parse declarators, we can
11067 resolve the declaration/expression ambiguity more quickly. */
11068 if (!function_definition_allowed_p
11069 && !decl_specifiers.any_specifiers_p)
11070 {
11071 cp_parser_error (parser, "expected declaration");
11072 goto done;
11073 }
11074
11075 /* If the next two tokens are both identifiers, the code is
11076 erroneous. The usual cause of this situation is code like:
11077
11078 T t;
11079
11080 where "T" should name a type -- but does not. */
11081 if (!decl_specifiers.any_type_specifiers_p
11082 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11083 {
11084 /* If parsing tentatively, we should commit; we really are
11085 looking at a declaration. */
11086 cp_parser_commit_to_tentative_parse (parser);
11087 /* Give up. */
11088 goto done;
11089 }
11090
11091 /* If we have seen at least one decl-specifier, and the next token
11092 is not a parenthesis, then we must be looking at a declaration.
11093 (After "int (" we might be looking at a functional cast.) */
11094 if (decl_specifiers.any_specifiers_p
11095 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11096 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11097 && !cp_parser_error_occurred (parser))
11098 cp_parser_commit_to_tentative_parse (parser);
11099
11100 /* Keep going until we hit the `;' at the end of the simple
11101 declaration. */
11102 saw_declarator = false;
11103 while (cp_lexer_next_token_is_not (parser->lexer,
11104 CPP_SEMICOLON))
11105 {
11106 cp_token *token;
11107 bool function_definition_p;
11108 tree decl;
11109
11110 if (saw_declarator)
11111 {
11112 /* If we are processing next declarator, coma is expected */
11113 token = cp_lexer_peek_token (parser->lexer);
11114 gcc_assert (token->type == CPP_COMMA);
11115 cp_lexer_consume_token (parser->lexer);
11116 if (maybe_range_for_decl)
11117 *maybe_range_for_decl = error_mark_node;
11118 }
11119 else
11120 saw_declarator = true;
11121
11122 /* Parse the init-declarator. */
11123 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11124 /*checks=*/NULL,
11125 function_definition_allowed_p,
11126 /*member_p=*/false,
11127 declares_class_or_enum,
11128 &function_definition_p,
11129 maybe_range_for_decl);
11130 /* If an error occurred while parsing tentatively, exit quickly.
11131 (That usually happens when in the body of a function; each
11132 statement is treated as a declaration-statement until proven
11133 otherwise.) */
11134 if (cp_parser_error_occurred (parser))
11135 goto done;
11136 /* Handle function definitions specially. */
11137 if (function_definition_p)
11138 {
11139 /* If the next token is a `,', then we are probably
11140 processing something like:
11141
11142 void f() {}, *p;
11143
11144 which is erroneous. */
11145 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11146 {
11147 cp_token *token = cp_lexer_peek_token (parser->lexer);
11148 error_at (token->location,
11149 "mixing"
11150 " declarations and function-definitions is forbidden");
11151 }
11152 /* Otherwise, we're done with the list of declarators. */
11153 else
11154 {
11155 pop_deferring_access_checks ();
11156 return;
11157 }
11158 }
11159 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11160 *maybe_range_for_decl = decl;
11161 /* The next token should be either a `,' or a `;'. */
11162 token = cp_lexer_peek_token (parser->lexer);
11163 /* If it's a `,', there are more declarators to come. */
11164 if (token->type == CPP_COMMA)
11165 /* will be consumed next time around */;
11166 /* If it's a `;', we are done. */
11167 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11168 break;
11169 /* Anything else is an error. */
11170 else
11171 {
11172 /* If we have already issued an error message we don't need
11173 to issue another one. */
11174 if (decl != error_mark_node
11175 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11176 cp_parser_error (parser, "expected %<,%> or %<;%>");
11177 /* Skip tokens until we reach the end of the statement. */
11178 cp_parser_skip_to_end_of_statement (parser);
11179 /* If the next token is now a `;', consume it. */
11180 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11181 cp_lexer_consume_token (parser->lexer);
11182 goto done;
11183 }
11184 /* After the first time around, a function-definition is not
11185 allowed -- even if it was OK at first. For example:
11186
11187 int i, f() {}
11188
11189 is not valid. */
11190 function_definition_allowed_p = false;
11191 }
11192
11193 /* Issue an error message if no declarators are present, and the
11194 decl-specifier-seq does not itself declare a class or
11195 enumeration: [dcl.dcl]/3. */
11196 if (!saw_declarator)
11197 {
11198 if (cp_parser_declares_only_class_p (parser))
11199 {
11200 if (!declares_class_or_enum
11201 && decl_specifiers.type
11202 && OVERLOAD_TYPE_P (decl_specifiers.type))
11203 /* Ensure an error is issued anyway when finish_decltype_type,
11204 called via cp_parser_decl_specifier_seq, returns a class or
11205 an enumeration (c++/51786). */
11206 decl_specifiers.type = NULL_TREE;
11207 shadow_tag (&decl_specifiers);
11208 }
11209 /* Perform any deferred access checks. */
11210 perform_deferred_access_checks (tf_warning_or_error);
11211 }
11212
11213 /* Consume the `;'. */
11214 if (!maybe_range_for_decl)
11215 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11216
11217 done:
11218 pop_deferring_access_checks ();
11219 }
11220
11221 /* Parse a decl-specifier-seq.
11222
11223 decl-specifier-seq:
11224 decl-specifier-seq [opt] decl-specifier
11225 decl-specifier attribute-specifier-seq [opt] (C++11)
11226
11227 decl-specifier:
11228 storage-class-specifier
11229 type-specifier
11230 function-specifier
11231 friend
11232 typedef
11233
11234 GNU Extension:
11235
11236 decl-specifier:
11237 attributes
11238
11239 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11240
11241 The parser flags FLAGS is used to control type-specifier parsing.
11242
11243 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11244 flags:
11245
11246 1: one of the decl-specifiers is an elaborated-type-specifier
11247 (i.e., a type declaration)
11248 2: one of the decl-specifiers is an enum-specifier or a
11249 class-specifier (i.e., a type definition)
11250
11251 */
11252
11253 static void
11254 cp_parser_decl_specifier_seq (cp_parser* parser,
11255 cp_parser_flags flags,
11256 cp_decl_specifier_seq *decl_specs,
11257 int* declares_class_or_enum)
11258 {
11259 bool constructor_possible_p = !parser->in_declarator_p;
11260 bool found_decl_spec = false;
11261 cp_token *start_token = NULL;
11262 cp_decl_spec ds;
11263
11264 /* Clear DECL_SPECS. */
11265 clear_decl_specs (decl_specs);
11266
11267 /* Assume no class or enumeration type is declared. */
11268 *declares_class_or_enum = 0;
11269
11270 /* Keep reading specifiers until there are no more to read. */
11271 while (true)
11272 {
11273 bool constructor_p;
11274 cp_token *token;
11275 ds = ds_last;
11276
11277 /* Peek at the next token. */
11278 token = cp_lexer_peek_token (parser->lexer);
11279
11280 /* Save the first token of the decl spec list for error
11281 reporting. */
11282 if (!start_token)
11283 start_token = token;
11284 /* Handle attributes. */
11285 if (cp_next_tokens_can_be_attribute_p (parser))
11286 {
11287 /* Parse the attributes. */
11288 tree attrs = cp_parser_attributes_opt (parser);
11289
11290 /* In a sequence of declaration specifiers, c++11 attributes
11291 appertain to the type that precede them. In that case
11292 [dcl.spec]/1 says:
11293
11294 The attribute-specifier-seq affects the type only for
11295 the declaration it appears in, not other declarations
11296 involving the same type.
11297
11298 But for now let's force the user to position the
11299 attribute either at the beginning of the declaration or
11300 after the declarator-id, which would clearly mean that it
11301 applies to the declarator. */
11302 if (cxx11_attribute_p (attrs))
11303 {
11304 if (!found_decl_spec)
11305 /* The c++11 attribute is at the beginning of the
11306 declaration. It appertains to the entity being
11307 declared. */;
11308 else
11309 {
11310 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11311 {
11312 /* This is an attribute following a
11313 class-specifier. */
11314 if (decl_specs->type_definition_p)
11315 warn_misplaced_attr_for_class_type (token->location,
11316 decl_specs->type);
11317 attrs = NULL_TREE;
11318 }
11319 else
11320 {
11321 decl_specs->std_attributes
11322 = chainon (decl_specs->std_attributes,
11323 attrs);
11324 if (decl_specs->locations[ds_std_attribute] == 0)
11325 decl_specs->locations[ds_std_attribute] = token->location;
11326 }
11327 continue;
11328 }
11329 }
11330
11331 decl_specs->attributes
11332 = chainon (decl_specs->attributes,
11333 attrs);
11334 if (decl_specs->locations[ds_attribute] == 0)
11335 decl_specs->locations[ds_attribute] = token->location;
11336 continue;
11337 }
11338 /* Assume we will find a decl-specifier keyword. */
11339 found_decl_spec = true;
11340 /* If the next token is an appropriate keyword, we can simply
11341 add it to the list. */
11342 switch (token->keyword)
11343 {
11344 /* decl-specifier:
11345 friend
11346 constexpr */
11347 case RID_FRIEND:
11348 if (!at_class_scope_p ())
11349 {
11350 error_at (token->location, "%<friend%> used outside of class");
11351 cp_lexer_purge_token (parser->lexer);
11352 }
11353 else
11354 {
11355 ds = ds_friend;
11356 /* Consume the token. */
11357 cp_lexer_consume_token (parser->lexer);
11358 }
11359 break;
11360
11361 case RID_CONSTEXPR:
11362 ds = ds_constexpr;
11363 cp_lexer_consume_token (parser->lexer);
11364 break;
11365
11366 /* function-specifier:
11367 inline
11368 virtual
11369 explicit */
11370 case RID_INLINE:
11371 case RID_VIRTUAL:
11372 case RID_EXPLICIT:
11373 cp_parser_function_specifier_opt (parser, decl_specs);
11374 break;
11375
11376 /* decl-specifier:
11377 typedef */
11378 case RID_TYPEDEF:
11379 ds = ds_typedef;
11380 /* Consume the token. */
11381 cp_lexer_consume_token (parser->lexer);
11382 /* A constructor declarator cannot appear in a typedef. */
11383 constructor_possible_p = false;
11384 /* The "typedef" keyword can only occur in a declaration; we
11385 may as well commit at this point. */
11386 cp_parser_commit_to_tentative_parse (parser);
11387
11388 if (decl_specs->storage_class != sc_none)
11389 decl_specs->conflicting_specifiers_p = true;
11390 break;
11391
11392 /* storage-class-specifier:
11393 auto
11394 register
11395 static
11396 extern
11397 mutable
11398
11399 GNU Extension:
11400 thread */
11401 case RID_AUTO:
11402 if (cxx_dialect == cxx98)
11403 {
11404 /* Consume the token. */
11405 cp_lexer_consume_token (parser->lexer);
11406
11407 /* Complain about `auto' as a storage specifier, if
11408 we're complaining about C++0x compatibility. */
11409 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11410 " changes meaning in C++11; please remove it");
11411
11412 /* Set the storage class anyway. */
11413 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11414 token);
11415 }
11416 else
11417 /* C++0x auto type-specifier. */
11418 found_decl_spec = false;
11419 break;
11420
11421 case RID_REGISTER:
11422 case RID_STATIC:
11423 case RID_EXTERN:
11424 case RID_MUTABLE:
11425 /* Consume the token. */
11426 cp_lexer_consume_token (parser->lexer);
11427 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11428 token);
11429 break;
11430 case RID_THREAD:
11431 /* Consume the token. */
11432 ds = ds_thread;
11433 cp_lexer_consume_token (parser->lexer);
11434 break;
11435
11436 default:
11437 /* We did not yet find a decl-specifier yet. */
11438 found_decl_spec = false;
11439 break;
11440 }
11441
11442 if (found_decl_spec
11443 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11444 && token->keyword != RID_CONSTEXPR)
11445 error ("decl-specifier invalid in condition");
11446
11447 if (ds != ds_last)
11448 set_and_check_decl_spec_loc (decl_specs, ds, token);
11449
11450 /* Constructors are a special case. The `S' in `S()' is not a
11451 decl-specifier; it is the beginning of the declarator. */
11452 constructor_p
11453 = (!found_decl_spec
11454 && constructor_possible_p
11455 && (cp_parser_constructor_declarator_p
11456 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11457
11458 /* If we don't have a DECL_SPEC yet, then we must be looking at
11459 a type-specifier. */
11460 if (!found_decl_spec && !constructor_p)
11461 {
11462 int decl_spec_declares_class_or_enum;
11463 bool is_cv_qualifier;
11464 tree type_spec;
11465
11466 type_spec
11467 = cp_parser_type_specifier (parser, flags,
11468 decl_specs,
11469 /*is_declaration=*/true,
11470 &decl_spec_declares_class_or_enum,
11471 &is_cv_qualifier);
11472 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11473
11474 /* If this type-specifier referenced a user-defined type
11475 (a typedef, class-name, etc.), then we can't allow any
11476 more such type-specifiers henceforth.
11477
11478 [dcl.spec]
11479
11480 The longest sequence of decl-specifiers that could
11481 possibly be a type name is taken as the
11482 decl-specifier-seq of a declaration. The sequence shall
11483 be self-consistent as described below.
11484
11485 [dcl.type]
11486
11487 As a general rule, at most one type-specifier is allowed
11488 in the complete decl-specifier-seq of a declaration. The
11489 only exceptions are the following:
11490
11491 -- const or volatile can be combined with any other
11492 type-specifier.
11493
11494 -- signed or unsigned can be combined with char, long,
11495 short, or int.
11496
11497 -- ..
11498
11499 Example:
11500
11501 typedef char* Pc;
11502 void g (const int Pc);
11503
11504 Here, Pc is *not* part of the decl-specifier seq; it's
11505 the declarator. Therefore, once we see a type-specifier
11506 (other than a cv-qualifier), we forbid any additional
11507 user-defined types. We *do* still allow things like `int
11508 int' to be considered a decl-specifier-seq, and issue the
11509 error message later. */
11510 if (type_spec && !is_cv_qualifier)
11511 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11512 /* A constructor declarator cannot follow a type-specifier. */
11513 if (type_spec)
11514 {
11515 constructor_possible_p = false;
11516 found_decl_spec = true;
11517 if (!is_cv_qualifier)
11518 decl_specs->any_type_specifiers_p = true;
11519 }
11520 }
11521
11522 /* If we still do not have a DECL_SPEC, then there are no more
11523 decl-specifiers. */
11524 if (!found_decl_spec)
11525 break;
11526
11527 decl_specs->any_specifiers_p = true;
11528 /* After we see one decl-specifier, further decl-specifiers are
11529 always optional. */
11530 flags |= CP_PARSER_FLAGS_OPTIONAL;
11531 }
11532
11533 /* Don't allow a friend specifier with a class definition. */
11534 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11535 && (*declares_class_or_enum & 2))
11536 error_at (decl_specs->locations[ds_friend],
11537 "class definition may not be declared a friend");
11538 }
11539
11540 /* Parse an (optional) storage-class-specifier.
11541
11542 storage-class-specifier:
11543 auto
11544 register
11545 static
11546 extern
11547 mutable
11548
11549 GNU Extension:
11550
11551 storage-class-specifier:
11552 thread
11553
11554 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11555
11556 static tree
11557 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11558 {
11559 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11560 {
11561 case RID_AUTO:
11562 if (cxx_dialect != cxx98)
11563 return NULL_TREE;
11564 /* Fall through for C++98. */
11565
11566 case RID_REGISTER:
11567 case RID_STATIC:
11568 case RID_EXTERN:
11569 case RID_MUTABLE:
11570 case RID_THREAD:
11571 /* Consume the token. */
11572 return cp_lexer_consume_token (parser->lexer)->u.value;
11573
11574 default:
11575 return NULL_TREE;
11576 }
11577 }
11578
11579 /* Parse an (optional) function-specifier.
11580
11581 function-specifier:
11582 inline
11583 virtual
11584 explicit
11585
11586 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11587 Updates DECL_SPECS, if it is non-NULL. */
11588
11589 static tree
11590 cp_parser_function_specifier_opt (cp_parser* parser,
11591 cp_decl_specifier_seq *decl_specs)
11592 {
11593 cp_token *token = cp_lexer_peek_token (parser->lexer);
11594 switch (token->keyword)
11595 {
11596 case RID_INLINE:
11597 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11598 break;
11599
11600 case RID_VIRTUAL:
11601 /* 14.5.2.3 [temp.mem]
11602
11603 A member function template shall not be virtual. */
11604 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11605 error_at (token->location, "templates may not be %<virtual%>");
11606 else
11607 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11608 break;
11609
11610 case RID_EXPLICIT:
11611 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11612 break;
11613
11614 default:
11615 return NULL_TREE;
11616 }
11617
11618 /* Consume the token. */
11619 return cp_lexer_consume_token (parser->lexer)->u.value;
11620 }
11621
11622 /* Parse a linkage-specification.
11623
11624 linkage-specification:
11625 extern string-literal { declaration-seq [opt] }
11626 extern string-literal declaration */
11627
11628 static void
11629 cp_parser_linkage_specification (cp_parser* parser)
11630 {
11631 tree linkage;
11632
11633 /* Look for the `extern' keyword. */
11634 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11635
11636 /* Look for the string-literal. */
11637 linkage = cp_parser_string_literal (parser, false, false);
11638
11639 /* Transform the literal into an identifier. If the literal is a
11640 wide-character string, or contains embedded NULs, then we can't
11641 handle it as the user wants. */
11642 if (strlen (TREE_STRING_POINTER (linkage))
11643 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11644 {
11645 cp_parser_error (parser, "invalid linkage-specification");
11646 /* Assume C++ linkage. */
11647 linkage = lang_name_cplusplus;
11648 }
11649 else
11650 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11651
11652 /* We're now using the new linkage. */
11653 push_lang_context (linkage);
11654
11655 /* If the next token is a `{', then we're using the first
11656 production. */
11657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11658 {
11659 cp_ensure_no_omp_declare_simd (parser);
11660
11661 /* Consume the `{' token. */
11662 cp_lexer_consume_token (parser->lexer);
11663 /* Parse the declarations. */
11664 cp_parser_declaration_seq_opt (parser);
11665 /* Look for the closing `}'. */
11666 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11667 }
11668 /* Otherwise, there's just one declaration. */
11669 else
11670 {
11671 bool saved_in_unbraced_linkage_specification_p;
11672
11673 saved_in_unbraced_linkage_specification_p
11674 = parser->in_unbraced_linkage_specification_p;
11675 parser->in_unbraced_linkage_specification_p = true;
11676 cp_parser_declaration (parser);
11677 parser->in_unbraced_linkage_specification_p
11678 = saved_in_unbraced_linkage_specification_p;
11679 }
11680
11681 /* We're done with the linkage-specification. */
11682 pop_lang_context ();
11683 }
11684
11685 /* Parse a static_assert-declaration.
11686
11687 static_assert-declaration:
11688 static_assert ( constant-expression , string-literal ) ;
11689
11690 If MEMBER_P, this static_assert is a class member. */
11691
11692 static void
11693 cp_parser_static_assert(cp_parser *parser, bool member_p)
11694 {
11695 tree condition;
11696 tree message;
11697 cp_token *token;
11698 location_t saved_loc;
11699 bool dummy;
11700
11701 /* Peek at the `static_assert' token so we can keep track of exactly
11702 where the static assertion started. */
11703 token = cp_lexer_peek_token (parser->lexer);
11704 saved_loc = token->location;
11705
11706 /* Look for the `static_assert' keyword. */
11707 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11708 RT_STATIC_ASSERT))
11709 return;
11710
11711 /* We know we are in a static assertion; commit to any tentative
11712 parse. */
11713 if (cp_parser_parsing_tentatively (parser))
11714 cp_parser_commit_to_tentative_parse (parser);
11715
11716 /* Parse the `(' starting the static assertion condition. */
11717 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11718
11719 /* Parse the constant-expression. Allow a non-constant expression
11720 here in order to give better diagnostics in finish_static_assert. */
11721 condition =
11722 cp_parser_constant_expression (parser,
11723 /*allow_non_constant_p=*/true,
11724 /*non_constant_p=*/&dummy);
11725
11726 /* Parse the separating `,'. */
11727 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11728
11729 /* Parse the string-literal message. */
11730 message = cp_parser_string_literal (parser,
11731 /*translate=*/false,
11732 /*wide_ok=*/true);
11733
11734 /* A `)' completes the static assertion. */
11735 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11736 cp_parser_skip_to_closing_parenthesis (parser,
11737 /*recovering=*/true,
11738 /*or_comma=*/false,
11739 /*consume_paren=*/true);
11740
11741 /* A semicolon terminates the declaration. */
11742 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11743
11744 /* Complete the static assertion, which may mean either processing
11745 the static assert now or saving it for template instantiation. */
11746 finish_static_assert (condition, message, saved_loc, member_p);
11747 }
11748
11749 /* Parse the expression in decltype ( expression ). */
11750
11751 static tree
11752 cp_parser_decltype_expr (cp_parser *parser,
11753 bool &id_expression_or_member_access_p)
11754 {
11755 cp_token *id_expr_start_token;
11756 tree expr;
11757
11758 /* First, try parsing an id-expression. */
11759 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11760 cp_parser_parse_tentatively (parser);
11761 expr = cp_parser_id_expression (parser,
11762 /*template_keyword_p=*/false,
11763 /*check_dependency_p=*/true,
11764 /*template_p=*/NULL,
11765 /*declarator_p=*/false,
11766 /*optional_p=*/false);
11767
11768 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11769 {
11770 bool non_integral_constant_expression_p = false;
11771 tree id_expression = expr;
11772 cp_id_kind idk;
11773 const char *error_msg;
11774
11775 if (identifier_p (expr))
11776 /* Lookup the name we got back from the id-expression. */
11777 expr = cp_parser_lookup_name_simple (parser, expr,
11778 id_expr_start_token->location);
11779
11780 if (expr
11781 && expr != error_mark_node
11782 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11783 && TREE_CODE (expr) != TYPE_DECL
11784 && (TREE_CODE (expr) != BIT_NOT_EXPR
11785 || !TYPE_P (TREE_OPERAND (expr, 0)))
11786 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11787 {
11788 /* Complete lookup of the id-expression. */
11789 expr = (finish_id_expression
11790 (id_expression, expr, parser->scope, &idk,
11791 /*integral_constant_expression_p=*/false,
11792 /*allow_non_integral_constant_expression_p=*/true,
11793 &non_integral_constant_expression_p,
11794 /*template_p=*/false,
11795 /*done=*/true,
11796 /*address_p=*/false,
11797 /*template_arg_p=*/false,
11798 &error_msg,
11799 id_expr_start_token->location));
11800
11801 if (expr == error_mark_node)
11802 /* We found an id-expression, but it was something that we
11803 should not have found. This is an error, not something
11804 we can recover from, so note that we found an
11805 id-expression and we'll recover as gracefully as
11806 possible. */
11807 id_expression_or_member_access_p = true;
11808 }
11809
11810 if (expr
11811 && expr != error_mark_node
11812 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11813 /* We have an id-expression. */
11814 id_expression_or_member_access_p = true;
11815 }
11816
11817 if (!id_expression_or_member_access_p)
11818 {
11819 /* Abort the id-expression parse. */
11820 cp_parser_abort_tentative_parse (parser);
11821
11822 /* Parsing tentatively, again. */
11823 cp_parser_parse_tentatively (parser);
11824
11825 /* Parse a class member access. */
11826 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11827 /*cast_p=*/false, /*decltype*/true,
11828 /*member_access_only_p=*/true, NULL);
11829
11830 if (expr
11831 && expr != error_mark_node
11832 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11833 /* We have an id-expression. */
11834 id_expression_or_member_access_p = true;
11835 }
11836
11837 if (id_expression_or_member_access_p)
11838 /* We have parsed the complete id-expression or member access. */
11839 cp_parser_parse_definitely (parser);
11840 else
11841 {
11842 /* Abort our attempt to parse an id-expression or member access
11843 expression. */
11844 cp_parser_abort_tentative_parse (parser);
11845
11846 /* Parse a full expression. */
11847 expr = cp_parser_expression (parser, /*cast_p=*/false,
11848 /*decltype*/true, NULL);
11849 }
11850
11851 return expr;
11852 }
11853
11854 /* Parse a `decltype' type. Returns the type.
11855
11856 simple-type-specifier:
11857 decltype ( expression )
11858 C++14 proposal:
11859 decltype ( auto ) */
11860
11861 static tree
11862 cp_parser_decltype (cp_parser *parser)
11863 {
11864 tree expr;
11865 bool id_expression_or_member_access_p = false;
11866 const char *saved_message;
11867 bool saved_integral_constant_expression_p;
11868 bool saved_non_integral_constant_expression_p;
11869 bool saved_greater_than_is_operator_p;
11870 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11871
11872 if (start_token->type == CPP_DECLTYPE)
11873 {
11874 /* Already parsed. */
11875 cp_lexer_consume_token (parser->lexer);
11876 return start_token->u.value;
11877 }
11878
11879 /* Look for the `decltype' token. */
11880 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11881 return error_mark_node;
11882
11883 /* Parse the opening `('. */
11884 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11885 return error_mark_node;
11886
11887 /* decltype (auto) */
11888 if (cxx_dialect >= cxx1y
11889 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11890 {
11891 cp_lexer_consume_token (parser->lexer);
11892 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11893 return error_mark_node;
11894 expr = make_decltype_auto ();
11895 AUTO_IS_DECLTYPE (expr) = true;
11896 goto rewrite;
11897 }
11898
11899 /* Types cannot be defined in a `decltype' expression. Save away the
11900 old message. */
11901 saved_message = parser->type_definition_forbidden_message;
11902
11903 /* And create the new one. */
11904 parser->type_definition_forbidden_message
11905 = G_("types may not be defined in %<decltype%> expressions");
11906
11907 /* The restrictions on constant-expressions do not apply inside
11908 decltype expressions. */
11909 saved_integral_constant_expression_p
11910 = parser->integral_constant_expression_p;
11911 saved_non_integral_constant_expression_p
11912 = parser->non_integral_constant_expression_p;
11913 parser->integral_constant_expression_p = false;
11914
11915 /* Within a parenthesized expression, a `>' token is always
11916 the greater-than operator. */
11917 saved_greater_than_is_operator_p
11918 = parser->greater_than_is_operator_p;
11919 parser->greater_than_is_operator_p = true;
11920
11921 /* Do not actually evaluate the expression. */
11922 ++cp_unevaluated_operand;
11923
11924 /* Do not warn about problems with the expression. */
11925 ++c_inhibit_evaluation_warnings;
11926
11927 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
11928
11929 /* Go back to evaluating expressions. */
11930 --cp_unevaluated_operand;
11931 --c_inhibit_evaluation_warnings;
11932
11933 /* The `>' token might be the end of a template-id or
11934 template-parameter-list now. */
11935 parser->greater_than_is_operator_p
11936 = saved_greater_than_is_operator_p;
11937
11938 /* Restore the old message and the integral constant expression
11939 flags. */
11940 parser->type_definition_forbidden_message = saved_message;
11941 parser->integral_constant_expression_p
11942 = saved_integral_constant_expression_p;
11943 parser->non_integral_constant_expression_p
11944 = saved_non_integral_constant_expression_p;
11945
11946 /* Parse to the closing `)'. */
11947 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11948 {
11949 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11950 /*consume_paren=*/true);
11951 return error_mark_node;
11952 }
11953
11954 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11955 tf_warning_or_error);
11956
11957 rewrite:
11958 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11959 it again. */
11960 start_token->type = CPP_DECLTYPE;
11961 start_token->u.value = expr;
11962 start_token->keyword = RID_MAX;
11963 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11964
11965 return expr;
11966 }
11967
11968 /* Special member functions [gram.special] */
11969
11970 /* Parse a conversion-function-id.
11971
11972 conversion-function-id:
11973 operator conversion-type-id
11974
11975 Returns an IDENTIFIER_NODE representing the operator. */
11976
11977 static tree
11978 cp_parser_conversion_function_id (cp_parser* parser)
11979 {
11980 tree type;
11981 tree saved_scope;
11982 tree saved_qualifying_scope;
11983 tree saved_object_scope;
11984 tree pushed_scope = NULL_TREE;
11985
11986 /* Look for the `operator' token. */
11987 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11988 return error_mark_node;
11989 /* When we parse the conversion-type-id, the current scope will be
11990 reset. However, we need that information in able to look up the
11991 conversion function later, so we save it here. */
11992 saved_scope = parser->scope;
11993 saved_qualifying_scope = parser->qualifying_scope;
11994 saved_object_scope = parser->object_scope;
11995 /* We must enter the scope of the class so that the names of
11996 entities declared within the class are available in the
11997 conversion-type-id. For example, consider:
11998
11999 struct S {
12000 typedef int I;
12001 operator I();
12002 };
12003
12004 S::operator I() { ... }
12005
12006 In order to see that `I' is a type-name in the definition, we
12007 must be in the scope of `S'. */
12008 if (saved_scope)
12009 pushed_scope = push_scope (saved_scope);
12010 /* Parse the conversion-type-id. */
12011 type = cp_parser_conversion_type_id (parser);
12012 /* Leave the scope of the class, if any. */
12013 if (pushed_scope)
12014 pop_scope (pushed_scope);
12015 /* Restore the saved scope. */
12016 parser->scope = saved_scope;
12017 parser->qualifying_scope = saved_qualifying_scope;
12018 parser->object_scope = saved_object_scope;
12019 /* If the TYPE is invalid, indicate failure. */
12020 if (type == error_mark_node)
12021 return error_mark_node;
12022 return mangle_conv_op_name_for_type (type);
12023 }
12024
12025 /* Parse a conversion-type-id:
12026
12027 conversion-type-id:
12028 type-specifier-seq conversion-declarator [opt]
12029
12030 Returns the TYPE specified. */
12031
12032 static tree
12033 cp_parser_conversion_type_id (cp_parser* parser)
12034 {
12035 tree attributes;
12036 cp_decl_specifier_seq type_specifiers;
12037 cp_declarator *declarator;
12038 tree type_specified;
12039 const char *saved_message;
12040
12041 /* Parse the attributes. */
12042 attributes = cp_parser_attributes_opt (parser);
12043
12044 saved_message = parser->type_definition_forbidden_message;
12045 parser->type_definition_forbidden_message
12046 = G_("types may not be defined in a conversion-type-id");
12047
12048 /* Parse the type-specifiers. */
12049 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12050 /*is_trailing_return=*/false,
12051 &type_specifiers);
12052
12053 parser->type_definition_forbidden_message = saved_message;
12054
12055 /* If that didn't work, stop. */
12056 if (type_specifiers.type == error_mark_node)
12057 return error_mark_node;
12058 /* Parse the conversion-declarator. */
12059 declarator = cp_parser_conversion_declarator_opt (parser);
12060
12061 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12062 /*initialized=*/0, &attributes);
12063 if (attributes)
12064 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12065
12066 /* Don't give this error when parsing tentatively. This happens to
12067 work because we always parse this definitively once. */
12068 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12069 && type_uses_auto (type_specified))
12070 {
12071 if (cxx_dialect < cxx1y)
12072 {
12073 error ("invalid use of %<auto%> in conversion operator");
12074 return error_mark_node;
12075 }
12076 else if (template_parm_scope_p ())
12077 warning (0, "use of %<auto%> in member template "
12078 "conversion operator can never be deduced");
12079 }
12080
12081 return type_specified;
12082 }
12083
12084 /* Parse an (optional) conversion-declarator.
12085
12086 conversion-declarator:
12087 ptr-operator conversion-declarator [opt]
12088
12089 */
12090
12091 static cp_declarator *
12092 cp_parser_conversion_declarator_opt (cp_parser* parser)
12093 {
12094 enum tree_code code;
12095 tree class_type, std_attributes = NULL_TREE;
12096 cp_cv_quals cv_quals;
12097
12098 /* We don't know if there's a ptr-operator next, or not. */
12099 cp_parser_parse_tentatively (parser);
12100 /* Try the ptr-operator. */
12101 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12102 &std_attributes);
12103 /* If it worked, look for more conversion-declarators. */
12104 if (cp_parser_parse_definitely (parser))
12105 {
12106 cp_declarator *declarator;
12107
12108 /* Parse another optional declarator. */
12109 declarator = cp_parser_conversion_declarator_opt (parser);
12110
12111 declarator = cp_parser_make_indirect_declarator
12112 (code, class_type, cv_quals, declarator, std_attributes);
12113
12114 return declarator;
12115 }
12116
12117 return NULL;
12118 }
12119
12120 /* Parse an (optional) ctor-initializer.
12121
12122 ctor-initializer:
12123 : mem-initializer-list
12124
12125 Returns TRUE iff the ctor-initializer was actually present. */
12126
12127 static bool
12128 cp_parser_ctor_initializer_opt (cp_parser* parser)
12129 {
12130 /* If the next token is not a `:', then there is no
12131 ctor-initializer. */
12132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12133 {
12134 /* Do default initialization of any bases and members. */
12135 if (DECL_CONSTRUCTOR_P (current_function_decl))
12136 finish_mem_initializers (NULL_TREE);
12137
12138 return false;
12139 }
12140
12141 /* Consume the `:' token. */
12142 cp_lexer_consume_token (parser->lexer);
12143 /* And the mem-initializer-list. */
12144 cp_parser_mem_initializer_list (parser);
12145
12146 return true;
12147 }
12148
12149 /* Parse a mem-initializer-list.
12150
12151 mem-initializer-list:
12152 mem-initializer ... [opt]
12153 mem-initializer ... [opt] , mem-initializer-list */
12154
12155 static void
12156 cp_parser_mem_initializer_list (cp_parser* parser)
12157 {
12158 tree mem_initializer_list = NULL_TREE;
12159 tree target_ctor = error_mark_node;
12160 cp_token *token = cp_lexer_peek_token (parser->lexer);
12161
12162 /* Let the semantic analysis code know that we are starting the
12163 mem-initializer-list. */
12164 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12165 error_at (token->location,
12166 "only constructors take member initializers");
12167
12168 /* Loop through the list. */
12169 while (true)
12170 {
12171 tree mem_initializer;
12172
12173 token = cp_lexer_peek_token (parser->lexer);
12174 /* Parse the mem-initializer. */
12175 mem_initializer = cp_parser_mem_initializer (parser);
12176 /* If the next token is a `...', we're expanding member initializers. */
12177 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12178 {
12179 /* Consume the `...'. */
12180 cp_lexer_consume_token (parser->lexer);
12181
12182 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12183 can be expanded but members cannot. */
12184 if (mem_initializer != error_mark_node
12185 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12186 {
12187 error_at (token->location,
12188 "cannot expand initializer for member %<%D%>",
12189 TREE_PURPOSE (mem_initializer));
12190 mem_initializer = error_mark_node;
12191 }
12192
12193 /* Construct the pack expansion type. */
12194 if (mem_initializer != error_mark_node)
12195 mem_initializer = make_pack_expansion (mem_initializer);
12196 }
12197 if (target_ctor != error_mark_node
12198 && mem_initializer != error_mark_node)
12199 {
12200 error ("mem-initializer for %qD follows constructor delegation",
12201 TREE_PURPOSE (mem_initializer));
12202 mem_initializer = error_mark_node;
12203 }
12204 /* Look for a target constructor. */
12205 if (mem_initializer != error_mark_node
12206 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12207 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12208 {
12209 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12210 if (mem_initializer_list)
12211 {
12212 error ("constructor delegation follows mem-initializer for %qD",
12213 TREE_PURPOSE (mem_initializer_list));
12214 mem_initializer = error_mark_node;
12215 }
12216 target_ctor = mem_initializer;
12217 }
12218 /* Add it to the list, unless it was erroneous. */
12219 if (mem_initializer != error_mark_node)
12220 {
12221 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12222 mem_initializer_list = mem_initializer;
12223 }
12224 /* If the next token is not a `,', we're done. */
12225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12226 break;
12227 /* Consume the `,' token. */
12228 cp_lexer_consume_token (parser->lexer);
12229 }
12230
12231 /* Perform semantic analysis. */
12232 if (DECL_CONSTRUCTOR_P (current_function_decl))
12233 finish_mem_initializers (mem_initializer_list);
12234 }
12235
12236 /* Parse a mem-initializer.
12237
12238 mem-initializer:
12239 mem-initializer-id ( expression-list [opt] )
12240 mem-initializer-id braced-init-list
12241
12242 GNU extension:
12243
12244 mem-initializer:
12245 ( expression-list [opt] )
12246
12247 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12248 class) or FIELD_DECL (for a non-static data member) to initialize;
12249 the TREE_VALUE is the expression-list. An empty initialization
12250 list is represented by void_list_node. */
12251
12252 static tree
12253 cp_parser_mem_initializer (cp_parser* parser)
12254 {
12255 tree mem_initializer_id;
12256 tree expression_list;
12257 tree member;
12258 cp_token *token = cp_lexer_peek_token (parser->lexer);
12259
12260 /* Find out what is being initialized. */
12261 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12262 {
12263 permerror (token->location,
12264 "anachronistic old-style base class initializer");
12265 mem_initializer_id = NULL_TREE;
12266 }
12267 else
12268 {
12269 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12270 if (mem_initializer_id == error_mark_node)
12271 return mem_initializer_id;
12272 }
12273 member = expand_member_init (mem_initializer_id);
12274 if (member && !DECL_P (member))
12275 in_base_initializer = 1;
12276
12277 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12278 {
12279 bool expr_non_constant_p;
12280 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12281 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12282 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12283 expression_list = build_tree_list (NULL_TREE, expression_list);
12284 }
12285 else
12286 {
12287 vec<tree, va_gc> *vec;
12288 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12289 /*cast_p=*/false,
12290 /*allow_expansion_p=*/true,
12291 /*non_constant_p=*/NULL);
12292 if (vec == NULL)
12293 return error_mark_node;
12294 expression_list = build_tree_list_vec (vec);
12295 release_tree_vector (vec);
12296 }
12297
12298 if (expression_list == error_mark_node)
12299 return error_mark_node;
12300 if (!expression_list)
12301 expression_list = void_type_node;
12302
12303 in_base_initializer = 0;
12304
12305 return member ? build_tree_list (member, expression_list) : error_mark_node;
12306 }
12307
12308 /* Parse a mem-initializer-id.
12309
12310 mem-initializer-id:
12311 :: [opt] nested-name-specifier [opt] class-name
12312 identifier
12313
12314 Returns a TYPE indicating the class to be initializer for the first
12315 production. Returns an IDENTIFIER_NODE indicating the data member
12316 to be initialized for the second production. */
12317
12318 static tree
12319 cp_parser_mem_initializer_id (cp_parser* parser)
12320 {
12321 bool global_scope_p;
12322 bool nested_name_specifier_p;
12323 bool template_p = false;
12324 tree id;
12325
12326 cp_token *token = cp_lexer_peek_token (parser->lexer);
12327
12328 /* `typename' is not allowed in this context ([temp.res]). */
12329 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12330 {
12331 error_at (token->location,
12332 "keyword %<typename%> not allowed in this context (a qualified "
12333 "member initializer is implicitly a type)");
12334 cp_lexer_consume_token (parser->lexer);
12335 }
12336 /* Look for the optional `::' operator. */
12337 global_scope_p
12338 = (cp_parser_global_scope_opt (parser,
12339 /*current_scope_valid_p=*/false)
12340 != NULL_TREE);
12341 /* Look for the optional nested-name-specifier. The simplest way to
12342 implement:
12343
12344 [temp.res]
12345
12346 The keyword `typename' is not permitted in a base-specifier or
12347 mem-initializer; in these contexts a qualified name that
12348 depends on a template-parameter is implicitly assumed to be a
12349 type name.
12350
12351 is to assume that we have seen the `typename' keyword at this
12352 point. */
12353 nested_name_specifier_p
12354 = (cp_parser_nested_name_specifier_opt (parser,
12355 /*typename_keyword_p=*/true,
12356 /*check_dependency_p=*/true,
12357 /*type_p=*/true,
12358 /*is_declaration=*/true)
12359 != NULL_TREE);
12360 if (nested_name_specifier_p)
12361 template_p = cp_parser_optional_template_keyword (parser);
12362 /* If there is a `::' operator or a nested-name-specifier, then we
12363 are definitely looking for a class-name. */
12364 if (global_scope_p || nested_name_specifier_p)
12365 return cp_parser_class_name (parser,
12366 /*typename_keyword_p=*/true,
12367 /*template_keyword_p=*/template_p,
12368 typename_type,
12369 /*check_dependency_p=*/true,
12370 /*class_head_p=*/false,
12371 /*is_declaration=*/true);
12372 /* Otherwise, we could also be looking for an ordinary identifier. */
12373 cp_parser_parse_tentatively (parser);
12374 /* Try a class-name. */
12375 id = cp_parser_class_name (parser,
12376 /*typename_keyword_p=*/true,
12377 /*template_keyword_p=*/false,
12378 none_type,
12379 /*check_dependency_p=*/true,
12380 /*class_head_p=*/false,
12381 /*is_declaration=*/true);
12382 /* If we found one, we're done. */
12383 if (cp_parser_parse_definitely (parser))
12384 return id;
12385 /* Otherwise, look for an ordinary identifier. */
12386 return cp_parser_identifier (parser);
12387 }
12388
12389 /* Overloading [gram.over] */
12390
12391 /* Parse an operator-function-id.
12392
12393 operator-function-id:
12394 operator operator
12395
12396 Returns an IDENTIFIER_NODE for the operator which is a
12397 human-readable spelling of the identifier, e.g., `operator +'. */
12398
12399 static tree
12400 cp_parser_operator_function_id (cp_parser* parser)
12401 {
12402 /* Look for the `operator' keyword. */
12403 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12404 return error_mark_node;
12405 /* And then the name of the operator itself. */
12406 return cp_parser_operator (parser);
12407 }
12408
12409 /* Return an identifier node for a user-defined literal operator.
12410 The suffix identifier is chained to the operator name identifier. */
12411
12412 static tree
12413 cp_literal_operator_id (const char* name)
12414 {
12415 tree identifier;
12416 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12417 + strlen (name) + 10);
12418 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12419 identifier = get_identifier (buffer);
12420
12421 return identifier;
12422 }
12423
12424 /* Parse an operator.
12425
12426 operator:
12427 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12428 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12429 || ++ -- , ->* -> () []
12430
12431 GNU Extensions:
12432
12433 operator:
12434 <? >? <?= >?=
12435
12436 Returns an IDENTIFIER_NODE for the operator which is a
12437 human-readable spelling of the identifier, e.g., `operator +'. */
12438
12439 static tree
12440 cp_parser_operator (cp_parser* parser)
12441 {
12442 tree id = NULL_TREE;
12443 cp_token *token;
12444 bool bad_encoding_prefix = false;
12445
12446 /* Peek at the next token. */
12447 token = cp_lexer_peek_token (parser->lexer);
12448 /* Figure out which operator we have. */
12449 switch (token->type)
12450 {
12451 case CPP_KEYWORD:
12452 {
12453 enum tree_code op;
12454
12455 /* The keyword should be either `new' or `delete'. */
12456 if (token->keyword == RID_NEW)
12457 op = NEW_EXPR;
12458 else if (token->keyword == RID_DELETE)
12459 op = DELETE_EXPR;
12460 else
12461 break;
12462
12463 /* Consume the `new' or `delete' token. */
12464 cp_lexer_consume_token (parser->lexer);
12465
12466 /* Peek at the next token. */
12467 token = cp_lexer_peek_token (parser->lexer);
12468 /* If it's a `[' token then this is the array variant of the
12469 operator. */
12470 if (token->type == CPP_OPEN_SQUARE)
12471 {
12472 /* Consume the `[' token. */
12473 cp_lexer_consume_token (parser->lexer);
12474 /* Look for the `]' token. */
12475 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12476 id = ansi_opname (op == NEW_EXPR
12477 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12478 }
12479 /* Otherwise, we have the non-array variant. */
12480 else
12481 id = ansi_opname (op);
12482
12483 return id;
12484 }
12485
12486 case CPP_PLUS:
12487 id = ansi_opname (PLUS_EXPR);
12488 break;
12489
12490 case CPP_MINUS:
12491 id = ansi_opname (MINUS_EXPR);
12492 break;
12493
12494 case CPP_MULT:
12495 id = ansi_opname (MULT_EXPR);
12496 break;
12497
12498 case CPP_DIV:
12499 id = ansi_opname (TRUNC_DIV_EXPR);
12500 break;
12501
12502 case CPP_MOD:
12503 id = ansi_opname (TRUNC_MOD_EXPR);
12504 break;
12505
12506 case CPP_XOR:
12507 id = ansi_opname (BIT_XOR_EXPR);
12508 break;
12509
12510 case CPP_AND:
12511 id = ansi_opname (BIT_AND_EXPR);
12512 break;
12513
12514 case CPP_OR:
12515 id = ansi_opname (BIT_IOR_EXPR);
12516 break;
12517
12518 case CPP_COMPL:
12519 id = ansi_opname (BIT_NOT_EXPR);
12520 break;
12521
12522 case CPP_NOT:
12523 id = ansi_opname (TRUTH_NOT_EXPR);
12524 break;
12525
12526 case CPP_EQ:
12527 id = ansi_assopname (NOP_EXPR);
12528 break;
12529
12530 case CPP_LESS:
12531 id = ansi_opname (LT_EXPR);
12532 break;
12533
12534 case CPP_GREATER:
12535 id = ansi_opname (GT_EXPR);
12536 break;
12537
12538 case CPP_PLUS_EQ:
12539 id = ansi_assopname (PLUS_EXPR);
12540 break;
12541
12542 case CPP_MINUS_EQ:
12543 id = ansi_assopname (MINUS_EXPR);
12544 break;
12545
12546 case CPP_MULT_EQ:
12547 id = ansi_assopname (MULT_EXPR);
12548 break;
12549
12550 case CPP_DIV_EQ:
12551 id = ansi_assopname (TRUNC_DIV_EXPR);
12552 break;
12553
12554 case CPP_MOD_EQ:
12555 id = ansi_assopname (TRUNC_MOD_EXPR);
12556 break;
12557
12558 case CPP_XOR_EQ:
12559 id = ansi_assopname (BIT_XOR_EXPR);
12560 break;
12561
12562 case CPP_AND_EQ:
12563 id = ansi_assopname (BIT_AND_EXPR);
12564 break;
12565
12566 case CPP_OR_EQ:
12567 id = ansi_assopname (BIT_IOR_EXPR);
12568 break;
12569
12570 case CPP_LSHIFT:
12571 id = ansi_opname (LSHIFT_EXPR);
12572 break;
12573
12574 case CPP_RSHIFT:
12575 id = ansi_opname (RSHIFT_EXPR);
12576 break;
12577
12578 case CPP_LSHIFT_EQ:
12579 id = ansi_assopname (LSHIFT_EXPR);
12580 break;
12581
12582 case CPP_RSHIFT_EQ:
12583 id = ansi_assopname (RSHIFT_EXPR);
12584 break;
12585
12586 case CPP_EQ_EQ:
12587 id = ansi_opname (EQ_EXPR);
12588 break;
12589
12590 case CPP_NOT_EQ:
12591 id = ansi_opname (NE_EXPR);
12592 break;
12593
12594 case CPP_LESS_EQ:
12595 id = ansi_opname (LE_EXPR);
12596 break;
12597
12598 case CPP_GREATER_EQ:
12599 id = ansi_opname (GE_EXPR);
12600 break;
12601
12602 case CPP_AND_AND:
12603 id = ansi_opname (TRUTH_ANDIF_EXPR);
12604 break;
12605
12606 case CPP_OR_OR:
12607 id = ansi_opname (TRUTH_ORIF_EXPR);
12608 break;
12609
12610 case CPP_PLUS_PLUS:
12611 id = ansi_opname (POSTINCREMENT_EXPR);
12612 break;
12613
12614 case CPP_MINUS_MINUS:
12615 id = ansi_opname (PREDECREMENT_EXPR);
12616 break;
12617
12618 case CPP_COMMA:
12619 id = ansi_opname (COMPOUND_EXPR);
12620 break;
12621
12622 case CPP_DEREF_STAR:
12623 id = ansi_opname (MEMBER_REF);
12624 break;
12625
12626 case CPP_DEREF:
12627 id = ansi_opname (COMPONENT_REF);
12628 break;
12629
12630 case CPP_OPEN_PAREN:
12631 /* Consume the `('. */
12632 cp_lexer_consume_token (parser->lexer);
12633 /* Look for the matching `)'. */
12634 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12635 return ansi_opname (CALL_EXPR);
12636
12637 case CPP_OPEN_SQUARE:
12638 /* Consume the `['. */
12639 cp_lexer_consume_token (parser->lexer);
12640 /* Look for the matching `]'. */
12641 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12642 return ansi_opname (ARRAY_REF);
12643
12644 case CPP_WSTRING:
12645 case CPP_STRING16:
12646 case CPP_STRING32:
12647 case CPP_UTF8STRING:
12648 bad_encoding_prefix = true;
12649 /* Fall through. */
12650
12651 case CPP_STRING:
12652 if (cxx_dialect == cxx98)
12653 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12654 if (bad_encoding_prefix)
12655 {
12656 error ("invalid encoding prefix in literal operator");
12657 return error_mark_node;
12658 }
12659 if (TREE_STRING_LENGTH (token->u.value) > 2)
12660 {
12661 error ("expected empty string after %<operator%> keyword");
12662 return error_mark_node;
12663 }
12664 /* Consume the string. */
12665 cp_lexer_consume_token (parser->lexer);
12666 /* Look for the suffix identifier. */
12667 token = cp_lexer_peek_token (parser->lexer);
12668 if (token->type == CPP_NAME)
12669 {
12670 id = cp_parser_identifier (parser);
12671 if (id != error_mark_node)
12672 {
12673 const char *name = IDENTIFIER_POINTER (id);
12674 return cp_literal_operator_id (name);
12675 }
12676 }
12677 else if (token->type == CPP_KEYWORD)
12678 {
12679 error ("unexpected keyword;"
12680 " remove space between quotes and suffix identifier");
12681 return error_mark_node;
12682 }
12683 else
12684 {
12685 error ("expected suffix identifier");
12686 return error_mark_node;
12687 }
12688
12689 case CPP_WSTRING_USERDEF:
12690 case CPP_STRING16_USERDEF:
12691 case CPP_STRING32_USERDEF:
12692 case CPP_UTF8STRING_USERDEF:
12693 bad_encoding_prefix = true;
12694 /* Fall through. */
12695
12696 case CPP_STRING_USERDEF:
12697 if (cxx_dialect == cxx98)
12698 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12699 if (bad_encoding_prefix)
12700 {
12701 error ("invalid encoding prefix in literal operator");
12702 return error_mark_node;
12703 }
12704 {
12705 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12706 if (TREE_STRING_LENGTH (string_tree) > 2)
12707 {
12708 error ("expected empty string after %<operator%> keyword");
12709 return error_mark_node;
12710 }
12711 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12712 /* Consume the user-defined string literal. */
12713 cp_lexer_consume_token (parser->lexer);
12714 if (id != error_mark_node)
12715 {
12716 const char *name = IDENTIFIER_POINTER (id);
12717 return cp_literal_operator_id (name);
12718 }
12719 else
12720 return error_mark_node;
12721 }
12722
12723 default:
12724 /* Anything else is an error. */
12725 break;
12726 }
12727
12728 /* If we have selected an identifier, we need to consume the
12729 operator token. */
12730 if (id)
12731 cp_lexer_consume_token (parser->lexer);
12732 /* Otherwise, no valid operator name was present. */
12733 else
12734 {
12735 cp_parser_error (parser, "expected operator");
12736 id = error_mark_node;
12737 }
12738
12739 return id;
12740 }
12741
12742 /* Parse a template-declaration.
12743
12744 template-declaration:
12745 export [opt] template < template-parameter-list > declaration
12746
12747 If MEMBER_P is TRUE, this template-declaration occurs within a
12748 class-specifier.
12749
12750 The grammar rule given by the standard isn't correct. What
12751 is really meant is:
12752
12753 template-declaration:
12754 export [opt] template-parameter-list-seq
12755 decl-specifier-seq [opt] init-declarator [opt] ;
12756 export [opt] template-parameter-list-seq
12757 function-definition
12758
12759 template-parameter-list-seq:
12760 template-parameter-list-seq [opt]
12761 template < template-parameter-list > */
12762
12763 static void
12764 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12765 {
12766 /* Check for `export'. */
12767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12768 {
12769 /* Consume the `export' token. */
12770 cp_lexer_consume_token (parser->lexer);
12771 /* Warn that we do not support `export'. */
12772 warning (0, "keyword %<export%> not implemented, and will be ignored");
12773 }
12774
12775 cp_parser_template_declaration_after_export (parser, member_p);
12776 }
12777
12778 /* Parse a template-parameter-list.
12779
12780 template-parameter-list:
12781 template-parameter
12782 template-parameter-list , template-parameter
12783
12784 Returns a TREE_LIST. Each node represents a template parameter.
12785 The nodes are connected via their TREE_CHAINs. */
12786
12787 static tree
12788 cp_parser_template_parameter_list (cp_parser* parser)
12789 {
12790 tree parameter_list = NULL_TREE;
12791
12792 begin_template_parm_list ();
12793
12794 /* The loop below parses the template parms. We first need to know
12795 the total number of template parms to be able to compute proper
12796 canonical types of each dependent type. So after the loop, when
12797 we know the total number of template parms,
12798 end_template_parm_list computes the proper canonical types and
12799 fixes up the dependent types accordingly. */
12800 while (true)
12801 {
12802 tree parameter;
12803 bool is_non_type;
12804 bool is_parameter_pack;
12805 location_t parm_loc;
12806
12807 /* Parse the template-parameter. */
12808 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12809 parameter = cp_parser_template_parameter (parser,
12810 &is_non_type,
12811 &is_parameter_pack);
12812 /* Add it to the list. */
12813 if (parameter != error_mark_node)
12814 parameter_list = process_template_parm (parameter_list,
12815 parm_loc,
12816 parameter,
12817 is_non_type,
12818 is_parameter_pack);
12819 else
12820 {
12821 tree err_parm = build_tree_list (parameter, parameter);
12822 parameter_list = chainon (parameter_list, err_parm);
12823 }
12824
12825 /* If the next token is not a `,', we're done. */
12826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12827 break;
12828 /* Otherwise, consume the `,' token. */
12829 cp_lexer_consume_token (parser->lexer);
12830 }
12831
12832 return end_template_parm_list (parameter_list);
12833 }
12834
12835 /* Parse a template-parameter.
12836
12837 template-parameter:
12838 type-parameter
12839 parameter-declaration
12840
12841 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12842 the parameter. The TREE_PURPOSE is the default value, if any.
12843 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12844 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12845 set to true iff this parameter is a parameter pack. */
12846
12847 static tree
12848 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12849 bool *is_parameter_pack)
12850 {
12851 cp_token *token;
12852 cp_parameter_declarator *parameter_declarator;
12853 cp_declarator *id_declarator;
12854 tree parm;
12855
12856 /* Assume it is a type parameter or a template parameter. */
12857 *is_non_type = false;
12858 /* Assume it not a parameter pack. */
12859 *is_parameter_pack = false;
12860 /* Peek at the next token. */
12861 token = cp_lexer_peek_token (parser->lexer);
12862 /* If it is `class' or `template', we have a type-parameter. */
12863 if (token->keyword == RID_TEMPLATE)
12864 return cp_parser_type_parameter (parser, is_parameter_pack);
12865 /* If it is `class' or `typename' we do not know yet whether it is a
12866 type parameter or a non-type parameter. Consider:
12867
12868 template <typename T, typename T::X X> ...
12869
12870 or:
12871
12872 template <class C, class D*> ...
12873
12874 Here, the first parameter is a type parameter, and the second is
12875 a non-type parameter. We can tell by looking at the token after
12876 the identifier -- if it is a `,', `=', or `>' then we have a type
12877 parameter. */
12878 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12879 {
12880 /* Peek at the token after `class' or `typename'. */
12881 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12882 /* If it's an ellipsis, we have a template type parameter
12883 pack. */
12884 if (token->type == CPP_ELLIPSIS)
12885 return cp_parser_type_parameter (parser, is_parameter_pack);
12886 /* If it's an identifier, skip it. */
12887 if (token->type == CPP_NAME)
12888 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12889 /* Now, see if the token looks like the end of a template
12890 parameter. */
12891 if (token->type == CPP_COMMA
12892 || token->type == CPP_EQ
12893 || token->type == CPP_GREATER)
12894 return cp_parser_type_parameter (parser, is_parameter_pack);
12895 }
12896
12897 /* Otherwise, it is a non-type parameter.
12898
12899 [temp.param]
12900
12901 When parsing a default template-argument for a non-type
12902 template-parameter, the first non-nested `>' is taken as the end
12903 of the template parameter-list rather than a greater-than
12904 operator. */
12905 *is_non_type = true;
12906 parameter_declarator
12907 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12908 /*parenthesized_p=*/NULL);
12909
12910 /* If the parameter declaration is marked as a parameter pack, set
12911 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12912 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12913 grokdeclarator. */
12914 if (parameter_declarator
12915 && parameter_declarator->declarator
12916 && parameter_declarator->declarator->parameter_pack_p)
12917 {
12918 *is_parameter_pack = true;
12919 parameter_declarator->declarator->parameter_pack_p = false;
12920 }
12921
12922 if (parameter_declarator
12923 && parameter_declarator->default_argument)
12924 {
12925 /* Can happen in some cases of erroneous input (c++/34892). */
12926 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12927 /* Consume the `...' for better error recovery. */
12928 cp_lexer_consume_token (parser->lexer);
12929 }
12930 /* If the next token is an ellipsis, and we don't already have it
12931 marked as a parameter pack, then we have a parameter pack (that
12932 has no declarator). */
12933 else if (!*is_parameter_pack
12934 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12935 && (declarator_can_be_parameter_pack
12936 (parameter_declarator->declarator)))
12937 {
12938 /* Consume the `...'. */
12939 cp_lexer_consume_token (parser->lexer);
12940 maybe_warn_variadic_templates ();
12941
12942 *is_parameter_pack = true;
12943 }
12944 /* We might end up with a pack expansion as the type of the non-type
12945 template parameter, in which case this is a non-type template
12946 parameter pack. */
12947 else if (parameter_declarator
12948 && parameter_declarator->decl_specifiers.type
12949 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12950 {
12951 *is_parameter_pack = true;
12952 parameter_declarator->decl_specifiers.type =
12953 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12954 }
12955
12956 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12957 {
12958 /* Parameter packs cannot have default arguments. However, a
12959 user may try to do so, so we'll parse them and give an
12960 appropriate diagnostic here. */
12961
12962 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12963
12964 /* Find the name of the parameter pack. */
12965 id_declarator = parameter_declarator->declarator;
12966 while (id_declarator && id_declarator->kind != cdk_id)
12967 id_declarator = id_declarator->declarator;
12968
12969 if (id_declarator && id_declarator->kind == cdk_id)
12970 error_at (start_token->location,
12971 "template parameter pack %qD cannot have a default argument",
12972 id_declarator->u.id.unqualified_name);
12973 else
12974 error_at (start_token->location,
12975 "template parameter pack cannot have a default argument");
12976
12977 /* Parse the default argument, but throw away the result. */
12978 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12979 }
12980
12981 parm = grokdeclarator (parameter_declarator->declarator,
12982 &parameter_declarator->decl_specifiers,
12983 TPARM, /*initialized=*/0,
12984 /*attrlist=*/NULL);
12985 if (parm == error_mark_node)
12986 return error_mark_node;
12987
12988 return build_tree_list (parameter_declarator->default_argument, parm);
12989 }
12990
12991 /* Parse a type-parameter.
12992
12993 type-parameter:
12994 class identifier [opt]
12995 class identifier [opt] = type-id
12996 typename identifier [opt]
12997 typename identifier [opt] = type-id
12998 template < template-parameter-list > class identifier [opt]
12999 template < template-parameter-list > class identifier [opt]
13000 = id-expression
13001
13002 GNU Extension (variadic templates):
13003
13004 type-parameter:
13005 class ... identifier [opt]
13006 typename ... identifier [opt]
13007
13008 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13009 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13010 the declaration of the parameter.
13011
13012 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13013
13014 static tree
13015 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13016 {
13017 cp_token *token;
13018 tree parameter;
13019
13020 /* Look for a keyword to tell us what kind of parameter this is. */
13021 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13022 if (!token)
13023 return error_mark_node;
13024
13025 switch (token->keyword)
13026 {
13027 case RID_CLASS:
13028 case RID_TYPENAME:
13029 {
13030 tree identifier;
13031 tree default_argument;
13032
13033 /* If the next token is an ellipsis, we have a template
13034 argument pack. */
13035 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13036 {
13037 /* Consume the `...' token. */
13038 cp_lexer_consume_token (parser->lexer);
13039 maybe_warn_variadic_templates ();
13040
13041 *is_parameter_pack = true;
13042 }
13043
13044 /* If the next token is an identifier, then it names the
13045 parameter. */
13046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13047 identifier = cp_parser_identifier (parser);
13048 else
13049 identifier = NULL_TREE;
13050
13051 /* Create the parameter. */
13052 parameter = finish_template_type_parm (class_type_node, identifier);
13053
13054 /* If the next token is an `=', we have a default argument. */
13055 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13056 {
13057 /* Consume the `=' token. */
13058 cp_lexer_consume_token (parser->lexer);
13059 /* Parse the default-argument. */
13060 push_deferring_access_checks (dk_no_deferred);
13061 default_argument = cp_parser_type_id (parser);
13062
13063 /* Template parameter packs cannot have default
13064 arguments. */
13065 if (*is_parameter_pack)
13066 {
13067 if (identifier)
13068 error_at (token->location,
13069 "template parameter pack %qD cannot have a "
13070 "default argument", identifier);
13071 else
13072 error_at (token->location,
13073 "template parameter packs cannot have "
13074 "default arguments");
13075 default_argument = NULL_TREE;
13076 }
13077 pop_deferring_access_checks ();
13078 }
13079 else
13080 default_argument = NULL_TREE;
13081
13082 /* Create the combined representation of the parameter and the
13083 default argument. */
13084 parameter = build_tree_list (default_argument, parameter);
13085 }
13086 break;
13087
13088 case RID_TEMPLATE:
13089 {
13090 tree identifier;
13091 tree default_argument;
13092
13093 /* Look for the `<'. */
13094 cp_parser_require (parser, CPP_LESS, RT_LESS);
13095 /* Parse the template-parameter-list. */
13096 cp_parser_template_parameter_list (parser);
13097 /* Look for the `>'. */
13098 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13099 /* Look for the `class' keyword. */
13100 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13101 /* If the next token is an ellipsis, we have a template
13102 argument pack. */
13103 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13104 {
13105 /* Consume the `...' token. */
13106 cp_lexer_consume_token (parser->lexer);
13107 maybe_warn_variadic_templates ();
13108
13109 *is_parameter_pack = true;
13110 }
13111 /* If the next token is an `=', then there is a
13112 default-argument. If the next token is a `>', we are at
13113 the end of the parameter-list. If the next token is a `,',
13114 then we are at the end of this parameter. */
13115 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13116 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13117 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13118 {
13119 identifier = cp_parser_identifier (parser);
13120 /* Treat invalid names as if the parameter were nameless. */
13121 if (identifier == error_mark_node)
13122 identifier = NULL_TREE;
13123 }
13124 else
13125 identifier = NULL_TREE;
13126
13127 /* Create the template parameter. */
13128 parameter = finish_template_template_parm (class_type_node,
13129 identifier);
13130
13131 /* If the next token is an `=', then there is a
13132 default-argument. */
13133 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13134 {
13135 bool is_template;
13136
13137 /* Consume the `='. */
13138 cp_lexer_consume_token (parser->lexer);
13139 /* Parse the id-expression. */
13140 push_deferring_access_checks (dk_no_deferred);
13141 /* save token before parsing the id-expression, for error
13142 reporting */
13143 token = cp_lexer_peek_token (parser->lexer);
13144 default_argument
13145 = cp_parser_id_expression (parser,
13146 /*template_keyword_p=*/false,
13147 /*check_dependency_p=*/true,
13148 /*template_p=*/&is_template,
13149 /*declarator_p=*/false,
13150 /*optional_p=*/false);
13151 if (TREE_CODE (default_argument) == TYPE_DECL)
13152 /* If the id-expression was a template-id that refers to
13153 a template-class, we already have the declaration here,
13154 so no further lookup is needed. */
13155 ;
13156 else
13157 /* Look up the name. */
13158 default_argument
13159 = cp_parser_lookup_name (parser, default_argument,
13160 none_type,
13161 /*is_template=*/is_template,
13162 /*is_namespace=*/false,
13163 /*check_dependency=*/true,
13164 /*ambiguous_decls=*/NULL,
13165 token->location);
13166 /* See if the default argument is valid. */
13167 default_argument
13168 = check_template_template_default_arg (default_argument);
13169
13170 /* Template parameter packs cannot have default
13171 arguments. */
13172 if (*is_parameter_pack)
13173 {
13174 if (identifier)
13175 error_at (token->location,
13176 "template parameter pack %qD cannot "
13177 "have a default argument",
13178 identifier);
13179 else
13180 error_at (token->location, "template parameter packs cannot "
13181 "have default arguments");
13182 default_argument = NULL_TREE;
13183 }
13184 pop_deferring_access_checks ();
13185 }
13186 else
13187 default_argument = NULL_TREE;
13188
13189 /* Create the combined representation of the parameter and the
13190 default argument. */
13191 parameter = build_tree_list (default_argument, parameter);
13192 }
13193 break;
13194
13195 default:
13196 gcc_unreachable ();
13197 break;
13198 }
13199
13200 return parameter;
13201 }
13202
13203 /* Parse a template-id.
13204
13205 template-id:
13206 template-name < template-argument-list [opt] >
13207
13208 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13209 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13210 returned. Otherwise, if the template-name names a function, or set
13211 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13212 names a class, returns a TYPE_DECL for the specialization.
13213
13214 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13215 uninstantiated templates. */
13216
13217 static tree
13218 cp_parser_template_id (cp_parser *parser,
13219 bool template_keyword_p,
13220 bool check_dependency_p,
13221 enum tag_types tag_type,
13222 bool is_declaration)
13223 {
13224 int i;
13225 tree templ;
13226 tree arguments;
13227 tree template_id;
13228 cp_token_position start_of_id = 0;
13229 deferred_access_check *chk;
13230 vec<deferred_access_check, va_gc> *access_check;
13231 cp_token *next_token = NULL, *next_token_2 = NULL;
13232 bool is_identifier;
13233
13234 /* If the next token corresponds to a template-id, there is no need
13235 to reparse it. */
13236 next_token = cp_lexer_peek_token (parser->lexer);
13237 if (next_token->type == CPP_TEMPLATE_ID)
13238 {
13239 struct tree_check *check_value;
13240
13241 /* Get the stored value. */
13242 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13243 /* Perform any access checks that were deferred. */
13244 access_check = check_value->checks;
13245 if (access_check)
13246 {
13247 FOR_EACH_VEC_ELT (*access_check, i, chk)
13248 perform_or_defer_access_check (chk->binfo,
13249 chk->decl,
13250 chk->diag_decl,
13251 tf_warning_or_error);
13252 }
13253 /* Return the stored value. */
13254 return check_value->value;
13255 }
13256
13257 /* Avoid performing name lookup if there is no possibility of
13258 finding a template-id. */
13259 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13260 || (next_token->type == CPP_NAME
13261 && !cp_parser_nth_token_starts_template_argument_list_p
13262 (parser, 2)))
13263 {
13264 cp_parser_error (parser, "expected template-id");
13265 return error_mark_node;
13266 }
13267
13268 /* Remember where the template-id starts. */
13269 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13270 start_of_id = cp_lexer_token_position (parser->lexer, false);
13271
13272 push_deferring_access_checks (dk_deferred);
13273
13274 /* Parse the template-name. */
13275 is_identifier = false;
13276 templ = cp_parser_template_name (parser, template_keyword_p,
13277 check_dependency_p,
13278 is_declaration,
13279 tag_type,
13280 &is_identifier);
13281 if (templ == error_mark_node || is_identifier)
13282 {
13283 pop_deferring_access_checks ();
13284 return templ;
13285 }
13286
13287 /* If we find the sequence `[:' after a template-name, it's probably
13288 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13289 parse correctly the argument list. */
13290 next_token = cp_lexer_peek_token (parser->lexer);
13291 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13292 if (next_token->type == CPP_OPEN_SQUARE
13293 && next_token->flags & DIGRAPH
13294 && next_token_2->type == CPP_COLON
13295 && !(next_token_2->flags & PREV_WHITE))
13296 {
13297 cp_parser_parse_tentatively (parser);
13298 /* Change `:' into `::'. */
13299 next_token_2->type = CPP_SCOPE;
13300 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13301 CPP_LESS. */
13302 cp_lexer_consume_token (parser->lexer);
13303
13304 /* Parse the arguments. */
13305 arguments = cp_parser_enclosed_template_argument_list (parser);
13306 if (!cp_parser_parse_definitely (parser))
13307 {
13308 /* If we couldn't parse an argument list, then we revert our changes
13309 and return simply an error. Maybe this is not a template-id
13310 after all. */
13311 next_token_2->type = CPP_COLON;
13312 cp_parser_error (parser, "expected %<<%>");
13313 pop_deferring_access_checks ();
13314 return error_mark_node;
13315 }
13316 /* Otherwise, emit an error about the invalid digraph, but continue
13317 parsing because we got our argument list. */
13318 if (permerror (next_token->location,
13319 "%<<::%> cannot begin a template-argument list"))
13320 {
13321 static bool hint = false;
13322 inform (next_token->location,
13323 "%<<:%> is an alternate spelling for %<[%>."
13324 " Insert whitespace between %<<%> and %<::%>");
13325 if (!hint && !flag_permissive)
13326 {
13327 inform (next_token->location, "(if you use %<-fpermissive%> "
13328 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13329 "accept your code)");
13330 hint = true;
13331 }
13332 }
13333 }
13334 else
13335 {
13336 /* Look for the `<' that starts the template-argument-list. */
13337 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13338 {
13339 pop_deferring_access_checks ();
13340 return error_mark_node;
13341 }
13342 /* Parse the arguments. */
13343 arguments = cp_parser_enclosed_template_argument_list (parser);
13344 }
13345
13346 /* Build a representation of the specialization. */
13347 if (identifier_p (templ))
13348 template_id = build_min_nt_loc (next_token->location,
13349 TEMPLATE_ID_EXPR,
13350 templ, arguments);
13351 else if (DECL_TYPE_TEMPLATE_P (templ)
13352 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13353 {
13354 bool entering_scope;
13355 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13356 template (rather than some instantiation thereof) only if
13357 is not nested within some other construct. For example, in
13358 "template <typename T> void f(T) { A<T>::", A<T> is just an
13359 instantiation of A. */
13360 entering_scope = (template_parm_scope_p ()
13361 && cp_lexer_next_token_is (parser->lexer,
13362 CPP_SCOPE));
13363 template_id
13364 = finish_template_type (templ, arguments, entering_scope);
13365 }
13366 else
13367 {
13368 /* If it's not a class-template or a template-template, it should be
13369 a function-template. */
13370 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13371 || TREE_CODE (templ) == OVERLOAD
13372 || BASELINK_P (templ)));
13373
13374 template_id = lookup_template_function (templ, arguments);
13375 }
13376
13377 /* If parsing tentatively, replace the sequence of tokens that makes
13378 up the template-id with a CPP_TEMPLATE_ID token. That way,
13379 should we re-parse the token stream, we will not have to repeat
13380 the effort required to do the parse, nor will we issue duplicate
13381 error messages about problems during instantiation of the
13382 template. */
13383 if (start_of_id)
13384 {
13385 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13386
13387 /* Reset the contents of the START_OF_ID token. */
13388 token->type = CPP_TEMPLATE_ID;
13389 /* Retrieve any deferred checks. Do not pop this access checks yet
13390 so the memory will not be reclaimed during token replacing below. */
13391 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13392 token->u.tree_check_value->value = template_id;
13393 token->u.tree_check_value->checks = get_deferred_access_checks ();
13394 token->keyword = RID_MAX;
13395
13396 /* Purge all subsequent tokens. */
13397 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13398
13399 /* ??? Can we actually assume that, if template_id ==
13400 error_mark_node, we will have issued a diagnostic to the
13401 user, as opposed to simply marking the tentative parse as
13402 failed? */
13403 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13404 error_at (token->location, "parse error in template argument list");
13405 }
13406
13407 pop_to_parent_deferring_access_checks ();
13408 return template_id;
13409 }
13410
13411 /* Parse a template-name.
13412
13413 template-name:
13414 identifier
13415
13416 The standard should actually say:
13417
13418 template-name:
13419 identifier
13420 operator-function-id
13421
13422 A defect report has been filed about this issue.
13423
13424 A conversion-function-id cannot be a template name because they cannot
13425 be part of a template-id. In fact, looking at this code:
13426
13427 a.operator K<int>()
13428
13429 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13430 It is impossible to call a templated conversion-function-id with an
13431 explicit argument list, since the only allowed template parameter is
13432 the type to which it is converting.
13433
13434 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13435 `template' keyword, in a construction like:
13436
13437 T::template f<3>()
13438
13439 In that case `f' is taken to be a template-name, even though there
13440 is no way of knowing for sure.
13441
13442 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13443 name refers to a set of overloaded functions, at least one of which
13444 is a template, or an IDENTIFIER_NODE with the name of the template,
13445 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13446 names are looked up inside uninstantiated templates. */
13447
13448 static tree
13449 cp_parser_template_name (cp_parser* parser,
13450 bool template_keyword_p,
13451 bool check_dependency_p,
13452 bool is_declaration,
13453 enum tag_types tag_type,
13454 bool *is_identifier)
13455 {
13456 tree identifier;
13457 tree decl;
13458 tree fns;
13459 cp_token *token = cp_lexer_peek_token (parser->lexer);
13460
13461 /* If the next token is `operator', then we have either an
13462 operator-function-id or a conversion-function-id. */
13463 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13464 {
13465 /* We don't know whether we're looking at an
13466 operator-function-id or a conversion-function-id. */
13467 cp_parser_parse_tentatively (parser);
13468 /* Try an operator-function-id. */
13469 identifier = cp_parser_operator_function_id (parser);
13470 /* If that didn't work, try a conversion-function-id. */
13471 if (!cp_parser_parse_definitely (parser))
13472 {
13473 cp_parser_error (parser, "expected template-name");
13474 return error_mark_node;
13475 }
13476 }
13477 /* Look for the identifier. */
13478 else
13479 identifier = cp_parser_identifier (parser);
13480
13481 /* If we didn't find an identifier, we don't have a template-id. */
13482 if (identifier == error_mark_node)
13483 return error_mark_node;
13484
13485 /* If the name immediately followed the `template' keyword, then it
13486 is a template-name. However, if the next token is not `<', then
13487 we do not treat it as a template-name, since it is not being used
13488 as part of a template-id. This enables us to handle constructs
13489 like:
13490
13491 template <typename T> struct S { S(); };
13492 template <typename T> S<T>::S();
13493
13494 correctly. We would treat `S' as a template -- if it were `S<T>'
13495 -- but we do not if there is no `<'. */
13496
13497 if (processing_template_decl
13498 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13499 {
13500 /* In a declaration, in a dependent context, we pretend that the
13501 "template" keyword was present in order to improve error
13502 recovery. For example, given:
13503
13504 template <typename T> void f(T::X<int>);
13505
13506 we want to treat "X<int>" as a template-id. */
13507 if (is_declaration
13508 && !template_keyword_p
13509 && parser->scope && TYPE_P (parser->scope)
13510 && check_dependency_p
13511 && dependent_scope_p (parser->scope)
13512 /* Do not do this for dtors (or ctors), since they never
13513 need the template keyword before their name. */
13514 && !constructor_name_p (identifier, parser->scope))
13515 {
13516 cp_token_position start = 0;
13517
13518 /* Explain what went wrong. */
13519 error_at (token->location, "non-template %qD used as template",
13520 identifier);
13521 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13522 parser->scope, identifier);
13523 /* If parsing tentatively, find the location of the "<" token. */
13524 if (cp_parser_simulate_error (parser))
13525 start = cp_lexer_token_position (parser->lexer, true);
13526 /* Parse the template arguments so that we can issue error
13527 messages about them. */
13528 cp_lexer_consume_token (parser->lexer);
13529 cp_parser_enclosed_template_argument_list (parser);
13530 /* Skip tokens until we find a good place from which to
13531 continue parsing. */
13532 cp_parser_skip_to_closing_parenthesis (parser,
13533 /*recovering=*/true,
13534 /*or_comma=*/true,
13535 /*consume_paren=*/false);
13536 /* If parsing tentatively, permanently remove the
13537 template argument list. That will prevent duplicate
13538 error messages from being issued about the missing
13539 "template" keyword. */
13540 if (start)
13541 cp_lexer_purge_tokens_after (parser->lexer, start);
13542 if (is_identifier)
13543 *is_identifier = true;
13544 return identifier;
13545 }
13546
13547 /* If the "template" keyword is present, then there is generally
13548 no point in doing name-lookup, so we just return IDENTIFIER.
13549 But, if the qualifying scope is non-dependent then we can
13550 (and must) do name-lookup normally. */
13551 if (template_keyword_p
13552 && (!parser->scope
13553 || (TYPE_P (parser->scope)
13554 && dependent_type_p (parser->scope))))
13555 return identifier;
13556 }
13557
13558 /* Look up the name. */
13559 decl = cp_parser_lookup_name (parser, identifier,
13560 tag_type,
13561 /*is_template=*/true,
13562 /*is_namespace=*/false,
13563 check_dependency_p,
13564 /*ambiguous_decls=*/NULL,
13565 token->location);
13566
13567 /* If DECL is a template, then the name was a template-name. */
13568 if (TREE_CODE (decl) == TEMPLATE_DECL)
13569 ;
13570 else
13571 {
13572 tree fn = NULL_TREE;
13573
13574 /* The standard does not explicitly indicate whether a name that
13575 names a set of overloaded declarations, some of which are
13576 templates, is a template-name. However, such a name should
13577 be a template-name; otherwise, there is no way to form a
13578 template-id for the overloaded templates. */
13579 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13580 if (TREE_CODE (fns) == OVERLOAD)
13581 for (fn = fns; fn; fn = OVL_NEXT (fn))
13582 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13583 break;
13584
13585 if (!fn)
13586 {
13587 /* The name does not name a template. */
13588 cp_parser_error (parser, "expected template-name");
13589 return error_mark_node;
13590 }
13591 }
13592
13593 /* If DECL is dependent, and refers to a function, then just return
13594 its name; we will look it up again during template instantiation. */
13595 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13596 {
13597 tree scope = ovl_scope (decl);
13598 if (TYPE_P (scope) && dependent_type_p (scope))
13599 return identifier;
13600 }
13601
13602 return decl;
13603 }
13604
13605 /* Parse a template-argument-list.
13606
13607 template-argument-list:
13608 template-argument ... [opt]
13609 template-argument-list , template-argument ... [opt]
13610
13611 Returns a TREE_VEC containing the arguments. */
13612
13613 static tree
13614 cp_parser_template_argument_list (cp_parser* parser)
13615 {
13616 tree fixed_args[10];
13617 unsigned n_args = 0;
13618 unsigned alloced = 10;
13619 tree *arg_ary = fixed_args;
13620 tree vec;
13621 bool saved_in_template_argument_list_p;
13622 bool saved_ice_p;
13623 bool saved_non_ice_p;
13624
13625 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13626 parser->in_template_argument_list_p = true;
13627 /* Even if the template-id appears in an integral
13628 constant-expression, the contents of the argument list do
13629 not. */
13630 saved_ice_p = parser->integral_constant_expression_p;
13631 parser->integral_constant_expression_p = false;
13632 saved_non_ice_p = parser->non_integral_constant_expression_p;
13633 parser->non_integral_constant_expression_p = false;
13634
13635 /* Parse the arguments. */
13636 do
13637 {
13638 tree argument;
13639
13640 if (n_args)
13641 /* Consume the comma. */
13642 cp_lexer_consume_token (parser->lexer);
13643
13644 /* Parse the template-argument. */
13645 argument = cp_parser_template_argument (parser);
13646
13647 /* If the next token is an ellipsis, we're expanding a template
13648 argument pack. */
13649 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13650 {
13651 if (argument == error_mark_node)
13652 {
13653 cp_token *token = cp_lexer_peek_token (parser->lexer);
13654 error_at (token->location,
13655 "expected parameter pack before %<...%>");
13656 }
13657 /* Consume the `...' token. */
13658 cp_lexer_consume_token (parser->lexer);
13659
13660 /* Make the argument into a TYPE_PACK_EXPANSION or
13661 EXPR_PACK_EXPANSION. */
13662 argument = make_pack_expansion (argument);
13663 }
13664
13665 if (n_args == alloced)
13666 {
13667 alloced *= 2;
13668
13669 if (arg_ary == fixed_args)
13670 {
13671 arg_ary = XNEWVEC (tree, alloced);
13672 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13673 }
13674 else
13675 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13676 }
13677 arg_ary[n_args++] = argument;
13678 }
13679 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13680
13681 vec = make_tree_vec (n_args);
13682
13683 while (n_args--)
13684 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13685
13686 if (arg_ary != fixed_args)
13687 free (arg_ary);
13688 parser->non_integral_constant_expression_p = saved_non_ice_p;
13689 parser->integral_constant_expression_p = saved_ice_p;
13690 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13691 #ifdef ENABLE_CHECKING
13692 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13693 #endif
13694 return vec;
13695 }
13696
13697 /* Parse a template-argument.
13698
13699 template-argument:
13700 assignment-expression
13701 type-id
13702 id-expression
13703
13704 The representation is that of an assignment-expression, type-id, or
13705 id-expression -- except that the qualified id-expression is
13706 evaluated, so that the value returned is either a DECL or an
13707 OVERLOAD.
13708
13709 Although the standard says "assignment-expression", it forbids
13710 throw-expressions or assignments in the template argument.
13711 Therefore, we use "conditional-expression" instead. */
13712
13713 static tree
13714 cp_parser_template_argument (cp_parser* parser)
13715 {
13716 tree argument;
13717 bool template_p;
13718 bool address_p;
13719 bool maybe_type_id = false;
13720 cp_token *token = NULL, *argument_start_token = NULL;
13721 location_t loc = 0;
13722 cp_id_kind idk;
13723
13724 /* There's really no way to know what we're looking at, so we just
13725 try each alternative in order.
13726
13727 [temp.arg]
13728
13729 In a template-argument, an ambiguity between a type-id and an
13730 expression is resolved to a type-id, regardless of the form of
13731 the corresponding template-parameter.
13732
13733 Therefore, we try a type-id first. */
13734 cp_parser_parse_tentatively (parser);
13735 argument = cp_parser_template_type_arg (parser);
13736 /* If there was no error parsing the type-id but the next token is a
13737 '>>', our behavior depends on which dialect of C++ we're
13738 parsing. In C++98, we probably found a typo for '> >'. But there
13739 are type-id which are also valid expressions. For instance:
13740
13741 struct X { int operator >> (int); };
13742 template <int V> struct Foo {};
13743 Foo<X () >> 5> r;
13744
13745 Here 'X()' is a valid type-id of a function type, but the user just
13746 wanted to write the expression "X() >> 5". Thus, we remember that we
13747 found a valid type-id, but we still try to parse the argument as an
13748 expression to see what happens.
13749
13750 In C++0x, the '>>' will be considered two separate '>'
13751 tokens. */
13752 if (!cp_parser_error_occurred (parser)
13753 && cxx_dialect == cxx98
13754 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13755 {
13756 maybe_type_id = true;
13757 cp_parser_abort_tentative_parse (parser);
13758 }
13759 else
13760 {
13761 /* If the next token isn't a `,' or a `>', then this argument wasn't
13762 really finished. This means that the argument is not a valid
13763 type-id. */
13764 if (!cp_parser_next_token_ends_template_argument_p (parser))
13765 cp_parser_error (parser, "expected template-argument");
13766 /* If that worked, we're done. */
13767 if (cp_parser_parse_definitely (parser))
13768 return argument;
13769 }
13770 /* We're still not sure what the argument will be. */
13771 cp_parser_parse_tentatively (parser);
13772 /* Try a template. */
13773 argument_start_token = cp_lexer_peek_token (parser->lexer);
13774 argument = cp_parser_id_expression (parser,
13775 /*template_keyword_p=*/false,
13776 /*check_dependency_p=*/true,
13777 &template_p,
13778 /*declarator_p=*/false,
13779 /*optional_p=*/false);
13780 /* If the next token isn't a `,' or a `>', then this argument wasn't
13781 really finished. */
13782 if (!cp_parser_next_token_ends_template_argument_p (parser))
13783 cp_parser_error (parser, "expected template-argument");
13784 if (!cp_parser_error_occurred (parser))
13785 {
13786 /* Figure out what is being referred to. If the id-expression
13787 was for a class template specialization, then we will have a
13788 TYPE_DECL at this point. There is no need to do name lookup
13789 at this point in that case. */
13790 if (TREE_CODE (argument) != TYPE_DECL)
13791 argument = cp_parser_lookup_name (parser, argument,
13792 none_type,
13793 /*is_template=*/template_p,
13794 /*is_namespace=*/false,
13795 /*check_dependency=*/true,
13796 /*ambiguous_decls=*/NULL,
13797 argument_start_token->location);
13798 if (TREE_CODE (argument) != TEMPLATE_DECL
13799 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13800 cp_parser_error (parser, "expected template-name");
13801 }
13802 if (cp_parser_parse_definitely (parser))
13803 return argument;
13804 /* It must be a non-type argument. There permitted cases are given
13805 in [temp.arg.nontype]:
13806
13807 -- an integral constant-expression of integral or enumeration
13808 type; or
13809
13810 -- the name of a non-type template-parameter; or
13811
13812 -- the name of an object or function with external linkage...
13813
13814 -- the address of an object or function with external linkage...
13815
13816 -- a pointer to member... */
13817 /* Look for a non-type template parameter. */
13818 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13819 {
13820 cp_parser_parse_tentatively (parser);
13821 argument = cp_parser_primary_expression (parser,
13822 /*address_p=*/false,
13823 /*cast_p=*/false,
13824 /*template_arg_p=*/true,
13825 &idk);
13826 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13827 || !cp_parser_next_token_ends_template_argument_p (parser))
13828 cp_parser_simulate_error (parser);
13829 if (cp_parser_parse_definitely (parser))
13830 return argument;
13831 }
13832
13833 /* If the next token is "&", the argument must be the address of an
13834 object or function with external linkage. */
13835 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13836 if (address_p)
13837 {
13838 loc = cp_lexer_peek_token (parser->lexer)->location;
13839 cp_lexer_consume_token (parser->lexer);
13840 }
13841 /* See if we might have an id-expression. */
13842 token = cp_lexer_peek_token (parser->lexer);
13843 if (token->type == CPP_NAME
13844 || token->keyword == RID_OPERATOR
13845 || token->type == CPP_SCOPE
13846 || token->type == CPP_TEMPLATE_ID
13847 || token->type == CPP_NESTED_NAME_SPECIFIER)
13848 {
13849 cp_parser_parse_tentatively (parser);
13850 argument = cp_parser_primary_expression (parser,
13851 address_p,
13852 /*cast_p=*/false,
13853 /*template_arg_p=*/true,
13854 &idk);
13855 if (cp_parser_error_occurred (parser)
13856 || !cp_parser_next_token_ends_template_argument_p (parser))
13857 cp_parser_abort_tentative_parse (parser);
13858 else
13859 {
13860 tree probe;
13861
13862 if (INDIRECT_REF_P (argument))
13863 {
13864 gcc_assert (REFERENCE_REF_P (argument));
13865 argument = TREE_OPERAND (argument, 0);
13866 }
13867
13868 /* If we're in a template, we represent a qualified-id referring
13869 to a static data member as a SCOPE_REF even if the scope isn't
13870 dependent so that we can check access control later. */
13871 probe = argument;
13872 if (TREE_CODE (probe) == SCOPE_REF)
13873 probe = TREE_OPERAND (probe, 1);
13874 if (VAR_P (probe))
13875 {
13876 /* A variable without external linkage might still be a
13877 valid constant-expression, so no error is issued here
13878 if the external-linkage check fails. */
13879 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13880 cp_parser_simulate_error (parser);
13881 }
13882 else if (is_overloaded_fn (argument))
13883 /* All overloaded functions are allowed; if the external
13884 linkage test does not pass, an error will be issued
13885 later. */
13886 ;
13887 else if (address_p
13888 && (TREE_CODE (argument) == OFFSET_REF
13889 || TREE_CODE (argument) == SCOPE_REF))
13890 /* A pointer-to-member. */
13891 ;
13892 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13893 ;
13894 else
13895 cp_parser_simulate_error (parser);
13896
13897 if (cp_parser_parse_definitely (parser))
13898 {
13899 if (address_p)
13900 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13901 tf_warning_or_error);
13902 return argument;
13903 }
13904 }
13905 }
13906 /* If the argument started with "&", there are no other valid
13907 alternatives at this point. */
13908 if (address_p)
13909 {
13910 cp_parser_error (parser, "invalid non-type template argument");
13911 return error_mark_node;
13912 }
13913
13914 /* If the argument wasn't successfully parsed as a type-id followed
13915 by '>>', the argument can only be a constant expression now.
13916 Otherwise, we try parsing the constant-expression tentatively,
13917 because the argument could really be a type-id. */
13918 if (maybe_type_id)
13919 cp_parser_parse_tentatively (parser);
13920 argument = cp_parser_constant_expression (parser,
13921 /*allow_non_constant_p=*/false,
13922 /*non_constant_p=*/NULL);
13923 if (!maybe_type_id)
13924 return argument;
13925 if (!cp_parser_next_token_ends_template_argument_p (parser))
13926 cp_parser_error (parser, "expected template-argument");
13927 if (cp_parser_parse_definitely (parser))
13928 return argument;
13929 /* We did our best to parse the argument as a non type-id, but that
13930 was the only alternative that matched (albeit with a '>' after
13931 it). We can assume it's just a typo from the user, and a
13932 diagnostic will then be issued. */
13933 return cp_parser_template_type_arg (parser);
13934 }
13935
13936 /* Parse an explicit-instantiation.
13937
13938 explicit-instantiation:
13939 template declaration
13940
13941 Although the standard says `declaration', what it really means is:
13942
13943 explicit-instantiation:
13944 template decl-specifier-seq [opt] declarator [opt] ;
13945
13946 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13947 supposed to be allowed. A defect report has been filed about this
13948 issue.
13949
13950 GNU Extension:
13951
13952 explicit-instantiation:
13953 storage-class-specifier template
13954 decl-specifier-seq [opt] declarator [opt] ;
13955 function-specifier template
13956 decl-specifier-seq [opt] declarator [opt] ; */
13957
13958 static void
13959 cp_parser_explicit_instantiation (cp_parser* parser)
13960 {
13961 int declares_class_or_enum;
13962 cp_decl_specifier_seq decl_specifiers;
13963 tree extension_specifier = NULL_TREE;
13964
13965 timevar_push (TV_TEMPLATE_INST);
13966
13967 /* Look for an (optional) storage-class-specifier or
13968 function-specifier. */
13969 if (cp_parser_allow_gnu_extensions_p (parser))
13970 {
13971 extension_specifier
13972 = cp_parser_storage_class_specifier_opt (parser);
13973 if (!extension_specifier)
13974 extension_specifier
13975 = cp_parser_function_specifier_opt (parser,
13976 /*decl_specs=*/NULL);
13977 }
13978
13979 /* Look for the `template' keyword. */
13980 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13981 /* Let the front end know that we are processing an explicit
13982 instantiation. */
13983 begin_explicit_instantiation ();
13984 /* [temp.explicit] says that we are supposed to ignore access
13985 control while processing explicit instantiation directives. */
13986 push_deferring_access_checks (dk_no_check);
13987 /* Parse a decl-specifier-seq. */
13988 cp_parser_decl_specifier_seq (parser,
13989 CP_PARSER_FLAGS_OPTIONAL,
13990 &decl_specifiers,
13991 &declares_class_or_enum);
13992 /* If there was exactly one decl-specifier, and it declared a class,
13993 and there's no declarator, then we have an explicit type
13994 instantiation. */
13995 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13996 {
13997 tree type;
13998
13999 type = check_tag_decl (&decl_specifiers,
14000 /*explicit_type_instantiation_p=*/true);
14001 /* Turn access control back on for names used during
14002 template instantiation. */
14003 pop_deferring_access_checks ();
14004 if (type)
14005 do_type_instantiation (type, extension_specifier,
14006 /*complain=*/tf_error);
14007 }
14008 else
14009 {
14010 cp_declarator *declarator;
14011 tree decl;
14012
14013 /* Parse the declarator. */
14014 declarator
14015 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14016 /*ctor_dtor_or_conv_p=*/NULL,
14017 /*parenthesized_p=*/NULL,
14018 /*member_p=*/false);
14019 if (declares_class_or_enum & 2)
14020 cp_parser_check_for_definition_in_return_type (declarator,
14021 decl_specifiers.type,
14022 decl_specifiers.locations[ds_type_spec]);
14023 if (declarator != cp_error_declarator)
14024 {
14025 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14026 permerror (decl_specifiers.locations[ds_inline],
14027 "explicit instantiation shall not use"
14028 " %<inline%> specifier");
14029 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14030 permerror (decl_specifiers.locations[ds_constexpr],
14031 "explicit instantiation shall not use"
14032 " %<constexpr%> specifier");
14033
14034 decl = grokdeclarator (declarator, &decl_specifiers,
14035 NORMAL, 0, &decl_specifiers.attributes);
14036 /* Turn access control back on for names used during
14037 template instantiation. */
14038 pop_deferring_access_checks ();
14039 /* Do the explicit instantiation. */
14040 do_decl_instantiation (decl, extension_specifier);
14041 }
14042 else
14043 {
14044 pop_deferring_access_checks ();
14045 /* Skip the body of the explicit instantiation. */
14046 cp_parser_skip_to_end_of_statement (parser);
14047 }
14048 }
14049 /* We're done with the instantiation. */
14050 end_explicit_instantiation ();
14051
14052 cp_parser_consume_semicolon_at_end_of_statement (parser);
14053
14054 timevar_pop (TV_TEMPLATE_INST);
14055 }
14056
14057 /* Parse an explicit-specialization.
14058
14059 explicit-specialization:
14060 template < > declaration
14061
14062 Although the standard says `declaration', what it really means is:
14063
14064 explicit-specialization:
14065 template <> decl-specifier [opt] init-declarator [opt] ;
14066 template <> function-definition
14067 template <> explicit-specialization
14068 template <> template-declaration */
14069
14070 static void
14071 cp_parser_explicit_specialization (cp_parser* parser)
14072 {
14073 bool need_lang_pop;
14074 cp_token *token = cp_lexer_peek_token (parser->lexer);
14075
14076 /* Look for the `template' keyword. */
14077 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14078 /* Look for the `<'. */
14079 cp_parser_require (parser, CPP_LESS, RT_LESS);
14080 /* Look for the `>'. */
14081 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14082 /* We have processed another parameter list. */
14083 ++parser->num_template_parameter_lists;
14084 /* [temp]
14085
14086 A template ... explicit specialization ... shall not have C
14087 linkage. */
14088 if (current_lang_name == lang_name_c)
14089 {
14090 error_at (token->location, "template specialization with C linkage");
14091 /* Give it C++ linkage to avoid confusing other parts of the
14092 front end. */
14093 push_lang_context (lang_name_cplusplus);
14094 need_lang_pop = true;
14095 }
14096 else
14097 need_lang_pop = false;
14098 /* Let the front end know that we are beginning a specialization. */
14099 if (!begin_specialization ())
14100 {
14101 end_specialization ();
14102 return;
14103 }
14104
14105 /* If the next keyword is `template', we need to figure out whether
14106 or not we're looking a template-declaration. */
14107 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14108 {
14109 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14110 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14111 cp_parser_template_declaration_after_export (parser,
14112 /*member_p=*/false);
14113 else
14114 cp_parser_explicit_specialization (parser);
14115 }
14116 else
14117 /* Parse the dependent declaration. */
14118 cp_parser_single_declaration (parser,
14119 /*checks=*/NULL,
14120 /*member_p=*/false,
14121 /*explicit_specialization_p=*/true,
14122 /*friend_p=*/NULL);
14123 /* We're done with the specialization. */
14124 end_specialization ();
14125 /* For the erroneous case of a template with C linkage, we pushed an
14126 implicit C++ linkage scope; exit that scope now. */
14127 if (need_lang_pop)
14128 pop_lang_context ();
14129 /* We're done with this parameter list. */
14130 --parser->num_template_parameter_lists;
14131 }
14132
14133 /* Parse a type-specifier.
14134
14135 type-specifier:
14136 simple-type-specifier
14137 class-specifier
14138 enum-specifier
14139 elaborated-type-specifier
14140 cv-qualifier
14141
14142 GNU Extension:
14143
14144 type-specifier:
14145 __complex__
14146
14147 Returns a representation of the type-specifier. For a
14148 class-specifier, enum-specifier, or elaborated-type-specifier, a
14149 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14150
14151 The parser flags FLAGS is used to control type-specifier parsing.
14152
14153 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14154 in a decl-specifier-seq.
14155
14156 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14157 class-specifier, enum-specifier, or elaborated-type-specifier, then
14158 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14159 if a type is declared; 2 if it is defined. Otherwise, it is set to
14160 zero.
14161
14162 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14163 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14164 is set to FALSE. */
14165
14166 static tree
14167 cp_parser_type_specifier (cp_parser* parser,
14168 cp_parser_flags flags,
14169 cp_decl_specifier_seq *decl_specs,
14170 bool is_declaration,
14171 int* declares_class_or_enum,
14172 bool* is_cv_qualifier)
14173 {
14174 tree type_spec = NULL_TREE;
14175 cp_token *token;
14176 enum rid keyword;
14177 cp_decl_spec ds = ds_last;
14178
14179 /* Assume this type-specifier does not declare a new type. */
14180 if (declares_class_or_enum)
14181 *declares_class_or_enum = 0;
14182 /* And that it does not specify a cv-qualifier. */
14183 if (is_cv_qualifier)
14184 *is_cv_qualifier = false;
14185 /* Peek at the next token. */
14186 token = cp_lexer_peek_token (parser->lexer);
14187
14188 /* If we're looking at a keyword, we can use that to guide the
14189 production we choose. */
14190 keyword = token->keyword;
14191 switch (keyword)
14192 {
14193 case RID_ENUM:
14194 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14195 goto elaborated_type_specifier;
14196
14197 /* Look for the enum-specifier. */
14198 type_spec = cp_parser_enum_specifier (parser);
14199 /* If that worked, we're done. */
14200 if (type_spec)
14201 {
14202 if (declares_class_or_enum)
14203 *declares_class_or_enum = 2;
14204 if (decl_specs)
14205 cp_parser_set_decl_spec_type (decl_specs,
14206 type_spec,
14207 token,
14208 /*type_definition_p=*/true);
14209 return type_spec;
14210 }
14211 else
14212 goto elaborated_type_specifier;
14213
14214 /* Any of these indicate either a class-specifier, or an
14215 elaborated-type-specifier. */
14216 case RID_CLASS:
14217 case RID_STRUCT:
14218 case RID_UNION:
14219 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14220 goto elaborated_type_specifier;
14221
14222 /* Parse tentatively so that we can back up if we don't find a
14223 class-specifier. */
14224 cp_parser_parse_tentatively (parser);
14225 /* Look for the class-specifier. */
14226 type_spec = cp_parser_class_specifier (parser);
14227 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14228 /* If that worked, we're done. */
14229 if (cp_parser_parse_definitely (parser))
14230 {
14231 if (declares_class_or_enum)
14232 *declares_class_or_enum = 2;
14233 if (decl_specs)
14234 cp_parser_set_decl_spec_type (decl_specs,
14235 type_spec,
14236 token,
14237 /*type_definition_p=*/true);
14238 return type_spec;
14239 }
14240
14241 /* Fall through. */
14242 elaborated_type_specifier:
14243 /* We're declaring (not defining) a class or enum. */
14244 if (declares_class_or_enum)
14245 *declares_class_or_enum = 1;
14246
14247 /* Fall through. */
14248 case RID_TYPENAME:
14249 /* Look for an elaborated-type-specifier. */
14250 type_spec
14251 = (cp_parser_elaborated_type_specifier
14252 (parser,
14253 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14254 is_declaration));
14255 if (decl_specs)
14256 cp_parser_set_decl_spec_type (decl_specs,
14257 type_spec,
14258 token,
14259 /*type_definition_p=*/false);
14260 return type_spec;
14261
14262 case RID_CONST:
14263 ds = ds_const;
14264 if (is_cv_qualifier)
14265 *is_cv_qualifier = true;
14266 break;
14267
14268 case RID_VOLATILE:
14269 ds = ds_volatile;
14270 if (is_cv_qualifier)
14271 *is_cv_qualifier = true;
14272 break;
14273
14274 case RID_RESTRICT:
14275 ds = ds_restrict;
14276 if (is_cv_qualifier)
14277 *is_cv_qualifier = true;
14278 break;
14279
14280 case RID_COMPLEX:
14281 /* The `__complex__' keyword is a GNU extension. */
14282 ds = ds_complex;
14283 break;
14284
14285 default:
14286 break;
14287 }
14288
14289 /* Handle simple keywords. */
14290 if (ds != ds_last)
14291 {
14292 if (decl_specs)
14293 {
14294 set_and_check_decl_spec_loc (decl_specs, ds, token);
14295 decl_specs->any_specifiers_p = true;
14296 }
14297 return cp_lexer_consume_token (parser->lexer)->u.value;
14298 }
14299
14300 /* If we do not already have a type-specifier, assume we are looking
14301 at a simple-type-specifier. */
14302 type_spec = cp_parser_simple_type_specifier (parser,
14303 decl_specs,
14304 flags);
14305
14306 /* If we didn't find a type-specifier, and a type-specifier was not
14307 optional in this context, issue an error message. */
14308 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14309 {
14310 cp_parser_error (parser, "expected type specifier");
14311 return error_mark_node;
14312 }
14313
14314 return type_spec;
14315 }
14316
14317 /* Parse a simple-type-specifier.
14318
14319 simple-type-specifier:
14320 :: [opt] nested-name-specifier [opt] type-name
14321 :: [opt] nested-name-specifier template template-id
14322 char
14323 wchar_t
14324 bool
14325 short
14326 int
14327 long
14328 signed
14329 unsigned
14330 float
14331 double
14332 void
14333
14334 C++0x Extension:
14335
14336 simple-type-specifier:
14337 auto
14338 decltype ( expression )
14339 char16_t
14340 char32_t
14341 __underlying_type ( type-id )
14342
14343 GNU Extension:
14344
14345 simple-type-specifier:
14346 __int128
14347 __typeof__ unary-expression
14348 __typeof__ ( type-id )
14349 __typeof__ ( type-id ) { initializer-list , [opt] }
14350
14351 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14352 appropriately updated. */
14353
14354 static tree
14355 cp_parser_simple_type_specifier (cp_parser* parser,
14356 cp_decl_specifier_seq *decl_specs,
14357 cp_parser_flags flags)
14358 {
14359 tree type = NULL_TREE;
14360 cp_token *token;
14361
14362 /* Peek at the next token. */
14363 token = cp_lexer_peek_token (parser->lexer);
14364
14365 /* If we're looking at a keyword, things are easy. */
14366 switch (token->keyword)
14367 {
14368 case RID_CHAR:
14369 if (decl_specs)
14370 decl_specs->explicit_char_p = true;
14371 type = char_type_node;
14372 break;
14373 case RID_CHAR16:
14374 type = char16_type_node;
14375 break;
14376 case RID_CHAR32:
14377 type = char32_type_node;
14378 break;
14379 case RID_WCHAR:
14380 type = wchar_type_node;
14381 break;
14382 case RID_BOOL:
14383 type = boolean_type_node;
14384 break;
14385 case RID_SHORT:
14386 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14387 type = short_integer_type_node;
14388 break;
14389 case RID_INT:
14390 if (decl_specs)
14391 decl_specs->explicit_int_p = true;
14392 type = integer_type_node;
14393 break;
14394 case RID_INT128:
14395 if (!int128_integer_type_node)
14396 break;
14397 if (decl_specs)
14398 decl_specs->explicit_int128_p = true;
14399 type = int128_integer_type_node;
14400 break;
14401 case RID_LONG:
14402 if (decl_specs)
14403 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14404 type = long_integer_type_node;
14405 break;
14406 case RID_SIGNED:
14407 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14408 type = integer_type_node;
14409 break;
14410 case RID_UNSIGNED:
14411 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14412 type = unsigned_type_node;
14413 break;
14414 case RID_FLOAT:
14415 type = float_type_node;
14416 break;
14417 case RID_DOUBLE:
14418 type = double_type_node;
14419 break;
14420 case RID_VOID:
14421 type = void_type_node;
14422 break;
14423
14424 case RID_AUTO:
14425 maybe_warn_cpp0x (CPP0X_AUTO);
14426 if (parser->auto_is_implicit_function_template_parm_p)
14427 {
14428 type = synthesize_implicit_template_parm (parser);
14429
14430 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14431 {
14432 if (cxx_dialect < cxx1y)
14433 pedwarn (location_of (type), 0,
14434 "use of %<auto%> in lambda parameter declaration "
14435 "only available with "
14436 "-std=c++1y or -std=gnu++1y");
14437 }
14438 else if (cxx_dialect < cxx1y)
14439 pedwarn (location_of (type), 0,
14440 "use of %<auto%> in parameter declaration "
14441 "only available with "
14442 "-std=c++1y or -std=gnu++1y");
14443 else
14444 pedwarn (location_of (type), OPT_Wpedantic,
14445 "ISO C++ forbids use of %<auto%> in parameter "
14446 "declaration");
14447 }
14448 else
14449 type = make_auto ();
14450 break;
14451
14452 case RID_DECLTYPE:
14453 /* Since DR 743, decltype can either be a simple-type-specifier by
14454 itself or begin a nested-name-specifier. Parsing it will replace
14455 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14456 handling below decide what to do. */
14457 cp_parser_decltype (parser);
14458 cp_lexer_set_token_position (parser->lexer, token);
14459 break;
14460
14461 case RID_TYPEOF:
14462 /* Consume the `typeof' token. */
14463 cp_lexer_consume_token (parser->lexer);
14464 /* Parse the operand to `typeof'. */
14465 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14466 /* If it is not already a TYPE, take its type. */
14467 if (!TYPE_P (type))
14468 type = finish_typeof (type);
14469
14470 if (decl_specs)
14471 cp_parser_set_decl_spec_type (decl_specs, type,
14472 token,
14473 /*type_definition_p=*/false);
14474
14475 return type;
14476
14477 case RID_UNDERLYING_TYPE:
14478 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14479 if (decl_specs)
14480 cp_parser_set_decl_spec_type (decl_specs, type,
14481 token,
14482 /*type_definition_p=*/false);
14483
14484 return type;
14485
14486 case RID_BASES:
14487 case RID_DIRECT_BASES:
14488 type = cp_parser_trait_expr (parser, token->keyword);
14489 if (decl_specs)
14490 cp_parser_set_decl_spec_type (decl_specs, type,
14491 token,
14492 /*type_definition_p=*/false);
14493 return type;
14494 default:
14495 break;
14496 }
14497
14498 /* If token is an already-parsed decltype not followed by ::,
14499 it's a simple-type-specifier. */
14500 if (token->type == CPP_DECLTYPE
14501 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14502 {
14503 type = token->u.value;
14504 if (decl_specs)
14505 cp_parser_set_decl_spec_type (decl_specs, type,
14506 token,
14507 /*type_definition_p=*/false);
14508 cp_lexer_consume_token (parser->lexer);
14509 return type;
14510 }
14511
14512 /* If the type-specifier was for a built-in type, we're done. */
14513 if (type)
14514 {
14515 /* Record the type. */
14516 if (decl_specs
14517 && (token->keyword != RID_SIGNED
14518 && token->keyword != RID_UNSIGNED
14519 && token->keyword != RID_SHORT
14520 && token->keyword != RID_LONG))
14521 cp_parser_set_decl_spec_type (decl_specs,
14522 type,
14523 token,
14524 /*type_definition_p=*/false);
14525 if (decl_specs)
14526 decl_specs->any_specifiers_p = true;
14527
14528 /* Consume the token. */
14529 cp_lexer_consume_token (parser->lexer);
14530
14531 /* There is no valid C++ program where a non-template type is
14532 followed by a "<". That usually indicates that the user thought
14533 that the type was a template. */
14534 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14535 token->location);
14536
14537 return TYPE_NAME (type);
14538 }
14539
14540 /* The type-specifier must be a user-defined type. */
14541 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14542 {
14543 bool qualified_p;
14544 bool global_p;
14545
14546 /* Don't gobble tokens or issue error messages if this is an
14547 optional type-specifier. */
14548 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14549 cp_parser_parse_tentatively (parser);
14550
14551 /* Look for the optional `::' operator. */
14552 global_p
14553 = (cp_parser_global_scope_opt (parser,
14554 /*current_scope_valid_p=*/false)
14555 != NULL_TREE);
14556 /* Look for the nested-name specifier. */
14557 qualified_p
14558 = (cp_parser_nested_name_specifier_opt (parser,
14559 /*typename_keyword_p=*/false,
14560 /*check_dependency_p=*/true,
14561 /*type_p=*/false,
14562 /*is_declaration=*/false)
14563 != NULL_TREE);
14564 token = cp_lexer_peek_token (parser->lexer);
14565 /* If we have seen a nested-name-specifier, and the next token
14566 is `template', then we are using the template-id production. */
14567 if (parser->scope
14568 && cp_parser_optional_template_keyword (parser))
14569 {
14570 /* Look for the template-id. */
14571 type = cp_parser_template_id (parser,
14572 /*template_keyword_p=*/true,
14573 /*check_dependency_p=*/true,
14574 none_type,
14575 /*is_declaration=*/false);
14576 /* If the template-id did not name a type, we are out of
14577 luck. */
14578 if (TREE_CODE (type) != TYPE_DECL)
14579 {
14580 cp_parser_error (parser, "expected template-id for type");
14581 type = NULL_TREE;
14582 }
14583 }
14584 /* Otherwise, look for a type-name. */
14585 else
14586 type = cp_parser_type_name (parser);
14587 /* Keep track of all name-lookups performed in class scopes. */
14588 if (type
14589 && !global_p
14590 && !qualified_p
14591 && TREE_CODE (type) == TYPE_DECL
14592 && identifier_p (DECL_NAME (type)))
14593 maybe_note_name_used_in_class (DECL_NAME (type), type);
14594 /* If it didn't work out, we don't have a TYPE. */
14595 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14596 && !cp_parser_parse_definitely (parser))
14597 type = NULL_TREE;
14598 if (type && decl_specs)
14599 cp_parser_set_decl_spec_type (decl_specs, type,
14600 token,
14601 /*type_definition_p=*/false);
14602 }
14603
14604 /* If we didn't get a type-name, issue an error message. */
14605 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14606 {
14607 cp_parser_error (parser, "expected type-name");
14608 return error_mark_node;
14609 }
14610
14611 if (type && type != error_mark_node)
14612 {
14613 /* See if TYPE is an Objective-C type, and if so, parse and
14614 accept any protocol references following it. Do this before
14615 the cp_parser_check_for_invalid_template_id() call, because
14616 Objective-C types can be followed by '<...>' which would
14617 enclose protocol names rather than template arguments, and so
14618 everything is fine. */
14619 if (c_dialect_objc () && !parser->scope
14620 && (objc_is_id (type) || objc_is_class_name (type)))
14621 {
14622 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14623 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14624
14625 /* Clobber the "unqualified" type previously entered into
14626 DECL_SPECS with the new, improved protocol-qualified version. */
14627 if (decl_specs)
14628 decl_specs->type = qual_type;
14629
14630 return qual_type;
14631 }
14632
14633 /* There is no valid C++ program where a non-template type is
14634 followed by a "<". That usually indicates that the user
14635 thought that the type was a template. */
14636 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14637 none_type,
14638 token->location);
14639 }
14640
14641 return type;
14642 }
14643
14644 /* Parse a type-name.
14645
14646 type-name:
14647 class-name
14648 enum-name
14649 typedef-name
14650 simple-template-id [in c++0x]
14651
14652 enum-name:
14653 identifier
14654
14655 typedef-name:
14656 identifier
14657
14658 Returns a TYPE_DECL for the type. */
14659
14660 static tree
14661 cp_parser_type_name (cp_parser* parser)
14662 {
14663 tree type_decl;
14664
14665 /* We can't know yet whether it is a class-name or not. */
14666 cp_parser_parse_tentatively (parser);
14667 /* Try a class-name. */
14668 type_decl = cp_parser_class_name (parser,
14669 /*typename_keyword_p=*/false,
14670 /*template_keyword_p=*/false,
14671 none_type,
14672 /*check_dependency_p=*/true,
14673 /*class_head_p=*/false,
14674 /*is_declaration=*/false);
14675 /* If it's not a class-name, keep looking. */
14676 if (!cp_parser_parse_definitely (parser))
14677 {
14678 if (cxx_dialect < cxx11)
14679 /* It must be a typedef-name or an enum-name. */
14680 return cp_parser_nonclass_name (parser);
14681
14682 cp_parser_parse_tentatively (parser);
14683 /* It is either a simple-template-id representing an
14684 instantiation of an alias template... */
14685 type_decl = cp_parser_template_id (parser,
14686 /*template_keyword_p=*/false,
14687 /*check_dependency_p=*/false,
14688 none_type,
14689 /*is_declaration=*/false);
14690 /* Note that this must be an instantiation of an alias template
14691 because [temp.names]/6 says:
14692
14693 A template-id that names an alias template specialization
14694 is a type-name.
14695
14696 Whereas [temp.names]/7 says:
14697
14698 A simple-template-id that names a class template
14699 specialization is a class-name. */
14700 if (type_decl != NULL_TREE
14701 && TREE_CODE (type_decl) == TYPE_DECL
14702 && TYPE_DECL_ALIAS_P (type_decl))
14703 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14704 else
14705 cp_parser_simulate_error (parser);
14706
14707 if (!cp_parser_parse_definitely (parser))
14708 /* ... Or a typedef-name or an enum-name. */
14709 return cp_parser_nonclass_name (parser);
14710 }
14711
14712 return type_decl;
14713 }
14714
14715 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14716
14717 enum-name:
14718 identifier
14719
14720 typedef-name:
14721 identifier
14722
14723 Returns a TYPE_DECL for the type. */
14724
14725 static tree
14726 cp_parser_nonclass_name (cp_parser* parser)
14727 {
14728 tree type_decl;
14729 tree identifier;
14730
14731 cp_token *token = cp_lexer_peek_token (parser->lexer);
14732 identifier = cp_parser_identifier (parser);
14733 if (identifier == error_mark_node)
14734 return error_mark_node;
14735
14736 /* Look up the type-name. */
14737 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14738
14739 if (TREE_CODE (type_decl) == USING_DECL)
14740 {
14741 if (!DECL_DEPENDENT_P (type_decl))
14742 type_decl = strip_using_decl (type_decl);
14743 else if (USING_DECL_TYPENAME_P (type_decl))
14744 {
14745 /* We have found a type introduced by a using
14746 declaration at class scope that refers to a dependent
14747 type.
14748
14749 using typename :: [opt] nested-name-specifier unqualified-id ;
14750 */
14751 type_decl = make_typename_type (TREE_TYPE (type_decl),
14752 DECL_NAME (type_decl),
14753 typename_type, tf_error);
14754 if (type_decl != error_mark_node)
14755 type_decl = TYPE_NAME (type_decl);
14756 }
14757 }
14758
14759 if (TREE_CODE (type_decl) != TYPE_DECL
14760 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14761 {
14762 /* See if this is an Objective-C type. */
14763 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14764 tree type = objc_get_protocol_qualified_type (identifier, protos);
14765 if (type)
14766 type_decl = TYPE_NAME (type);
14767 }
14768
14769 /* Issue an error if we did not find a type-name. */
14770 if (TREE_CODE (type_decl) != TYPE_DECL
14771 /* In Objective-C, we have the complication that class names are
14772 normally type names and start declarations (eg, the
14773 "NSObject" in "NSObject *object;"), but can be used in an
14774 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14775 is an expression. So, a classname followed by a dot is not a
14776 valid type-name. */
14777 || (objc_is_class_name (TREE_TYPE (type_decl))
14778 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14779 {
14780 if (!cp_parser_simulate_error (parser))
14781 cp_parser_name_lookup_error (parser, identifier, type_decl,
14782 NLE_TYPE, token->location);
14783 return error_mark_node;
14784 }
14785 /* Remember that the name was used in the definition of the
14786 current class so that we can check later to see if the
14787 meaning would have been different after the class was
14788 entirely defined. */
14789 else if (type_decl != error_mark_node
14790 && !parser->scope)
14791 maybe_note_name_used_in_class (identifier, type_decl);
14792
14793 return type_decl;
14794 }
14795
14796 /* Parse an elaborated-type-specifier. Note that the grammar given
14797 here incorporates the resolution to DR68.
14798
14799 elaborated-type-specifier:
14800 class-key :: [opt] nested-name-specifier [opt] identifier
14801 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14802 enum-key :: [opt] nested-name-specifier [opt] identifier
14803 typename :: [opt] nested-name-specifier identifier
14804 typename :: [opt] nested-name-specifier template [opt]
14805 template-id
14806
14807 GNU extension:
14808
14809 elaborated-type-specifier:
14810 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14811 class-key attributes :: [opt] nested-name-specifier [opt]
14812 template [opt] template-id
14813 enum attributes :: [opt] nested-name-specifier [opt] identifier
14814
14815 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14816 declared `friend'. If IS_DECLARATION is TRUE, then this
14817 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14818 something is being declared.
14819
14820 Returns the TYPE specified. */
14821
14822 static tree
14823 cp_parser_elaborated_type_specifier (cp_parser* parser,
14824 bool is_friend,
14825 bool is_declaration)
14826 {
14827 enum tag_types tag_type;
14828 tree identifier;
14829 tree type = NULL_TREE;
14830 tree attributes = NULL_TREE;
14831 tree globalscope;
14832 cp_token *token = NULL;
14833
14834 /* See if we're looking at the `enum' keyword. */
14835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14836 {
14837 /* Consume the `enum' token. */
14838 cp_lexer_consume_token (parser->lexer);
14839 /* Remember that it's an enumeration type. */
14840 tag_type = enum_type;
14841 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14842 enums) is used here. */
14843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14844 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14845 {
14846 pedwarn (input_location, 0, "elaborated-type-specifier "
14847 "for a scoped enum must not use the %<%D%> keyword",
14848 cp_lexer_peek_token (parser->lexer)->u.value);
14849 /* Consume the `struct' or `class' and parse it anyway. */
14850 cp_lexer_consume_token (parser->lexer);
14851 }
14852 /* Parse the attributes. */
14853 attributes = cp_parser_attributes_opt (parser);
14854 }
14855 /* Or, it might be `typename'. */
14856 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14857 RID_TYPENAME))
14858 {
14859 /* Consume the `typename' token. */
14860 cp_lexer_consume_token (parser->lexer);
14861 /* Remember that it's a `typename' type. */
14862 tag_type = typename_type;
14863 }
14864 /* Otherwise it must be a class-key. */
14865 else
14866 {
14867 tag_type = cp_parser_class_key (parser);
14868 if (tag_type == none_type)
14869 return error_mark_node;
14870 /* Parse the attributes. */
14871 attributes = cp_parser_attributes_opt (parser);
14872 }
14873
14874 /* Look for the `::' operator. */
14875 globalscope = cp_parser_global_scope_opt (parser,
14876 /*current_scope_valid_p=*/false);
14877 /* Look for the nested-name-specifier. */
14878 if (tag_type == typename_type && !globalscope)
14879 {
14880 if (!cp_parser_nested_name_specifier (parser,
14881 /*typename_keyword_p=*/true,
14882 /*check_dependency_p=*/true,
14883 /*type_p=*/true,
14884 is_declaration))
14885 return error_mark_node;
14886 }
14887 else
14888 /* Even though `typename' is not present, the proposed resolution
14889 to Core Issue 180 says that in `class A<T>::B', `B' should be
14890 considered a type-name, even if `A<T>' is dependent. */
14891 cp_parser_nested_name_specifier_opt (parser,
14892 /*typename_keyword_p=*/true,
14893 /*check_dependency_p=*/true,
14894 /*type_p=*/true,
14895 is_declaration);
14896 /* For everything but enumeration types, consider a template-id.
14897 For an enumeration type, consider only a plain identifier. */
14898 if (tag_type != enum_type)
14899 {
14900 bool template_p = false;
14901 tree decl;
14902
14903 /* Allow the `template' keyword. */
14904 template_p = cp_parser_optional_template_keyword (parser);
14905 /* If we didn't see `template', we don't know if there's a
14906 template-id or not. */
14907 if (!template_p)
14908 cp_parser_parse_tentatively (parser);
14909 /* Parse the template-id. */
14910 token = cp_lexer_peek_token (parser->lexer);
14911 decl = cp_parser_template_id (parser, template_p,
14912 /*check_dependency_p=*/true,
14913 tag_type,
14914 is_declaration);
14915 /* If we didn't find a template-id, look for an ordinary
14916 identifier. */
14917 if (!template_p && !cp_parser_parse_definitely (parser))
14918 ;
14919 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14920 in effect, then we must assume that, upon instantiation, the
14921 template will correspond to a class. */
14922 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14923 && tag_type == typename_type)
14924 type = make_typename_type (parser->scope, decl,
14925 typename_type,
14926 /*complain=*/tf_error);
14927 /* If the `typename' keyword is in effect and DECL is not a type
14928 decl, then type is non existent. */
14929 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14930 ;
14931 else if (TREE_CODE (decl) == TYPE_DECL)
14932 type = check_elaborated_type_specifier (tag_type, decl,
14933 /*allow_template_p=*/true);
14934 else if (decl == error_mark_node)
14935 type = error_mark_node;
14936 }
14937
14938 if (!type)
14939 {
14940 token = cp_lexer_peek_token (parser->lexer);
14941 identifier = cp_parser_identifier (parser);
14942
14943 if (identifier == error_mark_node)
14944 {
14945 parser->scope = NULL_TREE;
14946 return error_mark_node;
14947 }
14948
14949 /* For a `typename', we needn't call xref_tag. */
14950 if (tag_type == typename_type
14951 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14952 return cp_parser_make_typename_type (parser, parser->scope,
14953 identifier,
14954 token->location);
14955 /* Look up a qualified name in the usual way. */
14956 if (parser->scope)
14957 {
14958 tree decl;
14959 tree ambiguous_decls;
14960
14961 decl = cp_parser_lookup_name (parser, identifier,
14962 tag_type,
14963 /*is_template=*/false,
14964 /*is_namespace=*/false,
14965 /*check_dependency=*/true,
14966 &ambiguous_decls,
14967 token->location);
14968
14969 /* If the lookup was ambiguous, an error will already have been
14970 issued. */
14971 if (ambiguous_decls)
14972 return error_mark_node;
14973
14974 /* If we are parsing friend declaration, DECL may be a
14975 TEMPLATE_DECL tree node here. However, we need to check
14976 whether this TEMPLATE_DECL results in valid code. Consider
14977 the following example:
14978
14979 namespace N {
14980 template <class T> class C {};
14981 }
14982 class X {
14983 template <class T> friend class N::C; // #1, valid code
14984 };
14985 template <class T> class Y {
14986 friend class N::C; // #2, invalid code
14987 };
14988
14989 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14990 name lookup of `N::C'. We see that friend declaration must
14991 be template for the code to be valid. Note that
14992 processing_template_decl does not work here since it is
14993 always 1 for the above two cases. */
14994
14995 decl = (cp_parser_maybe_treat_template_as_class
14996 (decl, /*tag_name_p=*/is_friend
14997 && parser->num_template_parameter_lists));
14998
14999 if (TREE_CODE (decl) != TYPE_DECL)
15000 {
15001 cp_parser_diagnose_invalid_type_name (parser,
15002 parser->scope,
15003 identifier,
15004 token->location);
15005 return error_mark_node;
15006 }
15007
15008 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15009 {
15010 bool allow_template = (parser->num_template_parameter_lists
15011 || DECL_SELF_REFERENCE_P (decl));
15012 type = check_elaborated_type_specifier (tag_type, decl,
15013 allow_template);
15014
15015 if (type == error_mark_node)
15016 return error_mark_node;
15017 }
15018
15019 /* Forward declarations of nested types, such as
15020
15021 class C1::C2;
15022 class C1::C2::C3;
15023
15024 are invalid unless all components preceding the final '::'
15025 are complete. If all enclosing types are complete, these
15026 declarations become merely pointless.
15027
15028 Invalid forward declarations of nested types are errors
15029 caught elsewhere in parsing. Those that are pointless arrive
15030 here. */
15031
15032 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15033 && !is_friend && !processing_explicit_instantiation)
15034 warning (0, "declaration %qD does not declare anything", decl);
15035
15036 type = TREE_TYPE (decl);
15037 }
15038 else
15039 {
15040 /* An elaborated-type-specifier sometimes introduces a new type and
15041 sometimes names an existing type. Normally, the rule is that it
15042 introduces a new type only if there is not an existing type of
15043 the same name already in scope. For example, given:
15044
15045 struct S {};
15046 void f() { struct S s; }
15047
15048 the `struct S' in the body of `f' is the same `struct S' as in
15049 the global scope; the existing definition is used. However, if
15050 there were no global declaration, this would introduce a new
15051 local class named `S'.
15052
15053 An exception to this rule applies to the following code:
15054
15055 namespace N { struct S; }
15056
15057 Here, the elaborated-type-specifier names a new type
15058 unconditionally; even if there is already an `S' in the
15059 containing scope this declaration names a new type.
15060 This exception only applies if the elaborated-type-specifier
15061 forms the complete declaration:
15062
15063 [class.name]
15064
15065 A declaration consisting solely of `class-key identifier ;' is
15066 either a redeclaration of the name in the current scope or a
15067 forward declaration of the identifier as a class name. It
15068 introduces the name into the current scope.
15069
15070 We are in this situation precisely when the next token is a `;'.
15071
15072 An exception to the exception is that a `friend' declaration does
15073 *not* name a new type; i.e., given:
15074
15075 struct S { friend struct T; };
15076
15077 `T' is not a new type in the scope of `S'.
15078
15079 Also, `new struct S' or `sizeof (struct S)' never results in the
15080 definition of a new type; a new type can only be declared in a
15081 declaration context. */
15082
15083 tag_scope ts;
15084 bool template_p;
15085
15086 if (is_friend)
15087 /* Friends have special name lookup rules. */
15088 ts = ts_within_enclosing_non_class;
15089 else if (is_declaration
15090 && cp_lexer_next_token_is (parser->lexer,
15091 CPP_SEMICOLON))
15092 /* This is a `class-key identifier ;' */
15093 ts = ts_current;
15094 else
15095 ts = ts_global;
15096
15097 template_p =
15098 (parser->num_template_parameter_lists
15099 && (cp_parser_next_token_starts_class_definition_p (parser)
15100 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15101 /* An unqualified name was used to reference this type, so
15102 there were no qualifying templates. */
15103 if (!cp_parser_check_template_parameters (parser,
15104 /*num_templates=*/0,
15105 token->location,
15106 /*declarator=*/NULL))
15107 return error_mark_node;
15108 type = xref_tag (tag_type, identifier, ts, template_p);
15109 }
15110 }
15111
15112 if (type == error_mark_node)
15113 return error_mark_node;
15114
15115 /* Allow attributes on forward declarations of classes. */
15116 if (attributes)
15117 {
15118 if (TREE_CODE (type) == TYPENAME_TYPE)
15119 warning (OPT_Wattributes,
15120 "attributes ignored on uninstantiated type");
15121 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15122 && ! processing_explicit_instantiation)
15123 warning (OPT_Wattributes,
15124 "attributes ignored on template instantiation");
15125 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15126 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15127 else
15128 warning (OPT_Wattributes,
15129 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15130 }
15131
15132 if (tag_type != enum_type)
15133 {
15134 /* Indicate whether this class was declared as a `class' or as a
15135 `struct'. */
15136 if (TREE_CODE (type) == RECORD_TYPE)
15137 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15138 cp_parser_check_class_key (tag_type, type);
15139 }
15140
15141 /* A "<" cannot follow an elaborated type specifier. If that
15142 happens, the user was probably trying to form a template-id. */
15143 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15144 token->location);
15145
15146 return type;
15147 }
15148
15149 /* Parse an enum-specifier.
15150
15151 enum-specifier:
15152 enum-head { enumerator-list [opt] }
15153 enum-head { enumerator-list , } [C++0x]
15154
15155 enum-head:
15156 enum-key identifier [opt] enum-base [opt]
15157 enum-key nested-name-specifier identifier enum-base [opt]
15158
15159 enum-key:
15160 enum
15161 enum class [C++0x]
15162 enum struct [C++0x]
15163
15164 enum-base: [C++0x]
15165 : type-specifier-seq
15166
15167 opaque-enum-specifier:
15168 enum-key identifier enum-base [opt] ;
15169
15170 GNU Extensions:
15171 enum-key attributes[opt] identifier [opt] enum-base [opt]
15172 { enumerator-list [opt] }attributes[opt]
15173 enum-key attributes[opt] identifier [opt] enum-base [opt]
15174 { enumerator-list, }attributes[opt] [C++0x]
15175
15176 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15177 if the token stream isn't an enum-specifier after all. */
15178
15179 static tree
15180 cp_parser_enum_specifier (cp_parser* parser)
15181 {
15182 tree identifier;
15183 tree type = NULL_TREE;
15184 tree prev_scope;
15185 tree nested_name_specifier = NULL_TREE;
15186 tree attributes;
15187 bool scoped_enum_p = false;
15188 bool has_underlying_type = false;
15189 bool nested_being_defined = false;
15190 bool new_value_list = false;
15191 bool is_new_type = false;
15192 bool is_anonymous = false;
15193 tree underlying_type = NULL_TREE;
15194 cp_token *type_start_token = NULL;
15195 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15196
15197 parser->colon_corrects_to_scope_p = false;
15198
15199 /* Parse tentatively so that we can back up if we don't find a
15200 enum-specifier. */
15201 cp_parser_parse_tentatively (parser);
15202
15203 /* Caller guarantees that the current token is 'enum', an identifier
15204 possibly follows, and the token after that is an opening brace.
15205 If we don't have an identifier, fabricate an anonymous name for
15206 the enumeration being defined. */
15207 cp_lexer_consume_token (parser->lexer);
15208
15209 /* Parse the "class" or "struct", which indicates a scoped
15210 enumeration type in C++0x. */
15211 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15212 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15213 {
15214 if (cxx_dialect < cxx11)
15215 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15216
15217 /* Consume the `struct' or `class' token. */
15218 cp_lexer_consume_token (parser->lexer);
15219
15220 scoped_enum_p = true;
15221 }
15222
15223 attributes = cp_parser_attributes_opt (parser);
15224
15225 /* Clear the qualification. */
15226 parser->scope = NULL_TREE;
15227 parser->qualifying_scope = NULL_TREE;
15228 parser->object_scope = NULL_TREE;
15229
15230 /* Figure out in what scope the declaration is being placed. */
15231 prev_scope = current_scope ();
15232
15233 type_start_token = cp_lexer_peek_token (parser->lexer);
15234
15235 push_deferring_access_checks (dk_no_check);
15236 nested_name_specifier
15237 = cp_parser_nested_name_specifier_opt (parser,
15238 /*typename_keyword_p=*/true,
15239 /*check_dependency_p=*/false,
15240 /*type_p=*/false,
15241 /*is_declaration=*/false);
15242
15243 if (nested_name_specifier)
15244 {
15245 tree name;
15246
15247 identifier = cp_parser_identifier (parser);
15248 name = cp_parser_lookup_name (parser, identifier,
15249 enum_type,
15250 /*is_template=*/false,
15251 /*is_namespace=*/false,
15252 /*check_dependency=*/true,
15253 /*ambiguous_decls=*/NULL,
15254 input_location);
15255 if (name && name != error_mark_node)
15256 {
15257 type = TREE_TYPE (name);
15258 if (TREE_CODE (type) == TYPENAME_TYPE)
15259 {
15260 /* Are template enums allowed in ISO? */
15261 if (template_parm_scope_p ())
15262 pedwarn (type_start_token->location, OPT_Wpedantic,
15263 "%qD is an enumeration template", name);
15264 /* ignore a typename reference, for it will be solved by name
15265 in start_enum. */
15266 type = NULL_TREE;
15267 }
15268 }
15269 else if (nested_name_specifier == error_mark_node)
15270 /* We already issued an error. */;
15271 else
15272 error_at (type_start_token->location,
15273 "%qD is not an enumerator-name", identifier);
15274 }
15275 else
15276 {
15277 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15278 identifier = cp_parser_identifier (parser);
15279 else
15280 {
15281 identifier = make_anon_name ();
15282 is_anonymous = true;
15283 if (scoped_enum_p)
15284 error_at (type_start_token->location,
15285 "anonymous scoped enum is not allowed");
15286 }
15287 }
15288 pop_deferring_access_checks ();
15289
15290 /* Check for the `:' that denotes a specified underlying type in C++0x.
15291 Note that a ':' could also indicate a bitfield width, however. */
15292 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15293 {
15294 cp_decl_specifier_seq type_specifiers;
15295
15296 /* Consume the `:'. */
15297 cp_lexer_consume_token (parser->lexer);
15298
15299 /* Parse the type-specifier-seq. */
15300 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15301 /*is_trailing_return=*/false,
15302 &type_specifiers);
15303
15304 /* At this point this is surely not elaborated type specifier. */
15305 if (!cp_parser_parse_definitely (parser))
15306 return NULL_TREE;
15307
15308 if (cxx_dialect < cxx11)
15309 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15310
15311 has_underlying_type = true;
15312
15313 /* If that didn't work, stop. */
15314 if (type_specifiers.type != error_mark_node)
15315 {
15316 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15317 /*initialized=*/0, NULL);
15318 if (underlying_type == error_mark_node)
15319 underlying_type = NULL_TREE;
15320 }
15321 }
15322
15323 /* Look for the `{' but don't consume it yet. */
15324 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15325 {
15326 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15327 {
15328 cp_parser_error (parser, "expected %<{%>");
15329 if (has_underlying_type)
15330 {
15331 type = NULL_TREE;
15332 goto out;
15333 }
15334 }
15335 /* An opaque-enum-specifier must have a ';' here. */
15336 if ((scoped_enum_p || underlying_type)
15337 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15338 {
15339 cp_parser_error (parser, "expected %<;%> or %<{%>");
15340 if (has_underlying_type)
15341 {
15342 type = NULL_TREE;
15343 goto out;
15344 }
15345 }
15346 }
15347
15348 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15349 return NULL_TREE;
15350
15351 if (nested_name_specifier)
15352 {
15353 if (CLASS_TYPE_P (nested_name_specifier))
15354 {
15355 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15356 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15357 push_scope (nested_name_specifier);
15358 }
15359 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15360 {
15361 push_nested_namespace (nested_name_specifier);
15362 }
15363 }
15364
15365 /* Issue an error message if type-definitions are forbidden here. */
15366 if (!cp_parser_check_type_definition (parser))
15367 type = error_mark_node;
15368 else
15369 /* Create the new type. We do this before consuming the opening
15370 brace so the enum will be recorded as being on the line of its
15371 tag (or the 'enum' keyword, if there is no tag). */
15372 type = start_enum (identifier, type, underlying_type,
15373 scoped_enum_p, &is_new_type);
15374
15375 /* If the next token is not '{' it is an opaque-enum-specifier or an
15376 elaborated-type-specifier. */
15377 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15378 {
15379 timevar_push (TV_PARSE_ENUM);
15380 if (nested_name_specifier
15381 && nested_name_specifier != error_mark_node)
15382 {
15383 /* The following catches invalid code such as:
15384 enum class S<int>::E { A, B, C }; */
15385 if (!processing_specialization
15386 && CLASS_TYPE_P (nested_name_specifier)
15387 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15388 error_at (type_start_token->location, "cannot add an enumerator "
15389 "list to a template instantiation");
15390
15391 /* If that scope does not contain the scope in which the
15392 class was originally declared, the program is invalid. */
15393 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
15394 {
15395 if (at_namespace_scope_p ())
15396 error_at (type_start_token->location,
15397 "declaration of %qD in namespace %qD which does not "
15398 "enclose %qD",
15399 type, prev_scope, nested_name_specifier);
15400 else
15401 error_at (type_start_token->location,
15402 "declaration of %qD in %qD which does not enclose %qD",
15403 type, prev_scope, nested_name_specifier);
15404 type = error_mark_node;
15405 }
15406 }
15407
15408 if (scoped_enum_p)
15409 begin_scope (sk_scoped_enum, type);
15410
15411 /* Consume the opening brace. */
15412 cp_lexer_consume_token (parser->lexer);
15413
15414 if (type == error_mark_node)
15415 ; /* Nothing to add */
15416 else if (OPAQUE_ENUM_P (type)
15417 || (cxx_dialect > cxx98 && processing_specialization))
15418 {
15419 new_value_list = true;
15420 SET_OPAQUE_ENUM_P (type, false);
15421 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15422 }
15423 else
15424 {
15425 error_at (type_start_token->location, "multiple definition of %q#T", type);
15426 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15427 "previous definition here");
15428 type = error_mark_node;
15429 }
15430
15431 if (type == error_mark_node)
15432 cp_parser_skip_to_end_of_block_or_statement (parser);
15433 /* If the next token is not '}', then there are some enumerators. */
15434 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15435 {
15436 if (is_anonymous && !scoped_enum_p)
15437 pedwarn (type_start_token->location, OPT_Wpedantic,
15438 "ISO C++ forbids empty anonymous enum");
15439 }
15440 else
15441 cp_parser_enumerator_list (parser, type);
15442
15443 /* Consume the final '}'. */
15444 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15445
15446 if (scoped_enum_p)
15447 finish_scope ();
15448 timevar_pop (TV_PARSE_ENUM);
15449 }
15450 else
15451 {
15452 /* If a ';' follows, then it is an opaque-enum-specifier
15453 and additional restrictions apply. */
15454 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15455 {
15456 if (is_anonymous)
15457 error_at (type_start_token->location,
15458 "opaque-enum-specifier without name");
15459 else if (nested_name_specifier)
15460 error_at (type_start_token->location,
15461 "opaque-enum-specifier must use a simple identifier");
15462 }
15463 }
15464
15465 /* Look for trailing attributes to apply to this enumeration, and
15466 apply them if appropriate. */
15467 if (cp_parser_allow_gnu_extensions_p (parser))
15468 {
15469 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15470 trailing_attr = chainon (trailing_attr, attributes);
15471 cplus_decl_attributes (&type,
15472 trailing_attr,
15473 (int) ATTR_FLAG_TYPE_IN_PLACE);
15474 }
15475
15476 /* Finish up the enumeration. */
15477 if (type != error_mark_node)
15478 {
15479 if (new_value_list)
15480 finish_enum_value_list (type);
15481 if (is_new_type)
15482 finish_enum (type);
15483 }
15484
15485 if (nested_name_specifier)
15486 {
15487 if (CLASS_TYPE_P (nested_name_specifier))
15488 {
15489 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15490 pop_scope (nested_name_specifier);
15491 }
15492 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15493 {
15494 pop_nested_namespace (nested_name_specifier);
15495 }
15496 }
15497 out:
15498 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15499 return type;
15500 }
15501
15502 /* Parse an enumerator-list. The enumerators all have the indicated
15503 TYPE.
15504
15505 enumerator-list:
15506 enumerator-definition
15507 enumerator-list , enumerator-definition */
15508
15509 static void
15510 cp_parser_enumerator_list (cp_parser* parser, tree type)
15511 {
15512 while (true)
15513 {
15514 /* Parse an enumerator-definition. */
15515 cp_parser_enumerator_definition (parser, type);
15516
15517 /* If the next token is not a ',', we've reached the end of
15518 the list. */
15519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15520 break;
15521 /* Otherwise, consume the `,' and keep going. */
15522 cp_lexer_consume_token (parser->lexer);
15523 /* If the next token is a `}', there is a trailing comma. */
15524 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15525 {
15526 if (cxx_dialect < cxx11 && !in_system_header)
15527 pedwarn (input_location, OPT_Wpedantic,
15528 "comma at end of enumerator list");
15529 break;
15530 }
15531 }
15532 }
15533
15534 /* Parse an enumerator-definition. The enumerator has the indicated
15535 TYPE.
15536
15537 enumerator-definition:
15538 enumerator
15539 enumerator = constant-expression
15540
15541 enumerator:
15542 identifier */
15543
15544 static void
15545 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15546 {
15547 tree identifier;
15548 tree value;
15549 location_t loc;
15550
15551 /* Save the input location because we are interested in the location
15552 of the identifier and not the location of the explicit value. */
15553 loc = cp_lexer_peek_token (parser->lexer)->location;
15554
15555 /* Look for the identifier. */
15556 identifier = cp_parser_identifier (parser);
15557 if (identifier == error_mark_node)
15558 return;
15559
15560 /* If the next token is an '=', then there is an explicit value. */
15561 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15562 {
15563 /* Consume the `=' token. */
15564 cp_lexer_consume_token (parser->lexer);
15565 /* Parse the value. */
15566 value = cp_parser_constant_expression (parser,
15567 /*allow_non_constant_p=*/false,
15568 NULL);
15569 }
15570 else
15571 value = NULL_TREE;
15572
15573 /* If we are processing a template, make sure the initializer of the
15574 enumerator doesn't contain any bare template parameter pack. */
15575 if (check_for_bare_parameter_packs (value))
15576 value = error_mark_node;
15577
15578 /* integral_constant_value will pull out this expression, so make sure
15579 it's folded as appropriate. */
15580 value = fold_non_dependent_expr (value);
15581
15582 /* Create the enumerator. */
15583 build_enumerator (identifier, value, type, loc);
15584 }
15585
15586 /* Parse a namespace-name.
15587
15588 namespace-name:
15589 original-namespace-name
15590 namespace-alias
15591
15592 Returns the NAMESPACE_DECL for the namespace. */
15593
15594 static tree
15595 cp_parser_namespace_name (cp_parser* parser)
15596 {
15597 tree identifier;
15598 tree namespace_decl;
15599
15600 cp_token *token = cp_lexer_peek_token (parser->lexer);
15601
15602 /* Get the name of the namespace. */
15603 identifier = cp_parser_identifier (parser);
15604 if (identifier == error_mark_node)
15605 return error_mark_node;
15606
15607 /* Look up the identifier in the currently active scope. Look only
15608 for namespaces, due to:
15609
15610 [basic.lookup.udir]
15611
15612 When looking up a namespace-name in a using-directive or alias
15613 definition, only namespace names are considered.
15614
15615 And:
15616
15617 [basic.lookup.qual]
15618
15619 During the lookup of a name preceding the :: scope resolution
15620 operator, object, function, and enumerator names are ignored.
15621
15622 (Note that cp_parser_qualifying_entity only calls this
15623 function if the token after the name is the scope resolution
15624 operator.) */
15625 namespace_decl = cp_parser_lookup_name (parser, identifier,
15626 none_type,
15627 /*is_template=*/false,
15628 /*is_namespace=*/true,
15629 /*check_dependency=*/true,
15630 /*ambiguous_decls=*/NULL,
15631 token->location);
15632 /* If it's not a namespace, issue an error. */
15633 if (namespace_decl == error_mark_node
15634 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15635 {
15636 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15637 error_at (token->location, "%qD is not a namespace-name", identifier);
15638 cp_parser_error (parser, "expected namespace-name");
15639 namespace_decl = error_mark_node;
15640 }
15641
15642 return namespace_decl;
15643 }
15644
15645 /* Parse a namespace-definition.
15646
15647 namespace-definition:
15648 named-namespace-definition
15649 unnamed-namespace-definition
15650
15651 named-namespace-definition:
15652 original-namespace-definition
15653 extension-namespace-definition
15654
15655 original-namespace-definition:
15656 namespace identifier { namespace-body }
15657
15658 extension-namespace-definition:
15659 namespace original-namespace-name { namespace-body }
15660
15661 unnamed-namespace-definition:
15662 namespace { namespace-body } */
15663
15664 static void
15665 cp_parser_namespace_definition (cp_parser* parser)
15666 {
15667 tree identifier, attribs;
15668 bool has_visibility;
15669 bool is_inline;
15670
15671 cp_ensure_no_omp_declare_simd (parser);
15672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15673 {
15674 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15675 is_inline = true;
15676 cp_lexer_consume_token (parser->lexer);
15677 }
15678 else
15679 is_inline = false;
15680
15681 /* Look for the `namespace' keyword. */
15682 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15683
15684 /* Get the name of the namespace. We do not attempt to distinguish
15685 between an original-namespace-definition and an
15686 extension-namespace-definition at this point. The semantic
15687 analysis routines are responsible for that. */
15688 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15689 identifier = cp_parser_identifier (parser);
15690 else
15691 identifier = NULL_TREE;
15692
15693 /* Parse any specified attributes. */
15694 attribs = cp_parser_attributes_opt (parser);
15695
15696 /* Look for the `{' to start the namespace. */
15697 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15698 /* Start the namespace. */
15699 push_namespace (identifier);
15700
15701 /* "inline namespace" is equivalent to a stub namespace definition
15702 followed by a strong using directive. */
15703 if (is_inline)
15704 {
15705 tree name_space = current_namespace;
15706 /* Set up namespace association. */
15707 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15708 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15709 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15710 /* Import the contents of the inline namespace. */
15711 pop_namespace ();
15712 do_using_directive (name_space);
15713 push_namespace (identifier);
15714 }
15715
15716 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15717
15718 /* Parse the body of the namespace. */
15719 cp_parser_namespace_body (parser);
15720
15721 if (has_visibility)
15722 pop_visibility (1);
15723
15724 /* Finish the namespace. */
15725 pop_namespace ();
15726 /* Look for the final `}'. */
15727 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15728 }
15729
15730 /* Parse a namespace-body.
15731
15732 namespace-body:
15733 declaration-seq [opt] */
15734
15735 static void
15736 cp_parser_namespace_body (cp_parser* parser)
15737 {
15738 cp_parser_declaration_seq_opt (parser);
15739 }
15740
15741 /* Parse a namespace-alias-definition.
15742
15743 namespace-alias-definition:
15744 namespace identifier = qualified-namespace-specifier ; */
15745
15746 static void
15747 cp_parser_namespace_alias_definition (cp_parser* parser)
15748 {
15749 tree identifier;
15750 tree namespace_specifier;
15751
15752 cp_token *token = cp_lexer_peek_token (parser->lexer);
15753
15754 /* Look for the `namespace' keyword. */
15755 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15756 /* Look for the identifier. */
15757 identifier = cp_parser_identifier (parser);
15758 if (identifier == error_mark_node)
15759 return;
15760 /* Look for the `=' token. */
15761 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15762 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15763 {
15764 error_at (token->location, "%<namespace%> definition is not allowed here");
15765 /* Skip the definition. */
15766 cp_lexer_consume_token (parser->lexer);
15767 if (cp_parser_skip_to_closing_brace (parser))
15768 cp_lexer_consume_token (parser->lexer);
15769 return;
15770 }
15771 cp_parser_require (parser, CPP_EQ, RT_EQ);
15772 /* Look for the qualified-namespace-specifier. */
15773 namespace_specifier
15774 = cp_parser_qualified_namespace_specifier (parser);
15775 /* Look for the `;' token. */
15776 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15777
15778 /* Register the alias in the symbol table. */
15779 do_namespace_alias (identifier, namespace_specifier);
15780 }
15781
15782 /* Parse a qualified-namespace-specifier.
15783
15784 qualified-namespace-specifier:
15785 :: [opt] nested-name-specifier [opt] namespace-name
15786
15787 Returns a NAMESPACE_DECL corresponding to the specified
15788 namespace. */
15789
15790 static tree
15791 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15792 {
15793 /* Look for the optional `::'. */
15794 cp_parser_global_scope_opt (parser,
15795 /*current_scope_valid_p=*/false);
15796
15797 /* Look for the optional nested-name-specifier. */
15798 cp_parser_nested_name_specifier_opt (parser,
15799 /*typename_keyword_p=*/false,
15800 /*check_dependency_p=*/true,
15801 /*type_p=*/false,
15802 /*is_declaration=*/true);
15803
15804 return cp_parser_namespace_name (parser);
15805 }
15806
15807 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15808 access declaration.
15809
15810 using-declaration:
15811 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15812 using :: unqualified-id ;
15813
15814 access-declaration:
15815 qualified-id ;
15816
15817 */
15818
15819 static bool
15820 cp_parser_using_declaration (cp_parser* parser,
15821 bool access_declaration_p)
15822 {
15823 cp_token *token;
15824 bool typename_p = false;
15825 bool global_scope_p;
15826 tree decl;
15827 tree identifier;
15828 tree qscope;
15829 int oldcount = errorcount;
15830 cp_token *diag_token = NULL;
15831
15832 if (access_declaration_p)
15833 {
15834 diag_token = cp_lexer_peek_token (parser->lexer);
15835 cp_parser_parse_tentatively (parser);
15836 }
15837 else
15838 {
15839 /* Look for the `using' keyword. */
15840 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15841
15842 /* Peek at the next token. */
15843 token = cp_lexer_peek_token (parser->lexer);
15844 /* See if it's `typename'. */
15845 if (token->keyword == RID_TYPENAME)
15846 {
15847 /* Remember that we've seen it. */
15848 typename_p = true;
15849 /* Consume the `typename' token. */
15850 cp_lexer_consume_token (parser->lexer);
15851 }
15852 }
15853
15854 /* Look for the optional global scope qualification. */
15855 global_scope_p
15856 = (cp_parser_global_scope_opt (parser,
15857 /*current_scope_valid_p=*/false)
15858 != NULL_TREE);
15859
15860 /* If we saw `typename', or didn't see `::', then there must be a
15861 nested-name-specifier present. */
15862 if (typename_p || !global_scope_p)
15863 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15864 /*check_dependency_p=*/true,
15865 /*type_p=*/false,
15866 /*is_declaration=*/true);
15867 /* Otherwise, we could be in either of the two productions. In that
15868 case, treat the nested-name-specifier as optional. */
15869 else
15870 qscope = cp_parser_nested_name_specifier_opt (parser,
15871 /*typename_keyword_p=*/false,
15872 /*check_dependency_p=*/true,
15873 /*type_p=*/false,
15874 /*is_declaration=*/true);
15875 if (!qscope)
15876 qscope = global_namespace;
15877
15878 if (access_declaration_p && cp_parser_error_occurred (parser))
15879 /* Something has already gone wrong; there's no need to parse
15880 further. Since an error has occurred, the return value of
15881 cp_parser_parse_definitely will be false, as required. */
15882 return cp_parser_parse_definitely (parser);
15883
15884 token = cp_lexer_peek_token (parser->lexer);
15885 /* Parse the unqualified-id. */
15886 identifier = cp_parser_unqualified_id (parser,
15887 /*template_keyword_p=*/false,
15888 /*check_dependency_p=*/true,
15889 /*declarator_p=*/true,
15890 /*optional_p=*/false);
15891
15892 if (access_declaration_p)
15893 {
15894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15895 cp_parser_simulate_error (parser);
15896 if (!cp_parser_parse_definitely (parser))
15897 return false;
15898 }
15899
15900 /* The function we call to handle a using-declaration is different
15901 depending on what scope we are in. */
15902 if (qscope == error_mark_node || identifier == error_mark_node)
15903 ;
15904 else if (!identifier_p (identifier)
15905 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15906 /* [namespace.udecl]
15907
15908 A using declaration shall not name a template-id. */
15909 error_at (token->location,
15910 "a template-id may not appear in a using-declaration");
15911 else
15912 {
15913 if (at_class_scope_p ())
15914 {
15915 /* Create the USING_DECL. */
15916 decl = do_class_using_decl (parser->scope, identifier);
15917
15918 if (decl && typename_p)
15919 USING_DECL_TYPENAME_P (decl) = 1;
15920
15921 if (check_for_bare_parameter_packs (decl))
15922 return false;
15923 else
15924 /* Add it to the list of members in this class. */
15925 finish_member_declaration (decl);
15926 }
15927 else
15928 {
15929 decl = cp_parser_lookup_name_simple (parser,
15930 identifier,
15931 token->location);
15932 if (decl == error_mark_node)
15933 cp_parser_name_lookup_error (parser, identifier,
15934 decl, NLE_NULL,
15935 token->location);
15936 else if (check_for_bare_parameter_packs (decl))
15937 return false;
15938 else if (!at_namespace_scope_p ())
15939 do_local_using_decl (decl, qscope, identifier);
15940 else
15941 do_toplevel_using_decl (decl, qscope, identifier);
15942 }
15943 }
15944
15945 /* Look for the final `;'. */
15946 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15947
15948 if (access_declaration_p && errorcount == oldcount)
15949 warning_at (diag_token->location, OPT_Wdeprecated,
15950 "access declarations are deprecated "
15951 "in favour of using-declarations; "
15952 "suggestion: add the %<using%> keyword");
15953
15954 return true;
15955 }
15956
15957 /* Parse an alias-declaration.
15958
15959 alias-declaration:
15960 using identifier attribute-specifier-seq [opt] = type-id */
15961
15962 static tree
15963 cp_parser_alias_declaration (cp_parser* parser)
15964 {
15965 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15966 location_t id_location;
15967 cp_declarator *declarator;
15968 cp_decl_specifier_seq decl_specs;
15969 bool member_p;
15970 const char *saved_message = NULL;
15971
15972 /* Look for the `using' keyword. */
15973 cp_token *using_token
15974 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15975 if (using_token == NULL)
15976 return error_mark_node;
15977
15978 id_location = cp_lexer_peek_token (parser->lexer)->location;
15979 id = cp_parser_identifier (parser);
15980 if (id == error_mark_node)
15981 return error_mark_node;
15982
15983 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15984 attributes = cp_parser_attributes_opt (parser);
15985 if (attributes == error_mark_node)
15986 return error_mark_node;
15987
15988 cp_parser_require (parser, CPP_EQ, RT_EQ);
15989
15990 if (cp_parser_error_occurred (parser))
15991 return error_mark_node;
15992
15993 cp_parser_commit_to_tentative_parse (parser);
15994
15995 /* Now we are going to parse the type-id of the declaration. */
15996
15997 /*
15998 [dcl.type]/3 says:
15999
16000 "A type-specifier-seq shall not define a class or enumeration
16001 unless it appears in the type-id of an alias-declaration (7.1.3) that
16002 is not the declaration of a template-declaration."
16003
16004 In other words, if we currently are in an alias template, the
16005 type-id should not define a type.
16006
16007 So let's set parser->type_definition_forbidden_message in that
16008 case; cp_parser_check_type_definition (called by
16009 cp_parser_class_specifier) will then emit an error if a type is
16010 defined in the type-id. */
16011 if (parser->num_template_parameter_lists)
16012 {
16013 saved_message = parser->type_definition_forbidden_message;
16014 parser->type_definition_forbidden_message =
16015 G_("types may not be defined in alias template declarations");
16016 }
16017
16018 type = cp_parser_type_id (parser);
16019
16020 /* Restore the error message if need be. */
16021 if (parser->num_template_parameter_lists)
16022 parser->type_definition_forbidden_message = saved_message;
16023
16024 if (type == error_mark_node)
16025 {
16026 cp_parser_skip_to_end_of_block_or_statement (parser);
16027 return error_mark_node;
16028 }
16029
16030 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16031
16032 if (cp_parser_error_occurred (parser))
16033 {
16034 cp_parser_skip_to_end_of_block_or_statement (parser);
16035 return error_mark_node;
16036 }
16037
16038 /* A typedef-name can also be introduced by an alias-declaration. The
16039 identifier following the using keyword becomes a typedef-name. It has
16040 the same semantics as if it were introduced by the typedef
16041 specifier. In particular, it does not define a new type and it shall
16042 not appear in the type-id. */
16043
16044 clear_decl_specs (&decl_specs);
16045 decl_specs.type = type;
16046 if (attributes != NULL_TREE)
16047 {
16048 decl_specs.attributes = attributes;
16049 set_and_check_decl_spec_loc (&decl_specs,
16050 ds_attribute,
16051 attrs_token);
16052 }
16053 set_and_check_decl_spec_loc (&decl_specs,
16054 ds_typedef,
16055 using_token);
16056 set_and_check_decl_spec_loc (&decl_specs,
16057 ds_alias,
16058 using_token);
16059
16060 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16061 declarator->id_loc = id_location;
16062
16063 member_p = at_class_scope_p ();
16064 if (member_p)
16065 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16066 NULL_TREE, attributes);
16067 else
16068 decl = start_decl (declarator, &decl_specs, 0,
16069 attributes, NULL_TREE, &pushed_scope);
16070 if (decl == error_mark_node)
16071 return decl;
16072
16073 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16074
16075 if (pushed_scope)
16076 pop_scope (pushed_scope);
16077
16078 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16079 added into the symbol table; otherwise, return the TYPE_DECL. */
16080 if (DECL_LANG_SPECIFIC (decl)
16081 && DECL_TEMPLATE_INFO (decl)
16082 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16083 {
16084 decl = DECL_TI_TEMPLATE (decl);
16085 if (member_p)
16086 check_member_template (decl);
16087 }
16088
16089 return decl;
16090 }
16091
16092 /* Parse a using-directive.
16093
16094 using-directive:
16095 using namespace :: [opt] nested-name-specifier [opt]
16096 namespace-name ; */
16097
16098 static void
16099 cp_parser_using_directive (cp_parser* parser)
16100 {
16101 tree namespace_decl;
16102 tree attribs;
16103
16104 /* Look for the `using' keyword. */
16105 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16106 /* And the `namespace' keyword. */
16107 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16108 /* Look for the optional `::' operator. */
16109 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16110 /* And the optional nested-name-specifier. */
16111 cp_parser_nested_name_specifier_opt (parser,
16112 /*typename_keyword_p=*/false,
16113 /*check_dependency_p=*/true,
16114 /*type_p=*/false,
16115 /*is_declaration=*/true);
16116 /* Get the namespace being used. */
16117 namespace_decl = cp_parser_namespace_name (parser);
16118 /* And any specified attributes. */
16119 attribs = cp_parser_attributes_opt (parser);
16120 /* Update the symbol table. */
16121 parse_using_directive (namespace_decl, attribs);
16122 /* Look for the final `;'. */
16123 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16124 }
16125
16126 /* Parse an asm-definition.
16127
16128 asm-definition:
16129 asm ( string-literal ) ;
16130
16131 GNU Extension:
16132
16133 asm-definition:
16134 asm volatile [opt] ( string-literal ) ;
16135 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16136 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16137 : asm-operand-list [opt] ) ;
16138 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16139 : asm-operand-list [opt]
16140 : asm-clobber-list [opt] ) ;
16141 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16142 : asm-clobber-list [opt]
16143 : asm-goto-list ) ; */
16144
16145 static void
16146 cp_parser_asm_definition (cp_parser* parser)
16147 {
16148 tree string;
16149 tree outputs = NULL_TREE;
16150 tree inputs = NULL_TREE;
16151 tree clobbers = NULL_TREE;
16152 tree labels = NULL_TREE;
16153 tree asm_stmt;
16154 bool volatile_p = false;
16155 bool extended_p = false;
16156 bool invalid_inputs_p = false;
16157 bool invalid_outputs_p = false;
16158 bool goto_p = false;
16159 required_token missing = RT_NONE;
16160
16161 /* Look for the `asm' keyword. */
16162 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16163 /* See if the next token is `volatile'. */
16164 if (cp_parser_allow_gnu_extensions_p (parser)
16165 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16166 {
16167 /* Remember that we saw the `volatile' keyword. */
16168 volatile_p = true;
16169 /* Consume the token. */
16170 cp_lexer_consume_token (parser->lexer);
16171 }
16172 if (cp_parser_allow_gnu_extensions_p (parser)
16173 && parser->in_function_body
16174 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16175 {
16176 /* Remember that we saw the `goto' keyword. */
16177 goto_p = true;
16178 /* Consume the token. */
16179 cp_lexer_consume_token (parser->lexer);
16180 }
16181 /* Look for the opening `('. */
16182 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16183 return;
16184 /* Look for the string. */
16185 string = cp_parser_string_literal (parser, false, false);
16186 if (string == error_mark_node)
16187 {
16188 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16189 /*consume_paren=*/true);
16190 return;
16191 }
16192
16193 /* If we're allowing GNU extensions, check for the extended assembly
16194 syntax. Unfortunately, the `:' tokens need not be separated by
16195 a space in C, and so, for compatibility, we tolerate that here
16196 too. Doing that means that we have to treat the `::' operator as
16197 two `:' tokens. */
16198 if (cp_parser_allow_gnu_extensions_p (parser)
16199 && parser->in_function_body
16200 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16201 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16202 {
16203 bool inputs_p = false;
16204 bool clobbers_p = false;
16205 bool labels_p = false;
16206
16207 /* The extended syntax was used. */
16208 extended_p = true;
16209
16210 /* Look for outputs. */
16211 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16212 {
16213 /* Consume the `:'. */
16214 cp_lexer_consume_token (parser->lexer);
16215 /* Parse the output-operands. */
16216 if (cp_lexer_next_token_is_not (parser->lexer,
16217 CPP_COLON)
16218 && cp_lexer_next_token_is_not (parser->lexer,
16219 CPP_SCOPE)
16220 && cp_lexer_next_token_is_not (parser->lexer,
16221 CPP_CLOSE_PAREN)
16222 && !goto_p)
16223 outputs = cp_parser_asm_operand_list (parser);
16224
16225 if (outputs == error_mark_node)
16226 invalid_outputs_p = true;
16227 }
16228 /* If the next token is `::', there are no outputs, and the
16229 next token is the beginning of the inputs. */
16230 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16231 /* The inputs are coming next. */
16232 inputs_p = true;
16233
16234 /* Look for inputs. */
16235 if (inputs_p
16236 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16237 {
16238 /* Consume the `:' or `::'. */
16239 cp_lexer_consume_token (parser->lexer);
16240 /* Parse the output-operands. */
16241 if (cp_lexer_next_token_is_not (parser->lexer,
16242 CPP_COLON)
16243 && cp_lexer_next_token_is_not (parser->lexer,
16244 CPP_SCOPE)
16245 && cp_lexer_next_token_is_not (parser->lexer,
16246 CPP_CLOSE_PAREN))
16247 inputs = cp_parser_asm_operand_list (parser);
16248
16249 if (inputs == error_mark_node)
16250 invalid_inputs_p = true;
16251 }
16252 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16253 /* The clobbers are coming next. */
16254 clobbers_p = true;
16255
16256 /* Look for clobbers. */
16257 if (clobbers_p
16258 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16259 {
16260 clobbers_p = true;
16261 /* Consume the `:' or `::'. */
16262 cp_lexer_consume_token (parser->lexer);
16263 /* Parse the clobbers. */
16264 if (cp_lexer_next_token_is_not (parser->lexer,
16265 CPP_COLON)
16266 && cp_lexer_next_token_is_not (parser->lexer,
16267 CPP_CLOSE_PAREN))
16268 clobbers = cp_parser_asm_clobber_list (parser);
16269 }
16270 else if (goto_p
16271 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16272 /* The labels are coming next. */
16273 labels_p = true;
16274
16275 /* Look for labels. */
16276 if (labels_p
16277 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16278 {
16279 labels_p = true;
16280 /* Consume the `:' or `::'. */
16281 cp_lexer_consume_token (parser->lexer);
16282 /* Parse the labels. */
16283 labels = cp_parser_asm_label_list (parser);
16284 }
16285
16286 if (goto_p && !labels_p)
16287 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16288 }
16289 else if (goto_p)
16290 missing = RT_COLON_SCOPE;
16291
16292 /* Look for the closing `)'. */
16293 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16294 missing ? missing : RT_CLOSE_PAREN))
16295 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16296 /*consume_paren=*/true);
16297 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16298
16299 if (!invalid_inputs_p && !invalid_outputs_p)
16300 {
16301 /* Create the ASM_EXPR. */
16302 if (parser->in_function_body)
16303 {
16304 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16305 inputs, clobbers, labels);
16306 /* If the extended syntax was not used, mark the ASM_EXPR. */
16307 if (!extended_p)
16308 {
16309 tree temp = asm_stmt;
16310 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16311 temp = TREE_OPERAND (temp, 0);
16312
16313 ASM_INPUT_P (temp) = 1;
16314 }
16315 }
16316 else
16317 add_asm_node (string);
16318 }
16319 }
16320
16321 /* Declarators [gram.dcl.decl] */
16322
16323 /* Parse an init-declarator.
16324
16325 init-declarator:
16326 declarator initializer [opt]
16327
16328 GNU Extension:
16329
16330 init-declarator:
16331 declarator asm-specification [opt] attributes [opt] initializer [opt]
16332
16333 function-definition:
16334 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16335 function-body
16336 decl-specifier-seq [opt] declarator function-try-block
16337
16338 GNU Extension:
16339
16340 function-definition:
16341 __extension__ function-definition
16342
16343 TM Extension:
16344
16345 function-definition:
16346 decl-specifier-seq [opt] declarator function-transaction-block
16347
16348 The DECL_SPECIFIERS apply to this declarator. Returns a
16349 representation of the entity declared. If MEMBER_P is TRUE, then
16350 this declarator appears in a class scope. The new DECL created by
16351 this declarator is returned.
16352
16353 The CHECKS are access checks that should be performed once we know
16354 what entity is being declared (and, therefore, what classes have
16355 befriended it).
16356
16357 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16358 for a function-definition here as well. If the declarator is a
16359 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16360 be TRUE upon return. By that point, the function-definition will
16361 have been completely parsed.
16362
16363 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16364 is FALSE.
16365
16366 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16367 parsed declaration if it is an uninitialized single declarator not followed
16368 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16369 if present, will not be consumed. If returned, this declarator will be
16370 created with SD_INITIALIZED but will not call cp_finish_decl. */
16371
16372 static tree
16373 cp_parser_init_declarator (cp_parser* parser,
16374 cp_decl_specifier_seq *decl_specifiers,
16375 vec<deferred_access_check, va_gc> *checks,
16376 bool function_definition_allowed_p,
16377 bool member_p,
16378 int declares_class_or_enum,
16379 bool* function_definition_p,
16380 tree* maybe_range_for_decl)
16381 {
16382 cp_token *token = NULL, *asm_spec_start_token = NULL,
16383 *attributes_start_token = NULL;
16384 cp_declarator *declarator;
16385 tree prefix_attributes;
16386 tree attributes = NULL;
16387 tree asm_specification;
16388 tree initializer;
16389 tree decl = NULL_TREE;
16390 tree scope;
16391 int is_initialized;
16392 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16393 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16394 "(...)". */
16395 enum cpp_ttype initialization_kind;
16396 bool is_direct_init = false;
16397 bool is_non_constant_init;
16398 int ctor_dtor_or_conv_p;
16399 bool friend_p;
16400 tree pushed_scope = NULL_TREE;
16401 bool range_for_decl_p = false;
16402 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16403
16404 /* Gather the attributes that were provided with the
16405 decl-specifiers. */
16406 prefix_attributes = decl_specifiers->attributes;
16407
16408 /* Assume that this is not the declarator for a function
16409 definition. */
16410 if (function_definition_p)
16411 *function_definition_p = false;
16412
16413 /* Default arguments are only permitted for function parameters. */
16414 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16415 parser->default_arg_ok_p = false;
16416
16417 /* Defer access checks while parsing the declarator; we cannot know
16418 what names are accessible until we know what is being
16419 declared. */
16420 resume_deferring_access_checks ();
16421
16422 /* Parse the declarator. */
16423 token = cp_lexer_peek_token (parser->lexer);
16424 declarator
16425 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16426 &ctor_dtor_or_conv_p,
16427 /*parenthesized_p=*/NULL,
16428 member_p);
16429 /* Gather up the deferred checks. */
16430 stop_deferring_access_checks ();
16431
16432 parser->default_arg_ok_p = saved_default_arg_ok_p;
16433
16434 /* If the DECLARATOR was erroneous, there's no need to go
16435 further. */
16436 if (declarator == cp_error_declarator)
16437 return error_mark_node;
16438
16439 /* Check that the number of template-parameter-lists is OK. */
16440 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16441 token->location))
16442 return error_mark_node;
16443
16444 if (declares_class_or_enum & 2)
16445 cp_parser_check_for_definition_in_return_type (declarator,
16446 decl_specifiers->type,
16447 decl_specifiers->locations[ds_type_spec]);
16448
16449 /* Figure out what scope the entity declared by the DECLARATOR is
16450 located in. `grokdeclarator' sometimes changes the scope, so
16451 we compute it now. */
16452 scope = get_scope_of_declarator (declarator);
16453
16454 /* Perform any lookups in the declared type which were thought to be
16455 dependent, but are not in the scope of the declarator. */
16456 decl_specifiers->type
16457 = maybe_update_decl_type (decl_specifiers->type, scope);
16458
16459 /* If we're allowing GNU extensions, look for an
16460 asm-specification. */
16461 if (cp_parser_allow_gnu_extensions_p (parser))
16462 {
16463 /* Look for an asm-specification. */
16464 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16465 asm_specification = cp_parser_asm_specification_opt (parser);
16466 }
16467 else
16468 asm_specification = NULL_TREE;
16469
16470 /* Look for attributes. */
16471 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16472 attributes = cp_parser_attributes_opt (parser);
16473
16474 /* Peek at the next token. */
16475 token = cp_lexer_peek_token (parser->lexer);
16476
16477 if (function_declarator_p (declarator))
16478 {
16479 /* Check to see if the token indicates the start of a
16480 function-definition. */
16481 if (cp_parser_token_starts_function_definition_p (token))
16482 {
16483 if (!function_definition_allowed_p)
16484 {
16485 /* If a function-definition should not appear here, issue an
16486 error message. */
16487 cp_parser_error (parser,
16488 "a function-definition is not allowed here");
16489 return error_mark_node;
16490 }
16491
16492 location_t func_brace_location
16493 = cp_lexer_peek_token (parser->lexer)->location;
16494
16495 /* Neither attributes nor an asm-specification are allowed
16496 on a function-definition. */
16497 if (asm_specification)
16498 error_at (asm_spec_start_token->location,
16499 "an asm-specification is not allowed "
16500 "on a function-definition");
16501 if (attributes)
16502 error_at (attributes_start_token->location,
16503 "attributes are not allowed "
16504 "on a function-definition");
16505 /* This is a function-definition. */
16506 *function_definition_p = true;
16507
16508 /* Parse the function definition. */
16509 if (member_p)
16510 decl = cp_parser_save_member_function_body (parser,
16511 decl_specifiers,
16512 declarator,
16513 prefix_attributes);
16514 else
16515 decl =
16516 (cp_parser_function_definition_from_specifiers_and_declarator
16517 (parser, decl_specifiers, prefix_attributes, declarator));
16518
16519 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16520 {
16521 /* This is where the prologue starts... */
16522 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16523 = func_brace_location;
16524 }
16525
16526 return decl;
16527 }
16528 }
16529
16530 /* [dcl.dcl]
16531
16532 Only in function declarations for constructors, destructors, and
16533 type conversions can the decl-specifier-seq be omitted.
16534
16535 We explicitly postpone this check past the point where we handle
16536 function-definitions because we tolerate function-definitions
16537 that are missing their return types in some modes. */
16538 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16539 {
16540 cp_parser_error (parser,
16541 "expected constructor, destructor, or type conversion");
16542 return error_mark_node;
16543 }
16544
16545 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16546 if (token->type == CPP_EQ
16547 || token->type == CPP_OPEN_PAREN
16548 || token->type == CPP_OPEN_BRACE)
16549 {
16550 is_initialized = SD_INITIALIZED;
16551 initialization_kind = token->type;
16552 if (maybe_range_for_decl)
16553 *maybe_range_for_decl = error_mark_node;
16554
16555 if (token->type == CPP_EQ
16556 && function_declarator_p (declarator))
16557 {
16558 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16559 if (t2->keyword == RID_DEFAULT)
16560 is_initialized = SD_DEFAULTED;
16561 else if (t2->keyword == RID_DELETE)
16562 is_initialized = SD_DELETED;
16563 }
16564 }
16565 else
16566 {
16567 /* If the init-declarator isn't initialized and isn't followed by a
16568 `,' or `;', it's not a valid init-declarator. */
16569 if (token->type != CPP_COMMA
16570 && token->type != CPP_SEMICOLON)
16571 {
16572 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16573 range_for_decl_p = true;
16574 else
16575 {
16576 cp_parser_error (parser, "expected initializer");
16577 return error_mark_node;
16578 }
16579 }
16580 is_initialized = SD_UNINITIALIZED;
16581 initialization_kind = CPP_EOF;
16582 }
16583
16584 /* Because start_decl has side-effects, we should only call it if we
16585 know we're going ahead. By this point, we know that we cannot
16586 possibly be looking at any other construct. */
16587 cp_parser_commit_to_tentative_parse (parser);
16588
16589 /* If the decl specifiers were bad, issue an error now that we're
16590 sure this was intended to be a declarator. Then continue
16591 declaring the variable(s), as int, to try to cut down on further
16592 errors. */
16593 if (decl_specifiers->any_specifiers_p
16594 && decl_specifiers->type == error_mark_node)
16595 {
16596 cp_parser_error (parser, "invalid type in declaration");
16597 decl_specifiers->type = integer_type_node;
16598 }
16599
16600 /* Check to see whether or not this declaration is a friend. */
16601 friend_p = cp_parser_friend_p (decl_specifiers);
16602
16603 /* Enter the newly declared entry in the symbol table. If we're
16604 processing a declaration in a class-specifier, we wait until
16605 after processing the initializer. */
16606 if (!member_p)
16607 {
16608 if (parser->in_unbraced_linkage_specification_p)
16609 decl_specifiers->storage_class = sc_extern;
16610 decl = start_decl (declarator, decl_specifiers,
16611 range_for_decl_p? SD_INITIALIZED : is_initialized,
16612 attributes, prefix_attributes, &pushed_scope);
16613 cp_finalize_omp_declare_simd (parser, decl);
16614 /* Adjust location of decl if declarator->id_loc is more appropriate:
16615 set, and decl wasn't merged with another decl, in which case its
16616 location would be different from input_location, and more accurate. */
16617 if (DECL_P (decl)
16618 && declarator->id_loc != UNKNOWN_LOCATION
16619 && DECL_SOURCE_LOCATION (decl) == input_location)
16620 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16621 }
16622 else if (scope)
16623 /* Enter the SCOPE. That way unqualified names appearing in the
16624 initializer will be looked up in SCOPE. */
16625 pushed_scope = push_scope (scope);
16626
16627 /* Perform deferred access control checks, now that we know in which
16628 SCOPE the declared entity resides. */
16629 if (!member_p && decl)
16630 {
16631 tree saved_current_function_decl = NULL_TREE;
16632
16633 /* If the entity being declared is a function, pretend that we
16634 are in its scope. If it is a `friend', it may have access to
16635 things that would not otherwise be accessible. */
16636 if (TREE_CODE (decl) == FUNCTION_DECL)
16637 {
16638 saved_current_function_decl = current_function_decl;
16639 current_function_decl = decl;
16640 }
16641
16642 /* Perform access checks for template parameters. */
16643 cp_parser_perform_template_parameter_access_checks (checks);
16644
16645 /* Perform the access control checks for the declarator and the
16646 decl-specifiers. */
16647 perform_deferred_access_checks (tf_warning_or_error);
16648
16649 /* Restore the saved value. */
16650 if (TREE_CODE (decl) == FUNCTION_DECL)
16651 current_function_decl = saved_current_function_decl;
16652 }
16653
16654 /* Parse the initializer. */
16655 initializer = NULL_TREE;
16656 is_direct_init = false;
16657 is_non_constant_init = true;
16658 if (is_initialized)
16659 {
16660 if (function_declarator_p (declarator))
16661 {
16662 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16663 if (initialization_kind == CPP_EQ)
16664 initializer = cp_parser_pure_specifier (parser);
16665 else
16666 {
16667 /* If the declaration was erroneous, we don't really
16668 know what the user intended, so just silently
16669 consume the initializer. */
16670 if (decl != error_mark_node)
16671 error_at (initializer_start_token->location,
16672 "initializer provided for function");
16673 cp_parser_skip_to_closing_parenthesis (parser,
16674 /*recovering=*/true,
16675 /*or_comma=*/false,
16676 /*consume_paren=*/true);
16677 }
16678 }
16679 else
16680 {
16681 /* We want to record the extra mangling scope for in-class
16682 initializers of class members and initializers of static data
16683 member templates. The former involves deferring
16684 parsing of the initializer until end of class as with default
16685 arguments. So right here we only handle the latter. */
16686 if (!member_p && processing_template_decl)
16687 start_lambda_scope (decl);
16688 initializer = cp_parser_initializer (parser,
16689 &is_direct_init,
16690 &is_non_constant_init);
16691 if (!member_p && processing_template_decl)
16692 finish_lambda_scope ();
16693 if (initializer == error_mark_node)
16694 cp_parser_skip_to_end_of_statement (parser);
16695 }
16696 }
16697
16698 /* The old parser allows attributes to appear after a parenthesized
16699 initializer. Mark Mitchell proposed removing this functionality
16700 on the GCC mailing lists on 2002-08-13. This parser accepts the
16701 attributes -- but ignores them. */
16702 if (cp_parser_allow_gnu_extensions_p (parser)
16703 && initialization_kind == CPP_OPEN_PAREN)
16704 if (cp_parser_attributes_opt (parser))
16705 warning (OPT_Wattributes,
16706 "attributes after parenthesized initializer ignored");
16707
16708 /* For an in-class declaration, use `grokfield' to create the
16709 declaration. */
16710 if (member_p)
16711 {
16712 if (pushed_scope)
16713 {
16714 pop_scope (pushed_scope);
16715 pushed_scope = NULL_TREE;
16716 }
16717 decl = grokfield (declarator, decl_specifiers,
16718 initializer, !is_non_constant_init,
16719 /*asmspec=*/NULL_TREE,
16720 chainon (attributes, prefix_attributes));
16721 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16722 cp_parser_save_default_args (parser, decl);
16723 cp_finalize_omp_declare_simd (parser, decl);
16724 }
16725
16726 /* Finish processing the declaration. But, skip member
16727 declarations. */
16728 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16729 {
16730 cp_finish_decl (decl,
16731 initializer, !is_non_constant_init,
16732 asm_specification,
16733 /* If the initializer is in parentheses, then this is
16734 a direct-initialization, which means that an
16735 `explicit' constructor is OK. Otherwise, an
16736 `explicit' constructor cannot be used. */
16737 ((is_direct_init || !is_initialized)
16738 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16739 }
16740 else if ((cxx_dialect != cxx98) && friend_p
16741 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16742 /* Core issue #226 (C++0x only): A default template-argument
16743 shall not be specified in a friend class template
16744 declaration. */
16745 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16746 /*is_partial=*/false, /*is_friend_decl=*/1);
16747
16748 if (!friend_p && pushed_scope)
16749 pop_scope (pushed_scope);
16750
16751 if (function_declarator_p (declarator)
16752 && parser->fully_implicit_function_template_p)
16753 {
16754 if (member_p)
16755 decl = finish_fully_implicit_template (parser, decl);
16756 else
16757 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16758 }
16759
16760 return decl;
16761 }
16762
16763 /* Parse a declarator.
16764
16765 declarator:
16766 direct-declarator
16767 ptr-operator declarator
16768
16769 abstract-declarator:
16770 ptr-operator abstract-declarator [opt]
16771 direct-abstract-declarator
16772
16773 GNU Extensions:
16774
16775 declarator:
16776 attributes [opt] direct-declarator
16777 attributes [opt] ptr-operator declarator
16778
16779 abstract-declarator:
16780 attributes [opt] ptr-operator abstract-declarator [opt]
16781 attributes [opt] direct-abstract-declarator
16782
16783 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16784 detect constructor, destructor or conversion operators. It is set
16785 to -1 if the declarator is a name, and +1 if it is a
16786 function. Otherwise it is set to zero. Usually you just want to
16787 test for >0, but internally the negative value is used.
16788
16789 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16790 a decl-specifier-seq unless it declares a constructor, destructor,
16791 or conversion. It might seem that we could check this condition in
16792 semantic analysis, rather than parsing, but that makes it difficult
16793 to handle something like `f()'. We want to notice that there are
16794 no decl-specifiers, and therefore realize that this is an
16795 expression, not a declaration.)
16796
16797 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16798 the declarator is a direct-declarator of the form "(...)".
16799
16800 MEMBER_P is true iff this declarator is a member-declarator. */
16801
16802 static cp_declarator *
16803 cp_parser_declarator (cp_parser* parser,
16804 cp_parser_declarator_kind dcl_kind,
16805 int* ctor_dtor_or_conv_p,
16806 bool* parenthesized_p,
16807 bool member_p)
16808 {
16809 cp_declarator *declarator;
16810 enum tree_code code;
16811 cp_cv_quals cv_quals;
16812 tree class_type;
16813 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16814
16815 /* Assume this is not a constructor, destructor, or type-conversion
16816 operator. */
16817 if (ctor_dtor_or_conv_p)
16818 *ctor_dtor_or_conv_p = 0;
16819
16820 if (cp_parser_allow_gnu_extensions_p (parser))
16821 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16822
16823 /* Check for the ptr-operator production. */
16824 cp_parser_parse_tentatively (parser);
16825 /* Parse the ptr-operator. */
16826 code = cp_parser_ptr_operator (parser,
16827 &class_type,
16828 &cv_quals,
16829 &std_attributes);
16830
16831 /* If that worked, then we have a ptr-operator. */
16832 if (cp_parser_parse_definitely (parser))
16833 {
16834 /* If a ptr-operator was found, then this declarator was not
16835 parenthesized. */
16836 if (parenthesized_p)
16837 *parenthesized_p = true;
16838 /* The dependent declarator is optional if we are parsing an
16839 abstract-declarator. */
16840 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16841 cp_parser_parse_tentatively (parser);
16842
16843 /* Parse the dependent declarator. */
16844 declarator = cp_parser_declarator (parser, dcl_kind,
16845 /*ctor_dtor_or_conv_p=*/NULL,
16846 /*parenthesized_p=*/NULL,
16847 /*member_p=*/false);
16848
16849 /* If we are parsing an abstract-declarator, we must handle the
16850 case where the dependent declarator is absent. */
16851 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16852 && !cp_parser_parse_definitely (parser))
16853 declarator = NULL;
16854
16855 declarator = cp_parser_make_indirect_declarator
16856 (code, class_type, cv_quals, declarator, std_attributes);
16857 }
16858 /* Everything else is a direct-declarator. */
16859 else
16860 {
16861 if (parenthesized_p)
16862 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16863 CPP_OPEN_PAREN);
16864 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16865 ctor_dtor_or_conv_p,
16866 member_p);
16867 }
16868
16869 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16870 declarator->attributes = gnu_attributes;
16871 return declarator;
16872 }
16873
16874 /* Parse a direct-declarator or direct-abstract-declarator.
16875
16876 direct-declarator:
16877 declarator-id
16878 direct-declarator ( parameter-declaration-clause )
16879 cv-qualifier-seq [opt]
16880 ref-qualifier [opt]
16881 exception-specification [opt]
16882 direct-declarator [ constant-expression [opt] ]
16883 ( declarator )
16884
16885 direct-abstract-declarator:
16886 direct-abstract-declarator [opt]
16887 ( parameter-declaration-clause )
16888 cv-qualifier-seq [opt]
16889 ref-qualifier [opt]
16890 exception-specification [opt]
16891 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16892 ( abstract-declarator )
16893
16894 Returns a representation of the declarator. DCL_KIND is
16895 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16896 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16897 we are parsing a direct-declarator. It is
16898 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16899 of ambiguity we prefer an abstract declarator, as per
16900 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16901 cp_parser_declarator. */
16902
16903 static cp_declarator *
16904 cp_parser_direct_declarator (cp_parser* parser,
16905 cp_parser_declarator_kind dcl_kind,
16906 int* ctor_dtor_or_conv_p,
16907 bool member_p)
16908 {
16909 cp_token *token;
16910 cp_declarator *declarator = NULL;
16911 tree scope = NULL_TREE;
16912 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16913 bool saved_in_declarator_p = parser->in_declarator_p;
16914 bool first = true;
16915 tree pushed_scope = NULL_TREE;
16916
16917 while (true)
16918 {
16919 /* Peek at the next token. */
16920 token = cp_lexer_peek_token (parser->lexer);
16921 if (token->type == CPP_OPEN_PAREN)
16922 {
16923 /* This is either a parameter-declaration-clause, or a
16924 parenthesized declarator. When we know we are parsing a
16925 named declarator, it must be a parenthesized declarator
16926 if FIRST is true. For instance, `(int)' is a
16927 parameter-declaration-clause, with an omitted
16928 direct-abstract-declarator. But `((*))', is a
16929 parenthesized abstract declarator. Finally, when T is a
16930 template parameter `(T)' is a
16931 parameter-declaration-clause, and not a parenthesized
16932 named declarator.
16933
16934 We first try and parse a parameter-declaration-clause,
16935 and then try a nested declarator (if FIRST is true).
16936
16937 It is not an error for it not to be a
16938 parameter-declaration-clause, even when FIRST is
16939 false. Consider,
16940
16941 int i (int);
16942 int i (3);
16943
16944 The first is the declaration of a function while the
16945 second is the definition of a variable, including its
16946 initializer.
16947
16948 Having seen only the parenthesis, we cannot know which of
16949 these two alternatives should be selected. Even more
16950 complex are examples like:
16951
16952 int i (int (a));
16953 int i (int (3));
16954
16955 The former is a function-declaration; the latter is a
16956 variable initialization.
16957
16958 Thus again, we try a parameter-declaration-clause, and if
16959 that fails, we back out and return. */
16960
16961 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16962 {
16963 tree params;
16964 unsigned saved_num_template_parameter_lists;
16965 bool is_declarator = false;
16966
16967 /* In a member-declarator, the only valid interpretation
16968 of a parenthesis is the start of a
16969 parameter-declaration-clause. (It is invalid to
16970 initialize a static data member with a parenthesized
16971 initializer; only the "=" form of initialization is
16972 permitted.) */
16973 if (!member_p)
16974 cp_parser_parse_tentatively (parser);
16975
16976 /* Consume the `('. */
16977 cp_lexer_consume_token (parser->lexer);
16978 if (first)
16979 {
16980 /* If this is going to be an abstract declarator, we're
16981 in a declarator and we can't have default args. */
16982 parser->default_arg_ok_p = false;
16983 parser->in_declarator_p = true;
16984 }
16985
16986 /* Inside the function parameter list, surrounding
16987 template-parameter-lists do not apply. */
16988 saved_num_template_parameter_lists
16989 = parser->num_template_parameter_lists;
16990 parser->num_template_parameter_lists = 0;
16991
16992 begin_scope (sk_function_parms, NULL_TREE);
16993
16994 /* Parse the parameter-declaration-clause. */
16995 params = cp_parser_parameter_declaration_clause (parser);
16996
16997 /* Restore saved template parameter lists accounting for implicit
16998 template parameters. */
16999 parser->num_template_parameter_lists
17000 += saved_num_template_parameter_lists;
17001
17002 /* Consume the `)'. */
17003 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17004
17005 /* If all went well, parse the cv-qualifier-seq,
17006 ref-qualifier and the exception-specification. */
17007 if (member_p || cp_parser_parse_definitely (parser))
17008 {
17009 cp_cv_quals cv_quals;
17010 cp_virt_specifiers virt_specifiers;
17011 cp_ref_qualifier ref_qual;
17012 tree exception_specification;
17013 tree late_return;
17014 tree attrs;
17015 bool memfn = (member_p || (pushed_scope
17016 && CLASS_TYPE_P (pushed_scope)));
17017
17018 is_declarator = true;
17019
17020 if (ctor_dtor_or_conv_p)
17021 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17022 first = false;
17023
17024 /* Parse the cv-qualifier-seq. */
17025 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17026 /* Parse the ref-qualifier. */
17027 ref_qual = cp_parser_ref_qualifier_opt (parser);
17028 /* And the exception-specification. */
17029 exception_specification
17030 = cp_parser_exception_specification_opt (parser);
17031
17032 attrs = cp_parser_std_attribute_spec_seq (parser);
17033
17034 late_return = (cp_parser_late_return_type_opt
17035 (parser, declarator,
17036 memfn ? cv_quals : -1));
17037
17038
17039 /* Parse the virt-specifier-seq. */
17040 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17041
17042 /* Create the function-declarator. */
17043 declarator = make_call_declarator (declarator,
17044 params,
17045 cv_quals,
17046 virt_specifiers,
17047 ref_qual,
17048 exception_specification,
17049 late_return);
17050 declarator->std_attributes = attrs;
17051 /* Any subsequent parameter lists are to do with
17052 return type, so are not those of the declared
17053 function. */
17054 parser->default_arg_ok_p = false;
17055 }
17056
17057 /* Remove the function parms from scope. */
17058 pop_bindings_and_leave_scope ();
17059
17060 if (is_declarator)
17061 /* Repeat the main loop. */
17062 continue;
17063 }
17064
17065 /* If this is the first, we can try a parenthesized
17066 declarator. */
17067 if (first)
17068 {
17069 bool saved_in_type_id_in_expr_p;
17070
17071 parser->default_arg_ok_p = saved_default_arg_ok_p;
17072 parser->in_declarator_p = saved_in_declarator_p;
17073
17074 /* Consume the `('. */
17075 cp_lexer_consume_token (parser->lexer);
17076 /* Parse the nested declarator. */
17077 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17078 parser->in_type_id_in_expr_p = true;
17079 declarator
17080 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17081 /*parenthesized_p=*/NULL,
17082 member_p);
17083 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17084 first = false;
17085 /* Expect a `)'. */
17086 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17087 declarator = cp_error_declarator;
17088 if (declarator == cp_error_declarator)
17089 break;
17090
17091 goto handle_declarator;
17092 }
17093 /* Otherwise, we must be done. */
17094 else
17095 break;
17096 }
17097 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17098 && token->type == CPP_OPEN_SQUARE
17099 && !cp_next_tokens_can_be_attribute_p (parser))
17100 {
17101 /* Parse an array-declarator. */
17102 tree bounds, attrs;
17103
17104 if (ctor_dtor_or_conv_p)
17105 *ctor_dtor_or_conv_p = 0;
17106
17107 first = false;
17108 parser->default_arg_ok_p = false;
17109 parser->in_declarator_p = true;
17110 /* Consume the `['. */
17111 cp_lexer_consume_token (parser->lexer);
17112 /* Peek at the next token. */
17113 token = cp_lexer_peek_token (parser->lexer);
17114 /* If the next token is `]', then there is no
17115 constant-expression. */
17116 if (token->type != CPP_CLOSE_SQUARE)
17117 {
17118 bool non_constant_p;
17119 bounds
17120 = cp_parser_constant_expression (parser,
17121 /*allow_non_constant=*/true,
17122 &non_constant_p);
17123 if (!non_constant_p)
17124 /* OK */;
17125 else if (error_operand_p (bounds))
17126 /* Already gave an error. */;
17127 else if (!parser->in_function_body
17128 || current_binding_level->kind == sk_function_parms)
17129 {
17130 /* Normally, the array bound must be an integral constant
17131 expression. However, as an extension, we allow VLAs
17132 in function scopes as long as they aren't part of a
17133 parameter declaration. */
17134 cp_parser_error (parser,
17135 "array bound is not an integer constant");
17136 bounds = error_mark_node;
17137 }
17138 else if (processing_template_decl)
17139 {
17140 /* Remember this wasn't a constant-expression. */
17141 bounds = build_nop (TREE_TYPE (bounds), bounds);
17142 TREE_SIDE_EFFECTS (bounds) = 1;
17143 }
17144 }
17145 else
17146 bounds = NULL_TREE;
17147 /* Look for the closing `]'. */
17148 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17149 {
17150 declarator = cp_error_declarator;
17151 break;
17152 }
17153
17154 attrs = cp_parser_std_attribute_spec_seq (parser);
17155 declarator = make_array_declarator (declarator, bounds);
17156 declarator->std_attributes = attrs;
17157 }
17158 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17159 {
17160 {
17161 tree qualifying_scope;
17162 tree unqualified_name;
17163 tree attrs;
17164 special_function_kind sfk;
17165 bool abstract_ok;
17166 bool pack_expansion_p = false;
17167 cp_token *declarator_id_start_token;
17168
17169 /* Parse a declarator-id */
17170 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17171 if (abstract_ok)
17172 {
17173 cp_parser_parse_tentatively (parser);
17174
17175 /* If we see an ellipsis, we should be looking at a
17176 parameter pack. */
17177 if (token->type == CPP_ELLIPSIS)
17178 {
17179 /* Consume the `...' */
17180 cp_lexer_consume_token (parser->lexer);
17181
17182 pack_expansion_p = true;
17183 }
17184 }
17185
17186 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17187 unqualified_name
17188 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17189 qualifying_scope = parser->scope;
17190 if (abstract_ok)
17191 {
17192 bool okay = false;
17193
17194 if (!unqualified_name && pack_expansion_p)
17195 {
17196 /* Check whether an error occurred. */
17197 okay = !cp_parser_error_occurred (parser);
17198
17199 /* We already consumed the ellipsis to mark a
17200 parameter pack, but we have no way to report it,
17201 so abort the tentative parse. We will be exiting
17202 immediately anyway. */
17203 cp_parser_abort_tentative_parse (parser);
17204 }
17205 else
17206 okay = cp_parser_parse_definitely (parser);
17207
17208 if (!okay)
17209 unqualified_name = error_mark_node;
17210 else if (unqualified_name
17211 && (qualifying_scope
17212 || (!identifier_p (unqualified_name))))
17213 {
17214 cp_parser_error (parser, "expected unqualified-id");
17215 unqualified_name = error_mark_node;
17216 }
17217 }
17218
17219 if (!unqualified_name)
17220 return NULL;
17221 if (unqualified_name == error_mark_node)
17222 {
17223 declarator = cp_error_declarator;
17224 pack_expansion_p = false;
17225 declarator->parameter_pack_p = false;
17226 break;
17227 }
17228
17229 attrs = cp_parser_std_attribute_spec_seq (parser);
17230
17231 if (qualifying_scope && at_namespace_scope_p ()
17232 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17233 {
17234 /* In the declaration of a member of a template class
17235 outside of the class itself, the SCOPE will sometimes
17236 be a TYPENAME_TYPE. For example, given:
17237
17238 template <typename T>
17239 int S<T>::R::i = 3;
17240
17241 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17242 this context, we must resolve S<T>::R to an ordinary
17243 type, rather than a typename type.
17244
17245 The reason we normally avoid resolving TYPENAME_TYPEs
17246 is that a specialization of `S' might render
17247 `S<T>::R' not a type. However, if `S' is
17248 specialized, then this `i' will not be used, so there
17249 is no harm in resolving the types here. */
17250 tree type;
17251
17252 /* Resolve the TYPENAME_TYPE. */
17253 type = resolve_typename_type (qualifying_scope,
17254 /*only_current_p=*/false);
17255 /* If that failed, the declarator is invalid. */
17256 if (TREE_CODE (type) == TYPENAME_TYPE)
17257 {
17258 if (typedef_variant_p (type))
17259 error_at (declarator_id_start_token->location,
17260 "cannot define member of dependent typedef "
17261 "%qT", type);
17262 else
17263 error_at (declarator_id_start_token->location,
17264 "%<%T::%E%> is not a type",
17265 TYPE_CONTEXT (qualifying_scope),
17266 TYPE_IDENTIFIER (qualifying_scope));
17267 }
17268 qualifying_scope = type;
17269 }
17270
17271 sfk = sfk_none;
17272
17273 if (unqualified_name)
17274 {
17275 tree class_type;
17276
17277 if (qualifying_scope
17278 && CLASS_TYPE_P (qualifying_scope))
17279 class_type = qualifying_scope;
17280 else
17281 class_type = current_class_type;
17282
17283 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17284 {
17285 tree name_type = TREE_TYPE (unqualified_name);
17286 if (class_type && same_type_p (name_type, class_type))
17287 {
17288 if (qualifying_scope
17289 && CLASSTYPE_USE_TEMPLATE (name_type))
17290 {
17291 error_at (declarator_id_start_token->location,
17292 "invalid use of constructor as a template");
17293 inform (declarator_id_start_token->location,
17294 "use %<%T::%D%> instead of %<%T::%D%> to "
17295 "name the constructor in a qualified name",
17296 class_type,
17297 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17298 class_type, name_type);
17299 declarator = cp_error_declarator;
17300 break;
17301 }
17302 else
17303 unqualified_name = constructor_name (class_type);
17304 }
17305 else
17306 {
17307 /* We do not attempt to print the declarator
17308 here because we do not have enough
17309 information about its original syntactic
17310 form. */
17311 cp_parser_error (parser, "invalid declarator");
17312 declarator = cp_error_declarator;
17313 break;
17314 }
17315 }
17316
17317 if (class_type)
17318 {
17319 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17320 sfk = sfk_destructor;
17321 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17322 sfk = sfk_conversion;
17323 else if (/* There's no way to declare a constructor
17324 for an anonymous type, even if the type
17325 got a name for linkage purposes. */
17326 !TYPE_WAS_ANONYMOUS (class_type)
17327 && constructor_name_p (unqualified_name,
17328 class_type))
17329 {
17330 unqualified_name = constructor_name (class_type);
17331 sfk = sfk_constructor;
17332 }
17333 else if (is_overloaded_fn (unqualified_name)
17334 && DECL_CONSTRUCTOR_P (get_first_fn
17335 (unqualified_name)))
17336 sfk = sfk_constructor;
17337
17338 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17339 *ctor_dtor_or_conv_p = -1;
17340 }
17341 }
17342 declarator = make_id_declarator (qualifying_scope,
17343 unqualified_name,
17344 sfk);
17345 declarator->std_attributes = attrs;
17346 declarator->id_loc = token->location;
17347 declarator->parameter_pack_p = pack_expansion_p;
17348
17349 if (pack_expansion_p)
17350 maybe_warn_variadic_templates ();
17351 }
17352
17353 handle_declarator:;
17354 scope = get_scope_of_declarator (declarator);
17355 if (scope)
17356 {
17357 /* Any names that appear after the declarator-id for a
17358 member are looked up in the containing scope. */
17359 if (at_function_scope_p ())
17360 {
17361 /* But declarations with qualified-ids can't appear in a
17362 function. */
17363 cp_parser_error (parser, "qualified-id in declaration");
17364 break;
17365 }
17366 pushed_scope = push_scope (scope);
17367 }
17368 parser->in_declarator_p = true;
17369 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17370 || (declarator && declarator->kind == cdk_id))
17371 /* Default args are only allowed on function
17372 declarations. */
17373 parser->default_arg_ok_p = saved_default_arg_ok_p;
17374 else
17375 parser->default_arg_ok_p = false;
17376
17377 first = false;
17378 }
17379 /* We're done. */
17380 else
17381 break;
17382 }
17383
17384 /* For an abstract declarator, we might wind up with nothing at this
17385 point. That's an error; the declarator is not optional. */
17386 if (!declarator)
17387 cp_parser_error (parser, "expected declarator");
17388
17389 /* If we entered a scope, we must exit it now. */
17390 if (pushed_scope)
17391 pop_scope (pushed_scope);
17392
17393 parser->default_arg_ok_p = saved_default_arg_ok_p;
17394 parser->in_declarator_p = saved_in_declarator_p;
17395
17396 return declarator;
17397 }
17398
17399 /* Parse a ptr-operator.
17400
17401 ptr-operator:
17402 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17403 * cv-qualifier-seq [opt]
17404 &
17405 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17406 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17407
17408 GNU Extension:
17409
17410 ptr-operator:
17411 & cv-qualifier-seq [opt]
17412
17413 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17414 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17415 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17416 filled in with the TYPE containing the member. *CV_QUALS is
17417 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17418 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17419 Note that the tree codes returned by this function have nothing
17420 to do with the types of trees that will be eventually be created
17421 to represent the pointer or reference type being parsed. They are
17422 just constants with suggestive names. */
17423 static enum tree_code
17424 cp_parser_ptr_operator (cp_parser* parser,
17425 tree* type,
17426 cp_cv_quals *cv_quals,
17427 tree *attributes)
17428 {
17429 enum tree_code code = ERROR_MARK;
17430 cp_token *token;
17431 tree attrs = NULL_TREE;
17432
17433 /* Assume that it's not a pointer-to-member. */
17434 *type = NULL_TREE;
17435 /* And that there are no cv-qualifiers. */
17436 *cv_quals = TYPE_UNQUALIFIED;
17437
17438 /* Peek at the next token. */
17439 token = cp_lexer_peek_token (parser->lexer);
17440
17441 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17442 if (token->type == CPP_MULT)
17443 code = INDIRECT_REF;
17444 else if (token->type == CPP_AND)
17445 code = ADDR_EXPR;
17446 else if ((cxx_dialect != cxx98) &&
17447 token->type == CPP_AND_AND) /* C++0x only */
17448 code = NON_LVALUE_EXPR;
17449
17450 if (code != ERROR_MARK)
17451 {
17452 /* Consume the `*', `&' or `&&'. */
17453 cp_lexer_consume_token (parser->lexer);
17454
17455 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17456 `&', if we are allowing GNU extensions. (The only qualifier
17457 that can legally appear after `&' is `restrict', but that is
17458 enforced during semantic analysis. */
17459 if (code == INDIRECT_REF
17460 || cp_parser_allow_gnu_extensions_p (parser))
17461 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17462
17463 attrs = cp_parser_std_attribute_spec_seq (parser);
17464 if (attributes != NULL)
17465 *attributes = attrs;
17466 }
17467 else
17468 {
17469 /* Try the pointer-to-member case. */
17470 cp_parser_parse_tentatively (parser);
17471 /* Look for the optional `::' operator. */
17472 cp_parser_global_scope_opt (parser,
17473 /*current_scope_valid_p=*/false);
17474 /* Look for the nested-name specifier. */
17475 token = cp_lexer_peek_token (parser->lexer);
17476 cp_parser_nested_name_specifier (parser,
17477 /*typename_keyword_p=*/false,
17478 /*check_dependency_p=*/true,
17479 /*type_p=*/false,
17480 /*is_declaration=*/false);
17481 /* If we found it, and the next token is a `*', then we are
17482 indeed looking at a pointer-to-member operator. */
17483 if (!cp_parser_error_occurred (parser)
17484 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17485 {
17486 /* Indicate that the `*' operator was used. */
17487 code = INDIRECT_REF;
17488
17489 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17490 error_at (token->location, "%qD is a namespace", parser->scope);
17491 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17492 error_at (token->location, "cannot form pointer to member of "
17493 "non-class %q#T", parser->scope);
17494 else
17495 {
17496 /* The type of which the member is a member is given by the
17497 current SCOPE. */
17498 *type = parser->scope;
17499 /* The next name will not be qualified. */
17500 parser->scope = NULL_TREE;
17501 parser->qualifying_scope = NULL_TREE;
17502 parser->object_scope = NULL_TREE;
17503 /* Look for optional c++11 attributes. */
17504 attrs = cp_parser_std_attribute_spec_seq (parser);
17505 if (attributes != NULL)
17506 *attributes = attrs;
17507 /* Look for the optional cv-qualifier-seq. */
17508 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17509 }
17510 }
17511 /* If that didn't work we don't have a ptr-operator. */
17512 if (!cp_parser_parse_definitely (parser))
17513 cp_parser_error (parser, "expected ptr-operator");
17514 }
17515
17516 return code;
17517 }
17518
17519 /* Parse an (optional) cv-qualifier-seq.
17520
17521 cv-qualifier-seq:
17522 cv-qualifier cv-qualifier-seq [opt]
17523
17524 cv-qualifier:
17525 const
17526 volatile
17527
17528 GNU Extension:
17529
17530 cv-qualifier:
17531 __restrict__
17532
17533 Returns a bitmask representing the cv-qualifiers. */
17534
17535 static cp_cv_quals
17536 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17537 {
17538 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17539
17540 while (true)
17541 {
17542 cp_token *token;
17543 cp_cv_quals cv_qualifier;
17544
17545 /* Peek at the next token. */
17546 token = cp_lexer_peek_token (parser->lexer);
17547 /* See if it's a cv-qualifier. */
17548 switch (token->keyword)
17549 {
17550 case RID_CONST:
17551 cv_qualifier = TYPE_QUAL_CONST;
17552 break;
17553
17554 case RID_VOLATILE:
17555 cv_qualifier = TYPE_QUAL_VOLATILE;
17556 break;
17557
17558 case RID_RESTRICT:
17559 cv_qualifier = TYPE_QUAL_RESTRICT;
17560 break;
17561
17562 default:
17563 cv_qualifier = TYPE_UNQUALIFIED;
17564 break;
17565 }
17566
17567 if (!cv_qualifier)
17568 break;
17569
17570 if (cv_quals & cv_qualifier)
17571 {
17572 error_at (token->location, "duplicate cv-qualifier");
17573 cp_lexer_purge_token (parser->lexer);
17574 }
17575 else
17576 {
17577 cp_lexer_consume_token (parser->lexer);
17578 cv_quals |= cv_qualifier;
17579 }
17580 }
17581
17582 return cv_quals;
17583 }
17584
17585 /* Parse an (optional) ref-qualifier
17586
17587 ref-qualifier:
17588 &
17589 &&
17590
17591 Returns cp_ref_qualifier representing ref-qualifier. */
17592
17593 static cp_ref_qualifier
17594 cp_parser_ref_qualifier_opt (cp_parser* parser)
17595 {
17596 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17597
17598 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17599 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17600 return ref_qual;
17601
17602 while (true)
17603 {
17604 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17605 cp_token *token = cp_lexer_peek_token (parser->lexer);
17606
17607 switch (token->type)
17608 {
17609 case CPP_AND:
17610 curr_ref_qual = REF_QUAL_LVALUE;
17611 break;
17612
17613 case CPP_AND_AND:
17614 curr_ref_qual = REF_QUAL_RVALUE;
17615 break;
17616
17617 default:
17618 curr_ref_qual = REF_QUAL_NONE;
17619 break;
17620 }
17621
17622 if (!curr_ref_qual)
17623 break;
17624 else if (ref_qual)
17625 {
17626 error_at (token->location, "multiple ref-qualifiers");
17627 cp_lexer_purge_token (parser->lexer);
17628 }
17629 else
17630 {
17631 ref_qual = curr_ref_qual;
17632 cp_lexer_consume_token (parser->lexer);
17633 }
17634 }
17635
17636 return ref_qual;
17637 }
17638
17639 /* Parse an (optional) virt-specifier-seq.
17640
17641 virt-specifier-seq:
17642 virt-specifier virt-specifier-seq [opt]
17643
17644 virt-specifier:
17645 override
17646 final
17647
17648 Returns a bitmask representing the virt-specifiers. */
17649
17650 static cp_virt_specifiers
17651 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17652 {
17653 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17654
17655 while (true)
17656 {
17657 cp_token *token;
17658 cp_virt_specifiers virt_specifier;
17659
17660 /* Peek at the next token. */
17661 token = cp_lexer_peek_token (parser->lexer);
17662 /* See if it's a virt-specifier-qualifier. */
17663 if (token->type != CPP_NAME)
17664 break;
17665 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17666 {
17667 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17668 virt_specifier = VIRT_SPEC_OVERRIDE;
17669 }
17670 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17671 {
17672 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17673 virt_specifier = VIRT_SPEC_FINAL;
17674 }
17675 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17676 {
17677 virt_specifier = VIRT_SPEC_FINAL;
17678 }
17679 else
17680 break;
17681
17682 if (virt_specifiers & virt_specifier)
17683 {
17684 error_at (token->location, "duplicate virt-specifier");
17685 cp_lexer_purge_token (parser->lexer);
17686 }
17687 else
17688 {
17689 cp_lexer_consume_token (parser->lexer);
17690 virt_specifiers |= virt_specifier;
17691 }
17692 }
17693 return virt_specifiers;
17694 }
17695
17696 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17697 is in scope even though it isn't real. */
17698
17699 static void
17700 inject_this_parameter (tree ctype, cp_cv_quals quals)
17701 {
17702 tree this_parm;
17703
17704 if (current_class_ptr)
17705 {
17706 /* We don't clear this between NSDMIs. Is it already what we want? */
17707 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17708 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17709 && cp_type_quals (type) == quals)
17710 return;
17711 }
17712
17713 this_parm = build_this_parm (ctype, quals);
17714 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17715 current_class_ptr = NULL_TREE;
17716 current_class_ref
17717 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17718 current_class_ptr = this_parm;
17719 }
17720
17721 /* Return true iff our current scope is a non-static data member
17722 initializer. */
17723
17724 bool
17725 parsing_nsdmi (void)
17726 {
17727 /* We recognize NSDMI context by the context-less 'this' pointer set up
17728 by the function above. */
17729 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17730 return true;
17731 return false;
17732 }
17733
17734 /* Parse a late-specified return type, if any. This is not a separate
17735 non-terminal, but part of a function declarator, which looks like
17736
17737 -> trailing-type-specifier-seq abstract-declarator(opt)
17738
17739 Returns the type indicated by the type-id.
17740
17741 In addition to this this parses any queued up omp declare simd
17742 clauses.
17743
17744 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17745 function. */
17746
17747 static tree
17748 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17749 cp_cv_quals quals)
17750 {
17751 cp_token *token;
17752 tree type = NULL_TREE;
17753 bool declare_simd_p = (parser->omp_declare_simd
17754 && declarator
17755 && declarator->kind == cdk_id);
17756
17757 /* Peek at the next token. */
17758 token = cp_lexer_peek_token (parser->lexer);
17759 /* A late-specified return type is indicated by an initial '->'. */
17760 if (token->type != CPP_DEREF && !declare_simd_p)
17761 return NULL_TREE;
17762
17763 tree save_ccp = current_class_ptr;
17764 tree save_ccr = current_class_ref;
17765 if (quals >= 0)
17766 {
17767 /* DR 1207: 'this' is in scope in the trailing return type. */
17768 inject_this_parameter (current_class_type, quals);
17769 }
17770
17771 if (token->type == CPP_DEREF)
17772 {
17773 /* Consume the ->. */
17774 cp_lexer_consume_token (parser->lexer);
17775
17776 type = cp_parser_trailing_type_id (parser);
17777 }
17778
17779 if (declare_simd_p)
17780 declarator->std_attributes
17781 = cp_parser_late_parsing_omp_declare_simd (parser,
17782 declarator->std_attributes);
17783
17784 if (quals >= 0)
17785 {
17786 current_class_ptr = save_ccp;
17787 current_class_ref = save_ccr;
17788 }
17789
17790 return type;
17791 }
17792
17793 /* Parse a declarator-id.
17794
17795 declarator-id:
17796 id-expression
17797 :: [opt] nested-name-specifier [opt] type-name
17798
17799 In the `id-expression' case, the value returned is as for
17800 cp_parser_id_expression if the id-expression was an unqualified-id.
17801 If the id-expression was a qualified-id, then a SCOPE_REF is
17802 returned. The first operand is the scope (either a NAMESPACE_DECL
17803 or TREE_TYPE), but the second is still just a representation of an
17804 unqualified-id. */
17805
17806 static tree
17807 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17808 {
17809 tree id;
17810 /* The expression must be an id-expression. Assume that qualified
17811 names are the names of types so that:
17812
17813 template <class T>
17814 int S<T>::R::i = 3;
17815
17816 will work; we must treat `S<T>::R' as the name of a type.
17817 Similarly, assume that qualified names are templates, where
17818 required, so that:
17819
17820 template <class T>
17821 int S<T>::R<T>::i = 3;
17822
17823 will work, too. */
17824 id = cp_parser_id_expression (parser,
17825 /*template_keyword_p=*/false,
17826 /*check_dependency_p=*/false,
17827 /*template_p=*/NULL,
17828 /*declarator_p=*/true,
17829 optional_p);
17830 if (id && BASELINK_P (id))
17831 id = BASELINK_FUNCTIONS (id);
17832 return id;
17833 }
17834
17835 /* Parse a type-id.
17836
17837 type-id:
17838 type-specifier-seq abstract-declarator [opt]
17839
17840 Returns the TYPE specified. */
17841
17842 static tree
17843 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17844 bool is_trailing_return)
17845 {
17846 cp_decl_specifier_seq type_specifier_seq;
17847 cp_declarator *abstract_declarator;
17848
17849 /* Parse the type-specifier-seq. */
17850 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17851 is_trailing_return,
17852 &type_specifier_seq);
17853 if (type_specifier_seq.type == error_mark_node)
17854 return error_mark_node;
17855
17856 /* There might or might not be an abstract declarator. */
17857 cp_parser_parse_tentatively (parser);
17858 /* Look for the declarator. */
17859 abstract_declarator
17860 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17861 /*parenthesized_p=*/NULL,
17862 /*member_p=*/false);
17863 /* Check to see if there really was a declarator. */
17864 if (!cp_parser_parse_definitely (parser))
17865 abstract_declarator = NULL;
17866
17867 if (type_specifier_seq.type
17868 && cxx_dialect < cxx1y
17869 && type_uses_auto (type_specifier_seq.type))
17870 {
17871 /* A type-id with type 'auto' is only ok if the abstract declarator
17872 is a function declarator with a late-specified return type. */
17873 if (abstract_declarator
17874 && abstract_declarator->kind == cdk_function
17875 && abstract_declarator->u.function.late_return_type)
17876 /* OK */;
17877 else
17878 {
17879 error ("invalid use of %<auto%>");
17880 return error_mark_node;
17881 }
17882 }
17883
17884 return groktypename (&type_specifier_seq, abstract_declarator,
17885 is_template_arg);
17886 }
17887
17888 static tree cp_parser_type_id (cp_parser *parser)
17889 {
17890 return cp_parser_type_id_1 (parser, false, false);
17891 }
17892
17893 static tree cp_parser_template_type_arg (cp_parser *parser)
17894 {
17895 tree r;
17896 const char *saved_message = parser->type_definition_forbidden_message;
17897 parser->type_definition_forbidden_message
17898 = G_("types may not be defined in template arguments");
17899 r = cp_parser_type_id_1 (parser, true, false);
17900 parser->type_definition_forbidden_message = saved_message;
17901 return r;
17902 }
17903
17904 static tree cp_parser_trailing_type_id (cp_parser *parser)
17905 {
17906 return cp_parser_type_id_1 (parser, false, true);
17907 }
17908
17909 /* Parse a type-specifier-seq.
17910
17911 type-specifier-seq:
17912 type-specifier type-specifier-seq [opt]
17913
17914 GNU extension:
17915
17916 type-specifier-seq:
17917 attributes type-specifier-seq [opt]
17918
17919 If IS_DECLARATION is true, we are at the start of a "condition" or
17920 exception-declaration, so we might be followed by a declarator-id.
17921
17922 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17923 i.e. we've just seen "->".
17924
17925 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17926
17927 static void
17928 cp_parser_type_specifier_seq (cp_parser* parser,
17929 bool is_declaration,
17930 bool is_trailing_return,
17931 cp_decl_specifier_seq *type_specifier_seq)
17932 {
17933 bool seen_type_specifier = false;
17934 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17935 cp_token *start_token = NULL;
17936
17937 /* Clear the TYPE_SPECIFIER_SEQ. */
17938 clear_decl_specs (type_specifier_seq);
17939
17940 /* In the context of a trailing return type, enum E { } is an
17941 elaborated-type-specifier followed by a function-body, not an
17942 enum-specifier. */
17943 if (is_trailing_return)
17944 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17945
17946 /* Parse the type-specifiers and attributes. */
17947 while (true)
17948 {
17949 tree type_specifier;
17950 bool is_cv_qualifier;
17951
17952 /* Check for attributes first. */
17953 if (cp_next_tokens_can_be_attribute_p (parser))
17954 {
17955 type_specifier_seq->attributes =
17956 chainon (type_specifier_seq->attributes,
17957 cp_parser_attributes_opt (parser));
17958 continue;
17959 }
17960
17961 /* record the token of the beginning of the type specifier seq,
17962 for error reporting purposes*/
17963 if (!start_token)
17964 start_token = cp_lexer_peek_token (parser->lexer);
17965
17966 /* Look for the type-specifier. */
17967 type_specifier = cp_parser_type_specifier (parser,
17968 flags,
17969 type_specifier_seq,
17970 /*is_declaration=*/false,
17971 NULL,
17972 &is_cv_qualifier);
17973 if (!type_specifier)
17974 {
17975 /* If the first type-specifier could not be found, this is not a
17976 type-specifier-seq at all. */
17977 if (!seen_type_specifier)
17978 {
17979 cp_parser_error (parser, "expected type-specifier");
17980 type_specifier_seq->type = error_mark_node;
17981 return;
17982 }
17983 /* If subsequent type-specifiers could not be found, the
17984 type-specifier-seq is complete. */
17985 break;
17986 }
17987
17988 seen_type_specifier = true;
17989 /* The standard says that a condition can be:
17990
17991 type-specifier-seq declarator = assignment-expression
17992
17993 However, given:
17994
17995 struct S {};
17996 if (int S = ...)
17997
17998 we should treat the "S" as a declarator, not as a
17999 type-specifier. The standard doesn't say that explicitly for
18000 type-specifier-seq, but it does say that for
18001 decl-specifier-seq in an ordinary declaration. Perhaps it
18002 would be clearer just to allow a decl-specifier-seq here, and
18003 then add a semantic restriction that if any decl-specifiers
18004 that are not type-specifiers appear, the program is invalid. */
18005 if (is_declaration && !is_cv_qualifier)
18006 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18007 }
18008 }
18009
18010 /* Parse a parameter-declaration-clause.
18011
18012 parameter-declaration-clause:
18013 parameter-declaration-list [opt] ... [opt]
18014 parameter-declaration-list , ...
18015
18016 Returns a representation for the parameter declarations. A return
18017 value of NULL indicates a parameter-declaration-clause consisting
18018 only of an ellipsis. */
18019
18020 static tree
18021 cp_parser_parameter_declaration_clause (cp_parser* parser)
18022 {
18023 tree parameters;
18024 cp_token *token;
18025 bool ellipsis_p;
18026 bool is_error;
18027
18028 struct cleanup {
18029 cp_parser* parser;
18030 int auto_is_implicit_function_template_parm_p;
18031 ~cleanup() {
18032 parser->auto_is_implicit_function_template_parm_p
18033 = auto_is_implicit_function_template_parm_p;
18034 }
18035 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18036
18037 (void) cleanup;
18038
18039 if (!processing_specialization)
18040 parser->auto_is_implicit_function_template_parm_p = true;
18041
18042 /* Peek at the next token. */
18043 token = cp_lexer_peek_token (parser->lexer);
18044 /* Check for trivial parameter-declaration-clauses. */
18045 if (token->type == CPP_ELLIPSIS)
18046 {
18047 /* Consume the `...' token. */
18048 cp_lexer_consume_token (parser->lexer);
18049 return NULL_TREE;
18050 }
18051 else if (token->type == CPP_CLOSE_PAREN)
18052 /* There are no parameters. */
18053 {
18054 #ifndef NO_IMPLICIT_EXTERN_C
18055 if (in_system_header && current_class_type == NULL
18056 && current_lang_name == lang_name_c)
18057 return NULL_TREE;
18058 else
18059 #endif
18060 return void_list_node;
18061 }
18062 /* Check for `(void)', too, which is a special case. */
18063 else if (token->keyword == RID_VOID
18064 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18065 == CPP_CLOSE_PAREN))
18066 {
18067 /* Consume the `void' token. */
18068 cp_lexer_consume_token (parser->lexer);
18069 /* There are no parameters. */
18070 return void_list_node;
18071 }
18072
18073 /* Parse the parameter-declaration-list. */
18074 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18075 /* If a parse error occurred while parsing the
18076 parameter-declaration-list, then the entire
18077 parameter-declaration-clause is erroneous. */
18078 if (is_error)
18079 return NULL;
18080
18081 /* Peek at the next token. */
18082 token = cp_lexer_peek_token (parser->lexer);
18083 /* If it's a `,', the clause should terminate with an ellipsis. */
18084 if (token->type == CPP_COMMA)
18085 {
18086 /* Consume the `,'. */
18087 cp_lexer_consume_token (parser->lexer);
18088 /* Expect an ellipsis. */
18089 ellipsis_p
18090 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18091 }
18092 /* It might also be `...' if the optional trailing `,' was
18093 omitted. */
18094 else if (token->type == CPP_ELLIPSIS)
18095 {
18096 /* Consume the `...' token. */
18097 cp_lexer_consume_token (parser->lexer);
18098 /* And remember that we saw it. */
18099 ellipsis_p = true;
18100 }
18101 else
18102 ellipsis_p = false;
18103
18104 /* Finish the parameter list. */
18105 if (!ellipsis_p)
18106 parameters = chainon (parameters, void_list_node);
18107
18108 return parameters;
18109 }
18110
18111 /* Parse a parameter-declaration-list.
18112
18113 parameter-declaration-list:
18114 parameter-declaration
18115 parameter-declaration-list , parameter-declaration
18116
18117 Returns a representation of the parameter-declaration-list, as for
18118 cp_parser_parameter_declaration_clause. However, the
18119 `void_list_node' is never appended to the list. Upon return,
18120 *IS_ERROR will be true iff an error occurred. */
18121
18122 static tree
18123 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18124 {
18125 tree parameters = NULL_TREE;
18126 tree *tail = &parameters;
18127 bool saved_in_unbraced_linkage_specification_p;
18128 int index = 0;
18129
18130 /* Assume all will go well. */
18131 *is_error = false;
18132 /* The special considerations that apply to a function within an
18133 unbraced linkage specifications do not apply to the parameters
18134 to the function. */
18135 saved_in_unbraced_linkage_specification_p
18136 = parser->in_unbraced_linkage_specification_p;
18137 parser->in_unbraced_linkage_specification_p = false;
18138
18139 /* Look for more parameters. */
18140 while (true)
18141 {
18142 cp_parameter_declarator *parameter;
18143 tree decl = error_mark_node;
18144 bool parenthesized_p = false;
18145 int template_parm_idx = (parser->num_template_parameter_lists?
18146 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18147 (current_template_parms)) : 0);
18148
18149 /* Parse the parameter. */
18150 parameter
18151 = cp_parser_parameter_declaration (parser,
18152 /*template_parm_p=*/false,
18153 &parenthesized_p);
18154
18155 /* We don't know yet if the enclosing context is deprecated, so wait
18156 and warn in grokparms if appropriate. */
18157 deprecated_state = DEPRECATED_SUPPRESS;
18158
18159 if (parameter)
18160 {
18161 /* If a function parameter pack was specified and an implicit template
18162 parameter was introduced during cp_parser_parameter_declaration,
18163 change any implicit parameters introduced into packs. */
18164 if (parser->implicit_template_parms
18165 && parameter->declarator
18166 && parameter->declarator->parameter_pack_p)
18167 {
18168 int latest_template_parm_idx = TREE_VEC_LENGTH
18169 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18170
18171 if (latest_template_parm_idx != template_parm_idx)
18172 parameter->decl_specifiers.type = convert_generic_types_to_packs
18173 (parameter->decl_specifiers.type,
18174 template_parm_idx, latest_template_parm_idx);
18175 }
18176
18177 decl = grokdeclarator (parameter->declarator,
18178 &parameter->decl_specifiers,
18179 PARM,
18180 parameter->default_argument != NULL_TREE,
18181 &parameter->decl_specifiers.attributes);
18182 }
18183
18184 deprecated_state = DEPRECATED_NORMAL;
18185
18186 /* If a parse error occurred parsing the parameter declaration,
18187 then the entire parameter-declaration-list is erroneous. */
18188 if (decl == error_mark_node)
18189 {
18190 *is_error = true;
18191 parameters = error_mark_node;
18192 break;
18193 }
18194
18195 if (parameter->decl_specifiers.attributes)
18196 cplus_decl_attributes (&decl,
18197 parameter->decl_specifiers.attributes,
18198 0);
18199 if (DECL_NAME (decl))
18200 decl = pushdecl (decl);
18201
18202 if (decl != error_mark_node)
18203 {
18204 retrofit_lang_decl (decl);
18205 DECL_PARM_INDEX (decl) = ++index;
18206 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18207 }
18208
18209 /* Add the new parameter to the list. */
18210 *tail = build_tree_list (parameter->default_argument, decl);
18211 tail = &TREE_CHAIN (*tail);
18212
18213 /* Peek at the next token. */
18214 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18215 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18216 /* These are for Objective-C++ */
18217 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18218 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18219 /* The parameter-declaration-list is complete. */
18220 break;
18221 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18222 {
18223 cp_token *token;
18224
18225 /* Peek at the next token. */
18226 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18227 /* If it's an ellipsis, then the list is complete. */
18228 if (token->type == CPP_ELLIPSIS)
18229 break;
18230 /* Otherwise, there must be more parameters. Consume the
18231 `,'. */
18232 cp_lexer_consume_token (parser->lexer);
18233 /* When parsing something like:
18234
18235 int i(float f, double d)
18236
18237 we can tell after seeing the declaration for "f" that we
18238 are not looking at an initialization of a variable "i",
18239 but rather at the declaration of a function "i".
18240
18241 Due to the fact that the parsing of template arguments
18242 (as specified to a template-id) requires backtracking we
18243 cannot use this technique when inside a template argument
18244 list. */
18245 if (!parser->in_template_argument_list_p
18246 && !parser->in_type_id_in_expr_p
18247 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18248 /* However, a parameter-declaration of the form
18249 "float(f)" (which is a valid declaration of a
18250 parameter "f") can also be interpreted as an
18251 expression (the conversion of "f" to "float"). */
18252 && !parenthesized_p)
18253 cp_parser_commit_to_tentative_parse (parser);
18254 }
18255 else
18256 {
18257 cp_parser_error (parser, "expected %<,%> or %<...%>");
18258 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18259 cp_parser_skip_to_closing_parenthesis (parser,
18260 /*recovering=*/true,
18261 /*or_comma=*/false,
18262 /*consume_paren=*/false);
18263 break;
18264 }
18265 }
18266
18267 parser->in_unbraced_linkage_specification_p
18268 = saved_in_unbraced_linkage_specification_p;
18269
18270 if (cp_binding_level *its = parser->implicit_template_scope)
18271 if (current_binding_level->level_chain == its)
18272 {
18273 parser->implicit_template_parms = 0;
18274 parser->implicit_template_scope = 0;
18275 }
18276
18277 return parameters;
18278 }
18279
18280 /* Parse a parameter declaration.
18281
18282 parameter-declaration:
18283 decl-specifier-seq ... [opt] declarator
18284 decl-specifier-seq declarator = assignment-expression
18285 decl-specifier-seq ... [opt] abstract-declarator [opt]
18286 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18287
18288 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18289 declares a template parameter. (In that case, a non-nested `>'
18290 token encountered during the parsing of the assignment-expression
18291 is not interpreted as a greater-than operator.)
18292
18293 Returns a representation of the parameter, or NULL if an error
18294 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18295 true iff the declarator is of the form "(p)". */
18296
18297 static cp_parameter_declarator *
18298 cp_parser_parameter_declaration (cp_parser *parser,
18299 bool template_parm_p,
18300 bool *parenthesized_p)
18301 {
18302 int declares_class_or_enum;
18303 cp_decl_specifier_seq decl_specifiers;
18304 cp_declarator *declarator;
18305 tree default_argument;
18306 cp_token *token = NULL, *declarator_token_start = NULL;
18307 const char *saved_message;
18308
18309 /* In a template parameter, `>' is not an operator.
18310
18311 [temp.param]
18312
18313 When parsing a default template-argument for a non-type
18314 template-parameter, the first non-nested `>' is taken as the end
18315 of the template parameter-list rather than a greater-than
18316 operator. */
18317
18318 /* Type definitions may not appear in parameter types. */
18319 saved_message = parser->type_definition_forbidden_message;
18320 parser->type_definition_forbidden_message
18321 = G_("types may not be defined in parameter types");
18322
18323 /* Parse the declaration-specifiers. */
18324 cp_parser_decl_specifier_seq (parser,
18325 CP_PARSER_FLAGS_NONE,
18326 &decl_specifiers,
18327 &declares_class_or_enum);
18328
18329 /* Complain about missing 'typename' or other invalid type names. */
18330 if (!decl_specifiers.any_type_specifiers_p
18331 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18332 decl_specifiers.type = error_mark_node;
18333
18334 /* If an error occurred, there's no reason to attempt to parse the
18335 rest of the declaration. */
18336 if (cp_parser_error_occurred (parser))
18337 {
18338 parser->type_definition_forbidden_message = saved_message;
18339 return NULL;
18340 }
18341
18342 /* Peek at the next token. */
18343 token = cp_lexer_peek_token (parser->lexer);
18344
18345 /* If the next token is a `)', `,', `=', `>', or `...', then there
18346 is no declarator. However, when variadic templates are enabled,
18347 there may be a declarator following `...'. */
18348 if (token->type == CPP_CLOSE_PAREN
18349 || token->type == CPP_COMMA
18350 || token->type == CPP_EQ
18351 || token->type == CPP_GREATER)
18352 {
18353 declarator = NULL;
18354 if (parenthesized_p)
18355 *parenthesized_p = false;
18356 }
18357 /* Otherwise, there should be a declarator. */
18358 else
18359 {
18360 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18361 parser->default_arg_ok_p = false;
18362
18363 /* After seeing a decl-specifier-seq, if the next token is not a
18364 "(", there is no possibility that the code is a valid
18365 expression. Therefore, if parsing tentatively, we commit at
18366 this point. */
18367 if (!parser->in_template_argument_list_p
18368 /* In an expression context, having seen:
18369
18370 (int((char ...
18371
18372 we cannot be sure whether we are looking at a
18373 function-type (taking a "char" as a parameter) or a cast
18374 of some object of type "char" to "int". */
18375 && !parser->in_type_id_in_expr_p
18376 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18377 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18378 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18379 cp_parser_commit_to_tentative_parse (parser);
18380 /* Parse the declarator. */
18381 declarator_token_start = token;
18382 declarator = cp_parser_declarator (parser,
18383 CP_PARSER_DECLARATOR_EITHER,
18384 /*ctor_dtor_or_conv_p=*/NULL,
18385 parenthesized_p,
18386 /*member_p=*/false);
18387 parser->default_arg_ok_p = saved_default_arg_ok_p;
18388 /* After the declarator, allow more attributes. */
18389 decl_specifiers.attributes
18390 = chainon (decl_specifiers.attributes,
18391 cp_parser_attributes_opt (parser));
18392 }
18393
18394 /* If the next token is an ellipsis, and we have not seen a
18395 declarator name, and the type of the declarator contains parameter
18396 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18397 a parameter pack expansion expression. Otherwise, leave the
18398 ellipsis for a C-style variadic function. */
18399 token = cp_lexer_peek_token (parser->lexer);
18400 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18401 {
18402 tree type = decl_specifiers.type;
18403
18404 if (type && DECL_P (type))
18405 type = TREE_TYPE (type);
18406
18407 if (type
18408 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18409 && declarator_can_be_parameter_pack (declarator)
18410 && (!declarator || !declarator->parameter_pack_p)
18411 && uses_parameter_packs (type))
18412 {
18413 /* Consume the `...'. */
18414 cp_lexer_consume_token (parser->lexer);
18415 maybe_warn_variadic_templates ();
18416
18417 /* Build a pack expansion type */
18418 if (declarator)
18419 declarator->parameter_pack_p = true;
18420 else
18421 decl_specifiers.type = make_pack_expansion (type);
18422 }
18423 }
18424
18425 /* The restriction on defining new types applies only to the type
18426 of the parameter, not to the default argument. */
18427 parser->type_definition_forbidden_message = saved_message;
18428
18429 /* If the next token is `=', then process a default argument. */
18430 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18431 {
18432 token = cp_lexer_peek_token (parser->lexer);
18433 /* If we are defining a class, then the tokens that make up the
18434 default argument must be saved and processed later. */
18435 if (!template_parm_p && at_class_scope_p ()
18436 && TYPE_BEING_DEFINED (current_class_type)
18437 && !LAMBDA_TYPE_P (current_class_type))
18438 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18439 /* Outside of a class definition, we can just parse the
18440 assignment-expression. */
18441 else
18442 default_argument
18443 = cp_parser_default_argument (parser, template_parm_p);
18444
18445 if (!parser->default_arg_ok_p)
18446 {
18447 if (flag_permissive)
18448 warning (0, "deprecated use of default argument for parameter of non-function");
18449 else
18450 {
18451 error_at (token->location,
18452 "default arguments are only "
18453 "permitted for function parameters");
18454 default_argument = NULL_TREE;
18455 }
18456 }
18457 else if ((declarator && declarator->parameter_pack_p)
18458 || (decl_specifiers.type
18459 && PACK_EXPANSION_P (decl_specifiers.type)))
18460 {
18461 /* Find the name of the parameter pack. */
18462 cp_declarator *id_declarator = declarator;
18463 while (id_declarator && id_declarator->kind != cdk_id)
18464 id_declarator = id_declarator->declarator;
18465
18466 if (id_declarator && id_declarator->kind == cdk_id)
18467 error_at (declarator_token_start->location,
18468 template_parm_p
18469 ? G_("template parameter pack %qD "
18470 "cannot have a default argument")
18471 : G_("parameter pack %qD cannot have "
18472 "a default argument"),
18473 id_declarator->u.id.unqualified_name);
18474 else
18475 error_at (declarator_token_start->location,
18476 template_parm_p
18477 ? G_("template parameter pack cannot have "
18478 "a default argument")
18479 : G_("parameter pack cannot have a "
18480 "default argument"));
18481
18482 default_argument = NULL_TREE;
18483 }
18484 }
18485 else
18486 default_argument = NULL_TREE;
18487
18488 return make_parameter_declarator (&decl_specifiers,
18489 declarator,
18490 default_argument);
18491 }
18492
18493 /* Parse a default argument and return it.
18494
18495 TEMPLATE_PARM_P is true if this is a default argument for a
18496 non-type template parameter. */
18497 static tree
18498 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18499 {
18500 tree default_argument = NULL_TREE;
18501 bool saved_greater_than_is_operator_p;
18502 bool saved_local_variables_forbidden_p;
18503 bool non_constant_p, is_direct_init;
18504
18505 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18506 set correctly. */
18507 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18508 parser->greater_than_is_operator_p = !template_parm_p;
18509 /* Local variable names (and the `this' keyword) may not
18510 appear in a default argument. */
18511 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18512 parser->local_variables_forbidden_p = true;
18513 /* Parse the assignment-expression. */
18514 if (template_parm_p)
18515 push_deferring_access_checks (dk_no_deferred);
18516 default_argument
18517 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18518 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18519 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18520 if (template_parm_p)
18521 pop_deferring_access_checks ();
18522 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18523 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18524
18525 return default_argument;
18526 }
18527
18528 /* Parse a function-body.
18529
18530 function-body:
18531 compound_statement */
18532
18533 static void
18534 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18535 {
18536 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18537 }
18538
18539 /* Parse a ctor-initializer-opt followed by a function-body. Return
18540 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18541 is true we are parsing a function-try-block. */
18542
18543 static bool
18544 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18545 bool in_function_try_block)
18546 {
18547 tree body, list;
18548 bool ctor_initializer_p;
18549 const bool check_body_p =
18550 DECL_CONSTRUCTOR_P (current_function_decl)
18551 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18552 tree last = NULL;
18553
18554 /* Begin the function body. */
18555 body = begin_function_body ();
18556 /* Parse the optional ctor-initializer. */
18557 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18558
18559 /* If we're parsing a constexpr constructor definition, we need
18560 to check that the constructor body is indeed empty. However,
18561 before we get to cp_parser_function_body lot of junk has been
18562 generated, so we can't just check that we have an empty block.
18563 Rather we take a snapshot of the outermost block, and check whether
18564 cp_parser_function_body changed its state. */
18565 if (check_body_p)
18566 {
18567 list = cur_stmt_list;
18568 if (STATEMENT_LIST_TAIL (list))
18569 last = STATEMENT_LIST_TAIL (list)->stmt;
18570 }
18571 /* Parse the function-body. */
18572 cp_parser_function_body (parser, in_function_try_block);
18573 if (check_body_p)
18574 check_constexpr_ctor_body (last, list);
18575 /* Finish the function body. */
18576 finish_function_body (body);
18577
18578 return ctor_initializer_p;
18579 }
18580
18581 /* Parse an initializer.
18582
18583 initializer:
18584 = initializer-clause
18585 ( expression-list )
18586
18587 Returns an expression representing the initializer. If no
18588 initializer is present, NULL_TREE is returned.
18589
18590 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18591 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18592 set to TRUE if there is no initializer present. If there is an
18593 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18594 is set to true; otherwise it is set to false. */
18595
18596 static tree
18597 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18598 bool* non_constant_p)
18599 {
18600 cp_token *token;
18601 tree init;
18602
18603 /* Peek at the next token. */
18604 token = cp_lexer_peek_token (parser->lexer);
18605
18606 /* Let our caller know whether or not this initializer was
18607 parenthesized. */
18608 *is_direct_init = (token->type != CPP_EQ);
18609 /* Assume that the initializer is constant. */
18610 *non_constant_p = false;
18611
18612 if (token->type == CPP_EQ)
18613 {
18614 /* Consume the `='. */
18615 cp_lexer_consume_token (parser->lexer);
18616 /* Parse the initializer-clause. */
18617 init = cp_parser_initializer_clause (parser, non_constant_p);
18618 }
18619 else if (token->type == CPP_OPEN_PAREN)
18620 {
18621 vec<tree, va_gc> *vec;
18622 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18623 /*cast_p=*/false,
18624 /*allow_expansion_p=*/true,
18625 non_constant_p);
18626 if (vec == NULL)
18627 return error_mark_node;
18628 init = build_tree_list_vec (vec);
18629 release_tree_vector (vec);
18630 }
18631 else if (token->type == CPP_OPEN_BRACE)
18632 {
18633 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18634 init = cp_parser_braced_list (parser, non_constant_p);
18635 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18636 }
18637 else
18638 {
18639 /* Anything else is an error. */
18640 cp_parser_error (parser, "expected initializer");
18641 init = error_mark_node;
18642 }
18643
18644 return init;
18645 }
18646
18647 /* Parse an initializer-clause.
18648
18649 initializer-clause:
18650 assignment-expression
18651 braced-init-list
18652
18653 Returns an expression representing the initializer.
18654
18655 If the `assignment-expression' production is used the value
18656 returned is simply a representation for the expression.
18657
18658 Otherwise, calls cp_parser_braced_list. */
18659
18660 static tree
18661 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18662 {
18663 tree initializer;
18664
18665 /* Assume the expression is constant. */
18666 *non_constant_p = false;
18667
18668 /* If it is not a `{', then we are looking at an
18669 assignment-expression. */
18670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18671 {
18672 initializer
18673 = cp_parser_constant_expression (parser,
18674 /*allow_non_constant_p=*/true,
18675 non_constant_p);
18676 }
18677 else
18678 initializer = cp_parser_braced_list (parser, non_constant_p);
18679
18680 return initializer;
18681 }
18682
18683 /* Parse a brace-enclosed initializer list.
18684
18685 braced-init-list:
18686 { initializer-list , [opt] }
18687 { }
18688
18689 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18690 the elements of the initializer-list (or NULL, if the last
18691 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18692 NULL_TREE. There is no way to detect whether or not the optional
18693 trailing `,' was provided. NON_CONSTANT_P is as for
18694 cp_parser_initializer. */
18695
18696 static tree
18697 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18698 {
18699 tree initializer;
18700
18701 /* Consume the `{' token. */
18702 cp_lexer_consume_token (parser->lexer);
18703 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18704 initializer = make_node (CONSTRUCTOR);
18705 /* If it's not a `}', then there is a non-trivial initializer. */
18706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18707 {
18708 /* Parse the initializer list. */
18709 CONSTRUCTOR_ELTS (initializer)
18710 = cp_parser_initializer_list (parser, non_constant_p);
18711 /* A trailing `,' token is allowed. */
18712 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18713 cp_lexer_consume_token (parser->lexer);
18714 }
18715 else
18716 *non_constant_p = false;
18717 /* Now, there should be a trailing `}'. */
18718 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18719 TREE_TYPE (initializer) = init_list_type_node;
18720 return initializer;
18721 }
18722
18723 /* Parse an initializer-list.
18724
18725 initializer-list:
18726 initializer-clause ... [opt]
18727 initializer-list , initializer-clause ... [opt]
18728
18729 GNU Extension:
18730
18731 initializer-list:
18732 designation initializer-clause ...[opt]
18733 initializer-list , designation initializer-clause ...[opt]
18734
18735 designation:
18736 . identifier =
18737 identifier :
18738 [ constant-expression ] =
18739
18740 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18741 for the initializer. If the INDEX of the elt is non-NULL, it is the
18742 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18743 as for cp_parser_initializer. */
18744
18745 static vec<constructor_elt, va_gc> *
18746 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18747 {
18748 vec<constructor_elt, va_gc> *v = NULL;
18749
18750 /* Assume all of the expressions are constant. */
18751 *non_constant_p = false;
18752
18753 /* Parse the rest of the list. */
18754 while (true)
18755 {
18756 cp_token *token;
18757 tree designator;
18758 tree initializer;
18759 bool clause_non_constant_p;
18760
18761 /* If the next token is an identifier and the following one is a
18762 colon, we are looking at the GNU designated-initializer
18763 syntax. */
18764 if (cp_parser_allow_gnu_extensions_p (parser)
18765 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18766 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18767 {
18768 /* Warn the user that they are using an extension. */
18769 pedwarn (input_location, OPT_Wpedantic,
18770 "ISO C++ does not allow designated initializers");
18771 /* Consume the identifier. */
18772 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18773 /* Consume the `:'. */
18774 cp_lexer_consume_token (parser->lexer);
18775 }
18776 /* Also handle the C99 syntax, '. id ='. */
18777 else if (cp_parser_allow_gnu_extensions_p (parser)
18778 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18779 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18780 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18781 {
18782 /* Warn the user that they are using an extension. */
18783 pedwarn (input_location, OPT_Wpedantic,
18784 "ISO C++ does not allow C99 designated initializers");
18785 /* Consume the `.'. */
18786 cp_lexer_consume_token (parser->lexer);
18787 /* Consume the identifier. */
18788 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18789 /* Consume the `='. */
18790 cp_lexer_consume_token (parser->lexer);
18791 }
18792 /* Also handle C99 array designators, '[ const ] ='. */
18793 else if (cp_parser_allow_gnu_extensions_p (parser)
18794 && !c_dialect_objc ()
18795 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18796 {
18797 /* In C++11, [ could start a lambda-introducer. */
18798 bool non_const = false;
18799
18800 cp_parser_parse_tentatively (parser);
18801 cp_lexer_consume_token (parser->lexer);
18802 designator = cp_parser_constant_expression (parser, true, &non_const);
18803 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18804 cp_parser_require (parser, CPP_EQ, RT_EQ);
18805 if (!cp_parser_parse_definitely (parser))
18806 designator = NULL_TREE;
18807 else if (non_const)
18808 require_potential_rvalue_constant_expression (designator);
18809 }
18810 else
18811 designator = NULL_TREE;
18812
18813 /* Parse the initializer. */
18814 initializer = cp_parser_initializer_clause (parser,
18815 &clause_non_constant_p);
18816 /* If any clause is non-constant, so is the entire initializer. */
18817 if (clause_non_constant_p)
18818 *non_constant_p = true;
18819
18820 /* If we have an ellipsis, this is an initializer pack
18821 expansion. */
18822 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18823 {
18824 /* Consume the `...'. */
18825 cp_lexer_consume_token (parser->lexer);
18826
18827 /* Turn the initializer into an initializer expansion. */
18828 initializer = make_pack_expansion (initializer);
18829 }
18830
18831 /* Add it to the vector. */
18832 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18833
18834 /* If the next token is not a comma, we have reached the end of
18835 the list. */
18836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18837 break;
18838
18839 /* Peek at the next token. */
18840 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18841 /* If the next token is a `}', then we're still done. An
18842 initializer-clause can have a trailing `,' after the
18843 initializer-list and before the closing `}'. */
18844 if (token->type == CPP_CLOSE_BRACE)
18845 break;
18846
18847 /* Consume the `,' token. */
18848 cp_lexer_consume_token (parser->lexer);
18849 }
18850
18851 return v;
18852 }
18853
18854 /* Classes [gram.class] */
18855
18856 /* Parse a class-name.
18857
18858 class-name:
18859 identifier
18860 template-id
18861
18862 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18863 to indicate that names looked up in dependent types should be
18864 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18865 keyword has been used to indicate that the name that appears next
18866 is a template. TAG_TYPE indicates the explicit tag given before
18867 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18868 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18869 is the class being defined in a class-head.
18870
18871 Returns the TYPE_DECL representing the class. */
18872
18873 static tree
18874 cp_parser_class_name (cp_parser *parser,
18875 bool typename_keyword_p,
18876 bool template_keyword_p,
18877 enum tag_types tag_type,
18878 bool check_dependency_p,
18879 bool class_head_p,
18880 bool is_declaration)
18881 {
18882 tree decl;
18883 tree scope;
18884 bool typename_p;
18885 cp_token *token;
18886 tree identifier = NULL_TREE;
18887
18888 /* All class-names start with an identifier. */
18889 token = cp_lexer_peek_token (parser->lexer);
18890 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18891 {
18892 cp_parser_error (parser, "expected class-name");
18893 return error_mark_node;
18894 }
18895
18896 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18897 to a template-id, so we save it here. */
18898 scope = parser->scope;
18899 if (scope == error_mark_node)
18900 return error_mark_node;
18901
18902 /* Any name names a type if we're following the `typename' keyword
18903 in a qualified name where the enclosing scope is type-dependent. */
18904 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18905 && dependent_type_p (scope));
18906 /* Handle the common case (an identifier, but not a template-id)
18907 efficiently. */
18908 if (token->type == CPP_NAME
18909 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18910 {
18911 cp_token *identifier_token;
18912 bool ambiguous_p;
18913
18914 /* Look for the identifier. */
18915 identifier_token = cp_lexer_peek_token (parser->lexer);
18916 ambiguous_p = identifier_token->ambiguous_p;
18917 identifier = cp_parser_identifier (parser);
18918 /* If the next token isn't an identifier, we are certainly not
18919 looking at a class-name. */
18920 if (identifier == error_mark_node)
18921 decl = error_mark_node;
18922 /* If we know this is a type-name, there's no need to look it
18923 up. */
18924 else if (typename_p)
18925 decl = identifier;
18926 else
18927 {
18928 tree ambiguous_decls;
18929 /* If we already know that this lookup is ambiguous, then
18930 we've already issued an error message; there's no reason
18931 to check again. */
18932 if (ambiguous_p)
18933 {
18934 cp_parser_simulate_error (parser);
18935 return error_mark_node;
18936 }
18937 /* If the next token is a `::', then the name must be a type
18938 name.
18939
18940 [basic.lookup.qual]
18941
18942 During the lookup for a name preceding the :: scope
18943 resolution operator, object, function, and enumerator
18944 names are ignored. */
18945 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18946 tag_type = typename_type;
18947 /* Look up the name. */
18948 decl = cp_parser_lookup_name (parser, identifier,
18949 tag_type,
18950 /*is_template=*/false,
18951 /*is_namespace=*/false,
18952 check_dependency_p,
18953 &ambiguous_decls,
18954 identifier_token->location);
18955 if (ambiguous_decls)
18956 {
18957 if (cp_parser_parsing_tentatively (parser))
18958 cp_parser_simulate_error (parser);
18959 return error_mark_node;
18960 }
18961 }
18962 }
18963 else
18964 {
18965 /* Try a template-id. */
18966 decl = cp_parser_template_id (parser, template_keyword_p,
18967 check_dependency_p,
18968 tag_type,
18969 is_declaration);
18970 if (decl == error_mark_node)
18971 return error_mark_node;
18972 }
18973
18974 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18975
18976 /* If this is a typename, create a TYPENAME_TYPE. */
18977 if (typename_p && decl != error_mark_node)
18978 {
18979 decl = make_typename_type (scope, decl, typename_type,
18980 /*complain=*/tf_error);
18981 if (decl != error_mark_node)
18982 decl = TYPE_NAME (decl);
18983 }
18984
18985 decl = strip_using_decl (decl);
18986
18987 /* Check to see that it is really the name of a class. */
18988 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18989 && identifier_p (TREE_OPERAND (decl, 0))
18990 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18991 /* Situations like this:
18992
18993 template <typename T> struct A {
18994 typename T::template X<int>::I i;
18995 };
18996
18997 are problematic. Is `T::template X<int>' a class-name? The
18998 standard does not seem to be definitive, but there is no other
18999 valid interpretation of the following `::'. Therefore, those
19000 names are considered class-names. */
19001 {
19002 decl = make_typename_type (scope, decl, tag_type, tf_error);
19003 if (decl != error_mark_node)
19004 decl = TYPE_NAME (decl);
19005 }
19006 else if (TREE_CODE (decl) != TYPE_DECL
19007 || TREE_TYPE (decl) == error_mark_node
19008 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19009 /* In Objective-C 2.0, a classname followed by '.' starts a
19010 dot-syntax expression, and it's not a type-name. */
19011 || (c_dialect_objc ()
19012 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19013 && objc_is_class_name (decl)))
19014 decl = error_mark_node;
19015
19016 if (decl == error_mark_node)
19017 cp_parser_error (parser, "expected class-name");
19018 else if (identifier && !parser->scope)
19019 maybe_note_name_used_in_class (identifier, decl);
19020
19021 return decl;
19022 }
19023
19024 /* Parse a class-specifier.
19025
19026 class-specifier:
19027 class-head { member-specification [opt] }
19028
19029 Returns the TREE_TYPE representing the class. */
19030
19031 static tree
19032 cp_parser_class_specifier_1 (cp_parser* parser)
19033 {
19034 tree type;
19035 tree attributes = NULL_TREE;
19036 bool nested_name_specifier_p;
19037 unsigned saved_num_template_parameter_lists;
19038 bool saved_in_function_body;
19039 unsigned char in_statement;
19040 bool in_switch_statement_p;
19041 bool saved_in_unbraced_linkage_specification_p;
19042 tree old_scope = NULL_TREE;
19043 tree scope = NULL_TREE;
19044 cp_token *closing_brace;
19045
19046 push_deferring_access_checks (dk_no_deferred);
19047
19048 /* Parse the class-head. */
19049 type = cp_parser_class_head (parser,
19050 &nested_name_specifier_p);
19051 /* If the class-head was a semantic disaster, skip the entire body
19052 of the class. */
19053 if (!type)
19054 {
19055 cp_parser_skip_to_end_of_block_or_statement (parser);
19056 pop_deferring_access_checks ();
19057 return error_mark_node;
19058 }
19059
19060 /* Look for the `{'. */
19061 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19062 {
19063 pop_deferring_access_checks ();
19064 return error_mark_node;
19065 }
19066
19067 cp_ensure_no_omp_declare_simd (parser);
19068
19069 /* Issue an error message if type-definitions are forbidden here. */
19070 cp_parser_check_type_definition (parser);
19071 /* Remember that we are defining one more class. */
19072 ++parser->num_classes_being_defined;
19073 /* Inside the class, surrounding template-parameter-lists do not
19074 apply. */
19075 saved_num_template_parameter_lists
19076 = parser->num_template_parameter_lists;
19077 parser->num_template_parameter_lists = 0;
19078 /* We are not in a function body. */
19079 saved_in_function_body = parser->in_function_body;
19080 parser->in_function_body = false;
19081 /* Or in a loop. */
19082 in_statement = parser->in_statement;
19083 parser->in_statement = 0;
19084 /* Or in a switch. */
19085 in_switch_statement_p = parser->in_switch_statement_p;
19086 parser->in_switch_statement_p = false;
19087 /* We are not immediately inside an extern "lang" block. */
19088 saved_in_unbraced_linkage_specification_p
19089 = parser->in_unbraced_linkage_specification_p;
19090 parser->in_unbraced_linkage_specification_p = false;
19091
19092 /* Start the class. */
19093 if (nested_name_specifier_p)
19094 {
19095 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19096 old_scope = push_inner_scope (scope);
19097 }
19098 type = begin_class_definition (type);
19099
19100 if (type == error_mark_node)
19101 /* If the type is erroneous, skip the entire body of the class. */
19102 cp_parser_skip_to_closing_brace (parser);
19103 else
19104 /* Parse the member-specification. */
19105 cp_parser_member_specification_opt (parser);
19106
19107 /* Look for the trailing `}'. */
19108 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19109 /* Look for trailing attributes to apply to this class. */
19110 if (cp_parser_allow_gnu_extensions_p (parser))
19111 attributes = cp_parser_gnu_attributes_opt (parser);
19112 if (type != error_mark_node)
19113 type = finish_struct (type, attributes);
19114 if (nested_name_specifier_p)
19115 pop_inner_scope (old_scope, scope);
19116
19117 /* We've finished a type definition. Check for the common syntax
19118 error of forgetting a semicolon after the definition. We need to
19119 be careful, as we can't just check for not-a-semicolon and be done
19120 with it; the user might have typed:
19121
19122 class X { } c = ...;
19123 class X { } *p = ...;
19124
19125 and so forth. Instead, enumerate all the possible tokens that
19126 might follow this production; if we don't see one of them, then
19127 complain and silently insert the semicolon. */
19128 {
19129 cp_token *token = cp_lexer_peek_token (parser->lexer);
19130 bool want_semicolon = true;
19131
19132 if (cp_next_tokens_can_be_std_attribute_p (parser))
19133 /* Don't try to parse c++11 attributes here. As per the
19134 grammar, that should be a task for
19135 cp_parser_decl_specifier_seq. */
19136 want_semicolon = false;
19137
19138 switch (token->type)
19139 {
19140 case CPP_NAME:
19141 case CPP_SEMICOLON:
19142 case CPP_MULT:
19143 case CPP_AND:
19144 case CPP_OPEN_PAREN:
19145 case CPP_CLOSE_PAREN:
19146 case CPP_COMMA:
19147 want_semicolon = false;
19148 break;
19149
19150 /* While it's legal for type qualifiers and storage class
19151 specifiers to follow type definitions in the grammar, only
19152 compiler testsuites contain code like that. Assume that if
19153 we see such code, then what we're really seeing is a case
19154 like:
19155
19156 class X { }
19157 const <type> var = ...;
19158
19159 or
19160
19161 class Y { }
19162 static <type> func (...) ...
19163
19164 i.e. the qualifier or specifier applies to the next
19165 declaration. To do so, however, we need to look ahead one
19166 more token to see if *that* token is a type specifier.
19167
19168 This code could be improved to handle:
19169
19170 class Z { }
19171 static const <type> var = ...; */
19172 case CPP_KEYWORD:
19173 if (keyword_is_decl_specifier (token->keyword))
19174 {
19175 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19176
19177 /* Handling user-defined types here would be nice, but very
19178 tricky. */
19179 want_semicolon
19180 = (lookahead->type == CPP_KEYWORD
19181 && keyword_begins_type_specifier (lookahead->keyword));
19182 }
19183 break;
19184 default:
19185 break;
19186 }
19187
19188 /* If we don't have a type, then something is very wrong and we
19189 shouldn't try to do anything clever. Likewise for not seeing the
19190 closing brace. */
19191 if (closing_brace && TYPE_P (type) && want_semicolon)
19192 {
19193 cp_token_position prev
19194 = cp_lexer_previous_token_position (parser->lexer);
19195 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19196 location_t loc = prev_token->location;
19197
19198 if (CLASSTYPE_DECLARED_CLASS (type))
19199 error_at (loc, "expected %<;%> after class definition");
19200 else if (TREE_CODE (type) == RECORD_TYPE)
19201 error_at (loc, "expected %<;%> after struct definition");
19202 else if (TREE_CODE (type) == UNION_TYPE)
19203 error_at (loc, "expected %<;%> after union definition");
19204 else
19205 gcc_unreachable ();
19206
19207 /* Unget one token and smash it to look as though we encountered
19208 a semicolon in the input stream. */
19209 cp_lexer_set_token_position (parser->lexer, prev);
19210 token = cp_lexer_peek_token (parser->lexer);
19211 token->type = CPP_SEMICOLON;
19212 token->keyword = RID_MAX;
19213 }
19214 }
19215
19216 /* If this class is not itself within the scope of another class,
19217 then we need to parse the bodies of all of the queued function
19218 definitions. Note that the queued functions defined in a class
19219 are not always processed immediately following the
19220 class-specifier for that class. Consider:
19221
19222 struct A {
19223 struct B { void f() { sizeof (A); } };
19224 };
19225
19226 If `f' were processed before the processing of `A' were
19227 completed, there would be no way to compute the size of `A'.
19228 Note that the nesting we are interested in here is lexical --
19229 not the semantic nesting given by TYPE_CONTEXT. In particular,
19230 for:
19231
19232 struct A { struct B; };
19233 struct A::B { void f() { } };
19234
19235 there is no need to delay the parsing of `A::B::f'. */
19236 if (--parser->num_classes_being_defined == 0)
19237 {
19238 tree decl;
19239 tree class_type = NULL_TREE;
19240 tree pushed_scope = NULL_TREE;
19241 unsigned ix;
19242 cp_default_arg_entry *e;
19243 tree save_ccp, save_ccr;
19244
19245 /* In a first pass, parse default arguments to the functions.
19246 Then, in a second pass, parse the bodies of the functions.
19247 This two-phased approach handles cases like:
19248
19249 struct S {
19250 void f() { g(); }
19251 void g(int i = 3);
19252 };
19253
19254 */
19255 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19256 {
19257 decl = e->decl;
19258 /* If there are default arguments that have not yet been processed,
19259 take care of them now. */
19260 if (class_type != e->class_type)
19261 {
19262 if (pushed_scope)
19263 pop_scope (pushed_scope);
19264 class_type = e->class_type;
19265 pushed_scope = push_scope (class_type);
19266 }
19267 /* Make sure that any template parameters are in scope. */
19268 maybe_begin_member_template_processing (decl);
19269 /* Parse the default argument expressions. */
19270 cp_parser_late_parsing_default_args (parser, decl);
19271 /* Remove any template parameters from the symbol table. */
19272 maybe_end_member_template_processing ();
19273 }
19274 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19275 /* Now parse any NSDMIs. */
19276 save_ccp = current_class_ptr;
19277 save_ccr = current_class_ref;
19278 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19279 {
19280 if (class_type != DECL_CONTEXT (decl))
19281 {
19282 if (pushed_scope)
19283 pop_scope (pushed_scope);
19284 class_type = DECL_CONTEXT (decl);
19285 pushed_scope = push_scope (class_type);
19286 }
19287 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19288 cp_parser_late_parsing_nsdmi (parser, decl);
19289 }
19290 vec_safe_truncate (unparsed_nsdmis, 0);
19291 current_class_ptr = save_ccp;
19292 current_class_ref = save_ccr;
19293 if (pushed_scope)
19294 pop_scope (pushed_scope);
19295 /* Now parse the body of the functions. */
19296 if (flag_openmp)
19297 {
19298 /* OpenMP UDRs need to be parsed before all other functions. */
19299 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19300 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19301 cp_parser_late_parsing_for_member (parser, decl);
19302 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19303 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19304 cp_parser_late_parsing_for_member (parser, decl);
19305 }
19306 else
19307 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19308 cp_parser_late_parsing_for_member (parser, decl);
19309 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19310 }
19311
19312 /* Put back any saved access checks. */
19313 pop_deferring_access_checks ();
19314
19315 /* Restore saved state. */
19316 parser->in_switch_statement_p = in_switch_statement_p;
19317 parser->in_statement = in_statement;
19318 parser->in_function_body = saved_in_function_body;
19319 parser->num_template_parameter_lists
19320 = saved_num_template_parameter_lists;
19321 parser->in_unbraced_linkage_specification_p
19322 = saved_in_unbraced_linkage_specification_p;
19323
19324 return type;
19325 }
19326
19327 static tree
19328 cp_parser_class_specifier (cp_parser* parser)
19329 {
19330 tree ret;
19331 timevar_push (TV_PARSE_STRUCT);
19332 ret = cp_parser_class_specifier_1 (parser);
19333 timevar_pop (TV_PARSE_STRUCT);
19334 return ret;
19335 }
19336
19337 /* Parse a class-head.
19338
19339 class-head:
19340 class-key identifier [opt] base-clause [opt]
19341 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19342 class-key nested-name-specifier [opt] template-id
19343 base-clause [opt]
19344
19345 class-virt-specifier:
19346 final
19347
19348 GNU Extensions:
19349 class-key attributes identifier [opt] base-clause [opt]
19350 class-key attributes nested-name-specifier identifier base-clause [opt]
19351 class-key attributes nested-name-specifier [opt] template-id
19352 base-clause [opt]
19353
19354 Upon return BASES is initialized to the list of base classes (or
19355 NULL, if there are none) in the same form returned by
19356 cp_parser_base_clause.
19357
19358 Returns the TYPE of the indicated class. Sets
19359 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19360 involving a nested-name-specifier was used, and FALSE otherwise.
19361
19362 Returns error_mark_node if this is not a class-head.
19363
19364 Returns NULL_TREE if the class-head is syntactically valid, but
19365 semantically invalid in a way that means we should skip the entire
19366 body of the class. */
19367
19368 static tree
19369 cp_parser_class_head (cp_parser* parser,
19370 bool* nested_name_specifier_p)
19371 {
19372 tree nested_name_specifier;
19373 enum tag_types class_key;
19374 tree id = NULL_TREE;
19375 tree type = NULL_TREE;
19376 tree attributes;
19377 tree bases;
19378 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19379 bool template_id_p = false;
19380 bool qualified_p = false;
19381 bool invalid_nested_name_p = false;
19382 bool invalid_explicit_specialization_p = false;
19383 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19384 tree pushed_scope = NULL_TREE;
19385 unsigned num_templates;
19386 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19387 /* Assume no nested-name-specifier will be present. */
19388 *nested_name_specifier_p = false;
19389 /* Assume no template parameter lists will be used in defining the
19390 type. */
19391 num_templates = 0;
19392 parser->colon_corrects_to_scope_p = false;
19393
19394 /* Look for the class-key. */
19395 class_key = cp_parser_class_key (parser);
19396 if (class_key == none_type)
19397 return error_mark_node;
19398
19399 /* Parse the attributes. */
19400 attributes = cp_parser_attributes_opt (parser);
19401
19402 /* If the next token is `::', that is invalid -- but sometimes
19403 people do try to write:
19404
19405 struct ::S {};
19406
19407 Handle this gracefully by accepting the extra qualifier, and then
19408 issuing an error about it later if this really is a
19409 class-head. If it turns out just to be an elaborated type
19410 specifier, remain silent. */
19411 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19412 qualified_p = true;
19413
19414 push_deferring_access_checks (dk_no_check);
19415
19416 /* Determine the name of the class. Begin by looking for an
19417 optional nested-name-specifier. */
19418 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19419 nested_name_specifier
19420 = cp_parser_nested_name_specifier_opt (parser,
19421 /*typename_keyword_p=*/false,
19422 /*check_dependency_p=*/false,
19423 /*type_p=*/true,
19424 /*is_declaration=*/false);
19425 /* If there was a nested-name-specifier, then there *must* be an
19426 identifier. */
19427 if (nested_name_specifier)
19428 {
19429 type_start_token = cp_lexer_peek_token (parser->lexer);
19430 /* Although the grammar says `identifier', it really means
19431 `class-name' or `template-name'. You are only allowed to
19432 define a class that has already been declared with this
19433 syntax.
19434
19435 The proposed resolution for Core Issue 180 says that wherever
19436 you see `class T::X' you should treat `X' as a type-name.
19437
19438 It is OK to define an inaccessible class; for example:
19439
19440 class A { class B; };
19441 class A::B {};
19442
19443 We do not know if we will see a class-name, or a
19444 template-name. We look for a class-name first, in case the
19445 class-name is a template-id; if we looked for the
19446 template-name first we would stop after the template-name. */
19447 cp_parser_parse_tentatively (parser);
19448 type = cp_parser_class_name (parser,
19449 /*typename_keyword_p=*/false,
19450 /*template_keyword_p=*/false,
19451 class_type,
19452 /*check_dependency_p=*/false,
19453 /*class_head_p=*/true,
19454 /*is_declaration=*/false);
19455 /* If that didn't work, ignore the nested-name-specifier. */
19456 if (!cp_parser_parse_definitely (parser))
19457 {
19458 invalid_nested_name_p = true;
19459 type_start_token = cp_lexer_peek_token (parser->lexer);
19460 id = cp_parser_identifier (parser);
19461 if (id == error_mark_node)
19462 id = NULL_TREE;
19463 }
19464 /* If we could not find a corresponding TYPE, treat this
19465 declaration like an unqualified declaration. */
19466 if (type == error_mark_node)
19467 nested_name_specifier = NULL_TREE;
19468 /* Otherwise, count the number of templates used in TYPE and its
19469 containing scopes. */
19470 else
19471 {
19472 tree scope;
19473
19474 for (scope = TREE_TYPE (type);
19475 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19476 scope = get_containing_scope (scope))
19477 if (TYPE_P (scope)
19478 && CLASS_TYPE_P (scope)
19479 && CLASSTYPE_TEMPLATE_INFO (scope)
19480 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19481 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19482 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19483 ++num_templates;
19484 }
19485 }
19486 /* Otherwise, the identifier is optional. */
19487 else
19488 {
19489 /* We don't know whether what comes next is a template-id,
19490 an identifier, or nothing at all. */
19491 cp_parser_parse_tentatively (parser);
19492 /* Check for a template-id. */
19493 type_start_token = cp_lexer_peek_token (parser->lexer);
19494 id = cp_parser_template_id (parser,
19495 /*template_keyword_p=*/false,
19496 /*check_dependency_p=*/true,
19497 class_key,
19498 /*is_declaration=*/true);
19499 /* If that didn't work, it could still be an identifier. */
19500 if (!cp_parser_parse_definitely (parser))
19501 {
19502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19503 {
19504 type_start_token = cp_lexer_peek_token (parser->lexer);
19505 id = cp_parser_identifier (parser);
19506 }
19507 else
19508 id = NULL_TREE;
19509 }
19510 else
19511 {
19512 template_id_p = true;
19513 ++num_templates;
19514 }
19515 }
19516
19517 pop_deferring_access_checks ();
19518
19519 if (id)
19520 {
19521 cp_parser_check_for_invalid_template_id (parser, id,
19522 class_key,
19523 type_start_token->location);
19524 }
19525 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19526
19527 /* If it's not a `:' or a `{' then we can't really be looking at a
19528 class-head, since a class-head only appears as part of a
19529 class-specifier. We have to detect this situation before calling
19530 xref_tag, since that has irreversible side-effects. */
19531 if (!cp_parser_next_token_starts_class_definition_p (parser))
19532 {
19533 cp_parser_error (parser, "expected %<{%> or %<:%>");
19534 type = error_mark_node;
19535 goto out;
19536 }
19537
19538 /* At this point, we're going ahead with the class-specifier, even
19539 if some other problem occurs. */
19540 cp_parser_commit_to_tentative_parse (parser);
19541 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19542 {
19543 cp_parser_error (parser,
19544 "cannot specify %<override%> for a class");
19545 type = error_mark_node;
19546 goto out;
19547 }
19548 /* Issue the error about the overly-qualified name now. */
19549 if (qualified_p)
19550 {
19551 cp_parser_error (parser,
19552 "global qualification of class name is invalid");
19553 type = error_mark_node;
19554 goto out;
19555 }
19556 else if (invalid_nested_name_p)
19557 {
19558 cp_parser_error (parser,
19559 "qualified name does not name a class");
19560 type = error_mark_node;
19561 goto out;
19562 }
19563 else if (nested_name_specifier)
19564 {
19565 tree scope;
19566
19567 /* Reject typedef-names in class heads. */
19568 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19569 {
19570 error_at (type_start_token->location,
19571 "invalid class name in declaration of %qD",
19572 type);
19573 type = NULL_TREE;
19574 goto done;
19575 }
19576
19577 /* Figure out in what scope the declaration is being placed. */
19578 scope = current_scope ();
19579 /* If that scope does not contain the scope in which the
19580 class was originally declared, the program is invalid. */
19581 if (scope && !is_ancestor (scope, nested_name_specifier))
19582 {
19583 if (at_namespace_scope_p ())
19584 error_at (type_start_token->location,
19585 "declaration of %qD in namespace %qD which does not "
19586 "enclose %qD",
19587 type, scope, nested_name_specifier);
19588 else
19589 error_at (type_start_token->location,
19590 "declaration of %qD in %qD which does not enclose %qD",
19591 type, scope, nested_name_specifier);
19592 type = NULL_TREE;
19593 goto done;
19594 }
19595 /* [dcl.meaning]
19596
19597 A declarator-id shall not be qualified except for the
19598 definition of a ... nested class outside of its class
19599 ... [or] the definition or explicit instantiation of a
19600 class member of a namespace outside of its namespace. */
19601 if (scope == nested_name_specifier)
19602 {
19603 permerror (nested_name_specifier_token_start->location,
19604 "extra qualification not allowed");
19605 nested_name_specifier = NULL_TREE;
19606 num_templates = 0;
19607 }
19608 }
19609 /* An explicit-specialization must be preceded by "template <>". If
19610 it is not, try to recover gracefully. */
19611 if (at_namespace_scope_p ()
19612 && parser->num_template_parameter_lists == 0
19613 && template_id_p)
19614 {
19615 error_at (type_start_token->location,
19616 "an explicit specialization must be preceded by %<template <>%>");
19617 invalid_explicit_specialization_p = true;
19618 /* Take the same action that would have been taken by
19619 cp_parser_explicit_specialization. */
19620 ++parser->num_template_parameter_lists;
19621 begin_specialization ();
19622 }
19623 /* There must be no "return" statements between this point and the
19624 end of this function; set "type "to the correct return value and
19625 use "goto done;" to return. */
19626 /* Make sure that the right number of template parameters were
19627 present. */
19628 if (!cp_parser_check_template_parameters (parser, num_templates,
19629 type_start_token->location,
19630 /*declarator=*/NULL))
19631 {
19632 /* If something went wrong, there is no point in even trying to
19633 process the class-definition. */
19634 type = NULL_TREE;
19635 goto done;
19636 }
19637
19638 /* Look up the type. */
19639 if (template_id_p)
19640 {
19641 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19642 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19643 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19644 {
19645 error_at (type_start_token->location,
19646 "function template %qD redeclared as a class template", id);
19647 type = error_mark_node;
19648 }
19649 else
19650 {
19651 type = TREE_TYPE (id);
19652 type = maybe_process_partial_specialization (type);
19653 }
19654 if (nested_name_specifier)
19655 pushed_scope = push_scope (nested_name_specifier);
19656 }
19657 else if (nested_name_specifier)
19658 {
19659 tree class_type;
19660
19661 /* Given:
19662
19663 template <typename T> struct S { struct T };
19664 template <typename T> struct S<T>::T { };
19665
19666 we will get a TYPENAME_TYPE when processing the definition of
19667 `S::T'. We need to resolve it to the actual type before we
19668 try to define it. */
19669 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19670 {
19671 class_type = resolve_typename_type (TREE_TYPE (type),
19672 /*only_current_p=*/false);
19673 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19674 type = TYPE_NAME (class_type);
19675 else
19676 {
19677 cp_parser_error (parser, "could not resolve typename type");
19678 type = error_mark_node;
19679 }
19680 }
19681
19682 if (maybe_process_partial_specialization (TREE_TYPE (type))
19683 == error_mark_node)
19684 {
19685 type = NULL_TREE;
19686 goto done;
19687 }
19688
19689 class_type = current_class_type;
19690 /* Enter the scope indicated by the nested-name-specifier. */
19691 pushed_scope = push_scope (nested_name_specifier);
19692 /* Get the canonical version of this type. */
19693 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19694 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19695 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19696 {
19697 type = push_template_decl (type);
19698 if (type == error_mark_node)
19699 {
19700 type = NULL_TREE;
19701 goto done;
19702 }
19703 }
19704
19705 type = TREE_TYPE (type);
19706 *nested_name_specifier_p = true;
19707 }
19708 else /* The name is not a nested name. */
19709 {
19710 /* If the class was unnamed, create a dummy name. */
19711 if (!id)
19712 id = make_anon_name ();
19713 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19714 parser->num_template_parameter_lists);
19715 }
19716
19717 /* Indicate whether this class was declared as a `class' or as a
19718 `struct'. */
19719 if (TREE_CODE (type) == RECORD_TYPE)
19720 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19721 cp_parser_check_class_key (class_key, type);
19722
19723 /* If this type was already complete, and we see another definition,
19724 that's an error. */
19725 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19726 {
19727 error_at (type_start_token->location, "redefinition of %q#T",
19728 type);
19729 error_at (type_start_token->location, "previous definition of %q+#T",
19730 type);
19731 type = NULL_TREE;
19732 goto done;
19733 }
19734 else if (type == error_mark_node)
19735 type = NULL_TREE;
19736
19737 if (type)
19738 {
19739 /* Apply attributes now, before any use of the class as a template
19740 argument in its base list. */
19741 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19742 fixup_attribute_variants (type);
19743 }
19744
19745 /* We will have entered the scope containing the class; the names of
19746 base classes should be looked up in that context. For example:
19747
19748 struct A { struct B {}; struct C; };
19749 struct A::C : B {};
19750
19751 is valid. */
19752
19753 /* Get the list of base-classes, if there is one. */
19754 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19755 bases = cp_parser_base_clause (parser);
19756 else
19757 bases = NULL_TREE;
19758
19759 /* If we're really defining a class, process the base classes.
19760 If they're invalid, fail. */
19761 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19762 && !xref_basetypes (type, bases))
19763 type = NULL_TREE;
19764
19765 done:
19766 /* Leave the scope given by the nested-name-specifier. We will
19767 enter the class scope itself while processing the members. */
19768 if (pushed_scope)
19769 pop_scope (pushed_scope);
19770
19771 if (invalid_explicit_specialization_p)
19772 {
19773 end_specialization ();
19774 --parser->num_template_parameter_lists;
19775 }
19776
19777 if (type)
19778 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19779 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19780 CLASSTYPE_FINAL (type) = 1;
19781 out:
19782 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19783 return type;
19784 }
19785
19786 /* Parse a class-key.
19787
19788 class-key:
19789 class
19790 struct
19791 union
19792
19793 Returns the kind of class-key specified, or none_type to indicate
19794 error. */
19795
19796 static enum tag_types
19797 cp_parser_class_key (cp_parser* parser)
19798 {
19799 cp_token *token;
19800 enum tag_types tag_type;
19801
19802 /* Look for the class-key. */
19803 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19804 if (!token)
19805 return none_type;
19806
19807 /* Check to see if the TOKEN is a class-key. */
19808 tag_type = cp_parser_token_is_class_key (token);
19809 if (!tag_type)
19810 cp_parser_error (parser, "expected class-key");
19811 return tag_type;
19812 }
19813
19814 /* Parse an (optional) member-specification.
19815
19816 member-specification:
19817 member-declaration member-specification [opt]
19818 access-specifier : member-specification [opt] */
19819
19820 static void
19821 cp_parser_member_specification_opt (cp_parser* parser)
19822 {
19823 while (true)
19824 {
19825 cp_token *token;
19826 enum rid keyword;
19827
19828 /* Peek at the next token. */
19829 token = cp_lexer_peek_token (parser->lexer);
19830 /* If it's a `}', or EOF then we've seen all the members. */
19831 if (token->type == CPP_CLOSE_BRACE
19832 || token->type == CPP_EOF
19833 || token->type == CPP_PRAGMA_EOL)
19834 break;
19835
19836 /* See if this token is a keyword. */
19837 keyword = token->keyword;
19838 switch (keyword)
19839 {
19840 case RID_PUBLIC:
19841 case RID_PROTECTED:
19842 case RID_PRIVATE:
19843 /* Consume the access-specifier. */
19844 cp_lexer_consume_token (parser->lexer);
19845 /* Remember which access-specifier is active. */
19846 current_access_specifier = token->u.value;
19847 /* Look for the `:'. */
19848 cp_parser_require (parser, CPP_COLON, RT_COLON);
19849 break;
19850
19851 default:
19852 /* Accept #pragmas at class scope. */
19853 if (token->type == CPP_PRAGMA)
19854 {
19855 cp_parser_pragma (parser, pragma_member);
19856 break;
19857 }
19858
19859 /* Otherwise, the next construction must be a
19860 member-declaration. */
19861 cp_parser_member_declaration (parser);
19862 }
19863 }
19864 }
19865
19866 /* Parse a member-declaration.
19867
19868 member-declaration:
19869 decl-specifier-seq [opt] member-declarator-list [opt] ;
19870 function-definition ; [opt]
19871 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19872 using-declaration
19873 template-declaration
19874 alias-declaration
19875
19876 member-declarator-list:
19877 member-declarator
19878 member-declarator-list , member-declarator
19879
19880 member-declarator:
19881 declarator pure-specifier [opt]
19882 declarator constant-initializer [opt]
19883 identifier [opt] : constant-expression
19884
19885 GNU Extensions:
19886
19887 member-declaration:
19888 __extension__ member-declaration
19889
19890 member-declarator:
19891 declarator attributes [opt] pure-specifier [opt]
19892 declarator attributes [opt] constant-initializer [opt]
19893 identifier [opt] attributes [opt] : constant-expression
19894
19895 C++0x Extensions:
19896
19897 member-declaration:
19898 static_assert-declaration */
19899
19900 static void
19901 cp_parser_member_declaration (cp_parser* parser)
19902 {
19903 cp_decl_specifier_seq decl_specifiers;
19904 tree prefix_attributes;
19905 tree decl;
19906 int declares_class_or_enum;
19907 bool friend_p;
19908 cp_token *token = NULL;
19909 cp_token *decl_spec_token_start = NULL;
19910 cp_token *initializer_token_start = NULL;
19911 int saved_pedantic;
19912 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19913
19914 /* Check for the `__extension__' keyword. */
19915 if (cp_parser_extension_opt (parser, &saved_pedantic))
19916 {
19917 /* Recurse. */
19918 cp_parser_member_declaration (parser);
19919 /* Restore the old value of the PEDANTIC flag. */
19920 pedantic = saved_pedantic;
19921
19922 return;
19923 }
19924
19925 /* Check for a template-declaration. */
19926 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19927 {
19928 /* An explicit specialization here is an error condition, and we
19929 expect the specialization handler to detect and report this. */
19930 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19931 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19932 cp_parser_explicit_specialization (parser);
19933 else
19934 cp_parser_template_declaration (parser, /*member_p=*/true);
19935
19936 return;
19937 }
19938
19939 /* Check for a using-declaration. */
19940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19941 {
19942 if (cxx_dialect < cxx11)
19943 {
19944 /* Parse the using-declaration. */
19945 cp_parser_using_declaration (parser,
19946 /*access_declaration_p=*/false);
19947 return;
19948 }
19949 else
19950 {
19951 tree decl;
19952 bool alias_decl_expected;
19953 cp_parser_parse_tentatively (parser);
19954 decl = cp_parser_alias_declaration (parser);
19955 /* Note that if we actually see the '=' token after the
19956 identifier, cp_parser_alias_declaration commits the
19957 tentative parse. In that case, we really expects an
19958 alias-declaration. Otherwise, we expect a using
19959 declaration. */
19960 alias_decl_expected =
19961 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19962 cp_parser_parse_definitely (parser);
19963
19964 if (alias_decl_expected)
19965 finish_member_declaration (decl);
19966 else
19967 cp_parser_using_declaration (parser,
19968 /*access_declaration_p=*/false);
19969 return;
19970 }
19971 }
19972
19973 /* Check for @defs. */
19974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19975 {
19976 tree ivar, member;
19977 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19978 ivar = ivar_chains;
19979 while (ivar)
19980 {
19981 member = ivar;
19982 ivar = TREE_CHAIN (member);
19983 TREE_CHAIN (member) = NULL_TREE;
19984 finish_member_declaration (member);
19985 }
19986 return;
19987 }
19988
19989 /* If the next token is `static_assert' we have a static assertion. */
19990 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19991 {
19992 cp_parser_static_assert (parser, /*member_p=*/true);
19993 return;
19994 }
19995
19996 parser->colon_corrects_to_scope_p = false;
19997
19998 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19999 goto out;
20000
20001 /* Parse the decl-specifier-seq. */
20002 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20003 cp_parser_decl_specifier_seq (parser,
20004 CP_PARSER_FLAGS_OPTIONAL,
20005 &decl_specifiers,
20006 &declares_class_or_enum);
20007 /* Check for an invalid type-name. */
20008 if (!decl_specifiers.any_type_specifiers_p
20009 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20010 goto out;
20011 /* If there is no declarator, then the decl-specifier-seq should
20012 specify a type. */
20013 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20014 {
20015 /* If there was no decl-specifier-seq, and the next token is a
20016 `;', then we have something like:
20017
20018 struct S { ; };
20019
20020 [class.mem]
20021
20022 Each member-declaration shall declare at least one member
20023 name of the class. */
20024 if (!decl_specifiers.any_specifiers_p)
20025 {
20026 cp_token *token = cp_lexer_peek_token (parser->lexer);
20027 if (!in_system_header_at (token->location))
20028 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20029 }
20030 else
20031 {
20032 tree type;
20033
20034 /* See if this declaration is a friend. */
20035 friend_p = cp_parser_friend_p (&decl_specifiers);
20036 /* If there were decl-specifiers, check to see if there was
20037 a class-declaration. */
20038 type = check_tag_decl (&decl_specifiers,
20039 /*explicit_type_instantiation_p=*/false);
20040 /* Nested classes have already been added to the class, but
20041 a `friend' needs to be explicitly registered. */
20042 if (friend_p)
20043 {
20044 /* If the `friend' keyword was present, the friend must
20045 be introduced with a class-key. */
20046 if (!declares_class_or_enum && cxx_dialect < cxx11)
20047 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20048 "in C++03 a class-key must be used "
20049 "when declaring a friend");
20050 /* In this case:
20051
20052 template <typename T> struct A {
20053 friend struct A<T>::B;
20054 };
20055
20056 A<T>::B will be represented by a TYPENAME_TYPE, and
20057 therefore not recognized by check_tag_decl. */
20058 if (!type)
20059 {
20060 type = decl_specifiers.type;
20061 if (type && TREE_CODE (type) == TYPE_DECL)
20062 type = TREE_TYPE (type);
20063 }
20064 if (!type || !TYPE_P (type))
20065 error_at (decl_spec_token_start->location,
20066 "friend declaration does not name a class or "
20067 "function");
20068 else
20069 make_friend_class (current_class_type, type,
20070 /*complain=*/true);
20071 }
20072 /* If there is no TYPE, an error message will already have
20073 been issued. */
20074 else if (!type || type == error_mark_node)
20075 ;
20076 /* An anonymous aggregate has to be handled specially; such
20077 a declaration really declares a data member (with a
20078 particular type), as opposed to a nested class. */
20079 else if (ANON_AGGR_TYPE_P (type))
20080 {
20081 /* C++11 9.5/6. */
20082 if (decl_specifiers.storage_class != sc_none)
20083 error_at (decl_spec_token_start->location,
20084 "a storage class on an anonymous aggregate "
20085 "in class scope is not allowed");
20086
20087 /* Remove constructors and such from TYPE, now that we
20088 know it is an anonymous aggregate. */
20089 fixup_anonymous_aggr (type);
20090 /* And make the corresponding data member. */
20091 decl = build_decl (decl_spec_token_start->location,
20092 FIELD_DECL, NULL_TREE, type);
20093 /* Add it to the class. */
20094 finish_member_declaration (decl);
20095 }
20096 else
20097 cp_parser_check_access_in_redeclaration
20098 (TYPE_NAME (type),
20099 decl_spec_token_start->location);
20100 }
20101 }
20102 else
20103 {
20104 bool assume_semicolon = false;
20105
20106 /* Clear attributes from the decl_specifiers but keep them
20107 around as prefix attributes that apply them to the entity
20108 being declared. */
20109 prefix_attributes = decl_specifiers.attributes;
20110 decl_specifiers.attributes = NULL_TREE;
20111
20112 /* See if these declarations will be friends. */
20113 friend_p = cp_parser_friend_p (&decl_specifiers);
20114
20115 /* Keep going until we hit the `;' at the end of the
20116 declaration. */
20117 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20118 {
20119 tree attributes = NULL_TREE;
20120 tree first_attribute;
20121
20122 /* Peek at the next token. */
20123 token = cp_lexer_peek_token (parser->lexer);
20124
20125 /* Check for a bitfield declaration. */
20126 if (token->type == CPP_COLON
20127 || (token->type == CPP_NAME
20128 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20129 == CPP_COLON))
20130 {
20131 tree identifier;
20132 tree width;
20133
20134 /* Get the name of the bitfield. Note that we cannot just
20135 check TOKEN here because it may have been invalidated by
20136 the call to cp_lexer_peek_nth_token above. */
20137 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20138 identifier = cp_parser_identifier (parser);
20139 else
20140 identifier = NULL_TREE;
20141
20142 /* Consume the `:' token. */
20143 cp_lexer_consume_token (parser->lexer);
20144 /* Get the width of the bitfield. */
20145 width
20146 = cp_parser_constant_expression (parser,
20147 /*allow_non_constant=*/false,
20148 NULL);
20149
20150 /* Look for attributes that apply to the bitfield. */
20151 attributes = cp_parser_attributes_opt (parser);
20152 /* Remember which attributes are prefix attributes and
20153 which are not. */
20154 first_attribute = attributes;
20155 /* Combine the attributes. */
20156 attributes = chainon (prefix_attributes, attributes);
20157
20158 /* Create the bitfield declaration. */
20159 decl = grokbitfield (identifier
20160 ? make_id_declarator (NULL_TREE,
20161 identifier,
20162 sfk_none)
20163 : NULL,
20164 &decl_specifiers,
20165 width,
20166 attributes);
20167 }
20168 else
20169 {
20170 cp_declarator *declarator;
20171 tree initializer;
20172 tree asm_specification;
20173 int ctor_dtor_or_conv_p;
20174
20175 /* Parse the declarator. */
20176 declarator
20177 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20178 &ctor_dtor_or_conv_p,
20179 /*parenthesized_p=*/NULL,
20180 /*member_p=*/true);
20181
20182 /* If something went wrong parsing the declarator, make sure
20183 that we at least consume some tokens. */
20184 if (declarator == cp_error_declarator)
20185 {
20186 /* Skip to the end of the statement. */
20187 cp_parser_skip_to_end_of_statement (parser);
20188 /* If the next token is not a semicolon, that is
20189 probably because we just skipped over the body of
20190 a function. So, we consume a semicolon if
20191 present, but do not issue an error message if it
20192 is not present. */
20193 if (cp_lexer_next_token_is (parser->lexer,
20194 CPP_SEMICOLON))
20195 cp_lexer_consume_token (parser->lexer);
20196 goto out;
20197 }
20198
20199 if (declares_class_or_enum & 2)
20200 cp_parser_check_for_definition_in_return_type
20201 (declarator, decl_specifiers.type,
20202 decl_specifiers.locations[ds_type_spec]);
20203
20204 /* Look for an asm-specification. */
20205 asm_specification = cp_parser_asm_specification_opt (parser);
20206 /* Look for attributes that apply to the declaration. */
20207 attributes = cp_parser_attributes_opt (parser);
20208 /* Remember which attributes are prefix attributes and
20209 which are not. */
20210 first_attribute = attributes;
20211 /* Combine the attributes. */
20212 attributes = chainon (prefix_attributes, attributes);
20213
20214 /* If it's an `=', then we have a constant-initializer or a
20215 pure-specifier. It is not correct to parse the
20216 initializer before registering the member declaration
20217 since the member declaration should be in scope while
20218 its initializer is processed. However, the rest of the
20219 front end does not yet provide an interface that allows
20220 us to handle this correctly. */
20221 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20222 {
20223 /* In [class.mem]:
20224
20225 A pure-specifier shall be used only in the declaration of
20226 a virtual function.
20227
20228 A member-declarator can contain a constant-initializer
20229 only if it declares a static member of integral or
20230 enumeration type.
20231
20232 Therefore, if the DECLARATOR is for a function, we look
20233 for a pure-specifier; otherwise, we look for a
20234 constant-initializer. When we call `grokfield', it will
20235 perform more stringent semantics checks. */
20236 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20237 if (function_declarator_p (declarator)
20238 || (decl_specifiers.type
20239 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20240 && declarator->kind == cdk_id
20241 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20242 == FUNCTION_TYPE)))
20243 initializer = cp_parser_pure_specifier (parser);
20244 else if (decl_specifiers.storage_class != sc_static)
20245 initializer = cp_parser_save_nsdmi (parser);
20246 else if (cxx_dialect >= cxx11)
20247 {
20248 bool nonconst;
20249 /* Don't require a constant rvalue in C++11, since we
20250 might want a reference constant. We'll enforce
20251 constancy later. */
20252 cp_lexer_consume_token (parser->lexer);
20253 /* Parse the initializer. */
20254 initializer = cp_parser_initializer_clause (parser,
20255 &nonconst);
20256 }
20257 else
20258 /* Parse the initializer. */
20259 initializer = cp_parser_constant_initializer (parser);
20260 }
20261 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20262 && !function_declarator_p (declarator))
20263 {
20264 bool x;
20265 if (decl_specifiers.storage_class != sc_static)
20266 initializer = cp_parser_save_nsdmi (parser);
20267 else
20268 initializer = cp_parser_initializer (parser, &x, &x);
20269 }
20270 /* Otherwise, there is no initializer. */
20271 else
20272 initializer = NULL_TREE;
20273
20274 /* See if we are probably looking at a function
20275 definition. We are certainly not looking at a
20276 member-declarator. Calling `grokfield' has
20277 side-effects, so we must not do it unless we are sure
20278 that we are looking at a member-declarator. */
20279 if (cp_parser_token_starts_function_definition_p
20280 (cp_lexer_peek_token (parser->lexer)))
20281 {
20282 /* The grammar does not allow a pure-specifier to be
20283 used when a member function is defined. (It is
20284 possible that this fact is an oversight in the
20285 standard, since a pure function may be defined
20286 outside of the class-specifier. */
20287 if (initializer && initializer_token_start)
20288 error_at (initializer_token_start->location,
20289 "pure-specifier on function-definition");
20290 decl = cp_parser_save_member_function_body (parser,
20291 &decl_specifiers,
20292 declarator,
20293 attributes);
20294 /* If the member was not a friend, declare it here. */
20295 if (!friend_p)
20296 {
20297 if (parser->fully_implicit_function_template_p)
20298 decl = finish_fully_implicit_template (parser, decl);
20299 finish_member_declaration (decl);
20300 }
20301 /* Peek at the next token. */
20302 token = cp_lexer_peek_token (parser->lexer);
20303 /* If the next token is a semicolon, consume it. */
20304 if (token->type == CPP_SEMICOLON)
20305 cp_lexer_consume_token (parser->lexer);
20306 goto out;
20307 }
20308 else
20309 if (declarator->kind == cdk_function)
20310 declarator->id_loc = token->location;
20311 /* Create the declaration. */
20312 decl = grokfield (declarator, &decl_specifiers,
20313 initializer, /*init_const_expr_p=*/true,
20314 asm_specification, attributes);
20315 if (parser->fully_implicit_function_template_p)
20316 decl = finish_fully_implicit_template (parser, decl);
20317 }
20318
20319 cp_finalize_omp_declare_simd (parser, decl);
20320
20321 /* Reset PREFIX_ATTRIBUTES. */
20322 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20323 attributes = TREE_CHAIN (attributes);
20324 if (attributes)
20325 TREE_CHAIN (attributes) = NULL_TREE;
20326
20327 /* If there is any qualification still in effect, clear it
20328 now; we will be starting fresh with the next declarator. */
20329 parser->scope = NULL_TREE;
20330 parser->qualifying_scope = NULL_TREE;
20331 parser->object_scope = NULL_TREE;
20332 /* If it's a `,', then there are more declarators. */
20333 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20334 {
20335 cp_lexer_consume_token (parser->lexer);
20336 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20337 {
20338 cp_token *token = cp_lexer_previous_token (parser->lexer);
20339 error_at (token->location,
20340 "stray %<,%> at end of member declaration");
20341 }
20342 }
20343 /* If the next token isn't a `;', then we have a parse error. */
20344 else if (cp_lexer_next_token_is_not (parser->lexer,
20345 CPP_SEMICOLON))
20346 {
20347 /* The next token might be a ways away from where the
20348 actual semicolon is missing. Find the previous token
20349 and use that for our error position. */
20350 cp_token *token = cp_lexer_previous_token (parser->lexer);
20351 error_at (token->location,
20352 "expected %<;%> at end of member declaration");
20353
20354 /* Assume that the user meant to provide a semicolon. If
20355 we were to cp_parser_skip_to_end_of_statement, we might
20356 skip to a semicolon inside a member function definition
20357 and issue nonsensical error messages. */
20358 assume_semicolon = true;
20359 }
20360
20361 if (decl)
20362 {
20363 /* Add DECL to the list of members. */
20364 if (!friend_p)
20365 finish_member_declaration (decl);
20366
20367 if (TREE_CODE (decl) == FUNCTION_DECL)
20368 cp_parser_save_default_args (parser, decl);
20369 else if (TREE_CODE (decl) == FIELD_DECL
20370 && !DECL_C_BIT_FIELD (decl)
20371 && DECL_INITIAL (decl))
20372 /* Add DECL to the queue of NSDMI to be parsed later. */
20373 vec_safe_push (unparsed_nsdmis, decl);
20374 }
20375
20376 if (assume_semicolon)
20377 goto out;
20378 }
20379 }
20380
20381 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20382 out:
20383 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20384 }
20385
20386 /* Parse a pure-specifier.
20387
20388 pure-specifier:
20389 = 0
20390
20391 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20392 Otherwise, ERROR_MARK_NODE is returned. */
20393
20394 static tree
20395 cp_parser_pure_specifier (cp_parser* parser)
20396 {
20397 cp_token *token;
20398
20399 /* Look for the `=' token. */
20400 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20401 return error_mark_node;
20402 /* Look for the `0' token. */
20403 token = cp_lexer_peek_token (parser->lexer);
20404
20405 if (token->type == CPP_EOF
20406 || token->type == CPP_PRAGMA_EOL)
20407 return error_mark_node;
20408
20409 cp_lexer_consume_token (parser->lexer);
20410
20411 /* Accept = default or = delete in c++0x mode. */
20412 if (token->keyword == RID_DEFAULT
20413 || token->keyword == RID_DELETE)
20414 {
20415 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20416 return token->u.value;
20417 }
20418
20419 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20420 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20421 {
20422 cp_parser_error (parser,
20423 "invalid pure specifier (only %<= 0%> is allowed)");
20424 cp_parser_skip_to_end_of_statement (parser);
20425 return error_mark_node;
20426 }
20427 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20428 {
20429 error_at (token->location, "templates may not be %<virtual%>");
20430 return error_mark_node;
20431 }
20432
20433 return integer_zero_node;
20434 }
20435
20436 /* Parse a constant-initializer.
20437
20438 constant-initializer:
20439 = constant-expression
20440
20441 Returns a representation of the constant-expression. */
20442
20443 static tree
20444 cp_parser_constant_initializer (cp_parser* parser)
20445 {
20446 /* Look for the `=' token. */
20447 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20448 return error_mark_node;
20449
20450 /* It is invalid to write:
20451
20452 struct S { static const int i = { 7 }; };
20453
20454 */
20455 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20456 {
20457 cp_parser_error (parser,
20458 "a brace-enclosed initializer is not allowed here");
20459 /* Consume the opening brace. */
20460 cp_lexer_consume_token (parser->lexer);
20461 /* Skip the initializer. */
20462 cp_parser_skip_to_closing_brace (parser);
20463 /* Look for the trailing `}'. */
20464 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20465
20466 return error_mark_node;
20467 }
20468
20469 return cp_parser_constant_expression (parser,
20470 /*allow_non_constant=*/false,
20471 NULL);
20472 }
20473
20474 /* Derived classes [gram.class.derived] */
20475
20476 /* Parse a base-clause.
20477
20478 base-clause:
20479 : base-specifier-list
20480
20481 base-specifier-list:
20482 base-specifier ... [opt]
20483 base-specifier-list , base-specifier ... [opt]
20484
20485 Returns a TREE_LIST representing the base-classes, in the order in
20486 which they were declared. The representation of each node is as
20487 described by cp_parser_base_specifier.
20488
20489 In the case that no bases are specified, this function will return
20490 NULL_TREE, not ERROR_MARK_NODE. */
20491
20492 static tree
20493 cp_parser_base_clause (cp_parser* parser)
20494 {
20495 tree bases = NULL_TREE;
20496
20497 /* Look for the `:' that begins the list. */
20498 cp_parser_require (parser, CPP_COLON, RT_COLON);
20499
20500 /* Scan the base-specifier-list. */
20501 while (true)
20502 {
20503 cp_token *token;
20504 tree base;
20505 bool pack_expansion_p = false;
20506
20507 /* Look for the base-specifier. */
20508 base = cp_parser_base_specifier (parser);
20509 /* Look for the (optional) ellipsis. */
20510 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20511 {
20512 /* Consume the `...'. */
20513 cp_lexer_consume_token (parser->lexer);
20514
20515 pack_expansion_p = true;
20516 }
20517
20518 /* Add BASE to the front of the list. */
20519 if (base && base != error_mark_node)
20520 {
20521 if (pack_expansion_p)
20522 /* Make this a pack expansion type. */
20523 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20524
20525 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20526 {
20527 TREE_CHAIN (base) = bases;
20528 bases = base;
20529 }
20530 }
20531 /* Peek at the next token. */
20532 token = cp_lexer_peek_token (parser->lexer);
20533 /* If it's not a comma, then the list is complete. */
20534 if (token->type != CPP_COMMA)
20535 break;
20536 /* Consume the `,'. */
20537 cp_lexer_consume_token (parser->lexer);
20538 }
20539
20540 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20541 base class had a qualified name. However, the next name that
20542 appears is certainly not qualified. */
20543 parser->scope = NULL_TREE;
20544 parser->qualifying_scope = NULL_TREE;
20545 parser->object_scope = NULL_TREE;
20546
20547 return nreverse (bases);
20548 }
20549
20550 /* Parse a base-specifier.
20551
20552 base-specifier:
20553 :: [opt] nested-name-specifier [opt] class-name
20554 virtual access-specifier [opt] :: [opt] nested-name-specifier
20555 [opt] class-name
20556 access-specifier virtual [opt] :: [opt] nested-name-specifier
20557 [opt] class-name
20558
20559 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20560 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20561 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20562 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20563
20564 static tree
20565 cp_parser_base_specifier (cp_parser* parser)
20566 {
20567 cp_token *token;
20568 bool done = false;
20569 bool virtual_p = false;
20570 bool duplicate_virtual_error_issued_p = false;
20571 bool duplicate_access_error_issued_p = false;
20572 bool class_scope_p, template_p;
20573 tree access = access_default_node;
20574 tree type;
20575
20576 /* Process the optional `virtual' and `access-specifier'. */
20577 while (!done)
20578 {
20579 /* Peek at the next token. */
20580 token = cp_lexer_peek_token (parser->lexer);
20581 /* Process `virtual'. */
20582 switch (token->keyword)
20583 {
20584 case RID_VIRTUAL:
20585 /* If `virtual' appears more than once, issue an error. */
20586 if (virtual_p && !duplicate_virtual_error_issued_p)
20587 {
20588 cp_parser_error (parser,
20589 "%<virtual%> specified more than once in base-specified");
20590 duplicate_virtual_error_issued_p = true;
20591 }
20592
20593 virtual_p = true;
20594
20595 /* Consume the `virtual' token. */
20596 cp_lexer_consume_token (parser->lexer);
20597
20598 break;
20599
20600 case RID_PUBLIC:
20601 case RID_PROTECTED:
20602 case RID_PRIVATE:
20603 /* If more than one access specifier appears, issue an
20604 error. */
20605 if (access != access_default_node
20606 && !duplicate_access_error_issued_p)
20607 {
20608 cp_parser_error (parser,
20609 "more than one access specifier in base-specified");
20610 duplicate_access_error_issued_p = true;
20611 }
20612
20613 access = ridpointers[(int) token->keyword];
20614
20615 /* Consume the access-specifier. */
20616 cp_lexer_consume_token (parser->lexer);
20617
20618 break;
20619
20620 default:
20621 done = true;
20622 break;
20623 }
20624 }
20625 /* It is not uncommon to see programs mechanically, erroneously, use
20626 the 'typename' keyword to denote (dependent) qualified types
20627 as base classes. */
20628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20629 {
20630 token = cp_lexer_peek_token (parser->lexer);
20631 if (!processing_template_decl)
20632 error_at (token->location,
20633 "keyword %<typename%> not allowed outside of templates");
20634 else
20635 error_at (token->location,
20636 "keyword %<typename%> not allowed in this context "
20637 "(the base class is implicitly a type)");
20638 cp_lexer_consume_token (parser->lexer);
20639 }
20640
20641 /* Look for the optional `::' operator. */
20642 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20643 /* Look for the nested-name-specifier. The simplest way to
20644 implement:
20645
20646 [temp.res]
20647
20648 The keyword `typename' is not permitted in a base-specifier or
20649 mem-initializer; in these contexts a qualified name that
20650 depends on a template-parameter is implicitly assumed to be a
20651 type name.
20652
20653 is to pretend that we have seen the `typename' keyword at this
20654 point. */
20655 cp_parser_nested_name_specifier_opt (parser,
20656 /*typename_keyword_p=*/true,
20657 /*check_dependency_p=*/true,
20658 typename_type,
20659 /*is_declaration=*/true);
20660 /* If the base class is given by a qualified name, assume that names
20661 we see are type names or templates, as appropriate. */
20662 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20663 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20664
20665 if (!parser->scope
20666 && cp_lexer_next_token_is_decltype (parser->lexer))
20667 /* DR 950 allows decltype as a base-specifier. */
20668 type = cp_parser_decltype (parser);
20669 else
20670 {
20671 /* Otherwise, look for the class-name. */
20672 type = cp_parser_class_name (parser,
20673 class_scope_p,
20674 template_p,
20675 typename_type,
20676 /*check_dependency_p=*/true,
20677 /*class_head_p=*/false,
20678 /*is_declaration=*/true);
20679 type = TREE_TYPE (type);
20680 }
20681
20682 if (type == error_mark_node)
20683 return error_mark_node;
20684
20685 return finish_base_specifier (type, access, virtual_p);
20686 }
20687
20688 /* Exception handling [gram.exception] */
20689
20690 /* Parse an (optional) noexcept-specification.
20691
20692 noexcept-specification:
20693 noexcept ( constant-expression ) [opt]
20694
20695 If no noexcept-specification is present, returns NULL_TREE.
20696 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20697 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20698 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20699 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20700 in which case a boolean condition is returned instead. */
20701
20702 static tree
20703 cp_parser_noexcept_specification_opt (cp_parser* parser,
20704 bool require_constexpr,
20705 bool* consumed_expr,
20706 bool return_cond)
20707 {
20708 cp_token *token;
20709 const char *saved_message;
20710
20711 /* Peek at the next token. */
20712 token = cp_lexer_peek_token (parser->lexer);
20713
20714 /* Is it a noexcept-specification? */
20715 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20716 {
20717 tree expr;
20718 cp_lexer_consume_token (parser->lexer);
20719
20720 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20721 {
20722 cp_lexer_consume_token (parser->lexer);
20723
20724 if (require_constexpr)
20725 {
20726 /* Types may not be defined in an exception-specification. */
20727 saved_message = parser->type_definition_forbidden_message;
20728 parser->type_definition_forbidden_message
20729 = G_("types may not be defined in an exception-specification");
20730
20731 expr = cp_parser_constant_expression (parser, false, NULL);
20732
20733 /* Restore the saved message. */
20734 parser->type_definition_forbidden_message = saved_message;
20735 }
20736 else
20737 {
20738 expr = cp_parser_expression (parser, false, NULL);
20739 *consumed_expr = true;
20740 }
20741
20742 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20743 }
20744 else
20745 {
20746 expr = boolean_true_node;
20747 if (!require_constexpr)
20748 *consumed_expr = false;
20749 }
20750
20751 /* We cannot build a noexcept-spec right away because this will check
20752 that expr is a constexpr. */
20753 if (!return_cond)
20754 return build_noexcept_spec (expr, tf_warning_or_error);
20755 else
20756 return expr;
20757 }
20758 else
20759 return NULL_TREE;
20760 }
20761
20762 /* Parse an (optional) exception-specification.
20763
20764 exception-specification:
20765 throw ( type-id-list [opt] )
20766
20767 Returns a TREE_LIST representing the exception-specification. The
20768 TREE_VALUE of each node is a type. */
20769
20770 static tree
20771 cp_parser_exception_specification_opt (cp_parser* parser)
20772 {
20773 cp_token *token;
20774 tree type_id_list;
20775 const char *saved_message;
20776
20777 /* Peek at the next token. */
20778 token = cp_lexer_peek_token (parser->lexer);
20779
20780 /* Is it a noexcept-specification? */
20781 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20782 false);
20783 if (type_id_list != NULL_TREE)
20784 return type_id_list;
20785
20786 /* If it's not `throw', then there's no exception-specification. */
20787 if (!cp_parser_is_keyword (token, RID_THROW))
20788 return NULL_TREE;
20789
20790 #if 0
20791 /* Enable this once a lot of code has transitioned to noexcept? */
20792 if (cxx_dialect >= cxx11 && !in_system_header)
20793 warning (OPT_Wdeprecated, "dynamic exception specifications are "
20794 "deprecated in C++0x; use %<noexcept%> instead");
20795 #endif
20796
20797 /* Consume the `throw'. */
20798 cp_lexer_consume_token (parser->lexer);
20799
20800 /* Look for the `('. */
20801 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20802
20803 /* Peek at the next token. */
20804 token = cp_lexer_peek_token (parser->lexer);
20805 /* If it's not a `)', then there is a type-id-list. */
20806 if (token->type != CPP_CLOSE_PAREN)
20807 {
20808 /* Types may not be defined in an exception-specification. */
20809 saved_message = parser->type_definition_forbidden_message;
20810 parser->type_definition_forbidden_message
20811 = G_("types may not be defined in an exception-specification");
20812 /* Parse the type-id-list. */
20813 type_id_list = cp_parser_type_id_list (parser);
20814 /* Restore the saved message. */
20815 parser->type_definition_forbidden_message = saved_message;
20816 }
20817 else
20818 type_id_list = empty_except_spec;
20819
20820 /* Look for the `)'. */
20821 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20822
20823 return type_id_list;
20824 }
20825
20826 /* Parse an (optional) type-id-list.
20827
20828 type-id-list:
20829 type-id ... [opt]
20830 type-id-list , type-id ... [opt]
20831
20832 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20833 in the order that the types were presented. */
20834
20835 static tree
20836 cp_parser_type_id_list (cp_parser* parser)
20837 {
20838 tree types = NULL_TREE;
20839
20840 while (true)
20841 {
20842 cp_token *token;
20843 tree type;
20844
20845 /* Get the next type-id. */
20846 type = cp_parser_type_id (parser);
20847 /* Parse the optional ellipsis. */
20848 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20849 {
20850 /* Consume the `...'. */
20851 cp_lexer_consume_token (parser->lexer);
20852
20853 /* Turn the type into a pack expansion expression. */
20854 type = make_pack_expansion (type);
20855 }
20856 /* Add it to the list. */
20857 types = add_exception_specifier (types, type, /*complain=*/1);
20858 /* Peek at the next token. */
20859 token = cp_lexer_peek_token (parser->lexer);
20860 /* If it is not a `,', we are done. */
20861 if (token->type != CPP_COMMA)
20862 break;
20863 /* Consume the `,'. */
20864 cp_lexer_consume_token (parser->lexer);
20865 }
20866
20867 return nreverse (types);
20868 }
20869
20870 /* Parse a try-block.
20871
20872 try-block:
20873 try compound-statement handler-seq */
20874
20875 static tree
20876 cp_parser_try_block (cp_parser* parser)
20877 {
20878 tree try_block;
20879
20880 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20881 try_block = begin_try_block ();
20882 cp_parser_compound_statement (parser, NULL, true, false);
20883 finish_try_block (try_block);
20884 cp_parser_handler_seq (parser);
20885 finish_handler_sequence (try_block);
20886
20887 return try_block;
20888 }
20889
20890 /* Parse a function-try-block.
20891
20892 function-try-block:
20893 try ctor-initializer [opt] function-body handler-seq */
20894
20895 static bool
20896 cp_parser_function_try_block (cp_parser* parser)
20897 {
20898 tree compound_stmt;
20899 tree try_block;
20900 bool ctor_initializer_p;
20901
20902 /* Look for the `try' keyword. */
20903 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20904 return false;
20905 /* Let the rest of the front end know where we are. */
20906 try_block = begin_function_try_block (&compound_stmt);
20907 /* Parse the function-body. */
20908 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20909 (parser, /*in_function_try_block=*/true);
20910 /* We're done with the `try' part. */
20911 finish_function_try_block (try_block);
20912 /* Parse the handlers. */
20913 cp_parser_handler_seq (parser);
20914 /* We're done with the handlers. */
20915 finish_function_handler_sequence (try_block, compound_stmt);
20916
20917 return ctor_initializer_p;
20918 }
20919
20920 /* Parse a handler-seq.
20921
20922 handler-seq:
20923 handler handler-seq [opt] */
20924
20925 static void
20926 cp_parser_handler_seq (cp_parser* parser)
20927 {
20928 while (true)
20929 {
20930 cp_token *token;
20931
20932 /* Parse the handler. */
20933 cp_parser_handler (parser);
20934 /* Peek at the next token. */
20935 token = cp_lexer_peek_token (parser->lexer);
20936 /* If it's not `catch' then there are no more handlers. */
20937 if (!cp_parser_is_keyword (token, RID_CATCH))
20938 break;
20939 }
20940 }
20941
20942 /* Parse a handler.
20943
20944 handler:
20945 catch ( exception-declaration ) compound-statement */
20946
20947 static void
20948 cp_parser_handler (cp_parser* parser)
20949 {
20950 tree handler;
20951 tree declaration;
20952
20953 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20954 handler = begin_handler ();
20955 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20956 declaration = cp_parser_exception_declaration (parser);
20957 finish_handler_parms (declaration, handler);
20958 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20959 cp_parser_compound_statement (parser, NULL, false, false);
20960 finish_handler (handler);
20961 }
20962
20963 /* Parse an exception-declaration.
20964
20965 exception-declaration:
20966 type-specifier-seq declarator
20967 type-specifier-seq abstract-declarator
20968 type-specifier-seq
20969 ...
20970
20971 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20972 ellipsis variant is used. */
20973
20974 static tree
20975 cp_parser_exception_declaration (cp_parser* parser)
20976 {
20977 cp_decl_specifier_seq type_specifiers;
20978 cp_declarator *declarator;
20979 const char *saved_message;
20980
20981 /* If it's an ellipsis, it's easy to handle. */
20982 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20983 {
20984 /* Consume the `...' token. */
20985 cp_lexer_consume_token (parser->lexer);
20986 return NULL_TREE;
20987 }
20988
20989 /* Types may not be defined in exception-declarations. */
20990 saved_message = parser->type_definition_forbidden_message;
20991 parser->type_definition_forbidden_message
20992 = G_("types may not be defined in exception-declarations");
20993
20994 /* Parse the type-specifier-seq. */
20995 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20996 /*is_trailing_return=*/false,
20997 &type_specifiers);
20998 /* If it's a `)', then there is no declarator. */
20999 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21000 declarator = NULL;
21001 else
21002 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21003 /*ctor_dtor_or_conv_p=*/NULL,
21004 /*parenthesized_p=*/NULL,
21005 /*member_p=*/false);
21006
21007 /* Restore the saved message. */
21008 parser->type_definition_forbidden_message = saved_message;
21009
21010 if (!type_specifiers.any_specifiers_p)
21011 return error_mark_node;
21012
21013 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21014 }
21015
21016 /* Parse a throw-expression.
21017
21018 throw-expression:
21019 throw assignment-expression [opt]
21020
21021 Returns a THROW_EXPR representing the throw-expression. */
21022
21023 static tree
21024 cp_parser_throw_expression (cp_parser* parser)
21025 {
21026 tree expression;
21027 cp_token* token;
21028
21029 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21030 token = cp_lexer_peek_token (parser->lexer);
21031 /* Figure out whether or not there is an assignment-expression
21032 following the "throw" keyword. */
21033 if (token->type == CPP_COMMA
21034 || token->type == CPP_SEMICOLON
21035 || token->type == CPP_CLOSE_PAREN
21036 || token->type == CPP_CLOSE_SQUARE
21037 || token->type == CPP_CLOSE_BRACE
21038 || token->type == CPP_COLON)
21039 expression = NULL_TREE;
21040 else
21041 expression = cp_parser_assignment_expression (parser,
21042 /*cast_p=*/false, NULL);
21043
21044 return build_throw (expression);
21045 }
21046
21047 /* GNU Extensions */
21048
21049 /* Parse an (optional) asm-specification.
21050
21051 asm-specification:
21052 asm ( string-literal )
21053
21054 If the asm-specification is present, returns a STRING_CST
21055 corresponding to the string-literal. Otherwise, returns
21056 NULL_TREE. */
21057
21058 static tree
21059 cp_parser_asm_specification_opt (cp_parser* parser)
21060 {
21061 cp_token *token;
21062 tree asm_specification;
21063
21064 /* Peek at the next token. */
21065 token = cp_lexer_peek_token (parser->lexer);
21066 /* If the next token isn't the `asm' keyword, then there's no
21067 asm-specification. */
21068 if (!cp_parser_is_keyword (token, RID_ASM))
21069 return NULL_TREE;
21070
21071 /* Consume the `asm' token. */
21072 cp_lexer_consume_token (parser->lexer);
21073 /* Look for the `('. */
21074 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21075
21076 /* Look for the string-literal. */
21077 asm_specification = cp_parser_string_literal (parser, false, false);
21078
21079 /* Look for the `)'. */
21080 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21081
21082 return asm_specification;
21083 }
21084
21085 /* Parse an asm-operand-list.
21086
21087 asm-operand-list:
21088 asm-operand
21089 asm-operand-list , asm-operand
21090
21091 asm-operand:
21092 string-literal ( expression )
21093 [ string-literal ] string-literal ( expression )
21094
21095 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21096 each node is the expression. The TREE_PURPOSE is itself a
21097 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21098 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21099 is a STRING_CST for the string literal before the parenthesis. Returns
21100 ERROR_MARK_NODE if any of the operands are invalid. */
21101
21102 static tree
21103 cp_parser_asm_operand_list (cp_parser* parser)
21104 {
21105 tree asm_operands = NULL_TREE;
21106 bool invalid_operands = false;
21107
21108 while (true)
21109 {
21110 tree string_literal;
21111 tree expression;
21112 tree name;
21113
21114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21115 {
21116 /* Consume the `[' token. */
21117 cp_lexer_consume_token (parser->lexer);
21118 /* Read the operand name. */
21119 name = cp_parser_identifier (parser);
21120 if (name != error_mark_node)
21121 name = build_string (IDENTIFIER_LENGTH (name),
21122 IDENTIFIER_POINTER (name));
21123 /* Look for the closing `]'. */
21124 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21125 }
21126 else
21127 name = NULL_TREE;
21128 /* Look for the string-literal. */
21129 string_literal = cp_parser_string_literal (parser, false, false);
21130
21131 /* Look for the `('. */
21132 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21133 /* Parse the expression. */
21134 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21135 /* Look for the `)'. */
21136 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21137
21138 if (name == error_mark_node
21139 || string_literal == error_mark_node
21140 || expression == error_mark_node)
21141 invalid_operands = true;
21142
21143 /* Add this operand to the list. */
21144 asm_operands = tree_cons (build_tree_list (name, string_literal),
21145 expression,
21146 asm_operands);
21147 /* If the next token is not a `,', there are no more
21148 operands. */
21149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21150 break;
21151 /* Consume the `,'. */
21152 cp_lexer_consume_token (parser->lexer);
21153 }
21154
21155 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21156 }
21157
21158 /* Parse an asm-clobber-list.
21159
21160 asm-clobber-list:
21161 string-literal
21162 asm-clobber-list , string-literal
21163
21164 Returns a TREE_LIST, indicating the clobbers in the order that they
21165 appeared. The TREE_VALUE of each node is a STRING_CST. */
21166
21167 static tree
21168 cp_parser_asm_clobber_list (cp_parser* parser)
21169 {
21170 tree clobbers = NULL_TREE;
21171
21172 while (true)
21173 {
21174 tree string_literal;
21175
21176 /* Look for the string literal. */
21177 string_literal = cp_parser_string_literal (parser, false, false);
21178 /* Add it to the list. */
21179 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21180 /* If the next token is not a `,', then the list is
21181 complete. */
21182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21183 break;
21184 /* Consume the `,' token. */
21185 cp_lexer_consume_token (parser->lexer);
21186 }
21187
21188 return clobbers;
21189 }
21190
21191 /* Parse an asm-label-list.
21192
21193 asm-label-list:
21194 identifier
21195 asm-label-list , identifier
21196
21197 Returns a TREE_LIST, indicating the labels in the order that they
21198 appeared. The TREE_VALUE of each node is a label. */
21199
21200 static tree
21201 cp_parser_asm_label_list (cp_parser* parser)
21202 {
21203 tree labels = NULL_TREE;
21204
21205 while (true)
21206 {
21207 tree identifier, label, name;
21208
21209 /* Look for the identifier. */
21210 identifier = cp_parser_identifier (parser);
21211 if (!error_operand_p (identifier))
21212 {
21213 label = lookup_label (identifier);
21214 if (TREE_CODE (label) == LABEL_DECL)
21215 {
21216 TREE_USED (label) = 1;
21217 check_goto (label);
21218 name = build_string (IDENTIFIER_LENGTH (identifier),
21219 IDENTIFIER_POINTER (identifier));
21220 labels = tree_cons (name, label, labels);
21221 }
21222 }
21223 /* If the next token is not a `,', then the list is
21224 complete. */
21225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21226 break;
21227 /* Consume the `,' token. */
21228 cp_lexer_consume_token (parser->lexer);
21229 }
21230
21231 return nreverse (labels);
21232 }
21233
21234 /* Return TRUE iff the next tokens in the stream are possibly the
21235 beginning of a GNU extension attribute. */
21236
21237 static bool
21238 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21239 {
21240 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21241 }
21242
21243 /* Return TRUE iff the next tokens in the stream are possibly the
21244 beginning of a standard C++-11 attribute specifier. */
21245
21246 static bool
21247 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21248 {
21249 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21250 }
21251
21252 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21253 beginning of a standard C++-11 attribute specifier. */
21254
21255 static bool
21256 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21257 {
21258 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21259
21260 return (cxx_dialect >= cxx11
21261 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21262 || (token->type == CPP_OPEN_SQUARE
21263 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21264 && token->type == CPP_OPEN_SQUARE)));
21265 }
21266
21267 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21268 beginning of a GNU extension attribute. */
21269
21270 static bool
21271 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21272 {
21273 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21274
21275 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21276 }
21277
21278 /* Return true iff the next tokens can be the beginning of either a
21279 GNU attribute list, or a standard C++11 attribute sequence. */
21280
21281 static bool
21282 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21283 {
21284 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21285 || cp_next_tokens_can_be_std_attribute_p (parser));
21286 }
21287
21288 /* Return true iff the next Nth tokens can be the beginning of either
21289 a GNU attribute list, or a standard C++11 attribute sequence. */
21290
21291 static bool
21292 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21293 {
21294 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21295 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21296 }
21297
21298 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21299 of GNU attributes, or return NULL. */
21300
21301 static tree
21302 cp_parser_attributes_opt (cp_parser *parser)
21303 {
21304 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21305 return cp_parser_gnu_attributes_opt (parser);
21306 return cp_parser_std_attribute_spec_seq (parser);
21307 }
21308
21309 /* Parse an (optional) series of attributes.
21310
21311 attributes:
21312 attributes attribute
21313
21314 attribute:
21315 __attribute__ (( attribute-list [opt] ))
21316
21317 The return value is as for cp_parser_gnu_attribute_list. */
21318
21319 static tree
21320 cp_parser_gnu_attributes_opt (cp_parser* parser)
21321 {
21322 tree attributes = NULL_TREE;
21323
21324 while (true)
21325 {
21326 cp_token *token;
21327 tree attribute_list;
21328 bool ok = true;
21329
21330 /* Peek at the next token. */
21331 token = cp_lexer_peek_token (parser->lexer);
21332 /* If it's not `__attribute__', then we're done. */
21333 if (token->keyword != RID_ATTRIBUTE)
21334 break;
21335
21336 /* Consume the `__attribute__' keyword. */
21337 cp_lexer_consume_token (parser->lexer);
21338 /* Look for the two `(' tokens. */
21339 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21340 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21341
21342 /* Peek at the next token. */
21343 token = cp_lexer_peek_token (parser->lexer);
21344 if (token->type != CPP_CLOSE_PAREN)
21345 /* Parse the attribute-list. */
21346 attribute_list = cp_parser_gnu_attribute_list (parser);
21347 else
21348 /* If the next token is a `)', then there is no attribute
21349 list. */
21350 attribute_list = NULL;
21351
21352 /* Look for the two `)' tokens. */
21353 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21354 ok = false;
21355 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21356 ok = false;
21357 if (!ok)
21358 cp_parser_skip_to_end_of_statement (parser);
21359
21360 /* Add these new attributes to the list. */
21361 attributes = chainon (attributes, attribute_list);
21362 }
21363
21364 return attributes;
21365 }
21366
21367 /* Parse a GNU attribute-list.
21368
21369 attribute-list:
21370 attribute
21371 attribute-list , attribute
21372
21373 attribute:
21374 identifier
21375 identifier ( identifier )
21376 identifier ( identifier , expression-list )
21377 identifier ( expression-list )
21378
21379 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21380 to an attribute. The TREE_PURPOSE of each node is the identifier
21381 indicating which attribute is in use. The TREE_VALUE represents
21382 the arguments, if any. */
21383
21384 static tree
21385 cp_parser_gnu_attribute_list (cp_parser* parser)
21386 {
21387 tree attribute_list = NULL_TREE;
21388 bool save_translate_strings_p = parser->translate_strings_p;
21389
21390 parser->translate_strings_p = false;
21391 while (true)
21392 {
21393 cp_token *token;
21394 tree identifier;
21395 tree attribute;
21396
21397 /* Look for the identifier. We also allow keywords here; for
21398 example `__attribute__ ((const))' is legal. */
21399 token = cp_lexer_peek_token (parser->lexer);
21400 if (token->type == CPP_NAME
21401 || token->type == CPP_KEYWORD)
21402 {
21403 tree arguments = NULL_TREE;
21404
21405 /* Consume the token. */
21406 token = cp_lexer_consume_token (parser->lexer);
21407
21408 /* Save away the identifier that indicates which attribute
21409 this is. */
21410 identifier = (token->type == CPP_KEYWORD)
21411 /* For keywords, use the canonical spelling, not the
21412 parsed identifier. */
21413 ? ridpointers[(int) token->keyword]
21414 : token->u.value;
21415
21416 attribute = build_tree_list (identifier, NULL_TREE);
21417
21418 /* Peek at the next token. */
21419 token = cp_lexer_peek_token (parser->lexer);
21420 /* If it's an `(', then parse the attribute arguments. */
21421 if (token->type == CPP_OPEN_PAREN)
21422 {
21423 vec<tree, va_gc> *vec;
21424 int attr_flag = (attribute_takes_identifier_p (identifier)
21425 ? id_attr : normal_attr);
21426 vec = cp_parser_parenthesized_expression_list
21427 (parser, attr_flag, /*cast_p=*/false,
21428 /*allow_expansion_p=*/false,
21429 /*non_constant_p=*/NULL);
21430 if (vec == NULL)
21431 arguments = error_mark_node;
21432 else
21433 {
21434 arguments = build_tree_list_vec (vec);
21435 release_tree_vector (vec);
21436 }
21437 /* Save the arguments away. */
21438 TREE_VALUE (attribute) = arguments;
21439 }
21440
21441 if (arguments != error_mark_node)
21442 {
21443 /* Add this attribute to the list. */
21444 TREE_CHAIN (attribute) = attribute_list;
21445 attribute_list = attribute;
21446 }
21447
21448 token = cp_lexer_peek_token (parser->lexer);
21449 }
21450 /* Now, look for more attributes. If the next token isn't a
21451 `,', we're done. */
21452 if (token->type != CPP_COMMA)
21453 break;
21454
21455 /* Consume the comma and keep going. */
21456 cp_lexer_consume_token (parser->lexer);
21457 }
21458 parser->translate_strings_p = save_translate_strings_p;
21459
21460 /* We built up the list in reverse order. */
21461 return nreverse (attribute_list);
21462 }
21463
21464 /* Parse a standard C++11 attribute.
21465
21466 The returned representation is a TREE_LIST which TREE_PURPOSE is
21467 the scoped name of the attribute, and the TREE_VALUE is its
21468 arguments list.
21469
21470 Note that the scoped name of the attribute is itself a TREE_LIST
21471 which TREE_PURPOSE is the namespace of the attribute, and
21472 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21473 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21474 and which TREE_PURPOSE is directly the attribute name.
21475
21476 Clients of the attribute code should use get_attribute_namespace
21477 and get_attribute_name to get the actual namespace and name of
21478 attributes, regardless of their being GNU or C++11 attributes.
21479
21480 attribute:
21481 attribute-token attribute-argument-clause [opt]
21482
21483 attribute-token:
21484 identifier
21485 attribute-scoped-token
21486
21487 attribute-scoped-token:
21488 attribute-namespace :: identifier
21489
21490 attribute-namespace:
21491 identifier
21492
21493 attribute-argument-clause:
21494 ( balanced-token-seq )
21495
21496 balanced-token-seq:
21497 balanced-token [opt]
21498 balanced-token-seq balanced-token
21499
21500 balanced-token:
21501 ( balanced-token-seq )
21502 [ balanced-token-seq ]
21503 { balanced-token-seq }. */
21504
21505 static tree
21506 cp_parser_std_attribute (cp_parser *parser)
21507 {
21508 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21509 cp_token *token;
21510
21511 /* First, parse name of the the attribute, a.k.a
21512 attribute-token. */
21513
21514 token = cp_lexer_peek_token (parser->lexer);
21515 if (token->type == CPP_NAME)
21516 attr_id = token->u.value;
21517 else if (token->type == CPP_KEYWORD)
21518 attr_id = ridpointers[(int) token->keyword];
21519 else if (token->flags & NAMED_OP)
21520 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21521
21522 if (attr_id == NULL_TREE)
21523 return NULL_TREE;
21524
21525 cp_lexer_consume_token (parser->lexer);
21526
21527 token = cp_lexer_peek_token (parser->lexer);
21528 if (token->type == CPP_SCOPE)
21529 {
21530 /* We are seeing a scoped attribute token. */
21531
21532 cp_lexer_consume_token (parser->lexer);
21533 attr_ns = attr_id;
21534
21535 token = cp_lexer_consume_token (parser->lexer);
21536 if (token->type == CPP_NAME)
21537 attr_id = token->u.value;
21538 else if (token->type == CPP_KEYWORD)
21539 attr_id = ridpointers[(int) token->keyword];
21540 else
21541 {
21542 error_at (token->location,
21543 "expected an identifier for the attribute name");
21544 return error_mark_node;
21545 }
21546 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21547 NULL_TREE);
21548 token = cp_lexer_peek_token (parser->lexer);
21549 }
21550 else
21551 {
21552 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21553 NULL_TREE);
21554 /* C++11 noreturn attribute is equivalent to GNU's. */
21555 if (is_attribute_p ("noreturn", attr_id))
21556 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21557 /* C++14 deprecated attribute is equivalent to GNU's. */
21558 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21559 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21560 }
21561
21562 /* Now parse the optional argument clause of the attribute. */
21563
21564 if (token->type != CPP_OPEN_PAREN)
21565 return attribute;
21566
21567 {
21568 vec<tree, va_gc> *vec;
21569 int attr_flag = normal_attr;
21570
21571 if (attr_ns == get_identifier ("gnu")
21572 && attribute_takes_identifier_p (attr_id))
21573 /* A GNU attribute that takes an identifier in parameter. */
21574 attr_flag = id_attr;
21575
21576 vec = cp_parser_parenthesized_expression_list
21577 (parser, attr_flag, /*cast_p=*/false,
21578 /*allow_expansion_p=*/true,
21579 /*non_constant_p=*/NULL);
21580 if (vec == NULL)
21581 arguments = error_mark_node;
21582 else
21583 {
21584 arguments = build_tree_list_vec (vec);
21585 release_tree_vector (vec);
21586 }
21587
21588 if (arguments == error_mark_node)
21589 attribute = error_mark_node;
21590 else
21591 TREE_VALUE (attribute) = arguments;
21592 }
21593
21594 return attribute;
21595 }
21596
21597 /* Parse a list of standard C++-11 attributes.
21598
21599 attribute-list:
21600 attribute [opt]
21601 attribute-list , attribute[opt]
21602 attribute ...
21603 attribute-list , attribute ...
21604 */
21605
21606 static tree
21607 cp_parser_std_attribute_list (cp_parser *parser)
21608 {
21609 tree attributes = NULL_TREE, attribute = NULL_TREE;
21610 cp_token *token = NULL;
21611
21612 while (true)
21613 {
21614 attribute = cp_parser_std_attribute (parser);
21615 if (attribute == error_mark_node)
21616 break;
21617 if (attribute != NULL_TREE)
21618 {
21619 TREE_CHAIN (attribute) = attributes;
21620 attributes = attribute;
21621 }
21622 token = cp_lexer_peek_token (parser->lexer);
21623 if (token->type != CPP_COMMA)
21624 break;
21625 cp_lexer_consume_token (parser->lexer);
21626 }
21627 attributes = nreverse (attributes);
21628 return attributes;
21629 }
21630
21631 /* Parse a standard C++-11 attribute specifier.
21632
21633 attribute-specifier:
21634 [ [ attribute-list ] ]
21635 alignment-specifier
21636
21637 alignment-specifier:
21638 alignas ( type-id ... [opt] )
21639 alignas ( alignment-expression ... [opt] ). */
21640
21641 static tree
21642 cp_parser_std_attribute_spec (cp_parser *parser)
21643 {
21644 tree attributes = NULL_TREE;
21645 cp_token *token = cp_lexer_peek_token (parser->lexer);
21646
21647 if (token->type == CPP_OPEN_SQUARE
21648 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21649 {
21650 cp_lexer_consume_token (parser->lexer);
21651 cp_lexer_consume_token (parser->lexer);
21652
21653 attributes = cp_parser_std_attribute_list (parser);
21654
21655 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21656 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21657 cp_parser_skip_to_end_of_statement (parser);
21658 else
21659 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21660 when we are sure that we have actually parsed them. */
21661 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21662 }
21663 else
21664 {
21665 tree alignas_expr;
21666
21667 /* Look for an alignment-specifier. */
21668
21669 token = cp_lexer_peek_token (parser->lexer);
21670
21671 if (token->type != CPP_KEYWORD
21672 || token->keyword != RID_ALIGNAS)
21673 return NULL_TREE;
21674
21675 cp_lexer_consume_token (parser->lexer);
21676 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21677
21678 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21679 {
21680 cp_parser_error (parser, "expected %<(%>");
21681 return error_mark_node;
21682 }
21683
21684 cp_parser_parse_tentatively (parser);
21685 alignas_expr = cp_parser_type_id (parser);
21686
21687 if (!cp_parser_parse_definitely (parser))
21688 {
21689 gcc_assert (alignas_expr == error_mark_node
21690 || alignas_expr == NULL_TREE);
21691
21692 alignas_expr =
21693 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21694 /**cp_id_kind=*/NULL);
21695 if (alignas_expr == error_mark_node)
21696 cp_parser_skip_to_end_of_statement (parser);
21697 if (alignas_expr == NULL_TREE
21698 || alignas_expr == error_mark_node)
21699 return alignas_expr;
21700 }
21701
21702 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
21703 {
21704 cp_parser_error (parser, "expected %<)%>");
21705 return error_mark_node;
21706 }
21707
21708 alignas_expr = cxx_alignas_expr (alignas_expr);
21709
21710 /* Build the C++-11 representation of an 'aligned'
21711 attribute. */
21712 attributes =
21713 build_tree_list (build_tree_list (get_identifier ("gnu"),
21714 get_identifier ("aligned")),
21715 build_tree_list (NULL_TREE, alignas_expr));
21716 }
21717
21718 return attributes;
21719 }
21720
21721 /* Parse a standard C++-11 attribute-specifier-seq.
21722
21723 attribute-specifier-seq:
21724 attribute-specifier-seq [opt] attribute-specifier
21725 */
21726
21727 static tree
21728 cp_parser_std_attribute_spec_seq (cp_parser *parser)
21729 {
21730 tree attr_specs = NULL;
21731
21732 while (true)
21733 {
21734 tree attr_spec = cp_parser_std_attribute_spec (parser);
21735 if (attr_spec == NULL_TREE)
21736 break;
21737 if (attr_spec == error_mark_node)
21738 return error_mark_node;
21739
21740 TREE_CHAIN (attr_spec) = attr_specs;
21741 attr_specs = attr_spec;
21742 }
21743
21744 attr_specs = nreverse (attr_specs);
21745 return attr_specs;
21746 }
21747
21748 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21749 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21750 current value of the PEDANTIC flag, regardless of whether or not
21751 the `__extension__' keyword is present. The caller is responsible
21752 for restoring the value of the PEDANTIC flag. */
21753
21754 static bool
21755 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21756 {
21757 /* Save the old value of the PEDANTIC flag. */
21758 *saved_pedantic = pedantic;
21759
21760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21761 {
21762 /* Consume the `__extension__' token. */
21763 cp_lexer_consume_token (parser->lexer);
21764 /* We're not being pedantic while the `__extension__' keyword is
21765 in effect. */
21766 pedantic = 0;
21767
21768 return true;
21769 }
21770
21771 return false;
21772 }
21773
21774 /* Parse a label declaration.
21775
21776 label-declaration:
21777 __label__ label-declarator-seq ;
21778
21779 label-declarator-seq:
21780 identifier , label-declarator-seq
21781 identifier */
21782
21783 static void
21784 cp_parser_label_declaration (cp_parser* parser)
21785 {
21786 /* Look for the `__label__' keyword. */
21787 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21788
21789 while (true)
21790 {
21791 tree identifier;
21792
21793 /* Look for an identifier. */
21794 identifier = cp_parser_identifier (parser);
21795 /* If we failed, stop. */
21796 if (identifier == error_mark_node)
21797 break;
21798 /* Declare it as a label. */
21799 finish_label_decl (identifier);
21800 /* If the next token is a `;', stop. */
21801 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21802 break;
21803 /* Look for the `,' separating the label declarations. */
21804 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21805 }
21806
21807 /* Look for the final `;'. */
21808 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21809 }
21810
21811 /* Support Functions */
21812
21813 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21814 NAME should have one of the representations used for an
21815 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21816 is returned. If PARSER->SCOPE is a dependent type, then a
21817 SCOPE_REF is returned.
21818
21819 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21820 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21821 was formed. Abstractly, such entities should not be passed to this
21822 function, because they do not need to be looked up, but it is
21823 simpler to check for this special case here, rather than at the
21824 call-sites.
21825
21826 In cases not explicitly covered above, this function returns a
21827 DECL, OVERLOAD, or baselink representing the result of the lookup.
21828 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21829 is returned.
21830
21831 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21832 (e.g., "struct") that was used. In that case bindings that do not
21833 refer to types are ignored.
21834
21835 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21836 ignored.
21837
21838 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21839 are ignored.
21840
21841 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21842 types.
21843
21844 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21845 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21846 NULL_TREE otherwise. */
21847
21848 static tree
21849 cp_parser_lookup_name (cp_parser *parser, tree name,
21850 enum tag_types tag_type,
21851 bool is_template,
21852 bool is_namespace,
21853 bool check_dependency,
21854 tree *ambiguous_decls,
21855 location_t name_location)
21856 {
21857 tree decl;
21858 tree object_type = parser->context->object_type;
21859
21860 /* Assume that the lookup will be unambiguous. */
21861 if (ambiguous_decls)
21862 *ambiguous_decls = NULL_TREE;
21863
21864 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21865 no longer valid. Note that if we are parsing tentatively, and
21866 the parse fails, OBJECT_TYPE will be automatically restored. */
21867 parser->context->object_type = NULL_TREE;
21868
21869 if (name == error_mark_node)
21870 return error_mark_node;
21871
21872 /* A template-id has already been resolved; there is no lookup to
21873 do. */
21874 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21875 return name;
21876 if (BASELINK_P (name))
21877 {
21878 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21879 == TEMPLATE_ID_EXPR);
21880 return name;
21881 }
21882
21883 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21884 it should already have been checked to make sure that the name
21885 used matches the type being destroyed. */
21886 if (TREE_CODE (name) == BIT_NOT_EXPR)
21887 {
21888 tree type;
21889
21890 /* Figure out to which type this destructor applies. */
21891 if (parser->scope)
21892 type = parser->scope;
21893 else if (object_type)
21894 type = object_type;
21895 else
21896 type = current_class_type;
21897 /* If that's not a class type, there is no destructor. */
21898 if (!type || !CLASS_TYPE_P (type))
21899 return error_mark_node;
21900 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21901 lazily_declare_fn (sfk_destructor, type);
21902 if (!CLASSTYPE_DESTRUCTORS (type))
21903 return error_mark_node;
21904 /* If it was a class type, return the destructor. */
21905 return CLASSTYPE_DESTRUCTORS (type);
21906 }
21907
21908 /* By this point, the NAME should be an ordinary identifier. If
21909 the id-expression was a qualified name, the qualifying scope is
21910 stored in PARSER->SCOPE at this point. */
21911 gcc_assert (identifier_p (name));
21912
21913 /* Perform the lookup. */
21914 if (parser->scope)
21915 {
21916 bool dependent_p;
21917
21918 if (parser->scope == error_mark_node)
21919 return error_mark_node;
21920
21921 /* If the SCOPE is dependent, the lookup must be deferred until
21922 the template is instantiated -- unless we are explicitly
21923 looking up names in uninstantiated templates. Even then, we
21924 cannot look up the name if the scope is not a class type; it
21925 might, for example, be a template type parameter. */
21926 dependent_p = (TYPE_P (parser->scope)
21927 && dependent_scope_p (parser->scope));
21928 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21929 && dependent_p)
21930 /* Defer lookup. */
21931 decl = error_mark_node;
21932 else
21933 {
21934 tree pushed_scope = NULL_TREE;
21935
21936 /* If PARSER->SCOPE is a dependent type, then it must be a
21937 class type, and we must not be checking dependencies;
21938 otherwise, we would have processed this lookup above. So
21939 that PARSER->SCOPE is not considered a dependent base by
21940 lookup_member, we must enter the scope here. */
21941 if (dependent_p)
21942 pushed_scope = push_scope (parser->scope);
21943
21944 /* If the PARSER->SCOPE is a template specialization, it
21945 may be instantiated during name lookup. In that case,
21946 errors may be issued. Even if we rollback the current
21947 tentative parse, those errors are valid. */
21948 decl = lookup_qualified_name (parser->scope, name,
21949 tag_type != none_type,
21950 /*complain=*/true);
21951
21952 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21953 lookup result and the nested-name-specifier nominates a class C:
21954 * if the name specified after the nested-name-specifier, when
21955 looked up in C, is the injected-class-name of C (Clause 9), or
21956 * if the name specified after the nested-name-specifier is the
21957 same as the identifier or the simple-template-id's template-
21958 name in the last component of the nested-name-specifier,
21959 the name is instead considered to name the constructor of
21960 class C. [ Note: for example, the constructor is not an
21961 acceptable lookup result in an elaborated-type-specifier so
21962 the constructor would not be used in place of the
21963 injected-class-name. --end note ] Such a constructor name
21964 shall be used only in the declarator-id of a declaration that
21965 names a constructor or in a using-declaration. */
21966 if (tag_type == none_type
21967 && DECL_SELF_REFERENCE_P (decl)
21968 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21969 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21970 tag_type != none_type,
21971 /*complain=*/true);
21972
21973 /* If we have a single function from a using decl, pull it out. */
21974 if (TREE_CODE (decl) == OVERLOAD
21975 && !really_overloaded_fn (decl))
21976 decl = OVL_FUNCTION (decl);
21977
21978 if (pushed_scope)
21979 pop_scope (pushed_scope);
21980 }
21981
21982 /* If the scope is a dependent type and either we deferred lookup or
21983 we did lookup but didn't find the name, rememeber the name. */
21984 if (decl == error_mark_node && TYPE_P (parser->scope)
21985 && dependent_type_p (parser->scope))
21986 {
21987 if (tag_type)
21988 {
21989 tree type;
21990
21991 /* The resolution to Core Issue 180 says that `struct
21992 A::B' should be considered a type-name, even if `A'
21993 is dependent. */
21994 type = make_typename_type (parser->scope, name, tag_type,
21995 /*complain=*/tf_error);
21996 if (type != error_mark_node)
21997 decl = TYPE_NAME (type);
21998 }
21999 else if (is_template
22000 && (cp_parser_next_token_ends_template_argument_p (parser)
22001 || cp_lexer_next_token_is (parser->lexer,
22002 CPP_CLOSE_PAREN)))
22003 decl = make_unbound_class_template (parser->scope,
22004 name, NULL_TREE,
22005 /*complain=*/tf_error);
22006 else
22007 decl = build_qualified_name (/*type=*/NULL_TREE,
22008 parser->scope, name,
22009 is_template);
22010 }
22011 parser->qualifying_scope = parser->scope;
22012 parser->object_scope = NULL_TREE;
22013 }
22014 else if (object_type)
22015 {
22016 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22017 OBJECT_TYPE is not a class. */
22018 if (CLASS_TYPE_P (object_type))
22019 /* If the OBJECT_TYPE is a template specialization, it may
22020 be instantiated during name lookup. In that case, errors
22021 may be issued. Even if we rollback the current tentative
22022 parse, those errors are valid. */
22023 decl = lookup_member (object_type,
22024 name,
22025 /*protect=*/0,
22026 tag_type != none_type,
22027 tf_warning_or_error);
22028 else
22029 decl = NULL_TREE;
22030
22031 if (!decl)
22032 /* Look it up in the enclosing context. */
22033 decl = lookup_name_real (name, tag_type != none_type,
22034 /*nonclass=*/0,
22035 /*block_p=*/true, is_namespace, 0);
22036 parser->object_scope = object_type;
22037 parser->qualifying_scope = NULL_TREE;
22038 }
22039 else
22040 {
22041 decl = lookup_name_real (name, tag_type != none_type,
22042 /*nonclass=*/0,
22043 /*block_p=*/true, is_namespace, 0);
22044 parser->qualifying_scope = NULL_TREE;
22045 parser->object_scope = NULL_TREE;
22046 }
22047
22048 /* If the lookup failed, let our caller know. */
22049 if (!decl || decl == error_mark_node)
22050 return error_mark_node;
22051
22052 /* Pull out the template from an injected-class-name (or multiple). */
22053 if (is_template)
22054 decl = maybe_get_template_decl_from_type_decl (decl);
22055
22056 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22057 if (TREE_CODE (decl) == TREE_LIST)
22058 {
22059 if (ambiguous_decls)
22060 *ambiguous_decls = decl;
22061 /* The error message we have to print is too complicated for
22062 cp_parser_error, so we incorporate its actions directly. */
22063 if (!cp_parser_simulate_error (parser))
22064 {
22065 error_at (name_location, "reference to %qD is ambiguous",
22066 name);
22067 print_candidates (decl);
22068 }
22069 return error_mark_node;
22070 }
22071
22072 gcc_assert (DECL_P (decl)
22073 || TREE_CODE (decl) == OVERLOAD
22074 || TREE_CODE (decl) == SCOPE_REF
22075 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22076 || BASELINK_P (decl));
22077
22078 /* If we have resolved the name of a member declaration, check to
22079 see if the declaration is accessible. When the name resolves to
22080 set of overloaded functions, accessibility is checked when
22081 overload resolution is done.
22082
22083 During an explicit instantiation, access is not checked at all,
22084 as per [temp.explicit]. */
22085 if (DECL_P (decl))
22086 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22087
22088 maybe_record_typedef_use (decl);
22089
22090 return decl;
22091 }
22092
22093 /* Like cp_parser_lookup_name, but for use in the typical case where
22094 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22095 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22096
22097 static tree
22098 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22099 {
22100 return cp_parser_lookup_name (parser, name,
22101 none_type,
22102 /*is_template=*/false,
22103 /*is_namespace=*/false,
22104 /*check_dependency=*/true,
22105 /*ambiguous_decls=*/NULL,
22106 location);
22107 }
22108
22109 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22110 the current context, return the TYPE_DECL. If TAG_NAME_P is
22111 true, the DECL indicates the class being defined in a class-head,
22112 or declared in an elaborated-type-specifier.
22113
22114 Otherwise, return DECL. */
22115
22116 static tree
22117 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22118 {
22119 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22120 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22121
22122 struct A {
22123 template <typename T> struct B;
22124 };
22125
22126 template <typename T> struct A::B {};
22127
22128 Similarly, in an elaborated-type-specifier:
22129
22130 namespace N { struct X{}; }
22131
22132 struct A {
22133 template <typename T> friend struct N::X;
22134 };
22135
22136 However, if the DECL refers to a class type, and we are in
22137 the scope of the class, then the name lookup automatically
22138 finds the TYPE_DECL created by build_self_reference rather
22139 than a TEMPLATE_DECL. For example, in:
22140
22141 template <class T> struct S {
22142 S s;
22143 };
22144
22145 there is no need to handle such case. */
22146
22147 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22148 return DECL_TEMPLATE_RESULT (decl);
22149
22150 return decl;
22151 }
22152
22153 /* If too many, or too few, template-parameter lists apply to the
22154 declarator, issue an error message. Returns TRUE if all went well,
22155 and FALSE otherwise. */
22156
22157 static bool
22158 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22159 cp_declarator *declarator,
22160 location_t declarator_location)
22161 {
22162 switch (declarator->kind)
22163 {
22164 case cdk_id:
22165 {
22166 unsigned num_templates = 0;
22167 tree scope = declarator->u.id.qualifying_scope;
22168
22169 if (scope)
22170 num_templates = num_template_headers_for_class (scope);
22171 else if (TREE_CODE (declarator->u.id.unqualified_name)
22172 == TEMPLATE_ID_EXPR)
22173 /* If the DECLARATOR has the form `X<y>' then it uses one
22174 additional level of template parameters. */
22175 ++num_templates;
22176
22177 return cp_parser_check_template_parameters
22178 (parser, num_templates, declarator_location, declarator);
22179 }
22180
22181 case cdk_function:
22182 case cdk_array:
22183 case cdk_pointer:
22184 case cdk_reference:
22185 case cdk_ptrmem:
22186 return (cp_parser_check_declarator_template_parameters
22187 (parser, declarator->declarator, declarator_location));
22188
22189 case cdk_error:
22190 return true;
22191
22192 default:
22193 gcc_unreachable ();
22194 }
22195 return false;
22196 }
22197
22198 /* NUM_TEMPLATES were used in the current declaration. If that is
22199 invalid, return FALSE and issue an error messages. Otherwise,
22200 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22201 declarator and we can print more accurate diagnostics. */
22202
22203 static bool
22204 cp_parser_check_template_parameters (cp_parser* parser,
22205 unsigned num_templates,
22206 location_t location,
22207 cp_declarator *declarator)
22208 {
22209 /* If there are the same number of template classes and parameter
22210 lists, that's OK. */
22211 if (parser->num_template_parameter_lists == num_templates)
22212 return true;
22213 /* If there are more, but only one more, then we are referring to a
22214 member template. That's OK too. */
22215 if (parser->num_template_parameter_lists == num_templates + 1)
22216 return true;
22217 /* If there are more template classes than parameter lists, we have
22218 something like:
22219
22220 template <class T> void S<T>::R<T>::f (); */
22221 if (parser->num_template_parameter_lists < num_templates)
22222 {
22223 if (declarator && !current_function_decl)
22224 error_at (location, "specializing member %<%T::%E%> "
22225 "requires %<template<>%> syntax",
22226 declarator->u.id.qualifying_scope,
22227 declarator->u.id.unqualified_name);
22228 else if (declarator)
22229 error_at (location, "invalid declaration of %<%T::%E%>",
22230 declarator->u.id.qualifying_scope,
22231 declarator->u.id.unqualified_name);
22232 else
22233 error_at (location, "too few template-parameter-lists");
22234 return false;
22235 }
22236 /* Otherwise, there are too many template parameter lists. We have
22237 something like:
22238
22239 template <class T> template <class U> void S::f(); */
22240 error_at (location, "too many template-parameter-lists");
22241 return false;
22242 }
22243
22244 /* Parse an optional `::' token indicating that the following name is
22245 from the global namespace. If so, PARSER->SCOPE is set to the
22246 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22247 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22248 Returns the new value of PARSER->SCOPE, if the `::' token is
22249 present, and NULL_TREE otherwise. */
22250
22251 static tree
22252 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22253 {
22254 cp_token *token;
22255
22256 /* Peek at the next token. */
22257 token = cp_lexer_peek_token (parser->lexer);
22258 /* If we're looking at a `::' token then we're starting from the
22259 global namespace, not our current location. */
22260 if (token->type == CPP_SCOPE)
22261 {
22262 /* Consume the `::' token. */
22263 cp_lexer_consume_token (parser->lexer);
22264 /* Set the SCOPE so that we know where to start the lookup. */
22265 parser->scope = global_namespace;
22266 parser->qualifying_scope = global_namespace;
22267 parser->object_scope = NULL_TREE;
22268
22269 return parser->scope;
22270 }
22271 else if (!current_scope_valid_p)
22272 {
22273 parser->scope = NULL_TREE;
22274 parser->qualifying_scope = NULL_TREE;
22275 parser->object_scope = NULL_TREE;
22276 }
22277
22278 return NULL_TREE;
22279 }
22280
22281 /* Returns TRUE if the upcoming token sequence is the start of a
22282 constructor declarator. If FRIEND_P is true, the declarator is
22283 preceded by the `friend' specifier. */
22284
22285 static bool
22286 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22287 {
22288 bool constructor_p;
22289 bool outside_class_specifier_p;
22290 tree nested_name_specifier;
22291 cp_token *next_token;
22292
22293 /* The common case is that this is not a constructor declarator, so
22294 try to avoid doing lots of work if at all possible. It's not
22295 valid declare a constructor at function scope. */
22296 if (parser->in_function_body)
22297 return false;
22298 /* And only certain tokens can begin a constructor declarator. */
22299 next_token = cp_lexer_peek_token (parser->lexer);
22300 if (next_token->type != CPP_NAME
22301 && next_token->type != CPP_SCOPE
22302 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22303 && next_token->type != CPP_TEMPLATE_ID)
22304 return false;
22305
22306 /* Parse tentatively; we are going to roll back all of the tokens
22307 consumed here. */
22308 cp_parser_parse_tentatively (parser);
22309 /* Assume that we are looking at a constructor declarator. */
22310 constructor_p = true;
22311
22312 /* Look for the optional `::' operator. */
22313 cp_parser_global_scope_opt (parser,
22314 /*current_scope_valid_p=*/false);
22315 /* Look for the nested-name-specifier. */
22316 nested_name_specifier
22317 = (cp_parser_nested_name_specifier_opt (parser,
22318 /*typename_keyword_p=*/false,
22319 /*check_dependency_p=*/false,
22320 /*type_p=*/false,
22321 /*is_declaration=*/false));
22322
22323 outside_class_specifier_p = (!at_class_scope_p ()
22324 || !TYPE_BEING_DEFINED (current_class_type)
22325 || friend_p);
22326
22327 /* Outside of a class-specifier, there must be a
22328 nested-name-specifier. */
22329 if (!nested_name_specifier && outside_class_specifier_p)
22330 constructor_p = false;
22331 else if (nested_name_specifier == error_mark_node)
22332 constructor_p = false;
22333
22334 /* If we have a class scope, this is easy; DR 147 says that S::S always
22335 names the constructor, and no other qualified name could. */
22336 if (constructor_p && nested_name_specifier
22337 && CLASS_TYPE_P (nested_name_specifier))
22338 {
22339 tree id = cp_parser_unqualified_id (parser,
22340 /*template_keyword_p=*/false,
22341 /*check_dependency_p=*/false,
22342 /*declarator_p=*/true,
22343 /*optional_p=*/false);
22344 if (is_overloaded_fn (id))
22345 id = DECL_NAME (get_first_fn (id));
22346 if (!constructor_name_p (id, nested_name_specifier))
22347 constructor_p = false;
22348 }
22349 /* If we still think that this might be a constructor-declarator,
22350 look for a class-name. */
22351 else if (constructor_p)
22352 {
22353 /* If we have:
22354
22355 template <typename T> struct S {
22356 S();
22357 };
22358
22359 we must recognize that the nested `S' names a class. */
22360 tree type_decl;
22361 type_decl = cp_parser_class_name (parser,
22362 /*typename_keyword_p=*/false,
22363 /*template_keyword_p=*/false,
22364 none_type,
22365 /*check_dependency_p=*/false,
22366 /*class_head_p=*/false,
22367 /*is_declaration=*/false);
22368 /* If there was no class-name, then this is not a constructor.
22369 Otherwise, if we are in a class-specifier and we aren't
22370 handling a friend declaration, check that its type matches
22371 current_class_type (c++/38313). Note: error_mark_node
22372 is left alone for error recovery purposes. */
22373 constructor_p = (!cp_parser_error_occurred (parser)
22374 && (outside_class_specifier_p
22375 || type_decl == error_mark_node
22376 || same_type_p (current_class_type,
22377 TREE_TYPE (type_decl))));
22378
22379 /* If we're still considering a constructor, we have to see a `(',
22380 to begin the parameter-declaration-clause, followed by either a
22381 `)', an `...', or a decl-specifier. We need to check for a
22382 type-specifier to avoid being fooled into thinking that:
22383
22384 S (f) (int);
22385
22386 is a constructor. (It is actually a function named `f' that
22387 takes one parameter (of type `int') and returns a value of type
22388 `S'. */
22389 if (constructor_p
22390 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22391 constructor_p = false;
22392
22393 if (constructor_p
22394 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22395 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22396 /* A parameter declaration begins with a decl-specifier,
22397 which is either the "attribute" keyword, a storage class
22398 specifier, or (usually) a type-specifier. */
22399 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22400 {
22401 tree type;
22402 tree pushed_scope = NULL_TREE;
22403 unsigned saved_num_template_parameter_lists;
22404
22405 /* Names appearing in the type-specifier should be looked up
22406 in the scope of the class. */
22407 if (current_class_type)
22408 type = NULL_TREE;
22409 else
22410 {
22411 type = TREE_TYPE (type_decl);
22412 if (TREE_CODE (type) == TYPENAME_TYPE)
22413 {
22414 type = resolve_typename_type (type,
22415 /*only_current_p=*/false);
22416 if (TREE_CODE (type) == TYPENAME_TYPE)
22417 {
22418 cp_parser_abort_tentative_parse (parser);
22419 return false;
22420 }
22421 }
22422 pushed_scope = push_scope (type);
22423 }
22424
22425 /* Inside the constructor parameter list, surrounding
22426 template-parameter-lists do not apply. */
22427 saved_num_template_parameter_lists
22428 = parser->num_template_parameter_lists;
22429 parser->num_template_parameter_lists = 0;
22430
22431 /* Look for the type-specifier. */
22432 cp_parser_type_specifier (parser,
22433 CP_PARSER_FLAGS_NONE,
22434 /*decl_specs=*/NULL,
22435 /*is_declarator=*/true,
22436 /*declares_class_or_enum=*/NULL,
22437 /*is_cv_qualifier=*/NULL);
22438
22439 parser->num_template_parameter_lists
22440 = saved_num_template_parameter_lists;
22441
22442 /* Leave the scope of the class. */
22443 if (pushed_scope)
22444 pop_scope (pushed_scope);
22445
22446 constructor_p = !cp_parser_error_occurred (parser);
22447 }
22448 }
22449
22450 /* We did not really want to consume any tokens. */
22451 cp_parser_abort_tentative_parse (parser);
22452
22453 return constructor_p;
22454 }
22455
22456 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22457 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22458 they must be performed once we are in the scope of the function.
22459
22460 Returns the function defined. */
22461
22462 static tree
22463 cp_parser_function_definition_from_specifiers_and_declarator
22464 (cp_parser* parser,
22465 cp_decl_specifier_seq *decl_specifiers,
22466 tree attributes,
22467 const cp_declarator *declarator)
22468 {
22469 tree fn;
22470 bool success_p;
22471
22472 /* Begin the function-definition. */
22473 success_p = start_function (decl_specifiers, declarator, attributes);
22474
22475 /* The things we're about to see are not directly qualified by any
22476 template headers we've seen thus far. */
22477 reset_specialization ();
22478
22479 /* If there were names looked up in the decl-specifier-seq that we
22480 did not check, check them now. We must wait until we are in the
22481 scope of the function to perform the checks, since the function
22482 might be a friend. */
22483 perform_deferred_access_checks (tf_warning_or_error);
22484
22485 if (success_p)
22486 {
22487 cp_finalize_omp_declare_simd (parser, current_function_decl);
22488 parser->omp_declare_simd = NULL;
22489 }
22490
22491 if (!success_p)
22492 {
22493 /* Skip the entire function. */
22494 cp_parser_skip_to_end_of_block_or_statement (parser);
22495 fn = error_mark_node;
22496 }
22497 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22498 {
22499 /* Seen already, skip it. An error message has already been output. */
22500 cp_parser_skip_to_end_of_block_or_statement (parser);
22501 fn = current_function_decl;
22502 current_function_decl = NULL_TREE;
22503 /* If this is a function from a class, pop the nested class. */
22504 if (current_class_name)
22505 pop_nested_class ();
22506 }
22507 else
22508 {
22509 timevar_id_t tv;
22510 if (DECL_DECLARED_INLINE_P (current_function_decl))
22511 tv = TV_PARSE_INLINE;
22512 else
22513 tv = TV_PARSE_FUNC;
22514 timevar_push (tv);
22515 fn = cp_parser_function_definition_after_declarator (parser,
22516 /*inline_p=*/false);
22517 timevar_pop (tv);
22518 }
22519
22520 return fn;
22521 }
22522
22523 /* Parse the part of a function-definition that follows the
22524 declarator. INLINE_P is TRUE iff this function is an inline
22525 function defined within a class-specifier.
22526
22527 Returns the function defined. */
22528
22529 static tree
22530 cp_parser_function_definition_after_declarator (cp_parser* parser,
22531 bool inline_p)
22532 {
22533 tree fn;
22534 bool ctor_initializer_p = false;
22535 bool saved_in_unbraced_linkage_specification_p;
22536 bool saved_in_function_body;
22537 unsigned saved_num_template_parameter_lists;
22538 cp_token *token;
22539 bool fully_implicit_function_template_p
22540 = parser->fully_implicit_function_template_p;
22541 parser->fully_implicit_function_template_p = false;
22542 tree implicit_template_parms
22543 = parser->implicit_template_parms;
22544 parser->implicit_template_parms = 0;
22545 cp_binding_level* implicit_template_scope
22546 = parser->implicit_template_scope;
22547 parser->implicit_template_scope = 0;
22548
22549 saved_in_function_body = parser->in_function_body;
22550 parser->in_function_body = true;
22551 /* If the next token is `return', then the code may be trying to
22552 make use of the "named return value" extension that G++ used to
22553 support. */
22554 token = cp_lexer_peek_token (parser->lexer);
22555 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22556 {
22557 /* Consume the `return' keyword. */
22558 cp_lexer_consume_token (parser->lexer);
22559 /* Look for the identifier that indicates what value is to be
22560 returned. */
22561 cp_parser_identifier (parser);
22562 /* Issue an error message. */
22563 error_at (token->location,
22564 "named return values are no longer supported");
22565 /* Skip tokens until we reach the start of the function body. */
22566 while (true)
22567 {
22568 cp_token *token = cp_lexer_peek_token (parser->lexer);
22569 if (token->type == CPP_OPEN_BRACE
22570 || token->type == CPP_EOF
22571 || token->type == CPP_PRAGMA_EOL)
22572 break;
22573 cp_lexer_consume_token (parser->lexer);
22574 }
22575 }
22576 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22577 anything declared inside `f'. */
22578 saved_in_unbraced_linkage_specification_p
22579 = parser->in_unbraced_linkage_specification_p;
22580 parser->in_unbraced_linkage_specification_p = false;
22581 /* Inside the function, surrounding template-parameter-lists do not
22582 apply. */
22583 saved_num_template_parameter_lists
22584 = parser->num_template_parameter_lists;
22585 parser->num_template_parameter_lists = 0;
22586
22587 start_lambda_scope (current_function_decl);
22588
22589 /* If the next token is `try', `__transaction_atomic', or
22590 `__transaction_relaxed`, then we are looking at either function-try-block
22591 or function-transaction-block. Note that all of these include the
22592 function-body. */
22593 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22594 ctor_initializer_p = cp_parser_function_transaction (parser,
22595 RID_TRANSACTION_ATOMIC);
22596 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22597 RID_TRANSACTION_RELAXED))
22598 ctor_initializer_p = cp_parser_function_transaction (parser,
22599 RID_TRANSACTION_RELAXED);
22600 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22601 ctor_initializer_p = cp_parser_function_try_block (parser);
22602 else
22603 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22604 (parser, /*in_function_try_block=*/false);
22605
22606 finish_lambda_scope ();
22607
22608 /* Finish the function. */
22609 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22610 (inline_p ? 2 : 0));
22611 /* Generate code for it, if necessary. */
22612 expand_or_defer_fn (fn);
22613 /* Restore the saved values. */
22614 parser->in_unbraced_linkage_specification_p
22615 = saved_in_unbraced_linkage_specification_p;
22616 parser->num_template_parameter_lists
22617 = saved_num_template_parameter_lists;
22618 parser->in_function_body = saved_in_function_body;
22619
22620 parser->fully_implicit_function_template_p
22621 = fully_implicit_function_template_p;
22622 parser->implicit_template_parms
22623 = implicit_template_parms;
22624 parser->implicit_template_scope
22625 = implicit_template_scope;
22626
22627 if (parser->fully_implicit_function_template_p)
22628 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22629
22630 return fn;
22631 }
22632
22633 /* Parse a template-declaration, assuming that the `export' (and
22634 `extern') keywords, if present, has already been scanned. MEMBER_P
22635 is as for cp_parser_template_declaration. */
22636
22637 static void
22638 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22639 {
22640 tree decl = NULL_TREE;
22641 vec<deferred_access_check, va_gc> *checks;
22642 tree parameter_list;
22643 bool friend_p = false;
22644 bool need_lang_pop;
22645 cp_token *token;
22646
22647 /* Look for the `template' keyword. */
22648 token = cp_lexer_peek_token (parser->lexer);
22649 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22650 return;
22651
22652 /* And the `<'. */
22653 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22654 return;
22655 if (at_class_scope_p () && current_function_decl)
22656 {
22657 /* 14.5.2.2 [temp.mem]
22658
22659 A local class shall not have member templates. */
22660 error_at (token->location,
22661 "invalid declaration of member template in local class");
22662 cp_parser_skip_to_end_of_block_or_statement (parser);
22663 return;
22664 }
22665 /* [temp]
22666
22667 A template ... shall not have C linkage. */
22668 if (current_lang_name == lang_name_c)
22669 {
22670 error_at (token->location, "template with C linkage");
22671 /* Give it C++ linkage to avoid confusing other parts of the
22672 front end. */
22673 push_lang_context (lang_name_cplusplus);
22674 need_lang_pop = true;
22675 }
22676 else
22677 need_lang_pop = false;
22678
22679 /* We cannot perform access checks on the template parameter
22680 declarations until we know what is being declared, just as we
22681 cannot check the decl-specifier list. */
22682 push_deferring_access_checks (dk_deferred);
22683
22684 /* If the next token is `>', then we have an invalid
22685 specialization. Rather than complain about an invalid template
22686 parameter, issue an error message here. */
22687 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
22688 {
22689 cp_parser_error (parser, "invalid explicit specialization");
22690 begin_specialization ();
22691 parameter_list = NULL_TREE;
22692 }
22693 else
22694 {
22695 /* Parse the template parameters. */
22696 parameter_list = cp_parser_template_parameter_list (parser);
22697 }
22698
22699 /* Get the deferred access checks from the parameter list. These
22700 will be checked once we know what is being declared, as for a
22701 member template the checks must be performed in the scope of the
22702 class containing the member. */
22703 checks = get_deferred_access_checks ();
22704
22705 /* Look for the `>'. */
22706 cp_parser_skip_to_end_of_template_parameter_list (parser);
22707 /* We just processed one more parameter list. */
22708 ++parser->num_template_parameter_lists;
22709 /* If the next token is `template', there are more template
22710 parameters. */
22711 if (cp_lexer_next_token_is_keyword (parser->lexer,
22712 RID_TEMPLATE))
22713 cp_parser_template_declaration_after_export (parser, member_p);
22714 else if (cxx_dialect >= cxx11
22715 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22716 decl = cp_parser_alias_declaration (parser);
22717 else
22718 {
22719 /* There are no access checks when parsing a template, as we do not
22720 know if a specialization will be a friend. */
22721 push_deferring_access_checks (dk_no_check);
22722 token = cp_lexer_peek_token (parser->lexer);
22723 decl = cp_parser_single_declaration (parser,
22724 checks,
22725 member_p,
22726 /*explicit_specialization_p=*/false,
22727 &friend_p);
22728 pop_deferring_access_checks ();
22729
22730 /* If this is a member template declaration, let the front
22731 end know. */
22732 if (member_p && !friend_p && decl)
22733 {
22734 if (TREE_CODE (decl) == TYPE_DECL)
22735 cp_parser_check_access_in_redeclaration (decl, token->location);
22736
22737 decl = finish_member_template_decl (decl);
22738 }
22739 else if (friend_p && decl
22740 && DECL_DECLARES_TYPE_P (decl))
22741 make_friend_class (current_class_type, TREE_TYPE (decl),
22742 /*complain=*/true);
22743 }
22744 /* We are done with the current parameter list. */
22745 --parser->num_template_parameter_lists;
22746
22747 pop_deferring_access_checks ();
22748
22749 /* Finish up. */
22750 finish_template_decl (parameter_list);
22751
22752 /* Check the template arguments for a literal operator template. */
22753 if (decl
22754 && DECL_DECLARES_FUNCTION_P (decl)
22755 && UDLIT_OPER_P (DECL_NAME (decl)))
22756 {
22757 bool ok = true;
22758 if (parameter_list == NULL_TREE)
22759 ok = false;
22760 else
22761 {
22762 int num_parms = TREE_VEC_LENGTH (parameter_list);
22763 if (num_parms == 1)
22764 {
22765 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
22766 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22767 if (TREE_TYPE (parm) != char_type_node
22768 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22769 ok = false;
22770 }
22771 else if (num_parms == 2 && cxx_dialect >= cxx1y)
22772 {
22773 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
22774 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
22775 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
22776 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22777 if (TREE_TYPE (parm) != TREE_TYPE (type)
22778 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22779 ok = false;
22780 }
22781 else
22782 ok = false;
22783 }
22784 if (!ok)
22785 error ("literal operator template %qD has invalid parameter list."
22786 " Expected non-type template argument pack <char...>"
22787 " or <typename CharT, CharT...>",
22788 decl);
22789 }
22790 /* Register member declarations. */
22791 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22792 finish_member_declaration (decl);
22793 /* For the erroneous case of a template with C linkage, we pushed an
22794 implicit C++ linkage scope; exit that scope now. */
22795 if (need_lang_pop)
22796 pop_lang_context ();
22797 /* If DECL is a function template, we must return to parse it later.
22798 (Even though there is no definition, there might be default
22799 arguments that need handling.) */
22800 if (member_p && decl
22801 && DECL_DECLARES_FUNCTION_P (decl))
22802 vec_safe_push (unparsed_funs_with_definitions, decl);
22803 }
22804
22805 /* Perform the deferred access checks from a template-parameter-list.
22806 CHECKS is a TREE_LIST of access checks, as returned by
22807 get_deferred_access_checks. */
22808
22809 static void
22810 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22811 {
22812 ++processing_template_parmlist;
22813 perform_access_checks (checks, tf_warning_or_error);
22814 --processing_template_parmlist;
22815 }
22816
22817 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22818 `function-definition' sequence that follows a template header.
22819 If MEMBER_P is true, this declaration appears in a class scope.
22820
22821 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22822 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22823
22824 static tree
22825 cp_parser_single_declaration (cp_parser* parser,
22826 vec<deferred_access_check, va_gc> *checks,
22827 bool member_p,
22828 bool explicit_specialization_p,
22829 bool* friend_p)
22830 {
22831 int declares_class_or_enum;
22832 tree decl = NULL_TREE;
22833 cp_decl_specifier_seq decl_specifiers;
22834 bool function_definition_p = false;
22835 cp_token *decl_spec_token_start;
22836
22837 /* This function is only used when processing a template
22838 declaration. */
22839 gcc_assert (innermost_scope_kind () == sk_template_parms
22840 || innermost_scope_kind () == sk_template_spec);
22841
22842 /* Defer access checks until we know what is being declared. */
22843 push_deferring_access_checks (dk_deferred);
22844
22845 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22846 alternative. */
22847 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22848 cp_parser_decl_specifier_seq (parser,
22849 CP_PARSER_FLAGS_OPTIONAL,
22850 &decl_specifiers,
22851 &declares_class_or_enum);
22852 if (friend_p)
22853 *friend_p = cp_parser_friend_p (&decl_specifiers);
22854
22855 /* There are no template typedefs. */
22856 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22857 {
22858 error_at (decl_spec_token_start->location,
22859 "template declaration of %<typedef%>");
22860 decl = error_mark_node;
22861 }
22862
22863 /* Gather up the access checks that occurred the
22864 decl-specifier-seq. */
22865 stop_deferring_access_checks ();
22866
22867 /* Check for the declaration of a template class. */
22868 if (declares_class_or_enum)
22869 {
22870 if (cp_parser_declares_only_class_p (parser))
22871 {
22872 decl = shadow_tag (&decl_specifiers);
22873
22874 /* In this case:
22875
22876 struct C {
22877 friend template <typename T> struct A<T>::B;
22878 };
22879
22880 A<T>::B will be represented by a TYPENAME_TYPE, and
22881 therefore not recognized by shadow_tag. */
22882 if (friend_p && *friend_p
22883 && !decl
22884 && decl_specifiers.type
22885 && TYPE_P (decl_specifiers.type))
22886 decl = decl_specifiers.type;
22887
22888 if (decl && decl != error_mark_node)
22889 decl = TYPE_NAME (decl);
22890 else
22891 decl = error_mark_node;
22892
22893 /* Perform access checks for template parameters. */
22894 cp_parser_perform_template_parameter_access_checks (checks);
22895 }
22896 }
22897
22898 /* Complain about missing 'typename' or other invalid type names. */
22899 if (!decl_specifiers.any_type_specifiers_p
22900 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22901 {
22902 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22903 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22904 the rest of this declaration. */
22905 decl = error_mark_node;
22906 goto out;
22907 }
22908
22909 /* If it's not a template class, try for a template function. If
22910 the next token is a `;', then this declaration does not declare
22911 anything. But, if there were errors in the decl-specifiers, then
22912 the error might well have come from an attempted class-specifier.
22913 In that case, there's no need to warn about a missing declarator. */
22914 if (!decl
22915 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22916 || decl_specifiers.type != error_mark_node))
22917 {
22918 decl = cp_parser_init_declarator (parser,
22919 &decl_specifiers,
22920 checks,
22921 /*function_definition_allowed_p=*/true,
22922 member_p,
22923 declares_class_or_enum,
22924 &function_definition_p,
22925 NULL);
22926
22927 /* 7.1.1-1 [dcl.stc]
22928
22929 A storage-class-specifier shall not be specified in an explicit
22930 specialization... */
22931 if (decl
22932 && explicit_specialization_p
22933 && decl_specifiers.storage_class != sc_none)
22934 {
22935 error_at (decl_spec_token_start->location,
22936 "explicit template specialization cannot have a storage class");
22937 decl = error_mark_node;
22938 }
22939
22940 if (decl && VAR_P (decl))
22941 check_template_variable (decl);
22942 }
22943
22944 /* Look for a trailing `;' after the declaration. */
22945 if (!function_definition_p
22946 && (decl == error_mark_node
22947 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22948 cp_parser_skip_to_end_of_block_or_statement (parser);
22949
22950 out:
22951 pop_deferring_access_checks ();
22952
22953 /* Clear any current qualification; whatever comes next is the start
22954 of something new. */
22955 parser->scope = NULL_TREE;
22956 parser->qualifying_scope = NULL_TREE;
22957 parser->object_scope = NULL_TREE;
22958
22959 return decl;
22960 }
22961
22962 /* Parse a cast-expression that is not the operand of a unary "&". */
22963
22964 static tree
22965 cp_parser_simple_cast_expression (cp_parser *parser)
22966 {
22967 return cp_parser_cast_expression (parser, /*address_p=*/false,
22968 /*cast_p=*/false, /*decltype*/false, NULL);
22969 }
22970
22971 /* Parse a functional cast to TYPE. Returns an expression
22972 representing the cast. */
22973
22974 static tree
22975 cp_parser_functional_cast (cp_parser* parser, tree type)
22976 {
22977 vec<tree, va_gc> *vec;
22978 tree expression_list;
22979 tree cast;
22980 bool nonconst_p;
22981
22982 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22983 {
22984 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22985 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22986 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22987 if (TREE_CODE (type) == TYPE_DECL)
22988 type = TREE_TYPE (type);
22989 return finish_compound_literal (type, expression_list,
22990 tf_warning_or_error);
22991 }
22992
22993
22994 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22995 /*cast_p=*/true,
22996 /*allow_expansion_p=*/true,
22997 /*non_constant_p=*/NULL);
22998 if (vec == NULL)
22999 expression_list = error_mark_node;
23000 else
23001 {
23002 expression_list = build_tree_list_vec (vec);
23003 release_tree_vector (vec);
23004 }
23005
23006 cast = build_functional_cast (type, expression_list,
23007 tf_warning_or_error);
23008 /* [expr.const]/1: In an integral constant expression "only type
23009 conversions to integral or enumeration type can be used". */
23010 if (TREE_CODE (type) == TYPE_DECL)
23011 type = TREE_TYPE (type);
23012 if (cast != error_mark_node
23013 && !cast_valid_in_integral_constant_expression_p (type)
23014 && cp_parser_non_integral_constant_expression (parser,
23015 NIC_CONSTRUCTOR))
23016 return error_mark_node;
23017 return cast;
23018 }
23019
23020 /* Save the tokens that make up the body of a member function defined
23021 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23022 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23023 specifiers applied to the declaration. Returns the FUNCTION_DECL
23024 for the member function. */
23025
23026 static tree
23027 cp_parser_save_member_function_body (cp_parser* parser,
23028 cp_decl_specifier_seq *decl_specifiers,
23029 cp_declarator *declarator,
23030 tree attributes)
23031 {
23032 cp_token *first;
23033 cp_token *last;
23034 tree fn;
23035
23036 /* Create the FUNCTION_DECL. */
23037 fn = grokmethod (decl_specifiers, declarator, attributes);
23038 cp_finalize_omp_declare_simd (parser, fn);
23039 /* If something went badly wrong, bail out now. */
23040 if (fn == error_mark_node)
23041 {
23042 /* If there's a function-body, skip it. */
23043 if (cp_parser_token_starts_function_definition_p
23044 (cp_lexer_peek_token (parser->lexer)))
23045 cp_parser_skip_to_end_of_block_or_statement (parser);
23046 return error_mark_node;
23047 }
23048
23049 /* Remember it, if there default args to post process. */
23050 cp_parser_save_default_args (parser, fn);
23051
23052 /* Save away the tokens that make up the body of the
23053 function. */
23054 first = parser->lexer->next_token;
23055 /* Handle function try blocks. */
23056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23057 cp_lexer_consume_token (parser->lexer);
23058 /* We can have braced-init-list mem-initializers before the fn body. */
23059 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23060 {
23061 cp_lexer_consume_token (parser->lexer);
23062 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23063 {
23064 /* cache_group will stop after an un-nested { } pair, too. */
23065 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23066 break;
23067
23068 /* variadic mem-inits have ... after the ')'. */
23069 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23070 cp_lexer_consume_token (parser->lexer);
23071 }
23072 }
23073 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23074 /* Handle function try blocks. */
23075 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23076 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23077 last = parser->lexer->next_token;
23078
23079 /* Save away the inline definition; we will process it when the
23080 class is complete. */
23081 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23082 DECL_PENDING_INLINE_P (fn) = 1;
23083
23084 /* We need to know that this was defined in the class, so that
23085 friend templates are handled correctly. */
23086 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23087
23088 /* Add FN to the queue of functions to be parsed later. */
23089 vec_safe_push (unparsed_funs_with_definitions, fn);
23090
23091 return fn;
23092 }
23093
23094 /* Save the tokens that make up the in-class initializer for a non-static
23095 data member. Returns a DEFAULT_ARG. */
23096
23097 static tree
23098 cp_parser_save_nsdmi (cp_parser* parser)
23099 {
23100 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23101 }
23102
23103 /* Parse a template-argument-list, as well as the trailing ">" (but
23104 not the opening "<"). See cp_parser_template_argument_list for the
23105 return value. */
23106
23107 static tree
23108 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23109 {
23110 tree arguments;
23111 tree saved_scope;
23112 tree saved_qualifying_scope;
23113 tree saved_object_scope;
23114 bool saved_greater_than_is_operator_p;
23115 int saved_unevaluated_operand;
23116 int saved_inhibit_evaluation_warnings;
23117
23118 /* [temp.names]
23119
23120 When parsing a template-id, the first non-nested `>' is taken as
23121 the end of the template-argument-list rather than a greater-than
23122 operator. */
23123 saved_greater_than_is_operator_p
23124 = parser->greater_than_is_operator_p;
23125 parser->greater_than_is_operator_p = false;
23126 /* Parsing the argument list may modify SCOPE, so we save it
23127 here. */
23128 saved_scope = parser->scope;
23129 saved_qualifying_scope = parser->qualifying_scope;
23130 saved_object_scope = parser->object_scope;
23131 /* We need to evaluate the template arguments, even though this
23132 template-id may be nested within a "sizeof". */
23133 saved_unevaluated_operand = cp_unevaluated_operand;
23134 cp_unevaluated_operand = 0;
23135 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23136 c_inhibit_evaluation_warnings = 0;
23137 /* Parse the template-argument-list itself. */
23138 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23139 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23140 arguments = NULL_TREE;
23141 else
23142 arguments = cp_parser_template_argument_list (parser);
23143 /* Look for the `>' that ends the template-argument-list. If we find
23144 a '>>' instead, it's probably just a typo. */
23145 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23146 {
23147 if (cxx_dialect != cxx98)
23148 {
23149 /* In C++0x, a `>>' in a template argument list or cast
23150 expression is considered to be two separate `>'
23151 tokens. So, change the current token to a `>', but don't
23152 consume it: it will be consumed later when the outer
23153 template argument list (or cast expression) is parsed.
23154 Note that this replacement of `>' for `>>' is necessary
23155 even if we are parsing tentatively: in the tentative
23156 case, after calling
23157 cp_parser_enclosed_template_argument_list we will always
23158 throw away all of the template arguments and the first
23159 closing `>', either because the template argument list
23160 was erroneous or because we are replacing those tokens
23161 with a CPP_TEMPLATE_ID token. The second `>' (which will
23162 not have been thrown away) is needed either to close an
23163 outer template argument list or to complete a new-style
23164 cast. */
23165 cp_token *token = cp_lexer_peek_token (parser->lexer);
23166 token->type = CPP_GREATER;
23167 }
23168 else if (!saved_greater_than_is_operator_p)
23169 {
23170 /* If we're in a nested template argument list, the '>>' has
23171 to be a typo for '> >'. We emit the error message, but we
23172 continue parsing and we push a '>' as next token, so that
23173 the argument list will be parsed correctly. Note that the
23174 global source location is still on the token before the
23175 '>>', so we need to say explicitly where we want it. */
23176 cp_token *token = cp_lexer_peek_token (parser->lexer);
23177 error_at (token->location, "%<>>%> should be %<> >%> "
23178 "within a nested template argument list");
23179
23180 token->type = CPP_GREATER;
23181 }
23182 else
23183 {
23184 /* If this is not a nested template argument list, the '>>'
23185 is a typo for '>'. Emit an error message and continue.
23186 Same deal about the token location, but here we can get it
23187 right by consuming the '>>' before issuing the diagnostic. */
23188 cp_token *token = cp_lexer_consume_token (parser->lexer);
23189 error_at (token->location,
23190 "spurious %<>>%>, use %<>%> to terminate "
23191 "a template argument list");
23192 }
23193 }
23194 else
23195 cp_parser_skip_to_end_of_template_parameter_list (parser);
23196 /* The `>' token might be a greater-than operator again now. */
23197 parser->greater_than_is_operator_p
23198 = saved_greater_than_is_operator_p;
23199 /* Restore the SAVED_SCOPE. */
23200 parser->scope = saved_scope;
23201 parser->qualifying_scope = saved_qualifying_scope;
23202 parser->object_scope = saved_object_scope;
23203 cp_unevaluated_operand = saved_unevaluated_operand;
23204 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23205
23206 return arguments;
23207 }
23208
23209 /* MEMBER_FUNCTION is a member function, or a friend. If default
23210 arguments, or the body of the function have not yet been parsed,
23211 parse them now. */
23212
23213 static void
23214 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23215 {
23216 timevar_push (TV_PARSE_INMETH);
23217 /* If this member is a template, get the underlying
23218 FUNCTION_DECL. */
23219 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23220 member_function = DECL_TEMPLATE_RESULT (member_function);
23221
23222 /* There should not be any class definitions in progress at this
23223 point; the bodies of members are only parsed outside of all class
23224 definitions. */
23225 gcc_assert (parser->num_classes_being_defined == 0);
23226 /* While we're parsing the member functions we might encounter more
23227 classes. We want to handle them right away, but we don't want
23228 them getting mixed up with functions that are currently in the
23229 queue. */
23230 push_unparsed_function_queues (parser);
23231
23232 /* Make sure that any template parameters are in scope. */
23233 maybe_begin_member_template_processing (member_function);
23234
23235 /* If the body of the function has not yet been parsed, parse it
23236 now. */
23237 if (DECL_PENDING_INLINE_P (member_function))
23238 {
23239 tree function_scope;
23240 cp_token_cache *tokens;
23241
23242 /* The function is no longer pending; we are processing it. */
23243 tokens = DECL_PENDING_INLINE_INFO (member_function);
23244 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23245 DECL_PENDING_INLINE_P (member_function) = 0;
23246
23247 /* If this is a local class, enter the scope of the containing
23248 function. */
23249 function_scope = current_function_decl;
23250 if (function_scope)
23251 push_function_context ();
23252
23253 /* Push the body of the function onto the lexer stack. */
23254 cp_parser_push_lexer_for_tokens (parser, tokens);
23255
23256 /* Let the front end know that we going to be defining this
23257 function. */
23258 start_preparsed_function (member_function, NULL_TREE,
23259 SF_PRE_PARSED | SF_INCLASS_INLINE);
23260
23261 /* Don't do access checking if it is a templated function. */
23262 if (processing_template_decl)
23263 push_deferring_access_checks (dk_no_check);
23264
23265 /* #pragma omp declare reduction needs special parsing. */
23266 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23267 {
23268 parser->lexer->in_pragma = true;
23269 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23270 finish_function (0);
23271 cp_check_omp_declare_reduction (member_function);
23272 }
23273 else
23274 /* Now, parse the body of the function. */
23275 cp_parser_function_definition_after_declarator (parser,
23276 /*inline_p=*/true);
23277
23278 if (processing_template_decl)
23279 pop_deferring_access_checks ();
23280
23281 /* Leave the scope of the containing function. */
23282 if (function_scope)
23283 pop_function_context ();
23284 cp_parser_pop_lexer (parser);
23285 }
23286
23287 /* Remove any template parameters from the symbol table. */
23288 maybe_end_member_template_processing ();
23289
23290 /* Restore the queue. */
23291 pop_unparsed_function_queues (parser);
23292 timevar_pop (TV_PARSE_INMETH);
23293 }
23294
23295 /* If DECL contains any default args, remember it on the unparsed
23296 functions queue. */
23297
23298 static void
23299 cp_parser_save_default_args (cp_parser* parser, tree decl)
23300 {
23301 tree probe;
23302
23303 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23304 probe;
23305 probe = TREE_CHAIN (probe))
23306 if (TREE_PURPOSE (probe))
23307 {
23308 cp_default_arg_entry entry = {current_class_type, decl};
23309 vec_safe_push (unparsed_funs_with_default_args, entry);
23310 break;
23311 }
23312 }
23313
23314 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23315 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23316 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23317 from the parameter-type-list. */
23318
23319 static tree
23320 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23321 tree default_arg, tree parmtype)
23322 {
23323 cp_token_cache *tokens;
23324 tree parsed_arg;
23325 bool dummy;
23326
23327 if (default_arg == error_mark_node)
23328 return error_mark_node;
23329
23330 /* Push the saved tokens for the default argument onto the parser's
23331 lexer stack. */
23332 tokens = DEFARG_TOKENS (default_arg);
23333 cp_parser_push_lexer_for_tokens (parser, tokens);
23334
23335 start_lambda_scope (decl);
23336
23337 /* Parse the default argument. */
23338 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23339 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23340 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23341
23342 finish_lambda_scope ();
23343
23344 if (parsed_arg == error_mark_node)
23345 cp_parser_skip_to_end_of_statement (parser);
23346
23347 if (!processing_template_decl)
23348 {
23349 /* In a non-template class, check conversions now. In a template,
23350 we'll wait and instantiate these as needed. */
23351 if (TREE_CODE (decl) == PARM_DECL)
23352 parsed_arg = check_default_argument (parmtype, parsed_arg,
23353 tf_warning_or_error);
23354 else
23355 {
23356 int flags = LOOKUP_IMPLICIT;
23357 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
23358 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
23359 flags = LOOKUP_NORMAL;
23360 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
23361 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
23362 /* This represents the whole initialization. */
23363 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
23364 }
23365 }
23366
23367 /* If the token stream has not been completely used up, then
23368 there was extra junk after the end of the default
23369 argument. */
23370 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23371 {
23372 if (TREE_CODE (decl) == PARM_DECL)
23373 cp_parser_error (parser, "expected %<,%>");
23374 else
23375 cp_parser_error (parser, "expected %<;%>");
23376 }
23377
23378 /* Revert to the main lexer. */
23379 cp_parser_pop_lexer (parser);
23380
23381 return parsed_arg;
23382 }
23383
23384 /* FIELD is a non-static data member with an initializer which we saved for
23385 later; parse it now. */
23386
23387 static void
23388 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23389 {
23390 tree def;
23391
23392 maybe_begin_member_template_processing (field);
23393
23394 push_unparsed_function_queues (parser);
23395 def = cp_parser_late_parse_one_default_arg (parser, field,
23396 DECL_INITIAL (field),
23397 NULL_TREE);
23398 pop_unparsed_function_queues (parser);
23399
23400 maybe_end_member_template_processing ();
23401
23402 DECL_INITIAL (field) = def;
23403 }
23404
23405 /* FN is a FUNCTION_DECL which may contains a parameter with an
23406 unparsed DEFAULT_ARG. Parse the default args now. This function
23407 assumes that the current scope is the scope in which the default
23408 argument should be processed. */
23409
23410 static void
23411 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23412 {
23413 bool saved_local_variables_forbidden_p;
23414 tree parm, parmdecl;
23415
23416 /* While we're parsing the default args, we might (due to the
23417 statement expression extension) encounter more classes. We want
23418 to handle them right away, but we don't want them getting mixed
23419 up with default args that are currently in the queue. */
23420 push_unparsed_function_queues (parser);
23421
23422 /* Local variable names (and the `this' keyword) may not appear
23423 in a default argument. */
23424 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23425 parser->local_variables_forbidden_p = true;
23426
23427 push_defarg_context (fn);
23428
23429 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23430 parmdecl = DECL_ARGUMENTS (fn);
23431 parm && parm != void_list_node;
23432 parm = TREE_CHAIN (parm),
23433 parmdecl = DECL_CHAIN (parmdecl))
23434 {
23435 tree default_arg = TREE_PURPOSE (parm);
23436 tree parsed_arg;
23437 vec<tree, va_gc> *insts;
23438 tree copy;
23439 unsigned ix;
23440
23441 if (!default_arg)
23442 continue;
23443
23444 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23445 /* This can happen for a friend declaration for a function
23446 already declared with default arguments. */
23447 continue;
23448
23449 parsed_arg
23450 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23451 default_arg,
23452 TREE_VALUE (parm));
23453 if (parsed_arg == error_mark_node)
23454 {
23455 continue;
23456 }
23457
23458 TREE_PURPOSE (parm) = parsed_arg;
23459
23460 /* Update any instantiations we've already created. */
23461 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23462 vec_safe_iterate (insts, ix, &copy); ix++)
23463 TREE_PURPOSE (copy) = parsed_arg;
23464 }
23465
23466 pop_defarg_context ();
23467
23468 /* Make sure no default arg is missing. */
23469 check_default_args (fn);
23470
23471 /* Restore the state of local_variables_forbidden_p. */
23472 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23473
23474 /* Restore the queue. */
23475 pop_unparsed_function_queues (parser);
23476 }
23477
23478 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23479
23480 sizeof ... ( identifier )
23481
23482 where the 'sizeof' token has already been consumed. */
23483
23484 static tree
23485 cp_parser_sizeof_pack (cp_parser *parser)
23486 {
23487 /* Consume the `...'. */
23488 cp_lexer_consume_token (parser->lexer);
23489 maybe_warn_variadic_templates ();
23490
23491 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23492 if (paren)
23493 cp_lexer_consume_token (parser->lexer);
23494 else
23495 permerror (cp_lexer_peek_token (parser->lexer)->location,
23496 "%<sizeof...%> argument must be surrounded by parentheses");
23497
23498 cp_token *token = cp_lexer_peek_token (parser->lexer);
23499 tree name = cp_parser_identifier (parser);
23500 if (name == error_mark_node)
23501 return error_mark_node;
23502 /* The name is not qualified. */
23503 parser->scope = NULL_TREE;
23504 parser->qualifying_scope = NULL_TREE;
23505 parser->object_scope = NULL_TREE;
23506 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23507 if (expr == error_mark_node)
23508 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23509 token->location);
23510 if (TREE_CODE (expr) == TYPE_DECL)
23511 expr = TREE_TYPE (expr);
23512 else if (TREE_CODE (expr) == CONST_DECL)
23513 expr = DECL_INITIAL (expr);
23514 expr = make_pack_expansion (expr);
23515
23516 if (paren)
23517 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23518
23519 return expr;
23520 }
23521
23522 /* Parse the operand of `sizeof' (or a similar operator). Returns
23523 either a TYPE or an expression, depending on the form of the
23524 input. The KEYWORD indicates which kind of expression we have
23525 encountered. */
23526
23527 static tree
23528 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23529 {
23530 tree expr = NULL_TREE;
23531 const char *saved_message;
23532 char *tmp;
23533 bool saved_integral_constant_expression_p;
23534 bool saved_non_integral_constant_expression_p;
23535
23536 /* If it's a `...', then we are computing the length of a parameter
23537 pack. */
23538 if (keyword == RID_SIZEOF
23539 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23540 return cp_parser_sizeof_pack (parser);
23541
23542 /* Types cannot be defined in a `sizeof' expression. Save away the
23543 old message. */
23544 saved_message = parser->type_definition_forbidden_message;
23545 /* And create the new one. */
23546 tmp = concat ("types may not be defined in %<",
23547 IDENTIFIER_POINTER (ridpointers[keyword]),
23548 "%> expressions", NULL);
23549 parser->type_definition_forbidden_message = tmp;
23550
23551 /* The restrictions on constant-expressions do not apply inside
23552 sizeof expressions. */
23553 saved_integral_constant_expression_p
23554 = parser->integral_constant_expression_p;
23555 saved_non_integral_constant_expression_p
23556 = parser->non_integral_constant_expression_p;
23557 parser->integral_constant_expression_p = false;
23558
23559 /* Do not actually evaluate the expression. */
23560 ++cp_unevaluated_operand;
23561 ++c_inhibit_evaluation_warnings;
23562 /* If it's a `(', then we might be looking at the type-id
23563 construction. */
23564 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23565 {
23566 tree type = NULL_TREE;
23567 bool compound_literal_p;
23568
23569 /* We can't be sure yet whether we're looking at a type-id or an
23570 expression. */
23571 cp_parser_parse_tentatively (parser);
23572 /* Consume the `('. */
23573 cp_lexer_consume_token (parser->lexer);
23574 /* Note: as a GNU Extension, compound literals are considered
23575 postfix-expressions as they are in C99, so they are valid
23576 arguments to sizeof. See comment in cp_parser_cast_expression
23577 for details. */
23578 cp_lexer_save_tokens (parser->lexer);
23579 /* Skip tokens until the next token is a closing parenthesis.
23580 If we find the closing `)', and the next token is a `{', then
23581 we are looking at a compound-literal. */
23582 compound_literal_p
23583 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23584 /*consume_paren=*/true)
23585 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23586 /* Roll back the tokens we skipped. */
23587 cp_lexer_rollback_tokens (parser->lexer);
23588 /* If we were looking at a compound-literal, simulate an error
23589 so that the call to cp_parser_parse_definitely below will
23590 fail. */
23591 if (compound_literal_p)
23592 cp_parser_simulate_error (parser);
23593 else
23594 {
23595 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23596 parser->in_type_id_in_expr_p = true;
23597 /* Look for the type-id. */
23598 type = cp_parser_type_id (parser);
23599 /* Look for the closing `)'. */
23600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23601 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23602 }
23603
23604 /* If all went well, then we're done. */
23605 if (cp_parser_parse_definitely (parser))
23606 {
23607 cp_decl_specifier_seq decl_specs;
23608
23609 /* Build a trivial decl-specifier-seq. */
23610 clear_decl_specs (&decl_specs);
23611 decl_specs.type = type;
23612
23613 /* Call grokdeclarator to figure out what type this is. */
23614 expr = grokdeclarator (NULL,
23615 &decl_specs,
23616 TYPENAME,
23617 /*initialized=*/0,
23618 /*attrlist=*/NULL);
23619 }
23620 }
23621
23622 /* If the type-id production did not work out, then we must be
23623 looking at the unary-expression production. */
23624 if (!expr)
23625 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23626 /*cast_p=*/false, NULL);
23627
23628 /* Go back to evaluating expressions. */
23629 --cp_unevaluated_operand;
23630 --c_inhibit_evaluation_warnings;
23631
23632 /* Free the message we created. */
23633 free (tmp);
23634 /* And restore the old one. */
23635 parser->type_definition_forbidden_message = saved_message;
23636 parser->integral_constant_expression_p
23637 = saved_integral_constant_expression_p;
23638 parser->non_integral_constant_expression_p
23639 = saved_non_integral_constant_expression_p;
23640
23641 return expr;
23642 }
23643
23644 /* If the current declaration has no declarator, return true. */
23645
23646 static bool
23647 cp_parser_declares_only_class_p (cp_parser *parser)
23648 {
23649 /* If the next token is a `;' or a `,' then there is no
23650 declarator. */
23651 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23652 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23653 }
23654
23655 /* Update the DECL_SPECS to reflect the storage class indicated by
23656 KEYWORD. */
23657
23658 static void
23659 cp_parser_set_storage_class (cp_parser *parser,
23660 cp_decl_specifier_seq *decl_specs,
23661 enum rid keyword,
23662 cp_token *token)
23663 {
23664 cp_storage_class storage_class;
23665
23666 if (parser->in_unbraced_linkage_specification_p)
23667 {
23668 error_at (token->location, "invalid use of %qD in linkage specification",
23669 ridpointers[keyword]);
23670 return;
23671 }
23672 else if (decl_specs->storage_class != sc_none)
23673 {
23674 decl_specs->conflicting_specifiers_p = true;
23675 return;
23676 }
23677
23678 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
23679 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
23680 && decl_specs->gnu_thread_keyword_p)
23681 {
23682 pedwarn (decl_specs->locations[ds_thread], 0,
23683 "%<__thread%> before %qD", ridpointers[keyword]);
23684 }
23685
23686 switch (keyword)
23687 {
23688 case RID_AUTO:
23689 storage_class = sc_auto;
23690 break;
23691 case RID_REGISTER:
23692 storage_class = sc_register;
23693 break;
23694 case RID_STATIC:
23695 storage_class = sc_static;
23696 break;
23697 case RID_EXTERN:
23698 storage_class = sc_extern;
23699 break;
23700 case RID_MUTABLE:
23701 storage_class = sc_mutable;
23702 break;
23703 default:
23704 gcc_unreachable ();
23705 }
23706 decl_specs->storage_class = storage_class;
23707 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
23708
23709 /* A storage class specifier cannot be applied alongside a typedef
23710 specifier. If there is a typedef specifier present then set
23711 conflicting_specifiers_p which will trigger an error later
23712 on in grokdeclarator. */
23713 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
23714 decl_specs->conflicting_specifiers_p = true;
23715 }
23716
23717 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
23718 is true, the type is a class or enum definition. */
23719
23720 static void
23721 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
23722 tree type_spec,
23723 cp_token *token,
23724 bool type_definition_p)
23725 {
23726 decl_specs->any_specifiers_p = true;
23727
23728 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
23729 (with, for example, in "typedef int wchar_t;") we remember that
23730 this is what happened. In system headers, we ignore these
23731 declarations so that G++ can work with system headers that are not
23732 C++-safe. */
23733 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
23734 && !type_definition_p
23735 && (type_spec == boolean_type_node
23736 || type_spec == char16_type_node
23737 || type_spec == char32_type_node
23738 || type_spec == wchar_type_node)
23739 && (decl_specs->type
23740 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
23741 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
23742 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
23743 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
23744 {
23745 decl_specs->redefined_builtin_type = type_spec;
23746 set_and_check_decl_spec_loc (decl_specs,
23747 ds_redefined_builtin_type_spec,
23748 token);
23749 if (!decl_specs->type)
23750 {
23751 decl_specs->type = type_spec;
23752 decl_specs->type_definition_p = false;
23753 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
23754 }
23755 }
23756 else if (decl_specs->type)
23757 decl_specs->multiple_types_p = true;
23758 else
23759 {
23760 decl_specs->type = type_spec;
23761 decl_specs->type_definition_p = type_definition_p;
23762 decl_specs->redefined_builtin_type = NULL_TREE;
23763 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
23764 }
23765 }
23766
23767 /* True iff TOKEN is the GNU keyword __thread. */
23768
23769 static bool
23770 token_is__thread (cp_token *token)
23771 {
23772 gcc_assert (token->keyword == RID_THREAD);
23773 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
23774 }
23775
23776 /* Set the location for a declarator specifier and check if it is
23777 duplicated.
23778
23779 DECL_SPECS is the sequence of declarator specifiers onto which to
23780 set the location.
23781
23782 DS is the single declarator specifier to set which location is to
23783 be set onto the existing sequence of declarators.
23784
23785 LOCATION is the location for the declarator specifier to
23786 consider. */
23787
23788 static void
23789 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
23790 cp_decl_spec ds, cp_token *token)
23791 {
23792 gcc_assert (ds < ds_last);
23793
23794 if (decl_specs == NULL)
23795 return;
23796
23797 source_location location = token->location;
23798
23799 if (decl_specs->locations[ds] == 0)
23800 {
23801 decl_specs->locations[ds] = location;
23802 if (ds == ds_thread)
23803 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
23804 }
23805 else
23806 {
23807 if (ds == ds_long)
23808 {
23809 if (decl_specs->locations[ds_long_long] != 0)
23810 error_at (location,
23811 "%<long long long%> is too long for GCC");
23812 else
23813 {
23814 decl_specs->locations[ds_long_long] = location;
23815 pedwarn_cxx98 (location,
23816 OPT_Wlong_long,
23817 "ISO C++ 1998 does not support %<long long%>");
23818 }
23819 }
23820 else if (ds == ds_thread)
23821 {
23822 bool gnu = token_is__thread (token);
23823 if (gnu != decl_specs->gnu_thread_keyword_p)
23824 error_at (location,
23825 "both %<__thread%> and %<thread_local%> specified");
23826 else
23827 error_at (location, "duplicate %qD", token->u.value);
23828 }
23829 else
23830 {
23831 static const char *const decl_spec_names[] = {
23832 "signed",
23833 "unsigned",
23834 "short",
23835 "long",
23836 "const",
23837 "volatile",
23838 "restrict",
23839 "inline",
23840 "virtual",
23841 "explicit",
23842 "friend",
23843 "typedef",
23844 "using",
23845 "constexpr",
23846 "__complex"
23847 };
23848 error_at (location,
23849 "duplicate %qs", decl_spec_names[ds]);
23850 }
23851 }
23852 }
23853
23854 /* Return true iff the declarator specifier DS is present in the
23855 sequence of declarator specifiers DECL_SPECS. */
23856
23857 bool
23858 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23859 cp_decl_spec ds)
23860 {
23861 gcc_assert (ds < ds_last);
23862
23863 if (decl_specs == NULL)
23864 return false;
23865
23866 return decl_specs->locations[ds] != 0;
23867 }
23868
23869 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23870 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23871
23872 static bool
23873 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23874 {
23875 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23876 }
23877
23878 /* Issue an error message indicating that TOKEN_DESC was expected.
23879 If KEYWORD is true, it indicated this function is called by
23880 cp_parser_require_keword and the required token can only be
23881 a indicated keyword. */
23882
23883 static void
23884 cp_parser_required_error (cp_parser *parser,
23885 required_token token_desc,
23886 bool keyword)
23887 {
23888 switch (token_desc)
23889 {
23890 case RT_NEW:
23891 cp_parser_error (parser, "expected %<new%>");
23892 return;
23893 case RT_DELETE:
23894 cp_parser_error (parser, "expected %<delete%>");
23895 return;
23896 case RT_RETURN:
23897 cp_parser_error (parser, "expected %<return%>");
23898 return;
23899 case RT_WHILE:
23900 cp_parser_error (parser, "expected %<while%>");
23901 return;
23902 case RT_EXTERN:
23903 cp_parser_error (parser, "expected %<extern%>");
23904 return;
23905 case RT_STATIC_ASSERT:
23906 cp_parser_error (parser, "expected %<static_assert%>");
23907 return;
23908 case RT_DECLTYPE:
23909 cp_parser_error (parser, "expected %<decltype%>");
23910 return;
23911 case RT_OPERATOR:
23912 cp_parser_error (parser, "expected %<operator%>");
23913 return;
23914 case RT_CLASS:
23915 cp_parser_error (parser, "expected %<class%>");
23916 return;
23917 case RT_TEMPLATE:
23918 cp_parser_error (parser, "expected %<template%>");
23919 return;
23920 case RT_NAMESPACE:
23921 cp_parser_error (parser, "expected %<namespace%>");
23922 return;
23923 case RT_USING:
23924 cp_parser_error (parser, "expected %<using%>");
23925 return;
23926 case RT_ASM:
23927 cp_parser_error (parser, "expected %<asm%>");
23928 return;
23929 case RT_TRY:
23930 cp_parser_error (parser, "expected %<try%>");
23931 return;
23932 case RT_CATCH:
23933 cp_parser_error (parser, "expected %<catch%>");
23934 return;
23935 case RT_THROW:
23936 cp_parser_error (parser, "expected %<throw%>");
23937 return;
23938 case RT_LABEL:
23939 cp_parser_error (parser, "expected %<__label__%>");
23940 return;
23941 case RT_AT_TRY:
23942 cp_parser_error (parser, "expected %<@try%>");
23943 return;
23944 case RT_AT_SYNCHRONIZED:
23945 cp_parser_error (parser, "expected %<@synchronized%>");
23946 return;
23947 case RT_AT_THROW:
23948 cp_parser_error (parser, "expected %<@throw%>");
23949 return;
23950 case RT_TRANSACTION_ATOMIC:
23951 cp_parser_error (parser, "expected %<__transaction_atomic%>");
23952 return;
23953 case RT_TRANSACTION_RELAXED:
23954 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23955 return;
23956 default:
23957 break;
23958 }
23959 if (!keyword)
23960 {
23961 switch (token_desc)
23962 {
23963 case RT_SEMICOLON:
23964 cp_parser_error (parser, "expected %<;%>");
23965 return;
23966 case RT_OPEN_PAREN:
23967 cp_parser_error (parser, "expected %<(%>");
23968 return;
23969 case RT_CLOSE_BRACE:
23970 cp_parser_error (parser, "expected %<}%>");
23971 return;
23972 case RT_OPEN_BRACE:
23973 cp_parser_error (parser, "expected %<{%>");
23974 return;
23975 case RT_CLOSE_SQUARE:
23976 cp_parser_error (parser, "expected %<]%>");
23977 return;
23978 case RT_OPEN_SQUARE:
23979 cp_parser_error (parser, "expected %<[%>");
23980 return;
23981 case RT_COMMA:
23982 cp_parser_error (parser, "expected %<,%>");
23983 return;
23984 case RT_SCOPE:
23985 cp_parser_error (parser, "expected %<::%>");
23986 return;
23987 case RT_LESS:
23988 cp_parser_error (parser, "expected %<<%>");
23989 return;
23990 case RT_GREATER:
23991 cp_parser_error (parser, "expected %<>%>");
23992 return;
23993 case RT_EQ:
23994 cp_parser_error (parser, "expected %<=%>");
23995 return;
23996 case RT_ELLIPSIS:
23997 cp_parser_error (parser, "expected %<...%>");
23998 return;
23999 case RT_MULT:
24000 cp_parser_error (parser, "expected %<*%>");
24001 return;
24002 case RT_COMPL:
24003 cp_parser_error (parser, "expected %<~%>");
24004 return;
24005 case RT_COLON:
24006 cp_parser_error (parser, "expected %<:%>");
24007 return;
24008 case RT_COLON_SCOPE:
24009 cp_parser_error (parser, "expected %<:%> or %<::%>");
24010 return;
24011 case RT_CLOSE_PAREN:
24012 cp_parser_error (parser, "expected %<)%>");
24013 return;
24014 case RT_COMMA_CLOSE_PAREN:
24015 cp_parser_error (parser, "expected %<,%> or %<)%>");
24016 return;
24017 case RT_PRAGMA_EOL:
24018 cp_parser_error (parser, "expected end of line");
24019 return;
24020 case RT_NAME:
24021 cp_parser_error (parser, "expected identifier");
24022 return;
24023 case RT_SELECT:
24024 cp_parser_error (parser, "expected selection-statement");
24025 return;
24026 case RT_INTERATION:
24027 cp_parser_error (parser, "expected iteration-statement");
24028 return;
24029 case RT_JUMP:
24030 cp_parser_error (parser, "expected jump-statement");
24031 return;
24032 case RT_CLASS_KEY:
24033 cp_parser_error (parser, "expected class-key");
24034 return;
24035 case RT_CLASS_TYPENAME_TEMPLATE:
24036 cp_parser_error (parser,
24037 "expected %<class%>, %<typename%>, or %<template%>");
24038 return;
24039 default:
24040 gcc_unreachable ();
24041 }
24042 }
24043 else
24044 gcc_unreachable ();
24045 }
24046
24047
24048
24049 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24050 issue an error message indicating that TOKEN_DESC was expected.
24051
24052 Returns the token consumed, if the token had the appropriate type.
24053 Otherwise, returns NULL. */
24054
24055 static cp_token *
24056 cp_parser_require (cp_parser* parser,
24057 enum cpp_ttype type,
24058 required_token token_desc)
24059 {
24060 if (cp_lexer_next_token_is (parser->lexer, type))
24061 return cp_lexer_consume_token (parser->lexer);
24062 else
24063 {
24064 /* Output the MESSAGE -- unless we're parsing tentatively. */
24065 if (!cp_parser_simulate_error (parser))
24066 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24067 return NULL;
24068 }
24069 }
24070
24071 /* An error message is produced if the next token is not '>'.
24072 All further tokens are skipped until the desired token is
24073 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24074
24075 static void
24076 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24077 {
24078 /* Current level of '< ... >'. */
24079 unsigned level = 0;
24080 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24081 unsigned nesting_depth = 0;
24082
24083 /* Are we ready, yet? If not, issue error message. */
24084 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24085 return;
24086
24087 /* Skip tokens until the desired token is found. */
24088 while (true)
24089 {
24090 /* Peek at the next token. */
24091 switch (cp_lexer_peek_token (parser->lexer)->type)
24092 {
24093 case CPP_LESS:
24094 if (!nesting_depth)
24095 ++level;
24096 break;
24097
24098 case CPP_RSHIFT:
24099 if (cxx_dialect == cxx98)
24100 /* C++0x views the `>>' operator as two `>' tokens, but
24101 C++98 does not. */
24102 break;
24103 else if (!nesting_depth && level-- == 0)
24104 {
24105 /* We've hit a `>>' where the first `>' closes the
24106 template argument list, and the second `>' is
24107 spurious. Just consume the `>>' and stop; we've
24108 already produced at least one error. */
24109 cp_lexer_consume_token (parser->lexer);
24110 return;
24111 }
24112 /* Fall through for C++0x, so we handle the second `>' in
24113 the `>>'. */
24114
24115 case CPP_GREATER:
24116 if (!nesting_depth && level-- == 0)
24117 {
24118 /* We've reached the token we want, consume it and stop. */
24119 cp_lexer_consume_token (parser->lexer);
24120 return;
24121 }
24122 break;
24123
24124 case CPP_OPEN_PAREN:
24125 case CPP_OPEN_SQUARE:
24126 ++nesting_depth;
24127 break;
24128
24129 case CPP_CLOSE_PAREN:
24130 case CPP_CLOSE_SQUARE:
24131 if (nesting_depth-- == 0)
24132 return;
24133 break;
24134
24135 case CPP_EOF:
24136 case CPP_PRAGMA_EOL:
24137 case CPP_SEMICOLON:
24138 case CPP_OPEN_BRACE:
24139 case CPP_CLOSE_BRACE:
24140 /* The '>' was probably forgotten, don't look further. */
24141 return;
24142
24143 default:
24144 break;
24145 }
24146
24147 /* Consume this token. */
24148 cp_lexer_consume_token (parser->lexer);
24149 }
24150 }
24151
24152 /* If the next token is the indicated keyword, consume it. Otherwise,
24153 issue an error message indicating that TOKEN_DESC was expected.
24154
24155 Returns the token consumed, if the token had the appropriate type.
24156 Otherwise, returns NULL. */
24157
24158 static cp_token *
24159 cp_parser_require_keyword (cp_parser* parser,
24160 enum rid keyword,
24161 required_token token_desc)
24162 {
24163 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24164
24165 if (token && token->keyword != keyword)
24166 {
24167 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24168 return NULL;
24169 }
24170
24171 return token;
24172 }
24173
24174 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24175 function-definition. */
24176
24177 static bool
24178 cp_parser_token_starts_function_definition_p (cp_token* token)
24179 {
24180 return (/* An ordinary function-body begins with an `{'. */
24181 token->type == CPP_OPEN_BRACE
24182 /* A ctor-initializer begins with a `:'. */
24183 || token->type == CPP_COLON
24184 /* A function-try-block begins with `try'. */
24185 || token->keyword == RID_TRY
24186 /* A function-transaction-block begins with `__transaction_atomic'
24187 or `__transaction_relaxed'. */
24188 || token->keyword == RID_TRANSACTION_ATOMIC
24189 || token->keyword == RID_TRANSACTION_RELAXED
24190 /* The named return value extension begins with `return'. */
24191 || token->keyword == RID_RETURN);
24192 }
24193
24194 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24195 definition. */
24196
24197 static bool
24198 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24199 {
24200 cp_token *token;
24201
24202 token = cp_lexer_peek_token (parser->lexer);
24203 return (token->type == CPP_OPEN_BRACE
24204 || (token->type == CPP_COLON
24205 && !parser->colon_doesnt_start_class_def_p));
24206 }
24207
24208 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24209 C++0x) ending a template-argument. */
24210
24211 static bool
24212 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24213 {
24214 cp_token *token;
24215
24216 token = cp_lexer_peek_token (parser->lexer);
24217 return (token->type == CPP_COMMA
24218 || token->type == CPP_GREATER
24219 || token->type == CPP_ELLIPSIS
24220 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24221 }
24222
24223 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24224 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24225
24226 static bool
24227 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24228 size_t n)
24229 {
24230 cp_token *token;
24231
24232 token = cp_lexer_peek_nth_token (parser->lexer, n);
24233 if (token->type == CPP_LESS)
24234 return true;
24235 /* Check for the sequence `<::' in the original code. It would be lexed as
24236 `[:', where `[' is a digraph, and there is no whitespace before
24237 `:'. */
24238 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24239 {
24240 cp_token *token2;
24241 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24242 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24243 return true;
24244 }
24245 return false;
24246 }
24247
24248 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24249 or none_type otherwise. */
24250
24251 static enum tag_types
24252 cp_parser_token_is_class_key (cp_token* token)
24253 {
24254 switch (token->keyword)
24255 {
24256 case RID_CLASS:
24257 return class_type;
24258 case RID_STRUCT:
24259 return record_type;
24260 case RID_UNION:
24261 return union_type;
24262
24263 default:
24264 return none_type;
24265 }
24266 }
24267
24268 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24269
24270 static void
24271 cp_parser_check_class_key (enum tag_types class_key, tree type)
24272 {
24273 if (type == error_mark_node)
24274 return;
24275 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24276 {
24277 if (permerror (input_location, "%qs tag used in naming %q#T",
24278 class_key == union_type ? "union"
24279 : class_key == record_type ? "struct" : "class",
24280 type))
24281 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24282 "%q#T was previously declared here", type);
24283 }
24284 }
24285
24286 /* Issue an error message if DECL is redeclared with different
24287 access than its original declaration [class.access.spec/3].
24288 This applies to nested classes and nested class templates.
24289 [class.mem/1]. */
24290
24291 static void
24292 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24293 {
24294 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24295 return;
24296
24297 if ((TREE_PRIVATE (decl)
24298 != (current_access_specifier == access_private_node))
24299 || (TREE_PROTECTED (decl)
24300 != (current_access_specifier == access_protected_node)))
24301 error_at (location, "%qD redeclared with different access", decl);
24302 }
24303
24304 /* Look for the `template' keyword, as a syntactic disambiguator.
24305 Return TRUE iff it is present, in which case it will be
24306 consumed. */
24307
24308 static bool
24309 cp_parser_optional_template_keyword (cp_parser *parser)
24310 {
24311 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24312 {
24313 /* In C++98 the `template' keyword can only be used within templates;
24314 outside templates the parser can always figure out what is a
24315 template and what is not. In C++11, per the resolution of DR 468,
24316 `template' is allowed in cases where it is not strictly necessary. */
24317 if (!processing_template_decl
24318 && pedantic && cxx_dialect == cxx98)
24319 {
24320 cp_token *token = cp_lexer_peek_token (parser->lexer);
24321 pedwarn (token->location, OPT_Wpedantic,
24322 "in C++98 %<template%> (as a disambiguator) is only "
24323 "allowed within templates");
24324 /* If this part of the token stream is rescanned, the same
24325 error message would be generated. So, we purge the token
24326 from the stream. */
24327 cp_lexer_purge_token (parser->lexer);
24328 return false;
24329 }
24330 else
24331 {
24332 /* Consume the `template' keyword. */
24333 cp_lexer_consume_token (parser->lexer);
24334 return true;
24335 }
24336 }
24337 return false;
24338 }
24339
24340 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24341 set PARSER->SCOPE, and perform other related actions. */
24342
24343 static void
24344 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24345 {
24346 int i;
24347 struct tree_check *check_value;
24348 deferred_access_check *chk;
24349 vec<deferred_access_check, va_gc> *checks;
24350
24351 /* Get the stored value. */
24352 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24353 /* Perform any access checks that were deferred. */
24354 checks = check_value->checks;
24355 if (checks)
24356 {
24357 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24358 perform_or_defer_access_check (chk->binfo,
24359 chk->decl,
24360 chk->diag_decl, tf_warning_or_error);
24361 }
24362 /* Set the scope from the stored value. */
24363 parser->scope = check_value->value;
24364 parser->qualifying_scope = check_value->qualifying_scope;
24365 parser->object_scope = NULL_TREE;
24366 }
24367
24368 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24369 encounter the end of a block before what we were looking for. */
24370
24371 static bool
24372 cp_parser_cache_group (cp_parser *parser,
24373 enum cpp_ttype end,
24374 unsigned depth)
24375 {
24376 while (true)
24377 {
24378 cp_token *token = cp_lexer_peek_token (parser->lexer);
24379
24380 /* Abort a parenthesized expression if we encounter a semicolon. */
24381 if ((end == CPP_CLOSE_PAREN || depth == 0)
24382 && token->type == CPP_SEMICOLON)
24383 return true;
24384 /* If we've reached the end of the file, stop. */
24385 if (token->type == CPP_EOF
24386 || (end != CPP_PRAGMA_EOL
24387 && token->type == CPP_PRAGMA_EOL))
24388 return true;
24389 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24390 /* We've hit the end of an enclosing block, so there's been some
24391 kind of syntax error. */
24392 return true;
24393
24394 /* Consume the token. */
24395 cp_lexer_consume_token (parser->lexer);
24396 /* See if it starts a new group. */
24397 if (token->type == CPP_OPEN_BRACE)
24398 {
24399 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24400 /* In theory this should probably check end == '}', but
24401 cp_parser_save_member_function_body needs it to exit
24402 after either '}' or ')' when called with ')'. */
24403 if (depth == 0)
24404 return false;
24405 }
24406 else if (token->type == CPP_OPEN_PAREN)
24407 {
24408 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24409 if (depth == 0 && end == CPP_CLOSE_PAREN)
24410 return false;
24411 }
24412 else if (token->type == CPP_PRAGMA)
24413 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24414 else if (token->type == end)
24415 return false;
24416 }
24417 }
24418
24419 /* Like above, for caching a default argument or NSDMI. Both of these are
24420 terminated by a non-nested comma, but it can be unclear whether or not a
24421 comma is nested in a template argument list unless we do more parsing.
24422 In order to handle this ambiguity, when we encounter a ',' after a '<'
24423 we try to parse what follows as a parameter-declaration-list (in the
24424 case of a default argument) or a member-declarator (in the case of an
24425 NSDMI). If that succeeds, then we stop caching. */
24426
24427 static tree
24428 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24429 {
24430 unsigned depth = 0;
24431 int maybe_template_id = 0;
24432 cp_token *first_token;
24433 cp_token *token;
24434 tree default_argument;
24435
24436 /* Add tokens until we have processed the entire default
24437 argument. We add the range [first_token, token). */
24438 first_token = cp_lexer_peek_token (parser->lexer);
24439 if (first_token->type == CPP_OPEN_BRACE)
24440 {
24441 /* For list-initialization, this is straightforward. */
24442 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24443 token = cp_lexer_peek_token (parser->lexer);
24444 }
24445 else while (true)
24446 {
24447 bool done = false;
24448
24449 /* Peek at the next token. */
24450 token = cp_lexer_peek_token (parser->lexer);
24451 /* What we do depends on what token we have. */
24452 switch (token->type)
24453 {
24454 /* In valid code, a default argument must be
24455 immediately followed by a `,' `)', or `...'. */
24456 case CPP_COMMA:
24457 if (depth == 0 && maybe_template_id)
24458 {
24459 /* If we've seen a '<', we might be in a
24460 template-argument-list. Until Core issue 325 is
24461 resolved, we don't know how this situation ought
24462 to be handled, so try to DTRT. We check whether
24463 what comes after the comma is a valid parameter
24464 declaration list. If it is, then the comma ends
24465 the default argument; otherwise the default
24466 argument continues. */
24467 bool error = false;
24468
24469 /* Set ITALP so cp_parser_parameter_declaration_list
24470 doesn't decide to commit to this parse. */
24471 bool saved_italp = parser->in_template_argument_list_p;
24472 parser->in_template_argument_list_p = true;
24473
24474 cp_parser_parse_tentatively (parser);
24475 cp_lexer_consume_token (parser->lexer);
24476
24477 if (nsdmi)
24478 {
24479 int ctor_dtor_or_conv_p;
24480 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24481 &ctor_dtor_or_conv_p,
24482 /*parenthesized_p=*/NULL,
24483 /*member_p=*/true);
24484 }
24485 else
24486 {
24487 begin_scope (sk_function_parms, NULL_TREE);
24488 cp_parser_parameter_declaration_list (parser, &error);
24489 pop_bindings_and_leave_scope ();
24490 }
24491 if (!cp_parser_error_occurred (parser) && !error)
24492 done = true;
24493 cp_parser_abort_tentative_parse (parser);
24494
24495 parser->in_template_argument_list_p = saved_italp;
24496 break;
24497 }
24498 case CPP_CLOSE_PAREN:
24499 case CPP_ELLIPSIS:
24500 /* If we run into a non-nested `;', `}', or `]',
24501 then the code is invalid -- but the default
24502 argument is certainly over. */
24503 case CPP_SEMICOLON:
24504 case CPP_CLOSE_BRACE:
24505 case CPP_CLOSE_SQUARE:
24506 if (depth == 0
24507 /* Handle correctly int n = sizeof ... ( p ); */
24508 && !(nsdmi && token->type == CPP_ELLIPSIS))
24509 done = true;
24510 /* Update DEPTH, if necessary. */
24511 else if (token->type == CPP_CLOSE_PAREN
24512 || token->type == CPP_CLOSE_BRACE
24513 || token->type == CPP_CLOSE_SQUARE)
24514 --depth;
24515 break;
24516
24517 case CPP_OPEN_PAREN:
24518 case CPP_OPEN_SQUARE:
24519 case CPP_OPEN_BRACE:
24520 ++depth;
24521 break;
24522
24523 case CPP_LESS:
24524 if (depth == 0)
24525 /* This might be the comparison operator, or it might
24526 start a template argument list. */
24527 ++maybe_template_id;
24528 break;
24529
24530 case CPP_RSHIFT:
24531 if (cxx_dialect == cxx98)
24532 break;
24533 /* Fall through for C++0x, which treats the `>>'
24534 operator like two `>' tokens in certain
24535 cases. */
24536
24537 case CPP_GREATER:
24538 if (depth == 0)
24539 {
24540 /* This might be an operator, or it might close a
24541 template argument list. But if a previous '<'
24542 started a template argument list, this will have
24543 closed it, so we can't be in one anymore. */
24544 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24545 if (maybe_template_id < 0)
24546 maybe_template_id = 0;
24547 }
24548 break;
24549
24550 /* If we run out of tokens, issue an error message. */
24551 case CPP_EOF:
24552 case CPP_PRAGMA_EOL:
24553 error_at (token->location, "file ends in default argument");
24554 done = true;
24555 break;
24556
24557 case CPP_NAME:
24558 case CPP_SCOPE:
24559 /* In these cases, we should look for template-ids.
24560 For example, if the default argument is
24561 `X<int, double>()', we need to do name lookup to
24562 figure out whether or not `X' is a template; if
24563 so, the `,' does not end the default argument.
24564
24565 That is not yet done. */
24566 break;
24567
24568 default:
24569 break;
24570 }
24571
24572 /* If we've reached the end, stop. */
24573 if (done)
24574 break;
24575
24576 /* Add the token to the token block. */
24577 token = cp_lexer_consume_token (parser->lexer);
24578 }
24579
24580 /* Create a DEFAULT_ARG to represent the unparsed default
24581 argument. */
24582 default_argument = make_node (DEFAULT_ARG);
24583 DEFARG_TOKENS (default_argument)
24584 = cp_token_cache_new (first_token, token);
24585 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24586
24587 return default_argument;
24588 }
24589
24590 /* Begin parsing tentatively. We always save tokens while parsing
24591 tentatively so that if the tentative parsing fails we can restore the
24592 tokens. */
24593
24594 static void
24595 cp_parser_parse_tentatively (cp_parser* parser)
24596 {
24597 /* Enter a new parsing context. */
24598 parser->context = cp_parser_context_new (parser->context);
24599 /* Begin saving tokens. */
24600 cp_lexer_save_tokens (parser->lexer);
24601 /* In order to avoid repetitive access control error messages,
24602 access checks are queued up until we are no longer parsing
24603 tentatively. */
24604 push_deferring_access_checks (dk_deferred);
24605 }
24606
24607 /* Commit to the currently active tentative parse. */
24608
24609 static void
24610 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24611 {
24612 cp_parser_context *context;
24613 cp_lexer *lexer;
24614
24615 /* Mark all of the levels as committed. */
24616 lexer = parser->lexer;
24617 for (context = parser->context; context->next; context = context->next)
24618 {
24619 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24620 break;
24621 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24622 while (!cp_lexer_saving_tokens (lexer))
24623 lexer = lexer->next;
24624 cp_lexer_commit_tokens (lexer);
24625 }
24626 }
24627
24628 /* Commit to the topmost currently active tentative parse.
24629
24630 Note that this function shouldn't be called when there are
24631 irreversible side-effects while in a tentative state. For
24632 example, we shouldn't create a permanent entry in the symbol
24633 table, or issue an error message that might not apply if the
24634 tentative parse is aborted. */
24635
24636 static void
24637 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24638 {
24639 cp_parser_context *context = parser->context;
24640 cp_lexer *lexer = parser->lexer;
24641
24642 if (context)
24643 {
24644 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24645 return;
24646 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24647
24648 while (!cp_lexer_saving_tokens (lexer))
24649 lexer = lexer->next;
24650 cp_lexer_commit_tokens (lexer);
24651 }
24652 }
24653
24654 /* Abort the currently active tentative parse. All consumed tokens
24655 will be rolled back, and no diagnostics will be issued. */
24656
24657 static void
24658 cp_parser_abort_tentative_parse (cp_parser* parser)
24659 {
24660 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24661 || errorcount > 0);
24662 cp_parser_simulate_error (parser);
24663 /* Now, pretend that we want to see if the construct was
24664 successfully parsed. */
24665 cp_parser_parse_definitely (parser);
24666 }
24667
24668 /* Stop parsing tentatively. If a parse error has occurred, restore the
24669 token stream. Otherwise, commit to the tokens we have consumed.
24670 Returns true if no error occurred; false otherwise. */
24671
24672 static bool
24673 cp_parser_parse_definitely (cp_parser* parser)
24674 {
24675 bool error_occurred;
24676 cp_parser_context *context;
24677
24678 /* Remember whether or not an error occurred, since we are about to
24679 destroy that information. */
24680 error_occurred = cp_parser_error_occurred (parser);
24681 /* Remove the topmost context from the stack. */
24682 context = parser->context;
24683 parser->context = context->next;
24684 /* If no parse errors occurred, commit to the tentative parse. */
24685 if (!error_occurred)
24686 {
24687 /* Commit to the tokens read tentatively, unless that was
24688 already done. */
24689 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
24690 cp_lexer_commit_tokens (parser->lexer);
24691
24692 pop_to_parent_deferring_access_checks ();
24693 }
24694 /* Otherwise, if errors occurred, roll back our state so that things
24695 are just as they were before we began the tentative parse. */
24696 else
24697 {
24698 cp_lexer_rollback_tokens (parser->lexer);
24699 pop_deferring_access_checks ();
24700 }
24701 /* Add the context to the front of the free list. */
24702 context->next = cp_parser_context_free_list;
24703 cp_parser_context_free_list = context;
24704
24705 return !error_occurred;
24706 }
24707
24708 /* Returns true if we are parsing tentatively and are not committed to
24709 this tentative parse. */
24710
24711 static bool
24712 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
24713 {
24714 return (cp_parser_parsing_tentatively (parser)
24715 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
24716 }
24717
24718 /* Returns nonzero iff an error has occurred during the most recent
24719 tentative parse. */
24720
24721 static bool
24722 cp_parser_error_occurred (cp_parser* parser)
24723 {
24724 return (cp_parser_parsing_tentatively (parser)
24725 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
24726 }
24727
24728 /* Returns nonzero if GNU extensions are allowed. */
24729
24730 static bool
24731 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
24732 {
24733 return parser->allow_gnu_extensions_p;
24734 }
24735 \f
24736 /* Objective-C++ Productions */
24737
24738
24739 /* Parse an Objective-C expression, which feeds into a primary-expression
24740 above.
24741
24742 objc-expression:
24743 objc-message-expression
24744 objc-string-literal
24745 objc-encode-expression
24746 objc-protocol-expression
24747 objc-selector-expression
24748
24749 Returns a tree representation of the expression. */
24750
24751 static tree
24752 cp_parser_objc_expression (cp_parser* parser)
24753 {
24754 /* Try to figure out what kind of declaration is present. */
24755 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24756
24757 switch (kwd->type)
24758 {
24759 case CPP_OPEN_SQUARE:
24760 return cp_parser_objc_message_expression (parser);
24761
24762 case CPP_OBJC_STRING:
24763 kwd = cp_lexer_consume_token (parser->lexer);
24764 return objc_build_string_object (kwd->u.value);
24765
24766 case CPP_KEYWORD:
24767 switch (kwd->keyword)
24768 {
24769 case RID_AT_ENCODE:
24770 return cp_parser_objc_encode_expression (parser);
24771
24772 case RID_AT_PROTOCOL:
24773 return cp_parser_objc_protocol_expression (parser);
24774
24775 case RID_AT_SELECTOR:
24776 return cp_parser_objc_selector_expression (parser);
24777
24778 default:
24779 break;
24780 }
24781 default:
24782 error_at (kwd->location,
24783 "misplaced %<@%D%> Objective-C++ construct",
24784 kwd->u.value);
24785 cp_parser_skip_to_end_of_block_or_statement (parser);
24786 }
24787
24788 return error_mark_node;
24789 }
24790
24791 /* Parse an Objective-C message expression.
24792
24793 objc-message-expression:
24794 [ objc-message-receiver objc-message-args ]
24795
24796 Returns a representation of an Objective-C message. */
24797
24798 static tree
24799 cp_parser_objc_message_expression (cp_parser* parser)
24800 {
24801 tree receiver, messageargs;
24802
24803 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
24804 receiver = cp_parser_objc_message_receiver (parser);
24805 messageargs = cp_parser_objc_message_args (parser);
24806 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24807
24808 return objc_build_message_expr (receiver, messageargs);
24809 }
24810
24811 /* Parse an objc-message-receiver.
24812
24813 objc-message-receiver:
24814 expression
24815 simple-type-specifier
24816
24817 Returns a representation of the type or expression. */
24818
24819 static tree
24820 cp_parser_objc_message_receiver (cp_parser* parser)
24821 {
24822 tree rcv;
24823
24824 /* An Objective-C message receiver may be either (1) a type
24825 or (2) an expression. */
24826 cp_parser_parse_tentatively (parser);
24827 rcv = cp_parser_expression (parser, false, NULL);
24828
24829 if (cp_parser_parse_definitely (parser))
24830 return rcv;
24831
24832 rcv = cp_parser_simple_type_specifier (parser,
24833 /*decl_specs=*/NULL,
24834 CP_PARSER_FLAGS_NONE);
24835
24836 return objc_get_class_reference (rcv);
24837 }
24838
24839 /* Parse the arguments and selectors comprising an Objective-C message.
24840
24841 objc-message-args:
24842 objc-selector
24843 objc-selector-args
24844 objc-selector-args , objc-comma-args
24845
24846 objc-selector-args:
24847 objc-selector [opt] : assignment-expression
24848 objc-selector-args objc-selector [opt] : assignment-expression
24849
24850 objc-comma-args:
24851 assignment-expression
24852 objc-comma-args , assignment-expression
24853
24854 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24855 selector arguments and TREE_VALUE containing a list of comma
24856 arguments. */
24857
24858 static tree
24859 cp_parser_objc_message_args (cp_parser* parser)
24860 {
24861 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
24862 bool maybe_unary_selector_p = true;
24863 cp_token *token = cp_lexer_peek_token (parser->lexer);
24864
24865 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24866 {
24867 tree selector = NULL_TREE, arg;
24868
24869 if (token->type != CPP_COLON)
24870 selector = cp_parser_objc_selector (parser);
24871
24872 /* Detect if we have a unary selector. */
24873 if (maybe_unary_selector_p
24874 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24875 return build_tree_list (selector, NULL_TREE);
24876
24877 maybe_unary_selector_p = false;
24878 cp_parser_require (parser, CPP_COLON, RT_COLON);
24879 arg = cp_parser_assignment_expression (parser, false, NULL);
24880
24881 sel_args
24882 = chainon (sel_args,
24883 build_tree_list (selector, arg));
24884
24885 token = cp_lexer_peek_token (parser->lexer);
24886 }
24887
24888 /* Handle non-selector arguments, if any. */
24889 while (token->type == CPP_COMMA)
24890 {
24891 tree arg;
24892
24893 cp_lexer_consume_token (parser->lexer);
24894 arg = cp_parser_assignment_expression (parser, false, NULL);
24895
24896 addl_args
24897 = chainon (addl_args,
24898 build_tree_list (NULL_TREE, arg));
24899
24900 token = cp_lexer_peek_token (parser->lexer);
24901 }
24902
24903 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24904 {
24905 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24906 return build_tree_list (error_mark_node, error_mark_node);
24907 }
24908
24909 return build_tree_list (sel_args, addl_args);
24910 }
24911
24912 /* Parse an Objective-C encode expression.
24913
24914 objc-encode-expression:
24915 @encode objc-typename
24916
24917 Returns an encoded representation of the type argument. */
24918
24919 static tree
24920 cp_parser_objc_encode_expression (cp_parser* parser)
24921 {
24922 tree type;
24923 cp_token *token;
24924
24925 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
24926 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24927 token = cp_lexer_peek_token (parser->lexer);
24928 type = complete_type (cp_parser_type_id (parser));
24929 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24930
24931 if (!type)
24932 {
24933 error_at (token->location,
24934 "%<@encode%> must specify a type as an argument");
24935 return error_mark_node;
24936 }
24937
24938 /* This happens if we find @encode(T) (where T is a template
24939 typename or something dependent on a template typename) when
24940 parsing a template. In that case, we can't compile it
24941 immediately, but we rather create an AT_ENCODE_EXPR which will
24942 need to be instantiated when the template is used.
24943 */
24944 if (dependent_type_p (type))
24945 {
24946 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24947 TREE_READONLY (value) = 1;
24948 return value;
24949 }
24950
24951 return objc_build_encode_expr (type);
24952 }
24953
24954 /* Parse an Objective-C @defs expression. */
24955
24956 static tree
24957 cp_parser_objc_defs_expression (cp_parser *parser)
24958 {
24959 tree name;
24960
24961 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
24962 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24963 name = cp_parser_identifier (parser);
24964 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24965
24966 return objc_get_class_ivars (name);
24967 }
24968
24969 /* Parse an Objective-C protocol expression.
24970
24971 objc-protocol-expression:
24972 @protocol ( identifier )
24973
24974 Returns a representation of the protocol expression. */
24975
24976 static tree
24977 cp_parser_objc_protocol_expression (cp_parser* parser)
24978 {
24979 tree proto;
24980
24981 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24982 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24983 proto = cp_parser_identifier (parser);
24984 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24985
24986 return objc_build_protocol_expr (proto);
24987 }
24988
24989 /* Parse an Objective-C selector expression.
24990
24991 objc-selector-expression:
24992 @selector ( objc-method-signature )
24993
24994 objc-method-signature:
24995 objc-selector
24996 objc-selector-seq
24997
24998 objc-selector-seq:
24999 objc-selector :
25000 objc-selector-seq objc-selector :
25001
25002 Returns a representation of the method selector. */
25003
25004 static tree
25005 cp_parser_objc_selector_expression (cp_parser* parser)
25006 {
25007 tree sel_seq = NULL_TREE;
25008 bool maybe_unary_selector_p = true;
25009 cp_token *token;
25010 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25011
25012 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25013 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25014 token = cp_lexer_peek_token (parser->lexer);
25015
25016 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25017 || token->type == CPP_SCOPE)
25018 {
25019 tree selector = NULL_TREE;
25020
25021 if (token->type != CPP_COLON
25022 || token->type == CPP_SCOPE)
25023 selector = cp_parser_objc_selector (parser);
25024
25025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25026 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25027 {
25028 /* Detect if we have a unary selector. */
25029 if (maybe_unary_selector_p)
25030 {
25031 sel_seq = selector;
25032 goto finish_selector;
25033 }
25034 else
25035 {
25036 cp_parser_error (parser, "expected %<:%>");
25037 }
25038 }
25039 maybe_unary_selector_p = false;
25040 token = cp_lexer_consume_token (parser->lexer);
25041
25042 if (token->type == CPP_SCOPE)
25043 {
25044 sel_seq
25045 = chainon (sel_seq,
25046 build_tree_list (selector, NULL_TREE));
25047 sel_seq
25048 = chainon (sel_seq,
25049 build_tree_list (NULL_TREE, NULL_TREE));
25050 }
25051 else
25052 sel_seq
25053 = chainon (sel_seq,
25054 build_tree_list (selector, NULL_TREE));
25055
25056 token = cp_lexer_peek_token (parser->lexer);
25057 }
25058
25059 finish_selector:
25060 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25061
25062 return objc_build_selector_expr (loc, sel_seq);
25063 }
25064
25065 /* Parse a list of identifiers.
25066
25067 objc-identifier-list:
25068 identifier
25069 objc-identifier-list , identifier
25070
25071 Returns a TREE_LIST of identifier nodes. */
25072
25073 static tree
25074 cp_parser_objc_identifier_list (cp_parser* parser)
25075 {
25076 tree identifier;
25077 tree list;
25078 cp_token *sep;
25079
25080 identifier = cp_parser_identifier (parser);
25081 if (identifier == error_mark_node)
25082 return error_mark_node;
25083
25084 list = build_tree_list (NULL_TREE, identifier);
25085 sep = cp_lexer_peek_token (parser->lexer);
25086
25087 while (sep->type == CPP_COMMA)
25088 {
25089 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25090 identifier = cp_parser_identifier (parser);
25091 if (identifier == error_mark_node)
25092 return list;
25093
25094 list = chainon (list, build_tree_list (NULL_TREE,
25095 identifier));
25096 sep = cp_lexer_peek_token (parser->lexer);
25097 }
25098
25099 return list;
25100 }
25101
25102 /* Parse an Objective-C alias declaration.
25103
25104 objc-alias-declaration:
25105 @compatibility_alias identifier identifier ;
25106
25107 This function registers the alias mapping with the Objective-C front end.
25108 It returns nothing. */
25109
25110 static void
25111 cp_parser_objc_alias_declaration (cp_parser* parser)
25112 {
25113 tree alias, orig;
25114
25115 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25116 alias = cp_parser_identifier (parser);
25117 orig = cp_parser_identifier (parser);
25118 objc_declare_alias (alias, orig);
25119 cp_parser_consume_semicolon_at_end_of_statement (parser);
25120 }
25121
25122 /* Parse an Objective-C class forward-declaration.
25123
25124 objc-class-declaration:
25125 @class objc-identifier-list ;
25126
25127 The function registers the forward declarations with the Objective-C
25128 front end. It returns nothing. */
25129
25130 static void
25131 cp_parser_objc_class_declaration (cp_parser* parser)
25132 {
25133 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25134 while (true)
25135 {
25136 tree id;
25137
25138 id = cp_parser_identifier (parser);
25139 if (id == error_mark_node)
25140 break;
25141
25142 objc_declare_class (id);
25143
25144 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25145 cp_lexer_consume_token (parser->lexer);
25146 else
25147 break;
25148 }
25149 cp_parser_consume_semicolon_at_end_of_statement (parser);
25150 }
25151
25152 /* Parse a list of Objective-C protocol references.
25153
25154 objc-protocol-refs-opt:
25155 objc-protocol-refs [opt]
25156
25157 objc-protocol-refs:
25158 < objc-identifier-list >
25159
25160 Returns a TREE_LIST of identifiers, if any. */
25161
25162 static tree
25163 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25164 {
25165 tree protorefs = NULL_TREE;
25166
25167 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25168 {
25169 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25170 protorefs = cp_parser_objc_identifier_list (parser);
25171 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25172 }
25173
25174 return protorefs;
25175 }
25176
25177 /* Parse a Objective-C visibility specification. */
25178
25179 static void
25180 cp_parser_objc_visibility_spec (cp_parser* parser)
25181 {
25182 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25183
25184 switch (vis->keyword)
25185 {
25186 case RID_AT_PRIVATE:
25187 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25188 break;
25189 case RID_AT_PROTECTED:
25190 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25191 break;
25192 case RID_AT_PUBLIC:
25193 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25194 break;
25195 case RID_AT_PACKAGE:
25196 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25197 break;
25198 default:
25199 return;
25200 }
25201
25202 /* Eat '@private'/'@protected'/'@public'. */
25203 cp_lexer_consume_token (parser->lexer);
25204 }
25205
25206 /* Parse an Objective-C method type. Return 'true' if it is a class
25207 (+) method, and 'false' if it is an instance (-) method. */
25208
25209 static inline bool
25210 cp_parser_objc_method_type (cp_parser* parser)
25211 {
25212 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25213 return true;
25214 else
25215 return false;
25216 }
25217
25218 /* Parse an Objective-C protocol qualifier. */
25219
25220 static tree
25221 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25222 {
25223 tree quals = NULL_TREE, node;
25224 cp_token *token = cp_lexer_peek_token (parser->lexer);
25225
25226 node = token->u.value;
25227
25228 while (node && identifier_p (node)
25229 && (node == ridpointers [(int) RID_IN]
25230 || node == ridpointers [(int) RID_OUT]
25231 || node == ridpointers [(int) RID_INOUT]
25232 || node == ridpointers [(int) RID_BYCOPY]
25233 || node == ridpointers [(int) RID_BYREF]
25234 || node == ridpointers [(int) RID_ONEWAY]))
25235 {
25236 quals = tree_cons (NULL_TREE, node, quals);
25237 cp_lexer_consume_token (parser->lexer);
25238 token = cp_lexer_peek_token (parser->lexer);
25239 node = token->u.value;
25240 }
25241
25242 return quals;
25243 }
25244
25245 /* Parse an Objective-C typename. */
25246
25247 static tree
25248 cp_parser_objc_typename (cp_parser* parser)
25249 {
25250 tree type_name = NULL_TREE;
25251
25252 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25253 {
25254 tree proto_quals, cp_type = NULL_TREE;
25255
25256 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25257 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25258
25259 /* An ObjC type name may consist of just protocol qualifiers, in which
25260 case the type shall default to 'id'. */
25261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25262 {
25263 cp_type = cp_parser_type_id (parser);
25264
25265 /* If the type could not be parsed, an error has already
25266 been produced. For error recovery, behave as if it had
25267 not been specified, which will use the default type
25268 'id'. */
25269 if (cp_type == error_mark_node)
25270 {
25271 cp_type = NULL_TREE;
25272 /* We need to skip to the closing parenthesis as
25273 cp_parser_type_id() does not seem to do it for
25274 us. */
25275 cp_parser_skip_to_closing_parenthesis (parser,
25276 /*recovering=*/true,
25277 /*or_comma=*/false,
25278 /*consume_paren=*/false);
25279 }
25280 }
25281
25282 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25283 type_name = build_tree_list (proto_quals, cp_type);
25284 }
25285
25286 return type_name;
25287 }
25288
25289 /* Check to see if TYPE refers to an Objective-C selector name. */
25290
25291 static bool
25292 cp_parser_objc_selector_p (enum cpp_ttype type)
25293 {
25294 return (type == CPP_NAME || type == CPP_KEYWORD
25295 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25296 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25297 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25298 || type == CPP_XOR || type == CPP_XOR_EQ);
25299 }
25300
25301 /* Parse an Objective-C selector. */
25302
25303 static tree
25304 cp_parser_objc_selector (cp_parser* parser)
25305 {
25306 cp_token *token = cp_lexer_consume_token (parser->lexer);
25307
25308 if (!cp_parser_objc_selector_p (token->type))
25309 {
25310 error_at (token->location, "invalid Objective-C++ selector name");
25311 return error_mark_node;
25312 }
25313
25314 /* C++ operator names are allowed to appear in ObjC selectors. */
25315 switch (token->type)
25316 {
25317 case CPP_AND_AND: return get_identifier ("and");
25318 case CPP_AND_EQ: return get_identifier ("and_eq");
25319 case CPP_AND: return get_identifier ("bitand");
25320 case CPP_OR: return get_identifier ("bitor");
25321 case CPP_COMPL: return get_identifier ("compl");
25322 case CPP_NOT: return get_identifier ("not");
25323 case CPP_NOT_EQ: return get_identifier ("not_eq");
25324 case CPP_OR_OR: return get_identifier ("or");
25325 case CPP_OR_EQ: return get_identifier ("or_eq");
25326 case CPP_XOR: return get_identifier ("xor");
25327 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25328 default: return token->u.value;
25329 }
25330 }
25331
25332 /* Parse an Objective-C params list. */
25333
25334 static tree
25335 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25336 {
25337 tree params = NULL_TREE;
25338 bool maybe_unary_selector_p = true;
25339 cp_token *token = cp_lexer_peek_token (parser->lexer);
25340
25341 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25342 {
25343 tree selector = NULL_TREE, type_name, identifier;
25344 tree parm_attr = NULL_TREE;
25345
25346 if (token->keyword == RID_ATTRIBUTE)
25347 break;
25348
25349 if (token->type != CPP_COLON)
25350 selector = cp_parser_objc_selector (parser);
25351
25352 /* Detect if we have a unary selector. */
25353 if (maybe_unary_selector_p
25354 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25355 {
25356 params = selector; /* Might be followed by attributes. */
25357 break;
25358 }
25359
25360 maybe_unary_selector_p = false;
25361 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25362 {
25363 /* Something went quite wrong. There should be a colon
25364 here, but there is not. Stop parsing parameters. */
25365 break;
25366 }
25367 type_name = cp_parser_objc_typename (parser);
25368 /* New ObjC allows attributes on parameters too. */
25369 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25370 parm_attr = cp_parser_attributes_opt (parser);
25371 identifier = cp_parser_identifier (parser);
25372
25373 params
25374 = chainon (params,
25375 objc_build_keyword_decl (selector,
25376 type_name,
25377 identifier,
25378 parm_attr));
25379
25380 token = cp_lexer_peek_token (parser->lexer);
25381 }
25382
25383 if (params == NULL_TREE)
25384 {
25385 cp_parser_error (parser, "objective-c++ method declaration is expected");
25386 return error_mark_node;
25387 }
25388
25389 /* We allow tail attributes for the method. */
25390 if (token->keyword == RID_ATTRIBUTE)
25391 {
25392 *attributes = cp_parser_attributes_opt (parser);
25393 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25394 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25395 return params;
25396 cp_parser_error (parser,
25397 "method attributes must be specified at the end");
25398 return error_mark_node;
25399 }
25400
25401 if (params == NULL_TREE)
25402 {
25403 cp_parser_error (parser, "objective-c++ method declaration is expected");
25404 return error_mark_node;
25405 }
25406 return params;
25407 }
25408
25409 /* Parse the non-keyword Objective-C params. */
25410
25411 static tree
25412 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25413 tree* attributes)
25414 {
25415 tree params = make_node (TREE_LIST);
25416 cp_token *token = cp_lexer_peek_token (parser->lexer);
25417 *ellipsisp = false; /* Initially, assume no ellipsis. */
25418
25419 while (token->type == CPP_COMMA)
25420 {
25421 cp_parameter_declarator *parmdecl;
25422 tree parm;
25423
25424 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25425 token = cp_lexer_peek_token (parser->lexer);
25426
25427 if (token->type == CPP_ELLIPSIS)
25428 {
25429 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25430 *ellipsisp = true;
25431 token = cp_lexer_peek_token (parser->lexer);
25432 break;
25433 }
25434
25435 /* TODO: parse attributes for tail parameters. */
25436 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25437 parm = grokdeclarator (parmdecl->declarator,
25438 &parmdecl->decl_specifiers,
25439 PARM, /*initialized=*/0,
25440 /*attrlist=*/NULL);
25441
25442 chainon (params, build_tree_list (NULL_TREE, parm));
25443 token = cp_lexer_peek_token (parser->lexer);
25444 }
25445
25446 /* We allow tail attributes for the method. */
25447 if (token->keyword == RID_ATTRIBUTE)
25448 {
25449 if (*attributes == NULL_TREE)
25450 {
25451 *attributes = cp_parser_attributes_opt (parser);
25452 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25453 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25454 return params;
25455 }
25456 else
25457 /* We have an error, but parse the attributes, so that we can
25458 carry on. */
25459 *attributes = cp_parser_attributes_opt (parser);
25460
25461 cp_parser_error (parser,
25462 "method attributes must be specified at the end");
25463 return error_mark_node;
25464 }
25465
25466 return params;
25467 }
25468
25469 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25470
25471 static void
25472 cp_parser_objc_interstitial_code (cp_parser* parser)
25473 {
25474 cp_token *token = cp_lexer_peek_token (parser->lexer);
25475
25476 /* If the next token is `extern' and the following token is a string
25477 literal, then we have a linkage specification. */
25478 if (token->keyword == RID_EXTERN
25479 && cp_parser_is_pure_string_literal
25480 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25481 cp_parser_linkage_specification (parser);
25482 /* Handle #pragma, if any. */
25483 else if (token->type == CPP_PRAGMA)
25484 cp_parser_pragma (parser, pragma_objc_icode);
25485 /* Allow stray semicolons. */
25486 else if (token->type == CPP_SEMICOLON)
25487 cp_lexer_consume_token (parser->lexer);
25488 /* Mark methods as optional or required, when building protocols. */
25489 else if (token->keyword == RID_AT_OPTIONAL)
25490 {
25491 cp_lexer_consume_token (parser->lexer);
25492 objc_set_method_opt (true);
25493 }
25494 else if (token->keyword == RID_AT_REQUIRED)
25495 {
25496 cp_lexer_consume_token (parser->lexer);
25497 objc_set_method_opt (false);
25498 }
25499 else if (token->keyword == RID_NAMESPACE)
25500 cp_parser_namespace_definition (parser);
25501 /* Other stray characters must generate errors. */
25502 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25503 {
25504 cp_lexer_consume_token (parser->lexer);
25505 error ("stray %qs between Objective-C++ methods",
25506 token->type == CPP_OPEN_BRACE ? "{" : "}");
25507 }
25508 /* Finally, try to parse a block-declaration, or a function-definition. */
25509 else
25510 cp_parser_block_declaration (parser, /*statement_p=*/false);
25511 }
25512
25513 /* Parse a method signature. */
25514
25515 static tree
25516 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25517 {
25518 tree rettype, kwdparms, optparms;
25519 bool ellipsis = false;
25520 bool is_class_method;
25521
25522 is_class_method = cp_parser_objc_method_type (parser);
25523 rettype = cp_parser_objc_typename (parser);
25524 *attributes = NULL_TREE;
25525 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25526 if (kwdparms == error_mark_node)
25527 return error_mark_node;
25528 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25529 if (optparms == error_mark_node)
25530 return error_mark_node;
25531
25532 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25533 }
25534
25535 static bool
25536 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25537 {
25538 tree tattr;
25539 cp_lexer_save_tokens (parser->lexer);
25540 tattr = cp_parser_attributes_opt (parser);
25541 gcc_assert (tattr) ;
25542
25543 /* If the attributes are followed by a method introducer, this is not allowed.
25544 Dump the attributes and flag the situation. */
25545 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25546 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25547 return true;
25548
25549 /* Otherwise, the attributes introduce some interstitial code, possibly so
25550 rewind to allow that check. */
25551 cp_lexer_rollback_tokens (parser->lexer);
25552 return false;
25553 }
25554
25555 /* Parse an Objective-C method prototype list. */
25556
25557 static void
25558 cp_parser_objc_method_prototype_list (cp_parser* parser)
25559 {
25560 cp_token *token = cp_lexer_peek_token (parser->lexer);
25561
25562 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25563 {
25564 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25565 {
25566 tree attributes, sig;
25567 bool is_class_method;
25568 if (token->type == CPP_PLUS)
25569 is_class_method = true;
25570 else
25571 is_class_method = false;
25572 sig = cp_parser_objc_method_signature (parser, &attributes);
25573 if (sig == error_mark_node)
25574 {
25575 cp_parser_skip_to_end_of_block_or_statement (parser);
25576 token = cp_lexer_peek_token (parser->lexer);
25577 continue;
25578 }
25579 objc_add_method_declaration (is_class_method, sig, attributes);
25580 cp_parser_consume_semicolon_at_end_of_statement (parser);
25581 }
25582 else if (token->keyword == RID_AT_PROPERTY)
25583 cp_parser_objc_at_property_declaration (parser);
25584 else if (token->keyword == RID_ATTRIBUTE
25585 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25586 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25587 OPT_Wattributes,
25588 "prefix attributes are ignored for methods");
25589 else
25590 /* Allow for interspersed non-ObjC++ code. */
25591 cp_parser_objc_interstitial_code (parser);
25592
25593 token = cp_lexer_peek_token (parser->lexer);
25594 }
25595
25596 if (token->type != CPP_EOF)
25597 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25598 else
25599 cp_parser_error (parser, "expected %<@end%>");
25600
25601 objc_finish_interface ();
25602 }
25603
25604 /* Parse an Objective-C method definition list. */
25605
25606 static void
25607 cp_parser_objc_method_definition_list (cp_parser* parser)
25608 {
25609 cp_token *token = cp_lexer_peek_token (parser->lexer);
25610
25611 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25612 {
25613 tree meth;
25614
25615 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25616 {
25617 cp_token *ptk;
25618 tree sig, attribute;
25619 bool is_class_method;
25620 if (token->type == CPP_PLUS)
25621 is_class_method = true;
25622 else
25623 is_class_method = false;
25624 push_deferring_access_checks (dk_deferred);
25625 sig = cp_parser_objc_method_signature (parser, &attribute);
25626 if (sig == error_mark_node)
25627 {
25628 cp_parser_skip_to_end_of_block_or_statement (parser);
25629 token = cp_lexer_peek_token (parser->lexer);
25630 continue;
25631 }
25632 objc_start_method_definition (is_class_method, sig, attribute,
25633 NULL_TREE);
25634
25635 /* For historical reasons, we accept an optional semicolon. */
25636 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25637 cp_lexer_consume_token (parser->lexer);
25638
25639 ptk = cp_lexer_peek_token (parser->lexer);
25640 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25641 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25642 {
25643 perform_deferred_access_checks (tf_warning_or_error);
25644 stop_deferring_access_checks ();
25645 meth = cp_parser_function_definition_after_declarator (parser,
25646 false);
25647 pop_deferring_access_checks ();
25648 objc_finish_method_definition (meth);
25649 }
25650 }
25651 /* The following case will be removed once @synthesize is
25652 completely implemented. */
25653 else if (token->keyword == RID_AT_PROPERTY)
25654 cp_parser_objc_at_property_declaration (parser);
25655 else if (token->keyword == RID_AT_SYNTHESIZE)
25656 cp_parser_objc_at_synthesize_declaration (parser);
25657 else if (token->keyword == RID_AT_DYNAMIC)
25658 cp_parser_objc_at_dynamic_declaration (parser);
25659 else if (token->keyword == RID_ATTRIBUTE
25660 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25661 warning_at (token->location, OPT_Wattributes,
25662 "prefix attributes are ignored for methods");
25663 else
25664 /* Allow for interspersed non-ObjC++ code. */
25665 cp_parser_objc_interstitial_code (parser);
25666
25667 token = cp_lexer_peek_token (parser->lexer);
25668 }
25669
25670 if (token->type != CPP_EOF)
25671 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25672 else
25673 cp_parser_error (parser, "expected %<@end%>");
25674
25675 objc_finish_implementation ();
25676 }
25677
25678 /* Parse Objective-C ivars. */
25679
25680 static void
25681 cp_parser_objc_class_ivars (cp_parser* parser)
25682 {
25683 cp_token *token = cp_lexer_peek_token (parser->lexer);
25684
25685 if (token->type != CPP_OPEN_BRACE)
25686 return; /* No ivars specified. */
25687
25688 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
25689 token = cp_lexer_peek_token (parser->lexer);
25690
25691 while (token->type != CPP_CLOSE_BRACE
25692 && token->keyword != RID_AT_END && token->type != CPP_EOF)
25693 {
25694 cp_decl_specifier_seq declspecs;
25695 int decl_class_or_enum_p;
25696 tree prefix_attributes;
25697
25698 cp_parser_objc_visibility_spec (parser);
25699
25700 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25701 break;
25702
25703 cp_parser_decl_specifier_seq (parser,
25704 CP_PARSER_FLAGS_OPTIONAL,
25705 &declspecs,
25706 &decl_class_or_enum_p);
25707
25708 /* auto, register, static, extern, mutable. */
25709 if (declspecs.storage_class != sc_none)
25710 {
25711 cp_parser_error (parser, "invalid type for instance variable");
25712 declspecs.storage_class = sc_none;
25713 }
25714
25715 /* thread_local. */
25716 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25717 {
25718 cp_parser_error (parser, "invalid type for instance variable");
25719 declspecs.locations[ds_thread] = 0;
25720 }
25721
25722 /* typedef. */
25723 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25724 {
25725 cp_parser_error (parser, "invalid type for instance variable");
25726 declspecs.locations[ds_typedef] = 0;
25727 }
25728
25729 prefix_attributes = declspecs.attributes;
25730 declspecs.attributes = NULL_TREE;
25731
25732 /* Keep going until we hit the `;' at the end of the
25733 declaration. */
25734 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25735 {
25736 tree width = NULL_TREE, attributes, first_attribute, decl;
25737 cp_declarator *declarator = NULL;
25738 int ctor_dtor_or_conv_p;
25739
25740 /* Check for a (possibly unnamed) bitfield declaration. */
25741 token = cp_lexer_peek_token (parser->lexer);
25742 if (token->type == CPP_COLON)
25743 goto eat_colon;
25744
25745 if (token->type == CPP_NAME
25746 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25747 == CPP_COLON))
25748 {
25749 /* Get the name of the bitfield. */
25750 declarator = make_id_declarator (NULL_TREE,
25751 cp_parser_identifier (parser),
25752 sfk_none);
25753
25754 eat_colon:
25755 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25756 /* Get the width of the bitfield. */
25757 width
25758 = cp_parser_constant_expression (parser,
25759 /*allow_non_constant=*/false,
25760 NULL);
25761 }
25762 else
25763 {
25764 /* Parse the declarator. */
25765 declarator
25766 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25767 &ctor_dtor_or_conv_p,
25768 /*parenthesized_p=*/NULL,
25769 /*member_p=*/false);
25770 }
25771
25772 /* Look for attributes that apply to the ivar. */
25773 attributes = cp_parser_attributes_opt (parser);
25774 /* Remember which attributes are prefix attributes and
25775 which are not. */
25776 first_attribute = attributes;
25777 /* Combine the attributes. */
25778 attributes = chainon (prefix_attributes, attributes);
25779
25780 if (width)
25781 /* Create the bitfield declaration. */
25782 decl = grokbitfield (declarator, &declspecs,
25783 width,
25784 attributes);
25785 else
25786 decl = grokfield (declarator, &declspecs,
25787 NULL_TREE, /*init_const_expr_p=*/false,
25788 NULL_TREE, attributes);
25789
25790 /* Add the instance variable. */
25791 if (decl != error_mark_node && decl != NULL_TREE)
25792 objc_add_instance_variable (decl);
25793
25794 /* Reset PREFIX_ATTRIBUTES. */
25795 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25796 attributes = TREE_CHAIN (attributes);
25797 if (attributes)
25798 TREE_CHAIN (attributes) = NULL_TREE;
25799
25800 token = cp_lexer_peek_token (parser->lexer);
25801
25802 if (token->type == CPP_COMMA)
25803 {
25804 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25805 continue;
25806 }
25807 break;
25808 }
25809
25810 cp_parser_consume_semicolon_at_end_of_statement (parser);
25811 token = cp_lexer_peek_token (parser->lexer);
25812 }
25813
25814 if (token->keyword == RID_AT_END)
25815 cp_parser_error (parser, "expected %<}%>");
25816
25817 /* Do not consume the RID_AT_END, so it will be read again as terminating
25818 the @interface of @implementation. */
25819 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
25820 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
25821
25822 /* For historical reasons, we accept an optional semicolon. */
25823 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25824 cp_lexer_consume_token (parser->lexer);
25825 }
25826
25827 /* Parse an Objective-C protocol declaration. */
25828
25829 static void
25830 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
25831 {
25832 tree proto, protorefs;
25833 cp_token *tok;
25834
25835 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
25837 {
25838 tok = cp_lexer_peek_token (parser->lexer);
25839 error_at (tok->location, "identifier expected after %<@protocol%>");
25840 cp_parser_consume_semicolon_at_end_of_statement (parser);
25841 return;
25842 }
25843
25844 /* See if we have a forward declaration or a definition. */
25845 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
25846
25847 /* Try a forward declaration first. */
25848 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
25849 {
25850 while (true)
25851 {
25852 tree id;
25853
25854 id = cp_parser_identifier (parser);
25855 if (id == error_mark_node)
25856 break;
25857
25858 objc_declare_protocol (id, attributes);
25859
25860 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25861 cp_lexer_consume_token (parser->lexer);
25862 else
25863 break;
25864 }
25865 cp_parser_consume_semicolon_at_end_of_statement (parser);
25866 }
25867
25868 /* Ok, we got a full-fledged definition (or at least should). */
25869 else
25870 {
25871 proto = cp_parser_identifier (parser);
25872 protorefs = cp_parser_objc_protocol_refs_opt (parser);
25873 objc_start_protocol (proto, protorefs, attributes);
25874 cp_parser_objc_method_prototype_list (parser);
25875 }
25876 }
25877
25878 /* Parse an Objective-C superclass or category. */
25879
25880 static void
25881 cp_parser_objc_superclass_or_category (cp_parser *parser,
25882 bool iface_p,
25883 tree *super,
25884 tree *categ, bool *is_class_extension)
25885 {
25886 cp_token *next = cp_lexer_peek_token (parser->lexer);
25887
25888 *super = *categ = NULL_TREE;
25889 *is_class_extension = false;
25890 if (next->type == CPP_COLON)
25891 {
25892 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25893 *super = cp_parser_identifier (parser);
25894 }
25895 else if (next->type == CPP_OPEN_PAREN)
25896 {
25897 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25898
25899 /* If there is no category name, and this is an @interface, we
25900 have a class extension. */
25901 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25902 {
25903 *categ = NULL_TREE;
25904 *is_class_extension = true;
25905 }
25906 else
25907 *categ = cp_parser_identifier (parser);
25908
25909 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25910 }
25911 }
25912
25913 /* Parse an Objective-C class interface. */
25914
25915 static void
25916 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25917 {
25918 tree name, super, categ, protos;
25919 bool is_class_extension;
25920
25921 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
25922 name = cp_parser_identifier (parser);
25923 if (name == error_mark_node)
25924 {
25925 /* It's hard to recover because even if valid @interface stuff
25926 is to follow, we can't compile it (or validate it) if we
25927 don't even know which class it refers to. Let's assume this
25928 was a stray '@interface' token in the stream and skip it.
25929 */
25930 return;
25931 }
25932 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25933 &is_class_extension);
25934 protos = cp_parser_objc_protocol_refs_opt (parser);
25935
25936 /* We have either a class or a category on our hands. */
25937 if (categ || is_class_extension)
25938 objc_start_category_interface (name, categ, protos, attributes);
25939 else
25940 {
25941 objc_start_class_interface (name, super, protos, attributes);
25942 /* Handle instance variable declarations, if any. */
25943 cp_parser_objc_class_ivars (parser);
25944 objc_continue_interface ();
25945 }
25946
25947 cp_parser_objc_method_prototype_list (parser);
25948 }
25949
25950 /* Parse an Objective-C class implementation. */
25951
25952 static void
25953 cp_parser_objc_class_implementation (cp_parser* parser)
25954 {
25955 tree name, super, categ;
25956 bool is_class_extension;
25957
25958 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
25959 name = cp_parser_identifier (parser);
25960 if (name == error_mark_node)
25961 {
25962 /* It's hard to recover because even if valid @implementation
25963 stuff is to follow, we can't compile it (or validate it) if
25964 we don't even know which class it refers to. Let's assume
25965 this was a stray '@implementation' token in the stream and
25966 skip it.
25967 */
25968 return;
25969 }
25970 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25971 &is_class_extension);
25972
25973 /* We have either a class or a category on our hands. */
25974 if (categ)
25975 objc_start_category_implementation (name, categ);
25976 else
25977 {
25978 objc_start_class_implementation (name, super);
25979 /* Handle instance variable declarations, if any. */
25980 cp_parser_objc_class_ivars (parser);
25981 objc_continue_implementation ();
25982 }
25983
25984 cp_parser_objc_method_definition_list (parser);
25985 }
25986
25987 /* Consume the @end token and finish off the implementation. */
25988
25989 static void
25990 cp_parser_objc_end_implementation (cp_parser* parser)
25991 {
25992 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25993 objc_finish_implementation ();
25994 }
25995
25996 /* Parse an Objective-C declaration. */
25997
25998 static void
25999 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26000 {
26001 /* Try to figure out what kind of declaration is present. */
26002 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26003
26004 if (attributes)
26005 switch (kwd->keyword)
26006 {
26007 case RID_AT_ALIAS:
26008 case RID_AT_CLASS:
26009 case RID_AT_END:
26010 error_at (kwd->location, "attributes may not be specified before"
26011 " the %<@%D%> Objective-C++ keyword",
26012 kwd->u.value);
26013 attributes = NULL;
26014 break;
26015 case RID_AT_IMPLEMENTATION:
26016 warning_at (kwd->location, OPT_Wattributes,
26017 "prefix attributes are ignored before %<@%D%>",
26018 kwd->u.value);
26019 attributes = NULL;
26020 default:
26021 break;
26022 }
26023
26024 switch (kwd->keyword)
26025 {
26026 case RID_AT_ALIAS:
26027 cp_parser_objc_alias_declaration (parser);
26028 break;
26029 case RID_AT_CLASS:
26030 cp_parser_objc_class_declaration (parser);
26031 break;
26032 case RID_AT_PROTOCOL:
26033 cp_parser_objc_protocol_declaration (parser, attributes);
26034 break;
26035 case RID_AT_INTERFACE:
26036 cp_parser_objc_class_interface (parser, attributes);
26037 break;
26038 case RID_AT_IMPLEMENTATION:
26039 cp_parser_objc_class_implementation (parser);
26040 break;
26041 case RID_AT_END:
26042 cp_parser_objc_end_implementation (parser);
26043 break;
26044 default:
26045 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26046 kwd->u.value);
26047 cp_parser_skip_to_end_of_block_or_statement (parser);
26048 }
26049 }
26050
26051 /* Parse an Objective-C try-catch-finally statement.
26052
26053 objc-try-catch-finally-stmt:
26054 @try compound-statement objc-catch-clause-seq [opt]
26055 objc-finally-clause [opt]
26056
26057 objc-catch-clause-seq:
26058 objc-catch-clause objc-catch-clause-seq [opt]
26059
26060 objc-catch-clause:
26061 @catch ( objc-exception-declaration ) compound-statement
26062
26063 objc-finally-clause:
26064 @finally compound-statement
26065
26066 objc-exception-declaration:
26067 parameter-declaration
26068 '...'
26069
26070 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26071
26072 Returns NULL_TREE.
26073
26074 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26075 for C. Keep them in sync. */
26076
26077 static tree
26078 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26079 {
26080 location_t location;
26081 tree stmt;
26082
26083 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26084 location = cp_lexer_peek_token (parser->lexer)->location;
26085 objc_maybe_warn_exceptions (location);
26086 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26087 node, lest it get absorbed into the surrounding block. */
26088 stmt = push_stmt_list ();
26089 cp_parser_compound_statement (parser, NULL, false, false);
26090 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26091
26092 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26093 {
26094 cp_parameter_declarator *parm;
26095 tree parameter_declaration = error_mark_node;
26096 bool seen_open_paren = false;
26097
26098 cp_lexer_consume_token (parser->lexer);
26099 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26100 seen_open_paren = true;
26101 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26102 {
26103 /* We have "@catch (...)" (where the '...' are literally
26104 what is in the code). Skip the '...'.
26105 parameter_declaration is set to NULL_TREE, and
26106 objc_being_catch_clauses() knows that that means
26107 '...'. */
26108 cp_lexer_consume_token (parser->lexer);
26109 parameter_declaration = NULL_TREE;
26110 }
26111 else
26112 {
26113 /* We have "@catch (NSException *exception)" or something
26114 like that. Parse the parameter declaration. */
26115 parm = cp_parser_parameter_declaration (parser, false, NULL);
26116 if (parm == NULL)
26117 parameter_declaration = error_mark_node;
26118 else
26119 parameter_declaration = grokdeclarator (parm->declarator,
26120 &parm->decl_specifiers,
26121 PARM, /*initialized=*/0,
26122 /*attrlist=*/NULL);
26123 }
26124 if (seen_open_paren)
26125 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26126 else
26127 {
26128 /* If there was no open parenthesis, we are recovering from
26129 an error, and we are trying to figure out what mistake
26130 the user has made. */
26131
26132 /* If there is an immediate closing parenthesis, the user
26133 probably forgot the opening one (ie, they typed "@catch
26134 NSException *e)". Parse the closing parenthesis and keep
26135 going. */
26136 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26137 cp_lexer_consume_token (parser->lexer);
26138
26139 /* If these is no immediate closing parenthesis, the user
26140 probably doesn't know that parenthesis are required at
26141 all (ie, they typed "@catch NSException *e"). So, just
26142 forget about the closing parenthesis and keep going. */
26143 }
26144 objc_begin_catch_clause (parameter_declaration);
26145 cp_parser_compound_statement (parser, NULL, false, false);
26146 objc_finish_catch_clause ();
26147 }
26148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26149 {
26150 cp_lexer_consume_token (parser->lexer);
26151 location = cp_lexer_peek_token (parser->lexer)->location;
26152 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26153 node, lest it get absorbed into the surrounding block. */
26154 stmt = push_stmt_list ();
26155 cp_parser_compound_statement (parser, NULL, false, false);
26156 objc_build_finally_clause (location, pop_stmt_list (stmt));
26157 }
26158
26159 return objc_finish_try_stmt ();
26160 }
26161
26162 /* Parse an Objective-C synchronized statement.
26163
26164 objc-synchronized-stmt:
26165 @synchronized ( expression ) compound-statement
26166
26167 Returns NULL_TREE. */
26168
26169 static tree
26170 cp_parser_objc_synchronized_statement (cp_parser *parser)
26171 {
26172 location_t location;
26173 tree lock, stmt;
26174
26175 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26176
26177 location = cp_lexer_peek_token (parser->lexer)->location;
26178 objc_maybe_warn_exceptions (location);
26179 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26180 lock = cp_parser_expression (parser, false, NULL);
26181 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26182
26183 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26184 node, lest it get absorbed into the surrounding block. */
26185 stmt = push_stmt_list ();
26186 cp_parser_compound_statement (parser, NULL, false, false);
26187
26188 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26189 }
26190
26191 /* Parse an Objective-C throw statement.
26192
26193 objc-throw-stmt:
26194 @throw assignment-expression [opt] ;
26195
26196 Returns a constructed '@throw' statement. */
26197
26198 static tree
26199 cp_parser_objc_throw_statement (cp_parser *parser)
26200 {
26201 tree expr = NULL_TREE;
26202 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26203
26204 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26205
26206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26207 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26208
26209 cp_parser_consume_semicolon_at_end_of_statement (parser);
26210
26211 return objc_build_throw_stmt (loc, expr);
26212 }
26213
26214 /* Parse an Objective-C statement. */
26215
26216 static tree
26217 cp_parser_objc_statement (cp_parser * parser)
26218 {
26219 /* Try to figure out what kind of declaration is present. */
26220 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26221
26222 switch (kwd->keyword)
26223 {
26224 case RID_AT_TRY:
26225 return cp_parser_objc_try_catch_finally_statement (parser);
26226 case RID_AT_SYNCHRONIZED:
26227 return cp_parser_objc_synchronized_statement (parser);
26228 case RID_AT_THROW:
26229 return cp_parser_objc_throw_statement (parser);
26230 default:
26231 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26232 kwd->u.value);
26233 cp_parser_skip_to_end_of_block_or_statement (parser);
26234 }
26235
26236 return error_mark_node;
26237 }
26238
26239 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26240 look ahead to see if an objc keyword follows the attributes. This
26241 is to detect the use of prefix attributes on ObjC @interface and
26242 @protocol. */
26243
26244 static bool
26245 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26246 {
26247 cp_lexer_save_tokens (parser->lexer);
26248 *attrib = cp_parser_attributes_opt (parser);
26249 gcc_assert (*attrib);
26250 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26251 {
26252 cp_lexer_commit_tokens (parser->lexer);
26253 return true;
26254 }
26255 cp_lexer_rollback_tokens (parser->lexer);
26256 return false;
26257 }
26258
26259 /* This routine is a minimal replacement for
26260 c_parser_struct_declaration () used when parsing the list of
26261 types/names or ObjC++ properties. For example, when parsing the
26262 code
26263
26264 @property (readonly) int a, b, c;
26265
26266 this function is responsible for parsing "int a, int b, int c" and
26267 returning the declarations as CHAIN of DECLs.
26268
26269 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26270 similar parsing. */
26271 static tree
26272 cp_parser_objc_struct_declaration (cp_parser *parser)
26273 {
26274 tree decls = NULL_TREE;
26275 cp_decl_specifier_seq declspecs;
26276 int decl_class_or_enum_p;
26277 tree prefix_attributes;
26278
26279 cp_parser_decl_specifier_seq (parser,
26280 CP_PARSER_FLAGS_NONE,
26281 &declspecs,
26282 &decl_class_or_enum_p);
26283
26284 if (declspecs.type == error_mark_node)
26285 return error_mark_node;
26286
26287 /* auto, register, static, extern, mutable. */
26288 if (declspecs.storage_class != sc_none)
26289 {
26290 cp_parser_error (parser, "invalid type for property");
26291 declspecs.storage_class = sc_none;
26292 }
26293
26294 /* thread_local. */
26295 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26296 {
26297 cp_parser_error (parser, "invalid type for property");
26298 declspecs.locations[ds_thread] = 0;
26299 }
26300
26301 /* typedef. */
26302 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26303 {
26304 cp_parser_error (parser, "invalid type for property");
26305 declspecs.locations[ds_typedef] = 0;
26306 }
26307
26308 prefix_attributes = declspecs.attributes;
26309 declspecs.attributes = NULL_TREE;
26310
26311 /* Keep going until we hit the `;' at the end of the declaration. */
26312 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26313 {
26314 tree attributes, first_attribute, decl;
26315 cp_declarator *declarator;
26316 cp_token *token;
26317
26318 /* Parse the declarator. */
26319 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26320 NULL, NULL, false);
26321
26322 /* Look for attributes that apply to the ivar. */
26323 attributes = cp_parser_attributes_opt (parser);
26324 /* Remember which attributes are prefix attributes and
26325 which are not. */
26326 first_attribute = attributes;
26327 /* Combine the attributes. */
26328 attributes = chainon (prefix_attributes, attributes);
26329
26330 decl = grokfield (declarator, &declspecs,
26331 NULL_TREE, /*init_const_expr_p=*/false,
26332 NULL_TREE, attributes);
26333
26334 if (decl == error_mark_node || decl == NULL_TREE)
26335 return error_mark_node;
26336
26337 /* Reset PREFIX_ATTRIBUTES. */
26338 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26339 attributes = TREE_CHAIN (attributes);
26340 if (attributes)
26341 TREE_CHAIN (attributes) = NULL_TREE;
26342
26343 DECL_CHAIN (decl) = decls;
26344 decls = decl;
26345
26346 token = cp_lexer_peek_token (parser->lexer);
26347 if (token->type == CPP_COMMA)
26348 {
26349 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26350 continue;
26351 }
26352 else
26353 break;
26354 }
26355 return decls;
26356 }
26357
26358 /* Parse an Objective-C @property declaration. The syntax is:
26359
26360 objc-property-declaration:
26361 '@property' objc-property-attributes[opt] struct-declaration ;
26362
26363 objc-property-attributes:
26364 '(' objc-property-attribute-list ')'
26365
26366 objc-property-attribute-list:
26367 objc-property-attribute
26368 objc-property-attribute-list, objc-property-attribute
26369
26370 objc-property-attribute
26371 'getter' = identifier
26372 'setter' = identifier
26373 'readonly'
26374 'readwrite'
26375 'assign'
26376 'retain'
26377 'copy'
26378 'nonatomic'
26379
26380 For example:
26381 @property NSString *name;
26382 @property (readonly) id object;
26383 @property (retain, nonatomic, getter=getTheName) id name;
26384 @property int a, b, c;
26385
26386 PS: This function is identical to
26387 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26388 static void
26389 cp_parser_objc_at_property_declaration (cp_parser *parser)
26390 {
26391 /* The following variables hold the attributes of the properties as
26392 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26393 seen. When we see an attribute, we set them to 'true' (if they
26394 are boolean properties) or to the identifier (if they have an
26395 argument, ie, for getter and setter). Note that here we only
26396 parse the list of attributes, check the syntax and accumulate the
26397 attributes that we find. objc_add_property_declaration() will
26398 then process the information. */
26399 bool property_assign = false;
26400 bool property_copy = false;
26401 tree property_getter_ident = NULL_TREE;
26402 bool property_nonatomic = false;
26403 bool property_readonly = false;
26404 bool property_readwrite = false;
26405 bool property_retain = false;
26406 tree property_setter_ident = NULL_TREE;
26407
26408 /* 'properties' is the list of properties that we read. Usually a
26409 single one, but maybe more (eg, in "@property int a, b, c;" there
26410 are three). */
26411 tree properties;
26412 location_t loc;
26413
26414 loc = cp_lexer_peek_token (parser->lexer)->location;
26415
26416 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26417
26418 /* Parse the optional attribute list... */
26419 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26420 {
26421 /* Eat the '('. */
26422 cp_lexer_consume_token (parser->lexer);
26423
26424 while (true)
26425 {
26426 bool syntax_error = false;
26427 cp_token *token = cp_lexer_peek_token (parser->lexer);
26428 enum rid keyword;
26429
26430 if (token->type != CPP_NAME)
26431 {
26432 cp_parser_error (parser, "expected identifier");
26433 break;
26434 }
26435 keyword = C_RID_CODE (token->u.value);
26436 cp_lexer_consume_token (parser->lexer);
26437 switch (keyword)
26438 {
26439 case RID_ASSIGN: property_assign = true; break;
26440 case RID_COPY: property_copy = true; break;
26441 case RID_NONATOMIC: property_nonatomic = true; break;
26442 case RID_READONLY: property_readonly = true; break;
26443 case RID_READWRITE: property_readwrite = true; break;
26444 case RID_RETAIN: property_retain = true; break;
26445
26446 case RID_GETTER:
26447 case RID_SETTER:
26448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26449 {
26450 if (keyword == RID_GETTER)
26451 cp_parser_error (parser,
26452 "missing %<=%> (after %<getter%> attribute)");
26453 else
26454 cp_parser_error (parser,
26455 "missing %<=%> (after %<setter%> attribute)");
26456 syntax_error = true;
26457 break;
26458 }
26459 cp_lexer_consume_token (parser->lexer); /* eat the = */
26460 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26461 {
26462 cp_parser_error (parser, "expected identifier");
26463 syntax_error = true;
26464 break;
26465 }
26466 if (keyword == RID_SETTER)
26467 {
26468 if (property_setter_ident != NULL_TREE)
26469 {
26470 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26471 cp_lexer_consume_token (parser->lexer);
26472 }
26473 else
26474 property_setter_ident = cp_parser_objc_selector (parser);
26475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26476 cp_parser_error (parser, "setter name must terminate with %<:%>");
26477 else
26478 cp_lexer_consume_token (parser->lexer);
26479 }
26480 else
26481 {
26482 if (property_getter_ident != NULL_TREE)
26483 {
26484 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26485 cp_lexer_consume_token (parser->lexer);
26486 }
26487 else
26488 property_getter_ident = cp_parser_objc_selector (parser);
26489 }
26490 break;
26491 default:
26492 cp_parser_error (parser, "unknown property attribute");
26493 syntax_error = true;
26494 break;
26495 }
26496
26497 if (syntax_error)
26498 break;
26499
26500 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26501 cp_lexer_consume_token (parser->lexer);
26502 else
26503 break;
26504 }
26505
26506 /* FIXME: "@property (setter, assign);" will generate a spurious
26507 "error: expected ‘)’ before ‘,’ token". This is because
26508 cp_parser_require, unlike the C counterpart, will produce an
26509 error even if we are in error recovery. */
26510 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26511 {
26512 cp_parser_skip_to_closing_parenthesis (parser,
26513 /*recovering=*/true,
26514 /*or_comma=*/false,
26515 /*consume_paren=*/true);
26516 }
26517 }
26518
26519 /* ... and the property declaration(s). */
26520 properties = cp_parser_objc_struct_declaration (parser);
26521
26522 if (properties == error_mark_node)
26523 {
26524 cp_parser_skip_to_end_of_statement (parser);
26525 /* If the next token is now a `;', consume it. */
26526 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26527 cp_lexer_consume_token (parser->lexer);
26528 return;
26529 }
26530
26531 if (properties == NULL_TREE)
26532 cp_parser_error (parser, "expected identifier");
26533 else
26534 {
26535 /* Comma-separated properties are chained together in
26536 reverse order; add them one by one. */
26537 properties = nreverse (properties);
26538
26539 for (; properties; properties = TREE_CHAIN (properties))
26540 objc_add_property_declaration (loc, copy_node (properties),
26541 property_readonly, property_readwrite,
26542 property_assign, property_retain,
26543 property_copy, property_nonatomic,
26544 property_getter_ident, property_setter_ident);
26545 }
26546
26547 cp_parser_consume_semicolon_at_end_of_statement (parser);
26548 }
26549
26550 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26551
26552 objc-synthesize-declaration:
26553 @synthesize objc-synthesize-identifier-list ;
26554
26555 objc-synthesize-identifier-list:
26556 objc-synthesize-identifier
26557 objc-synthesize-identifier-list, objc-synthesize-identifier
26558
26559 objc-synthesize-identifier
26560 identifier
26561 identifier = identifier
26562
26563 For example:
26564 @synthesize MyProperty;
26565 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26566
26567 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26568 for C. Keep them in sync.
26569 */
26570 static void
26571 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26572 {
26573 tree list = NULL_TREE;
26574 location_t loc;
26575 loc = cp_lexer_peek_token (parser->lexer)->location;
26576
26577 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26578 while (true)
26579 {
26580 tree property, ivar;
26581 property = cp_parser_identifier (parser);
26582 if (property == error_mark_node)
26583 {
26584 cp_parser_consume_semicolon_at_end_of_statement (parser);
26585 return;
26586 }
26587 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26588 {
26589 cp_lexer_consume_token (parser->lexer);
26590 ivar = cp_parser_identifier (parser);
26591 if (ivar == error_mark_node)
26592 {
26593 cp_parser_consume_semicolon_at_end_of_statement (parser);
26594 return;
26595 }
26596 }
26597 else
26598 ivar = NULL_TREE;
26599 list = chainon (list, build_tree_list (ivar, property));
26600 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26601 cp_lexer_consume_token (parser->lexer);
26602 else
26603 break;
26604 }
26605 cp_parser_consume_semicolon_at_end_of_statement (parser);
26606 objc_add_synthesize_declaration (loc, list);
26607 }
26608
26609 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26610
26611 objc-dynamic-declaration:
26612 @dynamic identifier-list ;
26613
26614 For example:
26615 @dynamic MyProperty;
26616 @dynamic MyProperty, AnotherProperty;
26617
26618 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26619 for C. Keep them in sync.
26620 */
26621 static void
26622 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26623 {
26624 tree list = NULL_TREE;
26625 location_t loc;
26626 loc = cp_lexer_peek_token (parser->lexer)->location;
26627
26628 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26629 while (true)
26630 {
26631 tree property;
26632 property = cp_parser_identifier (parser);
26633 if (property == error_mark_node)
26634 {
26635 cp_parser_consume_semicolon_at_end_of_statement (parser);
26636 return;
26637 }
26638 list = chainon (list, build_tree_list (NULL, property));
26639 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26640 cp_lexer_consume_token (parser->lexer);
26641 else
26642 break;
26643 }
26644 cp_parser_consume_semicolon_at_end_of_statement (parser);
26645 objc_add_dynamic_declaration (loc, list);
26646 }
26647
26648 \f
26649 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26650
26651 /* Returns name of the next clause.
26652 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26653 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26654 returned and the token is consumed. */
26655
26656 static pragma_omp_clause
26657 cp_parser_omp_clause_name (cp_parser *parser)
26658 {
26659 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26660
26661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26662 result = PRAGMA_OMP_CLAUSE_IF;
26663 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26664 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26665 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26666 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26667 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26668 result = PRAGMA_OMP_CLAUSE_FOR;
26669 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26670 {
26671 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26672 const char *p = IDENTIFIER_POINTER (id);
26673
26674 switch (p[0])
26675 {
26676 case 'a':
26677 if (!strcmp ("aligned", p))
26678 result = PRAGMA_OMP_CLAUSE_ALIGNED;
26679 break;
26680 case 'c':
26681 if (!strcmp ("collapse", p))
26682 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
26683 else if (!strcmp ("copyin", p))
26684 result = PRAGMA_OMP_CLAUSE_COPYIN;
26685 else if (!strcmp ("copyprivate", p))
26686 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
26687 break;
26688 case 'd':
26689 if (!strcmp ("depend", p))
26690 result = PRAGMA_OMP_CLAUSE_DEPEND;
26691 else if (!strcmp ("device", p))
26692 result = PRAGMA_OMP_CLAUSE_DEVICE;
26693 else if (!strcmp ("dist_schedule", p))
26694 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
26695 break;
26696 case 'f':
26697 if (!strcmp ("final", p))
26698 result = PRAGMA_OMP_CLAUSE_FINAL;
26699 else if (!strcmp ("firstprivate", p))
26700 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
26701 else if (!strcmp ("from", p))
26702 result = PRAGMA_OMP_CLAUSE_FROM;
26703 break;
26704 case 'i':
26705 if (!strcmp ("inbranch", p))
26706 result = PRAGMA_OMP_CLAUSE_INBRANCH;
26707 break;
26708 case 'l':
26709 if (!strcmp ("lastprivate", p))
26710 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
26711 else if (!strcmp ("linear", p))
26712 result = PRAGMA_OMP_CLAUSE_LINEAR;
26713 break;
26714 case 'm':
26715 if (!strcmp ("map", p))
26716 result = PRAGMA_OMP_CLAUSE_MAP;
26717 else if (!strcmp ("mergeable", p))
26718 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
26719 break;
26720 case 'n':
26721 if (!strcmp ("notinbranch", p))
26722 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
26723 else if (!strcmp ("nowait", p))
26724 result = PRAGMA_OMP_CLAUSE_NOWAIT;
26725 else if (!strcmp ("num_teams", p))
26726 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
26727 else if (!strcmp ("num_threads", p))
26728 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
26729 break;
26730 case 'o':
26731 if (!strcmp ("ordered", p))
26732 result = PRAGMA_OMP_CLAUSE_ORDERED;
26733 break;
26734 case 'p':
26735 if (!strcmp ("parallel", p))
26736 result = PRAGMA_OMP_CLAUSE_PARALLEL;
26737 else if (!strcmp ("proc_bind", p))
26738 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
26739 break;
26740 case 'r':
26741 if (!strcmp ("reduction", p))
26742 result = PRAGMA_OMP_CLAUSE_REDUCTION;
26743 break;
26744 case 's':
26745 if (!strcmp ("safelen", p))
26746 result = PRAGMA_OMP_CLAUSE_SAFELEN;
26747 else if (!strcmp ("schedule", p))
26748 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
26749 else if (!strcmp ("sections", p))
26750 result = PRAGMA_OMP_CLAUSE_SECTIONS;
26751 else if (!strcmp ("shared", p))
26752 result = PRAGMA_OMP_CLAUSE_SHARED;
26753 else if (!strcmp ("simdlen", p))
26754 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
26755 break;
26756 case 't':
26757 if (!strcmp ("taskgroup", p))
26758 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
26759 else if (!strcmp ("thread_limit", p))
26760 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
26761 else if (!strcmp ("to", p))
26762 result = PRAGMA_OMP_CLAUSE_TO;
26763 break;
26764 case 'u':
26765 if (!strcmp ("uniform", p))
26766 result = PRAGMA_OMP_CLAUSE_UNIFORM;
26767 else if (!strcmp ("untied", p))
26768 result = PRAGMA_OMP_CLAUSE_UNTIED;
26769 break;
26770 }
26771 }
26772
26773 if (result != PRAGMA_OMP_CLAUSE_NONE)
26774 cp_lexer_consume_token (parser->lexer);
26775
26776 return result;
26777 }
26778
26779 /* Validate that a clause of the given type does not already exist. */
26780
26781 static void
26782 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
26783 const char *name, location_t location)
26784 {
26785 tree c;
26786
26787 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26788 if (OMP_CLAUSE_CODE (c) == code)
26789 {
26790 error_at (location, "too many %qs clauses", name);
26791 break;
26792 }
26793 }
26794
26795 /* OpenMP 2.5:
26796 variable-list:
26797 identifier
26798 variable-list , identifier
26799
26800 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
26801 colon). An opening parenthesis will have been consumed by the caller.
26802
26803 If KIND is nonzero, create the appropriate node and install the decl
26804 in OMP_CLAUSE_DECL and add the node to the head of the list.
26805
26806 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
26807 return the list created.
26808
26809 COLON can be NULL if only closing parenthesis should end the list,
26810 or pointer to bool which will receive false if the list is terminated
26811 by closing parenthesis or true if the list is terminated by colon. */
26812
26813 static tree
26814 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
26815 tree list, bool *colon)
26816 {
26817 cp_token *token;
26818 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26819 if (colon)
26820 {
26821 parser->colon_corrects_to_scope_p = false;
26822 *colon = false;
26823 }
26824 while (1)
26825 {
26826 tree name, decl;
26827
26828 token = cp_lexer_peek_token (parser->lexer);
26829 name = cp_parser_id_expression (parser, /*template_p=*/false,
26830 /*check_dependency_p=*/true,
26831 /*template_p=*/NULL,
26832 /*declarator_p=*/false,
26833 /*optional_p=*/false);
26834 if (name == error_mark_node)
26835 goto skip_comma;
26836
26837 decl = cp_parser_lookup_name_simple (parser, name, token->location);
26838 if (decl == error_mark_node)
26839 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
26840 token->location);
26841 else if (kind != 0)
26842 {
26843 switch (kind)
26844 {
26845 case OMP_CLAUSE_MAP:
26846 case OMP_CLAUSE_FROM:
26847 case OMP_CLAUSE_TO:
26848 case OMP_CLAUSE_DEPEND:
26849 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26850 {
26851 tree low_bound = NULL_TREE, length = NULL_TREE;
26852
26853 parser->colon_corrects_to_scope_p = false;
26854 cp_lexer_consume_token (parser->lexer);
26855 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26856 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
26857 NULL);
26858 if (!colon)
26859 parser->colon_corrects_to_scope_p
26860 = saved_colon_corrects_to_scope_p;
26861 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
26862 length = integer_one_node;
26863 else
26864 {
26865 /* Look for `:'. */
26866 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26867 goto skip_comma;
26868 if (!cp_lexer_next_token_is (parser->lexer,
26869 CPP_CLOSE_SQUARE))
26870 length = cp_parser_expression (parser,
26871 /*cast_p=*/false,
26872 NULL);
26873 }
26874 /* Look for the closing `]'. */
26875 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
26876 RT_CLOSE_SQUARE))
26877 goto skip_comma;
26878 decl = tree_cons (low_bound, length, decl);
26879 }
26880 break;
26881 default:
26882 break;
26883 }
26884
26885 tree u = build_omp_clause (token->location, kind);
26886 OMP_CLAUSE_DECL (u) = decl;
26887 OMP_CLAUSE_CHAIN (u) = list;
26888 list = u;
26889 }
26890 else
26891 list = tree_cons (decl, NULL_TREE, list);
26892
26893 get_comma:
26894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26895 break;
26896 cp_lexer_consume_token (parser->lexer);
26897 }
26898
26899 if (colon)
26900 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26901
26902 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26903 {
26904 *colon = true;
26905 cp_parser_require (parser, CPP_COLON, RT_COLON);
26906 return list;
26907 }
26908
26909 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26910 {
26911 int ending;
26912
26913 /* Try to resync to an unnested comma. Copied from
26914 cp_parser_parenthesized_expression_list. */
26915 skip_comma:
26916 if (colon)
26917 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26918 ending = cp_parser_skip_to_closing_parenthesis (parser,
26919 /*recovering=*/true,
26920 /*or_comma=*/true,
26921 /*consume_paren=*/true);
26922 if (ending < 0)
26923 goto get_comma;
26924 }
26925
26926 return list;
26927 }
26928
26929 /* Similarly, but expect leading and trailing parenthesis. This is a very
26930 common case for omp clauses. */
26931
26932 static tree
26933 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
26934 {
26935 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26936 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
26937 return list;
26938 }
26939
26940 /* OpenMP 3.0:
26941 collapse ( constant-expression ) */
26942
26943 static tree
26944 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
26945 {
26946 tree c, num;
26947 location_t loc;
26948 HOST_WIDE_INT n;
26949
26950 loc = cp_lexer_peek_token (parser->lexer)->location;
26951 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26952 return list;
26953
26954 num = cp_parser_constant_expression (parser, false, NULL);
26955
26956 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26957 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26958 /*or_comma=*/false,
26959 /*consume_paren=*/true);
26960
26961 if (num == error_mark_node)
26962 return list;
26963 num = fold_non_dependent_expr (num);
26964 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
26965 || !tree_fits_shwi_p (num)
26966 || (n = tree_low_cst (num, 0)) <= 0
26967 || (int) n != n)
26968 {
26969 error_at (loc, "collapse argument needs positive constant integer expression");
26970 return list;
26971 }
26972
26973 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
26974 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
26975 OMP_CLAUSE_CHAIN (c) = list;
26976 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
26977
26978 return c;
26979 }
26980
26981 /* OpenMP 2.5:
26982 default ( shared | none ) */
26983
26984 static tree
26985 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
26986 {
26987 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
26988 tree c;
26989
26990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26991 return list;
26992 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26993 {
26994 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26995 const char *p = IDENTIFIER_POINTER (id);
26996
26997 switch (p[0])
26998 {
26999 case 'n':
27000 if (strcmp ("none", p) != 0)
27001 goto invalid_kind;
27002 kind = OMP_CLAUSE_DEFAULT_NONE;
27003 break;
27004
27005 case 's':
27006 if (strcmp ("shared", p) != 0)
27007 goto invalid_kind;
27008 kind = OMP_CLAUSE_DEFAULT_SHARED;
27009 break;
27010
27011 default:
27012 goto invalid_kind;
27013 }
27014
27015 cp_lexer_consume_token (parser->lexer);
27016 }
27017 else
27018 {
27019 invalid_kind:
27020 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27021 }
27022
27023 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27024 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27025 /*or_comma=*/false,
27026 /*consume_paren=*/true);
27027
27028 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27029 return list;
27030
27031 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27032 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27033 OMP_CLAUSE_CHAIN (c) = list;
27034 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27035
27036 return c;
27037 }
27038
27039 /* OpenMP 3.1:
27040 final ( expression ) */
27041
27042 static tree
27043 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27044 {
27045 tree t, c;
27046
27047 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27048 return list;
27049
27050 t = cp_parser_condition (parser);
27051
27052 if (t == error_mark_node
27053 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27054 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27055 /*or_comma=*/false,
27056 /*consume_paren=*/true);
27057
27058 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27059
27060 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27061 OMP_CLAUSE_FINAL_EXPR (c) = t;
27062 OMP_CLAUSE_CHAIN (c) = list;
27063
27064 return c;
27065 }
27066
27067 /* OpenMP 2.5:
27068 if ( expression ) */
27069
27070 static tree
27071 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27072 {
27073 tree t, c;
27074
27075 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27076 return list;
27077
27078 t = cp_parser_condition (parser);
27079
27080 if (t == error_mark_node
27081 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27082 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27083 /*or_comma=*/false,
27084 /*consume_paren=*/true);
27085
27086 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27087
27088 c = build_omp_clause (location, OMP_CLAUSE_IF);
27089 OMP_CLAUSE_IF_EXPR (c) = t;
27090 OMP_CLAUSE_CHAIN (c) = list;
27091
27092 return c;
27093 }
27094
27095 /* OpenMP 3.1:
27096 mergeable */
27097
27098 static tree
27099 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27100 tree list, location_t location)
27101 {
27102 tree c;
27103
27104 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27105 location);
27106
27107 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27108 OMP_CLAUSE_CHAIN (c) = list;
27109 return c;
27110 }
27111
27112 /* OpenMP 2.5:
27113 nowait */
27114
27115 static tree
27116 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27117 tree list, location_t location)
27118 {
27119 tree c;
27120
27121 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27122
27123 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27124 OMP_CLAUSE_CHAIN (c) = list;
27125 return c;
27126 }
27127
27128 /* OpenMP 2.5:
27129 num_threads ( expression ) */
27130
27131 static tree
27132 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27133 location_t location)
27134 {
27135 tree t, c;
27136
27137 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27138 return list;
27139
27140 t = cp_parser_expression (parser, false, NULL);
27141
27142 if (t == error_mark_node
27143 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27145 /*or_comma=*/false,
27146 /*consume_paren=*/true);
27147
27148 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27149 "num_threads", location);
27150
27151 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27152 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27153 OMP_CLAUSE_CHAIN (c) = list;
27154
27155 return c;
27156 }
27157
27158 /* OpenMP 2.5:
27159 ordered */
27160
27161 static tree
27162 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27163 tree list, location_t location)
27164 {
27165 tree c;
27166
27167 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27168 "ordered", location);
27169
27170 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27171 OMP_CLAUSE_CHAIN (c) = list;
27172 return c;
27173 }
27174
27175 /* OpenMP 2.5:
27176 reduction ( reduction-operator : variable-list )
27177
27178 reduction-operator:
27179 One of: + * - & ^ | && ||
27180
27181 OpenMP 3.1:
27182
27183 reduction-operator:
27184 One of: + * - & ^ | && || min max
27185
27186 OpenMP 4.0:
27187
27188 reduction-operator:
27189 One of: + * - & ^ | && ||
27190 id-expression */
27191
27192 static tree
27193 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27194 {
27195 enum tree_code code = ERROR_MARK;
27196 tree nlist, c, id = NULL_TREE;
27197
27198 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27199 return list;
27200
27201 switch (cp_lexer_peek_token (parser->lexer)->type)
27202 {
27203 case CPP_PLUS: code = PLUS_EXPR; break;
27204 case CPP_MULT: code = MULT_EXPR; break;
27205 case CPP_MINUS: code = MINUS_EXPR; break;
27206 case CPP_AND: code = BIT_AND_EXPR; break;
27207 case CPP_XOR: code = BIT_XOR_EXPR; break;
27208 case CPP_OR: code = BIT_IOR_EXPR; break;
27209 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27210 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27211 default: break;
27212 }
27213
27214 if (code != ERROR_MARK)
27215 cp_lexer_consume_token (parser->lexer);
27216 else
27217 {
27218 bool saved_colon_corrects_to_scope_p;
27219 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27220 parser->colon_corrects_to_scope_p = false;
27221 id = cp_parser_id_expression (parser, /*template_p=*/false,
27222 /*check_dependency_p=*/true,
27223 /*template_p=*/NULL,
27224 /*declarator_p=*/false,
27225 /*optional_p=*/false);
27226 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27227 if (identifier_p (id))
27228 {
27229 const char *p = IDENTIFIER_POINTER (id);
27230
27231 if (strcmp (p, "min") == 0)
27232 code = MIN_EXPR;
27233 else if (strcmp (p, "max") == 0)
27234 code = MAX_EXPR;
27235 else if (id == ansi_opname (PLUS_EXPR))
27236 code = PLUS_EXPR;
27237 else if (id == ansi_opname (MULT_EXPR))
27238 code = MULT_EXPR;
27239 else if (id == ansi_opname (MINUS_EXPR))
27240 code = MINUS_EXPR;
27241 else if (id == ansi_opname (BIT_AND_EXPR))
27242 code = BIT_AND_EXPR;
27243 else if (id == ansi_opname (BIT_IOR_EXPR))
27244 code = BIT_IOR_EXPR;
27245 else if (id == ansi_opname (BIT_XOR_EXPR))
27246 code = BIT_XOR_EXPR;
27247 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27248 code = TRUTH_ANDIF_EXPR;
27249 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27250 code = TRUTH_ORIF_EXPR;
27251 id = omp_reduction_id (code, id, NULL_TREE);
27252 tree scope = parser->scope;
27253 if (scope)
27254 id = build_qualified_name (NULL_TREE, scope, id, false);
27255 parser->scope = NULL_TREE;
27256 parser->qualifying_scope = NULL_TREE;
27257 parser->object_scope = NULL_TREE;
27258 }
27259 else
27260 {
27261 error ("invalid reduction-identifier");
27262 resync_fail:
27263 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27264 /*or_comma=*/false,
27265 /*consume_paren=*/true);
27266 return list;
27267 }
27268 }
27269
27270 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27271 goto resync_fail;
27272
27273 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27274 NULL);
27275 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27276 {
27277 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27278 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27279 }
27280
27281 return nlist;
27282 }
27283
27284 /* OpenMP 2.5:
27285 schedule ( schedule-kind )
27286 schedule ( schedule-kind , expression )
27287
27288 schedule-kind:
27289 static | dynamic | guided | runtime | auto */
27290
27291 static tree
27292 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27293 {
27294 tree c, t;
27295
27296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27297 return list;
27298
27299 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27300
27301 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27302 {
27303 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27304 const char *p = IDENTIFIER_POINTER (id);
27305
27306 switch (p[0])
27307 {
27308 case 'd':
27309 if (strcmp ("dynamic", p) != 0)
27310 goto invalid_kind;
27311 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27312 break;
27313
27314 case 'g':
27315 if (strcmp ("guided", p) != 0)
27316 goto invalid_kind;
27317 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27318 break;
27319
27320 case 'r':
27321 if (strcmp ("runtime", p) != 0)
27322 goto invalid_kind;
27323 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27324 break;
27325
27326 default:
27327 goto invalid_kind;
27328 }
27329 }
27330 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27331 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27332 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27333 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27334 else
27335 goto invalid_kind;
27336 cp_lexer_consume_token (parser->lexer);
27337
27338 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27339 {
27340 cp_token *token;
27341 cp_lexer_consume_token (parser->lexer);
27342
27343 token = cp_lexer_peek_token (parser->lexer);
27344 t = cp_parser_assignment_expression (parser, false, NULL);
27345
27346 if (t == error_mark_node)
27347 goto resync_fail;
27348 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27349 error_at (token->location, "schedule %<runtime%> does not take "
27350 "a %<chunk_size%> parameter");
27351 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27352 error_at (token->location, "schedule %<auto%> does not take "
27353 "a %<chunk_size%> parameter");
27354 else
27355 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27356
27357 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27358 goto resync_fail;
27359 }
27360 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27361 goto resync_fail;
27362
27363 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27364 OMP_CLAUSE_CHAIN (c) = list;
27365 return c;
27366
27367 invalid_kind:
27368 cp_parser_error (parser, "invalid schedule kind");
27369 resync_fail:
27370 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27371 /*or_comma=*/false,
27372 /*consume_paren=*/true);
27373 return list;
27374 }
27375
27376 /* OpenMP 3.0:
27377 untied */
27378
27379 static tree
27380 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27381 tree list, location_t location)
27382 {
27383 tree c;
27384
27385 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27386
27387 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27388 OMP_CLAUSE_CHAIN (c) = list;
27389 return c;
27390 }
27391
27392 /* OpenMP 4.0:
27393 inbranch
27394 notinbranch */
27395
27396 static tree
27397 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27398 tree list, location_t location)
27399 {
27400 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27401 tree c = build_omp_clause (location, code);
27402 OMP_CLAUSE_CHAIN (c) = list;
27403 return c;
27404 }
27405
27406 /* OpenMP 4.0:
27407 parallel
27408 for
27409 sections
27410 taskgroup */
27411
27412 static tree
27413 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27414 enum omp_clause_code code,
27415 tree list, location_t location)
27416 {
27417 tree c = build_omp_clause (location, code);
27418 OMP_CLAUSE_CHAIN (c) = list;
27419 return c;
27420 }
27421
27422 /* OpenMP 4.0:
27423 num_teams ( expression ) */
27424
27425 static tree
27426 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27427 location_t location)
27428 {
27429 tree t, c;
27430
27431 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27432 return list;
27433
27434 t = cp_parser_expression (parser, false, NULL);
27435
27436 if (t == error_mark_node
27437 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27438 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27439 /*or_comma=*/false,
27440 /*consume_paren=*/true);
27441
27442 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27443 "num_teams", location);
27444
27445 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27446 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27447 OMP_CLAUSE_CHAIN (c) = list;
27448
27449 return c;
27450 }
27451
27452 /* OpenMP 4.0:
27453 thread_limit ( expression ) */
27454
27455 static tree
27456 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27457 location_t location)
27458 {
27459 tree t, c;
27460
27461 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27462 return list;
27463
27464 t = cp_parser_expression (parser, false, NULL);
27465
27466 if (t == error_mark_node
27467 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27468 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27469 /*or_comma=*/false,
27470 /*consume_paren=*/true);
27471
27472 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27473 "thread_limit", location);
27474
27475 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27476 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27477 OMP_CLAUSE_CHAIN (c) = list;
27478
27479 return c;
27480 }
27481
27482 /* OpenMP 4.0:
27483 aligned ( variable-list )
27484 aligned ( variable-list : constant-expression ) */
27485
27486 static tree
27487 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27488 {
27489 tree nlist, c, alignment = NULL_TREE;
27490 bool colon;
27491
27492 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27493 return list;
27494
27495 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27496 &colon);
27497
27498 if (colon)
27499 {
27500 alignment = cp_parser_constant_expression (parser, false, NULL);
27501
27502 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27504 /*or_comma=*/false,
27505 /*consume_paren=*/true);
27506
27507 if (alignment == error_mark_node)
27508 alignment = NULL_TREE;
27509 }
27510
27511 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27512 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27513
27514 return nlist;
27515 }
27516
27517 /* OpenMP 4.0:
27518 linear ( variable-list )
27519 linear ( variable-list : expression ) */
27520
27521 static tree
27522 cp_parser_omp_clause_linear (cp_parser *parser, tree list)
27523 {
27524 tree nlist, c, step = integer_one_node;
27525 bool colon;
27526
27527 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27528 return list;
27529
27530 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27531 &colon);
27532
27533 if (colon)
27534 {
27535 step = cp_parser_expression (parser, false, NULL);
27536
27537 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27538 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27539 /*or_comma=*/false,
27540 /*consume_paren=*/true);
27541
27542 if (step == error_mark_node)
27543 return list;
27544 }
27545
27546 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27547 OMP_CLAUSE_LINEAR_STEP (c) = step;
27548
27549 return nlist;
27550 }
27551
27552 /* OpenMP 4.0:
27553 safelen ( constant-expression ) */
27554
27555 static tree
27556 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27557 location_t location)
27558 {
27559 tree t, c;
27560
27561 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27562 return list;
27563
27564 t = cp_parser_constant_expression (parser, false, NULL);
27565
27566 if (t == error_mark_node
27567 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27568 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27569 /*or_comma=*/false,
27570 /*consume_paren=*/true);
27571
27572 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27573
27574 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27575 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27576 OMP_CLAUSE_CHAIN (c) = list;
27577
27578 return c;
27579 }
27580
27581 /* OpenMP 4.0:
27582 simdlen ( constant-expression ) */
27583
27584 static tree
27585 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27586 location_t location)
27587 {
27588 tree t, c;
27589
27590 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27591 return list;
27592
27593 t = cp_parser_constant_expression (parser, false, NULL);
27594
27595 if (t == error_mark_node
27596 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27597 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27598 /*or_comma=*/false,
27599 /*consume_paren=*/true);
27600
27601 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27602
27603 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27604 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27605 OMP_CLAUSE_CHAIN (c) = list;
27606
27607 return c;
27608 }
27609
27610 /* OpenMP 4.0:
27611 depend ( depend-kind : variable-list )
27612
27613 depend-kind:
27614 in | out | inout */
27615
27616 static tree
27617 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27618 {
27619 tree nlist, c;
27620 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27621
27622 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27623 return list;
27624
27625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27626 {
27627 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27628 const char *p = IDENTIFIER_POINTER (id);
27629
27630 if (strcmp ("in", p) == 0)
27631 kind = OMP_CLAUSE_DEPEND_IN;
27632 else if (strcmp ("inout", p) == 0)
27633 kind = OMP_CLAUSE_DEPEND_INOUT;
27634 else if (strcmp ("out", p) == 0)
27635 kind = OMP_CLAUSE_DEPEND_OUT;
27636 else
27637 goto invalid_kind;
27638 }
27639 else
27640 goto invalid_kind;
27641
27642 cp_lexer_consume_token (parser->lexer);
27643 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27644 goto resync_fail;
27645
27646 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27647 NULL);
27648
27649 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27650 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27651
27652 return nlist;
27653
27654 invalid_kind:
27655 cp_parser_error (parser, "invalid depend kind");
27656 resync_fail:
27657 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27658 /*or_comma=*/false,
27659 /*consume_paren=*/true);
27660 return list;
27661 }
27662
27663 /* OpenMP 4.0:
27664 map ( map-kind : variable-list )
27665 map ( variable-list )
27666
27667 map-kind:
27668 alloc | to | from | tofrom */
27669
27670 static tree
27671 cp_parser_omp_clause_map (cp_parser *parser, tree list)
27672 {
27673 tree nlist, c;
27674 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
27675
27676 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27677 return list;
27678
27679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
27680 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
27681 {
27682 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27683 const char *p = IDENTIFIER_POINTER (id);
27684
27685 if (strcmp ("alloc", p) == 0)
27686 kind = OMP_CLAUSE_MAP_ALLOC;
27687 else if (strcmp ("to", p) == 0)
27688 kind = OMP_CLAUSE_MAP_TO;
27689 else if (strcmp ("from", p) == 0)
27690 kind = OMP_CLAUSE_MAP_FROM;
27691 else if (strcmp ("tofrom", p) == 0)
27692 kind = OMP_CLAUSE_MAP_TOFROM;
27693 else
27694 {
27695 cp_parser_error (parser, "invalid map kind");
27696 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27697 /*or_comma=*/false,
27698 /*consume_paren=*/true);
27699 return list;
27700 }
27701 cp_lexer_consume_token (parser->lexer);
27702 cp_lexer_consume_token (parser->lexer);
27703 }
27704
27705 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
27706 NULL);
27707
27708 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27709 OMP_CLAUSE_MAP_KIND (c) = kind;
27710
27711 return nlist;
27712 }
27713
27714 /* OpenMP 4.0:
27715 device ( expression ) */
27716
27717 static tree
27718 cp_parser_omp_clause_device (cp_parser *parser, tree list,
27719 location_t location)
27720 {
27721 tree t, c;
27722
27723 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27724 return list;
27725
27726 t = cp_parser_expression (parser, false, NULL);
27727
27728 if (t == error_mark_node
27729 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27730 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27731 /*or_comma=*/false,
27732 /*consume_paren=*/true);
27733
27734 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
27735 "device", location);
27736
27737 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
27738 OMP_CLAUSE_DEVICE_ID (c) = t;
27739 OMP_CLAUSE_CHAIN (c) = list;
27740
27741 return c;
27742 }
27743
27744 /* OpenMP 4.0:
27745 dist_schedule ( static )
27746 dist_schedule ( static , expression ) */
27747
27748 static tree
27749 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
27750 location_t location)
27751 {
27752 tree c, t;
27753
27754 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27755 return list;
27756
27757 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
27758
27759 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27760 goto invalid_kind;
27761 cp_lexer_consume_token (parser->lexer);
27762
27763 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27764 {
27765 cp_lexer_consume_token (parser->lexer);
27766
27767 t = cp_parser_assignment_expression (parser, false, NULL);
27768
27769 if (t == error_mark_node)
27770 goto resync_fail;
27771 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
27772
27773 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27774 goto resync_fail;
27775 }
27776 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27777 goto resync_fail;
27778
27779 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
27780 location);
27781 OMP_CLAUSE_CHAIN (c) = list;
27782 return c;
27783
27784 invalid_kind:
27785 cp_parser_error (parser, "invalid dist_schedule kind");
27786 resync_fail:
27787 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27788 /*or_comma=*/false,
27789 /*consume_paren=*/true);
27790 return list;
27791 }
27792
27793 /* OpenMP 4.0:
27794 proc_bind ( proc-bind-kind )
27795
27796 proc-bind-kind:
27797 master | close | spread */
27798
27799 static tree
27800 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
27801 location_t location)
27802 {
27803 tree c;
27804 enum omp_clause_proc_bind_kind kind;
27805
27806 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27807 return list;
27808
27809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27810 {
27811 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27812 const char *p = IDENTIFIER_POINTER (id);
27813
27814 if (strcmp ("master", p) == 0)
27815 kind = OMP_CLAUSE_PROC_BIND_MASTER;
27816 else if (strcmp ("close", p) == 0)
27817 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
27818 else if (strcmp ("spread", p) == 0)
27819 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
27820 else
27821 goto invalid_kind;
27822 }
27823 else
27824 goto invalid_kind;
27825
27826 cp_lexer_consume_token (parser->lexer);
27827 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27828 goto resync_fail;
27829
27830 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
27831 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
27832 location);
27833 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
27834 OMP_CLAUSE_CHAIN (c) = list;
27835 return c;
27836
27837 invalid_kind:
27838 cp_parser_error (parser, "invalid depend kind");
27839 resync_fail:
27840 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27841 /*or_comma=*/false,
27842 /*consume_paren=*/true);
27843 return list;
27844 }
27845
27846 /* Parse all OpenMP clauses. The set clauses allowed by the directive
27847 is a bitmask in MASK. Return the list of clauses found; the result
27848 of clause default goes in *pdefault. */
27849
27850 static tree
27851 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
27852 const char *where, cp_token *pragma_tok,
27853 bool finish_p = true)
27854 {
27855 tree clauses = NULL;
27856 bool first = true;
27857 cp_token *token = NULL;
27858
27859 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
27860 {
27861 pragma_omp_clause c_kind;
27862 const char *c_name;
27863 tree prev = clauses;
27864
27865 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27866 cp_lexer_consume_token (parser->lexer);
27867
27868 token = cp_lexer_peek_token (parser->lexer);
27869 c_kind = cp_parser_omp_clause_name (parser);
27870
27871 switch (c_kind)
27872 {
27873 case PRAGMA_OMP_CLAUSE_COLLAPSE:
27874 clauses = cp_parser_omp_clause_collapse (parser, clauses,
27875 token->location);
27876 c_name = "collapse";
27877 break;
27878 case PRAGMA_OMP_CLAUSE_COPYIN:
27879 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
27880 c_name = "copyin";
27881 break;
27882 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
27883 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
27884 clauses);
27885 c_name = "copyprivate";
27886 break;
27887 case PRAGMA_OMP_CLAUSE_DEFAULT:
27888 clauses = cp_parser_omp_clause_default (parser, clauses,
27889 token->location);
27890 c_name = "default";
27891 break;
27892 case PRAGMA_OMP_CLAUSE_FINAL:
27893 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
27894 c_name = "final";
27895 break;
27896 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
27897 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
27898 clauses);
27899 c_name = "firstprivate";
27900 break;
27901 case PRAGMA_OMP_CLAUSE_IF:
27902 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
27903 c_name = "if";
27904 break;
27905 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
27906 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
27907 clauses);
27908 c_name = "lastprivate";
27909 break;
27910 case PRAGMA_OMP_CLAUSE_MERGEABLE:
27911 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
27912 token->location);
27913 c_name = "mergeable";
27914 break;
27915 case PRAGMA_OMP_CLAUSE_NOWAIT:
27916 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
27917 c_name = "nowait";
27918 break;
27919 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
27920 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
27921 token->location);
27922 c_name = "num_threads";
27923 break;
27924 case PRAGMA_OMP_CLAUSE_ORDERED:
27925 clauses = cp_parser_omp_clause_ordered (parser, clauses,
27926 token->location);
27927 c_name = "ordered";
27928 break;
27929 case PRAGMA_OMP_CLAUSE_PRIVATE:
27930 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
27931 clauses);
27932 c_name = "private";
27933 break;
27934 case PRAGMA_OMP_CLAUSE_REDUCTION:
27935 clauses = cp_parser_omp_clause_reduction (parser, clauses);
27936 c_name = "reduction";
27937 break;
27938 case PRAGMA_OMP_CLAUSE_SCHEDULE:
27939 clauses = cp_parser_omp_clause_schedule (parser, clauses,
27940 token->location);
27941 c_name = "schedule";
27942 break;
27943 case PRAGMA_OMP_CLAUSE_SHARED:
27944 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
27945 clauses);
27946 c_name = "shared";
27947 break;
27948 case PRAGMA_OMP_CLAUSE_UNTIED:
27949 clauses = cp_parser_omp_clause_untied (parser, clauses,
27950 token->location);
27951 c_name = "untied";
27952 break;
27953 case PRAGMA_OMP_CLAUSE_INBRANCH:
27954 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
27955 clauses, token->location);
27956 c_name = "inbranch";
27957 break;
27958 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
27959 clauses = cp_parser_omp_clause_branch (parser,
27960 OMP_CLAUSE_NOTINBRANCH,
27961 clauses, token->location);
27962 c_name = "notinbranch";
27963 break;
27964 case PRAGMA_OMP_CLAUSE_PARALLEL:
27965 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
27966 clauses, token->location);
27967 c_name = "parallel";
27968 if (!first)
27969 {
27970 clause_not_first:
27971 error_at (token->location, "%qs must be the first clause of %qs",
27972 c_name, where);
27973 clauses = prev;
27974 }
27975 break;
27976 case PRAGMA_OMP_CLAUSE_FOR:
27977 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
27978 clauses, token->location);
27979 c_name = "for";
27980 if (!first)
27981 goto clause_not_first;
27982 break;
27983 case PRAGMA_OMP_CLAUSE_SECTIONS:
27984 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
27985 clauses, token->location);
27986 c_name = "sections";
27987 if (!first)
27988 goto clause_not_first;
27989 break;
27990 case PRAGMA_OMP_CLAUSE_TASKGROUP:
27991 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
27992 clauses, token->location);
27993 c_name = "taskgroup";
27994 if (!first)
27995 goto clause_not_first;
27996 break;
27997 case PRAGMA_OMP_CLAUSE_TO:
27998 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
27999 clauses);
28000 c_name = "to";
28001 break;
28002 case PRAGMA_OMP_CLAUSE_FROM:
28003 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28004 clauses);
28005 c_name = "from";
28006 break;
28007 case PRAGMA_OMP_CLAUSE_UNIFORM:
28008 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28009 clauses);
28010 c_name = "uniform";
28011 break;
28012 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28013 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28014 token->location);
28015 c_name = "num_teams";
28016 break;
28017 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28018 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28019 token->location);
28020 c_name = "thread_limit";
28021 break;
28022 case PRAGMA_OMP_CLAUSE_ALIGNED:
28023 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28024 c_name = "aligned";
28025 break;
28026 case PRAGMA_OMP_CLAUSE_LINEAR:
28027 clauses = cp_parser_omp_clause_linear (parser, clauses);
28028 c_name = "linear";
28029 break;
28030 case PRAGMA_OMP_CLAUSE_DEPEND:
28031 clauses = cp_parser_omp_clause_depend (parser, clauses);
28032 c_name = "depend";
28033 break;
28034 case PRAGMA_OMP_CLAUSE_MAP:
28035 clauses = cp_parser_omp_clause_map (parser, clauses);
28036 c_name = "map";
28037 break;
28038 case PRAGMA_OMP_CLAUSE_DEVICE:
28039 clauses = cp_parser_omp_clause_device (parser, clauses,
28040 token->location);
28041 c_name = "device";
28042 break;
28043 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28044 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28045 token->location);
28046 c_name = "dist_schedule";
28047 break;
28048 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28049 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28050 token->location);
28051 c_name = "proc_bind";
28052 break;
28053 case PRAGMA_OMP_CLAUSE_SAFELEN:
28054 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28055 token->location);
28056 c_name = "safelen";
28057 break;
28058 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28059 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28060 token->location);
28061 c_name = "simdlen";
28062 break;
28063 default:
28064 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28065 goto saw_error;
28066 }
28067
28068 first = false;
28069
28070 if (((mask >> c_kind) & 1) == 0)
28071 {
28072 /* Remove the invalid clause(s) from the list to avoid
28073 confusing the rest of the compiler. */
28074 clauses = prev;
28075 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28076 }
28077 }
28078 saw_error:
28079 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28080 if (finish_p)
28081 return finish_omp_clauses (clauses);
28082 return clauses;
28083 }
28084
28085 /* OpenMP 2.5:
28086 structured-block:
28087 statement
28088
28089 In practice, we're also interested in adding the statement to an
28090 outer node. So it is convenient if we work around the fact that
28091 cp_parser_statement calls add_stmt. */
28092
28093 static unsigned
28094 cp_parser_begin_omp_structured_block (cp_parser *parser)
28095 {
28096 unsigned save = parser->in_statement;
28097
28098 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28099 This preserves the "not within loop or switch" style error messages
28100 for nonsense cases like
28101 void foo() {
28102 #pragma omp single
28103 break;
28104 }
28105 */
28106 if (parser->in_statement)
28107 parser->in_statement = IN_OMP_BLOCK;
28108
28109 return save;
28110 }
28111
28112 static void
28113 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28114 {
28115 parser->in_statement = save;
28116 }
28117
28118 static tree
28119 cp_parser_omp_structured_block (cp_parser *parser)
28120 {
28121 tree stmt = begin_omp_structured_block ();
28122 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28123
28124 cp_parser_statement (parser, NULL_TREE, false, NULL);
28125
28126 cp_parser_end_omp_structured_block (parser, save);
28127 return finish_omp_structured_block (stmt);
28128 }
28129
28130 /* OpenMP 2.5:
28131 # pragma omp atomic new-line
28132 expression-stmt
28133
28134 expression-stmt:
28135 x binop= expr | x++ | ++x | x-- | --x
28136 binop:
28137 +, *, -, /, &, ^, |, <<, >>
28138
28139 where x is an lvalue expression with scalar type.
28140
28141 OpenMP 3.1:
28142 # pragma omp atomic new-line
28143 update-stmt
28144
28145 # pragma omp atomic read new-line
28146 read-stmt
28147
28148 # pragma omp atomic write new-line
28149 write-stmt
28150
28151 # pragma omp atomic update new-line
28152 update-stmt
28153
28154 # pragma omp atomic capture new-line
28155 capture-stmt
28156
28157 # pragma omp atomic capture new-line
28158 capture-block
28159
28160 read-stmt:
28161 v = x
28162 write-stmt:
28163 x = expr
28164 update-stmt:
28165 expression-stmt | x = x binop expr
28166 capture-stmt:
28167 v = expression-stmt
28168 capture-block:
28169 { v = x; update-stmt; } | { update-stmt; v = x; }
28170
28171 OpenMP 4.0:
28172 update-stmt:
28173 expression-stmt | x = x binop expr | x = expr binop x
28174 capture-stmt:
28175 v = update-stmt
28176 capture-block:
28177 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28178
28179 where x and v are lvalue expressions with scalar type. */
28180
28181 static void
28182 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28183 {
28184 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28185 tree rhs1 = NULL_TREE, orig_lhs;
28186 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28187 bool structured_block = false;
28188 bool seq_cst = false;
28189
28190 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28191 {
28192 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28193 const char *p = IDENTIFIER_POINTER (id);
28194
28195 if (!strcmp (p, "read"))
28196 code = OMP_ATOMIC_READ;
28197 else if (!strcmp (p, "write"))
28198 code = NOP_EXPR;
28199 else if (!strcmp (p, "update"))
28200 code = OMP_ATOMIC;
28201 else if (!strcmp (p, "capture"))
28202 code = OMP_ATOMIC_CAPTURE_NEW;
28203 else
28204 p = NULL;
28205 if (p)
28206 cp_lexer_consume_token (parser->lexer);
28207 }
28208
28209 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28210 {
28211 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28212 const char *p = IDENTIFIER_POINTER (id);
28213
28214 if (!strcmp (p, "seq_cst"))
28215 {
28216 seq_cst = true;
28217 cp_lexer_consume_token (parser->lexer);
28218 }
28219 }
28220 cp_parser_require_pragma_eol (parser, pragma_tok);
28221
28222 switch (code)
28223 {
28224 case OMP_ATOMIC_READ:
28225 case NOP_EXPR: /* atomic write */
28226 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28227 /*cast_p=*/false, NULL);
28228 if (v == error_mark_node)
28229 goto saw_error;
28230 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28231 goto saw_error;
28232 if (code == NOP_EXPR)
28233 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28234 else
28235 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28236 /*cast_p=*/false, NULL);
28237 if (lhs == error_mark_node)
28238 goto saw_error;
28239 if (code == NOP_EXPR)
28240 {
28241 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28242 opcode. */
28243 code = OMP_ATOMIC;
28244 rhs = lhs;
28245 lhs = v;
28246 v = NULL_TREE;
28247 }
28248 goto done;
28249 case OMP_ATOMIC_CAPTURE_NEW:
28250 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28251 {
28252 cp_lexer_consume_token (parser->lexer);
28253 structured_block = true;
28254 }
28255 else
28256 {
28257 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28258 /*cast_p=*/false, NULL);
28259 if (v == error_mark_node)
28260 goto saw_error;
28261 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28262 goto saw_error;
28263 }
28264 default:
28265 break;
28266 }
28267
28268 restart:
28269 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28270 /*cast_p=*/false, NULL);
28271 orig_lhs = lhs;
28272 switch (TREE_CODE (lhs))
28273 {
28274 case ERROR_MARK:
28275 goto saw_error;
28276
28277 case POSTINCREMENT_EXPR:
28278 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28279 code = OMP_ATOMIC_CAPTURE_OLD;
28280 /* FALLTHROUGH */
28281 case PREINCREMENT_EXPR:
28282 lhs = TREE_OPERAND (lhs, 0);
28283 opcode = PLUS_EXPR;
28284 rhs = integer_one_node;
28285 break;
28286
28287 case POSTDECREMENT_EXPR:
28288 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28289 code = OMP_ATOMIC_CAPTURE_OLD;
28290 /* FALLTHROUGH */
28291 case PREDECREMENT_EXPR:
28292 lhs = TREE_OPERAND (lhs, 0);
28293 opcode = MINUS_EXPR;
28294 rhs = integer_one_node;
28295 break;
28296
28297 case COMPOUND_EXPR:
28298 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28299 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28300 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28301 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28302 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28303 (TREE_OPERAND (lhs, 1), 0), 0)))
28304 == BOOLEAN_TYPE)
28305 /* Undo effects of boolean_increment for post {in,de}crement. */
28306 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28307 /* FALLTHRU */
28308 case MODIFY_EXPR:
28309 if (TREE_CODE (lhs) == MODIFY_EXPR
28310 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28311 {
28312 /* Undo effects of boolean_increment. */
28313 if (integer_onep (TREE_OPERAND (lhs, 1)))
28314 {
28315 /* This is pre or post increment. */
28316 rhs = TREE_OPERAND (lhs, 1);
28317 lhs = TREE_OPERAND (lhs, 0);
28318 opcode = NOP_EXPR;
28319 if (code == OMP_ATOMIC_CAPTURE_NEW
28320 && !structured_block
28321 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28322 code = OMP_ATOMIC_CAPTURE_OLD;
28323 break;
28324 }
28325 }
28326 /* FALLTHRU */
28327 default:
28328 switch (cp_lexer_peek_token (parser->lexer)->type)
28329 {
28330 case CPP_MULT_EQ:
28331 opcode = MULT_EXPR;
28332 break;
28333 case CPP_DIV_EQ:
28334 opcode = TRUNC_DIV_EXPR;
28335 break;
28336 case CPP_PLUS_EQ:
28337 opcode = PLUS_EXPR;
28338 break;
28339 case CPP_MINUS_EQ:
28340 opcode = MINUS_EXPR;
28341 break;
28342 case CPP_LSHIFT_EQ:
28343 opcode = LSHIFT_EXPR;
28344 break;
28345 case CPP_RSHIFT_EQ:
28346 opcode = RSHIFT_EXPR;
28347 break;
28348 case CPP_AND_EQ:
28349 opcode = BIT_AND_EXPR;
28350 break;
28351 case CPP_OR_EQ:
28352 opcode = BIT_IOR_EXPR;
28353 break;
28354 case CPP_XOR_EQ:
28355 opcode = BIT_XOR_EXPR;
28356 break;
28357 case CPP_EQ:
28358 enum cp_parser_prec oprec;
28359 cp_token *token;
28360 cp_lexer_consume_token (parser->lexer);
28361 cp_parser_parse_tentatively (parser);
28362 rhs1 = cp_parser_simple_cast_expression (parser);
28363 if (rhs1 == error_mark_node)
28364 {
28365 cp_parser_abort_tentative_parse (parser);
28366 cp_parser_simple_cast_expression (parser);
28367 goto saw_error;
28368 }
28369 token = cp_lexer_peek_token (parser->lexer);
28370 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28371 {
28372 cp_parser_abort_tentative_parse (parser);
28373 cp_parser_parse_tentatively (parser);
28374 rhs = cp_parser_binary_expression (parser, false, true,
28375 PREC_NOT_OPERATOR, NULL);
28376 if (rhs == error_mark_node)
28377 {
28378 cp_parser_abort_tentative_parse (parser);
28379 cp_parser_binary_expression (parser, false, true,
28380 PREC_NOT_OPERATOR, NULL);
28381 goto saw_error;
28382 }
28383 switch (TREE_CODE (rhs))
28384 {
28385 case MULT_EXPR:
28386 case TRUNC_DIV_EXPR:
28387 case PLUS_EXPR:
28388 case MINUS_EXPR:
28389 case LSHIFT_EXPR:
28390 case RSHIFT_EXPR:
28391 case BIT_AND_EXPR:
28392 case BIT_IOR_EXPR:
28393 case BIT_XOR_EXPR:
28394 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28395 {
28396 if (cp_parser_parse_definitely (parser))
28397 {
28398 opcode = TREE_CODE (rhs);
28399 rhs1 = TREE_OPERAND (rhs, 0);
28400 rhs = TREE_OPERAND (rhs, 1);
28401 goto stmt_done;
28402 }
28403 else
28404 goto saw_error;
28405 }
28406 break;
28407 default:
28408 break;
28409 }
28410 cp_parser_abort_tentative_parse (parser);
28411 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28412 {
28413 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28414 if (rhs == error_mark_node)
28415 goto saw_error;
28416 opcode = NOP_EXPR;
28417 rhs1 = NULL_TREE;
28418 goto stmt_done;
28419 }
28420 cp_parser_error (parser,
28421 "invalid form of %<#pragma omp atomic%>");
28422 goto saw_error;
28423 }
28424 if (!cp_parser_parse_definitely (parser))
28425 goto saw_error;
28426 switch (token->type)
28427 {
28428 case CPP_SEMICOLON:
28429 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28430 {
28431 code = OMP_ATOMIC_CAPTURE_OLD;
28432 v = lhs;
28433 lhs = NULL_TREE;
28434 lhs1 = rhs1;
28435 rhs1 = NULL_TREE;
28436 cp_lexer_consume_token (parser->lexer);
28437 goto restart;
28438 }
28439 else if (structured_block)
28440 {
28441 opcode = NOP_EXPR;
28442 rhs = rhs1;
28443 rhs1 = NULL_TREE;
28444 goto stmt_done;
28445 }
28446 cp_parser_error (parser,
28447 "invalid form of %<#pragma omp atomic%>");
28448 goto saw_error;
28449 case CPP_MULT:
28450 opcode = MULT_EXPR;
28451 break;
28452 case CPP_DIV:
28453 opcode = TRUNC_DIV_EXPR;
28454 break;
28455 case CPP_PLUS:
28456 opcode = PLUS_EXPR;
28457 break;
28458 case CPP_MINUS:
28459 opcode = MINUS_EXPR;
28460 break;
28461 case CPP_LSHIFT:
28462 opcode = LSHIFT_EXPR;
28463 break;
28464 case CPP_RSHIFT:
28465 opcode = RSHIFT_EXPR;
28466 break;
28467 case CPP_AND:
28468 opcode = BIT_AND_EXPR;
28469 break;
28470 case CPP_OR:
28471 opcode = BIT_IOR_EXPR;
28472 break;
28473 case CPP_XOR:
28474 opcode = BIT_XOR_EXPR;
28475 break;
28476 default:
28477 cp_parser_error (parser,
28478 "invalid operator for %<#pragma omp atomic%>");
28479 goto saw_error;
28480 }
28481 oprec = TOKEN_PRECEDENCE (token);
28482 gcc_assert (oprec != PREC_NOT_OPERATOR);
28483 if (commutative_tree_code (opcode))
28484 oprec = (enum cp_parser_prec) (oprec - 1);
28485 cp_lexer_consume_token (parser->lexer);
28486 rhs = cp_parser_binary_expression (parser, false, false,
28487 oprec, NULL);
28488 if (rhs == error_mark_node)
28489 goto saw_error;
28490 goto stmt_done;
28491 /* FALLTHROUGH */
28492 default:
28493 cp_parser_error (parser,
28494 "invalid operator for %<#pragma omp atomic%>");
28495 goto saw_error;
28496 }
28497 cp_lexer_consume_token (parser->lexer);
28498
28499 rhs = cp_parser_expression (parser, false, NULL);
28500 if (rhs == error_mark_node)
28501 goto saw_error;
28502 break;
28503 }
28504 stmt_done:
28505 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28506 {
28507 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28508 goto saw_error;
28509 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28510 /*cast_p=*/false, NULL);
28511 if (v == error_mark_node)
28512 goto saw_error;
28513 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28514 goto saw_error;
28515 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28516 /*cast_p=*/false, NULL);
28517 if (lhs1 == error_mark_node)
28518 goto saw_error;
28519 }
28520 if (structured_block)
28521 {
28522 cp_parser_consume_semicolon_at_end_of_statement (parser);
28523 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28524 }
28525 done:
28526 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28527 if (!structured_block)
28528 cp_parser_consume_semicolon_at_end_of_statement (parser);
28529 return;
28530
28531 saw_error:
28532 cp_parser_skip_to_end_of_block_or_statement (parser);
28533 if (structured_block)
28534 {
28535 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28536 cp_lexer_consume_token (parser->lexer);
28537 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28538 {
28539 cp_parser_skip_to_end_of_block_or_statement (parser);
28540 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28541 cp_lexer_consume_token (parser->lexer);
28542 }
28543 }
28544 }
28545
28546
28547 /* OpenMP 2.5:
28548 # pragma omp barrier new-line */
28549
28550 static void
28551 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28552 {
28553 cp_parser_require_pragma_eol (parser, pragma_tok);
28554 finish_omp_barrier ();
28555 }
28556
28557 /* OpenMP 2.5:
28558 # pragma omp critical [(name)] new-line
28559 structured-block */
28560
28561 static tree
28562 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28563 {
28564 tree stmt, name = NULL;
28565
28566 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28567 {
28568 cp_lexer_consume_token (parser->lexer);
28569
28570 name = cp_parser_identifier (parser);
28571
28572 if (name == error_mark_node
28573 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28574 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28575 /*or_comma=*/false,
28576 /*consume_paren=*/true);
28577 if (name == error_mark_node)
28578 name = NULL;
28579 }
28580 cp_parser_require_pragma_eol (parser, pragma_tok);
28581
28582 stmt = cp_parser_omp_structured_block (parser);
28583 return c_finish_omp_critical (input_location, stmt, name);
28584 }
28585
28586 /* OpenMP 2.5:
28587 # pragma omp flush flush-vars[opt] new-line
28588
28589 flush-vars:
28590 ( variable-list ) */
28591
28592 static void
28593 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28594 {
28595 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28596 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28597 cp_parser_require_pragma_eol (parser, pragma_tok);
28598
28599 finish_omp_flush ();
28600 }
28601
28602 /* Helper function, to parse omp for increment expression. */
28603
28604 static tree
28605 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
28606 {
28607 tree cond = cp_parser_binary_expression (parser, false, true,
28608 PREC_NOT_OPERATOR, NULL);
28609 if (cond == error_mark_node
28610 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28611 {
28612 cp_parser_skip_to_end_of_statement (parser);
28613 return error_mark_node;
28614 }
28615
28616 switch (TREE_CODE (cond))
28617 {
28618 case GT_EXPR:
28619 case GE_EXPR:
28620 case LT_EXPR:
28621 case LE_EXPR:
28622 break;
28623 case NE_EXPR:
28624 if (code == CILK_SIMD)
28625 break;
28626 /* Fall through: OpenMP disallows NE_EXPR. */
28627 default:
28628 return error_mark_node;
28629 }
28630
28631 /* If decl is an iterator, preserve LHS and RHS of the relational
28632 expr until finish_omp_for. */
28633 if (decl
28634 && (type_dependent_expression_p (decl)
28635 || CLASS_TYPE_P (TREE_TYPE (decl))))
28636 return cond;
28637
28638 return build_x_binary_op (input_location, TREE_CODE (cond),
28639 TREE_OPERAND (cond, 0), ERROR_MARK,
28640 TREE_OPERAND (cond, 1), ERROR_MARK,
28641 /*overload=*/NULL, tf_warning_or_error);
28642 }
28643
28644 /* Helper function, to parse omp for increment expression. */
28645
28646 static tree
28647 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
28648 {
28649 cp_token *token = cp_lexer_peek_token (parser->lexer);
28650 enum tree_code op;
28651 tree lhs, rhs;
28652 cp_id_kind idk;
28653 bool decl_first;
28654
28655 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28656 {
28657 op = (token->type == CPP_PLUS_PLUS
28658 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
28659 cp_lexer_consume_token (parser->lexer);
28660 lhs = cp_parser_simple_cast_expression (parser);
28661 if (lhs != decl)
28662 return error_mark_node;
28663 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28664 }
28665
28666 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
28667 if (lhs != decl)
28668 return error_mark_node;
28669
28670 token = cp_lexer_peek_token (parser->lexer);
28671 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28672 {
28673 op = (token->type == CPP_PLUS_PLUS
28674 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
28675 cp_lexer_consume_token (parser->lexer);
28676 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28677 }
28678
28679 op = cp_parser_assignment_operator_opt (parser);
28680 if (op == ERROR_MARK)
28681 return error_mark_node;
28682
28683 if (op != NOP_EXPR)
28684 {
28685 rhs = cp_parser_assignment_expression (parser, false, NULL);
28686 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
28687 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
28688 }
28689
28690 lhs = cp_parser_binary_expression (parser, false, false,
28691 PREC_ADDITIVE_EXPRESSION, NULL);
28692 token = cp_lexer_peek_token (parser->lexer);
28693 decl_first = lhs == decl;
28694 if (decl_first)
28695 lhs = NULL_TREE;
28696 if (token->type != CPP_PLUS
28697 && token->type != CPP_MINUS)
28698 return error_mark_node;
28699
28700 do
28701 {
28702 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
28703 cp_lexer_consume_token (parser->lexer);
28704 rhs = cp_parser_binary_expression (parser, false, false,
28705 PREC_ADDITIVE_EXPRESSION, NULL);
28706 token = cp_lexer_peek_token (parser->lexer);
28707 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
28708 {
28709 if (lhs == NULL_TREE)
28710 {
28711 if (op == PLUS_EXPR)
28712 lhs = rhs;
28713 else
28714 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
28715 tf_warning_or_error);
28716 }
28717 else
28718 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
28719 ERROR_MARK, NULL, tf_warning_or_error);
28720 }
28721 }
28722 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
28723
28724 if (!decl_first)
28725 {
28726 if (rhs != decl || op == MINUS_EXPR)
28727 return error_mark_node;
28728 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
28729 }
28730 else
28731 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
28732
28733 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
28734 }
28735
28736 /* Parse the initialization statement of either an OpenMP for loop or
28737 a Cilk Plus for loop.
28738
28739 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
28740 Plus.
28741
28742 Return true if the resulting construct should have an
28743 OMP_CLAUSE_PRIVATE added to it. */
28744
28745 static bool
28746 cp_parser_omp_for_loop_init (cp_parser *parser,
28747 bool parsing_openmp,
28748 tree &this_pre_body,
28749 vec<tree, va_gc> *for_block,
28750 tree &init,
28751 tree &decl,
28752 tree &real_decl)
28753 {
28754 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28755 return false;
28756
28757 bool add_private_clause = false;
28758
28759 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
28760
28761 init-expr:
28762 var = lb
28763 integer-type var = lb
28764 random-access-iterator-type var = lb
28765 pointer-type var = lb
28766 */
28767 cp_decl_specifier_seq type_specifiers;
28768
28769 /* First, try to parse as an initialized declaration. See
28770 cp_parser_condition, from whence the bulk of this is copied. */
28771
28772 cp_parser_parse_tentatively (parser);
28773 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
28774 /*is_trailing_return=*/false,
28775 &type_specifiers);
28776 if (cp_parser_parse_definitely (parser))
28777 {
28778 /* If parsing a type specifier seq succeeded, then this
28779 MUST be a initialized declaration. */
28780 tree asm_specification, attributes;
28781 cp_declarator *declarator;
28782
28783 declarator = cp_parser_declarator (parser,
28784 CP_PARSER_DECLARATOR_NAMED,
28785 /*ctor_dtor_or_conv_p=*/NULL,
28786 /*parenthesized_p=*/NULL,
28787 /*member_p=*/false);
28788 attributes = cp_parser_attributes_opt (parser);
28789 asm_specification = cp_parser_asm_specification_opt (parser);
28790
28791 if (declarator == cp_error_declarator)
28792 cp_parser_skip_to_end_of_statement (parser);
28793
28794 else
28795 {
28796 tree pushed_scope, auto_node;
28797
28798 decl = start_decl (declarator, &type_specifiers,
28799 SD_INITIALIZED, attributes,
28800 /*prefix_attributes=*/NULL_TREE,
28801 &pushed_scope);
28802
28803 auto_node = type_uses_auto (TREE_TYPE (decl));
28804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28805 {
28806 if (cp_lexer_next_token_is (parser->lexer,
28807 CPP_OPEN_PAREN))
28808 {
28809 if (parsing_openmp)
28810 error ("parenthesized initialization is not allowed in "
28811 "OpenMP %<for%> loop");
28812 else
28813 error ("parenthesized initialization is "
28814 "not allowed in for-loop");
28815 }
28816 else
28817 /* Trigger an error. */
28818 cp_parser_require (parser, CPP_EQ, RT_EQ);
28819
28820 init = error_mark_node;
28821 cp_parser_skip_to_end_of_statement (parser);
28822 }
28823 else if (CLASS_TYPE_P (TREE_TYPE (decl))
28824 || type_dependent_expression_p (decl)
28825 || auto_node)
28826 {
28827 bool is_direct_init, is_non_constant_init;
28828
28829 init = cp_parser_initializer (parser,
28830 &is_direct_init,
28831 &is_non_constant_init);
28832
28833 if (auto_node)
28834 {
28835 TREE_TYPE (decl)
28836 = do_auto_deduction (TREE_TYPE (decl), init,
28837 auto_node);
28838
28839 if (!CLASS_TYPE_P (TREE_TYPE (decl))
28840 && !type_dependent_expression_p (decl))
28841 goto non_class;
28842 }
28843
28844 cp_finish_decl (decl, init, !is_non_constant_init,
28845 asm_specification,
28846 LOOKUP_ONLYCONVERTING);
28847 if (CLASS_TYPE_P (TREE_TYPE (decl)))
28848 {
28849 vec_safe_push (for_block, this_pre_body);
28850 init = NULL_TREE;
28851 }
28852 else
28853 init = pop_stmt_list (this_pre_body);
28854 this_pre_body = NULL_TREE;
28855 }
28856 else
28857 {
28858 /* Consume '='. */
28859 cp_lexer_consume_token (parser->lexer);
28860 init = cp_parser_assignment_expression (parser, false, NULL);
28861
28862 non_class:
28863 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
28864 init = error_mark_node;
28865 else
28866 cp_finish_decl (decl, NULL_TREE,
28867 /*init_const_expr_p=*/false,
28868 asm_specification,
28869 LOOKUP_ONLYCONVERTING);
28870 }
28871
28872 if (pushed_scope)
28873 pop_scope (pushed_scope);
28874 }
28875 }
28876 else
28877 {
28878 cp_id_kind idk;
28879 /* If parsing a type specifier sequence failed, then
28880 this MUST be a simple expression. */
28881 cp_parser_parse_tentatively (parser);
28882 decl = cp_parser_primary_expression (parser, false, false,
28883 false, &idk);
28884 if (!cp_parser_error_occurred (parser)
28885 && decl
28886 && DECL_P (decl)
28887 && CLASS_TYPE_P (TREE_TYPE (decl)))
28888 {
28889 tree rhs;
28890
28891 cp_parser_parse_definitely (parser);
28892 cp_parser_require (parser, CPP_EQ, RT_EQ);
28893 rhs = cp_parser_assignment_expression (parser, false, NULL);
28894 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
28895 decl, NOP_EXPR,
28896 rhs,
28897 tf_warning_or_error));
28898 add_private_clause = true;
28899 }
28900 else
28901 {
28902 decl = NULL;
28903 cp_parser_abort_tentative_parse (parser);
28904 init = cp_parser_expression (parser, false, NULL);
28905 if (init)
28906 {
28907 if (TREE_CODE (init) == MODIFY_EXPR
28908 || TREE_CODE (init) == MODOP_EXPR)
28909 real_decl = TREE_OPERAND (init, 0);
28910 }
28911 }
28912 }
28913 return add_private_clause;
28914 }
28915
28916 /* Parse the restricted form of the for statement allowed by OpenMP. */
28917
28918 static tree
28919 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
28920 tree *cclauses)
28921 {
28922 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
28923 tree real_decl, initv, condv, incrv, declv;
28924 tree this_pre_body, cl;
28925 location_t loc_first;
28926 bool collapse_err = false;
28927 int i, collapse = 1, nbraces = 0;
28928 vec<tree, va_gc> *for_block = make_tree_vector ();
28929
28930 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
28931 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
28932 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
28933
28934 gcc_assert (collapse >= 1);
28935
28936 declv = make_tree_vec (collapse);
28937 initv = make_tree_vec (collapse);
28938 condv = make_tree_vec (collapse);
28939 incrv = make_tree_vec (collapse);
28940
28941 loc_first = cp_lexer_peek_token (parser->lexer)->location;
28942
28943 for (i = 0; i < collapse; i++)
28944 {
28945 int bracecount = 0;
28946 bool add_private_clause = false;
28947 location_t loc;
28948
28949 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28950 {
28951 cp_parser_error (parser, "for statement expected");
28952 return NULL;
28953 }
28954 loc = cp_lexer_consume_token (parser->lexer)->location;
28955
28956 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28957 return NULL;
28958
28959 init = decl = real_decl = NULL;
28960 this_pre_body = push_stmt_list ();
28961
28962 add_private_clause
28963 |= cp_parser_omp_for_loop_init (parser,
28964 /*parsing_openmp=*/code != CILK_SIMD,
28965 this_pre_body, for_block,
28966 init, decl, real_decl);
28967
28968 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28969 if (this_pre_body)
28970 {
28971 this_pre_body = pop_stmt_list (this_pre_body);
28972 if (pre_body)
28973 {
28974 tree t = pre_body;
28975 pre_body = push_stmt_list ();
28976 add_stmt (t);
28977 add_stmt (this_pre_body);
28978 pre_body = pop_stmt_list (pre_body);
28979 }
28980 else
28981 pre_body = this_pre_body;
28982 }
28983
28984 if (decl)
28985 real_decl = decl;
28986 if (cclauses != NULL
28987 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
28988 && real_decl != NULL_TREE)
28989 {
28990 tree *c;
28991 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
28992 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
28993 && OMP_CLAUSE_DECL (*c) == real_decl)
28994 {
28995 error_at (loc, "iteration variable %qD"
28996 " should not be firstprivate", real_decl);
28997 *c = OMP_CLAUSE_CHAIN (*c);
28998 }
28999 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29000 && OMP_CLAUSE_DECL (*c) == real_decl)
29001 {
29002 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29003 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29004 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29005 OMP_CLAUSE_DECL (l) = real_decl;
29006 OMP_CLAUSE_CHAIN (l) = clauses;
29007 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29008 clauses = l;
29009 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29010 CP_OMP_CLAUSE_INFO (*c) = NULL;
29011 add_private_clause = false;
29012 }
29013 else
29014 {
29015 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29016 && OMP_CLAUSE_DECL (*c) == real_decl)
29017 add_private_clause = false;
29018 c = &OMP_CLAUSE_CHAIN (*c);
29019 }
29020 }
29021
29022 if (add_private_clause)
29023 {
29024 tree c;
29025 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29026 {
29027 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29028 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29029 && OMP_CLAUSE_DECL (c) == decl)
29030 break;
29031 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29032 && OMP_CLAUSE_DECL (c) == decl)
29033 error_at (loc, "iteration variable %qD "
29034 "should not be firstprivate",
29035 decl);
29036 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29037 && OMP_CLAUSE_DECL (c) == decl)
29038 error_at (loc, "iteration variable %qD should not be reduction",
29039 decl);
29040 }
29041 if (c == NULL)
29042 {
29043 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29044 OMP_CLAUSE_DECL (c) = decl;
29045 c = finish_omp_clauses (c);
29046 if (c)
29047 {
29048 OMP_CLAUSE_CHAIN (c) = clauses;
29049 clauses = c;
29050 }
29051 }
29052 }
29053
29054 cond = NULL;
29055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29056 cond = cp_parser_omp_for_cond (parser, decl, code);
29057 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29058
29059 incr = NULL;
29060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29061 {
29062 /* If decl is an iterator, preserve the operator on decl
29063 until finish_omp_for. */
29064 if (real_decl
29065 && ((processing_template_decl
29066 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29067 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29068 incr = cp_parser_omp_for_incr (parser, real_decl);
29069 else
29070 incr = cp_parser_expression (parser, false, NULL);
29071 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29072 SET_EXPR_LOCATION (incr, input_location);
29073 }
29074
29075 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29076 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29077 /*or_comma=*/false,
29078 /*consume_paren=*/true);
29079
29080 TREE_VEC_ELT (declv, i) = decl;
29081 TREE_VEC_ELT (initv, i) = init;
29082 TREE_VEC_ELT (condv, i) = cond;
29083 TREE_VEC_ELT (incrv, i) = incr;
29084
29085 if (i == collapse - 1)
29086 break;
29087
29088 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29089 in between the collapsed for loops to be still considered perfectly
29090 nested. Hopefully the final version clarifies this.
29091 For now handle (multiple) {'s and empty statements. */
29092 cp_parser_parse_tentatively (parser);
29093 do
29094 {
29095 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29096 break;
29097 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29098 {
29099 cp_lexer_consume_token (parser->lexer);
29100 bracecount++;
29101 }
29102 else if (bracecount
29103 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29104 cp_lexer_consume_token (parser->lexer);
29105 else
29106 {
29107 loc = cp_lexer_peek_token (parser->lexer)->location;
29108 error_at (loc, "not enough collapsed for loops");
29109 collapse_err = true;
29110 cp_parser_abort_tentative_parse (parser);
29111 declv = NULL_TREE;
29112 break;
29113 }
29114 }
29115 while (1);
29116
29117 if (declv)
29118 {
29119 cp_parser_parse_definitely (parser);
29120 nbraces += bracecount;
29121 }
29122 }
29123
29124 /* Note that we saved the original contents of this flag when we entered
29125 the structured block, and so we don't need to re-save it here. */
29126 if (code == CILK_SIMD)
29127 parser->in_statement = IN_CILK_SIMD_FOR;
29128 else
29129 parser->in_statement = IN_OMP_FOR;
29130
29131 /* Note that the grammar doesn't call for a structured block here,
29132 though the loop as a whole is a structured block. */
29133 body = push_stmt_list ();
29134 cp_parser_statement (parser, NULL_TREE, false, NULL);
29135 body = pop_stmt_list (body);
29136
29137 if (declv == NULL_TREE)
29138 ret = NULL_TREE;
29139 else
29140 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29141 pre_body, clauses);
29142
29143 while (nbraces)
29144 {
29145 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29146 {
29147 cp_lexer_consume_token (parser->lexer);
29148 nbraces--;
29149 }
29150 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29151 cp_lexer_consume_token (parser->lexer);
29152 else
29153 {
29154 if (!collapse_err)
29155 {
29156 error_at (cp_lexer_peek_token (parser->lexer)->location,
29157 "collapsed loops not perfectly nested");
29158 }
29159 collapse_err = true;
29160 cp_parser_statement_seq_opt (parser, NULL);
29161 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29162 break;
29163 }
29164 }
29165
29166 while (!for_block->is_empty ())
29167 add_stmt (pop_stmt_list (for_block->pop ()));
29168 release_tree_vector (for_block);
29169
29170 return ret;
29171 }
29172
29173 /* Helper function for OpenMP parsing, split clauses and call
29174 finish_omp_clauses on each of the set of clauses afterwards. */
29175
29176 static void
29177 cp_omp_split_clauses (location_t loc, enum tree_code code,
29178 omp_clause_mask mask, tree clauses, tree *cclauses)
29179 {
29180 int i;
29181 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29182 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29183 if (cclauses[i])
29184 cclauses[i] = finish_omp_clauses (cclauses[i]);
29185 }
29186
29187 /* OpenMP 4.0:
29188 #pragma omp simd simd-clause[optseq] new-line
29189 for-loop */
29190
29191 #define OMP_SIMD_CLAUSE_MASK \
29192 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29199
29200 static tree
29201 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29202 char *p_name, omp_clause_mask mask, tree *cclauses)
29203 {
29204 tree clauses, sb, ret;
29205 unsigned int save;
29206 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29207
29208 strcat (p_name, " simd");
29209 mask |= OMP_SIMD_CLAUSE_MASK;
29210 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29211
29212 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29213 cclauses == NULL);
29214 if (cclauses)
29215 {
29216 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29217 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29218 }
29219
29220 sb = begin_omp_structured_block ();
29221 save = cp_parser_begin_omp_structured_block (parser);
29222
29223 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29224
29225 cp_parser_end_omp_structured_block (parser, save);
29226 add_stmt (finish_omp_structured_block (sb));
29227
29228 return ret;
29229 }
29230
29231 /* OpenMP 2.5:
29232 #pragma omp for for-clause[optseq] new-line
29233 for-loop
29234
29235 OpenMP 4.0:
29236 #pragma omp for simd for-simd-clause[optseq] new-line
29237 for-loop */
29238
29239 #define OMP_FOR_CLAUSE_MASK \
29240 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29248
29249 static tree
29250 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29251 char *p_name, omp_clause_mask mask, tree *cclauses)
29252 {
29253 tree clauses, sb, ret;
29254 unsigned int save;
29255 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29256
29257 strcat (p_name, " for");
29258 mask |= OMP_FOR_CLAUSE_MASK;
29259 if (cclauses)
29260 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29261
29262 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29263 {
29264 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29265 const char *p = IDENTIFIER_POINTER (id);
29266
29267 if (strcmp (p, "simd") == 0)
29268 {
29269 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29270 if (cclauses == NULL)
29271 cclauses = cclauses_buf;
29272
29273 cp_lexer_consume_token (parser->lexer);
29274 if (!flag_openmp) /* flag_openmp_simd */
29275 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29276 cclauses);
29277 sb = begin_omp_structured_block ();
29278 save = cp_parser_begin_omp_structured_block (parser);
29279 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29280 cclauses);
29281 cp_parser_end_omp_structured_block (parser, save);
29282 tree body = finish_omp_structured_block (sb);
29283 if (ret == NULL)
29284 return ret;
29285 ret = make_node (OMP_FOR);
29286 TREE_TYPE (ret) = void_type_node;
29287 OMP_FOR_BODY (ret) = body;
29288 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29289 SET_EXPR_LOCATION (ret, loc);
29290 add_stmt (ret);
29291 return ret;
29292 }
29293 }
29294 if (!flag_openmp) /* flag_openmp_simd */
29295 {
29296 cp_parser_require_pragma_eol (parser, pragma_tok);
29297 return NULL_TREE;
29298 }
29299
29300 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29301 cclauses == NULL);
29302 if (cclauses)
29303 {
29304 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29305 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29306 }
29307
29308 sb = begin_omp_structured_block ();
29309 save = cp_parser_begin_omp_structured_block (parser);
29310
29311 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29312
29313 cp_parser_end_omp_structured_block (parser, save);
29314 add_stmt (finish_omp_structured_block (sb));
29315
29316 return ret;
29317 }
29318
29319 /* OpenMP 2.5:
29320 # pragma omp master new-line
29321 structured-block */
29322
29323 static tree
29324 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29325 {
29326 cp_parser_require_pragma_eol (parser, pragma_tok);
29327 return c_finish_omp_master (input_location,
29328 cp_parser_omp_structured_block (parser));
29329 }
29330
29331 /* OpenMP 2.5:
29332 # pragma omp ordered new-line
29333 structured-block */
29334
29335 static tree
29336 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29337 {
29338 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29339 cp_parser_require_pragma_eol (parser, pragma_tok);
29340 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29341 }
29342
29343 /* OpenMP 2.5:
29344
29345 section-scope:
29346 { section-sequence }
29347
29348 section-sequence:
29349 section-directive[opt] structured-block
29350 section-sequence section-directive structured-block */
29351
29352 static tree
29353 cp_parser_omp_sections_scope (cp_parser *parser)
29354 {
29355 tree stmt, substmt;
29356 bool error_suppress = false;
29357 cp_token *tok;
29358
29359 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29360 return NULL_TREE;
29361
29362 stmt = push_stmt_list ();
29363
29364 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29365 {
29366 substmt = cp_parser_omp_structured_block (parser);
29367 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29368 add_stmt (substmt);
29369 }
29370
29371 while (1)
29372 {
29373 tok = cp_lexer_peek_token (parser->lexer);
29374 if (tok->type == CPP_CLOSE_BRACE)
29375 break;
29376 if (tok->type == CPP_EOF)
29377 break;
29378
29379 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29380 {
29381 cp_lexer_consume_token (parser->lexer);
29382 cp_parser_require_pragma_eol (parser, tok);
29383 error_suppress = false;
29384 }
29385 else if (!error_suppress)
29386 {
29387 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29388 error_suppress = true;
29389 }
29390
29391 substmt = cp_parser_omp_structured_block (parser);
29392 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29393 add_stmt (substmt);
29394 }
29395 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29396
29397 substmt = pop_stmt_list (stmt);
29398
29399 stmt = make_node (OMP_SECTIONS);
29400 TREE_TYPE (stmt) = void_type_node;
29401 OMP_SECTIONS_BODY (stmt) = substmt;
29402
29403 add_stmt (stmt);
29404 return stmt;
29405 }
29406
29407 /* OpenMP 2.5:
29408 # pragma omp sections sections-clause[optseq] newline
29409 sections-scope */
29410
29411 #define OMP_SECTIONS_CLAUSE_MASK \
29412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29417
29418 static tree
29419 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29420 char *p_name, omp_clause_mask mask, tree *cclauses)
29421 {
29422 tree clauses, ret;
29423 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29424
29425 strcat (p_name, " sections");
29426 mask |= OMP_SECTIONS_CLAUSE_MASK;
29427 if (cclauses)
29428 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29429
29430 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29431 cclauses == NULL);
29432 if (cclauses)
29433 {
29434 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29435 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29436 }
29437
29438 ret = cp_parser_omp_sections_scope (parser);
29439 if (ret)
29440 OMP_SECTIONS_CLAUSES (ret) = clauses;
29441
29442 return ret;
29443 }
29444
29445 /* OpenMP 2.5:
29446 # pragma parallel parallel-clause new-line
29447 # pragma parallel for parallel-for-clause new-line
29448 # pragma parallel sections parallel-sections-clause new-line
29449
29450 OpenMP 4.0:
29451 # pragma parallel for simd parallel-for-simd-clause new-line */
29452
29453 #define OMP_PARALLEL_CLAUSE_MASK \
29454 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29463
29464 static tree
29465 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29466 char *p_name, omp_clause_mask mask, tree *cclauses)
29467 {
29468 tree stmt, clauses, block;
29469 unsigned int save;
29470 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29471
29472 strcat (p_name, " parallel");
29473 mask |= OMP_PARALLEL_CLAUSE_MASK;
29474
29475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29476 {
29477 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29478 if (cclauses == NULL)
29479 cclauses = cclauses_buf;
29480
29481 cp_lexer_consume_token (parser->lexer);
29482 if (!flag_openmp) /* flag_openmp_simd */
29483 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29484 block = begin_omp_parallel ();
29485 save = cp_parser_begin_omp_structured_block (parser);
29486 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29487 cp_parser_end_omp_structured_block (parser, save);
29488 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29489 block);
29490 OMP_PARALLEL_COMBINED (stmt) = 1;
29491 return stmt;
29492 }
29493 else if (cclauses)
29494 {
29495 error_at (loc, "expected %<for%> after %qs", p_name);
29496 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29497 return NULL_TREE;
29498 }
29499 else if (!flag_openmp) /* flag_openmp_simd */
29500 {
29501 cp_parser_require_pragma_eol (parser, pragma_tok);
29502 return NULL_TREE;
29503 }
29504 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29505 {
29506 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29507 const char *p = IDENTIFIER_POINTER (id);
29508 if (strcmp (p, "sections") == 0)
29509 {
29510 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29511 cclauses = cclauses_buf;
29512
29513 cp_lexer_consume_token (parser->lexer);
29514 block = begin_omp_parallel ();
29515 save = cp_parser_begin_omp_structured_block (parser);
29516 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29517 cp_parser_end_omp_structured_block (parser, save);
29518 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29519 block);
29520 OMP_PARALLEL_COMBINED (stmt) = 1;
29521 return stmt;
29522 }
29523 }
29524
29525 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29526
29527 block = begin_omp_parallel ();
29528 save = cp_parser_begin_omp_structured_block (parser);
29529 cp_parser_statement (parser, NULL_TREE, false, NULL);
29530 cp_parser_end_omp_structured_block (parser, save);
29531 stmt = finish_omp_parallel (clauses, block);
29532 return stmt;
29533 }
29534
29535 /* OpenMP 2.5:
29536 # pragma omp single single-clause[optseq] new-line
29537 structured-block */
29538
29539 #define OMP_SINGLE_CLAUSE_MASK \
29540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29544
29545 static tree
29546 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29547 {
29548 tree stmt = make_node (OMP_SINGLE);
29549 TREE_TYPE (stmt) = void_type_node;
29550
29551 OMP_SINGLE_CLAUSES (stmt)
29552 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29553 "#pragma omp single", pragma_tok);
29554 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29555
29556 return add_stmt (stmt);
29557 }
29558
29559 /* OpenMP 3.0:
29560 # pragma omp task task-clause[optseq] new-line
29561 structured-block */
29562
29563 #define OMP_TASK_CLAUSE_MASK \
29564 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29573
29574 static tree
29575 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29576 {
29577 tree clauses, block;
29578 unsigned int save;
29579
29580 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29581 "#pragma omp task", pragma_tok);
29582 block = begin_omp_task ();
29583 save = cp_parser_begin_omp_structured_block (parser);
29584 cp_parser_statement (parser, NULL_TREE, false, NULL);
29585 cp_parser_end_omp_structured_block (parser, save);
29586 return finish_omp_task (clauses, block);
29587 }
29588
29589 /* OpenMP 3.0:
29590 # pragma omp taskwait new-line */
29591
29592 static void
29593 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29594 {
29595 cp_parser_require_pragma_eol (parser, pragma_tok);
29596 finish_omp_taskwait ();
29597 }
29598
29599 /* OpenMP 3.1:
29600 # pragma omp taskyield new-line */
29601
29602 static void
29603 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29604 {
29605 cp_parser_require_pragma_eol (parser, pragma_tok);
29606 finish_omp_taskyield ();
29607 }
29608
29609 /* OpenMP 4.0:
29610 # pragma omp taskgroup new-line
29611 structured-block */
29612
29613 static tree
29614 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29615 {
29616 cp_parser_require_pragma_eol (parser, pragma_tok);
29617 return c_finish_omp_taskgroup (input_location,
29618 cp_parser_omp_structured_block (parser));
29619 }
29620
29621
29622 /* OpenMP 2.5:
29623 # pragma omp threadprivate (variable-list) */
29624
29625 static void
29626 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
29627 {
29628 tree vars;
29629
29630 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29631 cp_parser_require_pragma_eol (parser, pragma_tok);
29632
29633 finish_omp_threadprivate (vars);
29634 }
29635
29636 /* OpenMP 4.0:
29637 # pragma omp cancel cancel-clause[optseq] new-line */
29638
29639 #define OMP_CANCEL_CLAUSE_MASK \
29640 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29645
29646 static void
29647 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
29648 {
29649 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
29650 "#pragma omp cancel", pragma_tok);
29651 finish_omp_cancel (clauses);
29652 }
29653
29654 /* OpenMP 4.0:
29655 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
29656
29657 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
29658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
29662
29663 static void
29664 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
29665 {
29666 tree clauses;
29667 bool point_seen = false;
29668
29669 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29670 {
29671 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29672 const char *p = IDENTIFIER_POINTER (id);
29673
29674 if (strcmp (p, "point") == 0)
29675 {
29676 cp_lexer_consume_token (parser->lexer);
29677 point_seen = true;
29678 }
29679 }
29680 if (!point_seen)
29681 {
29682 cp_parser_error (parser, "expected %<point%>");
29683 cp_parser_require_pragma_eol (parser, pragma_tok);
29684 return;
29685 }
29686
29687 clauses = cp_parser_omp_all_clauses (parser,
29688 OMP_CANCELLATION_POINT_CLAUSE_MASK,
29689 "#pragma omp cancellation point",
29690 pragma_tok);
29691 finish_omp_cancellation_point (clauses);
29692 }
29693
29694 /* OpenMP 4.0:
29695 #pragma omp distribute distribute-clause[optseq] new-line
29696 for-loop */
29697
29698 #define OMP_DISTRIBUTE_CLAUSE_MASK \
29699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
29702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29703
29704 static tree
29705 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
29706 char *p_name, omp_clause_mask mask, tree *cclauses)
29707 {
29708 tree clauses, sb, ret;
29709 unsigned int save;
29710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29711
29712 strcat (p_name, " distribute");
29713 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
29714
29715 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29716 {
29717 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29718 const char *p = IDENTIFIER_POINTER (id);
29719 bool simd = false;
29720 bool parallel = false;
29721
29722 if (strcmp (p, "simd") == 0)
29723 simd = true;
29724 else
29725 parallel = strcmp (p, "parallel") == 0;
29726 if (parallel || simd)
29727 {
29728 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29729 if (cclauses == NULL)
29730 cclauses = cclauses_buf;
29731 cp_lexer_consume_token (parser->lexer);
29732 if (!flag_openmp) /* flag_openmp_simd */
29733 {
29734 if (simd)
29735 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29736 cclauses);
29737 else
29738 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
29739 cclauses);
29740 }
29741 sb = begin_omp_structured_block ();
29742 save = cp_parser_begin_omp_structured_block (parser);
29743 if (simd)
29744 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29745 cclauses);
29746 else
29747 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
29748 cclauses);
29749 cp_parser_end_omp_structured_block (parser, save);
29750 tree body = finish_omp_structured_block (sb);
29751 if (ret == NULL)
29752 return ret;
29753 ret = make_node (OMP_DISTRIBUTE);
29754 TREE_TYPE (ret) = void_type_node;
29755 OMP_FOR_BODY (ret) = body;
29756 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
29757 SET_EXPR_LOCATION (ret, loc);
29758 add_stmt (ret);
29759 return ret;
29760 }
29761 }
29762 if (!flag_openmp) /* flag_openmp_simd */
29763 {
29764 cp_parser_require_pragma_eol (parser, pragma_tok);
29765 return NULL_TREE;
29766 }
29767
29768 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29769 cclauses == NULL);
29770 if (cclauses)
29771 {
29772 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
29773 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
29774 }
29775
29776 sb = begin_omp_structured_block ();
29777 save = cp_parser_begin_omp_structured_block (parser);
29778
29779 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
29780
29781 cp_parser_end_omp_structured_block (parser, save);
29782 add_stmt (finish_omp_structured_block (sb));
29783
29784 return ret;
29785 }
29786
29787 /* OpenMP 4.0:
29788 # pragma omp teams teams-clause[optseq] new-line
29789 structured-block */
29790
29791 #define OMP_TEAMS_CLAUSE_MASK \
29792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
29797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
29798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
29799
29800 static tree
29801 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
29802 char *p_name, omp_clause_mask mask, tree *cclauses)
29803 {
29804 tree clauses, sb, ret;
29805 unsigned int save;
29806 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29807
29808 strcat (p_name, " teams");
29809 mask |= OMP_TEAMS_CLAUSE_MASK;
29810
29811 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29812 {
29813 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29814 const char *p = IDENTIFIER_POINTER (id);
29815 if (strcmp (p, "distribute") == 0)
29816 {
29817 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29818 if (cclauses == NULL)
29819 cclauses = cclauses_buf;
29820
29821 cp_lexer_consume_token (parser->lexer);
29822 if (!flag_openmp) /* flag_openmp_simd */
29823 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
29824 cclauses);
29825 sb = begin_omp_structured_block ();
29826 save = cp_parser_begin_omp_structured_block (parser);
29827 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
29828 cclauses);
29829 cp_parser_end_omp_structured_block (parser, save);
29830 tree body = finish_omp_structured_block (sb);
29831 if (ret == NULL)
29832 return ret;
29833 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
29834 ret = make_node (OMP_TEAMS);
29835 TREE_TYPE (ret) = void_type_node;
29836 OMP_TEAMS_CLAUSES (ret) = clauses;
29837 OMP_TEAMS_BODY (ret) = body;
29838 return add_stmt (ret);
29839 }
29840 }
29841 if (!flag_openmp) /* flag_openmp_simd */
29842 {
29843 cp_parser_require_pragma_eol (parser, pragma_tok);
29844 return NULL_TREE;
29845 }
29846
29847 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29848 cclauses == NULL);
29849 if (cclauses)
29850 {
29851 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
29852 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
29853 }
29854
29855 tree stmt = make_node (OMP_TEAMS);
29856 TREE_TYPE (stmt) = void_type_node;
29857 OMP_TEAMS_CLAUSES (stmt) = clauses;
29858 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
29859
29860 return add_stmt (stmt);
29861 }
29862
29863 /* OpenMP 4.0:
29864 # pragma omp target data target-data-clause[optseq] new-line
29865 structured-block */
29866
29867 #define OMP_TARGET_DATA_CLAUSE_MASK \
29868 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29871
29872 static tree
29873 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
29874 {
29875 tree stmt = make_node (OMP_TARGET_DATA);
29876 TREE_TYPE (stmt) = void_type_node;
29877
29878 OMP_TARGET_DATA_CLAUSES (stmt)
29879 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
29880 "#pragma omp target data", pragma_tok);
29881 keep_next_level (true);
29882 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
29883
29884 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29885 return add_stmt (stmt);
29886 }
29887
29888 /* OpenMP 4.0:
29889 # pragma omp target update target-update-clause[optseq] new-line */
29890
29891 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
29892 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
29893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
29894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29896
29897 static bool
29898 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
29899 enum pragma_context context)
29900 {
29901 if (context == pragma_stmt)
29902 {
29903 error_at (pragma_tok->location,
29904 "%<#pragma omp target update%> may only be "
29905 "used in compound statements");
29906 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29907 return false;
29908 }
29909
29910 tree clauses
29911 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
29912 "#pragma omp target update", pragma_tok);
29913 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
29914 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
29915 {
29916 error_at (pragma_tok->location,
29917 "%<#pragma omp target update must contain at least one "
29918 "%<from%> or %<to%> clauses");
29919 return false;
29920 }
29921
29922 tree stmt = make_node (OMP_TARGET_UPDATE);
29923 TREE_TYPE (stmt) = void_type_node;
29924 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
29925 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29926 add_stmt (stmt);
29927 return false;
29928 }
29929
29930 /* OpenMP 4.0:
29931 # pragma omp target target-clause[optseq] new-line
29932 structured-block */
29933
29934 #define OMP_TARGET_CLAUSE_MASK \
29935 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29938
29939 static bool
29940 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
29941 enum pragma_context context)
29942 {
29943 if (context != pragma_stmt && context != pragma_compound)
29944 {
29945 cp_parser_error (parser, "expected declaration specifiers");
29946 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29947 return false;
29948 }
29949
29950 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29951 {
29952 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29953 const char *p = IDENTIFIER_POINTER (id);
29954
29955 if (strcmp (p, "teams") == 0)
29956 {
29957 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
29958 char p_name[sizeof ("#pragma omp target teams distribute "
29959 "parallel for simd")];
29960
29961 cp_lexer_consume_token (parser->lexer);
29962 strcpy (p_name, "#pragma omp target");
29963 keep_next_level (true);
29964 if (!flag_openmp) /* flag_openmp_simd */
29965 return cp_parser_omp_teams (parser, pragma_tok, p_name,
29966 OMP_TARGET_CLAUSE_MASK, cclauses);
29967 tree sb = begin_omp_structured_block ();
29968 unsigned save = cp_parser_begin_omp_structured_block (parser);
29969 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
29970 OMP_TARGET_CLAUSE_MASK, cclauses);
29971 cp_parser_end_omp_structured_block (parser, save);
29972 tree body = finish_omp_structured_block (sb);
29973 if (ret == NULL)
29974 return ret;
29975 tree stmt = make_node (OMP_TARGET);
29976 TREE_TYPE (stmt) = void_type_node;
29977 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
29978 OMP_TARGET_BODY (stmt) = body;
29979 add_stmt (stmt);
29980 return true;
29981 }
29982 else if (!flag_openmp) /* flag_openmp_simd */
29983 {
29984 cp_parser_require_pragma_eol (parser, pragma_tok);
29985 return NULL_TREE;
29986 }
29987 else if (strcmp (p, "data") == 0)
29988 {
29989 cp_lexer_consume_token (parser->lexer);
29990 cp_parser_omp_target_data (parser, pragma_tok);
29991 return true;
29992 }
29993 else if (strcmp (p, "update") == 0)
29994 {
29995 cp_lexer_consume_token (parser->lexer);
29996 return cp_parser_omp_target_update (parser, pragma_tok, context);
29997 }
29998 }
29999
30000 tree stmt = make_node (OMP_TARGET);
30001 TREE_TYPE (stmt) = void_type_node;
30002
30003 OMP_TARGET_CLAUSES (stmt)
30004 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30005 "#pragma omp target", pragma_tok);
30006 keep_next_level (true);
30007 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30008
30009 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30010 add_stmt (stmt);
30011 return true;
30012 }
30013
30014 /* OpenMP 4.0:
30015 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30016
30017 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30024
30025 static void
30026 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30027 enum pragma_context context)
30028 {
30029 bool first_p = parser->omp_declare_simd == NULL;
30030 cp_omp_declare_simd_data data;
30031 if (first_p)
30032 {
30033 data.error_seen = false;
30034 data.fndecl_seen = false;
30035 data.tokens = vNULL;
30036 parser->omp_declare_simd = &data;
30037 }
30038 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30039 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30040 cp_lexer_consume_token (parser->lexer);
30041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30042 parser->omp_declare_simd->error_seen = true;
30043 cp_parser_require_pragma_eol (parser, pragma_tok);
30044 struct cp_token_cache *cp
30045 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30046 parser->omp_declare_simd->tokens.safe_push (cp);
30047 if (first_p)
30048 {
30049 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30050 cp_parser_pragma (parser, context);
30051 switch (context)
30052 {
30053 case pragma_external:
30054 cp_parser_declaration (parser);
30055 break;
30056 case pragma_member:
30057 cp_parser_member_declaration (parser);
30058 break;
30059 case pragma_objc_icode:
30060 cp_parser_block_declaration (parser, /*statement_p=*/false);
30061 break;
30062 default:
30063 cp_parser_declaration_statement (parser);
30064 break;
30065 }
30066 if (parser->omp_declare_simd
30067 && !parser->omp_declare_simd->error_seen
30068 && !parser->omp_declare_simd->fndecl_seen)
30069 error_at (pragma_tok->location,
30070 "%<#pragma omp declare simd%> not immediately followed by "
30071 "function declaration or definition");
30072 data.tokens.release ();
30073 parser->omp_declare_simd = NULL;
30074 }
30075 }
30076
30077 /* Finalize #pragma omp declare simd clauses after direct declarator has
30078 been parsed, and put that into "omp declare simd" attribute. */
30079
30080 static tree
30081 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30082 {
30083 struct cp_token_cache *ce;
30084 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30085 int i;
30086
30087 if (!data->error_seen && data->fndecl_seen)
30088 {
30089 error ("%<#pragma omp declare simd%> not immediately followed by "
30090 "a single function declaration or definition");
30091 data->error_seen = true;
30092 return attrs;
30093 }
30094 if (data->error_seen)
30095 return attrs;
30096
30097 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30098 {
30099 tree c, cl;
30100
30101 cp_parser_push_lexer_for_tokens (parser, ce);
30102 parser->lexer->in_pragma = true;
30103 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30104 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30105 cp_lexer_consume_token (parser->lexer);
30106 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30107 "#pragma omp declare simd", pragma_tok);
30108 cp_parser_pop_lexer (parser);
30109 if (cl)
30110 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30111 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30112 TREE_CHAIN (c) = attrs;
30113 if (processing_template_decl)
30114 ATTR_IS_DEPENDENT (c) = 1;
30115 attrs = c;
30116 }
30117
30118 data->fndecl_seen = true;
30119 return attrs;
30120 }
30121
30122
30123 /* OpenMP 4.0:
30124 # pragma omp declare target new-line
30125 declarations and definitions
30126 # pragma omp end declare target new-line */
30127
30128 static void
30129 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30130 {
30131 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30132 scope_chain->omp_declare_target_attribute++;
30133 }
30134
30135 static void
30136 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30137 {
30138 const char *p = "";
30139 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30140 {
30141 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30142 p = IDENTIFIER_POINTER (id);
30143 }
30144 if (strcmp (p, "declare") == 0)
30145 {
30146 cp_lexer_consume_token (parser->lexer);
30147 p = "";
30148 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30149 {
30150 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30151 p = IDENTIFIER_POINTER (id);
30152 }
30153 if (strcmp (p, "target") == 0)
30154 cp_lexer_consume_token (parser->lexer);
30155 else
30156 {
30157 cp_parser_error (parser, "expected %<target%>");
30158 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30159 return;
30160 }
30161 }
30162 else
30163 {
30164 cp_parser_error (parser, "expected %<declare%>");
30165 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30166 return;
30167 }
30168 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30169 if (!scope_chain->omp_declare_target_attribute)
30170 error_at (pragma_tok->location,
30171 "%<#pragma omp end declare target%> without corresponding "
30172 "%<#pragma omp declare target%>");
30173 else
30174 scope_chain->omp_declare_target_attribute--;
30175 }
30176
30177 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30178 expression and optional initializer clause of
30179 #pragma omp declare reduction. We store the expression(s) as
30180 either 3, 6 or 7 special statements inside of the artificial function's
30181 body. The first two statements are DECL_EXPRs for the artificial
30182 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30183 expression that uses those variables.
30184 If there was any INITIALIZER clause, this is followed by further statements,
30185 the fourth and fifth statements are DECL_EXPRs for the artificial
30186 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30187 constructor variant (first token after open paren is not omp_priv),
30188 then the sixth statement is a statement with the function call expression
30189 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30190 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30191 to initialize the OMP_PRIV artificial variable and there is seventh
30192 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30193
30194 static bool
30195 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30196 {
30197 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30198 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30199 type = TREE_TYPE (type);
30200 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30201 DECL_ARTIFICIAL (omp_out) = 1;
30202 pushdecl (omp_out);
30203 add_decl_expr (omp_out);
30204 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30205 DECL_ARTIFICIAL (omp_in) = 1;
30206 pushdecl (omp_in);
30207 add_decl_expr (omp_in);
30208 tree combiner;
30209 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30210
30211 keep_next_level (true);
30212 tree block = begin_omp_structured_block ();
30213 combiner = cp_parser_expression (parser, false, NULL);
30214 finish_expr_stmt (combiner);
30215 block = finish_omp_structured_block (block);
30216 add_stmt (block);
30217
30218 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30219 return false;
30220
30221 const char *p = "";
30222 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30223 {
30224 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30225 p = IDENTIFIER_POINTER (id);
30226 }
30227
30228 if (strcmp (p, "initializer") == 0)
30229 {
30230 cp_lexer_consume_token (parser->lexer);
30231 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30232 return false;
30233
30234 p = "";
30235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30236 {
30237 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30238 p = IDENTIFIER_POINTER (id);
30239 }
30240
30241 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30242 DECL_ARTIFICIAL (omp_priv) = 1;
30243 pushdecl (omp_priv);
30244 add_decl_expr (omp_priv);
30245 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30246 DECL_ARTIFICIAL (omp_orig) = 1;
30247 pushdecl (omp_orig);
30248 add_decl_expr (omp_orig);
30249
30250 keep_next_level (true);
30251 block = begin_omp_structured_block ();
30252
30253 bool ctor = false;
30254 if (strcmp (p, "omp_priv") == 0)
30255 {
30256 bool is_direct_init, is_non_constant_init;
30257 ctor = true;
30258 cp_lexer_consume_token (parser->lexer);
30259 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30260 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30261 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30262 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30263 == CPP_CLOSE_PAREN
30264 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30265 == CPP_CLOSE_PAREN))
30266 {
30267 finish_omp_structured_block (block);
30268 error ("invalid initializer clause");
30269 return false;
30270 }
30271 initializer = cp_parser_initializer (parser, &is_direct_init,
30272 &is_non_constant_init);
30273 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30274 NULL_TREE, LOOKUP_ONLYCONVERTING);
30275 }
30276 else
30277 {
30278 cp_parser_parse_tentatively (parser);
30279 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30280 /*check_dependency_p=*/true,
30281 /*template_p=*/NULL,
30282 /*declarator_p=*/false,
30283 /*optional_p=*/false);
30284 vec<tree, va_gc> *args;
30285 if (fn_name == error_mark_node
30286 || cp_parser_error_occurred (parser)
30287 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30288 || ((args = cp_parser_parenthesized_expression_list
30289 (parser, non_attr, /*cast_p=*/false,
30290 /*allow_expansion_p=*/true,
30291 /*non_constant_p=*/NULL)),
30292 cp_parser_error_occurred (parser)))
30293 {
30294 finish_omp_structured_block (block);
30295 cp_parser_abort_tentative_parse (parser);
30296 cp_parser_error (parser, "expected id-expression (arguments)");
30297 return false;
30298 }
30299 unsigned int i;
30300 tree arg;
30301 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30302 if (arg == omp_priv
30303 || (TREE_CODE (arg) == ADDR_EXPR
30304 && TREE_OPERAND (arg, 0) == omp_priv))
30305 break;
30306 cp_parser_abort_tentative_parse (parser);
30307 if (arg == NULL_TREE)
30308 error ("one of the initializer call arguments should be %<omp_priv%>"
30309 " or %<&omp_priv%>");
30310 initializer = cp_parser_postfix_expression (parser, false, false, false,
30311 false, NULL);
30312 finish_expr_stmt (initializer);
30313 }
30314
30315 block = finish_omp_structured_block (block);
30316 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30317 finish_expr_stmt (block);
30318
30319 if (ctor)
30320 add_decl_expr (omp_orig);
30321
30322 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30323 return false;
30324 }
30325
30326 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30327 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30328
30329 return true;
30330 }
30331
30332 /* OpenMP 4.0
30333 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30334 initializer-clause[opt] new-line
30335
30336 initializer-clause:
30337 initializer (omp_priv initializer)
30338 initializer (function-name (argument-list)) */
30339
30340 static void
30341 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30342 enum pragma_context)
30343 {
30344 vec<tree> types = vNULL;
30345 enum tree_code reduc_code = ERROR_MARK;
30346 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30347 unsigned int i;
30348 cp_token *first_token;
30349 cp_token_cache *cp;
30350 int errs;
30351
30352 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30353 goto fail;
30354
30355 switch (cp_lexer_peek_token (parser->lexer)->type)
30356 {
30357 case CPP_PLUS:
30358 reduc_code = PLUS_EXPR;
30359 break;
30360 case CPP_MULT:
30361 reduc_code = MULT_EXPR;
30362 break;
30363 case CPP_MINUS:
30364 reduc_code = MINUS_EXPR;
30365 break;
30366 case CPP_AND:
30367 reduc_code = BIT_AND_EXPR;
30368 break;
30369 case CPP_XOR:
30370 reduc_code = BIT_XOR_EXPR;
30371 break;
30372 case CPP_OR:
30373 reduc_code = BIT_IOR_EXPR;
30374 break;
30375 case CPP_AND_AND:
30376 reduc_code = TRUTH_ANDIF_EXPR;
30377 break;
30378 case CPP_OR_OR:
30379 reduc_code = TRUTH_ORIF_EXPR;
30380 break;
30381 case CPP_NAME:
30382 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30383 break;
30384 default:
30385 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30386 "%<|%>, %<&&%>, %<||%> or identifier");
30387 goto fail;
30388 }
30389
30390 if (reduc_code != ERROR_MARK)
30391 cp_lexer_consume_token (parser->lexer);
30392
30393 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30394 if (reduc_id == error_mark_node)
30395 goto fail;
30396
30397 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30398 goto fail;
30399
30400 /* Types may not be defined in declare reduction type list. */
30401 const char *saved_message;
30402 saved_message = parser->type_definition_forbidden_message;
30403 parser->type_definition_forbidden_message
30404 = G_("types may not be defined in declare reduction type list");
30405 bool saved_colon_corrects_to_scope_p;
30406 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30407 parser->colon_corrects_to_scope_p = false;
30408 bool saved_colon_doesnt_start_class_def_p;
30409 saved_colon_doesnt_start_class_def_p
30410 = parser->colon_doesnt_start_class_def_p;
30411 parser->colon_doesnt_start_class_def_p = true;
30412
30413 while (true)
30414 {
30415 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30416 type = cp_parser_type_id (parser);
30417 if (type == error_mark_node)
30418 ;
30419 else if (ARITHMETIC_TYPE_P (type)
30420 && (orig_reduc_id == NULL_TREE
30421 || (TREE_CODE (type) != COMPLEX_TYPE
30422 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30423 "min") == 0
30424 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30425 "max") == 0))))
30426 error_at (loc, "predeclared arithmetic type %qT in "
30427 "%<#pragma omp declare reduction%>", type);
30428 else if (TREE_CODE (type) == FUNCTION_TYPE
30429 || TREE_CODE (type) == METHOD_TYPE
30430 || TREE_CODE (type) == ARRAY_TYPE)
30431 error_at (loc, "function or array type %qT in "
30432 "%<#pragma omp declare reduction%>", type);
30433 else if (TREE_CODE (type) == REFERENCE_TYPE)
30434 error_at (loc, "reference type %qT in "
30435 "%<#pragma omp declare reduction%>", type);
30436 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30437 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30438 "%<#pragma omp declare reduction%>", type);
30439 else
30440 types.safe_push (type);
30441
30442 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30443 cp_lexer_consume_token (parser->lexer);
30444 else
30445 break;
30446 }
30447
30448 /* Restore the saved message. */
30449 parser->type_definition_forbidden_message = saved_message;
30450 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30451 parser->colon_doesnt_start_class_def_p
30452 = saved_colon_doesnt_start_class_def_p;
30453
30454 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30455 || types.is_empty ())
30456 {
30457 fail:
30458 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30459 types.release ();
30460 return;
30461 }
30462
30463 first_token = cp_lexer_peek_token (parser->lexer);
30464 cp = NULL;
30465 errs = errorcount;
30466 FOR_EACH_VEC_ELT (types, i, type)
30467 {
30468 tree fntype
30469 = build_function_type_list (void_type_node,
30470 cp_build_reference_type (type, false),
30471 NULL_TREE);
30472 tree this_reduc_id = reduc_id;
30473 if (!dependent_type_p (type))
30474 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30475 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30476 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30477 DECL_ARTIFICIAL (fndecl) = 1;
30478 DECL_EXTERNAL (fndecl) = 1;
30479 DECL_DECLARED_INLINE_P (fndecl) = 1;
30480 DECL_IGNORED_P (fndecl) = 1;
30481 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30482 DECL_ATTRIBUTES (fndecl)
30483 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30484 DECL_ATTRIBUTES (fndecl));
30485 if (processing_template_decl)
30486 fndecl = push_template_decl (fndecl);
30487 bool block_scope = false;
30488 tree block = NULL_TREE;
30489 if (current_function_decl)
30490 {
30491 block_scope = true;
30492 DECL_CONTEXT (fndecl) = global_namespace;
30493 if (!processing_template_decl)
30494 pushdecl (fndecl);
30495 }
30496 else if (current_class_type)
30497 {
30498 if (cp == NULL)
30499 {
30500 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30501 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30502 cp_lexer_consume_token (parser->lexer);
30503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30504 goto fail;
30505 cp = cp_token_cache_new (first_token,
30506 cp_lexer_peek_nth_token (parser->lexer,
30507 2));
30508 }
30509 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30510 finish_member_declaration (fndecl);
30511 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30512 DECL_PENDING_INLINE_P (fndecl) = 1;
30513 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30514 continue;
30515 }
30516 else
30517 {
30518 DECL_CONTEXT (fndecl) = current_namespace;
30519 pushdecl (fndecl);
30520 }
30521 if (!block_scope)
30522 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30523 else
30524 block = begin_omp_structured_block ();
30525 if (cp)
30526 {
30527 cp_parser_push_lexer_for_tokens (parser, cp);
30528 parser->lexer->in_pragma = true;
30529 }
30530 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30531 {
30532 if (!block_scope)
30533 finish_function (0);
30534 else
30535 DECL_CONTEXT (fndecl) = current_function_decl;
30536 if (cp)
30537 cp_parser_pop_lexer (parser);
30538 goto fail;
30539 }
30540 if (cp)
30541 cp_parser_pop_lexer (parser);
30542 if (!block_scope)
30543 finish_function (0);
30544 else
30545 {
30546 DECL_CONTEXT (fndecl) = current_function_decl;
30547 block = finish_omp_structured_block (block);
30548 if (TREE_CODE (block) == BIND_EXPR)
30549 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30550 else if (TREE_CODE (block) == STATEMENT_LIST)
30551 DECL_SAVED_TREE (fndecl) = block;
30552 if (processing_template_decl)
30553 add_decl_expr (fndecl);
30554 }
30555 cp_check_omp_declare_reduction (fndecl);
30556 if (cp == NULL && types.length () > 1)
30557 cp = cp_token_cache_new (first_token,
30558 cp_lexer_peek_nth_token (parser->lexer, 2));
30559 if (errs != errorcount)
30560 break;
30561 }
30562
30563 cp_parser_require_pragma_eol (parser, pragma_tok);
30564 types.release ();
30565 }
30566
30567 /* OpenMP 4.0
30568 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30569 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30570 initializer-clause[opt] new-line
30571 #pragma omp declare target new-line */
30572
30573 static void
30574 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
30575 enum pragma_context context)
30576 {
30577 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30578 {
30579 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30580 const char *p = IDENTIFIER_POINTER (id);
30581
30582 if (strcmp (p, "simd") == 0)
30583 {
30584 cp_lexer_consume_token (parser->lexer);
30585 cp_parser_omp_declare_simd (parser, pragma_tok,
30586 context);
30587 return;
30588 }
30589 cp_ensure_no_omp_declare_simd (parser);
30590 if (strcmp (p, "reduction") == 0)
30591 {
30592 cp_lexer_consume_token (parser->lexer);
30593 cp_parser_omp_declare_reduction (parser, pragma_tok,
30594 context);
30595 return;
30596 }
30597 if (!flag_openmp) /* flag_openmp_simd */
30598 {
30599 cp_parser_require_pragma_eol (parser, pragma_tok);
30600 return;
30601 }
30602 if (strcmp (p, "target") == 0)
30603 {
30604 cp_lexer_consume_token (parser->lexer);
30605 cp_parser_omp_declare_target (parser, pragma_tok);
30606 return;
30607 }
30608 }
30609 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
30610 "or %<target%>");
30611 cp_parser_require_pragma_eol (parser, pragma_tok);
30612 }
30613
30614 /* Main entry point to OpenMP statement pragmas. */
30615
30616 static void
30617 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
30618 {
30619 tree stmt;
30620 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
30621 omp_clause_mask mask (0);
30622
30623 switch (pragma_tok->pragma_kind)
30624 {
30625 case PRAGMA_OMP_ATOMIC:
30626 cp_parser_omp_atomic (parser, pragma_tok);
30627 return;
30628 case PRAGMA_OMP_CRITICAL:
30629 stmt = cp_parser_omp_critical (parser, pragma_tok);
30630 break;
30631 case PRAGMA_OMP_DISTRIBUTE:
30632 strcpy (p_name, "#pragma omp");
30633 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
30634 break;
30635 case PRAGMA_OMP_FOR:
30636 strcpy (p_name, "#pragma omp");
30637 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
30638 break;
30639 case PRAGMA_OMP_MASTER:
30640 stmt = cp_parser_omp_master (parser, pragma_tok);
30641 break;
30642 case PRAGMA_OMP_ORDERED:
30643 stmt = cp_parser_omp_ordered (parser, pragma_tok);
30644 break;
30645 case PRAGMA_OMP_PARALLEL:
30646 strcpy (p_name, "#pragma omp");
30647 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
30648 break;
30649 case PRAGMA_OMP_SECTIONS:
30650 strcpy (p_name, "#pragma omp");
30651 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
30652 break;
30653 case PRAGMA_OMP_SIMD:
30654 strcpy (p_name, "#pragma omp");
30655 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
30656 break;
30657 case PRAGMA_OMP_SINGLE:
30658 stmt = cp_parser_omp_single (parser, pragma_tok);
30659 break;
30660 case PRAGMA_OMP_TASK:
30661 stmt = cp_parser_omp_task (parser, pragma_tok);
30662 break;
30663 case PRAGMA_OMP_TASKGROUP:
30664 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
30665 break;
30666 case PRAGMA_OMP_TEAMS:
30667 strcpy (p_name, "#pragma omp");
30668 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
30669 break;
30670 default:
30671 gcc_unreachable ();
30672 }
30673
30674 if (stmt)
30675 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30676 }
30677 \f
30678 /* Transactional Memory parsing routines. */
30679
30680 /* Parse a transaction attribute.
30681
30682 txn-attribute:
30683 attribute
30684 [ [ identifier ] ]
30685
30686 ??? Simplify this when C++0x bracket attributes are
30687 implemented properly. */
30688
30689 static tree
30690 cp_parser_txn_attribute_opt (cp_parser *parser)
30691 {
30692 cp_token *token;
30693 tree attr_name, attr = NULL;
30694
30695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30696 return cp_parser_attributes_opt (parser);
30697
30698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
30699 return NULL_TREE;
30700 cp_lexer_consume_token (parser->lexer);
30701 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
30702 goto error1;
30703
30704 token = cp_lexer_peek_token (parser->lexer);
30705 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
30706 {
30707 token = cp_lexer_consume_token (parser->lexer);
30708
30709 attr_name = (token->type == CPP_KEYWORD
30710 /* For keywords, use the canonical spelling,
30711 not the parsed identifier. */
30712 ? ridpointers[(int) token->keyword]
30713 : token->u.value);
30714 attr = build_tree_list (attr_name, NULL_TREE);
30715 }
30716 else
30717 cp_parser_error (parser, "expected identifier");
30718
30719 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30720 error1:
30721 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30722 return attr;
30723 }
30724
30725 /* Parse a __transaction_atomic or __transaction_relaxed statement.
30726
30727 transaction-statement:
30728 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
30729 compound-statement
30730 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
30731 */
30732
30733 static tree
30734 cp_parser_transaction (cp_parser *parser, enum rid keyword)
30735 {
30736 unsigned char old_in = parser->in_transaction;
30737 unsigned char this_in = 1, new_in;
30738 cp_token *token;
30739 tree stmt, attrs, noex;
30740
30741 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30742 || keyword == RID_TRANSACTION_RELAXED);
30743 token = cp_parser_require_keyword (parser, keyword,
30744 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30745 : RT_TRANSACTION_RELAXED));
30746 gcc_assert (token != NULL);
30747
30748 if (keyword == RID_TRANSACTION_RELAXED)
30749 this_in |= TM_STMT_ATTR_RELAXED;
30750 else
30751 {
30752 attrs = cp_parser_txn_attribute_opt (parser);
30753 if (attrs)
30754 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
30755 }
30756
30757 /* Parse a noexcept specification. */
30758 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
30759
30760 /* Keep track if we're in the lexical scope of an outer transaction. */
30761 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
30762
30763 stmt = begin_transaction_stmt (token->location, NULL, this_in);
30764
30765 parser->in_transaction = new_in;
30766 cp_parser_compound_statement (parser, NULL, false, false);
30767 parser->in_transaction = old_in;
30768
30769 finish_transaction_stmt (stmt, NULL, this_in, noex);
30770
30771 return stmt;
30772 }
30773
30774 /* Parse a __transaction_atomic or __transaction_relaxed expression.
30775
30776 transaction-expression:
30777 __transaction_atomic txn-noexcept-spec[opt] ( expression )
30778 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
30779 */
30780
30781 static tree
30782 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
30783 {
30784 unsigned char old_in = parser->in_transaction;
30785 unsigned char this_in = 1;
30786 cp_token *token;
30787 tree expr, noex;
30788 bool noex_expr;
30789
30790 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30791 || keyword == RID_TRANSACTION_RELAXED);
30792
30793 if (!flag_tm)
30794 error (keyword == RID_TRANSACTION_RELAXED
30795 ? G_("%<__transaction_relaxed%> without transactional memory "
30796 "support enabled")
30797 : G_("%<__transaction_atomic%> without transactional memory "
30798 "support enabled"));
30799
30800 token = cp_parser_require_keyword (parser, keyword,
30801 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30802 : RT_TRANSACTION_RELAXED));
30803 gcc_assert (token != NULL);
30804
30805 if (keyword == RID_TRANSACTION_RELAXED)
30806 this_in |= TM_STMT_ATTR_RELAXED;
30807
30808 /* Set this early. This might mean that we allow transaction_cancel in
30809 an expression that we find out later actually has to be a constexpr.
30810 However, we expect that cxx_constant_value will be able to deal with
30811 this; also, if the noexcept has no constexpr, then what we parse next
30812 really is a transaction's body. */
30813 parser->in_transaction = this_in;
30814
30815 /* Parse a noexcept specification. */
30816 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
30817 true);
30818
30819 if (!noex || !noex_expr
30820 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
30821 {
30822 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30823
30824 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
30825 expr = finish_parenthesized_expr (expr);
30826
30827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30828 }
30829 else
30830 {
30831 /* The only expression that is available got parsed for the noexcept
30832 already. noexcept is true then. */
30833 expr = noex;
30834 noex = boolean_true_node;
30835 }
30836
30837 expr = build_transaction_expr (token->location, expr, this_in, noex);
30838 parser->in_transaction = old_in;
30839
30840 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
30841 return error_mark_node;
30842
30843 return (flag_tm ? expr : error_mark_node);
30844 }
30845
30846 /* Parse a function-transaction-block.
30847
30848 function-transaction-block:
30849 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
30850 function-body
30851 __transaction_atomic txn-attribute[opt] function-try-block
30852 __transaction_relaxed ctor-initializer[opt] function-body
30853 __transaction_relaxed function-try-block
30854 */
30855
30856 static bool
30857 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
30858 {
30859 unsigned char old_in = parser->in_transaction;
30860 unsigned char new_in = 1;
30861 tree compound_stmt, stmt, attrs;
30862 bool ctor_initializer_p;
30863 cp_token *token;
30864
30865 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30866 || keyword == RID_TRANSACTION_RELAXED);
30867 token = cp_parser_require_keyword (parser, keyword,
30868 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30869 : RT_TRANSACTION_RELAXED));
30870 gcc_assert (token != NULL);
30871
30872 if (keyword == RID_TRANSACTION_RELAXED)
30873 new_in |= TM_STMT_ATTR_RELAXED;
30874 else
30875 {
30876 attrs = cp_parser_txn_attribute_opt (parser);
30877 if (attrs)
30878 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
30879 }
30880
30881 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
30882
30883 parser->in_transaction = new_in;
30884
30885 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
30886 ctor_initializer_p = cp_parser_function_try_block (parser);
30887 else
30888 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
30889 (parser, /*in_function_try_block=*/false);
30890
30891 parser->in_transaction = old_in;
30892
30893 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
30894
30895 return ctor_initializer_p;
30896 }
30897
30898 /* Parse a __transaction_cancel statement.
30899
30900 cancel-statement:
30901 __transaction_cancel txn-attribute[opt] ;
30902 __transaction_cancel txn-attribute[opt] throw-expression ;
30903
30904 ??? Cancel and throw is not yet implemented. */
30905
30906 static tree
30907 cp_parser_transaction_cancel (cp_parser *parser)
30908 {
30909 cp_token *token;
30910 bool is_outer = false;
30911 tree stmt, attrs;
30912
30913 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
30914 RT_TRANSACTION_CANCEL);
30915 gcc_assert (token != NULL);
30916
30917 attrs = cp_parser_txn_attribute_opt (parser);
30918 if (attrs)
30919 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
30920
30921 /* ??? Parse cancel-and-throw here. */
30922
30923 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30924
30925 if (!flag_tm)
30926 {
30927 error_at (token->location, "%<__transaction_cancel%> without "
30928 "transactional memory support enabled");
30929 return error_mark_node;
30930 }
30931 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
30932 {
30933 error_at (token->location, "%<__transaction_cancel%> within a "
30934 "%<__transaction_relaxed%>");
30935 return error_mark_node;
30936 }
30937 else if (is_outer)
30938 {
30939 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
30940 && !is_tm_may_cancel_outer (current_function_decl))
30941 {
30942 error_at (token->location, "outer %<__transaction_cancel%> not "
30943 "within outer %<__transaction_atomic%>");
30944 error_at (token->location,
30945 " or a %<transaction_may_cancel_outer%> function");
30946 return error_mark_node;
30947 }
30948 }
30949 else if (parser->in_transaction == 0)
30950 {
30951 error_at (token->location, "%<__transaction_cancel%> not within "
30952 "%<__transaction_atomic%>");
30953 return error_mark_node;
30954 }
30955
30956 stmt = build_tm_abort_call (token->location, is_outer);
30957 add_stmt (stmt);
30958
30959 return stmt;
30960 }
30961 \f
30962 /* The parser. */
30963
30964 static GTY (()) cp_parser *the_parser;
30965
30966 \f
30967 /* Special handling for the first token or line in the file. The first
30968 thing in the file might be #pragma GCC pch_preprocess, which loads a
30969 PCH file, which is a GC collection point. So we need to handle this
30970 first pragma without benefit of an existing lexer structure.
30971
30972 Always returns one token to the caller in *FIRST_TOKEN. This is
30973 either the true first token of the file, or the first token after
30974 the initial pragma. */
30975
30976 static void
30977 cp_parser_initial_pragma (cp_token *first_token)
30978 {
30979 tree name = NULL;
30980
30981 cp_lexer_get_preprocessor_token (NULL, first_token);
30982 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
30983 return;
30984
30985 cp_lexer_get_preprocessor_token (NULL, first_token);
30986 if (first_token->type == CPP_STRING)
30987 {
30988 name = first_token->u.value;
30989
30990 cp_lexer_get_preprocessor_token (NULL, first_token);
30991 if (first_token->type != CPP_PRAGMA_EOL)
30992 error_at (first_token->location,
30993 "junk at end of %<#pragma GCC pch_preprocess%>");
30994 }
30995 else
30996 error_at (first_token->location, "expected string literal");
30997
30998 /* Skip to the end of the pragma. */
30999 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31000 cp_lexer_get_preprocessor_token (NULL, first_token);
31001
31002 /* Now actually load the PCH file. */
31003 if (name)
31004 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31005
31006 /* Read one more token to return to our caller. We have to do this
31007 after reading the PCH file in, since its pointers have to be
31008 live. */
31009 cp_lexer_get_preprocessor_token (NULL, first_token);
31010 }
31011
31012 /* Normal parsing of a pragma token. Here we can (and must) use the
31013 regular lexer. */
31014
31015 static bool
31016 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31017 {
31018 cp_token *pragma_tok;
31019 unsigned int id;
31020
31021 pragma_tok = cp_lexer_consume_token (parser->lexer);
31022 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31023 parser->lexer->in_pragma = true;
31024
31025 id = pragma_tok->pragma_kind;
31026 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31027 cp_ensure_no_omp_declare_simd (parser);
31028 switch (id)
31029 {
31030 case PRAGMA_GCC_PCH_PREPROCESS:
31031 error_at (pragma_tok->location,
31032 "%<#pragma GCC pch_preprocess%> must be first");
31033 break;
31034
31035 case PRAGMA_OMP_BARRIER:
31036 switch (context)
31037 {
31038 case pragma_compound:
31039 cp_parser_omp_barrier (parser, pragma_tok);
31040 return false;
31041 case pragma_stmt:
31042 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31043 "used in compound statements");
31044 break;
31045 default:
31046 goto bad_stmt;
31047 }
31048 break;
31049
31050 case PRAGMA_OMP_FLUSH:
31051 switch (context)
31052 {
31053 case pragma_compound:
31054 cp_parser_omp_flush (parser, pragma_tok);
31055 return false;
31056 case pragma_stmt:
31057 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31058 "used in compound statements");
31059 break;
31060 default:
31061 goto bad_stmt;
31062 }
31063 break;
31064
31065 case PRAGMA_OMP_TASKWAIT:
31066 switch (context)
31067 {
31068 case pragma_compound:
31069 cp_parser_omp_taskwait (parser, pragma_tok);
31070 return false;
31071 case pragma_stmt:
31072 error_at (pragma_tok->location,
31073 "%<#pragma omp taskwait%> may only be "
31074 "used in compound statements");
31075 break;
31076 default:
31077 goto bad_stmt;
31078 }
31079 break;
31080
31081 case PRAGMA_OMP_TASKYIELD:
31082 switch (context)
31083 {
31084 case pragma_compound:
31085 cp_parser_omp_taskyield (parser, pragma_tok);
31086 return false;
31087 case pragma_stmt:
31088 error_at (pragma_tok->location,
31089 "%<#pragma omp taskyield%> may only be "
31090 "used in compound statements");
31091 break;
31092 default:
31093 goto bad_stmt;
31094 }
31095 break;
31096
31097 case PRAGMA_OMP_CANCEL:
31098 switch (context)
31099 {
31100 case pragma_compound:
31101 cp_parser_omp_cancel (parser, pragma_tok);
31102 return false;
31103 case pragma_stmt:
31104 error_at (pragma_tok->location,
31105 "%<#pragma omp cancel%> may only be "
31106 "used in compound statements");
31107 break;
31108 default:
31109 goto bad_stmt;
31110 }
31111 break;
31112
31113 case PRAGMA_OMP_CANCELLATION_POINT:
31114 switch (context)
31115 {
31116 case pragma_compound:
31117 cp_parser_omp_cancellation_point (parser, pragma_tok);
31118 return false;
31119 case pragma_stmt:
31120 error_at (pragma_tok->location,
31121 "%<#pragma omp cancellation point%> may only be "
31122 "used in compound statements");
31123 break;
31124 default:
31125 goto bad_stmt;
31126 }
31127 break;
31128
31129 case PRAGMA_OMP_THREADPRIVATE:
31130 cp_parser_omp_threadprivate (parser, pragma_tok);
31131 return false;
31132
31133 case PRAGMA_OMP_DECLARE_REDUCTION:
31134 cp_parser_omp_declare (parser, pragma_tok, context);
31135 return false;
31136
31137 case PRAGMA_OMP_ATOMIC:
31138 case PRAGMA_OMP_CRITICAL:
31139 case PRAGMA_OMP_DISTRIBUTE:
31140 case PRAGMA_OMP_FOR:
31141 case PRAGMA_OMP_MASTER:
31142 case PRAGMA_OMP_ORDERED:
31143 case PRAGMA_OMP_PARALLEL:
31144 case PRAGMA_OMP_SECTIONS:
31145 case PRAGMA_OMP_SIMD:
31146 case PRAGMA_OMP_SINGLE:
31147 case PRAGMA_OMP_TASK:
31148 case PRAGMA_OMP_TASKGROUP:
31149 case PRAGMA_OMP_TEAMS:
31150 if (context != pragma_stmt && context != pragma_compound)
31151 goto bad_stmt;
31152 cp_parser_omp_construct (parser, pragma_tok);
31153 return true;
31154
31155 case PRAGMA_OMP_TARGET:
31156 return cp_parser_omp_target (parser, pragma_tok, context);
31157
31158 case PRAGMA_OMP_END_DECLARE_TARGET:
31159 cp_parser_omp_end_declare_target (parser, pragma_tok);
31160 return false;
31161
31162 case PRAGMA_OMP_SECTION:
31163 error_at (pragma_tok->location,
31164 "%<#pragma omp section%> may only be used in "
31165 "%<#pragma omp sections%> construct");
31166 break;
31167
31168 case PRAGMA_IVDEP:
31169 {
31170 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31171 cp_token *tok;
31172 tok = cp_lexer_peek_token (the_parser->lexer);
31173 if (tok->type != CPP_KEYWORD
31174 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31175 && tok->keyword != RID_DO))
31176 {
31177 cp_parser_error (parser, "for, while or do statement expected");
31178 return false;
31179 }
31180 cp_parser_iteration_statement (parser, true);
31181 return true;
31182 }
31183
31184 case PRAGMA_CILK_SIMD:
31185 if (context == pragma_external)
31186 {
31187 error_at (pragma_tok->location,
31188 "%<#pragma simd%> must be inside a function");
31189 break;
31190 }
31191 cp_parser_cilk_simd (parser, pragma_tok);
31192 return true;
31193
31194 default:
31195 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31196 c_invoke_pragma_handler (id);
31197 break;
31198
31199 bad_stmt:
31200 cp_parser_error (parser, "expected declaration specifiers");
31201 break;
31202 }
31203
31204 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31205 return false;
31206 }
31207
31208 /* The interface the pragma parsers have to the lexer. */
31209
31210 enum cpp_ttype
31211 pragma_lex (tree *value)
31212 {
31213 cp_token *tok;
31214 enum cpp_ttype ret;
31215
31216 tok = cp_lexer_peek_token (the_parser->lexer);
31217
31218 ret = tok->type;
31219 *value = tok->u.value;
31220
31221 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31222 ret = CPP_EOF;
31223 else if (ret == CPP_STRING)
31224 *value = cp_parser_string_literal (the_parser, false, false);
31225 else
31226 {
31227 cp_lexer_consume_token (the_parser->lexer);
31228 if (ret == CPP_KEYWORD)
31229 ret = CPP_NAME;
31230 }
31231
31232 return ret;
31233 }
31234
31235 \f
31236 /* External interface. */
31237
31238 /* Parse one entire translation unit. */
31239
31240 void
31241 c_parse_file (void)
31242 {
31243 static bool already_called = false;
31244
31245 if (already_called)
31246 {
31247 sorry ("inter-module optimizations not implemented for C++");
31248 return;
31249 }
31250 already_called = true;
31251
31252 the_parser = cp_parser_new ();
31253 push_deferring_access_checks (flag_access_control
31254 ? dk_no_deferred : dk_no_check);
31255 cp_parser_translation_unit (the_parser);
31256 the_parser = NULL;
31257 }
31258
31259 /* Parses the Cilk Plus #pragma simd vectorlength clause:
31260 Syntax:
31261 vectorlength ( constant-expression ) */
31262
31263 static tree
31264 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses)
31265 {
31266 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31267 tree expr;
31268 /* The vectorlength clause behaves exactly like OpenMP's safelen
31269 clause. Thus, vectorlength is represented as OMP 4.0
31270 safelen. */
31271 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", loc);
31272
31273 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31274 return error_mark_node;
31275
31276 expr = cp_parser_constant_expression (parser, false, NULL);
31277 expr = maybe_constant_value (expr);
31278
31279 if (TREE_CONSTANT (expr)
31280 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31281 error_at (loc, "vectorlength must be a power of 2");
31282 else if (expr != error_mark_node)
31283 {
31284 tree c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31285 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31286 OMP_CLAUSE_CHAIN (c) = clauses;
31287 clauses = c;
31288 }
31289
31290 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31291 return error_mark_node;
31292 return clauses;
31293 }
31294
31295 /* Handles the Cilk Plus #pragma simd linear clause.
31296 Syntax:
31297 linear ( simd-linear-variable-list )
31298
31299 simd-linear-variable-list:
31300 simd-linear-variable
31301 simd-linear-variable-list , simd-linear-variable
31302
31303 simd-linear-variable:
31304 id-expression
31305 id-expression : simd-linear-step
31306
31307 simd-linear-step:
31308 conditional-expression */
31309
31310 static tree
31311 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
31312 {
31313 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31314
31315 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31316 return clauses;
31317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31318 {
31319 cp_parser_error (parser, "expected identifier");
31320 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31321 return error_mark_node;
31322 }
31323
31324 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31325 parser->colon_corrects_to_scope_p = false;
31326 while (1)
31327 {
31328 cp_token *token = cp_lexer_peek_token (parser->lexer);
31329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31330 {
31331 cp_parser_error (parser, "expected variable-name");
31332 clauses = error_mark_node;
31333 break;
31334 }
31335
31336 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
31337 false, false);
31338 tree decl = cp_parser_lookup_name_simple (parser, var_name,
31339 token->location);
31340 if (decl == error_mark_node)
31341 {
31342 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
31343 token->location);
31344 clauses = error_mark_node;
31345 }
31346 else
31347 {
31348 tree e = NULL_TREE;
31349 tree step_size = integer_one_node;
31350
31351 /* If present, parse the linear step. Otherwise, assume the default
31352 value of 1. */
31353 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
31354 {
31355 cp_lexer_consume_token (parser->lexer);
31356
31357 e = cp_parser_assignment_expression (parser, false, NULL);
31358 e = maybe_constant_value (e);
31359
31360 if (e == error_mark_node)
31361 {
31362 /* If an error has occurred, then the whole pragma is
31363 considered ill-formed. Thus, no reason to keep
31364 parsing. */
31365 clauses = error_mark_node;
31366 break;
31367 }
31368 else if (type_dependent_expression_p (e)
31369 || value_dependent_expression_p (e)
31370 || (TREE_TYPE (e)
31371 && INTEGRAL_TYPE_P (TREE_TYPE (e))
31372 && (TREE_CONSTANT (e)
31373 || DECL_P (e))))
31374 step_size = e;
31375 else
31376 cp_parser_error (parser,
31377 "step size must be an integer constant "
31378 "expression or an integer variable");
31379 }
31380
31381 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31382 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31383 OMP_CLAUSE_DECL (l) = decl;
31384 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
31385 OMP_CLAUSE_CHAIN (l) = clauses;
31386 clauses = l;
31387 }
31388 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31389 cp_lexer_consume_token (parser->lexer);
31390 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31391 break;
31392 else
31393 {
31394 error_at (cp_lexer_peek_token (parser->lexer)->location,
31395 "expected %<,%> or %<)%> after %qE", decl);
31396 clauses = error_mark_node;
31397 break;
31398 }
31399 }
31400 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31401 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31402 return clauses;
31403 }
31404
31405 /* Returns the name of the next clause. If the clause is not
31406 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31407 token is not consumed. Otherwise, the appropriate enum from the
31408 pragma_simd_clause is returned and the token is consumed. */
31409
31410 static pragma_cilk_clause
31411 cp_parser_cilk_simd_clause_name (cp_parser *parser)
31412 {
31413 pragma_cilk_clause clause_type;
31414 cp_token *token = cp_lexer_peek_token (parser->lexer);
31415
31416 if (token->keyword == RID_PRIVATE)
31417 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
31418 else if (!token->u.value || token->type != CPP_NAME)
31419 return PRAGMA_CILK_CLAUSE_NONE;
31420 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
31421 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31422 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
31423 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
31424 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
31425 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
31426 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
31427 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
31428 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
31429 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
31430 else
31431 return PRAGMA_CILK_CLAUSE_NONE;
31432
31433 cp_lexer_consume_token (parser->lexer);
31434 return clause_type;
31435 }
31436
31437 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31438
31439 static tree
31440 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
31441 {
31442 tree clauses = NULL_TREE;
31443
31444 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31445 && clauses != error_mark_node)
31446 {
31447 pragma_cilk_clause c_kind;
31448 c_kind = cp_parser_cilk_simd_clause_name (parser);
31449 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
31450 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses);
31451 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
31452 clauses = cp_parser_cilk_simd_linear (parser, clauses);
31453 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
31454 /* Use the OpenMP 4.0 equivalent function. */
31455 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
31456 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
31457 /* Use the OpenMP 4.0 equivalent function. */
31458 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31459 clauses);
31460 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
31461 /* Use the OMP 4.0 equivalent function. */
31462 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31463 clauses);
31464 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
31465 /* Use the OMP 4.0 equivalent function. */
31466 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31467 else
31468 {
31469 clauses = error_mark_node;
31470 cp_parser_error (parser, "expected %<#pragma simd%> clause");
31471 break;
31472 }
31473 }
31474
31475 cp_parser_skip_to_pragma_eol (parser, pragma_token);
31476
31477 if (clauses == error_mark_node)
31478 return error_mark_node;
31479 else
31480 return c_finish_cilk_clauses (clauses);
31481 }
31482
31483 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31484
31485 static void
31486 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
31487 {
31488 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
31489
31490 if (clauses == error_mark_node)
31491 return;
31492
31493 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
31494 {
31495 error_at (cp_lexer_peek_token (parser->lexer)->location,
31496 "for statement expected");
31497 return;
31498 }
31499
31500 tree sb = begin_omp_structured_block ();
31501 int save = cp_parser_begin_omp_structured_block (parser);
31502 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
31503 if (ret)
31504 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
31505 cp_parser_end_omp_structured_block (parser, save);
31506 add_stmt (finish_omp_structured_block (sb));
31507 return;
31508 }
31509
31510 /* Create an identifier for a generic parameter type (a synthesized
31511 template parameter implied by `auto' or a concept identifier). */
31512
31513 static GTY(()) int generic_parm_count;
31514 static tree
31515 make_generic_type_name ()
31516 {
31517 char buf[32];
31518 sprintf (buf, "auto:%d", ++generic_parm_count);
31519 return get_identifier (buf);
31520 }
31521
31522 /* Predicate that behaves as is_auto_or_concept but matches the parent
31523 node of the generic type rather than the generic type itself. This
31524 allows for type transformation in add_implicit_template_parms. */
31525
31526 static inline bool
31527 tree_type_is_auto_or_concept (const_tree t)
31528 {
31529 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
31530 }
31531
31532 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
31533 (creating a new template parameter list if necessary). Returns the newly
31534 created template type parm. */
31535
31536 tree
31537 synthesize_implicit_template_parm (cp_parser *parser)
31538 {
31539 gcc_assert (current_binding_level->kind == sk_function_parms);
31540
31541 /* We are either continuing a function template that already contains implicit
31542 template parameters, creating a new fully-implicit function template, or
31543 extending an existing explicit function template with implicit template
31544 parameters. */
31545
31546 cp_binding_level *const entry_scope = current_binding_level;
31547
31548 bool become_template = false;
31549 cp_binding_level *parent_scope = 0;
31550
31551 if (parser->implicit_template_scope)
31552 {
31553 gcc_assert (parser->implicit_template_parms);
31554
31555 current_binding_level = parser->implicit_template_scope;
31556 }
31557 else
31558 {
31559 /* Roll back to the existing template parameter scope (in the case of
31560 extending an explicit function template) or introduce a new template
31561 parameter scope ahead of the function parameter scope (or class scope
31562 in the case of out-of-line member definitions). The function scope is
31563 added back after template parameter synthesis below. */
31564
31565 cp_binding_level *scope = entry_scope;
31566
31567 while (scope->kind == sk_function_parms)
31568 {
31569 parent_scope = scope;
31570 scope = scope->level_chain;
31571 }
31572 if (current_class_type && !LAMBDA_TYPE_P (current_class_type)
31573 && parser->num_classes_being_defined == 0)
31574 while (scope->kind == sk_class)
31575 {
31576 parent_scope = scope;
31577 scope = scope->level_chain;
31578 }
31579
31580 current_binding_level = scope;
31581
31582 if (scope->kind != sk_template_parms)
31583 {
31584 /* Introduce a new template parameter list for implicit template
31585 parameters. */
31586
31587 become_template = true;
31588
31589 parser->implicit_template_scope
31590 = begin_scope (sk_template_parms, NULL);
31591
31592 ++processing_template_decl;
31593
31594 parser->fully_implicit_function_template_p = true;
31595 ++parser->num_template_parameter_lists;
31596 }
31597 else
31598 {
31599 /* Synthesize implicit template parameters at the end of the explicit
31600 template parameter list. */
31601
31602 gcc_assert (current_template_parms);
31603
31604 parser->implicit_template_scope = scope;
31605
31606 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
31607 parser->implicit_template_parms
31608 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
31609 }
31610 }
31611
31612 /* Synthesize a new template parameter and track the current template
31613 parameter chain with implicit_template_parms. */
31614
31615 tree synth_id = make_generic_type_name ();
31616 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
31617 synth_id);
31618 tree new_parm
31619 = process_template_parm (parser->implicit_template_parms,
31620 input_location,
31621 build_tree_list (NULL_TREE, synth_tmpl_parm),
31622 /*non_type=*/false,
31623 /*param_pack=*/false);
31624
31625
31626 if (parser->implicit_template_parms)
31627 parser->implicit_template_parms
31628 = TREE_CHAIN (parser->implicit_template_parms);
31629 else
31630 parser->implicit_template_parms = new_parm;
31631
31632 tree new_type = TREE_TYPE (getdecls ());
31633
31634 /* If creating a fully implicit function template, start the new implicit
31635 template parameter list with this synthesized type, otherwise grow the
31636 current template parameter list. */
31637
31638 if (become_template)
31639 {
31640 parent_scope->level_chain = current_binding_level;
31641
31642 tree new_parms = make_tree_vec (1);
31643 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
31644 current_template_parms = tree_cons (size_int (processing_template_decl),
31645 new_parms, current_template_parms);
31646 }
31647 else
31648 {
31649 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
31650 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
31651 new_parms = grow_tree_vec_stat (new_parms, new_parm_idx + 1);
31652 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
31653 }
31654
31655 current_binding_level = entry_scope;
31656
31657 return new_type;
31658 }
31659
31660 /* Finish the declaration of a fully implicit function template. Such a
31661 template has no explicit template parameter list so has not been through the
31662 normal template head and tail processing. synthesize_implicit_template_parm
31663 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
31664 provided if the declaration is a class member such that its template
31665 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
31666 form is returned. Otherwise NULL_TREE is returned. */
31667
31668 tree
31669 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
31670 {
31671 gcc_assert (parser->fully_implicit_function_template_p);
31672
31673 if (member_decl_opt && member_decl_opt != error_mark_node
31674 && DECL_VIRTUAL_P (member_decl_opt))
31675 {
31676 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
31677 "implicit templates may not be %<virtual%>");
31678 DECL_VIRTUAL_P (member_decl_opt) = false;
31679 }
31680
31681 if (member_decl_opt)
31682 member_decl_opt = finish_member_template_decl (member_decl_opt);
31683 end_template_decl ();
31684
31685 parser->fully_implicit_function_template_p = false;
31686 --parser->num_template_parameter_lists;
31687
31688 return member_decl_opt;
31689 }
31690
31691 #include "gt-cp-parser.h"