3cb6dbf61a056869dba5476ee6203eba3258b0c4
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.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 /* Manifest constants. */
237 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
238 #define CP_SAVED_TOKEN_STACK 5
239
240 /* Variables. */
241
242 /* The stream to which debugging output should be written. */
243 static FILE *cp_lexer_debug_stream;
244
245 /* Nonzero if we are parsing an unevaluated operand: an operand to
246 sizeof, typeof, or alignof. */
247 int cp_unevaluated_operand;
248
249 /* Dump up to NUM tokens in BUFFER to FILE starting with token
250 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
251 first token in BUFFER. If NUM is 0, dump all the tokens. If
252 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
253 highlighted by surrounding it in [[ ]]. */
254
255 static void
256 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
257 cp_token *start_token, unsigned num,
258 cp_token *curr_token)
259 {
260 unsigned i, nprinted;
261 cp_token *token;
262 bool do_print;
263
264 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265
266 if (buffer == NULL)
267 return;
268
269 if (num == 0)
270 num = VEC_length (cp_token, buffer);
271
272 if (start_token == NULL)
273 start_token = VEC_address (cp_token, buffer);
274
275 if (start_token > VEC_address (cp_token, buffer))
276 {
277 cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
278 fprintf (file, " ... ");
279 }
280
281 do_print = false;
282 nprinted = 0;
283 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284 {
285 if (token == start_token)
286 do_print = true;
287
288 if (!do_print)
289 continue;
290
291 nprinted++;
292 if (token == curr_token)
293 fprintf (file, "[[");
294
295 cp_lexer_print_token (file, token);
296
297 if (token == curr_token)
298 fprintf (file, "]]");
299
300 switch (token->type)
301 {
302 case CPP_SEMICOLON:
303 case CPP_OPEN_BRACE:
304 case CPP_CLOSE_BRACE:
305 case CPP_EOF:
306 fputc ('\n', file);
307 break;
308
309 default:
310 fputc (' ', file);
311 }
312 }
313
314 if (i == num && i < VEC_length (cp_token, buffer))
315 {
316 fprintf (file, " ... ");
317 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
318 VEC_length (cp_token, buffer) - 1));
319 }
320
321 fprintf (file, "\n");
322 }
323
324
325 /* Dump all tokens in BUFFER to stderr. */
326
327 void
328 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
329 {
330 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
331 }
332
333
334 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
335 description for T. */
336
337 static void
338 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
339 {
340 if (t)
341 {
342 fprintf (file, "%s: ", desc);
343 print_node_brief (file, "", t, 0);
344 }
345 }
346
347
348 /* Dump parser context C to FILE. */
349
350 static void
351 cp_debug_print_context (FILE *file, cp_parser_context *c)
352 {
353 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
354 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
355 print_node_brief (file, "", c->object_type, 0);
356 fprintf (file, "}\n");
357 }
358
359
360 /* Print the stack of parsing contexts to FILE starting with FIRST. */
361
362 static void
363 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
364 {
365 unsigned i;
366 cp_parser_context *c;
367
368 fprintf (file, "Parsing context stack:\n");
369 for (i = 0, c = first; c; c = c->next, i++)
370 {
371 fprintf (file, "\t#%u: ", i);
372 cp_debug_print_context (file, c);
373 }
374 }
375
376
377 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
378
379 static void
380 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
381 {
382 if (flag)
383 fprintf (file, "%s: true\n", desc);
384 }
385
386
387 /* Print an unparsed function entry UF to FILE. */
388
389 static void
390 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
391 {
392 unsigned i;
393 cp_default_arg_entry *default_arg_fn;
394 tree fn;
395
396 fprintf (file, "\tFunctions with default args:\n");
397 for (i = 0;
398 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
399 default_arg_fn);
400 i++)
401 {
402 fprintf (file, "\t\tClass type: ");
403 print_node_brief (file, "", default_arg_fn->class_type, 0);
404 fprintf (file, "\t\tDeclaration: ");
405 print_node_brief (file, "", default_arg_fn->decl, 0);
406 fprintf (file, "\n");
407 }
408
409 fprintf (file, "\n\tFunctions with definitions that require "
410 "post-processing\n\t\t");
411 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
412 {
413 print_node_brief (file, "", fn, 0);
414 fprintf (file, " ");
415 }
416 fprintf (file, "\n");
417
418 fprintf (file, "\n\tNon-static data members with initializers that require "
419 "post-processing\n\t\t");
420 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
421 {
422 print_node_brief (file, "", fn, 0);
423 fprintf (file, " ");
424 }
425 fprintf (file, "\n");
426 }
427
428
429 /* Print the stack of unparsed member functions S to FILE. */
430
431 static void
432 cp_debug_print_unparsed_queues (FILE *file,
433 VEC(cp_unparsed_functions_entry, gc) *s)
434 {
435 unsigned i;
436 cp_unparsed_functions_entry *uf;
437
438 fprintf (file, "Unparsed functions\n");
439 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
440 {
441 fprintf (file, "#%u:\n", i);
442 cp_debug_print_unparsed_function (file, uf);
443 }
444 }
445
446
447 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
448 the given PARSER. If FILE is NULL, the output is printed on stderr. */
449
450 static void
451 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
452 {
453 cp_token *next_token, *first_token, *start_token;
454
455 if (file == NULL)
456 file = stderr;
457
458 next_token = parser->lexer->next_token;
459 first_token = VEC_address (cp_token, parser->lexer->buffer);
460 start_token = (next_token > first_token + window_size / 2)
461 ? next_token - window_size / 2
462 : first_token;
463 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
464 next_token);
465 }
466
467
468 /* Dump debugging information for the given PARSER. If FILE is NULL,
469 the output is printed on stderr. */
470
471 void
472 cp_debug_parser (FILE *file, cp_parser *parser)
473 {
474 const size_t window_size = 20;
475 cp_token *token;
476 expanded_location eloc;
477
478 if (file == NULL)
479 file = stderr;
480
481 fprintf (file, "Parser state\n\n");
482 fprintf (file, "Number of tokens: %u\n",
483 VEC_length (cp_token, parser->lexer->buffer));
484 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
485 cp_debug_print_tree_if_set (file, "Object scope",
486 parser->object_scope);
487 cp_debug_print_tree_if_set (file, "Qualifying scope",
488 parser->qualifying_scope);
489 cp_debug_print_context_stack (file, parser->context);
490 cp_debug_print_flag (file, "Allow GNU extensions",
491 parser->allow_gnu_extensions_p);
492 cp_debug_print_flag (file, "'>' token is greater-than",
493 parser->greater_than_is_operator_p);
494 cp_debug_print_flag (file, "Default args allowed in current "
495 "parameter list", parser->default_arg_ok_p);
496 cp_debug_print_flag (file, "Parsing integral constant-expression",
497 parser->integral_constant_expression_p);
498 cp_debug_print_flag (file, "Allow non-constant expression in current "
499 "constant-expression",
500 parser->allow_non_integral_constant_expression_p);
501 cp_debug_print_flag (file, "Seen non-constant expression",
502 parser->non_integral_constant_expression_p);
503 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
504 "current context",
505 parser->local_variables_forbidden_p);
506 cp_debug_print_flag (file, "In unbraced linkage specification",
507 parser->in_unbraced_linkage_specification_p);
508 cp_debug_print_flag (file, "Parsing a declarator",
509 parser->in_declarator_p);
510 cp_debug_print_flag (file, "In template argument list",
511 parser->in_template_argument_list_p);
512 cp_debug_print_flag (file, "Parsing an iteration statement",
513 parser->in_statement & IN_ITERATION_STMT);
514 cp_debug_print_flag (file, "Parsing a switch statement",
515 parser->in_statement & IN_SWITCH_STMT);
516 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
517 parser->in_statement & IN_OMP_BLOCK);
518 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
519 parser->in_statement & IN_OMP_FOR);
520 cp_debug_print_flag (file, "Parsing an if statement",
521 parser->in_statement & IN_IF_STMT);
522 cp_debug_print_flag (file, "Parsing a type-id in an expression "
523 "context", parser->in_type_id_in_expr_p);
524 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
525 parser->implicit_extern_c);
526 cp_debug_print_flag (file, "String expressions should be translated "
527 "to execution character set",
528 parser->translate_strings_p);
529 cp_debug_print_flag (file, "Parsing function body outside of a "
530 "local class", parser->in_function_body);
531 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
532 parser->colon_corrects_to_scope_p);
533 if (parser->type_definition_forbidden_message)
534 fprintf (file, "Error message for forbidden type definitions: %s\n",
535 parser->type_definition_forbidden_message);
536 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
537 fprintf (file, "Number of class definitions in progress: %u\n",
538 parser->num_classes_being_defined);
539 fprintf (file, "Number of template parameter lists for the current "
540 "declaration: %u\n", parser->num_template_parameter_lists);
541 cp_debug_parser_tokens (file, parser, window_size);
542 token = parser->lexer->next_token;
543 fprintf (file, "Next token to parse:\n");
544 fprintf (file, "\tToken: ");
545 cp_lexer_print_token (file, token);
546 eloc = expand_location (token->location);
547 fprintf (file, "\n\tFile: %s\n", eloc.file);
548 fprintf (file, "\tLine: %d\n", eloc.line);
549 fprintf (file, "\tColumn: %d\n", eloc.column);
550 }
551
552
553 /* Allocate memory for a new lexer object and return it. */
554
555 static cp_lexer *
556 cp_lexer_alloc (void)
557 {
558 cp_lexer *lexer;
559
560 c_common_no_more_pch ();
561
562 /* Allocate the memory. */
563 lexer = ggc_alloc_cleared_cp_lexer ();
564
565 /* Initially we are not debugging. */
566 lexer->debugging_p = false;
567
568 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
569 CP_SAVED_TOKEN_STACK);
570
571 /* Create the buffer. */
572 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
573
574 return lexer;
575 }
576
577
578 /* Create a new main C++ lexer, the lexer that gets tokens from the
579 preprocessor. */
580
581 static cp_lexer *
582 cp_lexer_new_main (void)
583 {
584 cp_lexer *lexer;
585 cp_token token;
586
587 /* It's possible that parsing the first pragma will load a PCH file,
588 which is a GC collection point. So we have to do that before
589 allocating any memory. */
590 cp_parser_initial_pragma (&token);
591
592 lexer = cp_lexer_alloc ();
593
594 /* Put the first token in the buffer. */
595 VEC_quick_push (cp_token, lexer->buffer, &token);
596
597 /* Get the remaining tokens from the preprocessor. */
598 while (token.type != CPP_EOF)
599 {
600 cp_lexer_get_preprocessor_token (lexer, &token);
601 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
602 }
603
604 lexer->last_token = VEC_address (cp_token, lexer->buffer)
605 + VEC_length (cp_token, lexer->buffer)
606 - 1;
607 lexer->next_token = VEC_length (cp_token, lexer->buffer)
608 ? VEC_address (cp_token, lexer->buffer)
609 : &eof_token;
610
611 /* Subsequent preprocessor diagnostics should use compiler
612 diagnostic functions to get the compiler source location. */
613 done_lexing = true;
614
615 gcc_assert (!lexer->next_token->purged_p);
616 return lexer;
617 }
618
619 /* Create a new lexer whose token stream is primed with the tokens in
620 CACHE. When these tokens are exhausted, no new tokens will be read. */
621
622 static cp_lexer *
623 cp_lexer_new_from_tokens (cp_token_cache *cache)
624 {
625 cp_token *first = cache->first;
626 cp_token *last = cache->last;
627 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
628
629 /* We do not own the buffer. */
630 lexer->buffer = NULL;
631 lexer->next_token = first == last ? &eof_token : first;
632 lexer->last_token = last;
633
634 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
635 CP_SAVED_TOKEN_STACK);
636
637 /* Initially we are not debugging. */
638 lexer->debugging_p = false;
639
640 gcc_assert (!lexer->next_token->purged_p);
641 return lexer;
642 }
643
644 /* Frees all resources associated with LEXER. */
645
646 static void
647 cp_lexer_destroy (cp_lexer *lexer)
648 {
649 VEC_free (cp_token, gc, lexer->buffer);
650 VEC_free (cp_token_position, heap, lexer->saved_tokens);
651 ggc_free (lexer);
652 }
653
654 /* Returns nonzero if debugging information should be output. */
655
656 static inline bool
657 cp_lexer_debugging_p (cp_lexer *lexer)
658 {
659 return lexer->debugging_p;
660 }
661
662
663 static inline cp_token_position
664 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
665 {
666 gcc_assert (!previous_p || lexer->next_token != &eof_token);
667
668 return lexer->next_token - previous_p;
669 }
670
671 static inline cp_token *
672 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
673 {
674 return pos;
675 }
676
677 static inline void
678 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
679 {
680 lexer->next_token = cp_lexer_token_at (lexer, pos);
681 }
682
683 static inline cp_token_position
684 cp_lexer_previous_token_position (cp_lexer *lexer)
685 {
686 if (lexer->next_token == &eof_token)
687 return lexer->last_token - 1;
688 else
689 return cp_lexer_token_position (lexer, true);
690 }
691
692 static inline cp_token *
693 cp_lexer_previous_token (cp_lexer *lexer)
694 {
695 cp_token_position tp = cp_lexer_previous_token_position (lexer);
696
697 return cp_lexer_token_at (lexer, tp);
698 }
699
700 /* nonzero if we are presently saving tokens. */
701
702 static inline int
703 cp_lexer_saving_tokens (const cp_lexer* lexer)
704 {
705 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
706 }
707
708 /* Store the next token from the preprocessor in *TOKEN. Return true
709 if we reach EOF. If LEXER is NULL, assume we are handling an
710 initial #pragma pch_preprocess, and thus want the lexer to return
711 processed strings. */
712
713 static void
714 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
715 {
716 static int is_extern_c = 0;
717
718 /* Get a new token from the preprocessor. */
719 token->type
720 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
721 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
722 token->keyword = RID_MAX;
723 token->pragma_kind = PRAGMA_NONE;
724 token->purged_p = false;
725
726 /* On some systems, some header files are surrounded by an
727 implicit extern "C" block. Set a flag in the token if it
728 comes from such a header. */
729 is_extern_c += pending_lang_change;
730 pending_lang_change = 0;
731 token->implicit_extern_c = is_extern_c > 0;
732
733 /* Check to see if this token is a keyword. */
734 if (token->type == CPP_NAME)
735 {
736 if (C_IS_RESERVED_WORD (token->u.value))
737 {
738 /* Mark this token as a keyword. */
739 token->type = CPP_KEYWORD;
740 /* Record which keyword. */
741 token->keyword = C_RID_CODE (token->u.value);
742 }
743 else
744 {
745 if (warn_cxx0x_compat
746 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
747 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
748 {
749 /* Warn about the C++0x keyword (but still treat it as
750 an identifier). */
751 warning (OPT_Wc__0x_compat,
752 "identifier %qE is a keyword in C++11",
753 token->u.value);
754
755 /* Clear out the C_RID_CODE so we don't warn about this
756 particular identifier-turned-keyword again. */
757 C_SET_RID_CODE (token->u.value, RID_MAX);
758 }
759
760 token->ambiguous_p = false;
761 token->keyword = RID_MAX;
762 }
763 }
764 else if (token->type == CPP_AT_NAME)
765 {
766 /* This only happens in Objective-C++; it must be a keyword. */
767 token->type = CPP_KEYWORD;
768 switch (C_RID_CODE (token->u.value))
769 {
770 /* Replace 'class' with '@class', 'private' with '@private',
771 etc. This prevents confusion with the C++ keyword
772 'class', and makes the tokens consistent with other
773 Objective-C 'AT' keywords. For example '@class' is
774 reported as RID_AT_CLASS which is consistent with
775 '@synchronized', which is reported as
776 RID_AT_SYNCHRONIZED.
777 */
778 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
779 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
780 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
781 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
782 case RID_THROW: token->keyword = RID_AT_THROW; break;
783 case RID_TRY: token->keyword = RID_AT_TRY; break;
784 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
785 default: token->keyword = C_RID_CODE (token->u.value);
786 }
787 }
788 else if (token->type == CPP_PRAGMA)
789 {
790 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
791 token->pragma_kind = ((enum pragma_kind)
792 TREE_INT_CST_LOW (token->u.value));
793 token->u.value = NULL_TREE;
794 }
795 }
796
797 /* Update the globals input_location and the input file stack from TOKEN. */
798 static inline void
799 cp_lexer_set_source_position_from_token (cp_token *token)
800 {
801 if (token->type != CPP_EOF)
802 {
803 input_location = token->location;
804 }
805 }
806
807 /* Return a pointer to the next token in the token stream, but do not
808 consume it. */
809
810 static inline cp_token *
811 cp_lexer_peek_token (cp_lexer *lexer)
812 {
813 if (cp_lexer_debugging_p (lexer))
814 {
815 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
816 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
817 putc ('\n', cp_lexer_debug_stream);
818 }
819 return lexer->next_token;
820 }
821
822 /* Return true if the next token has the indicated TYPE. */
823
824 static inline bool
825 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
826 {
827 return cp_lexer_peek_token (lexer)->type == type;
828 }
829
830 /* Return true if the next token does not have the indicated TYPE. */
831
832 static inline bool
833 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
834 {
835 return !cp_lexer_next_token_is (lexer, type);
836 }
837
838 /* Return true if the next token is the indicated KEYWORD. */
839
840 static inline bool
841 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
842 {
843 return cp_lexer_peek_token (lexer)->keyword == keyword;
844 }
845
846 /* Return true if the next token is not the indicated KEYWORD. */
847
848 static inline bool
849 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
850 {
851 return cp_lexer_peek_token (lexer)->keyword != keyword;
852 }
853
854 /* Return true if the next token is a keyword for a decl-specifier. */
855
856 static bool
857 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
858 {
859 cp_token *token;
860
861 token = cp_lexer_peek_token (lexer);
862 switch (token->keyword)
863 {
864 /* auto specifier: storage-class-specifier in C++,
865 simple-type-specifier in C++0x. */
866 case RID_AUTO:
867 /* Storage classes. */
868 case RID_REGISTER:
869 case RID_STATIC:
870 case RID_EXTERN:
871 case RID_MUTABLE:
872 case RID_THREAD:
873 /* Elaborated type specifiers. */
874 case RID_ENUM:
875 case RID_CLASS:
876 case RID_STRUCT:
877 case RID_UNION:
878 case RID_TYPENAME:
879 /* Simple type specifiers. */
880 case RID_CHAR:
881 case RID_CHAR16:
882 case RID_CHAR32:
883 case RID_WCHAR:
884 case RID_BOOL:
885 case RID_SHORT:
886 case RID_INT:
887 case RID_LONG:
888 case RID_INT128:
889 case RID_SIGNED:
890 case RID_UNSIGNED:
891 case RID_FLOAT:
892 case RID_DOUBLE:
893 case RID_VOID:
894 /* GNU extensions. */
895 case RID_ATTRIBUTE:
896 case RID_TYPEOF:
897 /* C++0x extensions. */
898 case RID_DECLTYPE:
899 case RID_UNDERLYING_TYPE:
900 return true;
901
902 default:
903 return false;
904 }
905 }
906
907 /* Returns TRUE iff the token T begins a decltype type. */
908
909 static bool
910 token_is_decltype (cp_token *t)
911 {
912 return (t->keyword == RID_DECLTYPE
913 || t->type == CPP_DECLTYPE);
914 }
915
916 /* Returns TRUE iff the next token begins a decltype type. */
917
918 static bool
919 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
920 {
921 cp_token *t = cp_lexer_peek_token (lexer);
922 return token_is_decltype (t);
923 }
924
925 /* Return a pointer to the Nth token in the token stream. If N is 1,
926 then this is precisely equivalent to cp_lexer_peek_token (except
927 that it is not inline). One would like to disallow that case, but
928 there is one case (cp_parser_nth_token_starts_template_id) where
929 the caller passes a variable for N and it might be 1. */
930
931 static cp_token *
932 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
933 {
934 cp_token *token;
935
936 /* N is 1-based, not zero-based. */
937 gcc_assert (n > 0);
938
939 if (cp_lexer_debugging_p (lexer))
940 fprintf (cp_lexer_debug_stream,
941 "cp_lexer: peeking ahead %ld at token: ", (long)n);
942
943 --n;
944 token = lexer->next_token;
945 gcc_assert (!n || token != &eof_token);
946 while (n != 0)
947 {
948 ++token;
949 if (token == lexer->last_token)
950 {
951 token = &eof_token;
952 break;
953 }
954
955 if (!token->purged_p)
956 --n;
957 }
958
959 if (cp_lexer_debugging_p (lexer))
960 {
961 cp_lexer_print_token (cp_lexer_debug_stream, token);
962 putc ('\n', cp_lexer_debug_stream);
963 }
964
965 return token;
966 }
967
968 /* Return the next token, and advance the lexer's next_token pointer
969 to point to the next non-purged token. */
970
971 static cp_token *
972 cp_lexer_consume_token (cp_lexer* lexer)
973 {
974 cp_token *token = lexer->next_token;
975
976 gcc_assert (token != &eof_token);
977 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
978
979 do
980 {
981 lexer->next_token++;
982 if (lexer->next_token == lexer->last_token)
983 {
984 lexer->next_token = &eof_token;
985 break;
986 }
987
988 }
989 while (lexer->next_token->purged_p);
990
991 cp_lexer_set_source_position_from_token (token);
992
993 /* Provide debugging output. */
994 if (cp_lexer_debugging_p (lexer))
995 {
996 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
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 /* Permanently remove the next token from the token stream, and
1005 advance the next_token pointer to refer to the next non-purged
1006 token. */
1007
1008 static void
1009 cp_lexer_purge_token (cp_lexer *lexer)
1010 {
1011 cp_token *tok = lexer->next_token;
1012
1013 gcc_assert (tok != &eof_token);
1014 tok->purged_p = true;
1015 tok->location = UNKNOWN_LOCATION;
1016 tok->u.value = NULL_TREE;
1017 tok->keyword = RID_MAX;
1018
1019 do
1020 {
1021 tok++;
1022 if (tok == lexer->last_token)
1023 {
1024 tok = &eof_token;
1025 break;
1026 }
1027 }
1028 while (tok->purged_p);
1029 lexer->next_token = tok;
1030 }
1031
1032 /* Permanently remove all tokens after TOK, up to, but not
1033 including, the token that will be returned next by
1034 cp_lexer_peek_token. */
1035
1036 static void
1037 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1038 {
1039 cp_token *peek = lexer->next_token;
1040
1041 if (peek == &eof_token)
1042 peek = lexer->last_token;
1043
1044 gcc_assert (tok < peek);
1045
1046 for ( tok += 1; tok != peek; tok += 1)
1047 {
1048 tok->purged_p = true;
1049 tok->location = UNKNOWN_LOCATION;
1050 tok->u.value = NULL_TREE;
1051 tok->keyword = RID_MAX;
1052 }
1053 }
1054
1055 /* Begin saving tokens. All tokens consumed after this point will be
1056 preserved. */
1057
1058 static void
1059 cp_lexer_save_tokens (cp_lexer* lexer)
1060 {
1061 /* Provide debugging output. */
1062 if (cp_lexer_debugging_p (lexer))
1063 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1064
1065 VEC_safe_push (cp_token_position, heap,
1066 lexer->saved_tokens, lexer->next_token);
1067 }
1068
1069 /* Commit to the portion of the token stream most recently saved. */
1070
1071 static void
1072 cp_lexer_commit_tokens (cp_lexer* lexer)
1073 {
1074 /* Provide debugging output. */
1075 if (cp_lexer_debugging_p (lexer))
1076 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1077
1078 VEC_pop (cp_token_position, lexer->saved_tokens);
1079 }
1080
1081 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1082 to the token stream. Stop saving tokens. */
1083
1084 static void
1085 cp_lexer_rollback_tokens (cp_lexer* lexer)
1086 {
1087 /* Provide debugging output. */
1088 if (cp_lexer_debugging_p (lexer))
1089 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1090
1091 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1092 }
1093
1094 /* Print a representation of the TOKEN on the STREAM. */
1095
1096 static void
1097 cp_lexer_print_token (FILE * stream, cp_token *token)
1098 {
1099 /* We don't use cpp_type2name here because the parser defines
1100 a few tokens of its own. */
1101 static const char *const token_names[] = {
1102 /* cpplib-defined token types */
1103 #define OP(e, s) #e,
1104 #define TK(e, s) #e,
1105 TTYPE_TABLE
1106 #undef OP
1107 #undef TK
1108 /* C++ parser token types - see "Manifest constants", above. */
1109 "KEYWORD",
1110 "TEMPLATE_ID",
1111 "NESTED_NAME_SPECIFIER",
1112 };
1113
1114 /* For some tokens, print the associated data. */
1115 switch (token->type)
1116 {
1117 case CPP_KEYWORD:
1118 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1119 For example, `struct' is mapped to an INTEGER_CST. */
1120 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1121 break;
1122 /* else fall through */
1123 case CPP_NAME:
1124 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1125 break;
1126
1127 case CPP_STRING:
1128 case CPP_STRING16:
1129 case CPP_STRING32:
1130 case CPP_WSTRING:
1131 case CPP_UTF8STRING:
1132 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1133 break;
1134
1135 case CPP_NUMBER:
1136 print_generic_expr (stream, token->u.value, 0);
1137 break;
1138
1139 default:
1140 /* If we have a name for the token, print it out. Otherwise, we
1141 simply give the numeric code. */
1142 if (token->type < ARRAY_SIZE(token_names))
1143 fputs (token_names[token->type], stream);
1144 else
1145 fprintf (stream, "[%d]", token->type);
1146 break;
1147 }
1148 }
1149
1150 /* Start emitting debugging information. */
1151
1152 static void
1153 cp_lexer_start_debugging (cp_lexer* lexer)
1154 {
1155 lexer->debugging_p = true;
1156 cp_lexer_debug_stream = stderr;
1157 }
1158
1159 /* Stop emitting debugging information. */
1160
1161 static void
1162 cp_lexer_stop_debugging (cp_lexer* lexer)
1163 {
1164 lexer->debugging_p = false;
1165 cp_lexer_debug_stream = NULL;
1166 }
1167
1168 /* Create a new cp_token_cache, representing a range of tokens. */
1169
1170 static cp_token_cache *
1171 cp_token_cache_new (cp_token *first, cp_token *last)
1172 {
1173 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1174 cache->first = first;
1175 cache->last = last;
1176 return cache;
1177 }
1178
1179 \f
1180 /* Decl-specifiers. */
1181
1182 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1183
1184 static void
1185 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1186 {
1187 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1188 }
1189
1190 /* Declarators. */
1191
1192 /* Nothing other than the parser should be creating declarators;
1193 declarators are a semi-syntactic representation of C++ entities.
1194 Other parts of the front end that need to create entities (like
1195 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1196
1197 static cp_declarator *make_call_declarator
1198 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1199 static cp_declarator *make_array_declarator
1200 (cp_declarator *, tree);
1201 static cp_declarator *make_pointer_declarator
1202 (cp_cv_quals, cp_declarator *);
1203 static cp_declarator *make_reference_declarator
1204 (cp_cv_quals, cp_declarator *, bool);
1205 static cp_parameter_declarator *make_parameter_declarator
1206 (cp_decl_specifier_seq *, cp_declarator *, tree);
1207 static cp_declarator *make_ptrmem_declarator
1208 (cp_cv_quals, tree, cp_declarator *);
1209
1210 /* An erroneous declarator. */
1211 static cp_declarator *cp_error_declarator;
1212
1213 /* The obstack on which declarators and related data structures are
1214 allocated. */
1215 static struct obstack declarator_obstack;
1216
1217 /* Alloc BYTES from the declarator memory pool. */
1218
1219 static inline void *
1220 alloc_declarator (size_t bytes)
1221 {
1222 return obstack_alloc (&declarator_obstack, bytes);
1223 }
1224
1225 /* Allocate a declarator of the indicated KIND. Clear fields that are
1226 common to all declarators. */
1227
1228 static cp_declarator *
1229 make_declarator (cp_declarator_kind kind)
1230 {
1231 cp_declarator *declarator;
1232
1233 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1234 declarator->kind = kind;
1235 declarator->attributes = NULL_TREE;
1236 declarator->declarator = NULL;
1237 declarator->parameter_pack_p = false;
1238 declarator->id_loc = UNKNOWN_LOCATION;
1239
1240 return declarator;
1241 }
1242
1243 /* Make a declarator for a generalized identifier. If
1244 QUALIFYING_SCOPE is non-NULL, the identifier is
1245 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1246 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1247 is, if any. */
1248
1249 static cp_declarator *
1250 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1251 special_function_kind sfk)
1252 {
1253 cp_declarator *declarator;
1254
1255 /* It is valid to write:
1256
1257 class C { void f(); };
1258 typedef C D;
1259 void D::f();
1260
1261 The standard is not clear about whether `typedef const C D' is
1262 legal; as of 2002-09-15 the committee is considering that
1263 question. EDG 3.0 allows that syntax. Therefore, we do as
1264 well. */
1265 if (qualifying_scope && TYPE_P (qualifying_scope))
1266 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267
1268 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1269 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1270 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271
1272 declarator = make_declarator (cdk_id);
1273 declarator->u.id.qualifying_scope = qualifying_scope;
1274 declarator->u.id.unqualified_name = unqualified_name;
1275 declarator->u.id.sfk = sfk;
1276
1277 return declarator;
1278 }
1279
1280 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1281 of modifiers such as const or volatile to apply to the pointer
1282 type, represented as identifiers. */
1283
1284 cp_declarator *
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1286 {
1287 cp_declarator *declarator;
1288
1289 declarator = make_declarator (cdk_pointer);
1290 declarator->declarator = target;
1291 declarator->u.pointer.qualifiers = cv_qualifiers;
1292 declarator->u.pointer.class_type = NULL_TREE;
1293 if (target)
1294 {
1295 declarator->id_loc = target->id_loc;
1296 declarator->parameter_pack_p = target->parameter_pack_p;
1297 target->parameter_pack_p = false;
1298 }
1299 else
1300 declarator->parameter_pack_p = false;
1301
1302 return declarator;
1303 }
1304
1305 /* Like make_pointer_declarator -- but for references. */
1306
1307 cp_declarator *
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309 bool rvalue_ref)
1310 {
1311 cp_declarator *declarator;
1312
1313 declarator = make_declarator (cdk_reference);
1314 declarator->declarator = target;
1315 declarator->u.reference.qualifiers = cv_qualifiers;
1316 declarator->u.reference.rvalue_ref = rvalue_ref;
1317 if (target)
1318 {
1319 declarator->id_loc = target->id_loc;
1320 declarator->parameter_pack_p = target->parameter_pack_p;
1321 target->parameter_pack_p = false;
1322 }
1323 else
1324 declarator->parameter_pack_p = false;
1325
1326 return declarator;
1327 }
1328
1329 /* Like make_pointer_declarator -- but for a pointer to a non-static
1330 member of CLASS_TYPE. */
1331
1332 cp_declarator *
1333 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1334 cp_declarator *pointee)
1335 {
1336 cp_declarator *declarator;
1337
1338 declarator = make_declarator (cdk_ptrmem);
1339 declarator->declarator = pointee;
1340 declarator->u.pointer.qualifiers = cv_qualifiers;
1341 declarator->u.pointer.class_type = class_type;
1342
1343 if (pointee)
1344 {
1345 declarator->parameter_pack_p = pointee->parameter_pack_p;
1346 pointee->parameter_pack_p = false;
1347 }
1348 else
1349 declarator->parameter_pack_p = false;
1350
1351 return declarator;
1352 }
1353
1354 /* Make a declarator for the function given by TARGET, with the
1355 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1356 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1357 indicates what exceptions can be thrown. */
1358
1359 cp_declarator *
1360 make_call_declarator (cp_declarator *target,
1361 tree parms,
1362 cp_cv_quals cv_qualifiers,
1363 cp_virt_specifiers virt_specifiers,
1364 tree exception_specification,
1365 tree late_return_type)
1366 {
1367 cp_declarator *declarator;
1368
1369 declarator = make_declarator (cdk_function);
1370 declarator->declarator = target;
1371 declarator->u.function.parameters = parms;
1372 declarator->u.function.qualifiers = cv_qualifiers;
1373 declarator->u.function.virt_specifiers = virt_specifiers;
1374 declarator->u.function.exception_specification = exception_specification;
1375 declarator->u.function.late_return_type = late_return_type;
1376 if (target)
1377 {
1378 declarator->id_loc = target->id_loc;
1379 declarator->parameter_pack_p = target->parameter_pack_p;
1380 target->parameter_pack_p = false;
1381 }
1382 else
1383 declarator->parameter_pack_p = false;
1384
1385 return declarator;
1386 }
1387
1388 /* Make a declarator for an array of BOUNDS elements, each of which is
1389 defined by ELEMENT. */
1390
1391 cp_declarator *
1392 make_array_declarator (cp_declarator *element, tree bounds)
1393 {
1394 cp_declarator *declarator;
1395
1396 declarator = make_declarator (cdk_array);
1397 declarator->declarator = element;
1398 declarator->u.array.bounds = bounds;
1399 if (element)
1400 {
1401 declarator->id_loc = element->id_loc;
1402 declarator->parameter_pack_p = element->parameter_pack_p;
1403 element->parameter_pack_p = false;
1404 }
1405 else
1406 declarator->parameter_pack_p = false;
1407
1408 return declarator;
1409 }
1410
1411 /* Determine whether the declarator we've seen so far can be a
1412 parameter pack, when followed by an ellipsis. */
1413 static bool
1414 declarator_can_be_parameter_pack (cp_declarator *declarator)
1415 {
1416 /* Search for a declarator name, or any other declarator that goes
1417 after the point where the ellipsis could appear in a parameter
1418 pack. If we find any of these, then this declarator can not be
1419 made into a parameter pack. */
1420 bool found = false;
1421 while (declarator && !found)
1422 {
1423 switch ((int)declarator->kind)
1424 {
1425 case cdk_id:
1426 case cdk_array:
1427 found = true;
1428 break;
1429
1430 case cdk_error:
1431 return true;
1432
1433 default:
1434 declarator = declarator->declarator;
1435 break;
1436 }
1437 }
1438
1439 return !found;
1440 }
1441
1442 cp_parameter_declarator *no_parameters;
1443
1444 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1445 DECLARATOR and DEFAULT_ARGUMENT. */
1446
1447 cp_parameter_declarator *
1448 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1449 cp_declarator *declarator,
1450 tree default_argument)
1451 {
1452 cp_parameter_declarator *parameter;
1453
1454 parameter = ((cp_parameter_declarator *)
1455 alloc_declarator (sizeof (cp_parameter_declarator)));
1456 parameter->next = NULL;
1457 if (decl_specifiers)
1458 parameter->decl_specifiers = *decl_specifiers;
1459 else
1460 clear_decl_specs (&parameter->decl_specifiers);
1461 parameter->declarator = declarator;
1462 parameter->default_argument = default_argument;
1463 parameter->ellipsis_p = false;
1464
1465 return parameter;
1466 }
1467
1468 /* Returns true iff DECLARATOR is a declaration for a function. */
1469
1470 static bool
1471 function_declarator_p (const cp_declarator *declarator)
1472 {
1473 while (declarator)
1474 {
1475 if (declarator->kind == cdk_function
1476 && declarator->declarator->kind == cdk_id)
1477 return true;
1478 if (declarator->kind == cdk_id
1479 || declarator->kind == cdk_error)
1480 return false;
1481 declarator = declarator->declarator;
1482 }
1483 return false;
1484 }
1485
1486 /* The parser. */
1487
1488 /* Overview
1489 --------
1490
1491 A cp_parser parses the token stream as specified by the C++
1492 grammar. Its job is purely parsing, not semantic analysis. For
1493 example, the parser breaks the token stream into declarators,
1494 expressions, statements, and other similar syntactic constructs.
1495 It does not check that the types of the expressions on either side
1496 of an assignment-statement are compatible, or that a function is
1497 not declared with a parameter of type `void'.
1498
1499 The parser invokes routines elsewhere in the compiler to perform
1500 semantic analysis and to build up the abstract syntax tree for the
1501 code processed.
1502
1503 The parser (and the template instantiation code, which is, in a
1504 way, a close relative of parsing) are the only parts of the
1505 compiler that should be calling push_scope and pop_scope, or
1506 related functions. The parser (and template instantiation code)
1507 keeps track of what scope is presently active; everything else
1508 should simply honor that. (The code that generates static
1509 initializers may also need to set the scope, in order to check
1510 access control correctly when emitting the initializers.)
1511
1512 Methodology
1513 -----------
1514
1515 The parser is of the standard recursive-descent variety. Upcoming
1516 tokens in the token stream are examined in order to determine which
1517 production to use when parsing a non-terminal. Some C++ constructs
1518 require arbitrary look ahead to disambiguate. For example, it is
1519 impossible, in the general case, to tell whether a statement is an
1520 expression or declaration without scanning the entire statement.
1521 Therefore, the parser is capable of "parsing tentatively." When the
1522 parser is not sure what construct comes next, it enters this mode.
1523 Then, while we attempt to parse the construct, the parser queues up
1524 error messages, rather than issuing them immediately, and saves the
1525 tokens it consumes. If the construct is parsed successfully, the
1526 parser "commits", i.e., it issues any queued error messages and
1527 the tokens that were being preserved are permanently discarded.
1528 If, however, the construct is not parsed successfully, the parser
1529 rolls back its state completely so that it can resume parsing using
1530 a different alternative.
1531
1532 Future Improvements
1533 -------------------
1534
1535 The performance of the parser could probably be improved substantially.
1536 We could often eliminate the need to parse tentatively by looking ahead
1537 a little bit. In some places, this approach might not entirely eliminate
1538 the need to parse tentatively, but it might still speed up the average
1539 case. */
1540
1541 /* Flags that are passed to some parsing functions. These values can
1542 be bitwise-ored together. */
1543
1544 enum
1545 {
1546 /* No flags. */
1547 CP_PARSER_FLAGS_NONE = 0x0,
1548 /* The construct is optional. If it is not present, then no error
1549 should be issued. */
1550 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1551 /* When parsing a type-specifier, treat user-defined type-names
1552 as non-type identifiers. */
1553 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1554 /* When parsing a type-specifier, do not try to parse a class-specifier
1555 or enum-specifier. */
1556 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1557 /* When parsing a decl-specifier-seq, only allow type-specifier or
1558 constexpr. */
1559 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1560 };
1561
1562 /* This type is used for parameters and variables which hold
1563 combinations of the above flags. */
1564 typedef int cp_parser_flags;
1565
1566 /* The different kinds of declarators we want to parse. */
1567
1568 typedef enum cp_parser_declarator_kind
1569 {
1570 /* We want an abstract declarator. */
1571 CP_PARSER_DECLARATOR_ABSTRACT,
1572 /* We want a named declarator. */
1573 CP_PARSER_DECLARATOR_NAMED,
1574 /* We don't mind, but the name must be an unqualified-id. */
1575 CP_PARSER_DECLARATOR_EITHER
1576 } cp_parser_declarator_kind;
1577
1578 /* The precedence values used to parse binary expressions. The minimum value
1579 of PREC must be 1, because zero is reserved to quickly discriminate
1580 binary operators from other tokens. */
1581
1582 enum cp_parser_prec
1583 {
1584 PREC_NOT_OPERATOR,
1585 PREC_LOGICAL_OR_EXPRESSION,
1586 PREC_LOGICAL_AND_EXPRESSION,
1587 PREC_INCLUSIVE_OR_EXPRESSION,
1588 PREC_EXCLUSIVE_OR_EXPRESSION,
1589 PREC_AND_EXPRESSION,
1590 PREC_EQUALITY_EXPRESSION,
1591 PREC_RELATIONAL_EXPRESSION,
1592 PREC_SHIFT_EXPRESSION,
1593 PREC_ADDITIVE_EXPRESSION,
1594 PREC_MULTIPLICATIVE_EXPRESSION,
1595 PREC_PM_EXPRESSION,
1596 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1597 };
1598
1599 /* A mapping from a token type to a corresponding tree node type, with a
1600 precedence value. */
1601
1602 typedef struct cp_parser_binary_operations_map_node
1603 {
1604 /* The token type. */
1605 enum cpp_ttype token_type;
1606 /* The corresponding tree code. */
1607 enum tree_code tree_type;
1608 /* The precedence of this operator. */
1609 enum cp_parser_prec prec;
1610 } cp_parser_binary_operations_map_node;
1611
1612 typedef struct cp_parser_expression_stack_entry
1613 {
1614 /* Left hand side of the binary operation we are currently
1615 parsing. */
1616 tree lhs;
1617 /* Original tree code for left hand side, if it was a binary
1618 expression itself (used for -Wparentheses). */
1619 enum tree_code lhs_type;
1620 /* Tree code for the binary operation we are parsing. */
1621 enum tree_code tree_type;
1622 /* Precedence of the binary operation we are parsing. */
1623 enum cp_parser_prec prec;
1624 } cp_parser_expression_stack_entry;
1625
1626 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1627 entries because precedence levels on the stack are monotonically
1628 increasing. */
1629 typedef struct cp_parser_expression_stack_entry
1630 cp_parser_expression_stack[NUM_PREC_VALUES];
1631
1632 /* Prototypes. */
1633
1634 /* Constructors and destructors. */
1635
1636 static cp_parser_context *cp_parser_context_new
1637 (cp_parser_context *);
1638
1639 /* Class variables. */
1640
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1642
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644 Transformed into an associative array (binops_by_token) by
1645 cp_parser_new. */
1646
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1650
1651 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654
1655 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657
1658 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660
1661 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665
1666 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1668
1669 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1670
1671 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1672
1673 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1674
1675 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1676
1677 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1678 };
1679
1680 /* The same as binops, but initialized by cp_parser_new so that
1681 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1682 for speed. */
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1684
1685 /* Constructors and destructors. */
1686
1687 /* Construct a new context. The context below this one on the stack
1688 is given by NEXT. */
1689
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1692 {
1693 cp_parser_context *context;
1694
1695 /* Allocate the storage. */
1696 if (cp_parser_context_free_list != NULL)
1697 {
1698 /* Pull the first entry from the free list. */
1699 context = cp_parser_context_free_list;
1700 cp_parser_context_free_list = context->next;
1701 memset (context, 0, sizeof (*context));
1702 }
1703 else
1704 context = ggc_alloc_cleared_cp_parser_context ();
1705
1706 /* No errors have occurred yet in this context. */
1707 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708 /* If this is not the bottommost context, copy information that we
1709 need from the previous context. */
1710 if (next)
1711 {
1712 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713 expression, then we are parsing one in this context, too. */
1714 context->object_type = next->object_type;
1715 /* Thread the stack. */
1716 context->next = next;
1717 }
1718
1719 return context;
1720 }
1721
1722 /* Managing the unparsed function queues. */
1723
1724 #define unparsed_funs_with_default_args \
1725 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1728 #define unparsed_nsdmis \
1729 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1730
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1733 {
1734 VEC_safe_push (cp_unparsed_functions_entry, gc,
1735 parser->unparsed_queues, NULL);
1736 unparsed_funs_with_default_args = NULL;
1737 unparsed_funs_with_definitions = make_tree_vector ();
1738 unparsed_nsdmis = NULL;
1739 }
1740
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1743 {
1744 release_tree_vector (unparsed_funs_with_definitions);
1745 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1746 }
1747
1748 /* Prototypes. */
1749
1750 /* Constructors and destructors. */
1751
1752 static cp_parser *cp_parser_new
1753 (void);
1754
1755 /* Routines to parse various constructs.
1756
1757 Those that return `tree' will return the error_mark_node (rather
1758 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759 Sometimes, they will return an ordinary node if error-recovery was
1760 attempted, even though a parse error occurred. So, to check
1761 whether or not a parse error occurred, you should always use
1762 cp_parser_error_occurred. If the construct is optional (indicated
1763 either by an `_opt' in the name of the function that does the
1764 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765 the construct is not present. */
1766
1767 /* Lexical conventions [gram.lex] */
1768
1769 static tree cp_parser_identifier
1770 (cp_parser *);
1771 static tree cp_parser_string_literal
1772 (cp_parser *, bool, bool);
1773 static tree cp_parser_userdef_char_literal
1774 (cp_parser *);
1775 static tree cp_parser_userdef_string_literal
1776 (cp_token *);
1777 static tree cp_parser_userdef_numeric_literal
1778 (cp_parser *);
1779
1780 /* Basic concepts [gram.basic] */
1781
1782 static bool cp_parser_translation_unit
1783 (cp_parser *);
1784
1785 /* Expressions [gram.expr] */
1786
1787 static tree cp_parser_primary_expression
1788 (cp_parser *, bool, bool, bool, cp_id_kind *);
1789 static tree cp_parser_id_expression
1790 (cp_parser *, bool, bool, bool *, bool, bool);
1791 static tree cp_parser_unqualified_id
1792 (cp_parser *, bool, bool, bool, bool);
1793 static tree cp_parser_nested_name_specifier_opt
1794 (cp_parser *, bool, bool, bool, bool);
1795 static tree cp_parser_nested_name_specifier
1796 (cp_parser *, bool, bool, bool, bool);
1797 static tree cp_parser_qualifying_entity
1798 (cp_parser *, bool, bool, bool, bool, bool);
1799 static tree cp_parser_postfix_expression
1800 (cp_parser *, bool, bool, bool, cp_id_kind *);
1801 static tree cp_parser_postfix_open_square_expression
1802 (cp_parser *, tree, bool);
1803 static tree cp_parser_postfix_dot_deref_expression
1804 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1805 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1806 (cp_parser *, int, bool, bool, bool *);
1807 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1808 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1809 static void cp_parser_pseudo_destructor_name
1810 (cp_parser *, tree *, tree *);
1811 static tree cp_parser_unary_expression
1812 (cp_parser *, bool, bool, cp_id_kind *);
1813 static enum tree_code cp_parser_unary_operator
1814 (cp_token *);
1815 static tree cp_parser_new_expression
1816 (cp_parser *);
1817 static VEC(tree,gc) *cp_parser_new_placement
1818 (cp_parser *);
1819 static tree cp_parser_new_type_id
1820 (cp_parser *, tree *);
1821 static cp_declarator *cp_parser_new_declarator_opt
1822 (cp_parser *);
1823 static cp_declarator *cp_parser_direct_new_declarator
1824 (cp_parser *);
1825 static VEC(tree,gc) *cp_parser_new_initializer
1826 (cp_parser *);
1827 static tree cp_parser_delete_expression
1828 (cp_parser *);
1829 static tree cp_parser_cast_expression
1830 (cp_parser *, bool, bool, cp_id_kind *);
1831 static tree cp_parser_binary_expression
1832 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1833 static tree cp_parser_question_colon_clause
1834 (cp_parser *, tree);
1835 static tree cp_parser_assignment_expression
1836 (cp_parser *, bool, cp_id_kind *);
1837 static enum tree_code cp_parser_assignment_operator_opt
1838 (cp_parser *);
1839 static tree cp_parser_expression
1840 (cp_parser *, bool, cp_id_kind *);
1841 static tree cp_parser_constant_expression
1842 (cp_parser *, bool, bool *);
1843 static tree cp_parser_builtin_offsetof
1844 (cp_parser *);
1845 static tree cp_parser_lambda_expression
1846 (cp_parser *);
1847 static void cp_parser_lambda_introducer
1848 (cp_parser *, tree);
1849 static bool cp_parser_lambda_declarator_opt
1850 (cp_parser *, tree);
1851 static void cp_parser_lambda_body
1852 (cp_parser *, tree);
1853
1854 /* Statements [gram.stmt.stmt] */
1855
1856 static void cp_parser_statement
1857 (cp_parser *, tree, bool, bool *);
1858 static void cp_parser_label_for_labeled_statement
1859 (cp_parser *);
1860 static tree cp_parser_expression_statement
1861 (cp_parser *, tree);
1862 static tree cp_parser_compound_statement
1863 (cp_parser *, tree, bool, bool);
1864 static void cp_parser_statement_seq_opt
1865 (cp_parser *, tree);
1866 static tree cp_parser_selection_statement
1867 (cp_parser *, bool *);
1868 static tree cp_parser_condition
1869 (cp_parser *);
1870 static tree cp_parser_iteration_statement
1871 (cp_parser *);
1872 static bool cp_parser_for_init_statement
1873 (cp_parser *, tree *decl);
1874 static tree cp_parser_for
1875 (cp_parser *);
1876 static tree cp_parser_c_for
1877 (cp_parser *, tree, tree);
1878 static tree cp_parser_range_for
1879 (cp_parser *, tree, tree, tree);
1880 static void do_range_for_auto_deduction
1881 (tree, tree);
1882 static tree cp_parser_perform_range_for_lookup
1883 (tree, tree *, tree *);
1884 static tree cp_parser_range_for_member_function
1885 (tree, tree);
1886 static tree cp_parser_jump_statement
1887 (cp_parser *);
1888 static void cp_parser_declaration_statement
1889 (cp_parser *);
1890
1891 static tree cp_parser_implicitly_scoped_statement
1892 (cp_parser *, bool *);
1893 static void cp_parser_already_scoped_statement
1894 (cp_parser *);
1895
1896 /* Declarations [gram.dcl.dcl] */
1897
1898 static void cp_parser_declaration_seq_opt
1899 (cp_parser *);
1900 static void cp_parser_declaration
1901 (cp_parser *);
1902 static void cp_parser_block_declaration
1903 (cp_parser *, bool);
1904 static void cp_parser_simple_declaration
1905 (cp_parser *, bool, tree *);
1906 static void cp_parser_decl_specifier_seq
1907 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1908 static tree cp_parser_storage_class_specifier_opt
1909 (cp_parser *);
1910 static tree cp_parser_function_specifier_opt
1911 (cp_parser *, cp_decl_specifier_seq *);
1912 static tree cp_parser_type_specifier
1913 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1914 int *, bool *);
1915 static tree cp_parser_simple_type_specifier
1916 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1917 static tree cp_parser_type_name
1918 (cp_parser *);
1919 static tree cp_parser_nonclass_name
1920 (cp_parser* parser);
1921 static tree cp_parser_elaborated_type_specifier
1922 (cp_parser *, bool, bool);
1923 static tree cp_parser_enum_specifier
1924 (cp_parser *);
1925 static void cp_parser_enumerator_list
1926 (cp_parser *, tree);
1927 static void cp_parser_enumerator_definition
1928 (cp_parser *, tree);
1929 static tree cp_parser_namespace_name
1930 (cp_parser *);
1931 static void cp_parser_namespace_definition
1932 (cp_parser *);
1933 static void cp_parser_namespace_body
1934 (cp_parser *);
1935 static tree cp_parser_qualified_namespace_specifier
1936 (cp_parser *);
1937 static void cp_parser_namespace_alias_definition
1938 (cp_parser *);
1939 static bool cp_parser_using_declaration
1940 (cp_parser *, bool);
1941 static void cp_parser_using_directive
1942 (cp_parser *);
1943 static tree cp_parser_alias_declaration
1944 (cp_parser *);
1945 static void cp_parser_asm_definition
1946 (cp_parser *);
1947 static void cp_parser_linkage_specification
1948 (cp_parser *);
1949 static void cp_parser_static_assert
1950 (cp_parser *, bool);
1951 static tree cp_parser_decltype
1952 (cp_parser *);
1953
1954 /* Declarators [gram.dcl.decl] */
1955
1956 static tree cp_parser_init_declarator
1957 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1958 static cp_declarator *cp_parser_declarator
1959 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1960 static cp_declarator *cp_parser_direct_declarator
1961 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1962 static enum tree_code cp_parser_ptr_operator
1963 (cp_parser *, tree *, cp_cv_quals *);
1964 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1965 (cp_parser *);
1966 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1967 (cp_parser *);
1968 static tree cp_parser_late_return_type_opt
1969 (cp_parser *, cp_cv_quals);
1970 static tree cp_parser_declarator_id
1971 (cp_parser *, bool);
1972 static tree cp_parser_type_id
1973 (cp_parser *);
1974 static tree cp_parser_template_type_arg
1975 (cp_parser *);
1976 static tree cp_parser_trailing_type_id (cp_parser *);
1977 static tree cp_parser_type_id_1
1978 (cp_parser *, bool, bool);
1979 static void cp_parser_type_specifier_seq
1980 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1981 static tree cp_parser_parameter_declaration_clause
1982 (cp_parser *);
1983 static tree cp_parser_parameter_declaration_list
1984 (cp_parser *, bool *);
1985 static cp_parameter_declarator *cp_parser_parameter_declaration
1986 (cp_parser *, bool, bool *);
1987 static tree cp_parser_default_argument
1988 (cp_parser *, bool);
1989 static void cp_parser_function_body
1990 (cp_parser *);
1991 static tree cp_parser_initializer
1992 (cp_parser *, bool *, bool *);
1993 static tree cp_parser_initializer_clause
1994 (cp_parser *, bool *);
1995 static tree cp_parser_braced_list
1996 (cp_parser*, bool*);
1997 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1998 (cp_parser *, bool *);
1999
2000 static bool cp_parser_ctor_initializer_opt_and_function_body
2001 (cp_parser *);
2002
2003 /* Classes [gram.class] */
2004
2005 static tree cp_parser_class_name
2006 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2007 static tree cp_parser_class_specifier
2008 (cp_parser *);
2009 static tree cp_parser_class_head
2010 (cp_parser *, bool *, tree *, tree *);
2011 static enum tag_types cp_parser_class_key
2012 (cp_parser *);
2013 static void cp_parser_member_specification_opt
2014 (cp_parser *);
2015 static void cp_parser_member_declaration
2016 (cp_parser *);
2017 static tree cp_parser_pure_specifier
2018 (cp_parser *);
2019 static tree cp_parser_constant_initializer
2020 (cp_parser *);
2021
2022 /* Derived classes [gram.class.derived] */
2023
2024 static tree cp_parser_base_clause
2025 (cp_parser *);
2026 static tree cp_parser_base_specifier
2027 (cp_parser *);
2028
2029 /* Special member functions [gram.special] */
2030
2031 static tree cp_parser_conversion_function_id
2032 (cp_parser *);
2033 static tree cp_parser_conversion_type_id
2034 (cp_parser *);
2035 static cp_declarator *cp_parser_conversion_declarator_opt
2036 (cp_parser *);
2037 static bool cp_parser_ctor_initializer_opt
2038 (cp_parser *);
2039 static void cp_parser_mem_initializer_list
2040 (cp_parser *);
2041 static tree cp_parser_mem_initializer
2042 (cp_parser *);
2043 static tree cp_parser_mem_initializer_id
2044 (cp_parser *);
2045
2046 /* Overloading [gram.over] */
2047
2048 static tree cp_parser_operator_function_id
2049 (cp_parser *);
2050 static tree cp_parser_operator
2051 (cp_parser *);
2052
2053 /* Templates [gram.temp] */
2054
2055 static void cp_parser_template_declaration
2056 (cp_parser *, bool);
2057 static tree cp_parser_template_parameter_list
2058 (cp_parser *);
2059 static tree cp_parser_template_parameter
2060 (cp_parser *, bool *, bool *);
2061 static tree cp_parser_type_parameter
2062 (cp_parser *, bool *);
2063 static tree cp_parser_template_id
2064 (cp_parser *, bool, bool, bool);
2065 static tree cp_parser_template_name
2066 (cp_parser *, bool, bool, bool, bool *);
2067 static tree cp_parser_template_argument_list
2068 (cp_parser *);
2069 static tree cp_parser_template_argument
2070 (cp_parser *);
2071 static void cp_parser_explicit_instantiation
2072 (cp_parser *);
2073 static void cp_parser_explicit_specialization
2074 (cp_parser *);
2075
2076 /* Exception handling [gram.exception] */
2077
2078 static tree cp_parser_try_block
2079 (cp_parser *);
2080 static bool cp_parser_function_try_block
2081 (cp_parser *);
2082 static void cp_parser_handler_seq
2083 (cp_parser *);
2084 static void cp_parser_handler
2085 (cp_parser *);
2086 static tree cp_parser_exception_declaration
2087 (cp_parser *);
2088 static tree cp_parser_throw_expression
2089 (cp_parser *);
2090 static tree cp_parser_exception_specification_opt
2091 (cp_parser *);
2092 static tree cp_parser_type_id_list
2093 (cp_parser *);
2094
2095 /* GNU Extensions */
2096
2097 static tree cp_parser_asm_specification_opt
2098 (cp_parser *);
2099 static tree cp_parser_asm_operand_list
2100 (cp_parser *);
2101 static tree cp_parser_asm_clobber_list
2102 (cp_parser *);
2103 static tree cp_parser_asm_label_list
2104 (cp_parser *);
2105 static tree cp_parser_attributes_opt
2106 (cp_parser *);
2107 static tree cp_parser_attribute_list
2108 (cp_parser *);
2109 static bool cp_parser_extension_opt
2110 (cp_parser *, int *);
2111 static void cp_parser_label_declaration
2112 (cp_parser *);
2113
2114 /* Transactional Memory Extensions */
2115
2116 static tree cp_parser_transaction
2117 (cp_parser *, enum rid);
2118 static tree cp_parser_transaction_expression
2119 (cp_parser *, enum rid);
2120 static bool cp_parser_function_transaction
2121 (cp_parser *, enum rid);
2122 static tree cp_parser_transaction_cancel
2123 (cp_parser *);
2124
2125 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2126 static bool cp_parser_pragma
2127 (cp_parser *, enum pragma_context);
2128
2129 /* Objective-C++ Productions */
2130
2131 static tree cp_parser_objc_message_receiver
2132 (cp_parser *);
2133 static tree cp_parser_objc_message_args
2134 (cp_parser *);
2135 static tree cp_parser_objc_message_expression
2136 (cp_parser *);
2137 static tree cp_parser_objc_encode_expression
2138 (cp_parser *);
2139 static tree cp_parser_objc_defs_expression
2140 (cp_parser *);
2141 static tree cp_parser_objc_protocol_expression
2142 (cp_parser *);
2143 static tree cp_parser_objc_selector_expression
2144 (cp_parser *);
2145 static tree cp_parser_objc_expression
2146 (cp_parser *);
2147 static bool cp_parser_objc_selector_p
2148 (enum cpp_ttype);
2149 static tree cp_parser_objc_selector
2150 (cp_parser *);
2151 static tree cp_parser_objc_protocol_refs_opt
2152 (cp_parser *);
2153 static void cp_parser_objc_declaration
2154 (cp_parser *, tree);
2155 static tree cp_parser_objc_statement
2156 (cp_parser *);
2157 static bool cp_parser_objc_valid_prefix_attributes
2158 (cp_parser *, tree *);
2159 static void cp_parser_objc_at_property_declaration
2160 (cp_parser *) ;
2161 static void cp_parser_objc_at_synthesize_declaration
2162 (cp_parser *) ;
2163 static void cp_parser_objc_at_dynamic_declaration
2164 (cp_parser *) ;
2165 static tree cp_parser_objc_struct_declaration
2166 (cp_parser *) ;
2167
2168 /* Utility Routines */
2169
2170 static tree cp_parser_lookup_name
2171 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2172 static tree cp_parser_lookup_name_simple
2173 (cp_parser *, tree, location_t);
2174 static tree cp_parser_maybe_treat_template_as_class
2175 (tree, bool);
2176 static bool cp_parser_check_declarator_template_parameters
2177 (cp_parser *, cp_declarator *, location_t);
2178 static bool cp_parser_check_template_parameters
2179 (cp_parser *, unsigned, location_t, cp_declarator *);
2180 static tree cp_parser_simple_cast_expression
2181 (cp_parser *);
2182 static tree cp_parser_global_scope_opt
2183 (cp_parser *, bool);
2184 static bool cp_parser_constructor_declarator_p
2185 (cp_parser *, bool);
2186 static tree cp_parser_function_definition_from_specifiers_and_declarator
2187 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2188 static tree cp_parser_function_definition_after_declarator
2189 (cp_parser *, bool);
2190 static void cp_parser_template_declaration_after_export
2191 (cp_parser *, bool);
2192 static void cp_parser_perform_template_parameter_access_checks
2193 (VEC (deferred_access_check,gc)*);
2194 static tree cp_parser_single_declaration
2195 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2196 static tree cp_parser_functional_cast
2197 (cp_parser *, tree);
2198 static tree cp_parser_save_member_function_body
2199 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2200 static tree cp_parser_save_nsdmi
2201 (cp_parser *);
2202 static tree cp_parser_enclosed_template_argument_list
2203 (cp_parser *);
2204 static void cp_parser_save_default_args
2205 (cp_parser *, tree);
2206 static void cp_parser_late_parsing_for_member
2207 (cp_parser *, tree);
2208 static tree cp_parser_late_parse_one_default_arg
2209 (cp_parser *, tree, tree, tree);
2210 static void cp_parser_late_parsing_nsdmi
2211 (cp_parser *, tree);
2212 static void cp_parser_late_parsing_default_args
2213 (cp_parser *, tree);
2214 static tree cp_parser_sizeof_operand
2215 (cp_parser *, enum rid);
2216 static tree cp_parser_trait_expr
2217 (cp_parser *, enum rid);
2218 static bool cp_parser_declares_only_class_p
2219 (cp_parser *);
2220 static void cp_parser_set_storage_class
2221 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2222 static void cp_parser_set_decl_spec_type
2223 (cp_decl_specifier_seq *, tree, location_t, bool);
2224 static bool cp_parser_friend_p
2225 (const cp_decl_specifier_seq *);
2226 static void cp_parser_required_error
2227 (cp_parser *, required_token, bool);
2228 static cp_token *cp_parser_require
2229 (cp_parser *, enum cpp_ttype, required_token);
2230 static cp_token *cp_parser_require_keyword
2231 (cp_parser *, enum rid, required_token);
2232 static bool cp_parser_token_starts_function_definition_p
2233 (cp_token *);
2234 static bool cp_parser_next_token_starts_class_definition_p
2235 (cp_parser *);
2236 static bool cp_parser_next_token_ends_template_argument_p
2237 (cp_parser *);
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239 (cp_parser *, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2241 (cp_token *);
2242 static void cp_parser_check_class_key
2243 (enum tag_types, tree type);
2244 static void cp_parser_check_access_in_redeclaration
2245 (tree type, location_t location);
2246 static bool cp_parser_optional_template_keyword
2247 (cp_parser *);
2248 static void cp_parser_pre_parsed_nested_name_specifier
2249 (cp_parser *);
2250 static bool cp_parser_cache_group
2251 (cp_parser *, enum cpp_ttype, unsigned);
2252 static void cp_parser_parse_tentatively
2253 (cp_parser *);
2254 static void cp_parser_commit_to_tentative_parse
2255 (cp_parser *);
2256 static void cp_parser_abort_tentative_parse
2257 (cp_parser *);
2258 static bool cp_parser_parse_definitely
2259 (cp_parser *);
2260 static inline bool cp_parser_parsing_tentatively
2261 (cp_parser *);
2262 static bool cp_parser_uncommitted_to_tentative_parse_p
2263 (cp_parser *);
2264 static void cp_parser_error
2265 (cp_parser *, const char *);
2266 static void cp_parser_name_lookup_error
2267 (cp_parser *, tree, tree, name_lookup_error, location_t);
2268 static bool cp_parser_simulate_error
2269 (cp_parser *);
2270 static bool cp_parser_check_type_definition
2271 (cp_parser *);
2272 static void cp_parser_check_for_definition_in_return_type
2273 (cp_declarator *, tree, location_t type_location);
2274 static void cp_parser_check_for_invalid_template_id
2275 (cp_parser *, tree, location_t location);
2276 static bool cp_parser_non_integral_constant_expression
2277 (cp_parser *, non_integral_constant);
2278 static void cp_parser_diagnose_invalid_type_name
2279 (cp_parser *, tree, tree, location_t);
2280 static bool cp_parser_parse_and_diagnose_invalid_type_name
2281 (cp_parser *);
2282 static int cp_parser_skip_to_closing_parenthesis
2283 (cp_parser *, bool, bool, bool);
2284 static void cp_parser_skip_to_end_of_statement
2285 (cp_parser *);
2286 static void cp_parser_consume_semicolon_at_end_of_statement
2287 (cp_parser *);
2288 static void cp_parser_skip_to_end_of_block_or_statement
2289 (cp_parser *);
2290 static bool cp_parser_skip_to_closing_brace
2291 (cp_parser *);
2292 static void cp_parser_skip_to_end_of_template_parameter_list
2293 (cp_parser *);
2294 static void cp_parser_skip_to_pragma_eol
2295 (cp_parser*, cp_token *);
2296 static bool cp_parser_error_occurred
2297 (cp_parser *);
2298 static bool cp_parser_allow_gnu_extensions_p
2299 (cp_parser *);
2300 static bool cp_parser_is_pure_string_literal
2301 (cp_token *);
2302 static bool cp_parser_is_string_literal
2303 (cp_token *);
2304 static bool cp_parser_is_keyword
2305 (cp_token *, enum rid);
2306 static tree cp_parser_make_typename_type
2307 (cp_parser *, tree, tree, location_t location);
2308 static cp_declarator * cp_parser_make_indirect_declarator
2309 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2310
2311 /* Returns nonzero if we are parsing tentatively. */
2312
2313 static inline bool
2314 cp_parser_parsing_tentatively (cp_parser* parser)
2315 {
2316 return parser->context->next != NULL;
2317 }
2318
2319 /* Returns nonzero if TOKEN is a string literal. */
2320
2321 static bool
2322 cp_parser_is_pure_string_literal (cp_token* token)
2323 {
2324 return (token->type == CPP_STRING ||
2325 token->type == CPP_STRING16 ||
2326 token->type == CPP_STRING32 ||
2327 token->type == CPP_WSTRING ||
2328 token->type == CPP_UTF8STRING);
2329 }
2330
2331 /* Returns nonzero if TOKEN is a string literal
2332 of a user-defined string literal. */
2333
2334 static bool
2335 cp_parser_is_string_literal (cp_token* token)
2336 {
2337 return (cp_parser_is_pure_string_literal (token) ||
2338 token->type == CPP_STRING_USERDEF ||
2339 token->type == CPP_STRING16_USERDEF ||
2340 token->type == CPP_STRING32_USERDEF ||
2341 token->type == CPP_WSTRING_USERDEF ||
2342 token->type == CPP_UTF8STRING_USERDEF);
2343 }
2344
2345 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2346
2347 static bool
2348 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2349 {
2350 return token->keyword == keyword;
2351 }
2352
2353 /* If not parsing tentatively, issue a diagnostic of the form
2354 FILE:LINE: MESSAGE before TOKEN
2355 where TOKEN is the next token in the input stream. MESSAGE
2356 (specified by the caller) is usually of the form "expected
2357 OTHER-TOKEN". */
2358
2359 static void
2360 cp_parser_error (cp_parser* parser, const char* gmsgid)
2361 {
2362 if (!cp_parser_simulate_error (parser))
2363 {
2364 cp_token *token = cp_lexer_peek_token (parser->lexer);
2365 /* This diagnostic makes more sense if it is tagged to the line
2366 of the token we just peeked at. */
2367 cp_lexer_set_source_position_from_token (token);
2368
2369 if (token->type == CPP_PRAGMA)
2370 {
2371 error_at (token->location,
2372 "%<#pragma%> is not allowed here");
2373 cp_parser_skip_to_pragma_eol (parser, token);
2374 return;
2375 }
2376
2377 c_parse_error (gmsgid,
2378 /* Because c_parser_error does not understand
2379 CPP_KEYWORD, keywords are treated like
2380 identifiers. */
2381 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2382 token->u.value, token->flags);
2383 }
2384 }
2385
2386 /* Issue an error about name-lookup failing. NAME is the
2387 IDENTIFIER_NODE DECL is the result of
2388 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2389 the thing that we hoped to find. */
2390
2391 static void
2392 cp_parser_name_lookup_error (cp_parser* parser,
2393 tree name,
2394 tree decl,
2395 name_lookup_error desired,
2396 location_t location)
2397 {
2398 /* If name lookup completely failed, tell the user that NAME was not
2399 declared. */
2400 if (decl == error_mark_node)
2401 {
2402 if (parser->scope && parser->scope != global_namespace)
2403 error_at (location, "%<%E::%E%> has not been declared",
2404 parser->scope, name);
2405 else if (parser->scope == global_namespace)
2406 error_at (location, "%<::%E%> has not been declared", name);
2407 else if (parser->object_scope
2408 && !CLASS_TYPE_P (parser->object_scope))
2409 error_at (location, "request for member %qE in non-class type %qT",
2410 name, parser->object_scope);
2411 else if (parser->object_scope)
2412 error_at (location, "%<%T::%E%> has not been declared",
2413 parser->object_scope, name);
2414 else
2415 error_at (location, "%qE has not been declared", name);
2416 }
2417 else if (parser->scope && parser->scope != global_namespace)
2418 {
2419 switch (desired)
2420 {
2421 case NLE_TYPE:
2422 error_at (location, "%<%E::%E%> is not a type",
2423 parser->scope, name);
2424 break;
2425 case NLE_CXX98:
2426 error_at (location, "%<%E::%E%> is not a class or namespace",
2427 parser->scope, name);
2428 break;
2429 case NLE_NOT_CXX98:
2430 error_at (location,
2431 "%<%E::%E%> is not a class, namespace, or enumeration",
2432 parser->scope, name);
2433 break;
2434 default:
2435 gcc_unreachable ();
2436
2437 }
2438 }
2439 else if (parser->scope == global_namespace)
2440 {
2441 switch (desired)
2442 {
2443 case NLE_TYPE:
2444 error_at (location, "%<::%E%> is not a type", name);
2445 break;
2446 case NLE_CXX98:
2447 error_at (location, "%<::%E%> is not a class or namespace", name);
2448 break;
2449 case NLE_NOT_CXX98:
2450 error_at (location,
2451 "%<::%E%> is not a class, namespace, or enumeration",
2452 name);
2453 break;
2454 default:
2455 gcc_unreachable ();
2456 }
2457 }
2458 else
2459 {
2460 switch (desired)
2461 {
2462 case NLE_TYPE:
2463 error_at (location, "%qE is not a type", name);
2464 break;
2465 case NLE_CXX98:
2466 error_at (location, "%qE is not a class or namespace", name);
2467 break;
2468 case NLE_NOT_CXX98:
2469 error_at (location,
2470 "%qE is not a class, namespace, or enumeration", name);
2471 break;
2472 default:
2473 gcc_unreachable ();
2474 }
2475 }
2476 }
2477
2478 /* If we are parsing tentatively, remember that an error has occurred
2479 during this tentative parse. Returns true if the error was
2480 simulated; false if a message should be issued by the caller. */
2481
2482 static bool
2483 cp_parser_simulate_error (cp_parser* parser)
2484 {
2485 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2486 {
2487 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2488 return true;
2489 }
2490 return false;
2491 }
2492
2493 /* Check for repeated decl-specifiers. */
2494
2495 static void
2496 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2497 location_t location)
2498 {
2499 int ds;
2500
2501 for (ds = ds_first; ds != ds_last; ++ds)
2502 {
2503 unsigned count = decl_specs->specs[ds];
2504 if (count < 2)
2505 continue;
2506 /* The "long" specifier is a special case because of "long long". */
2507 if (ds == ds_long)
2508 {
2509 if (count > 2)
2510 error_at (location, "%<long long long%> is too long for GCC");
2511 else
2512 pedwarn_cxx98 (location, OPT_Wlong_long,
2513 "ISO C++ 1998 does not support %<long long%>");
2514 }
2515 else if (count > 1)
2516 {
2517 static const char *const decl_spec_names[] = {
2518 "signed",
2519 "unsigned",
2520 "short",
2521 "long",
2522 "const",
2523 "volatile",
2524 "restrict",
2525 "inline",
2526 "virtual",
2527 "explicit",
2528 "friend",
2529 "typedef",
2530 "using",
2531 "constexpr",
2532 "__complex",
2533 "__thread"
2534 };
2535 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2536 }
2537 }
2538 }
2539
2540 /* This function is called when a type is defined. If type
2541 definitions are forbidden at this point, an error message is
2542 issued. */
2543
2544 static bool
2545 cp_parser_check_type_definition (cp_parser* parser)
2546 {
2547 /* If types are forbidden here, issue a message. */
2548 if (parser->type_definition_forbidden_message)
2549 {
2550 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2551 in the message need to be interpreted. */
2552 error (parser->type_definition_forbidden_message);
2553 return false;
2554 }
2555 return true;
2556 }
2557
2558 /* This function is called when the DECLARATOR is processed. The TYPE
2559 was a type defined in the decl-specifiers. If it is invalid to
2560 define a type in the decl-specifiers for DECLARATOR, an error is
2561 issued. TYPE_LOCATION is the location of TYPE and is used
2562 for error reporting. */
2563
2564 static void
2565 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2566 tree type, location_t type_location)
2567 {
2568 /* [dcl.fct] forbids type definitions in return types.
2569 Unfortunately, it's not easy to know whether or not we are
2570 processing a return type until after the fact. */
2571 while (declarator
2572 && (declarator->kind == cdk_pointer
2573 || declarator->kind == cdk_reference
2574 || declarator->kind == cdk_ptrmem))
2575 declarator = declarator->declarator;
2576 if (declarator
2577 && declarator->kind == cdk_function)
2578 {
2579 error_at (type_location,
2580 "new types may not be defined in a return type");
2581 inform (type_location,
2582 "(perhaps a semicolon is missing after the definition of %qT)",
2583 type);
2584 }
2585 }
2586
2587 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2588 "<" in any valid C++ program. If the next token is indeed "<",
2589 issue a message warning the user about what appears to be an
2590 invalid attempt to form a template-id. LOCATION is the location
2591 of the type-specifier (TYPE) */
2592
2593 static void
2594 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2595 tree type, location_t location)
2596 {
2597 cp_token_position start = 0;
2598
2599 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2600 {
2601 if (TYPE_P (type))
2602 error_at (location, "%qT is not a template", type);
2603 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2604 error_at (location, "%qE is not a template", type);
2605 else
2606 error_at (location, "invalid template-id");
2607 /* Remember the location of the invalid "<". */
2608 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2609 start = cp_lexer_token_position (parser->lexer, true);
2610 /* Consume the "<". */
2611 cp_lexer_consume_token (parser->lexer);
2612 /* Parse the template arguments. */
2613 cp_parser_enclosed_template_argument_list (parser);
2614 /* Permanently remove the invalid template arguments so that
2615 this error message is not issued again. */
2616 if (start)
2617 cp_lexer_purge_tokens_after (parser->lexer, start);
2618 }
2619 }
2620
2621 /* If parsing an integral constant-expression, issue an error message
2622 about the fact that THING appeared and return true. Otherwise,
2623 return false. In either case, set
2624 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2625
2626 static bool
2627 cp_parser_non_integral_constant_expression (cp_parser *parser,
2628 non_integral_constant thing)
2629 {
2630 parser->non_integral_constant_expression_p = true;
2631 if (parser->integral_constant_expression_p)
2632 {
2633 if (!parser->allow_non_integral_constant_expression_p)
2634 {
2635 const char *msg = NULL;
2636 switch (thing)
2637 {
2638 case NIC_FLOAT:
2639 error ("floating-point literal "
2640 "cannot appear in a constant-expression");
2641 return true;
2642 case NIC_CAST:
2643 error ("a cast to a type other than an integral or "
2644 "enumeration type cannot appear in a "
2645 "constant-expression");
2646 return true;
2647 case NIC_TYPEID:
2648 error ("%<typeid%> operator "
2649 "cannot appear in a constant-expression");
2650 return true;
2651 case NIC_NCC:
2652 error ("non-constant compound literals "
2653 "cannot appear in a constant-expression");
2654 return true;
2655 case NIC_FUNC_CALL:
2656 error ("a function call "
2657 "cannot appear in a constant-expression");
2658 return true;
2659 case NIC_INC:
2660 error ("an increment "
2661 "cannot appear in a constant-expression");
2662 return true;
2663 case NIC_DEC:
2664 error ("an decrement "
2665 "cannot appear in a constant-expression");
2666 return true;
2667 case NIC_ARRAY_REF:
2668 error ("an array reference "
2669 "cannot appear in a constant-expression");
2670 return true;
2671 case NIC_ADDR_LABEL:
2672 error ("the address of a label "
2673 "cannot appear in a constant-expression");
2674 return true;
2675 case NIC_OVERLOADED:
2676 error ("calls to overloaded operators "
2677 "cannot appear in a constant-expression");
2678 return true;
2679 case NIC_ASSIGNMENT:
2680 error ("an assignment cannot appear in a constant-expression");
2681 return true;
2682 case NIC_COMMA:
2683 error ("a comma operator "
2684 "cannot appear in a constant-expression");
2685 return true;
2686 case NIC_CONSTRUCTOR:
2687 error ("a call to a constructor "
2688 "cannot appear in a constant-expression");
2689 return true;
2690 case NIC_TRANSACTION:
2691 error ("a transaction expression "
2692 "cannot appear in a constant-expression");
2693 return true;
2694 case NIC_THIS:
2695 msg = "this";
2696 break;
2697 case NIC_FUNC_NAME:
2698 msg = "__FUNCTION__";
2699 break;
2700 case NIC_PRETTY_FUNC:
2701 msg = "__PRETTY_FUNCTION__";
2702 break;
2703 case NIC_C99_FUNC:
2704 msg = "__func__";
2705 break;
2706 case NIC_VA_ARG:
2707 msg = "va_arg";
2708 break;
2709 case NIC_ARROW:
2710 msg = "->";
2711 break;
2712 case NIC_POINT:
2713 msg = ".";
2714 break;
2715 case NIC_STAR:
2716 msg = "*";
2717 break;
2718 case NIC_ADDR:
2719 msg = "&";
2720 break;
2721 case NIC_PREINCREMENT:
2722 msg = "++";
2723 break;
2724 case NIC_PREDECREMENT:
2725 msg = "--";
2726 break;
2727 case NIC_NEW:
2728 msg = "new";
2729 break;
2730 case NIC_DEL:
2731 msg = "delete";
2732 break;
2733 default:
2734 gcc_unreachable ();
2735 }
2736 if (msg)
2737 error ("%qs cannot appear in a constant-expression", msg);
2738 return true;
2739 }
2740 }
2741 return false;
2742 }
2743
2744 /* Emit a diagnostic for an invalid type name. SCOPE is the
2745 qualifying scope (or NULL, if none) for ID. This function commits
2746 to the current active tentative parse, if any. (Otherwise, the
2747 problematic construct might be encountered again later, resulting
2748 in duplicate error messages.) LOCATION is the location of ID. */
2749
2750 static void
2751 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2752 tree scope, tree id,
2753 location_t location)
2754 {
2755 tree decl, old_scope;
2756 cp_parser_commit_to_tentative_parse (parser);
2757 /* Try to lookup the identifier. */
2758 old_scope = parser->scope;
2759 parser->scope = scope;
2760 decl = cp_parser_lookup_name_simple (parser, id, location);
2761 parser->scope = old_scope;
2762 /* If the lookup found a template-name, it means that the user forgot
2763 to specify an argument list. Emit a useful error message. */
2764 if (TREE_CODE (decl) == TEMPLATE_DECL)
2765 error_at (location,
2766 "invalid use of template-name %qE without an argument list",
2767 decl);
2768 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2769 error_at (location, "invalid use of destructor %qD as a type", id);
2770 else if (TREE_CODE (decl) == TYPE_DECL)
2771 /* Something like 'unsigned A a;' */
2772 error_at (location, "invalid combination of multiple type-specifiers");
2773 else if (!parser->scope)
2774 {
2775 /* Issue an error message. */
2776 error_at (location, "%qE does not name a type", id);
2777 /* If we're in a template class, it's possible that the user was
2778 referring to a type from a base class. For example:
2779
2780 template <typename T> struct A { typedef T X; };
2781 template <typename T> struct B : public A<T> { X x; };
2782
2783 The user should have said "typename A<T>::X". */
2784 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2785 inform (location, "C++11 %<constexpr%> only available with "
2786 "-std=c++11 or -std=gnu++11");
2787 else if (processing_template_decl && current_class_type
2788 && TYPE_BINFO (current_class_type))
2789 {
2790 tree b;
2791
2792 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2793 b;
2794 b = TREE_CHAIN (b))
2795 {
2796 tree base_type = BINFO_TYPE (b);
2797 if (CLASS_TYPE_P (base_type)
2798 && dependent_type_p (base_type))
2799 {
2800 tree field;
2801 /* Go from a particular instantiation of the
2802 template (which will have an empty TYPE_FIELDs),
2803 to the main version. */
2804 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2805 for (field = TYPE_FIELDS (base_type);
2806 field;
2807 field = DECL_CHAIN (field))
2808 if (TREE_CODE (field) == TYPE_DECL
2809 && DECL_NAME (field) == id)
2810 {
2811 inform (location,
2812 "(perhaps %<typename %T::%E%> was intended)",
2813 BINFO_TYPE (b), id);
2814 break;
2815 }
2816 if (field)
2817 break;
2818 }
2819 }
2820 }
2821 }
2822 /* Here we diagnose qualified-ids where the scope is actually correct,
2823 but the identifier does not resolve to a valid type name. */
2824 else if (parser->scope != error_mark_node)
2825 {
2826 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2827 error_at (location, "%qE in namespace %qE does not name a type",
2828 id, parser->scope);
2829 else if (CLASS_TYPE_P (parser->scope)
2830 && constructor_name_p (id, parser->scope))
2831 {
2832 /* A<T>::A<T>() */
2833 error_at (location, "%<%T::%E%> names the constructor, not"
2834 " the type", parser->scope, id);
2835 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2836 error_at (location, "and %qT has no template constructors",
2837 parser->scope);
2838 }
2839 else if (TYPE_P (parser->scope)
2840 && dependent_scope_p (parser->scope))
2841 error_at (location, "need %<typename%> before %<%T::%E%> because "
2842 "%qT is a dependent scope",
2843 parser->scope, id, parser->scope);
2844 else if (TYPE_P (parser->scope))
2845 error_at (location, "%qE in %q#T does not name a type",
2846 id, parser->scope);
2847 else
2848 gcc_unreachable ();
2849 }
2850 }
2851
2852 /* Check for a common situation where a type-name should be present,
2853 but is not, and issue a sensible error message. Returns true if an
2854 invalid type-name was detected.
2855
2856 The situation handled by this function are variable declarations of the
2857 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2858 Usually, `ID' should name a type, but if we got here it means that it
2859 does not. We try to emit the best possible error message depending on
2860 how exactly the id-expression looks like. */
2861
2862 static bool
2863 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2864 {
2865 tree id;
2866 cp_token *token = cp_lexer_peek_token (parser->lexer);
2867
2868 /* Avoid duplicate error about ambiguous lookup. */
2869 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2870 {
2871 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2872 if (next->type == CPP_NAME && next->ambiguous_p)
2873 goto out;
2874 }
2875
2876 cp_parser_parse_tentatively (parser);
2877 id = cp_parser_id_expression (parser,
2878 /*template_keyword_p=*/false,
2879 /*check_dependency_p=*/true,
2880 /*template_p=*/NULL,
2881 /*declarator_p=*/true,
2882 /*optional_p=*/false);
2883 /* If the next token is a (, this is a function with no explicit return
2884 type, i.e. constructor, destructor or conversion op. */
2885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2886 || TREE_CODE (id) == TYPE_DECL)
2887 {
2888 cp_parser_abort_tentative_parse (parser);
2889 return false;
2890 }
2891 if (!cp_parser_parse_definitely (parser))
2892 return false;
2893
2894 /* Emit a diagnostic for the invalid type. */
2895 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2896 id, token->location);
2897 out:
2898 /* If we aren't in the middle of a declarator (i.e. in a
2899 parameter-declaration-clause), skip to the end of the declaration;
2900 there's no point in trying to process it. */
2901 if (!parser->in_declarator_p)
2902 cp_parser_skip_to_end_of_block_or_statement (parser);
2903 return true;
2904 }
2905
2906 /* Consume tokens up to, and including, the next non-nested closing `)'.
2907 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2908 are doing error recovery. Returns -1 if OR_COMMA is true and we
2909 found an unnested comma. */
2910
2911 static int
2912 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2913 bool recovering,
2914 bool or_comma,
2915 bool consume_paren)
2916 {
2917 unsigned paren_depth = 0;
2918 unsigned brace_depth = 0;
2919 unsigned square_depth = 0;
2920
2921 if (recovering && !or_comma
2922 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2923 return 0;
2924
2925 while (true)
2926 {
2927 cp_token * token = cp_lexer_peek_token (parser->lexer);
2928
2929 switch (token->type)
2930 {
2931 case CPP_EOF:
2932 case CPP_PRAGMA_EOL:
2933 /* If we've run out of tokens, then there is no closing `)'. */
2934 return 0;
2935
2936 /* This is good for lambda expression capture-lists. */
2937 case CPP_OPEN_SQUARE:
2938 ++square_depth;
2939 break;
2940 case CPP_CLOSE_SQUARE:
2941 if (!square_depth--)
2942 return 0;
2943 break;
2944
2945 case CPP_SEMICOLON:
2946 /* This matches the processing in skip_to_end_of_statement. */
2947 if (!brace_depth)
2948 return 0;
2949 break;
2950
2951 case CPP_OPEN_BRACE:
2952 ++brace_depth;
2953 break;
2954 case CPP_CLOSE_BRACE:
2955 if (!brace_depth--)
2956 return 0;
2957 break;
2958
2959 case CPP_COMMA:
2960 if (recovering && or_comma && !brace_depth && !paren_depth
2961 && !square_depth)
2962 return -1;
2963 break;
2964
2965 case CPP_OPEN_PAREN:
2966 if (!brace_depth)
2967 ++paren_depth;
2968 break;
2969
2970 case CPP_CLOSE_PAREN:
2971 if (!brace_depth && !paren_depth--)
2972 {
2973 if (consume_paren)
2974 cp_lexer_consume_token (parser->lexer);
2975 return 1;
2976 }
2977 break;
2978
2979 default:
2980 break;
2981 }
2982
2983 /* Consume the token. */
2984 cp_lexer_consume_token (parser->lexer);
2985 }
2986 }
2987
2988 /* Consume tokens until we reach the end of the current statement.
2989 Normally, that will be just before consuming a `;'. However, if a
2990 non-nested `}' comes first, then we stop before consuming that. */
2991
2992 static void
2993 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2994 {
2995 unsigned nesting_depth = 0;
2996
2997 while (true)
2998 {
2999 cp_token *token = cp_lexer_peek_token (parser->lexer);
3000
3001 switch (token->type)
3002 {
3003 case CPP_EOF:
3004 case CPP_PRAGMA_EOL:
3005 /* If we've run out of tokens, stop. */
3006 return;
3007
3008 case CPP_SEMICOLON:
3009 /* If the next token is a `;', we have reached the end of the
3010 statement. */
3011 if (!nesting_depth)
3012 return;
3013 break;
3014
3015 case CPP_CLOSE_BRACE:
3016 /* If this is a non-nested '}', stop before consuming it.
3017 That way, when confronted with something like:
3018
3019 { 3 + }
3020
3021 we stop before consuming the closing '}', even though we
3022 have not yet reached a `;'. */
3023 if (nesting_depth == 0)
3024 return;
3025
3026 /* If it is the closing '}' for a block that we have
3027 scanned, stop -- but only after consuming the token.
3028 That way given:
3029
3030 void f g () { ... }
3031 typedef int I;
3032
3033 we will stop after the body of the erroneously declared
3034 function, but before consuming the following `typedef'
3035 declaration. */
3036 if (--nesting_depth == 0)
3037 {
3038 cp_lexer_consume_token (parser->lexer);
3039 return;
3040 }
3041
3042 case CPP_OPEN_BRACE:
3043 ++nesting_depth;
3044 break;
3045
3046 default:
3047 break;
3048 }
3049
3050 /* Consume the token. */
3051 cp_lexer_consume_token (parser->lexer);
3052 }
3053 }
3054
3055 /* This function is called at the end of a statement or declaration.
3056 If the next token is a semicolon, it is consumed; otherwise, error
3057 recovery is attempted. */
3058
3059 static void
3060 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3061 {
3062 /* Look for the trailing `;'. */
3063 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3064 {
3065 /* If there is additional (erroneous) input, skip to the end of
3066 the statement. */
3067 cp_parser_skip_to_end_of_statement (parser);
3068 /* If the next token is now a `;', consume it. */
3069 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3070 cp_lexer_consume_token (parser->lexer);
3071 }
3072 }
3073
3074 /* Skip tokens until we have consumed an entire block, or until we
3075 have consumed a non-nested `;'. */
3076
3077 static void
3078 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3079 {
3080 int nesting_depth = 0;
3081
3082 while (nesting_depth >= 0)
3083 {
3084 cp_token *token = cp_lexer_peek_token (parser->lexer);
3085
3086 switch (token->type)
3087 {
3088 case CPP_EOF:
3089 case CPP_PRAGMA_EOL:
3090 /* If we've run out of tokens, stop. */
3091 return;
3092
3093 case CPP_SEMICOLON:
3094 /* Stop if this is an unnested ';'. */
3095 if (!nesting_depth)
3096 nesting_depth = -1;
3097 break;
3098
3099 case CPP_CLOSE_BRACE:
3100 /* Stop if this is an unnested '}', or closes the outermost
3101 nesting level. */
3102 nesting_depth--;
3103 if (nesting_depth < 0)
3104 return;
3105 if (!nesting_depth)
3106 nesting_depth = -1;
3107 break;
3108
3109 case CPP_OPEN_BRACE:
3110 /* Nest. */
3111 nesting_depth++;
3112 break;
3113
3114 default:
3115 break;
3116 }
3117
3118 /* Consume the token. */
3119 cp_lexer_consume_token (parser->lexer);
3120 }
3121 }
3122
3123 /* Skip tokens until a non-nested closing curly brace is the next
3124 token, or there are no more tokens. Return true in the first case,
3125 false otherwise. */
3126
3127 static bool
3128 cp_parser_skip_to_closing_brace (cp_parser *parser)
3129 {
3130 unsigned nesting_depth = 0;
3131
3132 while (true)
3133 {
3134 cp_token *token = cp_lexer_peek_token (parser->lexer);
3135
3136 switch (token->type)
3137 {
3138 case CPP_EOF:
3139 case CPP_PRAGMA_EOL:
3140 /* If we've run out of tokens, stop. */
3141 return false;
3142
3143 case CPP_CLOSE_BRACE:
3144 /* If the next token is a non-nested `}', then we have reached
3145 the end of the current block. */
3146 if (nesting_depth-- == 0)
3147 return true;
3148 break;
3149
3150 case CPP_OPEN_BRACE:
3151 /* If it the next token is a `{', then we are entering a new
3152 block. Consume the entire block. */
3153 ++nesting_depth;
3154 break;
3155
3156 default:
3157 break;
3158 }
3159
3160 /* Consume the token. */
3161 cp_lexer_consume_token (parser->lexer);
3162 }
3163 }
3164
3165 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3166 parameter is the PRAGMA token, allowing us to purge the entire pragma
3167 sequence. */
3168
3169 static void
3170 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3171 {
3172 cp_token *token;
3173
3174 parser->lexer->in_pragma = false;
3175
3176 do
3177 token = cp_lexer_consume_token (parser->lexer);
3178 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3179
3180 /* Ensure that the pragma is not parsed again. */
3181 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3182 }
3183
3184 /* Require pragma end of line, resyncing with it as necessary. The
3185 arguments are as for cp_parser_skip_to_pragma_eol. */
3186
3187 static void
3188 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3189 {
3190 parser->lexer->in_pragma = false;
3191 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3192 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3193 }
3194
3195 /* This is a simple wrapper around make_typename_type. When the id is
3196 an unresolved identifier node, we can provide a superior diagnostic
3197 using cp_parser_diagnose_invalid_type_name. */
3198
3199 static tree
3200 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3201 tree id, location_t id_location)
3202 {
3203 tree result;
3204 if (TREE_CODE (id) == IDENTIFIER_NODE)
3205 {
3206 result = make_typename_type (scope, id, typename_type,
3207 /*complain=*/tf_none);
3208 if (result == error_mark_node)
3209 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3210 return result;
3211 }
3212 return make_typename_type (scope, id, typename_type, tf_error);
3213 }
3214
3215 /* This is a wrapper around the
3216 make_{pointer,ptrmem,reference}_declarator functions that decides
3217 which one to call based on the CODE and CLASS_TYPE arguments. The
3218 CODE argument should be one of the values returned by
3219 cp_parser_ptr_operator. */
3220 static cp_declarator *
3221 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3222 cp_cv_quals cv_qualifiers,
3223 cp_declarator *target)
3224 {
3225 if (code == ERROR_MARK)
3226 return cp_error_declarator;
3227
3228 if (code == INDIRECT_REF)
3229 if (class_type == NULL_TREE)
3230 return make_pointer_declarator (cv_qualifiers, target);
3231 else
3232 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3233 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3234 return make_reference_declarator (cv_qualifiers, target, false);
3235 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3236 return make_reference_declarator (cv_qualifiers, target, true);
3237 gcc_unreachable ();
3238 }
3239
3240 /* Create a new C++ parser. */
3241
3242 static cp_parser *
3243 cp_parser_new (void)
3244 {
3245 cp_parser *parser;
3246 cp_lexer *lexer;
3247 unsigned i;
3248
3249 /* cp_lexer_new_main is called before doing GC allocation because
3250 cp_lexer_new_main might load a PCH file. */
3251 lexer = cp_lexer_new_main ();
3252
3253 /* Initialize the binops_by_token so that we can get the tree
3254 directly from the token. */
3255 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3256 binops_by_token[binops[i].token_type] = binops[i];
3257
3258 parser = ggc_alloc_cleared_cp_parser ();
3259 parser->lexer = lexer;
3260 parser->context = cp_parser_context_new (NULL);
3261
3262 /* For now, we always accept GNU extensions. */
3263 parser->allow_gnu_extensions_p = 1;
3264
3265 /* The `>' token is a greater-than operator, not the end of a
3266 template-id. */
3267 parser->greater_than_is_operator_p = true;
3268
3269 parser->default_arg_ok_p = true;
3270
3271 /* We are not parsing a constant-expression. */
3272 parser->integral_constant_expression_p = false;
3273 parser->allow_non_integral_constant_expression_p = false;
3274 parser->non_integral_constant_expression_p = false;
3275
3276 /* Local variable names are not forbidden. */
3277 parser->local_variables_forbidden_p = false;
3278
3279 /* We are not processing an `extern "C"' declaration. */
3280 parser->in_unbraced_linkage_specification_p = false;
3281
3282 /* We are not processing a declarator. */
3283 parser->in_declarator_p = false;
3284
3285 /* We are not processing a template-argument-list. */
3286 parser->in_template_argument_list_p = false;
3287
3288 /* We are not in an iteration statement. */
3289 parser->in_statement = 0;
3290
3291 /* We are not in a switch statement. */
3292 parser->in_switch_statement_p = false;
3293
3294 /* We are not parsing a type-id inside an expression. */
3295 parser->in_type_id_in_expr_p = false;
3296
3297 /* Declarations aren't implicitly extern "C". */
3298 parser->implicit_extern_c = false;
3299
3300 /* String literals should be translated to the execution character set. */
3301 parser->translate_strings_p = true;
3302
3303 /* We are not parsing a function body. */
3304 parser->in_function_body = false;
3305
3306 /* We can correct until told otherwise. */
3307 parser->colon_corrects_to_scope_p = true;
3308
3309 /* The unparsed function queue is empty. */
3310 push_unparsed_function_queues (parser);
3311
3312 /* There are no classes being defined. */
3313 parser->num_classes_being_defined = 0;
3314
3315 /* No template parameters apply. */
3316 parser->num_template_parameter_lists = 0;
3317
3318 return parser;
3319 }
3320
3321 /* Create a cp_lexer structure which will emit the tokens in CACHE
3322 and push it onto the parser's lexer stack. This is used for delayed
3323 parsing of in-class method bodies and default arguments, and should
3324 not be confused with tentative parsing. */
3325 static void
3326 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3327 {
3328 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3329 lexer->next = parser->lexer;
3330 parser->lexer = lexer;
3331
3332 /* Move the current source position to that of the first token in the
3333 new lexer. */
3334 cp_lexer_set_source_position_from_token (lexer->next_token);
3335 }
3336
3337 /* Pop the top lexer off the parser stack. This is never used for the
3338 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3339 static void
3340 cp_parser_pop_lexer (cp_parser *parser)
3341 {
3342 cp_lexer *lexer = parser->lexer;
3343 parser->lexer = lexer->next;
3344 cp_lexer_destroy (lexer);
3345
3346 /* Put the current source position back where it was before this
3347 lexer was pushed. */
3348 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3349 }
3350
3351 /* Lexical conventions [gram.lex] */
3352
3353 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3354 identifier. */
3355
3356 static tree
3357 cp_parser_identifier (cp_parser* parser)
3358 {
3359 cp_token *token;
3360
3361 /* Look for the identifier. */
3362 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3363 /* Return the value. */
3364 return token ? token->u.value : error_mark_node;
3365 }
3366
3367 /* Parse a sequence of adjacent string constants. Returns a
3368 TREE_STRING representing the combined, nul-terminated string
3369 constant. If TRANSLATE is true, translate the string to the
3370 execution character set. If WIDE_OK is true, a wide string is
3371 invalid here.
3372
3373 C++98 [lex.string] says that if a narrow string literal token is
3374 adjacent to a wide string literal token, the behavior is undefined.
3375 However, C99 6.4.5p4 says that this results in a wide string literal.
3376 We follow C99 here, for consistency with the C front end.
3377
3378 This code is largely lifted from lex_string() in c-lex.c.
3379
3380 FUTURE: ObjC++ will need to handle @-strings here. */
3381 static tree
3382 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3383 {
3384 tree value;
3385 size_t count;
3386 struct obstack str_ob;
3387 cpp_string str, istr, *strs;
3388 cp_token *tok;
3389 enum cpp_ttype type, curr_type;
3390 int have_suffix_p = 0;
3391 tree string_tree;
3392 tree suffix_id = NULL_TREE;
3393 bool curr_tok_is_userdef_p = false;
3394
3395 tok = cp_lexer_peek_token (parser->lexer);
3396 if (!cp_parser_is_string_literal (tok))
3397 {
3398 cp_parser_error (parser, "expected string-literal");
3399 return error_mark_node;
3400 }
3401
3402 if (cpp_userdef_string_p (tok->type))
3403 {
3404 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3405 curr_type = cpp_userdef_string_remove_type (tok->type);
3406 curr_tok_is_userdef_p = true;
3407 }
3408 else
3409 {
3410 string_tree = tok->u.value;
3411 curr_type = tok->type;
3412 }
3413 type = curr_type;
3414
3415 /* Try to avoid the overhead of creating and destroying an obstack
3416 for the common case of just one string. */
3417 if (!cp_parser_is_string_literal
3418 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3419 {
3420 cp_lexer_consume_token (parser->lexer);
3421
3422 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3423 str.len = TREE_STRING_LENGTH (string_tree);
3424 count = 1;
3425
3426 if (curr_tok_is_userdef_p)
3427 {
3428 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3429 have_suffix_p = 1;
3430 curr_type = cpp_userdef_string_remove_type (tok->type);
3431 }
3432 else
3433 curr_type = tok->type;
3434
3435 strs = &str;
3436 }
3437 else
3438 {
3439 gcc_obstack_init (&str_ob);
3440 count = 0;
3441
3442 do
3443 {
3444 cp_lexer_consume_token (parser->lexer);
3445 count++;
3446 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3447 str.len = TREE_STRING_LENGTH (string_tree);
3448
3449 if (curr_tok_is_userdef_p)
3450 {
3451 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3452 if (have_suffix_p == 0)
3453 {
3454 suffix_id = curr_suffix_id;
3455 have_suffix_p = 1;
3456 }
3457 else if (have_suffix_p == 1
3458 && curr_suffix_id != suffix_id)
3459 {
3460 error ("inconsistent user-defined literal suffixes"
3461 " %qD and %qD in string literal",
3462 suffix_id, curr_suffix_id);
3463 have_suffix_p = -1;
3464 }
3465 curr_type = cpp_userdef_string_remove_type (tok->type);
3466 }
3467 else
3468 curr_type = tok->type;
3469
3470 if (type != curr_type)
3471 {
3472 if (type == CPP_STRING)
3473 type = curr_type;
3474 else if (curr_type != CPP_STRING)
3475 error_at (tok->location,
3476 "unsupported non-standard concatenation "
3477 "of string literals");
3478 }
3479
3480 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3481
3482 tok = cp_lexer_peek_token (parser->lexer);
3483 if (cpp_userdef_string_p (tok->type))
3484 {
3485 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3486 curr_type = cpp_userdef_string_remove_type (tok->type);
3487 curr_tok_is_userdef_p = true;
3488 }
3489 else
3490 {
3491 string_tree = tok->u.value;
3492 curr_type = tok->type;
3493 curr_tok_is_userdef_p = false;
3494 }
3495 }
3496 while (cp_parser_is_string_literal (tok));
3497
3498 strs = (cpp_string *) obstack_finish (&str_ob);
3499 }
3500
3501 if (type != CPP_STRING && !wide_ok)
3502 {
3503 cp_parser_error (parser, "a wide string is invalid in this context");
3504 type = CPP_STRING;
3505 }
3506
3507 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3508 (parse_in, strs, count, &istr, type))
3509 {
3510 value = build_string (istr.len, (const char *)istr.text);
3511 free (CONST_CAST (unsigned char *, istr.text));
3512
3513 switch (type)
3514 {
3515 default:
3516 case CPP_STRING:
3517 case CPP_UTF8STRING:
3518 TREE_TYPE (value) = char_array_type_node;
3519 break;
3520 case CPP_STRING16:
3521 TREE_TYPE (value) = char16_array_type_node;
3522 break;
3523 case CPP_STRING32:
3524 TREE_TYPE (value) = char32_array_type_node;
3525 break;
3526 case CPP_WSTRING:
3527 TREE_TYPE (value) = wchar_array_type_node;
3528 break;
3529 }
3530
3531 value = fix_string_type (value);
3532
3533 if (have_suffix_p)
3534 {
3535 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3536 tok->u.value = literal;
3537 return cp_parser_userdef_string_literal (tok);
3538 }
3539 }
3540 else
3541 /* cpp_interpret_string has issued an error. */
3542 value = error_mark_node;
3543
3544 if (count > 1)
3545 obstack_free (&str_ob, 0);
3546
3547 return value;
3548 }
3549
3550 /* Parse a user-defined char constant. Returns a call to a user-defined
3551 literal operator taking the character as an argument. */
3552
3553 static tree
3554 cp_parser_userdef_char_literal (cp_parser *parser)
3555 {
3556 cp_token *token = NULL;
3557 tree literal, suffix_id, value;
3558 tree name, decl;
3559 tree result;
3560 VEC(tree,gc) *vec;
3561
3562 token = cp_lexer_consume_token (parser->lexer);
3563 literal = token->u.value;
3564 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3565 value = USERDEF_LITERAL_VALUE (literal);
3566 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3567
3568 /* Build up a call to the user-defined operator */
3569 /* Lookup the name we got back from the id-expression. */
3570 vec = make_tree_vector ();
3571 VEC_safe_push (tree, gc, vec, value);
3572 decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3573 if (!decl || decl == error_mark_node)
3574 {
3575 error ("unable to find user-defined character literal operator %qD",
3576 name);
3577 release_tree_vector (vec);
3578 return error_mark_node;
3579 }
3580 result = finish_call_expr (decl, &vec, false, true, tf_warning_or_error);
3581 release_tree_vector (vec);
3582
3583 return result;
3584 }
3585
3586 /* A subroutine of cp_parser_userdef_numeric_literal to
3587 create a char... template parameter pack from a string node. */
3588
3589 static tree
3590 make_char_string_pack (tree value)
3591 {
3592 tree charvec;
3593 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3594 const char *str = TREE_STRING_POINTER (value);
3595 int i, len = TREE_STRING_LENGTH (value) - 1;
3596 tree argvec = make_tree_vec (1);
3597
3598 /* Fill in CHARVEC with all of the parameters. */
3599 charvec = make_tree_vec (len);
3600 for (i = 0; i < len; ++i)
3601 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3602
3603 /* Build the argument packs. */
3604 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3605 TREE_TYPE (argpack) = char_type_node;
3606
3607 TREE_VEC_ELT (argvec, 0) = argpack;
3608
3609 return argvec;
3610 }
3611
3612 /* Parse a user-defined numeric constant. returns a call to a user-defined
3613 literal operator. */
3614
3615 static tree
3616 cp_parser_userdef_numeric_literal (cp_parser *parser)
3617 {
3618 cp_token *token = NULL;
3619 tree literal, suffix_id, value, num_string;
3620 tree name, decl;
3621 tree result = error_mark_node;
3622 VEC(tree,gc) *args;
3623
3624 token = cp_lexer_consume_token (parser->lexer);
3625 literal = token->u.value;
3626 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3627 value = USERDEF_LITERAL_VALUE (literal);
3628 num_string = USERDEF_LITERAL_NUM_STRING (literal);
3629 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3630
3631 /* Build up a call to the user-defined operator */
3632 /* Lookup the name we got back from the id-expression. */
3633 /* Try to find the literal operator by finishing the call expression
3634 with the numeric argument. */
3635 args = make_tree_vector ();
3636 VEC_safe_push (tree, gc, args, value);
3637 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3638 if (decl && decl != error_mark_node)
3639 {
3640 result = finish_call_expr (decl, &args, false, true, tf_none);
3641 if (result != error_mark_node)
3642 {
3643 release_tree_vector (args);
3644 return result;
3645 }
3646 }
3647 release_tree_vector (args);
3648
3649 /* If the numeric argument didn't work, look for a raw literal
3650 operator taking a const char* argument consisting of the number
3651 in string format. */
3652 args = make_tree_vector ();
3653 VEC_safe_push (tree, gc, args, num_string);
3654 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3655 if (decl && decl != error_mark_node)
3656 {
3657 result = finish_call_expr (decl, &args, false, true, tf_none);
3658 if (result != error_mark_node)
3659 {
3660 release_tree_vector (args);
3661 return result;
3662 }
3663 }
3664 release_tree_vector (args);
3665
3666 /* If the raw literal didn't work, look for a non-type template
3667 function with parameter pack char.... Call the function with
3668 template parameter characters representing the number. */
3669 args = make_tree_vector ();
3670 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3671 if (decl && decl != error_mark_node)
3672 {
3673 tree tmpl_args = make_char_string_pack (num_string);
3674 decl = lookup_template_function (decl, tmpl_args);
3675 result = finish_call_expr (decl, &args, false, true, tf_none);
3676 if (result != error_mark_node)
3677 {
3678 release_tree_vector (args);
3679 return result;
3680 }
3681 }
3682 release_tree_vector (args);
3683
3684 if (result == error_mark_node)
3685 error ("unable to find user-defined numeric literal operator %qD", name);
3686
3687 return result;
3688 }
3689
3690 /* Parse a user-defined string constant. Returns a call to a user-defined
3691 literal operator taking a character pointer and the length of the string
3692 as arguments. */
3693
3694 static tree
3695 cp_parser_userdef_string_literal (cp_token *token)
3696 {
3697 tree literal, suffix_id, value;
3698 tree name, decl;
3699 tree result;
3700 VEC(tree,gc) *vec;
3701 int len;
3702
3703 literal = token->u.value;
3704 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3705 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3706 value = USERDEF_LITERAL_VALUE (literal);
3707 len = TREE_STRING_LENGTH (value)
3708 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3709 /* Build up a call to the user-defined operator */
3710 /* Lookup the name we got back from the id-expression. */
3711 vec = make_tree_vector ();
3712 VEC_safe_push (tree, gc, vec, value);
3713 VEC_safe_push (tree, gc, vec, build_int_cst (size_type_node, len));
3714 decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3715 if (!decl || decl == error_mark_node)
3716 {
3717 error ("unable to find user-defined string literal operator %qD", name);
3718 release_tree_vector (vec);
3719 return error_mark_node;
3720 }
3721 result = finish_call_expr (decl, &vec, false, true, tf_none);
3722 if (result == error_mark_node)
3723 error ("unable to find valid user-defined string literal operator %qD."
3724 " Possible missing length argument in string literal operator.",
3725 name);
3726 release_tree_vector (vec);
3727
3728 return result;
3729 }
3730
3731
3732 /* Basic concepts [gram.basic] */
3733
3734 /* Parse a translation-unit.
3735
3736 translation-unit:
3737 declaration-seq [opt]
3738
3739 Returns TRUE if all went well. */
3740
3741 static bool
3742 cp_parser_translation_unit (cp_parser* parser)
3743 {
3744 /* The address of the first non-permanent object on the declarator
3745 obstack. */
3746 static void *declarator_obstack_base;
3747
3748 bool success;
3749
3750 /* Create the declarator obstack, if necessary. */
3751 if (!cp_error_declarator)
3752 {
3753 gcc_obstack_init (&declarator_obstack);
3754 /* Create the error declarator. */
3755 cp_error_declarator = make_declarator (cdk_error);
3756 /* Create the empty parameter list. */
3757 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3758 /* Remember where the base of the declarator obstack lies. */
3759 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3760 }
3761
3762 cp_parser_declaration_seq_opt (parser);
3763
3764 /* If there are no tokens left then all went well. */
3765 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3766 {
3767 /* Get rid of the token array; we don't need it any more. */
3768 cp_lexer_destroy (parser->lexer);
3769 parser->lexer = NULL;
3770
3771 /* This file might have been a context that's implicitly extern
3772 "C". If so, pop the lang context. (Only relevant for PCH.) */
3773 if (parser->implicit_extern_c)
3774 {
3775 pop_lang_context ();
3776 parser->implicit_extern_c = false;
3777 }
3778
3779 /* Finish up. */
3780 finish_translation_unit ();
3781
3782 success = true;
3783 }
3784 else
3785 {
3786 cp_parser_error (parser, "expected declaration");
3787 success = false;
3788 }
3789
3790 /* Make sure the declarator obstack was fully cleaned up. */
3791 gcc_assert (obstack_next_free (&declarator_obstack)
3792 == declarator_obstack_base);
3793
3794 /* All went well. */
3795 return success;
3796 }
3797
3798 /* Expressions [gram.expr] */
3799
3800 /* Parse a primary-expression.
3801
3802 primary-expression:
3803 literal
3804 this
3805 ( expression )
3806 id-expression
3807
3808 GNU Extensions:
3809
3810 primary-expression:
3811 ( compound-statement )
3812 __builtin_va_arg ( assignment-expression , type-id )
3813 __builtin_offsetof ( type-id , offsetof-expression )
3814
3815 C++ Extensions:
3816 __has_nothrow_assign ( type-id )
3817 __has_nothrow_constructor ( type-id )
3818 __has_nothrow_copy ( type-id )
3819 __has_trivial_assign ( type-id )
3820 __has_trivial_constructor ( type-id )
3821 __has_trivial_copy ( type-id )
3822 __has_trivial_destructor ( type-id )
3823 __has_virtual_destructor ( type-id )
3824 __is_abstract ( type-id )
3825 __is_base_of ( type-id , type-id )
3826 __is_class ( type-id )
3827 __is_convertible_to ( type-id , type-id )
3828 __is_empty ( type-id )
3829 __is_enum ( type-id )
3830 __is_literal_type ( type-id )
3831 __is_pod ( type-id )
3832 __is_polymorphic ( type-id )
3833 __is_std_layout ( type-id )
3834 __is_trivial ( type-id )
3835 __is_union ( type-id )
3836
3837 Objective-C++ Extension:
3838
3839 primary-expression:
3840 objc-expression
3841
3842 literal:
3843 __null
3844
3845 ADDRESS_P is true iff this expression was immediately preceded by
3846 "&" and therefore might denote a pointer-to-member. CAST_P is true
3847 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3848 true iff this expression is a template argument.
3849
3850 Returns a representation of the expression. Upon return, *IDK
3851 indicates what kind of id-expression (if any) was present. */
3852
3853 static tree
3854 cp_parser_primary_expression (cp_parser *parser,
3855 bool address_p,
3856 bool cast_p,
3857 bool template_arg_p,
3858 cp_id_kind *idk)
3859 {
3860 cp_token *token = NULL;
3861
3862 /* Assume the primary expression is not an id-expression. */
3863 *idk = CP_ID_KIND_NONE;
3864
3865 /* Peek at the next token. */
3866 token = cp_lexer_peek_token (parser->lexer);
3867 switch (token->type)
3868 {
3869 /* literal:
3870 integer-literal
3871 character-literal
3872 floating-literal
3873 string-literal
3874 boolean-literal
3875 pointer-literal
3876 user-defined-literal */
3877 case CPP_CHAR:
3878 case CPP_CHAR16:
3879 case CPP_CHAR32:
3880 case CPP_WCHAR:
3881 case CPP_NUMBER:
3882 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3883 return cp_parser_userdef_numeric_literal (parser);
3884 token = cp_lexer_consume_token (parser->lexer);
3885 if (TREE_CODE (token->u.value) == FIXED_CST)
3886 {
3887 error_at (token->location,
3888 "fixed-point types not supported in C++");
3889 return error_mark_node;
3890 }
3891 /* Floating-point literals are only allowed in an integral
3892 constant expression if they are cast to an integral or
3893 enumeration type. */
3894 if (TREE_CODE (token->u.value) == REAL_CST
3895 && parser->integral_constant_expression_p
3896 && pedantic)
3897 {
3898 /* CAST_P will be set even in invalid code like "int(2.7 +
3899 ...)". Therefore, we have to check that the next token
3900 is sure to end the cast. */
3901 if (cast_p)
3902 {
3903 cp_token *next_token;
3904
3905 next_token = cp_lexer_peek_token (parser->lexer);
3906 if (/* The comma at the end of an
3907 enumerator-definition. */
3908 next_token->type != CPP_COMMA
3909 /* The curly brace at the end of an enum-specifier. */
3910 && next_token->type != CPP_CLOSE_BRACE
3911 /* The end of a statement. */
3912 && next_token->type != CPP_SEMICOLON
3913 /* The end of the cast-expression. */
3914 && next_token->type != CPP_CLOSE_PAREN
3915 /* The end of an array bound. */
3916 && next_token->type != CPP_CLOSE_SQUARE
3917 /* The closing ">" in a template-argument-list. */
3918 && (next_token->type != CPP_GREATER
3919 || parser->greater_than_is_operator_p)
3920 /* C++0x only: A ">>" treated like two ">" tokens,
3921 in a template-argument-list. */
3922 && (next_token->type != CPP_RSHIFT
3923 || (cxx_dialect == cxx98)
3924 || parser->greater_than_is_operator_p))
3925 cast_p = false;
3926 }
3927
3928 /* If we are within a cast, then the constraint that the
3929 cast is to an integral or enumeration type will be
3930 checked at that point. If we are not within a cast, then
3931 this code is invalid. */
3932 if (!cast_p)
3933 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3934 }
3935 return token->u.value;
3936
3937 case CPP_CHAR_USERDEF:
3938 case CPP_CHAR16_USERDEF:
3939 case CPP_CHAR32_USERDEF:
3940 case CPP_WCHAR_USERDEF:
3941 return cp_parser_userdef_char_literal (parser);
3942
3943 case CPP_STRING:
3944 case CPP_STRING16:
3945 case CPP_STRING32:
3946 case CPP_WSTRING:
3947 case CPP_UTF8STRING:
3948 case CPP_STRING_USERDEF:
3949 case CPP_STRING16_USERDEF:
3950 case CPP_STRING32_USERDEF:
3951 case CPP_WSTRING_USERDEF:
3952 case CPP_UTF8STRING_USERDEF:
3953 /* ??? Should wide strings be allowed when parser->translate_strings_p
3954 is false (i.e. in attributes)? If not, we can kill the third
3955 argument to cp_parser_string_literal. */
3956 return cp_parser_string_literal (parser,
3957 parser->translate_strings_p,
3958 true);
3959
3960 case CPP_OPEN_PAREN:
3961 {
3962 tree expr;
3963 bool saved_greater_than_is_operator_p;
3964
3965 /* Consume the `('. */
3966 cp_lexer_consume_token (parser->lexer);
3967 /* Within a parenthesized expression, a `>' token is always
3968 the greater-than operator. */
3969 saved_greater_than_is_operator_p
3970 = parser->greater_than_is_operator_p;
3971 parser->greater_than_is_operator_p = true;
3972 /* If we see `( { ' then we are looking at the beginning of
3973 a GNU statement-expression. */
3974 if (cp_parser_allow_gnu_extensions_p (parser)
3975 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3976 {
3977 /* Statement-expressions are not allowed by the standard. */
3978 pedwarn (token->location, OPT_pedantic,
3979 "ISO C++ forbids braced-groups within expressions");
3980
3981 /* And they're not allowed outside of a function-body; you
3982 cannot, for example, write:
3983
3984 int i = ({ int j = 3; j + 1; });
3985
3986 at class or namespace scope. */
3987 if (!parser->in_function_body
3988 || parser->in_template_argument_list_p)
3989 {
3990 error_at (token->location,
3991 "statement-expressions are not allowed outside "
3992 "functions nor in template-argument lists");
3993 cp_parser_skip_to_end_of_block_or_statement (parser);
3994 expr = error_mark_node;
3995 }
3996 else
3997 {
3998 /* Start the statement-expression. */
3999 expr = begin_stmt_expr ();
4000 /* Parse the compound-statement. */
4001 cp_parser_compound_statement (parser, expr, false, false);
4002 /* Finish up. */
4003 expr = finish_stmt_expr (expr, false);
4004 }
4005 }
4006 else
4007 {
4008 /* Parse the parenthesized expression. */
4009 expr = cp_parser_expression (parser, cast_p, idk);
4010 /* Let the front end know that this expression was
4011 enclosed in parentheses. This matters in case, for
4012 example, the expression is of the form `A::B', since
4013 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4014 not. */
4015 finish_parenthesized_expr (expr);
4016 /* DR 705: Wrapping an unqualified name in parentheses
4017 suppresses arg-dependent lookup. We want to pass back
4018 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4019 (c++/37862), but none of the others. */
4020 if (*idk != CP_ID_KIND_QUALIFIED)
4021 *idk = CP_ID_KIND_NONE;
4022 }
4023 /* The `>' token might be the end of a template-id or
4024 template-parameter-list now. */
4025 parser->greater_than_is_operator_p
4026 = saved_greater_than_is_operator_p;
4027 /* Consume the `)'. */
4028 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4029 cp_parser_skip_to_end_of_statement (parser);
4030
4031 return expr;
4032 }
4033
4034 case CPP_OPEN_SQUARE:
4035 if (c_dialect_objc ())
4036 /* We have an Objective-C++ message. */
4037 return cp_parser_objc_expression (parser);
4038 {
4039 tree lam = cp_parser_lambda_expression (parser);
4040 /* Don't warn about a failed tentative parse. */
4041 if (cp_parser_error_occurred (parser))
4042 return error_mark_node;
4043 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4044 return lam;
4045 }
4046
4047 case CPP_OBJC_STRING:
4048 if (c_dialect_objc ())
4049 /* We have an Objective-C++ string literal. */
4050 return cp_parser_objc_expression (parser);
4051 cp_parser_error (parser, "expected primary-expression");
4052 return error_mark_node;
4053
4054 case CPP_KEYWORD:
4055 switch (token->keyword)
4056 {
4057 /* These two are the boolean literals. */
4058 case RID_TRUE:
4059 cp_lexer_consume_token (parser->lexer);
4060 return boolean_true_node;
4061 case RID_FALSE:
4062 cp_lexer_consume_token (parser->lexer);
4063 return boolean_false_node;
4064
4065 /* The `__null' literal. */
4066 case RID_NULL:
4067 cp_lexer_consume_token (parser->lexer);
4068 return null_node;
4069
4070 /* The `nullptr' literal. */
4071 case RID_NULLPTR:
4072 cp_lexer_consume_token (parser->lexer);
4073 return nullptr_node;
4074
4075 /* Recognize the `this' keyword. */
4076 case RID_THIS:
4077 cp_lexer_consume_token (parser->lexer);
4078 if (parser->local_variables_forbidden_p)
4079 {
4080 error_at (token->location,
4081 "%<this%> may not be used in this context");
4082 return error_mark_node;
4083 }
4084 /* Pointers cannot appear in constant-expressions. */
4085 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4086 return error_mark_node;
4087 return finish_this_expr ();
4088
4089 /* The `operator' keyword can be the beginning of an
4090 id-expression. */
4091 case RID_OPERATOR:
4092 goto id_expression;
4093
4094 case RID_FUNCTION_NAME:
4095 case RID_PRETTY_FUNCTION_NAME:
4096 case RID_C99_FUNCTION_NAME:
4097 {
4098 non_integral_constant name;
4099
4100 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4101 __func__ are the names of variables -- but they are
4102 treated specially. Therefore, they are handled here,
4103 rather than relying on the generic id-expression logic
4104 below. Grammatically, these names are id-expressions.
4105
4106 Consume the token. */
4107 token = cp_lexer_consume_token (parser->lexer);
4108
4109 switch (token->keyword)
4110 {
4111 case RID_FUNCTION_NAME:
4112 name = NIC_FUNC_NAME;
4113 break;
4114 case RID_PRETTY_FUNCTION_NAME:
4115 name = NIC_PRETTY_FUNC;
4116 break;
4117 case RID_C99_FUNCTION_NAME:
4118 name = NIC_C99_FUNC;
4119 break;
4120 default:
4121 gcc_unreachable ();
4122 }
4123
4124 if (cp_parser_non_integral_constant_expression (parser, name))
4125 return error_mark_node;
4126
4127 /* Look up the name. */
4128 return finish_fname (token->u.value);
4129 }
4130
4131 case RID_VA_ARG:
4132 {
4133 tree expression;
4134 tree type;
4135
4136 /* The `__builtin_va_arg' construct is used to handle
4137 `va_arg'. Consume the `__builtin_va_arg' token. */
4138 cp_lexer_consume_token (parser->lexer);
4139 /* Look for the opening `('. */
4140 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4141 /* Now, parse the assignment-expression. */
4142 expression = cp_parser_assignment_expression (parser,
4143 /*cast_p=*/false, NULL);
4144 /* Look for the `,'. */
4145 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4146 /* Parse the type-id. */
4147 type = cp_parser_type_id (parser);
4148 /* Look for the closing `)'. */
4149 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4150 /* Using `va_arg' in a constant-expression is not
4151 allowed. */
4152 if (cp_parser_non_integral_constant_expression (parser,
4153 NIC_VA_ARG))
4154 return error_mark_node;
4155 return build_x_va_arg (expression, type);
4156 }
4157
4158 case RID_OFFSETOF:
4159 return cp_parser_builtin_offsetof (parser);
4160
4161 case RID_HAS_NOTHROW_ASSIGN:
4162 case RID_HAS_NOTHROW_CONSTRUCTOR:
4163 case RID_HAS_NOTHROW_COPY:
4164 case RID_HAS_TRIVIAL_ASSIGN:
4165 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4166 case RID_HAS_TRIVIAL_COPY:
4167 case RID_HAS_TRIVIAL_DESTRUCTOR:
4168 case RID_HAS_VIRTUAL_DESTRUCTOR:
4169 case RID_IS_ABSTRACT:
4170 case RID_IS_BASE_OF:
4171 case RID_IS_CLASS:
4172 case RID_IS_CONVERTIBLE_TO:
4173 case RID_IS_EMPTY:
4174 case RID_IS_ENUM:
4175 case RID_IS_LITERAL_TYPE:
4176 case RID_IS_POD:
4177 case RID_IS_POLYMORPHIC:
4178 case RID_IS_STD_LAYOUT:
4179 case RID_IS_TRIVIAL:
4180 case RID_IS_UNION:
4181 return cp_parser_trait_expr (parser, token->keyword);
4182
4183 /* Objective-C++ expressions. */
4184 case RID_AT_ENCODE:
4185 case RID_AT_PROTOCOL:
4186 case RID_AT_SELECTOR:
4187 return cp_parser_objc_expression (parser);
4188
4189 case RID_TEMPLATE:
4190 if (parser->in_function_body
4191 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4192 == CPP_LESS))
4193 {
4194 error_at (token->location,
4195 "a template declaration cannot appear at block scope");
4196 cp_parser_skip_to_end_of_block_or_statement (parser);
4197 return error_mark_node;
4198 }
4199 default:
4200 cp_parser_error (parser, "expected primary-expression");
4201 return error_mark_node;
4202 }
4203
4204 /* An id-expression can start with either an identifier, a
4205 `::' as the beginning of a qualified-id, or the "operator"
4206 keyword. */
4207 case CPP_NAME:
4208 case CPP_SCOPE:
4209 case CPP_TEMPLATE_ID:
4210 case CPP_NESTED_NAME_SPECIFIER:
4211 {
4212 tree id_expression;
4213 tree decl;
4214 const char *error_msg;
4215 bool template_p;
4216 bool done;
4217 cp_token *id_expr_token;
4218
4219 id_expression:
4220 /* Parse the id-expression. */
4221 id_expression
4222 = cp_parser_id_expression (parser,
4223 /*template_keyword_p=*/false,
4224 /*check_dependency_p=*/true,
4225 &template_p,
4226 /*declarator_p=*/false,
4227 /*optional_p=*/false);
4228 if (id_expression == error_mark_node)
4229 return error_mark_node;
4230 id_expr_token = token;
4231 token = cp_lexer_peek_token (parser->lexer);
4232 done = (token->type != CPP_OPEN_SQUARE
4233 && token->type != CPP_OPEN_PAREN
4234 && token->type != CPP_DOT
4235 && token->type != CPP_DEREF
4236 && token->type != CPP_PLUS_PLUS
4237 && token->type != CPP_MINUS_MINUS);
4238 /* If we have a template-id, then no further lookup is
4239 required. If the template-id was for a template-class, we
4240 will sometimes have a TYPE_DECL at this point. */
4241 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4242 || TREE_CODE (id_expression) == TYPE_DECL)
4243 decl = id_expression;
4244 /* Look up the name. */
4245 else
4246 {
4247 tree ambiguous_decls;
4248
4249 /* If we already know that this lookup is ambiguous, then
4250 we've already issued an error message; there's no reason
4251 to check again. */
4252 if (id_expr_token->type == CPP_NAME
4253 && id_expr_token->ambiguous_p)
4254 {
4255 cp_parser_simulate_error (parser);
4256 return error_mark_node;
4257 }
4258
4259 decl = cp_parser_lookup_name (parser, id_expression,
4260 none_type,
4261 template_p,
4262 /*is_namespace=*/false,
4263 /*check_dependency=*/true,
4264 &ambiguous_decls,
4265 id_expr_token->location);
4266 /* If the lookup was ambiguous, an error will already have
4267 been issued. */
4268 if (ambiguous_decls)
4269 return error_mark_node;
4270
4271 /* In Objective-C++, we may have an Objective-C 2.0
4272 dot-syntax for classes here. */
4273 if (c_dialect_objc ()
4274 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4275 && TREE_CODE (decl) == TYPE_DECL
4276 && objc_is_class_name (decl))
4277 {
4278 tree component;
4279 cp_lexer_consume_token (parser->lexer);
4280 component = cp_parser_identifier (parser);
4281 if (component == error_mark_node)
4282 return error_mark_node;
4283
4284 return objc_build_class_component_ref (id_expression, component);
4285 }
4286
4287 /* In Objective-C++, an instance variable (ivar) may be preferred
4288 to whatever cp_parser_lookup_name() found. */
4289 decl = objc_lookup_ivar (decl, id_expression);
4290
4291 /* If name lookup gives us a SCOPE_REF, then the
4292 qualifying scope was dependent. */
4293 if (TREE_CODE (decl) == SCOPE_REF)
4294 {
4295 /* At this point, we do not know if DECL is a valid
4296 integral constant expression. We assume that it is
4297 in fact such an expression, so that code like:
4298
4299 template <int N> struct A {
4300 int a[B<N>::i];
4301 };
4302
4303 is accepted. At template-instantiation time, we
4304 will check that B<N>::i is actually a constant. */
4305 return decl;
4306 }
4307 /* Check to see if DECL is a local variable in a context
4308 where that is forbidden. */
4309 if (parser->local_variables_forbidden_p
4310 && local_variable_p (decl))
4311 {
4312 /* It might be that we only found DECL because we are
4313 trying to be generous with pre-ISO scoping rules.
4314 For example, consider:
4315
4316 int i;
4317 void g() {
4318 for (int i = 0; i < 10; ++i) {}
4319 extern void f(int j = i);
4320 }
4321
4322 Here, name look up will originally find the out
4323 of scope `i'. We need to issue a warning message,
4324 but then use the global `i'. */
4325 decl = check_for_out_of_scope_variable (decl);
4326 if (local_variable_p (decl))
4327 {
4328 error_at (id_expr_token->location,
4329 "local variable %qD may not appear in this context",
4330 decl);
4331 return error_mark_node;
4332 }
4333 }
4334 }
4335
4336 decl = (finish_id_expression
4337 (id_expression, decl, parser->scope,
4338 idk,
4339 parser->integral_constant_expression_p,
4340 parser->allow_non_integral_constant_expression_p,
4341 &parser->non_integral_constant_expression_p,
4342 template_p, done, address_p,
4343 template_arg_p,
4344 &error_msg,
4345 id_expr_token->location));
4346 if (error_msg)
4347 cp_parser_error (parser, error_msg);
4348 return decl;
4349 }
4350
4351 /* Anything else is an error. */
4352 default:
4353 cp_parser_error (parser, "expected primary-expression");
4354 return error_mark_node;
4355 }
4356 }
4357
4358 /* Parse an id-expression.
4359
4360 id-expression:
4361 unqualified-id
4362 qualified-id
4363
4364 qualified-id:
4365 :: [opt] nested-name-specifier template [opt] unqualified-id
4366 :: identifier
4367 :: operator-function-id
4368 :: template-id
4369
4370 Return a representation of the unqualified portion of the
4371 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4372 a `::' or nested-name-specifier.
4373
4374 Often, if the id-expression was a qualified-id, the caller will
4375 want to make a SCOPE_REF to represent the qualified-id. This
4376 function does not do this in order to avoid wastefully creating
4377 SCOPE_REFs when they are not required.
4378
4379 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4380 `template' keyword.
4381
4382 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4383 uninstantiated templates.
4384
4385 If *TEMPLATE_P is non-NULL, it is set to true iff the
4386 `template' keyword is used to explicitly indicate that the entity
4387 named is a template.
4388
4389 If DECLARATOR_P is true, the id-expression is appearing as part of
4390 a declarator, rather than as part of an expression. */
4391
4392 static tree
4393 cp_parser_id_expression (cp_parser *parser,
4394 bool template_keyword_p,
4395 bool check_dependency_p,
4396 bool *template_p,
4397 bool declarator_p,
4398 bool optional_p)
4399 {
4400 bool global_scope_p;
4401 bool nested_name_specifier_p;
4402
4403 /* Assume the `template' keyword was not used. */
4404 if (template_p)
4405 *template_p = template_keyword_p;
4406
4407 /* Look for the optional `::' operator. */
4408 global_scope_p
4409 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4410 != NULL_TREE);
4411 /* Look for the optional nested-name-specifier. */
4412 nested_name_specifier_p
4413 = (cp_parser_nested_name_specifier_opt (parser,
4414 /*typename_keyword_p=*/false,
4415 check_dependency_p,
4416 /*type_p=*/false,
4417 declarator_p)
4418 != NULL_TREE);
4419 /* If there is a nested-name-specifier, then we are looking at
4420 the first qualified-id production. */
4421 if (nested_name_specifier_p)
4422 {
4423 tree saved_scope;
4424 tree saved_object_scope;
4425 tree saved_qualifying_scope;
4426 tree unqualified_id;
4427 bool is_template;
4428
4429 /* See if the next token is the `template' keyword. */
4430 if (!template_p)
4431 template_p = &is_template;
4432 *template_p = cp_parser_optional_template_keyword (parser);
4433 /* Name lookup we do during the processing of the
4434 unqualified-id might obliterate SCOPE. */
4435 saved_scope = parser->scope;
4436 saved_object_scope = parser->object_scope;
4437 saved_qualifying_scope = parser->qualifying_scope;
4438 /* Process the final unqualified-id. */
4439 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4440 check_dependency_p,
4441 declarator_p,
4442 /*optional_p=*/false);
4443 /* Restore the SAVED_SCOPE for our caller. */
4444 parser->scope = saved_scope;
4445 parser->object_scope = saved_object_scope;
4446 parser->qualifying_scope = saved_qualifying_scope;
4447
4448 return unqualified_id;
4449 }
4450 /* Otherwise, if we are in global scope, then we are looking at one
4451 of the other qualified-id productions. */
4452 else if (global_scope_p)
4453 {
4454 cp_token *token;
4455 tree id;
4456
4457 /* Peek at the next token. */
4458 token = cp_lexer_peek_token (parser->lexer);
4459
4460 /* If it's an identifier, and the next token is not a "<", then
4461 we can avoid the template-id case. This is an optimization
4462 for this common case. */
4463 if (token->type == CPP_NAME
4464 && !cp_parser_nth_token_starts_template_argument_list_p
4465 (parser, 2))
4466 return cp_parser_identifier (parser);
4467
4468 cp_parser_parse_tentatively (parser);
4469 /* Try a template-id. */
4470 id = cp_parser_template_id (parser,
4471 /*template_keyword_p=*/false,
4472 /*check_dependency_p=*/true,
4473 declarator_p);
4474 /* If that worked, we're done. */
4475 if (cp_parser_parse_definitely (parser))
4476 return id;
4477
4478 /* Peek at the next token. (Changes in the token buffer may
4479 have invalidated the pointer obtained above.) */
4480 token = cp_lexer_peek_token (parser->lexer);
4481
4482 switch (token->type)
4483 {
4484 case CPP_NAME:
4485 return cp_parser_identifier (parser);
4486
4487 case CPP_KEYWORD:
4488 if (token->keyword == RID_OPERATOR)
4489 return cp_parser_operator_function_id (parser);
4490 /* Fall through. */
4491
4492 default:
4493 cp_parser_error (parser, "expected id-expression");
4494 return error_mark_node;
4495 }
4496 }
4497 else
4498 return cp_parser_unqualified_id (parser, template_keyword_p,
4499 /*check_dependency_p=*/true,
4500 declarator_p,
4501 optional_p);
4502 }
4503
4504 /* Parse an unqualified-id.
4505
4506 unqualified-id:
4507 identifier
4508 operator-function-id
4509 conversion-function-id
4510 ~ class-name
4511 template-id
4512
4513 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4514 keyword, in a construct like `A::template ...'.
4515
4516 Returns a representation of unqualified-id. For the `identifier'
4517 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4518 production a BIT_NOT_EXPR is returned; the operand of the
4519 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4520 other productions, see the documentation accompanying the
4521 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4522 names are looked up in uninstantiated templates. If DECLARATOR_P
4523 is true, the unqualified-id is appearing as part of a declarator,
4524 rather than as part of an expression. */
4525
4526 static tree
4527 cp_parser_unqualified_id (cp_parser* parser,
4528 bool template_keyword_p,
4529 bool check_dependency_p,
4530 bool declarator_p,
4531 bool optional_p)
4532 {
4533 cp_token *token;
4534
4535 /* Peek at the next token. */
4536 token = cp_lexer_peek_token (parser->lexer);
4537
4538 switch (token->type)
4539 {
4540 case CPP_NAME:
4541 {
4542 tree id;
4543
4544 /* We don't know yet whether or not this will be a
4545 template-id. */
4546 cp_parser_parse_tentatively (parser);
4547 /* Try a template-id. */
4548 id = cp_parser_template_id (parser, template_keyword_p,
4549 check_dependency_p,
4550 declarator_p);
4551 /* If it worked, we're done. */
4552 if (cp_parser_parse_definitely (parser))
4553 return id;
4554 /* Otherwise, it's an ordinary identifier. */
4555 return cp_parser_identifier (parser);
4556 }
4557
4558 case CPP_TEMPLATE_ID:
4559 return cp_parser_template_id (parser, template_keyword_p,
4560 check_dependency_p,
4561 declarator_p);
4562
4563 case CPP_COMPL:
4564 {
4565 tree type_decl;
4566 tree qualifying_scope;
4567 tree object_scope;
4568 tree scope;
4569 bool done;
4570
4571 /* Consume the `~' token. */
4572 cp_lexer_consume_token (parser->lexer);
4573 /* Parse the class-name. The standard, as written, seems to
4574 say that:
4575
4576 template <typename T> struct S { ~S (); };
4577 template <typename T> S<T>::~S() {}
4578
4579 is invalid, since `~' must be followed by a class-name, but
4580 `S<T>' is dependent, and so not known to be a class.
4581 That's not right; we need to look in uninstantiated
4582 templates. A further complication arises from:
4583
4584 template <typename T> void f(T t) {
4585 t.T::~T();
4586 }
4587
4588 Here, it is not possible to look up `T' in the scope of `T'
4589 itself. We must look in both the current scope, and the
4590 scope of the containing complete expression.
4591
4592 Yet another issue is:
4593
4594 struct S {
4595 int S;
4596 ~S();
4597 };
4598
4599 S::~S() {}
4600
4601 The standard does not seem to say that the `S' in `~S'
4602 should refer to the type `S' and not the data member
4603 `S::S'. */
4604
4605 /* DR 244 says that we look up the name after the "~" in the
4606 same scope as we looked up the qualifying name. That idea
4607 isn't fully worked out; it's more complicated than that. */
4608 scope = parser->scope;
4609 object_scope = parser->object_scope;
4610 qualifying_scope = parser->qualifying_scope;
4611
4612 /* Check for invalid scopes. */
4613 if (scope == error_mark_node)
4614 {
4615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4616 cp_lexer_consume_token (parser->lexer);
4617 return error_mark_node;
4618 }
4619 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4620 {
4621 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4622 error_at (token->location,
4623 "scope %qT before %<~%> is not a class-name",
4624 scope);
4625 cp_parser_simulate_error (parser);
4626 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4627 cp_lexer_consume_token (parser->lexer);
4628 return error_mark_node;
4629 }
4630 gcc_assert (!scope || TYPE_P (scope));
4631
4632 /* If the name is of the form "X::~X" it's OK even if X is a
4633 typedef. */
4634 token = cp_lexer_peek_token (parser->lexer);
4635 if (scope
4636 && token->type == CPP_NAME
4637 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4638 != CPP_LESS)
4639 && (token->u.value == TYPE_IDENTIFIER (scope)
4640 || (CLASS_TYPE_P (scope)
4641 && constructor_name_p (token->u.value, scope))))
4642 {
4643 cp_lexer_consume_token (parser->lexer);
4644 return build_nt (BIT_NOT_EXPR, scope);
4645 }
4646
4647 /* If there was an explicit qualification (S::~T), first look
4648 in the scope given by the qualification (i.e., S).
4649
4650 Note: in the calls to cp_parser_class_name below we pass
4651 typename_type so that lookup finds the injected-class-name
4652 rather than the constructor. */
4653 done = false;
4654 type_decl = NULL_TREE;
4655 if (scope)
4656 {
4657 cp_parser_parse_tentatively (parser);
4658 type_decl = cp_parser_class_name (parser,
4659 /*typename_keyword_p=*/false,
4660 /*template_keyword_p=*/false,
4661 typename_type,
4662 /*check_dependency=*/false,
4663 /*class_head_p=*/false,
4664 declarator_p);
4665 if (cp_parser_parse_definitely (parser))
4666 done = true;
4667 }
4668 /* In "N::S::~S", look in "N" as well. */
4669 if (!done && scope && qualifying_scope)
4670 {
4671 cp_parser_parse_tentatively (parser);
4672 parser->scope = qualifying_scope;
4673 parser->object_scope = NULL_TREE;
4674 parser->qualifying_scope = NULL_TREE;
4675 type_decl
4676 = cp_parser_class_name (parser,
4677 /*typename_keyword_p=*/false,
4678 /*template_keyword_p=*/false,
4679 typename_type,
4680 /*check_dependency=*/false,
4681 /*class_head_p=*/false,
4682 declarator_p);
4683 if (cp_parser_parse_definitely (parser))
4684 done = true;
4685 }
4686 /* In "p->S::~T", look in the scope given by "*p" as well. */
4687 else if (!done && object_scope)
4688 {
4689 cp_parser_parse_tentatively (parser);
4690 parser->scope = object_scope;
4691 parser->object_scope = NULL_TREE;
4692 parser->qualifying_scope = NULL_TREE;
4693 type_decl
4694 = cp_parser_class_name (parser,
4695 /*typename_keyword_p=*/false,
4696 /*template_keyword_p=*/false,
4697 typename_type,
4698 /*check_dependency=*/false,
4699 /*class_head_p=*/false,
4700 declarator_p);
4701 if (cp_parser_parse_definitely (parser))
4702 done = true;
4703 }
4704 /* Look in the surrounding context. */
4705 if (!done)
4706 {
4707 parser->scope = NULL_TREE;
4708 parser->object_scope = NULL_TREE;
4709 parser->qualifying_scope = NULL_TREE;
4710 if (processing_template_decl)
4711 cp_parser_parse_tentatively (parser);
4712 type_decl
4713 = cp_parser_class_name (parser,
4714 /*typename_keyword_p=*/false,
4715 /*template_keyword_p=*/false,
4716 typename_type,
4717 /*check_dependency=*/false,
4718 /*class_head_p=*/false,
4719 declarator_p);
4720 if (processing_template_decl
4721 && ! cp_parser_parse_definitely (parser))
4722 {
4723 /* We couldn't find a type with this name, so just accept
4724 it and check for a match at instantiation time. */
4725 type_decl = cp_parser_identifier (parser);
4726 if (type_decl != error_mark_node)
4727 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4728 return type_decl;
4729 }
4730 }
4731 /* If an error occurred, assume that the name of the
4732 destructor is the same as the name of the qualifying
4733 class. That allows us to keep parsing after running
4734 into ill-formed destructor names. */
4735 if (type_decl == error_mark_node && scope)
4736 return build_nt (BIT_NOT_EXPR, scope);
4737 else if (type_decl == error_mark_node)
4738 return error_mark_node;
4739
4740 /* Check that destructor name and scope match. */
4741 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4742 {
4743 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4744 error_at (token->location,
4745 "declaration of %<~%T%> as member of %qT",
4746 type_decl, scope);
4747 cp_parser_simulate_error (parser);
4748 return error_mark_node;
4749 }
4750
4751 /* [class.dtor]
4752
4753 A typedef-name that names a class shall not be used as the
4754 identifier in the declarator for a destructor declaration. */
4755 if (declarator_p
4756 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4757 && !DECL_SELF_REFERENCE_P (type_decl)
4758 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4759 error_at (token->location,
4760 "typedef-name %qD used as destructor declarator",
4761 type_decl);
4762
4763 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4764 }
4765
4766 case CPP_KEYWORD:
4767 if (token->keyword == RID_OPERATOR)
4768 {
4769 tree id;
4770
4771 /* This could be a template-id, so we try that first. */
4772 cp_parser_parse_tentatively (parser);
4773 /* Try a template-id. */
4774 id = cp_parser_template_id (parser, template_keyword_p,
4775 /*check_dependency_p=*/true,
4776 declarator_p);
4777 /* If that worked, we're done. */
4778 if (cp_parser_parse_definitely (parser))
4779 return id;
4780 /* We still don't know whether we're looking at an
4781 operator-function-id or a conversion-function-id. */
4782 cp_parser_parse_tentatively (parser);
4783 /* Try an operator-function-id. */
4784 id = cp_parser_operator_function_id (parser);
4785 /* If that didn't work, try a conversion-function-id. */
4786 if (!cp_parser_parse_definitely (parser))
4787 id = cp_parser_conversion_function_id (parser);
4788 else if (UDLIT_OPER_P (id))
4789 {
4790 /* 17.6.3.3.5 */
4791 const char *name = UDLIT_OP_SUFFIX (id);
4792 if (name[0] != '_' && !in_system_header)
4793 warning (0, "literal operator suffixes not preceded by %<_%>"
4794 " are reserved for future standardization");
4795 }
4796
4797 return id;
4798 }
4799 /* Fall through. */
4800
4801 default:
4802 if (optional_p)
4803 return NULL_TREE;
4804 cp_parser_error (parser, "expected unqualified-id");
4805 return error_mark_node;
4806 }
4807 }
4808
4809 /* Parse an (optional) nested-name-specifier.
4810
4811 nested-name-specifier: [C++98]
4812 class-or-namespace-name :: nested-name-specifier [opt]
4813 class-or-namespace-name :: template nested-name-specifier [opt]
4814
4815 nested-name-specifier: [C++0x]
4816 type-name ::
4817 namespace-name ::
4818 nested-name-specifier identifier ::
4819 nested-name-specifier template [opt] simple-template-id ::
4820
4821 PARSER->SCOPE should be set appropriately before this function is
4822 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4823 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4824 in name lookups.
4825
4826 Sets PARSER->SCOPE to the class (TYPE) or namespace
4827 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4828 it unchanged if there is no nested-name-specifier. Returns the new
4829 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4830
4831 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4832 part of a declaration and/or decl-specifier. */
4833
4834 static tree
4835 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4836 bool typename_keyword_p,
4837 bool check_dependency_p,
4838 bool type_p,
4839 bool is_declaration)
4840 {
4841 bool success = false;
4842 cp_token_position start = 0;
4843 cp_token *token;
4844
4845 /* Remember where the nested-name-specifier starts. */
4846 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4847 {
4848 start = cp_lexer_token_position (parser->lexer, false);
4849 push_deferring_access_checks (dk_deferred);
4850 }
4851
4852 while (true)
4853 {
4854 tree new_scope;
4855 tree old_scope;
4856 tree saved_qualifying_scope;
4857 bool template_keyword_p;
4858
4859 /* Spot cases that cannot be the beginning of a
4860 nested-name-specifier. */
4861 token = cp_lexer_peek_token (parser->lexer);
4862
4863 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4864 the already parsed nested-name-specifier. */
4865 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4866 {
4867 /* Grab the nested-name-specifier and continue the loop. */
4868 cp_parser_pre_parsed_nested_name_specifier (parser);
4869 /* If we originally encountered this nested-name-specifier
4870 with IS_DECLARATION set to false, we will not have
4871 resolved TYPENAME_TYPEs, so we must do so here. */
4872 if (is_declaration
4873 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4874 {
4875 new_scope = resolve_typename_type (parser->scope,
4876 /*only_current_p=*/false);
4877 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4878 parser->scope = new_scope;
4879 }
4880 success = true;
4881 continue;
4882 }
4883
4884 /* Spot cases that cannot be the beginning of a
4885 nested-name-specifier. On the second and subsequent times
4886 through the loop, we look for the `template' keyword. */
4887 if (success && token->keyword == RID_TEMPLATE)
4888 ;
4889 /* A template-id can start a nested-name-specifier. */
4890 else if (token->type == CPP_TEMPLATE_ID)
4891 ;
4892 /* DR 743: decltype can be used in a nested-name-specifier. */
4893 else if (token_is_decltype (token))
4894 ;
4895 else
4896 {
4897 /* If the next token is not an identifier, then it is
4898 definitely not a type-name or namespace-name. */
4899 if (token->type != CPP_NAME)
4900 break;
4901 /* If the following token is neither a `<' (to begin a
4902 template-id), nor a `::', then we are not looking at a
4903 nested-name-specifier. */
4904 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4905
4906 if (token->type == CPP_COLON
4907 && parser->colon_corrects_to_scope_p
4908 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4909 {
4910 error_at (token->location,
4911 "found %<:%> in nested-name-specifier, expected %<::%>");
4912 token->type = CPP_SCOPE;
4913 }
4914
4915 if (token->type != CPP_SCOPE
4916 && !cp_parser_nth_token_starts_template_argument_list_p
4917 (parser, 2))
4918 break;
4919 }
4920
4921 /* The nested-name-specifier is optional, so we parse
4922 tentatively. */
4923 cp_parser_parse_tentatively (parser);
4924
4925 /* Look for the optional `template' keyword, if this isn't the
4926 first time through the loop. */
4927 if (success)
4928 template_keyword_p = cp_parser_optional_template_keyword (parser);
4929 else
4930 template_keyword_p = false;
4931
4932 /* Save the old scope since the name lookup we are about to do
4933 might destroy it. */
4934 old_scope = parser->scope;
4935 saved_qualifying_scope = parser->qualifying_scope;
4936 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4937 look up names in "X<T>::I" in order to determine that "Y" is
4938 a template. So, if we have a typename at this point, we make
4939 an effort to look through it. */
4940 if (is_declaration
4941 && !typename_keyword_p
4942 && parser->scope
4943 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4944 parser->scope = resolve_typename_type (parser->scope,
4945 /*only_current_p=*/false);
4946 /* Parse the qualifying entity. */
4947 new_scope
4948 = cp_parser_qualifying_entity (parser,
4949 typename_keyword_p,
4950 template_keyword_p,
4951 check_dependency_p,
4952 type_p,
4953 is_declaration);
4954 /* Look for the `::' token. */
4955 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4956
4957 /* If we found what we wanted, we keep going; otherwise, we're
4958 done. */
4959 if (!cp_parser_parse_definitely (parser))
4960 {
4961 bool error_p = false;
4962
4963 /* Restore the OLD_SCOPE since it was valid before the
4964 failed attempt at finding the last
4965 class-or-namespace-name. */
4966 parser->scope = old_scope;
4967 parser->qualifying_scope = saved_qualifying_scope;
4968
4969 /* If the next token is a decltype, and the one after that is a
4970 `::', then the decltype has failed to resolve to a class or
4971 enumeration type. Give this error even when parsing
4972 tentatively since it can't possibly be valid--and we're going
4973 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4974 won't get another chance.*/
4975 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4976 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4977 == CPP_SCOPE))
4978 {
4979 token = cp_lexer_consume_token (parser->lexer);
4980 error_at (token->location, "decltype evaluates to %qT, "
4981 "which is not a class or enumeration type",
4982 token->u.value);
4983 parser->scope = error_mark_node;
4984 error_p = true;
4985 /* As below. */
4986 success = true;
4987 cp_lexer_consume_token (parser->lexer);
4988 }
4989
4990 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4991 break;
4992 /* If the next token is an identifier, and the one after
4993 that is a `::', then any valid interpretation would have
4994 found a class-or-namespace-name. */
4995 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4996 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4997 == CPP_SCOPE)
4998 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4999 != CPP_COMPL))
5000 {
5001 token = cp_lexer_consume_token (parser->lexer);
5002 if (!error_p)
5003 {
5004 if (!token->ambiguous_p)
5005 {
5006 tree decl;
5007 tree ambiguous_decls;
5008
5009 decl = cp_parser_lookup_name (parser, token->u.value,
5010 none_type,
5011 /*is_template=*/false,
5012 /*is_namespace=*/false,
5013 /*check_dependency=*/true,
5014 &ambiguous_decls,
5015 token->location);
5016 if (TREE_CODE (decl) == TEMPLATE_DECL)
5017 error_at (token->location,
5018 "%qD used without template parameters",
5019 decl);
5020 else if (ambiguous_decls)
5021 {
5022 error_at (token->location,
5023 "reference to %qD is ambiguous",
5024 token->u.value);
5025 print_candidates (ambiguous_decls);
5026 decl = error_mark_node;
5027 }
5028 else
5029 {
5030 if (cxx_dialect != cxx98)
5031 cp_parser_name_lookup_error
5032 (parser, token->u.value, decl, NLE_NOT_CXX98,
5033 token->location);
5034 else
5035 cp_parser_name_lookup_error
5036 (parser, token->u.value, decl, NLE_CXX98,
5037 token->location);
5038 }
5039 }
5040 parser->scope = error_mark_node;
5041 error_p = true;
5042 /* Treat this as a successful nested-name-specifier
5043 due to:
5044
5045 [basic.lookup.qual]
5046
5047 If the name found is not a class-name (clause
5048 _class_) or namespace-name (_namespace.def_), the
5049 program is ill-formed. */
5050 success = true;
5051 }
5052 cp_lexer_consume_token (parser->lexer);
5053 }
5054 break;
5055 }
5056 /* We've found one valid nested-name-specifier. */
5057 success = true;
5058 /* Name lookup always gives us a DECL. */
5059 if (TREE_CODE (new_scope) == TYPE_DECL)
5060 new_scope = TREE_TYPE (new_scope);
5061 /* Uses of "template" must be followed by actual templates. */
5062 if (template_keyword_p
5063 && !(CLASS_TYPE_P (new_scope)
5064 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5065 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5066 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5067 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5068 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5069 == TEMPLATE_ID_EXPR)))
5070 permerror (input_location, TYPE_P (new_scope)
5071 ? G_("%qT is not a template")
5072 : G_("%qD is not a template"),
5073 new_scope);
5074 /* If it is a class scope, try to complete it; we are about to
5075 be looking up names inside the class. */
5076 if (TYPE_P (new_scope)
5077 /* Since checking types for dependency can be expensive,
5078 avoid doing it if the type is already complete. */
5079 && !COMPLETE_TYPE_P (new_scope)
5080 /* Do not try to complete dependent types. */
5081 && !dependent_type_p (new_scope))
5082 {
5083 new_scope = complete_type (new_scope);
5084 /* If it is a typedef to current class, use the current
5085 class instead, as the typedef won't have any names inside
5086 it yet. */
5087 if (!COMPLETE_TYPE_P (new_scope)
5088 && currently_open_class (new_scope))
5089 new_scope = TYPE_MAIN_VARIANT (new_scope);
5090 }
5091 /* Make sure we look in the right scope the next time through
5092 the loop. */
5093 parser->scope = new_scope;
5094 }
5095
5096 /* If parsing tentatively, replace the sequence of tokens that makes
5097 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5098 token. That way, should we re-parse the token stream, we will
5099 not have to repeat the effort required to do the parse, nor will
5100 we issue duplicate error messages. */
5101 if (success && start)
5102 {
5103 cp_token *token;
5104
5105 token = cp_lexer_token_at (parser->lexer, start);
5106 /* Reset the contents of the START token. */
5107 token->type = CPP_NESTED_NAME_SPECIFIER;
5108 /* Retrieve any deferred checks. Do not pop this access checks yet
5109 so the memory will not be reclaimed during token replacing below. */
5110 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5111 token->u.tree_check_value->value = parser->scope;
5112 token->u.tree_check_value->checks = get_deferred_access_checks ();
5113 token->u.tree_check_value->qualifying_scope =
5114 parser->qualifying_scope;
5115 token->keyword = RID_MAX;
5116
5117 /* Purge all subsequent tokens. */
5118 cp_lexer_purge_tokens_after (parser->lexer, start);
5119 }
5120
5121 if (start)
5122 pop_to_parent_deferring_access_checks ();
5123
5124 return success ? parser->scope : NULL_TREE;
5125 }
5126
5127 /* Parse a nested-name-specifier. See
5128 cp_parser_nested_name_specifier_opt for details. This function
5129 behaves identically, except that it will an issue an error if no
5130 nested-name-specifier is present. */
5131
5132 static tree
5133 cp_parser_nested_name_specifier (cp_parser *parser,
5134 bool typename_keyword_p,
5135 bool check_dependency_p,
5136 bool type_p,
5137 bool is_declaration)
5138 {
5139 tree scope;
5140
5141 /* Look for the nested-name-specifier. */
5142 scope = cp_parser_nested_name_specifier_opt (parser,
5143 typename_keyword_p,
5144 check_dependency_p,
5145 type_p,
5146 is_declaration);
5147 /* If it was not present, issue an error message. */
5148 if (!scope)
5149 {
5150 cp_parser_error (parser, "expected nested-name-specifier");
5151 parser->scope = NULL_TREE;
5152 }
5153
5154 return scope;
5155 }
5156
5157 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5158 this is either a class-name or a namespace-name (which corresponds
5159 to the class-or-namespace-name production in the grammar). For
5160 C++0x, it can also be a type-name that refers to an enumeration
5161 type or a simple-template-id.
5162
5163 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5164 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5165 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5166 TYPE_P is TRUE iff the next name should be taken as a class-name,
5167 even the same name is declared to be another entity in the same
5168 scope.
5169
5170 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5171 specified by the class-or-namespace-name. If neither is found the
5172 ERROR_MARK_NODE is returned. */
5173
5174 static tree
5175 cp_parser_qualifying_entity (cp_parser *parser,
5176 bool typename_keyword_p,
5177 bool template_keyword_p,
5178 bool check_dependency_p,
5179 bool type_p,
5180 bool is_declaration)
5181 {
5182 tree saved_scope;
5183 tree saved_qualifying_scope;
5184 tree saved_object_scope;
5185 tree scope;
5186 bool only_class_p;
5187 bool successful_parse_p;
5188
5189 /* DR 743: decltype can appear in a nested-name-specifier. */
5190 if (cp_lexer_next_token_is_decltype (parser->lexer))
5191 {
5192 scope = cp_parser_decltype (parser);
5193 if (TREE_CODE (scope) != ENUMERAL_TYPE
5194 && !MAYBE_CLASS_TYPE_P (scope))
5195 {
5196 cp_parser_simulate_error (parser);
5197 return error_mark_node;
5198 }
5199 if (TYPE_NAME (scope))
5200 scope = TYPE_NAME (scope);
5201 return scope;
5202 }
5203
5204 /* Before we try to parse the class-name, we must save away the
5205 current PARSER->SCOPE since cp_parser_class_name will destroy
5206 it. */
5207 saved_scope = parser->scope;
5208 saved_qualifying_scope = parser->qualifying_scope;
5209 saved_object_scope = parser->object_scope;
5210 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5211 there is no need to look for a namespace-name. */
5212 only_class_p = template_keyword_p
5213 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5214 if (!only_class_p)
5215 cp_parser_parse_tentatively (parser);
5216 scope = cp_parser_class_name (parser,
5217 typename_keyword_p,
5218 template_keyword_p,
5219 type_p ? class_type : none_type,
5220 check_dependency_p,
5221 /*class_head_p=*/false,
5222 is_declaration);
5223 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5224 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5225 if (!only_class_p
5226 && cxx_dialect != cxx98
5227 && !successful_parse_p)
5228 {
5229 /* Restore the saved scope. */
5230 parser->scope = saved_scope;
5231 parser->qualifying_scope = saved_qualifying_scope;
5232 parser->object_scope = saved_object_scope;
5233
5234 /* Parse tentatively. */
5235 cp_parser_parse_tentatively (parser);
5236
5237 /* Parse a type-name */
5238 scope = cp_parser_type_name (parser);
5239
5240 /* "If the name found does not designate a namespace or a class,
5241 enumeration, or dependent type, the program is ill-formed."
5242
5243 We cover classes and dependent types above and namespaces below,
5244 so this code is only looking for enums. */
5245 if (!scope || TREE_CODE (scope) != TYPE_DECL
5246 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5247 cp_parser_simulate_error (parser);
5248
5249 successful_parse_p = cp_parser_parse_definitely (parser);
5250 }
5251 /* If that didn't work, try for a namespace-name. */
5252 if (!only_class_p && !successful_parse_p)
5253 {
5254 /* Restore the saved scope. */
5255 parser->scope = saved_scope;
5256 parser->qualifying_scope = saved_qualifying_scope;
5257 parser->object_scope = saved_object_scope;
5258 /* If we are not looking at an identifier followed by the scope
5259 resolution operator, then this is not part of a
5260 nested-name-specifier. (Note that this function is only used
5261 to parse the components of a nested-name-specifier.) */
5262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5263 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5264 return error_mark_node;
5265 scope = cp_parser_namespace_name (parser);
5266 }
5267
5268 return scope;
5269 }
5270
5271 /* Parse a postfix-expression.
5272
5273 postfix-expression:
5274 primary-expression
5275 postfix-expression [ expression ]
5276 postfix-expression ( expression-list [opt] )
5277 simple-type-specifier ( expression-list [opt] )
5278 typename :: [opt] nested-name-specifier identifier
5279 ( expression-list [opt] )
5280 typename :: [opt] nested-name-specifier template [opt] template-id
5281 ( expression-list [opt] )
5282 postfix-expression . template [opt] id-expression
5283 postfix-expression -> template [opt] id-expression
5284 postfix-expression . pseudo-destructor-name
5285 postfix-expression -> pseudo-destructor-name
5286 postfix-expression ++
5287 postfix-expression --
5288 dynamic_cast < type-id > ( expression )
5289 static_cast < type-id > ( expression )
5290 reinterpret_cast < type-id > ( expression )
5291 const_cast < type-id > ( expression )
5292 typeid ( expression )
5293 typeid ( type-id )
5294
5295 GNU Extension:
5296
5297 postfix-expression:
5298 ( type-id ) { initializer-list , [opt] }
5299
5300 This extension is a GNU version of the C99 compound-literal
5301 construct. (The C99 grammar uses `type-name' instead of `type-id',
5302 but they are essentially the same concept.)
5303
5304 If ADDRESS_P is true, the postfix expression is the operand of the
5305 `&' operator. CAST_P is true if this expression is the target of a
5306 cast.
5307
5308 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5309 class member access expressions [expr.ref].
5310
5311 Returns a representation of the expression. */
5312
5313 static tree
5314 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5315 bool member_access_only_p,
5316 cp_id_kind * pidk_return)
5317 {
5318 cp_token *token;
5319 enum rid keyword;
5320 cp_id_kind idk = CP_ID_KIND_NONE;
5321 tree postfix_expression = NULL_TREE;
5322 bool is_member_access = false;
5323
5324 /* Peek at the next token. */
5325 token = cp_lexer_peek_token (parser->lexer);
5326 /* Some of the productions are determined by keywords. */
5327 keyword = token->keyword;
5328 switch (keyword)
5329 {
5330 case RID_DYNCAST:
5331 case RID_STATCAST:
5332 case RID_REINTCAST:
5333 case RID_CONSTCAST:
5334 {
5335 tree type;
5336 tree expression;
5337 const char *saved_message;
5338
5339 /* All of these can be handled in the same way from the point
5340 of view of parsing. Begin by consuming the token
5341 identifying the cast. */
5342 cp_lexer_consume_token (parser->lexer);
5343
5344 /* New types cannot be defined in the cast. */
5345 saved_message = parser->type_definition_forbidden_message;
5346 parser->type_definition_forbidden_message
5347 = G_("types may not be defined in casts");
5348
5349 /* Look for the opening `<'. */
5350 cp_parser_require (parser, CPP_LESS, RT_LESS);
5351 /* Parse the type to which we are casting. */
5352 type = cp_parser_type_id (parser);
5353 /* Look for the closing `>'. */
5354 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5355 /* Restore the old message. */
5356 parser->type_definition_forbidden_message = saved_message;
5357
5358 /* And the expression which is being cast. */
5359 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5360 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5361 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5362
5363 /* Only type conversions to integral or enumeration types
5364 can be used in constant-expressions. */
5365 if (!cast_valid_in_integral_constant_expression_p (type)
5366 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5367 return error_mark_node;
5368
5369 switch (keyword)
5370 {
5371 case RID_DYNCAST:
5372 postfix_expression
5373 = build_dynamic_cast (type, expression, tf_warning_or_error);
5374 break;
5375 case RID_STATCAST:
5376 postfix_expression
5377 = build_static_cast (type, expression, tf_warning_or_error);
5378 break;
5379 case RID_REINTCAST:
5380 postfix_expression
5381 = build_reinterpret_cast (type, expression,
5382 tf_warning_or_error);
5383 break;
5384 case RID_CONSTCAST:
5385 postfix_expression
5386 = build_const_cast (type, expression, tf_warning_or_error);
5387 break;
5388 default:
5389 gcc_unreachable ();
5390 }
5391 }
5392 break;
5393
5394 case RID_TYPEID:
5395 {
5396 tree type;
5397 const char *saved_message;
5398 bool saved_in_type_id_in_expr_p;
5399
5400 /* Consume the `typeid' token. */
5401 cp_lexer_consume_token (parser->lexer);
5402 /* Look for the `(' token. */
5403 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5404 /* Types cannot be defined in a `typeid' expression. */
5405 saved_message = parser->type_definition_forbidden_message;
5406 parser->type_definition_forbidden_message
5407 = G_("types may not be defined in a %<typeid%> expression");
5408 /* We can't be sure yet whether we're looking at a type-id or an
5409 expression. */
5410 cp_parser_parse_tentatively (parser);
5411 /* Try a type-id first. */
5412 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5413 parser->in_type_id_in_expr_p = true;
5414 type = cp_parser_type_id (parser);
5415 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5416 /* Look for the `)' token. Otherwise, we can't be sure that
5417 we're not looking at an expression: consider `typeid (int
5418 (3))', for example. */
5419 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5420 /* If all went well, simply lookup the type-id. */
5421 if (cp_parser_parse_definitely (parser))
5422 postfix_expression = get_typeid (type);
5423 /* Otherwise, fall back to the expression variant. */
5424 else
5425 {
5426 tree expression;
5427
5428 /* Look for an expression. */
5429 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5430 /* Compute its typeid. */
5431 postfix_expression = build_typeid (expression);
5432 /* Look for the `)' token. */
5433 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5434 }
5435 /* Restore the saved message. */
5436 parser->type_definition_forbidden_message = saved_message;
5437 /* `typeid' may not appear in an integral constant expression. */
5438 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5439 return error_mark_node;
5440 }
5441 break;
5442
5443 case RID_TYPENAME:
5444 {
5445 tree type;
5446 /* The syntax permitted here is the same permitted for an
5447 elaborated-type-specifier. */
5448 type = cp_parser_elaborated_type_specifier (parser,
5449 /*is_friend=*/false,
5450 /*is_declaration=*/false);
5451 postfix_expression = cp_parser_functional_cast (parser, type);
5452 }
5453 break;
5454
5455 default:
5456 {
5457 tree type;
5458
5459 /* If the next thing is a simple-type-specifier, we may be
5460 looking at a functional cast. We could also be looking at
5461 an id-expression. So, we try the functional cast, and if
5462 that doesn't work we fall back to the primary-expression. */
5463 cp_parser_parse_tentatively (parser);
5464 /* Look for the simple-type-specifier. */
5465 type = cp_parser_simple_type_specifier (parser,
5466 /*decl_specs=*/NULL,
5467 CP_PARSER_FLAGS_NONE);
5468 /* Parse the cast itself. */
5469 if (!cp_parser_error_occurred (parser))
5470 postfix_expression
5471 = cp_parser_functional_cast (parser, type);
5472 /* If that worked, we're done. */
5473 if (cp_parser_parse_definitely (parser))
5474 break;
5475
5476 /* If the functional-cast didn't work out, try a
5477 compound-literal. */
5478 if (cp_parser_allow_gnu_extensions_p (parser)
5479 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5480 {
5481 VEC(constructor_elt,gc) *initializer_list = NULL;
5482 bool saved_in_type_id_in_expr_p;
5483
5484 cp_parser_parse_tentatively (parser);
5485 /* Consume the `('. */
5486 cp_lexer_consume_token (parser->lexer);
5487 /* Parse the type. */
5488 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5489 parser->in_type_id_in_expr_p = true;
5490 type = cp_parser_type_id (parser);
5491 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5492 /* Look for the `)'. */
5493 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5494 /* Look for the `{'. */
5495 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5496 /* If things aren't going well, there's no need to
5497 keep going. */
5498 if (!cp_parser_error_occurred (parser))
5499 {
5500 bool non_constant_p;
5501 /* Parse the initializer-list. */
5502 initializer_list
5503 = cp_parser_initializer_list (parser, &non_constant_p);
5504 /* Allow a trailing `,'. */
5505 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5506 cp_lexer_consume_token (parser->lexer);
5507 /* Look for the final `}'. */
5508 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5509 }
5510 /* If that worked, we're definitely looking at a
5511 compound-literal expression. */
5512 if (cp_parser_parse_definitely (parser))
5513 {
5514 /* Warn the user that a compound literal is not
5515 allowed in standard C++. */
5516 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5517 /* For simplicity, we disallow compound literals in
5518 constant-expressions. We could
5519 allow compound literals of integer type, whose
5520 initializer was a constant, in constant
5521 expressions. Permitting that usage, as a further
5522 extension, would not change the meaning of any
5523 currently accepted programs. (Of course, as
5524 compound literals are not part of ISO C++, the
5525 standard has nothing to say.) */
5526 if (cp_parser_non_integral_constant_expression (parser,
5527 NIC_NCC))
5528 {
5529 postfix_expression = error_mark_node;
5530 break;
5531 }
5532 /* Form the representation of the compound-literal. */
5533 postfix_expression
5534 = (finish_compound_literal
5535 (type, build_constructor (init_list_type_node,
5536 initializer_list),
5537 tf_warning_or_error));
5538 break;
5539 }
5540 }
5541
5542 /* It must be a primary-expression. */
5543 postfix_expression
5544 = cp_parser_primary_expression (parser, address_p, cast_p,
5545 /*template_arg_p=*/false,
5546 &idk);
5547 }
5548 break;
5549 }
5550
5551 /* Keep looping until the postfix-expression is complete. */
5552 while (true)
5553 {
5554 if (idk == CP_ID_KIND_UNQUALIFIED
5555 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5556 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5557 /* It is not a Koenig lookup function call. */
5558 postfix_expression
5559 = unqualified_name_lookup_error (postfix_expression);
5560
5561 /* Peek at the next token. */
5562 token = cp_lexer_peek_token (parser->lexer);
5563
5564 switch (token->type)
5565 {
5566 case CPP_OPEN_SQUARE:
5567 postfix_expression
5568 = cp_parser_postfix_open_square_expression (parser,
5569 postfix_expression,
5570 false);
5571 idk = CP_ID_KIND_NONE;
5572 is_member_access = false;
5573 break;
5574
5575 case CPP_OPEN_PAREN:
5576 /* postfix-expression ( expression-list [opt] ) */
5577 {
5578 bool koenig_p;
5579 bool is_builtin_constant_p;
5580 bool saved_integral_constant_expression_p = false;
5581 bool saved_non_integral_constant_expression_p = false;
5582 VEC(tree,gc) *args;
5583
5584 is_member_access = false;
5585
5586 is_builtin_constant_p
5587 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5588 if (is_builtin_constant_p)
5589 {
5590 /* The whole point of __builtin_constant_p is to allow
5591 non-constant expressions to appear as arguments. */
5592 saved_integral_constant_expression_p
5593 = parser->integral_constant_expression_p;
5594 saved_non_integral_constant_expression_p
5595 = parser->non_integral_constant_expression_p;
5596 parser->integral_constant_expression_p = false;
5597 }
5598 args = (cp_parser_parenthesized_expression_list
5599 (parser, non_attr,
5600 /*cast_p=*/false, /*allow_expansion_p=*/true,
5601 /*non_constant_p=*/NULL));
5602 if (is_builtin_constant_p)
5603 {
5604 parser->integral_constant_expression_p
5605 = saved_integral_constant_expression_p;
5606 parser->non_integral_constant_expression_p
5607 = saved_non_integral_constant_expression_p;
5608 }
5609
5610 if (args == NULL)
5611 {
5612 postfix_expression = error_mark_node;
5613 break;
5614 }
5615
5616 /* Function calls are not permitted in
5617 constant-expressions. */
5618 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5619 && cp_parser_non_integral_constant_expression (parser,
5620 NIC_FUNC_CALL))
5621 {
5622 postfix_expression = error_mark_node;
5623 release_tree_vector (args);
5624 break;
5625 }
5626
5627 koenig_p = false;
5628 if (idk == CP_ID_KIND_UNQUALIFIED
5629 || idk == CP_ID_KIND_TEMPLATE_ID)
5630 {
5631 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5632 {
5633 if (!VEC_empty (tree, args))
5634 {
5635 koenig_p = true;
5636 if (!any_type_dependent_arguments_p (args))
5637 postfix_expression
5638 = perform_koenig_lookup (postfix_expression, args,
5639 /*include_std=*/false,
5640 tf_warning_or_error);
5641 }
5642 else
5643 postfix_expression
5644 = unqualified_fn_lookup_error (postfix_expression);
5645 }
5646 /* We do not perform argument-dependent lookup if
5647 normal lookup finds a non-function, in accordance
5648 with the expected resolution of DR 218. */
5649 else if (!VEC_empty (tree, args)
5650 && is_overloaded_fn (postfix_expression))
5651 {
5652 tree fn = get_first_fn (postfix_expression);
5653 fn = STRIP_TEMPLATE (fn);
5654
5655 /* Do not do argument dependent lookup if regular
5656 lookup finds a member function or a block-scope
5657 function declaration. [basic.lookup.argdep]/3 */
5658 if (!DECL_FUNCTION_MEMBER_P (fn)
5659 && !DECL_LOCAL_FUNCTION_P (fn))
5660 {
5661 koenig_p = true;
5662 if (!any_type_dependent_arguments_p (args))
5663 postfix_expression
5664 = perform_koenig_lookup (postfix_expression, args,
5665 /*include_std=*/false,
5666 tf_warning_or_error);
5667 }
5668 }
5669 }
5670
5671 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5672 {
5673 tree instance = TREE_OPERAND (postfix_expression, 0);
5674 tree fn = TREE_OPERAND (postfix_expression, 1);
5675
5676 if (processing_template_decl
5677 && (type_dependent_expression_p (instance)
5678 || (!BASELINK_P (fn)
5679 && TREE_CODE (fn) != FIELD_DECL)
5680 || type_dependent_expression_p (fn)
5681 || any_type_dependent_arguments_p (args)))
5682 {
5683 postfix_expression
5684 = build_nt_call_vec (postfix_expression, args);
5685 release_tree_vector (args);
5686 break;
5687 }
5688
5689 if (BASELINK_P (fn))
5690 {
5691 postfix_expression
5692 = (build_new_method_call
5693 (instance, fn, &args, NULL_TREE,
5694 (idk == CP_ID_KIND_QUALIFIED
5695 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5696 : LOOKUP_NORMAL),
5697 /*fn_p=*/NULL,
5698 tf_warning_or_error));
5699 }
5700 else
5701 postfix_expression
5702 = finish_call_expr (postfix_expression, &args,
5703 /*disallow_virtual=*/false,
5704 /*koenig_p=*/false,
5705 tf_warning_or_error);
5706 }
5707 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5708 || TREE_CODE (postfix_expression) == MEMBER_REF
5709 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5710 postfix_expression = (build_offset_ref_call_from_tree
5711 (postfix_expression, &args));
5712 else if (idk == CP_ID_KIND_QUALIFIED)
5713 /* A call to a static class member, or a namespace-scope
5714 function. */
5715 postfix_expression
5716 = finish_call_expr (postfix_expression, &args,
5717 /*disallow_virtual=*/true,
5718 koenig_p,
5719 tf_warning_or_error);
5720 else
5721 /* All other function calls. */
5722 postfix_expression
5723 = finish_call_expr (postfix_expression, &args,
5724 /*disallow_virtual=*/false,
5725 koenig_p,
5726 tf_warning_or_error);
5727
5728 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5729 idk = CP_ID_KIND_NONE;
5730
5731 release_tree_vector (args);
5732 }
5733 break;
5734
5735 case CPP_DOT:
5736 case CPP_DEREF:
5737 /* postfix-expression . template [opt] id-expression
5738 postfix-expression . pseudo-destructor-name
5739 postfix-expression -> template [opt] id-expression
5740 postfix-expression -> pseudo-destructor-name */
5741
5742 /* Consume the `.' or `->' operator. */
5743 cp_lexer_consume_token (parser->lexer);
5744
5745 postfix_expression
5746 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5747 postfix_expression,
5748 false, &idk,
5749 token->location);
5750
5751 is_member_access = true;
5752 break;
5753
5754 case CPP_PLUS_PLUS:
5755 /* postfix-expression ++ */
5756 /* Consume the `++' token. */
5757 cp_lexer_consume_token (parser->lexer);
5758 /* Generate a representation for the complete expression. */
5759 postfix_expression
5760 = finish_increment_expr (postfix_expression,
5761 POSTINCREMENT_EXPR);
5762 /* Increments may not appear in constant-expressions. */
5763 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5764 postfix_expression = error_mark_node;
5765 idk = CP_ID_KIND_NONE;
5766 is_member_access = false;
5767 break;
5768
5769 case CPP_MINUS_MINUS:
5770 /* postfix-expression -- */
5771 /* Consume the `--' token. */
5772 cp_lexer_consume_token (parser->lexer);
5773 /* Generate a representation for the complete expression. */
5774 postfix_expression
5775 = finish_increment_expr (postfix_expression,
5776 POSTDECREMENT_EXPR);
5777 /* Decrements may not appear in constant-expressions. */
5778 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5779 postfix_expression = error_mark_node;
5780 idk = CP_ID_KIND_NONE;
5781 is_member_access = false;
5782 break;
5783
5784 default:
5785 if (pidk_return != NULL)
5786 * pidk_return = idk;
5787 if (member_access_only_p)
5788 return is_member_access? postfix_expression : error_mark_node;
5789 else
5790 return postfix_expression;
5791 }
5792 }
5793
5794 /* We should never get here. */
5795 gcc_unreachable ();
5796 return error_mark_node;
5797 }
5798
5799 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5800 by cp_parser_builtin_offsetof. We're looking for
5801
5802 postfix-expression [ expression ]
5803
5804 FOR_OFFSETOF is set if we're being called in that context, which
5805 changes how we deal with integer constant expressions. */
5806
5807 static tree
5808 cp_parser_postfix_open_square_expression (cp_parser *parser,
5809 tree postfix_expression,
5810 bool for_offsetof)
5811 {
5812 tree index;
5813
5814 /* Consume the `[' token. */
5815 cp_lexer_consume_token (parser->lexer);
5816
5817 /* Parse the index expression. */
5818 /* ??? For offsetof, there is a question of what to allow here. If
5819 offsetof is not being used in an integral constant expression context,
5820 then we *could* get the right answer by computing the value at runtime.
5821 If we are in an integral constant expression context, then we might
5822 could accept any constant expression; hard to say without analysis.
5823 Rather than open the barn door too wide right away, allow only integer
5824 constant expressions here. */
5825 if (for_offsetof)
5826 index = cp_parser_constant_expression (parser, false, NULL);
5827 else
5828 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5829
5830 /* Look for the closing `]'. */
5831 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5832
5833 /* Build the ARRAY_REF. */
5834 postfix_expression = grok_array_decl (postfix_expression, index);
5835
5836 /* When not doing offsetof, array references are not permitted in
5837 constant-expressions. */
5838 if (!for_offsetof
5839 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5840 postfix_expression = error_mark_node;
5841
5842 return postfix_expression;
5843 }
5844
5845 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5846 by cp_parser_builtin_offsetof. We're looking for
5847
5848 postfix-expression . template [opt] id-expression
5849 postfix-expression . pseudo-destructor-name
5850 postfix-expression -> template [opt] id-expression
5851 postfix-expression -> pseudo-destructor-name
5852
5853 FOR_OFFSETOF is set if we're being called in that context. That sorta
5854 limits what of the above we'll actually accept, but nevermind.
5855 TOKEN_TYPE is the "." or "->" token, which will already have been
5856 removed from the stream. */
5857
5858 static tree
5859 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5860 enum cpp_ttype token_type,
5861 tree postfix_expression,
5862 bool for_offsetof, cp_id_kind *idk,
5863 location_t location)
5864 {
5865 tree name;
5866 bool dependent_p;
5867 bool pseudo_destructor_p;
5868 tree scope = NULL_TREE;
5869
5870 /* If this is a `->' operator, dereference the pointer. */
5871 if (token_type == CPP_DEREF)
5872 postfix_expression = build_x_arrow (postfix_expression);
5873 /* Check to see whether or not the expression is type-dependent. */
5874 dependent_p = type_dependent_expression_p (postfix_expression);
5875 /* The identifier following the `->' or `.' is not qualified. */
5876 parser->scope = NULL_TREE;
5877 parser->qualifying_scope = NULL_TREE;
5878 parser->object_scope = NULL_TREE;
5879 *idk = CP_ID_KIND_NONE;
5880
5881 /* Enter the scope corresponding to the type of the object
5882 given by the POSTFIX_EXPRESSION. */
5883 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5884 {
5885 scope = TREE_TYPE (postfix_expression);
5886 /* According to the standard, no expression should ever have
5887 reference type. Unfortunately, we do not currently match
5888 the standard in this respect in that our internal representation
5889 of an expression may have reference type even when the standard
5890 says it does not. Therefore, we have to manually obtain the
5891 underlying type here. */
5892 scope = non_reference (scope);
5893 /* The type of the POSTFIX_EXPRESSION must be complete. */
5894 if (scope == unknown_type_node)
5895 {
5896 error_at (location, "%qE does not have class type",
5897 postfix_expression);
5898 scope = NULL_TREE;
5899 }
5900 /* Unlike the object expression in other contexts, *this is not
5901 required to be of complete type for purposes of class member
5902 access (5.2.5) outside the member function body. */
5903 else if (scope != current_class_ref
5904 && !(processing_template_decl && scope == current_class_type))
5905 scope = complete_type_or_else (scope, NULL_TREE);
5906 /* Let the name lookup machinery know that we are processing a
5907 class member access expression. */
5908 parser->context->object_type = scope;
5909 /* If something went wrong, we want to be able to discern that case,
5910 as opposed to the case where there was no SCOPE due to the type
5911 of expression being dependent. */
5912 if (!scope)
5913 scope = error_mark_node;
5914 /* If the SCOPE was erroneous, make the various semantic analysis
5915 functions exit quickly -- and without issuing additional error
5916 messages. */
5917 if (scope == error_mark_node)
5918 postfix_expression = error_mark_node;
5919 }
5920
5921 /* Assume this expression is not a pseudo-destructor access. */
5922 pseudo_destructor_p = false;
5923
5924 /* If the SCOPE is a scalar type, then, if this is a valid program,
5925 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5926 is type dependent, it can be pseudo-destructor-name or something else.
5927 Try to parse it as pseudo-destructor-name first. */
5928 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5929 {
5930 tree s;
5931 tree type;
5932
5933 cp_parser_parse_tentatively (parser);
5934 /* Parse the pseudo-destructor-name. */
5935 s = NULL_TREE;
5936 cp_parser_pseudo_destructor_name (parser, &s, &type);
5937 if (dependent_p
5938 && (cp_parser_error_occurred (parser)
5939 || TREE_CODE (type) != TYPE_DECL
5940 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5941 cp_parser_abort_tentative_parse (parser);
5942 else if (cp_parser_parse_definitely (parser))
5943 {
5944 pseudo_destructor_p = true;
5945 postfix_expression
5946 = finish_pseudo_destructor_expr (postfix_expression,
5947 s, TREE_TYPE (type));
5948 }
5949 }
5950
5951 if (!pseudo_destructor_p)
5952 {
5953 /* If the SCOPE is not a scalar type, we are looking at an
5954 ordinary class member access expression, rather than a
5955 pseudo-destructor-name. */
5956 bool template_p;
5957 cp_token *token = cp_lexer_peek_token (parser->lexer);
5958 /* Parse the id-expression. */
5959 name = (cp_parser_id_expression
5960 (parser,
5961 cp_parser_optional_template_keyword (parser),
5962 /*check_dependency_p=*/true,
5963 &template_p,
5964 /*declarator_p=*/false,
5965 /*optional_p=*/false));
5966 /* In general, build a SCOPE_REF if the member name is qualified.
5967 However, if the name was not dependent and has already been
5968 resolved; there is no need to build the SCOPE_REF. For example;
5969
5970 struct X { void f(); };
5971 template <typename T> void f(T* t) { t->X::f(); }
5972
5973 Even though "t" is dependent, "X::f" is not and has been resolved
5974 to a BASELINK; there is no need to include scope information. */
5975
5976 /* But we do need to remember that there was an explicit scope for
5977 virtual function calls. */
5978 if (parser->scope)
5979 *idk = CP_ID_KIND_QUALIFIED;
5980
5981 /* If the name is a template-id that names a type, we will get a
5982 TYPE_DECL here. That is invalid code. */
5983 if (TREE_CODE (name) == TYPE_DECL)
5984 {
5985 error_at (token->location, "invalid use of %qD", name);
5986 postfix_expression = error_mark_node;
5987 }
5988 else
5989 {
5990 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5991 {
5992 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
5993 {
5994 error_at (token->location, "%<%D::%D%> is not a class member",
5995 parser->scope, name);
5996 postfix_expression = error_mark_node;
5997 }
5998 else
5999 name = build_qualified_name (/*type=*/NULL_TREE,
6000 parser->scope,
6001 name,
6002 template_p);
6003 parser->scope = NULL_TREE;
6004 parser->qualifying_scope = NULL_TREE;
6005 parser->object_scope = NULL_TREE;
6006 }
6007 if (scope && name && BASELINK_P (name))
6008 adjust_result_of_qualified_name_lookup
6009 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
6010 postfix_expression
6011 = finish_class_member_access_expr (postfix_expression, name,
6012 template_p,
6013 tf_warning_or_error);
6014 }
6015 }
6016
6017 /* We no longer need to look up names in the scope of the object on
6018 the left-hand side of the `.' or `->' operator. */
6019 parser->context->object_type = NULL_TREE;
6020
6021 /* Outside of offsetof, these operators may not appear in
6022 constant-expressions. */
6023 if (!for_offsetof
6024 && (cp_parser_non_integral_constant_expression
6025 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6026 postfix_expression = error_mark_node;
6027
6028 return postfix_expression;
6029 }
6030
6031 /* Parse a parenthesized expression-list.
6032
6033 expression-list:
6034 assignment-expression
6035 expression-list, assignment-expression
6036
6037 attribute-list:
6038 expression-list
6039 identifier
6040 identifier, expression-list
6041
6042 CAST_P is true if this expression is the target of a cast.
6043
6044 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6045 argument pack.
6046
6047 Returns a vector of trees. Each element is a representation of an
6048 assignment-expression. NULL is returned if the ( and or ) are
6049 missing. An empty, but allocated, vector is returned on no
6050 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6051 if we are parsing an attribute list for an attribute that wants a
6052 plain identifier argument, normal_attr for an attribute that wants
6053 an expression, or non_attr if we aren't parsing an attribute list. If
6054 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6055 not all of the expressions in the list were constant. */
6056
6057 static VEC(tree,gc) *
6058 cp_parser_parenthesized_expression_list (cp_parser* parser,
6059 int is_attribute_list,
6060 bool cast_p,
6061 bool allow_expansion_p,
6062 bool *non_constant_p)
6063 {
6064 VEC(tree,gc) *expression_list;
6065 bool fold_expr_p = is_attribute_list != non_attr;
6066 tree identifier = NULL_TREE;
6067 bool saved_greater_than_is_operator_p;
6068
6069 /* Assume all the expressions will be constant. */
6070 if (non_constant_p)
6071 *non_constant_p = false;
6072
6073 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6074 return NULL;
6075
6076 expression_list = make_tree_vector ();
6077
6078 /* Within a parenthesized expression, a `>' token is always
6079 the greater-than operator. */
6080 saved_greater_than_is_operator_p
6081 = parser->greater_than_is_operator_p;
6082 parser->greater_than_is_operator_p = true;
6083
6084 /* Consume expressions until there are no more. */
6085 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6086 while (true)
6087 {
6088 tree expr;
6089
6090 /* At the beginning of attribute lists, check to see if the
6091 next token is an identifier. */
6092 if (is_attribute_list == id_attr
6093 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6094 {
6095 cp_token *token;
6096
6097 /* Consume the identifier. */
6098 token = cp_lexer_consume_token (parser->lexer);
6099 /* Save the identifier. */
6100 identifier = token->u.value;
6101 }
6102 else
6103 {
6104 bool expr_non_constant_p;
6105
6106 /* Parse the next assignment-expression. */
6107 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6108 {
6109 /* A braced-init-list. */
6110 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6111 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6112 if (non_constant_p && expr_non_constant_p)
6113 *non_constant_p = true;
6114 }
6115 else if (non_constant_p)
6116 {
6117 expr = (cp_parser_constant_expression
6118 (parser, /*allow_non_constant_p=*/true,
6119 &expr_non_constant_p));
6120 if (expr_non_constant_p)
6121 *non_constant_p = true;
6122 }
6123 else
6124 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6125
6126 if (fold_expr_p)
6127 expr = fold_non_dependent_expr (expr);
6128
6129 /* If we have an ellipsis, then this is an expression
6130 expansion. */
6131 if (allow_expansion_p
6132 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6133 {
6134 /* Consume the `...'. */
6135 cp_lexer_consume_token (parser->lexer);
6136
6137 /* Build the argument pack. */
6138 expr = make_pack_expansion (expr);
6139 }
6140
6141 /* Add it to the list. We add error_mark_node
6142 expressions to the list, so that we can still tell if
6143 the correct form for a parenthesized expression-list
6144 is found. That gives better errors. */
6145 VEC_safe_push (tree, gc, expression_list, expr);
6146
6147 if (expr == error_mark_node)
6148 goto skip_comma;
6149 }
6150
6151 /* After the first item, attribute lists look the same as
6152 expression lists. */
6153 is_attribute_list = non_attr;
6154
6155 get_comma:;
6156 /* If the next token isn't a `,', then we are done. */
6157 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6158 break;
6159
6160 /* Otherwise, consume the `,' and keep going. */
6161 cp_lexer_consume_token (parser->lexer);
6162 }
6163
6164 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6165 {
6166 int ending;
6167
6168 skip_comma:;
6169 /* We try and resync to an unnested comma, as that will give the
6170 user better diagnostics. */
6171 ending = cp_parser_skip_to_closing_parenthesis (parser,
6172 /*recovering=*/true,
6173 /*or_comma=*/true,
6174 /*consume_paren=*/true);
6175 if (ending < 0)
6176 goto get_comma;
6177 if (!ending)
6178 {
6179 parser->greater_than_is_operator_p
6180 = saved_greater_than_is_operator_p;
6181 return NULL;
6182 }
6183 }
6184
6185 parser->greater_than_is_operator_p
6186 = saved_greater_than_is_operator_p;
6187
6188 if (identifier)
6189 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6190
6191 return expression_list;
6192 }
6193
6194 /* Parse a pseudo-destructor-name.
6195
6196 pseudo-destructor-name:
6197 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6198 :: [opt] nested-name-specifier template template-id :: ~ type-name
6199 :: [opt] nested-name-specifier [opt] ~ type-name
6200
6201 If either of the first two productions is used, sets *SCOPE to the
6202 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6203 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6204 or ERROR_MARK_NODE if the parse fails. */
6205
6206 static void
6207 cp_parser_pseudo_destructor_name (cp_parser* parser,
6208 tree* scope,
6209 tree* type)
6210 {
6211 bool nested_name_specifier_p;
6212
6213 /* Assume that things will not work out. */
6214 *type = error_mark_node;
6215
6216 /* Look for the optional `::' operator. */
6217 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6218 /* Look for the optional nested-name-specifier. */
6219 nested_name_specifier_p
6220 = (cp_parser_nested_name_specifier_opt (parser,
6221 /*typename_keyword_p=*/false,
6222 /*check_dependency_p=*/true,
6223 /*type_p=*/false,
6224 /*is_declaration=*/false)
6225 != NULL_TREE);
6226 /* Now, if we saw a nested-name-specifier, we might be doing the
6227 second production. */
6228 if (nested_name_specifier_p
6229 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6230 {
6231 /* Consume the `template' keyword. */
6232 cp_lexer_consume_token (parser->lexer);
6233 /* Parse the template-id. */
6234 cp_parser_template_id (parser,
6235 /*template_keyword_p=*/true,
6236 /*check_dependency_p=*/false,
6237 /*is_declaration=*/true);
6238 /* Look for the `::' token. */
6239 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6240 }
6241 /* If the next token is not a `~', then there might be some
6242 additional qualification. */
6243 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6244 {
6245 /* At this point, we're looking for "type-name :: ~". The type-name
6246 must not be a class-name, since this is a pseudo-destructor. So,
6247 it must be either an enum-name, or a typedef-name -- both of which
6248 are just identifiers. So, we peek ahead to check that the "::"
6249 and "~" tokens are present; if they are not, then we can avoid
6250 calling type_name. */
6251 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6252 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6253 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6254 {
6255 cp_parser_error (parser, "non-scalar type");
6256 return;
6257 }
6258
6259 /* Look for the type-name. */
6260 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6261 if (*scope == error_mark_node)
6262 return;
6263
6264 /* Look for the `::' token. */
6265 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6266 }
6267 else
6268 *scope = NULL_TREE;
6269
6270 /* Look for the `~'. */
6271 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6272
6273 /* Once we see the ~, this has to be a pseudo-destructor. */
6274 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6275 cp_parser_commit_to_tentative_parse (parser);
6276
6277 /* Look for the type-name again. We are not responsible for
6278 checking that it matches the first type-name. */
6279 *type = cp_parser_nonclass_name (parser);
6280 }
6281
6282 /* Parse a unary-expression.
6283
6284 unary-expression:
6285 postfix-expression
6286 ++ cast-expression
6287 -- cast-expression
6288 unary-operator cast-expression
6289 sizeof unary-expression
6290 sizeof ( type-id )
6291 alignof ( type-id ) [C++0x]
6292 new-expression
6293 delete-expression
6294
6295 GNU Extensions:
6296
6297 unary-expression:
6298 __extension__ cast-expression
6299 __alignof__ unary-expression
6300 __alignof__ ( type-id )
6301 alignof unary-expression [C++0x]
6302 __real__ cast-expression
6303 __imag__ cast-expression
6304 && identifier
6305
6306 ADDRESS_P is true iff the unary-expression is appearing as the
6307 operand of the `&' operator. CAST_P is true if this expression is
6308 the target of a cast.
6309
6310 Returns a representation of the expression. */
6311
6312 static tree
6313 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6314 cp_id_kind * pidk)
6315 {
6316 cp_token *token;
6317 enum tree_code unary_operator;
6318
6319 /* Peek at the next token. */
6320 token = cp_lexer_peek_token (parser->lexer);
6321 /* Some keywords give away the kind of expression. */
6322 if (token->type == CPP_KEYWORD)
6323 {
6324 enum rid keyword = token->keyword;
6325
6326 switch (keyword)
6327 {
6328 case RID_ALIGNOF:
6329 case RID_SIZEOF:
6330 {
6331 tree operand;
6332 enum tree_code op;
6333
6334 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6335 /* Consume the token. */
6336 cp_lexer_consume_token (parser->lexer);
6337 /* Parse the operand. */
6338 operand = cp_parser_sizeof_operand (parser, keyword);
6339
6340 if (TYPE_P (operand))
6341 return cxx_sizeof_or_alignof_type (operand, op, true);
6342 else
6343 {
6344 /* ISO C++ defines alignof only with types, not with
6345 expressions. So pedwarn if alignof is used with a non-
6346 type expression. However, __alignof__ is ok. */
6347 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6348 pedwarn (token->location, OPT_pedantic,
6349 "ISO C++ does not allow %<alignof%> "
6350 "with a non-type");
6351
6352 return cxx_sizeof_or_alignof_expr (operand, op, true);
6353 }
6354 }
6355
6356 case RID_NEW:
6357 return cp_parser_new_expression (parser);
6358
6359 case RID_DELETE:
6360 return cp_parser_delete_expression (parser);
6361
6362 case RID_EXTENSION:
6363 {
6364 /* The saved value of the PEDANTIC flag. */
6365 int saved_pedantic;
6366 tree expr;
6367
6368 /* Save away the PEDANTIC flag. */
6369 cp_parser_extension_opt (parser, &saved_pedantic);
6370 /* Parse the cast-expression. */
6371 expr = cp_parser_simple_cast_expression (parser);
6372 /* Restore the PEDANTIC flag. */
6373 pedantic = saved_pedantic;
6374
6375 return expr;
6376 }
6377
6378 case RID_REALPART:
6379 case RID_IMAGPART:
6380 {
6381 tree expression;
6382
6383 /* Consume the `__real__' or `__imag__' token. */
6384 cp_lexer_consume_token (parser->lexer);
6385 /* Parse the cast-expression. */
6386 expression = cp_parser_simple_cast_expression (parser);
6387 /* Create the complete representation. */
6388 return build_x_unary_op ((keyword == RID_REALPART
6389 ? REALPART_EXPR : IMAGPART_EXPR),
6390 expression,
6391 tf_warning_or_error);
6392 }
6393 break;
6394
6395 case RID_TRANSACTION_ATOMIC:
6396 case RID_TRANSACTION_RELAXED:
6397 return cp_parser_transaction_expression (parser, keyword);
6398
6399 case RID_NOEXCEPT:
6400 {
6401 tree expr;
6402 const char *saved_message;
6403 bool saved_integral_constant_expression_p;
6404 bool saved_non_integral_constant_expression_p;
6405 bool saved_greater_than_is_operator_p;
6406
6407 cp_lexer_consume_token (parser->lexer);
6408 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6409
6410 saved_message = parser->type_definition_forbidden_message;
6411 parser->type_definition_forbidden_message
6412 = G_("types may not be defined in %<noexcept%> expressions");
6413
6414 saved_integral_constant_expression_p
6415 = parser->integral_constant_expression_p;
6416 saved_non_integral_constant_expression_p
6417 = parser->non_integral_constant_expression_p;
6418 parser->integral_constant_expression_p = false;
6419
6420 saved_greater_than_is_operator_p
6421 = parser->greater_than_is_operator_p;
6422 parser->greater_than_is_operator_p = true;
6423
6424 ++cp_unevaluated_operand;
6425 ++c_inhibit_evaluation_warnings;
6426 expr = cp_parser_expression (parser, false, NULL);
6427 --c_inhibit_evaluation_warnings;
6428 --cp_unevaluated_operand;
6429
6430 parser->greater_than_is_operator_p
6431 = saved_greater_than_is_operator_p;
6432
6433 parser->integral_constant_expression_p
6434 = saved_integral_constant_expression_p;
6435 parser->non_integral_constant_expression_p
6436 = saved_non_integral_constant_expression_p;
6437
6438 parser->type_definition_forbidden_message = saved_message;
6439
6440 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6441 return finish_noexcept_expr (expr, tf_warning_or_error);
6442 }
6443
6444 default:
6445 break;
6446 }
6447 }
6448
6449 /* Look for the `:: new' and `:: delete', which also signal the
6450 beginning of a new-expression, or delete-expression,
6451 respectively. If the next token is `::', then it might be one of
6452 these. */
6453 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6454 {
6455 enum rid keyword;
6456
6457 /* See if the token after the `::' is one of the keywords in
6458 which we're interested. */
6459 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6460 /* If it's `new', we have a new-expression. */
6461 if (keyword == RID_NEW)
6462 return cp_parser_new_expression (parser);
6463 /* Similarly, for `delete'. */
6464 else if (keyword == RID_DELETE)
6465 return cp_parser_delete_expression (parser);
6466 }
6467
6468 /* Look for a unary operator. */
6469 unary_operator = cp_parser_unary_operator (token);
6470 /* The `++' and `--' operators can be handled similarly, even though
6471 they are not technically unary-operators in the grammar. */
6472 if (unary_operator == ERROR_MARK)
6473 {
6474 if (token->type == CPP_PLUS_PLUS)
6475 unary_operator = PREINCREMENT_EXPR;
6476 else if (token->type == CPP_MINUS_MINUS)
6477 unary_operator = PREDECREMENT_EXPR;
6478 /* Handle the GNU address-of-label extension. */
6479 else if (cp_parser_allow_gnu_extensions_p (parser)
6480 && token->type == CPP_AND_AND)
6481 {
6482 tree identifier;
6483 tree expression;
6484 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6485
6486 /* Consume the '&&' token. */
6487 cp_lexer_consume_token (parser->lexer);
6488 /* Look for the identifier. */
6489 identifier = cp_parser_identifier (parser);
6490 /* Create an expression representing the address. */
6491 expression = finish_label_address_expr (identifier, loc);
6492 if (cp_parser_non_integral_constant_expression (parser,
6493 NIC_ADDR_LABEL))
6494 expression = error_mark_node;
6495 return expression;
6496 }
6497 }
6498 if (unary_operator != ERROR_MARK)
6499 {
6500 tree cast_expression;
6501 tree expression = error_mark_node;
6502 non_integral_constant non_constant_p = NIC_NONE;
6503
6504 /* Consume the operator token. */
6505 token = cp_lexer_consume_token (parser->lexer);
6506 /* Parse the cast-expression. */
6507 cast_expression
6508 = cp_parser_cast_expression (parser,
6509 unary_operator == ADDR_EXPR,
6510 /*cast_p=*/false, pidk);
6511 /* Now, build an appropriate representation. */
6512 switch (unary_operator)
6513 {
6514 case INDIRECT_REF:
6515 non_constant_p = NIC_STAR;
6516 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6517 tf_warning_or_error);
6518 break;
6519
6520 case ADDR_EXPR:
6521 non_constant_p = NIC_ADDR;
6522 /* Fall through. */
6523 case BIT_NOT_EXPR:
6524 expression = build_x_unary_op (unary_operator, cast_expression,
6525 tf_warning_or_error);
6526 break;
6527
6528 case PREINCREMENT_EXPR:
6529 case PREDECREMENT_EXPR:
6530 non_constant_p = unary_operator == PREINCREMENT_EXPR
6531 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6532 /* Fall through. */
6533 case UNARY_PLUS_EXPR:
6534 case NEGATE_EXPR:
6535 case TRUTH_NOT_EXPR:
6536 expression = finish_unary_op_expr (unary_operator, cast_expression);
6537 break;
6538
6539 default:
6540 gcc_unreachable ();
6541 }
6542
6543 if (non_constant_p != NIC_NONE
6544 && cp_parser_non_integral_constant_expression (parser,
6545 non_constant_p))
6546 expression = error_mark_node;
6547
6548 return expression;
6549 }
6550
6551 return cp_parser_postfix_expression (parser, address_p, cast_p,
6552 /*member_access_only_p=*/false,
6553 pidk);
6554 }
6555
6556 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6557 unary-operator, the corresponding tree code is returned. */
6558
6559 static enum tree_code
6560 cp_parser_unary_operator (cp_token* token)
6561 {
6562 switch (token->type)
6563 {
6564 case CPP_MULT:
6565 return INDIRECT_REF;
6566
6567 case CPP_AND:
6568 return ADDR_EXPR;
6569
6570 case CPP_PLUS:
6571 return UNARY_PLUS_EXPR;
6572
6573 case CPP_MINUS:
6574 return NEGATE_EXPR;
6575
6576 case CPP_NOT:
6577 return TRUTH_NOT_EXPR;
6578
6579 case CPP_COMPL:
6580 return BIT_NOT_EXPR;
6581
6582 default:
6583 return ERROR_MARK;
6584 }
6585 }
6586
6587 /* Parse a new-expression.
6588
6589 new-expression:
6590 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6591 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6592
6593 Returns a representation of the expression. */
6594
6595 static tree
6596 cp_parser_new_expression (cp_parser* parser)
6597 {
6598 bool global_scope_p;
6599 VEC(tree,gc) *placement;
6600 tree type;
6601 VEC(tree,gc) *initializer;
6602 tree nelts;
6603 tree ret;
6604
6605 /* Look for the optional `::' operator. */
6606 global_scope_p
6607 = (cp_parser_global_scope_opt (parser,
6608 /*current_scope_valid_p=*/false)
6609 != NULL_TREE);
6610 /* Look for the `new' operator. */
6611 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6612 /* There's no easy way to tell a new-placement from the
6613 `( type-id )' construct. */
6614 cp_parser_parse_tentatively (parser);
6615 /* Look for a new-placement. */
6616 placement = cp_parser_new_placement (parser);
6617 /* If that didn't work out, there's no new-placement. */
6618 if (!cp_parser_parse_definitely (parser))
6619 {
6620 if (placement != NULL)
6621 release_tree_vector (placement);
6622 placement = NULL;
6623 }
6624
6625 /* If the next token is a `(', then we have a parenthesized
6626 type-id. */
6627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6628 {
6629 cp_token *token;
6630 /* Consume the `('. */
6631 cp_lexer_consume_token (parser->lexer);
6632 /* Parse the type-id. */
6633 type = cp_parser_type_id (parser);
6634 /* Look for the closing `)'. */
6635 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6636 token = cp_lexer_peek_token (parser->lexer);
6637 /* There should not be a direct-new-declarator in this production,
6638 but GCC used to allowed this, so we check and emit a sensible error
6639 message for this case. */
6640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6641 {
6642 error_at (token->location,
6643 "array bound forbidden after parenthesized type-id");
6644 inform (token->location,
6645 "try removing the parentheses around the type-id");
6646 cp_parser_direct_new_declarator (parser);
6647 }
6648 nelts = NULL_TREE;
6649 }
6650 /* Otherwise, there must be a new-type-id. */
6651 else
6652 type = cp_parser_new_type_id (parser, &nelts);
6653
6654 /* If the next token is a `(' or '{', then we have a new-initializer. */
6655 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6656 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6657 initializer = cp_parser_new_initializer (parser);
6658 else
6659 initializer = NULL;
6660
6661 /* A new-expression may not appear in an integral constant
6662 expression. */
6663 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6664 ret = error_mark_node;
6665 else
6666 {
6667 /* Create a representation of the new-expression. */
6668 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6669 tf_warning_or_error);
6670 }
6671
6672 if (placement != NULL)
6673 release_tree_vector (placement);
6674 if (initializer != NULL)
6675 release_tree_vector (initializer);
6676
6677 return ret;
6678 }
6679
6680 /* Parse a new-placement.
6681
6682 new-placement:
6683 ( expression-list )
6684
6685 Returns the same representation as for an expression-list. */
6686
6687 static VEC(tree,gc) *
6688 cp_parser_new_placement (cp_parser* parser)
6689 {
6690 VEC(tree,gc) *expression_list;
6691
6692 /* Parse the expression-list. */
6693 expression_list = (cp_parser_parenthesized_expression_list
6694 (parser, non_attr, /*cast_p=*/false,
6695 /*allow_expansion_p=*/true,
6696 /*non_constant_p=*/NULL));
6697
6698 return expression_list;
6699 }
6700
6701 /* Parse a new-type-id.
6702
6703 new-type-id:
6704 type-specifier-seq new-declarator [opt]
6705
6706 Returns the TYPE allocated. If the new-type-id indicates an array
6707 type, *NELTS is set to the number of elements in the last array
6708 bound; the TYPE will not include the last array bound. */
6709
6710 static tree
6711 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6712 {
6713 cp_decl_specifier_seq type_specifier_seq;
6714 cp_declarator *new_declarator;
6715 cp_declarator *declarator;
6716 cp_declarator *outer_declarator;
6717 const char *saved_message;
6718 tree type;
6719
6720 /* The type-specifier sequence must not contain type definitions.
6721 (It cannot contain declarations of new types either, but if they
6722 are not definitions we will catch that because they are not
6723 complete.) */
6724 saved_message = parser->type_definition_forbidden_message;
6725 parser->type_definition_forbidden_message
6726 = G_("types may not be defined in a new-type-id");
6727 /* Parse the type-specifier-seq. */
6728 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6729 /*is_trailing_return=*/false,
6730 &type_specifier_seq);
6731 /* Restore the old message. */
6732 parser->type_definition_forbidden_message = saved_message;
6733 /* Parse the new-declarator. */
6734 new_declarator = cp_parser_new_declarator_opt (parser);
6735
6736 /* Determine the number of elements in the last array dimension, if
6737 any. */
6738 *nelts = NULL_TREE;
6739 /* Skip down to the last array dimension. */
6740 declarator = new_declarator;
6741 outer_declarator = NULL;
6742 while (declarator && (declarator->kind == cdk_pointer
6743 || declarator->kind == cdk_ptrmem))
6744 {
6745 outer_declarator = declarator;
6746 declarator = declarator->declarator;
6747 }
6748 while (declarator
6749 && declarator->kind == cdk_array
6750 && declarator->declarator
6751 && declarator->declarator->kind == cdk_array)
6752 {
6753 outer_declarator = declarator;
6754 declarator = declarator->declarator;
6755 }
6756
6757 if (declarator && declarator->kind == cdk_array)
6758 {
6759 *nelts = declarator->u.array.bounds;
6760 if (*nelts == error_mark_node)
6761 *nelts = integer_one_node;
6762
6763 if (outer_declarator)
6764 outer_declarator->declarator = declarator->declarator;
6765 else
6766 new_declarator = NULL;
6767 }
6768
6769 type = groktypename (&type_specifier_seq, new_declarator, false);
6770 return type;
6771 }
6772
6773 /* Parse an (optional) new-declarator.
6774
6775 new-declarator:
6776 ptr-operator new-declarator [opt]
6777 direct-new-declarator
6778
6779 Returns the declarator. */
6780
6781 static cp_declarator *
6782 cp_parser_new_declarator_opt (cp_parser* parser)
6783 {
6784 enum tree_code code;
6785 tree type;
6786 cp_cv_quals cv_quals;
6787
6788 /* We don't know if there's a ptr-operator next, or not. */
6789 cp_parser_parse_tentatively (parser);
6790 /* Look for a ptr-operator. */
6791 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6792 /* If that worked, look for more new-declarators. */
6793 if (cp_parser_parse_definitely (parser))
6794 {
6795 cp_declarator *declarator;
6796
6797 /* Parse another optional declarator. */
6798 declarator = cp_parser_new_declarator_opt (parser);
6799
6800 return cp_parser_make_indirect_declarator
6801 (code, type, cv_quals, declarator);
6802 }
6803
6804 /* If the next token is a `[', there is a direct-new-declarator. */
6805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6806 return cp_parser_direct_new_declarator (parser);
6807
6808 return NULL;
6809 }
6810
6811 /* Parse a direct-new-declarator.
6812
6813 direct-new-declarator:
6814 [ expression ]
6815 direct-new-declarator [constant-expression]
6816
6817 */
6818
6819 static cp_declarator *
6820 cp_parser_direct_new_declarator (cp_parser* parser)
6821 {
6822 cp_declarator *declarator = NULL;
6823
6824 while (true)
6825 {
6826 tree expression;
6827
6828 /* Look for the opening `['. */
6829 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6830 /* The first expression is not required to be constant. */
6831 if (!declarator)
6832 {
6833 cp_token *token = cp_lexer_peek_token (parser->lexer);
6834 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6835 /* The standard requires that the expression have integral
6836 type. DR 74 adds enumeration types. We believe that the
6837 real intent is that these expressions be handled like the
6838 expression in a `switch' condition, which also allows
6839 classes with a single conversion to integral or
6840 enumeration type. */
6841 if (!processing_template_decl)
6842 {
6843 expression
6844 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6845 expression,
6846 /*complain=*/true);
6847 if (!expression)
6848 {
6849 error_at (token->location,
6850 "expression in new-declarator must have integral "
6851 "or enumeration type");
6852 expression = error_mark_node;
6853 }
6854 }
6855 }
6856 /* But all the other expressions must be. */
6857 else
6858 expression
6859 = cp_parser_constant_expression (parser,
6860 /*allow_non_constant=*/false,
6861 NULL);
6862 /* Look for the closing `]'. */
6863 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6864
6865 /* Add this bound to the declarator. */
6866 declarator = make_array_declarator (declarator, expression);
6867
6868 /* If the next token is not a `[', then there are no more
6869 bounds. */
6870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6871 break;
6872 }
6873
6874 return declarator;
6875 }
6876
6877 /* Parse a new-initializer.
6878
6879 new-initializer:
6880 ( expression-list [opt] )
6881 braced-init-list
6882
6883 Returns a representation of the expression-list. */
6884
6885 static VEC(tree,gc) *
6886 cp_parser_new_initializer (cp_parser* parser)
6887 {
6888 VEC(tree,gc) *expression_list;
6889
6890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6891 {
6892 tree t;
6893 bool expr_non_constant_p;
6894 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6895 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6896 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6897 expression_list = make_tree_vector_single (t);
6898 }
6899 else
6900 expression_list = (cp_parser_parenthesized_expression_list
6901 (parser, non_attr, /*cast_p=*/false,
6902 /*allow_expansion_p=*/true,
6903 /*non_constant_p=*/NULL));
6904
6905 return expression_list;
6906 }
6907
6908 /* Parse a delete-expression.
6909
6910 delete-expression:
6911 :: [opt] delete cast-expression
6912 :: [opt] delete [ ] cast-expression
6913
6914 Returns a representation of the expression. */
6915
6916 static tree
6917 cp_parser_delete_expression (cp_parser* parser)
6918 {
6919 bool global_scope_p;
6920 bool array_p;
6921 tree expression;
6922
6923 /* Look for the optional `::' operator. */
6924 global_scope_p
6925 = (cp_parser_global_scope_opt (parser,
6926 /*current_scope_valid_p=*/false)
6927 != NULL_TREE);
6928 /* Look for the `delete' keyword. */
6929 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6930 /* See if the array syntax is in use. */
6931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6932 {
6933 /* Consume the `[' token. */
6934 cp_lexer_consume_token (parser->lexer);
6935 /* Look for the `]' token. */
6936 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6937 /* Remember that this is the `[]' construct. */
6938 array_p = true;
6939 }
6940 else
6941 array_p = false;
6942
6943 /* Parse the cast-expression. */
6944 expression = cp_parser_simple_cast_expression (parser);
6945
6946 /* A delete-expression may not appear in an integral constant
6947 expression. */
6948 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6949 return error_mark_node;
6950
6951 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6952 tf_warning_or_error);
6953 }
6954
6955 /* Returns true if TOKEN may start a cast-expression and false
6956 otherwise. */
6957
6958 static bool
6959 cp_parser_token_starts_cast_expression (cp_token *token)
6960 {
6961 switch (token->type)
6962 {
6963 case CPP_COMMA:
6964 case CPP_SEMICOLON:
6965 case CPP_QUERY:
6966 case CPP_COLON:
6967 case CPP_CLOSE_SQUARE:
6968 case CPP_CLOSE_PAREN:
6969 case CPP_CLOSE_BRACE:
6970 case CPP_DOT:
6971 case CPP_DOT_STAR:
6972 case CPP_DEREF:
6973 case CPP_DEREF_STAR:
6974 case CPP_DIV:
6975 case CPP_MOD:
6976 case CPP_LSHIFT:
6977 case CPP_RSHIFT:
6978 case CPP_LESS:
6979 case CPP_GREATER:
6980 case CPP_LESS_EQ:
6981 case CPP_GREATER_EQ:
6982 case CPP_EQ_EQ:
6983 case CPP_NOT_EQ:
6984 case CPP_EQ:
6985 case CPP_MULT_EQ:
6986 case CPP_DIV_EQ:
6987 case CPP_MOD_EQ:
6988 case CPP_PLUS_EQ:
6989 case CPP_MINUS_EQ:
6990 case CPP_RSHIFT_EQ:
6991 case CPP_LSHIFT_EQ:
6992 case CPP_AND_EQ:
6993 case CPP_XOR_EQ:
6994 case CPP_OR_EQ:
6995 case CPP_XOR:
6996 case CPP_OR:
6997 case CPP_OR_OR:
6998 case CPP_EOF:
6999 return false;
7000
7001 /* '[' may start a primary-expression in obj-c++. */
7002 case CPP_OPEN_SQUARE:
7003 return c_dialect_objc ();
7004
7005 default:
7006 return true;
7007 }
7008 }
7009
7010 /* Parse a cast-expression.
7011
7012 cast-expression:
7013 unary-expression
7014 ( type-id ) cast-expression
7015
7016 ADDRESS_P is true iff the unary-expression is appearing as the
7017 operand of the `&' operator. CAST_P is true if this expression is
7018 the target of a cast.
7019
7020 Returns a representation of the expression. */
7021
7022 static tree
7023 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7024 cp_id_kind * pidk)
7025 {
7026 /* If it's a `(', then we might be looking at a cast. */
7027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7028 {
7029 tree type = NULL_TREE;
7030 tree expr = NULL_TREE;
7031 bool compound_literal_p;
7032 const char *saved_message;
7033
7034 /* There's no way to know yet whether or not this is a cast.
7035 For example, `(int (3))' is a unary-expression, while `(int)
7036 3' is a cast. So, we resort to parsing tentatively. */
7037 cp_parser_parse_tentatively (parser);
7038 /* Types may not be defined in a cast. */
7039 saved_message = parser->type_definition_forbidden_message;
7040 parser->type_definition_forbidden_message
7041 = G_("types may not be defined in casts");
7042 /* Consume the `('. */
7043 cp_lexer_consume_token (parser->lexer);
7044 /* A very tricky bit is that `(struct S) { 3 }' is a
7045 compound-literal (which we permit in C++ as an extension).
7046 But, that construct is not a cast-expression -- it is a
7047 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7048 is legal; if the compound-literal were a cast-expression,
7049 you'd need an extra set of parentheses.) But, if we parse
7050 the type-id, and it happens to be a class-specifier, then we
7051 will commit to the parse at that point, because we cannot
7052 undo the action that is done when creating a new class. So,
7053 then we cannot back up and do a postfix-expression.
7054
7055 Therefore, we scan ahead to the closing `)', and check to see
7056 if the token after the `)' is a `{'. If so, we are not
7057 looking at a cast-expression.
7058
7059 Save tokens so that we can put them back. */
7060 cp_lexer_save_tokens (parser->lexer);
7061 /* Skip tokens until the next token is a closing parenthesis.
7062 If we find the closing `)', and the next token is a `{', then
7063 we are looking at a compound-literal. */
7064 compound_literal_p
7065 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7066 /*consume_paren=*/true)
7067 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7068 /* Roll back the tokens we skipped. */
7069 cp_lexer_rollback_tokens (parser->lexer);
7070 /* If we were looking at a compound-literal, simulate an error
7071 so that the call to cp_parser_parse_definitely below will
7072 fail. */
7073 if (compound_literal_p)
7074 cp_parser_simulate_error (parser);
7075 else
7076 {
7077 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7078 parser->in_type_id_in_expr_p = true;
7079 /* Look for the type-id. */
7080 type = cp_parser_type_id (parser);
7081 /* Look for the closing `)'. */
7082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7083 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7084 }
7085
7086 /* Restore the saved message. */
7087 parser->type_definition_forbidden_message = saved_message;
7088
7089 /* At this point this can only be either a cast or a
7090 parenthesized ctor such as `(T ())' that looks like a cast to
7091 function returning T. */
7092 if (!cp_parser_error_occurred (parser)
7093 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7094 (parser->lexer)))
7095 {
7096 cp_parser_parse_definitely (parser);
7097 expr = cp_parser_cast_expression (parser,
7098 /*address_p=*/false,
7099 /*cast_p=*/true, pidk);
7100
7101 /* Warn about old-style casts, if so requested. */
7102 if (warn_old_style_cast
7103 && !in_system_header
7104 && !VOID_TYPE_P (type)
7105 && current_lang_name != lang_name_c)
7106 warning (OPT_Wold_style_cast, "use of old-style cast");
7107
7108 /* Only type conversions to integral or enumeration types
7109 can be used in constant-expressions. */
7110 if (!cast_valid_in_integral_constant_expression_p (type)
7111 && cp_parser_non_integral_constant_expression (parser,
7112 NIC_CAST))
7113 return error_mark_node;
7114
7115 /* Perform the cast. */
7116 expr = build_c_cast (input_location, type, expr);
7117 return expr;
7118 }
7119 else
7120 cp_parser_abort_tentative_parse (parser);
7121 }
7122
7123 /* If we get here, then it's not a cast, so it must be a
7124 unary-expression. */
7125 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7126 }
7127
7128 /* Parse a binary expression of the general form:
7129
7130 pm-expression:
7131 cast-expression
7132 pm-expression .* cast-expression
7133 pm-expression ->* cast-expression
7134
7135 multiplicative-expression:
7136 pm-expression
7137 multiplicative-expression * pm-expression
7138 multiplicative-expression / pm-expression
7139 multiplicative-expression % pm-expression
7140
7141 additive-expression:
7142 multiplicative-expression
7143 additive-expression + multiplicative-expression
7144 additive-expression - multiplicative-expression
7145
7146 shift-expression:
7147 additive-expression
7148 shift-expression << additive-expression
7149 shift-expression >> additive-expression
7150
7151 relational-expression:
7152 shift-expression
7153 relational-expression < shift-expression
7154 relational-expression > shift-expression
7155 relational-expression <= shift-expression
7156 relational-expression >= shift-expression
7157
7158 GNU Extension:
7159
7160 relational-expression:
7161 relational-expression <? shift-expression
7162 relational-expression >? shift-expression
7163
7164 equality-expression:
7165 relational-expression
7166 equality-expression == relational-expression
7167 equality-expression != relational-expression
7168
7169 and-expression:
7170 equality-expression
7171 and-expression & equality-expression
7172
7173 exclusive-or-expression:
7174 and-expression
7175 exclusive-or-expression ^ and-expression
7176
7177 inclusive-or-expression:
7178 exclusive-or-expression
7179 inclusive-or-expression | exclusive-or-expression
7180
7181 logical-and-expression:
7182 inclusive-or-expression
7183 logical-and-expression && inclusive-or-expression
7184
7185 logical-or-expression:
7186 logical-and-expression
7187 logical-or-expression || logical-and-expression
7188
7189 All these are implemented with a single function like:
7190
7191 binary-expression:
7192 simple-cast-expression
7193 binary-expression <token> binary-expression
7194
7195 CAST_P is true if this expression is the target of a cast.
7196
7197 The binops_by_token map is used to get the tree codes for each <token> type.
7198 binary-expressions are associated according to a precedence table. */
7199
7200 #define TOKEN_PRECEDENCE(token) \
7201 (((token->type == CPP_GREATER \
7202 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7203 && !parser->greater_than_is_operator_p) \
7204 ? PREC_NOT_OPERATOR \
7205 : binops_by_token[token->type].prec)
7206
7207 static tree
7208 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7209 bool no_toplevel_fold_p,
7210 enum cp_parser_prec prec,
7211 cp_id_kind * pidk)
7212 {
7213 cp_parser_expression_stack stack;
7214 cp_parser_expression_stack_entry *sp = &stack[0];
7215 tree lhs, rhs;
7216 cp_token *token;
7217 enum tree_code tree_type, lhs_type, rhs_type;
7218 enum cp_parser_prec new_prec, lookahead_prec;
7219 tree overload;
7220
7221 /* Parse the first expression. */
7222 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7223 lhs_type = ERROR_MARK;
7224
7225 for (;;)
7226 {
7227 /* Get an operator token. */
7228 token = cp_lexer_peek_token (parser->lexer);
7229
7230 if (warn_cxx0x_compat
7231 && token->type == CPP_RSHIFT
7232 && !parser->greater_than_is_operator_p)
7233 {
7234 if (warning_at (token->location, OPT_Wc__0x_compat,
7235 "%<>>%> operator is treated as"
7236 " two right angle brackets in C++11"))
7237 inform (token->location,
7238 "suggest parentheses around %<>>%> expression");
7239 }
7240
7241 new_prec = TOKEN_PRECEDENCE (token);
7242
7243 /* Popping an entry off the stack means we completed a subexpression:
7244 - either we found a token which is not an operator (`>' where it is not
7245 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7246 will happen repeatedly;
7247 - or, we found an operator which has lower priority. This is the case
7248 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7249 parsing `3 * 4'. */
7250 if (new_prec <= prec)
7251 {
7252 if (sp == stack)
7253 break;
7254 else
7255 goto pop;
7256 }
7257
7258 get_rhs:
7259 tree_type = binops_by_token[token->type].tree_type;
7260
7261 /* We used the operator token. */
7262 cp_lexer_consume_token (parser->lexer);
7263
7264 /* For "false && x" or "true || x", x will never be executed;
7265 disable warnings while evaluating it. */
7266 if (tree_type == TRUTH_ANDIF_EXPR)
7267 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7268 else if (tree_type == TRUTH_ORIF_EXPR)
7269 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7270
7271 /* Extract another operand. It may be the RHS of this expression
7272 or the LHS of a new, higher priority expression. */
7273 rhs = cp_parser_simple_cast_expression (parser);
7274 rhs_type = ERROR_MARK;
7275
7276 /* Get another operator token. Look up its precedence to avoid
7277 building a useless (immediately popped) stack entry for common
7278 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7279 token = cp_lexer_peek_token (parser->lexer);
7280 lookahead_prec = TOKEN_PRECEDENCE (token);
7281 if (lookahead_prec > new_prec)
7282 {
7283 /* ... and prepare to parse the RHS of the new, higher priority
7284 expression. Since precedence levels on the stack are
7285 monotonically increasing, we do not have to care about
7286 stack overflows. */
7287 sp->prec = prec;
7288 sp->tree_type = tree_type;
7289 sp->lhs = lhs;
7290 sp->lhs_type = lhs_type;
7291 sp++;
7292 lhs = rhs;
7293 lhs_type = rhs_type;
7294 prec = new_prec;
7295 new_prec = lookahead_prec;
7296 goto get_rhs;
7297
7298 pop:
7299 lookahead_prec = new_prec;
7300 /* If the stack is not empty, we have parsed into LHS the right side
7301 (`4' in the example above) of an expression we had suspended.
7302 We can use the information on the stack to recover the LHS (`3')
7303 from the stack together with the tree code (`MULT_EXPR'), and
7304 the precedence of the higher level subexpression
7305 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7306 which will be used to actually build the additive expression. */
7307 --sp;
7308 prec = sp->prec;
7309 tree_type = sp->tree_type;
7310 rhs = lhs;
7311 rhs_type = lhs_type;
7312 lhs = sp->lhs;
7313 lhs_type = sp->lhs_type;
7314 }
7315
7316 /* Undo the disabling of warnings done above. */
7317 if (tree_type == TRUTH_ANDIF_EXPR)
7318 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7319 else if (tree_type == TRUTH_ORIF_EXPR)
7320 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7321
7322 overload = NULL;
7323 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7324 ERROR_MARK for everything that is not a binary expression.
7325 This makes warn_about_parentheses miss some warnings that
7326 involve unary operators. For unary expressions we should
7327 pass the correct tree_code unless the unary expression was
7328 surrounded by parentheses.
7329 */
7330 if (no_toplevel_fold_p
7331 && lookahead_prec <= prec
7332 && sp == stack
7333 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7334 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7335 else
7336 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7337 &overload, tf_warning_or_error);
7338 lhs_type = tree_type;
7339
7340 /* If the binary operator required the use of an overloaded operator,
7341 then this expression cannot be an integral constant-expression.
7342 An overloaded operator can be used even if both operands are
7343 otherwise permissible in an integral constant-expression if at
7344 least one of the operands is of enumeration type. */
7345
7346 if (overload
7347 && cp_parser_non_integral_constant_expression (parser,
7348 NIC_OVERLOADED))
7349 return error_mark_node;
7350 }
7351
7352 return lhs;
7353 }
7354
7355
7356 /* Parse the `? expression : assignment-expression' part of a
7357 conditional-expression. The LOGICAL_OR_EXPR is the
7358 logical-or-expression that started the conditional-expression.
7359 Returns a representation of the entire conditional-expression.
7360
7361 This routine is used by cp_parser_assignment_expression.
7362
7363 ? expression : assignment-expression
7364
7365 GNU Extensions:
7366
7367 ? : assignment-expression */
7368
7369 static tree
7370 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7371 {
7372 tree expr;
7373 tree assignment_expr;
7374 struct cp_token *token;
7375
7376 /* Consume the `?' token. */
7377 cp_lexer_consume_token (parser->lexer);
7378 token = cp_lexer_peek_token (parser->lexer);
7379 if (cp_parser_allow_gnu_extensions_p (parser)
7380 && token->type == CPP_COLON)
7381 {
7382 pedwarn (token->location, OPT_pedantic,
7383 "ISO C++ does not allow ?: with omitted middle operand");
7384 /* Implicit true clause. */
7385 expr = NULL_TREE;
7386 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7387 warn_for_omitted_condop (token->location, logical_or_expr);
7388 }
7389 else
7390 {
7391 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7392 parser->colon_corrects_to_scope_p = false;
7393 /* Parse the expression. */
7394 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7395 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7396 c_inhibit_evaluation_warnings +=
7397 ((logical_or_expr == truthvalue_true_node)
7398 - (logical_or_expr == truthvalue_false_node));
7399 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7400 }
7401
7402 /* The next token should be a `:'. */
7403 cp_parser_require (parser, CPP_COLON, RT_COLON);
7404 /* Parse the assignment-expression. */
7405 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7406 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7407
7408 /* Build the conditional-expression. */
7409 return build_x_conditional_expr (logical_or_expr,
7410 expr,
7411 assignment_expr,
7412 tf_warning_or_error);
7413 }
7414
7415 /* Parse an assignment-expression.
7416
7417 assignment-expression:
7418 conditional-expression
7419 logical-or-expression assignment-operator assignment_expression
7420 throw-expression
7421
7422 CAST_P is true if this expression is the target of a cast.
7423
7424 Returns a representation for the expression. */
7425
7426 static tree
7427 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7428 cp_id_kind * pidk)
7429 {
7430 tree expr;
7431
7432 /* If the next token is the `throw' keyword, then we're looking at
7433 a throw-expression. */
7434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7435 expr = cp_parser_throw_expression (parser);
7436 /* Otherwise, it must be that we are looking at a
7437 logical-or-expression. */
7438 else
7439 {
7440 /* Parse the binary expressions (logical-or-expression). */
7441 expr = cp_parser_binary_expression (parser, cast_p, false,
7442 PREC_NOT_OPERATOR, pidk);
7443 /* If the next token is a `?' then we're actually looking at a
7444 conditional-expression. */
7445 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7446 return cp_parser_question_colon_clause (parser, expr);
7447 else
7448 {
7449 enum tree_code assignment_operator;
7450
7451 /* If it's an assignment-operator, we're using the second
7452 production. */
7453 assignment_operator
7454 = cp_parser_assignment_operator_opt (parser);
7455 if (assignment_operator != ERROR_MARK)
7456 {
7457 bool non_constant_p;
7458
7459 /* Parse the right-hand side of the assignment. */
7460 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7461
7462 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7463 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7464
7465 /* An assignment may not appear in a
7466 constant-expression. */
7467 if (cp_parser_non_integral_constant_expression (parser,
7468 NIC_ASSIGNMENT))
7469 return error_mark_node;
7470 /* Build the assignment expression. */
7471 expr = build_x_modify_expr (expr,
7472 assignment_operator,
7473 rhs,
7474 tf_warning_or_error);
7475 }
7476 }
7477 }
7478
7479 return expr;
7480 }
7481
7482 /* Parse an (optional) assignment-operator.
7483
7484 assignment-operator: one of
7485 = *= /= %= += -= >>= <<= &= ^= |=
7486
7487 GNU Extension:
7488
7489 assignment-operator: one of
7490 <?= >?=
7491
7492 If the next token is an assignment operator, the corresponding tree
7493 code is returned, and the token is consumed. For example, for
7494 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7495 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7496 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7497 operator, ERROR_MARK is returned. */
7498
7499 static enum tree_code
7500 cp_parser_assignment_operator_opt (cp_parser* parser)
7501 {
7502 enum tree_code op;
7503 cp_token *token;
7504
7505 /* Peek at the next token. */
7506 token = cp_lexer_peek_token (parser->lexer);
7507
7508 switch (token->type)
7509 {
7510 case CPP_EQ:
7511 op = NOP_EXPR;
7512 break;
7513
7514 case CPP_MULT_EQ:
7515 op = MULT_EXPR;
7516 break;
7517
7518 case CPP_DIV_EQ:
7519 op = TRUNC_DIV_EXPR;
7520 break;
7521
7522 case CPP_MOD_EQ:
7523 op = TRUNC_MOD_EXPR;
7524 break;
7525
7526 case CPP_PLUS_EQ:
7527 op = PLUS_EXPR;
7528 break;
7529
7530 case CPP_MINUS_EQ:
7531 op = MINUS_EXPR;
7532 break;
7533
7534 case CPP_RSHIFT_EQ:
7535 op = RSHIFT_EXPR;
7536 break;
7537
7538 case CPP_LSHIFT_EQ:
7539 op = LSHIFT_EXPR;
7540 break;
7541
7542 case CPP_AND_EQ:
7543 op = BIT_AND_EXPR;
7544 break;
7545
7546 case CPP_XOR_EQ:
7547 op = BIT_XOR_EXPR;
7548 break;
7549
7550 case CPP_OR_EQ:
7551 op = BIT_IOR_EXPR;
7552 break;
7553
7554 default:
7555 /* Nothing else is an assignment operator. */
7556 op = ERROR_MARK;
7557 }
7558
7559 /* If it was an assignment operator, consume it. */
7560 if (op != ERROR_MARK)
7561 cp_lexer_consume_token (parser->lexer);
7562
7563 return op;
7564 }
7565
7566 /* Parse an expression.
7567
7568 expression:
7569 assignment-expression
7570 expression , assignment-expression
7571
7572 CAST_P is true if this expression is the target of a cast.
7573
7574 Returns a representation of the expression. */
7575
7576 static tree
7577 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7578 {
7579 tree expression = NULL_TREE;
7580
7581 while (true)
7582 {
7583 tree assignment_expression;
7584
7585 /* Parse the next assignment-expression. */
7586 assignment_expression
7587 = cp_parser_assignment_expression (parser, cast_p, pidk);
7588 /* If this is the first assignment-expression, we can just
7589 save it away. */
7590 if (!expression)
7591 expression = assignment_expression;
7592 else
7593 expression = build_x_compound_expr (expression,
7594 assignment_expression,
7595 tf_warning_or_error);
7596 /* If the next token is not a comma, then we are done with the
7597 expression. */
7598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7599 break;
7600 /* Consume the `,'. */
7601 cp_lexer_consume_token (parser->lexer);
7602 /* A comma operator cannot appear in a constant-expression. */
7603 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7604 expression = error_mark_node;
7605 }
7606
7607 return expression;
7608 }
7609
7610 /* Parse a constant-expression.
7611
7612 constant-expression:
7613 conditional-expression
7614
7615 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7616 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7617 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7618 is false, NON_CONSTANT_P should be NULL. */
7619
7620 static tree
7621 cp_parser_constant_expression (cp_parser* parser,
7622 bool allow_non_constant_p,
7623 bool *non_constant_p)
7624 {
7625 bool saved_integral_constant_expression_p;
7626 bool saved_allow_non_integral_constant_expression_p;
7627 bool saved_non_integral_constant_expression_p;
7628 tree expression;
7629
7630 /* It might seem that we could simply parse the
7631 conditional-expression, and then check to see if it were
7632 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7633 one that the compiler can figure out is constant, possibly after
7634 doing some simplifications or optimizations. The standard has a
7635 precise definition of constant-expression, and we must honor
7636 that, even though it is somewhat more restrictive.
7637
7638 For example:
7639
7640 int i[(2, 3)];
7641
7642 is not a legal declaration, because `(2, 3)' is not a
7643 constant-expression. The `,' operator is forbidden in a
7644 constant-expression. However, GCC's constant-folding machinery
7645 will fold this operation to an INTEGER_CST for `3'. */
7646
7647 /* Save the old settings. */
7648 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7649 saved_allow_non_integral_constant_expression_p
7650 = parser->allow_non_integral_constant_expression_p;
7651 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7652 /* We are now parsing a constant-expression. */
7653 parser->integral_constant_expression_p = true;
7654 parser->allow_non_integral_constant_expression_p
7655 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7656 parser->non_integral_constant_expression_p = false;
7657 /* Although the grammar says "conditional-expression", we parse an
7658 "assignment-expression", which also permits "throw-expression"
7659 and the use of assignment operators. In the case that
7660 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7661 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7662 actually essential that we look for an assignment-expression.
7663 For example, cp_parser_initializer_clauses uses this function to
7664 determine whether a particular assignment-expression is in fact
7665 constant. */
7666 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7667 /* Restore the old settings. */
7668 parser->integral_constant_expression_p
7669 = saved_integral_constant_expression_p;
7670 parser->allow_non_integral_constant_expression_p
7671 = saved_allow_non_integral_constant_expression_p;
7672 if (cxx_dialect >= cxx0x)
7673 {
7674 /* Require an rvalue constant expression here; that's what our
7675 callers expect. Reference constant expressions are handled
7676 separately in e.g. cp_parser_template_argument. */
7677 bool is_const = potential_rvalue_constant_expression (expression);
7678 parser->non_integral_constant_expression_p = !is_const;
7679 if (!is_const && !allow_non_constant_p)
7680 require_potential_rvalue_constant_expression (expression);
7681 }
7682 if (allow_non_constant_p)
7683 *non_constant_p = parser->non_integral_constant_expression_p;
7684 parser->non_integral_constant_expression_p
7685 = saved_non_integral_constant_expression_p;
7686
7687 return expression;
7688 }
7689
7690 /* Parse __builtin_offsetof.
7691
7692 offsetof-expression:
7693 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7694
7695 offsetof-member-designator:
7696 id-expression
7697 | offsetof-member-designator "." id-expression
7698 | offsetof-member-designator "[" expression "]"
7699 | offsetof-member-designator "->" id-expression */
7700
7701 static tree
7702 cp_parser_builtin_offsetof (cp_parser *parser)
7703 {
7704 int save_ice_p, save_non_ice_p;
7705 tree type, expr;
7706 cp_id_kind dummy;
7707 cp_token *token;
7708
7709 /* We're about to accept non-integral-constant things, but will
7710 definitely yield an integral constant expression. Save and
7711 restore these values around our local parsing. */
7712 save_ice_p = parser->integral_constant_expression_p;
7713 save_non_ice_p = parser->non_integral_constant_expression_p;
7714
7715 /* Consume the "__builtin_offsetof" token. */
7716 cp_lexer_consume_token (parser->lexer);
7717 /* Consume the opening `('. */
7718 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7719 /* Parse the type-id. */
7720 type = cp_parser_type_id (parser);
7721 /* Look for the `,'. */
7722 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7723 token = cp_lexer_peek_token (parser->lexer);
7724
7725 /* Build the (type *)null that begins the traditional offsetof macro. */
7726 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7727 tf_warning_or_error);
7728
7729 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7730 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7731 true, &dummy, token->location);
7732 while (true)
7733 {
7734 token = cp_lexer_peek_token (parser->lexer);
7735 switch (token->type)
7736 {
7737 case CPP_OPEN_SQUARE:
7738 /* offsetof-member-designator "[" expression "]" */
7739 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7740 break;
7741
7742 case CPP_DEREF:
7743 /* offsetof-member-designator "->" identifier */
7744 expr = grok_array_decl (expr, integer_zero_node);
7745 /* FALLTHRU */
7746
7747 case CPP_DOT:
7748 /* offsetof-member-designator "." identifier */
7749 cp_lexer_consume_token (parser->lexer);
7750 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7751 expr, true, &dummy,
7752 token->location);
7753 break;
7754
7755 case CPP_CLOSE_PAREN:
7756 /* Consume the ")" token. */
7757 cp_lexer_consume_token (parser->lexer);
7758 goto success;
7759
7760 default:
7761 /* Error. We know the following require will fail, but
7762 that gives the proper error message. */
7763 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7764 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7765 expr = error_mark_node;
7766 goto failure;
7767 }
7768 }
7769
7770 success:
7771 /* If we're processing a template, we can't finish the semantics yet.
7772 Otherwise we can fold the entire expression now. */
7773 if (processing_template_decl)
7774 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7775 else
7776 expr = finish_offsetof (expr);
7777
7778 failure:
7779 parser->integral_constant_expression_p = save_ice_p;
7780 parser->non_integral_constant_expression_p = save_non_ice_p;
7781
7782 return expr;
7783 }
7784
7785 /* Parse a trait expression.
7786
7787 Returns a representation of the expression, the underlying type
7788 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7789
7790 static tree
7791 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7792 {
7793 cp_trait_kind kind;
7794 tree type1, type2 = NULL_TREE;
7795 bool binary = false;
7796 cp_decl_specifier_seq decl_specs;
7797
7798 switch (keyword)
7799 {
7800 case RID_HAS_NOTHROW_ASSIGN:
7801 kind = CPTK_HAS_NOTHROW_ASSIGN;
7802 break;
7803 case RID_HAS_NOTHROW_CONSTRUCTOR:
7804 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7805 break;
7806 case RID_HAS_NOTHROW_COPY:
7807 kind = CPTK_HAS_NOTHROW_COPY;
7808 break;
7809 case RID_HAS_TRIVIAL_ASSIGN:
7810 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7811 break;
7812 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7813 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7814 break;
7815 case RID_HAS_TRIVIAL_COPY:
7816 kind = CPTK_HAS_TRIVIAL_COPY;
7817 break;
7818 case RID_HAS_TRIVIAL_DESTRUCTOR:
7819 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7820 break;
7821 case RID_HAS_VIRTUAL_DESTRUCTOR:
7822 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7823 break;
7824 case RID_IS_ABSTRACT:
7825 kind = CPTK_IS_ABSTRACT;
7826 break;
7827 case RID_IS_BASE_OF:
7828 kind = CPTK_IS_BASE_OF;
7829 binary = true;
7830 break;
7831 case RID_IS_CLASS:
7832 kind = CPTK_IS_CLASS;
7833 break;
7834 case RID_IS_CONVERTIBLE_TO:
7835 kind = CPTK_IS_CONVERTIBLE_TO;
7836 binary = true;
7837 break;
7838 case RID_IS_EMPTY:
7839 kind = CPTK_IS_EMPTY;
7840 break;
7841 case RID_IS_ENUM:
7842 kind = CPTK_IS_ENUM;
7843 break;
7844 case RID_IS_LITERAL_TYPE:
7845 kind = CPTK_IS_LITERAL_TYPE;
7846 break;
7847 case RID_IS_POD:
7848 kind = CPTK_IS_POD;
7849 break;
7850 case RID_IS_POLYMORPHIC:
7851 kind = CPTK_IS_POLYMORPHIC;
7852 break;
7853 case RID_IS_STD_LAYOUT:
7854 kind = CPTK_IS_STD_LAYOUT;
7855 break;
7856 case RID_IS_TRIVIAL:
7857 kind = CPTK_IS_TRIVIAL;
7858 break;
7859 case RID_IS_UNION:
7860 kind = CPTK_IS_UNION;
7861 break;
7862 case RID_UNDERLYING_TYPE:
7863 kind = CPTK_UNDERLYING_TYPE;
7864 break;
7865 case RID_BASES:
7866 kind = CPTK_BASES;
7867 break;
7868 case RID_DIRECT_BASES:
7869 kind = CPTK_DIRECT_BASES;
7870 break;
7871 default:
7872 gcc_unreachable ();
7873 }
7874
7875 /* Consume the token. */
7876 cp_lexer_consume_token (parser->lexer);
7877
7878 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7879
7880 type1 = cp_parser_type_id (parser);
7881
7882 if (type1 == error_mark_node)
7883 return error_mark_node;
7884
7885 /* Build a trivial decl-specifier-seq. */
7886 clear_decl_specs (&decl_specs);
7887 decl_specs.type = type1;
7888
7889 /* Call grokdeclarator to figure out what type this is. */
7890 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7891 /*initialized=*/0, /*attrlist=*/NULL);
7892
7893 if (binary)
7894 {
7895 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7896
7897 type2 = cp_parser_type_id (parser);
7898
7899 if (type2 == error_mark_node)
7900 return error_mark_node;
7901
7902 /* Build a trivial decl-specifier-seq. */
7903 clear_decl_specs (&decl_specs);
7904 decl_specs.type = type2;
7905
7906 /* Call grokdeclarator to figure out what type this is. */
7907 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7908 /*initialized=*/0, /*attrlist=*/NULL);
7909 }
7910
7911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7912
7913 /* Complete the trait expression, which may mean either processing
7914 the trait expr now or saving it for template instantiation. */
7915 switch(kind)
7916 {
7917 case CPTK_UNDERLYING_TYPE:
7918 return finish_underlying_type (type1);
7919 case CPTK_BASES:
7920 return finish_bases (type1, false);
7921 case CPTK_DIRECT_BASES:
7922 return finish_bases (type1, true);
7923 default:
7924 return finish_trait_expr (kind, type1, type2);
7925 }
7926 }
7927
7928 /* Lambdas that appear in variable initializer or default argument scope
7929 get that in their mangling, so we need to record it. We might as well
7930 use the count for function and namespace scopes as well. */
7931 static GTY(()) tree lambda_scope;
7932 static GTY(()) int lambda_count;
7933 typedef struct GTY(()) tree_int
7934 {
7935 tree t;
7936 int i;
7937 } tree_int;
7938 DEF_VEC_O(tree_int);
7939 DEF_VEC_ALLOC_O(tree_int,gc);
7940 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7941
7942 static void
7943 start_lambda_scope (tree decl)
7944 {
7945 tree_int ti;
7946 gcc_assert (decl);
7947 /* Once we're inside a function, we ignore other scopes and just push
7948 the function again so that popping works properly. */
7949 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7950 decl = current_function_decl;
7951 ti.t = lambda_scope;
7952 ti.i = lambda_count;
7953 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7954 if (lambda_scope != decl)
7955 {
7956 /* Don't reset the count if we're still in the same function. */
7957 lambda_scope = decl;
7958 lambda_count = 0;
7959 }
7960 }
7961
7962 static void
7963 record_lambda_scope (tree lambda)
7964 {
7965 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7966 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7967 }
7968
7969 static void
7970 finish_lambda_scope (void)
7971 {
7972 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7973 if (lambda_scope != p->t)
7974 {
7975 lambda_scope = p->t;
7976 lambda_count = p->i;
7977 }
7978 VEC_pop (tree_int, lambda_scope_stack);
7979 }
7980
7981 /* Parse a lambda expression.
7982
7983 lambda-expression:
7984 lambda-introducer lambda-declarator [opt] compound-statement
7985
7986 Returns a representation of the expression. */
7987
7988 static tree
7989 cp_parser_lambda_expression (cp_parser* parser)
7990 {
7991 tree lambda_expr = build_lambda_expr ();
7992 tree type;
7993 bool ok;
7994
7995 LAMBDA_EXPR_LOCATION (lambda_expr)
7996 = cp_lexer_peek_token (parser->lexer)->location;
7997
7998 if (cp_unevaluated_operand)
7999 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8000 "lambda-expression in unevaluated context");
8001
8002 /* We may be in the middle of deferred access check. Disable
8003 it now. */
8004 push_deferring_access_checks (dk_no_deferred);
8005
8006 cp_parser_lambda_introducer (parser, lambda_expr);
8007
8008 type = begin_lambda_type (lambda_expr);
8009
8010 record_lambda_scope (lambda_expr);
8011
8012 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8013 determine_visibility (TYPE_NAME (type));
8014
8015 /* Now that we've started the type, add the capture fields for any
8016 explicit captures. */
8017 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8018
8019 {
8020 /* Inside the class, surrounding template-parameter-lists do not apply. */
8021 unsigned int saved_num_template_parameter_lists
8022 = parser->num_template_parameter_lists;
8023 unsigned char in_statement = parser->in_statement;
8024 bool in_switch_statement_p = parser->in_switch_statement_p;
8025
8026 parser->num_template_parameter_lists = 0;
8027 parser->in_statement = 0;
8028 parser->in_switch_statement_p = false;
8029
8030 /* By virtue of defining a local class, a lambda expression has access to
8031 the private variables of enclosing classes. */
8032
8033 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8034
8035 if (ok)
8036 cp_parser_lambda_body (parser, lambda_expr);
8037 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8038 cp_parser_skip_to_end_of_block_or_statement (parser);
8039
8040 /* The capture list was built up in reverse order; fix that now. */
8041 {
8042 tree newlist = NULL_TREE;
8043 tree elt, next;
8044
8045 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8046 elt; elt = next)
8047 {
8048 next = TREE_CHAIN (elt);
8049 TREE_CHAIN (elt) = newlist;
8050 newlist = elt;
8051 }
8052 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8053 }
8054
8055 if (ok)
8056 maybe_add_lambda_conv_op (type);
8057
8058 type = finish_struct (type, /*attributes=*/NULL_TREE);
8059
8060 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8061 parser->in_statement = in_statement;
8062 parser->in_switch_statement_p = in_switch_statement_p;
8063 }
8064
8065 pop_deferring_access_checks ();
8066
8067 /* This field is only used during parsing of the lambda. */
8068 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8069
8070 /* This lambda shouldn't have any proxies left at this point. */
8071 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8072 /* And now that we're done, push proxies for an enclosing lambda. */
8073 insert_pending_capture_proxies ();
8074
8075 if (ok)
8076 return build_lambda_object (lambda_expr);
8077 else
8078 return error_mark_node;
8079 }
8080
8081 /* Parse the beginning of a lambda expression.
8082
8083 lambda-introducer:
8084 [ lambda-capture [opt] ]
8085
8086 LAMBDA_EXPR is the current representation of the lambda expression. */
8087
8088 static void
8089 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8090 {
8091 /* Need commas after the first capture. */
8092 bool first = true;
8093
8094 /* Eat the leading `['. */
8095 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8096
8097 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8098 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8099 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8100 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8101 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8102 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8103
8104 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8105 {
8106 cp_lexer_consume_token (parser->lexer);
8107 first = false;
8108 }
8109
8110 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8111 {
8112 cp_token* capture_token;
8113 tree capture_id;
8114 tree capture_init_expr;
8115 cp_id_kind idk = CP_ID_KIND_NONE;
8116 bool explicit_init_p = false;
8117
8118 enum capture_kind_type
8119 {
8120 BY_COPY,
8121 BY_REFERENCE
8122 };
8123 enum capture_kind_type capture_kind = BY_COPY;
8124
8125 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8126 {
8127 error ("expected end of capture-list");
8128 return;
8129 }
8130
8131 if (first)
8132 first = false;
8133 else
8134 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8135
8136 /* Possibly capture `this'. */
8137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8138 {
8139 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8140 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8141 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8142 "with by-copy capture default");
8143 cp_lexer_consume_token (parser->lexer);
8144 add_capture (lambda_expr,
8145 /*id=*/this_identifier,
8146 /*initializer=*/finish_this_expr(),
8147 /*by_reference_p=*/false,
8148 explicit_init_p);
8149 continue;
8150 }
8151
8152 /* Remember whether we want to capture as a reference or not. */
8153 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8154 {
8155 capture_kind = BY_REFERENCE;
8156 cp_lexer_consume_token (parser->lexer);
8157 }
8158
8159 /* Get the identifier. */
8160 capture_token = cp_lexer_peek_token (parser->lexer);
8161 capture_id = cp_parser_identifier (parser);
8162
8163 if (capture_id == error_mark_node)
8164 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8165 delimiters, but I modified this to stop on unnested ']' as well. It
8166 was already changed to stop on unnested '}', so the
8167 "closing_parenthesis" name is no more misleading with my change. */
8168 {
8169 cp_parser_skip_to_closing_parenthesis (parser,
8170 /*recovering=*/true,
8171 /*or_comma=*/true,
8172 /*consume_paren=*/true);
8173 break;
8174 }
8175
8176 /* Find the initializer for this capture. */
8177 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8178 {
8179 /* An explicit expression exists. */
8180 cp_lexer_consume_token (parser->lexer);
8181 pedwarn (input_location, OPT_pedantic,
8182 "ISO C++ does not allow initializers "
8183 "in lambda expression capture lists");
8184 capture_init_expr = cp_parser_assignment_expression (parser,
8185 /*cast_p=*/true,
8186 &idk);
8187 explicit_init_p = true;
8188 }
8189 else
8190 {
8191 const char* error_msg;
8192
8193 /* Turn the identifier into an id-expression. */
8194 capture_init_expr
8195 = cp_parser_lookup_name
8196 (parser,
8197 capture_id,
8198 none_type,
8199 /*is_template=*/false,
8200 /*is_namespace=*/false,
8201 /*check_dependency=*/true,
8202 /*ambiguous_decls=*/NULL,
8203 capture_token->location);
8204
8205 if (capture_init_expr == error_mark_node)
8206 {
8207 unqualified_name_lookup_error (capture_id);
8208 continue;
8209 }
8210 else if (DECL_P (capture_init_expr)
8211 && (TREE_CODE (capture_init_expr) != VAR_DECL
8212 && TREE_CODE (capture_init_expr) != PARM_DECL))
8213 {
8214 error_at (capture_token->location,
8215 "capture of non-variable %qD ",
8216 capture_init_expr);
8217 inform (0, "%q+#D declared here", capture_init_expr);
8218 continue;
8219 }
8220 if (TREE_CODE (capture_init_expr) == VAR_DECL
8221 && decl_storage_duration (capture_init_expr) != dk_auto)
8222 {
8223 pedwarn (capture_token->location, 0, "capture of variable "
8224 "%qD with non-automatic storage duration",
8225 capture_init_expr);
8226 inform (0, "%q+#D declared here", capture_init_expr);
8227 continue;
8228 }
8229
8230 capture_init_expr
8231 = finish_id_expression
8232 (capture_id,
8233 capture_init_expr,
8234 parser->scope,
8235 &idk,
8236 /*integral_constant_expression_p=*/false,
8237 /*allow_non_integral_constant_expression_p=*/false,
8238 /*non_integral_constant_expression_p=*/NULL,
8239 /*template_p=*/false,
8240 /*done=*/true,
8241 /*address_p=*/false,
8242 /*template_arg_p=*/false,
8243 &error_msg,
8244 capture_token->location);
8245 }
8246
8247 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8248 && !explicit_init_p)
8249 {
8250 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8251 && capture_kind == BY_COPY)
8252 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8253 "of %qD redundant with by-copy capture default",
8254 capture_id);
8255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8256 && capture_kind == BY_REFERENCE)
8257 pedwarn (capture_token->location, 0, "explicit by-reference "
8258 "capture of %qD redundant with by-reference capture "
8259 "default", capture_id);
8260 }
8261
8262 add_capture (lambda_expr,
8263 capture_id,
8264 capture_init_expr,
8265 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8266 explicit_init_p);
8267 }
8268
8269 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8270 }
8271
8272 /* Parse the (optional) middle of a lambda expression.
8273
8274 lambda-declarator:
8275 ( parameter-declaration-clause [opt] )
8276 attribute-specifier [opt]
8277 mutable [opt]
8278 exception-specification [opt]
8279 lambda-return-type-clause [opt]
8280
8281 LAMBDA_EXPR is the current representation of the lambda expression. */
8282
8283 static bool
8284 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8285 {
8286 /* 5.1.1.4 of the standard says:
8287 If a lambda-expression does not include a lambda-declarator, it is as if
8288 the lambda-declarator were ().
8289 This means an empty parameter list, no attributes, and no exception
8290 specification. */
8291 tree param_list = void_list_node;
8292 tree attributes = NULL_TREE;
8293 tree exception_spec = NULL_TREE;
8294 tree t;
8295
8296 /* The lambda-declarator is optional, but must begin with an opening
8297 parenthesis if present. */
8298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8299 {
8300 cp_lexer_consume_token (parser->lexer);
8301
8302 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8303
8304 /* Parse parameters. */
8305 param_list = cp_parser_parameter_declaration_clause (parser);
8306
8307 /* Default arguments shall not be specified in the
8308 parameter-declaration-clause of a lambda-declarator. */
8309 for (t = param_list; t; t = TREE_CHAIN (t))
8310 if (TREE_PURPOSE (t))
8311 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8312 "default argument specified for lambda parameter");
8313
8314 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8315
8316 attributes = cp_parser_attributes_opt (parser);
8317
8318 /* Parse optional `mutable' keyword. */
8319 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8320 {
8321 cp_lexer_consume_token (parser->lexer);
8322 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8323 }
8324
8325 /* Parse optional exception specification. */
8326 exception_spec = cp_parser_exception_specification_opt (parser);
8327
8328 /* Parse optional trailing return type. */
8329 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8330 {
8331 cp_lexer_consume_token (parser->lexer);
8332 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8333 }
8334
8335 /* The function parameters must be in scope all the way until after the
8336 trailing-return-type in case of decltype. */
8337 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8338 pop_binding (DECL_NAME (t), t);
8339
8340 leave_scope ();
8341 }
8342
8343 /* Create the function call operator.
8344
8345 Messing with declarators like this is no uglier than building up the
8346 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8347 other code. */
8348 {
8349 cp_decl_specifier_seq return_type_specs;
8350 cp_declarator* declarator;
8351 tree fco;
8352 int quals;
8353 void *p;
8354
8355 clear_decl_specs (&return_type_specs);
8356 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8357 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8358 else
8359 /* Maybe we will deduce the return type later, but we can use void
8360 as a placeholder return type anyways. */
8361 return_type_specs.type = void_type_node;
8362
8363 p = obstack_alloc (&declarator_obstack, 0);
8364
8365 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8366 sfk_none);
8367
8368 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8369 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8370 declarator = make_call_declarator (declarator, param_list, quals,
8371 VIRT_SPEC_UNSPECIFIED,
8372 exception_spec,
8373 /*late_return_type=*/NULL_TREE);
8374 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8375
8376 fco = grokmethod (&return_type_specs,
8377 declarator,
8378 attributes);
8379 if (fco != error_mark_node)
8380 {
8381 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8382 DECL_ARTIFICIAL (fco) = 1;
8383 /* Give the object parameter a different name. */
8384 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8385 }
8386
8387 finish_member_declaration (fco);
8388
8389 obstack_free (&declarator_obstack, p);
8390
8391 return (fco != error_mark_node);
8392 }
8393 }
8394
8395 /* Parse the body of a lambda expression, which is simply
8396
8397 compound-statement
8398
8399 but which requires special handling.
8400 LAMBDA_EXPR is the current representation of the lambda expression. */
8401
8402 static void
8403 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8404 {
8405 bool nested = (current_function_decl != NULL_TREE);
8406 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8407 if (nested)
8408 push_function_context ();
8409 else
8410 /* Still increment function_depth so that we don't GC in the
8411 middle of an expression. */
8412 ++function_depth;
8413 /* Clear this in case we're in the middle of a default argument. */
8414 parser->local_variables_forbidden_p = false;
8415
8416 /* Finish the function call operator
8417 - class_specifier
8418 + late_parsing_for_member
8419 + function_definition_after_declarator
8420 + ctor_initializer_opt_and_function_body */
8421 {
8422 tree fco = lambda_function (lambda_expr);
8423 tree body;
8424 bool done = false;
8425 tree compound_stmt;
8426 tree cap;
8427
8428 /* Let the front end know that we are going to be defining this
8429 function. */
8430 start_preparsed_function (fco,
8431 NULL_TREE,
8432 SF_PRE_PARSED | SF_INCLASS_INLINE);
8433
8434 start_lambda_scope (fco);
8435 body = begin_function_body ();
8436
8437 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8438 goto out;
8439
8440 /* Push the proxies for any explicit captures. */
8441 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8442 cap = TREE_CHAIN (cap))
8443 build_capture_proxy (TREE_PURPOSE (cap));
8444
8445 compound_stmt = begin_compound_stmt (0);
8446
8447 /* 5.1.1.4 of the standard says:
8448 If a lambda-expression does not include a trailing-return-type, it
8449 is as if the trailing-return-type denotes the following type:
8450 * if the compound-statement is of the form
8451 { return attribute-specifier [opt] expression ; }
8452 the type of the returned expression after lvalue-to-rvalue
8453 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8454 (_conv.array_ 4.2), and function-to-pointer conversion
8455 (_conv.func_ 4.3);
8456 * otherwise, void. */
8457
8458 /* In a lambda that has neither a lambda-return-type-clause
8459 nor a deducible form, errors should be reported for return statements
8460 in the body. Since we used void as the placeholder return type, parsing
8461 the body as usual will give such desired behavior. */
8462 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8463 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8464 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8465 {
8466 tree expr = NULL_TREE;
8467 cp_id_kind idk = CP_ID_KIND_NONE;
8468
8469 /* Parse tentatively in case there's more after the initial return
8470 statement. */
8471 cp_parser_parse_tentatively (parser);
8472
8473 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8474
8475 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8476
8477 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8478 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8479
8480 if (cp_parser_parse_definitely (parser))
8481 {
8482 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8483
8484 /* Will get error here if type not deduced yet. */
8485 finish_return_stmt (expr);
8486
8487 done = true;
8488 }
8489 }
8490
8491 if (!done)
8492 {
8493 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8494 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8495 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8496 cp_parser_label_declaration (parser);
8497 cp_parser_statement_seq_opt (parser, NULL_TREE);
8498 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8499 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8500 }
8501
8502 finish_compound_stmt (compound_stmt);
8503
8504 out:
8505 finish_function_body (body);
8506 finish_lambda_scope ();
8507
8508 /* Finish the function and generate code for it if necessary. */
8509 expand_or_defer_fn (finish_function (/*inline*/2));
8510 }
8511
8512 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8513 if (nested)
8514 pop_function_context();
8515 else
8516 --function_depth;
8517 }
8518
8519 /* Statements [gram.stmt.stmt] */
8520
8521 /* Parse a statement.
8522
8523 statement:
8524 labeled-statement
8525 expression-statement
8526 compound-statement
8527 selection-statement
8528 iteration-statement
8529 jump-statement
8530 declaration-statement
8531 try-block
8532
8533 TM Extension:
8534
8535 statement:
8536 atomic-statement
8537
8538 IN_COMPOUND is true when the statement is nested inside a
8539 cp_parser_compound_statement; this matters for certain pragmas.
8540
8541 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8542 is a (possibly labeled) if statement which is not enclosed in braces
8543 and has an else clause. This is used to implement -Wparentheses. */
8544
8545 static void
8546 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8547 bool in_compound, bool *if_p)
8548 {
8549 tree statement;
8550 cp_token *token;
8551 location_t statement_location;
8552
8553 restart:
8554 if (if_p != NULL)
8555 *if_p = false;
8556 /* There is no statement yet. */
8557 statement = NULL_TREE;
8558 /* Peek at the next token. */
8559 token = cp_lexer_peek_token (parser->lexer);
8560 /* Remember the location of the first token in the statement. */
8561 statement_location = token->location;
8562 /* If this is a keyword, then that will often determine what kind of
8563 statement we have. */
8564 if (token->type == CPP_KEYWORD)
8565 {
8566 enum rid keyword = token->keyword;
8567
8568 switch (keyword)
8569 {
8570 case RID_CASE:
8571 case RID_DEFAULT:
8572 /* Looks like a labeled-statement with a case label.
8573 Parse the label, and then use tail recursion to parse
8574 the statement. */
8575 cp_parser_label_for_labeled_statement (parser);
8576 goto restart;
8577
8578 case RID_IF:
8579 case RID_SWITCH:
8580 statement = cp_parser_selection_statement (parser, if_p);
8581 break;
8582
8583 case RID_WHILE:
8584 case RID_DO:
8585 case RID_FOR:
8586 statement = cp_parser_iteration_statement (parser);
8587 break;
8588
8589 case RID_BREAK:
8590 case RID_CONTINUE:
8591 case RID_RETURN:
8592 case RID_GOTO:
8593 statement = cp_parser_jump_statement (parser);
8594 break;
8595
8596 /* Objective-C++ exception-handling constructs. */
8597 case RID_AT_TRY:
8598 case RID_AT_CATCH:
8599 case RID_AT_FINALLY:
8600 case RID_AT_SYNCHRONIZED:
8601 case RID_AT_THROW:
8602 statement = cp_parser_objc_statement (parser);
8603 break;
8604
8605 case RID_TRY:
8606 statement = cp_parser_try_block (parser);
8607 break;
8608
8609 case RID_NAMESPACE:
8610 /* This must be a namespace alias definition. */
8611 cp_parser_declaration_statement (parser);
8612 return;
8613
8614 case RID_TRANSACTION_ATOMIC:
8615 case RID_TRANSACTION_RELAXED:
8616 statement = cp_parser_transaction (parser, keyword);
8617 break;
8618 case RID_TRANSACTION_CANCEL:
8619 statement = cp_parser_transaction_cancel (parser);
8620 break;
8621
8622 default:
8623 /* It might be a keyword like `int' that can start a
8624 declaration-statement. */
8625 break;
8626 }
8627 }
8628 else if (token->type == CPP_NAME)
8629 {
8630 /* If the next token is a `:', then we are looking at a
8631 labeled-statement. */
8632 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8633 if (token->type == CPP_COLON)
8634 {
8635 /* Looks like a labeled-statement with an ordinary label.
8636 Parse the label, and then use tail recursion to parse
8637 the statement. */
8638 cp_parser_label_for_labeled_statement (parser);
8639 goto restart;
8640 }
8641 }
8642 /* Anything that starts with a `{' must be a compound-statement. */
8643 else if (token->type == CPP_OPEN_BRACE)
8644 statement = cp_parser_compound_statement (parser, NULL, false, false);
8645 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8646 a statement all its own. */
8647 else if (token->type == CPP_PRAGMA)
8648 {
8649 /* Only certain OpenMP pragmas are attached to statements, and thus
8650 are considered statements themselves. All others are not. In
8651 the context of a compound, accept the pragma as a "statement" and
8652 return so that we can check for a close brace. Otherwise we
8653 require a real statement and must go back and read one. */
8654 if (in_compound)
8655 cp_parser_pragma (parser, pragma_compound);
8656 else if (!cp_parser_pragma (parser, pragma_stmt))
8657 goto restart;
8658 return;
8659 }
8660 else if (token->type == CPP_EOF)
8661 {
8662 cp_parser_error (parser, "expected statement");
8663 return;
8664 }
8665
8666 /* Everything else must be a declaration-statement or an
8667 expression-statement. Try for the declaration-statement
8668 first, unless we are looking at a `;', in which case we know that
8669 we have an expression-statement. */
8670 if (!statement)
8671 {
8672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8673 {
8674 cp_parser_parse_tentatively (parser);
8675 /* Try to parse the declaration-statement. */
8676 cp_parser_declaration_statement (parser);
8677 /* If that worked, we're done. */
8678 if (cp_parser_parse_definitely (parser))
8679 return;
8680 }
8681 /* Look for an expression-statement instead. */
8682 statement = cp_parser_expression_statement (parser, in_statement_expr);
8683 }
8684
8685 /* Set the line number for the statement. */
8686 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8687 SET_EXPR_LOCATION (statement, statement_location);
8688 }
8689
8690 /* Parse the label for a labeled-statement, i.e.
8691
8692 identifier :
8693 case constant-expression :
8694 default :
8695
8696 GNU Extension:
8697 case constant-expression ... constant-expression : statement
8698
8699 When a label is parsed without errors, the label is added to the
8700 parse tree by the finish_* functions, so this function doesn't
8701 have to return the label. */
8702
8703 static void
8704 cp_parser_label_for_labeled_statement (cp_parser* parser)
8705 {
8706 cp_token *token;
8707 tree label = NULL_TREE;
8708 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8709
8710 /* The next token should be an identifier. */
8711 token = cp_lexer_peek_token (parser->lexer);
8712 if (token->type != CPP_NAME
8713 && token->type != CPP_KEYWORD)
8714 {
8715 cp_parser_error (parser, "expected labeled-statement");
8716 return;
8717 }
8718
8719 parser->colon_corrects_to_scope_p = false;
8720 switch (token->keyword)
8721 {
8722 case RID_CASE:
8723 {
8724 tree expr, expr_hi;
8725 cp_token *ellipsis;
8726
8727 /* Consume the `case' token. */
8728 cp_lexer_consume_token (parser->lexer);
8729 /* Parse the constant-expression. */
8730 expr = cp_parser_constant_expression (parser,
8731 /*allow_non_constant_p=*/false,
8732 NULL);
8733
8734 ellipsis = cp_lexer_peek_token (parser->lexer);
8735 if (ellipsis->type == CPP_ELLIPSIS)
8736 {
8737 /* Consume the `...' token. */
8738 cp_lexer_consume_token (parser->lexer);
8739 expr_hi =
8740 cp_parser_constant_expression (parser,
8741 /*allow_non_constant_p=*/false,
8742 NULL);
8743 /* We don't need to emit warnings here, as the common code
8744 will do this for us. */
8745 }
8746 else
8747 expr_hi = NULL_TREE;
8748
8749 if (parser->in_switch_statement_p)
8750 finish_case_label (token->location, expr, expr_hi);
8751 else
8752 error_at (token->location,
8753 "case label %qE not within a switch statement",
8754 expr);
8755 }
8756 break;
8757
8758 case RID_DEFAULT:
8759 /* Consume the `default' token. */
8760 cp_lexer_consume_token (parser->lexer);
8761
8762 if (parser->in_switch_statement_p)
8763 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8764 else
8765 error_at (token->location, "case label not within a switch statement");
8766 break;
8767
8768 default:
8769 /* Anything else must be an ordinary label. */
8770 label = finish_label_stmt (cp_parser_identifier (parser));
8771 break;
8772 }
8773
8774 /* Require the `:' token. */
8775 cp_parser_require (parser, CPP_COLON, RT_COLON);
8776
8777 /* An ordinary label may optionally be followed by attributes.
8778 However, this is only permitted if the attributes are then
8779 followed by a semicolon. This is because, for backward
8780 compatibility, when parsing
8781 lab: __attribute__ ((unused)) int i;
8782 we want the attribute to attach to "i", not "lab". */
8783 if (label != NULL_TREE
8784 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8785 {
8786 tree attrs;
8787
8788 cp_parser_parse_tentatively (parser);
8789 attrs = cp_parser_attributes_opt (parser);
8790 if (attrs == NULL_TREE
8791 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8792 cp_parser_abort_tentative_parse (parser);
8793 else if (!cp_parser_parse_definitely (parser))
8794 ;
8795 else
8796 cplus_decl_attributes (&label, attrs, 0);
8797 }
8798
8799 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8800 }
8801
8802 /* Parse an expression-statement.
8803
8804 expression-statement:
8805 expression [opt] ;
8806
8807 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8808 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8809 indicates whether this expression-statement is part of an
8810 expression statement. */
8811
8812 static tree
8813 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8814 {
8815 tree statement = NULL_TREE;
8816 cp_token *token = cp_lexer_peek_token (parser->lexer);
8817
8818 /* If the next token is a ';', then there is no expression
8819 statement. */
8820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8821 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8822
8823 /* Give a helpful message for "A<T>::type t;" and the like. */
8824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8825 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8826 {
8827 if (TREE_CODE (statement) == SCOPE_REF)
8828 error_at (token->location, "need %<typename%> before %qE because "
8829 "%qT is a dependent scope",
8830 statement, TREE_OPERAND (statement, 0));
8831 else if (is_overloaded_fn (statement)
8832 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8833 {
8834 /* A::A a; */
8835 tree fn = get_first_fn (statement);
8836 error_at (token->location,
8837 "%<%T::%D%> names the constructor, not the type",
8838 DECL_CONTEXT (fn), DECL_NAME (fn));
8839 }
8840 }
8841
8842 /* Consume the final `;'. */
8843 cp_parser_consume_semicolon_at_end_of_statement (parser);
8844
8845 if (in_statement_expr
8846 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8847 /* This is the final expression statement of a statement
8848 expression. */
8849 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8850 else if (statement)
8851 statement = finish_expr_stmt (statement);
8852 else
8853 finish_stmt ();
8854
8855 return statement;
8856 }
8857
8858 /* Parse a compound-statement.
8859
8860 compound-statement:
8861 { statement-seq [opt] }
8862
8863 GNU extension:
8864
8865 compound-statement:
8866 { label-declaration-seq [opt] statement-seq [opt] }
8867
8868 label-declaration-seq:
8869 label-declaration
8870 label-declaration-seq label-declaration
8871
8872 Returns a tree representing the statement. */
8873
8874 static tree
8875 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8876 bool in_try, bool function_body)
8877 {
8878 tree compound_stmt;
8879
8880 /* Consume the `{'. */
8881 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8882 return error_mark_node;
8883 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8884 && !function_body)
8885 pedwarn (input_location, OPT_pedantic,
8886 "compound-statement in constexpr function");
8887 /* Begin the compound-statement. */
8888 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8889 /* If the next keyword is `__label__' we have a label declaration. */
8890 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8891 cp_parser_label_declaration (parser);
8892 /* Parse an (optional) statement-seq. */
8893 cp_parser_statement_seq_opt (parser, in_statement_expr);
8894 /* Finish the compound-statement. */
8895 finish_compound_stmt (compound_stmt);
8896 /* Consume the `}'. */
8897 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8898
8899 return compound_stmt;
8900 }
8901
8902 /* Parse an (optional) statement-seq.
8903
8904 statement-seq:
8905 statement
8906 statement-seq [opt] statement */
8907
8908 static void
8909 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8910 {
8911 /* Scan statements until there aren't any more. */
8912 while (true)
8913 {
8914 cp_token *token = cp_lexer_peek_token (parser->lexer);
8915
8916 /* If we are looking at a `}', then we have run out of
8917 statements; the same is true if we have reached the end
8918 of file, or have stumbled upon a stray '@end'. */
8919 if (token->type == CPP_CLOSE_BRACE
8920 || token->type == CPP_EOF
8921 || token->type == CPP_PRAGMA_EOL
8922 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8923 break;
8924
8925 /* If we are in a compound statement and find 'else' then
8926 something went wrong. */
8927 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8928 {
8929 if (parser->in_statement & IN_IF_STMT)
8930 break;
8931 else
8932 {
8933 token = cp_lexer_consume_token (parser->lexer);
8934 error_at (token->location, "%<else%> without a previous %<if%>");
8935 }
8936 }
8937
8938 /* Parse the statement. */
8939 cp_parser_statement (parser, in_statement_expr, true, NULL);
8940 }
8941 }
8942
8943 /* Parse a selection-statement.
8944
8945 selection-statement:
8946 if ( condition ) statement
8947 if ( condition ) statement else statement
8948 switch ( condition ) statement
8949
8950 Returns the new IF_STMT or SWITCH_STMT.
8951
8952 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8953 is a (possibly labeled) if statement which is not enclosed in
8954 braces and has an else clause. This is used to implement
8955 -Wparentheses. */
8956
8957 static tree
8958 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8959 {
8960 cp_token *token;
8961 enum rid keyword;
8962
8963 if (if_p != NULL)
8964 *if_p = false;
8965
8966 /* Peek at the next token. */
8967 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8968
8969 /* See what kind of keyword it is. */
8970 keyword = token->keyword;
8971 switch (keyword)
8972 {
8973 case RID_IF:
8974 case RID_SWITCH:
8975 {
8976 tree statement;
8977 tree condition;
8978
8979 /* Look for the `('. */
8980 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8981 {
8982 cp_parser_skip_to_end_of_statement (parser);
8983 return error_mark_node;
8984 }
8985
8986 /* Begin the selection-statement. */
8987 if (keyword == RID_IF)
8988 statement = begin_if_stmt ();
8989 else
8990 statement = begin_switch_stmt ();
8991
8992 /* Parse the condition. */
8993 condition = cp_parser_condition (parser);
8994 /* Look for the `)'. */
8995 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8996 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8997 /*consume_paren=*/true);
8998
8999 if (keyword == RID_IF)
9000 {
9001 bool nested_if;
9002 unsigned char in_statement;
9003
9004 /* Add the condition. */
9005 finish_if_stmt_cond (condition, statement);
9006
9007 /* Parse the then-clause. */
9008 in_statement = parser->in_statement;
9009 parser->in_statement |= IN_IF_STMT;
9010 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9011 {
9012 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9013 add_stmt (build_empty_stmt (loc));
9014 cp_lexer_consume_token (parser->lexer);
9015 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9016 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9017 "empty body in an %<if%> statement");
9018 nested_if = false;
9019 }
9020 else
9021 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9022 parser->in_statement = in_statement;
9023
9024 finish_then_clause (statement);
9025
9026 /* If the next token is `else', parse the else-clause. */
9027 if (cp_lexer_next_token_is_keyword (parser->lexer,
9028 RID_ELSE))
9029 {
9030 /* Consume the `else' keyword. */
9031 cp_lexer_consume_token (parser->lexer);
9032 begin_else_clause (statement);
9033 /* Parse the else-clause. */
9034 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9035 {
9036 location_t loc;
9037 loc = cp_lexer_peek_token (parser->lexer)->location;
9038 warning_at (loc,
9039 OPT_Wempty_body, "suggest braces around "
9040 "empty body in an %<else%> statement");
9041 add_stmt (build_empty_stmt (loc));
9042 cp_lexer_consume_token (parser->lexer);
9043 }
9044 else
9045 cp_parser_implicitly_scoped_statement (parser, NULL);
9046
9047 finish_else_clause (statement);
9048
9049 /* If we are currently parsing a then-clause, then
9050 IF_P will not be NULL. We set it to true to
9051 indicate that this if statement has an else clause.
9052 This may trigger the Wparentheses warning below
9053 when we get back up to the parent if statement. */
9054 if (if_p != NULL)
9055 *if_p = true;
9056 }
9057 else
9058 {
9059 /* This if statement does not have an else clause. If
9060 NESTED_IF is true, then the then-clause is an if
9061 statement which does have an else clause. We warn
9062 about the potential ambiguity. */
9063 if (nested_if)
9064 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9065 "suggest explicit braces to avoid ambiguous"
9066 " %<else%>");
9067 }
9068
9069 /* Now we're all done with the if-statement. */
9070 finish_if_stmt (statement);
9071 }
9072 else
9073 {
9074 bool in_switch_statement_p;
9075 unsigned char in_statement;
9076
9077 /* Add the condition. */
9078 finish_switch_cond (condition, statement);
9079
9080 /* Parse the body of the switch-statement. */
9081 in_switch_statement_p = parser->in_switch_statement_p;
9082 in_statement = parser->in_statement;
9083 parser->in_switch_statement_p = true;
9084 parser->in_statement |= IN_SWITCH_STMT;
9085 cp_parser_implicitly_scoped_statement (parser, NULL);
9086 parser->in_switch_statement_p = in_switch_statement_p;
9087 parser->in_statement = in_statement;
9088
9089 /* Now we're all done with the switch-statement. */
9090 finish_switch_stmt (statement);
9091 }
9092
9093 return statement;
9094 }
9095 break;
9096
9097 default:
9098 cp_parser_error (parser, "expected selection-statement");
9099 return error_mark_node;
9100 }
9101 }
9102
9103 /* Parse a condition.
9104
9105 condition:
9106 expression
9107 type-specifier-seq declarator = initializer-clause
9108 type-specifier-seq declarator braced-init-list
9109
9110 GNU Extension:
9111
9112 condition:
9113 type-specifier-seq declarator asm-specification [opt]
9114 attributes [opt] = assignment-expression
9115
9116 Returns the expression that should be tested. */
9117
9118 static tree
9119 cp_parser_condition (cp_parser* parser)
9120 {
9121 cp_decl_specifier_seq type_specifiers;
9122 const char *saved_message;
9123 int declares_class_or_enum;
9124
9125 /* Try the declaration first. */
9126 cp_parser_parse_tentatively (parser);
9127 /* New types are not allowed in the type-specifier-seq for a
9128 condition. */
9129 saved_message = parser->type_definition_forbidden_message;
9130 parser->type_definition_forbidden_message
9131 = G_("types may not be defined in conditions");
9132 /* Parse the type-specifier-seq. */
9133 cp_parser_decl_specifier_seq (parser,
9134 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9135 &type_specifiers,
9136 &declares_class_or_enum);
9137 /* Restore the saved message. */
9138 parser->type_definition_forbidden_message = saved_message;
9139 /* If all is well, we might be looking at a declaration. */
9140 if (!cp_parser_error_occurred (parser))
9141 {
9142 tree decl;
9143 tree asm_specification;
9144 tree attributes;
9145 cp_declarator *declarator;
9146 tree initializer = NULL_TREE;
9147
9148 /* Parse the declarator. */
9149 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9150 /*ctor_dtor_or_conv_p=*/NULL,
9151 /*parenthesized_p=*/NULL,
9152 /*member_p=*/false);
9153 /* Parse the attributes. */
9154 attributes = cp_parser_attributes_opt (parser);
9155 /* Parse the asm-specification. */
9156 asm_specification = cp_parser_asm_specification_opt (parser);
9157 /* If the next token is not an `=' or '{', then we might still be
9158 looking at an expression. For example:
9159
9160 if (A(a).x)
9161
9162 looks like a decl-specifier-seq and a declarator -- but then
9163 there is no `=', so this is an expression. */
9164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9165 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9166 cp_parser_simulate_error (parser);
9167
9168 /* If we did see an `=' or '{', then we are looking at a declaration
9169 for sure. */
9170 if (cp_parser_parse_definitely (parser))
9171 {
9172 tree pushed_scope;
9173 bool non_constant_p;
9174 bool flags = LOOKUP_ONLYCONVERTING;
9175
9176 /* Create the declaration. */
9177 decl = start_decl (declarator, &type_specifiers,
9178 /*initialized_p=*/true,
9179 attributes, /*prefix_attributes=*/NULL_TREE,
9180 &pushed_scope);
9181
9182 /* Parse the initializer. */
9183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9184 {
9185 initializer = cp_parser_braced_list (parser, &non_constant_p);
9186 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9187 flags = 0;
9188 }
9189 else
9190 {
9191 /* Consume the `='. */
9192 cp_parser_require (parser, CPP_EQ, RT_EQ);
9193 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9194 }
9195 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9196 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9197
9198 /* Process the initializer. */
9199 cp_finish_decl (decl,
9200 initializer, !non_constant_p,
9201 asm_specification,
9202 flags);
9203
9204 if (pushed_scope)
9205 pop_scope (pushed_scope);
9206
9207 return convert_from_reference (decl);
9208 }
9209 }
9210 /* If we didn't even get past the declarator successfully, we are
9211 definitely not looking at a declaration. */
9212 else
9213 cp_parser_abort_tentative_parse (parser);
9214
9215 /* Otherwise, we are looking at an expression. */
9216 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9217 }
9218
9219 /* Parses a for-statement or range-for-statement until the closing ')',
9220 not included. */
9221
9222 static tree
9223 cp_parser_for (cp_parser *parser)
9224 {
9225 tree init, scope, decl;
9226 bool is_range_for;
9227
9228 /* Begin the for-statement. */
9229 scope = begin_for_scope (&init);
9230
9231 /* Parse the initialization. */
9232 is_range_for = cp_parser_for_init_statement (parser, &decl);
9233
9234 if (is_range_for)
9235 return cp_parser_range_for (parser, scope, init, decl);
9236 else
9237 return cp_parser_c_for (parser, scope, init);
9238 }
9239
9240 static tree
9241 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9242 {
9243 /* Normal for loop */
9244 tree condition = NULL_TREE;
9245 tree expression = NULL_TREE;
9246 tree stmt;
9247
9248 stmt = begin_for_stmt (scope, init);
9249 /* The for-init-statement has already been parsed in
9250 cp_parser_for_init_statement, so no work is needed here. */
9251 finish_for_init_stmt (stmt);
9252
9253 /* If there's a condition, process it. */
9254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9255 condition = cp_parser_condition (parser);
9256 finish_for_cond (condition, stmt);
9257 /* Look for the `;'. */
9258 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9259
9260 /* If there's an expression, process it. */
9261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9262 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9263 finish_for_expr (expression, stmt);
9264
9265 return stmt;
9266 }
9267
9268 /* Tries to parse a range-based for-statement:
9269
9270 range-based-for:
9271 decl-specifier-seq declarator : expression
9272
9273 The decl-specifier-seq declarator and the `:' are already parsed by
9274 cp_parser_for_init_statement. If processing_template_decl it returns a
9275 newly created RANGE_FOR_STMT; if not, it is converted to a
9276 regular FOR_STMT. */
9277
9278 static tree
9279 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9280 {
9281 tree stmt, range_expr;
9282
9283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9284 {
9285 bool expr_non_constant_p;
9286 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9287 }
9288 else
9289 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9290
9291 /* If in template, STMT is converted to a normal for-statement
9292 at instantiation. If not, it is done just ahead. */
9293 if (processing_template_decl)
9294 {
9295 stmt = begin_range_for_stmt (scope, init);
9296 finish_range_for_decl (stmt, range_decl, range_expr);
9297 if (!type_dependent_expression_p (range_expr)
9298 /* do_auto_deduction doesn't mess with template init-lists. */
9299 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9300 do_range_for_auto_deduction (range_decl, range_expr);
9301 }
9302 else
9303 {
9304 stmt = begin_for_stmt (scope, init);
9305 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9306 }
9307 return stmt;
9308 }
9309
9310 /* Subroutine of cp_convert_range_for: given the initializer expression,
9311 builds up the range temporary. */
9312
9313 static tree
9314 build_range_temp (tree range_expr)
9315 {
9316 tree range_type, range_temp;
9317
9318 /* Find out the type deduced by the declaration
9319 `auto &&__range = range_expr'. */
9320 range_type = cp_build_reference_type (make_auto (), true);
9321 range_type = do_auto_deduction (range_type, range_expr,
9322 type_uses_auto (range_type));
9323
9324 /* Create the __range variable. */
9325 range_temp = build_decl (input_location, VAR_DECL,
9326 get_identifier ("__for_range"), range_type);
9327 TREE_USED (range_temp) = 1;
9328 DECL_ARTIFICIAL (range_temp) = 1;
9329
9330 return range_temp;
9331 }
9332
9333 /* Used by cp_parser_range_for in template context: we aren't going to
9334 do a full conversion yet, but we still need to resolve auto in the
9335 type of the for-range-declaration if present. This is basically
9336 a shortcut version of cp_convert_range_for. */
9337
9338 static void
9339 do_range_for_auto_deduction (tree decl, tree range_expr)
9340 {
9341 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9342 if (auto_node)
9343 {
9344 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9345 range_temp = convert_from_reference (build_range_temp (range_expr));
9346 iter_type = (cp_parser_perform_range_for_lookup
9347 (range_temp, &begin_dummy, &end_dummy));
9348 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9349 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9350 tf_warning_or_error);
9351 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9352 iter_decl, auto_node);
9353 }
9354 }
9355
9356 /* Converts a range-based for-statement into a normal
9357 for-statement, as per the definition.
9358
9359 for (RANGE_DECL : RANGE_EXPR)
9360 BLOCK
9361
9362 should be equivalent to:
9363
9364 {
9365 auto &&__range = RANGE_EXPR;
9366 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9367 __begin != __end;
9368 ++__begin)
9369 {
9370 RANGE_DECL = *__begin;
9371 BLOCK
9372 }
9373 }
9374
9375 If RANGE_EXPR is an array:
9376 BEGIN_EXPR = __range
9377 END_EXPR = __range + ARRAY_SIZE(__range)
9378 Else if RANGE_EXPR has a member 'begin' or 'end':
9379 BEGIN_EXPR = __range.begin()
9380 END_EXPR = __range.end()
9381 Else:
9382 BEGIN_EXPR = begin(__range)
9383 END_EXPR = end(__range);
9384
9385 If __range has a member 'begin' but not 'end', or vice versa, we must
9386 still use the second alternative (it will surely fail, however).
9387 When calling begin()/end() in the third alternative we must use
9388 argument dependent lookup, but always considering 'std' as an associated
9389 namespace. */
9390
9391 tree
9392 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9393 {
9394 tree begin, end;
9395 tree iter_type, begin_expr, end_expr;
9396 tree condition, expression;
9397
9398 if (range_decl == error_mark_node || range_expr == error_mark_node)
9399 /* If an error happened previously do nothing or else a lot of
9400 unhelpful errors would be issued. */
9401 begin_expr = end_expr = iter_type = error_mark_node;
9402 else
9403 {
9404 tree range_temp = build_range_temp (range_expr);
9405 pushdecl (range_temp);
9406 cp_finish_decl (range_temp, range_expr,
9407 /*is_constant_init*/false, NULL_TREE,
9408 LOOKUP_ONLYCONVERTING);
9409
9410 range_temp = convert_from_reference (range_temp);
9411 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9412 &begin_expr, &end_expr);
9413 }
9414
9415 /* The new for initialization statement. */
9416 begin = build_decl (input_location, VAR_DECL,
9417 get_identifier ("__for_begin"), iter_type);
9418 TREE_USED (begin) = 1;
9419 DECL_ARTIFICIAL (begin) = 1;
9420 pushdecl (begin);
9421 cp_finish_decl (begin, begin_expr,
9422 /*is_constant_init*/false, NULL_TREE,
9423 LOOKUP_ONLYCONVERTING);
9424
9425 end = build_decl (input_location, VAR_DECL,
9426 get_identifier ("__for_end"), iter_type);
9427 TREE_USED (end) = 1;
9428 DECL_ARTIFICIAL (end) = 1;
9429 pushdecl (end);
9430 cp_finish_decl (end, end_expr,
9431 /*is_constant_init*/false, NULL_TREE,
9432 LOOKUP_ONLYCONVERTING);
9433
9434 finish_for_init_stmt (statement);
9435
9436 /* The new for condition. */
9437 condition = build_x_binary_op (NE_EXPR,
9438 begin, ERROR_MARK,
9439 end, ERROR_MARK,
9440 NULL, tf_warning_or_error);
9441 finish_for_cond (condition, statement);
9442
9443 /* The new increment expression. */
9444 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9445 finish_for_expr (expression, statement);
9446
9447 /* The declaration is initialized with *__begin inside the loop body. */
9448 cp_finish_decl (range_decl,
9449 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9450 /*is_constant_init*/false, NULL_TREE,
9451 LOOKUP_ONLYCONVERTING);
9452
9453 return statement;
9454 }
9455
9456 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9457 We need to solve both at the same time because the method used
9458 depends on the existence of members begin or end.
9459 Returns the type deduced for the iterator expression. */
9460
9461 static tree
9462 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9463 {
9464 if (error_operand_p (range))
9465 {
9466 *begin = *end = error_mark_node;
9467 return error_mark_node;
9468 }
9469
9470 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9471 {
9472 error ("range-based %<for%> expression of type %qT "
9473 "has incomplete type", TREE_TYPE (range));
9474 *begin = *end = error_mark_node;
9475 return error_mark_node;
9476 }
9477 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9478 {
9479 /* If RANGE is an array, we will use pointer arithmetic. */
9480 *begin = range;
9481 *end = build_binary_op (input_location, PLUS_EXPR,
9482 range,
9483 array_type_nelts_top (TREE_TYPE (range)),
9484 0);
9485 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9486 }
9487 else
9488 {
9489 /* If it is not an array, we must do a bit of magic. */
9490 tree id_begin, id_end;
9491 tree member_begin, member_end;
9492
9493 *begin = *end = error_mark_node;
9494
9495 id_begin = get_identifier ("begin");
9496 id_end = get_identifier ("end");
9497 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9498 /*protect=*/2, /*want_type=*/false,
9499 tf_warning_or_error);
9500 member_end = lookup_member (TREE_TYPE (range), id_end,
9501 /*protect=*/2, /*want_type=*/false,
9502 tf_warning_or_error);
9503
9504 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9505 {
9506 /* Use the member functions. */
9507 if (member_begin != NULL_TREE)
9508 *begin = cp_parser_range_for_member_function (range, id_begin);
9509 else
9510 error ("range-based %<for%> expression of type %qT has an "
9511 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9512
9513 if (member_end != NULL_TREE)
9514 *end = cp_parser_range_for_member_function (range, id_end);
9515 else
9516 error ("range-based %<for%> expression of type %qT has a "
9517 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9518 }
9519 else
9520 {
9521 /* Use global functions with ADL. */
9522 VEC(tree,gc) *vec;
9523 vec = make_tree_vector ();
9524
9525 VEC_safe_push (tree, gc, vec, range);
9526
9527 member_begin = perform_koenig_lookup (id_begin, vec,
9528 /*include_std=*/true,
9529 tf_warning_or_error);
9530 *begin = finish_call_expr (member_begin, &vec, false, true,
9531 tf_warning_or_error);
9532 member_end = perform_koenig_lookup (id_end, vec,
9533 /*include_std=*/true,
9534 tf_warning_or_error);
9535 *end = finish_call_expr (member_end, &vec, false, true,
9536 tf_warning_or_error);
9537
9538 release_tree_vector (vec);
9539 }
9540
9541 /* Last common checks. */
9542 if (*begin == error_mark_node || *end == error_mark_node)
9543 {
9544 /* If one of the expressions is an error do no more checks. */
9545 *begin = *end = error_mark_node;
9546 return error_mark_node;
9547 }
9548 else
9549 {
9550 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9551 /* The unqualified type of the __begin and __end temporaries should
9552 be the same, as required by the multiple auto declaration. */
9553 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9554 error ("inconsistent begin/end types in range-based %<for%> "
9555 "statement: %qT and %qT",
9556 TREE_TYPE (*begin), TREE_TYPE (*end));
9557 return iter_type;
9558 }
9559 }
9560 }
9561
9562 /* Helper function for cp_parser_perform_range_for_lookup.
9563 Builds a tree for RANGE.IDENTIFIER(). */
9564
9565 static tree
9566 cp_parser_range_for_member_function (tree range, tree identifier)
9567 {
9568 tree member, res;
9569 VEC(tree,gc) *vec;
9570
9571 member = finish_class_member_access_expr (range, identifier,
9572 false, tf_warning_or_error);
9573 if (member == error_mark_node)
9574 return error_mark_node;
9575
9576 vec = make_tree_vector ();
9577 res = finish_call_expr (member, &vec,
9578 /*disallow_virtual=*/false,
9579 /*koenig_p=*/false,
9580 tf_warning_or_error);
9581 release_tree_vector (vec);
9582 return res;
9583 }
9584
9585 /* Parse an iteration-statement.
9586
9587 iteration-statement:
9588 while ( condition ) statement
9589 do statement while ( expression ) ;
9590 for ( for-init-statement condition [opt] ; expression [opt] )
9591 statement
9592
9593 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9594
9595 static tree
9596 cp_parser_iteration_statement (cp_parser* parser)
9597 {
9598 cp_token *token;
9599 enum rid keyword;
9600 tree statement;
9601 unsigned char in_statement;
9602
9603 /* Peek at the next token. */
9604 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9605 if (!token)
9606 return error_mark_node;
9607
9608 /* Remember whether or not we are already within an iteration
9609 statement. */
9610 in_statement = parser->in_statement;
9611
9612 /* See what kind of keyword it is. */
9613 keyword = token->keyword;
9614 switch (keyword)
9615 {
9616 case RID_WHILE:
9617 {
9618 tree condition;
9619
9620 /* Begin the while-statement. */
9621 statement = begin_while_stmt ();
9622 /* Look for the `('. */
9623 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9624 /* Parse the condition. */
9625 condition = cp_parser_condition (parser);
9626 finish_while_stmt_cond (condition, statement);
9627 /* Look for the `)'. */
9628 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9629 /* Parse the dependent statement. */
9630 parser->in_statement = IN_ITERATION_STMT;
9631 cp_parser_already_scoped_statement (parser);
9632 parser->in_statement = in_statement;
9633 /* We're done with the while-statement. */
9634 finish_while_stmt (statement);
9635 }
9636 break;
9637
9638 case RID_DO:
9639 {
9640 tree expression;
9641
9642 /* Begin the do-statement. */
9643 statement = begin_do_stmt ();
9644 /* Parse the body of the do-statement. */
9645 parser->in_statement = IN_ITERATION_STMT;
9646 cp_parser_implicitly_scoped_statement (parser, NULL);
9647 parser->in_statement = in_statement;
9648 finish_do_body (statement);
9649 /* Look for the `while' keyword. */
9650 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9651 /* Look for the `('. */
9652 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9653 /* Parse the expression. */
9654 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9655 /* We're done with the do-statement. */
9656 finish_do_stmt (expression, statement);
9657 /* Look for the `)'. */
9658 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9659 /* Look for the `;'. */
9660 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9661 }
9662 break;
9663
9664 case RID_FOR:
9665 {
9666 /* Look for the `('. */
9667 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9668
9669 statement = cp_parser_for (parser);
9670
9671 /* Look for the `)'. */
9672 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9673
9674 /* Parse the body of the for-statement. */
9675 parser->in_statement = IN_ITERATION_STMT;
9676 cp_parser_already_scoped_statement (parser);
9677 parser->in_statement = in_statement;
9678
9679 /* We're done with the for-statement. */
9680 finish_for_stmt (statement);
9681 }
9682 break;
9683
9684 default:
9685 cp_parser_error (parser, "expected iteration-statement");
9686 statement = error_mark_node;
9687 break;
9688 }
9689
9690 return statement;
9691 }
9692
9693 /* Parse a for-init-statement or the declarator of a range-based-for.
9694 Returns true if a range-based-for declaration is seen.
9695
9696 for-init-statement:
9697 expression-statement
9698 simple-declaration */
9699
9700 static bool
9701 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9702 {
9703 /* If the next token is a `;', then we have an empty
9704 expression-statement. Grammatically, this is also a
9705 simple-declaration, but an invalid one, because it does not
9706 declare anything. Therefore, if we did not handle this case
9707 specially, we would issue an error message about an invalid
9708 declaration. */
9709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9710 {
9711 bool is_range_for = false;
9712 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9713
9714 parser->colon_corrects_to_scope_p = false;
9715
9716 /* We're going to speculatively look for a declaration, falling back
9717 to an expression, if necessary. */
9718 cp_parser_parse_tentatively (parser);
9719 /* Parse the declaration. */
9720 cp_parser_simple_declaration (parser,
9721 /*function_definition_allowed_p=*/false,
9722 decl);
9723 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9724 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9725 {
9726 /* It is a range-for, consume the ':' */
9727 cp_lexer_consume_token (parser->lexer);
9728 is_range_for = true;
9729 if (cxx_dialect < cxx0x)
9730 {
9731 error_at (cp_lexer_peek_token (parser->lexer)->location,
9732 "range-based %<for%> loops are not allowed "
9733 "in C++98 mode");
9734 *decl = error_mark_node;
9735 }
9736 }
9737 else
9738 /* The ';' is not consumed yet because we told
9739 cp_parser_simple_declaration not to. */
9740 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9741
9742 if (cp_parser_parse_definitely (parser))
9743 return is_range_for;
9744 /* If the tentative parse failed, then we shall need to look for an
9745 expression-statement. */
9746 }
9747 /* If we are here, it is an expression-statement. */
9748 cp_parser_expression_statement (parser, NULL_TREE);
9749 return false;
9750 }
9751
9752 /* Parse a jump-statement.
9753
9754 jump-statement:
9755 break ;
9756 continue ;
9757 return expression [opt] ;
9758 return braced-init-list ;
9759 goto identifier ;
9760
9761 GNU extension:
9762
9763 jump-statement:
9764 goto * expression ;
9765
9766 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9767
9768 static tree
9769 cp_parser_jump_statement (cp_parser* parser)
9770 {
9771 tree statement = error_mark_node;
9772 cp_token *token;
9773 enum rid keyword;
9774 unsigned char in_statement;
9775
9776 /* Peek at the next token. */
9777 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9778 if (!token)
9779 return error_mark_node;
9780
9781 /* See what kind of keyword it is. */
9782 keyword = token->keyword;
9783 switch (keyword)
9784 {
9785 case RID_BREAK:
9786 in_statement = parser->in_statement & ~IN_IF_STMT;
9787 switch (in_statement)
9788 {
9789 case 0:
9790 error_at (token->location, "break statement not within loop or switch");
9791 break;
9792 default:
9793 gcc_assert ((in_statement & IN_SWITCH_STMT)
9794 || in_statement == IN_ITERATION_STMT);
9795 statement = finish_break_stmt ();
9796 break;
9797 case IN_OMP_BLOCK:
9798 error_at (token->location, "invalid exit from OpenMP structured block");
9799 break;
9800 case IN_OMP_FOR:
9801 error_at (token->location, "break statement used with OpenMP for loop");
9802 break;
9803 }
9804 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9805 break;
9806
9807 case RID_CONTINUE:
9808 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9809 {
9810 case 0:
9811 error_at (token->location, "continue statement not within a loop");
9812 break;
9813 case IN_ITERATION_STMT:
9814 case IN_OMP_FOR:
9815 statement = finish_continue_stmt ();
9816 break;
9817 case IN_OMP_BLOCK:
9818 error_at (token->location, "invalid exit from OpenMP structured block");
9819 break;
9820 default:
9821 gcc_unreachable ();
9822 }
9823 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9824 break;
9825
9826 case RID_RETURN:
9827 {
9828 tree expr;
9829 bool expr_non_constant_p;
9830
9831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9832 {
9833 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9834 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9835 }
9836 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9837 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9838 else
9839 /* If the next token is a `;', then there is no
9840 expression. */
9841 expr = NULL_TREE;
9842 /* Build the return-statement. */
9843 statement = finish_return_stmt (expr);
9844 /* Look for the final `;'. */
9845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9846 }
9847 break;
9848
9849 case RID_GOTO:
9850 /* Create the goto-statement. */
9851 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9852 {
9853 /* Issue a warning about this use of a GNU extension. */
9854 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9855 /* Consume the '*' token. */
9856 cp_lexer_consume_token (parser->lexer);
9857 /* Parse the dependent expression. */
9858 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9859 }
9860 else
9861 finish_goto_stmt (cp_parser_identifier (parser));
9862 /* Look for the final `;'. */
9863 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9864 break;
9865
9866 default:
9867 cp_parser_error (parser, "expected jump-statement");
9868 break;
9869 }
9870
9871 return statement;
9872 }
9873
9874 /* Parse a declaration-statement.
9875
9876 declaration-statement:
9877 block-declaration */
9878
9879 static void
9880 cp_parser_declaration_statement (cp_parser* parser)
9881 {
9882 void *p;
9883
9884 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9885 p = obstack_alloc (&declarator_obstack, 0);
9886
9887 /* Parse the block-declaration. */
9888 cp_parser_block_declaration (parser, /*statement_p=*/true);
9889
9890 /* Free any declarators allocated. */
9891 obstack_free (&declarator_obstack, p);
9892
9893 /* Finish off the statement. */
9894 finish_stmt ();
9895 }
9896
9897 /* Some dependent statements (like `if (cond) statement'), are
9898 implicitly in their own scope. In other words, if the statement is
9899 a single statement (as opposed to a compound-statement), it is
9900 none-the-less treated as if it were enclosed in braces. Any
9901 declarations appearing in the dependent statement are out of scope
9902 after control passes that point. This function parses a statement,
9903 but ensures that is in its own scope, even if it is not a
9904 compound-statement.
9905
9906 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9907 is a (possibly labeled) if statement which is not enclosed in
9908 braces and has an else clause. This is used to implement
9909 -Wparentheses.
9910
9911 Returns the new statement. */
9912
9913 static tree
9914 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9915 {
9916 tree statement;
9917
9918 if (if_p != NULL)
9919 *if_p = false;
9920
9921 /* Mark if () ; with a special NOP_EXPR. */
9922 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9923 {
9924 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9925 cp_lexer_consume_token (parser->lexer);
9926 statement = add_stmt (build_empty_stmt (loc));
9927 }
9928 /* if a compound is opened, we simply parse the statement directly. */
9929 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9930 statement = cp_parser_compound_statement (parser, NULL, false, false);
9931 /* If the token is not a `{', then we must take special action. */
9932 else
9933 {
9934 /* Create a compound-statement. */
9935 statement = begin_compound_stmt (0);
9936 /* Parse the dependent-statement. */
9937 cp_parser_statement (parser, NULL_TREE, false, if_p);
9938 /* Finish the dummy compound-statement. */
9939 finish_compound_stmt (statement);
9940 }
9941
9942 /* Return the statement. */
9943 return statement;
9944 }
9945
9946 /* For some dependent statements (like `while (cond) statement'), we
9947 have already created a scope. Therefore, even if the dependent
9948 statement is a compound-statement, we do not want to create another
9949 scope. */
9950
9951 static void
9952 cp_parser_already_scoped_statement (cp_parser* parser)
9953 {
9954 /* If the token is a `{', then we must take special action. */
9955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9956 cp_parser_statement (parser, NULL_TREE, false, NULL);
9957 else
9958 {
9959 /* Avoid calling cp_parser_compound_statement, so that we
9960 don't create a new scope. Do everything else by hand. */
9961 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9962 /* If the next keyword is `__label__' we have a label declaration. */
9963 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9964 cp_parser_label_declaration (parser);
9965 /* Parse an (optional) statement-seq. */
9966 cp_parser_statement_seq_opt (parser, NULL_TREE);
9967 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9968 }
9969 }
9970
9971 /* Declarations [gram.dcl.dcl] */
9972
9973 /* Parse an optional declaration-sequence.
9974
9975 declaration-seq:
9976 declaration
9977 declaration-seq declaration */
9978
9979 static void
9980 cp_parser_declaration_seq_opt (cp_parser* parser)
9981 {
9982 while (true)
9983 {
9984 cp_token *token;
9985
9986 token = cp_lexer_peek_token (parser->lexer);
9987
9988 if (token->type == CPP_CLOSE_BRACE
9989 || token->type == CPP_EOF
9990 || token->type == CPP_PRAGMA_EOL)
9991 break;
9992
9993 if (token->type == CPP_SEMICOLON)
9994 {
9995 /* A declaration consisting of a single semicolon is
9996 invalid. Allow it unless we're being pedantic. */
9997 cp_lexer_consume_token (parser->lexer);
9998 if (!in_system_header)
9999 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10000 continue;
10001 }
10002
10003 /* If we're entering or exiting a region that's implicitly
10004 extern "C", modify the lang context appropriately. */
10005 if (!parser->implicit_extern_c && token->implicit_extern_c)
10006 {
10007 push_lang_context (lang_name_c);
10008 parser->implicit_extern_c = true;
10009 }
10010 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10011 {
10012 pop_lang_context ();
10013 parser->implicit_extern_c = false;
10014 }
10015
10016 if (token->type == CPP_PRAGMA)
10017 {
10018 /* A top-level declaration can consist solely of a #pragma.
10019 A nested declaration cannot, so this is done here and not
10020 in cp_parser_declaration. (A #pragma at block scope is
10021 handled in cp_parser_statement.) */
10022 cp_parser_pragma (parser, pragma_external);
10023 continue;
10024 }
10025
10026 /* Parse the declaration itself. */
10027 cp_parser_declaration (parser);
10028 }
10029 }
10030
10031 /* Parse a declaration.
10032
10033 declaration:
10034 block-declaration
10035 function-definition
10036 template-declaration
10037 explicit-instantiation
10038 explicit-specialization
10039 linkage-specification
10040 namespace-definition
10041
10042 GNU extension:
10043
10044 declaration:
10045 __extension__ declaration */
10046
10047 static void
10048 cp_parser_declaration (cp_parser* parser)
10049 {
10050 cp_token token1;
10051 cp_token token2;
10052 int saved_pedantic;
10053 void *p;
10054 tree attributes = NULL_TREE;
10055
10056 /* Check for the `__extension__' keyword. */
10057 if (cp_parser_extension_opt (parser, &saved_pedantic))
10058 {
10059 /* Parse the qualified declaration. */
10060 cp_parser_declaration (parser);
10061 /* Restore the PEDANTIC flag. */
10062 pedantic = saved_pedantic;
10063
10064 return;
10065 }
10066
10067 /* Try to figure out what kind of declaration is present. */
10068 token1 = *cp_lexer_peek_token (parser->lexer);
10069
10070 if (token1.type != CPP_EOF)
10071 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10072 else
10073 {
10074 token2.type = CPP_EOF;
10075 token2.keyword = RID_MAX;
10076 }
10077
10078 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10079 p = obstack_alloc (&declarator_obstack, 0);
10080
10081 /* If the next token is `extern' and the following token is a string
10082 literal, then we have a linkage specification. */
10083 if (token1.keyword == RID_EXTERN
10084 && cp_parser_is_pure_string_literal (&token2))
10085 cp_parser_linkage_specification (parser);
10086 /* If the next token is `template', then we have either a template
10087 declaration, an explicit instantiation, or an explicit
10088 specialization. */
10089 else if (token1.keyword == RID_TEMPLATE)
10090 {
10091 /* `template <>' indicates a template specialization. */
10092 if (token2.type == CPP_LESS
10093 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10094 cp_parser_explicit_specialization (parser);
10095 /* `template <' indicates a template declaration. */
10096 else if (token2.type == CPP_LESS)
10097 cp_parser_template_declaration (parser, /*member_p=*/false);
10098 /* Anything else must be an explicit instantiation. */
10099 else
10100 cp_parser_explicit_instantiation (parser);
10101 }
10102 /* If the next token is `export', then we have a template
10103 declaration. */
10104 else if (token1.keyword == RID_EXPORT)
10105 cp_parser_template_declaration (parser, /*member_p=*/false);
10106 /* If the next token is `extern', 'static' or 'inline' and the one
10107 after that is `template', we have a GNU extended explicit
10108 instantiation directive. */
10109 else if (cp_parser_allow_gnu_extensions_p (parser)
10110 && (token1.keyword == RID_EXTERN
10111 || token1.keyword == RID_STATIC
10112 || token1.keyword == RID_INLINE)
10113 && token2.keyword == RID_TEMPLATE)
10114 cp_parser_explicit_instantiation (parser);
10115 /* If the next token is `namespace', check for a named or unnamed
10116 namespace definition. */
10117 else if (token1.keyword == RID_NAMESPACE
10118 && (/* A named namespace definition. */
10119 (token2.type == CPP_NAME
10120 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10121 != CPP_EQ))
10122 /* An unnamed namespace definition. */
10123 || token2.type == CPP_OPEN_BRACE
10124 || token2.keyword == RID_ATTRIBUTE))
10125 cp_parser_namespace_definition (parser);
10126 /* An inline (associated) namespace definition. */
10127 else if (token1.keyword == RID_INLINE
10128 && token2.keyword == RID_NAMESPACE)
10129 cp_parser_namespace_definition (parser);
10130 /* Objective-C++ declaration/definition. */
10131 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10132 cp_parser_objc_declaration (parser, NULL_TREE);
10133 else if (c_dialect_objc ()
10134 && token1.keyword == RID_ATTRIBUTE
10135 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10136 cp_parser_objc_declaration (parser, attributes);
10137 /* We must have either a block declaration or a function
10138 definition. */
10139 else
10140 /* Try to parse a block-declaration, or a function-definition. */
10141 cp_parser_block_declaration (parser, /*statement_p=*/false);
10142
10143 /* Free any declarators allocated. */
10144 obstack_free (&declarator_obstack, p);
10145 }
10146
10147 /* Parse a block-declaration.
10148
10149 block-declaration:
10150 simple-declaration
10151 asm-definition
10152 namespace-alias-definition
10153 using-declaration
10154 using-directive
10155
10156 GNU Extension:
10157
10158 block-declaration:
10159 __extension__ block-declaration
10160
10161 C++0x Extension:
10162
10163 block-declaration:
10164 static_assert-declaration
10165
10166 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10167 part of a declaration-statement. */
10168
10169 static void
10170 cp_parser_block_declaration (cp_parser *parser,
10171 bool statement_p)
10172 {
10173 cp_token *token1;
10174 int saved_pedantic;
10175
10176 /* Check for the `__extension__' keyword. */
10177 if (cp_parser_extension_opt (parser, &saved_pedantic))
10178 {
10179 /* Parse the qualified declaration. */
10180 cp_parser_block_declaration (parser, statement_p);
10181 /* Restore the PEDANTIC flag. */
10182 pedantic = saved_pedantic;
10183
10184 return;
10185 }
10186
10187 /* Peek at the next token to figure out which kind of declaration is
10188 present. */
10189 token1 = cp_lexer_peek_token (parser->lexer);
10190
10191 /* If the next keyword is `asm', we have an asm-definition. */
10192 if (token1->keyword == RID_ASM)
10193 {
10194 if (statement_p)
10195 cp_parser_commit_to_tentative_parse (parser);
10196 cp_parser_asm_definition (parser);
10197 }
10198 /* If the next keyword is `namespace', we have a
10199 namespace-alias-definition. */
10200 else if (token1->keyword == RID_NAMESPACE)
10201 cp_parser_namespace_alias_definition (parser);
10202 /* If the next keyword is `using', we have a
10203 using-declaration, a using-directive, or an alias-declaration. */
10204 else if (token1->keyword == RID_USING)
10205 {
10206 cp_token *token2;
10207
10208 if (statement_p)
10209 cp_parser_commit_to_tentative_parse (parser);
10210 /* If the token after `using' is `namespace', then we have a
10211 using-directive. */
10212 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10213 if (token2->keyword == RID_NAMESPACE)
10214 cp_parser_using_directive (parser);
10215 /* If the second token after 'using' is '=', then we have an
10216 alias-declaration. */
10217 else if (cxx_dialect >= cxx0x
10218 && token2->type == CPP_NAME
10219 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10220 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10221 == RID_ATTRIBUTE)))
10222 cp_parser_alias_declaration (parser);
10223 /* Otherwise, it's a using-declaration. */
10224 else
10225 cp_parser_using_declaration (parser,
10226 /*access_declaration_p=*/false);
10227 }
10228 /* If the next keyword is `__label__' we have a misplaced label
10229 declaration. */
10230 else if (token1->keyword == RID_LABEL)
10231 {
10232 cp_lexer_consume_token (parser->lexer);
10233 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10234 cp_parser_skip_to_end_of_statement (parser);
10235 /* If the next token is now a `;', consume it. */
10236 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10237 cp_lexer_consume_token (parser->lexer);
10238 }
10239 /* If the next token is `static_assert' we have a static assertion. */
10240 else if (token1->keyword == RID_STATIC_ASSERT)
10241 cp_parser_static_assert (parser, /*member_p=*/false);
10242 /* Anything else must be a simple-declaration. */
10243 else
10244 cp_parser_simple_declaration (parser, !statement_p,
10245 /*maybe_range_for_decl*/NULL);
10246 }
10247
10248 /* Parse a simple-declaration.
10249
10250 simple-declaration:
10251 decl-specifier-seq [opt] init-declarator-list [opt] ;
10252
10253 init-declarator-list:
10254 init-declarator
10255 init-declarator-list , init-declarator
10256
10257 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10258 function-definition as a simple-declaration.
10259
10260 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10261 parsed declaration if it is an uninitialized single declarator not followed
10262 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10263 if present, will not be consumed. */
10264
10265 static void
10266 cp_parser_simple_declaration (cp_parser* parser,
10267 bool function_definition_allowed_p,
10268 tree *maybe_range_for_decl)
10269 {
10270 cp_decl_specifier_seq decl_specifiers;
10271 int declares_class_or_enum;
10272 bool saw_declarator;
10273
10274 if (maybe_range_for_decl)
10275 *maybe_range_for_decl = NULL_TREE;
10276
10277 /* Defer access checks until we know what is being declared; the
10278 checks for names appearing in the decl-specifier-seq should be
10279 done as if we were in the scope of the thing being declared. */
10280 push_deferring_access_checks (dk_deferred);
10281
10282 /* Parse the decl-specifier-seq. We have to keep track of whether
10283 or not the decl-specifier-seq declares a named class or
10284 enumeration type, since that is the only case in which the
10285 init-declarator-list is allowed to be empty.
10286
10287 [dcl.dcl]
10288
10289 In a simple-declaration, the optional init-declarator-list can be
10290 omitted only when declaring a class or enumeration, that is when
10291 the decl-specifier-seq contains either a class-specifier, an
10292 elaborated-type-specifier, or an enum-specifier. */
10293 cp_parser_decl_specifier_seq (parser,
10294 CP_PARSER_FLAGS_OPTIONAL,
10295 &decl_specifiers,
10296 &declares_class_or_enum);
10297 /* We no longer need to defer access checks. */
10298 stop_deferring_access_checks ();
10299
10300 /* In a block scope, a valid declaration must always have a
10301 decl-specifier-seq. By not trying to parse declarators, we can
10302 resolve the declaration/expression ambiguity more quickly. */
10303 if (!function_definition_allowed_p
10304 && !decl_specifiers.any_specifiers_p)
10305 {
10306 cp_parser_error (parser, "expected declaration");
10307 goto done;
10308 }
10309
10310 /* If the next two tokens are both identifiers, the code is
10311 erroneous. The usual cause of this situation is code like:
10312
10313 T t;
10314
10315 where "T" should name a type -- but does not. */
10316 if (!decl_specifiers.any_type_specifiers_p
10317 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10318 {
10319 /* If parsing tentatively, we should commit; we really are
10320 looking at a declaration. */
10321 cp_parser_commit_to_tentative_parse (parser);
10322 /* Give up. */
10323 goto done;
10324 }
10325
10326 /* If we have seen at least one decl-specifier, and the next token
10327 is not a parenthesis, then we must be looking at a declaration.
10328 (After "int (" we might be looking at a functional cast.) */
10329 if (decl_specifiers.any_specifiers_p
10330 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10331 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10332 && !cp_parser_error_occurred (parser))
10333 cp_parser_commit_to_tentative_parse (parser);
10334
10335 /* Keep going until we hit the `;' at the end of the simple
10336 declaration. */
10337 saw_declarator = false;
10338 while (cp_lexer_next_token_is_not (parser->lexer,
10339 CPP_SEMICOLON))
10340 {
10341 cp_token *token;
10342 bool function_definition_p;
10343 tree decl;
10344
10345 if (saw_declarator)
10346 {
10347 /* If we are processing next declarator, coma is expected */
10348 token = cp_lexer_peek_token (parser->lexer);
10349 gcc_assert (token->type == CPP_COMMA);
10350 cp_lexer_consume_token (parser->lexer);
10351 if (maybe_range_for_decl)
10352 *maybe_range_for_decl = error_mark_node;
10353 }
10354 else
10355 saw_declarator = true;
10356
10357 /* Parse the init-declarator. */
10358 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10359 /*checks=*/NULL,
10360 function_definition_allowed_p,
10361 /*member_p=*/false,
10362 declares_class_or_enum,
10363 &function_definition_p,
10364 maybe_range_for_decl);
10365 /* If an error occurred while parsing tentatively, exit quickly.
10366 (That usually happens when in the body of a function; each
10367 statement is treated as a declaration-statement until proven
10368 otherwise.) */
10369 if (cp_parser_error_occurred (parser))
10370 goto done;
10371 /* Handle function definitions specially. */
10372 if (function_definition_p)
10373 {
10374 /* If the next token is a `,', then we are probably
10375 processing something like:
10376
10377 void f() {}, *p;
10378
10379 which is erroneous. */
10380 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10381 {
10382 cp_token *token = cp_lexer_peek_token (parser->lexer);
10383 error_at (token->location,
10384 "mixing"
10385 " declarations and function-definitions is forbidden");
10386 }
10387 /* Otherwise, we're done with the list of declarators. */
10388 else
10389 {
10390 pop_deferring_access_checks ();
10391 return;
10392 }
10393 }
10394 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10395 *maybe_range_for_decl = decl;
10396 /* The next token should be either a `,' or a `;'. */
10397 token = cp_lexer_peek_token (parser->lexer);
10398 /* If it's a `,', there are more declarators to come. */
10399 if (token->type == CPP_COMMA)
10400 /* will be consumed next time around */;
10401 /* If it's a `;', we are done. */
10402 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10403 break;
10404 /* Anything else is an error. */
10405 else
10406 {
10407 /* If we have already issued an error message we don't need
10408 to issue another one. */
10409 if (decl != error_mark_node
10410 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10411 cp_parser_error (parser, "expected %<,%> or %<;%>");
10412 /* Skip tokens until we reach the end of the statement. */
10413 cp_parser_skip_to_end_of_statement (parser);
10414 /* If the next token is now a `;', consume it. */
10415 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10416 cp_lexer_consume_token (parser->lexer);
10417 goto done;
10418 }
10419 /* After the first time around, a function-definition is not
10420 allowed -- even if it was OK at first. For example:
10421
10422 int i, f() {}
10423
10424 is not valid. */
10425 function_definition_allowed_p = false;
10426 }
10427
10428 /* Issue an error message if no declarators are present, and the
10429 decl-specifier-seq does not itself declare a class or
10430 enumeration. */
10431 if (!saw_declarator)
10432 {
10433 if (cp_parser_declares_only_class_p (parser))
10434 shadow_tag (&decl_specifiers);
10435 /* Perform any deferred access checks. */
10436 perform_deferred_access_checks ();
10437 }
10438
10439 /* Consume the `;'. */
10440 if (!maybe_range_for_decl)
10441 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10442
10443 done:
10444 pop_deferring_access_checks ();
10445 }
10446
10447 /* Parse a decl-specifier-seq.
10448
10449 decl-specifier-seq:
10450 decl-specifier-seq [opt] decl-specifier
10451
10452 decl-specifier:
10453 storage-class-specifier
10454 type-specifier
10455 function-specifier
10456 friend
10457 typedef
10458
10459 GNU Extension:
10460
10461 decl-specifier:
10462 attributes
10463
10464 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10465
10466 The parser flags FLAGS is used to control type-specifier parsing.
10467
10468 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10469 flags:
10470
10471 1: one of the decl-specifiers is an elaborated-type-specifier
10472 (i.e., a type declaration)
10473 2: one of the decl-specifiers is an enum-specifier or a
10474 class-specifier (i.e., a type definition)
10475
10476 */
10477
10478 static void
10479 cp_parser_decl_specifier_seq (cp_parser* parser,
10480 cp_parser_flags flags,
10481 cp_decl_specifier_seq *decl_specs,
10482 int* declares_class_or_enum)
10483 {
10484 bool constructor_possible_p = !parser->in_declarator_p;
10485 cp_token *start_token = NULL;
10486
10487 /* Clear DECL_SPECS. */
10488 clear_decl_specs (decl_specs);
10489
10490 /* Assume no class or enumeration type is declared. */
10491 *declares_class_or_enum = 0;
10492
10493 /* Keep reading specifiers until there are no more to read. */
10494 while (true)
10495 {
10496 bool constructor_p;
10497 bool found_decl_spec;
10498 cp_token *token;
10499
10500 /* Peek at the next token. */
10501 token = cp_lexer_peek_token (parser->lexer);
10502
10503 /* Save the first token of the decl spec list for error
10504 reporting. */
10505 if (!start_token)
10506 start_token = token;
10507 /* Handle attributes. */
10508 if (token->keyword == RID_ATTRIBUTE)
10509 {
10510 /* Parse the attributes. */
10511 decl_specs->attributes
10512 = chainon (decl_specs->attributes,
10513 cp_parser_attributes_opt (parser));
10514 continue;
10515 }
10516 /* Assume we will find a decl-specifier keyword. */
10517 found_decl_spec = true;
10518 /* If the next token is an appropriate keyword, we can simply
10519 add it to the list. */
10520 switch (token->keyword)
10521 {
10522 /* decl-specifier:
10523 friend
10524 constexpr */
10525 case RID_FRIEND:
10526 if (!at_class_scope_p ())
10527 {
10528 error_at (token->location, "%<friend%> used outside of class");
10529 cp_lexer_purge_token (parser->lexer);
10530 }
10531 else
10532 {
10533 ++decl_specs->specs[(int) ds_friend];
10534 /* Consume the token. */
10535 cp_lexer_consume_token (parser->lexer);
10536 }
10537 break;
10538
10539 case RID_CONSTEXPR:
10540 ++decl_specs->specs[(int) ds_constexpr];
10541 cp_lexer_consume_token (parser->lexer);
10542 break;
10543
10544 /* function-specifier:
10545 inline
10546 virtual
10547 explicit */
10548 case RID_INLINE:
10549 case RID_VIRTUAL:
10550 case RID_EXPLICIT:
10551 cp_parser_function_specifier_opt (parser, decl_specs);
10552 break;
10553
10554 /* decl-specifier:
10555 typedef */
10556 case RID_TYPEDEF:
10557 ++decl_specs->specs[(int) ds_typedef];
10558 /* Consume the token. */
10559 cp_lexer_consume_token (parser->lexer);
10560 /* A constructor declarator cannot appear in a typedef. */
10561 constructor_possible_p = false;
10562 /* The "typedef" keyword can only occur in a declaration; we
10563 may as well commit at this point. */
10564 cp_parser_commit_to_tentative_parse (parser);
10565
10566 if (decl_specs->storage_class != sc_none)
10567 decl_specs->conflicting_specifiers_p = true;
10568 break;
10569
10570 /* storage-class-specifier:
10571 auto
10572 register
10573 static
10574 extern
10575 mutable
10576
10577 GNU Extension:
10578 thread */
10579 case RID_AUTO:
10580 if (cxx_dialect == cxx98)
10581 {
10582 /* Consume the token. */
10583 cp_lexer_consume_token (parser->lexer);
10584
10585 /* Complain about `auto' as a storage specifier, if
10586 we're complaining about C++0x compatibility. */
10587 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10588 " changes meaning in C++11; please remove it");
10589
10590 /* Set the storage class anyway. */
10591 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10592 token->location);
10593 }
10594 else
10595 /* C++0x auto type-specifier. */
10596 found_decl_spec = false;
10597 break;
10598
10599 case RID_REGISTER:
10600 case RID_STATIC:
10601 case RID_EXTERN:
10602 case RID_MUTABLE:
10603 /* Consume the token. */
10604 cp_lexer_consume_token (parser->lexer);
10605 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10606 token->location);
10607 break;
10608 case RID_THREAD:
10609 /* Consume the token. */
10610 cp_lexer_consume_token (parser->lexer);
10611 ++decl_specs->specs[(int) ds_thread];
10612 break;
10613
10614 default:
10615 /* We did not yet find a decl-specifier yet. */
10616 found_decl_spec = false;
10617 break;
10618 }
10619
10620 if (found_decl_spec
10621 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10622 && token->keyword != RID_CONSTEXPR)
10623 error ("decl-specifier invalid in condition");
10624
10625 /* Constructors are a special case. The `S' in `S()' is not a
10626 decl-specifier; it is the beginning of the declarator. */
10627 constructor_p
10628 = (!found_decl_spec
10629 && constructor_possible_p
10630 && (cp_parser_constructor_declarator_p
10631 (parser, decl_specs->specs[(int) ds_friend] != 0)));
10632
10633 /* If we don't have a DECL_SPEC yet, then we must be looking at
10634 a type-specifier. */
10635 if (!found_decl_spec && !constructor_p)
10636 {
10637 int decl_spec_declares_class_or_enum;
10638 bool is_cv_qualifier;
10639 tree type_spec;
10640
10641 type_spec
10642 = cp_parser_type_specifier (parser, flags,
10643 decl_specs,
10644 /*is_declaration=*/true,
10645 &decl_spec_declares_class_or_enum,
10646 &is_cv_qualifier);
10647 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10648
10649 /* If this type-specifier referenced a user-defined type
10650 (a typedef, class-name, etc.), then we can't allow any
10651 more such type-specifiers henceforth.
10652
10653 [dcl.spec]
10654
10655 The longest sequence of decl-specifiers that could
10656 possibly be a type name is taken as the
10657 decl-specifier-seq of a declaration. The sequence shall
10658 be self-consistent as described below.
10659
10660 [dcl.type]
10661
10662 As a general rule, at most one type-specifier is allowed
10663 in the complete decl-specifier-seq of a declaration. The
10664 only exceptions are the following:
10665
10666 -- const or volatile can be combined with any other
10667 type-specifier.
10668
10669 -- signed or unsigned can be combined with char, long,
10670 short, or int.
10671
10672 -- ..
10673
10674 Example:
10675
10676 typedef char* Pc;
10677 void g (const int Pc);
10678
10679 Here, Pc is *not* part of the decl-specifier seq; it's
10680 the declarator. Therefore, once we see a type-specifier
10681 (other than a cv-qualifier), we forbid any additional
10682 user-defined types. We *do* still allow things like `int
10683 int' to be considered a decl-specifier-seq, and issue the
10684 error message later. */
10685 if (type_spec && !is_cv_qualifier)
10686 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10687 /* A constructor declarator cannot follow a type-specifier. */
10688 if (type_spec)
10689 {
10690 constructor_possible_p = false;
10691 found_decl_spec = true;
10692 if (!is_cv_qualifier)
10693 decl_specs->any_type_specifiers_p = true;
10694 }
10695 }
10696
10697 /* If we still do not have a DECL_SPEC, then there are no more
10698 decl-specifiers. */
10699 if (!found_decl_spec)
10700 break;
10701
10702 decl_specs->any_specifiers_p = true;
10703 /* After we see one decl-specifier, further decl-specifiers are
10704 always optional. */
10705 flags |= CP_PARSER_FLAGS_OPTIONAL;
10706 }
10707
10708 cp_parser_check_decl_spec (decl_specs, start_token->location);
10709
10710 /* Don't allow a friend specifier with a class definition. */
10711 if (decl_specs->specs[(int) ds_friend] != 0
10712 && (*declares_class_or_enum & 2))
10713 error_at (start_token->location,
10714 "class definition may not be declared a friend");
10715 }
10716
10717 /* Parse an (optional) storage-class-specifier.
10718
10719 storage-class-specifier:
10720 auto
10721 register
10722 static
10723 extern
10724 mutable
10725
10726 GNU Extension:
10727
10728 storage-class-specifier:
10729 thread
10730
10731 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10732
10733 static tree
10734 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10735 {
10736 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10737 {
10738 case RID_AUTO:
10739 if (cxx_dialect != cxx98)
10740 return NULL_TREE;
10741 /* Fall through for C++98. */
10742
10743 case RID_REGISTER:
10744 case RID_STATIC:
10745 case RID_EXTERN:
10746 case RID_MUTABLE:
10747 case RID_THREAD:
10748 /* Consume the token. */
10749 return cp_lexer_consume_token (parser->lexer)->u.value;
10750
10751 default:
10752 return NULL_TREE;
10753 }
10754 }
10755
10756 /* Parse an (optional) function-specifier.
10757
10758 function-specifier:
10759 inline
10760 virtual
10761 explicit
10762
10763 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10764 Updates DECL_SPECS, if it is non-NULL. */
10765
10766 static tree
10767 cp_parser_function_specifier_opt (cp_parser* parser,
10768 cp_decl_specifier_seq *decl_specs)
10769 {
10770 cp_token *token = cp_lexer_peek_token (parser->lexer);
10771 switch (token->keyword)
10772 {
10773 case RID_INLINE:
10774 if (decl_specs)
10775 ++decl_specs->specs[(int) ds_inline];
10776 break;
10777
10778 case RID_VIRTUAL:
10779 /* 14.5.2.3 [temp.mem]
10780
10781 A member function template shall not be virtual. */
10782 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10783 error_at (token->location, "templates may not be %<virtual%>");
10784 else if (decl_specs)
10785 ++decl_specs->specs[(int) ds_virtual];
10786 break;
10787
10788 case RID_EXPLICIT:
10789 if (decl_specs)
10790 ++decl_specs->specs[(int) ds_explicit];
10791 break;
10792
10793 default:
10794 return NULL_TREE;
10795 }
10796
10797 /* Consume the token. */
10798 return cp_lexer_consume_token (parser->lexer)->u.value;
10799 }
10800
10801 /* Parse a linkage-specification.
10802
10803 linkage-specification:
10804 extern string-literal { declaration-seq [opt] }
10805 extern string-literal declaration */
10806
10807 static void
10808 cp_parser_linkage_specification (cp_parser* parser)
10809 {
10810 tree linkage;
10811
10812 /* Look for the `extern' keyword. */
10813 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10814
10815 /* Look for the string-literal. */
10816 linkage = cp_parser_string_literal (parser, false, false);
10817
10818 /* Transform the literal into an identifier. If the literal is a
10819 wide-character string, or contains embedded NULs, then we can't
10820 handle it as the user wants. */
10821 if (strlen (TREE_STRING_POINTER (linkage))
10822 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10823 {
10824 cp_parser_error (parser, "invalid linkage-specification");
10825 /* Assume C++ linkage. */
10826 linkage = lang_name_cplusplus;
10827 }
10828 else
10829 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10830
10831 /* We're now using the new linkage. */
10832 push_lang_context (linkage);
10833
10834 /* If the next token is a `{', then we're using the first
10835 production. */
10836 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10837 {
10838 /* Consume the `{' token. */
10839 cp_lexer_consume_token (parser->lexer);
10840 /* Parse the declarations. */
10841 cp_parser_declaration_seq_opt (parser);
10842 /* Look for the closing `}'. */
10843 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10844 }
10845 /* Otherwise, there's just one declaration. */
10846 else
10847 {
10848 bool saved_in_unbraced_linkage_specification_p;
10849
10850 saved_in_unbraced_linkage_specification_p
10851 = parser->in_unbraced_linkage_specification_p;
10852 parser->in_unbraced_linkage_specification_p = true;
10853 cp_parser_declaration (parser);
10854 parser->in_unbraced_linkage_specification_p
10855 = saved_in_unbraced_linkage_specification_p;
10856 }
10857
10858 /* We're done with the linkage-specification. */
10859 pop_lang_context ();
10860 }
10861
10862 /* Parse a static_assert-declaration.
10863
10864 static_assert-declaration:
10865 static_assert ( constant-expression , string-literal ) ;
10866
10867 If MEMBER_P, this static_assert is a class member. */
10868
10869 static void
10870 cp_parser_static_assert(cp_parser *parser, bool member_p)
10871 {
10872 tree condition;
10873 tree message;
10874 cp_token *token;
10875 location_t saved_loc;
10876 bool dummy;
10877
10878 /* Peek at the `static_assert' token so we can keep track of exactly
10879 where the static assertion started. */
10880 token = cp_lexer_peek_token (parser->lexer);
10881 saved_loc = token->location;
10882
10883 /* Look for the `static_assert' keyword. */
10884 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10885 RT_STATIC_ASSERT))
10886 return;
10887
10888 /* We know we are in a static assertion; commit to any tentative
10889 parse. */
10890 if (cp_parser_parsing_tentatively (parser))
10891 cp_parser_commit_to_tentative_parse (parser);
10892
10893 /* Parse the `(' starting the static assertion condition. */
10894 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10895
10896 /* Parse the constant-expression. Allow a non-constant expression
10897 here in order to give better diagnostics in finish_static_assert. */
10898 condition =
10899 cp_parser_constant_expression (parser,
10900 /*allow_non_constant_p=*/true,
10901 /*non_constant_p=*/&dummy);
10902
10903 /* Parse the separating `,'. */
10904 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10905
10906 /* Parse the string-literal message. */
10907 message = cp_parser_string_literal (parser,
10908 /*translate=*/false,
10909 /*wide_ok=*/true);
10910
10911 /* A `)' completes the static assertion. */
10912 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10913 cp_parser_skip_to_closing_parenthesis (parser,
10914 /*recovering=*/true,
10915 /*or_comma=*/false,
10916 /*consume_paren=*/true);
10917
10918 /* A semicolon terminates the declaration. */
10919 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10920
10921 /* Complete the static assertion, which may mean either processing
10922 the static assert now or saving it for template instantiation. */
10923 finish_static_assert (condition, message, saved_loc, member_p);
10924 }
10925
10926 /* Parse a `decltype' type. Returns the type.
10927
10928 simple-type-specifier:
10929 decltype ( expression ) */
10930
10931 static tree
10932 cp_parser_decltype (cp_parser *parser)
10933 {
10934 tree expr;
10935 bool id_expression_or_member_access_p = false;
10936 const char *saved_message;
10937 bool saved_integral_constant_expression_p;
10938 bool saved_non_integral_constant_expression_p;
10939 cp_token *id_expr_start_token;
10940 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10941
10942 if (start_token->type == CPP_DECLTYPE)
10943 {
10944 /* Already parsed. */
10945 cp_lexer_consume_token (parser->lexer);
10946 return start_token->u.value;
10947 }
10948
10949 /* Look for the `decltype' token. */
10950 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10951 return error_mark_node;
10952
10953 /* Types cannot be defined in a `decltype' expression. Save away the
10954 old message. */
10955 saved_message = parser->type_definition_forbidden_message;
10956
10957 /* And create the new one. */
10958 parser->type_definition_forbidden_message
10959 = G_("types may not be defined in %<decltype%> expressions");
10960
10961 /* The restrictions on constant-expressions do not apply inside
10962 decltype expressions. */
10963 saved_integral_constant_expression_p
10964 = parser->integral_constant_expression_p;
10965 saved_non_integral_constant_expression_p
10966 = parser->non_integral_constant_expression_p;
10967 parser->integral_constant_expression_p = false;
10968
10969 /* Do not actually evaluate the expression. */
10970 ++cp_unevaluated_operand;
10971
10972 /* Do not warn about problems with the expression. */
10973 ++c_inhibit_evaluation_warnings;
10974
10975 /* Parse the opening `('. */
10976 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10977 return error_mark_node;
10978
10979 /* First, try parsing an id-expression. */
10980 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10981 cp_parser_parse_tentatively (parser);
10982 expr = cp_parser_id_expression (parser,
10983 /*template_keyword_p=*/false,
10984 /*check_dependency_p=*/true,
10985 /*template_p=*/NULL,
10986 /*declarator_p=*/false,
10987 /*optional_p=*/false);
10988
10989 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10990 {
10991 bool non_integral_constant_expression_p = false;
10992 tree id_expression = expr;
10993 cp_id_kind idk;
10994 const char *error_msg;
10995
10996 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10997 /* Lookup the name we got back from the id-expression. */
10998 expr = cp_parser_lookup_name (parser, expr,
10999 none_type,
11000 /*is_template=*/false,
11001 /*is_namespace=*/false,
11002 /*check_dependency=*/true,
11003 /*ambiguous_decls=*/NULL,
11004 id_expr_start_token->location);
11005
11006 if (expr
11007 && expr != error_mark_node
11008 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11009 && TREE_CODE (expr) != TYPE_DECL
11010 && (TREE_CODE (expr) != BIT_NOT_EXPR
11011 || !TYPE_P (TREE_OPERAND (expr, 0)))
11012 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11013 {
11014 /* Complete lookup of the id-expression. */
11015 expr = (finish_id_expression
11016 (id_expression, expr, parser->scope, &idk,
11017 /*integral_constant_expression_p=*/false,
11018 /*allow_non_integral_constant_expression_p=*/true,
11019 &non_integral_constant_expression_p,
11020 /*template_p=*/false,
11021 /*done=*/true,
11022 /*address_p=*/false,
11023 /*template_arg_p=*/false,
11024 &error_msg,
11025 id_expr_start_token->location));
11026
11027 if (expr == error_mark_node)
11028 /* We found an id-expression, but it was something that we
11029 should not have found. This is an error, not something
11030 we can recover from, so note that we found an
11031 id-expression and we'll recover as gracefully as
11032 possible. */
11033 id_expression_or_member_access_p = true;
11034 }
11035
11036 if (expr
11037 && expr != error_mark_node
11038 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11039 /* We have an id-expression. */
11040 id_expression_or_member_access_p = true;
11041 }
11042
11043 if (!id_expression_or_member_access_p)
11044 {
11045 /* Abort the id-expression parse. */
11046 cp_parser_abort_tentative_parse (parser);
11047
11048 /* Parsing tentatively, again. */
11049 cp_parser_parse_tentatively (parser);
11050
11051 /* Parse a class member access. */
11052 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11053 /*cast_p=*/false,
11054 /*member_access_only_p=*/true, NULL);
11055
11056 if (expr
11057 && expr != error_mark_node
11058 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11059 /* We have an id-expression. */
11060 id_expression_or_member_access_p = true;
11061 }
11062
11063 if (id_expression_or_member_access_p)
11064 /* We have parsed the complete id-expression or member access. */
11065 cp_parser_parse_definitely (parser);
11066 else
11067 {
11068 bool saved_greater_than_is_operator_p;
11069
11070 /* Abort our attempt to parse an id-expression or member access
11071 expression. */
11072 cp_parser_abort_tentative_parse (parser);
11073
11074 /* Within a parenthesized expression, a `>' token is always
11075 the greater-than operator. */
11076 saved_greater_than_is_operator_p
11077 = parser->greater_than_is_operator_p;
11078 parser->greater_than_is_operator_p = true;
11079
11080 /* Parse a full expression. */
11081 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11082
11083 /* The `>' token might be the end of a template-id or
11084 template-parameter-list now. */
11085 parser->greater_than_is_operator_p
11086 = saved_greater_than_is_operator_p;
11087 }
11088
11089 /* Go back to evaluating expressions. */
11090 --cp_unevaluated_operand;
11091 --c_inhibit_evaluation_warnings;
11092
11093 /* Restore the old message and the integral constant expression
11094 flags. */
11095 parser->type_definition_forbidden_message = saved_message;
11096 parser->integral_constant_expression_p
11097 = saved_integral_constant_expression_p;
11098 parser->non_integral_constant_expression_p
11099 = saved_non_integral_constant_expression_p;
11100
11101 /* Parse to the closing `)'. */
11102 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11103 {
11104 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11105 /*consume_paren=*/true);
11106 return error_mark_node;
11107 }
11108
11109 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11110 tf_warning_or_error);
11111
11112 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11113 it again. */
11114 start_token->type = CPP_DECLTYPE;
11115 start_token->u.value = expr;
11116 start_token->keyword = RID_MAX;
11117 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11118
11119 return expr;
11120 }
11121
11122 /* Special member functions [gram.special] */
11123
11124 /* Parse a conversion-function-id.
11125
11126 conversion-function-id:
11127 operator conversion-type-id
11128
11129 Returns an IDENTIFIER_NODE representing the operator. */
11130
11131 static tree
11132 cp_parser_conversion_function_id (cp_parser* parser)
11133 {
11134 tree type;
11135 tree saved_scope;
11136 tree saved_qualifying_scope;
11137 tree saved_object_scope;
11138 tree pushed_scope = NULL_TREE;
11139
11140 /* Look for the `operator' token. */
11141 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11142 return error_mark_node;
11143 /* When we parse the conversion-type-id, the current scope will be
11144 reset. However, we need that information in able to look up the
11145 conversion function later, so we save it here. */
11146 saved_scope = parser->scope;
11147 saved_qualifying_scope = parser->qualifying_scope;
11148 saved_object_scope = parser->object_scope;
11149 /* We must enter the scope of the class so that the names of
11150 entities declared within the class are available in the
11151 conversion-type-id. For example, consider:
11152
11153 struct S {
11154 typedef int I;
11155 operator I();
11156 };
11157
11158 S::operator I() { ... }
11159
11160 In order to see that `I' is a type-name in the definition, we
11161 must be in the scope of `S'. */
11162 if (saved_scope)
11163 pushed_scope = push_scope (saved_scope);
11164 /* Parse the conversion-type-id. */
11165 type = cp_parser_conversion_type_id (parser);
11166 /* Leave the scope of the class, if any. */
11167 if (pushed_scope)
11168 pop_scope (pushed_scope);
11169 /* Restore the saved scope. */
11170 parser->scope = saved_scope;
11171 parser->qualifying_scope = saved_qualifying_scope;
11172 parser->object_scope = saved_object_scope;
11173 /* If the TYPE is invalid, indicate failure. */
11174 if (type == error_mark_node)
11175 return error_mark_node;
11176 return mangle_conv_op_name_for_type (type);
11177 }
11178
11179 /* Parse a conversion-type-id:
11180
11181 conversion-type-id:
11182 type-specifier-seq conversion-declarator [opt]
11183
11184 Returns the TYPE specified. */
11185
11186 static tree
11187 cp_parser_conversion_type_id (cp_parser* parser)
11188 {
11189 tree attributes;
11190 cp_decl_specifier_seq type_specifiers;
11191 cp_declarator *declarator;
11192 tree type_specified;
11193
11194 /* Parse the attributes. */
11195 attributes = cp_parser_attributes_opt (parser);
11196 /* Parse the type-specifiers. */
11197 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11198 /*is_trailing_return=*/false,
11199 &type_specifiers);
11200 /* If that didn't work, stop. */
11201 if (type_specifiers.type == error_mark_node)
11202 return error_mark_node;
11203 /* Parse the conversion-declarator. */
11204 declarator = cp_parser_conversion_declarator_opt (parser);
11205
11206 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11207 /*initialized=*/0, &attributes);
11208 if (attributes)
11209 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11210
11211 /* Don't give this error when parsing tentatively. This happens to
11212 work because we always parse this definitively once. */
11213 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11214 && type_uses_auto (type_specified))
11215 {
11216 error ("invalid use of %<auto%> in conversion operator");
11217 return error_mark_node;
11218 }
11219
11220 return type_specified;
11221 }
11222
11223 /* Parse an (optional) conversion-declarator.
11224
11225 conversion-declarator:
11226 ptr-operator conversion-declarator [opt]
11227
11228 */
11229
11230 static cp_declarator *
11231 cp_parser_conversion_declarator_opt (cp_parser* parser)
11232 {
11233 enum tree_code code;
11234 tree class_type;
11235 cp_cv_quals cv_quals;
11236
11237 /* We don't know if there's a ptr-operator next, or not. */
11238 cp_parser_parse_tentatively (parser);
11239 /* Try the ptr-operator. */
11240 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11241 /* If it worked, look for more conversion-declarators. */
11242 if (cp_parser_parse_definitely (parser))
11243 {
11244 cp_declarator *declarator;
11245
11246 /* Parse another optional declarator. */
11247 declarator = cp_parser_conversion_declarator_opt (parser);
11248
11249 return cp_parser_make_indirect_declarator
11250 (code, class_type, cv_quals, declarator);
11251 }
11252
11253 return NULL;
11254 }
11255
11256 /* Parse an (optional) ctor-initializer.
11257
11258 ctor-initializer:
11259 : mem-initializer-list
11260
11261 Returns TRUE iff the ctor-initializer was actually present. */
11262
11263 static bool
11264 cp_parser_ctor_initializer_opt (cp_parser* parser)
11265 {
11266 /* If the next token is not a `:', then there is no
11267 ctor-initializer. */
11268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11269 {
11270 /* Do default initialization of any bases and members. */
11271 if (DECL_CONSTRUCTOR_P (current_function_decl))
11272 finish_mem_initializers (NULL_TREE);
11273
11274 return false;
11275 }
11276
11277 /* Consume the `:' token. */
11278 cp_lexer_consume_token (parser->lexer);
11279 /* And the mem-initializer-list. */
11280 cp_parser_mem_initializer_list (parser);
11281
11282 return true;
11283 }
11284
11285 /* Parse a mem-initializer-list.
11286
11287 mem-initializer-list:
11288 mem-initializer ... [opt]
11289 mem-initializer ... [opt] , mem-initializer-list */
11290
11291 static void
11292 cp_parser_mem_initializer_list (cp_parser* parser)
11293 {
11294 tree mem_initializer_list = NULL_TREE;
11295 cp_token *token = cp_lexer_peek_token (parser->lexer);
11296
11297 /* Let the semantic analysis code know that we are starting the
11298 mem-initializer-list. */
11299 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11300 error_at (token->location,
11301 "only constructors take member initializers");
11302
11303 /* Loop through the list. */
11304 while (true)
11305 {
11306 tree mem_initializer;
11307
11308 token = cp_lexer_peek_token (parser->lexer);
11309 /* Parse the mem-initializer. */
11310 mem_initializer = cp_parser_mem_initializer (parser);
11311 /* If the next token is a `...', we're expanding member initializers. */
11312 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11313 {
11314 /* Consume the `...'. */
11315 cp_lexer_consume_token (parser->lexer);
11316
11317 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11318 can be expanded but members cannot. */
11319 if (mem_initializer != error_mark_node
11320 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11321 {
11322 error_at (token->location,
11323 "cannot expand initializer for member %<%D%>",
11324 TREE_PURPOSE (mem_initializer));
11325 mem_initializer = error_mark_node;
11326 }
11327
11328 /* Construct the pack expansion type. */
11329 if (mem_initializer != error_mark_node)
11330 mem_initializer = make_pack_expansion (mem_initializer);
11331 }
11332 /* Add it to the list, unless it was erroneous. */
11333 if (mem_initializer != error_mark_node)
11334 {
11335 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11336 mem_initializer_list = mem_initializer;
11337 }
11338 /* If the next token is not a `,', we're done. */
11339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11340 break;
11341 /* Consume the `,' token. */
11342 cp_lexer_consume_token (parser->lexer);
11343 }
11344
11345 /* Perform semantic analysis. */
11346 if (DECL_CONSTRUCTOR_P (current_function_decl))
11347 finish_mem_initializers (mem_initializer_list);
11348 }
11349
11350 /* Parse a mem-initializer.
11351
11352 mem-initializer:
11353 mem-initializer-id ( expression-list [opt] )
11354 mem-initializer-id braced-init-list
11355
11356 GNU extension:
11357
11358 mem-initializer:
11359 ( expression-list [opt] )
11360
11361 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11362 class) or FIELD_DECL (for a non-static data member) to initialize;
11363 the TREE_VALUE is the expression-list. An empty initialization
11364 list is represented by void_list_node. */
11365
11366 static tree
11367 cp_parser_mem_initializer (cp_parser* parser)
11368 {
11369 tree mem_initializer_id;
11370 tree expression_list;
11371 tree member;
11372 cp_token *token = cp_lexer_peek_token (parser->lexer);
11373
11374 /* Find out what is being initialized. */
11375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11376 {
11377 permerror (token->location,
11378 "anachronistic old-style base class initializer");
11379 mem_initializer_id = NULL_TREE;
11380 }
11381 else
11382 {
11383 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11384 if (mem_initializer_id == error_mark_node)
11385 return mem_initializer_id;
11386 }
11387 member = expand_member_init (mem_initializer_id);
11388 if (member && !DECL_P (member))
11389 in_base_initializer = 1;
11390
11391 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11392 {
11393 bool expr_non_constant_p;
11394 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11395 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11396 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11397 expression_list = build_tree_list (NULL_TREE, expression_list);
11398 }
11399 else
11400 {
11401 VEC(tree,gc)* vec;
11402 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11403 /*cast_p=*/false,
11404 /*allow_expansion_p=*/true,
11405 /*non_constant_p=*/NULL);
11406 if (vec == NULL)
11407 return error_mark_node;
11408 expression_list = build_tree_list_vec (vec);
11409 release_tree_vector (vec);
11410 }
11411
11412 if (expression_list == error_mark_node)
11413 return error_mark_node;
11414 if (!expression_list)
11415 expression_list = void_type_node;
11416
11417 in_base_initializer = 0;
11418
11419 return member ? build_tree_list (member, expression_list) : error_mark_node;
11420 }
11421
11422 /* Parse a mem-initializer-id.
11423
11424 mem-initializer-id:
11425 :: [opt] nested-name-specifier [opt] class-name
11426 identifier
11427
11428 Returns a TYPE indicating the class to be initializer for the first
11429 production. Returns an IDENTIFIER_NODE indicating the data member
11430 to be initialized for the second production. */
11431
11432 static tree
11433 cp_parser_mem_initializer_id (cp_parser* parser)
11434 {
11435 bool global_scope_p;
11436 bool nested_name_specifier_p;
11437 bool template_p = false;
11438 tree id;
11439
11440 cp_token *token = cp_lexer_peek_token (parser->lexer);
11441
11442 /* `typename' is not allowed in this context ([temp.res]). */
11443 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11444 {
11445 error_at (token->location,
11446 "keyword %<typename%> not allowed in this context (a qualified "
11447 "member initializer is implicitly a type)");
11448 cp_lexer_consume_token (parser->lexer);
11449 }
11450 /* Look for the optional `::' operator. */
11451 global_scope_p
11452 = (cp_parser_global_scope_opt (parser,
11453 /*current_scope_valid_p=*/false)
11454 != NULL_TREE);
11455 /* Look for the optional nested-name-specifier. The simplest way to
11456 implement:
11457
11458 [temp.res]
11459
11460 The keyword `typename' is not permitted in a base-specifier or
11461 mem-initializer; in these contexts a qualified name that
11462 depends on a template-parameter is implicitly assumed to be a
11463 type name.
11464
11465 is to assume that we have seen the `typename' keyword at this
11466 point. */
11467 nested_name_specifier_p
11468 = (cp_parser_nested_name_specifier_opt (parser,
11469 /*typename_keyword_p=*/true,
11470 /*check_dependency_p=*/true,
11471 /*type_p=*/true,
11472 /*is_declaration=*/true)
11473 != NULL_TREE);
11474 if (nested_name_specifier_p)
11475 template_p = cp_parser_optional_template_keyword (parser);
11476 /* If there is a `::' operator or a nested-name-specifier, then we
11477 are definitely looking for a class-name. */
11478 if (global_scope_p || nested_name_specifier_p)
11479 return cp_parser_class_name (parser,
11480 /*typename_keyword_p=*/true,
11481 /*template_keyword_p=*/template_p,
11482 typename_type,
11483 /*check_dependency_p=*/true,
11484 /*class_head_p=*/false,
11485 /*is_declaration=*/true);
11486 /* Otherwise, we could also be looking for an ordinary identifier. */
11487 cp_parser_parse_tentatively (parser);
11488 /* Try a class-name. */
11489 id = cp_parser_class_name (parser,
11490 /*typename_keyword_p=*/true,
11491 /*template_keyword_p=*/false,
11492 none_type,
11493 /*check_dependency_p=*/true,
11494 /*class_head_p=*/false,
11495 /*is_declaration=*/true);
11496 /* If we found one, we're done. */
11497 if (cp_parser_parse_definitely (parser))
11498 return id;
11499 /* Otherwise, look for an ordinary identifier. */
11500 return cp_parser_identifier (parser);
11501 }
11502
11503 /* Overloading [gram.over] */
11504
11505 /* Parse an operator-function-id.
11506
11507 operator-function-id:
11508 operator operator
11509
11510 Returns an IDENTIFIER_NODE for the operator which is a
11511 human-readable spelling of the identifier, e.g., `operator +'. */
11512
11513 static tree
11514 cp_parser_operator_function_id (cp_parser* parser)
11515 {
11516 /* Look for the `operator' keyword. */
11517 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11518 return error_mark_node;
11519 /* And then the name of the operator itself. */
11520 return cp_parser_operator (parser);
11521 }
11522
11523 /* Return an identifier node for a user-defined literal operator.
11524 The suffix identifier is chained to the operator name identifier. */
11525
11526 static tree
11527 cp_literal_operator_id (const char* name)
11528 {
11529 tree identifier;
11530 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11531 + strlen (name) + 10);
11532 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11533 identifier = get_identifier (buffer);
11534 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11535
11536 return identifier;
11537 }
11538
11539 /* Parse an operator.
11540
11541 operator:
11542 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11543 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11544 || ++ -- , ->* -> () []
11545
11546 GNU Extensions:
11547
11548 operator:
11549 <? >? <?= >?=
11550
11551 Returns an IDENTIFIER_NODE for the operator which is a
11552 human-readable spelling of the identifier, e.g., `operator +'. */
11553
11554 static tree
11555 cp_parser_operator (cp_parser* parser)
11556 {
11557 tree id = NULL_TREE;
11558 cp_token *token;
11559
11560 /* Peek at the next token. */
11561 token = cp_lexer_peek_token (parser->lexer);
11562 /* Figure out which operator we have. */
11563 switch (token->type)
11564 {
11565 case CPP_KEYWORD:
11566 {
11567 enum tree_code op;
11568
11569 /* The keyword should be either `new' or `delete'. */
11570 if (token->keyword == RID_NEW)
11571 op = NEW_EXPR;
11572 else if (token->keyword == RID_DELETE)
11573 op = DELETE_EXPR;
11574 else
11575 break;
11576
11577 /* Consume the `new' or `delete' token. */
11578 cp_lexer_consume_token (parser->lexer);
11579
11580 /* Peek at the next token. */
11581 token = cp_lexer_peek_token (parser->lexer);
11582 /* If it's a `[' token then this is the array variant of the
11583 operator. */
11584 if (token->type == CPP_OPEN_SQUARE)
11585 {
11586 /* Consume the `[' token. */
11587 cp_lexer_consume_token (parser->lexer);
11588 /* Look for the `]' token. */
11589 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11590 id = ansi_opname (op == NEW_EXPR
11591 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11592 }
11593 /* Otherwise, we have the non-array variant. */
11594 else
11595 id = ansi_opname (op);
11596
11597 return id;
11598 }
11599
11600 case CPP_PLUS:
11601 id = ansi_opname (PLUS_EXPR);
11602 break;
11603
11604 case CPP_MINUS:
11605 id = ansi_opname (MINUS_EXPR);
11606 break;
11607
11608 case CPP_MULT:
11609 id = ansi_opname (MULT_EXPR);
11610 break;
11611
11612 case CPP_DIV:
11613 id = ansi_opname (TRUNC_DIV_EXPR);
11614 break;
11615
11616 case CPP_MOD:
11617 id = ansi_opname (TRUNC_MOD_EXPR);
11618 break;
11619
11620 case CPP_XOR:
11621 id = ansi_opname (BIT_XOR_EXPR);
11622 break;
11623
11624 case CPP_AND:
11625 id = ansi_opname (BIT_AND_EXPR);
11626 break;
11627
11628 case CPP_OR:
11629 id = ansi_opname (BIT_IOR_EXPR);
11630 break;
11631
11632 case CPP_COMPL:
11633 id = ansi_opname (BIT_NOT_EXPR);
11634 break;
11635
11636 case CPP_NOT:
11637 id = ansi_opname (TRUTH_NOT_EXPR);
11638 break;
11639
11640 case CPP_EQ:
11641 id = ansi_assopname (NOP_EXPR);
11642 break;
11643
11644 case CPP_LESS:
11645 id = ansi_opname (LT_EXPR);
11646 break;
11647
11648 case CPP_GREATER:
11649 id = ansi_opname (GT_EXPR);
11650 break;
11651
11652 case CPP_PLUS_EQ:
11653 id = ansi_assopname (PLUS_EXPR);
11654 break;
11655
11656 case CPP_MINUS_EQ:
11657 id = ansi_assopname (MINUS_EXPR);
11658 break;
11659
11660 case CPP_MULT_EQ:
11661 id = ansi_assopname (MULT_EXPR);
11662 break;
11663
11664 case CPP_DIV_EQ:
11665 id = ansi_assopname (TRUNC_DIV_EXPR);
11666 break;
11667
11668 case CPP_MOD_EQ:
11669 id = ansi_assopname (TRUNC_MOD_EXPR);
11670 break;
11671
11672 case CPP_XOR_EQ:
11673 id = ansi_assopname (BIT_XOR_EXPR);
11674 break;
11675
11676 case CPP_AND_EQ:
11677 id = ansi_assopname (BIT_AND_EXPR);
11678 break;
11679
11680 case CPP_OR_EQ:
11681 id = ansi_assopname (BIT_IOR_EXPR);
11682 break;
11683
11684 case CPP_LSHIFT:
11685 id = ansi_opname (LSHIFT_EXPR);
11686 break;
11687
11688 case CPP_RSHIFT:
11689 id = ansi_opname (RSHIFT_EXPR);
11690 break;
11691
11692 case CPP_LSHIFT_EQ:
11693 id = ansi_assopname (LSHIFT_EXPR);
11694 break;
11695
11696 case CPP_RSHIFT_EQ:
11697 id = ansi_assopname (RSHIFT_EXPR);
11698 break;
11699
11700 case CPP_EQ_EQ:
11701 id = ansi_opname (EQ_EXPR);
11702 break;
11703
11704 case CPP_NOT_EQ:
11705 id = ansi_opname (NE_EXPR);
11706 break;
11707
11708 case CPP_LESS_EQ:
11709 id = ansi_opname (LE_EXPR);
11710 break;
11711
11712 case CPP_GREATER_EQ:
11713 id = ansi_opname (GE_EXPR);
11714 break;
11715
11716 case CPP_AND_AND:
11717 id = ansi_opname (TRUTH_ANDIF_EXPR);
11718 break;
11719
11720 case CPP_OR_OR:
11721 id = ansi_opname (TRUTH_ORIF_EXPR);
11722 break;
11723
11724 case CPP_PLUS_PLUS:
11725 id = ansi_opname (POSTINCREMENT_EXPR);
11726 break;
11727
11728 case CPP_MINUS_MINUS:
11729 id = ansi_opname (PREDECREMENT_EXPR);
11730 break;
11731
11732 case CPP_COMMA:
11733 id = ansi_opname (COMPOUND_EXPR);
11734 break;
11735
11736 case CPP_DEREF_STAR:
11737 id = ansi_opname (MEMBER_REF);
11738 break;
11739
11740 case CPP_DEREF:
11741 id = ansi_opname (COMPONENT_REF);
11742 break;
11743
11744 case CPP_OPEN_PAREN:
11745 /* Consume the `('. */
11746 cp_lexer_consume_token (parser->lexer);
11747 /* Look for the matching `)'. */
11748 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11749 return ansi_opname (CALL_EXPR);
11750
11751 case CPP_OPEN_SQUARE:
11752 /* Consume the `['. */
11753 cp_lexer_consume_token (parser->lexer);
11754 /* Look for the matching `]'. */
11755 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11756 return ansi_opname (ARRAY_REF);
11757
11758 case CPP_STRING:
11759 if (cxx_dialect == cxx98)
11760 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11761 if (TREE_STRING_LENGTH (token->u.value) > 2)
11762 {
11763 error ("expected empty string after %<operator%> keyword");
11764 return error_mark_node;
11765 }
11766 /* Consume the string. */
11767 cp_lexer_consume_token (parser->lexer);
11768 /* Look for the suffix identifier. */
11769 token = cp_lexer_peek_token (parser->lexer);
11770 if (token->type == CPP_NAME)
11771 {
11772 id = cp_parser_identifier (parser);
11773 if (id != error_mark_node)
11774 {
11775 const char *name = IDENTIFIER_POINTER (id);
11776 return cp_literal_operator_id (name);
11777 }
11778 }
11779 else
11780 {
11781 error ("expected suffix identifier");
11782 return error_mark_node;
11783 }
11784
11785 case CPP_STRING_USERDEF:
11786 error ("missing space between %<\"\"%> and suffix identifier");
11787 return error_mark_node;
11788
11789 default:
11790 /* Anything else is an error. */
11791 break;
11792 }
11793
11794 /* If we have selected an identifier, we need to consume the
11795 operator token. */
11796 if (id)
11797 cp_lexer_consume_token (parser->lexer);
11798 /* Otherwise, no valid operator name was present. */
11799 else
11800 {
11801 cp_parser_error (parser, "expected operator");
11802 id = error_mark_node;
11803 }
11804
11805 return id;
11806 }
11807
11808 /* Parse a template-declaration.
11809
11810 template-declaration:
11811 export [opt] template < template-parameter-list > declaration
11812
11813 If MEMBER_P is TRUE, this template-declaration occurs within a
11814 class-specifier.
11815
11816 The grammar rule given by the standard isn't correct. What
11817 is really meant is:
11818
11819 template-declaration:
11820 export [opt] template-parameter-list-seq
11821 decl-specifier-seq [opt] init-declarator [opt] ;
11822 export [opt] template-parameter-list-seq
11823 function-definition
11824
11825 template-parameter-list-seq:
11826 template-parameter-list-seq [opt]
11827 template < template-parameter-list > */
11828
11829 static void
11830 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11831 {
11832 /* Check for `export'. */
11833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11834 {
11835 /* Consume the `export' token. */
11836 cp_lexer_consume_token (parser->lexer);
11837 /* Warn that we do not support `export'. */
11838 warning (0, "keyword %<export%> not implemented, and will be ignored");
11839 }
11840
11841 cp_parser_template_declaration_after_export (parser, member_p);
11842 }
11843
11844 /* Parse a template-parameter-list.
11845
11846 template-parameter-list:
11847 template-parameter
11848 template-parameter-list , template-parameter
11849
11850 Returns a TREE_LIST. Each node represents a template parameter.
11851 The nodes are connected via their TREE_CHAINs. */
11852
11853 static tree
11854 cp_parser_template_parameter_list (cp_parser* parser)
11855 {
11856 tree parameter_list = NULL_TREE;
11857
11858 begin_template_parm_list ();
11859
11860 /* The loop below parses the template parms. We first need to know
11861 the total number of template parms to be able to compute proper
11862 canonical types of each dependent type. So after the loop, when
11863 we know the total number of template parms,
11864 end_template_parm_list computes the proper canonical types and
11865 fixes up the dependent types accordingly. */
11866 while (true)
11867 {
11868 tree parameter;
11869 bool is_non_type;
11870 bool is_parameter_pack;
11871 location_t parm_loc;
11872
11873 /* Parse the template-parameter. */
11874 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11875 parameter = cp_parser_template_parameter (parser,
11876 &is_non_type,
11877 &is_parameter_pack);
11878 /* Add it to the list. */
11879 if (parameter != error_mark_node)
11880 parameter_list = process_template_parm (parameter_list,
11881 parm_loc,
11882 parameter,
11883 is_non_type,
11884 is_parameter_pack,
11885 0);
11886 else
11887 {
11888 tree err_parm = build_tree_list (parameter, parameter);
11889 parameter_list = chainon (parameter_list, err_parm);
11890 }
11891
11892 /* If the next token is not a `,', we're done. */
11893 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11894 break;
11895 /* Otherwise, consume the `,' token. */
11896 cp_lexer_consume_token (parser->lexer);
11897 }
11898
11899 return end_template_parm_list (parameter_list);
11900 }
11901
11902 /* Parse a template-parameter.
11903
11904 template-parameter:
11905 type-parameter
11906 parameter-declaration
11907
11908 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11909 the parameter. The TREE_PURPOSE is the default value, if any.
11910 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11911 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11912 set to true iff this parameter is a parameter pack. */
11913
11914 static tree
11915 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11916 bool *is_parameter_pack)
11917 {
11918 cp_token *token;
11919 cp_parameter_declarator *parameter_declarator;
11920 cp_declarator *id_declarator;
11921 tree parm;
11922
11923 /* Assume it is a type parameter or a template parameter. */
11924 *is_non_type = false;
11925 /* Assume it not a parameter pack. */
11926 *is_parameter_pack = false;
11927 /* Peek at the next token. */
11928 token = cp_lexer_peek_token (parser->lexer);
11929 /* If it is `class' or `template', we have a type-parameter. */
11930 if (token->keyword == RID_TEMPLATE)
11931 return cp_parser_type_parameter (parser, is_parameter_pack);
11932 /* If it is `class' or `typename' we do not know yet whether it is a
11933 type parameter or a non-type parameter. Consider:
11934
11935 template <typename T, typename T::X X> ...
11936
11937 or:
11938
11939 template <class C, class D*> ...
11940
11941 Here, the first parameter is a type parameter, and the second is
11942 a non-type parameter. We can tell by looking at the token after
11943 the identifier -- if it is a `,', `=', or `>' then we have a type
11944 parameter. */
11945 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11946 {
11947 /* Peek at the token after `class' or `typename'. */
11948 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11949 /* If it's an ellipsis, we have a template type parameter
11950 pack. */
11951 if (token->type == CPP_ELLIPSIS)
11952 return cp_parser_type_parameter (parser, is_parameter_pack);
11953 /* If it's an identifier, skip it. */
11954 if (token->type == CPP_NAME)
11955 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11956 /* Now, see if the token looks like the end of a template
11957 parameter. */
11958 if (token->type == CPP_COMMA
11959 || token->type == CPP_EQ
11960 || token->type == CPP_GREATER)
11961 return cp_parser_type_parameter (parser, is_parameter_pack);
11962 }
11963
11964 /* Otherwise, it is a non-type parameter.
11965
11966 [temp.param]
11967
11968 When parsing a default template-argument for a non-type
11969 template-parameter, the first non-nested `>' is taken as the end
11970 of the template parameter-list rather than a greater-than
11971 operator. */
11972 *is_non_type = true;
11973 parameter_declarator
11974 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11975 /*parenthesized_p=*/NULL);
11976
11977 /* If the parameter declaration is marked as a parameter pack, set
11978 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11979 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11980 grokdeclarator. */
11981 if (parameter_declarator
11982 && parameter_declarator->declarator
11983 && parameter_declarator->declarator->parameter_pack_p)
11984 {
11985 *is_parameter_pack = true;
11986 parameter_declarator->declarator->parameter_pack_p = false;
11987 }
11988
11989 /* If the next token is an ellipsis, and we don't already have it
11990 marked as a parameter pack, then we have a parameter pack (that
11991 has no declarator). */
11992 if (!*is_parameter_pack
11993 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11994 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11995 {
11996 /* Consume the `...'. */
11997 cp_lexer_consume_token (parser->lexer);
11998 maybe_warn_variadic_templates ();
11999
12000 *is_parameter_pack = true;
12001 }
12002 /* We might end up with a pack expansion as the type of the non-type
12003 template parameter, in which case this is a non-type template
12004 parameter pack. */
12005 else if (parameter_declarator
12006 && parameter_declarator->decl_specifiers.type
12007 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12008 {
12009 *is_parameter_pack = true;
12010 parameter_declarator->decl_specifiers.type =
12011 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12012 }
12013
12014 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12015 {
12016 /* Parameter packs cannot have default arguments. However, a
12017 user may try to do so, so we'll parse them and give an
12018 appropriate diagnostic here. */
12019
12020 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12021
12022 /* Find the name of the parameter pack. */
12023 id_declarator = parameter_declarator->declarator;
12024 while (id_declarator && id_declarator->kind != cdk_id)
12025 id_declarator = id_declarator->declarator;
12026
12027 if (id_declarator && id_declarator->kind == cdk_id)
12028 error_at (start_token->location,
12029 "template parameter pack %qD cannot have a default argument",
12030 id_declarator->u.id.unqualified_name);
12031 else
12032 error_at (start_token->location,
12033 "template parameter pack cannot have a default argument");
12034
12035 /* Parse the default argument, but throw away the result. */
12036 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12037 }
12038
12039 parm = grokdeclarator (parameter_declarator->declarator,
12040 &parameter_declarator->decl_specifiers,
12041 TPARM, /*initialized=*/0,
12042 /*attrlist=*/NULL);
12043 if (parm == error_mark_node)
12044 return error_mark_node;
12045
12046 return build_tree_list (parameter_declarator->default_argument, parm);
12047 }
12048
12049 /* Parse a type-parameter.
12050
12051 type-parameter:
12052 class identifier [opt]
12053 class identifier [opt] = type-id
12054 typename identifier [opt]
12055 typename identifier [opt] = type-id
12056 template < template-parameter-list > class identifier [opt]
12057 template < template-parameter-list > class identifier [opt]
12058 = id-expression
12059
12060 GNU Extension (variadic templates):
12061
12062 type-parameter:
12063 class ... identifier [opt]
12064 typename ... identifier [opt]
12065
12066 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12067 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12068 the declaration of the parameter.
12069
12070 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12071
12072 static tree
12073 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12074 {
12075 cp_token *token;
12076 tree parameter;
12077
12078 /* Look for a keyword to tell us what kind of parameter this is. */
12079 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12080 if (!token)
12081 return error_mark_node;
12082
12083 switch (token->keyword)
12084 {
12085 case RID_CLASS:
12086 case RID_TYPENAME:
12087 {
12088 tree identifier;
12089 tree default_argument;
12090
12091 /* If the next token is an ellipsis, we have a template
12092 argument pack. */
12093 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12094 {
12095 /* Consume the `...' token. */
12096 cp_lexer_consume_token (parser->lexer);
12097 maybe_warn_variadic_templates ();
12098
12099 *is_parameter_pack = true;
12100 }
12101
12102 /* If the next token is an identifier, then it names the
12103 parameter. */
12104 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12105 identifier = cp_parser_identifier (parser);
12106 else
12107 identifier = NULL_TREE;
12108
12109 /* Create the parameter. */
12110 parameter = finish_template_type_parm (class_type_node, identifier);
12111
12112 /* If the next token is an `=', we have a default argument. */
12113 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12114 {
12115 /* Consume the `=' token. */
12116 cp_lexer_consume_token (parser->lexer);
12117 /* Parse the default-argument. */
12118 push_deferring_access_checks (dk_no_deferred);
12119 default_argument = cp_parser_type_id (parser);
12120
12121 /* Template parameter packs cannot have default
12122 arguments. */
12123 if (*is_parameter_pack)
12124 {
12125 if (identifier)
12126 error_at (token->location,
12127 "template parameter pack %qD cannot have a "
12128 "default argument", identifier);
12129 else
12130 error_at (token->location,
12131 "template parameter packs cannot have "
12132 "default arguments");
12133 default_argument = NULL_TREE;
12134 }
12135 pop_deferring_access_checks ();
12136 }
12137 else
12138 default_argument = NULL_TREE;
12139
12140 /* Create the combined representation of the parameter and the
12141 default argument. */
12142 parameter = build_tree_list (default_argument, parameter);
12143 }
12144 break;
12145
12146 case RID_TEMPLATE:
12147 {
12148 tree identifier;
12149 tree default_argument;
12150
12151 /* Look for the `<'. */
12152 cp_parser_require (parser, CPP_LESS, RT_LESS);
12153 /* Parse the template-parameter-list. */
12154 cp_parser_template_parameter_list (parser);
12155 /* Look for the `>'. */
12156 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12157 /* Look for the `class' keyword. */
12158 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12159 /* If the next token is an ellipsis, we have a template
12160 argument pack. */
12161 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12162 {
12163 /* Consume the `...' token. */
12164 cp_lexer_consume_token (parser->lexer);
12165 maybe_warn_variadic_templates ();
12166
12167 *is_parameter_pack = true;
12168 }
12169 /* If the next token is an `=', then there is a
12170 default-argument. If the next token is a `>', we are at
12171 the end of the parameter-list. If the next token is a `,',
12172 then we are at the end of this parameter. */
12173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12174 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12175 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12176 {
12177 identifier = cp_parser_identifier (parser);
12178 /* Treat invalid names as if the parameter were nameless. */
12179 if (identifier == error_mark_node)
12180 identifier = NULL_TREE;
12181 }
12182 else
12183 identifier = NULL_TREE;
12184
12185 /* Create the template parameter. */
12186 parameter = finish_template_template_parm (class_type_node,
12187 identifier);
12188
12189 /* If the next token is an `=', then there is a
12190 default-argument. */
12191 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12192 {
12193 bool is_template;
12194
12195 /* Consume the `='. */
12196 cp_lexer_consume_token (parser->lexer);
12197 /* Parse the id-expression. */
12198 push_deferring_access_checks (dk_no_deferred);
12199 /* save token before parsing the id-expression, for error
12200 reporting */
12201 token = cp_lexer_peek_token (parser->lexer);
12202 default_argument
12203 = cp_parser_id_expression (parser,
12204 /*template_keyword_p=*/false,
12205 /*check_dependency_p=*/true,
12206 /*template_p=*/&is_template,
12207 /*declarator_p=*/false,
12208 /*optional_p=*/false);
12209 if (TREE_CODE (default_argument) == TYPE_DECL)
12210 /* If the id-expression was a template-id that refers to
12211 a template-class, we already have the declaration here,
12212 so no further lookup is needed. */
12213 ;
12214 else
12215 /* Look up the name. */
12216 default_argument
12217 = cp_parser_lookup_name (parser, default_argument,
12218 none_type,
12219 /*is_template=*/is_template,
12220 /*is_namespace=*/false,
12221 /*check_dependency=*/true,
12222 /*ambiguous_decls=*/NULL,
12223 token->location);
12224 /* See if the default argument is valid. */
12225 default_argument
12226 = check_template_template_default_arg (default_argument);
12227
12228 /* Template parameter packs cannot have default
12229 arguments. */
12230 if (*is_parameter_pack)
12231 {
12232 if (identifier)
12233 error_at (token->location,
12234 "template parameter pack %qD cannot "
12235 "have a default argument",
12236 identifier);
12237 else
12238 error_at (token->location, "template parameter packs cannot "
12239 "have default arguments");
12240 default_argument = NULL_TREE;
12241 }
12242 pop_deferring_access_checks ();
12243 }
12244 else
12245 default_argument = NULL_TREE;
12246
12247 /* Create the combined representation of the parameter and the
12248 default argument. */
12249 parameter = build_tree_list (default_argument, parameter);
12250 }
12251 break;
12252
12253 default:
12254 gcc_unreachable ();
12255 break;
12256 }
12257
12258 return parameter;
12259 }
12260
12261 /* Parse a template-id.
12262
12263 template-id:
12264 template-name < template-argument-list [opt] >
12265
12266 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12267 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12268 returned. Otherwise, if the template-name names a function, or set
12269 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12270 names a class, returns a TYPE_DECL for the specialization.
12271
12272 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12273 uninstantiated templates. */
12274
12275 static tree
12276 cp_parser_template_id (cp_parser *parser,
12277 bool template_keyword_p,
12278 bool check_dependency_p,
12279 bool is_declaration)
12280 {
12281 int i;
12282 tree templ;
12283 tree arguments;
12284 tree template_id;
12285 cp_token_position start_of_id = 0;
12286 deferred_access_check *chk;
12287 VEC (deferred_access_check,gc) *access_check;
12288 cp_token *next_token = NULL, *next_token_2 = NULL;
12289 bool is_identifier;
12290
12291 /* If the next token corresponds to a template-id, there is no need
12292 to reparse it. */
12293 next_token = cp_lexer_peek_token (parser->lexer);
12294 if (next_token->type == CPP_TEMPLATE_ID)
12295 {
12296 struct tree_check *check_value;
12297
12298 /* Get the stored value. */
12299 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12300 /* Perform any access checks that were deferred. */
12301 access_check = check_value->checks;
12302 if (access_check)
12303 {
12304 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12305 perform_or_defer_access_check (chk->binfo,
12306 chk->decl,
12307 chk->diag_decl);
12308 }
12309 /* Return the stored value. */
12310 return check_value->value;
12311 }
12312
12313 /* Avoid performing name lookup if there is no possibility of
12314 finding a template-id. */
12315 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12316 || (next_token->type == CPP_NAME
12317 && !cp_parser_nth_token_starts_template_argument_list_p
12318 (parser, 2)))
12319 {
12320 cp_parser_error (parser, "expected template-id");
12321 return error_mark_node;
12322 }
12323
12324 /* Remember where the template-id starts. */
12325 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12326 start_of_id = cp_lexer_token_position (parser->lexer, false);
12327
12328 push_deferring_access_checks (dk_deferred);
12329
12330 /* Parse the template-name. */
12331 is_identifier = false;
12332 templ = cp_parser_template_name (parser, template_keyword_p,
12333 check_dependency_p,
12334 is_declaration,
12335 &is_identifier);
12336 if (templ == error_mark_node || is_identifier)
12337 {
12338 pop_deferring_access_checks ();
12339 return templ;
12340 }
12341
12342 /* If we find the sequence `[:' after a template-name, it's probably
12343 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12344 parse correctly the argument list. */
12345 next_token = cp_lexer_peek_token (parser->lexer);
12346 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12347 if (next_token->type == CPP_OPEN_SQUARE
12348 && next_token->flags & DIGRAPH
12349 && next_token_2->type == CPP_COLON
12350 && !(next_token_2->flags & PREV_WHITE))
12351 {
12352 cp_parser_parse_tentatively (parser);
12353 /* Change `:' into `::'. */
12354 next_token_2->type = CPP_SCOPE;
12355 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12356 CPP_LESS. */
12357 cp_lexer_consume_token (parser->lexer);
12358
12359 /* Parse the arguments. */
12360 arguments = cp_parser_enclosed_template_argument_list (parser);
12361 if (!cp_parser_parse_definitely (parser))
12362 {
12363 /* If we couldn't parse an argument list, then we revert our changes
12364 and return simply an error. Maybe this is not a template-id
12365 after all. */
12366 next_token_2->type = CPP_COLON;
12367 cp_parser_error (parser, "expected %<<%>");
12368 pop_deferring_access_checks ();
12369 return error_mark_node;
12370 }
12371 /* Otherwise, emit an error about the invalid digraph, but continue
12372 parsing because we got our argument list. */
12373 if (permerror (next_token->location,
12374 "%<<::%> cannot begin a template-argument list"))
12375 {
12376 static bool hint = false;
12377 inform (next_token->location,
12378 "%<<:%> is an alternate spelling for %<[%>."
12379 " Insert whitespace between %<<%> and %<::%>");
12380 if (!hint && !flag_permissive)
12381 {
12382 inform (next_token->location, "(if you use %<-fpermissive%>"
12383 " G++ will accept your code)");
12384 hint = true;
12385 }
12386 }
12387 }
12388 else
12389 {
12390 /* Look for the `<' that starts the template-argument-list. */
12391 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12392 {
12393 pop_deferring_access_checks ();
12394 return error_mark_node;
12395 }
12396 /* Parse the arguments. */
12397 arguments = cp_parser_enclosed_template_argument_list (parser);
12398 }
12399
12400 /* Build a representation of the specialization. */
12401 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12402 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12403 else if (DECL_TYPE_TEMPLATE_P (templ)
12404 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12405 {
12406 bool entering_scope;
12407 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12408 template (rather than some instantiation thereof) only if
12409 is not nested within some other construct. For example, in
12410 "template <typename T> void f(T) { A<T>::", A<T> is just an
12411 instantiation of A. */
12412 entering_scope = (template_parm_scope_p ()
12413 && cp_lexer_next_token_is (parser->lexer,
12414 CPP_SCOPE));
12415 template_id
12416 = finish_template_type (templ, arguments, entering_scope);
12417 }
12418 else
12419 {
12420 /* If it's not a class-template or a template-template, it should be
12421 a function-template. */
12422 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12423 || TREE_CODE (templ) == OVERLOAD
12424 || BASELINK_P (templ)));
12425
12426 template_id = lookup_template_function (templ, arguments);
12427 }
12428
12429 /* If parsing tentatively, replace the sequence of tokens that makes
12430 up the template-id with a CPP_TEMPLATE_ID token. That way,
12431 should we re-parse the token stream, we will not have to repeat
12432 the effort required to do the parse, nor will we issue duplicate
12433 error messages about problems during instantiation of the
12434 template. */
12435 if (start_of_id)
12436 {
12437 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12438
12439 /* Reset the contents of the START_OF_ID token. */
12440 token->type = CPP_TEMPLATE_ID;
12441 /* Retrieve any deferred checks. Do not pop this access checks yet
12442 so the memory will not be reclaimed during token replacing below. */
12443 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12444 token->u.tree_check_value->value = template_id;
12445 token->u.tree_check_value->checks = get_deferred_access_checks ();
12446 token->keyword = RID_MAX;
12447
12448 /* Purge all subsequent tokens. */
12449 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12450
12451 /* ??? Can we actually assume that, if template_id ==
12452 error_mark_node, we will have issued a diagnostic to the
12453 user, as opposed to simply marking the tentative parse as
12454 failed? */
12455 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12456 error_at (token->location, "parse error in template argument list");
12457 }
12458
12459 pop_deferring_access_checks ();
12460 return template_id;
12461 }
12462
12463 /* Parse a template-name.
12464
12465 template-name:
12466 identifier
12467
12468 The standard should actually say:
12469
12470 template-name:
12471 identifier
12472 operator-function-id
12473
12474 A defect report has been filed about this issue.
12475
12476 A conversion-function-id cannot be a template name because they cannot
12477 be part of a template-id. In fact, looking at this code:
12478
12479 a.operator K<int>()
12480
12481 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12482 It is impossible to call a templated conversion-function-id with an
12483 explicit argument list, since the only allowed template parameter is
12484 the type to which it is converting.
12485
12486 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12487 `template' keyword, in a construction like:
12488
12489 T::template f<3>()
12490
12491 In that case `f' is taken to be a template-name, even though there
12492 is no way of knowing for sure.
12493
12494 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12495 name refers to a set of overloaded functions, at least one of which
12496 is a template, or an IDENTIFIER_NODE with the name of the template,
12497 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12498 names are looked up inside uninstantiated templates. */
12499
12500 static tree
12501 cp_parser_template_name (cp_parser* parser,
12502 bool template_keyword_p,
12503 bool check_dependency_p,
12504 bool is_declaration,
12505 bool *is_identifier)
12506 {
12507 tree identifier;
12508 tree decl;
12509 tree fns;
12510 cp_token *token = cp_lexer_peek_token (parser->lexer);
12511
12512 /* If the next token is `operator', then we have either an
12513 operator-function-id or a conversion-function-id. */
12514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12515 {
12516 /* We don't know whether we're looking at an
12517 operator-function-id or a conversion-function-id. */
12518 cp_parser_parse_tentatively (parser);
12519 /* Try an operator-function-id. */
12520 identifier = cp_parser_operator_function_id (parser);
12521 /* If that didn't work, try a conversion-function-id. */
12522 if (!cp_parser_parse_definitely (parser))
12523 {
12524 cp_parser_error (parser, "expected template-name");
12525 return error_mark_node;
12526 }
12527 }
12528 /* Look for the identifier. */
12529 else
12530 identifier = cp_parser_identifier (parser);
12531
12532 /* If we didn't find an identifier, we don't have a template-id. */
12533 if (identifier == error_mark_node)
12534 return error_mark_node;
12535
12536 /* If the name immediately followed the `template' keyword, then it
12537 is a template-name. However, if the next token is not `<', then
12538 we do not treat it as a template-name, since it is not being used
12539 as part of a template-id. This enables us to handle constructs
12540 like:
12541
12542 template <typename T> struct S { S(); };
12543 template <typename T> S<T>::S();
12544
12545 correctly. We would treat `S' as a template -- if it were `S<T>'
12546 -- but we do not if there is no `<'. */
12547
12548 if (processing_template_decl
12549 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12550 {
12551 /* In a declaration, in a dependent context, we pretend that the
12552 "template" keyword was present in order to improve error
12553 recovery. For example, given:
12554
12555 template <typename T> void f(T::X<int>);
12556
12557 we want to treat "X<int>" as a template-id. */
12558 if (is_declaration
12559 && !template_keyword_p
12560 && parser->scope && TYPE_P (parser->scope)
12561 && check_dependency_p
12562 && dependent_scope_p (parser->scope)
12563 /* Do not do this for dtors (or ctors), since they never
12564 need the template keyword before their name. */
12565 && !constructor_name_p (identifier, parser->scope))
12566 {
12567 cp_token_position start = 0;
12568
12569 /* Explain what went wrong. */
12570 error_at (token->location, "non-template %qD used as template",
12571 identifier);
12572 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12573 parser->scope, identifier);
12574 /* If parsing tentatively, find the location of the "<" token. */
12575 if (cp_parser_simulate_error (parser))
12576 start = cp_lexer_token_position (parser->lexer, true);
12577 /* Parse the template arguments so that we can issue error
12578 messages about them. */
12579 cp_lexer_consume_token (parser->lexer);
12580 cp_parser_enclosed_template_argument_list (parser);
12581 /* Skip tokens until we find a good place from which to
12582 continue parsing. */
12583 cp_parser_skip_to_closing_parenthesis (parser,
12584 /*recovering=*/true,
12585 /*or_comma=*/true,
12586 /*consume_paren=*/false);
12587 /* If parsing tentatively, permanently remove the
12588 template argument list. That will prevent duplicate
12589 error messages from being issued about the missing
12590 "template" keyword. */
12591 if (start)
12592 cp_lexer_purge_tokens_after (parser->lexer, start);
12593 if (is_identifier)
12594 *is_identifier = true;
12595 return identifier;
12596 }
12597
12598 /* If the "template" keyword is present, then there is generally
12599 no point in doing name-lookup, so we just return IDENTIFIER.
12600 But, if the qualifying scope is non-dependent then we can
12601 (and must) do name-lookup normally. */
12602 if (template_keyword_p
12603 && (!parser->scope
12604 || (TYPE_P (parser->scope)
12605 && dependent_type_p (parser->scope))))
12606 return identifier;
12607 }
12608
12609 /* Look up the name. */
12610 decl = cp_parser_lookup_name (parser, identifier,
12611 none_type,
12612 /*is_template=*/true,
12613 /*is_namespace=*/false,
12614 check_dependency_p,
12615 /*ambiguous_decls=*/NULL,
12616 token->location);
12617
12618 /* If DECL is a template, then the name was a template-name. */
12619 if (TREE_CODE (decl) == TEMPLATE_DECL)
12620 ;
12621 else
12622 {
12623 tree fn = NULL_TREE;
12624
12625 /* The standard does not explicitly indicate whether a name that
12626 names a set of overloaded declarations, some of which are
12627 templates, is a template-name. However, such a name should
12628 be a template-name; otherwise, there is no way to form a
12629 template-id for the overloaded templates. */
12630 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12631 if (TREE_CODE (fns) == OVERLOAD)
12632 for (fn = fns; fn; fn = OVL_NEXT (fn))
12633 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12634 break;
12635
12636 if (!fn)
12637 {
12638 /* The name does not name a template. */
12639 cp_parser_error (parser, "expected template-name");
12640 return error_mark_node;
12641 }
12642 }
12643
12644 /* If DECL is dependent, and refers to a function, then just return
12645 its name; we will look it up again during template instantiation. */
12646 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12647 {
12648 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12649 if (TYPE_P (scope) && dependent_type_p (scope))
12650 return identifier;
12651 }
12652
12653 return decl;
12654 }
12655
12656 /* Parse a template-argument-list.
12657
12658 template-argument-list:
12659 template-argument ... [opt]
12660 template-argument-list , template-argument ... [opt]
12661
12662 Returns a TREE_VEC containing the arguments. */
12663
12664 static tree
12665 cp_parser_template_argument_list (cp_parser* parser)
12666 {
12667 tree fixed_args[10];
12668 unsigned n_args = 0;
12669 unsigned alloced = 10;
12670 tree *arg_ary = fixed_args;
12671 tree vec;
12672 bool saved_in_template_argument_list_p;
12673 bool saved_ice_p;
12674 bool saved_non_ice_p;
12675
12676 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12677 parser->in_template_argument_list_p = true;
12678 /* Even if the template-id appears in an integral
12679 constant-expression, the contents of the argument list do
12680 not. */
12681 saved_ice_p = parser->integral_constant_expression_p;
12682 parser->integral_constant_expression_p = false;
12683 saved_non_ice_p = parser->non_integral_constant_expression_p;
12684 parser->non_integral_constant_expression_p = false;
12685
12686 /* Parse the arguments. */
12687 do
12688 {
12689 tree argument;
12690
12691 if (n_args)
12692 /* Consume the comma. */
12693 cp_lexer_consume_token (parser->lexer);
12694
12695 /* Parse the template-argument. */
12696 argument = cp_parser_template_argument (parser);
12697
12698 /* If the next token is an ellipsis, we're expanding a template
12699 argument pack. */
12700 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12701 {
12702 if (argument == error_mark_node)
12703 {
12704 cp_token *token = cp_lexer_peek_token (parser->lexer);
12705 error_at (token->location,
12706 "expected parameter pack before %<...%>");
12707 }
12708 /* Consume the `...' token. */
12709 cp_lexer_consume_token (parser->lexer);
12710
12711 /* Make the argument into a TYPE_PACK_EXPANSION or
12712 EXPR_PACK_EXPANSION. */
12713 argument = make_pack_expansion (argument);
12714 }
12715
12716 if (n_args == alloced)
12717 {
12718 alloced *= 2;
12719
12720 if (arg_ary == fixed_args)
12721 {
12722 arg_ary = XNEWVEC (tree, alloced);
12723 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12724 }
12725 else
12726 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12727 }
12728 arg_ary[n_args++] = argument;
12729 }
12730 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12731
12732 vec = make_tree_vec (n_args);
12733
12734 while (n_args--)
12735 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12736
12737 if (arg_ary != fixed_args)
12738 free (arg_ary);
12739 parser->non_integral_constant_expression_p = saved_non_ice_p;
12740 parser->integral_constant_expression_p = saved_ice_p;
12741 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12742 #ifdef ENABLE_CHECKING
12743 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12744 #endif
12745 return vec;
12746 }
12747
12748 /* Parse a template-argument.
12749
12750 template-argument:
12751 assignment-expression
12752 type-id
12753 id-expression
12754
12755 The representation is that of an assignment-expression, type-id, or
12756 id-expression -- except that the qualified id-expression is
12757 evaluated, so that the value returned is either a DECL or an
12758 OVERLOAD.
12759
12760 Although the standard says "assignment-expression", it forbids
12761 throw-expressions or assignments in the template argument.
12762 Therefore, we use "conditional-expression" instead. */
12763
12764 static tree
12765 cp_parser_template_argument (cp_parser* parser)
12766 {
12767 tree argument;
12768 bool template_p;
12769 bool address_p;
12770 bool maybe_type_id = false;
12771 cp_token *token = NULL, *argument_start_token = NULL;
12772 cp_id_kind idk;
12773
12774 /* There's really no way to know what we're looking at, so we just
12775 try each alternative in order.
12776
12777 [temp.arg]
12778
12779 In a template-argument, an ambiguity between a type-id and an
12780 expression is resolved to a type-id, regardless of the form of
12781 the corresponding template-parameter.
12782
12783 Therefore, we try a type-id first. */
12784 cp_parser_parse_tentatively (parser);
12785 argument = cp_parser_template_type_arg (parser);
12786 /* If there was no error parsing the type-id but the next token is a
12787 '>>', our behavior depends on which dialect of C++ we're
12788 parsing. In C++98, we probably found a typo for '> >'. But there
12789 are type-id which are also valid expressions. For instance:
12790
12791 struct X { int operator >> (int); };
12792 template <int V> struct Foo {};
12793 Foo<X () >> 5> r;
12794
12795 Here 'X()' is a valid type-id of a function type, but the user just
12796 wanted to write the expression "X() >> 5". Thus, we remember that we
12797 found a valid type-id, but we still try to parse the argument as an
12798 expression to see what happens.
12799
12800 In C++0x, the '>>' will be considered two separate '>'
12801 tokens. */
12802 if (!cp_parser_error_occurred (parser)
12803 && cxx_dialect == cxx98
12804 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12805 {
12806 maybe_type_id = true;
12807 cp_parser_abort_tentative_parse (parser);
12808 }
12809 else
12810 {
12811 /* If the next token isn't a `,' or a `>', then this argument wasn't
12812 really finished. This means that the argument is not a valid
12813 type-id. */
12814 if (!cp_parser_next_token_ends_template_argument_p (parser))
12815 cp_parser_error (parser, "expected template-argument");
12816 /* If that worked, we're done. */
12817 if (cp_parser_parse_definitely (parser))
12818 return argument;
12819 }
12820 /* We're still not sure what the argument will be. */
12821 cp_parser_parse_tentatively (parser);
12822 /* Try a template. */
12823 argument_start_token = cp_lexer_peek_token (parser->lexer);
12824 argument = cp_parser_id_expression (parser,
12825 /*template_keyword_p=*/false,
12826 /*check_dependency_p=*/true,
12827 &template_p,
12828 /*declarator_p=*/false,
12829 /*optional_p=*/false);
12830 /* If the next token isn't a `,' or a `>', then this argument wasn't
12831 really finished. */
12832 if (!cp_parser_next_token_ends_template_argument_p (parser))
12833 cp_parser_error (parser, "expected template-argument");
12834 if (!cp_parser_error_occurred (parser))
12835 {
12836 /* Figure out what is being referred to. If the id-expression
12837 was for a class template specialization, then we will have a
12838 TYPE_DECL at this point. There is no need to do name lookup
12839 at this point in that case. */
12840 if (TREE_CODE (argument) != TYPE_DECL)
12841 argument = cp_parser_lookup_name (parser, argument,
12842 none_type,
12843 /*is_template=*/template_p,
12844 /*is_namespace=*/false,
12845 /*check_dependency=*/true,
12846 /*ambiguous_decls=*/NULL,
12847 argument_start_token->location);
12848 if (TREE_CODE (argument) != TEMPLATE_DECL
12849 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12850 cp_parser_error (parser, "expected template-name");
12851 }
12852 if (cp_parser_parse_definitely (parser))
12853 return argument;
12854 /* It must be a non-type argument. There permitted cases are given
12855 in [temp.arg.nontype]:
12856
12857 -- an integral constant-expression of integral or enumeration
12858 type; or
12859
12860 -- the name of a non-type template-parameter; or
12861
12862 -- the name of an object or function with external linkage...
12863
12864 -- the address of an object or function with external linkage...
12865
12866 -- a pointer to member... */
12867 /* Look for a non-type template parameter. */
12868 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12869 {
12870 cp_parser_parse_tentatively (parser);
12871 argument = cp_parser_primary_expression (parser,
12872 /*address_p=*/false,
12873 /*cast_p=*/false,
12874 /*template_arg_p=*/true,
12875 &idk);
12876 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12877 || !cp_parser_next_token_ends_template_argument_p (parser))
12878 cp_parser_simulate_error (parser);
12879 if (cp_parser_parse_definitely (parser))
12880 return argument;
12881 }
12882
12883 /* If the next token is "&", the argument must be the address of an
12884 object or function with external linkage. */
12885 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12886 if (address_p)
12887 cp_lexer_consume_token (parser->lexer);
12888 /* See if we might have an id-expression. */
12889 token = cp_lexer_peek_token (parser->lexer);
12890 if (token->type == CPP_NAME
12891 || token->keyword == RID_OPERATOR
12892 || token->type == CPP_SCOPE
12893 || token->type == CPP_TEMPLATE_ID
12894 || token->type == CPP_NESTED_NAME_SPECIFIER)
12895 {
12896 cp_parser_parse_tentatively (parser);
12897 argument = cp_parser_primary_expression (parser,
12898 address_p,
12899 /*cast_p=*/false,
12900 /*template_arg_p=*/true,
12901 &idk);
12902 if (cp_parser_error_occurred (parser)
12903 || !cp_parser_next_token_ends_template_argument_p (parser))
12904 cp_parser_abort_tentative_parse (parser);
12905 else
12906 {
12907 tree probe;
12908
12909 if (TREE_CODE (argument) == INDIRECT_REF)
12910 {
12911 gcc_assert (REFERENCE_REF_P (argument));
12912 argument = TREE_OPERAND (argument, 0);
12913 }
12914
12915 /* If we're in a template, we represent a qualified-id referring
12916 to a static data member as a SCOPE_REF even if the scope isn't
12917 dependent so that we can check access control later. */
12918 probe = argument;
12919 if (TREE_CODE (probe) == SCOPE_REF)
12920 probe = TREE_OPERAND (probe, 1);
12921 if (TREE_CODE (probe) == VAR_DECL)
12922 {
12923 /* A variable without external linkage might still be a
12924 valid constant-expression, so no error is issued here
12925 if the external-linkage check fails. */
12926 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12927 cp_parser_simulate_error (parser);
12928 }
12929 else if (is_overloaded_fn (argument))
12930 /* All overloaded functions are allowed; if the external
12931 linkage test does not pass, an error will be issued
12932 later. */
12933 ;
12934 else if (address_p
12935 && (TREE_CODE (argument) == OFFSET_REF
12936 || TREE_CODE (argument) == SCOPE_REF))
12937 /* A pointer-to-member. */
12938 ;
12939 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12940 ;
12941 else
12942 cp_parser_simulate_error (parser);
12943
12944 if (cp_parser_parse_definitely (parser))
12945 {
12946 if (address_p)
12947 argument = build_x_unary_op (ADDR_EXPR, argument,
12948 tf_warning_or_error);
12949 return argument;
12950 }
12951 }
12952 }
12953 /* If the argument started with "&", there are no other valid
12954 alternatives at this point. */
12955 if (address_p)
12956 {
12957 cp_parser_error (parser, "invalid non-type template argument");
12958 return error_mark_node;
12959 }
12960
12961 /* If the argument wasn't successfully parsed as a type-id followed
12962 by '>>', the argument can only be a constant expression now.
12963 Otherwise, we try parsing the constant-expression tentatively,
12964 because the argument could really be a type-id. */
12965 if (maybe_type_id)
12966 cp_parser_parse_tentatively (parser);
12967 argument = cp_parser_constant_expression (parser,
12968 /*allow_non_constant_p=*/false,
12969 /*non_constant_p=*/NULL);
12970 argument = fold_non_dependent_expr (argument);
12971 if (!maybe_type_id)
12972 return argument;
12973 if (!cp_parser_next_token_ends_template_argument_p (parser))
12974 cp_parser_error (parser, "expected template-argument");
12975 if (cp_parser_parse_definitely (parser))
12976 return argument;
12977 /* We did our best to parse the argument as a non type-id, but that
12978 was the only alternative that matched (albeit with a '>' after
12979 it). We can assume it's just a typo from the user, and a
12980 diagnostic will then be issued. */
12981 return cp_parser_template_type_arg (parser);
12982 }
12983
12984 /* Parse an explicit-instantiation.
12985
12986 explicit-instantiation:
12987 template declaration
12988
12989 Although the standard says `declaration', what it really means is:
12990
12991 explicit-instantiation:
12992 template decl-specifier-seq [opt] declarator [opt] ;
12993
12994 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12995 supposed to be allowed. A defect report has been filed about this
12996 issue.
12997
12998 GNU Extension:
12999
13000 explicit-instantiation:
13001 storage-class-specifier template
13002 decl-specifier-seq [opt] declarator [opt] ;
13003 function-specifier template
13004 decl-specifier-seq [opt] declarator [opt] ; */
13005
13006 static void
13007 cp_parser_explicit_instantiation (cp_parser* parser)
13008 {
13009 int declares_class_or_enum;
13010 cp_decl_specifier_seq decl_specifiers;
13011 tree extension_specifier = NULL_TREE;
13012
13013 timevar_push (TV_TEMPLATE_INST);
13014
13015 /* Look for an (optional) storage-class-specifier or
13016 function-specifier. */
13017 if (cp_parser_allow_gnu_extensions_p (parser))
13018 {
13019 extension_specifier
13020 = cp_parser_storage_class_specifier_opt (parser);
13021 if (!extension_specifier)
13022 extension_specifier
13023 = cp_parser_function_specifier_opt (parser,
13024 /*decl_specs=*/NULL);
13025 }
13026
13027 /* Look for the `template' keyword. */
13028 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13029 /* Let the front end know that we are processing an explicit
13030 instantiation. */
13031 begin_explicit_instantiation ();
13032 /* [temp.explicit] says that we are supposed to ignore access
13033 control while processing explicit instantiation directives. */
13034 push_deferring_access_checks (dk_no_check);
13035 /* Parse a decl-specifier-seq. */
13036 cp_parser_decl_specifier_seq (parser,
13037 CP_PARSER_FLAGS_OPTIONAL,
13038 &decl_specifiers,
13039 &declares_class_or_enum);
13040 /* If there was exactly one decl-specifier, and it declared a class,
13041 and there's no declarator, then we have an explicit type
13042 instantiation. */
13043 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13044 {
13045 tree type;
13046
13047 type = check_tag_decl (&decl_specifiers);
13048 /* Turn access control back on for names used during
13049 template instantiation. */
13050 pop_deferring_access_checks ();
13051 if (type)
13052 do_type_instantiation (type, extension_specifier,
13053 /*complain=*/tf_error);
13054 }
13055 else
13056 {
13057 cp_declarator *declarator;
13058 tree decl;
13059
13060 /* Parse the declarator. */
13061 declarator
13062 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13063 /*ctor_dtor_or_conv_p=*/NULL,
13064 /*parenthesized_p=*/NULL,
13065 /*member_p=*/false);
13066 if (declares_class_or_enum & 2)
13067 cp_parser_check_for_definition_in_return_type (declarator,
13068 decl_specifiers.type,
13069 decl_specifiers.type_location);
13070 if (declarator != cp_error_declarator)
13071 {
13072 if (decl_specifiers.specs[(int)ds_inline])
13073 permerror (input_location, "explicit instantiation shall not use"
13074 " %<inline%> specifier");
13075 if (decl_specifiers.specs[(int)ds_constexpr])
13076 permerror (input_location, "explicit instantiation shall not use"
13077 " %<constexpr%> specifier");
13078
13079 decl = grokdeclarator (declarator, &decl_specifiers,
13080 NORMAL, 0, &decl_specifiers.attributes);
13081 /* Turn access control back on for names used during
13082 template instantiation. */
13083 pop_deferring_access_checks ();
13084 /* Do the explicit instantiation. */
13085 do_decl_instantiation (decl, extension_specifier);
13086 }
13087 else
13088 {
13089 pop_deferring_access_checks ();
13090 /* Skip the body of the explicit instantiation. */
13091 cp_parser_skip_to_end_of_statement (parser);
13092 }
13093 }
13094 /* We're done with the instantiation. */
13095 end_explicit_instantiation ();
13096
13097 cp_parser_consume_semicolon_at_end_of_statement (parser);
13098
13099 timevar_pop (TV_TEMPLATE_INST);
13100 }
13101
13102 /* Parse an explicit-specialization.
13103
13104 explicit-specialization:
13105 template < > declaration
13106
13107 Although the standard says `declaration', what it really means is:
13108
13109 explicit-specialization:
13110 template <> decl-specifier [opt] init-declarator [opt] ;
13111 template <> function-definition
13112 template <> explicit-specialization
13113 template <> template-declaration */
13114
13115 static void
13116 cp_parser_explicit_specialization (cp_parser* parser)
13117 {
13118 bool need_lang_pop;
13119 cp_token *token = cp_lexer_peek_token (parser->lexer);
13120
13121 /* Look for the `template' keyword. */
13122 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13123 /* Look for the `<'. */
13124 cp_parser_require (parser, CPP_LESS, RT_LESS);
13125 /* Look for the `>'. */
13126 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13127 /* We have processed another parameter list. */
13128 ++parser->num_template_parameter_lists;
13129 /* [temp]
13130
13131 A template ... explicit specialization ... shall not have C
13132 linkage. */
13133 if (current_lang_name == lang_name_c)
13134 {
13135 error_at (token->location, "template specialization with C linkage");
13136 /* Give it C++ linkage to avoid confusing other parts of the
13137 front end. */
13138 push_lang_context (lang_name_cplusplus);
13139 need_lang_pop = true;
13140 }
13141 else
13142 need_lang_pop = false;
13143 /* Let the front end know that we are beginning a specialization. */
13144 if (!begin_specialization ())
13145 {
13146 end_specialization ();
13147 return;
13148 }
13149
13150 /* If the next keyword is `template', we need to figure out whether
13151 or not we're looking a template-declaration. */
13152 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13153 {
13154 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13155 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13156 cp_parser_template_declaration_after_export (parser,
13157 /*member_p=*/false);
13158 else
13159 cp_parser_explicit_specialization (parser);
13160 }
13161 else
13162 /* Parse the dependent declaration. */
13163 cp_parser_single_declaration (parser,
13164 /*checks=*/NULL,
13165 /*member_p=*/false,
13166 /*explicit_specialization_p=*/true,
13167 /*friend_p=*/NULL);
13168 /* We're done with the specialization. */
13169 end_specialization ();
13170 /* For the erroneous case of a template with C linkage, we pushed an
13171 implicit C++ linkage scope; exit that scope now. */
13172 if (need_lang_pop)
13173 pop_lang_context ();
13174 /* We're done with this parameter list. */
13175 --parser->num_template_parameter_lists;
13176 }
13177
13178 /* Parse a type-specifier.
13179
13180 type-specifier:
13181 simple-type-specifier
13182 class-specifier
13183 enum-specifier
13184 elaborated-type-specifier
13185 cv-qualifier
13186
13187 GNU Extension:
13188
13189 type-specifier:
13190 __complex__
13191
13192 Returns a representation of the type-specifier. For a
13193 class-specifier, enum-specifier, or elaborated-type-specifier, a
13194 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13195
13196 The parser flags FLAGS is used to control type-specifier parsing.
13197
13198 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13199 in a decl-specifier-seq.
13200
13201 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13202 class-specifier, enum-specifier, or elaborated-type-specifier, then
13203 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13204 if a type is declared; 2 if it is defined. Otherwise, it is set to
13205 zero.
13206
13207 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13208 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13209 is set to FALSE. */
13210
13211 static tree
13212 cp_parser_type_specifier (cp_parser* parser,
13213 cp_parser_flags flags,
13214 cp_decl_specifier_seq *decl_specs,
13215 bool is_declaration,
13216 int* declares_class_or_enum,
13217 bool* is_cv_qualifier)
13218 {
13219 tree type_spec = NULL_TREE;
13220 cp_token *token;
13221 enum rid keyword;
13222 cp_decl_spec ds = ds_last;
13223
13224 /* Assume this type-specifier does not declare a new type. */
13225 if (declares_class_or_enum)
13226 *declares_class_or_enum = 0;
13227 /* And that it does not specify a cv-qualifier. */
13228 if (is_cv_qualifier)
13229 *is_cv_qualifier = false;
13230 /* Peek at the next token. */
13231 token = cp_lexer_peek_token (parser->lexer);
13232
13233 /* If we're looking at a keyword, we can use that to guide the
13234 production we choose. */
13235 keyword = token->keyword;
13236 switch (keyword)
13237 {
13238 case RID_ENUM:
13239 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13240 goto elaborated_type_specifier;
13241
13242 /* Look for the enum-specifier. */
13243 type_spec = cp_parser_enum_specifier (parser);
13244 /* If that worked, we're done. */
13245 if (type_spec)
13246 {
13247 if (declares_class_or_enum)
13248 *declares_class_or_enum = 2;
13249 if (decl_specs)
13250 cp_parser_set_decl_spec_type (decl_specs,
13251 type_spec,
13252 token->location,
13253 /*type_definition_p=*/true);
13254 return type_spec;
13255 }
13256 else
13257 goto elaborated_type_specifier;
13258
13259 /* Any of these indicate either a class-specifier, or an
13260 elaborated-type-specifier. */
13261 case RID_CLASS:
13262 case RID_STRUCT:
13263 case RID_UNION:
13264 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13265 goto elaborated_type_specifier;
13266
13267 /* Parse tentatively so that we can back up if we don't find a
13268 class-specifier. */
13269 cp_parser_parse_tentatively (parser);
13270 /* Look for the class-specifier. */
13271 type_spec = cp_parser_class_specifier (parser);
13272 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13273 /* If that worked, we're done. */
13274 if (cp_parser_parse_definitely (parser))
13275 {
13276 if (declares_class_or_enum)
13277 *declares_class_or_enum = 2;
13278 if (decl_specs)
13279 cp_parser_set_decl_spec_type (decl_specs,
13280 type_spec,
13281 token->location,
13282 /*type_definition_p=*/true);
13283 return type_spec;
13284 }
13285
13286 /* Fall through. */
13287 elaborated_type_specifier:
13288 /* We're declaring (not defining) a class or enum. */
13289 if (declares_class_or_enum)
13290 *declares_class_or_enum = 1;
13291
13292 /* Fall through. */
13293 case RID_TYPENAME:
13294 /* Look for an elaborated-type-specifier. */
13295 type_spec
13296 = (cp_parser_elaborated_type_specifier
13297 (parser,
13298 decl_specs && decl_specs->specs[(int) ds_friend],
13299 is_declaration));
13300 if (decl_specs)
13301 cp_parser_set_decl_spec_type (decl_specs,
13302 type_spec,
13303 token->location,
13304 /*type_definition_p=*/false);
13305 return type_spec;
13306
13307 case RID_CONST:
13308 ds = ds_const;
13309 if (is_cv_qualifier)
13310 *is_cv_qualifier = true;
13311 break;
13312
13313 case RID_VOLATILE:
13314 ds = ds_volatile;
13315 if (is_cv_qualifier)
13316 *is_cv_qualifier = true;
13317 break;
13318
13319 case RID_RESTRICT:
13320 ds = ds_restrict;
13321 if (is_cv_qualifier)
13322 *is_cv_qualifier = true;
13323 break;
13324
13325 case RID_COMPLEX:
13326 /* The `__complex__' keyword is a GNU extension. */
13327 ds = ds_complex;
13328 break;
13329
13330 default:
13331 break;
13332 }
13333
13334 /* Handle simple keywords. */
13335 if (ds != ds_last)
13336 {
13337 if (decl_specs)
13338 {
13339 ++decl_specs->specs[(int)ds];
13340 decl_specs->any_specifiers_p = true;
13341 }
13342 return cp_lexer_consume_token (parser->lexer)->u.value;
13343 }
13344
13345 /* If we do not already have a type-specifier, assume we are looking
13346 at a simple-type-specifier. */
13347 type_spec = cp_parser_simple_type_specifier (parser,
13348 decl_specs,
13349 flags);
13350
13351 /* If we didn't find a type-specifier, and a type-specifier was not
13352 optional in this context, issue an error message. */
13353 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13354 {
13355 cp_parser_error (parser, "expected type specifier");
13356 return error_mark_node;
13357 }
13358
13359 return type_spec;
13360 }
13361
13362 /* Parse a simple-type-specifier.
13363
13364 simple-type-specifier:
13365 :: [opt] nested-name-specifier [opt] type-name
13366 :: [opt] nested-name-specifier template template-id
13367 char
13368 wchar_t
13369 bool
13370 short
13371 int
13372 long
13373 signed
13374 unsigned
13375 float
13376 double
13377 void
13378
13379 C++0x Extension:
13380
13381 simple-type-specifier:
13382 auto
13383 decltype ( expression )
13384 char16_t
13385 char32_t
13386 __underlying_type ( type-id )
13387
13388 GNU Extension:
13389
13390 simple-type-specifier:
13391 __int128
13392 __typeof__ unary-expression
13393 __typeof__ ( type-id )
13394
13395 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13396 appropriately updated. */
13397
13398 static tree
13399 cp_parser_simple_type_specifier (cp_parser* parser,
13400 cp_decl_specifier_seq *decl_specs,
13401 cp_parser_flags flags)
13402 {
13403 tree type = NULL_TREE;
13404 cp_token *token;
13405
13406 /* Peek at the next token. */
13407 token = cp_lexer_peek_token (parser->lexer);
13408
13409 /* If we're looking at a keyword, things are easy. */
13410 switch (token->keyword)
13411 {
13412 case RID_CHAR:
13413 if (decl_specs)
13414 decl_specs->explicit_char_p = true;
13415 type = char_type_node;
13416 break;
13417 case RID_CHAR16:
13418 type = char16_type_node;
13419 break;
13420 case RID_CHAR32:
13421 type = char32_type_node;
13422 break;
13423 case RID_WCHAR:
13424 type = wchar_type_node;
13425 break;
13426 case RID_BOOL:
13427 type = boolean_type_node;
13428 break;
13429 case RID_SHORT:
13430 if (decl_specs)
13431 ++decl_specs->specs[(int) ds_short];
13432 type = short_integer_type_node;
13433 break;
13434 case RID_INT:
13435 if (decl_specs)
13436 decl_specs->explicit_int_p = true;
13437 type = integer_type_node;
13438 break;
13439 case RID_INT128:
13440 if (!int128_integer_type_node)
13441 break;
13442 if (decl_specs)
13443 decl_specs->explicit_int128_p = true;
13444 type = int128_integer_type_node;
13445 break;
13446 case RID_LONG:
13447 if (decl_specs)
13448 ++decl_specs->specs[(int) ds_long];
13449 type = long_integer_type_node;
13450 break;
13451 case RID_SIGNED:
13452 if (decl_specs)
13453 ++decl_specs->specs[(int) ds_signed];
13454 type = integer_type_node;
13455 break;
13456 case RID_UNSIGNED:
13457 if (decl_specs)
13458 ++decl_specs->specs[(int) ds_unsigned];
13459 type = unsigned_type_node;
13460 break;
13461 case RID_FLOAT:
13462 type = float_type_node;
13463 break;
13464 case RID_DOUBLE:
13465 type = double_type_node;
13466 break;
13467 case RID_VOID:
13468 type = void_type_node;
13469 break;
13470
13471 case RID_AUTO:
13472 maybe_warn_cpp0x (CPP0X_AUTO);
13473 type = make_auto ();
13474 break;
13475
13476 case RID_DECLTYPE:
13477 /* Since DR 743, decltype can either be a simple-type-specifier by
13478 itself or begin a nested-name-specifier. Parsing it will replace
13479 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13480 handling below decide what to do. */
13481 cp_parser_decltype (parser);
13482 cp_lexer_set_token_position (parser->lexer, token);
13483 break;
13484
13485 case RID_TYPEOF:
13486 /* Consume the `typeof' token. */
13487 cp_lexer_consume_token (parser->lexer);
13488 /* Parse the operand to `typeof'. */
13489 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13490 /* If it is not already a TYPE, take its type. */
13491 if (!TYPE_P (type))
13492 type = finish_typeof (type);
13493
13494 if (decl_specs)
13495 cp_parser_set_decl_spec_type (decl_specs, type,
13496 token->location,
13497 /*type_definition_p=*/false);
13498
13499 return type;
13500
13501 case RID_UNDERLYING_TYPE:
13502 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13503 if (decl_specs)
13504 cp_parser_set_decl_spec_type (decl_specs, type,
13505 token->location,
13506 /*type_definition_p=*/false);
13507
13508 return type;
13509
13510 case RID_BASES:
13511 case RID_DIRECT_BASES:
13512 type = cp_parser_trait_expr (parser, token->keyword);
13513 if (decl_specs)
13514 cp_parser_set_decl_spec_type (decl_specs, type,
13515 token->location,
13516 /*type_definition_p=*/false);
13517 return type;
13518 default:
13519 break;
13520 }
13521
13522 /* If token is an already-parsed decltype not followed by ::,
13523 it's a simple-type-specifier. */
13524 if (token->type == CPP_DECLTYPE
13525 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13526 {
13527 type = token->u.value;
13528 if (decl_specs)
13529 cp_parser_set_decl_spec_type (decl_specs, type,
13530 token->location,
13531 /*type_definition_p=*/false);
13532 cp_lexer_consume_token (parser->lexer);
13533 return type;
13534 }
13535
13536 /* If the type-specifier was for a built-in type, we're done. */
13537 if (type)
13538 {
13539 /* Record the type. */
13540 if (decl_specs
13541 && (token->keyword != RID_SIGNED
13542 && token->keyword != RID_UNSIGNED
13543 && token->keyword != RID_SHORT
13544 && token->keyword != RID_LONG))
13545 cp_parser_set_decl_spec_type (decl_specs,
13546 type,
13547 token->location,
13548 /*type_definition_p=*/false);
13549 if (decl_specs)
13550 decl_specs->any_specifiers_p = true;
13551
13552 /* Consume the token. */
13553 cp_lexer_consume_token (parser->lexer);
13554
13555 /* There is no valid C++ program where a non-template type is
13556 followed by a "<". That usually indicates that the user thought
13557 that the type was a template. */
13558 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13559
13560 return TYPE_NAME (type);
13561 }
13562
13563 /* The type-specifier must be a user-defined type. */
13564 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13565 {
13566 bool qualified_p;
13567 bool global_p;
13568
13569 /* Don't gobble tokens or issue error messages if this is an
13570 optional type-specifier. */
13571 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13572 cp_parser_parse_tentatively (parser);
13573
13574 /* Look for the optional `::' operator. */
13575 global_p
13576 = (cp_parser_global_scope_opt (parser,
13577 /*current_scope_valid_p=*/false)
13578 != NULL_TREE);
13579 /* Look for the nested-name specifier. */
13580 qualified_p
13581 = (cp_parser_nested_name_specifier_opt (parser,
13582 /*typename_keyword_p=*/false,
13583 /*check_dependency_p=*/true,
13584 /*type_p=*/false,
13585 /*is_declaration=*/false)
13586 != NULL_TREE);
13587 token = cp_lexer_peek_token (parser->lexer);
13588 /* If we have seen a nested-name-specifier, and the next token
13589 is `template', then we are using the template-id production. */
13590 if (parser->scope
13591 && cp_parser_optional_template_keyword (parser))
13592 {
13593 /* Look for the template-id. */
13594 type = cp_parser_template_id (parser,
13595 /*template_keyword_p=*/true,
13596 /*check_dependency_p=*/true,
13597 /*is_declaration=*/false);
13598 /* If the template-id did not name a type, we are out of
13599 luck. */
13600 if (TREE_CODE (type) != TYPE_DECL)
13601 {
13602 cp_parser_error (parser, "expected template-id for type");
13603 type = NULL_TREE;
13604 }
13605 }
13606 /* Otherwise, look for a type-name. */
13607 else
13608 type = cp_parser_type_name (parser);
13609 /* Keep track of all name-lookups performed in class scopes. */
13610 if (type
13611 && !global_p
13612 && !qualified_p
13613 && TREE_CODE (type) == TYPE_DECL
13614 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13615 maybe_note_name_used_in_class (DECL_NAME (type), type);
13616 /* If it didn't work out, we don't have a TYPE. */
13617 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13618 && !cp_parser_parse_definitely (parser))
13619 type = NULL_TREE;
13620 if (type && decl_specs)
13621 cp_parser_set_decl_spec_type (decl_specs, type,
13622 token->location,
13623 /*type_definition_p=*/false);
13624 }
13625
13626 /* If we didn't get a type-name, issue an error message. */
13627 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13628 {
13629 cp_parser_error (parser, "expected type-name");
13630 return error_mark_node;
13631 }
13632
13633 if (type && type != error_mark_node)
13634 {
13635 /* See if TYPE is an Objective-C type, and if so, parse and
13636 accept any protocol references following it. Do this before
13637 the cp_parser_check_for_invalid_template_id() call, because
13638 Objective-C types can be followed by '<...>' which would
13639 enclose protocol names rather than template arguments, and so
13640 everything is fine. */
13641 if (c_dialect_objc () && !parser->scope
13642 && (objc_is_id (type) || objc_is_class_name (type)))
13643 {
13644 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13645 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13646
13647 /* Clobber the "unqualified" type previously entered into
13648 DECL_SPECS with the new, improved protocol-qualified version. */
13649 if (decl_specs)
13650 decl_specs->type = qual_type;
13651
13652 return qual_type;
13653 }
13654
13655 /* There is no valid C++ program where a non-template type is
13656 followed by a "<". That usually indicates that the user
13657 thought that the type was a template. */
13658 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13659 token->location);
13660 }
13661
13662 return type;
13663 }
13664
13665 /* Parse a type-name.
13666
13667 type-name:
13668 class-name
13669 enum-name
13670 typedef-name
13671 simple-template-id [in c++0x]
13672
13673 enum-name:
13674 identifier
13675
13676 typedef-name:
13677 identifier
13678
13679 Returns a TYPE_DECL for the type. */
13680
13681 static tree
13682 cp_parser_type_name (cp_parser* parser)
13683 {
13684 tree type_decl;
13685
13686 /* We can't know yet whether it is a class-name or not. */
13687 cp_parser_parse_tentatively (parser);
13688 /* Try a class-name. */
13689 type_decl = cp_parser_class_name (parser,
13690 /*typename_keyword_p=*/false,
13691 /*template_keyword_p=*/false,
13692 none_type,
13693 /*check_dependency_p=*/true,
13694 /*class_head_p=*/false,
13695 /*is_declaration=*/false);
13696 /* If it's not a class-name, keep looking. */
13697 if (!cp_parser_parse_definitely (parser))
13698 {
13699 if (cxx_dialect < cxx0x)
13700 /* It must be a typedef-name or an enum-name. */
13701 return cp_parser_nonclass_name (parser);
13702
13703 cp_parser_parse_tentatively (parser);
13704 /* It is either a simple-template-id representing an
13705 instantiation of an alias template... */
13706 type_decl = cp_parser_template_id (parser,
13707 /*template_keyword_p=*/false,
13708 /*check_dependency_p=*/false,
13709 /*is_declaration=*/false);
13710 /* Note that this must be an instantiation of an alias template
13711 because [temp.names]/6 says:
13712
13713 A template-id that names an alias template specialization
13714 is a type-name.
13715
13716 Whereas [temp.names]/7 says:
13717
13718 A simple-template-id that names a class template
13719 specialization is a class-name. */
13720 if (type_decl != NULL_TREE
13721 && TREE_CODE (type_decl) == TYPE_DECL
13722 && TYPE_DECL_ALIAS_P (type_decl))
13723 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13724 else
13725 cp_parser_simulate_error (parser);
13726
13727 if (!cp_parser_parse_definitely (parser))
13728 /* ... Or a typedef-name or an enum-name. */
13729 return cp_parser_nonclass_name (parser);
13730 }
13731
13732 return type_decl;
13733 }
13734
13735 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13736
13737 enum-name:
13738 identifier
13739
13740 typedef-name:
13741 identifier
13742
13743 Returns a TYPE_DECL for the type. */
13744
13745 static tree
13746 cp_parser_nonclass_name (cp_parser* parser)
13747 {
13748 tree type_decl;
13749 tree identifier;
13750
13751 cp_token *token = cp_lexer_peek_token (parser->lexer);
13752 identifier = cp_parser_identifier (parser);
13753 if (identifier == error_mark_node)
13754 return error_mark_node;
13755
13756 /* Look up the type-name. */
13757 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13758
13759 if (TREE_CODE (type_decl) != TYPE_DECL
13760 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13761 {
13762 /* See if this is an Objective-C type. */
13763 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13764 tree type = objc_get_protocol_qualified_type (identifier, protos);
13765 if (type)
13766 type_decl = TYPE_NAME (type);
13767 }
13768
13769 /* Issue an error if we did not find a type-name. */
13770 if (TREE_CODE (type_decl) != TYPE_DECL
13771 /* In Objective-C, we have the complication that class names are
13772 normally type names and start declarations (eg, the
13773 "NSObject" in "NSObject *object;"), but can be used in an
13774 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13775 is an expression. So, a classname followed by a dot is not a
13776 valid type-name. */
13777 || (objc_is_class_name (TREE_TYPE (type_decl))
13778 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13779 {
13780 if (!cp_parser_simulate_error (parser))
13781 cp_parser_name_lookup_error (parser, identifier, type_decl,
13782 NLE_TYPE, token->location);
13783 return error_mark_node;
13784 }
13785 /* Remember that the name was used in the definition of the
13786 current class so that we can check later to see if the
13787 meaning would have been different after the class was
13788 entirely defined. */
13789 else if (type_decl != error_mark_node
13790 && !parser->scope)
13791 maybe_note_name_used_in_class (identifier, type_decl);
13792
13793 return type_decl;
13794 }
13795
13796 /* Parse an elaborated-type-specifier. Note that the grammar given
13797 here incorporates the resolution to DR68.
13798
13799 elaborated-type-specifier:
13800 class-key :: [opt] nested-name-specifier [opt] identifier
13801 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13802 enum-key :: [opt] nested-name-specifier [opt] identifier
13803 typename :: [opt] nested-name-specifier identifier
13804 typename :: [opt] nested-name-specifier template [opt]
13805 template-id
13806
13807 GNU extension:
13808
13809 elaborated-type-specifier:
13810 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13811 class-key attributes :: [opt] nested-name-specifier [opt]
13812 template [opt] template-id
13813 enum attributes :: [opt] nested-name-specifier [opt] identifier
13814
13815 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13816 declared `friend'. If IS_DECLARATION is TRUE, then this
13817 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13818 something is being declared.
13819
13820 Returns the TYPE specified. */
13821
13822 static tree
13823 cp_parser_elaborated_type_specifier (cp_parser* parser,
13824 bool is_friend,
13825 bool is_declaration)
13826 {
13827 enum tag_types tag_type;
13828 tree identifier;
13829 tree type = NULL_TREE;
13830 tree attributes = NULL_TREE;
13831 tree globalscope;
13832 cp_token *token = NULL;
13833
13834 /* See if we're looking at the `enum' keyword. */
13835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13836 {
13837 /* Consume the `enum' token. */
13838 cp_lexer_consume_token (parser->lexer);
13839 /* Remember that it's an enumeration type. */
13840 tag_type = enum_type;
13841 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13842 enums) is used here. */
13843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13844 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13845 {
13846 pedwarn (input_location, 0, "elaborated-type-specifier "
13847 "for a scoped enum must not use the %<%D%> keyword",
13848 cp_lexer_peek_token (parser->lexer)->u.value);
13849 /* Consume the `struct' or `class' and parse it anyway. */
13850 cp_lexer_consume_token (parser->lexer);
13851 }
13852 /* Parse the attributes. */
13853 attributes = cp_parser_attributes_opt (parser);
13854 }
13855 /* Or, it might be `typename'. */
13856 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13857 RID_TYPENAME))
13858 {
13859 /* Consume the `typename' token. */
13860 cp_lexer_consume_token (parser->lexer);
13861 /* Remember that it's a `typename' type. */
13862 tag_type = typename_type;
13863 }
13864 /* Otherwise it must be a class-key. */
13865 else
13866 {
13867 tag_type = cp_parser_class_key (parser);
13868 if (tag_type == none_type)
13869 return error_mark_node;
13870 /* Parse the attributes. */
13871 attributes = cp_parser_attributes_opt (parser);
13872 }
13873
13874 /* Look for the `::' operator. */
13875 globalscope = cp_parser_global_scope_opt (parser,
13876 /*current_scope_valid_p=*/false);
13877 /* Look for the nested-name-specifier. */
13878 if (tag_type == typename_type && !globalscope)
13879 {
13880 if (!cp_parser_nested_name_specifier (parser,
13881 /*typename_keyword_p=*/true,
13882 /*check_dependency_p=*/true,
13883 /*type_p=*/true,
13884 is_declaration))
13885 return error_mark_node;
13886 }
13887 else
13888 /* Even though `typename' is not present, the proposed resolution
13889 to Core Issue 180 says that in `class A<T>::B', `B' should be
13890 considered a type-name, even if `A<T>' is dependent. */
13891 cp_parser_nested_name_specifier_opt (parser,
13892 /*typename_keyword_p=*/true,
13893 /*check_dependency_p=*/true,
13894 /*type_p=*/true,
13895 is_declaration);
13896 /* For everything but enumeration types, consider a template-id.
13897 For an enumeration type, consider only a plain identifier. */
13898 if (tag_type != enum_type)
13899 {
13900 bool template_p = false;
13901 tree decl;
13902
13903 /* Allow the `template' keyword. */
13904 template_p = cp_parser_optional_template_keyword (parser);
13905 /* If we didn't see `template', we don't know if there's a
13906 template-id or not. */
13907 if (!template_p)
13908 cp_parser_parse_tentatively (parser);
13909 /* Parse the template-id. */
13910 token = cp_lexer_peek_token (parser->lexer);
13911 decl = cp_parser_template_id (parser, template_p,
13912 /*check_dependency_p=*/true,
13913 is_declaration);
13914 /* If we didn't find a template-id, look for an ordinary
13915 identifier. */
13916 if (!template_p && !cp_parser_parse_definitely (parser))
13917 ;
13918 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13919 in effect, then we must assume that, upon instantiation, the
13920 template will correspond to a class. */
13921 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13922 && tag_type == typename_type)
13923 type = make_typename_type (parser->scope, decl,
13924 typename_type,
13925 /*complain=*/tf_error);
13926 /* If the `typename' keyword is in effect and DECL is not a type
13927 decl. Then type is non existant. */
13928 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13929 type = NULL_TREE;
13930 else
13931 type = TREE_TYPE (decl);
13932 }
13933
13934 if (!type)
13935 {
13936 token = cp_lexer_peek_token (parser->lexer);
13937 identifier = cp_parser_identifier (parser);
13938
13939 if (identifier == error_mark_node)
13940 {
13941 parser->scope = NULL_TREE;
13942 return error_mark_node;
13943 }
13944
13945 /* For a `typename', we needn't call xref_tag. */
13946 if (tag_type == typename_type
13947 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13948 return cp_parser_make_typename_type (parser, parser->scope,
13949 identifier,
13950 token->location);
13951 /* Look up a qualified name in the usual way. */
13952 if (parser->scope)
13953 {
13954 tree decl;
13955 tree ambiguous_decls;
13956
13957 decl = cp_parser_lookup_name (parser, identifier,
13958 tag_type,
13959 /*is_template=*/false,
13960 /*is_namespace=*/false,
13961 /*check_dependency=*/true,
13962 &ambiguous_decls,
13963 token->location);
13964
13965 /* If the lookup was ambiguous, an error will already have been
13966 issued. */
13967 if (ambiguous_decls)
13968 return error_mark_node;
13969
13970 /* If we are parsing friend declaration, DECL may be a
13971 TEMPLATE_DECL tree node here. However, we need to check
13972 whether this TEMPLATE_DECL results in valid code. Consider
13973 the following example:
13974
13975 namespace N {
13976 template <class T> class C {};
13977 }
13978 class X {
13979 template <class T> friend class N::C; // #1, valid code
13980 };
13981 template <class T> class Y {
13982 friend class N::C; // #2, invalid code
13983 };
13984
13985 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13986 name lookup of `N::C'. We see that friend declaration must
13987 be template for the code to be valid. Note that
13988 processing_template_decl does not work here since it is
13989 always 1 for the above two cases. */
13990
13991 decl = (cp_parser_maybe_treat_template_as_class
13992 (decl, /*tag_name_p=*/is_friend
13993 && parser->num_template_parameter_lists));
13994
13995 if (TREE_CODE (decl) != TYPE_DECL)
13996 {
13997 cp_parser_diagnose_invalid_type_name (parser,
13998 parser->scope,
13999 identifier,
14000 token->location);
14001 return error_mark_node;
14002 }
14003
14004 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14005 {
14006 bool allow_template = (parser->num_template_parameter_lists
14007 || DECL_SELF_REFERENCE_P (decl));
14008 type = check_elaborated_type_specifier (tag_type, decl,
14009 allow_template);
14010
14011 if (type == error_mark_node)
14012 return error_mark_node;
14013 }
14014
14015 /* Forward declarations of nested types, such as
14016
14017 class C1::C2;
14018 class C1::C2::C3;
14019
14020 are invalid unless all components preceding the final '::'
14021 are complete. If all enclosing types are complete, these
14022 declarations become merely pointless.
14023
14024 Invalid forward declarations of nested types are errors
14025 caught elsewhere in parsing. Those that are pointless arrive
14026 here. */
14027
14028 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14029 && !is_friend && !processing_explicit_instantiation)
14030 warning (0, "declaration %qD does not declare anything", decl);
14031
14032 type = TREE_TYPE (decl);
14033 }
14034 else
14035 {
14036 /* An elaborated-type-specifier sometimes introduces a new type and
14037 sometimes names an existing type. Normally, the rule is that it
14038 introduces a new type only if there is not an existing type of
14039 the same name already in scope. For example, given:
14040
14041 struct S {};
14042 void f() { struct S s; }
14043
14044 the `struct S' in the body of `f' is the same `struct S' as in
14045 the global scope; the existing definition is used. However, if
14046 there were no global declaration, this would introduce a new
14047 local class named `S'.
14048
14049 An exception to this rule applies to the following code:
14050
14051 namespace N { struct S; }
14052
14053 Here, the elaborated-type-specifier names a new type
14054 unconditionally; even if there is already an `S' in the
14055 containing scope this declaration names a new type.
14056 This exception only applies if the elaborated-type-specifier
14057 forms the complete declaration:
14058
14059 [class.name]
14060
14061 A declaration consisting solely of `class-key identifier ;' is
14062 either a redeclaration of the name in the current scope or a
14063 forward declaration of the identifier as a class name. It
14064 introduces the name into the current scope.
14065
14066 We are in this situation precisely when the next token is a `;'.
14067
14068 An exception to the exception is that a `friend' declaration does
14069 *not* name a new type; i.e., given:
14070
14071 struct S { friend struct T; };
14072
14073 `T' is not a new type in the scope of `S'.
14074
14075 Also, `new struct S' or `sizeof (struct S)' never results in the
14076 definition of a new type; a new type can only be declared in a
14077 declaration context. */
14078
14079 tag_scope ts;
14080 bool template_p;
14081
14082 if (is_friend)
14083 /* Friends have special name lookup rules. */
14084 ts = ts_within_enclosing_non_class;
14085 else if (is_declaration
14086 && cp_lexer_next_token_is (parser->lexer,
14087 CPP_SEMICOLON))
14088 /* This is a `class-key identifier ;' */
14089 ts = ts_current;
14090 else
14091 ts = ts_global;
14092
14093 template_p =
14094 (parser->num_template_parameter_lists
14095 && (cp_parser_next_token_starts_class_definition_p (parser)
14096 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14097 /* An unqualified name was used to reference this type, so
14098 there were no qualifying templates. */
14099 if (!cp_parser_check_template_parameters (parser,
14100 /*num_templates=*/0,
14101 token->location,
14102 /*declarator=*/NULL))
14103 return error_mark_node;
14104 type = xref_tag (tag_type, identifier, ts, template_p);
14105 }
14106 }
14107
14108 if (type == error_mark_node)
14109 return error_mark_node;
14110
14111 /* Allow attributes on forward declarations of classes. */
14112 if (attributes)
14113 {
14114 if (TREE_CODE (type) == TYPENAME_TYPE)
14115 warning (OPT_Wattributes,
14116 "attributes ignored on uninstantiated type");
14117 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14118 && ! processing_explicit_instantiation)
14119 warning (OPT_Wattributes,
14120 "attributes ignored on template instantiation");
14121 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14122 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14123 else
14124 warning (OPT_Wattributes,
14125 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14126 }
14127
14128 if (tag_type != enum_type)
14129 {
14130 /* Indicate whether this class was declared as a `class' or as a
14131 `struct'. */
14132 if (TREE_CODE (type) == RECORD_TYPE)
14133 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14134 cp_parser_check_class_key (tag_type, type);
14135 }
14136
14137 /* A "<" cannot follow an elaborated type specifier. If that
14138 happens, the user was probably trying to form a template-id. */
14139 cp_parser_check_for_invalid_template_id (parser, type, token->location);
14140
14141 return type;
14142 }
14143
14144 /* Parse an enum-specifier.
14145
14146 enum-specifier:
14147 enum-head { enumerator-list [opt] }
14148 enum-head { enumerator-list , } [C++0x]
14149
14150 enum-head:
14151 enum-key identifier [opt] enum-base [opt]
14152 enum-key nested-name-specifier identifier enum-base [opt]
14153
14154 enum-key:
14155 enum
14156 enum class [C++0x]
14157 enum struct [C++0x]
14158
14159 enum-base: [C++0x]
14160 : type-specifier-seq
14161
14162 opaque-enum-specifier:
14163 enum-key identifier enum-base [opt] ;
14164
14165 GNU Extensions:
14166 enum-key attributes[opt] identifier [opt] enum-base [opt]
14167 { enumerator-list [opt] }attributes[opt]
14168 enum-key attributes[opt] identifier [opt] enum-base [opt]
14169 { enumerator-list, }attributes[opt] [C++0x]
14170
14171 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14172 if the token stream isn't an enum-specifier after all. */
14173
14174 static tree
14175 cp_parser_enum_specifier (cp_parser* parser)
14176 {
14177 tree identifier;
14178 tree type = NULL_TREE;
14179 tree prev_scope;
14180 tree nested_name_specifier = NULL_TREE;
14181 tree attributes;
14182 bool scoped_enum_p = false;
14183 bool has_underlying_type = false;
14184 bool nested_being_defined = false;
14185 bool new_value_list = false;
14186 bool is_new_type = false;
14187 bool is_anonymous = false;
14188 tree underlying_type = NULL_TREE;
14189 cp_token *type_start_token = NULL;
14190 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14191
14192 parser->colon_corrects_to_scope_p = false;
14193
14194 /* Parse tentatively so that we can back up if we don't find a
14195 enum-specifier. */
14196 cp_parser_parse_tentatively (parser);
14197
14198 /* Caller guarantees that the current token is 'enum', an identifier
14199 possibly follows, and the token after that is an opening brace.
14200 If we don't have an identifier, fabricate an anonymous name for
14201 the enumeration being defined. */
14202 cp_lexer_consume_token (parser->lexer);
14203
14204 /* Parse the "class" or "struct", which indicates a scoped
14205 enumeration type in C++0x. */
14206 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14207 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14208 {
14209 if (cxx_dialect < cxx0x)
14210 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14211
14212 /* Consume the `struct' or `class' token. */
14213 cp_lexer_consume_token (parser->lexer);
14214
14215 scoped_enum_p = true;
14216 }
14217
14218 attributes = cp_parser_attributes_opt (parser);
14219
14220 /* Clear the qualification. */
14221 parser->scope = NULL_TREE;
14222 parser->qualifying_scope = NULL_TREE;
14223 parser->object_scope = NULL_TREE;
14224
14225 /* Figure out in what scope the declaration is being placed. */
14226 prev_scope = current_scope ();
14227
14228 type_start_token = cp_lexer_peek_token (parser->lexer);
14229
14230 push_deferring_access_checks (dk_no_check);
14231 nested_name_specifier
14232 = cp_parser_nested_name_specifier_opt (parser,
14233 /*typename_keyword_p=*/true,
14234 /*check_dependency_p=*/false,
14235 /*type_p=*/false,
14236 /*is_declaration=*/false);
14237
14238 if (nested_name_specifier)
14239 {
14240 tree name;
14241
14242 identifier = cp_parser_identifier (parser);
14243 name = cp_parser_lookup_name (parser, identifier,
14244 enum_type,
14245 /*is_template=*/false,
14246 /*is_namespace=*/false,
14247 /*check_dependency=*/true,
14248 /*ambiguous_decls=*/NULL,
14249 input_location);
14250 if (name)
14251 {
14252 type = TREE_TYPE (name);
14253 if (TREE_CODE (type) == TYPENAME_TYPE)
14254 {
14255 /* Are template enums allowed in ISO? */
14256 if (template_parm_scope_p ())
14257 pedwarn (type_start_token->location, OPT_pedantic,
14258 "%qD is an enumeration template", name);
14259 /* ignore a typename reference, for it will be solved by name
14260 in start_enum. */
14261 type = NULL_TREE;
14262 }
14263 }
14264 else
14265 error_at (type_start_token->location,
14266 "%qD is not an enumerator-name", identifier);
14267 }
14268 else
14269 {
14270 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14271 identifier = cp_parser_identifier (parser);
14272 else
14273 {
14274 identifier = make_anon_name ();
14275 is_anonymous = true;
14276 }
14277 }
14278 pop_deferring_access_checks ();
14279
14280 /* Check for the `:' that denotes a specified underlying type in C++0x.
14281 Note that a ':' could also indicate a bitfield width, however. */
14282 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14283 {
14284 cp_decl_specifier_seq type_specifiers;
14285
14286 /* Consume the `:'. */
14287 cp_lexer_consume_token (parser->lexer);
14288
14289 /* Parse the type-specifier-seq. */
14290 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14291 /*is_trailing_return=*/false,
14292 &type_specifiers);
14293
14294 /* At this point this is surely not elaborated type specifier. */
14295 if (!cp_parser_parse_definitely (parser))
14296 return NULL_TREE;
14297
14298 if (cxx_dialect < cxx0x)
14299 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14300
14301 has_underlying_type = true;
14302
14303 /* If that didn't work, stop. */
14304 if (type_specifiers.type != error_mark_node)
14305 {
14306 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14307 /*initialized=*/0, NULL);
14308 if (underlying_type == error_mark_node)
14309 underlying_type = NULL_TREE;
14310 }
14311 }
14312
14313 /* Look for the `{' but don't consume it yet. */
14314 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14315 {
14316 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14317 {
14318 cp_parser_error (parser, "expected %<{%>");
14319 if (has_underlying_type)
14320 {
14321 type = NULL_TREE;
14322 goto out;
14323 }
14324 }
14325 /* An opaque-enum-specifier must have a ';' here. */
14326 if ((scoped_enum_p || underlying_type)
14327 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14328 {
14329 cp_parser_error (parser, "expected %<;%> or %<{%>");
14330 if (has_underlying_type)
14331 {
14332 type = NULL_TREE;
14333 goto out;
14334 }
14335 }
14336 }
14337
14338 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14339 return NULL_TREE;
14340
14341 if (nested_name_specifier)
14342 {
14343 if (CLASS_TYPE_P (nested_name_specifier))
14344 {
14345 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14346 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14347 push_scope (nested_name_specifier);
14348 }
14349 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14350 {
14351 push_nested_namespace (nested_name_specifier);
14352 }
14353 }
14354
14355 /* Issue an error message if type-definitions are forbidden here. */
14356 if (!cp_parser_check_type_definition (parser))
14357 type = error_mark_node;
14358 else
14359 /* Create the new type. We do this before consuming the opening
14360 brace so the enum will be recorded as being on the line of its
14361 tag (or the 'enum' keyword, if there is no tag). */
14362 type = start_enum (identifier, type, underlying_type,
14363 scoped_enum_p, &is_new_type);
14364
14365 /* If the next token is not '{' it is an opaque-enum-specifier or an
14366 elaborated-type-specifier. */
14367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14368 {
14369 timevar_push (TV_PARSE_ENUM);
14370 if (nested_name_specifier)
14371 {
14372 /* The following catches invalid code such as:
14373 enum class S<int>::E { A, B, C }; */
14374 if (!processing_specialization
14375 && CLASS_TYPE_P (nested_name_specifier)
14376 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14377 error_at (type_start_token->location, "cannot add an enumerator "
14378 "list to a template instantiation");
14379
14380 /* If that scope does not contain the scope in which the
14381 class was originally declared, the program is invalid. */
14382 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14383 {
14384 if (at_namespace_scope_p ())
14385 error_at (type_start_token->location,
14386 "declaration of %qD in namespace %qD which does not "
14387 "enclose %qD",
14388 type, prev_scope, nested_name_specifier);
14389 else
14390 error_at (type_start_token->location,
14391 "declaration of %qD in %qD which does not enclose %qD",
14392 type, prev_scope, nested_name_specifier);
14393 type = error_mark_node;
14394 }
14395 }
14396
14397 if (scoped_enum_p)
14398 begin_scope (sk_scoped_enum, type);
14399
14400 /* Consume the opening brace. */
14401 cp_lexer_consume_token (parser->lexer);
14402
14403 if (type == error_mark_node)
14404 ; /* Nothing to add */
14405 else if (OPAQUE_ENUM_P (type)
14406 || (cxx_dialect > cxx98 && processing_specialization))
14407 {
14408 new_value_list = true;
14409 SET_OPAQUE_ENUM_P (type, false);
14410 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14411 }
14412 else
14413 {
14414 error_at (type_start_token->location, "multiple definition of %q#T", type);
14415 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14416 "previous definition here");
14417 type = error_mark_node;
14418 }
14419
14420 if (type == error_mark_node)
14421 cp_parser_skip_to_end_of_block_or_statement (parser);
14422 /* If the next token is not '}', then there are some enumerators. */
14423 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14424 cp_parser_enumerator_list (parser, type);
14425
14426 /* Consume the final '}'. */
14427 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14428
14429 if (scoped_enum_p)
14430 finish_scope ();
14431 timevar_pop (TV_PARSE_ENUM);
14432 }
14433 else
14434 {
14435 /* If a ';' follows, then it is an opaque-enum-specifier
14436 and additional restrictions apply. */
14437 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14438 {
14439 if (is_anonymous)
14440 error_at (type_start_token->location,
14441 "opaque-enum-specifier without name");
14442 else if (nested_name_specifier)
14443 error_at (type_start_token->location,
14444 "opaque-enum-specifier must use a simple identifier");
14445 }
14446 }
14447
14448 /* Look for trailing attributes to apply to this enumeration, and
14449 apply them if appropriate. */
14450 if (cp_parser_allow_gnu_extensions_p (parser))
14451 {
14452 tree trailing_attr = cp_parser_attributes_opt (parser);
14453 trailing_attr = chainon (trailing_attr, attributes);
14454 cplus_decl_attributes (&type,
14455 trailing_attr,
14456 (int) ATTR_FLAG_TYPE_IN_PLACE);
14457 }
14458
14459 /* Finish up the enumeration. */
14460 if (type != error_mark_node)
14461 {
14462 if (new_value_list)
14463 finish_enum_value_list (type);
14464 if (is_new_type)
14465 finish_enum (type);
14466 }
14467
14468 if (nested_name_specifier)
14469 {
14470 if (CLASS_TYPE_P (nested_name_specifier))
14471 {
14472 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14473 pop_scope (nested_name_specifier);
14474 }
14475 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14476 {
14477 pop_nested_namespace (nested_name_specifier);
14478 }
14479 }
14480 out:
14481 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14482 return type;
14483 }
14484
14485 /* Parse an enumerator-list. The enumerators all have the indicated
14486 TYPE.
14487
14488 enumerator-list:
14489 enumerator-definition
14490 enumerator-list , enumerator-definition */
14491
14492 static void
14493 cp_parser_enumerator_list (cp_parser* parser, tree type)
14494 {
14495 while (true)
14496 {
14497 /* Parse an enumerator-definition. */
14498 cp_parser_enumerator_definition (parser, type);
14499
14500 /* If the next token is not a ',', we've reached the end of
14501 the list. */
14502 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14503 break;
14504 /* Otherwise, consume the `,' and keep going. */
14505 cp_lexer_consume_token (parser->lexer);
14506 /* If the next token is a `}', there is a trailing comma. */
14507 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14508 {
14509 if (cxx_dialect < cxx0x && !in_system_header)
14510 pedwarn (input_location, OPT_pedantic,
14511 "comma at end of enumerator list");
14512 break;
14513 }
14514 }
14515 }
14516
14517 /* Parse an enumerator-definition. The enumerator has the indicated
14518 TYPE.
14519
14520 enumerator-definition:
14521 enumerator
14522 enumerator = constant-expression
14523
14524 enumerator:
14525 identifier */
14526
14527 static void
14528 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14529 {
14530 tree identifier;
14531 tree value;
14532 location_t loc;
14533
14534 /* Save the input location because we are interested in the location
14535 of the identifier and not the location of the explicit value. */
14536 loc = cp_lexer_peek_token (parser->lexer)->location;
14537
14538 /* Look for the identifier. */
14539 identifier = cp_parser_identifier (parser);
14540 if (identifier == error_mark_node)
14541 return;
14542
14543 /* If the next token is an '=', then there is an explicit value. */
14544 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14545 {
14546 /* Consume the `=' token. */
14547 cp_lexer_consume_token (parser->lexer);
14548 /* Parse the value. */
14549 value = cp_parser_constant_expression (parser,
14550 /*allow_non_constant_p=*/false,
14551 NULL);
14552 }
14553 else
14554 value = NULL_TREE;
14555
14556 /* If we are processing a template, make sure the initializer of the
14557 enumerator doesn't contain any bare template parameter pack. */
14558 if (check_for_bare_parameter_packs (value))
14559 value = error_mark_node;
14560
14561 /* integral_constant_value will pull out this expression, so make sure
14562 it's folded as appropriate. */
14563 value = fold_non_dependent_expr (value);
14564
14565 /* Create the enumerator. */
14566 build_enumerator (identifier, value, type, loc);
14567 }
14568
14569 /* Parse a namespace-name.
14570
14571 namespace-name:
14572 original-namespace-name
14573 namespace-alias
14574
14575 Returns the NAMESPACE_DECL for the namespace. */
14576
14577 static tree
14578 cp_parser_namespace_name (cp_parser* parser)
14579 {
14580 tree identifier;
14581 tree namespace_decl;
14582
14583 cp_token *token = cp_lexer_peek_token (parser->lexer);
14584
14585 /* Get the name of the namespace. */
14586 identifier = cp_parser_identifier (parser);
14587 if (identifier == error_mark_node)
14588 return error_mark_node;
14589
14590 /* Look up the identifier in the currently active scope. Look only
14591 for namespaces, due to:
14592
14593 [basic.lookup.udir]
14594
14595 When looking up a namespace-name in a using-directive or alias
14596 definition, only namespace names are considered.
14597
14598 And:
14599
14600 [basic.lookup.qual]
14601
14602 During the lookup of a name preceding the :: scope resolution
14603 operator, object, function, and enumerator names are ignored.
14604
14605 (Note that cp_parser_qualifying_entity only calls this
14606 function if the token after the name is the scope resolution
14607 operator.) */
14608 namespace_decl = cp_parser_lookup_name (parser, identifier,
14609 none_type,
14610 /*is_template=*/false,
14611 /*is_namespace=*/true,
14612 /*check_dependency=*/true,
14613 /*ambiguous_decls=*/NULL,
14614 token->location);
14615 /* If it's not a namespace, issue an error. */
14616 if (namespace_decl == error_mark_node
14617 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14618 {
14619 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14620 error_at (token->location, "%qD is not a namespace-name", identifier);
14621 cp_parser_error (parser, "expected namespace-name");
14622 namespace_decl = error_mark_node;
14623 }
14624
14625 return namespace_decl;
14626 }
14627
14628 /* Parse a namespace-definition.
14629
14630 namespace-definition:
14631 named-namespace-definition
14632 unnamed-namespace-definition
14633
14634 named-namespace-definition:
14635 original-namespace-definition
14636 extension-namespace-definition
14637
14638 original-namespace-definition:
14639 namespace identifier { namespace-body }
14640
14641 extension-namespace-definition:
14642 namespace original-namespace-name { namespace-body }
14643
14644 unnamed-namespace-definition:
14645 namespace { namespace-body } */
14646
14647 static void
14648 cp_parser_namespace_definition (cp_parser* parser)
14649 {
14650 tree identifier, attribs;
14651 bool has_visibility;
14652 bool is_inline;
14653
14654 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14655 {
14656 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14657 is_inline = true;
14658 cp_lexer_consume_token (parser->lexer);
14659 }
14660 else
14661 is_inline = false;
14662
14663 /* Look for the `namespace' keyword. */
14664 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14665
14666 /* Get the name of the namespace. We do not attempt to distinguish
14667 between an original-namespace-definition and an
14668 extension-namespace-definition at this point. The semantic
14669 analysis routines are responsible for that. */
14670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14671 identifier = cp_parser_identifier (parser);
14672 else
14673 identifier = NULL_TREE;
14674
14675 /* Parse any specified attributes. */
14676 attribs = cp_parser_attributes_opt (parser);
14677
14678 /* Look for the `{' to start the namespace. */
14679 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14680 /* Start the namespace. */
14681 push_namespace (identifier);
14682
14683 /* "inline namespace" is equivalent to a stub namespace definition
14684 followed by a strong using directive. */
14685 if (is_inline)
14686 {
14687 tree name_space = current_namespace;
14688 /* Set up namespace association. */
14689 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14690 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14691 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14692 /* Import the contents of the inline namespace. */
14693 pop_namespace ();
14694 do_using_directive (name_space);
14695 push_namespace (identifier);
14696 }
14697
14698 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14699
14700 /* Parse the body of the namespace. */
14701 cp_parser_namespace_body (parser);
14702
14703 if (has_visibility)
14704 pop_visibility (1);
14705
14706 /* Finish the namespace. */
14707 pop_namespace ();
14708 /* Look for the final `}'. */
14709 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14710 }
14711
14712 /* Parse a namespace-body.
14713
14714 namespace-body:
14715 declaration-seq [opt] */
14716
14717 static void
14718 cp_parser_namespace_body (cp_parser* parser)
14719 {
14720 cp_parser_declaration_seq_opt (parser);
14721 }
14722
14723 /* Parse a namespace-alias-definition.
14724
14725 namespace-alias-definition:
14726 namespace identifier = qualified-namespace-specifier ; */
14727
14728 static void
14729 cp_parser_namespace_alias_definition (cp_parser* parser)
14730 {
14731 tree identifier;
14732 tree namespace_specifier;
14733
14734 cp_token *token = cp_lexer_peek_token (parser->lexer);
14735
14736 /* Look for the `namespace' keyword. */
14737 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14738 /* Look for the identifier. */
14739 identifier = cp_parser_identifier (parser);
14740 if (identifier == error_mark_node)
14741 return;
14742 /* Look for the `=' token. */
14743 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14744 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14745 {
14746 error_at (token->location, "%<namespace%> definition is not allowed here");
14747 /* Skip the definition. */
14748 cp_lexer_consume_token (parser->lexer);
14749 if (cp_parser_skip_to_closing_brace (parser))
14750 cp_lexer_consume_token (parser->lexer);
14751 return;
14752 }
14753 cp_parser_require (parser, CPP_EQ, RT_EQ);
14754 /* Look for the qualified-namespace-specifier. */
14755 namespace_specifier
14756 = cp_parser_qualified_namespace_specifier (parser);
14757 /* Look for the `;' token. */
14758 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14759
14760 /* Register the alias in the symbol table. */
14761 do_namespace_alias (identifier, namespace_specifier);
14762 }
14763
14764 /* Parse a qualified-namespace-specifier.
14765
14766 qualified-namespace-specifier:
14767 :: [opt] nested-name-specifier [opt] namespace-name
14768
14769 Returns a NAMESPACE_DECL corresponding to the specified
14770 namespace. */
14771
14772 static tree
14773 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14774 {
14775 /* Look for the optional `::'. */
14776 cp_parser_global_scope_opt (parser,
14777 /*current_scope_valid_p=*/false);
14778
14779 /* Look for the optional nested-name-specifier. */
14780 cp_parser_nested_name_specifier_opt (parser,
14781 /*typename_keyword_p=*/false,
14782 /*check_dependency_p=*/true,
14783 /*type_p=*/false,
14784 /*is_declaration=*/true);
14785
14786 return cp_parser_namespace_name (parser);
14787 }
14788
14789 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14790 access declaration.
14791
14792 using-declaration:
14793 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14794 using :: unqualified-id ;
14795
14796 access-declaration:
14797 qualified-id ;
14798
14799 */
14800
14801 static bool
14802 cp_parser_using_declaration (cp_parser* parser,
14803 bool access_declaration_p)
14804 {
14805 cp_token *token;
14806 bool typename_p = false;
14807 bool global_scope_p;
14808 tree decl;
14809 tree identifier;
14810 tree qscope;
14811
14812 if (access_declaration_p)
14813 cp_parser_parse_tentatively (parser);
14814 else
14815 {
14816 /* Look for the `using' keyword. */
14817 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14818
14819 /* Peek at the next token. */
14820 token = cp_lexer_peek_token (parser->lexer);
14821 /* See if it's `typename'. */
14822 if (token->keyword == RID_TYPENAME)
14823 {
14824 /* Remember that we've seen it. */
14825 typename_p = true;
14826 /* Consume the `typename' token. */
14827 cp_lexer_consume_token (parser->lexer);
14828 }
14829 }
14830
14831 /* Look for the optional global scope qualification. */
14832 global_scope_p
14833 = (cp_parser_global_scope_opt (parser,
14834 /*current_scope_valid_p=*/false)
14835 != NULL_TREE);
14836
14837 /* If we saw `typename', or didn't see `::', then there must be a
14838 nested-name-specifier present. */
14839 if (typename_p || !global_scope_p)
14840 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14841 /*check_dependency_p=*/true,
14842 /*type_p=*/false,
14843 /*is_declaration=*/true);
14844 /* Otherwise, we could be in either of the two productions. In that
14845 case, treat the nested-name-specifier as optional. */
14846 else
14847 qscope = cp_parser_nested_name_specifier_opt (parser,
14848 /*typename_keyword_p=*/false,
14849 /*check_dependency_p=*/true,
14850 /*type_p=*/false,
14851 /*is_declaration=*/true);
14852 if (!qscope)
14853 qscope = global_namespace;
14854
14855 if (access_declaration_p && cp_parser_error_occurred (parser))
14856 /* Something has already gone wrong; there's no need to parse
14857 further. Since an error has occurred, the return value of
14858 cp_parser_parse_definitely will be false, as required. */
14859 return cp_parser_parse_definitely (parser);
14860
14861 token = cp_lexer_peek_token (parser->lexer);
14862 /* Parse the unqualified-id. */
14863 identifier = cp_parser_unqualified_id (parser,
14864 /*template_keyword_p=*/false,
14865 /*check_dependency_p=*/true,
14866 /*declarator_p=*/true,
14867 /*optional_p=*/false);
14868
14869 if (access_declaration_p)
14870 {
14871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14872 cp_parser_simulate_error (parser);
14873 if (!cp_parser_parse_definitely (parser))
14874 return false;
14875 }
14876
14877 /* The function we call to handle a using-declaration is different
14878 depending on what scope we are in. */
14879 if (qscope == error_mark_node || identifier == error_mark_node)
14880 ;
14881 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14882 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14883 /* [namespace.udecl]
14884
14885 A using declaration shall not name a template-id. */
14886 error_at (token->location,
14887 "a template-id may not appear in a using-declaration");
14888 else
14889 {
14890 if (at_class_scope_p ())
14891 {
14892 /* Create the USING_DECL. */
14893 decl = do_class_using_decl (parser->scope, identifier);
14894
14895 if (check_for_bare_parameter_packs (decl))
14896 return false;
14897 else
14898 /* Add it to the list of members in this class. */
14899 finish_member_declaration (decl);
14900 }
14901 else
14902 {
14903 decl = cp_parser_lookup_name_simple (parser,
14904 identifier,
14905 token->location);
14906 if (decl == error_mark_node)
14907 cp_parser_name_lookup_error (parser, identifier,
14908 decl, NLE_NULL,
14909 token->location);
14910 else if (check_for_bare_parameter_packs (decl))
14911 return false;
14912 else if (!at_namespace_scope_p ())
14913 do_local_using_decl (decl, qscope, identifier);
14914 else
14915 do_toplevel_using_decl (decl, qscope, identifier);
14916 }
14917 }
14918
14919 /* Look for the final `;'. */
14920 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14921
14922 return true;
14923 }
14924
14925 /* Parse an alias-declaration.
14926
14927 alias-declaration:
14928 using identifier attribute-specifier-seq [opt] = type-id */
14929
14930 static tree
14931 cp_parser_alias_declaration (cp_parser* parser)
14932 {
14933 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
14934 location_t id_location;
14935 cp_declarator *declarator;
14936 cp_decl_specifier_seq decl_specs;
14937 bool member_p;
14938
14939 /* Look for the `using' keyword. */
14940 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14941 id_location = cp_lexer_peek_token (parser->lexer)->location;
14942 id = cp_parser_identifier (parser);
14943 attributes = cp_parser_attributes_opt (parser);
14944 cp_parser_require (parser, CPP_EQ, RT_EQ);
14945
14946 type = cp_parser_type_id (parser);
14947 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14948
14949 if (cp_parser_error_occurred (parser))
14950 return error_mark_node;
14951
14952 /* A typedef-name can also be introduced by an alias-declaration. The
14953 identifier following the using keyword becomes a typedef-name. It has
14954 the same semantics as if it were introduced by the typedef
14955 specifier. In particular, it does not define a new type and it shall
14956 not appear in the type-id. */
14957
14958 clear_decl_specs (&decl_specs);
14959 decl_specs.type = type;
14960 decl_specs.attributes = attributes;
14961 ++decl_specs.specs[(int) ds_typedef];
14962 ++decl_specs.specs[(int) ds_alias];
14963
14964 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
14965 declarator->id_loc = id_location;
14966
14967 member_p = at_class_scope_p ();
14968 if (member_p)
14969 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
14970 NULL_TREE, attributes);
14971 else
14972 decl = start_decl (declarator, &decl_specs, 0,
14973 attributes, NULL_TREE, &pushed_scope);
14974 if (decl == error_mark_node)
14975 return decl;
14976
14977 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
14978
14979 if (pushed_scope)
14980 pop_scope (pushed_scope);
14981
14982 /* If decl is a template, return its TEMPLATE_DECL so that it gets
14983 added into the symbol table; otherwise, return the TYPE_DECL. */
14984 if (DECL_LANG_SPECIFIC (decl)
14985 && DECL_TEMPLATE_INFO (decl)
14986 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
14987 {
14988 decl = DECL_TI_TEMPLATE (decl);
14989 if (member_p)
14990 check_member_template (decl);
14991 }
14992
14993 return decl;
14994 }
14995
14996 /* Parse a using-directive.
14997
14998 using-directive:
14999 using namespace :: [opt] nested-name-specifier [opt]
15000 namespace-name ; */
15001
15002 static void
15003 cp_parser_using_directive (cp_parser* parser)
15004 {
15005 tree namespace_decl;
15006 tree attribs;
15007
15008 /* Look for the `using' keyword. */
15009 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15010 /* And the `namespace' keyword. */
15011 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15012 /* Look for the optional `::' operator. */
15013 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15014 /* And the optional nested-name-specifier. */
15015 cp_parser_nested_name_specifier_opt (parser,
15016 /*typename_keyword_p=*/false,
15017 /*check_dependency_p=*/true,
15018 /*type_p=*/false,
15019 /*is_declaration=*/true);
15020 /* Get the namespace being used. */
15021 namespace_decl = cp_parser_namespace_name (parser);
15022 /* And any specified attributes. */
15023 attribs = cp_parser_attributes_opt (parser);
15024 /* Update the symbol table. */
15025 parse_using_directive (namespace_decl, attribs);
15026 /* Look for the final `;'. */
15027 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15028 }
15029
15030 /* Parse an asm-definition.
15031
15032 asm-definition:
15033 asm ( string-literal ) ;
15034
15035 GNU Extension:
15036
15037 asm-definition:
15038 asm volatile [opt] ( string-literal ) ;
15039 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15040 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15041 : asm-operand-list [opt] ) ;
15042 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15043 : asm-operand-list [opt]
15044 : asm-clobber-list [opt] ) ;
15045 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15046 : asm-clobber-list [opt]
15047 : asm-goto-list ) ; */
15048
15049 static void
15050 cp_parser_asm_definition (cp_parser* parser)
15051 {
15052 tree string;
15053 tree outputs = NULL_TREE;
15054 tree inputs = NULL_TREE;
15055 tree clobbers = NULL_TREE;
15056 tree labels = NULL_TREE;
15057 tree asm_stmt;
15058 bool volatile_p = false;
15059 bool extended_p = false;
15060 bool invalid_inputs_p = false;
15061 bool invalid_outputs_p = false;
15062 bool goto_p = false;
15063 required_token missing = RT_NONE;
15064
15065 /* Look for the `asm' keyword. */
15066 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15067 /* See if the next token is `volatile'. */
15068 if (cp_parser_allow_gnu_extensions_p (parser)
15069 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15070 {
15071 /* Remember that we saw the `volatile' keyword. */
15072 volatile_p = true;
15073 /* Consume the token. */
15074 cp_lexer_consume_token (parser->lexer);
15075 }
15076 if (cp_parser_allow_gnu_extensions_p (parser)
15077 && parser->in_function_body
15078 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15079 {
15080 /* Remember that we saw the `goto' keyword. */
15081 goto_p = true;
15082 /* Consume the token. */
15083 cp_lexer_consume_token (parser->lexer);
15084 }
15085 /* Look for the opening `('. */
15086 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15087 return;
15088 /* Look for the string. */
15089 string = cp_parser_string_literal (parser, false, false);
15090 if (string == error_mark_node)
15091 {
15092 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15093 /*consume_paren=*/true);
15094 return;
15095 }
15096
15097 /* If we're allowing GNU extensions, check for the extended assembly
15098 syntax. Unfortunately, the `:' tokens need not be separated by
15099 a space in C, and so, for compatibility, we tolerate that here
15100 too. Doing that means that we have to treat the `::' operator as
15101 two `:' tokens. */
15102 if (cp_parser_allow_gnu_extensions_p (parser)
15103 && parser->in_function_body
15104 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15105 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15106 {
15107 bool inputs_p = false;
15108 bool clobbers_p = false;
15109 bool labels_p = false;
15110
15111 /* The extended syntax was used. */
15112 extended_p = true;
15113
15114 /* Look for outputs. */
15115 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15116 {
15117 /* Consume the `:'. */
15118 cp_lexer_consume_token (parser->lexer);
15119 /* Parse the output-operands. */
15120 if (cp_lexer_next_token_is_not (parser->lexer,
15121 CPP_COLON)
15122 && cp_lexer_next_token_is_not (parser->lexer,
15123 CPP_SCOPE)
15124 && cp_lexer_next_token_is_not (parser->lexer,
15125 CPP_CLOSE_PAREN)
15126 && !goto_p)
15127 outputs = cp_parser_asm_operand_list (parser);
15128
15129 if (outputs == error_mark_node)
15130 invalid_outputs_p = true;
15131 }
15132 /* If the next token is `::', there are no outputs, and the
15133 next token is the beginning of the inputs. */
15134 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15135 /* The inputs are coming next. */
15136 inputs_p = true;
15137
15138 /* Look for inputs. */
15139 if (inputs_p
15140 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15141 {
15142 /* Consume the `:' or `::'. */
15143 cp_lexer_consume_token (parser->lexer);
15144 /* Parse the output-operands. */
15145 if (cp_lexer_next_token_is_not (parser->lexer,
15146 CPP_COLON)
15147 && cp_lexer_next_token_is_not (parser->lexer,
15148 CPP_SCOPE)
15149 && cp_lexer_next_token_is_not (parser->lexer,
15150 CPP_CLOSE_PAREN))
15151 inputs = cp_parser_asm_operand_list (parser);
15152
15153 if (inputs == error_mark_node)
15154 invalid_inputs_p = true;
15155 }
15156 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15157 /* The clobbers are coming next. */
15158 clobbers_p = true;
15159
15160 /* Look for clobbers. */
15161 if (clobbers_p
15162 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15163 {
15164 clobbers_p = true;
15165 /* Consume the `:' or `::'. */
15166 cp_lexer_consume_token (parser->lexer);
15167 /* Parse the clobbers. */
15168 if (cp_lexer_next_token_is_not (parser->lexer,
15169 CPP_COLON)
15170 && cp_lexer_next_token_is_not (parser->lexer,
15171 CPP_CLOSE_PAREN))
15172 clobbers = cp_parser_asm_clobber_list (parser);
15173 }
15174 else if (goto_p
15175 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15176 /* The labels are coming next. */
15177 labels_p = true;
15178
15179 /* Look for labels. */
15180 if (labels_p
15181 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15182 {
15183 labels_p = true;
15184 /* Consume the `:' or `::'. */
15185 cp_lexer_consume_token (parser->lexer);
15186 /* Parse the labels. */
15187 labels = cp_parser_asm_label_list (parser);
15188 }
15189
15190 if (goto_p && !labels_p)
15191 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15192 }
15193 else if (goto_p)
15194 missing = RT_COLON_SCOPE;
15195
15196 /* Look for the closing `)'. */
15197 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15198 missing ? missing : RT_CLOSE_PAREN))
15199 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15200 /*consume_paren=*/true);
15201 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15202
15203 if (!invalid_inputs_p && !invalid_outputs_p)
15204 {
15205 /* Create the ASM_EXPR. */
15206 if (parser->in_function_body)
15207 {
15208 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15209 inputs, clobbers, labels);
15210 /* If the extended syntax was not used, mark the ASM_EXPR. */
15211 if (!extended_p)
15212 {
15213 tree temp = asm_stmt;
15214 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15215 temp = TREE_OPERAND (temp, 0);
15216
15217 ASM_INPUT_P (temp) = 1;
15218 }
15219 }
15220 else
15221 cgraph_add_asm_node (string);
15222 }
15223 }
15224
15225 /* Declarators [gram.dcl.decl] */
15226
15227 /* Parse an init-declarator.
15228
15229 init-declarator:
15230 declarator initializer [opt]
15231
15232 GNU Extension:
15233
15234 init-declarator:
15235 declarator asm-specification [opt] attributes [opt] initializer [opt]
15236
15237 function-definition:
15238 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15239 function-body
15240 decl-specifier-seq [opt] declarator function-try-block
15241
15242 GNU Extension:
15243
15244 function-definition:
15245 __extension__ function-definition
15246
15247 TM Extension:
15248
15249 function-definition:
15250 decl-specifier-seq [opt] declarator function-transaction-block
15251
15252 The DECL_SPECIFIERS apply to this declarator. Returns a
15253 representation of the entity declared. If MEMBER_P is TRUE, then
15254 this declarator appears in a class scope. The new DECL created by
15255 this declarator is returned.
15256
15257 The CHECKS are access checks that should be performed once we know
15258 what entity is being declared (and, therefore, what classes have
15259 befriended it).
15260
15261 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15262 for a function-definition here as well. If the declarator is a
15263 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15264 be TRUE upon return. By that point, the function-definition will
15265 have been completely parsed.
15266
15267 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15268 is FALSE.
15269
15270 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15271 parsed declaration if it is an uninitialized single declarator not followed
15272 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15273 if present, will not be consumed. If returned, this declarator will be
15274 created with SD_INITIALIZED but will not call cp_finish_decl. */
15275
15276 static tree
15277 cp_parser_init_declarator (cp_parser* parser,
15278 cp_decl_specifier_seq *decl_specifiers,
15279 VEC (deferred_access_check,gc)* checks,
15280 bool function_definition_allowed_p,
15281 bool member_p,
15282 int declares_class_or_enum,
15283 bool* function_definition_p,
15284 tree* maybe_range_for_decl)
15285 {
15286 cp_token *token = NULL, *asm_spec_start_token = NULL,
15287 *attributes_start_token = NULL;
15288 cp_declarator *declarator;
15289 tree prefix_attributes;
15290 tree attributes;
15291 tree asm_specification;
15292 tree initializer;
15293 tree decl = NULL_TREE;
15294 tree scope;
15295 int is_initialized;
15296 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15297 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15298 "(...)". */
15299 enum cpp_ttype initialization_kind;
15300 bool is_direct_init = false;
15301 bool is_non_constant_init;
15302 int ctor_dtor_or_conv_p;
15303 bool friend_p;
15304 tree pushed_scope = NULL_TREE;
15305 bool range_for_decl_p = false;
15306
15307 /* Gather the attributes that were provided with the
15308 decl-specifiers. */
15309 prefix_attributes = decl_specifiers->attributes;
15310
15311 /* Assume that this is not the declarator for a function
15312 definition. */
15313 if (function_definition_p)
15314 *function_definition_p = false;
15315
15316 /* Defer access checks while parsing the declarator; we cannot know
15317 what names are accessible until we know what is being
15318 declared. */
15319 resume_deferring_access_checks ();
15320
15321 /* Parse the declarator. */
15322 token = cp_lexer_peek_token (parser->lexer);
15323 declarator
15324 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15325 &ctor_dtor_or_conv_p,
15326 /*parenthesized_p=*/NULL,
15327 member_p);
15328 /* Gather up the deferred checks. */
15329 stop_deferring_access_checks ();
15330
15331 /* If the DECLARATOR was erroneous, there's no need to go
15332 further. */
15333 if (declarator == cp_error_declarator)
15334 return error_mark_node;
15335
15336 /* Check that the number of template-parameter-lists is OK. */
15337 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15338 token->location))
15339 return error_mark_node;
15340
15341 if (declares_class_or_enum & 2)
15342 cp_parser_check_for_definition_in_return_type (declarator,
15343 decl_specifiers->type,
15344 decl_specifiers->type_location);
15345
15346 /* Figure out what scope the entity declared by the DECLARATOR is
15347 located in. `grokdeclarator' sometimes changes the scope, so
15348 we compute it now. */
15349 scope = get_scope_of_declarator (declarator);
15350
15351 /* Perform any lookups in the declared type which were thought to be
15352 dependent, but are not in the scope of the declarator. */
15353 decl_specifiers->type
15354 = maybe_update_decl_type (decl_specifiers->type, scope);
15355
15356 /* If we're allowing GNU extensions, look for an asm-specification
15357 and attributes. */
15358 if (cp_parser_allow_gnu_extensions_p (parser))
15359 {
15360 /* Look for an asm-specification. */
15361 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15362 asm_specification = cp_parser_asm_specification_opt (parser);
15363 /* And attributes. */
15364 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15365 attributes = cp_parser_attributes_opt (parser);
15366 }
15367 else
15368 {
15369 asm_specification = NULL_TREE;
15370 attributes = NULL_TREE;
15371 }
15372
15373 /* Peek at the next token. */
15374 token = cp_lexer_peek_token (parser->lexer);
15375 /* Check to see if the token indicates the start of a
15376 function-definition. */
15377 if (function_declarator_p (declarator)
15378 && cp_parser_token_starts_function_definition_p (token))
15379 {
15380 if (!function_definition_allowed_p)
15381 {
15382 /* If a function-definition should not appear here, issue an
15383 error message. */
15384 cp_parser_error (parser,
15385 "a function-definition is not allowed here");
15386 return error_mark_node;
15387 }
15388 else
15389 {
15390 location_t func_brace_location
15391 = cp_lexer_peek_token (parser->lexer)->location;
15392
15393 /* Neither attributes nor an asm-specification are allowed
15394 on a function-definition. */
15395 if (asm_specification)
15396 error_at (asm_spec_start_token->location,
15397 "an asm-specification is not allowed "
15398 "on a function-definition");
15399 if (attributes)
15400 error_at (attributes_start_token->location,
15401 "attributes are not allowed on a function-definition");
15402 /* This is a function-definition. */
15403 *function_definition_p = true;
15404
15405 /* Parse the function definition. */
15406 if (member_p)
15407 decl = cp_parser_save_member_function_body (parser,
15408 decl_specifiers,
15409 declarator,
15410 prefix_attributes);
15411 else
15412 decl
15413 = (cp_parser_function_definition_from_specifiers_and_declarator
15414 (parser, decl_specifiers, prefix_attributes, declarator));
15415
15416 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15417 {
15418 /* This is where the prologue starts... */
15419 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15420 = func_brace_location;
15421 }
15422
15423 return decl;
15424 }
15425 }
15426
15427 /* [dcl.dcl]
15428
15429 Only in function declarations for constructors, destructors, and
15430 type conversions can the decl-specifier-seq be omitted.
15431
15432 We explicitly postpone this check past the point where we handle
15433 function-definitions because we tolerate function-definitions
15434 that are missing their return types in some modes. */
15435 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15436 {
15437 cp_parser_error (parser,
15438 "expected constructor, destructor, or type conversion");
15439 return error_mark_node;
15440 }
15441
15442 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15443 if (token->type == CPP_EQ
15444 || token->type == CPP_OPEN_PAREN
15445 || token->type == CPP_OPEN_BRACE)
15446 {
15447 is_initialized = SD_INITIALIZED;
15448 initialization_kind = token->type;
15449 if (maybe_range_for_decl)
15450 *maybe_range_for_decl = error_mark_node;
15451
15452 if (token->type == CPP_EQ
15453 && function_declarator_p (declarator))
15454 {
15455 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15456 if (t2->keyword == RID_DEFAULT)
15457 is_initialized = SD_DEFAULTED;
15458 else if (t2->keyword == RID_DELETE)
15459 is_initialized = SD_DELETED;
15460 }
15461 }
15462 else
15463 {
15464 /* If the init-declarator isn't initialized and isn't followed by a
15465 `,' or `;', it's not a valid init-declarator. */
15466 if (token->type != CPP_COMMA
15467 && token->type != CPP_SEMICOLON)
15468 {
15469 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15470 range_for_decl_p = true;
15471 else
15472 {
15473 cp_parser_error (parser, "expected initializer");
15474 return error_mark_node;
15475 }
15476 }
15477 is_initialized = SD_UNINITIALIZED;
15478 initialization_kind = CPP_EOF;
15479 }
15480
15481 /* Because start_decl has side-effects, we should only call it if we
15482 know we're going ahead. By this point, we know that we cannot
15483 possibly be looking at any other construct. */
15484 cp_parser_commit_to_tentative_parse (parser);
15485
15486 /* If the decl specifiers were bad, issue an error now that we're
15487 sure this was intended to be a declarator. Then continue
15488 declaring the variable(s), as int, to try to cut down on further
15489 errors. */
15490 if (decl_specifiers->any_specifiers_p
15491 && decl_specifiers->type == error_mark_node)
15492 {
15493 cp_parser_error (parser, "invalid type in declaration");
15494 decl_specifiers->type = integer_type_node;
15495 }
15496
15497 /* Check to see whether or not this declaration is a friend. */
15498 friend_p = cp_parser_friend_p (decl_specifiers);
15499
15500 /* Enter the newly declared entry in the symbol table. If we're
15501 processing a declaration in a class-specifier, we wait until
15502 after processing the initializer. */
15503 if (!member_p)
15504 {
15505 if (parser->in_unbraced_linkage_specification_p)
15506 decl_specifiers->storage_class = sc_extern;
15507 decl = start_decl (declarator, decl_specifiers,
15508 range_for_decl_p? SD_INITIALIZED : is_initialized,
15509 attributes, prefix_attributes,
15510 &pushed_scope);
15511 /* Adjust location of decl if declarator->id_loc is more appropriate:
15512 set, and decl wasn't merged with another decl, in which case its
15513 location would be different from input_location, and more accurate. */
15514 if (DECL_P (decl)
15515 && declarator->id_loc != UNKNOWN_LOCATION
15516 && DECL_SOURCE_LOCATION (decl) == input_location)
15517 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15518 }
15519 else if (scope)
15520 /* Enter the SCOPE. That way unqualified names appearing in the
15521 initializer will be looked up in SCOPE. */
15522 pushed_scope = push_scope (scope);
15523
15524 /* Perform deferred access control checks, now that we know in which
15525 SCOPE the declared entity resides. */
15526 if (!member_p && decl)
15527 {
15528 tree saved_current_function_decl = NULL_TREE;
15529
15530 /* If the entity being declared is a function, pretend that we
15531 are in its scope. If it is a `friend', it may have access to
15532 things that would not otherwise be accessible. */
15533 if (TREE_CODE (decl) == FUNCTION_DECL)
15534 {
15535 saved_current_function_decl = current_function_decl;
15536 current_function_decl = decl;
15537 }
15538
15539 /* Perform access checks for template parameters. */
15540 cp_parser_perform_template_parameter_access_checks (checks);
15541
15542 /* Perform the access control checks for the declarator and the
15543 decl-specifiers. */
15544 perform_deferred_access_checks ();
15545
15546 /* Restore the saved value. */
15547 if (TREE_CODE (decl) == FUNCTION_DECL)
15548 current_function_decl = saved_current_function_decl;
15549 }
15550
15551 /* Parse the initializer. */
15552 initializer = NULL_TREE;
15553 is_direct_init = false;
15554 is_non_constant_init = true;
15555 if (is_initialized)
15556 {
15557 if (function_declarator_p (declarator))
15558 {
15559 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15560 if (initialization_kind == CPP_EQ)
15561 initializer = cp_parser_pure_specifier (parser);
15562 else
15563 {
15564 /* If the declaration was erroneous, we don't really
15565 know what the user intended, so just silently
15566 consume the initializer. */
15567 if (decl != error_mark_node)
15568 error_at (initializer_start_token->location,
15569 "initializer provided for function");
15570 cp_parser_skip_to_closing_parenthesis (parser,
15571 /*recovering=*/true,
15572 /*or_comma=*/false,
15573 /*consume_paren=*/true);
15574 }
15575 }
15576 else
15577 {
15578 /* We want to record the extra mangling scope for in-class
15579 initializers of class members and initializers of static data
15580 member templates. The former is a C++0x feature which isn't
15581 implemented yet, and I expect it will involve deferring
15582 parsing of the initializer until end of class as with default
15583 arguments. So right here we only handle the latter. */
15584 if (!member_p && processing_template_decl)
15585 start_lambda_scope (decl);
15586 initializer = cp_parser_initializer (parser,
15587 &is_direct_init,
15588 &is_non_constant_init);
15589 if (!member_p && processing_template_decl)
15590 finish_lambda_scope ();
15591 }
15592 }
15593
15594 /* The old parser allows attributes to appear after a parenthesized
15595 initializer. Mark Mitchell proposed removing this functionality
15596 on the GCC mailing lists on 2002-08-13. This parser accepts the
15597 attributes -- but ignores them. */
15598 if (cp_parser_allow_gnu_extensions_p (parser)
15599 && initialization_kind == CPP_OPEN_PAREN)
15600 if (cp_parser_attributes_opt (parser))
15601 warning (OPT_Wattributes,
15602 "attributes after parenthesized initializer ignored");
15603
15604 /* For an in-class declaration, use `grokfield' to create the
15605 declaration. */
15606 if (member_p)
15607 {
15608 if (pushed_scope)
15609 {
15610 pop_scope (pushed_scope);
15611 pushed_scope = NULL_TREE;
15612 }
15613 decl = grokfield (declarator, decl_specifiers,
15614 initializer, !is_non_constant_init,
15615 /*asmspec=*/NULL_TREE,
15616 prefix_attributes);
15617 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15618 cp_parser_save_default_args (parser, decl);
15619 }
15620
15621 /* Finish processing the declaration. But, skip member
15622 declarations. */
15623 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15624 {
15625 cp_finish_decl (decl,
15626 initializer, !is_non_constant_init,
15627 asm_specification,
15628 /* If the initializer is in parentheses, then this is
15629 a direct-initialization, which means that an
15630 `explicit' constructor is OK. Otherwise, an
15631 `explicit' constructor cannot be used. */
15632 ((is_direct_init || !is_initialized)
15633 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15634 }
15635 else if ((cxx_dialect != cxx98) && friend_p
15636 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15637 /* Core issue #226 (C++0x only): A default template-argument
15638 shall not be specified in a friend class template
15639 declaration. */
15640 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15641 /*is_partial=*/0, /*is_friend_decl=*/1);
15642
15643 if (!friend_p && pushed_scope)
15644 pop_scope (pushed_scope);
15645
15646 return decl;
15647 }
15648
15649 /* Parse a declarator.
15650
15651 declarator:
15652 direct-declarator
15653 ptr-operator declarator
15654
15655 abstract-declarator:
15656 ptr-operator abstract-declarator [opt]
15657 direct-abstract-declarator
15658
15659 GNU Extensions:
15660
15661 declarator:
15662 attributes [opt] direct-declarator
15663 attributes [opt] ptr-operator declarator
15664
15665 abstract-declarator:
15666 attributes [opt] ptr-operator abstract-declarator [opt]
15667 attributes [opt] direct-abstract-declarator
15668
15669 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15670 detect constructor, destructor or conversion operators. It is set
15671 to -1 if the declarator is a name, and +1 if it is a
15672 function. Otherwise it is set to zero. Usually you just want to
15673 test for >0, but internally the negative value is used.
15674
15675 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15676 a decl-specifier-seq unless it declares a constructor, destructor,
15677 or conversion. It might seem that we could check this condition in
15678 semantic analysis, rather than parsing, but that makes it difficult
15679 to handle something like `f()'. We want to notice that there are
15680 no decl-specifiers, and therefore realize that this is an
15681 expression, not a declaration.)
15682
15683 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15684 the declarator is a direct-declarator of the form "(...)".
15685
15686 MEMBER_P is true iff this declarator is a member-declarator. */
15687
15688 static cp_declarator *
15689 cp_parser_declarator (cp_parser* parser,
15690 cp_parser_declarator_kind dcl_kind,
15691 int* ctor_dtor_or_conv_p,
15692 bool* parenthesized_p,
15693 bool member_p)
15694 {
15695 cp_declarator *declarator;
15696 enum tree_code code;
15697 cp_cv_quals cv_quals;
15698 tree class_type;
15699 tree attributes = NULL_TREE;
15700
15701 /* Assume this is not a constructor, destructor, or type-conversion
15702 operator. */
15703 if (ctor_dtor_or_conv_p)
15704 *ctor_dtor_or_conv_p = 0;
15705
15706 if (cp_parser_allow_gnu_extensions_p (parser))
15707 attributes = cp_parser_attributes_opt (parser);
15708
15709 /* Check for the ptr-operator production. */
15710 cp_parser_parse_tentatively (parser);
15711 /* Parse the ptr-operator. */
15712 code = cp_parser_ptr_operator (parser,
15713 &class_type,
15714 &cv_quals);
15715 /* If that worked, then we have a ptr-operator. */
15716 if (cp_parser_parse_definitely (parser))
15717 {
15718 /* If a ptr-operator was found, then this declarator was not
15719 parenthesized. */
15720 if (parenthesized_p)
15721 *parenthesized_p = true;
15722 /* The dependent declarator is optional if we are parsing an
15723 abstract-declarator. */
15724 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15725 cp_parser_parse_tentatively (parser);
15726
15727 /* Parse the dependent declarator. */
15728 declarator = cp_parser_declarator (parser, dcl_kind,
15729 /*ctor_dtor_or_conv_p=*/NULL,
15730 /*parenthesized_p=*/NULL,
15731 /*member_p=*/false);
15732
15733 /* If we are parsing an abstract-declarator, we must handle the
15734 case where the dependent declarator is absent. */
15735 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15736 && !cp_parser_parse_definitely (parser))
15737 declarator = NULL;
15738
15739 declarator = cp_parser_make_indirect_declarator
15740 (code, class_type, cv_quals, declarator);
15741 }
15742 /* Everything else is a direct-declarator. */
15743 else
15744 {
15745 if (parenthesized_p)
15746 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15747 CPP_OPEN_PAREN);
15748 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15749 ctor_dtor_or_conv_p,
15750 member_p);
15751 }
15752
15753 if (attributes && declarator && declarator != cp_error_declarator)
15754 declarator->attributes = attributes;
15755
15756 return declarator;
15757 }
15758
15759 /* Parse a direct-declarator or direct-abstract-declarator.
15760
15761 direct-declarator:
15762 declarator-id
15763 direct-declarator ( parameter-declaration-clause )
15764 cv-qualifier-seq [opt]
15765 exception-specification [opt]
15766 direct-declarator [ constant-expression [opt] ]
15767 ( declarator )
15768
15769 direct-abstract-declarator:
15770 direct-abstract-declarator [opt]
15771 ( parameter-declaration-clause )
15772 cv-qualifier-seq [opt]
15773 exception-specification [opt]
15774 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15775 ( abstract-declarator )
15776
15777 Returns a representation of the declarator. DCL_KIND is
15778 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15779 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15780 we are parsing a direct-declarator. It is
15781 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15782 of ambiguity we prefer an abstract declarator, as per
15783 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15784 cp_parser_declarator. */
15785
15786 static cp_declarator *
15787 cp_parser_direct_declarator (cp_parser* parser,
15788 cp_parser_declarator_kind dcl_kind,
15789 int* ctor_dtor_or_conv_p,
15790 bool member_p)
15791 {
15792 cp_token *token;
15793 cp_declarator *declarator = NULL;
15794 tree scope = NULL_TREE;
15795 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15796 bool saved_in_declarator_p = parser->in_declarator_p;
15797 bool first = true;
15798 tree pushed_scope = NULL_TREE;
15799
15800 while (true)
15801 {
15802 /* Peek at the next token. */
15803 token = cp_lexer_peek_token (parser->lexer);
15804 if (token->type == CPP_OPEN_PAREN)
15805 {
15806 /* This is either a parameter-declaration-clause, or a
15807 parenthesized declarator. When we know we are parsing a
15808 named declarator, it must be a parenthesized declarator
15809 if FIRST is true. For instance, `(int)' is a
15810 parameter-declaration-clause, with an omitted
15811 direct-abstract-declarator. But `((*))', is a
15812 parenthesized abstract declarator. Finally, when T is a
15813 template parameter `(T)' is a
15814 parameter-declaration-clause, and not a parenthesized
15815 named declarator.
15816
15817 We first try and parse a parameter-declaration-clause,
15818 and then try a nested declarator (if FIRST is true).
15819
15820 It is not an error for it not to be a
15821 parameter-declaration-clause, even when FIRST is
15822 false. Consider,
15823
15824 int i (int);
15825 int i (3);
15826
15827 The first is the declaration of a function while the
15828 second is the definition of a variable, including its
15829 initializer.
15830
15831 Having seen only the parenthesis, we cannot know which of
15832 these two alternatives should be selected. Even more
15833 complex are examples like:
15834
15835 int i (int (a));
15836 int i (int (3));
15837
15838 The former is a function-declaration; the latter is a
15839 variable initialization.
15840
15841 Thus again, we try a parameter-declaration-clause, and if
15842 that fails, we back out and return. */
15843
15844 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15845 {
15846 tree params;
15847 unsigned saved_num_template_parameter_lists;
15848 bool is_declarator = false;
15849 tree t;
15850
15851 /* In a member-declarator, the only valid interpretation
15852 of a parenthesis is the start of a
15853 parameter-declaration-clause. (It is invalid to
15854 initialize a static data member with a parenthesized
15855 initializer; only the "=" form of initialization is
15856 permitted.) */
15857 if (!member_p)
15858 cp_parser_parse_tentatively (parser);
15859
15860 /* Consume the `('. */
15861 cp_lexer_consume_token (parser->lexer);
15862 if (first)
15863 {
15864 /* If this is going to be an abstract declarator, we're
15865 in a declarator and we can't have default args. */
15866 parser->default_arg_ok_p = false;
15867 parser->in_declarator_p = true;
15868 }
15869
15870 /* Inside the function parameter list, surrounding
15871 template-parameter-lists do not apply. */
15872 saved_num_template_parameter_lists
15873 = parser->num_template_parameter_lists;
15874 parser->num_template_parameter_lists = 0;
15875
15876 begin_scope (sk_function_parms, NULL_TREE);
15877
15878 /* Parse the parameter-declaration-clause. */
15879 params = cp_parser_parameter_declaration_clause (parser);
15880
15881 parser->num_template_parameter_lists
15882 = saved_num_template_parameter_lists;
15883
15884 /* Consume the `)'. */
15885 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15886
15887 /* If all went well, parse the cv-qualifier-seq and the
15888 exception-specification. */
15889 if (member_p || cp_parser_parse_definitely (parser))
15890 {
15891 cp_cv_quals cv_quals;
15892 cp_virt_specifiers virt_specifiers;
15893 tree exception_specification;
15894 tree late_return;
15895
15896 is_declarator = true;
15897
15898 if (ctor_dtor_or_conv_p)
15899 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15900 first = false;
15901
15902 /* Parse the cv-qualifier-seq. */
15903 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15904 /* And the exception-specification. */
15905 exception_specification
15906 = cp_parser_exception_specification_opt (parser);
15907 /* Parse the virt-specifier-seq. */
15908 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15909
15910 late_return = (cp_parser_late_return_type_opt
15911 (parser, member_p ? cv_quals : -1));
15912
15913 /* Create the function-declarator. */
15914 declarator = make_call_declarator (declarator,
15915 params,
15916 cv_quals,
15917 virt_specifiers,
15918 exception_specification,
15919 late_return);
15920 /* Any subsequent parameter lists are to do with
15921 return type, so are not those of the declared
15922 function. */
15923 parser->default_arg_ok_p = false;
15924 }
15925
15926 /* Remove the function parms from scope. */
15927 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15928 pop_binding (DECL_NAME (t), t);
15929 leave_scope();
15930
15931 if (is_declarator)
15932 /* Repeat the main loop. */
15933 continue;
15934 }
15935
15936 /* If this is the first, we can try a parenthesized
15937 declarator. */
15938 if (first)
15939 {
15940 bool saved_in_type_id_in_expr_p;
15941
15942 parser->default_arg_ok_p = saved_default_arg_ok_p;
15943 parser->in_declarator_p = saved_in_declarator_p;
15944
15945 /* Consume the `('. */
15946 cp_lexer_consume_token (parser->lexer);
15947 /* Parse the nested declarator. */
15948 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15949 parser->in_type_id_in_expr_p = true;
15950 declarator
15951 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15952 /*parenthesized_p=*/NULL,
15953 member_p);
15954 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15955 first = false;
15956 /* Expect a `)'. */
15957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15958 declarator = cp_error_declarator;
15959 if (declarator == cp_error_declarator)
15960 break;
15961
15962 goto handle_declarator;
15963 }
15964 /* Otherwise, we must be done. */
15965 else
15966 break;
15967 }
15968 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15969 && token->type == CPP_OPEN_SQUARE)
15970 {
15971 /* Parse an array-declarator. */
15972 tree bounds;
15973
15974 if (ctor_dtor_or_conv_p)
15975 *ctor_dtor_or_conv_p = 0;
15976
15977 first = false;
15978 parser->default_arg_ok_p = false;
15979 parser->in_declarator_p = true;
15980 /* Consume the `['. */
15981 cp_lexer_consume_token (parser->lexer);
15982 /* Peek at the next token. */
15983 token = cp_lexer_peek_token (parser->lexer);
15984 /* If the next token is `]', then there is no
15985 constant-expression. */
15986 if (token->type != CPP_CLOSE_SQUARE)
15987 {
15988 bool non_constant_p;
15989
15990 bounds
15991 = cp_parser_constant_expression (parser,
15992 /*allow_non_constant=*/true,
15993 &non_constant_p);
15994 if (!non_constant_p)
15995 /* OK */;
15996 /* Normally, the array bound must be an integral constant
15997 expression. However, as an extension, we allow VLAs
15998 in function scopes as long as they aren't part of a
15999 parameter declaration. */
16000 else if (!parser->in_function_body
16001 || current_binding_level->kind == sk_function_parms)
16002 {
16003 cp_parser_error (parser,
16004 "array bound is not an integer constant");
16005 bounds = error_mark_node;
16006 }
16007 else if (processing_template_decl && !error_operand_p (bounds))
16008 {
16009 /* Remember this wasn't a constant-expression. */
16010 bounds = build_nop (TREE_TYPE (bounds), bounds);
16011 TREE_SIDE_EFFECTS (bounds) = 1;
16012 }
16013 }
16014 else
16015 bounds = NULL_TREE;
16016 /* Look for the closing `]'. */
16017 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16018 {
16019 declarator = cp_error_declarator;
16020 break;
16021 }
16022
16023 declarator = make_array_declarator (declarator, bounds);
16024 }
16025 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16026 {
16027 {
16028 tree qualifying_scope;
16029 tree unqualified_name;
16030 special_function_kind sfk;
16031 bool abstract_ok;
16032 bool pack_expansion_p = false;
16033 cp_token *declarator_id_start_token;
16034
16035 /* Parse a declarator-id */
16036 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16037 if (abstract_ok)
16038 {
16039 cp_parser_parse_tentatively (parser);
16040
16041 /* If we see an ellipsis, we should be looking at a
16042 parameter pack. */
16043 if (token->type == CPP_ELLIPSIS)
16044 {
16045 /* Consume the `...' */
16046 cp_lexer_consume_token (parser->lexer);
16047
16048 pack_expansion_p = true;
16049 }
16050 }
16051
16052 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16053 unqualified_name
16054 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16055 qualifying_scope = parser->scope;
16056 if (abstract_ok)
16057 {
16058 bool okay = false;
16059
16060 if (!unqualified_name && pack_expansion_p)
16061 {
16062 /* Check whether an error occurred. */
16063 okay = !cp_parser_error_occurred (parser);
16064
16065 /* We already consumed the ellipsis to mark a
16066 parameter pack, but we have no way to report it,
16067 so abort the tentative parse. We will be exiting
16068 immediately anyway. */
16069 cp_parser_abort_tentative_parse (parser);
16070 }
16071 else
16072 okay = cp_parser_parse_definitely (parser);
16073
16074 if (!okay)
16075 unqualified_name = error_mark_node;
16076 else if (unqualified_name
16077 && (qualifying_scope
16078 || (TREE_CODE (unqualified_name)
16079 != IDENTIFIER_NODE)))
16080 {
16081 cp_parser_error (parser, "expected unqualified-id");
16082 unqualified_name = error_mark_node;
16083 }
16084 }
16085
16086 if (!unqualified_name)
16087 return NULL;
16088 if (unqualified_name == error_mark_node)
16089 {
16090 declarator = cp_error_declarator;
16091 pack_expansion_p = false;
16092 declarator->parameter_pack_p = false;
16093 break;
16094 }
16095
16096 if (qualifying_scope && at_namespace_scope_p ()
16097 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16098 {
16099 /* In the declaration of a member of a template class
16100 outside of the class itself, the SCOPE will sometimes
16101 be a TYPENAME_TYPE. For example, given:
16102
16103 template <typename T>
16104 int S<T>::R::i = 3;
16105
16106 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16107 this context, we must resolve S<T>::R to an ordinary
16108 type, rather than a typename type.
16109
16110 The reason we normally avoid resolving TYPENAME_TYPEs
16111 is that a specialization of `S' might render
16112 `S<T>::R' not a type. However, if `S' is
16113 specialized, then this `i' will not be used, so there
16114 is no harm in resolving the types here. */
16115 tree type;
16116
16117 /* Resolve the TYPENAME_TYPE. */
16118 type = resolve_typename_type (qualifying_scope,
16119 /*only_current_p=*/false);
16120 /* If that failed, the declarator is invalid. */
16121 if (TREE_CODE (type) == TYPENAME_TYPE)
16122 {
16123 if (typedef_variant_p (type))
16124 error_at (declarator_id_start_token->location,
16125 "cannot define member of dependent typedef "
16126 "%qT", type);
16127 else
16128 error_at (declarator_id_start_token->location,
16129 "%<%T::%E%> is not a type",
16130 TYPE_CONTEXT (qualifying_scope),
16131 TYPE_IDENTIFIER (qualifying_scope));
16132 }
16133 qualifying_scope = type;
16134 }
16135
16136 sfk = sfk_none;
16137
16138 if (unqualified_name)
16139 {
16140 tree class_type;
16141
16142 if (qualifying_scope
16143 && CLASS_TYPE_P (qualifying_scope))
16144 class_type = qualifying_scope;
16145 else
16146 class_type = current_class_type;
16147
16148 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16149 {
16150 tree name_type = TREE_TYPE (unqualified_name);
16151 if (class_type && same_type_p (name_type, class_type))
16152 {
16153 if (qualifying_scope
16154 && CLASSTYPE_USE_TEMPLATE (name_type))
16155 {
16156 error_at (declarator_id_start_token->location,
16157 "invalid use of constructor as a template");
16158 inform (declarator_id_start_token->location,
16159 "use %<%T::%D%> instead of %<%T::%D%> to "
16160 "name the constructor in a qualified name",
16161 class_type,
16162 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16163 class_type, name_type);
16164 declarator = cp_error_declarator;
16165 break;
16166 }
16167 else
16168 unqualified_name = constructor_name (class_type);
16169 }
16170 else
16171 {
16172 /* We do not attempt to print the declarator
16173 here because we do not have enough
16174 information about its original syntactic
16175 form. */
16176 cp_parser_error (parser, "invalid declarator");
16177 declarator = cp_error_declarator;
16178 break;
16179 }
16180 }
16181
16182 if (class_type)
16183 {
16184 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16185 sfk = sfk_destructor;
16186 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16187 sfk = sfk_conversion;
16188 else if (/* There's no way to declare a constructor
16189 for an anonymous type, even if the type
16190 got a name for linkage purposes. */
16191 !TYPE_WAS_ANONYMOUS (class_type)
16192 && constructor_name_p (unqualified_name,
16193 class_type))
16194 {
16195 unqualified_name = constructor_name (class_type);
16196 sfk = sfk_constructor;
16197 }
16198 else if (is_overloaded_fn (unqualified_name)
16199 && DECL_CONSTRUCTOR_P (get_first_fn
16200 (unqualified_name)))
16201 sfk = sfk_constructor;
16202
16203 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16204 *ctor_dtor_or_conv_p = -1;
16205 }
16206 }
16207 declarator = make_id_declarator (qualifying_scope,
16208 unqualified_name,
16209 sfk);
16210 declarator->id_loc = token->location;
16211 declarator->parameter_pack_p = pack_expansion_p;
16212
16213 if (pack_expansion_p)
16214 maybe_warn_variadic_templates ();
16215 }
16216
16217 handle_declarator:;
16218 scope = get_scope_of_declarator (declarator);
16219 if (scope)
16220 /* Any names that appear after the declarator-id for a
16221 member are looked up in the containing scope. */
16222 pushed_scope = push_scope (scope);
16223 parser->in_declarator_p = true;
16224 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16225 || (declarator && declarator->kind == cdk_id))
16226 /* Default args are only allowed on function
16227 declarations. */
16228 parser->default_arg_ok_p = saved_default_arg_ok_p;
16229 else
16230 parser->default_arg_ok_p = false;
16231
16232 first = false;
16233 }
16234 /* We're done. */
16235 else
16236 break;
16237 }
16238
16239 /* For an abstract declarator, we might wind up with nothing at this
16240 point. That's an error; the declarator is not optional. */
16241 if (!declarator)
16242 cp_parser_error (parser, "expected declarator");
16243
16244 /* If we entered a scope, we must exit it now. */
16245 if (pushed_scope)
16246 pop_scope (pushed_scope);
16247
16248 parser->default_arg_ok_p = saved_default_arg_ok_p;
16249 parser->in_declarator_p = saved_in_declarator_p;
16250
16251 return declarator;
16252 }
16253
16254 /* Parse a ptr-operator.
16255
16256 ptr-operator:
16257 * cv-qualifier-seq [opt]
16258 &
16259 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16260
16261 GNU Extension:
16262
16263 ptr-operator:
16264 & cv-qualifier-seq [opt]
16265
16266 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16267 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16268 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16269 filled in with the TYPE containing the member. *CV_QUALS is
16270 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16271 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16272 Note that the tree codes returned by this function have nothing
16273 to do with the types of trees that will be eventually be created
16274 to represent the pointer or reference type being parsed. They are
16275 just constants with suggestive names. */
16276 static enum tree_code
16277 cp_parser_ptr_operator (cp_parser* parser,
16278 tree* type,
16279 cp_cv_quals *cv_quals)
16280 {
16281 enum tree_code code = ERROR_MARK;
16282 cp_token *token;
16283
16284 /* Assume that it's not a pointer-to-member. */
16285 *type = NULL_TREE;
16286 /* And that there are no cv-qualifiers. */
16287 *cv_quals = TYPE_UNQUALIFIED;
16288
16289 /* Peek at the next token. */
16290 token = cp_lexer_peek_token (parser->lexer);
16291
16292 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16293 if (token->type == CPP_MULT)
16294 code = INDIRECT_REF;
16295 else if (token->type == CPP_AND)
16296 code = ADDR_EXPR;
16297 else if ((cxx_dialect != cxx98) &&
16298 token->type == CPP_AND_AND) /* C++0x only */
16299 code = NON_LVALUE_EXPR;
16300
16301 if (code != ERROR_MARK)
16302 {
16303 /* Consume the `*', `&' or `&&'. */
16304 cp_lexer_consume_token (parser->lexer);
16305
16306 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16307 `&', if we are allowing GNU extensions. (The only qualifier
16308 that can legally appear after `&' is `restrict', but that is
16309 enforced during semantic analysis. */
16310 if (code == INDIRECT_REF
16311 || cp_parser_allow_gnu_extensions_p (parser))
16312 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16313 }
16314 else
16315 {
16316 /* Try the pointer-to-member case. */
16317 cp_parser_parse_tentatively (parser);
16318 /* Look for the optional `::' operator. */
16319 cp_parser_global_scope_opt (parser,
16320 /*current_scope_valid_p=*/false);
16321 /* Look for the nested-name specifier. */
16322 token = cp_lexer_peek_token (parser->lexer);
16323 cp_parser_nested_name_specifier (parser,
16324 /*typename_keyword_p=*/false,
16325 /*check_dependency_p=*/true,
16326 /*type_p=*/false,
16327 /*is_declaration=*/false);
16328 /* If we found it, and the next token is a `*', then we are
16329 indeed looking at a pointer-to-member operator. */
16330 if (!cp_parser_error_occurred (parser)
16331 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16332 {
16333 /* Indicate that the `*' operator was used. */
16334 code = INDIRECT_REF;
16335
16336 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16337 error_at (token->location, "%qD is a namespace", parser->scope);
16338 else
16339 {
16340 /* The type of which the member is a member is given by the
16341 current SCOPE. */
16342 *type = parser->scope;
16343 /* The next name will not be qualified. */
16344 parser->scope = NULL_TREE;
16345 parser->qualifying_scope = NULL_TREE;
16346 parser->object_scope = NULL_TREE;
16347 /* Look for the optional cv-qualifier-seq. */
16348 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16349 }
16350 }
16351 /* If that didn't work we don't have a ptr-operator. */
16352 if (!cp_parser_parse_definitely (parser))
16353 cp_parser_error (parser, "expected ptr-operator");
16354 }
16355
16356 return code;
16357 }
16358
16359 /* Parse an (optional) cv-qualifier-seq.
16360
16361 cv-qualifier-seq:
16362 cv-qualifier cv-qualifier-seq [opt]
16363
16364 cv-qualifier:
16365 const
16366 volatile
16367
16368 GNU Extension:
16369
16370 cv-qualifier:
16371 __restrict__
16372
16373 Returns a bitmask representing the cv-qualifiers. */
16374
16375 static cp_cv_quals
16376 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16377 {
16378 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16379
16380 while (true)
16381 {
16382 cp_token *token;
16383 cp_cv_quals cv_qualifier;
16384
16385 /* Peek at the next token. */
16386 token = cp_lexer_peek_token (parser->lexer);
16387 /* See if it's a cv-qualifier. */
16388 switch (token->keyword)
16389 {
16390 case RID_CONST:
16391 cv_qualifier = TYPE_QUAL_CONST;
16392 break;
16393
16394 case RID_VOLATILE:
16395 cv_qualifier = TYPE_QUAL_VOLATILE;
16396 break;
16397
16398 case RID_RESTRICT:
16399 cv_qualifier = TYPE_QUAL_RESTRICT;
16400 break;
16401
16402 default:
16403 cv_qualifier = TYPE_UNQUALIFIED;
16404 break;
16405 }
16406
16407 if (!cv_qualifier)
16408 break;
16409
16410 if (cv_quals & cv_qualifier)
16411 {
16412 error_at (token->location, "duplicate cv-qualifier");
16413 cp_lexer_purge_token (parser->lexer);
16414 }
16415 else
16416 {
16417 cp_lexer_consume_token (parser->lexer);
16418 cv_quals |= cv_qualifier;
16419 }
16420 }
16421
16422 return cv_quals;
16423 }
16424
16425 /* Parse an (optional) virt-specifier-seq.
16426
16427 virt-specifier-seq:
16428 virt-specifier virt-specifier-seq [opt]
16429
16430 virt-specifier:
16431 override
16432 final
16433
16434 Returns a bitmask representing the virt-specifiers. */
16435
16436 static cp_virt_specifiers
16437 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16438 {
16439 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16440
16441 while (true)
16442 {
16443 cp_token *token;
16444 cp_virt_specifiers virt_specifier;
16445
16446 /* Peek at the next token. */
16447 token = cp_lexer_peek_token (parser->lexer);
16448 /* See if it's a virt-specifier-qualifier. */
16449 if (token->type != CPP_NAME)
16450 break;
16451 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16452 {
16453 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16454 virt_specifier = VIRT_SPEC_OVERRIDE;
16455 }
16456 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16457 {
16458 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16459 virt_specifier = VIRT_SPEC_FINAL;
16460 }
16461 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16462 {
16463 virt_specifier = VIRT_SPEC_FINAL;
16464 }
16465 else
16466 break;
16467
16468 if (virt_specifiers & virt_specifier)
16469 {
16470 error_at (token->location, "duplicate virt-specifier");
16471 cp_lexer_purge_token (parser->lexer);
16472 }
16473 else
16474 {
16475 cp_lexer_consume_token (parser->lexer);
16476 virt_specifiers |= virt_specifier;
16477 }
16478 }
16479 return virt_specifiers;
16480 }
16481
16482 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16483 is in scope even though it isn't real. */
16484
16485 static void
16486 inject_this_parameter (tree ctype, cp_cv_quals quals)
16487 {
16488 tree this_parm;
16489
16490 if (current_class_ptr)
16491 {
16492 /* We don't clear this between NSDMIs. Is it already what we want? */
16493 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16494 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16495 && cp_type_quals (type) == quals)
16496 return;
16497 }
16498
16499 this_parm = build_this_parm (ctype, quals);
16500 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16501 current_class_ptr = NULL_TREE;
16502 current_class_ref
16503 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16504 current_class_ptr = this_parm;
16505 }
16506
16507 /* Parse a late-specified return type, if any. This is not a separate
16508 non-terminal, but part of a function declarator, which looks like
16509
16510 -> trailing-type-specifier-seq abstract-declarator(opt)
16511
16512 Returns the type indicated by the type-id.
16513
16514 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16515 function. */
16516
16517 static tree
16518 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16519 {
16520 cp_token *token;
16521 tree type;
16522
16523 /* Peek at the next token. */
16524 token = cp_lexer_peek_token (parser->lexer);
16525 /* A late-specified return type is indicated by an initial '->'. */
16526 if (token->type != CPP_DEREF)
16527 return NULL_TREE;
16528
16529 /* Consume the ->. */
16530 cp_lexer_consume_token (parser->lexer);
16531
16532 if (quals >= 0)
16533 {
16534 /* DR 1207: 'this' is in scope in the trailing return type. */
16535 gcc_assert (current_class_ptr == NULL_TREE);
16536 inject_this_parameter (current_class_type, quals);
16537 }
16538
16539 type = cp_parser_trailing_type_id (parser);
16540
16541 if (quals >= 0)
16542 current_class_ptr = current_class_ref = NULL_TREE;
16543
16544 return type;
16545 }
16546
16547 /* Parse a declarator-id.
16548
16549 declarator-id:
16550 id-expression
16551 :: [opt] nested-name-specifier [opt] type-name
16552
16553 In the `id-expression' case, the value returned is as for
16554 cp_parser_id_expression if the id-expression was an unqualified-id.
16555 If the id-expression was a qualified-id, then a SCOPE_REF is
16556 returned. The first operand is the scope (either a NAMESPACE_DECL
16557 or TREE_TYPE), but the second is still just a representation of an
16558 unqualified-id. */
16559
16560 static tree
16561 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16562 {
16563 tree id;
16564 /* The expression must be an id-expression. Assume that qualified
16565 names are the names of types so that:
16566
16567 template <class T>
16568 int S<T>::R::i = 3;
16569
16570 will work; we must treat `S<T>::R' as the name of a type.
16571 Similarly, assume that qualified names are templates, where
16572 required, so that:
16573
16574 template <class T>
16575 int S<T>::R<T>::i = 3;
16576
16577 will work, too. */
16578 id = cp_parser_id_expression (parser,
16579 /*template_keyword_p=*/false,
16580 /*check_dependency_p=*/false,
16581 /*template_p=*/NULL,
16582 /*declarator_p=*/true,
16583 optional_p);
16584 if (id && BASELINK_P (id))
16585 id = BASELINK_FUNCTIONS (id);
16586 return id;
16587 }
16588
16589 /* Parse a type-id.
16590
16591 type-id:
16592 type-specifier-seq abstract-declarator [opt]
16593
16594 Returns the TYPE specified. */
16595
16596 static tree
16597 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16598 bool is_trailing_return)
16599 {
16600 cp_decl_specifier_seq type_specifier_seq;
16601 cp_declarator *abstract_declarator;
16602
16603 /* Parse the type-specifier-seq. */
16604 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16605 is_trailing_return,
16606 &type_specifier_seq);
16607 if (type_specifier_seq.type == error_mark_node)
16608 return error_mark_node;
16609
16610 /* There might or might not be an abstract declarator. */
16611 cp_parser_parse_tentatively (parser);
16612 /* Look for the declarator. */
16613 abstract_declarator
16614 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16615 /*parenthesized_p=*/NULL,
16616 /*member_p=*/false);
16617 /* Check to see if there really was a declarator. */
16618 if (!cp_parser_parse_definitely (parser))
16619 abstract_declarator = NULL;
16620
16621 if (type_specifier_seq.type
16622 && type_uses_auto (type_specifier_seq.type))
16623 {
16624 /* A type-id with type 'auto' is only ok if the abstract declarator
16625 is a function declarator with a late-specified return type. */
16626 if (abstract_declarator
16627 && abstract_declarator->kind == cdk_function
16628 && abstract_declarator->u.function.late_return_type)
16629 /* OK */;
16630 else
16631 {
16632 error ("invalid use of %<auto%>");
16633 return error_mark_node;
16634 }
16635 }
16636
16637 return groktypename (&type_specifier_seq, abstract_declarator,
16638 is_template_arg);
16639 }
16640
16641 static tree cp_parser_type_id (cp_parser *parser)
16642 {
16643 return cp_parser_type_id_1 (parser, false, false);
16644 }
16645
16646 static tree cp_parser_template_type_arg (cp_parser *parser)
16647 {
16648 tree r;
16649 const char *saved_message = parser->type_definition_forbidden_message;
16650 parser->type_definition_forbidden_message
16651 = G_("types may not be defined in template arguments");
16652 r = cp_parser_type_id_1 (parser, true, false);
16653 parser->type_definition_forbidden_message = saved_message;
16654 return r;
16655 }
16656
16657 static tree cp_parser_trailing_type_id (cp_parser *parser)
16658 {
16659 return cp_parser_type_id_1 (parser, false, true);
16660 }
16661
16662 /* Parse a type-specifier-seq.
16663
16664 type-specifier-seq:
16665 type-specifier type-specifier-seq [opt]
16666
16667 GNU extension:
16668
16669 type-specifier-seq:
16670 attributes type-specifier-seq [opt]
16671
16672 If IS_DECLARATION is true, we are at the start of a "condition" or
16673 exception-declaration, so we might be followed by a declarator-id.
16674
16675 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16676 i.e. we've just seen "->".
16677
16678 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16679
16680 static void
16681 cp_parser_type_specifier_seq (cp_parser* parser,
16682 bool is_declaration,
16683 bool is_trailing_return,
16684 cp_decl_specifier_seq *type_specifier_seq)
16685 {
16686 bool seen_type_specifier = false;
16687 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16688 cp_token *start_token = NULL;
16689
16690 /* Clear the TYPE_SPECIFIER_SEQ. */
16691 clear_decl_specs (type_specifier_seq);
16692
16693 /* In the context of a trailing return type, enum E { } is an
16694 elaborated-type-specifier followed by a function-body, not an
16695 enum-specifier. */
16696 if (is_trailing_return)
16697 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16698
16699 /* Parse the type-specifiers and attributes. */
16700 while (true)
16701 {
16702 tree type_specifier;
16703 bool is_cv_qualifier;
16704
16705 /* Check for attributes first. */
16706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16707 {
16708 type_specifier_seq->attributes =
16709 chainon (type_specifier_seq->attributes,
16710 cp_parser_attributes_opt (parser));
16711 continue;
16712 }
16713
16714 /* record the token of the beginning of the type specifier seq,
16715 for error reporting purposes*/
16716 if (!start_token)
16717 start_token = cp_lexer_peek_token (parser->lexer);
16718
16719 /* Look for the type-specifier. */
16720 type_specifier = cp_parser_type_specifier (parser,
16721 flags,
16722 type_specifier_seq,
16723 /*is_declaration=*/false,
16724 NULL,
16725 &is_cv_qualifier);
16726 if (!type_specifier)
16727 {
16728 /* If the first type-specifier could not be found, this is not a
16729 type-specifier-seq at all. */
16730 if (!seen_type_specifier)
16731 {
16732 cp_parser_error (parser, "expected type-specifier");
16733 type_specifier_seq->type = error_mark_node;
16734 return;
16735 }
16736 /* If subsequent type-specifiers could not be found, the
16737 type-specifier-seq is complete. */
16738 break;
16739 }
16740
16741 seen_type_specifier = true;
16742 /* The standard says that a condition can be:
16743
16744 type-specifier-seq declarator = assignment-expression
16745
16746 However, given:
16747
16748 struct S {};
16749 if (int S = ...)
16750
16751 we should treat the "S" as a declarator, not as a
16752 type-specifier. The standard doesn't say that explicitly for
16753 type-specifier-seq, but it does say that for
16754 decl-specifier-seq in an ordinary declaration. Perhaps it
16755 would be clearer just to allow a decl-specifier-seq here, and
16756 then add a semantic restriction that if any decl-specifiers
16757 that are not type-specifiers appear, the program is invalid. */
16758 if (is_declaration && !is_cv_qualifier)
16759 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16760 }
16761
16762 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16763 }
16764
16765 /* Parse a parameter-declaration-clause.
16766
16767 parameter-declaration-clause:
16768 parameter-declaration-list [opt] ... [opt]
16769 parameter-declaration-list , ...
16770
16771 Returns a representation for the parameter declarations. A return
16772 value of NULL indicates a parameter-declaration-clause consisting
16773 only of an ellipsis. */
16774
16775 static tree
16776 cp_parser_parameter_declaration_clause (cp_parser* parser)
16777 {
16778 tree parameters;
16779 cp_token *token;
16780 bool ellipsis_p;
16781 bool is_error;
16782
16783 /* Peek at the next token. */
16784 token = cp_lexer_peek_token (parser->lexer);
16785 /* Check for trivial parameter-declaration-clauses. */
16786 if (token->type == CPP_ELLIPSIS)
16787 {
16788 /* Consume the `...' token. */
16789 cp_lexer_consume_token (parser->lexer);
16790 return NULL_TREE;
16791 }
16792 else if (token->type == CPP_CLOSE_PAREN)
16793 /* There are no parameters. */
16794 {
16795 #ifndef NO_IMPLICIT_EXTERN_C
16796 if (in_system_header && current_class_type == NULL
16797 && current_lang_name == lang_name_c)
16798 return NULL_TREE;
16799 else
16800 #endif
16801 return void_list_node;
16802 }
16803 /* Check for `(void)', too, which is a special case. */
16804 else if (token->keyword == RID_VOID
16805 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16806 == CPP_CLOSE_PAREN))
16807 {
16808 /* Consume the `void' token. */
16809 cp_lexer_consume_token (parser->lexer);
16810 /* There are no parameters. */
16811 return void_list_node;
16812 }
16813
16814 /* Parse the parameter-declaration-list. */
16815 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16816 /* If a parse error occurred while parsing the
16817 parameter-declaration-list, then the entire
16818 parameter-declaration-clause is erroneous. */
16819 if (is_error)
16820 return NULL;
16821
16822 /* Peek at the next token. */
16823 token = cp_lexer_peek_token (parser->lexer);
16824 /* If it's a `,', the clause should terminate with an ellipsis. */
16825 if (token->type == CPP_COMMA)
16826 {
16827 /* Consume the `,'. */
16828 cp_lexer_consume_token (parser->lexer);
16829 /* Expect an ellipsis. */
16830 ellipsis_p
16831 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16832 }
16833 /* It might also be `...' if the optional trailing `,' was
16834 omitted. */
16835 else if (token->type == CPP_ELLIPSIS)
16836 {
16837 /* Consume the `...' token. */
16838 cp_lexer_consume_token (parser->lexer);
16839 /* And remember that we saw it. */
16840 ellipsis_p = true;
16841 }
16842 else
16843 ellipsis_p = false;
16844
16845 /* Finish the parameter list. */
16846 if (!ellipsis_p)
16847 parameters = chainon (parameters, void_list_node);
16848
16849 return parameters;
16850 }
16851
16852 /* Parse a parameter-declaration-list.
16853
16854 parameter-declaration-list:
16855 parameter-declaration
16856 parameter-declaration-list , parameter-declaration
16857
16858 Returns a representation of the parameter-declaration-list, as for
16859 cp_parser_parameter_declaration_clause. However, the
16860 `void_list_node' is never appended to the list. Upon return,
16861 *IS_ERROR will be true iff an error occurred. */
16862
16863 static tree
16864 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16865 {
16866 tree parameters = NULL_TREE;
16867 tree *tail = &parameters;
16868 bool saved_in_unbraced_linkage_specification_p;
16869 int index = 0;
16870
16871 /* Assume all will go well. */
16872 *is_error = false;
16873 /* The special considerations that apply to a function within an
16874 unbraced linkage specifications do not apply to the parameters
16875 to the function. */
16876 saved_in_unbraced_linkage_specification_p
16877 = parser->in_unbraced_linkage_specification_p;
16878 parser->in_unbraced_linkage_specification_p = false;
16879
16880 /* Look for more parameters. */
16881 while (true)
16882 {
16883 cp_parameter_declarator *parameter;
16884 tree decl = error_mark_node;
16885 bool parenthesized_p = false;
16886 /* Parse the parameter. */
16887 parameter
16888 = cp_parser_parameter_declaration (parser,
16889 /*template_parm_p=*/false,
16890 &parenthesized_p);
16891
16892 /* We don't know yet if the enclosing context is deprecated, so wait
16893 and warn in grokparms if appropriate. */
16894 deprecated_state = DEPRECATED_SUPPRESS;
16895
16896 if (parameter)
16897 decl = grokdeclarator (parameter->declarator,
16898 &parameter->decl_specifiers,
16899 PARM,
16900 parameter->default_argument != NULL_TREE,
16901 &parameter->decl_specifiers.attributes);
16902
16903 deprecated_state = DEPRECATED_NORMAL;
16904
16905 /* If a parse error occurred parsing the parameter declaration,
16906 then the entire parameter-declaration-list is erroneous. */
16907 if (decl == error_mark_node)
16908 {
16909 *is_error = true;
16910 parameters = error_mark_node;
16911 break;
16912 }
16913
16914 if (parameter->decl_specifiers.attributes)
16915 cplus_decl_attributes (&decl,
16916 parameter->decl_specifiers.attributes,
16917 0);
16918 if (DECL_NAME (decl))
16919 decl = pushdecl (decl);
16920
16921 if (decl != error_mark_node)
16922 {
16923 retrofit_lang_decl (decl);
16924 DECL_PARM_INDEX (decl) = ++index;
16925 DECL_PARM_LEVEL (decl) = function_parm_depth ();
16926 }
16927
16928 /* Add the new parameter to the list. */
16929 *tail = build_tree_list (parameter->default_argument, decl);
16930 tail = &TREE_CHAIN (*tail);
16931
16932 /* Peek at the next token. */
16933 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16934 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16935 /* These are for Objective-C++ */
16936 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16937 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16938 /* The parameter-declaration-list is complete. */
16939 break;
16940 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16941 {
16942 cp_token *token;
16943
16944 /* Peek at the next token. */
16945 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16946 /* If it's an ellipsis, then the list is complete. */
16947 if (token->type == CPP_ELLIPSIS)
16948 break;
16949 /* Otherwise, there must be more parameters. Consume the
16950 `,'. */
16951 cp_lexer_consume_token (parser->lexer);
16952 /* When parsing something like:
16953
16954 int i(float f, double d)
16955
16956 we can tell after seeing the declaration for "f" that we
16957 are not looking at an initialization of a variable "i",
16958 but rather at the declaration of a function "i".
16959
16960 Due to the fact that the parsing of template arguments
16961 (as specified to a template-id) requires backtracking we
16962 cannot use this technique when inside a template argument
16963 list. */
16964 if (!parser->in_template_argument_list_p
16965 && !parser->in_type_id_in_expr_p
16966 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16967 /* However, a parameter-declaration of the form
16968 "foat(f)" (which is a valid declaration of a
16969 parameter "f") can also be interpreted as an
16970 expression (the conversion of "f" to "float"). */
16971 && !parenthesized_p)
16972 cp_parser_commit_to_tentative_parse (parser);
16973 }
16974 else
16975 {
16976 cp_parser_error (parser, "expected %<,%> or %<...%>");
16977 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16978 cp_parser_skip_to_closing_parenthesis (parser,
16979 /*recovering=*/true,
16980 /*or_comma=*/false,
16981 /*consume_paren=*/false);
16982 break;
16983 }
16984 }
16985
16986 parser->in_unbraced_linkage_specification_p
16987 = saved_in_unbraced_linkage_specification_p;
16988
16989 return parameters;
16990 }
16991
16992 /* Parse a parameter declaration.
16993
16994 parameter-declaration:
16995 decl-specifier-seq ... [opt] declarator
16996 decl-specifier-seq declarator = assignment-expression
16997 decl-specifier-seq ... [opt] abstract-declarator [opt]
16998 decl-specifier-seq abstract-declarator [opt] = assignment-expression
16999
17000 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17001 declares a template parameter. (In that case, a non-nested `>'
17002 token encountered during the parsing of the assignment-expression
17003 is not interpreted as a greater-than operator.)
17004
17005 Returns a representation of the parameter, or NULL if an error
17006 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17007 true iff the declarator is of the form "(p)". */
17008
17009 static cp_parameter_declarator *
17010 cp_parser_parameter_declaration (cp_parser *parser,
17011 bool template_parm_p,
17012 bool *parenthesized_p)
17013 {
17014 int declares_class_or_enum;
17015 cp_decl_specifier_seq decl_specifiers;
17016 cp_declarator *declarator;
17017 tree default_argument;
17018 cp_token *token = NULL, *declarator_token_start = NULL;
17019 const char *saved_message;
17020
17021 /* In a template parameter, `>' is not an operator.
17022
17023 [temp.param]
17024
17025 When parsing a default template-argument for a non-type
17026 template-parameter, the first non-nested `>' is taken as the end
17027 of the template parameter-list rather than a greater-than
17028 operator. */
17029
17030 /* Type definitions may not appear in parameter types. */
17031 saved_message = parser->type_definition_forbidden_message;
17032 parser->type_definition_forbidden_message
17033 = G_("types may not be defined in parameter types");
17034
17035 /* Parse the declaration-specifiers. */
17036 cp_parser_decl_specifier_seq (parser,
17037 CP_PARSER_FLAGS_NONE,
17038 &decl_specifiers,
17039 &declares_class_or_enum);
17040
17041 /* Complain about missing 'typename' or other invalid type names. */
17042 if (!decl_specifiers.any_type_specifiers_p)
17043 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17044
17045 /* If an error occurred, there's no reason to attempt to parse the
17046 rest of the declaration. */
17047 if (cp_parser_error_occurred (parser))
17048 {
17049 parser->type_definition_forbidden_message = saved_message;
17050 return NULL;
17051 }
17052
17053 /* Peek at the next token. */
17054 token = cp_lexer_peek_token (parser->lexer);
17055
17056 /* If the next token is a `)', `,', `=', `>', or `...', then there
17057 is no declarator. However, when variadic templates are enabled,
17058 there may be a declarator following `...'. */
17059 if (token->type == CPP_CLOSE_PAREN
17060 || token->type == CPP_COMMA
17061 || token->type == CPP_EQ
17062 || token->type == CPP_GREATER)
17063 {
17064 declarator = NULL;
17065 if (parenthesized_p)
17066 *parenthesized_p = false;
17067 }
17068 /* Otherwise, there should be a declarator. */
17069 else
17070 {
17071 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17072 parser->default_arg_ok_p = false;
17073
17074 /* After seeing a decl-specifier-seq, if the next token is not a
17075 "(", there is no possibility that the code is a valid
17076 expression. Therefore, if parsing tentatively, we commit at
17077 this point. */
17078 if (!parser->in_template_argument_list_p
17079 /* In an expression context, having seen:
17080
17081 (int((char ...
17082
17083 we cannot be sure whether we are looking at a
17084 function-type (taking a "char" as a parameter) or a cast
17085 of some object of type "char" to "int". */
17086 && !parser->in_type_id_in_expr_p
17087 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17088 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17089 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17090 cp_parser_commit_to_tentative_parse (parser);
17091 /* Parse the declarator. */
17092 declarator_token_start = token;
17093 declarator = cp_parser_declarator (parser,
17094 CP_PARSER_DECLARATOR_EITHER,
17095 /*ctor_dtor_or_conv_p=*/NULL,
17096 parenthesized_p,
17097 /*member_p=*/false);
17098 parser->default_arg_ok_p = saved_default_arg_ok_p;
17099 /* After the declarator, allow more attributes. */
17100 decl_specifiers.attributes
17101 = chainon (decl_specifiers.attributes,
17102 cp_parser_attributes_opt (parser));
17103 }
17104
17105 /* If the next token is an ellipsis, and we have not seen a
17106 declarator name, and the type of the declarator contains parameter
17107 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17108 a parameter pack expansion expression. Otherwise, leave the
17109 ellipsis for a C-style variadic function. */
17110 token = cp_lexer_peek_token (parser->lexer);
17111 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17112 {
17113 tree type = decl_specifiers.type;
17114
17115 if (type && DECL_P (type))
17116 type = TREE_TYPE (type);
17117
17118 if (type
17119 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17120 && declarator_can_be_parameter_pack (declarator)
17121 && (!declarator || !declarator->parameter_pack_p)
17122 && uses_parameter_packs (type))
17123 {
17124 /* Consume the `...'. */
17125 cp_lexer_consume_token (parser->lexer);
17126 maybe_warn_variadic_templates ();
17127
17128 /* Build a pack expansion type */
17129 if (declarator)
17130 declarator->parameter_pack_p = true;
17131 else
17132 decl_specifiers.type = make_pack_expansion (type);
17133 }
17134 }
17135
17136 /* The restriction on defining new types applies only to the type
17137 of the parameter, not to the default argument. */
17138 parser->type_definition_forbidden_message = saved_message;
17139
17140 /* If the next token is `=', then process a default argument. */
17141 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17142 {
17143 /* If we are defining a class, then the tokens that make up the
17144 default argument must be saved and processed later. */
17145 if (!template_parm_p && at_class_scope_p ()
17146 && TYPE_BEING_DEFINED (current_class_type)
17147 && !LAMBDA_TYPE_P (current_class_type))
17148 {
17149 unsigned depth = 0;
17150 int maybe_template_id = 0;
17151 cp_token *first_token;
17152 cp_token *token;
17153
17154 /* Add tokens until we have processed the entire default
17155 argument. We add the range [first_token, token). */
17156 first_token = cp_lexer_peek_token (parser->lexer);
17157 while (true)
17158 {
17159 bool done = false;
17160
17161 /* Peek at the next token. */
17162 token = cp_lexer_peek_token (parser->lexer);
17163 /* What we do depends on what token we have. */
17164 switch (token->type)
17165 {
17166 /* In valid code, a default argument must be
17167 immediately followed by a `,' `)', or `...'. */
17168 case CPP_COMMA:
17169 if (depth == 0 && maybe_template_id)
17170 {
17171 /* If we've seen a '<', we might be in a
17172 template-argument-list. Until Core issue 325 is
17173 resolved, we don't know how this situation ought
17174 to be handled, so try to DTRT. We check whether
17175 what comes after the comma is a valid parameter
17176 declaration list. If it is, then the comma ends
17177 the default argument; otherwise the default
17178 argument continues. */
17179 bool error = false;
17180 tree t;
17181
17182 /* Set ITALP so cp_parser_parameter_declaration_list
17183 doesn't decide to commit to this parse. */
17184 bool saved_italp = parser->in_template_argument_list_p;
17185 parser->in_template_argument_list_p = true;
17186
17187 cp_parser_parse_tentatively (parser);
17188 cp_lexer_consume_token (parser->lexer);
17189 begin_scope (sk_function_parms, NULL_TREE);
17190 cp_parser_parameter_declaration_list (parser, &error);
17191 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17192 pop_binding (DECL_NAME (t), t);
17193 leave_scope ();
17194 if (!cp_parser_error_occurred (parser) && !error)
17195 done = true;
17196 cp_parser_abort_tentative_parse (parser);
17197
17198 parser->in_template_argument_list_p = saved_italp;
17199 break;
17200 }
17201 case CPP_CLOSE_PAREN:
17202 case CPP_ELLIPSIS:
17203 /* If we run into a non-nested `;', `}', or `]',
17204 then the code is invalid -- but the default
17205 argument is certainly over. */
17206 case CPP_SEMICOLON:
17207 case CPP_CLOSE_BRACE:
17208 case CPP_CLOSE_SQUARE:
17209 if (depth == 0)
17210 done = true;
17211 /* Update DEPTH, if necessary. */
17212 else if (token->type == CPP_CLOSE_PAREN
17213 || token->type == CPP_CLOSE_BRACE
17214 || token->type == CPP_CLOSE_SQUARE)
17215 --depth;
17216 break;
17217
17218 case CPP_OPEN_PAREN:
17219 case CPP_OPEN_SQUARE:
17220 case CPP_OPEN_BRACE:
17221 ++depth;
17222 break;
17223
17224 case CPP_LESS:
17225 if (depth == 0)
17226 /* This might be the comparison operator, or it might
17227 start a template argument list. */
17228 ++maybe_template_id;
17229 break;
17230
17231 case CPP_RSHIFT:
17232 if (cxx_dialect == cxx98)
17233 break;
17234 /* Fall through for C++0x, which treats the `>>'
17235 operator like two `>' tokens in certain
17236 cases. */
17237
17238 case CPP_GREATER:
17239 if (depth == 0)
17240 {
17241 /* This might be an operator, or it might close a
17242 template argument list. But if a previous '<'
17243 started a template argument list, this will have
17244 closed it, so we can't be in one anymore. */
17245 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17246 if (maybe_template_id < 0)
17247 maybe_template_id = 0;
17248 }
17249 break;
17250
17251 /* If we run out of tokens, issue an error message. */
17252 case CPP_EOF:
17253 case CPP_PRAGMA_EOL:
17254 error_at (token->location, "file ends in default argument");
17255 done = true;
17256 break;
17257
17258 case CPP_NAME:
17259 case CPP_SCOPE:
17260 /* In these cases, we should look for template-ids.
17261 For example, if the default argument is
17262 `X<int, double>()', we need to do name lookup to
17263 figure out whether or not `X' is a template; if
17264 so, the `,' does not end the default argument.
17265
17266 That is not yet done. */
17267 break;
17268
17269 default:
17270 break;
17271 }
17272
17273 /* If we've reached the end, stop. */
17274 if (done)
17275 break;
17276
17277 /* Add the token to the token block. */
17278 token = cp_lexer_consume_token (parser->lexer);
17279 }
17280
17281 /* Create a DEFAULT_ARG to represent the unparsed default
17282 argument. */
17283 default_argument = make_node (DEFAULT_ARG);
17284 DEFARG_TOKENS (default_argument)
17285 = cp_token_cache_new (first_token, token);
17286 DEFARG_INSTANTIATIONS (default_argument) = NULL;
17287 }
17288 /* Outside of a class definition, we can just parse the
17289 assignment-expression. */
17290 else
17291 {
17292 token = cp_lexer_peek_token (parser->lexer);
17293 default_argument
17294 = cp_parser_default_argument (parser, template_parm_p);
17295 }
17296
17297 if (!parser->default_arg_ok_p)
17298 {
17299 if (flag_permissive)
17300 warning (0, "deprecated use of default argument for parameter of non-function");
17301 else
17302 {
17303 error_at (token->location,
17304 "default arguments are only "
17305 "permitted for function parameters");
17306 default_argument = NULL_TREE;
17307 }
17308 }
17309 else if ((declarator && declarator->parameter_pack_p)
17310 || (decl_specifiers.type
17311 && PACK_EXPANSION_P (decl_specifiers.type)))
17312 {
17313 /* Find the name of the parameter pack. */
17314 cp_declarator *id_declarator = declarator;
17315 while (id_declarator && id_declarator->kind != cdk_id)
17316 id_declarator = id_declarator->declarator;
17317
17318 if (id_declarator && id_declarator->kind == cdk_id)
17319 error_at (declarator_token_start->location,
17320 template_parm_p
17321 ? G_("template parameter pack %qD "
17322 "cannot have a default argument")
17323 : G_("parameter pack %qD cannot have "
17324 "a default argument"),
17325 id_declarator->u.id.unqualified_name);
17326 else
17327 error_at (declarator_token_start->location,
17328 template_parm_p
17329 ? G_("template parameter pack cannot have "
17330 "a default argument")
17331 : G_("parameter pack cannot have a "
17332 "default argument"));
17333
17334 default_argument = NULL_TREE;
17335 }
17336 }
17337 else
17338 default_argument = NULL_TREE;
17339
17340 return make_parameter_declarator (&decl_specifiers,
17341 declarator,
17342 default_argument);
17343 }
17344
17345 /* Parse a default argument and return it.
17346
17347 TEMPLATE_PARM_P is true if this is a default argument for a
17348 non-type template parameter. */
17349 static tree
17350 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17351 {
17352 tree default_argument = NULL_TREE;
17353 bool saved_greater_than_is_operator_p;
17354 bool saved_local_variables_forbidden_p;
17355 bool non_constant_p, is_direct_init;
17356
17357 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17358 set correctly. */
17359 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17360 parser->greater_than_is_operator_p = !template_parm_p;
17361 /* Local variable names (and the `this' keyword) may not
17362 appear in a default argument. */
17363 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17364 parser->local_variables_forbidden_p = true;
17365 /* Parse the assignment-expression. */
17366 if (template_parm_p)
17367 push_deferring_access_checks (dk_no_deferred);
17368 default_argument
17369 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17370 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17371 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17372 if (template_parm_p)
17373 pop_deferring_access_checks ();
17374 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17375 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17376
17377 return default_argument;
17378 }
17379
17380 /* Parse a function-body.
17381
17382 function-body:
17383 compound_statement */
17384
17385 static void
17386 cp_parser_function_body (cp_parser *parser)
17387 {
17388 cp_parser_compound_statement (parser, NULL, false, true);
17389 }
17390
17391 /* Parse a ctor-initializer-opt followed by a function-body. Return
17392 true if a ctor-initializer was present. */
17393
17394 static bool
17395 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17396 {
17397 tree body, list;
17398 bool ctor_initializer_p;
17399 const bool check_body_p =
17400 DECL_CONSTRUCTOR_P (current_function_decl)
17401 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17402 tree last = NULL;
17403
17404 /* Begin the function body. */
17405 body = begin_function_body ();
17406 /* Parse the optional ctor-initializer. */
17407 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17408
17409 /* If we're parsing a constexpr constructor definition, we need
17410 to check that the constructor body is indeed empty. However,
17411 before we get to cp_parser_function_body lot of junk has been
17412 generated, so we can't just check that we have an empty block.
17413 Rather we take a snapshot of the outermost block, and check whether
17414 cp_parser_function_body changed its state. */
17415 if (check_body_p)
17416 {
17417 list = body;
17418 if (TREE_CODE (list) == BIND_EXPR)
17419 list = BIND_EXPR_BODY (list);
17420 if (TREE_CODE (list) == STATEMENT_LIST
17421 && STATEMENT_LIST_TAIL (list) != NULL)
17422 last = STATEMENT_LIST_TAIL (list)->stmt;
17423 }
17424 /* Parse the function-body. */
17425 cp_parser_function_body (parser);
17426 if (check_body_p)
17427 check_constexpr_ctor_body (last, list);
17428 /* Finish the function body. */
17429 finish_function_body (body);
17430
17431 return ctor_initializer_p;
17432 }
17433
17434 /* Parse an initializer.
17435
17436 initializer:
17437 = initializer-clause
17438 ( expression-list )
17439
17440 Returns an expression representing the initializer. If no
17441 initializer is present, NULL_TREE is returned.
17442
17443 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17444 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17445 set to TRUE if there is no initializer present. If there is an
17446 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17447 is set to true; otherwise it is set to false. */
17448
17449 static tree
17450 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17451 bool* non_constant_p)
17452 {
17453 cp_token *token;
17454 tree init;
17455
17456 /* Peek at the next token. */
17457 token = cp_lexer_peek_token (parser->lexer);
17458
17459 /* Let our caller know whether or not this initializer was
17460 parenthesized. */
17461 *is_direct_init = (token->type != CPP_EQ);
17462 /* Assume that the initializer is constant. */
17463 *non_constant_p = false;
17464
17465 if (token->type == CPP_EQ)
17466 {
17467 /* Consume the `='. */
17468 cp_lexer_consume_token (parser->lexer);
17469 /* Parse the initializer-clause. */
17470 init = cp_parser_initializer_clause (parser, non_constant_p);
17471 }
17472 else if (token->type == CPP_OPEN_PAREN)
17473 {
17474 VEC(tree,gc) *vec;
17475 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17476 /*cast_p=*/false,
17477 /*allow_expansion_p=*/true,
17478 non_constant_p);
17479 if (vec == NULL)
17480 return error_mark_node;
17481 init = build_tree_list_vec (vec);
17482 release_tree_vector (vec);
17483 }
17484 else if (token->type == CPP_OPEN_BRACE)
17485 {
17486 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17487 init = cp_parser_braced_list (parser, non_constant_p);
17488 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17489 }
17490 else
17491 {
17492 /* Anything else is an error. */
17493 cp_parser_error (parser, "expected initializer");
17494 init = error_mark_node;
17495 }
17496
17497 return init;
17498 }
17499
17500 /* Parse an initializer-clause.
17501
17502 initializer-clause:
17503 assignment-expression
17504 braced-init-list
17505
17506 Returns an expression representing the initializer.
17507
17508 If the `assignment-expression' production is used the value
17509 returned is simply a representation for the expression.
17510
17511 Otherwise, calls cp_parser_braced_list. */
17512
17513 static tree
17514 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17515 {
17516 tree initializer;
17517
17518 /* Assume the expression is constant. */
17519 *non_constant_p = false;
17520
17521 /* If it is not a `{', then we are looking at an
17522 assignment-expression. */
17523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17524 {
17525 initializer
17526 = cp_parser_constant_expression (parser,
17527 /*allow_non_constant_p=*/true,
17528 non_constant_p);
17529 }
17530 else
17531 initializer = cp_parser_braced_list (parser, non_constant_p);
17532
17533 return initializer;
17534 }
17535
17536 /* Parse a brace-enclosed initializer list.
17537
17538 braced-init-list:
17539 { initializer-list , [opt] }
17540 { }
17541
17542 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17543 the elements of the initializer-list (or NULL, if the last
17544 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17545 NULL_TREE. There is no way to detect whether or not the optional
17546 trailing `,' was provided. NON_CONSTANT_P is as for
17547 cp_parser_initializer. */
17548
17549 static tree
17550 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17551 {
17552 tree initializer;
17553
17554 /* Consume the `{' token. */
17555 cp_lexer_consume_token (parser->lexer);
17556 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17557 initializer = make_node (CONSTRUCTOR);
17558 /* If it's not a `}', then there is a non-trivial initializer. */
17559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17560 {
17561 /* Parse the initializer list. */
17562 CONSTRUCTOR_ELTS (initializer)
17563 = cp_parser_initializer_list (parser, non_constant_p);
17564 /* A trailing `,' token is allowed. */
17565 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17566 cp_lexer_consume_token (parser->lexer);
17567 }
17568 /* Now, there should be a trailing `}'. */
17569 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17570 TREE_TYPE (initializer) = init_list_type_node;
17571 return initializer;
17572 }
17573
17574 /* Parse an initializer-list.
17575
17576 initializer-list:
17577 initializer-clause ... [opt]
17578 initializer-list , initializer-clause ... [opt]
17579
17580 GNU Extension:
17581
17582 initializer-list:
17583 designation initializer-clause ...[opt]
17584 initializer-list , designation initializer-clause ...[opt]
17585
17586 designation:
17587 . identifier =
17588 identifier :
17589 [ constant-expression ] =
17590
17591 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17592 for the initializer. If the INDEX of the elt is non-NULL, it is the
17593 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17594 as for cp_parser_initializer. */
17595
17596 static VEC(constructor_elt,gc) *
17597 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17598 {
17599 VEC(constructor_elt,gc) *v = NULL;
17600
17601 /* Assume all of the expressions are constant. */
17602 *non_constant_p = false;
17603
17604 /* Parse the rest of the list. */
17605 while (true)
17606 {
17607 cp_token *token;
17608 tree designator;
17609 tree initializer;
17610 bool clause_non_constant_p;
17611
17612 /* If the next token is an identifier and the following one is a
17613 colon, we are looking at the GNU designated-initializer
17614 syntax. */
17615 if (cp_parser_allow_gnu_extensions_p (parser)
17616 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17617 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17618 {
17619 /* Warn the user that they are using an extension. */
17620 pedwarn (input_location, OPT_pedantic,
17621 "ISO C++ does not allow designated initializers");
17622 /* Consume the identifier. */
17623 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17624 /* Consume the `:'. */
17625 cp_lexer_consume_token (parser->lexer);
17626 }
17627 /* Also handle the C99 syntax, '. id ='. */
17628 else if (cp_parser_allow_gnu_extensions_p (parser)
17629 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17630 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17631 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17632 {
17633 /* Warn the user that they are using an extension. */
17634 pedwarn (input_location, OPT_pedantic,
17635 "ISO C++ does not allow C99 designated initializers");
17636 /* Consume the `.'. */
17637 cp_lexer_consume_token (parser->lexer);
17638 /* Consume the identifier. */
17639 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17640 /* Consume the `='. */
17641 cp_lexer_consume_token (parser->lexer);
17642 }
17643 /* Also handle C99 array designators, '[ const ] ='. */
17644 else if (cp_parser_allow_gnu_extensions_p (parser)
17645 && !c_dialect_objc ()
17646 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17647 {
17648 /* In C++11, [ could start a lambda-introducer. */
17649 cp_parser_parse_tentatively (parser);
17650 cp_lexer_consume_token (parser->lexer);
17651 designator = cp_parser_constant_expression (parser, false, NULL);
17652 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17653 cp_parser_require (parser, CPP_EQ, RT_EQ);
17654 cp_parser_parse_definitely (parser);
17655 }
17656 else
17657 designator = NULL_TREE;
17658
17659 /* Parse the initializer. */
17660 initializer = cp_parser_initializer_clause (parser,
17661 &clause_non_constant_p);
17662 /* If any clause is non-constant, so is the entire initializer. */
17663 if (clause_non_constant_p)
17664 *non_constant_p = true;
17665
17666 /* If we have an ellipsis, this is an initializer pack
17667 expansion. */
17668 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17669 {
17670 /* Consume the `...'. */
17671 cp_lexer_consume_token (parser->lexer);
17672
17673 /* Turn the initializer into an initializer expansion. */
17674 initializer = make_pack_expansion (initializer);
17675 }
17676
17677 /* Add it to the vector. */
17678 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17679
17680 /* If the next token is not a comma, we have reached the end of
17681 the list. */
17682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17683 break;
17684
17685 /* Peek at the next token. */
17686 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17687 /* If the next token is a `}', then we're still done. An
17688 initializer-clause can have a trailing `,' after the
17689 initializer-list and before the closing `}'. */
17690 if (token->type == CPP_CLOSE_BRACE)
17691 break;
17692
17693 /* Consume the `,' token. */
17694 cp_lexer_consume_token (parser->lexer);
17695 }
17696
17697 return v;
17698 }
17699
17700 /* Classes [gram.class] */
17701
17702 /* Parse a class-name.
17703
17704 class-name:
17705 identifier
17706 template-id
17707
17708 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17709 to indicate that names looked up in dependent types should be
17710 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17711 keyword has been used to indicate that the name that appears next
17712 is a template. TAG_TYPE indicates the explicit tag given before
17713 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17714 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17715 is the class being defined in a class-head.
17716
17717 Returns the TYPE_DECL representing the class. */
17718
17719 static tree
17720 cp_parser_class_name (cp_parser *parser,
17721 bool typename_keyword_p,
17722 bool template_keyword_p,
17723 enum tag_types tag_type,
17724 bool check_dependency_p,
17725 bool class_head_p,
17726 bool is_declaration)
17727 {
17728 tree decl;
17729 tree scope;
17730 bool typename_p;
17731 cp_token *token;
17732 tree identifier = NULL_TREE;
17733
17734 /* All class-names start with an identifier. */
17735 token = cp_lexer_peek_token (parser->lexer);
17736 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17737 {
17738 cp_parser_error (parser, "expected class-name");
17739 return error_mark_node;
17740 }
17741
17742 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17743 to a template-id, so we save it here. */
17744 scope = parser->scope;
17745 if (scope == error_mark_node)
17746 return error_mark_node;
17747
17748 /* Any name names a type if we're following the `typename' keyword
17749 in a qualified name where the enclosing scope is type-dependent. */
17750 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17751 && dependent_type_p (scope));
17752 /* Handle the common case (an identifier, but not a template-id)
17753 efficiently. */
17754 if (token->type == CPP_NAME
17755 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17756 {
17757 cp_token *identifier_token;
17758 bool ambiguous_p;
17759
17760 /* Look for the identifier. */
17761 identifier_token = cp_lexer_peek_token (parser->lexer);
17762 ambiguous_p = identifier_token->ambiguous_p;
17763 identifier = cp_parser_identifier (parser);
17764 /* If the next token isn't an identifier, we are certainly not
17765 looking at a class-name. */
17766 if (identifier == error_mark_node)
17767 decl = error_mark_node;
17768 /* If we know this is a type-name, there's no need to look it
17769 up. */
17770 else if (typename_p)
17771 decl = identifier;
17772 else
17773 {
17774 tree ambiguous_decls;
17775 /* If we already know that this lookup is ambiguous, then
17776 we've already issued an error message; there's no reason
17777 to check again. */
17778 if (ambiguous_p)
17779 {
17780 cp_parser_simulate_error (parser);
17781 return error_mark_node;
17782 }
17783 /* If the next token is a `::', then the name must be a type
17784 name.
17785
17786 [basic.lookup.qual]
17787
17788 During the lookup for a name preceding the :: scope
17789 resolution operator, object, function, and enumerator
17790 names are ignored. */
17791 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17792 tag_type = typename_type;
17793 /* Look up the name. */
17794 decl = cp_parser_lookup_name (parser, identifier,
17795 tag_type,
17796 /*is_template=*/false,
17797 /*is_namespace=*/false,
17798 check_dependency_p,
17799 &ambiguous_decls,
17800 identifier_token->location);
17801 if (ambiguous_decls)
17802 {
17803 if (cp_parser_parsing_tentatively (parser))
17804 cp_parser_simulate_error (parser);
17805 return error_mark_node;
17806 }
17807 }
17808 }
17809 else
17810 {
17811 /* Try a template-id. */
17812 decl = cp_parser_template_id (parser, template_keyword_p,
17813 check_dependency_p,
17814 is_declaration);
17815 if (decl == error_mark_node)
17816 return error_mark_node;
17817 }
17818
17819 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17820
17821 /* If this is a typename, create a TYPENAME_TYPE. */
17822 if (typename_p && decl != error_mark_node)
17823 {
17824 decl = make_typename_type (scope, decl, typename_type,
17825 /*complain=*/tf_error);
17826 if (decl != error_mark_node)
17827 decl = TYPE_NAME (decl);
17828 }
17829
17830 /* Check to see that it is really the name of a class. */
17831 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17832 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17833 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17834 /* Situations like this:
17835
17836 template <typename T> struct A {
17837 typename T::template X<int>::I i;
17838 };
17839
17840 are problematic. Is `T::template X<int>' a class-name? The
17841 standard does not seem to be definitive, but there is no other
17842 valid interpretation of the following `::'. Therefore, those
17843 names are considered class-names. */
17844 {
17845 decl = make_typename_type (scope, decl, tag_type, tf_error);
17846 if (decl != error_mark_node)
17847 decl = TYPE_NAME (decl);
17848 }
17849 else if (TREE_CODE (decl) != TYPE_DECL
17850 || TREE_TYPE (decl) == error_mark_node
17851 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17852 /* In Objective-C 2.0, a classname followed by '.' starts a
17853 dot-syntax expression, and it's not a type-name. */
17854 || (c_dialect_objc ()
17855 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17856 && objc_is_class_name (decl)))
17857 decl = error_mark_node;
17858
17859 if (decl == error_mark_node)
17860 cp_parser_error (parser, "expected class-name");
17861 else if (identifier && !parser->scope)
17862 maybe_note_name_used_in_class (identifier, decl);
17863
17864 return decl;
17865 }
17866
17867 /* Parse a class-specifier.
17868
17869 class-specifier:
17870 class-head { member-specification [opt] }
17871
17872 Returns the TREE_TYPE representing the class. */
17873
17874 static tree
17875 cp_parser_class_specifier_1 (cp_parser* parser)
17876 {
17877 tree type;
17878 tree attributes = NULL_TREE;
17879 bool nested_name_specifier_p;
17880 unsigned saved_num_template_parameter_lists;
17881 bool saved_in_function_body;
17882 unsigned char in_statement;
17883 bool in_switch_statement_p;
17884 bool saved_in_unbraced_linkage_specification_p;
17885 tree old_scope = NULL_TREE;
17886 tree scope = NULL_TREE;
17887 tree bases;
17888 cp_token *closing_brace;
17889
17890 push_deferring_access_checks (dk_no_deferred);
17891
17892 /* Parse the class-head. */
17893 type = cp_parser_class_head (parser,
17894 &nested_name_specifier_p,
17895 &attributes,
17896 &bases);
17897 /* If the class-head was a semantic disaster, skip the entire body
17898 of the class. */
17899 if (!type)
17900 {
17901 cp_parser_skip_to_end_of_block_or_statement (parser);
17902 pop_deferring_access_checks ();
17903 return error_mark_node;
17904 }
17905
17906 /* Look for the `{'. */
17907 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17908 {
17909 pop_deferring_access_checks ();
17910 return error_mark_node;
17911 }
17912
17913 /* Process the base classes. If they're invalid, skip the
17914 entire class body. */
17915 if (!xref_basetypes (type, bases))
17916 {
17917 /* Consuming the closing brace yields better error messages
17918 later on. */
17919 if (cp_parser_skip_to_closing_brace (parser))
17920 cp_lexer_consume_token (parser->lexer);
17921 pop_deferring_access_checks ();
17922 return error_mark_node;
17923 }
17924
17925 /* Issue an error message if type-definitions are forbidden here. */
17926 cp_parser_check_type_definition (parser);
17927 /* Remember that we are defining one more class. */
17928 ++parser->num_classes_being_defined;
17929 /* Inside the class, surrounding template-parameter-lists do not
17930 apply. */
17931 saved_num_template_parameter_lists
17932 = parser->num_template_parameter_lists;
17933 parser->num_template_parameter_lists = 0;
17934 /* We are not in a function body. */
17935 saved_in_function_body = parser->in_function_body;
17936 parser->in_function_body = false;
17937 /* Or in a loop. */
17938 in_statement = parser->in_statement;
17939 parser->in_statement = 0;
17940 /* Or in a switch. */
17941 in_switch_statement_p = parser->in_switch_statement_p;
17942 parser->in_switch_statement_p = false;
17943 /* We are not immediately inside an extern "lang" block. */
17944 saved_in_unbraced_linkage_specification_p
17945 = parser->in_unbraced_linkage_specification_p;
17946 parser->in_unbraced_linkage_specification_p = false;
17947
17948 /* Start the class. */
17949 if (nested_name_specifier_p)
17950 {
17951 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17952 old_scope = push_inner_scope (scope);
17953 }
17954 type = begin_class_definition (type, attributes);
17955
17956 if (type == error_mark_node)
17957 /* If the type is erroneous, skip the entire body of the class. */
17958 cp_parser_skip_to_closing_brace (parser);
17959 else
17960 /* Parse the member-specification. */
17961 cp_parser_member_specification_opt (parser);
17962
17963 /* Look for the trailing `}'. */
17964 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17965 /* Look for trailing attributes to apply to this class. */
17966 if (cp_parser_allow_gnu_extensions_p (parser))
17967 attributes = cp_parser_attributes_opt (parser);
17968 if (type != error_mark_node)
17969 type = finish_struct (type, attributes);
17970 if (nested_name_specifier_p)
17971 pop_inner_scope (old_scope, scope);
17972
17973 /* We've finished a type definition. Check for the common syntax
17974 error of forgetting a semicolon after the definition. We need to
17975 be careful, as we can't just check for not-a-semicolon and be done
17976 with it; the user might have typed:
17977
17978 class X { } c = ...;
17979 class X { } *p = ...;
17980
17981 and so forth. Instead, enumerate all the possible tokens that
17982 might follow this production; if we don't see one of them, then
17983 complain and silently insert the semicolon. */
17984 {
17985 cp_token *token = cp_lexer_peek_token (parser->lexer);
17986 bool want_semicolon = true;
17987
17988 switch (token->type)
17989 {
17990 case CPP_NAME:
17991 case CPP_SEMICOLON:
17992 case CPP_MULT:
17993 case CPP_AND:
17994 case CPP_OPEN_PAREN:
17995 case CPP_CLOSE_PAREN:
17996 case CPP_COMMA:
17997 want_semicolon = false;
17998 break;
17999
18000 /* While it's legal for type qualifiers and storage class
18001 specifiers to follow type definitions in the grammar, only
18002 compiler testsuites contain code like that. Assume that if
18003 we see such code, then what we're really seeing is a case
18004 like:
18005
18006 class X { }
18007 const <type> var = ...;
18008
18009 or
18010
18011 class Y { }
18012 static <type> func (...) ...
18013
18014 i.e. the qualifier or specifier applies to the next
18015 declaration. To do so, however, we need to look ahead one
18016 more token to see if *that* token is a type specifier.
18017
18018 This code could be improved to handle:
18019
18020 class Z { }
18021 static const <type> var = ...; */
18022 case CPP_KEYWORD:
18023 if (keyword_is_decl_specifier (token->keyword))
18024 {
18025 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18026
18027 /* Handling user-defined types here would be nice, but very
18028 tricky. */
18029 want_semicolon
18030 = (lookahead->type == CPP_KEYWORD
18031 && keyword_begins_type_specifier (lookahead->keyword));
18032 }
18033 break;
18034 default:
18035 break;
18036 }
18037
18038 /* If we don't have a type, then something is very wrong and we
18039 shouldn't try to do anything clever. Likewise for not seeing the
18040 closing brace. */
18041 if (closing_brace && TYPE_P (type) && want_semicolon)
18042 {
18043 cp_token_position prev
18044 = cp_lexer_previous_token_position (parser->lexer);
18045 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18046 location_t loc = prev_token->location;
18047
18048 if (CLASSTYPE_DECLARED_CLASS (type))
18049 error_at (loc, "expected %<;%> after class definition");
18050 else if (TREE_CODE (type) == RECORD_TYPE)
18051 error_at (loc, "expected %<;%> after struct definition");
18052 else if (TREE_CODE (type) == UNION_TYPE)
18053 error_at (loc, "expected %<;%> after union definition");
18054 else
18055 gcc_unreachable ();
18056
18057 /* Unget one token and smash it to look as though we encountered
18058 a semicolon in the input stream. */
18059 cp_lexer_set_token_position (parser->lexer, prev);
18060 token = cp_lexer_peek_token (parser->lexer);
18061 token->type = CPP_SEMICOLON;
18062 token->keyword = RID_MAX;
18063 }
18064 }
18065
18066 /* If this class is not itself within the scope of another class,
18067 then we need to parse the bodies of all of the queued function
18068 definitions. Note that the queued functions defined in a class
18069 are not always processed immediately following the
18070 class-specifier for that class. Consider:
18071
18072 struct A {
18073 struct B { void f() { sizeof (A); } };
18074 };
18075
18076 If `f' were processed before the processing of `A' were
18077 completed, there would be no way to compute the size of `A'.
18078 Note that the nesting we are interested in here is lexical --
18079 not the semantic nesting given by TYPE_CONTEXT. In particular,
18080 for:
18081
18082 struct A { struct B; };
18083 struct A::B { void f() { } };
18084
18085 there is no need to delay the parsing of `A::B::f'. */
18086 if (--parser->num_classes_being_defined == 0)
18087 {
18088 tree decl;
18089 tree class_type = NULL_TREE;
18090 tree pushed_scope = NULL_TREE;
18091 unsigned ix;
18092 cp_default_arg_entry *e;
18093 tree save_ccp, save_ccr;
18094
18095 /* In a first pass, parse default arguments to the functions.
18096 Then, in a second pass, parse the bodies of the functions.
18097 This two-phased approach handles cases like:
18098
18099 struct S {
18100 void f() { g(); }
18101 void g(int i = 3);
18102 };
18103
18104 */
18105 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18106 ix, e)
18107 {
18108 decl = e->decl;
18109 /* If there are default arguments that have not yet been processed,
18110 take care of them now. */
18111 if (class_type != e->class_type)
18112 {
18113 if (pushed_scope)
18114 pop_scope (pushed_scope);
18115 class_type = e->class_type;
18116 pushed_scope = push_scope (class_type);
18117 }
18118 /* Make sure that any template parameters are in scope. */
18119 maybe_begin_member_template_processing (decl);
18120 /* Parse the default argument expressions. */
18121 cp_parser_late_parsing_default_args (parser, decl);
18122 /* Remove any template parameters from the symbol table. */
18123 maybe_end_member_template_processing ();
18124 }
18125 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18126 /* Now parse any NSDMIs. */
18127 save_ccp = current_class_ptr;
18128 save_ccr = current_class_ref;
18129 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18130 {
18131 if (class_type != DECL_CONTEXT (decl))
18132 {
18133 if (pushed_scope)
18134 pop_scope (pushed_scope);
18135 class_type = DECL_CONTEXT (decl);
18136 pushed_scope = push_scope (class_type);
18137 }
18138 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18139 cp_parser_late_parsing_nsdmi (parser, decl);
18140 }
18141 VEC_truncate (tree, unparsed_nsdmis, 0);
18142 current_class_ptr = save_ccp;
18143 current_class_ref = save_ccr;
18144 if (pushed_scope)
18145 pop_scope (pushed_scope);
18146 /* Now parse the body of the functions. */
18147 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18148 cp_parser_late_parsing_for_member (parser, decl);
18149 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18150 }
18151
18152 /* Put back any saved access checks. */
18153 pop_deferring_access_checks ();
18154
18155 /* Restore saved state. */
18156 parser->in_switch_statement_p = in_switch_statement_p;
18157 parser->in_statement = in_statement;
18158 parser->in_function_body = saved_in_function_body;
18159 parser->num_template_parameter_lists
18160 = saved_num_template_parameter_lists;
18161 parser->in_unbraced_linkage_specification_p
18162 = saved_in_unbraced_linkage_specification_p;
18163
18164 return type;
18165 }
18166
18167 static tree
18168 cp_parser_class_specifier (cp_parser* parser)
18169 {
18170 tree ret;
18171 timevar_push (TV_PARSE_STRUCT);
18172 ret = cp_parser_class_specifier_1 (parser);
18173 timevar_pop (TV_PARSE_STRUCT);
18174 return ret;
18175 }
18176
18177 /* Parse a class-head.
18178
18179 class-head:
18180 class-key identifier [opt] base-clause [opt]
18181 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18182 class-key nested-name-specifier [opt] template-id
18183 base-clause [opt]
18184
18185 class-virt-specifier:
18186 final
18187
18188 GNU Extensions:
18189 class-key attributes identifier [opt] base-clause [opt]
18190 class-key attributes nested-name-specifier identifier base-clause [opt]
18191 class-key attributes nested-name-specifier [opt] template-id
18192 base-clause [opt]
18193
18194 Upon return BASES is initialized to the list of base classes (or
18195 NULL, if there are none) in the same form returned by
18196 cp_parser_base_clause.
18197
18198 Returns the TYPE of the indicated class. Sets
18199 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18200 involving a nested-name-specifier was used, and FALSE otherwise.
18201
18202 Returns error_mark_node if this is not a class-head.
18203
18204 Returns NULL_TREE if the class-head is syntactically valid, but
18205 semantically invalid in a way that means we should skip the entire
18206 body of the class. */
18207
18208 static tree
18209 cp_parser_class_head (cp_parser* parser,
18210 bool* nested_name_specifier_p,
18211 tree *attributes_p,
18212 tree *bases)
18213 {
18214 tree nested_name_specifier;
18215 enum tag_types class_key;
18216 tree id = NULL_TREE;
18217 tree type = NULL_TREE;
18218 tree attributes;
18219 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18220 bool template_id_p = false;
18221 bool qualified_p = false;
18222 bool invalid_nested_name_p = false;
18223 bool invalid_explicit_specialization_p = false;
18224 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18225 tree pushed_scope = NULL_TREE;
18226 unsigned num_templates;
18227 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18228 /* Assume no nested-name-specifier will be present. */
18229 *nested_name_specifier_p = false;
18230 /* Assume no template parameter lists will be used in defining the
18231 type. */
18232 num_templates = 0;
18233 parser->colon_corrects_to_scope_p = false;
18234
18235 *bases = NULL_TREE;
18236
18237 /* Look for the class-key. */
18238 class_key = cp_parser_class_key (parser);
18239 if (class_key == none_type)
18240 return error_mark_node;
18241
18242 /* Parse the attributes. */
18243 attributes = cp_parser_attributes_opt (parser);
18244
18245 /* If the next token is `::', that is invalid -- but sometimes
18246 people do try to write:
18247
18248 struct ::S {};
18249
18250 Handle this gracefully by accepting the extra qualifier, and then
18251 issuing an error about it later if this really is a
18252 class-head. If it turns out just to be an elaborated type
18253 specifier, remain silent. */
18254 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18255 qualified_p = true;
18256
18257 push_deferring_access_checks (dk_no_check);
18258
18259 /* Determine the name of the class. Begin by looking for an
18260 optional nested-name-specifier. */
18261 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18262 nested_name_specifier
18263 = cp_parser_nested_name_specifier_opt (parser,
18264 /*typename_keyword_p=*/false,
18265 /*check_dependency_p=*/false,
18266 /*type_p=*/false,
18267 /*is_declaration=*/false);
18268 /* If there was a nested-name-specifier, then there *must* be an
18269 identifier. */
18270 if (nested_name_specifier)
18271 {
18272 type_start_token = cp_lexer_peek_token (parser->lexer);
18273 /* Although the grammar says `identifier', it really means
18274 `class-name' or `template-name'. You are only allowed to
18275 define a class that has already been declared with this
18276 syntax.
18277
18278 The proposed resolution for Core Issue 180 says that wherever
18279 you see `class T::X' you should treat `X' as a type-name.
18280
18281 It is OK to define an inaccessible class; for example:
18282
18283 class A { class B; };
18284 class A::B {};
18285
18286 We do not know if we will see a class-name, or a
18287 template-name. We look for a class-name first, in case the
18288 class-name is a template-id; if we looked for the
18289 template-name first we would stop after the template-name. */
18290 cp_parser_parse_tentatively (parser);
18291 type = cp_parser_class_name (parser,
18292 /*typename_keyword_p=*/false,
18293 /*template_keyword_p=*/false,
18294 class_type,
18295 /*check_dependency_p=*/false,
18296 /*class_head_p=*/true,
18297 /*is_declaration=*/false);
18298 /* If that didn't work, ignore the nested-name-specifier. */
18299 if (!cp_parser_parse_definitely (parser))
18300 {
18301 invalid_nested_name_p = true;
18302 type_start_token = cp_lexer_peek_token (parser->lexer);
18303 id = cp_parser_identifier (parser);
18304 if (id == error_mark_node)
18305 id = NULL_TREE;
18306 }
18307 /* If we could not find a corresponding TYPE, treat this
18308 declaration like an unqualified declaration. */
18309 if (type == error_mark_node)
18310 nested_name_specifier = NULL_TREE;
18311 /* Otherwise, count the number of templates used in TYPE and its
18312 containing scopes. */
18313 else
18314 {
18315 tree scope;
18316
18317 for (scope = TREE_TYPE (type);
18318 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18319 scope = (TYPE_P (scope)
18320 ? TYPE_CONTEXT (scope)
18321 : DECL_CONTEXT (scope)))
18322 if (TYPE_P (scope)
18323 && CLASS_TYPE_P (scope)
18324 && CLASSTYPE_TEMPLATE_INFO (scope)
18325 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18326 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18327 ++num_templates;
18328 }
18329 }
18330 /* Otherwise, the identifier is optional. */
18331 else
18332 {
18333 /* We don't know whether what comes next is a template-id,
18334 an identifier, or nothing at all. */
18335 cp_parser_parse_tentatively (parser);
18336 /* Check for a template-id. */
18337 type_start_token = cp_lexer_peek_token (parser->lexer);
18338 id = cp_parser_template_id (parser,
18339 /*template_keyword_p=*/false,
18340 /*check_dependency_p=*/true,
18341 /*is_declaration=*/true);
18342 /* If that didn't work, it could still be an identifier. */
18343 if (!cp_parser_parse_definitely (parser))
18344 {
18345 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18346 {
18347 type_start_token = cp_lexer_peek_token (parser->lexer);
18348 id = cp_parser_identifier (parser);
18349 }
18350 else
18351 id = NULL_TREE;
18352 }
18353 else
18354 {
18355 template_id_p = true;
18356 ++num_templates;
18357 }
18358 }
18359
18360 pop_deferring_access_checks ();
18361
18362 if (id)
18363 {
18364 cp_parser_check_for_invalid_template_id (parser, id,
18365 type_start_token->location);
18366 }
18367 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18368
18369 /* If it's not a `:' or a `{' then we can't really be looking at a
18370 class-head, since a class-head only appears as part of a
18371 class-specifier. We have to detect this situation before calling
18372 xref_tag, since that has irreversible side-effects. */
18373 if (!cp_parser_next_token_starts_class_definition_p (parser))
18374 {
18375 cp_parser_error (parser, "expected %<{%> or %<:%>");
18376 type = error_mark_node;
18377 goto out;
18378 }
18379
18380 /* At this point, we're going ahead with the class-specifier, even
18381 if some other problem occurs. */
18382 cp_parser_commit_to_tentative_parse (parser);
18383 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18384 {
18385 cp_parser_error (parser,
18386 "cannot specify %<override%> for a class");
18387 type = error_mark_node;
18388 goto out;
18389 }
18390 /* Issue the error about the overly-qualified name now. */
18391 if (qualified_p)
18392 {
18393 cp_parser_error (parser,
18394 "global qualification of class name is invalid");
18395 type = error_mark_node;
18396 goto out;
18397 }
18398 else if (invalid_nested_name_p)
18399 {
18400 cp_parser_error (parser,
18401 "qualified name does not name a class");
18402 type = error_mark_node;
18403 goto out;
18404 }
18405 else if (nested_name_specifier)
18406 {
18407 tree scope;
18408
18409 /* Reject typedef-names in class heads. */
18410 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18411 {
18412 error_at (type_start_token->location,
18413 "invalid class name in declaration of %qD",
18414 type);
18415 type = NULL_TREE;
18416 goto done;
18417 }
18418
18419 /* Figure out in what scope the declaration is being placed. */
18420 scope = current_scope ();
18421 /* If that scope does not contain the scope in which the
18422 class was originally declared, the program is invalid. */
18423 if (scope && !is_ancestor (scope, nested_name_specifier))
18424 {
18425 if (at_namespace_scope_p ())
18426 error_at (type_start_token->location,
18427 "declaration of %qD in namespace %qD which does not "
18428 "enclose %qD",
18429 type, scope, nested_name_specifier);
18430 else
18431 error_at (type_start_token->location,
18432 "declaration of %qD in %qD which does not enclose %qD",
18433 type, scope, nested_name_specifier);
18434 type = NULL_TREE;
18435 goto done;
18436 }
18437 /* [dcl.meaning]
18438
18439 A declarator-id shall not be qualified except for the
18440 definition of a ... nested class outside of its class
18441 ... [or] the definition or explicit instantiation of a
18442 class member of a namespace outside of its namespace. */
18443 if (scope == nested_name_specifier)
18444 {
18445 permerror (nested_name_specifier_token_start->location,
18446 "extra qualification not allowed");
18447 nested_name_specifier = NULL_TREE;
18448 num_templates = 0;
18449 }
18450 }
18451 /* An explicit-specialization must be preceded by "template <>". If
18452 it is not, try to recover gracefully. */
18453 if (at_namespace_scope_p ()
18454 && parser->num_template_parameter_lists == 0
18455 && template_id_p)
18456 {
18457 error_at (type_start_token->location,
18458 "an explicit specialization must be preceded by %<template <>%>");
18459 invalid_explicit_specialization_p = true;
18460 /* Take the same action that would have been taken by
18461 cp_parser_explicit_specialization. */
18462 ++parser->num_template_parameter_lists;
18463 begin_specialization ();
18464 }
18465 /* There must be no "return" statements between this point and the
18466 end of this function; set "type "to the correct return value and
18467 use "goto done;" to return. */
18468 /* Make sure that the right number of template parameters were
18469 present. */
18470 if (!cp_parser_check_template_parameters (parser, num_templates,
18471 type_start_token->location,
18472 /*declarator=*/NULL))
18473 {
18474 /* If something went wrong, there is no point in even trying to
18475 process the class-definition. */
18476 type = NULL_TREE;
18477 goto done;
18478 }
18479
18480 /* Look up the type. */
18481 if (template_id_p)
18482 {
18483 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18484 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18485 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18486 {
18487 error_at (type_start_token->location,
18488 "function template %qD redeclared as a class template", id);
18489 type = error_mark_node;
18490 }
18491 else
18492 {
18493 type = TREE_TYPE (id);
18494 type = maybe_process_partial_specialization (type);
18495 }
18496 if (nested_name_specifier)
18497 pushed_scope = push_scope (nested_name_specifier);
18498 }
18499 else if (nested_name_specifier)
18500 {
18501 tree class_type;
18502
18503 /* Given:
18504
18505 template <typename T> struct S { struct T };
18506 template <typename T> struct S<T>::T { };
18507
18508 we will get a TYPENAME_TYPE when processing the definition of
18509 `S::T'. We need to resolve it to the actual type before we
18510 try to define it. */
18511 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18512 {
18513 class_type = resolve_typename_type (TREE_TYPE (type),
18514 /*only_current_p=*/false);
18515 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18516 type = TYPE_NAME (class_type);
18517 else
18518 {
18519 cp_parser_error (parser, "could not resolve typename type");
18520 type = error_mark_node;
18521 }
18522 }
18523
18524 if (maybe_process_partial_specialization (TREE_TYPE (type))
18525 == error_mark_node)
18526 {
18527 type = NULL_TREE;
18528 goto done;
18529 }
18530
18531 class_type = current_class_type;
18532 /* Enter the scope indicated by the nested-name-specifier. */
18533 pushed_scope = push_scope (nested_name_specifier);
18534 /* Get the canonical version of this type. */
18535 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18536 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18537 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18538 {
18539 type = push_template_decl (type);
18540 if (type == error_mark_node)
18541 {
18542 type = NULL_TREE;
18543 goto done;
18544 }
18545 }
18546
18547 type = TREE_TYPE (type);
18548 *nested_name_specifier_p = true;
18549 }
18550 else /* The name is not a nested name. */
18551 {
18552 /* If the class was unnamed, create a dummy name. */
18553 if (!id)
18554 id = make_anon_name ();
18555 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18556 parser->num_template_parameter_lists);
18557 }
18558
18559 /* Indicate whether this class was declared as a `class' or as a
18560 `struct'. */
18561 if (TREE_CODE (type) == RECORD_TYPE)
18562 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18563 cp_parser_check_class_key (class_key, type);
18564
18565 /* If this type was already complete, and we see another definition,
18566 that's an error. */
18567 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18568 {
18569 error_at (type_start_token->location, "redefinition of %q#T",
18570 type);
18571 error_at (type_start_token->location, "previous definition of %q+#T",
18572 type);
18573 type = NULL_TREE;
18574 goto done;
18575 }
18576 else if (type == error_mark_node)
18577 type = NULL_TREE;
18578
18579 /* We will have entered the scope containing the class; the names of
18580 base classes should be looked up in that context. For example:
18581
18582 struct A { struct B {}; struct C; };
18583 struct A::C : B {};
18584
18585 is valid. */
18586
18587 /* Get the list of base-classes, if there is one. */
18588 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18589 *bases = cp_parser_base_clause (parser);
18590
18591 done:
18592 /* Leave the scope given by the nested-name-specifier. We will
18593 enter the class scope itself while processing the members. */
18594 if (pushed_scope)
18595 pop_scope (pushed_scope);
18596
18597 if (invalid_explicit_specialization_p)
18598 {
18599 end_specialization ();
18600 --parser->num_template_parameter_lists;
18601 }
18602
18603 if (type)
18604 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18605 *attributes_p = attributes;
18606 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18607 CLASSTYPE_FINAL (type) = 1;
18608 out:
18609 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18610 return type;
18611 }
18612
18613 /* Parse a class-key.
18614
18615 class-key:
18616 class
18617 struct
18618 union
18619
18620 Returns the kind of class-key specified, or none_type to indicate
18621 error. */
18622
18623 static enum tag_types
18624 cp_parser_class_key (cp_parser* parser)
18625 {
18626 cp_token *token;
18627 enum tag_types tag_type;
18628
18629 /* Look for the class-key. */
18630 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18631 if (!token)
18632 return none_type;
18633
18634 /* Check to see if the TOKEN is a class-key. */
18635 tag_type = cp_parser_token_is_class_key (token);
18636 if (!tag_type)
18637 cp_parser_error (parser, "expected class-key");
18638 return tag_type;
18639 }
18640
18641 /* Parse an (optional) member-specification.
18642
18643 member-specification:
18644 member-declaration member-specification [opt]
18645 access-specifier : member-specification [opt] */
18646
18647 static void
18648 cp_parser_member_specification_opt (cp_parser* parser)
18649 {
18650 while (true)
18651 {
18652 cp_token *token;
18653 enum rid keyword;
18654
18655 /* Peek at the next token. */
18656 token = cp_lexer_peek_token (parser->lexer);
18657 /* If it's a `}', or EOF then we've seen all the members. */
18658 if (token->type == CPP_CLOSE_BRACE
18659 || token->type == CPP_EOF
18660 || token->type == CPP_PRAGMA_EOL)
18661 break;
18662
18663 /* See if this token is a keyword. */
18664 keyword = token->keyword;
18665 switch (keyword)
18666 {
18667 case RID_PUBLIC:
18668 case RID_PROTECTED:
18669 case RID_PRIVATE:
18670 /* Consume the access-specifier. */
18671 cp_lexer_consume_token (parser->lexer);
18672 /* Remember which access-specifier is active. */
18673 current_access_specifier = token->u.value;
18674 /* Look for the `:'. */
18675 cp_parser_require (parser, CPP_COLON, RT_COLON);
18676 break;
18677
18678 default:
18679 /* Accept #pragmas at class scope. */
18680 if (token->type == CPP_PRAGMA)
18681 {
18682 cp_parser_pragma (parser, pragma_external);
18683 break;
18684 }
18685
18686 /* Otherwise, the next construction must be a
18687 member-declaration. */
18688 cp_parser_member_declaration (parser);
18689 }
18690 }
18691 }
18692
18693 /* Parse a member-declaration.
18694
18695 member-declaration:
18696 decl-specifier-seq [opt] member-declarator-list [opt] ;
18697 function-definition ; [opt]
18698 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18699 using-declaration
18700 template-declaration
18701 alias-declaration
18702
18703 member-declarator-list:
18704 member-declarator
18705 member-declarator-list , member-declarator
18706
18707 member-declarator:
18708 declarator pure-specifier [opt]
18709 declarator constant-initializer [opt]
18710 identifier [opt] : constant-expression
18711
18712 GNU Extensions:
18713
18714 member-declaration:
18715 __extension__ member-declaration
18716
18717 member-declarator:
18718 declarator attributes [opt] pure-specifier [opt]
18719 declarator attributes [opt] constant-initializer [opt]
18720 identifier [opt] attributes [opt] : constant-expression
18721
18722 C++0x Extensions:
18723
18724 member-declaration:
18725 static_assert-declaration */
18726
18727 static void
18728 cp_parser_member_declaration (cp_parser* parser)
18729 {
18730 cp_decl_specifier_seq decl_specifiers;
18731 tree prefix_attributes;
18732 tree decl;
18733 int declares_class_or_enum;
18734 bool friend_p;
18735 cp_token *token = NULL;
18736 cp_token *decl_spec_token_start = NULL;
18737 cp_token *initializer_token_start = NULL;
18738 int saved_pedantic;
18739 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18740
18741 /* Check for the `__extension__' keyword. */
18742 if (cp_parser_extension_opt (parser, &saved_pedantic))
18743 {
18744 /* Recurse. */
18745 cp_parser_member_declaration (parser);
18746 /* Restore the old value of the PEDANTIC flag. */
18747 pedantic = saved_pedantic;
18748
18749 return;
18750 }
18751
18752 /* Check for a template-declaration. */
18753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18754 {
18755 /* An explicit specialization here is an error condition, and we
18756 expect the specialization handler to detect and report this. */
18757 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18758 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18759 cp_parser_explicit_specialization (parser);
18760 else
18761 cp_parser_template_declaration (parser, /*member_p=*/true);
18762
18763 return;
18764 }
18765
18766 /* Check for a using-declaration. */
18767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18768 {
18769 if (cxx_dialect < cxx0x)
18770 {
18771 /* Parse the using-declaration. */
18772 cp_parser_using_declaration (parser,
18773 /*access_declaration_p=*/false);
18774 return;
18775 }
18776 else
18777 {
18778 tree decl;
18779 cp_parser_parse_tentatively (parser);
18780 decl = cp_parser_alias_declaration (parser);
18781 if (cp_parser_parse_definitely (parser))
18782 finish_member_declaration (decl);
18783 else
18784 cp_parser_using_declaration (parser,
18785 /*access_declaration_p=*/false);
18786 return;
18787 }
18788 }
18789
18790 /* Check for @defs. */
18791 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18792 {
18793 tree ivar, member;
18794 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18795 ivar = ivar_chains;
18796 while (ivar)
18797 {
18798 member = ivar;
18799 ivar = TREE_CHAIN (member);
18800 TREE_CHAIN (member) = NULL_TREE;
18801 finish_member_declaration (member);
18802 }
18803 return;
18804 }
18805
18806 /* If the next token is `static_assert' we have a static assertion. */
18807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18808 {
18809 cp_parser_static_assert (parser, /*member_p=*/true);
18810 return;
18811 }
18812
18813 parser->colon_corrects_to_scope_p = false;
18814
18815 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18816 goto out;
18817
18818 /* Parse the decl-specifier-seq. */
18819 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18820 cp_parser_decl_specifier_seq (parser,
18821 CP_PARSER_FLAGS_OPTIONAL,
18822 &decl_specifiers,
18823 &declares_class_or_enum);
18824 prefix_attributes = decl_specifiers.attributes;
18825 decl_specifiers.attributes = NULL_TREE;
18826 /* Check for an invalid type-name. */
18827 if (!decl_specifiers.any_type_specifiers_p
18828 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18829 goto out;
18830 /* If there is no declarator, then the decl-specifier-seq should
18831 specify a type. */
18832 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18833 {
18834 /* If there was no decl-specifier-seq, and the next token is a
18835 `;', then we have something like:
18836
18837 struct S { ; };
18838
18839 [class.mem]
18840
18841 Each member-declaration shall declare at least one member
18842 name of the class. */
18843 if (!decl_specifiers.any_specifiers_p)
18844 {
18845 cp_token *token = cp_lexer_peek_token (parser->lexer);
18846 if (!in_system_header_at (token->location))
18847 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18848 }
18849 else
18850 {
18851 tree type;
18852
18853 /* See if this declaration is a friend. */
18854 friend_p = cp_parser_friend_p (&decl_specifiers);
18855 /* If there were decl-specifiers, check to see if there was
18856 a class-declaration. */
18857 type = check_tag_decl (&decl_specifiers);
18858 /* Nested classes have already been added to the class, but
18859 a `friend' needs to be explicitly registered. */
18860 if (friend_p)
18861 {
18862 /* If the `friend' keyword was present, the friend must
18863 be introduced with a class-key. */
18864 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18865 pedwarn (decl_spec_token_start->location, OPT_pedantic,
18866 "in C++03 a class-key must be used "
18867 "when declaring a friend");
18868 /* In this case:
18869
18870 template <typename T> struct A {
18871 friend struct A<T>::B;
18872 };
18873
18874 A<T>::B will be represented by a TYPENAME_TYPE, and
18875 therefore not recognized by check_tag_decl. */
18876 if (!type)
18877 {
18878 type = decl_specifiers.type;
18879 if (type && TREE_CODE (type) == TYPE_DECL)
18880 type = TREE_TYPE (type);
18881 }
18882 if (!type || !TYPE_P (type))
18883 error_at (decl_spec_token_start->location,
18884 "friend declaration does not name a class or "
18885 "function");
18886 else
18887 make_friend_class (current_class_type, type,
18888 /*complain=*/true);
18889 }
18890 /* If there is no TYPE, an error message will already have
18891 been issued. */
18892 else if (!type || type == error_mark_node)
18893 ;
18894 /* An anonymous aggregate has to be handled specially; such
18895 a declaration really declares a data member (with a
18896 particular type), as opposed to a nested class. */
18897 else if (ANON_AGGR_TYPE_P (type))
18898 {
18899 /* Remove constructors and such from TYPE, now that we
18900 know it is an anonymous aggregate. */
18901 fixup_anonymous_aggr (type);
18902 /* And make the corresponding data member. */
18903 decl = build_decl (decl_spec_token_start->location,
18904 FIELD_DECL, NULL_TREE, type);
18905 /* Add it to the class. */
18906 finish_member_declaration (decl);
18907 }
18908 else
18909 cp_parser_check_access_in_redeclaration
18910 (TYPE_NAME (type),
18911 decl_spec_token_start->location);
18912 }
18913 }
18914 else
18915 {
18916 bool assume_semicolon = false;
18917
18918 /* See if these declarations will be friends. */
18919 friend_p = cp_parser_friend_p (&decl_specifiers);
18920
18921 /* Keep going until we hit the `;' at the end of the
18922 declaration. */
18923 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18924 {
18925 tree attributes = NULL_TREE;
18926 tree first_attribute;
18927
18928 /* Peek at the next token. */
18929 token = cp_lexer_peek_token (parser->lexer);
18930
18931 /* Check for a bitfield declaration. */
18932 if (token->type == CPP_COLON
18933 || (token->type == CPP_NAME
18934 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18935 == CPP_COLON))
18936 {
18937 tree identifier;
18938 tree width;
18939
18940 /* Get the name of the bitfield. Note that we cannot just
18941 check TOKEN here because it may have been invalidated by
18942 the call to cp_lexer_peek_nth_token above. */
18943 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18944 identifier = cp_parser_identifier (parser);
18945 else
18946 identifier = NULL_TREE;
18947
18948 /* Consume the `:' token. */
18949 cp_lexer_consume_token (parser->lexer);
18950 /* Get the width of the bitfield. */
18951 width
18952 = cp_parser_constant_expression (parser,
18953 /*allow_non_constant=*/false,
18954 NULL);
18955
18956 /* Look for attributes that apply to the bitfield. */
18957 attributes = cp_parser_attributes_opt (parser);
18958 /* Remember which attributes are prefix attributes and
18959 which are not. */
18960 first_attribute = attributes;
18961 /* Combine the attributes. */
18962 attributes = chainon (prefix_attributes, attributes);
18963
18964 /* Create the bitfield declaration. */
18965 decl = grokbitfield (identifier
18966 ? make_id_declarator (NULL_TREE,
18967 identifier,
18968 sfk_none)
18969 : NULL,
18970 &decl_specifiers,
18971 width,
18972 attributes);
18973 }
18974 else
18975 {
18976 cp_declarator *declarator;
18977 tree initializer;
18978 tree asm_specification;
18979 int ctor_dtor_or_conv_p;
18980
18981 /* Parse the declarator. */
18982 declarator
18983 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18984 &ctor_dtor_or_conv_p,
18985 /*parenthesized_p=*/NULL,
18986 /*member_p=*/true);
18987
18988 /* If something went wrong parsing the declarator, make sure
18989 that we at least consume some tokens. */
18990 if (declarator == cp_error_declarator)
18991 {
18992 /* Skip to the end of the statement. */
18993 cp_parser_skip_to_end_of_statement (parser);
18994 /* If the next token is not a semicolon, that is
18995 probably because we just skipped over the body of
18996 a function. So, we consume a semicolon if
18997 present, but do not issue an error message if it
18998 is not present. */
18999 if (cp_lexer_next_token_is (parser->lexer,
19000 CPP_SEMICOLON))
19001 cp_lexer_consume_token (parser->lexer);
19002 goto out;
19003 }
19004
19005 if (declares_class_or_enum & 2)
19006 cp_parser_check_for_definition_in_return_type
19007 (declarator, decl_specifiers.type,
19008 decl_specifiers.type_location);
19009
19010 /* Look for an asm-specification. */
19011 asm_specification = cp_parser_asm_specification_opt (parser);
19012 /* Look for attributes that apply to the declaration. */
19013 attributes = cp_parser_attributes_opt (parser);
19014 /* Remember which attributes are prefix attributes and
19015 which are not. */
19016 first_attribute = attributes;
19017 /* Combine the attributes. */
19018 attributes = chainon (prefix_attributes, attributes);
19019
19020 /* If it's an `=', then we have a constant-initializer or a
19021 pure-specifier. It is not correct to parse the
19022 initializer before registering the member declaration
19023 since the member declaration should be in scope while
19024 its initializer is processed. However, the rest of the
19025 front end does not yet provide an interface that allows
19026 us to handle this correctly. */
19027 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19028 {
19029 /* In [class.mem]:
19030
19031 A pure-specifier shall be used only in the declaration of
19032 a virtual function.
19033
19034 A member-declarator can contain a constant-initializer
19035 only if it declares a static member of integral or
19036 enumeration type.
19037
19038 Therefore, if the DECLARATOR is for a function, we look
19039 for a pure-specifier; otherwise, we look for a
19040 constant-initializer. When we call `grokfield', it will
19041 perform more stringent semantics checks. */
19042 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19043 if (function_declarator_p (declarator)
19044 || (decl_specifiers.type
19045 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19046 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19047 == FUNCTION_TYPE)))
19048 initializer = cp_parser_pure_specifier (parser);
19049 else if (decl_specifiers.storage_class != sc_static)
19050 initializer = cp_parser_save_nsdmi (parser);
19051 else if (cxx_dialect >= cxx0x)
19052 {
19053 bool nonconst;
19054 /* Don't require a constant rvalue in C++11, since we
19055 might want a reference constant. We'll enforce
19056 constancy later. */
19057 cp_lexer_consume_token (parser->lexer);
19058 /* Parse the initializer. */
19059 initializer = cp_parser_initializer_clause (parser,
19060 &nonconst);
19061 }
19062 else
19063 /* Parse the initializer. */
19064 initializer = cp_parser_constant_initializer (parser);
19065 }
19066 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19067 && !function_declarator_p (declarator))
19068 {
19069 bool x;
19070 if (decl_specifiers.storage_class != sc_static)
19071 initializer = cp_parser_save_nsdmi (parser);
19072 else
19073 initializer = cp_parser_initializer (parser, &x, &x);
19074 }
19075 /* Otherwise, there is no initializer. */
19076 else
19077 initializer = NULL_TREE;
19078
19079 /* See if we are probably looking at a function
19080 definition. We are certainly not looking at a
19081 member-declarator. Calling `grokfield' has
19082 side-effects, so we must not do it unless we are sure
19083 that we are looking at a member-declarator. */
19084 if (cp_parser_token_starts_function_definition_p
19085 (cp_lexer_peek_token (parser->lexer)))
19086 {
19087 /* The grammar does not allow a pure-specifier to be
19088 used when a member function is defined. (It is
19089 possible that this fact is an oversight in the
19090 standard, since a pure function may be defined
19091 outside of the class-specifier. */
19092 if (initializer)
19093 error_at (initializer_token_start->location,
19094 "pure-specifier on function-definition");
19095 decl = cp_parser_save_member_function_body (parser,
19096 &decl_specifiers,
19097 declarator,
19098 attributes);
19099 /* If the member was not a friend, declare it here. */
19100 if (!friend_p)
19101 finish_member_declaration (decl);
19102 /* Peek at the next token. */
19103 token = cp_lexer_peek_token (parser->lexer);
19104 /* If the next token is a semicolon, consume it. */
19105 if (token->type == CPP_SEMICOLON)
19106 cp_lexer_consume_token (parser->lexer);
19107 goto out;
19108 }
19109 else
19110 if (declarator->kind == cdk_function)
19111 declarator->id_loc = token->location;
19112 /* Create the declaration. */
19113 decl = grokfield (declarator, &decl_specifiers,
19114 initializer, /*init_const_expr_p=*/true,
19115 asm_specification,
19116 attributes);
19117 }
19118
19119 /* Reset PREFIX_ATTRIBUTES. */
19120 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19121 attributes = TREE_CHAIN (attributes);
19122 if (attributes)
19123 TREE_CHAIN (attributes) = NULL_TREE;
19124
19125 /* If there is any qualification still in effect, clear it
19126 now; we will be starting fresh with the next declarator. */
19127 parser->scope = NULL_TREE;
19128 parser->qualifying_scope = NULL_TREE;
19129 parser->object_scope = NULL_TREE;
19130 /* If it's a `,', then there are more declarators. */
19131 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19132 cp_lexer_consume_token (parser->lexer);
19133 /* If the next token isn't a `;', then we have a parse error. */
19134 else if (cp_lexer_next_token_is_not (parser->lexer,
19135 CPP_SEMICOLON))
19136 {
19137 /* The next token might be a ways away from where the
19138 actual semicolon is missing. Find the previous token
19139 and use that for our error position. */
19140 cp_token *token = cp_lexer_previous_token (parser->lexer);
19141 error_at (token->location,
19142 "expected %<;%> at end of member declaration");
19143
19144 /* Assume that the user meant to provide a semicolon. If
19145 we were to cp_parser_skip_to_end_of_statement, we might
19146 skip to a semicolon inside a member function definition
19147 and issue nonsensical error messages. */
19148 assume_semicolon = true;
19149 }
19150
19151 if (decl)
19152 {
19153 /* Add DECL to the list of members. */
19154 if (!friend_p)
19155 finish_member_declaration (decl);
19156
19157 if (TREE_CODE (decl) == FUNCTION_DECL)
19158 cp_parser_save_default_args (parser, decl);
19159 else if (TREE_CODE (decl) == FIELD_DECL
19160 && !DECL_C_BIT_FIELD (decl)
19161 && DECL_INITIAL (decl))
19162 /* Add DECL to the queue of NSDMI to be parsed later. */
19163 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19164 }
19165
19166 if (assume_semicolon)
19167 goto out;
19168 }
19169 }
19170
19171 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19172 out:
19173 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19174 }
19175
19176 /* Parse a pure-specifier.
19177
19178 pure-specifier:
19179 = 0
19180
19181 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19182 Otherwise, ERROR_MARK_NODE is returned. */
19183
19184 static tree
19185 cp_parser_pure_specifier (cp_parser* parser)
19186 {
19187 cp_token *token;
19188
19189 /* Look for the `=' token. */
19190 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19191 return error_mark_node;
19192 /* Look for the `0' token. */
19193 token = cp_lexer_peek_token (parser->lexer);
19194
19195 if (token->type == CPP_EOF
19196 || token->type == CPP_PRAGMA_EOL)
19197 return error_mark_node;
19198
19199 cp_lexer_consume_token (parser->lexer);
19200
19201 /* Accept = default or = delete in c++0x mode. */
19202 if (token->keyword == RID_DEFAULT
19203 || token->keyword == RID_DELETE)
19204 {
19205 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19206 return token->u.value;
19207 }
19208
19209 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19210 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19211 {
19212 cp_parser_error (parser,
19213 "invalid pure specifier (only %<= 0%> is allowed)");
19214 cp_parser_skip_to_end_of_statement (parser);
19215 return error_mark_node;
19216 }
19217 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19218 {
19219 error_at (token->location, "templates may not be %<virtual%>");
19220 return error_mark_node;
19221 }
19222
19223 return integer_zero_node;
19224 }
19225
19226 /* Parse a constant-initializer.
19227
19228 constant-initializer:
19229 = constant-expression
19230
19231 Returns a representation of the constant-expression. */
19232
19233 static tree
19234 cp_parser_constant_initializer (cp_parser* parser)
19235 {
19236 /* Look for the `=' token. */
19237 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19238 return error_mark_node;
19239
19240 /* It is invalid to write:
19241
19242 struct S { static const int i = { 7 }; };
19243
19244 */
19245 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19246 {
19247 cp_parser_error (parser,
19248 "a brace-enclosed initializer is not allowed here");
19249 /* Consume the opening brace. */
19250 cp_lexer_consume_token (parser->lexer);
19251 /* Skip the initializer. */
19252 cp_parser_skip_to_closing_brace (parser);
19253 /* Look for the trailing `}'. */
19254 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19255
19256 return error_mark_node;
19257 }
19258
19259 return cp_parser_constant_expression (parser,
19260 /*allow_non_constant=*/false,
19261 NULL);
19262 }
19263
19264 /* Derived classes [gram.class.derived] */
19265
19266 /* Parse a base-clause.
19267
19268 base-clause:
19269 : base-specifier-list
19270
19271 base-specifier-list:
19272 base-specifier ... [opt]
19273 base-specifier-list , base-specifier ... [opt]
19274
19275 Returns a TREE_LIST representing the base-classes, in the order in
19276 which they were declared. The representation of each node is as
19277 described by cp_parser_base_specifier.
19278
19279 In the case that no bases are specified, this function will return
19280 NULL_TREE, not ERROR_MARK_NODE. */
19281
19282 static tree
19283 cp_parser_base_clause (cp_parser* parser)
19284 {
19285 tree bases = NULL_TREE;
19286
19287 /* Look for the `:' that begins the list. */
19288 cp_parser_require (parser, CPP_COLON, RT_COLON);
19289
19290 /* Scan the base-specifier-list. */
19291 while (true)
19292 {
19293 cp_token *token;
19294 tree base;
19295 bool pack_expansion_p = false;
19296
19297 /* Look for the base-specifier. */
19298 base = cp_parser_base_specifier (parser);
19299 /* Look for the (optional) ellipsis. */
19300 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19301 {
19302 /* Consume the `...'. */
19303 cp_lexer_consume_token (parser->lexer);
19304
19305 pack_expansion_p = true;
19306 }
19307
19308 /* Add BASE to the front of the list. */
19309 if (base && base != error_mark_node)
19310 {
19311 if (pack_expansion_p)
19312 /* Make this a pack expansion type. */
19313 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19314
19315 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19316 {
19317 TREE_CHAIN (base) = bases;
19318 bases = base;
19319 }
19320 }
19321 /* Peek at the next token. */
19322 token = cp_lexer_peek_token (parser->lexer);
19323 /* If it's not a comma, then the list is complete. */
19324 if (token->type != CPP_COMMA)
19325 break;
19326 /* Consume the `,'. */
19327 cp_lexer_consume_token (parser->lexer);
19328 }
19329
19330 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19331 base class had a qualified name. However, the next name that
19332 appears is certainly not qualified. */
19333 parser->scope = NULL_TREE;
19334 parser->qualifying_scope = NULL_TREE;
19335 parser->object_scope = NULL_TREE;
19336
19337 return nreverse (bases);
19338 }
19339
19340 /* Parse a base-specifier.
19341
19342 base-specifier:
19343 :: [opt] nested-name-specifier [opt] class-name
19344 virtual access-specifier [opt] :: [opt] nested-name-specifier
19345 [opt] class-name
19346 access-specifier virtual [opt] :: [opt] nested-name-specifier
19347 [opt] class-name
19348
19349 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19350 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19351 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19352 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19353
19354 static tree
19355 cp_parser_base_specifier (cp_parser* parser)
19356 {
19357 cp_token *token;
19358 bool done = false;
19359 bool virtual_p = false;
19360 bool duplicate_virtual_error_issued_p = false;
19361 bool duplicate_access_error_issued_p = false;
19362 bool class_scope_p, template_p;
19363 tree access = access_default_node;
19364 tree type;
19365
19366 /* Process the optional `virtual' and `access-specifier'. */
19367 while (!done)
19368 {
19369 /* Peek at the next token. */
19370 token = cp_lexer_peek_token (parser->lexer);
19371 /* Process `virtual'. */
19372 switch (token->keyword)
19373 {
19374 case RID_VIRTUAL:
19375 /* If `virtual' appears more than once, issue an error. */
19376 if (virtual_p && !duplicate_virtual_error_issued_p)
19377 {
19378 cp_parser_error (parser,
19379 "%<virtual%> specified more than once in base-specified");
19380 duplicate_virtual_error_issued_p = true;
19381 }
19382
19383 virtual_p = true;
19384
19385 /* Consume the `virtual' token. */
19386 cp_lexer_consume_token (parser->lexer);
19387
19388 break;
19389
19390 case RID_PUBLIC:
19391 case RID_PROTECTED:
19392 case RID_PRIVATE:
19393 /* If more than one access specifier appears, issue an
19394 error. */
19395 if (access != access_default_node
19396 && !duplicate_access_error_issued_p)
19397 {
19398 cp_parser_error (parser,
19399 "more than one access specifier in base-specified");
19400 duplicate_access_error_issued_p = true;
19401 }
19402
19403 access = ridpointers[(int) token->keyword];
19404
19405 /* Consume the access-specifier. */
19406 cp_lexer_consume_token (parser->lexer);
19407
19408 break;
19409
19410 default:
19411 done = true;
19412 break;
19413 }
19414 }
19415 /* It is not uncommon to see programs mechanically, erroneously, use
19416 the 'typename' keyword to denote (dependent) qualified types
19417 as base classes. */
19418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19419 {
19420 token = cp_lexer_peek_token (parser->lexer);
19421 if (!processing_template_decl)
19422 error_at (token->location,
19423 "keyword %<typename%> not allowed outside of templates");
19424 else
19425 error_at (token->location,
19426 "keyword %<typename%> not allowed in this context "
19427 "(the base class is implicitly a type)");
19428 cp_lexer_consume_token (parser->lexer);
19429 }
19430
19431 /* Look for the optional `::' operator. */
19432 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19433 /* Look for the nested-name-specifier. The simplest way to
19434 implement:
19435
19436 [temp.res]
19437
19438 The keyword `typename' is not permitted in a base-specifier or
19439 mem-initializer; in these contexts a qualified name that
19440 depends on a template-parameter is implicitly assumed to be a
19441 type name.
19442
19443 is to pretend that we have seen the `typename' keyword at this
19444 point. */
19445 cp_parser_nested_name_specifier_opt (parser,
19446 /*typename_keyword_p=*/true,
19447 /*check_dependency_p=*/true,
19448 typename_type,
19449 /*is_declaration=*/true);
19450 /* If the base class is given by a qualified name, assume that names
19451 we see are type names or templates, as appropriate. */
19452 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19453 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19454
19455 if (!parser->scope
19456 && cp_lexer_next_token_is_decltype (parser->lexer))
19457 /* DR 950 allows decltype as a base-specifier. */
19458 type = cp_parser_decltype (parser);
19459 else
19460 {
19461 /* Otherwise, look for the class-name. */
19462 type = cp_parser_class_name (parser,
19463 class_scope_p,
19464 template_p,
19465 typename_type,
19466 /*check_dependency_p=*/true,
19467 /*class_head_p=*/false,
19468 /*is_declaration=*/true);
19469 type = TREE_TYPE (type);
19470 }
19471
19472 if (type == error_mark_node)
19473 return error_mark_node;
19474
19475 return finish_base_specifier (type, access, virtual_p);
19476 }
19477
19478 /* Exception handling [gram.exception] */
19479
19480 /* Parse an (optional) exception-specification.
19481
19482 exception-specification:
19483 throw ( type-id-list [opt] )
19484
19485 Returns a TREE_LIST representing the exception-specification. The
19486 TREE_VALUE of each node is a type. */
19487
19488 static tree
19489 cp_parser_exception_specification_opt (cp_parser* parser)
19490 {
19491 cp_token *token;
19492 tree type_id_list;
19493 const char *saved_message;
19494
19495 /* Peek at the next token. */
19496 token = cp_lexer_peek_token (parser->lexer);
19497
19498 /* Is it a noexcept-specification? */
19499 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19500 {
19501 tree expr;
19502 cp_lexer_consume_token (parser->lexer);
19503
19504 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19505 {
19506 cp_lexer_consume_token (parser->lexer);
19507
19508 /* Types may not be defined in an exception-specification. */
19509 saved_message = parser->type_definition_forbidden_message;
19510 parser->type_definition_forbidden_message
19511 = G_("types may not be defined in an exception-specification");
19512
19513 expr = cp_parser_constant_expression (parser, false, NULL);
19514
19515 /* Restore the saved message. */
19516 parser->type_definition_forbidden_message = saved_message;
19517
19518 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19519 }
19520 else
19521 expr = boolean_true_node;
19522
19523 return build_noexcept_spec (expr, tf_warning_or_error);
19524 }
19525
19526 /* If it's not `throw', then there's no exception-specification. */
19527 if (!cp_parser_is_keyword (token, RID_THROW))
19528 return NULL_TREE;
19529
19530 #if 0
19531 /* Enable this once a lot of code has transitioned to noexcept? */
19532 if (cxx_dialect == cxx0x && !in_system_header)
19533 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19534 "deprecated in C++0x; use %<noexcept%> instead");
19535 #endif
19536
19537 /* Consume the `throw'. */
19538 cp_lexer_consume_token (parser->lexer);
19539
19540 /* Look for the `('. */
19541 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19542
19543 /* Peek at the next token. */
19544 token = cp_lexer_peek_token (parser->lexer);
19545 /* If it's not a `)', then there is a type-id-list. */
19546 if (token->type != CPP_CLOSE_PAREN)
19547 {
19548 /* Types may not be defined in an exception-specification. */
19549 saved_message = parser->type_definition_forbidden_message;
19550 parser->type_definition_forbidden_message
19551 = G_("types may not be defined in an exception-specification");
19552 /* Parse the type-id-list. */
19553 type_id_list = cp_parser_type_id_list (parser);
19554 /* Restore the saved message. */
19555 parser->type_definition_forbidden_message = saved_message;
19556 }
19557 else
19558 type_id_list = empty_except_spec;
19559
19560 /* Look for the `)'. */
19561 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19562
19563 return type_id_list;
19564 }
19565
19566 /* Parse an (optional) type-id-list.
19567
19568 type-id-list:
19569 type-id ... [opt]
19570 type-id-list , type-id ... [opt]
19571
19572 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19573 in the order that the types were presented. */
19574
19575 static tree
19576 cp_parser_type_id_list (cp_parser* parser)
19577 {
19578 tree types = NULL_TREE;
19579
19580 while (true)
19581 {
19582 cp_token *token;
19583 tree type;
19584
19585 /* Get the next type-id. */
19586 type = cp_parser_type_id (parser);
19587 /* Parse the optional ellipsis. */
19588 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19589 {
19590 /* Consume the `...'. */
19591 cp_lexer_consume_token (parser->lexer);
19592
19593 /* Turn the type into a pack expansion expression. */
19594 type = make_pack_expansion (type);
19595 }
19596 /* Add it to the list. */
19597 types = add_exception_specifier (types, type, /*complain=*/1);
19598 /* Peek at the next token. */
19599 token = cp_lexer_peek_token (parser->lexer);
19600 /* If it is not a `,', we are done. */
19601 if (token->type != CPP_COMMA)
19602 break;
19603 /* Consume the `,'. */
19604 cp_lexer_consume_token (parser->lexer);
19605 }
19606
19607 return nreverse (types);
19608 }
19609
19610 /* Parse a try-block.
19611
19612 try-block:
19613 try compound-statement handler-seq */
19614
19615 static tree
19616 cp_parser_try_block (cp_parser* parser)
19617 {
19618 tree try_block;
19619
19620 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19621 try_block = begin_try_block ();
19622 cp_parser_compound_statement (parser, NULL, true, false);
19623 finish_try_block (try_block);
19624 cp_parser_handler_seq (parser);
19625 finish_handler_sequence (try_block);
19626
19627 return try_block;
19628 }
19629
19630 /* Parse a function-try-block.
19631
19632 function-try-block:
19633 try ctor-initializer [opt] function-body handler-seq */
19634
19635 static bool
19636 cp_parser_function_try_block (cp_parser* parser)
19637 {
19638 tree compound_stmt;
19639 tree try_block;
19640 bool ctor_initializer_p;
19641
19642 /* Look for the `try' keyword. */
19643 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19644 return false;
19645 /* Let the rest of the front end know where we are. */
19646 try_block = begin_function_try_block (&compound_stmt);
19647 /* Parse the function-body. */
19648 ctor_initializer_p
19649 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19650 /* We're done with the `try' part. */
19651 finish_function_try_block (try_block);
19652 /* Parse the handlers. */
19653 cp_parser_handler_seq (parser);
19654 /* We're done with the handlers. */
19655 finish_function_handler_sequence (try_block, compound_stmt);
19656
19657 return ctor_initializer_p;
19658 }
19659
19660 /* Parse a handler-seq.
19661
19662 handler-seq:
19663 handler handler-seq [opt] */
19664
19665 static void
19666 cp_parser_handler_seq (cp_parser* parser)
19667 {
19668 while (true)
19669 {
19670 cp_token *token;
19671
19672 /* Parse the handler. */
19673 cp_parser_handler (parser);
19674 /* Peek at the next token. */
19675 token = cp_lexer_peek_token (parser->lexer);
19676 /* If it's not `catch' then there are no more handlers. */
19677 if (!cp_parser_is_keyword (token, RID_CATCH))
19678 break;
19679 }
19680 }
19681
19682 /* Parse a handler.
19683
19684 handler:
19685 catch ( exception-declaration ) compound-statement */
19686
19687 static void
19688 cp_parser_handler (cp_parser* parser)
19689 {
19690 tree handler;
19691 tree declaration;
19692
19693 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19694 handler = begin_handler ();
19695 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19696 declaration = cp_parser_exception_declaration (parser);
19697 finish_handler_parms (declaration, handler);
19698 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19699 cp_parser_compound_statement (parser, NULL, false, false);
19700 finish_handler (handler);
19701 }
19702
19703 /* Parse an exception-declaration.
19704
19705 exception-declaration:
19706 type-specifier-seq declarator
19707 type-specifier-seq abstract-declarator
19708 type-specifier-seq
19709 ...
19710
19711 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19712 ellipsis variant is used. */
19713
19714 static tree
19715 cp_parser_exception_declaration (cp_parser* parser)
19716 {
19717 cp_decl_specifier_seq type_specifiers;
19718 cp_declarator *declarator;
19719 const char *saved_message;
19720
19721 /* If it's an ellipsis, it's easy to handle. */
19722 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19723 {
19724 /* Consume the `...' token. */
19725 cp_lexer_consume_token (parser->lexer);
19726 return NULL_TREE;
19727 }
19728
19729 /* Types may not be defined in exception-declarations. */
19730 saved_message = parser->type_definition_forbidden_message;
19731 parser->type_definition_forbidden_message
19732 = G_("types may not be defined in exception-declarations");
19733
19734 /* Parse the type-specifier-seq. */
19735 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19736 /*is_trailing_return=*/false,
19737 &type_specifiers);
19738 /* If it's a `)', then there is no declarator. */
19739 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19740 declarator = NULL;
19741 else
19742 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19743 /*ctor_dtor_or_conv_p=*/NULL,
19744 /*parenthesized_p=*/NULL,
19745 /*member_p=*/false);
19746
19747 /* Restore the saved message. */
19748 parser->type_definition_forbidden_message = saved_message;
19749
19750 if (!type_specifiers.any_specifiers_p)
19751 return error_mark_node;
19752
19753 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19754 }
19755
19756 /* Parse a throw-expression.
19757
19758 throw-expression:
19759 throw assignment-expression [opt]
19760
19761 Returns a THROW_EXPR representing the throw-expression. */
19762
19763 static tree
19764 cp_parser_throw_expression (cp_parser* parser)
19765 {
19766 tree expression;
19767 cp_token* token;
19768
19769 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19770 token = cp_lexer_peek_token (parser->lexer);
19771 /* Figure out whether or not there is an assignment-expression
19772 following the "throw" keyword. */
19773 if (token->type == CPP_COMMA
19774 || token->type == CPP_SEMICOLON
19775 || token->type == CPP_CLOSE_PAREN
19776 || token->type == CPP_CLOSE_SQUARE
19777 || token->type == CPP_CLOSE_BRACE
19778 || token->type == CPP_COLON)
19779 expression = NULL_TREE;
19780 else
19781 expression = cp_parser_assignment_expression (parser,
19782 /*cast_p=*/false, NULL);
19783
19784 return build_throw (expression);
19785 }
19786
19787 /* GNU Extensions */
19788
19789 /* Parse an (optional) asm-specification.
19790
19791 asm-specification:
19792 asm ( string-literal )
19793
19794 If the asm-specification is present, returns a STRING_CST
19795 corresponding to the string-literal. Otherwise, returns
19796 NULL_TREE. */
19797
19798 static tree
19799 cp_parser_asm_specification_opt (cp_parser* parser)
19800 {
19801 cp_token *token;
19802 tree asm_specification;
19803
19804 /* Peek at the next token. */
19805 token = cp_lexer_peek_token (parser->lexer);
19806 /* If the next token isn't the `asm' keyword, then there's no
19807 asm-specification. */
19808 if (!cp_parser_is_keyword (token, RID_ASM))
19809 return NULL_TREE;
19810
19811 /* Consume the `asm' token. */
19812 cp_lexer_consume_token (parser->lexer);
19813 /* Look for the `('. */
19814 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19815
19816 /* Look for the string-literal. */
19817 asm_specification = cp_parser_string_literal (parser, false, false);
19818
19819 /* Look for the `)'. */
19820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19821
19822 return asm_specification;
19823 }
19824
19825 /* Parse an asm-operand-list.
19826
19827 asm-operand-list:
19828 asm-operand
19829 asm-operand-list , asm-operand
19830
19831 asm-operand:
19832 string-literal ( expression )
19833 [ string-literal ] string-literal ( expression )
19834
19835 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19836 each node is the expression. The TREE_PURPOSE is itself a
19837 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19838 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19839 is a STRING_CST for the string literal before the parenthesis. Returns
19840 ERROR_MARK_NODE if any of the operands are invalid. */
19841
19842 static tree
19843 cp_parser_asm_operand_list (cp_parser* parser)
19844 {
19845 tree asm_operands = NULL_TREE;
19846 bool invalid_operands = false;
19847
19848 while (true)
19849 {
19850 tree string_literal;
19851 tree expression;
19852 tree name;
19853
19854 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19855 {
19856 /* Consume the `[' token. */
19857 cp_lexer_consume_token (parser->lexer);
19858 /* Read the operand name. */
19859 name = cp_parser_identifier (parser);
19860 if (name != error_mark_node)
19861 name = build_string (IDENTIFIER_LENGTH (name),
19862 IDENTIFIER_POINTER (name));
19863 /* Look for the closing `]'. */
19864 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19865 }
19866 else
19867 name = NULL_TREE;
19868 /* Look for the string-literal. */
19869 string_literal = cp_parser_string_literal (parser, false, false);
19870
19871 /* Look for the `('. */
19872 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19873 /* Parse the expression. */
19874 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19875 /* Look for the `)'. */
19876 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19877
19878 if (name == error_mark_node
19879 || string_literal == error_mark_node
19880 || expression == error_mark_node)
19881 invalid_operands = true;
19882
19883 /* Add this operand to the list. */
19884 asm_operands = tree_cons (build_tree_list (name, string_literal),
19885 expression,
19886 asm_operands);
19887 /* If the next token is not a `,', there are no more
19888 operands. */
19889 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19890 break;
19891 /* Consume the `,'. */
19892 cp_lexer_consume_token (parser->lexer);
19893 }
19894
19895 return invalid_operands ? error_mark_node : nreverse (asm_operands);
19896 }
19897
19898 /* Parse an asm-clobber-list.
19899
19900 asm-clobber-list:
19901 string-literal
19902 asm-clobber-list , string-literal
19903
19904 Returns a TREE_LIST, indicating the clobbers in the order that they
19905 appeared. The TREE_VALUE of each node is a STRING_CST. */
19906
19907 static tree
19908 cp_parser_asm_clobber_list (cp_parser* parser)
19909 {
19910 tree clobbers = NULL_TREE;
19911
19912 while (true)
19913 {
19914 tree string_literal;
19915
19916 /* Look for the string literal. */
19917 string_literal = cp_parser_string_literal (parser, false, false);
19918 /* Add it to the list. */
19919 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19920 /* If the next token is not a `,', then the list is
19921 complete. */
19922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19923 break;
19924 /* Consume the `,' token. */
19925 cp_lexer_consume_token (parser->lexer);
19926 }
19927
19928 return clobbers;
19929 }
19930
19931 /* Parse an asm-label-list.
19932
19933 asm-label-list:
19934 identifier
19935 asm-label-list , identifier
19936
19937 Returns a TREE_LIST, indicating the labels in the order that they
19938 appeared. The TREE_VALUE of each node is a label. */
19939
19940 static tree
19941 cp_parser_asm_label_list (cp_parser* parser)
19942 {
19943 tree labels = NULL_TREE;
19944
19945 while (true)
19946 {
19947 tree identifier, label, name;
19948
19949 /* Look for the identifier. */
19950 identifier = cp_parser_identifier (parser);
19951 if (!error_operand_p (identifier))
19952 {
19953 label = lookup_label (identifier);
19954 if (TREE_CODE (label) == LABEL_DECL)
19955 {
19956 TREE_USED (label) = 1;
19957 check_goto (label);
19958 name = build_string (IDENTIFIER_LENGTH (identifier),
19959 IDENTIFIER_POINTER (identifier));
19960 labels = tree_cons (name, label, labels);
19961 }
19962 }
19963 /* If the next token is not a `,', then the list is
19964 complete. */
19965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19966 break;
19967 /* Consume the `,' token. */
19968 cp_lexer_consume_token (parser->lexer);
19969 }
19970
19971 return nreverse (labels);
19972 }
19973
19974 /* Parse an (optional) series of attributes.
19975
19976 attributes:
19977 attributes attribute
19978
19979 attribute:
19980 __attribute__ (( attribute-list [opt] ))
19981
19982 The return value is as for cp_parser_attribute_list. */
19983
19984 static tree
19985 cp_parser_attributes_opt (cp_parser* parser)
19986 {
19987 tree attributes = NULL_TREE;
19988
19989 while (true)
19990 {
19991 cp_token *token;
19992 tree attribute_list;
19993
19994 /* Peek at the next token. */
19995 token = cp_lexer_peek_token (parser->lexer);
19996 /* If it's not `__attribute__', then we're done. */
19997 if (token->keyword != RID_ATTRIBUTE)
19998 break;
19999
20000 /* Consume the `__attribute__' keyword. */
20001 cp_lexer_consume_token (parser->lexer);
20002 /* Look for the two `(' tokens. */
20003 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20004 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20005
20006 /* Peek at the next token. */
20007 token = cp_lexer_peek_token (parser->lexer);
20008 if (token->type != CPP_CLOSE_PAREN)
20009 /* Parse the attribute-list. */
20010 attribute_list = cp_parser_attribute_list (parser);
20011 else
20012 /* If the next token is a `)', then there is no attribute
20013 list. */
20014 attribute_list = NULL;
20015
20016 /* Look for the two `)' tokens. */
20017 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20018 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20019
20020 /* Add these new attributes to the list. */
20021 attributes = chainon (attributes, attribute_list);
20022 }
20023
20024 return attributes;
20025 }
20026
20027 /* Parse an attribute-list.
20028
20029 attribute-list:
20030 attribute
20031 attribute-list , attribute
20032
20033 attribute:
20034 identifier
20035 identifier ( identifier )
20036 identifier ( identifier , expression-list )
20037 identifier ( expression-list )
20038
20039 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20040 to an attribute. The TREE_PURPOSE of each node is the identifier
20041 indicating which attribute is in use. The TREE_VALUE represents
20042 the arguments, if any. */
20043
20044 static tree
20045 cp_parser_attribute_list (cp_parser* parser)
20046 {
20047 tree attribute_list = NULL_TREE;
20048 bool save_translate_strings_p = parser->translate_strings_p;
20049
20050 parser->translate_strings_p = false;
20051 while (true)
20052 {
20053 cp_token *token;
20054 tree identifier;
20055 tree attribute;
20056
20057 /* Look for the identifier. We also allow keywords here; for
20058 example `__attribute__ ((const))' is legal. */
20059 token = cp_lexer_peek_token (parser->lexer);
20060 if (token->type == CPP_NAME
20061 || token->type == CPP_KEYWORD)
20062 {
20063 tree arguments = NULL_TREE;
20064
20065 /* Consume the token. */
20066 token = cp_lexer_consume_token (parser->lexer);
20067
20068 /* Save away the identifier that indicates which attribute
20069 this is. */
20070 identifier = (token->type == CPP_KEYWORD)
20071 /* For keywords, use the canonical spelling, not the
20072 parsed identifier. */
20073 ? ridpointers[(int) token->keyword]
20074 : token->u.value;
20075
20076 attribute = build_tree_list (identifier, NULL_TREE);
20077
20078 /* Peek at the next token. */
20079 token = cp_lexer_peek_token (parser->lexer);
20080 /* If it's an `(', then parse the attribute arguments. */
20081 if (token->type == CPP_OPEN_PAREN)
20082 {
20083 VEC(tree,gc) *vec;
20084 int attr_flag = (attribute_takes_identifier_p (identifier)
20085 ? id_attr : normal_attr);
20086 vec = cp_parser_parenthesized_expression_list
20087 (parser, attr_flag, /*cast_p=*/false,
20088 /*allow_expansion_p=*/false,
20089 /*non_constant_p=*/NULL);
20090 if (vec == NULL)
20091 arguments = error_mark_node;
20092 else
20093 {
20094 arguments = build_tree_list_vec (vec);
20095 release_tree_vector (vec);
20096 }
20097 /* Save the arguments away. */
20098 TREE_VALUE (attribute) = arguments;
20099 }
20100
20101 if (arguments != error_mark_node)
20102 {
20103 /* Add this attribute to the list. */
20104 TREE_CHAIN (attribute) = attribute_list;
20105 attribute_list = attribute;
20106 }
20107
20108 token = cp_lexer_peek_token (parser->lexer);
20109 }
20110 /* Now, look for more attributes. If the next token isn't a
20111 `,', we're done. */
20112 if (token->type != CPP_COMMA)
20113 break;
20114
20115 /* Consume the comma and keep going. */
20116 cp_lexer_consume_token (parser->lexer);
20117 }
20118 parser->translate_strings_p = save_translate_strings_p;
20119
20120 /* We built up the list in reverse order. */
20121 return nreverse (attribute_list);
20122 }
20123
20124 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20125 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20126 current value of the PEDANTIC flag, regardless of whether or not
20127 the `__extension__' keyword is present. The caller is responsible
20128 for restoring the value of the PEDANTIC flag. */
20129
20130 static bool
20131 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20132 {
20133 /* Save the old value of the PEDANTIC flag. */
20134 *saved_pedantic = pedantic;
20135
20136 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20137 {
20138 /* Consume the `__extension__' token. */
20139 cp_lexer_consume_token (parser->lexer);
20140 /* We're not being pedantic while the `__extension__' keyword is
20141 in effect. */
20142 pedantic = 0;
20143
20144 return true;
20145 }
20146
20147 return false;
20148 }
20149
20150 /* Parse a label declaration.
20151
20152 label-declaration:
20153 __label__ label-declarator-seq ;
20154
20155 label-declarator-seq:
20156 identifier , label-declarator-seq
20157 identifier */
20158
20159 static void
20160 cp_parser_label_declaration (cp_parser* parser)
20161 {
20162 /* Look for the `__label__' keyword. */
20163 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20164
20165 while (true)
20166 {
20167 tree identifier;
20168
20169 /* Look for an identifier. */
20170 identifier = cp_parser_identifier (parser);
20171 /* If we failed, stop. */
20172 if (identifier == error_mark_node)
20173 break;
20174 /* Declare it as a label. */
20175 finish_label_decl (identifier);
20176 /* If the next token is a `;', stop. */
20177 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20178 break;
20179 /* Look for the `,' separating the label declarations. */
20180 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20181 }
20182
20183 /* Look for the final `;'. */
20184 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20185 }
20186
20187 /* Support Functions */
20188
20189 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20190 NAME should have one of the representations used for an
20191 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20192 is returned. If PARSER->SCOPE is a dependent type, then a
20193 SCOPE_REF is returned.
20194
20195 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20196 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20197 was formed. Abstractly, such entities should not be passed to this
20198 function, because they do not need to be looked up, but it is
20199 simpler to check for this special case here, rather than at the
20200 call-sites.
20201
20202 In cases not explicitly covered above, this function returns a
20203 DECL, OVERLOAD, or baselink representing the result of the lookup.
20204 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20205 is returned.
20206
20207 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20208 (e.g., "struct") that was used. In that case bindings that do not
20209 refer to types are ignored.
20210
20211 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20212 ignored.
20213
20214 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20215 are ignored.
20216
20217 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20218 types.
20219
20220 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20221 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20222 NULL_TREE otherwise. */
20223
20224 static tree
20225 cp_parser_lookup_name (cp_parser *parser, tree name,
20226 enum tag_types tag_type,
20227 bool is_template,
20228 bool is_namespace,
20229 bool check_dependency,
20230 tree *ambiguous_decls,
20231 location_t name_location)
20232 {
20233 int flags = 0;
20234 tree decl;
20235 tree object_type = parser->context->object_type;
20236
20237 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20238 flags |= LOOKUP_COMPLAIN;
20239
20240 /* Assume that the lookup will be unambiguous. */
20241 if (ambiguous_decls)
20242 *ambiguous_decls = NULL_TREE;
20243
20244 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20245 no longer valid. Note that if we are parsing tentatively, and
20246 the parse fails, OBJECT_TYPE will be automatically restored. */
20247 parser->context->object_type = NULL_TREE;
20248
20249 if (name == error_mark_node)
20250 return error_mark_node;
20251
20252 /* A template-id has already been resolved; there is no lookup to
20253 do. */
20254 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20255 return name;
20256 if (BASELINK_P (name))
20257 {
20258 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20259 == TEMPLATE_ID_EXPR);
20260 return name;
20261 }
20262
20263 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20264 it should already have been checked to make sure that the name
20265 used matches the type being destroyed. */
20266 if (TREE_CODE (name) == BIT_NOT_EXPR)
20267 {
20268 tree type;
20269
20270 /* Figure out to which type this destructor applies. */
20271 if (parser->scope)
20272 type = parser->scope;
20273 else if (object_type)
20274 type = object_type;
20275 else
20276 type = current_class_type;
20277 /* If that's not a class type, there is no destructor. */
20278 if (!type || !CLASS_TYPE_P (type))
20279 return error_mark_node;
20280 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20281 lazily_declare_fn (sfk_destructor, type);
20282 if (!CLASSTYPE_DESTRUCTORS (type))
20283 return error_mark_node;
20284 /* If it was a class type, return the destructor. */
20285 return CLASSTYPE_DESTRUCTORS (type);
20286 }
20287
20288 /* By this point, the NAME should be an ordinary identifier. If
20289 the id-expression was a qualified name, the qualifying scope is
20290 stored in PARSER->SCOPE at this point. */
20291 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20292
20293 /* Perform the lookup. */
20294 if (parser->scope)
20295 {
20296 bool dependent_p;
20297
20298 if (parser->scope == error_mark_node)
20299 return error_mark_node;
20300
20301 /* If the SCOPE is dependent, the lookup must be deferred until
20302 the template is instantiated -- unless we are explicitly
20303 looking up names in uninstantiated templates. Even then, we
20304 cannot look up the name if the scope is not a class type; it
20305 might, for example, be a template type parameter. */
20306 dependent_p = (TYPE_P (parser->scope)
20307 && dependent_scope_p (parser->scope));
20308 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20309 && dependent_p)
20310 /* Defer lookup. */
20311 decl = error_mark_node;
20312 else
20313 {
20314 tree pushed_scope = NULL_TREE;
20315
20316 /* If PARSER->SCOPE is a dependent type, then it must be a
20317 class type, and we must not be checking dependencies;
20318 otherwise, we would have processed this lookup above. So
20319 that PARSER->SCOPE is not considered a dependent base by
20320 lookup_member, we must enter the scope here. */
20321 if (dependent_p)
20322 pushed_scope = push_scope (parser->scope);
20323
20324 /* If the PARSER->SCOPE is a template specialization, it
20325 may be instantiated during name lookup. In that case,
20326 errors may be issued. Even if we rollback the current
20327 tentative parse, those errors are valid. */
20328 decl = lookup_qualified_name (parser->scope, name,
20329 tag_type != none_type,
20330 /*complain=*/true);
20331
20332 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20333 lookup result and the nested-name-specifier nominates a class C:
20334 * if the name specified after the nested-name-specifier, when
20335 looked up in C, is the injected-class-name of C (Clause 9), or
20336 * if the name specified after the nested-name-specifier is the
20337 same as the identifier or the simple-template-id's template-
20338 name in the last component of the nested-name-specifier,
20339 the name is instead considered to name the constructor of
20340 class C. [ Note: for example, the constructor is not an
20341 acceptable lookup result in an elaborated-type-specifier so
20342 the constructor would not be used in place of the
20343 injected-class-name. --end note ] Such a constructor name
20344 shall be used only in the declarator-id of a declaration that
20345 names a constructor or in a using-declaration. */
20346 if (tag_type == none_type
20347 && DECL_SELF_REFERENCE_P (decl)
20348 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20349 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20350 tag_type != none_type,
20351 /*complain=*/true);
20352
20353 /* If we have a single function from a using decl, pull it out. */
20354 if (TREE_CODE (decl) == OVERLOAD
20355 && !really_overloaded_fn (decl))
20356 decl = OVL_FUNCTION (decl);
20357
20358 if (pushed_scope)
20359 pop_scope (pushed_scope);
20360 }
20361
20362 /* If the scope is a dependent type and either we deferred lookup or
20363 we did lookup but didn't find the name, rememeber the name. */
20364 if (decl == error_mark_node && TYPE_P (parser->scope)
20365 && dependent_type_p (parser->scope))
20366 {
20367 if (tag_type)
20368 {
20369 tree type;
20370
20371 /* The resolution to Core Issue 180 says that `struct
20372 A::B' should be considered a type-name, even if `A'
20373 is dependent. */
20374 type = make_typename_type (parser->scope, name, tag_type,
20375 /*complain=*/tf_error);
20376 decl = TYPE_NAME (type);
20377 }
20378 else if (is_template
20379 && (cp_parser_next_token_ends_template_argument_p (parser)
20380 || cp_lexer_next_token_is (parser->lexer,
20381 CPP_CLOSE_PAREN)))
20382 decl = make_unbound_class_template (parser->scope,
20383 name, NULL_TREE,
20384 /*complain=*/tf_error);
20385 else
20386 decl = build_qualified_name (/*type=*/NULL_TREE,
20387 parser->scope, name,
20388 is_template);
20389 }
20390 parser->qualifying_scope = parser->scope;
20391 parser->object_scope = NULL_TREE;
20392 }
20393 else if (object_type)
20394 {
20395 tree object_decl = NULL_TREE;
20396 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20397 OBJECT_TYPE is not a class. */
20398 if (CLASS_TYPE_P (object_type))
20399 /* If the OBJECT_TYPE is a template specialization, it may
20400 be instantiated during name lookup. In that case, errors
20401 may be issued. Even if we rollback the current tentative
20402 parse, those errors are valid. */
20403 object_decl = lookup_member (object_type,
20404 name,
20405 /*protect=*/0,
20406 tag_type != none_type,
20407 tf_warning_or_error);
20408 /* Look it up in the enclosing context, too. */
20409 decl = lookup_name_real (name, tag_type != none_type,
20410 /*nonclass=*/0,
20411 /*block_p=*/true, is_namespace, flags);
20412 parser->object_scope = object_type;
20413 parser->qualifying_scope = NULL_TREE;
20414 if (object_decl)
20415 decl = object_decl;
20416 }
20417 else
20418 {
20419 decl = lookup_name_real (name, tag_type != none_type,
20420 /*nonclass=*/0,
20421 /*block_p=*/true, is_namespace, flags);
20422 parser->qualifying_scope = NULL_TREE;
20423 parser->object_scope = NULL_TREE;
20424 }
20425
20426 /* If the lookup failed, let our caller know. */
20427 if (!decl || decl == error_mark_node)
20428 return error_mark_node;
20429
20430 /* Pull out the template from an injected-class-name (or multiple). */
20431 if (is_template)
20432 decl = maybe_get_template_decl_from_type_decl (decl);
20433
20434 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20435 if (TREE_CODE (decl) == TREE_LIST)
20436 {
20437 if (ambiguous_decls)
20438 *ambiguous_decls = decl;
20439 /* The error message we have to print is too complicated for
20440 cp_parser_error, so we incorporate its actions directly. */
20441 if (!cp_parser_simulate_error (parser))
20442 {
20443 error_at (name_location, "reference to %qD is ambiguous",
20444 name);
20445 print_candidates (decl);
20446 }
20447 return error_mark_node;
20448 }
20449
20450 gcc_assert (DECL_P (decl)
20451 || TREE_CODE (decl) == OVERLOAD
20452 || TREE_CODE (decl) == SCOPE_REF
20453 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20454 || BASELINK_P (decl));
20455
20456 /* If we have resolved the name of a member declaration, check to
20457 see if the declaration is accessible. When the name resolves to
20458 set of overloaded functions, accessibility is checked when
20459 overload resolution is done.
20460
20461 During an explicit instantiation, access is not checked at all,
20462 as per [temp.explicit]. */
20463 if (DECL_P (decl))
20464 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20465
20466 maybe_record_typedef_use (decl);
20467
20468 return decl;
20469 }
20470
20471 /* Like cp_parser_lookup_name, but for use in the typical case where
20472 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20473 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20474
20475 static tree
20476 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20477 {
20478 return cp_parser_lookup_name (parser, name,
20479 none_type,
20480 /*is_template=*/false,
20481 /*is_namespace=*/false,
20482 /*check_dependency=*/true,
20483 /*ambiguous_decls=*/NULL,
20484 location);
20485 }
20486
20487 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20488 the current context, return the TYPE_DECL. If TAG_NAME_P is
20489 true, the DECL indicates the class being defined in a class-head,
20490 or declared in an elaborated-type-specifier.
20491
20492 Otherwise, return DECL. */
20493
20494 static tree
20495 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20496 {
20497 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20498 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20499
20500 struct A {
20501 template <typename T> struct B;
20502 };
20503
20504 template <typename T> struct A::B {};
20505
20506 Similarly, in an elaborated-type-specifier:
20507
20508 namespace N { struct X{}; }
20509
20510 struct A {
20511 template <typename T> friend struct N::X;
20512 };
20513
20514 However, if the DECL refers to a class type, and we are in
20515 the scope of the class, then the name lookup automatically
20516 finds the TYPE_DECL created by build_self_reference rather
20517 than a TEMPLATE_DECL. For example, in:
20518
20519 template <class T> struct S {
20520 S s;
20521 };
20522
20523 there is no need to handle such case. */
20524
20525 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20526 return DECL_TEMPLATE_RESULT (decl);
20527
20528 return decl;
20529 }
20530
20531 /* If too many, or too few, template-parameter lists apply to the
20532 declarator, issue an error message. Returns TRUE if all went well,
20533 and FALSE otherwise. */
20534
20535 static bool
20536 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20537 cp_declarator *declarator,
20538 location_t declarator_location)
20539 {
20540 unsigned num_templates;
20541
20542 /* We haven't seen any classes that involve template parameters yet. */
20543 num_templates = 0;
20544
20545 switch (declarator->kind)
20546 {
20547 case cdk_id:
20548 if (declarator->u.id.qualifying_scope)
20549 {
20550 tree scope;
20551
20552 scope = declarator->u.id.qualifying_scope;
20553
20554 while (scope && CLASS_TYPE_P (scope))
20555 {
20556 /* You're supposed to have one `template <...>'
20557 for every template class, but you don't need one
20558 for a full specialization. For example:
20559
20560 template <class T> struct S{};
20561 template <> struct S<int> { void f(); };
20562 void S<int>::f () {}
20563
20564 is correct; there shouldn't be a `template <>' for
20565 the definition of `S<int>::f'. */
20566 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20567 /* If SCOPE does not have template information of any
20568 kind, then it is not a template, nor is it nested
20569 within a template. */
20570 break;
20571 if (explicit_class_specialization_p (scope))
20572 break;
20573 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20574 ++num_templates;
20575
20576 scope = TYPE_CONTEXT (scope);
20577 }
20578 }
20579 else if (TREE_CODE (declarator->u.id.unqualified_name)
20580 == TEMPLATE_ID_EXPR)
20581 /* If the DECLARATOR has the form `X<y>' then it uses one
20582 additional level of template parameters. */
20583 ++num_templates;
20584
20585 return cp_parser_check_template_parameters
20586 (parser, num_templates, declarator_location, declarator);
20587
20588
20589 case cdk_function:
20590 case cdk_array:
20591 case cdk_pointer:
20592 case cdk_reference:
20593 case cdk_ptrmem:
20594 return (cp_parser_check_declarator_template_parameters
20595 (parser, declarator->declarator, declarator_location));
20596
20597 case cdk_error:
20598 return true;
20599
20600 default:
20601 gcc_unreachable ();
20602 }
20603 return false;
20604 }
20605
20606 /* NUM_TEMPLATES were used in the current declaration. If that is
20607 invalid, return FALSE and issue an error messages. Otherwise,
20608 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20609 declarator and we can print more accurate diagnostics. */
20610
20611 static bool
20612 cp_parser_check_template_parameters (cp_parser* parser,
20613 unsigned num_templates,
20614 location_t location,
20615 cp_declarator *declarator)
20616 {
20617 /* If there are the same number of template classes and parameter
20618 lists, that's OK. */
20619 if (parser->num_template_parameter_lists == num_templates)
20620 return true;
20621 /* If there are more, but only one more, then we are referring to a
20622 member template. That's OK too. */
20623 if (parser->num_template_parameter_lists == num_templates + 1)
20624 return true;
20625 /* If there are more template classes than parameter lists, we have
20626 something like:
20627
20628 template <class T> void S<T>::R<T>::f (); */
20629 if (parser->num_template_parameter_lists < num_templates)
20630 {
20631 if (declarator && !current_function_decl)
20632 error_at (location, "specializing member %<%T::%E%> "
20633 "requires %<template<>%> syntax",
20634 declarator->u.id.qualifying_scope,
20635 declarator->u.id.unqualified_name);
20636 else if (declarator)
20637 error_at (location, "invalid declaration of %<%T::%E%>",
20638 declarator->u.id.qualifying_scope,
20639 declarator->u.id.unqualified_name);
20640 else
20641 error_at (location, "too few template-parameter-lists");
20642 return false;
20643 }
20644 /* Otherwise, there are too many template parameter lists. We have
20645 something like:
20646
20647 template <class T> template <class U> void S::f(); */
20648 error_at (location, "too many template-parameter-lists");
20649 return false;
20650 }
20651
20652 /* Parse an optional `::' token indicating that the following name is
20653 from the global namespace. If so, PARSER->SCOPE is set to the
20654 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20655 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20656 Returns the new value of PARSER->SCOPE, if the `::' token is
20657 present, and NULL_TREE otherwise. */
20658
20659 static tree
20660 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20661 {
20662 cp_token *token;
20663
20664 /* Peek at the next token. */
20665 token = cp_lexer_peek_token (parser->lexer);
20666 /* If we're looking at a `::' token then we're starting from the
20667 global namespace, not our current location. */
20668 if (token->type == CPP_SCOPE)
20669 {
20670 /* Consume the `::' token. */
20671 cp_lexer_consume_token (parser->lexer);
20672 /* Set the SCOPE so that we know where to start the lookup. */
20673 parser->scope = global_namespace;
20674 parser->qualifying_scope = global_namespace;
20675 parser->object_scope = NULL_TREE;
20676
20677 return parser->scope;
20678 }
20679 else if (!current_scope_valid_p)
20680 {
20681 parser->scope = NULL_TREE;
20682 parser->qualifying_scope = NULL_TREE;
20683 parser->object_scope = NULL_TREE;
20684 }
20685
20686 return NULL_TREE;
20687 }
20688
20689 /* Returns TRUE if the upcoming token sequence is the start of a
20690 constructor declarator. If FRIEND_P is true, the declarator is
20691 preceded by the `friend' specifier. */
20692
20693 static bool
20694 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20695 {
20696 bool constructor_p;
20697 tree nested_name_specifier;
20698 cp_token *next_token;
20699
20700 /* The common case is that this is not a constructor declarator, so
20701 try to avoid doing lots of work if at all possible. It's not
20702 valid declare a constructor at function scope. */
20703 if (parser->in_function_body)
20704 return false;
20705 /* And only certain tokens can begin a constructor declarator. */
20706 next_token = cp_lexer_peek_token (parser->lexer);
20707 if (next_token->type != CPP_NAME
20708 && next_token->type != CPP_SCOPE
20709 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20710 && next_token->type != CPP_TEMPLATE_ID)
20711 return false;
20712
20713 /* Parse tentatively; we are going to roll back all of the tokens
20714 consumed here. */
20715 cp_parser_parse_tentatively (parser);
20716 /* Assume that we are looking at a constructor declarator. */
20717 constructor_p = true;
20718
20719 /* Look for the optional `::' operator. */
20720 cp_parser_global_scope_opt (parser,
20721 /*current_scope_valid_p=*/false);
20722 /* Look for the nested-name-specifier. */
20723 nested_name_specifier
20724 = (cp_parser_nested_name_specifier_opt (parser,
20725 /*typename_keyword_p=*/false,
20726 /*check_dependency_p=*/false,
20727 /*type_p=*/false,
20728 /*is_declaration=*/false));
20729 /* Outside of a class-specifier, there must be a
20730 nested-name-specifier. */
20731 if (!nested_name_specifier &&
20732 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20733 || friend_p))
20734 constructor_p = false;
20735 else if (nested_name_specifier == error_mark_node)
20736 constructor_p = false;
20737
20738 /* If we have a class scope, this is easy; DR 147 says that S::S always
20739 names the constructor, and no other qualified name could. */
20740 if (constructor_p && nested_name_specifier
20741 && CLASS_TYPE_P (nested_name_specifier))
20742 {
20743 tree id = cp_parser_unqualified_id (parser,
20744 /*template_keyword_p=*/false,
20745 /*check_dependency_p=*/false,
20746 /*declarator_p=*/true,
20747 /*optional_p=*/false);
20748 if (is_overloaded_fn (id))
20749 id = DECL_NAME (get_first_fn (id));
20750 if (!constructor_name_p (id, nested_name_specifier))
20751 constructor_p = false;
20752 }
20753 /* If we still think that this might be a constructor-declarator,
20754 look for a class-name. */
20755 else if (constructor_p)
20756 {
20757 /* If we have:
20758
20759 template <typename T> struct S {
20760 S();
20761 };
20762
20763 we must recognize that the nested `S' names a class. */
20764 tree type_decl;
20765 type_decl = cp_parser_class_name (parser,
20766 /*typename_keyword_p=*/false,
20767 /*template_keyword_p=*/false,
20768 none_type,
20769 /*check_dependency_p=*/false,
20770 /*class_head_p=*/false,
20771 /*is_declaration=*/false);
20772 /* If there was no class-name, then this is not a constructor. */
20773 constructor_p = !cp_parser_error_occurred (parser);
20774
20775 /* If we're still considering a constructor, we have to see a `(',
20776 to begin the parameter-declaration-clause, followed by either a
20777 `)', an `...', or a decl-specifier. We need to check for a
20778 type-specifier to avoid being fooled into thinking that:
20779
20780 S (f) (int);
20781
20782 is a constructor. (It is actually a function named `f' that
20783 takes one parameter (of type `int') and returns a value of type
20784 `S'. */
20785 if (constructor_p
20786 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20787 constructor_p = false;
20788
20789 if (constructor_p
20790 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20791 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20792 /* A parameter declaration begins with a decl-specifier,
20793 which is either the "attribute" keyword, a storage class
20794 specifier, or (usually) a type-specifier. */
20795 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20796 {
20797 tree type;
20798 tree pushed_scope = NULL_TREE;
20799 unsigned saved_num_template_parameter_lists;
20800
20801 /* Names appearing in the type-specifier should be looked up
20802 in the scope of the class. */
20803 if (current_class_type)
20804 type = NULL_TREE;
20805 else
20806 {
20807 type = TREE_TYPE (type_decl);
20808 if (TREE_CODE (type) == TYPENAME_TYPE)
20809 {
20810 type = resolve_typename_type (type,
20811 /*only_current_p=*/false);
20812 if (TREE_CODE (type) == TYPENAME_TYPE)
20813 {
20814 cp_parser_abort_tentative_parse (parser);
20815 return false;
20816 }
20817 }
20818 pushed_scope = push_scope (type);
20819 }
20820
20821 /* Inside the constructor parameter list, surrounding
20822 template-parameter-lists do not apply. */
20823 saved_num_template_parameter_lists
20824 = parser->num_template_parameter_lists;
20825 parser->num_template_parameter_lists = 0;
20826
20827 /* Look for the type-specifier. */
20828 cp_parser_type_specifier (parser,
20829 CP_PARSER_FLAGS_NONE,
20830 /*decl_specs=*/NULL,
20831 /*is_declarator=*/true,
20832 /*declares_class_or_enum=*/NULL,
20833 /*is_cv_qualifier=*/NULL);
20834
20835 parser->num_template_parameter_lists
20836 = saved_num_template_parameter_lists;
20837
20838 /* Leave the scope of the class. */
20839 if (pushed_scope)
20840 pop_scope (pushed_scope);
20841
20842 constructor_p = !cp_parser_error_occurred (parser);
20843 }
20844 }
20845
20846 /* We did not really want to consume any tokens. */
20847 cp_parser_abort_tentative_parse (parser);
20848
20849 return constructor_p;
20850 }
20851
20852 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20853 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20854 they must be performed once we are in the scope of the function.
20855
20856 Returns the function defined. */
20857
20858 static tree
20859 cp_parser_function_definition_from_specifiers_and_declarator
20860 (cp_parser* parser,
20861 cp_decl_specifier_seq *decl_specifiers,
20862 tree attributes,
20863 const cp_declarator *declarator)
20864 {
20865 tree fn;
20866 bool success_p;
20867
20868 /* Begin the function-definition. */
20869 success_p = start_function (decl_specifiers, declarator, attributes);
20870
20871 /* The things we're about to see are not directly qualified by any
20872 template headers we've seen thus far. */
20873 reset_specialization ();
20874
20875 /* If there were names looked up in the decl-specifier-seq that we
20876 did not check, check them now. We must wait until we are in the
20877 scope of the function to perform the checks, since the function
20878 might be a friend. */
20879 perform_deferred_access_checks ();
20880
20881 if (!success_p)
20882 {
20883 /* Skip the entire function. */
20884 cp_parser_skip_to_end_of_block_or_statement (parser);
20885 fn = error_mark_node;
20886 }
20887 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20888 {
20889 /* Seen already, skip it. An error message has already been output. */
20890 cp_parser_skip_to_end_of_block_or_statement (parser);
20891 fn = current_function_decl;
20892 current_function_decl = NULL_TREE;
20893 /* If this is a function from a class, pop the nested class. */
20894 if (current_class_name)
20895 pop_nested_class ();
20896 }
20897 else
20898 {
20899 timevar_id_t tv;
20900 if (DECL_DECLARED_INLINE_P (current_function_decl))
20901 tv = TV_PARSE_INLINE;
20902 else
20903 tv = TV_PARSE_FUNC;
20904 timevar_push (tv);
20905 fn = cp_parser_function_definition_after_declarator (parser,
20906 /*inline_p=*/false);
20907 timevar_pop (tv);
20908 }
20909
20910 return fn;
20911 }
20912
20913 /* Parse the part of a function-definition that follows the
20914 declarator. INLINE_P is TRUE iff this function is an inline
20915 function defined within a class-specifier.
20916
20917 Returns the function defined. */
20918
20919 static tree
20920 cp_parser_function_definition_after_declarator (cp_parser* parser,
20921 bool inline_p)
20922 {
20923 tree fn;
20924 bool ctor_initializer_p = false;
20925 bool saved_in_unbraced_linkage_specification_p;
20926 bool saved_in_function_body;
20927 unsigned saved_num_template_parameter_lists;
20928 cp_token *token;
20929
20930 saved_in_function_body = parser->in_function_body;
20931 parser->in_function_body = true;
20932 /* If the next token is `return', then the code may be trying to
20933 make use of the "named return value" extension that G++ used to
20934 support. */
20935 token = cp_lexer_peek_token (parser->lexer);
20936 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20937 {
20938 /* Consume the `return' keyword. */
20939 cp_lexer_consume_token (parser->lexer);
20940 /* Look for the identifier that indicates what value is to be
20941 returned. */
20942 cp_parser_identifier (parser);
20943 /* Issue an error message. */
20944 error_at (token->location,
20945 "named return values are no longer supported");
20946 /* Skip tokens until we reach the start of the function body. */
20947 while (true)
20948 {
20949 cp_token *token = cp_lexer_peek_token (parser->lexer);
20950 if (token->type == CPP_OPEN_BRACE
20951 || token->type == CPP_EOF
20952 || token->type == CPP_PRAGMA_EOL)
20953 break;
20954 cp_lexer_consume_token (parser->lexer);
20955 }
20956 }
20957 /* The `extern' in `extern "C" void f () { ... }' does not apply to
20958 anything declared inside `f'. */
20959 saved_in_unbraced_linkage_specification_p
20960 = parser->in_unbraced_linkage_specification_p;
20961 parser->in_unbraced_linkage_specification_p = false;
20962 /* Inside the function, surrounding template-parameter-lists do not
20963 apply. */
20964 saved_num_template_parameter_lists
20965 = parser->num_template_parameter_lists;
20966 parser->num_template_parameter_lists = 0;
20967
20968 start_lambda_scope (current_function_decl);
20969
20970 /* If the next token is `try', `__transaction_atomic', or
20971 `__transaction_relaxed`, then we are looking at either function-try-block
20972 or function-transaction-block. Note that all of these include the
20973 function-body. */
20974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
20975 ctor_initializer_p = cp_parser_function_transaction (parser,
20976 RID_TRANSACTION_ATOMIC);
20977 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20978 RID_TRANSACTION_RELAXED))
20979 ctor_initializer_p = cp_parser_function_transaction (parser,
20980 RID_TRANSACTION_RELAXED);
20981 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20982 ctor_initializer_p = cp_parser_function_try_block (parser);
20983 else
20984 ctor_initializer_p
20985 = cp_parser_ctor_initializer_opt_and_function_body (parser);
20986
20987 finish_lambda_scope ();
20988
20989 /* Finish the function. */
20990 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20991 (inline_p ? 2 : 0));
20992 /* Generate code for it, if necessary. */
20993 expand_or_defer_fn (fn);
20994 /* Restore the saved values. */
20995 parser->in_unbraced_linkage_specification_p
20996 = saved_in_unbraced_linkage_specification_p;
20997 parser->num_template_parameter_lists
20998 = saved_num_template_parameter_lists;
20999 parser->in_function_body = saved_in_function_body;
21000
21001 return fn;
21002 }
21003
21004 /* Parse a template-declaration, assuming that the `export' (and
21005 `extern') keywords, if present, has already been scanned. MEMBER_P
21006 is as for cp_parser_template_declaration. */
21007
21008 static void
21009 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21010 {
21011 tree decl = NULL_TREE;
21012 VEC (deferred_access_check,gc) *checks;
21013 tree parameter_list;
21014 bool friend_p = false;
21015 bool need_lang_pop;
21016 cp_token *token;
21017
21018 /* Look for the `template' keyword. */
21019 token = cp_lexer_peek_token (parser->lexer);
21020 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21021 return;
21022
21023 /* And the `<'. */
21024 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21025 return;
21026 if (at_class_scope_p () && current_function_decl)
21027 {
21028 /* 14.5.2.2 [temp.mem]
21029
21030 A local class shall not have member templates. */
21031 error_at (token->location,
21032 "invalid declaration of member template in local class");
21033 cp_parser_skip_to_end_of_block_or_statement (parser);
21034 return;
21035 }
21036 /* [temp]
21037
21038 A template ... shall not have C linkage. */
21039 if (current_lang_name == lang_name_c)
21040 {
21041 error_at (token->location, "template with C linkage");
21042 /* Give it C++ linkage to avoid confusing other parts of the
21043 front end. */
21044 push_lang_context (lang_name_cplusplus);
21045 need_lang_pop = true;
21046 }
21047 else
21048 need_lang_pop = false;
21049
21050 /* We cannot perform access checks on the template parameter
21051 declarations until we know what is being declared, just as we
21052 cannot check the decl-specifier list. */
21053 push_deferring_access_checks (dk_deferred);
21054
21055 /* If the next token is `>', then we have an invalid
21056 specialization. Rather than complain about an invalid template
21057 parameter, issue an error message here. */
21058 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21059 {
21060 cp_parser_error (parser, "invalid explicit specialization");
21061 begin_specialization ();
21062 parameter_list = NULL_TREE;
21063 }
21064 else
21065 {
21066 /* Parse the template parameters. */
21067 parameter_list = cp_parser_template_parameter_list (parser);
21068 fixup_template_parms ();
21069 }
21070
21071 /* Get the deferred access checks from the parameter list. These
21072 will be checked once we know what is being declared, as for a
21073 member template the checks must be performed in the scope of the
21074 class containing the member. */
21075 checks = get_deferred_access_checks ();
21076
21077 /* Look for the `>'. */
21078 cp_parser_skip_to_end_of_template_parameter_list (parser);
21079 /* We just processed one more parameter list. */
21080 ++parser->num_template_parameter_lists;
21081 /* If the next token is `template', there are more template
21082 parameters. */
21083 if (cp_lexer_next_token_is_keyword (parser->lexer,
21084 RID_TEMPLATE))
21085 cp_parser_template_declaration_after_export (parser, member_p);
21086 else if (cxx_dialect >= cxx0x
21087 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21088 decl = cp_parser_alias_declaration (parser);
21089 else
21090 {
21091 /* There are no access checks when parsing a template, as we do not
21092 know if a specialization will be a friend. */
21093 push_deferring_access_checks (dk_no_check);
21094 token = cp_lexer_peek_token (parser->lexer);
21095 decl = cp_parser_single_declaration (parser,
21096 checks,
21097 member_p,
21098 /*explicit_specialization_p=*/false,
21099 &friend_p);
21100 pop_deferring_access_checks ();
21101
21102 /* If this is a member template declaration, let the front
21103 end know. */
21104 if (member_p && !friend_p && decl)
21105 {
21106 if (TREE_CODE (decl) == TYPE_DECL)
21107 cp_parser_check_access_in_redeclaration (decl, token->location);
21108
21109 decl = finish_member_template_decl (decl);
21110 }
21111 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21112 make_friend_class (current_class_type, TREE_TYPE (decl),
21113 /*complain=*/true);
21114 }
21115 /* We are done with the current parameter list. */
21116 --parser->num_template_parameter_lists;
21117
21118 pop_deferring_access_checks ();
21119
21120 /* Finish up. */
21121 finish_template_decl (parameter_list);
21122
21123 /* Check the template arguments for a literal operator template. */
21124 if (decl
21125 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21126 && UDLIT_OPER_P (DECL_NAME (decl)))
21127 {
21128 bool ok = true;
21129 if (parameter_list == NULL_TREE)
21130 ok = false;
21131 else
21132 {
21133 int num_parms = TREE_VEC_LENGTH (parameter_list);
21134 if (num_parms != 1)
21135 ok = false;
21136 else
21137 {
21138 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21139 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21140 if (TREE_TYPE (parm) != char_type_node
21141 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21142 ok = false;
21143 }
21144 }
21145 if (!ok)
21146 error ("literal operator template %qD has invalid parameter list."
21147 " Expected non-type template argument pack <char...>",
21148 decl);
21149 }
21150 /* Register member declarations. */
21151 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21152 finish_member_declaration (decl);
21153 /* For the erroneous case of a template with C linkage, we pushed an
21154 implicit C++ linkage scope; exit that scope now. */
21155 if (need_lang_pop)
21156 pop_lang_context ();
21157 /* If DECL is a function template, we must return to parse it later.
21158 (Even though there is no definition, there might be default
21159 arguments that need handling.) */
21160 if (member_p && decl
21161 && (TREE_CODE (decl) == FUNCTION_DECL
21162 || DECL_FUNCTION_TEMPLATE_P (decl)))
21163 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21164 }
21165
21166 /* Perform the deferred access checks from a template-parameter-list.
21167 CHECKS is a TREE_LIST of access checks, as returned by
21168 get_deferred_access_checks. */
21169
21170 static void
21171 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21172 {
21173 ++processing_template_parmlist;
21174 perform_access_checks (checks);
21175 --processing_template_parmlist;
21176 }
21177
21178 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21179 `function-definition' sequence. MEMBER_P is true, this declaration
21180 appears in a class scope.
21181
21182 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21183 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21184
21185 static tree
21186 cp_parser_single_declaration (cp_parser* parser,
21187 VEC (deferred_access_check,gc)* checks,
21188 bool member_p,
21189 bool explicit_specialization_p,
21190 bool* friend_p)
21191 {
21192 int declares_class_or_enum;
21193 tree decl = NULL_TREE;
21194 cp_decl_specifier_seq decl_specifiers;
21195 bool function_definition_p = false;
21196 cp_token *decl_spec_token_start;
21197
21198 /* This function is only used when processing a template
21199 declaration. */
21200 gcc_assert (innermost_scope_kind () == sk_template_parms
21201 || innermost_scope_kind () == sk_template_spec);
21202
21203 /* Defer access checks until we know what is being declared. */
21204 push_deferring_access_checks (dk_deferred);
21205
21206 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21207 alternative. */
21208 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21209 cp_parser_decl_specifier_seq (parser,
21210 CP_PARSER_FLAGS_OPTIONAL,
21211 &decl_specifiers,
21212 &declares_class_or_enum);
21213 if (friend_p)
21214 *friend_p = cp_parser_friend_p (&decl_specifiers);
21215
21216 /* There are no template typedefs. */
21217 if (decl_specifiers.specs[(int) ds_typedef])
21218 {
21219 error_at (decl_spec_token_start->location,
21220 "template declaration of %<typedef%>");
21221 decl = error_mark_node;
21222 }
21223
21224 /* Gather up the access checks that occurred the
21225 decl-specifier-seq. */
21226 stop_deferring_access_checks ();
21227
21228 /* Check for the declaration of a template class. */
21229 if (declares_class_or_enum)
21230 {
21231 if (cp_parser_declares_only_class_p (parser))
21232 {
21233 decl = shadow_tag (&decl_specifiers);
21234
21235 /* In this case:
21236
21237 struct C {
21238 friend template <typename T> struct A<T>::B;
21239 };
21240
21241 A<T>::B will be represented by a TYPENAME_TYPE, and
21242 therefore not recognized by shadow_tag. */
21243 if (friend_p && *friend_p
21244 && !decl
21245 && decl_specifiers.type
21246 && TYPE_P (decl_specifiers.type))
21247 decl = decl_specifiers.type;
21248
21249 if (decl && decl != error_mark_node)
21250 decl = TYPE_NAME (decl);
21251 else
21252 decl = error_mark_node;
21253
21254 /* Perform access checks for template parameters. */
21255 cp_parser_perform_template_parameter_access_checks (checks);
21256 }
21257 }
21258
21259 /* Complain about missing 'typename' or other invalid type names. */
21260 if (!decl_specifiers.any_type_specifiers_p
21261 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21262 {
21263 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21264 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21265 the rest of this declaration. */
21266 decl = error_mark_node;
21267 goto out;
21268 }
21269
21270 /* If it's not a template class, try for a template function. If
21271 the next token is a `;', then this declaration does not declare
21272 anything. But, if there were errors in the decl-specifiers, then
21273 the error might well have come from an attempted class-specifier.
21274 In that case, there's no need to warn about a missing declarator. */
21275 if (!decl
21276 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21277 || decl_specifiers.type != error_mark_node))
21278 {
21279 decl = cp_parser_init_declarator (parser,
21280 &decl_specifiers,
21281 checks,
21282 /*function_definition_allowed_p=*/true,
21283 member_p,
21284 declares_class_or_enum,
21285 &function_definition_p,
21286 NULL);
21287
21288 /* 7.1.1-1 [dcl.stc]
21289
21290 A storage-class-specifier shall not be specified in an explicit
21291 specialization... */
21292 if (decl
21293 && explicit_specialization_p
21294 && decl_specifiers.storage_class != sc_none)
21295 {
21296 error_at (decl_spec_token_start->location,
21297 "explicit template specialization cannot have a storage class");
21298 decl = error_mark_node;
21299 }
21300 }
21301
21302 /* Look for a trailing `;' after the declaration. */
21303 if (!function_definition_p
21304 && (decl == error_mark_node
21305 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21306 cp_parser_skip_to_end_of_block_or_statement (parser);
21307
21308 out:
21309 pop_deferring_access_checks ();
21310
21311 /* Clear any current qualification; whatever comes next is the start
21312 of something new. */
21313 parser->scope = NULL_TREE;
21314 parser->qualifying_scope = NULL_TREE;
21315 parser->object_scope = NULL_TREE;
21316
21317 return decl;
21318 }
21319
21320 /* Parse a cast-expression that is not the operand of a unary "&". */
21321
21322 static tree
21323 cp_parser_simple_cast_expression (cp_parser *parser)
21324 {
21325 return cp_parser_cast_expression (parser, /*address_p=*/false,
21326 /*cast_p=*/false, NULL);
21327 }
21328
21329 /* Parse a functional cast to TYPE. Returns an expression
21330 representing the cast. */
21331
21332 static tree
21333 cp_parser_functional_cast (cp_parser* parser, tree type)
21334 {
21335 VEC(tree,gc) *vec;
21336 tree expression_list;
21337 tree cast;
21338 bool nonconst_p;
21339
21340 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21341 {
21342 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21343 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21344 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21345 if (TREE_CODE (type) == TYPE_DECL)
21346 type = TREE_TYPE (type);
21347 return finish_compound_literal (type, expression_list,
21348 tf_warning_or_error);
21349 }
21350
21351
21352 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21353 /*cast_p=*/true,
21354 /*allow_expansion_p=*/true,
21355 /*non_constant_p=*/NULL);
21356 if (vec == NULL)
21357 expression_list = error_mark_node;
21358 else
21359 {
21360 expression_list = build_tree_list_vec (vec);
21361 release_tree_vector (vec);
21362 }
21363
21364 cast = build_functional_cast (type, expression_list,
21365 tf_warning_or_error);
21366 /* [expr.const]/1: In an integral constant expression "only type
21367 conversions to integral or enumeration type can be used". */
21368 if (TREE_CODE (type) == TYPE_DECL)
21369 type = TREE_TYPE (type);
21370 if (cast != error_mark_node
21371 && !cast_valid_in_integral_constant_expression_p (type)
21372 && cp_parser_non_integral_constant_expression (parser,
21373 NIC_CONSTRUCTOR))
21374 return error_mark_node;
21375 return cast;
21376 }
21377
21378 /* Save the tokens that make up the body of a member function defined
21379 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21380 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21381 specifiers applied to the declaration. Returns the FUNCTION_DECL
21382 for the member function. */
21383
21384 static tree
21385 cp_parser_save_member_function_body (cp_parser* parser,
21386 cp_decl_specifier_seq *decl_specifiers,
21387 cp_declarator *declarator,
21388 tree attributes)
21389 {
21390 cp_token *first;
21391 cp_token *last;
21392 tree fn;
21393
21394 /* Create the FUNCTION_DECL. */
21395 fn = grokmethod (decl_specifiers, declarator, attributes);
21396 /* If something went badly wrong, bail out now. */
21397 if (fn == error_mark_node)
21398 {
21399 /* If there's a function-body, skip it. */
21400 if (cp_parser_token_starts_function_definition_p
21401 (cp_lexer_peek_token (parser->lexer)))
21402 cp_parser_skip_to_end_of_block_or_statement (parser);
21403 return error_mark_node;
21404 }
21405
21406 /* Remember it, if there default args to post process. */
21407 cp_parser_save_default_args (parser, fn);
21408
21409 /* Save away the tokens that make up the body of the
21410 function. */
21411 first = parser->lexer->next_token;
21412 /* We can have braced-init-list mem-initializers before the fn body. */
21413 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21414 {
21415 cp_lexer_consume_token (parser->lexer);
21416 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21417 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21418 {
21419 /* cache_group will stop after an un-nested { } pair, too. */
21420 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21421 break;
21422
21423 /* variadic mem-inits have ... after the ')'. */
21424 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21425 cp_lexer_consume_token (parser->lexer);
21426 }
21427 }
21428 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21429 /* Handle function try blocks. */
21430 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21431 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21432 last = parser->lexer->next_token;
21433
21434 /* Save away the inline definition; we will process it when the
21435 class is complete. */
21436 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21437 DECL_PENDING_INLINE_P (fn) = 1;
21438
21439 /* We need to know that this was defined in the class, so that
21440 friend templates are handled correctly. */
21441 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21442
21443 /* Add FN to the queue of functions to be parsed later. */
21444 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21445
21446 return fn;
21447 }
21448
21449 /* Save the tokens that make up the in-class initializer for a non-static
21450 data member. Returns a DEFAULT_ARG. */
21451
21452 static tree
21453 cp_parser_save_nsdmi (cp_parser* parser)
21454 {
21455 /* Save away the tokens that make up the body of the
21456 function. */
21457 cp_token *first = parser->lexer->next_token;
21458 cp_token *last;
21459 tree node;
21460
21461 /* Save tokens until the next comma or semicolon. */
21462 cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21463
21464 last = parser->lexer->next_token;
21465
21466 node = make_node (DEFAULT_ARG);
21467 DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21468 DEFARG_INSTANTIATIONS (node) = NULL;
21469
21470 return node;
21471 }
21472
21473
21474 /* Parse a template-argument-list, as well as the trailing ">" (but
21475 not the opening "<"). See cp_parser_template_argument_list for the
21476 return value. */
21477
21478 static tree
21479 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21480 {
21481 tree arguments;
21482 tree saved_scope;
21483 tree saved_qualifying_scope;
21484 tree saved_object_scope;
21485 bool saved_greater_than_is_operator_p;
21486 int saved_unevaluated_operand;
21487 int saved_inhibit_evaluation_warnings;
21488
21489 /* [temp.names]
21490
21491 When parsing a template-id, the first non-nested `>' is taken as
21492 the end of the template-argument-list rather than a greater-than
21493 operator. */
21494 saved_greater_than_is_operator_p
21495 = parser->greater_than_is_operator_p;
21496 parser->greater_than_is_operator_p = false;
21497 /* Parsing the argument list may modify SCOPE, so we save it
21498 here. */
21499 saved_scope = parser->scope;
21500 saved_qualifying_scope = parser->qualifying_scope;
21501 saved_object_scope = parser->object_scope;
21502 /* We need to evaluate the template arguments, even though this
21503 template-id may be nested within a "sizeof". */
21504 saved_unevaluated_operand = cp_unevaluated_operand;
21505 cp_unevaluated_operand = 0;
21506 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21507 c_inhibit_evaluation_warnings = 0;
21508 /* Parse the template-argument-list itself. */
21509 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21510 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21511 arguments = NULL_TREE;
21512 else
21513 arguments = cp_parser_template_argument_list (parser);
21514 /* Look for the `>' that ends the template-argument-list. If we find
21515 a '>>' instead, it's probably just a typo. */
21516 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21517 {
21518 if (cxx_dialect != cxx98)
21519 {
21520 /* In C++0x, a `>>' in a template argument list or cast
21521 expression is considered to be two separate `>'
21522 tokens. So, change the current token to a `>', but don't
21523 consume it: it will be consumed later when the outer
21524 template argument list (or cast expression) is parsed.
21525 Note that this replacement of `>' for `>>' is necessary
21526 even if we are parsing tentatively: in the tentative
21527 case, after calling
21528 cp_parser_enclosed_template_argument_list we will always
21529 throw away all of the template arguments and the first
21530 closing `>', either because the template argument list
21531 was erroneous or because we are replacing those tokens
21532 with a CPP_TEMPLATE_ID token. The second `>' (which will
21533 not have been thrown away) is needed either to close an
21534 outer template argument list or to complete a new-style
21535 cast. */
21536 cp_token *token = cp_lexer_peek_token (parser->lexer);
21537 token->type = CPP_GREATER;
21538 }
21539 else if (!saved_greater_than_is_operator_p)
21540 {
21541 /* If we're in a nested template argument list, the '>>' has
21542 to be a typo for '> >'. We emit the error message, but we
21543 continue parsing and we push a '>' as next token, so that
21544 the argument list will be parsed correctly. Note that the
21545 global source location is still on the token before the
21546 '>>', so we need to say explicitly where we want it. */
21547 cp_token *token = cp_lexer_peek_token (parser->lexer);
21548 error_at (token->location, "%<>>%> should be %<> >%> "
21549 "within a nested template argument list");
21550
21551 token->type = CPP_GREATER;
21552 }
21553 else
21554 {
21555 /* If this is not a nested template argument list, the '>>'
21556 is a typo for '>'. Emit an error message and continue.
21557 Same deal about the token location, but here we can get it
21558 right by consuming the '>>' before issuing the diagnostic. */
21559 cp_token *token = cp_lexer_consume_token (parser->lexer);
21560 error_at (token->location,
21561 "spurious %<>>%>, use %<>%> to terminate "
21562 "a template argument list");
21563 }
21564 }
21565 else
21566 cp_parser_skip_to_end_of_template_parameter_list (parser);
21567 /* The `>' token might be a greater-than operator again now. */
21568 parser->greater_than_is_operator_p
21569 = saved_greater_than_is_operator_p;
21570 /* Restore the SAVED_SCOPE. */
21571 parser->scope = saved_scope;
21572 parser->qualifying_scope = saved_qualifying_scope;
21573 parser->object_scope = saved_object_scope;
21574 cp_unevaluated_operand = saved_unevaluated_operand;
21575 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21576
21577 return arguments;
21578 }
21579
21580 /* MEMBER_FUNCTION is a member function, or a friend. If default
21581 arguments, or the body of the function have not yet been parsed,
21582 parse them now. */
21583
21584 static void
21585 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21586 {
21587 timevar_push (TV_PARSE_INMETH);
21588 /* If this member is a template, get the underlying
21589 FUNCTION_DECL. */
21590 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21591 member_function = DECL_TEMPLATE_RESULT (member_function);
21592
21593 /* There should not be any class definitions in progress at this
21594 point; the bodies of members are only parsed outside of all class
21595 definitions. */
21596 gcc_assert (parser->num_classes_being_defined == 0);
21597 /* While we're parsing the member functions we might encounter more
21598 classes. We want to handle them right away, but we don't want
21599 them getting mixed up with functions that are currently in the
21600 queue. */
21601 push_unparsed_function_queues (parser);
21602
21603 /* Make sure that any template parameters are in scope. */
21604 maybe_begin_member_template_processing (member_function);
21605
21606 /* If the body of the function has not yet been parsed, parse it
21607 now. */
21608 if (DECL_PENDING_INLINE_P (member_function))
21609 {
21610 tree function_scope;
21611 cp_token_cache *tokens;
21612
21613 /* The function is no longer pending; we are processing it. */
21614 tokens = DECL_PENDING_INLINE_INFO (member_function);
21615 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21616 DECL_PENDING_INLINE_P (member_function) = 0;
21617
21618 /* If this is a local class, enter the scope of the containing
21619 function. */
21620 function_scope = current_function_decl;
21621 if (function_scope)
21622 push_function_context ();
21623
21624 /* Push the body of the function onto the lexer stack. */
21625 cp_parser_push_lexer_for_tokens (parser, tokens);
21626
21627 /* Let the front end know that we going to be defining this
21628 function. */
21629 start_preparsed_function (member_function, NULL_TREE,
21630 SF_PRE_PARSED | SF_INCLASS_INLINE);
21631
21632 /* Don't do access checking if it is a templated function. */
21633 if (processing_template_decl)
21634 push_deferring_access_checks (dk_no_check);
21635
21636 /* Now, parse the body of the function. */
21637 cp_parser_function_definition_after_declarator (parser,
21638 /*inline_p=*/true);
21639
21640 if (processing_template_decl)
21641 pop_deferring_access_checks ();
21642
21643 /* Leave the scope of the containing function. */
21644 if (function_scope)
21645 pop_function_context ();
21646 cp_parser_pop_lexer (parser);
21647 }
21648
21649 /* Remove any template parameters from the symbol table. */
21650 maybe_end_member_template_processing ();
21651
21652 /* Restore the queue. */
21653 pop_unparsed_function_queues (parser);
21654 timevar_pop (TV_PARSE_INMETH);
21655 }
21656
21657 /* If DECL contains any default args, remember it on the unparsed
21658 functions queue. */
21659
21660 static void
21661 cp_parser_save_default_args (cp_parser* parser, tree decl)
21662 {
21663 tree probe;
21664
21665 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21666 probe;
21667 probe = TREE_CHAIN (probe))
21668 if (TREE_PURPOSE (probe))
21669 {
21670 cp_default_arg_entry *entry
21671 = VEC_safe_push (cp_default_arg_entry, gc,
21672 unparsed_funs_with_default_args, NULL);
21673 entry->class_type = current_class_type;
21674 entry->decl = decl;
21675 break;
21676 }
21677 }
21678
21679 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21680 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21681 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21682 from the parameter-type-list. */
21683
21684 static tree
21685 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21686 tree default_arg, tree parmtype)
21687 {
21688 cp_token_cache *tokens;
21689 tree parsed_arg;
21690 bool dummy;
21691
21692 /* Push the saved tokens for the default argument onto the parser's
21693 lexer stack. */
21694 tokens = DEFARG_TOKENS (default_arg);
21695 cp_parser_push_lexer_for_tokens (parser, tokens);
21696
21697 start_lambda_scope (decl);
21698
21699 /* Parse the default argument. */
21700 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21701 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21702 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21703
21704 finish_lambda_scope ();
21705
21706 if (!processing_template_decl)
21707 {
21708 /* In a non-template class, check conversions now. In a template,
21709 we'll wait and instantiate these as needed. */
21710 if (TREE_CODE (decl) == PARM_DECL)
21711 parsed_arg = check_default_argument (parmtype, parsed_arg);
21712 else
21713 {
21714 int flags = LOOKUP_IMPLICIT;
21715 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21716 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21717 flags = LOOKUP_NORMAL;
21718 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21719 }
21720 }
21721
21722 /* If the token stream has not been completely used up, then
21723 there was extra junk after the end of the default
21724 argument. */
21725 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21726 {
21727 if (TREE_CODE (decl) == PARM_DECL)
21728 cp_parser_error (parser, "expected %<,%>");
21729 else
21730 cp_parser_error (parser, "expected %<;%>");
21731 }
21732
21733 /* Revert to the main lexer. */
21734 cp_parser_pop_lexer (parser);
21735
21736 return parsed_arg;
21737 }
21738
21739 /* FIELD is a non-static data member with an initializer which we saved for
21740 later; parse it now. */
21741
21742 static void
21743 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21744 {
21745 tree def;
21746
21747 push_unparsed_function_queues (parser);
21748 def = cp_parser_late_parse_one_default_arg (parser, field,
21749 DECL_INITIAL (field),
21750 NULL_TREE);
21751 pop_unparsed_function_queues (parser);
21752
21753 DECL_INITIAL (field) = def;
21754 }
21755
21756 /* FN is a FUNCTION_DECL which may contains a parameter with an
21757 unparsed DEFAULT_ARG. Parse the default args now. This function
21758 assumes that the current scope is the scope in which the default
21759 argument should be processed. */
21760
21761 static void
21762 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21763 {
21764 bool saved_local_variables_forbidden_p;
21765 tree parm, parmdecl;
21766
21767 /* While we're parsing the default args, we might (due to the
21768 statement expression extension) encounter more classes. We want
21769 to handle them right away, but we don't want them getting mixed
21770 up with default args that are currently in the queue. */
21771 push_unparsed_function_queues (parser);
21772
21773 /* Local variable names (and the `this' keyword) may not appear
21774 in a default argument. */
21775 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21776 parser->local_variables_forbidden_p = true;
21777
21778 push_defarg_context (fn);
21779
21780 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21781 parmdecl = DECL_ARGUMENTS (fn);
21782 parm && parm != void_list_node;
21783 parm = TREE_CHAIN (parm),
21784 parmdecl = DECL_CHAIN (parmdecl))
21785 {
21786 tree default_arg = TREE_PURPOSE (parm);
21787 tree parsed_arg;
21788 VEC(tree,gc) *insts;
21789 tree copy;
21790 unsigned ix;
21791
21792 if (!default_arg)
21793 continue;
21794
21795 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21796 /* This can happen for a friend declaration for a function
21797 already declared with default arguments. */
21798 continue;
21799
21800 parsed_arg
21801 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21802 default_arg,
21803 TREE_VALUE (parm));
21804 if (parsed_arg == error_mark_node)
21805 {
21806 continue;
21807 }
21808
21809 TREE_PURPOSE (parm) = parsed_arg;
21810
21811 /* Update any instantiations we've already created. */
21812 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21813 VEC_iterate (tree, insts, ix, copy); ix++)
21814 TREE_PURPOSE (copy) = parsed_arg;
21815 }
21816
21817 pop_defarg_context ();
21818
21819 /* Make sure no default arg is missing. */
21820 check_default_args (fn);
21821
21822 /* Restore the state of local_variables_forbidden_p. */
21823 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21824
21825 /* Restore the queue. */
21826 pop_unparsed_function_queues (parser);
21827 }
21828
21829 /* Parse the operand of `sizeof' (or a similar operator). Returns
21830 either a TYPE or an expression, depending on the form of the
21831 input. The KEYWORD indicates which kind of expression we have
21832 encountered. */
21833
21834 static tree
21835 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21836 {
21837 tree expr = NULL_TREE;
21838 const char *saved_message;
21839 char *tmp;
21840 bool saved_integral_constant_expression_p;
21841 bool saved_non_integral_constant_expression_p;
21842 bool pack_expansion_p = false;
21843
21844 /* Types cannot be defined in a `sizeof' expression. Save away the
21845 old message. */
21846 saved_message = parser->type_definition_forbidden_message;
21847 /* And create the new one. */
21848 tmp = concat ("types may not be defined in %<",
21849 IDENTIFIER_POINTER (ridpointers[keyword]),
21850 "%> expressions", NULL);
21851 parser->type_definition_forbidden_message = tmp;
21852
21853 /* The restrictions on constant-expressions do not apply inside
21854 sizeof expressions. */
21855 saved_integral_constant_expression_p
21856 = parser->integral_constant_expression_p;
21857 saved_non_integral_constant_expression_p
21858 = parser->non_integral_constant_expression_p;
21859 parser->integral_constant_expression_p = false;
21860
21861 /* If it's a `...', then we are computing the length of a parameter
21862 pack. */
21863 if (keyword == RID_SIZEOF
21864 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21865 {
21866 /* Consume the `...'. */
21867 cp_lexer_consume_token (parser->lexer);
21868 maybe_warn_variadic_templates ();
21869
21870 /* Note that this is an expansion. */
21871 pack_expansion_p = true;
21872 }
21873
21874 /* Do not actually evaluate the expression. */
21875 ++cp_unevaluated_operand;
21876 ++c_inhibit_evaluation_warnings;
21877 /* If it's a `(', then we might be looking at the type-id
21878 construction. */
21879 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21880 {
21881 tree type;
21882 bool saved_in_type_id_in_expr_p;
21883
21884 /* We can't be sure yet whether we're looking at a type-id or an
21885 expression. */
21886 cp_parser_parse_tentatively (parser);
21887 /* Consume the `('. */
21888 cp_lexer_consume_token (parser->lexer);
21889 /* Parse the type-id. */
21890 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21891 parser->in_type_id_in_expr_p = true;
21892 type = cp_parser_type_id (parser);
21893 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21894 /* Now, look for the trailing `)'. */
21895 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21896 /* If all went well, then we're done. */
21897 if (cp_parser_parse_definitely (parser))
21898 {
21899 cp_decl_specifier_seq decl_specs;
21900
21901 /* Build a trivial decl-specifier-seq. */
21902 clear_decl_specs (&decl_specs);
21903 decl_specs.type = type;
21904
21905 /* Call grokdeclarator to figure out what type this is. */
21906 expr = grokdeclarator (NULL,
21907 &decl_specs,
21908 TYPENAME,
21909 /*initialized=*/0,
21910 /*attrlist=*/NULL);
21911 }
21912 }
21913
21914 /* If the type-id production did not work out, then we must be
21915 looking at the unary-expression production. */
21916 if (!expr)
21917 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21918 /*cast_p=*/false, NULL);
21919
21920 if (pack_expansion_p)
21921 /* Build a pack expansion. */
21922 expr = make_pack_expansion (expr);
21923
21924 /* Go back to evaluating expressions. */
21925 --cp_unevaluated_operand;
21926 --c_inhibit_evaluation_warnings;
21927
21928 /* Free the message we created. */
21929 free (tmp);
21930 /* And restore the old one. */
21931 parser->type_definition_forbidden_message = saved_message;
21932 parser->integral_constant_expression_p
21933 = saved_integral_constant_expression_p;
21934 parser->non_integral_constant_expression_p
21935 = saved_non_integral_constant_expression_p;
21936
21937 return expr;
21938 }
21939
21940 /* If the current declaration has no declarator, return true. */
21941
21942 static bool
21943 cp_parser_declares_only_class_p (cp_parser *parser)
21944 {
21945 /* If the next token is a `;' or a `,' then there is no
21946 declarator. */
21947 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21948 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21949 }
21950
21951 /* Update the DECL_SPECS to reflect the storage class indicated by
21952 KEYWORD. */
21953
21954 static void
21955 cp_parser_set_storage_class (cp_parser *parser,
21956 cp_decl_specifier_seq *decl_specs,
21957 enum rid keyword,
21958 location_t location)
21959 {
21960 cp_storage_class storage_class;
21961
21962 if (parser->in_unbraced_linkage_specification_p)
21963 {
21964 error_at (location, "invalid use of %qD in linkage specification",
21965 ridpointers[keyword]);
21966 return;
21967 }
21968 else if (decl_specs->storage_class != sc_none)
21969 {
21970 decl_specs->conflicting_specifiers_p = true;
21971 return;
21972 }
21973
21974 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
21975 && decl_specs->specs[(int) ds_thread])
21976 {
21977 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
21978 decl_specs->specs[(int) ds_thread] = 0;
21979 }
21980
21981 switch (keyword)
21982 {
21983 case RID_AUTO:
21984 storage_class = sc_auto;
21985 break;
21986 case RID_REGISTER:
21987 storage_class = sc_register;
21988 break;
21989 case RID_STATIC:
21990 storage_class = sc_static;
21991 break;
21992 case RID_EXTERN:
21993 storage_class = sc_extern;
21994 break;
21995 case RID_MUTABLE:
21996 storage_class = sc_mutable;
21997 break;
21998 default:
21999 gcc_unreachable ();
22000 }
22001 decl_specs->storage_class = storage_class;
22002
22003 /* A storage class specifier cannot be applied alongside a typedef
22004 specifier. If there is a typedef specifier present then set
22005 conflicting_specifiers_p which will trigger an error later
22006 on in grokdeclarator. */
22007 if (decl_specs->specs[(int)ds_typedef])
22008 decl_specs->conflicting_specifiers_p = true;
22009 }
22010
22011 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22012 is true, the type is a class or enum definition. */
22013
22014 static void
22015 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22016 tree type_spec,
22017 location_t location,
22018 bool type_definition_p)
22019 {
22020 decl_specs->any_specifiers_p = true;
22021
22022 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22023 (with, for example, in "typedef int wchar_t;") we remember that
22024 this is what happened. In system headers, we ignore these
22025 declarations so that G++ can work with system headers that are not
22026 C++-safe. */
22027 if (decl_specs->specs[(int) ds_typedef]
22028 && !type_definition_p
22029 && (type_spec == boolean_type_node
22030 || type_spec == char16_type_node
22031 || type_spec == char32_type_node
22032 || type_spec == wchar_type_node)
22033 && (decl_specs->type
22034 || decl_specs->specs[(int) ds_long]
22035 || decl_specs->specs[(int) ds_short]
22036 || decl_specs->specs[(int) ds_unsigned]
22037 || decl_specs->specs[(int) ds_signed]))
22038 {
22039 decl_specs->redefined_builtin_type = type_spec;
22040 if (!decl_specs->type)
22041 {
22042 decl_specs->type = type_spec;
22043 decl_specs->type_definition_p = false;
22044 decl_specs->type_location = location;
22045 }
22046 }
22047 else if (decl_specs->type)
22048 decl_specs->multiple_types_p = true;
22049 else
22050 {
22051 decl_specs->type = type_spec;
22052 decl_specs->type_definition_p = type_definition_p;
22053 decl_specs->redefined_builtin_type = NULL_TREE;
22054 decl_specs->type_location = location;
22055 }
22056 }
22057
22058 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22059 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22060
22061 static bool
22062 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22063 {
22064 return decl_specifiers->specs[(int) ds_friend] != 0;
22065 }
22066
22067 /* Issue an error message indicating that TOKEN_DESC was expected.
22068 If KEYWORD is true, it indicated this function is called by
22069 cp_parser_require_keword and the required token can only be
22070 a indicated keyword. */
22071
22072 static void
22073 cp_parser_required_error (cp_parser *parser,
22074 required_token token_desc,
22075 bool keyword)
22076 {
22077 switch (token_desc)
22078 {
22079 case RT_NEW:
22080 cp_parser_error (parser, "expected %<new%>");
22081 return;
22082 case RT_DELETE:
22083 cp_parser_error (parser, "expected %<delete%>");
22084 return;
22085 case RT_RETURN:
22086 cp_parser_error (parser, "expected %<return%>");
22087 return;
22088 case RT_WHILE:
22089 cp_parser_error (parser, "expected %<while%>");
22090 return;
22091 case RT_EXTERN:
22092 cp_parser_error (parser, "expected %<extern%>");
22093 return;
22094 case RT_STATIC_ASSERT:
22095 cp_parser_error (parser, "expected %<static_assert%>");
22096 return;
22097 case RT_DECLTYPE:
22098 cp_parser_error (parser, "expected %<decltype%>");
22099 return;
22100 case RT_OPERATOR:
22101 cp_parser_error (parser, "expected %<operator%>");
22102 return;
22103 case RT_CLASS:
22104 cp_parser_error (parser, "expected %<class%>");
22105 return;
22106 case RT_TEMPLATE:
22107 cp_parser_error (parser, "expected %<template%>");
22108 return;
22109 case RT_NAMESPACE:
22110 cp_parser_error (parser, "expected %<namespace%>");
22111 return;
22112 case RT_USING:
22113 cp_parser_error (parser, "expected %<using%>");
22114 return;
22115 case RT_ASM:
22116 cp_parser_error (parser, "expected %<asm%>");
22117 return;
22118 case RT_TRY:
22119 cp_parser_error (parser, "expected %<try%>");
22120 return;
22121 case RT_CATCH:
22122 cp_parser_error (parser, "expected %<catch%>");
22123 return;
22124 case RT_THROW:
22125 cp_parser_error (parser, "expected %<throw%>");
22126 return;
22127 case RT_LABEL:
22128 cp_parser_error (parser, "expected %<__label__%>");
22129 return;
22130 case RT_AT_TRY:
22131 cp_parser_error (parser, "expected %<@try%>");
22132 return;
22133 case RT_AT_SYNCHRONIZED:
22134 cp_parser_error (parser, "expected %<@synchronized%>");
22135 return;
22136 case RT_AT_THROW:
22137 cp_parser_error (parser, "expected %<@throw%>");
22138 return;
22139 case RT_TRANSACTION_ATOMIC:
22140 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22141 return;
22142 case RT_TRANSACTION_RELAXED:
22143 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22144 return;
22145 default:
22146 break;
22147 }
22148 if (!keyword)
22149 {
22150 switch (token_desc)
22151 {
22152 case RT_SEMICOLON:
22153 cp_parser_error (parser, "expected %<;%>");
22154 return;
22155 case RT_OPEN_PAREN:
22156 cp_parser_error (parser, "expected %<(%>");
22157 return;
22158 case RT_CLOSE_BRACE:
22159 cp_parser_error (parser, "expected %<}%>");
22160 return;
22161 case RT_OPEN_BRACE:
22162 cp_parser_error (parser, "expected %<{%>");
22163 return;
22164 case RT_CLOSE_SQUARE:
22165 cp_parser_error (parser, "expected %<]%>");
22166 return;
22167 case RT_OPEN_SQUARE:
22168 cp_parser_error (parser, "expected %<[%>");
22169 return;
22170 case RT_COMMA:
22171 cp_parser_error (parser, "expected %<,%>");
22172 return;
22173 case RT_SCOPE:
22174 cp_parser_error (parser, "expected %<::%>");
22175 return;
22176 case RT_LESS:
22177 cp_parser_error (parser, "expected %<<%>");
22178 return;
22179 case RT_GREATER:
22180 cp_parser_error (parser, "expected %<>%>");
22181 return;
22182 case RT_EQ:
22183 cp_parser_error (parser, "expected %<=%>");
22184 return;
22185 case RT_ELLIPSIS:
22186 cp_parser_error (parser, "expected %<...%>");
22187 return;
22188 case RT_MULT:
22189 cp_parser_error (parser, "expected %<*%>");
22190 return;
22191 case RT_COMPL:
22192 cp_parser_error (parser, "expected %<~%>");
22193 return;
22194 case RT_COLON:
22195 cp_parser_error (parser, "expected %<:%>");
22196 return;
22197 case RT_COLON_SCOPE:
22198 cp_parser_error (parser, "expected %<:%> or %<::%>");
22199 return;
22200 case RT_CLOSE_PAREN:
22201 cp_parser_error (parser, "expected %<)%>");
22202 return;
22203 case RT_COMMA_CLOSE_PAREN:
22204 cp_parser_error (parser, "expected %<,%> or %<)%>");
22205 return;
22206 case RT_PRAGMA_EOL:
22207 cp_parser_error (parser, "expected end of line");
22208 return;
22209 case RT_NAME:
22210 cp_parser_error (parser, "expected identifier");
22211 return;
22212 case RT_SELECT:
22213 cp_parser_error (parser, "expected selection-statement");
22214 return;
22215 case RT_INTERATION:
22216 cp_parser_error (parser, "expected iteration-statement");
22217 return;
22218 case RT_JUMP:
22219 cp_parser_error (parser, "expected jump-statement");
22220 return;
22221 case RT_CLASS_KEY:
22222 cp_parser_error (parser, "expected class-key");
22223 return;
22224 case RT_CLASS_TYPENAME_TEMPLATE:
22225 cp_parser_error (parser,
22226 "expected %<class%>, %<typename%>, or %<template%>");
22227 return;
22228 default:
22229 gcc_unreachable ();
22230 }
22231 }
22232 else
22233 gcc_unreachable ();
22234 }
22235
22236
22237
22238 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22239 issue an error message indicating that TOKEN_DESC was expected.
22240
22241 Returns the token consumed, if the token had the appropriate type.
22242 Otherwise, returns NULL. */
22243
22244 static cp_token *
22245 cp_parser_require (cp_parser* parser,
22246 enum cpp_ttype type,
22247 required_token token_desc)
22248 {
22249 if (cp_lexer_next_token_is (parser->lexer, type))
22250 return cp_lexer_consume_token (parser->lexer);
22251 else
22252 {
22253 /* Output the MESSAGE -- unless we're parsing tentatively. */
22254 if (!cp_parser_simulate_error (parser))
22255 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22256 return NULL;
22257 }
22258 }
22259
22260 /* An error message is produced if the next token is not '>'.
22261 All further tokens are skipped until the desired token is
22262 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22263
22264 static void
22265 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22266 {
22267 /* Current level of '< ... >'. */
22268 unsigned level = 0;
22269 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22270 unsigned nesting_depth = 0;
22271
22272 /* Are we ready, yet? If not, issue error message. */
22273 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22274 return;
22275
22276 /* Skip tokens until the desired token is found. */
22277 while (true)
22278 {
22279 /* Peek at the next token. */
22280 switch (cp_lexer_peek_token (parser->lexer)->type)
22281 {
22282 case CPP_LESS:
22283 if (!nesting_depth)
22284 ++level;
22285 break;
22286
22287 case CPP_RSHIFT:
22288 if (cxx_dialect == cxx98)
22289 /* C++0x views the `>>' operator as two `>' tokens, but
22290 C++98 does not. */
22291 break;
22292 else if (!nesting_depth && level-- == 0)
22293 {
22294 /* We've hit a `>>' where the first `>' closes the
22295 template argument list, and the second `>' is
22296 spurious. Just consume the `>>' and stop; we've
22297 already produced at least one error. */
22298 cp_lexer_consume_token (parser->lexer);
22299 return;
22300 }
22301 /* Fall through for C++0x, so we handle the second `>' in
22302 the `>>'. */
22303
22304 case CPP_GREATER:
22305 if (!nesting_depth && level-- == 0)
22306 {
22307 /* We've reached the token we want, consume it and stop. */
22308 cp_lexer_consume_token (parser->lexer);
22309 return;
22310 }
22311 break;
22312
22313 case CPP_OPEN_PAREN:
22314 case CPP_OPEN_SQUARE:
22315 ++nesting_depth;
22316 break;
22317
22318 case CPP_CLOSE_PAREN:
22319 case CPP_CLOSE_SQUARE:
22320 if (nesting_depth-- == 0)
22321 return;
22322 break;
22323
22324 case CPP_EOF:
22325 case CPP_PRAGMA_EOL:
22326 case CPP_SEMICOLON:
22327 case CPP_OPEN_BRACE:
22328 case CPP_CLOSE_BRACE:
22329 /* The '>' was probably forgotten, don't look further. */
22330 return;
22331
22332 default:
22333 break;
22334 }
22335
22336 /* Consume this token. */
22337 cp_lexer_consume_token (parser->lexer);
22338 }
22339 }
22340
22341 /* If the next token is the indicated keyword, consume it. Otherwise,
22342 issue an error message indicating that TOKEN_DESC was expected.
22343
22344 Returns the token consumed, if the token had the appropriate type.
22345 Otherwise, returns NULL. */
22346
22347 static cp_token *
22348 cp_parser_require_keyword (cp_parser* parser,
22349 enum rid keyword,
22350 required_token token_desc)
22351 {
22352 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22353
22354 if (token && token->keyword != keyword)
22355 {
22356 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22357 return NULL;
22358 }
22359
22360 return token;
22361 }
22362
22363 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22364 function-definition. */
22365
22366 static bool
22367 cp_parser_token_starts_function_definition_p (cp_token* token)
22368 {
22369 return (/* An ordinary function-body begins with an `{'. */
22370 token->type == CPP_OPEN_BRACE
22371 /* A ctor-initializer begins with a `:'. */
22372 || token->type == CPP_COLON
22373 /* A function-try-block begins with `try'. */
22374 || token->keyword == RID_TRY
22375 /* A function-transaction-block begins with `__transaction_atomic'
22376 or `__transaction_relaxed'. */
22377 || token->keyword == RID_TRANSACTION_ATOMIC
22378 || token->keyword == RID_TRANSACTION_RELAXED
22379 /* The named return value extension begins with `return'. */
22380 || token->keyword == RID_RETURN);
22381 }
22382
22383 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22384 definition. */
22385
22386 static bool
22387 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22388 {
22389 cp_token *token;
22390
22391 token = cp_lexer_peek_token (parser->lexer);
22392 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22393 }
22394
22395 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22396 C++0x) ending a template-argument. */
22397
22398 static bool
22399 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22400 {
22401 cp_token *token;
22402
22403 token = cp_lexer_peek_token (parser->lexer);
22404 return (token->type == CPP_COMMA
22405 || token->type == CPP_GREATER
22406 || token->type == CPP_ELLIPSIS
22407 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22408 }
22409
22410 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22411 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22412
22413 static bool
22414 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22415 size_t n)
22416 {
22417 cp_token *token;
22418
22419 token = cp_lexer_peek_nth_token (parser->lexer, n);
22420 if (token->type == CPP_LESS)
22421 return true;
22422 /* Check for the sequence `<::' in the original code. It would be lexed as
22423 `[:', where `[' is a digraph, and there is no whitespace before
22424 `:'. */
22425 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22426 {
22427 cp_token *token2;
22428 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22429 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22430 return true;
22431 }
22432 return false;
22433 }
22434
22435 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22436 or none_type otherwise. */
22437
22438 static enum tag_types
22439 cp_parser_token_is_class_key (cp_token* token)
22440 {
22441 switch (token->keyword)
22442 {
22443 case RID_CLASS:
22444 return class_type;
22445 case RID_STRUCT:
22446 return record_type;
22447 case RID_UNION:
22448 return union_type;
22449
22450 default:
22451 return none_type;
22452 }
22453 }
22454
22455 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22456
22457 static void
22458 cp_parser_check_class_key (enum tag_types class_key, tree type)
22459 {
22460 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22461 permerror (input_location, "%qs tag used in naming %q#T",
22462 class_key == union_type ? "union"
22463 : class_key == record_type ? "struct" : "class",
22464 type);
22465 }
22466
22467 /* Issue an error message if DECL is redeclared with different
22468 access than its original declaration [class.access.spec/3].
22469 This applies to nested classes and nested class templates.
22470 [class.mem/1]. */
22471
22472 static void
22473 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22474 {
22475 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22476 return;
22477
22478 if ((TREE_PRIVATE (decl)
22479 != (current_access_specifier == access_private_node))
22480 || (TREE_PROTECTED (decl)
22481 != (current_access_specifier == access_protected_node)))
22482 error_at (location, "%qD redeclared with different access", decl);
22483 }
22484
22485 /* Look for the `template' keyword, as a syntactic disambiguator.
22486 Return TRUE iff it is present, in which case it will be
22487 consumed. */
22488
22489 static bool
22490 cp_parser_optional_template_keyword (cp_parser *parser)
22491 {
22492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22493 {
22494 /* The `template' keyword can only be used within templates;
22495 outside templates the parser can always figure out what is a
22496 template and what is not. */
22497 if (!processing_template_decl)
22498 {
22499 cp_token *token = cp_lexer_peek_token (parser->lexer);
22500 error_at (token->location,
22501 "%<template%> (as a disambiguator) is only allowed "
22502 "within templates");
22503 /* If this part of the token stream is rescanned, the same
22504 error message would be generated. So, we purge the token
22505 from the stream. */
22506 cp_lexer_purge_token (parser->lexer);
22507 return false;
22508 }
22509 else
22510 {
22511 /* Consume the `template' keyword. */
22512 cp_lexer_consume_token (parser->lexer);
22513 return true;
22514 }
22515 }
22516
22517 return false;
22518 }
22519
22520 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22521 set PARSER->SCOPE, and perform other related actions. */
22522
22523 static void
22524 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22525 {
22526 int i;
22527 struct tree_check *check_value;
22528 deferred_access_check *chk;
22529 VEC (deferred_access_check,gc) *checks;
22530
22531 /* Get the stored value. */
22532 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22533 /* Perform any access checks that were deferred. */
22534 checks = check_value->checks;
22535 if (checks)
22536 {
22537 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22538 perform_or_defer_access_check (chk->binfo,
22539 chk->decl,
22540 chk->diag_decl);
22541 }
22542 /* Set the scope from the stored value. */
22543 parser->scope = check_value->value;
22544 parser->qualifying_scope = check_value->qualifying_scope;
22545 parser->object_scope = NULL_TREE;
22546 }
22547
22548 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22549 encounter the end of a block before what we were looking for. */
22550
22551 static bool
22552 cp_parser_cache_group (cp_parser *parser,
22553 enum cpp_ttype end,
22554 unsigned depth)
22555 {
22556 while (true)
22557 {
22558 cp_token *token = cp_lexer_peek_token (parser->lexer);
22559
22560 /* Abort a parenthesized expression if we encounter a semicolon. */
22561 if ((end == CPP_CLOSE_PAREN || depth == 0)
22562 && token->type == CPP_SEMICOLON)
22563 return true;
22564 /* If we've reached the end of the file, stop. */
22565 if (token->type == CPP_EOF
22566 || (end != CPP_PRAGMA_EOL
22567 && token->type == CPP_PRAGMA_EOL))
22568 return true;
22569 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22570 /* We've hit the end of an enclosing block, so there's been some
22571 kind of syntax error. */
22572 return true;
22573
22574 /* If we're caching something finished by a comma (or semicolon),
22575 such as an NSDMI, don't consume the comma. */
22576 if (end == CPP_COMMA
22577 && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22578 return false;
22579
22580 /* Consume the token. */
22581 cp_lexer_consume_token (parser->lexer);
22582 /* See if it starts a new group. */
22583 if (token->type == CPP_OPEN_BRACE)
22584 {
22585 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22586 /* In theory this should probably check end == '}', but
22587 cp_parser_save_member_function_body needs it to exit
22588 after either '}' or ')' when called with ')'. */
22589 if (depth == 0)
22590 return false;
22591 }
22592 else if (token->type == CPP_OPEN_PAREN)
22593 {
22594 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22595 if (depth == 0 && end == CPP_CLOSE_PAREN)
22596 return false;
22597 }
22598 else if (token->type == CPP_PRAGMA)
22599 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22600 else if (token->type == end)
22601 return false;
22602 }
22603 }
22604
22605 /* Begin parsing tentatively. We always save tokens while parsing
22606 tentatively so that if the tentative parsing fails we can restore the
22607 tokens. */
22608
22609 static void
22610 cp_parser_parse_tentatively (cp_parser* parser)
22611 {
22612 /* Enter a new parsing context. */
22613 parser->context = cp_parser_context_new (parser->context);
22614 /* Begin saving tokens. */
22615 cp_lexer_save_tokens (parser->lexer);
22616 /* In order to avoid repetitive access control error messages,
22617 access checks are queued up until we are no longer parsing
22618 tentatively. */
22619 push_deferring_access_checks (dk_deferred);
22620 }
22621
22622 /* Commit to the currently active tentative parse. */
22623
22624 static void
22625 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22626 {
22627 cp_parser_context *context;
22628 cp_lexer *lexer;
22629
22630 /* Mark all of the levels as committed. */
22631 lexer = parser->lexer;
22632 for (context = parser->context; context->next; context = context->next)
22633 {
22634 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22635 break;
22636 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22637 while (!cp_lexer_saving_tokens (lexer))
22638 lexer = lexer->next;
22639 cp_lexer_commit_tokens (lexer);
22640 }
22641 }
22642
22643 /* Abort the currently active tentative parse. All consumed tokens
22644 will be rolled back, and no diagnostics will be issued. */
22645
22646 static void
22647 cp_parser_abort_tentative_parse (cp_parser* parser)
22648 {
22649 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22650 || errorcount > 0);
22651 cp_parser_simulate_error (parser);
22652 /* Now, pretend that we want to see if the construct was
22653 successfully parsed. */
22654 cp_parser_parse_definitely (parser);
22655 }
22656
22657 /* Stop parsing tentatively. If a parse error has occurred, restore the
22658 token stream. Otherwise, commit to the tokens we have consumed.
22659 Returns true if no error occurred; false otherwise. */
22660
22661 static bool
22662 cp_parser_parse_definitely (cp_parser* parser)
22663 {
22664 bool error_occurred;
22665 cp_parser_context *context;
22666
22667 /* Remember whether or not an error occurred, since we are about to
22668 destroy that information. */
22669 error_occurred = cp_parser_error_occurred (parser);
22670 /* Remove the topmost context from the stack. */
22671 context = parser->context;
22672 parser->context = context->next;
22673 /* If no parse errors occurred, commit to the tentative parse. */
22674 if (!error_occurred)
22675 {
22676 /* Commit to the tokens read tentatively, unless that was
22677 already done. */
22678 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22679 cp_lexer_commit_tokens (parser->lexer);
22680
22681 pop_to_parent_deferring_access_checks ();
22682 }
22683 /* Otherwise, if errors occurred, roll back our state so that things
22684 are just as they were before we began the tentative parse. */
22685 else
22686 {
22687 cp_lexer_rollback_tokens (parser->lexer);
22688 pop_deferring_access_checks ();
22689 }
22690 /* Add the context to the front of the free list. */
22691 context->next = cp_parser_context_free_list;
22692 cp_parser_context_free_list = context;
22693
22694 return !error_occurred;
22695 }
22696
22697 /* Returns true if we are parsing tentatively and are not committed to
22698 this tentative parse. */
22699
22700 static bool
22701 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22702 {
22703 return (cp_parser_parsing_tentatively (parser)
22704 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22705 }
22706
22707 /* Returns nonzero iff an error has occurred during the most recent
22708 tentative parse. */
22709
22710 static bool
22711 cp_parser_error_occurred (cp_parser* parser)
22712 {
22713 return (cp_parser_parsing_tentatively (parser)
22714 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22715 }
22716
22717 /* Returns nonzero if GNU extensions are allowed. */
22718
22719 static bool
22720 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22721 {
22722 return parser->allow_gnu_extensions_p;
22723 }
22724 \f
22725 /* Objective-C++ Productions */
22726
22727
22728 /* Parse an Objective-C expression, which feeds into a primary-expression
22729 above.
22730
22731 objc-expression:
22732 objc-message-expression
22733 objc-string-literal
22734 objc-encode-expression
22735 objc-protocol-expression
22736 objc-selector-expression
22737
22738 Returns a tree representation of the expression. */
22739
22740 static tree
22741 cp_parser_objc_expression (cp_parser* parser)
22742 {
22743 /* Try to figure out what kind of declaration is present. */
22744 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22745
22746 switch (kwd->type)
22747 {
22748 case CPP_OPEN_SQUARE:
22749 return cp_parser_objc_message_expression (parser);
22750
22751 case CPP_OBJC_STRING:
22752 kwd = cp_lexer_consume_token (parser->lexer);
22753 return objc_build_string_object (kwd->u.value);
22754
22755 case CPP_KEYWORD:
22756 switch (kwd->keyword)
22757 {
22758 case RID_AT_ENCODE:
22759 return cp_parser_objc_encode_expression (parser);
22760
22761 case RID_AT_PROTOCOL:
22762 return cp_parser_objc_protocol_expression (parser);
22763
22764 case RID_AT_SELECTOR:
22765 return cp_parser_objc_selector_expression (parser);
22766
22767 default:
22768 break;
22769 }
22770 default:
22771 error_at (kwd->location,
22772 "misplaced %<@%D%> Objective-C++ construct",
22773 kwd->u.value);
22774 cp_parser_skip_to_end_of_block_or_statement (parser);
22775 }
22776
22777 return error_mark_node;
22778 }
22779
22780 /* Parse an Objective-C message expression.
22781
22782 objc-message-expression:
22783 [ objc-message-receiver objc-message-args ]
22784
22785 Returns a representation of an Objective-C message. */
22786
22787 static tree
22788 cp_parser_objc_message_expression (cp_parser* parser)
22789 {
22790 tree receiver, messageargs;
22791
22792 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
22793 receiver = cp_parser_objc_message_receiver (parser);
22794 messageargs = cp_parser_objc_message_args (parser);
22795 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22796
22797 return objc_build_message_expr (receiver, messageargs);
22798 }
22799
22800 /* Parse an objc-message-receiver.
22801
22802 objc-message-receiver:
22803 expression
22804 simple-type-specifier
22805
22806 Returns a representation of the type or expression. */
22807
22808 static tree
22809 cp_parser_objc_message_receiver (cp_parser* parser)
22810 {
22811 tree rcv;
22812
22813 /* An Objective-C message receiver may be either (1) a type
22814 or (2) an expression. */
22815 cp_parser_parse_tentatively (parser);
22816 rcv = cp_parser_expression (parser, false, NULL);
22817
22818 if (cp_parser_parse_definitely (parser))
22819 return rcv;
22820
22821 rcv = cp_parser_simple_type_specifier (parser,
22822 /*decl_specs=*/NULL,
22823 CP_PARSER_FLAGS_NONE);
22824
22825 return objc_get_class_reference (rcv);
22826 }
22827
22828 /* Parse the arguments and selectors comprising an Objective-C message.
22829
22830 objc-message-args:
22831 objc-selector
22832 objc-selector-args
22833 objc-selector-args , objc-comma-args
22834
22835 objc-selector-args:
22836 objc-selector [opt] : assignment-expression
22837 objc-selector-args objc-selector [opt] : assignment-expression
22838
22839 objc-comma-args:
22840 assignment-expression
22841 objc-comma-args , assignment-expression
22842
22843 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22844 selector arguments and TREE_VALUE containing a list of comma
22845 arguments. */
22846
22847 static tree
22848 cp_parser_objc_message_args (cp_parser* parser)
22849 {
22850 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22851 bool maybe_unary_selector_p = true;
22852 cp_token *token = cp_lexer_peek_token (parser->lexer);
22853
22854 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22855 {
22856 tree selector = NULL_TREE, arg;
22857
22858 if (token->type != CPP_COLON)
22859 selector = cp_parser_objc_selector (parser);
22860
22861 /* Detect if we have a unary selector. */
22862 if (maybe_unary_selector_p
22863 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22864 return build_tree_list (selector, NULL_TREE);
22865
22866 maybe_unary_selector_p = false;
22867 cp_parser_require (parser, CPP_COLON, RT_COLON);
22868 arg = cp_parser_assignment_expression (parser, false, NULL);
22869
22870 sel_args
22871 = chainon (sel_args,
22872 build_tree_list (selector, arg));
22873
22874 token = cp_lexer_peek_token (parser->lexer);
22875 }
22876
22877 /* Handle non-selector arguments, if any. */
22878 while (token->type == CPP_COMMA)
22879 {
22880 tree arg;
22881
22882 cp_lexer_consume_token (parser->lexer);
22883 arg = cp_parser_assignment_expression (parser, false, NULL);
22884
22885 addl_args
22886 = chainon (addl_args,
22887 build_tree_list (NULL_TREE, arg));
22888
22889 token = cp_lexer_peek_token (parser->lexer);
22890 }
22891
22892 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22893 {
22894 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22895 return build_tree_list (error_mark_node, error_mark_node);
22896 }
22897
22898 return build_tree_list (sel_args, addl_args);
22899 }
22900
22901 /* Parse an Objective-C encode expression.
22902
22903 objc-encode-expression:
22904 @encode objc-typename
22905
22906 Returns an encoded representation of the type argument. */
22907
22908 static tree
22909 cp_parser_objc_encode_expression (cp_parser* parser)
22910 {
22911 tree type;
22912 cp_token *token;
22913
22914 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
22915 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22916 token = cp_lexer_peek_token (parser->lexer);
22917 type = complete_type (cp_parser_type_id (parser));
22918 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22919
22920 if (!type)
22921 {
22922 error_at (token->location,
22923 "%<@encode%> must specify a type as an argument");
22924 return error_mark_node;
22925 }
22926
22927 /* This happens if we find @encode(T) (where T is a template
22928 typename or something dependent on a template typename) when
22929 parsing a template. In that case, we can't compile it
22930 immediately, but we rather create an AT_ENCODE_EXPR which will
22931 need to be instantiated when the template is used.
22932 */
22933 if (dependent_type_p (type))
22934 {
22935 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22936 TREE_READONLY (value) = 1;
22937 return value;
22938 }
22939
22940 return objc_build_encode_expr (type);
22941 }
22942
22943 /* Parse an Objective-C @defs expression. */
22944
22945 static tree
22946 cp_parser_objc_defs_expression (cp_parser *parser)
22947 {
22948 tree name;
22949
22950 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
22951 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22952 name = cp_parser_identifier (parser);
22953 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22954
22955 return objc_get_class_ivars (name);
22956 }
22957
22958 /* Parse an Objective-C protocol expression.
22959
22960 objc-protocol-expression:
22961 @protocol ( identifier )
22962
22963 Returns a representation of the protocol expression. */
22964
22965 static tree
22966 cp_parser_objc_protocol_expression (cp_parser* parser)
22967 {
22968 tree proto;
22969
22970 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22971 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22972 proto = cp_parser_identifier (parser);
22973 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22974
22975 return objc_build_protocol_expr (proto);
22976 }
22977
22978 /* Parse an Objective-C selector expression.
22979
22980 objc-selector-expression:
22981 @selector ( objc-method-signature )
22982
22983 objc-method-signature:
22984 objc-selector
22985 objc-selector-seq
22986
22987 objc-selector-seq:
22988 objc-selector :
22989 objc-selector-seq objc-selector :
22990
22991 Returns a representation of the method selector. */
22992
22993 static tree
22994 cp_parser_objc_selector_expression (cp_parser* parser)
22995 {
22996 tree sel_seq = NULL_TREE;
22997 bool maybe_unary_selector_p = true;
22998 cp_token *token;
22999 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23000
23001 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23002 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23003 token = cp_lexer_peek_token (parser->lexer);
23004
23005 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23006 || token->type == CPP_SCOPE)
23007 {
23008 tree selector = NULL_TREE;
23009
23010 if (token->type != CPP_COLON
23011 || token->type == CPP_SCOPE)
23012 selector = cp_parser_objc_selector (parser);
23013
23014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23015 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23016 {
23017 /* Detect if we have a unary selector. */
23018 if (maybe_unary_selector_p)
23019 {
23020 sel_seq = selector;
23021 goto finish_selector;
23022 }
23023 else
23024 {
23025 cp_parser_error (parser, "expected %<:%>");
23026 }
23027 }
23028 maybe_unary_selector_p = false;
23029 token = cp_lexer_consume_token (parser->lexer);
23030
23031 if (token->type == CPP_SCOPE)
23032 {
23033 sel_seq
23034 = chainon (sel_seq,
23035 build_tree_list (selector, NULL_TREE));
23036 sel_seq
23037 = chainon (sel_seq,
23038 build_tree_list (NULL_TREE, NULL_TREE));
23039 }
23040 else
23041 sel_seq
23042 = chainon (sel_seq,
23043 build_tree_list (selector, NULL_TREE));
23044
23045 token = cp_lexer_peek_token (parser->lexer);
23046 }
23047
23048 finish_selector:
23049 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23050
23051 return objc_build_selector_expr (loc, sel_seq);
23052 }
23053
23054 /* Parse a list of identifiers.
23055
23056 objc-identifier-list:
23057 identifier
23058 objc-identifier-list , identifier
23059
23060 Returns a TREE_LIST of identifier nodes. */
23061
23062 static tree
23063 cp_parser_objc_identifier_list (cp_parser* parser)
23064 {
23065 tree identifier;
23066 tree list;
23067 cp_token *sep;
23068
23069 identifier = cp_parser_identifier (parser);
23070 if (identifier == error_mark_node)
23071 return error_mark_node;
23072
23073 list = build_tree_list (NULL_TREE, identifier);
23074 sep = cp_lexer_peek_token (parser->lexer);
23075
23076 while (sep->type == CPP_COMMA)
23077 {
23078 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23079 identifier = cp_parser_identifier (parser);
23080 if (identifier == error_mark_node)
23081 return list;
23082
23083 list = chainon (list, build_tree_list (NULL_TREE,
23084 identifier));
23085 sep = cp_lexer_peek_token (parser->lexer);
23086 }
23087
23088 return list;
23089 }
23090
23091 /* Parse an Objective-C alias declaration.
23092
23093 objc-alias-declaration:
23094 @compatibility_alias identifier identifier ;
23095
23096 This function registers the alias mapping with the Objective-C front end.
23097 It returns nothing. */
23098
23099 static void
23100 cp_parser_objc_alias_declaration (cp_parser* parser)
23101 {
23102 tree alias, orig;
23103
23104 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23105 alias = cp_parser_identifier (parser);
23106 orig = cp_parser_identifier (parser);
23107 objc_declare_alias (alias, orig);
23108 cp_parser_consume_semicolon_at_end_of_statement (parser);
23109 }
23110
23111 /* Parse an Objective-C class forward-declaration.
23112
23113 objc-class-declaration:
23114 @class objc-identifier-list ;
23115
23116 The function registers the forward declarations with the Objective-C
23117 front end. It returns nothing. */
23118
23119 static void
23120 cp_parser_objc_class_declaration (cp_parser* parser)
23121 {
23122 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23123 while (true)
23124 {
23125 tree id;
23126
23127 id = cp_parser_identifier (parser);
23128 if (id == error_mark_node)
23129 break;
23130
23131 objc_declare_class (id);
23132
23133 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23134 cp_lexer_consume_token (parser->lexer);
23135 else
23136 break;
23137 }
23138 cp_parser_consume_semicolon_at_end_of_statement (parser);
23139 }
23140
23141 /* Parse a list of Objective-C protocol references.
23142
23143 objc-protocol-refs-opt:
23144 objc-protocol-refs [opt]
23145
23146 objc-protocol-refs:
23147 < objc-identifier-list >
23148
23149 Returns a TREE_LIST of identifiers, if any. */
23150
23151 static tree
23152 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23153 {
23154 tree protorefs = NULL_TREE;
23155
23156 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23157 {
23158 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23159 protorefs = cp_parser_objc_identifier_list (parser);
23160 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23161 }
23162
23163 return protorefs;
23164 }
23165
23166 /* Parse a Objective-C visibility specification. */
23167
23168 static void
23169 cp_parser_objc_visibility_spec (cp_parser* parser)
23170 {
23171 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23172
23173 switch (vis->keyword)
23174 {
23175 case RID_AT_PRIVATE:
23176 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23177 break;
23178 case RID_AT_PROTECTED:
23179 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23180 break;
23181 case RID_AT_PUBLIC:
23182 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23183 break;
23184 case RID_AT_PACKAGE:
23185 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23186 break;
23187 default:
23188 return;
23189 }
23190
23191 /* Eat '@private'/'@protected'/'@public'. */
23192 cp_lexer_consume_token (parser->lexer);
23193 }
23194
23195 /* Parse an Objective-C method type. Return 'true' if it is a class
23196 (+) method, and 'false' if it is an instance (-) method. */
23197
23198 static inline bool
23199 cp_parser_objc_method_type (cp_parser* parser)
23200 {
23201 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23202 return true;
23203 else
23204 return false;
23205 }
23206
23207 /* Parse an Objective-C protocol qualifier. */
23208
23209 static tree
23210 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23211 {
23212 tree quals = NULL_TREE, node;
23213 cp_token *token = cp_lexer_peek_token (parser->lexer);
23214
23215 node = token->u.value;
23216
23217 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23218 && (node == ridpointers [(int) RID_IN]
23219 || node == ridpointers [(int) RID_OUT]
23220 || node == ridpointers [(int) RID_INOUT]
23221 || node == ridpointers [(int) RID_BYCOPY]
23222 || node == ridpointers [(int) RID_BYREF]
23223 || node == ridpointers [(int) RID_ONEWAY]))
23224 {
23225 quals = tree_cons (NULL_TREE, node, quals);
23226 cp_lexer_consume_token (parser->lexer);
23227 token = cp_lexer_peek_token (parser->lexer);
23228 node = token->u.value;
23229 }
23230
23231 return quals;
23232 }
23233
23234 /* Parse an Objective-C typename. */
23235
23236 static tree
23237 cp_parser_objc_typename (cp_parser* parser)
23238 {
23239 tree type_name = NULL_TREE;
23240
23241 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23242 {
23243 tree proto_quals, cp_type = NULL_TREE;
23244
23245 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23246 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23247
23248 /* An ObjC type name may consist of just protocol qualifiers, in which
23249 case the type shall default to 'id'. */
23250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23251 {
23252 cp_type = cp_parser_type_id (parser);
23253
23254 /* If the type could not be parsed, an error has already
23255 been produced. For error recovery, behave as if it had
23256 not been specified, which will use the default type
23257 'id'. */
23258 if (cp_type == error_mark_node)
23259 {
23260 cp_type = NULL_TREE;
23261 /* We need to skip to the closing parenthesis as
23262 cp_parser_type_id() does not seem to do it for
23263 us. */
23264 cp_parser_skip_to_closing_parenthesis (parser,
23265 /*recovering=*/true,
23266 /*or_comma=*/false,
23267 /*consume_paren=*/false);
23268 }
23269 }
23270
23271 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23272 type_name = build_tree_list (proto_quals, cp_type);
23273 }
23274
23275 return type_name;
23276 }
23277
23278 /* Check to see if TYPE refers to an Objective-C selector name. */
23279
23280 static bool
23281 cp_parser_objc_selector_p (enum cpp_ttype type)
23282 {
23283 return (type == CPP_NAME || type == CPP_KEYWORD
23284 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23285 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23286 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23287 || type == CPP_XOR || type == CPP_XOR_EQ);
23288 }
23289
23290 /* Parse an Objective-C selector. */
23291
23292 static tree
23293 cp_parser_objc_selector (cp_parser* parser)
23294 {
23295 cp_token *token = cp_lexer_consume_token (parser->lexer);
23296
23297 if (!cp_parser_objc_selector_p (token->type))
23298 {
23299 error_at (token->location, "invalid Objective-C++ selector name");
23300 return error_mark_node;
23301 }
23302
23303 /* C++ operator names are allowed to appear in ObjC selectors. */
23304 switch (token->type)
23305 {
23306 case CPP_AND_AND: return get_identifier ("and");
23307 case CPP_AND_EQ: return get_identifier ("and_eq");
23308 case CPP_AND: return get_identifier ("bitand");
23309 case CPP_OR: return get_identifier ("bitor");
23310 case CPP_COMPL: return get_identifier ("compl");
23311 case CPP_NOT: return get_identifier ("not");
23312 case CPP_NOT_EQ: return get_identifier ("not_eq");
23313 case CPP_OR_OR: return get_identifier ("or");
23314 case CPP_OR_EQ: return get_identifier ("or_eq");
23315 case CPP_XOR: return get_identifier ("xor");
23316 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23317 default: return token->u.value;
23318 }
23319 }
23320
23321 /* Parse an Objective-C params list. */
23322
23323 static tree
23324 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23325 {
23326 tree params = NULL_TREE;
23327 bool maybe_unary_selector_p = true;
23328 cp_token *token = cp_lexer_peek_token (parser->lexer);
23329
23330 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23331 {
23332 tree selector = NULL_TREE, type_name, identifier;
23333 tree parm_attr = NULL_TREE;
23334
23335 if (token->keyword == RID_ATTRIBUTE)
23336 break;
23337
23338 if (token->type != CPP_COLON)
23339 selector = cp_parser_objc_selector (parser);
23340
23341 /* Detect if we have a unary selector. */
23342 if (maybe_unary_selector_p
23343 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23344 {
23345 params = selector; /* Might be followed by attributes. */
23346 break;
23347 }
23348
23349 maybe_unary_selector_p = false;
23350 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23351 {
23352 /* Something went quite wrong. There should be a colon
23353 here, but there is not. Stop parsing parameters. */
23354 break;
23355 }
23356 type_name = cp_parser_objc_typename (parser);
23357 /* New ObjC allows attributes on parameters too. */
23358 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23359 parm_attr = cp_parser_attributes_opt (parser);
23360 identifier = cp_parser_identifier (parser);
23361
23362 params
23363 = chainon (params,
23364 objc_build_keyword_decl (selector,
23365 type_name,
23366 identifier,
23367 parm_attr));
23368
23369 token = cp_lexer_peek_token (parser->lexer);
23370 }
23371
23372 if (params == NULL_TREE)
23373 {
23374 cp_parser_error (parser, "objective-c++ method declaration is expected");
23375 return error_mark_node;
23376 }
23377
23378 /* We allow tail attributes for the method. */
23379 if (token->keyword == RID_ATTRIBUTE)
23380 {
23381 *attributes = cp_parser_attributes_opt (parser);
23382 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23383 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23384 return params;
23385 cp_parser_error (parser,
23386 "method attributes must be specified at the end");
23387 return error_mark_node;
23388 }
23389
23390 if (params == NULL_TREE)
23391 {
23392 cp_parser_error (parser, "objective-c++ method declaration is expected");
23393 return error_mark_node;
23394 }
23395 return params;
23396 }
23397
23398 /* Parse the non-keyword Objective-C params. */
23399
23400 static tree
23401 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23402 tree* attributes)
23403 {
23404 tree params = make_node (TREE_LIST);
23405 cp_token *token = cp_lexer_peek_token (parser->lexer);
23406 *ellipsisp = false; /* Initially, assume no ellipsis. */
23407
23408 while (token->type == CPP_COMMA)
23409 {
23410 cp_parameter_declarator *parmdecl;
23411 tree parm;
23412
23413 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23414 token = cp_lexer_peek_token (parser->lexer);
23415
23416 if (token->type == CPP_ELLIPSIS)
23417 {
23418 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23419 *ellipsisp = true;
23420 token = cp_lexer_peek_token (parser->lexer);
23421 break;
23422 }
23423
23424 /* TODO: parse attributes for tail parameters. */
23425 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23426 parm = grokdeclarator (parmdecl->declarator,
23427 &parmdecl->decl_specifiers,
23428 PARM, /*initialized=*/0,
23429 /*attrlist=*/NULL);
23430
23431 chainon (params, build_tree_list (NULL_TREE, parm));
23432 token = cp_lexer_peek_token (parser->lexer);
23433 }
23434
23435 /* We allow tail attributes for the method. */
23436 if (token->keyword == RID_ATTRIBUTE)
23437 {
23438 if (*attributes == NULL_TREE)
23439 {
23440 *attributes = cp_parser_attributes_opt (parser);
23441 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23442 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23443 return params;
23444 }
23445 else
23446 /* We have an error, but parse the attributes, so that we can
23447 carry on. */
23448 *attributes = cp_parser_attributes_opt (parser);
23449
23450 cp_parser_error (parser,
23451 "method attributes must be specified at the end");
23452 return error_mark_node;
23453 }
23454
23455 return params;
23456 }
23457
23458 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23459
23460 static void
23461 cp_parser_objc_interstitial_code (cp_parser* parser)
23462 {
23463 cp_token *token = cp_lexer_peek_token (parser->lexer);
23464
23465 /* If the next token is `extern' and the following token is a string
23466 literal, then we have a linkage specification. */
23467 if (token->keyword == RID_EXTERN
23468 && cp_parser_is_pure_string_literal
23469 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23470 cp_parser_linkage_specification (parser);
23471 /* Handle #pragma, if any. */
23472 else if (token->type == CPP_PRAGMA)
23473 cp_parser_pragma (parser, pragma_external);
23474 /* Allow stray semicolons. */
23475 else if (token->type == CPP_SEMICOLON)
23476 cp_lexer_consume_token (parser->lexer);
23477 /* Mark methods as optional or required, when building protocols. */
23478 else if (token->keyword == RID_AT_OPTIONAL)
23479 {
23480 cp_lexer_consume_token (parser->lexer);
23481 objc_set_method_opt (true);
23482 }
23483 else if (token->keyword == RID_AT_REQUIRED)
23484 {
23485 cp_lexer_consume_token (parser->lexer);
23486 objc_set_method_opt (false);
23487 }
23488 else if (token->keyword == RID_NAMESPACE)
23489 cp_parser_namespace_definition (parser);
23490 /* Other stray characters must generate errors. */
23491 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23492 {
23493 cp_lexer_consume_token (parser->lexer);
23494 error ("stray %qs between Objective-C++ methods",
23495 token->type == CPP_OPEN_BRACE ? "{" : "}");
23496 }
23497 /* Finally, try to parse a block-declaration, or a function-definition. */
23498 else
23499 cp_parser_block_declaration (parser, /*statement_p=*/false);
23500 }
23501
23502 /* Parse a method signature. */
23503
23504 static tree
23505 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23506 {
23507 tree rettype, kwdparms, optparms;
23508 bool ellipsis = false;
23509 bool is_class_method;
23510
23511 is_class_method = cp_parser_objc_method_type (parser);
23512 rettype = cp_parser_objc_typename (parser);
23513 *attributes = NULL_TREE;
23514 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23515 if (kwdparms == error_mark_node)
23516 return error_mark_node;
23517 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23518 if (optparms == error_mark_node)
23519 return error_mark_node;
23520
23521 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23522 }
23523
23524 static bool
23525 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23526 {
23527 tree tattr;
23528 cp_lexer_save_tokens (parser->lexer);
23529 tattr = cp_parser_attributes_opt (parser);
23530 gcc_assert (tattr) ;
23531
23532 /* If the attributes are followed by a method introducer, this is not allowed.
23533 Dump the attributes and flag the situation. */
23534 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23535 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23536 return true;
23537
23538 /* Otherwise, the attributes introduce some interstitial code, possibly so
23539 rewind to allow that check. */
23540 cp_lexer_rollback_tokens (parser->lexer);
23541 return false;
23542 }
23543
23544 /* Parse an Objective-C method prototype list. */
23545
23546 static void
23547 cp_parser_objc_method_prototype_list (cp_parser* parser)
23548 {
23549 cp_token *token = cp_lexer_peek_token (parser->lexer);
23550
23551 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23552 {
23553 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23554 {
23555 tree attributes, sig;
23556 bool is_class_method;
23557 if (token->type == CPP_PLUS)
23558 is_class_method = true;
23559 else
23560 is_class_method = false;
23561 sig = cp_parser_objc_method_signature (parser, &attributes);
23562 if (sig == error_mark_node)
23563 {
23564 cp_parser_skip_to_end_of_block_or_statement (parser);
23565 token = cp_lexer_peek_token (parser->lexer);
23566 continue;
23567 }
23568 objc_add_method_declaration (is_class_method, sig, attributes);
23569 cp_parser_consume_semicolon_at_end_of_statement (parser);
23570 }
23571 else if (token->keyword == RID_AT_PROPERTY)
23572 cp_parser_objc_at_property_declaration (parser);
23573 else if (token->keyword == RID_ATTRIBUTE
23574 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23575 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23576 OPT_Wattributes,
23577 "prefix attributes are ignored for methods");
23578 else
23579 /* Allow for interspersed non-ObjC++ code. */
23580 cp_parser_objc_interstitial_code (parser);
23581
23582 token = cp_lexer_peek_token (parser->lexer);
23583 }
23584
23585 if (token->type != CPP_EOF)
23586 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23587 else
23588 cp_parser_error (parser, "expected %<@end%>");
23589
23590 objc_finish_interface ();
23591 }
23592
23593 /* Parse an Objective-C method definition list. */
23594
23595 static void
23596 cp_parser_objc_method_definition_list (cp_parser* parser)
23597 {
23598 cp_token *token = cp_lexer_peek_token (parser->lexer);
23599
23600 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23601 {
23602 tree meth;
23603
23604 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23605 {
23606 cp_token *ptk;
23607 tree sig, attribute;
23608 bool is_class_method;
23609 if (token->type == CPP_PLUS)
23610 is_class_method = true;
23611 else
23612 is_class_method = false;
23613 push_deferring_access_checks (dk_deferred);
23614 sig = cp_parser_objc_method_signature (parser, &attribute);
23615 if (sig == error_mark_node)
23616 {
23617 cp_parser_skip_to_end_of_block_or_statement (parser);
23618 token = cp_lexer_peek_token (parser->lexer);
23619 continue;
23620 }
23621 objc_start_method_definition (is_class_method, sig, attribute,
23622 NULL_TREE);
23623
23624 /* For historical reasons, we accept an optional semicolon. */
23625 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23626 cp_lexer_consume_token (parser->lexer);
23627
23628 ptk = cp_lexer_peek_token (parser->lexer);
23629 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23630 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23631 {
23632 perform_deferred_access_checks ();
23633 stop_deferring_access_checks ();
23634 meth = cp_parser_function_definition_after_declarator (parser,
23635 false);
23636 pop_deferring_access_checks ();
23637 objc_finish_method_definition (meth);
23638 }
23639 }
23640 /* The following case will be removed once @synthesize is
23641 completely implemented. */
23642 else if (token->keyword == RID_AT_PROPERTY)
23643 cp_parser_objc_at_property_declaration (parser);
23644 else if (token->keyword == RID_AT_SYNTHESIZE)
23645 cp_parser_objc_at_synthesize_declaration (parser);
23646 else if (token->keyword == RID_AT_DYNAMIC)
23647 cp_parser_objc_at_dynamic_declaration (parser);
23648 else if (token->keyword == RID_ATTRIBUTE
23649 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23650 warning_at (token->location, OPT_Wattributes,
23651 "prefix attributes are ignored for methods");
23652 else
23653 /* Allow for interspersed non-ObjC++ code. */
23654 cp_parser_objc_interstitial_code (parser);
23655
23656 token = cp_lexer_peek_token (parser->lexer);
23657 }
23658
23659 if (token->type != CPP_EOF)
23660 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23661 else
23662 cp_parser_error (parser, "expected %<@end%>");
23663
23664 objc_finish_implementation ();
23665 }
23666
23667 /* Parse Objective-C ivars. */
23668
23669 static void
23670 cp_parser_objc_class_ivars (cp_parser* parser)
23671 {
23672 cp_token *token = cp_lexer_peek_token (parser->lexer);
23673
23674 if (token->type != CPP_OPEN_BRACE)
23675 return; /* No ivars specified. */
23676
23677 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
23678 token = cp_lexer_peek_token (parser->lexer);
23679
23680 while (token->type != CPP_CLOSE_BRACE
23681 && token->keyword != RID_AT_END && token->type != CPP_EOF)
23682 {
23683 cp_decl_specifier_seq declspecs;
23684 int decl_class_or_enum_p;
23685 tree prefix_attributes;
23686
23687 cp_parser_objc_visibility_spec (parser);
23688
23689 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23690 break;
23691
23692 cp_parser_decl_specifier_seq (parser,
23693 CP_PARSER_FLAGS_OPTIONAL,
23694 &declspecs,
23695 &decl_class_or_enum_p);
23696
23697 /* auto, register, static, extern, mutable. */
23698 if (declspecs.storage_class != sc_none)
23699 {
23700 cp_parser_error (parser, "invalid type for instance variable");
23701 declspecs.storage_class = sc_none;
23702 }
23703
23704 /* __thread. */
23705 if (declspecs.specs[(int) ds_thread])
23706 {
23707 cp_parser_error (parser, "invalid type for instance variable");
23708 declspecs.specs[(int) ds_thread] = 0;
23709 }
23710
23711 /* typedef. */
23712 if (declspecs.specs[(int) ds_typedef])
23713 {
23714 cp_parser_error (parser, "invalid type for instance variable");
23715 declspecs.specs[(int) ds_typedef] = 0;
23716 }
23717
23718 prefix_attributes = declspecs.attributes;
23719 declspecs.attributes = NULL_TREE;
23720
23721 /* Keep going until we hit the `;' at the end of the
23722 declaration. */
23723 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23724 {
23725 tree width = NULL_TREE, attributes, first_attribute, decl;
23726 cp_declarator *declarator = NULL;
23727 int ctor_dtor_or_conv_p;
23728
23729 /* Check for a (possibly unnamed) bitfield declaration. */
23730 token = cp_lexer_peek_token (parser->lexer);
23731 if (token->type == CPP_COLON)
23732 goto eat_colon;
23733
23734 if (token->type == CPP_NAME
23735 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23736 == CPP_COLON))
23737 {
23738 /* Get the name of the bitfield. */
23739 declarator = make_id_declarator (NULL_TREE,
23740 cp_parser_identifier (parser),
23741 sfk_none);
23742
23743 eat_colon:
23744 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23745 /* Get the width of the bitfield. */
23746 width
23747 = cp_parser_constant_expression (parser,
23748 /*allow_non_constant=*/false,
23749 NULL);
23750 }
23751 else
23752 {
23753 /* Parse the declarator. */
23754 declarator
23755 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23756 &ctor_dtor_or_conv_p,
23757 /*parenthesized_p=*/NULL,
23758 /*member_p=*/false);
23759 }
23760
23761 /* Look for attributes that apply to the ivar. */
23762 attributes = cp_parser_attributes_opt (parser);
23763 /* Remember which attributes are prefix attributes and
23764 which are not. */
23765 first_attribute = attributes;
23766 /* Combine the attributes. */
23767 attributes = chainon (prefix_attributes, attributes);
23768
23769 if (width)
23770 /* Create the bitfield declaration. */
23771 decl = grokbitfield (declarator, &declspecs,
23772 width,
23773 attributes);
23774 else
23775 decl = grokfield (declarator, &declspecs,
23776 NULL_TREE, /*init_const_expr_p=*/false,
23777 NULL_TREE, attributes);
23778
23779 /* Add the instance variable. */
23780 if (decl != error_mark_node && decl != NULL_TREE)
23781 objc_add_instance_variable (decl);
23782
23783 /* Reset PREFIX_ATTRIBUTES. */
23784 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23785 attributes = TREE_CHAIN (attributes);
23786 if (attributes)
23787 TREE_CHAIN (attributes) = NULL_TREE;
23788
23789 token = cp_lexer_peek_token (parser->lexer);
23790
23791 if (token->type == CPP_COMMA)
23792 {
23793 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23794 continue;
23795 }
23796 break;
23797 }
23798
23799 cp_parser_consume_semicolon_at_end_of_statement (parser);
23800 token = cp_lexer_peek_token (parser->lexer);
23801 }
23802
23803 if (token->keyword == RID_AT_END)
23804 cp_parser_error (parser, "expected %<}%>");
23805
23806 /* Do not consume the RID_AT_END, so it will be read again as terminating
23807 the @interface of @implementation. */
23808 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23809 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
23810
23811 /* For historical reasons, we accept an optional semicolon. */
23812 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23813 cp_lexer_consume_token (parser->lexer);
23814 }
23815
23816 /* Parse an Objective-C protocol declaration. */
23817
23818 static void
23819 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23820 {
23821 tree proto, protorefs;
23822 cp_token *tok;
23823
23824 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23826 {
23827 tok = cp_lexer_peek_token (parser->lexer);
23828 error_at (tok->location, "identifier expected after %<@protocol%>");
23829 cp_parser_consume_semicolon_at_end_of_statement (parser);
23830 return;
23831 }
23832
23833 /* See if we have a forward declaration or a definition. */
23834 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23835
23836 /* Try a forward declaration first. */
23837 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23838 {
23839 while (true)
23840 {
23841 tree id;
23842
23843 id = cp_parser_identifier (parser);
23844 if (id == error_mark_node)
23845 break;
23846
23847 objc_declare_protocol (id, attributes);
23848
23849 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23850 cp_lexer_consume_token (parser->lexer);
23851 else
23852 break;
23853 }
23854 cp_parser_consume_semicolon_at_end_of_statement (parser);
23855 }
23856
23857 /* Ok, we got a full-fledged definition (or at least should). */
23858 else
23859 {
23860 proto = cp_parser_identifier (parser);
23861 protorefs = cp_parser_objc_protocol_refs_opt (parser);
23862 objc_start_protocol (proto, protorefs, attributes);
23863 cp_parser_objc_method_prototype_list (parser);
23864 }
23865 }
23866
23867 /* Parse an Objective-C superclass or category. */
23868
23869 static void
23870 cp_parser_objc_superclass_or_category (cp_parser *parser,
23871 bool iface_p,
23872 tree *super,
23873 tree *categ, bool *is_class_extension)
23874 {
23875 cp_token *next = cp_lexer_peek_token (parser->lexer);
23876
23877 *super = *categ = NULL_TREE;
23878 *is_class_extension = false;
23879 if (next->type == CPP_COLON)
23880 {
23881 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23882 *super = cp_parser_identifier (parser);
23883 }
23884 else if (next->type == CPP_OPEN_PAREN)
23885 {
23886 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23887
23888 /* If there is no category name, and this is an @interface, we
23889 have a class extension. */
23890 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23891 {
23892 *categ = NULL_TREE;
23893 *is_class_extension = true;
23894 }
23895 else
23896 *categ = cp_parser_identifier (parser);
23897
23898 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23899 }
23900 }
23901
23902 /* Parse an Objective-C class interface. */
23903
23904 static void
23905 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23906 {
23907 tree name, super, categ, protos;
23908 bool is_class_extension;
23909
23910 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
23911 name = cp_parser_identifier (parser);
23912 if (name == error_mark_node)
23913 {
23914 /* It's hard to recover because even if valid @interface stuff
23915 is to follow, we can't compile it (or validate it) if we
23916 don't even know which class it refers to. Let's assume this
23917 was a stray '@interface' token in the stream and skip it.
23918 */
23919 return;
23920 }
23921 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23922 &is_class_extension);
23923 protos = cp_parser_objc_protocol_refs_opt (parser);
23924
23925 /* We have either a class or a category on our hands. */
23926 if (categ || is_class_extension)
23927 objc_start_category_interface (name, categ, protos, attributes);
23928 else
23929 {
23930 objc_start_class_interface (name, super, protos, attributes);
23931 /* Handle instance variable declarations, if any. */
23932 cp_parser_objc_class_ivars (parser);
23933 objc_continue_interface ();
23934 }
23935
23936 cp_parser_objc_method_prototype_list (parser);
23937 }
23938
23939 /* Parse an Objective-C class implementation. */
23940
23941 static void
23942 cp_parser_objc_class_implementation (cp_parser* parser)
23943 {
23944 tree name, super, categ;
23945 bool is_class_extension;
23946
23947 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
23948 name = cp_parser_identifier (parser);
23949 if (name == error_mark_node)
23950 {
23951 /* It's hard to recover because even if valid @implementation
23952 stuff is to follow, we can't compile it (or validate it) if
23953 we don't even know which class it refers to. Let's assume
23954 this was a stray '@implementation' token in the stream and
23955 skip it.
23956 */
23957 return;
23958 }
23959 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23960 &is_class_extension);
23961
23962 /* We have either a class or a category on our hands. */
23963 if (categ)
23964 objc_start_category_implementation (name, categ);
23965 else
23966 {
23967 objc_start_class_implementation (name, super);
23968 /* Handle instance variable declarations, if any. */
23969 cp_parser_objc_class_ivars (parser);
23970 objc_continue_implementation ();
23971 }
23972
23973 cp_parser_objc_method_definition_list (parser);
23974 }
23975
23976 /* Consume the @end token and finish off the implementation. */
23977
23978 static void
23979 cp_parser_objc_end_implementation (cp_parser* parser)
23980 {
23981 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23982 objc_finish_implementation ();
23983 }
23984
23985 /* Parse an Objective-C declaration. */
23986
23987 static void
23988 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
23989 {
23990 /* Try to figure out what kind of declaration is present. */
23991 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23992
23993 if (attributes)
23994 switch (kwd->keyword)
23995 {
23996 case RID_AT_ALIAS:
23997 case RID_AT_CLASS:
23998 case RID_AT_END:
23999 error_at (kwd->location, "attributes may not be specified before"
24000 " the %<@%D%> Objective-C++ keyword",
24001 kwd->u.value);
24002 attributes = NULL;
24003 break;
24004 case RID_AT_IMPLEMENTATION:
24005 warning_at (kwd->location, OPT_Wattributes,
24006 "prefix attributes are ignored before %<@%D%>",
24007 kwd->u.value);
24008 attributes = NULL;
24009 default:
24010 break;
24011 }
24012
24013 switch (kwd->keyword)
24014 {
24015 case RID_AT_ALIAS:
24016 cp_parser_objc_alias_declaration (parser);
24017 break;
24018 case RID_AT_CLASS:
24019 cp_parser_objc_class_declaration (parser);
24020 break;
24021 case RID_AT_PROTOCOL:
24022 cp_parser_objc_protocol_declaration (parser, attributes);
24023 break;
24024 case RID_AT_INTERFACE:
24025 cp_parser_objc_class_interface (parser, attributes);
24026 break;
24027 case RID_AT_IMPLEMENTATION:
24028 cp_parser_objc_class_implementation (parser);
24029 break;
24030 case RID_AT_END:
24031 cp_parser_objc_end_implementation (parser);
24032 break;
24033 default:
24034 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24035 kwd->u.value);
24036 cp_parser_skip_to_end_of_block_or_statement (parser);
24037 }
24038 }
24039
24040 /* Parse an Objective-C try-catch-finally statement.
24041
24042 objc-try-catch-finally-stmt:
24043 @try compound-statement objc-catch-clause-seq [opt]
24044 objc-finally-clause [opt]
24045
24046 objc-catch-clause-seq:
24047 objc-catch-clause objc-catch-clause-seq [opt]
24048
24049 objc-catch-clause:
24050 @catch ( objc-exception-declaration ) compound-statement
24051
24052 objc-finally-clause:
24053 @finally compound-statement
24054
24055 objc-exception-declaration:
24056 parameter-declaration
24057 '...'
24058
24059 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24060
24061 Returns NULL_TREE.
24062
24063 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24064 for C. Keep them in sync. */
24065
24066 static tree
24067 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24068 {
24069 location_t location;
24070 tree stmt;
24071
24072 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24073 location = cp_lexer_peek_token (parser->lexer)->location;
24074 objc_maybe_warn_exceptions (location);
24075 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24076 node, lest it get absorbed into the surrounding block. */
24077 stmt = push_stmt_list ();
24078 cp_parser_compound_statement (parser, NULL, false, false);
24079 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24080
24081 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24082 {
24083 cp_parameter_declarator *parm;
24084 tree parameter_declaration = error_mark_node;
24085 bool seen_open_paren = false;
24086
24087 cp_lexer_consume_token (parser->lexer);
24088 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24089 seen_open_paren = true;
24090 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24091 {
24092 /* We have "@catch (...)" (where the '...' are literally
24093 what is in the code). Skip the '...'.
24094 parameter_declaration is set to NULL_TREE, and
24095 objc_being_catch_clauses() knows that that means
24096 '...'. */
24097 cp_lexer_consume_token (parser->lexer);
24098 parameter_declaration = NULL_TREE;
24099 }
24100 else
24101 {
24102 /* We have "@catch (NSException *exception)" or something
24103 like that. Parse the parameter declaration. */
24104 parm = cp_parser_parameter_declaration (parser, false, NULL);
24105 if (parm == NULL)
24106 parameter_declaration = error_mark_node;
24107 else
24108 parameter_declaration = grokdeclarator (parm->declarator,
24109 &parm->decl_specifiers,
24110 PARM, /*initialized=*/0,
24111 /*attrlist=*/NULL);
24112 }
24113 if (seen_open_paren)
24114 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24115 else
24116 {
24117 /* If there was no open parenthesis, we are recovering from
24118 an error, and we are trying to figure out what mistake
24119 the user has made. */
24120
24121 /* If there is an immediate closing parenthesis, the user
24122 probably forgot the opening one (ie, they typed "@catch
24123 NSException *e)". Parse the closing parenthesis and keep
24124 going. */
24125 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24126 cp_lexer_consume_token (parser->lexer);
24127
24128 /* If these is no immediate closing parenthesis, the user
24129 probably doesn't know that parenthesis are required at
24130 all (ie, they typed "@catch NSException *e"). So, just
24131 forget about the closing parenthesis and keep going. */
24132 }
24133 objc_begin_catch_clause (parameter_declaration);
24134 cp_parser_compound_statement (parser, NULL, false, false);
24135 objc_finish_catch_clause ();
24136 }
24137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24138 {
24139 cp_lexer_consume_token (parser->lexer);
24140 location = cp_lexer_peek_token (parser->lexer)->location;
24141 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24142 node, lest it get absorbed into the surrounding block. */
24143 stmt = push_stmt_list ();
24144 cp_parser_compound_statement (parser, NULL, false, false);
24145 objc_build_finally_clause (location, pop_stmt_list (stmt));
24146 }
24147
24148 return objc_finish_try_stmt ();
24149 }
24150
24151 /* Parse an Objective-C synchronized statement.
24152
24153 objc-synchronized-stmt:
24154 @synchronized ( expression ) compound-statement
24155
24156 Returns NULL_TREE. */
24157
24158 static tree
24159 cp_parser_objc_synchronized_statement (cp_parser *parser)
24160 {
24161 location_t location;
24162 tree lock, stmt;
24163
24164 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24165
24166 location = cp_lexer_peek_token (parser->lexer)->location;
24167 objc_maybe_warn_exceptions (location);
24168 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24169 lock = cp_parser_expression (parser, false, NULL);
24170 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24171
24172 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24173 node, lest it get absorbed into the surrounding block. */
24174 stmt = push_stmt_list ();
24175 cp_parser_compound_statement (parser, NULL, false, false);
24176
24177 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24178 }
24179
24180 /* Parse an Objective-C throw statement.
24181
24182 objc-throw-stmt:
24183 @throw assignment-expression [opt] ;
24184
24185 Returns a constructed '@throw' statement. */
24186
24187 static tree
24188 cp_parser_objc_throw_statement (cp_parser *parser)
24189 {
24190 tree expr = NULL_TREE;
24191 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24192
24193 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24194
24195 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24196 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24197
24198 cp_parser_consume_semicolon_at_end_of_statement (parser);
24199
24200 return objc_build_throw_stmt (loc, expr);
24201 }
24202
24203 /* Parse an Objective-C statement. */
24204
24205 static tree
24206 cp_parser_objc_statement (cp_parser * parser)
24207 {
24208 /* Try to figure out what kind of declaration is present. */
24209 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24210
24211 switch (kwd->keyword)
24212 {
24213 case RID_AT_TRY:
24214 return cp_parser_objc_try_catch_finally_statement (parser);
24215 case RID_AT_SYNCHRONIZED:
24216 return cp_parser_objc_synchronized_statement (parser);
24217 case RID_AT_THROW:
24218 return cp_parser_objc_throw_statement (parser);
24219 default:
24220 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24221 kwd->u.value);
24222 cp_parser_skip_to_end_of_block_or_statement (parser);
24223 }
24224
24225 return error_mark_node;
24226 }
24227
24228 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24229 look ahead to see if an objc keyword follows the attributes. This
24230 is to detect the use of prefix attributes on ObjC @interface and
24231 @protocol. */
24232
24233 static bool
24234 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24235 {
24236 cp_lexer_save_tokens (parser->lexer);
24237 *attrib = cp_parser_attributes_opt (parser);
24238 gcc_assert (*attrib);
24239 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24240 {
24241 cp_lexer_commit_tokens (parser->lexer);
24242 return true;
24243 }
24244 cp_lexer_rollback_tokens (parser->lexer);
24245 return false;
24246 }
24247
24248 /* This routine is a minimal replacement for
24249 c_parser_struct_declaration () used when parsing the list of
24250 types/names or ObjC++ properties. For example, when parsing the
24251 code
24252
24253 @property (readonly) int a, b, c;
24254
24255 this function is responsible for parsing "int a, int b, int c" and
24256 returning the declarations as CHAIN of DECLs.
24257
24258 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24259 similar parsing. */
24260 static tree
24261 cp_parser_objc_struct_declaration (cp_parser *parser)
24262 {
24263 tree decls = NULL_TREE;
24264 cp_decl_specifier_seq declspecs;
24265 int decl_class_or_enum_p;
24266 tree prefix_attributes;
24267
24268 cp_parser_decl_specifier_seq (parser,
24269 CP_PARSER_FLAGS_NONE,
24270 &declspecs,
24271 &decl_class_or_enum_p);
24272
24273 if (declspecs.type == error_mark_node)
24274 return error_mark_node;
24275
24276 /* auto, register, static, extern, mutable. */
24277 if (declspecs.storage_class != sc_none)
24278 {
24279 cp_parser_error (parser, "invalid type for property");
24280 declspecs.storage_class = sc_none;
24281 }
24282
24283 /* __thread. */
24284 if (declspecs.specs[(int) ds_thread])
24285 {
24286 cp_parser_error (parser, "invalid type for property");
24287 declspecs.specs[(int) ds_thread] = 0;
24288 }
24289
24290 /* typedef. */
24291 if (declspecs.specs[(int) ds_typedef])
24292 {
24293 cp_parser_error (parser, "invalid type for property");
24294 declspecs.specs[(int) ds_typedef] = 0;
24295 }
24296
24297 prefix_attributes = declspecs.attributes;
24298 declspecs.attributes = NULL_TREE;
24299
24300 /* Keep going until we hit the `;' at the end of the declaration. */
24301 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24302 {
24303 tree attributes, first_attribute, decl;
24304 cp_declarator *declarator;
24305 cp_token *token;
24306
24307 /* Parse the declarator. */
24308 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24309 NULL, NULL, false);
24310
24311 /* Look for attributes that apply to the ivar. */
24312 attributes = cp_parser_attributes_opt (parser);
24313 /* Remember which attributes are prefix attributes and
24314 which are not. */
24315 first_attribute = attributes;
24316 /* Combine the attributes. */
24317 attributes = chainon (prefix_attributes, attributes);
24318
24319 decl = grokfield (declarator, &declspecs,
24320 NULL_TREE, /*init_const_expr_p=*/false,
24321 NULL_TREE, attributes);
24322
24323 if (decl == error_mark_node || decl == NULL_TREE)
24324 return error_mark_node;
24325
24326 /* Reset PREFIX_ATTRIBUTES. */
24327 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24328 attributes = TREE_CHAIN (attributes);
24329 if (attributes)
24330 TREE_CHAIN (attributes) = NULL_TREE;
24331
24332 DECL_CHAIN (decl) = decls;
24333 decls = decl;
24334
24335 token = cp_lexer_peek_token (parser->lexer);
24336 if (token->type == CPP_COMMA)
24337 {
24338 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24339 continue;
24340 }
24341 else
24342 break;
24343 }
24344 return decls;
24345 }
24346
24347 /* Parse an Objective-C @property declaration. The syntax is:
24348
24349 objc-property-declaration:
24350 '@property' objc-property-attributes[opt] struct-declaration ;
24351
24352 objc-property-attributes:
24353 '(' objc-property-attribute-list ')'
24354
24355 objc-property-attribute-list:
24356 objc-property-attribute
24357 objc-property-attribute-list, objc-property-attribute
24358
24359 objc-property-attribute
24360 'getter' = identifier
24361 'setter' = identifier
24362 'readonly'
24363 'readwrite'
24364 'assign'
24365 'retain'
24366 'copy'
24367 'nonatomic'
24368
24369 For example:
24370 @property NSString *name;
24371 @property (readonly) id object;
24372 @property (retain, nonatomic, getter=getTheName) id name;
24373 @property int a, b, c;
24374
24375 PS: This function is identical to
24376 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24377 static void
24378 cp_parser_objc_at_property_declaration (cp_parser *parser)
24379 {
24380 /* The following variables hold the attributes of the properties as
24381 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24382 seen. When we see an attribute, we set them to 'true' (if they
24383 are boolean properties) or to the identifier (if they have an
24384 argument, ie, for getter and setter). Note that here we only
24385 parse the list of attributes, check the syntax and accumulate the
24386 attributes that we find. objc_add_property_declaration() will
24387 then process the information. */
24388 bool property_assign = false;
24389 bool property_copy = false;
24390 tree property_getter_ident = NULL_TREE;
24391 bool property_nonatomic = false;
24392 bool property_readonly = false;
24393 bool property_readwrite = false;
24394 bool property_retain = false;
24395 tree property_setter_ident = NULL_TREE;
24396
24397 /* 'properties' is the list of properties that we read. Usually a
24398 single one, but maybe more (eg, in "@property int a, b, c;" there
24399 are three). */
24400 tree properties;
24401 location_t loc;
24402
24403 loc = cp_lexer_peek_token (parser->lexer)->location;
24404
24405 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24406
24407 /* Parse the optional attribute list... */
24408 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24409 {
24410 /* Eat the '('. */
24411 cp_lexer_consume_token (parser->lexer);
24412
24413 while (true)
24414 {
24415 bool syntax_error = false;
24416 cp_token *token = cp_lexer_peek_token (parser->lexer);
24417 enum rid keyword;
24418
24419 if (token->type != CPP_NAME)
24420 {
24421 cp_parser_error (parser, "expected identifier");
24422 break;
24423 }
24424 keyword = C_RID_CODE (token->u.value);
24425 cp_lexer_consume_token (parser->lexer);
24426 switch (keyword)
24427 {
24428 case RID_ASSIGN: property_assign = true; break;
24429 case RID_COPY: property_copy = true; break;
24430 case RID_NONATOMIC: property_nonatomic = true; break;
24431 case RID_READONLY: property_readonly = true; break;
24432 case RID_READWRITE: property_readwrite = true; break;
24433 case RID_RETAIN: property_retain = true; break;
24434
24435 case RID_GETTER:
24436 case RID_SETTER:
24437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24438 {
24439 if (keyword == RID_GETTER)
24440 cp_parser_error (parser,
24441 "missing %<=%> (after %<getter%> attribute)");
24442 else
24443 cp_parser_error (parser,
24444 "missing %<=%> (after %<setter%> attribute)");
24445 syntax_error = true;
24446 break;
24447 }
24448 cp_lexer_consume_token (parser->lexer); /* eat the = */
24449 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24450 {
24451 cp_parser_error (parser, "expected identifier");
24452 syntax_error = true;
24453 break;
24454 }
24455 if (keyword == RID_SETTER)
24456 {
24457 if (property_setter_ident != NULL_TREE)
24458 {
24459 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24460 cp_lexer_consume_token (parser->lexer);
24461 }
24462 else
24463 property_setter_ident = cp_parser_objc_selector (parser);
24464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24465 cp_parser_error (parser, "setter name must terminate with %<:%>");
24466 else
24467 cp_lexer_consume_token (parser->lexer);
24468 }
24469 else
24470 {
24471 if (property_getter_ident != NULL_TREE)
24472 {
24473 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24474 cp_lexer_consume_token (parser->lexer);
24475 }
24476 else
24477 property_getter_ident = cp_parser_objc_selector (parser);
24478 }
24479 break;
24480 default:
24481 cp_parser_error (parser, "unknown property attribute");
24482 syntax_error = true;
24483 break;
24484 }
24485
24486 if (syntax_error)
24487 break;
24488
24489 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24490 cp_lexer_consume_token (parser->lexer);
24491 else
24492 break;
24493 }
24494
24495 /* FIXME: "@property (setter, assign);" will generate a spurious
24496 "error: expected ‘)’ before ‘,’ token". This is because
24497 cp_parser_require, unlike the C counterpart, will produce an
24498 error even if we are in error recovery. */
24499 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24500 {
24501 cp_parser_skip_to_closing_parenthesis (parser,
24502 /*recovering=*/true,
24503 /*or_comma=*/false,
24504 /*consume_paren=*/true);
24505 }
24506 }
24507
24508 /* ... and the property declaration(s). */
24509 properties = cp_parser_objc_struct_declaration (parser);
24510
24511 if (properties == error_mark_node)
24512 {
24513 cp_parser_skip_to_end_of_statement (parser);
24514 /* If the next token is now a `;', consume it. */
24515 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24516 cp_lexer_consume_token (parser->lexer);
24517 return;
24518 }
24519
24520 if (properties == NULL_TREE)
24521 cp_parser_error (parser, "expected identifier");
24522 else
24523 {
24524 /* Comma-separated properties are chained together in
24525 reverse order; add them one by one. */
24526 properties = nreverse (properties);
24527
24528 for (; properties; properties = TREE_CHAIN (properties))
24529 objc_add_property_declaration (loc, copy_node (properties),
24530 property_readonly, property_readwrite,
24531 property_assign, property_retain,
24532 property_copy, property_nonatomic,
24533 property_getter_ident, property_setter_ident);
24534 }
24535
24536 cp_parser_consume_semicolon_at_end_of_statement (parser);
24537 }
24538
24539 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24540
24541 objc-synthesize-declaration:
24542 @synthesize objc-synthesize-identifier-list ;
24543
24544 objc-synthesize-identifier-list:
24545 objc-synthesize-identifier
24546 objc-synthesize-identifier-list, objc-synthesize-identifier
24547
24548 objc-synthesize-identifier
24549 identifier
24550 identifier = identifier
24551
24552 For example:
24553 @synthesize MyProperty;
24554 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24555
24556 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24557 for C. Keep them in sync.
24558 */
24559 static void
24560 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24561 {
24562 tree list = NULL_TREE;
24563 location_t loc;
24564 loc = cp_lexer_peek_token (parser->lexer)->location;
24565
24566 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24567 while (true)
24568 {
24569 tree property, ivar;
24570 property = cp_parser_identifier (parser);
24571 if (property == error_mark_node)
24572 {
24573 cp_parser_consume_semicolon_at_end_of_statement (parser);
24574 return;
24575 }
24576 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24577 {
24578 cp_lexer_consume_token (parser->lexer);
24579 ivar = cp_parser_identifier (parser);
24580 if (ivar == error_mark_node)
24581 {
24582 cp_parser_consume_semicolon_at_end_of_statement (parser);
24583 return;
24584 }
24585 }
24586 else
24587 ivar = NULL_TREE;
24588 list = chainon (list, build_tree_list (ivar, property));
24589 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24590 cp_lexer_consume_token (parser->lexer);
24591 else
24592 break;
24593 }
24594 cp_parser_consume_semicolon_at_end_of_statement (parser);
24595 objc_add_synthesize_declaration (loc, list);
24596 }
24597
24598 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24599
24600 objc-dynamic-declaration:
24601 @dynamic identifier-list ;
24602
24603 For example:
24604 @dynamic MyProperty;
24605 @dynamic MyProperty, AnotherProperty;
24606
24607 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24608 for C. Keep them in sync.
24609 */
24610 static void
24611 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24612 {
24613 tree list = NULL_TREE;
24614 location_t loc;
24615 loc = cp_lexer_peek_token (parser->lexer)->location;
24616
24617 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24618 while (true)
24619 {
24620 tree property;
24621 property = cp_parser_identifier (parser);
24622 if (property == error_mark_node)
24623 {
24624 cp_parser_consume_semicolon_at_end_of_statement (parser);
24625 return;
24626 }
24627 list = chainon (list, build_tree_list (NULL, property));
24628 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24629 cp_lexer_consume_token (parser->lexer);
24630 else
24631 break;
24632 }
24633 cp_parser_consume_semicolon_at_end_of_statement (parser);
24634 objc_add_dynamic_declaration (loc, list);
24635 }
24636
24637 \f
24638 /* OpenMP 2.5 parsing routines. */
24639
24640 /* Returns name of the next clause.
24641 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24642 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24643 returned and the token is consumed. */
24644
24645 static pragma_omp_clause
24646 cp_parser_omp_clause_name (cp_parser *parser)
24647 {
24648 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24649
24650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24651 result = PRAGMA_OMP_CLAUSE_IF;
24652 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24653 result = PRAGMA_OMP_CLAUSE_DEFAULT;
24654 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24655 result = PRAGMA_OMP_CLAUSE_PRIVATE;
24656 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24657 {
24658 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24659 const char *p = IDENTIFIER_POINTER (id);
24660
24661 switch (p[0])
24662 {
24663 case 'c':
24664 if (!strcmp ("collapse", p))
24665 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24666 else if (!strcmp ("copyin", p))
24667 result = PRAGMA_OMP_CLAUSE_COPYIN;
24668 else if (!strcmp ("copyprivate", p))
24669 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24670 break;
24671 case 'f':
24672 if (!strcmp ("final", p))
24673 result = PRAGMA_OMP_CLAUSE_FINAL;
24674 else if (!strcmp ("firstprivate", p))
24675 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24676 break;
24677 case 'l':
24678 if (!strcmp ("lastprivate", p))
24679 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24680 break;
24681 case 'm':
24682 if (!strcmp ("mergeable", p))
24683 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24684 break;
24685 case 'n':
24686 if (!strcmp ("nowait", p))
24687 result = PRAGMA_OMP_CLAUSE_NOWAIT;
24688 else if (!strcmp ("num_threads", p))
24689 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24690 break;
24691 case 'o':
24692 if (!strcmp ("ordered", p))
24693 result = PRAGMA_OMP_CLAUSE_ORDERED;
24694 break;
24695 case 'r':
24696 if (!strcmp ("reduction", p))
24697 result = PRAGMA_OMP_CLAUSE_REDUCTION;
24698 break;
24699 case 's':
24700 if (!strcmp ("schedule", p))
24701 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24702 else if (!strcmp ("shared", p))
24703 result = PRAGMA_OMP_CLAUSE_SHARED;
24704 break;
24705 case 'u':
24706 if (!strcmp ("untied", p))
24707 result = PRAGMA_OMP_CLAUSE_UNTIED;
24708 break;
24709 }
24710 }
24711
24712 if (result != PRAGMA_OMP_CLAUSE_NONE)
24713 cp_lexer_consume_token (parser->lexer);
24714
24715 return result;
24716 }
24717
24718 /* Validate that a clause of the given type does not already exist. */
24719
24720 static void
24721 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24722 const char *name, location_t location)
24723 {
24724 tree c;
24725
24726 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24727 if (OMP_CLAUSE_CODE (c) == code)
24728 {
24729 error_at (location, "too many %qs clauses", name);
24730 break;
24731 }
24732 }
24733
24734 /* OpenMP 2.5:
24735 variable-list:
24736 identifier
24737 variable-list , identifier
24738
24739 In addition, we match a closing parenthesis. An opening parenthesis
24740 will have been consumed by the caller.
24741
24742 If KIND is nonzero, create the appropriate node and install the decl
24743 in OMP_CLAUSE_DECL and add the node to the head of the list.
24744
24745 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24746 return the list created. */
24747
24748 static tree
24749 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24750 tree list)
24751 {
24752 cp_token *token;
24753 while (1)
24754 {
24755 tree name, decl;
24756
24757 token = cp_lexer_peek_token (parser->lexer);
24758 name = cp_parser_id_expression (parser, /*template_p=*/false,
24759 /*check_dependency_p=*/true,
24760 /*template_p=*/NULL,
24761 /*declarator_p=*/false,
24762 /*optional_p=*/false);
24763 if (name == error_mark_node)
24764 goto skip_comma;
24765
24766 decl = cp_parser_lookup_name_simple (parser, name, token->location);
24767 if (decl == error_mark_node)
24768 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24769 token->location);
24770 else if (kind != 0)
24771 {
24772 tree u = build_omp_clause (token->location, kind);
24773 OMP_CLAUSE_DECL (u) = decl;
24774 OMP_CLAUSE_CHAIN (u) = list;
24775 list = u;
24776 }
24777 else
24778 list = tree_cons (decl, NULL_TREE, list);
24779
24780 get_comma:
24781 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24782 break;
24783 cp_lexer_consume_token (parser->lexer);
24784 }
24785
24786 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24787 {
24788 int ending;
24789
24790 /* Try to resync to an unnested comma. Copied from
24791 cp_parser_parenthesized_expression_list. */
24792 skip_comma:
24793 ending = cp_parser_skip_to_closing_parenthesis (parser,
24794 /*recovering=*/true,
24795 /*or_comma=*/true,
24796 /*consume_paren=*/true);
24797 if (ending < 0)
24798 goto get_comma;
24799 }
24800
24801 return list;
24802 }
24803
24804 /* Similarly, but expect leading and trailing parenthesis. This is a very
24805 common case for omp clauses. */
24806
24807 static tree
24808 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24809 {
24810 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24811 return cp_parser_omp_var_list_no_open (parser, kind, list);
24812 return list;
24813 }
24814
24815 /* OpenMP 3.0:
24816 collapse ( constant-expression ) */
24817
24818 static tree
24819 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24820 {
24821 tree c, num;
24822 location_t loc;
24823 HOST_WIDE_INT n;
24824
24825 loc = cp_lexer_peek_token (parser->lexer)->location;
24826 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24827 return list;
24828
24829 num = cp_parser_constant_expression (parser, false, NULL);
24830
24831 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24832 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24833 /*or_comma=*/false,
24834 /*consume_paren=*/true);
24835
24836 if (num == error_mark_node)
24837 return list;
24838 num = fold_non_dependent_expr (num);
24839 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24840 || !host_integerp (num, 0)
24841 || (n = tree_low_cst (num, 0)) <= 0
24842 || (int) n != n)
24843 {
24844 error_at (loc, "collapse argument needs positive constant integer expression");
24845 return list;
24846 }
24847
24848 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24849 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24850 OMP_CLAUSE_CHAIN (c) = list;
24851 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24852
24853 return c;
24854 }
24855
24856 /* OpenMP 2.5:
24857 default ( shared | none ) */
24858
24859 static tree
24860 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24861 {
24862 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24863 tree c;
24864
24865 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24866 return list;
24867 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24868 {
24869 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24870 const char *p = IDENTIFIER_POINTER (id);
24871
24872 switch (p[0])
24873 {
24874 case 'n':
24875 if (strcmp ("none", p) != 0)
24876 goto invalid_kind;
24877 kind = OMP_CLAUSE_DEFAULT_NONE;
24878 break;
24879
24880 case 's':
24881 if (strcmp ("shared", p) != 0)
24882 goto invalid_kind;
24883 kind = OMP_CLAUSE_DEFAULT_SHARED;
24884 break;
24885
24886 default:
24887 goto invalid_kind;
24888 }
24889
24890 cp_lexer_consume_token (parser->lexer);
24891 }
24892 else
24893 {
24894 invalid_kind:
24895 cp_parser_error (parser, "expected %<none%> or %<shared%>");
24896 }
24897
24898 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24899 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24900 /*or_comma=*/false,
24901 /*consume_paren=*/true);
24902
24903 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24904 return list;
24905
24906 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24907 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24908 OMP_CLAUSE_CHAIN (c) = list;
24909 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24910
24911 return c;
24912 }
24913
24914 /* OpenMP 3.1:
24915 final ( expression ) */
24916
24917 static tree
24918 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24919 {
24920 tree t, c;
24921
24922 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24923 return list;
24924
24925 t = cp_parser_condition (parser);
24926
24927 if (t == error_mark_node
24928 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24929 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24930 /*or_comma=*/false,
24931 /*consume_paren=*/true);
24932
24933 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24934
24935 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24936 OMP_CLAUSE_FINAL_EXPR (c) = t;
24937 OMP_CLAUSE_CHAIN (c) = list;
24938
24939 return c;
24940 }
24941
24942 /* OpenMP 2.5:
24943 if ( expression ) */
24944
24945 static tree
24946 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24947 {
24948 tree t, c;
24949
24950 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24951 return list;
24952
24953 t = cp_parser_condition (parser);
24954
24955 if (t == error_mark_node
24956 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24957 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24958 /*or_comma=*/false,
24959 /*consume_paren=*/true);
24960
24961 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24962
24963 c = build_omp_clause (location, OMP_CLAUSE_IF);
24964 OMP_CLAUSE_IF_EXPR (c) = t;
24965 OMP_CLAUSE_CHAIN (c) = list;
24966
24967 return c;
24968 }
24969
24970 /* OpenMP 3.1:
24971 mergeable */
24972
24973 static tree
24974 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
24975 tree list, location_t location)
24976 {
24977 tree c;
24978
24979 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
24980 location);
24981
24982 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
24983 OMP_CLAUSE_CHAIN (c) = list;
24984 return c;
24985 }
24986
24987 /* OpenMP 2.5:
24988 nowait */
24989
24990 static tree
24991 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
24992 tree list, location_t location)
24993 {
24994 tree c;
24995
24996 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
24997
24998 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
24999 OMP_CLAUSE_CHAIN (c) = list;
25000 return c;
25001 }
25002
25003 /* OpenMP 2.5:
25004 num_threads ( expression ) */
25005
25006 static tree
25007 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25008 location_t location)
25009 {
25010 tree t, c;
25011
25012 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25013 return list;
25014
25015 t = cp_parser_expression (parser, false, NULL);
25016
25017 if (t == error_mark_node
25018 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25019 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25020 /*or_comma=*/false,
25021 /*consume_paren=*/true);
25022
25023 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25024 "num_threads", location);
25025
25026 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25027 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25028 OMP_CLAUSE_CHAIN (c) = list;
25029
25030 return c;
25031 }
25032
25033 /* OpenMP 2.5:
25034 ordered */
25035
25036 static tree
25037 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25038 tree list, location_t location)
25039 {
25040 tree c;
25041
25042 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25043 "ordered", location);
25044
25045 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25046 OMP_CLAUSE_CHAIN (c) = list;
25047 return c;
25048 }
25049
25050 /* OpenMP 2.5:
25051 reduction ( reduction-operator : variable-list )
25052
25053 reduction-operator:
25054 One of: + * - & ^ | && ||
25055
25056 OpenMP 3.1:
25057
25058 reduction-operator:
25059 One of: + * - & ^ | && || min max */
25060
25061 static tree
25062 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25063 {
25064 enum tree_code code;
25065 tree nlist, c;
25066
25067 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25068 return list;
25069
25070 switch (cp_lexer_peek_token (parser->lexer)->type)
25071 {
25072 case CPP_PLUS:
25073 code = PLUS_EXPR;
25074 break;
25075 case CPP_MULT:
25076 code = MULT_EXPR;
25077 break;
25078 case CPP_MINUS:
25079 code = MINUS_EXPR;
25080 break;
25081 case CPP_AND:
25082 code = BIT_AND_EXPR;
25083 break;
25084 case CPP_XOR:
25085 code = BIT_XOR_EXPR;
25086 break;
25087 case CPP_OR:
25088 code = BIT_IOR_EXPR;
25089 break;
25090 case CPP_AND_AND:
25091 code = TRUTH_ANDIF_EXPR;
25092 break;
25093 case CPP_OR_OR:
25094 code = TRUTH_ORIF_EXPR;
25095 break;
25096 case CPP_NAME:
25097 {
25098 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25099 const char *p = IDENTIFIER_POINTER (id);
25100
25101 if (strcmp (p, "min") == 0)
25102 {
25103 code = MIN_EXPR;
25104 break;
25105 }
25106 if (strcmp (p, "max") == 0)
25107 {
25108 code = MAX_EXPR;
25109 break;
25110 }
25111 }
25112 /* FALLTHROUGH */
25113 default:
25114 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25115 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25116 resync_fail:
25117 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25118 /*or_comma=*/false,
25119 /*consume_paren=*/true);
25120 return list;
25121 }
25122 cp_lexer_consume_token (parser->lexer);
25123
25124 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25125 goto resync_fail;
25126
25127 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25128 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25129 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25130
25131 return nlist;
25132 }
25133
25134 /* OpenMP 2.5:
25135 schedule ( schedule-kind )
25136 schedule ( schedule-kind , expression )
25137
25138 schedule-kind:
25139 static | dynamic | guided | runtime | auto */
25140
25141 static tree
25142 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25143 {
25144 tree c, t;
25145
25146 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25147 return list;
25148
25149 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25150
25151 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25152 {
25153 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25154 const char *p = IDENTIFIER_POINTER (id);
25155
25156 switch (p[0])
25157 {
25158 case 'd':
25159 if (strcmp ("dynamic", p) != 0)
25160 goto invalid_kind;
25161 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25162 break;
25163
25164 case 'g':
25165 if (strcmp ("guided", p) != 0)
25166 goto invalid_kind;
25167 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25168 break;
25169
25170 case 'r':
25171 if (strcmp ("runtime", p) != 0)
25172 goto invalid_kind;
25173 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25174 break;
25175
25176 default:
25177 goto invalid_kind;
25178 }
25179 }
25180 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25181 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25182 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25183 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25184 else
25185 goto invalid_kind;
25186 cp_lexer_consume_token (parser->lexer);
25187
25188 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25189 {
25190 cp_token *token;
25191 cp_lexer_consume_token (parser->lexer);
25192
25193 token = cp_lexer_peek_token (parser->lexer);
25194 t = cp_parser_assignment_expression (parser, false, NULL);
25195
25196 if (t == error_mark_node)
25197 goto resync_fail;
25198 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25199 error_at (token->location, "schedule %<runtime%> does not take "
25200 "a %<chunk_size%> parameter");
25201 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25202 error_at (token->location, "schedule %<auto%> does not take "
25203 "a %<chunk_size%> parameter");
25204 else
25205 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25206
25207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25208 goto resync_fail;
25209 }
25210 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25211 goto resync_fail;
25212
25213 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25214 OMP_CLAUSE_CHAIN (c) = list;
25215 return c;
25216
25217 invalid_kind:
25218 cp_parser_error (parser, "invalid schedule kind");
25219 resync_fail:
25220 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25221 /*or_comma=*/false,
25222 /*consume_paren=*/true);
25223 return list;
25224 }
25225
25226 /* OpenMP 3.0:
25227 untied */
25228
25229 static tree
25230 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25231 tree list, location_t location)
25232 {
25233 tree c;
25234
25235 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25236
25237 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25238 OMP_CLAUSE_CHAIN (c) = list;
25239 return c;
25240 }
25241
25242 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25243 is a bitmask in MASK. Return the list of clauses found; the result
25244 of clause default goes in *pdefault. */
25245
25246 static tree
25247 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25248 const char *where, cp_token *pragma_tok)
25249 {
25250 tree clauses = NULL;
25251 bool first = true;
25252 cp_token *token = NULL;
25253
25254 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25255 {
25256 pragma_omp_clause c_kind;
25257 const char *c_name;
25258 tree prev = clauses;
25259
25260 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25261 cp_lexer_consume_token (parser->lexer);
25262
25263 token = cp_lexer_peek_token (parser->lexer);
25264 c_kind = cp_parser_omp_clause_name (parser);
25265 first = false;
25266
25267 switch (c_kind)
25268 {
25269 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25270 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25271 token->location);
25272 c_name = "collapse";
25273 break;
25274 case PRAGMA_OMP_CLAUSE_COPYIN:
25275 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25276 c_name = "copyin";
25277 break;
25278 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25279 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25280 clauses);
25281 c_name = "copyprivate";
25282 break;
25283 case PRAGMA_OMP_CLAUSE_DEFAULT:
25284 clauses = cp_parser_omp_clause_default (parser, clauses,
25285 token->location);
25286 c_name = "default";
25287 break;
25288 case PRAGMA_OMP_CLAUSE_FINAL:
25289 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25290 c_name = "final";
25291 break;
25292 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25293 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25294 clauses);
25295 c_name = "firstprivate";
25296 break;
25297 case PRAGMA_OMP_CLAUSE_IF:
25298 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25299 c_name = "if";
25300 break;
25301 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25302 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25303 clauses);
25304 c_name = "lastprivate";
25305 break;
25306 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25307 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25308 token->location);
25309 c_name = "mergeable";
25310 break;
25311 case PRAGMA_OMP_CLAUSE_NOWAIT:
25312 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25313 c_name = "nowait";
25314 break;
25315 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25316 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25317 token->location);
25318 c_name = "num_threads";
25319 break;
25320 case PRAGMA_OMP_CLAUSE_ORDERED:
25321 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25322 token->location);
25323 c_name = "ordered";
25324 break;
25325 case PRAGMA_OMP_CLAUSE_PRIVATE:
25326 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25327 clauses);
25328 c_name = "private";
25329 break;
25330 case PRAGMA_OMP_CLAUSE_REDUCTION:
25331 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25332 c_name = "reduction";
25333 break;
25334 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25335 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25336 token->location);
25337 c_name = "schedule";
25338 break;
25339 case PRAGMA_OMP_CLAUSE_SHARED:
25340 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25341 clauses);
25342 c_name = "shared";
25343 break;
25344 case PRAGMA_OMP_CLAUSE_UNTIED:
25345 clauses = cp_parser_omp_clause_untied (parser, clauses,
25346 token->location);
25347 c_name = "nowait";
25348 break;
25349 default:
25350 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25351 goto saw_error;
25352 }
25353
25354 if (((mask >> c_kind) & 1) == 0)
25355 {
25356 /* Remove the invalid clause(s) from the list to avoid
25357 confusing the rest of the compiler. */
25358 clauses = prev;
25359 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25360 }
25361 }
25362 saw_error:
25363 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25364 return finish_omp_clauses (clauses);
25365 }
25366
25367 /* OpenMP 2.5:
25368 structured-block:
25369 statement
25370
25371 In practice, we're also interested in adding the statement to an
25372 outer node. So it is convenient if we work around the fact that
25373 cp_parser_statement calls add_stmt. */
25374
25375 static unsigned
25376 cp_parser_begin_omp_structured_block (cp_parser *parser)
25377 {
25378 unsigned save = parser->in_statement;
25379
25380 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25381 This preserves the "not within loop or switch" style error messages
25382 for nonsense cases like
25383 void foo() {
25384 #pragma omp single
25385 break;
25386 }
25387 */
25388 if (parser->in_statement)
25389 parser->in_statement = IN_OMP_BLOCK;
25390
25391 return save;
25392 }
25393
25394 static void
25395 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25396 {
25397 parser->in_statement = save;
25398 }
25399
25400 static tree
25401 cp_parser_omp_structured_block (cp_parser *parser)
25402 {
25403 tree stmt = begin_omp_structured_block ();
25404 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25405
25406 cp_parser_statement (parser, NULL_TREE, false, NULL);
25407
25408 cp_parser_end_omp_structured_block (parser, save);
25409 return finish_omp_structured_block (stmt);
25410 }
25411
25412 /* OpenMP 2.5:
25413 # pragma omp atomic new-line
25414 expression-stmt
25415
25416 expression-stmt:
25417 x binop= expr | x++ | ++x | x-- | --x
25418 binop:
25419 +, *, -, /, &, ^, |, <<, >>
25420
25421 where x is an lvalue expression with scalar type.
25422
25423 OpenMP 3.1:
25424 # pragma omp atomic new-line
25425 update-stmt
25426
25427 # pragma omp atomic read new-line
25428 read-stmt
25429
25430 # pragma omp atomic write new-line
25431 write-stmt
25432
25433 # pragma omp atomic update new-line
25434 update-stmt
25435
25436 # pragma omp atomic capture new-line
25437 capture-stmt
25438
25439 # pragma omp atomic capture new-line
25440 capture-block
25441
25442 read-stmt:
25443 v = x
25444 write-stmt:
25445 x = expr
25446 update-stmt:
25447 expression-stmt | x = x binop expr
25448 capture-stmt:
25449 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25450 capture-block:
25451 { v = x; update-stmt; } | { update-stmt; v = x; }
25452
25453 where x and v are lvalue expressions with scalar type. */
25454
25455 static void
25456 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25457 {
25458 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25459 tree rhs1 = NULL_TREE, orig_lhs;
25460 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25461 bool structured_block = false;
25462
25463 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25464 {
25465 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25466 const char *p = IDENTIFIER_POINTER (id);
25467
25468 if (!strcmp (p, "read"))
25469 code = OMP_ATOMIC_READ;
25470 else if (!strcmp (p, "write"))
25471 code = NOP_EXPR;
25472 else if (!strcmp (p, "update"))
25473 code = OMP_ATOMIC;
25474 else if (!strcmp (p, "capture"))
25475 code = OMP_ATOMIC_CAPTURE_NEW;
25476 else
25477 p = NULL;
25478 if (p)
25479 cp_lexer_consume_token (parser->lexer);
25480 }
25481 cp_parser_require_pragma_eol (parser, pragma_tok);
25482
25483 switch (code)
25484 {
25485 case OMP_ATOMIC_READ:
25486 case NOP_EXPR: /* atomic write */
25487 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25488 /*cast_p=*/false, NULL);
25489 if (v == error_mark_node)
25490 goto saw_error;
25491 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25492 goto saw_error;
25493 if (code == NOP_EXPR)
25494 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25495 else
25496 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25497 /*cast_p=*/false, NULL);
25498 if (lhs == error_mark_node)
25499 goto saw_error;
25500 if (code == NOP_EXPR)
25501 {
25502 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25503 opcode. */
25504 code = OMP_ATOMIC;
25505 rhs = lhs;
25506 lhs = v;
25507 v = NULL_TREE;
25508 }
25509 goto done;
25510 case OMP_ATOMIC_CAPTURE_NEW:
25511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25512 {
25513 cp_lexer_consume_token (parser->lexer);
25514 structured_block = true;
25515 }
25516 else
25517 {
25518 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25519 /*cast_p=*/false, NULL);
25520 if (v == error_mark_node)
25521 goto saw_error;
25522 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25523 goto saw_error;
25524 }
25525 default:
25526 break;
25527 }
25528
25529 restart:
25530 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25531 /*cast_p=*/false, NULL);
25532 orig_lhs = lhs;
25533 switch (TREE_CODE (lhs))
25534 {
25535 case ERROR_MARK:
25536 goto saw_error;
25537
25538 case POSTINCREMENT_EXPR:
25539 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25540 code = OMP_ATOMIC_CAPTURE_OLD;
25541 /* FALLTHROUGH */
25542 case PREINCREMENT_EXPR:
25543 lhs = TREE_OPERAND (lhs, 0);
25544 opcode = PLUS_EXPR;
25545 rhs = integer_one_node;
25546 break;
25547
25548 case POSTDECREMENT_EXPR:
25549 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25550 code = OMP_ATOMIC_CAPTURE_OLD;
25551 /* FALLTHROUGH */
25552 case PREDECREMENT_EXPR:
25553 lhs = TREE_OPERAND (lhs, 0);
25554 opcode = MINUS_EXPR;
25555 rhs = integer_one_node;
25556 break;
25557
25558 case COMPOUND_EXPR:
25559 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25560 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25561 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25562 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25563 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25564 (TREE_OPERAND (lhs, 1), 0), 0)))
25565 == BOOLEAN_TYPE)
25566 /* Undo effects of boolean_increment for post {in,de}crement. */
25567 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25568 /* FALLTHRU */
25569 case MODIFY_EXPR:
25570 if (TREE_CODE (lhs) == MODIFY_EXPR
25571 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25572 {
25573 /* Undo effects of boolean_increment. */
25574 if (integer_onep (TREE_OPERAND (lhs, 1)))
25575 {
25576 /* This is pre or post increment. */
25577 rhs = TREE_OPERAND (lhs, 1);
25578 lhs = TREE_OPERAND (lhs, 0);
25579 opcode = NOP_EXPR;
25580 if (code == OMP_ATOMIC_CAPTURE_NEW
25581 && !structured_block
25582 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25583 code = OMP_ATOMIC_CAPTURE_OLD;
25584 break;
25585 }
25586 }
25587 /* FALLTHRU */
25588 default:
25589 switch (cp_lexer_peek_token (parser->lexer)->type)
25590 {
25591 case CPP_MULT_EQ:
25592 opcode = MULT_EXPR;
25593 break;
25594 case CPP_DIV_EQ:
25595 opcode = TRUNC_DIV_EXPR;
25596 break;
25597 case CPP_PLUS_EQ:
25598 opcode = PLUS_EXPR;
25599 break;
25600 case CPP_MINUS_EQ:
25601 opcode = MINUS_EXPR;
25602 break;
25603 case CPP_LSHIFT_EQ:
25604 opcode = LSHIFT_EXPR;
25605 break;
25606 case CPP_RSHIFT_EQ:
25607 opcode = RSHIFT_EXPR;
25608 break;
25609 case CPP_AND_EQ:
25610 opcode = BIT_AND_EXPR;
25611 break;
25612 case CPP_OR_EQ:
25613 opcode = BIT_IOR_EXPR;
25614 break;
25615 case CPP_XOR_EQ:
25616 opcode = BIT_XOR_EXPR;
25617 break;
25618 case CPP_EQ:
25619 if (structured_block || code == OMP_ATOMIC)
25620 {
25621 enum cp_parser_prec oprec;
25622 cp_token *token;
25623 cp_lexer_consume_token (parser->lexer);
25624 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25625 /*cast_p=*/false, NULL);
25626 if (rhs1 == error_mark_node)
25627 goto saw_error;
25628 token = cp_lexer_peek_token (parser->lexer);
25629 switch (token->type)
25630 {
25631 case CPP_SEMICOLON:
25632 if (code == OMP_ATOMIC_CAPTURE_NEW)
25633 {
25634 code = OMP_ATOMIC_CAPTURE_OLD;
25635 v = lhs;
25636 lhs = NULL_TREE;
25637 lhs1 = rhs1;
25638 rhs1 = NULL_TREE;
25639 cp_lexer_consume_token (parser->lexer);
25640 goto restart;
25641 }
25642 cp_parser_error (parser,
25643 "invalid form of %<#pragma omp atomic%>");
25644 goto saw_error;
25645 case CPP_MULT:
25646 opcode = MULT_EXPR;
25647 break;
25648 case CPP_DIV:
25649 opcode = TRUNC_DIV_EXPR;
25650 break;
25651 case CPP_PLUS:
25652 opcode = PLUS_EXPR;
25653 break;
25654 case CPP_MINUS:
25655 opcode = MINUS_EXPR;
25656 break;
25657 case CPP_LSHIFT:
25658 opcode = LSHIFT_EXPR;
25659 break;
25660 case CPP_RSHIFT:
25661 opcode = RSHIFT_EXPR;
25662 break;
25663 case CPP_AND:
25664 opcode = BIT_AND_EXPR;
25665 break;
25666 case CPP_OR:
25667 opcode = BIT_IOR_EXPR;
25668 break;
25669 case CPP_XOR:
25670 opcode = BIT_XOR_EXPR;
25671 break;
25672 default:
25673 cp_parser_error (parser,
25674 "invalid operator for %<#pragma omp atomic%>");
25675 goto saw_error;
25676 }
25677 oprec = TOKEN_PRECEDENCE (token);
25678 gcc_assert (oprec != PREC_NOT_OPERATOR);
25679 if (commutative_tree_code (opcode))
25680 oprec = (enum cp_parser_prec) (oprec - 1);
25681 cp_lexer_consume_token (parser->lexer);
25682 rhs = cp_parser_binary_expression (parser, false, false,
25683 oprec, NULL);
25684 if (rhs == error_mark_node)
25685 goto saw_error;
25686 goto stmt_done;
25687 }
25688 /* FALLTHROUGH */
25689 default:
25690 cp_parser_error (parser,
25691 "invalid operator for %<#pragma omp atomic%>");
25692 goto saw_error;
25693 }
25694 cp_lexer_consume_token (parser->lexer);
25695
25696 rhs = cp_parser_expression (parser, false, NULL);
25697 if (rhs == error_mark_node)
25698 goto saw_error;
25699 break;
25700 }
25701 stmt_done:
25702 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25703 {
25704 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25705 goto saw_error;
25706 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25707 /*cast_p=*/false, NULL);
25708 if (v == error_mark_node)
25709 goto saw_error;
25710 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25711 goto saw_error;
25712 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25713 /*cast_p=*/false, NULL);
25714 if (lhs1 == error_mark_node)
25715 goto saw_error;
25716 }
25717 if (structured_block)
25718 {
25719 cp_parser_consume_semicolon_at_end_of_statement (parser);
25720 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25721 }
25722 done:
25723 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25724 if (!structured_block)
25725 cp_parser_consume_semicolon_at_end_of_statement (parser);
25726 return;
25727
25728 saw_error:
25729 cp_parser_skip_to_end_of_block_or_statement (parser);
25730 if (structured_block)
25731 {
25732 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25733 cp_lexer_consume_token (parser->lexer);
25734 else if (code == OMP_ATOMIC_CAPTURE_NEW)
25735 {
25736 cp_parser_skip_to_end_of_block_or_statement (parser);
25737 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25738 cp_lexer_consume_token (parser->lexer);
25739 }
25740 }
25741 }
25742
25743
25744 /* OpenMP 2.5:
25745 # pragma omp barrier new-line */
25746
25747 static void
25748 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25749 {
25750 cp_parser_require_pragma_eol (parser, pragma_tok);
25751 finish_omp_barrier ();
25752 }
25753
25754 /* OpenMP 2.5:
25755 # pragma omp critical [(name)] new-line
25756 structured-block */
25757
25758 static tree
25759 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25760 {
25761 tree stmt, name = NULL;
25762
25763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25764 {
25765 cp_lexer_consume_token (parser->lexer);
25766
25767 name = cp_parser_identifier (parser);
25768
25769 if (name == error_mark_node
25770 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25771 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25772 /*or_comma=*/false,
25773 /*consume_paren=*/true);
25774 if (name == error_mark_node)
25775 name = NULL;
25776 }
25777 cp_parser_require_pragma_eol (parser, pragma_tok);
25778
25779 stmt = cp_parser_omp_structured_block (parser);
25780 return c_finish_omp_critical (input_location, stmt, name);
25781 }
25782
25783 /* OpenMP 2.5:
25784 # pragma omp flush flush-vars[opt] new-line
25785
25786 flush-vars:
25787 ( variable-list ) */
25788
25789 static void
25790 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25791 {
25792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25793 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25794 cp_parser_require_pragma_eol (parser, pragma_tok);
25795
25796 finish_omp_flush ();
25797 }
25798
25799 /* Helper function, to parse omp for increment expression. */
25800
25801 static tree
25802 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25803 {
25804 tree cond = cp_parser_binary_expression (parser, false, true,
25805 PREC_NOT_OPERATOR, NULL);
25806 if (cond == error_mark_node
25807 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25808 {
25809 cp_parser_skip_to_end_of_statement (parser);
25810 return error_mark_node;
25811 }
25812
25813 switch (TREE_CODE (cond))
25814 {
25815 case GT_EXPR:
25816 case GE_EXPR:
25817 case LT_EXPR:
25818 case LE_EXPR:
25819 break;
25820 default:
25821 return error_mark_node;
25822 }
25823
25824 /* If decl is an iterator, preserve LHS and RHS of the relational
25825 expr until finish_omp_for. */
25826 if (decl
25827 && (type_dependent_expression_p (decl)
25828 || CLASS_TYPE_P (TREE_TYPE (decl))))
25829 return cond;
25830
25831 return build_x_binary_op (TREE_CODE (cond),
25832 TREE_OPERAND (cond, 0), ERROR_MARK,
25833 TREE_OPERAND (cond, 1), ERROR_MARK,
25834 /*overload=*/NULL, tf_warning_or_error);
25835 }
25836
25837 /* Helper function, to parse omp for increment expression. */
25838
25839 static tree
25840 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25841 {
25842 cp_token *token = cp_lexer_peek_token (parser->lexer);
25843 enum tree_code op;
25844 tree lhs, rhs;
25845 cp_id_kind idk;
25846 bool decl_first;
25847
25848 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25849 {
25850 op = (token->type == CPP_PLUS_PLUS
25851 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25852 cp_lexer_consume_token (parser->lexer);
25853 lhs = cp_parser_cast_expression (parser, false, false, NULL);
25854 if (lhs != decl)
25855 return error_mark_node;
25856 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25857 }
25858
25859 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
25860 if (lhs != decl)
25861 return error_mark_node;
25862
25863 token = cp_lexer_peek_token (parser->lexer);
25864 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25865 {
25866 op = (token->type == CPP_PLUS_PLUS
25867 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25868 cp_lexer_consume_token (parser->lexer);
25869 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25870 }
25871
25872 op = cp_parser_assignment_operator_opt (parser);
25873 if (op == ERROR_MARK)
25874 return error_mark_node;
25875
25876 if (op != NOP_EXPR)
25877 {
25878 rhs = cp_parser_assignment_expression (parser, false, NULL);
25879 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25880 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25881 }
25882
25883 lhs = cp_parser_binary_expression (parser, false, false,
25884 PREC_ADDITIVE_EXPRESSION, NULL);
25885 token = cp_lexer_peek_token (parser->lexer);
25886 decl_first = lhs == decl;
25887 if (decl_first)
25888 lhs = NULL_TREE;
25889 if (token->type != CPP_PLUS
25890 && token->type != CPP_MINUS)
25891 return error_mark_node;
25892
25893 do
25894 {
25895 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25896 cp_lexer_consume_token (parser->lexer);
25897 rhs = cp_parser_binary_expression (parser, false, false,
25898 PREC_ADDITIVE_EXPRESSION, NULL);
25899 token = cp_lexer_peek_token (parser->lexer);
25900 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25901 {
25902 if (lhs == NULL_TREE)
25903 {
25904 if (op == PLUS_EXPR)
25905 lhs = rhs;
25906 else
25907 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25908 }
25909 else
25910 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25911 NULL, tf_warning_or_error);
25912 }
25913 }
25914 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25915
25916 if (!decl_first)
25917 {
25918 if (rhs != decl || op == MINUS_EXPR)
25919 return error_mark_node;
25920 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25921 }
25922 else
25923 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25924
25925 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25926 }
25927
25928 /* Parse the restricted form of the for statement allowed by OpenMP. */
25929
25930 static tree
25931 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25932 {
25933 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25934 tree real_decl, initv, condv, incrv, declv;
25935 tree this_pre_body, cl;
25936 location_t loc_first;
25937 bool collapse_err = false;
25938 int i, collapse = 1, nbraces = 0;
25939 VEC(tree,gc) *for_block = make_tree_vector ();
25940
25941 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25942 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25943 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25944
25945 gcc_assert (collapse >= 1);
25946
25947 declv = make_tree_vec (collapse);
25948 initv = make_tree_vec (collapse);
25949 condv = make_tree_vec (collapse);
25950 incrv = make_tree_vec (collapse);
25951
25952 loc_first = cp_lexer_peek_token (parser->lexer)->location;
25953
25954 for (i = 0; i < collapse; i++)
25955 {
25956 int bracecount = 0;
25957 bool add_private_clause = false;
25958 location_t loc;
25959
25960 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25961 {
25962 cp_parser_error (parser, "for statement expected");
25963 return NULL;
25964 }
25965 loc = cp_lexer_consume_token (parser->lexer)->location;
25966
25967 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25968 return NULL;
25969
25970 init = decl = real_decl = NULL;
25971 this_pre_body = push_stmt_list ();
25972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25973 {
25974 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
25975
25976 init-expr:
25977 var = lb
25978 integer-type var = lb
25979 random-access-iterator-type var = lb
25980 pointer-type var = lb
25981 */
25982 cp_decl_specifier_seq type_specifiers;
25983
25984 /* First, try to parse as an initialized declaration. See
25985 cp_parser_condition, from whence the bulk of this is copied. */
25986
25987 cp_parser_parse_tentatively (parser);
25988 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25989 /*is_trailing_return=*/false,
25990 &type_specifiers);
25991 if (cp_parser_parse_definitely (parser))
25992 {
25993 /* If parsing a type specifier seq succeeded, then this
25994 MUST be a initialized declaration. */
25995 tree asm_specification, attributes;
25996 cp_declarator *declarator;
25997
25998 declarator = cp_parser_declarator (parser,
25999 CP_PARSER_DECLARATOR_NAMED,
26000 /*ctor_dtor_or_conv_p=*/NULL,
26001 /*parenthesized_p=*/NULL,
26002 /*member_p=*/false);
26003 attributes = cp_parser_attributes_opt (parser);
26004 asm_specification = cp_parser_asm_specification_opt (parser);
26005
26006 if (declarator == cp_error_declarator)
26007 cp_parser_skip_to_end_of_statement (parser);
26008
26009 else
26010 {
26011 tree pushed_scope, auto_node;
26012
26013 decl = start_decl (declarator, &type_specifiers,
26014 SD_INITIALIZED, attributes,
26015 /*prefix_attributes=*/NULL_TREE,
26016 &pushed_scope);
26017
26018 auto_node = type_uses_auto (TREE_TYPE (decl));
26019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26020 {
26021 if (cp_lexer_next_token_is (parser->lexer,
26022 CPP_OPEN_PAREN))
26023 error ("parenthesized initialization is not allowed in "
26024 "OpenMP %<for%> loop");
26025 else
26026 /* Trigger an error. */
26027 cp_parser_require (parser, CPP_EQ, RT_EQ);
26028
26029 init = error_mark_node;
26030 cp_parser_skip_to_end_of_statement (parser);
26031 }
26032 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26033 || type_dependent_expression_p (decl)
26034 || auto_node)
26035 {
26036 bool is_direct_init, is_non_constant_init;
26037
26038 init = cp_parser_initializer (parser,
26039 &is_direct_init,
26040 &is_non_constant_init);
26041
26042 if (auto_node)
26043 {
26044 TREE_TYPE (decl)
26045 = do_auto_deduction (TREE_TYPE (decl), init,
26046 auto_node);
26047
26048 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26049 && !type_dependent_expression_p (decl))
26050 goto non_class;
26051 }
26052
26053 cp_finish_decl (decl, init, !is_non_constant_init,
26054 asm_specification,
26055 LOOKUP_ONLYCONVERTING);
26056 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26057 {
26058 VEC_safe_push (tree, gc, for_block, this_pre_body);
26059 init = NULL_TREE;
26060 }
26061 else
26062 init = pop_stmt_list (this_pre_body);
26063 this_pre_body = NULL_TREE;
26064 }
26065 else
26066 {
26067 /* Consume '='. */
26068 cp_lexer_consume_token (parser->lexer);
26069 init = cp_parser_assignment_expression (parser, false, NULL);
26070
26071 non_class:
26072 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26073 init = error_mark_node;
26074 else
26075 cp_finish_decl (decl, NULL_TREE,
26076 /*init_const_expr_p=*/false,
26077 asm_specification,
26078 LOOKUP_ONLYCONVERTING);
26079 }
26080
26081 if (pushed_scope)
26082 pop_scope (pushed_scope);
26083 }
26084 }
26085 else
26086 {
26087 cp_id_kind idk;
26088 /* If parsing a type specifier sequence failed, then
26089 this MUST be a simple expression. */
26090 cp_parser_parse_tentatively (parser);
26091 decl = cp_parser_primary_expression (parser, false, false,
26092 false, &idk);
26093 if (!cp_parser_error_occurred (parser)
26094 && decl
26095 && DECL_P (decl)
26096 && CLASS_TYPE_P (TREE_TYPE (decl)))
26097 {
26098 tree rhs;
26099
26100 cp_parser_parse_definitely (parser);
26101 cp_parser_require (parser, CPP_EQ, RT_EQ);
26102 rhs = cp_parser_assignment_expression (parser, false, NULL);
26103 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26104 rhs,
26105 tf_warning_or_error));
26106 add_private_clause = true;
26107 }
26108 else
26109 {
26110 decl = NULL;
26111 cp_parser_abort_tentative_parse (parser);
26112 init = cp_parser_expression (parser, false, NULL);
26113 if (init)
26114 {
26115 if (TREE_CODE (init) == MODIFY_EXPR
26116 || TREE_CODE (init) == MODOP_EXPR)
26117 real_decl = TREE_OPERAND (init, 0);
26118 }
26119 }
26120 }
26121 }
26122 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26123 if (this_pre_body)
26124 {
26125 this_pre_body = pop_stmt_list (this_pre_body);
26126 if (pre_body)
26127 {
26128 tree t = pre_body;
26129 pre_body = push_stmt_list ();
26130 add_stmt (t);
26131 add_stmt (this_pre_body);
26132 pre_body = pop_stmt_list (pre_body);
26133 }
26134 else
26135 pre_body = this_pre_body;
26136 }
26137
26138 if (decl)
26139 real_decl = decl;
26140 if (par_clauses != NULL && real_decl != NULL_TREE)
26141 {
26142 tree *c;
26143 for (c = par_clauses; *c ; )
26144 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26145 && OMP_CLAUSE_DECL (*c) == real_decl)
26146 {
26147 error_at (loc, "iteration variable %qD"
26148 " should not be firstprivate", real_decl);
26149 *c = OMP_CLAUSE_CHAIN (*c);
26150 }
26151 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26152 && OMP_CLAUSE_DECL (*c) == real_decl)
26153 {
26154 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26155 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26156 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26157 OMP_CLAUSE_DECL (l) = real_decl;
26158 OMP_CLAUSE_CHAIN (l) = clauses;
26159 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26160 clauses = l;
26161 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26162 CP_OMP_CLAUSE_INFO (*c) = NULL;
26163 add_private_clause = false;
26164 }
26165 else
26166 {
26167 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26168 && OMP_CLAUSE_DECL (*c) == real_decl)
26169 add_private_clause = false;
26170 c = &OMP_CLAUSE_CHAIN (*c);
26171 }
26172 }
26173
26174 if (add_private_clause)
26175 {
26176 tree c;
26177 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26178 {
26179 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26180 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26181 && OMP_CLAUSE_DECL (c) == decl)
26182 break;
26183 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26184 && OMP_CLAUSE_DECL (c) == decl)
26185 error_at (loc, "iteration variable %qD "
26186 "should not be firstprivate",
26187 decl);
26188 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26189 && OMP_CLAUSE_DECL (c) == decl)
26190 error_at (loc, "iteration variable %qD should not be reduction",
26191 decl);
26192 }
26193 if (c == NULL)
26194 {
26195 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26196 OMP_CLAUSE_DECL (c) = decl;
26197 c = finish_omp_clauses (c);
26198 if (c)
26199 {
26200 OMP_CLAUSE_CHAIN (c) = clauses;
26201 clauses = c;
26202 }
26203 }
26204 }
26205
26206 cond = NULL;
26207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26208 cond = cp_parser_omp_for_cond (parser, decl);
26209 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26210
26211 incr = NULL;
26212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26213 {
26214 /* If decl is an iterator, preserve the operator on decl
26215 until finish_omp_for. */
26216 if (decl
26217 && ((type_dependent_expression_p (decl)
26218 && !POINTER_TYPE_P (TREE_TYPE (decl)))
26219 || CLASS_TYPE_P (TREE_TYPE (decl))))
26220 incr = cp_parser_omp_for_incr (parser, decl);
26221 else
26222 incr = cp_parser_expression (parser, false, NULL);
26223 }
26224
26225 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26226 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26227 /*or_comma=*/false,
26228 /*consume_paren=*/true);
26229
26230 TREE_VEC_ELT (declv, i) = decl;
26231 TREE_VEC_ELT (initv, i) = init;
26232 TREE_VEC_ELT (condv, i) = cond;
26233 TREE_VEC_ELT (incrv, i) = incr;
26234
26235 if (i == collapse - 1)
26236 break;
26237
26238 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26239 in between the collapsed for loops to be still considered perfectly
26240 nested. Hopefully the final version clarifies this.
26241 For now handle (multiple) {'s and empty statements. */
26242 cp_parser_parse_tentatively (parser);
26243 do
26244 {
26245 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26246 break;
26247 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26248 {
26249 cp_lexer_consume_token (parser->lexer);
26250 bracecount++;
26251 }
26252 else if (bracecount
26253 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26254 cp_lexer_consume_token (parser->lexer);
26255 else
26256 {
26257 loc = cp_lexer_peek_token (parser->lexer)->location;
26258 error_at (loc, "not enough collapsed for loops");
26259 collapse_err = true;
26260 cp_parser_abort_tentative_parse (parser);
26261 declv = NULL_TREE;
26262 break;
26263 }
26264 }
26265 while (1);
26266
26267 if (declv)
26268 {
26269 cp_parser_parse_definitely (parser);
26270 nbraces += bracecount;
26271 }
26272 }
26273
26274 /* Note that we saved the original contents of this flag when we entered
26275 the structured block, and so we don't need to re-save it here. */
26276 parser->in_statement = IN_OMP_FOR;
26277
26278 /* Note that the grammar doesn't call for a structured block here,
26279 though the loop as a whole is a structured block. */
26280 body = push_stmt_list ();
26281 cp_parser_statement (parser, NULL_TREE, false, NULL);
26282 body = pop_stmt_list (body);
26283
26284 if (declv == NULL_TREE)
26285 ret = NULL_TREE;
26286 else
26287 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26288 pre_body, clauses);
26289
26290 while (nbraces)
26291 {
26292 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26293 {
26294 cp_lexer_consume_token (parser->lexer);
26295 nbraces--;
26296 }
26297 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26298 cp_lexer_consume_token (parser->lexer);
26299 else
26300 {
26301 if (!collapse_err)
26302 {
26303 error_at (cp_lexer_peek_token (parser->lexer)->location,
26304 "collapsed loops not perfectly nested");
26305 }
26306 collapse_err = true;
26307 cp_parser_statement_seq_opt (parser, NULL);
26308 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26309 break;
26310 }
26311 }
26312
26313 while (!VEC_empty (tree, for_block))
26314 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26315 release_tree_vector (for_block);
26316
26317 return ret;
26318 }
26319
26320 /* OpenMP 2.5:
26321 #pragma omp for for-clause[optseq] new-line
26322 for-loop */
26323
26324 #define OMP_FOR_CLAUSE_MASK \
26325 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26326 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26327 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26328 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26329 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26330 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26331 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26332 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26333
26334 static tree
26335 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26336 {
26337 tree clauses, sb, ret;
26338 unsigned int save;
26339
26340 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26341 "#pragma omp for", pragma_tok);
26342
26343 sb = begin_omp_structured_block ();
26344 save = cp_parser_begin_omp_structured_block (parser);
26345
26346 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26347
26348 cp_parser_end_omp_structured_block (parser, save);
26349 add_stmt (finish_omp_structured_block (sb));
26350
26351 return ret;
26352 }
26353
26354 /* OpenMP 2.5:
26355 # pragma omp master new-line
26356 structured-block */
26357
26358 static tree
26359 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26360 {
26361 cp_parser_require_pragma_eol (parser, pragma_tok);
26362 return c_finish_omp_master (input_location,
26363 cp_parser_omp_structured_block (parser));
26364 }
26365
26366 /* OpenMP 2.5:
26367 # pragma omp ordered new-line
26368 structured-block */
26369
26370 static tree
26371 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26372 {
26373 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26374 cp_parser_require_pragma_eol (parser, pragma_tok);
26375 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26376 }
26377
26378 /* OpenMP 2.5:
26379
26380 section-scope:
26381 { section-sequence }
26382
26383 section-sequence:
26384 section-directive[opt] structured-block
26385 section-sequence section-directive structured-block */
26386
26387 static tree
26388 cp_parser_omp_sections_scope (cp_parser *parser)
26389 {
26390 tree stmt, substmt;
26391 bool error_suppress = false;
26392 cp_token *tok;
26393
26394 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26395 return NULL_TREE;
26396
26397 stmt = push_stmt_list ();
26398
26399 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26400 {
26401 unsigned save;
26402
26403 substmt = begin_omp_structured_block ();
26404 save = cp_parser_begin_omp_structured_block (parser);
26405
26406 while (1)
26407 {
26408 cp_parser_statement (parser, NULL_TREE, false, NULL);
26409
26410 tok = cp_lexer_peek_token (parser->lexer);
26411 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26412 break;
26413 if (tok->type == CPP_CLOSE_BRACE)
26414 break;
26415 if (tok->type == CPP_EOF)
26416 break;
26417 }
26418
26419 cp_parser_end_omp_structured_block (parser, save);
26420 substmt = finish_omp_structured_block (substmt);
26421 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26422 add_stmt (substmt);
26423 }
26424
26425 while (1)
26426 {
26427 tok = cp_lexer_peek_token (parser->lexer);
26428 if (tok->type == CPP_CLOSE_BRACE)
26429 break;
26430 if (tok->type == CPP_EOF)
26431 break;
26432
26433 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26434 {
26435 cp_lexer_consume_token (parser->lexer);
26436 cp_parser_require_pragma_eol (parser, tok);
26437 error_suppress = false;
26438 }
26439 else if (!error_suppress)
26440 {
26441 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26442 error_suppress = true;
26443 }
26444
26445 substmt = cp_parser_omp_structured_block (parser);
26446 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26447 add_stmt (substmt);
26448 }
26449 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26450
26451 substmt = pop_stmt_list (stmt);
26452
26453 stmt = make_node (OMP_SECTIONS);
26454 TREE_TYPE (stmt) = void_type_node;
26455 OMP_SECTIONS_BODY (stmt) = substmt;
26456
26457 add_stmt (stmt);
26458 return stmt;
26459 }
26460
26461 /* OpenMP 2.5:
26462 # pragma omp sections sections-clause[optseq] newline
26463 sections-scope */
26464
26465 #define OMP_SECTIONS_CLAUSE_MASK \
26466 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26467 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26468 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26469 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26470 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26471
26472 static tree
26473 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26474 {
26475 tree clauses, ret;
26476
26477 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26478 "#pragma omp sections", pragma_tok);
26479
26480 ret = cp_parser_omp_sections_scope (parser);
26481 if (ret)
26482 OMP_SECTIONS_CLAUSES (ret) = clauses;
26483
26484 return ret;
26485 }
26486
26487 /* OpenMP 2.5:
26488 # pragma parallel parallel-clause new-line
26489 # pragma parallel for parallel-for-clause new-line
26490 # pragma parallel sections parallel-sections-clause new-line */
26491
26492 #define OMP_PARALLEL_CLAUSE_MASK \
26493 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26494 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26495 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26496 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26497 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26498 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26499 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26500 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26501
26502 static tree
26503 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26504 {
26505 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26506 const char *p_name = "#pragma omp parallel";
26507 tree stmt, clauses, par_clause, ws_clause, block;
26508 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26509 unsigned int save;
26510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26511
26512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26513 {
26514 cp_lexer_consume_token (parser->lexer);
26515 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26516 p_name = "#pragma omp parallel for";
26517 mask |= OMP_FOR_CLAUSE_MASK;
26518 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26519 }
26520 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26521 {
26522 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26523 const char *p = IDENTIFIER_POINTER (id);
26524 if (strcmp (p, "sections") == 0)
26525 {
26526 cp_lexer_consume_token (parser->lexer);
26527 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26528 p_name = "#pragma omp parallel sections";
26529 mask |= OMP_SECTIONS_CLAUSE_MASK;
26530 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26531 }
26532 }
26533
26534 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26535 block = begin_omp_parallel ();
26536 save = cp_parser_begin_omp_structured_block (parser);
26537
26538 switch (p_kind)
26539 {
26540 case PRAGMA_OMP_PARALLEL:
26541 cp_parser_statement (parser, NULL_TREE, false, NULL);
26542 par_clause = clauses;
26543 break;
26544
26545 case PRAGMA_OMP_PARALLEL_FOR:
26546 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26547 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26548 break;
26549
26550 case PRAGMA_OMP_PARALLEL_SECTIONS:
26551 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26552 stmt = cp_parser_omp_sections_scope (parser);
26553 if (stmt)
26554 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26555 break;
26556
26557 default:
26558 gcc_unreachable ();
26559 }
26560
26561 cp_parser_end_omp_structured_block (parser, save);
26562 stmt = finish_omp_parallel (par_clause, block);
26563 if (p_kind != PRAGMA_OMP_PARALLEL)
26564 OMP_PARALLEL_COMBINED (stmt) = 1;
26565 return stmt;
26566 }
26567
26568 /* OpenMP 2.5:
26569 # pragma omp single single-clause[optseq] new-line
26570 structured-block */
26571
26572 #define OMP_SINGLE_CLAUSE_MASK \
26573 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26574 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26575 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26576 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26577
26578 static tree
26579 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26580 {
26581 tree stmt = make_node (OMP_SINGLE);
26582 TREE_TYPE (stmt) = void_type_node;
26583
26584 OMP_SINGLE_CLAUSES (stmt)
26585 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26586 "#pragma omp single", pragma_tok);
26587 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26588
26589 return add_stmt (stmt);
26590 }
26591
26592 /* OpenMP 3.0:
26593 # pragma omp task task-clause[optseq] new-line
26594 structured-block */
26595
26596 #define OMP_TASK_CLAUSE_MASK \
26597 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26598 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26599 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26600 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26601 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26602 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26603 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26604 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26605
26606 static tree
26607 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26608 {
26609 tree clauses, block;
26610 unsigned int save;
26611
26612 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26613 "#pragma omp task", pragma_tok);
26614 block = begin_omp_task ();
26615 save = cp_parser_begin_omp_structured_block (parser);
26616 cp_parser_statement (parser, NULL_TREE, false, NULL);
26617 cp_parser_end_omp_structured_block (parser, save);
26618 return finish_omp_task (clauses, block);
26619 }
26620
26621 /* OpenMP 3.0:
26622 # pragma omp taskwait new-line */
26623
26624 static void
26625 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26626 {
26627 cp_parser_require_pragma_eol (parser, pragma_tok);
26628 finish_omp_taskwait ();
26629 }
26630
26631 /* OpenMP 3.1:
26632 # pragma omp taskyield new-line */
26633
26634 static void
26635 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26636 {
26637 cp_parser_require_pragma_eol (parser, pragma_tok);
26638 finish_omp_taskyield ();
26639 }
26640
26641 /* OpenMP 2.5:
26642 # pragma omp threadprivate (variable-list) */
26643
26644 static void
26645 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26646 {
26647 tree vars;
26648
26649 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26650 cp_parser_require_pragma_eol (parser, pragma_tok);
26651
26652 finish_omp_threadprivate (vars);
26653 }
26654
26655 /* Main entry point to OpenMP statement pragmas. */
26656
26657 static void
26658 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26659 {
26660 tree stmt;
26661
26662 switch (pragma_tok->pragma_kind)
26663 {
26664 case PRAGMA_OMP_ATOMIC:
26665 cp_parser_omp_atomic (parser, pragma_tok);
26666 return;
26667 case PRAGMA_OMP_CRITICAL:
26668 stmt = cp_parser_omp_critical (parser, pragma_tok);
26669 break;
26670 case PRAGMA_OMP_FOR:
26671 stmt = cp_parser_omp_for (parser, pragma_tok);
26672 break;
26673 case PRAGMA_OMP_MASTER:
26674 stmt = cp_parser_omp_master (parser, pragma_tok);
26675 break;
26676 case PRAGMA_OMP_ORDERED:
26677 stmt = cp_parser_omp_ordered (parser, pragma_tok);
26678 break;
26679 case PRAGMA_OMP_PARALLEL:
26680 stmt = cp_parser_omp_parallel (parser, pragma_tok);
26681 break;
26682 case PRAGMA_OMP_SECTIONS:
26683 stmt = cp_parser_omp_sections (parser, pragma_tok);
26684 break;
26685 case PRAGMA_OMP_SINGLE:
26686 stmt = cp_parser_omp_single (parser, pragma_tok);
26687 break;
26688 case PRAGMA_OMP_TASK:
26689 stmt = cp_parser_omp_task (parser, pragma_tok);
26690 break;
26691 default:
26692 gcc_unreachable ();
26693 }
26694
26695 if (stmt)
26696 SET_EXPR_LOCATION (stmt, pragma_tok->location);
26697 }
26698 \f
26699 /* Transactional Memory parsing routines. */
26700
26701 /* Parse a transaction attribute.
26702
26703 txn-attribute:
26704 attribute
26705 [ [ identifier ] ]
26706
26707 ??? Simplify this when C++0x bracket attributes are
26708 implemented properly. */
26709
26710 static tree
26711 cp_parser_txn_attribute_opt (cp_parser *parser)
26712 {
26713 cp_token *token;
26714 tree attr_name, attr = NULL;
26715
26716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26717 return cp_parser_attributes_opt (parser);
26718
26719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26720 return NULL_TREE;
26721 cp_lexer_consume_token (parser->lexer);
26722 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26723 goto error1;
26724
26725 token = cp_lexer_peek_token (parser->lexer);
26726 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26727 {
26728 token = cp_lexer_consume_token (parser->lexer);
26729
26730 attr_name = (token->type == CPP_KEYWORD
26731 /* For keywords, use the canonical spelling,
26732 not the parsed identifier. */
26733 ? ridpointers[(int) token->keyword]
26734 : token->u.value);
26735 attr = build_tree_list (attr_name, NULL_TREE);
26736 }
26737 else
26738 cp_parser_error (parser, "expected identifier");
26739
26740 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26741 error1:
26742 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26743 return attr;
26744 }
26745
26746 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26747
26748 transaction-statement:
26749 __transaction_atomic txn-attribute[opt] txn-exception-spec[opt]
26750 compound-statement
26751 __transaction_relaxed txn-exception-spec[opt] compound-statement
26752
26753 ??? The exception specification is not yet implemented.
26754 */
26755
26756 static tree
26757 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26758 {
26759 unsigned char old_in = parser->in_transaction;
26760 unsigned char this_in = 1, new_in;
26761 cp_token *token;
26762 tree stmt, attrs;
26763
26764 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26765 || keyword == RID_TRANSACTION_RELAXED);
26766 token = cp_parser_require_keyword (parser, keyword,
26767 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26768 : RT_TRANSACTION_RELAXED));
26769 gcc_assert (token != NULL);
26770
26771 if (keyword == RID_TRANSACTION_RELAXED)
26772 this_in |= TM_STMT_ATTR_RELAXED;
26773 else
26774 {
26775 attrs = cp_parser_txn_attribute_opt (parser);
26776 if (attrs)
26777 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26778 }
26779
26780 /* Keep track if we're in the lexical scope of an outer transaction. */
26781 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
26782
26783 stmt = begin_transaction_stmt (token->location, NULL, this_in);
26784
26785 parser->in_transaction = new_in;
26786 cp_parser_compound_statement (parser, NULL, false, false);
26787 parser->in_transaction = old_in;
26788
26789 finish_transaction_stmt (stmt, NULL, this_in);
26790
26791 return stmt;
26792 }
26793
26794 /* Parse a __transaction_atomic or __transaction_relaxed expression.
26795
26796 transaction-expression:
26797 __transaction_atomic txn-exception-spec[opt] ( expression )
26798 __transaction_relaxed txn-exception-spec[opt] ( expression )
26799
26800 ??? The exception specification is not yet implemented.
26801 */
26802
26803 static tree
26804 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
26805 {
26806 unsigned char old_in = parser->in_transaction;
26807 unsigned char this_in = 1;
26808 cp_token *token;
26809 tree ret;
26810
26811 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26812 || keyword == RID_TRANSACTION_RELAXED);
26813
26814 if (!flag_tm)
26815 error (keyword == RID_TRANSACTION_RELAXED
26816 ? G_("%<__transaction_relaxed%> without transactional memory "
26817 "support enabled")
26818 : G_("%<__transaction_atomic%> without transactional memory "
26819 "support enabled"));
26820
26821 token = cp_parser_require_keyword (parser, keyword,
26822 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26823 : RT_TRANSACTION_RELAXED));
26824 gcc_assert (token != NULL);
26825
26826 if (keyword == RID_TRANSACTION_RELAXED)
26827 this_in |= TM_STMT_ATTR_RELAXED;
26828
26829 parser->in_transaction = this_in;
26830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26831 {
26832 tree expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26833 ret = build_transaction_expr (token->location, expr, this_in);
26834 }
26835 else
26836 {
26837 cp_parser_error (parser, "expected %<(%>");
26838 ret = error_mark_node;
26839 }
26840 parser->in_transaction = old_in;
26841
26842 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
26843 return error_mark_node;
26844
26845 return (flag_tm ? ret : error_mark_node);
26846 }
26847
26848 /* Parse a function-transaction-block.
26849
26850 function-transaction-block:
26851 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
26852 function-body
26853 __transaction_atomic txn-attribute[opt] function-try-block
26854 __transaction_relaxed ctor-initializer[opt] function-body
26855 __transaction_relaxed function-try-block
26856 */
26857
26858 static bool
26859 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
26860 {
26861 unsigned char old_in = parser->in_transaction;
26862 unsigned char new_in = 1;
26863 tree compound_stmt, stmt, attrs;
26864 bool ctor_initializer_p;
26865 cp_token *token;
26866
26867 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26868 || keyword == RID_TRANSACTION_RELAXED);
26869 token = cp_parser_require_keyword (parser, keyword,
26870 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26871 : RT_TRANSACTION_RELAXED));
26872 gcc_assert (token != NULL);
26873
26874 if (keyword == RID_TRANSACTION_RELAXED)
26875 new_in |= TM_STMT_ATTR_RELAXED;
26876 else
26877 {
26878 attrs = cp_parser_txn_attribute_opt (parser);
26879 if (attrs)
26880 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26881 }
26882
26883 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
26884
26885 parser->in_transaction = new_in;
26886
26887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26888 ctor_initializer_p = cp_parser_function_try_block (parser);
26889 else
26890 ctor_initializer_p
26891 = cp_parser_ctor_initializer_opt_and_function_body (parser);
26892
26893 parser->in_transaction = old_in;
26894
26895 finish_transaction_stmt (stmt, compound_stmt, new_in);
26896
26897 return ctor_initializer_p;
26898 }
26899
26900 /* Parse a __transaction_cancel statement.
26901
26902 cancel-statement:
26903 __transaction_cancel txn-attribute[opt] ;
26904 __transaction_cancel txn-attribute[opt] throw-expression ;
26905
26906 ??? Cancel and throw is not yet implemented. */
26907
26908 static tree
26909 cp_parser_transaction_cancel (cp_parser *parser)
26910 {
26911 cp_token *token;
26912 bool is_outer = false;
26913 tree stmt, attrs;
26914
26915 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
26916 RT_TRANSACTION_CANCEL);
26917 gcc_assert (token != NULL);
26918
26919 attrs = cp_parser_txn_attribute_opt (parser);
26920 if (attrs)
26921 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
26922
26923 /* ??? Parse cancel-and-throw here. */
26924
26925 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26926
26927 if (!flag_tm)
26928 {
26929 error_at (token->location, "%<__transaction_cancel%> without "
26930 "transactional memory support enabled");
26931 return error_mark_node;
26932 }
26933 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
26934 {
26935 error_at (token->location, "%<__transaction_cancel%> within a "
26936 "%<__transaction_relaxed%>");
26937 return error_mark_node;
26938 }
26939 else if (is_outer)
26940 {
26941 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
26942 && !is_tm_may_cancel_outer (current_function_decl))
26943 {
26944 error_at (token->location, "outer %<__transaction_cancel%> not "
26945 "within outer %<__transaction_atomic%>");
26946 error_at (token->location,
26947 " or a %<transaction_may_cancel_outer%> function");
26948 return error_mark_node;
26949 }
26950 }
26951 else if (parser->in_transaction == 0)
26952 {
26953 error_at (token->location, "%<__transaction_cancel%> not within "
26954 "%<__transaction_atomic%>");
26955 return error_mark_node;
26956 }
26957
26958 stmt = build_tm_abort_call (token->location, is_outer);
26959 add_stmt (stmt);
26960 finish_stmt ();
26961
26962 return stmt;
26963 }
26964 \f
26965 /* The parser. */
26966
26967 static GTY (()) cp_parser *the_parser;
26968
26969 \f
26970 /* Special handling for the first token or line in the file. The first
26971 thing in the file might be #pragma GCC pch_preprocess, which loads a
26972 PCH file, which is a GC collection point. So we need to handle this
26973 first pragma without benefit of an existing lexer structure.
26974
26975 Always returns one token to the caller in *FIRST_TOKEN. This is
26976 either the true first token of the file, or the first token after
26977 the initial pragma. */
26978
26979 static void
26980 cp_parser_initial_pragma (cp_token *first_token)
26981 {
26982 tree name = NULL;
26983
26984 cp_lexer_get_preprocessor_token (NULL, first_token);
26985 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
26986 return;
26987
26988 cp_lexer_get_preprocessor_token (NULL, first_token);
26989 if (first_token->type == CPP_STRING)
26990 {
26991 name = first_token->u.value;
26992
26993 cp_lexer_get_preprocessor_token (NULL, first_token);
26994 if (first_token->type != CPP_PRAGMA_EOL)
26995 error_at (first_token->location,
26996 "junk at end of %<#pragma GCC pch_preprocess%>");
26997 }
26998 else
26999 error_at (first_token->location, "expected string literal");
27000
27001 /* Skip to the end of the pragma. */
27002 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27003 cp_lexer_get_preprocessor_token (NULL, first_token);
27004
27005 /* Now actually load the PCH file. */
27006 if (name)
27007 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27008
27009 /* Read one more token to return to our caller. We have to do this
27010 after reading the PCH file in, since its pointers have to be
27011 live. */
27012 cp_lexer_get_preprocessor_token (NULL, first_token);
27013 }
27014
27015 /* Normal parsing of a pragma token. Here we can (and must) use the
27016 regular lexer. */
27017
27018 static bool
27019 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27020 {
27021 cp_token *pragma_tok;
27022 unsigned int id;
27023
27024 pragma_tok = cp_lexer_consume_token (parser->lexer);
27025 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27026 parser->lexer->in_pragma = true;
27027
27028 id = pragma_tok->pragma_kind;
27029 switch (id)
27030 {
27031 case PRAGMA_GCC_PCH_PREPROCESS:
27032 error_at (pragma_tok->location,
27033 "%<#pragma GCC pch_preprocess%> must be first");
27034 break;
27035
27036 case PRAGMA_OMP_BARRIER:
27037 switch (context)
27038 {
27039 case pragma_compound:
27040 cp_parser_omp_barrier (parser, pragma_tok);
27041 return false;
27042 case pragma_stmt:
27043 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27044 "used in compound statements");
27045 break;
27046 default:
27047 goto bad_stmt;
27048 }
27049 break;
27050
27051 case PRAGMA_OMP_FLUSH:
27052 switch (context)
27053 {
27054 case pragma_compound:
27055 cp_parser_omp_flush (parser, pragma_tok);
27056 return false;
27057 case pragma_stmt:
27058 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27059 "used in compound statements");
27060 break;
27061 default:
27062 goto bad_stmt;
27063 }
27064 break;
27065
27066 case PRAGMA_OMP_TASKWAIT:
27067 switch (context)
27068 {
27069 case pragma_compound:
27070 cp_parser_omp_taskwait (parser, pragma_tok);
27071 return false;
27072 case pragma_stmt:
27073 error_at (pragma_tok->location,
27074 "%<#pragma omp taskwait%> may only be "
27075 "used in compound statements");
27076 break;
27077 default:
27078 goto bad_stmt;
27079 }
27080 break;
27081
27082 case PRAGMA_OMP_TASKYIELD:
27083 switch (context)
27084 {
27085 case pragma_compound:
27086 cp_parser_omp_taskyield (parser, pragma_tok);
27087 return false;
27088 case pragma_stmt:
27089 error_at (pragma_tok->location,
27090 "%<#pragma omp taskyield%> may only be "
27091 "used in compound statements");
27092 break;
27093 default:
27094 goto bad_stmt;
27095 }
27096 break;
27097
27098 case PRAGMA_OMP_THREADPRIVATE:
27099 cp_parser_omp_threadprivate (parser, pragma_tok);
27100 return false;
27101
27102 case PRAGMA_OMP_ATOMIC:
27103 case PRAGMA_OMP_CRITICAL:
27104 case PRAGMA_OMP_FOR:
27105 case PRAGMA_OMP_MASTER:
27106 case PRAGMA_OMP_ORDERED:
27107 case PRAGMA_OMP_PARALLEL:
27108 case PRAGMA_OMP_SECTIONS:
27109 case PRAGMA_OMP_SINGLE:
27110 case PRAGMA_OMP_TASK:
27111 if (context == pragma_external)
27112 goto bad_stmt;
27113 cp_parser_omp_construct (parser, pragma_tok);
27114 return true;
27115
27116 case PRAGMA_OMP_SECTION:
27117 error_at (pragma_tok->location,
27118 "%<#pragma omp section%> may only be used in "
27119 "%<#pragma omp sections%> construct");
27120 break;
27121
27122 default:
27123 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27124 c_invoke_pragma_handler (id);
27125 break;
27126
27127 bad_stmt:
27128 cp_parser_error (parser, "expected declaration specifiers");
27129 break;
27130 }
27131
27132 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27133 return false;
27134 }
27135
27136 /* The interface the pragma parsers have to the lexer. */
27137
27138 enum cpp_ttype
27139 pragma_lex (tree *value)
27140 {
27141 cp_token *tok;
27142 enum cpp_ttype ret;
27143
27144 tok = cp_lexer_peek_token (the_parser->lexer);
27145
27146 ret = tok->type;
27147 *value = tok->u.value;
27148
27149 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27150 ret = CPP_EOF;
27151 else if (ret == CPP_STRING)
27152 *value = cp_parser_string_literal (the_parser, false, false);
27153 else
27154 {
27155 cp_lexer_consume_token (the_parser->lexer);
27156 if (ret == CPP_KEYWORD)
27157 ret = CPP_NAME;
27158 }
27159
27160 return ret;
27161 }
27162
27163 \f
27164 /* External interface. */
27165
27166 /* Parse one entire translation unit. */
27167
27168 void
27169 c_parse_file (void)
27170 {
27171 static bool already_called = false;
27172
27173 if (already_called)
27174 {
27175 sorry ("inter-module optimizations not implemented for C++");
27176 return;
27177 }
27178 already_called = true;
27179
27180 the_parser = cp_parser_new ();
27181 push_deferring_access_checks (flag_access_control
27182 ? dk_no_deferred : dk_no_check);
27183 cp_parser_translation_unit (the_parser);
27184 the_parser = NULL;
27185 }
27186
27187 #include "gt-cp-parser.h"