re PR c++/58162 ([C++11] bogus error: use of deleted function 'constexpr A::A(const...
[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 bool cp_parser_omp_declare_reduction_exprs
237 (tree, cp_parser *);
238
239 /* Manifest constants. */
240 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
241 #define CP_SAVED_TOKEN_STACK 5
242
243 /* Variables. */
244
245 /* The stream to which debugging output should be written. */
246 static FILE *cp_lexer_debug_stream;
247
248 /* Nonzero if we are parsing an unevaluated operand: an operand to
249 sizeof, typeof, or alignof. */
250 int cp_unevaluated_operand;
251
252 /* Dump up to NUM tokens in BUFFER to FILE starting with token
253 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
254 first token in BUFFER. If NUM is 0, dump all the tokens. If
255 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
256 highlighted by surrounding it in [[ ]]. */
257
258 static void
259 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
260 cp_token *start_token, unsigned num,
261 cp_token *curr_token)
262 {
263 unsigned i, nprinted;
264 cp_token *token;
265 bool do_print;
266
267 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
268
269 if (buffer == NULL)
270 return;
271
272 if (num == 0)
273 num = buffer->length ();
274
275 if (start_token == NULL)
276 start_token = buffer->address ();
277
278 if (start_token > buffer->address ())
279 {
280 cp_lexer_print_token (file, &(*buffer)[0]);
281 fprintf (file, " ... ");
282 }
283
284 do_print = false;
285 nprinted = 0;
286 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
287 {
288 if (token == start_token)
289 do_print = true;
290
291 if (!do_print)
292 continue;
293
294 nprinted++;
295 if (token == curr_token)
296 fprintf (file, "[[");
297
298 cp_lexer_print_token (file, token);
299
300 if (token == curr_token)
301 fprintf (file, "]]");
302
303 switch (token->type)
304 {
305 case CPP_SEMICOLON:
306 case CPP_OPEN_BRACE:
307 case CPP_CLOSE_BRACE:
308 case CPP_EOF:
309 fputc ('\n', file);
310 break;
311
312 default:
313 fputc (' ', file);
314 }
315 }
316
317 if (i == num && i < buffer->length ())
318 {
319 fprintf (file, " ... ");
320 cp_lexer_print_token (file, &buffer->last ());
321 }
322
323 fprintf (file, "\n");
324 }
325
326
327 /* Dump all tokens in BUFFER to stderr. */
328
329 void
330 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
331 {
332 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
333 }
334
335 DEBUG_FUNCTION void
336 debug (vec<cp_token, va_gc> &ref)
337 {
338 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
339 }
340
341 DEBUG_FUNCTION void
342 debug (vec<cp_token, va_gc> *ptr)
343 {
344 if (ptr)
345 debug (*ptr);
346 else
347 fprintf (stderr, "<nil>\n");
348 }
349
350
351 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
352 description for T. */
353
354 static void
355 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
356 {
357 if (t)
358 {
359 fprintf (file, "%s: ", desc);
360 print_node_brief (file, "", t, 0);
361 }
362 }
363
364
365 /* Dump parser context C to FILE. */
366
367 static void
368 cp_debug_print_context (FILE *file, cp_parser_context *c)
369 {
370 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
371 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
372 print_node_brief (file, "", c->object_type, 0);
373 fprintf (file, "}\n");
374 }
375
376
377 /* Print the stack of parsing contexts to FILE starting with FIRST. */
378
379 static void
380 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
381 {
382 unsigned i;
383 cp_parser_context *c;
384
385 fprintf (file, "Parsing context stack:\n");
386 for (i = 0, c = first; c; c = c->next, i++)
387 {
388 fprintf (file, "\t#%u: ", i);
389 cp_debug_print_context (file, c);
390 }
391 }
392
393
394 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
395
396 static void
397 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
398 {
399 if (flag)
400 fprintf (file, "%s: true\n", desc);
401 }
402
403
404 /* Print an unparsed function entry UF to FILE. */
405
406 static void
407 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
408 {
409 unsigned i;
410 cp_default_arg_entry *default_arg_fn;
411 tree fn;
412
413 fprintf (file, "\tFunctions with default args:\n");
414 for (i = 0;
415 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
416 i++)
417 {
418 fprintf (file, "\t\tClass type: ");
419 print_node_brief (file, "", default_arg_fn->class_type, 0);
420 fprintf (file, "\t\tDeclaration: ");
421 print_node_brief (file, "", default_arg_fn->decl, 0);
422 fprintf (file, "\n");
423 }
424
425 fprintf (file, "\n\tFunctions with definitions that require "
426 "post-processing\n\t\t");
427 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
428 {
429 print_node_brief (file, "", fn, 0);
430 fprintf (file, " ");
431 }
432 fprintf (file, "\n");
433
434 fprintf (file, "\n\tNon-static data members with initializers that require "
435 "post-processing\n\t\t");
436 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
437 {
438 print_node_brief (file, "", fn, 0);
439 fprintf (file, " ");
440 }
441 fprintf (file, "\n");
442 }
443
444
445 /* Print the stack of unparsed member functions S to FILE. */
446
447 static void
448 cp_debug_print_unparsed_queues (FILE *file,
449 vec<cp_unparsed_functions_entry, va_gc> *s)
450 {
451 unsigned i;
452 cp_unparsed_functions_entry *uf;
453
454 fprintf (file, "Unparsed functions\n");
455 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
456 {
457 fprintf (file, "#%u:\n", i);
458 cp_debug_print_unparsed_function (file, uf);
459 }
460 }
461
462
463 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
464 the given PARSER. If FILE is NULL, the output is printed on stderr. */
465
466 static void
467 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
468 {
469 cp_token *next_token, *first_token, *start_token;
470
471 if (file == NULL)
472 file = stderr;
473
474 next_token = parser->lexer->next_token;
475 first_token = parser->lexer->buffer->address ();
476 start_token = (next_token > first_token + window_size / 2)
477 ? next_token - window_size / 2
478 : first_token;
479 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
480 next_token);
481 }
482
483
484 /* Dump debugging information for the given PARSER. If FILE is NULL,
485 the output is printed on stderr. */
486
487 void
488 cp_debug_parser (FILE *file, cp_parser *parser)
489 {
490 const size_t window_size = 20;
491 cp_token *token;
492 expanded_location eloc;
493
494 if (file == NULL)
495 file = stderr;
496
497 fprintf (file, "Parser state\n\n");
498 fprintf (file, "Number of tokens: %u\n",
499 vec_safe_length (parser->lexer->buffer));
500 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
501 cp_debug_print_tree_if_set (file, "Object scope",
502 parser->object_scope);
503 cp_debug_print_tree_if_set (file, "Qualifying scope",
504 parser->qualifying_scope);
505 cp_debug_print_context_stack (file, parser->context);
506 cp_debug_print_flag (file, "Allow GNU extensions",
507 parser->allow_gnu_extensions_p);
508 cp_debug_print_flag (file, "'>' token is greater-than",
509 parser->greater_than_is_operator_p);
510 cp_debug_print_flag (file, "Default args allowed in current "
511 "parameter list", parser->default_arg_ok_p);
512 cp_debug_print_flag (file, "Parsing integral constant-expression",
513 parser->integral_constant_expression_p);
514 cp_debug_print_flag (file, "Allow non-constant expression in current "
515 "constant-expression",
516 parser->allow_non_integral_constant_expression_p);
517 cp_debug_print_flag (file, "Seen non-constant expression",
518 parser->non_integral_constant_expression_p);
519 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
520 "current context",
521 parser->local_variables_forbidden_p);
522 cp_debug_print_flag (file, "In unbraced linkage specification",
523 parser->in_unbraced_linkage_specification_p);
524 cp_debug_print_flag (file, "Parsing a declarator",
525 parser->in_declarator_p);
526 cp_debug_print_flag (file, "In template argument list",
527 parser->in_template_argument_list_p);
528 cp_debug_print_flag (file, "Parsing an iteration statement",
529 parser->in_statement & IN_ITERATION_STMT);
530 cp_debug_print_flag (file, "Parsing a switch statement",
531 parser->in_statement & IN_SWITCH_STMT);
532 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
533 parser->in_statement & IN_OMP_BLOCK);
534 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
535 parser->in_statement & IN_OMP_FOR);
536 cp_debug_print_flag (file, "Parsing an if statement",
537 parser->in_statement & IN_IF_STMT);
538 cp_debug_print_flag (file, "Parsing a type-id in an expression "
539 "context", parser->in_type_id_in_expr_p);
540 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
541 parser->implicit_extern_c);
542 cp_debug_print_flag (file, "String expressions should be translated "
543 "to execution character set",
544 parser->translate_strings_p);
545 cp_debug_print_flag (file, "Parsing function body outside of a "
546 "local class", parser->in_function_body);
547 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
548 parser->colon_corrects_to_scope_p);
549 cp_debug_print_flag (file, "Colon doesn't start a class definition",
550 parser->colon_doesnt_start_class_def_p);
551 if (parser->type_definition_forbidden_message)
552 fprintf (file, "Error message for forbidden type definitions: %s\n",
553 parser->type_definition_forbidden_message);
554 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
555 fprintf (file, "Number of class definitions in progress: %u\n",
556 parser->num_classes_being_defined);
557 fprintf (file, "Number of template parameter lists for the current "
558 "declaration: %u\n", parser->num_template_parameter_lists);
559 cp_debug_parser_tokens (file, parser, window_size);
560 token = parser->lexer->next_token;
561 fprintf (file, "Next token to parse:\n");
562 fprintf (file, "\tToken: ");
563 cp_lexer_print_token (file, token);
564 eloc = expand_location (token->location);
565 fprintf (file, "\n\tFile: %s\n", eloc.file);
566 fprintf (file, "\tLine: %d\n", eloc.line);
567 fprintf (file, "\tColumn: %d\n", eloc.column);
568 }
569
570 DEBUG_FUNCTION void
571 debug (cp_parser &ref)
572 {
573 cp_debug_parser (stderr, &ref);
574 }
575
576 DEBUG_FUNCTION void
577 debug (cp_parser *ptr)
578 {
579 if (ptr)
580 debug (*ptr);
581 else
582 fprintf (stderr, "<nil>\n");
583 }
584
585 /* Allocate memory for a new lexer object and return it. */
586
587 static cp_lexer *
588 cp_lexer_alloc (void)
589 {
590 cp_lexer *lexer;
591
592 c_common_no_more_pch ();
593
594 /* Allocate the memory. */
595 lexer = ggc_alloc_cleared_cp_lexer ();
596
597 /* Initially we are not debugging. */
598 lexer->debugging_p = false;
599
600 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
601
602 /* Create the buffer. */
603 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
604
605 return lexer;
606 }
607
608
609 /* Create a new main C++ lexer, the lexer that gets tokens from the
610 preprocessor. */
611
612 static cp_lexer *
613 cp_lexer_new_main (void)
614 {
615 cp_lexer *lexer;
616 cp_token token;
617
618 /* It's possible that parsing the first pragma will load a PCH file,
619 which is a GC collection point. So we have to do that before
620 allocating any memory. */
621 cp_parser_initial_pragma (&token);
622
623 lexer = cp_lexer_alloc ();
624
625 /* Put the first token in the buffer. */
626 lexer->buffer->quick_push (token);
627
628 /* Get the remaining tokens from the preprocessor. */
629 while (token.type != CPP_EOF)
630 {
631 cp_lexer_get_preprocessor_token (lexer, &token);
632 vec_safe_push (lexer->buffer, token);
633 }
634
635 lexer->last_token = lexer->buffer->address ()
636 + lexer->buffer->length ()
637 - 1;
638 lexer->next_token = lexer->buffer->length ()
639 ? lexer->buffer->address ()
640 : &eof_token;
641
642 /* Subsequent preprocessor diagnostics should use compiler
643 diagnostic functions to get the compiler source location. */
644 done_lexing = true;
645
646 gcc_assert (!lexer->next_token->purged_p);
647 return lexer;
648 }
649
650 /* Create a new lexer whose token stream is primed with the tokens in
651 CACHE. When these tokens are exhausted, no new tokens will be read. */
652
653 static cp_lexer *
654 cp_lexer_new_from_tokens (cp_token_cache *cache)
655 {
656 cp_token *first = cache->first;
657 cp_token *last = cache->last;
658 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
659
660 /* We do not own the buffer. */
661 lexer->buffer = NULL;
662 lexer->next_token = first == last ? &eof_token : first;
663 lexer->last_token = last;
664
665 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
666
667 /* Initially we are not debugging. */
668 lexer->debugging_p = false;
669
670 gcc_assert (!lexer->next_token->purged_p);
671 return lexer;
672 }
673
674 /* Frees all resources associated with LEXER. */
675
676 static void
677 cp_lexer_destroy (cp_lexer *lexer)
678 {
679 vec_free (lexer->buffer);
680 lexer->saved_tokens.release ();
681 ggc_free (lexer);
682 }
683
684 /* Returns nonzero if debugging information should be output. */
685
686 static inline bool
687 cp_lexer_debugging_p (cp_lexer *lexer)
688 {
689 return lexer->debugging_p;
690 }
691
692
693 static inline cp_token_position
694 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
695 {
696 gcc_assert (!previous_p || lexer->next_token != &eof_token);
697
698 return lexer->next_token - previous_p;
699 }
700
701 static inline cp_token *
702 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
703 {
704 return pos;
705 }
706
707 static inline void
708 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
709 {
710 lexer->next_token = cp_lexer_token_at (lexer, pos);
711 }
712
713 static inline cp_token_position
714 cp_lexer_previous_token_position (cp_lexer *lexer)
715 {
716 if (lexer->next_token == &eof_token)
717 return lexer->last_token - 1;
718 else
719 return cp_lexer_token_position (lexer, true);
720 }
721
722 static inline cp_token *
723 cp_lexer_previous_token (cp_lexer *lexer)
724 {
725 cp_token_position tp = cp_lexer_previous_token_position (lexer);
726
727 return cp_lexer_token_at (lexer, tp);
728 }
729
730 /* nonzero if we are presently saving tokens. */
731
732 static inline int
733 cp_lexer_saving_tokens (const cp_lexer* lexer)
734 {
735 return lexer->saved_tokens.length () != 0;
736 }
737
738 /* Store the next token from the preprocessor in *TOKEN. Return true
739 if we reach EOF. If LEXER is NULL, assume we are handling an
740 initial #pragma pch_preprocess, and thus want the lexer to return
741 processed strings. */
742
743 static void
744 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
745 {
746 static int is_extern_c = 0;
747
748 /* Get a new token from the preprocessor. */
749 token->type
750 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
751 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
752 token->keyword = RID_MAX;
753 token->pragma_kind = PRAGMA_NONE;
754 token->purged_p = false;
755
756 /* On some systems, some header files are surrounded by an
757 implicit extern "C" block. Set a flag in the token if it
758 comes from such a header. */
759 is_extern_c += pending_lang_change;
760 pending_lang_change = 0;
761 token->implicit_extern_c = is_extern_c > 0;
762
763 /* Check to see if this token is a keyword. */
764 if (token->type == CPP_NAME)
765 {
766 if (C_IS_RESERVED_WORD (token->u.value))
767 {
768 /* Mark this token as a keyword. */
769 token->type = CPP_KEYWORD;
770 /* Record which keyword. */
771 token->keyword = C_RID_CODE (token->u.value);
772 }
773 else
774 {
775 if (warn_cxx0x_compat
776 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
777 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
778 {
779 /* Warn about the C++0x keyword (but still treat it as
780 an identifier). */
781 warning (OPT_Wc__0x_compat,
782 "identifier %qE is a keyword in C++11",
783 token->u.value);
784
785 /* Clear out the C_RID_CODE so we don't warn about this
786 particular identifier-turned-keyword again. */
787 C_SET_RID_CODE (token->u.value, RID_MAX);
788 }
789
790 token->ambiguous_p = false;
791 token->keyword = RID_MAX;
792 }
793 }
794 else if (token->type == CPP_AT_NAME)
795 {
796 /* This only happens in Objective-C++; it must be a keyword. */
797 token->type = CPP_KEYWORD;
798 switch (C_RID_CODE (token->u.value))
799 {
800 /* Replace 'class' with '@class', 'private' with '@private',
801 etc. This prevents confusion with the C++ keyword
802 'class', and makes the tokens consistent with other
803 Objective-C 'AT' keywords. For example '@class' is
804 reported as RID_AT_CLASS which is consistent with
805 '@synchronized', which is reported as
806 RID_AT_SYNCHRONIZED.
807 */
808 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
809 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
810 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
811 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
812 case RID_THROW: token->keyword = RID_AT_THROW; break;
813 case RID_TRY: token->keyword = RID_AT_TRY; break;
814 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
815 default: token->keyword = C_RID_CODE (token->u.value);
816 }
817 }
818 else if (token->type == CPP_PRAGMA)
819 {
820 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
821 token->pragma_kind = ((enum pragma_kind)
822 TREE_INT_CST_LOW (token->u.value));
823 token->u.value = NULL_TREE;
824 }
825 }
826
827 /* Update the globals input_location and the input file stack from TOKEN. */
828 static inline void
829 cp_lexer_set_source_position_from_token (cp_token *token)
830 {
831 if (token->type != CPP_EOF)
832 {
833 input_location = token->location;
834 }
835 }
836
837 /* Return a pointer to the next token in the token stream, but do not
838 consume it. */
839
840 static inline cp_token *
841 cp_lexer_peek_token (cp_lexer *lexer)
842 {
843 if (cp_lexer_debugging_p (lexer))
844 {
845 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
846 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
847 putc ('\n', cp_lexer_debug_stream);
848 }
849 return lexer->next_token;
850 }
851
852 /* Return true if the next token has the indicated TYPE. */
853
854 static inline bool
855 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
856 {
857 return cp_lexer_peek_token (lexer)->type == type;
858 }
859
860 /* Return true if the next token does not have the indicated TYPE. */
861
862 static inline bool
863 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
864 {
865 return !cp_lexer_next_token_is (lexer, type);
866 }
867
868 /* Return true if the next token is the indicated KEYWORD. */
869
870 static inline bool
871 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
872 {
873 return cp_lexer_peek_token (lexer)->keyword == keyword;
874 }
875
876 static inline bool
877 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
878 {
879 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
880 }
881
882 /* Return true if the next token is not the indicated KEYWORD. */
883
884 static inline bool
885 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
886 {
887 return cp_lexer_peek_token (lexer)->keyword != keyword;
888 }
889
890 /* Return true if the next token is a keyword for a decl-specifier. */
891
892 static bool
893 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
894 {
895 cp_token *token;
896
897 token = cp_lexer_peek_token (lexer);
898 switch (token->keyword)
899 {
900 /* auto specifier: storage-class-specifier in C++,
901 simple-type-specifier in C++0x. */
902 case RID_AUTO:
903 /* Storage classes. */
904 case RID_REGISTER:
905 case RID_STATIC:
906 case RID_EXTERN:
907 case RID_MUTABLE:
908 case RID_THREAD:
909 /* Elaborated type specifiers. */
910 case RID_ENUM:
911 case RID_CLASS:
912 case RID_STRUCT:
913 case RID_UNION:
914 case RID_TYPENAME:
915 /* Simple type specifiers. */
916 case RID_CHAR:
917 case RID_CHAR16:
918 case RID_CHAR32:
919 case RID_WCHAR:
920 case RID_BOOL:
921 case RID_SHORT:
922 case RID_INT:
923 case RID_LONG:
924 case RID_INT128:
925 case RID_SIGNED:
926 case RID_UNSIGNED:
927 case RID_FLOAT:
928 case RID_DOUBLE:
929 case RID_VOID:
930 /* GNU extensions. */
931 case RID_ATTRIBUTE:
932 case RID_TYPEOF:
933 /* C++0x extensions. */
934 case RID_DECLTYPE:
935 case RID_UNDERLYING_TYPE:
936 return true;
937
938 default:
939 return false;
940 }
941 }
942
943 /* Returns TRUE iff the token T begins a decltype type. */
944
945 static bool
946 token_is_decltype (cp_token *t)
947 {
948 return (t->keyword == RID_DECLTYPE
949 || t->type == CPP_DECLTYPE);
950 }
951
952 /* Returns TRUE iff the next token begins a decltype type. */
953
954 static bool
955 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
956 {
957 cp_token *t = cp_lexer_peek_token (lexer);
958 return token_is_decltype (t);
959 }
960
961 /* Return a pointer to the Nth token in the token stream. If N is 1,
962 then this is precisely equivalent to cp_lexer_peek_token (except
963 that it is not inline). One would like to disallow that case, but
964 there is one case (cp_parser_nth_token_starts_template_id) where
965 the caller passes a variable for N and it might be 1. */
966
967 static cp_token *
968 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
969 {
970 cp_token *token;
971
972 /* N is 1-based, not zero-based. */
973 gcc_assert (n > 0);
974
975 if (cp_lexer_debugging_p (lexer))
976 fprintf (cp_lexer_debug_stream,
977 "cp_lexer: peeking ahead %ld at token: ", (long)n);
978
979 --n;
980 token = lexer->next_token;
981 gcc_assert (!n || token != &eof_token);
982 while (n != 0)
983 {
984 ++token;
985 if (token == lexer->last_token)
986 {
987 token = &eof_token;
988 break;
989 }
990
991 if (!token->purged_p)
992 --n;
993 }
994
995 if (cp_lexer_debugging_p (lexer))
996 {
997 cp_lexer_print_token (cp_lexer_debug_stream, token);
998 putc ('\n', cp_lexer_debug_stream);
999 }
1000
1001 return token;
1002 }
1003
1004 /* Return the next token, and advance the lexer's next_token pointer
1005 to point to the next non-purged token. */
1006
1007 static cp_token *
1008 cp_lexer_consume_token (cp_lexer* lexer)
1009 {
1010 cp_token *token = lexer->next_token;
1011
1012 gcc_assert (token != &eof_token);
1013 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1014
1015 do
1016 {
1017 lexer->next_token++;
1018 if (lexer->next_token == lexer->last_token)
1019 {
1020 lexer->next_token = &eof_token;
1021 break;
1022 }
1023
1024 }
1025 while (lexer->next_token->purged_p);
1026
1027 cp_lexer_set_source_position_from_token (token);
1028
1029 /* Provide debugging output. */
1030 if (cp_lexer_debugging_p (lexer))
1031 {
1032 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1033 cp_lexer_print_token (cp_lexer_debug_stream, token);
1034 putc ('\n', cp_lexer_debug_stream);
1035 }
1036
1037 return token;
1038 }
1039
1040 /* Permanently remove the next token from the token stream, and
1041 advance the next_token pointer to refer to the next non-purged
1042 token. */
1043
1044 static void
1045 cp_lexer_purge_token (cp_lexer *lexer)
1046 {
1047 cp_token *tok = lexer->next_token;
1048
1049 gcc_assert (tok != &eof_token);
1050 tok->purged_p = true;
1051 tok->location = UNKNOWN_LOCATION;
1052 tok->u.value = NULL_TREE;
1053 tok->keyword = RID_MAX;
1054
1055 do
1056 {
1057 tok++;
1058 if (tok == lexer->last_token)
1059 {
1060 tok = &eof_token;
1061 break;
1062 }
1063 }
1064 while (tok->purged_p);
1065 lexer->next_token = tok;
1066 }
1067
1068 /* Permanently remove all tokens after TOK, up to, but not
1069 including, the token that will be returned next by
1070 cp_lexer_peek_token. */
1071
1072 static void
1073 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1074 {
1075 cp_token *peek = lexer->next_token;
1076
1077 if (peek == &eof_token)
1078 peek = lexer->last_token;
1079
1080 gcc_assert (tok < peek);
1081
1082 for ( tok += 1; tok != peek; tok += 1)
1083 {
1084 tok->purged_p = true;
1085 tok->location = UNKNOWN_LOCATION;
1086 tok->u.value = NULL_TREE;
1087 tok->keyword = RID_MAX;
1088 }
1089 }
1090
1091 /* Begin saving tokens. All tokens consumed after this point will be
1092 preserved. */
1093
1094 static void
1095 cp_lexer_save_tokens (cp_lexer* lexer)
1096 {
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer))
1099 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1100
1101 lexer->saved_tokens.safe_push (lexer->next_token);
1102 }
1103
1104 /* Commit to the portion of the token stream most recently saved. */
1105
1106 static void
1107 cp_lexer_commit_tokens (cp_lexer* lexer)
1108 {
1109 /* Provide debugging output. */
1110 if (cp_lexer_debugging_p (lexer))
1111 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1112
1113 lexer->saved_tokens.pop ();
1114 }
1115
1116 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1117 to the token stream. Stop saving tokens. */
1118
1119 static void
1120 cp_lexer_rollback_tokens (cp_lexer* lexer)
1121 {
1122 /* Provide debugging output. */
1123 if (cp_lexer_debugging_p (lexer))
1124 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1125
1126 lexer->next_token = lexer->saved_tokens.pop ();
1127 }
1128
1129 /* Print a representation of the TOKEN on the STREAM. */
1130
1131 static void
1132 cp_lexer_print_token (FILE * stream, cp_token *token)
1133 {
1134 /* We don't use cpp_type2name here because the parser defines
1135 a few tokens of its own. */
1136 static const char *const token_names[] = {
1137 /* cpplib-defined token types */
1138 #define OP(e, s) #e,
1139 #define TK(e, s) #e,
1140 TTYPE_TABLE
1141 #undef OP
1142 #undef TK
1143 /* C++ parser token types - see "Manifest constants", above. */
1144 "KEYWORD",
1145 "TEMPLATE_ID",
1146 "NESTED_NAME_SPECIFIER",
1147 };
1148
1149 /* For some tokens, print the associated data. */
1150 switch (token->type)
1151 {
1152 case CPP_KEYWORD:
1153 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1154 For example, `struct' is mapped to an INTEGER_CST. */
1155 if (!identifier_p (token->u.value))
1156 break;
1157 /* else fall through */
1158 case CPP_NAME:
1159 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1160 break;
1161
1162 case CPP_STRING:
1163 case CPP_STRING16:
1164 case CPP_STRING32:
1165 case CPP_WSTRING:
1166 case CPP_UTF8STRING:
1167 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1168 break;
1169
1170 case CPP_NUMBER:
1171 print_generic_expr (stream, token->u.value, 0);
1172 break;
1173
1174 default:
1175 /* If we have a name for the token, print it out. Otherwise, we
1176 simply give the numeric code. */
1177 if (token->type < ARRAY_SIZE(token_names))
1178 fputs (token_names[token->type], stream);
1179 else
1180 fprintf (stream, "[%d]", token->type);
1181 break;
1182 }
1183 }
1184
1185 DEBUG_FUNCTION void
1186 debug (cp_token &ref)
1187 {
1188 cp_lexer_print_token (stderr, &ref);
1189 fprintf (stderr, "\n");
1190 }
1191
1192 DEBUG_FUNCTION void
1193 debug (cp_token *ptr)
1194 {
1195 if (ptr)
1196 debug (*ptr);
1197 else
1198 fprintf (stderr, "<nil>\n");
1199 }
1200
1201
1202 /* Start emitting debugging information. */
1203
1204 static void
1205 cp_lexer_start_debugging (cp_lexer* lexer)
1206 {
1207 lexer->debugging_p = true;
1208 cp_lexer_debug_stream = stderr;
1209 }
1210
1211 /* Stop emitting debugging information. */
1212
1213 static void
1214 cp_lexer_stop_debugging (cp_lexer* lexer)
1215 {
1216 lexer->debugging_p = false;
1217 cp_lexer_debug_stream = NULL;
1218 }
1219
1220 /* Create a new cp_token_cache, representing a range of tokens. */
1221
1222 static cp_token_cache *
1223 cp_token_cache_new (cp_token *first, cp_token *last)
1224 {
1225 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1226 cache->first = first;
1227 cache->last = last;
1228 return cache;
1229 }
1230
1231 /* Diagnose if #pragma omp declare simd isn't followed immediately
1232 by function declaration or definition. */
1233
1234 static inline void
1235 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1236 {
1237 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1238 {
1239 error ("%<#pragma omp declare simd%> not immediately followed by "
1240 "function declaration or definition");
1241 parser->omp_declare_simd = NULL;
1242 }
1243 }
1244
1245 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1246 and put that into "omp declare simd" attribute. */
1247
1248 static inline void
1249 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1250 {
1251 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1252 {
1253 if (fndecl == error_mark_node)
1254 {
1255 parser->omp_declare_simd = NULL;
1256 return;
1257 }
1258 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1259 {
1260 cp_ensure_no_omp_declare_simd (parser);
1261 return;
1262 }
1263 }
1264 }
1265 \f
1266 /* Decl-specifiers. */
1267
1268 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1269
1270 static void
1271 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1272 {
1273 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1274 }
1275
1276 /* Declarators. */
1277
1278 /* Nothing other than the parser should be creating declarators;
1279 declarators are a semi-syntactic representation of C++ entities.
1280 Other parts of the front end that need to create entities (like
1281 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1282
1283 static cp_declarator *make_call_declarator
1284 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1285 static cp_declarator *make_array_declarator
1286 (cp_declarator *, tree);
1287 static cp_declarator *make_pointer_declarator
1288 (cp_cv_quals, cp_declarator *, tree);
1289 static cp_declarator *make_reference_declarator
1290 (cp_cv_quals, cp_declarator *, bool, tree);
1291 static cp_parameter_declarator *make_parameter_declarator
1292 (cp_decl_specifier_seq *, cp_declarator *, tree);
1293 static cp_declarator *make_ptrmem_declarator
1294 (cp_cv_quals, tree, cp_declarator *, tree);
1295
1296 /* An erroneous declarator. */
1297 static cp_declarator *cp_error_declarator;
1298
1299 /* The obstack on which declarators and related data structures are
1300 allocated. */
1301 static struct obstack declarator_obstack;
1302
1303 /* Alloc BYTES from the declarator memory pool. */
1304
1305 static inline void *
1306 alloc_declarator (size_t bytes)
1307 {
1308 return obstack_alloc (&declarator_obstack, bytes);
1309 }
1310
1311 /* Allocate a declarator of the indicated KIND. Clear fields that are
1312 common to all declarators. */
1313
1314 static cp_declarator *
1315 make_declarator (cp_declarator_kind kind)
1316 {
1317 cp_declarator *declarator;
1318
1319 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1320 declarator->kind = kind;
1321 declarator->attributes = NULL_TREE;
1322 declarator->std_attributes = NULL_TREE;
1323 declarator->declarator = NULL;
1324 declarator->parameter_pack_p = false;
1325 declarator->id_loc = UNKNOWN_LOCATION;
1326
1327 return declarator;
1328 }
1329
1330 /* Make a declarator for a generalized identifier. If
1331 QUALIFYING_SCOPE is non-NULL, the identifier is
1332 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1333 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1334 is, if any. */
1335
1336 static cp_declarator *
1337 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1338 special_function_kind sfk)
1339 {
1340 cp_declarator *declarator;
1341
1342 /* It is valid to write:
1343
1344 class C { void f(); };
1345 typedef C D;
1346 void D::f();
1347
1348 The standard is not clear about whether `typedef const C D' is
1349 legal; as of 2002-09-15 the committee is considering that
1350 question. EDG 3.0 allows that syntax. Therefore, we do as
1351 well. */
1352 if (qualifying_scope && TYPE_P (qualifying_scope))
1353 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1354
1355 gcc_assert (identifier_p (unqualified_name)
1356 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1357 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1358
1359 declarator = make_declarator (cdk_id);
1360 declarator->u.id.qualifying_scope = qualifying_scope;
1361 declarator->u.id.unqualified_name = unqualified_name;
1362 declarator->u.id.sfk = sfk;
1363
1364 return declarator;
1365 }
1366
1367 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1368 of modifiers such as const or volatile to apply to the pointer
1369 type, represented as identifiers. ATTRIBUTES represent the attributes that
1370 appertain to the pointer or reference. */
1371
1372 cp_declarator *
1373 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1374 tree attributes)
1375 {
1376 cp_declarator *declarator;
1377
1378 declarator = make_declarator (cdk_pointer);
1379 declarator->declarator = target;
1380 declarator->u.pointer.qualifiers = cv_qualifiers;
1381 declarator->u.pointer.class_type = NULL_TREE;
1382 if (target)
1383 {
1384 declarator->id_loc = target->id_loc;
1385 declarator->parameter_pack_p = target->parameter_pack_p;
1386 target->parameter_pack_p = false;
1387 }
1388 else
1389 declarator->parameter_pack_p = false;
1390
1391 declarator->std_attributes = attributes;
1392
1393 return declarator;
1394 }
1395
1396 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1397 represent the attributes that appertain to the pointer or
1398 reference. */
1399
1400 cp_declarator *
1401 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1402 bool rvalue_ref, tree attributes)
1403 {
1404 cp_declarator *declarator;
1405
1406 declarator = make_declarator (cdk_reference);
1407 declarator->declarator = target;
1408 declarator->u.reference.qualifiers = cv_qualifiers;
1409 declarator->u.reference.rvalue_ref = rvalue_ref;
1410 if (target)
1411 {
1412 declarator->id_loc = target->id_loc;
1413 declarator->parameter_pack_p = target->parameter_pack_p;
1414 target->parameter_pack_p = false;
1415 }
1416 else
1417 declarator->parameter_pack_p = false;
1418
1419 declarator->std_attributes = attributes;
1420
1421 return declarator;
1422 }
1423
1424 /* Like make_pointer_declarator -- but for a pointer to a non-static
1425 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1426 appertain to the pointer or reference. */
1427
1428 cp_declarator *
1429 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1430 cp_declarator *pointee,
1431 tree attributes)
1432 {
1433 cp_declarator *declarator;
1434
1435 declarator = make_declarator (cdk_ptrmem);
1436 declarator->declarator = pointee;
1437 declarator->u.pointer.qualifiers = cv_qualifiers;
1438 declarator->u.pointer.class_type = class_type;
1439
1440 if (pointee)
1441 {
1442 declarator->parameter_pack_p = pointee->parameter_pack_p;
1443 pointee->parameter_pack_p = false;
1444 }
1445 else
1446 declarator->parameter_pack_p = false;
1447
1448 declarator->std_attributes = attributes;
1449
1450 return declarator;
1451 }
1452
1453 /* Make a declarator for the function given by TARGET, with the
1454 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1455 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1456 indicates what exceptions can be thrown. */
1457
1458 cp_declarator *
1459 make_call_declarator (cp_declarator *target,
1460 tree parms,
1461 cp_cv_quals cv_qualifiers,
1462 cp_virt_specifiers virt_specifiers,
1463 cp_ref_qualifier ref_qualifier,
1464 tree exception_specification,
1465 tree late_return_type)
1466 {
1467 cp_declarator *declarator;
1468
1469 declarator = make_declarator (cdk_function);
1470 declarator->declarator = target;
1471 declarator->u.function.parameters = parms;
1472 declarator->u.function.qualifiers = cv_qualifiers;
1473 declarator->u.function.virt_specifiers = virt_specifiers;
1474 declarator->u.function.ref_qualifier = ref_qualifier;
1475 declarator->u.function.exception_specification = exception_specification;
1476 declarator->u.function.late_return_type = late_return_type;
1477 if (target)
1478 {
1479 declarator->id_loc = target->id_loc;
1480 declarator->parameter_pack_p = target->parameter_pack_p;
1481 target->parameter_pack_p = false;
1482 }
1483 else
1484 declarator->parameter_pack_p = false;
1485
1486 return declarator;
1487 }
1488
1489 /* Make a declarator for an array of BOUNDS elements, each of which is
1490 defined by ELEMENT. */
1491
1492 cp_declarator *
1493 make_array_declarator (cp_declarator *element, tree bounds)
1494 {
1495 cp_declarator *declarator;
1496
1497 declarator = make_declarator (cdk_array);
1498 declarator->declarator = element;
1499 declarator->u.array.bounds = bounds;
1500 if (element)
1501 {
1502 declarator->id_loc = element->id_loc;
1503 declarator->parameter_pack_p = element->parameter_pack_p;
1504 element->parameter_pack_p = false;
1505 }
1506 else
1507 declarator->parameter_pack_p = false;
1508
1509 return declarator;
1510 }
1511
1512 /* Determine whether the declarator we've seen so far can be a
1513 parameter pack, when followed by an ellipsis. */
1514 static bool
1515 declarator_can_be_parameter_pack (cp_declarator *declarator)
1516 {
1517 /* Search for a declarator name, or any other declarator that goes
1518 after the point where the ellipsis could appear in a parameter
1519 pack. If we find any of these, then this declarator can not be
1520 made into a parameter pack. */
1521 bool found = false;
1522 while (declarator && !found)
1523 {
1524 switch ((int)declarator->kind)
1525 {
1526 case cdk_id:
1527 case cdk_array:
1528 found = true;
1529 break;
1530
1531 case cdk_error:
1532 return true;
1533
1534 default:
1535 declarator = declarator->declarator;
1536 break;
1537 }
1538 }
1539
1540 return !found;
1541 }
1542
1543 cp_parameter_declarator *no_parameters;
1544
1545 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1546 DECLARATOR and DEFAULT_ARGUMENT. */
1547
1548 cp_parameter_declarator *
1549 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1550 cp_declarator *declarator,
1551 tree default_argument)
1552 {
1553 cp_parameter_declarator *parameter;
1554
1555 parameter = ((cp_parameter_declarator *)
1556 alloc_declarator (sizeof (cp_parameter_declarator)));
1557 parameter->next = NULL;
1558 if (decl_specifiers)
1559 parameter->decl_specifiers = *decl_specifiers;
1560 else
1561 clear_decl_specs (&parameter->decl_specifiers);
1562 parameter->declarator = declarator;
1563 parameter->default_argument = default_argument;
1564 parameter->ellipsis_p = false;
1565
1566 return parameter;
1567 }
1568
1569 /* Returns true iff DECLARATOR is a declaration for a function. */
1570
1571 static bool
1572 function_declarator_p (const cp_declarator *declarator)
1573 {
1574 while (declarator)
1575 {
1576 if (declarator->kind == cdk_function
1577 && declarator->declarator->kind == cdk_id)
1578 return true;
1579 if (declarator->kind == cdk_id
1580 || declarator->kind == cdk_error)
1581 return false;
1582 declarator = declarator->declarator;
1583 }
1584 return false;
1585 }
1586
1587 /* The parser. */
1588
1589 /* Overview
1590 --------
1591
1592 A cp_parser parses the token stream as specified by the C++
1593 grammar. Its job is purely parsing, not semantic analysis. For
1594 example, the parser breaks the token stream into declarators,
1595 expressions, statements, and other similar syntactic constructs.
1596 It does not check that the types of the expressions on either side
1597 of an assignment-statement are compatible, or that a function is
1598 not declared with a parameter of type `void'.
1599
1600 The parser invokes routines elsewhere in the compiler to perform
1601 semantic analysis and to build up the abstract syntax tree for the
1602 code processed.
1603
1604 The parser (and the template instantiation code, which is, in a
1605 way, a close relative of parsing) are the only parts of the
1606 compiler that should be calling push_scope and pop_scope, or
1607 related functions. The parser (and template instantiation code)
1608 keeps track of what scope is presently active; everything else
1609 should simply honor that. (The code that generates static
1610 initializers may also need to set the scope, in order to check
1611 access control correctly when emitting the initializers.)
1612
1613 Methodology
1614 -----------
1615
1616 The parser is of the standard recursive-descent variety. Upcoming
1617 tokens in the token stream are examined in order to determine which
1618 production to use when parsing a non-terminal. Some C++ constructs
1619 require arbitrary look ahead to disambiguate. For example, it is
1620 impossible, in the general case, to tell whether a statement is an
1621 expression or declaration without scanning the entire statement.
1622 Therefore, the parser is capable of "parsing tentatively." When the
1623 parser is not sure what construct comes next, it enters this mode.
1624 Then, while we attempt to parse the construct, the parser queues up
1625 error messages, rather than issuing them immediately, and saves the
1626 tokens it consumes. If the construct is parsed successfully, the
1627 parser "commits", i.e., it issues any queued error messages and
1628 the tokens that were being preserved are permanently discarded.
1629 If, however, the construct is not parsed successfully, the parser
1630 rolls back its state completely so that it can resume parsing using
1631 a different alternative.
1632
1633 Future Improvements
1634 -------------------
1635
1636 The performance of the parser could probably be improved substantially.
1637 We could often eliminate the need to parse tentatively by looking ahead
1638 a little bit. In some places, this approach might not entirely eliminate
1639 the need to parse tentatively, but it might still speed up the average
1640 case. */
1641
1642 /* Flags that are passed to some parsing functions. These values can
1643 be bitwise-ored together. */
1644
1645 enum
1646 {
1647 /* No flags. */
1648 CP_PARSER_FLAGS_NONE = 0x0,
1649 /* The construct is optional. If it is not present, then no error
1650 should be issued. */
1651 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1652 /* When parsing a type-specifier, treat user-defined type-names
1653 as non-type identifiers. */
1654 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1655 /* When parsing a type-specifier, do not try to parse a class-specifier
1656 or enum-specifier. */
1657 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1658 /* When parsing a decl-specifier-seq, only allow type-specifier or
1659 constexpr. */
1660 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1661 };
1662
1663 /* This type is used for parameters and variables which hold
1664 combinations of the above flags. */
1665 typedef int cp_parser_flags;
1666
1667 /* The different kinds of declarators we want to parse. */
1668
1669 typedef enum cp_parser_declarator_kind
1670 {
1671 /* We want an abstract declarator. */
1672 CP_PARSER_DECLARATOR_ABSTRACT,
1673 /* We want a named declarator. */
1674 CP_PARSER_DECLARATOR_NAMED,
1675 /* We don't mind, but the name must be an unqualified-id. */
1676 CP_PARSER_DECLARATOR_EITHER
1677 } cp_parser_declarator_kind;
1678
1679 /* The precedence values used to parse binary expressions. The minimum value
1680 of PREC must be 1, because zero is reserved to quickly discriminate
1681 binary operators from other tokens. */
1682
1683 enum cp_parser_prec
1684 {
1685 PREC_NOT_OPERATOR,
1686 PREC_LOGICAL_OR_EXPRESSION,
1687 PREC_LOGICAL_AND_EXPRESSION,
1688 PREC_INCLUSIVE_OR_EXPRESSION,
1689 PREC_EXCLUSIVE_OR_EXPRESSION,
1690 PREC_AND_EXPRESSION,
1691 PREC_EQUALITY_EXPRESSION,
1692 PREC_RELATIONAL_EXPRESSION,
1693 PREC_SHIFT_EXPRESSION,
1694 PREC_ADDITIVE_EXPRESSION,
1695 PREC_MULTIPLICATIVE_EXPRESSION,
1696 PREC_PM_EXPRESSION,
1697 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1698 };
1699
1700 /* A mapping from a token type to a corresponding tree node type, with a
1701 precedence value. */
1702
1703 typedef struct cp_parser_binary_operations_map_node
1704 {
1705 /* The token type. */
1706 enum cpp_ttype token_type;
1707 /* The corresponding tree code. */
1708 enum tree_code tree_type;
1709 /* The precedence of this operator. */
1710 enum cp_parser_prec prec;
1711 } cp_parser_binary_operations_map_node;
1712
1713 typedef struct cp_parser_expression_stack_entry
1714 {
1715 /* Left hand side of the binary operation we are currently
1716 parsing. */
1717 tree lhs;
1718 /* Original tree code for left hand side, if it was a binary
1719 expression itself (used for -Wparentheses). */
1720 enum tree_code lhs_type;
1721 /* Tree code for the binary operation we are parsing. */
1722 enum tree_code tree_type;
1723 /* Precedence of the binary operation we are parsing. */
1724 enum cp_parser_prec prec;
1725 /* Location of the binary operation we are parsing. */
1726 location_t loc;
1727 } cp_parser_expression_stack_entry;
1728
1729 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1730 entries because precedence levels on the stack are monotonically
1731 increasing. */
1732 typedef struct cp_parser_expression_stack_entry
1733 cp_parser_expression_stack[NUM_PREC_VALUES];
1734
1735 /* Prototypes. */
1736
1737 /* Constructors and destructors. */
1738
1739 static cp_parser_context *cp_parser_context_new
1740 (cp_parser_context *);
1741
1742 /* Class variables. */
1743
1744 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1745
1746 /* The operator-precedence table used by cp_parser_binary_expression.
1747 Transformed into an associative array (binops_by_token) by
1748 cp_parser_new. */
1749
1750 static const cp_parser_binary_operations_map_node binops[] = {
1751 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1752 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1753
1754 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1755 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1756 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1757
1758 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1759 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1760
1761 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1762 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1763
1764 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1765 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1766 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1767 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1768
1769 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1770 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1771
1772 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1773
1774 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1775
1776 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1777
1778 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1779
1780 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1781 };
1782
1783 /* The same as binops, but initialized by cp_parser_new so that
1784 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1785 for speed. */
1786 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1787
1788 /* Constructors and destructors. */
1789
1790 /* Construct a new context. The context below this one on the stack
1791 is given by NEXT. */
1792
1793 static cp_parser_context *
1794 cp_parser_context_new (cp_parser_context* next)
1795 {
1796 cp_parser_context *context;
1797
1798 /* Allocate the storage. */
1799 if (cp_parser_context_free_list != NULL)
1800 {
1801 /* Pull the first entry from the free list. */
1802 context = cp_parser_context_free_list;
1803 cp_parser_context_free_list = context->next;
1804 memset (context, 0, sizeof (*context));
1805 }
1806 else
1807 context = ggc_alloc_cleared_cp_parser_context ();
1808
1809 /* No errors have occurred yet in this context. */
1810 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1811 /* If this is not the bottommost context, copy information that we
1812 need from the previous context. */
1813 if (next)
1814 {
1815 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1816 expression, then we are parsing one in this context, too. */
1817 context->object_type = next->object_type;
1818 /* Thread the stack. */
1819 context->next = next;
1820 }
1821
1822 return context;
1823 }
1824
1825 /* Managing the unparsed function queues. */
1826
1827 #define unparsed_funs_with_default_args \
1828 parser->unparsed_queues->last ().funs_with_default_args
1829 #define unparsed_funs_with_definitions \
1830 parser->unparsed_queues->last ().funs_with_definitions
1831 #define unparsed_nsdmis \
1832 parser->unparsed_queues->last ().nsdmis
1833
1834 static void
1835 push_unparsed_function_queues (cp_parser *parser)
1836 {
1837 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1838 vec_safe_push (parser->unparsed_queues, e);
1839 }
1840
1841 static void
1842 pop_unparsed_function_queues (cp_parser *parser)
1843 {
1844 release_tree_vector (unparsed_funs_with_definitions);
1845 parser->unparsed_queues->pop ();
1846 }
1847
1848 /* Prototypes. */
1849
1850 /* Constructors and destructors. */
1851
1852 static cp_parser *cp_parser_new
1853 (void);
1854
1855 /* Routines to parse various constructs.
1856
1857 Those that return `tree' will return the error_mark_node (rather
1858 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1859 Sometimes, they will return an ordinary node if error-recovery was
1860 attempted, even though a parse error occurred. So, to check
1861 whether or not a parse error occurred, you should always use
1862 cp_parser_error_occurred. If the construct is optional (indicated
1863 either by an `_opt' in the name of the function that does the
1864 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1865 the construct is not present. */
1866
1867 /* Lexical conventions [gram.lex] */
1868
1869 static tree cp_parser_identifier
1870 (cp_parser *);
1871 static tree cp_parser_string_literal
1872 (cp_parser *, bool, bool);
1873 static tree cp_parser_userdef_char_literal
1874 (cp_parser *);
1875 static tree cp_parser_userdef_string_literal
1876 (cp_token *);
1877 static tree cp_parser_userdef_numeric_literal
1878 (cp_parser *);
1879
1880 /* Basic concepts [gram.basic] */
1881
1882 static bool cp_parser_translation_unit
1883 (cp_parser *);
1884
1885 /* Expressions [gram.expr] */
1886
1887 static tree cp_parser_primary_expression
1888 (cp_parser *, bool, bool, bool, cp_id_kind *);
1889 static tree cp_parser_id_expression
1890 (cp_parser *, bool, bool, bool *, bool, bool);
1891 static tree cp_parser_unqualified_id
1892 (cp_parser *, bool, bool, bool, bool);
1893 static tree cp_parser_nested_name_specifier_opt
1894 (cp_parser *, bool, bool, bool, bool);
1895 static tree cp_parser_nested_name_specifier
1896 (cp_parser *, bool, bool, bool, bool);
1897 static tree cp_parser_qualifying_entity
1898 (cp_parser *, bool, bool, bool, bool, bool);
1899 static tree cp_parser_postfix_expression
1900 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1901 static tree cp_parser_postfix_open_square_expression
1902 (cp_parser *, tree, bool, bool);
1903 static tree cp_parser_postfix_dot_deref_expression
1904 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1905 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1906 (cp_parser *, int, bool, bool, bool *);
1907 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1908 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1909 static void cp_parser_pseudo_destructor_name
1910 (cp_parser *, tree, tree *, tree *);
1911 static tree cp_parser_unary_expression
1912 (cp_parser *, bool, bool, cp_id_kind *);
1913 static enum tree_code cp_parser_unary_operator
1914 (cp_token *);
1915 static tree cp_parser_new_expression
1916 (cp_parser *);
1917 static vec<tree, va_gc> *cp_parser_new_placement
1918 (cp_parser *);
1919 static tree cp_parser_new_type_id
1920 (cp_parser *, tree *);
1921 static cp_declarator *cp_parser_new_declarator_opt
1922 (cp_parser *);
1923 static cp_declarator *cp_parser_direct_new_declarator
1924 (cp_parser *);
1925 static vec<tree, va_gc> *cp_parser_new_initializer
1926 (cp_parser *);
1927 static tree cp_parser_delete_expression
1928 (cp_parser *);
1929 static tree cp_parser_cast_expression
1930 (cp_parser *, bool, bool, bool, cp_id_kind *);
1931 static tree cp_parser_binary_expression
1932 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1933 static tree cp_parser_question_colon_clause
1934 (cp_parser *, tree);
1935 static tree cp_parser_assignment_expression
1936 (cp_parser *, bool, cp_id_kind *);
1937 static enum tree_code cp_parser_assignment_operator_opt
1938 (cp_parser *);
1939 static tree cp_parser_expression
1940 (cp_parser *, bool, cp_id_kind *);
1941 static tree cp_parser_expression
1942 (cp_parser *, bool, bool, cp_id_kind *);
1943 static tree cp_parser_constant_expression
1944 (cp_parser *, bool, bool *);
1945 static tree cp_parser_builtin_offsetof
1946 (cp_parser *);
1947 static tree cp_parser_lambda_expression
1948 (cp_parser *);
1949 static void cp_parser_lambda_introducer
1950 (cp_parser *, tree);
1951 static bool cp_parser_lambda_declarator_opt
1952 (cp_parser *, tree);
1953 static void cp_parser_lambda_body
1954 (cp_parser *, tree);
1955
1956 /* Statements [gram.stmt.stmt] */
1957
1958 static void cp_parser_statement
1959 (cp_parser *, tree, bool, bool *);
1960 static void cp_parser_label_for_labeled_statement
1961 (cp_parser *, tree);
1962 static tree cp_parser_expression_statement
1963 (cp_parser *, tree);
1964 static tree cp_parser_compound_statement
1965 (cp_parser *, tree, bool, bool);
1966 static void cp_parser_statement_seq_opt
1967 (cp_parser *, tree);
1968 static tree cp_parser_selection_statement
1969 (cp_parser *, bool *);
1970 static tree cp_parser_condition
1971 (cp_parser *);
1972 static tree cp_parser_iteration_statement
1973 (cp_parser *, bool);
1974 static bool cp_parser_for_init_statement
1975 (cp_parser *, tree *decl);
1976 static tree cp_parser_for
1977 (cp_parser *, bool);
1978 static tree cp_parser_c_for
1979 (cp_parser *, tree, tree, bool);
1980 static tree cp_parser_range_for
1981 (cp_parser *, tree, tree, tree, bool);
1982 static void do_range_for_auto_deduction
1983 (tree, tree);
1984 static tree cp_parser_perform_range_for_lookup
1985 (tree, tree *, tree *);
1986 static tree cp_parser_range_for_member_function
1987 (tree, tree);
1988 static tree cp_parser_jump_statement
1989 (cp_parser *);
1990 static void cp_parser_declaration_statement
1991 (cp_parser *);
1992
1993 static tree cp_parser_implicitly_scoped_statement
1994 (cp_parser *, bool *);
1995 static void cp_parser_already_scoped_statement
1996 (cp_parser *);
1997
1998 /* Declarations [gram.dcl.dcl] */
1999
2000 static void cp_parser_declaration_seq_opt
2001 (cp_parser *);
2002 static void cp_parser_declaration
2003 (cp_parser *);
2004 static void cp_parser_block_declaration
2005 (cp_parser *, bool);
2006 static void cp_parser_simple_declaration
2007 (cp_parser *, bool, tree *);
2008 static void cp_parser_decl_specifier_seq
2009 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2010 static tree cp_parser_storage_class_specifier_opt
2011 (cp_parser *);
2012 static tree cp_parser_function_specifier_opt
2013 (cp_parser *, cp_decl_specifier_seq *);
2014 static tree cp_parser_type_specifier
2015 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2016 int *, bool *);
2017 static tree cp_parser_simple_type_specifier
2018 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2019 static tree cp_parser_type_name
2020 (cp_parser *);
2021 static tree cp_parser_nonclass_name
2022 (cp_parser* parser);
2023 static tree cp_parser_elaborated_type_specifier
2024 (cp_parser *, bool, bool);
2025 static tree cp_parser_enum_specifier
2026 (cp_parser *);
2027 static void cp_parser_enumerator_list
2028 (cp_parser *, tree);
2029 static void cp_parser_enumerator_definition
2030 (cp_parser *, tree);
2031 static tree cp_parser_namespace_name
2032 (cp_parser *);
2033 static void cp_parser_namespace_definition
2034 (cp_parser *);
2035 static void cp_parser_namespace_body
2036 (cp_parser *);
2037 static tree cp_parser_qualified_namespace_specifier
2038 (cp_parser *);
2039 static void cp_parser_namespace_alias_definition
2040 (cp_parser *);
2041 static bool cp_parser_using_declaration
2042 (cp_parser *, bool);
2043 static void cp_parser_using_directive
2044 (cp_parser *);
2045 static tree cp_parser_alias_declaration
2046 (cp_parser *);
2047 static void cp_parser_asm_definition
2048 (cp_parser *);
2049 static void cp_parser_linkage_specification
2050 (cp_parser *);
2051 static void cp_parser_static_assert
2052 (cp_parser *, bool);
2053 static tree cp_parser_decltype
2054 (cp_parser *);
2055
2056 /* Declarators [gram.dcl.decl] */
2057
2058 static tree cp_parser_init_declarator
2059 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2060 static cp_declarator *cp_parser_declarator
2061 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2062 static cp_declarator *cp_parser_direct_declarator
2063 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2064 static enum tree_code cp_parser_ptr_operator
2065 (cp_parser *, tree *, cp_cv_quals *, tree *);
2066 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2067 (cp_parser *);
2068 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2069 (cp_parser *);
2070 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2071 (cp_parser *);
2072 static tree cp_parser_late_return_type_opt
2073 (cp_parser *, cp_declarator *, cp_cv_quals);
2074 static tree cp_parser_declarator_id
2075 (cp_parser *, bool);
2076 static tree cp_parser_type_id
2077 (cp_parser *);
2078 static tree cp_parser_template_type_arg
2079 (cp_parser *);
2080 static tree cp_parser_trailing_type_id (cp_parser *);
2081 static tree cp_parser_type_id_1
2082 (cp_parser *, bool, bool);
2083 static void cp_parser_type_specifier_seq
2084 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2085 static tree cp_parser_parameter_declaration_clause
2086 (cp_parser *);
2087 static tree cp_parser_parameter_declaration_list
2088 (cp_parser *, bool *);
2089 static cp_parameter_declarator *cp_parser_parameter_declaration
2090 (cp_parser *, bool, bool *);
2091 static tree cp_parser_default_argument
2092 (cp_parser *, bool);
2093 static void cp_parser_function_body
2094 (cp_parser *, bool);
2095 static tree cp_parser_initializer
2096 (cp_parser *, bool *, bool *);
2097 static tree cp_parser_initializer_clause
2098 (cp_parser *, bool *);
2099 static tree cp_parser_braced_list
2100 (cp_parser*, bool*);
2101 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2102 (cp_parser *, bool *);
2103
2104 static bool cp_parser_ctor_initializer_opt_and_function_body
2105 (cp_parser *, bool);
2106
2107 static tree cp_parser_late_parsing_omp_declare_simd
2108 (cp_parser *, tree);
2109
2110 static tree add_implicit_template_parms
2111 (cp_parser *, size_t, tree);
2112 static tree finish_fully_implicit_template
2113 (cp_parser *, tree);
2114
2115 /* Classes [gram.class] */
2116
2117 static tree cp_parser_class_name
2118 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2119 static tree cp_parser_class_specifier
2120 (cp_parser *);
2121 static tree cp_parser_class_head
2122 (cp_parser *, bool *);
2123 static enum tag_types cp_parser_class_key
2124 (cp_parser *);
2125 static void cp_parser_member_specification_opt
2126 (cp_parser *);
2127 static void cp_parser_member_declaration
2128 (cp_parser *);
2129 static tree cp_parser_pure_specifier
2130 (cp_parser *);
2131 static tree cp_parser_constant_initializer
2132 (cp_parser *);
2133
2134 /* Derived classes [gram.class.derived] */
2135
2136 static tree cp_parser_base_clause
2137 (cp_parser *);
2138 static tree cp_parser_base_specifier
2139 (cp_parser *);
2140
2141 /* Special member functions [gram.special] */
2142
2143 static tree cp_parser_conversion_function_id
2144 (cp_parser *);
2145 static tree cp_parser_conversion_type_id
2146 (cp_parser *);
2147 static cp_declarator *cp_parser_conversion_declarator_opt
2148 (cp_parser *);
2149 static bool cp_parser_ctor_initializer_opt
2150 (cp_parser *);
2151 static void cp_parser_mem_initializer_list
2152 (cp_parser *);
2153 static tree cp_parser_mem_initializer
2154 (cp_parser *);
2155 static tree cp_parser_mem_initializer_id
2156 (cp_parser *);
2157
2158 /* Overloading [gram.over] */
2159
2160 static tree cp_parser_operator_function_id
2161 (cp_parser *);
2162 static tree cp_parser_operator
2163 (cp_parser *);
2164
2165 /* Templates [gram.temp] */
2166
2167 static void cp_parser_template_declaration
2168 (cp_parser *, bool);
2169 static tree cp_parser_template_parameter_list
2170 (cp_parser *);
2171 static tree cp_parser_template_parameter
2172 (cp_parser *, bool *, bool *);
2173 static tree cp_parser_type_parameter
2174 (cp_parser *, bool *);
2175 static tree cp_parser_template_id
2176 (cp_parser *, bool, bool, enum tag_types, bool);
2177 static tree cp_parser_template_name
2178 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2179 static tree cp_parser_template_argument_list
2180 (cp_parser *);
2181 static tree cp_parser_template_argument
2182 (cp_parser *);
2183 static void cp_parser_explicit_instantiation
2184 (cp_parser *);
2185 static void cp_parser_explicit_specialization
2186 (cp_parser *);
2187
2188 /* Exception handling [gram.exception] */
2189
2190 static tree cp_parser_try_block
2191 (cp_parser *);
2192 static bool cp_parser_function_try_block
2193 (cp_parser *);
2194 static void cp_parser_handler_seq
2195 (cp_parser *);
2196 static void cp_parser_handler
2197 (cp_parser *);
2198 static tree cp_parser_exception_declaration
2199 (cp_parser *);
2200 static tree cp_parser_throw_expression
2201 (cp_parser *);
2202 static tree cp_parser_exception_specification_opt
2203 (cp_parser *);
2204 static tree cp_parser_type_id_list
2205 (cp_parser *);
2206
2207 /* GNU Extensions */
2208
2209 static tree cp_parser_asm_specification_opt
2210 (cp_parser *);
2211 static tree cp_parser_asm_operand_list
2212 (cp_parser *);
2213 static tree cp_parser_asm_clobber_list
2214 (cp_parser *);
2215 static tree cp_parser_asm_label_list
2216 (cp_parser *);
2217 static bool cp_next_tokens_can_be_attribute_p
2218 (cp_parser *);
2219 static bool cp_next_tokens_can_be_gnu_attribute_p
2220 (cp_parser *);
2221 static bool cp_next_tokens_can_be_std_attribute_p
2222 (cp_parser *);
2223 static bool cp_nth_tokens_can_be_std_attribute_p
2224 (cp_parser *, size_t);
2225 static bool cp_nth_tokens_can_be_gnu_attribute_p
2226 (cp_parser *, size_t);
2227 static bool cp_nth_tokens_can_be_attribute_p
2228 (cp_parser *, size_t);
2229 static tree cp_parser_attributes_opt
2230 (cp_parser *);
2231 static tree cp_parser_gnu_attributes_opt
2232 (cp_parser *);
2233 static tree cp_parser_gnu_attribute_list
2234 (cp_parser *);
2235 static tree cp_parser_std_attribute
2236 (cp_parser *);
2237 static tree cp_parser_std_attribute_spec
2238 (cp_parser *);
2239 static tree cp_parser_std_attribute_spec_seq
2240 (cp_parser *);
2241 static bool cp_parser_extension_opt
2242 (cp_parser *, int *);
2243 static void cp_parser_label_declaration
2244 (cp_parser *);
2245
2246 /* Transactional Memory Extensions */
2247
2248 static tree cp_parser_transaction
2249 (cp_parser *, enum rid);
2250 static tree cp_parser_transaction_expression
2251 (cp_parser *, enum rid);
2252 static bool cp_parser_function_transaction
2253 (cp_parser *, enum rid);
2254 static tree cp_parser_transaction_cancel
2255 (cp_parser *);
2256
2257 enum pragma_context {
2258 pragma_external,
2259 pragma_member,
2260 pragma_objc_icode,
2261 pragma_stmt,
2262 pragma_compound
2263 };
2264 static bool cp_parser_pragma
2265 (cp_parser *, enum pragma_context);
2266
2267 /* Objective-C++ Productions */
2268
2269 static tree cp_parser_objc_message_receiver
2270 (cp_parser *);
2271 static tree cp_parser_objc_message_args
2272 (cp_parser *);
2273 static tree cp_parser_objc_message_expression
2274 (cp_parser *);
2275 static tree cp_parser_objc_encode_expression
2276 (cp_parser *);
2277 static tree cp_parser_objc_defs_expression
2278 (cp_parser *);
2279 static tree cp_parser_objc_protocol_expression
2280 (cp_parser *);
2281 static tree cp_parser_objc_selector_expression
2282 (cp_parser *);
2283 static tree cp_parser_objc_expression
2284 (cp_parser *);
2285 static bool cp_parser_objc_selector_p
2286 (enum cpp_ttype);
2287 static tree cp_parser_objc_selector
2288 (cp_parser *);
2289 static tree cp_parser_objc_protocol_refs_opt
2290 (cp_parser *);
2291 static void cp_parser_objc_declaration
2292 (cp_parser *, tree);
2293 static tree cp_parser_objc_statement
2294 (cp_parser *);
2295 static bool cp_parser_objc_valid_prefix_attributes
2296 (cp_parser *, tree *);
2297 static void cp_parser_objc_at_property_declaration
2298 (cp_parser *) ;
2299 static void cp_parser_objc_at_synthesize_declaration
2300 (cp_parser *) ;
2301 static void cp_parser_objc_at_dynamic_declaration
2302 (cp_parser *) ;
2303 static tree cp_parser_objc_struct_declaration
2304 (cp_parser *) ;
2305
2306 /* Utility Routines */
2307
2308 static tree cp_parser_lookup_name
2309 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2310 static tree cp_parser_lookup_name_simple
2311 (cp_parser *, tree, location_t);
2312 static tree cp_parser_maybe_treat_template_as_class
2313 (tree, bool);
2314 static bool cp_parser_check_declarator_template_parameters
2315 (cp_parser *, cp_declarator *, location_t);
2316 static bool cp_parser_check_template_parameters
2317 (cp_parser *, unsigned, location_t, cp_declarator *);
2318 static tree cp_parser_simple_cast_expression
2319 (cp_parser *);
2320 static tree cp_parser_global_scope_opt
2321 (cp_parser *, bool);
2322 static bool cp_parser_constructor_declarator_p
2323 (cp_parser *, bool);
2324 static tree cp_parser_function_definition_from_specifiers_and_declarator
2325 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2326 static tree cp_parser_function_definition_after_declarator
2327 (cp_parser *, bool);
2328 static void cp_parser_template_declaration_after_export
2329 (cp_parser *, bool);
2330 static void cp_parser_perform_template_parameter_access_checks
2331 (vec<deferred_access_check, va_gc> *);
2332 static tree cp_parser_single_declaration
2333 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2334 static tree cp_parser_functional_cast
2335 (cp_parser *, tree);
2336 static tree cp_parser_save_member_function_body
2337 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2338 static tree cp_parser_save_nsdmi
2339 (cp_parser *);
2340 static tree cp_parser_enclosed_template_argument_list
2341 (cp_parser *);
2342 static void cp_parser_save_default_args
2343 (cp_parser *, tree);
2344 static void cp_parser_late_parsing_for_member
2345 (cp_parser *, tree);
2346 static tree cp_parser_late_parse_one_default_arg
2347 (cp_parser *, tree, tree, tree);
2348 static void cp_parser_late_parsing_nsdmi
2349 (cp_parser *, tree);
2350 static void cp_parser_late_parsing_default_args
2351 (cp_parser *, tree);
2352 static tree cp_parser_sizeof_operand
2353 (cp_parser *, enum rid);
2354 static tree cp_parser_trait_expr
2355 (cp_parser *, enum rid);
2356 static bool cp_parser_declares_only_class_p
2357 (cp_parser *);
2358 static void cp_parser_set_storage_class
2359 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2360 static void cp_parser_set_decl_spec_type
2361 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2362 static void set_and_check_decl_spec_loc
2363 (cp_decl_specifier_seq *decl_specs,
2364 cp_decl_spec ds, cp_token *);
2365 static bool cp_parser_friend_p
2366 (const cp_decl_specifier_seq *);
2367 static void cp_parser_required_error
2368 (cp_parser *, required_token, bool);
2369 static cp_token *cp_parser_require
2370 (cp_parser *, enum cpp_ttype, required_token);
2371 static cp_token *cp_parser_require_keyword
2372 (cp_parser *, enum rid, required_token);
2373 static bool cp_parser_token_starts_function_definition_p
2374 (cp_token *);
2375 static bool cp_parser_next_token_starts_class_definition_p
2376 (cp_parser *);
2377 static bool cp_parser_next_token_ends_template_argument_p
2378 (cp_parser *);
2379 static bool cp_parser_nth_token_starts_template_argument_list_p
2380 (cp_parser *, size_t);
2381 static enum tag_types cp_parser_token_is_class_key
2382 (cp_token *);
2383 static void cp_parser_check_class_key
2384 (enum tag_types, tree type);
2385 static void cp_parser_check_access_in_redeclaration
2386 (tree type, location_t location);
2387 static bool cp_parser_optional_template_keyword
2388 (cp_parser *);
2389 static void cp_parser_pre_parsed_nested_name_specifier
2390 (cp_parser *);
2391 static bool cp_parser_cache_group
2392 (cp_parser *, enum cpp_ttype, unsigned);
2393 static tree cp_parser_cache_defarg
2394 (cp_parser *parser, bool nsdmi);
2395 static void cp_parser_parse_tentatively
2396 (cp_parser *);
2397 static void cp_parser_commit_to_tentative_parse
2398 (cp_parser *);
2399 static void cp_parser_commit_to_topmost_tentative_parse
2400 (cp_parser *);
2401 static void cp_parser_abort_tentative_parse
2402 (cp_parser *);
2403 static bool cp_parser_parse_definitely
2404 (cp_parser *);
2405 static inline bool cp_parser_parsing_tentatively
2406 (cp_parser *);
2407 static bool cp_parser_uncommitted_to_tentative_parse_p
2408 (cp_parser *);
2409 static void cp_parser_error
2410 (cp_parser *, const char *);
2411 static void cp_parser_name_lookup_error
2412 (cp_parser *, tree, tree, name_lookup_error, location_t);
2413 static bool cp_parser_simulate_error
2414 (cp_parser *);
2415 static bool cp_parser_check_type_definition
2416 (cp_parser *);
2417 static void cp_parser_check_for_definition_in_return_type
2418 (cp_declarator *, tree, location_t type_location);
2419 static void cp_parser_check_for_invalid_template_id
2420 (cp_parser *, tree, enum tag_types, location_t location);
2421 static bool cp_parser_non_integral_constant_expression
2422 (cp_parser *, non_integral_constant);
2423 static void cp_parser_diagnose_invalid_type_name
2424 (cp_parser *, tree, tree, location_t);
2425 static bool cp_parser_parse_and_diagnose_invalid_type_name
2426 (cp_parser *);
2427 static int cp_parser_skip_to_closing_parenthesis
2428 (cp_parser *, bool, bool, bool);
2429 static void cp_parser_skip_to_end_of_statement
2430 (cp_parser *);
2431 static void cp_parser_consume_semicolon_at_end_of_statement
2432 (cp_parser *);
2433 static void cp_parser_skip_to_end_of_block_or_statement
2434 (cp_parser *);
2435 static bool cp_parser_skip_to_closing_brace
2436 (cp_parser *);
2437 static void cp_parser_skip_to_end_of_template_parameter_list
2438 (cp_parser *);
2439 static void cp_parser_skip_to_pragma_eol
2440 (cp_parser*, cp_token *);
2441 static bool cp_parser_error_occurred
2442 (cp_parser *);
2443 static bool cp_parser_allow_gnu_extensions_p
2444 (cp_parser *);
2445 static bool cp_parser_is_pure_string_literal
2446 (cp_token *);
2447 static bool cp_parser_is_string_literal
2448 (cp_token *);
2449 static bool cp_parser_is_keyword
2450 (cp_token *, enum rid);
2451 static tree cp_parser_make_typename_type
2452 (cp_parser *, tree, tree, location_t location);
2453 static cp_declarator * cp_parser_make_indirect_declarator
2454 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2455
2456 /* Returns nonzero if we are parsing tentatively. */
2457
2458 static inline bool
2459 cp_parser_parsing_tentatively (cp_parser* parser)
2460 {
2461 return parser->context->next != NULL;
2462 }
2463
2464 /* Returns nonzero if TOKEN is a string literal. */
2465
2466 static bool
2467 cp_parser_is_pure_string_literal (cp_token* token)
2468 {
2469 return (token->type == CPP_STRING ||
2470 token->type == CPP_STRING16 ||
2471 token->type == CPP_STRING32 ||
2472 token->type == CPP_WSTRING ||
2473 token->type == CPP_UTF8STRING);
2474 }
2475
2476 /* Returns nonzero if TOKEN is a string literal
2477 of a user-defined string literal. */
2478
2479 static bool
2480 cp_parser_is_string_literal (cp_token* token)
2481 {
2482 return (cp_parser_is_pure_string_literal (token) ||
2483 token->type == CPP_STRING_USERDEF ||
2484 token->type == CPP_STRING16_USERDEF ||
2485 token->type == CPP_STRING32_USERDEF ||
2486 token->type == CPP_WSTRING_USERDEF ||
2487 token->type == CPP_UTF8STRING_USERDEF);
2488 }
2489
2490 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2491
2492 static bool
2493 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2494 {
2495 return token->keyword == keyword;
2496 }
2497
2498 /* If not parsing tentatively, issue a diagnostic of the form
2499 FILE:LINE: MESSAGE before TOKEN
2500 where TOKEN is the next token in the input stream. MESSAGE
2501 (specified by the caller) is usually of the form "expected
2502 OTHER-TOKEN". */
2503
2504 static void
2505 cp_parser_error (cp_parser* parser, const char* gmsgid)
2506 {
2507 if (!cp_parser_simulate_error (parser))
2508 {
2509 cp_token *token = cp_lexer_peek_token (parser->lexer);
2510 /* This diagnostic makes more sense if it is tagged to the line
2511 of the token we just peeked at. */
2512 cp_lexer_set_source_position_from_token (token);
2513
2514 if (token->type == CPP_PRAGMA)
2515 {
2516 error_at (token->location,
2517 "%<#pragma%> is not allowed here");
2518 cp_parser_skip_to_pragma_eol (parser, token);
2519 return;
2520 }
2521
2522 c_parse_error (gmsgid,
2523 /* Because c_parser_error does not understand
2524 CPP_KEYWORD, keywords are treated like
2525 identifiers. */
2526 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2527 token->u.value, token->flags);
2528 }
2529 }
2530
2531 /* Issue an error about name-lookup failing. NAME is the
2532 IDENTIFIER_NODE DECL is the result of
2533 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2534 the thing that we hoped to find. */
2535
2536 static void
2537 cp_parser_name_lookup_error (cp_parser* parser,
2538 tree name,
2539 tree decl,
2540 name_lookup_error desired,
2541 location_t location)
2542 {
2543 /* If name lookup completely failed, tell the user that NAME was not
2544 declared. */
2545 if (decl == error_mark_node)
2546 {
2547 if (parser->scope && parser->scope != global_namespace)
2548 error_at (location, "%<%E::%E%> has not been declared",
2549 parser->scope, name);
2550 else if (parser->scope == global_namespace)
2551 error_at (location, "%<::%E%> has not been declared", name);
2552 else if (parser->object_scope
2553 && !CLASS_TYPE_P (parser->object_scope))
2554 error_at (location, "request for member %qE in non-class type %qT",
2555 name, parser->object_scope);
2556 else if (parser->object_scope)
2557 error_at (location, "%<%T::%E%> has not been declared",
2558 parser->object_scope, name);
2559 else
2560 error_at (location, "%qE has not been declared", name);
2561 }
2562 else if (parser->scope && parser->scope != global_namespace)
2563 {
2564 switch (desired)
2565 {
2566 case NLE_TYPE:
2567 error_at (location, "%<%E::%E%> is not a type",
2568 parser->scope, name);
2569 break;
2570 case NLE_CXX98:
2571 error_at (location, "%<%E::%E%> is not a class or namespace",
2572 parser->scope, name);
2573 break;
2574 case NLE_NOT_CXX98:
2575 error_at (location,
2576 "%<%E::%E%> is not a class, namespace, or enumeration",
2577 parser->scope, name);
2578 break;
2579 default:
2580 gcc_unreachable ();
2581
2582 }
2583 }
2584 else if (parser->scope == global_namespace)
2585 {
2586 switch (desired)
2587 {
2588 case NLE_TYPE:
2589 error_at (location, "%<::%E%> is not a type", name);
2590 break;
2591 case NLE_CXX98:
2592 error_at (location, "%<::%E%> is not a class or namespace", name);
2593 break;
2594 case NLE_NOT_CXX98:
2595 error_at (location,
2596 "%<::%E%> is not a class, namespace, or enumeration",
2597 name);
2598 break;
2599 default:
2600 gcc_unreachable ();
2601 }
2602 }
2603 else
2604 {
2605 switch (desired)
2606 {
2607 case NLE_TYPE:
2608 error_at (location, "%qE is not a type", name);
2609 break;
2610 case NLE_CXX98:
2611 error_at (location, "%qE is not a class or namespace", name);
2612 break;
2613 case NLE_NOT_CXX98:
2614 error_at (location,
2615 "%qE is not a class, namespace, or enumeration", name);
2616 break;
2617 default:
2618 gcc_unreachable ();
2619 }
2620 }
2621 }
2622
2623 /* If we are parsing tentatively, remember that an error has occurred
2624 during this tentative parse. Returns true if the error was
2625 simulated; false if a message should be issued by the caller. */
2626
2627 static bool
2628 cp_parser_simulate_error (cp_parser* parser)
2629 {
2630 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2631 {
2632 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2633 return true;
2634 }
2635 return false;
2636 }
2637
2638 /* This function is called when a type is defined. If type
2639 definitions are forbidden at this point, an error message is
2640 issued. */
2641
2642 static bool
2643 cp_parser_check_type_definition (cp_parser* parser)
2644 {
2645 /* If types are forbidden here, issue a message. */
2646 if (parser->type_definition_forbidden_message)
2647 {
2648 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2649 in the message need to be interpreted. */
2650 error (parser->type_definition_forbidden_message);
2651 return false;
2652 }
2653 return true;
2654 }
2655
2656 /* This function is called when the DECLARATOR is processed. The TYPE
2657 was a type defined in the decl-specifiers. If it is invalid to
2658 define a type in the decl-specifiers for DECLARATOR, an error is
2659 issued. TYPE_LOCATION is the location of TYPE and is used
2660 for error reporting. */
2661
2662 static void
2663 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2664 tree type, location_t type_location)
2665 {
2666 /* [dcl.fct] forbids type definitions in return types.
2667 Unfortunately, it's not easy to know whether or not we are
2668 processing a return type until after the fact. */
2669 while (declarator
2670 && (declarator->kind == cdk_pointer
2671 || declarator->kind == cdk_reference
2672 || declarator->kind == cdk_ptrmem))
2673 declarator = declarator->declarator;
2674 if (declarator
2675 && declarator->kind == cdk_function)
2676 {
2677 error_at (type_location,
2678 "new types may not be defined in a return type");
2679 inform (type_location,
2680 "(perhaps a semicolon is missing after the definition of %qT)",
2681 type);
2682 }
2683 }
2684
2685 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2686 "<" in any valid C++ program. If the next token is indeed "<",
2687 issue a message warning the user about what appears to be an
2688 invalid attempt to form a template-id. LOCATION is the location
2689 of the type-specifier (TYPE) */
2690
2691 static void
2692 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2693 tree type,
2694 enum tag_types tag_type,
2695 location_t location)
2696 {
2697 cp_token_position start = 0;
2698
2699 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2700 {
2701 if (TYPE_P (type))
2702 error_at (location, "%qT is not a template", type);
2703 else if (identifier_p (type))
2704 {
2705 if (tag_type != none_type)
2706 error_at (location, "%qE is not a class template", type);
2707 else
2708 error_at (location, "%qE is not a template", type);
2709 }
2710 else
2711 error_at (location, "invalid template-id");
2712 /* Remember the location of the invalid "<". */
2713 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2714 start = cp_lexer_token_position (parser->lexer, true);
2715 /* Consume the "<". */
2716 cp_lexer_consume_token (parser->lexer);
2717 /* Parse the template arguments. */
2718 cp_parser_enclosed_template_argument_list (parser);
2719 /* Permanently remove the invalid template arguments so that
2720 this error message is not issued again. */
2721 if (start)
2722 cp_lexer_purge_tokens_after (parser->lexer, start);
2723 }
2724 }
2725
2726 /* If parsing an integral constant-expression, issue an error message
2727 about the fact that THING appeared and return true. Otherwise,
2728 return false. In either case, set
2729 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2730
2731 static bool
2732 cp_parser_non_integral_constant_expression (cp_parser *parser,
2733 non_integral_constant thing)
2734 {
2735 parser->non_integral_constant_expression_p = true;
2736 if (parser->integral_constant_expression_p)
2737 {
2738 if (!parser->allow_non_integral_constant_expression_p)
2739 {
2740 const char *msg = NULL;
2741 switch (thing)
2742 {
2743 case NIC_FLOAT:
2744 error ("floating-point literal "
2745 "cannot appear in a constant-expression");
2746 return true;
2747 case NIC_CAST:
2748 error ("a cast to a type other than an integral or "
2749 "enumeration type cannot appear in a "
2750 "constant-expression");
2751 return true;
2752 case NIC_TYPEID:
2753 error ("%<typeid%> operator "
2754 "cannot appear in a constant-expression");
2755 return true;
2756 case NIC_NCC:
2757 error ("non-constant compound literals "
2758 "cannot appear in a constant-expression");
2759 return true;
2760 case NIC_FUNC_CALL:
2761 error ("a function call "
2762 "cannot appear in a constant-expression");
2763 return true;
2764 case NIC_INC:
2765 error ("an increment "
2766 "cannot appear in a constant-expression");
2767 return true;
2768 case NIC_DEC:
2769 error ("an decrement "
2770 "cannot appear in a constant-expression");
2771 return true;
2772 case NIC_ARRAY_REF:
2773 error ("an array reference "
2774 "cannot appear in a constant-expression");
2775 return true;
2776 case NIC_ADDR_LABEL:
2777 error ("the address of a label "
2778 "cannot appear in a constant-expression");
2779 return true;
2780 case NIC_OVERLOADED:
2781 error ("calls to overloaded operators "
2782 "cannot appear in a constant-expression");
2783 return true;
2784 case NIC_ASSIGNMENT:
2785 error ("an assignment cannot appear in a constant-expression");
2786 return true;
2787 case NIC_COMMA:
2788 error ("a comma operator "
2789 "cannot appear in a constant-expression");
2790 return true;
2791 case NIC_CONSTRUCTOR:
2792 error ("a call to a constructor "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_TRANSACTION:
2796 error ("a transaction expression "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_THIS:
2800 msg = "this";
2801 break;
2802 case NIC_FUNC_NAME:
2803 msg = "__FUNCTION__";
2804 break;
2805 case NIC_PRETTY_FUNC:
2806 msg = "__PRETTY_FUNCTION__";
2807 break;
2808 case NIC_C99_FUNC:
2809 msg = "__func__";
2810 break;
2811 case NIC_VA_ARG:
2812 msg = "va_arg";
2813 break;
2814 case NIC_ARROW:
2815 msg = "->";
2816 break;
2817 case NIC_POINT:
2818 msg = ".";
2819 break;
2820 case NIC_STAR:
2821 msg = "*";
2822 break;
2823 case NIC_ADDR:
2824 msg = "&";
2825 break;
2826 case NIC_PREINCREMENT:
2827 msg = "++";
2828 break;
2829 case NIC_PREDECREMENT:
2830 msg = "--";
2831 break;
2832 case NIC_NEW:
2833 msg = "new";
2834 break;
2835 case NIC_DEL:
2836 msg = "delete";
2837 break;
2838 default:
2839 gcc_unreachable ();
2840 }
2841 if (msg)
2842 error ("%qs cannot appear in a constant-expression", msg);
2843 return true;
2844 }
2845 }
2846 return false;
2847 }
2848
2849 /* Emit a diagnostic for an invalid type name. SCOPE is the
2850 qualifying scope (or NULL, if none) for ID. This function commits
2851 to the current active tentative parse, if any. (Otherwise, the
2852 problematic construct might be encountered again later, resulting
2853 in duplicate error messages.) LOCATION is the location of ID. */
2854
2855 static void
2856 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2857 tree scope, tree id,
2858 location_t location)
2859 {
2860 tree decl, old_scope;
2861 cp_parser_commit_to_tentative_parse (parser);
2862 /* Try to lookup the identifier. */
2863 old_scope = parser->scope;
2864 parser->scope = scope;
2865 decl = cp_parser_lookup_name_simple (parser, id, location);
2866 parser->scope = old_scope;
2867 /* If the lookup found a template-name, it means that the user forgot
2868 to specify an argument list. Emit a useful error message. */
2869 if (TREE_CODE (decl) == TEMPLATE_DECL)
2870 error_at (location,
2871 "invalid use of template-name %qE without an argument list",
2872 decl);
2873 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2874 error_at (location, "invalid use of destructor %qD as a type", id);
2875 else if (TREE_CODE (decl) == TYPE_DECL)
2876 /* Something like 'unsigned A a;' */
2877 error_at (location, "invalid combination of multiple type-specifiers");
2878 else if (!parser->scope)
2879 {
2880 /* Issue an error message. */
2881 error_at (location, "%qE does not name a type", id);
2882 /* If we're in a template class, it's possible that the user was
2883 referring to a type from a base class. For example:
2884
2885 template <typename T> struct A { typedef T X; };
2886 template <typename T> struct B : public A<T> { X x; };
2887
2888 The user should have said "typename A<T>::X". */
2889 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2890 inform (location, "C++11 %<constexpr%> only available with "
2891 "-std=c++11 or -std=gnu++11");
2892 else if (processing_template_decl && current_class_type
2893 && TYPE_BINFO (current_class_type))
2894 {
2895 tree b;
2896
2897 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2898 b;
2899 b = TREE_CHAIN (b))
2900 {
2901 tree base_type = BINFO_TYPE (b);
2902 if (CLASS_TYPE_P (base_type)
2903 && dependent_type_p (base_type))
2904 {
2905 tree field;
2906 /* Go from a particular instantiation of the
2907 template (which will have an empty TYPE_FIELDs),
2908 to the main version. */
2909 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2910 for (field = TYPE_FIELDS (base_type);
2911 field;
2912 field = DECL_CHAIN (field))
2913 if (TREE_CODE (field) == TYPE_DECL
2914 && DECL_NAME (field) == id)
2915 {
2916 inform (location,
2917 "(perhaps %<typename %T::%E%> was intended)",
2918 BINFO_TYPE (b), id);
2919 break;
2920 }
2921 if (field)
2922 break;
2923 }
2924 }
2925 }
2926 }
2927 /* Here we diagnose qualified-ids where the scope is actually correct,
2928 but the identifier does not resolve to a valid type name. */
2929 else if (parser->scope != error_mark_node)
2930 {
2931 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2932 {
2933 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2934 error_at (location_of (id),
2935 "%qE in namespace %qE does not name a template type",
2936 id, parser->scope);
2937 else
2938 error_at (location_of (id),
2939 "%qE in namespace %qE does not name a type",
2940 id, parser->scope);
2941 }
2942 else if (CLASS_TYPE_P (parser->scope)
2943 && constructor_name_p (id, parser->scope))
2944 {
2945 /* A<T>::A<T>() */
2946 error_at (location, "%<%T::%E%> names the constructor, not"
2947 " the type", parser->scope, id);
2948 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2949 error_at (location, "and %qT has no template constructors",
2950 parser->scope);
2951 }
2952 else if (TYPE_P (parser->scope)
2953 && dependent_scope_p (parser->scope))
2954 error_at (location, "need %<typename%> before %<%T::%E%> because "
2955 "%qT is a dependent scope",
2956 parser->scope, id, parser->scope);
2957 else if (TYPE_P (parser->scope))
2958 {
2959 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2960 error_at (location_of (id),
2961 "%qE in %q#T does not name a template type",
2962 id, parser->scope);
2963 else
2964 error_at (location_of (id),
2965 "%qE in %q#T does not name a type",
2966 id, parser->scope);
2967 }
2968 else
2969 gcc_unreachable ();
2970 }
2971 }
2972
2973 /* Check for a common situation where a type-name should be present,
2974 but is not, and issue a sensible error message. Returns true if an
2975 invalid type-name was detected.
2976
2977 The situation handled by this function are variable declarations of the
2978 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2979 Usually, `ID' should name a type, but if we got here it means that it
2980 does not. We try to emit the best possible error message depending on
2981 how exactly the id-expression looks like. */
2982
2983 static bool
2984 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2985 {
2986 tree id;
2987 cp_token *token = cp_lexer_peek_token (parser->lexer);
2988
2989 /* Avoid duplicate error about ambiguous lookup. */
2990 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2991 {
2992 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2993 if (next->type == CPP_NAME && next->ambiguous_p)
2994 goto out;
2995 }
2996
2997 cp_parser_parse_tentatively (parser);
2998 id = cp_parser_id_expression (parser,
2999 /*template_keyword_p=*/false,
3000 /*check_dependency_p=*/true,
3001 /*template_p=*/NULL,
3002 /*declarator_p=*/true,
3003 /*optional_p=*/false);
3004 /* If the next token is a (, this is a function with no explicit return
3005 type, i.e. constructor, destructor or conversion op. */
3006 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3007 || TREE_CODE (id) == TYPE_DECL)
3008 {
3009 cp_parser_abort_tentative_parse (parser);
3010 return false;
3011 }
3012 if (!cp_parser_parse_definitely (parser))
3013 return false;
3014
3015 /* Emit a diagnostic for the invalid type. */
3016 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3017 id, token->location);
3018 out:
3019 /* If we aren't in the middle of a declarator (i.e. in a
3020 parameter-declaration-clause), skip to the end of the declaration;
3021 there's no point in trying to process it. */
3022 if (!parser->in_declarator_p)
3023 cp_parser_skip_to_end_of_block_or_statement (parser);
3024 return true;
3025 }
3026
3027 /* Consume tokens up to, and including, the next non-nested closing `)'.
3028 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3029 are doing error recovery. Returns -1 if OR_COMMA is true and we
3030 found an unnested comma. */
3031
3032 static int
3033 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3034 bool recovering,
3035 bool or_comma,
3036 bool consume_paren)
3037 {
3038 unsigned paren_depth = 0;
3039 unsigned brace_depth = 0;
3040 unsigned square_depth = 0;
3041
3042 if (recovering && !or_comma
3043 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3044 return 0;
3045
3046 while (true)
3047 {
3048 cp_token * token = cp_lexer_peek_token (parser->lexer);
3049
3050 switch (token->type)
3051 {
3052 case CPP_EOF:
3053 case CPP_PRAGMA_EOL:
3054 /* If we've run out of tokens, then there is no closing `)'. */
3055 return 0;
3056
3057 /* This is good for lambda expression capture-lists. */
3058 case CPP_OPEN_SQUARE:
3059 ++square_depth;
3060 break;
3061 case CPP_CLOSE_SQUARE:
3062 if (!square_depth--)
3063 return 0;
3064 break;
3065
3066 case CPP_SEMICOLON:
3067 /* This matches the processing in skip_to_end_of_statement. */
3068 if (!brace_depth)
3069 return 0;
3070 break;
3071
3072 case CPP_OPEN_BRACE:
3073 ++brace_depth;
3074 break;
3075 case CPP_CLOSE_BRACE:
3076 if (!brace_depth--)
3077 return 0;
3078 break;
3079
3080 case CPP_COMMA:
3081 if (recovering && or_comma && !brace_depth && !paren_depth
3082 && !square_depth)
3083 return -1;
3084 break;
3085
3086 case CPP_OPEN_PAREN:
3087 if (!brace_depth)
3088 ++paren_depth;
3089 break;
3090
3091 case CPP_CLOSE_PAREN:
3092 if (!brace_depth && !paren_depth--)
3093 {
3094 if (consume_paren)
3095 cp_lexer_consume_token (parser->lexer);
3096 return 1;
3097 }
3098 break;
3099
3100 default:
3101 break;
3102 }
3103
3104 /* Consume the token. */
3105 cp_lexer_consume_token (parser->lexer);
3106 }
3107 }
3108
3109 /* Consume tokens until we reach the end of the current statement.
3110 Normally, that will be just before consuming a `;'. However, if a
3111 non-nested `}' comes first, then we stop before consuming that. */
3112
3113 static void
3114 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3115 {
3116 unsigned nesting_depth = 0;
3117
3118 while (true)
3119 {
3120 cp_token *token = cp_lexer_peek_token (parser->lexer);
3121
3122 switch (token->type)
3123 {
3124 case CPP_EOF:
3125 case CPP_PRAGMA_EOL:
3126 /* If we've run out of tokens, stop. */
3127 return;
3128
3129 case CPP_SEMICOLON:
3130 /* If the next token is a `;', we have reached the end of the
3131 statement. */
3132 if (!nesting_depth)
3133 return;
3134 break;
3135
3136 case CPP_CLOSE_BRACE:
3137 /* If this is a non-nested '}', stop before consuming it.
3138 That way, when confronted with something like:
3139
3140 { 3 + }
3141
3142 we stop before consuming the closing '}', even though we
3143 have not yet reached a `;'. */
3144 if (nesting_depth == 0)
3145 return;
3146
3147 /* If it is the closing '}' for a block that we have
3148 scanned, stop -- but only after consuming the token.
3149 That way given:
3150
3151 void f g () { ... }
3152 typedef int I;
3153
3154 we will stop after the body of the erroneously declared
3155 function, but before consuming the following `typedef'
3156 declaration. */
3157 if (--nesting_depth == 0)
3158 {
3159 cp_lexer_consume_token (parser->lexer);
3160 return;
3161 }
3162
3163 case CPP_OPEN_BRACE:
3164 ++nesting_depth;
3165 break;
3166
3167 default:
3168 break;
3169 }
3170
3171 /* Consume the token. */
3172 cp_lexer_consume_token (parser->lexer);
3173 }
3174 }
3175
3176 /* This function is called at the end of a statement or declaration.
3177 If the next token is a semicolon, it is consumed; otherwise, error
3178 recovery is attempted. */
3179
3180 static void
3181 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3182 {
3183 /* Look for the trailing `;'. */
3184 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3185 {
3186 /* If there is additional (erroneous) input, skip to the end of
3187 the statement. */
3188 cp_parser_skip_to_end_of_statement (parser);
3189 /* If the next token is now a `;', consume it. */
3190 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3191 cp_lexer_consume_token (parser->lexer);
3192 }
3193 }
3194
3195 /* Skip tokens until we have consumed an entire block, or until we
3196 have consumed a non-nested `;'. */
3197
3198 static void
3199 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3200 {
3201 int nesting_depth = 0;
3202
3203 while (nesting_depth >= 0)
3204 {
3205 cp_token *token = cp_lexer_peek_token (parser->lexer);
3206
3207 switch (token->type)
3208 {
3209 case CPP_EOF:
3210 case CPP_PRAGMA_EOL:
3211 /* If we've run out of tokens, stop. */
3212 return;
3213
3214 case CPP_SEMICOLON:
3215 /* Stop if this is an unnested ';'. */
3216 if (!nesting_depth)
3217 nesting_depth = -1;
3218 break;
3219
3220 case CPP_CLOSE_BRACE:
3221 /* Stop if this is an unnested '}', or closes the outermost
3222 nesting level. */
3223 nesting_depth--;
3224 if (nesting_depth < 0)
3225 return;
3226 if (!nesting_depth)
3227 nesting_depth = -1;
3228 break;
3229
3230 case CPP_OPEN_BRACE:
3231 /* Nest. */
3232 nesting_depth++;
3233 break;
3234
3235 default:
3236 break;
3237 }
3238
3239 /* Consume the token. */
3240 cp_lexer_consume_token (parser->lexer);
3241 }
3242 }
3243
3244 /* Skip tokens until a non-nested closing curly brace is the next
3245 token, or there are no more tokens. Return true in the first case,
3246 false otherwise. */
3247
3248 static bool
3249 cp_parser_skip_to_closing_brace (cp_parser *parser)
3250 {
3251 unsigned nesting_depth = 0;
3252
3253 while (true)
3254 {
3255 cp_token *token = cp_lexer_peek_token (parser->lexer);
3256
3257 switch (token->type)
3258 {
3259 case CPP_EOF:
3260 case CPP_PRAGMA_EOL:
3261 /* If we've run out of tokens, stop. */
3262 return false;
3263
3264 case CPP_CLOSE_BRACE:
3265 /* If the next token is a non-nested `}', then we have reached
3266 the end of the current block. */
3267 if (nesting_depth-- == 0)
3268 return true;
3269 break;
3270
3271 case CPP_OPEN_BRACE:
3272 /* If it the next token is a `{', then we are entering a new
3273 block. Consume the entire block. */
3274 ++nesting_depth;
3275 break;
3276
3277 default:
3278 break;
3279 }
3280
3281 /* Consume the token. */
3282 cp_lexer_consume_token (parser->lexer);
3283 }
3284 }
3285
3286 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3287 parameter is the PRAGMA token, allowing us to purge the entire pragma
3288 sequence. */
3289
3290 static void
3291 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3292 {
3293 cp_token *token;
3294
3295 parser->lexer->in_pragma = false;
3296
3297 do
3298 token = cp_lexer_consume_token (parser->lexer);
3299 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3300
3301 /* Ensure that the pragma is not parsed again. */
3302 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3303 }
3304
3305 /* Require pragma end of line, resyncing with it as necessary. The
3306 arguments are as for cp_parser_skip_to_pragma_eol. */
3307
3308 static void
3309 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3310 {
3311 parser->lexer->in_pragma = false;
3312 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3313 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3314 }
3315
3316 /* This is a simple wrapper around make_typename_type. When the id is
3317 an unresolved identifier node, we can provide a superior diagnostic
3318 using cp_parser_diagnose_invalid_type_name. */
3319
3320 static tree
3321 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3322 tree id, location_t id_location)
3323 {
3324 tree result;
3325 if (identifier_p (id))
3326 {
3327 result = make_typename_type (scope, id, typename_type,
3328 /*complain=*/tf_none);
3329 if (result == error_mark_node)
3330 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3331 return result;
3332 }
3333 return make_typename_type (scope, id, typename_type, tf_error);
3334 }
3335
3336 /* This is a wrapper around the
3337 make_{pointer,ptrmem,reference}_declarator functions that decides
3338 which one to call based on the CODE and CLASS_TYPE arguments. The
3339 CODE argument should be one of the values returned by
3340 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3341 appertain to the pointer or reference. */
3342
3343 static cp_declarator *
3344 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3345 cp_cv_quals cv_qualifiers,
3346 cp_declarator *target,
3347 tree attributes)
3348 {
3349 if (code == ERROR_MARK)
3350 return cp_error_declarator;
3351
3352 if (code == INDIRECT_REF)
3353 if (class_type == NULL_TREE)
3354 return make_pointer_declarator (cv_qualifiers, target, attributes);
3355 else
3356 return make_ptrmem_declarator (cv_qualifiers, class_type,
3357 target, attributes);
3358 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3359 return make_reference_declarator (cv_qualifiers, target,
3360 false, attributes);
3361 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3362 return make_reference_declarator (cv_qualifiers, target,
3363 true, attributes);
3364 gcc_unreachable ();
3365 }
3366
3367 /* Create a new C++ parser. */
3368
3369 static cp_parser *
3370 cp_parser_new (void)
3371 {
3372 cp_parser *parser;
3373 cp_lexer *lexer;
3374 unsigned i;
3375
3376 /* cp_lexer_new_main is called before doing GC allocation because
3377 cp_lexer_new_main might load a PCH file. */
3378 lexer = cp_lexer_new_main ();
3379
3380 /* Initialize the binops_by_token so that we can get the tree
3381 directly from the token. */
3382 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3383 binops_by_token[binops[i].token_type] = binops[i];
3384
3385 parser = ggc_alloc_cleared_cp_parser ();
3386 parser->lexer = lexer;
3387 parser->context = cp_parser_context_new (NULL);
3388
3389 /* For now, we always accept GNU extensions. */
3390 parser->allow_gnu_extensions_p = 1;
3391
3392 /* The `>' token is a greater-than operator, not the end of a
3393 template-id. */
3394 parser->greater_than_is_operator_p = true;
3395
3396 parser->default_arg_ok_p = true;
3397
3398 /* We are not parsing a constant-expression. */
3399 parser->integral_constant_expression_p = false;
3400 parser->allow_non_integral_constant_expression_p = false;
3401 parser->non_integral_constant_expression_p = false;
3402
3403 /* Local variable names are not forbidden. */
3404 parser->local_variables_forbidden_p = false;
3405
3406 /* We are not processing an `extern "C"' declaration. */
3407 parser->in_unbraced_linkage_specification_p = false;
3408
3409 /* We are not processing a declarator. */
3410 parser->in_declarator_p = false;
3411
3412 /* We are not processing a template-argument-list. */
3413 parser->in_template_argument_list_p = false;
3414
3415 /* We are not in an iteration statement. */
3416 parser->in_statement = 0;
3417
3418 /* We are not in a switch statement. */
3419 parser->in_switch_statement_p = false;
3420
3421 /* We are not parsing a type-id inside an expression. */
3422 parser->in_type_id_in_expr_p = false;
3423
3424 /* Declarations aren't implicitly extern "C". */
3425 parser->implicit_extern_c = false;
3426
3427 /* String literals should be translated to the execution character set. */
3428 parser->translate_strings_p = true;
3429
3430 /* We are not parsing a function body. */
3431 parser->in_function_body = false;
3432
3433 /* We can correct until told otherwise. */
3434 parser->colon_corrects_to_scope_p = true;
3435
3436 /* The unparsed function queue is empty. */
3437 push_unparsed_function_queues (parser);
3438
3439 /* There are no classes being defined. */
3440 parser->num_classes_being_defined = 0;
3441
3442 /* No template parameters apply. */
3443 parser->num_template_parameter_lists = 0;
3444
3445 /* Not declaring an implicit function template. */
3446 parser->fully_implicit_function_template_p = false;
3447
3448 return parser;
3449 }
3450
3451 /* Create a cp_lexer structure which will emit the tokens in CACHE
3452 and push it onto the parser's lexer stack. This is used for delayed
3453 parsing of in-class method bodies and default arguments, and should
3454 not be confused with tentative parsing. */
3455 static void
3456 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3457 {
3458 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3459 lexer->next = parser->lexer;
3460 parser->lexer = lexer;
3461
3462 /* Move the current source position to that of the first token in the
3463 new lexer. */
3464 cp_lexer_set_source_position_from_token (lexer->next_token);
3465 }
3466
3467 /* Pop the top lexer off the parser stack. This is never used for the
3468 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3469 static void
3470 cp_parser_pop_lexer (cp_parser *parser)
3471 {
3472 cp_lexer *lexer = parser->lexer;
3473 parser->lexer = lexer->next;
3474 cp_lexer_destroy (lexer);
3475
3476 /* Put the current source position back where it was before this
3477 lexer was pushed. */
3478 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3479 }
3480
3481 /* Lexical conventions [gram.lex] */
3482
3483 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3484 identifier. */
3485
3486 static tree
3487 cp_parser_identifier (cp_parser* parser)
3488 {
3489 cp_token *token;
3490
3491 /* Look for the identifier. */
3492 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3493 /* Return the value. */
3494 return token ? token->u.value : error_mark_node;
3495 }
3496
3497 /* Parse a sequence of adjacent string constants. Returns a
3498 TREE_STRING representing the combined, nul-terminated string
3499 constant. If TRANSLATE is true, translate the string to the
3500 execution character set. If WIDE_OK is true, a wide string is
3501 invalid here.
3502
3503 C++98 [lex.string] says that if a narrow string literal token is
3504 adjacent to a wide string literal token, the behavior is undefined.
3505 However, C99 6.4.5p4 says that this results in a wide string literal.
3506 We follow C99 here, for consistency with the C front end.
3507
3508 This code is largely lifted from lex_string() in c-lex.c.
3509
3510 FUTURE: ObjC++ will need to handle @-strings here. */
3511 static tree
3512 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3513 {
3514 tree value;
3515 size_t count;
3516 struct obstack str_ob;
3517 cpp_string str, istr, *strs;
3518 cp_token *tok;
3519 enum cpp_ttype type, curr_type;
3520 int have_suffix_p = 0;
3521 tree string_tree;
3522 tree suffix_id = NULL_TREE;
3523 bool curr_tok_is_userdef_p = false;
3524
3525 tok = cp_lexer_peek_token (parser->lexer);
3526 if (!cp_parser_is_string_literal (tok))
3527 {
3528 cp_parser_error (parser, "expected string-literal");
3529 return error_mark_node;
3530 }
3531
3532 if (cpp_userdef_string_p (tok->type))
3533 {
3534 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3535 curr_type = cpp_userdef_string_remove_type (tok->type);
3536 curr_tok_is_userdef_p = true;
3537 }
3538 else
3539 {
3540 string_tree = tok->u.value;
3541 curr_type = tok->type;
3542 }
3543 type = curr_type;
3544
3545 /* Try to avoid the overhead of creating and destroying an obstack
3546 for the common case of just one string. */
3547 if (!cp_parser_is_string_literal
3548 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3549 {
3550 cp_lexer_consume_token (parser->lexer);
3551
3552 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3553 str.len = TREE_STRING_LENGTH (string_tree);
3554 count = 1;
3555
3556 if (curr_tok_is_userdef_p)
3557 {
3558 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3559 have_suffix_p = 1;
3560 curr_type = cpp_userdef_string_remove_type (tok->type);
3561 }
3562 else
3563 curr_type = tok->type;
3564
3565 strs = &str;
3566 }
3567 else
3568 {
3569 gcc_obstack_init (&str_ob);
3570 count = 0;
3571
3572 do
3573 {
3574 cp_lexer_consume_token (parser->lexer);
3575 count++;
3576 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3577 str.len = TREE_STRING_LENGTH (string_tree);
3578
3579 if (curr_tok_is_userdef_p)
3580 {
3581 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3582 if (have_suffix_p == 0)
3583 {
3584 suffix_id = curr_suffix_id;
3585 have_suffix_p = 1;
3586 }
3587 else if (have_suffix_p == 1
3588 && curr_suffix_id != suffix_id)
3589 {
3590 error ("inconsistent user-defined literal suffixes"
3591 " %qD and %qD in string literal",
3592 suffix_id, curr_suffix_id);
3593 have_suffix_p = -1;
3594 }
3595 curr_type = cpp_userdef_string_remove_type (tok->type);
3596 }
3597 else
3598 curr_type = tok->type;
3599
3600 if (type != curr_type)
3601 {
3602 if (type == CPP_STRING)
3603 type = curr_type;
3604 else if (curr_type != CPP_STRING)
3605 error_at (tok->location,
3606 "unsupported non-standard concatenation "
3607 "of string literals");
3608 }
3609
3610 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3611
3612 tok = cp_lexer_peek_token (parser->lexer);
3613 if (cpp_userdef_string_p (tok->type))
3614 {
3615 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3616 curr_type = cpp_userdef_string_remove_type (tok->type);
3617 curr_tok_is_userdef_p = true;
3618 }
3619 else
3620 {
3621 string_tree = tok->u.value;
3622 curr_type = tok->type;
3623 curr_tok_is_userdef_p = false;
3624 }
3625 }
3626 while (cp_parser_is_string_literal (tok));
3627
3628 strs = (cpp_string *) obstack_finish (&str_ob);
3629 }
3630
3631 if (type != CPP_STRING && !wide_ok)
3632 {
3633 cp_parser_error (parser, "a wide string is invalid in this context");
3634 type = CPP_STRING;
3635 }
3636
3637 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3638 (parse_in, strs, count, &istr, type))
3639 {
3640 value = build_string (istr.len, (const char *)istr.text);
3641 free (CONST_CAST (unsigned char *, istr.text));
3642
3643 switch (type)
3644 {
3645 default:
3646 case CPP_STRING:
3647 case CPP_UTF8STRING:
3648 TREE_TYPE (value) = char_array_type_node;
3649 break;
3650 case CPP_STRING16:
3651 TREE_TYPE (value) = char16_array_type_node;
3652 break;
3653 case CPP_STRING32:
3654 TREE_TYPE (value) = char32_array_type_node;
3655 break;
3656 case CPP_WSTRING:
3657 TREE_TYPE (value) = wchar_array_type_node;
3658 break;
3659 }
3660
3661 value = fix_string_type (value);
3662
3663 if (have_suffix_p)
3664 {
3665 tree literal = build_userdef_literal (suffix_id, value,
3666 OT_NONE, NULL_TREE);
3667 tok->u.value = literal;
3668 return cp_parser_userdef_string_literal (tok);
3669 }
3670 }
3671 else
3672 /* cpp_interpret_string has issued an error. */
3673 value = error_mark_node;
3674
3675 if (count > 1)
3676 obstack_free (&str_ob, 0);
3677
3678 return value;
3679 }
3680
3681 /* Look up a literal operator with the name and the exact arguments. */
3682
3683 static tree
3684 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3685 {
3686 tree decl, fns;
3687 decl = lookup_name (name);
3688 if (!decl || !is_overloaded_fn (decl))
3689 return error_mark_node;
3690
3691 for (fns = decl; fns; fns = OVL_NEXT (fns))
3692 {
3693 unsigned int ix;
3694 bool found = true;
3695 tree fn = OVL_CURRENT (fns);
3696 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3697 if (parmtypes != NULL_TREE)
3698 {
3699 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3700 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3701 {
3702 tree tparm = TREE_VALUE (parmtypes);
3703 tree targ = TREE_TYPE ((*args)[ix]);
3704 bool ptr = TYPE_PTR_P (tparm);
3705 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3706 if ((ptr || arr || !same_type_p (tparm, targ))
3707 && (!ptr || !arr
3708 || !same_type_p (TREE_TYPE (tparm),
3709 TREE_TYPE (targ))))
3710 found = false;
3711 }
3712 if (found
3713 && ix == vec_safe_length (args)
3714 /* May be this should be sufficient_parms_p instead,
3715 depending on how exactly should user-defined literals
3716 work in presence of default arguments on the literal
3717 operator parameters. */
3718 && parmtypes == void_list_node)
3719 return fn;
3720 }
3721 }
3722
3723 return error_mark_node;
3724 }
3725
3726 /* Parse a user-defined char constant. Returns a call to a user-defined
3727 literal operator taking the character as an argument. */
3728
3729 static tree
3730 cp_parser_userdef_char_literal (cp_parser *parser)
3731 {
3732 cp_token *token = cp_lexer_consume_token (parser->lexer);
3733 tree literal = token->u.value;
3734 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3735 tree value = USERDEF_LITERAL_VALUE (literal);
3736 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3737 tree decl, result;
3738
3739 /* Build up a call to the user-defined operator */
3740 /* Lookup the name we got back from the id-expression. */
3741 vec<tree, va_gc> *args = make_tree_vector ();
3742 vec_safe_push (args, value);
3743 decl = lookup_literal_operator (name, args);
3744 if (!decl || decl == error_mark_node)
3745 {
3746 error ("unable to find character literal operator %qD with %qT argument",
3747 name, TREE_TYPE (value));
3748 release_tree_vector (args);
3749 return error_mark_node;
3750 }
3751 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3752 release_tree_vector (args);
3753 if (result != error_mark_node)
3754 return result;
3755
3756 error ("unable to find character literal operator %qD with %qT argument",
3757 name, TREE_TYPE (value));
3758 return error_mark_node;
3759 }
3760
3761 /* A subroutine of cp_parser_userdef_numeric_literal to
3762 create a char... template parameter pack from a string node. */
3763
3764 static tree
3765 make_char_string_pack (tree value)
3766 {
3767 tree charvec;
3768 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3769 const char *str = TREE_STRING_POINTER (value);
3770 int i, len = TREE_STRING_LENGTH (value) - 1;
3771 tree argvec = make_tree_vec (1);
3772
3773 /* Fill in CHARVEC with all of the parameters. */
3774 charvec = make_tree_vec (len);
3775 for (i = 0; i < len; ++i)
3776 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3777
3778 /* Build the argument packs. */
3779 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3780 TREE_TYPE (argpack) = char_type_node;
3781
3782 TREE_VEC_ELT (argvec, 0) = argpack;
3783
3784 return argvec;
3785 }
3786
3787 /* A subroutine of cp_parser_userdef_numeric_literal to
3788 create a char... template parameter pack from a string node. */
3789
3790 static tree
3791 make_string_pack (tree value)
3792 {
3793 tree charvec;
3794 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3795 const char *str = TREE_STRING_POINTER (value);
3796 int i, len = TREE_STRING_LENGTH (value) - 1;
3797 tree argvec = make_tree_vec (2);
3798
3799 tree string_char_type_node = TREE_TYPE (TREE_TYPE (value));
3800
3801 /* First template parm is character type. */
3802 TREE_VEC_ELT (argvec, 0) = string_char_type_node;
3803
3804 /* Fill in CHARVEC with all of the parameters. */
3805 charvec = make_tree_vec (len);
3806 for (i = 0; i < len; ++i)
3807 TREE_VEC_ELT (charvec, i) = build_int_cst (string_char_type_node, str[i]);
3808
3809 /* Build the argument packs. */
3810 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3811 TREE_TYPE (argpack) = string_char_type_node;
3812
3813 TREE_VEC_ELT (argvec, 1) = argpack;
3814
3815 return argvec;
3816 }
3817
3818 /* Parse a user-defined numeric constant. returns a call to a user-defined
3819 literal operator. */
3820
3821 static tree
3822 cp_parser_userdef_numeric_literal (cp_parser *parser)
3823 {
3824 cp_token *token = cp_lexer_consume_token (parser->lexer);
3825 tree literal = token->u.value;
3826 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3827 tree value = USERDEF_LITERAL_VALUE (literal);
3828 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3829 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3830 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3831 tree decl, result;
3832 vec<tree, va_gc> *args;
3833
3834 /* Look for a literal operator taking the exact type of numeric argument
3835 as the literal value. */
3836 args = make_tree_vector ();
3837 vec_safe_push (args, value);
3838 decl = lookup_literal_operator (name, args);
3839 if (decl && decl != error_mark_node)
3840 {
3841 result = finish_call_expr (decl, &args, false, true, tf_none);
3842 if (result != error_mark_node)
3843 {
3844 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3845 warning_at (token->location, OPT_Woverflow,
3846 "integer literal exceeds range of %qT type",
3847 long_long_unsigned_type_node);
3848 else
3849 {
3850 if (overflow > 0)
3851 warning_at (token->location, OPT_Woverflow,
3852 "floating literal exceeds range of %qT type",
3853 long_double_type_node);
3854 else if (overflow < 0)
3855 warning_at (token->location, OPT_Woverflow,
3856 "floating literal truncated to zero");
3857 }
3858 release_tree_vector (args);
3859 return result;
3860 }
3861 }
3862 release_tree_vector (args);
3863
3864 /* If the numeric argument didn't work, look for a raw literal
3865 operator taking a const char* argument consisting of the number
3866 in string format. */
3867 args = make_tree_vector ();
3868 vec_safe_push (args, num_string);
3869 decl = lookup_literal_operator (name, args);
3870 if (decl && decl != error_mark_node)
3871 {
3872 result = finish_call_expr (decl, &args, false, true, tf_none);
3873 if (result != error_mark_node)
3874 {
3875 release_tree_vector (args);
3876 return result;
3877 }
3878 }
3879 release_tree_vector (args);
3880
3881 /* If the raw literal didn't work, look for a non-type template
3882 function with parameter pack char.... Call the function with
3883 template parameter characters representing the number. */
3884 args = make_tree_vector ();
3885 decl = lookup_literal_operator (name, args);
3886 if (decl && decl != error_mark_node)
3887 {
3888 tree tmpl_args = make_char_string_pack (num_string);
3889 decl = lookup_template_function (decl, tmpl_args);
3890 result = finish_call_expr (decl, &args, false, true, tf_none);
3891 if (result != error_mark_node)
3892 {
3893 release_tree_vector (args);
3894 return result;
3895 }
3896 }
3897 release_tree_vector (args);
3898
3899 error ("unable to find numeric literal operator %qD", name);
3900 return error_mark_node;
3901 }
3902
3903 /* Parse a user-defined string constant. Returns a call to a user-defined
3904 literal operator taking a character pointer and the length of the string
3905 as arguments. */
3906
3907 static tree
3908 cp_parser_userdef_string_literal (cp_token *token)
3909 {
3910 tree literal = token->u.value;
3911 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3912 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3913 tree value = USERDEF_LITERAL_VALUE (literal);
3914 int len = TREE_STRING_LENGTH (value)
3915 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3916 tree decl, result;
3917 vec<tree, va_gc> *args;
3918
3919 /* Look for a template function with typename parameter CharT
3920 and parameter pack CharT... Call the function with
3921 template parameter characters representing the string. */
3922 args = make_tree_vector ();
3923 decl = lookup_literal_operator (name, args);
3924 if (decl && decl != error_mark_node)
3925 {
3926 tree tmpl_args = make_string_pack (value);
3927 decl = lookup_template_function (decl, tmpl_args);
3928 result = finish_call_expr (decl, &args, false, true, tf_none);
3929 if (result != error_mark_node)
3930 {
3931 release_tree_vector (args);
3932 return result;
3933 }
3934 }
3935 release_tree_vector (args);
3936
3937 /* Build up a call to the user-defined operator */
3938 /* Lookup the name we got back from the id-expression. */
3939 args = make_tree_vector ();
3940 vec_safe_push (args, value);
3941 vec_safe_push (args, build_int_cst (size_type_node, len));
3942 decl = lookup_name (name);
3943 if (!decl || decl == error_mark_node)
3944 {
3945 error ("unable to find string literal operator %qD", name);
3946 release_tree_vector (args);
3947 return error_mark_node;
3948 }
3949 result = finish_call_expr (decl, &args, false, true, tf_none);
3950 release_tree_vector (args);
3951 if (result != error_mark_node)
3952 return result;
3953
3954 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3955 name, TREE_TYPE (value), size_type_node);
3956 return error_mark_node;
3957 }
3958
3959
3960 /* Basic concepts [gram.basic] */
3961
3962 /* Parse a translation-unit.
3963
3964 translation-unit:
3965 declaration-seq [opt]
3966
3967 Returns TRUE if all went well. */
3968
3969 static bool
3970 cp_parser_translation_unit (cp_parser* parser)
3971 {
3972 /* The address of the first non-permanent object on the declarator
3973 obstack. */
3974 static void *declarator_obstack_base;
3975
3976 bool success;
3977
3978 /* Create the declarator obstack, if necessary. */
3979 if (!cp_error_declarator)
3980 {
3981 gcc_obstack_init (&declarator_obstack);
3982 /* Create the error declarator. */
3983 cp_error_declarator = make_declarator (cdk_error);
3984 /* Create the empty parameter list. */
3985 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3986 /* Remember where the base of the declarator obstack lies. */
3987 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3988 }
3989
3990 cp_parser_declaration_seq_opt (parser);
3991
3992 /* If there are no tokens left then all went well. */
3993 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3994 {
3995 /* Get rid of the token array; we don't need it any more. */
3996 cp_lexer_destroy (parser->lexer);
3997 parser->lexer = NULL;
3998
3999 /* This file might have been a context that's implicitly extern
4000 "C". If so, pop the lang context. (Only relevant for PCH.) */
4001 if (parser->implicit_extern_c)
4002 {
4003 pop_lang_context ();
4004 parser->implicit_extern_c = false;
4005 }
4006
4007 /* Finish up. */
4008 finish_translation_unit ();
4009
4010 success = true;
4011 }
4012 else
4013 {
4014 cp_parser_error (parser, "expected declaration");
4015 success = false;
4016 }
4017
4018 /* Make sure the declarator obstack was fully cleaned up. */
4019 gcc_assert (obstack_next_free (&declarator_obstack)
4020 == declarator_obstack_base);
4021
4022 /* All went well. */
4023 return success;
4024 }
4025
4026 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4027 decltype context. */
4028
4029 static inline tsubst_flags_t
4030 complain_flags (bool decltype_p)
4031 {
4032 tsubst_flags_t complain = tf_warning_or_error;
4033 if (decltype_p)
4034 complain |= tf_decltype;
4035 return complain;
4036 }
4037
4038 /* Expressions [gram.expr] */
4039
4040 /* Parse a primary-expression.
4041
4042 primary-expression:
4043 literal
4044 this
4045 ( expression )
4046 id-expression
4047
4048 GNU Extensions:
4049
4050 primary-expression:
4051 ( compound-statement )
4052 __builtin_va_arg ( assignment-expression , type-id )
4053 __builtin_offsetof ( type-id , offsetof-expression )
4054
4055 C++ Extensions:
4056 __has_nothrow_assign ( type-id )
4057 __has_nothrow_constructor ( type-id )
4058 __has_nothrow_copy ( type-id )
4059 __has_trivial_assign ( type-id )
4060 __has_trivial_constructor ( type-id )
4061 __has_trivial_copy ( type-id )
4062 __has_trivial_destructor ( type-id )
4063 __has_virtual_destructor ( type-id )
4064 __is_abstract ( type-id )
4065 __is_base_of ( type-id , type-id )
4066 __is_class ( type-id )
4067 __is_convertible_to ( type-id , type-id )
4068 __is_empty ( type-id )
4069 __is_enum ( type-id )
4070 __is_final ( type-id )
4071 __is_literal_type ( type-id )
4072 __is_pod ( type-id )
4073 __is_polymorphic ( type-id )
4074 __is_std_layout ( type-id )
4075 __is_trivial ( type-id )
4076 __is_union ( type-id )
4077
4078 Objective-C++ Extension:
4079
4080 primary-expression:
4081 objc-expression
4082
4083 literal:
4084 __null
4085
4086 ADDRESS_P is true iff this expression was immediately preceded by
4087 "&" and therefore might denote a pointer-to-member. CAST_P is true
4088 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4089 true iff this expression is a template argument.
4090
4091 Returns a representation of the expression. Upon return, *IDK
4092 indicates what kind of id-expression (if any) was present. */
4093
4094 static tree
4095 cp_parser_primary_expression (cp_parser *parser,
4096 bool address_p,
4097 bool cast_p,
4098 bool template_arg_p,
4099 bool decltype_p,
4100 cp_id_kind *idk)
4101 {
4102 cp_token *token = NULL;
4103
4104 /* Assume the primary expression is not an id-expression. */
4105 *idk = CP_ID_KIND_NONE;
4106
4107 /* Peek at the next token. */
4108 token = cp_lexer_peek_token (parser->lexer);
4109 switch (token->type)
4110 {
4111 /* literal:
4112 integer-literal
4113 character-literal
4114 floating-literal
4115 string-literal
4116 boolean-literal
4117 pointer-literal
4118 user-defined-literal */
4119 case CPP_CHAR:
4120 case CPP_CHAR16:
4121 case CPP_CHAR32:
4122 case CPP_WCHAR:
4123 case CPP_NUMBER:
4124 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4125 return cp_parser_userdef_numeric_literal (parser);
4126 token = cp_lexer_consume_token (parser->lexer);
4127 if (TREE_CODE (token->u.value) == FIXED_CST)
4128 {
4129 error_at (token->location,
4130 "fixed-point types not supported in C++");
4131 return error_mark_node;
4132 }
4133 /* Floating-point literals are only allowed in an integral
4134 constant expression if they are cast to an integral or
4135 enumeration type. */
4136 if (TREE_CODE (token->u.value) == REAL_CST
4137 && parser->integral_constant_expression_p
4138 && pedantic)
4139 {
4140 /* CAST_P will be set even in invalid code like "int(2.7 +
4141 ...)". Therefore, we have to check that the next token
4142 is sure to end the cast. */
4143 if (cast_p)
4144 {
4145 cp_token *next_token;
4146
4147 next_token = cp_lexer_peek_token (parser->lexer);
4148 if (/* The comma at the end of an
4149 enumerator-definition. */
4150 next_token->type != CPP_COMMA
4151 /* The curly brace at the end of an enum-specifier. */
4152 && next_token->type != CPP_CLOSE_BRACE
4153 /* The end of a statement. */
4154 && next_token->type != CPP_SEMICOLON
4155 /* The end of the cast-expression. */
4156 && next_token->type != CPP_CLOSE_PAREN
4157 /* The end of an array bound. */
4158 && next_token->type != CPP_CLOSE_SQUARE
4159 /* The closing ">" in a template-argument-list. */
4160 && (next_token->type != CPP_GREATER
4161 || parser->greater_than_is_operator_p)
4162 /* C++0x only: A ">>" treated like two ">" tokens,
4163 in a template-argument-list. */
4164 && (next_token->type != CPP_RSHIFT
4165 || (cxx_dialect == cxx98)
4166 || parser->greater_than_is_operator_p))
4167 cast_p = false;
4168 }
4169
4170 /* If we are within a cast, then the constraint that the
4171 cast is to an integral or enumeration type will be
4172 checked at that point. If we are not within a cast, then
4173 this code is invalid. */
4174 if (!cast_p)
4175 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4176 }
4177 return token->u.value;
4178
4179 case CPP_CHAR_USERDEF:
4180 case CPP_CHAR16_USERDEF:
4181 case CPP_CHAR32_USERDEF:
4182 case CPP_WCHAR_USERDEF:
4183 return cp_parser_userdef_char_literal (parser);
4184
4185 case CPP_STRING:
4186 case CPP_STRING16:
4187 case CPP_STRING32:
4188 case CPP_WSTRING:
4189 case CPP_UTF8STRING:
4190 case CPP_STRING_USERDEF:
4191 case CPP_STRING16_USERDEF:
4192 case CPP_STRING32_USERDEF:
4193 case CPP_WSTRING_USERDEF:
4194 case CPP_UTF8STRING_USERDEF:
4195 /* ??? Should wide strings be allowed when parser->translate_strings_p
4196 is false (i.e. in attributes)? If not, we can kill the third
4197 argument to cp_parser_string_literal. */
4198 return cp_parser_string_literal (parser,
4199 parser->translate_strings_p,
4200 true);
4201
4202 case CPP_OPEN_PAREN:
4203 {
4204 tree expr;
4205 bool saved_greater_than_is_operator_p;
4206
4207 /* Consume the `('. */
4208 cp_lexer_consume_token (parser->lexer);
4209 /* Within a parenthesized expression, a `>' token is always
4210 the greater-than operator. */
4211 saved_greater_than_is_operator_p
4212 = parser->greater_than_is_operator_p;
4213 parser->greater_than_is_operator_p = true;
4214 /* If we see `( { ' then we are looking at the beginning of
4215 a GNU statement-expression. */
4216 if (cp_parser_allow_gnu_extensions_p (parser)
4217 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4218 {
4219 /* Statement-expressions are not allowed by the standard. */
4220 pedwarn (token->location, OPT_Wpedantic,
4221 "ISO C++ forbids braced-groups within expressions");
4222
4223 /* And they're not allowed outside of a function-body; you
4224 cannot, for example, write:
4225
4226 int i = ({ int j = 3; j + 1; });
4227
4228 at class or namespace scope. */
4229 if (!parser->in_function_body
4230 || parser->in_template_argument_list_p)
4231 {
4232 error_at (token->location,
4233 "statement-expressions are not allowed outside "
4234 "functions nor in template-argument lists");
4235 cp_parser_skip_to_end_of_block_or_statement (parser);
4236 expr = error_mark_node;
4237 }
4238 else
4239 {
4240 /* Start the statement-expression. */
4241 expr = begin_stmt_expr ();
4242 /* Parse the compound-statement. */
4243 cp_parser_compound_statement (parser, expr, false, false);
4244 /* Finish up. */
4245 expr = finish_stmt_expr (expr, false);
4246 }
4247 }
4248 else
4249 {
4250 /* Parse the parenthesized expression. */
4251 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4252 /* Let the front end know that this expression was
4253 enclosed in parentheses. This matters in case, for
4254 example, the expression is of the form `A::B', since
4255 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4256 not. */
4257 expr = finish_parenthesized_expr (expr);
4258 /* DR 705: Wrapping an unqualified name in parentheses
4259 suppresses arg-dependent lookup. We want to pass back
4260 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4261 (c++/37862), but none of the others. */
4262 if (*idk != CP_ID_KIND_QUALIFIED)
4263 *idk = CP_ID_KIND_NONE;
4264 }
4265 /* The `>' token might be the end of a template-id or
4266 template-parameter-list now. */
4267 parser->greater_than_is_operator_p
4268 = saved_greater_than_is_operator_p;
4269 /* Consume the `)'. */
4270 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4271 cp_parser_skip_to_end_of_statement (parser);
4272
4273 return expr;
4274 }
4275
4276 case CPP_OPEN_SQUARE:
4277 if (c_dialect_objc ())
4278 /* We have an Objective-C++ message. */
4279 return cp_parser_objc_expression (parser);
4280 {
4281 tree lam = cp_parser_lambda_expression (parser);
4282 /* Don't warn about a failed tentative parse. */
4283 if (cp_parser_error_occurred (parser))
4284 return error_mark_node;
4285 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4286 return lam;
4287 }
4288
4289 case CPP_OBJC_STRING:
4290 if (c_dialect_objc ())
4291 /* We have an Objective-C++ string literal. */
4292 return cp_parser_objc_expression (parser);
4293 cp_parser_error (parser, "expected primary-expression");
4294 return error_mark_node;
4295
4296 case CPP_KEYWORD:
4297 switch (token->keyword)
4298 {
4299 /* These two are the boolean literals. */
4300 case RID_TRUE:
4301 cp_lexer_consume_token (parser->lexer);
4302 return boolean_true_node;
4303 case RID_FALSE:
4304 cp_lexer_consume_token (parser->lexer);
4305 return boolean_false_node;
4306
4307 /* The `__null' literal. */
4308 case RID_NULL:
4309 cp_lexer_consume_token (parser->lexer);
4310 return null_node;
4311
4312 /* The `nullptr' literal. */
4313 case RID_NULLPTR:
4314 cp_lexer_consume_token (parser->lexer);
4315 return nullptr_node;
4316
4317 /* Recognize the `this' keyword. */
4318 case RID_THIS:
4319 cp_lexer_consume_token (parser->lexer);
4320 if (parser->local_variables_forbidden_p)
4321 {
4322 error_at (token->location,
4323 "%<this%> may not be used in this context");
4324 return error_mark_node;
4325 }
4326 /* Pointers cannot appear in constant-expressions. */
4327 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4328 return error_mark_node;
4329 return finish_this_expr ();
4330
4331 /* The `operator' keyword can be the beginning of an
4332 id-expression. */
4333 case RID_OPERATOR:
4334 goto id_expression;
4335
4336 case RID_FUNCTION_NAME:
4337 case RID_PRETTY_FUNCTION_NAME:
4338 case RID_C99_FUNCTION_NAME:
4339 {
4340 non_integral_constant name;
4341
4342 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4343 __func__ are the names of variables -- but they are
4344 treated specially. Therefore, they are handled here,
4345 rather than relying on the generic id-expression logic
4346 below. Grammatically, these names are id-expressions.
4347
4348 Consume the token. */
4349 token = cp_lexer_consume_token (parser->lexer);
4350
4351 switch (token->keyword)
4352 {
4353 case RID_FUNCTION_NAME:
4354 name = NIC_FUNC_NAME;
4355 break;
4356 case RID_PRETTY_FUNCTION_NAME:
4357 name = NIC_PRETTY_FUNC;
4358 break;
4359 case RID_C99_FUNCTION_NAME:
4360 name = NIC_C99_FUNC;
4361 break;
4362 default:
4363 gcc_unreachable ();
4364 }
4365
4366 if (cp_parser_non_integral_constant_expression (parser, name))
4367 return error_mark_node;
4368
4369 /* Look up the name. */
4370 return finish_fname (token->u.value);
4371 }
4372
4373 case RID_VA_ARG:
4374 {
4375 tree expression;
4376 tree type;
4377 source_location type_location;
4378
4379 /* The `__builtin_va_arg' construct is used to handle
4380 `va_arg'. Consume the `__builtin_va_arg' token. */
4381 cp_lexer_consume_token (parser->lexer);
4382 /* Look for the opening `('. */
4383 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4384 /* Now, parse the assignment-expression. */
4385 expression = cp_parser_assignment_expression (parser,
4386 /*cast_p=*/false, NULL);
4387 /* Look for the `,'. */
4388 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4389 type_location = cp_lexer_peek_token (parser->lexer)->location;
4390 /* Parse the type-id. */
4391 type = cp_parser_type_id (parser);
4392 /* Look for the closing `)'. */
4393 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4394 /* Using `va_arg' in a constant-expression is not
4395 allowed. */
4396 if (cp_parser_non_integral_constant_expression (parser,
4397 NIC_VA_ARG))
4398 return error_mark_node;
4399 return build_x_va_arg (type_location, expression, type);
4400 }
4401
4402 case RID_OFFSETOF:
4403 return cp_parser_builtin_offsetof (parser);
4404
4405 case RID_HAS_NOTHROW_ASSIGN:
4406 case RID_HAS_NOTHROW_CONSTRUCTOR:
4407 case RID_HAS_NOTHROW_COPY:
4408 case RID_HAS_TRIVIAL_ASSIGN:
4409 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4410 case RID_HAS_TRIVIAL_COPY:
4411 case RID_HAS_TRIVIAL_DESTRUCTOR:
4412 case RID_HAS_VIRTUAL_DESTRUCTOR:
4413 case RID_IS_ABSTRACT:
4414 case RID_IS_BASE_OF:
4415 case RID_IS_CLASS:
4416 case RID_IS_CONVERTIBLE_TO:
4417 case RID_IS_EMPTY:
4418 case RID_IS_ENUM:
4419 case RID_IS_FINAL:
4420 case RID_IS_LITERAL_TYPE:
4421 case RID_IS_POD:
4422 case RID_IS_POLYMORPHIC:
4423 case RID_IS_STD_LAYOUT:
4424 case RID_IS_TRIVIAL:
4425 case RID_IS_UNION:
4426 return cp_parser_trait_expr (parser, token->keyword);
4427
4428 /* Objective-C++ expressions. */
4429 case RID_AT_ENCODE:
4430 case RID_AT_PROTOCOL:
4431 case RID_AT_SELECTOR:
4432 return cp_parser_objc_expression (parser);
4433
4434 case RID_TEMPLATE:
4435 if (parser->in_function_body
4436 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4437 == CPP_LESS))
4438 {
4439 error_at (token->location,
4440 "a template declaration cannot appear at block scope");
4441 cp_parser_skip_to_end_of_block_or_statement (parser);
4442 return error_mark_node;
4443 }
4444 default:
4445 cp_parser_error (parser, "expected primary-expression");
4446 return error_mark_node;
4447 }
4448
4449 /* An id-expression can start with either an identifier, a
4450 `::' as the beginning of a qualified-id, or the "operator"
4451 keyword. */
4452 case CPP_NAME:
4453 case CPP_SCOPE:
4454 case CPP_TEMPLATE_ID:
4455 case CPP_NESTED_NAME_SPECIFIER:
4456 {
4457 tree id_expression;
4458 tree decl;
4459 const char *error_msg;
4460 bool template_p;
4461 bool done;
4462 cp_token *id_expr_token;
4463
4464 id_expression:
4465 /* Parse the id-expression. */
4466 id_expression
4467 = cp_parser_id_expression (parser,
4468 /*template_keyword_p=*/false,
4469 /*check_dependency_p=*/true,
4470 &template_p,
4471 /*declarator_p=*/false,
4472 /*optional_p=*/false);
4473 if (id_expression == error_mark_node)
4474 return error_mark_node;
4475 id_expr_token = token;
4476 token = cp_lexer_peek_token (parser->lexer);
4477 done = (token->type != CPP_OPEN_SQUARE
4478 && token->type != CPP_OPEN_PAREN
4479 && token->type != CPP_DOT
4480 && token->type != CPP_DEREF
4481 && token->type != CPP_PLUS_PLUS
4482 && token->type != CPP_MINUS_MINUS);
4483 /* If we have a template-id, then no further lookup is
4484 required. If the template-id was for a template-class, we
4485 will sometimes have a TYPE_DECL at this point. */
4486 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4487 || TREE_CODE (id_expression) == TYPE_DECL)
4488 decl = id_expression;
4489 /* Look up the name. */
4490 else
4491 {
4492 tree ambiguous_decls;
4493
4494 /* If we already know that this lookup is ambiguous, then
4495 we've already issued an error message; there's no reason
4496 to check again. */
4497 if (id_expr_token->type == CPP_NAME
4498 && id_expr_token->ambiguous_p)
4499 {
4500 cp_parser_simulate_error (parser);
4501 return error_mark_node;
4502 }
4503
4504 decl = cp_parser_lookup_name (parser, id_expression,
4505 none_type,
4506 template_p,
4507 /*is_namespace=*/false,
4508 /*check_dependency=*/true,
4509 &ambiguous_decls,
4510 id_expr_token->location);
4511 /* If the lookup was ambiguous, an error will already have
4512 been issued. */
4513 if (ambiguous_decls)
4514 return error_mark_node;
4515
4516 /* In Objective-C++, we may have an Objective-C 2.0
4517 dot-syntax for classes here. */
4518 if (c_dialect_objc ()
4519 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4520 && TREE_CODE (decl) == TYPE_DECL
4521 && objc_is_class_name (decl))
4522 {
4523 tree component;
4524 cp_lexer_consume_token (parser->lexer);
4525 component = cp_parser_identifier (parser);
4526 if (component == error_mark_node)
4527 return error_mark_node;
4528
4529 return objc_build_class_component_ref (id_expression, component);
4530 }
4531
4532 /* In Objective-C++, an instance variable (ivar) may be preferred
4533 to whatever cp_parser_lookup_name() found. */
4534 decl = objc_lookup_ivar (decl, id_expression);
4535
4536 /* If name lookup gives us a SCOPE_REF, then the
4537 qualifying scope was dependent. */
4538 if (TREE_CODE (decl) == SCOPE_REF)
4539 {
4540 /* At this point, we do not know if DECL is a valid
4541 integral constant expression. We assume that it is
4542 in fact such an expression, so that code like:
4543
4544 template <int N> struct A {
4545 int a[B<N>::i];
4546 };
4547
4548 is accepted. At template-instantiation time, we
4549 will check that B<N>::i is actually a constant. */
4550 return decl;
4551 }
4552 /* Check to see if DECL is a local variable in a context
4553 where that is forbidden. */
4554 if (parser->local_variables_forbidden_p
4555 && local_variable_p (decl))
4556 {
4557 /* It might be that we only found DECL because we are
4558 trying to be generous with pre-ISO scoping rules.
4559 For example, consider:
4560
4561 int i;
4562 void g() {
4563 for (int i = 0; i < 10; ++i) {}
4564 extern void f(int j = i);
4565 }
4566
4567 Here, name look up will originally find the out
4568 of scope `i'. We need to issue a warning message,
4569 but then use the global `i'. */
4570 decl = check_for_out_of_scope_variable (decl);
4571 if (local_variable_p (decl))
4572 {
4573 error_at (id_expr_token->location,
4574 "local variable %qD may not appear in this context",
4575 decl);
4576 return error_mark_node;
4577 }
4578 }
4579 }
4580
4581 decl = (finish_id_expression
4582 (id_expression, decl, parser->scope,
4583 idk,
4584 parser->integral_constant_expression_p,
4585 parser->allow_non_integral_constant_expression_p,
4586 &parser->non_integral_constant_expression_p,
4587 template_p, done, address_p,
4588 template_arg_p,
4589 &error_msg,
4590 id_expr_token->location));
4591 if (error_msg)
4592 cp_parser_error (parser, error_msg);
4593 return decl;
4594 }
4595
4596 /* Anything else is an error. */
4597 default:
4598 cp_parser_error (parser, "expected primary-expression");
4599 return error_mark_node;
4600 }
4601 }
4602
4603 static inline tree
4604 cp_parser_primary_expression (cp_parser *parser,
4605 bool address_p,
4606 bool cast_p,
4607 bool template_arg_p,
4608 cp_id_kind *idk)
4609 {
4610 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4611 /*decltype*/false, idk);
4612 }
4613
4614 /* Parse an id-expression.
4615
4616 id-expression:
4617 unqualified-id
4618 qualified-id
4619
4620 qualified-id:
4621 :: [opt] nested-name-specifier template [opt] unqualified-id
4622 :: identifier
4623 :: operator-function-id
4624 :: template-id
4625
4626 Return a representation of the unqualified portion of the
4627 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4628 a `::' or nested-name-specifier.
4629
4630 Often, if the id-expression was a qualified-id, the caller will
4631 want to make a SCOPE_REF to represent the qualified-id. This
4632 function does not do this in order to avoid wastefully creating
4633 SCOPE_REFs when they are not required.
4634
4635 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4636 `template' keyword.
4637
4638 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4639 uninstantiated templates.
4640
4641 If *TEMPLATE_P is non-NULL, it is set to true iff the
4642 `template' keyword is used to explicitly indicate that the entity
4643 named is a template.
4644
4645 If DECLARATOR_P is true, the id-expression is appearing as part of
4646 a declarator, rather than as part of an expression. */
4647
4648 static tree
4649 cp_parser_id_expression (cp_parser *parser,
4650 bool template_keyword_p,
4651 bool check_dependency_p,
4652 bool *template_p,
4653 bool declarator_p,
4654 bool optional_p)
4655 {
4656 bool global_scope_p;
4657 bool nested_name_specifier_p;
4658
4659 /* Assume the `template' keyword was not used. */
4660 if (template_p)
4661 *template_p = template_keyword_p;
4662
4663 /* Look for the optional `::' operator. */
4664 global_scope_p
4665 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4666 != NULL_TREE);
4667 /* Look for the optional nested-name-specifier. */
4668 nested_name_specifier_p
4669 = (cp_parser_nested_name_specifier_opt (parser,
4670 /*typename_keyword_p=*/false,
4671 check_dependency_p,
4672 /*type_p=*/false,
4673 declarator_p)
4674 != NULL_TREE);
4675 /* If there is a nested-name-specifier, then we are looking at
4676 the first qualified-id production. */
4677 if (nested_name_specifier_p)
4678 {
4679 tree saved_scope;
4680 tree saved_object_scope;
4681 tree saved_qualifying_scope;
4682 tree unqualified_id;
4683 bool is_template;
4684
4685 /* See if the next token is the `template' keyword. */
4686 if (!template_p)
4687 template_p = &is_template;
4688 *template_p = cp_parser_optional_template_keyword (parser);
4689 /* Name lookup we do during the processing of the
4690 unqualified-id might obliterate SCOPE. */
4691 saved_scope = parser->scope;
4692 saved_object_scope = parser->object_scope;
4693 saved_qualifying_scope = parser->qualifying_scope;
4694 /* Process the final unqualified-id. */
4695 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4696 check_dependency_p,
4697 declarator_p,
4698 /*optional_p=*/false);
4699 /* Restore the SAVED_SCOPE for our caller. */
4700 parser->scope = saved_scope;
4701 parser->object_scope = saved_object_scope;
4702 parser->qualifying_scope = saved_qualifying_scope;
4703
4704 return unqualified_id;
4705 }
4706 /* Otherwise, if we are in global scope, then we are looking at one
4707 of the other qualified-id productions. */
4708 else if (global_scope_p)
4709 {
4710 cp_token *token;
4711 tree id;
4712
4713 /* Peek at the next token. */
4714 token = cp_lexer_peek_token (parser->lexer);
4715
4716 /* If it's an identifier, and the next token is not a "<", then
4717 we can avoid the template-id case. This is an optimization
4718 for this common case. */
4719 if (token->type == CPP_NAME
4720 && !cp_parser_nth_token_starts_template_argument_list_p
4721 (parser, 2))
4722 return cp_parser_identifier (parser);
4723
4724 cp_parser_parse_tentatively (parser);
4725 /* Try a template-id. */
4726 id = cp_parser_template_id (parser,
4727 /*template_keyword_p=*/false,
4728 /*check_dependency_p=*/true,
4729 none_type,
4730 declarator_p);
4731 /* If that worked, we're done. */
4732 if (cp_parser_parse_definitely (parser))
4733 return id;
4734
4735 /* Peek at the next token. (Changes in the token buffer may
4736 have invalidated the pointer obtained above.) */
4737 token = cp_lexer_peek_token (parser->lexer);
4738
4739 switch (token->type)
4740 {
4741 case CPP_NAME:
4742 return cp_parser_identifier (parser);
4743
4744 case CPP_KEYWORD:
4745 if (token->keyword == RID_OPERATOR)
4746 return cp_parser_operator_function_id (parser);
4747 /* Fall through. */
4748
4749 default:
4750 cp_parser_error (parser, "expected id-expression");
4751 return error_mark_node;
4752 }
4753 }
4754 else
4755 return cp_parser_unqualified_id (parser, template_keyword_p,
4756 /*check_dependency_p=*/true,
4757 declarator_p,
4758 optional_p);
4759 }
4760
4761 /* Parse an unqualified-id.
4762
4763 unqualified-id:
4764 identifier
4765 operator-function-id
4766 conversion-function-id
4767 ~ class-name
4768 template-id
4769
4770 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4771 keyword, in a construct like `A::template ...'.
4772
4773 Returns a representation of unqualified-id. For the `identifier'
4774 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4775 production a BIT_NOT_EXPR is returned; the operand of the
4776 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4777 other productions, see the documentation accompanying the
4778 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4779 names are looked up in uninstantiated templates. If DECLARATOR_P
4780 is true, the unqualified-id is appearing as part of a declarator,
4781 rather than as part of an expression. */
4782
4783 static tree
4784 cp_parser_unqualified_id (cp_parser* parser,
4785 bool template_keyword_p,
4786 bool check_dependency_p,
4787 bool declarator_p,
4788 bool optional_p)
4789 {
4790 cp_token *token;
4791
4792 /* Peek at the next token. */
4793 token = cp_lexer_peek_token (parser->lexer);
4794
4795 switch (token->type)
4796 {
4797 case CPP_NAME:
4798 {
4799 tree id;
4800
4801 /* We don't know yet whether or not this will be a
4802 template-id. */
4803 cp_parser_parse_tentatively (parser);
4804 /* Try a template-id. */
4805 id = cp_parser_template_id (parser, template_keyword_p,
4806 check_dependency_p,
4807 none_type,
4808 declarator_p);
4809 /* If it worked, we're done. */
4810 if (cp_parser_parse_definitely (parser))
4811 return id;
4812 /* Otherwise, it's an ordinary identifier. */
4813 return cp_parser_identifier (parser);
4814 }
4815
4816 case CPP_TEMPLATE_ID:
4817 return cp_parser_template_id (parser, template_keyword_p,
4818 check_dependency_p,
4819 none_type,
4820 declarator_p);
4821
4822 case CPP_COMPL:
4823 {
4824 tree type_decl;
4825 tree qualifying_scope;
4826 tree object_scope;
4827 tree scope;
4828 bool done;
4829
4830 /* Consume the `~' token. */
4831 cp_lexer_consume_token (parser->lexer);
4832 /* Parse the class-name. The standard, as written, seems to
4833 say that:
4834
4835 template <typename T> struct S { ~S (); };
4836 template <typename T> S<T>::~S() {}
4837
4838 is invalid, since `~' must be followed by a class-name, but
4839 `S<T>' is dependent, and so not known to be a class.
4840 That's not right; we need to look in uninstantiated
4841 templates. A further complication arises from:
4842
4843 template <typename T> void f(T t) {
4844 t.T::~T();
4845 }
4846
4847 Here, it is not possible to look up `T' in the scope of `T'
4848 itself. We must look in both the current scope, and the
4849 scope of the containing complete expression.
4850
4851 Yet another issue is:
4852
4853 struct S {
4854 int S;
4855 ~S();
4856 };
4857
4858 S::~S() {}
4859
4860 The standard does not seem to say that the `S' in `~S'
4861 should refer to the type `S' and not the data member
4862 `S::S'. */
4863
4864 /* DR 244 says that we look up the name after the "~" in the
4865 same scope as we looked up the qualifying name. That idea
4866 isn't fully worked out; it's more complicated than that. */
4867 scope = parser->scope;
4868 object_scope = parser->object_scope;
4869 qualifying_scope = parser->qualifying_scope;
4870
4871 /* Check for invalid scopes. */
4872 if (scope == error_mark_node)
4873 {
4874 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4875 cp_lexer_consume_token (parser->lexer);
4876 return error_mark_node;
4877 }
4878 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4879 {
4880 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4881 error_at (token->location,
4882 "scope %qT before %<~%> is not a class-name",
4883 scope);
4884 cp_parser_simulate_error (parser);
4885 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4886 cp_lexer_consume_token (parser->lexer);
4887 return error_mark_node;
4888 }
4889 gcc_assert (!scope || TYPE_P (scope));
4890
4891 /* If the name is of the form "X::~X" it's OK even if X is a
4892 typedef. */
4893 token = cp_lexer_peek_token (parser->lexer);
4894 if (scope
4895 && token->type == CPP_NAME
4896 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4897 != CPP_LESS)
4898 && (token->u.value == TYPE_IDENTIFIER (scope)
4899 || (CLASS_TYPE_P (scope)
4900 && constructor_name_p (token->u.value, scope))))
4901 {
4902 cp_lexer_consume_token (parser->lexer);
4903 return build_nt (BIT_NOT_EXPR, scope);
4904 }
4905
4906 /* ~auto means the destructor of whatever the object is. */
4907 if (cp_parser_is_keyword (token, RID_AUTO))
4908 {
4909 if (cxx_dialect < cxx1y)
4910 pedwarn (input_location, 0,
4911 "%<~auto%> only available with "
4912 "-std=c++1y or -std=gnu++1y");
4913 cp_lexer_consume_token (parser->lexer);
4914 return build_nt (BIT_NOT_EXPR, make_auto ());
4915 }
4916
4917 /* If there was an explicit qualification (S::~T), first look
4918 in the scope given by the qualification (i.e., S).
4919
4920 Note: in the calls to cp_parser_class_name below we pass
4921 typename_type so that lookup finds the injected-class-name
4922 rather than the constructor. */
4923 done = false;
4924 type_decl = NULL_TREE;
4925 if (scope)
4926 {
4927 cp_parser_parse_tentatively (parser);
4928 type_decl = cp_parser_class_name (parser,
4929 /*typename_keyword_p=*/false,
4930 /*template_keyword_p=*/false,
4931 typename_type,
4932 /*check_dependency=*/false,
4933 /*class_head_p=*/false,
4934 declarator_p);
4935 if (cp_parser_parse_definitely (parser))
4936 done = true;
4937 }
4938 /* In "N::S::~S", look in "N" as well. */
4939 if (!done && scope && qualifying_scope)
4940 {
4941 cp_parser_parse_tentatively (parser);
4942 parser->scope = qualifying_scope;
4943 parser->object_scope = NULL_TREE;
4944 parser->qualifying_scope = NULL_TREE;
4945 type_decl
4946 = cp_parser_class_name (parser,
4947 /*typename_keyword_p=*/false,
4948 /*template_keyword_p=*/false,
4949 typename_type,
4950 /*check_dependency=*/false,
4951 /*class_head_p=*/false,
4952 declarator_p);
4953 if (cp_parser_parse_definitely (parser))
4954 done = true;
4955 }
4956 /* In "p->S::~T", look in the scope given by "*p" as well. */
4957 else if (!done && object_scope)
4958 {
4959 cp_parser_parse_tentatively (parser);
4960 parser->scope = object_scope;
4961 parser->object_scope = NULL_TREE;
4962 parser->qualifying_scope = NULL_TREE;
4963 type_decl
4964 = cp_parser_class_name (parser,
4965 /*typename_keyword_p=*/false,
4966 /*template_keyword_p=*/false,
4967 typename_type,
4968 /*check_dependency=*/false,
4969 /*class_head_p=*/false,
4970 declarator_p);
4971 if (cp_parser_parse_definitely (parser))
4972 done = true;
4973 }
4974 /* Look in the surrounding context. */
4975 if (!done)
4976 {
4977 parser->scope = NULL_TREE;
4978 parser->object_scope = NULL_TREE;
4979 parser->qualifying_scope = NULL_TREE;
4980 if (processing_template_decl)
4981 cp_parser_parse_tentatively (parser);
4982 type_decl
4983 = cp_parser_class_name (parser,
4984 /*typename_keyword_p=*/false,
4985 /*template_keyword_p=*/false,
4986 typename_type,
4987 /*check_dependency=*/false,
4988 /*class_head_p=*/false,
4989 declarator_p);
4990 if (processing_template_decl
4991 && ! cp_parser_parse_definitely (parser))
4992 {
4993 /* We couldn't find a type with this name, so just accept
4994 it and check for a match at instantiation time. */
4995 type_decl = cp_parser_identifier (parser);
4996 if (type_decl != error_mark_node)
4997 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4998 return type_decl;
4999 }
5000 }
5001 /* If an error occurred, assume that the name of the
5002 destructor is the same as the name of the qualifying
5003 class. That allows us to keep parsing after running
5004 into ill-formed destructor names. */
5005 if (type_decl == error_mark_node && scope)
5006 return build_nt (BIT_NOT_EXPR, scope);
5007 else if (type_decl == error_mark_node)
5008 return error_mark_node;
5009
5010 /* Check that destructor name and scope match. */
5011 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5012 {
5013 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5014 error_at (token->location,
5015 "declaration of %<~%T%> as member of %qT",
5016 type_decl, scope);
5017 cp_parser_simulate_error (parser);
5018 return error_mark_node;
5019 }
5020
5021 /* [class.dtor]
5022
5023 A typedef-name that names a class shall not be used as the
5024 identifier in the declarator for a destructor declaration. */
5025 if (declarator_p
5026 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5027 && !DECL_SELF_REFERENCE_P (type_decl)
5028 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5029 error_at (token->location,
5030 "typedef-name %qD used as destructor declarator",
5031 type_decl);
5032
5033 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5034 }
5035
5036 case CPP_KEYWORD:
5037 if (token->keyword == RID_OPERATOR)
5038 {
5039 tree id;
5040
5041 /* This could be a template-id, so we try that first. */
5042 cp_parser_parse_tentatively (parser);
5043 /* Try a template-id. */
5044 id = cp_parser_template_id (parser, template_keyword_p,
5045 /*check_dependency_p=*/true,
5046 none_type,
5047 declarator_p);
5048 /* If that worked, we're done. */
5049 if (cp_parser_parse_definitely (parser))
5050 return id;
5051 /* We still don't know whether we're looking at an
5052 operator-function-id or a conversion-function-id. */
5053 cp_parser_parse_tentatively (parser);
5054 /* Try an operator-function-id. */
5055 id = cp_parser_operator_function_id (parser);
5056 /* If that didn't work, try a conversion-function-id. */
5057 if (!cp_parser_parse_definitely (parser))
5058 id = cp_parser_conversion_function_id (parser);
5059 else if (UDLIT_OPER_P (id))
5060 {
5061 /* 17.6.3.3.5 */
5062 const char *name = UDLIT_OP_SUFFIX (id);
5063 if (name[0] != '_' && !in_system_header && declarator_p)
5064 warning (0, "literal operator suffixes not preceded by %<_%>"
5065 " are reserved for future standardization");
5066 }
5067
5068 return id;
5069 }
5070 /* Fall through. */
5071
5072 default:
5073 if (optional_p)
5074 return NULL_TREE;
5075 cp_parser_error (parser, "expected unqualified-id");
5076 return error_mark_node;
5077 }
5078 }
5079
5080 /* Parse an (optional) nested-name-specifier.
5081
5082 nested-name-specifier: [C++98]
5083 class-or-namespace-name :: nested-name-specifier [opt]
5084 class-or-namespace-name :: template nested-name-specifier [opt]
5085
5086 nested-name-specifier: [C++0x]
5087 type-name ::
5088 namespace-name ::
5089 nested-name-specifier identifier ::
5090 nested-name-specifier template [opt] simple-template-id ::
5091
5092 PARSER->SCOPE should be set appropriately before this function is
5093 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5094 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5095 in name lookups.
5096
5097 Sets PARSER->SCOPE to the class (TYPE) or namespace
5098 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5099 it unchanged if there is no nested-name-specifier. Returns the new
5100 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5101
5102 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5103 part of a declaration and/or decl-specifier. */
5104
5105 static tree
5106 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5107 bool typename_keyword_p,
5108 bool check_dependency_p,
5109 bool type_p,
5110 bool is_declaration)
5111 {
5112 bool success = false;
5113 cp_token_position start = 0;
5114 cp_token *token;
5115
5116 /* Remember where the nested-name-specifier starts. */
5117 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5118 {
5119 start = cp_lexer_token_position (parser->lexer, false);
5120 push_deferring_access_checks (dk_deferred);
5121 }
5122
5123 while (true)
5124 {
5125 tree new_scope;
5126 tree old_scope;
5127 tree saved_qualifying_scope;
5128 bool template_keyword_p;
5129
5130 /* Spot cases that cannot be the beginning of a
5131 nested-name-specifier. */
5132 token = cp_lexer_peek_token (parser->lexer);
5133
5134 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5135 the already parsed nested-name-specifier. */
5136 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5137 {
5138 /* Grab the nested-name-specifier and continue the loop. */
5139 cp_parser_pre_parsed_nested_name_specifier (parser);
5140 /* If we originally encountered this nested-name-specifier
5141 with IS_DECLARATION set to false, we will not have
5142 resolved TYPENAME_TYPEs, so we must do so here. */
5143 if (is_declaration
5144 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5145 {
5146 new_scope = resolve_typename_type (parser->scope,
5147 /*only_current_p=*/false);
5148 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5149 parser->scope = new_scope;
5150 }
5151 success = true;
5152 continue;
5153 }
5154
5155 /* Spot cases that cannot be the beginning of a
5156 nested-name-specifier. On the second and subsequent times
5157 through the loop, we look for the `template' keyword. */
5158 if (success && token->keyword == RID_TEMPLATE)
5159 ;
5160 /* A template-id can start a nested-name-specifier. */
5161 else if (token->type == CPP_TEMPLATE_ID)
5162 ;
5163 /* DR 743: decltype can be used in a nested-name-specifier. */
5164 else if (token_is_decltype (token))
5165 ;
5166 else
5167 {
5168 /* If the next token is not an identifier, then it is
5169 definitely not a type-name or namespace-name. */
5170 if (token->type != CPP_NAME)
5171 break;
5172 /* If the following token is neither a `<' (to begin a
5173 template-id), nor a `::', then we are not looking at a
5174 nested-name-specifier. */
5175 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5176
5177 if (token->type == CPP_COLON
5178 && parser->colon_corrects_to_scope_p
5179 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5180 {
5181 error_at (token->location,
5182 "found %<:%> in nested-name-specifier, expected %<::%>");
5183 token->type = CPP_SCOPE;
5184 }
5185
5186 if (token->type != CPP_SCOPE
5187 && !cp_parser_nth_token_starts_template_argument_list_p
5188 (parser, 2))
5189 break;
5190 }
5191
5192 /* The nested-name-specifier is optional, so we parse
5193 tentatively. */
5194 cp_parser_parse_tentatively (parser);
5195
5196 /* Look for the optional `template' keyword, if this isn't the
5197 first time through the loop. */
5198 if (success)
5199 template_keyword_p = cp_parser_optional_template_keyword (parser);
5200 else
5201 template_keyword_p = false;
5202
5203 /* Save the old scope since the name lookup we are about to do
5204 might destroy it. */
5205 old_scope = parser->scope;
5206 saved_qualifying_scope = parser->qualifying_scope;
5207 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5208 look up names in "X<T>::I" in order to determine that "Y" is
5209 a template. So, if we have a typename at this point, we make
5210 an effort to look through it. */
5211 if (is_declaration
5212 && !typename_keyword_p
5213 && parser->scope
5214 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5215 parser->scope = resolve_typename_type (parser->scope,
5216 /*only_current_p=*/false);
5217 /* Parse the qualifying entity. */
5218 new_scope
5219 = cp_parser_qualifying_entity (parser,
5220 typename_keyword_p,
5221 template_keyword_p,
5222 check_dependency_p,
5223 type_p,
5224 is_declaration);
5225 /* Look for the `::' token. */
5226 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5227
5228 /* If we found what we wanted, we keep going; otherwise, we're
5229 done. */
5230 if (!cp_parser_parse_definitely (parser))
5231 {
5232 bool error_p = false;
5233
5234 /* Restore the OLD_SCOPE since it was valid before the
5235 failed attempt at finding the last
5236 class-or-namespace-name. */
5237 parser->scope = old_scope;
5238 parser->qualifying_scope = saved_qualifying_scope;
5239
5240 /* If the next token is a decltype, and the one after that is a
5241 `::', then the decltype has failed to resolve to a class or
5242 enumeration type. Give this error even when parsing
5243 tentatively since it can't possibly be valid--and we're going
5244 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5245 won't get another chance.*/
5246 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5247 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5248 == CPP_SCOPE))
5249 {
5250 token = cp_lexer_consume_token (parser->lexer);
5251 error_at (token->location, "decltype evaluates to %qT, "
5252 "which is not a class or enumeration type",
5253 token->u.value);
5254 parser->scope = error_mark_node;
5255 error_p = true;
5256 /* As below. */
5257 success = true;
5258 cp_lexer_consume_token (parser->lexer);
5259 }
5260
5261 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5262 break;
5263 /* If the next token is an identifier, and the one after
5264 that is a `::', then any valid interpretation would have
5265 found a class-or-namespace-name. */
5266 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5267 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5268 == CPP_SCOPE)
5269 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5270 != CPP_COMPL))
5271 {
5272 token = cp_lexer_consume_token (parser->lexer);
5273 if (!error_p)
5274 {
5275 if (!token->ambiguous_p)
5276 {
5277 tree decl;
5278 tree ambiguous_decls;
5279
5280 decl = cp_parser_lookup_name (parser, token->u.value,
5281 none_type,
5282 /*is_template=*/false,
5283 /*is_namespace=*/false,
5284 /*check_dependency=*/true,
5285 &ambiguous_decls,
5286 token->location);
5287 if (TREE_CODE (decl) == TEMPLATE_DECL)
5288 error_at (token->location,
5289 "%qD used without template parameters",
5290 decl);
5291 else if (ambiguous_decls)
5292 {
5293 // cp_parser_lookup_name has the same diagnostic,
5294 // thus make sure to emit it at most once.
5295 if (cp_parser_uncommitted_to_tentative_parse_p
5296 (parser))
5297 {
5298 error_at (token->location,
5299 "reference to %qD is ambiguous",
5300 token->u.value);
5301 print_candidates (ambiguous_decls);
5302 }
5303 decl = error_mark_node;
5304 }
5305 else
5306 {
5307 if (cxx_dialect != cxx98)
5308 cp_parser_name_lookup_error
5309 (parser, token->u.value, decl, NLE_NOT_CXX98,
5310 token->location);
5311 else
5312 cp_parser_name_lookup_error
5313 (parser, token->u.value, decl, NLE_CXX98,
5314 token->location);
5315 }
5316 }
5317 parser->scope = error_mark_node;
5318 error_p = true;
5319 /* Treat this as a successful nested-name-specifier
5320 due to:
5321
5322 [basic.lookup.qual]
5323
5324 If the name found is not a class-name (clause
5325 _class_) or namespace-name (_namespace.def_), the
5326 program is ill-formed. */
5327 success = true;
5328 }
5329 cp_lexer_consume_token (parser->lexer);
5330 }
5331 break;
5332 }
5333 /* We've found one valid nested-name-specifier. */
5334 success = true;
5335 /* Name lookup always gives us a DECL. */
5336 if (TREE_CODE (new_scope) == TYPE_DECL)
5337 new_scope = TREE_TYPE (new_scope);
5338 /* Uses of "template" must be followed by actual templates. */
5339 if (template_keyword_p
5340 && !(CLASS_TYPE_P (new_scope)
5341 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5342 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5343 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5344 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5345 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5346 == TEMPLATE_ID_EXPR)))
5347 permerror (input_location, TYPE_P (new_scope)
5348 ? G_("%qT is not a template")
5349 : G_("%qD is not a template"),
5350 new_scope);
5351 /* If it is a class scope, try to complete it; we are about to
5352 be looking up names inside the class. */
5353 if (TYPE_P (new_scope)
5354 /* Since checking types for dependency can be expensive,
5355 avoid doing it if the type is already complete. */
5356 && !COMPLETE_TYPE_P (new_scope)
5357 /* Do not try to complete dependent types. */
5358 && !dependent_type_p (new_scope))
5359 {
5360 new_scope = complete_type (new_scope);
5361 /* If it is a typedef to current class, use the current
5362 class instead, as the typedef won't have any names inside
5363 it yet. */
5364 if (!COMPLETE_TYPE_P (new_scope)
5365 && currently_open_class (new_scope))
5366 new_scope = TYPE_MAIN_VARIANT (new_scope);
5367 }
5368 /* Make sure we look in the right scope the next time through
5369 the loop. */
5370 parser->scope = new_scope;
5371 }
5372
5373 /* If parsing tentatively, replace the sequence of tokens that makes
5374 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5375 token. That way, should we re-parse the token stream, we will
5376 not have to repeat the effort required to do the parse, nor will
5377 we issue duplicate error messages. */
5378 if (success && start)
5379 {
5380 cp_token *token;
5381
5382 token = cp_lexer_token_at (parser->lexer, start);
5383 /* Reset the contents of the START token. */
5384 token->type = CPP_NESTED_NAME_SPECIFIER;
5385 /* Retrieve any deferred checks. Do not pop this access checks yet
5386 so the memory will not be reclaimed during token replacing below. */
5387 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5388 token->u.tree_check_value->value = parser->scope;
5389 token->u.tree_check_value->checks = get_deferred_access_checks ();
5390 token->u.tree_check_value->qualifying_scope =
5391 parser->qualifying_scope;
5392 token->keyword = RID_MAX;
5393
5394 /* Purge all subsequent tokens. */
5395 cp_lexer_purge_tokens_after (parser->lexer, start);
5396 }
5397
5398 if (start)
5399 pop_to_parent_deferring_access_checks ();
5400
5401 return success ? parser->scope : NULL_TREE;
5402 }
5403
5404 /* Parse a nested-name-specifier. See
5405 cp_parser_nested_name_specifier_opt for details. This function
5406 behaves identically, except that it will an issue an error if no
5407 nested-name-specifier is present. */
5408
5409 static tree
5410 cp_parser_nested_name_specifier (cp_parser *parser,
5411 bool typename_keyword_p,
5412 bool check_dependency_p,
5413 bool type_p,
5414 bool is_declaration)
5415 {
5416 tree scope;
5417
5418 /* Look for the nested-name-specifier. */
5419 scope = cp_parser_nested_name_specifier_opt (parser,
5420 typename_keyword_p,
5421 check_dependency_p,
5422 type_p,
5423 is_declaration);
5424 /* If it was not present, issue an error message. */
5425 if (!scope)
5426 {
5427 cp_parser_error (parser, "expected nested-name-specifier");
5428 parser->scope = NULL_TREE;
5429 }
5430
5431 return scope;
5432 }
5433
5434 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5435 this is either a class-name or a namespace-name (which corresponds
5436 to the class-or-namespace-name production in the grammar). For
5437 C++0x, it can also be a type-name that refers to an enumeration
5438 type or a simple-template-id.
5439
5440 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5441 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5442 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5443 TYPE_P is TRUE iff the next name should be taken as a class-name,
5444 even the same name is declared to be another entity in the same
5445 scope.
5446
5447 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5448 specified by the class-or-namespace-name. If neither is found the
5449 ERROR_MARK_NODE is returned. */
5450
5451 static tree
5452 cp_parser_qualifying_entity (cp_parser *parser,
5453 bool typename_keyword_p,
5454 bool template_keyword_p,
5455 bool check_dependency_p,
5456 bool type_p,
5457 bool is_declaration)
5458 {
5459 tree saved_scope;
5460 tree saved_qualifying_scope;
5461 tree saved_object_scope;
5462 tree scope;
5463 bool only_class_p;
5464 bool successful_parse_p;
5465
5466 /* DR 743: decltype can appear in a nested-name-specifier. */
5467 if (cp_lexer_next_token_is_decltype (parser->lexer))
5468 {
5469 scope = cp_parser_decltype (parser);
5470 if (TREE_CODE (scope) != ENUMERAL_TYPE
5471 && !MAYBE_CLASS_TYPE_P (scope))
5472 {
5473 cp_parser_simulate_error (parser);
5474 return error_mark_node;
5475 }
5476 if (TYPE_NAME (scope))
5477 scope = TYPE_NAME (scope);
5478 return scope;
5479 }
5480
5481 /* Before we try to parse the class-name, we must save away the
5482 current PARSER->SCOPE since cp_parser_class_name will destroy
5483 it. */
5484 saved_scope = parser->scope;
5485 saved_qualifying_scope = parser->qualifying_scope;
5486 saved_object_scope = parser->object_scope;
5487 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5488 there is no need to look for a namespace-name. */
5489 only_class_p = template_keyword_p
5490 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5491 if (!only_class_p)
5492 cp_parser_parse_tentatively (parser);
5493 scope = cp_parser_class_name (parser,
5494 typename_keyword_p,
5495 template_keyword_p,
5496 type_p ? class_type : none_type,
5497 check_dependency_p,
5498 /*class_head_p=*/false,
5499 is_declaration);
5500 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5501 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5502 if (!only_class_p
5503 && cxx_dialect != cxx98
5504 && !successful_parse_p)
5505 {
5506 /* Restore the saved scope. */
5507 parser->scope = saved_scope;
5508 parser->qualifying_scope = saved_qualifying_scope;
5509 parser->object_scope = saved_object_scope;
5510
5511 /* Parse tentatively. */
5512 cp_parser_parse_tentatively (parser);
5513
5514 /* Parse a type-name */
5515 scope = cp_parser_type_name (parser);
5516
5517 /* "If the name found does not designate a namespace or a class,
5518 enumeration, or dependent type, the program is ill-formed."
5519
5520 We cover classes and dependent types above and namespaces below,
5521 so this code is only looking for enums. */
5522 if (!scope || TREE_CODE (scope) != TYPE_DECL
5523 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5524 cp_parser_simulate_error (parser);
5525
5526 successful_parse_p = cp_parser_parse_definitely (parser);
5527 }
5528 /* If that didn't work, try for a namespace-name. */
5529 if (!only_class_p && !successful_parse_p)
5530 {
5531 /* Restore the saved scope. */
5532 parser->scope = saved_scope;
5533 parser->qualifying_scope = saved_qualifying_scope;
5534 parser->object_scope = saved_object_scope;
5535 /* If we are not looking at an identifier followed by the scope
5536 resolution operator, then this is not part of a
5537 nested-name-specifier. (Note that this function is only used
5538 to parse the components of a nested-name-specifier.) */
5539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5540 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5541 return error_mark_node;
5542 scope = cp_parser_namespace_name (parser);
5543 }
5544
5545 return scope;
5546 }
5547
5548 /* Parse a postfix-expression.
5549
5550 postfix-expression:
5551 primary-expression
5552 postfix-expression [ expression ]
5553 postfix-expression ( expression-list [opt] )
5554 simple-type-specifier ( expression-list [opt] )
5555 typename :: [opt] nested-name-specifier identifier
5556 ( expression-list [opt] )
5557 typename :: [opt] nested-name-specifier template [opt] template-id
5558 ( expression-list [opt] )
5559 postfix-expression . template [opt] id-expression
5560 postfix-expression -> template [opt] id-expression
5561 postfix-expression . pseudo-destructor-name
5562 postfix-expression -> pseudo-destructor-name
5563 postfix-expression ++
5564 postfix-expression --
5565 dynamic_cast < type-id > ( expression )
5566 static_cast < type-id > ( expression )
5567 reinterpret_cast < type-id > ( expression )
5568 const_cast < type-id > ( expression )
5569 typeid ( expression )
5570 typeid ( type-id )
5571
5572 GNU Extension:
5573
5574 postfix-expression:
5575 ( type-id ) { initializer-list , [opt] }
5576
5577 This extension is a GNU version of the C99 compound-literal
5578 construct. (The C99 grammar uses `type-name' instead of `type-id',
5579 but they are essentially the same concept.)
5580
5581 If ADDRESS_P is true, the postfix expression is the operand of the
5582 `&' operator. CAST_P is true if this expression is the target of a
5583 cast.
5584
5585 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5586 class member access expressions [expr.ref].
5587
5588 Returns a representation of the expression. */
5589
5590 static tree
5591 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5592 bool member_access_only_p, bool decltype_p,
5593 cp_id_kind * pidk_return)
5594 {
5595 cp_token *token;
5596 location_t loc;
5597 enum rid keyword;
5598 cp_id_kind idk = CP_ID_KIND_NONE;
5599 tree postfix_expression = NULL_TREE;
5600 bool is_member_access = false;
5601
5602 /* Peek at the next token. */
5603 token = cp_lexer_peek_token (parser->lexer);
5604 loc = token->location;
5605 /* Some of the productions are determined by keywords. */
5606 keyword = token->keyword;
5607 switch (keyword)
5608 {
5609 case RID_DYNCAST:
5610 case RID_STATCAST:
5611 case RID_REINTCAST:
5612 case RID_CONSTCAST:
5613 {
5614 tree type;
5615 tree expression;
5616 const char *saved_message;
5617 bool saved_in_type_id_in_expr_p;
5618
5619 /* All of these can be handled in the same way from the point
5620 of view of parsing. Begin by consuming the token
5621 identifying the cast. */
5622 cp_lexer_consume_token (parser->lexer);
5623
5624 /* New types cannot be defined in the cast. */
5625 saved_message = parser->type_definition_forbidden_message;
5626 parser->type_definition_forbidden_message
5627 = G_("types may not be defined in casts");
5628
5629 /* Look for the opening `<'. */
5630 cp_parser_require (parser, CPP_LESS, RT_LESS);
5631 /* Parse the type to which we are casting. */
5632 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5633 parser->in_type_id_in_expr_p = true;
5634 type = cp_parser_type_id (parser);
5635 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5636 /* Look for the closing `>'. */
5637 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5638 /* Restore the old message. */
5639 parser->type_definition_forbidden_message = saved_message;
5640
5641 bool saved_greater_than_is_operator_p
5642 = parser->greater_than_is_operator_p;
5643 parser->greater_than_is_operator_p = true;
5644
5645 /* And the expression which is being cast. */
5646 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5647 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5648 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5649
5650 parser->greater_than_is_operator_p
5651 = saved_greater_than_is_operator_p;
5652
5653 /* Only type conversions to integral or enumeration types
5654 can be used in constant-expressions. */
5655 if (!cast_valid_in_integral_constant_expression_p (type)
5656 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5657 return error_mark_node;
5658
5659 switch (keyword)
5660 {
5661 case RID_DYNCAST:
5662 postfix_expression
5663 = build_dynamic_cast (type, expression, tf_warning_or_error);
5664 break;
5665 case RID_STATCAST:
5666 postfix_expression
5667 = build_static_cast (type, expression, tf_warning_or_error);
5668 break;
5669 case RID_REINTCAST:
5670 postfix_expression
5671 = build_reinterpret_cast (type, expression,
5672 tf_warning_or_error);
5673 break;
5674 case RID_CONSTCAST:
5675 postfix_expression
5676 = build_const_cast (type, expression, tf_warning_or_error);
5677 break;
5678 default:
5679 gcc_unreachable ();
5680 }
5681 }
5682 break;
5683
5684 case RID_TYPEID:
5685 {
5686 tree type;
5687 const char *saved_message;
5688 bool saved_in_type_id_in_expr_p;
5689
5690 /* Consume the `typeid' token. */
5691 cp_lexer_consume_token (parser->lexer);
5692 /* Look for the `(' token. */
5693 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5694 /* Types cannot be defined in a `typeid' expression. */
5695 saved_message = parser->type_definition_forbidden_message;
5696 parser->type_definition_forbidden_message
5697 = G_("types may not be defined in a %<typeid%> expression");
5698 /* We can't be sure yet whether we're looking at a type-id or an
5699 expression. */
5700 cp_parser_parse_tentatively (parser);
5701 /* Try a type-id first. */
5702 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5703 parser->in_type_id_in_expr_p = true;
5704 type = cp_parser_type_id (parser);
5705 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5706 /* Look for the `)' token. Otherwise, we can't be sure that
5707 we're not looking at an expression: consider `typeid (int
5708 (3))', for example. */
5709 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5710 /* If all went well, simply lookup the type-id. */
5711 if (cp_parser_parse_definitely (parser))
5712 postfix_expression = get_typeid (type, tf_warning_or_error);
5713 /* Otherwise, fall back to the expression variant. */
5714 else
5715 {
5716 tree expression;
5717
5718 /* Look for an expression. */
5719 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5720 /* Compute its typeid. */
5721 postfix_expression = build_typeid (expression, tf_warning_or_error);
5722 /* Look for the `)' token. */
5723 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5724 }
5725 /* Restore the saved message. */
5726 parser->type_definition_forbidden_message = saved_message;
5727 /* `typeid' may not appear in an integral constant expression. */
5728 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5729 return error_mark_node;
5730 }
5731 break;
5732
5733 case RID_TYPENAME:
5734 {
5735 tree type;
5736 /* The syntax permitted here is the same permitted for an
5737 elaborated-type-specifier. */
5738 type = cp_parser_elaborated_type_specifier (parser,
5739 /*is_friend=*/false,
5740 /*is_declaration=*/false);
5741 postfix_expression = cp_parser_functional_cast (parser, type);
5742 }
5743 break;
5744
5745 case RID_BUILTIN_SHUFFLE:
5746 {
5747 vec<tree, va_gc> *vec;
5748 unsigned int i;
5749 tree p;
5750
5751 cp_lexer_consume_token (parser->lexer);
5752 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5753 /*cast_p=*/false, /*allow_expansion_p=*/true,
5754 /*non_constant_p=*/NULL);
5755 if (vec == NULL)
5756 return error_mark_node;
5757
5758 FOR_EACH_VEC_ELT (*vec, i, p)
5759 mark_exp_read (p);
5760
5761 if (vec->length () == 2)
5762 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5763 tf_warning_or_error);
5764 else if (vec->length () == 3)
5765 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5766 tf_warning_or_error);
5767 else
5768 {
5769 error_at (loc, "wrong number of arguments to "
5770 "%<__builtin_shuffle%>");
5771 return error_mark_node;
5772 }
5773 break;
5774 }
5775
5776 default:
5777 {
5778 tree type;
5779
5780 /* If the next thing is a simple-type-specifier, we may be
5781 looking at a functional cast. We could also be looking at
5782 an id-expression. So, we try the functional cast, and if
5783 that doesn't work we fall back to the primary-expression. */
5784 cp_parser_parse_tentatively (parser);
5785 /* Look for the simple-type-specifier. */
5786 type = cp_parser_simple_type_specifier (parser,
5787 /*decl_specs=*/NULL,
5788 CP_PARSER_FLAGS_NONE);
5789 /* Parse the cast itself. */
5790 if (!cp_parser_error_occurred (parser))
5791 postfix_expression
5792 = cp_parser_functional_cast (parser, type);
5793 /* If that worked, we're done. */
5794 if (cp_parser_parse_definitely (parser))
5795 break;
5796
5797 /* If the functional-cast didn't work out, try a
5798 compound-literal. */
5799 if (cp_parser_allow_gnu_extensions_p (parser)
5800 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5801 {
5802 tree initializer = NULL_TREE;
5803 bool saved_in_type_id_in_expr_p;
5804
5805 cp_parser_parse_tentatively (parser);
5806 /* Consume the `('. */
5807 cp_lexer_consume_token (parser->lexer);
5808 /* Parse the type. */
5809 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5810 parser->in_type_id_in_expr_p = true;
5811 type = cp_parser_type_id (parser);
5812 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5813 /* Look for the `)'. */
5814 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5815 /* If things aren't going well, there's no need to
5816 keep going. */
5817 if (!cp_parser_error_occurred (parser))
5818 {
5819 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5820 {
5821 bool non_constant_p;
5822 /* Parse the brace-enclosed initializer list. */
5823 initializer = cp_parser_braced_list (parser,
5824 &non_constant_p);
5825 }
5826 else
5827 cp_parser_simulate_error (parser);
5828 }
5829 /* If that worked, we're definitely looking at a
5830 compound-literal expression. */
5831 if (cp_parser_parse_definitely (parser))
5832 {
5833 /* Warn the user that a compound literal is not
5834 allowed in standard C++. */
5835 pedwarn (input_location, OPT_Wpedantic,
5836 "ISO C++ forbids compound-literals");
5837 /* For simplicity, we disallow compound literals in
5838 constant-expressions. We could
5839 allow compound literals of integer type, whose
5840 initializer was a constant, in constant
5841 expressions. Permitting that usage, as a further
5842 extension, would not change the meaning of any
5843 currently accepted programs. (Of course, as
5844 compound literals are not part of ISO C++, the
5845 standard has nothing to say.) */
5846 if (cp_parser_non_integral_constant_expression (parser,
5847 NIC_NCC))
5848 {
5849 postfix_expression = error_mark_node;
5850 break;
5851 }
5852 /* Form the representation of the compound-literal. */
5853 postfix_expression
5854 = finish_compound_literal (type, initializer,
5855 tf_warning_or_error);
5856 break;
5857 }
5858 }
5859
5860 /* It must be a primary-expression. */
5861 postfix_expression
5862 = cp_parser_primary_expression (parser, address_p, cast_p,
5863 /*template_arg_p=*/false,
5864 decltype_p,
5865 &idk);
5866 }
5867 break;
5868 }
5869
5870 /* Note that we don't need to worry about calling build_cplus_new on a
5871 class-valued CALL_EXPR in decltype when it isn't the end of the
5872 postfix-expression; unary_complex_lvalue will take care of that for
5873 all these cases. */
5874
5875 /* Keep looping until the postfix-expression is complete. */
5876 while (true)
5877 {
5878 if (idk == CP_ID_KIND_UNQUALIFIED
5879 && identifier_p (postfix_expression)
5880 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5881 /* It is not a Koenig lookup function call. */
5882 postfix_expression
5883 = unqualified_name_lookup_error (postfix_expression);
5884
5885 /* Peek at the next token. */
5886 token = cp_lexer_peek_token (parser->lexer);
5887
5888 switch (token->type)
5889 {
5890 case CPP_OPEN_SQUARE:
5891 if (cp_next_tokens_can_be_std_attribute_p (parser))
5892 {
5893 cp_parser_error (parser,
5894 "two consecutive %<[%> shall "
5895 "only introduce an attribute");
5896 return error_mark_node;
5897 }
5898 postfix_expression
5899 = cp_parser_postfix_open_square_expression (parser,
5900 postfix_expression,
5901 false,
5902 decltype_p);
5903 idk = CP_ID_KIND_NONE;
5904 is_member_access = false;
5905 break;
5906
5907 case CPP_OPEN_PAREN:
5908 /* postfix-expression ( expression-list [opt] ) */
5909 {
5910 bool koenig_p;
5911 bool is_builtin_constant_p;
5912 bool saved_integral_constant_expression_p = false;
5913 bool saved_non_integral_constant_expression_p = false;
5914 tsubst_flags_t complain = complain_flags (decltype_p);
5915 vec<tree, va_gc> *args;
5916
5917 is_member_access = false;
5918
5919 is_builtin_constant_p
5920 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5921 if (is_builtin_constant_p)
5922 {
5923 /* The whole point of __builtin_constant_p is to allow
5924 non-constant expressions to appear as arguments. */
5925 saved_integral_constant_expression_p
5926 = parser->integral_constant_expression_p;
5927 saved_non_integral_constant_expression_p
5928 = parser->non_integral_constant_expression_p;
5929 parser->integral_constant_expression_p = false;
5930 }
5931 args = (cp_parser_parenthesized_expression_list
5932 (parser, non_attr,
5933 /*cast_p=*/false, /*allow_expansion_p=*/true,
5934 /*non_constant_p=*/NULL));
5935 if (is_builtin_constant_p)
5936 {
5937 parser->integral_constant_expression_p
5938 = saved_integral_constant_expression_p;
5939 parser->non_integral_constant_expression_p
5940 = saved_non_integral_constant_expression_p;
5941 }
5942
5943 if (args == NULL)
5944 {
5945 postfix_expression = error_mark_node;
5946 break;
5947 }
5948
5949 /* Function calls are not permitted in
5950 constant-expressions. */
5951 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5952 && cp_parser_non_integral_constant_expression (parser,
5953 NIC_FUNC_CALL))
5954 {
5955 postfix_expression = error_mark_node;
5956 release_tree_vector (args);
5957 break;
5958 }
5959
5960 koenig_p = false;
5961 if (idk == CP_ID_KIND_UNQUALIFIED
5962 || idk == CP_ID_KIND_TEMPLATE_ID)
5963 {
5964 if (identifier_p (postfix_expression))
5965 {
5966 if (!args->is_empty ())
5967 {
5968 koenig_p = true;
5969 if (!any_type_dependent_arguments_p (args))
5970 postfix_expression
5971 = perform_koenig_lookup (postfix_expression, args,
5972 /*include_std=*/false,
5973 complain);
5974 }
5975 else
5976 postfix_expression
5977 = unqualified_fn_lookup_error (postfix_expression);
5978 }
5979 /* We do not perform argument-dependent lookup if
5980 normal lookup finds a non-function, in accordance
5981 with the expected resolution of DR 218. */
5982 else if (!args->is_empty ()
5983 && is_overloaded_fn (postfix_expression))
5984 {
5985 tree fn = get_first_fn (postfix_expression);
5986 fn = STRIP_TEMPLATE (fn);
5987
5988 /* Do not do argument dependent lookup if regular
5989 lookup finds a member function or a block-scope
5990 function declaration. [basic.lookup.argdep]/3 */
5991 if (!DECL_FUNCTION_MEMBER_P (fn)
5992 && !DECL_LOCAL_FUNCTION_P (fn))
5993 {
5994 koenig_p = true;
5995 if (!any_type_dependent_arguments_p (args))
5996 postfix_expression
5997 = perform_koenig_lookup (postfix_expression, args,
5998 /*include_std=*/false,
5999 complain);
6000 }
6001 }
6002 }
6003
6004 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6005 {
6006 tree instance = TREE_OPERAND (postfix_expression, 0);
6007 tree fn = TREE_OPERAND (postfix_expression, 1);
6008
6009 if (processing_template_decl
6010 && (type_dependent_expression_p (instance)
6011 || (!BASELINK_P (fn)
6012 && TREE_CODE (fn) != FIELD_DECL)
6013 || type_dependent_expression_p (fn)
6014 || any_type_dependent_arguments_p (args)))
6015 {
6016 postfix_expression
6017 = build_nt_call_vec (postfix_expression, args);
6018 release_tree_vector (args);
6019 break;
6020 }
6021
6022 if (BASELINK_P (fn))
6023 {
6024 postfix_expression
6025 = (build_new_method_call
6026 (instance, fn, &args, NULL_TREE,
6027 (idk == CP_ID_KIND_QUALIFIED
6028 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6029 : LOOKUP_NORMAL),
6030 /*fn_p=*/NULL,
6031 complain));
6032 }
6033 else
6034 postfix_expression
6035 = finish_call_expr (postfix_expression, &args,
6036 /*disallow_virtual=*/false,
6037 /*koenig_p=*/false,
6038 complain);
6039 }
6040 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6041 || TREE_CODE (postfix_expression) == MEMBER_REF
6042 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6043 postfix_expression = (build_offset_ref_call_from_tree
6044 (postfix_expression, &args,
6045 complain));
6046 else if (idk == CP_ID_KIND_QUALIFIED)
6047 /* A call to a static class member, or a namespace-scope
6048 function. */
6049 postfix_expression
6050 = finish_call_expr (postfix_expression, &args,
6051 /*disallow_virtual=*/true,
6052 koenig_p,
6053 complain);
6054 else
6055 /* All other function calls. */
6056 postfix_expression
6057 = finish_call_expr (postfix_expression, &args,
6058 /*disallow_virtual=*/false,
6059 koenig_p,
6060 complain);
6061
6062 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6063 idk = CP_ID_KIND_NONE;
6064
6065 release_tree_vector (args);
6066 }
6067 break;
6068
6069 case CPP_DOT:
6070 case CPP_DEREF:
6071 /* postfix-expression . template [opt] id-expression
6072 postfix-expression . pseudo-destructor-name
6073 postfix-expression -> template [opt] id-expression
6074 postfix-expression -> pseudo-destructor-name */
6075
6076 /* Consume the `.' or `->' operator. */
6077 cp_lexer_consume_token (parser->lexer);
6078
6079 postfix_expression
6080 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6081 postfix_expression,
6082 false, &idk, loc);
6083
6084 is_member_access = true;
6085 break;
6086
6087 case CPP_PLUS_PLUS:
6088 /* postfix-expression ++ */
6089 /* Consume the `++' token. */
6090 cp_lexer_consume_token (parser->lexer);
6091 /* Generate a representation for the complete expression. */
6092 postfix_expression
6093 = finish_increment_expr (postfix_expression,
6094 POSTINCREMENT_EXPR);
6095 /* Increments may not appear in constant-expressions. */
6096 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6097 postfix_expression = error_mark_node;
6098 idk = CP_ID_KIND_NONE;
6099 is_member_access = false;
6100 break;
6101
6102 case CPP_MINUS_MINUS:
6103 /* postfix-expression -- */
6104 /* Consume the `--' token. */
6105 cp_lexer_consume_token (parser->lexer);
6106 /* Generate a representation for the complete expression. */
6107 postfix_expression
6108 = finish_increment_expr (postfix_expression,
6109 POSTDECREMENT_EXPR);
6110 /* Decrements may not appear in constant-expressions. */
6111 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6112 postfix_expression = error_mark_node;
6113 idk = CP_ID_KIND_NONE;
6114 is_member_access = false;
6115 break;
6116
6117 default:
6118 if (pidk_return != NULL)
6119 * pidk_return = idk;
6120 if (member_access_only_p)
6121 return is_member_access? postfix_expression : error_mark_node;
6122 else
6123 return postfix_expression;
6124 }
6125 }
6126
6127 /* We should never get here. */
6128 gcc_unreachable ();
6129 return error_mark_node;
6130 }
6131
6132 /* This function parses Cilk Plus array notations. If a normal array expr. is
6133 parsed then the array index is passed back to the caller through *INIT_INDEX
6134 and the function returns a NULL_TREE. If array notation expr. is parsed,
6135 then *INIT_INDEX is ignored by the caller and the function returns
6136 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6137 error_mark_node. */
6138
6139 static tree
6140 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6141 tree array_value)
6142 {
6143 cp_token *token = NULL;
6144 tree length_index, stride = NULL_TREE, value_tree, array_type;
6145 if (!array_value || array_value == error_mark_node)
6146 {
6147 cp_parser_skip_to_end_of_statement (parser);
6148 return error_mark_node;
6149 }
6150
6151 array_type = TREE_TYPE (array_value);
6152
6153 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6154 parser->colon_corrects_to_scope_p = false;
6155 token = cp_lexer_peek_token (parser->lexer);
6156
6157 if (!token)
6158 {
6159 cp_parser_error (parser, "expected %<:%> or numeral");
6160 return error_mark_node;
6161 }
6162 else if (token->type == CPP_COLON)
6163 {
6164 /* Consume the ':'. */
6165 cp_lexer_consume_token (parser->lexer);
6166
6167 /* If we are here, then we have a case like this A[:]. */
6168 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6169 {
6170 cp_parser_error (parser, "expected %<]%>");
6171 cp_parser_skip_to_end_of_statement (parser);
6172 return error_mark_node;
6173 }
6174 *init_index = NULL_TREE;
6175 stride = NULL_TREE;
6176 length_index = NULL_TREE;
6177 }
6178 else
6179 {
6180 /* If we are here, then there are three valid possibilities:
6181 1. ARRAY [ EXP ]
6182 2. ARRAY [ EXP : EXP ]
6183 3. ARRAY [ EXP : EXP : EXP ] */
6184
6185 *init_index = cp_parser_expression (parser, false, NULL);
6186 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6187 {
6188 /* This indicates that we have a normal array expression. */
6189 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6190 return NULL_TREE;
6191 }
6192
6193 /* Consume the ':'. */
6194 cp_lexer_consume_token (parser->lexer);
6195 length_index = cp_parser_expression (parser, false, NULL);
6196 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6197 {
6198 cp_lexer_consume_token (parser->lexer);
6199 stride = cp_parser_expression (parser, false, NULL);
6200 }
6201 }
6202 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6203
6204 if (*init_index == error_mark_node || length_index == error_mark_node
6205 || stride == error_mark_node)
6206 {
6207 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6208 cp_lexer_consume_token (parser->lexer);
6209 return error_mark_node;
6210 }
6211 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6212
6213 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6214 length_index, stride, array_type);
6215 return value_tree;
6216 }
6217
6218 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6219 by cp_parser_builtin_offsetof. We're looking for
6220
6221 postfix-expression [ expression ]
6222 postfix-expression [ braced-init-list ] (C++11)
6223
6224 FOR_OFFSETOF is set if we're being called in that context, which
6225 changes how we deal with integer constant expressions. */
6226
6227 static tree
6228 cp_parser_postfix_open_square_expression (cp_parser *parser,
6229 tree postfix_expression,
6230 bool for_offsetof,
6231 bool decltype_p)
6232 {
6233 tree index = NULL_TREE;
6234 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6235 bool saved_greater_than_is_operator_p;
6236
6237 /* Consume the `[' token. */
6238 cp_lexer_consume_token (parser->lexer);
6239
6240 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6241 parser->greater_than_is_operator_p = true;
6242
6243 /* Parse the index expression. */
6244 /* ??? For offsetof, there is a question of what to allow here. If
6245 offsetof is not being used in an integral constant expression context,
6246 then we *could* get the right answer by computing the value at runtime.
6247 If we are in an integral constant expression context, then we might
6248 could accept any constant expression; hard to say without analysis.
6249 Rather than open the barn door too wide right away, allow only integer
6250 constant expressions here. */
6251 if (for_offsetof)
6252 index = cp_parser_constant_expression (parser, false, NULL);
6253 else
6254 {
6255 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6256 {
6257 bool expr_nonconst_p;
6258 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6259 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6260 if (flag_enable_cilkplus
6261 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6262 {
6263 error_at (cp_lexer_peek_token (parser->lexer)->location,
6264 "braced list index is not allowed with array "
6265 "notation");
6266 cp_parser_skip_to_end_of_statement (parser);
6267 return error_mark_node;
6268 }
6269 }
6270 else if (flag_enable_cilkplus)
6271 {
6272 /* Here are have these two options:
6273 ARRAY[EXP : EXP] - Array notation expr with default
6274 stride of 1.
6275 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6276 stride. */
6277 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6278 postfix_expression);
6279 if (an_exp)
6280 return an_exp;
6281 }
6282 else
6283 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6284 }
6285
6286 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6287
6288 /* Look for the closing `]'. */
6289 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6290
6291 /* Build the ARRAY_REF. */
6292 postfix_expression = grok_array_decl (loc, postfix_expression,
6293 index, decltype_p);
6294
6295 /* When not doing offsetof, array references are not permitted in
6296 constant-expressions. */
6297 if (!for_offsetof
6298 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6299 postfix_expression = error_mark_node;
6300
6301 return postfix_expression;
6302 }
6303
6304 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6305 by cp_parser_builtin_offsetof. We're looking for
6306
6307 postfix-expression . template [opt] id-expression
6308 postfix-expression . pseudo-destructor-name
6309 postfix-expression -> template [opt] id-expression
6310 postfix-expression -> pseudo-destructor-name
6311
6312 FOR_OFFSETOF is set if we're being called in that context. That sorta
6313 limits what of the above we'll actually accept, but nevermind.
6314 TOKEN_TYPE is the "." or "->" token, which will already have been
6315 removed from the stream. */
6316
6317 static tree
6318 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6319 enum cpp_ttype token_type,
6320 tree postfix_expression,
6321 bool for_offsetof, cp_id_kind *idk,
6322 location_t location)
6323 {
6324 tree name;
6325 bool dependent_p;
6326 bool pseudo_destructor_p;
6327 tree scope = NULL_TREE;
6328
6329 /* If this is a `->' operator, dereference the pointer. */
6330 if (token_type == CPP_DEREF)
6331 postfix_expression = build_x_arrow (location, postfix_expression,
6332 tf_warning_or_error);
6333 /* Check to see whether or not the expression is type-dependent. */
6334 dependent_p = type_dependent_expression_p (postfix_expression);
6335 /* The identifier following the `->' or `.' is not qualified. */
6336 parser->scope = NULL_TREE;
6337 parser->qualifying_scope = NULL_TREE;
6338 parser->object_scope = NULL_TREE;
6339 *idk = CP_ID_KIND_NONE;
6340
6341 /* Enter the scope corresponding to the type of the object
6342 given by the POSTFIX_EXPRESSION. */
6343 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6344 {
6345 scope = TREE_TYPE (postfix_expression);
6346 /* According to the standard, no expression should ever have
6347 reference type. Unfortunately, we do not currently match
6348 the standard in this respect in that our internal representation
6349 of an expression may have reference type even when the standard
6350 says it does not. Therefore, we have to manually obtain the
6351 underlying type here. */
6352 scope = non_reference (scope);
6353 /* The type of the POSTFIX_EXPRESSION must be complete. */
6354 if (scope == unknown_type_node)
6355 {
6356 error_at (location, "%qE does not have class type",
6357 postfix_expression);
6358 scope = NULL_TREE;
6359 }
6360 /* Unlike the object expression in other contexts, *this is not
6361 required to be of complete type for purposes of class member
6362 access (5.2.5) outside the member function body. */
6363 else if (postfix_expression != current_class_ref
6364 && !(processing_template_decl && scope == current_class_type))
6365 scope = complete_type_or_else (scope, NULL_TREE);
6366 /* Let the name lookup machinery know that we are processing a
6367 class member access expression. */
6368 parser->context->object_type = scope;
6369 /* If something went wrong, we want to be able to discern that case,
6370 as opposed to the case where there was no SCOPE due to the type
6371 of expression being dependent. */
6372 if (!scope)
6373 scope = error_mark_node;
6374 /* If the SCOPE was erroneous, make the various semantic analysis
6375 functions exit quickly -- and without issuing additional error
6376 messages. */
6377 if (scope == error_mark_node)
6378 postfix_expression = error_mark_node;
6379 }
6380
6381 /* Assume this expression is not a pseudo-destructor access. */
6382 pseudo_destructor_p = false;
6383
6384 /* If the SCOPE is a scalar type, then, if this is a valid program,
6385 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6386 is type dependent, it can be pseudo-destructor-name or something else.
6387 Try to parse it as pseudo-destructor-name first. */
6388 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6389 {
6390 tree s;
6391 tree type;
6392
6393 cp_parser_parse_tentatively (parser);
6394 /* Parse the pseudo-destructor-name. */
6395 s = NULL_TREE;
6396 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6397 &s, &type);
6398 if (dependent_p
6399 && (cp_parser_error_occurred (parser)
6400 || !SCALAR_TYPE_P (type)))
6401 cp_parser_abort_tentative_parse (parser);
6402 else if (cp_parser_parse_definitely (parser))
6403 {
6404 pseudo_destructor_p = true;
6405 postfix_expression
6406 = finish_pseudo_destructor_expr (postfix_expression,
6407 s, type, location);
6408 }
6409 }
6410
6411 if (!pseudo_destructor_p)
6412 {
6413 /* If the SCOPE is not a scalar type, we are looking at an
6414 ordinary class member access expression, rather than a
6415 pseudo-destructor-name. */
6416 bool template_p;
6417 cp_token *token = cp_lexer_peek_token (parser->lexer);
6418 /* Parse the id-expression. */
6419 name = (cp_parser_id_expression
6420 (parser,
6421 cp_parser_optional_template_keyword (parser),
6422 /*check_dependency_p=*/true,
6423 &template_p,
6424 /*declarator_p=*/false,
6425 /*optional_p=*/false));
6426 /* In general, build a SCOPE_REF if the member name is qualified.
6427 However, if the name was not dependent and has already been
6428 resolved; there is no need to build the SCOPE_REF. For example;
6429
6430 struct X { void f(); };
6431 template <typename T> void f(T* t) { t->X::f(); }
6432
6433 Even though "t" is dependent, "X::f" is not and has been resolved
6434 to a BASELINK; there is no need to include scope information. */
6435
6436 /* But we do need to remember that there was an explicit scope for
6437 virtual function calls. */
6438 if (parser->scope)
6439 *idk = CP_ID_KIND_QUALIFIED;
6440
6441 /* If the name is a template-id that names a type, we will get a
6442 TYPE_DECL here. That is invalid code. */
6443 if (TREE_CODE (name) == TYPE_DECL)
6444 {
6445 error_at (token->location, "invalid use of %qD", name);
6446 postfix_expression = error_mark_node;
6447 }
6448 else
6449 {
6450 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6451 {
6452 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6453 {
6454 error_at (token->location, "%<%D::%D%> is not a class member",
6455 parser->scope, name);
6456 postfix_expression = error_mark_node;
6457 }
6458 else
6459 name = build_qualified_name (/*type=*/NULL_TREE,
6460 parser->scope,
6461 name,
6462 template_p);
6463 parser->scope = NULL_TREE;
6464 parser->qualifying_scope = NULL_TREE;
6465 parser->object_scope = NULL_TREE;
6466 }
6467 if (parser->scope && name && BASELINK_P (name))
6468 adjust_result_of_qualified_name_lookup
6469 (name, parser->scope, scope);
6470 postfix_expression
6471 = finish_class_member_access_expr (postfix_expression, name,
6472 template_p,
6473 tf_warning_or_error);
6474 }
6475 }
6476
6477 /* We no longer need to look up names in the scope of the object on
6478 the left-hand side of the `.' or `->' operator. */
6479 parser->context->object_type = NULL_TREE;
6480
6481 /* Outside of offsetof, these operators may not appear in
6482 constant-expressions. */
6483 if (!for_offsetof
6484 && (cp_parser_non_integral_constant_expression
6485 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6486 postfix_expression = error_mark_node;
6487
6488 return postfix_expression;
6489 }
6490
6491 /* Parse a parenthesized expression-list.
6492
6493 expression-list:
6494 assignment-expression
6495 expression-list, assignment-expression
6496
6497 attribute-list:
6498 expression-list
6499 identifier
6500 identifier, expression-list
6501
6502 CAST_P is true if this expression is the target of a cast.
6503
6504 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6505 argument pack.
6506
6507 Returns a vector of trees. Each element is a representation of an
6508 assignment-expression. NULL is returned if the ( and or ) are
6509 missing. An empty, but allocated, vector is returned on no
6510 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6511 if we are parsing an attribute list for an attribute that wants a
6512 plain identifier argument, normal_attr for an attribute that wants
6513 an expression, or non_attr if we aren't parsing an attribute list. If
6514 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6515 not all of the expressions in the list were constant. */
6516
6517 static vec<tree, va_gc> *
6518 cp_parser_parenthesized_expression_list (cp_parser* parser,
6519 int is_attribute_list,
6520 bool cast_p,
6521 bool allow_expansion_p,
6522 bool *non_constant_p)
6523 {
6524 vec<tree, va_gc> *expression_list;
6525 bool fold_expr_p = is_attribute_list != non_attr;
6526 tree identifier = NULL_TREE;
6527 bool saved_greater_than_is_operator_p;
6528
6529 /* Assume all the expressions will be constant. */
6530 if (non_constant_p)
6531 *non_constant_p = false;
6532
6533 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6534 return NULL;
6535
6536 expression_list = make_tree_vector ();
6537
6538 /* Within a parenthesized expression, a `>' token is always
6539 the greater-than operator. */
6540 saved_greater_than_is_operator_p
6541 = parser->greater_than_is_operator_p;
6542 parser->greater_than_is_operator_p = true;
6543
6544 /* Consume expressions until there are no more. */
6545 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6546 while (true)
6547 {
6548 tree expr;
6549
6550 /* At the beginning of attribute lists, check to see if the
6551 next token is an identifier. */
6552 if (is_attribute_list == id_attr
6553 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6554 {
6555 cp_token *token;
6556
6557 /* Consume the identifier. */
6558 token = cp_lexer_consume_token (parser->lexer);
6559 /* Save the identifier. */
6560 identifier = token->u.value;
6561 }
6562 else
6563 {
6564 bool expr_non_constant_p;
6565
6566 /* Parse the next assignment-expression. */
6567 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6568 {
6569 /* A braced-init-list. */
6570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6571 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6572 if (non_constant_p && expr_non_constant_p)
6573 *non_constant_p = true;
6574 }
6575 else if (non_constant_p)
6576 {
6577 expr = (cp_parser_constant_expression
6578 (parser, /*allow_non_constant_p=*/true,
6579 &expr_non_constant_p));
6580 if (expr_non_constant_p)
6581 *non_constant_p = true;
6582 }
6583 else
6584 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6585
6586 if (fold_expr_p)
6587 expr = fold_non_dependent_expr (expr);
6588
6589 /* If we have an ellipsis, then this is an expression
6590 expansion. */
6591 if (allow_expansion_p
6592 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6593 {
6594 /* Consume the `...'. */
6595 cp_lexer_consume_token (parser->lexer);
6596
6597 /* Build the argument pack. */
6598 expr = make_pack_expansion (expr);
6599 }
6600
6601 /* Add it to the list. We add error_mark_node
6602 expressions to the list, so that we can still tell if
6603 the correct form for a parenthesized expression-list
6604 is found. That gives better errors. */
6605 vec_safe_push (expression_list, expr);
6606
6607 if (expr == error_mark_node)
6608 goto skip_comma;
6609 }
6610
6611 /* After the first item, attribute lists look the same as
6612 expression lists. */
6613 is_attribute_list = non_attr;
6614
6615 get_comma:;
6616 /* If the next token isn't a `,', then we are done. */
6617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6618 break;
6619
6620 /* Otherwise, consume the `,' and keep going. */
6621 cp_lexer_consume_token (parser->lexer);
6622 }
6623
6624 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6625 {
6626 int ending;
6627
6628 skip_comma:;
6629 /* We try and resync to an unnested comma, as that will give the
6630 user better diagnostics. */
6631 ending = cp_parser_skip_to_closing_parenthesis (parser,
6632 /*recovering=*/true,
6633 /*or_comma=*/true,
6634 /*consume_paren=*/true);
6635 if (ending < 0)
6636 goto get_comma;
6637 if (!ending)
6638 {
6639 parser->greater_than_is_operator_p
6640 = saved_greater_than_is_operator_p;
6641 return NULL;
6642 }
6643 }
6644
6645 parser->greater_than_is_operator_p
6646 = saved_greater_than_is_operator_p;
6647
6648 if (identifier)
6649 vec_safe_insert (expression_list, 0, identifier);
6650
6651 return expression_list;
6652 }
6653
6654 /* Parse a pseudo-destructor-name.
6655
6656 pseudo-destructor-name:
6657 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6658 :: [opt] nested-name-specifier template template-id :: ~ type-name
6659 :: [opt] nested-name-specifier [opt] ~ type-name
6660
6661 If either of the first two productions is used, sets *SCOPE to the
6662 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6663 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6664 or ERROR_MARK_NODE if the parse fails. */
6665
6666 static void
6667 cp_parser_pseudo_destructor_name (cp_parser* parser,
6668 tree object,
6669 tree* scope,
6670 tree* type)
6671 {
6672 bool nested_name_specifier_p;
6673
6674 /* Handle ~auto. */
6675 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6676 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6677 && !type_dependent_expression_p (object))
6678 {
6679 if (cxx_dialect < cxx1y)
6680 pedwarn (input_location, 0,
6681 "%<~auto%> only available with "
6682 "-std=c++1y or -std=gnu++1y");
6683 cp_lexer_consume_token (parser->lexer);
6684 cp_lexer_consume_token (parser->lexer);
6685 *scope = NULL_TREE;
6686 *type = TREE_TYPE (object);
6687 return;
6688 }
6689
6690 /* Assume that things will not work out. */
6691 *type = error_mark_node;
6692
6693 /* Look for the optional `::' operator. */
6694 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6695 /* Look for the optional nested-name-specifier. */
6696 nested_name_specifier_p
6697 = (cp_parser_nested_name_specifier_opt (parser,
6698 /*typename_keyword_p=*/false,
6699 /*check_dependency_p=*/true,
6700 /*type_p=*/false,
6701 /*is_declaration=*/false)
6702 != NULL_TREE);
6703 /* Now, if we saw a nested-name-specifier, we might be doing the
6704 second production. */
6705 if (nested_name_specifier_p
6706 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6707 {
6708 /* Consume the `template' keyword. */
6709 cp_lexer_consume_token (parser->lexer);
6710 /* Parse the template-id. */
6711 cp_parser_template_id (parser,
6712 /*template_keyword_p=*/true,
6713 /*check_dependency_p=*/false,
6714 class_type,
6715 /*is_declaration=*/true);
6716 /* Look for the `::' token. */
6717 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6718 }
6719 /* If the next token is not a `~', then there might be some
6720 additional qualification. */
6721 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6722 {
6723 /* At this point, we're looking for "type-name :: ~". The type-name
6724 must not be a class-name, since this is a pseudo-destructor. So,
6725 it must be either an enum-name, or a typedef-name -- both of which
6726 are just identifiers. So, we peek ahead to check that the "::"
6727 and "~" tokens are present; if they are not, then we can avoid
6728 calling type_name. */
6729 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6730 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6731 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6732 {
6733 cp_parser_error (parser, "non-scalar type");
6734 return;
6735 }
6736
6737 /* Look for the type-name. */
6738 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6739 if (*scope == error_mark_node)
6740 return;
6741
6742 /* Look for the `::' token. */
6743 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6744 }
6745 else
6746 *scope = NULL_TREE;
6747
6748 /* Look for the `~'. */
6749 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6750
6751 /* Once we see the ~, this has to be a pseudo-destructor. */
6752 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6753 cp_parser_commit_to_topmost_tentative_parse (parser);
6754
6755 /* Look for the type-name again. We are not responsible for
6756 checking that it matches the first type-name. */
6757 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6758 }
6759
6760 /* Parse a unary-expression.
6761
6762 unary-expression:
6763 postfix-expression
6764 ++ cast-expression
6765 -- cast-expression
6766 unary-operator cast-expression
6767 sizeof unary-expression
6768 sizeof ( type-id )
6769 alignof ( type-id ) [C++0x]
6770 new-expression
6771 delete-expression
6772
6773 GNU Extensions:
6774
6775 unary-expression:
6776 __extension__ cast-expression
6777 __alignof__ unary-expression
6778 __alignof__ ( type-id )
6779 alignof unary-expression [C++0x]
6780 __real__ cast-expression
6781 __imag__ cast-expression
6782 && identifier
6783 sizeof ( type-id ) { initializer-list , [opt] }
6784 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6785 __alignof__ ( type-id ) { initializer-list , [opt] }
6786
6787 ADDRESS_P is true iff the unary-expression is appearing as the
6788 operand of the `&' operator. CAST_P is true if this expression is
6789 the target of a cast.
6790
6791 Returns a representation of the expression. */
6792
6793 static tree
6794 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6795 bool decltype_p, cp_id_kind * pidk)
6796 {
6797 cp_token *token;
6798 enum tree_code unary_operator;
6799
6800 /* Peek at the next token. */
6801 token = cp_lexer_peek_token (parser->lexer);
6802 /* Some keywords give away the kind of expression. */
6803 if (token->type == CPP_KEYWORD)
6804 {
6805 enum rid keyword = token->keyword;
6806
6807 switch (keyword)
6808 {
6809 case RID_ALIGNOF:
6810 case RID_SIZEOF:
6811 {
6812 tree operand, ret;
6813 enum tree_code op;
6814 location_t first_loc;
6815
6816 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6817 /* Consume the token. */
6818 cp_lexer_consume_token (parser->lexer);
6819 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6820 /* Parse the operand. */
6821 operand = cp_parser_sizeof_operand (parser, keyword);
6822
6823 if (TYPE_P (operand))
6824 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6825 else
6826 {
6827 /* ISO C++ defines alignof only with types, not with
6828 expressions. So pedwarn if alignof is used with a non-
6829 type expression. However, __alignof__ is ok. */
6830 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6831 pedwarn (token->location, OPT_Wpedantic,
6832 "ISO C++ does not allow %<alignof%> "
6833 "with a non-type");
6834
6835 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6836 }
6837 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6838 SIZEOF_EXPR with the original operand. */
6839 if (op == SIZEOF_EXPR && ret != error_mark_node)
6840 {
6841 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6842 {
6843 if (!processing_template_decl && TYPE_P (operand))
6844 {
6845 ret = build_min (SIZEOF_EXPR, size_type_node,
6846 build1 (NOP_EXPR, operand,
6847 error_mark_node));
6848 SIZEOF_EXPR_TYPE_P (ret) = 1;
6849 }
6850 else
6851 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6852 TREE_SIDE_EFFECTS (ret) = 0;
6853 TREE_READONLY (ret) = 1;
6854 }
6855 SET_EXPR_LOCATION (ret, first_loc);
6856 }
6857 return ret;
6858 }
6859
6860 case RID_NEW:
6861 return cp_parser_new_expression (parser);
6862
6863 case RID_DELETE:
6864 return cp_parser_delete_expression (parser);
6865
6866 case RID_EXTENSION:
6867 {
6868 /* The saved value of the PEDANTIC flag. */
6869 int saved_pedantic;
6870 tree expr;
6871
6872 /* Save away the PEDANTIC flag. */
6873 cp_parser_extension_opt (parser, &saved_pedantic);
6874 /* Parse the cast-expression. */
6875 expr = cp_parser_simple_cast_expression (parser);
6876 /* Restore the PEDANTIC flag. */
6877 pedantic = saved_pedantic;
6878
6879 return expr;
6880 }
6881
6882 case RID_REALPART:
6883 case RID_IMAGPART:
6884 {
6885 tree expression;
6886
6887 /* Consume the `__real__' or `__imag__' token. */
6888 cp_lexer_consume_token (parser->lexer);
6889 /* Parse the cast-expression. */
6890 expression = cp_parser_simple_cast_expression (parser);
6891 /* Create the complete representation. */
6892 return build_x_unary_op (token->location,
6893 (keyword == RID_REALPART
6894 ? REALPART_EXPR : IMAGPART_EXPR),
6895 expression,
6896 tf_warning_or_error);
6897 }
6898 break;
6899
6900 case RID_TRANSACTION_ATOMIC:
6901 case RID_TRANSACTION_RELAXED:
6902 return cp_parser_transaction_expression (parser, keyword);
6903
6904 case RID_NOEXCEPT:
6905 {
6906 tree expr;
6907 const char *saved_message;
6908 bool saved_integral_constant_expression_p;
6909 bool saved_non_integral_constant_expression_p;
6910 bool saved_greater_than_is_operator_p;
6911
6912 cp_lexer_consume_token (parser->lexer);
6913 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6914
6915 saved_message = parser->type_definition_forbidden_message;
6916 parser->type_definition_forbidden_message
6917 = G_("types may not be defined in %<noexcept%> expressions");
6918
6919 saved_integral_constant_expression_p
6920 = parser->integral_constant_expression_p;
6921 saved_non_integral_constant_expression_p
6922 = parser->non_integral_constant_expression_p;
6923 parser->integral_constant_expression_p = false;
6924
6925 saved_greater_than_is_operator_p
6926 = parser->greater_than_is_operator_p;
6927 parser->greater_than_is_operator_p = true;
6928
6929 ++cp_unevaluated_operand;
6930 ++c_inhibit_evaluation_warnings;
6931 expr = cp_parser_expression (parser, false, NULL);
6932 --c_inhibit_evaluation_warnings;
6933 --cp_unevaluated_operand;
6934
6935 parser->greater_than_is_operator_p
6936 = saved_greater_than_is_operator_p;
6937
6938 parser->integral_constant_expression_p
6939 = saved_integral_constant_expression_p;
6940 parser->non_integral_constant_expression_p
6941 = saved_non_integral_constant_expression_p;
6942
6943 parser->type_definition_forbidden_message = saved_message;
6944
6945 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6946 return finish_noexcept_expr (expr, tf_warning_or_error);
6947 }
6948
6949 default:
6950 break;
6951 }
6952 }
6953
6954 /* Look for the `:: new' and `:: delete', which also signal the
6955 beginning of a new-expression, or delete-expression,
6956 respectively. If the next token is `::', then it might be one of
6957 these. */
6958 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6959 {
6960 enum rid keyword;
6961
6962 /* See if the token after the `::' is one of the keywords in
6963 which we're interested. */
6964 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6965 /* If it's `new', we have a new-expression. */
6966 if (keyword == RID_NEW)
6967 return cp_parser_new_expression (parser);
6968 /* Similarly, for `delete'. */
6969 else if (keyword == RID_DELETE)
6970 return cp_parser_delete_expression (parser);
6971 }
6972
6973 /* Look for a unary operator. */
6974 unary_operator = cp_parser_unary_operator (token);
6975 /* The `++' and `--' operators can be handled similarly, even though
6976 they are not technically unary-operators in the grammar. */
6977 if (unary_operator == ERROR_MARK)
6978 {
6979 if (token->type == CPP_PLUS_PLUS)
6980 unary_operator = PREINCREMENT_EXPR;
6981 else if (token->type == CPP_MINUS_MINUS)
6982 unary_operator = PREDECREMENT_EXPR;
6983 /* Handle the GNU address-of-label extension. */
6984 else if (cp_parser_allow_gnu_extensions_p (parser)
6985 && token->type == CPP_AND_AND)
6986 {
6987 tree identifier;
6988 tree expression;
6989 location_t loc = token->location;
6990
6991 /* Consume the '&&' token. */
6992 cp_lexer_consume_token (parser->lexer);
6993 /* Look for the identifier. */
6994 identifier = cp_parser_identifier (parser);
6995 /* Create an expression representing the address. */
6996 expression = finish_label_address_expr (identifier, loc);
6997 if (cp_parser_non_integral_constant_expression (parser,
6998 NIC_ADDR_LABEL))
6999 expression = error_mark_node;
7000 return expression;
7001 }
7002 }
7003 if (unary_operator != ERROR_MARK)
7004 {
7005 tree cast_expression;
7006 tree expression = error_mark_node;
7007 non_integral_constant non_constant_p = NIC_NONE;
7008 location_t loc = token->location;
7009 tsubst_flags_t complain = complain_flags (decltype_p);
7010
7011 /* Consume the operator token. */
7012 token = cp_lexer_consume_token (parser->lexer);
7013 /* Parse the cast-expression. */
7014 cast_expression
7015 = cp_parser_cast_expression (parser,
7016 unary_operator == ADDR_EXPR,
7017 /*cast_p=*/false,
7018 /*decltype*/false,
7019 pidk);
7020 /* Now, build an appropriate representation. */
7021 switch (unary_operator)
7022 {
7023 case INDIRECT_REF:
7024 non_constant_p = NIC_STAR;
7025 expression = build_x_indirect_ref (loc, cast_expression,
7026 RO_UNARY_STAR,
7027 complain);
7028 break;
7029
7030 case ADDR_EXPR:
7031 non_constant_p = NIC_ADDR;
7032 /* Fall through. */
7033 case BIT_NOT_EXPR:
7034 expression = build_x_unary_op (loc, unary_operator,
7035 cast_expression,
7036 complain);
7037 break;
7038
7039 case PREINCREMENT_EXPR:
7040 case PREDECREMENT_EXPR:
7041 non_constant_p = unary_operator == PREINCREMENT_EXPR
7042 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7043 /* Fall through. */
7044 case UNARY_PLUS_EXPR:
7045 case NEGATE_EXPR:
7046 case TRUTH_NOT_EXPR:
7047 expression = finish_unary_op_expr (loc, unary_operator,
7048 cast_expression, complain);
7049 break;
7050
7051 default:
7052 gcc_unreachable ();
7053 }
7054
7055 if (non_constant_p != NIC_NONE
7056 && cp_parser_non_integral_constant_expression (parser,
7057 non_constant_p))
7058 expression = error_mark_node;
7059
7060 return expression;
7061 }
7062
7063 return cp_parser_postfix_expression (parser, address_p, cast_p,
7064 /*member_access_only_p=*/false,
7065 decltype_p,
7066 pidk);
7067 }
7068
7069 static inline tree
7070 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7071 cp_id_kind * pidk)
7072 {
7073 return cp_parser_unary_expression (parser, address_p, cast_p,
7074 /*decltype*/false, pidk);
7075 }
7076
7077 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7078 unary-operator, the corresponding tree code is returned. */
7079
7080 static enum tree_code
7081 cp_parser_unary_operator (cp_token* token)
7082 {
7083 switch (token->type)
7084 {
7085 case CPP_MULT:
7086 return INDIRECT_REF;
7087
7088 case CPP_AND:
7089 return ADDR_EXPR;
7090
7091 case CPP_PLUS:
7092 return UNARY_PLUS_EXPR;
7093
7094 case CPP_MINUS:
7095 return NEGATE_EXPR;
7096
7097 case CPP_NOT:
7098 return TRUTH_NOT_EXPR;
7099
7100 case CPP_COMPL:
7101 return BIT_NOT_EXPR;
7102
7103 default:
7104 return ERROR_MARK;
7105 }
7106 }
7107
7108 /* Parse a new-expression.
7109
7110 new-expression:
7111 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7112 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7113
7114 Returns a representation of the expression. */
7115
7116 static tree
7117 cp_parser_new_expression (cp_parser* parser)
7118 {
7119 bool global_scope_p;
7120 vec<tree, va_gc> *placement;
7121 tree type;
7122 vec<tree, va_gc> *initializer;
7123 tree nelts = NULL_TREE;
7124 tree ret;
7125
7126 /* Look for the optional `::' operator. */
7127 global_scope_p
7128 = (cp_parser_global_scope_opt (parser,
7129 /*current_scope_valid_p=*/false)
7130 != NULL_TREE);
7131 /* Look for the `new' operator. */
7132 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7133 /* There's no easy way to tell a new-placement from the
7134 `( type-id )' construct. */
7135 cp_parser_parse_tentatively (parser);
7136 /* Look for a new-placement. */
7137 placement = cp_parser_new_placement (parser);
7138 /* If that didn't work out, there's no new-placement. */
7139 if (!cp_parser_parse_definitely (parser))
7140 {
7141 if (placement != NULL)
7142 release_tree_vector (placement);
7143 placement = NULL;
7144 }
7145
7146 /* If the next token is a `(', then we have a parenthesized
7147 type-id. */
7148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7149 {
7150 cp_token *token;
7151 const char *saved_message = parser->type_definition_forbidden_message;
7152
7153 /* Consume the `('. */
7154 cp_lexer_consume_token (parser->lexer);
7155
7156 /* Parse the type-id. */
7157 parser->type_definition_forbidden_message
7158 = G_("types may not be defined in a new-expression");
7159 type = cp_parser_type_id (parser);
7160 parser->type_definition_forbidden_message = saved_message;
7161
7162 /* Look for the closing `)'. */
7163 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7164 token = cp_lexer_peek_token (parser->lexer);
7165 /* There should not be a direct-new-declarator in this production,
7166 but GCC used to allowed this, so we check and emit a sensible error
7167 message for this case. */
7168 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7169 {
7170 error_at (token->location,
7171 "array bound forbidden after parenthesized type-id");
7172 inform (token->location,
7173 "try removing the parentheses around the type-id");
7174 cp_parser_direct_new_declarator (parser);
7175 }
7176 }
7177 /* Otherwise, there must be a new-type-id. */
7178 else
7179 type = cp_parser_new_type_id (parser, &nelts);
7180
7181 /* If the next token is a `(' or '{', then we have a new-initializer. */
7182 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7183 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7184 initializer = cp_parser_new_initializer (parser);
7185 else
7186 initializer = NULL;
7187
7188 /* A new-expression may not appear in an integral constant
7189 expression. */
7190 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7191 ret = error_mark_node;
7192 else
7193 {
7194 /* Create a representation of the new-expression. */
7195 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7196 tf_warning_or_error);
7197 }
7198
7199 if (placement != NULL)
7200 release_tree_vector (placement);
7201 if (initializer != NULL)
7202 release_tree_vector (initializer);
7203
7204 return ret;
7205 }
7206
7207 /* Parse a new-placement.
7208
7209 new-placement:
7210 ( expression-list )
7211
7212 Returns the same representation as for an expression-list. */
7213
7214 static vec<tree, va_gc> *
7215 cp_parser_new_placement (cp_parser* parser)
7216 {
7217 vec<tree, va_gc> *expression_list;
7218
7219 /* Parse the expression-list. */
7220 expression_list = (cp_parser_parenthesized_expression_list
7221 (parser, non_attr, /*cast_p=*/false,
7222 /*allow_expansion_p=*/true,
7223 /*non_constant_p=*/NULL));
7224
7225 return expression_list;
7226 }
7227
7228 /* Parse a new-type-id.
7229
7230 new-type-id:
7231 type-specifier-seq new-declarator [opt]
7232
7233 Returns the TYPE allocated. If the new-type-id indicates an array
7234 type, *NELTS is set to the number of elements in the last array
7235 bound; the TYPE will not include the last array bound. */
7236
7237 static tree
7238 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7239 {
7240 cp_decl_specifier_seq type_specifier_seq;
7241 cp_declarator *new_declarator;
7242 cp_declarator *declarator;
7243 cp_declarator *outer_declarator;
7244 const char *saved_message;
7245
7246 /* The type-specifier sequence must not contain type definitions.
7247 (It cannot contain declarations of new types either, but if they
7248 are not definitions we will catch that because they are not
7249 complete.) */
7250 saved_message = parser->type_definition_forbidden_message;
7251 parser->type_definition_forbidden_message
7252 = G_("types may not be defined in a new-type-id");
7253 /* Parse the type-specifier-seq. */
7254 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7255 /*is_trailing_return=*/false,
7256 &type_specifier_seq);
7257 /* Restore the old message. */
7258 parser->type_definition_forbidden_message = saved_message;
7259
7260 if (type_specifier_seq.type == error_mark_node)
7261 return error_mark_node;
7262
7263 /* Parse the new-declarator. */
7264 new_declarator = cp_parser_new_declarator_opt (parser);
7265
7266 /* Determine the number of elements in the last array dimension, if
7267 any. */
7268 *nelts = NULL_TREE;
7269 /* Skip down to the last array dimension. */
7270 declarator = new_declarator;
7271 outer_declarator = NULL;
7272 while (declarator && (declarator->kind == cdk_pointer
7273 || declarator->kind == cdk_ptrmem))
7274 {
7275 outer_declarator = declarator;
7276 declarator = declarator->declarator;
7277 }
7278 while (declarator
7279 && declarator->kind == cdk_array
7280 && declarator->declarator
7281 && declarator->declarator->kind == cdk_array)
7282 {
7283 outer_declarator = declarator;
7284 declarator = declarator->declarator;
7285 }
7286
7287 if (declarator && declarator->kind == cdk_array)
7288 {
7289 *nelts = declarator->u.array.bounds;
7290 if (*nelts == error_mark_node)
7291 *nelts = integer_one_node;
7292
7293 if (outer_declarator)
7294 outer_declarator->declarator = declarator->declarator;
7295 else
7296 new_declarator = NULL;
7297 }
7298
7299 return groktypename (&type_specifier_seq, new_declarator, false);
7300 }
7301
7302 /* Parse an (optional) new-declarator.
7303
7304 new-declarator:
7305 ptr-operator new-declarator [opt]
7306 direct-new-declarator
7307
7308 Returns the declarator. */
7309
7310 static cp_declarator *
7311 cp_parser_new_declarator_opt (cp_parser* parser)
7312 {
7313 enum tree_code code;
7314 tree type, std_attributes = NULL_TREE;
7315 cp_cv_quals cv_quals;
7316
7317 /* We don't know if there's a ptr-operator next, or not. */
7318 cp_parser_parse_tentatively (parser);
7319 /* Look for a ptr-operator. */
7320 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7321 /* If that worked, look for more new-declarators. */
7322 if (cp_parser_parse_definitely (parser))
7323 {
7324 cp_declarator *declarator;
7325
7326 /* Parse another optional declarator. */
7327 declarator = cp_parser_new_declarator_opt (parser);
7328
7329 declarator = cp_parser_make_indirect_declarator
7330 (code, type, cv_quals, declarator, std_attributes);
7331
7332 return declarator;
7333 }
7334
7335 /* If the next token is a `[', there is a direct-new-declarator. */
7336 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7337 return cp_parser_direct_new_declarator (parser);
7338
7339 return NULL;
7340 }
7341
7342 /* Parse a direct-new-declarator.
7343
7344 direct-new-declarator:
7345 [ expression ]
7346 direct-new-declarator [constant-expression]
7347
7348 */
7349
7350 static cp_declarator *
7351 cp_parser_direct_new_declarator (cp_parser* parser)
7352 {
7353 cp_declarator *declarator = NULL;
7354
7355 while (true)
7356 {
7357 tree expression;
7358 cp_token *token;
7359
7360 /* Look for the opening `['. */
7361 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7362
7363 token = cp_lexer_peek_token (parser->lexer);
7364 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7365 /* The standard requires that the expression have integral
7366 type. DR 74 adds enumeration types. We believe that the
7367 real intent is that these expressions be handled like the
7368 expression in a `switch' condition, which also allows
7369 classes with a single conversion to integral or
7370 enumeration type. */
7371 if (!processing_template_decl)
7372 {
7373 expression
7374 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7375 expression,
7376 /*complain=*/true);
7377 if (!expression)
7378 {
7379 error_at (token->location,
7380 "expression in new-declarator must have integral "
7381 "or enumeration type");
7382 expression = error_mark_node;
7383 }
7384 }
7385
7386 /* Look for the closing `]'. */
7387 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7388
7389 /* Add this bound to the declarator. */
7390 declarator = make_array_declarator (declarator, expression);
7391
7392 /* If the next token is not a `[', then there are no more
7393 bounds. */
7394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7395 break;
7396 }
7397
7398 return declarator;
7399 }
7400
7401 /* Parse a new-initializer.
7402
7403 new-initializer:
7404 ( expression-list [opt] )
7405 braced-init-list
7406
7407 Returns a representation of the expression-list. */
7408
7409 static vec<tree, va_gc> *
7410 cp_parser_new_initializer (cp_parser* parser)
7411 {
7412 vec<tree, va_gc> *expression_list;
7413
7414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7415 {
7416 tree t;
7417 bool expr_non_constant_p;
7418 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7419 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7420 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7421 expression_list = make_tree_vector_single (t);
7422 }
7423 else
7424 expression_list = (cp_parser_parenthesized_expression_list
7425 (parser, non_attr, /*cast_p=*/false,
7426 /*allow_expansion_p=*/true,
7427 /*non_constant_p=*/NULL));
7428
7429 return expression_list;
7430 }
7431
7432 /* Parse a delete-expression.
7433
7434 delete-expression:
7435 :: [opt] delete cast-expression
7436 :: [opt] delete [ ] cast-expression
7437
7438 Returns a representation of the expression. */
7439
7440 static tree
7441 cp_parser_delete_expression (cp_parser* parser)
7442 {
7443 bool global_scope_p;
7444 bool array_p;
7445 tree expression;
7446
7447 /* Look for the optional `::' operator. */
7448 global_scope_p
7449 = (cp_parser_global_scope_opt (parser,
7450 /*current_scope_valid_p=*/false)
7451 != NULL_TREE);
7452 /* Look for the `delete' keyword. */
7453 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7454 /* See if the array syntax is in use. */
7455 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7456 {
7457 /* Consume the `[' token. */
7458 cp_lexer_consume_token (parser->lexer);
7459 /* Look for the `]' token. */
7460 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7461 /* Remember that this is the `[]' construct. */
7462 array_p = true;
7463 }
7464 else
7465 array_p = false;
7466
7467 /* Parse the cast-expression. */
7468 expression = cp_parser_simple_cast_expression (parser);
7469
7470 /* A delete-expression may not appear in an integral constant
7471 expression. */
7472 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7473 return error_mark_node;
7474
7475 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7476 tf_warning_or_error);
7477 }
7478
7479 /* Returns true if TOKEN may start a cast-expression and false
7480 otherwise. */
7481
7482 static bool
7483 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7484 {
7485 cp_token *token = cp_lexer_peek_token (parser->lexer);
7486 switch (token->type)
7487 {
7488 case CPP_COMMA:
7489 case CPP_SEMICOLON:
7490 case CPP_QUERY:
7491 case CPP_COLON:
7492 case CPP_CLOSE_SQUARE:
7493 case CPP_CLOSE_PAREN:
7494 case CPP_CLOSE_BRACE:
7495 case CPP_DOT:
7496 case CPP_DOT_STAR:
7497 case CPP_DEREF:
7498 case CPP_DEREF_STAR:
7499 case CPP_DIV:
7500 case CPP_MOD:
7501 case CPP_LSHIFT:
7502 case CPP_RSHIFT:
7503 case CPP_LESS:
7504 case CPP_GREATER:
7505 case CPP_LESS_EQ:
7506 case CPP_GREATER_EQ:
7507 case CPP_EQ_EQ:
7508 case CPP_NOT_EQ:
7509 case CPP_EQ:
7510 case CPP_MULT_EQ:
7511 case CPP_DIV_EQ:
7512 case CPP_MOD_EQ:
7513 case CPP_PLUS_EQ:
7514 case CPP_MINUS_EQ:
7515 case CPP_RSHIFT_EQ:
7516 case CPP_LSHIFT_EQ:
7517 case CPP_AND_EQ:
7518 case CPP_XOR_EQ:
7519 case CPP_OR_EQ:
7520 case CPP_XOR:
7521 case CPP_OR:
7522 case CPP_OR_OR:
7523 case CPP_EOF:
7524 return false;
7525
7526 case CPP_OPEN_PAREN:
7527 /* In ((type ()) () the last () isn't a valid cast-expression,
7528 so the whole must be parsed as postfix-expression. */
7529 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7530 != CPP_CLOSE_PAREN;
7531
7532 /* '[' may start a primary-expression in obj-c++. */
7533 case CPP_OPEN_SQUARE:
7534 return c_dialect_objc ();
7535
7536 default:
7537 return true;
7538 }
7539 }
7540
7541 /* Parse a cast-expression.
7542
7543 cast-expression:
7544 unary-expression
7545 ( type-id ) cast-expression
7546
7547 ADDRESS_P is true iff the unary-expression is appearing as the
7548 operand of the `&' operator. CAST_P is true if this expression is
7549 the target of a cast.
7550
7551 Returns a representation of the expression. */
7552
7553 static tree
7554 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7555 bool decltype_p, cp_id_kind * pidk)
7556 {
7557 /* If it's a `(', then we might be looking at a cast. */
7558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7559 {
7560 tree type = NULL_TREE;
7561 tree expr = NULL_TREE;
7562 bool compound_literal_p;
7563 const char *saved_message;
7564
7565 /* There's no way to know yet whether or not this is a cast.
7566 For example, `(int (3))' is a unary-expression, while `(int)
7567 3' is a cast. So, we resort to parsing tentatively. */
7568 cp_parser_parse_tentatively (parser);
7569 /* Types may not be defined in a cast. */
7570 saved_message = parser->type_definition_forbidden_message;
7571 parser->type_definition_forbidden_message
7572 = G_("types may not be defined in casts");
7573 /* Consume the `('. */
7574 cp_lexer_consume_token (parser->lexer);
7575 /* A very tricky bit is that `(struct S) { 3 }' is a
7576 compound-literal (which we permit in C++ as an extension).
7577 But, that construct is not a cast-expression -- it is a
7578 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7579 is legal; if the compound-literal were a cast-expression,
7580 you'd need an extra set of parentheses.) But, if we parse
7581 the type-id, and it happens to be a class-specifier, then we
7582 will commit to the parse at that point, because we cannot
7583 undo the action that is done when creating a new class. So,
7584 then we cannot back up and do a postfix-expression.
7585
7586 Therefore, we scan ahead to the closing `)', and check to see
7587 if the token after the `)' is a `{'. If so, we are not
7588 looking at a cast-expression.
7589
7590 Save tokens so that we can put them back. */
7591 cp_lexer_save_tokens (parser->lexer);
7592 /* Skip tokens until the next token is a closing parenthesis.
7593 If we find the closing `)', and the next token is a `{', then
7594 we are looking at a compound-literal. */
7595 compound_literal_p
7596 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7597 /*consume_paren=*/true)
7598 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7599 /* Roll back the tokens we skipped. */
7600 cp_lexer_rollback_tokens (parser->lexer);
7601 /* If we were looking at a compound-literal, simulate an error
7602 so that the call to cp_parser_parse_definitely below will
7603 fail. */
7604 if (compound_literal_p)
7605 cp_parser_simulate_error (parser);
7606 else
7607 {
7608 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7609 parser->in_type_id_in_expr_p = true;
7610 /* Look for the type-id. */
7611 type = cp_parser_type_id (parser);
7612 /* Look for the closing `)'. */
7613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7614 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7615 }
7616
7617 /* Restore the saved message. */
7618 parser->type_definition_forbidden_message = saved_message;
7619
7620 /* At this point this can only be either a cast or a
7621 parenthesized ctor such as `(T ())' that looks like a cast to
7622 function returning T. */
7623 if (!cp_parser_error_occurred (parser)
7624 && cp_parser_tokens_start_cast_expression (parser))
7625 {
7626 cp_parser_parse_definitely (parser);
7627 expr = cp_parser_cast_expression (parser,
7628 /*address_p=*/false,
7629 /*cast_p=*/true,
7630 /*decltype_p=*/false,
7631 pidk);
7632
7633 /* Warn about old-style casts, if so requested. */
7634 if (warn_old_style_cast
7635 && !in_system_header
7636 && !VOID_TYPE_P (type)
7637 && current_lang_name != lang_name_c)
7638 warning (OPT_Wold_style_cast, "use of old-style cast");
7639
7640 /* Only type conversions to integral or enumeration types
7641 can be used in constant-expressions. */
7642 if (!cast_valid_in_integral_constant_expression_p (type)
7643 && cp_parser_non_integral_constant_expression (parser,
7644 NIC_CAST))
7645 return error_mark_node;
7646
7647 /* Perform the cast. */
7648 expr = build_c_cast (input_location, type, expr);
7649 return expr;
7650 }
7651 else
7652 cp_parser_abort_tentative_parse (parser);
7653 }
7654
7655 /* If we get here, then it's not a cast, so it must be a
7656 unary-expression. */
7657 return cp_parser_unary_expression (parser, address_p, cast_p,
7658 decltype_p, pidk);
7659 }
7660
7661 /* Parse a binary expression of the general form:
7662
7663 pm-expression:
7664 cast-expression
7665 pm-expression .* cast-expression
7666 pm-expression ->* cast-expression
7667
7668 multiplicative-expression:
7669 pm-expression
7670 multiplicative-expression * pm-expression
7671 multiplicative-expression / pm-expression
7672 multiplicative-expression % pm-expression
7673
7674 additive-expression:
7675 multiplicative-expression
7676 additive-expression + multiplicative-expression
7677 additive-expression - multiplicative-expression
7678
7679 shift-expression:
7680 additive-expression
7681 shift-expression << additive-expression
7682 shift-expression >> additive-expression
7683
7684 relational-expression:
7685 shift-expression
7686 relational-expression < shift-expression
7687 relational-expression > shift-expression
7688 relational-expression <= shift-expression
7689 relational-expression >= shift-expression
7690
7691 GNU Extension:
7692
7693 relational-expression:
7694 relational-expression <? shift-expression
7695 relational-expression >? shift-expression
7696
7697 equality-expression:
7698 relational-expression
7699 equality-expression == relational-expression
7700 equality-expression != relational-expression
7701
7702 and-expression:
7703 equality-expression
7704 and-expression & equality-expression
7705
7706 exclusive-or-expression:
7707 and-expression
7708 exclusive-or-expression ^ and-expression
7709
7710 inclusive-or-expression:
7711 exclusive-or-expression
7712 inclusive-or-expression | exclusive-or-expression
7713
7714 logical-and-expression:
7715 inclusive-or-expression
7716 logical-and-expression && inclusive-or-expression
7717
7718 logical-or-expression:
7719 logical-and-expression
7720 logical-or-expression || logical-and-expression
7721
7722 All these are implemented with a single function like:
7723
7724 binary-expression:
7725 simple-cast-expression
7726 binary-expression <token> binary-expression
7727
7728 CAST_P is true if this expression is the target of a cast.
7729
7730 The binops_by_token map is used to get the tree codes for each <token> type.
7731 binary-expressions are associated according to a precedence table. */
7732
7733 #define TOKEN_PRECEDENCE(token) \
7734 (((token->type == CPP_GREATER \
7735 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7736 && !parser->greater_than_is_operator_p) \
7737 ? PREC_NOT_OPERATOR \
7738 : binops_by_token[token->type].prec)
7739
7740 static tree
7741 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7742 bool no_toplevel_fold_p,
7743 bool decltype_p,
7744 enum cp_parser_prec prec,
7745 cp_id_kind * pidk)
7746 {
7747 cp_parser_expression_stack stack;
7748 cp_parser_expression_stack_entry *sp = &stack[0];
7749 cp_parser_expression_stack_entry current;
7750 tree rhs;
7751 cp_token *token;
7752 enum tree_code rhs_type;
7753 enum cp_parser_prec new_prec, lookahead_prec;
7754 tree overload;
7755
7756 /* Parse the first expression. */
7757 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7758 cast_p, decltype_p, pidk);
7759 current.lhs_type = ERROR_MARK;
7760 current.prec = prec;
7761
7762 if (cp_parser_error_occurred (parser))
7763 return error_mark_node;
7764
7765 for (;;)
7766 {
7767 /* Get an operator token. */
7768 token = cp_lexer_peek_token (parser->lexer);
7769
7770 if (warn_cxx0x_compat
7771 && token->type == CPP_RSHIFT
7772 && !parser->greater_than_is_operator_p)
7773 {
7774 if (warning_at (token->location, OPT_Wc__0x_compat,
7775 "%<>>%> operator is treated"
7776 " as two right angle brackets in C++11"))
7777 inform (token->location,
7778 "suggest parentheses around %<>>%> expression");
7779 }
7780
7781 new_prec = TOKEN_PRECEDENCE (token);
7782
7783 /* Popping an entry off the stack means we completed a subexpression:
7784 - either we found a token which is not an operator (`>' where it is not
7785 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7786 will happen repeatedly;
7787 - or, we found an operator which has lower priority. This is the case
7788 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7789 parsing `3 * 4'. */
7790 if (new_prec <= current.prec)
7791 {
7792 if (sp == stack)
7793 break;
7794 else
7795 goto pop;
7796 }
7797
7798 get_rhs:
7799 current.tree_type = binops_by_token[token->type].tree_type;
7800 current.loc = token->location;
7801
7802 /* We used the operator token. */
7803 cp_lexer_consume_token (parser->lexer);
7804
7805 /* For "false && x" or "true || x", x will never be executed;
7806 disable warnings while evaluating it. */
7807 if (current.tree_type == TRUTH_ANDIF_EXPR)
7808 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7809 else if (current.tree_type == TRUTH_ORIF_EXPR)
7810 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7811
7812 /* Extract another operand. It may be the RHS of this expression
7813 or the LHS of a new, higher priority expression. */
7814 rhs = cp_parser_simple_cast_expression (parser);
7815 rhs_type = ERROR_MARK;
7816
7817 /* Get another operator token. Look up its precedence to avoid
7818 building a useless (immediately popped) stack entry for common
7819 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7820 token = cp_lexer_peek_token (parser->lexer);
7821 lookahead_prec = TOKEN_PRECEDENCE (token);
7822 if (lookahead_prec > new_prec)
7823 {
7824 /* ... and prepare to parse the RHS of the new, higher priority
7825 expression. Since precedence levels on the stack are
7826 monotonically increasing, we do not have to care about
7827 stack overflows. */
7828 *sp = current;
7829 ++sp;
7830 current.lhs = rhs;
7831 current.lhs_type = rhs_type;
7832 current.prec = new_prec;
7833 new_prec = lookahead_prec;
7834 goto get_rhs;
7835
7836 pop:
7837 lookahead_prec = new_prec;
7838 /* If the stack is not empty, we have parsed into LHS the right side
7839 (`4' in the example above) of an expression we had suspended.
7840 We can use the information on the stack to recover the LHS (`3')
7841 from the stack together with the tree code (`MULT_EXPR'), and
7842 the precedence of the higher level subexpression
7843 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7844 which will be used to actually build the additive expression. */
7845 rhs = current.lhs;
7846 rhs_type = current.lhs_type;
7847 --sp;
7848 current = *sp;
7849 }
7850
7851 /* Undo the disabling of warnings done above. */
7852 if (current.tree_type == TRUTH_ANDIF_EXPR)
7853 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7854 else if (current.tree_type == TRUTH_ORIF_EXPR)
7855 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7856
7857 overload = NULL;
7858 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7859 ERROR_MARK for everything that is not a binary expression.
7860 This makes warn_about_parentheses miss some warnings that
7861 involve unary operators. For unary expressions we should
7862 pass the correct tree_code unless the unary expression was
7863 surrounded by parentheses.
7864 */
7865 if (no_toplevel_fold_p
7866 && lookahead_prec <= current.prec
7867 && sp == stack)
7868 current.lhs = build2 (current.tree_type,
7869 TREE_CODE_CLASS (current.tree_type)
7870 == tcc_comparison
7871 ? boolean_type_node : TREE_TYPE (current.lhs),
7872 current.lhs, rhs);
7873 else
7874 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7875 current.lhs, current.lhs_type,
7876 rhs, rhs_type, &overload,
7877 complain_flags (decltype_p));
7878 current.lhs_type = current.tree_type;
7879 if (EXPR_P (current.lhs))
7880 SET_EXPR_LOCATION (current.lhs, current.loc);
7881
7882 /* If the binary operator required the use of an overloaded operator,
7883 then this expression cannot be an integral constant-expression.
7884 An overloaded operator can be used even if both operands are
7885 otherwise permissible in an integral constant-expression if at
7886 least one of the operands is of enumeration type. */
7887
7888 if (overload
7889 && cp_parser_non_integral_constant_expression (parser,
7890 NIC_OVERLOADED))
7891 return error_mark_node;
7892 }
7893
7894 return current.lhs;
7895 }
7896
7897 static tree
7898 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7899 bool no_toplevel_fold_p,
7900 enum cp_parser_prec prec,
7901 cp_id_kind * pidk)
7902 {
7903 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7904 /*decltype*/false, prec, pidk);
7905 }
7906
7907 /* Parse the `? expression : assignment-expression' part of a
7908 conditional-expression. The LOGICAL_OR_EXPR is the
7909 logical-or-expression that started the conditional-expression.
7910 Returns a representation of the entire conditional-expression.
7911
7912 This routine is used by cp_parser_assignment_expression.
7913
7914 ? expression : assignment-expression
7915
7916 GNU Extensions:
7917
7918 ? : assignment-expression */
7919
7920 static tree
7921 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7922 {
7923 tree expr;
7924 tree assignment_expr;
7925 struct cp_token *token;
7926 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7927
7928 /* Consume the `?' token. */
7929 cp_lexer_consume_token (parser->lexer);
7930 token = cp_lexer_peek_token (parser->lexer);
7931 if (cp_parser_allow_gnu_extensions_p (parser)
7932 && token->type == CPP_COLON)
7933 {
7934 pedwarn (token->location, OPT_Wpedantic,
7935 "ISO C++ does not allow ?: with omitted middle operand");
7936 /* Implicit true clause. */
7937 expr = NULL_TREE;
7938 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7939 warn_for_omitted_condop (token->location, logical_or_expr);
7940 }
7941 else
7942 {
7943 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7944 parser->colon_corrects_to_scope_p = false;
7945 /* Parse the expression. */
7946 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7947 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7948 c_inhibit_evaluation_warnings +=
7949 ((logical_or_expr == truthvalue_true_node)
7950 - (logical_or_expr == truthvalue_false_node));
7951 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7952 }
7953
7954 /* The next token should be a `:'. */
7955 cp_parser_require (parser, CPP_COLON, RT_COLON);
7956 /* Parse the assignment-expression. */
7957 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7958 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7959
7960 /* Build the conditional-expression. */
7961 return build_x_conditional_expr (loc, logical_or_expr,
7962 expr,
7963 assignment_expr,
7964 tf_warning_or_error);
7965 }
7966
7967 /* Parse an assignment-expression.
7968
7969 assignment-expression:
7970 conditional-expression
7971 logical-or-expression assignment-operator assignment_expression
7972 throw-expression
7973
7974 CAST_P is true if this expression is the target of a cast.
7975 DECLTYPE_P is true if this expression is the operand of decltype.
7976
7977 Returns a representation for the expression. */
7978
7979 static tree
7980 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7981 bool decltype_p, cp_id_kind * pidk)
7982 {
7983 tree expr;
7984
7985 /* If the next token is the `throw' keyword, then we're looking at
7986 a throw-expression. */
7987 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7988 expr = cp_parser_throw_expression (parser);
7989 /* Otherwise, it must be that we are looking at a
7990 logical-or-expression. */
7991 else
7992 {
7993 /* Parse the binary expressions (logical-or-expression). */
7994 expr = cp_parser_binary_expression (parser, cast_p, false,
7995 decltype_p,
7996 PREC_NOT_OPERATOR, pidk);
7997 /* If the next token is a `?' then we're actually looking at a
7998 conditional-expression. */
7999 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8000 return cp_parser_question_colon_clause (parser, expr);
8001 else
8002 {
8003 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8004
8005 /* If it's an assignment-operator, we're using the second
8006 production. */
8007 enum tree_code assignment_operator
8008 = cp_parser_assignment_operator_opt (parser);
8009 if (assignment_operator != ERROR_MARK)
8010 {
8011 bool non_constant_p;
8012 location_t saved_input_location;
8013
8014 /* Parse the right-hand side of the assignment. */
8015 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8016
8017 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8018 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8019
8020 /* An assignment may not appear in a
8021 constant-expression. */
8022 if (cp_parser_non_integral_constant_expression (parser,
8023 NIC_ASSIGNMENT))
8024 return error_mark_node;
8025 /* Build the assignment expression. Its default
8026 location is the location of the '=' token. */
8027 saved_input_location = input_location;
8028 input_location = loc;
8029 expr = build_x_modify_expr (loc, expr,
8030 assignment_operator,
8031 rhs,
8032 complain_flags (decltype_p));
8033 input_location = saved_input_location;
8034 }
8035 }
8036 }
8037
8038 return expr;
8039 }
8040
8041 static tree
8042 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8043 cp_id_kind * pidk)
8044 {
8045 return cp_parser_assignment_expression (parser, cast_p,
8046 /*decltype*/false, pidk);
8047 }
8048
8049 /* Parse an (optional) assignment-operator.
8050
8051 assignment-operator: one of
8052 = *= /= %= += -= >>= <<= &= ^= |=
8053
8054 GNU Extension:
8055
8056 assignment-operator: one of
8057 <?= >?=
8058
8059 If the next token is an assignment operator, the corresponding tree
8060 code is returned, and the token is consumed. For example, for
8061 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8062 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8063 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8064 operator, ERROR_MARK is returned. */
8065
8066 static enum tree_code
8067 cp_parser_assignment_operator_opt (cp_parser* parser)
8068 {
8069 enum tree_code op;
8070 cp_token *token;
8071
8072 /* Peek at the next token. */
8073 token = cp_lexer_peek_token (parser->lexer);
8074
8075 switch (token->type)
8076 {
8077 case CPP_EQ:
8078 op = NOP_EXPR;
8079 break;
8080
8081 case CPP_MULT_EQ:
8082 op = MULT_EXPR;
8083 break;
8084
8085 case CPP_DIV_EQ:
8086 op = TRUNC_DIV_EXPR;
8087 break;
8088
8089 case CPP_MOD_EQ:
8090 op = TRUNC_MOD_EXPR;
8091 break;
8092
8093 case CPP_PLUS_EQ:
8094 op = PLUS_EXPR;
8095 break;
8096
8097 case CPP_MINUS_EQ:
8098 op = MINUS_EXPR;
8099 break;
8100
8101 case CPP_RSHIFT_EQ:
8102 op = RSHIFT_EXPR;
8103 break;
8104
8105 case CPP_LSHIFT_EQ:
8106 op = LSHIFT_EXPR;
8107 break;
8108
8109 case CPP_AND_EQ:
8110 op = BIT_AND_EXPR;
8111 break;
8112
8113 case CPP_XOR_EQ:
8114 op = BIT_XOR_EXPR;
8115 break;
8116
8117 case CPP_OR_EQ:
8118 op = BIT_IOR_EXPR;
8119 break;
8120
8121 default:
8122 /* Nothing else is an assignment operator. */
8123 op = ERROR_MARK;
8124 }
8125
8126 /* If it was an assignment operator, consume it. */
8127 if (op != ERROR_MARK)
8128 cp_lexer_consume_token (parser->lexer);
8129
8130 return op;
8131 }
8132
8133 /* Parse an expression.
8134
8135 expression:
8136 assignment-expression
8137 expression , assignment-expression
8138
8139 CAST_P is true if this expression is the target of a cast.
8140 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8141 except possibly parenthesized or on the RHS of a comma (N3276).
8142
8143 Returns a representation of the expression. */
8144
8145 static tree
8146 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8147 cp_id_kind * pidk)
8148 {
8149 tree expression = NULL_TREE;
8150 location_t loc = UNKNOWN_LOCATION;
8151
8152 while (true)
8153 {
8154 tree assignment_expression;
8155
8156 /* Parse the next assignment-expression. */
8157 assignment_expression
8158 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8159
8160 /* We don't create a temporary for a call that is the immediate operand
8161 of decltype or on the RHS of a comma. But when we see a comma, we
8162 need to create a temporary for a call on the LHS. */
8163 if (decltype_p && !processing_template_decl
8164 && TREE_CODE (assignment_expression) == CALL_EXPR
8165 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8166 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8167 assignment_expression
8168 = build_cplus_new (TREE_TYPE (assignment_expression),
8169 assignment_expression, tf_warning_or_error);
8170
8171 /* If this is the first assignment-expression, we can just
8172 save it away. */
8173 if (!expression)
8174 expression = assignment_expression;
8175 else
8176 expression = build_x_compound_expr (loc, expression,
8177 assignment_expression,
8178 complain_flags (decltype_p));
8179 /* If the next token is not a comma, then we are done with the
8180 expression. */
8181 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8182 break;
8183 /* Consume the `,'. */
8184 loc = cp_lexer_peek_token (parser->lexer)->location;
8185 cp_lexer_consume_token (parser->lexer);
8186 /* A comma operator cannot appear in a constant-expression. */
8187 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8188 expression = error_mark_node;
8189 }
8190
8191 return expression;
8192 }
8193
8194 static inline tree
8195 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8196 {
8197 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8198 }
8199
8200 /* Parse a constant-expression.
8201
8202 constant-expression:
8203 conditional-expression
8204
8205 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8206 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8207 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8208 is false, NON_CONSTANT_P should be NULL. */
8209
8210 static tree
8211 cp_parser_constant_expression (cp_parser* parser,
8212 bool allow_non_constant_p,
8213 bool *non_constant_p)
8214 {
8215 bool saved_integral_constant_expression_p;
8216 bool saved_allow_non_integral_constant_expression_p;
8217 bool saved_non_integral_constant_expression_p;
8218 tree expression;
8219
8220 /* It might seem that we could simply parse the
8221 conditional-expression, and then check to see if it were
8222 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8223 one that the compiler can figure out is constant, possibly after
8224 doing some simplifications or optimizations. The standard has a
8225 precise definition of constant-expression, and we must honor
8226 that, even though it is somewhat more restrictive.
8227
8228 For example:
8229
8230 int i[(2, 3)];
8231
8232 is not a legal declaration, because `(2, 3)' is not a
8233 constant-expression. The `,' operator is forbidden in a
8234 constant-expression. However, GCC's constant-folding machinery
8235 will fold this operation to an INTEGER_CST for `3'. */
8236
8237 /* Save the old settings. */
8238 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8239 saved_allow_non_integral_constant_expression_p
8240 = parser->allow_non_integral_constant_expression_p;
8241 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8242 /* We are now parsing a constant-expression. */
8243 parser->integral_constant_expression_p = true;
8244 parser->allow_non_integral_constant_expression_p
8245 = (allow_non_constant_p || cxx_dialect >= cxx11);
8246 parser->non_integral_constant_expression_p = false;
8247 /* Although the grammar says "conditional-expression", we parse an
8248 "assignment-expression", which also permits "throw-expression"
8249 and the use of assignment operators. In the case that
8250 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8251 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8252 actually essential that we look for an assignment-expression.
8253 For example, cp_parser_initializer_clauses uses this function to
8254 determine whether a particular assignment-expression is in fact
8255 constant. */
8256 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8257 /* Restore the old settings. */
8258 parser->integral_constant_expression_p
8259 = saved_integral_constant_expression_p;
8260 parser->allow_non_integral_constant_expression_p
8261 = saved_allow_non_integral_constant_expression_p;
8262 if (cxx_dialect >= cxx11)
8263 {
8264 /* Require an rvalue constant expression here; that's what our
8265 callers expect. Reference constant expressions are handled
8266 separately in e.g. cp_parser_template_argument. */
8267 bool is_const = potential_rvalue_constant_expression (expression);
8268 parser->non_integral_constant_expression_p = !is_const;
8269 if (!is_const && !allow_non_constant_p)
8270 require_potential_rvalue_constant_expression (expression);
8271 }
8272 if (allow_non_constant_p)
8273 *non_constant_p = parser->non_integral_constant_expression_p;
8274 parser->non_integral_constant_expression_p
8275 = saved_non_integral_constant_expression_p;
8276
8277 return expression;
8278 }
8279
8280 /* Parse __builtin_offsetof.
8281
8282 offsetof-expression:
8283 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8284
8285 offsetof-member-designator:
8286 id-expression
8287 | offsetof-member-designator "." id-expression
8288 | offsetof-member-designator "[" expression "]"
8289 | offsetof-member-designator "->" id-expression */
8290
8291 static tree
8292 cp_parser_builtin_offsetof (cp_parser *parser)
8293 {
8294 int save_ice_p, save_non_ice_p;
8295 tree type, expr;
8296 cp_id_kind dummy;
8297 cp_token *token;
8298
8299 /* We're about to accept non-integral-constant things, but will
8300 definitely yield an integral constant expression. Save and
8301 restore these values around our local parsing. */
8302 save_ice_p = parser->integral_constant_expression_p;
8303 save_non_ice_p = parser->non_integral_constant_expression_p;
8304
8305 /* Consume the "__builtin_offsetof" token. */
8306 cp_lexer_consume_token (parser->lexer);
8307 /* Consume the opening `('. */
8308 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8309 /* Parse the type-id. */
8310 type = cp_parser_type_id (parser);
8311 /* Look for the `,'. */
8312 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8313 token = cp_lexer_peek_token (parser->lexer);
8314
8315 /* Build the (type *)null that begins the traditional offsetof macro. */
8316 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8317 tf_warning_or_error);
8318
8319 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8320 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8321 true, &dummy, token->location);
8322 while (true)
8323 {
8324 token = cp_lexer_peek_token (parser->lexer);
8325 switch (token->type)
8326 {
8327 case CPP_OPEN_SQUARE:
8328 /* offsetof-member-designator "[" expression "]" */
8329 expr = cp_parser_postfix_open_square_expression (parser, expr,
8330 true, false);
8331 break;
8332
8333 case CPP_DEREF:
8334 /* offsetof-member-designator "->" identifier */
8335 expr = grok_array_decl (token->location, expr,
8336 integer_zero_node, false);
8337 /* FALLTHRU */
8338
8339 case CPP_DOT:
8340 /* offsetof-member-designator "." identifier */
8341 cp_lexer_consume_token (parser->lexer);
8342 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8343 expr, true, &dummy,
8344 token->location);
8345 break;
8346
8347 case CPP_CLOSE_PAREN:
8348 /* Consume the ")" token. */
8349 cp_lexer_consume_token (parser->lexer);
8350 goto success;
8351
8352 default:
8353 /* Error. We know the following require will fail, but
8354 that gives the proper error message. */
8355 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8356 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8357 expr = error_mark_node;
8358 goto failure;
8359 }
8360 }
8361
8362 success:
8363 /* If we're processing a template, we can't finish the semantics yet.
8364 Otherwise we can fold the entire expression now. */
8365 if (processing_template_decl)
8366 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8367 else
8368 expr = finish_offsetof (expr);
8369
8370 failure:
8371 parser->integral_constant_expression_p = save_ice_p;
8372 parser->non_integral_constant_expression_p = save_non_ice_p;
8373
8374 return expr;
8375 }
8376
8377 /* Parse a trait expression.
8378
8379 Returns a representation of the expression, the underlying type
8380 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8381
8382 static tree
8383 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8384 {
8385 cp_trait_kind kind;
8386 tree type1, type2 = NULL_TREE;
8387 bool binary = false;
8388 cp_decl_specifier_seq decl_specs;
8389
8390 switch (keyword)
8391 {
8392 case RID_HAS_NOTHROW_ASSIGN:
8393 kind = CPTK_HAS_NOTHROW_ASSIGN;
8394 break;
8395 case RID_HAS_NOTHROW_CONSTRUCTOR:
8396 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8397 break;
8398 case RID_HAS_NOTHROW_COPY:
8399 kind = CPTK_HAS_NOTHROW_COPY;
8400 break;
8401 case RID_HAS_TRIVIAL_ASSIGN:
8402 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8403 break;
8404 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8405 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8406 break;
8407 case RID_HAS_TRIVIAL_COPY:
8408 kind = CPTK_HAS_TRIVIAL_COPY;
8409 break;
8410 case RID_HAS_TRIVIAL_DESTRUCTOR:
8411 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8412 break;
8413 case RID_HAS_VIRTUAL_DESTRUCTOR:
8414 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8415 break;
8416 case RID_IS_ABSTRACT:
8417 kind = CPTK_IS_ABSTRACT;
8418 break;
8419 case RID_IS_BASE_OF:
8420 kind = CPTK_IS_BASE_OF;
8421 binary = true;
8422 break;
8423 case RID_IS_CLASS:
8424 kind = CPTK_IS_CLASS;
8425 break;
8426 case RID_IS_CONVERTIBLE_TO:
8427 kind = CPTK_IS_CONVERTIBLE_TO;
8428 binary = true;
8429 break;
8430 case RID_IS_EMPTY:
8431 kind = CPTK_IS_EMPTY;
8432 break;
8433 case RID_IS_ENUM:
8434 kind = CPTK_IS_ENUM;
8435 break;
8436 case RID_IS_FINAL:
8437 kind = CPTK_IS_FINAL;
8438 break;
8439 case RID_IS_LITERAL_TYPE:
8440 kind = CPTK_IS_LITERAL_TYPE;
8441 break;
8442 case RID_IS_POD:
8443 kind = CPTK_IS_POD;
8444 break;
8445 case RID_IS_POLYMORPHIC:
8446 kind = CPTK_IS_POLYMORPHIC;
8447 break;
8448 case RID_IS_STD_LAYOUT:
8449 kind = CPTK_IS_STD_LAYOUT;
8450 break;
8451 case RID_IS_TRIVIAL:
8452 kind = CPTK_IS_TRIVIAL;
8453 break;
8454 case RID_IS_UNION:
8455 kind = CPTK_IS_UNION;
8456 break;
8457 case RID_UNDERLYING_TYPE:
8458 kind = CPTK_UNDERLYING_TYPE;
8459 break;
8460 case RID_BASES:
8461 kind = CPTK_BASES;
8462 break;
8463 case RID_DIRECT_BASES:
8464 kind = CPTK_DIRECT_BASES;
8465 break;
8466 default:
8467 gcc_unreachable ();
8468 }
8469
8470 /* Consume the token. */
8471 cp_lexer_consume_token (parser->lexer);
8472
8473 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8474
8475 type1 = cp_parser_type_id (parser);
8476
8477 if (type1 == error_mark_node)
8478 return error_mark_node;
8479
8480 /* Build a trivial decl-specifier-seq. */
8481 clear_decl_specs (&decl_specs);
8482 decl_specs.type = type1;
8483
8484 /* Call grokdeclarator to figure out what type this is. */
8485 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8486 /*initialized=*/0, /*attrlist=*/NULL);
8487
8488 if (binary)
8489 {
8490 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8491
8492 type2 = cp_parser_type_id (parser);
8493
8494 if (type2 == error_mark_node)
8495 return error_mark_node;
8496
8497 /* Build a trivial decl-specifier-seq. */
8498 clear_decl_specs (&decl_specs);
8499 decl_specs.type = type2;
8500
8501 /* Call grokdeclarator to figure out what type this is. */
8502 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8503 /*initialized=*/0, /*attrlist=*/NULL);
8504 }
8505
8506 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8507
8508 /* Complete the trait expression, which may mean either processing
8509 the trait expr now or saving it for template instantiation. */
8510 switch(kind)
8511 {
8512 case CPTK_UNDERLYING_TYPE:
8513 return finish_underlying_type (type1);
8514 case CPTK_BASES:
8515 return finish_bases (type1, false);
8516 case CPTK_DIRECT_BASES:
8517 return finish_bases (type1, true);
8518 default:
8519 return finish_trait_expr (kind, type1, type2);
8520 }
8521 }
8522
8523 /* Lambdas that appear in variable initializer or default argument scope
8524 get that in their mangling, so we need to record it. We might as well
8525 use the count for function and namespace scopes as well. */
8526 static GTY(()) tree lambda_scope;
8527 static GTY(()) int lambda_count;
8528 typedef struct GTY(()) tree_int
8529 {
8530 tree t;
8531 int i;
8532 } tree_int;
8533 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8534
8535 static void
8536 start_lambda_scope (tree decl)
8537 {
8538 tree_int ti;
8539 gcc_assert (decl);
8540 /* Once we're inside a function, we ignore other scopes and just push
8541 the function again so that popping works properly. */
8542 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8543 decl = current_function_decl;
8544 ti.t = lambda_scope;
8545 ti.i = lambda_count;
8546 vec_safe_push (lambda_scope_stack, ti);
8547 if (lambda_scope != decl)
8548 {
8549 /* Don't reset the count if we're still in the same function. */
8550 lambda_scope = decl;
8551 lambda_count = 0;
8552 }
8553 }
8554
8555 static void
8556 record_lambda_scope (tree lambda)
8557 {
8558 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8559 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8560 }
8561
8562 static void
8563 finish_lambda_scope (void)
8564 {
8565 tree_int *p = &lambda_scope_stack->last ();
8566 if (lambda_scope != p->t)
8567 {
8568 lambda_scope = p->t;
8569 lambda_count = p->i;
8570 }
8571 lambda_scope_stack->pop ();
8572 }
8573
8574 /* Parse a lambda expression.
8575
8576 lambda-expression:
8577 lambda-introducer lambda-declarator [opt] compound-statement
8578
8579 Returns a representation of the expression. */
8580
8581 static tree
8582 cp_parser_lambda_expression (cp_parser* parser)
8583 {
8584 tree lambda_expr = build_lambda_expr ();
8585 tree type;
8586 bool ok;
8587
8588 LAMBDA_EXPR_LOCATION (lambda_expr)
8589 = cp_lexer_peek_token (parser->lexer)->location;
8590
8591 if (cp_unevaluated_operand)
8592 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8593 "lambda-expression in unevaluated context");
8594
8595 /* We may be in the middle of deferred access check. Disable
8596 it now. */
8597 push_deferring_access_checks (dk_no_deferred);
8598
8599 cp_parser_lambda_introducer (parser, lambda_expr);
8600
8601 type = begin_lambda_type (lambda_expr);
8602 if (type == error_mark_node)
8603 return error_mark_node;
8604
8605 record_lambda_scope (lambda_expr);
8606
8607 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8608 determine_visibility (TYPE_NAME (type));
8609
8610 /* Now that we've started the type, add the capture fields for any
8611 explicit captures. */
8612 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8613
8614 {
8615 /* Inside the class, surrounding template-parameter-lists do not apply. */
8616 unsigned int saved_num_template_parameter_lists
8617 = parser->num_template_parameter_lists;
8618 unsigned char in_statement = parser->in_statement;
8619 bool in_switch_statement_p = parser->in_switch_statement_p;
8620 bool fully_implicit_function_template_p = parser->fully_implicit_function_template_p;
8621
8622 parser->num_template_parameter_lists = 0;
8623 parser->in_statement = 0;
8624 parser->in_switch_statement_p = false;
8625 parser->fully_implicit_function_template_p = false;
8626
8627 /* By virtue of defining a local class, a lambda expression has access to
8628 the private variables of enclosing classes. */
8629
8630 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8631
8632 if (ok)
8633 cp_parser_lambda_body (parser, lambda_expr);
8634 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8635 cp_parser_skip_to_end_of_block_or_statement (parser);
8636
8637 /* The capture list was built up in reverse order; fix that now. */
8638 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8639 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8640
8641 if (ok)
8642 maybe_add_lambda_conv_op (type);
8643
8644 type = finish_struct (type, /*attributes=*/NULL_TREE);
8645
8646 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8647 parser->in_statement = in_statement;
8648 parser->in_switch_statement_p = in_switch_statement_p;
8649 parser->fully_implicit_function_template_p = fully_implicit_function_template_p;
8650 }
8651
8652 pop_deferring_access_checks ();
8653
8654 /* This field is only used during parsing of the lambda. */
8655 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8656
8657 /* This lambda shouldn't have any proxies left at this point. */
8658 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8659 /* And now that we're done, push proxies for an enclosing lambda. */
8660 insert_pending_capture_proxies ();
8661
8662 if (ok)
8663 return build_lambda_object (lambda_expr);
8664 else
8665 return error_mark_node;
8666 }
8667
8668 /* Parse the beginning of a lambda expression.
8669
8670 lambda-introducer:
8671 [ lambda-capture [opt] ]
8672
8673 LAMBDA_EXPR is the current representation of the lambda expression. */
8674
8675 static void
8676 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8677 {
8678 /* Need commas after the first capture. */
8679 bool first = true;
8680
8681 /* Eat the leading `['. */
8682 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8683
8684 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8685 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8686 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8687 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8688 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8689 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8690
8691 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8692 {
8693 cp_lexer_consume_token (parser->lexer);
8694 first = false;
8695 }
8696
8697 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8698 {
8699 cp_token* capture_token;
8700 tree capture_id;
8701 tree capture_init_expr;
8702 cp_id_kind idk = CP_ID_KIND_NONE;
8703 bool explicit_init_p = false;
8704
8705 enum capture_kind_type
8706 {
8707 BY_COPY,
8708 BY_REFERENCE
8709 };
8710 enum capture_kind_type capture_kind = BY_COPY;
8711
8712 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8713 {
8714 error ("expected end of capture-list");
8715 return;
8716 }
8717
8718 if (first)
8719 first = false;
8720 else
8721 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8722
8723 /* Possibly capture `this'. */
8724 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8725 {
8726 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8727 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8728 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8729 "with by-copy capture default");
8730 cp_lexer_consume_token (parser->lexer);
8731 add_capture (lambda_expr,
8732 /*id=*/this_identifier,
8733 /*initializer=*/finish_this_expr(),
8734 /*by_reference_p=*/false,
8735 explicit_init_p);
8736 continue;
8737 }
8738
8739 /* Remember whether we want to capture as a reference or not. */
8740 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8741 {
8742 capture_kind = BY_REFERENCE;
8743 cp_lexer_consume_token (parser->lexer);
8744 }
8745
8746 /* Get the identifier. */
8747 capture_token = cp_lexer_peek_token (parser->lexer);
8748 capture_id = cp_parser_identifier (parser);
8749
8750 if (capture_id == error_mark_node)
8751 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8752 delimiters, but I modified this to stop on unnested ']' as well. It
8753 was already changed to stop on unnested '}', so the
8754 "closing_parenthesis" name is no more misleading with my change. */
8755 {
8756 cp_parser_skip_to_closing_parenthesis (parser,
8757 /*recovering=*/true,
8758 /*or_comma=*/true,
8759 /*consume_paren=*/true);
8760 break;
8761 }
8762
8763 /* Find the initializer for this capture. */
8764 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8765 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8766 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8767 {
8768 bool direct, non_constant;
8769 /* An explicit initializer exists. */
8770 if (cxx_dialect < cxx1y)
8771 pedwarn (input_location, 0,
8772 "lambda capture initializers "
8773 "only available with -std=c++1y or -std=gnu++1y");
8774 capture_init_expr = cp_parser_initializer (parser, &direct,
8775 &non_constant);
8776 explicit_init_p = true;
8777 }
8778 else
8779 {
8780 const char* error_msg;
8781
8782 /* Turn the identifier into an id-expression. */
8783 capture_init_expr
8784 = cp_parser_lookup_name_simple (parser, capture_id,
8785 capture_token->location);
8786
8787 if (capture_init_expr == error_mark_node)
8788 {
8789 unqualified_name_lookup_error (capture_id);
8790 continue;
8791 }
8792 else if (DECL_P (capture_init_expr)
8793 && (!VAR_P (capture_init_expr)
8794 && TREE_CODE (capture_init_expr) != PARM_DECL))
8795 {
8796 error_at (capture_token->location,
8797 "capture of non-variable %qD ",
8798 capture_init_expr);
8799 inform (0, "%q+#D declared here", capture_init_expr);
8800 continue;
8801 }
8802 if (VAR_P (capture_init_expr)
8803 && decl_storage_duration (capture_init_expr) != dk_auto)
8804 {
8805 pedwarn (capture_token->location, 0, "capture of variable "
8806 "%qD with non-automatic storage duration",
8807 capture_init_expr);
8808 inform (0, "%q+#D declared here", capture_init_expr);
8809 continue;
8810 }
8811
8812 capture_init_expr
8813 = finish_id_expression
8814 (capture_id,
8815 capture_init_expr,
8816 parser->scope,
8817 &idk,
8818 /*integral_constant_expression_p=*/false,
8819 /*allow_non_integral_constant_expression_p=*/false,
8820 /*non_integral_constant_expression_p=*/NULL,
8821 /*template_p=*/false,
8822 /*done=*/true,
8823 /*address_p=*/false,
8824 /*template_arg_p=*/false,
8825 &error_msg,
8826 capture_token->location);
8827
8828 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8829 {
8830 cp_lexer_consume_token (parser->lexer);
8831 capture_init_expr = make_pack_expansion (capture_init_expr);
8832 }
8833 else
8834 check_for_bare_parameter_packs (capture_init_expr);
8835 }
8836
8837 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8838 && !explicit_init_p)
8839 {
8840 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8841 && capture_kind == BY_COPY)
8842 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8843 "of %qD redundant with by-copy capture default",
8844 capture_id);
8845 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8846 && capture_kind == BY_REFERENCE)
8847 pedwarn (capture_token->location, 0, "explicit by-reference "
8848 "capture of %qD redundant with by-reference capture "
8849 "default", capture_id);
8850 }
8851
8852 add_capture (lambda_expr,
8853 capture_id,
8854 capture_init_expr,
8855 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8856 explicit_init_p);
8857 }
8858
8859 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8860 }
8861
8862 /* Parse the (optional) middle of a lambda expression.
8863
8864 lambda-declarator:
8865 < template-parameter-list [opt] >
8866 ( parameter-declaration-clause [opt] )
8867 attribute-specifier [opt]
8868 mutable [opt]
8869 exception-specification [opt]
8870 lambda-return-type-clause [opt]
8871
8872 LAMBDA_EXPR is the current representation of the lambda expression. */
8873
8874 static bool
8875 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8876 {
8877 /* 5.1.1.4 of the standard says:
8878 If a lambda-expression does not include a lambda-declarator, it is as if
8879 the lambda-declarator were ().
8880 This means an empty parameter list, no attributes, and no exception
8881 specification. */
8882 tree param_list = void_list_node;
8883 tree attributes = NULL_TREE;
8884 tree exception_spec = NULL_TREE;
8885 tree template_param_list = NULL_TREE;
8886
8887 /* The template-parameter-list is optional, but must begin with
8888 an opening angle if present. */
8889 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8890 {
8891 if (cxx_dialect < cxx1y)
8892 pedwarn (parser->lexer->next_token->location, 0,
8893 "lambda templates are only available with "
8894 "-std=c++1y or -std=gnu++1y");
8895
8896 cp_lexer_consume_token (parser->lexer);
8897
8898 template_param_list = cp_parser_template_parameter_list (parser);
8899
8900 cp_parser_skip_to_end_of_template_parameter_list (parser);
8901
8902 /* We just processed one more parameter list. */
8903 ++parser->num_template_parameter_lists;
8904 }
8905
8906 /* The parameter-declaration-clause is optional (unless
8907 template-parameter-list was given), but must begin with an
8908 opening parenthesis if present. */
8909 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8910 {
8911 cp_lexer_consume_token (parser->lexer);
8912
8913 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8914
8915 /* Parse parameters. */
8916 param_list = cp_parser_parameter_declaration_clause (parser);
8917
8918 /* Default arguments shall not be specified in the
8919 parameter-declaration-clause of a lambda-declarator. */
8920 for (tree t = param_list; t; t = TREE_CHAIN (t))
8921 if (TREE_PURPOSE (t))
8922 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8923 "default argument specified for lambda parameter");
8924
8925 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8926
8927 attributes = cp_parser_attributes_opt (parser);
8928
8929 /* Parse optional `mutable' keyword. */
8930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8931 {
8932 cp_lexer_consume_token (parser->lexer);
8933 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8934 }
8935
8936 /* Parse optional exception specification. */
8937 exception_spec = cp_parser_exception_specification_opt (parser);
8938
8939 /* Parse optional trailing return type. */
8940 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8941 {
8942 cp_lexer_consume_token (parser->lexer);
8943 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8944 = cp_parser_trailing_type_id (parser);
8945 }
8946
8947 /* The function parameters must be in scope all the way until after the
8948 trailing-return-type in case of decltype. */
8949 pop_bindings_and_leave_scope ();
8950 }
8951 else if (template_param_list != NULL_TREE) // generate diagnostic
8952 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8953
8954 /* Create the function call operator.
8955
8956 Messing with declarators like this is no uglier than building up the
8957 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8958 other code. */
8959 {
8960 cp_decl_specifier_seq return_type_specs;
8961 cp_declarator* declarator;
8962 tree fco;
8963 int quals;
8964 void *p;
8965
8966 clear_decl_specs (&return_type_specs);
8967 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8968 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8969 else
8970 /* Maybe we will deduce the return type later. */
8971 return_type_specs.type = make_auto ();
8972
8973 p = obstack_alloc (&declarator_obstack, 0);
8974
8975 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8976 sfk_none);
8977
8978 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8979 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8980 declarator = make_call_declarator (declarator, param_list, quals,
8981 VIRT_SPEC_UNSPECIFIED,
8982 REF_QUAL_NONE,
8983 exception_spec,
8984 /*late_return_type=*/NULL_TREE);
8985 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8986
8987 fco = grokmethod (&return_type_specs,
8988 declarator,
8989 attributes);
8990 if (fco != error_mark_node)
8991 {
8992 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8993 DECL_ARTIFICIAL (fco) = 1;
8994 /* Give the object parameter a different name. */
8995 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8996 if (template_param_list)
8997 {
8998 fco = finish_member_template_decl (fco);
8999 finish_template_decl (template_param_list);
9000 --parser->num_template_parameter_lists;
9001 }
9002 else if (parser->fully_implicit_function_template_p)
9003 fco = finish_fully_implicit_template (parser, fco);
9004 }
9005
9006 finish_member_declaration (fco);
9007
9008 obstack_free (&declarator_obstack, p);
9009
9010 return (fco != error_mark_node);
9011 }
9012 }
9013
9014 /* Parse the body of a lambda expression, which is simply
9015
9016 compound-statement
9017
9018 but which requires special handling.
9019 LAMBDA_EXPR is the current representation of the lambda expression. */
9020
9021 static void
9022 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9023 {
9024 bool nested = (current_function_decl != NULL_TREE);
9025 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9026 if (nested)
9027 push_function_context ();
9028 else
9029 /* Still increment function_depth so that we don't GC in the
9030 middle of an expression. */
9031 ++function_depth;
9032 /* Clear this in case we're in the middle of a default argument. */
9033 parser->local_variables_forbidden_p = false;
9034
9035 /* Finish the function call operator
9036 - class_specifier
9037 + late_parsing_for_member
9038 + function_definition_after_declarator
9039 + ctor_initializer_opt_and_function_body */
9040 {
9041 tree fco = lambda_function (lambda_expr);
9042 tree body;
9043 bool done = false;
9044 tree compound_stmt;
9045 tree cap;
9046
9047 /* Let the front end know that we are going to be defining this
9048 function. */
9049 start_preparsed_function (fco,
9050 NULL_TREE,
9051 SF_PRE_PARSED | SF_INCLASS_INLINE);
9052
9053 start_lambda_scope (fco);
9054 body = begin_function_body ();
9055
9056 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9057 goto out;
9058
9059 /* Push the proxies for any explicit captures. */
9060 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9061 cap = TREE_CHAIN (cap))
9062 build_capture_proxy (TREE_PURPOSE (cap));
9063
9064 compound_stmt = begin_compound_stmt (0);
9065
9066 /* 5.1.1.4 of the standard says:
9067 If a lambda-expression does not include a trailing-return-type, it
9068 is as if the trailing-return-type denotes the following type:
9069 * if the compound-statement is of the form
9070 { return attribute-specifier [opt] expression ; }
9071 the type of the returned expression after lvalue-to-rvalue
9072 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9073 (_conv.array_ 4.2), and function-to-pointer conversion
9074 (_conv.func_ 4.3);
9075 * otherwise, void. */
9076
9077 /* In a lambda that has neither a lambda-return-type-clause
9078 nor a deducible form, errors should be reported for return statements
9079 in the body. Since we used void as the placeholder return type, parsing
9080 the body as usual will give such desired behavior. */
9081 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9082 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9083 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9084 {
9085 tree expr = NULL_TREE;
9086 cp_id_kind idk = CP_ID_KIND_NONE;
9087
9088 /* Parse tentatively in case there's more after the initial return
9089 statement. */
9090 cp_parser_parse_tentatively (parser);
9091
9092 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9093
9094 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9095
9096 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9097 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9098
9099 if (cp_parser_parse_definitely (parser))
9100 {
9101 if (!processing_template_decl)
9102 apply_deduced_return_type (fco, lambda_return_type (expr));
9103
9104 /* Will get error here if type not deduced yet. */
9105 finish_return_stmt (expr);
9106
9107 done = true;
9108 }
9109 }
9110
9111 if (!done)
9112 {
9113 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9114 cp_parser_label_declaration (parser);
9115 cp_parser_statement_seq_opt (parser, NULL_TREE);
9116 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9117 }
9118
9119 finish_compound_stmt (compound_stmt);
9120
9121 out:
9122 finish_function_body (body);
9123 finish_lambda_scope ();
9124
9125 /* Finish the function and generate code for it if necessary. */
9126 tree fn = finish_function (/*inline*/2);
9127
9128 /* Only expand if the call op is not a template. */
9129 if (!DECL_TEMPLATE_INFO (fco))
9130 expand_or_defer_fn (fn);
9131 }
9132
9133 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9134 if (nested)
9135 pop_function_context();
9136 else
9137 --function_depth;
9138 }
9139
9140 /* Statements [gram.stmt.stmt] */
9141
9142 /* Parse a statement.
9143
9144 statement:
9145 labeled-statement
9146 expression-statement
9147 compound-statement
9148 selection-statement
9149 iteration-statement
9150 jump-statement
9151 declaration-statement
9152 try-block
9153
9154 C++11:
9155
9156 statement:
9157 labeled-statement
9158 attribute-specifier-seq (opt) expression-statement
9159 attribute-specifier-seq (opt) compound-statement
9160 attribute-specifier-seq (opt) selection-statement
9161 attribute-specifier-seq (opt) iteration-statement
9162 attribute-specifier-seq (opt) jump-statement
9163 declaration-statement
9164 attribute-specifier-seq (opt) try-block
9165
9166 TM Extension:
9167
9168 statement:
9169 atomic-statement
9170
9171 IN_COMPOUND is true when the statement is nested inside a
9172 cp_parser_compound_statement; this matters for certain pragmas.
9173
9174 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9175 is a (possibly labeled) if statement which is not enclosed in braces
9176 and has an else clause. This is used to implement -Wparentheses. */
9177
9178 static void
9179 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9180 bool in_compound, bool *if_p)
9181 {
9182 tree statement, std_attrs = NULL_TREE;
9183 cp_token *token;
9184 location_t statement_location, attrs_location;
9185
9186 restart:
9187 if (if_p != NULL)
9188 *if_p = false;
9189 /* There is no statement yet. */
9190 statement = NULL_TREE;
9191
9192 cp_lexer_save_tokens (parser->lexer);
9193 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9194 if (c_dialect_objc ())
9195 /* In obj-c++, seeing '[[' might be the either the beginning of
9196 c++11 attributes, or a nested objc-message-expression. So
9197 let's parse the c++11 attributes tentatively. */
9198 cp_parser_parse_tentatively (parser);
9199 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9200 if (c_dialect_objc ())
9201 {
9202 if (!cp_parser_parse_definitely (parser))
9203 std_attrs = NULL_TREE;
9204 }
9205
9206 /* Peek at the next token. */
9207 token = cp_lexer_peek_token (parser->lexer);
9208 /* Remember the location of the first token in the statement. */
9209 statement_location = token->location;
9210 /* If this is a keyword, then that will often determine what kind of
9211 statement we have. */
9212 if (token->type == CPP_KEYWORD)
9213 {
9214 enum rid keyword = token->keyword;
9215
9216 switch (keyword)
9217 {
9218 case RID_CASE:
9219 case RID_DEFAULT:
9220 /* Looks like a labeled-statement with a case label.
9221 Parse the label, and then use tail recursion to parse
9222 the statement. */
9223 cp_parser_label_for_labeled_statement (parser, std_attrs);
9224 goto restart;
9225
9226 case RID_IF:
9227 case RID_SWITCH:
9228 statement = cp_parser_selection_statement (parser, if_p);
9229 break;
9230
9231 case RID_WHILE:
9232 case RID_DO:
9233 case RID_FOR:
9234 statement = cp_parser_iteration_statement (parser, false);
9235 break;
9236
9237 case RID_BREAK:
9238 case RID_CONTINUE:
9239 case RID_RETURN:
9240 case RID_GOTO:
9241 statement = cp_parser_jump_statement (parser);
9242 break;
9243
9244 /* Objective-C++ exception-handling constructs. */
9245 case RID_AT_TRY:
9246 case RID_AT_CATCH:
9247 case RID_AT_FINALLY:
9248 case RID_AT_SYNCHRONIZED:
9249 case RID_AT_THROW:
9250 statement = cp_parser_objc_statement (parser);
9251 break;
9252
9253 case RID_TRY:
9254 statement = cp_parser_try_block (parser);
9255 break;
9256
9257 case RID_NAMESPACE:
9258 /* This must be a namespace alias definition. */
9259 cp_parser_declaration_statement (parser);
9260 return;
9261
9262 case RID_TRANSACTION_ATOMIC:
9263 case RID_TRANSACTION_RELAXED:
9264 statement = cp_parser_transaction (parser, keyword);
9265 break;
9266 case RID_TRANSACTION_CANCEL:
9267 statement = cp_parser_transaction_cancel (parser);
9268 break;
9269
9270 default:
9271 /* It might be a keyword like `int' that can start a
9272 declaration-statement. */
9273 break;
9274 }
9275 }
9276 else if (token->type == CPP_NAME)
9277 {
9278 /* If the next token is a `:', then we are looking at a
9279 labeled-statement. */
9280 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9281 if (token->type == CPP_COLON)
9282 {
9283 /* Looks like a labeled-statement with an ordinary label.
9284 Parse the label, and then use tail recursion to parse
9285 the statement. */
9286
9287 cp_parser_label_for_labeled_statement (parser, std_attrs);
9288 goto restart;
9289 }
9290 }
9291 /* Anything that starts with a `{' must be a compound-statement. */
9292 else if (token->type == CPP_OPEN_BRACE)
9293 statement = cp_parser_compound_statement (parser, NULL, false, false);
9294 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9295 a statement all its own. */
9296 else if (token->type == CPP_PRAGMA)
9297 {
9298 /* Only certain OpenMP pragmas are attached to statements, and thus
9299 are considered statements themselves. All others are not. In
9300 the context of a compound, accept the pragma as a "statement" and
9301 return so that we can check for a close brace. Otherwise we
9302 require a real statement and must go back and read one. */
9303 if (in_compound)
9304 cp_parser_pragma (parser, pragma_compound);
9305 else if (!cp_parser_pragma (parser, pragma_stmt))
9306 goto restart;
9307 return;
9308 }
9309 else if (token->type == CPP_EOF)
9310 {
9311 cp_parser_error (parser, "expected statement");
9312 return;
9313 }
9314
9315 /* Everything else must be a declaration-statement or an
9316 expression-statement. Try for the declaration-statement
9317 first, unless we are looking at a `;', in which case we know that
9318 we have an expression-statement. */
9319 if (!statement)
9320 {
9321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9322 {
9323 if (std_attrs != NULL_TREE)
9324 {
9325 /* Attributes should be parsed as part of the the
9326 declaration, so let's un-parse them. */
9327 cp_lexer_rollback_tokens (parser->lexer);
9328 std_attrs = NULL_TREE;
9329 }
9330
9331 cp_parser_parse_tentatively (parser);
9332 /* Try to parse the declaration-statement. */
9333 cp_parser_declaration_statement (parser);
9334 /* If that worked, we're done. */
9335 if (cp_parser_parse_definitely (parser))
9336 return;
9337 }
9338 /* Look for an expression-statement instead. */
9339 statement = cp_parser_expression_statement (parser, in_statement_expr);
9340 }
9341
9342 /* Set the line number for the statement. */
9343 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9344 SET_EXPR_LOCATION (statement, statement_location);
9345
9346 /* Note that for now, we don't do anything with c++11 statements
9347 parsed at this level. */
9348 if (std_attrs != NULL_TREE)
9349 warning_at (attrs_location,
9350 OPT_Wattributes,
9351 "attributes at the beginning of statement are ignored");
9352 }
9353
9354 /* Parse the label for a labeled-statement, i.e.
9355
9356 identifier :
9357 case constant-expression :
9358 default :
9359
9360 GNU Extension:
9361 case constant-expression ... constant-expression : statement
9362
9363 When a label is parsed without errors, the label is added to the
9364 parse tree by the finish_* functions, so this function doesn't
9365 have to return the label. */
9366
9367 static void
9368 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9369 {
9370 cp_token *token;
9371 tree label = NULL_TREE;
9372 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9373
9374 /* The next token should be an identifier. */
9375 token = cp_lexer_peek_token (parser->lexer);
9376 if (token->type != CPP_NAME
9377 && token->type != CPP_KEYWORD)
9378 {
9379 cp_parser_error (parser, "expected labeled-statement");
9380 return;
9381 }
9382
9383 parser->colon_corrects_to_scope_p = false;
9384 switch (token->keyword)
9385 {
9386 case RID_CASE:
9387 {
9388 tree expr, expr_hi;
9389 cp_token *ellipsis;
9390
9391 /* Consume the `case' token. */
9392 cp_lexer_consume_token (parser->lexer);
9393 /* Parse the constant-expression. */
9394 expr = cp_parser_constant_expression (parser,
9395 /*allow_non_constant_p=*/false,
9396 NULL);
9397
9398 ellipsis = cp_lexer_peek_token (parser->lexer);
9399 if (ellipsis->type == CPP_ELLIPSIS)
9400 {
9401 /* Consume the `...' token. */
9402 cp_lexer_consume_token (parser->lexer);
9403 expr_hi =
9404 cp_parser_constant_expression (parser,
9405 /*allow_non_constant_p=*/false,
9406 NULL);
9407 /* We don't need to emit warnings here, as the common code
9408 will do this for us. */
9409 }
9410 else
9411 expr_hi = NULL_TREE;
9412
9413 if (parser->in_switch_statement_p)
9414 finish_case_label (token->location, expr, expr_hi);
9415 else
9416 error_at (token->location,
9417 "case label %qE not within a switch statement",
9418 expr);
9419 }
9420 break;
9421
9422 case RID_DEFAULT:
9423 /* Consume the `default' token. */
9424 cp_lexer_consume_token (parser->lexer);
9425
9426 if (parser->in_switch_statement_p)
9427 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9428 else
9429 error_at (token->location, "case label not within a switch statement");
9430 break;
9431
9432 default:
9433 /* Anything else must be an ordinary label. */
9434 label = finish_label_stmt (cp_parser_identifier (parser));
9435 break;
9436 }
9437
9438 /* Require the `:' token. */
9439 cp_parser_require (parser, CPP_COLON, RT_COLON);
9440
9441 /* An ordinary label may optionally be followed by attributes.
9442 However, this is only permitted if the attributes are then
9443 followed by a semicolon. This is because, for backward
9444 compatibility, when parsing
9445 lab: __attribute__ ((unused)) int i;
9446 we want the attribute to attach to "i", not "lab". */
9447 if (label != NULL_TREE
9448 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9449 {
9450 tree attrs;
9451 cp_parser_parse_tentatively (parser);
9452 attrs = cp_parser_gnu_attributes_opt (parser);
9453 if (attrs == NULL_TREE
9454 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9455 cp_parser_abort_tentative_parse (parser);
9456 else if (!cp_parser_parse_definitely (parser))
9457 ;
9458 else
9459 attributes = chainon (attributes, attrs);
9460 }
9461
9462 if (attributes != NULL_TREE)
9463 cplus_decl_attributes (&label, attributes, 0);
9464
9465 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9466 }
9467
9468 /* Parse an expression-statement.
9469
9470 expression-statement:
9471 expression [opt] ;
9472
9473 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9474 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9475 indicates whether this expression-statement is part of an
9476 expression statement. */
9477
9478 static tree
9479 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9480 {
9481 tree statement = NULL_TREE;
9482 cp_token *token = cp_lexer_peek_token (parser->lexer);
9483
9484 /* If the next token is a ';', then there is no expression
9485 statement. */
9486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9487 {
9488 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9489 if (statement == error_mark_node
9490 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9491 {
9492 cp_parser_skip_to_end_of_block_or_statement (parser);
9493 return error_mark_node;
9494 }
9495 }
9496
9497 /* Give a helpful message for "A<T>::type t;" and the like. */
9498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9499 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9500 {
9501 if (TREE_CODE (statement) == SCOPE_REF)
9502 error_at (token->location, "need %<typename%> before %qE because "
9503 "%qT is a dependent scope",
9504 statement, TREE_OPERAND (statement, 0));
9505 else if (is_overloaded_fn (statement)
9506 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9507 {
9508 /* A::A a; */
9509 tree fn = get_first_fn (statement);
9510 error_at (token->location,
9511 "%<%T::%D%> names the constructor, not the type",
9512 DECL_CONTEXT (fn), DECL_NAME (fn));
9513 }
9514 }
9515
9516 /* Consume the final `;'. */
9517 cp_parser_consume_semicolon_at_end_of_statement (parser);
9518
9519 if (in_statement_expr
9520 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9521 /* This is the final expression statement of a statement
9522 expression. */
9523 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9524 else if (statement)
9525 statement = finish_expr_stmt (statement);
9526
9527 return statement;
9528 }
9529
9530 /* Parse a compound-statement.
9531
9532 compound-statement:
9533 { statement-seq [opt] }
9534
9535 GNU extension:
9536
9537 compound-statement:
9538 { label-declaration-seq [opt] statement-seq [opt] }
9539
9540 label-declaration-seq:
9541 label-declaration
9542 label-declaration-seq label-declaration
9543
9544 Returns a tree representing the statement. */
9545
9546 static tree
9547 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9548 bool in_try, bool function_body)
9549 {
9550 tree compound_stmt;
9551
9552 /* Consume the `{'. */
9553 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9554 return error_mark_node;
9555 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9556 && !function_body)
9557 pedwarn (input_location, OPT_Wpedantic,
9558 "compound-statement in constexpr function");
9559 /* Begin the compound-statement. */
9560 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9561 /* If the next keyword is `__label__' we have a label declaration. */
9562 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9563 cp_parser_label_declaration (parser);
9564 /* Parse an (optional) statement-seq. */
9565 cp_parser_statement_seq_opt (parser, in_statement_expr);
9566 /* Finish the compound-statement. */
9567 finish_compound_stmt (compound_stmt);
9568 /* Consume the `}'. */
9569 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9570
9571 return compound_stmt;
9572 }
9573
9574 /* Parse an (optional) statement-seq.
9575
9576 statement-seq:
9577 statement
9578 statement-seq [opt] statement */
9579
9580 static void
9581 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9582 {
9583 /* Scan statements until there aren't any more. */
9584 while (true)
9585 {
9586 cp_token *token = cp_lexer_peek_token (parser->lexer);
9587
9588 /* If we are looking at a `}', then we have run out of
9589 statements; the same is true if we have reached the end
9590 of file, or have stumbled upon a stray '@end'. */
9591 if (token->type == CPP_CLOSE_BRACE
9592 || token->type == CPP_EOF
9593 || token->type == CPP_PRAGMA_EOL
9594 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9595 break;
9596
9597 /* If we are in a compound statement and find 'else' then
9598 something went wrong. */
9599 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9600 {
9601 if (parser->in_statement & IN_IF_STMT)
9602 break;
9603 else
9604 {
9605 token = cp_lexer_consume_token (parser->lexer);
9606 error_at (token->location, "%<else%> without a previous %<if%>");
9607 }
9608 }
9609
9610 /* Parse the statement. */
9611 cp_parser_statement (parser, in_statement_expr, true, NULL);
9612 }
9613 }
9614
9615 /* Parse a selection-statement.
9616
9617 selection-statement:
9618 if ( condition ) statement
9619 if ( condition ) statement else statement
9620 switch ( condition ) statement
9621
9622 Returns the new IF_STMT or SWITCH_STMT.
9623
9624 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9625 is a (possibly labeled) if statement which is not enclosed in
9626 braces and has an else clause. This is used to implement
9627 -Wparentheses. */
9628
9629 static tree
9630 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9631 {
9632 cp_token *token;
9633 enum rid keyword;
9634
9635 if (if_p != NULL)
9636 *if_p = false;
9637
9638 /* Peek at the next token. */
9639 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9640
9641 /* See what kind of keyword it is. */
9642 keyword = token->keyword;
9643 switch (keyword)
9644 {
9645 case RID_IF:
9646 case RID_SWITCH:
9647 {
9648 tree statement;
9649 tree condition;
9650
9651 /* Look for the `('. */
9652 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9653 {
9654 cp_parser_skip_to_end_of_statement (parser);
9655 return error_mark_node;
9656 }
9657
9658 /* Begin the selection-statement. */
9659 if (keyword == RID_IF)
9660 statement = begin_if_stmt ();
9661 else
9662 statement = begin_switch_stmt ();
9663
9664 /* Parse the condition. */
9665 condition = cp_parser_condition (parser);
9666 /* Look for the `)'. */
9667 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9668 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9669 /*consume_paren=*/true);
9670
9671 if (keyword == RID_IF)
9672 {
9673 bool nested_if;
9674 unsigned char in_statement;
9675
9676 /* Add the condition. */
9677 finish_if_stmt_cond (condition, statement);
9678
9679 /* Parse the then-clause. */
9680 in_statement = parser->in_statement;
9681 parser->in_statement |= IN_IF_STMT;
9682 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9683 {
9684 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9685 add_stmt (build_empty_stmt (loc));
9686 cp_lexer_consume_token (parser->lexer);
9687 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9688 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9689 "empty body in an %<if%> statement");
9690 nested_if = false;
9691 }
9692 else
9693 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9694 parser->in_statement = in_statement;
9695
9696 finish_then_clause (statement);
9697
9698 /* If the next token is `else', parse the else-clause. */
9699 if (cp_lexer_next_token_is_keyword (parser->lexer,
9700 RID_ELSE))
9701 {
9702 /* Consume the `else' keyword. */
9703 cp_lexer_consume_token (parser->lexer);
9704 begin_else_clause (statement);
9705 /* Parse the else-clause. */
9706 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9707 {
9708 location_t loc;
9709 loc = cp_lexer_peek_token (parser->lexer)->location;
9710 warning_at (loc,
9711 OPT_Wempty_body, "suggest braces around "
9712 "empty body in an %<else%> statement");
9713 add_stmt (build_empty_stmt (loc));
9714 cp_lexer_consume_token (parser->lexer);
9715 }
9716 else
9717 cp_parser_implicitly_scoped_statement (parser, NULL);
9718
9719 finish_else_clause (statement);
9720
9721 /* If we are currently parsing a then-clause, then
9722 IF_P will not be NULL. We set it to true to
9723 indicate that this if statement has an else clause.
9724 This may trigger the Wparentheses warning below
9725 when we get back up to the parent if statement. */
9726 if (if_p != NULL)
9727 *if_p = true;
9728 }
9729 else
9730 {
9731 /* This if statement does not have an else clause. If
9732 NESTED_IF is true, then the then-clause is an if
9733 statement which does have an else clause. We warn
9734 about the potential ambiguity. */
9735 if (nested_if)
9736 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9737 "suggest explicit braces to avoid ambiguous"
9738 " %<else%>");
9739 }
9740
9741 /* Now we're all done with the if-statement. */
9742 finish_if_stmt (statement);
9743 }
9744 else
9745 {
9746 bool in_switch_statement_p;
9747 unsigned char in_statement;
9748
9749 /* Add the condition. */
9750 finish_switch_cond (condition, statement);
9751
9752 /* Parse the body of the switch-statement. */
9753 in_switch_statement_p = parser->in_switch_statement_p;
9754 in_statement = parser->in_statement;
9755 parser->in_switch_statement_p = true;
9756 parser->in_statement |= IN_SWITCH_STMT;
9757 cp_parser_implicitly_scoped_statement (parser, NULL);
9758 parser->in_switch_statement_p = in_switch_statement_p;
9759 parser->in_statement = in_statement;
9760
9761 /* Now we're all done with the switch-statement. */
9762 finish_switch_stmt (statement);
9763 }
9764
9765 return statement;
9766 }
9767 break;
9768
9769 default:
9770 cp_parser_error (parser, "expected selection-statement");
9771 return error_mark_node;
9772 }
9773 }
9774
9775 /* Parse a condition.
9776
9777 condition:
9778 expression
9779 type-specifier-seq declarator = initializer-clause
9780 type-specifier-seq declarator braced-init-list
9781
9782 GNU Extension:
9783
9784 condition:
9785 type-specifier-seq declarator asm-specification [opt]
9786 attributes [opt] = assignment-expression
9787
9788 Returns the expression that should be tested. */
9789
9790 static tree
9791 cp_parser_condition (cp_parser* parser)
9792 {
9793 cp_decl_specifier_seq type_specifiers;
9794 const char *saved_message;
9795 int declares_class_or_enum;
9796
9797 /* Try the declaration first. */
9798 cp_parser_parse_tentatively (parser);
9799 /* New types are not allowed in the type-specifier-seq for a
9800 condition. */
9801 saved_message = parser->type_definition_forbidden_message;
9802 parser->type_definition_forbidden_message
9803 = G_("types may not be defined in conditions");
9804 /* Parse the type-specifier-seq. */
9805 cp_parser_decl_specifier_seq (parser,
9806 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9807 &type_specifiers,
9808 &declares_class_or_enum);
9809 /* Restore the saved message. */
9810 parser->type_definition_forbidden_message = saved_message;
9811 /* If all is well, we might be looking at a declaration. */
9812 if (!cp_parser_error_occurred (parser))
9813 {
9814 tree decl;
9815 tree asm_specification;
9816 tree attributes;
9817 cp_declarator *declarator;
9818 tree initializer = NULL_TREE;
9819
9820 /* Parse the declarator. */
9821 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9822 /*ctor_dtor_or_conv_p=*/NULL,
9823 /*parenthesized_p=*/NULL,
9824 /*member_p=*/false);
9825 /* Parse the attributes. */
9826 attributes = cp_parser_attributes_opt (parser);
9827 /* Parse the asm-specification. */
9828 asm_specification = cp_parser_asm_specification_opt (parser);
9829 /* If the next token is not an `=' or '{', then we might still be
9830 looking at an expression. For example:
9831
9832 if (A(a).x)
9833
9834 looks like a decl-specifier-seq and a declarator -- but then
9835 there is no `=', so this is an expression. */
9836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9837 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9838 cp_parser_simulate_error (parser);
9839
9840 /* If we did see an `=' or '{', then we are looking at a declaration
9841 for sure. */
9842 if (cp_parser_parse_definitely (parser))
9843 {
9844 tree pushed_scope;
9845 bool non_constant_p;
9846 bool flags = LOOKUP_ONLYCONVERTING;
9847
9848 /* Create the declaration. */
9849 decl = start_decl (declarator, &type_specifiers,
9850 /*initialized_p=*/true,
9851 attributes, /*prefix_attributes=*/NULL_TREE,
9852 &pushed_scope);
9853
9854 /* Parse the initializer. */
9855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9856 {
9857 initializer = cp_parser_braced_list (parser, &non_constant_p);
9858 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9859 flags = 0;
9860 }
9861 else
9862 {
9863 /* Consume the `='. */
9864 cp_parser_require (parser, CPP_EQ, RT_EQ);
9865 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9866 }
9867 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9868 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9869
9870 /* Process the initializer. */
9871 cp_finish_decl (decl,
9872 initializer, !non_constant_p,
9873 asm_specification,
9874 flags);
9875
9876 if (pushed_scope)
9877 pop_scope (pushed_scope);
9878
9879 return convert_from_reference (decl);
9880 }
9881 }
9882 /* If we didn't even get past the declarator successfully, we are
9883 definitely not looking at a declaration. */
9884 else
9885 cp_parser_abort_tentative_parse (parser);
9886
9887 /* Otherwise, we are looking at an expression. */
9888 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9889 }
9890
9891 /* Parses a for-statement or range-for-statement until the closing ')',
9892 not included. */
9893
9894 static tree
9895 cp_parser_for (cp_parser *parser, bool ivdep)
9896 {
9897 tree init, scope, decl;
9898 bool is_range_for;
9899
9900 /* Begin the for-statement. */
9901 scope = begin_for_scope (&init);
9902
9903 /* Parse the initialization. */
9904 is_range_for = cp_parser_for_init_statement (parser, &decl);
9905
9906 if (is_range_for)
9907 return cp_parser_range_for (parser, scope, init, decl, ivdep);
9908 else
9909 return cp_parser_c_for (parser, scope, init, ivdep);
9910 }
9911
9912 static tree
9913 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
9914 {
9915 /* Normal for loop */
9916 tree condition = NULL_TREE;
9917 tree expression = NULL_TREE;
9918 tree stmt;
9919
9920 stmt = begin_for_stmt (scope, init);
9921 /* The for-init-statement has already been parsed in
9922 cp_parser_for_init_statement, so no work is needed here. */
9923 finish_for_init_stmt (stmt);
9924
9925 /* If there's a condition, process it. */
9926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9927 condition = cp_parser_condition (parser);
9928 else if (ivdep)
9929 {
9930 cp_parser_error (parser, "missing loop condition in loop with "
9931 "%<GCC ivdep%> pragma");
9932 condition = error_mark_node;
9933 }
9934 finish_for_cond (condition, stmt, ivdep);
9935 /* Look for the `;'. */
9936 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9937
9938 /* If there's an expression, process it. */
9939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9940 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9941 finish_for_expr (expression, stmt);
9942
9943 return stmt;
9944 }
9945
9946 /* Tries to parse a range-based for-statement:
9947
9948 range-based-for:
9949 decl-specifier-seq declarator : expression
9950
9951 The decl-specifier-seq declarator and the `:' are already parsed by
9952 cp_parser_for_init_statement. If processing_template_decl it returns a
9953 newly created RANGE_FOR_STMT; if not, it is converted to a
9954 regular FOR_STMT. */
9955
9956 static tree
9957 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
9958 bool ivdep)
9959 {
9960 tree stmt, range_expr;
9961
9962 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9963 {
9964 bool expr_non_constant_p;
9965 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9966 }
9967 else
9968 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9969
9970 /* If in template, STMT is converted to a normal for-statement
9971 at instantiation. If not, it is done just ahead. */
9972 if (processing_template_decl)
9973 {
9974 if (check_for_bare_parameter_packs (range_expr))
9975 range_expr = error_mark_node;
9976 stmt = begin_range_for_stmt (scope, init);
9977 if (ivdep)
9978 RANGE_FOR_IVDEP (stmt) = 1;
9979 finish_range_for_decl (stmt, range_decl, range_expr);
9980 if (!type_dependent_expression_p (range_expr)
9981 /* do_auto_deduction doesn't mess with template init-lists. */
9982 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9983 do_range_for_auto_deduction (range_decl, range_expr);
9984 }
9985 else
9986 {
9987 stmt = begin_for_stmt (scope, init);
9988 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
9989 }
9990 return stmt;
9991 }
9992
9993 /* Subroutine of cp_convert_range_for: given the initializer expression,
9994 builds up the range temporary. */
9995
9996 static tree
9997 build_range_temp (tree range_expr)
9998 {
9999 tree range_type, range_temp;
10000
10001 /* Find out the type deduced by the declaration
10002 `auto &&__range = range_expr'. */
10003 range_type = cp_build_reference_type (make_auto (), true);
10004 range_type = do_auto_deduction (range_type, range_expr,
10005 type_uses_auto (range_type));
10006
10007 /* Create the __range variable. */
10008 range_temp = build_decl (input_location, VAR_DECL,
10009 get_identifier ("__for_range"), range_type);
10010 TREE_USED (range_temp) = 1;
10011 DECL_ARTIFICIAL (range_temp) = 1;
10012
10013 return range_temp;
10014 }
10015
10016 /* Used by cp_parser_range_for in template context: we aren't going to
10017 do a full conversion yet, but we still need to resolve auto in the
10018 type of the for-range-declaration if present. This is basically
10019 a shortcut version of cp_convert_range_for. */
10020
10021 static void
10022 do_range_for_auto_deduction (tree decl, tree range_expr)
10023 {
10024 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10025 if (auto_node)
10026 {
10027 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10028 range_temp = convert_from_reference (build_range_temp (range_expr));
10029 iter_type = (cp_parser_perform_range_for_lookup
10030 (range_temp, &begin_dummy, &end_dummy));
10031 if (iter_type)
10032 {
10033 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10034 iter_type);
10035 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10036 tf_warning_or_error);
10037 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10038 iter_decl, auto_node);
10039 }
10040 }
10041 }
10042
10043 /* Converts a range-based for-statement into a normal
10044 for-statement, as per the definition.
10045
10046 for (RANGE_DECL : RANGE_EXPR)
10047 BLOCK
10048
10049 should be equivalent to:
10050
10051 {
10052 auto &&__range = RANGE_EXPR;
10053 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10054 __begin != __end;
10055 ++__begin)
10056 {
10057 RANGE_DECL = *__begin;
10058 BLOCK
10059 }
10060 }
10061
10062 If RANGE_EXPR is an array:
10063 BEGIN_EXPR = __range
10064 END_EXPR = __range + ARRAY_SIZE(__range)
10065 Else if RANGE_EXPR has a member 'begin' or 'end':
10066 BEGIN_EXPR = __range.begin()
10067 END_EXPR = __range.end()
10068 Else:
10069 BEGIN_EXPR = begin(__range)
10070 END_EXPR = end(__range);
10071
10072 If __range has a member 'begin' but not 'end', or vice versa, we must
10073 still use the second alternative (it will surely fail, however).
10074 When calling begin()/end() in the third alternative we must use
10075 argument dependent lookup, but always considering 'std' as an associated
10076 namespace. */
10077
10078 tree
10079 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10080 bool ivdep)
10081 {
10082 tree begin, end;
10083 tree iter_type, begin_expr, end_expr;
10084 tree condition, expression;
10085
10086 if (range_decl == error_mark_node || range_expr == error_mark_node)
10087 /* If an error happened previously do nothing or else a lot of
10088 unhelpful errors would be issued. */
10089 begin_expr = end_expr = iter_type = error_mark_node;
10090 else
10091 {
10092 tree range_temp;
10093
10094 if (TREE_CODE (range_expr) == VAR_DECL
10095 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10096 /* Can't bind a reference to an array of runtime bound. */
10097 range_temp = range_expr;
10098 else
10099 {
10100 range_temp = build_range_temp (range_expr);
10101 pushdecl (range_temp);
10102 cp_finish_decl (range_temp, range_expr,
10103 /*is_constant_init*/false, NULL_TREE,
10104 LOOKUP_ONLYCONVERTING);
10105 range_temp = convert_from_reference (range_temp);
10106 }
10107 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10108 &begin_expr, &end_expr);
10109 }
10110
10111 /* The new for initialization statement. */
10112 begin = build_decl (input_location, VAR_DECL,
10113 get_identifier ("__for_begin"), iter_type);
10114 TREE_USED (begin) = 1;
10115 DECL_ARTIFICIAL (begin) = 1;
10116 pushdecl (begin);
10117 cp_finish_decl (begin, begin_expr,
10118 /*is_constant_init*/false, NULL_TREE,
10119 LOOKUP_ONLYCONVERTING);
10120
10121 end = build_decl (input_location, VAR_DECL,
10122 get_identifier ("__for_end"), iter_type);
10123 TREE_USED (end) = 1;
10124 DECL_ARTIFICIAL (end) = 1;
10125 pushdecl (end);
10126 cp_finish_decl (end, end_expr,
10127 /*is_constant_init*/false, NULL_TREE,
10128 LOOKUP_ONLYCONVERTING);
10129
10130 finish_for_init_stmt (statement);
10131
10132 /* The new for condition. */
10133 condition = build_x_binary_op (input_location, NE_EXPR,
10134 begin, ERROR_MARK,
10135 end, ERROR_MARK,
10136 NULL, tf_warning_or_error);
10137 finish_for_cond (condition, statement, ivdep);
10138
10139 /* The new increment expression. */
10140 expression = finish_unary_op_expr (input_location,
10141 PREINCREMENT_EXPR, begin,
10142 tf_warning_or_error);
10143 finish_for_expr (expression, statement);
10144
10145 /* The declaration is initialized with *__begin inside the loop body. */
10146 cp_finish_decl (range_decl,
10147 build_x_indirect_ref (input_location, begin, RO_NULL,
10148 tf_warning_or_error),
10149 /*is_constant_init*/false, NULL_TREE,
10150 LOOKUP_ONLYCONVERTING);
10151
10152 return statement;
10153 }
10154
10155 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10156 We need to solve both at the same time because the method used
10157 depends on the existence of members begin or end.
10158 Returns the type deduced for the iterator expression. */
10159
10160 static tree
10161 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10162 {
10163 if (error_operand_p (range))
10164 {
10165 *begin = *end = error_mark_node;
10166 return error_mark_node;
10167 }
10168
10169 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10170 {
10171 error ("range-based %<for%> expression of type %qT "
10172 "has incomplete type", TREE_TYPE (range));
10173 *begin = *end = error_mark_node;
10174 return error_mark_node;
10175 }
10176 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10177 {
10178 /* If RANGE is an array, we will use pointer arithmetic. */
10179 *begin = range;
10180 *end = build_binary_op (input_location, PLUS_EXPR,
10181 range,
10182 array_type_nelts_top (TREE_TYPE (range)),
10183 0);
10184 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10185 }
10186 else
10187 {
10188 /* If it is not an array, we must do a bit of magic. */
10189 tree id_begin, id_end;
10190 tree member_begin, member_end;
10191
10192 *begin = *end = error_mark_node;
10193
10194 id_begin = get_identifier ("begin");
10195 id_end = get_identifier ("end");
10196 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10197 /*protect=*/2, /*want_type=*/false,
10198 tf_warning_or_error);
10199 member_end = lookup_member (TREE_TYPE (range), id_end,
10200 /*protect=*/2, /*want_type=*/false,
10201 tf_warning_or_error);
10202
10203 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10204 {
10205 /* Use the member functions. */
10206 if (member_begin != NULL_TREE)
10207 *begin = cp_parser_range_for_member_function (range, id_begin);
10208 else
10209 error ("range-based %<for%> expression of type %qT has an "
10210 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10211
10212 if (member_end != NULL_TREE)
10213 *end = cp_parser_range_for_member_function (range, id_end);
10214 else
10215 error ("range-based %<for%> expression of type %qT has a "
10216 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10217 }
10218 else
10219 {
10220 /* Use global functions with ADL. */
10221 vec<tree, va_gc> *vec;
10222 vec = make_tree_vector ();
10223
10224 vec_safe_push (vec, range);
10225
10226 member_begin = perform_koenig_lookup (id_begin, vec,
10227 /*include_std=*/true,
10228 tf_warning_or_error);
10229 *begin = finish_call_expr (member_begin, &vec, false, true,
10230 tf_warning_or_error);
10231 member_end = perform_koenig_lookup (id_end, vec,
10232 /*include_std=*/true,
10233 tf_warning_or_error);
10234 *end = finish_call_expr (member_end, &vec, false, true,
10235 tf_warning_or_error);
10236
10237 release_tree_vector (vec);
10238 }
10239
10240 /* Last common checks. */
10241 if (*begin == error_mark_node || *end == error_mark_node)
10242 {
10243 /* If one of the expressions is an error do no more checks. */
10244 *begin = *end = error_mark_node;
10245 return error_mark_node;
10246 }
10247 else if (type_dependent_expression_p (*begin)
10248 || type_dependent_expression_p (*end))
10249 /* Can happen, when, eg, in a template context, Koenig lookup
10250 can't resolve begin/end (c++/58503). */
10251 return NULL_TREE;
10252 else
10253 {
10254 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10255 /* The unqualified type of the __begin and __end temporaries should
10256 be the same, as required by the multiple auto declaration. */
10257 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10258 error ("inconsistent begin/end types in range-based %<for%> "
10259 "statement: %qT and %qT",
10260 TREE_TYPE (*begin), TREE_TYPE (*end));
10261 return iter_type;
10262 }
10263 }
10264 }
10265
10266 /* Helper function for cp_parser_perform_range_for_lookup.
10267 Builds a tree for RANGE.IDENTIFIER(). */
10268
10269 static tree
10270 cp_parser_range_for_member_function (tree range, tree identifier)
10271 {
10272 tree member, res;
10273 vec<tree, va_gc> *vec;
10274
10275 member = finish_class_member_access_expr (range, identifier,
10276 false, tf_warning_or_error);
10277 if (member == error_mark_node)
10278 return error_mark_node;
10279
10280 vec = make_tree_vector ();
10281 res = finish_call_expr (member, &vec,
10282 /*disallow_virtual=*/false,
10283 /*koenig_p=*/false,
10284 tf_warning_or_error);
10285 release_tree_vector (vec);
10286 return res;
10287 }
10288
10289 /* Parse an iteration-statement.
10290
10291 iteration-statement:
10292 while ( condition ) statement
10293 do statement while ( expression ) ;
10294 for ( for-init-statement condition [opt] ; expression [opt] )
10295 statement
10296
10297 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10298
10299 static tree
10300 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10301 {
10302 cp_token *token;
10303 enum rid keyword;
10304 tree statement;
10305 unsigned char in_statement;
10306
10307 /* Peek at the next token. */
10308 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10309 if (!token)
10310 return error_mark_node;
10311
10312 /* Remember whether or not we are already within an iteration
10313 statement. */
10314 in_statement = parser->in_statement;
10315
10316 /* See what kind of keyword it is. */
10317 keyword = token->keyword;
10318 switch (keyword)
10319 {
10320 case RID_WHILE:
10321 {
10322 tree condition;
10323
10324 /* Begin the while-statement. */
10325 statement = begin_while_stmt ();
10326 /* Look for the `('. */
10327 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10328 /* Parse the condition. */
10329 condition = cp_parser_condition (parser);
10330 finish_while_stmt_cond (condition, statement, ivdep);
10331 /* Look for the `)'. */
10332 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10333 /* Parse the dependent statement. */
10334 parser->in_statement = IN_ITERATION_STMT;
10335 cp_parser_already_scoped_statement (parser);
10336 parser->in_statement = in_statement;
10337 /* We're done with the while-statement. */
10338 finish_while_stmt (statement);
10339 }
10340 break;
10341
10342 case RID_DO:
10343 {
10344 tree expression;
10345
10346 /* Begin the do-statement. */
10347 statement = begin_do_stmt ();
10348 /* Parse the body of the do-statement. */
10349 parser->in_statement = IN_ITERATION_STMT;
10350 cp_parser_implicitly_scoped_statement (parser, NULL);
10351 parser->in_statement = in_statement;
10352 finish_do_body (statement);
10353 /* Look for the `while' keyword. */
10354 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10355 /* Look for the `('. */
10356 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10357 /* Parse the expression. */
10358 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10359 /* We're done with the do-statement. */
10360 finish_do_stmt (expression, statement, ivdep);
10361 /* Look for the `)'. */
10362 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10363 /* Look for the `;'. */
10364 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10365 }
10366 break;
10367
10368 case RID_FOR:
10369 {
10370 /* Look for the `('. */
10371 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10372
10373 statement = cp_parser_for (parser, ivdep);
10374
10375 /* Look for the `)'. */
10376 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10377
10378 /* Parse the body of the for-statement. */
10379 parser->in_statement = IN_ITERATION_STMT;
10380 cp_parser_already_scoped_statement (parser);
10381 parser->in_statement = in_statement;
10382
10383 /* We're done with the for-statement. */
10384 finish_for_stmt (statement);
10385 }
10386 break;
10387
10388 default:
10389 cp_parser_error (parser, "expected iteration-statement");
10390 statement = error_mark_node;
10391 break;
10392 }
10393
10394 return statement;
10395 }
10396
10397 /* Parse a for-init-statement or the declarator of a range-based-for.
10398 Returns true if a range-based-for declaration is seen.
10399
10400 for-init-statement:
10401 expression-statement
10402 simple-declaration */
10403
10404 static bool
10405 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10406 {
10407 /* If the next token is a `;', then we have an empty
10408 expression-statement. Grammatically, this is also a
10409 simple-declaration, but an invalid one, because it does not
10410 declare anything. Therefore, if we did not handle this case
10411 specially, we would issue an error message about an invalid
10412 declaration. */
10413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10414 {
10415 bool is_range_for = false;
10416 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10417
10418 parser->colon_corrects_to_scope_p = false;
10419
10420 /* We're going to speculatively look for a declaration, falling back
10421 to an expression, if necessary. */
10422 cp_parser_parse_tentatively (parser);
10423 /* Parse the declaration. */
10424 cp_parser_simple_declaration (parser,
10425 /*function_definition_allowed_p=*/false,
10426 decl);
10427 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10428 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10429 {
10430 /* It is a range-for, consume the ':' */
10431 cp_lexer_consume_token (parser->lexer);
10432 is_range_for = true;
10433 if (cxx_dialect < cxx11)
10434 {
10435 error_at (cp_lexer_peek_token (parser->lexer)->location,
10436 "range-based %<for%> loops are not allowed "
10437 "in C++98 mode");
10438 *decl = error_mark_node;
10439 }
10440 }
10441 else
10442 /* The ';' is not consumed yet because we told
10443 cp_parser_simple_declaration not to. */
10444 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10445
10446 if (cp_parser_parse_definitely (parser))
10447 return is_range_for;
10448 /* If the tentative parse failed, then we shall need to look for an
10449 expression-statement. */
10450 }
10451 /* If we are here, it is an expression-statement. */
10452 cp_parser_expression_statement (parser, NULL_TREE);
10453 return false;
10454 }
10455
10456 /* Parse a jump-statement.
10457
10458 jump-statement:
10459 break ;
10460 continue ;
10461 return expression [opt] ;
10462 return braced-init-list ;
10463 goto identifier ;
10464
10465 GNU extension:
10466
10467 jump-statement:
10468 goto * expression ;
10469
10470 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10471
10472 static tree
10473 cp_parser_jump_statement (cp_parser* parser)
10474 {
10475 tree statement = error_mark_node;
10476 cp_token *token;
10477 enum rid keyword;
10478 unsigned char in_statement;
10479
10480 /* Peek at the next token. */
10481 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10482 if (!token)
10483 return error_mark_node;
10484
10485 /* See what kind of keyword it is. */
10486 keyword = token->keyword;
10487 switch (keyword)
10488 {
10489 case RID_BREAK:
10490 in_statement = parser->in_statement & ~IN_IF_STMT;
10491 switch (in_statement)
10492 {
10493 case 0:
10494 error_at (token->location, "break statement not within loop or switch");
10495 break;
10496 default:
10497 gcc_assert ((in_statement & IN_SWITCH_STMT)
10498 || in_statement == IN_ITERATION_STMT);
10499 statement = finish_break_stmt ();
10500 break;
10501 case IN_OMP_BLOCK:
10502 error_at (token->location, "invalid exit from OpenMP structured block");
10503 break;
10504 case IN_OMP_FOR:
10505 error_at (token->location, "break statement used with OpenMP for loop");
10506 break;
10507 }
10508 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10509 break;
10510
10511 case RID_CONTINUE:
10512 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10513 {
10514 case 0:
10515 error_at (token->location, "continue statement not within a loop");
10516 break;
10517 case IN_ITERATION_STMT:
10518 case IN_OMP_FOR:
10519 statement = finish_continue_stmt ();
10520 break;
10521 case IN_OMP_BLOCK:
10522 error_at (token->location, "invalid exit from OpenMP structured block");
10523 break;
10524 default:
10525 gcc_unreachable ();
10526 }
10527 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10528 break;
10529
10530 case RID_RETURN:
10531 {
10532 tree expr;
10533 bool expr_non_constant_p;
10534
10535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10536 {
10537 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10538 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10539 }
10540 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10541 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10542 else
10543 /* If the next token is a `;', then there is no
10544 expression. */
10545 expr = NULL_TREE;
10546 /* Build the return-statement. */
10547 statement = finish_return_stmt (expr);
10548 /* Look for the final `;'. */
10549 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10550 }
10551 break;
10552
10553 case RID_GOTO:
10554 /* Create the goto-statement. */
10555 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10556 {
10557 /* Issue a warning about this use of a GNU extension. */
10558 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10559 /* Consume the '*' token. */
10560 cp_lexer_consume_token (parser->lexer);
10561 /* Parse the dependent expression. */
10562 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10563 }
10564 else
10565 finish_goto_stmt (cp_parser_identifier (parser));
10566 /* Look for the final `;'. */
10567 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10568 break;
10569
10570 default:
10571 cp_parser_error (parser, "expected jump-statement");
10572 break;
10573 }
10574
10575 return statement;
10576 }
10577
10578 /* Parse a declaration-statement.
10579
10580 declaration-statement:
10581 block-declaration */
10582
10583 static void
10584 cp_parser_declaration_statement (cp_parser* parser)
10585 {
10586 void *p;
10587
10588 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10589 p = obstack_alloc (&declarator_obstack, 0);
10590
10591 /* Parse the block-declaration. */
10592 cp_parser_block_declaration (parser, /*statement_p=*/true);
10593
10594 /* Free any declarators allocated. */
10595 obstack_free (&declarator_obstack, p);
10596 }
10597
10598 /* Some dependent statements (like `if (cond) statement'), are
10599 implicitly in their own scope. In other words, if the statement is
10600 a single statement (as opposed to a compound-statement), it is
10601 none-the-less treated as if it were enclosed in braces. Any
10602 declarations appearing in the dependent statement are out of scope
10603 after control passes that point. This function parses a statement,
10604 but ensures that is in its own scope, even if it is not a
10605 compound-statement.
10606
10607 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10608 is a (possibly labeled) if statement which is not enclosed in
10609 braces and has an else clause. This is used to implement
10610 -Wparentheses.
10611
10612 Returns the new statement. */
10613
10614 static tree
10615 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10616 {
10617 tree statement;
10618
10619 if (if_p != NULL)
10620 *if_p = false;
10621
10622 /* Mark if () ; with a special NOP_EXPR. */
10623 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10624 {
10625 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10626 cp_lexer_consume_token (parser->lexer);
10627 statement = add_stmt (build_empty_stmt (loc));
10628 }
10629 /* if a compound is opened, we simply parse the statement directly. */
10630 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10631 statement = cp_parser_compound_statement (parser, NULL, false, false);
10632 /* If the token is not a `{', then we must take special action. */
10633 else
10634 {
10635 /* Create a compound-statement. */
10636 statement = begin_compound_stmt (0);
10637 /* Parse the dependent-statement. */
10638 cp_parser_statement (parser, NULL_TREE, false, if_p);
10639 /* Finish the dummy compound-statement. */
10640 finish_compound_stmt (statement);
10641 }
10642
10643 /* Return the statement. */
10644 return statement;
10645 }
10646
10647 /* For some dependent statements (like `while (cond) statement'), we
10648 have already created a scope. Therefore, even if the dependent
10649 statement is a compound-statement, we do not want to create another
10650 scope. */
10651
10652 static void
10653 cp_parser_already_scoped_statement (cp_parser* parser)
10654 {
10655 /* If the token is a `{', then we must take special action. */
10656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10657 cp_parser_statement (parser, NULL_TREE, false, NULL);
10658 else
10659 {
10660 /* Avoid calling cp_parser_compound_statement, so that we
10661 don't create a new scope. Do everything else by hand. */
10662 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10663 /* If the next keyword is `__label__' we have a label declaration. */
10664 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10665 cp_parser_label_declaration (parser);
10666 /* Parse an (optional) statement-seq. */
10667 cp_parser_statement_seq_opt (parser, NULL_TREE);
10668 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10669 }
10670 }
10671
10672 /* Declarations [gram.dcl.dcl] */
10673
10674 /* Parse an optional declaration-sequence.
10675
10676 declaration-seq:
10677 declaration
10678 declaration-seq declaration */
10679
10680 static void
10681 cp_parser_declaration_seq_opt (cp_parser* parser)
10682 {
10683 while (true)
10684 {
10685 cp_token *token;
10686
10687 token = cp_lexer_peek_token (parser->lexer);
10688
10689 if (token->type == CPP_CLOSE_BRACE
10690 || token->type == CPP_EOF
10691 || token->type == CPP_PRAGMA_EOL)
10692 break;
10693
10694 if (token->type == CPP_SEMICOLON)
10695 {
10696 /* A declaration consisting of a single semicolon is
10697 invalid. Allow it unless we're being pedantic. */
10698 cp_lexer_consume_token (parser->lexer);
10699 if (!in_system_header)
10700 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10701 continue;
10702 }
10703
10704 /* If we're entering or exiting a region that's implicitly
10705 extern "C", modify the lang context appropriately. */
10706 if (!parser->implicit_extern_c && token->implicit_extern_c)
10707 {
10708 push_lang_context (lang_name_c);
10709 parser->implicit_extern_c = true;
10710 }
10711 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10712 {
10713 pop_lang_context ();
10714 parser->implicit_extern_c = false;
10715 }
10716
10717 if (token->type == CPP_PRAGMA)
10718 {
10719 /* A top-level declaration can consist solely of a #pragma.
10720 A nested declaration cannot, so this is done here and not
10721 in cp_parser_declaration. (A #pragma at block scope is
10722 handled in cp_parser_statement.) */
10723 cp_parser_pragma (parser, pragma_external);
10724 continue;
10725 }
10726
10727 /* Parse the declaration itself. */
10728 cp_parser_declaration (parser);
10729 }
10730 }
10731
10732 /* Parse a declaration.
10733
10734 declaration:
10735 block-declaration
10736 function-definition
10737 template-declaration
10738 explicit-instantiation
10739 explicit-specialization
10740 linkage-specification
10741 namespace-definition
10742
10743 GNU extension:
10744
10745 declaration:
10746 __extension__ declaration */
10747
10748 static void
10749 cp_parser_declaration (cp_parser* parser)
10750 {
10751 cp_token token1;
10752 cp_token token2;
10753 int saved_pedantic;
10754 void *p;
10755 tree attributes = NULL_TREE;
10756
10757 /* Check for the `__extension__' keyword. */
10758 if (cp_parser_extension_opt (parser, &saved_pedantic))
10759 {
10760 /* Parse the qualified declaration. */
10761 cp_parser_declaration (parser);
10762 /* Restore the PEDANTIC flag. */
10763 pedantic = saved_pedantic;
10764
10765 return;
10766 }
10767
10768 /* Try to figure out what kind of declaration is present. */
10769 token1 = *cp_lexer_peek_token (parser->lexer);
10770
10771 if (token1.type != CPP_EOF)
10772 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10773 else
10774 {
10775 token2.type = CPP_EOF;
10776 token2.keyword = RID_MAX;
10777 }
10778
10779 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10780 p = obstack_alloc (&declarator_obstack, 0);
10781
10782 /* If the next token is `extern' and the following token is a string
10783 literal, then we have a linkage specification. */
10784 if (token1.keyword == RID_EXTERN
10785 && cp_parser_is_pure_string_literal (&token2))
10786 cp_parser_linkage_specification (parser);
10787 /* If the next token is `template', then we have either a template
10788 declaration, an explicit instantiation, or an explicit
10789 specialization. */
10790 else if (token1.keyword == RID_TEMPLATE)
10791 {
10792 /* `template <>' indicates a template specialization. */
10793 if (token2.type == CPP_LESS
10794 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10795 cp_parser_explicit_specialization (parser);
10796 /* `template <' indicates a template declaration. */
10797 else if (token2.type == CPP_LESS)
10798 cp_parser_template_declaration (parser, /*member_p=*/false);
10799 /* Anything else must be an explicit instantiation. */
10800 else
10801 cp_parser_explicit_instantiation (parser);
10802 }
10803 /* If the next token is `export', then we have a template
10804 declaration. */
10805 else if (token1.keyword == RID_EXPORT)
10806 cp_parser_template_declaration (parser, /*member_p=*/false);
10807 /* If the next token is `extern', 'static' or 'inline' and the one
10808 after that is `template', we have a GNU extended explicit
10809 instantiation directive. */
10810 else if (cp_parser_allow_gnu_extensions_p (parser)
10811 && (token1.keyword == RID_EXTERN
10812 || token1.keyword == RID_STATIC
10813 || token1.keyword == RID_INLINE)
10814 && token2.keyword == RID_TEMPLATE)
10815 cp_parser_explicit_instantiation (parser);
10816 /* If the next token is `namespace', check for a named or unnamed
10817 namespace definition. */
10818 else if (token1.keyword == RID_NAMESPACE
10819 && (/* A named namespace definition. */
10820 (token2.type == CPP_NAME
10821 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10822 != CPP_EQ))
10823 /* An unnamed namespace definition. */
10824 || token2.type == CPP_OPEN_BRACE
10825 || token2.keyword == RID_ATTRIBUTE))
10826 cp_parser_namespace_definition (parser);
10827 /* An inline (associated) namespace definition. */
10828 else if (token1.keyword == RID_INLINE
10829 && token2.keyword == RID_NAMESPACE)
10830 cp_parser_namespace_definition (parser);
10831 /* Objective-C++ declaration/definition. */
10832 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10833 cp_parser_objc_declaration (parser, NULL_TREE);
10834 else if (c_dialect_objc ()
10835 && token1.keyword == RID_ATTRIBUTE
10836 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10837 cp_parser_objc_declaration (parser, attributes);
10838 /* We must have either a block declaration or a function
10839 definition. */
10840 else
10841 /* Try to parse a block-declaration, or a function-definition. */
10842 cp_parser_block_declaration (parser, /*statement_p=*/false);
10843
10844 /* Free any declarators allocated. */
10845 obstack_free (&declarator_obstack, p);
10846 }
10847
10848 /* Parse a block-declaration.
10849
10850 block-declaration:
10851 simple-declaration
10852 asm-definition
10853 namespace-alias-definition
10854 using-declaration
10855 using-directive
10856
10857 GNU Extension:
10858
10859 block-declaration:
10860 __extension__ block-declaration
10861
10862 C++0x Extension:
10863
10864 block-declaration:
10865 static_assert-declaration
10866
10867 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10868 part of a declaration-statement. */
10869
10870 static void
10871 cp_parser_block_declaration (cp_parser *parser,
10872 bool statement_p)
10873 {
10874 cp_token *token1;
10875 int saved_pedantic;
10876
10877 /* Check for the `__extension__' keyword. */
10878 if (cp_parser_extension_opt (parser, &saved_pedantic))
10879 {
10880 /* Parse the qualified declaration. */
10881 cp_parser_block_declaration (parser, statement_p);
10882 /* Restore the PEDANTIC flag. */
10883 pedantic = saved_pedantic;
10884
10885 return;
10886 }
10887
10888 /* Peek at the next token to figure out which kind of declaration is
10889 present. */
10890 token1 = cp_lexer_peek_token (parser->lexer);
10891
10892 /* If the next keyword is `asm', we have an asm-definition. */
10893 if (token1->keyword == RID_ASM)
10894 {
10895 if (statement_p)
10896 cp_parser_commit_to_tentative_parse (parser);
10897 cp_parser_asm_definition (parser);
10898 }
10899 /* If the next keyword is `namespace', we have a
10900 namespace-alias-definition. */
10901 else if (token1->keyword == RID_NAMESPACE)
10902 cp_parser_namespace_alias_definition (parser);
10903 /* If the next keyword is `using', we have a
10904 using-declaration, a using-directive, or an alias-declaration. */
10905 else if (token1->keyword == RID_USING)
10906 {
10907 cp_token *token2;
10908
10909 if (statement_p)
10910 cp_parser_commit_to_tentative_parse (parser);
10911 /* If the token after `using' is `namespace', then we have a
10912 using-directive. */
10913 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10914 if (token2->keyword == RID_NAMESPACE)
10915 cp_parser_using_directive (parser);
10916 /* If the second token after 'using' is '=', then we have an
10917 alias-declaration. */
10918 else if (cxx_dialect >= cxx11
10919 && token2->type == CPP_NAME
10920 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10921 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10922 cp_parser_alias_declaration (parser);
10923 /* Otherwise, it's a using-declaration. */
10924 else
10925 cp_parser_using_declaration (parser,
10926 /*access_declaration_p=*/false);
10927 }
10928 /* If the next keyword is `__label__' we have a misplaced label
10929 declaration. */
10930 else if (token1->keyword == RID_LABEL)
10931 {
10932 cp_lexer_consume_token (parser->lexer);
10933 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10934 cp_parser_skip_to_end_of_statement (parser);
10935 /* If the next token is now a `;', consume it. */
10936 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10937 cp_lexer_consume_token (parser->lexer);
10938 }
10939 /* If the next token is `static_assert' we have a static assertion. */
10940 else if (token1->keyword == RID_STATIC_ASSERT)
10941 cp_parser_static_assert (parser, /*member_p=*/false);
10942 /* Anything else must be a simple-declaration. */
10943 else
10944 cp_parser_simple_declaration (parser, !statement_p,
10945 /*maybe_range_for_decl*/NULL);
10946 }
10947
10948 /* Parse a simple-declaration.
10949
10950 simple-declaration:
10951 decl-specifier-seq [opt] init-declarator-list [opt] ;
10952
10953 init-declarator-list:
10954 init-declarator
10955 init-declarator-list , init-declarator
10956
10957 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10958 function-definition as a simple-declaration.
10959
10960 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10961 parsed declaration if it is an uninitialized single declarator not followed
10962 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10963 if present, will not be consumed. */
10964
10965 static void
10966 cp_parser_simple_declaration (cp_parser* parser,
10967 bool function_definition_allowed_p,
10968 tree *maybe_range_for_decl)
10969 {
10970 cp_decl_specifier_seq decl_specifiers;
10971 int declares_class_or_enum;
10972 bool saw_declarator;
10973
10974 if (maybe_range_for_decl)
10975 *maybe_range_for_decl = NULL_TREE;
10976
10977 /* Defer access checks until we know what is being declared; the
10978 checks for names appearing in the decl-specifier-seq should be
10979 done as if we were in the scope of the thing being declared. */
10980 push_deferring_access_checks (dk_deferred);
10981
10982 /* Parse the decl-specifier-seq. We have to keep track of whether
10983 or not the decl-specifier-seq declares a named class or
10984 enumeration type, since that is the only case in which the
10985 init-declarator-list is allowed to be empty.
10986
10987 [dcl.dcl]
10988
10989 In a simple-declaration, the optional init-declarator-list can be
10990 omitted only when declaring a class or enumeration, that is when
10991 the decl-specifier-seq contains either a class-specifier, an
10992 elaborated-type-specifier, or an enum-specifier. */
10993 cp_parser_decl_specifier_seq (parser,
10994 CP_PARSER_FLAGS_OPTIONAL,
10995 &decl_specifiers,
10996 &declares_class_or_enum);
10997 /* We no longer need to defer access checks. */
10998 stop_deferring_access_checks ();
10999
11000 /* In a block scope, a valid declaration must always have a
11001 decl-specifier-seq. By not trying to parse declarators, we can
11002 resolve the declaration/expression ambiguity more quickly. */
11003 if (!function_definition_allowed_p
11004 && !decl_specifiers.any_specifiers_p)
11005 {
11006 cp_parser_error (parser, "expected declaration");
11007 goto done;
11008 }
11009
11010 /* If the next two tokens are both identifiers, the code is
11011 erroneous. The usual cause of this situation is code like:
11012
11013 T t;
11014
11015 where "T" should name a type -- but does not. */
11016 if (!decl_specifiers.any_type_specifiers_p
11017 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11018 {
11019 /* If parsing tentatively, we should commit; we really are
11020 looking at a declaration. */
11021 cp_parser_commit_to_tentative_parse (parser);
11022 /* Give up. */
11023 goto done;
11024 }
11025
11026 /* If we have seen at least one decl-specifier, and the next token
11027 is not a parenthesis, then we must be looking at a declaration.
11028 (After "int (" we might be looking at a functional cast.) */
11029 if (decl_specifiers.any_specifiers_p
11030 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11031 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11032 && !cp_parser_error_occurred (parser))
11033 cp_parser_commit_to_tentative_parse (parser);
11034
11035 /* Keep going until we hit the `;' at the end of the simple
11036 declaration. */
11037 saw_declarator = false;
11038 while (cp_lexer_next_token_is_not (parser->lexer,
11039 CPP_SEMICOLON))
11040 {
11041 cp_token *token;
11042 bool function_definition_p;
11043 tree decl;
11044
11045 if (saw_declarator)
11046 {
11047 /* If we are processing next declarator, coma is expected */
11048 token = cp_lexer_peek_token (parser->lexer);
11049 gcc_assert (token->type == CPP_COMMA);
11050 cp_lexer_consume_token (parser->lexer);
11051 if (maybe_range_for_decl)
11052 *maybe_range_for_decl = error_mark_node;
11053 }
11054 else
11055 saw_declarator = true;
11056
11057 /* Parse the init-declarator. */
11058 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11059 /*checks=*/NULL,
11060 function_definition_allowed_p,
11061 /*member_p=*/false,
11062 declares_class_or_enum,
11063 &function_definition_p,
11064 maybe_range_for_decl);
11065 /* If an error occurred while parsing tentatively, exit quickly.
11066 (That usually happens when in the body of a function; each
11067 statement is treated as a declaration-statement until proven
11068 otherwise.) */
11069 if (cp_parser_error_occurred (parser))
11070 goto done;
11071 /* Handle function definitions specially. */
11072 if (function_definition_p)
11073 {
11074 /* If the next token is a `,', then we are probably
11075 processing something like:
11076
11077 void f() {}, *p;
11078
11079 which is erroneous. */
11080 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11081 {
11082 cp_token *token = cp_lexer_peek_token (parser->lexer);
11083 error_at (token->location,
11084 "mixing"
11085 " declarations and function-definitions is forbidden");
11086 }
11087 /* Otherwise, we're done with the list of declarators. */
11088 else
11089 {
11090 pop_deferring_access_checks ();
11091 return;
11092 }
11093 }
11094 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11095 *maybe_range_for_decl = decl;
11096 /* The next token should be either a `,' or a `;'. */
11097 token = cp_lexer_peek_token (parser->lexer);
11098 /* If it's a `,', there are more declarators to come. */
11099 if (token->type == CPP_COMMA)
11100 /* will be consumed next time around */;
11101 /* If it's a `;', we are done. */
11102 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11103 break;
11104 /* Anything else is an error. */
11105 else
11106 {
11107 /* If we have already issued an error message we don't need
11108 to issue another one. */
11109 if (decl != error_mark_node
11110 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11111 cp_parser_error (parser, "expected %<,%> or %<;%>");
11112 /* Skip tokens until we reach the end of the statement. */
11113 cp_parser_skip_to_end_of_statement (parser);
11114 /* If the next token is now a `;', consume it. */
11115 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11116 cp_lexer_consume_token (parser->lexer);
11117 goto done;
11118 }
11119 /* After the first time around, a function-definition is not
11120 allowed -- even if it was OK at first. For example:
11121
11122 int i, f() {}
11123
11124 is not valid. */
11125 function_definition_allowed_p = false;
11126 }
11127
11128 /* Issue an error message if no declarators are present, and the
11129 decl-specifier-seq does not itself declare a class or
11130 enumeration: [dcl.dcl]/3. */
11131 if (!saw_declarator)
11132 {
11133 if (cp_parser_declares_only_class_p (parser))
11134 {
11135 if (!declares_class_or_enum
11136 && decl_specifiers.type
11137 && OVERLOAD_TYPE_P (decl_specifiers.type))
11138 /* Ensure an error is issued anyway when finish_decltype_type,
11139 called via cp_parser_decl_specifier_seq, returns a class or
11140 an enumeration (c++/51786). */
11141 decl_specifiers.type = NULL_TREE;
11142 shadow_tag (&decl_specifiers);
11143 }
11144 /* Perform any deferred access checks. */
11145 perform_deferred_access_checks (tf_warning_or_error);
11146 }
11147
11148 /* Consume the `;'. */
11149 if (!maybe_range_for_decl)
11150 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11151
11152 done:
11153 pop_deferring_access_checks ();
11154 }
11155
11156 /* Parse a decl-specifier-seq.
11157
11158 decl-specifier-seq:
11159 decl-specifier-seq [opt] decl-specifier
11160 decl-specifier attribute-specifier-seq [opt] (C++11)
11161
11162 decl-specifier:
11163 storage-class-specifier
11164 type-specifier
11165 function-specifier
11166 friend
11167 typedef
11168
11169 GNU Extension:
11170
11171 decl-specifier:
11172 attributes
11173
11174 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11175
11176 The parser flags FLAGS is used to control type-specifier parsing.
11177
11178 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11179 flags:
11180
11181 1: one of the decl-specifiers is an elaborated-type-specifier
11182 (i.e., a type declaration)
11183 2: one of the decl-specifiers is an enum-specifier or a
11184 class-specifier (i.e., a type definition)
11185
11186 */
11187
11188 static void
11189 cp_parser_decl_specifier_seq (cp_parser* parser,
11190 cp_parser_flags flags,
11191 cp_decl_specifier_seq *decl_specs,
11192 int* declares_class_or_enum)
11193 {
11194 bool constructor_possible_p = !parser->in_declarator_p;
11195 bool found_decl_spec = false;
11196 cp_token *start_token = NULL;
11197 cp_decl_spec ds;
11198
11199 /* Clear DECL_SPECS. */
11200 clear_decl_specs (decl_specs);
11201
11202 /* Assume no class or enumeration type is declared. */
11203 *declares_class_or_enum = 0;
11204
11205 /* Keep reading specifiers until there are no more to read. */
11206 while (true)
11207 {
11208 bool constructor_p;
11209 cp_token *token;
11210 ds = ds_last;
11211
11212 /* Peek at the next token. */
11213 token = cp_lexer_peek_token (parser->lexer);
11214
11215 /* Save the first token of the decl spec list for error
11216 reporting. */
11217 if (!start_token)
11218 start_token = token;
11219 /* Handle attributes. */
11220 if (cp_next_tokens_can_be_attribute_p (parser))
11221 {
11222 /* Parse the attributes. */
11223 tree attrs = cp_parser_attributes_opt (parser);
11224
11225 /* In a sequence of declaration specifiers, c++11 attributes
11226 appertain to the type that precede them. In that case
11227 [dcl.spec]/1 says:
11228
11229 The attribute-specifier-seq affects the type only for
11230 the declaration it appears in, not other declarations
11231 involving the same type.
11232
11233 But for now let's force the user to position the
11234 attribute either at the beginning of the declaration or
11235 after the declarator-id, which would clearly mean that it
11236 applies to the declarator. */
11237 if (cxx11_attribute_p (attrs))
11238 {
11239 if (!found_decl_spec)
11240 /* The c++11 attribute is at the beginning of the
11241 declaration. It appertains to the entity being
11242 declared. */;
11243 else
11244 {
11245 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11246 {
11247 /* This is an attribute following a
11248 class-specifier. */
11249 if (decl_specs->type_definition_p)
11250 warn_misplaced_attr_for_class_type (token->location,
11251 decl_specs->type);
11252 attrs = NULL_TREE;
11253 }
11254 else
11255 {
11256 decl_specs->std_attributes
11257 = chainon (decl_specs->std_attributes,
11258 attrs);
11259 if (decl_specs->locations[ds_std_attribute] == 0)
11260 decl_specs->locations[ds_std_attribute] = token->location;
11261 }
11262 continue;
11263 }
11264 }
11265
11266 decl_specs->attributes
11267 = chainon (decl_specs->attributes,
11268 attrs);
11269 if (decl_specs->locations[ds_attribute] == 0)
11270 decl_specs->locations[ds_attribute] = token->location;
11271 continue;
11272 }
11273 /* Assume we will find a decl-specifier keyword. */
11274 found_decl_spec = true;
11275 /* If the next token is an appropriate keyword, we can simply
11276 add it to the list. */
11277 switch (token->keyword)
11278 {
11279 /* decl-specifier:
11280 friend
11281 constexpr */
11282 case RID_FRIEND:
11283 if (!at_class_scope_p ())
11284 {
11285 error_at (token->location, "%<friend%> used outside of class");
11286 cp_lexer_purge_token (parser->lexer);
11287 }
11288 else
11289 {
11290 ds = ds_friend;
11291 /* Consume the token. */
11292 cp_lexer_consume_token (parser->lexer);
11293 }
11294 break;
11295
11296 case RID_CONSTEXPR:
11297 ds = ds_constexpr;
11298 cp_lexer_consume_token (parser->lexer);
11299 break;
11300
11301 /* function-specifier:
11302 inline
11303 virtual
11304 explicit */
11305 case RID_INLINE:
11306 case RID_VIRTUAL:
11307 case RID_EXPLICIT:
11308 cp_parser_function_specifier_opt (parser, decl_specs);
11309 break;
11310
11311 /* decl-specifier:
11312 typedef */
11313 case RID_TYPEDEF:
11314 ds = ds_typedef;
11315 /* Consume the token. */
11316 cp_lexer_consume_token (parser->lexer);
11317 /* A constructor declarator cannot appear in a typedef. */
11318 constructor_possible_p = false;
11319 /* The "typedef" keyword can only occur in a declaration; we
11320 may as well commit at this point. */
11321 cp_parser_commit_to_tentative_parse (parser);
11322
11323 if (decl_specs->storage_class != sc_none)
11324 decl_specs->conflicting_specifiers_p = true;
11325 break;
11326
11327 /* storage-class-specifier:
11328 auto
11329 register
11330 static
11331 extern
11332 mutable
11333
11334 GNU Extension:
11335 thread */
11336 case RID_AUTO:
11337 if (cxx_dialect == cxx98)
11338 {
11339 /* Consume the token. */
11340 cp_lexer_consume_token (parser->lexer);
11341
11342 /* Complain about `auto' as a storage specifier, if
11343 we're complaining about C++0x compatibility. */
11344 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11345 " changes meaning in C++11; please remove it");
11346
11347 /* Set the storage class anyway. */
11348 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11349 token);
11350 }
11351 else
11352 /* C++0x auto type-specifier. */
11353 found_decl_spec = false;
11354 break;
11355
11356 case RID_REGISTER:
11357 case RID_STATIC:
11358 case RID_EXTERN:
11359 case RID_MUTABLE:
11360 /* Consume the token. */
11361 cp_lexer_consume_token (parser->lexer);
11362 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11363 token);
11364 break;
11365 case RID_THREAD:
11366 /* Consume the token. */
11367 ds = ds_thread;
11368 cp_lexer_consume_token (parser->lexer);
11369 break;
11370
11371 default:
11372 /* We did not yet find a decl-specifier yet. */
11373 found_decl_spec = false;
11374 break;
11375 }
11376
11377 if (found_decl_spec
11378 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11379 && token->keyword != RID_CONSTEXPR)
11380 error ("decl-specifier invalid in condition");
11381
11382 if (ds != ds_last)
11383 set_and_check_decl_spec_loc (decl_specs, ds, token);
11384
11385 /* Constructors are a special case. The `S' in `S()' is not a
11386 decl-specifier; it is the beginning of the declarator. */
11387 constructor_p
11388 = (!found_decl_spec
11389 && constructor_possible_p
11390 && (cp_parser_constructor_declarator_p
11391 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11392
11393 /* If we don't have a DECL_SPEC yet, then we must be looking at
11394 a type-specifier. */
11395 if (!found_decl_spec && !constructor_p)
11396 {
11397 int decl_spec_declares_class_or_enum;
11398 bool is_cv_qualifier;
11399 tree type_spec;
11400
11401 type_spec
11402 = cp_parser_type_specifier (parser, flags,
11403 decl_specs,
11404 /*is_declaration=*/true,
11405 &decl_spec_declares_class_or_enum,
11406 &is_cv_qualifier);
11407 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11408
11409 /* If this type-specifier referenced a user-defined type
11410 (a typedef, class-name, etc.), then we can't allow any
11411 more such type-specifiers henceforth.
11412
11413 [dcl.spec]
11414
11415 The longest sequence of decl-specifiers that could
11416 possibly be a type name is taken as the
11417 decl-specifier-seq of a declaration. The sequence shall
11418 be self-consistent as described below.
11419
11420 [dcl.type]
11421
11422 As a general rule, at most one type-specifier is allowed
11423 in the complete decl-specifier-seq of a declaration. The
11424 only exceptions are the following:
11425
11426 -- const or volatile can be combined with any other
11427 type-specifier.
11428
11429 -- signed or unsigned can be combined with char, long,
11430 short, or int.
11431
11432 -- ..
11433
11434 Example:
11435
11436 typedef char* Pc;
11437 void g (const int Pc);
11438
11439 Here, Pc is *not* part of the decl-specifier seq; it's
11440 the declarator. Therefore, once we see a type-specifier
11441 (other than a cv-qualifier), we forbid any additional
11442 user-defined types. We *do* still allow things like `int
11443 int' to be considered a decl-specifier-seq, and issue the
11444 error message later. */
11445 if (type_spec && !is_cv_qualifier)
11446 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11447 /* A constructor declarator cannot follow a type-specifier. */
11448 if (type_spec)
11449 {
11450 constructor_possible_p = false;
11451 found_decl_spec = true;
11452 if (!is_cv_qualifier)
11453 decl_specs->any_type_specifiers_p = true;
11454 }
11455 }
11456
11457 /* If we still do not have a DECL_SPEC, then there are no more
11458 decl-specifiers. */
11459 if (!found_decl_spec)
11460 break;
11461
11462 decl_specs->any_specifiers_p = true;
11463 /* After we see one decl-specifier, further decl-specifiers are
11464 always optional. */
11465 flags |= CP_PARSER_FLAGS_OPTIONAL;
11466 }
11467
11468 /* Don't allow a friend specifier with a class definition. */
11469 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11470 && (*declares_class_or_enum & 2))
11471 error_at (decl_specs->locations[ds_friend],
11472 "class definition may not be declared a friend");
11473 }
11474
11475 /* Parse an (optional) storage-class-specifier.
11476
11477 storage-class-specifier:
11478 auto
11479 register
11480 static
11481 extern
11482 mutable
11483
11484 GNU Extension:
11485
11486 storage-class-specifier:
11487 thread
11488
11489 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11490
11491 static tree
11492 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11493 {
11494 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11495 {
11496 case RID_AUTO:
11497 if (cxx_dialect != cxx98)
11498 return NULL_TREE;
11499 /* Fall through for C++98. */
11500
11501 case RID_REGISTER:
11502 case RID_STATIC:
11503 case RID_EXTERN:
11504 case RID_MUTABLE:
11505 case RID_THREAD:
11506 /* Consume the token. */
11507 return cp_lexer_consume_token (parser->lexer)->u.value;
11508
11509 default:
11510 return NULL_TREE;
11511 }
11512 }
11513
11514 /* Parse an (optional) function-specifier.
11515
11516 function-specifier:
11517 inline
11518 virtual
11519 explicit
11520
11521 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11522 Updates DECL_SPECS, if it is non-NULL. */
11523
11524 static tree
11525 cp_parser_function_specifier_opt (cp_parser* parser,
11526 cp_decl_specifier_seq *decl_specs)
11527 {
11528 cp_token *token = cp_lexer_peek_token (parser->lexer);
11529 switch (token->keyword)
11530 {
11531 case RID_INLINE:
11532 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11533 break;
11534
11535 case RID_VIRTUAL:
11536 /* 14.5.2.3 [temp.mem]
11537
11538 A member function template shall not be virtual. */
11539 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11540 error_at (token->location, "templates may not be %<virtual%>");
11541 else
11542 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11543 break;
11544
11545 case RID_EXPLICIT:
11546 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11547 break;
11548
11549 default:
11550 return NULL_TREE;
11551 }
11552
11553 /* Consume the token. */
11554 return cp_lexer_consume_token (parser->lexer)->u.value;
11555 }
11556
11557 /* Parse a linkage-specification.
11558
11559 linkage-specification:
11560 extern string-literal { declaration-seq [opt] }
11561 extern string-literal declaration */
11562
11563 static void
11564 cp_parser_linkage_specification (cp_parser* parser)
11565 {
11566 tree linkage;
11567
11568 /* Look for the `extern' keyword. */
11569 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11570
11571 /* Look for the string-literal. */
11572 linkage = cp_parser_string_literal (parser, false, false);
11573
11574 /* Transform the literal into an identifier. If the literal is a
11575 wide-character string, or contains embedded NULs, then we can't
11576 handle it as the user wants. */
11577 if (strlen (TREE_STRING_POINTER (linkage))
11578 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11579 {
11580 cp_parser_error (parser, "invalid linkage-specification");
11581 /* Assume C++ linkage. */
11582 linkage = lang_name_cplusplus;
11583 }
11584 else
11585 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11586
11587 /* We're now using the new linkage. */
11588 push_lang_context (linkage);
11589
11590 /* If the next token is a `{', then we're using the first
11591 production. */
11592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11593 {
11594 cp_ensure_no_omp_declare_simd (parser);
11595
11596 /* Consume the `{' token. */
11597 cp_lexer_consume_token (parser->lexer);
11598 /* Parse the declarations. */
11599 cp_parser_declaration_seq_opt (parser);
11600 /* Look for the closing `}'. */
11601 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11602 }
11603 /* Otherwise, there's just one declaration. */
11604 else
11605 {
11606 bool saved_in_unbraced_linkage_specification_p;
11607
11608 saved_in_unbraced_linkage_specification_p
11609 = parser->in_unbraced_linkage_specification_p;
11610 parser->in_unbraced_linkage_specification_p = true;
11611 cp_parser_declaration (parser);
11612 parser->in_unbraced_linkage_specification_p
11613 = saved_in_unbraced_linkage_specification_p;
11614 }
11615
11616 /* We're done with the linkage-specification. */
11617 pop_lang_context ();
11618 }
11619
11620 /* Parse a static_assert-declaration.
11621
11622 static_assert-declaration:
11623 static_assert ( constant-expression , string-literal ) ;
11624
11625 If MEMBER_P, this static_assert is a class member. */
11626
11627 static void
11628 cp_parser_static_assert(cp_parser *parser, bool member_p)
11629 {
11630 tree condition;
11631 tree message;
11632 cp_token *token;
11633 location_t saved_loc;
11634 bool dummy;
11635
11636 /* Peek at the `static_assert' token so we can keep track of exactly
11637 where the static assertion started. */
11638 token = cp_lexer_peek_token (parser->lexer);
11639 saved_loc = token->location;
11640
11641 /* Look for the `static_assert' keyword. */
11642 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11643 RT_STATIC_ASSERT))
11644 return;
11645
11646 /* We know we are in a static assertion; commit to any tentative
11647 parse. */
11648 if (cp_parser_parsing_tentatively (parser))
11649 cp_parser_commit_to_tentative_parse (parser);
11650
11651 /* Parse the `(' starting the static assertion condition. */
11652 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11653
11654 /* Parse the constant-expression. Allow a non-constant expression
11655 here in order to give better diagnostics in finish_static_assert. */
11656 condition =
11657 cp_parser_constant_expression (parser,
11658 /*allow_non_constant_p=*/true,
11659 /*non_constant_p=*/&dummy);
11660
11661 /* Parse the separating `,'. */
11662 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11663
11664 /* Parse the string-literal message. */
11665 message = cp_parser_string_literal (parser,
11666 /*translate=*/false,
11667 /*wide_ok=*/true);
11668
11669 /* A `)' completes the static assertion. */
11670 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11671 cp_parser_skip_to_closing_parenthesis (parser,
11672 /*recovering=*/true,
11673 /*or_comma=*/false,
11674 /*consume_paren=*/true);
11675
11676 /* A semicolon terminates the declaration. */
11677 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11678
11679 /* Complete the static assertion, which may mean either processing
11680 the static assert now or saving it for template instantiation. */
11681 finish_static_assert (condition, message, saved_loc, member_p);
11682 }
11683
11684 /* Parse the expression in decltype ( expression ). */
11685
11686 static tree
11687 cp_parser_decltype_expr (cp_parser *parser,
11688 bool &id_expression_or_member_access_p)
11689 {
11690 cp_token *id_expr_start_token;
11691 tree expr;
11692
11693 /* First, try parsing an id-expression. */
11694 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11695 cp_parser_parse_tentatively (parser);
11696 expr = cp_parser_id_expression (parser,
11697 /*template_keyword_p=*/false,
11698 /*check_dependency_p=*/true,
11699 /*template_p=*/NULL,
11700 /*declarator_p=*/false,
11701 /*optional_p=*/false);
11702
11703 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11704 {
11705 bool non_integral_constant_expression_p = false;
11706 tree id_expression = expr;
11707 cp_id_kind idk;
11708 const char *error_msg;
11709
11710 if (identifier_p (expr))
11711 /* Lookup the name we got back from the id-expression. */
11712 expr = cp_parser_lookup_name_simple (parser, expr,
11713 id_expr_start_token->location);
11714
11715 if (expr
11716 && expr != error_mark_node
11717 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11718 && TREE_CODE (expr) != TYPE_DECL
11719 && (TREE_CODE (expr) != BIT_NOT_EXPR
11720 || !TYPE_P (TREE_OPERAND (expr, 0)))
11721 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11722 {
11723 /* Complete lookup of the id-expression. */
11724 expr = (finish_id_expression
11725 (id_expression, expr, parser->scope, &idk,
11726 /*integral_constant_expression_p=*/false,
11727 /*allow_non_integral_constant_expression_p=*/true,
11728 &non_integral_constant_expression_p,
11729 /*template_p=*/false,
11730 /*done=*/true,
11731 /*address_p=*/false,
11732 /*template_arg_p=*/false,
11733 &error_msg,
11734 id_expr_start_token->location));
11735
11736 if (expr == error_mark_node)
11737 /* We found an id-expression, but it was something that we
11738 should not have found. This is an error, not something
11739 we can recover from, so note that we found an
11740 id-expression and we'll recover as gracefully as
11741 possible. */
11742 id_expression_or_member_access_p = true;
11743 }
11744
11745 if (expr
11746 && expr != error_mark_node
11747 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11748 /* We have an id-expression. */
11749 id_expression_or_member_access_p = true;
11750 }
11751
11752 if (!id_expression_or_member_access_p)
11753 {
11754 /* Abort the id-expression parse. */
11755 cp_parser_abort_tentative_parse (parser);
11756
11757 /* Parsing tentatively, again. */
11758 cp_parser_parse_tentatively (parser);
11759
11760 /* Parse a class member access. */
11761 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11762 /*cast_p=*/false, /*decltype*/true,
11763 /*member_access_only_p=*/true, NULL);
11764
11765 if (expr
11766 && expr != error_mark_node
11767 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11768 /* We have an id-expression. */
11769 id_expression_or_member_access_p = true;
11770 }
11771
11772 if (id_expression_or_member_access_p)
11773 /* We have parsed the complete id-expression or member access. */
11774 cp_parser_parse_definitely (parser);
11775 else
11776 {
11777 /* Abort our attempt to parse an id-expression or member access
11778 expression. */
11779 cp_parser_abort_tentative_parse (parser);
11780
11781 /* Parse a full expression. */
11782 expr = cp_parser_expression (parser, /*cast_p=*/false,
11783 /*decltype*/true, NULL);
11784 }
11785
11786 return expr;
11787 }
11788
11789 /* Parse a `decltype' type. Returns the type.
11790
11791 simple-type-specifier:
11792 decltype ( expression )
11793 C++14 proposal:
11794 decltype ( auto ) */
11795
11796 static tree
11797 cp_parser_decltype (cp_parser *parser)
11798 {
11799 tree expr;
11800 bool id_expression_or_member_access_p = false;
11801 const char *saved_message;
11802 bool saved_integral_constant_expression_p;
11803 bool saved_non_integral_constant_expression_p;
11804 bool saved_greater_than_is_operator_p;
11805 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11806
11807 if (start_token->type == CPP_DECLTYPE)
11808 {
11809 /* Already parsed. */
11810 cp_lexer_consume_token (parser->lexer);
11811 return start_token->u.value;
11812 }
11813
11814 /* Look for the `decltype' token. */
11815 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11816 return error_mark_node;
11817
11818 /* Parse the opening `('. */
11819 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11820 return error_mark_node;
11821
11822 /* decltype (auto) */
11823 if (cxx_dialect >= cxx1y
11824 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11825 {
11826 cp_lexer_consume_token (parser->lexer);
11827 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11828 return error_mark_node;
11829 expr = make_decltype_auto ();
11830 AUTO_IS_DECLTYPE (expr) = true;
11831 goto rewrite;
11832 }
11833
11834 /* Types cannot be defined in a `decltype' expression. Save away the
11835 old message. */
11836 saved_message = parser->type_definition_forbidden_message;
11837
11838 /* And create the new one. */
11839 parser->type_definition_forbidden_message
11840 = G_("types may not be defined in %<decltype%> expressions");
11841
11842 /* The restrictions on constant-expressions do not apply inside
11843 decltype expressions. */
11844 saved_integral_constant_expression_p
11845 = parser->integral_constant_expression_p;
11846 saved_non_integral_constant_expression_p
11847 = parser->non_integral_constant_expression_p;
11848 parser->integral_constant_expression_p = false;
11849
11850 /* Within a parenthesized expression, a `>' token is always
11851 the greater-than operator. */
11852 saved_greater_than_is_operator_p
11853 = parser->greater_than_is_operator_p;
11854 parser->greater_than_is_operator_p = true;
11855
11856 /* Do not actually evaluate the expression. */
11857 ++cp_unevaluated_operand;
11858
11859 /* Do not warn about problems with the expression. */
11860 ++c_inhibit_evaluation_warnings;
11861
11862 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
11863
11864 /* Go back to evaluating expressions. */
11865 --cp_unevaluated_operand;
11866 --c_inhibit_evaluation_warnings;
11867
11868 /* The `>' token might be the end of a template-id or
11869 template-parameter-list now. */
11870 parser->greater_than_is_operator_p
11871 = saved_greater_than_is_operator_p;
11872
11873 /* Restore the old message and the integral constant expression
11874 flags. */
11875 parser->type_definition_forbidden_message = saved_message;
11876 parser->integral_constant_expression_p
11877 = saved_integral_constant_expression_p;
11878 parser->non_integral_constant_expression_p
11879 = saved_non_integral_constant_expression_p;
11880
11881 /* Parse to the closing `)'. */
11882 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11883 {
11884 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11885 /*consume_paren=*/true);
11886 return error_mark_node;
11887 }
11888
11889 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11890 tf_warning_or_error);
11891
11892 rewrite:
11893 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11894 it again. */
11895 start_token->type = CPP_DECLTYPE;
11896 start_token->u.value = expr;
11897 start_token->keyword = RID_MAX;
11898 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11899
11900 return expr;
11901 }
11902
11903 /* Special member functions [gram.special] */
11904
11905 /* Parse a conversion-function-id.
11906
11907 conversion-function-id:
11908 operator conversion-type-id
11909
11910 Returns an IDENTIFIER_NODE representing the operator. */
11911
11912 static tree
11913 cp_parser_conversion_function_id (cp_parser* parser)
11914 {
11915 tree type;
11916 tree saved_scope;
11917 tree saved_qualifying_scope;
11918 tree saved_object_scope;
11919 tree pushed_scope = NULL_TREE;
11920
11921 /* Look for the `operator' token. */
11922 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11923 return error_mark_node;
11924 /* When we parse the conversion-type-id, the current scope will be
11925 reset. However, we need that information in able to look up the
11926 conversion function later, so we save it here. */
11927 saved_scope = parser->scope;
11928 saved_qualifying_scope = parser->qualifying_scope;
11929 saved_object_scope = parser->object_scope;
11930 /* We must enter the scope of the class so that the names of
11931 entities declared within the class are available in the
11932 conversion-type-id. For example, consider:
11933
11934 struct S {
11935 typedef int I;
11936 operator I();
11937 };
11938
11939 S::operator I() { ... }
11940
11941 In order to see that `I' is a type-name in the definition, we
11942 must be in the scope of `S'. */
11943 if (saved_scope)
11944 pushed_scope = push_scope (saved_scope);
11945 /* Parse the conversion-type-id. */
11946 type = cp_parser_conversion_type_id (parser);
11947 /* Leave the scope of the class, if any. */
11948 if (pushed_scope)
11949 pop_scope (pushed_scope);
11950 /* Restore the saved scope. */
11951 parser->scope = saved_scope;
11952 parser->qualifying_scope = saved_qualifying_scope;
11953 parser->object_scope = saved_object_scope;
11954 /* If the TYPE is invalid, indicate failure. */
11955 if (type == error_mark_node)
11956 return error_mark_node;
11957 return mangle_conv_op_name_for_type (type);
11958 }
11959
11960 /* Parse a conversion-type-id:
11961
11962 conversion-type-id:
11963 type-specifier-seq conversion-declarator [opt]
11964
11965 Returns the TYPE specified. */
11966
11967 static tree
11968 cp_parser_conversion_type_id (cp_parser* parser)
11969 {
11970 tree attributes;
11971 cp_decl_specifier_seq type_specifiers;
11972 cp_declarator *declarator;
11973 tree type_specified;
11974 const char *saved_message;
11975
11976 /* Parse the attributes. */
11977 attributes = cp_parser_attributes_opt (parser);
11978
11979 saved_message = parser->type_definition_forbidden_message;
11980 parser->type_definition_forbidden_message
11981 = G_("types may not be defined in a conversion-type-id");
11982
11983 /* Parse the type-specifiers. */
11984 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11985 /*is_trailing_return=*/false,
11986 &type_specifiers);
11987
11988 parser->type_definition_forbidden_message = saved_message;
11989
11990 /* If that didn't work, stop. */
11991 if (type_specifiers.type == error_mark_node)
11992 return error_mark_node;
11993 /* Parse the conversion-declarator. */
11994 declarator = cp_parser_conversion_declarator_opt (parser);
11995
11996 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11997 /*initialized=*/0, &attributes);
11998 if (attributes)
11999 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12000
12001 /* Don't give this error when parsing tentatively. This happens to
12002 work because we always parse this definitively once. */
12003 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12004 && type_uses_auto (type_specified))
12005 {
12006 if (cxx_dialect < cxx1y)
12007 {
12008 error ("invalid use of %<auto%> in conversion operator");
12009 return error_mark_node;
12010 }
12011 else if (template_parm_scope_p ())
12012 warning (0, "use of %<auto%> in member template "
12013 "conversion operator can never be deduced");
12014 }
12015
12016 return type_specified;
12017 }
12018
12019 /* Parse an (optional) conversion-declarator.
12020
12021 conversion-declarator:
12022 ptr-operator conversion-declarator [opt]
12023
12024 */
12025
12026 static cp_declarator *
12027 cp_parser_conversion_declarator_opt (cp_parser* parser)
12028 {
12029 enum tree_code code;
12030 tree class_type, std_attributes = NULL_TREE;
12031 cp_cv_quals cv_quals;
12032
12033 /* We don't know if there's a ptr-operator next, or not. */
12034 cp_parser_parse_tentatively (parser);
12035 /* Try the ptr-operator. */
12036 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12037 &std_attributes);
12038 /* If it worked, look for more conversion-declarators. */
12039 if (cp_parser_parse_definitely (parser))
12040 {
12041 cp_declarator *declarator;
12042
12043 /* Parse another optional declarator. */
12044 declarator = cp_parser_conversion_declarator_opt (parser);
12045
12046 declarator = cp_parser_make_indirect_declarator
12047 (code, class_type, cv_quals, declarator, std_attributes);
12048
12049 return declarator;
12050 }
12051
12052 return NULL;
12053 }
12054
12055 /* Parse an (optional) ctor-initializer.
12056
12057 ctor-initializer:
12058 : mem-initializer-list
12059
12060 Returns TRUE iff the ctor-initializer was actually present. */
12061
12062 static bool
12063 cp_parser_ctor_initializer_opt (cp_parser* parser)
12064 {
12065 /* If the next token is not a `:', then there is no
12066 ctor-initializer. */
12067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12068 {
12069 /* Do default initialization of any bases and members. */
12070 if (DECL_CONSTRUCTOR_P (current_function_decl))
12071 finish_mem_initializers (NULL_TREE);
12072
12073 return false;
12074 }
12075
12076 /* Consume the `:' token. */
12077 cp_lexer_consume_token (parser->lexer);
12078 /* And the mem-initializer-list. */
12079 cp_parser_mem_initializer_list (parser);
12080
12081 return true;
12082 }
12083
12084 /* Parse a mem-initializer-list.
12085
12086 mem-initializer-list:
12087 mem-initializer ... [opt]
12088 mem-initializer ... [opt] , mem-initializer-list */
12089
12090 static void
12091 cp_parser_mem_initializer_list (cp_parser* parser)
12092 {
12093 tree mem_initializer_list = NULL_TREE;
12094 tree target_ctor = error_mark_node;
12095 cp_token *token = cp_lexer_peek_token (parser->lexer);
12096
12097 /* Let the semantic analysis code know that we are starting the
12098 mem-initializer-list. */
12099 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12100 error_at (token->location,
12101 "only constructors take member initializers");
12102
12103 /* Loop through the list. */
12104 while (true)
12105 {
12106 tree mem_initializer;
12107
12108 token = cp_lexer_peek_token (parser->lexer);
12109 /* Parse the mem-initializer. */
12110 mem_initializer = cp_parser_mem_initializer (parser);
12111 /* If the next token is a `...', we're expanding member initializers. */
12112 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12113 {
12114 /* Consume the `...'. */
12115 cp_lexer_consume_token (parser->lexer);
12116
12117 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12118 can be expanded but members cannot. */
12119 if (mem_initializer != error_mark_node
12120 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12121 {
12122 error_at (token->location,
12123 "cannot expand initializer for member %<%D%>",
12124 TREE_PURPOSE (mem_initializer));
12125 mem_initializer = error_mark_node;
12126 }
12127
12128 /* Construct the pack expansion type. */
12129 if (mem_initializer != error_mark_node)
12130 mem_initializer = make_pack_expansion (mem_initializer);
12131 }
12132 if (target_ctor != error_mark_node
12133 && mem_initializer != error_mark_node)
12134 {
12135 error ("mem-initializer for %qD follows constructor delegation",
12136 TREE_PURPOSE (mem_initializer));
12137 mem_initializer = error_mark_node;
12138 }
12139 /* Look for a target constructor. */
12140 if (mem_initializer != error_mark_node
12141 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12142 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12143 {
12144 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12145 if (mem_initializer_list)
12146 {
12147 error ("constructor delegation follows mem-initializer for %qD",
12148 TREE_PURPOSE (mem_initializer_list));
12149 mem_initializer = error_mark_node;
12150 }
12151 target_ctor = mem_initializer;
12152 }
12153 /* Add it to the list, unless it was erroneous. */
12154 if (mem_initializer != error_mark_node)
12155 {
12156 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12157 mem_initializer_list = mem_initializer;
12158 }
12159 /* If the next token is not a `,', we're done. */
12160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12161 break;
12162 /* Consume the `,' token. */
12163 cp_lexer_consume_token (parser->lexer);
12164 }
12165
12166 /* Perform semantic analysis. */
12167 if (DECL_CONSTRUCTOR_P (current_function_decl))
12168 finish_mem_initializers (mem_initializer_list);
12169 }
12170
12171 /* Parse a mem-initializer.
12172
12173 mem-initializer:
12174 mem-initializer-id ( expression-list [opt] )
12175 mem-initializer-id braced-init-list
12176
12177 GNU extension:
12178
12179 mem-initializer:
12180 ( expression-list [opt] )
12181
12182 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12183 class) or FIELD_DECL (for a non-static data member) to initialize;
12184 the TREE_VALUE is the expression-list. An empty initialization
12185 list is represented by void_list_node. */
12186
12187 static tree
12188 cp_parser_mem_initializer (cp_parser* parser)
12189 {
12190 tree mem_initializer_id;
12191 tree expression_list;
12192 tree member;
12193 cp_token *token = cp_lexer_peek_token (parser->lexer);
12194
12195 /* Find out what is being initialized. */
12196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12197 {
12198 permerror (token->location,
12199 "anachronistic old-style base class initializer");
12200 mem_initializer_id = NULL_TREE;
12201 }
12202 else
12203 {
12204 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12205 if (mem_initializer_id == error_mark_node)
12206 return mem_initializer_id;
12207 }
12208 member = expand_member_init (mem_initializer_id);
12209 if (member && !DECL_P (member))
12210 in_base_initializer = 1;
12211
12212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12213 {
12214 bool expr_non_constant_p;
12215 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12216 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12217 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12218 expression_list = build_tree_list (NULL_TREE, expression_list);
12219 }
12220 else
12221 {
12222 vec<tree, va_gc> *vec;
12223 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12224 /*cast_p=*/false,
12225 /*allow_expansion_p=*/true,
12226 /*non_constant_p=*/NULL);
12227 if (vec == NULL)
12228 return error_mark_node;
12229 expression_list = build_tree_list_vec (vec);
12230 release_tree_vector (vec);
12231 }
12232
12233 if (expression_list == error_mark_node)
12234 return error_mark_node;
12235 if (!expression_list)
12236 expression_list = void_type_node;
12237
12238 in_base_initializer = 0;
12239
12240 return member ? build_tree_list (member, expression_list) : error_mark_node;
12241 }
12242
12243 /* Parse a mem-initializer-id.
12244
12245 mem-initializer-id:
12246 :: [opt] nested-name-specifier [opt] class-name
12247 identifier
12248
12249 Returns a TYPE indicating the class to be initializer for the first
12250 production. Returns an IDENTIFIER_NODE indicating the data member
12251 to be initialized for the second production. */
12252
12253 static tree
12254 cp_parser_mem_initializer_id (cp_parser* parser)
12255 {
12256 bool global_scope_p;
12257 bool nested_name_specifier_p;
12258 bool template_p = false;
12259 tree id;
12260
12261 cp_token *token = cp_lexer_peek_token (parser->lexer);
12262
12263 /* `typename' is not allowed in this context ([temp.res]). */
12264 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12265 {
12266 error_at (token->location,
12267 "keyword %<typename%> not allowed in this context (a qualified "
12268 "member initializer is implicitly a type)");
12269 cp_lexer_consume_token (parser->lexer);
12270 }
12271 /* Look for the optional `::' operator. */
12272 global_scope_p
12273 = (cp_parser_global_scope_opt (parser,
12274 /*current_scope_valid_p=*/false)
12275 != NULL_TREE);
12276 /* Look for the optional nested-name-specifier. The simplest way to
12277 implement:
12278
12279 [temp.res]
12280
12281 The keyword `typename' is not permitted in a base-specifier or
12282 mem-initializer; in these contexts a qualified name that
12283 depends on a template-parameter is implicitly assumed to be a
12284 type name.
12285
12286 is to assume that we have seen the `typename' keyword at this
12287 point. */
12288 nested_name_specifier_p
12289 = (cp_parser_nested_name_specifier_opt (parser,
12290 /*typename_keyword_p=*/true,
12291 /*check_dependency_p=*/true,
12292 /*type_p=*/true,
12293 /*is_declaration=*/true)
12294 != NULL_TREE);
12295 if (nested_name_specifier_p)
12296 template_p = cp_parser_optional_template_keyword (parser);
12297 /* If there is a `::' operator or a nested-name-specifier, then we
12298 are definitely looking for a class-name. */
12299 if (global_scope_p || nested_name_specifier_p)
12300 return cp_parser_class_name (parser,
12301 /*typename_keyword_p=*/true,
12302 /*template_keyword_p=*/template_p,
12303 typename_type,
12304 /*check_dependency_p=*/true,
12305 /*class_head_p=*/false,
12306 /*is_declaration=*/true);
12307 /* Otherwise, we could also be looking for an ordinary identifier. */
12308 cp_parser_parse_tentatively (parser);
12309 /* Try a class-name. */
12310 id = cp_parser_class_name (parser,
12311 /*typename_keyword_p=*/true,
12312 /*template_keyword_p=*/false,
12313 none_type,
12314 /*check_dependency_p=*/true,
12315 /*class_head_p=*/false,
12316 /*is_declaration=*/true);
12317 /* If we found one, we're done. */
12318 if (cp_parser_parse_definitely (parser))
12319 return id;
12320 /* Otherwise, look for an ordinary identifier. */
12321 return cp_parser_identifier (parser);
12322 }
12323
12324 /* Overloading [gram.over] */
12325
12326 /* Parse an operator-function-id.
12327
12328 operator-function-id:
12329 operator operator
12330
12331 Returns an IDENTIFIER_NODE for the operator which is a
12332 human-readable spelling of the identifier, e.g., `operator +'. */
12333
12334 static tree
12335 cp_parser_operator_function_id (cp_parser* parser)
12336 {
12337 /* Look for the `operator' keyword. */
12338 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12339 return error_mark_node;
12340 /* And then the name of the operator itself. */
12341 return cp_parser_operator (parser);
12342 }
12343
12344 /* Return an identifier node for a user-defined literal operator.
12345 The suffix identifier is chained to the operator name identifier. */
12346
12347 static tree
12348 cp_literal_operator_id (const char* name)
12349 {
12350 tree identifier;
12351 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12352 + strlen (name) + 10);
12353 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12354 identifier = get_identifier (buffer);
12355
12356 return identifier;
12357 }
12358
12359 /* Parse an operator.
12360
12361 operator:
12362 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12363 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12364 || ++ -- , ->* -> () []
12365
12366 GNU Extensions:
12367
12368 operator:
12369 <? >? <?= >?=
12370
12371 Returns an IDENTIFIER_NODE for the operator which is a
12372 human-readable spelling of the identifier, e.g., `operator +'. */
12373
12374 static tree
12375 cp_parser_operator (cp_parser* parser)
12376 {
12377 tree id = NULL_TREE;
12378 cp_token *token;
12379 bool bad_encoding_prefix = false;
12380
12381 /* Peek at the next token. */
12382 token = cp_lexer_peek_token (parser->lexer);
12383 /* Figure out which operator we have. */
12384 switch (token->type)
12385 {
12386 case CPP_KEYWORD:
12387 {
12388 enum tree_code op;
12389
12390 /* The keyword should be either `new' or `delete'. */
12391 if (token->keyword == RID_NEW)
12392 op = NEW_EXPR;
12393 else if (token->keyword == RID_DELETE)
12394 op = DELETE_EXPR;
12395 else
12396 break;
12397
12398 /* Consume the `new' or `delete' token. */
12399 cp_lexer_consume_token (parser->lexer);
12400
12401 /* Peek at the next token. */
12402 token = cp_lexer_peek_token (parser->lexer);
12403 /* If it's a `[' token then this is the array variant of the
12404 operator. */
12405 if (token->type == CPP_OPEN_SQUARE)
12406 {
12407 /* Consume the `[' token. */
12408 cp_lexer_consume_token (parser->lexer);
12409 /* Look for the `]' token. */
12410 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12411 id = ansi_opname (op == NEW_EXPR
12412 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12413 }
12414 /* Otherwise, we have the non-array variant. */
12415 else
12416 id = ansi_opname (op);
12417
12418 return id;
12419 }
12420
12421 case CPP_PLUS:
12422 id = ansi_opname (PLUS_EXPR);
12423 break;
12424
12425 case CPP_MINUS:
12426 id = ansi_opname (MINUS_EXPR);
12427 break;
12428
12429 case CPP_MULT:
12430 id = ansi_opname (MULT_EXPR);
12431 break;
12432
12433 case CPP_DIV:
12434 id = ansi_opname (TRUNC_DIV_EXPR);
12435 break;
12436
12437 case CPP_MOD:
12438 id = ansi_opname (TRUNC_MOD_EXPR);
12439 break;
12440
12441 case CPP_XOR:
12442 id = ansi_opname (BIT_XOR_EXPR);
12443 break;
12444
12445 case CPP_AND:
12446 id = ansi_opname (BIT_AND_EXPR);
12447 break;
12448
12449 case CPP_OR:
12450 id = ansi_opname (BIT_IOR_EXPR);
12451 break;
12452
12453 case CPP_COMPL:
12454 id = ansi_opname (BIT_NOT_EXPR);
12455 break;
12456
12457 case CPP_NOT:
12458 id = ansi_opname (TRUTH_NOT_EXPR);
12459 break;
12460
12461 case CPP_EQ:
12462 id = ansi_assopname (NOP_EXPR);
12463 break;
12464
12465 case CPP_LESS:
12466 id = ansi_opname (LT_EXPR);
12467 break;
12468
12469 case CPP_GREATER:
12470 id = ansi_opname (GT_EXPR);
12471 break;
12472
12473 case CPP_PLUS_EQ:
12474 id = ansi_assopname (PLUS_EXPR);
12475 break;
12476
12477 case CPP_MINUS_EQ:
12478 id = ansi_assopname (MINUS_EXPR);
12479 break;
12480
12481 case CPP_MULT_EQ:
12482 id = ansi_assopname (MULT_EXPR);
12483 break;
12484
12485 case CPP_DIV_EQ:
12486 id = ansi_assopname (TRUNC_DIV_EXPR);
12487 break;
12488
12489 case CPP_MOD_EQ:
12490 id = ansi_assopname (TRUNC_MOD_EXPR);
12491 break;
12492
12493 case CPP_XOR_EQ:
12494 id = ansi_assopname (BIT_XOR_EXPR);
12495 break;
12496
12497 case CPP_AND_EQ:
12498 id = ansi_assopname (BIT_AND_EXPR);
12499 break;
12500
12501 case CPP_OR_EQ:
12502 id = ansi_assopname (BIT_IOR_EXPR);
12503 break;
12504
12505 case CPP_LSHIFT:
12506 id = ansi_opname (LSHIFT_EXPR);
12507 break;
12508
12509 case CPP_RSHIFT:
12510 id = ansi_opname (RSHIFT_EXPR);
12511 break;
12512
12513 case CPP_LSHIFT_EQ:
12514 id = ansi_assopname (LSHIFT_EXPR);
12515 break;
12516
12517 case CPP_RSHIFT_EQ:
12518 id = ansi_assopname (RSHIFT_EXPR);
12519 break;
12520
12521 case CPP_EQ_EQ:
12522 id = ansi_opname (EQ_EXPR);
12523 break;
12524
12525 case CPP_NOT_EQ:
12526 id = ansi_opname (NE_EXPR);
12527 break;
12528
12529 case CPP_LESS_EQ:
12530 id = ansi_opname (LE_EXPR);
12531 break;
12532
12533 case CPP_GREATER_EQ:
12534 id = ansi_opname (GE_EXPR);
12535 break;
12536
12537 case CPP_AND_AND:
12538 id = ansi_opname (TRUTH_ANDIF_EXPR);
12539 break;
12540
12541 case CPP_OR_OR:
12542 id = ansi_opname (TRUTH_ORIF_EXPR);
12543 break;
12544
12545 case CPP_PLUS_PLUS:
12546 id = ansi_opname (POSTINCREMENT_EXPR);
12547 break;
12548
12549 case CPP_MINUS_MINUS:
12550 id = ansi_opname (PREDECREMENT_EXPR);
12551 break;
12552
12553 case CPP_COMMA:
12554 id = ansi_opname (COMPOUND_EXPR);
12555 break;
12556
12557 case CPP_DEREF_STAR:
12558 id = ansi_opname (MEMBER_REF);
12559 break;
12560
12561 case CPP_DEREF:
12562 id = ansi_opname (COMPONENT_REF);
12563 break;
12564
12565 case CPP_OPEN_PAREN:
12566 /* Consume the `('. */
12567 cp_lexer_consume_token (parser->lexer);
12568 /* Look for the matching `)'. */
12569 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12570 return ansi_opname (CALL_EXPR);
12571
12572 case CPP_OPEN_SQUARE:
12573 /* Consume the `['. */
12574 cp_lexer_consume_token (parser->lexer);
12575 /* Look for the matching `]'. */
12576 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12577 return ansi_opname (ARRAY_REF);
12578
12579 case CPP_WSTRING:
12580 case CPP_STRING16:
12581 case CPP_STRING32:
12582 case CPP_UTF8STRING:
12583 bad_encoding_prefix = true;
12584 /* Fall through. */
12585
12586 case CPP_STRING:
12587 if (cxx_dialect == cxx98)
12588 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12589 if (bad_encoding_prefix)
12590 {
12591 error ("invalid encoding prefix in literal operator");
12592 return error_mark_node;
12593 }
12594 if (TREE_STRING_LENGTH (token->u.value) > 2)
12595 {
12596 error ("expected empty string after %<operator%> keyword");
12597 return error_mark_node;
12598 }
12599 /* Consume the string. */
12600 cp_lexer_consume_token (parser->lexer);
12601 /* Look for the suffix identifier. */
12602 token = cp_lexer_peek_token (parser->lexer);
12603 if (token->type == CPP_NAME)
12604 {
12605 id = cp_parser_identifier (parser);
12606 if (id != error_mark_node)
12607 {
12608 const char *name = IDENTIFIER_POINTER (id);
12609 return cp_literal_operator_id (name);
12610 }
12611 }
12612 else if (token->type == CPP_KEYWORD)
12613 {
12614 error ("unexpected keyword;"
12615 " remove space between quotes and suffix identifier");
12616 return error_mark_node;
12617 }
12618 else
12619 {
12620 error ("expected suffix identifier");
12621 return error_mark_node;
12622 }
12623
12624 case CPP_WSTRING_USERDEF:
12625 case CPP_STRING16_USERDEF:
12626 case CPP_STRING32_USERDEF:
12627 case CPP_UTF8STRING_USERDEF:
12628 bad_encoding_prefix = true;
12629 /* Fall through. */
12630
12631 case CPP_STRING_USERDEF:
12632 if (cxx_dialect == cxx98)
12633 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12634 if (bad_encoding_prefix)
12635 {
12636 error ("invalid encoding prefix in literal operator");
12637 return error_mark_node;
12638 }
12639 {
12640 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12641 if (TREE_STRING_LENGTH (string_tree) > 2)
12642 {
12643 error ("expected empty string after %<operator%> keyword");
12644 return error_mark_node;
12645 }
12646 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12647 /* Consume the user-defined string literal. */
12648 cp_lexer_consume_token (parser->lexer);
12649 if (id != error_mark_node)
12650 {
12651 const char *name = IDENTIFIER_POINTER (id);
12652 return cp_literal_operator_id (name);
12653 }
12654 else
12655 return error_mark_node;
12656 }
12657
12658 default:
12659 /* Anything else is an error. */
12660 break;
12661 }
12662
12663 /* If we have selected an identifier, we need to consume the
12664 operator token. */
12665 if (id)
12666 cp_lexer_consume_token (parser->lexer);
12667 /* Otherwise, no valid operator name was present. */
12668 else
12669 {
12670 cp_parser_error (parser, "expected operator");
12671 id = error_mark_node;
12672 }
12673
12674 return id;
12675 }
12676
12677 /* Parse a template-declaration.
12678
12679 template-declaration:
12680 export [opt] template < template-parameter-list > declaration
12681
12682 If MEMBER_P is TRUE, this template-declaration occurs within a
12683 class-specifier.
12684
12685 The grammar rule given by the standard isn't correct. What
12686 is really meant is:
12687
12688 template-declaration:
12689 export [opt] template-parameter-list-seq
12690 decl-specifier-seq [opt] init-declarator [opt] ;
12691 export [opt] template-parameter-list-seq
12692 function-definition
12693
12694 template-parameter-list-seq:
12695 template-parameter-list-seq [opt]
12696 template < template-parameter-list > */
12697
12698 static void
12699 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12700 {
12701 /* Check for `export'. */
12702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12703 {
12704 /* Consume the `export' token. */
12705 cp_lexer_consume_token (parser->lexer);
12706 /* Warn that we do not support `export'. */
12707 warning (0, "keyword %<export%> not implemented, and will be ignored");
12708 }
12709
12710 cp_parser_template_declaration_after_export (parser, member_p);
12711 }
12712
12713 /* Parse a template-parameter-list.
12714
12715 template-parameter-list:
12716 template-parameter
12717 template-parameter-list , template-parameter
12718
12719 Returns a TREE_LIST. Each node represents a template parameter.
12720 The nodes are connected via their TREE_CHAINs. */
12721
12722 static tree
12723 cp_parser_template_parameter_list (cp_parser* parser)
12724 {
12725 tree parameter_list = NULL_TREE;
12726
12727 begin_template_parm_list ();
12728
12729 /* The loop below parses the template parms. We first need to know
12730 the total number of template parms to be able to compute proper
12731 canonical types of each dependent type. So after the loop, when
12732 we know the total number of template parms,
12733 end_template_parm_list computes the proper canonical types and
12734 fixes up the dependent types accordingly. */
12735 while (true)
12736 {
12737 tree parameter;
12738 bool is_non_type;
12739 bool is_parameter_pack;
12740 location_t parm_loc;
12741
12742 /* Parse the template-parameter. */
12743 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12744 parameter = cp_parser_template_parameter (parser,
12745 &is_non_type,
12746 &is_parameter_pack);
12747 /* Add it to the list. */
12748 if (parameter != error_mark_node)
12749 parameter_list = process_template_parm (parameter_list,
12750 parm_loc,
12751 parameter,
12752 is_non_type,
12753 is_parameter_pack);
12754 else
12755 {
12756 tree err_parm = build_tree_list (parameter, parameter);
12757 parameter_list = chainon (parameter_list, err_parm);
12758 }
12759
12760 /* If the next token is not a `,', we're done. */
12761 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12762 break;
12763 /* Otherwise, consume the `,' token. */
12764 cp_lexer_consume_token (parser->lexer);
12765 }
12766
12767 return end_template_parm_list (parameter_list);
12768 }
12769
12770 /* Parse a template-parameter.
12771
12772 template-parameter:
12773 type-parameter
12774 parameter-declaration
12775
12776 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12777 the parameter. The TREE_PURPOSE is the default value, if any.
12778 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12779 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12780 set to true iff this parameter is a parameter pack. */
12781
12782 static tree
12783 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12784 bool *is_parameter_pack)
12785 {
12786 cp_token *token;
12787 cp_parameter_declarator *parameter_declarator;
12788 cp_declarator *id_declarator;
12789 tree parm;
12790
12791 /* Assume it is a type parameter or a template parameter. */
12792 *is_non_type = false;
12793 /* Assume it not a parameter pack. */
12794 *is_parameter_pack = false;
12795 /* Peek at the next token. */
12796 token = cp_lexer_peek_token (parser->lexer);
12797 /* If it is `class' or `template', we have a type-parameter. */
12798 if (token->keyword == RID_TEMPLATE)
12799 return cp_parser_type_parameter (parser, is_parameter_pack);
12800 /* If it is `class' or `typename' we do not know yet whether it is a
12801 type parameter or a non-type parameter. Consider:
12802
12803 template <typename T, typename T::X X> ...
12804
12805 or:
12806
12807 template <class C, class D*> ...
12808
12809 Here, the first parameter is a type parameter, and the second is
12810 a non-type parameter. We can tell by looking at the token after
12811 the identifier -- if it is a `,', `=', or `>' then we have a type
12812 parameter. */
12813 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12814 {
12815 /* Peek at the token after `class' or `typename'. */
12816 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12817 /* If it's an ellipsis, we have a template type parameter
12818 pack. */
12819 if (token->type == CPP_ELLIPSIS)
12820 return cp_parser_type_parameter (parser, is_parameter_pack);
12821 /* If it's an identifier, skip it. */
12822 if (token->type == CPP_NAME)
12823 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12824 /* Now, see if the token looks like the end of a template
12825 parameter. */
12826 if (token->type == CPP_COMMA
12827 || token->type == CPP_EQ
12828 || token->type == CPP_GREATER)
12829 return cp_parser_type_parameter (parser, is_parameter_pack);
12830 }
12831
12832 /* Otherwise, it is a non-type parameter.
12833
12834 [temp.param]
12835
12836 When parsing a default template-argument for a non-type
12837 template-parameter, the first non-nested `>' is taken as the end
12838 of the template parameter-list rather than a greater-than
12839 operator. */
12840 *is_non_type = true;
12841 parameter_declarator
12842 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12843 /*parenthesized_p=*/NULL);
12844
12845 /* If the parameter declaration is marked as a parameter pack, set
12846 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12847 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12848 grokdeclarator. */
12849 if (parameter_declarator
12850 && parameter_declarator->declarator
12851 && parameter_declarator->declarator->parameter_pack_p)
12852 {
12853 *is_parameter_pack = true;
12854 parameter_declarator->declarator->parameter_pack_p = false;
12855 }
12856
12857 if (parameter_declarator
12858 && parameter_declarator->default_argument)
12859 {
12860 /* Can happen in some cases of erroneous input (c++/34892). */
12861 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12862 /* Consume the `...' for better error recovery. */
12863 cp_lexer_consume_token (parser->lexer);
12864 }
12865 /* If the next token is an ellipsis, and we don't already have it
12866 marked as a parameter pack, then we have a parameter pack (that
12867 has no declarator). */
12868 else if (!*is_parameter_pack
12869 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12870 && (declarator_can_be_parameter_pack
12871 (parameter_declarator->declarator)))
12872 {
12873 /* Consume the `...'. */
12874 cp_lexer_consume_token (parser->lexer);
12875 maybe_warn_variadic_templates ();
12876
12877 *is_parameter_pack = true;
12878 }
12879 /* We might end up with a pack expansion as the type of the non-type
12880 template parameter, in which case this is a non-type template
12881 parameter pack. */
12882 else if (parameter_declarator
12883 && parameter_declarator->decl_specifiers.type
12884 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12885 {
12886 *is_parameter_pack = true;
12887 parameter_declarator->decl_specifiers.type =
12888 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12889 }
12890
12891 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12892 {
12893 /* Parameter packs cannot have default arguments. However, a
12894 user may try to do so, so we'll parse them and give an
12895 appropriate diagnostic here. */
12896
12897 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12898
12899 /* Find the name of the parameter pack. */
12900 id_declarator = parameter_declarator->declarator;
12901 while (id_declarator && id_declarator->kind != cdk_id)
12902 id_declarator = id_declarator->declarator;
12903
12904 if (id_declarator && id_declarator->kind == cdk_id)
12905 error_at (start_token->location,
12906 "template parameter pack %qD cannot have a default argument",
12907 id_declarator->u.id.unqualified_name);
12908 else
12909 error_at (start_token->location,
12910 "template parameter pack cannot have a default argument");
12911
12912 /* Parse the default argument, but throw away the result. */
12913 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12914 }
12915
12916 parm = grokdeclarator (parameter_declarator->declarator,
12917 &parameter_declarator->decl_specifiers,
12918 TPARM, /*initialized=*/0,
12919 /*attrlist=*/NULL);
12920 if (parm == error_mark_node)
12921 return error_mark_node;
12922
12923 return build_tree_list (parameter_declarator->default_argument, parm);
12924 }
12925
12926 /* Parse a type-parameter.
12927
12928 type-parameter:
12929 class identifier [opt]
12930 class identifier [opt] = type-id
12931 typename identifier [opt]
12932 typename identifier [opt] = type-id
12933 template < template-parameter-list > class identifier [opt]
12934 template < template-parameter-list > class identifier [opt]
12935 = id-expression
12936
12937 GNU Extension (variadic templates):
12938
12939 type-parameter:
12940 class ... identifier [opt]
12941 typename ... identifier [opt]
12942
12943 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12944 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12945 the declaration of the parameter.
12946
12947 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12948
12949 static tree
12950 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12951 {
12952 cp_token *token;
12953 tree parameter;
12954
12955 /* Look for a keyword to tell us what kind of parameter this is. */
12956 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12957 if (!token)
12958 return error_mark_node;
12959
12960 switch (token->keyword)
12961 {
12962 case RID_CLASS:
12963 case RID_TYPENAME:
12964 {
12965 tree identifier;
12966 tree default_argument;
12967
12968 /* If the next token is an ellipsis, we have a template
12969 argument pack. */
12970 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12971 {
12972 /* Consume the `...' token. */
12973 cp_lexer_consume_token (parser->lexer);
12974 maybe_warn_variadic_templates ();
12975
12976 *is_parameter_pack = true;
12977 }
12978
12979 /* If the next token is an identifier, then it names the
12980 parameter. */
12981 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12982 identifier = cp_parser_identifier (parser);
12983 else
12984 identifier = NULL_TREE;
12985
12986 /* Create the parameter. */
12987 parameter = finish_template_type_parm (class_type_node, identifier);
12988
12989 /* If the next token is an `=', we have a default argument. */
12990 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12991 {
12992 /* Consume the `=' token. */
12993 cp_lexer_consume_token (parser->lexer);
12994 /* Parse the default-argument. */
12995 push_deferring_access_checks (dk_no_deferred);
12996 default_argument = cp_parser_type_id (parser);
12997
12998 /* Template parameter packs cannot have default
12999 arguments. */
13000 if (*is_parameter_pack)
13001 {
13002 if (identifier)
13003 error_at (token->location,
13004 "template parameter pack %qD cannot have a "
13005 "default argument", identifier);
13006 else
13007 error_at (token->location,
13008 "template parameter packs cannot have "
13009 "default arguments");
13010 default_argument = NULL_TREE;
13011 }
13012 pop_deferring_access_checks ();
13013 }
13014 else
13015 default_argument = NULL_TREE;
13016
13017 /* Create the combined representation of the parameter and the
13018 default argument. */
13019 parameter = build_tree_list (default_argument, parameter);
13020 }
13021 break;
13022
13023 case RID_TEMPLATE:
13024 {
13025 tree identifier;
13026 tree default_argument;
13027
13028 /* Look for the `<'. */
13029 cp_parser_require (parser, CPP_LESS, RT_LESS);
13030 /* Parse the template-parameter-list. */
13031 cp_parser_template_parameter_list (parser);
13032 /* Look for the `>'. */
13033 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13034 /* Look for the `class' keyword. */
13035 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13036 /* If the next token is an ellipsis, we have a template
13037 argument pack. */
13038 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13039 {
13040 /* Consume the `...' token. */
13041 cp_lexer_consume_token (parser->lexer);
13042 maybe_warn_variadic_templates ();
13043
13044 *is_parameter_pack = true;
13045 }
13046 /* If the next token is an `=', then there is a
13047 default-argument. If the next token is a `>', we are at
13048 the end of the parameter-list. If the next token is a `,',
13049 then we are at the end of this parameter. */
13050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13051 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13052 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13053 {
13054 identifier = cp_parser_identifier (parser);
13055 /* Treat invalid names as if the parameter were nameless. */
13056 if (identifier == error_mark_node)
13057 identifier = NULL_TREE;
13058 }
13059 else
13060 identifier = NULL_TREE;
13061
13062 /* Create the template parameter. */
13063 parameter = finish_template_template_parm (class_type_node,
13064 identifier);
13065
13066 /* If the next token is an `=', then there is a
13067 default-argument. */
13068 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13069 {
13070 bool is_template;
13071
13072 /* Consume the `='. */
13073 cp_lexer_consume_token (parser->lexer);
13074 /* Parse the id-expression. */
13075 push_deferring_access_checks (dk_no_deferred);
13076 /* save token before parsing the id-expression, for error
13077 reporting */
13078 token = cp_lexer_peek_token (parser->lexer);
13079 default_argument
13080 = cp_parser_id_expression (parser,
13081 /*template_keyword_p=*/false,
13082 /*check_dependency_p=*/true,
13083 /*template_p=*/&is_template,
13084 /*declarator_p=*/false,
13085 /*optional_p=*/false);
13086 if (TREE_CODE (default_argument) == TYPE_DECL)
13087 /* If the id-expression was a template-id that refers to
13088 a template-class, we already have the declaration here,
13089 so no further lookup is needed. */
13090 ;
13091 else
13092 /* Look up the name. */
13093 default_argument
13094 = cp_parser_lookup_name (parser, default_argument,
13095 none_type,
13096 /*is_template=*/is_template,
13097 /*is_namespace=*/false,
13098 /*check_dependency=*/true,
13099 /*ambiguous_decls=*/NULL,
13100 token->location);
13101 /* See if the default argument is valid. */
13102 default_argument
13103 = check_template_template_default_arg (default_argument);
13104
13105 /* Template parameter packs cannot have default
13106 arguments. */
13107 if (*is_parameter_pack)
13108 {
13109 if (identifier)
13110 error_at (token->location,
13111 "template parameter pack %qD cannot "
13112 "have a default argument",
13113 identifier);
13114 else
13115 error_at (token->location, "template parameter packs cannot "
13116 "have default arguments");
13117 default_argument = NULL_TREE;
13118 }
13119 pop_deferring_access_checks ();
13120 }
13121 else
13122 default_argument = NULL_TREE;
13123
13124 /* Create the combined representation of the parameter and the
13125 default argument. */
13126 parameter = build_tree_list (default_argument, parameter);
13127 }
13128 break;
13129
13130 default:
13131 gcc_unreachable ();
13132 break;
13133 }
13134
13135 return parameter;
13136 }
13137
13138 /* Parse a template-id.
13139
13140 template-id:
13141 template-name < template-argument-list [opt] >
13142
13143 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13144 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13145 returned. Otherwise, if the template-name names a function, or set
13146 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13147 names a class, returns a TYPE_DECL for the specialization.
13148
13149 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13150 uninstantiated templates. */
13151
13152 static tree
13153 cp_parser_template_id (cp_parser *parser,
13154 bool template_keyword_p,
13155 bool check_dependency_p,
13156 enum tag_types tag_type,
13157 bool is_declaration)
13158 {
13159 int i;
13160 tree templ;
13161 tree arguments;
13162 tree template_id;
13163 cp_token_position start_of_id = 0;
13164 deferred_access_check *chk;
13165 vec<deferred_access_check, va_gc> *access_check;
13166 cp_token *next_token = NULL, *next_token_2 = NULL;
13167 bool is_identifier;
13168
13169 /* If the next token corresponds to a template-id, there is no need
13170 to reparse it. */
13171 next_token = cp_lexer_peek_token (parser->lexer);
13172 if (next_token->type == CPP_TEMPLATE_ID)
13173 {
13174 struct tree_check *check_value;
13175
13176 /* Get the stored value. */
13177 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13178 /* Perform any access checks that were deferred. */
13179 access_check = check_value->checks;
13180 if (access_check)
13181 {
13182 FOR_EACH_VEC_ELT (*access_check, i, chk)
13183 perform_or_defer_access_check (chk->binfo,
13184 chk->decl,
13185 chk->diag_decl,
13186 tf_warning_or_error);
13187 }
13188 /* Return the stored value. */
13189 return check_value->value;
13190 }
13191
13192 /* Avoid performing name lookup if there is no possibility of
13193 finding a template-id. */
13194 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13195 || (next_token->type == CPP_NAME
13196 && !cp_parser_nth_token_starts_template_argument_list_p
13197 (parser, 2)))
13198 {
13199 cp_parser_error (parser, "expected template-id");
13200 return error_mark_node;
13201 }
13202
13203 /* Remember where the template-id starts. */
13204 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13205 start_of_id = cp_lexer_token_position (parser->lexer, false);
13206
13207 push_deferring_access_checks (dk_deferred);
13208
13209 /* Parse the template-name. */
13210 is_identifier = false;
13211 templ = cp_parser_template_name (parser, template_keyword_p,
13212 check_dependency_p,
13213 is_declaration,
13214 tag_type,
13215 &is_identifier);
13216 if (templ == error_mark_node || is_identifier)
13217 {
13218 pop_deferring_access_checks ();
13219 return templ;
13220 }
13221
13222 /* If we find the sequence `[:' after a template-name, it's probably
13223 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13224 parse correctly the argument list. */
13225 next_token = cp_lexer_peek_token (parser->lexer);
13226 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13227 if (next_token->type == CPP_OPEN_SQUARE
13228 && next_token->flags & DIGRAPH
13229 && next_token_2->type == CPP_COLON
13230 && !(next_token_2->flags & PREV_WHITE))
13231 {
13232 cp_parser_parse_tentatively (parser);
13233 /* Change `:' into `::'. */
13234 next_token_2->type = CPP_SCOPE;
13235 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13236 CPP_LESS. */
13237 cp_lexer_consume_token (parser->lexer);
13238
13239 /* Parse the arguments. */
13240 arguments = cp_parser_enclosed_template_argument_list (parser);
13241 if (!cp_parser_parse_definitely (parser))
13242 {
13243 /* If we couldn't parse an argument list, then we revert our changes
13244 and return simply an error. Maybe this is not a template-id
13245 after all. */
13246 next_token_2->type = CPP_COLON;
13247 cp_parser_error (parser, "expected %<<%>");
13248 pop_deferring_access_checks ();
13249 return error_mark_node;
13250 }
13251 /* Otherwise, emit an error about the invalid digraph, but continue
13252 parsing because we got our argument list. */
13253 if (permerror (next_token->location,
13254 "%<<::%> cannot begin a template-argument list"))
13255 {
13256 static bool hint = false;
13257 inform (next_token->location,
13258 "%<<:%> is an alternate spelling for %<[%>."
13259 " Insert whitespace between %<<%> and %<::%>");
13260 if (!hint && !flag_permissive)
13261 {
13262 inform (next_token->location, "(if you use %<-fpermissive%> "
13263 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13264 "accept your code)");
13265 hint = true;
13266 }
13267 }
13268 }
13269 else
13270 {
13271 /* Look for the `<' that starts the template-argument-list. */
13272 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13273 {
13274 pop_deferring_access_checks ();
13275 return error_mark_node;
13276 }
13277 /* Parse the arguments. */
13278 arguments = cp_parser_enclosed_template_argument_list (parser);
13279 }
13280
13281 /* Build a representation of the specialization. */
13282 if (identifier_p (templ))
13283 template_id = build_min_nt_loc (next_token->location,
13284 TEMPLATE_ID_EXPR,
13285 templ, arguments);
13286 else if (DECL_TYPE_TEMPLATE_P (templ)
13287 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13288 {
13289 bool entering_scope;
13290 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13291 template (rather than some instantiation thereof) only if
13292 is not nested within some other construct. For example, in
13293 "template <typename T> void f(T) { A<T>::", A<T> is just an
13294 instantiation of A. */
13295 entering_scope = (template_parm_scope_p ()
13296 && cp_lexer_next_token_is (parser->lexer,
13297 CPP_SCOPE));
13298 template_id
13299 = finish_template_type (templ, arguments, entering_scope);
13300 }
13301 else
13302 {
13303 /* If it's not a class-template or a template-template, it should be
13304 a function-template. */
13305 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13306 || TREE_CODE (templ) == OVERLOAD
13307 || BASELINK_P (templ)));
13308
13309 template_id = lookup_template_function (templ, arguments);
13310 }
13311
13312 /* If parsing tentatively, replace the sequence of tokens that makes
13313 up the template-id with a CPP_TEMPLATE_ID token. That way,
13314 should we re-parse the token stream, we will not have to repeat
13315 the effort required to do the parse, nor will we issue duplicate
13316 error messages about problems during instantiation of the
13317 template. */
13318 if (start_of_id)
13319 {
13320 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13321
13322 /* Reset the contents of the START_OF_ID token. */
13323 token->type = CPP_TEMPLATE_ID;
13324 /* Retrieve any deferred checks. Do not pop this access checks yet
13325 so the memory will not be reclaimed during token replacing below. */
13326 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13327 token->u.tree_check_value->value = template_id;
13328 token->u.tree_check_value->checks = get_deferred_access_checks ();
13329 token->keyword = RID_MAX;
13330
13331 /* Purge all subsequent tokens. */
13332 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13333
13334 /* ??? Can we actually assume that, if template_id ==
13335 error_mark_node, we will have issued a diagnostic to the
13336 user, as opposed to simply marking the tentative parse as
13337 failed? */
13338 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13339 error_at (token->location, "parse error in template argument list");
13340 }
13341
13342 pop_to_parent_deferring_access_checks ();
13343 return template_id;
13344 }
13345
13346 /* Parse a template-name.
13347
13348 template-name:
13349 identifier
13350
13351 The standard should actually say:
13352
13353 template-name:
13354 identifier
13355 operator-function-id
13356
13357 A defect report has been filed about this issue.
13358
13359 A conversion-function-id cannot be a template name because they cannot
13360 be part of a template-id. In fact, looking at this code:
13361
13362 a.operator K<int>()
13363
13364 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13365 It is impossible to call a templated conversion-function-id with an
13366 explicit argument list, since the only allowed template parameter is
13367 the type to which it is converting.
13368
13369 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13370 `template' keyword, in a construction like:
13371
13372 T::template f<3>()
13373
13374 In that case `f' is taken to be a template-name, even though there
13375 is no way of knowing for sure.
13376
13377 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13378 name refers to a set of overloaded functions, at least one of which
13379 is a template, or an IDENTIFIER_NODE with the name of the template,
13380 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13381 names are looked up inside uninstantiated templates. */
13382
13383 static tree
13384 cp_parser_template_name (cp_parser* parser,
13385 bool template_keyword_p,
13386 bool check_dependency_p,
13387 bool is_declaration,
13388 enum tag_types tag_type,
13389 bool *is_identifier)
13390 {
13391 tree identifier;
13392 tree decl;
13393 tree fns;
13394 cp_token *token = cp_lexer_peek_token (parser->lexer);
13395
13396 /* If the next token is `operator', then we have either an
13397 operator-function-id or a conversion-function-id. */
13398 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13399 {
13400 /* We don't know whether we're looking at an
13401 operator-function-id or a conversion-function-id. */
13402 cp_parser_parse_tentatively (parser);
13403 /* Try an operator-function-id. */
13404 identifier = cp_parser_operator_function_id (parser);
13405 /* If that didn't work, try a conversion-function-id. */
13406 if (!cp_parser_parse_definitely (parser))
13407 {
13408 cp_parser_error (parser, "expected template-name");
13409 return error_mark_node;
13410 }
13411 }
13412 /* Look for the identifier. */
13413 else
13414 identifier = cp_parser_identifier (parser);
13415
13416 /* If we didn't find an identifier, we don't have a template-id. */
13417 if (identifier == error_mark_node)
13418 return error_mark_node;
13419
13420 /* If the name immediately followed the `template' keyword, then it
13421 is a template-name. However, if the next token is not `<', then
13422 we do not treat it as a template-name, since it is not being used
13423 as part of a template-id. This enables us to handle constructs
13424 like:
13425
13426 template <typename T> struct S { S(); };
13427 template <typename T> S<T>::S();
13428
13429 correctly. We would treat `S' as a template -- if it were `S<T>'
13430 -- but we do not if there is no `<'. */
13431
13432 if (processing_template_decl
13433 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13434 {
13435 /* In a declaration, in a dependent context, we pretend that the
13436 "template" keyword was present in order to improve error
13437 recovery. For example, given:
13438
13439 template <typename T> void f(T::X<int>);
13440
13441 we want to treat "X<int>" as a template-id. */
13442 if (is_declaration
13443 && !template_keyword_p
13444 && parser->scope && TYPE_P (parser->scope)
13445 && check_dependency_p
13446 && dependent_scope_p (parser->scope)
13447 /* Do not do this for dtors (or ctors), since they never
13448 need the template keyword before their name. */
13449 && !constructor_name_p (identifier, parser->scope))
13450 {
13451 cp_token_position start = 0;
13452
13453 /* Explain what went wrong. */
13454 error_at (token->location, "non-template %qD used as template",
13455 identifier);
13456 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13457 parser->scope, identifier);
13458 /* If parsing tentatively, find the location of the "<" token. */
13459 if (cp_parser_simulate_error (parser))
13460 start = cp_lexer_token_position (parser->lexer, true);
13461 /* Parse the template arguments so that we can issue error
13462 messages about them. */
13463 cp_lexer_consume_token (parser->lexer);
13464 cp_parser_enclosed_template_argument_list (parser);
13465 /* Skip tokens until we find a good place from which to
13466 continue parsing. */
13467 cp_parser_skip_to_closing_parenthesis (parser,
13468 /*recovering=*/true,
13469 /*or_comma=*/true,
13470 /*consume_paren=*/false);
13471 /* If parsing tentatively, permanently remove the
13472 template argument list. That will prevent duplicate
13473 error messages from being issued about the missing
13474 "template" keyword. */
13475 if (start)
13476 cp_lexer_purge_tokens_after (parser->lexer, start);
13477 if (is_identifier)
13478 *is_identifier = true;
13479 return identifier;
13480 }
13481
13482 /* If the "template" keyword is present, then there is generally
13483 no point in doing name-lookup, so we just return IDENTIFIER.
13484 But, if the qualifying scope is non-dependent then we can
13485 (and must) do name-lookup normally. */
13486 if (template_keyword_p
13487 && (!parser->scope
13488 || (TYPE_P (parser->scope)
13489 && dependent_type_p (parser->scope))))
13490 return identifier;
13491 }
13492
13493 /* Look up the name. */
13494 decl = cp_parser_lookup_name (parser, identifier,
13495 tag_type,
13496 /*is_template=*/true,
13497 /*is_namespace=*/false,
13498 check_dependency_p,
13499 /*ambiguous_decls=*/NULL,
13500 token->location);
13501
13502 /* If DECL is a template, then the name was a template-name. */
13503 if (TREE_CODE (decl) == TEMPLATE_DECL)
13504 ;
13505 else
13506 {
13507 tree fn = NULL_TREE;
13508
13509 /* The standard does not explicitly indicate whether a name that
13510 names a set of overloaded declarations, some of which are
13511 templates, is a template-name. However, such a name should
13512 be a template-name; otherwise, there is no way to form a
13513 template-id for the overloaded templates. */
13514 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13515 if (TREE_CODE (fns) == OVERLOAD)
13516 for (fn = fns; fn; fn = OVL_NEXT (fn))
13517 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13518 break;
13519
13520 if (!fn)
13521 {
13522 /* The name does not name a template. */
13523 cp_parser_error (parser, "expected template-name");
13524 return error_mark_node;
13525 }
13526 }
13527
13528 /* If DECL is dependent, and refers to a function, then just return
13529 its name; we will look it up again during template instantiation. */
13530 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13531 {
13532 tree scope = ovl_scope (decl);
13533 if (TYPE_P (scope) && dependent_type_p (scope))
13534 return identifier;
13535 }
13536
13537 return decl;
13538 }
13539
13540 /* Parse a template-argument-list.
13541
13542 template-argument-list:
13543 template-argument ... [opt]
13544 template-argument-list , template-argument ... [opt]
13545
13546 Returns a TREE_VEC containing the arguments. */
13547
13548 static tree
13549 cp_parser_template_argument_list (cp_parser* parser)
13550 {
13551 tree fixed_args[10];
13552 unsigned n_args = 0;
13553 unsigned alloced = 10;
13554 tree *arg_ary = fixed_args;
13555 tree vec;
13556 bool saved_in_template_argument_list_p;
13557 bool saved_ice_p;
13558 bool saved_non_ice_p;
13559
13560 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13561 parser->in_template_argument_list_p = true;
13562 /* Even if the template-id appears in an integral
13563 constant-expression, the contents of the argument list do
13564 not. */
13565 saved_ice_p = parser->integral_constant_expression_p;
13566 parser->integral_constant_expression_p = false;
13567 saved_non_ice_p = parser->non_integral_constant_expression_p;
13568 parser->non_integral_constant_expression_p = false;
13569
13570 /* Parse the arguments. */
13571 do
13572 {
13573 tree argument;
13574
13575 if (n_args)
13576 /* Consume the comma. */
13577 cp_lexer_consume_token (parser->lexer);
13578
13579 /* Parse the template-argument. */
13580 argument = cp_parser_template_argument (parser);
13581
13582 /* If the next token is an ellipsis, we're expanding a template
13583 argument pack. */
13584 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13585 {
13586 if (argument == error_mark_node)
13587 {
13588 cp_token *token = cp_lexer_peek_token (parser->lexer);
13589 error_at (token->location,
13590 "expected parameter pack before %<...%>");
13591 }
13592 /* Consume the `...' token. */
13593 cp_lexer_consume_token (parser->lexer);
13594
13595 /* Make the argument into a TYPE_PACK_EXPANSION or
13596 EXPR_PACK_EXPANSION. */
13597 argument = make_pack_expansion (argument);
13598 }
13599
13600 if (n_args == alloced)
13601 {
13602 alloced *= 2;
13603
13604 if (arg_ary == fixed_args)
13605 {
13606 arg_ary = XNEWVEC (tree, alloced);
13607 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13608 }
13609 else
13610 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13611 }
13612 arg_ary[n_args++] = argument;
13613 }
13614 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13615
13616 vec = make_tree_vec (n_args);
13617
13618 while (n_args--)
13619 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13620
13621 if (arg_ary != fixed_args)
13622 free (arg_ary);
13623 parser->non_integral_constant_expression_p = saved_non_ice_p;
13624 parser->integral_constant_expression_p = saved_ice_p;
13625 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13626 #ifdef ENABLE_CHECKING
13627 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13628 #endif
13629 return vec;
13630 }
13631
13632 /* Parse a template-argument.
13633
13634 template-argument:
13635 assignment-expression
13636 type-id
13637 id-expression
13638
13639 The representation is that of an assignment-expression, type-id, or
13640 id-expression -- except that the qualified id-expression is
13641 evaluated, so that the value returned is either a DECL or an
13642 OVERLOAD.
13643
13644 Although the standard says "assignment-expression", it forbids
13645 throw-expressions or assignments in the template argument.
13646 Therefore, we use "conditional-expression" instead. */
13647
13648 static tree
13649 cp_parser_template_argument (cp_parser* parser)
13650 {
13651 tree argument;
13652 bool template_p;
13653 bool address_p;
13654 bool maybe_type_id = false;
13655 cp_token *token = NULL, *argument_start_token = NULL;
13656 location_t loc = 0;
13657 cp_id_kind idk;
13658
13659 /* There's really no way to know what we're looking at, so we just
13660 try each alternative in order.
13661
13662 [temp.arg]
13663
13664 In a template-argument, an ambiguity between a type-id and an
13665 expression is resolved to a type-id, regardless of the form of
13666 the corresponding template-parameter.
13667
13668 Therefore, we try a type-id first. */
13669 cp_parser_parse_tentatively (parser);
13670 argument = cp_parser_template_type_arg (parser);
13671 /* If there was no error parsing the type-id but the next token is a
13672 '>>', our behavior depends on which dialect of C++ we're
13673 parsing. In C++98, we probably found a typo for '> >'. But there
13674 are type-id which are also valid expressions. For instance:
13675
13676 struct X { int operator >> (int); };
13677 template <int V> struct Foo {};
13678 Foo<X () >> 5> r;
13679
13680 Here 'X()' is a valid type-id of a function type, but the user just
13681 wanted to write the expression "X() >> 5". Thus, we remember that we
13682 found a valid type-id, but we still try to parse the argument as an
13683 expression to see what happens.
13684
13685 In C++0x, the '>>' will be considered two separate '>'
13686 tokens. */
13687 if (!cp_parser_error_occurred (parser)
13688 && cxx_dialect == cxx98
13689 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13690 {
13691 maybe_type_id = true;
13692 cp_parser_abort_tentative_parse (parser);
13693 }
13694 else
13695 {
13696 /* If the next token isn't a `,' or a `>', then this argument wasn't
13697 really finished. This means that the argument is not a valid
13698 type-id. */
13699 if (!cp_parser_next_token_ends_template_argument_p (parser))
13700 cp_parser_error (parser, "expected template-argument");
13701 /* If that worked, we're done. */
13702 if (cp_parser_parse_definitely (parser))
13703 return argument;
13704 }
13705 /* We're still not sure what the argument will be. */
13706 cp_parser_parse_tentatively (parser);
13707 /* Try a template. */
13708 argument_start_token = cp_lexer_peek_token (parser->lexer);
13709 argument = cp_parser_id_expression (parser,
13710 /*template_keyword_p=*/false,
13711 /*check_dependency_p=*/true,
13712 &template_p,
13713 /*declarator_p=*/false,
13714 /*optional_p=*/false);
13715 /* If the next token isn't a `,' or a `>', then this argument wasn't
13716 really finished. */
13717 if (!cp_parser_next_token_ends_template_argument_p (parser))
13718 cp_parser_error (parser, "expected template-argument");
13719 if (!cp_parser_error_occurred (parser))
13720 {
13721 /* Figure out what is being referred to. If the id-expression
13722 was for a class template specialization, then we will have a
13723 TYPE_DECL at this point. There is no need to do name lookup
13724 at this point in that case. */
13725 if (TREE_CODE (argument) != TYPE_DECL)
13726 argument = cp_parser_lookup_name (parser, argument,
13727 none_type,
13728 /*is_template=*/template_p,
13729 /*is_namespace=*/false,
13730 /*check_dependency=*/true,
13731 /*ambiguous_decls=*/NULL,
13732 argument_start_token->location);
13733 if (TREE_CODE (argument) != TEMPLATE_DECL
13734 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13735 cp_parser_error (parser, "expected template-name");
13736 }
13737 if (cp_parser_parse_definitely (parser))
13738 return argument;
13739 /* It must be a non-type argument. There permitted cases are given
13740 in [temp.arg.nontype]:
13741
13742 -- an integral constant-expression of integral or enumeration
13743 type; or
13744
13745 -- the name of a non-type template-parameter; or
13746
13747 -- the name of an object or function with external linkage...
13748
13749 -- the address of an object or function with external linkage...
13750
13751 -- a pointer to member... */
13752 /* Look for a non-type template parameter. */
13753 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13754 {
13755 cp_parser_parse_tentatively (parser);
13756 argument = cp_parser_primary_expression (parser,
13757 /*address_p=*/false,
13758 /*cast_p=*/false,
13759 /*template_arg_p=*/true,
13760 &idk);
13761 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13762 || !cp_parser_next_token_ends_template_argument_p (parser))
13763 cp_parser_simulate_error (parser);
13764 if (cp_parser_parse_definitely (parser))
13765 return argument;
13766 }
13767
13768 /* If the next token is "&", the argument must be the address of an
13769 object or function with external linkage. */
13770 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13771 if (address_p)
13772 {
13773 loc = cp_lexer_peek_token (parser->lexer)->location;
13774 cp_lexer_consume_token (parser->lexer);
13775 }
13776 /* See if we might have an id-expression. */
13777 token = cp_lexer_peek_token (parser->lexer);
13778 if (token->type == CPP_NAME
13779 || token->keyword == RID_OPERATOR
13780 || token->type == CPP_SCOPE
13781 || token->type == CPP_TEMPLATE_ID
13782 || token->type == CPP_NESTED_NAME_SPECIFIER)
13783 {
13784 cp_parser_parse_tentatively (parser);
13785 argument = cp_parser_primary_expression (parser,
13786 address_p,
13787 /*cast_p=*/false,
13788 /*template_arg_p=*/true,
13789 &idk);
13790 if (cp_parser_error_occurred (parser)
13791 || !cp_parser_next_token_ends_template_argument_p (parser))
13792 cp_parser_abort_tentative_parse (parser);
13793 else
13794 {
13795 tree probe;
13796
13797 if (INDIRECT_REF_P (argument))
13798 {
13799 gcc_assert (REFERENCE_REF_P (argument));
13800 argument = TREE_OPERAND (argument, 0);
13801 }
13802
13803 /* If we're in a template, we represent a qualified-id referring
13804 to a static data member as a SCOPE_REF even if the scope isn't
13805 dependent so that we can check access control later. */
13806 probe = argument;
13807 if (TREE_CODE (probe) == SCOPE_REF)
13808 probe = TREE_OPERAND (probe, 1);
13809 if (VAR_P (probe))
13810 {
13811 /* A variable without external linkage might still be a
13812 valid constant-expression, so no error is issued here
13813 if the external-linkage check fails. */
13814 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13815 cp_parser_simulate_error (parser);
13816 }
13817 else if (is_overloaded_fn (argument))
13818 /* All overloaded functions are allowed; if the external
13819 linkage test does not pass, an error will be issued
13820 later. */
13821 ;
13822 else if (address_p
13823 && (TREE_CODE (argument) == OFFSET_REF
13824 || TREE_CODE (argument) == SCOPE_REF))
13825 /* A pointer-to-member. */
13826 ;
13827 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13828 ;
13829 else
13830 cp_parser_simulate_error (parser);
13831
13832 if (cp_parser_parse_definitely (parser))
13833 {
13834 if (address_p)
13835 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13836 tf_warning_or_error);
13837 return argument;
13838 }
13839 }
13840 }
13841 /* If the argument started with "&", there are no other valid
13842 alternatives at this point. */
13843 if (address_p)
13844 {
13845 cp_parser_error (parser, "invalid non-type template argument");
13846 return error_mark_node;
13847 }
13848
13849 /* If the argument wasn't successfully parsed as a type-id followed
13850 by '>>', the argument can only be a constant expression now.
13851 Otherwise, we try parsing the constant-expression tentatively,
13852 because the argument could really be a type-id. */
13853 if (maybe_type_id)
13854 cp_parser_parse_tentatively (parser);
13855 argument = cp_parser_constant_expression (parser,
13856 /*allow_non_constant_p=*/false,
13857 /*non_constant_p=*/NULL);
13858 if (!maybe_type_id)
13859 return argument;
13860 if (!cp_parser_next_token_ends_template_argument_p (parser))
13861 cp_parser_error (parser, "expected template-argument");
13862 if (cp_parser_parse_definitely (parser))
13863 return argument;
13864 /* We did our best to parse the argument as a non type-id, but that
13865 was the only alternative that matched (albeit with a '>' after
13866 it). We can assume it's just a typo from the user, and a
13867 diagnostic will then be issued. */
13868 return cp_parser_template_type_arg (parser);
13869 }
13870
13871 /* Parse an explicit-instantiation.
13872
13873 explicit-instantiation:
13874 template declaration
13875
13876 Although the standard says `declaration', what it really means is:
13877
13878 explicit-instantiation:
13879 template decl-specifier-seq [opt] declarator [opt] ;
13880
13881 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13882 supposed to be allowed. A defect report has been filed about this
13883 issue.
13884
13885 GNU Extension:
13886
13887 explicit-instantiation:
13888 storage-class-specifier template
13889 decl-specifier-seq [opt] declarator [opt] ;
13890 function-specifier template
13891 decl-specifier-seq [opt] declarator [opt] ; */
13892
13893 static void
13894 cp_parser_explicit_instantiation (cp_parser* parser)
13895 {
13896 int declares_class_or_enum;
13897 cp_decl_specifier_seq decl_specifiers;
13898 tree extension_specifier = NULL_TREE;
13899
13900 timevar_push (TV_TEMPLATE_INST);
13901
13902 /* Look for an (optional) storage-class-specifier or
13903 function-specifier. */
13904 if (cp_parser_allow_gnu_extensions_p (parser))
13905 {
13906 extension_specifier
13907 = cp_parser_storage_class_specifier_opt (parser);
13908 if (!extension_specifier)
13909 extension_specifier
13910 = cp_parser_function_specifier_opt (parser,
13911 /*decl_specs=*/NULL);
13912 }
13913
13914 /* Look for the `template' keyword. */
13915 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13916 /* Let the front end know that we are processing an explicit
13917 instantiation. */
13918 begin_explicit_instantiation ();
13919 /* [temp.explicit] says that we are supposed to ignore access
13920 control while processing explicit instantiation directives. */
13921 push_deferring_access_checks (dk_no_check);
13922 /* Parse a decl-specifier-seq. */
13923 cp_parser_decl_specifier_seq (parser,
13924 CP_PARSER_FLAGS_OPTIONAL,
13925 &decl_specifiers,
13926 &declares_class_or_enum);
13927 /* If there was exactly one decl-specifier, and it declared a class,
13928 and there's no declarator, then we have an explicit type
13929 instantiation. */
13930 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13931 {
13932 tree type;
13933
13934 type = check_tag_decl (&decl_specifiers,
13935 /*explicit_type_instantiation_p=*/true);
13936 /* Turn access control back on for names used during
13937 template instantiation. */
13938 pop_deferring_access_checks ();
13939 if (type)
13940 do_type_instantiation (type, extension_specifier,
13941 /*complain=*/tf_error);
13942 }
13943 else
13944 {
13945 cp_declarator *declarator;
13946 tree decl;
13947
13948 /* Parse the declarator. */
13949 declarator
13950 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13951 /*ctor_dtor_or_conv_p=*/NULL,
13952 /*parenthesized_p=*/NULL,
13953 /*member_p=*/false);
13954 if (declares_class_or_enum & 2)
13955 cp_parser_check_for_definition_in_return_type (declarator,
13956 decl_specifiers.type,
13957 decl_specifiers.locations[ds_type_spec]);
13958 if (declarator != cp_error_declarator)
13959 {
13960 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13961 permerror (decl_specifiers.locations[ds_inline],
13962 "explicit instantiation shall not use"
13963 " %<inline%> specifier");
13964 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13965 permerror (decl_specifiers.locations[ds_constexpr],
13966 "explicit instantiation shall not use"
13967 " %<constexpr%> specifier");
13968
13969 decl = grokdeclarator (declarator, &decl_specifiers,
13970 NORMAL, 0, &decl_specifiers.attributes);
13971 /* Turn access control back on for names used during
13972 template instantiation. */
13973 pop_deferring_access_checks ();
13974 /* Do the explicit instantiation. */
13975 do_decl_instantiation (decl, extension_specifier);
13976 }
13977 else
13978 {
13979 pop_deferring_access_checks ();
13980 /* Skip the body of the explicit instantiation. */
13981 cp_parser_skip_to_end_of_statement (parser);
13982 }
13983 }
13984 /* We're done with the instantiation. */
13985 end_explicit_instantiation ();
13986
13987 cp_parser_consume_semicolon_at_end_of_statement (parser);
13988
13989 timevar_pop (TV_TEMPLATE_INST);
13990 }
13991
13992 /* Parse an explicit-specialization.
13993
13994 explicit-specialization:
13995 template < > declaration
13996
13997 Although the standard says `declaration', what it really means is:
13998
13999 explicit-specialization:
14000 template <> decl-specifier [opt] init-declarator [opt] ;
14001 template <> function-definition
14002 template <> explicit-specialization
14003 template <> template-declaration */
14004
14005 static void
14006 cp_parser_explicit_specialization (cp_parser* parser)
14007 {
14008 bool need_lang_pop;
14009 cp_token *token = cp_lexer_peek_token (parser->lexer);
14010
14011 /* Look for the `template' keyword. */
14012 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14013 /* Look for the `<'. */
14014 cp_parser_require (parser, CPP_LESS, RT_LESS);
14015 /* Look for the `>'. */
14016 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14017 /* We have processed another parameter list. */
14018 ++parser->num_template_parameter_lists;
14019 /* [temp]
14020
14021 A template ... explicit specialization ... shall not have C
14022 linkage. */
14023 if (current_lang_name == lang_name_c)
14024 {
14025 error_at (token->location, "template specialization with C linkage");
14026 /* Give it C++ linkage to avoid confusing other parts of the
14027 front end. */
14028 push_lang_context (lang_name_cplusplus);
14029 need_lang_pop = true;
14030 }
14031 else
14032 need_lang_pop = false;
14033 /* Let the front end know that we are beginning a specialization. */
14034 if (!begin_specialization ())
14035 {
14036 end_specialization ();
14037 return;
14038 }
14039
14040 /* If the next keyword is `template', we need to figure out whether
14041 or not we're looking a template-declaration. */
14042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14043 {
14044 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14045 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14046 cp_parser_template_declaration_after_export (parser,
14047 /*member_p=*/false);
14048 else
14049 cp_parser_explicit_specialization (parser);
14050 }
14051 else
14052 /* Parse the dependent declaration. */
14053 cp_parser_single_declaration (parser,
14054 /*checks=*/NULL,
14055 /*member_p=*/false,
14056 /*explicit_specialization_p=*/true,
14057 /*friend_p=*/NULL);
14058 /* We're done with the specialization. */
14059 end_specialization ();
14060 /* For the erroneous case of a template with C linkage, we pushed an
14061 implicit C++ linkage scope; exit that scope now. */
14062 if (need_lang_pop)
14063 pop_lang_context ();
14064 /* We're done with this parameter list. */
14065 --parser->num_template_parameter_lists;
14066 }
14067
14068 /* Parse a type-specifier.
14069
14070 type-specifier:
14071 simple-type-specifier
14072 class-specifier
14073 enum-specifier
14074 elaborated-type-specifier
14075 cv-qualifier
14076
14077 GNU Extension:
14078
14079 type-specifier:
14080 __complex__
14081
14082 Returns a representation of the type-specifier. For a
14083 class-specifier, enum-specifier, or elaborated-type-specifier, a
14084 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14085
14086 The parser flags FLAGS is used to control type-specifier parsing.
14087
14088 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14089 in a decl-specifier-seq.
14090
14091 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14092 class-specifier, enum-specifier, or elaborated-type-specifier, then
14093 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14094 if a type is declared; 2 if it is defined. Otherwise, it is set to
14095 zero.
14096
14097 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14098 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14099 is set to FALSE. */
14100
14101 static tree
14102 cp_parser_type_specifier (cp_parser* parser,
14103 cp_parser_flags flags,
14104 cp_decl_specifier_seq *decl_specs,
14105 bool is_declaration,
14106 int* declares_class_or_enum,
14107 bool* is_cv_qualifier)
14108 {
14109 tree type_spec = NULL_TREE;
14110 cp_token *token;
14111 enum rid keyword;
14112 cp_decl_spec ds = ds_last;
14113
14114 /* Assume this type-specifier does not declare a new type. */
14115 if (declares_class_or_enum)
14116 *declares_class_or_enum = 0;
14117 /* And that it does not specify a cv-qualifier. */
14118 if (is_cv_qualifier)
14119 *is_cv_qualifier = false;
14120 /* Peek at the next token. */
14121 token = cp_lexer_peek_token (parser->lexer);
14122
14123 /* If we're looking at a keyword, we can use that to guide the
14124 production we choose. */
14125 keyword = token->keyword;
14126 switch (keyword)
14127 {
14128 case RID_ENUM:
14129 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14130 goto elaborated_type_specifier;
14131
14132 /* Look for the enum-specifier. */
14133 type_spec = cp_parser_enum_specifier (parser);
14134 /* If that worked, we're done. */
14135 if (type_spec)
14136 {
14137 if (declares_class_or_enum)
14138 *declares_class_or_enum = 2;
14139 if (decl_specs)
14140 cp_parser_set_decl_spec_type (decl_specs,
14141 type_spec,
14142 token,
14143 /*type_definition_p=*/true);
14144 return type_spec;
14145 }
14146 else
14147 goto elaborated_type_specifier;
14148
14149 /* Any of these indicate either a class-specifier, or an
14150 elaborated-type-specifier. */
14151 case RID_CLASS:
14152 case RID_STRUCT:
14153 case RID_UNION:
14154 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14155 goto elaborated_type_specifier;
14156
14157 /* Parse tentatively so that we can back up if we don't find a
14158 class-specifier. */
14159 cp_parser_parse_tentatively (parser);
14160 /* Look for the class-specifier. */
14161 type_spec = cp_parser_class_specifier (parser);
14162 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14163 /* If that worked, we're done. */
14164 if (cp_parser_parse_definitely (parser))
14165 {
14166 if (declares_class_or_enum)
14167 *declares_class_or_enum = 2;
14168 if (decl_specs)
14169 cp_parser_set_decl_spec_type (decl_specs,
14170 type_spec,
14171 token,
14172 /*type_definition_p=*/true);
14173 return type_spec;
14174 }
14175
14176 /* Fall through. */
14177 elaborated_type_specifier:
14178 /* We're declaring (not defining) a class or enum. */
14179 if (declares_class_or_enum)
14180 *declares_class_or_enum = 1;
14181
14182 /* Fall through. */
14183 case RID_TYPENAME:
14184 /* Look for an elaborated-type-specifier. */
14185 type_spec
14186 = (cp_parser_elaborated_type_specifier
14187 (parser,
14188 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14189 is_declaration));
14190 if (decl_specs)
14191 cp_parser_set_decl_spec_type (decl_specs,
14192 type_spec,
14193 token,
14194 /*type_definition_p=*/false);
14195 return type_spec;
14196
14197 case RID_CONST:
14198 ds = ds_const;
14199 if (is_cv_qualifier)
14200 *is_cv_qualifier = true;
14201 break;
14202
14203 case RID_VOLATILE:
14204 ds = ds_volatile;
14205 if (is_cv_qualifier)
14206 *is_cv_qualifier = true;
14207 break;
14208
14209 case RID_RESTRICT:
14210 ds = ds_restrict;
14211 if (is_cv_qualifier)
14212 *is_cv_qualifier = true;
14213 break;
14214
14215 case RID_COMPLEX:
14216 /* The `__complex__' keyword is a GNU extension. */
14217 ds = ds_complex;
14218 break;
14219
14220 default:
14221 break;
14222 }
14223
14224 /* Handle simple keywords. */
14225 if (ds != ds_last)
14226 {
14227 if (decl_specs)
14228 {
14229 set_and_check_decl_spec_loc (decl_specs, ds, token);
14230 decl_specs->any_specifiers_p = true;
14231 }
14232 return cp_lexer_consume_token (parser->lexer)->u.value;
14233 }
14234
14235 /* If we do not already have a type-specifier, assume we are looking
14236 at a simple-type-specifier. */
14237 type_spec = cp_parser_simple_type_specifier (parser,
14238 decl_specs,
14239 flags);
14240
14241 /* If we didn't find a type-specifier, and a type-specifier was not
14242 optional in this context, issue an error message. */
14243 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14244 {
14245 cp_parser_error (parser, "expected type specifier");
14246 return error_mark_node;
14247 }
14248
14249 return type_spec;
14250 }
14251
14252 /* Parse a simple-type-specifier.
14253
14254 simple-type-specifier:
14255 :: [opt] nested-name-specifier [opt] type-name
14256 :: [opt] nested-name-specifier template template-id
14257 char
14258 wchar_t
14259 bool
14260 short
14261 int
14262 long
14263 signed
14264 unsigned
14265 float
14266 double
14267 void
14268
14269 C++0x Extension:
14270
14271 simple-type-specifier:
14272 auto
14273 decltype ( expression )
14274 char16_t
14275 char32_t
14276 __underlying_type ( type-id )
14277
14278 GNU Extension:
14279
14280 simple-type-specifier:
14281 __int128
14282 __typeof__ unary-expression
14283 __typeof__ ( type-id )
14284 __typeof__ ( type-id ) { initializer-list , [opt] }
14285
14286 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14287 appropriately updated. */
14288
14289 static tree
14290 cp_parser_simple_type_specifier (cp_parser* parser,
14291 cp_decl_specifier_seq *decl_specs,
14292 cp_parser_flags flags)
14293 {
14294 tree type = NULL_TREE;
14295 cp_token *token;
14296
14297 /* Peek at the next token. */
14298 token = cp_lexer_peek_token (parser->lexer);
14299
14300 /* If we're looking at a keyword, things are easy. */
14301 switch (token->keyword)
14302 {
14303 case RID_CHAR:
14304 if (decl_specs)
14305 decl_specs->explicit_char_p = true;
14306 type = char_type_node;
14307 break;
14308 case RID_CHAR16:
14309 type = char16_type_node;
14310 break;
14311 case RID_CHAR32:
14312 type = char32_type_node;
14313 break;
14314 case RID_WCHAR:
14315 type = wchar_type_node;
14316 break;
14317 case RID_BOOL:
14318 type = boolean_type_node;
14319 break;
14320 case RID_SHORT:
14321 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14322 type = short_integer_type_node;
14323 break;
14324 case RID_INT:
14325 if (decl_specs)
14326 decl_specs->explicit_int_p = true;
14327 type = integer_type_node;
14328 break;
14329 case RID_INT128:
14330 if (!int128_integer_type_node)
14331 break;
14332 if (decl_specs)
14333 decl_specs->explicit_int128_p = true;
14334 type = int128_integer_type_node;
14335 break;
14336 case RID_LONG:
14337 if (decl_specs)
14338 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14339 type = long_integer_type_node;
14340 break;
14341 case RID_SIGNED:
14342 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14343 type = integer_type_node;
14344 break;
14345 case RID_UNSIGNED:
14346 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14347 type = unsigned_type_node;
14348 break;
14349 case RID_FLOAT:
14350 type = float_type_node;
14351 break;
14352 case RID_DOUBLE:
14353 type = double_type_node;
14354 break;
14355 case RID_VOID:
14356 type = void_type_node;
14357 break;
14358
14359 case RID_AUTO:
14360 maybe_warn_cpp0x (CPP0X_AUTO);
14361 type = make_auto ();
14362 break;
14363
14364 case RID_DECLTYPE:
14365 /* Since DR 743, decltype can either be a simple-type-specifier by
14366 itself or begin a nested-name-specifier. Parsing it will replace
14367 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14368 handling below decide what to do. */
14369 cp_parser_decltype (parser);
14370 cp_lexer_set_token_position (parser->lexer, token);
14371 break;
14372
14373 case RID_TYPEOF:
14374 /* Consume the `typeof' token. */
14375 cp_lexer_consume_token (parser->lexer);
14376 /* Parse the operand to `typeof'. */
14377 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14378 /* If it is not already a TYPE, take its type. */
14379 if (!TYPE_P (type))
14380 type = finish_typeof (type);
14381
14382 if (decl_specs)
14383 cp_parser_set_decl_spec_type (decl_specs, type,
14384 token,
14385 /*type_definition_p=*/false);
14386
14387 return type;
14388
14389 case RID_UNDERLYING_TYPE:
14390 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14391 if (decl_specs)
14392 cp_parser_set_decl_spec_type (decl_specs, type,
14393 token,
14394 /*type_definition_p=*/false);
14395
14396 return type;
14397
14398 case RID_BASES:
14399 case RID_DIRECT_BASES:
14400 type = cp_parser_trait_expr (parser, token->keyword);
14401 if (decl_specs)
14402 cp_parser_set_decl_spec_type (decl_specs, type,
14403 token,
14404 /*type_definition_p=*/false);
14405 return type;
14406 default:
14407 break;
14408 }
14409
14410 /* If token is an already-parsed decltype not followed by ::,
14411 it's a simple-type-specifier. */
14412 if (token->type == CPP_DECLTYPE
14413 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14414 {
14415 type = token->u.value;
14416 if (decl_specs)
14417 cp_parser_set_decl_spec_type (decl_specs, type,
14418 token,
14419 /*type_definition_p=*/false);
14420 cp_lexer_consume_token (parser->lexer);
14421 return type;
14422 }
14423
14424 /* If the type-specifier was for a built-in type, we're done. */
14425 if (type)
14426 {
14427 /* Record the type. */
14428 if (decl_specs
14429 && (token->keyword != RID_SIGNED
14430 && token->keyword != RID_UNSIGNED
14431 && token->keyword != RID_SHORT
14432 && token->keyword != RID_LONG))
14433 cp_parser_set_decl_spec_type (decl_specs,
14434 type,
14435 token,
14436 /*type_definition_p=*/false);
14437 if (decl_specs)
14438 decl_specs->any_specifiers_p = true;
14439
14440 /* Consume the token. */
14441 cp_lexer_consume_token (parser->lexer);
14442
14443 /* There is no valid C++ program where a non-template type is
14444 followed by a "<". That usually indicates that the user thought
14445 that the type was a template. */
14446 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14447 token->location);
14448
14449 return TYPE_NAME (type);
14450 }
14451
14452 /* The type-specifier must be a user-defined type. */
14453 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14454 {
14455 bool qualified_p;
14456 bool global_p;
14457
14458 /* Don't gobble tokens or issue error messages if this is an
14459 optional type-specifier. */
14460 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14461 cp_parser_parse_tentatively (parser);
14462
14463 /* Look for the optional `::' operator. */
14464 global_p
14465 = (cp_parser_global_scope_opt (parser,
14466 /*current_scope_valid_p=*/false)
14467 != NULL_TREE);
14468 /* Look for the nested-name specifier. */
14469 qualified_p
14470 = (cp_parser_nested_name_specifier_opt (parser,
14471 /*typename_keyword_p=*/false,
14472 /*check_dependency_p=*/true,
14473 /*type_p=*/false,
14474 /*is_declaration=*/false)
14475 != NULL_TREE);
14476 token = cp_lexer_peek_token (parser->lexer);
14477 /* If we have seen a nested-name-specifier, and the next token
14478 is `template', then we are using the template-id production. */
14479 if (parser->scope
14480 && cp_parser_optional_template_keyword (parser))
14481 {
14482 /* Look for the template-id. */
14483 type = cp_parser_template_id (parser,
14484 /*template_keyword_p=*/true,
14485 /*check_dependency_p=*/true,
14486 none_type,
14487 /*is_declaration=*/false);
14488 /* If the template-id did not name a type, we are out of
14489 luck. */
14490 if (TREE_CODE (type) != TYPE_DECL)
14491 {
14492 cp_parser_error (parser, "expected template-id for type");
14493 type = NULL_TREE;
14494 }
14495 }
14496 /* Otherwise, look for a type-name. */
14497 else
14498 type = cp_parser_type_name (parser);
14499 /* Keep track of all name-lookups performed in class scopes. */
14500 if (type
14501 && !global_p
14502 && !qualified_p
14503 && TREE_CODE (type) == TYPE_DECL
14504 && identifier_p (DECL_NAME (type)))
14505 maybe_note_name_used_in_class (DECL_NAME (type), type);
14506 /* If it didn't work out, we don't have a TYPE. */
14507 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14508 && !cp_parser_parse_definitely (parser))
14509 type = NULL_TREE;
14510 if (type && decl_specs)
14511 cp_parser_set_decl_spec_type (decl_specs, type,
14512 token,
14513 /*type_definition_p=*/false);
14514 }
14515
14516 /* If we didn't get a type-name, issue an error message. */
14517 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14518 {
14519 cp_parser_error (parser, "expected type-name");
14520 return error_mark_node;
14521 }
14522
14523 if (type && type != error_mark_node)
14524 {
14525 /* See if TYPE is an Objective-C type, and if so, parse and
14526 accept any protocol references following it. Do this before
14527 the cp_parser_check_for_invalid_template_id() call, because
14528 Objective-C types can be followed by '<...>' which would
14529 enclose protocol names rather than template arguments, and so
14530 everything is fine. */
14531 if (c_dialect_objc () && !parser->scope
14532 && (objc_is_id (type) || objc_is_class_name (type)))
14533 {
14534 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14535 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14536
14537 /* Clobber the "unqualified" type previously entered into
14538 DECL_SPECS with the new, improved protocol-qualified version. */
14539 if (decl_specs)
14540 decl_specs->type = qual_type;
14541
14542 return qual_type;
14543 }
14544
14545 /* There is no valid C++ program where a non-template type is
14546 followed by a "<". That usually indicates that the user
14547 thought that the type was a template. */
14548 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14549 none_type,
14550 token->location);
14551 }
14552
14553 return type;
14554 }
14555
14556 /* Parse a type-name.
14557
14558 type-name:
14559 class-name
14560 enum-name
14561 typedef-name
14562 simple-template-id [in c++0x]
14563
14564 enum-name:
14565 identifier
14566
14567 typedef-name:
14568 identifier
14569
14570 Returns a TYPE_DECL for the type. */
14571
14572 static tree
14573 cp_parser_type_name (cp_parser* parser)
14574 {
14575 tree type_decl;
14576
14577 /* We can't know yet whether it is a class-name or not. */
14578 cp_parser_parse_tentatively (parser);
14579 /* Try a class-name. */
14580 type_decl = cp_parser_class_name (parser,
14581 /*typename_keyword_p=*/false,
14582 /*template_keyword_p=*/false,
14583 none_type,
14584 /*check_dependency_p=*/true,
14585 /*class_head_p=*/false,
14586 /*is_declaration=*/false);
14587 /* If it's not a class-name, keep looking. */
14588 if (!cp_parser_parse_definitely (parser))
14589 {
14590 if (cxx_dialect < cxx11)
14591 /* It must be a typedef-name or an enum-name. */
14592 return cp_parser_nonclass_name (parser);
14593
14594 cp_parser_parse_tentatively (parser);
14595 /* It is either a simple-template-id representing an
14596 instantiation of an alias template... */
14597 type_decl = cp_parser_template_id (parser,
14598 /*template_keyword_p=*/false,
14599 /*check_dependency_p=*/false,
14600 none_type,
14601 /*is_declaration=*/false);
14602 /* Note that this must be an instantiation of an alias template
14603 because [temp.names]/6 says:
14604
14605 A template-id that names an alias template specialization
14606 is a type-name.
14607
14608 Whereas [temp.names]/7 says:
14609
14610 A simple-template-id that names a class template
14611 specialization is a class-name. */
14612 if (type_decl != NULL_TREE
14613 && TREE_CODE (type_decl) == TYPE_DECL
14614 && TYPE_DECL_ALIAS_P (type_decl))
14615 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14616 else
14617 cp_parser_simulate_error (parser);
14618
14619 if (!cp_parser_parse_definitely (parser))
14620 /* ... Or a typedef-name or an enum-name. */
14621 return cp_parser_nonclass_name (parser);
14622 }
14623
14624 return type_decl;
14625 }
14626
14627 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14628
14629 enum-name:
14630 identifier
14631
14632 typedef-name:
14633 identifier
14634
14635 Returns a TYPE_DECL for the type. */
14636
14637 static tree
14638 cp_parser_nonclass_name (cp_parser* parser)
14639 {
14640 tree type_decl;
14641 tree identifier;
14642
14643 cp_token *token = cp_lexer_peek_token (parser->lexer);
14644 identifier = cp_parser_identifier (parser);
14645 if (identifier == error_mark_node)
14646 return error_mark_node;
14647
14648 /* Look up the type-name. */
14649 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14650
14651 if (TREE_CODE (type_decl) == USING_DECL)
14652 {
14653 if (!DECL_DEPENDENT_P (type_decl))
14654 type_decl = strip_using_decl (type_decl);
14655 else if (USING_DECL_TYPENAME_P (type_decl))
14656 {
14657 /* We have found a type introduced by a using
14658 declaration at class scope that refers to a dependent
14659 type.
14660
14661 using typename :: [opt] nested-name-specifier unqualified-id ;
14662 */
14663 type_decl = make_typename_type (TREE_TYPE (type_decl),
14664 DECL_NAME (type_decl),
14665 typename_type, tf_error);
14666 if (type_decl != error_mark_node)
14667 type_decl = TYPE_NAME (type_decl);
14668 }
14669 }
14670
14671 if (TREE_CODE (type_decl) != TYPE_DECL
14672 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14673 {
14674 /* See if this is an Objective-C type. */
14675 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14676 tree type = objc_get_protocol_qualified_type (identifier, protos);
14677 if (type)
14678 type_decl = TYPE_NAME (type);
14679 }
14680
14681 /* Issue an error if we did not find a type-name. */
14682 if (TREE_CODE (type_decl) != TYPE_DECL
14683 /* In Objective-C, we have the complication that class names are
14684 normally type names and start declarations (eg, the
14685 "NSObject" in "NSObject *object;"), but can be used in an
14686 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14687 is an expression. So, a classname followed by a dot is not a
14688 valid type-name. */
14689 || (objc_is_class_name (TREE_TYPE (type_decl))
14690 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14691 {
14692 if (!cp_parser_simulate_error (parser))
14693 cp_parser_name_lookup_error (parser, identifier, type_decl,
14694 NLE_TYPE, token->location);
14695 return error_mark_node;
14696 }
14697 /* Remember that the name was used in the definition of the
14698 current class so that we can check later to see if the
14699 meaning would have been different after the class was
14700 entirely defined. */
14701 else if (type_decl != error_mark_node
14702 && !parser->scope)
14703 maybe_note_name_used_in_class (identifier, type_decl);
14704
14705 return type_decl;
14706 }
14707
14708 /* Parse an elaborated-type-specifier. Note that the grammar given
14709 here incorporates the resolution to DR68.
14710
14711 elaborated-type-specifier:
14712 class-key :: [opt] nested-name-specifier [opt] identifier
14713 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14714 enum-key :: [opt] nested-name-specifier [opt] identifier
14715 typename :: [opt] nested-name-specifier identifier
14716 typename :: [opt] nested-name-specifier template [opt]
14717 template-id
14718
14719 GNU extension:
14720
14721 elaborated-type-specifier:
14722 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14723 class-key attributes :: [opt] nested-name-specifier [opt]
14724 template [opt] template-id
14725 enum attributes :: [opt] nested-name-specifier [opt] identifier
14726
14727 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14728 declared `friend'. If IS_DECLARATION is TRUE, then this
14729 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14730 something is being declared.
14731
14732 Returns the TYPE specified. */
14733
14734 static tree
14735 cp_parser_elaborated_type_specifier (cp_parser* parser,
14736 bool is_friend,
14737 bool is_declaration)
14738 {
14739 enum tag_types tag_type;
14740 tree identifier;
14741 tree type = NULL_TREE;
14742 tree attributes = NULL_TREE;
14743 tree globalscope;
14744 cp_token *token = NULL;
14745
14746 /* See if we're looking at the `enum' keyword. */
14747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14748 {
14749 /* Consume the `enum' token. */
14750 cp_lexer_consume_token (parser->lexer);
14751 /* Remember that it's an enumeration type. */
14752 tag_type = enum_type;
14753 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14754 enums) is used here. */
14755 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14756 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14757 {
14758 pedwarn (input_location, 0, "elaborated-type-specifier "
14759 "for a scoped enum must not use the %<%D%> keyword",
14760 cp_lexer_peek_token (parser->lexer)->u.value);
14761 /* Consume the `struct' or `class' and parse it anyway. */
14762 cp_lexer_consume_token (parser->lexer);
14763 }
14764 /* Parse the attributes. */
14765 attributes = cp_parser_attributes_opt (parser);
14766 }
14767 /* Or, it might be `typename'. */
14768 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14769 RID_TYPENAME))
14770 {
14771 /* Consume the `typename' token. */
14772 cp_lexer_consume_token (parser->lexer);
14773 /* Remember that it's a `typename' type. */
14774 tag_type = typename_type;
14775 }
14776 /* Otherwise it must be a class-key. */
14777 else
14778 {
14779 tag_type = cp_parser_class_key (parser);
14780 if (tag_type == none_type)
14781 return error_mark_node;
14782 /* Parse the attributes. */
14783 attributes = cp_parser_attributes_opt (parser);
14784 }
14785
14786 /* Look for the `::' operator. */
14787 globalscope = cp_parser_global_scope_opt (parser,
14788 /*current_scope_valid_p=*/false);
14789 /* Look for the nested-name-specifier. */
14790 if (tag_type == typename_type && !globalscope)
14791 {
14792 if (!cp_parser_nested_name_specifier (parser,
14793 /*typename_keyword_p=*/true,
14794 /*check_dependency_p=*/true,
14795 /*type_p=*/true,
14796 is_declaration))
14797 return error_mark_node;
14798 }
14799 else
14800 /* Even though `typename' is not present, the proposed resolution
14801 to Core Issue 180 says that in `class A<T>::B', `B' should be
14802 considered a type-name, even if `A<T>' is dependent. */
14803 cp_parser_nested_name_specifier_opt (parser,
14804 /*typename_keyword_p=*/true,
14805 /*check_dependency_p=*/true,
14806 /*type_p=*/true,
14807 is_declaration);
14808 /* For everything but enumeration types, consider a template-id.
14809 For an enumeration type, consider only a plain identifier. */
14810 if (tag_type != enum_type)
14811 {
14812 bool template_p = false;
14813 tree decl;
14814
14815 /* Allow the `template' keyword. */
14816 template_p = cp_parser_optional_template_keyword (parser);
14817 /* If we didn't see `template', we don't know if there's a
14818 template-id or not. */
14819 if (!template_p)
14820 cp_parser_parse_tentatively (parser);
14821 /* Parse the template-id. */
14822 token = cp_lexer_peek_token (parser->lexer);
14823 decl = cp_parser_template_id (parser, template_p,
14824 /*check_dependency_p=*/true,
14825 tag_type,
14826 is_declaration);
14827 /* If we didn't find a template-id, look for an ordinary
14828 identifier. */
14829 if (!template_p && !cp_parser_parse_definitely (parser))
14830 ;
14831 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14832 in effect, then we must assume that, upon instantiation, the
14833 template will correspond to a class. */
14834 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14835 && tag_type == typename_type)
14836 type = make_typename_type (parser->scope, decl,
14837 typename_type,
14838 /*complain=*/tf_error);
14839 /* If the `typename' keyword is in effect and DECL is not a type
14840 decl, then type is non existent. */
14841 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14842 ;
14843 else if (TREE_CODE (decl) == TYPE_DECL)
14844 type = check_elaborated_type_specifier (tag_type, decl,
14845 /*allow_template_p=*/true);
14846 else if (decl == error_mark_node)
14847 type = error_mark_node;
14848 }
14849
14850 if (!type)
14851 {
14852 token = cp_lexer_peek_token (parser->lexer);
14853 identifier = cp_parser_identifier (parser);
14854
14855 if (identifier == error_mark_node)
14856 {
14857 parser->scope = NULL_TREE;
14858 return error_mark_node;
14859 }
14860
14861 /* For a `typename', we needn't call xref_tag. */
14862 if (tag_type == typename_type
14863 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14864 return cp_parser_make_typename_type (parser, parser->scope,
14865 identifier,
14866 token->location);
14867 /* Look up a qualified name in the usual way. */
14868 if (parser->scope)
14869 {
14870 tree decl;
14871 tree ambiguous_decls;
14872
14873 decl = cp_parser_lookup_name (parser, identifier,
14874 tag_type,
14875 /*is_template=*/false,
14876 /*is_namespace=*/false,
14877 /*check_dependency=*/true,
14878 &ambiguous_decls,
14879 token->location);
14880
14881 /* If the lookup was ambiguous, an error will already have been
14882 issued. */
14883 if (ambiguous_decls)
14884 return error_mark_node;
14885
14886 /* If we are parsing friend declaration, DECL may be a
14887 TEMPLATE_DECL tree node here. However, we need to check
14888 whether this TEMPLATE_DECL results in valid code. Consider
14889 the following example:
14890
14891 namespace N {
14892 template <class T> class C {};
14893 }
14894 class X {
14895 template <class T> friend class N::C; // #1, valid code
14896 };
14897 template <class T> class Y {
14898 friend class N::C; // #2, invalid code
14899 };
14900
14901 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14902 name lookup of `N::C'. We see that friend declaration must
14903 be template for the code to be valid. Note that
14904 processing_template_decl does not work here since it is
14905 always 1 for the above two cases. */
14906
14907 decl = (cp_parser_maybe_treat_template_as_class
14908 (decl, /*tag_name_p=*/is_friend
14909 && parser->num_template_parameter_lists));
14910
14911 if (TREE_CODE (decl) != TYPE_DECL)
14912 {
14913 cp_parser_diagnose_invalid_type_name (parser,
14914 parser->scope,
14915 identifier,
14916 token->location);
14917 return error_mark_node;
14918 }
14919
14920 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14921 {
14922 bool allow_template = (parser->num_template_parameter_lists
14923 || DECL_SELF_REFERENCE_P (decl));
14924 type = check_elaborated_type_specifier (tag_type, decl,
14925 allow_template);
14926
14927 if (type == error_mark_node)
14928 return error_mark_node;
14929 }
14930
14931 /* Forward declarations of nested types, such as
14932
14933 class C1::C2;
14934 class C1::C2::C3;
14935
14936 are invalid unless all components preceding the final '::'
14937 are complete. If all enclosing types are complete, these
14938 declarations become merely pointless.
14939
14940 Invalid forward declarations of nested types are errors
14941 caught elsewhere in parsing. Those that are pointless arrive
14942 here. */
14943
14944 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14945 && !is_friend && !processing_explicit_instantiation)
14946 warning (0, "declaration %qD does not declare anything", decl);
14947
14948 type = TREE_TYPE (decl);
14949 }
14950 else
14951 {
14952 /* An elaborated-type-specifier sometimes introduces a new type and
14953 sometimes names an existing type. Normally, the rule is that it
14954 introduces a new type only if there is not an existing type of
14955 the same name already in scope. For example, given:
14956
14957 struct S {};
14958 void f() { struct S s; }
14959
14960 the `struct S' in the body of `f' is the same `struct S' as in
14961 the global scope; the existing definition is used. However, if
14962 there were no global declaration, this would introduce a new
14963 local class named `S'.
14964
14965 An exception to this rule applies to the following code:
14966
14967 namespace N { struct S; }
14968
14969 Here, the elaborated-type-specifier names a new type
14970 unconditionally; even if there is already an `S' in the
14971 containing scope this declaration names a new type.
14972 This exception only applies if the elaborated-type-specifier
14973 forms the complete declaration:
14974
14975 [class.name]
14976
14977 A declaration consisting solely of `class-key identifier ;' is
14978 either a redeclaration of the name in the current scope or a
14979 forward declaration of the identifier as a class name. It
14980 introduces the name into the current scope.
14981
14982 We are in this situation precisely when the next token is a `;'.
14983
14984 An exception to the exception is that a `friend' declaration does
14985 *not* name a new type; i.e., given:
14986
14987 struct S { friend struct T; };
14988
14989 `T' is not a new type in the scope of `S'.
14990
14991 Also, `new struct S' or `sizeof (struct S)' never results in the
14992 definition of a new type; a new type can only be declared in a
14993 declaration context. */
14994
14995 tag_scope ts;
14996 bool template_p;
14997
14998 if (is_friend)
14999 /* Friends have special name lookup rules. */
15000 ts = ts_within_enclosing_non_class;
15001 else if (is_declaration
15002 && cp_lexer_next_token_is (parser->lexer,
15003 CPP_SEMICOLON))
15004 /* This is a `class-key identifier ;' */
15005 ts = ts_current;
15006 else
15007 ts = ts_global;
15008
15009 template_p =
15010 (parser->num_template_parameter_lists
15011 && (cp_parser_next_token_starts_class_definition_p (parser)
15012 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15013 /* An unqualified name was used to reference this type, so
15014 there were no qualifying templates. */
15015 if (!cp_parser_check_template_parameters (parser,
15016 /*num_templates=*/0,
15017 token->location,
15018 /*declarator=*/NULL))
15019 return error_mark_node;
15020 type = xref_tag (tag_type, identifier, ts, template_p);
15021 }
15022 }
15023
15024 if (type == error_mark_node)
15025 return error_mark_node;
15026
15027 /* Allow attributes on forward declarations of classes. */
15028 if (attributes)
15029 {
15030 if (TREE_CODE (type) == TYPENAME_TYPE)
15031 warning (OPT_Wattributes,
15032 "attributes ignored on uninstantiated type");
15033 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15034 && ! processing_explicit_instantiation)
15035 warning (OPT_Wattributes,
15036 "attributes ignored on template instantiation");
15037 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15038 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15039 else
15040 warning (OPT_Wattributes,
15041 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15042 }
15043
15044 if (tag_type != enum_type)
15045 {
15046 /* Indicate whether this class was declared as a `class' or as a
15047 `struct'. */
15048 if (TREE_CODE (type) == RECORD_TYPE)
15049 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15050 cp_parser_check_class_key (tag_type, type);
15051 }
15052
15053 /* A "<" cannot follow an elaborated type specifier. If that
15054 happens, the user was probably trying to form a template-id. */
15055 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15056 token->location);
15057
15058 return type;
15059 }
15060
15061 /* Parse an enum-specifier.
15062
15063 enum-specifier:
15064 enum-head { enumerator-list [opt] }
15065 enum-head { enumerator-list , } [C++0x]
15066
15067 enum-head:
15068 enum-key identifier [opt] enum-base [opt]
15069 enum-key nested-name-specifier identifier enum-base [opt]
15070
15071 enum-key:
15072 enum
15073 enum class [C++0x]
15074 enum struct [C++0x]
15075
15076 enum-base: [C++0x]
15077 : type-specifier-seq
15078
15079 opaque-enum-specifier:
15080 enum-key identifier enum-base [opt] ;
15081
15082 GNU Extensions:
15083 enum-key attributes[opt] identifier [opt] enum-base [opt]
15084 { enumerator-list [opt] }attributes[opt]
15085 enum-key attributes[opt] identifier [opt] enum-base [opt]
15086 { enumerator-list, }attributes[opt] [C++0x]
15087
15088 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15089 if the token stream isn't an enum-specifier after all. */
15090
15091 static tree
15092 cp_parser_enum_specifier (cp_parser* parser)
15093 {
15094 tree identifier;
15095 tree type = NULL_TREE;
15096 tree prev_scope;
15097 tree nested_name_specifier = NULL_TREE;
15098 tree attributes;
15099 bool scoped_enum_p = false;
15100 bool has_underlying_type = false;
15101 bool nested_being_defined = false;
15102 bool new_value_list = false;
15103 bool is_new_type = false;
15104 bool is_anonymous = false;
15105 tree underlying_type = NULL_TREE;
15106 cp_token *type_start_token = NULL;
15107 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15108
15109 parser->colon_corrects_to_scope_p = false;
15110
15111 /* Parse tentatively so that we can back up if we don't find a
15112 enum-specifier. */
15113 cp_parser_parse_tentatively (parser);
15114
15115 /* Caller guarantees that the current token is 'enum', an identifier
15116 possibly follows, and the token after that is an opening brace.
15117 If we don't have an identifier, fabricate an anonymous name for
15118 the enumeration being defined. */
15119 cp_lexer_consume_token (parser->lexer);
15120
15121 /* Parse the "class" or "struct", which indicates a scoped
15122 enumeration type in C++0x. */
15123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15124 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15125 {
15126 if (cxx_dialect < cxx11)
15127 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15128
15129 /* Consume the `struct' or `class' token. */
15130 cp_lexer_consume_token (parser->lexer);
15131
15132 scoped_enum_p = true;
15133 }
15134
15135 attributes = cp_parser_attributes_opt (parser);
15136
15137 /* Clear the qualification. */
15138 parser->scope = NULL_TREE;
15139 parser->qualifying_scope = NULL_TREE;
15140 parser->object_scope = NULL_TREE;
15141
15142 /* Figure out in what scope the declaration is being placed. */
15143 prev_scope = current_scope ();
15144
15145 type_start_token = cp_lexer_peek_token (parser->lexer);
15146
15147 push_deferring_access_checks (dk_no_check);
15148 nested_name_specifier
15149 = cp_parser_nested_name_specifier_opt (parser,
15150 /*typename_keyword_p=*/true,
15151 /*check_dependency_p=*/false,
15152 /*type_p=*/false,
15153 /*is_declaration=*/false);
15154
15155 if (nested_name_specifier)
15156 {
15157 tree name;
15158
15159 identifier = cp_parser_identifier (parser);
15160 name = cp_parser_lookup_name (parser, identifier,
15161 enum_type,
15162 /*is_template=*/false,
15163 /*is_namespace=*/false,
15164 /*check_dependency=*/true,
15165 /*ambiguous_decls=*/NULL,
15166 input_location);
15167 if (name && name != error_mark_node)
15168 {
15169 type = TREE_TYPE (name);
15170 if (TREE_CODE (type) == TYPENAME_TYPE)
15171 {
15172 /* Are template enums allowed in ISO? */
15173 if (template_parm_scope_p ())
15174 pedwarn (type_start_token->location, OPT_Wpedantic,
15175 "%qD is an enumeration template", name);
15176 /* ignore a typename reference, for it will be solved by name
15177 in start_enum. */
15178 type = NULL_TREE;
15179 }
15180 }
15181 else if (nested_name_specifier == error_mark_node)
15182 /* We already issued an error. */;
15183 else
15184 error_at (type_start_token->location,
15185 "%qD is not an enumerator-name", identifier);
15186 }
15187 else
15188 {
15189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15190 identifier = cp_parser_identifier (parser);
15191 else
15192 {
15193 identifier = make_anon_name ();
15194 is_anonymous = true;
15195 if (scoped_enum_p)
15196 error_at (type_start_token->location,
15197 "anonymous scoped enum is not allowed");
15198 }
15199 }
15200 pop_deferring_access_checks ();
15201
15202 /* Check for the `:' that denotes a specified underlying type in C++0x.
15203 Note that a ':' could also indicate a bitfield width, however. */
15204 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15205 {
15206 cp_decl_specifier_seq type_specifiers;
15207
15208 /* Consume the `:'. */
15209 cp_lexer_consume_token (parser->lexer);
15210
15211 /* Parse the type-specifier-seq. */
15212 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15213 /*is_trailing_return=*/false,
15214 &type_specifiers);
15215
15216 /* At this point this is surely not elaborated type specifier. */
15217 if (!cp_parser_parse_definitely (parser))
15218 return NULL_TREE;
15219
15220 if (cxx_dialect < cxx11)
15221 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15222
15223 has_underlying_type = true;
15224
15225 /* If that didn't work, stop. */
15226 if (type_specifiers.type != error_mark_node)
15227 {
15228 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15229 /*initialized=*/0, NULL);
15230 if (underlying_type == error_mark_node)
15231 underlying_type = NULL_TREE;
15232 }
15233 }
15234
15235 /* Look for the `{' but don't consume it yet. */
15236 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15237 {
15238 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15239 {
15240 cp_parser_error (parser, "expected %<{%>");
15241 if (has_underlying_type)
15242 {
15243 type = NULL_TREE;
15244 goto out;
15245 }
15246 }
15247 /* An opaque-enum-specifier must have a ';' here. */
15248 if ((scoped_enum_p || underlying_type)
15249 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15250 {
15251 cp_parser_error (parser, "expected %<;%> or %<{%>");
15252 if (has_underlying_type)
15253 {
15254 type = NULL_TREE;
15255 goto out;
15256 }
15257 }
15258 }
15259
15260 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15261 return NULL_TREE;
15262
15263 if (nested_name_specifier)
15264 {
15265 if (CLASS_TYPE_P (nested_name_specifier))
15266 {
15267 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15268 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15269 push_scope (nested_name_specifier);
15270 }
15271 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15272 {
15273 push_nested_namespace (nested_name_specifier);
15274 }
15275 }
15276
15277 /* Issue an error message if type-definitions are forbidden here. */
15278 if (!cp_parser_check_type_definition (parser))
15279 type = error_mark_node;
15280 else
15281 /* Create the new type. We do this before consuming the opening
15282 brace so the enum will be recorded as being on the line of its
15283 tag (or the 'enum' keyword, if there is no tag). */
15284 type = start_enum (identifier, type, underlying_type,
15285 scoped_enum_p, &is_new_type);
15286
15287 /* If the next token is not '{' it is an opaque-enum-specifier or an
15288 elaborated-type-specifier. */
15289 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15290 {
15291 timevar_push (TV_PARSE_ENUM);
15292 if (nested_name_specifier
15293 && nested_name_specifier != error_mark_node)
15294 {
15295 /* The following catches invalid code such as:
15296 enum class S<int>::E { A, B, C }; */
15297 if (!processing_specialization
15298 && CLASS_TYPE_P (nested_name_specifier)
15299 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15300 error_at (type_start_token->location, "cannot add an enumerator "
15301 "list to a template instantiation");
15302
15303 /* If that scope does not contain the scope in which the
15304 class was originally declared, the program is invalid. */
15305 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
15306 {
15307 if (at_namespace_scope_p ())
15308 error_at (type_start_token->location,
15309 "declaration of %qD in namespace %qD which does not "
15310 "enclose %qD",
15311 type, prev_scope, nested_name_specifier);
15312 else
15313 error_at (type_start_token->location,
15314 "declaration of %qD in %qD which does not enclose %qD",
15315 type, prev_scope, nested_name_specifier);
15316 type = error_mark_node;
15317 }
15318 }
15319
15320 if (scoped_enum_p)
15321 begin_scope (sk_scoped_enum, type);
15322
15323 /* Consume the opening brace. */
15324 cp_lexer_consume_token (parser->lexer);
15325
15326 if (type == error_mark_node)
15327 ; /* Nothing to add */
15328 else if (OPAQUE_ENUM_P (type)
15329 || (cxx_dialect > cxx98 && processing_specialization))
15330 {
15331 new_value_list = true;
15332 SET_OPAQUE_ENUM_P (type, false);
15333 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15334 }
15335 else
15336 {
15337 error_at (type_start_token->location, "multiple definition of %q#T", type);
15338 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15339 "previous definition here");
15340 type = error_mark_node;
15341 }
15342
15343 if (type == error_mark_node)
15344 cp_parser_skip_to_end_of_block_or_statement (parser);
15345 /* If the next token is not '}', then there are some enumerators. */
15346 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15347 {
15348 if (is_anonymous && !scoped_enum_p)
15349 pedwarn (type_start_token->location, OPT_Wpedantic,
15350 "ISO C++ forbids empty anonymous enum");
15351 }
15352 else
15353 cp_parser_enumerator_list (parser, type);
15354
15355 /* Consume the final '}'. */
15356 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15357
15358 if (scoped_enum_p)
15359 finish_scope ();
15360 timevar_pop (TV_PARSE_ENUM);
15361 }
15362 else
15363 {
15364 /* If a ';' follows, then it is an opaque-enum-specifier
15365 and additional restrictions apply. */
15366 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15367 {
15368 if (is_anonymous)
15369 error_at (type_start_token->location,
15370 "opaque-enum-specifier without name");
15371 else if (nested_name_specifier)
15372 error_at (type_start_token->location,
15373 "opaque-enum-specifier must use a simple identifier");
15374 }
15375 }
15376
15377 /* Look for trailing attributes to apply to this enumeration, and
15378 apply them if appropriate. */
15379 if (cp_parser_allow_gnu_extensions_p (parser))
15380 {
15381 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15382 trailing_attr = chainon (trailing_attr, attributes);
15383 cplus_decl_attributes (&type,
15384 trailing_attr,
15385 (int) ATTR_FLAG_TYPE_IN_PLACE);
15386 }
15387
15388 /* Finish up the enumeration. */
15389 if (type != error_mark_node)
15390 {
15391 if (new_value_list)
15392 finish_enum_value_list (type);
15393 if (is_new_type)
15394 finish_enum (type);
15395 }
15396
15397 if (nested_name_specifier)
15398 {
15399 if (CLASS_TYPE_P (nested_name_specifier))
15400 {
15401 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15402 pop_scope (nested_name_specifier);
15403 }
15404 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15405 {
15406 pop_nested_namespace (nested_name_specifier);
15407 }
15408 }
15409 out:
15410 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15411 return type;
15412 }
15413
15414 /* Parse an enumerator-list. The enumerators all have the indicated
15415 TYPE.
15416
15417 enumerator-list:
15418 enumerator-definition
15419 enumerator-list , enumerator-definition */
15420
15421 static void
15422 cp_parser_enumerator_list (cp_parser* parser, tree type)
15423 {
15424 while (true)
15425 {
15426 /* Parse an enumerator-definition. */
15427 cp_parser_enumerator_definition (parser, type);
15428
15429 /* If the next token is not a ',', we've reached the end of
15430 the list. */
15431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15432 break;
15433 /* Otherwise, consume the `,' and keep going. */
15434 cp_lexer_consume_token (parser->lexer);
15435 /* If the next token is a `}', there is a trailing comma. */
15436 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15437 {
15438 if (cxx_dialect < cxx11 && !in_system_header)
15439 pedwarn (input_location, OPT_Wpedantic,
15440 "comma at end of enumerator list");
15441 break;
15442 }
15443 }
15444 }
15445
15446 /* Parse an enumerator-definition. The enumerator has the indicated
15447 TYPE.
15448
15449 enumerator-definition:
15450 enumerator
15451 enumerator = constant-expression
15452
15453 enumerator:
15454 identifier */
15455
15456 static void
15457 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15458 {
15459 tree identifier;
15460 tree value;
15461 location_t loc;
15462
15463 /* Save the input location because we are interested in the location
15464 of the identifier and not the location of the explicit value. */
15465 loc = cp_lexer_peek_token (parser->lexer)->location;
15466
15467 /* Look for the identifier. */
15468 identifier = cp_parser_identifier (parser);
15469 if (identifier == error_mark_node)
15470 return;
15471
15472 /* If the next token is an '=', then there is an explicit value. */
15473 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15474 {
15475 /* Consume the `=' token. */
15476 cp_lexer_consume_token (parser->lexer);
15477 /* Parse the value. */
15478 value = cp_parser_constant_expression (parser,
15479 /*allow_non_constant_p=*/false,
15480 NULL);
15481 }
15482 else
15483 value = NULL_TREE;
15484
15485 /* If we are processing a template, make sure the initializer of the
15486 enumerator doesn't contain any bare template parameter pack. */
15487 if (check_for_bare_parameter_packs (value))
15488 value = error_mark_node;
15489
15490 /* integral_constant_value will pull out this expression, so make sure
15491 it's folded as appropriate. */
15492 value = fold_non_dependent_expr (value);
15493
15494 /* Create the enumerator. */
15495 build_enumerator (identifier, value, type, loc);
15496 }
15497
15498 /* Parse a namespace-name.
15499
15500 namespace-name:
15501 original-namespace-name
15502 namespace-alias
15503
15504 Returns the NAMESPACE_DECL for the namespace. */
15505
15506 static tree
15507 cp_parser_namespace_name (cp_parser* parser)
15508 {
15509 tree identifier;
15510 tree namespace_decl;
15511
15512 cp_token *token = cp_lexer_peek_token (parser->lexer);
15513
15514 /* Get the name of the namespace. */
15515 identifier = cp_parser_identifier (parser);
15516 if (identifier == error_mark_node)
15517 return error_mark_node;
15518
15519 /* Look up the identifier in the currently active scope. Look only
15520 for namespaces, due to:
15521
15522 [basic.lookup.udir]
15523
15524 When looking up a namespace-name in a using-directive or alias
15525 definition, only namespace names are considered.
15526
15527 And:
15528
15529 [basic.lookup.qual]
15530
15531 During the lookup of a name preceding the :: scope resolution
15532 operator, object, function, and enumerator names are ignored.
15533
15534 (Note that cp_parser_qualifying_entity only calls this
15535 function if the token after the name is the scope resolution
15536 operator.) */
15537 namespace_decl = cp_parser_lookup_name (parser, identifier,
15538 none_type,
15539 /*is_template=*/false,
15540 /*is_namespace=*/true,
15541 /*check_dependency=*/true,
15542 /*ambiguous_decls=*/NULL,
15543 token->location);
15544 /* If it's not a namespace, issue an error. */
15545 if (namespace_decl == error_mark_node
15546 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15547 {
15548 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15549 error_at (token->location, "%qD is not a namespace-name", identifier);
15550 cp_parser_error (parser, "expected namespace-name");
15551 namespace_decl = error_mark_node;
15552 }
15553
15554 return namespace_decl;
15555 }
15556
15557 /* Parse a namespace-definition.
15558
15559 namespace-definition:
15560 named-namespace-definition
15561 unnamed-namespace-definition
15562
15563 named-namespace-definition:
15564 original-namespace-definition
15565 extension-namespace-definition
15566
15567 original-namespace-definition:
15568 namespace identifier { namespace-body }
15569
15570 extension-namespace-definition:
15571 namespace original-namespace-name { namespace-body }
15572
15573 unnamed-namespace-definition:
15574 namespace { namespace-body } */
15575
15576 static void
15577 cp_parser_namespace_definition (cp_parser* parser)
15578 {
15579 tree identifier, attribs;
15580 bool has_visibility;
15581 bool is_inline;
15582
15583 cp_ensure_no_omp_declare_simd (parser);
15584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15585 {
15586 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15587 is_inline = true;
15588 cp_lexer_consume_token (parser->lexer);
15589 }
15590 else
15591 is_inline = false;
15592
15593 /* Look for the `namespace' keyword. */
15594 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15595
15596 /* Get the name of the namespace. We do not attempt to distinguish
15597 between an original-namespace-definition and an
15598 extension-namespace-definition at this point. The semantic
15599 analysis routines are responsible for that. */
15600 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15601 identifier = cp_parser_identifier (parser);
15602 else
15603 identifier = NULL_TREE;
15604
15605 /* Parse any specified attributes. */
15606 attribs = cp_parser_attributes_opt (parser);
15607
15608 /* Look for the `{' to start the namespace. */
15609 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15610 /* Start the namespace. */
15611 push_namespace (identifier);
15612
15613 /* "inline namespace" is equivalent to a stub namespace definition
15614 followed by a strong using directive. */
15615 if (is_inline)
15616 {
15617 tree name_space = current_namespace;
15618 /* Set up namespace association. */
15619 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15620 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15621 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15622 /* Import the contents of the inline namespace. */
15623 pop_namespace ();
15624 do_using_directive (name_space);
15625 push_namespace (identifier);
15626 }
15627
15628 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15629
15630 /* Parse the body of the namespace. */
15631 cp_parser_namespace_body (parser);
15632
15633 if (has_visibility)
15634 pop_visibility (1);
15635
15636 /* Finish the namespace. */
15637 pop_namespace ();
15638 /* Look for the final `}'. */
15639 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15640 }
15641
15642 /* Parse a namespace-body.
15643
15644 namespace-body:
15645 declaration-seq [opt] */
15646
15647 static void
15648 cp_parser_namespace_body (cp_parser* parser)
15649 {
15650 cp_parser_declaration_seq_opt (parser);
15651 }
15652
15653 /* Parse a namespace-alias-definition.
15654
15655 namespace-alias-definition:
15656 namespace identifier = qualified-namespace-specifier ; */
15657
15658 static void
15659 cp_parser_namespace_alias_definition (cp_parser* parser)
15660 {
15661 tree identifier;
15662 tree namespace_specifier;
15663
15664 cp_token *token = cp_lexer_peek_token (parser->lexer);
15665
15666 /* Look for the `namespace' keyword. */
15667 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15668 /* Look for the identifier. */
15669 identifier = cp_parser_identifier (parser);
15670 if (identifier == error_mark_node)
15671 return;
15672 /* Look for the `=' token. */
15673 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15674 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15675 {
15676 error_at (token->location, "%<namespace%> definition is not allowed here");
15677 /* Skip the definition. */
15678 cp_lexer_consume_token (parser->lexer);
15679 if (cp_parser_skip_to_closing_brace (parser))
15680 cp_lexer_consume_token (parser->lexer);
15681 return;
15682 }
15683 cp_parser_require (parser, CPP_EQ, RT_EQ);
15684 /* Look for the qualified-namespace-specifier. */
15685 namespace_specifier
15686 = cp_parser_qualified_namespace_specifier (parser);
15687 /* Look for the `;' token. */
15688 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15689
15690 /* Register the alias in the symbol table. */
15691 do_namespace_alias (identifier, namespace_specifier);
15692 }
15693
15694 /* Parse a qualified-namespace-specifier.
15695
15696 qualified-namespace-specifier:
15697 :: [opt] nested-name-specifier [opt] namespace-name
15698
15699 Returns a NAMESPACE_DECL corresponding to the specified
15700 namespace. */
15701
15702 static tree
15703 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15704 {
15705 /* Look for the optional `::'. */
15706 cp_parser_global_scope_opt (parser,
15707 /*current_scope_valid_p=*/false);
15708
15709 /* Look for the optional nested-name-specifier. */
15710 cp_parser_nested_name_specifier_opt (parser,
15711 /*typename_keyword_p=*/false,
15712 /*check_dependency_p=*/true,
15713 /*type_p=*/false,
15714 /*is_declaration=*/true);
15715
15716 return cp_parser_namespace_name (parser);
15717 }
15718
15719 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15720 access declaration.
15721
15722 using-declaration:
15723 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15724 using :: unqualified-id ;
15725
15726 access-declaration:
15727 qualified-id ;
15728
15729 */
15730
15731 static bool
15732 cp_parser_using_declaration (cp_parser* parser,
15733 bool access_declaration_p)
15734 {
15735 cp_token *token;
15736 bool typename_p = false;
15737 bool global_scope_p;
15738 tree decl;
15739 tree identifier;
15740 tree qscope;
15741 int oldcount = errorcount;
15742 cp_token *diag_token = NULL;
15743
15744 if (access_declaration_p)
15745 {
15746 diag_token = cp_lexer_peek_token (parser->lexer);
15747 cp_parser_parse_tentatively (parser);
15748 }
15749 else
15750 {
15751 /* Look for the `using' keyword. */
15752 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15753
15754 /* Peek at the next token. */
15755 token = cp_lexer_peek_token (parser->lexer);
15756 /* See if it's `typename'. */
15757 if (token->keyword == RID_TYPENAME)
15758 {
15759 /* Remember that we've seen it. */
15760 typename_p = true;
15761 /* Consume the `typename' token. */
15762 cp_lexer_consume_token (parser->lexer);
15763 }
15764 }
15765
15766 /* Look for the optional global scope qualification. */
15767 global_scope_p
15768 = (cp_parser_global_scope_opt (parser,
15769 /*current_scope_valid_p=*/false)
15770 != NULL_TREE);
15771
15772 /* If we saw `typename', or didn't see `::', then there must be a
15773 nested-name-specifier present. */
15774 if (typename_p || !global_scope_p)
15775 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15776 /*check_dependency_p=*/true,
15777 /*type_p=*/false,
15778 /*is_declaration=*/true);
15779 /* Otherwise, we could be in either of the two productions. In that
15780 case, treat the nested-name-specifier as optional. */
15781 else
15782 qscope = cp_parser_nested_name_specifier_opt (parser,
15783 /*typename_keyword_p=*/false,
15784 /*check_dependency_p=*/true,
15785 /*type_p=*/false,
15786 /*is_declaration=*/true);
15787 if (!qscope)
15788 qscope = global_namespace;
15789
15790 if (access_declaration_p && cp_parser_error_occurred (parser))
15791 /* Something has already gone wrong; there's no need to parse
15792 further. Since an error has occurred, the return value of
15793 cp_parser_parse_definitely will be false, as required. */
15794 return cp_parser_parse_definitely (parser);
15795
15796 token = cp_lexer_peek_token (parser->lexer);
15797 /* Parse the unqualified-id. */
15798 identifier = cp_parser_unqualified_id (parser,
15799 /*template_keyword_p=*/false,
15800 /*check_dependency_p=*/true,
15801 /*declarator_p=*/true,
15802 /*optional_p=*/false);
15803
15804 if (access_declaration_p)
15805 {
15806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15807 cp_parser_simulate_error (parser);
15808 if (!cp_parser_parse_definitely (parser))
15809 return false;
15810 }
15811
15812 /* The function we call to handle a using-declaration is different
15813 depending on what scope we are in. */
15814 if (qscope == error_mark_node || identifier == error_mark_node)
15815 ;
15816 else if (!identifier_p (identifier)
15817 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15818 /* [namespace.udecl]
15819
15820 A using declaration shall not name a template-id. */
15821 error_at (token->location,
15822 "a template-id may not appear in a using-declaration");
15823 else
15824 {
15825 if (at_class_scope_p ())
15826 {
15827 /* Create the USING_DECL. */
15828 decl = do_class_using_decl (parser->scope, identifier);
15829
15830 if (decl && typename_p)
15831 USING_DECL_TYPENAME_P (decl) = 1;
15832
15833 if (check_for_bare_parameter_packs (decl))
15834 return false;
15835 else
15836 /* Add it to the list of members in this class. */
15837 finish_member_declaration (decl);
15838 }
15839 else
15840 {
15841 decl = cp_parser_lookup_name_simple (parser,
15842 identifier,
15843 token->location);
15844 if (decl == error_mark_node)
15845 cp_parser_name_lookup_error (parser, identifier,
15846 decl, NLE_NULL,
15847 token->location);
15848 else if (check_for_bare_parameter_packs (decl))
15849 return false;
15850 else if (!at_namespace_scope_p ())
15851 do_local_using_decl (decl, qscope, identifier);
15852 else
15853 do_toplevel_using_decl (decl, qscope, identifier);
15854 }
15855 }
15856
15857 /* Look for the final `;'. */
15858 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15859
15860 if (access_declaration_p && errorcount == oldcount)
15861 warning_at (diag_token->location, OPT_Wdeprecated,
15862 "access declarations are deprecated "
15863 "in favour of using-declarations; "
15864 "suggestion: add the %<using%> keyword");
15865
15866 return true;
15867 }
15868
15869 /* Parse an alias-declaration.
15870
15871 alias-declaration:
15872 using identifier attribute-specifier-seq [opt] = type-id */
15873
15874 static tree
15875 cp_parser_alias_declaration (cp_parser* parser)
15876 {
15877 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15878 location_t id_location;
15879 cp_declarator *declarator;
15880 cp_decl_specifier_seq decl_specs;
15881 bool member_p;
15882 const char *saved_message = NULL;
15883
15884 /* Look for the `using' keyword. */
15885 cp_token *using_token
15886 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15887 if (using_token == NULL)
15888 return error_mark_node;
15889
15890 id_location = cp_lexer_peek_token (parser->lexer)->location;
15891 id = cp_parser_identifier (parser);
15892 if (id == error_mark_node)
15893 return error_mark_node;
15894
15895 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15896 attributes = cp_parser_attributes_opt (parser);
15897 if (attributes == error_mark_node)
15898 return error_mark_node;
15899
15900 cp_parser_require (parser, CPP_EQ, RT_EQ);
15901
15902 if (cp_parser_error_occurred (parser))
15903 return error_mark_node;
15904
15905 cp_parser_commit_to_tentative_parse (parser);
15906
15907 /* Now we are going to parse the type-id of the declaration. */
15908
15909 /*
15910 [dcl.type]/3 says:
15911
15912 "A type-specifier-seq shall not define a class or enumeration
15913 unless it appears in the type-id of an alias-declaration (7.1.3) that
15914 is not the declaration of a template-declaration."
15915
15916 In other words, if we currently are in an alias template, the
15917 type-id should not define a type.
15918
15919 So let's set parser->type_definition_forbidden_message in that
15920 case; cp_parser_check_type_definition (called by
15921 cp_parser_class_specifier) will then emit an error if a type is
15922 defined in the type-id. */
15923 if (parser->num_template_parameter_lists)
15924 {
15925 saved_message = parser->type_definition_forbidden_message;
15926 parser->type_definition_forbidden_message =
15927 G_("types may not be defined in alias template declarations");
15928 }
15929
15930 type = cp_parser_type_id (parser);
15931
15932 /* Restore the error message if need be. */
15933 if (parser->num_template_parameter_lists)
15934 parser->type_definition_forbidden_message = saved_message;
15935
15936 if (type == error_mark_node)
15937 {
15938 cp_parser_skip_to_end_of_block_or_statement (parser);
15939 return error_mark_node;
15940 }
15941
15942 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15943
15944 if (cp_parser_error_occurred (parser))
15945 {
15946 cp_parser_skip_to_end_of_block_or_statement (parser);
15947 return error_mark_node;
15948 }
15949
15950 /* A typedef-name can also be introduced by an alias-declaration. The
15951 identifier following the using keyword becomes a typedef-name. It has
15952 the same semantics as if it were introduced by the typedef
15953 specifier. In particular, it does not define a new type and it shall
15954 not appear in the type-id. */
15955
15956 clear_decl_specs (&decl_specs);
15957 decl_specs.type = type;
15958 if (attributes != NULL_TREE)
15959 {
15960 decl_specs.attributes = attributes;
15961 set_and_check_decl_spec_loc (&decl_specs,
15962 ds_attribute,
15963 attrs_token);
15964 }
15965 set_and_check_decl_spec_loc (&decl_specs,
15966 ds_typedef,
15967 using_token);
15968 set_and_check_decl_spec_loc (&decl_specs,
15969 ds_alias,
15970 using_token);
15971
15972 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15973 declarator->id_loc = id_location;
15974
15975 member_p = at_class_scope_p ();
15976 if (member_p)
15977 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15978 NULL_TREE, attributes);
15979 else
15980 decl = start_decl (declarator, &decl_specs, 0,
15981 attributes, NULL_TREE, &pushed_scope);
15982 if (decl == error_mark_node)
15983 return decl;
15984
15985 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15986
15987 if (pushed_scope)
15988 pop_scope (pushed_scope);
15989
15990 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15991 added into the symbol table; otherwise, return the TYPE_DECL. */
15992 if (DECL_LANG_SPECIFIC (decl)
15993 && DECL_TEMPLATE_INFO (decl)
15994 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15995 {
15996 decl = DECL_TI_TEMPLATE (decl);
15997 if (member_p)
15998 check_member_template (decl);
15999 }
16000
16001 return decl;
16002 }
16003
16004 /* Parse a using-directive.
16005
16006 using-directive:
16007 using namespace :: [opt] nested-name-specifier [opt]
16008 namespace-name ; */
16009
16010 static void
16011 cp_parser_using_directive (cp_parser* parser)
16012 {
16013 tree namespace_decl;
16014 tree attribs;
16015
16016 /* Look for the `using' keyword. */
16017 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16018 /* And the `namespace' keyword. */
16019 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16020 /* Look for the optional `::' operator. */
16021 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16022 /* And the optional nested-name-specifier. */
16023 cp_parser_nested_name_specifier_opt (parser,
16024 /*typename_keyword_p=*/false,
16025 /*check_dependency_p=*/true,
16026 /*type_p=*/false,
16027 /*is_declaration=*/true);
16028 /* Get the namespace being used. */
16029 namespace_decl = cp_parser_namespace_name (parser);
16030 /* And any specified attributes. */
16031 attribs = cp_parser_attributes_opt (parser);
16032 /* Update the symbol table. */
16033 parse_using_directive (namespace_decl, attribs);
16034 /* Look for the final `;'. */
16035 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16036 }
16037
16038 /* Parse an asm-definition.
16039
16040 asm-definition:
16041 asm ( string-literal ) ;
16042
16043 GNU Extension:
16044
16045 asm-definition:
16046 asm volatile [opt] ( string-literal ) ;
16047 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16048 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16049 : asm-operand-list [opt] ) ;
16050 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16051 : asm-operand-list [opt]
16052 : asm-clobber-list [opt] ) ;
16053 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16054 : asm-clobber-list [opt]
16055 : asm-goto-list ) ; */
16056
16057 static void
16058 cp_parser_asm_definition (cp_parser* parser)
16059 {
16060 tree string;
16061 tree outputs = NULL_TREE;
16062 tree inputs = NULL_TREE;
16063 tree clobbers = NULL_TREE;
16064 tree labels = NULL_TREE;
16065 tree asm_stmt;
16066 bool volatile_p = false;
16067 bool extended_p = false;
16068 bool invalid_inputs_p = false;
16069 bool invalid_outputs_p = false;
16070 bool goto_p = false;
16071 required_token missing = RT_NONE;
16072
16073 /* Look for the `asm' keyword. */
16074 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16075 /* See if the next token is `volatile'. */
16076 if (cp_parser_allow_gnu_extensions_p (parser)
16077 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16078 {
16079 /* Remember that we saw the `volatile' keyword. */
16080 volatile_p = true;
16081 /* Consume the token. */
16082 cp_lexer_consume_token (parser->lexer);
16083 }
16084 if (cp_parser_allow_gnu_extensions_p (parser)
16085 && parser->in_function_body
16086 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16087 {
16088 /* Remember that we saw the `goto' keyword. */
16089 goto_p = true;
16090 /* Consume the token. */
16091 cp_lexer_consume_token (parser->lexer);
16092 }
16093 /* Look for the opening `('. */
16094 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16095 return;
16096 /* Look for the string. */
16097 string = cp_parser_string_literal (parser, false, false);
16098 if (string == error_mark_node)
16099 {
16100 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16101 /*consume_paren=*/true);
16102 return;
16103 }
16104
16105 /* If we're allowing GNU extensions, check for the extended assembly
16106 syntax. Unfortunately, the `:' tokens need not be separated by
16107 a space in C, and so, for compatibility, we tolerate that here
16108 too. Doing that means that we have to treat the `::' operator as
16109 two `:' tokens. */
16110 if (cp_parser_allow_gnu_extensions_p (parser)
16111 && parser->in_function_body
16112 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16113 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16114 {
16115 bool inputs_p = false;
16116 bool clobbers_p = false;
16117 bool labels_p = false;
16118
16119 /* The extended syntax was used. */
16120 extended_p = true;
16121
16122 /* Look for outputs. */
16123 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16124 {
16125 /* Consume the `:'. */
16126 cp_lexer_consume_token (parser->lexer);
16127 /* Parse the output-operands. */
16128 if (cp_lexer_next_token_is_not (parser->lexer,
16129 CPP_COLON)
16130 && cp_lexer_next_token_is_not (parser->lexer,
16131 CPP_SCOPE)
16132 && cp_lexer_next_token_is_not (parser->lexer,
16133 CPP_CLOSE_PAREN)
16134 && !goto_p)
16135 outputs = cp_parser_asm_operand_list (parser);
16136
16137 if (outputs == error_mark_node)
16138 invalid_outputs_p = true;
16139 }
16140 /* If the next token is `::', there are no outputs, and the
16141 next token is the beginning of the inputs. */
16142 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16143 /* The inputs are coming next. */
16144 inputs_p = true;
16145
16146 /* Look for inputs. */
16147 if (inputs_p
16148 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16149 {
16150 /* Consume the `:' or `::'. */
16151 cp_lexer_consume_token (parser->lexer);
16152 /* Parse the output-operands. */
16153 if (cp_lexer_next_token_is_not (parser->lexer,
16154 CPP_COLON)
16155 && cp_lexer_next_token_is_not (parser->lexer,
16156 CPP_SCOPE)
16157 && cp_lexer_next_token_is_not (parser->lexer,
16158 CPP_CLOSE_PAREN))
16159 inputs = cp_parser_asm_operand_list (parser);
16160
16161 if (inputs == error_mark_node)
16162 invalid_inputs_p = true;
16163 }
16164 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16165 /* The clobbers are coming next. */
16166 clobbers_p = true;
16167
16168 /* Look for clobbers. */
16169 if (clobbers_p
16170 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16171 {
16172 clobbers_p = true;
16173 /* Consume the `:' or `::'. */
16174 cp_lexer_consume_token (parser->lexer);
16175 /* Parse the clobbers. */
16176 if (cp_lexer_next_token_is_not (parser->lexer,
16177 CPP_COLON)
16178 && cp_lexer_next_token_is_not (parser->lexer,
16179 CPP_CLOSE_PAREN))
16180 clobbers = cp_parser_asm_clobber_list (parser);
16181 }
16182 else if (goto_p
16183 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16184 /* The labels are coming next. */
16185 labels_p = true;
16186
16187 /* Look for labels. */
16188 if (labels_p
16189 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16190 {
16191 labels_p = true;
16192 /* Consume the `:' or `::'. */
16193 cp_lexer_consume_token (parser->lexer);
16194 /* Parse the labels. */
16195 labels = cp_parser_asm_label_list (parser);
16196 }
16197
16198 if (goto_p && !labels_p)
16199 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16200 }
16201 else if (goto_p)
16202 missing = RT_COLON_SCOPE;
16203
16204 /* Look for the closing `)'. */
16205 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16206 missing ? missing : RT_CLOSE_PAREN))
16207 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16208 /*consume_paren=*/true);
16209 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16210
16211 if (!invalid_inputs_p && !invalid_outputs_p)
16212 {
16213 /* Create the ASM_EXPR. */
16214 if (parser->in_function_body)
16215 {
16216 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16217 inputs, clobbers, labels);
16218 /* If the extended syntax was not used, mark the ASM_EXPR. */
16219 if (!extended_p)
16220 {
16221 tree temp = asm_stmt;
16222 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16223 temp = TREE_OPERAND (temp, 0);
16224
16225 ASM_INPUT_P (temp) = 1;
16226 }
16227 }
16228 else
16229 add_asm_node (string);
16230 }
16231 }
16232
16233 /* Declarators [gram.dcl.decl] */
16234
16235 /* Parse an init-declarator.
16236
16237 init-declarator:
16238 declarator initializer [opt]
16239
16240 GNU Extension:
16241
16242 init-declarator:
16243 declarator asm-specification [opt] attributes [opt] initializer [opt]
16244
16245 function-definition:
16246 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16247 function-body
16248 decl-specifier-seq [opt] declarator function-try-block
16249
16250 GNU Extension:
16251
16252 function-definition:
16253 __extension__ function-definition
16254
16255 TM Extension:
16256
16257 function-definition:
16258 decl-specifier-seq [opt] declarator function-transaction-block
16259
16260 The DECL_SPECIFIERS apply to this declarator. Returns a
16261 representation of the entity declared. If MEMBER_P is TRUE, then
16262 this declarator appears in a class scope. The new DECL created by
16263 this declarator is returned.
16264
16265 The CHECKS are access checks that should be performed once we know
16266 what entity is being declared (and, therefore, what classes have
16267 befriended it).
16268
16269 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16270 for a function-definition here as well. If the declarator is a
16271 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16272 be TRUE upon return. By that point, the function-definition will
16273 have been completely parsed.
16274
16275 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16276 is FALSE.
16277
16278 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16279 parsed declaration if it is an uninitialized single declarator not followed
16280 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16281 if present, will not be consumed. If returned, this declarator will be
16282 created with SD_INITIALIZED but will not call cp_finish_decl. */
16283
16284 static tree
16285 cp_parser_init_declarator (cp_parser* parser,
16286 cp_decl_specifier_seq *decl_specifiers,
16287 vec<deferred_access_check, va_gc> *checks,
16288 bool function_definition_allowed_p,
16289 bool member_p,
16290 int declares_class_or_enum,
16291 bool* function_definition_p,
16292 tree* maybe_range_for_decl)
16293 {
16294 cp_token *token = NULL, *asm_spec_start_token = NULL,
16295 *attributes_start_token = NULL;
16296 cp_declarator *declarator;
16297 tree prefix_attributes;
16298 tree attributes = NULL;
16299 tree asm_specification;
16300 tree initializer;
16301 tree decl = NULL_TREE;
16302 tree scope;
16303 int is_initialized;
16304 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16305 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16306 "(...)". */
16307 enum cpp_ttype initialization_kind;
16308 bool is_direct_init = false;
16309 bool is_non_constant_init;
16310 int ctor_dtor_or_conv_p;
16311 bool friend_p;
16312 tree pushed_scope = NULL_TREE;
16313 bool range_for_decl_p = false;
16314 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16315
16316 /* Gather the attributes that were provided with the
16317 decl-specifiers. */
16318 prefix_attributes = decl_specifiers->attributes;
16319
16320 /* Assume that this is not the declarator for a function
16321 definition. */
16322 if (function_definition_p)
16323 *function_definition_p = false;
16324
16325 /* Default arguments are only permitted for function parameters. */
16326 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16327 parser->default_arg_ok_p = false;
16328
16329 /* Defer access checks while parsing the declarator; we cannot know
16330 what names are accessible until we know what is being
16331 declared. */
16332 resume_deferring_access_checks ();
16333
16334 /* Parse the declarator. */
16335 token = cp_lexer_peek_token (parser->lexer);
16336 declarator
16337 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16338 &ctor_dtor_or_conv_p,
16339 /*parenthesized_p=*/NULL,
16340 member_p);
16341 /* Gather up the deferred checks. */
16342 stop_deferring_access_checks ();
16343
16344 parser->default_arg_ok_p = saved_default_arg_ok_p;
16345
16346 /* If the DECLARATOR was erroneous, there's no need to go
16347 further. */
16348 if (declarator == cp_error_declarator)
16349 return error_mark_node;
16350
16351 /* Check that the number of template-parameter-lists is OK. */
16352 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16353 token->location))
16354 return error_mark_node;
16355
16356 if (declares_class_or_enum & 2)
16357 cp_parser_check_for_definition_in_return_type (declarator,
16358 decl_specifiers->type,
16359 decl_specifiers->locations[ds_type_spec]);
16360
16361 /* Figure out what scope the entity declared by the DECLARATOR is
16362 located in. `grokdeclarator' sometimes changes the scope, so
16363 we compute it now. */
16364 scope = get_scope_of_declarator (declarator);
16365
16366 /* Perform any lookups in the declared type which were thought to be
16367 dependent, but are not in the scope of the declarator. */
16368 decl_specifiers->type
16369 = maybe_update_decl_type (decl_specifiers->type, scope);
16370
16371 /* If we're allowing GNU extensions, look for an
16372 asm-specification. */
16373 if (cp_parser_allow_gnu_extensions_p (parser))
16374 {
16375 /* Look for an asm-specification. */
16376 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16377 asm_specification = cp_parser_asm_specification_opt (parser);
16378 }
16379 else
16380 asm_specification = NULL_TREE;
16381
16382 /* Look for attributes. */
16383 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16384 attributes = cp_parser_attributes_opt (parser);
16385
16386 /* Peek at the next token. */
16387 token = cp_lexer_peek_token (parser->lexer);
16388
16389 if (function_declarator_p (declarator))
16390 {
16391 /* Check to see if the token indicates the start of a
16392 function-definition. */
16393 if (cp_parser_token_starts_function_definition_p (token))
16394 {
16395 if (!function_definition_allowed_p)
16396 {
16397 /* If a function-definition should not appear here, issue an
16398 error message. */
16399 cp_parser_error (parser,
16400 "a function-definition is not allowed here");
16401 return error_mark_node;
16402 }
16403
16404 location_t func_brace_location
16405 = cp_lexer_peek_token (parser->lexer)->location;
16406
16407 /* Neither attributes nor an asm-specification are allowed
16408 on a function-definition. */
16409 if (asm_specification)
16410 error_at (asm_spec_start_token->location,
16411 "an asm-specification is not allowed "
16412 "on a function-definition");
16413 if (attributes)
16414 error_at (attributes_start_token->location,
16415 "attributes are not allowed "
16416 "on a function-definition");
16417 /* This is a function-definition. */
16418 *function_definition_p = true;
16419
16420 /* Parse the function definition. */
16421 if (member_p)
16422 decl = cp_parser_save_member_function_body (parser,
16423 decl_specifiers,
16424 declarator,
16425 prefix_attributes);
16426 else
16427 decl =
16428 (cp_parser_function_definition_from_specifiers_and_declarator
16429 (parser, decl_specifiers, prefix_attributes, declarator));
16430
16431 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16432 {
16433 /* This is where the prologue starts... */
16434 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16435 = func_brace_location;
16436 }
16437
16438 return decl;
16439 }
16440 }
16441
16442 /* [dcl.dcl]
16443
16444 Only in function declarations for constructors, destructors, and
16445 type conversions can the decl-specifier-seq be omitted.
16446
16447 We explicitly postpone this check past the point where we handle
16448 function-definitions because we tolerate function-definitions
16449 that are missing their return types in some modes. */
16450 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16451 {
16452 cp_parser_error (parser,
16453 "expected constructor, destructor, or type conversion");
16454 return error_mark_node;
16455 }
16456
16457 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16458 if (token->type == CPP_EQ
16459 || token->type == CPP_OPEN_PAREN
16460 || token->type == CPP_OPEN_BRACE)
16461 {
16462 is_initialized = SD_INITIALIZED;
16463 initialization_kind = token->type;
16464 if (maybe_range_for_decl)
16465 *maybe_range_for_decl = error_mark_node;
16466
16467 if (token->type == CPP_EQ
16468 && function_declarator_p (declarator))
16469 {
16470 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16471 if (t2->keyword == RID_DEFAULT)
16472 is_initialized = SD_DEFAULTED;
16473 else if (t2->keyword == RID_DELETE)
16474 is_initialized = SD_DELETED;
16475 }
16476 }
16477 else
16478 {
16479 /* If the init-declarator isn't initialized and isn't followed by a
16480 `,' or `;', it's not a valid init-declarator. */
16481 if (token->type != CPP_COMMA
16482 && token->type != CPP_SEMICOLON)
16483 {
16484 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16485 range_for_decl_p = true;
16486 else
16487 {
16488 cp_parser_error (parser, "expected initializer");
16489 return error_mark_node;
16490 }
16491 }
16492 is_initialized = SD_UNINITIALIZED;
16493 initialization_kind = CPP_EOF;
16494 }
16495
16496 /* Because start_decl has side-effects, we should only call it if we
16497 know we're going ahead. By this point, we know that we cannot
16498 possibly be looking at any other construct. */
16499 cp_parser_commit_to_tentative_parse (parser);
16500
16501 /* If the decl specifiers were bad, issue an error now that we're
16502 sure this was intended to be a declarator. Then continue
16503 declaring the variable(s), as int, to try to cut down on further
16504 errors. */
16505 if (decl_specifiers->any_specifiers_p
16506 && decl_specifiers->type == error_mark_node)
16507 {
16508 cp_parser_error (parser, "invalid type in declaration");
16509 decl_specifiers->type = integer_type_node;
16510 }
16511
16512 /* Check to see whether or not this declaration is a friend. */
16513 friend_p = cp_parser_friend_p (decl_specifiers);
16514
16515 /* Enter the newly declared entry in the symbol table. If we're
16516 processing a declaration in a class-specifier, we wait until
16517 after processing the initializer. */
16518 if (!member_p)
16519 {
16520 if (parser->in_unbraced_linkage_specification_p)
16521 decl_specifiers->storage_class = sc_extern;
16522 decl = start_decl (declarator, decl_specifiers,
16523 range_for_decl_p? SD_INITIALIZED : is_initialized,
16524 attributes, prefix_attributes, &pushed_scope);
16525 cp_finalize_omp_declare_simd (parser, decl);
16526 /* Adjust location of decl if declarator->id_loc is more appropriate:
16527 set, and decl wasn't merged with another decl, in which case its
16528 location would be different from input_location, and more accurate. */
16529 if (DECL_P (decl)
16530 && declarator->id_loc != UNKNOWN_LOCATION
16531 && DECL_SOURCE_LOCATION (decl) == input_location)
16532 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16533 }
16534 else if (scope)
16535 /* Enter the SCOPE. That way unqualified names appearing in the
16536 initializer will be looked up in SCOPE. */
16537 pushed_scope = push_scope (scope);
16538
16539 /* Perform deferred access control checks, now that we know in which
16540 SCOPE the declared entity resides. */
16541 if (!member_p && decl)
16542 {
16543 tree saved_current_function_decl = NULL_TREE;
16544
16545 /* If the entity being declared is a function, pretend that we
16546 are in its scope. If it is a `friend', it may have access to
16547 things that would not otherwise be accessible. */
16548 if (TREE_CODE (decl) == FUNCTION_DECL)
16549 {
16550 saved_current_function_decl = current_function_decl;
16551 current_function_decl = decl;
16552 }
16553
16554 /* Perform access checks for template parameters. */
16555 cp_parser_perform_template_parameter_access_checks (checks);
16556
16557 /* Perform the access control checks for the declarator and the
16558 decl-specifiers. */
16559 perform_deferred_access_checks (tf_warning_or_error);
16560
16561 /* Restore the saved value. */
16562 if (TREE_CODE (decl) == FUNCTION_DECL)
16563 current_function_decl = saved_current_function_decl;
16564 }
16565
16566 /* Parse the initializer. */
16567 initializer = NULL_TREE;
16568 is_direct_init = false;
16569 is_non_constant_init = true;
16570 if (is_initialized)
16571 {
16572 if (function_declarator_p (declarator))
16573 {
16574 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16575 if (initialization_kind == CPP_EQ)
16576 initializer = cp_parser_pure_specifier (parser);
16577 else
16578 {
16579 /* If the declaration was erroneous, we don't really
16580 know what the user intended, so just silently
16581 consume the initializer. */
16582 if (decl != error_mark_node)
16583 error_at (initializer_start_token->location,
16584 "initializer provided for function");
16585 cp_parser_skip_to_closing_parenthesis (parser,
16586 /*recovering=*/true,
16587 /*or_comma=*/false,
16588 /*consume_paren=*/true);
16589 }
16590 }
16591 else
16592 {
16593 /* We want to record the extra mangling scope for in-class
16594 initializers of class members and initializers of static data
16595 member templates. The former involves deferring
16596 parsing of the initializer until end of class as with default
16597 arguments. So right here we only handle the latter. */
16598 if (!member_p && processing_template_decl)
16599 start_lambda_scope (decl);
16600 initializer = cp_parser_initializer (parser,
16601 &is_direct_init,
16602 &is_non_constant_init);
16603 if (!member_p && processing_template_decl)
16604 finish_lambda_scope ();
16605 if (initializer == error_mark_node)
16606 cp_parser_skip_to_end_of_statement (parser);
16607 }
16608 }
16609
16610 /* The old parser allows attributes to appear after a parenthesized
16611 initializer. Mark Mitchell proposed removing this functionality
16612 on the GCC mailing lists on 2002-08-13. This parser accepts the
16613 attributes -- but ignores them. */
16614 if (cp_parser_allow_gnu_extensions_p (parser)
16615 && initialization_kind == CPP_OPEN_PAREN)
16616 if (cp_parser_attributes_opt (parser))
16617 warning (OPT_Wattributes,
16618 "attributes after parenthesized initializer ignored");
16619
16620 /* For an in-class declaration, use `grokfield' to create the
16621 declaration. */
16622 if (member_p)
16623 {
16624 if (pushed_scope)
16625 {
16626 pop_scope (pushed_scope);
16627 pushed_scope = NULL_TREE;
16628 }
16629 decl = grokfield (declarator, decl_specifiers,
16630 initializer, !is_non_constant_init,
16631 /*asmspec=*/NULL_TREE,
16632 chainon (attributes, prefix_attributes));
16633 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16634 cp_parser_save_default_args (parser, decl);
16635 cp_finalize_omp_declare_simd (parser, decl);
16636 }
16637
16638 /* Finish processing the declaration. But, skip member
16639 declarations. */
16640 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16641 {
16642 cp_finish_decl (decl,
16643 initializer, !is_non_constant_init,
16644 asm_specification,
16645 /* If the initializer is in parentheses, then this is
16646 a direct-initialization, which means that an
16647 `explicit' constructor is OK. Otherwise, an
16648 `explicit' constructor cannot be used. */
16649 ((is_direct_init || !is_initialized)
16650 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16651 }
16652 else if ((cxx_dialect != cxx98) && friend_p
16653 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16654 /* Core issue #226 (C++0x only): A default template-argument
16655 shall not be specified in a friend class template
16656 declaration. */
16657 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16658 /*is_partial=*/false, /*is_friend_decl=*/1);
16659
16660 if (!friend_p && pushed_scope)
16661 pop_scope (pushed_scope);
16662
16663 if (function_declarator_p (declarator)
16664 && parser->fully_implicit_function_template_p)
16665 {
16666 if (member_p)
16667 decl = finish_fully_implicit_template (parser, decl);
16668 else
16669 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16670 }
16671
16672 return decl;
16673 }
16674
16675 /* Parse a declarator.
16676
16677 declarator:
16678 direct-declarator
16679 ptr-operator declarator
16680
16681 abstract-declarator:
16682 ptr-operator abstract-declarator [opt]
16683 direct-abstract-declarator
16684
16685 GNU Extensions:
16686
16687 declarator:
16688 attributes [opt] direct-declarator
16689 attributes [opt] ptr-operator declarator
16690
16691 abstract-declarator:
16692 attributes [opt] ptr-operator abstract-declarator [opt]
16693 attributes [opt] direct-abstract-declarator
16694
16695 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16696 detect constructor, destructor or conversion operators. It is set
16697 to -1 if the declarator is a name, and +1 if it is a
16698 function. Otherwise it is set to zero. Usually you just want to
16699 test for >0, but internally the negative value is used.
16700
16701 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16702 a decl-specifier-seq unless it declares a constructor, destructor,
16703 or conversion. It might seem that we could check this condition in
16704 semantic analysis, rather than parsing, but that makes it difficult
16705 to handle something like `f()'. We want to notice that there are
16706 no decl-specifiers, and therefore realize that this is an
16707 expression, not a declaration.)
16708
16709 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16710 the declarator is a direct-declarator of the form "(...)".
16711
16712 MEMBER_P is true iff this declarator is a member-declarator. */
16713
16714 static cp_declarator *
16715 cp_parser_declarator (cp_parser* parser,
16716 cp_parser_declarator_kind dcl_kind,
16717 int* ctor_dtor_or_conv_p,
16718 bool* parenthesized_p,
16719 bool member_p)
16720 {
16721 cp_declarator *declarator;
16722 enum tree_code code;
16723 cp_cv_quals cv_quals;
16724 tree class_type;
16725 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16726
16727 /* Assume this is not a constructor, destructor, or type-conversion
16728 operator. */
16729 if (ctor_dtor_or_conv_p)
16730 *ctor_dtor_or_conv_p = 0;
16731
16732 if (cp_parser_allow_gnu_extensions_p (parser))
16733 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16734
16735 /* Check for the ptr-operator production. */
16736 cp_parser_parse_tentatively (parser);
16737 /* Parse the ptr-operator. */
16738 code = cp_parser_ptr_operator (parser,
16739 &class_type,
16740 &cv_quals,
16741 &std_attributes);
16742
16743 /* If that worked, then we have a ptr-operator. */
16744 if (cp_parser_parse_definitely (parser))
16745 {
16746 /* If a ptr-operator was found, then this declarator was not
16747 parenthesized. */
16748 if (parenthesized_p)
16749 *parenthesized_p = true;
16750 /* The dependent declarator is optional if we are parsing an
16751 abstract-declarator. */
16752 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16753 cp_parser_parse_tentatively (parser);
16754
16755 /* Parse the dependent declarator. */
16756 declarator = cp_parser_declarator (parser, dcl_kind,
16757 /*ctor_dtor_or_conv_p=*/NULL,
16758 /*parenthesized_p=*/NULL,
16759 /*member_p=*/false);
16760
16761 /* If we are parsing an abstract-declarator, we must handle the
16762 case where the dependent declarator is absent. */
16763 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16764 && !cp_parser_parse_definitely (parser))
16765 declarator = NULL;
16766
16767 declarator = cp_parser_make_indirect_declarator
16768 (code, class_type, cv_quals, declarator, std_attributes);
16769 }
16770 /* Everything else is a direct-declarator. */
16771 else
16772 {
16773 if (parenthesized_p)
16774 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16775 CPP_OPEN_PAREN);
16776 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16777 ctor_dtor_or_conv_p,
16778 member_p);
16779 }
16780
16781 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16782 declarator->attributes = gnu_attributes;
16783 return declarator;
16784 }
16785
16786 /* Parse a direct-declarator or direct-abstract-declarator.
16787
16788 direct-declarator:
16789 declarator-id
16790 direct-declarator ( parameter-declaration-clause )
16791 cv-qualifier-seq [opt]
16792 ref-qualifier [opt]
16793 exception-specification [opt]
16794 direct-declarator [ constant-expression [opt] ]
16795 ( declarator )
16796
16797 direct-abstract-declarator:
16798 direct-abstract-declarator [opt]
16799 ( parameter-declaration-clause )
16800 cv-qualifier-seq [opt]
16801 ref-qualifier [opt]
16802 exception-specification [opt]
16803 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16804 ( abstract-declarator )
16805
16806 Returns a representation of the declarator. DCL_KIND is
16807 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16808 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16809 we are parsing a direct-declarator. It is
16810 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16811 of ambiguity we prefer an abstract declarator, as per
16812 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16813 cp_parser_declarator. */
16814
16815 static cp_declarator *
16816 cp_parser_direct_declarator (cp_parser* parser,
16817 cp_parser_declarator_kind dcl_kind,
16818 int* ctor_dtor_or_conv_p,
16819 bool member_p)
16820 {
16821 cp_token *token;
16822 cp_declarator *declarator = NULL;
16823 tree scope = NULL_TREE;
16824 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16825 bool saved_in_declarator_p = parser->in_declarator_p;
16826 bool first = true;
16827 tree pushed_scope = NULL_TREE;
16828
16829 while (true)
16830 {
16831 /* Peek at the next token. */
16832 token = cp_lexer_peek_token (parser->lexer);
16833 if (token->type == CPP_OPEN_PAREN)
16834 {
16835 /* This is either a parameter-declaration-clause, or a
16836 parenthesized declarator. When we know we are parsing a
16837 named declarator, it must be a parenthesized declarator
16838 if FIRST is true. For instance, `(int)' is a
16839 parameter-declaration-clause, with an omitted
16840 direct-abstract-declarator. But `((*))', is a
16841 parenthesized abstract declarator. Finally, when T is a
16842 template parameter `(T)' is a
16843 parameter-declaration-clause, and not a parenthesized
16844 named declarator.
16845
16846 We first try and parse a parameter-declaration-clause,
16847 and then try a nested declarator (if FIRST is true).
16848
16849 It is not an error for it not to be a
16850 parameter-declaration-clause, even when FIRST is
16851 false. Consider,
16852
16853 int i (int);
16854 int i (3);
16855
16856 The first is the declaration of a function while the
16857 second is the definition of a variable, including its
16858 initializer.
16859
16860 Having seen only the parenthesis, we cannot know which of
16861 these two alternatives should be selected. Even more
16862 complex are examples like:
16863
16864 int i (int (a));
16865 int i (int (3));
16866
16867 The former is a function-declaration; the latter is a
16868 variable initialization.
16869
16870 Thus again, we try a parameter-declaration-clause, and if
16871 that fails, we back out and return. */
16872
16873 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16874 {
16875 tree params;
16876 unsigned saved_num_template_parameter_lists;
16877 bool is_declarator = false;
16878
16879 /* In a member-declarator, the only valid interpretation
16880 of a parenthesis is the start of a
16881 parameter-declaration-clause. (It is invalid to
16882 initialize a static data member with a parenthesized
16883 initializer; only the "=" form of initialization is
16884 permitted.) */
16885 if (!member_p)
16886 cp_parser_parse_tentatively (parser);
16887
16888 /* Consume the `('. */
16889 cp_lexer_consume_token (parser->lexer);
16890 if (first)
16891 {
16892 /* If this is going to be an abstract declarator, we're
16893 in a declarator and we can't have default args. */
16894 parser->default_arg_ok_p = false;
16895 parser->in_declarator_p = true;
16896 }
16897
16898 /* Inside the function parameter list, surrounding
16899 template-parameter-lists do not apply. */
16900 saved_num_template_parameter_lists
16901 = parser->num_template_parameter_lists;
16902 parser->num_template_parameter_lists = 0;
16903
16904 begin_scope (sk_function_parms, NULL_TREE);
16905
16906 /* Parse the parameter-declaration-clause. */
16907 params = cp_parser_parameter_declaration_clause (parser);
16908
16909 /* Restore saved template parameter lists accounting for implicit
16910 template parameters. */
16911 parser->num_template_parameter_lists
16912 += saved_num_template_parameter_lists;
16913
16914 /* Consume the `)'. */
16915 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16916
16917 /* If all went well, parse the cv-qualifier-seq,
16918 ref-qualifier and the exception-specification. */
16919 if (member_p || cp_parser_parse_definitely (parser))
16920 {
16921 cp_cv_quals cv_quals;
16922 cp_virt_specifiers virt_specifiers;
16923 cp_ref_qualifier ref_qual;
16924 tree exception_specification;
16925 tree late_return;
16926 tree attrs;
16927 bool memfn = (member_p || (pushed_scope
16928 && CLASS_TYPE_P (pushed_scope)));
16929
16930 is_declarator = true;
16931
16932 if (ctor_dtor_or_conv_p)
16933 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16934 first = false;
16935
16936 /* Parse the cv-qualifier-seq. */
16937 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16938 /* Parse the ref-qualifier. */
16939 ref_qual = cp_parser_ref_qualifier_opt (parser);
16940 /* And the exception-specification. */
16941 exception_specification
16942 = cp_parser_exception_specification_opt (parser);
16943
16944 attrs = cp_parser_std_attribute_spec_seq (parser);
16945
16946 late_return = (cp_parser_late_return_type_opt
16947 (parser, declarator,
16948 memfn ? cv_quals : -1));
16949
16950
16951 /* Parse the virt-specifier-seq. */
16952 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16953
16954 /* Create the function-declarator. */
16955 declarator = make_call_declarator (declarator,
16956 params,
16957 cv_quals,
16958 virt_specifiers,
16959 ref_qual,
16960 exception_specification,
16961 late_return);
16962 declarator->std_attributes = attrs;
16963 /* Any subsequent parameter lists are to do with
16964 return type, so are not those of the declared
16965 function. */
16966 parser->default_arg_ok_p = false;
16967 }
16968
16969 /* Remove the function parms from scope. */
16970 pop_bindings_and_leave_scope ();
16971
16972 if (is_declarator)
16973 /* Repeat the main loop. */
16974 continue;
16975 }
16976
16977 /* If this is the first, we can try a parenthesized
16978 declarator. */
16979 if (first)
16980 {
16981 bool saved_in_type_id_in_expr_p;
16982
16983 parser->default_arg_ok_p = saved_default_arg_ok_p;
16984 parser->in_declarator_p = saved_in_declarator_p;
16985
16986 /* Consume the `('. */
16987 cp_lexer_consume_token (parser->lexer);
16988 /* Parse the nested declarator. */
16989 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16990 parser->in_type_id_in_expr_p = true;
16991 declarator
16992 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16993 /*parenthesized_p=*/NULL,
16994 member_p);
16995 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16996 first = false;
16997 /* Expect a `)'. */
16998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16999 declarator = cp_error_declarator;
17000 if (declarator == cp_error_declarator)
17001 break;
17002
17003 goto handle_declarator;
17004 }
17005 /* Otherwise, we must be done. */
17006 else
17007 break;
17008 }
17009 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17010 && token->type == CPP_OPEN_SQUARE
17011 && !cp_next_tokens_can_be_attribute_p (parser))
17012 {
17013 /* Parse an array-declarator. */
17014 tree bounds, attrs;
17015
17016 if (ctor_dtor_or_conv_p)
17017 *ctor_dtor_or_conv_p = 0;
17018
17019 first = false;
17020 parser->default_arg_ok_p = false;
17021 parser->in_declarator_p = true;
17022 /* Consume the `['. */
17023 cp_lexer_consume_token (parser->lexer);
17024 /* Peek at the next token. */
17025 token = cp_lexer_peek_token (parser->lexer);
17026 /* If the next token is `]', then there is no
17027 constant-expression. */
17028 if (token->type != CPP_CLOSE_SQUARE)
17029 {
17030 bool non_constant_p;
17031 bounds
17032 = cp_parser_constant_expression (parser,
17033 /*allow_non_constant=*/true,
17034 &non_constant_p);
17035 if (!non_constant_p)
17036 /* OK */;
17037 else if (error_operand_p (bounds))
17038 /* Already gave an error. */;
17039 else if (!parser->in_function_body
17040 || current_binding_level->kind == sk_function_parms)
17041 {
17042 /* Normally, the array bound must be an integral constant
17043 expression. However, as an extension, we allow VLAs
17044 in function scopes as long as they aren't part of a
17045 parameter declaration. */
17046 cp_parser_error (parser,
17047 "array bound is not an integer constant");
17048 bounds = error_mark_node;
17049 }
17050 else if (processing_template_decl)
17051 {
17052 /* Remember this wasn't a constant-expression. */
17053 bounds = build_nop (TREE_TYPE (bounds), bounds);
17054 TREE_SIDE_EFFECTS (bounds) = 1;
17055 }
17056 }
17057 else
17058 bounds = NULL_TREE;
17059 /* Look for the closing `]'. */
17060 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17061 {
17062 declarator = cp_error_declarator;
17063 break;
17064 }
17065
17066 attrs = cp_parser_std_attribute_spec_seq (parser);
17067 declarator = make_array_declarator (declarator, bounds);
17068 declarator->std_attributes = attrs;
17069 }
17070 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17071 {
17072 {
17073 tree qualifying_scope;
17074 tree unqualified_name;
17075 tree attrs;
17076 special_function_kind sfk;
17077 bool abstract_ok;
17078 bool pack_expansion_p = false;
17079 cp_token *declarator_id_start_token;
17080
17081 /* Parse a declarator-id */
17082 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17083 if (abstract_ok)
17084 {
17085 cp_parser_parse_tentatively (parser);
17086
17087 /* If we see an ellipsis, we should be looking at a
17088 parameter pack. */
17089 if (token->type == CPP_ELLIPSIS)
17090 {
17091 /* Consume the `...' */
17092 cp_lexer_consume_token (parser->lexer);
17093
17094 pack_expansion_p = true;
17095 }
17096 }
17097
17098 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17099 unqualified_name
17100 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17101 qualifying_scope = parser->scope;
17102 if (abstract_ok)
17103 {
17104 bool okay = false;
17105
17106 if (!unqualified_name && pack_expansion_p)
17107 {
17108 /* Check whether an error occurred. */
17109 okay = !cp_parser_error_occurred (parser);
17110
17111 /* We already consumed the ellipsis to mark a
17112 parameter pack, but we have no way to report it,
17113 so abort the tentative parse. We will be exiting
17114 immediately anyway. */
17115 cp_parser_abort_tentative_parse (parser);
17116 }
17117 else
17118 okay = cp_parser_parse_definitely (parser);
17119
17120 if (!okay)
17121 unqualified_name = error_mark_node;
17122 else if (unqualified_name
17123 && (qualifying_scope
17124 || (!identifier_p (unqualified_name))))
17125 {
17126 cp_parser_error (parser, "expected unqualified-id");
17127 unqualified_name = error_mark_node;
17128 }
17129 }
17130
17131 if (!unqualified_name)
17132 return NULL;
17133 if (unqualified_name == error_mark_node)
17134 {
17135 declarator = cp_error_declarator;
17136 pack_expansion_p = false;
17137 declarator->parameter_pack_p = false;
17138 break;
17139 }
17140
17141 attrs = cp_parser_std_attribute_spec_seq (parser);
17142
17143 if (qualifying_scope && at_namespace_scope_p ()
17144 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17145 {
17146 /* In the declaration of a member of a template class
17147 outside of the class itself, the SCOPE will sometimes
17148 be a TYPENAME_TYPE. For example, given:
17149
17150 template <typename T>
17151 int S<T>::R::i = 3;
17152
17153 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17154 this context, we must resolve S<T>::R to an ordinary
17155 type, rather than a typename type.
17156
17157 The reason we normally avoid resolving TYPENAME_TYPEs
17158 is that a specialization of `S' might render
17159 `S<T>::R' not a type. However, if `S' is
17160 specialized, then this `i' will not be used, so there
17161 is no harm in resolving the types here. */
17162 tree type;
17163
17164 /* Resolve the TYPENAME_TYPE. */
17165 type = resolve_typename_type (qualifying_scope,
17166 /*only_current_p=*/false);
17167 /* If that failed, the declarator is invalid. */
17168 if (TREE_CODE (type) == TYPENAME_TYPE)
17169 {
17170 if (typedef_variant_p (type))
17171 error_at (declarator_id_start_token->location,
17172 "cannot define member of dependent typedef "
17173 "%qT", type);
17174 else
17175 error_at (declarator_id_start_token->location,
17176 "%<%T::%E%> is not a type",
17177 TYPE_CONTEXT (qualifying_scope),
17178 TYPE_IDENTIFIER (qualifying_scope));
17179 }
17180 qualifying_scope = type;
17181 }
17182
17183 sfk = sfk_none;
17184
17185 if (unqualified_name)
17186 {
17187 tree class_type;
17188
17189 if (qualifying_scope
17190 && CLASS_TYPE_P (qualifying_scope))
17191 class_type = qualifying_scope;
17192 else
17193 class_type = current_class_type;
17194
17195 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17196 {
17197 tree name_type = TREE_TYPE (unqualified_name);
17198 if (class_type && same_type_p (name_type, class_type))
17199 {
17200 if (qualifying_scope
17201 && CLASSTYPE_USE_TEMPLATE (name_type))
17202 {
17203 error_at (declarator_id_start_token->location,
17204 "invalid use of constructor as a template");
17205 inform (declarator_id_start_token->location,
17206 "use %<%T::%D%> instead of %<%T::%D%> to "
17207 "name the constructor in a qualified name",
17208 class_type,
17209 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17210 class_type, name_type);
17211 declarator = cp_error_declarator;
17212 break;
17213 }
17214 else
17215 unqualified_name = constructor_name (class_type);
17216 }
17217 else
17218 {
17219 /* We do not attempt to print the declarator
17220 here because we do not have enough
17221 information about its original syntactic
17222 form. */
17223 cp_parser_error (parser, "invalid declarator");
17224 declarator = cp_error_declarator;
17225 break;
17226 }
17227 }
17228
17229 if (class_type)
17230 {
17231 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17232 sfk = sfk_destructor;
17233 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17234 sfk = sfk_conversion;
17235 else if (/* There's no way to declare a constructor
17236 for an anonymous type, even if the type
17237 got a name for linkage purposes. */
17238 !TYPE_WAS_ANONYMOUS (class_type)
17239 && constructor_name_p (unqualified_name,
17240 class_type))
17241 {
17242 unqualified_name = constructor_name (class_type);
17243 sfk = sfk_constructor;
17244 }
17245 else if (is_overloaded_fn (unqualified_name)
17246 && DECL_CONSTRUCTOR_P (get_first_fn
17247 (unqualified_name)))
17248 sfk = sfk_constructor;
17249
17250 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17251 *ctor_dtor_or_conv_p = -1;
17252 }
17253 }
17254 declarator = make_id_declarator (qualifying_scope,
17255 unqualified_name,
17256 sfk);
17257 declarator->std_attributes = attrs;
17258 declarator->id_loc = token->location;
17259 declarator->parameter_pack_p = pack_expansion_p;
17260
17261 if (pack_expansion_p)
17262 maybe_warn_variadic_templates ();
17263 }
17264
17265 handle_declarator:;
17266 scope = get_scope_of_declarator (declarator);
17267 if (scope)
17268 {
17269 /* Any names that appear after the declarator-id for a
17270 member are looked up in the containing scope. */
17271 if (at_function_scope_p ())
17272 {
17273 /* But declarations with qualified-ids can't appear in a
17274 function. */
17275 cp_parser_error (parser, "qualified-id in declaration");
17276 break;
17277 }
17278 pushed_scope = push_scope (scope);
17279 }
17280 parser->in_declarator_p = true;
17281 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17282 || (declarator && declarator->kind == cdk_id))
17283 /* Default args are only allowed on function
17284 declarations. */
17285 parser->default_arg_ok_p = saved_default_arg_ok_p;
17286 else
17287 parser->default_arg_ok_p = false;
17288
17289 first = false;
17290 }
17291 /* We're done. */
17292 else
17293 break;
17294 }
17295
17296 /* For an abstract declarator, we might wind up with nothing at this
17297 point. That's an error; the declarator is not optional. */
17298 if (!declarator)
17299 cp_parser_error (parser, "expected declarator");
17300
17301 /* If we entered a scope, we must exit it now. */
17302 if (pushed_scope)
17303 pop_scope (pushed_scope);
17304
17305 parser->default_arg_ok_p = saved_default_arg_ok_p;
17306 parser->in_declarator_p = saved_in_declarator_p;
17307
17308 return declarator;
17309 }
17310
17311 /* Parse a ptr-operator.
17312
17313 ptr-operator:
17314 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17315 * cv-qualifier-seq [opt]
17316 &
17317 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17318 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17319
17320 GNU Extension:
17321
17322 ptr-operator:
17323 & cv-qualifier-seq [opt]
17324
17325 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17326 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17327 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17328 filled in with the TYPE containing the member. *CV_QUALS is
17329 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17330 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17331 Note that the tree codes returned by this function have nothing
17332 to do with the types of trees that will be eventually be created
17333 to represent the pointer or reference type being parsed. They are
17334 just constants with suggestive names. */
17335 static enum tree_code
17336 cp_parser_ptr_operator (cp_parser* parser,
17337 tree* type,
17338 cp_cv_quals *cv_quals,
17339 tree *attributes)
17340 {
17341 enum tree_code code = ERROR_MARK;
17342 cp_token *token;
17343 tree attrs = NULL_TREE;
17344
17345 /* Assume that it's not a pointer-to-member. */
17346 *type = NULL_TREE;
17347 /* And that there are no cv-qualifiers. */
17348 *cv_quals = TYPE_UNQUALIFIED;
17349
17350 /* Peek at the next token. */
17351 token = cp_lexer_peek_token (parser->lexer);
17352
17353 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17354 if (token->type == CPP_MULT)
17355 code = INDIRECT_REF;
17356 else if (token->type == CPP_AND)
17357 code = ADDR_EXPR;
17358 else if ((cxx_dialect != cxx98) &&
17359 token->type == CPP_AND_AND) /* C++0x only */
17360 code = NON_LVALUE_EXPR;
17361
17362 if (code != ERROR_MARK)
17363 {
17364 /* Consume the `*', `&' or `&&'. */
17365 cp_lexer_consume_token (parser->lexer);
17366
17367 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17368 `&', if we are allowing GNU extensions. (The only qualifier
17369 that can legally appear after `&' is `restrict', but that is
17370 enforced during semantic analysis. */
17371 if (code == INDIRECT_REF
17372 || cp_parser_allow_gnu_extensions_p (parser))
17373 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17374
17375 attrs = cp_parser_std_attribute_spec_seq (parser);
17376 if (attributes != NULL)
17377 *attributes = attrs;
17378 }
17379 else
17380 {
17381 /* Try the pointer-to-member case. */
17382 cp_parser_parse_tentatively (parser);
17383 /* Look for the optional `::' operator. */
17384 cp_parser_global_scope_opt (parser,
17385 /*current_scope_valid_p=*/false);
17386 /* Look for the nested-name specifier. */
17387 token = cp_lexer_peek_token (parser->lexer);
17388 cp_parser_nested_name_specifier (parser,
17389 /*typename_keyword_p=*/false,
17390 /*check_dependency_p=*/true,
17391 /*type_p=*/false,
17392 /*is_declaration=*/false);
17393 /* If we found it, and the next token is a `*', then we are
17394 indeed looking at a pointer-to-member operator. */
17395 if (!cp_parser_error_occurred (parser)
17396 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17397 {
17398 /* Indicate that the `*' operator was used. */
17399 code = INDIRECT_REF;
17400
17401 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17402 error_at (token->location, "%qD is a namespace", parser->scope);
17403 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17404 error_at (token->location, "cannot form pointer to member of "
17405 "non-class %q#T", parser->scope);
17406 else
17407 {
17408 /* The type of which the member is a member is given by the
17409 current SCOPE. */
17410 *type = parser->scope;
17411 /* The next name will not be qualified. */
17412 parser->scope = NULL_TREE;
17413 parser->qualifying_scope = NULL_TREE;
17414 parser->object_scope = NULL_TREE;
17415 /* Look for optional c++11 attributes. */
17416 attrs = cp_parser_std_attribute_spec_seq (parser);
17417 if (attributes != NULL)
17418 *attributes = attrs;
17419 /* Look for the optional cv-qualifier-seq. */
17420 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17421 }
17422 }
17423 /* If that didn't work we don't have a ptr-operator. */
17424 if (!cp_parser_parse_definitely (parser))
17425 cp_parser_error (parser, "expected ptr-operator");
17426 }
17427
17428 return code;
17429 }
17430
17431 /* Parse an (optional) cv-qualifier-seq.
17432
17433 cv-qualifier-seq:
17434 cv-qualifier cv-qualifier-seq [opt]
17435
17436 cv-qualifier:
17437 const
17438 volatile
17439
17440 GNU Extension:
17441
17442 cv-qualifier:
17443 __restrict__
17444
17445 Returns a bitmask representing the cv-qualifiers. */
17446
17447 static cp_cv_quals
17448 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17449 {
17450 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17451
17452 while (true)
17453 {
17454 cp_token *token;
17455 cp_cv_quals cv_qualifier;
17456
17457 /* Peek at the next token. */
17458 token = cp_lexer_peek_token (parser->lexer);
17459 /* See if it's a cv-qualifier. */
17460 switch (token->keyword)
17461 {
17462 case RID_CONST:
17463 cv_qualifier = TYPE_QUAL_CONST;
17464 break;
17465
17466 case RID_VOLATILE:
17467 cv_qualifier = TYPE_QUAL_VOLATILE;
17468 break;
17469
17470 case RID_RESTRICT:
17471 cv_qualifier = TYPE_QUAL_RESTRICT;
17472 break;
17473
17474 default:
17475 cv_qualifier = TYPE_UNQUALIFIED;
17476 break;
17477 }
17478
17479 if (!cv_qualifier)
17480 break;
17481
17482 if (cv_quals & cv_qualifier)
17483 {
17484 error_at (token->location, "duplicate cv-qualifier");
17485 cp_lexer_purge_token (parser->lexer);
17486 }
17487 else
17488 {
17489 cp_lexer_consume_token (parser->lexer);
17490 cv_quals |= cv_qualifier;
17491 }
17492 }
17493
17494 return cv_quals;
17495 }
17496
17497 /* Parse an (optional) ref-qualifier
17498
17499 ref-qualifier:
17500 &
17501 &&
17502
17503 Returns cp_ref_qualifier representing ref-qualifier. */
17504
17505 static cp_ref_qualifier
17506 cp_parser_ref_qualifier_opt (cp_parser* parser)
17507 {
17508 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17509
17510 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17511 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17512 return ref_qual;
17513
17514 while (true)
17515 {
17516 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17517 cp_token *token = cp_lexer_peek_token (parser->lexer);
17518
17519 switch (token->type)
17520 {
17521 case CPP_AND:
17522 curr_ref_qual = REF_QUAL_LVALUE;
17523 break;
17524
17525 case CPP_AND_AND:
17526 curr_ref_qual = REF_QUAL_RVALUE;
17527 break;
17528
17529 default:
17530 curr_ref_qual = REF_QUAL_NONE;
17531 break;
17532 }
17533
17534 if (!curr_ref_qual)
17535 break;
17536 else if (ref_qual)
17537 {
17538 error_at (token->location, "multiple ref-qualifiers");
17539 cp_lexer_purge_token (parser->lexer);
17540 }
17541 else
17542 {
17543 ref_qual = curr_ref_qual;
17544 cp_lexer_consume_token (parser->lexer);
17545 }
17546 }
17547
17548 return ref_qual;
17549 }
17550
17551 /* Parse an (optional) virt-specifier-seq.
17552
17553 virt-specifier-seq:
17554 virt-specifier virt-specifier-seq [opt]
17555
17556 virt-specifier:
17557 override
17558 final
17559
17560 Returns a bitmask representing the virt-specifiers. */
17561
17562 static cp_virt_specifiers
17563 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17564 {
17565 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17566
17567 while (true)
17568 {
17569 cp_token *token;
17570 cp_virt_specifiers virt_specifier;
17571
17572 /* Peek at the next token. */
17573 token = cp_lexer_peek_token (parser->lexer);
17574 /* See if it's a virt-specifier-qualifier. */
17575 if (token->type != CPP_NAME)
17576 break;
17577 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17578 {
17579 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17580 virt_specifier = VIRT_SPEC_OVERRIDE;
17581 }
17582 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17583 {
17584 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17585 virt_specifier = VIRT_SPEC_FINAL;
17586 }
17587 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17588 {
17589 virt_specifier = VIRT_SPEC_FINAL;
17590 }
17591 else
17592 break;
17593
17594 if (virt_specifiers & virt_specifier)
17595 {
17596 error_at (token->location, "duplicate virt-specifier");
17597 cp_lexer_purge_token (parser->lexer);
17598 }
17599 else
17600 {
17601 cp_lexer_consume_token (parser->lexer);
17602 virt_specifiers |= virt_specifier;
17603 }
17604 }
17605 return virt_specifiers;
17606 }
17607
17608 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17609 is in scope even though it isn't real. */
17610
17611 static void
17612 inject_this_parameter (tree ctype, cp_cv_quals quals)
17613 {
17614 tree this_parm;
17615
17616 if (current_class_ptr)
17617 {
17618 /* We don't clear this between NSDMIs. Is it already what we want? */
17619 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17620 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17621 && cp_type_quals (type) == quals)
17622 return;
17623 }
17624
17625 this_parm = build_this_parm (ctype, quals);
17626 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17627 current_class_ptr = NULL_TREE;
17628 current_class_ref
17629 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17630 current_class_ptr = this_parm;
17631 }
17632
17633 /* Return true iff our current scope is a non-static data member
17634 initializer. */
17635
17636 bool
17637 parsing_nsdmi (void)
17638 {
17639 /* We recognize NSDMI context by the context-less 'this' pointer set up
17640 by the function above. */
17641 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17642 return true;
17643 return false;
17644 }
17645
17646 /* Parse a late-specified return type, if any. This is not a separate
17647 non-terminal, but part of a function declarator, which looks like
17648
17649 -> trailing-type-specifier-seq abstract-declarator(opt)
17650
17651 Returns the type indicated by the type-id.
17652
17653 In addition to this this parses any queued up omp declare simd
17654 clauses.
17655
17656 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17657 function. */
17658
17659 static tree
17660 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17661 cp_cv_quals quals)
17662 {
17663 cp_token *token;
17664 tree type = NULL_TREE;
17665 bool declare_simd_p = (parser->omp_declare_simd
17666 && declarator
17667 && declarator->kind == cdk_id);
17668
17669 /* Peek at the next token. */
17670 token = cp_lexer_peek_token (parser->lexer);
17671 /* A late-specified return type is indicated by an initial '->'. */
17672 if (token->type != CPP_DEREF && !declare_simd_p)
17673 return NULL_TREE;
17674
17675 tree save_ccp = current_class_ptr;
17676 tree save_ccr = current_class_ref;
17677 if (quals >= 0)
17678 {
17679 /* DR 1207: 'this' is in scope in the trailing return type. */
17680 inject_this_parameter (current_class_type, quals);
17681 }
17682
17683 if (token->type == CPP_DEREF)
17684 {
17685 /* Consume the ->. */
17686 cp_lexer_consume_token (parser->lexer);
17687
17688 type = cp_parser_trailing_type_id (parser);
17689 }
17690
17691 if (declare_simd_p)
17692 declarator->std_attributes
17693 = cp_parser_late_parsing_omp_declare_simd (parser,
17694 declarator->std_attributes);
17695
17696 if (quals >= 0)
17697 {
17698 current_class_ptr = save_ccp;
17699 current_class_ref = save_ccr;
17700 }
17701
17702 return type;
17703 }
17704
17705 /* Parse a declarator-id.
17706
17707 declarator-id:
17708 id-expression
17709 :: [opt] nested-name-specifier [opt] type-name
17710
17711 In the `id-expression' case, the value returned is as for
17712 cp_parser_id_expression if the id-expression was an unqualified-id.
17713 If the id-expression was a qualified-id, then a SCOPE_REF is
17714 returned. The first operand is the scope (either a NAMESPACE_DECL
17715 or TREE_TYPE), but the second is still just a representation of an
17716 unqualified-id. */
17717
17718 static tree
17719 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17720 {
17721 tree id;
17722 /* The expression must be an id-expression. Assume that qualified
17723 names are the names of types so that:
17724
17725 template <class T>
17726 int S<T>::R::i = 3;
17727
17728 will work; we must treat `S<T>::R' as the name of a type.
17729 Similarly, assume that qualified names are templates, where
17730 required, so that:
17731
17732 template <class T>
17733 int S<T>::R<T>::i = 3;
17734
17735 will work, too. */
17736 id = cp_parser_id_expression (parser,
17737 /*template_keyword_p=*/false,
17738 /*check_dependency_p=*/false,
17739 /*template_p=*/NULL,
17740 /*declarator_p=*/true,
17741 optional_p);
17742 if (id && BASELINK_P (id))
17743 id = BASELINK_FUNCTIONS (id);
17744 return id;
17745 }
17746
17747 /* Parse a type-id.
17748
17749 type-id:
17750 type-specifier-seq abstract-declarator [opt]
17751
17752 Returns the TYPE specified. */
17753
17754 static tree
17755 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17756 bool is_trailing_return)
17757 {
17758 cp_decl_specifier_seq type_specifier_seq;
17759 cp_declarator *abstract_declarator;
17760
17761 /* Parse the type-specifier-seq. */
17762 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17763 is_trailing_return,
17764 &type_specifier_seq);
17765 if (type_specifier_seq.type == error_mark_node)
17766 return error_mark_node;
17767
17768 /* There might or might not be an abstract declarator. */
17769 cp_parser_parse_tentatively (parser);
17770 /* Look for the declarator. */
17771 abstract_declarator
17772 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17773 /*parenthesized_p=*/NULL,
17774 /*member_p=*/false);
17775 /* Check to see if there really was a declarator. */
17776 if (!cp_parser_parse_definitely (parser))
17777 abstract_declarator = NULL;
17778
17779 if (type_specifier_seq.type
17780 && cxx_dialect < cxx1y
17781 && type_uses_auto (type_specifier_seq.type))
17782 {
17783 /* A type-id with type 'auto' is only ok if the abstract declarator
17784 is a function declarator with a late-specified return type. */
17785 if (abstract_declarator
17786 && abstract_declarator->kind == cdk_function
17787 && abstract_declarator->u.function.late_return_type)
17788 /* OK */;
17789 else
17790 {
17791 error ("invalid use of %<auto%>");
17792 return error_mark_node;
17793 }
17794 }
17795
17796 return groktypename (&type_specifier_seq, abstract_declarator,
17797 is_template_arg);
17798 }
17799
17800 static tree cp_parser_type_id (cp_parser *parser)
17801 {
17802 return cp_parser_type_id_1 (parser, false, false);
17803 }
17804
17805 static tree cp_parser_template_type_arg (cp_parser *parser)
17806 {
17807 tree r;
17808 const char *saved_message = parser->type_definition_forbidden_message;
17809 parser->type_definition_forbidden_message
17810 = G_("types may not be defined in template arguments");
17811 r = cp_parser_type_id_1 (parser, true, false);
17812 parser->type_definition_forbidden_message = saved_message;
17813 return r;
17814 }
17815
17816 static tree cp_parser_trailing_type_id (cp_parser *parser)
17817 {
17818 return cp_parser_type_id_1 (parser, false, true);
17819 }
17820
17821 /* Parse a type-specifier-seq.
17822
17823 type-specifier-seq:
17824 type-specifier type-specifier-seq [opt]
17825
17826 GNU extension:
17827
17828 type-specifier-seq:
17829 attributes type-specifier-seq [opt]
17830
17831 If IS_DECLARATION is true, we are at the start of a "condition" or
17832 exception-declaration, so we might be followed by a declarator-id.
17833
17834 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17835 i.e. we've just seen "->".
17836
17837 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17838
17839 static void
17840 cp_parser_type_specifier_seq (cp_parser* parser,
17841 bool is_declaration,
17842 bool is_trailing_return,
17843 cp_decl_specifier_seq *type_specifier_seq)
17844 {
17845 bool seen_type_specifier = false;
17846 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17847 cp_token *start_token = NULL;
17848
17849 /* Clear the TYPE_SPECIFIER_SEQ. */
17850 clear_decl_specs (type_specifier_seq);
17851
17852 /* In the context of a trailing return type, enum E { } is an
17853 elaborated-type-specifier followed by a function-body, not an
17854 enum-specifier. */
17855 if (is_trailing_return)
17856 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17857
17858 /* Parse the type-specifiers and attributes. */
17859 while (true)
17860 {
17861 tree type_specifier;
17862 bool is_cv_qualifier;
17863
17864 /* Check for attributes first. */
17865 if (cp_next_tokens_can_be_attribute_p (parser))
17866 {
17867 type_specifier_seq->attributes =
17868 chainon (type_specifier_seq->attributes,
17869 cp_parser_attributes_opt (parser));
17870 continue;
17871 }
17872
17873 /* record the token of the beginning of the type specifier seq,
17874 for error reporting purposes*/
17875 if (!start_token)
17876 start_token = cp_lexer_peek_token (parser->lexer);
17877
17878 /* Look for the type-specifier. */
17879 type_specifier = cp_parser_type_specifier (parser,
17880 flags,
17881 type_specifier_seq,
17882 /*is_declaration=*/false,
17883 NULL,
17884 &is_cv_qualifier);
17885 if (!type_specifier)
17886 {
17887 /* If the first type-specifier could not be found, this is not a
17888 type-specifier-seq at all. */
17889 if (!seen_type_specifier)
17890 {
17891 cp_parser_error (parser, "expected type-specifier");
17892 type_specifier_seq->type = error_mark_node;
17893 return;
17894 }
17895 /* If subsequent type-specifiers could not be found, the
17896 type-specifier-seq is complete. */
17897 break;
17898 }
17899
17900 seen_type_specifier = true;
17901 /* The standard says that a condition can be:
17902
17903 type-specifier-seq declarator = assignment-expression
17904
17905 However, given:
17906
17907 struct S {};
17908 if (int S = ...)
17909
17910 we should treat the "S" as a declarator, not as a
17911 type-specifier. The standard doesn't say that explicitly for
17912 type-specifier-seq, but it does say that for
17913 decl-specifier-seq in an ordinary declaration. Perhaps it
17914 would be clearer just to allow a decl-specifier-seq here, and
17915 then add a semantic restriction that if any decl-specifiers
17916 that are not type-specifiers appear, the program is invalid. */
17917 if (is_declaration && !is_cv_qualifier)
17918 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17919 }
17920 }
17921
17922 /* Parse a parameter-declaration-clause.
17923
17924 parameter-declaration-clause:
17925 parameter-declaration-list [opt] ... [opt]
17926 parameter-declaration-list , ...
17927
17928 Returns a representation for the parameter declarations. A return
17929 value of NULL indicates a parameter-declaration-clause consisting
17930 only of an ellipsis. */
17931
17932 static tree
17933 cp_parser_parameter_declaration_clause (cp_parser* parser)
17934 {
17935 tree parameters;
17936 cp_token *token;
17937 bool ellipsis_p;
17938 bool is_error;
17939
17940 /* Peek at the next token. */
17941 token = cp_lexer_peek_token (parser->lexer);
17942 /* Check for trivial parameter-declaration-clauses. */
17943 if (token->type == CPP_ELLIPSIS)
17944 {
17945 /* Consume the `...' token. */
17946 cp_lexer_consume_token (parser->lexer);
17947 return NULL_TREE;
17948 }
17949 else if (token->type == CPP_CLOSE_PAREN)
17950 /* There are no parameters. */
17951 {
17952 #ifndef NO_IMPLICIT_EXTERN_C
17953 if (in_system_header && current_class_type == NULL
17954 && current_lang_name == lang_name_c)
17955 return NULL_TREE;
17956 else
17957 #endif
17958 return void_list_node;
17959 }
17960 /* Check for `(void)', too, which is a special case. */
17961 else if (token->keyword == RID_VOID
17962 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17963 == CPP_CLOSE_PAREN))
17964 {
17965 /* Consume the `void' token. */
17966 cp_lexer_consume_token (parser->lexer);
17967 /* There are no parameters. */
17968 return void_list_node;
17969 }
17970
17971 /* Parse the parameter-declaration-list. */
17972 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17973 /* If a parse error occurred while parsing the
17974 parameter-declaration-list, then the entire
17975 parameter-declaration-clause is erroneous. */
17976 if (is_error)
17977 return NULL;
17978
17979 /* Peek at the next token. */
17980 token = cp_lexer_peek_token (parser->lexer);
17981 /* If it's a `,', the clause should terminate with an ellipsis. */
17982 if (token->type == CPP_COMMA)
17983 {
17984 /* Consume the `,'. */
17985 cp_lexer_consume_token (parser->lexer);
17986 /* Expect an ellipsis. */
17987 ellipsis_p
17988 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17989 }
17990 /* It might also be `...' if the optional trailing `,' was
17991 omitted. */
17992 else if (token->type == CPP_ELLIPSIS)
17993 {
17994 /* Consume the `...' token. */
17995 cp_lexer_consume_token (parser->lexer);
17996 /* And remember that we saw it. */
17997 ellipsis_p = true;
17998 }
17999 else
18000 ellipsis_p = false;
18001
18002 /* Finish the parameter list. */
18003 if (!ellipsis_p)
18004 parameters = chainon (parameters, void_list_node);
18005
18006 return parameters;
18007 }
18008
18009 /* Parse a parameter-declaration-list.
18010
18011 parameter-declaration-list:
18012 parameter-declaration
18013 parameter-declaration-list , parameter-declaration
18014
18015 Returns a representation of the parameter-declaration-list, as for
18016 cp_parser_parameter_declaration_clause. However, the
18017 `void_list_node' is never appended to the list. Upon return,
18018 *IS_ERROR will be true iff an error occurred. */
18019
18020 static tree
18021 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18022 {
18023 tree parameters = NULL_TREE;
18024 tree *tail = &parameters;
18025 bool saved_in_unbraced_linkage_specification_p;
18026 int index = 0;
18027 int implicit_template_parms = 0;
18028
18029 /* Assume all will go well. */
18030 *is_error = false;
18031 /* The special considerations that apply to a function within an
18032 unbraced linkage specifications do not apply to the parameters
18033 to the function. */
18034 saved_in_unbraced_linkage_specification_p
18035 = parser->in_unbraced_linkage_specification_p;
18036 parser->in_unbraced_linkage_specification_p = false;
18037
18038 /* Look for more parameters. */
18039 while (true)
18040 {
18041 cp_parameter_declarator *parameter;
18042 tree decl = error_mark_node;
18043 bool parenthesized_p = false;
18044 /* Parse the parameter. */
18045 parameter
18046 = cp_parser_parameter_declaration (parser,
18047 /*template_parm_p=*/false,
18048 &parenthesized_p);
18049
18050 /* We don't know yet if the enclosing context is deprecated, so wait
18051 and warn in grokparms if appropriate. */
18052 deprecated_state = DEPRECATED_SUPPRESS;
18053
18054 if (parameter)
18055 {
18056 decl = grokdeclarator (parameter->declarator,
18057 &parameter->decl_specifiers,
18058 PARM,
18059 parameter->default_argument != NULL_TREE,
18060 &parameter->decl_specifiers.attributes);
18061
18062 if (TREE_TYPE (decl) != error_mark_node
18063 && parameter->decl_specifiers.type
18064 && is_auto_or_concept (parameter->decl_specifiers.type))
18065 ++implicit_template_parms;
18066 }
18067
18068 deprecated_state = DEPRECATED_NORMAL;
18069
18070 /* If a parse error occurred parsing the parameter declaration,
18071 then the entire parameter-declaration-list is erroneous. */
18072 if (decl == error_mark_node)
18073 {
18074 *is_error = true;
18075 parameters = error_mark_node;
18076 break;
18077 }
18078
18079 if (parameter->decl_specifiers.attributes)
18080 cplus_decl_attributes (&decl,
18081 parameter->decl_specifiers.attributes,
18082 0);
18083 if (DECL_NAME (decl))
18084 decl = pushdecl (decl);
18085
18086 if (decl != error_mark_node)
18087 {
18088 retrofit_lang_decl (decl);
18089 DECL_PARM_INDEX (decl) = ++index;
18090 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18091 }
18092
18093 /* Add the new parameter to the list. */
18094 *tail = build_tree_list (parameter->default_argument, decl);
18095 tail = &TREE_CHAIN (*tail);
18096
18097 /* Peek at the next token. */
18098 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18099 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18100 /* These are for Objective-C++ */
18101 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18102 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18103 /* The parameter-declaration-list is complete. */
18104 break;
18105 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18106 {
18107 cp_token *token;
18108
18109 /* Peek at the next token. */
18110 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18111 /* If it's an ellipsis, then the list is complete. */
18112 if (token->type == CPP_ELLIPSIS)
18113 break;
18114 /* Otherwise, there must be more parameters. Consume the
18115 `,'. */
18116 cp_lexer_consume_token (parser->lexer);
18117 /* When parsing something like:
18118
18119 int i(float f, double d)
18120
18121 we can tell after seeing the declaration for "f" that we
18122 are not looking at an initialization of a variable "i",
18123 but rather at the declaration of a function "i".
18124
18125 Due to the fact that the parsing of template arguments
18126 (as specified to a template-id) requires backtracking we
18127 cannot use this technique when inside a template argument
18128 list. */
18129 if (!parser->in_template_argument_list_p
18130 && !parser->in_type_id_in_expr_p
18131 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18132 /* However, a parameter-declaration of the form
18133 "float(f)" (which is a valid declaration of a
18134 parameter "f") can also be interpreted as an
18135 expression (the conversion of "f" to "float"). */
18136 && !parenthesized_p)
18137 cp_parser_commit_to_tentative_parse (parser);
18138 }
18139 else
18140 {
18141 cp_parser_error (parser, "expected %<,%> or %<...%>");
18142 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18143 cp_parser_skip_to_closing_parenthesis (parser,
18144 /*recovering=*/true,
18145 /*or_comma=*/false,
18146 /*consume_paren=*/false);
18147 break;
18148 }
18149 }
18150
18151 parser->in_unbraced_linkage_specification_p
18152 = saved_in_unbraced_linkage_specification_p;
18153
18154 if (parameters != error_mark_node && implicit_template_parms)
18155 parameters = add_implicit_template_parms (parser,
18156 implicit_template_parms,
18157 parameters);
18158
18159 return parameters;
18160 }
18161
18162 /* Parse a parameter declaration.
18163
18164 parameter-declaration:
18165 decl-specifier-seq ... [opt] declarator
18166 decl-specifier-seq declarator = assignment-expression
18167 decl-specifier-seq ... [opt] abstract-declarator [opt]
18168 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18169
18170 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18171 declares a template parameter. (In that case, a non-nested `>'
18172 token encountered during the parsing of the assignment-expression
18173 is not interpreted as a greater-than operator.)
18174
18175 Returns a representation of the parameter, or NULL if an error
18176 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18177 true iff the declarator is of the form "(p)". */
18178
18179 static cp_parameter_declarator *
18180 cp_parser_parameter_declaration (cp_parser *parser,
18181 bool template_parm_p,
18182 bool *parenthesized_p)
18183 {
18184 int declares_class_or_enum;
18185 cp_decl_specifier_seq decl_specifiers;
18186 cp_declarator *declarator;
18187 tree default_argument;
18188 cp_token *token = NULL, *declarator_token_start = NULL;
18189 const char *saved_message;
18190
18191 /* In a template parameter, `>' is not an operator.
18192
18193 [temp.param]
18194
18195 When parsing a default template-argument for a non-type
18196 template-parameter, the first non-nested `>' is taken as the end
18197 of the template parameter-list rather than a greater-than
18198 operator. */
18199
18200 /* Type definitions may not appear in parameter types. */
18201 saved_message = parser->type_definition_forbidden_message;
18202 parser->type_definition_forbidden_message
18203 = G_("types may not be defined in parameter types");
18204
18205 /* Parse the declaration-specifiers. */
18206 cp_parser_decl_specifier_seq (parser,
18207 CP_PARSER_FLAGS_NONE,
18208 &decl_specifiers,
18209 &declares_class_or_enum);
18210
18211 /* Complain about missing 'typename' or other invalid type names. */
18212 if (!decl_specifiers.any_type_specifiers_p
18213 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18214 decl_specifiers.type = error_mark_node;
18215
18216 /* If an error occurred, there's no reason to attempt to parse the
18217 rest of the declaration. */
18218 if (cp_parser_error_occurred (parser))
18219 {
18220 parser->type_definition_forbidden_message = saved_message;
18221 return NULL;
18222 }
18223
18224 /* Peek at the next token. */
18225 token = cp_lexer_peek_token (parser->lexer);
18226
18227 /* If the next token is a `)', `,', `=', `>', or `...', then there
18228 is no declarator. However, when variadic templates are enabled,
18229 there may be a declarator following `...'. */
18230 if (token->type == CPP_CLOSE_PAREN
18231 || token->type == CPP_COMMA
18232 || token->type == CPP_EQ
18233 || token->type == CPP_GREATER)
18234 {
18235 declarator = NULL;
18236 if (parenthesized_p)
18237 *parenthesized_p = false;
18238 }
18239 /* Otherwise, there should be a declarator. */
18240 else
18241 {
18242 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18243 parser->default_arg_ok_p = false;
18244
18245 /* After seeing a decl-specifier-seq, if the next token is not a
18246 "(", there is no possibility that the code is a valid
18247 expression. Therefore, if parsing tentatively, we commit at
18248 this point. */
18249 if (!parser->in_template_argument_list_p
18250 /* In an expression context, having seen:
18251
18252 (int((char ...
18253
18254 we cannot be sure whether we are looking at a
18255 function-type (taking a "char" as a parameter) or a cast
18256 of some object of type "char" to "int". */
18257 && !parser->in_type_id_in_expr_p
18258 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18259 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18260 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18261 cp_parser_commit_to_tentative_parse (parser);
18262 /* Parse the declarator. */
18263 declarator_token_start = token;
18264 declarator = cp_parser_declarator (parser,
18265 CP_PARSER_DECLARATOR_EITHER,
18266 /*ctor_dtor_or_conv_p=*/NULL,
18267 parenthesized_p,
18268 /*member_p=*/false);
18269 parser->default_arg_ok_p = saved_default_arg_ok_p;
18270 /* After the declarator, allow more attributes. */
18271 decl_specifiers.attributes
18272 = chainon (decl_specifiers.attributes,
18273 cp_parser_attributes_opt (parser));
18274 }
18275
18276 /* If the next token is an ellipsis, and we have not seen a
18277 declarator name, and the type of the declarator contains parameter
18278 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18279 a parameter pack expansion expression. Otherwise, leave the
18280 ellipsis for a C-style variadic function. */
18281 token = cp_lexer_peek_token (parser->lexer);
18282 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18283 {
18284 tree type = decl_specifiers.type;
18285
18286 if (type && DECL_P (type))
18287 type = TREE_TYPE (type);
18288
18289 if (type
18290 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18291 && declarator_can_be_parameter_pack (declarator)
18292 && (!declarator || !declarator->parameter_pack_p)
18293 && uses_parameter_packs (type))
18294 {
18295 /* Consume the `...'. */
18296 cp_lexer_consume_token (parser->lexer);
18297 maybe_warn_variadic_templates ();
18298
18299 /* Build a pack expansion type */
18300 if (declarator)
18301 declarator->parameter_pack_p = true;
18302 else
18303 decl_specifiers.type = make_pack_expansion (type);
18304 }
18305 }
18306
18307 /* The restriction on defining new types applies only to the type
18308 of the parameter, not to the default argument. */
18309 parser->type_definition_forbidden_message = saved_message;
18310
18311 /* If the next token is `=', then process a default argument. */
18312 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18313 {
18314 token = cp_lexer_peek_token (parser->lexer);
18315 /* If we are defining a class, then the tokens that make up the
18316 default argument must be saved and processed later. */
18317 if (!template_parm_p && at_class_scope_p ()
18318 && TYPE_BEING_DEFINED (current_class_type)
18319 && !LAMBDA_TYPE_P (current_class_type))
18320 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18321 /* Outside of a class definition, we can just parse the
18322 assignment-expression. */
18323 else
18324 default_argument
18325 = cp_parser_default_argument (parser, template_parm_p);
18326
18327 if (!parser->default_arg_ok_p)
18328 {
18329 if (flag_permissive)
18330 warning (0, "deprecated use of default argument for parameter of non-function");
18331 else
18332 {
18333 error_at (token->location,
18334 "default arguments are only "
18335 "permitted for function parameters");
18336 default_argument = NULL_TREE;
18337 }
18338 }
18339 else if ((declarator && declarator->parameter_pack_p)
18340 || (decl_specifiers.type
18341 && PACK_EXPANSION_P (decl_specifiers.type)))
18342 {
18343 /* Find the name of the parameter pack. */
18344 cp_declarator *id_declarator = declarator;
18345 while (id_declarator && id_declarator->kind != cdk_id)
18346 id_declarator = id_declarator->declarator;
18347
18348 if (id_declarator && id_declarator->kind == cdk_id)
18349 error_at (declarator_token_start->location,
18350 template_parm_p
18351 ? G_("template parameter pack %qD "
18352 "cannot have a default argument")
18353 : G_("parameter pack %qD cannot have "
18354 "a default argument"),
18355 id_declarator->u.id.unqualified_name);
18356 else
18357 error_at (declarator_token_start->location,
18358 template_parm_p
18359 ? G_("template parameter pack cannot have "
18360 "a default argument")
18361 : G_("parameter pack cannot have a "
18362 "default argument"));
18363
18364 default_argument = NULL_TREE;
18365 }
18366 }
18367 else
18368 default_argument = NULL_TREE;
18369
18370 return make_parameter_declarator (&decl_specifiers,
18371 declarator,
18372 default_argument);
18373 }
18374
18375 /* Parse a default argument and return it.
18376
18377 TEMPLATE_PARM_P is true if this is a default argument for a
18378 non-type template parameter. */
18379 static tree
18380 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18381 {
18382 tree default_argument = NULL_TREE;
18383 bool saved_greater_than_is_operator_p;
18384 bool saved_local_variables_forbidden_p;
18385 bool non_constant_p, is_direct_init;
18386
18387 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18388 set correctly. */
18389 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18390 parser->greater_than_is_operator_p = !template_parm_p;
18391 /* Local variable names (and the `this' keyword) may not
18392 appear in a default argument. */
18393 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18394 parser->local_variables_forbidden_p = true;
18395 /* Parse the assignment-expression. */
18396 if (template_parm_p)
18397 push_deferring_access_checks (dk_no_deferred);
18398 default_argument
18399 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18400 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18401 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18402 if (template_parm_p)
18403 pop_deferring_access_checks ();
18404 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18405 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18406
18407 return default_argument;
18408 }
18409
18410 /* Parse a function-body.
18411
18412 function-body:
18413 compound_statement */
18414
18415 static void
18416 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18417 {
18418 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18419 }
18420
18421 /* Parse a ctor-initializer-opt followed by a function-body. Return
18422 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18423 is true we are parsing a function-try-block. */
18424
18425 static bool
18426 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18427 bool in_function_try_block)
18428 {
18429 tree body, list;
18430 bool ctor_initializer_p;
18431 const bool check_body_p =
18432 DECL_CONSTRUCTOR_P (current_function_decl)
18433 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18434 tree last = NULL;
18435
18436 /* Begin the function body. */
18437 body = begin_function_body ();
18438 /* Parse the optional ctor-initializer. */
18439 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18440
18441 /* If we're parsing a constexpr constructor definition, we need
18442 to check that the constructor body is indeed empty. However,
18443 before we get to cp_parser_function_body lot of junk has been
18444 generated, so we can't just check that we have an empty block.
18445 Rather we take a snapshot of the outermost block, and check whether
18446 cp_parser_function_body changed its state. */
18447 if (check_body_p)
18448 {
18449 list = cur_stmt_list;
18450 if (STATEMENT_LIST_TAIL (list))
18451 last = STATEMENT_LIST_TAIL (list)->stmt;
18452 }
18453 /* Parse the function-body. */
18454 cp_parser_function_body (parser, in_function_try_block);
18455 if (check_body_p)
18456 check_constexpr_ctor_body (last, list);
18457 /* Finish the function body. */
18458 finish_function_body (body);
18459
18460 return ctor_initializer_p;
18461 }
18462
18463 /* Parse an initializer.
18464
18465 initializer:
18466 = initializer-clause
18467 ( expression-list )
18468
18469 Returns an expression representing the initializer. If no
18470 initializer is present, NULL_TREE is returned.
18471
18472 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18473 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18474 set to TRUE if there is no initializer present. If there is an
18475 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18476 is set to true; otherwise it is set to false. */
18477
18478 static tree
18479 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18480 bool* non_constant_p)
18481 {
18482 cp_token *token;
18483 tree init;
18484
18485 /* Peek at the next token. */
18486 token = cp_lexer_peek_token (parser->lexer);
18487
18488 /* Let our caller know whether or not this initializer was
18489 parenthesized. */
18490 *is_direct_init = (token->type != CPP_EQ);
18491 /* Assume that the initializer is constant. */
18492 *non_constant_p = false;
18493
18494 if (token->type == CPP_EQ)
18495 {
18496 /* Consume the `='. */
18497 cp_lexer_consume_token (parser->lexer);
18498 /* Parse the initializer-clause. */
18499 init = cp_parser_initializer_clause (parser, non_constant_p);
18500 }
18501 else if (token->type == CPP_OPEN_PAREN)
18502 {
18503 vec<tree, va_gc> *vec;
18504 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18505 /*cast_p=*/false,
18506 /*allow_expansion_p=*/true,
18507 non_constant_p);
18508 if (vec == NULL)
18509 return error_mark_node;
18510 init = build_tree_list_vec (vec);
18511 release_tree_vector (vec);
18512 }
18513 else if (token->type == CPP_OPEN_BRACE)
18514 {
18515 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18516 init = cp_parser_braced_list (parser, non_constant_p);
18517 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18518 }
18519 else
18520 {
18521 /* Anything else is an error. */
18522 cp_parser_error (parser, "expected initializer");
18523 init = error_mark_node;
18524 }
18525
18526 return init;
18527 }
18528
18529 /* Parse an initializer-clause.
18530
18531 initializer-clause:
18532 assignment-expression
18533 braced-init-list
18534
18535 Returns an expression representing the initializer.
18536
18537 If the `assignment-expression' production is used the value
18538 returned is simply a representation for the expression.
18539
18540 Otherwise, calls cp_parser_braced_list. */
18541
18542 static tree
18543 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18544 {
18545 tree initializer;
18546
18547 /* Assume the expression is constant. */
18548 *non_constant_p = false;
18549
18550 /* If it is not a `{', then we are looking at an
18551 assignment-expression. */
18552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18553 {
18554 initializer
18555 = cp_parser_constant_expression (parser,
18556 /*allow_non_constant_p=*/true,
18557 non_constant_p);
18558 }
18559 else
18560 initializer = cp_parser_braced_list (parser, non_constant_p);
18561
18562 return initializer;
18563 }
18564
18565 /* Parse a brace-enclosed initializer list.
18566
18567 braced-init-list:
18568 { initializer-list , [opt] }
18569 { }
18570
18571 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18572 the elements of the initializer-list (or NULL, if the last
18573 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18574 NULL_TREE. There is no way to detect whether or not the optional
18575 trailing `,' was provided. NON_CONSTANT_P is as for
18576 cp_parser_initializer. */
18577
18578 static tree
18579 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18580 {
18581 tree initializer;
18582
18583 /* Consume the `{' token. */
18584 cp_lexer_consume_token (parser->lexer);
18585 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18586 initializer = make_node (CONSTRUCTOR);
18587 /* If it's not a `}', then there is a non-trivial initializer. */
18588 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18589 {
18590 /* Parse the initializer list. */
18591 CONSTRUCTOR_ELTS (initializer)
18592 = cp_parser_initializer_list (parser, non_constant_p);
18593 /* A trailing `,' token is allowed. */
18594 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18595 cp_lexer_consume_token (parser->lexer);
18596 }
18597 else
18598 *non_constant_p = false;
18599 /* Now, there should be a trailing `}'. */
18600 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18601 TREE_TYPE (initializer) = init_list_type_node;
18602 return initializer;
18603 }
18604
18605 /* Parse an initializer-list.
18606
18607 initializer-list:
18608 initializer-clause ... [opt]
18609 initializer-list , initializer-clause ... [opt]
18610
18611 GNU Extension:
18612
18613 initializer-list:
18614 designation initializer-clause ...[opt]
18615 initializer-list , designation initializer-clause ...[opt]
18616
18617 designation:
18618 . identifier =
18619 identifier :
18620 [ constant-expression ] =
18621
18622 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18623 for the initializer. If the INDEX of the elt is non-NULL, it is the
18624 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18625 as for cp_parser_initializer. */
18626
18627 static vec<constructor_elt, va_gc> *
18628 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18629 {
18630 vec<constructor_elt, va_gc> *v = NULL;
18631
18632 /* Assume all of the expressions are constant. */
18633 *non_constant_p = false;
18634
18635 /* Parse the rest of the list. */
18636 while (true)
18637 {
18638 cp_token *token;
18639 tree designator;
18640 tree initializer;
18641 bool clause_non_constant_p;
18642
18643 /* If the next token is an identifier and the following one is a
18644 colon, we are looking at the GNU designated-initializer
18645 syntax. */
18646 if (cp_parser_allow_gnu_extensions_p (parser)
18647 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18648 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18649 {
18650 /* Warn the user that they are using an extension. */
18651 pedwarn (input_location, OPT_Wpedantic,
18652 "ISO C++ does not allow designated initializers");
18653 /* Consume the identifier. */
18654 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18655 /* Consume the `:'. */
18656 cp_lexer_consume_token (parser->lexer);
18657 }
18658 /* Also handle the C99 syntax, '. id ='. */
18659 else if (cp_parser_allow_gnu_extensions_p (parser)
18660 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18661 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18662 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18663 {
18664 /* Warn the user that they are using an extension. */
18665 pedwarn (input_location, OPT_Wpedantic,
18666 "ISO C++ does not allow C99 designated initializers");
18667 /* Consume the `.'. */
18668 cp_lexer_consume_token (parser->lexer);
18669 /* Consume the identifier. */
18670 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18671 /* Consume the `='. */
18672 cp_lexer_consume_token (parser->lexer);
18673 }
18674 /* Also handle C99 array designators, '[ const ] ='. */
18675 else if (cp_parser_allow_gnu_extensions_p (parser)
18676 && !c_dialect_objc ()
18677 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18678 {
18679 /* In C++11, [ could start a lambda-introducer. */
18680 bool non_const = false;
18681
18682 cp_parser_parse_tentatively (parser);
18683 cp_lexer_consume_token (parser->lexer);
18684 designator = cp_parser_constant_expression (parser, true, &non_const);
18685 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18686 cp_parser_require (parser, CPP_EQ, RT_EQ);
18687 if (!cp_parser_parse_definitely (parser))
18688 designator = NULL_TREE;
18689 else if (non_const)
18690 require_potential_rvalue_constant_expression (designator);
18691 }
18692 else
18693 designator = NULL_TREE;
18694
18695 /* Parse the initializer. */
18696 initializer = cp_parser_initializer_clause (parser,
18697 &clause_non_constant_p);
18698 /* If any clause is non-constant, so is the entire initializer. */
18699 if (clause_non_constant_p)
18700 *non_constant_p = true;
18701
18702 /* If we have an ellipsis, this is an initializer pack
18703 expansion. */
18704 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18705 {
18706 /* Consume the `...'. */
18707 cp_lexer_consume_token (parser->lexer);
18708
18709 /* Turn the initializer into an initializer expansion. */
18710 initializer = make_pack_expansion (initializer);
18711 }
18712
18713 /* Add it to the vector. */
18714 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18715
18716 /* If the next token is not a comma, we have reached the end of
18717 the list. */
18718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18719 break;
18720
18721 /* Peek at the next token. */
18722 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18723 /* If the next token is a `}', then we're still done. An
18724 initializer-clause can have a trailing `,' after the
18725 initializer-list and before the closing `}'. */
18726 if (token->type == CPP_CLOSE_BRACE)
18727 break;
18728
18729 /* Consume the `,' token. */
18730 cp_lexer_consume_token (parser->lexer);
18731 }
18732
18733 return v;
18734 }
18735
18736 /* Classes [gram.class] */
18737
18738 /* Parse a class-name.
18739
18740 class-name:
18741 identifier
18742 template-id
18743
18744 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18745 to indicate that names looked up in dependent types should be
18746 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18747 keyword has been used to indicate that the name that appears next
18748 is a template. TAG_TYPE indicates the explicit tag given before
18749 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18750 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18751 is the class being defined in a class-head.
18752
18753 Returns the TYPE_DECL representing the class. */
18754
18755 static tree
18756 cp_parser_class_name (cp_parser *parser,
18757 bool typename_keyword_p,
18758 bool template_keyword_p,
18759 enum tag_types tag_type,
18760 bool check_dependency_p,
18761 bool class_head_p,
18762 bool is_declaration)
18763 {
18764 tree decl;
18765 tree scope;
18766 bool typename_p;
18767 cp_token *token;
18768 tree identifier = NULL_TREE;
18769
18770 /* All class-names start with an identifier. */
18771 token = cp_lexer_peek_token (parser->lexer);
18772 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18773 {
18774 cp_parser_error (parser, "expected class-name");
18775 return error_mark_node;
18776 }
18777
18778 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18779 to a template-id, so we save it here. */
18780 scope = parser->scope;
18781 if (scope == error_mark_node)
18782 return error_mark_node;
18783
18784 /* Any name names a type if we're following the `typename' keyword
18785 in a qualified name where the enclosing scope is type-dependent. */
18786 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18787 && dependent_type_p (scope));
18788 /* Handle the common case (an identifier, but not a template-id)
18789 efficiently. */
18790 if (token->type == CPP_NAME
18791 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18792 {
18793 cp_token *identifier_token;
18794 bool ambiguous_p;
18795
18796 /* Look for the identifier. */
18797 identifier_token = cp_lexer_peek_token (parser->lexer);
18798 ambiguous_p = identifier_token->ambiguous_p;
18799 identifier = cp_parser_identifier (parser);
18800 /* If the next token isn't an identifier, we are certainly not
18801 looking at a class-name. */
18802 if (identifier == error_mark_node)
18803 decl = error_mark_node;
18804 /* If we know this is a type-name, there's no need to look it
18805 up. */
18806 else if (typename_p)
18807 decl = identifier;
18808 else
18809 {
18810 tree ambiguous_decls;
18811 /* If we already know that this lookup is ambiguous, then
18812 we've already issued an error message; there's no reason
18813 to check again. */
18814 if (ambiguous_p)
18815 {
18816 cp_parser_simulate_error (parser);
18817 return error_mark_node;
18818 }
18819 /* If the next token is a `::', then the name must be a type
18820 name.
18821
18822 [basic.lookup.qual]
18823
18824 During the lookup for a name preceding the :: scope
18825 resolution operator, object, function, and enumerator
18826 names are ignored. */
18827 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18828 tag_type = typename_type;
18829 /* Look up the name. */
18830 decl = cp_parser_lookup_name (parser, identifier,
18831 tag_type,
18832 /*is_template=*/false,
18833 /*is_namespace=*/false,
18834 check_dependency_p,
18835 &ambiguous_decls,
18836 identifier_token->location);
18837 if (ambiguous_decls)
18838 {
18839 if (cp_parser_parsing_tentatively (parser))
18840 cp_parser_simulate_error (parser);
18841 return error_mark_node;
18842 }
18843 }
18844 }
18845 else
18846 {
18847 /* Try a template-id. */
18848 decl = cp_parser_template_id (parser, template_keyword_p,
18849 check_dependency_p,
18850 tag_type,
18851 is_declaration);
18852 if (decl == error_mark_node)
18853 return error_mark_node;
18854 }
18855
18856 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18857
18858 /* If this is a typename, create a TYPENAME_TYPE. */
18859 if (typename_p && decl != error_mark_node)
18860 {
18861 decl = make_typename_type (scope, decl, typename_type,
18862 /*complain=*/tf_error);
18863 if (decl != error_mark_node)
18864 decl = TYPE_NAME (decl);
18865 }
18866
18867 decl = strip_using_decl (decl);
18868
18869 /* Check to see that it is really the name of a class. */
18870 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18871 && identifier_p (TREE_OPERAND (decl, 0))
18872 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18873 /* Situations like this:
18874
18875 template <typename T> struct A {
18876 typename T::template X<int>::I i;
18877 };
18878
18879 are problematic. Is `T::template X<int>' a class-name? The
18880 standard does not seem to be definitive, but there is no other
18881 valid interpretation of the following `::'. Therefore, those
18882 names are considered class-names. */
18883 {
18884 decl = make_typename_type (scope, decl, tag_type, tf_error);
18885 if (decl != error_mark_node)
18886 decl = TYPE_NAME (decl);
18887 }
18888 else if (TREE_CODE (decl) != TYPE_DECL
18889 || TREE_TYPE (decl) == error_mark_node
18890 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18891 /* In Objective-C 2.0, a classname followed by '.' starts a
18892 dot-syntax expression, and it's not a type-name. */
18893 || (c_dialect_objc ()
18894 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18895 && objc_is_class_name (decl)))
18896 decl = error_mark_node;
18897
18898 if (decl == error_mark_node)
18899 cp_parser_error (parser, "expected class-name");
18900 else if (identifier && !parser->scope)
18901 maybe_note_name_used_in_class (identifier, decl);
18902
18903 return decl;
18904 }
18905
18906 /* Parse a class-specifier.
18907
18908 class-specifier:
18909 class-head { member-specification [opt] }
18910
18911 Returns the TREE_TYPE representing the class. */
18912
18913 static tree
18914 cp_parser_class_specifier_1 (cp_parser* parser)
18915 {
18916 tree type;
18917 tree attributes = NULL_TREE;
18918 bool nested_name_specifier_p;
18919 unsigned saved_num_template_parameter_lists;
18920 bool saved_in_function_body;
18921 unsigned char in_statement;
18922 bool in_switch_statement_p;
18923 bool saved_in_unbraced_linkage_specification_p;
18924 tree old_scope = NULL_TREE;
18925 tree scope = NULL_TREE;
18926 cp_token *closing_brace;
18927
18928 push_deferring_access_checks (dk_no_deferred);
18929
18930 /* Parse the class-head. */
18931 type = cp_parser_class_head (parser,
18932 &nested_name_specifier_p);
18933 /* If the class-head was a semantic disaster, skip the entire body
18934 of the class. */
18935 if (!type)
18936 {
18937 cp_parser_skip_to_end_of_block_or_statement (parser);
18938 pop_deferring_access_checks ();
18939 return error_mark_node;
18940 }
18941
18942 /* Look for the `{'. */
18943 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18944 {
18945 pop_deferring_access_checks ();
18946 return error_mark_node;
18947 }
18948
18949 cp_ensure_no_omp_declare_simd (parser);
18950
18951 /* Issue an error message if type-definitions are forbidden here. */
18952 cp_parser_check_type_definition (parser);
18953 /* Remember that we are defining one more class. */
18954 ++parser->num_classes_being_defined;
18955 /* Inside the class, surrounding template-parameter-lists do not
18956 apply. */
18957 saved_num_template_parameter_lists
18958 = parser->num_template_parameter_lists;
18959 parser->num_template_parameter_lists = 0;
18960 /* We are not in a function body. */
18961 saved_in_function_body = parser->in_function_body;
18962 parser->in_function_body = false;
18963 /* Or in a loop. */
18964 in_statement = parser->in_statement;
18965 parser->in_statement = 0;
18966 /* Or in a switch. */
18967 in_switch_statement_p = parser->in_switch_statement_p;
18968 parser->in_switch_statement_p = false;
18969 /* We are not immediately inside an extern "lang" block. */
18970 saved_in_unbraced_linkage_specification_p
18971 = parser->in_unbraced_linkage_specification_p;
18972 parser->in_unbraced_linkage_specification_p = false;
18973
18974 /* Start the class. */
18975 if (nested_name_specifier_p)
18976 {
18977 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18978 old_scope = push_inner_scope (scope);
18979 }
18980 type = begin_class_definition (type);
18981
18982 if (type == error_mark_node)
18983 /* If the type is erroneous, skip the entire body of the class. */
18984 cp_parser_skip_to_closing_brace (parser);
18985 else
18986 /* Parse the member-specification. */
18987 cp_parser_member_specification_opt (parser);
18988
18989 /* Look for the trailing `}'. */
18990 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18991 /* Look for trailing attributes to apply to this class. */
18992 if (cp_parser_allow_gnu_extensions_p (parser))
18993 attributes = cp_parser_gnu_attributes_opt (parser);
18994 if (type != error_mark_node)
18995 type = finish_struct (type, attributes);
18996 if (nested_name_specifier_p)
18997 pop_inner_scope (old_scope, scope);
18998
18999 /* We've finished a type definition. Check for the common syntax
19000 error of forgetting a semicolon after the definition. We need to
19001 be careful, as we can't just check for not-a-semicolon and be done
19002 with it; the user might have typed:
19003
19004 class X { } c = ...;
19005 class X { } *p = ...;
19006
19007 and so forth. Instead, enumerate all the possible tokens that
19008 might follow this production; if we don't see one of them, then
19009 complain and silently insert the semicolon. */
19010 {
19011 cp_token *token = cp_lexer_peek_token (parser->lexer);
19012 bool want_semicolon = true;
19013
19014 if (cp_next_tokens_can_be_std_attribute_p (parser))
19015 /* Don't try to parse c++11 attributes here. As per the
19016 grammar, that should be a task for
19017 cp_parser_decl_specifier_seq. */
19018 want_semicolon = false;
19019
19020 switch (token->type)
19021 {
19022 case CPP_NAME:
19023 case CPP_SEMICOLON:
19024 case CPP_MULT:
19025 case CPP_AND:
19026 case CPP_OPEN_PAREN:
19027 case CPP_CLOSE_PAREN:
19028 case CPP_COMMA:
19029 want_semicolon = false;
19030 break;
19031
19032 /* While it's legal for type qualifiers and storage class
19033 specifiers to follow type definitions in the grammar, only
19034 compiler testsuites contain code like that. Assume that if
19035 we see such code, then what we're really seeing is a case
19036 like:
19037
19038 class X { }
19039 const <type> var = ...;
19040
19041 or
19042
19043 class Y { }
19044 static <type> func (...) ...
19045
19046 i.e. the qualifier or specifier applies to the next
19047 declaration. To do so, however, we need to look ahead one
19048 more token to see if *that* token is a type specifier.
19049
19050 This code could be improved to handle:
19051
19052 class Z { }
19053 static const <type> var = ...; */
19054 case CPP_KEYWORD:
19055 if (keyword_is_decl_specifier (token->keyword))
19056 {
19057 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19058
19059 /* Handling user-defined types here would be nice, but very
19060 tricky. */
19061 want_semicolon
19062 = (lookahead->type == CPP_KEYWORD
19063 && keyword_begins_type_specifier (lookahead->keyword));
19064 }
19065 break;
19066 default:
19067 break;
19068 }
19069
19070 /* If we don't have a type, then something is very wrong and we
19071 shouldn't try to do anything clever. Likewise for not seeing the
19072 closing brace. */
19073 if (closing_brace && TYPE_P (type) && want_semicolon)
19074 {
19075 cp_token_position prev
19076 = cp_lexer_previous_token_position (parser->lexer);
19077 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19078 location_t loc = prev_token->location;
19079
19080 if (CLASSTYPE_DECLARED_CLASS (type))
19081 error_at (loc, "expected %<;%> after class definition");
19082 else if (TREE_CODE (type) == RECORD_TYPE)
19083 error_at (loc, "expected %<;%> after struct definition");
19084 else if (TREE_CODE (type) == UNION_TYPE)
19085 error_at (loc, "expected %<;%> after union definition");
19086 else
19087 gcc_unreachable ();
19088
19089 /* Unget one token and smash it to look as though we encountered
19090 a semicolon in the input stream. */
19091 cp_lexer_set_token_position (parser->lexer, prev);
19092 token = cp_lexer_peek_token (parser->lexer);
19093 token->type = CPP_SEMICOLON;
19094 token->keyword = RID_MAX;
19095 }
19096 }
19097
19098 /* If this class is not itself within the scope of another class,
19099 then we need to parse the bodies of all of the queued function
19100 definitions. Note that the queued functions defined in a class
19101 are not always processed immediately following the
19102 class-specifier for that class. Consider:
19103
19104 struct A {
19105 struct B { void f() { sizeof (A); } };
19106 };
19107
19108 If `f' were processed before the processing of `A' were
19109 completed, there would be no way to compute the size of `A'.
19110 Note that the nesting we are interested in here is lexical --
19111 not the semantic nesting given by TYPE_CONTEXT. In particular,
19112 for:
19113
19114 struct A { struct B; };
19115 struct A::B { void f() { } };
19116
19117 there is no need to delay the parsing of `A::B::f'. */
19118 if (--parser->num_classes_being_defined == 0)
19119 {
19120 tree decl;
19121 tree class_type = NULL_TREE;
19122 tree pushed_scope = NULL_TREE;
19123 unsigned ix;
19124 cp_default_arg_entry *e;
19125 tree save_ccp, save_ccr;
19126
19127 /* In a first pass, parse default arguments to the functions.
19128 Then, in a second pass, parse the bodies of the functions.
19129 This two-phased approach handles cases like:
19130
19131 struct S {
19132 void f() { g(); }
19133 void g(int i = 3);
19134 };
19135
19136 */
19137 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19138 {
19139 decl = e->decl;
19140 /* If there are default arguments that have not yet been processed,
19141 take care of them now. */
19142 if (class_type != e->class_type)
19143 {
19144 if (pushed_scope)
19145 pop_scope (pushed_scope);
19146 class_type = e->class_type;
19147 pushed_scope = push_scope (class_type);
19148 }
19149 /* Make sure that any template parameters are in scope. */
19150 maybe_begin_member_template_processing (decl);
19151 /* Parse the default argument expressions. */
19152 cp_parser_late_parsing_default_args (parser, decl);
19153 /* Remove any template parameters from the symbol table. */
19154 maybe_end_member_template_processing ();
19155 }
19156 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19157 /* Now parse any NSDMIs. */
19158 save_ccp = current_class_ptr;
19159 save_ccr = current_class_ref;
19160 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19161 {
19162 if (class_type != DECL_CONTEXT (decl))
19163 {
19164 if (pushed_scope)
19165 pop_scope (pushed_scope);
19166 class_type = DECL_CONTEXT (decl);
19167 pushed_scope = push_scope (class_type);
19168 }
19169 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19170 cp_parser_late_parsing_nsdmi (parser, decl);
19171 }
19172 vec_safe_truncate (unparsed_nsdmis, 0);
19173 current_class_ptr = save_ccp;
19174 current_class_ref = save_ccr;
19175 if (pushed_scope)
19176 pop_scope (pushed_scope);
19177 /* Now parse the body of the functions. */
19178 if (flag_openmp)
19179 {
19180 /* OpenMP UDRs need to be parsed before all other functions. */
19181 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19182 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19183 cp_parser_late_parsing_for_member (parser, decl);
19184 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19185 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19186 cp_parser_late_parsing_for_member (parser, decl);
19187 }
19188 else
19189 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19190 cp_parser_late_parsing_for_member (parser, decl);
19191 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19192 }
19193
19194 /* Put back any saved access checks. */
19195 pop_deferring_access_checks ();
19196
19197 /* Restore saved state. */
19198 parser->in_switch_statement_p = in_switch_statement_p;
19199 parser->in_statement = in_statement;
19200 parser->in_function_body = saved_in_function_body;
19201 parser->num_template_parameter_lists
19202 = saved_num_template_parameter_lists;
19203 parser->in_unbraced_linkage_specification_p
19204 = saved_in_unbraced_linkage_specification_p;
19205
19206 return type;
19207 }
19208
19209 static tree
19210 cp_parser_class_specifier (cp_parser* parser)
19211 {
19212 tree ret;
19213 timevar_push (TV_PARSE_STRUCT);
19214 ret = cp_parser_class_specifier_1 (parser);
19215 timevar_pop (TV_PARSE_STRUCT);
19216 return ret;
19217 }
19218
19219 /* Parse a class-head.
19220
19221 class-head:
19222 class-key identifier [opt] base-clause [opt]
19223 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19224 class-key nested-name-specifier [opt] template-id
19225 base-clause [opt]
19226
19227 class-virt-specifier:
19228 final
19229
19230 GNU Extensions:
19231 class-key attributes identifier [opt] base-clause [opt]
19232 class-key attributes nested-name-specifier identifier base-clause [opt]
19233 class-key attributes nested-name-specifier [opt] template-id
19234 base-clause [opt]
19235
19236 Upon return BASES is initialized to the list of base classes (or
19237 NULL, if there are none) in the same form returned by
19238 cp_parser_base_clause.
19239
19240 Returns the TYPE of the indicated class. Sets
19241 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19242 involving a nested-name-specifier was used, and FALSE otherwise.
19243
19244 Returns error_mark_node if this is not a class-head.
19245
19246 Returns NULL_TREE if the class-head is syntactically valid, but
19247 semantically invalid in a way that means we should skip the entire
19248 body of the class. */
19249
19250 static tree
19251 cp_parser_class_head (cp_parser* parser,
19252 bool* nested_name_specifier_p)
19253 {
19254 tree nested_name_specifier;
19255 enum tag_types class_key;
19256 tree id = NULL_TREE;
19257 tree type = NULL_TREE;
19258 tree attributes;
19259 tree bases;
19260 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19261 bool template_id_p = false;
19262 bool qualified_p = false;
19263 bool invalid_nested_name_p = false;
19264 bool invalid_explicit_specialization_p = false;
19265 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19266 tree pushed_scope = NULL_TREE;
19267 unsigned num_templates;
19268 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19269 /* Assume no nested-name-specifier will be present. */
19270 *nested_name_specifier_p = false;
19271 /* Assume no template parameter lists will be used in defining the
19272 type. */
19273 num_templates = 0;
19274 parser->colon_corrects_to_scope_p = false;
19275
19276 /* Look for the class-key. */
19277 class_key = cp_parser_class_key (parser);
19278 if (class_key == none_type)
19279 return error_mark_node;
19280
19281 /* Parse the attributes. */
19282 attributes = cp_parser_attributes_opt (parser);
19283
19284 /* If the next token is `::', that is invalid -- but sometimes
19285 people do try to write:
19286
19287 struct ::S {};
19288
19289 Handle this gracefully by accepting the extra qualifier, and then
19290 issuing an error about it later if this really is a
19291 class-head. If it turns out just to be an elaborated type
19292 specifier, remain silent. */
19293 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19294 qualified_p = true;
19295
19296 push_deferring_access_checks (dk_no_check);
19297
19298 /* Determine the name of the class. Begin by looking for an
19299 optional nested-name-specifier. */
19300 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19301 nested_name_specifier
19302 = cp_parser_nested_name_specifier_opt (parser,
19303 /*typename_keyword_p=*/false,
19304 /*check_dependency_p=*/false,
19305 /*type_p=*/true,
19306 /*is_declaration=*/false);
19307 /* If there was a nested-name-specifier, then there *must* be an
19308 identifier. */
19309 if (nested_name_specifier)
19310 {
19311 type_start_token = cp_lexer_peek_token (parser->lexer);
19312 /* Although the grammar says `identifier', it really means
19313 `class-name' or `template-name'. You are only allowed to
19314 define a class that has already been declared with this
19315 syntax.
19316
19317 The proposed resolution for Core Issue 180 says that wherever
19318 you see `class T::X' you should treat `X' as a type-name.
19319
19320 It is OK to define an inaccessible class; for example:
19321
19322 class A { class B; };
19323 class A::B {};
19324
19325 We do not know if we will see a class-name, or a
19326 template-name. We look for a class-name first, in case the
19327 class-name is a template-id; if we looked for the
19328 template-name first we would stop after the template-name. */
19329 cp_parser_parse_tentatively (parser);
19330 type = cp_parser_class_name (parser,
19331 /*typename_keyword_p=*/false,
19332 /*template_keyword_p=*/false,
19333 class_type,
19334 /*check_dependency_p=*/false,
19335 /*class_head_p=*/true,
19336 /*is_declaration=*/false);
19337 /* If that didn't work, ignore the nested-name-specifier. */
19338 if (!cp_parser_parse_definitely (parser))
19339 {
19340 invalid_nested_name_p = true;
19341 type_start_token = cp_lexer_peek_token (parser->lexer);
19342 id = cp_parser_identifier (parser);
19343 if (id == error_mark_node)
19344 id = NULL_TREE;
19345 }
19346 /* If we could not find a corresponding TYPE, treat this
19347 declaration like an unqualified declaration. */
19348 if (type == error_mark_node)
19349 nested_name_specifier = NULL_TREE;
19350 /* Otherwise, count the number of templates used in TYPE and its
19351 containing scopes. */
19352 else
19353 {
19354 tree scope;
19355
19356 for (scope = TREE_TYPE (type);
19357 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19358 scope = get_containing_scope (scope))
19359 if (TYPE_P (scope)
19360 && CLASS_TYPE_P (scope)
19361 && CLASSTYPE_TEMPLATE_INFO (scope)
19362 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19363 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19364 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19365 ++num_templates;
19366 }
19367 }
19368 /* Otherwise, the identifier is optional. */
19369 else
19370 {
19371 /* We don't know whether what comes next is a template-id,
19372 an identifier, or nothing at all. */
19373 cp_parser_parse_tentatively (parser);
19374 /* Check for a template-id. */
19375 type_start_token = cp_lexer_peek_token (parser->lexer);
19376 id = cp_parser_template_id (parser,
19377 /*template_keyword_p=*/false,
19378 /*check_dependency_p=*/true,
19379 class_key,
19380 /*is_declaration=*/true);
19381 /* If that didn't work, it could still be an identifier. */
19382 if (!cp_parser_parse_definitely (parser))
19383 {
19384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19385 {
19386 type_start_token = cp_lexer_peek_token (parser->lexer);
19387 id = cp_parser_identifier (parser);
19388 }
19389 else
19390 id = NULL_TREE;
19391 }
19392 else
19393 {
19394 template_id_p = true;
19395 ++num_templates;
19396 }
19397 }
19398
19399 pop_deferring_access_checks ();
19400
19401 if (id)
19402 {
19403 cp_parser_check_for_invalid_template_id (parser, id,
19404 class_key,
19405 type_start_token->location);
19406 }
19407 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19408
19409 /* If it's not a `:' or a `{' then we can't really be looking at a
19410 class-head, since a class-head only appears as part of a
19411 class-specifier. We have to detect this situation before calling
19412 xref_tag, since that has irreversible side-effects. */
19413 if (!cp_parser_next_token_starts_class_definition_p (parser))
19414 {
19415 cp_parser_error (parser, "expected %<{%> or %<:%>");
19416 type = error_mark_node;
19417 goto out;
19418 }
19419
19420 /* At this point, we're going ahead with the class-specifier, even
19421 if some other problem occurs. */
19422 cp_parser_commit_to_tentative_parse (parser);
19423 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19424 {
19425 cp_parser_error (parser,
19426 "cannot specify %<override%> for a class");
19427 type = error_mark_node;
19428 goto out;
19429 }
19430 /* Issue the error about the overly-qualified name now. */
19431 if (qualified_p)
19432 {
19433 cp_parser_error (parser,
19434 "global qualification of class name is invalid");
19435 type = error_mark_node;
19436 goto out;
19437 }
19438 else if (invalid_nested_name_p)
19439 {
19440 cp_parser_error (parser,
19441 "qualified name does not name a class");
19442 type = error_mark_node;
19443 goto out;
19444 }
19445 else if (nested_name_specifier)
19446 {
19447 tree scope;
19448
19449 /* Reject typedef-names in class heads. */
19450 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19451 {
19452 error_at (type_start_token->location,
19453 "invalid class name in declaration of %qD",
19454 type);
19455 type = NULL_TREE;
19456 goto done;
19457 }
19458
19459 /* Figure out in what scope the declaration is being placed. */
19460 scope = current_scope ();
19461 /* If that scope does not contain the scope in which the
19462 class was originally declared, the program is invalid. */
19463 if (scope && !is_ancestor (scope, nested_name_specifier))
19464 {
19465 if (at_namespace_scope_p ())
19466 error_at (type_start_token->location,
19467 "declaration of %qD in namespace %qD which does not "
19468 "enclose %qD",
19469 type, scope, nested_name_specifier);
19470 else
19471 error_at (type_start_token->location,
19472 "declaration of %qD in %qD which does not enclose %qD",
19473 type, scope, nested_name_specifier);
19474 type = NULL_TREE;
19475 goto done;
19476 }
19477 /* [dcl.meaning]
19478
19479 A declarator-id shall not be qualified except for the
19480 definition of a ... nested class outside of its class
19481 ... [or] the definition or explicit instantiation of a
19482 class member of a namespace outside of its namespace. */
19483 if (scope == nested_name_specifier)
19484 {
19485 permerror (nested_name_specifier_token_start->location,
19486 "extra qualification not allowed");
19487 nested_name_specifier = NULL_TREE;
19488 num_templates = 0;
19489 }
19490 }
19491 /* An explicit-specialization must be preceded by "template <>". If
19492 it is not, try to recover gracefully. */
19493 if (at_namespace_scope_p ()
19494 && parser->num_template_parameter_lists == 0
19495 && template_id_p)
19496 {
19497 error_at (type_start_token->location,
19498 "an explicit specialization must be preceded by %<template <>%>");
19499 invalid_explicit_specialization_p = true;
19500 /* Take the same action that would have been taken by
19501 cp_parser_explicit_specialization. */
19502 ++parser->num_template_parameter_lists;
19503 begin_specialization ();
19504 }
19505 /* There must be no "return" statements between this point and the
19506 end of this function; set "type "to the correct return value and
19507 use "goto done;" to return. */
19508 /* Make sure that the right number of template parameters were
19509 present. */
19510 if (!cp_parser_check_template_parameters (parser, num_templates,
19511 type_start_token->location,
19512 /*declarator=*/NULL))
19513 {
19514 /* If something went wrong, there is no point in even trying to
19515 process the class-definition. */
19516 type = NULL_TREE;
19517 goto done;
19518 }
19519
19520 /* Look up the type. */
19521 if (template_id_p)
19522 {
19523 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19524 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19525 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19526 {
19527 error_at (type_start_token->location,
19528 "function template %qD redeclared as a class template", id);
19529 type = error_mark_node;
19530 }
19531 else
19532 {
19533 type = TREE_TYPE (id);
19534 type = maybe_process_partial_specialization (type);
19535 }
19536 if (nested_name_specifier)
19537 pushed_scope = push_scope (nested_name_specifier);
19538 }
19539 else if (nested_name_specifier)
19540 {
19541 tree class_type;
19542
19543 /* Given:
19544
19545 template <typename T> struct S { struct T };
19546 template <typename T> struct S<T>::T { };
19547
19548 we will get a TYPENAME_TYPE when processing the definition of
19549 `S::T'. We need to resolve it to the actual type before we
19550 try to define it. */
19551 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19552 {
19553 class_type = resolve_typename_type (TREE_TYPE (type),
19554 /*only_current_p=*/false);
19555 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19556 type = TYPE_NAME (class_type);
19557 else
19558 {
19559 cp_parser_error (parser, "could not resolve typename type");
19560 type = error_mark_node;
19561 }
19562 }
19563
19564 if (maybe_process_partial_specialization (TREE_TYPE (type))
19565 == error_mark_node)
19566 {
19567 type = NULL_TREE;
19568 goto done;
19569 }
19570
19571 class_type = current_class_type;
19572 /* Enter the scope indicated by the nested-name-specifier. */
19573 pushed_scope = push_scope (nested_name_specifier);
19574 /* Get the canonical version of this type. */
19575 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19576 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19577 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19578 {
19579 type = push_template_decl (type);
19580 if (type == error_mark_node)
19581 {
19582 type = NULL_TREE;
19583 goto done;
19584 }
19585 }
19586
19587 type = TREE_TYPE (type);
19588 *nested_name_specifier_p = true;
19589 }
19590 else /* The name is not a nested name. */
19591 {
19592 /* If the class was unnamed, create a dummy name. */
19593 if (!id)
19594 id = make_anon_name ();
19595 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19596 parser->num_template_parameter_lists);
19597 }
19598
19599 /* Indicate whether this class was declared as a `class' or as a
19600 `struct'. */
19601 if (TREE_CODE (type) == RECORD_TYPE)
19602 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19603 cp_parser_check_class_key (class_key, type);
19604
19605 /* If this type was already complete, and we see another definition,
19606 that's an error. */
19607 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19608 {
19609 error_at (type_start_token->location, "redefinition of %q#T",
19610 type);
19611 error_at (type_start_token->location, "previous definition of %q+#T",
19612 type);
19613 type = NULL_TREE;
19614 goto done;
19615 }
19616 else if (type == error_mark_node)
19617 type = NULL_TREE;
19618
19619 if (type)
19620 {
19621 /* Apply attributes now, before any use of the class as a template
19622 argument in its base list. */
19623 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19624 fixup_attribute_variants (type);
19625 }
19626
19627 /* We will have entered the scope containing the class; the names of
19628 base classes should be looked up in that context. For example:
19629
19630 struct A { struct B {}; struct C; };
19631 struct A::C : B {};
19632
19633 is valid. */
19634
19635 /* Get the list of base-classes, if there is one. */
19636 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19637 bases = cp_parser_base_clause (parser);
19638 else
19639 bases = NULL_TREE;
19640
19641 /* If we're really defining a class, process the base classes.
19642 If they're invalid, fail. */
19643 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19644 && !xref_basetypes (type, bases))
19645 type = NULL_TREE;
19646
19647 done:
19648 /* Leave the scope given by the nested-name-specifier. We will
19649 enter the class scope itself while processing the members. */
19650 if (pushed_scope)
19651 pop_scope (pushed_scope);
19652
19653 if (invalid_explicit_specialization_p)
19654 {
19655 end_specialization ();
19656 --parser->num_template_parameter_lists;
19657 }
19658
19659 if (type)
19660 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19661 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19662 CLASSTYPE_FINAL (type) = 1;
19663 out:
19664 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19665 return type;
19666 }
19667
19668 /* Parse a class-key.
19669
19670 class-key:
19671 class
19672 struct
19673 union
19674
19675 Returns the kind of class-key specified, or none_type to indicate
19676 error. */
19677
19678 static enum tag_types
19679 cp_parser_class_key (cp_parser* parser)
19680 {
19681 cp_token *token;
19682 enum tag_types tag_type;
19683
19684 /* Look for the class-key. */
19685 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19686 if (!token)
19687 return none_type;
19688
19689 /* Check to see if the TOKEN is a class-key. */
19690 tag_type = cp_parser_token_is_class_key (token);
19691 if (!tag_type)
19692 cp_parser_error (parser, "expected class-key");
19693 return tag_type;
19694 }
19695
19696 /* Parse an (optional) member-specification.
19697
19698 member-specification:
19699 member-declaration member-specification [opt]
19700 access-specifier : member-specification [opt] */
19701
19702 static void
19703 cp_parser_member_specification_opt (cp_parser* parser)
19704 {
19705 while (true)
19706 {
19707 cp_token *token;
19708 enum rid keyword;
19709
19710 /* Peek at the next token. */
19711 token = cp_lexer_peek_token (parser->lexer);
19712 /* If it's a `}', or EOF then we've seen all the members. */
19713 if (token->type == CPP_CLOSE_BRACE
19714 || token->type == CPP_EOF
19715 || token->type == CPP_PRAGMA_EOL)
19716 break;
19717
19718 /* See if this token is a keyword. */
19719 keyword = token->keyword;
19720 switch (keyword)
19721 {
19722 case RID_PUBLIC:
19723 case RID_PROTECTED:
19724 case RID_PRIVATE:
19725 /* Consume the access-specifier. */
19726 cp_lexer_consume_token (parser->lexer);
19727 /* Remember which access-specifier is active. */
19728 current_access_specifier = token->u.value;
19729 /* Look for the `:'. */
19730 cp_parser_require (parser, CPP_COLON, RT_COLON);
19731 break;
19732
19733 default:
19734 /* Accept #pragmas at class scope. */
19735 if (token->type == CPP_PRAGMA)
19736 {
19737 cp_parser_pragma (parser, pragma_member);
19738 break;
19739 }
19740
19741 /* Otherwise, the next construction must be a
19742 member-declaration. */
19743 cp_parser_member_declaration (parser);
19744 }
19745 }
19746 }
19747
19748 /* Parse a member-declaration.
19749
19750 member-declaration:
19751 decl-specifier-seq [opt] member-declarator-list [opt] ;
19752 function-definition ; [opt]
19753 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19754 using-declaration
19755 template-declaration
19756 alias-declaration
19757
19758 member-declarator-list:
19759 member-declarator
19760 member-declarator-list , member-declarator
19761
19762 member-declarator:
19763 declarator pure-specifier [opt]
19764 declarator constant-initializer [opt]
19765 identifier [opt] : constant-expression
19766
19767 GNU Extensions:
19768
19769 member-declaration:
19770 __extension__ member-declaration
19771
19772 member-declarator:
19773 declarator attributes [opt] pure-specifier [opt]
19774 declarator attributes [opt] constant-initializer [opt]
19775 identifier [opt] attributes [opt] : constant-expression
19776
19777 C++0x Extensions:
19778
19779 member-declaration:
19780 static_assert-declaration */
19781
19782 static void
19783 cp_parser_member_declaration (cp_parser* parser)
19784 {
19785 cp_decl_specifier_seq decl_specifiers;
19786 tree prefix_attributes;
19787 tree decl;
19788 int declares_class_or_enum;
19789 bool friend_p;
19790 cp_token *token = NULL;
19791 cp_token *decl_spec_token_start = NULL;
19792 cp_token *initializer_token_start = NULL;
19793 int saved_pedantic;
19794 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19795
19796 /* Check for the `__extension__' keyword. */
19797 if (cp_parser_extension_opt (parser, &saved_pedantic))
19798 {
19799 /* Recurse. */
19800 cp_parser_member_declaration (parser);
19801 /* Restore the old value of the PEDANTIC flag. */
19802 pedantic = saved_pedantic;
19803
19804 return;
19805 }
19806
19807 /* Check for a template-declaration. */
19808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19809 {
19810 /* An explicit specialization here is an error condition, and we
19811 expect the specialization handler to detect and report this. */
19812 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19813 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19814 cp_parser_explicit_specialization (parser);
19815 else
19816 cp_parser_template_declaration (parser, /*member_p=*/true);
19817
19818 return;
19819 }
19820
19821 /* Check for a using-declaration. */
19822 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19823 {
19824 if (cxx_dialect < cxx11)
19825 {
19826 /* Parse the using-declaration. */
19827 cp_parser_using_declaration (parser,
19828 /*access_declaration_p=*/false);
19829 return;
19830 }
19831 else
19832 {
19833 tree decl;
19834 bool alias_decl_expected;
19835 cp_parser_parse_tentatively (parser);
19836 decl = cp_parser_alias_declaration (parser);
19837 /* Note that if we actually see the '=' token after the
19838 identifier, cp_parser_alias_declaration commits the
19839 tentative parse. In that case, we really expects an
19840 alias-declaration. Otherwise, we expect a using
19841 declaration. */
19842 alias_decl_expected =
19843 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19844 cp_parser_parse_definitely (parser);
19845
19846 if (alias_decl_expected)
19847 finish_member_declaration (decl);
19848 else
19849 cp_parser_using_declaration (parser,
19850 /*access_declaration_p=*/false);
19851 return;
19852 }
19853 }
19854
19855 /* Check for @defs. */
19856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19857 {
19858 tree ivar, member;
19859 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19860 ivar = ivar_chains;
19861 while (ivar)
19862 {
19863 member = ivar;
19864 ivar = TREE_CHAIN (member);
19865 TREE_CHAIN (member) = NULL_TREE;
19866 finish_member_declaration (member);
19867 }
19868 return;
19869 }
19870
19871 /* If the next token is `static_assert' we have a static assertion. */
19872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19873 {
19874 cp_parser_static_assert (parser, /*member_p=*/true);
19875 return;
19876 }
19877
19878 parser->colon_corrects_to_scope_p = false;
19879
19880 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19881 goto out;
19882
19883 /* Parse the decl-specifier-seq. */
19884 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19885 cp_parser_decl_specifier_seq (parser,
19886 CP_PARSER_FLAGS_OPTIONAL,
19887 &decl_specifiers,
19888 &declares_class_or_enum);
19889 /* Check for an invalid type-name. */
19890 if (!decl_specifiers.any_type_specifiers_p
19891 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19892 goto out;
19893 /* If there is no declarator, then the decl-specifier-seq should
19894 specify a type. */
19895 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19896 {
19897 /* If there was no decl-specifier-seq, and the next token is a
19898 `;', then we have something like:
19899
19900 struct S { ; };
19901
19902 [class.mem]
19903
19904 Each member-declaration shall declare at least one member
19905 name of the class. */
19906 if (!decl_specifiers.any_specifiers_p)
19907 {
19908 cp_token *token = cp_lexer_peek_token (parser->lexer);
19909 if (!in_system_header_at (token->location))
19910 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19911 }
19912 else
19913 {
19914 tree type;
19915
19916 /* See if this declaration is a friend. */
19917 friend_p = cp_parser_friend_p (&decl_specifiers);
19918 /* If there were decl-specifiers, check to see if there was
19919 a class-declaration. */
19920 type = check_tag_decl (&decl_specifiers,
19921 /*explicit_type_instantiation_p=*/false);
19922 /* Nested classes have already been added to the class, but
19923 a `friend' needs to be explicitly registered. */
19924 if (friend_p)
19925 {
19926 /* If the `friend' keyword was present, the friend must
19927 be introduced with a class-key. */
19928 if (!declares_class_or_enum && cxx_dialect < cxx11)
19929 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19930 "in C++03 a class-key must be used "
19931 "when declaring a friend");
19932 /* In this case:
19933
19934 template <typename T> struct A {
19935 friend struct A<T>::B;
19936 };
19937
19938 A<T>::B will be represented by a TYPENAME_TYPE, and
19939 therefore not recognized by check_tag_decl. */
19940 if (!type)
19941 {
19942 type = decl_specifiers.type;
19943 if (type && TREE_CODE (type) == TYPE_DECL)
19944 type = TREE_TYPE (type);
19945 }
19946 if (!type || !TYPE_P (type))
19947 error_at (decl_spec_token_start->location,
19948 "friend declaration does not name a class or "
19949 "function");
19950 else
19951 make_friend_class (current_class_type, type,
19952 /*complain=*/true);
19953 }
19954 /* If there is no TYPE, an error message will already have
19955 been issued. */
19956 else if (!type || type == error_mark_node)
19957 ;
19958 /* An anonymous aggregate has to be handled specially; such
19959 a declaration really declares a data member (with a
19960 particular type), as opposed to a nested class. */
19961 else if (ANON_AGGR_TYPE_P (type))
19962 {
19963 /* C++11 9.5/6. */
19964 if (decl_specifiers.storage_class != sc_none)
19965 error_at (decl_spec_token_start->location,
19966 "a storage class on an anonymous aggregate "
19967 "in class scope is not allowed");
19968
19969 /* Remove constructors and such from TYPE, now that we
19970 know it is an anonymous aggregate. */
19971 fixup_anonymous_aggr (type);
19972 /* And make the corresponding data member. */
19973 decl = build_decl (decl_spec_token_start->location,
19974 FIELD_DECL, NULL_TREE, type);
19975 /* Add it to the class. */
19976 finish_member_declaration (decl);
19977 }
19978 else
19979 cp_parser_check_access_in_redeclaration
19980 (TYPE_NAME (type),
19981 decl_spec_token_start->location);
19982 }
19983 }
19984 else
19985 {
19986 bool assume_semicolon = false;
19987
19988 /* Clear attributes from the decl_specifiers but keep them
19989 around as prefix attributes that apply them to the entity
19990 being declared. */
19991 prefix_attributes = decl_specifiers.attributes;
19992 decl_specifiers.attributes = NULL_TREE;
19993
19994 /* See if these declarations will be friends. */
19995 friend_p = cp_parser_friend_p (&decl_specifiers);
19996
19997 /* Keep going until we hit the `;' at the end of the
19998 declaration. */
19999 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20000 {
20001 tree attributes = NULL_TREE;
20002 tree first_attribute;
20003
20004 /* Peek at the next token. */
20005 token = cp_lexer_peek_token (parser->lexer);
20006
20007 /* Check for a bitfield declaration. */
20008 if (token->type == CPP_COLON
20009 || (token->type == CPP_NAME
20010 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20011 == CPP_COLON))
20012 {
20013 tree identifier;
20014 tree width;
20015
20016 /* Get the name of the bitfield. Note that we cannot just
20017 check TOKEN here because it may have been invalidated by
20018 the call to cp_lexer_peek_nth_token above. */
20019 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20020 identifier = cp_parser_identifier (parser);
20021 else
20022 identifier = NULL_TREE;
20023
20024 /* Consume the `:' token. */
20025 cp_lexer_consume_token (parser->lexer);
20026 /* Get the width of the bitfield. */
20027 width
20028 = cp_parser_constant_expression (parser,
20029 /*allow_non_constant=*/false,
20030 NULL);
20031
20032 /* Look for attributes that apply to the bitfield. */
20033 attributes = cp_parser_attributes_opt (parser);
20034 /* Remember which attributes are prefix attributes and
20035 which are not. */
20036 first_attribute = attributes;
20037 /* Combine the attributes. */
20038 attributes = chainon (prefix_attributes, attributes);
20039
20040 /* Create the bitfield declaration. */
20041 decl = grokbitfield (identifier
20042 ? make_id_declarator (NULL_TREE,
20043 identifier,
20044 sfk_none)
20045 : NULL,
20046 &decl_specifiers,
20047 width,
20048 attributes);
20049 }
20050 else
20051 {
20052 cp_declarator *declarator;
20053 tree initializer;
20054 tree asm_specification;
20055 int ctor_dtor_or_conv_p;
20056
20057 /* Parse the declarator. */
20058 declarator
20059 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20060 &ctor_dtor_or_conv_p,
20061 /*parenthesized_p=*/NULL,
20062 /*member_p=*/true);
20063
20064 /* If something went wrong parsing the declarator, make sure
20065 that we at least consume some tokens. */
20066 if (declarator == cp_error_declarator)
20067 {
20068 /* Skip to the end of the statement. */
20069 cp_parser_skip_to_end_of_statement (parser);
20070 /* If the next token is not a semicolon, that is
20071 probably because we just skipped over the body of
20072 a function. So, we consume a semicolon if
20073 present, but do not issue an error message if it
20074 is not present. */
20075 if (cp_lexer_next_token_is (parser->lexer,
20076 CPP_SEMICOLON))
20077 cp_lexer_consume_token (parser->lexer);
20078 goto out;
20079 }
20080
20081 if (declares_class_or_enum & 2)
20082 cp_parser_check_for_definition_in_return_type
20083 (declarator, decl_specifiers.type,
20084 decl_specifiers.locations[ds_type_spec]);
20085
20086 /* Look for an asm-specification. */
20087 asm_specification = cp_parser_asm_specification_opt (parser);
20088 /* Look for attributes that apply to the declaration. */
20089 attributes = cp_parser_attributes_opt (parser);
20090 /* Remember which attributes are prefix attributes and
20091 which are not. */
20092 first_attribute = attributes;
20093 /* Combine the attributes. */
20094 attributes = chainon (prefix_attributes, attributes);
20095
20096 /* If it's an `=', then we have a constant-initializer or a
20097 pure-specifier. It is not correct to parse the
20098 initializer before registering the member declaration
20099 since the member declaration should be in scope while
20100 its initializer is processed. However, the rest of the
20101 front end does not yet provide an interface that allows
20102 us to handle this correctly. */
20103 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20104 {
20105 /* In [class.mem]:
20106
20107 A pure-specifier shall be used only in the declaration of
20108 a virtual function.
20109
20110 A member-declarator can contain a constant-initializer
20111 only if it declares a static member of integral or
20112 enumeration type.
20113
20114 Therefore, if the DECLARATOR is for a function, we look
20115 for a pure-specifier; otherwise, we look for a
20116 constant-initializer. When we call `grokfield', it will
20117 perform more stringent semantics checks. */
20118 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20119 if (function_declarator_p (declarator)
20120 || (decl_specifiers.type
20121 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20122 && declarator->kind == cdk_id
20123 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20124 == FUNCTION_TYPE)))
20125 initializer = cp_parser_pure_specifier (parser);
20126 else if (decl_specifiers.storage_class != sc_static)
20127 initializer = cp_parser_save_nsdmi (parser);
20128 else if (cxx_dialect >= cxx11)
20129 {
20130 bool nonconst;
20131 /* Don't require a constant rvalue in C++11, since we
20132 might want a reference constant. We'll enforce
20133 constancy later. */
20134 cp_lexer_consume_token (parser->lexer);
20135 /* Parse the initializer. */
20136 initializer = cp_parser_initializer_clause (parser,
20137 &nonconst);
20138 }
20139 else
20140 /* Parse the initializer. */
20141 initializer = cp_parser_constant_initializer (parser);
20142 }
20143 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20144 && !function_declarator_p (declarator))
20145 {
20146 bool x;
20147 if (decl_specifiers.storage_class != sc_static)
20148 initializer = cp_parser_save_nsdmi (parser);
20149 else
20150 initializer = cp_parser_initializer (parser, &x, &x);
20151 }
20152 /* Otherwise, there is no initializer. */
20153 else
20154 initializer = NULL_TREE;
20155
20156 /* See if we are probably looking at a function
20157 definition. We are certainly not looking at a
20158 member-declarator. Calling `grokfield' has
20159 side-effects, so we must not do it unless we are sure
20160 that we are looking at a member-declarator. */
20161 if (cp_parser_token_starts_function_definition_p
20162 (cp_lexer_peek_token (parser->lexer)))
20163 {
20164 /* The grammar does not allow a pure-specifier to be
20165 used when a member function is defined. (It is
20166 possible that this fact is an oversight in the
20167 standard, since a pure function may be defined
20168 outside of the class-specifier. */
20169 if (initializer && initializer_token_start)
20170 error_at (initializer_token_start->location,
20171 "pure-specifier on function-definition");
20172 decl = cp_parser_save_member_function_body (parser,
20173 &decl_specifiers,
20174 declarator,
20175 attributes);
20176 /* If the member was not a friend, declare it here. */
20177 if (!friend_p)
20178 {
20179 if (parser->fully_implicit_function_template_p)
20180 decl = finish_fully_implicit_template (parser, decl);
20181 finish_member_declaration (decl);
20182 }
20183 /* Peek at the next token. */
20184 token = cp_lexer_peek_token (parser->lexer);
20185 /* If the next token is a semicolon, consume it. */
20186 if (token->type == CPP_SEMICOLON)
20187 cp_lexer_consume_token (parser->lexer);
20188 goto out;
20189 }
20190 else
20191 if (declarator->kind == cdk_function)
20192 declarator->id_loc = token->location;
20193 /* Create the declaration. */
20194 decl = grokfield (declarator, &decl_specifiers,
20195 initializer, /*init_const_expr_p=*/true,
20196 asm_specification, attributes);
20197 if (parser->fully_implicit_function_template_p)
20198 decl = finish_fully_implicit_template (parser, decl);
20199 }
20200
20201 cp_finalize_omp_declare_simd (parser, decl);
20202
20203 /* Reset PREFIX_ATTRIBUTES. */
20204 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20205 attributes = TREE_CHAIN (attributes);
20206 if (attributes)
20207 TREE_CHAIN (attributes) = NULL_TREE;
20208
20209 /* If there is any qualification still in effect, clear it
20210 now; we will be starting fresh with the next declarator. */
20211 parser->scope = NULL_TREE;
20212 parser->qualifying_scope = NULL_TREE;
20213 parser->object_scope = NULL_TREE;
20214 /* If it's a `,', then there are more declarators. */
20215 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20216 {
20217 cp_lexer_consume_token (parser->lexer);
20218 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20219 {
20220 cp_token *token = cp_lexer_previous_token (parser->lexer);
20221 error_at (token->location,
20222 "stray %<,%> at end of member declaration");
20223 }
20224 }
20225 /* If the next token isn't a `;', then we have a parse error. */
20226 else if (cp_lexer_next_token_is_not (parser->lexer,
20227 CPP_SEMICOLON))
20228 {
20229 /* The next token might be a ways away from where the
20230 actual semicolon is missing. Find the previous token
20231 and use that for our error position. */
20232 cp_token *token = cp_lexer_previous_token (parser->lexer);
20233 error_at (token->location,
20234 "expected %<;%> at end of member declaration");
20235
20236 /* Assume that the user meant to provide a semicolon. If
20237 we were to cp_parser_skip_to_end_of_statement, we might
20238 skip to a semicolon inside a member function definition
20239 and issue nonsensical error messages. */
20240 assume_semicolon = true;
20241 }
20242
20243 if (decl)
20244 {
20245 /* Add DECL to the list of members. */
20246 if (!friend_p)
20247 finish_member_declaration (decl);
20248
20249 if (TREE_CODE (decl) == FUNCTION_DECL)
20250 cp_parser_save_default_args (parser, decl);
20251 else if (TREE_CODE (decl) == FIELD_DECL
20252 && !DECL_C_BIT_FIELD (decl)
20253 && DECL_INITIAL (decl))
20254 /* Add DECL to the queue of NSDMI to be parsed later. */
20255 vec_safe_push (unparsed_nsdmis, decl);
20256 }
20257
20258 if (assume_semicolon)
20259 goto out;
20260 }
20261 }
20262
20263 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20264 out:
20265 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20266 }
20267
20268 /* Parse a pure-specifier.
20269
20270 pure-specifier:
20271 = 0
20272
20273 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20274 Otherwise, ERROR_MARK_NODE is returned. */
20275
20276 static tree
20277 cp_parser_pure_specifier (cp_parser* parser)
20278 {
20279 cp_token *token;
20280
20281 /* Look for the `=' token. */
20282 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20283 return error_mark_node;
20284 /* Look for the `0' token. */
20285 token = cp_lexer_peek_token (parser->lexer);
20286
20287 if (token->type == CPP_EOF
20288 || token->type == CPP_PRAGMA_EOL)
20289 return error_mark_node;
20290
20291 cp_lexer_consume_token (parser->lexer);
20292
20293 /* Accept = default or = delete in c++0x mode. */
20294 if (token->keyword == RID_DEFAULT
20295 || token->keyword == RID_DELETE)
20296 {
20297 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20298 return token->u.value;
20299 }
20300
20301 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20302 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20303 {
20304 cp_parser_error (parser,
20305 "invalid pure specifier (only %<= 0%> is allowed)");
20306 cp_parser_skip_to_end_of_statement (parser);
20307 return error_mark_node;
20308 }
20309 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20310 {
20311 error_at (token->location, "templates may not be %<virtual%>");
20312 return error_mark_node;
20313 }
20314
20315 return integer_zero_node;
20316 }
20317
20318 /* Parse a constant-initializer.
20319
20320 constant-initializer:
20321 = constant-expression
20322
20323 Returns a representation of the constant-expression. */
20324
20325 static tree
20326 cp_parser_constant_initializer (cp_parser* parser)
20327 {
20328 /* Look for the `=' token. */
20329 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20330 return error_mark_node;
20331
20332 /* It is invalid to write:
20333
20334 struct S { static const int i = { 7 }; };
20335
20336 */
20337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20338 {
20339 cp_parser_error (parser,
20340 "a brace-enclosed initializer is not allowed here");
20341 /* Consume the opening brace. */
20342 cp_lexer_consume_token (parser->lexer);
20343 /* Skip the initializer. */
20344 cp_parser_skip_to_closing_brace (parser);
20345 /* Look for the trailing `}'. */
20346 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20347
20348 return error_mark_node;
20349 }
20350
20351 return cp_parser_constant_expression (parser,
20352 /*allow_non_constant=*/false,
20353 NULL);
20354 }
20355
20356 /* Derived classes [gram.class.derived] */
20357
20358 /* Parse a base-clause.
20359
20360 base-clause:
20361 : base-specifier-list
20362
20363 base-specifier-list:
20364 base-specifier ... [opt]
20365 base-specifier-list , base-specifier ... [opt]
20366
20367 Returns a TREE_LIST representing the base-classes, in the order in
20368 which they were declared. The representation of each node is as
20369 described by cp_parser_base_specifier.
20370
20371 In the case that no bases are specified, this function will return
20372 NULL_TREE, not ERROR_MARK_NODE. */
20373
20374 static tree
20375 cp_parser_base_clause (cp_parser* parser)
20376 {
20377 tree bases = NULL_TREE;
20378
20379 /* Look for the `:' that begins the list. */
20380 cp_parser_require (parser, CPP_COLON, RT_COLON);
20381
20382 /* Scan the base-specifier-list. */
20383 while (true)
20384 {
20385 cp_token *token;
20386 tree base;
20387 bool pack_expansion_p = false;
20388
20389 /* Look for the base-specifier. */
20390 base = cp_parser_base_specifier (parser);
20391 /* Look for the (optional) ellipsis. */
20392 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20393 {
20394 /* Consume the `...'. */
20395 cp_lexer_consume_token (parser->lexer);
20396
20397 pack_expansion_p = true;
20398 }
20399
20400 /* Add BASE to the front of the list. */
20401 if (base && base != error_mark_node)
20402 {
20403 if (pack_expansion_p)
20404 /* Make this a pack expansion type. */
20405 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20406
20407 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20408 {
20409 TREE_CHAIN (base) = bases;
20410 bases = base;
20411 }
20412 }
20413 /* Peek at the next token. */
20414 token = cp_lexer_peek_token (parser->lexer);
20415 /* If it's not a comma, then the list is complete. */
20416 if (token->type != CPP_COMMA)
20417 break;
20418 /* Consume the `,'. */
20419 cp_lexer_consume_token (parser->lexer);
20420 }
20421
20422 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20423 base class had a qualified name. However, the next name that
20424 appears is certainly not qualified. */
20425 parser->scope = NULL_TREE;
20426 parser->qualifying_scope = NULL_TREE;
20427 parser->object_scope = NULL_TREE;
20428
20429 return nreverse (bases);
20430 }
20431
20432 /* Parse a base-specifier.
20433
20434 base-specifier:
20435 :: [opt] nested-name-specifier [opt] class-name
20436 virtual access-specifier [opt] :: [opt] nested-name-specifier
20437 [opt] class-name
20438 access-specifier virtual [opt] :: [opt] nested-name-specifier
20439 [opt] class-name
20440
20441 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20442 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20443 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20444 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20445
20446 static tree
20447 cp_parser_base_specifier (cp_parser* parser)
20448 {
20449 cp_token *token;
20450 bool done = false;
20451 bool virtual_p = false;
20452 bool duplicate_virtual_error_issued_p = false;
20453 bool duplicate_access_error_issued_p = false;
20454 bool class_scope_p, template_p;
20455 tree access = access_default_node;
20456 tree type;
20457
20458 /* Process the optional `virtual' and `access-specifier'. */
20459 while (!done)
20460 {
20461 /* Peek at the next token. */
20462 token = cp_lexer_peek_token (parser->lexer);
20463 /* Process `virtual'. */
20464 switch (token->keyword)
20465 {
20466 case RID_VIRTUAL:
20467 /* If `virtual' appears more than once, issue an error. */
20468 if (virtual_p && !duplicate_virtual_error_issued_p)
20469 {
20470 cp_parser_error (parser,
20471 "%<virtual%> specified more than once in base-specified");
20472 duplicate_virtual_error_issued_p = true;
20473 }
20474
20475 virtual_p = true;
20476
20477 /* Consume the `virtual' token. */
20478 cp_lexer_consume_token (parser->lexer);
20479
20480 break;
20481
20482 case RID_PUBLIC:
20483 case RID_PROTECTED:
20484 case RID_PRIVATE:
20485 /* If more than one access specifier appears, issue an
20486 error. */
20487 if (access != access_default_node
20488 && !duplicate_access_error_issued_p)
20489 {
20490 cp_parser_error (parser,
20491 "more than one access specifier in base-specified");
20492 duplicate_access_error_issued_p = true;
20493 }
20494
20495 access = ridpointers[(int) token->keyword];
20496
20497 /* Consume the access-specifier. */
20498 cp_lexer_consume_token (parser->lexer);
20499
20500 break;
20501
20502 default:
20503 done = true;
20504 break;
20505 }
20506 }
20507 /* It is not uncommon to see programs mechanically, erroneously, use
20508 the 'typename' keyword to denote (dependent) qualified types
20509 as base classes. */
20510 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20511 {
20512 token = cp_lexer_peek_token (parser->lexer);
20513 if (!processing_template_decl)
20514 error_at (token->location,
20515 "keyword %<typename%> not allowed outside of templates");
20516 else
20517 error_at (token->location,
20518 "keyword %<typename%> not allowed in this context "
20519 "(the base class is implicitly a type)");
20520 cp_lexer_consume_token (parser->lexer);
20521 }
20522
20523 /* Look for the optional `::' operator. */
20524 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20525 /* Look for the nested-name-specifier. The simplest way to
20526 implement:
20527
20528 [temp.res]
20529
20530 The keyword `typename' is not permitted in a base-specifier or
20531 mem-initializer; in these contexts a qualified name that
20532 depends on a template-parameter is implicitly assumed to be a
20533 type name.
20534
20535 is to pretend that we have seen the `typename' keyword at this
20536 point. */
20537 cp_parser_nested_name_specifier_opt (parser,
20538 /*typename_keyword_p=*/true,
20539 /*check_dependency_p=*/true,
20540 typename_type,
20541 /*is_declaration=*/true);
20542 /* If the base class is given by a qualified name, assume that names
20543 we see are type names or templates, as appropriate. */
20544 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20545 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20546
20547 if (!parser->scope
20548 && cp_lexer_next_token_is_decltype (parser->lexer))
20549 /* DR 950 allows decltype as a base-specifier. */
20550 type = cp_parser_decltype (parser);
20551 else
20552 {
20553 /* Otherwise, look for the class-name. */
20554 type = cp_parser_class_name (parser,
20555 class_scope_p,
20556 template_p,
20557 typename_type,
20558 /*check_dependency_p=*/true,
20559 /*class_head_p=*/false,
20560 /*is_declaration=*/true);
20561 type = TREE_TYPE (type);
20562 }
20563
20564 if (type == error_mark_node)
20565 return error_mark_node;
20566
20567 return finish_base_specifier (type, access, virtual_p);
20568 }
20569
20570 /* Exception handling [gram.exception] */
20571
20572 /* Parse an (optional) noexcept-specification.
20573
20574 noexcept-specification:
20575 noexcept ( constant-expression ) [opt]
20576
20577 If no noexcept-specification is present, returns NULL_TREE.
20578 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20579 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20580 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20581 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20582 in which case a boolean condition is returned instead. */
20583
20584 static tree
20585 cp_parser_noexcept_specification_opt (cp_parser* parser,
20586 bool require_constexpr,
20587 bool* consumed_expr,
20588 bool return_cond)
20589 {
20590 cp_token *token;
20591 const char *saved_message;
20592
20593 /* Peek at the next token. */
20594 token = cp_lexer_peek_token (parser->lexer);
20595
20596 /* Is it a noexcept-specification? */
20597 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20598 {
20599 tree expr;
20600 cp_lexer_consume_token (parser->lexer);
20601
20602 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20603 {
20604 cp_lexer_consume_token (parser->lexer);
20605
20606 if (require_constexpr)
20607 {
20608 /* Types may not be defined in an exception-specification. */
20609 saved_message = parser->type_definition_forbidden_message;
20610 parser->type_definition_forbidden_message
20611 = G_("types may not be defined in an exception-specification");
20612
20613 expr = cp_parser_constant_expression (parser, false, NULL);
20614
20615 /* Restore the saved message. */
20616 parser->type_definition_forbidden_message = saved_message;
20617 }
20618 else
20619 {
20620 expr = cp_parser_expression (parser, false, NULL);
20621 *consumed_expr = true;
20622 }
20623
20624 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20625 }
20626 else
20627 {
20628 expr = boolean_true_node;
20629 if (!require_constexpr)
20630 *consumed_expr = false;
20631 }
20632
20633 /* We cannot build a noexcept-spec right away because this will check
20634 that expr is a constexpr. */
20635 if (!return_cond)
20636 return build_noexcept_spec (expr, tf_warning_or_error);
20637 else
20638 return expr;
20639 }
20640 else
20641 return NULL_TREE;
20642 }
20643
20644 /* Parse an (optional) exception-specification.
20645
20646 exception-specification:
20647 throw ( type-id-list [opt] )
20648
20649 Returns a TREE_LIST representing the exception-specification. The
20650 TREE_VALUE of each node is a type. */
20651
20652 static tree
20653 cp_parser_exception_specification_opt (cp_parser* parser)
20654 {
20655 cp_token *token;
20656 tree type_id_list;
20657 const char *saved_message;
20658
20659 /* Peek at the next token. */
20660 token = cp_lexer_peek_token (parser->lexer);
20661
20662 /* Is it a noexcept-specification? */
20663 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20664 false);
20665 if (type_id_list != NULL_TREE)
20666 return type_id_list;
20667
20668 /* If it's not `throw', then there's no exception-specification. */
20669 if (!cp_parser_is_keyword (token, RID_THROW))
20670 return NULL_TREE;
20671
20672 #if 0
20673 /* Enable this once a lot of code has transitioned to noexcept? */
20674 if (cxx_dialect >= cxx11 && !in_system_header)
20675 warning (OPT_Wdeprecated, "dynamic exception specifications are "
20676 "deprecated in C++0x; use %<noexcept%> instead");
20677 #endif
20678
20679 /* Consume the `throw'. */
20680 cp_lexer_consume_token (parser->lexer);
20681
20682 /* Look for the `('. */
20683 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20684
20685 /* Peek at the next token. */
20686 token = cp_lexer_peek_token (parser->lexer);
20687 /* If it's not a `)', then there is a type-id-list. */
20688 if (token->type != CPP_CLOSE_PAREN)
20689 {
20690 /* Types may not be defined in an exception-specification. */
20691 saved_message = parser->type_definition_forbidden_message;
20692 parser->type_definition_forbidden_message
20693 = G_("types may not be defined in an exception-specification");
20694 /* Parse the type-id-list. */
20695 type_id_list = cp_parser_type_id_list (parser);
20696 /* Restore the saved message. */
20697 parser->type_definition_forbidden_message = saved_message;
20698 }
20699 else
20700 type_id_list = empty_except_spec;
20701
20702 /* Look for the `)'. */
20703 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20704
20705 return type_id_list;
20706 }
20707
20708 /* Parse an (optional) type-id-list.
20709
20710 type-id-list:
20711 type-id ... [opt]
20712 type-id-list , type-id ... [opt]
20713
20714 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20715 in the order that the types were presented. */
20716
20717 static tree
20718 cp_parser_type_id_list (cp_parser* parser)
20719 {
20720 tree types = NULL_TREE;
20721
20722 while (true)
20723 {
20724 cp_token *token;
20725 tree type;
20726
20727 /* Get the next type-id. */
20728 type = cp_parser_type_id (parser);
20729 /* Parse the optional ellipsis. */
20730 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20731 {
20732 /* Consume the `...'. */
20733 cp_lexer_consume_token (parser->lexer);
20734
20735 /* Turn the type into a pack expansion expression. */
20736 type = make_pack_expansion (type);
20737 }
20738 /* Add it to the list. */
20739 types = add_exception_specifier (types, type, /*complain=*/1);
20740 /* Peek at the next token. */
20741 token = cp_lexer_peek_token (parser->lexer);
20742 /* If it is not a `,', we are done. */
20743 if (token->type != CPP_COMMA)
20744 break;
20745 /* Consume the `,'. */
20746 cp_lexer_consume_token (parser->lexer);
20747 }
20748
20749 return nreverse (types);
20750 }
20751
20752 /* Parse a try-block.
20753
20754 try-block:
20755 try compound-statement handler-seq */
20756
20757 static tree
20758 cp_parser_try_block (cp_parser* parser)
20759 {
20760 tree try_block;
20761
20762 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20763 try_block = begin_try_block ();
20764 cp_parser_compound_statement (parser, NULL, true, false);
20765 finish_try_block (try_block);
20766 cp_parser_handler_seq (parser);
20767 finish_handler_sequence (try_block);
20768
20769 return try_block;
20770 }
20771
20772 /* Parse a function-try-block.
20773
20774 function-try-block:
20775 try ctor-initializer [opt] function-body handler-seq */
20776
20777 static bool
20778 cp_parser_function_try_block (cp_parser* parser)
20779 {
20780 tree compound_stmt;
20781 tree try_block;
20782 bool ctor_initializer_p;
20783
20784 /* Look for the `try' keyword. */
20785 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20786 return false;
20787 /* Let the rest of the front end know where we are. */
20788 try_block = begin_function_try_block (&compound_stmt);
20789 /* Parse the function-body. */
20790 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20791 (parser, /*in_function_try_block=*/true);
20792 /* We're done with the `try' part. */
20793 finish_function_try_block (try_block);
20794 /* Parse the handlers. */
20795 cp_parser_handler_seq (parser);
20796 /* We're done with the handlers. */
20797 finish_function_handler_sequence (try_block, compound_stmt);
20798
20799 return ctor_initializer_p;
20800 }
20801
20802 /* Parse a handler-seq.
20803
20804 handler-seq:
20805 handler handler-seq [opt] */
20806
20807 static void
20808 cp_parser_handler_seq (cp_parser* parser)
20809 {
20810 while (true)
20811 {
20812 cp_token *token;
20813
20814 /* Parse the handler. */
20815 cp_parser_handler (parser);
20816 /* Peek at the next token. */
20817 token = cp_lexer_peek_token (parser->lexer);
20818 /* If it's not `catch' then there are no more handlers. */
20819 if (!cp_parser_is_keyword (token, RID_CATCH))
20820 break;
20821 }
20822 }
20823
20824 /* Parse a handler.
20825
20826 handler:
20827 catch ( exception-declaration ) compound-statement */
20828
20829 static void
20830 cp_parser_handler (cp_parser* parser)
20831 {
20832 tree handler;
20833 tree declaration;
20834
20835 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20836 handler = begin_handler ();
20837 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20838 declaration = cp_parser_exception_declaration (parser);
20839 finish_handler_parms (declaration, handler);
20840 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20841 cp_parser_compound_statement (parser, NULL, false, false);
20842 finish_handler (handler);
20843 }
20844
20845 /* Parse an exception-declaration.
20846
20847 exception-declaration:
20848 type-specifier-seq declarator
20849 type-specifier-seq abstract-declarator
20850 type-specifier-seq
20851 ...
20852
20853 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20854 ellipsis variant is used. */
20855
20856 static tree
20857 cp_parser_exception_declaration (cp_parser* parser)
20858 {
20859 cp_decl_specifier_seq type_specifiers;
20860 cp_declarator *declarator;
20861 const char *saved_message;
20862
20863 /* If it's an ellipsis, it's easy to handle. */
20864 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20865 {
20866 /* Consume the `...' token. */
20867 cp_lexer_consume_token (parser->lexer);
20868 return NULL_TREE;
20869 }
20870
20871 /* Types may not be defined in exception-declarations. */
20872 saved_message = parser->type_definition_forbidden_message;
20873 parser->type_definition_forbidden_message
20874 = G_("types may not be defined in exception-declarations");
20875
20876 /* Parse the type-specifier-seq. */
20877 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20878 /*is_trailing_return=*/false,
20879 &type_specifiers);
20880 /* If it's a `)', then there is no declarator. */
20881 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20882 declarator = NULL;
20883 else
20884 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20885 /*ctor_dtor_or_conv_p=*/NULL,
20886 /*parenthesized_p=*/NULL,
20887 /*member_p=*/false);
20888
20889 /* Restore the saved message. */
20890 parser->type_definition_forbidden_message = saved_message;
20891
20892 if (!type_specifiers.any_specifiers_p)
20893 return error_mark_node;
20894
20895 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20896 }
20897
20898 /* Parse a throw-expression.
20899
20900 throw-expression:
20901 throw assignment-expression [opt]
20902
20903 Returns a THROW_EXPR representing the throw-expression. */
20904
20905 static tree
20906 cp_parser_throw_expression (cp_parser* parser)
20907 {
20908 tree expression;
20909 cp_token* token;
20910
20911 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20912 token = cp_lexer_peek_token (parser->lexer);
20913 /* Figure out whether or not there is an assignment-expression
20914 following the "throw" keyword. */
20915 if (token->type == CPP_COMMA
20916 || token->type == CPP_SEMICOLON
20917 || token->type == CPP_CLOSE_PAREN
20918 || token->type == CPP_CLOSE_SQUARE
20919 || token->type == CPP_CLOSE_BRACE
20920 || token->type == CPP_COLON)
20921 expression = NULL_TREE;
20922 else
20923 expression = cp_parser_assignment_expression (parser,
20924 /*cast_p=*/false, NULL);
20925
20926 return build_throw (expression);
20927 }
20928
20929 /* GNU Extensions */
20930
20931 /* Parse an (optional) asm-specification.
20932
20933 asm-specification:
20934 asm ( string-literal )
20935
20936 If the asm-specification is present, returns a STRING_CST
20937 corresponding to the string-literal. Otherwise, returns
20938 NULL_TREE. */
20939
20940 static tree
20941 cp_parser_asm_specification_opt (cp_parser* parser)
20942 {
20943 cp_token *token;
20944 tree asm_specification;
20945
20946 /* Peek at the next token. */
20947 token = cp_lexer_peek_token (parser->lexer);
20948 /* If the next token isn't the `asm' keyword, then there's no
20949 asm-specification. */
20950 if (!cp_parser_is_keyword (token, RID_ASM))
20951 return NULL_TREE;
20952
20953 /* Consume the `asm' token. */
20954 cp_lexer_consume_token (parser->lexer);
20955 /* Look for the `('. */
20956 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20957
20958 /* Look for the string-literal. */
20959 asm_specification = cp_parser_string_literal (parser, false, false);
20960
20961 /* Look for the `)'. */
20962 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20963
20964 return asm_specification;
20965 }
20966
20967 /* Parse an asm-operand-list.
20968
20969 asm-operand-list:
20970 asm-operand
20971 asm-operand-list , asm-operand
20972
20973 asm-operand:
20974 string-literal ( expression )
20975 [ string-literal ] string-literal ( expression )
20976
20977 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20978 each node is the expression. The TREE_PURPOSE is itself a
20979 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20980 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20981 is a STRING_CST for the string literal before the parenthesis. Returns
20982 ERROR_MARK_NODE if any of the operands are invalid. */
20983
20984 static tree
20985 cp_parser_asm_operand_list (cp_parser* parser)
20986 {
20987 tree asm_operands = NULL_TREE;
20988 bool invalid_operands = false;
20989
20990 while (true)
20991 {
20992 tree string_literal;
20993 tree expression;
20994 tree name;
20995
20996 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20997 {
20998 /* Consume the `[' token. */
20999 cp_lexer_consume_token (parser->lexer);
21000 /* Read the operand name. */
21001 name = cp_parser_identifier (parser);
21002 if (name != error_mark_node)
21003 name = build_string (IDENTIFIER_LENGTH (name),
21004 IDENTIFIER_POINTER (name));
21005 /* Look for the closing `]'. */
21006 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21007 }
21008 else
21009 name = NULL_TREE;
21010 /* Look for the string-literal. */
21011 string_literal = cp_parser_string_literal (parser, false, false);
21012
21013 /* Look for the `('. */
21014 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21015 /* Parse the expression. */
21016 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21017 /* Look for the `)'. */
21018 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21019
21020 if (name == error_mark_node
21021 || string_literal == error_mark_node
21022 || expression == error_mark_node)
21023 invalid_operands = true;
21024
21025 /* Add this operand to the list. */
21026 asm_operands = tree_cons (build_tree_list (name, string_literal),
21027 expression,
21028 asm_operands);
21029 /* If the next token is not a `,', there are no more
21030 operands. */
21031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21032 break;
21033 /* Consume the `,'. */
21034 cp_lexer_consume_token (parser->lexer);
21035 }
21036
21037 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21038 }
21039
21040 /* Parse an asm-clobber-list.
21041
21042 asm-clobber-list:
21043 string-literal
21044 asm-clobber-list , string-literal
21045
21046 Returns a TREE_LIST, indicating the clobbers in the order that they
21047 appeared. The TREE_VALUE of each node is a STRING_CST. */
21048
21049 static tree
21050 cp_parser_asm_clobber_list (cp_parser* parser)
21051 {
21052 tree clobbers = NULL_TREE;
21053
21054 while (true)
21055 {
21056 tree string_literal;
21057
21058 /* Look for the string literal. */
21059 string_literal = cp_parser_string_literal (parser, false, false);
21060 /* Add it to the list. */
21061 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21062 /* If the next token is not a `,', then the list is
21063 complete. */
21064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21065 break;
21066 /* Consume the `,' token. */
21067 cp_lexer_consume_token (parser->lexer);
21068 }
21069
21070 return clobbers;
21071 }
21072
21073 /* Parse an asm-label-list.
21074
21075 asm-label-list:
21076 identifier
21077 asm-label-list , identifier
21078
21079 Returns a TREE_LIST, indicating the labels in the order that they
21080 appeared. The TREE_VALUE of each node is a label. */
21081
21082 static tree
21083 cp_parser_asm_label_list (cp_parser* parser)
21084 {
21085 tree labels = NULL_TREE;
21086
21087 while (true)
21088 {
21089 tree identifier, label, name;
21090
21091 /* Look for the identifier. */
21092 identifier = cp_parser_identifier (parser);
21093 if (!error_operand_p (identifier))
21094 {
21095 label = lookup_label (identifier);
21096 if (TREE_CODE (label) == LABEL_DECL)
21097 {
21098 TREE_USED (label) = 1;
21099 check_goto (label);
21100 name = build_string (IDENTIFIER_LENGTH (identifier),
21101 IDENTIFIER_POINTER (identifier));
21102 labels = tree_cons (name, label, labels);
21103 }
21104 }
21105 /* If the next token is not a `,', then the list is
21106 complete. */
21107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21108 break;
21109 /* Consume the `,' token. */
21110 cp_lexer_consume_token (parser->lexer);
21111 }
21112
21113 return nreverse (labels);
21114 }
21115
21116 /* Return TRUE iff the next tokens in the stream are possibly the
21117 beginning of a GNU extension attribute. */
21118
21119 static bool
21120 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21121 {
21122 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21123 }
21124
21125 /* Return TRUE iff the next tokens in the stream are possibly the
21126 beginning of a standard C++-11 attribute specifier. */
21127
21128 static bool
21129 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21130 {
21131 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21132 }
21133
21134 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21135 beginning of a standard C++-11 attribute specifier. */
21136
21137 static bool
21138 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21139 {
21140 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21141
21142 return (cxx_dialect >= cxx11
21143 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21144 || (token->type == CPP_OPEN_SQUARE
21145 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21146 && token->type == CPP_OPEN_SQUARE)));
21147 }
21148
21149 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21150 beginning of a GNU extension attribute. */
21151
21152 static bool
21153 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21154 {
21155 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21156
21157 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21158 }
21159
21160 /* Return true iff the next tokens can be the beginning of either a
21161 GNU attribute list, or a standard C++11 attribute sequence. */
21162
21163 static bool
21164 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21165 {
21166 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21167 || cp_next_tokens_can_be_std_attribute_p (parser));
21168 }
21169
21170 /* Return true iff the next Nth tokens can be the beginning of either
21171 a GNU attribute list, or a standard C++11 attribute sequence. */
21172
21173 static bool
21174 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21175 {
21176 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21177 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21178 }
21179
21180 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21181 of GNU attributes, or return NULL. */
21182
21183 static tree
21184 cp_parser_attributes_opt (cp_parser *parser)
21185 {
21186 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21187 return cp_parser_gnu_attributes_opt (parser);
21188 return cp_parser_std_attribute_spec_seq (parser);
21189 }
21190
21191 /* Parse an (optional) series of attributes.
21192
21193 attributes:
21194 attributes attribute
21195
21196 attribute:
21197 __attribute__ (( attribute-list [opt] ))
21198
21199 The return value is as for cp_parser_gnu_attribute_list. */
21200
21201 static tree
21202 cp_parser_gnu_attributes_opt (cp_parser* parser)
21203 {
21204 tree attributes = NULL_TREE;
21205
21206 while (true)
21207 {
21208 cp_token *token;
21209 tree attribute_list;
21210 bool ok = true;
21211
21212 /* Peek at the next token. */
21213 token = cp_lexer_peek_token (parser->lexer);
21214 /* If it's not `__attribute__', then we're done. */
21215 if (token->keyword != RID_ATTRIBUTE)
21216 break;
21217
21218 /* Consume the `__attribute__' keyword. */
21219 cp_lexer_consume_token (parser->lexer);
21220 /* Look for the two `(' tokens. */
21221 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21222 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21223
21224 /* Peek at the next token. */
21225 token = cp_lexer_peek_token (parser->lexer);
21226 if (token->type != CPP_CLOSE_PAREN)
21227 /* Parse the attribute-list. */
21228 attribute_list = cp_parser_gnu_attribute_list (parser);
21229 else
21230 /* If the next token is a `)', then there is no attribute
21231 list. */
21232 attribute_list = NULL;
21233
21234 /* Look for the two `)' tokens. */
21235 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21236 ok = false;
21237 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21238 ok = false;
21239 if (!ok)
21240 cp_parser_skip_to_end_of_statement (parser);
21241
21242 /* Add these new attributes to the list. */
21243 attributes = chainon (attributes, attribute_list);
21244 }
21245
21246 return attributes;
21247 }
21248
21249 /* Parse a GNU attribute-list.
21250
21251 attribute-list:
21252 attribute
21253 attribute-list , attribute
21254
21255 attribute:
21256 identifier
21257 identifier ( identifier )
21258 identifier ( identifier , expression-list )
21259 identifier ( expression-list )
21260
21261 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21262 to an attribute. The TREE_PURPOSE of each node is the identifier
21263 indicating which attribute is in use. The TREE_VALUE represents
21264 the arguments, if any. */
21265
21266 static tree
21267 cp_parser_gnu_attribute_list (cp_parser* parser)
21268 {
21269 tree attribute_list = NULL_TREE;
21270 bool save_translate_strings_p = parser->translate_strings_p;
21271
21272 parser->translate_strings_p = false;
21273 while (true)
21274 {
21275 cp_token *token;
21276 tree identifier;
21277 tree attribute;
21278
21279 /* Look for the identifier. We also allow keywords here; for
21280 example `__attribute__ ((const))' is legal. */
21281 token = cp_lexer_peek_token (parser->lexer);
21282 if (token->type == CPP_NAME
21283 || token->type == CPP_KEYWORD)
21284 {
21285 tree arguments = NULL_TREE;
21286
21287 /* Consume the token. */
21288 token = cp_lexer_consume_token (parser->lexer);
21289
21290 /* Save away the identifier that indicates which attribute
21291 this is. */
21292 identifier = (token->type == CPP_KEYWORD)
21293 /* For keywords, use the canonical spelling, not the
21294 parsed identifier. */
21295 ? ridpointers[(int) token->keyword]
21296 : token->u.value;
21297
21298 attribute = build_tree_list (identifier, NULL_TREE);
21299
21300 /* Peek at the next token. */
21301 token = cp_lexer_peek_token (parser->lexer);
21302 /* If it's an `(', then parse the attribute arguments. */
21303 if (token->type == CPP_OPEN_PAREN)
21304 {
21305 vec<tree, va_gc> *vec;
21306 int attr_flag = (attribute_takes_identifier_p (identifier)
21307 ? id_attr : normal_attr);
21308 vec = cp_parser_parenthesized_expression_list
21309 (parser, attr_flag, /*cast_p=*/false,
21310 /*allow_expansion_p=*/false,
21311 /*non_constant_p=*/NULL);
21312 if (vec == NULL)
21313 arguments = error_mark_node;
21314 else
21315 {
21316 arguments = build_tree_list_vec (vec);
21317 release_tree_vector (vec);
21318 }
21319 /* Save the arguments away. */
21320 TREE_VALUE (attribute) = arguments;
21321 }
21322
21323 if (arguments != error_mark_node)
21324 {
21325 /* Add this attribute to the list. */
21326 TREE_CHAIN (attribute) = attribute_list;
21327 attribute_list = attribute;
21328 }
21329
21330 token = cp_lexer_peek_token (parser->lexer);
21331 }
21332 /* Now, look for more attributes. If the next token isn't a
21333 `,', we're done. */
21334 if (token->type != CPP_COMMA)
21335 break;
21336
21337 /* Consume the comma and keep going. */
21338 cp_lexer_consume_token (parser->lexer);
21339 }
21340 parser->translate_strings_p = save_translate_strings_p;
21341
21342 /* We built up the list in reverse order. */
21343 return nreverse (attribute_list);
21344 }
21345
21346 /* Parse a standard C++11 attribute.
21347
21348 The returned representation is a TREE_LIST which TREE_PURPOSE is
21349 the scoped name of the attribute, and the TREE_VALUE is its
21350 arguments list.
21351
21352 Note that the scoped name of the attribute is itself a TREE_LIST
21353 which TREE_PURPOSE is the namespace of the attribute, and
21354 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21355 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21356 and which TREE_PURPOSE is directly the attribute name.
21357
21358 Clients of the attribute code should use get_attribute_namespace
21359 and get_attribute_name to get the actual namespace and name of
21360 attributes, regardless of their being GNU or C++11 attributes.
21361
21362 attribute:
21363 attribute-token attribute-argument-clause [opt]
21364
21365 attribute-token:
21366 identifier
21367 attribute-scoped-token
21368
21369 attribute-scoped-token:
21370 attribute-namespace :: identifier
21371
21372 attribute-namespace:
21373 identifier
21374
21375 attribute-argument-clause:
21376 ( balanced-token-seq )
21377
21378 balanced-token-seq:
21379 balanced-token [opt]
21380 balanced-token-seq balanced-token
21381
21382 balanced-token:
21383 ( balanced-token-seq )
21384 [ balanced-token-seq ]
21385 { balanced-token-seq }. */
21386
21387 static tree
21388 cp_parser_std_attribute (cp_parser *parser)
21389 {
21390 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21391 cp_token *token;
21392
21393 /* First, parse name of the the attribute, a.k.a
21394 attribute-token. */
21395
21396 token = cp_lexer_peek_token (parser->lexer);
21397 if (token->type == CPP_NAME)
21398 attr_id = token->u.value;
21399 else if (token->type == CPP_KEYWORD)
21400 attr_id = ridpointers[(int) token->keyword];
21401 else if (token->flags & NAMED_OP)
21402 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21403
21404 if (attr_id == NULL_TREE)
21405 return NULL_TREE;
21406
21407 cp_lexer_consume_token (parser->lexer);
21408
21409 token = cp_lexer_peek_token (parser->lexer);
21410 if (token->type == CPP_SCOPE)
21411 {
21412 /* We are seeing a scoped attribute token. */
21413
21414 cp_lexer_consume_token (parser->lexer);
21415 attr_ns = attr_id;
21416
21417 token = cp_lexer_consume_token (parser->lexer);
21418 if (token->type == CPP_NAME)
21419 attr_id = token->u.value;
21420 else if (token->type == CPP_KEYWORD)
21421 attr_id = ridpointers[(int) token->keyword];
21422 else
21423 {
21424 error_at (token->location,
21425 "expected an identifier for the attribute name");
21426 return error_mark_node;
21427 }
21428 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21429 NULL_TREE);
21430 token = cp_lexer_peek_token (parser->lexer);
21431 }
21432 else
21433 {
21434 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21435 NULL_TREE);
21436 /* C++11 noreturn attribute is equivalent to GNU's. */
21437 if (is_attribute_p ("noreturn", attr_id))
21438 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21439 /* C++14 deprecated attribute is equivalent to GNU's. */
21440 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21441 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21442 }
21443
21444 /* Now parse the optional argument clause of the attribute. */
21445
21446 if (token->type != CPP_OPEN_PAREN)
21447 return attribute;
21448
21449 {
21450 vec<tree, va_gc> *vec;
21451 int attr_flag = normal_attr;
21452
21453 if (attr_ns == get_identifier ("gnu")
21454 && attribute_takes_identifier_p (attr_id))
21455 /* A GNU attribute that takes an identifier in parameter. */
21456 attr_flag = id_attr;
21457
21458 vec = cp_parser_parenthesized_expression_list
21459 (parser, attr_flag, /*cast_p=*/false,
21460 /*allow_expansion_p=*/true,
21461 /*non_constant_p=*/NULL);
21462 if (vec == NULL)
21463 arguments = error_mark_node;
21464 else
21465 {
21466 arguments = build_tree_list_vec (vec);
21467 release_tree_vector (vec);
21468 }
21469
21470 if (arguments == error_mark_node)
21471 attribute = error_mark_node;
21472 else
21473 TREE_VALUE (attribute) = arguments;
21474 }
21475
21476 return attribute;
21477 }
21478
21479 /* Parse a list of standard C++-11 attributes.
21480
21481 attribute-list:
21482 attribute [opt]
21483 attribute-list , attribute[opt]
21484 attribute ...
21485 attribute-list , attribute ...
21486 */
21487
21488 static tree
21489 cp_parser_std_attribute_list (cp_parser *parser)
21490 {
21491 tree attributes = NULL_TREE, attribute = NULL_TREE;
21492 cp_token *token = NULL;
21493
21494 while (true)
21495 {
21496 attribute = cp_parser_std_attribute (parser);
21497 if (attribute == error_mark_node)
21498 break;
21499 if (attribute != NULL_TREE)
21500 {
21501 TREE_CHAIN (attribute) = attributes;
21502 attributes = attribute;
21503 }
21504 token = cp_lexer_peek_token (parser->lexer);
21505 if (token->type != CPP_COMMA)
21506 break;
21507 cp_lexer_consume_token (parser->lexer);
21508 }
21509 attributes = nreverse (attributes);
21510 return attributes;
21511 }
21512
21513 /* Parse a standard C++-11 attribute specifier.
21514
21515 attribute-specifier:
21516 [ [ attribute-list ] ]
21517 alignment-specifier
21518
21519 alignment-specifier:
21520 alignas ( type-id ... [opt] )
21521 alignas ( alignment-expression ... [opt] ). */
21522
21523 static tree
21524 cp_parser_std_attribute_spec (cp_parser *parser)
21525 {
21526 tree attributes = NULL_TREE;
21527 cp_token *token = cp_lexer_peek_token (parser->lexer);
21528
21529 if (token->type == CPP_OPEN_SQUARE
21530 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21531 {
21532 cp_lexer_consume_token (parser->lexer);
21533 cp_lexer_consume_token (parser->lexer);
21534
21535 attributes = cp_parser_std_attribute_list (parser);
21536
21537 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21538 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21539 cp_parser_skip_to_end_of_statement (parser);
21540 else
21541 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21542 when we are sure that we have actually parsed them. */
21543 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21544 }
21545 else
21546 {
21547 tree alignas_expr;
21548
21549 /* Look for an alignment-specifier. */
21550
21551 token = cp_lexer_peek_token (parser->lexer);
21552
21553 if (token->type != CPP_KEYWORD
21554 || token->keyword != RID_ALIGNAS)
21555 return NULL_TREE;
21556
21557 cp_lexer_consume_token (parser->lexer);
21558 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21559
21560 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21561 {
21562 cp_parser_error (parser, "expected %<(%>");
21563 return error_mark_node;
21564 }
21565
21566 cp_parser_parse_tentatively (parser);
21567 alignas_expr = cp_parser_type_id (parser);
21568
21569 if (!cp_parser_parse_definitely (parser))
21570 {
21571 gcc_assert (alignas_expr == error_mark_node
21572 || alignas_expr == NULL_TREE);
21573
21574 alignas_expr =
21575 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21576 /**cp_id_kind=*/NULL);
21577 if (alignas_expr == error_mark_node)
21578 cp_parser_skip_to_end_of_statement (parser);
21579 if (alignas_expr == NULL_TREE
21580 || alignas_expr == error_mark_node)
21581 return alignas_expr;
21582 }
21583
21584 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
21585 {
21586 cp_parser_error (parser, "expected %<)%>");
21587 return error_mark_node;
21588 }
21589
21590 alignas_expr = cxx_alignas_expr (alignas_expr);
21591
21592 /* Build the C++-11 representation of an 'aligned'
21593 attribute. */
21594 attributes =
21595 build_tree_list (build_tree_list (get_identifier ("gnu"),
21596 get_identifier ("aligned")),
21597 build_tree_list (NULL_TREE, alignas_expr));
21598 }
21599
21600 return attributes;
21601 }
21602
21603 /* Parse a standard C++-11 attribute-specifier-seq.
21604
21605 attribute-specifier-seq:
21606 attribute-specifier-seq [opt] attribute-specifier
21607 */
21608
21609 static tree
21610 cp_parser_std_attribute_spec_seq (cp_parser *parser)
21611 {
21612 tree attr_specs = NULL;
21613
21614 while (true)
21615 {
21616 tree attr_spec = cp_parser_std_attribute_spec (parser);
21617 if (attr_spec == NULL_TREE)
21618 break;
21619 if (attr_spec == error_mark_node)
21620 return error_mark_node;
21621
21622 TREE_CHAIN (attr_spec) = attr_specs;
21623 attr_specs = attr_spec;
21624 }
21625
21626 attr_specs = nreverse (attr_specs);
21627 return attr_specs;
21628 }
21629
21630 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21631 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21632 current value of the PEDANTIC flag, regardless of whether or not
21633 the `__extension__' keyword is present. The caller is responsible
21634 for restoring the value of the PEDANTIC flag. */
21635
21636 static bool
21637 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21638 {
21639 /* Save the old value of the PEDANTIC flag. */
21640 *saved_pedantic = pedantic;
21641
21642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21643 {
21644 /* Consume the `__extension__' token. */
21645 cp_lexer_consume_token (parser->lexer);
21646 /* We're not being pedantic while the `__extension__' keyword is
21647 in effect. */
21648 pedantic = 0;
21649
21650 return true;
21651 }
21652
21653 return false;
21654 }
21655
21656 /* Parse a label declaration.
21657
21658 label-declaration:
21659 __label__ label-declarator-seq ;
21660
21661 label-declarator-seq:
21662 identifier , label-declarator-seq
21663 identifier */
21664
21665 static void
21666 cp_parser_label_declaration (cp_parser* parser)
21667 {
21668 /* Look for the `__label__' keyword. */
21669 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21670
21671 while (true)
21672 {
21673 tree identifier;
21674
21675 /* Look for an identifier. */
21676 identifier = cp_parser_identifier (parser);
21677 /* If we failed, stop. */
21678 if (identifier == error_mark_node)
21679 break;
21680 /* Declare it as a label. */
21681 finish_label_decl (identifier);
21682 /* If the next token is a `;', stop. */
21683 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21684 break;
21685 /* Look for the `,' separating the label declarations. */
21686 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21687 }
21688
21689 /* Look for the final `;'. */
21690 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21691 }
21692
21693 /* Support Functions */
21694
21695 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21696 NAME should have one of the representations used for an
21697 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21698 is returned. If PARSER->SCOPE is a dependent type, then a
21699 SCOPE_REF is returned.
21700
21701 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21702 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21703 was formed. Abstractly, such entities should not be passed to this
21704 function, because they do not need to be looked up, but it is
21705 simpler to check for this special case here, rather than at the
21706 call-sites.
21707
21708 In cases not explicitly covered above, this function returns a
21709 DECL, OVERLOAD, or baselink representing the result of the lookup.
21710 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21711 is returned.
21712
21713 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21714 (e.g., "struct") that was used. In that case bindings that do not
21715 refer to types are ignored.
21716
21717 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21718 ignored.
21719
21720 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21721 are ignored.
21722
21723 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21724 types.
21725
21726 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21727 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21728 NULL_TREE otherwise. */
21729
21730 static tree
21731 cp_parser_lookup_name (cp_parser *parser, tree name,
21732 enum tag_types tag_type,
21733 bool is_template,
21734 bool is_namespace,
21735 bool check_dependency,
21736 tree *ambiguous_decls,
21737 location_t name_location)
21738 {
21739 tree decl;
21740 tree object_type = parser->context->object_type;
21741
21742 /* Assume that the lookup will be unambiguous. */
21743 if (ambiguous_decls)
21744 *ambiguous_decls = NULL_TREE;
21745
21746 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21747 no longer valid. Note that if we are parsing tentatively, and
21748 the parse fails, OBJECT_TYPE will be automatically restored. */
21749 parser->context->object_type = NULL_TREE;
21750
21751 if (name == error_mark_node)
21752 return error_mark_node;
21753
21754 /* A template-id has already been resolved; there is no lookup to
21755 do. */
21756 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21757 return name;
21758 if (BASELINK_P (name))
21759 {
21760 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21761 == TEMPLATE_ID_EXPR);
21762 return name;
21763 }
21764
21765 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21766 it should already have been checked to make sure that the name
21767 used matches the type being destroyed. */
21768 if (TREE_CODE (name) == BIT_NOT_EXPR)
21769 {
21770 tree type;
21771
21772 /* Figure out to which type this destructor applies. */
21773 if (parser->scope)
21774 type = parser->scope;
21775 else if (object_type)
21776 type = object_type;
21777 else
21778 type = current_class_type;
21779 /* If that's not a class type, there is no destructor. */
21780 if (!type || !CLASS_TYPE_P (type))
21781 return error_mark_node;
21782 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21783 lazily_declare_fn (sfk_destructor, type);
21784 if (!CLASSTYPE_DESTRUCTORS (type))
21785 return error_mark_node;
21786 /* If it was a class type, return the destructor. */
21787 return CLASSTYPE_DESTRUCTORS (type);
21788 }
21789
21790 /* By this point, the NAME should be an ordinary identifier. If
21791 the id-expression was a qualified name, the qualifying scope is
21792 stored in PARSER->SCOPE at this point. */
21793 gcc_assert (identifier_p (name));
21794
21795 /* Perform the lookup. */
21796 if (parser->scope)
21797 {
21798 bool dependent_p;
21799
21800 if (parser->scope == error_mark_node)
21801 return error_mark_node;
21802
21803 /* If the SCOPE is dependent, the lookup must be deferred until
21804 the template is instantiated -- unless we are explicitly
21805 looking up names in uninstantiated templates. Even then, we
21806 cannot look up the name if the scope is not a class type; it
21807 might, for example, be a template type parameter. */
21808 dependent_p = (TYPE_P (parser->scope)
21809 && dependent_scope_p (parser->scope));
21810 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21811 && dependent_p)
21812 /* Defer lookup. */
21813 decl = error_mark_node;
21814 else
21815 {
21816 tree pushed_scope = NULL_TREE;
21817
21818 /* If PARSER->SCOPE is a dependent type, then it must be a
21819 class type, and we must not be checking dependencies;
21820 otherwise, we would have processed this lookup above. So
21821 that PARSER->SCOPE is not considered a dependent base by
21822 lookup_member, we must enter the scope here. */
21823 if (dependent_p)
21824 pushed_scope = push_scope (parser->scope);
21825
21826 /* If the PARSER->SCOPE is a template specialization, it
21827 may be instantiated during name lookup. In that case,
21828 errors may be issued. Even if we rollback the current
21829 tentative parse, those errors are valid. */
21830 decl = lookup_qualified_name (parser->scope, name,
21831 tag_type != none_type,
21832 /*complain=*/true);
21833
21834 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21835 lookup result and the nested-name-specifier nominates a class C:
21836 * if the name specified after the nested-name-specifier, when
21837 looked up in C, is the injected-class-name of C (Clause 9), or
21838 * if the name specified after the nested-name-specifier is the
21839 same as the identifier or the simple-template-id's template-
21840 name in the last component of the nested-name-specifier,
21841 the name is instead considered to name the constructor of
21842 class C. [ Note: for example, the constructor is not an
21843 acceptable lookup result in an elaborated-type-specifier so
21844 the constructor would not be used in place of the
21845 injected-class-name. --end note ] Such a constructor name
21846 shall be used only in the declarator-id of a declaration that
21847 names a constructor or in a using-declaration. */
21848 if (tag_type == none_type
21849 && DECL_SELF_REFERENCE_P (decl)
21850 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21851 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21852 tag_type != none_type,
21853 /*complain=*/true);
21854
21855 /* If we have a single function from a using decl, pull it out. */
21856 if (TREE_CODE (decl) == OVERLOAD
21857 && !really_overloaded_fn (decl))
21858 decl = OVL_FUNCTION (decl);
21859
21860 if (pushed_scope)
21861 pop_scope (pushed_scope);
21862 }
21863
21864 /* If the scope is a dependent type and either we deferred lookup or
21865 we did lookup but didn't find the name, rememeber the name. */
21866 if (decl == error_mark_node && TYPE_P (parser->scope)
21867 && dependent_type_p (parser->scope))
21868 {
21869 if (tag_type)
21870 {
21871 tree type;
21872
21873 /* The resolution to Core Issue 180 says that `struct
21874 A::B' should be considered a type-name, even if `A'
21875 is dependent. */
21876 type = make_typename_type (parser->scope, name, tag_type,
21877 /*complain=*/tf_error);
21878 if (type != error_mark_node)
21879 decl = TYPE_NAME (type);
21880 }
21881 else if (is_template
21882 && (cp_parser_next_token_ends_template_argument_p (parser)
21883 || cp_lexer_next_token_is (parser->lexer,
21884 CPP_CLOSE_PAREN)))
21885 decl = make_unbound_class_template (parser->scope,
21886 name, NULL_TREE,
21887 /*complain=*/tf_error);
21888 else
21889 decl = build_qualified_name (/*type=*/NULL_TREE,
21890 parser->scope, name,
21891 is_template);
21892 }
21893 parser->qualifying_scope = parser->scope;
21894 parser->object_scope = NULL_TREE;
21895 }
21896 else if (object_type)
21897 {
21898 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21899 OBJECT_TYPE is not a class. */
21900 if (CLASS_TYPE_P (object_type))
21901 /* If the OBJECT_TYPE is a template specialization, it may
21902 be instantiated during name lookup. In that case, errors
21903 may be issued. Even if we rollback the current tentative
21904 parse, those errors are valid. */
21905 decl = lookup_member (object_type,
21906 name,
21907 /*protect=*/0,
21908 tag_type != none_type,
21909 tf_warning_or_error);
21910 else
21911 decl = NULL_TREE;
21912
21913 if (!decl)
21914 /* Look it up in the enclosing context. */
21915 decl = lookup_name_real (name, tag_type != none_type,
21916 /*nonclass=*/0,
21917 /*block_p=*/true, is_namespace, 0);
21918 parser->object_scope = object_type;
21919 parser->qualifying_scope = NULL_TREE;
21920 }
21921 else
21922 {
21923 decl = lookup_name_real (name, tag_type != none_type,
21924 /*nonclass=*/0,
21925 /*block_p=*/true, is_namespace, 0);
21926 parser->qualifying_scope = NULL_TREE;
21927 parser->object_scope = NULL_TREE;
21928 }
21929
21930 /* If the lookup failed, let our caller know. */
21931 if (!decl || decl == error_mark_node)
21932 return error_mark_node;
21933
21934 /* Pull out the template from an injected-class-name (or multiple). */
21935 if (is_template)
21936 decl = maybe_get_template_decl_from_type_decl (decl);
21937
21938 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21939 if (TREE_CODE (decl) == TREE_LIST)
21940 {
21941 if (ambiguous_decls)
21942 *ambiguous_decls = decl;
21943 /* The error message we have to print is too complicated for
21944 cp_parser_error, so we incorporate its actions directly. */
21945 if (!cp_parser_simulate_error (parser))
21946 {
21947 error_at (name_location, "reference to %qD is ambiguous",
21948 name);
21949 print_candidates (decl);
21950 }
21951 return error_mark_node;
21952 }
21953
21954 gcc_assert (DECL_P (decl)
21955 || TREE_CODE (decl) == OVERLOAD
21956 || TREE_CODE (decl) == SCOPE_REF
21957 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21958 || BASELINK_P (decl));
21959
21960 /* If we have resolved the name of a member declaration, check to
21961 see if the declaration is accessible. When the name resolves to
21962 set of overloaded functions, accessibility is checked when
21963 overload resolution is done.
21964
21965 During an explicit instantiation, access is not checked at all,
21966 as per [temp.explicit]. */
21967 if (DECL_P (decl))
21968 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21969
21970 maybe_record_typedef_use (decl);
21971
21972 return decl;
21973 }
21974
21975 /* Like cp_parser_lookup_name, but for use in the typical case where
21976 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21977 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21978
21979 static tree
21980 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21981 {
21982 return cp_parser_lookup_name (parser, name,
21983 none_type,
21984 /*is_template=*/false,
21985 /*is_namespace=*/false,
21986 /*check_dependency=*/true,
21987 /*ambiguous_decls=*/NULL,
21988 location);
21989 }
21990
21991 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21992 the current context, return the TYPE_DECL. If TAG_NAME_P is
21993 true, the DECL indicates the class being defined in a class-head,
21994 or declared in an elaborated-type-specifier.
21995
21996 Otherwise, return DECL. */
21997
21998 static tree
21999 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22000 {
22001 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22002 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22003
22004 struct A {
22005 template <typename T> struct B;
22006 };
22007
22008 template <typename T> struct A::B {};
22009
22010 Similarly, in an elaborated-type-specifier:
22011
22012 namespace N { struct X{}; }
22013
22014 struct A {
22015 template <typename T> friend struct N::X;
22016 };
22017
22018 However, if the DECL refers to a class type, and we are in
22019 the scope of the class, then the name lookup automatically
22020 finds the TYPE_DECL created by build_self_reference rather
22021 than a TEMPLATE_DECL. For example, in:
22022
22023 template <class T> struct S {
22024 S s;
22025 };
22026
22027 there is no need to handle such case. */
22028
22029 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22030 return DECL_TEMPLATE_RESULT (decl);
22031
22032 return decl;
22033 }
22034
22035 /* If too many, or too few, template-parameter lists apply to the
22036 declarator, issue an error message. Returns TRUE if all went well,
22037 and FALSE otherwise. */
22038
22039 static bool
22040 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22041 cp_declarator *declarator,
22042 location_t declarator_location)
22043 {
22044 switch (declarator->kind)
22045 {
22046 case cdk_id:
22047 {
22048 unsigned num_templates = 0;
22049 tree scope = declarator->u.id.qualifying_scope;
22050
22051 if (scope)
22052 num_templates = num_template_headers_for_class (scope);
22053 else if (TREE_CODE (declarator->u.id.unqualified_name)
22054 == TEMPLATE_ID_EXPR)
22055 /* If the DECLARATOR has the form `X<y>' then it uses one
22056 additional level of template parameters. */
22057 ++num_templates;
22058
22059 return cp_parser_check_template_parameters
22060 (parser, num_templates, declarator_location, declarator);
22061 }
22062
22063 case cdk_function:
22064 case cdk_array:
22065 case cdk_pointer:
22066 case cdk_reference:
22067 case cdk_ptrmem:
22068 return (cp_parser_check_declarator_template_parameters
22069 (parser, declarator->declarator, declarator_location));
22070
22071 case cdk_error:
22072 return true;
22073
22074 default:
22075 gcc_unreachable ();
22076 }
22077 return false;
22078 }
22079
22080 /* NUM_TEMPLATES were used in the current declaration. If that is
22081 invalid, return FALSE and issue an error messages. Otherwise,
22082 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22083 declarator and we can print more accurate diagnostics. */
22084
22085 static bool
22086 cp_parser_check_template_parameters (cp_parser* parser,
22087 unsigned num_templates,
22088 location_t location,
22089 cp_declarator *declarator)
22090 {
22091 /* If there are the same number of template classes and parameter
22092 lists, that's OK. */
22093 if (parser->num_template_parameter_lists == num_templates)
22094 return true;
22095 /* If there are more, but only one more, then we are referring to a
22096 member template. That's OK too. */
22097 if (parser->num_template_parameter_lists == num_templates + 1)
22098 return true;
22099 /* If there are more template classes than parameter lists, we have
22100 something like:
22101
22102 template <class T> void S<T>::R<T>::f (); */
22103 if (parser->num_template_parameter_lists < num_templates)
22104 {
22105 if (declarator && !current_function_decl)
22106 error_at (location, "specializing member %<%T::%E%> "
22107 "requires %<template<>%> syntax",
22108 declarator->u.id.qualifying_scope,
22109 declarator->u.id.unqualified_name);
22110 else if (declarator)
22111 error_at (location, "invalid declaration of %<%T::%E%>",
22112 declarator->u.id.qualifying_scope,
22113 declarator->u.id.unqualified_name);
22114 else
22115 error_at (location, "too few template-parameter-lists");
22116 return false;
22117 }
22118 /* Otherwise, there are too many template parameter lists. We have
22119 something like:
22120
22121 template <class T> template <class U> void S::f(); */
22122 error_at (location, "too many template-parameter-lists");
22123 return false;
22124 }
22125
22126 /* Parse an optional `::' token indicating that the following name is
22127 from the global namespace. If so, PARSER->SCOPE is set to the
22128 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22129 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22130 Returns the new value of PARSER->SCOPE, if the `::' token is
22131 present, and NULL_TREE otherwise. */
22132
22133 static tree
22134 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22135 {
22136 cp_token *token;
22137
22138 /* Peek at the next token. */
22139 token = cp_lexer_peek_token (parser->lexer);
22140 /* If we're looking at a `::' token then we're starting from the
22141 global namespace, not our current location. */
22142 if (token->type == CPP_SCOPE)
22143 {
22144 /* Consume the `::' token. */
22145 cp_lexer_consume_token (parser->lexer);
22146 /* Set the SCOPE so that we know where to start the lookup. */
22147 parser->scope = global_namespace;
22148 parser->qualifying_scope = global_namespace;
22149 parser->object_scope = NULL_TREE;
22150
22151 return parser->scope;
22152 }
22153 else if (!current_scope_valid_p)
22154 {
22155 parser->scope = NULL_TREE;
22156 parser->qualifying_scope = NULL_TREE;
22157 parser->object_scope = NULL_TREE;
22158 }
22159
22160 return NULL_TREE;
22161 }
22162
22163 /* Returns TRUE if the upcoming token sequence is the start of a
22164 constructor declarator. If FRIEND_P is true, the declarator is
22165 preceded by the `friend' specifier. */
22166
22167 static bool
22168 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22169 {
22170 bool constructor_p;
22171 tree nested_name_specifier;
22172 cp_token *next_token;
22173
22174 /* The common case is that this is not a constructor declarator, so
22175 try to avoid doing lots of work if at all possible. It's not
22176 valid declare a constructor at function scope. */
22177 if (parser->in_function_body)
22178 return false;
22179 /* And only certain tokens can begin a constructor declarator. */
22180 next_token = cp_lexer_peek_token (parser->lexer);
22181 if (next_token->type != CPP_NAME
22182 && next_token->type != CPP_SCOPE
22183 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22184 && next_token->type != CPP_TEMPLATE_ID)
22185 return false;
22186
22187 /* Parse tentatively; we are going to roll back all of the tokens
22188 consumed here. */
22189 cp_parser_parse_tentatively (parser);
22190 /* Assume that we are looking at a constructor declarator. */
22191 constructor_p = true;
22192
22193 /* Look for the optional `::' operator. */
22194 cp_parser_global_scope_opt (parser,
22195 /*current_scope_valid_p=*/false);
22196 /* Look for the nested-name-specifier. */
22197 nested_name_specifier
22198 = (cp_parser_nested_name_specifier_opt (parser,
22199 /*typename_keyword_p=*/false,
22200 /*check_dependency_p=*/false,
22201 /*type_p=*/false,
22202 /*is_declaration=*/false));
22203 /* Outside of a class-specifier, there must be a
22204 nested-name-specifier. */
22205 if (!nested_name_specifier &&
22206 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
22207 || friend_p))
22208 constructor_p = false;
22209 else if (nested_name_specifier == error_mark_node)
22210 constructor_p = false;
22211
22212 /* If we have a class scope, this is easy; DR 147 says that S::S always
22213 names the constructor, and no other qualified name could. */
22214 if (constructor_p && nested_name_specifier
22215 && CLASS_TYPE_P (nested_name_specifier))
22216 {
22217 tree id = cp_parser_unqualified_id (parser,
22218 /*template_keyword_p=*/false,
22219 /*check_dependency_p=*/false,
22220 /*declarator_p=*/true,
22221 /*optional_p=*/false);
22222 if (is_overloaded_fn (id))
22223 id = DECL_NAME (get_first_fn (id));
22224 if (!constructor_name_p (id, nested_name_specifier))
22225 constructor_p = false;
22226 }
22227 /* If we still think that this might be a constructor-declarator,
22228 look for a class-name. */
22229 else if (constructor_p)
22230 {
22231 /* If we have:
22232
22233 template <typename T> struct S {
22234 S();
22235 };
22236
22237 we must recognize that the nested `S' names a class. */
22238 tree type_decl;
22239 type_decl = cp_parser_class_name (parser,
22240 /*typename_keyword_p=*/false,
22241 /*template_keyword_p=*/false,
22242 none_type,
22243 /*check_dependency_p=*/false,
22244 /*class_head_p=*/false,
22245 /*is_declaration=*/false);
22246 /* If there was no class-name, then this is not a constructor. */
22247 constructor_p = !cp_parser_error_occurred (parser);
22248
22249 /* If we're still considering a constructor, we have to see a `(',
22250 to begin the parameter-declaration-clause, followed by either a
22251 `)', an `...', or a decl-specifier. We need to check for a
22252 type-specifier to avoid being fooled into thinking that:
22253
22254 S (f) (int);
22255
22256 is a constructor. (It is actually a function named `f' that
22257 takes one parameter (of type `int') and returns a value of type
22258 `S'. */
22259 if (constructor_p
22260 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22261 constructor_p = false;
22262
22263 if (constructor_p
22264 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22265 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22266 /* A parameter declaration begins with a decl-specifier,
22267 which is either the "attribute" keyword, a storage class
22268 specifier, or (usually) a type-specifier. */
22269 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22270 {
22271 tree type;
22272 tree pushed_scope = NULL_TREE;
22273 unsigned saved_num_template_parameter_lists;
22274
22275 /* Names appearing in the type-specifier should be looked up
22276 in the scope of the class. */
22277 if (current_class_type)
22278 type = NULL_TREE;
22279 else
22280 {
22281 type = TREE_TYPE (type_decl);
22282 if (TREE_CODE (type) == TYPENAME_TYPE)
22283 {
22284 type = resolve_typename_type (type,
22285 /*only_current_p=*/false);
22286 if (TREE_CODE (type) == TYPENAME_TYPE)
22287 {
22288 cp_parser_abort_tentative_parse (parser);
22289 return false;
22290 }
22291 }
22292 pushed_scope = push_scope (type);
22293 }
22294
22295 /* Inside the constructor parameter list, surrounding
22296 template-parameter-lists do not apply. */
22297 saved_num_template_parameter_lists
22298 = parser->num_template_parameter_lists;
22299 parser->num_template_parameter_lists = 0;
22300
22301 /* Look for the type-specifier. */
22302 cp_parser_type_specifier (parser,
22303 CP_PARSER_FLAGS_NONE,
22304 /*decl_specs=*/NULL,
22305 /*is_declarator=*/true,
22306 /*declares_class_or_enum=*/NULL,
22307 /*is_cv_qualifier=*/NULL);
22308
22309 parser->num_template_parameter_lists
22310 = saved_num_template_parameter_lists;
22311
22312 /* Leave the scope of the class. */
22313 if (pushed_scope)
22314 pop_scope (pushed_scope);
22315
22316 constructor_p = !cp_parser_error_occurred (parser);
22317 }
22318 }
22319
22320 /* We did not really want to consume any tokens. */
22321 cp_parser_abort_tentative_parse (parser);
22322
22323 return constructor_p;
22324 }
22325
22326 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22327 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22328 they must be performed once we are in the scope of the function.
22329
22330 Returns the function defined. */
22331
22332 static tree
22333 cp_parser_function_definition_from_specifiers_and_declarator
22334 (cp_parser* parser,
22335 cp_decl_specifier_seq *decl_specifiers,
22336 tree attributes,
22337 const cp_declarator *declarator)
22338 {
22339 tree fn;
22340 bool success_p;
22341
22342 /* Begin the function-definition. */
22343 success_p = start_function (decl_specifiers, declarator, attributes);
22344
22345 /* The things we're about to see are not directly qualified by any
22346 template headers we've seen thus far. */
22347 reset_specialization ();
22348
22349 /* If there were names looked up in the decl-specifier-seq that we
22350 did not check, check them now. We must wait until we are in the
22351 scope of the function to perform the checks, since the function
22352 might be a friend. */
22353 perform_deferred_access_checks (tf_warning_or_error);
22354
22355 if (success_p)
22356 {
22357 cp_finalize_omp_declare_simd (parser, current_function_decl);
22358 parser->omp_declare_simd = NULL;
22359 }
22360
22361 if (!success_p)
22362 {
22363 /* Skip the entire function. */
22364 cp_parser_skip_to_end_of_block_or_statement (parser);
22365 fn = error_mark_node;
22366 }
22367 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22368 {
22369 /* Seen already, skip it. An error message has already been output. */
22370 cp_parser_skip_to_end_of_block_or_statement (parser);
22371 fn = current_function_decl;
22372 current_function_decl = NULL_TREE;
22373 /* If this is a function from a class, pop the nested class. */
22374 if (current_class_name)
22375 pop_nested_class ();
22376 }
22377 else
22378 {
22379 timevar_id_t tv;
22380 if (DECL_DECLARED_INLINE_P (current_function_decl))
22381 tv = TV_PARSE_INLINE;
22382 else
22383 tv = TV_PARSE_FUNC;
22384 timevar_push (tv);
22385 fn = cp_parser_function_definition_after_declarator (parser,
22386 /*inline_p=*/false);
22387 timevar_pop (tv);
22388 }
22389
22390 return fn;
22391 }
22392
22393 /* Parse the part of a function-definition that follows the
22394 declarator. INLINE_P is TRUE iff this function is an inline
22395 function defined within a class-specifier.
22396
22397 Returns the function defined. */
22398
22399 static tree
22400 cp_parser_function_definition_after_declarator (cp_parser* parser,
22401 bool inline_p)
22402 {
22403 tree fn;
22404 bool ctor_initializer_p = false;
22405 bool saved_in_unbraced_linkage_specification_p;
22406 bool saved_in_function_body;
22407 unsigned saved_num_template_parameter_lists;
22408 cp_token *token;
22409
22410 saved_in_function_body = parser->in_function_body;
22411 parser->in_function_body = true;
22412 /* If the next token is `return', then the code may be trying to
22413 make use of the "named return value" extension that G++ used to
22414 support. */
22415 token = cp_lexer_peek_token (parser->lexer);
22416 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22417 {
22418 /* Consume the `return' keyword. */
22419 cp_lexer_consume_token (parser->lexer);
22420 /* Look for the identifier that indicates what value is to be
22421 returned. */
22422 cp_parser_identifier (parser);
22423 /* Issue an error message. */
22424 error_at (token->location,
22425 "named return values are no longer supported");
22426 /* Skip tokens until we reach the start of the function body. */
22427 while (true)
22428 {
22429 cp_token *token = cp_lexer_peek_token (parser->lexer);
22430 if (token->type == CPP_OPEN_BRACE
22431 || token->type == CPP_EOF
22432 || token->type == CPP_PRAGMA_EOL)
22433 break;
22434 cp_lexer_consume_token (parser->lexer);
22435 }
22436 }
22437 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22438 anything declared inside `f'. */
22439 saved_in_unbraced_linkage_specification_p
22440 = parser->in_unbraced_linkage_specification_p;
22441 parser->in_unbraced_linkage_specification_p = false;
22442 /* Inside the function, surrounding template-parameter-lists do not
22443 apply. */
22444 saved_num_template_parameter_lists
22445 = parser->num_template_parameter_lists;
22446 parser->num_template_parameter_lists = 0;
22447
22448 start_lambda_scope (current_function_decl);
22449
22450 /* If the next token is `try', `__transaction_atomic', or
22451 `__transaction_relaxed`, then we are looking at either function-try-block
22452 or function-transaction-block. Note that all of these include the
22453 function-body. */
22454 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22455 ctor_initializer_p = cp_parser_function_transaction (parser,
22456 RID_TRANSACTION_ATOMIC);
22457 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22458 RID_TRANSACTION_RELAXED))
22459 ctor_initializer_p = cp_parser_function_transaction (parser,
22460 RID_TRANSACTION_RELAXED);
22461 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22462 ctor_initializer_p = cp_parser_function_try_block (parser);
22463 else
22464 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22465 (parser, /*in_function_try_block=*/false);
22466
22467 finish_lambda_scope ();
22468
22469 /* Finish the function. */
22470 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22471 (inline_p ? 2 : 0));
22472 /* Generate code for it, if necessary. */
22473 expand_or_defer_fn (fn);
22474 /* Restore the saved values. */
22475 parser->in_unbraced_linkage_specification_p
22476 = saved_in_unbraced_linkage_specification_p;
22477 parser->num_template_parameter_lists
22478 = saved_num_template_parameter_lists;
22479 parser->in_function_body = saved_in_function_body;
22480
22481 if (parser->fully_implicit_function_template_p)
22482 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22483
22484 return fn;
22485 }
22486
22487 /* Parse a template-declaration, assuming that the `export' (and
22488 `extern') keywords, if present, has already been scanned. MEMBER_P
22489 is as for cp_parser_template_declaration. */
22490
22491 static void
22492 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22493 {
22494 tree decl = NULL_TREE;
22495 vec<deferred_access_check, va_gc> *checks;
22496 tree parameter_list;
22497 bool friend_p = false;
22498 bool need_lang_pop;
22499 cp_token *token;
22500
22501 /* Look for the `template' keyword. */
22502 token = cp_lexer_peek_token (parser->lexer);
22503 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22504 return;
22505
22506 /* And the `<'. */
22507 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22508 return;
22509 if (at_class_scope_p () && current_function_decl)
22510 {
22511 /* 14.5.2.2 [temp.mem]
22512
22513 A local class shall not have member templates. */
22514 error_at (token->location,
22515 "invalid declaration of member template in local class");
22516 cp_parser_skip_to_end_of_block_or_statement (parser);
22517 return;
22518 }
22519 /* [temp]
22520
22521 A template ... shall not have C linkage. */
22522 if (current_lang_name == lang_name_c)
22523 {
22524 error_at (token->location, "template with C linkage");
22525 /* Give it C++ linkage to avoid confusing other parts of the
22526 front end. */
22527 push_lang_context (lang_name_cplusplus);
22528 need_lang_pop = true;
22529 }
22530 else
22531 need_lang_pop = false;
22532
22533 /* We cannot perform access checks on the template parameter
22534 declarations until we know what is being declared, just as we
22535 cannot check the decl-specifier list. */
22536 push_deferring_access_checks (dk_deferred);
22537
22538 /* If the next token is `>', then we have an invalid
22539 specialization. Rather than complain about an invalid template
22540 parameter, issue an error message here. */
22541 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
22542 {
22543 cp_parser_error (parser, "invalid explicit specialization");
22544 begin_specialization ();
22545 parameter_list = NULL_TREE;
22546 }
22547 else
22548 {
22549 /* Parse the template parameters. */
22550 parameter_list = cp_parser_template_parameter_list (parser);
22551 }
22552
22553 /* Get the deferred access checks from the parameter list. These
22554 will be checked once we know what is being declared, as for a
22555 member template the checks must be performed in the scope of the
22556 class containing the member. */
22557 checks = get_deferred_access_checks ();
22558
22559 /* Look for the `>'. */
22560 cp_parser_skip_to_end_of_template_parameter_list (parser);
22561 /* We just processed one more parameter list. */
22562 ++parser->num_template_parameter_lists;
22563 /* If the next token is `template', there are more template
22564 parameters. */
22565 if (cp_lexer_next_token_is_keyword (parser->lexer,
22566 RID_TEMPLATE))
22567 cp_parser_template_declaration_after_export (parser, member_p);
22568 else if (cxx_dialect >= cxx11
22569 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22570 decl = cp_parser_alias_declaration (parser);
22571 else
22572 {
22573 /* There are no access checks when parsing a template, as we do not
22574 know if a specialization will be a friend. */
22575 push_deferring_access_checks (dk_no_check);
22576 token = cp_lexer_peek_token (parser->lexer);
22577 decl = cp_parser_single_declaration (parser,
22578 checks,
22579 member_p,
22580 /*explicit_specialization_p=*/false,
22581 &friend_p);
22582 pop_deferring_access_checks ();
22583
22584 /* If this is a member template declaration, let the front
22585 end know. */
22586 if (member_p && !friend_p && decl)
22587 {
22588 if (TREE_CODE (decl) == TYPE_DECL)
22589 cp_parser_check_access_in_redeclaration (decl, token->location);
22590
22591 decl = finish_member_template_decl (decl);
22592 }
22593 else if (friend_p && decl
22594 && DECL_DECLARES_TYPE_P (decl))
22595 make_friend_class (current_class_type, TREE_TYPE (decl),
22596 /*complain=*/true);
22597 }
22598 /* We are done with the current parameter list. */
22599 --parser->num_template_parameter_lists;
22600
22601 pop_deferring_access_checks ();
22602
22603 /* Finish up. */
22604 finish_template_decl (parameter_list);
22605
22606 /* Check the template arguments for a literal operator template. */
22607 if (decl
22608 && DECL_DECLARES_FUNCTION_P (decl)
22609 && UDLIT_OPER_P (DECL_NAME (decl)))
22610 {
22611 bool ok = true;
22612 if (parameter_list == NULL_TREE)
22613 ok = false;
22614 else
22615 {
22616 int num_parms = TREE_VEC_LENGTH (parameter_list);
22617 if (num_parms == 1)
22618 {
22619 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
22620 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22621 if (TREE_TYPE (parm) != char_type_node
22622 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22623 ok = false;
22624 }
22625 else if (num_parms == 2 && cxx_dialect >= cxx1y)
22626 {
22627 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
22628 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
22629 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
22630 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22631 if (TREE_TYPE (parm) != TREE_TYPE (type)
22632 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22633 ok = false;
22634 }
22635 else
22636 ok = false;
22637 }
22638 if (!ok)
22639 error ("literal operator template %qD has invalid parameter list."
22640 " Expected non-type template argument pack <char...>"
22641 " or <typename CharT, CharT...>",
22642 decl);
22643 }
22644 /* Register member declarations. */
22645 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22646 finish_member_declaration (decl);
22647 /* For the erroneous case of a template with C linkage, we pushed an
22648 implicit C++ linkage scope; exit that scope now. */
22649 if (need_lang_pop)
22650 pop_lang_context ();
22651 /* If DECL is a function template, we must return to parse it later.
22652 (Even though there is no definition, there might be default
22653 arguments that need handling.) */
22654 if (member_p && decl
22655 && DECL_DECLARES_FUNCTION_P (decl))
22656 vec_safe_push (unparsed_funs_with_definitions, decl);
22657 }
22658
22659 /* Perform the deferred access checks from a template-parameter-list.
22660 CHECKS is a TREE_LIST of access checks, as returned by
22661 get_deferred_access_checks. */
22662
22663 static void
22664 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22665 {
22666 ++processing_template_parmlist;
22667 perform_access_checks (checks, tf_warning_or_error);
22668 --processing_template_parmlist;
22669 }
22670
22671 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22672 `function-definition' sequence that follows a template header.
22673 If MEMBER_P is true, this declaration appears in a class scope.
22674
22675 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22676 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22677
22678 static tree
22679 cp_parser_single_declaration (cp_parser* parser,
22680 vec<deferred_access_check, va_gc> *checks,
22681 bool member_p,
22682 bool explicit_specialization_p,
22683 bool* friend_p)
22684 {
22685 int declares_class_or_enum;
22686 tree decl = NULL_TREE;
22687 cp_decl_specifier_seq decl_specifiers;
22688 bool function_definition_p = false;
22689 cp_token *decl_spec_token_start;
22690
22691 /* This function is only used when processing a template
22692 declaration. */
22693 gcc_assert (innermost_scope_kind () == sk_template_parms
22694 || innermost_scope_kind () == sk_template_spec);
22695
22696 /* Defer access checks until we know what is being declared. */
22697 push_deferring_access_checks (dk_deferred);
22698
22699 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22700 alternative. */
22701 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22702 cp_parser_decl_specifier_seq (parser,
22703 CP_PARSER_FLAGS_OPTIONAL,
22704 &decl_specifiers,
22705 &declares_class_or_enum);
22706 if (friend_p)
22707 *friend_p = cp_parser_friend_p (&decl_specifiers);
22708
22709 /* There are no template typedefs. */
22710 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22711 {
22712 error_at (decl_spec_token_start->location,
22713 "template declaration of %<typedef%>");
22714 decl = error_mark_node;
22715 }
22716
22717 /* Gather up the access checks that occurred the
22718 decl-specifier-seq. */
22719 stop_deferring_access_checks ();
22720
22721 /* Check for the declaration of a template class. */
22722 if (declares_class_or_enum)
22723 {
22724 if (cp_parser_declares_only_class_p (parser))
22725 {
22726 decl = shadow_tag (&decl_specifiers);
22727
22728 /* In this case:
22729
22730 struct C {
22731 friend template <typename T> struct A<T>::B;
22732 };
22733
22734 A<T>::B will be represented by a TYPENAME_TYPE, and
22735 therefore not recognized by shadow_tag. */
22736 if (friend_p && *friend_p
22737 && !decl
22738 && decl_specifiers.type
22739 && TYPE_P (decl_specifiers.type))
22740 decl = decl_specifiers.type;
22741
22742 if (decl && decl != error_mark_node)
22743 decl = TYPE_NAME (decl);
22744 else
22745 decl = error_mark_node;
22746
22747 /* Perform access checks for template parameters. */
22748 cp_parser_perform_template_parameter_access_checks (checks);
22749 }
22750 }
22751
22752 /* Complain about missing 'typename' or other invalid type names. */
22753 if (!decl_specifiers.any_type_specifiers_p
22754 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22755 {
22756 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22757 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22758 the rest of this declaration. */
22759 decl = error_mark_node;
22760 goto out;
22761 }
22762
22763 /* If it's not a template class, try for a template function. If
22764 the next token is a `;', then this declaration does not declare
22765 anything. But, if there were errors in the decl-specifiers, then
22766 the error might well have come from an attempted class-specifier.
22767 In that case, there's no need to warn about a missing declarator. */
22768 if (!decl
22769 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22770 || decl_specifiers.type != error_mark_node))
22771 {
22772 decl = cp_parser_init_declarator (parser,
22773 &decl_specifiers,
22774 checks,
22775 /*function_definition_allowed_p=*/true,
22776 member_p,
22777 declares_class_or_enum,
22778 &function_definition_p,
22779 NULL);
22780
22781 /* 7.1.1-1 [dcl.stc]
22782
22783 A storage-class-specifier shall not be specified in an explicit
22784 specialization... */
22785 if (decl
22786 && explicit_specialization_p
22787 && decl_specifiers.storage_class != sc_none)
22788 {
22789 error_at (decl_spec_token_start->location,
22790 "explicit template specialization cannot have a storage class");
22791 decl = error_mark_node;
22792 }
22793
22794 if (decl && VAR_P (decl))
22795 check_template_variable (decl);
22796 }
22797
22798 /* Look for a trailing `;' after the declaration. */
22799 if (!function_definition_p
22800 && (decl == error_mark_node
22801 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22802 cp_parser_skip_to_end_of_block_or_statement (parser);
22803
22804 out:
22805 pop_deferring_access_checks ();
22806
22807 /* Clear any current qualification; whatever comes next is the start
22808 of something new. */
22809 parser->scope = NULL_TREE;
22810 parser->qualifying_scope = NULL_TREE;
22811 parser->object_scope = NULL_TREE;
22812
22813 return decl;
22814 }
22815
22816 /* Parse a cast-expression that is not the operand of a unary "&". */
22817
22818 static tree
22819 cp_parser_simple_cast_expression (cp_parser *parser)
22820 {
22821 return cp_parser_cast_expression (parser, /*address_p=*/false,
22822 /*cast_p=*/false, /*decltype*/false, NULL);
22823 }
22824
22825 /* Parse a functional cast to TYPE. Returns an expression
22826 representing the cast. */
22827
22828 static tree
22829 cp_parser_functional_cast (cp_parser* parser, tree type)
22830 {
22831 vec<tree, va_gc> *vec;
22832 tree expression_list;
22833 tree cast;
22834 bool nonconst_p;
22835
22836 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22837 {
22838 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22839 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22840 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22841 if (TREE_CODE (type) == TYPE_DECL)
22842 type = TREE_TYPE (type);
22843 return finish_compound_literal (type, expression_list,
22844 tf_warning_or_error);
22845 }
22846
22847
22848 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22849 /*cast_p=*/true,
22850 /*allow_expansion_p=*/true,
22851 /*non_constant_p=*/NULL);
22852 if (vec == NULL)
22853 expression_list = error_mark_node;
22854 else
22855 {
22856 expression_list = build_tree_list_vec (vec);
22857 release_tree_vector (vec);
22858 }
22859
22860 cast = build_functional_cast (type, expression_list,
22861 tf_warning_or_error);
22862 /* [expr.const]/1: In an integral constant expression "only type
22863 conversions to integral or enumeration type can be used". */
22864 if (TREE_CODE (type) == TYPE_DECL)
22865 type = TREE_TYPE (type);
22866 if (cast != error_mark_node
22867 && !cast_valid_in_integral_constant_expression_p (type)
22868 && cp_parser_non_integral_constant_expression (parser,
22869 NIC_CONSTRUCTOR))
22870 return error_mark_node;
22871 return cast;
22872 }
22873
22874 /* Save the tokens that make up the body of a member function defined
22875 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22876 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22877 specifiers applied to the declaration. Returns the FUNCTION_DECL
22878 for the member function. */
22879
22880 static tree
22881 cp_parser_save_member_function_body (cp_parser* parser,
22882 cp_decl_specifier_seq *decl_specifiers,
22883 cp_declarator *declarator,
22884 tree attributes)
22885 {
22886 cp_token *first;
22887 cp_token *last;
22888 tree fn;
22889
22890 /* Create the FUNCTION_DECL. */
22891 fn = grokmethod (decl_specifiers, declarator, attributes);
22892 cp_finalize_omp_declare_simd (parser, fn);
22893 /* If something went badly wrong, bail out now. */
22894 if (fn == error_mark_node)
22895 {
22896 /* If there's a function-body, skip it. */
22897 if (cp_parser_token_starts_function_definition_p
22898 (cp_lexer_peek_token (parser->lexer)))
22899 cp_parser_skip_to_end_of_block_or_statement (parser);
22900 return error_mark_node;
22901 }
22902
22903 /* Remember it, if there default args to post process. */
22904 cp_parser_save_default_args (parser, fn);
22905
22906 /* Save away the tokens that make up the body of the
22907 function. */
22908 first = parser->lexer->next_token;
22909 /* Handle function try blocks. */
22910 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22911 cp_lexer_consume_token (parser->lexer);
22912 /* We can have braced-init-list mem-initializers before the fn body. */
22913 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22914 {
22915 cp_lexer_consume_token (parser->lexer);
22916 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22917 {
22918 /* cache_group will stop after an un-nested { } pair, too. */
22919 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22920 break;
22921
22922 /* variadic mem-inits have ... after the ')'. */
22923 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22924 cp_lexer_consume_token (parser->lexer);
22925 }
22926 }
22927 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22928 /* Handle function try blocks. */
22929 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22930 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22931 last = parser->lexer->next_token;
22932
22933 /* Save away the inline definition; we will process it when the
22934 class is complete. */
22935 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22936 DECL_PENDING_INLINE_P (fn) = 1;
22937
22938 /* We need to know that this was defined in the class, so that
22939 friend templates are handled correctly. */
22940 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22941
22942 /* Add FN to the queue of functions to be parsed later. */
22943 vec_safe_push (unparsed_funs_with_definitions, fn);
22944
22945 return fn;
22946 }
22947
22948 /* Save the tokens that make up the in-class initializer for a non-static
22949 data member. Returns a DEFAULT_ARG. */
22950
22951 static tree
22952 cp_parser_save_nsdmi (cp_parser* parser)
22953 {
22954 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22955 }
22956
22957 /* Parse a template-argument-list, as well as the trailing ">" (but
22958 not the opening "<"). See cp_parser_template_argument_list for the
22959 return value. */
22960
22961 static tree
22962 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22963 {
22964 tree arguments;
22965 tree saved_scope;
22966 tree saved_qualifying_scope;
22967 tree saved_object_scope;
22968 bool saved_greater_than_is_operator_p;
22969 int saved_unevaluated_operand;
22970 int saved_inhibit_evaluation_warnings;
22971
22972 /* [temp.names]
22973
22974 When parsing a template-id, the first non-nested `>' is taken as
22975 the end of the template-argument-list rather than a greater-than
22976 operator. */
22977 saved_greater_than_is_operator_p
22978 = parser->greater_than_is_operator_p;
22979 parser->greater_than_is_operator_p = false;
22980 /* Parsing the argument list may modify SCOPE, so we save it
22981 here. */
22982 saved_scope = parser->scope;
22983 saved_qualifying_scope = parser->qualifying_scope;
22984 saved_object_scope = parser->object_scope;
22985 /* We need to evaluate the template arguments, even though this
22986 template-id may be nested within a "sizeof". */
22987 saved_unevaluated_operand = cp_unevaluated_operand;
22988 cp_unevaluated_operand = 0;
22989 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22990 c_inhibit_evaluation_warnings = 0;
22991 /* Parse the template-argument-list itself. */
22992 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22993 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22994 arguments = NULL_TREE;
22995 else
22996 arguments = cp_parser_template_argument_list (parser);
22997 /* Look for the `>' that ends the template-argument-list. If we find
22998 a '>>' instead, it's probably just a typo. */
22999 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23000 {
23001 if (cxx_dialect != cxx98)
23002 {
23003 /* In C++0x, a `>>' in a template argument list or cast
23004 expression is considered to be two separate `>'
23005 tokens. So, change the current token to a `>', but don't
23006 consume it: it will be consumed later when the outer
23007 template argument list (or cast expression) is parsed.
23008 Note that this replacement of `>' for `>>' is necessary
23009 even if we are parsing tentatively: in the tentative
23010 case, after calling
23011 cp_parser_enclosed_template_argument_list we will always
23012 throw away all of the template arguments and the first
23013 closing `>', either because the template argument list
23014 was erroneous or because we are replacing those tokens
23015 with a CPP_TEMPLATE_ID token. The second `>' (which will
23016 not have been thrown away) is needed either to close an
23017 outer template argument list or to complete a new-style
23018 cast. */
23019 cp_token *token = cp_lexer_peek_token (parser->lexer);
23020 token->type = CPP_GREATER;
23021 }
23022 else if (!saved_greater_than_is_operator_p)
23023 {
23024 /* If we're in a nested template argument list, the '>>' has
23025 to be a typo for '> >'. We emit the error message, but we
23026 continue parsing and we push a '>' as next token, so that
23027 the argument list will be parsed correctly. Note that the
23028 global source location is still on the token before the
23029 '>>', so we need to say explicitly where we want it. */
23030 cp_token *token = cp_lexer_peek_token (parser->lexer);
23031 error_at (token->location, "%<>>%> should be %<> >%> "
23032 "within a nested template argument list");
23033
23034 token->type = CPP_GREATER;
23035 }
23036 else
23037 {
23038 /* If this is not a nested template argument list, the '>>'
23039 is a typo for '>'. Emit an error message and continue.
23040 Same deal about the token location, but here we can get it
23041 right by consuming the '>>' before issuing the diagnostic. */
23042 cp_token *token = cp_lexer_consume_token (parser->lexer);
23043 error_at (token->location,
23044 "spurious %<>>%>, use %<>%> to terminate "
23045 "a template argument list");
23046 }
23047 }
23048 else
23049 cp_parser_skip_to_end_of_template_parameter_list (parser);
23050 /* The `>' token might be a greater-than operator again now. */
23051 parser->greater_than_is_operator_p
23052 = saved_greater_than_is_operator_p;
23053 /* Restore the SAVED_SCOPE. */
23054 parser->scope = saved_scope;
23055 parser->qualifying_scope = saved_qualifying_scope;
23056 parser->object_scope = saved_object_scope;
23057 cp_unevaluated_operand = saved_unevaluated_operand;
23058 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23059
23060 return arguments;
23061 }
23062
23063 /* MEMBER_FUNCTION is a member function, or a friend. If default
23064 arguments, or the body of the function have not yet been parsed,
23065 parse them now. */
23066
23067 static void
23068 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23069 {
23070 timevar_push (TV_PARSE_INMETH);
23071 /* If this member is a template, get the underlying
23072 FUNCTION_DECL. */
23073 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23074 member_function = DECL_TEMPLATE_RESULT (member_function);
23075
23076 /* There should not be any class definitions in progress at this
23077 point; the bodies of members are only parsed outside of all class
23078 definitions. */
23079 gcc_assert (parser->num_classes_being_defined == 0);
23080 /* While we're parsing the member functions we might encounter more
23081 classes. We want to handle them right away, but we don't want
23082 them getting mixed up with functions that are currently in the
23083 queue. */
23084 push_unparsed_function_queues (parser);
23085
23086 /* Make sure that any template parameters are in scope. */
23087 maybe_begin_member_template_processing (member_function);
23088
23089 /* If the body of the function has not yet been parsed, parse it
23090 now. */
23091 if (DECL_PENDING_INLINE_P (member_function))
23092 {
23093 tree function_scope;
23094 cp_token_cache *tokens;
23095
23096 /* The function is no longer pending; we are processing it. */
23097 tokens = DECL_PENDING_INLINE_INFO (member_function);
23098 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23099 DECL_PENDING_INLINE_P (member_function) = 0;
23100
23101 /* If this is a local class, enter the scope of the containing
23102 function. */
23103 function_scope = current_function_decl;
23104 if (function_scope)
23105 push_function_context ();
23106
23107 /* Push the body of the function onto the lexer stack. */
23108 cp_parser_push_lexer_for_tokens (parser, tokens);
23109
23110 /* Let the front end know that we going to be defining this
23111 function. */
23112 start_preparsed_function (member_function, NULL_TREE,
23113 SF_PRE_PARSED | SF_INCLASS_INLINE);
23114
23115 /* Don't do access checking if it is a templated function. */
23116 if (processing_template_decl)
23117 push_deferring_access_checks (dk_no_check);
23118
23119 /* #pragma omp declare reduction needs special parsing. */
23120 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23121 {
23122 parser->lexer->in_pragma = true;
23123 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23124 finish_function (0);
23125 cp_check_omp_declare_reduction (member_function);
23126 }
23127 else
23128 /* Now, parse the body of the function. */
23129 cp_parser_function_definition_after_declarator (parser,
23130 /*inline_p=*/true);
23131
23132 if (processing_template_decl)
23133 pop_deferring_access_checks ();
23134
23135 /* Leave the scope of the containing function. */
23136 if (function_scope)
23137 pop_function_context ();
23138 cp_parser_pop_lexer (parser);
23139 }
23140
23141 /* Remove any template parameters from the symbol table. */
23142 maybe_end_member_template_processing ();
23143
23144 /* Restore the queue. */
23145 pop_unparsed_function_queues (parser);
23146 timevar_pop (TV_PARSE_INMETH);
23147 }
23148
23149 /* If DECL contains any default args, remember it on the unparsed
23150 functions queue. */
23151
23152 static void
23153 cp_parser_save_default_args (cp_parser* parser, tree decl)
23154 {
23155 tree probe;
23156
23157 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23158 probe;
23159 probe = TREE_CHAIN (probe))
23160 if (TREE_PURPOSE (probe))
23161 {
23162 cp_default_arg_entry entry = {current_class_type, decl};
23163 vec_safe_push (unparsed_funs_with_default_args, entry);
23164 break;
23165 }
23166 }
23167
23168 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23169 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23170 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23171 from the parameter-type-list. */
23172
23173 static tree
23174 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23175 tree default_arg, tree parmtype)
23176 {
23177 cp_token_cache *tokens;
23178 tree parsed_arg;
23179 bool dummy;
23180
23181 if (default_arg == error_mark_node)
23182 return error_mark_node;
23183
23184 /* Push the saved tokens for the default argument onto the parser's
23185 lexer stack. */
23186 tokens = DEFARG_TOKENS (default_arg);
23187 cp_parser_push_lexer_for_tokens (parser, tokens);
23188
23189 start_lambda_scope (decl);
23190
23191 /* Parse the default argument. */
23192 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23193 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23194 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23195
23196 finish_lambda_scope ();
23197
23198 if (parsed_arg == error_mark_node)
23199 cp_parser_skip_to_end_of_statement (parser);
23200
23201 if (!processing_template_decl)
23202 {
23203 /* In a non-template class, check conversions now. In a template,
23204 we'll wait and instantiate these as needed. */
23205 if (TREE_CODE (decl) == PARM_DECL)
23206 parsed_arg = check_default_argument (parmtype, parsed_arg,
23207 tf_warning_or_error);
23208 else
23209 {
23210 int flags = LOOKUP_IMPLICIT;
23211 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
23212 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
23213 flags = LOOKUP_NORMAL;
23214 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
23215 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
23216 /* This represents the whole initialization. */
23217 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
23218 }
23219 }
23220
23221 /* If the token stream has not been completely used up, then
23222 there was extra junk after the end of the default
23223 argument. */
23224 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23225 {
23226 if (TREE_CODE (decl) == PARM_DECL)
23227 cp_parser_error (parser, "expected %<,%>");
23228 else
23229 cp_parser_error (parser, "expected %<;%>");
23230 }
23231
23232 /* Revert to the main lexer. */
23233 cp_parser_pop_lexer (parser);
23234
23235 return parsed_arg;
23236 }
23237
23238 /* FIELD is a non-static data member with an initializer which we saved for
23239 later; parse it now. */
23240
23241 static void
23242 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23243 {
23244 tree def;
23245
23246 push_unparsed_function_queues (parser);
23247 def = cp_parser_late_parse_one_default_arg (parser, field,
23248 DECL_INITIAL (field),
23249 NULL_TREE);
23250 pop_unparsed_function_queues (parser);
23251
23252 DECL_INITIAL (field) = def;
23253 }
23254
23255 /* FN is a FUNCTION_DECL which may contains a parameter with an
23256 unparsed DEFAULT_ARG. Parse the default args now. This function
23257 assumes that the current scope is the scope in which the default
23258 argument should be processed. */
23259
23260 static void
23261 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23262 {
23263 bool saved_local_variables_forbidden_p;
23264 tree parm, parmdecl;
23265
23266 /* While we're parsing the default args, we might (due to the
23267 statement expression extension) encounter more classes. We want
23268 to handle them right away, but we don't want them getting mixed
23269 up with default args that are currently in the queue. */
23270 push_unparsed_function_queues (parser);
23271
23272 /* Local variable names (and the `this' keyword) may not appear
23273 in a default argument. */
23274 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23275 parser->local_variables_forbidden_p = true;
23276
23277 push_defarg_context (fn);
23278
23279 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23280 parmdecl = DECL_ARGUMENTS (fn);
23281 parm && parm != void_list_node;
23282 parm = TREE_CHAIN (parm),
23283 parmdecl = DECL_CHAIN (parmdecl))
23284 {
23285 tree default_arg = TREE_PURPOSE (parm);
23286 tree parsed_arg;
23287 vec<tree, va_gc> *insts;
23288 tree copy;
23289 unsigned ix;
23290
23291 if (!default_arg)
23292 continue;
23293
23294 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23295 /* This can happen for a friend declaration for a function
23296 already declared with default arguments. */
23297 continue;
23298
23299 parsed_arg
23300 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23301 default_arg,
23302 TREE_VALUE (parm));
23303 if (parsed_arg == error_mark_node)
23304 {
23305 continue;
23306 }
23307
23308 TREE_PURPOSE (parm) = parsed_arg;
23309
23310 /* Update any instantiations we've already created. */
23311 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23312 vec_safe_iterate (insts, ix, &copy); ix++)
23313 TREE_PURPOSE (copy) = parsed_arg;
23314 }
23315
23316 pop_defarg_context ();
23317
23318 /* Make sure no default arg is missing. */
23319 check_default_args (fn);
23320
23321 /* Restore the state of local_variables_forbidden_p. */
23322 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23323
23324 /* Restore the queue. */
23325 pop_unparsed_function_queues (parser);
23326 }
23327
23328 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23329
23330 sizeof ... ( identifier )
23331
23332 where the 'sizeof' token has already been consumed. */
23333
23334 static tree
23335 cp_parser_sizeof_pack (cp_parser *parser)
23336 {
23337 /* Consume the `...'. */
23338 cp_lexer_consume_token (parser->lexer);
23339 maybe_warn_variadic_templates ();
23340
23341 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23342 if (paren)
23343 cp_lexer_consume_token (parser->lexer);
23344 else
23345 permerror (cp_lexer_peek_token (parser->lexer)->location,
23346 "%<sizeof...%> argument must be surrounded by parentheses");
23347
23348 cp_token *token = cp_lexer_peek_token (parser->lexer);
23349 tree name = cp_parser_identifier (parser);
23350 if (name == error_mark_node)
23351 return error_mark_node;
23352 /* The name is not qualified. */
23353 parser->scope = NULL_TREE;
23354 parser->qualifying_scope = NULL_TREE;
23355 parser->object_scope = NULL_TREE;
23356 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23357 if (expr == error_mark_node)
23358 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23359 token->location);
23360 if (TREE_CODE (expr) == TYPE_DECL)
23361 expr = TREE_TYPE (expr);
23362 else if (TREE_CODE (expr) == CONST_DECL)
23363 expr = DECL_INITIAL (expr);
23364 expr = make_pack_expansion (expr);
23365
23366 if (paren)
23367 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23368
23369 return expr;
23370 }
23371
23372 /* Parse the operand of `sizeof' (or a similar operator). Returns
23373 either a TYPE or an expression, depending on the form of the
23374 input. The KEYWORD indicates which kind of expression we have
23375 encountered. */
23376
23377 static tree
23378 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23379 {
23380 tree expr = NULL_TREE;
23381 const char *saved_message;
23382 char *tmp;
23383 bool saved_integral_constant_expression_p;
23384 bool saved_non_integral_constant_expression_p;
23385
23386 /* If it's a `...', then we are computing the length of a parameter
23387 pack. */
23388 if (keyword == RID_SIZEOF
23389 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23390 return cp_parser_sizeof_pack (parser);
23391
23392 /* Types cannot be defined in a `sizeof' expression. Save away the
23393 old message. */
23394 saved_message = parser->type_definition_forbidden_message;
23395 /* And create the new one. */
23396 tmp = concat ("types may not be defined in %<",
23397 IDENTIFIER_POINTER (ridpointers[keyword]),
23398 "%> expressions", NULL);
23399 parser->type_definition_forbidden_message = tmp;
23400
23401 /* The restrictions on constant-expressions do not apply inside
23402 sizeof expressions. */
23403 saved_integral_constant_expression_p
23404 = parser->integral_constant_expression_p;
23405 saved_non_integral_constant_expression_p
23406 = parser->non_integral_constant_expression_p;
23407 parser->integral_constant_expression_p = false;
23408
23409 /* Do not actually evaluate the expression. */
23410 ++cp_unevaluated_operand;
23411 ++c_inhibit_evaluation_warnings;
23412 /* If it's a `(', then we might be looking at the type-id
23413 construction. */
23414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23415 {
23416 tree type = NULL_TREE;
23417 bool compound_literal_p;
23418
23419 /* We can't be sure yet whether we're looking at a type-id or an
23420 expression. */
23421 cp_parser_parse_tentatively (parser);
23422 /* Consume the `('. */
23423 cp_lexer_consume_token (parser->lexer);
23424 /* Note: as a GNU Extension, compound literals are considered
23425 postfix-expressions as they are in C99, so they are valid
23426 arguments to sizeof. See comment in cp_parser_cast_expression
23427 for details. */
23428 cp_lexer_save_tokens (parser->lexer);
23429 /* Skip tokens until the next token is a closing parenthesis.
23430 If we find the closing `)', and the next token is a `{', then
23431 we are looking at a compound-literal. */
23432 compound_literal_p
23433 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23434 /*consume_paren=*/true)
23435 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23436 /* Roll back the tokens we skipped. */
23437 cp_lexer_rollback_tokens (parser->lexer);
23438 /* If we were looking at a compound-literal, simulate an error
23439 so that the call to cp_parser_parse_definitely below will
23440 fail. */
23441 if (compound_literal_p)
23442 cp_parser_simulate_error (parser);
23443 else
23444 {
23445 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23446 parser->in_type_id_in_expr_p = true;
23447 /* Look for the type-id. */
23448 type = cp_parser_type_id (parser);
23449 /* Look for the closing `)'. */
23450 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23451 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23452 }
23453
23454 /* If all went well, then we're done. */
23455 if (cp_parser_parse_definitely (parser))
23456 {
23457 cp_decl_specifier_seq decl_specs;
23458
23459 /* Build a trivial decl-specifier-seq. */
23460 clear_decl_specs (&decl_specs);
23461 decl_specs.type = type;
23462
23463 /* Call grokdeclarator to figure out what type this is. */
23464 expr = grokdeclarator (NULL,
23465 &decl_specs,
23466 TYPENAME,
23467 /*initialized=*/0,
23468 /*attrlist=*/NULL);
23469 }
23470 }
23471
23472 /* If the type-id production did not work out, then we must be
23473 looking at the unary-expression production. */
23474 if (!expr)
23475 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23476 /*cast_p=*/false, NULL);
23477
23478 /* Go back to evaluating expressions. */
23479 --cp_unevaluated_operand;
23480 --c_inhibit_evaluation_warnings;
23481
23482 /* Free the message we created. */
23483 free (tmp);
23484 /* And restore the old one. */
23485 parser->type_definition_forbidden_message = saved_message;
23486 parser->integral_constant_expression_p
23487 = saved_integral_constant_expression_p;
23488 parser->non_integral_constant_expression_p
23489 = saved_non_integral_constant_expression_p;
23490
23491 return expr;
23492 }
23493
23494 /* If the current declaration has no declarator, return true. */
23495
23496 static bool
23497 cp_parser_declares_only_class_p (cp_parser *parser)
23498 {
23499 /* If the next token is a `;' or a `,' then there is no
23500 declarator. */
23501 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23502 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23503 }
23504
23505 /* Update the DECL_SPECS to reflect the storage class indicated by
23506 KEYWORD. */
23507
23508 static void
23509 cp_parser_set_storage_class (cp_parser *parser,
23510 cp_decl_specifier_seq *decl_specs,
23511 enum rid keyword,
23512 cp_token *token)
23513 {
23514 cp_storage_class storage_class;
23515
23516 if (parser->in_unbraced_linkage_specification_p)
23517 {
23518 error_at (token->location, "invalid use of %qD in linkage specification",
23519 ridpointers[keyword]);
23520 return;
23521 }
23522 else if (decl_specs->storage_class != sc_none)
23523 {
23524 decl_specs->conflicting_specifiers_p = true;
23525 return;
23526 }
23527
23528 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
23529 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
23530 && decl_specs->gnu_thread_keyword_p)
23531 {
23532 pedwarn (decl_specs->locations[ds_thread], 0,
23533 "%<__thread%> before %qD", ridpointers[keyword]);
23534 }
23535
23536 switch (keyword)
23537 {
23538 case RID_AUTO:
23539 storage_class = sc_auto;
23540 break;
23541 case RID_REGISTER:
23542 storage_class = sc_register;
23543 break;
23544 case RID_STATIC:
23545 storage_class = sc_static;
23546 break;
23547 case RID_EXTERN:
23548 storage_class = sc_extern;
23549 break;
23550 case RID_MUTABLE:
23551 storage_class = sc_mutable;
23552 break;
23553 default:
23554 gcc_unreachable ();
23555 }
23556 decl_specs->storage_class = storage_class;
23557 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
23558
23559 /* A storage class specifier cannot be applied alongside a typedef
23560 specifier. If there is a typedef specifier present then set
23561 conflicting_specifiers_p which will trigger an error later
23562 on in grokdeclarator. */
23563 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
23564 decl_specs->conflicting_specifiers_p = true;
23565 }
23566
23567 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
23568 is true, the type is a class or enum definition. */
23569
23570 static void
23571 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
23572 tree type_spec,
23573 cp_token *token,
23574 bool type_definition_p)
23575 {
23576 decl_specs->any_specifiers_p = true;
23577
23578 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
23579 (with, for example, in "typedef int wchar_t;") we remember that
23580 this is what happened. In system headers, we ignore these
23581 declarations so that G++ can work with system headers that are not
23582 C++-safe. */
23583 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
23584 && !type_definition_p
23585 && (type_spec == boolean_type_node
23586 || type_spec == char16_type_node
23587 || type_spec == char32_type_node
23588 || type_spec == wchar_type_node)
23589 && (decl_specs->type
23590 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
23591 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
23592 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
23593 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
23594 {
23595 decl_specs->redefined_builtin_type = type_spec;
23596 set_and_check_decl_spec_loc (decl_specs,
23597 ds_redefined_builtin_type_spec,
23598 token);
23599 if (!decl_specs->type)
23600 {
23601 decl_specs->type = type_spec;
23602 decl_specs->type_definition_p = false;
23603 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
23604 }
23605 }
23606 else if (decl_specs->type)
23607 decl_specs->multiple_types_p = true;
23608 else
23609 {
23610 decl_specs->type = type_spec;
23611 decl_specs->type_definition_p = type_definition_p;
23612 decl_specs->redefined_builtin_type = NULL_TREE;
23613 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
23614 }
23615 }
23616
23617 /* True iff TOKEN is the GNU keyword __thread. */
23618
23619 static bool
23620 token_is__thread (cp_token *token)
23621 {
23622 gcc_assert (token->keyword == RID_THREAD);
23623 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
23624 }
23625
23626 /* Set the location for a declarator specifier and check if it is
23627 duplicated.
23628
23629 DECL_SPECS is the sequence of declarator specifiers onto which to
23630 set the location.
23631
23632 DS is the single declarator specifier to set which location is to
23633 be set onto the existing sequence of declarators.
23634
23635 LOCATION is the location for the declarator specifier to
23636 consider. */
23637
23638 static void
23639 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
23640 cp_decl_spec ds, cp_token *token)
23641 {
23642 gcc_assert (ds < ds_last);
23643
23644 if (decl_specs == NULL)
23645 return;
23646
23647 source_location location = token->location;
23648
23649 if (decl_specs->locations[ds] == 0)
23650 {
23651 decl_specs->locations[ds] = location;
23652 if (ds == ds_thread)
23653 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
23654 }
23655 else
23656 {
23657 if (ds == ds_long)
23658 {
23659 if (decl_specs->locations[ds_long_long] != 0)
23660 error_at (location,
23661 "%<long long long%> is too long for GCC");
23662 else
23663 {
23664 decl_specs->locations[ds_long_long] = location;
23665 pedwarn_cxx98 (location,
23666 OPT_Wlong_long,
23667 "ISO C++ 1998 does not support %<long long%>");
23668 }
23669 }
23670 else if (ds == ds_thread)
23671 {
23672 bool gnu = token_is__thread (token);
23673 if (gnu != decl_specs->gnu_thread_keyword_p)
23674 error_at (location,
23675 "both %<__thread%> and %<thread_local%> specified");
23676 else
23677 error_at (location, "duplicate %qD", token->u.value);
23678 }
23679 else
23680 {
23681 static const char *const decl_spec_names[] = {
23682 "signed",
23683 "unsigned",
23684 "short",
23685 "long",
23686 "const",
23687 "volatile",
23688 "restrict",
23689 "inline",
23690 "virtual",
23691 "explicit",
23692 "friend",
23693 "typedef",
23694 "using",
23695 "constexpr",
23696 "__complex"
23697 };
23698 error_at (location,
23699 "duplicate %qs", decl_spec_names[ds]);
23700 }
23701 }
23702 }
23703
23704 /* Return true iff the declarator specifier DS is present in the
23705 sequence of declarator specifiers DECL_SPECS. */
23706
23707 bool
23708 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23709 cp_decl_spec ds)
23710 {
23711 gcc_assert (ds < ds_last);
23712
23713 if (decl_specs == NULL)
23714 return false;
23715
23716 return decl_specs->locations[ds] != 0;
23717 }
23718
23719 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23720 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23721
23722 static bool
23723 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23724 {
23725 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23726 }
23727
23728 /* Issue an error message indicating that TOKEN_DESC was expected.
23729 If KEYWORD is true, it indicated this function is called by
23730 cp_parser_require_keword and the required token can only be
23731 a indicated keyword. */
23732
23733 static void
23734 cp_parser_required_error (cp_parser *parser,
23735 required_token token_desc,
23736 bool keyword)
23737 {
23738 switch (token_desc)
23739 {
23740 case RT_NEW:
23741 cp_parser_error (parser, "expected %<new%>");
23742 return;
23743 case RT_DELETE:
23744 cp_parser_error (parser, "expected %<delete%>");
23745 return;
23746 case RT_RETURN:
23747 cp_parser_error (parser, "expected %<return%>");
23748 return;
23749 case RT_WHILE:
23750 cp_parser_error (parser, "expected %<while%>");
23751 return;
23752 case RT_EXTERN:
23753 cp_parser_error (parser, "expected %<extern%>");
23754 return;
23755 case RT_STATIC_ASSERT:
23756 cp_parser_error (parser, "expected %<static_assert%>");
23757 return;
23758 case RT_DECLTYPE:
23759 cp_parser_error (parser, "expected %<decltype%>");
23760 return;
23761 case RT_OPERATOR:
23762 cp_parser_error (parser, "expected %<operator%>");
23763 return;
23764 case RT_CLASS:
23765 cp_parser_error (parser, "expected %<class%>");
23766 return;
23767 case RT_TEMPLATE:
23768 cp_parser_error (parser, "expected %<template%>");
23769 return;
23770 case RT_NAMESPACE:
23771 cp_parser_error (parser, "expected %<namespace%>");
23772 return;
23773 case RT_USING:
23774 cp_parser_error (parser, "expected %<using%>");
23775 return;
23776 case RT_ASM:
23777 cp_parser_error (parser, "expected %<asm%>");
23778 return;
23779 case RT_TRY:
23780 cp_parser_error (parser, "expected %<try%>");
23781 return;
23782 case RT_CATCH:
23783 cp_parser_error (parser, "expected %<catch%>");
23784 return;
23785 case RT_THROW:
23786 cp_parser_error (parser, "expected %<throw%>");
23787 return;
23788 case RT_LABEL:
23789 cp_parser_error (parser, "expected %<__label__%>");
23790 return;
23791 case RT_AT_TRY:
23792 cp_parser_error (parser, "expected %<@try%>");
23793 return;
23794 case RT_AT_SYNCHRONIZED:
23795 cp_parser_error (parser, "expected %<@synchronized%>");
23796 return;
23797 case RT_AT_THROW:
23798 cp_parser_error (parser, "expected %<@throw%>");
23799 return;
23800 case RT_TRANSACTION_ATOMIC:
23801 cp_parser_error (parser, "expected %<__transaction_atomic%>");
23802 return;
23803 case RT_TRANSACTION_RELAXED:
23804 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23805 return;
23806 default:
23807 break;
23808 }
23809 if (!keyword)
23810 {
23811 switch (token_desc)
23812 {
23813 case RT_SEMICOLON:
23814 cp_parser_error (parser, "expected %<;%>");
23815 return;
23816 case RT_OPEN_PAREN:
23817 cp_parser_error (parser, "expected %<(%>");
23818 return;
23819 case RT_CLOSE_BRACE:
23820 cp_parser_error (parser, "expected %<}%>");
23821 return;
23822 case RT_OPEN_BRACE:
23823 cp_parser_error (parser, "expected %<{%>");
23824 return;
23825 case RT_CLOSE_SQUARE:
23826 cp_parser_error (parser, "expected %<]%>");
23827 return;
23828 case RT_OPEN_SQUARE:
23829 cp_parser_error (parser, "expected %<[%>");
23830 return;
23831 case RT_COMMA:
23832 cp_parser_error (parser, "expected %<,%>");
23833 return;
23834 case RT_SCOPE:
23835 cp_parser_error (parser, "expected %<::%>");
23836 return;
23837 case RT_LESS:
23838 cp_parser_error (parser, "expected %<<%>");
23839 return;
23840 case RT_GREATER:
23841 cp_parser_error (parser, "expected %<>%>");
23842 return;
23843 case RT_EQ:
23844 cp_parser_error (parser, "expected %<=%>");
23845 return;
23846 case RT_ELLIPSIS:
23847 cp_parser_error (parser, "expected %<...%>");
23848 return;
23849 case RT_MULT:
23850 cp_parser_error (parser, "expected %<*%>");
23851 return;
23852 case RT_COMPL:
23853 cp_parser_error (parser, "expected %<~%>");
23854 return;
23855 case RT_COLON:
23856 cp_parser_error (parser, "expected %<:%>");
23857 return;
23858 case RT_COLON_SCOPE:
23859 cp_parser_error (parser, "expected %<:%> or %<::%>");
23860 return;
23861 case RT_CLOSE_PAREN:
23862 cp_parser_error (parser, "expected %<)%>");
23863 return;
23864 case RT_COMMA_CLOSE_PAREN:
23865 cp_parser_error (parser, "expected %<,%> or %<)%>");
23866 return;
23867 case RT_PRAGMA_EOL:
23868 cp_parser_error (parser, "expected end of line");
23869 return;
23870 case RT_NAME:
23871 cp_parser_error (parser, "expected identifier");
23872 return;
23873 case RT_SELECT:
23874 cp_parser_error (parser, "expected selection-statement");
23875 return;
23876 case RT_INTERATION:
23877 cp_parser_error (parser, "expected iteration-statement");
23878 return;
23879 case RT_JUMP:
23880 cp_parser_error (parser, "expected jump-statement");
23881 return;
23882 case RT_CLASS_KEY:
23883 cp_parser_error (parser, "expected class-key");
23884 return;
23885 case RT_CLASS_TYPENAME_TEMPLATE:
23886 cp_parser_error (parser,
23887 "expected %<class%>, %<typename%>, or %<template%>");
23888 return;
23889 default:
23890 gcc_unreachable ();
23891 }
23892 }
23893 else
23894 gcc_unreachable ();
23895 }
23896
23897
23898
23899 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23900 issue an error message indicating that TOKEN_DESC was expected.
23901
23902 Returns the token consumed, if the token had the appropriate type.
23903 Otherwise, returns NULL. */
23904
23905 static cp_token *
23906 cp_parser_require (cp_parser* parser,
23907 enum cpp_ttype type,
23908 required_token token_desc)
23909 {
23910 if (cp_lexer_next_token_is (parser->lexer, type))
23911 return cp_lexer_consume_token (parser->lexer);
23912 else
23913 {
23914 /* Output the MESSAGE -- unless we're parsing tentatively. */
23915 if (!cp_parser_simulate_error (parser))
23916 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23917 return NULL;
23918 }
23919 }
23920
23921 /* An error message is produced if the next token is not '>'.
23922 All further tokens are skipped until the desired token is
23923 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23924
23925 static void
23926 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23927 {
23928 /* Current level of '< ... >'. */
23929 unsigned level = 0;
23930 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23931 unsigned nesting_depth = 0;
23932
23933 /* Are we ready, yet? If not, issue error message. */
23934 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23935 return;
23936
23937 /* Skip tokens until the desired token is found. */
23938 while (true)
23939 {
23940 /* Peek at the next token. */
23941 switch (cp_lexer_peek_token (parser->lexer)->type)
23942 {
23943 case CPP_LESS:
23944 if (!nesting_depth)
23945 ++level;
23946 break;
23947
23948 case CPP_RSHIFT:
23949 if (cxx_dialect == cxx98)
23950 /* C++0x views the `>>' operator as two `>' tokens, but
23951 C++98 does not. */
23952 break;
23953 else if (!nesting_depth && level-- == 0)
23954 {
23955 /* We've hit a `>>' where the first `>' closes the
23956 template argument list, and the second `>' is
23957 spurious. Just consume the `>>' and stop; we've
23958 already produced at least one error. */
23959 cp_lexer_consume_token (parser->lexer);
23960 return;
23961 }
23962 /* Fall through for C++0x, so we handle the second `>' in
23963 the `>>'. */
23964
23965 case CPP_GREATER:
23966 if (!nesting_depth && level-- == 0)
23967 {
23968 /* We've reached the token we want, consume it and stop. */
23969 cp_lexer_consume_token (parser->lexer);
23970 return;
23971 }
23972 break;
23973
23974 case CPP_OPEN_PAREN:
23975 case CPP_OPEN_SQUARE:
23976 ++nesting_depth;
23977 break;
23978
23979 case CPP_CLOSE_PAREN:
23980 case CPP_CLOSE_SQUARE:
23981 if (nesting_depth-- == 0)
23982 return;
23983 break;
23984
23985 case CPP_EOF:
23986 case CPP_PRAGMA_EOL:
23987 case CPP_SEMICOLON:
23988 case CPP_OPEN_BRACE:
23989 case CPP_CLOSE_BRACE:
23990 /* The '>' was probably forgotten, don't look further. */
23991 return;
23992
23993 default:
23994 break;
23995 }
23996
23997 /* Consume this token. */
23998 cp_lexer_consume_token (parser->lexer);
23999 }
24000 }
24001
24002 /* If the next token is the indicated keyword, consume it. Otherwise,
24003 issue an error message indicating that TOKEN_DESC was expected.
24004
24005 Returns the token consumed, if the token had the appropriate type.
24006 Otherwise, returns NULL. */
24007
24008 static cp_token *
24009 cp_parser_require_keyword (cp_parser* parser,
24010 enum rid keyword,
24011 required_token token_desc)
24012 {
24013 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24014
24015 if (token && token->keyword != keyword)
24016 {
24017 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24018 return NULL;
24019 }
24020
24021 return token;
24022 }
24023
24024 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24025 function-definition. */
24026
24027 static bool
24028 cp_parser_token_starts_function_definition_p (cp_token* token)
24029 {
24030 return (/* An ordinary function-body begins with an `{'. */
24031 token->type == CPP_OPEN_BRACE
24032 /* A ctor-initializer begins with a `:'. */
24033 || token->type == CPP_COLON
24034 /* A function-try-block begins with `try'. */
24035 || token->keyword == RID_TRY
24036 /* A function-transaction-block begins with `__transaction_atomic'
24037 or `__transaction_relaxed'. */
24038 || token->keyword == RID_TRANSACTION_ATOMIC
24039 || token->keyword == RID_TRANSACTION_RELAXED
24040 /* The named return value extension begins with `return'. */
24041 || token->keyword == RID_RETURN);
24042 }
24043
24044 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24045 definition. */
24046
24047 static bool
24048 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24049 {
24050 cp_token *token;
24051
24052 token = cp_lexer_peek_token (parser->lexer);
24053 return (token->type == CPP_OPEN_BRACE
24054 || (token->type == CPP_COLON
24055 && !parser->colon_doesnt_start_class_def_p));
24056 }
24057
24058 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24059 C++0x) ending a template-argument. */
24060
24061 static bool
24062 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24063 {
24064 cp_token *token;
24065
24066 token = cp_lexer_peek_token (parser->lexer);
24067 return (token->type == CPP_COMMA
24068 || token->type == CPP_GREATER
24069 || token->type == CPP_ELLIPSIS
24070 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24071 }
24072
24073 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24074 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24075
24076 static bool
24077 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24078 size_t n)
24079 {
24080 cp_token *token;
24081
24082 token = cp_lexer_peek_nth_token (parser->lexer, n);
24083 if (token->type == CPP_LESS)
24084 return true;
24085 /* Check for the sequence `<::' in the original code. It would be lexed as
24086 `[:', where `[' is a digraph, and there is no whitespace before
24087 `:'. */
24088 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24089 {
24090 cp_token *token2;
24091 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24092 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24093 return true;
24094 }
24095 return false;
24096 }
24097
24098 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24099 or none_type otherwise. */
24100
24101 static enum tag_types
24102 cp_parser_token_is_class_key (cp_token* token)
24103 {
24104 switch (token->keyword)
24105 {
24106 case RID_CLASS:
24107 return class_type;
24108 case RID_STRUCT:
24109 return record_type;
24110 case RID_UNION:
24111 return union_type;
24112
24113 default:
24114 return none_type;
24115 }
24116 }
24117
24118 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24119
24120 static void
24121 cp_parser_check_class_key (enum tag_types class_key, tree type)
24122 {
24123 if (type == error_mark_node)
24124 return;
24125 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24126 {
24127 if (permerror (input_location, "%qs tag used in naming %q#T",
24128 class_key == union_type ? "union"
24129 : class_key == record_type ? "struct" : "class",
24130 type))
24131 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24132 "%q#T was previously declared here", type);
24133 }
24134 }
24135
24136 /* Issue an error message if DECL is redeclared with different
24137 access than its original declaration [class.access.spec/3].
24138 This applies to nested classes and nested class templates.
24139 [class.mem/1]. */
24140
24141 static void
24142 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24143 {
24144 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24145 return;
24146
24147 if ((TREE_PRIVATE (decl)
24148 != (current_access_specifier == access_private_node))
24149 || (TREE_PROTECTED (decl)
24150 != (current_access_specifier == access_protected_node)))
24151 error_at (location, "%qD redeclared with different access", decl);
24152 }
24153
24154 /* Look for the `template' keyword, as a syntactic disambiguator.
24155 Return TRUE iff it is present, in which case it will be
24156 consumed. */
24157
24158 static bool
24159 cp_parser_optional_template_keyword (cp_parser *parser)
24160 {
24161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24162 {
24163 /* In C++98 the `template' keyword can only be used within templates;
24164 outside templates the parser can always figure out what is a
24165 template and what is not. In C++11, per the resolution of DR 468,
24166 `template' is allowed in cases where it is not strictly necessary. */
24167 if (!processing_template_decl
24168 && pedantic && cxx_dialect == cxx98)
24169 {
24170 cp_token *token = cp_lexer_peek_token (parser->lexer);
24171 pedwarn (token->location, OPT_Wpedantic,
24172 "in C++98 %<template%> (as a disambiguator) is only "
24173 "allowed within templates");
24174 /* If this part of the token stream is rescanned, the same
24175 error message would be generated. So, we purge the token
24176 from the stream. */
24177 cp_lexer_purge_token (parser->lexer);
24178 return false;
24179 }
24180 else
24181 {
24182 /* Consume the `template' keyword. */
24183 cp_lexer_consume_token (parser->lexer);
24184 return true;
24185 }
24186 }
24187 return false;
24188 }
24189
24190 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24191 set PARSER->SCOPE, and perform other related actions. */
24192
24193 static void
24194 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24195 {
24196 int i;
24197 struct tree_check *check_value;
24198 deferred_access_check *chk;
24199 vec<deferred_access_check, va_gc> *checks;
24200
24201 /* Get the stored value. */
24202 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24203 /* Perform any access checks that were deferred. */
24204 checks = check_value->checks;
24205 if (checks)
24206 {
24207 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24208 perform_or_defer_access_check (chk->binfo,
24209 chk->decl,
24210 chk->diag_decl, tf_warning_or_error);
24211 }
24212 /* Set the scope from the stored value. */
24213 parser->scope = check_value->value;
24214 parser->qualifying_scope = check_value->qualifying_scope;
24215 parser->object_scope = NULL_TREE;
24216 }
24217
24218 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24219 encounter the end of a block before what we were looking for. */
24220
24221 static bool
24222 cp_parser_cache_group (cp_parser *parser,
24223 enum cpp_ttype end,
24224 unsigned depth)
24225 {
24226 while (true)
24227 {
24228 cp_token *token = cp_lexer_peek_token (parser->lexer);
24229
24230 /* Abort a parenthesized expression if we encounter a semicolon. */
24231 if ((end == CPP_CLOSE_PAREN || depth == 0)
24232 && token->type == CPP_SEMICOLON)
24233 return true;
24234 /* If we've reached the end of the file, stop. */
24235 if (token->type == CPP_EOF
24236 || (end != CPP_PRAGMA_EOL
24237 && token->type == CPP_PRAGMA_EOL))
24238 return true;
24239 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24240 /* We've hit the end of an enclosing block, so there's been some
24241 kind of syntax error. */
24242 return true;
24243
24244 /* Consume the token. */
24245 cp_lexer_consume_token (parser->lexer);
24246 /* See if it starts a new group. */
24247 if (token->type == CPP_OPEN_BRACE)
24248 {
24249 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24250 /* In theory this should probably check end == '}', but
24251 cp_parser_save_member_function_body needs it to exit
24252 after either '}' or ')' when called with ')'. */
24253 if (depth == 0)
24254 return false;
24255 }
24256 else if (token->type == CPP_OPEN_PAREN)
24257 {
24258 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24259 if (depth == 0 && end == CPP_CLOSE_PAREN)
24260 return false;
24261 }
24262 else if (token->type == CPP_PRAGMA)
24263 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24264 else if (token->type == end)
24265 return false;
24266 }
24267 }
24268
24269 /* Like above, for caching a default argument or NSDMI. Both of these are
24270 terminated by a non-nested comma, but it can be unclear whether or not a
24271 comma is nested in a template argument list unless we do more parsing.
24272 In order to handle this ambiguity, when we encounter a ',' after a '<'
24273 we try to parse what follows as a parameter-declaration-list (in the
24274 case of a default argument) or a member-declarator (in the case of an
24275 NSDMI). If that succeeds, then we stop caching. */
24276
24277 static tree
24278 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24279 {
24280 unsigned depth = 0;
24281 int maybe_template_id = 0;
24282 cp_token *first_token;
24283 cp_token *token;
24284 tree default_argument;
24285
24286 /* Add tokens until we have processed the entire default
24287 argument. We add the range [first_token, token). */
24288 first_token = cp_lexer_peek_token (parser->lexer);
24289 if (first_token->type == CPP_OPEN_BRACE)
24290 {
24291 /* For list-initialization, this is straightforward. */
24292 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24293 token = cp_lexer_peek_token (parser->lexer);
24294 }
24295 else while (true)
24296 {
24297 bool done = false;
24298
24299 /* Peek at the next token. */
24300 token = cp_lexer_peek_token (parser->lexer);
24301 /* What we do depends on what token we have. */
24302 switch (token->type)
24303 {
24304 /* In valid code, a default argument must be
24305 immediately followed by a `,' `)', or `...'. */
24306 case CPP_COMMA:
24307 if (depth == 0 && maybe_template_id)
24308 {
24309 /* If we've seen a '<', we might be in a
24310 template-argument-list. Until Core issue 325 is
24311 resolved, we don't know how this situation ought
24312 to be handled, so try to DTRT. We check whether
24313 what comes after the comma is a valid parameter
24314 declaration list. If it is, then the comma ends
24315 the default argument; otherwise the default
24316 argument continues. */
24317 bool error = false;
24318
24319 /* Set ITALP so cp_parser_parameter_declaration_list
24320 doesn't decide to commit to this parse. */
24321 bool saved_italp = parser->in_template_argument_list_p;
24322 parser->in_template_argument_list_p = true;
24323
24324 cp_parser_parse_tentatively (parser);
24325 cp_lexer_consume_token (parser->lexer);
24326
24327 if (nsdmi)
24328 {
24329 int ctor_dtor_or_conv_p;
24330 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24331 &ctor_dtor_or_conv_p,
24332 /*parenthesized_p=*/NULL,
24333 /*member_p=*/true);
24334 }
24335 else
24336 {
24337 begin_scope (sk_function_parms, NULL_TREE);
24338 cp_parser_parameter_declaration_list (parser, &error);
24339 pop_bindings_and_leave_scope ();
24340 }
24341 if (!cp_parser_error_occurred (parser) && !error)
24342 done = true;
24343 cp_parser_abort_tentative_parse (parser);
24344
24345 parser->in_template_argument_list_p = saved_italp;
24346 break;
24347 }
24348 case CPP_CLOSE_PAREN:
24349 case CPP_ELLIPSIS:
24350 /* If we run into a non-nested `;', `}', or `]',
24351 then the code is invalid -- but the default
24352 argument is certainly over. */
24353 case CPP_SEMICOLON:
24354 case CPP_CLOSE_BRACE:
24355 case CPP_CLOSE_SQUARE:
24356 if (depth == 0
24357 /* Handle correctly int n = sizeof ... ( p ); */
24358 && !(nsdmi && token->type == CPP_ELLIPSIS))
24359 done = true;
24360 /* Update DEPTH, if necessary. */
24361 else if (token->type == CPP_CLOSE_PAREN
24362 || token->type == CPP_CLOSE_BRACE
24363 || token->type == CPP_CLOSE_SQUARE)
24364 --depth;
24365 break;
24366
24367 case CPP_OPEN_PAREN:
24368 case CPP_OPEN_SQUARE:
24369 case CPP_OPEN_BRACE:
24370 ++depth;
24371 break;
24372
24373 case CPP_LESS:
24374 if (depth == 0)
24375 /* This might be the comparison operator, or it might
24376 start a template argument list. */
24377 ++maybe_template_id;
24378 break;
24379
24380 case CPP_RSHIFT:
24381 if (cxx_dialect == cxx98)
24382 break;
24383 /* Fall through for C++0x, which treats the `>>'
24384 operator like two `>' tokens in certain
24385 cases. */
24386
24387 case CPP_GREATER:
24388 if (depth == 0)
24389 {
24390 /* This might be an operator, or it might close a
24391 template argument list. But if a previous '<'
24392 started a template argument list, this will have
24393 closed it, so we can't be in one anymore. */
24394 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24395 if (maybe_template_id < 0)
24396 maybe_template_id = 0;
24397 }
24398 break;
24399
24400 /* If we run out of tokens, issue an error message. */
24401 case CPP_EOF:
24402 case CPP_PRAGMA_EOL:
24403 error_at (token->location, "file ends in default argument");
24404 done = true;
24405 break;
24406
24407 case CPP_NAME:
24408 case CPP_SCOPE:
24409 /* In these cases, we should look for template-ids.
24410 For example, if the default argument is
24411 `X<int, double>()', we need to do name lookup to
24412 figure out whether or not `X' is a template; if
24413 so, the `,' does not end the default argument.
24414
24415 That is not yet done. */
24416 break;
24417
24418 default:
24419 break;
24420 }
24421
24422 /* If we've reached the end, stop. */
24423 if (done)
24424 break;
24425
24426 /* Add the token to the token block. */
24427 token = cp_lexer_consume_token (parser->lexer);
24428 }
24429
24430 /* Create a DEFAULT_ARG to represent the unparsed default
24431 argument. */
24432 default_argument = make_node (DEFAULT_ARG);
24433 DEFARG_TOKENS (default_argument)
24434 = cp_token_cache_new (first_token, token);
24435 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24436
24437 return default_argument;
24438 }
24439
24440 /* Begin parsing tentatively. We always save tokens while parsing
24441 tentatively so that if the tentative parsing fails we can restore the
24442 tokens. */
24443
24444 static void
24445 cp_parser_parse_tentatively (cp_parser* parser)
24446 {
24447 /* Enter a new parsing context. */
24448 parser->context = cp_parser_context_new (parser->context);
24449 /* Begin saving tokens. */
24450 cp_lexer_save_tokens (parser->lexer);
24451 /* In order to avoid repetitive access control error messages,
24452 access checks are queued up until we are no longer parsing
24453 tentatively. */
24454 push_deferring_access_checks (dk_deferred);
24455 }
24456
24457 /* Commit to the currently active tentative parse. */
24458
24459 static void
24460 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24461 {
24462 cp_parser_context *context;
24463 cp_lexer *lexer;
24464
24465 /* Mark all of the levels as committed. */
24466 lexer = parser->lexer;
24467 for (context = parser->context; context->next; context = context->next)
24468 {
24469 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24470 break;
24471 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24472 while (!cp_lexer_saving_tokens (lexer))
24473 lexer = lexer->next;
24474 cp_lexer_commit_tokens (lexer);
24475 }
24476 }
24477
24478 /* Commit to the topmost currently active tentative parse.
24479
24480 Note that this function shouldn't be called when there are
24481 irreversible side-effects while in a tentative state. For
24482 example, we shouldn't create a permanent entry in the symbol
24483 table, or issue an error message that might not apply if the
24484 tentative parse is aborted. */
24485
24486 static void
24487 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24488 {
24489 cp_parser_context *context = parser->context;
24490 cp_lexer *lexer = parser->lexer;
24491
24492 if (context)
24493 {
24494 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24495 return;
24496 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24497
24498 while (!cp_lexer_saving_tokens (lexer))
24499 lexer = lexer->next;
24500 cp_lexer_commit_tokens (lexer);
24501 }
24502 }
24503
24504 /* Abort the currently active tentative parse. All consumed tokens
24505 will be rolled back, and no diagnostics will be issued. */
24506
24507 static void
24508 cp_parser_abort_tentative_parse (cp_parser* parser)
24509 {
24510 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24511 || errorcount > 0);
24512 cp_parser_simulate_error (parser);
24513 /* Now, pretend that we want to see if the construct was
24514 successfully parsed. */
24515 cp_parser_parse_definitely (parser);
24516 }
24517
24518 /* Stop parsing tentatively. If a parse error has occurred, restore the
24519 token stream. Otherwise, commit to the tokens we have consumed.
24520 Returns true if no error occurred; false otherwise. */
24521
24522 static bool
24523 cp_parser_parse_definitely (cp_parser* parser)
24524 {
24525 bool error_occurred;
24526 cp_parser_context *context;
24527
24528 /* Remember whether or not an error occurred, since we are about to
24529 destroy that information. */
24530 error_occurred = cp_parser_error_occurred (parser);
24531 /* Remove the topmost context from the stack. */
24532 context = parser->context;
24533 parser->context = context->next;
24534 /* If no parse errors occurred, commit to the tentative parse. */
24535 if (!error_occurred)
24536 {
24537 /* Commit to the tokens read tentatively, unless that was
24538 already done. */
24539 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
24540 cp_lexer_commit_tokens (parser->lexer);
24541
24542 pop_to_parent_deferring_access_checks ();
24543 }
24544 /* Otherwise, if errors occurred, roll back our state so that things
24545 are just as they were before we began the tentative parse. */
24546 else
24547 {
24548 cp_lexer_rollback_tokens (parser->lexer);
24549 pop_deferring_access_checks ();
24550 }
24551 /* Add the context to the front of the free list. */
24552 context->next = cp_parser_context_free_list;
24553 cp_parser_context_free_list = context;
24554
24555 return !error_occurred;
24556 }
24557
24558 /* Returns true if we are parsing tentatively and are not committed to
24559 this tentative parse. */
24560
24561 static bool
24562 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
24563 {
24564 return (cp_parser_parsing_tentatively (parser)
24565 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
24566 }
24567
24568 /* Returns nonzero iff an error has occurred during the most recent
24569 tentative parse. */
24570
24571 static bool
24572 cp_parser_error_occurred (cp_parser* parser)
24573 {
24574 return (cp_parser_parsing_tentatively (parser)
24575 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
24576 }
24577
24578 /* Returns nonzero if GNU extensions are allowed. */
24579
24580 static bool
24581 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
24582 {
24583 return parser->allow_gnu_extensions_p;
24584 }
24585 \f
24586 /* Objective-C++ Productions */
24587
24588
24589 /* Parse an Objective-C expression, which feeds into a primary-expression
24590 above.
24591
24592 objc-expression:
24593 objc-message-expression
24594 objc-string-literal
24595 objc-encode-expression
24596 objc-protocol-expression
24597 objc-selector-expression
24598
24599 Returns a tree representation of the expression. */
24600
24601 static tree
24602 cp_parser_objc_expression (cp_parser* parser)
24603 {
24604 /* Try to figure out what kind of declaration is present. */
24605 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24606
24607 switch (kwd->type)
24608 {
24609 case CPP_OPEN_SQUARE:
24610 return cp_parser_objc_message_expression (parser);
24611
24612 case CPP_OBJC_STRING:
24613 kwd = cp_lexer_consume_token (parser->lexer);
24614 return objc_build_string_object (kwd->u.value);
24615
24616 case CPP_KEYWORD:
24617 switch (kwd->keyword)
24618 {
24619 case RID_AT_ENCODE:
24620 return cp_parser_objc_encode_expression (parser);
24621
24622 case RID_AT_PROTOCOL:
24623 return cp_parser_objc_protocol_expression (parser);
24624
24625 case RID_AT_SELECTOR:
24626 return cp_parser_objc_selector_expression (parser);
24627
24628 default:
24629 break;
24630 }
24631 default:
24632 error_at (kwd->location,
24633 "misplaced %<@%D%> Objective-C++ construct",
24634 kwd->u.value);
24635 cp_parser_skip_to_end_of_block_or_statement (parser);
24636 }
24637
24638 return error_mark_node;
24639 }
24640
24641 /* Parse an Objective-C message expression.
24642
24643 objc-message-expression:
24644 [ objc-message-receiver objc-message-args ]
24645
24646 Returns a representation of an Objective-C message. */
24647
24648 static tree
24649 cp_parser_objc_message_expression (cp_parser* parser)
24650 {
24651 tree receiver, messageargs;
24652
24653 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
24654 receiver = cp_parser_objc_message_receiver (parser);
24655 messageargs = cp_parser_objc_message_args (parser);
24656 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24657
24658 return objc_build_message_expr (receiver, messageargs);
24659 }
24660
24661 /* Parse an objc-message-receiver.
24662
24663 objc-message-receiver:
24664 expression
24665 simple-type-specifier
24666
24667 Returns a representation of the type or expression. */
24668
24669 static tree
24670 cp_parser_objc_message_receiver (cp_parser* parser)
24671 {
24672 tree rcv;
24673
24674 /* An Objective-C message receiver may be either (1) a type
24675 or (2) an expression. */
24676 cp_parser_parse_tentatively (parser);
24677 rcv = cp_parser_expression (parser, false, NULL);
24678
24679 if (cp_parser_parse_definitely (parser))
24680 return rcv;
24681
24682 rcv = cp_parser_simple_type_specifier (parser,
24683 /*decl_specs=*/NULL,
24684 CP_PARSER_FLAGS_NONE);
24685
24686 return objc_get_class_reference (rcv);
24687 }
24688
24689 /* Parse the arguments and selectors comprising an Objective-C message.
24690
24691 objc-message-args:
24692 objc-selector
24693 objc-selector-args
24694 objc-selector-args , objc-comma-args
24695
24696 objc-selector-args:
24697 objc-selector [opt] : assignment-expression
24698 objc-selector-args objc-selector [opt] : assignment-expression
24699
24700 objc-comma-args:
24701 assignment-expression
24702 objc-comma-args , assignment-expression
24703
24704 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24705 selector arguments and TREE_VALUE containing a list of comma
24706 arguments. */
24707
24708 static tree
24709 cp_parser_objc_message_args (cp_parser* parser)
24710 {
24711 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
24712 bool maybe_unary_selector_p = true;
24713 cp_token *token = cp_lexer_peek_token (parser->lexer);
24714
24715 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24716 {
24717 tree selector = NULL_TREE, arg;
24718
24719 if (token->type != CPP_COLON)
24720 selector = cp_parser_objc_selector (parser);
24721
24722 /* Detect if we have a unary selector. */
24723 if (maybe_unary_selector_p
24724 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24725 return build_tree_list (selector, NULL_TREE);
24726
24727 maybe_unary_selector_p = false;
24728 cp_parser_require (parser, CPP_COLON, RT_COLON);
24729 arg = cp_parser_assignment_expression (parser, false, NULL);
24730
24731 sel_args
24732 = chainon (sel_args,
24733 build_tree_list (selector, arg));
24734
24735 token = cp_lexer_peek_token (parser->lexer);
24736 }
24737
24738 /* Handle non-selector arguments, if any. */
24739 while (token->type == CPP_COMMA)
24740 {
24741 tree arg;
24742
24743 cp_lexer_consume_token (parser->lexer);
24744 arg = cp_parser_assignment_expression (parser, false, NULL);
24745
24746 addl_args
24747 = chainon (addl_args,
24748 build_tree_list (NULL_TREE, arg));
24749
24750 token = cp_lexer_peek_token (parser->lexer);
24751 }
24752
24753 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24754 {
24755 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24756 return build_tree_list (error_mark_node, error_mark_node);
24757 }
24758
24759 return build_tree_list (sel_args, addl_args);
24760 }
24761
24762 /* Parse an Objective-C encode expression.
24763
24764 objc-encode-expression:
24765 @encode objc-typename
24766
24767 Returns an encoded representation of the type argument. */
24768
24769 static tree
24770 cp_parser_objc_encode_expression (cp_parser* parser)
24771 {
24772 tree type;
24773 cp_token *token;
24774
24775 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
24776 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24777 token = cp_lexer_peek_token (parser->lexer);
24778 type = complete_type (cp_parser_type_id (parser));
24779 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24780
24781 if (!type)
24782 {
24783 error_at (token->location,
24784 "%<@encode%> must specify a type as an argument");
24785 return error_mark_node;
24786 }
24787
24788 /* This happens if we find @encode(T) (where T is a template
24789 typename or something dependent on a template typename) when
24790 parsing a template. In that case, we can't compile it
24791 immediately, but we rather create an AT_ENCODE_EXPR which will
24792 need to be instantiated when the template is used.
24793 */
24794 if (dependent_type_p (type))
24795 {
24796 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24797 TREE_READONLY (value) = 1;
24798 return value;
24799 }
24800
24801 return objc_build_encode_expr (type);
24802 }
24803
24804 /* Parse an Objective-C @defs expression. */
24805
24806 static tree
24807 cp_parser_objc_defs_expression (cp_parser *parser)
24808 {
24809 tree name;
24810
24811 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
24812 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24813 name = cp_parser_identifier (parser);
24814 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24815
24816 return objc_get_class_ivars (name);
24817 }
24818
24819 /* Parse an Objective-C protocol expression.
24820
24821 objc-protocol-expression:
24822 @protocol ( identifier )
24823
24824 Returns a representation of the protocol expression. */
24825
24826 static tree
24827 cp_parser_objc_protocol_expression (cp_parser* parser)
24828 {
24829 tree proto;
24830
24831 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24832 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24833 proto = cp_parser_identifier (parser);
24834 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24835
24836 return objc_build_protocol_expr (proto);
24837 }
24838
24839 /* Parse an Objective-C selector expression.
24840
24841 objc-selector-expression:
24842 @selector ( objc-method-signature )
24843
24844 objc-method-signature:
24845 objc-selector
24846 objc-selector-seq
24847
24848 objc-selector-seq:
24849 objc-selector :
24850 objc-selector-seq objc-selector :
24851
24852 Returns a representation of the method selector. */
24853
24854 static tree
24855 cp_parser_objc_selector_expression (cp_parser* parser)
24856 {
24857 tree sel_seq = NULL_TREE;
24858 bool maybe_unary_selector_p = true;
24859 cp_token *token;
24860 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24861
24862 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
24863 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24864 token = cp_lexer_peek_token (parser->lexer);
24865
24866 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
24867 || token->type == CPP_SCOPE)
24868 {
24869 tree selector = NULL_TREE;
24870
24871 if (token->type != CPP_COLON
24872 || token->type == CPP_SCOPE)
24873 selector = cp_parser_objc_selector (parser);
24874
24875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
24876 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
24877 {
24878 /* Detect if we have a unary selector. */
24879 if (maybe_unary_selector_p)
24880 {
24881 sel_seq = selector;
24882 goto finish_selector;
24883 }
24884 else
24885 {
24886 cp_parser_error (parser, "expected %<:%>");
24887 }
24888 }
24889 maybe_unary_selector_p = false;
24890 token = cp_lexer_consume_token (parser->lexer);
24891
24892 if (token->type == CPP_SCOPE)
24893 {
24894 sel_seq
24895 = chainon (sel_seq,
24896 build_tree_list (selector, NULL_TREE));
24897 sel_seq
24898 = chainon (sel_seq,
24899 build_tree_list (NULL_TREE, NULL_TREE));
24900 }
24901 else
24902 sel_seq
24903 = chainon (sel_seq,
24904 build_tree_list (selector, NULL_TREE));
24905
24906 token = cp_lexer_peek_token (parser->lexer);
24907 }
24908
24909 finish_selector:
24910 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24911
24912 return objc_build_selector_expr (loc, sel_seq);
24913 }
24914
24915 /* Parse a list of identifiers.
24916
24917 objc-identifier-list:
24918 identifier
24919 objc-identifier-list , identifier
24920
24921 Returns a TREE_LIST of identifier nodes. */
24922
24923 static tree
24924 cp_parser_objc_identifier_list (cp_parser* parser)
24925 {
24926 tree identifier;
24927 tree list;
24928 cp_token *sep;
24929
24930 identifier = cp_parser_identifier (parser);
24931 if (identifier == error_mark_node)
24932 return error_mark_node;
24933
24934 list = build_tree_list (NULL_TREE, identifier);
24935 sep = cp_lexer_peek_token (parser->lexer);
24936
24937 while (sep->type == CPP_COMMA)
24938 {
24939 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24940 identifier = cp_parser_identifier (parser);
24941 if (identifier == error_mark_node)
24942 return list;
24943
24944 list = chainon (list, build_tree_list (NULL_TREE,
24945 identifier));
24946 sep = cp_lexer_peek_token (parser->lexer);
24947 }
24948
24949 return list;
24950 }
24951
24952 /* Parse an Objective-C alias declaration.
24953
24954 objc-alias-declaration:
24955 @compatibility_alias identifier identifier ;
24956
24957 This function registers the alias mapping with the Objective-C front end.
24958 It returns nothing. */
24959
24960 static void
24961 cp_parser_objc_alias_declaration (cp_parser* parser)
24962 {
24963 tree alias, orig;
24964
24965 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24966 alias = cp_parser_identifier (parser);
24967 orig = cp_parser_identifier (parser);
24968 objc_declare_alias (alias, orig);
24969 cp_parser_consume_semicolon_at_end_of_statement (parser);
24970 }
24971
24972 /* Parse an Objective-C class forward-declaration.
24973
24974 objc-class-declaration:
24975 @class objc-identifier-list ;
24976
24977 The function registers the forward declarations with the Objective-C
24978 front end. It returns nothing. */
24979
24980 static void
24981 cp_parser_objc_class_declaration (cp_parser* parser)
24982 {
24983 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24984 while (true)
24985 {
24986 tree id;
24987
24988 id = cp_parser_identifier (parser);
24989 if (id == error_mark_node)
24990 break;
24991
24992 objc_declare_class (id);
24993
24994 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24995 cp_lexer_consume_token (parser->lexer);
24996 else
24997 break;
24998 }
24999 cp_parser_consume_semicolon_at_end_of_statement (parser);
25000 }
25001
25002 /* Parse a list of Objective-C protocol references.
25003
25004 objc-protocol-refs-opt:
25005 objc-protocol-refs [opt]
25006
25007 objc-protocol-refs:
25008 < objc-identifier-list >
25009
25010 Returns a TREE_LIST of identifiers, if any. */
25011
25012 static tree
25013 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25014 {
25015 tree protorefs = NULL_TREE;
25016
25017 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25018 {
25019 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25020 protorefs = cp_parser_objc_identifier_list (parser);
25021 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25022 }
25023
25024 return protorefs;
25025 }
25026
25027 /* Parse a Objective-C visibility specification. */
25028
25029 static void
25030 cp_parser_objc_visibility_spec (cp_parser* parser)
25031 {
25032 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25033
25034 switch (vis->keyword)
25035 {
25036 case RID_AT_PRIVATE:
25037 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25038 break;
25039 case RID_AT_PROTECTED:
25040 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25041 break;
25042 case RID_AT_PUBLIC:
25043 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25044 break;
25045 case RID_AT_PACKAGE:
25046 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25047 break;
25048 default:
25049 return;
25050 }
25051
25052 /* Eat '@private'/'@protected'/'@public'. */
25053 cp_lexer_consume_token (parser->lexer);
25054 }
25055
25056 /* Parse an Objective-C method type. Return 'true' if it is a class
25057 (+) method, and 'false' if it is an instance (-) method. */
25058
25059 static inline bool
25060 cp_parser_objc_method_type (cp_parser* parser)
25061 {
25062 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25063 return true;
25064 else
25065 return false;
25066 }
25067
25068 /* Parse an Objective-C protocol qualifier. */
25069
25070 static tree
25071 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25072 {
25073 tree quals = NULL_TREE, node;
25074 cp_token *token = cp_lexer_peek_token (parser->lexer);
25075
25076 node = token->u.value;
25077
25078 while (node && identifier_p (node)
25079 && (node == ridpointers [(int) RID_IN]
25080 || node == ridpointers [(int) RID_OUT]
25081 || node == ridpointers [(int) RID_INOUT]
25082 || node == ridpointers [(int) RID_BYCOPY]
25083 || node == ridpointers [(int) RID_BYREF]
25084 || node == ridpointers [(int) RID_ONEWAY]))
25085 {
25086 quals = tree_cons (NULL_TREE, node, quals);
25087 cp_lexer_consume_token (parser->lexer);
25088 token = cp_lexer_peek_token (parser->lexer);
25089 node = token->u.value;
25090 }
25091
25092 return quals;
25093 }
25094
25095 /* Parse an Objective-C typename. */
25096
25097 static tree
25098 cp_parser_objc_typename (cp_parser* parser)
25099 {
25100 tree type_name = NULL_TREE;
25101
25102 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25103 {
25104 tree proto_quals, cp_type = NULL_TREE;
25105
25106 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25107 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25108
25109 /* An ObjC type name may consist of just protocol qualifiers, in which
25110 case the type shall default to 'id'. */
25111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25112 {
25113 cp_type = cp_parser_type_id (parser);
25114
25115 /* If the type could not be parsed, an error has already
25116 been produced. For error recovery, behave as if it had
25117 not been specified, which will use the default type
25118 'id'. */
25119 if (cp_type == error_mark_node)
25120 {
25121 cp_type = NULL_TREE;
25122 /* We need to skip to the closing parenthesis as
25123 cp_parser_type_id() does not seem to do it for
25124 us. */
25125 cp_parser_skip_to_closing_parenthesis (parser,
25126 /*recovering=*/true,
25127 /*or_comma=*/false,
25128 /*consume_paren=*/false);
25129 }
25130 }
25131
25132 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25133 type_name = build_tree_list (proto_quals, cp_type);
25134 }
25135
25136 return type_name;
25137 }
25138
25139 /* Check to see if TYPE refers to an Objective-C selector name. */
25140
25141 static bool
25142 cp_parser_objc_selector_p (enum cpp_ttype type)
25143 {
25144 return (type == CPP_NAME || type == CPP_KEYWORD
25145 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25146 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25147 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25148 || type == CPP_XOR || type == CPP_XOR_EQ);
25149 }
25150
25151 /* Parse an Objective-C selector. */
25152
25153 static tree
25154 cp_parser_objc_selector (cp_parser* parser)
25155 {
25156 cp_token *token = cp_lexer_consume_token (parser->lexer);
25157
25158 if (!cp_parser_objc_selector_p (token->type))
25159 {
25160 error_at (token->location, "invalid Objective-C++ selector name");
25161 return error_mark_node;
25162 }
25163
25164 /* C++ operator names are allowed to appear in ObjC selectors. */
25165 switch (token->type)
25166 {
25167 case CPP_AND_AND: return get_identifier ("and");
25168 case CPP_AND_EQ: return get_identifier ("and_eq");
25169 case CPP_AND: return get_identifier ("bitand");
25170 case CPP_OR: return get_identifier ("bitor");
25171 case CPP_COMPL: return get_identifier ("compl");
25172 case CPP_NOT: return get_identifier ("not");
25173 case CPP_NOT_EQ: return get_identifier ("not_eq");
25174 case CPP_OR_OR: return get_identifier ("or");
25175 case CPP_OR_EQ: return get_identifier ("or_eq");
25176 case CPP_XOR: return get_identifier ("xor");
25177 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25178 default: return token->u.value;
25179 }
25180 }
25181
25182 /* Parse an Objective-C params list. */
25183
25184 static tree
25185 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25186 {
25187 tree params = NULL_TREE;
25188 bool maybe_unary_selector_p = true;
25189 cp_token *token = cp_lexer_peek_token (parser->lexer);
25190
25191 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25192 {
25193 tree selector = NULL_TREE, type_name, identifier;
25194 tree parm_attr = NULL_TREE;
25195
25196 if (token->keyword == RID_ATTRIBUTE)
25197 break;
25198
25199 if (token->type != CPP_COLON)
25200 selector = cp_parser_objc_selector (parser);
25201
25202 /* Detect if we have a unary selector. */
25203 if (maybe_unary_selector_p
25204 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25205 {
25206 params = selector; /* Might be followed by attributes. */
25207 break;
25208 }
25209
25210 maybe_unary_selector_p = false;
25211 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25212 {
25213 /* Something went quite wrong. There should be a colon
25214 here, but there is not. Stop parsing parameters. */
25215 break;
25216 }
25217 type_name = cp_parser_objc_typename (parser);
25218 /* New ObjC allows attributes on parameters too. */
25219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25220 parm_attr = cp_parser_attributes_opt (parser);
25221 identifier = cp_parser_identifier (parser);
25222
25223 params
25224 = chainon (params,
25225 objc_build_keyword_decl (selector,
25226 type_name,
25227 identifier,
25228 parm_attr));
25229
25230 token = cp_lexer_peek_token (parser->lexer);
25231 }
25232
25233 if (params == NULL_TREE)
25234 {
25235 cp_parser_error (parser, "objective-c++ method declaration is expected");
25236 return error_mark_node;
25237 }
25238
25239 /* We allow tail attributes for the method. */
25240 if (token->keyword == RID_ATTRIBUTE)
25241 {
25242 *attributes = cp_parser_attributes_opt (parser);
25243 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25244 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25245 return params;
25246 cp_parser_error (parser,
25247 "method attributes must be specified at the end");
25248 return error_mark_node;
25249 }
25250
25251 if (params == NULL_TREE)
25252 {
25253 cp_parser_error (parser, "objective-c++ method declaration is expected");
25254 return error_mark_node;
25255 }
25256 return params;
25257 }
25258
25259 /* Parse the non-keyword Objective-C params. */
25260
25261 static tree
25262 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25263 tree* attributes)
25264 {
25265 tree params = make_node (TREE_LIST);
25266 cp_token *token = cp_lexer_peek_token (parser->lexer);
25267 *ellipsisp = false; /* Initially, assume no ellipsis. */
25268
25269 while (token->type == CPP_COMMA)
25270 {
25271 cp_parameter_declarator *parmdecl;
25272 tree parm;
25273
25274 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25275 token = cp_lexer_peek_token (parser->lexer);
25276
25277 if (token->type == CPP_ELLIPSIS)
25278 {
25279 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25280 *ellipsisp = true;
25281 token = cp_lexer_peek_token (parser->lexer);
25282 break;
25283 }
25284
25285 /* TODO: parse attributes for tail parameters. */
25286 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25287 parm = grokdeclarator (parmdecl->declarator,
25288 &parmdecl->decl_specifiers,
25289 PARM, /*initialized=*/0,
25290 /*attrlist=*/NULL);
25291
25292 chainon (params, build_tree_list (NULL_TREE, parm));
25293 token = cp_lexer_peek_token (parser->lexer);
25294 }
25295
25296 /* We allow tail attributes for the method. */
25297 if (token->keyword == RID_ATTRIBUTE)
25298 {
25299 if (*attributes == NULL_TREE)
25300 {
25301 *attributes = cp_parser_attributes_opt (parser);
25302 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25303 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25304 return params;
25305 }
25306 else
25307 /* We have an error, but parse the attributes, so that we can
25308 carry on. */
25309 *attributes = cp_parser_attributes_opt (parser);
25310
25311 cp_parser_error (parser,
25312 "method attributes must be specified at the end");
25313 return error_mark_node;
25314 }
25315
25316 return params;
25317 }
25318
25319 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25320
25321 static void
25322 cp_parser_objc_interstitial_code (cp_parser* parser)
25323 {
25324 cp_token *token = cp_lexer_peek_token (parser->lexer);
25325
25326 /* If the next token is `extern' and the following token is a string
25327 literal, then we have a linkage specification. */
25328 if (token->keyword == RID_EXTERN
25329 && cp_parser_is_pure_string_literal
25330 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25331 cp_parser_linkage_specification (parser);
25332 /* Handle #pragma, if any. */
25333 else if (token->type == CPP_PRAGMA)
25334 cp_parser_pragma (parser, pragma_objc_icode);
25335 /* Allow stray semicolons. */
25336 else if (token->type == CPP_SEMICOLON)
25337 cp_lexer_consume_token (parser->lexer);
25338 /* Mark methods as optional or required, when building protocols. */
25339 else if (token->keyword == RID_AT_OPTIONAL)
25340 {
25341 cp_lexer_consume_token (parser->lexer);
25342 objc_set_method_opt (true);
25343 }
25344 else if (token->keyword == RID_AT_REQUIRED)
25345 {
25346 cp_lexer_consume_token (parser->lexer);
25347 objc_set_method_opt (false);
25348 }
25349 else if (token->keyword == RID_NAMESPACE)
25350 cp_parser_namespace_definition (parser);
25351 /* Other stray characters must generate errors. */
25352 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25353 {
25354 cp_lexer_consume_token (parser->lexer);
25355 error ("stray %qs between Objective-C++ methods",
25356 token->type == CPP_OPEN_BRACE ? "{" : "}");
25357 }
25358 /* Finally, try to parse a block-declaration, or a function-definition. */
25359 else
25360 cp_parser_block_declaration (parser, /*statement_p=*/false);
25361 }
25362
25363 /* Parse a method signature. */
25364
25365 static tree
25366 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25367 {
25368 tree rettype, kwdparms, optparms;
25369 bool ellipsis = false;
25370 bool is_class_method;
25371
25372 is_class_method = cp_parser_objc_method_type (parser);
25373 rettype = cp_parser_objc_typename (parser);
25374 *attributes = NULL_TREE;
25375 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25376 if (kwdparms == error_mark_node)
25377 return error_mark_node;
25378 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25379 if (optparms == error_mark_node)
25380 return error_mark_node;
25381
25382 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25383 }
25384
25385 static bool
25386 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25387 {
25388 tree tattr;
25389 cp_lexer_save_tokens (parser->lexer);
25390 tattr = cp_parser_attributes_opt (parser);
25391 gcc_assert (tattr) ;
25392
25393 /* If the attributes are followed by a method introducer, this is not allowed.
25394 Dump the attributes and flag the situation. */
25395 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25396 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25397 return true;
25398
25399 /* Otherwise, the attributes introduce some interstitial code, possibly so
25400 rewind to allow that check. */
25401 cp_lexer_rollback_tokens (parser->lexer);
25402 return false;
25403 }
25404
25405 /* Parse an Objective-C method prototype list. */
25406
25407 static void
25408 cp_parser_objc_method_prototype_list (cp_parser* parser)
25409 {
25410 cp_token *token = cp_lexer_peek_token (parser->lexer);
25411
25412 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25413 {
25414 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25415 {
25416 tree attributes, sig;
25417 bool is_class_method;
25418 if (token->type == CPP_PLUS)
25419 is_class_method = true;
25420 else
25421 is_class_method = false;
25422 sig = cp_parser_objc_method_signature (parser, &attributes);
25423 if (sig == error_mark_node)
25424 {
25425 cp_parser_skip_to_end_of_block_or_statement (parser);
25426 token = cp_lexer_peek_token (parser->lexer);
25427 continue;
25428 }
25429 objc_add_method_declaration (is_class_method, sig, attributes);
25430 cp_parser_consume_semicolon_at_end_of_statement (parser);
25431 }
25432 else if (token->keyword == RID_AT_PROPERTY)
25433 cp_parser_objc_at_property_declaration (parser);
25434 else if (token->keyword == RID_ATTRIBUTE
25435 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25436 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25437 OPT_Wattributes,
25438 "prefix attributes are ignored for methods");
25439 else
25440 /* Allow for interspersed non-ObjC++ code. */
25441 cp_parser_objc_interstitial_code (parser);
25442
25443 token = cp_lexer_peek_token (parser->lexer);
25444 }
25445
25446 if (token->type != CPP_EOF)
25447 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25448 else
25449 cp_parser_error (parser, "expected %<@end%>");
25450
25451 objc_finish_interface ();
25452 }
25453
25454 /* Parse an Objective-C method definition list. */
25455
25456 static void
25457 cp_parser_objc_method_definition_list (cp_parser* parser)
25458 {
25459 cp_token *token = cp_lexer_peek_token (parser->lexer);
25460
25461 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25462 {
25463 tree meth;
25464
25465 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25466 {
25467 cp_token *ptk;
25468 tree sig, attribute;
25469 bool is_class_method;
25470 if (token->type == CPP_PLUS)
25471 is_class_method = true;
25472 else
25473 is_class_method = false;
25474 push_deferring_access_checks (dk_deferred);
25475 sig = cp_parser_objc_method_signature (parser, &attribute);
25476 if (sig == error_mark_node)
25477 {
25478 cp_parser_skip_to_end_of_block_or_statement (parser);
25479 token = cp_lexer_peek_token (parser->lexer);
25480 continue;
25481 }
25482 objc_start_method_definition (is_class_method, sig, attribute,
25483 NULL_TREE);
25484
25485 /* For historical reasons, we accept an optional semicolon. */
25486 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25487 cp_lexer_consume_token (parser->lexer);
25488
25489 ptk = cp_lexer_peek_token (parser->lexer);
25490 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25491 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25492 {
25493 perform_deferred_access_checks (tf_warning_or_error);
25494 stop_deferring_access_checks ();
25495 meth = cp_parser_function_definition_after_declarator (parser,
25496 false);
25497 pop_deferring_access_checks ();
25498 objc_finish_method_definition (meth);
25499 }
25500 }
25501 /* The following case will be removed once @synthesize is
25502 completely implemented. */
25503 else if (token->keyword == RID_AT_PROPERTY)
25504 cp_parser_objc_at_property_declaration (parser);
25505 else if (token->keyword == RID_AT_SYNTHESIZE)
25506 cp_parser_objc_at_synthesize_declaration (parser);
25507 else if (token->keyword == RID_AT_DYNAMIC)
25508 cp_parser_objc_at_dynamic_declaration (parser);
25509 else if (token->keyword == RID_ATTRIBUTE
25510 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25511 warning_at (token->location, OPT_Wattributes,
25512 "prefix attributes are ignored for methods");
25513 else
25514 /* Allow for interspersed non-ObjC++ code. */
25515 cp_parser_objc_interstitial_code (parser);
25516
25517 token = cp_lexer_peek_token (parser->lexer);
25518 }
25519
25520 if (token->type != CPP_EOF)
25521 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25522 else
25523 cp_parser_error (parser, "expected %<@end%>");
25524
25525 objc_finish_implementation ();
25526 }
25527
25528 /* Parse Objective-C ivars. */
25529
25530 static void
25531 cp_parser_objc_class_ivars (cp_parser* parser)
25532 {
25533 cp_token *token = cp_lexer_peek_token (parser->lexer);
25534
25535 if (token->type != CPP_OPEN_BRACE)
25536 return; /* No ivars specified. */
25537
25538 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
25539 token = cp_lexer_peek_token (parser->lexer);
25540
25541 while (token->type != CPP_CLOSE_BRACE
25542 && token->keyword != RID_AT_END && token->type != CPP_EOF)
25543 {
25544 cp_decl_specifier_seq declspecs;
25545 int decl_class_or_enum_p;
25546 tree prefix_attributes;
25547
25548 cp_parser_objc_visibility_spec (parser);
25549
25550 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25551 break;
25552
25553 cp_parser_decl_specifier_seq (parser,
25554 CP_PARSER_FLAGS_OPTIONAL,
25555 &declspecs,
25556 &decl_class_or_enum_p);
25557
25558 /* auto, register, static, extern, mutable. */
25559 if (declspecs.storage_class != sc_none)
25560 {
25561 cp_parser_error (parser, "invalid type for instance variable");
25562 declspecs.storage_class = sc_none;
25563 }
25564
25565 /* thread_local. */
25566 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25567 {
25568 cp_parser_error (parser, "invalid type for instance variable");
25569 declspecs.locations[ds_thread] = 0;
25570 }
25571
25572 /* typedef. */
25573 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25574 {
25575 cp_parser_error (parser, "invalid type for instance variable");
25576 declspecs.locations[ds_typedef] = 0;
25577 }
25578
25579 prefix_attributes = declspecs.attributes;
25580 declspecs.attributes = NULL_TREE;
25581
25582 /* Keep going until we hit the `;' at the end of the
25583 declaration. */
25584 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25585 {
25586 tree width = NULL_TREE, attributes, first_attribute, decl;
25587 cp_declarator *declarator = NULL;
25588 int ctor_dtor_or_conv_p;
25589
25590 /* Check for a (possibly unnamed) bitfield declaration. */
25591 token = cp_lexer_peek_token (parser->lexer);
25592 if (token->type == CPP_COLON)
25593 goto eat_colon;
25594
25595 if (token->type == CPP_NAME
25596 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25597 == CPP_COLON))
25598 {
25599 /* Get the name of the bitfield. */
25600 declarator = make_id_declarator (NULL_TREE,
25601 cp_parser_identifier (parser),
25602 sfk_none);
25603
25604 eat_colon:
25605 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25606 /* Get the width of the bitfield. */
25607 width
25608 = cp_parser_constant_expression (parser,
25609 /*allow_non_constant=*/false,
25610 NULL);
25611 }
25612 else
25613 {
25614 /* Parse the declarator. */
25615 declarator
25616 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25617 &ctor_dtor_or_conv_p,
25618 /*parenthesized_p=*/NULL,
25619 /*member_p=*/false);
25620 }
25621
25622 /* Look for attributes that apply to the ivar. */
25623 attributes = cp_parser_attributes_opt (parser);
25624 /* Remember which attributes are prefix attributes and
25625 which are not. */
25626 first_attribute = attributes;
25627 /* Combine the attributes. */
25628 attributes = chainon (prefix_attributes, attributes);
25629
25630 if (width)
25631 /* Create the bitfield declaration. */
25632 decl = grokbitfield (declarator, &declspecs,
25633 width,
25634 attributes);
25635 else
25636 decl = grokfield (declarator, &declspecs,
25637 NULL_TREE, /*init_const_expr_p=*/false,
25638 NULL_TREE, attributes);
25639
25640 /* Add the instance variable. */
25641 if (decl != error_mark_node && decl != NULL_TREE)
25642 objc_add_instance_variable (decl);
25643
25644 /* Reset PREFIX_ATTRIBUTES. */
25645 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25646 attributes = TREE_CHAIN (attributes);
25647 if (attributes)
25648 TREE_CHAIN (attributes) = NULL_TREE;
25649
25650 token = cp_lexer_peek_token (parser->lexer);
25651
25652 if (token->type == CPP_COMMA)
25653 {
25654 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25655 continue;
25656 }
25657 break;
25658 }
25659
25660 cp_parser_consume_semicolon_at_end_of_statement (parser);
25661 token = cp_lexer_peek_token (parser->lexer);
25662 }
25663
25664 if (token->keyword == RID_AT_END)
25665 cp_parser_error (parser, "expected %<}%>");
25666
25667 /* Do not consume the RID_AT_END, so it will be read again as terminating
25668 the @interface of @implementation. */
25669 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
25670 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
25671
25672 /* For historical reasons, we accept an optional semicolon. */
25673 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25674 cp_lexer_consume_token (parser->lexer);
25675 }
25676
25677 /* Parse an Objective-C protocol declaration. */
25678
25679 static void
25680 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
25681 {
25682 tree proto, protorefs;
25683 cp_token *tok;
25684
25685 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
25687 {
25688 tok = cp_lexer_peek_token (parser->lexer);
25689 error_at (tok->location, "identifier expected after %<@protocol%>");
25690 cp_parser_consume_semicolon_at_end_of_statement (parser);
25691 return;
25692 }
25693
25694 /* See if we have a forward declaration or a definition. */
25695 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
25696
25697 /* Try a forward declaration first. */
25698 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
25699 {
25700 while (true)
25701 {
25702 tree id;
25703
25704 id = cp_parser_identifier (parser);
25705 if (id == error_mark_node)
25706 break;
25707
25708 objc_declare_protocol (id, attributes);
25709
25710 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25711 cp_lexer_consume_token (parser->lexer);
25712 else
25713 break;
25714 }
25715 cp_parser_consume_semicolon_at_end_of_statement (parser);
25716 }
25717
25718 /* Ok, we got a full-fledged definition (or at least should). */
25719 else
25720 {
25721 proto = cp_parser_identifier (parser);
25722 protorefs = cp_parser_objc_protocol_refs_opt (parser);
25723 objc_start_protocol (proto, protorefs, attributes);
25724 cp_parser_objc_method_prototype_list (parser);
25725 }
25726 }
25727
25728 /* Parse an Objective-C superclass or category. */
25729
25730 static void
25731 cp_parser_objc_superclass_or_category (cp_parser *parser,
25732 bool iface_p,
25733 tree *super,
25734 tree *categ, bool *is_class_extension)
25735 {
25736 cp_token *next = cp_lexer_peek_token (parser->lexer);
25737
25738 *super = *categ = NULL_TREE;
25739 *is_class_extension = false;
25740 if (next->type == CPP_COLON)
25741 {
25742 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25743 *super = cp_parser_identifier (parser);
25744 }
25745 else if (next->type == CPP_OPEN_PAREN)
25746 {
25747 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25748
25749 /* If there is no category name, and this is an @interface, we
25750 have a class extension. */
25751 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25752 {
25753 *categ = NULL_TREE;
25754 *is_class_extension = true;
25755 }
25756 else
25757 *categ = cp_parser_identifier (parser);
25758
25759 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25760 }
25761 }
25762
25763 /* Parse an Objective-C class interface. */
25764
25765 static void
25766 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25767 {
25768 tree name, super, categ, protos;
25769 bool is_class_extension;
25770
25771 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
25772 name = cp_parser_identifier (parser);
25773 if (name == error_mark_node)
25774 {
25775 /* It's hard to recover because even if valid @interface stuff
25776 is to follow, we can't compile it (or validate it) if we
25777 don't even know which class it refers to. Let's assume this
25778 was a stray '@interface' token in the stream and skip it.
25779 */
25780 return;
25781 }
25782 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25783 &is_class_extension);
25784 protos = cp_parser_objc_protocol_refs_opt (parser);
25785
25786 /* We have either a class or a category on our hands. */
25787 if (categ || is_class_extension)
25788 objc_start_category_interface (name, categ, protos, attributes);
25789 else
25790 {
25791 objc_start_class_interface (name, super, protos, attributes);
25792 /* Handle instance variable declarations, if any. */
25793 cp_parser_objc_class_ivars (parser);
25794 objc_continue_interface ();
25795 }
25796
25797 cp_parser_objc_method_prototype_list (parser);
25798 }
25799
25800 /* Parse an Objective-C class implementation. */
25801
25802 static void
25803 cp_parser_objc_class_implementation (cp_parser* parser)
25804 {
25805 tree name, super, categ;
25806 bool is_class_extension;
25807
25808 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
25809 name = cp_parser_identifier (parser);
25810 if (name == error_mark_node)
25811 {
25812 /* It's hard to recover because even if valid @implementation
25813 stuff is to follow, we can't compile it (or validate it) if
25814 we don't even know which class it refers to. Let's assume
25815 this was a stray '@implementation' token in the stream and
25816 skip it.
25817 */
25818 return;
25819 }
25820 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25821 &is_class_extension);
25822
25823 /* We have either a class or a category on our hands. */
25824 if (categ)
25825 objc_start_category_implementation (name, categ);
25826 else
25827 {
25828 objc_start_class_implementation (name, super);
25829 /* Handle instance variable declarations, if any. */
25830 cp_parser_objc_class_ivars (parser);
25831 objc_continue_implementation ();
25832 }
25833
25834 cp_parser_objc_method_definition_list (parser);
25835 }
25836
25837 /* Consume the @end token and finish off the implementation. */
25838
25839 static void
25840 cp_parser_objc_end_implementation (cp_parser* parser)
25841 {
25842 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25843 objc_finish_implementation ();
25844 }
25845
25846 /* Parse an Objective-C declaration. */
25847
25848 static void
25849 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
25850 {
25851 /* Try to figure out what kind of declaration is present. */
25852 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25853
25854 if (attributes)
25855 switch (kwd->keyword)
25856 {
25857 case RID_AT_ALIAS:
25858 case RID_AT_CLASS:
25859 case RID_AT_END:
25860 error_at (kwd->location, "attributes may not be specified before"
25861 " the %<@%D%> Objective-C++ keyword",
25862 kwd->u.value);
25863 attributes = NULL;
25864 break;
25865 case RID_AT_IMPLEMENTATION:
25866 warning_at (kwd->location, OPT_Wattributes,
25867 "prefix attributes are ignored before %<@%D%>",
25868 kwd->u.value);
25869 attributes = NULL;
25870 default:
25871 break;
25872 }
25873
25874 switch (kwd->keyword)
25875 {
25876 case RID_AT_ALIAS:
25877 cp_parser_objc_alias_declaration (parser);
25878 break;
25879 case RID_AT_CLASS:
25880 cp_parser_objc_class_declaration (parser);
25881 break;
25882 case RID_AT_PROTOCOL:
25883 cp_parser_objc_protocol_declaration (parser, attributes);
25884 break;
25885 case RID_AT_INTERFACE:
25886 cp_parser_objc_class_interface (parser, attributes);
25887 break;
25888 case RID_AT_IMPLEMENTATION:
25889 cp_parser_objc_class_implementation (parser);
25890 break;
25891 case RID_AT_END:
25892 cp_parser_objc_end_implementation (parser);
25893 break;
25894 default:
25895 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25896 kwd->u.value);
25897 cp_parser_skip_to_end_of_block_or_statement (parser);
25898 }
25899 }
25900
25901 /* Parse an Objective-C try-catch-finally statement.
25902
25903 objc-try-catch-finally-stmt:
25904 @try compound-statement objc-catch-clause-seq [opt]
25905 objc-finally-clause [opt]
25906
25907 objc-catch-clause-seq:
25908 objc-catch-clause objc-catch-clause-seq [opt]
25909
25910 objc-catch-clause:
25911 @catch ( objc-exception-declaration ) compound-statement
25912
25913 objc-finally-clause:
25914 @finally compound-statement
25915
25916 objc-exception-declaration:
25917 parameter-declaration
25918 '...'
25919
25920 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25921
25922 Returns NULL_TREE.
25923
25924 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25925 for C. Keep them in sync. */
25926
25927 static tree
25928 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25929 {
25930 location_t location;
25931 tree stmt;
25932
25933 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25934 location = cp_lexer_peek_token (parser->lexer)->location;
25935 objc_maybe_warn_exceptions (location);
25936 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25937 node, lest it get absorbed into the surrounding block. */
25938 stmt = push_stmt_list ();
25939 cp_parser_compound_statement (parser, NULL, false, false);
25940 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25941
25942 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25943 {
25944 cp_parameter_declarator *parm;
25945 tree parameter_declaration = error_mark_node;
25946 bool seen_open_paren = false;
25947
25948 cp_lexer_consume_token (parser->lexer);
25949 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25950 seen_open_paren = true;
25951 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25952 {
25953 /* We have "@catch (...)" (where the '...' are literally
25954 what is in the code). Skip the '...'.
25955 parameter_declaration is set to NULL_TREE, and
25956 objc_being_catch_clauses() knows that that means
25957 '...'. */
25958 cp_lexer_consume_token (parser->lexer);
25959 parameter_declaration = NULL_TREE;
25960 }
25961 else
25962 {
25963 /* We have "@catch (NSException *exception)" or something
25964 like that. Parse the parameter declaration. */
25965 parm = cp_parser_parameter_declaration (parser, false, NULL);
25966 if (parm == NULL)
25967 parameter_declaration = error_mark_node;
25968 else
25969 parameter_declaration = grokdeclarator (parm->declarator,
25970 &parm->decl_specifiers,
25971 PARM, /*initialized=*/0,
25972 /*attrlist=*/NULL);
25973 }
25974 if (seen_open_paren)
25975 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25976 else
25977 {
25978 /* If there was no open parenthesis, we are recovering from
25979 an error, and we are trying to figure out what mistake
25980 the user has made. */
25981
25982 /* If there is an immediate closing parenthesis, the user
25983 probably forgot the opening one (ie, they typed "@catch
25984 NSException *e)". Parse the closing parenthesis and keep
25985 going. */
25986 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25987 cp_lexer_consume_token (parser->lexer);
25988
25989 /* If these is no immediate closing parenthesis, the user
25990 probably doesn't know that parenthesis are required at
25991 all (ie, they typed "@catch NSException *e"). So, just
25992 forget about the closing parenthesis and keep going. */
25993 }
25994 objc_begin_catch_clause (parameter_declaration);
25995 cp_parser_compound_statement (parser, NULL, false, false);
25996 objc_finish_catch_clause ();
25997 }
25998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25999 {
26000 cp_lexer_consume_token (parser->lexer);
26001 location = cp_lexer_peek_token (parser->lexer)->location;
26002 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26003 node, lest it get absorbed into the surrounding block. */
26004 stmt = push_stmt_list ();
26005 cp_parser_compound_statement (parser, NULL, false, false);
26006 objc_build_finally_clause (location, pop_stmt_list (stmt));
26007 }
26008
26009 return objc_finish_try_stmt ();
26010 }
26011
26012 /* Parse an Objective-C synchronized statement.
26013
26014 objc-synchronized-stmt:
26015 @synchronized ( expression ) compound-statement
26016
26017 Returns NULL_TREE. */
26018
26019 static tree
26020 cp_parser_objc_synchronized_statement (cp_parser *parser)
26021 {
26022 location_t location;
26023 tree lock, stmt;
26024
26025 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26026
26027 location = cp_lexer_peek_token (parser->lexer)->location;
26028 objc_maybe_warn_exceptions (location);
26029 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26030 lock = cp_parser_expression (parser, false, NULL);
26031 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26032
26033 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26034 node, lest it get absorbed into the surrounding block. */
26035 stmt = push_stmt_list ();
26036 cp_parser_compound_statement (parser, NULL, false, false);
26037
26038 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26039 }
26040
26041 /* Parse an Objective-C throw statement.
26042
26043 objc-throw-stmt:
26044 @throw assignment-expression [opt] ;
26045
26046 Returns a constructed '@throw' statement. */
26047
26048 static tree
26049 cp_parser_objc_throw_statement (cp_parser *parser)
26050 {
26051 tree expr = NULL_TREE;
26052 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26053
26054 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26055
26056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26057 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26058
26059 cp_parser_consume_semicolon_at_end_of_statement (parser);
26060
26061 return objc_build_throw_stmt (loc, expr);
26062 }
26063
26064 /* Parse an Objective-C statement. */
26065
26066 static tree
26067 cp_parser_objc_statement (cp_parser * parser)
26068 {
26069 /* Try to figure out what kind of declaration is present. */
26070 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26071
26072 switch (kwd->keyword)
26073 {
26074 case RID_AT_TRY:
26075 return cp_parser_objc_try_catch_finally_statement (parser);
26076 case RID_AT_SYNCHRONIZED:
26077 return cp_parser_objc_synchronized_statement (parser);
26078 case RID_AT_THROW:
26079 return cp_parser_objc_throw_statement (parser);
26080 default:
26081 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26082 kwd->u.value);
26083 cp_parser_skip_to_end_of_block_or_statement (parser);
26084 }
26085
26086 return error_mark_node;
26087 }
26088
26089 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26090 look ahead to see if an objc keyword follows the attributes. This
26091 is to detect the use of prefix attributes on ObjC @interface and
26092 @protocol. */
26093
26094 static bool
26095 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26096 {
26097 cp_lexer_save_tokens (parser->lexer);
26098 *attrib = cp_parser_attributes_opt (parser);
26099 gcc_assert (*attrib);
26100 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26101 {
26102 cp_lexer_commit_tokens (parser->lexer);
26103 return true;
26104 }
26105 cp_lexer_rollback_tokens (parser->lexer);
26106 return false;
26107 }
26108
26109 /* This routine is a minimal replacement for
26110 c_parser_struct_declaration () used when parsing the list of
26111 types/names or ObjC++ properties. For example, when parsing the
26112 code
26113
26114 @property (readonly) int a, b, c;
26115
26116 this function is responsible for parsing "int a, int b, int c" and
26117 returning the declarations as CHAIN of DECLs.
26118
26119 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26120 similar parsing. */
26121 static tree
26122 cp_parser_objc_struct_declaration (cp_parser *parser)
26123 {
26124 tree decls = NULL_TREE;
26125 cp_decl_specifier_seq declspecs;
26126 int decl_class_or_enum_p;
26127 tree prefix_attributes;
26128
26129 cp_parser_decl_specifier_seq (parser,
26130 CP_PARSER_FLAGS_NONE,
26131 &declspecs,
26132 &decl_class_or_enum_p);
26133
26134 if (declspecs.type == error_mark_node)
26135 return error_mark_node;
26136
26137 /* auto, register, static, extern, mutable. */
26138 if (declspecs.storage_class != sc_none)
26139 {
26140 cp_parser_error (parser, "invalid type for property");
26141 declspecs.storage_class = sc_none;
26142 }
26143
26144 /* thread_local. */
26145 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26146 {
26147 cp_parser_error (parser, "invalid type for property");
26148 declspecs.locations[ds_thread] = 0;
26149 }
26150
26151 /* typedef. */
26152 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26153 {
26154 cp_parser_error (parser, "invalid type for property");
26155 declspecs.locations[ds_typedef] = 0;
26156 }
26157
26158 prefix_attributes = declspecs.attributes;
26159 declspecs.attributes = NULL_TREE;
26160
26161 /* Keep going until we hit the `;' at the end of the declaration. */
26162 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26163 {
26164 tree attributes, first_attribute, decl;
26165 cp_declarator *declarator;
26166 cp_token *token;
26167
26168 /* Parse the declarator. */
26169 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26170 NULL, NULL, false);
26171
26172 /* Look for attributes that apply to the ivar. */
26173 attributes = cp_parser_attributes_opt (parser);
26174 /* Remember which attributes are prefix attributes and
26175 which are not. */
26176 first_attribute = attributes;
26177 /* Combine the attributes. */
26178 attributes = chainon (prefix_attributes, attributes);
26179
26180 decl = grokfield (declarator, &declspecs,
26181 NULL_TREE, /*init_const_expr_p=*/false,
26182 NULL_TREE, attributes);
26183
26184 if (decl == error_mark_node || decl == NULL_TREE)
26185 return error_mark_node;
26186
26187 /* Reset PREFIX_ATTRIBUTES. */
26188 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26189 attributes = TREE_CHAIN (attributes);
26190 if (attributes)
26191 TREE_CHAIN (attributes) = NULL_TREE;
26192
26193 DECL_CHAIN (decl) = decls;
26194 decls = decl;
26195
26196 token = cp_lexer_peek_token (parser->lexer);
26197 if (token->type == CPP_COMMA)
26198 {
26199 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26200 continue;
26201 }
26202 else
26203 break;
26204 }
26205 return decls;
26206 }
26207
26208 /* Parse an Objective-C @property declaration. The syntax is:
26209
26210 objc-property-declaration:
26211 '@property' objc-property-attributes[opt] struct-declaration ;
26212
26213 objc-property-attributes:
26214 '(' objc-property-attribute-list ')'
26215
26216 objc-property-attribute-list:
26217 objc-property-attribute
26218 objc-property-attribute-list, objc-property-attribute
26219
26220 objc-property-attribute
26221 'getter' = identifier
26222 'setter' = identifier
26223 'readonly'
26224 'readwrite'
26225 'assign'
26226 'retain'
26227 'copy'
26228 'nonatomic'
26229
26230 For example:
26231 @property NSString *name;
26232 @property (readonly) id object;
26233 @property (retain, nonatomic, getter=getTheName) id name;
26234 @property int a, b, c;
26235
26236 PS: This function is identical to
26237 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26238 static void
26239 cp_parser_objc_at_property_declaration (cp_parser *parser)
26240 {
26241 /* The following variables hold the attributes of the properties as
26242 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26243 seen. When we see an attribute, we set them to 'true' (if they
26244 are boolean properties) or to the identifier (if they have an
26245 argument, ie, for getter and setter). Note that here we only
26246 parse the list of attributes, check the syntax and accumulate the
26247 attributes that we find. objc_add_property_declaration() will
26248 then process the information. */
26249 bool property_assign = false;
26250 bool property_copy = false;
26251 tree property_getter_ident = NULL_TREE;
26252 bool property_nonatomic = false;
26253 bool property_readonly = false;
26254 bool property_readwrite = false;
26255 bool property_retain = false;
26256 tree property_setter_ident = NULL_TREE;
26257
26258 /* 'properties' is the list of properties that we read. Usually a
26259 single one, but maybe more (eg, in "@property int a, b, c;" there
26260 are three). */
26261 tree properties;
26262 location_t loc;
26263
26264 loc = cp_lexer_peek_token (parser->lexer)->location;
26265
26266 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26267
26268 /* Parse the optional attribute list... */
26269 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26270 {
26271 /* Eat the '('. */
26272 cp_lexer_consume_token (parser->lexer);
26273
26274 while (true)
26275 {
26276 bool syntax_error = false;
26277 cp_token *token = cp_lexer_peek_token (parser->lexer);
26278 enum rid keyword;
26279
26280 if (token->type != CPP_NAME)
26281 {
26282 cp_parser_error (parser, "expected identifier");
26283 break;
26284 }
26285 keyword = C_RID_CODE (token->u.value);
26286 cp_lexer_consume_token (parser->lexer);
26287 switch (keyword)
26288 {
26289 case RID_ASSIGN: property_assign = true; break;
26290 case RID_COPY: property_copy = true; break;
26291 case RID_NONATOMIC: property_nonatomic = true; break;
26292 case RID_READONLY: property_readonly = true; break;
26293 case RID_READWRITE: property_readwrite = true; break;
26294 case RID_RETAIN: property_retain = true; break;
26295
26296 case RID_GETTER:
26297 case RID_SETTER:
26298 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26299 {
26300 if (keyword == RID_GETTER)
26301 cp_parser_error (parser,
26302 "missing %<=%> (after %<getter%> attribute)");
26303 else
26304 cp_parser_error (parser,
26305 "missing %<=%> (after %<setter%> attribute)");
26306 syntax_error = true;
26307 break;
26308 }
26309 cp_lexer_consume_token (parser->lexer); /* eat the = */
26310 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26311 {
26312 cp_parser_error (parser, "expected identifier");
26313 syntax_error = true;
26314 break;
26315 }
26316 if (keyword == RID_SETTER)
26317 {
26318 if (property_setter_ident != NULL_TREE)
26319 {
26320 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26321 cp_lexer_consume_token (parser->lexer);
26322 }
26323 else
26324 property_setter_ident = cp_parser_objc_selector (parser);
26325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26326 cp_parser_error (parser, "setter name must terminate with %<:%>");
26327 else
26328 cp_lexer_consume_token (parser->lexer);
26329 }
26330 else
26331 {
26332 if (property_getter_ident != NULL_TREE)
26333 {
26334 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26335 cp_lexer_consume_token (parser->lexer);
26336 }
26337 else
26338 property_getter_ident = cp_parser_objc_selector (parser);
26339 }
26340 break;
26341 default:
26342 cp_parser_error (parser, "unknown property attribute");
26343 syntax_error = true;
26344 break;
26345 }
26346
26347 if (syntax_error)
26348 break;
26349
26350 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26351 cp_lexer_consume_token (parser->lexer);
26352 else
26353 break;
26354 }
26355
26356 /* FIXME: "@property (setter, assign);" will generate a spurious
26357 "error: expected ‘)’ before ‘,’ token". This is because
26358 cp_parser_require, unlike the C counterpart, will produce an
26359 error even if we are in error recovery. */
26360 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26361 {
26362 cp_parser_skip_to_closing_parenthesis (parser,
26363 /*recovering=*/true,
26364 /*or_comma=*/false,
26365 /*consume_paren=*/true);
26366 }
26367 }
26368
26369 /* ... and the property declaration(s). */
26370 properties = cp_parser_objc_struct_declaration (parser);
26371
26372 if (properties == error_mark_node)
26373 {
26374 cp_parser_skip_to_end_of_statement (parser);
26375 /* If the next token is now a `;', consume it. */
26376 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26377 cp_lexer_consume_token (parser->lexer);
26378 return;
26379 }
26380
26381 if (properties == NULL_TREE)
26382 cp_parser_error (parser, "expected identifier");
26383 else
26384 {
26385 /* Comma-separated properties are chained together in
26386 reverse order; add them one by one. */
26387 properties = nreverse (properties);
26388
26389 for (; properties; properties = TREE_CHAIN (properties))
26390 objc_add_property_declaration (loc, copy_node (properties),
26391 property_readonly, property_readwrite,
26392 property_assign, property_retain,
26393 property_copy, property_nonatomic,
26394 property_getter_ident, property_setter_ident);
26395 }
26396
26397 cp_parser_consume_semicolon_at_end_of_statement (parser);
26398 }
26399
26400 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26401
26402 objc-synthesize-declaration:
26403 @synthesize objc-synthesize-identifier-list ;
26404
26405 objc-synthesize-identifier-list:
26406 objc-synthesize-identifier
26407 objc-synthesize-identifier-list, objc-synthesize-identifier
26408
26409 objc-synthesize-identifier
26410 identifier
26411 identifier = identifier
26412
26413 For example:
26414 @synthesize MyProperty;
26415 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26416
26417 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26418 for C. Keep them in sync.
26419 */
26420 static void
26421 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26422 {
26423 tree list = NULL_TREE;
26424 location_t loc;
26425 loc = cp_lexer_peek_token (parser->lexer)->location;
26426
26427 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26428 while (true)
26429 {
26430 tree property, ivar;
26431 property = cp_parser_identifier (parser);
26432 if (property == error_mark_node)
26433 {
26434 cp_parser_consume_semicolon_at_end_of_statement (parser);
26435 return;
26436 }
26437 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26438 {
26439 cp_lexer_consume_token (parser->lexer);
26440 ivar = cp_parser_identifier (parser);
26441 if (ivar == error_mark_node)
26442 {
26443 cp_parser_consume_semicolon_at_end_of_statement (parser);
26444 return;
26445 }
26446 }
26447 else
26448 ivar = NULL_TREE;
26449 list = chainon (list, build_tree_list (ivar, property));
26450 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26451 cp_lexer_consume_token (parser->lexer);
26452 else
26453 break;
26454 }
26455 cp_parser_consume_semicolon_at_end_of_statement (parser);
26456 objc_add_synthesize_declaration (loc, list);
26457 }
26458
26459 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26460
26461 objc-dynamic-declaration:
26462 @dynamic identifier-list ;
26463
26464 For example:
26465 @dynamic MyProperty;
26466 @dynamic MyProperty, AnotherProperty;
26467
26468 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26469 for C. Keep them in sync.
26470 */
26471 static void
26472 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26473 {
26474 tree list = NULL_TREE;
26475 location_t loc;
26476 loc = cp_lexer_peek_token (parser->lexer)->location;
26477
26478 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26479 while (true)
26480 {
26481 tree property;
26482 property = cp_parser_identifier (parser);
26483 if (property == error_mark_node)
26484 {
26485 cp_parser_consume_semicolon_at_end_of_statement (parser);
26486 return;
26487 }
26488 list = chainon (list, build_tree_list (NULL, property));
26489 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26490 cp_lexer_consume_token (parser->lexer);
26491 else
26492 break;
26493 }
26494 cp_parser_consume_semicolon_at_end_of_statement (parser);
26495 objc_add_dynamic_declaration (loc, list);
26496 }
26497
26498 \f
26499 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26500
26501 /* Returns name of the next clause.
26502 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26503 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26504 returned and the token is consumed. */
26505
26506 static pragma_omp_clause
26507 cp_parser_omp_clause_name (cp_parser *parser)
26508 {
26509 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26510
26511 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26512 result = PRAGMA_OMP_CLAUSE_IF;
26513 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26514 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26515 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26516 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26517 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26518 result = PRAGMA_OMP_CLAUSE_FOR;
26519 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26520 {
26521 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26522 const char *p = IDENTIFIER_POINTER (id);
26523
26524 switch (p[0])
26525 {
26526 case 'a':
26527 if (!strcmp ("aligned", p))
26528 result = PRAGMA_OMP_CLAUSE_ALIGNED;
26529 break;
26530 case 'c':
26531 if (!strcmp ("collapse", p))
26532 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
26533 else if (!strcmp ("copyin", p))
26534 result = PRAGMA_OMP_CLAUSE_COPYIN;
26535 else if (!strcmp ("copyprivate", p))
26536 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
26537 break;
26538 case 'd':
26539 if (!strcmp ("depend", p))
26540 result = PRAGMA_OMP_CLAUSE_DEPEND;
26541 else if (!strcmp ("device", p))
26542 result = PRAGMA_OMP_CLAUSE_DEVICE;
26543 else if (!strcmp ("dist_schedule", p))
26544 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
26545 break;
26546 case 'f':
26547 if (!strcmp ("final", p))
26548 result = PRAGMA_OMP_CLAUSE_FINAL;
26549 else if (!strcmp ("firstprivate", p))
26550 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
26551 else if (!strcmp ("from", p))
26552 result = PRAGMA_OMP_CLAUSE_FROM;
26553 break;
26554 case 'i':
26555 if (!strcmp ("inbranch", p))
26556 result = PRAGMA_OMP_CLAUSE_INBRANCH;
26557 break;
26558 case 'l':
26559 if (!strcmp ("lastprivate", p))
26560 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
26561 else if (!strcmp ("linear", p))
26562 result = PRAGMA_OMP_CLAUSE_LINEAR;
26563 break;
26564 case 'm':
26565 if (!strcmp ("map", p))
26566 result = PRAGMA_OMP_CLAUSE_MAP;
26567 else if (!strcmp ("mergeable", p))
26568 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
26569 break;
26570 case 'n':
26571 if (!strcmp ("notinbranch", p))
26572 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
26573 else if (!strcmp ("nowait", p))
26574 result = PRAGMA_OMP_CLAUSE_NOWAIT;
26575 else if (!strcmp ("num_teams", p))
26576 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
26577 else if (!strcmp ("num_threads", p))
26578 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
26579 break;
26580 case 'o':
26581 if (!strcmp ("ordered", p))
26582 result = PRAGMA_OMP_CLAUSE_ORDERED;
26583 break;
26584 case 'p':
26585 if (!strcmp ("parallel", p))
26586 result = PRAGMA_OMP_CLAUSE_PARALLEL;
26587 else if (!strcmp ("proc_bind", p))
26588 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
26589 break;
26590 case 'r':
26591 if (!strcmp ("reduction", p))
26592 result = PRAGMA_OMP_CLAUSE_REDUCTION;
26593 break;
26594 case 's':
26595 if (!strcmp ("safelen", p))
26596 result = PRAGMA_OMP_CLAUSE_SAFELEN;
26597 else if (!strcmp ("schedule", p))
26598 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
26599 else if (!strcmp ("sections", p))
26600 result = PRAGMA_OMP_CLAUSE_SECTIONS;
26601 else if (!strcmp ("shared", p))
26602 result = PRAGMA_OMP_CLAUSE_SHARED;
26603 else if (!strcmp ("simdlen", p))
26604 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
26605 break;
26606 case 't':
26607 if (!strcmp ("taskgroup", p))
26608 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
26609 else if (!strcmp ("thread_limit", p))
26610 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
26611 else if (!strcmp ("to", p))
26612 result = PRAGMA_OMP_CLAUSE_TO;
26613 break;
26614 case 'u':
26615 if (!strcmp ("uniform", p))
26616 result = PRAGMA_OMP_CLAUSE_UNIFORM;
26617 else if (!strcmp ("untied", p))
26618 result = PRAGMA_OMP_CLAUSE_UNTIED;
26619 break;
26620 }
26621 }
26622
26623 if (result != PRAGMA_OMP_CLAUSE_NONE)
26624 cp_lexer_consume_token (parser->lexer);
26625
26626 return result;
26627 }
26628
26629 /* Validate that a clause of the given type does not already exist. */
26630
26631 static void
26632 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
26633 const char *name, location_t location)
26634 {
26635 tree c;
26636
26637 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26638 if (OMP_CLAUSE_CODE (c) == code)
26639 {
26640 error_at (location, "too many %qs clauses", name);
26641 break;
26642 }
26643 }
26644
26645 /* OpenMP 2.5:
26646 variable-list:
26647 identifier
26648 variable-list , identifier
26649
26650 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
26651 colon). An opening parenthesis will have been consumed by the caller.
26652
26653 If KIND is nonzero, create the appropriate node and install the decl
26654 in OMP_CLAUSE_DECL and add the node to the head of the list.
26655
26656 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
26657 return the list created.
26658
26659 COLON can be NULL if only closing parenthesis should end the list,
26660 or pointer to bool which will receive false if the list is terminated
26661 by closing parenthesis or true if the list is terminated by colon. */
26662
26663 static tree
26664 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
26665 tree list, bool *colon)
26666 {
26667 cp_token *token;
26668 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26669 if (colon)
26670 {
26671 parser->colon_corrects_to_scope_p = false;
26672 *colon = false;
26673 }
26674 while (1)
26675 {
26676 tree name, decl;
26677
26678 token = cp_lexer_peek_token (parser->lexer);
26679 name = cp_parser_id_expression (parser, /*template_p=*/false,
26680 /*check_dependency_p=*/true,
26681 /*template_p=*/NULL,
26682 /*declarator_p=*/false,
26683 /*optional_p=*/false);
26684 if (name == error_mark_node)
26685 goto skip_comma;
26686
26687 decl = cp_parser_lookup_name_simple (parser, name, token->location);
26688 if (decl == error_mark_node)
26689 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
26690 token->location);
26691 else if (kind != 0)
26692 {
26693 switch (kind)
26694 {
26695 case OMP_CLAUSE_MAP:
26696 case OMP_CLAUSE_FROM:
26697 case OMP_CLAUSE_TO:
26698 case OMP_CLAUSE_DEPEND:
26699 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26700 {
26701 tree low_bound = NULL_TREE, length = NULL_TREE;
26702
26703 parser->colon_corrects_to_scope_p = false;
26704 cp_lexer_consume_token (parser->lexer);
26705 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26706 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
26707 NULL);
26708 if (!colon)
26709 parser->colon_corrects_to_scope_p
26710 = saved_colon_corrects_to_scope_p;
26711 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
26712 length = integer_one_node;
26713 else
26714 {
26715 /* Look for `:'. */
26716 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26717 goto skip_comma;
26718 if (!cp_lexer_next_token_is (parser->lexer,
26719 CPP_CLOSE_SQUARE))
26720 length = cp_parser_expression (parser,
26721 /*cast_p=*/false,
26722 NULL);
26723 }
26724 /* Look for the closing `]'. */
26725 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
26726 RT_CLOSE_SQUARE))
26727 goto skip_comma;
26728 decl = tree_cons (low_bound, length, decl);
26729 }
26730 break;
26731 default:
26732 break;
26733 }
26734
26735 tree u = build_omp_clause (token->location, kind);
26736 OMP_CLAUSE_DECL (u) = decl;
26737 OMP_CLAUSE_CHAIN (u) = list;
26738 list = u;
26739 }
26740 else
26741 list = tree_cons (decl, NULL_TREE, list);
26742
26743 get_comma:
26744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26745 break;
26746 cp_lexer_consume_token (parser->lexer);
26747 }
26748
26749 if (colon)
26750 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26751
26752 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26753 {
26754 *colon = true;
26755 cp_parser_require (parser, CPP_COLON, RT_COLON);
26756 return list;
26757 }
26758
26759 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26760 {
26761 int ending;
26762
26763 /* Try to resync to an unnested comma. Copied from
26764 cp_parser_parenthesized_expression_list. */
26765 skip_comma:
26766 if (colon)
26767 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26768 ending = cp_parser_skip_to_closing_parenthesis (parser,
26769 /*recovering=*/true,
26770 /*or_comma=*/true,
26771 /*consume_paren=*/true);
26772 if (ending < 0)
26773 goto get_comma;
26774 }
26775
26776 return list;
26777 }
26778
26779 /* Similarly, but expect leading and trailing parenthesis. This is a very
26780 common case for omp clauses. */
26781
26782 static tree
26783 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
26784 {
26785 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26786 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
26787 return list;
26788 }
26789
26790 /* OpenMP 3.0:
26791 collapse ( constant-expression ) */
26792
26793 static tree
26794 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
26795 {
26796 tree c, num;
26797 location_t loc;
26798 HOST_WIDE_INT n;
26799
26800 loc = cp_lexer_peek_token (parser->lexer)->location;
26801 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26802 return list;
26803
26804 num = cp_parser_constant_expression (parser, false, NULL);
26805
26806 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26807 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26808 /*or_comma=*/false,
26809 /*consume_paren=*/true);
26810
26811 if (num == error_mark_node)
26812 return list;
26813 num = fold_non_dependent_expr (num);
26814 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
26815 || !host_integerp (num, 0)
26816 || (n = tree_low_cst (num, 0)) <= 0
26817 || (int) n != n)
26818 {
26819 error_at (loc, "collapse argument needs positive constant integer expression");
26820 return list;
26821 }
26822
26823 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
26824 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
26825 OMP_CLAUSE_CHAIN (c) = list;
26826 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
26827
26828 return c;
26829 }
26830
26831 /* OpenMP 2.5:
26832 default ( shared | none ) */
26833
26834 static tree
26835 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
26836 {
26837 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
26838 tree c;
26839
26840 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26841 return list;
26842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26843 {
26844 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26845 const char *p = IDENTIFIER_POINTER (id);
26846
26847 switch (p[0])
26848 {
26849 case 'n':
26850 if (strcmp ("none", p) != 0)
26851 goto invalid_kind;
26852 kind = OMP_CLAUSE_DEFAULT_NONE;
26853 break;
26854
26855 case 's':
26856 if (strcmp ("shared", p) != 0)
26857 goto invalid_kind;
26858 kind = OMP_CLAUSE_DEFAULT_SHARED;
26859 break;
26860
26861 default:
26862 goto invalid_kind;
26863 }
26864
26865 cp_lexer_consume_token (parser->lexer);
26866 }
26867 else
26868 {
26869 invalid_kind:
26870 cp_parser_error (parser, "expected %<none%> or %<shared%>");
26871 }
26872
26873 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26874 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26875 /*or_comma=*/false,
26876 /*consume_paren=*/true);
26877
26878 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
26879 return list;
26880
26881 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
26882 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
26883 OMP_CLAUSE_CHAIN (c) = list;
26884 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
26885
26886 return c;
26887 }
26888
26889 /* OpenMP 3.1:
26890 final ( expression ) */
26891
26892 static tree
26893 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
26894 {
26895 tree t, c;
26896
26897 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26898 return list;
26899
26900 t = cp_parser_condition (parser);
26901
26902 if (t == error_mark_node
26903 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26904 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26905 /*or_comma=*/false,
26906 /*consume_paren=*/true);
26907
26908 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
26909
26910 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
26911 OMP_CLAUSE_FINAL_EXPR (c) = t;
26912 OMP_CLAUSE_CHAIN (c) = list;
26913
26914 return c;
26915 }
26916
26917 /* OpenMP 2.5:
26918 if ( expression ) */
26919
26920 static tree
26921 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
26922 {
26923 tree t, c;
26924
26925 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26926 return list;
26927
26928 t = cp_parser_condition (parser);
26929
26930 if (t == error_mark_node
26931 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26932 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26933 /*or_comma=*/false,
26934 /*consume_paren=*/true);
26935
26936 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
26937
26938 c = build_omp_clause (location, OMP_CLAUSE_IF);
26939 OMP_CLAUSE_IF_EXPR (c) = t;
26940 OMP_CLAUSE_CHAIN (c) = list;
26941
26942 return c;
26943 }
26944
26945 /* OpenMP 3.1:
26946 mergeable */
26947
26948 static tree
26949 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
26950 tree list, location_t location)
26951 {
26952 tree c;
26953
26954 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
26955 location);
26956
26957 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
26958 OMP_CLAUSE_CHAIN (c) = list;
26959 return c;
26960 }
26961
26962 /* OpenMP 2.5:
26963 nowait */
26964
26965 static tree
26966 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
26967 tree list, location_t location)
26968 {
26969 tree c;
26970
26971 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
26972
26973 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
26974 OMP_CLAUSE_CHAIN (c) = list;
26975 return c;
26976 }
26977
26978 /* OpenMP 2.5:
26979 num_threads ( expression ) */
26980
26981 static tree
26982 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
26983 location_t location)
26984 {
26985 tree t, c;
26986
26987 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26988 return list;
26989
26990 t = cp_parser_expression (parser, false, NULL);
26991
26992 if (t == error_mark_node
26993 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26994 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26995 /*or_comma=*/false,
26996 /*consume_paren=*/true);
26997
26998 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
26999 "num_threads", location);
27000
27001 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27002 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27003 OMP_CLAUSE_CHAIN (c) = list;
27004
27005 return c;
27006 }
27007
27008 /* OpenMP 2.5:
27009 ordered */
27010
27011 static tree
27012 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27013 tree list, location_t location)
27014 {
27015 tree c;
27016
27017 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27018 "ordered", location);
27019
27020 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27021 OMP_CLAUSE_CHAIN (c) = list;
27022 return c;
27023 }
27024
27025 /* OpenMP 2.5:
27026 reduction ( reduction-operator : variable-list )
27027
27028 reduction-operator:
27029 One of: + * - & ^ | && ||
27030
27031 OpenMP 3.1:
27032
27033 reduction-operator:
27034 One of: + * - & ^ | && || min max
27035
27036 OpenMP 4.0:
27037
27038 reduction-operator:
27039 One of: + * - & ^ | && ||
27040 id-expression */
27041
27042 static tree
27043 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27044 {
27045 enum tree_code code = ERROR_MARK;
27046 tree nlist, c, id = NULL_TREE;
27047
27048 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27049 return list;
27050
27051 switch (cp_lexer_peek_token (parser->lexer)->type)
27052 {
27053 case CPP_PLUS: code = PLUS_EXPR; break;
27054 case CPP_MULT: code = MULT_EXPR; break;
27055 case CPP_MINUS: code = MINUS_EXPR; break;
27056 case CPP_AND: code = BIT_AND_EXPR; break;
27057 case CPP_XOR: code = BIT_XOR_EXPR; break;
27058 case CPP_OR: code = BIT_IOR_EXPR; break;
27059 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27060 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27061 default: break;
27062 }
27063
27064 if (code != ERROR_MARK)
27065 cp_lexer_consume_token (parser->lexer);
27066 else
27067 {
27068 bool saved_colon_corrects_to_scope_p;
27069 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27070 parser->colon_corrects_to_scope_p = false;
27071 id = cp_parser_id_expression (parser, /*template_p=*/false,
27072 /*check_dependency_p=*/true,
27073 /*template_p=*/NULL,
27074 /*declarator_p=*/false,
27075 /*optional_p=*/false);
27076 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27077 if (identifier_p (id))
27078 {
27079 const char *p = IDENTIFIER_POINTER (id);
27080
27081 if (strcmp (p, "min") == 0)
27082 code = MIN_EXPR;
27083 else if (strcmp (p, "max") == 0)
27084 code = MAX_EXPR;
27085 else if (id == ansi_opname (PLUS_EXPR))
27086 code = PLUS_EXPR;
27087 else if (id == ansi_opname (MULT_EXPR))
27088 code = MULT_EXPR;
27089 else if (id == ansi_opname (MINUS_EXPR))
27090 code = MINUS_EXPR;
27091 else if (id == ansi_opname (BIT_AND_EXPR))
27092 code = BIT_AND_EXPR;
27093 else if (id == ansi_opname (BIT_IOR_EXPR))
27094 code = BIT_IOR_EXPR;
27095 else if (id == ansi_opname (BIT_XOR_EXPR))
27096 code = BIT_XOR_EXPR;
27097 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27098 code = TRUTH_ANDIF_EXPR;
27099 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27100 code = TRUTH_ORIF_EXPR;
27101 id = omp_reduction_id (code, id, NULL_TREE);
27102 tree scope = parser->scope;
27103 if (scope)
27104 id = build_qualified_name (NULL_TREE, scope, id, false);
27105 parser->scope = NULL_TREE;
27106 parser->qualifying_scope = NULL_TREE;
27107 parser->object_scope = NULL_TREE;
27108 }
27109 else
27110 {
27111 error ("invalid reduction-identifier");
27112 resync_fail:
27113 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27114 /*or_comma=*/false,
27115 /*consume_paren=*/true);
27116 return list;
27117 }
27118 }
27119
27120 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27121 goto resync_fail;
27122
27123 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27124 NULL);
27125 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27126 {
27127 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27128 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27129 }
27130
27131 return nlist;
27132 }
27133
27134 /* OpenMP 2.5:
27135 schedule ( schedule-kind )
27136 schedule ( schedule-kind , expression )
27137
27138 schedule-kind:
27139 static | dynamic | guided | runtime | auto */
27140
27141 static tree
27142 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27143 {
27144 tree c, t;
27145
27146 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27147 return list;
27148
27149 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27150
27151 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27152 {
27153 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27154 const char *p = IDENTIFIER_POINTER (id);
27155
27156 switch (p[0])
27157 {
27158 case 'd':
27159 if (strcmp ("dynamic", p) != 0)
27160 goto invalid_kind;
27161 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27162 break;
27163
27164 case 'g':
27165 if (strcmp ("guided", p) != 0)
27166 goto invalid_kind;
27167 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27168 break;
27169
27170 case 'r':
27171 if (strcmp ("runtime", p) != 0)
27172 goto invalid_kind;
27173 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27174 break;
27175
27176 default:
27177 goto invalid_kind;
27178 }
27179 }
27180 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27181 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27182 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27183 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27184 else
27185 goto invalid_kind;
27186 cp_lexer_consume_token (parser->lexer);
27187
27188 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27189 {
27190 cp_token *token;
27191 cp_lexer_consume_token (parser->lexer);
27192
27193 token = cp_lexer_peek_token (parser->lexer);
27194 t = cp_parser_assignment_expression (parser, false, NULL);
27195
27196 if (t == error_mark_node)
27197 goto resync_fail;
27198 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27199 error_at (token->location, "schedule %<runtime%> does not take "
27200 "a %<chunk_size%> parameter");
27201 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27202 error_at (token->location, "schedule %<auto%> does not take "
27203 "a %<chunk_size%> parameter");
27204 else
27205 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27206
27207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27208 goto resync_fail;
27209 }
27210 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27211 goto resync_fail;
27212
27213 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27214 OMP_CLAUSE_CHAIN (c) = list;
27215 return c;
27216
27217 invalid_kind:
27218 cp_parser_error (parser, "invalid schedule kind");
27219 resync_fail:
27220 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27221 /*or_comma=*/false,
27222 /*consume_paren=*/true);
27223 return list;
27224 }
27225
27226 /* OpenMP 3.0:
27227 untied */
27228
27229 static tree
27230 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27231 tree list, location_t location)
27232 {
27233 tree c;
27234
27235 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27236
27237 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27238 OMP_CLAUSE_CHAIN (c) = list;
27239 return c;
27240 }
27241
27242 /* OpenMP 4.0:
27243 inbranch
27244 notinbranch */
27245
27246 static tree
27247 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27248 tree list, location_t location)
27249 {
27250 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27251 tree c = build_omp_clause (location, code);
27252 OMP_CLAUSE_CHAIN (c) = list;
27253 return c;
27254 }
27255
27256 /* OpenMP 4.0:
27257 parallel
27258 for
27259 sections
27260 taskgroup */
27261
27262 static tree
27263 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27264 enum omp_clause_code code,
27265 tree list, location_t location)
27266 {
27267 tree c = build_omp_clause (location, code);
27268 OMP_CLAUSE_CHAIN (c) = list;
27269 return c;
27270 }
27271
27272 /* OpenMP 4.0:
27273 num_teams ( expression ) */
27274
27275 static tree
27276 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27277 location_t location)
27278 {
27279 tree t, c;
27280
27281 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27282 return list;
27283
27284 t = cp_parser_expression (parser, false, NULL);
27285
27286 if (t == error_mark_node
27287 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27288 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27289 /*or_comma=*/false,
27290 /*consume_paren=*/true);
27291
27292 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27293 "num_teams", location);
27294
27295 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27296 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27297 OMP_CLAUSE_CHAIN (c) = list;
27298
27299 return c;
27300 }
27301
27302 /* OpenMP 4.0:
27303 thread_limit ( expression ) */
27304
27305 static tree
27306 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27307 location_t location)
27308 {
27309 tree t, c;
27310
27311 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27312 return list;
27313
27314 t = cp_parser_expression (parser, false, NULL);
27315
27316 if (t == error_mark_node
27317 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27318 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27319 /*or_comma=*/false,
27320 /*consume_paren=*/true);
27321
27322 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27323 "thread_limit", location);
27324
27325 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27326 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27327 OMP_CLAUSE_CHAIN (c) = list;
27328
27329 return c;
27330 }
27331
27332 /* OpenMP 4.0:
27333 aligned ( variable-list )
27334 aligned ( variable-list : constant-expression ) */
27335
27336 static tree
27337 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27338 {
27339 tree nlist, c, alignment = NULL_TREE;
27340 bool colon;
27341
27342 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27343 return list;
27344
27345 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27346 &colon);
27347
27348 if (colon)
27349 {
27350 alignment = cp_parser_constant_expression (parser, false, NULL);
27351
27352 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27353 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27354 /*or_comma=*/false,
27355 /*consume_paren=*/true);
27356
27357 if (alignment == error_mark_node)
27358 alignment = NULL_TREE;
27359 }
27360
27361 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27362 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27363
27364 return nlist;
27365 }
27366
27367 /* OpenMP 4.0:
27368 linear ( variable-list )
27369 linear ( variable-list : expression ) */
27370
27371 static tree
27372 cp_parser_omp_clause_linear (cp_parser *parser, tree list)
27373 {
27374 tree nlist, c, step = integer_one_node;
27375 bool colon;
27376
27377 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27378 return list;
27379
27380 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27381 &colon);
27382
27383 if (colon)
27384 {
27385 step = cp_parser_expression (parser, false, NULL);
27386
27387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27388 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27389 /*or_comma=*/false,
27390 /*consume_paren=*/true);
27391
27392 if (step == error_mark_node)
27393 return list;
27394 }
27395
27396 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27397 OMP_CLAUSE_LINEAR_STEP (c) = step;
27398
27399 return nlist;
27400 }
27401
27402 /* OpenMP 4.0:
27403 safelen ( constant-expression ) */
27404
27405 static tree
27406 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27407 location_t location)
27408 {
27409 tree t, c;
27410
27411 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27412 return list;
27413
27414 t = cp_parser_constant_expression (parser, false, NULL);
27415
27416 if (t == error_mark_node
27417 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27418 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27419 /*or_comma=*/false,
27420 /*consume_paren=*/true);
27421
27422 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27423
27424 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27425 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27426 OMP_CLAUSE_CHAIN (c) = list;
27427
27428 return c;
27429 }
27430
27431 /* OpenMP 4.0:
27432 simdlen ( constant-expression ) */
27433
27434 static tree
27435 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27436 location_t location)
27437 {
27438 tree t, c;
27439
27440 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27441 return list;
27442
27443 t = cp_parser_constant_expression (parser, false, NULL);
27444
27445 if (t == error_mark_node
27446 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27448 /*or_comma=*/false,
27449 /*consume_paren=*/true);
27450
27451 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27452
27453 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27454 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27455 OMP_CLAUSE_CHAIN (c) = list;
27456
27457 return c;
27458 }
27459
27460 /* OpenMP 4.0:
27461 depend ( depend-kind : variable-list )
27462
27463 depend-kind:
27464 in | out | inout */
27465
27466 static tree
27467 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27468 {
27469 tree nlist, c;
27470 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27471
27472 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27473 return list;
27474
27475 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27476 {
27477 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27478 const char *p = IDENTIFIER_POINTER (id);
27479
27480 if (strcmp ("in", p) == 0)
27481 kind = OMP_CLAUSE_DEPEND_IN;
27482 else if (strcmp ("inout", p) == 0)
27483 kind = OMP_CLAUSE_DEPEND_INOUT;
27484 else if (strcmp ("out", p) == 0)
27485 kind = OMP_CLAUSE_DEPEND_OUT;
27486 else
27487 goto invalid_kind;
27488 }
27489 else
27490 goto invalid_kind;
27491
27492 cp_lexer_consume_token (parser->lexer);
27493 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27494 goto resync_fail;
27495
27496 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27497 NULL);
27498
27499 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27500 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27501
27502 return nlist;
27503
27504 invalid_kind:
27505 cp_parser_error (parser, "invalid depend kind");
27506 resync_fail:
27507 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27508 /*or_comma=*/false,
27509 /*consume_paren=*/true);
27510 return list;
27511 }
27512
27513 /* OpenMP 4.0:
27514 map ( map-kind : variable-list )
27515 map ( variable-list )
27516
27517 map-kind:
27518 alloc | to | from | tofrom */
27519
27520 static tree
27521 cp_parser_omp_clause_map (cp_parser *parser, tree list)
27522 {
27523 tree nlist, c;
27524 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
27525
27526 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27527 return list;
27528
27529 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
27530 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
27531 {
27532 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27533 const char *p = IDENTIFIER_POINTER (id);
27534
27535 if (strcmp ("alloc", p) == 0)
27536 kind = OMP_CLAUSE_MAP_ALLOC;
27537 else if (strcmp ("to", p) == 0)
27538 kind = OMP_CLAUSE_MAP_TO;
27539 else if (strcmp ("from", p) == 0)
27540 kind = OMP_CLAUSE_MAP_FROM;
27541 else if (strcmp ("tofrom", p) == 0)
27542 kind = OMP_CLAUSE_MAP_TOFROM;
27543 else
27544 {
27545 cp_parser_error (parser, "invalid map kind");
27546 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27547 /*or_comma=*/false,
27548 /*consume_paren=*/true);
27549 return list;
27550 }
27551 cp_lexer_consume_token (parser->lexer);
27552 cp_lexer_consume_token (parser->lexer);
27553 }
27554
27555 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
27556 NULL);
27557
27558 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27559 OMP_CLAUSE_MAP_KIND (c) = kind;
27560
27561 return nlist;
27562 }
27563
27564 /* OpenMP 4.0:
27565 device ( expression ) */
27566
27567 static tree
27568 cp_parser_omp_clause_device (cp_parser *parser, tree list,
27569 location_t location)
27570 {
27571 tree t, c;
27572
27573 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27574 return list;
27575
27576 t = cp_parser_expression (parser, false, NULL);
27577
27578 if (t == error_mark_node
27579 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27580 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27581 /*or_comma=*/false,
27582 /*consume_paren=*/true);
27583
27584 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
27585 "device", location);
27586
27587 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
27588 OMP_CLAUSE_DEVICE_ID (c) = t;
27589 OMP_CLAUSE_CHAIN (c) = list;
27590
27591 return c;
27592 }
27593
27594 /* OpenMP 4.0:
27595 dist_schedule ( static )
27596 dist_schedule ( static , expression ) */
27597
27598 static tree
27599 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
27600 location_t location)
27601 {
27602 tree c, t;
27603
27604 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27605 return list;
27606
27607 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
27608
27609 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27610 goto invalid_kind;
27611 cp_lexer_consume_token (parser->lexer);
27612
27613 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27614 {
27615 cp_lexer_consume_token (parser->lexer);
27616
27617 t = cp_parser_assignment_expression (parser, false, NULL);
27618
27619 if (t == error_mark_node)
27620 goto resync_fail;
27621 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
27622
27623 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27624 goto resync_fail;
27625 }
27626 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27627 goto resync_fail;
27628
27629 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
27630 location);
27631 OMP_CLAUSE_CHAIN (c) = list;
27632 return c;
27633
27634 invalid_kind:
27635 cp_parser_error (parser, "invalid dist_schedule kind");
27636 resync_fail:
27637 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27638 /*or_comma=*/false,
27639 /*consume_paren=*/true);
27640 return list;
27641 }
27642
27643 /* OpenMP 4.0:
27644 proc_bind ( proc-bind-kind )
27645
27646 proc-bind-kind:
27647 master | close | spread */
27648
27649 static tree
27650 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
27651 location_t location)
27652 {
27653 tree c;
27654 enum omp_clause_proc_bind_kind kind;
27655
27656 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27657 return list;
27658
27659 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27660 {
27661 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27662 const char *p = IDENTIFIER_POINTER (id);
27663
27664 if (strcmp ("master", p) == 0)
27665 kind = OMP_CLAUSE_PROC_BIND_MASTER;
27666 else if (strcmp ("close", p) == 0)
27667 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
27668 else if (strcmp ("spread", p) == 0)
27669 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
27670 else
27671 goto invalid_kind;
27672 }
27673 else
27674 goto invalid_kind;
27675
27676 cp_lexer_consume_token (parser->lexer);
27677 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27678 goto resync_fail;
27679
27680 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
27681 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
27682 location);
27683 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
27684 OMP_CLAUSE_CHAIN (c) = list;
27685 return c;
27686
27687 invalid_kind:
27688 cp_parser_error (parser, "invalid depend kind");
27689 resync_fail:
27690 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27691 /*or_comma=*/false,
27692 /*consume_paren=*/true);
27693 return list;
27694 }
27695
27696 /* Parse all OpenMP clauses. The set clauses allowed by the directive
27697 is a bitmask in MASK. Return the list of clauses found; the result
27698 of clause default goes in *pdefault. */
27699
27700 static tree
27701 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
27702 const char *where, cp_token *pragma_tok,
27703 bool finish_p = true)
27704 {
27705 tree clauses = NULL;
27706 bool first = true;
27707 cp_token *token = NULL;
27708
27709 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
27710 {
27711 pragma_omp_clause c_kind;
27712 const char *c_name;
27713 tree prev = clauses;
27714
27715 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27716 cp_lexer_consume_token (parser->lexer);
27717
27718 token = cp_lexer_peek_token (parser->lexer);
27719 c_kind = cp_parser_omp_clause_name (parser);
27720
27721 switch (c_kind)
27722 {
27723 case PRAGMA_OMP_CLAUSE_COLLAPSE:
27724 clauses = cp_parser_omp_clause_collapse (parser, clauses,
27725 token->location);
27726 c_name = "collapse";
27727 break;
27728 case PRAGMA_OMP_CLAUSE_COPYIN:
27729 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
27730 c_name = "copyin";
27731 break;
27732 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
27733 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
27734 clauses);
27735 c_name = "copyprivate";
27736 break;
27737 case PRAGMA_OMP_CLAUSE_DEFAULT:
27738 clauses = cp_parser_omp_clause_default (parser, clauses,
27739 token->location);
27740 c_name = "default";
27741 break;
27742 case PRAGMA_OMP_CLAUSE_FINAL:
27743 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
27744 c_name = "final";
27745 break;
27746 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
27747 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
27748 clauses);
27749 c_name = "firstprivate";
27750 break;
27751 case PRAGMA_OMP_CLAUSE_IF:
27752 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
27753 c_name = "if";
27754 break;
27755 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
27756 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
27757 clauses);
27758 c_name = "lastprivate";
27759 break;
27760 case PRAGMA_OMP_CLAUSE_MERGEABLE:
27761 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
27762 token->location);
27763 c_name = "mergeable";
27764 break;
27765 case PRAGMA_OMP_CLAUSE_NOWAIT:
27766 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
27767 c_name = "nowait";
27768 break;
27769 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
27770 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
27771 token->location);
27772 c_name = "num_threads";
27773 break;
27774 case PRAGMA_OMP_CLAUSE_ORDERED:
27775 clauses = cp_parser_omp_clause_ordered (parser, clauses,
27776 token->location);
27777 c_name = "ordered";
27778 break;
27779 case PRAGMA_OMP_CLAUSE_PRIVATE:
27780 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
27781 clauses);
27782 c_name = "private";
27783 break;
27784 case PRAGMA_OMP_CLAUSE_REDUCTION:
27785 clauses = cp_parser_omp_clause_reduction (parser, clauses);
27786 c_name = "reduction";
27787 break;
27788 case PRAGMA_OMP_CLAUSE_SCHEDULE:
27789 clauses = cp_parser_omp_clause_schedule (parser, clauses,
27790 token->location);
27791 c_name = "schedule";
27792 break;
27793 case PRAGMA_OMP_CLAUSE_SHARED:
27794 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
27795 clauses);
27796 c_name = "shared";
27797 break;
27798 case PRAGMA_OMP_CLAUSE_UNTIED:
27799 clauses = cp_parser_omp_clause_untied (parser, clauses,
27800 token->location);
27801 c_name = "untied";
27802 break;
27803 case PRAGMA_OMP_CLAUSE_INBRANCH:
27804 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
27805 clauses, token->location);
27806 c_name = "inbranch";
27807 break;
27808 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
27809 clauses = cp_parser_omp_clause_branch (parser,
27810 OMP_CLAUSE_NOTINBRANCH,
27811 clauses, token->location);
27812 c_name = "notinbranch";
27813 break;
27814 case PRAGMA_OMP_CLAUSE_PARALLEL:
27815 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
27816 clauses, token->location);
27817 c_name = "parallel";
27818 if (!first)
27819 {
27820 clause_not_first:
27821 error_at (token->location, "%qs must be the first clause of %qs",
27822 c_name, where);
27823 clauses = prev;
27824 }
27825 break;
27826 case PRAGMA_OMP_CLAUSE_FOR:
27827 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
27828 clauses, token->location);
27829 c_name = "for";
27830 if (!first)
27831 goto clause_not_first;
27832 break;
27833 case PRAGMA_OMP_CLAUSE_SECTIONS:
27834 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
27835 clauses, token->location);
27836 c_name = "sections";
27837 if (!first)
27838 goto clause_not_first;
27839 break;
27840 case PRAGMA_OMP_CLAUSE_TASKGROUP:
27841 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
27842 clauses, token->location);
27843 c_name = "taskgroup";
27844 if (!first)
27845 goto clause_not_first;
27846 break;
27847 case PRAGMA_OMP_CLAUSE_TO:
27848 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
27849 clauses);
27850 c_name = "to";
27851 break;
27852 case PRAGMA_OMP_CLAUSE_FROM:
27853 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
27854 clauses);
27855 c_name = "from";
27856 break;
27857 case PRAGMA_OMP_CLAUSE_UNIFORM:
27858 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
27859 clauses);
27860 c_name = "uniform";
27861 break;
27862 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
27863 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
27864 token->location);
27865 c_name = "num_teams";
27866 break;
27867 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
27868 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
27869 token->location);
27870 c_name = "thread_limit";
27871 break;
27872 case PRAGMA_OMP_CLAUSE_ALIGNED:
27873 clauses = cp_parser_omp_clause_aligned (parser, clauses);
27874 c_name = "aligned";
27875 break;
27876 case PRAGMA_OMP_CLAUSE_LINEAR:
27877 clauses = cp_parser_omp_clause_linear (parser, clauses);
27878 c_name = "linear";
27879 break;
27880 case PRAGMA_OMP_CLAUSE_DEPEND:
27881 clauses = cp_parser_omp_clause_depend (parser, clauses);
27882 c_name = "depend";
27883 break;
27884 case PRAGMA_OMP_CLAUSE_MAP:
27885 clauses = cp_parser_omp_clause_map (parser, clauses);
27886 c_name = "map";
27887 break;
27888 case PRAGMA_OMP_CLAUSE_DEVICE:
27889 clauses = cp_parser_omp_clause_device (parser, clauses,
27890 token->location);
27891 c_name = "device";
27892 break;
27893 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
27894 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
27895 token->location);
27896 c_name = "dist_schedule";
27897 break;
27898 case PRAGMA_OMP_CLAUSE_PROC_BIND:
27899 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
27900 token->location);
27901 c_name = "proc_bind";
27902 break;
27903 case PRAGMA_OMP_CLAUSE_SAFELEN:
27904 clauses = cp_parser_omp_clause_safelen (parser, clauses,
27905 token->location);
27906 c_name = "safelen";
27907 break;
27908 case PRAGMA_OMP_CLAUSE_SIMDLEN:
27909 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
27910 token->location);
27911 c_name = "simdlen";
27912 break;
27913 default:
27914 cp_parser_error (parser, "expected %<#pragma omp%> clause");
27915 goto saw_error;
27916 }
27917
27918 first = false;
27919
27920 if (((mask >> c_kind) & 1) == 0)
27921 {
27922 /* Remove the invalid clause(s) from the list to avoid
27923 confusing the rest of the compiler. */
27924 clauses = prev;
27925 error_at (token->location, "%qs is not valid for %qs", c_name, where);
27926 }
27927 }
27928 saw_error:
27929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27930 if (finish_p)
27931 return finish_omp_clauses (clauses);
27932 return clauses;
27933 }
27934
27935 /* OpenMP 2.5:
27936 structured-block:
27937 statement
27938
27939 In practice, we're also interested in adding the statement to an
27940 outer node. So it is convenient if we work around the fact that
27941 cp_parser_statement calls add_stmt. */
27942
27943 static unsigned
27944 cp_parser_begin_omp_structured_block (cp_parser *parser)
27945 {
27946 unsigned save = parser->in_statement;
27947
27948 /* Only move the values to IN_OMP_BLOCK if they weren't false.
27949 This preserves the "not within loop or switch" style error messages
27950 for nonsense cases like
27951 void foo() {
27952 #pragma omp single
27953 break;
27954 }
27955 */
27956 if (parser->in_statement)
27957 parser->in_statement = IN_OMP_BLOCK;
27958
27959 return save;
27960 }
27961
27962 static void
27963 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
27964 {
27965 parser->in_statement = save;
27966 }
27967
27968 static tree
27969 cp_parser_omp_structured_block (cp_parser *parser)
27970 {
27971 tree stmt = begin_omp_structured_block ();
27972 unsigned int save = cp_parser_begin_omp_structured_block (parser);
27973
27974 cp_parser_statement (parser, NULL_TREE, false, NULL);
27975
27976 cp_parser_end_omp_structured_block (parser, save);
27977 return finish_omp_structured_block (stmt);
27978 }
27979
27980 /* OpenMP 2.5:
27981 # pragma omp atomic new-line
27982 expression-stmt
27983
27984 expression-stmt:
27985 x binop= expr | x++ | ++x | x-- | --x
27986 binop:
27987 +, *, -, /, &, ^, |, <<, >>
27988
27989 where x is an lvalue expression with scalar type.
27990
27991 OpenMP 3.1:
27992 # pragma omp atomic new-line
27993 update-stmt
27994
27995 # pragma omp atomic read new-line
27996 read-stmt
27997
27998 # pragma omp atomic write new-line
27999 write-stmt
28000
28001 # pragma omp atomic update new-line
28002 update-stmt
28003
28004 # pragma omp atomic capture new-line
28005 capture-stmt
28006
28007 # pragma omp atomic capture new-line
28008 capture-block
28009
28010 read-stmt:
28011 v = x
28012 write-stmt:
28013 x = expr
28014 update-stmt:
28015 expression-stmt | x = x binop expr
28016 capture-stmt:
28017 v = expression-stmt
28018 capture-block:
28019 { v = x; update-stmt; } | { update-stmt; v = x; }
28020
28021 OpenMP 4.0:
28022 update-stmt:
28023 expression-stmt | x = x binop expr | x = expr binop x
28024 capture-stmt:
28025 v = update-stmt
28026 capture-block:
28027 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28028
28029 where x and v are lvalue expressions with scalar type. */
28030
28031 static void
28032 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28033 {
28034 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28035 tree rhs1 = NULL_TREE, orig_lhs;
28036 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28037 bool structured_block = false;
28038 bool seq_cst = false;
28039
28040 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28041 {
28042 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28043 const char *p = IDENTIFIER_POINTER (id);
28044
28045 if (!strcmp (p, "read"))
28046 code = OMP_ATOMIC_READ;
28047 else if (!strcmp (p, "write"))
28048 code = NOP_EXPR;
28049 else if (!strcmp (p, "update"))
28050 code = OMP_ATOMIC;
28051 else if (!strcmp (p, "capture"))
28052 code = OMP_ATOMIC_CAPTURE_NEW;
28053 else
28054 p = NULL;
28055 if (p)
28056 cp_lexer_consume_token (parser->lexer);
28057 }
28058
28059 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28060 {
28061 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28062 const char *p = IDENTIFIER_POINTER (id);
28063
28064 if (!strcmp (p, "seq_cst"))
28065 {
28066 seq_cst = true;
28067 cp_lexer_consume_token (parser->lexer);
28068 }
28069 }
28070 cp_parser_require_pragma_eol (parser, pragma_tok);
28071
28072 switch (code)
28073 {
28074 case OMP_ATOMIC_READ:
28075 case NOP_EXPR: /* atomic write */
28076 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28077 /*cast_p=*/false, NULL);
28078 if (v == error_mark_node)
28079 goto saw_error;
28080 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28081 goto saw_error;
28082 if (code == NOP_EXPR)
28083 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28084 else
28085 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28086 /*cast_p=*/false, NULL);
28087 if (lhs == error_mark_node)
28088 goto saw_error;
28089 if (code == NOP_EXPR)
28090 {
28091 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28092 opcode. */
28093 code = OMP_ATOMIC;
28094 rhs = lhs;
28095 lhs = v;
28096 v = NULL_TREE;
28097 }
28098 goto done;
28099 case OMP_ATOMIC_CAPTURE_NEW:
28100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28101 {
28102 cp_lexer_consume_token (parser->lexer);
28103 structured_block = true;
28104 }
28105 else
28106 {
28107 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28108 /*cast_p=*/false, NULL);
28109 if (v == error_mark_node)
28110 goto saw_error;
28111 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28112 goto saw_error;
28113 }
28114 default:
28115 break;
28116 }
28117
28118 restart:
28119 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28120 /*cast_p=*/false, NULL);
28121 orig_lhs = lhs;
28122 switch (TREE_CODE (lhs))
28123 {
28124 case ERROR_MARK:
28125 goto saw_error;
28126
28127 case POSTINCREMENT_EXPR:
28128 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28129 code = OMP_ATOMIC_CAPTURE_OLD;
28130 /* FALLTHROUGH */
28131 case PREINCREMENT_EXPR:
28132 lhs = TREE_OPERAND (lhs, 0);
28133 opcode = PLUS_EXPR;
28134 rhs = integer_one_node;
28135 break;
28136
28137 case POSTDECREMENT_EXPR:
28138 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28139 code = OMP_ATOMIC_CAPTURE_OLD;
28140 /* FALLTHROUGH */
28141 case PREDECREMENT_EXPR:
28142 lhs = TREE_OPERAND (lhs, 0);
28143 opcode = MINUS_EXPR;
28144 rhs = integer_one_node;
28145 break;
28146
28147 case COMPOUND_EXPR:
28148 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28149 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28150 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28151 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28152 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28153 (TREE_OPERAND (lhs, 1), 0), 0)))
28154 == BOOLEAN_TYPE)
28155 /* Undo effects of boolean_increment for post {in,de}crement. */
28156 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28157 /* FALLTHRU */
28158 case MODIFY_EXPR:
28159 if (TREE_CODE (lhs) == MODIFY_EXPR
28160 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28161 {
28162 /* Undo effects of boolean_increment. */
28163 if (integer_onep (TREE_OPERAND (lhs, 1)))
28164 {
28165 /* This is pre or post increment. */
28166 rhs = TREE_OPERAND (lhs, 1);
28167 lhs = TREE_OPERAND (lhs, 0);
28168 opcode = NOP_EXPR;
28169 if (code == OMP_ATOMIC_CAPTURE_NEW
28170 && !structured_block
28171 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28172 code = OMP_ATOMIC_CAPTURE_OLD;
28173 break;
28174 }
28175 }
28176 /* FALLTHRU */
28177 default:
28178 switch (cp_lexer_peek_token (parser->lexer)->type)
28179 {
28180 case CPP_MULT_EQ:
28181 opcode = MULT_EXPR;
28182 break;
28183 case CPP_DIV_EQ:
28184 opcode = TRUNC_DIV_EXPR;
28185 break;
28186 case CPP_PLUS_EQ:
28187 opcode = PLUS_EXPR;
28188 break;
28189 case CPP_MINUS_EQ:
28190 opcode = MINUS_EXPR;
28191 break;
28192 case CPP_LSHIFT_EQ:
28193 opcode = LSHIFT_EXPR;
28194 break;
28195 case CPP_RSHIFT_EQ:
28196 opcode = RSHIFT_EXPR;
28197 break;
28198 case CPP_AND_EQ:
28199 opcode = BIT_AND_EXPR;
28200 break;
28201 case CPP_OR_EQ:
28202 opcode = BIT_IOR_EXPR;
28203 break;
28204 case CPP_XOR_EQ:
28205 opcode = BIT_XOR_EXPR;
28206 break;
28207 case CPP_EQ:
28208 enum cp_parser_prec oprec;
28209 cp_token *token;
28210 cp_lexer_consume_token (parser->lexer);
28211 cp_parser_parse_tentatively (parser);
28212 rhs1 = cp_parser_simple_cast_expression (parser);
28213 if (rhs1 == error_mark_node)
28214 {
28215 cp_parser_abort_tentative_parse (parser);
28216 cp_parser_simple_cast_expression (parser);
28217 goto saw_error;
28218 }
28219 token = cp_lexer_peek_token (parser->lexer);
28220 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28221 {
28222 cp_parser_abort_tentative_parse (parser);
28223 cp_parser_parse_tentatively (parser);
28224 rhs = cp_parser_binary_expression (parser, false, true,
28225 PREC_NOT_OPERATOR, NULL);
28226 if (rhs == error_mark_node)
28227 {
28228 cp_parser_abort_tentative_parse (parser);
28229 cp_parser_binary_expression (parser, false, true,
28230 PREC_NOT_OPERATOR, NULL);
28231 goto saw_error;
28232 }
28233 switch (TREE_CODE (rhs))
28234 {
28235 case MULT_EXPR:
28236 case TRUNC_DIV_EXPR:
28237 case PLUS_EXPR:
28238 case MINUS_EXPR:
28239 case LSHIFT_EXPR:
28240 case RSHIFT_EXPR:
28241 case BIT_AND_EXPR:
28242 case BIT_IOR_EXPR:
28243 case BIT_XOR_EXPR:
28244 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28245 {
28246 if (cp_parser_parse_definitely (parser))
28247 {
28248 opcode = TREE_CODE (rhs);
28249 rhs1 = TREE_OPERAND (rhs, 0);
28250 rhs = TREE_OPERAND (rhs, 1);
28251 goto stmt_done;
28252 }
28253 else
28254 goto saw_error;
28255 }
28256 break;
28257 default:
28258 break;
28259 }
28260 cp_parser_abort_tentative_parse (parser);
28261 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28262 {
28263 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28264 if (rhs == error_mark_node)
28265 goto saw_error;
28266 opcode = NOP_EXPR;
28267 rhs1 = NULL_TREE;
28268 goto stmt_done;
28269 }
28270 cp_parser_error (parser,
28271 "invalid form of %<#pragma omp atomic%>");
28272 goto saw_error;
28273 }
28274 if (!cp_parser_parse_definitely (parser))
28275 goto saw_error;
28276 switch (token->type)
28277 {
28278 case CPP_SEMICOLON:
28279 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28280 {
28281 code = OMP_ATOMIC_CAPTURE_OLD;
28282 v = lhs;
28283 lhs = NULL_TREE;
28284 lhs1 = rhs1;
28285 rhs1 = NULL_TREE;
28286 cp_lexer_consume_token (parser->lexer);
28287 goto restart;
28288 }
28289 else if (structured_block)
28290 {
28291 opcode = NOP_EXPR;
28292 rhs = rhs1;
28293 rhs1 = NULL_TREE;
28294 goto stmt_done;
28295 }
28296 cp_parser_error (parser,
28297 "invalid form of %<#pragma omp atomic%>");
28298 goto saw_error;
28299 case CPP_MULT:
28300 opcode = MULT_EXPR;
28301 break;
28302 case CPP_DIV:
28303 opcode = TRUNC_DIV_EXPR;
28304 break;
28305 case CPP_PLUS:
28306 opcode = PLUS_EXPR;
28307 break;
28308 case CPP_MINUS:
28309 opcode = MINUS_EXPR;
28310 break;
28311 case CPP_LSHIFT:
28312 opcode = LSHIFT_EXPR;
28313 break;
28314 case CPP_RSHIFT:
28315 opcode = RSHIFT_EXPR;
28316 break;
28317 case CPP_AND:
28318 opcode = BIT_AND_EXPR;
28319 break;
28320 case CPP_OR:
28321 opcode = BIT_IOR_EXPR;
28322 break;
28323 case CPP_XOR:
28324 opcode = BIT_XOR_EXPR;
28325 break;
28326 default:
28327 cp_parser_error (parser,
28328 "invalid operator for %<#pragma omp atomic%>");
28329 goto saw_error;
28330 }
28331 oprec = TOKEN_PRECEDENCE (token);
28332 gcc_assert (oprec != PREC_NOT_OPERATOR);
28333 if (commutative_tree_code (opcode))
28334 oprec = (enum cp_parser_prec) (oprec - 1);
28335 cp_lexer_consume_token (parser->lexer);
28336 rhs = cp_parser_binary_expression (parser, false, false,
28337 oprec, NULL);
28338 if (rhs == error_mark_node)
28339 goto saw_error;
28340 goto stmt_done;
28341 /* FALLTHROUGH */
28342 default:
28343 cp_parser_error (parser,
28344 "invalid operator for %<#pragma omp atomic%>");
28345 goto saw_error;
28346 }
28347 cp_lexer_consume_token (parser->lexer);
28348
28349 rhs = cp_parser_expression (parser, false, NULL);
28350 if (rhs == error_mark_node)
28351 goto saw_error;
28352 break;
28353 }
28354 stmt_done:
28355 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28356 {
28357 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28358 goto saw_error;
28359 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28360 /*cast_p=*/false, NULL);
28361 if (v == error_mark_node)
28362 goto saw_error;
28363 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28364 goto saw_error;
28365 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28366 /*cast_p=*/false, NULL);
28367 if (lhs1 == error_mark_node)
28368 goto saw_error;
28369 }
28370 if (structured_block)
28371 {
28372 cp_parser_consume_semicolon_at_end_of_statement (parser);
28373 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28374 }
28375 done:
28376 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28377 if (!structured_block)
28378 cp_parser_consume_semicolon_at_end_of_statement (parser);
28379 return;
28380
28381 saw_error:
28382 cp_parser_skip_to_end_of_block_or_statement (parser);
28383 if (structured_block)
28384 {
28385 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28386 cp_lexer_consume_token (parser->lexer);
28387 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28388 {
28389 cp_parser_skip_to_end_of_block_or_statement (parser);
28390 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28391 cp_lexer_consume_token (parser->lexer);
28392 }
28393 }
28394 }
28395
28396
28397 /* OpenMP 2.5:
28398 # pragma omp barrier new-line */
28399
28400 static void
28401 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28402 {
28403 cp_parser_require_pragma_eol (parser, pragma_tok);
28404 finish_omp_barrier ();
28405 }
28406
28407 /* OpenMP 2.5:
28408 # pragma omp critical [(name)] new-line
28409 structured-block */
28410
28411 static tree
28412 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28413 {
28414 tree stmt, name = NULL;
28415
28416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28417 {
28418 cp_lexer_consume_token (parser->lexer);
28419
28420 name = cp_parser_identifier (parser);
28421
28422 if (name == error_mark_node
28423 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28424 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28425 /*or_comma=*/false,
28426 /*consume_paren=*/true);
28427 if (name == error_mark_node)
28428 name = NULL;
28429 }
28430 cp_parser_require_pragma_eol (parser, pragma_tok);
28431
28432 stmt = cp_parser_omp_structured_block (parser);
28433 return c_finish_omp_critical (input_location, stmt, name);
28434 }
28435
28436 /* OpenMP 2.5:
28437 # pragma omp flush flush-vars[opt] new-line
28438
28439 flush-vars:
28440 ( variable-list ) */
28441
28442 static void
28443 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28444 {
28445 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28446 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28447 cp_parser_require_pragma_eol (parser, pragma_tok);
28448
28449 finish_omp_flush ();
28450 }
28451
28452 /* Helper function, to parse omp for increment expression. */
28453
28454 static tree
28455 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
28456 {
28457 tree cond = cp_parser_binary_expression (parser, false, true,
28458 PREC_NOT_OPERATOR, NULL);
28459 if (cond == error_mark_node
28460 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28461 {
28462 cp_parser_skip_to_end_of_statement (parser);
28463 return error_mark_node;
28464 }
28465
28466 switch (TREE_CODE (cond))
28467 {
28468 case GT_EXPR:
28469 case GE_EXPR:
28470 case LT_EXPR:
28471 case LE_EXPR:
28472 break;
28473 default:
28474 return error_mark_node;
28475 }
28476
28477 /* If decl is an iterator, preserve LHS and RHS of the relational
28478 expr until finish_omp_for. */
28479 if (decl
28480 && (type_dependent_expression_p (decl)
28481 || CLASS_TYPE_P (TREE_TYPE (decl))))
28482 return cond;
28483
28484 return build_x_binary_op (input_location, TREE_CODE (cond),
28485 TREE_OPERAND (cond, 0), ERROR_MARK,
28486 TREE_OPERAND (cond, 1), ERROR_MARK,
28487 /*overload=*/NULL, tf_warning_or_error);
28488 }
28489
28490 /* Helper function, to parse omp for increment expression. */
28491
28492 static tree
28493 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
28494 {
28495 cp_token *token = cp_lexer_peek_token (parser->lexer);
28496 enum tree_code op;
28497 tree lhs, rhs;
28498 cp_id_kind idk;
28499 bool decl_first;
28500
28501 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28502 {
28503 op = (token->type == CPP_PLUS_PLUS
28504 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
28505 cp_lexer_consume_token (parser->lexer);
28506 lhs = cp_parser_simple_cast_expression (parser);
28507 if (lhs != decl)
28508 return error_mark_node;
28509 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28510 }
28511
28512 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
28513 if (lhs != decl)
28514 return error_mark_node;
28515
28516 token = cp_lexer_peek_token (parser->lexer);
28517 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28518 {
28519 op = (token->type == CPP_PLUS_PLUS
28520 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
28521 cp_lexer_consume_token (parser->lexer);
28522 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28523 }
28524
28525 op = cp_parser_assignment_operator_opt (parser);
28526 if (op == ERROR_MARK)
28527 return error_mark_node;
28528
28529 if (op != NOP_EXPR)
28530 {
28531 rhs = cp_parser_assignment_expression (parser, false, NULL);
28532 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
28533 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
28534 }
28535
28536 lhs = cp_parser_binary_expression (parser, false, false,
28537 PREC_ADDITIVE_EXPRESSION, NULL);
28538 token = cp_lexer_peek_token (parser->lexer);
28539 decl_first = lhs == decl;
28540 if (decl_first)
28541 lhs = NULL_TREE;
28542 if (token->type != CPP_PLUS
28543 && token->type != CPP_MINUS)
28544 return error_mark_node;
28545
28546 do
28547 {
28548 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
28549 cp_lexer_consume_token (parser->lexer);
28550 rhs = cp_parser_binary_expression (parser, false, false,
28551 PREC_ADDITIVE_EXPRESSION, NULL);
28552 token = cp_lexer_peek_token (parser->lexer);
28553 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
28554 {
28555 if (lhs == NULL_TREE)
28556 {
28557 if (op == PLUS_EXPR)
28558 lhs = rhs;
28559 else
28560 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
28561 tf_warning_or_error);
28562 }
28563 else
28564 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
28565 ERROR_MARK, NULL, tf_warning_or_error);
28566 }
28567 }
28568 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
28569
28570 if (!decl_first)
28571 {
28572 if (rhs != decl || op == MINUS_EXPR)
28573 return error_mark_node;
28574 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
28575 }
28576 else
28577 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
28578
28579 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
28580 }
28581
28582 /* Parse the restricted form of the for statement allowed by OpenMP. */
28583
28584 static tree
28585 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
28586 tree *cclauses)
28587 {
28588 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
28589 tree real_decl, initv, condv, incrv, declv;
28590 tree this_pre_body, cl;
28591 location_t loc_first;
28592 bool collapse_err = false;
28593 int i, collapse = 1, nbraces = 0;
28594 vec<tree, va_gc> *for_block = make_tree_vector ();
28595
28596 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
28597 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
28598 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
28599
28600 gcc_assert (collapse >= 1);
28601
28602 declv = make_tree_vec (collapse);
28603 initv = make_tree_vec (collapse);
28604 condv = make_tree_vec (collapse);
28605 incrv = make_tree_vec (collapse);
28606
28607 loc_first = cp_lexer_peek_token (parser->lexer)->location;
28608
28609 for (i = 0; i < collapse; i++)
28610 {
28611 int bracecount = 0;
28612 bool add_private_clause = false;
28613 location_t loc;
28614
28615 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28616 {
28617 cp_parser_error (parser, "for statement expected");
28618 return NULL;
28619 }
28620 loc = cp_lexer_consume_token (parser->lexer)->location;
28621
28622 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28623 return NULL;
28624
28625 init = decl = real_decl = NULL;
28626 this_pre_body = push_stmt_list ();
28627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28628 {
28629 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
28630
28631 init-expr:
28632 var = lb
28633 integer-type var = lb
28634 random-access-iterator-type var = lb
28635 pointer-type var = lb
28636 */
28637 cp_decl_specifier_seq type_specifiers;
28638
28639 /* First, try to parse as an initialized declaration. See
28640 cp_parser_condition, from whence the bulk of this is copied. */
28641
28642 cp_parser_parse_tentatively (parser);
28643 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
28644 /*is_trailing_return=*/false,
28645 &type_specifiers);
28646 if (cp_parser_parse_definitely (parser))
28647 {
28648 /* If parsing a type specifier seq succeeded, then this
28649 MUST be a initialized declaration. */
28650 tree asm_specification, attributes;
28651 cp_declarator *declarator;
28652
28653 declarator = cp_parser_declarator (parser,
28654 CP_PARSER_DECLARATOR_NAMED,
28655 /*ctor_dtor_or_conv_p=*/NULL,
28656 /*parenthesized_p=*/NULL,
28657 /*member_p=*/false);
28658 attributes = cp_parser_attributes_opt (parser);
28659 asm_specification = cp_parser_asm_specification_opt (parser);
28660
28661 if (declarator == cp_error_declarator)
28662 cp_parser_skip_to_end_of_statement (parser);
28663
28664 else
28665 {
28666 tree pushed_scope, auto_node;
28667
28668 decl = start_decl (declarator, &type_specifiers,
28669 SD_INITIALIZED, attributes,
28670 /*prefix_attributes=*/NULL_TREE,
28671 &pushed_scope);
28672
28673 auto_node = type_uses_auto (TREE_TYPE (decl));
28674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28675 {
28676 if (cp_lexer_next_token_is (parser->lexer,
28677 CPP_OPEN_PAREN))
28678 error ("parenthesized initialization is not allowed in "
28679 "OpenMP %<for%> loop");
28680 else
28681 /* Trigger an error. */
28682 cp_parser_require (parser, CPP_EQ, RT_EQ);
28683
28684 init = error_mark_node;
28685 cp_parser_skip_to_end_of_statement (parser);
28686 }
28687 else if (CLASS_TYPE_P (TREE_TYPE (decl))
28688 || type_dependent_expression_p (decl)
28689 || auto_node)
28690 {
28691 bool is_direct_init, is_non_constant_init;
28692
28693 init = cp_parser_initializer (parser,
28694 &is_direct_init,
28695 &is_non_constant_init);
28696
28697 if (auto_node)
28698 {
28699 TREE_TYPE (decl)
28700 = do_auto_deduction (TREE_TYPE (decl), init,
28701 auto_node);
28702
28703 if (!CLASS_TYPE_P (TREE_TYPE (decl))
28704 && !type_dependent_expression_p (decl))
28705 goto non_class;
28706 }
28707
28708 cp_finish_decl (decl, init, !is_non_constant_init,
28709 asm_specification,
28710 LOOKUP_ONLYCONVERTING);
28711 if (CLASS_TYPE_P (TREE_TYPE (decl)))
28712 {
28713 vec_safe_push (for_block, this_pre_body);
28714 init = NULL_TREE;
28715 }
28716 else
28717 init = pop_stmt_list (this_pre_body);
28718 this_pre_body = NULL_TREE;
28719 }
28720 else
28721 {
28722 /* Consume '='. */
28723 cp_lexer_consume_token (parser->lexer);
28724 init = cp_parser_assignment_expression (parser, false, NULL);
28725
28726 non_class:
28727 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
28728 init = error_mark_node;
28729 else
28730 cp_finish_decl (decl, NULL_TREE,
28731 /*init_const_expr_p=*/false,
28732 asm_specification,
28733 LOOKUP_ONLYCONVERTING);
28734 }
28735
28736 if (pushed_scope)
28737 pop_scope (pushed_scope);
28738 }
28739 }
28740 else
28741 {
28742 cp_id_kind idk;
28743 /* If parsing a type specifier sequence failed, then
28744 this MUST be a simple expression. */
28745 cp_parser_parse_tentatively (parser);
28746 decl = cp_parser_primary_expression (parser, false, false,
28747 false, &idk);
28748 if (!cp_parser_error_occurred (parser)
28749 && decl
28750 && DECL_P (decl)
28751 && CLASS_TYPE_P (TREE_TYPE (decl)))
28752 {
28753 tree rhs;
28754
28755 cp_parser_parse_definitely (parser);
28756 cp_parser_require (parser, CPP_EQ, RT_EQ);
28757 rhs = cp_parser_assignment_expression (parser, false, NULL);
28758 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
28759 decl, NOP_EXPR,
28760 rhs,
28761 tf_warning_or_error));
28762 add_private_clause = true;
28763 }
28764 else
28765 {
28766 decl = NULL;
28767 cp_parser_abort_tentative_parse (parser);
28768 init = cp_parser_expression (parser, false, NULL);
28769 if (init)
28770 {
28771 if (TREE_CODE (init) == MODIFY_EXPR
28772 || TREE_CODE (init) == MODOP_EXPR)
28773 real_decl = TREE_OPERAND (init, 0);
28774 }
28775 }
28776 }
28777 }
28778 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28779 if (this_pre_body)
28780 {
28781 this_pre_body = pop_stmt_list (this_pre_body);
28782 if (pre_body)
28783 {
28784 tree t = pre_body;
28785 pre_body = push_stmt_list ();
28786 add_stmt (t);
28787 add_stmt (this_pre_body);
28788 pre_body = pop_stmt_list (pre_body);
28789 }
28790 else
28791 pre_body = this_pre_body;
28792 }
28793
28794 if (decl)
28795 real_decl = decl;
28796 if (cclauses != NULL
28797 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
28798 && real_decl != NULL_TREE)
28799 {
28800 tree *c;
28801 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
28802 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
28803 && OMP_CLAUSE_DECL (*c) == real_decl)
28804 {
28805 error_at (loc, "iteration variable %qD"
28806 " should not be firstprivate", real_decl);
28807 *c = OMP_CLAUSE_CHAIN (*c);
28808 }
28809 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
28810 && OMP_CLAUSE_DECL (*c) == real_decl)
28811 {
28812 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
28813 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
28814 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
28815 OMP_CLAUSE_DECL (l) = real_decl;
28816 OMP_CLAUSE_CHAIN (l) = clauses;
28817 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
28818 clauses = l;
28819 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
28820 CP_OMP_CLAUSE_INFO (*c) = NULL;
28821 add_private_clause = false;
28822 }
28823 else
28824 {
28825 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
28826 && OMP_CLAUSE_DECL (*c) == real_decl)
28827 add_private_clause = false;
28828 c = &OMP_CLAUSE_CHAIN (*c);
28829 }
28830 }
28831
28832 if (add_private_clause)
28833 {
28834 tree c;
28835 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
28836 {
28837 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
28838 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
28839 && OMP_CLAUSE_DECL (c) == decl)
28840 break;
28841 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
28842 && OMP_CLAUSE_DECL (c) == decl)
28843 error_at (loc, "iteration variable %qD "
28844 "should not be firstprivate",
28845 decl);
28846 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
28847 && OMP_CLAUSE_DECL (c) == decl)
28848 error_at (loc, "iteration variable %qD should not be reduction",
28849 decl);
28850 }
28851 if (c == NULL)
28852 {
28853 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
28854 OMP_CLAUSE_DECL (c) = decl;
28855 c = finish_omp_clauses (c);
28856 if (c)
28857 {
28858 OMP_CLAUSE_CHAIN (c) = clauses;
28859 clauses = c;
28860 }
28861 }
28862 }
28863
28864 cond = NULL;
28865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28866 cond = cp_parser_omp_for_cond (parser, decl);
28867 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28868
28869 incr = NULL;
28870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28871 {
28872 /* If decl is an iterator, preserve the operator on decl
28873 until finish_omp_for. */
28874 if (real_decl
28875 && ((processing_template_decl
28876 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
28877 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
28878 incr = cp_parser_omp_for_incr (parser, real_decl);
28879 else
28880 incr = cp_parser_expression (parser, false, NULL);
28881 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
28882 SET_EXPR_LOCATION (incr, input_location);
28883 }
28884
28885 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28886 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28887 /*or_comma=*/false,
28888 /*consume_paren=*/true);
28889
28890 TREE_VEC_ELT (declv, i) = decl;
28891 TREE_VEC_ELT (initv, i) = init;
28892 TREE_VEC_ELT (condv, i) = cond;
28893 TREE_VEC_ELT (incrv, i) = incr;
28894
28895 if (i == collapse - 1)
28896 break;
28897
28898 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
28899 in between the collapsed for loops to be still considered perfectly
28900 nested. Hopefully the final version clarifies this.
28901 For now handle (multiple) {'s and empty statements. */
28902 cp_parser_parse_tentatively (parser);
28903 do
28904 {
28905 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28906 break;
28907 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28908 {
28909 cp_lexer_consume_token (parser->lexer);
28910 bracecount++;
28911 }
28912 else if (bracecount
28913 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28914 cp_lexer_consume_token (parser->lexer);
28915 else
28916 {
28917 loc = cp_lexer_peek_token (parser->lexer)->location;
28918 error_at (loc, "not enough collapsed for loops");
28919 collapse_err = true;
28920 cp_parser_abort_tentative_parse (parser);
28921 declv = NULL_TREE;
28922 break;
28923 }
28924 }
28925 while (1);
28926
28927 if (declv)
28928 {
28929 cp_parser_parse_definitely (parser);
28930 nbraces += bracecount;
28931 }
28932 }
28933
28934 /* Note that we saved the original contents of this flag when we entered
28935 the structured block, and so we don't need to re-save it here. */
28936 parser->in_statement = IN_OMP_FOR;
28937
28938 /* Note that the grammar doesn't call for a structured block here,
28939 though the loop as a whole is a structured block. */
28940 body = push_stmt_list ();
28941 cp_parser_statement (parser, NULL_TREE, false, NULL);
28942 body = pop_stmt_list (body);
28943
28944 if (declv == NULL_TREE)
28945 ret = NULL_TREE;
28946 else
28947 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
28948 pre_body, clauses);
28949
28950 while (nbraces)
28951 {
28952 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28953 {
28954 cp_lexer_consume_token (parser->lexer);
28955 nbraces--;
28956 }
28957 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28958 cp_lexer_consume_token (parser->lexer);
28959 else
28960 {
28961 if (!collapse_err)
28962 {
28963 error_at (cp_lexer_peek_token (parser->lexer)->location,
28964 "collapsed loops not perfectly nested");
28965 }
28966 collapse_err = true;
28967 cp_parser_statement_seq_opt (parser, NULL);
28968 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28969 break;
28970 }
28971 }
28972
28973 while (!for_block->is_empty ())
28974 add_stmt (pop_stmt_list (for_block->pop ()));
28975 release_tree_vector (for_block);
28976
28977 return ret;
28978 }
28979
28980 /* Helper function for OpenMP parsing, split clauses and call
28981 finish_omp_clauses on each of the set of clauses afterwards. */
28982
28983 static void
28984 cp_omp_split_clauses (location_t loc, enum tree_code code,
28985 omp_clause_mask mask, tree clauses, tree *cclauses)
28986 {
28987 int i;
28988 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
28989 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
28990 if (cclauses[i])
28991 cclauses[i] = finish_omp_clauses (cclauses[i]);
28992 }
28993
28994 /* OpenMP 4.0:
28995 #pragma omp simd simd-clause[optseq] new-line
28996 for-loop */
28997
28998 #define OMP_SIMD_CLAUSE_MASK \
28999 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29006
29007 static tree
29008 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29009 char *p_name, omp_clause_mask mask, tree *cclauses)
29010 {
29011 tree clauses, sb, ret;
29012 unsigned int save;
29013 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29014
29015 strcat (p_name, " simd");
29016 mask |= OMP_SIMD_CLAUSE_MASK;
29017 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29018
29019 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29020 cclauses == NULL);
29021 if (cclauses)
29022 {
29023 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29024 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29025 }
29026
29027 sb = begin_omp_structured_block ();
29028 save = cp_parser_begin_omp_structured_block (parser);
29029
29030 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29031
29032 cp_parser_end_omp_structured_block (parser, save);
29033 add_stmt (finish_omp_structured_block (sb));
29034
29035 return ret;
29036 }
29037
29038 /* OpenMP 2.5:
29039 #pragma omp for for-clause[optseq] new-line
29040 for-loop
29041
29042 OpenMP 4.0:
29043 #pragma omp for simd for-simd-clause[optseq] new-line
29044 for-loop */
29045
29046 #define OMP_FOR_CLAUSE_MASK \
29047 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29055
29056 static tree
29057 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29058 char *p_name, omp_clause_mask mask, tree *cclauses)
29059 {
29060 tree clauses, sb, ret;
29061 unsigned int save;
29062 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29063
29064 strcat (p_name, " for");
29065 mask |= OMP_FOR_CLAUSE_MASK;
29066 if (cclauses)
29067 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29068
29069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29070 {
29071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29072 const char *p = IDENTIFIER_POINTER (id);
29073
29074 if (strcmp (p, "simd") == 0)
29075 {
29076 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29077 if (cclauses == NULL)
29078 cclauses = cclauses_buf;
29079
29080 cp_lexer_consume_token (parser->lexer);
29081 sb = begin_omp_structured_block ();
29082 save = cp_parser_begin_omp_structured_block (parser);
29083 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29084 cclauses);
29085 cp_parser_end_omp_structured_block (parser, save);
29086 tree body = finish_omp_structured_block (sb);
29087 if (ret == NULL)
29088 return ret;
29089 ret = make_node (OMP_FOR);
29090 TREE_TYPE (ret) = void_type_node;
29091 OMP_FOR_BODY (ret) = body;
29092 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29093 SET_EXPR_LOCATION (ret, loc);
29094 add_stmt (ret);
29095 return ret;
29096 }
29097 }
29098
29099 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29100 cclauses == NULL);
29101 if (cclauses)
29102 {
29103 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29104 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29105 }
29106
29107 sb = begin_omp_structured_block ();
29108 save = cp_parser_begin_omp_structured_block (parser);
29109
29110 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29111
29112 cp_parser_end_omp_structured_block (parser, save);
29113 add_stmt (finish_omp_structured_block (sb));
29114
29115 return ret;
29116 }
29117
29118 /* OpenMP 2.5:
29119 # pragma omp master new-line
29120 structured-block */
29121
29122 static tree
29123 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29124 {
29125 cp_parser_require_pragma_eol (parser, pragma_tok);
29126 return c_finish_omp_master (input_location,
29127 cp_parser_omp_structured_block (parser));
29128 }
29129
29130 /* OpenMP 2.5:
29131 # pragma omp ordered new-line
29132 structured-block */
29133
29134 static tree
29135 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29136 {
29137 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29138 cp_parser_require_pragma_eol (parser, pragma_tok);
29139 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29140 }
29141
29142 /* OpenMP 2.5:
29143
29144 section-scope:
29145 { section-sequence }
29146
29147 section-sequence:
29148 section-directive[opt] structured-block
29149 section-sequence section-directive structured-block */
29150
29151 static tree
29152 cp_parser_omp_sections_scope (cp_parser *parser)
29153 {
29154 tree stmt, substmt;
29155 bool error_suppress = false;
29156 cp_token *tok;
29157
29158 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29159 return NULL_TREE;
29160
29161 stmt = push_stmt_list ();
29162
29163 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29164 {
29165 substmt = cp_parser_omp_structured_block (parser);
29166 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29167 add_stmt (substmt);
29168 }
29169
29170 while (1)
29171 {
29172 tok = cp_lexer_peek_token (parser->lexer);
29173 if (tok->type == CPP_CLOSE_BRACE)
29174 break;
29175 if (tok->type == CPP_EOF)
29176 break;
29177
29178 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29179 {
29180 cp_lexer_consume_token (parser->lexer);
29181 cp_parser_require_pragma_eol (parser, tok);
29182 error_suppress = false;
29183 }
29184 else if (!error_suppress)
29185 {
29186 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29187 error_suppress = true;
29188 }
29189
29190 substmt = cp_parser_omp_structured_block (parser);
29191 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29192 add_stmt (substmt);
29193 }
29194 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29195
29196 substmt = pop_stmt_list (stmt);
29197
29198 stmt = make_node (OMP_SECTIONS);
29199 TREE_TYPE (stmt) = void_type_node;
29200 OMP_SECTIONS_BODY (stmt) = substmt;
29201
29202 add_stmt (stmt);
29203 return stmt;
29204 }
29205
29206 /* OpenMP 2.5:
29207 # pragma omp sections sections-clause[optseq] newline
29208 sections-scope */
29209
29210 #define OMP_SECTIONS_CLAUSE_MASK \
29211 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29216
29217 static tree
29218 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29219 char *p_name, omp_clause_mask mask, tree *cclauses)
29220 {
29221 tree clauses, ret;
29222 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29223
29224 strcat (p_name, " sections");
29225 mask |= OMP_SECTIONS_CLAUSE_MASK;
29226 if (cclauses)
29227 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29228
29229 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29230 cclauses == NULL);
29231 if (cclauses)
29232 {
29233 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29234 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29235 }
29236
29237 ret = cp_parser_omp_sections_scope (parser);
29238 if (ret)
29239 OMP_SECTIONS_CLAUSES (ret) = clauses;
29240
29241 return ret;
29242 }
29243
29244 /* OpenMP 2.5:
29245 # pragma parallel parallel-clause new-line
29246 # pragma parallel for parallel-for-clause new-line
29247 # pragma parallel sections parallel-sections-clause new-line
29248
29249 OpenMP 4.0:
29250 # pragma parallel for simd parallel-for-simd-clause new-line */
29251
29252 #define OMP_PARALLEL_CLAUSE_MASK \
29253 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29262
29263 static tree
29264 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29265 char *p_name, omp_clause_mask mask, tree *cclauses)
29266 {
29267 tree stmt, clauses, block;
29268 unsigned int save;
29269 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29270
29271 strcat (p_name, " parallel");
29272 mask |= OMP_PARALLEL_CLAUSE_MASK;
29273
29274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29275 {
29276 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29277 if (cclauses == NULL)
29278 cclauses = cclauses_buf;
29279
29280 cp_lexer_consume_token (parser->lexer);
29281 block = begin_omp_parallel ();
29282 save = cp_parser_begin_omp_structured_block (parser);
29283 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29284 cp_parser_end_omp_structured_block (parser, save);
29285 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29286 block);
29287 OMP_PARALLEL_COMBINED (stmt) = 1;
29288 return stmt;
29289 }
29290 else if (cclauses)
29291 {
29292 error_at (loc, "expected %<for%> after %qs", p_name);
29293 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29294 return NULL_TREE;
29295 }
29296 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29297 {
29298 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29299 const char *p = IDENTIFIER_POINTER (id);
29300 if (strcmp (p, "sections") == 0)
29301 {
29302 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29303 cclauses = cclauses_buf;
29304
29305 cp_lexer_consume_token (parser->lexer);
29306 block = begin_omp_parallel ();
29307 save = cp_parser_begin_omp_structured_block (parser);
29308 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29309 cp_parser_end_omp_structured_block (parser, save);
29310 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29311 block);
29312 OMP_PARALLEL_COMBINED (stmt) = 1;
29313 return stmt;
29314 }
29315 }
29316
29317 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29318
29319 block = begin_omp_parallel ();
29320 save = cp_parser_begin_omp_structured_block (parser);
29321 cp_parser_statement (parser, NULL_TREE, false, NULL);
29322 cp_parser_end_omp_structured_block (parser, save);
29323 stmt = finish_omp_parallel (clauses, block);
29324 return stmt;
29325 }
29326
29327 /* OpenMP 2.5:
29328 # pragma omp single single-clause[optseq] new-line
29329 structured-block */
29330
29331 #define OMP_SINGLE_CLAUSE_MASK \
29332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29336
29337 static tree
29338 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29339 {
29340 tree stmt = make_node (OMP_SINGLE);
29341 TREE_TYPE (stmt) = void_type_node;
29342
29343 OMP_SINGLE_CLAUSES (stmt)
29344 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29345 "#pragma omp single", pragma_tok);
29346 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29347
29348 return add_stmt (stmt);
29349 }
29350
29351 /* OpenMP 3.0:
29352 # pragma omp task task-clause[optseq] new-line
29353 structured-block */
29354
29355 #define OMP_TASK_CLAUSE_MASK \
29356 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29365
29366 static tree
29367 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29368 {
29369 tree clauses, block;
29370 unsigned int save;
29371
29372 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29373 "#pragma omp task", pragma_tok);
29374 block = begin_omp_task ();
29375 save = cp_parser_begin_omp_structured_block (parser);
29376 cp_parser_statement (parser, NULL_TREE, false, NULL);
29377 cp_parser_end_omp_structured_block (parser, save);
29378 return finish_omp_task (clauses, block);
29379 }
29380
29381 /* OpenMP 3.0:
29382 # pragma omp taskwait new-line */
29383
29384 static void
29385 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29386 {
29387 cp_parser_require_pragma_eol (parser, pragma_tok);
29388 finish_omp_taskwait ();
29389 }
29390
29391 /* OpenMP 3.1:
29392 # pragma omp taskyield new-line */
29393
29394 static void
29395 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29396 {
29397 cp_parser_require_pragma_eol (parser, pragma_tok);
29398 finish_omp_taskyield ();
29399 }
29400
29401 /* OpenMP 4.0:
29402 # pragma omp taskgroup new-line
29403 structured-block */
29404
29405 static tree
29406 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29407 {
29408 cp_parser_require_pragma_eol (parser, pragma_tok);
29409 return c_finish_omp_taskgroup (input_location,
29410 cp_parser_omp_structured_block (parser));
29411 }
29412
29413
29414 /* OpenMP 2.5:
29415 # pragma omp threadprivate (variable-list) */
29416
29417 static void
29418 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
29419 {
29420 tree vars;
29421
29422 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29423 cp_parser_require_pragma_eol (parser, pragma_tok);
29424
29425 finish_omp_threadprivate (vars);
29426 }
29427
29428 /* OpenMP 4.0:
29429 # pragma omp cancel cancel-clause[optseq] new-line */
29430
29431 #define OMP_CANCEL_CLAUSE_MASK \
29432 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29437
29438 static void
29439 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
29440 {
29441 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
29442 "#pragma omp cancel", pragma_tok);
29443 finish_omp_cancel (clauses);
29444 }
29445
29446 /* OpenMP 4.0:
29447 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
29448
29449 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
29450 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
29454
29455 static void
29456 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
29457 {
29458 tree clauses;
29459 bool point_seen = false;
29460
29461 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29462 {
29463 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29464 const char *p = IDENTIFIER_POINTER (id);
29465
29466 if (strcmp (p, "point") == 0)
29467 {
29468 cp_lexer_consume_token (parser->lexer);
29469 point_seen = true;
29470 }
29471 }
29472 if (!point_seen)
29473 {
29474 cp_parser_error (parser, "expected %<point%>");
29475 cp_parser_require_pragma_eol (parser, pragma_tok);
29476 return;
29477 }
29478
29479 clauses = cp_parser_omp_all_clauses (parser,
29480 OMP_CANCELLATION_POINT_CLAUSE_MASK,
29481 "#pragma omp cancellation point",
29482 pragma_tok);
29483 finish_omp_cancellation_point (clauses);
29484 }
29485
29486 /* OpenMP 4.0:
29487 #pragma omp distribute distribute-clause[optseq] new-line
29488 for-loop */
29489
29490 #define OMP_DISTRIBUTE_CLAUSE_MASK \
29491 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
29494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29495
29496 static tree
29497 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
29498 char *p_name, omp_clause_mask mask, tree *cclauses)
29499 {
29500 tree clauses, sb, ret;
29501 unsigned int save;
29502 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29503
29504 strcat (p_name, " distribute");
29505 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
29506
29507 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29508 {
29509 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29510 const char *p = IDENTIFIER_POINTER (id);
29511 bool simd = false;
29512 bool parallel = false;
29513
29514 if (strcmp (p, "simd") == 0)
29515 simd = true;
29516 else
29517 parallel = strcmp (p, "parallel") == 0;
29518 if (parallel || simd)
29519 {
29520 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29521 if (cclauses == NULL)
29522 cclauses = cclauses_buf;
29523 cp_lexer_consume_token (parser->lexer);
29524 sb = begin_omp_structured_block ();
29525 save = cp_parser_begin_omp_structured_block (parser);
29526 if (simd)
29527 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29528 cclauses);
29529 else
29530 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
29531 cclauses);
29532 cp_parser_end_omp_structured_block (parser, save);
29533 tree body = finish_omp_structured_block (sb);
29534 if (ret == NULL)
29535 return ret;
29536 ret = make_node (OMP_DISTRIBUTE);
29537 TREE_TYPE (ret) = void_type_node;
29538 OMP_FOR_BODY (ret) = body;
29539 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
29540 SET_EXPR_LOCATION (ret, loc);
29541 add_stmt (ret);
29542 return ret;
29543 }
29544 }
29545
29546 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29547 cclauses == NULL);
29548 if (cclauses)
29549 {
29550 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
29551 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
29552 }
29553
29554 sb = begin_omp_structured_block ();
29555 save = cp_parser_begin_omp_structured_block (parser);
29556
29557 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
29558
29559 cp_parser_end_omp_structured_block (parser, save);
29560 add_stmt (finish_omp_structured_block (sb));
29561
29562 return ret;
29563 }
29564
29565 /* OpenMP 4.0:
29566 # pragma omp teams teams-clause[optseq] new-line
29567 structured-block */
29568
29569 #define OMP_TEAMS_CLAUSE_MASK \
29570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
29575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
29576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
29577
29578 static tree
29579 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
29580 char *p_name, omp_clause_mask mask, tree *cclauses)
29581 {
29582 tree clauses, sb, ret;
29583 unsigned int save;
29584 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29585
29586 strcat (p_name, " teams");
29587 mask |= OMP_TEAMS_CLAUSE_MASK;
29588
29589 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29590 {
29591 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29592 const char *p = IDENTIFIER_POINTER (id);
29593 if (strcmp (p, "distribute") == 0)
29594 {
29595 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29596 if (cclauses == NULL)
29597 cclauses = cclauses_buf;
29598
29599 cp_lexer_consume_token (parser->lexer);
29600 sb = begin_omp_structured_block ();
29601 save = cp_parser_begin_omp_structured_block (parser);
29602 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
29603 cclauses);
29604 cp_parser_end_omp_structured_block (parser, save);
29605 tree body = finish_omp_structured_block (sb);
29606 if (ret == NULL)
29607 return ret;
29608 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
29609 ret = make_node (OMP_TEAMS);
29610 TREE_TYPE (ret) = void_type_node;
29611 OMP_TEAMS_CLAUSES (ret) = clauses;
29612 OMP_TEAMS_BODY (ret) = body;
29613 return add_stmt (ret);
29614 }
29615 }
29616
29617 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29618 cclauses == NULL);
29619 if (cclauses)
29620 {
29621 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
29622 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
29623 }
29624
29625 tree stmt = make_node (OMP_TEAMS);
29626 TREE_TYPE (stmt) = void_type_node;
29627 OMP_TEAMS_CLAUSES (stmt) = clauses;
29628 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
29629
29630 return add_stmt (stmt);
29631 }
29632
29633 /* OpenMP 4.0:
29634 # pragma omp target data target-data-clause[optseq] new-line
29635 structured-block */
29636
29637 #define OMP_TARGET_DATA_CLAUSE_MASK \
29638 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29641
29642 static tree
29643 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
29644 {
29645 tree stmt = make_node (OMP_TARGET_DATA);
29646 TREE_TYPE (stmt) = void_type_node;
29647
29648 OMP_TARGET_DATA_CLAUSES (stmt)
29649 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
29650 "#pragma omp target data", pragma_tok);
29651 keep_next_level (true);
29652 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
29653
29654 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29655 return add_stmt (stmt);
29656 }
29657
29658 /* OpenMP 4.0:
29659 # pragma omp target update target-update-clause[optseq] new-line */
29660
29661 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
29662 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
29663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
29664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29666
29667 static bool
29668 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
29669 enum pragma_context context)
29670 {
29671 if (context == pragma_stmt)
29672 {
29673 error_at (pragma_tok->location,
29674 "%<#pragma omp target update%> may only be "
29675 "used in compound statements");
29676 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29677 return false;
29678 }
29679
29680 tree clauses
29681 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
29682 "#pragma omp target update", pragma_tok);
29683 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
29684 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
29685 {
29686 error_at (pragma_tok->location,
29687 "%<#pragma omp target update must contain at least one "
29688 "%<from%> or %<to%> clauses");
29689 return false;
29690 }
29691
29692 tree stmt = make_node (OMP_TARGET_UPDATE);
29693 TREE_TYPE (stmt) = void_type_node;
29694 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
29695 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29696 add_stmt (stmt);
29697 return false;
29698 }
29699
29700 /* OpenMP 4.0:
29701 # pragma omp target target-clause[optseq] new-line
29702 structured-block */
29703
29704 #define OMP_TARGET_CLAUSE_MASK \
29705 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29708
29709 static bool
29710 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
29711 enum pragma_context context)
29712 {
29713 if (context != pragma_stmt && context != pragma_compound)
29714 {
29715 cp_parser_error (parser, "expected declaration specifiers");
29716 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29717 return false;
29718 }
29719
29720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29721 {
29722 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29723 const char *p = IDENTIFIER_POINTER (id);
29724
29725 if (strcmp (p, "data") == 0)
29726 {
29727 cp_lexer_consume_token (parser->lexer);
29728 cp_parser_omp_target_data (parser, pragma_tok);
29729 return true;
29730 }
29731 else if (strcmp (p, "update") == 0)
29732 {
29733 cp_lexer_consume_token (parser->lexer);
29734 return cp_parser_omp_target_update (parser, pragma_tok, context);
29735 }
29736 else if (strcmp (p, "teams") == 0)
29737 {
29738 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
29739 char p_name[sizeof ("#pragma omp target teams distribute "
29740 "parallel for simd")];
29741
29742 cp_lexer_consume_token (parser->lexer);
29743 strcpy (p_name, "#pragma omp target");
29744 keep_next_level (true);
29745 tree sb = begin_omp_structured_block ();
29746 unsigned save = cp_parser_begin_omp_structured_block (parser);
29747 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
29748 OMP_TARGET_CLAUSE_MASK, 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 tree stmt = make_node (OMP_TARGET);
29754 TREE_TYPE (stmt) = void_type_node;
29755 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
29756 OMP_TARGET_BODY (stmt) = body;
29757 add_stmt (stmt);
29758 return true;
29759 }
29760 }
29761
29762 tree stmt = make_node (OMP_TARGET);
29763 TREE_TYPE (stmt) = void_type_node;
29764
29765 OMP_TARGET_CLAUSES (stmt)
29766 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
29767 "#pragma omp target", pragma_tok);
29768 keep_next_level (true);
29769 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
29770
29771 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29772 add_stmt (stmt);
29773 return true;
29774 }
29775
29776 /* OpenMP 4.0:
29777 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
29778
29779 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
29780 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
29781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
29784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
29785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
29786
29787 static void
29788 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
29789 enum pragma_context context)
29790 {
29791 bool first_p = parser->omp_declare_simd == NULL;
29792 cp_omp_declare_simd_data data;
29793 if (first_p)
29794 {
29795 data.error_seen = false;
29796 data.fndecl_seen = false;
29797 data.tokens = vNULL;
29798 parser->omp_declare_simd = &data;
29799 }
29800 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
29801 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
29802 cp_lexer_consume_token (parser->lexer);
29803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29804 parser->omp_declare_simd->error_seen = true;
29805 cp_parser_require_pragma_eol (parser, pragma_tok);
29806 struct cp_token_cache *cp
29807 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
29808 parser->omp_declare_simd->tokens.safe_push (cp);
29809 if (first_p)
29810 {
29811 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
29812 cp_parser_pragma (parser, context);
29813 switch (context)
29814 {
29815 case pragma_external:
29816 cp_parser_declaration (parser);
29817 break;
29818 case pragma_member:
29819 cp_parser_member_declaration (parser);
29820 break;
29821 case pragma_objc_icode:
29822 cp_parser_block_declaration (parser, /*statement_p=*/false);
29823 break;
29824 default:
29825 cp_parser_declaration_statement (parser);
29826 break;
29827 }
29828 if (parser->omp_declare_simd
29829 && !parser->omp_declare_simd->error_seen
29830 && !parser->omp_declare_simd->fndecl_seen)
29831 error_at (pragma_tok->location,
29832 "%<#pragma omp declare simd%> not immediately followed by "
29833 "function declaration or definition");
29834 data.tokens.release ();
29835 parser->omp_declare_simd = NULL;
29836 }
29837 }
29838
29839 /* Finalize #pragma omp declare simd clauses after direct declarator has
29840 been parsed, and put that into "omp declare simd" attribute. */
29841
29842 static tree
29843 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
29844 {
29845 struct cp_token_cache *ce;
29846 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
29847 int i;
29848
29849 if (!data->error_seen && data->fndecl_seen)
29850 {
29851 error ("%<#pragma omp declare simd%> not immediately followed by "
29852 "a single function declaration or definition");
29853 data->error_seen = true;
29854 return attrs;
29855 }
29856 if (data->error_seen)
29857 return attrs;
29858
29859 FOR_EACH_VEC_ELT (data->tokens, i, ce)
29860 {
29861 tree c, cl;
29862
29863 cp_parser_push_lexer_for_tokens (parser, ce);
29864 parser->lexer->in_pragma = true;
29865 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
29866 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
29867 cp_lexer_consume_token (parser->lexer);
29868 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
29869 "#pragma omp declare simd", pragma_tok);
29870 cp_parser_pop_lexer (parser);
29871 if (cl)
29872 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
29873 c = build_tree_list (get_identifier ("omp declare simd"), cl);
29874 TREE_CHAIN (c) = attrs;
29875 if (processing_template_decl)
29876 ATTR_IS_DEPENDENT (c) = 1;
29877 attrs = c;
29878 }
29879
29880 data->fndecl_seen = true;
29881 return attrs;
29882 }
29883
29884
29885 /* OpenMP 4.0:
29886 # pragma omp declare target new-line
29887 declarations and definitions
29888 # pragma omp end declare target new-line */
29889
29890 static void
29891 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
29892 {
29893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29894 scope_chain->omp_declare_target_attribute++;
29895 }
29896
29897 static void
29898 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
29899 {
29900 const char *p = "";
29901 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29902 {
29903 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29904 p = IDENTIFIER_POINTER (id);
29905 }
29906 if (strcmp (p, "declare") == 0)
29907 {
29908 cp_lexer_consume_token (parser->lexer);
29909 p = "";
29910 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29911 {
29912 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29913 p = IDENTIFIER_POINTER (id);
29914 }
29915 if (strcmp (p, "target") == 0)
29916 cp_lexer_consume_token (parser->lexer);
29917 else
29918 {
29919 cp_parser_error (parser, "expected %<target%>");
29920 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29921 return;
29922 }
29923 }
29924 else
29925 {
29926 cp_parser_error (parser, "expected %<declare%>");
29927 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29928 return;
29929 }
29930 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29931 if (!scope_chain->omp_declare_target_attribute)
29932 error_at (pragma_tok->location,
29933 "%<#pragma omp end declare target%> without corresponding "
29934 "%<#pragma omp declare target%>");
29935 else
29936 scope_chain->omp_declare_target_attribute--;
29937 }
29938
29939 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
29940 expression and optional initializer clause of
29941 #pragma omp declare reduction. We store the expression(s) as
29942 either 3, 6 or 7 special statements inside of the artificial function's
29943 body. The first two statements are DECL_EXPRs for the artificial
29944 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
29945 expression that uses those variables.
29946 If there was any INITIALIZER clause, this is followed by further statements,
29947 the fourth and fifth statements are DECL_EXPRs for the artificial
29948 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
29949 constructor variant (first token after open paren is not omp_priv),
29950 then the sixth statement is a statement with the function call expression
29951 that uses the OMP_PRIV and optionally OMP_ORIG variable.
29952 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
29953 to initialize the OMP_PRIV artificial variable and there is seventh
29954 statement, a DECL_EXPR of the OMP_PRIV statement again. */
29955
29956 static bool
29957 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
29958 {
29959 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
29960 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
29961 type = TREE_TYPE (type);
29962 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
29963 DECL_ARTIFICIAL (omp_out) = 1;
29964 pushdecl (omp_out);
29965 add_decl_expr (omp_out);
29966 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
29967 DECL_ARTIFICIAL (omp_in) = 1;
29968 pushdecl (omp_in);
29969 add_decl_expr (omp_in);
29970 tree combiner;
29971 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
29972
29973 keep_next_level (true);
29974 tree block = begin_omp_structured_block ();
29975 combiner = cp_parser_expression (parser, false, NULL);
29976 finish_expr_stmt (combiner);
29977 block = finish_omp_structured_block (block);
29978 add_stmt (block);
29979
29980 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29981 return false;
29982
29983 const char *p = "";
29984 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29985 {
29986 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29987 p = IDENTIFIER_POINTER (id);
29988 }
29989
29990 if (strcmp (p, "initializer") == 0)
29991 {
29992 cp_lexer_consume_token (parser->lexer);
29993 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29994 return false;
29995
29996 p = "";
29997 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29998 {
29999 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30000 p = IDENTIFIER_POINTER (id);
30001 }
30002
30003 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30004 DECL_ARTIFICIAL (omp_priv) = 1;
30005 pushdecl (omp_priv);
30006 add_decl_expr (omp_priv);
30007 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30008 DECL_ARTIFICIAL (omp_orig) = 1;
30009 pushdecl (omp_orig);
30010 add_decl_expr (omp_orig);
30011
30012 keep_next_level (true);
30013 block = begin_omp_structured_block ();
30014
30015 bool ctor = false;
30016 if (strcmp (p, "omp_priv") == 0)
30017 {
30018 bool is_direct_init, is_non_constant_init;
30019 ctor = true;
30020 cp_lexer_consume_token (parser->lexer);
30021 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30022 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30023 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30024 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30025 == CPP_CLOSE_PAREN
30026 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30027 == CPP_CLOSE_PAREN))
30028 {
30029 finish_omp_structured_block (block);
30030 error ("invalid initializer clause");
30031 return false;
30032 }
30033 initializer = cp_parser_initializer (parser, &is_direct_init,
30034 &is_non_constant_init);
30035 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30036 NULL_TREE, LOOKUP_ONLYCONVERTING);
30037 }
30038 else
30039 {
30040 cp_parser_parse_tentatively (parser);
30041 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30042 /*check_dependency_p=*/true,
30043 /*template_p=*/NULL,
30044 /*declarator_p=*/false,
30045 /*optional_p=*/false);
30046 vec<tree, va_gc> *args;
30047 if (fn_name == error_mark_node
30048 || cp_parser_error_occurred (parser)
30049 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30050 || ((args = cp_parser_parenthesized_expression_list
30051 (parser, non_attr, /*cast_p=*/false,
30052 /*allow_expansion_p=*/true,
30053 /*non_constant_p=*/NULL)),
30054 cp_parser_error_occurred (parser)))
30055 {
30056 finish_omp_structured_block (block);
30057 cp_parser_abort_tentative_parse (parser);
30058 cp_parser_error (parser, "expected id-expression (arguments)");
30059 return false;
30060 }
30061 unsigned int i;
30062 tree arg;
30063 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30064 if (arg == omp_priv
30065 || (TREE_CODE (arg) == ADDR_EXPR
30066 && TREE_OPERAND (arg, 0) == omp_priv))
30067 break;
30068 cp_parser_abort_tentative_parse (parser);
30069 if (arg == NULL_TREE)
30070 error ("one of the initializer call arguments should be %<omp_priv%>"
30071 " or %<&omp_priv%>");
30072 initializer = cp_parser_postfix_expression (parser, false, false, false,
30073 false, NULL);
30074 finish_expr_stmt (initializer);
30075 }
30076
30077 block = finish_omp_structured_block (block);
30078 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30079 finish_expr_stmt (block);
30080
30081 if (ctor)
30082 add_decl_expr (omp_orig);
30083
30084 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30085 return false;
30086 }
30087
30088 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30089 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30090
30091 return true;
30092 }
30093
30094 /* OpenMP 4.0
30095 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30096 initializer-clause[opt] new-line
30097
30098 initializer-clause:
30099 initializer (omp_priv initializer)
30100 initializer (function-name (argument-list)) */
30101
30102 static void
30103 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30104 enum pragma_context)
30105 {
30106 vec<tree> types = vNULL;
30107 enum tree_code reduc_code = ERROR_MARK;
30108 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30109 unsigned int i;
30110 cp_token *first_token;
30111 cp_token_cache *cp;
30112 int errs;
30113
30114 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30115 goto fail;
30116
30117 switch (cp_lexer_peek_token (parser->lexer)->type)
30118 {
30119 case CPP_PLUS:
30120 reduc_code = PLUS_EXPR;
30121 break;
30122 case CPP_MULT:
30123 reduc_code = MULT_EXPR;
30124 break;
30125 case CPP_MINUS:
30126 reduc_code = MINUS_EXPR;
30127 break;
30128 case CPP_AND:
30129 reduc_code = BIT_AND_EXPR;
30130 break;
30131 case CPP_XOR:
30132 reduc_code = BIT_XOR_EXPR;
30133 break;
30134 case CPP_OR:
30135 reduc_code = BIT_IOR_EXPR;
30136 break;
30137 case CPP_AND_AND:
30138 reduc_code = TRUTH_ANDIF_EXPR;
30139 break;
30140 case CPP_OR_OR:
30141 reduc_code = TRUTH_ORIF_EXPR;
30142 break;
30143 case CPP_NAME:
30144 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30145 break;
30146 default:
30147 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30148 "%<|%>, %<&&%>, %<||%> or identifier");
30149 goto fail;
30150 }
30151
30152 if (reduc_code != ERROR_MARK)
30153 cp_lexer_consume_token (parser->lexer);
30154
30155 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30156 if (reduc_id == error_mark_node)
30157 goto fail;
30158
30159 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30160 goto fail;
30161
30162 /* Types may not be defined in declare reduction type list. */
30163 const char *saved_message;
30164 saved_message = parser->type_definition_forbidden_message;
30165 parser->type_definition_forbidden_message
30166 = G_("types may not be defined in declare reduction type list");
30167 bool saved_colon_corrects_to_scope_p;
30168 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30169 parser->colon_corrects_to_scope_p = false;
30170 bool saved_colon_doesnt_start_class_def_p;
30171 saved_colon_doesnt_start_class_def_p
30172 = parser->colon_doesnt_start_class_def_p;
30173 parser->colon_doesnt_start_class_def_p = true;
30174
30175 while (true)
30176 {
30177 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30178 type = cp_parser_type_id (parser);
30179 if (type == error_mark_node)
30180 ;
30181 else if (ARITHMETIC_TYPE_P (type)
30182 && (orig_reduc_id == NULL_TREE
30183 || (TREE_CODE (type) != COMPLEX_TYPE
30184 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30185 "min") == 0
30186 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30187 "max") == 0))))
30188 error_at (loc, "predeclared arithmetic type %qT in "
30189 "%<#pragma omp declare reduction%>", type);
30190 else if (TREE_CODE (type) == FUNCTION_TYPE
30191 || TREE_CODE (type) == METHOD_TYPE
30192 || TREE_CODE (type) == ARRAY_TYPE)
30193 error_at (loc, "function or array type %qT in "
30194 "%<#pragma omp declare reduction%>", type);
30195 else if (TREE_CODE (type) == REFERENCE_TYPE)
30196 error_at (loc, "reference type %qT in "
30197 "%<#pragma omp declare reduction%>", type);
30198 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30199 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30200 "%<#pragma omp declare reduction%>", type);
30201 else
30202 types.safe_push (type);
30203
30204 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30205 cp_lexer_consume_token (parser->lexer);
30206 else
30207 break;
30208 }
30209
30210 /* Restore the saved message. */
30211 parser->type_definition_forbidden_message = saved_message;
30212 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30213 parser->colon_doesnt_start_class_def_p
30214 = saved_colon_doesnt_start_class_def_p;
30215
30216 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30217 || types.is_empty ())
30218 {
30219 fail:
30220 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30221 types.release ();
30222 return;
30223 }
30224
30225 first_token = cp_lexer_peek_token (parser->lexer);
30226 cp = NULL;
30227 errs = errorcount;
30228 FOR_EACH_VEC_ELT (types, i, type)
30229 {
30230 tree fntype
30231 = build_function_type_list (void_type_node,
30232 cp_build_reference_type (type, false),
30233 NULL_TREE);
30234 tree this_reduc_id = reduc_id;
30235 if (!dependent_type_p (type))
30236 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30237 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30238 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30239 DECL_ARTIFICIAL (fndecl) = 1;
30240 DECL_EXTERNAL (fndecl) = 1;
30241 DECL_DECLARED_INLINE_P (fndecl) = 1;
30242 DECL_IGNORED_P (fndecl) = 1;
30243 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30244 DECL_ATTRIBUTES (fndecl)
30245 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30246 DECL_ATTRIBUTES (fndecl));
30247 if (processing_template_decl)
30248 fndecl = push_template_decl (fndecl);
30249 bool block_scope = false;
30250 tree block = NULL_TREE;
30251 if (current_function_decl)
30252 {
30253 block_scope = true;
30254 DECL_CONTEXT (fndecl) = global_namespace;
30255 if (!processing_template_decl)
30256 pushdecl (fndecl);
30257 }
30258 else if (current_class_type)
30259 {
30260 if (cp == NULL)
30261 {
30262 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30263 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30264 cp_lexer_consume_token (parser->lexer);
30265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30266 goto fail;
30267 cp = cp_token_cache_new (first_token,
30268 cp_lexer_peek_nth_token (parser->lexer,
30269 2));
30270 }
30271 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30272 finish_member_declaration (fndecl);
30273 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30274 DECL_PENDING_INLINE_P (fndecl) = 1;
30275 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30276 continue;
30277 }
30278 else
30279 {
30280 DECL_CONTEXT (fndecl) = current_namespace;
30281 pushdecl (fndecl);
30282 }
30283 if (!block_scope)
30284 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30285 else
30286 block = begin_omp_structured_block ();
30287 if (cp)
30288 {
30289 cp_parser_push_lexer_for_tokens (parser, cp);
30290 parser->lexer->in_pragma = true;
30291 }
30292 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30293 {
30294 if (!block_scope)
30295 finish_function (0);
30296 else
30297 DECL_CONTEXT (fndecl) = current_function_decl;
30298 if (cp)
30299 cp_parser_pop_lexer (parser);
30300 goto fail;
30301 }
30302 if (cp)
30303 cp_parser_pop_lexer (parser);
30304 if (!block_scope)
30305 finish_function (0);
30306 else
30307 {
30308 DECL_CONTEXT (fndecl) = current_function_decl;
30309 block = finish_omp_structured_block (block);
30310 if (TREE_CODE (block) == BIND_EXPR)
30311 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30312 else if (TREE_CODE (block) == STATEMENT_LIST)
30313 DECL_SAVED_TREE (fndecl) = block;
30314 if (processing_template_decl)
30315 add_decl_expr (fndecl);
30316 }
30317 cp_check_omp_declare_reduction (fndecl);
30318 if (cp == NULL && types.length () > 1)
30319 cp = cp_token_cache_new (first_token,
30320 cp_lexer_peek_nth_token (parser->lexer, 2));
30321 if (errs != errorcount)
30322 break;
30323 }
30324
30325 cp_parser_require_pragma_eol (parser, pragma_tok);
30326 types.release ();
30327 }
30328
30329 /* OpenMP 4.0
30330 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30331 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30332 initializer-clause[opt] new-line
30333 #pragma omp declare target new-line */
30334
30335 static void
30336 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
30337 enum pragma_context context)
30338 {
30339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30340 {
30341 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30342 const char *p = IDENTIFIER_POINTER (id);
30343
30344 if (strcmp (p, "simd") == 0)
30345 {
30346 cp_lexer_consume_token (parser->lexer);
30347 cp_parser_omp_declare_simd (parser, pragma_tok,
30348 context);
30349 return;
30350 }
30351 cp_ensure_no_omp_declare_simd (parser);
30352 if (strcmp (p, "reduction") == 0)
30353 {
30354 cp_lexer_consume_token (parser->lexer);
30355 cp_parser_omp_declare_reduction (parser, pragma_tok,
30356 context);
30357 return;
30358 }
30359 if (strcmp (p, "target") == 0)
30360 {
30361 cp_lexer_consume_token (parser->lexer);
30362 cp_parser_omp_declare_target (parser, pragma_tok);
30363 return;
30364 }
30365 }
30366 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
30367 "or %<target%>");
30368 cp_parser_require_pragma_eol (parser, pragma_tok);
30369 }
30370
30371 /* Main entry point to OpenMP statement pragmas. */
30372
30373 static void
30374 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
30375 {
30376 tree stmt;
30377 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
30378 omp_clause_mask mask (0);
30379
30380 switch (pragma_tok->pragma_kind)
30381 {
30382 case PRAGMA_OMP_ATOMIC:
30383 cp_parser_omp_atomic (parser, pragma_tok);
30384 return;
30385 case PRAGMA_OMP_CRITICAL:
30386 stmt = cp_parser_omp_critical (parser, pragma_tok);
30387 break;
30388 case PRAGMA_OMP_DISTRIBUTE:
30389 strcpy (p_name, "#pragma omp");
30390 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
30391 break;
30392 case PRAGMA_OMP_FOR:
30393 strcpy (p_name, "#pragma omp");
30394 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
30395 break;
30396 case PRAGMA_OMP_MASTER:
30397 stmt = cp_parser_omp_master (parser, pragma_tok);
30398 break;
30399 case PRAGMA_OMP_ORDERED:
30400 stmt = cp_parser_omp_ordered (parser, pragma_tok);
30401 break;
30402 case PRAGMA_OMP_PARALLEL:
30403 strcpy (p_name, "#pragma omp");
30404 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
30405 break;
30406 case PRAGMA_OMP_SECTIONS:
30407 strcpy (p_name, "#pragma omp");
30408 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
30409 break;
30410 case PRAGMA_OMP_SIMD:
30411 strcpy (p_name, "#pragma omp");
30412 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
30413 break;
30414 case PRAGMA_OMP_SINGLE:
30415 stmt = cp_parser_omp_single (parser, pragma_tok);
30416 break;
30417 case PRAGMA_OMP_TASK:
30418 stmt = cp_parser_omp_task (parser, pragma_tok);
30419 break;
30420 case PRAGMA_OMP_TASKGROUP:
30421 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
30422 break;
30423 case PRAGMA_OMP_TEAMS:
30424 strcpy (p_name, "#pragma omp");
30425 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
30426 break;
30427 default:
30428 gcc_unreachable ();
30429 }
30430
30431 if (stmt)
30432 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30433 }
30434 \f
30435 /* Transactional Memory parsing routines. */
30436
30437 /* Parse a transaction attribute.
30438
30439 txn-attribute:
30440 attribute
30441 [ [ identifier ] ]
30442
30443 ??? Simplify this when C++0x bracket attributes are
30444 implemented properly. */
30445
30446 static tree
30447 cp_parser_txn_attribute_opt (cp_parser *parser)
30448 {
30449 cp_token *token;
30450 tree attr_name, attr = NULL;
30451
30452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30453 return cp_parser_attributes_opt (parser);
30454
30455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
30456 return NULL_TREE;
30457 cp_lexer_consume_token (parser->lexer);
30458 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
30459 goto error1;
30460
30461 token = cp_lexer_peek_token (parser->lexer);
30462 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
30463 {
30464 token = cp_lexer_consume_token (parser->lexer);
30465
30466 attr_name = (token->type == CPP_KEYWORD
30467 /* For keywords, use the canonical spelling,
30468 not the parsed identifier. */
30469 ? ridpointers[(int) token->keyword]
30470 : token->u.value);
30471 attr = build_tree_list (attr_name, NULL_TREE);
30472 }
30473 else
30474 cp_parser_error (parser, "expected identifier");
30475
30476 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30477 error1:
30478 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30479 return attr;
30480 }
30481
30482 /* Parse a __transaction_atomic or __transaction_relaxed statement.
30483
30484 transaction-statement:
30485 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
30486 compound-statement
30487 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
30488 */
30489
30490 static tree
30491 cp_parser_transaction (cp_parser *parser, enum rid keyword)
30492 {
30493 unsigned char old_in = parser->in_transaction;
30494 unsigned char this_in = 1, new_in;
30495 cp_token *token;
30496 tree stmt, attrs, noex;
30497
30498 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30499 || keyword == RID_TRANSACTION_RELAXED);
30500 token = cp_parser_require_keyword (parser, keyword,
30501 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30502 : RT_TRANSACTION_RELAXED));
30503 gcc_assert (token != NULL);
30504
30505 if (keyword == RID_TRANSACTION_RELAXED)
30506 this_in |= TM_STMT_ATTR_RELAXED;
30507 else
30508 {
30509 attrs = cp_parser_txn_attribute_opt (parser);
30510 if (attrs)
30511 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
30512 }
30513
30514 /* Parse a noexcept specification. */
30515 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
30516
30517 /* Keep track if we're in the lexical scope of an outer transaction. */
30518 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
30519
30520 stmt = begin_transaction_stmt (token->location, NULL, this_in);
30521
30522 parser->in_transaction = new_in;
30523 cp_parser_compound_statement (parser, NULL, false, false);
30524 parser->in_transaction = old_in;
30525
30526 finish_transaction_stmt (stmt, NULL, this_in, noex);
30527
30528 return stmt;
30529 }
30530
30531 /* Parse a __transaction_atomic or __transaction_relaxed expression.
30532
30533 transaction-expression:
30534 __transaction_atomic txn-noexcept-spec[opt] ( expression )
30535 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
30536 */
30537
30538 static tree
30539 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
30540 {
30541 unsigned char old_in = parser->in_transaction;
30542 unsigned char this_in = 1;
30543 cp_token *token;
30544 tree expr, noex;
30545 bool noex_expr;
30546
30547 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30548 || keyword == RID_TRANSACTION_RELAXED);
30549
30550 if (!flag_tm)
30551 error (keyword == RID_TRANSACTION_RELAXED
30552 ? G_("%<__transaction_relaxed%> without transactional memory "
30553 "support enabled")
30554 : G_("%<__transaction_atomic%> without transactional memory "
30555 "support enabled"));
30556
30557 token = cp_parser_require_keyword (parser, keyword,
30558 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30559 : RT_TRANSACTION_RELAXED));
30560 gcc_assert (token != NULL);
30561
30562 if (keyword == RID_TRANSACTION_RELAXED)
30563 this_in |= TM_STMT_ATTR_RELAXED;
30564
30565 /* Set this early. This might mean that we allow transaction_cancel in
30566 an expression that we find out later actually has to be a constexpr.
30567 However, we expect that cxx_constant_value will be able to deal with
30568 this; also, if the noexcept has no constexpr, then what we parse next
30569 really is a transaction's body. */
30570 parser->in_transaction = this_in;
30571
30572 /* Parse a noexcept specification. */
30573 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
30574 true);
30575
30576 if (!noex || !noex_expr
30577 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
30578 {
30579 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30580
30581 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
30582 expr = finish_parenthesized_expr (expr);
30583
30584 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30585 }
30586 else
30587 {
30588 /* The only expression that is available got parsed for the noexcept
30589 already. noexcept is true then. */
30590 expr = noex;
30591 noex = boolean_true_node;
30592 }
30593
30594 expr = build_transaction_expr (token->location, expr, this_in, noex);
30595 parser->in_transaction = old_in;
30596
30597 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
30598 return error_mark_node;
30599
30600 return (flag_tm ? expr : error_mark_node);
30601 }
30602
30603 /* Parse a function-transaction-block.
30604
30605 function-transaction-block:
30606 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
30607 function-body
30608 __transaction_atomic txn-attribute[opt] function-try-block
30609 __transaction_relaxed ctor-initializer[opt] function-body
30610 __transaction_relaxed function-try-block
30611 */
30612
30613 static bool
30614 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
30615 {
30616 unsigned char old_in = parser->in_transaction;
30617 unsigned char new_in = 1;
30618 tree compound_stmt, stmt, attrs;
30619 bool ctor_initializer_p;
30620 cp_token *token;
30621
30622 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30623 || keyword == RID_TRANSACTION_RELAXED);
30624 token = cp_parser_require_keyword (parser, keyword,
30625 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30626 : RT_TRANSACTION_RELAXED));
30627 gcc_assert (token != NULL);
30628
30629 if (keyword == RID_TRANSACTION_RELAXED)
30630 new_in |= TM_STMT_ATTR_RELAXED;
30631 else
30632 {
30633 attrs = cp_parser_txn_attribute_opt (parser);
30634 if (attrs)
30635 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
30636 }
30637
30638 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
30639
30640 parser->in_transaction = new_in;
30641
30642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
30643 ctor_initializer_p = cp_parser_function_try_block (parser);
30644 else
30645 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
30646 (parser, /*in_function_try_block=*/false);
30647
30648 parser->in_transaction = old_in;
30649
30650 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
30651
30652 return ctor_initializer_p;
30653 }
30654
30655 /* Parse a __transaction_cancel statement.
30656
30657 cancel-statement:
30658 __transaction_cancel txn-attribute[opt] ;
30659 __transaction_cancel txn-attribute[opt] throw-expression ;
30660
30661 ??? Cancel and throw is not yet implemented. */
30662
30663 static tree
30664 cp_parser_transaction_cancel (cp_parser *parser)
30665 {
30666 cp_token *token;
30667 bool is_outer = false;
30668 tree stmt, attrs;
30669
30670 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
30671 RT_TRANSACTION_CANCEL);
30672 gcc_assert (token != NULL);
30673
30674 attrs = cp_parser_txn_attribute_opt (parser);
30675 if (attrs)
30676 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
30677
30678 /* ??? Parse cancel-and-throw here. */
30679
30680 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30681
30682 if (!flag_tm)
30683 {
30684 error_at (token->location, "%<__transaction_cancel%> without "
30685 "transactional memory support enabled");
30686 return error_mark_node;
30687 }
30688 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
30689 {
30690 error_at (token->location, "%<__transaction_cancel%> within a "
30691 "%<__transaction_relaxed%>");
30692 return error_mark_node;
30693 }
30694 else if (is_outer)
30695 {
30696 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
30697 && !is_tm_may_cancel_outer (current_function_decl))
30698 {
30699 error_at (token->location, "outer %<__transaction_cancel%> not "
30700 "within outer %<__transaction_atomic%>");
30701 error_at (token->location,
30702 " or a %<transaction_may_cancel_outer%> function");
30703 return error_mark_node;
30704 }
30705 }
30706 else if (parser->in_transaction == 0)
30707 {
30708 error_at (token->location, "%<__transaction_cancel%> not within "
30709 "%<__transaction_atomic%>");
30710 return error_mark_node;
30711 }
30712
30713 stmt = build_tm_abort_call (token->location, is_outer);
30714 add_stmt (stmt);
30715
30716 return stmt;
30717 }
30718 \f
30719 /* The parser. */
30720
30721 static GTY (()) cp_parser *the_parser;
30722
30723 \f
30724 /* Special handling for the first token or line in the file. The first
30725 thing in the file might be #pragma GCC pch_preprocess, which loads a
30726 PCH file, which is a GC collection point. So we need to handle this
30727 first pragma without benefit of an existing lexer structure.
30728
30729 Always returns one token to the caller in *FIRST_TOKEN. This is
30730 either the true first token of the file, or the first token after
30731 the initial pragma. */
30732
30733 static void
30734 cp_parser_initial_pragma (cp_token *first_token)
30735 {
30736 tree name = NULL;
30737
30738 cp_lexer_get_preprocessor_token (NULL, first_token);
30739 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
30740 return;
30741
30742 cp_lexer_get_preprocessor_token (NULL, first_token);
30743 if (first_token->type == CPP_STRING)
30744 {
30745 name = first_token->u.value;
30746
30747 cp_lexer_get_preprocessor_token (NULL, first_token);
30748 if (first_token->type != CPP_PRAGMA_EOL)
30749 error_at (first_token->location,
30750 "junk at end of %<#pragma GCC pch_preprocess%>");
30751 }
30752 else
30753 error_at (first_token->location, "expected string literal");
30754
30755 /* Skip to the end of the pragma. */
30756 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
30757 cp_lexer_get_preprocessor_token (NULL, first_token);
30758
30759 /* Now actually load the PCH file. */
30760 if (name)
30761 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
30762
30763 /* Read one more token to return to our caller. We have to do this
30764 after reading the PCH file in, since its pointers have to be
30765 live. */
30766 cp_lexer_get_preprocessor_token (NULL, first_token);
30767 }
30768
30769 /* Normal parsing of a pragma token. Here we can (and must) use the
30770 regular lexer. */
30771
30772 static bool
30773 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
30774 {
30775 cp_token *pragma_tok;
30776 unsigned int id;
30777
30778 pragma_tok = cp_lexer_consume_token (parser->lexer);
30779 gcc_assert (pragma_tok->type == CPP_PRAGMA);
30780 parser->lexer->in_pragma = true;
30781
30782 id = pragma_tok->pragma_kind;
30783 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
30784 cp_ensure_no_omp_declare_simd (parser);
30785 switch (id)
30786 {
30787 case PRAGMA_GCC_PCH_PREPROCESS:
30788 error_at (pragma_tok->location,
30789 "%<#pragma GCC pch_preprocess%> must be first");
30790 break;
30791
30792 case PRAGMA_OMP_BARRIER:
30793 switch (context)
30794 {
30795 case pragma_compound:
30796 cp_parser_omp_barrier (parser, pragma_tok);
30797 return false;
30798 case pragma_stmt:
30799 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
30800 "used in compound statements");
30801 break;
30802 default:
30803 goto bad_stmt;
30804 }
30805 break;
30806
30807 case PRAGMA_OMP_FLUSH:
30808 switch (context)
30809 {
30810 case pragma_compound:
30811 cp_parser_omp_flush (parser, pragma_tok);
30812 return false;
30813 case pragma_stmt:
30814 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
30815 "used in compound statements");
30816 break;
30817 default:
30818 goto bad_stmt;
30819 }
30820 break;
30821
30822 case PRAGMA_OMP_TASKWAIT:
30823 switch (context)
30824 {
30825 case pragma_compound:
30826 cp_parser_omp_taskwait (parser, pragma_tok);
30827 return false;
30828 case pragma_stmt:
30829 error_at (pragma_tok->location,
30830 "%<#pragma omp taskwait%> may only be "
30831 "used in compound statements");
30832 break;
30833 default:
30834 goto bad_stmt;
30835 }
30836 break;
30837
30838 case PRAGMA_OMP_TASKYIELD:
30839 switch (context)
30840 {
30841 case pragma_compound:
30842 cp_parser_omp_taskyield (parser, pragma_tok);
30843 return false;
30844 case pragma_stmt:
30845 error_at (pragma_tok->location,
30846 "%<#pragma omp taskyield%> may only be "
30847 "used in compound statements");
30848 break;
30849 default:
30850 goto bad_stmt;
30851 }
30852 break;
30853
30854 case PRAGMA_OMP_CANCEL:
30855 switch (context)
30856 {
30857 case pragma_compound:
30858 cp_parser_omp_cancel (parser, pragma_tok);
30859 return false;
30860 case pragma_stmt:
30861 error_at (pragma_tok->location,
30862 "%<#pragma omp cancel%> may only be "
30863 "used in compound statements");
30864 break;
30865 default:
30866 goto bad_stmt;
30867 }
30868 break;
30869
30870 case PRAGMA_OMP_CANCELLATION_POINT:
30871 switch (context)
30872 {
30873 case pragma_compound:
30874 cp_parser_omp_cancellation_point (parser, pragma_tok);
30875 return false;
30876 case pragma_stmt:
30877 error_at (pragma_tok->location,
30878 "%<#pragma omp cancellation point%> may only be "
30879 "used in compound statements");
30880 break;
30881 default:
30882 goto bad_stmt;
30883 }
30884 break;
30885
30886 case PRAGMA_OMP_THREADPRIVATE:
30887 cp_parser_omp_threadprivate (parser, pragma_tok);
30888 return false;
30889
30890 case PRAGMA_OMP_DECLARE_REDUCTION:
30891 cp_parser_omp_declare (parser, pragma_tok, context);
30892 return false;
30893
30894 case PRAGMA_OMP_ATOMIC:
30895 case PRAGMA_OMP_CRITICAL:
30896 case PRAGMA_OMP_DISTRIBUTE:
30897 case PRAGMA_OMP_FOR:
30898 case PRAGMA_OMP_MASTER:
30899 case PRAGMA_OMP_ORDERED:
30900 case PRAGMA_OMP_PARALLEL:
30901 case PRAGMA_OMP_SECTIONS:
30902 case PRAGMA_OMP_SIMD:
30903 case PRAGMA_OMP_SINGLE:
30904 case PRAGMA_OMP_TASK:
30905 case PRAGMA_OMP_TASKGROUP:
30906 case PRAGMA_OMP_TEAMS:
30907 if (context != pragma_stmt && context != pragma_compound)
30908 goto bad_stmt;
30909 cp_parser_omp_construct (parser, pragma_tok);
30910 return true;
30911
30912 case PRAGMA_OMP_TARGET:
30913 return cp_parser_omp_target (parser, pragma_tok, context);
30914
30915 case PRAGMA_OMP_END_DECLARE_TARGET:
30916 cp_parser_omp_end_declare_target (parser, pragma_tok);
30917 return false;
30918
30919 case PRAGMA_OMP_SECTION:
30920 error_at (pragma_tok->location,
30921 "%<#pragma omp section%> may only be used in "
30922 "%<#pragma omp sections%> construct");
30923 break;
30924
30925 case PRAGMA_IVDEP:
30926 {
30927 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30928 cp_token *tok;
30929 tok = cp_lexer_peek_token (the_parser->lexer);
30930 if (tok->type != CPP_KEYWORD
30931 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
30932 && tok->keyword != RID_DO))
30933 {
30934 cp_parser_error (parser, "for, while or do statement expected");
30935 return false;
30936 }
30937 cp_parser_iteration_statement (parser, true);
30938 return true;
30939 }
30940
30941 default:
30942 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
30943 c_invoke_pragma_handler (id);
30944 break;
30945
30946 bad_stmt:
30947 cp_parser_error (parser, "expected declaration specifiers");
30948 break;
30949 }
30950
30951 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30952 return false;
30953 }
30954
30955 /* The interface the pragma parsers have to the lexer. */
30956
30957 enum cpp_ttype
30958 pragma_lex (tree *value)
30959 {
30960 cp_token *tok;
30961 enum cpp_ttype ret;
30962
30963 tok = cp_lexer_peek_token (the_parser->lexer);
30964
30965 ret = tok->type;
30966 *value = tok->u.value;
30967
30968 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
30969 ret = CPP_EOF;
30970 else if (ret == CPP_STRING)
30971 *value = cp_parser_string_literal (the_parser, false, false);
30972 else
30973 {
30974 cp_lexer_consume_token (the_parser->lexer);
30975 if (ret == CPP_KEYWORD)
30976 ret = CPP_NAME;
30977 }
30978
30979 return ret;
30980 }
30981
30982 \f
30983 /* External interface. */
30984
30985 /* Parse one entire translation unit. */
30986
30987 void
30988 c_parse_file (void)
30989 {
30990 static bool already_called = false;
30991
30992 if (already_called)
30993 {
30994 sorry ("inter-module optimizations not implemented for C++");
30995 return;
30996 }
30997 already_called = true;
30998
30999 the_parser = cp_parser_new ();
31000 push_deferring_access_checks (flag_access_control
31001 ? dk_no_deferred : dk_no_check);
31002 cp_parser_translation_unit (the_parser);
31003 the_parser = NULL;
31004 }
31005
31006 /* Create an identifier for a generic parameter type (a synthesized
31007 template parameter implied by `auto' or a concept identifier). */
31008
31009 static GTY(()) int generic_parm_count;
31010 static tree
31011 make_generic_type_name ()
31012 {
31013 char buf[32];
31014 sprintf (buf, "<auto%d>", ++generic_parm_count);
31015 return get_identifier (buf);
31016 }
31017
31018 /* Predicate that behaves as is_auto_or_concept but matches the parent
31019 node of the generic type rather than the generic type itself. This
31020 allows for type transformation in add_implicit_template_parms. */
31021
31022 static inline bool
31023 tree_type_is_auto_or_concept (const_tree t)
31024 {
31025 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
31026 }
31027
31028 /* Add EXPECT_COUNT implicit template parameters gleaned from the generic
31029 type parameters in PARAMETERS to the CURRENT_TEMPLATE_PARMS (creating a new
31030 template parameter list if necessary). Returns PARAMETERS suitably rewritten
31031 to reference the newly created types or ERROR_MARK_NODE on failure. */
31032
31033 tree
31034 add_implicit_template_parms (cp_parser *parser, size_t expect_count,
31035 tree parameters)
31036 {
31037 gcc_assert (current_binding_level->kind == sk_function_parms);
31038
31039 cp_binding_level *fn_parms_scope = current_binding_level;
31040
31041 bool become_template =
31042 fn_parms_scope->level_chain->kind != sk_template_parms;
31043
31044 size_t synth_count = 0;
31045
31046 /* Roll back a scope level and either introduce a new template parameter list
31047 or update an existing one. The function scope is added back after template
31048 parameter synthesis below. */
31049 current_binding_level = fn_parms_scope->level_chain;
31050
31051 /* TPARMS tracks the function's template parameter list. This is either a new
31052 chain in the case of a fully implicit function template or an extension of
31053 the function's explicitly specified template parameter list. */
31054 tree tparms = NULL_TREE;
31055
31056 if (become_template)
31057 {
31058 push_deferring_access_checks (dk_deferred);
31059 begin_template_parm_list ();
31060
31061 parser->fully_implicit_function_template_p = true;
31062 ++parser->num_template_parameter_lists;
31063 }
31064 else
31065 {
31066 /* Roll back the innermost template parameter list such that it may be
31067 extended in the loop below as if it were being explicitly declared. */
31068
31069 gcc_assert (current_template_parms);
31070
31071 /* Pop the innermost template parms into TPARMS. */
31072 tree inner_vec = INNERMOST_TEMPLATE_PARMS (current_template_parms);
31073 current_template_parms = TREE_CHAIN (current_template_parms);
31074
31075 size_t inner_vec_len = TREE_VEC_LENGTH (inner_vec);
31076 if (inner_vec_len != 0)
31077 {
31078 tree t = tparms = TREE_VEC_ELT (inner_vec, 0);
31079 for (size_t n = 1; n < inner_vec_len; ++n)
31080 t = TREE_CHAIN (t) = TREE_VEC_ELT (inner_vec, n);
31081 }
31082
31083 ++processing_template_parmlist;
31084 }
31085
31086 for (tree p = parameters; p && synth_count < expect_count; p = TREE_CHAIN (p))
31087 {
31088 tree generic_type_ptr
31089 = find_type_usage (TREE_VALUE (p), tree_type_is_auto_or_concept);
31090
31091 if (!generic_type_ptr)
31092 continue;
31093
31094 ++synth_count;
31095
31096 tree synth_id = make_generic_type_name ();
31097 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
31098 synth_id);
31099 tparms = process_template_parm (tparms, DECL_SOURCE_LOCATION (TREE_VALUE
31100 (p)),
31101 build_tree_list (NULL_TREE,
31102 synth_tmpl_parm),
31103 /*non_type=*/false,
31104 /*param_pack=*/false);
31105
31106 /* Rewrite the type of P to be the template_parm added above (getdecls is
31107 used to retrieve it since it is the most recent declaration in this
31108 scope). Qualifiers need to be preserved also. */
31109
31110 tree& cur_type = TREE_TYPE (generic_type_ptr);
31111 tree new_type = TREE_TYPE (getdecls ());
31112
31113 if (TYPE_QUALS (cur_type))
31114 cur_type = cp_build_qualified_type (new_type, TYPE_QUALS (cur_type));
31115 else
31116 cur_type = new_type;
31117 }
31118
31119 gcc_assert (synth_count == expect_count);
31120
31121 push_binding_level (fn_parms_scope);
31122
31123 end_template_parm_list (tparms);
31124
31125 return parameters;
31126 }
31127
31128 /* Finish the declaration of a fully implicit function template. Such a
31129 template has no explicit template parameter list so has not been through the
31130 normal template head and tail processing. add_implicit_template_parms tries
31131 to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
31132 provided if the declaration is a class member such that its template
31133 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
31134 form is returned. Otherwise NULL_TREE is returned. */
31135
31136 tree
31137 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
31138 {
31139 gcc_assert (parser->fully_implicit_function_template_p);
31140
31141 if (member_decl_opt && member_decl_opt != error_mark_node
31142 && DECL_VIRTUAL_P (member_decl_opt))
31143 {
31144 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
31145 "implicit templates may not be %<virtual%>");
31146 DECL_VIRTUAL_P (member_decl_opt) = false;
31147 }
31148
31149 pop_deferring_access_checks ();
31150 if (member_decl_opt)
31151 member_decl_opt = finish_member_template_decl (member_decl_opt);
31152 end_template_decl ();
31153
31154 parser->fully_implicit_function_template_p = false;
31155 --parser->num_template_parameter_lists;
31156
31157 return member_decl_opt;
31158 }
31159
31160 #include "gt-cp-parser.h"