0a075b221cfefbbd4b21a67f7ea7d09103876f56
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "target.h"
35 #include "cgraph.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "plugin.h"
39 #include "tree-pretty-print.h"
40 #include "parser.h"
41
42 \f
43 /* The lexer. */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
47
48 static cp_token eof_token =
49 {
50 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
51 };
52
53 /* The various kinds of non integral constant we encounter. */
54 typedef enum non_integral_constant {
55 NIC_NONE,
56 /* floating-point literal */
57 NIC_FLOAT,
58 /* %<this%> */
59 NIC_THIS,
60 /* %<__FUNCTION__%> */
61 NIC_FUNC_NAME,
62 /* %<__PRETTY_FUNCTION__%> */
63 NIC_PRETTY_FUNC,
64 /* %<__func__%> */
65 NIC_C99_FUNC,
66 /* "%<va_arg%> */
67 NIC_VA_ARG,
68 /* a cast */
69 NIC_CAST,
70 /* %<typeid%> operator */
71 NIC_TYPEID,
72 /* non-constant compound literals */
73 NIC_NCC,
74 /* a function call */
75 NIC_FUNC_CALL,
76 /* an increment */
77 NIC_INC,
78 /* an decrement */
79 NIC_DEC,
80 /* an array reference */
81 NIC_ARRAY_REF,
82 /* %<->%> */
83 NIC_ARROW,
84 /* %<.%> */
85 NIC_POINT,
86 /* the address of a label */
87 NIC_ADDR_LABEL,
88 /* %<*%> */
89 NIC_STAR,
90 /* %<&%> */
91 NIC_ADDR,
92 /* %<++%> */
93 NIC_PREINCREMENT,
94 /* %<--%> */
95 NIC_PREDECREMENT,
96 /* %<new%> */
97 NIC_NEW,
98 /* %<delete%> */
99 NIC_DEL,
100 /* calls to overloaded operators */
101 NIC_OVERLOADED,
102 /* an assignment */
103 NIC_ASSIGNMENT,
104 /* a comma operator */
105 NIC_COMMA,
106 /* a call to a constructor */
107 NIC_CONSTRUCTOR,
108 /* a transaction expression */
109 NIC_TRANSACTION
110 } non_integral_constant;
111
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114 /* NULL */
115 NLE_NULL,
116 /* is not a type */
117 NLE_TYPE,
118 /* is not a class or namespace */
119 NLE_CXX98,
120 /* is not a class, namespace, or enumeration */
121 NLE_NOT_CXX98
122 } name_lookup_error;
123
124 /* The various kinds of required token */
125 typedef enum required_token {
126 RT_NONE,
127 RT_SEMICOLON, /* ';' */
128 RT_OPEN_PAREN, /* '(' */
129 RT_CLOSE_BRACE, /* '}' */
130 RT_OPEN_BRACE, /* '{' */
131 RT_CLOSE_SQUARE, /* ']' */
132 RT_OPEN_SQUARE, /* '[' */
133 RT_COMMA, /* ',' */
134 RT_SCOPE, /* '::' */
135 RT_LESS, /* '<' */
136 RT_GREATER, /* '>' */
137 RT_EQ, /* '=' */
138 RT_ELLIPSIS, /* '...' */
139 RT_MULT, /* '*' */
140 RT_COMPL, /* '~' */
141 RT_COLON, /* ':' */
142 RT_COLON_SCOPE, /* ':' or '::' */
143 RT_CLOSE_PAREN, /* ')' */
144 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145 RT_PRAGMA_EOL, /* end of line */
146 RT_NAME, /* identifier */
147
148 /* The type is CPP_KEYWORD */
149 RT_NEW, /* new */
150 RT_DELETE, /* delete */
151 RT_RETURN, /* return */
152 RT_WHILE, /* while */
153 RT_EXTERN, /* extern */
154 RT_STATIC_ASSERT, /* static_assert */
155 RT_DECLTYPE, /* decltype */
156 RT_OPERATOR, /* operator */
157 RT_CLASS, /* class */
158 RT_TEMPLATE, /* template */
159 RT_NAMESPACE, /* namespace */
160 RT_USING, /* using */
161 RT_ASM, /* asm */
162 RT_TRY, /* try */
163 RT_CATCH, /* catch */
164 RT_THROW, /* throw */
165 RT_LABEL, /* __label__ */
166 RT_AT_TRY, /* @try */
167 RT_AT_SYNCHRONIZED, /* @synchronized */
168 RT_AT_THROW, /* @throw */
169
170 RT_SELECT, /* selection-statement */
171 RT_INTERATION, /* iteration-statement */
172 RT_JUMP, /* jump-statement */
173 RT_CLASS_KEY, /* class-key */
174 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
175 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
176 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
177 RT_TRANSACTION_CANCEL /* __transaction_cancel */
178 } required_token;
179
180 /* Prototypes. */
181
182 static cp_lexer *cp_lexer_new_main
183 (void);
184 static cp_lexer *cp_lexer_new_from_tokens
185 (cp_token_cache *tokens);
186 static void cp_lexer_destroy
187 (cp_lexer *);
188 static int cp_lexer_saving_tokens
189 (const cp_lexer *);
190 static cp_token *cp_lexer_token_at
191 (cp_lexer *, cp_token_position);
192 static void cp_lexer_get_preprocessor_token
193 (cp_lexer *, cp_token *);
194 static inline cp_token *cp_lexer_peek_token
195 (cp_lexer *);
196 static cp_token *cp_lexer_peek_nth_token
197 (cp_lexer *, size_t);
198 static inline bool cp_lexer_next_token_is
199 (cp_lexer *, enum cpp_ttype);
200 static bool cp_lexer_next_token_is_not
201 (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_keyword
203 (cp_lexer *, enum rid);
204 static cp_token *cp_lexer_consume_token
205 (cp_lexer *);
206 static void cp_lexer_purge_token
207 (cp_lexer *);
208 static void cp_lexer_purge_tokens_after
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_save_tokens
211 (cp_lexer *);
212 static void cp_lexer_commit_tokens
213 (cp_lexer *);
214 static void cp_lexer_rollback_tokens
215 (cp_lexer *);
216 static void cp_lexer_print_token
217 (FILE *, cp_token *);
218 static inline bool cp_lexer_debugging_p
219 (cp_lexer *);
220 static void cp_lexer_start_debugging
221 (cp_lexer *) ATTRIBUTE_UNUSED;
222 static void cp_lexer_stop_debugging
223 (cp_lexer *) ATTRIBUTE_UNUSED;
224
225 static cp_token_cache *cp_token_cache_new
226 (cp_token *, cp_token *);
227
228 static void cp_parser_initial_pragma
229 (cp_token *);
230
231 static tree cp_literal_operator_id
232 (const char *);
233
234 /* Manifest constants. */
235 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
236 #define CP_SAVED_TOKEN_STACK 5
237
238 /* Variables. */
239
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream;
242
243 /* Nonzero if we are parsing an unevaluated operand: an operand to
244 sizeof, typeof, or alignof. */
245 int cp_unevaluated_operand;
246
247 /* Dump up to NUM tokens in BUFFER to FILE starting with token
248 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
249 first token in BUFFER. If NUM is 0, dump all the tokens. If
250 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
251 highlighted by surrounding it in [[ ]]. */
252
253 static void
254 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
255 cp_token *start_token, unsigned num,
256 cp_token *curr_token)
257 {
258 unsigned i, nprinted;
259 cp_token *token;
260 bool do_print;
261
262 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
263
264 if (buffer == NULL)
265 return;
266
267 if (num == 0)
268 num = buffer->length ();
269
270 if (start_token == NULL)
271 start_token = buffer->address ();
272
273 if (start_token > buffer->address ())
274 {
275 cp_lexer_print_token (file, &(*buffer)[0]);
276 fprintf (file, " ... ");
277 }
278
279 do_print = false;
280 nprinted = 0;
281 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
282 {
283 if (token == start_token)
284 do_print = true;
285
286 if (!do_print)
287 continue;
288
289 nprinted++;
290 if (token == curr_token)
291 fprintf (file, "[[");
292
293 cp_lexer_print_token (file, token);
294
295 if (token == curr_token)
296 fprintf (file, "]]");
297
298 switch (token->type)
299 {
300 case CPP_SEMICOLON:
301 case CPP_OPEN_BRACE:
302 case CPP_CLOSE_BRACE:
303 case CPP_EOF:
304 fputc ('\n', file);
305 break;
306
307 default:
308 fputc (' ', file);
309 }
310 }
311
312 if (i == num && i < buffer->length ())
313 {
314 fprintf (file, " ... ");
315 cp_lexer_print_token (file, &buffer->last ());
316 }
317
318 fprintf (file, "\n");
319 }
320
321
322 /* Dump all tokens in BUFFER to stderr. */
323
324 void
325 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
326 {
327 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
328 }
329
330 DEBUG_FUNCTION void
331 debug (vec<cp_token, va_gc> &ref)
332 {
333 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
334 }
335
336 DEBUG_FUNCTION void
337 debug (vec<cp_token, va_gc> *ptr)
338 {
339 if (ptr)
340 debug (*ptr);
341 else
342 fprintf (stderr, "<nil>\n");
343 }
344
345
346 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
347 description for T. */
348
349 static void
350 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
351 {
352 if (t)
353 {
354 fprintf (file, "%s: ", desc);
355 print_node_brief (file, "", t, 0);
356 }
357 }
358
359
360 /* Dump parser context C to FILE. */
361
362 static void
363 cp_debug_print_context (FILE *file, cp_parser_context *c)
364 {
365 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
366 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
367 print_node_brief (file, "", c->object_type, 0);
368 fprintf (file, "}\n");
369 }
370
371
372 /* Print the stack of parsing contexts to FILE starting with FIRST. */
373
374 static void
375 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
376 {
377 unsigned i;
378 cp_parser_context *c;
379
380 fprintf (file, "Parsing context stack:\n");
381 for (i = 0, c = first; c; c = c->next, i++)
382 {
383 fprintf (file, "\t#%u: ", i);
384 cp_debug_print_context (file, c);
385 }
386 }
387
388
389 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
390
391 static void
392 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
393 {
394 if (flag)
395 fprintf (file, "%s: true\n", desc);
396 }
397
398
399 /* Print an unparsed function entry UF to FILE. */
400
401 static void
402 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
403 {
404 unsigned i;
405 cp_default_arg_entry *default_arg_fn;
406 tree fn;
407
408 fprintf (file, "\tFunctions with default args:\n");
409 for (i = 0;
410 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
411 i++)
412 {
413 fprintf (file, "\t\tClass type: ");
414 print_node_brief (file, "", default_arg_fn->class_type, 0);
415 fprintf (file, "\t\tDeclaration: ");
416 print_node_brief (file, "", default_arg_fn->decl, 0);
417 fprintf (file, "\n");
418 }
419
420 fprintf (file, "\n\tFunctions with definitions that require "
421 "post-processing\n\t\t");
422 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
423 {
424 print_node_brief (file, "", fn, 0);
425 fprintf (file, " ");
426 }
427 fprintf (file, "\n");
428
429 fprintf (file, "\n\tNon-static data members with initializers that require "
430 "post-processing\n\t\t");
431 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
432 {
433 print_node_brief (file, "", fn, 0);
434 fprintf (file, " ");
435 }
436 fprintf (file, "\n");
437 }
438
439
440 /* Print the stack of unparsed member functions S to FILE. */
441
442 static void
443 cp_debug_print_unparsed_queues (FILE *file,
444 vec<cp_unparsed_functions_entry, va_gc> *s)
445 {
446 unsigned i;
447 cp_unparsed_functions_entry *uf;
448
449 fprintf (file, "Unparsed functions\n");
450 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
451 {
452 fprintf (file, "#%u:\n", i);
453 cp_debug_print_unparsed_function (file, uf);
454 }
455 }
456
457
458 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
459 the given PARSER. If FILE is NULL, the output is printed on stderr. */
460
461 static void
462 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
463 {
464 cp_token *next_token, *first_token, *start_token;
465
466 if (file == NULL)
467 file = stderr;
468
469 next_token = parser->lexer->next_token;
470 first_token = parser->lexer->buffer->address ();
471 start_token = (next_token > first_token + window_size / 2)
472 ? next_token - window_size / 2
473 : first_token;
474 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
475 next_token);
476 }
477
478
479 /* Dump debugging information for the given PARSER. If FILE is NULL,
480 the output is printed on stderr. */
481
482 void
483 cp_debug_parser (FILE *file, cp_parser *parser)
484 {
485 const size_t window_size = 20;
486 cp_token *token;
487 expanded_location eloc;
488
489 if (file == NULL)
490 file = stderr;
491
492 fprintf (file, "Parser state\n\n");
493 fprintf (file, "Number of tokens: %u\n",
494 vec_safe_length (parser->lexer->buffer));
495 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
496 cp_debug_print_tree_if_set (file, "Object scope",
497 parser->object_scope);
498 cp_debug_print_tree_if_set (file, "Qualifying scope",
499 parser->qualifying_scope);
500 cp_debug_print_context_stack (file, parser->context);
501 cp_debug_print_flag (file, "Allow GNU extensions",
502 parser->allow_gnu_extensions_p);
503 cp_debug_print_flag (file, "'>' token is greater-than",
504 parser->greater_than_is_operator_p);
505 cp_debug_print_flag (file, "Default args allowed in current "
506 "parameter list", parser->default_arg_ok_p);
507 cp_debug_print_flag (file, "Parsing integral constant-expression",
508 parser->integral_constant_expression_p);
509 cp_debug_print_flag (file, "Allow non-constant expression in current "
510 "constant-expression",
511 parser->allow_non_integral_constant_expression_p);
512 cp_debug_print_flag (file, "Seen non-constant expression",
513 parser->non_integral_constant_expression_p);
514 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
515 "current context",
516 parser->local_variables_forbidden_p);
517 cp_debug_print_flag (file, "In unbraced linkage specification",
518 parser->in_unbraced_linkage_specification_p);
519 cp_debug_print_flag (file, "Parsing a declarator",
520 parser->in_declarator_p);
521 cp_debug_print_flag (file, "In template argument list",
522 parser->in_template_argument_list_p);
523 cp_debug_print_flag (file, "Parsing an iteration statement",
524 parser->in_statement & IN_ITERATION_STMT);
525 cp_debug_print_flag (file, "Parsing a switch statement",
526 parser->in_statement & IN_SWITCH_STMT);
527 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
528 parser->in_statement & IN_OMP_BLOCK);
529 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
530 parser->in_statement & IN_OMP_FOR);
531 cp_debug_print_flag (file, "Parsing an if statement",
532 parser->in_statement & IN_IF_STMT);
533 cp_debug_print_flag (file, "Parsing a type-id in an expression "
534 "context", parser->in_type_id_in_expr_p);
535 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
536 parser->implicit_extern_c);
537 cp_debug_print_flag (file, "String expressions should be translated "
538 "to execution character set",
539 parser->translate_strings_p);
540 cp_debug_print_flag (file, "Parsing function body outside of a "
541 "local class", parser->in_function_body);
542 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
543 parser->colon_corrects_to_scope_p);
544 if (parser->type_definition_forbidden_message)
545 fprintf (file, "Error message for forbidden type definitions: %s\n",
546 parser->type_definition_forbidden_message);
547 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
548 fprintf (file, "Number of class definitions in progress: %u\n",
549 parser->num_classes_being_defined);
550 fprintf (file, "Number of template parameter lists for the current "
551 "declaration: %u\n", parser->num_template_parameter_lists);
552 cp_debug_parser_tokens (file, parser, window_size);
553 token = parser->lexer->next_token;
554 fprintf (file, "Next token to parse:\n");
555 fprintf (file, "\tToken: ");
556 cp_lexer_print_token (file, token);
557 eloc = expand_location (token->location);
558 fprintf (file, "\n\tFile: %s\n", eloc.file);
559 fprintf (file, "\tLine: %d\n", eloc.line);
560 fprintf (file, "\tColumn: %d\n", eloc.column);
561 }
562
563 DEBUG_FUNCTION void
564 debug (cp_parser &ref)
565 {
566 cp_debug_parser (stderr, &ref);
567 }
568
569 DEBUG_FUNCTION void
570 debug (cp_parser *ptr)
571 {
572 if (ptr)
573 debug (*ptr);
574 else
575 fprintf (stderr, "<nil>\n");
576 }
577
578 /* Allocate memory for a new lexer object and return it. */
579
580 static cp_lexer *
581 cp_lexer_alloc (void)
582 {
583 cp_lexer *lexer;
584
585 c_common_no_more_pch ();
586
587 /* Allocate the memory. */
588 lexer = ggc_alloc_cleared_cp_lexer ();
589
590 /* Initially we are not debugging. */
591 lexer->debugging_p = false;
592
593 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
594
595 /* Create the buffer. */
596 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
597
598 return lexer;
599 }
600
601
602 /* Create a new main C++ lexer, the lexer that gets tokens from the
603 preprocessor. */
604
605 static cp_lexer *
606 cp_lexer_new_main (void)
607 {
608 cp_lexer *lexer;
609 cp_token token;
610
611 /* It's possible that parsing the first pragma will load a PCH file,
612 which is a GC collection point. So we have to do that before
613 allocating any memory. */
614 cp_parser_initial_pragma (&token);
615
616 lexer = cp_lexer_alloc ();
617
618 /* Put the first token in the buffer. */
619 lexer->buffer->quick_push (token);
620
621 /* Get the remaining tokens from the preprocessor. */
622 while (token.type != CPP_EOF)
623 {
624 cp_lexer_get_preprocessor_token (lexer, &token);
625 vec_safe_push (lexer->buffer, token);
626 }
627
628 lexer->last_token = lexer->buffer->address ()
629 + lexer->buffer->length ()
630 - 1;
631 lexer->next_token = lexer->buffer->length ()
632 ? lexer->buffer->address ()
633 : &eof_token;
634
635 /* Subsequent preprocessor diagnostics should use compiler
636 diagnostic functions to get the compiler source location. */
637 done_lexing = true;
638
639 gcc_assert (!lexer->next_token->purged_p);
640 return lexer;
641 }
642
643 /* Create a new lexer whose token stream is primed with the tokens in
644 CACHE. When these tokens are exhausted, no new tokens will be read. */
645
646 static cp_lexer *
647 cp_lexer_new_from_tokens (cp_token_cache *cache)
648 {
649 cp_token *first = cache->first;
650 cp_token *last = cache->last;
651 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
652
653 /* We do not own the buffer. */
654 lexer->buffer = NULL;
655 lexer->next_token = first == last ? &eof_token : first;
656 lexer->last_token = last;
657
658 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
659
660 /* Initially we are not debugging. */
661 lexer->debugging_p = false;
662
663 gcc_assert (!lexer->next_token->purged_p);
664 return lexer;
665 }
666
667 /* Frees all resources associated with LEXER. */
668
669 static void
670 cp_lexer_destroy (cp_lexer *lexer)
671 {
672 vec_free (lexer->buffer);
673 lexer->saved_tokens.release ();
674 ggc_free (lexer);
675 }
676
677 /* Returns nonzero if debugging information should be output. */
678
679 static inline bool
680 cp_lexer_debugging_p (cp_lexer *lexer)
681 {
682 return lexer->debugging_p;
683 }
684
685
686 static inline cp_token_position
687 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
688 {
689 gcc_assert (!previous_p || lexer->next_token != &eof_token);
690
691 return lexer->next_token - previous_p;
692 }
693
694 static inline cp_token *
695 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
696 {
697 return pos;
698 }
699
700 static inline void
701 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
702 {
703 lexer->next_token = cp_lexer_token_at (lexer, pos);
704 }
705
706 static inline cp_token_position
707 cp_lexer_previous_token_position (cp_lexer *lexer)
708 {
709 if (lexer->next_token == &eof_token)
710 return lexer->last_token - 1;
711 else
712 return cp_lexer_token_position (lexer, true);
713 }
714
715 static inline cp_token *
716 cp_lexer_previous_token (cp_lexer *lexer)
717 {
718 cp_token_position tp = cp_lexer_previous_token_position (lexer);
719
720 return cp_lexer_token_at (lexer, tp);
721 }
722
723 /* nonzero if we are presently saving tokens. */
724
725 static inline int
726 cp_lexer_saving_tokens (const cp_lexer* lexer)
727 {
728 return lexer->saved_tokens.length () != 0;
729 }
730
731 /* Store the next token from the preprocessor in *TOKEN. Return true
732 if we reach EOF. If LEXER is NULL, assume we are handling an
733 initial #pragma pch_preprocess, and thus want the lexer to return
734 processed strings. */
735
736 static void
737 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
738 {
739 static int is_extern_c = 0;
740
741 /* Get a new token from the preprocessor. */
742 token->type
743 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
744 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
745 token->keyword = RID_MAX;
746 token->pragma_kind = PRAGMA_NONE;
747 token->purged_p = false;
748
749 /* On some systems, some header files are surrounded by an
750 implicit extern "C" block. Set a flag in the token if it
751 comes from such a header. */
752 is_extern_c += pending_lang_change;
753 pending_lang_change = 0;
754 token->implicit_extern_c = is_extern_c > 0;
755
756 /* Check to see if this token is a keyword. */
757 if (token->type == CPP_NAME)
758 {
759 if (C_IS_RESERVED_WORD (token->u.value))
760 {
761 /* Mark this token as a keyword. */
762 token->type = CPP_KEYWORD;
763 /* Record which keyword. */
764 token->keyword = C_RID_CODE (token->u.value);
765 }
766 else
767 {
768 if (warn_cxx0x_compat
769 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
770 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
771 {
772 /* Warn about the C++0x keyword (but still treat it as
773 an identifier). */
774 warning (OPT_Wc__0x_compat,
775 "identifier %qE is a keyword in C++11",
776 token->u.value);
777
778 /* Clear out the C_RID_CODE so we don't warn about this
779 particular identifier-turned-keyword again. */
780 C_SET_RID_CODE (token->u.value, RID_MAX);
781 }
782
783 token->ambiguous_p = false;
784 token->keyword = RID_MAX;
785 }
786 }
787 else if (token->type == CPP_AT_NAME)
788 {
789 /* This only happens in Objective-C++; it must be a keyword. */
790 token->type = CPP_KEYWORD;
791 switch (C_RID_CODE (token->u.value))
792 {
793 /* Replace 'class' with '@class', 'private' with '@private',
794 etc. This prevents confusion with the C++ keyword
795 'class', and makes the tokens consistent with other
796 Objective-C 'AT' keywords. For example '@class' is
797 reported as RID_AT_CLASS which is consistent with
798 '@synchronized', which is reported as
799 RID_AT_SYNCHRONIZED.
800 */
801 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
802 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
803 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
804 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
805 case RID_THROW: token->keyword = RID_AT_THROW; break;
806 case RID_TRY: token->keyword = RID_AT_TRY; break;
807 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
808 default: token->keyword = C_RID_CODE (token->u.value);
809 }
810 }
811 else if (token->type == CPP_PRAGMA)
812 {
813 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
814 token->pragma_kind = ((enum pragma_kind)
815 TREE_INT_CST_LOW (token->u.value));
816 token->u.value = NULL_TREE;
817 }
818 }
819
820 /* Update the globals input_location and the input file stack from TOKEN. */
821 static inline void
822 cp_lexer_set_source_position_from_token (cp_token *token)
823 {
824 if (token->type != CPP_EOF)
825 {
826 input_location = token->location;
827 }
828 }
829
830 /* Return a pointer to the next token in the token stream, but do not
831 consume it. */
832
833 static inline cp_token *
834 cp_lexer_peek_token (cp_lexer *lexer)
835 {
836 if (cp_lexer_debugging_p (lexer))
837 {
838 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
839 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
840 putc ('\n', cp_lexer_debug_stream);
841 }
842 return lexer->next_token;
843 }
844
845 /* Return true if the next token has the indicated TYPE. */
846
847 static inline bool
848 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
849 {
850 return cp_lexer_peek_token (lexer)->type == type;
851 }
852
853 /* Return true if the next token does not have the indicated TYPE. */
854
855 static inline bool
856 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
857 {
858 return !cp_lexer_next_token_is (lexer, type);
859 }
860
861 /* Return true if the next token is the indicated KEYWORD. */
862
863 static inline bool
864 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
865 {
866 return cp_lexer_peek_token (lexer)->keyword == keyword;
867 }
868
869 static inline bool
870 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
871 {
872 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
873 }
874
875 /* Return true if the next token is not the indicated KEYWORD. */
876
877 static inline bool
878 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
879 {
880 return cp_lexer_peek_token (lexer)->keyword != keyword;
881 }
882
883 /* Return true if the next token is a keyword for a decl-specifier. */
884
885 static bool
886 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
887 {
888 cp_token *token;
889
890 token = cp_lexer_peek_token (lexer);
891 switch (token->keyword)
892 {
893 /* auto specifier: storage-class-specifier in C++,
894 simple-type-specifier in C++0x. */
895 case RID_AUTO:
896 /* Storage classes. */
897 case RID_REGISTER:
898 case RID_STATIC:
899 case RID_EXTERN:
900 case RID_MUTABLE:
901 case RID_THREAD:
902 /* Elaborated type specifiers. */
903 case RID_ENUM:
904 case RID_CLASS:
905 case RID_STRUCT:
906 case RID_UNION:
907 case RID_TYPENAME:
908 /* Simple type specifiers. */
909 case RID_CHAR:
910 case RID_CHAR16:
911 case RID_CHAR32:
912 case RID_WCHAR:
913 case RID_BOOL:
914 case RID_SHORT:
915 case RID_INT:
916 case RID_LONG:
917 case RID_INT128:
918 case RID_SIGNED:
919 case RID_UNSIGNED:
920 case RID_FLOAT:
921 case RID_DOUBLE:
922 case RID_VOID:
923 /* GNU extensions. */
924 case RID_ATTRIBUTE:
925 case RID_TYPEOF:
926 /* C++0x extensions. */
927 case RID_DECLTYPE:
928 case RID_UNDERLYING_TYPE:
929 return true;
930
931 default:
932 return false;
933 }
934 }
935
936 /* Returns TRUE iff the token T begins a decltype type. */
937
938 static bool
939 token_is_decltype (cp_token *t)
940 {
941 return (t->keyword == RID_DECLTYPE
942 || t->type == CPP_DECLTYPE);
943 }
944
945 /* Returns TRUE iff the next token begins a decltype type. */
946
947 static bool
948 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
949 {
950 cp_token *t = cp_lexer_peek_token (lexer);
951 return token_is_decltype (t);
952 }
953
954 /* Return a pointer to the Nth token in the token stream. If N is 1,
955 then this is precisely equivalent to cp_lexer_peek_token (except
956 that it is not inline). One would like to disallow that case, but
957 there is one case (cp_parser_nth_token_starts_template_id) where
958 the caller passes a variable for N and it might be 1. */
959
960 static cp_token *
961 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
962 {
963 cp_token *token;
964
965 /* N is 1-based, not zero-based. */
966 gcc_assert (n > 0);
967
968 if (cp_lexer_debugging_p (lexer))
969 fprintf (cp_lexer_debug_stream,
970 "cp_lexer: peeking ahead %ld at token: ", (long)n);
971
972 --n;
973 token = lexer->next_token;
974 gcc_assert (!n || token != &eof_token);
975 while (n != 0)
976 {
977 ++token;
978 if (token == lexer->last_token)
979 {
980 token = &eof_token;
981 break;
982 }
983
984 if (!token->purged_p)
985 --n;
986 }
987
988 if (cp_lexer_debugging_p (lexer))
989 {
990 cp_lexer_print_token (cp_lexer_debug_stream, token);
991 putc ('\n', cp_lexer_debug_stream);
992 }
993
994 return token;
995 }
996
997 /* Return the next token, and advance the lexer's next_token pointer
998 to point to the next non-purged token. */
999
1000 static cp_token *
1001 cp_lexer_consume_token (cp_lexer* lexer)
1002 {
1003 cp_token *token = lexer->next_token;
1004
1005 gcc_assert (token != &eof_token);
1006 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1007
1008 do
1009 {
1010 lexer->next_token++;
1011 if (lexer->next_token == lexer->last_token)
1012 {
1013 lexer->next_token = &eof_token;
1014 break;
1015 }
1016
1017 }
1018 while (lexer->next_token->purged_p);
1019
1020 cp_lexer_set_source_position_from_token (token);
1021
1022 /* Provide debugging output. */
1023 if (cp_lexer_debugging_p (lexer))
1024 {
1025 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1026 cp_lexer_print_token (cp_lexer_debug_stream, token);
1027 putc ('\n', cp_lexer_debug_stream);
1028 }
1029
1030 return token;
1031 }
1032
1033 /* Permanently remove the next token from the token stream, and
1034 advance the next_token pointer to refer to the next non-purged
1035 token. */
1036
1037 static void
1038 cp_lexer_purge_token (cp_lexer *lexer)
1039 {
1040 cp_token *tok = lexer->next_token;
1041
1042 gcc_assert (tok != &eof_token);
1043 tok->purged_p = true;
1044 tok->location = UNKNOWN_LOCATION;
1045 tok->u.value = NULL_TREE;
1046 tok->keyword = RID_MAX;
1047
1048 do
1049 {
1050 tok++;
1051 if (tok == lexer->last_token)
1052 {
1053 tok = &eof_token;
1054 break;
1055 }
1056 }
1057 while (tok->purged_p);
1058 lexer->next_token = tok;
1059 }
1060
1061 /* Permanently remove all tokens after TOK, up to, but not
1062 including, the token that will be returned next by
1063 cp_lexer_peek_token. */
1064
1065 static void
1066 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1067 {
1068 cp_token *peek = lexer->next_token;
1069
1070 if (peek == &eof_token)
1071 peek = lexer->last_token;
1072
1073 gcc_assert (tok < peek);
1074
1075 for ( tok += 1; tok != peek; tok += 1)
1076 {
1077 tok->purged_p = true;
1078 tok->location = UNKNOWN_LOCATION;
1079 tok->u.value = NULL_TREE;
1080 tok->keyword = RID_MAX;
1081 }
1082 }
1083
1084 /* Begin saving tokens. All tokens consumed after this point will be
1085 preserved. */
1086
1087 static void
1088 cp_lexer_save_tokens (cp_lexer* lexer)
1089 {
1090 /* Provide debugging output. */
1091 if (cp_lexer_debugging_p (lexer))
1092 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1093
1094 lexer->saved_tokens.safe_push (lexer->next_token);
1095 }
1096
1097 /* Commit to the portion of the token stream most recently saved. */
1098
1099 static void
1100 cp_lexer_commit_tokens (cp_lexer* lexer)
1101 {
1102 /* Provide debugging output. */
1103 if (cp_lexer_debugging_p (lexer))
1104 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1105
1106 lexer->saved_tokens.pop ();
1107 }
1108
1109 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1110 to the token stream. Stop saving tokens. */
1111
1112 static void
1113 cp_lexer_rollback_tokens (cp_lexer* lexer)
1114 {
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1117 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1118
1119 lexer->next_token = lexer->saved_tokens.pop ();
1120 }
1121
1122 /* Print a representation of the TOKEN on the STREAM. */
1123
1124 static void
1125 cp_lexer_print_token (FILE * stream, cp_token *token)
1126 {
1127 /* We don't use cpp_type2name here because the parser defines
1128 a few tokens of its own. */
1129 static const char *const token_names[] = {
1130 /* cpplib-defined token types */
1131 #define OP(e, s) #e,
1132 #define TK(e, s) #e,
1133 TTYPE_TABLE
1134 #undef OP
1135 #undef TK
1136 /* C++ parser token types - see "Manifest constants", above. */
1137 "KEYWORD",
1138 "TEMPLATE_ID",
1139 "NESTED_NAME_SPECIFIER",
1140 };
1141
1142 /* For some tokens, print the associated data. */
1143 switch (token->type)
1144 {
1145 case CPP_KEYWORD:
1146 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1147 For example, `struct' is mapped to an INTEGER_CST. */
1148 if (!identifier_p (token->u.value))
1149 break;
1150 /* else fall through */
1151 case CPP_NAME:
1152 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1153 break;
1154
1155 case CPP_STRING:
1156 case CPP_STRING16:
1157 case CPP_STRING32:
1158 case CPP_WSTRING:
1159 case CPP_UTF8STRING:
1160 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1161 break;
1162
1163 case CPP_NUMBER:
1164 print_generic_expr (stream, token->u.value, 0);
1165 break;
1166
1167 default:
1168 /* If we have a name for the token, print it out. Otherwise, we
1169 simply give the numeric code. */
1170 if (token->type < ARRAY_SIZE(token_names))
1171 fputs (token_names[token->type], stream);
1172 else
1173 fprintf (stream, "[%d]", token->type);
1174 break;
1175 }
1176 }
1177
1178 DEBUG_FUNCTION void
1179 debug (cp_token &ref)
1180 {
1181 cp_lexer_print_token (stderr, &ref);
1182 fprintf (stderr, "\n");
1183 }
1184
1185 DEBUG_FUNCTION void
1186 debug (cp_token *ptr)
1187 {
1188 if (ptr)
1189 debug (*ptr);
1190 else
1191 fprintf (stderr, "<nil>\n");
1192 }
1193
1194
1195 /* Start emitting debugging information. */
1196
1197 static void
1198 cp_lexer_start_debugging (cp_lexer* lexer)
1199 {
1200 lexer->debugging_p = true;
1201 cp_lexer_debug_stream = stderr;
1202 }
1203
1204 /* Stop emitting debugging information. */
1205
1206 static void
1207 cp_lexer_stop_debugging (cp_lexer* lexer)
1208 {
1209 lexer->debugging_p = false;
1210 cp_lexer_debug_stream = NULL;
1211 }
1212
1213 /* Create a new cp_token_cache, representing a range of tokens. */
1214
1215 static cp_token_cache *
1216 cp_token_cache_new (cp_token *first, cp_token *last)
1217 {
1218 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1219 cache->first = first;
1220 cache->last = last;
1221 return cache;
1222 }
1223
1224 \f
1225 /* Decl-specifiers. */
1226
1227 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1228
1229 static void
1230 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1231 {
1232 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1233 }
1234
1235 /* Declarators. */
1236
1237 /* Nothing other than the parser should be creating declarators;
1238 declarators are a semi-syntactic representation of C++ entities.
1239 Other parts of the front end that need to create entities (like
1240 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1241
1242 static cp_declarator *make_call_declarator
1243 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1244 static cp_declarator *make_array_declarator
1245 (cp_declarator *, tree);
1246 static cp_declarator *make_pointer_declarator
1247 (cp_cv_quals, cp_declarator *, tree);
1248 static cp_declarator *make_reference_declarator
1249 (cp_cv_quals, cp_declarator *, bool, tree);
1250 static cp_parameter_declarator *make_parameter_declarator
1251 (cp_decl_specifier_seq *, cp_declarator *, tree);
1252 static cp_declarator *make_ptrmem_declarator
1253 (cp_cv_quals, tree, cp_declarator *, tree);
1254
1255 /* An erroneous declarator. */
1256 static cp_declarator *cp_error_declarator;
1257
1258 /* The obstack on which declarators and related data structures are
1259 allocated. */
1260 static struct obstack declarator_obstack;
1261
1262 /* Alloc BYTES from the declarator memory pool. */
1263
1264 static inline void *
1265 alloc_declarator (size_t bytes)
1266 {
1267 return obstack_alloc (&declarator_obstack, bytes);
1268 }
1269
1270 /* Allocate a declarator of the indicated KIND. Clear fields that are
1271 common to all declarators. */
1272
1273 static cp_declarator *
1274 make_declarator (cp_declarator_kind kind)
1275 {
1276 cp_declarator *declarator;
1277
1278 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1279 declarator->kind = kind;
1280 declarator->attributes = NULL_TREE;
1281 declarator->std_attributes = NULL_TREE;
1282 declarator->declarator = NULL;
1283 declarator->parameter_pack_p = false;
1284 declarator->id_loc = UNKNOWN_LOCATION;
1285
1286 return declarator;
1287 }
1288
1289 /* Make a declarator for a generalized identifier. If
1290 QUALIFYING_SCOPE is non-NULL, the identifier is
1291 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1292 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1293 is, if any. */
1294
1295 static cp_declarator *
1296 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1297 special_function_kind sfk)
1298 {
1299 cp_declarator *declarator;
1300
1301 /* It is valid to write:
1302
1303 class C { void f(); };
1304 typedef C D;
1305 void D::f();
1306
1307 The standard is not clear about whether `typedef const C D' is
1308 legal; as of 2002-09-15 the committee is considering that
1309 question. EDG 3.0 allows that syntax. Therefore, we do as
1310 well. */
1311 if (qualifying_scope && TYPE_P (qualifying_scope))
1312 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1313
1314 gcc_assert (identifier_p (unqualified_name)
1315 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1316 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1317
1318 declarator = make_declarator (cdk_id);
1319 declarator->u.id.qualifying_scope = qualifying_scope;
1320 declarator->u.id.unqualified_name = unqualified_name;
1321 declarator->u.id.sfk = sfk;
1322
1323 return declarator;
1324 }
1325
1326 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1327 of modifiers such as const or volatile to apply to the pointer
1328 type, represented as identifiers. ATTRIBUTES represent the attributes that
1329 appertain to the pointer or reference. */
1330
1331 cp_declarator *
1332 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1333 tree attributes)
1334 {
1335 cp_declarator *declarator;
1336
1337 declarator = make_declarator (cdk_pointer);
1338 declarator->declarator = target;
1339 declarator->u.pointer.qualifiers = cv_qualifiers;
1340 declarator->u.pointer.class_type = NULL_TREE;
1341 if (target)
1342 {
1343 declarator->id_loc = target->id_loc;
1344 declarator->parameter_pack_p = target->parameter_pack_p;
1345 target->parameter_pack_p = false;
1346 }
1347 else
1348 declarator->parameter_pack_p = false;
1349
1350 declarator->std_attributes = attributes;
1351
1352 return declarator;
1353 }
1354
1355 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1356 represent the attributes that appertain to the pointer or
1357 reference. */
1358
1359 cp_declarator *
1360 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1361 bool rvalue_ref, tree attributes)
1362 {
1363 cp_declarator *declarator;
1364
1365 declarator = make_declarator (cdk_reference);
1366 declarator->declarator = target;
1367 declarator->u.reference.qualifiers = cv_qualifiers;
1368 declarator->u.reference.rvalue_ref = rvalue_ref;
1369 if (target)
1370 {
1371 declarator->id_loc = target->id_loc;
1372 declarator->parameter_pack_p = target->parameter_pack_p;
1373 target->parameter_pack_p = false;
1374 }
1375 else
1376 declarator->parameter_pack_p = false;
1377
1378 declarator->std_attributes = attributes;
1379
1380 return declarator;
1381 }
1382
1383 /* Like make_pointer_declarator -- but for a pointer to a non-static
1384 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1385 appertain to the pointer or reference. */
1386
1387 cp_declarator *
1388 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1389 cp_declarator *pointee,
1390 tree attributes)
1391 {
1392 cp_declarator *declarator;
1393
1394 declarator = make_declarator (cdk_ptrmem);
1395 declarator->declarator = pointee;
1396 declarator->u.pointer.qualifiers = cv_qualifiers;
1397 declarator->u.pointer.class_type = class_type;
1398
1399 if (pointee)
1400 {
1401 declarator->parameter_pack_p = pointee->parameter_pack_p;
1402 pointee->parameter_pack_p = false;
1403 }
1404 else
1405 declarator->parameter_pack_p = false;
1406
1407 declarator->std_attributes = attributes;
1408
1409 return declarator;
1410 }
1411
1412 /* Make a declarator for the function given by TARGET, with the
1413 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1414 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1415 indicates what exceptions can be thrown. */
1416
1417 cp_declarator *
1418 make_call_declarator (cp_declarator *target,
1419 tree parms,
1420 cp_cv_quals cv_qualifiers,
1421 cp_virt_specifiers virt_specifiers,
1422 cp_ref_qualifier ref_qualifier,
1423 tree exception_specification,
1424 tree late_return_type)
1425 {
1426 cp_declarator *declarator;
1427
1428 declarator = make_declarator (cdk_function);
1429 declarator->declarator = target;
1430 declarator->u.function.parameters = parms;
1431 declarator->u.function.qualifiers = cv_qualifiers;
1432 declarator->u.function.virt_specifiers = virt_specifiers;
1433 declarator->u.function.ref_qualifier = ref_qualifier;
1434 declarator->u.function.exception_specification = exception_specification;
1435 declarator->u.function.late_return_type = late_return_type;
1436 if (target)
1437 {
1438 declarator->id_loc = target->id_loc;
1439 declarator->parameter_pack_p = target->parameter_pack_p;
1440 target->parameter_pack_p = false;
1441 }
1442 else
1443 declarator->parameter_pack_p = false;
1444
1445 return declarator;
1446 }
1447
1448 /* Make a declarator for an array of BOUNDS elements, each of which is
1449 defined by ELEMENT. */
1450
1451 cp_declarator *
1452 make_array_declarator (cp_declarator *element, tree bounds)
1453 {
1454 cp_declarator *declarator;
1455
1456 declarator = make_declarator (cdk_array);
1457 declarator->declarator = element;
1458 declarator->u.array.bounds = bounds;
1459 if (element)
1460 {
1461 declarator->id_loc = element->id_loc;
1462 declarator->parameter_pack_p = element->parameter_pack_p;
1463 element->parameter_pack_p = false;
1464 }
1465 else
1466 declarator->parameter_pack_p = false;
1467
1468 return declarator;
1469 }
1470
1471 /* Determine whether the declarator we've seen so far can be a
1472 parameter pack, when followed by an ellipsis. */
1473 static bool
1474 declarator_can_be_parameter_pack (cp_declarator *declarator)
1475 {
1476 /* Search for a declarator name, or any other declarator that goes
1477 after the point where the ellipsis could appear in a parameter
1478 pack. If we find any of these, then this declarator can not be
1479 made into a parameter pack. */
1480 bool found = false;
1481 while (declarator && !found)
1482 {
1483 switch ((int)declarator->kind)
1484 {
1485 case cdk_id:
1486 case cdk_array:
1487 found = true;
1488 break;
1489
1490 case cdk_error:
1491 return true;
1492
1493 default:
1494 declarator = declarator->declarator;
1495 break;
1496 }
1497 }
1498
1499 return !found;
1500 }
1501
1502 cp_parameter_declarator *no_parameters;
1503
1504 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1505 DECLARATOR and DEFAULT_ARGUMENT. */
1506
1507 cp_parameter_declarator *
1508 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1509 cp_declarator *declarator,
1510 tree default_argument)
1511 {
1512 cp_parameter_declarator *parameter;
1513
1514 parameter = ((cp_parameter_declarator *)
1515 alloc_declarator (sizeof (cp_parameter_declarator)));
1516 parameter->next = NULL;
1517 if (decl_specifiers)
1518 parameter->decl_specifiers = *decl_specifiers;
1519 else
1520 clear_decl_specs (&parameter->decl_specifiers);
1521 parameter->declarator = declarator;
1522 parameter->default_argument = default_argument;
1523 parameter->ellipsis_p = false;
1524
1525 return parameter;
1526 }
1527
1528 /* Returns true iff DECLARATOR is a declaration for a function. */
1529
1530 static bool
1531 function_declarator_p (const cp_declarator *declarator)
1532 {
1533 while (declarator)
1534 {
1535 if (declarator->kind == cdk_function
1536 && declarator->declarator->kind == cdk_id)
1537 return true;
1538 if (declarator->kind == cdk_id
1539 || declarator->kind == cdk_error)
1540 return false;
1541 declarator = declarator->declarator;
1542 }
1543 return false;
1544 }
1545
1546 /* The parser. */
1547
1548 /* Overview
1549 --------
1550
1551 A cp_parser parses the token stream as specified by the C++
1552 grammar. Its job is purely parsing, not semantic analysis. For
1553 example, the parser breaks the token stream into declarators,
1554 expressions, statements, and other similar syntactic constructs.
1555 It does not check that the types of the expressions on either side
1556 of an assignment-statement are compatible, or that a function is
1557 not declared with a parameter of type `void'.
1558
1559 The parser invokes routines elsewhere in the compiler to perform
1560 semantic analysis and to build up the abstract syntax tree for the
1561 code processed.
1562
1563 The parser (and the template instantiation code, which is, in a
1564 way, a close relative of parsing) are the only parts of the
1565 compiler that should be calling push_scope and pop_scope, or
1566 related functions. The parser (and template instantiation code)
1567 keeps track of what scope is presently active; everything else
1568 should simply honor that. (The code that generates static
1569 initializers may also need to set the scope, in order to check
1570 access control correctly when emitting the initializers.)
1571
1572 Methodology
1573 -----------
1574
1575 The parser is of the standard recursive-descent variety. Upcoming
1576 tokens in the token stream are examined in order to determine which
1577 production to use when parsing a non-terminal. Some C++ constructs
1578 require arbitrary look ahead to disambiguate. For example, it is
1579 impossible, in the general case, to tell whether a statement is an
1580 expression or declaration without scanning the entire statement.
1581 Therefore, the parser is capable of "parsing tentatively." When the
1582 parser is not sure what construct comes next, it enters this mode.
1583 Then, while we attempt to parse the construct, the parser queues up
1584 error messages, rather than issuing them immediately, and saves the
1585 tokens it consumes. If the construct is parsed successfully, the
1586 parser "commits", i.e., it issues any queued error messages and
1587 the tokens that were being preserved are permanently discarded.
1588 If, however, the construct is not parsed successfully, the parser
1589 rolls back its state completely so that it can resume parsing using
1590 a different alternative.
1591
1592 Future Improvements
1593 -------------------
1594
1595 The performance of the parser could probably be improved substantially.
1596 We could often eliminate the need to parse tentatively by looking ahead
1597 a little bit. In some places, this approach might not entirely eliminate
1598 the need to parse tentatively, but it might still speed up the average
1599 case. */
1600
1601 /* Flags that are passed to some parsing functions. These values can
1602 be bitwise-ored together. */
1603
1604 enum
1605 {
1606 /* No flags. */
1607 CP_PARSER_FLAGS_NONE = 0x0,
1608 /* The construct is optional. If it is not present, then no error
1609 should be issued. */
1610 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1611 /* When parsing a type-specifier, treat user-defined type-names
1612 as non-type identifiers. */
1613 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1614 /* When parsing a type-specifier, do not try to parse a class-specifier
1615 or enum-specifier. */
1616 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1617 /* When parsing a decl-specifier-seq, only allow type-specifier or
1618 constexpr. */
1619 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1620 };
1621
1622 /* This type is used for parameters and variables which hold
1623 combinations of the above flags. */
1624 typedef int cp_parser_flags;
1625
1626 /* The different kinds of declarators we want to parse. */
1627
1628 typedef enum cp_parser_declarator_kind
1629 {
1630 /* We want an abstract declarator. */
1631 CP_PARSER_DECLARATOR_ABSTRACT,
1632 /* We want a named declarator. */
1633 CP_PARSER_DECLARATOR_NAMED,
1634 /* We don't mind, but the name must be an unqualified-id. */
1635 CP_PARSER_DECLARATOR_EITHER
1636 } cp_parser_declarator_kind;
1637
1638 /* The precedence values used to parse binary expressions. The minimum value
1639 of PREC must be 1, because zero is reserved to quickly discriminate
1640 binary operators from other tokens. */
1641
1642 enum cp_parser_prec
1643 {
1644 PREC_NOT_OPERATOR,
1645 PREC_LOGICAL_OR_EXPRESSION,
1646 PREC_LOGICAL_AND_EXPRESSION,
1647 PREC_INCLUSIVE_OR_EXPRESSION,
1648 PREC_EXCLUSIVE_OR_EXPRESSION,
1649 PREC_AND_EXPRESSION,
1650 PREC_EQUALITY_EXPRESSION,
1651 PREC_RELATIONAL_EXPRESSION,
1652 PREC_SHIFT_EXPRESSION,
1653 PREC_ADDITIVE_EXPRESSION,
1654 PREC_MULTIPLICATIVE_EXPRESSION,
1655 PREC_PM_EXPRESSION,
1656 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1657 };
1658
1659 /* A mapping from a token type to a corresponding tree node type, with a
1660 precedence value. */
1661
1662 typedef struct cp_parser_binary_operations_map_node
1663 {
1664 /* The token type. */
1665 enum cpp_ttype token_type;
1666 /* The corresponding tree code. */
1667 enum tree_code tree_type;
1668 /* The precedence of this operator. */
1669 enum cp_parser_prec prec;
1670 } cp_parser_binary_operations_map_node;
1671
1672 typedef struct cp_parser_expression_stack_entry
1673 {
1674 /* Left hand side of the binary operation we are currently
1675 parsing. */
1676 tree lhs;
1677 /* Original tree code for left hand side, if it was a binary
1678 expression itself (used for -Wparentheses). */
1679 enum tree_code lhs_type;
1680 /* Tree code for the binary operation we are parsing. */
1681 enum tree_code tree_type;
1682 /* Precedence of the binary operation we are parsing. */
1683 enum cp_parser_prec prec;
1684 /* Location of the binary operation we are parsing. */
1685 location_t loc;
1686 } cp_parser_expression_stack_entry;
1687
1688 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1689 entries because precedence levels on the stack are monotonically
1690 increasing. */
1691 typedef struct cp_parser_expression_stack_entry
1692 cp_parser_expression_stack[NUM_PREC_VALUES];
1693
1694 /* Prototypes. */
1695
1696 /* Constructors and destructors. */
1697
1698 static cp_parser_context *cp_parser_context_new
1699 (cp_parser_context *);
1700
1701 /* Class variables. */
1702
1703 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1704
1705 /* The operator-precedence table used by cp_parser_binary_expression.
1706 Transformed into an associative array (binops_by_token) by
1707 cp_parser_new. */
1708
1709 static const cp_parser_binary_operations_map_node binops[] = {
1710 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1711 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1712
1713 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1714 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1715 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1716
1717 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1718 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1719
1720 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1721 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1722
1723 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1724 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1725 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1726 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1727
1728 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1729 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1730
1731 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1732
1733 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1734
1735 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1736
1737 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1738
1739 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1740 };
1741
1742 /* The same as binops, but initialized by cp_parser_new so that
1743 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1744 for speed. */
1745 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1746
1747 /* Constructors and destructors. */
1748
1749 /* Construct a new context. The context below this one on the stack
1750 is given by NEXT. */
1751
1752 static cp_parser_context *
1753 cp_parser_context_new (cp_parser_context* next)
1754 {
1755 cp_parser_context *context;
1756
1757 /* Allocate the storage. */
1758 if (cp_parser_context_free_list != NULL)
1759 {
1760 /* Pull the first entry from the free list. */
1761 context = cp_parser_context_free_list;
1762 cp_parser_context_free_list = context->next;
1763 memset (context, 0, sizeof (*context));
1764 }
1765 else
1766 context = ggc_alloc_cleared_cp_parser_context ();
1767
1768 /* No errors have occurred yet in this context. */
1769 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1770 /* If this is not the bottommost context, copy information that we
1771 need from the previous context. */
1772 if (next)
1773 {
1774 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1775 expression, then we are parsing one in this context, too. */
1776 context->object_type = next->object_type;
1777 /* Thread the stack. */
1778 context->next = next;
1779 }
1780
1781 return context;
1782 }
1783
1784 /* Managing the unparsed function queues. */
1785
1786 #define unparsed_funs_with_default_args \
1787 parser->unparsed_queues->last ().funs_with_default_args
1788 #define unparsed_funs_with_definitions \
1789 parser->unparsed_queues->last ().funs_with_definitions
1790 #define unparsed_nsdmis \
1791 parser->unparsed_queues->last ().nsdmis
1792
1793 static void
1794 push_unparsed_function_queues (cp_parser *parser)
1795 {
1796 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1797 vec_safe_push (parser->unparsed_queues, e);
1798 }
1799
1800 static void
1801 pop_unparsed_function_queues (cp_parser *parser)
1802 {
1803 release_tree_vector (unparsed_funs_with_definitions);
1804 parser->unparsed_queues->pop ();
1805 }
1806
1807 /* Prototypes. */
1808
1809 /* Constructors and destructors. */
1810
1811 static cp_parser *cp_parser_new
1812 (void);
1813
1814 /* Routines to parse various constructs.
1815
1816 Those that return `tree' will return the error_mark_node (rather
1817 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1818 Sometimes, they will return an ordinary node if error-recovery was
1819 attempted, even though a parse error occurred. So, to check
1820 whether or not a parse error occurred, you should always use
1821 cp_parser_error_occurred. If the construct is optional (indicated
1822 either by an `_opt' in the name of the function that does the
1823 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1824 the construct is not present. */
1825
1826 /* Lexical conventions [gram.lex] */
1827
1828 static tree cp_parser_identifier
1829 (cp_parser *);
1830 static tree cp_parser_string_literal
1831 (cp_parser *, bool, bool);
1832 static tree cp_parser_userdef_char_literal
1833 (cp_parser *);
1834 static tree cp_parser_userdef_string_literal
1835 (cp_token *);
1836 static tree cp_parser_userdef_numeric_literal
1837 (cp_parser *);
1838
1839 /* Basic concepts [gram.basic] */
1840
1841 static bool cp_parser_translation_unit
1842 (cp_parser *);
1843
1844 /* Expressions [gram.expr] */
1845
1846 static tree cp_parser_primary_expression
1847 (cp_parser *, bool, bool, bool, cp_id_kind *);
1848 static tree cp_parser_id_expression
1849 (cp_parser *, bool, bool, bool *, bool, bool);
1850 static tree cp_parser_unqualified_id
1851 (cp_parser *, bool, bool, bool, bool);
1852 static tree cp_parser_nested_name_specifier_opt
1853 (cp_parser *, bool, bool, bool, bool);
1854 static tree cp_parser_nested_name_specifier
1855 (cp_parser *, bool, bool, bool, bool);
1856 static tree cp_parser_qualifying_entity
1857 (cp_parser *, bool, bool, bool, bool, bool);
1858 static tree cp_parser_postfix_expression
1859 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1860 static tree cp_parser_postfix_open_square_expression
1861 (cp_parser *, tree, bool, bool);
1862 static tree cp_parser_postfix_dot_deref_expression
1863 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1864 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1865 (cp_parser *, int, bool, bool, bool *);
1866 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1867 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1868 static void cp_parser_pseudo_destructor_name
1869 (cp_parser *, tree, tree *, tree *);
1870 static tree cp_parser_unary_expression
1871 (cp_parser *, bool, bool, cp_id_kind *);
1872 static enum tree_code cp_parser_unary_operator
1873 (cp_token *);
1874 static tree cp_parser_new_expression
1875 (cp_parser *);
1876 static vec<tree, va_gc> *cp_parser_new_placement
1877 (cp_parser *);
1878 static tree cp_parser_new_type_id
1879 (cp_parser *, tree *);
1880 static cp_declarator *cp_parser_new_declarator_opt
1881 (cp_parser *);
1882 static cp_declarator *cp_parser_direct_new_declarator
1883 (cp_parser *);
1884 static vec<tree, va_gc> *cp_parser_new_initializer
1885 (cp_parser *);
1886 static tree cp_parser_delete_expression
1887 (cp_parser *);
1888 static tree cp_parser_cast_expression
1889 (cp_parser *, bool, bool, bool, cp_id_kind *);
1890 static tree cp_parser_binary_expression
1891 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1892 static tree cp_parser_question_colon_clause
1893 (cp_parser *, tree);
1894 static tree cp_parser_assignment_expression
1895 (cp_parser *, bool, cp_id_kind *);
1896 static enum tree_code cp_parser_assignment_operator_opt
1897 (cp_parser *);
1898 static tree cp_parser_expression
1899 (cp_parser *, bool, cp_id_kind *);
1900 static tree cp_parser_expression
1901 (cp_parser *, bool, bool, cp_id_kind *);
1902 static tree cp_parser_constant_expression
1903 (cp_parser *, bool, bool *);
1904 static tree cp_parser_builtin_offsetof
1905 (cp_parser *);
1906 static tree cp_parser_lambda_expression
1907 (cp_parser *);
1908 static void cp_parser_lambda_introducer
1909 (cp_parser *, tree);
1910 static bool cp_parser_lambda_declarator_opt
1911 (cp_parser *, tree);
1912 static void cp_parser_lambda_body
1913 (cp_parser *, tree);
1914
1915 /* Statements [gram.stmt.stmt] */
1916
1917 static void cp_parser_statement
1918 (cp_parser *, tree, bool, bool *);
1919 static void cp_parser_label_for_labeled_statement
1920 (cp_parser *, tree);
1921 static tree cp_parser_expression_statement
1922 (cp_parser *, tree);
1923 static tree cp_parser_compound_statement
1924 (cp_parser *, tree, bool, bool);
1925 static void cp_parser_statement_seq_opt
1926 (cp_parser *, tree);
1927 static tree cp_parser_selection_statement
1928 (cp_parser *, bool *);
1929 static tree cp_parser_condition
1930 (cp_parser *);
1931 static tree cp_parser_iteration_statement
1932 (cp_parser *);
1933 static bool cp_parser_for_init_statement
1934 (cp_parser *, tree *decl);
1935 static tree cp_parser_for
1936 (cp_parser *);
1937 static tree cp_parser_c_for
1938 (cp_parser *, tree, tree);
1939 static tree cp_parser_range_for
1940 (cp_parser *, tree, tree, tree);
1941 static void do_range_for_auto_deduction
1942 (tree, tree);
1943 static tree cp_parser_perform_range_for_lookup
1944 (tree, tree *, tree *);
1945 static tree cp_parser_range_for_member_function
1946 (tree, tree);
1947 static tree cp_parser_jump_statement
1948 (cp_parser *);
1949 static void cp_parser_declaration_statement
1950 (cp_parser *);
1951
1952 static tree cp_parser_implicitly_scoped_statement
1953 (cp_parser *, bool *);
1954 static void cp_parser_already_scoped_statement
1955 (cp_parser *);
1956
1957 /* Declarations [gram.dcl.dcl] */
1958
1959 static void cp_parser_declaration_seq_opt
1960 (cp_parser *);
1961 static void cp_parser_declaration
1962 (cp_parser *);
1963 static void cp_parser_block_declaration
1964 (cp_parser *, bool);
1965 static void cp_parser_simple_declaration
1966 (cp_parser *, bool, tree *);
1967 static void cp_parser_decl_specifier_seq
1968 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1969 static tree cp_parser_storage_class_specifier_opt
1970 (cp_parser *);
1971 static tree cp_parser_function_specifier_opt
1972 (cp_parser *, cp_decl_specifier_seq *);
1973 static tree cp_parser_type_specifier
1974 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1975 int *, bool *);
1976 static tree cp_parser_simple_type_specifier
1977 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1978 static tree cp_parser_type_name
1979 (cp_parser *);
1980 static tree cp_parser_nonclass_name
1981 (cp_parser* parser);
1982 static tree cp_parser_elaborated_type_specifier
1983 (cp_parser *, bool, bool);
1984 static tree cp_parser_enum_specifier
1985 (cp_parser *);
1986 static void cp_parser_enumerator_list
1987 (cp_parser *, tree);
1988 static void cp_parser_enumerator_definition
1989 (cp_parser *, tree);
1990 static tree cp_parser_namespace_name
1991 (cp_parser *);
1992 static void cp_parser_namespace_definition
1993 (cp_parser *);
1994 static void cp_parser_namespace_body
1995 (cp_parser *);
1996 static tree cp_parser_qualified_namespace_specifier
1997 (cp_parser *);
1998 static void cp_parser_namespace_alias_definition
1999 (cp_parser *);
2000 static bool cp_parser_using_declaration
2001 (cp_parser *, bool);
2002 static void cp_parser_using_directive
2003 (cp_parser *);
2004 static tree cp_parser_alias_declaration
2005 (cp_parser *);
2006 static void cp_parser_asm_definition
2007 (cp_parser *);
2008 static void cp_parser_linkage_specification
2009 (cp_parser *);
2010 static void cp_parser_static_assert
2011 (cp_parser *, bool);
2012 static tree cp_parser_decltype
2013 (cp_parser *);
2014
2015 /* Declarators [gram.dcl.decl] */
2016
2017 static tree cp_parser_init_declarator
2018 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2019 static cp_declarator *cp_parser_declarator
2020 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2021 static cp_declarator *cp_parser_direct_declarator
2022 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2023 static enum tree_code cp_parser_ptr_operator
2024 (cp_parser *, tree *, cp_cv_quals *, tree *);
2025 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2026 (cp_parser *);
2027 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2028 (cp_parser *);
2029 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2030 (cp_parser *);
2031 static tree cp_parser_late_return_type_opt
2032 (cp_parser *, cp_cv_quals);
2033 static tree cp_parser_declarator_id
2034 (cp_parser *, bool);
2035 static tree cp_parser_type_id
2036 (cp_parser *);
2037 static tree cp_parser_template_type_arg
2038 (cp_parser *);
2039 static tree cp_parser_trailing_type_id (cp_parser *);
2040 static tree cp_parser_type_id_1
2041 (cp_parser *, bool, bool);
2042 static void cp_parser_type_specifier_seq
2043 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2044 static tree cp_parser_parameter_declaration_clause
2045 (cp_parser *);
2046 static tree cp_parser_parameter_declaration_list
2047 (cp_parser *, bool *);
2048 static cp_parameter_declarator *cp_parser_parameter_declaration
2049 (cp_parser *, bool, bool *);
2050 static tree cp_parser_default_argument
2051 (cp_parser *, bool);
2052 static void cp_parser_function_body
2053 (cp_parser *, bool);
2054 static tree cp_parser_initializer
2055 (cp_parser *, bool *, bool *);
2056 static tree cp_parser_initializer_clause
2057 (cp_parser *, bool *);
2058 static tree cp_parser_braced_list
2059 (cp_parser*, bool*);
2060 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2061 (cp_parser *, bool *);
2062
2063 static bool cp_parser_ctor_initializer_opt_and_function_body
2064 (cp_parser *, bool);
2065
2066 /* Classes [gram.class] */
2067
2068 static tree cp_parser_class_name
2069 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2070 static tree cp_parser_class_specifier
2071 (cp_parser *);
2072 static tree cp_parser_class_head
2073 (cp_parser *, bool *);
2074 static enum tag_types cp_parser_class_key
2075 (cp_parser *);
2076 static void cp_parser_member_specification_opt
2077 (cp_parser *);
2078 static void cp_parser_member_declaration
2079 (cp_parser *);
2080 static tree cp_parser_pure_specifier
2081 (cp_parser *);
2082 static tree cp_parser_constant_initializer
2083 (cp_parser *);
2084
2085 /* Derived classes [gram.class.derived] */
2086
2087 static tree cp_parser_base_clause
2088 (cp_parser *);
2089 static tree cp_parser_base_specifier
2090 (cp_parser *);
2091
2092 /* Special member functions [gram.special] */
2093
2094 static tree cp_parser_conversion_function_id
2095 (cp_parser *);
2096 static tree cp_parser_conversion_type_id
2097 (cp_parser *);
2098 static cp_declarator *cp_parser_conversion_declarator_opt
2099 (cp_parser *);
2100 static bool cp_parser_ctor_initializer_opt
2101 (cp_parser *);
2102 static void cp_parser_mem_initializer_list
2103 (cp_parser *);
2104 static tree cp_parser_mem_initializer
2105 (cp_parser *);
2106 static tree cp_parser_mem_initializer_id
2107 (cp_parser *);
2108
2109 /* Overloading [gram.over] */
2110
2111 static tree cp_parser_operator_function_id
2112 (cp_parser *);
2113 static tree cp_parser_operator
2114 (cp_parser *);
2115
2116 /* Templates [gram.temp] */
2117
2118 static void cp_parser_template_declaration
2119 (cp_parser *, bool);
2120 static tree cp_parser_template_parameter_list
2121 (cp_parser *);
2122 static tree cp_parser_template_parameter
2123 (cp_parser *, bool *, bool *);
2124 static tree cp_parser_type_parameter
2125 (cp_parser *, bool *);
2126 static tree cp_parser_template_id
2127 (cp_parser *, bool, bool, enum tag_types, bool);
2128 static tree cp_parser_template_name
2129 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2130 static tree cp_parser_template_argument_list
2131 (cp_parser *);
2132 static tree cp_parser_template_argument
2133 (cp_parser *);
2134 static void cp_parser_explicit_instantiation
2135 (cp_parser *);
2136 static void cp_parser_explicit_specialization
2137 (cp_parser *);
2138
2139 /* Exception handling [gram.exception] */
2140
2141 static tree cp_parser_try_block
2142 (cp_parser *);
2143 static bool cp_parser_function_try_block
2144 (cp_parser *);
2145 static void cp_parser_handler_seq
2146 (cp_parser *);
2147 static void cp_parser_handler
2148 (cp_parser *);
2149 static tree cp_parser_exception_declaration
2150 (cp_parser *);
2151 static tree cp_parser_throw_expression
2152 (cp_parser *);
2153 static tree cp_parser_exception_specification_opt
2154 (cp_parser *);
2155 static tree cp_parser_type_id_list
2156 (cp_parser *);
2157
2158 /* GNU Extensions */
2159
2160 static tree cp_parser_asm_specification_opt
2161 (cp_parser *);
2162 static tree cp_parser_asm_operand_list
2163 (cp_parser *);
2164 static tree cp_parser_asm_clobber_list
2165 (cp_parser *);
2166 static tree cp_parser_asm_label_list
2167 (cp_parser *);
2168 static bool cp_next_tokens_can_be_attribute_p
2169 (cp_parser *);
2170 static bool cp_next_tokens_can_be_gnu_attribute_p
2171 (cp_parser *);
2172 static bool cp_next_tokens_can_be_std_attribute_p
2173 (cp_parser *);
2174 static bool cp_nth_tokens_can_be_std_attribute_p
2175 (cp_parser *, size_t);
2176 static bool cp_nth_tokens_can_be_gnu_attribute_p
2177 (cp_parser *, size_t);
2178 static bool cp_nth_tokens_can_be_attribute_p
2179 (cp_parser *, size_t);
2180 static tree cp_parser_attributes_opt
2181 (cp_parser *);
2182 static tree cp_parser_gnu_attributes_opt
2183 (cp_parser *);
2184 static tree cp_parser_gnu_attribute_list
2185 (cp_parser *);
2186 static tree cp_parser_std_attribute
2187 (cp_parser *);
2188 static tree cp_parser_std_attribute_spec
2189 (cp_parser *);
2190 static tree cp_parser_std_attribute_spec_seq
2191 (cp_parser *);
2192 static bool cp_parser_extension_opt
2193 (cp_parser *, int *);
2194 static void cp_parser_label_declaration
2195 (cp_parser *);
2196
2197 /* Transactional Memory Extensions */
2198
2199 static tree cp_parser_transaction
2200 (cp_parser *, enum rid);
2201 static tree cp_parser_transaction_expression
2202 (cp_parser *, enum rid);
2203 static bool cp_parser_function_transaction
2204 (cp_parser *, enum rid);
2205 static tree cp_parser_transaction_cancel
2206 (cp_parser *);
2207
2208 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2209 static bool cp_parser_pragma
2210 (cp_parser *, enum pragma_context);
2211
2212 /* Objective-C++ Productions */
2213
2214 static tree cp_parser_objc_message_receiver
2215 (cp_parser *);
2216 static tree cp_parser_objc_message_args
2217 (cp_parser *);
2218 static tree cp_parser_objc_message_expression
2219 (cp_parser *);
2220 static tree cp_parser_objc_encode_expression
2221 (cp_parser *);
2222 static tree cp_parser_objc_defs_expression
2223 (cp_parser *);
2224 static tree cp_parser_objc_protocol_expression
2225 (cp_parser *);
2226 static tree cp_parser_objc_selector_expression
2227 (cp_parser *);
2228 static tree cp_parser_objc_expression
2229 (cp_parser *);
2230 static bool cp_parser_objc_selector_p
2231 (enum cpp_ttype);
2232 static tree cp_parser_objc_selector
2233 (cp_parser *);
2234 static tree cp_parser_objc_protocol_refs_opt
2235 (cp_parser *);
2236 static void cp_parser_objc_declaration
2237 (cp_parser *, tree);
2238 static tree cp_parser_objc_statement
2239 (cp_parser *);
2240 static bool cp_parser_objc_valid_prefix_attributes
2241 (cp_parser *, tree *);
2242 static void cp_parser_objc_at_property_declaration
2243 (cp_parser *) ;
2244 static void cp_parser_objc_at_synthesize_declaration
2245 (cp_parser *) ;
2246 static void cp_parser_objc_at_dynamic_declaration
2247 (cp_parser *) ;
2248 static tree cp_parser_objc_struct_declaration
2249 (cp_parser *) ;
2250
2251 /* Utility Routines */
2252
2253 static tree cp_parser_lookup_name
2254 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2255 static tree cp_parser_lookup_name_simple
2256 (cp_parser *, tree, location_t);
2257 static tree cp_parser_maybe_treat_template_as_class
2258 (tree, bool);
2259 static bool cp_parser_check_declarator_template_parameters
2260 (cp_parser *, cp_declarator *, location_t);
2261 static bool cp_parser_check_template_parameters
2262 (cp_parser *, unsigned, location_t, cp_declarator *);
2263 static tree cp_parser_simple_cast_expression
2264 (cp_parser *);
2265 static tree cp_parser_global_scope_opt
2266 (cp_parser *, bool);
2267 static bool cp_parser_constructor_declarator_p
2268 (cp_parser *, bool);
2269 static tree cp_parser_function_definition_from_specifiers_and_declarator
2270 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2271 static tree cp_parser_function_definition_after_declarator
2272 (cp_parser *, bool);
2273 static void cp_parser_template_declaration_after_export
2274 (cp_parser *, bool);
2275 static void cp_parser_perform_template_parameter_access_checks
2276 (vec<deferred_access_check, va_gc> *);
2277 static tree cp_parser_single_declaration
2278 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2279 static tree cp_parser_functional_cast
2280 (cp_parser *, tree);
2281 static tree cp_parser_save_member_function_body
2282 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2283 static tree cp_parser_save_nsdmi
2284 (cp_parser *);
2285 static tree cp_parser_enclosed_template_argument_list
2286 (cp_parser *);
2287 static void cp_parser_save_default_args
2288 (cp_parser *, tree);
2289 static void cp_parser_late_parsing_for_member
2290 (cp_parser *, tree);
2291 static tree cp_parser_late_parse_one_default_arg
2292 (cp_parser *, tree, tree, tree);
2293 static void cp_parser_late_parsing_nsdmi
2294 (cp_parser *, tree);
2295 static void cp_parser_late_parsing_default_args
2296 (cp_parser *, tree);
2297 static tree cp_parser_sizeof_operand
2298 (cp_parser *, enum rid);
2299 static tree cp_parser_trait_expr
2300 (cp_parser *, enum rid);
2301 static bool cp_parser_declares_only_class_p
2302 (cp_parser *);
2303 static void cp_parser_set_storage_class
2304 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2305 static void cp_parser_set_decl_spec_type
2306 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2307 static void set_and_check_decl_spec_loc
2308 (cp_decl_specifier_seq *decl_specs,
2309 cp_decl_spec ds, cp_token *);
2310 static bool cp_parser_friend_p
2311 (const cp_decl_specifier_seq *);
2312 static void cp_parser_required_error
2313 (cp_parser *, required_token, bool);
2314 static cp_token *cp_parser_require
2315 (cp_parser *, enum cpp_ttype, required_token);
2316 static cp_token *cp_parser_require_keyword
2317 (cp_parser *, enum rid, required_token);
2318 static bool cp_parser_token_starts_function_definition_p
2319 (cp_token *);
2320 static bool cp_parser_next_token_starts_class_definition_p
2321 (cp_parser *);
2322 static bool cp_parser_next_token_ends_template_argument_p
2323 (cp_parser *);
2324 static bool cp_parser_nth_token_starts_template_argument_list_p
2325 (cp_parser *, size_t);
2326 static enum tag_types cp_parser_token_is_class_key
2327 (cp_token *);
2328 static void cp_parser_check_class_key
2329 (enum tag_types, tree type);
2330 static void cp_parser_check_access_in_redeclaration
2331 (tree type, location_t location);
2332 static bool cp_parser_optional_template_keyword
2333 (cp_parser *);
2334 static void cp_parser_pre_parsed_nested_name_specifier
2335 (cp_parser *);
2336 static bool cp_parser_cache_group
2337 (cp_parser *, enum cpp_ttype, unsigned);
2338 static tree cp_parser_cache_defarg
2339 (cp_parser *parser, bool nsdmi);
2340 static void cp_parser_parse_tentatively
2341 (cp_parser *);
2342 static void cp_parser_commit_to_tentative_parse
2343 (cp_parser *);
2344 static void cp_parser_abort_tentative_parse
2345 (cp_parser *);
2346 static bool cp_parser_parse_definitely
2347 (cp_parser *);
2348 static inline bool cp_parser_parsing_tentatively
2349 (cp_parser *);
2350 static bool cp_parser_uncommitted_to_tentative_parse_p
2351 (cp_parser *);
2352 static void cp_parser_error
2353 (cp_parser *, const char *);
2354 static void cp_parser_name_lookup_error
2355 (cp_parser *, tree, tree, name_lookup_error, location_t);
2356 static bool cp_parser_simulate_error
2357 (cp_parser *);
2358 static bool cp_parser_check_type_definition
2359 (cp_parser *);
2360 static void cp_parser_check_for_definition_in_return_type
2361 (cp_declarator *, tree, location_t type_location);
2362 static void cp_parser_check_for_invalid_template_id
2363 (cp_parser *, tree, enum tag_types, location_t location);
2364 static bool cp_parser_non_integral_constant_expression
2365 (cp_parser *, non_integral_constant);
2366 static void cp_parser_diagnose_invalid_type_name
2367 (cp_parser *, tree, tree, location_t);
2368 static bool cp_parser_parse_and_diagnose_invalid_type_name
2369 (cp_parser *);
2370 static int cp_parser_skip_to_closing_parenthesis
2371 (cp_parser *, bool, bool, bool);
2372 static void cp_parser_skip_to_end_of_statement
2373 (cp_parser *);
2374 static void cp_parser_consume_semicolon_at_end_of_statement
2375 (cp_parser *);
2376 static void cp_parser_skip_to_end_of_block_or_statement
2377 (cp_parser *);
2378 static bool cp_parser_skip_to_closing_brace
2379 (cp_parser *);
2380 static void cp_parser_skip_to_end_of_template_parameter_list
2381 (cp_parser *);
2382 static void cp_parser_skip_to_pragma_eol
2383 (cp_parser*, cp_token *);
2384 static bool cp_parser_error_occurred
2385 (cp_parser *);
2386 static bool cp_parser_allow_gnu_extensions_p
2387 (cp_parser *);
2388 static bool cp_parser_is_pure_string_literal
2389 (cp_token *);
2390 static bool cp_parser_is_string_literal
2391 (cp_token *);
2392 static bool cp_parser_is_keyword
2393 (cp_token *, enum rid);
2394 static tree cp_parser_make_typename_type
2395 (cp_parser *, tree, tree, location_t location);
2396 static cp_declarator * cp_parser_make_indirect_declarator
2397 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2398
2399 /* Returns nonzero if we are parsing tentatively. */
2400
2401 static inline bool
2402 cp_parser_parsing_tentatively (cp_parser* parser)
2403 {
2404 return parser->context->next != NULL;
2405 }
2406
2407 /* Returns nonzero if TOKEN is a string literal. */
2408
2409 static bool
2410 cp_parser_is_pure_string_literal (cp_token* token)
2411 {
2412 return (token->type == CPP_STRING ||
2413 token->type == CPP_STRING16 ||
2414 token->type == CPP_STRING32 ||
2415 token->type == CPP_WSTRING ||
2416 token->type == CPP_UTF8STRING);
2417 }
2418
2419 /* Returns nonzero if TOKEN is a string literal
2420 of a user-defined string literal. */
2421
2422 static bool
2423 cp_parser_is_string_literal (cp_token* token)
2424 {
2425 return (cp_parser_is_pure_string_literal (token) ||
2426 token->type == CPP_STRING_USERDEF ||
2427 token->type == CPP_STRING16_USERDEF ||
2428 token->type == CPP_STRING32_USERDEF ||
2429 token->type == CPP_WSTRING_USERDEF ||
2430 token->type == CPP_UTF8STRING_USERDEF);
2431 }
2432
2433 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2434
2435 static bool
2436 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2437 {
2438 return token->keyword == keyword;
2439 }
2440
2441 /* If not parsing tentatively, issue a diagnostic of the form
2442 FILE:LINE: MESSAGE before TOKEN
2443 where TOKEN is the next token in the input stream. MESSAGE
2444 (specified by the caller) is usually of the form "expected
2445 OTHER-TOKEN". */
2446
2447 static void
2448 cp_parser_error (cp_parser* parser, const char* gmsgid)
2449 {
2450 if (!cp_parser_simulate_error (parser))
2451 {
2452 cp_token *token = cp_lexer_peek_token (parser->lexer);
2453 /* This diagnostic makes more sense if it is tagged to the line
2454 of the token we just peeked at. */
2455 cp_lexer_set_source_position_from_token (token);
2456
2457 if (token->type == CPP_PRAGMA)
2458 {
2459 error_at (token->location,
2460 "%<#pragma%> is not allowed here");
2461 cp_parser_skip_to_pragma_eol (parser, token);
2462 return;
2463 }
2464
2465 c_parse_error (gmsgid,
2466 /* Because c_parser_error does not understand
2467 CPP_KEYWORD, keywords are treated like
2468 identifiers. */
2469 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2470 token->u.value, token->flags);
2471 }
2472 }
2473
2474 /* Issue an error about name-lookup failing. NAME is the
2475 IDENTIFIER_NODE DECL is the result of
2476 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2477 the thing that we hoped to find. */
2478
2479 static void
2480 cp_parser_name_lookup_error (cp_parser* parser,
2481 tree name,
2482 tree decl,
2483 name_lookup_error desired,
2484 location_t location)
2485 {
2486 /* If name lookup completely failed, tell the user that NAME was not
2487 declared. */
2488 if (decl == error_mark_node)
2489 {
2490 if (parser->scope && parser->scope != global_namespace)
2491 error_at (location, "%<%E::%E%> has not been declared",
2492 parser->scope, name);
2493 else if (parser->scope == global_namespace)
2494 error_at (location, "%<::%E%> has not been declared", name);
2495 else if (parser->object_scope
2496 && !CLASS_TYPE_P (parser->object_scope))
2497 error_at (location, "request for member %qE in non-class type %qT",
2498 name, parser->object_scope);
2499 else if (parser->object_scope)
2500 error_at (location, "%<%T::%E%> has not been declared",
2501 parser->object_scope, name);
2502 else
2503 error_at (location, "%qE has not been declared", name);
2504 }
2505 else if (parser->scope && parser->scope != global_namespace)
2506 {
2507 switch (desired)
2508 {
2509 case NLE_TYPE:
2510 error_at (location, "%<%E::%E%> is not a type",
2511 parser->scope, name);
2512 break;
2513 case NLE_CXX98:
2514 error_at (location, "%<%E::%E%> is not a class or namespace",
2515 parser->scope, name);
2516 break;
2517 case NLE_NOT_CXX98:
2518 error_at (location,
2519 "%<%E::%E%> is not a class, namespace, or enumeration",
2520 parser->scope, name);
2521 break;
2522 default:
2523 gcc_unreachable ();
2524
2525 }
2526 }
2527 else if (parser->scope == global_namespace)
2528 {
2529 switch (desired)
2530 {
2531 case NLE_TYPE:
2532 error_at (location, "%<::%E%> is not a type", name);
2533 break;
2534 case NLE_CXX98:
2535 error_at (location, "%<::%E%> is not a class or namespace", name);
2536 break;
2537 case NLE_NOT_CXX98:
2538 error_at (location,
2539 "%<::%E%> is not a class, namespace, or enumeration",
2540 name);
2541 break;
2542 default:
2543 gcc_unreachable ();
2544 }
2545 }
2546 else
2547 {
2548 switch (desired)
2549 {
2550 case NLE_TYPE:
2551 error_at (location, "%qE is not a type", name);
2552 break;
2553 case NLE_CXX98:
2554 error_at (location, "%qE is not a class or namespace", name);
2555 break;
2556 case NLE_NOT_CXX98:
2557 error_at (location,
2558 "%qE is not a class, namespace, or enumeration", name);
2559 break;
2560 default:
2561 gcc_unreachable ();
2562 }
2563 }
2564 }
2565
2566 /* If we are parsing tentatively, remember that an error has occurred
2567 during this tentative parse. Returns true if the error was
2568 simulated; false if a message should be issued by the caller. */
2569
2570 static bool
2571 cp_parser_simulate_error (cp_parser* parser)
2572 {
2573 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2574 {
2575 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2576 return true;
2577 }
2578 return false;
2579 }
2580
2581 /* This function is called when a type is defined. If type
2582 definitions are forbidden at this point, an error message is
2583 issued. */
2584
2585 static bool
2586 cp_parser_check_type_definition (cp_parser* parser)
2587 {
2588 /* If types are forbidden here, issue a message. */
2589 if (parser->type_definition_forbidden_message)
2590 {
2591 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2592 in the message need to be interpreted. */
2593 error (parser->type_definition_forbidden_message);
2594 return false;
2595 }
2596 return true;
2597 }
2598
2599 /* This function is called when the DECLARATOR is processed. The TYPE
2600 was a type defined in the decl-specifiers. If it is invalid to
2601 define a type in the decl-specifiers for DECLARATOR, an error is
2602 issued. TYPE_LOCATION is the location of TYPE and is used
2603 for error reporting. */
2604
2605 static void
2606 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2607 tree type, location_t type_location)
2608 {
2609 /* [dcl.fct] forbids type definitions in return types.
2610 Unfortunately, it's not easy to know whether or not we are
2611 processing a return type until after the fact. */
2612 while (declarator
2613 && (declarator->kind == cdk_pointer
2614 || declarator->kind == cdk_reference
2615 || declarator->kind == cdk_ptrmem))
2616 declarator = declarator->declarator;
2617 if (declarator
2618 && declarator->kind == cdk_function)
2619 {
2620 error_at (type_location,
2621 "new types may not be defined in a return type");
2622 inform (type_location,
2623 "(perhaps a semicolon is missing after the definition of %qT)",
2624 type);
2625 }
2626 }
2627
2628 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2629 "<" in any valid C++ program. If the next token is indeed "<",
2630 issue a message warning the user about what appears to be an
2631 invalid attempt to form a template-id. LOCATION is the location
2632 of the type-specifier (TYPE) */
2633
2634 static void
2635 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2636 tree type,
2637 enum tag_types tag_type,
2638 location_t location)
2639 {
2640 cp_token_position start = 0;
2641
2642 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2643 {
2644 if (TYPE_P (type))
2645 error_at (location, "%qT is not a template", type);
2646 else if (identifier_p (type))
2647 {
2648 if (tag_type != none_type)
2649 error_at (location, "%qE is not a class template", type);
2650 else
2651 error_at (location, "%qE is not a template", type);
2652 }
2653 else
2654 error_at (location, "invalid template-id");
2655 /* Remember the location of the invalid "<". */
2656 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2657 start = cp_lexer_token_position (parser->lexer, true);
2658 /* Consume the "<". */
2659 cp_lexer_consume_token (parser->lexer);
2660 /* Parse the template arguments. */
2661 cp_parser_enclosed_template_argument_list (parser);
2662 /* Permanently remove the invalid template arguments so that
2663 this error message is not issued again. */
2664 if (start)
2665 cp_lexer_purge_tokens_after (parser->lexer, start);
2666 }
2667 }
2668
2669 /* If parsing an integral constant-expression, issue an error message
2670 about the fact that THING appeared and return true. Otherwise,
2671 return false. In either case, set
2672 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2673
2674 static bool
2675 cp_parser_non_integral_constant_expression (cp_parser *parser,
2676 non_integral_constant thing)
2677 {
2678 parser->non_integral_constant_expression_p = true;
2679 if (parser->integral_constant_expression_p)
2680 {
2681 if (!parser->allow_non_integral_constant_expression_p)
2682 {
2683 const char *msg = NULL;
2684 switch (thing)
2685 {
2686 case NIC_FLOAT:
2687 error ("floating-point literal "
2688 "cannot appear in a constant-expression");
2689 return true;
2690 case NIC_CAST:
2691 error ("a cast to a type other than an integral or "
2692 "enumeration type cannot appear in a "
2693 "constant-expression");
2694 return true;
2695 case NIC_TYPEID:
2696 error ("%<typeid%> operator "
2697 "cannot appear in a constant-expression");
2698 return true;
2699 case NIC_NCC:
2700 error ("non-constant compound literals "
2701 "cannot appear in a constant-expression");
2702 return true;
2703 case NIC_FUNC_CALL:
2704 error ("a function call "
2705 "cannot appear in a constant-expression");
2706 return true;
2707 case NIC_INC:
2708 error ("an increment "
2709 "cannot appear in a constant-expression");
2710 return true;
2711 case NIC_DEC:
2712 error ("an decrement "
2713 "cannot appear in a constant-expression");
2714 return true;
2715 case NIC_ARRAY_REF:
2716 error ("an array reference "
2717 "cannot appear in a constant-expression");
2718 return true;
2719 case NIC_ADDR_LABEL:
2720 error ("the address of a label "
2721 "cannot appear in a constant-expression");
2722 return true;
2723 case NIC_OVERLOADED:
2724 error ("calls to overloaded operators "
2725 "cannot appear in a constant-expression");
2726 return true;
2727 case NIC_ASSIGNMENT:
2728 error ("an assignment cannot appear in a constant-expression");
2729 return true;
2730 case NIC_COMMA:
2731 error ("a comma operator "
2732 "cannot appear in a constant-expression");
2733 return true;
2734 case NIC_CONSTRUCTOR:
2735 error ("a call to a constructor "
2736 "cannot appear in a constant-expression");
2737 return true;
2738 case NIC_TRANSACTION:
2739 error ("a transaction expression "
2740 "cannot appear in a constant-expression");
2741 return true;
2742 case NIC_THIS:
2743 msg = "this";
2744 break;
2745 case NIC_FUNC_NAME:
2746 msg = "__FUNCTION__";
2747 break;
2748 case NIC_PRETTY_FUNC:
2749 msg = "__PRETTY_FUNCTION__";
2750 break;
2751 case NIC_C99_FUNC:
2752 msg = "__func__";
2753 break;
2754 case NIC_VA_ARG:
2755 msg = "va_arg";
2756 break;
2757 case NIC_ARROW:
2758 msg = "->";
2759 break;
2760 case NIC_POINT:
2761 msg = ".";
2762 break;
2763 case NIC_STAR:
2764 msg = "*";
2765 break;
2766 case NIC_ADDR:
2767 msg = "&";
2768 break;
2769 case NIC_PREINCREMENT:
2770 msg = "++";
2771 break;
2772 case NIC_PREDECREMENT:
2773 msg = "--";
2774 break;
2775 case NIC_NEW:
2776 msg = "new";
2777 break;
2778 case NIC_DEL:
2779 msg = "delete";
2780 break;
2781 default:
2782 gcc_unreachable ();
2783 }
2784 if (msg)
2785 error ("%qs cannot appear in a constant-expression", msg);
2786 return true;
2787 }
2788 }
2789 return false;
2790 }
2791
2792 /* Emit a diagnostic for an invalid type name. SCOPE is the
2793 qualifying scope (or NULL, if none) for ID. This function commits
2794 to the current active tentative parse, if any. (Otherwise, the
2795 problematic construct might be encountered again later, resulting
2796 in duplicate error messages.) LOCATION is the location of ID. */
2797
2798 static void
2799 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2800 tree scope, tree id,
2801 location_t location)
2802 {
2803 tree decl, old_scope;
2804 cp_parser_commit_to_tentative_parse (parser);
2805 /* Try to lookup the identifier. */
2806 old_scope = parser->scope;
2807 parser->scope = scope;
2808 decl = cp_parser_lookup_name_simple (parser, id, location);
2809 parser->scope = old_scope;
2810 /* If the lookup found a template-name, it means that the user forgot
2811 to specify an argument list. Emit a useful error message. */
2812 if (TREE_CODE (decl) == TEMPLATE_DECL)
2813 error_at (location,
2814 "invalid use of template-name %qE without an argument list",
2815 decl);
2816 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2817 error_at (location, "invalid use of destructor %qD as a type", id);
2818 else if (TREE_CODE (decl) == TYPE_DECL)
2819 /* Something like 'unsigned A a;' */
2820 error_at (location, "invalid combination of multiple type-specifiers");
2821 else if (!parser->scope)
2822 {
2823 /* Issue an error message. */
2824 error_at (location, "%qE does not name a type", id);
2825 /* If we're in a template class, it's possible that the user was
2826 referring to a type from a base class. For example:
2827
2828 template <typename T> struct A { typedef T X; };
2829 template <typename T> struct B : public A<T> { X x; };
2830
2831 The user should have said "typename A<T>::X". */
2832 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2833 inform (location, "C++11 %<constexpr%> only available with "
2834 "-std=c++11 or -std=gnu++11");
2835 else if (processing_template_decl && current_class_type
2836 && TYPE_BINFO (current_class_type))
2837 {
2838 tree b;
2839
2840 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2841 b;
2842 b = TREE_CHAIN (b))
2843 {
2844 tree base_type = BINFO_TYPE (b);
2845 if (CLASS_TYPE_P (base_type)
2846 && dependent_type_p (base_type))
2847 {
2848 tree field;
2849 /* Go from a particular instantiation of the
2850 template (which will have an empty TYPE_FIELDs),
2851 to the main version. */
2852 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2853 for (field = TYPE_FIELDS (base_type);
2854 field;
2855 field = DECL_CHAIN (field))
2856 if (TREE_CODE (field) == TYPE_DECL
2857 && DECL_NAME (field) == id)
2858 {
2859 inform (location,
2860 "(perhaps %<typename %T::%E%> was intended)",
2861 BINFO_TYPE (b), id);
2862 break;
2863 }
2864 if (field)
2865 break;
2866 }
2867 }
2868 }
2869 }
2870 /* Here we diagnose qualified-ids where the scope is actually correct,
2871 but the identifier does not resolve to a valid type name. */
2872 else if (parser->scope != error_mark_node)
2873 {
2874 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2875 {
2876 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2877 error_at (location_of (id),
2878 "%qE in namespace %qE does not name a template type",
2879 id, parser->scope);
2880 else
2881 error_at (location_of (id),
2882 "%qE in namespace %qE does not name a type",
2883 id, parser->scope);
2884 }
2885 else if (CLASS_TYPE_P (parser->scope)
2886 && constructor_name_p (id, parser->scope))
2887 {
2888 /* A<T>::A<T>() */
2889 error_at (location, "%<%T::%E%> names the constructor, not"
2890 " the type", parser->scope, id);
2891 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2892 error_at (location, "and %qT has no template constructors",
2893 parser->scope);
2894 }
2895 else if (TYPE_P (parser->scope)
2896 && dependent_scope_p (parser->scope))
2897 error_at (location, "need %<typename%> before %<%T::%E%> because "
2898 "%qT is a dependent scope",
2899 parser->scope, id, parser->scope);
2900 else if (TYPE_P (parser->scope))
2901 {
2902 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2903 error_at (location_of (id),
2904 "%qE in %q#T does not name a template type",
2905 id, parser->scope);
2906 else
2907 error_at (location_of (id),
2908 "%qE in %q#T does not name a type",
2909 id, parser->scope);
2910 }
2911 else
2912 gcc_unreachable ();
2913 }
2914 }
2915
2916 /* Check for a common situation where a type-name should be present,
2917 but is not, and issue a sensible error message. Returns true if an
2918 invalid type-name was detected.
2919
2920 The situation handled by this function are variable declarations of the
2921 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2922 Usually, `ID' should name a type, but if we got here it means that it
2923 does not. We try to emit the best possible error message depending on
2924 how exactly the id-expression looks like. */
2925
2926 static bool
2927 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2928 {
2929 tree id;
2930 cp_token *token = cp_lexer_peek_token (parser->lexer);
2931
2932 /* Avoid duplicate error about ambiguous lookup. */
2933 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2934 {
2935 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2936 if (next->type == CPP_NAME && next->ambiguous_p)
2937 goto out;
2938 }
2939
2940 cp_parser_parse_tentatively (parser);
2941 id = cp_parser_id_expression (parser,
2942 /*template_keyword_p=*/false,
2943 /*check_dependency_p=*/true,
2944 /*template_p=*/NULL,
2945 /*declarator_p=*/true,
2946 /*optional_p=*/false);
2947 /* If the next token is a (, this is a function with no explicit return
2948 type, i.e. constructor, destructor or conversion op. */
2949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2950 || TREE_CODE (id) == TYPE_DECL)
2951 {
2952 cp_parser_abort_tentative_parse (parser);
2953 return false;
2954 }
2955 if (!cp_parser_parse_definitely (parser))
2956 return false;
2957
2958 /* Emit a diagnostic for the invalid type. */
2959 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2960 id, token->location);
2961 out:
2962 /* If we aren't in the middle of a declarator (i.e. in a
2963 parameter-declaration-clause), skip to the end of the declaration;
2964 there's no point in trying to process it. */
2965 if (!parser->in_declarator_p)
2966 cp_parser_skip_to_end_of_block_or_statement (parser);
2967 return true;
2968 }
2969
2970 /* Consume tokens up to, and including, the next non-nested closing `)'.
2971 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2972 are doing error recovery. Returns -1 if OR_COMMA is true and we
2973 found an unnested comma. */
2974
2975 static int
2976 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2977 bool recovering,
2978 bool or_comma,
2979 bool consume_paren)
2980 {
2981 unsigned paren_depth = 0;
2982 unsigned brace_depth = 0;
2983 unsigned square_depth = 0;
2984
2985 if (recovering && !or_comma
2986 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2987 return 0;
2988
2989 while (true)
2990 {
2991 cp_token * token = cp_lexer_peek_token (parser->lexer);
2992
2993 switch (token->type)
2994 {
2995 case CPP_EOF:
2996 case CPP_PRAGMA_EOL:
2997 /* If we've run out of tokens, then there is no closing `)'. */
2998 return 0;
2999
3000 /* This is good for lambda expression capture-lists. */
3001 case CPP_OPEN_SQUARE:
3002 ++square_depth;
3003 break;
3004 case CPP_CLOSE_SQUARE:
3005 if (!square_depth--)
3006 return 0;
3007 break;
3008
3009 case CPP_SEMICOLON:
3010 /* This matches the processing in skip_to_end_of_statement. */
3011 if (!brace_depth)
3012 return 0;
3013 break;
3014
3015 case CPP_OPEN_BRACE:
3016 ++brace_depth;
3017 break;
3018 case CPP_CLOSE_BRACE:
3019 if (!brace_depth--)
3020 return 0;
3021 break;
3022
3023 case CPP_COMMA:
3024 if (recovering && or_comma && !brace_depth && !paren_depth
3025 && !square_depth)
3026 return -1;
3027 break;
3028
3029 case CPP_OPEN_PAREN:
3030 if (!brace_depth)
3031 ++paren_depth;
3032 break;
3033
3034 case CPP_CLOSE_PAREN:
3035 if (!brace_depth && !paren_depth--)
3036 {
3037 if (consume_paren)
3038 cp_lexer_consume_token (parser->lexer);
3039 return 1;
3040 }
3041 break;
3042
3043 default:
3044 break;
3045 }
3046
3047 /* Consume the token. */
3048 cp_lexer_consume_token (parser->lexer);
3049 }
3050 }
3051
3052 /* Consume tokens until we reach the end of the current statement.
3053 Normally, that will be just before consuming a `;'. However, if a
3054 non-nested `}' comes first, then we stop before consuming that. */
3055
3056 static void
3057 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3058 {
3059 unsigned nesting_depth = 0;
3060
3061 while (true)
3062 {
3063 cp_token *token = cp_lexer_peek_token (parser->lexer);
3064
3065 switch (token->type)
3066 {
3067 case CPP_EOF:
3068 case CPP_PRAGMA_EOL:
3069 /* If we've run out of tokens, stop. */
3070 return;
3071
3072 case CPP_SEMICOLON:
3073 /* If the next token is a `;', we have reached the end of the
3074 statement. */
3075 if (!nesting_depth)
3076 return;
3077 break;
3078
3079 case CPP_CLOSE_BRACE:
3080 /* If this is a non-nested '}', stop before consuming it.
3081 That way, when confronted with something like:
3082
3083 { 3 + }
3084
3085 we stop before consuming the closing '}', even though we
3086 have not yet reached a `;'. */
3087 if (nesting_depth == 0)
3088 return;
3089
3090 /* If it is the closing '}' for a block that we have
3091 scanned, stop -- but only after consuming the token.
3092 That way given:
3093
3094 void f g () { ... }
3095 typedef int I;
3096
3097 we will stop after the body of the erroneously declared
3098 function, but before consuming the following `typedef'
3099 declaration. */
3100 if (--nesting_depth == 0)
3101 {
3102 cp_lexer_consume_token (parser->lexer);
3103 return;
3104 }
3105
3106 case CPP_OPEN_BRACE:
3107 ++nesting_depth;
3108 break;
3109
3110 default:
3111 break;
3112 }
3113
3114 /* Consume the token. */
3115 cp_lexer_consume_token (parser->lexer);
3116 }
3117 }
3118
3119 /* This function is called at the end of a statement or declaration.
3120 If the next token is a semicolon, it is consumed; otherwise, error
3121 recovery is attempted. */
3122
3123 static void
3124 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3125 {
3126 /* Look for the trailing `;'. */
3127 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3128 {
3129 /* If there is additional (erroneous) input, skip to the end of
3130 the statement. */
3131 cp_parser_skip_to_end_of_statement (parser);
3132 /* If the next token is now a `;', consume it. */
3133 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3134 cp_lexer_consume_token (parser->lexer);
3135 }
3136 }
3137
3138 /* Skip tokens until we have consumed an entire block, or until we
3139 have consumed a non-nested `;'. */
3140
3141 static void
3142 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3143 {
3144 int nesting_depth = 0;
3145
3146 while (nesting_depth >= 0)
3147 {
3148 cp_token *token = cp_lexer_peek_token (parser->lexer);
3149
3150 switch (token->type)
3151 {
3152 case CPP_EOF:
3153 case CPP_PRAGMA_EOL:
3154 /* If we've run out of tokens, stop. */
3155 return;
3156
3157 case CPP_SEMICOLON:
3158 /* Stop if this is an unnested ';'. */
3159 if (!nesting_depth)
3160 nesting_depth = -1;
3161 break;
3162
3163 case CPP_CLOSE_BRACE:
3164 /* Stop if this is an unnested '}', or closes the outermost
3165 nesting level. */
3166 nesting_depth--;
3167 if (nesting_depth < 0)
3168 return;
3169 if (!nesting_depth)
3170 nesting_depth = -1;
3171 break;
3172
3173 case CPP_OPEN_BRACE:
3174 /* Nest. */
3175 nesting_depth++;
3176 break;
3177
3178 default:
3179 break;
3180 }
3181
3182 /* Consume the token. */
3183 cp_lexer_consume_token (parser->lexer);
3184 }
3185 }
3186
3187 /* Skip tokens until a non-nested closing curly brace is the next
3188 token, or there are no more tokens. Return true in the first case,
3189 false otherwise. */
3190
3191 static bool
3192 cp_parser_skip_to_closing_brace (cp_parser *parser)
3193 {
3194 unsigned nesting_depth = 0;
3195
3196 while (true)
3197 {
3198 cp_token *token = cp_lexer_peek_token (parser->lexer);
3199
3200 switch (token->type)
3201 {
3202 case CPP_EOF:
3203 case CPP_PRAGMA_EOL:
3204 /* If we've run out of tokens, stop. */
3205 return false;
3206
3207 case CPP_CLOSE_BRACE:
3208 /* If the next token is a non-nested `}', then we have reached
3209 the end of the current block. */
3210 if (nesting_depth-- == 0)
3211 return true;
3212 break;
3213
3214 case CPP_OPEN_BRACE:
3215 /* If it the next token is a `{', then we are entering a new
3216 block. Consume the entire block. */
3217 ++nesting_depth;
3218 break;
3219
3220 default:
3221 break;
3222 }
3223
3224 /* Consume the token. */
3225 cp_lexer_consume_token (parser->lexer);
3226 }
3227 }
3228
3229 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3230 parameter is the PRAGMA token, allowing us to purge the entire pragma
3231 sequence. */
3232
3233 static void
3234 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3235 {
3236 cp_token *token;
3237
3238 parser->lexer->in_pragma = false;
3239
3240 do
3241 token = cp_lexer_consume_token (parser->lexer);
3242 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3243
3244 /* Ensure that the pragma is not parsed again. */
3245 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3246 }
3247
3248 /* Require pragma end of line, resyncing with it as necessary. The
3249 arguments are as for cp_parser_skip_to_pragma_eol. */
3250
3251 static void
3252 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3253 {
3254 parser->lexer->in_pragma = false;
3255 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3256 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3257 }
3258
3259 /* This is a simple wrapper around make_typename_type. When the id is
3260 an unresolved identifier node, we can provide a superior diagnostic
3261 using cp_parser_diagnose_invalid_type_name. */
3262
3263 static tree
3264 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3265 tree id, location_t id_location)
3266 {
3267 tree result;
3268 if (identifier_p (id))
3269 {
3270 result = make_typename_type (scope, id, typename_type,
3271 /*complain=*/tf_none);
3272 if (result == error_mark_node)
3273 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3274 return result;
3275 }
3276 return make_typename_type (scope, id, typename_type, tf_error);
3277 }
3278
3279 /* This is a wrapper around the
3280 make_{pointer,ptrmem,reference}_declarator functions that decides
3281 which one to call based on the CODE and CLASS_TYPE arguments. The
3282 CODE argument should be one of the values returned by
3283 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3284 appertain to the pointer or reference. */
3285
3286 static cp_declarator *
3287 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3288 cp_cv_quals cv_qualifiers,
3289 cp_declarator *target,
3290 tree attributes)
3291 {
3292 if (code == ERROR_MARK)
3293 return cp_error_declarator;
3294
3295 if (code == INDIRECT_REF)
3296 if (class_type == NULL_TREE)
3297 return make_pointer_declarator (cv_qualifiers, target, attributes);
3298 else
3299 return make_ptrmem_declarator (cv_qualifiers, class_type,
3300 target, attributes);
3301 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3302 return make_reference_declarator (cv_qualifiers, target,
3303 false, attributes);
3304 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3305 return make_reference_declarator (cv_qualifiers, target,
3306 true, attributes);
3307 gcc_unreachable ();
3308 }
3309
3310 /* Create a new C++ parser. */
3311
3312 static cp_parser *
3313 cp_parser_new (void)
3314 {
3315 cp_parser *parser;
3316 cp_lexer *lexer;
3317 unsigned i;
3318
3319 /* cp_lexer_new_main is called before doing GC allocation because
3320 cp_lexer_new_main might load a PCH file. */
3321 lexer = cp_lexer_new_main ();
3322
3323 /* Initialize the binops_by_token so that we can get the tree
3324 directly from the token. */
3325 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3326 binops_by_token[binops[i].token_type] = binops[i];
3327
3328 parser = ggc_alloc_cleared_cp_parser ();
3329 parser->lexer = lexer;
3330 parser->context = cp_parser_context_new (NULL);
3331
3332 /* For now, we always accept GNU extensions. */
3333 parser->allow_gnu_extensions_p = 1;
3334
3335 /* The `>' token is a greater-than operator, not the end of a
3336 template-id. */
3337 parser->greater_than_is_operator_p = true;
3338
3339 parser->default_arg_ok_p = true;
3340
3341 /* We are not parsing a constant-expression. */
3342 parser->integral_constant_expression_p = false;
3343 parser->allow_non_integral_constant_expression_p = false;
3344 parser->non_integral_constant_expression_p = false;
3345
3346 /* Local variable names are not forbidden. */
3347 parser->local_variables_forbidden_p = false;
3348
3349 /* We are not processing an `extern "C"' declaration. */
3350 parser->in_unbraced_linkage_specification_p = false;
3351
3352 /* We are not processing a declarator. */
3353 parser->in_declarator_p = false;
3354
3355 /* We are not processing a template-argument-list. */
3356 parser->in_template_argument_list_p = false;
3357
3358 /* We are not in an iteration statement. */
3359 parser->in_statement = 0;
3360
3361 /* We are not in a switch statement. */
3362 parser->in_switch_statement_p = false;
3363
3364 /* We are not parsing a type-id inside an expression. */
3365 parser->in_type_id_in_expr_p = false;
3366
3367 /* Declarations aren't implicitly extern "C". */
3368 parser->implicit_extern_c = false;
3369
3370 /* String literals should be translated to the execution character set. */
3371 parser->translate_strings_p = true;
3372
3373 /* We are not parsing a function body. */
3374 parser->in_function_body = false;
3375
3376 /* We can correct until told otherwise. */
3377 parser->colon_corrects_to_scope_p = true;
3378
3379 /* The unparsed function queue is empty. */
3380 push_unparsed_function_queues (parser);
3381
3382 /* There are no classes being defined. */
3383 parser->num_classes_being_defined = 0;
3384
3385 /* No template parameters apply. */
3386 parser->num_template_parameter_lists = 0;
3387
3388 return parser;
3389 }
3390
3391 /* Create a cp_lexer structure which will emit the tokens in CACHE
3392 and push it onto the parser's lexer stack. This is used for delayed
3393 parsing of in-class method bodies and default arguments, and should
3394 not be confused with tentative parsing. */
3395 static void
3396 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3397 {
3398 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3399 lexer->next = parser->lexer;
3400 parser->lexer = lexer;
3401
3402 /* Move the current source position to that of the first token in the
3403 new lexer. */
3404 cp_lexer_set_source_position_from_token (lexer->next_token);
3405 }
3406
3407 /* Pop the top lexer off the parser stack. This is never used for the
3408 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3409 static void
3410 cp_parser_pop_lexer (cp_parser *parser)
3411 {
3412 cp_lexer *lexer = parser->lexer;
3413 parser->lexer = lexer->next;
3414 cp_lexer_destroy (lexer);
3415
3416 /* Put the current source position back where it was before this
3417 lexer was pushed. */
3418 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3419 }
3420
3421 /* Lexical conventions [gram.lex] */
3422
3423 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3424 identifier. */
3425
3426 static tree
3427 cp_parser_identifier (cp_parser* parser)
3428 {
3429 cp_token *token;
3430
3431 /* Look for the identifier. */
3432 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3433 /* Return the value. */
3434 return token ? token->u.value : error_mark_node;
3435 }
3436
3437 /* Parse a sequence of adjacent string constants. Returns a
3438 TREE_STRING representing the combined, nul-terminated string
3439 constant. If TRANSLATE is true, translate the string to the
3440 execution character set. If WIDE_OK is true, a wide string is
3441 invalid here.
3442
3443 C++98 [lex.string] says that if a narrow string literal token is
3444 adjacent to a wide string literal token, the behavior is undefined.
3445 However, C99 6.4.5p4 says that this results in a wide string literal.
3446 We follow C99 here, for consistency with the C front end.
3447
3448 This code is largely lifted from lex_string() in c-lex.c.
3449
3450 FUTURE: ObjC++ will need to handle @-strings here. */
3451 static tree
3452 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3453 {
3454 tree value;
3455 size_t count;
3456 struct obstack str_ob;
3457 cpp_string str, istr, *strs;
3458 cp_token *tok;
3459 enum cpp_ttype type, curr_type;
3460 int have_suffix_p = 0;
3461 tree string_tree;
3462 tree suffix_id = NULL_TREE;
3463 bool curr_tok_is_userdef_p = false;
3464
3465 tok = cp_lexer_peek_token (parser->lexer);
3466 if (!cp_parser_is_string_literal (tok))
3467 {
3468 cp_parser_error (parser, "expected string-literal");
3469 return error_mark_node;
3470 }
3471
3472 if (cpp_userdef_string_p (tok->type))
3473 {
3474 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3475 curr_type = cpp_userdef_string_remove_type (tok->type);
3476 curr_tok_is_userdef_p = true;
3477 }
3478 else
3479 {
3480 string_tree = tok->u.value;
3481 curr_type = tok->type;
3482 }
3483 type = curr_type;
3484
3485 /* Try to avoid the overhead of creating and destroying an obstack
3486 for the common case of just one string. */
3487 if (!cp_parser_is_string_literal
3488 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3489 {
3490 cp_lexer_consume_token (parser->lexer);
3491
3492 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3493 str.len = TREE_STRING_LENGTH (string_tree);
3494 count = 1;
3495
3496 if (curr_tok_is_userdef_p)
3497 {
3498 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3499 have_suffix_p = 1;
3500 curr_type = cpp_userdef_string_remove_type (tok->type);
3501 }
3502 else
3503 curr_type = tok->type;
3504
3505 strs = &str;
3506 }
3507 else
3508 {
3509 gcc_obstack_init (&str_ob);
3510 count = 0;
3511
3512 do
3513 {
3514 cp_lexer_consume_token (parser->lexer);
3515 count++;
3516 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3517 str.len = TREE_STRING_LENGTH (string_tree);
3518
3519 if (curr_tok_is_userdef_p)
3520 {
3521 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3522 if (have_suffix_p == 0)
3523 {
3524 suffix_id = curr_suffix_id;
3525 have_suffix_p = 1;
3526 }
3527 else if (have_suffix_p == 1
3528 && curr_suffix_id != suffix_id)
3529 {
3530 error ("inconsistent user-defined literal suffixes"
3531 " %qD and %qD in string literal",
3532 suffix_id, curr_suffix_id);
3533 have_suffix_p = -1;
3534 }
3535 curr_type = cpp_userdef_string_remove_type (tok->type);
3536 }
3537 else
3538 curr_type = tok->type;
3539
3540 if (type != curr_type)
3541 {
3542 if (type == CPP_STRING)
3543 type = curr_type;
3544 else if (curr_type != CPP_STRING)
3545 error_at (tok->location,
3546 "unsupported non-standard concatenation "
3547 "of string literals");
3548 }
3549
3550 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3551
3552 tok = cp_lexer_peek_token (parser->lexer);
3553 if (cpp_userdef_string_p (tok->type))
3554 {
3555 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3556 curr_type = cpp_userdef_string_remove_type (tok->type);
3557 curr_tok_is_userdef_p = true;
3558 }
3559 else
3560 {
3561 string_tree = tok->u.value;
3562 curr_type = tok->type;
3563 curr_tok_is_userdef_p = false;
3564 }
3565 }
3566 while (cp_parser_is_string_literal (tok));
3567
3568 strs = (cpp_string *) obstack_finish (&str_ob);
3569 }
3570
3571 if (type != CPP_STRING && !wide_ok)
3572 {
3573 cp_parser_error (parser, "a wide string is invalid in this context");
3574 type = CPP_STRING;
3575 }
3576
3577 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3578 (parse_in, strs, count, &istr, type))
3579 {
3580 value = build_string (istr.len, (const char *)istr.text);
3581 free (CONST_CAST (unsigned char *, istr.text));
3582
3583 switch (type)
3584 {
3585 default:
3586 case CPP_STRING:
3587 case CPP_UTF8STRING:
3588 TREE_TYPE (value) = char_array_type_node;
3589 break;
3590 case CPP_STRING16:
3591 TREE_TYPE (value) = char16_array_type_node;
3592 break;
3593 case CPP_STRING32:
3594 TREE_TYPE (value) = char32_array_type_node;
3595 break;
3596 case CPP_WSTRING:
3597 TREE_TYPE (value) = wchar_array_type_node;
3598 break;
3599 }
3600
3601 value = fix_string_type (value);
3602
3603 if (have_suffix_p)
3604 {
3605 tree literal = build_userdef_literal (suffix_id, value,
3606 OT_NONE, NULL_TREE);
3607 tok->u.value = literal;
3608 return cp_parser_userdef_string_literal (tok);
3609 }
3610 }
3611 else
3612 /* cpp_interpret_string has issued an error. */
3613 value = error_mark_node;
3614
3615 if (count > 1)
3616 obstack_free (&str_ob, 0);
3617
3618 return value;
3619 }
3620
3621 /* Look up a literal operator with the name and the exact arguments. */
3622
3623 static tree
3624 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3625 {
3626 tree decl, fns;
3627 decl = lookup_name (name);
3628 if (!decl || !is_overloaded_fn (decl))
3629 return error_mark_node;
3630
3631 for (fns = decl; fns; fns = OVL_NEXT (fns))
3632 {
3633 unsigned int ix;
3634 bool found = true;
3635 tree fn = OVL_CURRENT (fns);
3636 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3637 if (parmtypes != NULL_TREE)
3638 {
3639 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3640 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3641 {
3642 tree tparm = TREE_VALUE (parmtypes);
3643 tree targ = TREE_TYPE ((*args)[ix]);
3644 bool ptr = TYPE_PTR_P (tparm);
3645 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3646 if ((ptr || arr || !same_type_p (tparm, targ))
3647 && (!ptr || !arr
3648 || !same_type_p (TREE_TYPE (tparm),
3649 TREE_TYPE (targ))))
3650 found = false;
3651 }
3652 if (found
3653 && ix == vec_safe_length (args)
3654 /* May be this should be sufficient_parms_p instead,
3655 depending on how exactly should user-defined literals
3656 work in presence of default arguments on the literal
3657 operator parameters. */
3658 && parmtypes == void_list_node)
3659 return fn;
3660 }
3661 }
3662
3663 return error_mark_node;
3664 }
3665
3666 /* Parse a user-defined char constant. Returns a call to a user-defined
3667 literal operator taking the character as an argument. */
3668
3669 static tree
3670 cp_parser_userdef_char_literal (cp_parser *parser)
3671 {
3672 cp_token *token = cp_lexer_consume_token (parser->lexer);
3673 tree literal = token->u.value;
3674 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3675 tree value = USERDEF_LITERAL_VALUE (literal);
3676 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3677 tree decl, result;
3678
3679 /* Build up a call to the user-defined operator */
3680 /* Lookup the name we got back from the id-expression. */
3681 vec<tree, va_gc> *args = make_tree_vector ();
3682 vec_safe_push (args, value);
3683 decl = lookup_literal_operator (name, args);
3684 if (!decl || decl == error_mark_node)
3685 {
3686 error ("unable to find character literal operator %qD with %qT argument",
3687 name, TREE_TYPE (value));
3688 release_tree_vector (args);
3689 return error_mark_node;
3690 }
3691 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3692 release_tree_vector (args);
3693 if (result != error_mark_node)
3694 return result;
3695
3696 error ("unable to find character literal operator %qD with %qT argument",
3697 name, TREE_TYPE (value));
3698 return error_mark_node;
3699 }
3700
3701 /* A subroutine of cp_parser_userdef_numeric_literal to
3702 create a char... template parameter pack from a string node. */
3703
3704 static tree
3705 make_char_string_pack (tree value)
3706 {
3707 tree charvec;
3708 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3709 const char *str = TREE_STRING_POINTER (value);
3710 int i, len = TREE_STRING_LENGTH (value) - 1;
3711 tree argvec = make_tree_vec (1);
3712
3713 /* Fill in CHARVEC with all of the parameters. */
3714 charvec = make_tree_vec (len);
3715 for (i = 0; i < len; ++i)
3716 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3717
3718 /* Build the argument packs. */
3719 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3720 TREE_TYPE (argpack) = char_type_node;
3721
3722 TREE_VEC_ELT (argvec, 0) = argpack;
3723
3724 return argvec;
3725 }
3726
3727 /* A subroutine of cp_parser_userdef_numeric_literal to
3728 create a char... template parameter pack from a string node. */
3729
3730 static tree
3731 make_string_pack (tree value)
3732 {
3733 tree charvec;
3734 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3735 const char *str = TREE_STRING_POINTER (value);
3736 int i, len = TREE_STRING_LENGTH (value) - 1;
3737 tree argvec = make_tree_vec (2);
3738
3739 tree string_char_type_node = TREE_TYPE (TREE_TYPE (value));
3740
3741 /* First template parm is character type. */
3742 TREE_VEC_ELT (argvec, 0) = string_char_type_node;
3743
3744 /* Fill in CHARVEC with all of the parameters. */
3745 charvec = make_tree_vec (len);
3746 for (i = 0; i < len; ++i)
3747 TREE_VEC_ELT (charvec, i) = build_int_cst (string_char_type_node, str[i]);
3748
3749 /* Build the argument packs. */
3750 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3751 TREE_TYPE (argpack) = string_char_type_node;
3752
3753 TREE_VEC_ELT (argvec, 1) = argpack;
3754
3755 return argvec;
3756 }
3757
3758 /* Parse a user-defined numeric constant. returns a call to a user-defined
3759 literal operator. */
3760
3761 static tree
3762 cp_parser_userdef_numeric_literal (cp_parser *parser)
3763 {
3764 cp_token *token = cp_lexer_consume_token (parser->lexer);
3765 tree literal = token->u.value;
3766 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3767 tree value = USERDEF_LITERAL_VALUE (literal);
3768 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3769 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3770 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3771 tree decl, result;
3772 vec<tree, va_gc> *args;
3773
3774 /* Look for a literal operator taking the exact type of numeric argument
3775 as the literal value. */
3776 args = make_tree_vector ();
3777 vec_safe_push (args, value);
3778 decl = lookup_literal_operator (name, args);
3779 if (decl && decl != error_mark_node)
3780 {
3781 result = finish_call_expr (decl, &args, false, true, tf_none);
3782 if (result != error_mark_node)
3783 {
3784 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3785 warning_at (token->location, OPT_Woverflow,
3786 "integer literal exceeds range of %qT type",
3787 long_long_unsigned_type_node);
3788 else
3789 {
3790 if (overflow > 0)
3791 warning_at (token->location, OPT_Woverflow,
3792 "floating literal exceeds range of %qT type",
3793 long_double_type_node);
3794 else if (overflow < 0)
3795 warning_at (token->location, OPT_Woverflow,
3796 "floating literal truncated to zero");
3797 }
3798 release_tree_vector (args);
3799 return result;
3800 }
3801 }
3802 release_tree_vector (args);
3803
3804 /* If the numeric argument didn't work, look for a raw literal
3805 operator taking a const char* argument consisting of the number
3806 in string format. */
3807 args = make_tree_vector ();
3808 vec_safe_push (args, num_string);
3809 decl = lookup_literal_operator (name, args);
3810 if (decl && decl != error_mark_node)
3811 {
3812 result = finish_call_expr (decl, &args, false, true, tf_none);
3813 if (result != error_mark_node)
3814 {
3815 release_tree_vector (args);
3816 return result;
3817 }
3818 }
3819 release_tree_vector (args);
3820
3821 /* If the raw literal didn't work, look for a non-type template
3822 function with parameter pack char.... Call the function with
3823 template parameter characters representing the number. */
3824 args = make_tree_vector ();
3825 decl = lookup_literal_operator (name, args);
3826 if (decl && decl != error_mark_node)
3827 {
3828 tree tmpl_args = make_char_string_pack (num_string);
3829 decl = lookup_template_function (decl, tmpl_args);
3830 result = finish_call_expr (decl, &args, false, true, tf_none);
3831 if (result != error_mark_node)
3832 {
3833 release_tree_vector (args);
3834 return result;
3835 }
3836 }
3837 release_tree_vector (args);
3838
3839 error ("unable to find numeric literal operator %qD", name);
3840 return error_mark_node;
3841 }
3842
3843 /* Parse a user-defined string constant. Returns a call to a user-defined
3844 literal operator taking a character pointer and the length of the string
3845 as arguments. */
3846
3847 static tree
3848 cp_parser_userdef_string_literal (cp_token *token)
3849 {
3850 tree literal = token->u.value;
3851 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3852 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3853 tree value = USERDEF_LITERAL_VALUE (literal);
3854 int len = TREE_STRING_LENGTH (value)
3855 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3856 tree decl, result;
3857 vec<tree, va_gc> *args;
3858
3859 /* Look for a template function with typename parameter CharT
3860 and parameter pack CharT... Call the function with
3861 template parameter characters representing the string. */
3862 args = make_tree_vector ();
3863 decl = lookup_literal_operator (name, args);
3864 if (decl && decl != error_mark_node)
3865 {
3866 tree tmpl_args = make_string_pack (value);
3867 decl = lookup_template_function (decl, tmpl_args);
3868 result = finish_call_expr (decl, &args, false, true, tf_none);
3869 if (result != error_mark_node)
3870 {
3871 release_tree_vector (args);
3872 return result;
3873 }
3874 }
3875 release_tree_vector (args);
3876
3877 /* Build up a call to the user-defined operator */
3878 /* Lookup the name we got back from the id-expression. */
3879 args = make_tree_vector ();
3880 vec_safe_push (args, value);
3881 vec_safe_push (args, build_int_cst (size_type_node, len));
3882 decl = lookup_name (name);
3883 if (!decl || decl == error_mark_node)
3884 {
3885 error ("unable to find string literal operator %qD", name);
3886 release_tree_vector (args);
3887 return error_mark_node;
3888 }
3889 result = finish_call_expr (decl, &args, false, true, tf_none);
3890 release_tree_vector (args);
3891 if (result != error_mark_node)
3892 return result;
3893
3894 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3895 name, TREE_TYPE (value), size_type_node);
3896 return error_mark_node;
3897 }
3898
3899
3900 /* Basic concepts [gram.basic] */
3901
3902 /* Parse a translation-unit.
3903
3904 translation-unit:
3905 declaration-seq [opt]
3906
3907 Returns TRUE if all went well. */
3908
3909 static bool
3910 cp_parser_translation_unit (cp_parser* parser)
3911 {
3912 /* The address of the first non-permanent object on the declarator
3913 obstack. */
3914 static void *declarator_obstack_base;
3915
3916 bool success;
3917
3918 /* Create the declarator obstack, if necessary. */
3919 if (!cp_error_declarator)
3920 {
3921 gcc_obstack_init (&declarator_obstack);
3922 /* Create the error declarator. */
3923 cp_error_declarator = make_declarator (cdk_error);
3924 /* Create the empty parameter list. */
3925 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3926 /* Remember where the base of the declarator obstack lies. */
3927 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3928 }
3929
3930 cp_parser_declaration_seq_opt (parser);
3931
3932 /* If there are no tokens left then all went well. */
3933 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3934 {
3935 /* Get rid of the token array; we don't need it any more. */
3936 cp_lexer_destroy (parser->lexer);
3937 parser->lexer = NULL;
3938
3939 /* This file might have been a context that's implicitly extern
3940 "C". If so, pop the lang context. (Only relevant for PCH.) */
3941 if (parser->implicit_extern_c)
3942 {
3943 pop_lang_context ();
3944 parser->implicit_extern_c = false;
3945 }
3946
3947 /* Finish up. */
3948 finish_translation_unit ();
3949
3950 success = true;
3951 }
3952 else
3953 {
3954 cp_parser_error (parser, "expected declaration");
3955 success = false;
3956 }
3957
3958 /* Make sure the declarator obstack was fully cleaned up. */
3959 gcc_assert (obstack_next_free (&declarator_obstack)
3960 == declarator_obstack_base);
3961
3962 /* All went well. */
3963 return success;
3964 }
3965
3966 /* Return the appropriate tsubst flags for parsing, possibly in N3276
3967 decltype context. */
3968
3969 static inline tsubst_flags_t
3970 complain_flags (bool decltype_p)
3971 {
3972 tsubst_flags_t complain = tf_warning_or_error;
3973 if (decltype_p)
3974 complain |= tf_decltype;
3975 return complain;
3976 }
3977
3978 /* Expressions [gram.expr] */
3979
3980 /* Parse a primary-expression.
3981
3982 primary-expression:
3983 literal
3984 this
3985 ( expression )
3986 id-expression
3987
3988 GNU Extensions:
3989
3990 primary-expression:
3991 ( compound-statement )
3992 __builtin_va_arg ( assignment-expression , type-id )
3993 __builtin_offsetof ( type-id , offsetof-expression )
3994
3995 C++ Extensions:
3996 __has_nothrow_assign ( type-id )
3997 __has_nothrow_constructor ( type-id )
3998 __has_nothrow_copy ( type-id )
3999 __has_trivial_assign ( type-id )
4000 __has_trivial_constructor ( type-id )
4001 __has_trivial_copy ( type-id )
4002 __has_trivial_destructor ( type-id )
4003 __has_virtual_destructor ( type-id )
4004 __is_abstract ( type-id )
4005 __is_base_of ( type-id , type-id )
4006 __is_class ( type-id )
4007 __is_convertible_to ( type-id , type-id )
4008 __is_empty ( type-id )
4009 __is_enum ( type-id )
4010 __is_final ( type-id )
4011 __is_literal_type ( type-id )
4012 __is_pod ( type-id )
4013 __is_polymorphic ( type-id )
4014 __is_std_layout ( type-id )
4015 __is_trivial ( type-id )
4016 __is_union ( type-id )
4017
4018 Objective-C++ Extension:
4019
4020 primary-expression:
4021 objc-expression
4022
4023 literal:
4024 __null
4025
4026 ADDRESS_P is true iff this expression was immediately preceded by
4027 "&" and therefore might denote a pointer-to-member. CAST_P is true
4028 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4029 true iff this expression is a template argument.
4030
4031 Returns a representation of the expression. Upon return, *IDK
4032 indicates what kind of id-expression (if any) was present. */
4033
4034 static tree
4035 cp_parser_primary_expression (cp_parser *parser,
4036 bool address_p,
4037 bool cast_p,
4038 bool template_arg_p,
4039 bool decltype_p,
4040 cp_id_kind *idk)
4041 {
4042 cp_token *token = NULL;
4043
4044 /* Assume the primary expression is not an id-expression. */
4045 *idk = CP_ID_KIND_NONE;
4046
4047 /* Peek at the next token. */
4048 token = cp_lexer_peek_token (parser->lexer);
4049 switch (token->type)
4050 {
4051 /* literal:
4052 integer-literal
4053 character-literal
4054 floating-literal
4055 string-literal
4056 boolean-literal
4057 pointer-literal
4058 user-defined-literal */
4059 case CPP_CHAR:
4060 case CPP_CHAR16:
4061 case CPP_CHAR32:
4062 case CPP_WCHAR:
4063 case CPP_NUMBER:
4064 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4065 return cp_parser_userdef_numeric_literal (parser);
4066 token = cp_lexer_consume_token (parser->lexer);
4067 if (TREE_CODE (token->u.value) == FIXED_CST)
4068 {
4069 error_at (token->location,
4070 "fixed-point types not supported in C++");
4071 return error_mark_node;
4072 }
4073 /* Floating-point literals are only allowed in an integral
4074 constant expression if they are cast to an integral or
4075 enumeration type. */
4076 if (TREE_CODE (token->u.value) == REAL_CST
4077 && parser->integral_constant_expression_p
4078 && pedantic)
4079 {
4080 /* CAST_P will be set even in invalid code like "int(2.7 +
4081 ...)". Therefore, we have to check that the next token
4082 is sure to end the cast. */
4083 if (cast_p)
4084 {
4085 cp_token *next_token;
4086
4087 next_token = cp_lexer_peek_token (parser->lexer);
4088 if (/* The comma at the end of an
4089 enumerator-definition. */
4090 next_token->type != CPP_COMMA
4091 /* The curly brace at the end of an enum-specifier. */
4092 && next_token->type != CPP_CLOSE_BRACE
4093 /* The end of a statement. */
4094 && next_token->type != CPP_SEMICOLON
4095 /* The end of the cast-expression. */
4096 && next_token->type != CPP_CLOSE_PAREN
4097 /* The end of an array bound. */
4098 && next_token->type != CPP_CLOSE_SQUARE
4099 /* The closing ">" in a template-argument-list. */
4100 && (next_token->type != CPP_GREATER
4101 || parser->greater_than_is_operator_p)
4102 /* C++0x only: A ">>" treated like two ">" tokens,
4103 in a template-argument-list. */
4104 && (next_token->type != CPP_RSHIFT
4105 || (cxx_dialect == cxx98)
4106 || parser->greater_than_is_operator_p))
4107 cast_p = false;
4108 }
4109
4110 /* If we are within a cast, then the constraint that the
4111 cast is to an integral or enumeration type will be
4112 checked at that point. If we are not within a cast, then
4113 this code is invalid. */
4114 if (!cast_p)
4115 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4116 }
4117 return token->u.value;
4118
4119 case CPP_CHAR_USERDEF:
4120 case CPP_CHAR16_USERDEF:
4121 case CPP_CHAR32_USERDEF:
4122 case CPP_WCHAR_USERDEF:
4123 return cp_parser_userdef_char_literal (parser);
4124
4125 case CPP_STRING:
4126 case CPP_STRING16:
4127 case CPP_STRING32:
4128 case CPP_WSTRING:
4129 case CPP_UTF8STRING:
4130 case CPP_STRING_USERDEF:
4131 case CPP_STRING16_USERDEF:
4132 case CPP_STRING32_USERDEF:
4133 case CPP_WSTRING_USERDEF:
4134 case CPP_UTF8STRING_USERDEF:
4135 /* ??? Should wide strings be allowed when parser->translate_strings_p
4136 is false (i.e. in attributes)? If not, we can kill the third
4137 argument to cp_parser_string_literal. */
4138 return cp_parser_string_literal (parser,
4139 parser->translate_strings_p,
4140 true);
4141
4142 case CPP_OPEN_PAREN:
4143 {
4144 tree expr;
4145 bool saved_greater_than_is_operator_p;
4146
4147 /* Consume the `('. */
4148 cp_lexer_consume_token (parser->lexer);
4149 /* Within a parenthesized expression, a `>' token is always
4150 the greater-than operator. */
4151 saved_greater_than_is_operator_p
4152 = parser->greater_than_is_operator_p;
4153 parser->greater_than_is_operator_p = true;
4154 /* If we see `( { ' then we are looking at the beginning of
4155 a GNU statement-expression. */
4156 if (cp_parser_allow_gnu_extensions_p (parser)
4157 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4158 {
4159 /* Statement-expressions are not allowed by the standard. */
4160 pedwarn (token->location, OPT_Wpedantic,
4161 "ISO C++ forbids braced-groups within expressions");
4162
4163 /* And they're not allowed outside of a function-body; you
4164 cannot, for example, write:
4165
4166 int i = ({ int j = 3; j + 1; });
4167
4168 at class or namespace scope. */
4169 if (!parser->in_function_body
4170 || parser->in_template_argument_list_p)
4171 {
4172 error_at (token->location,
4173 "statement-expressions are not allowed outside "
4174 "functions nor in template-argument lists");
4175 cp_parser_skip_to_end_of_block_or_statement (parser);
4176 expr = error_mark_node;
4177 }
4178 else
4179 {
4180 /* Start the statement-expression. */
4181 expr = begin_stmt_expr ();
4182 /* Parse the compound-statement. */
4183 cp_parser_compound_statement (parser, expr, false, false);
4184 /* Finish up. */
4185 expr = finish_stmt_expr (expr, false);
4186 }
4187 }
4188 else
4189 {
4190 /* Parse the parenthesized expression. */
4191 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4192 /* Let the front end know that this expression was
4193 enclosed in parentheses. This matters in case, for
4194 example, the expression is of the form `A::B', since
4195 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4196 not. */
4197 expr = finish_parenthesized_expr (expr);
4198 /* DR 705: Wrapping an unqualified name in parentheses
4199 suppresses arg-dependent lookup. We want to pass back
4200 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4201 (c++/37862), but none of the others. */
4202 if (*idk != CP_ID_KIND_QUALIFIED)
4203 *idk = CP_ID_KIND_NONE;
4204 }
4205 /* The `>' token might be the end of a template-id or
4206 template-parameter-list now. */
4207 parser->greater_than_is_operator_p
4208 = saved_greater_than_is_operator_p;
4209 /* Consume the `)'. */
4210 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4211 cp_parser_skip_to_end_of_statement (parser);
4212
4213 return expr;
4214 }
4215
4216 case CPP_OPEN_SQUARE:
4217 if (c_dialect_objc ())
4218 /* We have an Objective-C++ message. */
4219 return cp_parser_objc_expression (parser);
4220 {
4221 tree lam = cp_parser_lambda_expression (parser);
4222 /* Don't warn about a failed tentative parse. */
4223 if (cp_parser_error_occurred (parser))
4224 return error_mark_node;
4225 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4226 return lam;
4227 }
4228
4229 case CPP_OBJC_STRING:
4230 if (c_dialect_objc ())
4231 /* We have an Objective-C++ string literal. */
4232 return cp_parser_objc_expression (parser);
4233 cp_parser_error (parser, "expected primary-expression");
4234 return error_mark_node;
4235
4236 case CPP_KEYWORD:
4237 switch (token->keyword)
4238 {
4239 /* These two are the boolean literals. */
4240 case RID_TRUE:
4241 cp_lexer_consume_token (parser->lexer);
4242 return boolean_true_node;
4243 case RID_FALSE:
4244 cp_lexer_consume_token (parser->lexer);
4245 return boolean_false_node;
4246
4247 /* The `__null' literal. */
4248 case RID_NULL:
4249 cp_lexer_consume_token (parser->lexer);
4250 return null_node;
4251
4252 /* The `nullptr' literal. */
4253 case RID_NULLPTR:
4254 cp_lexer_consume_token (parser->lexer);
4255 return nullptr_node;
4256
4257 /* Recognize the `this' keyword. */
4258 case RID_THIS:
4259 cp_lexer_consume_token (parser->lexer);
4260 if (parser->local_variables_forbidden_p)
4261 {
4262 error_at (token->location,
4263 "%<this%> may not be used in this context");
4264 return error_mark_node;
4265 }
4266 /* Pointers cannot appear in constant-expressions. */
4267 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4268 return error_mark_node;
4269 return finish_this_expr ();
4270
4271 /* The `operator' keyword can be the beginning of an
4272 id-expression. */
4273 case RID_OPERATOR:
4274 goto id_expression;
4275
4276 case RID_FUNCTION_NAME:
4277 case RID_PRETTY_FUNCTION_NAME:
4278 case RID_C99_FUNCTION_NAME:
4279 {
4280 non_integral_constant name;
4281
4282 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4283 __func__ are the names of variables -- but they are
4284 treated specially. Therefore, they are handled here,
4285 rather than relying on the generic id-expression logic
4286 below. Grammatically, these names are id-expressions.
4287
4288 Consume the token. */
4289 token = cp_lexer_consume_token (parser->lexer);
4290
4291 switch (token->keyword)
4292 {
4293 case RID_FUNCTION_NAME:
4294 name = NIC_FUNC_NAME;
4295 break;
4296 case RID_PRETTY_FUNCTION_NAME:
4297 name = NIC_PRETTY_FUNC;
4298 break;
4299 case RID_C99_FUNCTION_NAME:
4300 name = NIC_C99_FUNC;
4301 break;
4302 default:
4303 gcc_unreachable ();
4304 }
4305
4306 if (cp_parser_non_integral_constant_expression (parser, name))
4307 return error_mark_node;
4308
4309 /* Look up the name. */
4310 return finish_fname (token->u.value);
4311 }
4312
4313 case RID_VA_ARG:
4314 {
4315 tree expression;
4316 tree type;
4317 source_location type_location;
4318
4319 /* The `__builtin_va_arg' construct is used to handle
4320 `va_arg'. Consume the `__builtin_va_arg' token. */
4321 cp_lexer_consume_token (parser->lexer);
4322 /* Look for the opening `('. */
4323 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4324 /* Now, parse the assignment-expression. */
4325 expression = cp_parser_assignment_expression (parser,
4326 /*cast_p=*/false, NULL);
4327 /* Look for the `,'. */
4328 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4329 type_location = cp_lexer_peek_token (parser->lexer)->location;
4330 /* Parse the type-id. */
4331 type = cp_parser_type_id (parser);
4332 /* Look for the closing `)'. */
4333 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4334 /* Using `va_arg' in a constant-expression is not
4335 allowed. */
4336 if (cp_parser_non_integral_constant_expression (parser,
4337 NIC_VA_ARG))
4338 return error_mark_node;
4339 return build_x_va_arg (type_location, expression, type);
4340 }
4341
4342 case RID_OFFSETOF:
4343 return cp_parser_builtin_offsetof (parser);
4344
4345 case RID_HAS_NOTHROW_ASSIGN:
4346 case RID_HAS_NOTHROW_CONSTRUCTOR:
4347 case RID_HAS_NOTHROW_COPY:
4348 case RID_HAS_TRIVIAL_ASSIGN:
4349 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4350 case RID_HAS_TRIVIAL_COPY:
4351 case RID_HAS_TRIVIAL_DESTRUCTOR:
4352 case RID_HAS_VIRTUAL_DESTRUCTOR:
4353 case RID_IS_ABSTRACT:
4354 case RID_IS_BASE_OF:
4355 case RID_IS_CLASS:
4356 case RID_IS_CONVERTIBLE_TO:
4357 case RID_IS_EMPTY:
4358 case RID_IS_ENUM:
4359 case RID_IS_FINAL:
4360 case RID_IS_LITERAL_TYPE:
4361 case RID_IS_POD:
4362 case RID_IS_POLYMORPHIC:
4363 case RID_IS_STD_LAYOUT:
4364 case RID_IS_TRIVIAL:
4365 case RID_IS_UNION:
4366 return cp_parser_trait_expr (parser, token->keyword);
4367
4368 /* Objective-C++ expressions. */
4369 case RID_AT_ENCODE:
4370 case RID_AT_PROTOCOL:
4371 case RID_AT_SELECTOR:
4372 return cp_parser_objc_expression (parser);
4373
4374 case RID_TEMPLATE:
4375 if (parser->in_function_body
4376 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4377 == CPP_LESS))
4378 {
4379 error_at (token->location,
4380 "a template declaration cannot appear at block scope");
4381 cp_parser_skip_to_end_of_block_or_statement (parser);
4382 return error_mark_node;
4383 }
4384 default:
4385 cp_parser_error (parser, "expected primary-expression");
4386 return error_mark_node;
4387 }
4388
4389 /* An id-expression can start with either an identifier, a
4390 `::' as the beginning of a qualified-id, or the "operator"
4391 keyword. */
4392 case CPP_NAME:
4393 case CPP_SCOPE:
4394 case CPP_TEMPLATE_ID:
4395 case CPP_NESTED_NAME_SPECIFIER:
4396 {
4397 tree id_expression;
4398 tree decl;
4399 const char *error_msg;
4400 bool template_p;
4401 bool done;
4402 cp_token *id_expr_token;
4403
4404 id_expression:
4405 /* Parse the id-expression. */
4406 id_expression
4407 = cp_parser_id_expression (parser,
4408 /*template_keyword_p=*/false,
4409 /*check_dependency_p=*/true,
4410 &template_p,
4411 /*declarator_p=*/false,
4412 /*optional_p=*/false);
4413 if (id_expression == error_mark_node)
4414 return error_mark_node;
4415 id_expr_token = token;
4416 token = cp_lexer_peek_token (parser->lexer);
4417 done = (token->type != CPP_OPEN_SQUARE
4418 && token->type != CPP_OPEN_PAREN
4419 && token->type != CPP_DOT
4420 && token->type != CPP_DEREF
4421 && token->type != CPP_PLUS_PLUS
4422 && token->type != CPP_MINUS_MINUS);
4423 /* If we have a template-id, then no further lookup is
4424 required. If the template-id was for a template-class, we
4425 will sometimes have a TYPE_DECL at this point. */
4426 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4427 || TREE_CODE (id_expression) == TYPE_DECL)
4428 decl = id_expression;
4429 /* Look up the name. */
4430 else
4431 {
4432 tree ambiguous_decls;
4433
4434 /* If we already know that this lookup is ambiguous, then
4435 we've already issued an error message; there's no reason
4436 to check again. */
4437 if (id_expr_token->type == CPP_NAME
4438 && id_expr_token->ambiguous_p)
4439 {
4440 cp_parser_simulate_error (parser);
4441 return error_mark_node;
4442 }
4443
4444 decl = cp_parser_lookup_name (parser, id_expression,
4445 none_type,
4446 template_p,
4447 /*is_namespace=*/false,
4448 /*check_dependency=*/true,
4449 &ambiguous_decls,
4450 id_expr_token->location);
4451 /* If the lookup was ambiguous, an error will already have
4452 been issued. */
4453 if (ambiguous_decls)
4454 return error_mark_node;
4455
4456 /* In Objective-C++, we may have an Objective-C 2.0
4457 dot-syntax for classes here. */
4458 if (c_dialect_objc ()
4459 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4460 && TREE_CODE (decl) == TYPE_DECL
4461 && objc_is_class_name (decl))
4462 {
4463 tree component;
4464 cp_lexer_consume_token (parser->lexer);
4465 component = cp_parser_identifier (parser);
4466 if (component == error_mark_node)
4467 return error_mark_node;
4468
4469 return objc_build_class_component_ref (id_expression, component);
4470 }
4471
4472 /* In Objective-C++, an instance variable (ivar) may be preferred
4473 to whatever cp_parser_lookup_name() found. */
4474 decl = objc_lookup_ivar (decl, id_expression);
4475
4476 /* If name lookup gives us a SCOPE_REF, then the
4477 qualifying scope was dependent. */
4478 if (TREE_CODE (decl) == SCOPE_REF)
4479 {
4480 /* At this point, we do not know if DECL is a valid
4481 integral constant expression. We assume that it is
4482 in fact such an expression, so that code like:
4483
4484 template <int N> struct A {
4485 int a[B<N>::i];
4486 };
4487
4488 is accepted. At template-instantiation time, we
4489 will check that B<N>::i is actually a constant. */
4490 return decl;
4491 }
4492 /* Check to see if DECL is a local variable in a context
4493 where that is forbidden. */
4494 if (parser->local_variables_forbidden_p
4495 && local_variable_p (decl))
4496 {
4497 /* It might be that we only found DECL because we are
4498 trying to be generous with pre-ISO scoping rules.
4499 For example, consider:
4500
4501 int i;
4502 void g() {
4503 for (int i = 0; i < 10; ++i) {}
4504 extern void f(int j = i);
4505 }
4506
4507 Here, name look up will originally find the out
4508 of scope `i'. We need to issue a warning message,
4509 but then use the global `i'. */
4510 decl = check_for_out_of_scope_variable (decl);
4511 if (local_variable_p (decl))
4512 {
4513 error_at (id_expr_token->location,
4514 "local variable %qD may not appear in this context",
4515 decl);
4516 return error_mark_node;
4517 }
4518 }
4519 }
4520
4521 decl = (finish_id_expression
4522 (id_expression, decl, parser->scope,
4523 idk,
4524 parser->integral_constant_expression_p,
4525 parser->allow_non_integral_constant_expression_p,
4526 &parser->non_integral_constant_expression_p,
4527 template_p, done, address_p,
4528 template_arg_p,
4529 &error_msg,
4530 id_expr_token->location));
4531 if (error_msg)
4532 cp_parser_error (parser, error_msg);
4533 return decl;
4534 }
4535
4536 /* Anything else is an error. */
4537 default:
4538 cp_parser_error (parser, "expected primary-expression");
4539 return error_mark_node;
4540 }
4541 }
4542
4543 static inline tree
4544 cp_parser_primary_expression (cp_parser *parser,
4545 bool address_p,
4546 bool cast_p,
4547 bool template_arg_p,
4548 cp_id_kind *idk)
4549 {
4550 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4551 /*decltype*/false, idk);
4552 }
4553
4554 /* Parse an id-expression.
4555
4556 id-expression:
4557 unqualified-id
4558 qualified-id
4559
4560 qualified-id:
4561 :: [opt] nested-name-specifier template [opt] unqualified-id
4562 :: identifier
4563 :: operator-function-id
4564 :: template-id
4565
4566 Return a representation of the unqualified portion of the
4567 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4568 a `::' or nested-name-specifier.
4569
4570 Often, if the id-expression was a qualified-id, the caller will
4571 want to make a SCOPE_REF to represent the qualified-id. This
4572 function does not do this in order to avoid wastefully creating
4573 SCOPE_REFs when they are not required.
4574
4575 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4576 `template' keyword.
4577
4578 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4579 uninstantiated templates.
4580
4581 If *TEMPLATE_P is non-NULL, it is set to true iff the
4582 `template' keyword is used to explicitly indicate that the entity
4583 named is a template.
4584
4585 If DECLARATOR_P is true, the id-expression is appearing as part of
4586 a declarator, rather than as part of an expression. */
4587
4588 static tree
4589 cp_parser_id_expression (cp_parser *parser,
4590 bool template_keyword_p,
4591 bool check_dependency_p,
4592 bool *template_p,
4593 bool declarator_p,
4594 bool optional_p)
4595 {
4596 bool global_scope_p;
4597 bool nested_name_specifier_p;
4598
4599 /* Assume the `template' keyword was not used. */
4600 if (template_p)
4601 *template_p = template_keyword_p;
4602
4603 /* Look for the optional `::' operator. */
4604 global_scope_p
4605 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4606 != NULL_TREE);
4607 /* Look for the optional nested-name-specifier. */
4608 nested_name_specifier_p
4609 = (cp_parser_nested_name_specifier_opt (parser,
4610 /*typename_keyword_p=*/false,
4611 check_dependency_p,
4612 /*type_p=*/false,
4613 declarator_p)
4614 != NULL_TREE);
4615 /* If there is a nested-name-specifier, then we are looking at
4616 the first qualified-id production. */
4617 if (nested_name_specifier_p)
4618 {
4619 tree saved_scope;
4620 tree saved_object_scope;
4621 tree saved_qualifying_scope;
4622 tree unqualified_id;
4623 bool is_template;
4624
4625 /* See if the next token is the `template' keyword. */
4626 if (!template_p)
4627 template_p = &is_template;
4628 *template_p = cp_parser_optional_template_keyword (parser);
4629 /* Name lookup we do during the processing of the
4630 unqualified-id might obliterate SCOPE. */
4631 saved_scope = parser->scope;
4632 saved_object_scope = parser->object_scope;
4633 saved_qualifying_scope = parser->qualifying_scope;
4634 /* Process the final unqualified-id. */
4635 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4636 check_dependency_p,
4637 declarator_p,
4638 /*optional_p=*/false);
4639 /* Restore the SAVED_SCOPE for our caller. */
4640 parser->scope = saved_scope;
4641 parser->object_scope = saved_object_scope;
4642 parser->qualifying_scope = saved_qualifying_scope;
4643
4644 return unqualified_id;
4645 }
4646 /* Otherwise, if we are in global scope, then we are looking at one
4647 of the other qualified-id productions. */
4648 else if (global_scope_p)
4649 {
4650 cp_token *token;
4651 tree id;
4652
4653 /* Peek at the next token. */
4654 token = cp_lexer_peek_token (parser->lexer);
4655
4656 /* If it's an identifier, and the next token is not a "<", then
4657 we can avoid the template-id case. This is an optimization
4658 for this common case. */
4659 if (token->type == CPP_NAME
4660 && !cp_parser_nth_token_starts_template_argument_list_p
4661 (parser, 2))
4662 return cp_parser_identifier (parser);
4663
4664 cp_parser_parse_tentatively (parser);
4665 /* Try a template-id. */
4666 id = cp_parser_template_id (parser,
4667 /*template_keyword_p=*/false,
4668 /*check_dependency_p=*/true,
4669 none_type,
4670 declarator_p);
4671 /* If that worked, we're done. */
4672 if (cp_parser_parse_definitely (parser))
4673 return id;
4674
4675 /* Peek at the next token. (Changes in the token buffer may
4676 have invalidated the pointer obtained above.) */
4677 token = cp_lexer_peek_token (parser->lexer);
4678
4679 switch (token->type)
4680 {
4681 case CPP_NAME:
4682 return cp_parser_identifier (parser);
4683
4684 case CPP_KEYWORD:
4685 if (token->keyword == RID_OPERATOR)
4686 return cp_parser_operator_function_id (parser);
4687 /* Fall through. */
4688
4689 default:
4690 cp_parser_error (parser, "expected id-expression");
4691 return error_mark_node;
4692 }
4693 }
4694 else
4695 return cp_parser_unqualified_id (parser, template_keyword_p,
4696 /*check_dependency_p=*/true,
4697 declarator_p,
4698 optional_p);
4699 }
4700
4701 /* Parse an unqualified-id.
4702
4703 unqualified-id:
4704 identifier
4705 operator-function-id
4706 conversion-function-id
4707 ~ class-name
4708 template-id
4709
4710 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4711 keyword, in a construct like `A::template ...'.
4712
4713 Returns a representation of unqualified-id. For the `identifier'
4714 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4715 production a BIT_NOT_EXPR is returned; the operand of the
4716 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4717 other productions, see the documentation accompanying the
4718 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4719 names are looked up in uninstantiated templates. If DECLARATOR_P
4720 is true, the unqualified-id is appearing as part of a declarator,
4721 rather than as part of an expression. */
4722
4723 static tree
4724 cp_parser_unqualified_id (cp_parser* parser,
4725 bool template_keyword_p,
4726 bool check_dependency_p,
4727 bool declarator_p,
4728 bool optional_p)
4729 {
4730 cp_token *token;
4731
4732 /* Peek at the next token. */
4733 token = cp_lexer_peek_token (parser->lexer);
4734
4735 switch (token->type)
4736 {
4737 case CPP_NAME:
4738 {
4739 tree id;
4740
4741 /* We don't know yet whether or not this will be a
4742 template-id. */
4743 cp_parser_parse_tentatively (parser);
4744 /* Try a template-id. */
4745 id = cp_parser_template_id (parser, template_keyword_p,
4746 check_dependency_p,
4747 none_type,
4748 declarator_p);
4749 /* If it worked, we're done. */
4750 if (cp_parser_parse_definitely (parser))
4751 return id;
4752 /* Otherwise, it's an ordinary identifier. */
4753 return cp_parser_identifier (parser);
4754 }
4755
4756 case CPP_TEMPLATE_ID:
4757 return cp_parser_template_id (parser, template_keyword_p,
4758 check_dependency_p,
4759 none_type,
4760 declarator_p);
4761
4762 case CPP_COMPL:
4763 {
4764 tree type_decl;
4765 tree qualifying_scope;
4766 tree object_scope;
4767 tree scope;
4768 bool done;
4769
4770 /* Consume the `~' token. */
4771 cp_lexer_consume_token (parser->lexer);
4772 /* Parse the class-name. The standard, as written, seems to
4773 say that:
4774
4775 template <typename T> struct S { ~S (); };
4776 template <typename T> S<T>::~S() {}
4777
4778 is invalid, since `~' must be followed by a class-name, but
4779 `S<T>' is dependent, and so not known to be a class.
4780 That's not right; we need to look in uninstantiated
4781 templates. A further complication arises from:
4782
4783 template <typename T> void f(T t) {
4784 t.T::~T();
4785 }
4786
4787 Here, it is not possible to look up `T' in the scope of `T'
4788 itself. We must look in both the current scope, and the
4789 scope of the containing complete expression.
4790
4791 Yet another issue is:
4792
4793 struct S {
4794 int S;
4795 ~S();
4796 };
4797
4798 S::~S() {}
4799
4800 The standard does not seem to say that the `S' in `~S'
4801 should refer to the type `S' and not the data member
4802 `S::S'. */
4803
4804 /* DR 244 says that we look up the name after the "~" in the
4805 same scope as we looked up the qualifying name. That idea
4806 isn't fully worked out; it's more complicated than that. */
4807 scope = parser->scope;
4808 object_scope = parser->object_scope;
4809 qualifying_scope = parser->qualifying_scope;
4810
4811 /* Check for invalid scopes. */
4812 if (scope == error_mark_node)
4813 {
4814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4815 cp_lexer_consume_token (parser->lexer);
4816 return error_mark_node;
4817 }
4818 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4819 {
4820 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4821 error_at (token->location,
4822 "scope %qT before %<~%> is not a class-name",
4823 scope);
4824 cp_parser_simulate_error (parser);
4825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4826 cp_lexer_consume_token (parser->lexer);
4827 return error_mark_node;
4828 }
4829 gcc_assert (!scope || TYPE_P (scope));
4830
4831 /* If the name is of the form "X::~X" it's OK even if X is a
4832 typedef. */
4833 token = cp_lexer_peek_token (parser->lexer);
4834 if (scope
4835 && token->type == CPP_NAME
4836 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4837 != CPP_LESS)
4838 && (token->u.value == TYPE_IDENTIFIER (scope)
4839 || (CLASS_TYPE_P (scope)
4840 && constructor_name_p (token->u.value, scope))))
4841 {
4842 cp_lexer_consume_token (parser->lexer);
4843 return build_nt (BIT_NOT_EXPR, scope);
4844 }
4845
4846 /* ~auto means the destructor of whatever the object is. */
4847 if (cp_parser_is_keyword (token, RID_AUTO))
4848 {
4849 if (cxx_dialect < cxx1y)
4850 pedwarn (input_location, 0,
4851 "%<~auto%> only available with "
4852 "-std=c++1y or -std=gnu++1y");
4853 cp_lexer_consume_token (parser->lexer);
4854 return build_nt (BIT_NOT_EXPR, make_auto ());
4855 }
4856
4857 /* If there was an explicit qualification (S::~T), first look
4858 in the scope given by the qualification (i.e., S).
4859
4860 Note: in the calls to cp_parser_class_name below we pass
4861 typename_type so that lookup finds the injected-class-name
4862 rather than the constructor. */
4863 done = false;
4864 type_decl = NULL_TREE;
4865 if (scope)
4866 {
4867 cp_parser_parse_tentatively (parser);
4868 type_decl = cp_parser_class_name (parser,
4869 /*typename_keyword_p=*/false,
4870 /*template_keyword_p=*/false,
4871 typename_type,
4872 /*check_dependency=*/false,
4873 /*class_head_p=*/false,
4874 declarator_p);
4875 if (cp_parser_parse_definitely (parser))
4876 done = true;
4877 }
4878 /* In "N::S::~S", look in "N" as well. */
4879 if (!done && scope && qualifying_scope)
4880 {
4881 cp_parser_parse_tentatively (parser);
4882 parser->scope = qualifying_scope;
4883 parser->object_scope = NULL_TREE;
4884 parser->qualifying_scope = NULL_TREE;
4885 type_decl
4886 = cp_parser_class_name (parser,
4887 /*typename_keyword_p=*/false,
4888 /*template_keyword_p=*/false,
4889 typename_type,
4890 /*check_dependency=*/false,
4891 /*class_head_p=*/false,
4892 declarator_p);
4893 if (cp_parser_parse_definitely (parser))
4894 done = true;
4895 }
4896 /* In "p->S::~T", look in the scope given by "*p" as well. */
4897 else if (!done && object_scope)
4898 {
4899 cp_parser_parse_tentatively (parser);
4900 parser->scope = object_scope;
4901 parser->object_scope = NULL_TREE;
4902 parser->qualifying_scope = NULL_TREE;
4903 type_decl
4904 = cp_parser_class_name (parser,
4905 /*typename_keyword_p=*/false,
4906 /*template_keyword_p=*/false,
4907 typename_type,
4908 /*check_dependency=*/false,
4909 /*class_head_p=*/false,
4910 declarator_p);
4911 if (cp_parser_parse_definitely (parser))
4912 done = true;
4913 }
4914 /* Look in the surrounding context. */
4915 if (!done)
4916 {
4917 parser->scope = NULL_TREE;
4918 parser->object_scope = NULL_TREE;
4919 parser->qualifying_scope = NULL_TREE;
4920 if (processing_template_decl)
4921 cp_parser_parse_tentatively (parser);
4922 type_decl
4923 = cp_parser_class_name (parser,
4924 /*typename_keyword_p=*/false,
4925 /*template_keyword_p=*/false,
4926 typename_type,
4927 /*check_dependency=*/false,
4928 /*class_head_p=*/false,
4929 declarator_p);
4930 if (processing_template_decl
4931 && ! cp_parser_parse_definitely (parser))
4932 {
4933 /* We couldn't find a type with this name, so just accept
4934 it and check for a match at instantiation time. */
4935 type_decl = cp_parser_identifier (parser);
4936 if (type_decl != error_mark_node)
4937 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4938 return type_decl;
4939 }
4940 }
4941 /* If an error occurred, assume that the name of the
4942 destructor is the same as the name of the qualifying
4943 class. That allows us to keep parsing after running
4944 into ill-formed destructor names. */
4945 if (type_decl == error_mark_node && scope)
4946 return build_nt (BIT_NOT_EXPR, scope);
4947 else if (type_decl == error_mark_node)
4948 return error_mark_node;
4949
4950 /* Check that destructor name and scope match. */
4951 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4952 {
4953 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4954 error_at (token->location,
4955 "declaration of %<~%T%> as member of %qT",
4956 type_decl, scope);
4957 cp_parser_simulate_error (parser);
4958 return error_mark_node;
4959 }
4960
4961 /* [class.dtor]
4962
4963 A typedef-name that names a class shall not be used as the
4964 identifier in the declarator for a destructor declaration. */
4965 if (declarator_p
4966 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4967 && !DECL_SELF_REFERENCE_P (type_decl)
4968 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4969 error_at (token->location,
4970 "typedef-name %qD used as destructor declarator",
4971 type_decl);
4972
4973 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4974 }
4975
4976 case CPP_KEYWORD:
4977 if (token->keyword == RID_OPERATOR)
4978 {
4979 tree id;
4980
4981 /* This could be a template-id, so we try that first. */
4982 cp_parser_parse_tentatively (parser);
4983 /* Try a template-id. */
4984 id = cp_parser_template_id (parser, template_keyword_p,
4985 /*check_dependency_p=*/true,
4986 none_type,
4987 declarator_p);
4988 /* If that worked, we're done. */
4989 if (cp_parser_parse_definitely (parser))
4990 return id;
4991 /* We still don't know whether we're looking at an
4992 operator-function-id or a conversion-function-id. */
4993 cp_parser_parse_tentatively (parser);
4994 /* Try an operator-function-id. */
4995 id = cp_parser_operator_function_id (parser);
4996 /* If that didn't work, try a conversion-function-id. */
4997 if (!cp_parser_parse_definitely (parser))
4998 id = cp_parser_conversion_function_id (parser);
4999 else if (UDLIT_OPER_P (id))
5000 {
5001 /* 17.6.3.3.5 */
5002 const char *name = UDLIT_OP_SUFFIX (id);
5003 if (name[0] != '_' && !in_system_header)
5004 warning (0, "literal operator suffixes not preceded by %<_%>"
5005 " are reserved for future standardization");
5006 }
5007
5008 return id;
5009 }
5010 /* Fall through. */
5011
5012 default:
5013 if (optional_p)
5014 return NULL_TREE;
5015 cp_parser_error (parser, "expected unqualified-id");
5016 return error_mark_node;
5017 }
5018 }
5019
5020 /* Parse an (optional) nested-name-specifier.
5021
5022 nested-name-specifier: [C++98]
5023 class-or-namespace-name :: nested-name-specifier [opt]
5024 class-or-namespace-name :: template nested-name-specifier [opt]
5025
5026 nested-name-specifier: [C++0x]
5027 type-name ::
5028 namespace-name ::
5029 nested-name-specifier identifier ::
5030 nested-name-specifier template [opt] simple-template-id ::
5031
5032 PARSER->SCOPE should be set appropriately before this function is
5033 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5034 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5035 in name lookups.
5036
5037 Sets PARSER->SCOPE to the class (TYPE) or namespace
5038 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5039 it unchanged if there is no nested-name-specifier. Returns the new
5040 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5041
5042 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5043 part of a declaration and/or decl-specifier. */
5044
5045 static tree
5046 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5047 bool typename_keyword_p,
5048 bool check_dependency_p,
5049 bool type_p,
5050 bool is_declaration)
5051 {
5052 bool success = false;
5053 cp_token_position start = 0;
5054 cp_token *token;
5055
5056 /* Remember where the nested-name-specifier starts. */
5057 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5058 {
5059 start = cp_lexer_token_position (parser->lexer, false);
5060 push_deferring_access_checks (dk_deferred);
5061 }
5062
5063 while (true)
5064 {
5065 tree new_scope;
5066 tree old_scope;
5067 tree saved_qualifying_scope;
5068 bool template_keyword_p;
5069
5070 /* Spot cases that cannot be the beginning of a
5071 nested-name-specifier. */
5072 token = cp_lexer_peek_token (parser->lexer);
5073
5074 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5075 the already parsed nested-name-specifier. */
5076 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5077 {
5078 /* Grab the nested-name-specifier and continue the loop. */
5079 cp_parser_pre_parsed_nested_name_specifier (parser);
5080 /* If we originally encountered this nested-name-specifier
5081 with IS_DECLARATION set to false, we will not have
5082 resolved TYPENAME_TYPEs, so we must do so here. */
5083 if (is_declaration
5084 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5085 {
5086 new_scope = resolve_typename_type (parser->scope,
5087 /*only_current_p=*/false);
5088 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5089 parser->scope = new_scope;
5090 }
5091 success = true;
5092 continue;
5093 }
5094
5095 /* Spot cases that cannot be the beginning of a
5096 nested-name-specifier. On the second and subsequent times
5097 through the loop, we look for the `template' keyword. */
5098 if (success && token->keyword == RID_TEMPLATE)
5099 ;
5100 /* A template-id can start a nested-name-specifier. */
5101 else if (token->type == CPP_TEMPLATE_ID)
5102 ;
5103 /* DR 743: decltype can be used in a nested-name-specifier. */
5104 else if (token_is_decltype (token))
5105 ;
5106 else
5107 {
5108 /* If the next token is not an identifier, then it is
5109 definitely not a type-name or namespace-name. */
5110 if (token->type != CPP_NAME)
5111 break;
5112 /* If the following token is neither a `<' (to begin a
5113 template-id), nor a `::', then we are not looking at a
5114 nested-name-specifier. */
5115 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5116
5117 if (token->type == CPP_COLON
5118 && parser->colon_corrects_to_scope_p
5119 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5120 {
5121 error_at (token->location,
5122 "found %<:%> in nested-name-specifier, expected %<::%>");
5123 token->type = CPP_SCOPE;
5124 }
5125
5126 if (token->type != CPP_SCOPE
5127 && !cp_parser_nth_token_starts_template_argument_list_p
5128 (parser, 2))
5129 break;
5130 }
5131
5132 /* The nested-name-specifier is optional, so we parse
5133 tentatively. */
5134 cp_parser_parse_tentatively (parser);
5135
5136 /* Look for the optional `template' keyword, if this isn't the
5137 first time through the loop. */
5138 if (success)
5139 template_keyword_p = cp_parser_optional_template_keyword (parser);
5140 else
5141 template_keyword_p = false;
5142
5143 /* Save the old scope since the name lookup we are about to do
5144 might destroy it. */
5145 old_scope = parser->scope;
5146 saved_qualifying_scope = parser->qualifying_scope;
5147 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5148 look up names in "X<T>::I" in order to determine that "Y" is
5149 a template. So, if we have a typename at this point, we make
5150 an effort to look through it. */
5151 if (is_declaration
5152 && !typename_keyword_p
5153 && parser->scope
5154 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5155 parser->scope = resolve_typename_type (parser->scope,
5156 /*only_current_p=*/false);
5157 /* Parse the qualifying entity. */
5158 new_scope
5159 = cp_parser_qualifying_entity (parser,
5160 typename_keyword_p,
5161 template_keyword_p,
5162 check_dependency_p,
5163 type_p,
5164 is_declaration);
5165 /* Look for the `::' token. */
5166 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5167
5168 /* If we found what we wanted, we keep going; otherwise, we're
5169 done. */
5170 if (!cp_parser_parse_definitely (parser))
5171 {
5172 bool error_p = false;
5173
5174 /* Restore the OLD_SCOPE since it was valid before the
5175 failed attempt at finding the last
5176 class-or-namespace-name. */
5177 parser->scope = old_scope;
5178 parser->qualifying_scope = saved_qualifying_scope;
5179
5180 /* If the next token is a decltype, and the one after that is a
5181 `::', then the decltype has failed to resolve to a class or
5182 enumeration type. Give this error even when parsing
5183 tentatively since it can't possibly be valid--and we're going
5184 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5185 won't get another chance.*/
5186 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5187 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5188 == CPP_SCOPE))
5189 {
5190 token = cp_lexer_consume_token (parser->lexer);
5191 error_at (token->location, "decltype evaluates to %qT, "
5192 "which is not a class or enumeration type",
5193 token->u.value);
5194 parser->scope = error_mark_node;
5195 error_p = true;
5196 /* As below. */
5197 success = true;
5198 cp_lexer_consume_token (parser->lexer);
5199 }
5200
5201 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5202 break;
5203 /* If the next token is an identifier, and the one after
5204 that is a `::', then any valid interpretation would have
5205 found a class-or-namespace-name. */
5206 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5207 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5208 == CPP_SCOPE)
5209 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5210 != CPP_COMPL))
5211 {
5212 token = cp_lexer_consume_token (parser->lexer);
5213 if (!error_p)
5214 {
5215 if (!token->ambiguous_p)
5216 {
5217 tree decl;
5218 tree ambiguous_decls;
5219
5220 decl = cp_parser_lookup_name (parser, token->u.value,
5221 none_type,
5222 /*is_template=*/false,
5223 /*is_namespace=*/false,
5224 /*check_dependency=*/true,
5225 &ambiguous_decls,
5226 token->location);
5227 if (TREE_CODE (decl) == TEMPLATE_DECL)
5228 error_at (token->location,
5229 "%qD used without template parameters",
5230 decl);
5231 else if (ambiguous_decls)
5232 {
5233 error_at (token->location,
5234 "reference to %qD is ambiguous",
5235 token->u.value);
5236 print_candidates (ambiguous_decls);
5237 decl = error_mark_node;
5238 }
5239 else
5240 {
5241 if (cxx_dialect != cxx98)
5242 cp_parser_name_lookup_error
5243 (parser, token->u.value, decl, NLE_NOT_CXX98,
5244 token->location);
5245 else
5246 cp_parser_name_lookup_error
5247 (parser, token->u.value, decl, NLE_CXX98,
5248 token->location);
5249 }
5250 }
5251 parser->scope = error_mark_node;
5252 error_p = true;
5253 /* Treat this as a successful nested-name-specifier
5254 due to:
5255
5256 [basic.lookup.qual]
5257
5258 If the name found is not a class-name (clause
5259 _class_) or namespace-name (_namespace.def_), the
5260 program is ill-formed. */
5261 success = true;
5262 }
5263 cp_lexer_consume_token (parser->lexer);
5264 }
5265 break;
5266 }
5267 /* We've found one valid nested-name-specifier. */
5268 success = true;
5269 /* Name lookup always gives us a DECL. */
5270 if (TREE_CODE (new_scope) == TYPE_DECL)
5271 new_scope = TREE_TYPE (new_scope);
5272 /* Uses of "template" must be followed by actual templates. */
5273 if (template_keyword_p
5274 && !(CLASS_TYPE_P (new_scope)
5275 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5276 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5277 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5278 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5279 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5280 == TEMPLATE_ID_EXPR)))
5281 permerror (input_location, TYPE_P (new_scope)
5282 ? G_("%qT is not a template")
5283 : G_("%qD is not a template"),
5284 new_scope);
5285 /* If it is a class scope, try to complete it; we are about to
5286 be looking up names inside the class. */
5287 if (TYPE_P (new_scope)
5288 /* Since checking types for dependency can be expensive,
5289 avoid doing it if the type is already complete. */
5290 && !COMPLETE_TYPE_P (new_scope)
5291 /* Do not try to complete dependent types. */
5292 && !dependent_type_p (new_scope))
5293 {
5294 new_scope = complete_type (new_scope);
5295 /* If it is a typedef to current class, use the current
5296 class instead, as the typedef won't have any names inside
5297 it yet. */
5298 if (!COMPLETE_TYPE_P (new_scope)
5299 && currently_open_class (new_scope))
5300 new_scope = TYPE_MAIN_VARIANT (new_scope);
5301 }
5302 /* Make sure we look in the right scope the next time through
5303 the loop. */
5304 parser->scope = new_scope;
5305 }
5306
5307 /* If parsing tentatively, replace the sequence of tokens that makes
5308 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5309 token. That way, should we re-parse the token stream, we will
5310 not have to repeat the effort required to do the parse, nor will
5311 we issue duplicate error messages. */
5312 if (success && start)
5313 {
5314 cp_token *token;
5315
5316 token = cp_lexer_token_at (parser->lexer, start);
5317 /* Reset the contents of the START token. */
5318 token->type = CPP_NESTED_NAME_SPECIFIER;
5319 /* Retrieve any deferred checks. Do not pop this access checks yet
5320 so the memory will not be reclaimed during token replacing below. */
5321 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5322 token->u.tree_check_value->value = parser->scope;
5323 token->u.tree_check_value->checks = get_deferred_access_checks ();
5324 token->u.tree_check_value->qualifying_scope =
5325 parser->qualifying_scope;
5326 token->keyword = RID_MAX;
5327
5328 /* Purge all subsequent tokens. */
5329 cp_lexer_purge_tokens_after (parser->lexer, start);
5330 }
5331
5332 if (start)
5333 pop_to_parent_deferring_access_checks ();
5334
5335 return success ? parser->scope : NULL_TREE;
5336 }
5337
5338 /* Parse a nested-name-specifier. See
5339 cp_parser_nested_name_specifier_opt for details. This function
5340 behaves identically, except that it will an issue an error if no
5341 nested-name-specifier is present. */
5342
5343 static tree
5344 cp_parser_nested_name_specifier (cp_parser *parser,
5345 bool typename_keyword_p,
5346 bool check_dependency_p,
5347 bool type_p,
5348 bool is_declaration)
5349 {
5350 tree scope;
5351
5352 /* Look for the nested-name-specifier. */
5353 scope = cp_parser_nested_name_specifier_opt (parser,
5354 typename_keyword_p,
5355 check_dependency_p,
5356 type_p,
5357 is_declaration);
5358 /* If it was not present, issue an error message. */
5359 if (!scope)
5360 {
5361 cp_parser_error (parser, "expected nested-name-specifier");
5362 parser->scope = NULL_TREE;
5363 }
5364
5365 return scope;
5366 }
5367
5368 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5369 this is either a class-name or a namespace-name (which corresponds
5370 to the class-or-namespace-name production in the grammar). For
5371 C++0x, it can also be a type-name that refers to an enumeration
5372 type or a simple-template-id.
5373
5374 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5375 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5376 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5377 TYPE_P is TRUE iff the next name should be taken as a class-name,
5378 even the same name is declared to be another entity in the same
5379 scope.
5380
5381 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5382 specified by the class-or-namespace-name. If neither is found the
5383 ERROR_MARK_NODE is returned. */
5384
5385 static tree
5386 cp_parser_qualifying_entity (cp_parser *parser,
5387 bool typename_keyword_p,
5388 bool template_keyword_p,
5389 bool check_dependency_p,
5390 bool type_p,
5391 bool is_declaration)
5392 {
5393 tree saved_scope;
5394 tree saved_qualifying_scope;
5395 tree saved_object_scope;
5396 tree scope;
5397 bool only_class_p;
5398 bool successful_parse_p;
5399
5400 /* DR 743: decltype can appear in a nested-name-specifier. */
5401 if (cp_lexer_next_token_is_decltype (parser->lexer))
5402 {
5403 scope = cp_parser_decltype (parser);
5404 if (TREE_CODE (scope) != ENUMERAL_TYPE
5405 && !MAYBE_CLASS_TYPE_P (scope))
5406 {
5407 cp_parser_simulate_error (parser);
5408 return error_mark_node;
5409 }
5410 if (TYPE_NAME (scope))
5411 scope = TYPE_NAME (scope);
5412 return scope;
5413 }
5414
5415 /* Before we try to parse the class-name, we must save away the
5416 current PARSER->SCOPE since cp_parser_class_name will destroy
5417 it. */
5418 saved_scope = parser->scope;
5419 saved_qualifying_scope = parser->qualifying_scope;
5420 saved_object_scope = parser->object_scope;
5421 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5422 there is no need to look for a namespace-name. */
5423 only_class_p = template_keyword_p
5424 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5425 if (!only_class_p)
5426 cp_parser_parse_tentatively (parser);
5427 scope = cp_parser_class_name (parser,
5428 typename_keyword_p,
5429 template_keyword_p,
5430 type_p ? class_type : none_type,
5431 check_dependency_p,
5432 /*class_head_p=*/false,
5433 is_declaration);
5434 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5435 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5436 if (!only_class_p
5437 && cxx_dialect != cxx98
5438 && !successful_parse_p)
5439 {
5440 /* Restore the saved scope. */
5441 parser->scope = saved_scope;
5442 parser->qualifying_scope = saved_qualifying_scope;
5443 parser->object_scope = saved_object_scope;
5444
5445 /* Parse tentatively. */
5446 cp_parser_parse_tentatively (parser);
5447
5448 /* Parse a type-name */
5449 scope = cp_parser_type_name (parser);
5450
5451 /* "If the name found does not designate a namespace or a class,
5452 enumeration, or dependent type, the program is ill-formed."
5453
5454 We cover classes and dependent types above and namespaces below,
5455 so this code is only looking for enums. */
5456 if (!scope || TREE_CODE (scope) != TYPE_DECL
5457 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5458 cp_parser_simulate_error (parser);
5459
5460 successful_parse_p = cp_parser_parse_definitely (parser);
5461 }
5462 /* If that didn't work, try for a namespace-name. */
5463 if (!only_class_p && !successful_parse_p)
5464 {
5465 /* Restore the saved scope. */
5466 parser->scope = saved_scope;
5467 parser->qualifying_scope = saved_qualifying_scope;
5468 parser->object_scope = saved_object_scope;
5469 /* If we are not looking at an identifier followed by the scope
5470 resolution operator, then this is not part of a
5471 nested-name-specifier. (Note that this function is only used
5472 to parse the components of a nested-name-specifier.) */
5473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5474 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5475 return error_mark_node;
5476 scope = cp_parser_namespace_name (parser);
5477 }
5478
5479 return scope;
5480 }
5481
5482 /* Parse a postfix-expression.
5483
5484 postfix-expression:
5485 primary-expression
5486 postfix-expression [ expression ]
5487 postfix-expression ( expression-list [opt] )
5488 simple-type-specifier ( expression-list [opt] )
5489 typename :: [opt] nested-name-specifier identifier
5490 ( expression-list [opt] )
5491 typename :: [opt] nested-name-specifier template [opt] template-id
5492 ( expression-list [opt] )
5493 postfix-expression . template [opt] id-expression
5494 postfix-expression -> template [opt] id-expression
5495 postfix-expression . pseudo-destructor-name
5496 postfix-expression -> pseudo-destructor-name
5497 postfix-expression ++
5498 postfix-expression --
5499 dynamic_cast < type-id > ( expression )
5500 static_cast < type-id > ( expression )
5501 reinterpret_cast < type-id > ( expression )
5502 const_cast < type-id > ( expression )
5503 typeid ( expression )
5504 typeid ( type-id )
5505
5506 GNU Extension:
5507
5508 postfix-expression:
5509 ( type-id ) { initializer-list , [opt] }
5510
5511 This extension is a GNU version of the C99 compound-literal
5512 construct. (The C99 grammar uses `type-name' instead of `type-id',
5513 but they are essentially the same concept.)
5514
5515 If ADDRESS_P is true, the postfix expression is the operand of the
5516 `&' operator. CAST_P is true if this expression is the target of a
5517 cast.
5518
5519 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5520 class member access expressions [expr.ref].
5521
5522 Returns a representation of the expression. */
5523
5524 static tree
5525 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5526 bool member_access_only_p, bool decltype_p,
5527 cp_id_kind * pidk_return)
5528 {
5529 cp_token *token;
5530 enum rid keyword;
5531 cp_id_kind idk = CP_ID_KIND_NONE;
5532 tree postfix_expression = NULL_TREE;
5533 bool is_member_access = false;
5534
5535 /* Peek at the next token. */
5536 token = cp_lexer_peek_token (parser->lexer);
5537 /* Some of the productions are determined by keywords. */
5538 keyword = token->keyword;
5539 switch (keyword)
5540 {
5541 case RID_DYNCAST:
5542 case RID_STATCAST:
5543 case RID_REINTCAST:
5544 case RID_CONSTCAST:
5545 {
5546 tree type;
5547 tree expression;
5548 const char *saved_message;
5549
5550 /* All of these can be handled in the same way from the point
5551 of view of parsing. Begin by consuming the token
5552 identifying the cast. */
5553 cp_lexer_consume_token (parser->lexer);
5554
5555 /* New types cannot be defined in the cast. */
5556 saved_message = parser->type_definition_forbidden_message;
5557 parser->type_definition_forbidden_message
5558 = G_("types may not be defined in casts");
5559
5560 /* Look for the opening `<'. */
5561 cp_parser_require (parser, CPP_LESS, RT_LESS);
5562 /* Parse the type to which we are casting. */
5563 type = cp_parser_type_id (parser);
5564 /* Look for the closing `>'. */
5565 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5566 /* Restore the old message. */
5567 parser->type_definition_forbidden_message = saved_message;
5568
5569 /* And the expression which is being cast. */
5570 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5571 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5572 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5573
5574 /* Only type conversions to integral or enumeration types
5575 can be used in constant-expressions. */
5576 if (!cast_valid_in_integral_constant_expression_p (type)
5577 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5578 return error_mark_node;
5579
5580 switch (keyword)
5581 {
5582 case RID_DYNCAST:
5583 postfix_expression
5584 = build_dynamic_cast (type, expression, tf_warning_or_error);
5585 break;
5586 case RID_STATCAST:
5587 postfix_expression
5588 = build_static_cast (type, expression, tf_warning_or_error);
5589 break;
5590 case RID_REINTCAST:
5591 postfix_expression
5592 = build_reinterpret_cast (type, expression,
5593 tf_warning_or_error);
5594 break;
5595 case RID_CONSTCAST:
5596 postfix_expression
5597 = build_const_cast (type, expression, tf_warning_or_error);
5598 break;
5599 default:
5600 gcc_unreachable ();
5601 }
5602 }
5603 break;
5604
5605 case RID_TYPEID:
5606 {
5607 tree type;
5608 const char *saved_message;
5609 bool saved_in_type_id_in_expr_p;
5610
5611 /* Consume the `typeid' token. */
5612 cp_lexer_consume_token (parser->lexer);
5613 /* Look for the `(' token. */
5614 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5615 /* Types cannot be defined in a `typeid' expression. */
5616 saved_message = parser->type_definition_forbidden_message;
5617 parser->type_definition_forbidden_message
5618 = G_("types may not be defined in a %<typeid%> expression");
5619 /* We can't be sure yet whether we're looking at a type-id or an
5620 expression. */
5621 cp_parser_parse_tentatively (parser);
5622 /* Try a type-id first. */
5623 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5624 parser->in_type_id_in_expr_p = true;
5625 type = cp_parser_type_id (parser);
5626 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5627 /* Look for the `)' token. Otherwise, we can't be sure that
5628 we're not looking at an expression: consider `typeid (int
5629 (3))', for example. */
5630 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5631 /* If all went well, simply lookup the type-id. */
5632 if (cp_parser_parse_definitely (parser))
5633 postfix_expression = get_typeid (type, tf_warning_or_error);
5634 /* Otherwise, fall back to the expression variant. */
5635 else
5636 {
5637 tree expression;
5638
5639 /* Look for an expression. */
5640 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5641 /* Compute its typeid. */
5642 postfix_expression = build_typeid (expression, tf_warning_or_error);
5643 /* Look for the `)' token. */
5644 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5645 }
5646 /* Restore the saved message. */
5647 parser->type_definition_forbidden_message = saved_message;
5648 /* `typeid' may not appear in an integral constant expression. */
5649 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5650 return error_mark_node;
5651 }
5652 break;
5653
5654 case RID_TYPENAME:
5655 {
5656 tree type;
5657 /* The syntax permitted here is the same permitted for an
5658 elaborated-type-specifier. */
5659 type = cp_parser_elaborated_type_specifier (parser,
5660 /*is_friend=*/false,
5661 /*is_declaration=*/false);
5662 postfix_expression = cp_parser_functional_cast (parser, type);
5663 }
5664 break;
5665
5666 case RID_BUILTIN_SHUFFLE:
5667 {
5668 vec<tree, va_gc> *vec;
5669 unsigned int i;
5670 tree p;
5671 location_t loc = token->location;
5672
5673 cp_lexer_consume_token (parser->lexer);
5674 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5675 /*cast_p=*/false, /*allow_expansion_p=*/true,
5676 /*non_constant_p=*/NULL);
5677 if (vec == NULL)
5678 return error_mark_node;
5679
5680 FOR_EACH_VEC_ELT (*vec, i, p)
5681 mark_exp_read (p);
5682
5683 if (vec->length () == 2)
5684 return c_build_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1]);
5685 else if (vec->length () == 3)
5686 return c_build_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2]);
5687 else
5688 {
5689 error_at (loc, "wrong number of arguments to "
5690 "%<__builtin_shuffle%>");
5691 return error_mark_node;
5692 }
5693 break;
5694 }
5695
5696 default:
5697 {
5698 tree type;
5699
5700 /* If the next thing is a simple-type-specifier, we may be
5701 looking at a functional cast. We could also be looking at
5702 an id-expression. So, we try the functional cast, and if
5703 that doesn't work we fall back to the primary-expression. */
5704 cp_parser_parse_tentatively (parser);
5705 /* Look for the simple-type-specifier. */
5706 type = cp_parser_simple_type_specifier (parser,
5707 /*decl_specs=*/NULL,
5708 CP_PARSER_FLAGS_NONE);
5709 /* Parse the cast itself. */
5710 if (!cp_parser_error_occurred (parser))
5711 postfix_expression
5712 = cp_parser_functional_cast (parser, type);
5713 /* If that worked, we're done. */
5714 if (cp_parser_parse_definitely (parser))
5715 break;
5716
5717 /* If the functional-cast didn't work out, try a
5718 compound-literal. */
5719 if (cp_parser_allow_gnu_extensions_p (parser)
5720 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5721 {
5722 vec<constructor_elt, va_gc> *initializer_list = NULL;
5723 bool saved_in_type_id_in_expr_p;
5724
5725 cp_parser_parse_tentatively (parser);
5726 /* Consume the `('. */
5727 cp_lexer_consume_token (parser->lexer);
5728 /* Parse the type. */
5729 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5730 parser->in_type_id_in_expr_p = true;
5731 type = cp_parser_type_id (parser);
5732 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5733 /* Look for the `)'. */
5734 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5735 /* Look for the `{'. */
5736 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5737 /* If things aren't going well, there's no need to
5738 keep going. */
5739 if (!cp_parser_error_occurred (parser))
5740 {
5741 bool non_constant_p;
5742 /* Parse the initializer-list. */
5743 initializer_list
5744 = cp_parser_initializer_list (parser, &non_constant_p);
5745 /* Allow a trailing `,'. */
5746 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5747 cp_lexer_consume_token (parser->lexer);
5748 /* Look for the final `}'. */
5749 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5750 }
5751 /* If that worked, we're definitely looking at a
5752 compound-literal expression. */
5753 if (cp_parser_parse_definitely (parser))
5754 {
5755 /* Warn the user that a compound literal is not
5756 allowed in standard C++. */
5757 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5758 /* For simplicity, we disallow compound literals in
5759 constant-expressions. We could
5760 allow compound literals of integer type, whose
5761 initializer was a constant, in constant
5762 expressions. Permitting that usage, as a further
5763 extension, would not change the meaning of any
5764 currently accepted programs. (Of course, as
5765 compound literals are not part of ISO C++, the
5766 standard has nothing to say.) */
5767 if (cp_parser_non_integral_constant_expression (parser,
5768 NIC_NCC))
5769 {
5770 postfix_expression = error_mark_node;
5771 break;
5772 }
5773 /* Form the representation of the compound-literal. */
5774 postfix_expression
5775 = (finish_compound_literal
5776 (type, build_constructor (init_list_type_node,
5777 initializer_list),
5778 tf_warning_or_error));
5779 break;
5780 }
5781 }
5782
5783 /* It must be a primary-expression. */
5784 postfix_expression
5785 = cp_parser_primary_expression (parser, address_p, cast_p,
5786 /*template_arg_p=*/false,
5787 decltype_p,
5788 &idk);
5789 }
5790 break;
5791 }
5792
5793 /* Note that we don't need to worry about calling build_cplus_new on a
5794 class-valued CALL_EXPR in decltype when it isn't the end of the
5795 postfix-expression; unary_complex_lvalue will take care of that for
5796 all these cases. */
5797
5798 /* Keep looping until the postfix-expression is complete. */
5799 while (true)
5800 {
5801 if (idk == CP_ID_KIND_UNQUALIFIED
5802 && identifier_p (postfix_expression)
5803 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5804 /* It is not a Koenig lookup function call. */
5805 postfix_expression
5806 = unqualified_name_lookup_error (postfix_expression);
5807
5808 /* Peek at the next token. */
5809 token = cp_lexer_peek_token (parser->lexer);
5810
5811 switch (token->type)
5812 {
5813 case CPP_OPEN_SQUARE:
5814 if (cp_next_tokens_can_be_std_attribute_p (parser))
5815 {
5816 cp_parser_error (parser,
5817 "two consecutive %<[%> shall "
5818 "only introduce an attribute");
5819 return error_mark_node;
5820 }
5821 postfix_expression
5822 = cp_parser_postfix_open_square_expression (parser,
5823 postfix_expression,
5824 false,
5825 decltype_p);
5826 idk = CP_ID_KIND_NONE;
5827 is_member_access = false;
5828 break;
5829
5830 case CPP_OPEN_PAREN:
5831 /* postfix-expression ( expression-list [opt] ) */
5832 {
5833 bool koenig_p;
5834 bool is_builtin_constant_p;
5835 bool saved_integral_constant_expression_p = false;
5836 bool saved_non_integral_constant_expression_p = false;
5837 tsubst_flags_t complain = complain_flags (decltype_p);
5838 vec<tree, va_gc> *args;
5839
5840 is_member_access = false;
5841
5842 is_builtin_constant_p
5843 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5844 if (is_builtin_constant_p)
5845 {
5846 /* The whole point of __builtin_constant_p is to allow
5847 non-constant expressions to appear as arguments. */
5848 saved_integral_constant_expression_p
5849 = parser->integral_constant_expression_p;
5850 saved_non_integral_constant_expression_p
5851 = parser->non_integral_constant_expression_p;
5852 parser->integral_constant_expression_p = false;
5853 }
5854 args = (cp_parser_parenthesized_expression_list
5855 (parser, non_attr,
5856 /*cast_p=*/false, /*allow_expansion_p=*/true,
5857 /*non_constant_p=*/NULL));
5858 if (is_builtin_constant_p)
5859 {
5860 parser->integral_constant_expression_p
5861 = saved_integral_constant_expression_p;
5862 parser->non_integral_constant_expression_p
5863 = saved_non_integral_constant_expression_p;
5864 }
5865
5866 if (args == NULL)
5867 {
5868 postfix_expression = error_mark_node;
5869 break;
5870 }
5871
5872 /* Function calls are not permitted in
5873 constant-expressions. */
5874 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5875 && cp_parser_non_integral_constant_expression (parser,
5876 NIC_FUNC_CALL))
5877 {
5878 postfix_expression = error_mark_node;
5879 release_tree_vector (args);
5880 break;
5881 }
5882
5883 koenig_p = false;
5884 if (idk == CP_ID_KIND_UNQUALIFIED
5885 || idk == CP_ID_KIND_TEMPLATE_ID)
5886 {
5887 if (identifier_p (postfix_expression))
5888 {
5889 if (!args->is_empty ())
5890 {
5891 koenig_p = true;
5892 if (!any_type_dependent_arguments_p (args))
5893 postfix_expression
5894 = perform_koenig_lookup (postfix_expression, args,
5895 /*include_std=*/false,
5896 complain);
5897 }
5898 else
5899 postfix_expression
5900 = unqualified_fn_lookup_error (postfix_expression);
5901 }
5902 /* We do not perform argument-dependent lookup if
5903 normal lookup finds a non-function, in accordance
5904 with the expected resolution of DR 218. */
5905 else if (!args->is_empty ()
5906 && is_overloaded_fn (postfix_expression))
5907 {
5908 tree fn = get_first_fn (postfix_expression);
5909 fn = STRIP_TEMPLATE (fn);
5910
5911 /* Do not do argument dependent lookup if regular
5912 lookup finds a member function or a block-scope
5913 function declaration. [basic.lookup.argdep]/3 */
5914 if (!DECL_FUNCTION_MEMBER_P (fn)
5915 && !DECL_LOCAL_FUNCTION_P (fn))
5916 {
5917 koenig_p = true;
5918 if (!any_type_dependent_arguments_p (args))
5919 postfix_expression
5920 = perform_koenig_lookup (postfix_expression, args,
5921 /*include_std=*/false,
5922 complain);
5923 }
5924 }
5925 }
5926
5927 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5928 {
5929 tree instance = TREE_OPERAND (postfix_expression, 0);
5930 tree fn = TREE_OPERAND (postfix_expression, 1);
5931
5932 if (processing_template_decl
5933 && (type_dependent_expression_p (instance)
5934 || (!BASELINK_P (fn)
5935 && TREE_CODE (fn) != FIELD_DECL)
5936 || type_dependent_expression_p (fn)
5937 || any_type_dependent_arguments_p (args)))
5938 {
5939 postfix_expression
5940 = build_nt_call_vec (postfix_expression, args);
5941 release_tree_vector (args);
5942 break;
5943 }
5944
5945 if (BASELINK_P (fn))
5946 {
5947 postfix_expression
5948 = (build_new_method_call
5949 (instance, fn, &args, NULL_TREE,
5950 (idk == CP_ID_KIND_QUALIFIED
5951 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5952 : LOOKUP_NORMAL),
5953 /*fn_p=*/NULL,
5954 complain));
5955 }
5956 else
5957 postfix_expression
5958 = finish_call_expr (postfix_expression, &args,
5959 /*disallow_virtual=*/false,
5960 /*koenig_p=*/false,
5961 complain);
5962 }
5963 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5964 || TREE_CODE (postfix_expression) == MEMBER_REF
5965 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5966 postfix_expression = (build_offset_ref_call_from_tree
5967 (postfix_expression, &args,
5968 complain));
5969 else if (idk == CP_ID_KIND_QUALIFIED)
5970 /* A call to a static class member, or a namespace-scope
5971 function. */
5972 postfix_expression
5973 = finish_call_expr (postfix_expression, &args,
5974 /*disallow_virtual=*/true,
5975 koenig_p,
5976 complain);
5977 else
5978 /* All other function calls. */
5979 postfix_expression
5980 = finish_call_expr (postfix_expression, &args,
5981 /*disallow_virtual=*/false,
5982 koenig_p,
5983 complain);
5984
5985 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5986 idk = CP_ID_KIND_NONE;
5987
5988 release_tree_vector (args);
5989 }
5990 break;
5991
5992 case CPP_DOT:
5993 case CPP_DEREF:
5994 /* postfix-expression . template [opt] id-expression
5995 postfix-expression . pseudo-destructor-name
5996 postfix-expression -> template [opt] id-expression
5997 postfix-expression -> pseudo-destructor-name */
5998
5999 /* Consume the `.' or `->' operator. */
6000 cp_lexer_consume_token (parser->lexer);
6001
6002 postfix_expression
6003 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6004 postfix_expression,
6005 false, &idk,
6006 token->location);
6007
6008 is_member_access = true;
6009 break;
6010
6011 case CPP_PLUS_PLUS:
6012 /* postfix-expression ++ */
6013 /* Consume the `++' token. */
6014 cp_lexer_consume_token (parser->lexer);
6015 /* Generate a representation for the complete expression. */
6016 postfix_expression
6017 = finish_increment_expr (postfix_expression,
6018 POSTINCREMENT_EXPR);
6019 /* Increments may not appear in constant-expressions. */
6020 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6021 postfix_expression = error_mark_node;
6022 idk = CP_ID_KIND_NONE;
6023 is_member_access = false;
6024 break;
6025
6026 case CPP_MINUS_MINUS:
6027 /* postfix-expression -- */
6028 /* Consume the `--' token. */
6029 cp_lexer_consume_token (parser->lexer);
6030 /* Generate a representation for the complete expression. */
6031 postfix_expression
6032 = finish_increment_expr (postfix_expression,
6033 POSTDECREMENT_EXPR);
6034 /* Decrements may not appear in constant-expressions. */
6035 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6036 postfix_expression = error_mark_node;
6037 idk = CP_ID_KIND_NONE;
6038 is_member_access = false;
6039 break;
6040
6041 default:
6042 if (pidk_return != NULL)
6043 * pidk_return = idk;
6044 if (member_access_only_p)
6045 return is_member_access? postfix_expression : error_mark_node;
6046 else
6047 return postfix_expression;
6048 }
6049 }
6050
6051 /* We should never get here. */
6052 gcc_unreachable ();
6053 return error_mark_node;
6054 }
6055
6056 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6057 by cp_parser_builtin_offsetof. We're looking for
6058
6059 postfix-expression [ expression ]
6060 postfix-expression [ braced-init-list ] (C++11)
6061
6062 FOR_OFFSETOF is set if we're being called in that context, which
6063 changes how we deal with integer constant expressions. */
6064
6065 static tree
6066 cp_parser_postfix_open_square_expression (cp_parser *parser,
6067 tree postfix_expression,
6068 bool for_offsetof,
6069 bool decltype_p)
6070 {
6071 tree index;
6072 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6073
6074 /* Consume the `[' token. */
6075 cp_lexer_consume_token (parser->lexer);
6076
6077 /* Parse the index expression. */
6078 /* ??? For offsetof, there is a question of what to allow here. If
6079 offsetof is not being used in an integral constant expression context,
6080 then we *could* get the right answer by computing the value at runtime.
6081 If we are in an integral constant expression context, then we might
6082 could accept any constant expression; hard to say without analysis.
6083 Rather than open the barn door too wide right away, allow only integer
6084 constant expressions here. */
6085 if (for_offsetof)
6086 index = cp_parser_constant_expression (parser, false, NULL);
6087 else
6088 {
6089 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6090 {
6091 bool expr_nonconst_p;
6092 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6093 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6094 }
6095 else
6096 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6097 }
6098
6099 /* Look for the closing `]'. */
6100 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6101
6102 /* Build the ARRAY_REF. */
6103 postfix_expression = grok_array_decl (loc, postfix_expression,
6104 index, decltype_p);
6105
6106 /* When not doing offsetof, array references are not permitted in
6107 constant-expressions. */
6108 if (!for_offsetof
6109 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6110 postfix_expression = error_mark_node;
6111
6112 return postfix_expression;
6113 }
6114
6115 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6116 by cp_parser_builtin_offsetof. We're looking for
6117
6118 postfix-expression . template [opt] id-expression
6119 postfix-expression . pseudo-destructor-name
6120 postfix-expression -> template [opt] id-expression
6121 postfix-expression -> pseudo-destructor-name
6122
6123 FOR_OFFSETOF is set if we're being called in that context. That sorta
6124 limits what of the above we'll actually accept, but nevermind.
6125 TOKEN_TYPE is the "." or "->" token, which will already have been
6126 removed from the stream. */
6127
6128 static tree
6129 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6130 enum cpp_ttype token_type,
6131 tree postfix_expression,
6132 bool for_offsetof, cp_id_kind *idk,
6133 location_t location)
6134 {
6135 tree name;
6136 bool dependent_p;
6137 bool pseudo_destructor_p;
6138 tree scope = NULL_TREE;
6139
6140 /* If this is a `->' operator, dereference the pointer. */
6141 if (token_type == CPP_DEREF)
6142 postfix_expression = build_x_arrow (location, postfix_expression,
6143 tf_warning_or_error);
6144 /* Check to see whether or not the expression is type-dependent. */
6145 dependent_p = type_dependent_expression_p (postfix_expression);
6146 /* The identifier following the `->' or `.' is not qualified. */
6147 parser->scope = NULL_TREE;
6148 parser->qualifying_scope = NULL_TREE;
6149 parser->object_scope = NULL_TREE;
6150 *idk = CP_ID_KIND_NONE;
6151
6152 /* Enter the scope corresponding to the type of the object
6153 given by the POSTFIX_EXPRESSION. */
6154 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6155 {
6156 scope = TREE_TYPE (postfix_expression);
6157 /* According to the standard, no expression should ever have
6158 reference type. Unfortunately, we do not currently match
6159 the standard in this respect in that our internal representation
6160 of an expression may have reference type even when the standard
6161 says it does not. Therefore, we have to manually obtain the
6162 underlying type here. */
6163 scope = non_reference (scope);
6164 /* The type of the POSTFIX_EXPRESSION must be complete. */
6165 if (scope == unknown_type_node)
6166 {
6167 error_at (location, "%qE does not have class type",
6168 postfix_expression);
6169 scope = NULL_TREE;
6170 }
6171 /* Unlike the object expression in other contexts, *this is not
6172 required to be of complete type for purposes of class member
6173 access (5.2.5) outside the member function body. */
6174 else if (postfix_expression != current_class_ref
6175 && !(processing_template_decl && scope == current_class_type))
6176 scope = complete_type_or_else (scope, NULL_TREE);
6177 /* Let the name lookup machinery know that we are processing a
6178 class member access expression. */
6179 parser->context->object_type = scope;
6180 /* If something went wrong, we want to be able to discern that case,
6181 as opposed to the case where there was no SCOPE due to the type
6182 of expression being dependent. */
6183 if (!scope)
6184 scope = error_mark_node;
6185 /* If the SCOPE was erroneous, make the various semantic analysis
6186 functions exit quickly -- and without issuing additional error
6187 messages. */
6188 if (scope == error_mark_node)
6189 postfix_expression = error_mark_node;
6190 }
6191
6192 /* Assume this expression is not a pseudo-destructor access. */
6193 pseudo_destructor_p = false;
6194
6195 /* If the SCOPE is a scalar type, then, if this is a valid program,
6196 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6197 is type dependent, it can be pseudo-destructor-name or something else.
6198 Try to parse it as pseudo-destructor-name first. */
6199 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6200 {
6201 tree s;
6202 tree type;
6203
6204 cp_parser_parse_tentatively (parser);
6205 /* Parse the pseudo-destructor-name. */
6206 s = NULL_TREE;
6207 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6208 &s, &type);
6209 if (dependent_p
6210 && (cp_parser_error_occurred (parser)
6211 || !SCALAR_TYPE_P (type)))
6212 cp_parser_abort_tentative_parse (parser);
6213 else if (cp_parser_parse_definitely (parser))
6214 {
6215 pseudo_destructor_p = true;
6216 postfix_expression
6217 = finish_pseudo_destructor_expr (postfix_expression,
6218 s, type);
6219 }
6220 }
6221
6222 if (!pseudo_destructor_p)
6223 {
6224 /* If the SCOPE is not a scalar type, we are looking at an
6225 ordinary class member access expression, rather than a
6226 pseudo-destructor-name. */
6227 bool template_p;
6228 cp_token *token = cp_lexer_peek_token (parser->lexer);
6229 /* Parse the id-expression. */
6230 name = (cp_parser_id_expression
6231 (parser,
6232 cp_parser_optional_template_keyword (parser),
6233 /*check_dependency_p=*/true,
6234 &template_p,
6235 /*declarator_p=*/false,
6236 /*optional_p=*/false));
6237 /* In general, build a SCOPE_REF if the member name is qualified.
6238 However, if the name was not dependent and has already been
6239 resolved; there is no need to build the SCOPE_REF. For example;
6240
6241 struct X { void f(); };
6242 template <typename T> void f(T* t) { t->X::f(); }
6243
6244 Even though "t" is dependent, "X::f" is not and has been resolved
6245 to a BASELINK; there is no need to include scope information. */
6246
6247 /* But we do need to remember that there was an explicit scope for
6248 virtual function calls. */
6249 if (parser->scope)
6250 *idk = CP_ID_KIND_QUALIFIED;
6251
6252 /* If the name is a template-id that names a type, we will get a
6253 TYPE_DECL here. That is invalid code. */
6254 if (TREE_CODE (name) == TYPE_DECL)
6255 {
6256 error_at (token->location, "invalid use of %qD", name);
6257 postfix_expression = error_mark_node;
6258 }
6259 else
6260 {
6261 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6262 {
6263 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6264 {
6265 error_at (token->location, "%<%D::%D%> is not a class member",
6266 parser->scope, name);
6267 postfix_expression = error_mark_node;
6268 }
6269 else
6270 name = build_qualified_name (/*type=*/NULL_TREE,
6271 parser->scope,
6272 name,
6273 template_p);
6274 parser->scope = NULL_TREE;
6275 parser->qualifying_scope = NULL_TREE;
6276 parser->object_scope = NULL_TREE;
6277 }
6278 if (parser->scope && name && BASELINK_P (name))
6279 adjust_result_of_qualified_name_lookup
6280 (name, parser->scope, scope);
6281 postfix_expression
6282 = finish_class_member_access_expr (postfix_expression, name,
6283 template_p,
6284 tf_warning_or_error);
6285 }
6286 }
6287
6288 /* We no longer need to look up names in the scope of the object on
6289 the left-hand side of the `.' or `->' operator. */
6290 parser->context->object_type = NULL_TREE;
6291
6292 /* Outside of offsetof, these operators may not appear in
6293 constant-expressions. */
6294 if (!for_offsetof
6295 && (cp_parser_non_integral_constant_expression
6296 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6297 postfix_expression = error_mark_node;
6298
6299 return postfix_expression;
6300 }
6301
6302 /* Parse a parenthesized expression-list.
6303
6304 expression-list:
6305 assignment-expression
6306 expression-list, assignment-expression
6307
6308 attribute-list:
6309 expression-list
6310 identifier
6311 identifier, expression-list
6312
6313 CAST_P is true if this expression is the target of a cast.
6314
6315 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6316 argument pack.
6317
6318 Returns a vector of trees. Each element is a representation of an
6319 assignment-expression. NULL is returned if the ( and or ) are
6320 missing. An empty, but allocated, vector is returned on no
6321 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6322 if we are parsing an attribute list for an attribute that wants a
6323 plain identifier argument, normal_attr for an attribute that wants
6324 an expression, or non_attr if we aren't parsing an attribute list. If
6325 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6326 not all of the expressions in the list were constant. */
6327
6328 static vec<tree, va_gc> *
6329 cp_parser_parenthesized_expression_list (cp_parser* parser,
6330 int is_attribute_list,
6331 bool cast_p,
6332 bool allow_expansion_p,
6333 bool *non_constant_p)
6334 {
6335 vec<tree, va_gc> *expression_list;
6336 bool fold_expr_p = is_attribute_list != non_attr;
6337 tree identifier = NULL_TREE;
6338 bool saved_greater_than_is_operator_p;
6339
6340 /* Assume all the expressions will be constant. */
6341 if (non_constant_p)
6342 *non_constant_p = false;
6343
6344 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6345 return NULL;
6346
6347 expression_list = make_tree_vector ();
6348
6349 /* Within a parenthesized expression, a `>' token is always
6350 the greater-than operator. */
6351 saved_greater_than_is_operator_p
6352 = parser->greater_than_is_operator_p;
6353 parser->greater_than_is_operator_p = true;
6354
6355 /* Consume expressions until there are no more. */
6356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6357 while (true)
6358 {
6359 tree expr;
6360
6361 /* At the beginning of attribute lists, check to see if the
6362 next token is an identifier. */
6363 if (is_attribute_list == id_attr
6364 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6365 {
6366 cp_token *token;
6367
6368 /* Consume the identifier. */
6369 token = cp_lexer_consume_token (parser->lexer);
6370 /* Save the identifier. */
6371 identifier = token->u.value;
6372 }
6373 else
6374 {
6375 bool expr_non_constant_p;
6376
6377 /* Parse the next assignment-expression. */
6378 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6379 {
6380 /* A braced-init-list. */
6381 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6382 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6383 if (non_constant_p && expr_non_constant_p)
6384 *non_constant_p = true;
6385 }
6386 else if (non_constant_p)
6387 {
6388 expr = (cp_parser_constant_expression
6389 (parser, /*allow_non_constant_p=*/true,
6390 &expr_non_constant_p));
6391 if (expr_non_constant_p)
6392 *non_constant_p = true;
6393 }
6394 else
6395 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6396
6397 if (fold_expr_p)
6398 expr = fold_non_dependent_expr (expr);
6399
6400 /* If we have an ellipsis, then this is an expression
6401 expansion. */
6402 if (allow_expansion_p
6403 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6404 {
6405 /* Consume the `...'. */
6406 cp_lexer_consume_token (parser->lexer);
6407
6408 /* Build the argument pack. */
6409 expr = make_pack_expansion (expr);
6410 }
6411
6412 /* Add it to the list. We add error_mark_node
6413 expressions to the list, so that we can still tell if
6414 the correct form for a parenthesized expression-list
6415 is found. That gives better errors. */
6416 vec_safe_push (expression_list, expr);
6417
6418 if (expr == error_mark_node)
6419 goto skip_comma;
6420 }
6421
6422 /* After the first item, attribute lists look the same as
6423 expression lists. */
6424 is_attribute_list = non_attr;
6425
6426 get_comma:;
6427 /* If the next token isn't a `,', then we are done. */
6428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6429 break;
6430
6431 /* Otherwise, consume the `,' and keep going. */
6432 cp_lexer_consume_token (parser->lexer);
6433 }
6434
6435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6436 {
6437 int ending;
6438
6439 skip_comma:;
6440 /* We try and resync to an unnested comma, as that will give the
6441 user better diagnostics. */
6442 ending = cp_parser_skip_to_closing_parenthesis (parser,
6443 /*recovering=*/true,
6444 /*or_comma=*/true,
6445 /*consume_paren=*/true);
6446 if (ending < 0)
6447 goto get_comma;
6448 if (!ending)
6449 {
6450 parser->greater_than_is_operator_p
6451 = saved_greater_than_is_operator_p;
6452 return NULL;
6453 }
6454 }
6455
6456 parser->greater_than_is_operator_p
6457 = saved_greater_than_is_operator_p;
6458
6459 if (identifier)
6460 vec_safe_insert (expression_list, 0, identifier);
6461
6462 return expression_list;
6463 }
6464
6465 /* Parse a pseudo-destructor-name.
6466
6467 pseudo-destructor-name:
6468 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6469 :: [opt] nested-name-specifier template template-id :: ~ type-name
6470 :: [opt] nested-name-specifier [opt] ~ type-name
6471
6472 If either of the first two productions is used, sets *SCOPE to the
6473 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6474 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6475 or ERROR_MARK_NODE if the parse fails. */
6476
6477 static void
6478 cp_parser_pseudo_destructor_name (cp_parser* parser,
6479 tree object,
6480 tree* scope,
6481 tree* type)
6482 {
6483 bool nested_name_specifier_p;
6484
6485 /* Handle ~auto. */
6486 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6487 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6488 && !type_dependent_expression_p (object))
6489 {
6490 if (cxx_dialect < cxx1y)
6491 pedwarn (input_location, 0,
6492 "%<~auto%> only available with "
6493 "-std=c++1y or -std=gnu++1y");
6494 cp_lexer_consume_token (parser->lexer);
6495 cp_lexer_consume_token (parser->lexer);
6496 *scope = NULL_TREE;
6497 *type = TREE_TYPE (object);
6498 return;
6499 }
6500
6501 /* Assume that things will not work out. */
6502 *type = error_mark_node;
6503
6504 /* Look for the optional `::' operator. */
6505 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6506 /* Look for the optional nested-name-specifier. */
6507 nested_name_specifier_p
6508 = (cp_parser_nested_name_specifier_opt (parser,
6509 /*typename_keyword_p=*/false,
6510 /*check_dependency_p=*/true,
6511 /*type_p=*/false,
6512 /*is_declaration=*/false)
6513 != NULL_TREE);
6514 /* Now, if we saw a nested-name-specifier, we might be doing the
6515 second production. */
6516 if (nested_name_specifier_p
6517 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6518 {
6519 /* Consume the `template' keyword. */
6520 cp_lexer_consume_token (parser->lexer);
6521 /* Parse the template-id. */
6522 cp_parser_template_id (parser,
6523 /*template_keyword_p=*/true,
6524 /*check_dependency_p=*/false,
6525 class_type,
6526 /*is_declaration=*/true);
6527 /* Look for the `::' token. */
6528 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6529 }
6530 /* If the next token is not a `~', then there might be some
6531 additional qualification. */
6532 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6533 {
6534 /* At this point, we're looking for "type-name :: ~". The type-name
6535 must not be a class-name, since this is a pseudo-destructor. So,
6536 it must be either an enum-name, or a typedef-name -- both of which
6537 are just identifiers. So, we peek ahead to check that the "::"
6538 and "~" tokens are present; if they are not, then we can avoid
6539 calling type_name. */
6540 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6541 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6542 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6543 {
6544 cp_parser_error (parser, "non-scalar type");
6545 return;
6546 }
6547
6548 /* Look for the type-name. */
6549 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6550 if (*scope == error_mark_node)
6551 return;
6552
6553 /* Look for the `::' token. */
6554 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6555 }
6556 else
6557 *scope = NULL_TREE;
6558
6559 /* Look for the `~'. */
6560 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6561
6562 /* Once we see the ~, this has to be a pseudo-destructor. */
6563 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6564 cp_parser_commit_to_tentative_parse (parser);
6565
6566 /* Look for the type-name again. We are not responsible for
6567 checking that it matches the first type-name. */
6568 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6569 }
6570
6571 /* Parse a unary-expression.
6572
6573 unary-expression:
6574 postfix-expression
6575 ++ cast-expression
6576 -- cast-expression
6577 unary-operator cast-expression
6578 sizeof unary-expression
6579 sizeof ( type-id )
6580 alignof ( type-id ) [C++0x]
6581 new-expression
6582 delete-expression
6583
6584 GNU Extensions:
6585
6586 unary-expression:
6587 __extension__ cast-expression
6588 __alignof__ unary-expression
6589 __alignof__ ( type-id )
6590 alignof unary-expression [C++0x]
6591 __real__ cast-expression
6592 __imag__ cast-expression
6593 && identifier
6594
6595 ADDRESS_P is true iff the unary-expression is appearing as the
6596 operand of the `&' operator. CAST_P is true if this expression is
6597 the target of a cast.
6598
6599 Returns a representation of the expression. */
6600
6601 static tree
6602 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6603 bool decltype_p, cp_id_kind * pidk)
6604 {
6605 cp_token *token;
6606 enum tree_code unary_operator;
6607
6608 /* Peek at the next token. */
6609 token = cp_lexer_peek_token (parser->lexer);
6610 /* Some keywords give away the kind of expression. */
6611 if (token->type == CPP_KEYWORD)
6612 {
6613 enum rid keyword = token->keyword;
6614
6615 switch (keyword)
6616 {
6617 case RID_ALIGNOF:
6618 case RID_SIZEOF:
6619 {
6620 tree operand, ret;
6621 enum tree_code op;
6622 location_t first_loc;
6623
6624 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6625 /* Consume the token. */
6626 cp_lexer_consume_token (parser->lexer);
6627 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6628 /* Parse the operand. */
6629 operand = cp_parser_sizeof_operand (parser, keyword);
6630
6631 if (TYPE_P (operand))
6632 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6633 else
6634 {
6635 /* ISO C++ defines alignof only with types, not with
6636 expressions. So pedwarn if alignof is used with a non-
6637 type expression. However, __alignof__ is ok. */
6638 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6639 pedwarn (token->location, OPT_Wpedantic,
6640 "ISO C++ does not allow %<alignof%> "
6641 "with a non-type");
6642
6643 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6644 }
6645 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6646 SIZEOF_EXPR with the original operand. */
6647 if (op == SIZEOF_EXPR && ret != error_mark_node)
6648 {
6649 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6650 {
6651 if (!processing_template_decl && TYPE_P (operand))
6652 {
6653 ret = build_min (SIZEOF_EXPR, size_type_node,
6654 build1 (NOP_EXPR, operand,
6655 error_mark_node));
6656 SIZEOF_EXPR_TYPE_P (ret) = 1;
6657 }
6658 else
6659 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6660 TREE_SIDE_EFFECTS (ret) = 0;
6661 TREE_READONLY (ret) = 1;
6662 }
6663 SET_EXPR_LOCATION (ret, first_loc);
6664 }
6665 return ret;
6666 }
6667
6668 case RID_NEW:
6669 return cp_parser_new_expression (parser);
6670
6671 case RID_DELETE:
6672 return cp_parser_delete_expression (parser);
6673
6674 case RID_EXTENSION:
6675 {
6676 /* The saved value of the PEDANTIC flag. */
6677 int saved_pedantic;
6678 tree expr;
6679
6680 /* Save away the PEDANTIC flag. */
6681 cp_parser_extension_opt (parser, &saved_pedantic);
6682 /* Parse the cast-expression. */
6683 expr = cp_parser_simple_cast_expression (parser);
6684 /* Restore the PEDANTIC flag. */
6685 pedantic = saved_pedantic;
6686
6687 return expr;
6688 }
6689
6690 case RID_REALPART:
6691 case RID_IMAGPART:
6692 {
6693 tree expression;
6694
6695 /* Consume the `__real__' or `__imag__' token. */
6696 cp_lexer_consume_token (parser->lexer);
6697 /* Parse the cast-expression. */
6698 expression = cp_parser_simple_cast_expression (parser);
6699 /* Create the complete representation. */
6700 return build_x_unary_op (token->location,
6701 (keyword == RID_REALPART
6702 ? REALPART_EXPR : IMAGPART_EXPR),
6703 expression,
6704 tf_warning_or_error);
6705 }
6706 break;
6707
6708 case RID_TRANSACTION_ATOMIC:
6709 case RID_TRANSACTION_RELAXED:
6710 return cp_parser_transaction_expression (parser, keyword);
6711
6712 case RID_NOEXCEPT:
6713 {
6714 tree expr;
6715 const char *saved_message;
6716 bool saved_integral_constant_expression_p;
6717 bool saved_non_integral_constant_expression_p;
6718 bool saved_greater_than_is_operator_p;
6719
6720 cp_lexer_consume_token (parser->lexer);
6721 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6722
6723 saved_message = parser->type_definition_forbidden_message;
6724 parser->type_definition_forbidden_message
6725 = G_("types may not be defined in %<noexcept%> expressions");
6726
6727 saved_integral_constant_expression_p
6728 = parser->integral_constant_expression_p;
6729 saved_non_integral_constant_expression_p
6730 = parser->non_integral_constant_expression_p;
6731 parser->integral_constant_expression_p = false;
6732
6733 saved_greater_than_is_operator_p
6734 = parser->greater_than_is_operator_p;
6735 parser->greater_than_is_operator_p = true;
6736
6737 ++cp_unevaluated_operand;
6738 ++c_inhibit_evaluation_warnings;
6739 expr = cp_parser_expression (parser, false, NULL);
6740 --c_inhibit_evaluation_warnings;
6741 --cp_unevaluated_operand;
6742
6743 parser->greater_than_is_operator_p
6744 = saved_greater_than_is_operator_p;
6745
6746 parser->integral_constant_expression_p
6747 = saved_integral_constant_expression_p;
6748 parser->non_integral_constant_expression_p
6749 = saved_non_integral_constant_expression_p;
6750
6751 parser->type_definition_forbidden_message = saved_message;
6752
6753 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6754 return finish_noexcept_expr (expr, tf_warning_or_error);
6755 }
6756
6757 default:
6758 break;
6759 }
6760 }
6761
6762 /* Look for the `:: new' and `:: delete', which also signal the
6763 beginning of a new-expression, or delete-expression,
6764 respectively. If the next token is `::', then it might be one of
6765 these. */
6766 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6767 {
6768 enum rid keyword;
6769
6770 /* See if the token after the `::' is one of the keywords in
6771 which we're interested. */
6772 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6773 /* If it's `new', we have a new-expression. */
6774 if (keyword == RID_NEW)
6775 return cp_parser_new_expression (parser);
6776 /* Similarly, for `delete'. */
6777 else if (keyword == RID_DELETE)
6778 return cp_parser_delete_expression (parser);
6779 }
6780
6781 /* Look for a unary operator. */
6782 unary_operator = cp_parser_unary_operator (token);
6783 /* The `++' and `--' operators can be handled similarly, even though
6784 they are not technically unary-operators in the grammar. */
6785 if (unary_operator == ERROR_MARK)
6786 {
6787 if (token->type == CPP_PLUS_PLUS)
6788 unary_operator = PREINCREMENT_EXPR;
6789 else if (token->type == CPP_MINUS_MINUS)
6790 unary_operator = PREDECREMENT_EXPR;
6791 /* Handle the GNU address-of-label extension. */
6792 else if (cp_parser_allow_gnu_extensions_p (parser)
6793 && token->type == CPP_AND_AND)
6794 {
6795 tree identifier;
6796 tree expression;
6797 location_t loc = token->location;
6798
6799 /* Consume the '&&' token. */
6800 cp_lexer_consume_token (parser->lexer);
6801 /* Look for the identifier. */
6802 identifier = cp_parser_identifier (parser);
6803 /* Create an expression representing the address. */
6804 expression = finish_label_address_expr (identifier, loc);
6805 if (cp_parser_non_integral_constant_expression (parser,
6806 NIC_ADDR_LABEL))
6807 expression = error_mark_node;
6808 return expression;
6809 }
6810 }
6811 if (unary_operator != ERROR_MARK)
6812 {
6813 tree cast_expression;
6814 tree expression = error_mark_node;
6815 non_integral_constant non_constant_p = NIC_NONE;
6816 location_t loc = token->location;
6817 tsubst_flags_t complain = complain_flags (decltype_p);
6818
6819 /* Consume the operator token. */
6820 token = cp_lexer_consume_token (parser->lexer);
6821 /* Parse the cast-expression. */
6822 cast_expression
6823 = cp_parser_cast_expression (parser,
6824 unary_operator == ADDR_EXPR,
6825 /*cast_p=*/false,
6826 /*decltype*/false,
6827 pidk);
6828 /* Now, build an appropriate representation. */
6829 switch (unary_operator)
6830 {
6831 case INDIRECT_REF:
6832 non_constant_p = NIC_STAR;
6833 expression = build_x_indirect_ref (loc, cast_expression,
6834 RO_UNARY_STAR,
6835 complain);
6836 break;
6837
6838 case ADDR_EXPR:
6839 non_constant_p = NIC_ADDR;
6840 /* Fall through. */
6841 case BIT_NOT_EXPR:
6842 expression = build_x_unary_op (loc, unary_operator,
6843 cast_expression,
6844 complain);
6845 break;
6846
6847 case PREINCREMENT_EXPR:
6848 case PREDECREMENT_EXPR:
6849 non_constant_p = unary_operator == PREINCREMENT_EXPR
6850 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6851 /* Fall through. */
6852 case UNARY_PLUS_EXPR:
6853 case NEGATE_EXPR:
6854 case TRUTH_NOT_EXPR:
6855 expression = finish_unary_op_expr (loc, unary_operator,
6856 cast_expression, complain);
6857 break;
6858
6859 default:
6860 gcc_unreachable ();
6861 }
6862
6863 if (non_constant_p != NIC_NONE
6864 && cp_parser_non_integral_constant_expression (parser,
6865 non_constant_p))
6866 expression = error_mark_node;
6867
6868 return expression;
6869 }
6870
6871 return cp_parser_postfix_expression (parser, address_p, cast_p,
6872 /*member_access_only_p=*/false,
6873 decltype_p,
6874 pidk);
6875 }
6876
6877 static inline tree
6878 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6879 cp_id_kind * pidk)
6880 {
6881 return cp_parser_unary_expression (parser, address_p, cast_p,
6882 /*decltype*/false, pidk);
6883 }
6884
6885 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6886 unary-operator, the corresponding tree code is returned. */
6887
6888 static enum tree_code
6889 cp_parser_unary_operator (cp_token* token)
6890 {
6891 switch (token->type)
6892 {
6893 case CPP_MULT:
6894 return INDIRECT_REF;
6895
6896 case CPP_AND:
6897 return ADDR_EXPR;
6898
6899 case CPP_PLUS:
6900 return UNARY_PLUS_EXPR;
6901
6902 case CPP_MINUS:
6903 return NEGATE_EXPR;
6904
6905 case CPP_NOT:
6906 return TRUTH_NOT_EXPR;
6907
6908 case CPP_COMPL:
6909 return BIT_NOT_EXPR;
6910
6911 default:
6912 return ERROR_MARK;
6913 }
6914 }
6915
6916 /* Parse a new-expression.
6917
6918 new-expression:
6919 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6920 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6921
6922 Returns a representation of the expression. */
6923
6924 static tree
6925 cp_parser_new_expression (cp_parser* parser)
6926 {
6927 bool global_scope_p;
6928 vec<tree, va_gc> *placement;
6929 tree type;
6930 vec<tree, va_gc> *initializer;
6931 tree nelts = NULL_TREE;
6932 tree ret;
6933
6934 /* Look for the optional `::' operator. */
6935 global_scope_p
6936 = (cp_parser_global_scope_opt (parser,
6937 /*current_scope_valid_p=*/false)
6938 != NULL_TREE);
6939 /* Look for the `new' operator. */
6940 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6941 /* There's no easy way to tell a new-placement from the
6942 `( type-id )' construct. */
6943 cp_parser_parse_tentatively (parser);
6944 /* Look for a new-placement. */
6945 placement = cp_parser_new_placement (parser);
6946 /* If that didn't work out, there's no new-placement. */
6947 if (!cp_parser_parse_definitely (parser))
6948 {
6949 if (placement != NULL)
6950 release_tree_vector (placement);
6951 placement = NULL;
6952 }
6953
6954 /* If the next token is a `(', then we have a parenthesized
6955 type-id. */
6956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6957 {
6958 cp_token *token;
6959 const char *saved_message = parser->type_definition_forbidden_message;
6960
6961 /* Consume the `('. */
6962 cp_lexer_consume_token (parser->lexer);
6963
6964 /* Parse the type-id. */
6965 parser->type_definition_forbidden_message
6966 = G_("types may not be defined in a new-expression");
6967 type = cp_parser_type_id (parser);
6968 parser->type_definition_forbidden_message = saved_message;
6969
6970 /* Look for the closing `)'. */
6971 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6972 token = cp_lexer_peek_token (parser->lexer);
6973 /* There should not be a direct-new-declarator in this production,
6974 but GCC used to allowed this, so we check and emit a sensible error
6975 message for this case. */
6976 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6977 {
6978 error_at (token->location,
6979 "array bound forbidden after parenthesized type-id");
6980 inform (token->location,
6981 "try removing the parentheses around the type-id");
6982 cp_parser_direct_new_declarator (parser);
6983 }
6984 }
6985 /* Otherwise, there must be a new-type-id. */
6986 else
6987 type = cp_parser_new_type_id (parser, &nelts);
6988
6989 /* If the next token is a `(' or '{', then we have a new-initializer. */
6990 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6991 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6992 initializer = cp_parser_new_initializer (parser);
6993 else
6994 initializer = NULL;
6995
6996 /* A new-expression may not appear in an integral constant
6997 expression. */
6998 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6999 ret = error_mark_node;
7000 else
7001 {
7002 /* Create a representation of the new-expression. */
7003 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7004 tf_warning_or_error);
7005 }
7006
7007 if (placement != NULL)
7008 release_tree_vector (placement);
7009 if (initializer != NULL)
7010 release_tree_vector (initializer);
7011
7012 return ret;
7013 }
7014
7015 /* Parse a new-placement.
7016
7017 new-placement:
7018 ( expression-list )
7019
7020 Returns the same representation as for an expression-list. */
7021
7022 static vec<tree, va_gc> *
7023 cp_parser_new_placement (cp_parser* parser)
7024 {
7025 vec<tree, va_gc> *expression_list;
7026
7027 /* Parse the expression-list. */
7028 expression_list = (cp_parser_parenthesized_expression_list
7029 (parser, non_attr, /*cast_p=*/false,
7030 /*allow_expansion_p=*/true,
7031 /*non_constant_p=*/NULL));
7032
7033 return expression_list;
7034 }
7035
7036 /* Parse a new-type-id.
7037
7038 new-type-id:
7039 type-specifier-seq new-declarator [opt]
7040
7041 Returns the TYPE allocated. If the new-type-id indicates an array
7042 type, *NELTS is set to the number of elements in the last array
7043 bound; the TYPE will not include the last array bound. */
7044
7045 static tree
7046 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7047 {
7048 cp_decl_specifier_seq type_specifier_seq;
7049 cp_declarator *new_declarator;
7050 cp_declarator *declarator;
7051 cp_declarator *outer_declarator;
7052 const char *saved_message;
7053
7054 /* The type-specifier sequence must not contain type definitions.
7055 (It cannot contain declarations of new types either, but if they
7056 are not definitions we will catch that because they are not
7057 complete.) */
7058 saved_message = parser->type_definition_forbidden_message;
7059 parser->type_definition_forbidden_message
7060 = G_("types may not be defined in a new-type-id");
7061 /* Parse the type-specifier-seq. */
7062 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7063 /*is_trailing_return=*/false,
7064 &type_specifier_seq);
7065 /* Restore the old message. */
7066 parser->type_definition_forbidden_message = saved_message;
7067
7068 if (type_specifier_seq.type == error_mark_node)
7069 return error_mark_node;
7070
7071 /* Parse the new-declarator. */
7072 new_declarator = cp_parser_new_declarator_opt (parser);
7073
7074 /* Determine the number of elements in the last array dimension, if
7075 any. */
7076 *nelts = NULL_TREE;
7077 /* Skip down to the last array dimension. */
7078 declarator = new_declarator;
7079 outer_declarator = NULL;
7080 while (declarator && (declarator->kind == cdk_pointer
7081 || declarator->kind == cdk_ptrmem))
7082 {
7083 outer_declarator = declarator;
7084 declarator = declarator->declarator;
7085 }
7086 while (declarator
7087 && declarator->kind == cdk_array
7088 && declarator->declarator
7089 && declarator->declarator->kind == cdk_array)
7090 {
7091 outer_declarator = declarator;
7092 declarator = declarator->declarator;
7093 }
7094
7095 if (declarator && declarator->kind == cdk_array)
7096 {
7097 *nelts = declarator->u.array.bounds;
7098 if (*nelts == error_mark_node)
7099 *nelts = integer_one_node;
7100
7101 if (outer_declarator)
7102 outer_declarator->declarator = declarator->declarator;
7103 else
7104 new_declarator = NULL;
7105 }
7106
7107 return groktypename (&type_specifier_seq, new_declarator, false);
7108 }
7109
7110 /* Parse an (optional) new-declarator.
7111
7112 new-declarator:
7113 ptr-operator new-declarator [opt]
7114 direct-new-declarator
7115
7116 Returns the declarator. */
7117
7118 static cp_declarator *
7119 cp_parser_new_declarator_opt (cp_parser* parser)
7120 {
7121 enum tree_code code;
7122 tree type, std_attributes = NULL_TREE;
7123 cp_cv_quals cv_quals;
7124
7125 /* We don't know if there's a ptr-operator next, or not. */
7126 cp_parser_parse_tentatively (parser);
7127 /* Look for a ptr-operator. */
7128 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7129 /* If that worked, look for more new-declarators. */
7130 if (cp_parser_parse_definitely (parser))
7131 {
7132 cp_declarator *declarator;
7133
7134 /* Parse another optional declarator. */
7135 declarator = cp_parser_new_declarator_opt (parser);
7136
7137 declarator = cp_parser_make_indirect_declarator
7138 (code, type, cv_quals, declarator, std_attributes);
7139
7140 return declarator;
7141 }
7142
7143 /* If the next token is a `[', there is a direct-new-declarator. */
7144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7145 return cp_parser_direct_new_declarator (parser);
7146
7147 return NULL;
7148 }
7149
7150 /* Parse a direct-new-declarator.
7151
7152 direct-new-declarator:
7153 [ expression ]
7154 direct-new-declarator [constant-expression]
7155
7156 */
7157
7158 static cp_declarator *
7159 cp_parser_direct_new_declarator (cp_parser* parser)
7160 {
7161 cp_declarator *declarator = NULL;
7162
7163 while (true)
7164 {
7165 tree expression;
7166 cp_token *token;
7167
7168 /* Look for the opening `['. */
7169 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7170
7171 token = cp_lexer_peek_token (parser->lexer);
7172 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7173 /* The standard requires that the expression have integral
7174 type. DR 74 adds enumeration types. We believe that the
7175 real intent is that these expressions be handled like the
7176 expression in a `switch' condition, which also allows
7177 classes with a single conversion to integral or
7178 enumeration type. */
7179 if (!processing_template_decl)
7180 {
7181 expression
7182 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7183 expression,
7184 /*complain=*/true);
7185 if (!expression)
7186 {
7187 error_at (token->location,
7188 "expression in new-declarator must have integral "
7189 "or enumeration type");
7190 expression = error_mark_node;
7191 }
7192 }
7193
7194 /* Look for the closing `]'. */
7195 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7196
7197 /* Add this bound to the declarator. */
7198 declarator = make_array_declarator (declarator, expression);
7199
7200 /* If the next token is not a `[', then there are no more
7201 bounds. */
7202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7203 break;
7204 }
7205
7206 return declarator;
7207 }
7208
7209 /* Parse a new-initializer.
7210
7211 new-initializer:
7212 ( expression-list [opt] )
7213 braced-init-list
7214
7215 Returns a representation of the expression-list. */
7216
7217 static vec<tree, va_gc> *
7218 cp_parser_new_initializer (cp_parser* parser)
7219 {
7220 vec<tree, va_gc> *expression_list;
7221
7222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7223 {
7224 tree t;
7225 bool expr_non_constant_p;
7226 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7227 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7228 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7229 expression_list = make_tree_vector_single (t);
7230 }
7231 else
7232 expression_list = (cp_parser_parenthesized_expression_list
7233 (parser, non_attr, /*cast_p=*/false,
7234 /*allow_expansion_p=*/true,
7235 /*non_constant_p=*/NULL));
7236
7237 return expression_list;
7238 }
7239
7240 /* Parse a delete-expression.
7241
7242 delete-expression:
7243 :: [opt] delete cast-expression
7244 :: [opt] delete [ ] cast-expression
7245
7246 Returns a representation of the expression. */
7247
7248 static tree
7249 cp_parser_delete_expression (cp_parser* parser)
7250 {
7251 bool global_scope_p;
7252 bool array_p;
7253 tree expression;
7254
7255 /* Look for the optional `::' operator. */
7256 global_scope_p
7257 = (cp_parser_global_scope_opt (parser,
7258 /*current_scope_valid_p=*/false)
7259 != NULL_TREE);
7260 /* Look for the `delete' keyword. */
7261 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7262 /* See if the array syntax is in use. */
7263 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7264 {
7265 /* Consume the `[' token. */
7266 cp_lexer_consume_token (parser->lexer);
7267 /* Look for the `]' token. */
7268 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7269 /* Remember that this is the `[]' construct. */
7270 array_p = true;
7271 }
7272 else
7273 array_p = false;
7274
7275 /* Parse the cast-expression. */
7276 expression = cp_parser_simple_cast_expression (parser);
7277
7278 /* A delete-expression may not appear in an integral constant
7279 expression. */
7280 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7281 return error_mark_node;
7282
7283 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7284 tf_warning_or_error);
7285 }
7286
7287 /* Returns true if TOKEN may start a cast-expression and false
7288 otherwise. */
7289
7290 static bool
7291 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7292 {
7293 cp_token *token = cp_lexer_peek_token (parser->lexer);
7294 switch (token->type)
7295 {
7296 case CPP_COMMA:
7297 case CPP_SEMICOLON:
7298 case CPP_QUERY:
7299 case CPP_COLON:
7300 case CPP_CLOSE_SQUARE:
7301 case CPP_CLOSE_PAREN:
7302 case CPP_CLOSE_BRACE:
7303 case CPP_DOT:
7304 case CPP_DOT_STAR:
7305 case CPP_DEREF:
7306 case CPP_DEREF_STAR:
7307 case CPP_DIV:
7308 case CPP_MOD:
7309 case CPP_LSHIFT:
7310 case CPP_RSHIFT:
7311 case CPP_LESS:
7312 case CPP_GREATER:
7313 case CPP_LESS_EQ:
7314 case CPP_GREATER_EQ:
7315 case CPP_EQ_EQ:
7316 case CPP_NOT_EQ:
7317 case CPP_EQ:
7318 case CPP_MULT_EQ:
7319 case CPP_DIV_EQ:
7320 case CPP_MOD_EQ:
7321 case CPP_PLUS_EQ:
7322 case CPP_MINUS_EQ:
7323 case CPP_RSHIFT_EQ:
7324 case CPP_LSHIFT_EQ:
7325 case CPP_AND_EQ:
7326 case CPP_XOR_EQ:
7327 case CPP_OR_EQ:
7328 case CPP_XOR:
7329 case CPP_OR:
7330 case CPP_OR_OR:
7331 case CPP_EOF:
7332 return false;
7333
7334 case CPP_OPEN_PAREN:
7335 /* In ((type ()) () the last () isn't a valid cast-expression,
7336 so the whole must be parsed as postfix-expression. */
7337 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7338 != CPP_CLOSE_PAREN;
7339
7340 /* '[' may start a primary-expression in obj-c++. */
7341 case CPP_OPEN_SQUARE:
7342 return c_dialect_objc ();
7343
7344 default:
7345 return true;
7346 }
7347 }
7348
7349 /* Parse a cast-expression.
7350
7351 cast-expression:
7352 unary-expression
7353 ( type-id ) cast-expression
7354
7355 ADDRESS_P is true iff the unary-expression is appearing as the
7356 operand of the `&' operator. CAST_P is true if this expression is
7357 the target of a cast.
7358
7359 Returns a representation of the expression. */
7360
7361 static tree
7362 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7363 bool decltype_p, cp_id_kind * pidk)
7364 {
7365 /* If it's a `(', then we might be looking at a cast. */
7366 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7367 {
7368 tree type = NULL_TREE;
7369 tree expr = NULL_TREE;
7370 bool compound_literal_p;
7371 const char *saved_message;
7372
7373 /* There's no way to know yet whether or not this is a cast.
7374 For example, `(int (3))' is a unary-expression, while `(int)
7375 3' is a cast. So, we resort to parsing tentatively. */
7376 cp_parser_parse_tentatively (parser);
7377 /* Types may not be defined in a cast. */
7378 saved_message = parser->type_definition_forbidden_message;
7379 parser->type_definition_forbidden_message
7380 = G_("types may not be defined in casts");
7381 /* Consume the `('. */
7382 cp_lexer_consume_token (parser->lexer);
7383 /* A very tricky bit is that `(struct S) { 3 }' is a
7384 compound-literal (which we permit in C++ as an extension).
7385 But, that construct is not a cast-expression -- it is a
7386 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7387 is legal; if the compound-literal were a cast-expression,
7388 you'd need an extra set of parentheses.) But, if we parse
7389 the type-id, and it happens to be a class-specifier, then we
7390 will commit to the parse at that point, because we cannot
7391 undo the action that is done when creating a new class. So,
7392 then we cannot back up and do a postfix-expression.
7393
7394 Therefore, we scan ahead to the closing `)', and check to see
7395 if the token after the `)' is a `{'. If so, we are not
7396 looking at a cast-expression.
7397
7398 Save tokens so that we can put them back. */
7399 cp_lexer_save_tokens (parser->lexer);
7400 /* Skip tokens until the next token is a closing parenthesis.
7401 If we find the closing `)', and the next token is a `{', then
7402 we are looking at a compound-literal. */
7403 compound_literal_p
7404 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7405 /*consume_paren=*/true)
7406 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7407 /* Roll back the tokens we skipped. */
7408 cp_lexer_rollback_tokens (parser->lexer);
7409 /* If we were looking at a compound-literal, simulate an error
7410 so that the call to cp_parser_parse_definitely below will
7411 fail. */
7412 if (compound_literal_p)
7413 cp_parser_simulate_error (parser);
7414 else
7415 {
7416 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7417 parser->in_type_id_in_expr_p = true;
7418 /* Look for the type-id. */
7419 type = cp_parser_type_id (parser);
7420 /* Look for the closing `)'. */
7421 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7422 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7423 }
7424
7425 /* Restore the saved message. */
7426 parser->type_definition_forbidden_message = saved_message;
7427
7428 /* At this point this can only be either a cast or a
7429 parenthesized ctor such as `(T ())' that looks like a cast to
7430 function returning T. */
7431 if (!cp_parser_error_occurred (parser)
7432 && cp_parser_tokens_start_cast_expression (parser))
7433 {
7434 cp_parser_parse_definitely (parser);
7435 expr = cp_parser_cast_expression (parser,
7436 /*address_p=*/false,
7437 /*cast_p=*/true,
7438 /*decltype_p=*/false,
7439 pidk);
7440
7441 /* Warn about old-style casts, if so requested. */
7442 if (warn_old_style_cast
7443 && !in_system_header
7444 && !VOID_TYPE_P (type)
7445 && current_lang_name != lang_name_c)
7446 warning (OPT_Wold_style_cast, "use of old-style cast");
7447
7448 /* Only type conversions to integral or enumeration types
7449 can be used in constant-expressions. */
7450 if (!cast_valid_in_integral_constant_expression_p (type)
7451 && cp_parser_non_integral_constant_expression (parser,
7452 NIC_CAST))
7453 return error_mark_node;
7454
7455 /* Perform the cast. */
7456 expr = build_c_cast (input_location, type, expr);
7457 return expr;
7458 }
7459 else
7460 cp_parser_abort_tentative_parse (parser);
7461 }
7462
7463 /* If we get here, then it's not a cast, so it must be a
7464 unary-expression. */
7465 return cp_parser_unary_expression (parser, address_p, cast_p,
7466 decltype_p, pidk);
7467 }
7468
7469 /* Parse a binary expression of the general form:
7470
7471 pm-expression:
7472 cast-expression
7473 pm-expression .* cast-expression
7474 pm-expression ->* cast-expression
7475
7476 multiplicative-expression:
7477 pm-expression
7478 multiplicative-expression * pm-expression
7479 multiplicative-expression / pm-expression
7480 multiplicative-expression % pm-expression
7481
7482 additive-expression:
7483 multiplicative-expression
7484 additive-expression + multiplicative-expression
7485 additive-expression - multiplicative-expression
7486
7487 shift-expression:
7488 additive-expression
7489 shift-expression << additive-expression
7490 shift-expression >> additive-expression
7491
7492 relational-expression:
7493 shift-expression
7494 relational-expression < shift-expression
7495 relational-expression > shift-expression
7496 relational-expression <= shift-expression
7497 relational-expression >= shift-expression
7498
7499 GNU Extension:
7500
7501 relational-expression:
7502 relational-expression <? shift-expression
7503 relational-expression >? shift-expression
7504
7505 equality-expression:
7506 relational-expression
7507 equality-expression == relational-expression
7508 equality-expression != relational-expression
7509
7510 and-expression:
7511 equality-expression
7512 and-expression & equality-expression
7513
7514 exclusive-or-expression:
7515 and-expression
7516 exclusive-or-expression ^ and-expression
7517
7518 inclusive-or-expression:
7519 exclusive-or-expression
7520 inclusive-or-expression | exclusive-or-expression
7521
7522 logical-and-expression:
7523 inclusive-or-expression
7524 logical-and-expression && inclusive-or-expression
7525
7526 logical-or-expression:
7527 logical-and-expression
7528 logical-or-expression || logical-and-expression
7529
7530 All these are implemented with a single function like:
7531
7532 binary-expression:
7533 simple-cast-expression
7534 binary-expression <token> binary-expression
7535
7536 CAST_P is true if this expression is the target of a cast.
7537
7538 The binops_by_token map is used to get the tree codes for each <token> type.
7539 binary-expressions are associated according to a precedence table. */
7540
7541 #define TOKEN_PRECEDENCE(token) \
7542 (((token->type == CPP_GREATER \
7543 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7544 && !parser->greater_than_is_operator_p) \
7545 ? PREC_NOT_OPERATOR \
7546 : binops_by_token[token->type].prec)
7547
7548 static tree
7549 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7550 bool no_toplevel_fold_p,
7551 bool decltype_p,
7552 enum cp_parser_prec prec,
7553 cp_id_kind * pidk)
7554 {
7555 cp_parser_expression_stack stack;
7556 cp_parser_expression_stack_entry *sp = &stack[0];
7557 cp_parser_expression_stack_entry current;
7558 tree rhs;
7559 cp_token *token;
7560 enum tree_code rhs_type;
7561 enum cp_parser_prec new_prec, lookahead_prec;
7562 tree overload;
7563
7564 /* Parse the first expression. */
7565 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7566 cast_p, decltype_p, pidk);
7567 current.lhs_type = ERROR_MARK;
7568 current.prec = prec;
7569
7570 if (cp_parser_error_occurred (parser))
7571 return error_mark_node;
7572
7573 for (;;)
7574 {
7575 /* Get an operator token. */
7576 token = cp_lexer_peek_token (parser->lexer);
7577
7578 if (warn_cxx0x_compat
7579 && token->type == CPP_RSHIFT
7580 && !parser->greater_than_is_operator_p)
7581 {
7582 if (warning_at (token->location, OPT_Wc__0x_compat,
7583 "%<>>%> operator is treated"
7584 " as two right angle brackets in C++11"))
7585 inform (token->location,
7586 "suggest parentheses around %<>>%> expression");
7587 }
7588
7589 new_prec = TOKEN_PRECEDENCE (token);
7590
7591 /* Popping an entry off the stack means we completed a subexpression:
7592 - either we found a token which is not an operator (`>' where it is not
7593 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7594 will happen repeatedly;
7595 - or, we found an operator which has lower priority. This is the case
7596 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7597 parsing `3 * 4'. */
7598 if (new_prec <= current.prec)
7599 {
7600 if (sp == stack)
7601 break;
7602 else
7603 goto pop;
7604 }
7605
7606 get_rhs:
7607 current.tree_type = binops_by_token[token->type].tree_type;
7608 current.loc = token->location;
7609
7610 /* We used the operator token. */
7611 cp_lexer_consume_token (parser->lexer);
7612
7613 /* For "false && x" or "true || x", x will never be executed;
7614 disable warnings while evaluating it. */
7615 if (current.tree_type == TRUTH_ANDIF_EXPR)
7616 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7617 else if (current.tree_type == TRUTH_ORIF_EXPR)
7618 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7619
7620 /* Extract another operand. It may be the RHS of this expression
7621 or the LHS of a new, higher priority expression. */
7622 rhs = cp_parser_simple_cast_expression (parser);
7623 rhs_type = ERROR_MARK;
7624
7625 /* Get another operator token. Look up its precedence to avoid
7626 building a useless (immediately popped) stack entry for common
7627 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7628 token = cp_lexer_peek_token (parser->lexer);
7629 lookahead_prec = TOKEN_PRECEDENCE (token);
7630 if (lookahead_prec > new_prec)
7631 {
7632 /* ... and prepare to parse the RHS of the new, higher priority
7633 expression. Since precedence levels on the stack are
7634 monotonically increasing, we do not have to care about
7635 stack overflows. */
7636 *sp = current;
7637 ++sp;
7638 current.lhs = rhs;
7639 current.lhs_type = rhs_type;
7640 current.prec = new_prec;
7641 new_prec = lookahead_prec;
7642 goto get_rhs;
7643
7644 pop:
7645 lookahead_prec = new_prec;
7646 /* If the stack is not empty, we have parsed into LHS the right side
7647 (`4' in the example above) of an expression we had suspended.
7648 We can use the information on the stack to recover the LHS (`3')
7649 from the stack together with the tree code (`MULT_EXPR'), and
7650 the precedence of the higher level subexpression
7651 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7652 which will be used to actually build the additive expression. */
7653 rhs = current.lhs;
7654 rhs_type = current.lhs_type;
7655 --sp;
7656 current = *sp;
7657 }
7658
7659 /* Undo the disabling of warnings done above. */
7660 if (current.tree_type == TRUTH_ANDIF_EXPR)
7661 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7662 else if (current.tree_type == TRUTH_ORIF_EXPR)
7663 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7664
7665 overload = NULL;
7666 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7667 ERROR_MARK for everything that is not a binary expression.
7668 This makes warn_about_parentheses miss some warnings that
7669 involve unary operators. For unary expressions we should
7670 pass the correct tree_code unless the unary expression was
7671 surrounded by parentheses.
7672 */
7673 if (no_toplevel_fold_p
7674 && lookahead_prec <= current.prec
7675 && sp == stack
7676 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7677 current.lhs = build2 (current.tree_type, boolean_type_node,
7678 current.lhs, rhs);
7679 else
7680 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7681 current.lhs, current.lhs_type,
7682 rhs, rhs_type, &overload,
7683 complain_flags (decltype_p));
7684 current.lhs_type = current.tree_type;
7685 if (EXPR_P (current.lhs))
7686 SET_EXPR_LOCATION (current.lhs, current.loc);
7687
7688 /* If the binary operator required the use of an overloaded operator,
7689 then this expression cannot be an integral constant-expression.
7690 An overloaded operator can be used even if both operands are
7691 otherwise permissible in an integral constant-expression if at
7692 least one of the operands is of enumeration type. */
7693
7694 if (overload
7695 && cp_parser_non_integral_constant_expression (parser,
7696 NIC_OVERLOADED))
7697 return error_mark_node;
7698 }
7699
7700 return current.lhs;
7701 }
7702
7703 static tree
7704 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7705 bool no_toplevel_fold_p,
7706 enum cp_parser_prec prec,
7707 cp_id_kind * pidk)
7708 {
7709 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7710 /*decltype*/false, prec, pidk);
7711 }
7712
7713 /* Parse the `? expression : assignment-expression' part of a
7714 conditional-expression. The LOGICAL_OR_EXPR is the
7715 logical-or-expression that started the conditional-expression.
7716 Returns a representation of the entire conditional-expression.
7717
7718 This routine is used by cp_parser_assignment_expression.
7719
7720 ? expression : assignment-expression
7721
7722 GNU Extensions:
7723
7724 ? : assignment-expression */
7725
7726 static tree
7727 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7728 {
7729 tree expr;
7730 tree assignment_expr;
7731 struct cp_token *token;
7732 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7733
7734 /* Consume the `?' token. */
7735 cp_lexer_consume_token (parser->lexer);
7736 token = cp_lexer_peek_token (parser->lexer);
7737 if (cp_parser_allow_gnu_extensions_p (parser)
7738 && token->type == CPP_COLON)
7739 {
7740 pedwarn (token->location, OPT_Wpedantic,
7741 "ISO C++ does not allow ?: with omitted middle operand");
7742 /* Implicit true clause. */
7743 expr = NULL_TREE;
7744 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7745 warn_for_omitted_condop (token->location, logical_or_expr);
7746 }
7747 else
7748 {
7749 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7750 parser->colon_corrects_to_scope_p = false;
7751 /* Parse the expression. */
7752 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7753 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7754 c_inhibit_evaluation_warnings +=
7755 ((logical_or_expr == truthvalue_true_node)
7756 - (logical_or_expr == truthvalue_false_node));
7757 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7758 }
7759
7760 /* The next token should be a `:'. */
7761 cp_parser_require (parser, CPP_COLON, RT_COLON);
7762 /* Parse the assignment-expression. */
7763 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7764 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7765
7766 /* Build the conditional-expression. */
7767 return build_x_conditional_expr (loc, logical_or_expr,
7768 expr,
7769 assignment_expr,
7770 tf_warning_or_error);
7771 }
7772
7773 /* Parse an assignment-expression.
7774
7775 assignment-expression:
7776 conditional-expression
7777 logical-or-expression assignment-operator assignment_expression
7778 throw-expression
7779
7780 CAST_P is true if this expression is the target of a cast.
7781 DECLTYPE_P is true if this expression is the operand of decltype.
7782
7783 Returns a representation for the expression. */
7784
7785 static tree
7786 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7787 bool decltype_p, cp_id_kind * pidk)
7788 {
7789 tree expr;
7790
7791 /* If the next token is the `throw' keyword, then we're looking at
7792 a throw-expression. */
7793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7794 expr = cp_parser_throw_expression (parser);
7795 /* Otherwise, it must be that we are looking at a
7796 logical-or-expression. */
7797 else
7798 {
7799 /* Parse the binary expressions (logical-or-expression). */
7800 expr = cp_parser_binary_expression (parser, cast_p, false,
7801 decltype_p,
7802 PREC_NOT_OPERATOR, pidk);
7803 /* If the next token is a `?' then we're actually looking at a
7804 conditional-expression. */
7805 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7806 return cp_parser_question_colon_clause (parser, expr);
7807 else
7808 {
7809 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7810
7811 /* If it's an assignment-operator, we're using the second
7812 production. */
7813 enum tree_code assignment_operator
7814 = cp_parser_assignment_operator_opt (parser);
7815 if (assignment_operator != ERROR_MARK)
7816 {
7817 bool non_constant_p;
7818 location_t saved_input_location;
7819
7820 /* Parse the right-hand side of the assignment. */
7821 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7822
7823 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7824 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7825
7826 /* An assignment may not appear in a
7827 constant-expression. */
7828 if (cp_parser_non_integral_constant_expression (parser,
7829 NIC_ASSIGNMENT))
7830 return error_mark_node;
7831 /* Build the assignment expression. Its default
7832 location is the location of the '=' token. */
7833 saved_input_location = input_location;
7834 input_location = loc;
7835 expr = build_x_modify_expr (loc, expr,
7836 assignment_operator,
7837 rhs,
7838 complain_flags (decltype_p));
7839 input_location = saved_input_location;
7840 }
7841 }
7842 }
7843
7844 return expr;
7845 }
7846
7847 static tree
7848 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7849 cp_id_kind * pidk)
7850 {
7851 return cp_parser_assignment_expression (parser, cast_p,
7852 /*decltype*/false, pidk);
7853 }
7854
7855 /* Parse an (optional) assignment-operator.
7856
7857 assignment-operator: one of
7858 = *= /= %= += -= >>= <<= &= ^= |=
7859
7860 GNU Extension:
7861
7862 assignment-operator: one of
7863 <?= >?=
7864
7865 If the next token is an assignment operator, the corresponding tree
7866 code is returned, and the token is consumed. For example, for
7867 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7868 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7869 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7870 operator, ERROR_MARK is returned. */
7871
7872 static enum tree_code
7873 cp_parser_assignment_operator_opt (cp_parser* parser)
7874 {
7875 enum tree_code op;
7876 cp_token *token;
7877
7878 /* Peek at the next token. */
7879 token = cp_lexer_peek_token (parser->lexer);
7880
7881 switch (token->type)
7882 {
7883 case CPP_EQ:
7884 op = NOP_EXPR;
7885 break;
7886
7887 case CPP_MULT_EQ:
7888 op = MULT_EXPR;
7889 break;
7890
7891 case CPP_DIV_EQ:
7892 op = TRUNC_DIV_EXPR;
7893 break;
7894
7895 case CPP_MOD_EQ:
7896 op = TRUNC_MOD_EXPR;
7897 break;
7898
7899 case CPP_PLUS_EQ:
7900 op = PLUS_EXPR;
7901 break;
7902
7903 case CPP_MINUS_EQ:
7904 op = MINUS_EXPR;
7905 break;
7906
7907 case CPP_RSHIFT_EQ:
7908 op = RSHIFT_EXPR;
7909 break;
7910
7911 case CPP_LSHIFT_EQ:
7912 op = LSHIFT_EXPR;
7913 break;
7914
7915 case CPP_AND_EQ:
7916 op = BIT_AND_EXPR;
7917 break;
7918
7919 case CPP_XOR_EQ:
7920 op = BIT_XOR_EXPR;
7921 break;
7922
7923 case CPP_OR_EQ:
7924 op = BIT_IOR_EXPR;
7925 break;
7926
7927 default:
7928 /* Nothing else is an assignment operator. */
7929 op = ERROR_MARK;
7930 }
7931
7932 /* If it was an assignment operator, consume it. */
7933 if (op != ERROR_MARK)
7934 cp_lexer_consume_token (parser->lexer);
7935
7936 return op;
7937 }
7938
7939 /* Parse an expression.
7940
7941 expression:
7942 assignment-expression
7943 expression , assignment-expression
7944
7945 CAST_P is true if this expression is the target of a cast.
7946 DECLTYPE_P is true if this expression is the immediate operand of decltype,
7947 except possibly parenthesized or on the RHS of a comma (N3276).
7948
7949 Returns a representation of the expression. */
7950
7951 static tree
7952 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
7953 cp_id_kind * pidk)
7954 {
7955 tree expression = NULL_TREE;
7956 location_t loc = UNKNOWN_LOCATION;
7957
7958 while (true)
7959 {
7960 tree assignment_expression;
7961
7962 /* Parse the next assignment-expression. */
7963 assignment_expression
7964 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
7965
7966 /* We don't create a temporary for a call that is the immediate operand
7967 of decltype or on the RHS of a comma. But when we see a comma, we
7968 need to create a temporary for a call on the LHS. */
7969 if (decltype_p && !processing_template_decl
7970 && TREE_CODE (assignment_expression) == CALL_EXPR
7971 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
7972 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7973 assignment_expression
7974 = build_cplus_new (TREE_TYPE (assignment_expression),
7975 assignment_expression, tf_warning_or_error);
7976
7977 /* If this is the first assignment-expression, we can just
7978 save it away. */
7979 if (!expression)
7980 expression = assignment_expression;
7981 else
7982 expression = build_x_compound_expr (loc, expression,
7983 assignment_expression,
7984 complain_flags (decltype_p));
7985 /* If the next token is not a comma, then we are done with the
7986 expression. */
7987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7988 break;
7989 /* Consume the `,'. */
7990 loc = cp_lexer_peek_token (parser->lexer)->location;
7991 cp_lexer_consume_token (parser->lexer);
7992 /* A comma operator cannot appear in a constant-expression. */
7993 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7994 expression = error_mark_node;
7995 }
7996
7997 return expression;
7998 }
7999
8000 static inline tree
8001 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8002 {
8003 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8004 }
8005
8006 /* Parse a constant-expression.
8007
8008 constant-expression:
8009 conditional-expression
8010
8011 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8012 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8013 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8014 is false, NON_CONSTANT_P should be NULL. */
8015
8016 static tree
8017 cp_parser_constant_expression (cp_parser* parser,
8018 bool allow_non_constant_p,
8019 bool *non_constant_p)
8020 {
8021 bool saved_integral_constant_expression_p;
8022 bool saved_allow_non_integral_constant_expression_p;
8023 bool saved_non_integral_constant_expression_p;
8024 tree expression;
8025
8026 /* It might seem that we could simply parse the
8027 conditional-expression, and then check to see if it were
8028 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8029 one that the compiler can figure out is constant, possibly after
8030 doing some simplifications or optimizations. The standard has a
8031 precise definition of constant-expression, and we must honor
8032 that, even though it is somewhat more restrictive.
8033
8034 For example:
8035
8036 int i[(2, 3)];
8037
8038 is not a legal declaration, because `(2, 3)' is not a
8039 constant-expression. The `,' operator is forbidden in a
8040 constant-expression. However, GCC's constant-folding machinery
8041 will fold this operation to an INTEGER_CST for `3'. */
8042
8043 /* Save the old settings. */
8044 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8045 saved_allow_non_integral_constant_expression_p
8046 = parser->allow_non_integral_constant_expression_p;
8047 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8048 /* We are now parsing a constant-expression. */
8049 parser->integral_constant_expression_p = true;
8050 parser->allow_non_integral_constant_expression_p
8051 = (allow_non_constant_p || cxx_dialect >= cxx0x);
8052 parser->non_integral_constant_expression_p = false;
8053 /* Although the grammar says "conditional-expression", we parse an
8054 "assignment-expression", which also permits "throw-expression"
8055 and the use of assignment operators. In the case that
8056 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8057 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8058 actually essential that we look for an assignment-expression.
8059 For example, cp_parser_initializer_clauses uses this function to
8060 determine whether a particular assignment-expression is in fact
8061 constant. */
8062 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8063 /* Restore the old settings. */
8064 parser->integral_constant_expression_p
8065 = saved_integral_constant_expression_p;
8066 parser->allow_non_integral_constant_expression_p
8067 = saved_allow_non_integral_constant_expression_p;
8068 if (cxx_dialect >= cxx0x)
8069 {
8070 /* Require an rvalue constant expression here; that's what our
8071 callers expect. Reference constant expressions are handled
8072 separately in e.g. cp_parser_template_argument. */
8073 bool is_const = potential_rvalue_constant_expression (expression);
8074 parser->non_integral_constant_expression_p = !is_const;
8075 if (!is_const && !allow_non_constant_p)
8076 require_potential_rvalue_constant_expression (expression);
8077 }
8078 if (allow_non_constant_p)
8079 *non_constant_p = parser->non_integral_constant_expression_p;
8080 parser->non_integral_constant_expression_p
8081 = saved_non_integral_constant_expression_p;
8082
8083 return expression;
8084 }
8085
8086 /* Parse __builtin_offsetof.
8087
8088 offsetof-expression:
8089 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8090
8091 offsetof-member-designator:
8092 id-expression
8093 | offsetof-member-designator "." id-expression
8094 | offsetof-member-designator "[" expression "]"
8095 | offsetof-member-designator "->" id-expression */
8096
8097 static tree
8098 cp_parser_builtin_offsetof (cp_parser *parser)
8099 {
8100 int save_ice_p, save_non_ice_p;
8101 tree type, expr;
8102 cp_id_kind dummy;
8103 cp_token *token;
8104
8105 /* We're about to accept non-integral-constant things, but will
8106 definitely yield an integral constant expression. Save and
8107 restore these values around our local parsing. */
8108 save_ice_p = parser->integral_constant_expression_p;
8109 save_non_ice_p = parser->non_integral_constant_expression_p;
8110
8111 /* Consume the "__builtin_offsetof" token. */
8112 cp_lexer_consume_token (parser->lexer);
8113 /* Consume the opening `('. */
8114 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8115 /* Parse the type-id. */
8116 type = cp_parser_type_id (parser);
8117 /* Look for the `,'. */
8118 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8119 token = cp_lexer_peek_token (parser->lexer);
8120
8121 /* Build the (type *)null that begins the traditional offsetof macro. */
8122 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8123 tf_warning_or_error);
8124
8125 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8126 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8127 true, &dummy, token->location);
8128 while (true)
8129 {
8130 token = cp_lexer_peek_token (parser->lexer);
8131 switch (token->type)
8132 {
8133 case CPP_OPEN_SQUARE:
8134 /* offsetof-member-designator "[" expression "]" */
8135 expr = cp_parser_postfix_open_square_expression (parser, expr,
8136 true, false);
8137 break;
8138
8139 case CPP_DEREF:
8140 /* offsetof-member-designator "->" identifier */
8141 expr = grok_array_decl (token->location, expr,
8142 integer_zero_node, false);
8143 /* FALLTHRU */
8144
8145 case CPP_DOT:
8146 /* offsetof-member-designator "." identifier */
8147 cp_lexer_consume_token (parser->lexer);
8148 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8149 expr, true, &dummy,
8150 token->location);
8151 break;
8152
8153 case CPP_CLOSE_PAREN:
8154 /* Consume the ")" token. */
8155 cp_lexer_consume_token (parser->lexer);
8156 goto success;
8157
8158 default:
8159 /* Error. We know the following require will fail, but
8160 that gives the proper error message. */
8161 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8162 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8163 expr = error_mark_node;
8164 goto failure;
8165 }
8166 }
8167
8168 success:
8169 /* If we're processing a template, we can't finish the semantics yet.
8170 Otherwise we can fold the entire expression now. */
8171 if (processing_template_decl)
8172 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8173 else
8174 expr = finish_offsetof (expr);
8175
8176 failure:
8177 parser->integral_constant_expression_p = save_ice_p;
8178 parser->non_integral_constant_expression_p = save_non_ice_p;
8179
8180 return expr;
8181 }
8182
8183 /* Parse a trait expression.
8184
8185 Returns a representation of the expression, the underlying type
8186 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8187
8188 static tree
8189 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8190 {
8191 cp_trait_kind kind;
8192 tree type1, type2 = NULL_TREE;
8193 bool binary = false;
8194 cp_decl_specifier_seq decl_specs;
8195
8196 switch (keyword)
8197 {
8198 case RID_HAS_NOTHROW_ASSIGN:
8199 kind = CPTK_HAS_NOTHROW_ASSIGN;
8200 break;
8201 case RID_HAS_NOTHROW_CONSTRUCTOR:
8202 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8203 break;
8204 case RID_HAS_NOTHROW_COPY:
8205 kind = CPTK_HAS_NOTHROW_COPY;
8206 break;
8207 case RID_HAS_TRIVIAL_ASSIGN:
8208 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8209 break;
8210 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8211 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8212 break;
8213 case RID_HAS_TRIVIAL_COPY:
8214 kind = CPTK_HAS_TRIVIAL_COPY;
8215 break;
8216 case RID_HAS_TRIVIAL_DESTRUCTOR:
8217 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8218 break;
8219 case RID_HAS_VIRTUAL_DESTRUCTOR:
8220 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8221 break;
8222 case RID_IS_ABSTRACT:
8223 kind = CPTK_IS_ABSTRACT;
8224 break;
8225 case RID_IS_BASE_OF:
8226 kind = CPTK_IS_BASE_OF;
8227 binary = true;
8228 break;
8229 case RID_IS_CLASS:
8230 kind = CPTK_IS_CLASS;
8231 break;
8232 case RID_IS_CONVERTIBLE_TO:
8233 kind = CPTK_IS_CONVERTIBLE_TO;
8234 binary = true;
8235 break;
8236 case RID_IS_EMPTY:
8237 kind = CPTK_IS_EMPTY;
8238 break;
8239 case RID_IS_ENUM:
8240 kind = CPTK_IS_ENUM;
8241 break;
8242 case RID_IS_FINAL:
8243 kind = CPTK_IS_FINAL;
8244 break;
8245 case RID_IS_LITERAL_TYPE:
8246 kind = CPTK_IS_LITERAL_TYPE;
8247 break;
8248 case RID_IS_POD:
8249 kind = CPTK_IS_POD;
8250 break;
8251 case RID_IS_POLYMORPHIC:
8252 kind = CPTK_IS_POLYMORPHIC;
8253 break;
8254 case RID_IS_STD_LAYOUT:
8255 kind = CPTK_IS_STD_LAYOUT;
8256 break;
8257 case RID_IS_TRIVIAL:
8258 kind = CPTK_IS_TRIVIAL;
8259 break;
8260 case RID_IS_UNION:
8261 kind = CPTK_IS_UNION;
8262 break;
8263 case RID_UNDERLYING_TYPE:
8264 kind = CPTK_UNDERLYING_TYPE;
8265 break;
8266 case RID_BASES:
8267 kind = CPTK_BASES;
8268 break;
8269 case RID_DIRECT_BASES:
8270 kind = CPTK_DIRECT_BASES;
8271 break;
8272 default:
8273 gcc_unreachable ();
8274 }
8275
8276 /* Consume the token. */
8277 cp_lexer_consume_token (parser->lexer);
8278
8279 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8280
8281 type1 = cp_parser_type_id (parser);
8282
8283 if (type1 == error_mark_node)
8284 return error_mark_node;
8285
8286 /* Build a trivial decl-specifier-seq. */
8287 clear_decl_specs (&decl_specs);
8288 decl_specs.type = type1;
8289
8290 /* Call grokdeclarator to figure out what type this is. */
8291 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8292 /*initialized=*/0, /*attrlist=*/NULL);
8293
8294 if (binary)
8295 {
8296 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8297
8298 type2 = cp_parser_type_id (parser);
8299
8300 if (type2 == error_mark_node)
8301 return error_mark_node;
8302
8303 /* Build a trivial decl-specifier-seq. */
8304 clear_decl_specs (&decl_specs);
8305 decl_specs.type = type2;
8306
8307 /* Call grokdeclarator to figure out what type this is. */
8308 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8309 /*initialized=*/0, /*attrlist=*/NULL);
8310 }
8311
8312 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8313
8314 /* Complete the trait expression, which may mean either processing
8315 the trait expr now or saving it for template instantiation. */
8316 switch(kind)
8317 {
8318 case CPTK_UNDERLYING_TYPE:
8319 return finish_underlying_type (type1);
8320 case CPTK_BASES:
8321 return finish_bases (type1, false);
8322 case CPTK_DIRECT_BASES:
8323 return finish_bases (type1, true);
8324 default:
8325 return finish_trait_expr (kind, type1, type2);
8326 }
8327 }
8328
8329 /* Lambdas that appear in variable initializer or default argument scope
8330 get that in their mangling, so we need to record it. We might as well
8331 use the count for function and namespace scopes as well. */
8332 static GTY(()) tree lambda_scope;
8333 static GTY(()) int lambda_count;
8334 typedef struct GTY(()) tree_int
8335 {
8336 tree t;
8337 int i;
8338 } tree_int;
8339 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8340
8341 static void
8342 start_lambda_scope (tree decl)
8343 {
8344 tree_int ti;
8345 gcc_assert (decl);
8346 /* Once we're inside a function, we ignore other scopes and just push
8347 the function again so that popping works properly. */
8348 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8349 decl = current_function_decl;
8350 ti.t = lambda_scope;
8351 ti.i = lambda_count;
8352 vec_safe_push (lambda_scope_stack, ti);
8353 if (lambda_scope != decl)
8354 {
8355 /* Don't reset the count if we're still in the same function. */
8356 lambda_scope = decl;
8357 lambda_count = 0;
8358 }
8359 }
8360
8361 static void
8362 record_lambda_scope (tree lambda)
8363 {
8364 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8365 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8366 }
8367
8368 static void
8369 finish_lambda_scope (void)
8370 {
8371 tree_int *p = &lambda_scope_stack->last ();
8372 if (lambda_scope != p->t)
8373 {
8374 lambda_scope = p->t;
8375 lambda_count = p->i;
8376 }
8377 lambda_scope_stack->pop ();
8378 }
8379
8380 /* Parse a lambda expression.
8381
8382 lambda-expression:
8383 lambda-introducer lambda-declarator [opt] compound-statement
8384
8385 Returns a representation of the expression. */
8386
8387 static tree
8388 cp_parser_lambda_expression (cp_parser* parser)
8389 {
8390 tree lambda_expr = build_lambda_expr ();
8391 tree type;
8392 bool ok;
8393
8394 LAMBDA_EXPR_LOCATION (lambda_expr)
8395 = cp_lexer_peek_token (parser->lexer)->location;
8396
8397 if (cp_unevaluated_operand)
8398 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8399 "lambda-expression in unevaluated context");
8400
8401 /* We may be in the middle of deferred access check. Disable
8402 it now. */
8403 push_deferring_access_checks (dk_no_deferred);
8404
8405 cp_parser_lambda_introducer (parser, lambda_expr);
8406
8407 type = begin_lambda_type (lambda_expr);
8408 if (type == error_mark_node)
8409 return error_mark_node;
8410
8411 record_lambda_scope (lambda_expr);
8412
8413 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8414 determine_visibility (TYPE_NAME (type));
8415
8416 /* Now that we've started the type, add the capture fields for any
8417 explicit captures. */
8418 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8419
8420 {
8421 /* Inside the class, surrounding template-parameter-lists do not apply. */
8422 unsigned int saved_num_template_parameter_lists
8423 = parser->num_template_parameter_lists;
8424 unsigned char in_statement = parser->in_statement;
8425 bool in_switch_statement_p = parser->in_switch_statement_p;
8426
8427 parser->num_template_parameter_lists = 0;
8428 parser->in_statement = 0;
8429 parser->in_switch_statement_p = false;
8430
8431 /* By virtue of defining a local class, a lambda expression has access to
8432 the private variables of enclosing classes. */
8433
8434 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8435
8436 if (ok)
8437 cp_parser_lambda_body (parser, lambda_expr);
8438 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8439 cp_parser_skip_to_end_of_block_or_statement (parser);
8440
8441 /* The capture list was built up in reverse order; fix that now. */
8442 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8443 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8444
8445 if (ok)
8446 maybe_add_lambda_conv_op (type);
8447
8448 type = finish_struct (type, /*attributes=*/NULL_TREE);
8449
8450 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8451 parser->in_statement = in_statement;
8452 parser->in_switch_statement_p = in_switch_statement_p;
8453 }
8454
8455 pop_deferring_access_checks ();
8456
8457 /* This field is only used during parsing of the lambda. */
8458 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8459
8460 /* This lambda shouldn't have any proxies left at this point. */
8461 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8462 /* And now that we're done, push proxies for an enclosing lambda. */
8463 insert_pending_capture_proxies ();
8464
8465 if (ok)
8466 return build_lambda_object (lambda_expr);
8467 else
8468 return error_mark_node;
8469 }
8470
8471 /* Parse the beginning of a lambda expression.
8472
8473 lambda-introducer:
8474 [ lambda-capture [opt] ]
8475
8476 LAMBDA_EXPR is the current representation of the lambda expression. */
8477
8478 static void
8479 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8480 {
8481 /* Need commas after the first capture. */
8482 bool first = true;
8483
8484 /* Eat the leading `['. */
8485 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8486
8487 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8488 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8489 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8490 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8491 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8492 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8493
8494 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8495 {
8496 cp_lexer_consume_token (parser->lexer);
8497 first = false;
8498 }
8499
8500 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8501 {
8502 cp_token* capture_token;
8503 tree capture_id;
8504 tree capture_init_expr;
8505 cp_id_kind idk = CP_ID_KIND_NONE;
8506 bool explicit_init_p = false;
8507
8508 enum capture_kind_type
8509 {
8510 BY_COPY,
8511 BY_REFERENCE
8512 };
8513 enum capture_kind_type capture_kind = BY_COPY;
8514
8515 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8516 {
8517 error ("expected end of capture-list");
8518 return;
8519 }
8520
8521 if (first)
8522 first = false;
8523 else
8524 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8525
8526 /* Possibly capture `this'. */
8527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8528 {
8529 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8530 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8531 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8532 "with by-copy capture default");
8533 cp_lexer_consume_token (parser->lexer);
8534 add_capture (lambda_expr,
8535 /*id=*/this_identifier,
8536 /*initializer=*/finish_this_expr(),
8537 /*by_reference_p=*/false,
8538 explicit_init_p);
8539 continue;
8540 }
8541
8542 /* Remember whether we want to capture as a reference or not. */
8543 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8544 {
8545 capture_kind = BY_REFERENCE;
8546 cp_lexer_consume_token (parser->lexer);
8547 }
8548
8549 /* Get the identifier. */
8550 capture_token = cp_lexer_peek_token (parser->lexer);
8551 capture_id = cp_parser_identifier (parser);
8552
8553 if (capture_id == error_mark_node)
8554 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8555 delimiters, but I modified this to stop on unnested ']' as well. It
8556 was already changed to stop on unnested '}', so the
8557 "closing_parenthesis" name is no more misleading with my change. */
8558 {
8559 cp_parser_skip_to_closing_parenthesis (parser,
8560 /*recovering=*/true,
8561 /*or_comma=*/true,
8562 /*consume_paren=*/true);
8563 break;
8564 }
8565
8566 /* Find the initializer for this capture. */
8567 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8568 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8569 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8570 {
8571 bool direct, non_constant;
8572 /* An explicit initializer exists. */
8573 if (cxx_dialect < cxx1y)
8574 pedwarn (input_location, 0,
8575 "lambda capture initializers "
8576 "only available with -std=c++1y or -std=gnu++1y");
8577 capture_init_expr = cp_parser_initializer (parser, &direct,
8578 &non_constant);
8579 explicit_init_p = true;
8580 }
8581 else
8582 {
8583 const char* error_msg;
8584
8585 /* Turn the identifier into an id-expression. */
8586 capture_init_expr
8587 = cp_parser_lookup_name
8588 (parser,
8589 capture_id,
8590 none_type,
8591 /*is_template=*/false,
8592 /*is_namespace=*/false,
8593 /*check_dependency=*/true,
8594 /*ambiguous_decls=*/NULL,
8595 capture_token->location);
8596
8597 if (capture_init_expr == error_mark_node)
8598 {
8599 unqualified_name_lookup_error (capture_id);
8600 continue;
8601 }
8602 else if (DECL_P (capture_init_expr)
8603 && (!VAR_P (capture_init_expr)
8604 && TREE_CODE (capture_init_expr) != PARM_DECL))
8605 {
8606 error_at (capture_token->location,
8607 "capture of non-variable %qD ",
8608 capture_init_expr);
8609 inform (0, "%q+#D declared here", capture_init_expr);
8610 continue;
8611 }
8612 if (VAR_P (capture_init_expr)
8613 && decl_storage_duration (capture_init_expr) != dk_auto)
8614 {
8615 pedwarn (capture_token->location, 0, "capture of variable "
8616 "%qD with non-automatic storage duration",
8617 capture_init_expr);
8618 inform (0, "%q+#D declared here", capture_init_expr);
8619 continue;
8620 }
8621
8622 capture_init_expr
8623 = finish_id_expression
8624 (capture_id,
8625 capture_init_expr,
8626 parser->scope,
8627 &idk,
8628 /*integral_constant_expression_p=*/false,
8629 /*allow_non_integral_constant_expression_p=*/false,
8630 /*non_integral_constant_expression_p=*/NULL,
8631 /*template_p=*/false,
8632 /*done=*/true,
8633 /*address_p=*/false,
8634 /*template_arg_p=*/false,
8635 &error_msg,
8636 capture_token->location);
8637 }
8638
8639 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8640 && !explicit_init_p)
8641 {
8642 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8643 && capture_kind == BY_COPY)
8644 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8645 "of %qD redundant with by-copy capture default",
8646 capture_id);
8647 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8648 && capture_kind == BY_REFERENCE)
8649 pedwarn (capture_token->location, 0, "explicit by-reference "
8650 "capture of %qD redundant with by-reference capture "
8651 "default", capture_id);
8652 }
8653
8654 add_capture (lambda_expr,
8655 capture_id,
8656 capture_init_expr,
8657 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8658 explicit_init_p);
8659 }
8660
8661 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8662 }
8663
8664 /* Parse the (optional) middle of a lambda expression.
8665
8666 lambda-declarator:
8667 ( parameter-declaration-clause [opt] )
8668 attribute-specifier [opt]
8669 mutable [opt]
8670 exception-specification [opt]
8671 lambda-return-type-clause [opt]
8672
8673 LAMBDA_EXPR is the current representation of the lambda expression. */
8674
8675 static bool
8676 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8677 {
8678 /* 5.1.1.4 of the standard says:
8679 If a lambda-expression does not include a lambda-declarator, it is as if
8680 the lambda-declarator were ().
8681 This means an empty parameter list, no attributes, and no exception
8682 specification. */
8683 tree param_list = void_list_node;
8684 tree attributes = NULL_TREE;
8685 tree exception_spec = NULL_TREE;
8686 tree t;
8687
8688 /* The lambda-declarator is optional, but must begin with an opening
8689 parenthesis if present. */
8690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8691 {
8692 cp_lexer_consume_token (parser->lexer);
8693
8694 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8695
8696 /* Parse parameters. */
8697 param_list = cp_parser_parameter_declaration_clause (parser);
8698
8699 /* Default arguments shall not be specified in the
8700 parameter-declaration-clause of a lambda-declarator. */
8701 for (t = param_list; t; t = TREE_CHAIN (t))
8702 if (TREE_PURPOSE (t))
8703 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8704 "default argument specified for lambda parameter");
8705
8706 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8707
8708 attributes = cp_parser_attributes_opt (parser);
8709
8710 /* Parse optional `mutable' keyword. */
8711 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8712 {
8713 cp_lexer_consume_token (parser->lexer);
8714 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8715 }
8716
8717 /* Parse optional exception specification. */
8718 exception_spec = cp_parser_exception_specification_opt (parser);
8719
8720 /* Parse optional trailing return type. */
8721 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8722 {
8723 cp_lexer_consume_token (parser->lexer);
8724 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8725 = cp_parser_trailing_type_id (parser);
8726 }
8727
8728 /* The function parameters must be in scope all the way until after the
8729 trailing-return-type in case of decltype. */
8730 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8731 pop_binding (DECL_NAME (t), t);
8732
8733 leave_scope ();
8734 }
8735
8736 /* Create the function call operator.
8737
8738 Messing with declarators like this is no uglier than building up the
8739 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8740 other code. */
8741 {
8742 cp_decl_specifier_seq return_type_specs;
8743 cp_declarator* declarator;
8744 tree fco;
8745 int quals;
8746 void *p;
8747
8748 clear_decl_specs (&return_type_specs);
8749 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8750 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8751 else
8752 /* Maybe we will deduce the return type later. */
8753 return_type_specs.type = make_auto ();
8754
8755 p = obstack_alloc (&declarator_obstack, 0);
8756
8757 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8758 sfk_none);
8759
8760 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8761 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8762 declarator = make_call_declarator (declarator, param_list, quals,
8763 VIRT_SPEC_UNSPECIFIED,
8764 REF_QUAL_NONE,
8765 exception_spec,
8766 /*late_return_type=*/NULL_TREE);
8767 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8768
8769 fco = grokmethod (&return_type_specs,
8770 declarator,
8771 attributes);
8772 if (fco != error_mark_node)
8773 {
8774 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8775 DECL_ARTIFICIAL (fco) = 1;
8776 /* Give the object parameter a different name. */
8777 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8778 }
8779
8780 finish_member_declaration (fco);
8781
8782 obstack_free (&declarator_obstack, p);
8783
8784 return (fco != error_mark_node);
8785 }
8786 }
8787
8788 /* Parse the body of a lambda expression, which is simply
8789
8790 compound-statement
8791
8792 but which requires special handling.
8793 LAMBDA_EXPR is the current representation of the lambda expression. */
8794
8795 static void
8796 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8797 {
8798 bool nested = (current_function_decl != NULL_TREE);
8799 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8800 if (nested)
8801 push_function_context ();
8802 else
8803 /* Still increment function_depth so that we don't GC in the
8804 middle of an expression. */
8805 ++function_depth;
8806 /* Clear this in case we're in the middle of a default argument. */
8807 parser->local_variables_forbidden_p = false;
8808
8809 /* Finish the function call operator
8810 - class_specifier
8811 + late_parsing_for_member
8812 + function_definition_after_declarator
8813 + ctor_initializer_opt_and_function_body */
8814 {
8815 tree fco = lambda_function (lambda_expr);
8816 tree body;
8817 bool done = false;
8818 tree compound_stmt;
8819 tree cap;
8820
8821 /* Let the front end know that we are going to be defining this
8822 function. */
8823 start_preparsed_function (fco,
8824 NULL_TREE,
8825 SF_PRE_PARSED | SF_INCLASS_INLINE);
8826
8827 start_lambda_scope (fco);
8828 body = begin_function_body ();
8829
8830 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8831 goto out;
8832
8833 /* Push the proxies for any explicit captures. */
8834 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8835 cap = TREE_CHAIN (cap))
8836 build_capture_proxy (TREE_PURPOSE (cap));
8837
8838 compound_stmt = begin_compound_stmt (0);
8839
8840 /* 5.1.1.4 of the standard says:
8841 If a lambda-expression does not include a trailing-return-type, it
8842 is as if the trailing-return-type denotes the following type:
8843 * if the compound-statement is of the form
8844 { return attribute-specifier [opt] expression ; }
8845 the type of the returned expression after lvalue-to-rvalue
8846 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8847 (_conv.array_ 4.2), and function-to-pointer conversion
8848 (_conv.func_ 4.3);
8849 * otherwise, void. */
8850
8851 /* In a lambda that has neither a lambda-return-type-clause
8852 nor a deducible form, errors should be reported for return statements
8853 in the body. Since we used void as the placeholder return type, parsing
8854 the body as usual will give such desired behavior. */
8855 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8856 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8857 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8858 {
8859 tree expr = NULL_TREE;
8860 cp_id_kind idk = CP_ID_KIND_NONE;
8861
8862 /* Parse tentatively in case there's more after the initial return
8863 statement. */
8864 cp_parser_parse_tentatively (parser);
8865
8866 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8867
8868 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8869
8870 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8871 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8872
8873 if (cp_parser_parse_definitely (parser))
8874 {
8875 if (!processing_template_decl)
8876 apply_deduced_return_type (fco, lambda_return_type (expr));
8877
8878 /* Will get error here if type not deduced yet. */
8879 finish_return_stmt (expr);
8880
8881 done = true;
8882 }
8883 }
8884
8885 if (!done)
8886 {
8887 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8888 cp_parser_label_declaration (parser);
8889 cp_parser_statement_seq_opt (parser, NULL_TREE);
8890 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8891 }
8892
8893 finish_compound_stmt (compound_stmt);
8894
8895 out:
8896 finish_function_body (body);
8897 finish_lambda_scope ();
8898
8899 /* Finish the function and generate code for it if necessary. */
8900 expand_or_defer_fn (finish_function (/*inline*/2));
8901 }
8902
8903 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8904 if (nested)
8905 pop_function_context();
8906 else
8907 --function_depth;
8908 }
8909
8910 /* Statements [gram.stmt.stmt] */
8911
8912 /* Parse a statement.
8913
8914 statement:
8915 labeled-statement
8916 expression-statement
8917 compound-statement
8918 selection-statement
8919 iteration-statement
8920 jump-statement
8921 declaration-statement
8922 try-block
8923
8924 C++11:
8925
8926 statement:
8927 labeled-statement
8928 attribute-specifier-seq (opt) expression-statement
8929 attribute-specifier-seq (opt) compound-statement
8930 attribute-specifier-seq (opt) selection-statement
8931 attribute-specifier-seq (opt) iteration-statement
8932 attribute-specifier-seq (opt) jump-statement
8933 declaration-statement
8934 attribute-specifier-seq (opt) try-block
8935
8936 TM Extension:
8937
8938 statement:
8939 atomic-statement
8940
8941 IN_COMPOUND is true when the statement is nested inside a
8942 cp_parser_compound_statement; this matters for certain pragmas.
8943
8944 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8945 is a (possibly labeled) if statement which is not enclosed in braces
8946 and has an else clause. This is used to implement -Wparentheses. */
8947
8948 static void
8949 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8950 bool in_compound, bool *if_p)
8951 {
8952 tree statement, std_attrs = NULL_TREE;
8953 cp_token *token;
8954 location_t statement_location, attrs_location;
8955
8956 restart:
8957 if (if_p != NULL)
8958 *if_p = false;
8959 /* There is no statement yet. */
8960 statement = NULL_TREE;
8961
8962 cp_lexer_save_tokens (parser->lexer);
8963 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8964 if (c_dialect_objc ())
8965 /* In obj-c++, seing '[[' might be the either the beginning of
8966 c++11 attributes, or a nested objc-message-expression. So
8967 let's parse the c++11 attributes tentatively. */
8968 cp_parser_parse_tentatively (parser);
8969 std_attrs = cp_parser_std_attribute_spec_seq (parser);
8970 if (c_dialect_objc ())
8971 {
8972 if (!cp_parser_parse_definitely (parser))
8973 std_attrs = NULL_TREE;
8974 }
8975
8976 /* Peek at the next token. */
8977 token = cp_lexer_peek_token (parser->lexer);
8978 /* Remember the location of the first token in the statement. */
8979 statement_location = token->location;
8980 /* If this is a keyword, then that will often determine what kind of
8981 statement we have. */
8982 if (token->type == CPP_KEYWORD)
8983 {
8984 enum rid keyword = token->keyword;
8985
8986 switch (keyword)
8987 {
8988 case RID_CASE:
8989 case RID_DEFAULT:
8990 /* Looks like a labeled-statement with a case label.
8991 Parse the label, and then use tail recursion to parse
8992 the statement. */
8993 cp_parser_label_for_labeled_statement (parser, std_attrs);
8994 goto restart;
8995
8996 case RID_IF:
8997 case RID_SWITCH:
8998 statement = cp_parser_selection_statement (parser, if_p);
8999 break;
9000
9001 case RID_WHILE:
9002 case RID_DO:
9003 case RID_FOR:
9004 statement = cp_parser_iteration_statement (parser);
9005 break;
9006
9007 case RID_BREAK:
9008 case RID_CONTINUE:
9009 case RID_RETURN:
9010 case RID_GOTO:
9011 statement = cp_parser_jump_statement (parser);
9012 break;
9013
9014 /* Objective-C++ exception-handling constructs. */
9015 case RID_AT_TRY:
9016 case RID_AT_CATCH:
9017 case RID_AT_FINALLY:
9018 case RID_AT_SYNCHRONIZED:
9019 case RID_AT_THROW:
9020 statement = cp_parser_objc_statement (parser);
9021 break;
9022
9023 case RID_TRY:
9024 statement = cp_parser_try_block (parser);
9025 break;
9026
9027 case RID_NAMESPACE:
9028 /* This must be a namespace alias definition. */
9029 cp_parser_declaration_statement (parser);
9030 return;
9031
9032 case RID_TRANSACTION_ATOMIC:
9033 case RID_TRANSACTION_RELAXED:
9034 statement = cp_parser_transaction (parser, keyword);
9035 break;
9036 case RID_TRANSACTION_CANCEL:
9037 statement = cp_parser_transaction_cancel (parser);
9038 break;
9039
9040 default:
9041 /* It might be a keyword like `int' that can start a
9042 declaration-statement. */
9043 break;
9044 }
9045 }
9046 else if (token->type == CPP_NAME)
9047 {
9048 /* If the next token is a `:', then we are looking at a
9049 labeled-statement. */
9050 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9051 if (token->type == CPP_COLON)
9052 {
9053 /* Looks like a labeled-statement with an ordinary label.
9054 Parse the label, and then use tail recursion to parse
9055 the statement. */
9056
9057 cp_parser_label_for_labeled_statement (parser, std_attrs);
9058 goto restart;
9059 }
9060 }
9061 /* Anything that starts with a `{' must be a compound-statement. */
9062 else if (token->type == CPP_OPEN_BRACE)
9063 statement = cp_parser_compound_statement (parser, NULL, false, false);
9064 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9065 a statement all its own. */
9066 else if (token->type == CPP_PRAGMA)
9067 {
9068 /* Only certain OpenMP pragmas are attached to statements, and thus
9069 are considered statements themselves. All others are not. In
9070 the context of a compound, accept the pragma as a "statement" and
9071 return so that we can check for a close brace. Otherwise we
9072 require a real statement and must go back and read one. */
9073 if (in_compound)
9074 cp_parser_pragma (parser, pragma_compound);
9075 else if (!cp_parser_pragma (parser, pragma_stmt))
9076 goto restart;
9077 return;
9078 }
9079 else if (token->type == CPP_EOF)
9080 {
9081 cp_parser_error (parser, "expected statement");
9082 return;
9083 }
9084
9085 /* Everything else must be a declaration-statement or an
9086 expression-statement. Try for the declaration-statement
9087 first, unless we are looking at a `;', in which case we know that
9088 we have an expression-statement. */
9089 if (!statement)
9090 {
9091 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9092 {
9093 if (std_attrs != NULL_TREE)
9094 {
9095 /* Attributes should be parsed as part of the the
9096 declaration, so let's un-parse them. */
9097 cp_lexer_rollback_tokens (parser->lexer);
9098 std_attrs = NULL_TREE;
9099 }
9100
9101 cp_parser_parse_tentatively (parser);
9102 /* Try to parse the declaration-statement. */
9103 cp_parser_declaration_statement (parser);
9104 /* If that worked, we're done. */
9105 if (cp_parser_parse_definitely (parser))
9106 return;
9107 }
9108 /* Look for an expression-statement instead. */
9109 statement = cp_parser_expression_statement (parser, in_statement_expr);
9110 }
9111
9112 /* Set the line number for the statement. */
9113 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9114 SET_EXPR_LOCATION (statement, statement_location);
9115
9116 /* Note that for now, we don't do anything with c++11 statements
9117 parsed at this level. */
9118 if (std_attrs != NULL_TREE)
9119 warning_at (attrs_location,
9120 OPT_Wattributes,
9121 "attributes at the beginning of statement are ignored");
9122 }
9123
9124 /* Parse the label for a labeled-statement, i.e.
9125
9126 identifier :
9127 case constant-expression :
9128 default :
9129
9130 GNU Extension:
9131 case constant-expression ... constant-expression : statement
9132
9133 When a label is parsed without errors, the label is added to the
9134 parse tree by the finish_* functions, so this function doesn't
9135 have to return the label. */
9136
9137 static void
9138 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9139 {
9140 cp_token *token;
9141 tree label = NULL_TREE;
9142 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9143
9144 /* The next token should be an identifier. */
9145 token = cp_lexer_peek_token (parser->lexer);
9146 if (token->type != CPP_NAME
9147 && token->type != CPP_KEYWORD)
9148 {
9149 cp_parser_error (parser, "expected labeled-statement");
9150 return;
9151 }
9152
9153 parser->colon_corrects_to_scope_p = false;
9154 switch (token->keyword)
9155 {
9156 case RID_CASE:
9157 {
9158 tree expr, expr_hi;
9159 cp_token *ellipsis;
9160
9161 /* Consume the `case' token. */
9162 cp_lexer_consume_token (parser->lexer);
9163 /* Parse the constant-expression. */
9164 expr = cp_parser_constant_expression (parser,
9165 /*allow_non_constant_p=*/false,
9166 NULL);
9167
9168 ellipsis = cp_lexer_peek_token (parser->lexer);
9169 if (ellipsis->type == CPP_ELLIPSIS)
9170 {
9171 /* Consume the `...' token. */
9172 cp_lexer_consume_token (parser->lexer);
9173 expr_hi =
9174 cp_parser_constant_expression (parser,
9175 /*allow_non_constant_p=*/false,
9176 NULL);
9177 /* We don't need to emit warnings here, as the common code
9178 will do this for us. */
9179 }
9180 else
9181 expr_hi = NULL_TREE;
9182
9183 if (parser->in_switch_statement_p)
9184 finish_case_label (token->location, expr, expr_hi);
9185 else
9186 error_at (token->location,
9187 "case label %qE not within a switch statement",
9188 expr);
9189 }
9190 break;
9191
9192 case RID_DEFAULT:
9193 /* Consume the `default' token. */
9194 cp_lexer_consume_token (parser->lexer);
9195
9196 if (parser->in_switch_statement_p)
9197 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9198 else
9199 error_at (token->location, "case label not within a switch statement");
9200 break;
9201
9202 default:
9203 /* Anything else must be an ordinary label. */
9204 label = finish_label_stmt (cp_parser_identifier (parser));
9205 break;
9206 }
9207
9208 /* Require the `:' token. */
9209 cp_parser_require (parser, CPP_COLON, RT_COLON);
9210
9211 /* An ordinary label may optionally be followed by attributes.
9212 However, this is only permitted if the attributes are then
9213 followed by a semicolon. This is because, for backward
9214 compatibility, when parsing
9215 lab: __attribute__ ((unused)) int i;
9216 we want the attribute to attach to "i", not "lab". */
9217 if (label != NULL_TREE
9218 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9219 {
9220 tree attrs;
9221 cp_parser_parse_tentatively (parser);
9222 attrs = cp_parser_gnu_attributes_opt (parser);
9223 if (attrs == NULL_TREE
9224 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9225 cp_parser_abort_tentative_parse (parser);
9226 else if (!cp_parser_parse_definitely (parser))
9227 ;
9228 else
9229 attributes = chainon (attributes, attrs);
9230 }
9231
9232 if (attributes != NULL_TREE)
9233 cplus_decl_attributes (&label, attributes, 0);
9234
9235 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9236 }
9237
9238 /* Parse an expression-statement.
9239
9240 expression-statement:
9241 expression [opt] ;
9242
9243 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9244 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9245 indicates whether this expression-statement is part of an
9246 expression statement. */
9247
9248 static tree
9249 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9250 {
9251 tree statement = NULL_TREE;
9252 cp_token *token = cp_lexer_peek_token (parser->lexer);
9253
9254 /* If the next token is a ';', then there is no expression
9255 statement. */
9256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9257 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9258
9259 /* Give a helpful message for "A<T>::type t;" and the like. */
9260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9261 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9262 {
9263 if (TREE_CODE (statement) == SCOPE_REF)
9264 error_at (token->location, "need %<typename%> before %qE because "
9265 "%qT is a dependent scope",
9266 statement, TREE_OPERAND (statement, 0));
9267 else if (is_overloaded_fn (statement)
9268 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9269 {
9270 /* A::A a; */
9271 tree fn = get_first_fn (statement);
9272 error_at (token->location,
9273 "%<%T::%D%> names the constructor, not the type",
9274 DECL_CONTEXT (fn), DECL_NAME (fn));
9275 }
9276 }
9277
9278 /* Consume the final `;'. */
9279 cp_parser_consume_semicolon_at_end_of_statement (parser);
9280
9281 if (in_statement_expr
9282 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9283 /* This is the final expression statement of a statement
9284 expression. */
9285 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9286 else if (statement)
9287 statement = finish_expr_stmt (statement);
9288 else
9289 finish_stmt ();
9290
9291 return statement;
9292 }
9293
9294 /* Parse a compound-statement.
9295
9296 compound-statement:
9297 { statement-seq [opt] }
9298
9299 GNU extension:
9300
9301 compound-statement:
9302 { label-declaration-seq [opt] statement-seq [opt] }
9303
9304 label-declaration-seq:
9305 label-declaration
9306 label-declaration-seq label-declaration
9307
9308 Returns a tree representing the statement. */
9309
9310 static tree
9311 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9312 bool in_try, bool function_body)
9313 {
9314 tree compound_stmt;
9315
9316 /* Consume the `{'. */
9317 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9318 return error_mark_node;
9319 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9320 && !function_body)
9321 pedwarn (input_location, OPT_Wpedantic,
9322 "compound-statement in constexpr function");
9323 /* Begin the compound-statement. */
9324 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9325 /* If the next keyword is `__label__' we have a label declaration. */
9326 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9327 cp_parser_label_declaration (parser);
9328 /* Parse an (optional) statement-seq. */
9329 cp_parser_statement_seq_opt (parser, in_statement_expr);
9330 /* Finish the compound-statement. */
9331 finish_compound_stmt (compound_stmt);
9332 /* Consume the `}'. */
9333 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9334
9335 return compound_stmt;
9336 }
9337
9338 /* Parse an (optional) statement-seq.
9339
9340 statement-seq:
9341 statement
9342 statement-seq [opt] statement */
9343
9344 static void
9345 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9346 {
9347 /* Scan statements until there aren't any more. */
9348 while (true)
9349 {
9350 cp_token *token = cp_lexer_peek_token (parser->lexer);
9351
9352 /* If we are looking at a `}', then we have run out of
9353 statements; the same is true if we have reached the end
9354 of file, or have stumbled upon a stray '@end'. */
9355 if (token->type == CPP_CLOSE_BRACE
9356 || token->type == CPP_EOF
9357 || token->type == CPP_PRAGMA_EOL
9358 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9359 break;
9360
9361 /* If we are in a compound statement and find 'else' then
9362 something went wrong. */
9363 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9364 {
9365 if (parser->in_statement & IN_IF_STMT)
9366 break;
9367 else
9368 {
9369 token = cp_lexer_consume_token (parser->lexer);
9370 error_at (token->location, "%<else%> without a previous %<if%>");
9371 }
9372 }
9373
9374 /* Parse the statement. */
9375 cp_parser_statement (parser, in_statement_expr, true, NULL);
9376 }
9377 }
9378
9379 /* Parse a selection-statement.
9380
9381 selection-statement:
9382 if ( condition ) statement
9383 if ( condition ) statement else statement
9384 switch ( condition ) statement
9385
9386 Returns the new IF_STMT or SWITCH_STMT.
9387
9388 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9389 is a (possibly labeled) if statement which is not enclosed in
9390 braces and has an else clause. This is used to implement
9391 -Wparentheses. */
9392
9393 static tree
9394 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9395 {
9396 cp_token *token;
9397 enum rid keyword;
9398
9399 if (if_p != NULL)
9400 *if_p = false;
9401
9402 /* Peek at the next token. */
9403 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9404
9405 /* See what kind of keyword it is. */
9406 keyword = token->keyword;
9407 switch (keyword)
9408 {
9409 case RID_IF:
9410 case RID_SWITCH:
9411 {
9412 tree statement;
9413 tree condition;
9414
9415 /* Look for the `('. */
9416 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9417 {
9418 cp_parser_skip_to_end_of_statement (parser);
9419 return error_mark_node;
9420 }
9421
9422 /* Begin the selection-statement. */
9423 if (keyword == RID_IF)
9424 statement = begin_if_stmt ();
9425 else
9426 statement = begin_switch_stmt ();
9427
9428 /* Parse the condition. */
9429 condition = cp_parser_condition (parser);
9430 /* Look for the `)'. */
9431 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9432 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9433 /*consume_paren=*/true);
9434
9435 if (keyword == RID_IF)
9436 {
9437 bool nested_if;
9438 unsigned char in_statement;
9439
9440 /* Add the condition. */
9441 finish_if_stmt_cond (condition, statement);
9442
9443 /* Parse the then-clause. */
9444 in_statement = parser->in_statement;
9445 parser->in_statement |= IN_IF_STMT;
9446 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9447 {
9448 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9449 add_stmt (build_empty_stmt (loc));
9450 cp_lexer_consume_token (parser->lexer);
9451 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9452 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9453 "empty body in an %<if%> statement");
9454 nested_if = false;
9455 }
9456 else
9457 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9458 parser->in_statement = in_statement;
9459
9460 finish_then_clause (statement);
9461
9462 /* If the next token is `else', parse the else-clause. */
9463 if (cp_lexer_next_token_is_keyword (parser->lexer,
9464 RID_ELSE))
9465 {
9466 /* Consume the `else' keyword. */
9467 cp_lexer_consume_token (parser->lexer);
9468 begin_else_clause (statement);
9469 /* Parse the else-clause. */
9470 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9471 {
9472 location_t loc;
9473 loc = cp_lexer_peek_token (parser->lexer)->location;
9474 warning_at (loc,
9475 OPT_Wempty_body, "suggest braces around "
9476 "empty body in an %<else%> statement");
9477 add_stmt (build_empty_stmt (loc));
9478 cp_lexer_consume_token (parser->lexer);
9479 }
9480 else
9481 cp_parser_implicitly_scoped_statement (parser, NULL);
9482
9483 finish_else_clause (statement);
9484
9485 /* If we are currently parsing a then-clause, then
9486 IF_P will not be NULL. We set it to true to
9487 indicate that this if statement has an else clause.
9488 This may trigger the Wparentheses warning below
9489 when we get back up to the parent if statement. */
9490 if (if_p != NULL)
9491 *if_p = true;
9492 }
9493 else
9494 {
9495 /* This if statement does not have an else clause. If
9496 NESTED_IF is true, then the then-clause is an if
9497 statement which does have an else clause. We warn
9498 about the potential ambiguity. */
9499 if (nested_if)
9500 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9501 "suggest explicit braces to avoid ambiguous"
9502 " %<else%>");
9503 }
9504
9505 /* Now we're all done with the if-statement. */
9506 finish_if_stmt (statement);
9507 }
9508 else
9509 {
9510 bool in_switch_statement_p;
9511 unsigned char in_statement;
9512
9513 /* Add the condition. */
9514 finish_switch_cond (condition, statement);
9515
9516 /* Parse the body of the switch-statement. */
9517 in_switch_statement_p = parser->in_switch_statement_p;
9518 in_statement = parser->in_statement;
9519 parser->in_switch_statement_p = true;
9520 parser->in_statement |= IN_SWITCH_STMT;
9521 cp_parser_implicitly_scoped_statement (parser, NULL);
9522 parser->in_switch_statement_p = in_switch_statement_p;
9523 parser->in_statement = in_statement;
9524
9525 /* Now we're all done with the switch-statement. */
9526 finish_switch_stmt (statement);
9527 }
9528
9529 return statement;
9530 }
9531 break;
9532
9533 default:
9534 cp_parser_error (parser, "expected selection-statement");
9535 return error_mark_node;
9536 }
9537 }
9538
9539 /* Parse a condition.
9540
9541 condition:
9542 expression
9543 type-specifier-seq declarator = initializer-clause
9544 type-specifier-seq declarator braced-init-list
9545
9546 GNU Extension:
9547
9548 condition:
9549 type-specifier-seq declarator asm-specification [opt]
9550 attributes [opt] = assignment-expression
9551
9552 Returns the expression that should be tested. */
9553
9554 static tree
9555 cp_parser_condition (cp_parser* parser)
9556 {
9557 cp_decl_specifier_seq type_specifiers;
9558 const char *saved_message;
9559 int declares_class_or_enum;
9560
9561 /* Try the declaration first. */
9562 cp_parser_parse_tentatively (parser);
9563 /* New types are not allowed in the type-specifier-seq for a
9564 condition. */
9565 saved_message = parser->type_definition_forbidden_message;
9566 parser->type_definition_forbidden_message
9567 = G_("types may not be defined in conditions");
9568 /* Parse the type-specifier-seq. */
9569 cp_parser_decl_specifier_seq (parser,
9570 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9571 &type_specifiers,
9572 &declares_class_or_enum);
9573 /* Restore the saved message. */
9574 parser->type_definition_forbidden_message = saved_message;
9575 /* If all is well, we might be looking at a declaration. */
9576 if (!cp_parser_error_occurred (parser))
9577 {
9578 tree decl;
9579 tree asm_specification;
9580 tree attributes;
9581 cp_declarator *declarator;
9582 tree initializer = NULL_TREE;
9583
9584 /* Parse the declarator. */
9585 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9586 /*ctor_dtor_or_conv_p=*/NULL,
9587 /*parenthesized_p=*/NULL,
9588 /*member_p=*/false);
9589 /* Parse the attributes. */
9590 attributes = cp_parser_attributes_opt (parser);
9591 /* Parse the asm-specification. */
9592 asm_specification = cp_parser_asm_specification_opt (parser);
9593 /* If the next token is not an `=' or '{', then we might still be
9594 looking at an expression. For example:
9595
9596 if (A(a).x)
9597
9598 looks like a decl-specifier-seq and a declarator -- but then
9599 there is no `=', so this is an expression. */
9600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9601 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9602 cp_parser_simulate_error (parser);
9603
9604 /* If we did see an `=' or '{', then we are looking at a declaration
9605 for sure. */
9606 if (cp_parser_parse_definitely (parser))
9607 {
9608 tree pushed_scope;
9609 bool non_constant_p;
9610 bool flags = LOOKUP_ONLYCONVERTING;
9611
9612 /* Create the declaration. */
9613 decl = start_decl (declarator, &type_specifiers,
9614 /*initialized_p=*/true,
9615 attributes, /*prefix_attributes=*/NULL_TREE,
9616 &pushed_scope);
9617
9618 /* Parse the initializer. */
9619 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9620 {
9621 initializer = cp_parser_braced_list (parser, &non_constant_p);
9622 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9623 flags = 0;
9624 }
9625 else
9626 {
9627 /* Consume the `='. */
9628 cp_parser_require (parser, CPP_EQ, RT_EQ);
9629 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9630 }
9631 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9632 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9633
9634 /* Process the initializer. */
9635 cp_finish_decl (decl,
9636 initializer, !non_constant_p,
9637 asm_specification,
9638 flags);
9639
9640 if (pushed_scope)
9641 pop_scope (pushed_scope);
9642
9643 return convert_from_reference (decl);
9644 }
9645 }
9646 /* If we didn't even get past the declarator successfully, we are
9647 definitely not looking at a declaration. */
9648 else
9649 cp_parser_abort_tentative_parse (parser);
9650
9651 /* Otherwise, we are looking at an expression. */
9652 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9653 }
9654
9655 /* Parses a for-statement or range-for-statement until the closing ')',
9656 not included. */
9657
9658 static tree
9659 cp_parser_for (cp_parser *parser)
9660 {
9661 tree init, scope, decl;
9662 bool is_range_for;
9663
9664 /* Begin the for-statement. */
9665 scope = begin_for_scope (&init);
9666
9667 /* Parse the initialization. */
9668 is_range_for = cp_parser_for_init_statement (parser, &decl);
9669
9670 if (is_range_for)
9671 return cp_parser_range_for (parser, scope, init, decl);
9672 else
9673 return cp_parser_c_for (parser, scope, init);
9674 }
9675
9676 static tree
9677 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9678 {
9679 /* Normal for loop */
9680 tree condition = NULL_TREE;
9681 tree expression = NULL_TREE;
9682 tree stmt;
9683
9684 stmt = begin_for_stmt (scope, init);
9685 /* The for-init-statement has already been parsed in
9686 cp_parser_for_init_statement, so no work is needed here. */
9687 finish_for_init_stmt (stmt);
9688
9689 /* If there's a condition, process it. */
9690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9691 condition = cp_parser_condition (parser);
9692 finish_for_cond (condition, stmt);
9693 /* Look for the `;'. */
9694 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9695
9696 /* If there's an expression, process it. */
9697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9698 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9699 finish_for_expr (expression, stmt);
9700
9701 return stmt;
9702 }
9703
9704 /* Tries to parse a range-based for-statement:
9705
9706 range-based-for:
9707 decl-specifier-seq declarator : expression
9708
9709 The decl-specifier-seq declarator and the `:' are already parsed by
9710 cp_parser_for_init_statement. If processing_template_decl it returns a
9711 newly created RANGE_FOR_STMT; if not, it is converted to a
9712 regular FOR_STMT. */
9713
9714 static tree
9715 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9716 {
9717 tree stmt, range_expr;
9718
9719 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9720 {
9721 bool expr_non_constant_p;
9722 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9723 }
9724 else
9725 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9726
9727 /* If in template, STMT is converted to a normal for-statement
9728 at instantiation. If not, it is done just ahead. */
9729 if (processing_template_decl)
9730 {
9731 if (check_for_bare_parameter_packs (range_expr))
9732 range_expr = error_mark_node;
9733 stmt = begin_range_for_stmt (scope, init);
9734 finish_range_for_decl (stmt, range_decl, range_expr);
9735 if (range_expr != error_mark_node
9736 && !type_dependent_expression_p (range_expr)
9737 /* The length of an array might be dependent. */
9738 && COMPLETE_TYPE_P (complete_type (TREE_TYPE (range_expr)))
9739 /* do_auto_deduction doesn't mess with template init-lists. */
9740 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9741 do_range_for_auto_deduction (range_decl, range_expr);
9742 }
9743 else
9744 {
9745 stmt = begin_for_stmt (scope, init);
9746 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9747 }
9748 return stmt;
9749 }
9750
9751 /* Subroutine of cp_convert_range_for: given the initializer expression,
9752 builds up the range temporary. */
9753
9754 static tree
9755 build_range_temp (tree range_expr)
9756 {
9757 tree range_type, range_temp;
9758
9759 /* Find out the type deduced by the declaration
9760 `auto &&__range = range_expr'. */
9761 range_type = cp_build_reference_type (make_auto (), true);
9762 range_type = do_auto_deduction (range_type, range_expr,
9763 type_uses_auto (range_type));
9764
9765 /* Create the __range variable. */
9766 range_temp = build_decl (input_location, VAR_DECL,
9767 get_identifier ("__for_range"), range_type);
9768 TREE_USED (range_temp) = 1;
9769 DECL_ARTIFICIAL (range_temp) = 1;
9770
9771 return range_temp;
9772 }
9773
9774 /* Used by cp_parser_range_for in template context: we aren't going to
9775 do a full conversion yet, but we still need to resolve auto in the
9776 type of the for-range-declaration if present. This is basically
9777 a shortcut version of cp_convert_range_for. */
9778
9779 static void
9780 do_range_for_auto_deduction (tree decl, tree range_expr)
9781 {
9782 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9783 if (auto_node)
9784 {
9785 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9786 range_temp = convert_from_reference (build_range_temp (range_expr));
9787 iter_type = (cp_parser_perform_range_for_lookup
9788 (range_temp, &begin_dummy, &end_dummy));
9789 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9790 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9791 tf_warning_or_error);
9792 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9793 iter_decl, auto_node);
9794 }
9795 }
9796
9797 /* Converts a range-based for-statement into a normal
9798 for-statement, as per the definition.
9799
9800 for (RANGE_DECL : RANGE_EXPR)
9801 BLOCK
9802
9803 should be equivalent to:
9804
9805 {
9806 auto &&__range = RANGE_EXPR;
9807 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9808 __begin != __end;
9809 ++__begin)
9810 {
9811 RANGE_DECL = *__begin;
9812 BLOCK
9813 }
9814 }
9815
9816 If RANGE_EXPR is an array:
9817 BEGIN_EXPR = __range
9818 END_EXPR = __range + ARRAY_SIZE(__range)
9819 Else if RANGE_EXPR has a member 'begin' or 'end':
9820 BEGIN_EXPR = __range.begin()
9821 END_EXPR = __range.end()
9822 Else:
9823 BEGIN_EXPR = begin(__range)
9824 END_EXPR = end(__range);
9825
9826 If __range has a member 'begin' but not 'end', or vice versa, we must
9827 still use the second alternative (it will surely fail, however).
9828 When calling begin()/end() in the third alternative we must use
9829 argument dependent lookup, but always considering 'std' as an associated
9830 namespace. */
9831
9832 tree
9833 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9834 {
9835 tree begin, end;
9836 tree iter_type, begin_expr, end_expr;
9837 tree condition, expression;
9838
9839 if (range_decl == error_mark_node || range_expr == error_mark_node)
9840 /* If an error happened previously do nothing or else a lot of
9841 unhelpful errors would be issued. */
9842 begin_expr = end_expr = iter_type = error_mark_node;
9843 else
9844 {
9845 tree range_temp;
9846
9847 if (TREE_CODE (range_expr) == VAR_DECL
9848 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
9849 /* Can't bind a reference to an array of runtime bound. */
9850 range_temp = range_expr;
9851 else
9852 {
9853 range_temp = build_range_temp (range_expr);
9854 pushdecl (range_temp);
9855 cp_finish_decl (range_temp, range_expr,
9856 /*is_constant_init*/false, NULL_TREE,
9857 LOOKUP_ONLYCONVERTING);
9858 range_temp = convert_from_reference (range_temp);
9859 }
9860 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9861 &begin_expr, &end_expr);
9862 }
9863
9864 /* The new for initialization statement. */
9865 begin = build_decl (input_location, VAR_DECL,
9866 get_identifier ("__for_begin"), iter_type);
9867 TREE_USED (begin) = 1;
9868 DECL_ARTIFICIAL (begin) = 1;
9869 pushdecl (begin);
9870 cp_finish_decl (begin, begin_expr,
9871 /*is_constant_init*/false, NULL_TREE,
9872 LOOKUP_ONLYCONVERTING);
9873
9874 end = build_decl (input_location, VAR_DECL,
9875 get_identifier ("__for_end"), iter_type);
9876 TREE_USED (end) = 1;
9877 DECL_ARTIFICIAL (end) = 1;
9878 pushdecl (end);
9879 cp_finish_decl (end, end_expr,
9880 /*is_constant_init*/false, NULL_TREE,
9881 LOOKUP_ONLYCONVERTING);
9882
9883 finish_for_init_stmt (statement);
9884
9885 /* The new for condition. */
9886 condition = build_x_binary_op (input_location, NE_EXPR,
9887 begin, ERROR_MARK,
9888 end, ERROR_MARK,
9889 NULL, tf_warning_or_error);
9890 finish_for_cond (condition, statement);
9891
9892 /* The new increment expression. */
9893 expression = finish_unary_op_expr (input_location,
9894 PREINCREMENT_EXPR, begin,
9895 tf_warning_or_error);
9896 finish_for_expr (expression, statement);
9897
9898 /* The declaration is initialized with *__begin inside the loop body. */
9899 cp_finish_decl (range_decl,
9900 build_x_indirect_ref (input_location, begin, RO_NULL,
9901 tf_warning_or_error),
9902 /*is_constant_init*/false, NULL_TREE,
9903 LOOKUP_ONLYCONVERTING);
9904
9905 return statement;
9906 }
9907
9908 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9909 We need to solve both at the same time because the method used
9910 depends on the existence of members begin or end.
9911 Returns the type deduced for the iterator expression. */
9912
9913 static tree
9914 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9915 {
9916 if (error_operand_p (range))
9917 {
9918 *begin = *end = error_mark_node;
9919 return error_mark_node;
9920 }
9921
9922 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9923 {
9924 error ("range-based %<for%> expression of type %qT "
9925 "has incomplete type", TREE_TYPE (range));
9926 *begin = *end = error_mark_node;
9927 return error_mark_node;
9928 }
9929 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9930 {
9931 /* If RANGE is an array, we will use pointer arithmetic. */
9932 *begin = range;
9933 *end = build_binary_op (input_location, PLUS_EXPR,
9934 range,
9935 array_type_nelts_top (TREE_TYPE (range)),
9936 0);
9937 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9938 }
9939 else
9940 {
9941 /* If it is not an array, we must do a bit of magic. */
9942 tree id_begin, id_end;
9943 tree member_begin, member_end;
9944
9945 *begin = *end = error_mark_node;
9946
9947 id_begin = get_identifier ("begin");
9948 id_end = get_identifier ("end");
9949 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9950 /*protect=*/2, /*want_type=*/false,
9951 tf_warning_or_error);
9952 member_end = lookup_member (TREE_TYPE (range), id_end,
9953 /*protect=*/2, /*want_type=*/false,
9954 tf_warning_or_error);
9955
9956 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9957 {
9958 /* Use the member functions. */
9959 if (member_begin != NULL_TREE)
9960 *begin = cp_parser_range_for_member_function (range, id_begin);
9961 else
9962 error ("range-based %<for%> expression of type %qT has an "
9963 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9964
9965 if (member_end != NULL_TREE)
9966 *end = cp_parser_range_for_member_function (range, id_end);
9967 else
9968 error ("range-based %<for%> expression of type %qT has a "
9969 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9970 }
9971 else
9972 {
9973 /* Use global functions with ADL. */
9974 vec<tree, va_gc> *vec;
9975 vec = make_tree_vector ();
9976
9977 vec_safe_push (vec, range);
9978
9979 member_begin = perform_koenig_lookup (id_begin, vec,
9980 /*include_std=*/true,
9981 tf_warning_or_error);
9982 *begin = finish_call_expr (member_begin, &vec, false, true,
9983 tf_warning_or_error);
9984 member_end = perform_koenig_lookup (id_end, vec,
9985 /*include_std=*/true,
9986 tf_warning_or_error);
9987 *end = finish_call_expr (member_end, &vec, false, true,
9988 tf_warning_or_error);
9989
9990 release_tree_vector (vec);
9991 }
9992
9993 /* Last common checks. */
9994 if (*begin == error_mark_node || *end == error_mark_node)
9995 {
9996 /* If one of the expressions is an error do no more checks. */
9997 *begin = *end = error_mark_node;
9998 return error_mark_node;
9999 }
10000 else
10001 {
10002 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10003 /* The unqualified type of the __begin and __end temporaries should
10004 be the same, as required by the multiple auto declaration. */
10005 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10006 error ("inconsistent begin/end types in range-based %<for%> "
10007 "statement: %qT and %qT",
10008 TREE_TYPE (*begin), TREE_TYPE (*end));
10009 return iter_type;
10010 }
10011 }
10012 }
10013
10014 /* Helper function for cp_parser_perform_range_for_lookup.
10015 Builds a tree for RANGE.IDENTIFIER(). */
10016
10017 static tree
10018 cp_parser_range_for_member_function (tree range, tree identifier)
10019 {
10020 tree member, res;
10021 vec<tree, va_gc> *vec;
10022
10023 member = finish_class_member_access_expr (range, identifier,
10024 false, tf_warning_or_error);
10025 if (member == error_mark_node)
10026 return error_mark_node;
10027
10028 vec = make_tree_vector ();
10029 res = finish_call_expr (member, &vec,
10030 /*disallow_virtual=*/false,
10031 /*koenig_p=*/false,
10032 tf_warning_or_error);
10033 release_tree_vector (vec);
10034 return res;
10035 }
10036
10037 /* Parse an iteration-statement.
10038
10039 iteration-statement:
10040 while ( condition ) statement
10041 do statement while ( expression ) ;
10042 for ( for-init-statement condition [opt] ; expression [opt] )
10043 statement
10044
10045 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10046
10047 static tree
10048 cp_parser_iteration_statement (cp_parser* parser)
10049 {
10050 cp_token *token;
10051 enum rid keyword;
10052 tree statement;
10053 unsigned char in_statement;
10054
10055 /* Peek at the next token. */
10056 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10057 if (!token)
10058 return error_mark_node;
10059
10060 /* Remember whether or not we are already within an iteration
10061 statement. */
10062 in_statement = parser->in_statement;
10063
10064 /* See what kind of keyword it is. */
10065 keyword = token->keyword;
10066 switch (keyword)
10067 {
10068 case RID_WHILE:
10069 {
10070 tree condition;
10071
10072 /* Begin the while-statement. */
10073 statement = begin_while_stmt ();
10074 /* Look for the `('. */
10075 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10076 /* Parse the condition. */
10077 condition = cp_parser_condition (parser);
10078 finish_while_stmt_cond (condition, statement);
10079 /* Look for the `)'. */
10080 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10081 /* Parse the dependent statement. */
10082 parser->in_statement = IN_ITERATION_STMT;
10083 cp_parser_already_scoped_statement (parser);
10084 parser->in_statement = in_statement;
10085 /* We're done with the while-statement. */
10086 finish_while_stmt (statement);
10087 }
10088 break;
10089
10090 case RID_DO:
10091 {
10092 tree expression;
10093
10094 /* Begin the do-statement. */
10095 statement = begin_do_stmt ();
10096 /* Parse the body of the do-statement. */
10097 parser->in_statement = IN_ITERATION_STMT;
10098 cp_parser_implicitly_scoped_statement (parser, NULL);
10099 parser->in_statement = in_statement;
10100 finish_do_body (statement);
10101 /* Look for the `while' keyword. */
10102 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10103 /* Look for the `('. */
10104 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10105 /* Parse the expression. */
10106 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10107 /* We're done with the do-statement. */
10108 finish_do_stmt (expression, statement);
10109 /* Look for the `)'. */
10110 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10111 /* Look for the `;'. */
10112 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10113 }
10114 break;
10115
10116 case RID_FOR:
10117 {
10118 /* Look for the `('. */
10119 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10120
10121 statement = cp_parser_for (parser);
10122
10123 /* Look for the `)'. */
10124 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10125
10126 /* Parse the body of the for-statement. */
10127 parser->in_statement = IN_ITERATION_STMT;
10128 cp_parser_already_scoped_statement (parser);
10129 parser->in_statement = in_statement;
10130
10131 /* We're done with the for-statement. */
10132 finish_for_stmt (statement);
10133 }
10134 break;
10135
10136 default:
10137 cp_parser_error (parser, "expected iteration-statement");
10138 statement = error_mark_node;
10139 break;
10140 }
10141
10142 return statement;
10143 }
10144
10145 /* Parse a for-init-statement or the declarator of a range-based-for.
10146 Returns true if a range-based-for declaration is seen.
10147
10148 for-init-statement:
10149 expression-statement
10150 simple-declaration */
10151
10152 static bool
10153 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10154 {
10155 /* If the next token is a `;', then we have an empty
10156 expression-statement. Grammatically, this is also a
10157 simple-declaration, but an invalid one, because it does not
10158 declare anything. Therefore, if we did not handle this case
10159 specially, we would issue an error message about an invalid
10160 declaration. */
10161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10162 {
10163 bool is_range_for = false;
10164 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10165
10166 parser->colon_corrects_to_scope_p = false;
10167
10168 /* We're going to speculatively look for a declaration, falling back
10169 to an expression, if necessary. */
10170 cp_parser_parse_tentatively (parser);
10171 /* Parse the declaration. */
10172 cp_parser_simple_declaration (parser,
10173 /*function_definition_allowed_p=*/false,
10174 decl);
10175 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10176 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10177 {
10178 /* It is a range-for, consume the ':' */
10179 cp_lexer_consume_token (parser->lexer);
10180 is_range_for = true;
10181 if (cxx_dialect < cxx0x)
10182 {
10183 error_at (cp_lexer_peek_token (parser->lexer)->location,
10184 "range-based %<for%> loops are not allowed "
10185 "in C++98 mode");
10186 *decl = error_mark_node;
10187 }
10188 }
10189 else
10190 /* The ';' is not consumed yet because we told
10191 cp_parser_simple_declaration not to. */
10192 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10193
10194 if (cp_parser_parse_definitely (parser))
10195 return is_range_for;
10196 /* If the tentative parse failed, then we shall need to look for an
10197 expression-statement. */
10198 }
10199 /* If we are here, it is an expression-statement. */
10200 cp_parser_expression_statement (parser, NULL_TREE);
10201 return false;
10202 }
10203
10204 /* Parse a jump-statement.
10205
10206 jump-statement:
10207 break ;
10208 continue ;
10209 return expression [opt] ;
10210 return braced-init-list ;
10211 goto identifier ;
10212
10213 GNU extension:
10214
10215 jump-statement:
10216 goto * expression ;
10217
10218 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10219
10220 static tree
10221 cp_parser_jump_statement (cp_parser* parser)
10222 {
10223 tree statement = error_mark_node;
10224 cp_token *token;
10225 enum rid keyword;
10226 unsigned char in_statement;
10227
10228 /* Peek at the next token. */
10229 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10230 if (!token)
10231 return error_mark_node;
10232
10233 /* See what kind of keyword it is. */
10234 keyword = token->keyword;
10235 switch (keyword)
10236 {
10237 case RID_BREAK:
10238 in_statement = parser->in_statement & ~IN_IF_STMT;
10239 switch (in_statement)
10240 {
10241 case 0:
10242 error_at (token->location, "break statement not within loop or switch");
10243 break;
10244 default:
10245 gcc_assert ((in_statement & IN_SWITCH_STMT)
10246 || in_statement == IN_ITERATION_STMT);
10247 statement = finish_break_stmt ();
10248 break;
10249 case IN_OMP_BLOCK:
10250 error_at (token->location, "invalid exit from OpenMP structured block");
10251 break;
10252 case IN_OMP_FOR:
10253 error_at (token->location, "break statement used with OpenMP for loop");
10254 break;
10255 }
10256 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10257 break;
10258
10259 case RID_CONTINUE:
10260 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10261 {
10262 case 0:
10263 error_at (token->location, "continue statement not within a loop");
10264 break;
10265 case IN_ITERATION_STMT:
10266 case IN_OMP_FOR:
10267 statement = finish_continue_stmt ();
10268 break;
10269 case IN_OMP_BLOCK:
10270 error_at (token->location, "invalid exit from OpenMP structured block");
10271 break;
10272 default:
10273 gcc_unreachable ();
10274 }
10275 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10276 break;
10277
10278 case RID_RETURN:
10279 {
10280 tree expr;
10281 bool expr_non_constant_p;
10282
10283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10284 {
10285 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10286 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10287 }
10288 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10289 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10290 else
10291 /* If the next token is a `;', then there is no
10292 expression. */
10293 expr = NULL_TREE;
10294 /* Build the return-statement. */
10295 statement = finish_return_stmt (expr);
10296 /* Look for the final `;'. */
10297 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10298 }
10299 break;
10300
10301 case RID_GOTO:
10302 /* Create the goto-statement. */
10303 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10304 {
10305 /* Issue a warning about this use of a GNU extension. */
10306 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10307 /* Consume the '*' token. */
10308 cp_lexer_consume_token (parser->lexer);
10309 /* Parse the dependent expression. */
10310 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10311 }
10312 else
10313 finish_goto_stmt (cp_parser_identifier (parser));
10314 /* Look for the final `;'. */
10315 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10316 break;
10317
10318 default:
10319 cp_parser_error (parser, "expected jump-statement");
10320 break;
10321 }
10322
10323 return statement;
10324 }
10325
10326 /* Parse a declaration-statement.
10327
10328 declaration-statement:
10329 block-declaration */
10330
10331 static void
10332 cp_parser_declaration_statement (cp_parser* parser)
10333 {
10334 void *p;
10335
10336 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10337 p = obstack_alloc (&declarator_obstack, 0);
10338
10339 /* Parse the block-declaration. */
10340 cp_parser_block_declaration (parser, /*statement_p=*/true);
10341
10342 /* Free any declarators allocated. */
10343 obstack_free (&declarator_obstack, p);
10344
10345 /* Finish off the statement. */
10346 finish_stmt ();
10347 }
10348
10349 /* Some dependent statements (like `if (cond) statement'), are
10350 implicitly in their own scope. In other words, if the statement is
10351 a single statement (as opposed to a compound-statement), it is
10352 none-the-less treated as if it were enclosed in braces. Any
10353 declarations appearing in the dependent statement are out of scope
10354 after control passes that point. This function parses a statement,
10355 but ensures that is in its own scope, even if it is not a
10356 compound-statement.
10357
10358 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10359 is a (possibly labeled) if statement which is not enclosed in
10360 braces and has an else clause. This is used to implement
10361 -Wparentheses.
10362
10363 Returns the new statement. */
10364
10365 static tree
10366 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10367 {
10368 tree statement;
10369
10370 if (if_p != NULL)
10371 *if_p = false;
10372
10373 /* Mark if () ; with a special NOP_EXPR. */
10374 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10375 {
10376 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10377 cp_lexer_consume_token (parser->lexer);
10378 statement = add_stmt (build_empty_stmt (loc));
10379 }
10380 /* if a compound is opened, we simply parse the statement directly. */
10381 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10382 statement = cp_parser_compound_statement (parser, NULL, false, false);
10383 /* If the token is not a `{', then we must take special action. */
10384 else
10385 {
10386 /* Create a compound-statement. */
10387 statement = begin_compound_stmt (0);
10388 /* Parse the dependent-statement. */
10389 cp_parser_statement (parser, NULL_TREE, false, if_p);
10390 /* Finish the dummy compound-statement. */
10391 finish_compound_stmt (statement);
10392 }
10393
10394 /* Return the statement. */
10395 return statement;
10396 }
10397
10398 /* For some dependent statements (like `while (cond) statement'), we
10399 have already created a scope. Therefore, even if the dependent
10400 statement is a compound-statement, we do not want to create another
10401 scope. */
10402
10403 static void
10404 cp_parser_already_scoped_statement (cp_parser* parser)
10405 {
10406 /* If the token is a `{', then we must take special action. */
10407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10408 cp_parser_statement (parser, NULL_TREE, false, NULL);
10409 else
10410 {
10411 /* Avoid calling cp_parser_compound_statement, so that we
10412 don't create a new scope. Do everything else by hand. */
10413 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10414 /* If the next keyword is `__label__' we have a label declaration. */
10415 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10416 cp_parser_label_declaration (parser);
10417 /* Parse an (optional) statement-seq. */
10418 cp_parser_statement_seq_opt (parser, NULL_TREE);
10419 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10420 }
10421 }
10422
10423 /* Declarations [gram.dcl.dcl] */
10424
10425 /* Parse an optional declaration-sequence.
10426
10427 declaration-seq:
10428 declaration
10429 declaration-seq declaration */
10430
10431 static void
10432 cp_parser_declaration_seq_opt (cp_parser* parser)
10433 {
10434 while (true)
10435 {
10436 cp_token *token;
10437
10438 token = cp_lexer_peek_token (parser->lexer);
10439
10440 if (token->type == CPP_CLOSE_BRACE
10441 || token->type == CPP_EOF
10442 || token->type == CPP_PRAGMA_EOL)
10443 break;
10444
10445 if (token->type == CPP_SEMICOLON)
10446 {
10447 /* A declaration consisting of a single semicolon is
10448 invalid. Allow it unless we're being pedantic. */
10449 cp_lexer_consume_token (parser->lexer);
10450 if (!in_system_header)
10451 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10452 continue;
10453 }
10454
10455 /* If we're entering or exiting a region that's implicitly
10456 extern "C", modify the lang context appropriately. */
10457 if (!parser->implicit_extern_c && token->implicit_extern_c)
10458 {
10459 push_lang_context (lang_name_c);
10460 parser->implicit_extern_c = true;
10461 }
10462 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10463 {
10464 pop_lang_context ();
10465 parser->implicit_extern_c = false;
10466 }
10467
10468 if (token->type == CPP_PRAGMA)
10469 {
10470 /* A top-level declaration can consist solely of a #pragma.
10471 A nested declaration cannot, so this is done here and not
10472 in cp_parser_declaration. (A #pragma at block scope is
10473 handled in cp_parser_statement.) */
10474 cp_parser_pragma (parser, pragma_external);
10475 continue;
10476 }
10477
10478 /* Parse the declaration itself. */
10479 cp_parser_declaration (parser);
10480 }
10481 }
10482
10483 /* Parse a declaration.
10484
10485 declaration:
10486 block-declaration
10487 function-definition
10488 template-declaration
10489 explicit-instantiation
10490 explicit-specialization
10491 linkage-specification
10492 namespace-definition
10493
10494 GNU extension:
10495
10496 declaration:
10497 __extension__ declaration */
10498
10499 static void
10500 cp_parser_declaration (cp_parser* parser)
10501 {
10502 cp_token token1;
10503 cp_token token2;
10504 int saved_pedantic;
10505 void *p;
10506 tree attributes = NULL_TREE;
10507
10508 /* Check for the `__extension__' keyword. */
10509 if (cp_parser_extension_opt (parser, &saved_pedantic))
10510 {
10511 /* Parse the qualified declaration. */
10512 cp_parser_declaration (parser);
10513 /* Restore the PEDANTIC flag. */
10514 pedantic = saved_pedantic;
10515
10516 return;
10517 }
10518
10519 /* Try to figure out what kind of declaration is present. */
10520 token1 = *cp_lexer_peek_token (parser->lexer);
10521
10522 if (token1.type != CPP_EOF)
10523 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10524 else
10525 {
10526 token2.type = CPP_EOF;
10527 token2.keyword = RID_MAX;
10528 }
10529
10530 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10531 p = obstack_alloc (&declarator_obstack, 0);
10532
10533 /* If the next token is `extern' and the following token is a string
10534 literal, then we have a linkage specification. */
10535 if (token1.keyword == RID_EXTERN
10536 && cp_parser_is_pure_string_literal (&token2))
10537 cp_parser_linkage_specification (parser);
10538 /* If the next token is `template', then we have either a template
10539 declaration, an explicit instantiation, or an explicit
10540 specialization. */
10541 else if (token1.keyword == RID_TEMPLATE)
10542 {
10543 /* `template <>' indicates a template specialization. */
10544 if (token2.type == CPP_LESS
10545 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10546 cp_parser_explicit_specialization (parser);
10547 /* `template <' indicates a template declaration. */
10548 else if (token2.type == CPP_LESS)
10549 cp_parser_template_declaration (parser, /*member_p=*/false);
10550 /* Anything else must be an explicit instantiation. */
10551 else
10552 cp_parser_explicit_instantiation (parser);
10553 }
10554 /* If the next token is `export', then we have a template
10555 declaration. */
10556 else if (token1.keyword == RID_EXPORT)
10557 cp_parser_template_declaration (parser, /*member_p=*/false);
10558 /* If the next token is `extern', 'static' or 'inline' and the one
10559 after that is `template', we have a GNU extended explicit
10560 instantiation directive. */
10561 else if (cp_parser_allow_gnu_extensions_p (parser)
10562 && (token1.keyword == RID_EXTERN
10563 || token1.keyword == RID_STATIC
10564 || token1.keyword == RID_INLINE)
10565 && token2.keyword == RID_TEMPLATE)
10566 cp_parser_explicit_instantiation (parser);
10567 /* If the next token is `namespace', check for a named or unnamed
10568 namespace definition. */
10569 else if (token1.keyword == RID_NAMESPACE
10570 && (/* A named namespace definition. */
10571 (token2.type == CPP_NAME
10572 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10573 != CPP_EQ))
10574 /* An unnamed namespace definition. */
10575 || token2.type == CPP_OPEN_BRACE
10576 || token2.keyword == RID_ATTRIBUTE))
10577 cp_parser_namespace_definition (parser);
10578 /* An inline (associated) namespace definition. */
10579 else if (token1.keyword == RID_INLINE
10580 && token2.keyword == RID_NAMESPACE)
10581 cp_parser_namespace_definition (parser);
10582 /* Objective-C++ declaration/definition. */
10583 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10584 cp_parser_objc_declaration (parser, NULL_TREE);
10585 else if (c_dialect_objc ()
10586 && token1.keyword == RID_ATTRIBUTE
10587 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10588 cp_parser_objc_declaration (parser, attributes);
10589 /* We must have either a block declaration or a function
10590 definition. */
10591 else
10592 /* Try to parse a block-declaration, or a function-definition. */
10593 cp_parser_block_declaration (parser, /*statement_p=*/false);
10594
10595 /* Free any declarators allocated. */
10596 obstack_free (&declarator_obstack, p);
10597 }
10598
10599 /* Parse a block-declaration.
10600
10601 block-declaration:
10602 simple-declaration
10603 asm-definition
10604 namespace-alias-definition
10605 using-declaration
10606 using-directive
10607
10608 GNU Extension:
10609
10610 block-declaration:
10611 __extension__ block-declaration
10612
10613 C++0x Extension:
10614
10615 block-declaration:
10616 static_assert-declaration
10617
10618 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10619 part of a declaration-statement. */
10620
10621 static void
10622 cp_parser_block_declaration (cp_parser *parser,
10623 bool statement_p)
10624 {
10625 cp_token *token1;
10626 int saved_pedantic;
10627
10628 /* Check for the `__extension__' keyword. */
10629 if (cp_parser_extension_opt (parser, &saved_pedantic))
10630 {
10631 /* Parse the qualified declaration. */
10632 cp_parser_block_declaration (parser, statement_p);
10633 /* Restore the PEDANTIC flag. */
10634 pedantic = saved_pedantic;
10635
10636 return;
10637 }
10638
10639 /* Peek at the next token to figure out which kind of declaration is
10640 present. */
10641 token1 = cp_lexer_peek_token (parser->lexer);
10642
10643 /* If the next keyword is `asm', we have an asm-definition. */
10644 if (token1->keyword == RID_ASM)
10645 {
10646 if (statement_p)
10647 cp_parser_commit_to_tentative_parse (parser);
10648 cp_parser_asm_definition (parser);
10649 }
10650 /* If the next keyword is `namespace', we have a
10651 namespace-alias-definition. */
10652 else if (token1->keyword == RID_NAMESPACE)
10653 cp_parser_namespace_alias_definition (parser);
10654 /* If the next keyword is `using', we have a
10655 using-declaration, a using-directive, or an alias-declaration. */
10656 else if (token1->keyword == RID_USING)
10657 {
10658 cp_token *token2;
10659
10660 if (statement_p)
10661 cp_parser_commit_to_tentative_parse (parser);
10662 /* If the token after `using' is `namespace', then we have a
10663 using-directive. */
10664 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10665 if (token2->keyword == RID_NAMESPACE)
10666 cp_parser_using_directive (parser);
10667 /* If the second token after 'using' is '=', then we have an
10668 alias-declaration. */
10669 else if (cxx_dialect >= cxx0x
10670 && token2->type == CPP_NAME
10671 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10672 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10673 cp_parser_alias_declaration (parser);
10674 /* Otherwise, it's a using-declaration. */
10675 else
10676 cp_parser_using_declaration (parser,
10677 /*access_declaration_p=*/false);
10678 }
10679 /* If the next keyword is `__label__' we have a misplaced label
10680 declaration. */
10681 else if (token1->keyword == RID_LABEL)
10682 {
10683 cp_lexer_consume_token (parser->lexer);
10684 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10685 cp_parser_skip_to_end_of_statement (parser);
10686 /* If the next token is now a `;', consume it. */
10687 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10688 cp_lexer_consume_token (parser->lexer);
10689 }
10690 /* If the next token is `static_assert' we have a static assertion. */
10691 else if (token1->keyword == RID_STATIC_ASSERT)
10692 cp_parser_static_assert (parser, /*member_p=*/false);
10693 /* Anything else must be a simple-declaration. */
10694 else
10695 cp_parser_simple_declaration (parser, !statement_p,
10696 /*maybe_range_for_decl*/NULL);
10697 }
10698
10699 /* Parse a simple-declaration.
10700
10701 simple-declaration:
10702 decl-specifier-seq [opt] init-declarator-list [opt] ;
10703
10704 init-declarator-list:
10705 init-declarator
10706 init-declarator-list , init-declarator
10707
10708 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10709 function-definition as a simple-declaration.
10710
10711 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10712 parsed declaration if it is an uninitialized single declarator not followed
10713 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10714 if present, will not be consumed. */
10715
10716 static void
10717 cp_parser_simple_declaration (cp_parser* parser,
10718 bool function_definition_allowed_p,
10719 tree *maybe_range_for_decl)
10720 {
10721 cp_decl_specifier_seq decl_specifiers;
10722 int declares_class_or_enum;
10723 bool saw_declarator;
10724
10725 if (maybe_range_for_decl)
10726 *maybe_range_for_decl = NULL_TREE;
10727
10728 /* Defer access checks until we know what is being declared; the
10729 checks for names appearing in the decl-specifier-seq should be
10730 done as if we were in the scope of the thing being declared. */
10731 push_deferring_access_checks (dk_deferred);
10732
10733 /* Parse the decl-specifier-seq. We have to keep track of whether
10734 or not the decl-specifier-seq declares a named class or
10735 enumeration type, since that is the only case in which the
10736 init-declarator-list is allowed to be empty.
10737
10738 [dcl.dcl]
10739
10740 In a simple-declaration, the optional init-declarator-list can be
10741 omitted only when declaring a class or enumeration, that is when
10742 the decl-specifier-seq contains either a class-specifier, an
10743 elaborated-type-specifier, or an enum-specifier. */
10744 cp_parser_decl_specifier_seq (parser,
10745 CP_PARSER_FLAGS_OPTIONAL,
10746 &decl_specifiers,
10747 &declares_class_or_enum);
10748 /* We no longer need to defer access checks. */
10749 stop_deferring_access_checks ();
10750
10751 /* In a block scope, a valid declaration must always have a
10752 decl-specifier-seq. By not trying to parse declarators, we can
10753 resolve the declaration/expression ambiguity more quickly. */
10754 if (!function_definition_allowed_p
10755 && !decl_specifiers.any_specifiers_p)
10756 {
10757 cp_parser_error (parser, "expected declaration");
10758 goto done;
10759 }
10760
10761 /* If the next two tokens are both identifiers, the code is
10762 erroneous. The usual cause of this situation is code like:
10763
10764 T t;
10765
10766 where "T" should name a type -- but does not. */
10767 if (!decl_specifiers.any_type_specifiers_p
10768 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10769 {
10770 /* If parsing tentatively, we should commit; we really are
10771 looking at a declaration. */
10772 cp_parser_commit_to_tentative_parse (parser);
10773 /* Give up. */
10774 goto done;
10775 }
10776
10777 /* If we have seen at least one decl-specifier, and the next token
10778 is not a parenthesis, then we must be looking at a declaration.
10779 (After "int (" we might be looking at a functional cast.) */
10780 if (decl_specifiers.any_specifiers_p
10781 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10782 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10783 && !cp_parser_error_occurred (parser))
10784 cp_parser_commit_to_tentative_parse (parser);
10785
10786 /* Keep going until we hit the `;' at the end of the simple
10787 declaration. */
10788 saw_declarator = false;
10789 while (cp_lexer_next_token_is_not (parser->lexer,
10790 CPP_SEMICOLON))
10791 {
10792 cp_token *token;
10793 bool function_definition_p;
10794 tree decl;
10795
10796 if (saw_declarator)
10797 {
10798 /* If we are processing next declarator, coma is expected */
10799 token = cp_lexer_peek_token (parser->lexer);
10800 gcc_assert (token->type == CPP_COMMA);
10801 cp_lexer_consume_token (parser->lexer);
10802 if (maybe_range_for_decl)
10803 *maybe_range_for_decl = error_mark_node;
10804 }
10805 else
10806 saw_declarator = true;
10807
10808 /* Parse the init-declarator. */
10809 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10810 /*checks=*/NULL,
10811 function_definition_allowed_p,
10812 /*member_p=*/false,
10813 declares_class_or_enum,
10814 &function_definition_p,
10815 maybe_range_for_decl);
10816 /* If an error occurred while parsing tentatively, exit quickly.
10817 (That usually happens when in the body of a function; each
10818 statement is treated as a declaration-statement until proven
10819 otherwise.) */
10820 if (cp_parser_error_occurred (parser))
10821 goto done;
10822 /* Handle function definitions specially. */
10823 if (function_definition_p)
10824 {
10825 /* If the next token is a `,', then we are probably
10826 processing something like:
10827
10828 void f() {}, *p;
10829
10830 which is erroneous. */
10831 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10832 {
10833 cp_token *token = cp_lexer_peek_token (parser->lexer);
10834 error_at (token->location,
10835 "mixing"
10836 " declarations and function-definitions is forbidden");
10837 }
10838 /* Otherwise, we're done with the list of declarators. */
10839 else
10840 {
10841 pop_deferring_access_checks ();
10842 return;
10843 }
10844 }
10845 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10846 *maybe_range_for_decl = decl;
10847 /* The next token should be either a `,' or a `;'. */
10848 token = cp_lexer_peek_token (parser->lexer);
10849 /* If it's a `,', there are more declarators to come. */
10850 if (token->type == CPP_COMMA)
10851 /* will be consumed next time around */;
10852 /* If it's a `;', we are done. */
10853 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10854 break;
10855 /* Anything else is an error. */
10856 else
10857 {
10858 /* If we have already issued an error message we don't need
10859 to issue another one. */
10860 if (decl != error_mark_node
10861 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10862 cp_parser_error (parser, "expected %<,%> or %<;%>");
10863 /* Skip tokens until we reach the end of the statement. */
10864 cp_parser_skip_to_end_of_statement (parser);
10865 /* If the next token is now a `;', consume it. */
10866 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10867 cp_lexer_consume_token (parser->lexer);
10868 goto done;
10869 }
10870 /* After the first time around, a function-definition is not
10871 allowed -- even if it was OK at first. For example:
10872
10873 int i, f() {}
10874
10875 is not valid. */
10876 function_definition_allowed_p = false;
10877 }
10878
10879 /* Issue an error message if no declarators are present, and the
10880 decl-specifier-seq does not itself declare a class or
10881 enumeration. */
10882 if (!saw_declarator)
10883 {
10884 if (cp_parser_declares_only_class_p (parser))
10885 shadow_tag (&decl_specifiers);
10886 /* Perform any deferred access checks. */
10887 perform_deferred_access_checks (tf_warning_or_error);
10888 }
10889
10890 /* Consume the `;'. */
10891 if (!maybe_range_for_decl)
10892 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10893
10894 done:
10895 pop_deferring_access_checks ();
10896 }
10897
10898 /* Parse a decl-specifier-seq.
10899
10900 decl-specifier-seq:
10901 decl-specifier-seq [opt] decl-specifier
10902 decl-specifier attribute-specifier-seq [opt] (C++11)
10903
10904 decl-specifier:
10905 storage-class-specifier
10906 type-specifier
10907 function-specifier
10908 friend
10909 typedef
10910
10911 GNU Extension:
10912
10913 decl-specifier:
10914 attributes
10915
10916 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10917
10918 The parser flags FLAGS is used to control type-specifier parsing.
10919
10920 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10921 flags:
10922
10923 1: one of the decl-specifiers is an elaborated-type-specifier
10924 (i.e., a type declaration)
10925 2: one of the decl-specifiers is an enum-specifier or a
10926 class-specifier (i.e., a type definition)
10927
10928 */
10929
10930 static void
10931 cp_parser_decl_specifier_seq (cp_parser* parser,
10932 cp_parser_flags flags,
10933 cp_decl_specifier_seq *decl_specs,
10934 int* declares_class_or_enum)
10935 {
10936 bool constructor_possible_p = !parser->in_declarator_p;
10937 bool found_decl_spec = false;
10938 cp_token *start_token = NULL;
10939 cp_decl_spec ds;
10940
10941 /* Clear DECL_SPECS. */
10942 clear_decl_specs (decl_specs);
10943
10944 /* Assume no class or enumeration type is declared. */
10945 *declares_class_or_enum = 0;
10946
10947 /* Keep reading specifiers until there are no more to read. */
10948 while (true)
10949 {
10950 bool constructor_p;
10951 cp_token *token;
10952 ds = ds_last;
10953
10954 /* Peek at the next token. */
10955 token = cp_lexer_peek_token (parser->lexer);
10956
10957 /* Save the first token of the decl spec list for error
10958 reporting. */
10959 if (!start_token)
10960 start_token = token;
10961 /* Handle attributes. */
10962 if (cp_next_tokens_can_be_attribute_p (parser))
10963 {
10964 /* Parse the attributes. */
10965 tree attrs = cp_parser_attributes_opt (parser);
10966
10967 /* In a sequence of declaration specifiers, c++11 attributes
10968 appertain to the type that precede them. In that case
10969 [dcl.spec]/1 says:
10970
10971 The attribute-specifier-seq affects the type only for
10972 the declaration it appears in, not other declarations
10973 involving the same type.
10974
10975 But for now let's force the user to position the
10976 attribute either at the beginning of the declaration or
10977 after the declarator-id, which would clearly mean that it
10978 applies to the declarator. */
10979 if (cxx11_attribute_p (attrs))
10980 {
10981 if (!found_decl_spec)
10982 /* The c++11 attribute is at the beginning of the
10983 declaration. It appertains to the entity being
10984 declared. */;
10985 else
10986 {
10987 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10988 {
10989 /* This is an attribute following a
10990 class-specifier. */
10991 if (decl_specs->type_definition_p)
10992 warn_misplaced_attr_for_class_type (token->location,
10993 decl_specs->type);
10994 attrs = NULL_TREE;
10995 }
10996 else
10997 {
10998 decl_specs->std_attributes
10999 = chainon (decl_specs->std_attributes,
11000 attrs);
11001 if (decl_specs->locations[ds_std_attribute] == 0)
11002 decl_specs->locations[ds_std_attribute] = token->location;
11003 }
11004 continue;
11005 }
11006 }
11007
11008 decl_specs->attributes
11009 = chainon (decl_specs->attributes,
11010 attrs);
11011 if (decl_specs->locations[ds_attribute] == 0)
11012 decl_specs->locations[ds_attribute] = token->location;
11013 continue;
11014 }
11015 /* Assume we will find a decl-specifier keyword. */
11016 found_decl_spec = true;
11017 /* If the next token is an appropriate keyword, we can simply
11018 add it to the list. */
11019 switch (token->keyword)
11020 {
11021 /* decl-specifier:
11022 friend
11023 constexpr */
11024 case RID_FRIEND:
11025 if (!at_class_scope_p ())
11026 {
11027 error_at (token->location, "%<friend%> used outside of class");
11028 cp_lexer_purge_token (parser->lexer);
11029 }
11030 else
11031 {
11032 ds = ds_friend;
11033 /* Consume the token. */
11034 cp_lexer_consume_token (parser->lexer);
11035 }
11036 break;
11037
11038 case RID_CONSTEXPR:
11039 ds = ds_constexpr;
11040 cp_lexer_consume_token (parser->lexer);
11041 break;
11042
11043 /* function-specifier:
11044 inline
11045 virtual
11046 explicit */
11047 case RID_INLINE:
11048 case RID_VIRTUAL:
11049 case RID_EXPLICIT:
11050 cp_parser_function_specifier_opt (parser, decl_specs);
11051 break;
11052
11053 /* decl-specifier:
11054 typedef */
11055 case RID_TYPEDEF:
11056 ds = ds_typedef;
11057 /* Consume the token. */
11058 cp_lexer_consume_token (parser->lexer);
11059 /* A constructor declarator cannot appear in a typedef. */
11060 constructor_possible_p = false;
11061 /* The "typedef" keyword can only occur in a declaration; we
11062 may as well commit at this point. */
11063 cp_parser_commit_to_tentative_parse (parser);
11064
11065 if (decl_specs->storage_class != sc_none)
11066 decl_specs->conflicting_specifiers_p = true;
11067 break;
11068
11069 /* storage-class-specifier:
11070 auto
11071 register
11072 static
11073 extern
11074 mutable
11075
11076 GNU Extension:
11077 thread */
11078 case RID_AUTO:
11079 if (cxx_dialect == cxx98)
11080 {
11081 /* Consume the token. */
11082 cp_lexer_consume_token (parser->lexer);
11083
11084 /* Complain about `auto' as a storage specifier, if
11085 we're complaining about C++0x compatibility. */
11086 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11087 " changes meaning in C++11; please remove it");
11088
11089 /* Set the storage class anyway. */
11090 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11091 token);
11092 }
11093 else
11094 /* C++0x auto type-specifier. */
11095 found_decl_spec = false;
11096 break;
11097
11098 case RID_REGISTER:
11099 case RID_STATIC:
11100 case RID_EXTERN:
11101 case RID_MUTABLE:
11102 /* Consume the token. */
11103 cp_lexer_consume_token (parser->lexer);
11104 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11105 token);
11106 break;
11107 case RID_THREAD:
11108 /* Consume the token. */
11109 ds = ds_thread;
11110 cp_lexer_consume_token (parser->lexer);
11111 break;
11112
11113 default:
11114 /* We did not yet find a decl-specifier yet. */
11115 found_decl_spec = false;
11116 break;
11117 }
11118
11119 if (found_decl_spec
11120 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11121 && token->keyword != RID_CONSTEXPR)
11122 error ("decl-specifier invalid in condition");
11123
11124 if (ds != ds_last)
11125 set_and_check_decl_spec_loc (decl_specs, ds, token);
11126
11127 /* Constructors are a special case. The `S' in `S()' is not a
11128 decl-specifier; it is the beginning of the declarator. */
11129 constructor_p
11130 = (!found_decl_spec
11131 && constructor_possible_p
11132 && (cp_parser_constructor_declarator_p
11133 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11134
11135 /* If we don't have a DECL_SPEC yet, then we must be looking at
11136 a type-specifier. */
11137 if (!found_decl_spec && !constructor_p)
11138 {
11139 int decl_spec_declares_class_or_enum;
11140 bool is_cv_qualifier;
11141 tree type_spec;
11142
11143 type_spec
11144 = cp_parser_type_specifier (parser, flags,
11145 decl_specs,
11146 /*is_declaration=*/true,
11147 &decl_spec_declares_class_or_enum,
11148 &is_cv_qualifier);
11149 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11150
11151 /* If this type-specifier referenced a user-defined type
11152 (a typedef, class-name, etc.), then we can't allow any
11153 more such type-specifiers henceforth.
11154
11155 [dcl.spec]
11156
11157 The longest sequence of decl-specifiers that could
11158 possibly be a type name is taken as the
11159 decl-specifier-seq of a declaration. The sequence shall
11160 be self-consistent as described below.
11161
11162 [dcl.type]
11163
11164 As a general rule, at most one type-specifier is allowed
11165 in the complete decl-specifier-seq of a declaration. The
11166 only exceptions are the following:
11167
11168 -- const or volatile can be combined with any other
11169 type-specifier.
11170
11171 -- signed or unsigned can be combined with char, long,
11172 short, or int.
11173
11174 -- ..
11175
11176 Example:
11177
11178 typedef char* Pc;
11179 void g (const int Pc);
11180
11181 Here, Pc is *not* part of the decl-specifier seq; it's
11182 the declarator. Therefore, once we see a type-specifier
11183 (other than a cv-qualifier), we forbid any additional
11184 user-defined types. We *do* still allow things like `int
11185 int' to be considered a decl-specifier-seq, and issue the
11186 error message later. */
11187 if (type_spec && !is_cv_qualifier)
11188 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11189 /* A constructor declarator cannot follow a type-specifier. */
11190 if (type_spec)
11191 {
11192 constructor_possible_p = false;
11193 found_decl_spec = true;
11194 if (!is_cv_qualifier)
11195 decl_specs->any_type_specifiers_p = true;
11196 }
11197 }
11198
11199 /* If we still do not have a DECL_SPEC, then there are no more
11200 decl-specifiers. */
11201 if (!found_decl_spec)
11202 break;
11203
11204 decl_specs->any_specifiers_p = true;
11205 /* After we see one decl-specifier, further decl-specifiers are
11206 always optional. */
11207 flags |= CP_PARSER_FLAGS_OPTIONAL;
11208 }
11209
11210 /* Don't allow a friend specifier with a class definition. */
11211 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11212 && (*declares_class_or_enum & 2))
11213 error_at (decl_specs->locations[ds_friend],
11214 "class definition may not be declared a friend");
11215 }
11216
11217 /* Parse an (optional) storage-class-specifier.
11218
11219 storage-class-specifier:
11220 auto
11221 register
11222 static
11223 extern
11224 mutable
11225
11226 GNU Extension:
11227
11228 storage-class-specifier:
11229 thread
11230
11231 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11232
11233 static tree
11234 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11235 {
11236 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11237 {
11238 case RID_AUTO:
11239 if (cxx_dialect != cxx98)
11240 return NULL_TREE;
11241 /* Fall through for C++98. */
11242
11243 case RID_REGISTER:
11244 case RID_STATIC:
11245 case RID_EXTERN:
11246 case RID_MUTABLE:
11247 case RID_THREAD:
11248 /* Consume the token. */
11249 return cp_lexer_consume_token (parser->lexer)->u.value;
11250
11251 default:
11252 return NULL_TREE;
11253 }
11254 }
11255
11256 /* Parse an (optional) function-specifier.
11257
11258 function-specifier:
11259 inline
11260 virtual
11261 explicit
11262
11263 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11264 Updates DECL_SPECS, if it is non-NULL. */
11265
11266 static tree
11267 cp_parser_function_specifier_opt (cp_parser* parser,
11268 cp_decl_specifier_seq *decl_specs)
11269 {
11270 cp_token *token = cp_lexer_peek_token (parser->lexer);
11271 switch (token->keyword)
11272 {
11273 case RID_INLINE:
11274 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11275 break;
11276
11277 case RID_VIRTUAL:
11278 /* 14.5.2.3 [temp.mem]
11279
11280 A member function template shall not be virtual. */
11281 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11282 error_at (token->location, "templates may not be %<virtual%>");
11283 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11284 break;
11285
11286 case RID_EXPLICIT:
11287 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11288 break;
11289
11290 default:
11291 return NULL_TREE;
11292 }
11293
11294 /* Consume the token. */
11295 return cp_lexer_consume_token (parser->lexer)->u.value;
11296 }
11297
11298 /* Parse a linkage-specification.
11299
11300 linkage-specification:
11301 extern string-literal { declaration-seq [opt] }
11302 extern string-literal declaration */
11303
11304 static void
11305 cp_parser_linkage_specification (cp_parser* parser)
11306 {
11307 tree linkage;
11308
11309 /* Look for the `extern' keyword. */
11310 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11311
11312 /* Look for the string-literal. */
11313 linkage = cp_parser_string_literal (parser, false, false);
11314
11315 /* Transform the literal into an identifier. If the literal is a
11316 wide-character string, or contains embedded NULs, then we can't
11317 handle it as the user wants. */
11318 if (strlen (TREE_STRING_POINTER (linkage))
11319 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11320 {
11321 cp_parser_error (parser, "invalid linkage-specification");
11322 /* Assume C++ linkage. */
11323 linkage = lang_name_cplusplus;
11324 }
11325 else
11326 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11327
11328 /* We're now using the new linkage. */
11329 push_lang_context (linkage);
11330
11331 /* If the next token is a `{', then we're using the first
11332 production. */
11333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11334 {
11335 /* Consume the `{' token. */
11336 cp_lexer_consume_token (parser->lexer);
11337 /* Parse the declarations. */
11338 cp_parser_declaration_seq_opt (parser);
11339 /* Look for the closing `}'. */
11340 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11341 }
11342 /* Otherwise, there's just one declaration. */
11343 else
11344 {
11345 bool saved_in_unbraced_linkage_specification_p;
11346
11347 saved_in_unbraced_linkage_specification_p
11348 = parser->in_unbraced_linkage_specification_p;
11349 parser->in_unbraced_linkage_specification_p = true;
11350 cp_parser_declaration (parser);
11351 parser->in_unbraced_linkage_specification_p
11352 = saved_in_unbraced_linkage_specification_p;
11353 }
11354
11355 /* We're done with the linkage-specification. */
11356 pop_lang_context ();
11357 }
11358
11359 /* Parse a static_assert-declaration.
11360
11361 static_assert-declaration:
11362 static_assert ( constant-expression , string-literal ) ;
11363
11364 If MEMBER_P, this static_assert is a class member. */
11365
11366 static void
11367 cp_parser_static_assert(cp_parser *parser, bool member_p)
11368 {
11369 tree condition;
11370 tree message;
11371 cp_token *token;
11372 location_t saved_loc;
11373 bool dummy;
11374
11375 /* Peek at the `static_assert' token so we can keep track of exactly
11376 where the static assertion started. */
11377 token = cp_lexer_peek_token (parser->lexer);
11378 saved_loc = token->location;
11379
11380 /* Look for the `static_assert' keyword. */
11381 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11382 RT_STATIC_ASSERT))
11383 return;
11384
11385 /* We know we are in a static assertion; commit to any tentative
11386 parse. */
11387 if (cp_parser_parsing_tentatively (parser))
11388 cp_parser_commit_to_tentative_parse (parser);
11389
11390 /* Parse the `(' starting the static assertion condition. */
11391 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11392
11393 /* Parse the constant-expression. Allow a non-constant expression
11394 here in order to give better diagnostics in finish_static_assert. */
11395 condition =
11396 cp_parser_constant_expression (parser,
11397 /*allow_non_constant_p=*/true,
11398 /*non_constant_p=*/&dummy);
11399
11400 /* Parse the separating `,'. */
11401 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11402
11403 /* Parse the string-literal message. */
11404 message = cp_parser_string_literal (parser,
11405 /*translate=*/false,
11406 /*wide_ok=*/true);
11407
11408 /* A `)' completes the static assertion. */
11409 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11410 cp_parser_skip_to_closing_parenthesis (parser,
11411 /*recovering=*/true,
11412 /*or_comma=*/false,
11413 /*consume_paren=*/true);
11414
11415 /* A semicolon terminates the declaration. */
11416 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11417
11418 /* Complete the static assertion, which may mean either processing
11419 the static assert now or saving it for template instantiation. */
11420 finish_static_assert (condition, message, saved_loc, member_p);
11421 }
11422
11423 /* Parse the expression in decltype ( expression ). */
11424
11425 static tree
11426 cp_parser_decltype_expr (cp_parser *parser,
11427 bool &id_expression_or_member_access_p)
11428 {
11429 cp_token *id_expr_start_token;
11430 tree expr;
11431
11432 /* First, try parsing an id-expression. */
11433 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11434 cp_parser_parse_tentatively (parser);
11435 expr = cp_parser_id_expression (parser,
11436 /*template_keyword_p=*/false,
11437 /*check_dependency_p=*/true,
11438 /*template_p=*/NULL,
11439 /*declarator_p=*/false,
11440 /*optional_p=*/false);
11441
11442 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11443 {
11444 bool non_integral_constant_expression_p = false;
11445 tree id_expression = expr;
11446 cp_id_kind idk;
11447 const char *error_msg;
11448
11449 if (identifier_p (expr))
11450 /* Lookup the name we got back from the id-expression. */
11451 expr = cp_parser_lookup_name (parser, expr,
11452 none_type,
11453 /*is_template=*/false,
11454 /*is_namespace=*/false,
11455 /*check_dependency=*/true,
11456 /*ambiguous_decls=*/NULL,
11457 id_expr_start_token->location);
11458
11459 if (expr
11460 && expr != error_mark_node
11461 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11462 && TREE_CODE (expr) != TYPE_DECL
11463 && (TREE_CODE (expr) != BIT_NOT_EXPR
11464 || !TYPE_P (TREE_OPERAND (expr, 0)))
11465 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11466 {
11467 /* Complete lookup of the id-expression. */
11468 expr = (finish_id_expression
11469 (id_expression, expr, parser->scope, &idk,
11470 /*integral_constant_expression_p=*/false,
11471 /*allow_non_integral_constant_expression_p=*/true,
11472 &non_integral_constant_expression_p,
11473 /*template_p=*/false,
11474 /*done=*/true,
11475 /*address_p=*/false,
11476 /*template_arg_p=*/false,
11477 &error_msg,
11478 id_expr_start_token->location));
11479
11480 if (expr == error_mark_node)
11481 /* We found an id-expression, but it was something that we
11482 should not have found. This is an error, not something
11483 we can recover from, so note that we found an
11484 id-expression and we'll recover as gracefully as
11485 possible. */
11486 id_expression_or_member_access_p = true;
11487 }
11488
11489 if (expr
11490 && expr != error_mark_node
11491 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11492 /* We have an id-expression. */
11493 id_expression_or_member_access_p = true;
11494 }
11495
11496 if (!id_expression_or_member_access_p)
11497 {
11498 /* Abort the id-expression parse. */
11499 cp_parser_abort_tentative_parse (parser);
11500
11501 /* Parsing tentatively, again. */
11502 cp_parser_parse_tentatively (parser);
11503
11504 /* Parse a class member access. */
11505 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11506 /*cast_p=*/false, /*decltype*/true,
11507 /*member_access_only_p=*/true, NULL);
11508
11509 if (expr
11510 && expr != error_mark_node
11511 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11512 /* We have an id-expression. */
11513 id_expression_or_member_access_p = true;
11514 }
11515
11516 if (id_expression_or_member_access_p)
11517 /* We have parsed the complete id-expression or member access. */
11518 cp_parser_parse_definitely (parser);
11519 else
11520 {
11521 /* Abort our attempt to parse an id-expression or member access
11522 expression. */
11523 cp_parser_abort_tentative_parse (parser);
11524
11525 /* Parse a full expression. */
11526 expr = cp_parser_expression (parser, /*cast_p=*/false,
11527 /*decltype*/true, NULL);
11528 }
11529
11530 return expr;
11531 }
11532
11533 /* Parse a `decltype' type. Returns the type.
11534
11535 simple-type-specifier:
11536 decltype ( expression )
11537 C++14 proposal:
11538 decltype ( auto ) */
11539
11540 static tree
11541 cp_parser_decltype (cp_parser *parser)
11542 {
11543 tree expr;
11544 bool id_expression_or_member_access_p = false;
11545 const char *saved_message;
11546 bool saved_integral_constant_expression_p;
11547 bool saved_non_integral_constant_expression_p;
11548 bool saved_greater_than_is_operator_p;
11549 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11550
11551 if (start_token->type == CPP_DECLTYPE)
11552 {
11553 /* Already parsed. */
11554 cp_lexer_consume_token (parser->lexer);
11555 return start_token->u.value;
11556 }
11557
11558 /* Look for the `decltype' token. */
11559 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11560 return error_mark_node;
11561
11562 /* Parse the opening `('. */
11563 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11564 return error_mark_node;
11565
11566 /* decltype (auto) */
11567 if (cxx_dialect >= cxx1y
11568 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11569 {
11570 cp_lexer_consume_token (parser->lexer);
11571 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11572 return error_mark_node;
11573 expr = make_decltype_auto ();
11574 AUTO_IS_DECLTYPE (expr) = true;
11575 goto rewrite;
11576 }
11577
11578 /* Types cannot be defined in a `decltype' expression. Save away the
11579 old message. */
11580 saved_message = parser->type_definition_forbidden_message;
11581
11582 /* And create the new one. */
11583 parser->type_definition_forbidden_message
11584 = G_("types may not be defined in %<decltype%> expressions");
11585
11586 /* The restrictions on constant-expressions do not apply inside
11587 decltype expressions. */
11588 saved_integral_constant_expression_p
11589 = parser->integral_constant_expression_p;
11590 saved_non_integral_constant_expression_p
11591 = parser->non_integral_constant_expression_p;
11592 parser->integral_constant_expression_p = false;
11593
11594 /* Within a parenthesized expression, a `>' token is always
11595 the greater-than operator. */
11596 saved_greater_than_is_operator_p
11597 = parser->greater_than_is_operator_p;
11598 parser->greater_than_is_operator_p = true;
11599
11600 /* Do not actually evaluate the expression. */
11601 ++cp_unevaluated_operand;
11602
11603 /* Do not warn about problems with the expression. */
11604 ++c_inhibit_evaluation_warnings;
11605
11606 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
11607
11608 /* Go back to evaluating expressions. */
11609 --cp_unevaluated_operand;
11610 --c_inhibit_evaluation_warnings;
11611
11612 /* The `>' token might be the end of a template-id or
11613 template-parameter-list now. */
11614 parser->greater_than_is_operator_p
11615 = saved_greater_than_is_operator_p;
11616
11617 /* Restore the old message and the integral constant expression
11618 flags. */
11619 parser->type_definition_forbidden_message = saved_message;
11620 parser->integral_constant_expression_p
11621 = saved_integral_constant_expression_p;
11622 parser->non_integral_constant_expression_p
11623 = saved_non_integral_constant_expression_p;
11624
11625 /* Parse to the closing `)'. */
11626 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11627 {
11628 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11629 /*consume_paren=*/true);
11630 return error_mark_node;
11631 }
11632
11633 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11634 tf_warning_or_error);
11635
11636 rewrite:
11637 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11638 it again. */
11639 start_token->type = CPP_DECLTYPE;
11640 start_token->u.value = expr;
11641 start_token->keyword = RID_MAX;
11642 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11643
11644 return expr;
11645 }
11646
11647 /* Special member functions [gram.special] */
11648
11649 /* Parse a conversion-function-id.
11650
11651 conversion-function-id:
11652 operator conversion-type-id
11653
11654 Returns an IDENTIFIER_NODE representing the operator. */
11655
11656 static tree
11657 cp_parser_conversion_function_id (cp_parser* parser)
11658 {
11659 tree type;
11660 tree saved_scope;
11661 tree saved_qualifying_scope;
11662 tree saved_object_scope;
11663 tree pushed_scope = NULL_TREE;
11664
11665 /* Look for the `operator' token. */
11666 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11667 return error_mark_node;
11668 /* When we parse the conversion-type-id, the current scope will be
11669 reset. However, we need that information in able to look up the
11670 conversion function later, so we save it here. */
11671 saved_scope = parser->scope;
11672 saved_qualifying_scope = parser->qualifying_scope;
11673 saved_object_scope = parser->object_scope;
11674 /* We must enter the scope of the class so that the names of
11675 entities declared within the class are available in the
11676 conversion-type-id. For example, consider:
11677
11678 struct S {
11679 typedef int I;
11680 operator I();
11681 };
11682
11683 S::operator I() { ... }
11684
11685 In order to see that `I' is a type-name in the definition, we
11686 must be in the scope of `S'. */
11687 if (saved_scope)
11688 pushed_scope = push_scope (saved_scope);
11689 /* Parse the conversion-type-id. */
11690 type = cp_parser_conversion_type_id (parser);
11691 /* Leave the scope of the class, if any. */
11692 if (pushed_scope)
11693 pop_scope (pushed_scope);
11694 /* Restore the saved scope. */
11695 parser->scope = saved_scope;
11696 parser->qualifying_scope = saved_qualifying_scope;
11697 parser->object_scope = saved_object_scope;
11698 /* If the TYPE is invalid, indicate failure. */
11699 if (type == error_mark_node)
11700 return error_mark_node;
11701 return mangle_conv_op_name_for_type (type);
11702 }
11703
11704 /* Parse a conversion-type-id:
11705
11706 conversion-type-id:
11707 type-specifier-seq conversion-declarator [opt]
11708
11709 Returns the TYPE specified. */
11710
11711 static tree
11712 cp_parser_conversion_type_id (cp_parser* parser)
11713 {
11714 tree attributes;
11715 cp_decl_specifier_seq type_specifiers;
11716 cp_declarator *declarator;
11717 tree type_specified;
11718
11719 /* Parse the attributes. */
11720 attributes = cp_parser_attributes_opt (parser);
11721 /* Parse the type-specifiers. */
11722 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11723 /*is_trailing_return=*/false,
11724 &type_specifiers);
11725 /* If that didn't work, stop. */
11726 if (type_specifiers.type == error_mark_node)
11727 return error_mark_node;
11728 /* Parse the conversion-declarator. */
11729 declarator = cp_parser_conversion_declarator_opt (parser);
11730
11731 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11732 /*initialized=*/0, &attributes);
11733 if (attributes)
11734 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11735
11736 /* Don't give this error when parsing tentatively. This happens to
11737 work because we always parse this definitively once. */
11738 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11739 && type_uses_auto (type_specified))
11740 {
11741 if (cxx_dialect < cxx1y)
11742 {
11743 error ("invalid use of %<auto%> in conversion operator");
11744 return error_mark_node;
11745 }
11746 else if (template_parm_scope_p ())
11747 warning (0, "use of %<auto%> in member template "
11748 "conversion operator can never be deduced");
11749 }
11750
11751 return type_specified;
11752 }
11753
11754 /* Parse an (optional) conversion-declarator.
11755
11756 conversion-declarator:
11757 ptr-operator conversion-declarator [opt]
11758
11759 */
11760
11761 static cp_declarator *
11762 cp_parser_conversion_declarator_opt (cp_parser* parser)
11763 {
11764 enum tree_code code;
11765 tree class_type, std_attributes = NULL_TREE;
11766 cp_cv_quals cv_quals;
11767
11768 /* We don't know if there's a ptr-operator next, or not. */
11769 cp_parser_parse_tentatively (parser);
11770 /* Try the ptr-operator. */
11771 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11772 &std_attributes);
11773 /* If it worked, look for more conversion-declarators. */
11774 if (cp_parser_parse_definitely (parser))
11775 {
11776 cp_declarator *declarator;
11777
11778 /* Parse another optional declarator. */
11779 declarator = cp_parser_conversion_declarator_opt (parser);
11780
11781 declarator = cp_parser_make_indirect_declarator
11782 (code, class_type, cv_quals, declarator, std_attributes);
11783
11784 return declarator;
11785 }
11786
11787 return NULL;
11788 }
11789
11790 /* Parse an (optional) ctor-initializer.
11791
11792 ctor-initializer:
11793 : mem-initializer-list
11794
11795 Returns TRUE iff the ctor-initializer was actually present. */
11796
11797 static bool
11798 cp_parser_ctor_initializer_opt (cp_parser* parser)
11799 {
11800 /* If the next token is not a `:', then there is no
11801 ctor-initializer. */
11802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11803 {
11804 /* Do default initialization of any bases and members. */
11805 if (DECL_CONSTRUCTOR_P (current_function_decl))
11806 finish_mem_initializers (NULL_TREE);
11807
11808 return false;
11809 }
11810
11811 /* Consume the `:' token. */
11812 cp_lexer_consume_token (parser->lexer);
11813 /* And the mem-initializer-list. */
11814 cp_parser_mem_initializer_list (parser);
11815
11816 return true;
11817 }
11818
11819 /* Parse a mem-initializer-list.
11820
11821 mem-initializer-list:
11822 mem-initializer ... [opt]
11823 mem-initializer ... [opt] , mem-initializer-list */
11824
11825 static void
11826 cp_parser_mem_initializer_list (cp_parser* parser)
11827 {
11828 tree mem_initializer_list = NULL_TREE;
11829 tree target_ctor = error_mark_node;
11830 cp_token *token = cp_lexer_peek_token (parser->lexer);
11831
11832 /* Let the semantic analysis code know that we are starting the
11833 mem-initializer-list. */
11834 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11835 error_at (token->location,
11836 "only constructors take member initializers");
11837
11838 /* Loop through the list. */
11839 while (true)
11840 {
11841 tree mem_initializer;
11842
11843 token = cp_lexer_peek_token (parser->lexer);
11844 /* Parse the mem-initializer. */
11845 mem_initializer = cp_parser_mem_initializer (parser);
11846 /* If the next token is a `...', we're expanding member initializers. */
11847 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11848 {
11849 /* Consume the `...'. */
11850 cp_lexer_consume_token (parser->lexer);
11851
11852 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11853 can be expanded but members cannot. */
11854 if (mem_initializer != error_mark_node
11855 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11856 {
11857 error_at (token->location,
11858 "cannot expand initializer for member %<%D%>",
11859 TREE_PURPOSE (mem_initializer));
11860 mem_initializer = error_mark_node;
11861 }
11862
11863 /* Construct the pack expansion type. */
11864 if (mem_initializer != error_mark_node)
11865 mem_initializer = make_pack_expansion (mem_initializer);
11866 }
11867 if (target_ctor != error_mark_node
11868 && mem_initializer != error_mark_node)
11869 {
11870 error ("mem-initializer for %qD follows constructor delegation",
11871 TREE_PURPOSE (mem_initializer));
11872 mem_initializer = error_mark_node;
11873 }
11874 /* Look for a target constructor. */
11875 if (mem_initializer != error_mark_node
11876 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
11877 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11878 {
11879 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11880 if (mem_initializer_list)
11881 {
11882 error ("constructor delegation follows mem-initializer for %qD",
11883 TREE_PURPOSE (mem_initializer_list));
11884 mem_initializer = error_mark_node;
11885 }
11886 target_ctor = mem_initializer;
11887 }
11888 /* Add it to the list, unless it was erroneous. */
11889 if (mem_initializer != error_mark_node)
11890 {
11891 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11892 mem_initializer_list = mem_initializer;
11893 }
11894 /* If the next token is not a `,', we're done. */
11895 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11896 break;
11897 /* Consume the `,' token. */
11898 cp_lexer_consume_token (parser->lexer);
11899 }
11900
11901 /* Perform semantic analysis. */
11902 if (DECL_CONSTRUCTOR_P (current_function_decl))
11903 finish_mem_initializers (mem_initializer_list);
11904 }
11905
11906 /* Parse a mem-initializer.
11907
11908 mem-initializer:
11909 mem-initializer-id ( expression-list [opt] )
11910 mem-initializer-id braced-init-list
11911
11912 GNU extension:
11913
11914 mem-initializer:
11915 ( expression-list [opt] )
11916
11917 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11918 class) or FIELD_DECL (for a non-static data member) to initialize;
11919 the TREE_VALUE is the expression-list. An empty initialization
11920 list is represented by void_list_node. */
11921
11922 static tree
11923 cp_parser_mem_initializer (cp_parser* parser)
11924 {
11925 tree mem_initializer_id;
11926 tree expression_list;
11927 tree member;
11928 cp_token *token = cp_lexer_peek_token (parser->lexer);
11929
11930 /* Find out what is being initialized. */
11931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11932 {
11933 permerror (token->location,
11934 "anachronistic old-style base class initializer");
11935 mem_initializer_id = NULL_TREE;
11936 }
11937 else
11938 {
11939 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11940 if (mem_initializer_id == error_mark_node)
11941 return mem_initializer_id;
11942 }
11943 member = expand_member_init (mem_initializer_id);
11944 if (member && !DECL_P (member))
11945 in_base_initializer = 1;
11946
11947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11948 {
11949 bool expr_non_constant_p;
11950 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11951 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11952 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11953 expression_list = build_tree_list (NULL_TREE, expression_list);
11954 }
11955 else
11956 {
11957 vec<tree, va_gc> *vec;
11958 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11959 /*cast_p=*/false,
11960 /*allow_expansion_p=*/true,
11961 /*non_constant_p=*/NULL);
11962 if (vec == NULL)
11963 return error_mark_node;
11964 expression_list = build_tree_list_vec (vec);
11965 release_tree_vector (vec);
11966 }
11967
11968 if (expression_list == error_mark_node)
11969 return error_mark_node;
11970 if (!expression_list)
11971 expression_list = void_type_node;
11972
11973 in_base_initializer = 0;
11974
11975 return member ? build_tree_list (member, expression_list) : error_mark_node;
11976 }
11977
11978 /* Parse a mem-initializer-id.
11979
11980 mem-initializer-id:
11981 :: [opt] nested-name-specifier [opt] class-name
11982 identifier
11983
11984 Returns a TYPE indicating the class to be initializer for the first
11985 production. Returns an IDENTIFIER_NODE indicating the data member
11986 to be initialized for the second production. */
11987
11988 static tree
11989 cp_parser_mem_initializer_id (cp_parser* parser)
11990 {
11991 bool global_scope_p;
11992 bool nested_name_specifier_p;
11993 bool template_p = false;
11994 tree id;
11995
11996 cp_token *token = cp_lexer_peek_token (parser->lexer);
11997
11998 /* `typename' is not allowed in this context ([temp.res]). */
11999 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12000 {
12001 error_at (token->location,
12002 "keyword %<typename%> not allowed in this context (a qualified "
12003 "member initializer is implicitly a type)");
12004 cp_lexer_consume_token (parser->lexer);
12005 }
12006 /* Look for the optional `::' operator. */
12007 global_scope_p
12008 = (cp_parser_global_scope_opt (parser,
12009 /*current_scope_valid_p=*/false)
12010 != NULL_TREE);
12011 /* Look for the optional nested-name-specifier. The simplest way to
12012 implement:
12013
12014 [temp.res]
12015
12016 The keyword `typename' is not permitted in a base-specifier or
12017 mem-initializer; in these contexts a qualified name that
12018 depends on a template-parameter is implicitly assumed to be a
12019 type name.
12020
12021 is to assume that we have seen the `typename' keyword at this
12022 point. */
12023 nested_name_specifier_p
12024 = (cp_parser_nested_name_specifier_opt (parser,
12025 /*typename_keyword_p=*/true,
12026 /*check_dependency_p=*/true,
12027 /*type_p=*/true,
12028 /*is_declaration=*/true)
12029 != NULL_TREE);
12030 if (nested_name_specifier_p)
12031 template_p = cp_parser_optional_template_keyword (parser);
12032 /* If there is a `::' operator or a nested-name-specifier, then we
12033 are definitely looking for a class-name. */
12034 if (global_scope_p || nested_name_specifier_p)
12035 return cp_parser_class_name (parser,
12036 /*typename_keyword_p=*/true,
12037 /*template_keyword_p=*/template_p,
12038 typename_type,
12039 /*check_dependency_p=*/true,
12040 /*class_head_p=*/false,
12041 /*is_declaration=*/true);
12042 /* Otherwise, we could also be looking for an ordinary identifier. */
12043 cp_parser_parse_tentatively (parser);
12044 /* Try a class-name. */
12045 id = cp_parser_class_name (parser,
12046 /*typename_keyword_p=*/true,
12047 /*template_keyword_p=*/false,
12048 none_type,
12049 /*check_dependency_p=*/true,
12050 /*class_head_p=*/false,
12051 /*is_declaration=*/true);
12052 /* If we found one, we're done. */
12053 if (cp_parser_parse_definitely (parser))
12054 return id;
12055 /* Otherwise, look for an ordinary identifier. */
12056 return cp_parser_identifier (parser);
12057 }
12058
12059 /* Overloading [gram.over] */
12060
12061 /* Parse an operator-function-id.
12062
12063 operator-function-id:
12064 operator operator
12065
12066 Returns an IDENTIFIER_NODE for the operator which is a
12067 human-readable spelling of the identifier, e.g., `operator +'. */
12068
12069 static tree
12070 cp_parser_operator_function_id (cp_parser* parser)
12071 {
12072 /* Look for the `operator' keyword. */
12073 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12074 return error_mark_node;
12075 /* And then the name of the operator itself. */
12076 return cp_parser_operator (parser);
12077 }
12078
12079 /* Return an identifier node for a user-defined literal operator.
12080 The suffix identifier is chained to the operator name identifier. */
12081
12082 static tree
12083 cp_literal_operator_id (const char* name)
12084 {
12085 tree identifier;
12086 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12087 + strlen (name) + 10);
12088 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12089 identifier = get_identifier (buffer);
12090 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
12091
12092 return identifier;
12093 }
12094
12095 /* Parse an operator.
12096
12097 operator:
12098 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12099 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12100 || ++ -- , ->* -> () []
12101
12102 GNU Extensions:
12103
12104 operator:
12105 <? >? <?= >?=
12106
12107 Returns an IDENTIFIER_NODE for the operator which is a
12108 human-readable spelling of the identifier, e.g., `operator +'. */
12109
12110 static tree
12111 cp_parser_operator (cp_parser* parser)
12112 {
12113 tree id = NULL_TREE;
12114 cp_token *token;
12115
12116 /* Peek at the next token. */
12117 token = cp_lexer_peek_token (parser->lexer);
12118 /* Figure out which operator we have. */
12119 switch (token->type)
12120 {
12121 case CPP_KEYWORD:
12122 {
12123 enum tree_code op;
12124
12125 /* The keyword should be either `new' or `delete'. */
12126 if (token->keyword == RID_NEW)
12127 op = NEW_EXPR;
12128 else if (token->keyword == RID_DELETE)
12129 op = DELETE_EXPR;
12130 else
12131 break;
12132
12133 /* Consume the `new' or `delete' token. */
12134 cp_lexer_consume_token (parser->lexer);
12135
12136 /* Peek at the next token. */
12137 token = cp_lexer_peek_token (parser->lexer);
12138 /* If it's a `[' token then this is the array variant of the
12139 operator. */
12140 if (token->type == CPP_OPEN_SQUARE)
12141 {
12142 /* Consume the `[' token. */
12143 cp_lexer_consume_token (parser->lexer);
12144 /* Look for the `]' token. */
12145 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12146 id = ansi_opname (op == NEW_EXPR
12147 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12148 }
12149 /* Otherwise, we have the non-array variant. */
12150 else
12151 id = ansi_opname (op);
12152
12153 return id;
12154 }
12155
12156 case CPP_PLUS:
12157 id = ansi_opname (PLUS_EXPR);
12158 break;
12159
12160 case CPP_MINUS:
12161 id = ansi_opname (MINUS_EXPR);
12162 break;
12163
12164 case CPP_MULT:
12165 id = ansi_opname (MULT_EXPR);
12166 break;
12167
12168 case CPP_DIV:
12169 id = ansi_opname (TRUNC_DIV_EXPR);
12170 break;
12171
12172 case CPP_MOD:
12173 id = ansi_opname (TRUNC_MOD_EXPR);
12174 break;
12175
12176 case CPP_XOR:
12177 id = ansi_opname (BIT_XOR_EXPR);
12178 break;
12179
12180 case CPP_AND:
12181 id = ansi_opname (BIT_AND_EXPR);
12182 break;
12183
12184 case CPP_OR:
12185 id = ansi_opname (BIT_IOR_EXPR);
12186 break;
12187
12188 case CPP_COMPL:
12189 id = ansi_opname (BIT_NOT_EXPR);
12190 break;
12191
12192 case CPP_NOT:
12193 id = ansi_opname (TRUTH_NOT_EXPR);
12194 break;
12195
12196 case CPP_EQ:
12197 id = ansi_assopname (NOP_EXPR);
12198 break;
12199
12200 case CPP_LESS:
12201 id = ansi_opname (LT_EXPR);
12202 break;
12203
12204 case CPP_GREATER:
12205 id = ansi_opname (GT_EXPR);
12206 break;
12207
12208 case CPP_PLUS_EQ:
12209 id = ansi_assopname (PLUS_EXPR);
12210 break;
12211
12212 case CPP_MINUS_EQ:
12213 id = ansi_assopname (MINUS_EXPR);
12214 break;
12215
12216 case CPP_MULT_EQ:
12217 id = ansi_assopname (MULT_EXPR);
12218 break;
12219
12220 case CPP_DIV_EQ:
12221 id = ansi_assopname (TRUNC_DIV_EXPR);
12222 break;
12223
12224 case CPP_MOD_EQ:
12225 id = ansi_assopname (TRUNC_MOD_EXPR);
12226 break;
12227
12228 case CPP_XOR_EQ:
12229 id = ansi_assopname (BIT_XOR_EXPR);
12230 break;
12231
12232 case CPP_AND_EQ:
12233 id = ansi_assopname (BIT_AND_EXPR);
12234 break;
12235
12236 case CPP_OR_EQ:
12237 id = ansi_assopname (BIT_IOR_EXPR);
12238 break;
12239
12240 case CPP_LSHIFT:
12241 id = ansi_opname (LSHIFT_EXPR);
12242 break;
12243
12244 case CPP_RSHIFT:
12245 id = ansi_opname (RSHIFT_EXPR);
12246 break;
12247
12248 case CPP_LSHIFT_EQ:
12249 id = ansi_assopname (LSHIFT_EXPR);
12250 break;
12251
12252 case CPP_RSHIFT_EQ:
12253 id = ansi_assopname (RSHIFT_EXPR);
12254 break;
12255
12256 case CPP_EQ_EQ:
12257 id = ansi_opname (EQ_EXPR);
12258 break;
12259
12260 case CPP_NOT_EQ:
12261 id = ansi_opname (NE_EXPR);
12262 break;
12263
12264 case CPP_LESS_EQ:
12265 id = ansi_opname (LE_EXPR);
12266 break;
12267
12268 case CPP_GREATER_EQ:
12269 id = ansi_opname (GE_EXPR);
12270 break;
12271
12272 case CPP_AND_AND:
12273 id = ansi_opname (TRUTH_ANDIF_EXPR);
12274 break;
12275
12276 case CPP_OR_OR:
12277 id = ansi_opname (TRUTH_ORIF_EXPR);
12278 break;
12279
12280 case CPP_PLUS_PLUS:
12281 id = ansi_opname (POSTINCREMENT_EXPR);
12282 break;
12283
12284 case CPP_MINUS_MINUS:
12285 id = ansi_opname (PREDECREMENT_EXPR);
12286 break;
12287
12288 case CPP_COMMA:
12289 id = ansi_opname (COMPOUND_EXPR);
12290 break;
12291
12292 case CPP_DEREF_STAR:
12293 id = ansi_opname (MEMBER_REF);
12294 break;
12295
12296 case CPP_DEREF:
12297 id = ansi_opname (COMPONENT_REF);
12298 break;
12299
12300 case CPP_OPEN_PAREN:
12301 /* Consume the `('. */
12302 cp_lexer_consume_token (parser->lexer);
12303 /* Look for the matching `)'. */
12304 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12305 return ansi_opname (CALL_EXPR);
12306
12307 case CPP_OPEN_SQUARE:
12308 /* Consume the `['. */
12309 cp_lexer_consume_token (parser->lexer);
12310 /* Look for the matching `]'. */
12311 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12312 return ansi_opname (ARRAY_REF);
12313
12314 case CPP_STRING:
12315 if (cxx_dialect == cxx98)
12316 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12317 if (TREE_STRING_LENGTH (token->u.value) > 2)
12318 {
12319 error ("expected empty string after %<operator%> keyword");
12320 return error_mark_node;
12321 }
12322 /* Consume the string. */
12323 cp_lexer_consume_token (parser->lexer);
12324 /* Look for the suffix identifier. */
12325 token = cp_lexer_peek_token (parser->lexer);
12326 if (token->type == CPP_NAME)
12327 {
12328 id = cp_parser_identifier (parser);
12329 if (id != error_mark_node)
12330 {
12331 const char *name = IDENTIFIER_POINTER (id);
12332 return cp_literal_operator_id (name);
12333 }
12334 }
12335 else
12336 {
12337 error ("expected suffix identifier");
12338 return error_mark_node;
12339 }
12340
12341 case CPP_STRING_USERDEF:
12342 error ("missing space between %<\"\"%> and suffix identifier");
12343 return error_mark_node;
12344
12345 default:
12346 /* Anything else is an error. */
12347 break;
12348 }
12349
12350 /* If we have selected an identifier, we need to consume the
12351 operator token. */
12352 if (id)
12353 cp_lexer_consume_token (parser->lexer);
12354 /* Otherwise, no valid operator name was present. */
12355 else
12356 {
12357 cp_parser_error (parser, "expected operator");
12358 id = error_mark_node;
12359 }
12360
12361 return id;
12362 }
12363
12364 /* Parse a template-declaration.
12365
12366 template-declaration:
12367 export [opt] template < template-parameter-list > declaration
12368
12369 If MEMBER_P is TRUE, this template-declaration occurs within a
12370 class-specifier.
12371
12372 The grammar rule given by the standard isn't correct. What
12373 is really meant is:
12374
12375 template-declaration:
12376 export [opt] template-parameter-list-seq
12377 decl-specifier-seq [opt] init-declarator [opt] ;
12378 export [opt] template-parameter-list-seq
12379 function-definition
12380
12381 template-parameter-list-seq:
12382 template-parameter-list-seq [opt]
12383 template < template-parameter-list > */
12384
12385 static void
12386 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12387 {
12388 /* Check for `export'. */
12389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12390 {
12391 /* Consume the `export' token. */
12392 cp_lexer_consume_token (parser->lexer);
12393 /* Warn that we do not support `export'. */
12394 warning (0, "keyword %<export%> not implemented, and will be ignored");
12395 }
12396
12397 cp_parser_template_declaration_after_export (parser, member_p);
12398 }
12399
12400 /* Parse a template-parameter-list.
12401
12402 template-parameter-list:
12403 template-parameter
12404 template-parameter-list , template-parameter
12405
12406 Returns a TREE_LIST. Each node represents a template parameter.
12407 The nodes are connected via their TREE_CHAINs. */
12408
12409 static tree
12410 cp_parser_template_parameter_list (cp_parser* parser)
12411 {
12412 tree parameter_list = NULL_TREE;
12413
12414 begin_template_parm_list ();
12415
12416 /* The loop below parses the template parms. We first need to know
12417 the total number of template parms to be able to compute proper
12418 canonical types of each dependent type. So after the loop, when
12419 we know the total number of template parms,
12420 end_template_parm_list computes the proper canonical types and
12421 fixes up the dependent types accordingly. */
12422 while (true)
12423 {
12424 tree parameter;
12425 bool is_non_type;
12426 bool is_parameter_pack;
12427 location_t parm_loc;
12428
12429 /* Parse the template-parameter. */
12430 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12431 parameter = cp_parser_template_parameter (parser,
12432 &is_non_type,
12433 &is_parameter_pack);
12434 /* Add it to the list. */
12435 if (parameter != error_mark_node)
12436 parameter_list = process_template_parm (parameter_list,
12437 parm_loc,
12438 parameter,
12439 is_non_type,
12440 is_parameter_pack);
12441 else
12442 {
12443 tree err_parm = build_tree_list (parameter, parameter);
12444 parameter_list = chainon (parameter_list, err_parm);
12445 }
12446
12447 /* If the next token is not a `,', we're done. */
12448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12449 break;
12450 /* Otherwise, consume the `,' token. */
12451 cp_lexer_consume_token (parser->lexer);
12452 }
12453
12454 return end_template_parm_list (parameter_list);
12455 }
12456
12457 /* Parse a template-parameter.
12458
12459 template-parameter:
12460 type-parameter
12461 parameter-declaration
12462
12463 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12464 the parameter. The TREE_PURPOSE is the default value, if any.
12465 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12466 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12467 set to true iff this parameter is a parameter pack. */
12468
12469 static tree
12470 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12471 bool *is_parameter_pack)
12472 {
12473 cp_token *token;
12474 cp_parameter_declarator *parameter_declarator;
12475 cp_declarator *id_declarator;
12476 tree parm;
12477
12478 /* Assume it is a type parameter or a template parameter. */
12479 *is_non_type = false;
12480 /* Assume it not a parameter pack. */
12481 *is_parameter_pack = false;
12482 /* Peek at the next token. */
12483 token = cp_lexer_peek_token (parser->lexer);
12484 /* If it is `class' or `template', we have a type-parameter. */
12485 if (token->keyword == RID_TEMPLATE)
12486 return cp_parser_type_parameter (parser, is_parameter_pack);
12487 /* If it is `class' or `typename' we do not know yet whether it is a
12488 type parameter or a non-type parameter. Consider:
12489
12490 template <typename T, typename T::X X> ...
12491
12492 or:
12493
12494 template <class C, class D*> ...
12495
12496 Here, the first parameter is a type parameter, and the second is
12497 a non-type parameter. We can tell by looking at the token after
12498 the identifier -- if it is a `,', `=', or `>' then we have a type
12499 parameter. */
12500 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12501 {
12502 /* Peek at the token after `class' or `typename'. */
12503 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12504 /* If it's an ellipsis, we have a template type parameter
12505 pack. */
12506 if (token->type == CPP_ELLIPSIS)
12507 return cp_parser_type_parameter (parser, is_parameter_pack);
12508 /* If it's an identifier, skip it. */
12509 if (token->type == CPP_NAME)
12510 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12511 /* Now, see if the token looks like the end of a template
12512 parameter. */
12513 if (token->type == CPP_COMMA
12514 || token->type == CPP_EQ
12515 || token->type == CPP_GREATER)
12516 return cp_parser_type_parameter (parser, is_parameter_pack);
12517 }
12518
12519 /* Otherwise, it is a non-type parameter.
12520
12521 [temp.param]
12522
12523 When parsing a default template-argument for a non-type
12524 template-parameter, the first non-nested `>' is taken as the end
12525 of the template parameter-list rather than a greater-than
12526 operator. */
12527 *is_non_type = true;
12528 parameter_declarator
12529 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12530 /*parenthesized_p=*/NULL);
12531
12532 /* If the parameter declaration is marked as a parameter pack, set
12533 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12534 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12535 grokdeclarator. */
12536 if (parameter_declarator
12537 && parameter_declarator->declarator
12538 && parameter_declarator->declarator->parameter_pack_p)
12539 {
12540 *is_parameter_pack = true;
12541 parameter_declarator->declarator->parameter_pack_p = false;
12542 }
12543
12544 if (parameter_declarator
12545 && parameter_declarator->default_argument)
12546 {
12547 /* Can happen in some cases of erroneous input (c++/34892). */
12548 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12549 /* Consume the `...' for better error recovery. */
12550 cp_lexer_consume_token (parser->lexer);
12551 }
12552 /* If the next token is an ellipsis, and we don't already have it
12553 marked as a parameter pack, then we have a parameter pack (that
12554 has no declarator). */
12555 else if (!*is_parameter_pack
12556 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12557 && (declarator_can_be_parameter_pack
12558 (parameter_declarator->declarator)))
12559 {
12560 /* Consume the `...'. */
12561 cp_lexer_consume_token (parser->lexer);
12562 maybe_warn_variadic_templates ();
12563
12564 *is_parameter_pack = true;
12565 }
12566 /* We might end up with a pack expansion as the type of the non-type
12567 template parameter, in which case this is a non-type template
12568 parameter pack. */
12569 else if (parameter_declarator
12570 && parameter_declarator->decl_specifiers.type
12571 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12572 {
12573 *is_parameter_pack = true;
12574 parameter_declarator->decl_specifiers.type =
12575 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12576 }
12577
12578 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12579 {
12580 /* Parameter packs cannot have default arguments. However, a
12581 user may try to do so, so we'll parse them and give an
12582 appropriate diagnostic here. */
12583
12584 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12585
12586 /* Find the name of the parameter pack. */
12587 id_declarator = parameter_declarator->declarator;
12588 while (id_declarator && id_declarator->kind != cdk_id)
12589 id_declarator = id_declarator->declarator;
12590
12591 if (id_declarator && id_declarator->kind == cdk_id)
12592 error_at (start_token->location,
12593 "template parameter pack %qD cannot have a default argument",
12594 id_declarator->u.id.unqualified_name);
12595 else
12596 error_at (start_token->location,
12597 "template parameter pack cannot have a default argument");
12598
12599 /* Parse the default argument, but throw away the result. */
12600 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12601 }
12602
12603 parm = grokdeclarator (parameter_declarator->declarator,
12604 &parameter_declarator->decl_specifiers,
12605 TPARM, /*initialized=*/0,
12606 /*attrlist=*/NULL);
12607 if (parm == error_mark_node)
12608 return error_mark_node;
12609
12610 return build_tree_list (parameter_declarator->default_argument, parm);
12611 }
12612
12613 /* Parse a type-parameter.
12614
12615 type-parameter:
12616 class identifier [opt]
12617 class identifier [opt] = type-id
12618 typename identifier [opt]
12619 typename identifier [opt] = type-id
12620 template < template-parameter-list > class identifier [opt]
12621 template < template-parameter-list > class identifier [opt]
12622 = id-expression
12623
12624 GNU Extension (variadic templates):
12625
12626 type-parameter:
12627 class ... identifier [opt]
12628 typename ... identifier [opt]
12629
12630 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12631 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12632 the declaration of the parameter.
12633
12634 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12635
12636 static tree
12637 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12638 {
12639 cp_token *token;
12640 tree parameter;
12641
12642 /* Look for a keyword to tell us what kind of parameter this is. */
12643 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12644 if (!token)
12645 return error_mark_node;
12646
12647 switch (token->keyword)
12648 {
12649 case RID_CLASS:
12650 case RID_TYPENAME:
12651 {
12652 tree identifier;
12653 tree default_argument;
12654
12655 /* If the next token is an ellipsis, we have a template
12656 argument pack. */
12657 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12658 {
12659 /* Consume the `...' token. */
12660 cp_lexer_consume_token (parser->lexer);
12661 maybe_warn_variadic_templates ();
12662
12663 *is_parameter_pack = true;
12664 }
12665
12666 /* If the next token is an identifier, then it names the
12667 parameter. */
12668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12669 identifier = cp_parser_identifier (parser);
12670 else
12671 identifier = NULL_TREE;
12672
12673 /* Create the parameter. */
12674 parameter = finish_template_type_parm (class_type_node, identifier);
12675
12676 /* If the next token is an `=', we have a default argument. */
12677 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12678 {
12679 /* Consume the `=' token. */
12680 cp_lexer_consume_token (parser->lexer);
12681 /* Parse the default-argument. */
12682 push_deferring_access_checks (dk_no_deferred);
12683 default_argument = cp_parser_type_id (parser);
12684
12685 /* Template parameter packs cannot have default
12686 arguments. */
12687 if (*is_parameter_pack)
12688 {
12689 if (identifier)
12690 error_at (token->location,
12691 "template parameter pack %qD cannot have a "
12692 "default argument", identifier);
12693 else
12694 error_at (token->location,
12695 "template parameter packs cannot have "
12696 "default arguments");
12697 default_argument = NULL_TREE;
12698 }
12699 pop_deferring_access_checks ();
12700 }
12701 else
12702 default_argument = NULL_TREE;
12703
12704 /* Create the combined representation of the parameter and the
12705 default argument. */
12706 parameter = build_tree_list (default_argument, parameter);
12707 }
12708 break;
12709
12710 case RID_TEMPLATE:
12711 {
12712 tree identifier;
12713 tree default_argument;
12714
12715 /* Look for the `<'. */
12716 cp_parser_require (parser, CPP_LESS, RT_LESS);
12717 /* Parse the template-parameter-list. */
12718 cp_parser_template_parameter_list (parser);
12719 /* Look for the `>'. */
12720 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12721 /* Look for the `class' keyword. */
12722 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12723 /* If the next token is an ellipsis, we have a template
12724 argument pack. */
12725 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12726 {
12727 /* Consume the `...' token. */
12728 cp_lexer_consume_token (parser->lexer);
12729 maybe_warn_variadic_templates ();
12730
12731 *is_parameter_pack = true;
12732 }
12733 /* If the next token is an `=', then there is a
12734 default-argument. If the next token is a `>', we are at
12735 the end of the parameter-list. If the next token is a `,',
12736 then we are at the end of this parameter. */
12737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12738 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12739 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12740 {
12741 identifier = cp_parser_identifier (parser);
12742 /* Treat invalid names as if the parameter were nameless. */
12743 if (identifier == error_mark_node)
12744 identifier = NULL_TREE;
12745 }
12746 else
12747 identifier = NULL_TREE;
12748
12749 /* Create the template parameter. */
12750 parameter = finish_template_template_parm (class_type_node,
12751 identifier);
12752
12753 /* If the next token is an `=', then there is a
12754 default-argument. */
12755 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12756 {
12757 bool is_template;
12758
12759 /* Consume the `='. */
12760 cp_lexer_consume_token (parser->lexer);
12761 /* Parse the id-expression. */
12762 push_deferring_access_checks (dk_no_deferred);
12763 /* save token before parsing the id-expression, for error
12764 reporting */
12765 token = cp_lexer_peek_token (parser->lexer);
12766 default_argument
12767 = cp_parser_id_expression (parser,
12768 /*template_keyword_p=*/false,
12769 /*check_dependency_p=*/true,
12770 /*template_p=*/&is_template,
12771 /*declarator_p=*/false,
12772 /*optional_p=*/false);
12773 if (TREE_CODE (default_argument) == TYPE_DECL)
12774 /* If the id-expression was a template-id that refers to
12775 a template-class, we already have the declaration here,
12776 so no further lookup is needed. */
12777 ;
12778 else
12779 /* Look up the name. */
12780 default_argument
12781 = cp_parser_lookup_name (parser, default_argument,
12782 none_type,
12783 /*is_template=*/is_template,
12784 /*is_namespace=*/false,
12785 /*check_dependency=*/true,
12786 /*ambiguous_decls=*/NULL,
12787 token->location);
12788 /* See if the default argument is valid. */
12789 default_argument
12790 = check_template_template_default_arg (default_argument);
12791
12792 /* Template parameter packs cannot have default
12793 arguments. */
12794 if (*is_parameter_pack)
12795 {
12796 if (identifier)
12797 error_at (token->location,
12798 "template parameter pack %qD cannot "
12799 "have a default argument",
12800 identifier);
12801 else
12802 error_at (token->location, "template parameter packs cannot "
12803 "have default arguments");
12804 default_argument = NULL_TREE;
12805 }
12806 pop_deferring_access_checks ();
12807 }
12808 else
12809 default_argument = NULL_TREE;
12810
12811 /* Create the combined representation of the parameter and the
12812 default argument. */
12813 parameter = build_tree_list (default_argument, parameter);
12814 }
12815 break;
12816
12817 default:
12818 gcc_unreachable ();
12819 break;
12820 }
12821
12822 return parameter;
12823 }
12824
12825 /* Parse a template-id.
12826
12827 template-id:
12828 template-name < template-argument-list [opt] >
12829
12830 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12831 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12832 returned. Otherwise, if the template-name names a function, or set
12833 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12834 names a class, returns a TYPE_DECL for the specialization.
12835
12836 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12837 uninstantiated templates. */
12838
12839 static tree
12840 cp_parser_template_id (cp_parser *parser,
12841 bool template_keyword_p,
12842 bool check_dependency_p,
12843 enum tag_types tag_type,
12844 bool is_declaration)
12845 {
12846 int i;
12847 tree templ;
12848 tree arguments;
12849 tree template_id;
12850 cp_token_position start_of_id = 0;
12851 deferred_access_check *chk;
12852 vec<deferred_access_check, va_gc> *access_check;
12853 cp_token *next_token = NULL, *next_token_2 = NULL;
12854 bool is_identifier;
12855
12856 /* If the next token corresponds to a template-id, there is no need
12857 to reparse it. */
12858 next_token = cp_lexer_peek_token (parser->lexer);
12859 if (next_token->type == CPP_TEMPLATE_ID)
12860 {
12861 struct tree_check *check_value;
12862
12863 /* Get the stored value. */
12864 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12865 /* Perform any access checks that were deferred. */
12866 access_check = check_value->checks;
12867 if (access_check)
12868 {
12869 FOR_EACH_VEC_ELT (*access_check, i, chk)
12870 perform_or_defer_access_check (chk->binfo,
12871 chk->decl,
12872 chk->diag_decl,
12873 tf_warning_or_error);
12874 }
12875 /* Return the stored value. */
12876 return check_value->value;
12877 }
12878
12879 /* Avoid performing name lookup if there is no possibility of
12880 finding a template-id. */
12881 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12882 || (next_token->type == CPP_NAME
12883 && !cp_parser_nth_token_starts_template_argument_list_p
12884 (parser, 2)))
12885 {
12886 cp_parser_error (parser, "expected template-id");
12887 return error_mark_node;
12888 }
12889
12890 /* Remember where the template-id starts. */
12891 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12892 start_of_id = cp_lexer_token_position (parser->lexer, false);
12893
12894 push_deferring_access_checks (dk_deferred);
12895
12896 /* Parse the template-name. */
12897 is_identifier = false;
12898 templ = cp_parser_template_name (parser, template_keyword_p,
12899 check_dependency_p,
12900 is_declaration,
12901 tag_type,
12902 &is_identifier);
12903 if (templ == error_mark_node || is_identifier)
12904 {
12905 pop_deferring_access_checks ();
12906 return templ;
12907 }
12908
12909 /* If we find the sequence `[:' after a template-name, it's probably
12910 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12911 parse correctly the argument list. */
12912 next_token = cp_lexer_peek_token (parser->lexer);
12913 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12914 if (next_token->type == CPP_OPEN_SQUARE
12915 && next_token->flags & DIGRAPH
12916 && next_token_2->type == CPP_COLON
12917 && !(next_token_2->flags & PREV_WHITE))
12918 {
12919 cp_parser_parse_tentatively (parser);
12920 /* Change `:' into `::'. */
12921 next_token_2->type = CPP_SCOPE;
12922 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12923 CPP_LESS. */
12924 cp_lexer_consume_token (parser->lexer);
12925
12926 /* Parse the arguments. */
12927 arguments = cp_parser_enclosed_template_argument_list (parser);
12928 if (!cp_parser_parse_definitely (parser))
12929 {
12930 /* If we couldn't parse an argument list, then we revert our changes
12931 and return simply an error. Maybe this is not a template-id
12932 after all. */
12933 next_token_2->type = CPP_COLON;
12934 cp_parser_error (parser, "expected %<<%>");
12935 pop_deferring_access_checks ();
12936 return error_mark_node;
12937 }
12938 /* Otherwise, emit an error about the invalid digraph, but continue
12939 parsing because we got our argument list. */
12940 if (permerror (next_token->location,
12941 "%<<::%> cannot begin a template-argument list"))
12942 {
12943 static bool hint = false;
12944 inform (next_token->location,
12945 "%<<:%> is an alternate spelling for %<[%>."
12946 " Insert whitespace between %<<%> and %<::%>");
12947 if (!hint && !flag_permissive)
12948 {
12949 inform (next_token->location, "(if you use %<-fpermissive%> "
12950 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12951 "accept your code)");
12952 hint = true;
12953 }
12954 }
12955 }
12956 else
12957 {
12958 /* Look for the `<' that starts the template-argument-list. */
12959 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12960 {
12961 pop_deferring_access_checks ();
12962 return error_mark_node;
12963 }
12964 /* Parse the arguments. */
12965 arguments = cp_parser_enclosed_template_argument_list (parser);
12966 }
12967
12968 /* Build a representation of the specialization. */
12969 if (identifier_p (templ))
12970 template_id = build_min_nt_loc (next_token->location,
12971 TEMPLATE_ID_EXPR,
12972 templ, arguments);
12973 else if (DECL_TYPE_TEMPLATE_P (templ)
12974 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12975 {
12976 bool entering_scope;
12977 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12978 template (rather than some instantiation thereof) only if
12979 is not nested within some other construct. For example, in
12980 "template <typename T> void f(T) { A<T>::", A<T> is just an
12981 instantiation of A. */
12982 entering_scope = (template_parm_scope_p ()
12983 && cp_lexer_next_token_is (parser->lexer,
12984 CPP_SCOPE));
12985 template_id
12986 = finish_template_type (templ, arguments, entering_scope);
12987 }
12988 else
12989 {
12990 /* If it's not a class-template or a template-template, it should be
12991 a function-template. */
12992 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12993 || TREE_CODE (templ) == OVERLOAD
12994 || BASELINK_P (templ)));
12995
12996 template_id = lookup_template_function (templ, arguments);
12997 }
12998
12999 /* If parsing tentatively, replace the sequence of tokens that makes
13000 up the template-id with a CPP_TEMPLATE_ID token. That way,
13001 should we re-parse the token stream, we will not have to repeat
13002 the effort required to do the parse, nor will we issue duplicate
13003 error messages about problems during instantiation of the
13004 template. */
13005 if (start_of_id)
13006 {
13007 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13008
13009 /* Reset the contents of the START_OF_ID token. */
13010 token->type = CPP_TEMPLATE_ID;
13011 /* Retrieve any deferred checks. Do not pop this access checks yet
13012 so the memory will not be reclaimed during token replacing below. */
13013 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13014 token->u.tree_check_value->value = template_id;
13015 token->u.tree_check_value->checks = get_deferred_access_checks ();
13016 token->keyword = RID_MAX;
13017
13018 /* Purge all subsequent tokens. */
13019 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13020
13021 /* ??? Can we actually assume that, if template_id ==
13022 error_mark_node, we will have issued a diagnostic to the
13023 user, as opposed to simply marking the tentative parse as
13024 failed? */
13025 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13026 error_at (token->location, "parse error in template argument list");
13027 }
13028
13029 pop_to_parent_deferring_access_checks ();
13030 return template_id;
13031 }
13032
13033 /* Parse a template-name.
13034
13035 template-name:
13036 identifier
13037
13038 The standard should actually say:
13039
13040 template-name:
13041 identifier
13042 operator-function-id
13043
13044 A defect report has been filed about this issue.
13045
13046 A conversion-function-id cannot be a template name because they cannot
13047 be part of a template-id. In fact, looking at this code:
13048
13049 a.operator K<int>()
13050
13051 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13052 It is impossible to call a templated conversion-function-id with an
13053 explicit argument list, since the only allowed template parameter is
13054 the type to which it is converting.
13055
13056 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13057 `template' keyword, in a construction like:
13058
13059 T::template f<3>()
13060
13061 In that case `f' is taken to be a template-name, even though there
13062 is no way of knowing for sure.
13063
13064 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13065 name refers to a set of overloaded functions, at least one of which
13066 is a template, or an IDENTIFIER_NODE with the name of the template,
13067 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13068 names are looked up inside uninstantiated templates. */
13069
13070 static tree
13071 cp_parser_template_name (cp_parser* parser,
13072 bool template_keyword_p,
13073 bool check_dependency_p,
13074 bool is_declaration,
13075 enum tag_types tag_type,
13076 bool *is_identifier)
13077 {
13078 tree identifier;
13079 tree decl;
13080 tree fns;
13081 cp_token *token = cp_lexer_peek_token (parser->lexer);
13082
13083 /* If the next token is `operator', then we have either an
13084 operator-function-id or a conversion-function-id. */
13085 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13086 {
13087 /* We don't know whether we're looking at an
13088 operator-function-id or a conversion-function-id. */
13089 cp_parser_parse_tentatively (parser);
13090 /* Try an operator-function-id. */
13091 identifier = cp_parser_operator_function_id (parser);
13092 /* If that didn't work, try a conversion-function-id. */
13093 if (!cp_parser_parse_definitely (parser))
13094 {
13095 cp_parser_error (parser, "expected template-name");
13096 return error_mark_node;
13097 }
13098 }
13099 /* Look for the identifier. */
13100 else
13101 identifier = cp_parser_identifier (parser);
13102
13103 /* If we didn't find an identifier, we don't have a template-id. */
13104 if (identifier == error_mark_node)
13105 return error_mark_node;
13106
13107 /* If the name immediately followed the `template' keyword, then it
13108 is a template-name. However, if the next token is not `<', then
13109 we do not treat it as a template-name, since it is not being used
13110 as part of a template-id. This enables us to handle constructs
13111 like:
13112
13113 template <typename T> struct S { S(); };
13114 template <typename T> S<T>::S();
13115
13116 correctly. We would treat `S' as a template -- if it were `S<T>'
13117 -- but we do not if there is no `<'. */
13118
13119 if (processing_template_decl
13120 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13121 {
13122 /* In a declaration, in a dependent context, we pretend that the
13123 "template" keyword was present in order to improve error
13124 recovery. For example, given:
13125
13126 template <typename T> void f(T::X<int>);
13127
13128 we want to treat "X<int>" as a template-id. */
13129 if (is_declaration
13130 && !template_keyword_p
13131 && parser->scope && TYPE_P (parser->scope)
13132 && check_dependency_p
13133 && dependent_scope_p (parser->scope)
13134 /* Do not do this for dtors (or ctors), since they never
13135 need the template keyword before their name. */
13136 && !constructor_name_p (identifier, parser->scope))
13137 {
13138 cp_token_position start = 0;
13139
13140 /* Explain what went wrong. */
13141 error_at (token->location, "non-template %qD used as template",
13142 identifier);
13143 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13144 parser->scope, identifier);
13145 /* If parsing tentatively, find the location of the "<" token. */
13146 if (cp_parser_simulate_error (parser))
13147 start = cp_lexer_token_position (parser->lexer, true);
13148 /* Parse the template arguments so that we can issue error
13149 messages about them. */
13150 cp_lexer_consume_token (parser->lexer);
13151 cp_parser_enclosed_template_argument_list (parser);
13152 /* Skip tokens until we find a good place from which to
13153 continue parsing. */
13154 cp_parser_skip_to_closing_parenthesis (parser,
13155 /*recovering=*/true,
13156 /*or_comma=*/true,
13157 /*consume_paren=*/false);
13158 /* If parsing tentatively, permanently remove the
13159 template argument list. That will prevent duplicate
13160 error messages from being issued about the missing
13161 "template" keyword. */
13162 if (start)
13163 cp_lexer_purge_tokens_after (parser->lexer, start);
13164 if (is_identifier)
13165 *is_identifier = true;
13166 return identifier;
13167 }
13168
13169 /* If the "template" keyword is present, then there is generally
13170 no point in doing name-lookup, so we just return IDENTIFIER.
13171 But, if the qualifying scope is non-dependent then we can
13172 (and must) do name-lookup normally. */
13173 if (template_keyword_p
13174 && (!parser->scope
13175 || (TYPE_P (parser->scope)
13176 && dependent_type_p (parser->scope))))
13177 return identifier;
13178 }
13179
13180 /* Look up the name. */
13181 decl = cp_parser_lookup_name (parser, identifier,
13182 tag_type,
13183 /*is_template=*/true,
13184 /*is_namespace=*/false,
13185 check_dependency_p,
13186 /*ambiguous_decls=*/NULL,
13187 token->location);
13188
13189 /* If DECL is a template, then the name was a template-name. */
13190 if (TREE_CODE (decl) == TEMPLATE_DECL)
13191 ;
13192 else
13193 {
13194 tree fn = NULL_TREE;
13195
13196 /* The standard does not explicitly indicate whether a name that
13197 names a set of overloaded declarations, some of which are
13198 templates, is a template-name. However, such a name should
13199 be a template-name; otherwise, there is no way to form a
13200 template-id for the overloaded templates. */
13201 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13202 if (TREE_CODE (fns) == OVERLOAD)
13203 for (fn = fns; fn; fn = OVL_NEXT (fn))
13204 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13205 break;
13206
13207 if (!fn)
13208 {
13209 /* The name does not name a template. */
13210 cp_parser_error (parser, "expected template-name");
13211 return error_mark_node;
13212 }
13213 }
13214
13215 /* If DECL is dependent, and refers to a function, then just return
13216 its name; we will look it up again during template instantiation. */
13217 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13218 {
13219 tree scope = ovl_scope (decl);
13220 if (TYPE_P (scope) && dependent_type_p (scope))
13221 return identifier;
13222 }
13223
13224 return decl;
13225 }
13226
13227 /* Parse a template-argument-list.
13228
13229 template-argument-list:
13230 template-argument ... [opt]
13231 template-argument-list , template-argument ... [opt]
13232
13233 Returns a TREE_VEC containing the arguments. */
13234
13235 static tree
13236 cp_parser_template_argument_list (cp_parser* parser)
13237 {
13238 tree fixed_args[10];
13239 unsigned n_args = 0;
13240 unsigned alloced = 10;
13241 tree *arg_ary = fixed_args;
13242 tree vec;
13243 bool saved_in_template_argument_list_p;
13244 bool saved_ice_p;
13245 bool saved_non_ice_p;
13246
13247 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13248 parser->in_template_argument_list_p = true;
13249 /* Even if the template-id appears in an integral
13250 constant-expression, the contents of the argument list do
13251 not. */
13252 saved_ice_p = parser->integral_constant_expression_p;
13253 parser->integral_constant_expression_p = false;
13254 saved_non_ice_p = parser->non_integral_constant_expression_p;
13255 parser->non_integral_constant_expression_p = false;
13256
13257 /* Parse the arguments. */
13258 do
13259 {
13260 tree argument;
13261
13262 if (n_args)
13263 /* Consume the comma. */
13264 cp_lexer_consume_token (parser->lexer);
13265
13266 /* Parse the template-argument. */
13267 argument = cp_parser_template_argument (parser);
13268
13269 /* If the next token is an ellipsis, we're expanding a template
13270 argument pack. */
13271 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13272 {
13273 if (argument == error_mark_node)
13274 {
13275 cp_token *token = cp_lexer_peek_token (parser->lexer);
13276 error_at (token->location,
13277 "expected parameter pack before %<...%>");
13278 }
13279 /* Consume the `...' token. */
13280 cp_lexer_consume_token (parser->lexer);
13281
13282 /* Make the argument into a TYPE_PACK_EXPANSION or
13283 EXPR_PACK_EXPANSION. */
13284 argument = make_pack_expansion (argument);
13285 }
13286
13287 if (n_args == alloced)
13288 {
13289 alloced *= 2;
13290
13291 if (arg_ary == fixed_args)
13292 {
13293 arg_ary = XNEWVEC (tree, alloced);
13294 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13295 }
13296 else
13297 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13298 }
13299 arg_ary[n_args++] = argument;
13300 }
13301 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13302
13303 vec = make_tree_vec (n_args);
13304
13305 while (n_args--)
13306 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13307
13308 if (arg_ary != fixed_args)
13309 free (arg_ary);
13310 parser->non_integral_constant_expression_p = saved_non_ice_p;
13311 parser->integral_constant_expression_p = saved_ice_p;
13312 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13313 #ifdef ENABLE_CHECKING
13314 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13315 #endif
13316 return vec;
13317 }
13318
13319 /* Parse a template-argument.
13320
13321 template-argument:
13322 assignment-expression
13323 type-id
13324 id-expression
13325
13326 The representation is that of an assignment-expression, type-id, or
13327 id-expression -- except that the qualified id-expression is
13328 evaluated, so that the value returned is either a DECL or an
13329 OVERLOAD.
13330
13331 Although the standard says "assignment-expression", it forbids
13332 throw-expressions or assignments in the template argument.
13333 Therefore, we use "conditional-expression" instead. */
13334
13335 static tree
13336 cp_parser_template_argument (cp_parser* parser)
13337 {
13338 tree argument;
13339 bool template_p;
13340 bool address_p;
13341 bool maybe_type_id = false;
13342 cp_token *token = NULL, *argument_start_token = NULL;
13343 location_t loc = 0;
13344 cp_id_kind idk;
13345
13346 /* There's really no way to know what we're looking at, so we just
13347 try each alternative in order.
13348
13349 [temp.arg]
13350
13351 In a template-argument, an ambiguity between a type-id and an
13352 expression is resolved to a type-id, regardless of the form of
13353 the corresponding template-parameter.
13354
13355 Therefore, we try a type-id first. */
13356 cp_parser_parse_tentatively (parser);
13357 argument = cp_parser_template_type_arg (parser);
13358 /* If there was no error parsing the type-id but the next token is a
13359 '>>', our behavior depends on which dialect of C++ we're
13360 parsing. In C++98, we probably found a typo for '> >'. But there
13361 are type-id which are also valid expressions. For instance:
13362
13363 struct X { int operator >> (int); };
13364 template <int V> struct Foo {};
13365 Foo<X () >> 5> r;
13366
13367 Here 'X()' is a valid type-id of a function type, but the user just
13368 wanted to write the expression "X() >> 5". Thus, we remember that we
13369 found a valid type-id, but we still try to parse the argument as an
13370 expression to see what happens.
13371
13372 In C++0x, the '>>' will be considered two separate '>'
13373 tokens. */
13374 if (!cp_parser_error_occurred (parser)
13375 && cxx_dialect == cxx98
13376 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13377 {
13378 maybe_type_id = true;
13379 cp_parser_abort_tentative_parse (parser);
13380 }
13381 else
13382 {
13383 /* If the next token isn't a `,' or a `>', then this argument wasn't
13384 really finished. This means that the argument is not a valid
13385 type-id. */
13386 if (!cp_parser_next_token_ends_template_argument_p (parser))
13387 cp_parser_error (parser, "expected template-argument");
13388 /* If that worked, we're done. */
13389 if (cp_parser_parse_definitely (parser))
13390 return argument;
13391 }
13392 /* We're still not sure what the argument will be. */
13393 cp_parser_parse_tentatively (parser);
13394 /* Try a template. */
13395 argument_start_token = cp_lexer_peek_token (parser->lexer);
13396 argument = cp_parser_id_expression (parser,
13397 /*template_keyword_p=*/false,
13398 /*check_dependency_p=*/true,
13399 &template_p,
13400 /*declarator_p=*/false,
13401 /*optional_p=*/false);
13402 /* If the next token isn't a `,' or a `>', then this argument wasn't
13403 really finished. */
13404 if (!cp_parser_next_token_ends_template_argument_p (parser))
13405 cp_parser_error (parser, "expected template-argument");
13406 if (!cp_parser_error_occurred (parser))
13407 {
13408 /* Figure out what is being referred to. If the id-expression
13409 was for a class template specialization, then we will have a
13410 TYPE_DECL at this point. There is no need to do name lookup
13411 at this point in that case. */
13412 if (TREE_CODE (argument) != TYPE_DECL)
13413 argument = cp_parser_lookup_name (parser, argument,
13414 none_type,
13415 /*is_template=*/template_p,
13416 /*is_namespace=*/false,
13417 /*check_dependency=*/true,
13418 /*ambiguous_decls=*/NULL,
13419 argument_start_token->location);
13420 if (TREE_CODE (argument) != TEMPLATE_DECL
13421 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13422 cp_parser_error (parser, "expected template-name");
13423 }
13424 if (cp_parser_parse_definitely (parser))
13425 return argument;
13426 /* It must be a non-type argument. There permitted cases are given
13427 in [temp.arg.nontype]:
13428
13429 -- an integral constant-expression of integral or enumeration
13430 type; or
13431
13432 -- the name of a non-type template-parameter; or
13433
13434 -- the name of an object or function with external linkage...
13435
13436 -- the address of an object or function with external linkage...
13437
13438 -- a pointer to member... */
13439 /* Look for a non-type template parameter. */
13440 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13441 {
13442 cp_parser_parse_tentatively (parser);
13443 argument = cp_parser_primary_expression (parser,
13444 /*address_p=*/false,
13445 /*cast_p=*/false,
13446 /*template_arg_p=*/true,
13447 &idk);
13448 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13449 || !cp_parser_next_token_ends_template_argument_p (parser))
13450 cp_parser_simulate_error (parser);
13451 if (cp_parser_parse_definitely (parser))
13452 return argument;
13453 }
13454
13455 /* If the next token is "&", the argument must be the address of an
13456 object or function with external linkage. */
13457 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13458 if (address_p)
13459 {
13460 loc = cp_lexer_peek_token (parser->lexer)->location;
13461 cp_lexer_consume_token (parser->lexer);
13462 }
13463 /* See if we might have an id-expression. */
13464 token = cp_lexer_peek_token (parser->lexer);
13465 if (token->type == CPP_NAME
13466 || token->keyword == RID_OPERATOR
13467 || token->type == CPP_SCOPE
13468 || token->type == CPP_TEMPLATE_ID
13469 || token->type == CPP_NESTED_NAME_SPECIFIER)
13470 {
13471 cp_parser_parse_tentatively (parser);
13472 argument = cp_parser_primary_expression (parser,
13473 address_p,
13474 /*cast_p=*/false,
13475 /*template_arg_p=*/true,
13476 &idk);
13477 if (cp_parser_error_occurred (parser)
13478 || !cp_parser_next_token_ends_template_argument_p (parser))
13479 cp_parser_abort_tentative_parse (parser);
13480 else
13481 {
13482 tree probe;
13483
13484 if (INDIRECT_REF_P (argument))
13485 {
13486 gcc_assert (REFERENCE_REF_P (argument));
13487 argument = TREE_OPERAND (argument, 0);
13488 }
13489
13490 /* If we're in a template, we represent a qualified-id referring
13491 to a static data member as a SCOPE_REF even if the scope isn't
13492 dependent so that we can check access control later. */
13493 probe = argument;
13494 if (TREE_CODE (probe) == SCOPE_REF)
13495 probe = TREE_OPERAND (probe, 1);
13496 if (VAR_P (probe))
13497 {
13498 /* A variable without external linkage might still be a
13499 valid constant-expression, so no error is issued here
13500 if the external-linkage check fails. */
13501 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13502 cp_parser_simulate_error (parser);
13503 }
13504 else if (is_overloaded_fn (argument))
13505 /* All overloaded functions are allowed; if the external
13506 linkage test does not pass, an error will be issued
13507 later. */
13508 ;
13509 else if (address_p
13510 && (TREE_CODE (argument) == OFFSET_REF
13511 || TREE_CODE (argument) == SCOPE_REF))
13512 /* A pointer-to-member. */
13513 ;
13514 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13515 ;
13516 else
13517 cp_parser_simulate_error (parser);
13518
13519 if (cp_parser_parse_definitely (parser))
13520 {
13521 if (address_p)
13522 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13523 tf_warning_or_error);
13524 return argument;
13525 }
13526 }
13527 }
13528 /* If the argument started with "&", there are no other valid
13529 alternatives at this point. */
13530 if (address_p)
13531 {
13532 cp_parser_error (parser, "invalid non-type template argument");
13533 return error_mark_node;
13534 }
13535
13536 /* If the argument wasn't successfully parsed as a type-id followed
13537 by '>>', the argument can only be a constant expression now.
13538 Otherwise, we try parsing the constant-expression tentatively,
13539 because the argument could really be a type-id. */
13540 if (maybe_type_id)
13541 cp_parser_parse_tentatively (parser);
13542 argument = cp_parser_constant_expression (parser,
13543 /*allow_non_constant_p=*/false,
13544 /*non_constant_p=*/NULL);
13545 if (!maybe_type_id)
13546 return argument;
13547 if (!cp_parser_next_token_ends_template_argument_p (parser))
13548 cp_parser_error (parser, "expected template-argument");
13549 if (cp_parser_parse_definitely (parser))
13550 return argument;
13551 /* We did our best to parse the argument as a non type-id, but that
13552 was the only alternative that matched (albeit with a '>' after
13553 it). We can assume it's just a typo from the user, and a
13554 diagnostic will then be issued. */
13555 return cp_parser_template_type_arg (parser);
13556 }
13557
13558 /* Parse an explicit-instantiation.
13559
13560 explicit-instantiation:
13561 template declaration
13562
13563 Although the standard says `declaration', what it really means is:
13564
13565 explicit-instantiation:
13566 template decl-specifier-seq [opt] declarator [opt] ;
13567
13568 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13569 supposed to be allowed. A defect report has been filed about this
13570 issue.
13571
13572 GNU Extension:
13573
13574 explicit-instantiation:
13575 storage-class-specifier template
13576 decl-specifier-seq [opt] declarator [opt] ;
13577 function-specifier template
13578 decl-specifier-seq [opt] declarator [opt] ; */
13579
13580 static void
13581 cp_parser_explicit_instantiation (cp_parser* parser)
13582 {
13583 int declares_class_or_enum;
13584 cp_decl_specifier_seq decl_specifiers;
13585 tree extension_specifier = NULL_TREE;
13586
13587 timevar_push (TV_TEMPLATE_INST);
13588
13589 /* Look for an (optional) storage-class-specifier or
13590 function-specifier. */
13591 if (cp_parser_allow_gnu_extensions_p (parser))
13592 {
13593 extension_specifier
13594 = cp_parser_storage_class_specifier_opt (parser);
13595 if (!extension_specifier)
13596 extension_specifier
13597 = cp_parser_function_specifier_opt (parser,
13598 /*decl_specs=*/NULL);
13599 }
13600
13601 /* Look for the `template' keyword. */
13602 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13603 /* Let the front end know that we are processing an explicit
13604 instantiation. */
13605 begin_explicit_instantiation ();
13606 /* [temp.explicit] says that we are supposed to ignore access
13607 control while processing explicit instantiation directives. */
13608 push_deferring_access_checks (dk_no_check);
13609 /* Parse a decl-specifier-seq. */
13610 cp_parser_decl_specifier_seq (parser,
13611 CP_PARSER_FLAGS_OPTIONAL,
13612 &decl_specifiers,
13613 &declares_class_or_enum);
13614 /* If there was exactly one decl-specifier, and it declared a class,
13615 and there's no declarator, then we have an explicit type
13616 instantiation. */
13617 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13618 {
13619 tree type;
13620
13621 type = check_tag_decl (&decl_specifiers,
13622 /*explicit_type_instantiation_p=*/true);
13623 /* Turn access control back on for names used during
13624 template instantiation. */
13625 pop_deferring_access_checks ();
13626 if (type)
13627 do_type_instantiation (type, extension_specifier,
13628 /*complain=*/tf_error);
13629 }
13630 else
13631 {
13632 cp_declarator *declarator;
13633 tree decl;
13634
13635 /* Parse the declarator. */
13636 declarator
13637 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13638 /*ctor_dtor_or_conv_p=*/NULL,
13639 /*parenthesized_p=*/NULL,
13640 /*member_p=*/false);
13641 if (declares_class_or_enum & 2)
13642 cp_parser_check_for_definition_in_return_type (declarator,
13643 decl_specifiers.type,
13644 decl_specifiers.locations[ds_type_spec]);
13645 if (declarator != cp_error_declarator)
13646 {
13647 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13648 permerror (decl_specifiers.locations[ds_inline],
13649 "explicit instantiation shall not use"
13650 " %<inline%> specifier");
13651 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13652 permerror (decl_specifiers.locations[ds_constexpr],
13653 "explicit instantiation shall not use"
13654 " %<constexpr%> specifier");
13655
13656 decl = grokdeclarator (declarator, &decl_specifiers,
13657 NORMAL, 0, &decl_specifiers.attributes);
13658 /* Turn access control back on for names used during
13659 template instantiation. */
13660 pop_deferring_access_checks ();
13661 /* Do the explicit instantiation. */
13662 do_decl_instantiation (decl, extension_specifier);
13663 }
13664 else
13665 {
13666 pop_deferring_access_checks ();
13667 /* Skip the body of the explicit instantiation. */
13668 cp_parser_skip_to_end_of_statement (parser);
13669 }
13670 }
13671 /* We're done with the instantiation. */
13672 end_explicit_instantiation ();
13673
13674 cp_parser_consume_semicolon_at_end_of_statement (parser);
13675
13676 timevar_pop (TV_TEMPLATE_INST);
13677 }
13678
13679 /* Parse an explicit-specialization.
13680
13681 explicit-specialization:
13682 template < > declaration
13683
13684 Although the standard says `declaration', what it really means is:
13685
13686 explicit-specialization:
13687 template <> decl-specifier [opt] init-declarator [opt] ;
13688 template <> function-definition
13689 template <> explicit-specialization
13690 template <> template-declaration */
13691
13692 static void
13693 cp_parser_explicit_specialization (cp_parser* parser)
13694 {
13695 bool need_lang_pop;
13696 cp_token *token = cp_lexer_peek_token (parser->lexer);
13697
13698 /* Look for the `template' keyword. */
13699 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13700 /* Look for the `<'. */
13701 cp_parser_require (parser, CPP_LESS, RT_LESS);
13702 /* Look for the `>'. */
13703 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13704 /* We have processed another parameter list. */
13705 ++parser->num_template_parameter_lists;
13706 /* [temp]
13707
13708 A template ... explicit specialization ... shall not have C
13709 linkage. */
13710 if (current_lang_name == lang_name_c)
13711 {
13712 error_at (token->location, "template specialization with C linkage");
13713 /* Give it C++ linkage to avoid confusing other parts of the
13714 front end. */
13715 push_lang_context (lang_name_cplusplus);
13716 need_lang_pop = true;
13717 }
13718 else
13719 need_lang_pop = false;
13720 /* Let the front end know that we are beginning a specialization. */
13721 if (!begin_specialization ())
13722 {
13723 end_specialization ();
13724 return;
13725 }
13726
13727 /* If the next keyword is `template', we need to figure out whether
13728 or not we're looking a template-declaration. */
13729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13730 {
13731 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13732 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13733 cp_parser_template_declaration_after_export (parser,
13734 /*member_p=*/false);
13735 else
13736 cp_parser_explicit_specialization (parser);
13737 }
13738 else
13739 /* Parse the dependent declaration. */
13740 cp_parser_single_declaration (parser,
13741 /*checks=*/NULL,
13742 /*member_p=*/false,
13743 /*explicit_specialization_p=*/true,
13744 /*friend_p=*/NULL);
13745 /* We're done with the specialization. */
13746 end_specialization ();
13747 /* For the erroneous case of a template with C linkage, we pushed an
13748 implicit C++ linkage scope; exit that scope now. */
13749 if (need_lang_pop)
13750 pop_lang_context ();
13751 /* We're done with this parameter list. */
13752 --parser->num_template_parameter_lists;
13753 }
13754
13755 /* Parse a type-specifier.
13756
13757 type-specifier:
13758 simple-type-specifier
13759 class-specifier
13760 enum-specifier
13761 elaborated-type-specifier
13762 cv-qualifier
13763
13764 GNU Extension:
13765
13766 type-specifier:
13767 __complex__
13768
13769 Returns a representation of the type-specifier. For a
13770 class-specifier, enum-specifier, or elaborated-type-specifier, a
13771 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13772
13773 The parser flags FLAGS is used to control type-specifier parsing.
13774
13775 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13776 in a decl-specifier-seq.
13777
13778 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13779 class-specifier, enum-specifier, or elaborated-type-specifier, then
13780 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13781 if a type is declared; 2 if it is defined. Otherwise, it is set to
13782 zero.
13783
13784 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13785 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13786 is set to FALSE. */
13787
13788 static tree
13789 cp_parser_type_specifier (cp_parser* parser,
13790 cp_parser_flags flags,
13791 cp_decl_specifier_seq *decl_specs,
13792 bool is_declaration,
13793 int* declares_class_or_enum,
13794 bool* is_cv_qualifier)
13795 {
13796 tree type_spec = NULL_TREE;
13797 cp_token *token;
13798 enum rid keyword;
13799 cp_decl_spec ds = ds_last;
13800
13801 /* Assume this type-specifier does not declare a new type. */
13802 if (declares_class_or_enum)
13803 *declares_class_or_enum = 0;
13804 /* And that it does not specify a cv-qualifier. */
13805 if (is_cv_qualifier)
13806 *is_cv_qualifier = false;
13807 /* Peek at the next token. */
13808 token = cp_lexer_peek_token (parser->lexer);
13809
13810 /* If we're looking at a keyword, we can use that to guide the
13811 production we choose. */
13812 keyword = token->keyword;
13813 switch (keyword)
13814 {
13815 case RID_ENUM:
13816 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13817 goto elaborated_type_specifier;
13818
13819 /* Look for the enum-specifier. */
13820 type_spec = cp_parser_enum_specifier (parser);
13821 /* If that worked, we're done. */
13822 if (type_spec)
13823 {
13824 if (declares_class_or_enum)
13825 *declares_class_or_enum = 2;
13826 if (decl_specs)
13827 cp_parser_set_decl_spec_type (decl_specs,
13828 type_spec,
13829 token,
13830 /*type_definition_p=*/true);
13831 return type_spec;
13832 }
13833 else
13834 goto elaborated_type_specifier;
13835
13836 /* Any of these indicate either a class-specifier, or an
13837 elaborated-type-specifier. */
13838 case RID_CLASS:
13839 case RID_STRUCT:
13840 case RID_UNION:
13841 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13842 goto elaborated_type_specifier;
13843
13844 /* Parse tentatively so that we can back up if we don't find a
13845 class-specifier. */
13846 cp_parser_parse_tentatively (parser);
13847 /* Look for the class-specifier. */
13848 type_spec = cp_parser_class_specifier (parser);
13849 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13850 /* If that worked, we're done. */
13851 if (cp_parser_parse_definitely (parser))
13852 {
13853 if (declares_class_or_enum)
13854 *declares_class_or_enum = 2;
13855 if (decl_specs)
13856 cp_parser_set_decl_spec_type (decl_specs,
13857 type_spec,
13858 token,
13859 /*type_definition_p=*/true);
13860 return type_spec;
13861 }
13862
13863 /* Fall through. */
13864 elaborated_type_specifier:
13865 /* We're declaring (not defining) a class or enum. */
13866 if (declares_class_or_enum)
13867 *declares_class_or_enum = 1;
13868
13869 /* Fall through. */
13870 case RID_TYPENAME:
13871 /* Look for an elaborated-type-specifier. */
13872 type_spec
13873 = (cp_parser_elaborated_type_specifier
13874 (parser,
13875 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13876 is_declaration));
13877 if (decl_specs)
13878 cp_parser_set_decl_spec_type (decl_specs,
13879 type_spec,
13880 token,
13881 /*type_definition_p=*/false);
13882 return type_spec;
13883
13884 case RID_CONST:
13885 ds = ds_const;
13886 if (is_cv_qualifier)
13887 *is_cv_qualifier = true;
13888 break;
13889
13890 case RID_VOLATILE:
13891 ds = ds_volatile;
13892 if (is_cv_qualifier)
13893 *is_cv_qualifier = true;
13894 break;
13895
13896 case RID_RESTRICT:
13897 ds = ds_restrict;
13898 if (is_cv_qualifier)
13899 *is_cv_qualifier = true;
13900 break;
13901
13902 case RID_COMPLEX:
13903 /* The `__complex__' keyword is a GNU extension. */
13904 ds = ds_complex;
13905 break;
13906
13907 default:
13908 break;
13909 }
13910
13911 /* Handle simple keywords. */
13912 if (ds != ds_last)
13913 {
13914 if (decl_specs)
13915 {
13916 set_and_check_decl_spec_loc (decl_specs, ds, token);
13917 decl_specs->any_specifiers_p = true;
13918 }
13919 return cp_lexer_consume_token (parser->lexer)->u.value;
13920 }
13921
13922 /* If we do not already have a type-specifier, assume we are looking
13923 at a simple-type-specifier. */
13924 type_spec = cp_parser_simple_type_specifier (parser,
13925 decl_specs,
13926 flags);
13927
13928 /* If we didn't find a type-specifier, and a type-specifier was not
13929 optional in this context, issue an error message. */
13930 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13931 {
13932 cp_parser_error (parser, "expected type specifier");
13933 return error_mark_node;
13934 }
13935
13936 return type_spec;
13937 }
13938
13939 /* Parse a simple-type-specifier.
13940
13941 simple-type-specifier:
13942 :: [opt] nested-name-specifier [opt] type-name
13943 :: [opt] nested-name-specifier template template-id
13944 char
13945 wchar_t
13946 bool
13947 short
13948 int
13949 long
13950 signed
13951 unsigned
13952 float
13953 double
13954 void
13955
13956 C++0x Extension:
13957
13958 simple-type-specifier:
13959 auto
13960 decltype ( expression )
13961 char16_t
13962 char32_t
13963 __underlying_type ( type-id )
13964
13965 GNU Extension:
13966
13967 simple-type-specifier:
13968 __int128
13969 __typeof__ unary-expression
13970 __typeof__ ( type-id )
13971
13972 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13973 appropriately updated. */
13974
13975 static tree
13976 cp_parser_simple_type_specifier (cp_parser* parser,
13977 cp_decl_specifier_seq *decl_specs,
13978 cp_parser_flags flags)
13979 {
13980 tree type = NULL_TREE;
13981 cp_token *token;
13982
13983 /* Peek at the next token. */
13984 token = cp_lexer_peek_token (parser->lexer);
13985
13986 /* If we're looking at a keyword, things are easy. */
13987 switch (token->keyword)
13988 {
13989 case RID_CHAR:
13990 if (decl_specs)
13991 decl_specs->explicit_char_p = true;
13992 type = char_type_node;
13993 break;
13994 case RID_CHAR16:
13995 type = char16_type_node;
13996 break;
13997 case RID_CHAR32:
13998 type = char32_type_node;
13999 break;
14000 case RID_WCHAR:
14001 type = wchar_type_node;
14002 break;
14003 case RID_BOOL:
14004 type = boolean_type_node;
14005 break;
14006 case RID_SHORT:
14007 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14008 type = short_integer_type_node;
14009 break;
14010 case RID_INT:
14011 if (decl_specs)
14012 decl_specs->explicit_int_p = true;
14013 type = integer_type_node;
14014 break;
14015 case RID_INT128:
14016 if (!int128_integer_type_node)
14017 break;
14018 if (decl_specs)
14019 decl_specs->explicit_int128_p = true;
14020 type = int128_integer_type_node;
14021 break;
14022 case RID_LONG:
14023 if (decl_specs)
14024 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14025 type = long_integer_type_node;
14026 break;
14027 case RID_SIGNED:
14028 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14029 type = integer_type_node;
14030 break;
14031 case RID_UNSIGNED:
14032 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14033 type = unsigned_type_node;
14034 break;
14035 case RID_FLOAT:
14036 type = float_type_node;
14037 break;
14038 case RID_DOUBLE:
14039 type = double_type_node;
14040 break;
14041 case RID_VOID:
14042 type = void_type_node;
14043 break;
14044
14045 case RID_AUTO:
14046 maybe_warn_cpp0x (CPP0X_AUTO);
14047 type = make_auto ();
14048 break;
14049
14050 case RID_DECLTYPE:
14051 /* Since DR 743, decltype can either be a simple-type-specifier by
14052 itself or begin a nested-name-specifier. Parsing it will replace
14053 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14054 handling below decide what to do. */
14055 cp_parser_decltype (parser);
14056 cp_lexer_set_token_position (parser->lexer, token);
14057 break;
14058
14059 case RID_TYPEOF:
14060 /* Consume the `typeof' token. */
14061 cp_lexer_consume_token (parser->lexer);
14062 /* Parse the operand to `typeof'. */
14063 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14064 /* If it is not already a TYPE, take its type. */
14065 if (!TYPE_P (type))
14066 type = finish_typeof (type);
14067
14068 if (decl_specs)
14069 cp_parser_set_decl_spec_type (decl_specs, type,
14070 token,
14071 /*type_definition_p=*/false);
14072
14073 return type;
14074
14075 case RID_UNDERLYING_TYPE:
14076 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14077 if (decl_specs)
14078 cp_parser_set_decl_spec_type (decl_specs, type,
14079 token,
14080 /*type_definition_p=*/false);
14081
14082 return type;
14083
14084 case RID_BASES:
14085 case RID_DIRECT_BASES:
14086 type = cp_parser_trait_expr (parser, token->keyword);
14087 if (decl_specs)
14088 cp_parser_set_decl_spec_type (decl_specs, type,
14089 token,
14090 /*type_definition_p=*/false);
14091 return type;
14092 default:
14093 break;
14094 }
14095
14096 /* If token is an already-parsed decltype not followed by ::,
14097 it's a simple-type-specifier. */
14098 if (token->type == CPP_DECLTYPE
14099 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14100 {
14101 type = token->u.value;
14102 if (decl_specs)
14103 cp_parser_set_decl_spec_type (decl_specs, type,
14104 token,
14105 /*type_definition_p=*/false);
14106 cp_lexer_consume_token (parser->lexer);
14107 return type;
14108 }
14109
14110 /* If the type-specifier was for a built-in type, we're done. */
14111 if (type)
14112 {
14113 /* Record the type. */
14114 if (decl_specs
14115 && (token->keyword != RID_SIGNED
14116 && token->keyword != RID_UNSIGNED
14117 && token->keyword != RID_SHORT
14118 && token->keyword != RID_LONG))
14119 cp_parser_set_decl_spec_type (decl_specs,
14120 type,
14121 token,
14122 /*type_definition_p=*/false);
14123 if (decl_specs)
14124 decl_specs->any_specifiers_p = true;
14125
14126 /* Consume the token. */
14127 cp_lexer_consume_token (parser->lexer);
14128
14129 /* There is no valid C++ program where a non-template type is
14130 followed by a "<". That usually indicates that the user thought
14131 that the type was a template. */
14132 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14133 token->location);
14134
14135 return TYPE_NAME (type);
14136 }
14137
14138 /* The type-specifier must be a user-defined type. */
14139 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14140 {
14141 bool qualified_p;
14142 bool global_p;
14143
14144 /* Don't gobble tokens or issue error messages if this is an
14145 optional type-specifier. */
14146 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14147 cp_parser_parse_tentatively (parser);
14148
14149 /* Look for the optional `::' operator. */
14150 global_p
14151 = (cp_parser_global_scope_opt (parser,
14152 /*current_scope_valid_p=*/false)
14153 != NULL_TREE);
14154 /* Look for the nested-name specifier. */
14155 qualified_p
14156 = (cp_parser_nested_name_specifier_opt (parser,
14157 /*typename_keyword_p=*/false,
14158 /*check_dependency_p=*/true,
14159 /*type_p=*/false,
14160 /*is_declaration=*/false)
14161 != NULL_TREE);
14162 token = cp_lexer_peek_token (parser->lexer);
14163 /* If we have seen a nested-name-specifier, and the next token
14164 is `template', then we are using the template-id production. */
14165 if (parser->scope
14166 && cp_parser_optional_template_keyword (parser))
14167 {
14168 /* Look for the template-id. */
14169 type = cp_parser_template_id (parser,
14170 /*template_keyword_p=*/true,
14171 /*check_dependency_p=*/true,
14172 none_type,
14173 /*is_declaration=*/false);
14174 /* If the template-id did not name a type, we are out of
14175 luck. */
14176 if (TREE_CODE (type) != TYPE_DECL)
14177 {
14178 cp_parser_error (parser, "expected template-id for type");
14179 type = NULL_TREE;
14180 }
14181 }
14182 /* Otherwise, look for a type-name. */
14183 else
14184 type = cp_parser_type_name (parser);
14185 /* Keep track of all name-lookups performed in class scopes. */
14186 if (type
14187 && !global_p
14188 && !qualified_p
14189 && TREE_CODE (type) == TYPE_DECL
14190 && identifier_p (DECL_NAME (type)))
14191 maybe_note_name_used_in_class (DECL_NAME (type), type);
14192 /* If it didn't work out, we don't have a TYPE. */
14193 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14194 && !cp_parser_parse_definitely (parser))
14195 type = NULL_TREE;
14196 if (type && decl_specs)
14197 cp_parser_set_decl_spec_type (decl_specs, type,
14198 token,
14199 /*type_definition_p=*/false);
14200 }
14201
14202 /* If we didn't get a type-name, issue an error message. */
14203 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14204 {
14205 cp_parser_error (parser, "expected type-name");
14206 return error_mark_node;
14207 }
14208
14209 if (type && type != error_mark_node)
14210 {
14211 /* See if TYPE is an Objective-C type, and if so, parse and
14212 accept any protocol references following it. Do this before
14213 the cp_parser_check_for_invalid_template_id() call, because
14214 Objective-C types can be followed by '<...>' which would
14215 enclose protocol names rather than template arguments, and so
14216 everything is fine. */
14217 if (c_dialect_objc () && !parser->scope
14218 && (objc_is_id (type) || objc_is_class_name (type)))
14219 {
14220 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14221 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14222
14223 /* Clobber the "unqualified" type previously entered into
14224 DECL_SPECS with the new, improved protocol-qualified version. */
14225 if (decl_specs)
14226 decl_specs->type = qual_type;
14227
14228 return qual_type;
14229 }
14230
14231 /* There is no valid C++ program where a non-template type is
14232 followed by a "<". That usually indicates that the user
14233 thought that the type was a template. */
14234 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14235 none_type,
14236 token->location);
14237 }
14238
14239 return type;
14240 }
14241
14242 /* Parse a type-name.
14243
14244 type-name:
14245 class-name
14246 enum-name
14247 typedef-name
14248 simple-template-id [in c++0x]
14249
14250 enum-name:
14251 identifier
14252
14253 typedef-name:
14254 identifier
14255
14256 Returns a TYPE_DECL for the type. */
14257
14258 static tree
14259 cp_parser_type_name (cp_parser* parser)
14260 {
14261 tree type_decl;
14262
14263 /* We can't know yet whether it is a class-name or not. */
14264 cp_parser_parse_tentatively (parser);
14265 /* Try a class-name. */
14266 type_decl = cp_parser_class_name (parser,
14267 /*typename_keyword_p=*/false,
14268 /*template_keyword_p=*/false,
14269 none_type,
14270 /*check_dependency_p=*/true,
14271 /*class_head_p=*/false,
14272 /*is_declaration=*/false);
14273 /* If it's not a class-name, keep looking. */
14274 if (!cp_parser_parse_definitely (parser))
14275 {
14276 if (cxx_dialect < cxx0x)
14277 /* It must be a typedef-name or an enum-name. */
14278 return cp_parser_nonclass_name (parser);
14279
14280 cp_parser_parse_tentatively (parser);
14281 /* It is either a simple-template-id representing an
14282 instantiation of an alias template... */
14283 type_decl = cp_parser_template_id (parser,
14284 /*template_keyword_p=*/false,
14285 /*check_dependency_p=*/false,
14286 none_type,
14287 /*is_declaration=*/false);
14288 /* Note that this must be an instantiation of an alias template
14289 because [temp.names]/6 says:
14290
14291 A template-id that names an alias template specialization
14292 is a type-name.
14293
14294 Whereas [temp.names]/7 says:
14295
14296 A simple-template-id that names a class template
14297 specialization is a class-name. */
14298 if (type_decl != NULL_TREE
14299 && TREE_CODE (type_decl) == TYPE_DECL
14300 && TYPE_DECL_ALIAS_P (type_decl))
14301 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14302 else
14303 cp_parser_simulate_error (parser);
14304
14305 if (!cp_parser_parse_definitely (parser))
14306 /* ... Or a typedef-name or an enum-name. */
14307 return cp_parser_nonclass_name (parser);
14308 }
14309
14310 return type_decl;
14311 }
14312
14313 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14314
14315 enum-name:
14316 identifier
14317
14318 typedef-name:
14319 identifier
14320
14321 Returns a TYPE_DECL for the type. */
14322
14323 static tree
14324 cp_parser_nonclass_name (cp_parser* parser)
14325 {
14326 tree type_decl;
14327 tree identifier;
14328
14329 cp_token *token = cp_lexer_peek_token (parser->lexer);
14330 identifier = cp_parser_identifier (parser);
14331 if (identifier == error_mark_node)
14332 return error_mark_node;
14333
14334 /* Look up the type-name. */
14335 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14336
14337 if (TREE_CODE (type_decl) == USING_DECL)
14338 {
14339 if (!DECL_DEPENDENT_P (type_decl))
14340 type_decl = strip_using_decl (type_decl);
14341 else if (USING_DECL_TYPENAME_P (type_decl))
14342 {
14343 /* We have found a type introduced by a using
14344 declaration at class scope that refers to a dependent
14345 type.
14346
14347 using typename :: [opt] nested-name-specifier unqualified-id ;
14348 */
14349 type_decl = make_typename_type (TREE_TYPE (type_decl),
14350 DECL_NAME (type_decl),
14351 typename_type, tf_error);
14352 if (type_decl != error_mark_node)
14353 type_decl = TYPE_NAME (type_decl);
14354 }
14355 }
14356
14357 if (TREE_CODE (type_decl) != TYPE_DECL
14358 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14359 {
14360 /* See if this is an Objective-C type. */
14361 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14362 tree type = objc_get_protocol_qualified_type (identifier, protos);
14363 if (type)
14364 type_decl = TYPE_NAME (type);
14365 }
14366
14367 /* Issue an error if we did not find a type-name. */
14368 if (TREE_CODE (type_decl) != TYPE_DECL
14369 /* In Objective-C, we have the complication that class names are
14370 normally type names and start declarations (eg, the
14371 "NSObject" in "NSObject *object;"), but can be used in an
14372 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14373 is an expression. So, a classname followed by a dot is not a
14374 valid type-name. */
14375 || (objc_is_class_name (TREE_TYPE (type_decl))
14376 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14377 {
14378 if (!cp_parser_simulate_error (parser))
14379 cp_parser_name_lookup_error (parser, identifier, type_decl,
14380 NLE_TYPE, token->location);
14381 return error_mark_node;
14382 }
14383 /* Remember that the name was used in the definition of the
14384 current class so that we can check later to see if the
14385 meaning would have been different after the class was
14386 entirely defined. */
14387 else if (type_decl != error_mark_node
14388 && !parser->scope)
14389 maybe_note_name_used_in_class (identifier, type_decl);
14390
14391 return type_decl;
14392 }
14393
14394 /* Parse an elaborated-type-specifier. Note that the grammar given
14395 here incorporates the resolution to DR68.
14396
14397 elaborated-type-specifier:
14398 class-key :: [opt] nested-name-specifier [opt] identifier
14399 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14400 enum-key :: [opt] nested-name-specifier [opt] identifier
14401 typename :: [opt] nested-name-specifier identifier
14402 typename :: [opt] nested-name-specifier template [opt]
14403 template-id
14404
14405 GNU extension:
14406
14407 elaborated-type-specifier:
14408 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14409 class-key attributes :: [opt] nested-name-specifier [opt]
14410 template [opt] template-id
14411 enum attributes :: [opt] nested-name-specifier [opt] identifier
14412
14413 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14414 declared `friend'. If IS_DECLARATION is TRUE, then this
14415 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14416 something is being declared.
14417
14418 Returns the TYPE specified. */
14419
14420 static tree
14421 cp_parser_elaborated_type_specifier (cp_parser* parser,
14422 bool is_friend,
14423 bool is_declaration)
14424 {
14425 enum tag_types tag_type;
14426 tree identifier;
14427 tree type = NULL_TREE;
14428 tree attributes = NULL_TREE;
14429 tree globalscope;
14430 cp_token *token = NULL;
14431
14432 /* See if we're looking at the `enum' keyword. */
14433 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14434 {
14435 /* Consume the `enum' token. */
14436 cp_lexer_consume_token (parser->lexer);
14437 /* Remember that it's an enumeration type. */
14438 tag_type = enum_type;
14439 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14440 enums) is used here. */
14441 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14442 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14443 {
14444 pedwarn (input_location, 0, "elaborated-type-specifier "
14445 "for a scoped enum must not use the %<%D%> keyword",
14446 cp_lexer_peek_token (parser->lexer)->u.value);
14447 /* Consume the `struct' or `class' and parse it anyway. */
14448 cp_lexer_consume_token (parser->lexer);
14449 }
14450 /* Parse the attributes. */
14451 attributes = cp_parser_attributes_opt (parser);
14452 }
14453 /* Or, it might be `typename'. */
14454 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14455 RID_TYPENAME))
14456 {
14457 /* Consume the `typename' token. */
14458 cp_lexer_consume_token (parser->lexer);
14459 /* Remember that it's a `typename' type. */
14460 tag_type = typename_type;
14461 }
14462 /* Otherwise it must be a class-key. */
14463 else
14464 {
14465 tag_type = cp_parser_class_key (parser);
14466 if (tag_type == none_type)
14467 return error_mark_node;
14468 /* Parse the attributes. */
14469 attributes = cp_parser_attributes_opt (parser);
14470 }
14471
14472 /* Look for the `::' operator. */
14473 globalscope = cp_parser_global_scope_opt (parser,
14474 /*current_scope_valid_p=*/false);
14475 /* Look for the nested-name-specifier. */
14476 if (tag_type == typename_type && !globalscope)
14477 {
14478 if (!cp_parser_nested_name_specifier (parser,
14479 /*typename_keyword_p=*/true,
14480 /*check_dependency_p=*/true,
14481 /*type_p=*/true,
14482 is_declaration))
14483 return error_mark_node;
14484 }
14485 else
14486 /* Even though `typename' is not present, the proposed resolution
14487 to Core Issue 180 says that in `class A<T>::B', `B' should be
14488 considered a type-name, even if `A<T>' is dependent. */
14489 cp_parser_nested_name_specifier_opt (parser,
14490 /*typename_keyword_p=*/true,
14491 /*check_dependency_p=*/true,
14492 /*type_p=*/true,
14493 is_declaration);
14494 /* For everything but enumeration types, consider a template-id.
14495 For an enumeration type, consider only a plain identifier. */
14496 if (tag_type != enum_type)
14497 {
14498 bool template_p = false;
14499 tree decl;
14500
14501 /* Allow the `template' keyword. */
14502 template_p = cp_parser_optional_template_keyword (parser);
14503 /* If we didn't see `template', we don't know if there's a
14504 template-id or not. */
14505 if (!template_p)
14506 cp_parser_parse_tentatively (parser);
14507 /* Parse the template-id. */
14508 token = cp_lexer_peek_token (parser->lexer);
14509 decl = cp_parser_template_id (parser, template_p,
14510 /*check_dependency_p=*/true,
14511 tag_type,
14512 is_declaration);
14513 /* If we didn't find a template-id, look for an ordinary
14514 identifier. */
14515 if (!template_p && !cp_parser_parse_definitely (parser))
14516 ;
14517 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14518 in effect, then we must assume that, upon instantiation, the
14519 template will correspond to a class. */
14520 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14521 && tag_type == typename_type)
14522 type = make_typename_type (parser->scope, decl,
14523 typename_type,
14524 /*complain=*/tf_error);
14525 /* If the `typename' keyword is in effect and DECL is not a type
14526 decl, then type is non existent. */
14527 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14528 ;
14529 else if (TREE_CODE (decl) == TYPE_DECL)
14530 type = check_elaborated_type_specifier (tag_type, decl,
14531 /*allow_template_p=*/true);
14532 else if (decl == error_mark_node)
14533 type = error_mark_node;
14534 }
14535
14536 if (!type)
14537 {
14538 token = cp_lexer_peek_token (parser->lexer);
14539 identifier = cp_parser_identifier (parser);
14540
14541 if (identifier == error_mark_node)
14542 {
14543 parser->scope = NULL_TREE;
14544 return error_mark_node;
14545 }
14546
14547 /* For a `typename', we needn't call xref_tag. */
14548 if (tag_type == typename_type
14549 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14550 return cp_parser_make_typename_type (parser, parser->scope,
14551 identifier,
14552 token->location);
14553 /* Look up a qualified name in the usual way. */
14554 if (parser->scope)
14555 {
14556 tree decl;
14557 tree ambiguous_decls;
14558
14559 decl = cp_parser_lookup_name (parser, identifier,
14560 tag_type,
14561 /*is_template=*/false,
14562 /*is_namespace=*/false,
14563 /*check_dependency=*/true,
14564 &ambiguous_decls,
14565 token->location);
14566
14567 /* If the lookup was ambiguous, an error will already have been
14568 issued. */
14569 if (ambiguous_decls)
14570 return error_mark_node;
14571
14572 /* If we are parsing friend declaration, DECL may be a
14573 TEMPLATE_DECL tree node here. However, we need to check
14574 whether this TEMPLATE_DECL results in valid code. Consider
14575 the following example:
14576
14577 namespace N {
14578 template <class T> class C {};
14579 }
14580 class X {
14581 template <class T> friend class N::C; // #1, valid code
14582 };
14583 template <class T> class Y {
14584 friend class N::C; // #2, invalid code
14585 };
14586
14587 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14588 name lookup of `N::C'. We see that friend declaration must
14589 be template for the code to be valid. Note that
14590 processing_template_decl does not work here since it is
14591 always 1 for the above two cases. */
14592
14593 decl = (cp_parser_maybe_treat_template_as_class
14594 (decl, /*tag_name_p=*/is_friend
14595 && parser->num_template_parameter_lists));
14596
14597 if (TREE_CODE (decl) != TYPE_DECL)
14598 {
14599 cp_parser_diagnose_invalid_type_name (parser,
14600 parser->scope,
14601 identifier,
14602 token->location);
14603 return error_mark_node;
14604 }
14605
14606 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14607 {
14608 bool allow_template = (parser->num_template_parameter_lists
14609 || DECL_SELF_REFERENCE_P (decl));
14610 type = check_elaborated_type_specifier (tag_type, decl,
14611 allow_template);
14612
14613 if (type == error_mark_node)
14614 return error_mark_node;
14615 }
14616
14617 /* Forward declarations of nested types, such as
14618
14619 class C1::C2;
14620 class C1::C2::C3;
14621
14622 are invalid unless all components preceding the final '::'
14623 are complete. If all enclosing types are complete, these
14624 declarations become merely pointless.
14625
14626 Invalid forward declarations of nested types are errors
14627 caught elsewhere in parsing. Those that are pointless arrive
14628 here. */
14629
14630 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14631 && !is_friend && !processing_explicit_instantiation)
14632 warning (0, "declaration %qD does not declare anything", decl);
14633
14634 type = TREE_TYPE (decl);
14635 }
14636 else
14637 {
14638 /* An elaborated-type-specifier sometimes introduces a new type and
14639 sometimes names an existing type. Normally, the rule is that it
14640 introduces a new type only if there is not an existing type of
14641 the same name already in scope. For example, given:
14642
14643 struct S {};
14644 void f() { struct S s; }
14645
14646 the `struct S' in the body of `f' is the same `struct S' as in
14647 the global scope; the existing definition is used. However, if
14648 there were no global declaration, this would introduce a new
14649 local class named `S'.
14650
14651 An exception to this rule applies to the following code:
14652
14653 namespace N { struct S; }
14654
14655 Here, the elaborated-type-specifier names a new type
14656 unconditionally; even if there is already an `S' in the
14657 containing scope this declaration names a new type.
14658 This exception only applies if the elaborated-type-specifier
14659 forms the complete declaration:
14660
14661 [class.name]
14662
14663 A declaration consisting solely of `class-key identifier ;' is
14664 either a redeclaration of the name in the current scope or a
14665 forward declaration of the identifier as a class name. It
14666 introduces the name into the current scope.
14667
14668 We are in this situation precisely when the next token is a `;'.
14669
14670 An exception to the exception is that a `friend' declaration does
14671 *not* name a new type; i.e., given:
14672
14673 struct S { friend struct T; };
14674
14675 `T' is not a new type in the scope of `S'.
14676
14677 Also, `new struct S' or `sizeof (struct S)' never results in the
14678 definition of a new type; a new type can only be declared in a
14679 declaration context. */
14680
14681 tag_scope ts;
14682 bool template_p;
14683
14684 if (is_friend)
14685 /* Friends have special name lookup rules. */
14686 ts = ts_within_enclosing_non_class;
14687 else if (is_declaration
14688 && cp_lexer_next_token_is (parser->lexer,
14689 CPP_SEMICOLON))
14690 /* This is a `class-key identifier ;' */
14691 ts = ts_current;
14692 else
14693 ts = ts_global;
14694
14695 template_p =
14696 (parser->num_template_parameter_lists
14697 && (cp_parser_next_token_starts_class_definition_p (parser)
14698 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14699 /* An unqualified name was used to reference this type, so
14700 there were no qualifying templates. */
14701 if (!cp_parser_check_template_parameters (parser,
14702 /*num_templates=*/0,
14703 token->location,
14704 /*declarator=*/NULL))
14705 return error_mark_node;
14706 type = xref_tag (tag_type, identifier, ts, template_p);
14707 }
14708 }
14709
14710 if (type == error_mark_node)
14711 return error_mark_node;
14712
14713 /* Allow attributes on forward declarations of classes. */
14714 if (attributes)
14715 {
14716 if (TREE_CODE (type) == TYPENAME_TYPE)
14717 warning (OPT_Wattributes,
14718 "attributes ignored on uninstantiated type");
14719 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14720 && ! processing_explicit_instantiation)
14721 warning (OPT_Wattributes,
14722 "attributes ignored on template instantiation");
14723 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14724 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14725 else
14726 warning (OPT_Wattributes,
14727 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14728 }
14729
14730 if (tag_type != enum_type)
14731 {
14732 /* Indicate whether this class was declared as a `class' or as a
14733 `struct'. */
14734 if (TREE_CODE (type) == RECORD_TYPE)
14735 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14736 cp_parser_check_class_key (tag_type, type);
14737 }
14738
14739 /* A "<" cannot follow an elaborated type specifier. If that
14740 happens, the user was probably trying to form a template-id. */
14741 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14742 token->location);
14743
14744 return type;
14745 }
14746
14747 /* Parse an enum-specifier.
14748
14749 enum-specifier:
14750 enum-head { enumerator-list [opt] }
14751 enum-head { enumerator-list , } [C++0x]
14752
14753 enum-head:
14754 enum-key identifier [opt] enum-base [opt]
14755 enum-key nested-name-specifier identifier enum-base [opt]
14756
14757 enum-key:
14758 enum
14759 enum class [C++0x]
14760 enum struct [C++0x]
14761
14762 enum-base: [C++0x]
14763 : type-specifier-seq
14764
14765 opaque-enum-specifier:
14766 enum-key identifier enum-base [opt] ;
14767
14768 GNU Extensions:
14769 enum-key attributes[opt] identifier [opt] enum-base [opt]
14770 { enumerator-list [opt] }attributes[opt]
14771 enum-key attributes[opt] identifier [opt] enum-base [opt]
14772 { enumerator-list, }attributes[opt] [C++0x]
14773
14774 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14775 if the token stream isn't an enum-specifier after all. */
14776
14777 static tree
14778 cp_parser_enum_specifier (cp_parser* parser)
14779 {
14780 tree identifier;
14781 tree type = NULL_TREE;
14782 tree prev_scope;
14783 tree nested_name_specifier = NULL_TREE;
14784 tree attributes;
14785 bool scoped_enum_p = false;
14786 bool has_underlying_type = false;
14787 bool nested_being_defined = false;
14788 bool new_value_list = false;
14789 bool is_new_type = false;
14790 bool is_anonymous = false;
14791 tree underlying_type = NULL_TREE;
14792 cp_token *type_start_token = NULL;
14793 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14794
14795 parser->colon_corrects_to_scope_p = false;
14796
14797 /* Parse tentatively so that we can back up if we don't find a
14798 enum-specifier. */
14799 cp_parser_parse_tentatively (parser);
14800
14801 /* Caller guarantees that the current token is 'enum', an identifier
14802 possibly follows, and the token after that is an opening brace.
14803 If we don't have an identifier, fabricate an anonymous name for
14804 the enumeration being defined. */
14805 cp_lexer_consume_token (parser->lexer);
14806
14807 /* Parse the "class" or "struct", which indicates a scoped
14808 enumeration type in C++0x. */
14809 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14810 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14811 {
14812 if (cxx_dialect < cxx0x)
14813 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14814
14815 /* Consume the `struct' or `class' token. */
14816 cp_lexer_consume_token (parser->lexer);
14817
14818 scoped_enum_p = true;
14819 }
14820
14821 attributes = cp_parser_attributes_opt (parser);
14822
14823 /* Clear the qualification. */
14824 parser->scope = NULL_TREE;
14825 parser->qualifying_scope = NULL_TREE;
14826 parser->object_scope = NULL_TREE;
14827
14828 /* Figure out in what scope the declaration is being placed. */
14829 prev_scope = current_scope ();
14830
14831 type_start_token = cp_lexer_peek_token (parser->lexer);
14832
14833 push_deferring_access_checks (dk_no_check);
14834 nested_name_specifier
14835 = cp_parser_nested_name_specifier_opt (parser,
14836 /*typename_keyword_p=*/true,
14837 /*check_dependency_p=*/false,
14838 /*type_p=*/false,
14839 /*is_declaration=*/false);
14840
14841 if (nested_name_specifier)
14842 {
14843 tree name;
14844
14845 identifier = cp_parser_identifier (parser);
14846 name = cp_parser_lookup_name (parser, identifier,
14847 enum_type,
14848 /*is_template=*/false,
14849 /*is_namespace=*/false,
14850 /*check_dependency=*/true,
14851 /*ambiguous_decls=*/NULL,
14852 input_location);
14853 if (name && name != error_mark_node)
14854 {
14855 type = TREE_TYPE (name);
14856 if (TREE_CODE (type) == TYPENAME_TYPE)
14857 {
14858 /* Are template enums allowed in ISO? */
14859 if (template_parm_scope_p ())
14860 pedwarn (type_start_token->location, OPT_Wpedantic,
14861 "%qD is an enumeration template", name);
14862 /* ignore a typename reference, for it will be solved by name
14863 in start_enum. */
14864 type = NULL_TREE;
14865 }
14866 }
14867 else if (nested_name_specifier == error_mark_node)
14868 /* We already issued an error. */;
14869 else
14870 error_at (type_start_token->location,
14871 "%qD is not an enumerator-name", identifier);
14872 }
14873 else
14874 {
14875 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14876 identifier = cp_parser_identifier (parser);
14877 else
14878 {
14879 identifier = make_anon_name ();
14880 is_anonymous = true;
14881 if (scoped_enum_p)
14882 error_at (type_start_token->location,
14883 "anonymous scoped enum is not allowed");
14884 }
14885 }
14886 pop_deferring_access_checks ();
14887
14888 /* Check for the `:' that denotes a specified underlying type in C++0x.
14889 Note that a ':' could also indicate a bitfield width, however. */
14890 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14891 {
14892 cp_decl_specifier_seq type_specifiers;
14893
14894 /* Consume the `:'. */
14895 cp_lexer_consume_token (parser->lexer);
14896
14897 /* Parse the type-specifier-seq. */
14898 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14899 /*is_trailing_return=*/false,
14900 &type_specifiers);
14901
14902 /* At this point this is surely not elaborated type specifier. */
14903 if (!cp_parser_parse_definitely (parser))
14904 return NULL_TREE;
14905
14906 if (cxx_dialect < cxx0x)
14907 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14908
14909 has_underlying_type = true;
14910
14911 /* If that didn't work, stop. */
14912 if (type_specifiers.type != error_mark_node)
14913 {
14914 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14915 /*initialized=*/0, NULL);
14916 if (underlying_type == error_mark_node)
14917 underlying_type = NULL_TREE;
14918 }
14919 }
14920
14921 /* Look for the `{' but don't consume it yet. */
14922 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14923 {
14924 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14925 {
14926 cp_parser_error (parser, "expected %<{%>");
14927 if (has_underlying_type)
14928 {
14929 type = NULL_TREE;
14930 goto out;
14931 }
14932 }
14933 /* An opaque-enum-specifier must have a ';' here. */
14934 if ((scoped_enum_p || underlying_type)
14935 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14936 {
14937 cp_parser_error (parser, "expected %<;%> or %<{%>");
14938 if (has_underlying_type)
14939 {
14940 type = NULL_TREE;
14941 goto out;
14942 }
14943 }
14944 }
14945
14946 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14947 return NULL_TREE;
14948
14949 if (nested_name_specifier)
14950 {
14951 if (CLASS_TYPE_P (nested_name_specifier))
14952 {
14953 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14954 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14955 push_scope (nested_name_specifier);
14956 }
14957 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14958 {
14959 push_nested_namespace (nested_name_specifier);
14960 }
14961 }
14962
14963 /* Issue an error message if type-definitions are forbidden here. */
14964 if (!cp_parser_check_type_definition (parser))
14965 type = error_mark_node;
14966 else
14967 /* Create the new type. We do this before consuming the opening
14968 brace so the enum will be recorded as being on the line of its
14969 tag (or the 'enum' keyword, if there is no tag). */
14970 type = start_enum (identifier, type, underlying_type,
14971 scoped_enum_p, &is_new_type);
14972
14973 /* If the next token is not '{' it is an opaque-enum-specifier or an
14974 elaborated-type-specifier. */
14975 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14976 {
14977 timevar_push (TV_PARSE_ENUM);
14978 if (nested_name_specifier
14979 && nested_name_specifier != error_mark_node)
14980 {
14981 /* The following catches invalid code such as:
14982 enum class S<int>::E { A, B, C }; */
14983 if (!processing_specialization
14984 && CLASS_TYPE_P (nested_name_specifier)
14985 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14986 error_at (type_start_token->location, "cannot add an enumerator "
14987 "list to a template instantiation");
14988
14989 /* If that scope does not contain the scope in which the
14990 class was originally declared, the program is invalid. */
14991 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14992 {
14993 if (at_namespace_scope_p ())
14994 error_at (type_start_token->location,
14995 "declaration of %qD in namespace %qD which does not "
14996 "enclose %qD",
14997 type, prev_scope, nested_name_specifier);
14998 else
14999 error_at (type_start_token->location,
15000 "declaration of %qD in %qD which does not enclose %qD",
15001 type, prev_scope, nested_name_specifier);
15002 type = error_mark_node;
15003 }
15004 }
15005
15006 if (scoped_enum_p)
15007 begin_scope (sk_scoped_enum, type);
15008
15009 /* Consume the opening brace. */
15010 cp_lexer_consume_token (parser->lexer);
15011
15012 if (type == error_mark_node)
15013 ; /* Nothing to add */
15014 else if (OPAQUE_ENUM_P (type)
15015 || (cxx_dialect > cxx98 && processing_specialization))
15016 {
15017 new_value_list = true;
15018 SET_OPAQUE_ENUM_P (type, false);
15019 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15020 }
15021 else
15022 {
15023 error_at (type_start_token->location, "multiple definition of %q#T", type);
15024 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15025 "previous definition here");
15026 type = error_mark_node;
15027 }
15028
15029 if (type == error_mark_node)
15030 cp_parser_skip_to_end_of_block_or_statement (parser);
15031 /* If the next token is not '}', then there are some enumerators. */
15032 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15033 {
15034 if (is_anonymous && !scoped_enum_p)
15035 pedwarn (type_start_token->location, OPT_Wpedantic,
15036 "ISO C++ forbids empty anonymous enum");
15037 }
15038 else
15039 cp_parser_enumerator_list (parser, type);
15040
15041 /* Consume the final '}'. */
15042 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15043
15044 if (scoped_enum_p)
15045 finish_scope ();
15046 timevar_pop (TV_PARSE_ENUM);
15047 }
15048 else
15049 {
15050 /* If a ';' follows, then it is an opaque-enum-specifier
15051 and additional restrictions apply. */
15052 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15053 {
15054 if (is_anonymous)
15055 error_at (type_start_token->location,
15056 "opaque-enum-specifier without name");
15057 else if (nested_name_specifier)
15058 error_at (type_start_token->location,
15059 "opaque-enum-specifier must use a simple identifier");
15060 }
15061 }
15062
15063 /* Look for trailing attributes to apply to this enumeration, and
15064 apply them if appropriate. */
15065 if (cp_parser_allow_gnu_extensions_p (parser))
15066 {
15067 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15068 trailing_attr = chainon (trailing_attr, attributes);
15069 cplus_decl_attributes (&type,
15070 trailing_attr,
15071 (int) ATTR_FLAG_TYPE_IN_PLACE);
15072 }
15073
15074 /* Finish up the enumeration. */
15075 if (type != error_mark_node)
15076 {
15077 if (new_value_list)
15078 finish_enum_value_list (type);
15079 if (is_new_type)
15080 finish_enum (type);
15081 }
15082
15083 if (nested_name_specifier)
15084 {
15085 if (CLASS_TYPE_P (nested_name_specifier))
15086 {
15087 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15088 pop_scope (nested_name_specifier);
15089 }
15090 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15091 {
15092 pop_nested_namespace (nested_name_specifier);
15093 }
15094 }
15095 out:
15096 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15097 return type;
15098 }
15099
15100 /* Parse an enumerator-list. The enumerators all have the indicated
15101 TYPE.
15102
15103 enumerator-list:
15104 enumerator-definition
15105 enumerator-list , enumerator-definition */
15106
15107 static void
15108 cp_parser_enumerator_list (cp_parser* parser, tree type)
15109 {
15110 while (true)
15111 {
15112 /* Parse an enumerator-definition. */
15113 cp_parser_enumerator_definition (parser, type);
15114
15115 /* If the next token is not a ',', we've reached the end of
15116 the list. */
15117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15118 break;
15119 /* Otherwise, consume the `,' and keep going. */
15120 cp_lexer_consume_token (parser->lexer);
15121 /* If the next token is a `}', there is a trailing comma. */
15122 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15123 {
15124 if (cxx_dialect < cxx0x && !in_system_header)
15125 pedwarn (input_location, OPT_Wpedantic,
15126 "comma at end of enumerator list");
15127 break;
15128 }
15129 }
15130 }
15131
15132 /* Parse an enumerator-definition. The enumerator has the indicated
15133 TYPE.
15134
15135 enumerator-definition:
15136 enumerator
15137 enumerator = constant-expression
15138
15139 enumerator:
15140 identifier */
15141
15142 static void
15143 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15144 {
15145 tree identifier;
15146 tree value;
15147 location_t loc;
15148
15149 /* Save the input location because we are interested in the location
15150 of the identifier and not the location of the explicit value. */
15151 loc = cp_lexer_peek_token (parser->lexer)->location;
15152
15153 /* Look for the identifier. */
15154 identifier = cp_parser_identifier (parser);
15155 if (identifier == error_mark_node)
15156 return;
15157
15158 /* If the next token is an '=', then there is an explicit value. */
15159 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15160 {
15161 /* Consume the `=' token. */
15162 cp_lexer_consume_token (parser->lexer);
15163 /* Parse the value. */
15164 value = cp_parser_constant_expression (parser,
15165 /*allow_non_constant_p=*/false,
15166 NULL);
15167 }
15168 else
15169 value = NULL_TREE;
15170
15171 /* If we are processing a template, make sure the initializer of the
15172 enumerator doesn't contain any bare template parameter pack. */
15173 if (check_for_bare_parameter_packs (value))
15174 value = error_mark_node;
15175
15176 /* integral_constant_value will pull out this expression, so make sure
15177 it's folded as appropriate. */
15178 value = fold_non_dependent_expr (value);
15179
15180 /* Create the enumerator. */
15181 build_enumerator (identifier, value, type, loc);
15182 }
15183
15184 /* Parse a namespace-name.
15185
15186 namespace-name:
15187 original-namespace-name
15188 namespace-alias
15189
15190 Returns the NAMESPACE_DECL for the namespace. */
15191
15192 static tree
15193 cp_parser_namespace_name (cp_parser* parser)
15194 {
15195 tree identifier;
15196 tree namespace_decl;
15197
15198 cp_token *token = cp_lexer_peek_token (parser->lexer);
15199
15200 /* Get the name of the namespace. */
15201 identifier = cp_parser_identifier (parser);
15202 if (identifier == error_mark_node)
15203 return error_mark_node;
15204
15205 /* Look up the identifier in the currently active scope. Look only
15206 for namespaces, due to:
15207
15208 [basic.lookup.udir]
15209
15210 When looking up a namespace-name in a using-directive or alias
15211 definition, only namespace names are considered.
15212
15213 And:
15214
15215 [basic.lookup.qual]
15216
15217 During the lookup of a name preceding the :: scope resolution
15218 operator, object, function, and enumerator names are ignored.
15219
15220 (Note that cp_parser_qualifying_entity only calls this
15221 function if the token after the name is the scope resolution
15222 operator.) */
15223 namespace_decl = cp_parser_lookup_name (parser, identifier,
15224 none_type,
15225 /*is_template=*/false,
15226 /*is_namespace=*/true,
15227 /*check_dependency=*/true,
15228 /*ambiguous_decls=*/NULL,
15229 token->location);
15230 /* If it's not a namespace, issue an error. */
15231 if (namespace_decl == error_mark_node
15232 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15233 {
15234 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15235 error_at (token->location, "%qD is not a namespace-name", identifier);
15236 cp_parser_error (parser, "expected namespace-name");
15237 namespace_decl = error_mark_node;
15238 }
15239
15240 return namespace_decl;
15241 }
15242
15243 /* Parse a namespace-definition.
15244
15245 namespace-definition:
15246 named-namespace-definition
15247 unnamed-namespace-definition
15248
15249 named-namespace-definition:
15250 original-namespace-definition
15251 extension-namespace-definition
15252
15253 original-namespace-definition:
15254 namespace identifier { namespace-body }
15255
15256 extension-namespace-definition:
15257 namespace original-namespace-name { namespace-body }
15258
15259 unnamed-namespace-definition:
15260 namespace { namespace-body } */
15261
15262 static void
15263 cp_parser_namespace_definition (cp_parser* parser)
15264 {
15265 tree identifier, attribs;
15266 bool has_visibility;
15267 bool is_inline;
15268
15269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15270 {
15271 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15272 is_inline = true;
15273 cp_lexer_consume_token (parser->lexer);
15274 }
15275 else
15276 is_inline = false;
15277
15278 /* Look for the `namespace' keyword. */
15279 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15280
15281 /* Get the name of the namespace. We do not attempt to distinguish
15282 between an original-namespace-definition and an
15283 extension-namespace-definition at this point. The semantic
15284 analysis routines are responsible for that. */
15285 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15286 identifier = cp_parser_identifier (parser);
15287 else
15288 identifier = NULL_TREE;
15289
15290 /* Parse any specified attributes. */
15291 attribs = cp_parser_attributes_opt (parser);
15292
15293 /* Look for the `{' to start the namespace. */
15294 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15295 /* Start the namespace. */
15296 push_namespace (identifier);
15297
15298 /* "inline namespace" is equivalent to a stub namespace definition
15299 followed by a strong using directive. */
15300 if (is_inline)
15301 {
15302 tree name_space = current_namespace;
15303 /* Set up namespace association. */
15304 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15305 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15306 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15307 /* Import the contents of the inline namespace. */
15308 pop_namespace ();
15309 do_using_directive (name_space);
15310 push_namespace (identifier);
15311 }
15312
15313 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15314
15315 /* Parse the body of the namespace. */
15316 cp_parser_namespace_body (parser);
15317
15318 if (has_visibility)
15319 pop_visibility (1);
15320
15321 /* Finish the namespace. */
15322 pop_namespace ();
15323 /* Look for the final `}'. */
15324 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15325 }
15326
15327 /* Parse a namespace-body.
15328
15329 namespace-body:
15330 declaration-seq [opt] */
15331
15332 static void
15333 cp_parser_namespace_body (cp_parser* parser)
15334 {
15335 cp_parser_declaration_seq_opt (parser);
15336 }
15337
15338 /* Parse a namespace-alias-definition.
15339
15340 namespace-alias-definition:
15341 namespace identifier = qualified-namespace-specifier ; */
15342
15343 static void
15344 cp_parser_namespace_alias_definition (cp_parser* parser)
15345 {
15346 tree identifier;
15347 tree namespace_specifier;
15348
15349 cp_token *token = cp_lexer_peek_token (parser->lexer);
15350
15351 /* Look for the `namespace' keyword. */
15352 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15353 /* Look for the identifier. */
15354 identifier = cp_parser_identifier (parser);
15355 if (identifier == error_mark_node)
15356 return;
15357 /* Look for the `=' token. */
15358 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15359 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15360 {
15361 error_at (token->location, "%<namespace%> definition is not allowed here");
15362 /* Skip the definition. */
15363 cp_lexer_consume_token (parser->lexer);
15364 if (cp_parser_skip_to_closing_brace (parser))
15365 cp_lexer_consume_token (parser->lexer);
15366 return;
15367 }
15368 cp_parser_require (parser, CPP_EQ, RT_EQ);
15369 /* Look for the qualified-namespace-specifier. */
15370 namespace_specifier
15371 = cp_parser_qualified_namespace_specifier (parser);
15372 /* Look for the `;' token. */
15373 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15374
15375 /* Register the alias in the symbol table. */
15376 do_namespace_alias (identifier, namespace_specifier);
15377 }
15378
15379 /* Parse a qualified-namespace-specifier.
15380
15381 qualified-namespace-specifier:
15382 :: [opt] nested-name-specifier [opt] namespace-name
15383
15384 Returns a NAMESPACE_DECL corresponding to the specified
15385 namespace. */
15386
15387 static tree
15388 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15389 {
15390 /* Look for the optional `::'. */
15391 cp_parser_global_scope_opt (parser,
15392 /*current_scope_valid_p=*/false);
15393
15394 /* Look for the optional nested-name-specifier. */
15395 cp_parser_nested_name_specifier_opt (parser,
15396 /*typename_keyword_p=*/false,
15397 /*check_dependency_p=*/true,
15398 /*type_p=*/false,
15399 /*is_declaration=*/true);
15400
15401 return cp_parser_namespace_name (parser);
15402 }
15403
15404 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15405 access declaration.
15406
15407 using-declaration:
15408 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15409 using :: unqualified-id ;
15410
15411 access-declaration:
15412 qualified-id ;
15413
15414 */
15415
15416 static bool
15417 cp_parser_using_declaration (cp_parser* parser,
15418 bool access_declaration_p)
15419 {
15420 cp_token *token;
15421 bool typename_p = false;
15422 bool global_scope_p;
15423 tree decl;
15424 tree identifier;
15425 tree qscope;
15426 int oldcount = errorcount;
15427 cp_token *diag_token = NULL;
15428
15429 if (access_declaration_p)
15430 {
15431 diag_token = cp_lexer_peek_token (parser->lexer);
15432 cp_parser_parse_tentatively (parser);
15433 }
15434 else
15435 {
15436 /* Look for the `using' keyword. */
15437 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15438
15439 /* Peek at the next token. */
15440 token = cp_lexer_peek_token (parser->lexer);
15441 /* See if it's `typename'. */
15442 if (token->keyword == RID_TYPENAME)
15443 {
15444 /* Remember that we've seen it. */
15445 typename_p = true;
15446 /* Consume the `typename' token. */
15447 cp_lexer_consume_token (parser->lexer);
15448 }
15449 }
15450
15451 /* Look for the optional global scope qualification. */
15452 global_scope_p
15453 = (cp_parser_global_scope_opt (parser,
15454 /*current_scope_valid_p=*/false)
15455 != NULL_TREE);
15456
15457 /* If we saw `typename', or didn't see `::', then there must be a
15458 nested-name-specifier present. */
15459 if (typename_p || !global_scope_p)
15460 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15461 /*check_dependency_p=*/true,
15462 /*type_p=*/false,
15463 /*is_declaration=*/true);
15464 /* Otherwise, we could be in either of the two productions. In that
15465 case, treat the nested-name-specifier as optional. */
15466 else
15467 qscope = cp_parser_nested_name_specifier_opt (parser,
15468 /*typename_keyword_p=*/false,
15469 /*check_dependency_p=*/true,
15470 /*type_p=*/false,
15471 /*is_declaration=*/true);
15472 if (!qscope)
15473 qscope = global_namespace;
15474
15475 if (access_declaration_p && cp_parser_error_occurred (parser))
15476 /* Something has already gone wrong; there's no need to parse
15477 further. Since an error has occurred, the return value of
15478 cp_parser_parse_definitely will be false, as required. */
15479 return cp_parser_parse_definitely (parser);
15480
15481 token = cp_lexer_peek_token (parser->lexer);
15482 /* Parse the unqualified-id. */
15483 identifier = cp_parser_unqualified_id (parser,
15484 /*template_keyword_p=*/false,
15485 /*check_dependency_p=*/true,
15486 /*declarator_p=*/true,
15487 /*optional_p=*/false);
15488
15489 if (access_declaration_p)
15490 {
15491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15492 cp_parser_simulate_error (parser);
15493 if (!cp_parser_parse_definitely (parser))
15494 return false;
15495 }
15496
15497 /* The function we call to handle a using-declaration is different
15498 depending on what scope we are in. */
15499 if (qscope == error_mark_node || identifier == error_mark_node)
15500 ;
15501 else if (!identifier_p (identifier)
15502 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15503 /* [namespace.udecl]
15504
15505 A using declaration shall not name a template-id. */
15506 error_at (token->location,
15507 "a template-id may not appear in a using-declaration");
15508 else
15509 {
15510 if (at_class_scope_p ())
15511 {
15512 /* Create the USING_DECL. */
15513 decl = do_class_using_decl (parser->scope, identifier);
15514
15515 if (decl && typename_p)
15516 USING_DECL_TYPENAME_P (decl) = 1;
15517
15518 if (check_for_bare_parameter_packs (decl))
15519 return false;
15520 else
15521 /* Add it to the list of members in this class. */
15522 finish_member_declaration (decl);
15523 }
15524 else
15525 {
15526 decl = cp_parser_lookup_name_simple (parser,
15527 identifier,
15528 token->location);
15529 if (decl == error_mark_node)
15530 cp_parser_name_lookup_error (parser, identifier,
15531 decl, NLE_NULL,
15532 token->location);
15533 else if (check_for_bare_parameter_packs (decl))
15534 return false;
15535 else if (!at_namespace_scope_p ())
15536 do_local_using_decl (decl, qscope, identifier);
15537 else
15538 do_toplevel_using_decl (decl, qscope, identifier);
15539 }
15540 }
15541
15542 /* Look for the final `;'. */
15543 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15544
15545 if (access_declaration_p && errorcount == oldcount)
15546 warning_at (diag_token->location, OPT_Wdeprecated,
15547 "access declarations are deprecated "
15548 "in favour of using-declarations; "
15549 "suggestion: add the %<using%> keyword");
15550
15551 return true;
15552 }
15553
15554 /* Parse an alias-declaration.
15555
15556 alias-declaration:
15557 using identifier attribute-specifier-seq [opt] = type-id */
15558
15559 static tree
15560 cp_parser_alias_declaration (cp_parser* parser)
15561 {
15562 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15563 location_t id_location;
15564 cp_declarator *declarator;
15565 cp_decl_specifier_seq decl_specs;
15566 bool member_p;
15567 const char *saved_message = NULL;
15568
15569 /* Look for the `using' keyword. */
15570 cp_token *using_token
15571 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15572 if (using_token == NULL)
15573 return error_mark_node;
15574
15575 id_location = cp_lexer_peek_token (parser->lexer)->location;
15576 id = cp_parser_identifier (parser);
15577 if (id == error_mark_node)
15578 return error_mark_node;
15579
15580 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15581 attributes = cp_parser_attributes_opt (parser);
15582 if (attributes == error_mark_node)
15583 return error_mark_node;
15584
15585 cp_parser_require (parser, CPP_EQ, RT_EQ);
15586
15587 if (cp_parser_error_occurred (parser))
15588 return error_mark_node;
15589
15590 cp_parser_commit_to_tentative_parse (parser);
15591
15592 /* Now we are going to parse the type-id of the declaration. */
15593
15594 /*
15595 [dcl.type]/3 says:
15596
15597 "A type-specifier-seq shall not define a class or enumeration
15598 unless it appears in the type-id of an alias-declaration (7.1.3) that
15599 is not the declaration of a template-declaration."
15600
15601 In other words, if we currently are in an alias template, the
15602 type-id should not define a type.
15603
15604 So let's set parser->type_definition_forbidden_message in that
15605 case; cp_parser_check_type_definition (called by
15606 cp_parser_class_specifier) will then emit an error if a type is
15607 defined in the type-id. */
15608 if (parser->num_template_parameter_lists)
15609 {
15610 saved_message = parser->type_definition_forbidden_message;
15611 parser->type_definition_forbidden_message =
15612 G_("types may not be defined in alias template declarations");
15613 }
15614
15615 type = cp_parser_type_id (parser);
15616
15617 /* Restore the error message if need be. */
15618 if (parser->num_template_parameter_lists)
15619 parser->type_definition_forbidden_message = saved_message;
15620
15621 if (type == error_mark_node)
15622 {
15623 cp_parser_skip_to_end_of_block_or_statement (parser);
15624 return error_mark_node;
15625 }
15626
15627 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15628
15629 if (cp_parser_error_occurred (parser))
15630 {
15631 cp_parser_skip_to_end_of_block_or_statement (parser);
15632 return error_mark_node;
15633 }
15634
15635 /* A typedef-name can also be introduced by an alias-declaration. The
15636 identifier following the using keyword becomes a typedef-name. It has
15637 the same semantics as if it were introduced by the typedef
15638 specifier. In particular, it does not define a new type and it shall
15639 not appear in the type-id. */
15640
15641 clear_decl_specs (&decl_specs);
15642 decl_specs.type = type;
15643 if (attributes != NULL_TREE)
15644 {
15645 decl_specs.attributes = attributes;
15646 set_and_check_decl_spec_loc (&decl_specs,
15647 ds_attribute,
15648 attrs_token);
15649 }
15650 set_and_check_decl_spec_loc (&decl_specs,
15651 ds_typedef,
15652 using_token);
15653 set_and_check_decl_spec_loc (&decl_specs,
15654 ds_alias,
15655 using_token);
15656
15657 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15658 declarator->id_loc = id_location;
15659
15660 member_p = at_class_scope_p ();
15661 if (member_p)
15662 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15663 NULL_TREE, attributes);
15664 else
15665 decl = start_decl (declarator, &decl_specs, 0,
15666 attributes, NULL_TREE, &pushed_scope);
15667 if (decl == error_mark_node)
15668 return decl;
15669
15670 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15671
15672 if (pushed_scope)
15673 pop_scope (pushed_scope);
15674
15675 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15676 added into the symbol table; otherwise, return the TYPE_DECL. */
15677 if (DECL_LANG_SPECIFIC (decl)
15678 && DECL_TEMPLATE_INFO (decl)
15679 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15680 {
15681 decl = DECL_TI_TEMPLATE (decl);
15682 if (member_p)
15683 check_member_template (decl);
15684 }
15685
15686 return decl;
15687 }
15688
15689 /* Parse a using-directive.
15690
15691 using-directive:
15692 using namespace :: [opt] nested-name-specifier [opt]
15693 namespace-name ; */
15694
15695 static void
15696 cp_parser_using_directive (cp_parser* parser)
15697 {
15698 tree namespace_decl;
15699 tree attribs;
15700
15701 /* Look for the `using' keyword. */
15702 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15703 /* And the `namespace' keyword. */
15704 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15705 /* Look for the optional `::' operator. */
15706 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15707 /* And the optional nested-name-specifier. */
15708 cp_parser_nested_name_specifier_opt (parser,
15709 /*typename_keyword_p=*/false,
15710 /*check_dependency_p=*/true,
15711 /*type_p=*/false,
15712 /*is_declaration=*/true);
15713 /* Get the namespace being used. */
15714 namespace_decl = cp_parser_namespace_name (parser);
15715 /* And any specified attributes. */
15716 attribs = cp_parser_attributes_opt (parser);
15717 /* Update the symbol table. */
15718 parse_using_directive (namespace_decl, attribs);
15719 /* Look for the final `;'. */
15720 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15721 }
15722
15723 /* Parse an asm-definition.
15724
15725 asm-definition:
15726 asm ( string-literal ) ;
15727
15728 GNU Extension:
15729
15730 asm-definition:
15731 asm volatile [opt] ( string-literal ) ;
15732 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15733 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15734 : asm-operand-list [opt] ) ;
15735 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15736 : asm-operand-list [opt]
15737 : asm-clobber-list [opt] ) ;
15738 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15739 : asm-clobber-list [opt]
15740 : asm-goto-list ) ; */
15741
15742 static void
15743 cp_parser_asm_definition (cp_parser* parser)
15744 {
15745 tree string;
15746 tree outputs = NULL_TREE;
15747 tree inputs = NULL_TREE;
15748 tree clobbers = NULL_TREE;
15749 tree labels = NULL_TREE;
15750 tree asm_stmt;
15751 bool volatile_p = false;
15752 bool extended_p = false;
15753 bool invalid_inputs_p = false;
15754 bool invalid_outputs_p = false;
15755 bool goto_p = false;
15756 required_token missing = RT_NONE;
15757
15758 /* Look for the `asm' keyword. */
15759 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15760 /* See if the next token is `volatile'. */
15761 if (cp_parser_allow_gnu_extensions_p (parser)
15762 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15763 {
15764 /* Remember that we saw the `volatile' keyword. */
15765 volatile_p = true;
15766 /* Consume the token. */
15767 cp_lexer_consume_token (parser->lexer);
15768 }
15769 if (cp_parser_allow_gnu_extensions_p (parser)
15770 && parser->in_function_body
15771 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15772 {
15773 /* Remember that we saw the `goto' keyword. */
15774 goto_p = true;
15775 /* Consume the token. */
15776 cp_lexer_consume_token (parser->lexer);
15777 }
15778 /* Look for the opening `('. */
15779 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15780 return;
15781 /* Look for the string. */
15782 string = cp_parser_string_literal (parser, false, false);
15783 if (string == error_mark_node)
15784 {
15785 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15786 /*consume_paren=*/true);
15787 return;
15788 }
15789
15790 /* If we're allowing GNU extensions, check for the extended assembly
15791 syntax. Unfortunately, the `:' tokens need not be separated by
15792 a space in C, and so, for compatibility, we tolerate that here
15793 too. Doing that means that we have to treat the `::' operator as
15794 two `:' tokens. */
15795 if (cp_parser_allow_gnu_extensions_p (parser)
15796 && parser->in_function_body
15797 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15798 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15799 {
15800 bool inputs_p = false;
15801 bool clobbers_p = false;
15802 bool labels_p = false;
15803
15804 /* The extended syntax was used. */
15805 extended_p = true;
15806
15807 /* Look for outputs. */
15808 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15809 {
15810 /* Consume the `:'. */
15811 cp_lexer_consume_token (parser->lexer);
15812 /* Parse the output-operands. */
15813 if (cp_lexer_next_token_is_not (parser->lexer,
15814 CPP_COLON)
15815 && cp_lexer_next_token_is_not (parser->lexer,
15816 CPP_SCOPE)
15817 && cp_lexer_next_token_is_not (parser->lexer,
15818 CPP_CLOSE_PAREN)
15819 && !goto_p)
15820 outputs = cp_parser_asm_operand_list (parser);
15821
15822 if (outputs == error_mark_node)
15823 invalid_outputs_p = true;
15824 }
15825 /* If the next token is `::', there are no outputs, and the
15826 next token is the beginning of the inputs. */
15827 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15828 /* The inputs are coming next. */
15829 inputs_p = true;
15830
15831 /* Look for inputs. */
15832 if (inputs_p
15833 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15834 {
15835 /* Consume the `:' or `::'. */
15836 cp_lexer_consume_token (parser->lexer);
15837 /* Parse the output-operands. */
15838 if (cp_lexer_next_token_is_not (parser->lexer,
15839 CPP_COLON)
15840 && cp_lexer_next_token_is_not (parser->lexer,
15841 CPP_SCOPE)
15842 && cp_lexer_next_token_is_not (parser->lexer,
15843 CPP_CLOSE_PAREN))
15844 inputs = cp_parser_asm_operand_list (parser);
15845
15846 if (inputs == error_mark_node)
15847 invalid_inputs_p = true;
15848 }
15849 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15850 /* The clobbers are coming next. */
15851 clobbers_p = true;
15852
15853 /* Look for clobbers. */
15854 if (clobbers_p
15855 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15856 {
15857 clobbers_p = true;
15858 /* Consume the `:' or `::'. */
15859 cp_lexer_consume_token (parser->lexer);
15860 /* Parse the clobbers. */
15861 if (cp_lexer_next_token_is_not (parser->lexer,
15862 CPP_COLON)
15863 && cp_lexer_next_token_is_not (parser->lexer,
15864 CPP_CLOSE_PAREN))
15865 clobbers = cp_parser_asm_clobber_list (parser);
15866 }
15867 else if (goto_p
15868 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15869 /* The labels are coming next. */
15870 labels_p = true;
15871
15872 /* Look for labels. */
15873 if (labels_p
15874 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15875 {
15876 labels_p = true;
15877 /* Consume the `:' or `::'. */
15878 cp_lexer_consume_token (parser->lexer);
15879 /* Parse the labels. */
15880 labels = cp_parser_asm_label_list (parser);
15881 }
15882
15883 if (goto_p && !labels_p)
15884 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15885 }
15886 else if (goto_p)
15887 missing = RT_COLON_SCOPE;
15888
15889 /* Look for the closing `)'. */
15890 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15891 missing ? missing : RT_CLOSE_PAREN))
15892 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15893 /*consume_paren=*/true);
15894 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15895
15896 if (!invalid_inputs_p && !invalid_outputs_p)
15897 {
15898 /* Create the ASM_EXPR. */
15899 if (parser->in_function_body)
15900 {
15901 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15902 inputs, clobbers, labels);
15903 /* If the extended syntax was not used, mark the ASM_EXPR. */
15904 if (!extended_p)
15905 {
15906 tree temp = asm_stmt;
15907 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15908 temp = TREE_OPERAND (temp, 0);
15909
15910 ASM_INPUT_P (temp) = 1;
15911 }
15912 }
15913 else
15914 add_asm_node (string);
15915 }
15916 }
15917
15918 /* Declarators [gram.dcl.decl] */
15919
15920 /* Parse an init-declarator.
15921
15922 init-declarator:
15923 declarator initializer [opt]
15924
15925 GNU Extension:
15926
15927 init-declarator:
15928 declarator asm-specification [opt] attributes [opt] initializer [opt]
15929
15930 function-definition:
15931 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15932 function-body
15933 decl-specifier-seq [opt] declarator function-try-block
15934
15935 GNU Extension:
15936
15937 function-definition:
15938 __extension__ function-definition
15939
15940 TM Extension:
15941
15942 function-definition:
15943 decl-specifier-seq [opt] declarator function-transaction-block
15944
15945 The DECL_SPECIFIERS apply to this declarator. Returns a
15946 representation of the entity declared. If MEMBER_P is TRUE, then
15947 this declarator appears in a class scope. The new DECL created by
15948 this declarator is returned.
15949
15950 The CHECKS are access checks that should be performed once we know
15951 what entity is being declared (and, therefore, what classes have
15952 befriended it).
15953
15954 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15955 for a function-definition here as well. If the declarator is a
15956 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15957 be TRUE upon return. By that point, the function-definition will
15958 have been completely parsed.
15959
15960 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15961 is FALSE.
15962
15963 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15964 parsed declaration if it is an uninitialized single declarator not followed
15965 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15966 if present, will not be consumed. If returned, this declarator will be
15967 created with SD_INITIALIZED but will not call cp_finish_decl. */
15968
15969 static tree
15970 cp_parser_init_declarator (cp_parser* parser,
15971 cp_decl_specifier_seq *decl_specifiers,
15972 vec<deferred_access_check, va_gc> *checks,
15973 bool function_definition_allowed_p,
15974 bool member_p,
15975 int declares_class_or_enum,
15976 bool* function_definition_p,
15977 tree* maybe_range_for_decl)
15978 {
15979 cp_token *token = NULL, *asm_spec_start_token = NULL,
15980 *attributes_start_token = NULL;
15981 cp_declarator *declarator;
15982 tree prefix_attributes;
15983 tree attributes = NULL;
15984 tree asm_specification;
15985 tree initializer;
15986 tree decl = NULL_TREE;
15987 tree scope;
15988 int is_initialized;
15989 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15990 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15991 "(...)". */
15992 enum cpp_ttype initialization_kind;
15993 bool is_direct_init = false;
15994 bool is_non_constant_init;
15995 int ctor_dtor_or_conv_p;
15996 bool friend_p;
15997 tree pushed_scope = NULL_TREE;
15998 bool range_for_decl_p = false;
15999
16000 /* Gather the attributes that were provided with the
16001 decl-specifiers. */
16002 prefix_attributes = decl_specifiers->attributes;
16003
16004 /* Assume that this is not the declarator for a function
16005 definition. */
16006 if (function_definition_p)
16007 *function_definition_p = false;
16008
16009 /* Defer access checks while parsing the declarator; we cannot know
16010 what names are accessible until we know what is being
16011 declared. */
16012 resume_deferring_access_checks ();
16013
16014 /* Parse the declarator. */
16015 token = cp_lexer_peek_token (parser->lexer);
16016 declarator
16017 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16018 &ctor_dtor_or_conv_p,
16019 /*parenthesized_p=*/NULL,
16020 member_p);
16021 /* Gather up the deferred checks. */
16022 stop_deferring_access_checks ();
16023
16024 /* If the DECLARATOR was erroneous, there's no need to go
16025 further. */
16026 if (declarator == cp_error_declarator)
16027 return error_mark_node;
16028
16029 /* Check that the number of template-parameter-lists is OK. */
16030 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16031 token->location))
16032 return error_mark_node;
16033
16034 if (declares_class_or_enum & 2)
16035 cp_parser_check_for_definition_in_return_type (declarator,
16036 decl_specifiers->type,
16037 decl_specifiers->locations[ds_type_spec]);
16038
16039 /* Figure out what scope the entity declared by the DECLARATOR is
16040 located in. `grokdeclarator' sometimes changes the scope, so
16041 we compute it now. */
16042 scope = get_scope_of_declarator (declarator);
16043
16044 /* Perform any lookups in the declared type which were thought to be
16045 dependent, but are not in the scope of the declarator. */
16046 decl_specifiers->type
16047 = maybe_update_decl_type (decl_specifiers->type, scope);
16048
16049 /* If we're allowing GNU extensions, look for an
16050 asm-specification. */
16051 if (cp_parser_allow_gnu_extensions_p (parser))
16052 {
16053 /* Look for an asm-specification. */
16054 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16055 asm_specification = cp_parser_asm_specification_opt (parser);
16056 }
16057 else
16058 asm_specification = NULL_TREE;
16059
16060 /* Look for attributes. */
16061 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16062 attributes = cp_parser_attributes_opt (parser);
16063
16064 /* Peek at the next token. */
16065 token = cp_lexer_peek_token (parser->lexer);
16066 /* Check to see if the token indicates the start of a
16067 function-definition. */
16068 if (function_declarator_p (declarator)
16069 && cp_parser_token_starts_function_definition_p (token))
16070 {
16071 if (!function_definition_allowed_p)
16072 {
16073 /* If a function-definition should not appear here, issue an
16074 error message. */
16075 cp_parser_error (parser,
16076 "a function-definition is not allowed here");
16077 return error_mark_node;
16078 }
16079 else
16080 {
16081 location_t func_brace_location
16082 = cp_lexer_peek_token (parser->lexer)->location;
16083
16084 /* Neither attributes nor an asm-specification are allowed
16085 on a function-definition. */
16086 if (asm_specification)
16087 error_at (asm_spec_start_token->location,
16088 "an asm-specification is not allowed "
16089 "on a function-definition");
16090 if (attributes)
16091 error_at (attributes_start_token->location,
16092 "attributes are not allowed on a function-definition");
16093 /* This is a function-definition. */
16094 *function_definition_p = true;
16095
16096 /* Parse the function definition. */
16097 if (member_p)
16098 decl = cp_parser_save_member_function_body (parser,
16099 decl_specifiers,
16100 declarator,
16101 prefix_attributes);
16102 else
16103 decl
16104 = (cp_parser_function_definition_from_specifiers_and_declarator
16105 (parser, decl_specifiers, prefix_attributes, declarator));
16106
16107 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16108 {
16109 /* This is where the prologue starts... */
16110 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16111 = func_brace_location;
16112 }
16113
16114 return decl;
16115 }
16116 }
16117
16118 /* [dcl.dcl]
16119
16120 Only in function declarations for constructors, destructors, and
16121 type conversions can the decl-specifier-seq be omitted.
16122
16123 We explicitly postpone this check past the point where we handle
16124 function-definitions because we tolerate function-definitions
16125 that are missing their return types in some modes. */
16126 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16127 {
16128 cp_parser_error (parser,
16129 "expected constructor, destructor, or type conversion");
16130 return error_mark_node;
16131 }
16132
16133 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16134 if (token->type == CPP_EQ
16135 || token->type == CPP_OPEN_PAREN
16136 || token->type == CPP_OPEN_BRACE)
16137 {
16138 is_initialized = SD_INITIALIZED;
16139 initialization_kind = token->type;
16140 if (maybe_range_for_decl)
16141 *maybe_range_for_decl = error_mark_node;
16142
16143 if (token->type == CPP_EQ
16144 && function_declarator_p (declarator))
16145 {
16146 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16147 if (t2->keyword == RID_DEFAULT)
16148 is_initialized = SD_DEFAULTED;
16149 else if (t2->keyword == RID_DELETE)
16150 is_initialized = SD_DELETED;
16151 }
16152 }
16153 else
16154 {
16155 /* If the init-declarator isn't initialized and isn't followed by a
16156 `,' or `;', it's not a valid init-declarator. */
16157 if (token->type != CPP_COMMA
16158 && token->type != CPP_SEMICOLON)
16159 {
16160 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16161 range_for_decl_p = true;
16162 else
16163 {
16164 cp_parser_error (parser, "expected initializer");
16165 return error_mark_node;
16166 }
16167 }
16168 is_initialized = SD_UNINITIALIZED;
16169 initialization_kind = CPP_EOF;
16170 }
16171
16172 /* Because start_decl has side-effects, we should only call it if we
16173 know we're going ahead. By this point, we know that we cannot
16174 possibly be looking at any other construct. */
16175 cp_parser_commit_to_tentative_parse (parser);
16176
16177 /* If the decl specifiers were bad, issue an error now that we're
16178 sure this was intended to be a declarator. Then continue
16179 declaring the variable(s), as int, to try to cut down on further
16180 errors. */
16181 if (decl_specifiers->any_specifiers_p
16182 && decl_specifiers->type == error_mark_node)
16183 {
16184 cp_parser_error (parser, "invalid type in declaration");
16185 decl_specifiers->type = integer_type_node;
16186 }
16187
16188 /* Check to see whether or not this declaration is a friend. */
16189 friend_p = cp_parser_friend_p (decl_specifiers);
16190
16191 /* Enter the newly declared entry in the symbol table. If we're
16192 processing a declaration in a class-specifier, we wait until
16193 after processing the initializer. */
16194 if (!member_p)
16195 {
16196 if (parser->in_unbraced_linkage_specification_p)
16197 decl_specifiers->storage_class = sc_extern;
16198 decl = start_decl (declarator, decl_specifiers,
16199 range_for_decl_p? SD_INITIALIZED : is_initialized,
16200 attributes, prefix_attributes,
16201 &pushed_scope);
16202 /* Adjust location of decl if declarator->id_loc is more appropriate:
16203 set, and decl wasn't merged with another decl, in which case its
16204 location would be different from input_location, and more accurate. */
16205 if (DECL_P (decl)
16206 && declarator->id_loc != UNKNOWN_LOCATION
16207 && DECL_SOURCE_LOCATION (decl) == input_location)
16208 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16209 }
16210 else if (scope)
16211 /* Enter the SCOPE. That way unqualified names appearing in the
16212 initializer will be looked up in SCOPE. */
16213 pushed_scope = push_scope (scope);
16214
16215 /* Perform deferred access control checks, now that we know in which
16216 SCOPE the declared entity resides. */
16217 if (!member_p && decl)
16218 {
16219 tree saved_current_function_decl = NULL_TREE;
16220
16221 /* If the entity being declared is a function, pretend that we
16222 are in its scope. If it is a `friend', it may have access to
16223 things that would not otherwise be accessible. */
16224 if (TREE_CODE (decl) == FUNCTION_DECL)
16225 {
16226 saved_current_function_decl = current_function_decl;
16227 current_function_decl = decl;
16228 }
16229
16230 /* Perform access checks for template parameters. */
16231 cp_parser_perform_template_parameter_access_checks (checks);
16232
16233 /* Perform the access control checks for the declarator and the
16234 decl-specifiers. */
16235 perform_deferred_access_checks (tf_warning_or_error);
16236
16237 /* Restore the saved value. */
16238 if (TREE_CODE (decl) == FUNCTION_DECL)
16239 current_function_decl = saved_current_function_decl;
16240 }
16241
16242 /* Parse the initializer. */
16243 initializer = NULL_TREE;
16244 is_direct_init = false;
16245 is_non_constant_init = true;
16246 if (is_initialized)
16247 {
16248 if (function_declarator_p (declarator))
16249 {
16250 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16251 if (initialization_kind == CPP_EQ)
16252 initializer = cp_parser_pure_specifier (parser);
16253 else
16254 {
16255 /* If the declaration was erroneous, we don't really
16256 know what the user intended, so just silently
16257 consume the initializer. */
16258 if (decl != error_mark_node)
16259 error_at (initializer_start_token->location,
16260 "initializer provided for function");
16261 cp_parser_skip_to_closing_parenthesis (parser,
16262 /*recovering=*/true,
16263 /*or_comma=*/false,
16264 /*consume_paren=*/true);
16265 }
16266 }
16267 else
16268 {
16269 /* We want to record the extra mangling scope for in-class
16270 initializers of class members and initializers of static data
16271 member templates. The former involves deferring
16272 parsing of the initializer until end of class as with default
16273 arguments. So right here we only handle the latter. */
16274 if (!member_p && processing_template_decl)
16275 start_lambda_scope (decl);
16276 initializer = cp_parser_initializer (parser,
16277 &is_direct_init,
16278 &is_non_constant_init);
16279 if (!member_p && processing_template_decl)
16280 finish_lambda_scope ();
16281 if (initializer == error_mark_node)
16282 cp_parser_skip_to_end_of_statement (parser);
16283 }
16284 }
16285
16286 /* The old parser allows attributes to appear after a parenthesized
16287 initializer. Mark Mitchell proposed removing this functionality
16288 on the GCC mailing lists on 2002-08-13. This parser accepts the
16289 attributes -- but ignores them. */
16290 if (cp_parser_allow_gnu_extensions_p (parser)
16291 && initialization_kind == CPP_OPEN_PAREN)
16292 if (cp_parser_attributes_opt (parser))
16293 warning (OPT_Wattributes,
16294 "attributes after parenthesized initializer ignored");
16295
16296 /* For an in-class declaration, use `grokfield' to create the
16297 declaration. */
16298 if (member_p)
16299 {
16300 if (pushed_scope)
16301 {
16302 pop_scope (pushed_scope);
16303 pushed_scope = NULL_TREE;
16304 }
16305 decl = grokfield (declarator, decl_specifiers,
16306 initializer, !is_non_constant_init,
16307 /*asmspec=*/NULL_TREE,
16308 prefix_attributes);
16309 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16310 cp_parser_save_default_args (parser, decl);
16311 }
16312
16313 /* Finish processing the declaration. But, skip member
16314 declarations. */
16315 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16316 {
16317 cp_finish_decl (decl,
16318 initializer, !is_non_constant_init,
16319 asm_specification,
16320 /* If the initializer is in parentheses, then this is
16321 a direct-initialization, which means that an
16322 `explicit' constructor is OK. Otherwise, an
16323 `explicit' constructor cannot be used. */
16324 ((is_direct_init || !is_initialized)
16325 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16326 }
16327 else if ((cxx_dialect != cxx98) && friend_p
16328 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16329 /* Core issue #226 (C++0x only): A default template-argument
16330 shall not be specified in a friend class template
16331 declaration. */
16332 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16333 /*is_partial=*/false, /*is_friend_decl=*/1);
16334
16335 if (!friend_p && pushed_scope)
16336 pop_scope (pushed_scope);
16337
16338 return decl;
16339 }
16340
16341 /* Parse a declarator.
16342
16343 declarator:
16344 direct-declarator
16345 ptr-operator declarator
16346
16347 abstract-declarator:
16348 ptr-operator abstract-declarator [opt]
16349 direct-abstract-declarator
16350
16351 GNU Extensions:
16352
16353 declarator:
16354 attributes [opt] direct-declarator
16355 attributes [opt] ptr-operator declarator
16356
16357 abstract-declarator:
16358 attributes [opt] ptr-operator abstract-declarator [opt]
16359 attributes [opt] direct-abstract-declarator
16360
16361 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16362 detect constructor, destructor or conversion operators. It is set
16363 to -1 if the declarator is a name, and +1 if it is a
16364 function. Otherwise it is set to zero. Usually you just want to
16365 test for >0, but internally the negative value is used.
16366
16367 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16368 a decl-specifier-seq unless it declares a constructor, destructor,
16369 or conversion. It might seem that we could check this condition in
16370 semantic analysis, rather than parsing, but that makes it difficult
16371 to handle something like `f()'. We want to notice that there are
16372 no decl-specifiers, and therefore realize that this is an
16373 expression, not a declaration.)
16374
16375 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16376 the declarator is a direct-declarator of the form "(...)".
16377
16378 MEMBER_P is true iff this declarator is a member-declarator. */
16379
16380 static cp_declarator *
16381 cp_parser_declarator (cp_parser* parser,
16382 cp_parser_declarator_kind dcl_kind,
16383 int* ctor_dtor_or_conv_p,
16384 bool* parenthesized_p,
16385 bool member_p)
16386 {
16387 cp_declarator *declarator;
16388 enum tree_code code;
16389 cp_cv_quals cv_quals;
16390 tree class_type;
16391 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16392
16393 /* Assume this is not a constructor, destructor, or type-conversion
16394 operator. */
16395 if (ctor_dtor_or_conv_p)
16396 *ctor_dtor_or_conv_p = 0;
16397
16398 if (cp_parser_allow_gnu_extensions_p (parser))
16399 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16400
16401 /* Check for the ptr-operator production. */
16402 cp_parser_parse_tentatively (parser);
16403 /* Parse the ptr-operator. */
16404 code = cp_parser_ptr_operator (parser,
16405 &class_type,
16406 &cv_quals,
16407 &std_attributes);
16408
16409 /* If that worked, then we have a ptr-operator. */
16410 if (cp_parser_parse_definitely (parser))
16411 {
16412 /* If a ptr-operator was found, then this declarator was not
16413 parenthesized. */
16414 if (parenthesized_p)
16415 *parenthesized_p = true;
16416 /* The dependent declarator is optional if we are parsing an
16417 abstract-declarator. */
16418 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16419 cp_parser_parse_tentatively (parser);
16420
16421 /* Parse the dependent declarator. */
16422 declarator = cp_parser_declarator (parser, dcl_kind,
16423 /*ctor_dtor_or_conv_p=*/NULL,
16424 /*parenthesized_p=*/NULL,
16425 /*member_p=*/false);
16426
16427 /* If we are parsing an abstract-declarator, we must handle the
16428 case where the dependent declarator is absent. */
16429 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16430 && !cp_parser_parse_definitely (parser))
16431 declarator = NULL;
16432
16433 declarator = cp_parser_make_indirect_declarator
16434 (code, class_type, cv_quals, declarator, std_attributes);
16435 }
16436 /* Everything else is a direct-declarator. */
16437 else
16438 {
16439 if (parenthesized_p)
16440 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16441 CPP_OPEN_PAREN);
16442 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16443 ctor_dtor_or_conv_p,
16444 member_p);
16445 }
16446
16447 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16448 declarator->attributes = gnu_attributes;
16449 return declarator;
16450 }
16451
16452 /* Parse a direct-declarator or direct-abstract-declarator.
16453
16454 direct-declarator:
16455 declarator-id
16456 direct-declarator ( parameter-declaration-clause )
16457 cv-qualifier-seq [opt]
16458 ref-qualifier [opt]
16459 exception-specification [opt]
16460 direct-declarator [ constant-expression [opt] ]
16461 ( declarator )
16462
16463 direct-abstract-declarator:
16464 direct-abstract-declarator [opt]
16465 ( parameter-declaration-clause )
16466 cv-qualifier-seq [opt]
16467 ref-qualifier [opt]
16468 exception-specification [opt]
16469 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16470 ( abstract-declarator )
16471
16472 Returns a representation of the declarator. DCL_KIND is
16473 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16474 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16475 we are parsing a direct-declarator. It is
16476 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16477 of ambiguity we prefer an abstract declarator, as per
16478 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16479 cp_parser_declarator. */
16480
16481 static cp_declarator *
16482 cp_parser_direct_declarator (cp_parser* parser,
16483 cp_parser_declarator_kind dcl_kind,
16484 int* ctor_dtor_or_conv_p,
16485 bool member_p)
16486 {
16487 cp_token *token;
16488 cp_declarator *declarator = NULL;
16489 tree scope = NULL_TREE;
16490 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16491 bool saved_in_declarator_p = parser->in_declarator_p;
16492 bool first = true;
16493 tree pushed_scope = NULL_TREE;
16494
16495 while (true)
16496 {
16497 /* Peek at the next token. */
16498 token = cp_lexer_peek_token (parser->lexer);
16499 if (token->type == CPP_OPEN_PAREN)
16500 {
16501 /* This is either a parameter-declaration-clause, or a
16502 parenthesized declarator. When we know we are parsing a
16503 named declarator, it must be a parenthesized declarator
16504 if FIRST is true. For instance, `(int)' is a
16505 parameter-declaration-clause, with an omitted
16506 direct-abstract-declarator. But `((*))', is a
16507 parenthesized abstract declarator. Finally, when T is a
16508 template parameter `(T)' is a
16509 parameter-declaration-clause, and not a parenthesized
16510 named declarator.
16511
16512 We first try and parse a parameter-declaration-clause,
16513 and then try a nested declarator (if FIRST is true).
16514
16515 It is not an error for it not to be a
16516 parameter-declaration-clause, even when FIRST is
16517 false. Consider,
16518
16519 int i (int);
16520 int i (3);
16521
16522 The first is the declaration of a function while the
16523 second is the definition of a variable, including its
16524 initializer.
16525
16526 Having seen only the parenthesis, we cannot know which of
16527 these two alternatives should be selected. Even more
16528 complex are examples like:
16529
16530 int i (int (a));
16531 int i (int (3));
16532
16533 The former is a function-declaration; the latter is a
16534 variable initialization.
16535
16536 Thus again, we try a parameter-declaration-clause, and if
16537 that fails, we back out and return. */
16538
16539 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16540 {
16541 tree params;
16542 unsigned saved_num_template_parameter_lists;
16543 bool is_declarator = false;
16544 tree t;
16545
16546 /* In a member-declarator, the only valid interpretation
16547 of a parenthesis is the start of a
16548 parameter-declaration-clause. (It is invalid to
16549 initialize a static data member with a parenthesized
16550 initializer; only the "=" form of initialization is
16551 permitted.) */
16552 if (!member_p)
16553 cp_parser_parse_tentatively (parser);
16554
16555 /* Consume the `('. */
16556 cp_lexer_consume_token (parser->lexer);
16557 if (first)
16558 {
16559 /* If this is going to be an abstract declarator, we're
16560 in a declarator and we can't have default args. */
16561 parser->default_arg_ok_p = false;
16562 parser->in_declarator_p = true;
16563 }
16564
16565 /* Inside the function parameter list, surrounding
16566 template-parameter-lists do not apply. */
16567 saved_num_template_parameter_lists
16568 = parser->num_template_parameter_lists;
16569 parser->num_template_parameter_lists = 0;
16570
16571 begin_scope (sk_function_parms, NULL_TREE);
16572
16573 /* Parse the parameter-declaration-clause. */
16574 params = cp_parser_parameter_declaration_clause (parser);
16575
16576 parser->num_template_parameter_lists
16577 = saved_num_template_parameter_lists;
16578
16579 /* Consume the `)'. */
16580 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16581
16582 /* If all went well, parse the cv-qualifier-seq,
16583 ref-qualifier and the exception-specification. */
16584 if (member_p || cp_parser_parse_definitely (parser))
16585 {
16586 cp_cv_quals cv_quals;
16587 cp_virt_specifiers virt_specifiers;
16588 cp_ref_qualifier ref_qual;
16589 tree exception_specification;
16590 tree late_return;
16591 tree attrs;
16592 bool memfn = (member_p || (pushed_scope
16593 && CLASS_TYPE_P (pushed_scope)));
16594
16595 is_declarator = true;
16596
16597 if (ctor_dtor_or_conv_p)
16598 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16599 first = false;
16600
16601 /* Parse the cv-qualifier-seq. */
16602 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16603 /* Parse the ref-qualifier. */
16604 ref_qual = cp_parser_ref_qualifier_opt (parser);
16605 /* And the exception-specification. */
16606 exception_specification
16607 = cp_parser_exception_specification_opt (parser);
16608
16609 attrs = cp_parser_std_attribute_spec_seq (parser);
16610
16611 late_return = (cp_parser_late_return_type_opt
16612 (parser, memfn ? cv_quals : -1));
16613
16614 /* Parse the virt-specifier-seq. */
16615 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16616
16617 /* Create the function-declarator. */
16618 declarator = make_call_declarator (declarator,
16619 params,
16620 cv_quals,
16621 virt_specifiers,
16622 ref_qual,
16623 exception_specification,
16624 late_return);
16625 declarator->std_attributes = attrs;
16626 /* Any subsequent parameter lists are to do with
16627 return type, so are not those of the declared
16628 function. */
16629 parser->default_arg_ok_p = false;
16630 }
16631
16632 /* Remove the function parms from scope. */
16633 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16634 pop_binding (DECL_NAME (t), t);
16635 leave_scope();
16636
16637 if (is_declarator)
16638 /* Repeat the main loop. */
16639 continue;
16640 }
16641
16642 /* If this is the first, we can try a parenthesized
16643 declarator. */
16644 if (first)
16645 {
16646 bool saved_in_type_id_in_expr_p;
16647
16648 parser->default_arg_ok_p = saved_default_arg_ok_p;
16649 parser->in_declarator_p = saved_in_declarator_p;
16650
16651 /* Consume the `('. */
16652 cp_lexer_consume_token (parser->lexer);
16653 /* Parse the nested declarator. */
16654 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16655 parser->in_type_id_in_expr_p = true;
16656 declarator
16657 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16658 /*parenthesized_p=*/NULL,
16659 member_p);
16660 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16661 first = false;
16662 /* Expect a `)'. */
16663 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16664 declarator = cp_error_declarator;
16665 if (declarator == cp_error_declarator)
16666 break;
16667
16668 goto handle_declarator;
16669 }
16670 /* Otherwise, we must be done. */
16671 else
16672 break;
16673 }
16674 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16675 && token->type == CPP_OPEN_SQUARE
16676 && !cp_next_tokens_can_be_attribute_p (parser))
16677 {
16678 /* Parse an array-declarator. */
16679 tree bounds, attrs;
16680
16681 if (ctor_dtor_or_conv_p)
16682 *ctor_dtor_or_conv_p = 0;
16683
16684 first = false;
16685 parser->default_arg_ok_p = false;
16686 parser->in_declarator_p = true;
16687 /* Consume the `['. */
16688 cp_lexer_consume_token (parser->lexer);
16689 /* Peek at the next token. */
16690 token = cp_lexer_peek_token (parser->lexer);
16691 /* If the next token is `]', then there is no
16692 constant-expression. */
16693 if (token->type != CPP_CLOSE_SQUARE)
16694 {
16695 bool non_constant_p;
16696
16697 bounds
16698 = cp_parser_constant_expression (parser,
16699 /*allow_non_constant=*/true,
16700 &non_constant_p);
16701 if (!non_constant_p)
16702 /* OK */;
16703 else if (error_operand_p (bounds))
16704 /* Already gave an error. */;
16705 else if (!parser->in_function_body
16706 || current_binding_level->kind == sk_function_parms)
16707 {
16708 /* Normally, the array bound must be an integral constant
16709 expression. However, as an extension, we allow VLAs
16710 in function scopes as long as they aren't part of a
16711 parameter declaration. */
16712 cp_parser_error (parser,
16713 "array bound is not an integer constant");
16714 bounds = error_mark_node;
16715 }
16716 else if (processing_template_decl)
16717 {
16718 /* Remember this wasn't a constant-expression. */
16719 bounds = build_nop (TREE_TYPE (bounds), bounds);
16720 TREE_SIDE_EFFECTS (bounds) = 1;
16721 }
16722 }
16723 else
16724 bounds = NULL_TREE;
16725 /* Look for the closing `]'. */
16726 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16727 {
16728 declarator = cp_error_declarator;
16729 break;
16730 }
16731
16732 attrs = cp_parser_std_attribute_spec_seq (parser);
16733 declarator = make_array_declarator (declarator, bounds);
16734 declarator->std_attributes = attrs;
16735 }
16736 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16737 {
16738 {
16739 tree qualifying_scope;
16740 tree unqualified_name;
16741 tree attrs;
16742 special_function_kind sfk;
16743 bool abstract_ok;
16744 bool pack_expansion_p = false;
16745 cp_token *declarator_id_start_token;
16746
16747 /* Parse a declarator-id */
16748 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16749 if (abstract_ok)
16750 {
16751 cp_parser_parse_tentatively (parser);
16752
16753 /* If we see an ellipsis, we should be looking at a
16754 parameter pack. */
16755 if (token->type == CPP_ELLIPSIS)
16756 {
16757 /* Consume the `...' */
16758 cp_lexer_consume_token (parser->lexer);
16759
16760 pack_expansion_p = true;
16761 }
16762 }
16763
16764 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16765 unqualified_name
16766 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16767 qualifying_scope = parser->scope;
16768 if (abstract_ok)
16769 {
16770 bool okay = false;
16771
16772 if (!unqualified_name && pack_expansion_p)
16773 {
16774 /* Check whether an error occurred. */
16775 okay = !cp_parser_error_occurred (parser);
16776
16777 /* We already consumed the ellipsis to mark a
16778 parameter pack, but we have no way to report it,
16779 so abort the tentative parse. We will be exiting
16780 immediately anyway. */
16781 cp_parser_abort_tentative_parse (parser);
16782 }
16783 else
16784 okay = cp_parser_parse_definitely (parser);
16785
16786 if (!okay)
16787 unqualified_name = error_mark_node;
16788 else if (unqualified_name
16789 && (qualifying_scope
16790 || (!identifier_p (unqualified_name))))
16791 {
16792 cp_parser_error (parser, "expected unqualified-id");
16793 unqualified_name = error_mark_node;
16794 }
16795 }
16796
16797 if (!unqualified_name)
16798 return NULL;
16799 if (unqualified_name == error_mark_node)
16800 {
16801 declarator = cp_error_declarator;
16802 pack_expansion_p = false;
16803 declarator->parameter_pack_p = false;
16804 break;
16805 }
16806
16807 attrs = cp_parser_std_attribute_spec_seq (parser);
16808
16809 if (qualifying_scope && at_namespace_scope_p ()
16810 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16811 {
16812 /* In the declaration of a member of a template class
16813 outside of the class itself, the SCOPE will sometimes
16814 be a TYPENAME_TYPE. For example, given:
16815
16816 template <typename T>
16817 int S<T>::R::i = 3;
16818
16819 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16820 this context, we must resolve S<T>::R to an ordinary
16821 type, rather than a typename type.
16822
16823 The reason we normally avoid resolving TYPENAME_TYPEs
16824 is that a specialization of `S' might render
16825 `S<T>::R' not a type. However, if `S' is
16826 specialized, then this `i' will not be used, so there
16827 is no harm in resolving the types here. */
16828 tree type;
16829
16830 /* Resolve the TYPENAME_TYPE. */
16831 type = resolve_typename_type (qualifying_scope,
16832 /*only_current_p=*/false);
16833 /* If that failed, the declarator is invalid. */
16834 if (TREE_CODE (type) == TYPENAME_TYPE)
16835 {
16836 if (typedef_variant_p (type))
16837 error_at (declarator_id_start_token->location,
16838 "cannot define member of dependent typedef "
16839 "%qT", type);
16840 else
16841 error_at (declarator_id_start_token->location,
16842 "%<%T::%E%> is not a type",
16843 TYPE_CONTEXT (qualifying_scope),
16844 TYPE_IDENTIFIER (qualifying_scope));
16845 }
16846 qualifying_scope = type;
16847 }
16848
16849 sfk = sfk_none;
16850
16851 if (unqualified_name)
16852 {
16853 tree class_type;
16854
16855 if (qualifying_scope
16856 && CLASS_TYPE_P (qualifying_scope))
16857 class_type = qualifying_scope;
16858 else
16859 class_type = current_class_type;
16860
16861 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16862 {
16863 tree name_type = TREE_TYPE (unqualified_name);
16864 if (class_type && same_type_p (name_type, class_type))
16865 {
16866 if (qualifying_scope
16867 && CLASSTYPE_USE_TEMPLATE (name_type))
16868 {
16869 error_at (declarator_id_start_token->location,
16870 "invalid use of constructor as a template");
16871 inform (declarator_id_start_token->location,
16872 "use %<%T::%D%> instead of %<%T::%D%> to "
16873 "name the constructor in a qualified name",
16874 class_type,
16875 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16876 class_type, name_type);
16877 declarator = cp_error_declarator;
16878 break;
16879 }
16880 else
16881 unqualified_name = constructor_name (class_type);
16882 }
16883 else
16884 {
16885 /* We do not attempt to print the declarator
16886 here because we do not have enough
16887 information about its original syntactic
16888 form. */
16889 cp_parser_error (parser, "invalid declarator");
16890 declarator = cp_error_declarator;
16891 break;
16892 }
16893 }
16894
16895 if (class_type)
16896 {
16897 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16898 sfk = sfk_destructor;
16899 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16900 sfk = sfk_conversion;
16901 else if (/* There's no way to declare a constructor
16902 for an anonymous type, even if the type
16903 got a name for linkage purposes. */
16904 !TYPE_WAS_ANONYMOUS (class_type)
16905 && constructor_name_p (unqualified_name,
16906 class_type))
16907 {
16908 unqualified_name = constructor_name (class_type);
16909 sfk = sfk_constructor;
16910 }
16911 else if (is_overloaded_fn (unqualified_name)
16912 && DECL_CONSTRUCTOR_P (get_first_fn
16913 (unqualified_name)))
16914 sfk = sfk_constructor;
16915
16916 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16917 *ctor_dtor_or_conv_p = -1;
16918 }
16919 }
16920 declarator = make_id_declarator (qualifying_scope,
16921 unqualified_name,
16922 sfk);
16923 declarator->std_attributes = attrs;
16924 declarator->id_loc = token->location;
16925 declarator->parameter_pack_p = pack_expansion_p;
16926
16927 if (pack_expansion_p)
16928 maybe_warn_variadic_templates ();
16929 }
16930
16931 handle_declarator:;
16932 scope = get_scope_of_declarator (declarator);
16933 if (scope)
16934 {
16935 /* Any names that appear after the declarator-id for a
16936 member are looked up in the containing scope. */
16937 if (at_function_scope_p ())
16938 {
16939 /* But declarations with qualified-ids can't appear in a
16940 function. */
16941 cp_parser_error (parser, "qualified-id in declaration");
16942 break;
16943 }
16944 pushed_scope = push_scope (scope);
16945 }
16946 parser->in_declarator_p = true;
16947 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16948 || (declarator && declarator->kind == cdk_id))
16949 /* Default args are only allowed on function
16950 declarations. */
16951 parser->default_arg_ok_p = saved_default_arg_ok_p;
16952 else
16953 parser->default_arg_ok_p = false;
16954
16955 first = false;
16956 }
16957 /* We're done. */
16958 else
16959 break;
16960 }
16961
16962 /* For an abstract declarator, we might wind up with nothing at this
16963 point. That's an error; the declarator is not optional. */
16964 if (!declarator)
16965 cp_parser_error (parser, "expected declarator");
16966
16967 /* If we entered a scope, we must exit it now. */
16968 if (pushed_scope)
16969 pop_scope (pushed_scope);
16970
16971 parser->default_arg_ok_p = saved_default_arg_ok_p;
16972 parser->in_declarator_p = saved_in_declarator_p;
16973
16974 return declarator;
16975 }
16976
16977 /* Parse a ptr-operator.
16978
16979 ptr-operator:
16980 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16981 * cv-qualifier-seq [opt]
16982 &
16983 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16984 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16985
16986 GNU Extension:
16987
16988 ptr-operator:
16989 & cv-qualifier-seq [opt]
16990
16991 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16992 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16993 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16994 filled in with the TYPE containing the member. *CV_QUALS is
16995 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16996 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16997 Note that the tree codes returned by this function have nothing
16998 to do with the types of trees that will be eventually be created
16999 to represent the pointer or reference type being parsed. They are
17000 just constants with suggestive names. */
17001 static enum tree_code
17002 cp_parser_ptr_operator (cp_parser* parser,
17003 tree* type,
17004 cp_cv_quals *cv_quals,
17005 tree *attributes)
17006 {
17007 enum tree_code code = ERROR_MARK;
17008 cp_token *token;
17009 tree attrs = NULL_TREE;
17010
17011 /* Assume that it's not a pointer-to-member. */
17012 *type = NULL_TREE;
17013 /* And that there are no cv-qualifiers. */
17014 *cv_quals = TYPE_UNQUALIFIED;
17015
17016 /* Peek at the next token. */
17017 token = cp_lexer_peek_token (parser->lexer);
17018
17019 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17020 if (token->type == CPP_MULT)
17021 code = INDIRECT_REF;
17022 else if (token->type == CPP_AND)
17023 code = ADDR_EXPR;
17024 else if ((cxx_dialect != cxx98) &&
17025 token->type == CPP_AND_AND) /* C++0x only */
17026 code = NON_LVALUE_EXPR;
17027
17028 if (code != ERROR_MARK)
17029 {
17030 /* Consume the `*', `&' or `&&'. */
17031 cp_lexer_consume_token (parser->lexer);
17032
17033 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17034 `&', if we are allowing GNU extensions. (The only qualifier
17035 that can legally appear after `&' is `restrict', but that is
17036 enforced during semantic analysis. */
17037 if (code == INDIRECT_REF
17038 || cp_parser_allow_gnu_extensions_p (parser))
17039 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17040
17041 attrs = cp_parser_std_attribute_spec_seq (parser);
17042 if (attributes != NULL)
17043 *attributes = attrs;
17044 }
17045 else
17046 {
17047 /* Try the pointer-to-member case. */
17048 cp_parser_parse_tentatively (parser);
17049 /* Look for the optional `::' operator. */
17050 cp_parser_global_scope_opt (parser,
17051 /*current_scope_valid_p=*/false);
17052 /* Look for the nested-name specifier. */
17053 token = cp_lexer_peek_token (parser->lexer);
17054 cp_parser_nested_name_specifier (parser,
17055 /*typename_keyword_p=*/false,
17056 /*check_dependency_p=*/true,
17057 /*type_p=*/false,
17058 /*is_declaration=*/false);
17059 /* If we found it, and the next token is a `*', then we are
17060 indeed looking at a pointer-to-member operator. */
17061 if (!cp_parser_error_occurred (parser)
17062 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17063 {
17064 /* Indicate that the `*' operator was used. */
17065 code = INDIRECT_REF;
17066
17067 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17068 error_at (token->location, "%qD is a namespace", parser->scope);
17069 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17070 error_at (token->location, "cannot form pointer to member of "
17071 "non-class %q#T", parser->scope);
17072 else
17073 {
17074 /* The type of which the member is a member is given by the
17075 current SCOPE. */
17076 *type = parser->scope;
17077 /* The next name will not be qualified. */
17078 parser->scope = NULL_TREE;
17079 parser->qualifying_scope = NULL_TREE;
17080 parser->object_scope = NULL_TREE;
17081 /* Look for optional c++11 attributes. */
17082 attrs = cp_parser_std_attribute_spec_seq (parser);
17083 if (attributes != NULL)
17084 *attributes = attrs;
17085 /* Look for the optional cv-qualifier-seq. */
17086 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17087 }
17088 }
17089 /* If that didn't work we don't have a ptr-operator. */
17090 if (!cp_parser_parse_definitely (parser))
17091 cp_parser_error (parser, "expected ptr-operator");
17092 }
17093
17094 return code;
17095 }
17096
17097 /* Parse an (optional) cv-qualifier-seq.
17098
17099 cv-qualifier-seq:
17100 cv-qualifier cv-qualifier-seq [opt]
17101
17102 cv-qualifier:
17103 const
17104 volatile
17105
17106 GNU Extension:
17107
17108 cv-qualifier:
17109 __restrict__
17110
17111 Returns a bitmask representing the cv-qualifiers. */
17112
17113 static cp_cv_quals
17114 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17115 {
17116 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17117
17118 while (true)
17119 {
17120 cp_token *token;
17121 cp_cv_quals cv_qualifier;
17122
17123 /* Peek at the next token. */
17124 token = cp_lexer_peek_token (parser->lexer);
17125 /* See if it's a cv-qualifier. */
17126 switch (token->keyword)
17127 {
17128 case RID_CONST:
17129 cv_qualifier = TYPE_QUAL_CONST;
17130 break;
17131
17132 case RID_VOLATILE:
17133 cv_qualifier = TYPE_QUAL_VOLATILE;
17134 break;
17135
17136 case RID_RESTRICT:
17137 cv_qualifier = TYPE_QUAL_RESTRICT;
17138 break;
17139
17140 default:
17141 cv_qualifier = TYPE_UNQUALIFIED;
17142 break;
17143 }
17144
17145 if (!cv_qualifier)
17146 break;
17147
17148 if (cv_quals & cv_qualifier)
17149 {
17150 error_at (token->location, "duplicate cv-qualifier");
17151 cp_lexer_purge_token (parser->lexer);
17152 }
17153 else
17154 {
17155 cp_lexer_consume_token (parser->lexer);
17156 cv_quals |= cv_qualifier;
17157 }
17158 }
17159
17160 return cv_quals;
17161 }
17162
17163 /* Parse an (optional) ref-qualifier
17164
17165 ref-qualifier:
17166 &
17167 &&
17168
17169 Returns cp_ref_qualifier representing ref-qualifier. */
17170
17171 static cp_ref_qualifier
17172 cp_parser_ref_qualifier_opt (cp_parser* parser)
17173 {
17174 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17175
17176 while (true)
17177 {
17178 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17179 cp_token *token = cp_lexer_peek_token (parser->lexer);
17180
17181 switch (token->type)
17182 {
17183 case CPP_AND:
17184 curr_ref_qual = REF_QUAL_LVALUE;
17185 break;
17186
17187 case CPP_AND_AND:
17188 curr_ref_qual = REF_QUAL_RVALUE;
17189 break;
17190
17191 default:
17192 curr_ref_qual = REF_QUAL_NONE;
17193 break;
17194 }
17195
17196 if (!curr_ref_qual)
17197 break;
17198 else if (ref_qual)
17199 {
17200 error_at (token->location, "multiple ref-qualifiers");
17201 cp_lexer_purge_token (parser->lexer);
17202 }
17203 else
17204 {
17205 ref_qual = curr_ref_qual;
17206 cp_lexer_consume_token (parser->lexer);
17207 }
17208 }
17209
17210 return ref_qual;
17211 }
17212
17213 /* Parse an (optional) virt-specifier-seq.
17214
17215 virt-specifier-seq:
17216 virt-specifier virt-specifier-seq [opt]
17217
17218 virt-specifier:
17219 override
17220 final
17221
17222 Returns a bitmask representing the virt-specifiers. */
17223
17224 static cp_virt_specifiers
17225 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17226 {
17227 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17228
17229 while (true)
17230 {
17231 cp_token *token;
17232 cp_virt_specifiers virt_specifier;
17233
17234 /* Peek at the next token. */
17235 token = cp_lexer_peek_token (parser->lexer);
17236 /* See if it's a virt-specifier-qualifier. */
17237 if (token->type != CPP_NAME)
17238 break;
17239 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17240 {
17241 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17242 virt_specifier = VIRT_SPEC_OVERRIDE;
17243 }
17244 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17245 {
17246 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17247 virt_specifier = VIRT_SPEC_FINAL;
17248 }
17249 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17250 {
17251 virt_specifier = VIRT_SPEC_FINAL;
17252 }
17253 else
17254 break;
17255
17256 if (virt_specifiers & virt_specifier)
17257 {
17258 error_at (token->location, "duplicate virt-specifier");
17259 cp_lexer_purge_token (parser->lexer);
17260 }
17261 else
17262 {
17263 cp_lexer_consume_token (parser->lexer);
17264 virt_specifiers |= virt_specifier;
17265 }
17266 }
17267 return virt_specifiers;
17268 }
17269
17270 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17271 is in scope even though it isn't real. */
17272
17273 static void
17274 inject_this_parameter (tree ctype, cp_cv_quals quals)
17275 {
17276 tree this_parm;
17277
17278 if (current_class_ptr)
17279 {
17280 /* We don't clear this between NSDMIs. Is it already what we want? */
17281 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17282 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17283 && cp_type_quals (type) == quals)
17284 return;
17285 }
17286
17287 this_parm = build_this_parm (ctype, quals);
17288 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17289 current_class_ptr = NULL_TREE;
17290 current_class_ref
17291 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17292 current_class_ptr = this_parm;
17293 }
17294
17295 /* Return true iff our current scope is a non-static data member
17296 initializer. */
17297
17298 bool
17299 parsing_nsdmi (void)
17300 {
17301 /* We recognize NSDMI context by the context-less 'this' pointer set up
17302 by the function above. */
17303 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17304 return true;
17305 return false;
17306 }
17307
17308 /* Parse a late-specified return type, if any. This is not a separate
17309 non-terminal, but part of a function declarator, which looks like
17310
17311 -> trailing-type-specifier-seq abstract-declarator(opt)
17312
17313 Returns the type indicated by the type-id.
17314
17315 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17316 function. */
17317
17318 static tree
17319 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
17320 {
17321 cp_token *token;
17322 tree type;
17323
17324 /* Peek at the next token. */
17325 token = cp_lexer_peek_token (parser->lexer);
17326 /* A late-specified return type is indicated by an initial '->'. */
17327 if (token->type != CPP_DEREF)
17328 return NULL_TREE;
17329
17330 /* Consume the ->. */
17331 cp_lexer_consume_token (parser->lexer);
17332
17333 tree save_ccp = current_class_ptr;
17334 tree save_ccr = current_class_ref;
17335 if (quals >= 0)
17336 {
17337 /* DR 1207: 'this' is in scope in the trailing return type. */
17338 inject_this_parameter (current_class_type, quals);
17339 }
17340
17341 type = cp_parser_trailing_type_id (parser);
17342
17343 if (quals >= 0)
17344 {
17345 current_class_ptr = save_ccp;
17346 current_class_ref = save_ccr;
17347 }
17348
17349 return type;
17350 }
17351
17352 /* Parse a declarator-id.
17353
17354 declarator-id:
17355 id-expression
17356 :: [opt] nested-name-specifier [opt] type-name
17357
17358 In the `id-expression' case, the value returned is as for
17359 cp_parser_id_expression if the id-expression was an unqualified-id.
17360 If the id-expression was a qualified-id, then a SCOPE_REF is
17361 returned. The first operand is the scope (either a NAMESPACE_DECL
17362 or TREE_TYPE), but the second is still just a representation of an
17363 unqualified-id. */
17364
17365 static tree
17366 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17367 {
17368 tree id;
17369 /* The expression must be an id-expression. Assume that qualified
17370 names are the names of types so that:
17371
17372 template <class T>
17373 int S<T>::R::i = 3;
17374
17375 will work; we must treat `S<T>::R' as the name of a type.
17376 Similarly, assume that qualified names are templates, where
17377 required, so that:
17378
17379 template <class T>
17380 int S<T>::R<T>::i = 3;
17381
17382 will work, too. */
17383 id = cp_parser_id_expression (parser,
17384 /*template_keyword_p=*/false,
17385 /*check_dependency_p=*/false,
17386 /*template_p=*/NULL,
17387 /*declarator_p=*/true,
17388 optional_p);
17389 if (id && BASELINK_P (id))
17390 id = BASELINK_FUNCTIONS (id);
17391 return id;
17392 }
17393
17394 /* Parse a type-id.
17395
17396 type-id:
17397 type-specifier-seq abstract-declarator [opt]
17398
17399 Returns the TYPE specified. */
17400
17401 static tree
17402 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17403 bool is_trailing_return)
17404 {
17405 cp_decl_specifier_seq type_specifier_seq;
17406 cp_declarator *abstract_declarator;
17407
17408 /* Parse the type-specifier-seq. */
17409 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17410 is_trailing_return,
17411 &type_specifier_seq);
17412 if (type_specifier_seq.type == error_mark_node)
17413 return error_mark_node;
17414
17415 /* There might or might not be an abstract declarator. */
17416 cp_parser_parse_tentatively (parser);
17417 /* Look for the declarator. */
17418 abstract_declarator
17419 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17420 /*parenthesized_p=*/NULL,
17421 /*member_p=*/false);
17422 /* Check to see if there really was a declarator. */
17423 if (!cp_parser_parse_definitely (parser))
17424 abstract_declarator = NULL;
17425
17426 if (type_specifier_seq.type
17427 && cxx_dialect < cxx1y
17428 && type_uses_auto (type_specifier_seq.type))
17429 {
17430 /* A type-id with type 'auto' is only ok if the abstract declarator
17431 is a function declarator with a late-specified return type. */
17432 if (abstract_declarator
17433 && abstract_declarator->kind == cdk_function
17434 && abstract_declarator->u.function.late_return_type)
17435 /* OK */;
17436 else
17437 {
17438 error ("invalid use of %<auto%>");
17439 return error_mark_node;
17440 }
17441 }
17442
17443 return groktypename (&type_specifier_seq, abstract_declarator,
17444 is_template_arg);
17445 }
17446
17447 static tree cp_parser_type_id (cp_parser *parser)
17448 {
17449 return cp_parser_type_id_1 (parser, false, false);
17450 }
17451
17452 static tree cp_parser_template_type_arg (cp_parser *parser)
17453 {
17454 tree r;
17455 const char *saved_message = parser->type_definition_forbidden_message;
17456 parser->type_definition_forbidden_message
17457 = G_("types may not be defined in template arguments");
17458 r = cp_parser_type_id_1 (parser, true, false);
17459 parser->type_definition_forbidden_message = saved_message;
17460 return r;
17461 }
17462
17463 static tree cp_parser_trailing_type_id (cp_parser *parser)
17464 {
17465 return cp_parser_type_id_1 (parser, false, true);
17466 }
17467
17468 /* Parse a type-specifier-seq.
17469
17470 type-specifier-seq:
17471 type-specifier type-specifier-seq [opt]
17472
17473 GNU extension:
17474
17475 type-specifier-seq:
17476 attributes type-specifier-seq [opt]
17477
17478 If IS_DECLARATION is true, we are at the start of a "condition" or
17479 exception-declaration, so we might be followed by a declarator-id.
17480
17481 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17482 i.e. we've just seen "->".
17483
17484 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17485
17486 static void
17487 cp_parser_type_specifier_seq (cp_parser* parser,
17488 bool is_declaration,
17489 bool is_trailing_return,
17490 cp_decl_specifier_seq *type_specifier_seq)
17491 {
17492 bool seen_type_specifier = false;
17493 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17494 cp_token *start_token = NULL;
17495
17496 /* Clear the TYPE_SPECIFIER_SEQ. */
17497 clear_decl_specs (type_specifier_seq);
17498
17499 /* In the context of a trailing return type, enum E { } is an
17500 elaborated-type-specifier followed by a function-body, not an
17501 enum-specifier. */
17502 if (is_trailing_return)
17503 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17504
17505 /* Parse the type-specifiers and attributes. */
17506 while (true)
17507 {
17508 tree type_specifier;
17509 bool is_cv_qualifier;
17510
17511 /* Check for attributes first. */
17512 if (cp_next_tokens_can_be_attribute_p (parser))
17513 {
17514 type_specifier_seq->attributes =
17515 chainon (type_specifier_seq->attributes,
17516 cp_parser_attributes_opt (parser));
17517 continue;
17518 }
17519
17520 /* record the token of the beginning of the type specifier seq,
17521 for error reporting purposes*/
17522 if (!start_token)
17523 start_token = cp_lexer_peek_token (parser->lexer);
17524
17525 /* Look for the type-specifier. */
17526 type_specifier = cp_parser_type_specifier (parser,
17527 flags,
17528 type_specifier_seq,
17529 /*is_declaration=*/false,
17530 NULL,
17531 &is_cv_qualifier);
17532 if (!type_specifier)
17533 {
17534 /* If the first type-specifier could not be found, this is not a
17535 type-specifier-seq at all. */
17536 if (!seen_type_specifier)
17537 {
17538 cp_parser_error (parser, "expected type-specifier");
17539 type_specifier_seq->type = error_mark_node;
17540 return;
17541 }
17542 /* If subsequent type-specifiers could not be found, the
17543 type-specifier-seq is complete. */
17544 break;
17545 }
17546
17547 seen_type_specifier = true;
17548 /* The standard says that a condition can be:
17549
17550 type-specifier-seq declarator = assignment-expression
17551
17552 However, given:
17553
17554 struct S {};
17555 if (int S = ...)
17556
17557 we should treat the "S" as a declarator, not as a
17558 type-specifier. The standard doesn't say that explicitly for
17559 type-specifier-seq, but it does say that for
17560 decl-specifier-seq in an ordinary declaration. Perhaps it
17561 would be clearer just to allow a decl-specifier-seq here, and
17562 then add a semantic restriction that if any decl-specifiers
17563 that are not type-specifiers appear, the program is invalid. */
17564 if (is_declaration && !is_cv_qualifier)
17565 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17566 }
17567 }
17568
17569 /* Parse a parameter-declaration-clause.
17570
17571 parameter-declaration-clause:
17572 parameter-declaration-list [opt] ... [opt]
17573 parameter-declaration-list , ...
17574
17575 Returns a representation for the parameter declarations. A return
17576 value of NULL indicates a parameter-declaration-clause consisting
17577 only of an ellipsis. */
17578
17579 static tree
17580 cp_parser_parameter_declaration_clause (cp_parser* parser)
17581 {
17582 tree parameters;
17583 cp_token *token;
17584 bool ellipsis_p;
17585 bool is_error;
17586
17587 /* Peek at the next token. */
17588 token = cp_lexer_peek_token (parser->lexer);
17589 /* Check for trivial parameter-declaration-clauses. */
17590 if (token->type == CPP_ELLIPSIS)
17591 {
17592 /* Consume the `...' token. */
17593 cp_lexer_consume_token (parser->lexer);
17594 return NULL_TREE;
17595 }
17596 else if (token->type == CPP_CLOSE_PAREN)
17597 /* There are no parameters. */
17598 {
17599 #ifndef NO_IMPLICIT_EXTERN_C
17600 if (in_system_header && current_class_type == NULL
17601 && current_lang_name == lang_name_c)
17602 return NULL_TREE;
17603 else
17604 #endif
17605 return void_list_node;
17606 }
17607 /* Check for `(void)', too, which is a special case. */
17608 else if (token->keyword == RID_VOID
17609 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17610 == CPP_CLOSE_PAREN))
17611 {
17612 /* Consume the `void' token. */
17613 cp_lexer_consume_token (parser->lexer);
17614 /* There are no parameters. */
17615 return void_list_node;
17616 }
17617
17618 /* Parse the parameter-declaration-list. */
17619 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17620 /* If a parse error occurred while parsing the
17621 parameter-declaration-list, then the entire
17622 parameter-declaration-clause is erroneous. */
17623 if (is_error)
17624 return NULL;
17625
17626 /* Peek at the next token. */
17627 token = cp_lexer_peek_token (parser->lexer);
17628 /* If it's a `,', the clause should terminate with an ellipsis. */
17629 if (token->type == CPP_COMMA)
17630 {
17631 /* Consume the `,'. */
17632 cp_lexer_consume_token (parser->lexer);
17633 /* Expect an ellipsis. */
17634 ellipsis_p
17635 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17636 }
17637 /* It might also be `...' if the optional trailing `,' was
17638 omitted. */
17639 else if (token->type == CPP_ELLIPSIS)
17640 {
17641 /* Consume the `...' token. */
17642 cp_lexer_consume_token (parser->lexer);
17643 /* And remember that we saw it. */
17644 ellipsis_p = true;
17645 }
17646 else
17647 ellipsis_p = false;
17648
17649 /* Finish the parameter list. */
17650 if (!ellipsis_p)
17651 parameters = chainon (parameters, void_list_node);
17652
17653 return parameters;
17654 }
17655
17656 /* Parse a parameter-declaration-list.
17657
17658 parameter-declaration-list:
17659 parameter-declaration
17660 parameter-declaration-list , parameter-declaration
17661
17662 Returns a representation of the parameter-declaration-list, as for
17663 cp_parser_parameter_declaration_clause. However, the
17664 `void_list_node' is never appended to the list. Upon return,
17665 *IS_ERROR will be true iff an error occurred. */
17666
17667 static tree
17668 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17669 {
17670 tree parameters = NULL_TREE;
17671 tree *tail = &parameters;
17672 bool saved_in_unbraced_linkage_specification_p;
17673 int index = 0;
17674
17675 /* Assume all will go well. */
17676 *is_error = false;
17677 /* The special considerations that apply to a function within an
17678 unbraced linkage specifications do not apply to the parameters
17679 to the function. */
17680 saved_in_unbraced_linkage_specification_p
17681 = parser->in_unbraced_linkage_specification_p;
17682 parser->in_unbraced_linkage_specification_p = false;
17683
17684 /* Look for more parameters. */
17685 while (true)
17686 {
17687 cp_parameter_declarator *parameter;
17688 tree decl = error_mark_node;
17689 bool parenthesized_p = false;
17690 /* Parse the parameter. */
17691 parameter
17692 = cp_parser_parameter_declaration (parser,
17693 /*template_parm_p=*/false,
17694 &parenthesized_p);
17695
17696 /* We don't know yet if the enclosing context is deprecated, so wait
17697 and warn in grokparms if appropriate. */
17698 deprecated_state = DEPRECATED_SUPPRESS;
17699
17700 if (parameter)
17701 decl = grokdeclarator (parameter->declarator,
17702 &parameter->decl_specifiers,
17703 PARM,
17704 parameter->default_argument != NULL_TREE,
17705 &parameter->decl_specifiers.attributes);
17706
17707 deprecated_state = DEPRECATED_NORMAL;
17708
17709 /* If a parse error occurred parsing the parameter declaration,
17710 then the entire parameter-declaration-list is erroneous. */
17711 if (decl == error_mark_node)
17712 {
17713 *is_error = true;
17714 parameters = error_mark_node;
17715 break;
17716 }
17717
17718 if (parameter->decl_specifiers.attributes)
17719 cplus_decl_attributes (&decl,
17720 parameter->decl_specifiers.attributes,
17721 0);
17722 if (DECL_NAME (decl))
17723 decl = pushdecl (decl);
17724
17725 if (decl != error_mark_node)
17726 {
17727 retrofit_lang_decl (decl);
17728 DECL_PARM_INDEX (decl) = ++index;
17729 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17730 }
17731
17732 /* Add the new parameter to the list. */
17733 *tail = build_tree_list (parameter->default_argument, decl);
17734 tail = &TREE_CHAIN (*tail);
17735
17736 /* Peek at the next token. */
17737 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17738 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17739 /* These are for Objective-C++ */
17740 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17741 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17742 /* The parameter-declaration-list is complete. */
17743 break;
17744 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17745 {
17746 cp_token *token;
17747
17748 /* Peek at the next token. */
17749 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17750 /* If it's an ellipsis, then the list is complete. */
17751 if (token->type == CPP_ELLIPSIS)
17752 break;
17753 /* Otherwise, there must be more parameters. Consume the
17754 `,'. */
17755 cp_lexer_consume_token (parser->lexer);
17756 /* When parsing something like:
17757
17758 int i(float f, double d)
17759
17760 we can tell after seeing the declaration for "f" that we
17761 are not looking at an initialization of a variable "i",
17762 but rather at the declaration of a function "i".
17763
17764 Due to the fact that the parsing of template arguments
17765 (as specified to a template-id) requires backtracking we
17766 cannot use this technique when inside a template argument
17767 list. */
17768 if (!parser->in_template_argument_list_p
17769 && !parser->in_type_id_in_expr_p
17770 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17771 /* However, a parameter-declaration of the form
17772 "foat(f)" (which is a valid declaration of a
17773 parameter "f") can also be interpreted as an
17774 expression (the conversion of "f" to "float"). */
17775 && !parenthesized_p)
17776 cp_parser_commit_to_tentative_parse (parser);
17777 }
17778 else
17779 {
17780 cp_parser_error (parser, "expected %<,%> or %<...%>");
17781 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17782 cp_parser_skip_to_closing_parenthesis (parser,
17783 /*recovering=*/true,
17784 /*or_comma=*/false,
17785 /*consume_paren=*/false);
17786 break;
17787 }
17788 }
17789
17790 parser->in_unbraced_linkage_specification_p
17791 = saved_in_unbraced_linkage_specification_p;
17792
17793 return parameters;
17794 }
17795
17796 /* Parse a parameter declaration.
17797
17798 parameter-declaration:
17799 decl-specifier-seq ... [opt] declarator
17800 decl-specifier-seq declarator = assignment-expression
17801 decl-specifier-seq ... [opt] abstract-declarator [opt]
17802 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17803
17804 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17805 declares a template parameter. (In that case, a non-nested `>'
17806 token encountered during the parsing of the assignment-expression
17807 is not interpreted as a greater-than operator.)
17808
17809 Returns a representation of the parameter, or NULL if an error
17810 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17811 true iff the declarator is of the form "(p)". */
17812
17813 static cp_parameter_declarator *
17814 cp_parser_parameter_declaration (cp_parser *parser,
17815 bool template_parm_p,
17816 bool *parenthesized_p)
17817 {
17818 int declares_class_or_enum;
17819 cp_decl_specifier_seq decl_specifiers;
17820 cp_declarator *declarator;
17821 tree default_argument;
17822 cp_token *token = NULL, *declarator_token_start = NULL;
17823 const char *saved_message;
17824
17825 /* In a template parameter, `>' is not an operator.
17826
17827 [temp.param]
17828
17829 When parsing a default template-argument for a non-type
17830 template-parameter, the first non-nested `>' is taken as the end
17831 of the template parameter-list rather than a greater-than
17832 operator. */
17833
17834 /* Type definitions may not appear in parameter types. */
17835 saved_message = parser->type_definition_forbidden_message;
17836 parser->type_definition_forbidden_message
17837 = G_("types may not be defined in parameter types");
17838
17839 /* Parse the declaration-specifiers. */
17840 cp_parser_decl_specifier_seq (parser,
17841 CP_PARSER_FLAGS_NONE,
17842 &decl_specifiers,
17843 &declares_class_or_enum);
17844
17845 /* Complain about missing 'typename' or other invalid type names. */
17846 if (!decl_specifiers.any_type_specifiers_p)
17847 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17848
17849 /* If an error occurred, there's no reason to attempt to parse the
17850 rest of the declaration. */
17851 if (cp_parser_error_occurred (parser))
17852 {
17853 parser->type_definition_forbidden_message = saved_message;
17854 return NULL;
17855 }
17856
17857 /* Peek at the next token. */
17858 token = cp_lexer_peek_token (parser->lexer);
17859
17860 /* If the next token is a `)', `,', `=', `>', or `...', then there
17861 is no declarator. However, when variadic templates are enabled,
17862 there may be a declarator following `...'. */
17863 if (token->type == CPP_CLOSE_PAREN
17864 || token->type == CPP_COMMA
17865 || token->type == CPP_EQ
17866 || token->type == CPP_GREATER)
17867 {
17868 declarator = NULL;
17869 if (parenthesized_p)
17870 *parenthesized_p = false;
17871 }
17872 /* Otherwise, there should be a declarator. */
17873 else
17874 {
17875 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17876 parser->default_arg_ok_p = false;
17877
17878 /* After seeing a decl-specifier-seq, if the next token is not a
17879 "(", there is no possibility that the code is a valid
17880 expression. Therefore, if parsing tentatively, we commit at
17881 this point. */
17882 if (!parser->in_template_argument_list_p
17883 /* In an expression context, having seen:
17884
17885 (int((char ...
17886
17887 we cannot be sure whether we are looking at a
17888 function-type (taking a "char" as a parameter) or a cast
17889 of some object of type "char" to "int". */
17890 && !parser->in_type_id_in_expr_p
17891 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17892 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17893 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17894 cp_parser_commit_to_tentative_parse (parser);
17895 /* Parse the declarator. */
17896 declarator_token_start = token;
17897 declarator = cp_parser_declarator (parser,
17898 CP_PARSER_DECLARATOR_EITHER,
17899 /*ctor_dtor_or_conv_p=*/NULL,
17900 parenthesized_p,
17901 /*member_p=*/false);
17902 parser->default_arg_ok_p = saved_default_arg_ok_p;
17903 /* After the declarator, allow more attributes. */
17904 decl_specifiers.attributes
17905 = chainon (decl_specifiers.attributes,
17906 cp_parser_attributes_opt (parser));
17907 }
17908
17909 /* If the next token is an ellipsis, and we have not seen a
17910 declarator name, and the type of the declarator contains parameter
17911 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17912 a parameter pack expansion expression. Otherwise, leave the
17913 ellipsis for a C-style variadic function. */
17914 token = cp_lexer_peek_token (parser->lexer);
17915 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17916 {
17917 tree type = decl_specifiers.type;
17918
17919 if (type && DECL_P (type))
17920 type = TREE_TYPE (type);
17921
17922 if (type
17923 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17924 && declarator_can_be_parameter_pack (declarator)
17925 && (!declarator || !declarator->parameter_pack_p)
17926 && uses_parameter_packs (type))
17927 {
17928 /* Consume the `...'. */
17929 cp_lexer_consume_token (parser->lexer);
17930 maybe_warn_variadic_templates ();
17931
17932 /* Build a pack expansion type */
17933 if (declarator)
17934 declarator->parameter_pack_p = true;
17935 else
17936 decl_specifiers.type = make_pack_expansion (type);
17937 }
17938 }
17939
17940 /* The restriction on defining new types applies only to the type
17941 of the parameter, not to the default argument. */
17942 parser->type_definition_forbidden_message = saved_message;
17943
17944 /* If the next token is `=', then process a default argument. */
17945 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17946 {
17947 token = cp_lexer_peek_token (parser->lexer);
17948 /* If we are defining a class, then the tokens that make up the
17949 default argument must be saved and processed later. */
17950 if (!template_parm_p && at_class_scope_p ()
17951 && TYPE_BEING_DEFINED (current_class_type)
17952 && !LAMBDA_TYPE_P (current_class_type))
17953 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17954 /* Outside of a class definition, we can just parse the
17955 assignment-expression. */
17956 else
17957 default_argument
17958 = cp_parser_default_argument (parser, template_parm_p);
17959
17960 if (!parser->default_arg_ok_p)
17961 {
17962 if (flag_permissive)
17963 warning (0, "deprecated use of default argument for parameter of non-function");
17964 else
17965 {
17966 error_at (token->location,
17967 "default arguments are only "
17968 "permitted for function parameters");
17969 default_argument = NULL_TREE;
17970 }
17971 }
17972 else if ((declarator && declarator->parameter_pack_p)
17973 || (decl_specifiers.type
17974 && PACK_EXPANSION_P (decl_specifiers.type)))
17975 {
17976 /* Find the name of the parameter pack. */
17977 cp_declarator *id_declarator = declarator;
17978 while (id_declarator && id_declarator->kind != cdk_id)
17979 id_declarator = id_declarator->declarator;
17980
17981 if (id_declarator && id_declarator->kind == cdk_id)
17982 error_at (declarator_token_start->location,
17983 template_parm_p
17984 ? G_("template parameter pack %qD "
17985 "cannot have a default argument")
17986 : G_("parameter pack %qD cannot have "
17987 "a default argument"),
17988 id_declarator->u.id.unqualified_name);
17989 else
17990 error_at (declarator_token_start->location,
17991 template_parm_p
17992 ? G_("template parameter pack cannot have "
17993 "a default argument")
17994 : G_("parameter pack cannot have a "
17995 "default argument"));
17996
17997 default_argument = NULL_TREE;
17998 }
17999 }
18000 else
18001 default_argument = NULL_TREE;
18002
18003 return make_parameter_declarator (&decl_specifiers,
18004 declarator,
18005 default_argument);
18006 }
18007
18008 /* Parse a default argument and return it.
18009
18010 TEMPLATE_PARM_P is true if this is a default argument for a
18011 non-type template parameter. */
18012 static tree
18013 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18014 {
18015 tree default_argument = NULL_TREE;
18016 bool saved_greater_than_is_operator_p;
18017 bool saved_local_variables_forbidden_p;
18018 bool non_constant_p, is_direct_init;
18019
18020 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18021 set correctly. */
18022 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18023 parser->greater_than_is_operator_p = !template_parm_p;
18024 /* Local variable names (and the `this' keyword) may not
18025 appear in a default argument. */
18026 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18027 parser->local_variables_forbidden_p = true;
18028 /* Parse the assignment-expression. */
18029 if (template_parm_p)
18030 push_deferring_access_checks (dk_no_deferred);
18031 default_argument
18032 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18033 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18034 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18035 if (template_parm_p)
18036 pop_deferring_access_checks ();
18037 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18038 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18039
18040 return default_argument;
18041 }
18042
18043 /* Parse a function-body.
18044
18045 function-body:
18046 compound_statement */
18047
18048 static void
18049 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18050 {
18051 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18052 }
18053
18054 /* Parse a ctor-initializer-opt followed by a function-body. Return
18055 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18056 is true we are parsing a function-try-block. */
18057
18058 static bool
18059 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18060 bool in_function_try_block)
18061 {
18062 tree body, list;
18063 bool ctor_initializer_p;
18064 const bool check_body_p =
18065 DECL_CONSTRUCTOR_P (current_function_decl)
18066 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18067 tree last = NULL;
18068
18069 /* Begin the function body. */
18070 body = begin_function_body ();
18071 /* Parse the optional ctor-initializer. */
18072 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18073
18074 /* If we're parsing a constexpr constructor definition, we need
18075 to check that the constructor body is indeed empty. However,
18076 before we get to cp_parser_function_body lot of junk has been
18077 generated, so we can't just check that we have an empty block.
18078 Rather we take a snapshot of the outermost block, and check whether
18079 cp_parser_function_body changed its state. */
18080 if (check_body_p)
18081 {
18082 list = cur_stmt_list;
18083 if (STATEMENT_LIST_TAIL (list))
18084 last = STATEMENT_LIST_TAIL (list)->stmt;
18085 }
18086 /* Parse the function-body. */
18087 cp_parser_function_body (parser, in_function_try_block);
18088 if (check_body_p)
18089 check_constexpr_ctor_body (last, list);
18090 /* Finish the function body. */
18091 finish_function_body (body);
18092
18093 return ctor_initializer_p;
18094 }
18095
18096 /* Parse an initializer.
18097
18098 initializer:
18099 = initializer-clause
18100 ( expression-list )
18101
18102 Returns an expression representing the initializer. If no
18103 initializer is present, NULL_TREE is returned.
18104
18105 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18106 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18107 set to TRUE if there is no initializer present. If there is an
18108 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18109 is set to true; otherwise it is set to false. */
18110
18111 static tree
18112 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18113 bool* non_constant_p)
18114 {
18115 cp_token *token;
18116 tree init;
18117
18118 /* Peek at the next token. */
18119 token = cp_lexer_peek_token (parser->lexer);
18120
18121 /* Let our caller know whether or not this initializer was
18122 parenthesized. */
18123 *is_direct_init = (token->type != CPP_EQ);
18124 /* Assume that the initializer is constant. */
18125 *non_constant_p = false;
18126
18127 if (token->type == CPP_EQ)
18128 {
18129 /* Consume the `='. */
18130 cp_lexer_consume_token (parser->lexer);
18131 /* Parse the initializer-clause. */
18132 init = cp_parser_initializer_clause (parser, non_constant_p);
18133 }
18134 else if (token->type == CPP_OPEN_PAREN)
18135 {
18136 vec<tree, va_gc> *vec;
18137 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18138 /*cast_p=*/false,
18139 /*allow_expansion_p=*/true,
18140 non_constant_p);
18141 if (vec == NULL)
18142 return error_mark_node;
18143 init = build_tree_list_vec (vec);
18144 release_tree_vector (vec);
18145 }
18146 else if (token->type == CPP_OPEN_BRACE)
18147 {
18148 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18149 init = cp_parser_braced_list (parser, non_constant_p);
18150 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18151 }
18152 else
18153 {
18154 /* Anything else is an error. */
18155 cp_parser_error (parser, "expected initializer");
18156 init = error_mark_node;
18157 }
18158
18159 return init;
18160 }
18161
18162 /* Parse an initializer-clause.
18163
18164 initializer-clause:
18165 assignment-expression
18166 braced-init-list
18167
18168 Returns an expression representing the initializer.
18169
18170 If the `assignment-expression' production is used the value
18171 returned is simply a representation for the expression.
18172
18173 Otherwise, calls cp_parser_braced_list. */
18174
18175 static tree
18176 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18177 {
18178 tree initializer;
18179
18180 /* Assume the expression is constant. */
18181 *non_constant_p = false;
18182
18183 /* If it is not a `{', then we are looking at an
18184 assignment-expression. */
18185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18186 {
18187 initializer
18188 = cp_parser_constant_expression (parser,
18189 /*allow_non_constant_p=*/true,
18190 non_constant_p);
18191 }
18192 else
18193 initializer = cp_parser_braced_list (parser, non_constant_p);
18194
18195 return initializer;
18196 }
18197
18198 /* Parse a brace-enclosed initializer list.
18199
18200 braced-init-list:
18201 { initializer-list , [opt] }
18202 { }
18203
18204 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18205 the elements of the initializer-list (or NULL, if the last
18206 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18207 NULL_TREE. There is no way to detect whether or not the optional
18208 trailing `,' was provided. NON_CONSTANT_P is as for
18209 cp_parser_initializer. */
18210
18211 static tree
18212 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18213 {
18214 tree initializer;
18215
18216 /* Consume the `{' token. */
18217 cp_lexer_consume_token (parser->lexer);
18218 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18219 initializer = make_node (CONSTRUCTOR);
18220 /* If it's not a `}', then there is a non-trivial initializer. */
18221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18222 {
18223 /* Parse the initializer list. */
18224 CONSTRUCTOR_ELTS (initializer)
18225 = cp_parser_initializer_list (parser, non_constant_p);
18226 /* A trailing `,' token is allowed. */
18227 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18228 cp_lexer_consume_token (parser->lexer);
18229 }
18230 else
18231 *non_constant_p = false;
18232 /* Now, there should be a trailing `}'. */
18233 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18234 TREE_TYPE (initializer) = init_list_type_node;
18235 return initializer;
18236 }
18237
18238 /* Parse an initializer-list.
18239
18240 initializer-list:
18241 initializer-clause ... [opt]
18242 initializer-list , initializer-clause ... [opt]
18243
18244 GNU Extension:
18245
18246 initializer-list:
18247 designation initializer-clause ...[opt]
18248 initializer-list , designation initializer-clause ...[opt]
18249
18250 designation:
18251 . identifier =
18252 identifier :
18253 [ constant-expression ] =
18254
18255 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18256 for the initializer. If the INDEX of the elt is non-NULL, it is the
18257 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18258 as for cp_parser_initializer. */
18259
18260 static vec<constructor_elt, va_gc> *
18261 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18262 {
18263 vec<constructor_elt, va_gc> *v = NULL;
18264
18265 /* Assume all of the expressions are constant. */
18266 *non_constant_p = false;
18267
18268 /* Parse the rest of the list. */
18269 while (true)
18270 {
18271 cp_token *token;
18272 tree designator;
18273 tree initializer;
18274 bool clause_non_constant_p;
18275
18276 /* If the next token is an identifier and the following one is a
18277 colon, we are looking at the GNU designated-initializer
18278 syntax. */
18279 if (cp_parser_allow_gnu_extensions_p (parser)
18280 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18281 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18282 {
18283 /* Warn the user that they are using an extension. */
18284 pedwarn (input_location, OPT_Wpedantic,
18285 "ISO C++ does not allow designated initializers");
18286 /* Consume the identifier. */
18287 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18288 /* Consume the `:'. */
18289 cp_lexer_consume_token (parser->lexer);
18290 }
18291 /* Also handle the C99 syntax, '. id ='. */
18292 else if (cp_parser_allow_gnu_extensions_p (parser)
18293 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18294 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18295 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18296 {
18297 /* Warn the user that they are using an extension. */
18298 pedwarn (input_location, OPT_Wpedantic,
18299 "ISO C++ does not allow C99 designated initializers");
18300 /* Consume the `.'. */
18301 cp_lexer_consume_token (parser->lexer);
18302 /* Consume the identifier. */
18303 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18304 /* Consume the `='. */
18305 cp_lexer_consume_token (parser->lexer);
18306 }
18307 /* Also handle C99 array designators, '[ const ] ='. */
18308 else if (cp_parser_allow_gnu_extensions_p (parser)
18309 && !c_dialect_objc ()
18310 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18311 {
18312 /* In C++11, [ could start a lambda-introducer. */
18313 bool non_const = false;
18314
18315 cp_parser_parse_tentatively (parser);
18316 cp_lexer_consume_token (parser->lexer);
18317 designator = cp_parser_constant_expression (parser, true, &non_const);
18318 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18319 cp_parser_require (parser, CPP_EQ, RT_EQ);
18320 if (!cp_parser_parse_definitely (parser))
18321 designator = NULL_TREE;
18322 else if (non_const)
18323 require_potential_rvalue_constant_expression (designator);
18324 }
18325 else
18326 designator = NULL_TREE;
18327
18328 /* Parse the initializer. */
18329 initializer = cp_parser_initializer_clause (parser,
18330 &clause_non_constant_p);
18331 /* If any clause is non-constant, so is the entire initializer. */
18332 if (clause_non_constant_p)
18333 *non_constant_p = true;
18334
18335 /* If we have an ellipsis, this is an initializer pack
18336 expansion. */
18337 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18338 {
18339 /* Consume the `...'. */
18340 cp_lexer_consume_token (parser->lexer);
18341
18342 /* Turn the initializer into an initializer expansion. */
18343 initializer = make_pack_expansion (initializer);
18344 }
18345
18346 /* Add it to the vector. */
18347 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18348
18349 /* If the next token is not a comma, we have reached the end of
18350 the list. */
18351 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18352 break;
18353
18354 /* Peek at the next token. */
18355 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18356 /* If the next token is a `}', then we're still done. An
18357 initializer-clause can have a trailing `,' after the
18358 initializer-list and before the closing `}'. */
18359 if (token->type == CPP_CLOSE_BRACE)
18360 break;
18361
18362 /* Consume the `,' token. */
18363 cp_lexer_consume_token (parser->lexer);
18364 }
18365
18366 return v;
18367 }
18368
18369 /* Classes [gram.class] */
18370
18371 /* Parse a class-name.
18372
18373 class-name:
18374 identifier
18375 template-id
18376
18377 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18378 to indicate that names looked up in dependent types should be
18379 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18380 keyword has been used to indicate that the name that appears next
18381 is a template. TAG_TYPE indicates the explicit tag given before
18382 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18383 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18384 is the class being defined in a class-head.
18385
18386 Returns the TYPE_DECL representing the class. */
18387
18388 static tree
18389 cp_parser_class_name (cp_parser *parser,
18390 bool typename_keyword_p,
18391 bool template_keyword_p,
18392 enum tag_types tag_type,
18393 bool check_dependency_p,
18394 bool class_head_p,
18395 bool is_declaration)
18396 {
18397 tree decl;
18398 tree scope;
18399 bool typename_p;
18400 cp_token *token;
18401 tree identifier = NULL_TREE;
18402
18403 /* All class-names start with an identifier. */
18404 token = cp_lexer_peek_token (parser->lexer);
18405 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18406 {
18407 cp_parser_error (parser, "expected class-name");
18408 return error_mark_node;
18409 }
18410
18411 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18412 to a template-id, so we save it here. */
18413 scope = parser->scope;
18414 if (scope == error_mark_node)
18415 return error_mark_node;
18416
18417 /* Any name names a type if we're following the `typename' keyword
18418 in a qualified name where the enclosing scope is type-dependent. */
18419 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18420 && dependent_type_p (scope));
18421 /* Handle the common case (an identifier, but not a template-id)
18422 efficiently. */
18423 if (token->type == CPP_NAME
18424 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18425 {
18426 cp_token *identifier_token;
18427 bool ambiguous_p;
18428
18429 /* Look for the identifier. */
18430 identifier_token = cp_lexer_peek_token (parser->lexer);
18431 ambiguous_p = identifier_token->ambiguous_p;
18432 identifier = cp_parser_identifier (parser);
18433 /* If the next token isn't an identifier, we are certainly not
18434 looking at a class-name. */
18435 if (identifier == error_mark_node)
18436 decl = error_mark_node;
18437 /* If we know this is a type-name, there's no need to look it
18438 up. */
18439 else if (typename_p)
18440 decl = identifier;
18441 else
18442 {
18443 tree ambiguous_decls;
18444 /* If we already know that this lookup is ambiguous, then
18445 we've already issued an error message; there's no reason
18446 to check again. */
18447 if (ambiguous_p)
18448 {
18449 cp_parser_simulate_error (parser);
18450 return error_mark_node;
18451 }
18452 /* If the next token is a `::', then the name must be a type
18453 name.
18454
18455 [basic.lookup.qual]
18456
18457 During the lookup for a name preceding the :: scope
18458 resolution operator, object, function, and enumerator
18459 names are ignored. */
18460 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18461 tag_type = typename_type;
18462 /* Look up the name. */
18463 decl = cp_parser_lookup_name (parser, identifier,
18464 tag_type,
18465 /*is_template=*/false,
18466 /*is_namespace=*/false,
18467 check_dependency_p,
18468 &ambiguous_decls,
18469 identifier_token->location);
18470 if (ambiguous_decls)
18471 {
18472 if (cp_parser_parsing_tentatively (parser))
18473 cp_parser_simulate_error (parser);
18474 return error_mark_node;
18475 }
18476 }
18477 }
18478 else
18479 {
18480 /* Try a template-id. */
18481 decl = cp_parser_template_id (parser, template_keyword_p,
18482 check_dependency_p,
18483 tag_type,
18484 is_declaration);
18485 if (decl == error_mark_node)
18486 return error_mark_node;
18487 }
18488
18489 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18490
18491 /* If this is a typename, create a TYPENAME_TYPE. */
18492 if (typename_p && decl != error_mark_node)
18493 {
18494 decl = make_typename_type (scope, decl, typename_type,
18495 /*complain=*/tf_error);
18496 if (decl != error_mark_node)
18497 decl = TYPE_NAME (decl);
18498 }
18499
18500 decl = strip_using_decl (decl);
18501
18502 /* Check to see that it is really the name of a class. */
18503 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18504 && identifier_p (TREE_OPERAND (decl, 0))
18505 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18506 /* Situations like this:
18507
18508 template <typename T> struct A {
18509 typename T::template X<int>::I i;
18510 };
18511
18512 are problematic. Is `T::template X<int>' a class-name? The
18513 standard does not seem to be definitive, but there is no other
18514 valid interpretation of the following `::'. Therefore, those
18515 names are considered class-names. */
18516 {
18517 decl = make_typename_type (scope, decl, tag_type, tf_error);
18518 if (decl != error_mark_node)
18519 decl = TYPE_NAME (decl);
18520 }
18521 else if (TREE_CODE (decl) != TYPE_DECL
18522 || TREE_TYPE (decl) == error_mark_node
18523 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18524 /* In Objective-C 2.0, a classname followed by '.' starts a
18525 dot-syntax expression, and it's not a type-name. */
18526 || (c_dialect_objc ()
18527 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18528 && objc_is_class_name (decl)))
18529 decl = error_mark_node;
18530
18531 if (decl == error_mark_node)
18532 cp_parser_error (parser, "expected class-name");
18533 else if (identifier && !parser->scope)
18534 maybe_note_name_used_in_class (identifier, decl);
18535
18536 return decl;
18537 }
18538
18539 /* Parse a class-specifier.
18540
18541 class-specifier:
18542 class-head { member-specification [opt] }
18543
18544 Returns the TREE_TYPE representing the class. */
18545
18546 static tree
18547 cp_parser_class_specifier_1 (cp_parser* parser)
18548 {
18549 tree type;
18550 tree attributes = NULL_TREE;
18551 bool nested_name_specifier_p;
18552 unsigned saved_num_template_parameter_lists;
18553 bool saved_in_function_body;
18554 unsigned char in_statement;
18555 bool in_switch_statement_p;
18556 bool saved_in_unbraced_linkage_specification_p;
18557 tree old_scope = NULL_TREE;
18558 tree scope = NULL_TREE;
18559 cp_token *closing_brace;
18560
18561 push_deferring_access_checks (dk_no_deferred);
18562
18563 /* Parse the class-head. */
18564 type = cp_parser_class_head (parser,
18565 &nested_name_specifier_p);
18566 /* If the class-head was a semantic disaster, skip the entire body
18567 of the class. */
18568 if (!type)
18569 {
18570 cp_parser_skip_to_end_of_block_or_statement (parser);
18571 pop_deferring_access_checks ();
18572 return error_mark_node;
18573 }
18574
18575 /* Look for the `{'. */
18576 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18577 {
18578 pop_deferring_access_checks ();
18579 return error_mark_node;
18580 }
18581
18582 /* Issue an error message if type-definitions are forbidden here. */
18583 cp_parser_check_type_definition (parser);
18584 /* Remember that we are defining one more class. */
18585 ++parser->num_classes_being_defined;
18586 /* Inside the class, surrounding template-parameter-lists do not
18587 apply. */
18588 saved_num_template_parameter_lists
18589 = parser->num_template_parameter_lists;
18590 parser->num_template_parameter_lists = 0;
18591 /* We are not in a function body. */
18592 saved_in_function_body = parser->in_function_body;
18593 parser->in_function_body = false;
18594 /* Or in a loop. */
18595 in_statement = parser->in_statement;
18596 parser->in_statement = 0;
18597 /* Or in a switch. */
18598 in_switch_statement_p = parser->in_switch_statement_p;
18599 parser->in_switch_statement_p = false;
18600 /* We are not immediately inside an extern "lang" block. */
18601 saved_in_unbraced_linkage_specification_p
18602 = parser->in_unbraced_linkage_specification_p;
18603 parser->in_unbraced_linkage_specification_p = false;
18604
18605 /* Start the class. */
18606 if (nested_name_specifier_p)
18607 {
18608 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18609 old_scope = push_inner_scope (scope);
18610 }
18611 type = begin_class_definition (type);
18612
18613 if (type == error_mark_node)
18614 /* If the type is erroneous, skip the entire body of the class. */
18615 cp_parser_skip_to_closing_brace (parser);
18616 else
18617 /* Parse the member-specification. */
18618 cp_parser_member_specification_opt (parser);
18619
18620 /* Look for the trailing `}'. */
18621 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18622 /* Look for trailing attributes to apply to this class. */
18623 if (cp_parser_allow_gnu_extensions_p (parser))
18624 attributes = cp_parser_gnu_attributes_opt (parser);
18625 if (type != error_mark_node)
18626 type = finish_struct (type, attributes);
18627 if (nested_name_specifier_p)
18628 pop_inner_scope (old_scope, scope);
18629
18630 /* We've finished a type definition. Check for the common syntax
18631 error of forgetting a semicolon after the definition. We need to
18632 be careful, as we can't just check for not-a-semicolon and be done
18633 with it; the user might have typed:
18634
18635 class X { } c = ...;
18636 class X { } *p = ...;
18637
18638 and so forth. Instead, enumerate all the possible tokens that
18639 might follow this production; if we don't see one of them, then
18640 complain and silently insert the semicolon. */
18641 {
18642 cp_token *token = cp_lexer_peek_token (parser->lexer);
18643 bool want_semicolon = true;
18644
18645 if (cp_next_tokens_can_be_std_attribute_p (parser))
18646 /* Don't try to parse c++11 attributes here. As per the
18647 grammar, that should be a task for
18648 cp_parser_decl_specifier_seq. */
18649 want_semicolon = false;
18650
18651 switch (token->type)
18652 {
18653 case CPP_NAME:
18654 case CPP_SEMICOLON:
18655 case CPP_MULT:
18656 case CPP_AND:
18657 case CPP_OPEN_PAREN:
18658 case CPP_CLOSE_PAREN:
18659 case CPP_COMMA:
18660 want_semicolon = false;
18661 break;
18662
18663 /* While it's legal for type qualifiers and storage class
18664 specifiers to follow type definitions in the grammar, only
18665 compiler testsuites contain code like that. Assume that if
18666 we see such code, then what we're really seeing is a case
18667 like:
18668
18669 class X { }
18670 const <type> var = ...;
18671
18672 or
18673
18674 class Y { }
18675 static <type> func (...) ...
18676
18677 i.e. the qualifier or specifier applies to the next
18678 declaration. To do so, however, we need to look ahead one
18679 more token to see if *that* token is a type specifier.
18680
18681 This code could be improved to handle:
18682
18683 class Z { }
18684 static const <type> var = ...; */
18685 case CPP_KEYWORD:
18686 if (keyword_is_decl_specifier (token->keyword))
18687 {
18688 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18689
18690 /* Handling user-defined types here would be nice, but very
18691 tricky. */
18692 want_semicolon
18693 = (lookahead->type == CPP_KEYWORD
18694 && keyword_begins_type_specifier (lookahead->keyword));
18695 }
18696 break;
18697 default:
18698 break;
18699 }
18700
18701 /* If we don't have a type, then something is very wrong and we
18702 shouldn't try to do anything clever. Likewise for not seeing the
18703 closing brace. */
18704 if (closing_brace && TYPE_P (type) && want_semicolon)
18705 {
18706 cp_token_position prev
18707 = cp_lexer_previous_token_position (parser->lexer);
18708 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18709 location_t loc = prev_token->location;
18710
18711 if (CLASSTYPE_DECLARED_CLASS (type))
18712 error_at (loc, "expected %<;%> after class definition");
18713 else if (TREE_CODE (type) == RECORD_TYPE)
18714 error_at (loc, "expected %<;%> after struct definition");
18715 else if (TREE_CODE (type) == UNION_TYPE)
18716 error_at (loc, "expected %<;%> after union definition");
18717 else
18718 gcc_unreachable ();
18719
18720 /* Unget one token and smash it to look as though we encountered
18721 a semicolon in the input stream. */
18722 cp_lexer_set_token_position (parser->lexer, prev);
18723 token = cp_lexer_peek_token (parser->lexer);
18724 token->type = CPP_SEMICOLON;
18725 token->keyword = RID_MAX;
18726 }
18727 }
18728
18729 /* If this class is not itself within the scope of another class,
18730 then we need to parse the bodies of all of the queued function
18731 definitions. Note that the queued functions defined in a class
18732 are not always processed immediately following the
18733 class-specifier for that class. Consider:
18734
18735 struct A {
18736 struct B { void f() { sizeof (A); } };
18737 };
18738
18739 If `f' were processed before the processing of `A' were
18740 completed, there would be no way to compute the size of `A'.
18741 Note that the nesting we are interested in here is lexical --
18742 not the semantic nesting given by TYPE_CONTEXT. In particular,
18743 for:
18744
18745 struct A { struct B; };
18746 struct A::B { void f() { } };
18747
18748 there is no need to delay the parsing of `A::B::f'. */
18749 if (--parser->num_classes_being_defined == 0)
18750 {
18751 tree decl;
18752 tree class_type = NULL_TREE;
18753 tree pushed_scope = NULL_TREE;
18754 unsigned ix;
18755 cp_default_arg_entry *e;
18756 tree save_ccp, save_ccr;
18757
18758 /* In a first pass, parse default arguments to the functions.
18759 Then, in a second pass, parse the bodies of the functions.
18760 This two-phased approach handles cases like:
18761
18762 struct S {
18763 void f() { g(); }
18764 void g(int i = 3);
18765 };
18766
18767 */
18768 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
18769 {
18770 decl = e->decl;
18771 /* If there are default arguments that have not yet been processed,
18772 take care of them now. */
18773 if (class_type != e->class_type)
18774 {
18775 if (pushed_scope)
18776 pop_scope (pushed_scope);
18777 class_type = e->class_type;
18778 pushed_scope = push_scope (class_type);
18779 }
18780 /* Make sure that any template parameters are in scope. */
18781 maybe_begin_member_template_processing (decl);
18782 /* Parse the default argument expressions. */
18783 cp_parser_late_parsing_default_args (parser, decl);
18784 /* Remove any template parameters from the symbol table. */
18785 maybe_end_member_template_processing ();
18786 }
18787 vec_safe_truncate (unparsed_funs_with_default_args, 0);
18788 /* Now parse any NSDMIs. */
18789 save_ccp = current_class_ptr;
18790 save_ccr = current_class_ref;
18791 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
18792 {
18793 if (class_type != DECL_CONTEXT (decl))
18794 {
18795 if (pushed_scope)
18796 pop_scope (pushed_scope);
18797 class_type = DECL_CONTEXT (decl);
18798 pushed_scope = push_scope (class_type);
18799 }
18800 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18801 cp_parser_late_parsing_nsdmi (parser, decl);
18802 }
18803 vec_safe_truncate (unparsed_nsdmis, 0);
18804 current_class_ptr = save_ccp;
18805 current_class_ref = save_ccr;
18806 if (pushed_scope)
18807 pop_scope (pushed_scope);
18808 /* Now parse the body of the functions. */
18809 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
18810 cp_parser_late_parsing_for_member (parser, decl);
18811 vec_safe_truncate (unparsed_funs_with_definitions, 0);
18812 }
18813
18814 /* Put back any saved access checks. */
18815 pop_deferring_access_checks ();
18816
18817 /* Restore saved state. */
18818 parser->in_switch_statement_p = in_switch_statement_p;
18819 parser->in_statement = in_statement;
18820 parser->in_function_body = saved_in_function_body;
18821 parser->num_template_parameter_lists
18822 = saved_num_template_parameter_lists;
18823 parser->in_unbraced_linkage_specification_p
18824 = saved_in_unbraced_linkage_specification_p;
18825
18826 return type;
18827 }
18828
18829 static tree
18830 cp_parser_class_specifier (cp_parser* parser)
18831 {
18832 tree ret;
18833 timevar_push (TV_PARSE_STRUCT);
18834 ret = cp_parser_class_specifier_1 (parser);
18835 timevar_pop (TV_PARSE_STRUCT);
18836 return ret;
18837 }
18838
18839 /* Parse a class-head.
18840
18841 class-head:
18842 class-key identifier [opt] base-clause [opt]
18843 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18844 class-key nested-name-specifier [opt] template-id
18845 base-clause [opt]
18846
18847 class-virt-specifier:
18848 final
18849
18850 GNU Extensions:
18851 class-key attributes identifier [opt] base-clause [opt]
18852 class-key attributes nested-name-specifier identifier base-clause [opt]
18853 class-key attributes nested-name-specifier [opt] template-id
18854 base-clause [opt]
18855
18856 Upon return BASES is initialized to the list of base classes (or
18857 NULL, if there are none) in the same form returned by
18858 cp_parser_base_clause.
18859
18860 Returns the TYPE of the indicated class. Sets
18861 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18862 involving a nested-name-specifier was used, and FALSE otherwise.
18863
18864 Returns error_mark_node if this is not a class-head.
18865
18866 Returns NULL_TREE if the class-head is syntactically valid, but
18867 semantically invalid in a way that means we should skip the entire
18868 body of the class. */
18869
18870 static tree
18871 cp_parser_class_head (cp_parser* parser,
18872 bool* nested_name_specifier_p)
18873 {
18874 tree nested_name_specifier;
18875 enum tag_types class_key;
18876 tree id = NULL_TREE;
18877 tree type = NULL_TREE;
18878 tree attributes;
18879 tree bases;
18880 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18881 bool template_id_p = false;
18882 bool qualified_p = false;
18883 bool invalid_nested_name_p = false;
18884 bool invalid_explicit_specialization_p = false;
18885 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18886 tree pushed_scope = NULL_TREE;
18887 unsigned num_templates;
18888 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18889 /* Assume no nested-name-specifier will be present. */
18890 *nested_name_specifier_p = false;
18891 /* Assume no template parameter lists will be used in defining the
18892 type. */
18893 num_templates = 0;
18894 parser->colon_corrects_to_scope_p = false;
18895
18896 /* Look for the class-key. */
18897 class_key = cp_parser_class_key (parser);
18898 if (class_key == none_type)
18899 return error_mark_node;
18900
18901 /* Parse the attributes. */
18902 attributes = cp_parser_attributes_opt (parser);
18903
18904 /* If the next token is `::', that is invalid -- but sometimes
18905 people do try to write:
18906
18907 struct ::S {};
18908
18909 Handle this gracefully by accepting the extra qualifier, and then
18910 issuing an error about it later if this really is a
18911 class-head. If it turns out just to be an elaborated type
18912 specifier, remain silent. */
18913 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18914 qualified_p = true;
18915
18916 push_deferring_access_checks (dk_no_check);
18917
18918 /* Determine the name of the class. Begin by looking for an
18919 optional nested-name-specifier. */
18920 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18921 nested_name_specifier
18922 = cp_parser_nested_name_specifier_opt (parser,
18923 /*typename_keyword_p=*/false,
18924 /*check_dependency_p=*/false,
18925 /*type_p=*/true,
18926 /*is_declaration=*/false);
18927 /* If there was a nested-name-specifier, then there *must* be an
18928 identifier. */
18929 if (nested_name_specifier)
18930 {
18931 type_start_token = cp_lexer_peek_token (parser->lexer);
18932 /* Although the grammar says `identifier', it really means
18933 `class-name' or `template-name'. You are only allowed to
18934 define a class that has already been declared with this
18935 syntax.
18936
18937 The proposed resolution for Core Issue 180 says that wherever
18938 you see `class T::X' you should treat `X' as a type-name.
18939
18940 It is OK to define an inaccessible class; for example:
18941
18942 class A { class B; };
18943 class A::B {};
18944
18945 We do not know if we will see a class-name, or a
18946 template-name. We look for a class-name first, in case the
18947 class-name is a template-id; if we looked for the
18948 template-name first we would stop after the template-name. */
18949 cp_parser_parse_tentatively (parser);
18950 type = cp_parser_class_name (parser,
18951 /*typename_keyword_p=*/false,
18952 /*template_keyword_p=*/false,
18953 class_type,
18954 /*check_dependency_p=*/false,
18955 /*class_head_p=*/true,
18956 /*is_declaration=*/false);
18957 /* If that didn't work, ignore the nested-name-specifier. */
18958 if (!cp_parser_parse_definitely (parser))
18959 {
18960 invalid_nested_name_p = true;
18961 type_start_token = cp_lexer_peek_token (parser->lexer);
18962 id = cp_parser_identifier (parser);
18963 if (id == error_mark_node)
18964 id = NULL_TREE;
18965 }
18966 /* If we could not find a corresponding TYPE, treat this
18967 declaration like an unqualified declaration. */
18968 if (type == error_mark_node)
18969 nested_name_specifier = NULL_TREE;
18970 /* Otherwise, count the number of templates used in TYPE and its
18971 containing scopes. */
18972 else
18973 {
18974 tree scope;
18975
18976 for (scope = TREE_TYPE (type);
18977 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18978 scope = get_containing_scope (scope))
18979 if (TYPE_P (scope)
18980 && CLASS_TYPE_P (scope)
18981 && CLASSTYPE_TEMPLATE_INFO (scope)
18982 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18983 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
18984 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
18985 ++num_templates;
18986 }
18987 }
18988 /* Otherwise, the identifier is optional. */
18989 else
18990 {
18991 /* We don't know whether what comes next is a template-id,
18992 an identifier, or nothing at all. */
18993 cp_parser_parse_tentatively (parser);
18994 /* Check for a template-id. */
18995 type_start_token = cp_lexer_peek_token (parser->lexer);
18996 id = cp_parser_template_id (parser,
18997 /*template_keyword_p=*/false,
18998 /*check_dependency_p=*/true,
18999 class_key,
19000 /*is_declaration=*/true);
19001 /* If that didn't work, it could still be an identifier. */
19002 if (!cp_parser_parse_definitely (parser))
19003 {
19004 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19005 {
19006 type_start_token = cp_lexer_peek_token (parser->lexer);
19007 id = cp_parser_identifier (parser);
19008 }
19009 else
19010 id = NULL_TREE;
19011 }
19012 else
19013 {
19014 template_id_p = true;
19015 ++num_templates;
19016 }
19017 }
19018
19019 pop_deferring_access_checks ();
19020
19021 if (id)
19022 {
19023 cp_parser_check_for_invalid_template_id (parser, id,
19024 class_key,
19025 type_start_token->location);
19026 }
19027 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19028
19029 /* If it's not a `:' or a `{' then we can't really be looking at a
19030 class-head, since a class-head only appears as part of a
19031 class-specifier. We have to detect this situation before calling
19032 xref_tag, since that has irreversible side-effects. */
19033 if (!cp_parser_next_token_starts_class_definition_p (parser))
19034 {
19035 cp_parser_error (parser, "expected %<{%> or %<:%>");
19036 type = error_mark_node;
19037 goto out;
19038 }
19039
19040 /* At this point, we're going ahead with the class-specifier, even
19041 if some other problem occurs. */
19042 cp_parser_commit_to_tentative_parse (parser);
19043 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19044 {
19045 cp_parser_error (parser,
19046 "cannot specify %<override%> for a class");
19047 type = error_mark_node;
19048 goto out;
19049 }
19050 /* Issue the error about the overly-qualified name now. */
19051 if (qualified_p)
19052 {
19053 cp_parser_error (parser,
19054 "global qualification of class name is invalid");
19055 type = error_mark_node;
19056 goto out;
19057 }
19058 else if (invalid_nested_name_p)
19059 {
19060 cp_parser_error (parser,
19061 "qualified name does not name a class");
19062 type = error_mark_node;
19063 goto out;
19064 }
19065 else if (nested_name_specifier)
19066 {
19067 tree scope;
19068
19069 /* Reject typedef-names in class heads. */
19070 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19071 {
19072 error_at (type_start_token->location,
19073 "invalid class name in declaration of %qD",
19074 type);
19075 type = NULL_TREE;
19076 goto done;
19077 }
19078
19079 /* Figure out in what scope the declaration is being placed. */
19080 scope = current_scope ();
19081 /* If that scope does not contain the scope in which the
19082 class was originally declared, the program is invalid. */
19083 if (scope && !is_ancestor (scope, nested_name_specifier))
19084 {
19085 if (at_namespace_scope_p ())
19086 error_at (type_start_token->location,
19087 "declaration of %qD in namespace %qD which does not "
19088 "enclose %qD",
19089 type, scope, nested_name_specifier);
19090 else
19091 error_at (type_start_token->location,
19092 "declaration of %qD in %qD which does not enclose %qD",
19093 type, scope, nested_name_specifier);
19094 type = NULL_TREE;
19095 goto done;
19096 }
19097 /* [dcl.meaning]
19098
19099 A declarator-id shall not be qualified except for the
19100 definition of a ... nested class outside of its class
19101 ... [or] the definition or explicit instantiation of a
19102 class member of a namespace outside of its namespace. */
19103 if (scope == nested_name_specifier)
19104 {
19105 permerror (nested_name_specifier_token_start->location,
19106 "extra qualification not allowed");
19107 nested_name_specifier = NULL_TREE;
19108 num_templates = 0;
19109 }
19110 }
19111 /* An explicit-specialization must be preceded by "template <>". If
19112 it is not, try to recover gracefully. */
19113 if (at_namespace_scope_p ()
19114 && parser->num_template_parameter_lists == 0
19115 && template_id_p)
19116 {
19117 error_at (type_start_token->location,
19118 "an explicit specialization must be preceded by %<template <>%>");
19119 invalid_explicit_specialization_p = true;
19120 /* Take the same action that would have been taken by
19121 cp_parser_explicit_specialization. */
19122 ++parser->num_template_parameter_lists;
19123 begin_specialization ();
19124 }
19125 /* There must be no "return" statements between this point and the
19126 end of this function; set "type "to the correct return value and
19127 use "goto done;" to return. */
19128 /* Make sure that the right number of template parameters were
19129 present. */
19130 if (!cp_parser_check_template_parameters (parser, num_templates,
19131 type_start_token->location,
19132 /*declarator=*/NULL))
19133 {
19134 /* If something went wrong, there is no point in even trying to
19135 process the class-definition. */
19136 type = NULL_TREE;
19137 goto done;
19138 }
19139
19140 /* Look up the type. */
19141 if (template_id_p)
19142 {
19143 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19144 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19145 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19146 {
19147 error_at (type_start_token->location,
19148 "function template %qD redeclared as a class template", id);
19149 type = error_mark_node;
19150 }
19151 else
19152 {
19153 type = TREE_TYPE (id);
19154 type = maybe_process_partial_specialization (type);
19155 }
19156 if (nested_name_specifier)
19157 pushed_scope = push_scope (nested_name_specifier);
19158 }
19159 else if (nested_name_specifier)
19160 {
19161 tree class_type;
19162
19163 /* Given:
19164
19165 template <typename T> struct S { struct T };
19166 template <typename T> struct S<T>::T { };
19167
19168 we will get a TYPENAME_TYPE when processing the definition of
19169 `S::T'. We need to resolve it to the actual type before we
19170 try to define it. */
19171 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19172 {
19173 class_type = resolve_typename_type (TREE_TYPE (type),
19174 /*only_current_p=*/false);
19175 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19176 type = TYPE_NAME (class_type);
19177 else
19178 {
19179 cp_parser_error (parser, "could not resolve typename type");
19180 type = error_mark_node;
19181 }
19182 }
19183
19184 if (maybe_process_partial_specialization (TREE_TYPE (type))
19185 == error_mark_node)
19186 {
19187 type = NULL_TREE;
19188 goto done;
19189 }
19190
19191 class_type = current_class_type;
19192 /* Enter the scope indicated by the nested-name-specifier. */
19193 pushed_scope = push_scope (nested_name_specifier);
19194 /* Get the canonical version of this type. */
19195 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19196 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19197 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19198 {
19199 type = push_template_decl (type);
19200 if (type == error_mark_node)
19201 {
19202 type = NULL_TREE;
19203 goto done;
19204 }
19205 }
19206
19207 type = TREE_TYPE (type);
19208 *nested_name_specifier_p = true;
19209 }
19210 else /* The name is not a nested name. */
19211 {
19212 /* If the class was unnamed, create a dummy name. */
19213 if (!id)
19214 id = make_anon_name ();
19215 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19216 parser->num_template_parameter_lists);
19217 }
19218
19219 /* Indicate whether this class was declared as a `class' or as a
19220 `struct'. */
19221 if (TREE_CODE (type) == RECORD_TYPE)
19222 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19223 cp_parser_check_class_key (class_key, type);
19224
19225 /* If this type was already complete, and we see another definition,
19226 that's an error. */
19227 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19228 {
19229 error_at (type_start_token->location, "redefinition of %q#T",
19230 type);
19231 error_at (type_start_token->location, "previous definition of %q+#T",
19232 type);
19233 type = NULL_TREE;
19234 goto done;
19235 }
19236 else if (type == error_mark_node)
19237 type = NULL_TREE;
19238
19239 if (type)
19240 {
19241 /* Apply attributes now, before any use of the class as a template
19242 argument in its base list. */
19243 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19244 fixup_attribute_variants (type);
19245 }
19246
19247 /* We will have entered the scope containing the class; the names of
19248 base classes should be looked up in that context. For example:
19249
19250 struct A { struct B {}; struct C; };
19251 struct A::C : B {};
19252
19253 is valid. */
19254
19255 /* Get the list of base-classes, if there is one. */
19256 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19257 bases = cp_parser_base_clause (parser);
19258 else
19259 bases = NULL_TREE;
19260
19261 /* If we're really defining a class, process the base classes.
19262 If they're invalid, fail. */
19263 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19264 && !xref_basetypes (type, bases))
19265 type = NULL_TREE;
19266
19267 done:
19268 /* Leave the scope given by the nested-name-specifier. We will
19269 enter the class scope itself while processing the members. */
19270 if (pushed_scope)
19271 pop_scope (pushed_scope);
19272
19273 if (invalid_explicit_specialization_p)
19274 {
19275 end_specialization ();
19276 --parser->num_template_parameter_lists;
19277 }
19278
19279 if (type)
19280 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19281 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19282 CLASSTYPE_FINAL (type) = 1;
19283 out:
19284 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19285 return type;
19286 }
19287
19288 /* Parse a class-key.
19289
19290 class-key:
19291 class
19292 struct
19293 union
19294
19295 Returns the kind of class-key specified, or none_type to indicate
19296 error. */
19297
19298 static enum tag_types
19299 cp_parser_class_key (cp_parser* parser)
19300 {
19301 cp_token *token;
19302 enum tag_types tag_type;
19303
19304 /* Look for the class-key. */
19305 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19306 if (!token)
19307 return none_type;
19308
19309 /* Check to see if the TOKEN is a class-key. */
19310 tag_type = cp_parser_token_is_class_key (token);
19311 if (!tag_type)
19312 cp_parser_error (parser, "expected class-key");
19313 return tag_type;
19314 }
19315
19316 /* Parse an (optional) member-specification.
19317
19318 member-specification:
19319 member-declaration member-specification [opt]
19320 access-specifier : member-specification [opt] */
19321
19322 static void
19323 cp_parser_member_specification_opt (cp_parser* parser)
19324 {
19325 while (true)
19326 {
19327 cp_token *token;
19328 enum rid keyword;
19329
19330 /* Peek at the next token. */
19331 token = cp_lexer_peek_token (parser->lexer);
19332 /* If it's a `}', or EOF then we've seen all the members. */
19333 if (token->type == CPP_CLOSE_BRACE
19334 || token->type == CPP_EOF
19335 || token->type == CPP_PRAGMA_EOL)
19336 break;
19337
19338 /* See if this token is a keyword. */
19339 keyword = token->keyword;
19340 switch (keyword)
19341 {
19342 case RID_PUBLIC:
19343 case RID_PROTECTED:
19344 case RID_PRIVATE:
19345 /* Consume the access-specifier. */
19346 cp_lexer_consume_token (parser->lexer);
19347 /* Remember which access-specifier is active. */
19348 current_access_specifier = token->u.value;
19349 /* Look for the `:'. */
19350 cp_parser_require (parser, CPP_COLON, RT_COLON);
19351 break;
19352
19353 default:
19354 /* Accept #pragmas at class scope. */
19355 if (token->type == CPP_PRAGMA)
19356 {
19357 cp_parser_pragma (parser, pragma_external);
19358 break;
19359 }
19360
19361 /* Otherwise, the next construction must be a
19362 member-declaration. */
19363 cp_parser_member_declaration (parser);
19364 }
19365 }
19366 }
19367
19368 /* Parse a member-declaration.
19369
19370 member-declaration:
19371 decl-specifier-seq [opt] member-declarator-list [opt] ;
19372 function-definition ; [opt]
19373 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19374 using-declaration
19375 template-declaration
19376 alias-declaration
19377
19378 member-declarator-list:
19379 member-declarator
19380 member-declarator-list , member-declarator
19381
19382 member-declarator:
19383 declarator pure-specifier [opt]
19384 declarator constant-initializer [opt]
19385 identifier [opt] : constant-expression
19386
19387 GNU Extensions:
19388
19389 member-declaration:
19390 __extension__ member-declaration
19391
19392 member-declarator:
19393 declarator attributes [opt] pure-specifier [opt]
19394 declarator attributes [opt] constant-initializer [opt]
19395 identifier [opt] attributes [opt] : constant-expression
19396
19397 C++0x Extensions:
19398
19399 member-declaration:
19400 static_assert-declaration */
19401
19402 static void
19403 cp_parser_member_declaration (cp_parser* parser)
19404 {
19405 cp_decl_specifier_seq decl_specifiers;
19406 tree prefix_attributes;
19407 tree decl;
19408 int declares_class_or_enum;
19409 bool friend_p;
19410 cp_token *token = NULL;
19411 cp_token *decl_spec_token_start = NULL;
19412 cp_token *initializer_token_start = NULL;
19413 int saved_pedantic;
19414 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19415
19416 /* Check for the `__extension__' keyword. */
19417 if (cp_parser_extension_opt (parser, &saved_pedantic))
19418 {
19419 /* Recurse. */
19420 cp_parser_member_declaration (parser);
19421 /* Restore the old value of the PEDANTIC flag. */
19422 pedantic = saved_pedantic;
19423
19424 return;
19425 }
19426
19427 /* Check for a template-declaration. */
19428 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19429 {
19430 /* An explicit specialization here is an error condition, and we
19431 expect the specialization handler to detect and report this. */
19432 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19433 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19434 cp_parser_explicit_specialization (parser);
19435 else
19436 cp_parser_template_declaration (parser, /*member_p=*/true);
19437
19438 return;
19439 }
19440
19441 /* Check for a using-declaration. */
19442 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19443 {
19444 if (cxx_dialect < cxx0x)
19445 {
19446 /* Parse the using-declaration. */
19447 cp_parser_using_declaration (parser,
19448 /*access_declaration_p=*/false);
19449 return;
19450 }
19451 else
19452 {
19453 tree decl;
19454 bool alias_decl_expected;
19455 cp_parser_parse_tentatively (parser);
19456 decl = cp_parser_alias_declaration (parser);
19457 /* Note that if we actually see the '=' token after the
19458 identifier, cp_parser_alias_declaration commits the
19459 tentative parse. In that case, we really expects an
19460 alias-declaration. Otherwise, we expect a using
19461 declaration. */
19462 alias_decl_expected =
19463 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19464 cp_parser_parse_definitely (parser);
19465
19466 if (alias_decl_expected)
19467 finish_member_declaration (decl);
19468 else
19469 cp_parser_using_declaration (parser,
19470 /*access_declaration_p=*/false);
19471 return;
19472 }
19473 }
19474
19475 /* Check for @defs. */
19476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19477 {
19478 tree ivar, member;
19479 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19480 ivar = ivar_chains;
19481 while (ivar)
19482 {
19483 member = ivar;
19484 ivar = TREE_CHAIN (member);
19485 TREE_CHAIN (member) = NULL_TREE;
19486 finish_member_declaration (member);
19487 }
19488 return;
19489 }
19490
19491 /* If the next token is `static_assert' we have a static assertion. */
19492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19493 {
19494 cp_parser_static_assert (parser, /*member_p=*/true);
19495 return;
19496 }
19497
19498 parser->colon_corrects_to_scope_p = false;
19499
19500 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19501 goto out;
19502
19503 /* Parse the decl-specifier-seq. */
19504 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19505 cp_parser_decl_specifier_seq (parser,
19506 CP_PARSER_FLAGS_OPTIONAL,
19507 &decl_specifiers,
19508 &declares_class_or_enum);
19509 /* Check for an invalid type-name. */
19510 if (!decl_specifiers.any_type_specifiers_p
19511 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19512 goto out;
19513 /* If there is no declarator, then the decl-specifier-seq should
19514 specify a type. */
19515 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19516 {
19517 /* If there was no decl-specifier-seq, and the next token is a
19518 `;', then we have something like:
19519
19520 struct S { ; };
19521
19522 [class.mem]
19523
19524 Each member-declaration shall declare at least one member
19525 name of the class. */
19526 if (!decl_specifiers.any_specifiers_p)
19527 {
19528 cp_token *token = cp_lexer_peek_token (parser->lexer);
19529 if (!in_system_header_at (token->location))
19530 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19531 }
19532 else
19533 {
19534 tree type;
19535
19536 /* See if this declaration is a friend. */
19537 friend_p = cp_parser_friend_p (&decl_specifiers);
19538 /* If there were decl-specifiers, check to see if there was
19539 a class-declaration. */
19540 type = check_tag_decl (&decl_specifiers,
19541 /*explicit_type_instantiation_p=*/false);
19542 /* Nested classes have already been added to the class, but
19543 a `friend' needs to be explicitly registered. */
19544 if (friend_p)
19545 {
19546 /* If the `friend' keyword was present, the friend must
19547 be introduced with a class-key. */
19548 if (!declares_class_or_enum && cxx_dialect < cxx0x)
19549 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19550 "in C++03 a class-key must be used "
19551 "when declaring a friend");
19552 /* In this case:
19553
19554 template <typename T> struct A {
19555 friend struct A<T>::B;
19556 };
19557
19558 A<T>::B will be represented by a TYPENAME_TYPE, and
19559 therefore not recognized by check_tag_decl. */
19560 if (!type)
19561 {
19562 type = decl_specifiers.type;
19563 if (type && TREE_CODE (type) == TYPE_DECL)
19564 type = TREE_TYPE (type);
19565 }
19566 if (!type || !TYPE_P (type))
19567 error_at (decl_spec_token_start->location,
19568 "friend declaration does not name a class or "
19569 "function");
19570 else
19571 make_friend_class (current_class_type, type,
19572 /*complain=*/true);
19573 }
19574 /* If there is no TYPE, an error message will already have
19575 been issued. */
19576 else if (!type || type == error_mark_node)
19577 ;
19578 /* An anonymous aggregate has to be handled specially; such
19579 a declaration really declares a data member (with a
19580 particular type), as opposed to a nested class. */
19581 else if (ANON_AGGR_TYPE_P (type))
19582 {
19583 /* C++11 9.5/6. */
19584 if (decl_specifiers.storage_class != sc_none)
19585 error_at (decl_spec_token_start->location,
19586 "a storage class on an anonymous aggregate "
19587 "in class scope is not allowed");
19588
19589 /* Remove constructors and such from TYPE, now that we
19590 know it is an anonymous aggregate. */
19591 fixup_anonymous_aggr (type);
19592 /* And make the corresponding data member. */
19593 decl = build_decl (decl_spec_token_start->location,
19594 FIELD_DECL, NULL_TREE, type);
19595 /* Add it to the class. */
19596 finish_member_declaration (decl);
19597 }
19598 else
19599 cp_parser_check_access_in_redeclaration
19600 (TYPE_NAME (type),
19601 decl_spec_token_start->location);
19602 }
19603 }
19604 else
19605 {
19606 bool assume_semicolon = false;
19607
19608 /* Clear attributes from the decl_specifiers but keep them
19609 around as prefix attributes that apply them to the entity
19610 being declared. */
19611 prefix_attributes = decl_specifiers.attributes;
19612 decl_specifiers.attributes = NULL_TREE;
19613
19614 /* See if these declarations will be friends. */
19615 friend_p = cp_parser_friend_p (&decl_specifiers);
19616
19617 /* Keep going until we hit the `;' at the end of the
19618 declaration. */
19619 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19620 {
19621 tree attributes = NULL_TREE;
19622 tree first_attribute;
19623
19624 /* Peek at the next token. */
19625 token = cp_lexer_peek_token (parser->lexer);
19626
19627 /* Check for a bitfield declaration. */
19628 if (token->type == CPP_COLON
19629 || (token->type == CPP_NAME
19630 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19631 == CPP_COLON))
19632 {
19633 tree identifier;
19634 tree width;
19635
19636 /* Get the name of the bitfield. Note that we cannot just
19637 check TOKEN here because it may have been invalidated by
19638 the call to cp_lexer_peek_nth_token above. */
19639 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19640 identifier = cp_parser_identifier (parser);
19641 else
19642 identifier = NULL_TREE;
19643
19644 /* Consume the `:' token. */
19645 cp_lexer_consume_token (parser->lexer);
19646 /* Get the width of the bitfield. */
19647 width
19648 = cp_parser_constant_expression (parser,
19649 /*allow_non_constant=*/false,
19650 NULL);
19651
19652 /* Look for attributes that apply to the bitfield. */
19653 attributes = cp_parser_attributes_opt (parser);
19654 /* Remember which attributes are prefix attributes and
19655 which are not. */
19656 first_attribute = attributes;
19657 /* Combine the attributes. */
19658 attributes = chainon (prefix_attributes, attributes);
19659
19660 /* Create the bitfield declaration. */
19661 decl = grokbitfield (identifier
19662 ? make_id_declarator (NULL_TREE,
19663 identifier,
19664 sfk_none)
19665 : NULL,
19666 &decl_specifiers,
19667 width,
19668 attributes);
19669 }
19670 else
19671 {
19672 cp_declarator *declarator;
19673 tree initializer;
19674 tree asm_specification;
19675 int ctor_dtor_or_conv_p;
19676
19677 /* Parse the declarator. */
19678 declarator
19679 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19680 &ctor_dtor_or_conv_p,
19681 /*parenthesized_p=*/NULL,
19682 /*member_p=*/true);
19683
19684 /* If something went wrong parsing the declarator, make sure
19685 that we at least consume some tokens. */
19686 if (declarator == cp_error_declarator)
19687 {
19688 /* Skip to the end of the statement. */
19689 cp_parser_skip_to_end_of_statement (parser);
19690 /* If the next token is not a semicolon, that is
19691 probably because we just skipped over the body of
19692 a function. So, we consume a semicolon if
19693 present, but do not issue an error message if it
19694 is not present. */
19695 if (cp_lexer_next_token_is (parser->lexer,
19696 CPP_SEMICOLON))
19697 cp_lexer_consume_token (parser->lexer);
19698 goto out;
19699 }
19700
19701 if (declares_class_or_enum & 2)
19702 cp_parser_check_for_definition_in_return_type
19703 (declarator, decl_specifiers.type,
19704 decl_specifiers.locations[ds_type_spec]);
19705
19706 /* Look for an asm-specification. */
19707 asm_specification = cp_parser_asm_specification_opt (parser);
19708 /* Look for attributes that apply to the declaration. */
19709 attributes = cp_parser_attributes_opt (parser);
19710 /* Remember which attributes are prefix attributes and
19711 which are not. */
19712 first_attribute = attributes;
19713 /* Combine the attributes. */
19714 attributes = chainon (prefix_attributes, attributes);
19715
19716 /* If it's an `=', then we have a constant-initializer or a
19717 pure-specifier. It is not correct to parse the
19718 initializer before registering the member declaration
19719 since the member declaration should be in scope while
19720 its initializer is processed. However, the rest of the
19721 front end does not yet provide an interface that allows
19722 us to handle this correctly. */
19723 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19724 {
19725 /* In [class.mem]:
19726
19727 A pure-specifier shall be used only in the declaration of
19728 a virtual function.
19729
19730 A member-declarator can contain a constant-initializer
19731 only if it declares a static member of integral or
19732 enumeration type.
19733
19734 Therefore, if the DECLARATOR is for a function, we look
19735 for a pure-specifier; otherwise, we look for a
19736 constant-initializer. When we call `grokfield', it will
19737 perform more stringent semantics checks. */
19738 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19739 if (function_declarator_p (declarator)
19740 || (decl_specifiers.type
19741 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19742 && declarator->kind == cdk_id
19743 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19744 == FUNCTION_TYPE)))
19745 initializer = cp_parser_pure_specifier (parser);
19746 else if (decl_specifiers.storage_class != sc_static)
19747 initializer = cp_parser_save_nsdmi (parser);
19748 else if (cxx_dialect >= cxx0x)
19749 {
19750 bool nonconst;
19751 /* Don't require a constant rvalue in C++11, since we
19752 might want a reference constant. We'll enforce
19753 constancy later. */
19754 cp_lexer_consume_token (parser->lexer);
19755 /* Parse the initializer. */
19756 initializer = cp_parser_initializer_clause (parser,
19757 &nonconst);
19758 }
19759 else
19760 /* Parse the initializer. */
19761 initializer = cp_parser_constant_initializer (parser);
19762 }
19763 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19764 && !function_declarator_p (declarator))
19765 {
19766 bool x;
19767 if (decl_specifiers.storage_class != sc_static)
19768 initializer = cp_parser_save_nsdmi (parser);
19769 else
19770 initializer = cp_parser_initializer (parser, &x, &x);
19771 }
19772 /* Otherwise, there is no initializer. */
19773 else
19774 initializer = NULL_TREE;
19775
19776 /* See if we are probably looking at a function
19777 definition. We are certainly not looking at a
19778 member-declarator. Calling `grokfield' has
19779 side-effects, so we must not do it unless we are sure
19780 that we are looking at a member-declarator. */
19781 if (cp_parser_token_starts_function_definition_p
19782 (cp_lexer_peek_token (parser->lexer)))
19783 {
19784 /* The grammar does not allow a pure-specifier to be
19785 used when a member function is defined. (It is
19786 possible that this fact is an oversight in the
19787 standard, since a pure function may be defined
19788 outside of the class-specifier. */
19789 if (initializer && initializer_token_start)
19790 error_at (initializer_token_start->location,
19791 "pure-specifier on function-definition");
19792 decl = cp_parser_save_member_function_body (parser,
19793 &decl_specifiers,
19794 declarator,
19795 attributes);
19796 /* If the member was not a friend, declare it here. */
19797 if (!friend_p)
19798 finish_member_declaration (decl);
19799 /* Peek at the next token. */
19800 token = cp_lexer_peek_token (parser->lexer);
19801 /* If the next token is a semicolon, consume it. */
19802 if (token->type == CPP_SEMICOLON)
19803 cp_lexer_consume_token (parser->lexer);
19804 goto out;
19805 }
19806 else
19807 if (declarator->kind == cdk_function)
19808 declarator->id_loc = token->location;
19809 /* Create the declaration. */
19810 decl = grokfield (declarator, &decl_specifiers,
19811 initializer, /*init_const_expr_p=*/true,
19812 asm_specification,
19813 attributes);
19814 }
19815
19816 /* Reset PREFIX_ATTRIBUTES. */
19817 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19818 attributes = TREE_CHAIN (attributes);
19819 if (attributes)
19820 TREE_CHAIN (attributes) = NULL_TREE;
19821
19822 /* If there is any qualification still in effect, clear it
19823 now; we will be starting fresh with the next declarator. */
19824 parser->scope = NULL_TREE;
19825 parser->qualifying_scope = NULL_TREE;
19826 parser->object_scope = NULL_TREE;
19827 /* If it's a `,', then there are more declarators. */
19828 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19829 {
19830 cp_lexer_consume_token (parser->lexer);
19831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19832 {
19833 cp_token *token = cp_lexer_previous_token (parser->lexer);
19834 error_at (token->location,
19835 "stray %<,%> at end of member declaration");
19836 }
19837 }
19838 /* If the next token isn't a `;', then we have a parse error. */
19839 else if (cp_lexer_next_token_is_not (parser->lexer,
19840 CPP_SEMICOLON))
19841 {
19842 /* The next token might be a ways away from where the
19843 actual semicolon is missing. Find the previous token
19844 and use that for our error position. */
19845 cp_token *token = cp_lexer_previous_token (parser->lexer);
19846 error_at (token->location,
19847 "expected %<;%> at end of member declaration");
19848
19849 /* Assume that the user meant to provide a semicolon. If
19850 we were to cp_parser_skip_to_end_of_statement, we might
19851 skip to a semicolon inside a member function definition
19852 and issue nonsensical error messages. */
19853 assume_semicolon = true;
19854 }
19855
19856 if (decl)
19857 {
19858 /* Add DECL to the list of members. */
19859 if (!friend_p)
19860 finish_member_declaration (decl);
19861
19862 if (TREE_CODE (decl) == FUNCTION_DECL)
19863 cp_parser_save_default_args (parser, decl);
19864 else if (TREE_CODE (decl) == FIELD_DECL
19865 && !DECL_C_BIT_FIELD (decl)
19866 && DECL_INITIAL (decl))
19867 /* Add DECL to the queue of NSDMI to be parsed later. */
19868 vec_safe_push (unparsed_nsdmis, decl);
19869 }
19870
19871 if (assume_semicolon)
19872 goto out;
19873 }
19874 }
19875
19876 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19877 out:
19878 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19879 }
19880
19881 /* Parse a pure-specifier.
19882
19883 pure-specifier:
19884 = 0
19885
19886 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19887 Otherwise, ERROR_MARK_NODE is returned. */
19888
19889 static tree
19890 cp_parser_pure_specifier (cp_parser* parser)
19891 {
19892 cp_token *token;
19893
19894 /* Look for the `=' token. */
19895 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19896 return error_mark_node;
19897 /* Look for the `0' token. */
19898 token = cp_lexer_peek_token (parser->lexer);
19899
19900 if (token->type == CPP_EOF
19901 || token->type == CPP_PRAGMA_EOL)
19902 return error_mark_node;
19903
19904 cp_lexer_consume_token (parser->lexer);
19905
19906 /* Accept = default or = delete in c++0x mode. */
19907 if (token->keyword == RID_DEFAULT
19908 || token->keyword == RID_DELETE)
19909 {
19910 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19911 return token->u.value;
19912 }
19913
19914 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19915 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19916 {
19917 cp_parser_error (parser,
19918 "invalid pure specifier (only %<= 0%> is allowed)");
19919 cp_parser_skip_to_end_of_statement (parser);
19920 return error_mark_node;
19921 }
19922 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19923 {
19924 error_at (token->location, "templates may not be %<virtual%>");
19925 return error_mark_node;
19926 }
19927
19928 return integer_zero_node;
19929 }
19930
19931 /* Parse a constant-initializer.
19932
19933 constant-initializer:
19934 = constant-expression
19935
19936 Returns a representation of the constant-expression. */
19937
19938 static tree
19939 cp_parser_constant_initializer (cp_parser* parser)
19940 {
19941 /* Look for the `=' token. */
19942 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19943 return error_mark_node;
19944
19945 /* It is invalid to write:
19946
19947 struct S { static const int i = { 7 }; };
19948
19949 */
19950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19951 {
19952 cp_parser_error (parser,
19953 "a brace-enclosed initializer is not allowed here");
19954 /* Consume the opening brace. */
19955 cp_lexer_consume_token (parser->lexer);
19956 /* Skip the initializer. */
19957 cp_parser_skip_to_closing_brace (parser);
19958 /* Look for the trailing `}'. */
19959 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19960
19961 return error_mark_node;
19962 }
19963
19964 return cp_parser_constant_expression (parser,
19965 /*allow_non_constant=*/false,
19966 NULL);
19967 }
19968
19969 /* Derived classes [gram.class.derived] */
19970
19971 /* Parse a base-clause.
19972
19973 base-clause:
19974 : base-specifier-list
19975
19976 base-specifier-list:
19977 base-specifier ... [opt]
19978 base-specifier-list , base-specifier ... [opt]
19979
19980 Returns a TREE_LIST representing the base-classes, in the order in
19981 which they were declared. The representation of each node is as
19982 described by cp_parser_base_specifier.
19983
19984 In the case that no bases are specified, this function will return
19985 NULL_TREE, not ERROR_MARK_NODE. */
19986
19987 static tree
19988 cp_parser_base_clause (cp_parser* parser)
19989 {
19990 tree bases = NULL_TREE;
19991
19992 /* Look for the `:' that begins the list. */
19993 cp_parser_require (parser, CPP_COLON, RT_COLON);
19994
19995 /* Scan the base-specifier-list. */
19996 while (true)
19997 {
19998 cp_token *token;
19999 tree base;
20000 bool pack_expansion_p = false;
20001
20002 /* Look for the base-specifier. */
20003 base = cp_parser_base_specifier (parser);
20004 /* Look for the (optional) ellipsis. */
20005 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20006 {
20007 /* Consume the `...'. */
20008 cp_lexer_consume_token (parser->lexer);
20009
20010 pack_expansion_p = true;
20011 }
20012
20013 /* Add BASE to the front of the list. */
20014 if (base && base != error_mark_node)
20015 {
20016 if (pack_expansion_p)
20017 /* Make this a pack expansion type. */
20018 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20019
20020 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20021 {
20022 TREE_CHAIN (base) = bases;
20023 bases = base;
20024 }
20025 }
20026 /* Peek at the next token. */
20027 token = cp_lexer_peek_token (parser->lexer);
20028 /* If it's not a comma, then the list is complete. */
20029 if (token->type != CPP_COMMA)
20030 break;
20031 /* Consume the `,'. */
20032 cp_lexer_consume_token (parser->lexer);
20033 }
20034
20035 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20036 base class had a qualified name. However, the next name that
20037 appears is certainly not qualified. */
20038 parser->scope = NULL_TREE;
20039 parser->qualifying_scope = NULL_TREE;
20040 parser->object_scope = NULL_TREE;
20041
20042 return nreverse (bases);
20043 }
20044
20045 /* Parse a base-specifier.
20046
20047 base-specifier:
20048 :: [opt] nested-name-specifier [opt] class-name
20049 virtual access-specifier [opt] :: [opt] nested-name-specifier
20050 [opt] class-name
20051 access-specifier virtual [opt] :: [opt] nested-name-specifier
20052 [opt] class-name
20053
20054 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20055 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20056 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20057 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20058
20059 static tree
20060 cp_parser_base_specifier (cp_parser* parser)
20061 {
20062 cp_token *token;
20063 bool done = false;
20064 bool virtual_p = false;
20065 bool duplicate_virtual_error_issued_p = false;
20066 bool duplicate_access_error_issued_p = false;
20067 bool class_scope_p, template_p;
20068 tree access = access_default_node;
20069 tree type;
20070
20071 /* Process the optional `virtual' and `access-specifier'. */
20072 while (!done)
20073 {
20074 /* Peek at the next token. */
20075 token = cp_lexer_peek_token (parser->lexer);
20076 /* Process `virtual'. */
20077 switch (token->keyword)
20078 {
20079 case RID_VIRTUAL:
20080 /* If `virtual' appears more than once, issue an error. */
20081 if (virtual_p && !duplicate_virtual_error_issued_p)
20082 {
20083 cp_parser_error (parser,
20084 "%<virtual%> specified more than once in base-specified");
20085 duplicate_virtual_error_issued_p = true;
20086 }
20087
20088 virtual_p = true;
20089
20090 /* Consume the `virtual' token. */
20091 cp_lexer_consume_token (parser->lexer);
20092
20093 break;
20094
20095 case RID_PUBLIC:
20096 case RID_PROTECTED:
20097 case RID_PRIVATE:
20098 /* If more than one access specifier appears, issue an
20099 error. */
20100 if (access != access_default_node
20101 && !duplicate_access_error_issued_p)
20102 {
20103 cp_parser_error (parser,
20104 "more than one access specifier in base-specified");
20105 duplicate_access_error_issued_p = true;
20106 }
20107
20108 access = ridpointers[(int) token->keyword];
20109
20110 /* Consume the access-specifier. */
20111 cp_lexer_consume_token (parser->lexer);
20112
20113 break;
20114
20115 default:
20116 done = true;
20117 break;
20118 }
20119 }
20120 /* It is not uncommon to see programs mechanically, erroneously, use
20121 the 'typename' keyword to denote (dependent) qualified types
20122 as base classes. */
20123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20124 {
20125 token = cp_lexer_peek_token (parser->lexer);
20126 if (!processing_template_decl)
20127 error_at (token->location,
20128 "keyword %<typename%> not allowed outside of templates");
20129 else
20130 error_at (token->location,
20131 "keyword %<typename%> not allowed in this context "
20132 "(the base class is implicitly a type)");
20133 cp_lexer_consume_token (parser->lexer);
20134 }
20135
20136 /* Look for the optional `::' operator. */
20137 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20138 /* Look for the nested-name-specifier. The simplest way to
20139 implement:
20140
20141 [temp.res]
20142
20143 The keyword `typename' is not permitted in a base-specifier or
20144 mem-initializer; in these contexts a qualified name that
20145 depends on a template-parameter is implicitly assumed to be a
20146 type name.
20147
20148 is to pretend that we have seen the `typename' keyword at this
20149 point. */
20150 cp_parser_nested_name_specifier_opt (parser,
20151 /*typename_keyword_p=*/true,
20152 /*check_dependency_p=*/true,
20153 typename_type,
20154 /*is_declaration=*/true);
20155 /* If the base class is given by a qualified name, assume that names
20156 we see are type names or templates, as appropriate. */
20157 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20158 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20159
20160 if (!parser->scope
20161 && cp_lexer_next_token_is_decltype (parser->lexer))
20162 /* DR 950 allows decltype as a base-specifier. */
20163 type = cp_parser_decltype (parser);
20164 else
20165 {
20166 /* Otherwise, look for the class-name. */
20167 type = cp_parser_class_name (parser,
20168 class_scope_p,
20169 template_p,
20170 typename_type,
20171 /*check_dependency_p=*/true,
20172 /*class_head_p=*/false,
20173 /*is_declaration=*/true);
20174 type = TREE_TYPE (type);
20175 }
20176
20177 if (type == error_mark_node)
20178 return error_mark_node;
20179
20180 return finish_base_specifier (type, access, virtual_p);
20181 }
20182
20183 /* Exception handling [gram.exception] */
20184
20185 /* Parse an (optional) noexcept-specification.
20186
20187 noexcept-specification:
20188 noexcept ( constant-expression ) [opt]
20189
20190 If no noexcept-specification is present, returns NULL_TREE.
20191 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20192 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20193 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20194 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20195 in which case a boolean condition is returned instead. */
20196
20197 static tree
20198 cp_parser_noexcept_specification_opt (cp_parser* parser,
20199 bool require_constexpr,
20200 bool* consumed_expr,
20201 bool return_cond)
20202 {
20203 cp_token *token;
20204 const char *saved_message;
20205
20206 /* Peek at the next token. */
20207 token = cp_lexer_peek_token (parser->lexer);
20208
20209 /* Is it a noexcept-specification? */
20210 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20211 {
20212 tree expr;
20213 cp_lexer_consume_token (parser->lexer);
20214
20215 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20216 {
20217 cp_lexer_consume_token (parser->lexer);
20218
20219 if (require_constexpr)
20220 {
20221 /* Types may not be defined in an exception-specification. */
20222 saved_message = parser->type_definition_forbidden_message;
20223 parser->type_definition_forbidden_message
20224 = G_("types may not be defined in an exception-specification");
20225
20226 expr = cp_parser_constant_expression (parser, false, NULL);
20227
20228 /* Restore the saved message. */
20229 parser->type_definition_forbidden_message = saved_message;
20230 }
20231 else
20232 {
20233 expr = cp_parser_expression (parser, false, NULL);
20234 *consumed_expr = true;
20235 }
20236
20237 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20238 }
20239 else
20240 {
20241 expr = boolean_true_node;
20242 if (!require_constexpr)
20243 *consumed_expr = false;
20244 }
20245
20246 /* We cannot build a noexcept-spec right away because this will check
20247 that expr is a constexpr. */
20248 if (!return_cond)
20249 return build_noexcept_spec (expr, tf_warning_or_error);
20250 else
20251 return expr;
20252 }
20253 else
20254 return NULL_TREE;
20255 }
20256
20257 /* Parse an (optional) exception-specification.
20258
20259 exception-specification:
20260 throw ( type-id-list [opt] )
20261
20262 Returns a TREE_LIST representing the exception-specification. The
20263 TREE_VALUE of each node is a type. */
20264
20265 static tree
20266 cp_parser_exception_specification_opt (cp_parser* parser)
20267 {
20268 cp_token *token;
20269 tree type_id_list;
20270 const char *saved_message;
20271
20272 /* Peek at the next token. */
20273 token = cp_lexer_peek_token (parser->lexer);
20274
20275 /* Is it a noexcept-specification? */
20276 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20277 false);
20278 if (type_id_list != NULL_TREE)
20279 return type_id_list;
20280
20281 /* If it's not `throw', then there's no exception-specification. */
20282 if (!cp_parser_is_keyword (token, RID_THROW))
20283 return NULL_TREE;
20284
20285 #if 0
20286 /* Enable this once a lot of code has transitioned to noexcept? */
20287 if (cxx_dialect >= cxx0x && !in_system_header)
20288 warning (OPT_Wdeprecated, "dynamic exception specifications are "
20289 "deprecated in C++0x; use %<noexcept%> instead");
20290 #endif
20291
20292 /* Consume the `throw'. */
20293 cp_lexer_consume_token (parser->lexer);
20294
20295 /* Look for the `('. */
20296 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20297
20298 /* Peek at the next token. */
20299 token = cp_lexer_peek_token (parser->lexer);
20300 /* If it's not a `)', then there is a type-id-list. */
20301 if (token->type != CPP_CLOSE_PAREN)
20302 {
20303 /* Types may not be defined in an exception-specification. */
20304 saved_message = parser->type_definition_forbidden_message;
20305 parser->type_definition_forbidden_message
20306 = G_("types may not be defined in an exception-specification");
20307 /* Parse the type-id-list. */
20308 type_id_list = cp_parser_type_id_list (parser);
20309 /* Restore the saved message. */
20310 parser->type_definition_forbidden_message = saved_message;
20311 }
20312 else
20313 type_id_list = empty_except_spec;
20314
20315 /* Look for the `)'. */
20316 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20317
20318 return type_id_list;
20319 }
20320
20321 /* Parse an (optional) type-id-list.
20322
20323 type-id-list:
20324 type-id ... [opt]
20325 type-id-list , type-id ... [opt]
20326
20327 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20328 in the order that the types were presented. */
20329
20330 static tree
20331 cp_parser_type_id_list (cp_parser* parser)
20332 {
20333 tree types = NULL_TREE;
20334
20335 while (true)
20336 {
20337 cp_token *token;
20338 tree type;
20339
20340 /* Get the next type-id. */
20341 type = cp_parser_type_id (parser);
20342 /* Parse the optional ellipsis. */
20343 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20344 {
20345 /* Consume the `...'. */
20346 cp_lexer_consume_token (parser->lexer);
20347
20348 /* Turn the type into a pack expansion expression. */
20349 type = make_pack_expansion (type);
20350 }
20351 /* Add it to the list. */
20352 types = add_exception_specifier (types, type, /*complain=*/1);
20353 /* Peek at the next token. */
20354 token = cp_lexer_peek_token (parser->lexer);
20355 /* If it is not a `,', we are done. */
20356 if (token->type != CPP_COMMA)
20357 break;
20358 /* Consume the `,'. */
20359 cp_lexer_consume_token (parser->lexer);
20360 }
20361
20362 return nreverse (types);
20363 }
20364
20365 /* Parse a try-block.
20366
20367 try-block:
20368 try compound-statement handler-seq */
20369
20370 static tree
20371 cp_parser_try_block (cp_parser* parser)
20372 {
20373 tree try_block;
20374
20375 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20376 try_block = begin_try_block ();
20377 cp_parser_compound_statement (parser, NULL, true, false);
20378 finish_try_block (try_block);
20379 cp_parser_handler_seq (parser);
20380 finish_handler_sequence (try_block);
20381
20382 return try_block;
20383 }
20384
20385 /* Parse a function-try-block.
20386
20387 function-try-block:
20388 try ctor-initializer [opt] function-body handler-seq */
20389
20390 static bool
20391 cp_parser_function_try_block (cp_parser* parser)
20392 {
20393 tree compound_stmt;
20394 tree try_block;
20395 bool ctor_initializer_p;
20396
20397 /* Look for the `try' keyword. */
20398 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20399 return false;
20400 /* Let the rest of the front end know where we are. */
20401 try_block = begin_function_try_block (&compound_stmt);
20402 /* Parse the function-body. */
20403 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20404 (parser, /*in_function_try_block=*/true);
20405 /* We're done with the `try' part. */
20406 finish_function_try_block (try_block);
20407 /* Parse the handlers. */
20408 cp_parser_handler_seq (parser);
20409 /* We're done with the handlers. */
20410 finish_function_handler_sequence (try_block, compound_stmt);
20411
20412 return ctor_initializer_p;
20413 }
20414
20415 /* Parse a handler-seq.
20416
20417 handler-seq:
20418 handler handler-seq [opt] */
20419
20420 static void
20421 cp_parser_handler_seq (cp_parser* parser)
20422 {
20423 while (true)
20424 {
20425 cp_token *token;
20426
20427 /* Parse the handler. */
20428 cp_parser_handler (parser);
20429 /* Peek at the next token. */
20430 token = cp_lexer_peek_token (parser->lexer);
20431 /* If it's not `catch' then there are no more handlers. */
20432 if (!cp_parser_is_keyword (token, RID_CATCH))
20433 break;
20434 }
20435 }
20436
20437 /* Parse a handler.
20438
20439 handler:
20440 catch ( exception-declaration ) compound-statement */
20441
20442 static void
20443 cp_parser_handler (cp_parser* parser)
20444 {
20445 tree handler;
20446 tree declaration;
20447
20448 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20449 handler = begin_handler ();
20450 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20451 declaration = cp_parser_exception_declaration (parser);
20452 finish_handler_parms (declaration, handler);
20453 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20454 cp_parser_compound_statement (parser, NULL, false, false);
20455 finish_handler (handler);
20456 }
20457
20458 /* Parse an exception-declaration.
20459
20460 exception-declaration:
20461 type-specifier-seq declarator
20462 type-specifier-seq abstract-declarator
20463 type-specifier-seq
20464 ...
20465
20466 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20467 ellipsis variant is used. */
20468
20469 static tree
20470 cp_parser_exception_declaration (cp_parser* parser)
20471 {
20472 cp_decl_specifier_seq type_specifiers;
20473 cp_declarator *declarator;
20474 const char *saved_message;
20475
20476 /* If it's an ellipsis, it's easy to handle. */
20477 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20478 {
20479 /* Consume the `...' token. */
20480 cp_lexer_consume_token (parser->lexer);
20481 return NULL_TREE;
20482 }
20483
20484 /* Types may not be defined in exception-declarations. */
20485 saved_message = parser->type_definition_forbidden_message;
20486 parser->type_definition_forbidden_message
20487 = G_("types may not be defined in exception-declarations");
20488
20489 /* Parse the type-specifier-seq. */
20490 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20491 /*is_trailing_return=*/false,
20492 &type_specifiers);
20493 /* If it's a `)', then there is no declarator. */
20494 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20495 declarator = NULL;
20496 else
20497 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20498 /*ctor_dtor_or_conv_p=*/NULL,
20499 /*parenthesized_p=*/NULL,
20500 /*member_p=*/false);
20501
20502 /* Restore the saved message. */
20503 parser->type_definition_forbidden_message = saved_message;
20504
20505 if (!type_specifiers.any_specifiers_p)
20506 return error_mark_node;
20507
20508 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20509 }
20510
20511 /* Parse a throw-expression.
20512
20513 throw-expression:
20514 throw assignment-expression [opt]
20515
20516 Returns a THROW_EXPR representing the throw-expression. */
20517
20518 static tree
20519 cp_parser_throw_expression (cp_parser* parser)
20520 {
20521 tree expression;
20522 cp_token* token;
20523
20524 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20525 token = cp_lexer_peek_token (parser->lexer);
20526 /* Figure out whether or not there is an assignment-expression
20527 following the "throw" keyword. */
20528 if (token->type == CPP_COMMA
20529 || token->type == CPP_SEMICOLON
20530 || token->type == CPP_CLOSE_PAREN
20531 || token->type == CPP_CLOSE_SQUARE
20532 || token->type == CPP_CLOSE_BRACE
20533 || token->type == CPP_COLON)
20534 expression = NULL_TREE;
20535 else
20536 expression = cp_parser_assignment_expression (parser,
20537 /*cast_p=*/false, NULL);
20538
20539 return build_throw (expression);
20540 }
20541
20542 /* GNU Extensions */
20543
20544 /* Parse an (optional) asm-specification.
20545
20546 asm-specification:
20547 asm ( string-literal )
20548
20549 If the asm-specification is present, returns a STRING_CST
20550 corresponding to the string-literal. Otherwise, returns
20551 NULL_TREE. */
20552
20553 static tree
20554 cp_parser_asm_specification_opt (cp_parser* parser)
20555 {
20556 cp_token *token;
20557 tree asm_specification;
20558
20559 /* Peek at the next token. */
20560 token = cp_lexer_peek_token (parser->lexer);
20561 /* If the next token isn't the `asm' keyword, then there's no
20562 asm-specification. */
20563 if (!cp_parser_is_keyword (token, RID_ASM))
20564 return NULL_TREE;
20565
20566 /* Consume the `asm' token. */
20567 cp_lexer_consume_token (parser->lexer);
20568 /* Look for the `('. */
20569 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20570
20571 /* Look for the string-literal. */
20572 asm_specification = cp_parser_string_literal (parser, false, false);
20573
20574 /* Look for the `)'. */
20575 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20576
20577 return asm_specification;
20578 }
20579
20580 /* Parse an asm-operand-list.
20581
20582 asm-operand-list:
20583 asm-operand
20584 asm-operand-list , asm-operand
20585
20586 asm-operand:
20587 string-literal ( expression )
20588 [ string-literal ] string-literal ( expression )
20589
20590 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20591 each node is the expression. The TREE_PURPOSE is itself a
20592 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20593 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20594 is a STRING_CST for the string literal before the parenthesis. Returns
20595 ERROR_MARK_NODE if any of the operands are invalid. */
20596
20597 static tree
20598 cp_parser_asm_operand_list (cp_parser* parser)
20599 {
20600 tree asm_operands = NULL_TREE;
20601 bool invalid_operands = false;
20602
20603 while (true)
20604 {
20605 tree string_literal;
20606 tree expression;
20607 tree name;
20608
20609 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20610 {
20611 /* Consume the `[' token. */
20612 cp_lexer_consume_token (parser->lexer);
20613 /* Read the operand name. */
20614 name = cp_parser_identifier (parser);
20615 if (name != error_mark_node)
20616 name = build_string (IDENTIFIER_LENGTH (name),
20617 IDENTIFIER_POINTER (name));
20618 /* Look for the closing `]'. */
20619 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20620 }
20621 else
20622 name = NULL_TREE;
20623 /* Look for the string-literal. */
20624 string_literal = cp_parser_string_literal (parser, false, false);
20625
20626 /* Look for the `('. */
20627 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20628 /* Parse the expression. */
20629 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20630 /* Look for the `)'. */
20631 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20632
20633 if (name == error_mark_node
20634 || string_literal == error_mark_node
20635 || expression == error_mark_node)
20636 invalid_operands = true;
20637
20638 /* Add this operand to the list. */
20639 asm_operands = tree_cons (build_tree_list (name, string_literal),
20640 expression,
20641 asm_operands);
20642 /* If the next token is not a `,', there are no more
20643 operands. */
20644 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20645 break;
20646 /* Consume the `,'. */
20647 cp_lexer_consume_token (parser->lexer);
20648 }
20649
20650 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20651 }
20652
20653 /* Parse an asm-clobber-list.
20654
20655 asm-clobber-list:
20656 string-literal
20657 asm-clobber-list , string-literal
20658
20659 Returns a TREE_LIST, indicating the clobbers in the order that they
20660 appeared. The TREE_VALUE of each node is a STRING_CST. */
20661
20662 static tree
20663 cp_parser_asm_clobber_list (cp_parser* parser)
20664 {
20665 tree clobbers = NULL_TREE;
20666
20667 while (true)
20668 {
20669 tree string_literal;
20670
20671 /* Look for the string literal. */
20672 string_literal = cp_parser_string_literal (parser, false, false);
20673 /* Add it to the list. */
20674 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20675 /* If the next token is not a `,', then the list is
20676 complete. */
20677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20678 break;
20679 /* Consume the `,' token. */
20680 cp_lexer_consume_token (parser->lexer);
20681 }
20682
20683 return clobbers;
20684 }
20685
20686 /* Parse an asm-label-list.
20687
20688 asm-label-list:
20689 identifier
20690 asm-label-list , identifier
20691
20692 Returns a TREE_LIST, indicating the labels in the order that they
20693 appeared. The TREE_VALUE of each node is a label. */
20694
20695 static tree
20696 cp_parser_asm_label_list (cp_parser* parser)
20697 {
20698 tree labels = NULL_TREE;
20699
20700 while (true)
20701 {
20702 tree identifier, label, name;
20703
20704 /* Look for the identifier. */
20705 identifier = cp_parser_identifier (parser);
20706 if (!error_operand_p (identifier))
20707 {
20708 label = lookup_label (identifier);
20709 if (TREE_CODE (label) == LABEL_DECL)
20710 {
20711 TREE_USED (label) = 1;
20712 check_goto (label);
20713 name = build_string (IDENTIFIER_LENGTH (identifier),
20714 IDENTIFIER_POINTER (identifier));
20715 labels = tree_cons (name, label, labels);
20716 }
20717 }
20718 /* If the next token is not a `,', then the list is
20719 complete. */
20720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20721 break;
20722 /* Consume the `,' token. */
20723 cp_lexer_consume_token (parser->lexer);
20724 }
20725
20726 return nreverse (labels);
20727 }
20728
20729 /* Return TRUE iff the next tokens in the stream are possibly the
20730 beginning of a GNU extension attribute. */
20731
20732 static bool
20733 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20734 {
20735 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20736 }
20737
20738 /* Return TRUE iff the next tokens in the stream are possibly the
20739 beginning of a standard C++-11 attribute specifier. */
20740
20741 static bool
20742 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20743 {
20744 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20745 }
20746
20747 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20748 beginning of a standard C++-11 attribute specifier. */
20749
20750 static bool
20751 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20752 {
20753 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20754
20755 return (cxx_dialect >= cxx0x
20756 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20757 || (token->type == CPP_OPEN_SQUARE
20758 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20759 && token->type == CPP_OPEN_SQUARE)));
20760 }
20761
20762 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20763 beginning of a GNU extension attribute. */
20764
20765 static bool
20766 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20767 {
20768 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20769
20770 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20771 }
20772
20773 /* Return true iff the next tokens can be the beginning of either a
20774 GNU attribute list, or a standard C++11 attribute sequence. */
20775
20776 static bool
20777 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20778 {
20779 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20780 || cp_next_tokens_can_be_std_attribute_p (parser));
20781 }
20782
20783 /* Return true iff the next Nth tokens can be the beginning of either
20784 a GNU attribute list, or a standard C++11 attribute sequence. */
20785
20786 static bool
20787 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20788 {
20789 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20790 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20791 }
20792
20793 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20794 of GNU attributes, or return NULL. */
20795
20796 static tree
20797 cp_parser_attributes_opt (cp_parser *parser)
20798 {
20799 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20800 return cp_parser_gnu_attributes_opt (parser);
20801 return cp_parser_std_attribute_spec_seq (parser);
20802 }
20803
20804 /* Parse an (optional) series of attributes.
20805
20806 attributes:
20807 attributes attribute
20808
20809 attribute:
20810 __attribute__ (( attribute-list [opt] ))
20811
20812 The return value is as for cp_parser_gnu_attribute_list. */
20813
20814 static tree
20815 cp_parser_gnu_attributes_opt (cp_parser* parser)
20816 {
20817 tree attributes = NULL_TREE;
20818
20819 while (true)
20820 {
20821 cp_token *token;
20822 tree attribute_list;
20823 bool ok = true;
20824
20825 /* Peek at the next token. */
20826 token = cp_lexer_peek_token (parser->lexer);
20827 /* If it's not `__attribute__', then we're done. */
20828 if (token->keyword != RID_ATTRIBUTE)
20829 break;
20830
20831 /* Consume the `__attribute__' keyword. */
20832 cp_lexer_consume_token (parser->lexer);
20833 /* Look for the two `(' tokens. */
20834 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20835 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20836
20837 /* Peek at the next token. */
20838 token = cp_lexer_peek_token (parser->lexer);
20839 if (token->type != CPP_CLOSE_PAREN)
20840 /* Parse the attribute-list. */
20841 attribute_list = cp_parser_gnu_attribute_list (parser);
20842 else
20843 /* If the next token is a `)', then there is no attribute
20844 list. */
20845 attribute_list = NULL;
20846
20847 /* Look for the two `)' tokens. */
20848 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20849 ok = false;
20850 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20851 ok = false;
20852 if (!ok)
20853 cp_parser_skip_to_end_of_statement (parser);
20854
20855 /* Add these new attributes to the list. */
20856 attributes = chainon (attributes, attribute_list);
20857 }
20858
20859 return attributes;
20860 }
20861
20862 /* Parse a GNU attribute-list.
20863
20864 attribute-list:
20865 attribute
20866 attribute-list , attribute
20867
20868 attribute:
20869 identifier
20870 identifier ( identifier )
20871 identifier ( identifier , expression-list )
20872 identifier ( expression-list )
20873
20874 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20875 to an attribute. The TREE_PURPOSE of each node is the identifier
20876 indicating which attribute is in use. The TREE_VALUE represents
20877 the arguments, if any. */
20878
20879 static tree
20880 cp_parser_gnu_attribute_list (cp_parser* parser)
20881 {
20882 tree attribute_list = NULL_TREE;
20883 bool save_translate_strings_p = parser->translate_strings_p;
20884
20885 parser->translate_strings_p = false;
20886 while (true)
20887 {
20888 cp_token *token;
20889 tree identifier;
20890 tree attribute;
20891
20892 /* Look for the identifier. We also allow keywords here; for
20893 example `__attribute__ ((const))' is legal. */
20894 token = cp_lexer_peek_token (parser->lexer);
20895 if (token->type == CPP_NAME
20896 || token->type == CPP_KEYWORD)
20897 {
20898 tree arguments = NULL_TREE;
20899
20900 /* Consume the token. */
20901 token = cp_lexer_consume_token (parser->lexer);
20902
20903 /* Save away the identifier that indicates which attribute
20904 this is. */
20905 identifier = (token->type == CPP_KEYWORD)
20906 /* For keywords, use the canonical spelling, not the
20907 parsed identifier. */
20908 ? ridpointers[(int) token->keyword]
20909 : token->u.value;
20910
20911 attribute = build_tree_list (identifier, NULL_TREE);
20912
20913 /* Peek at the next token. */
20914 token = cp_lexer_peek_token (parser->lexer);
20915 /* If it's an `(', then parse the attribute arguments. */
20916 if (token->type == CPP_OPEN_PAREN)
20917 {
20918 vec<tree, va_gc> *vec;
20919 int attr_flag = (attribute_takes_identifier_p (identifier)
20920 ? id_attr : normal_attr);
20921 vec = cp_parser_parenthesized_expression_list
20922 (parser, attr_flag, /*cast_p=*/false,
20923 /*allow_expansion_p=*/false,
20924 /*non_constant_p=*/NULL);
20925 if (vec == NULL)
20926 arguments = error_mark_node;
20927 else
20928 {
20929 arguments = build_tree_list_vec (vec);
20930 release_tree_vector (vec);
20931 }
20932 /* Save the arguments away. */
20933 TREE_VALUE (attribute) = arguments;
20934 }
20935
20936 if (arguments != error_mark_node)
20937 {
20938 /* Add this attribute to the list. */
20939 TREE_CHAIN (attribute) = attribute_list;
20940 attribute_list = attribute;
20941 }
20942
20943 token = cp_lexer_peek_token (parser->lexer);
20944 }
20945 /* Now, look for more attributes. If the next token isn't a
20946 `,', we're done. */
20947 if (token->type != CPP_COMMA)
20948 break;
20949
20950 /* Consume the comma and keep going. */
20951 cp_lexer_consume_token (parser->lexer);
20952 }
20953 parser->translate_strings_p = save_translate_strings_p;
20954
20955 /* We built up the list in reverse order. */
20956 return nreverse (attribute_list);
20957 }
20958
20959 /* Parse a standard C++11 attribute.
20960
20961 The returned representation is a TREE_LIST which TREE_PURPOSE is
20962 the scoped name of the attribute, and the TREE_VALUE is its
20963 arguments list.
20964
20965 Note that the scoped name of the attribute is itself a TREE_LIST
20966 which TREE_PURPOSE is the namespace of the attribute, and
20967 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20968 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20969 and which TREE_PURPOSE is directly the attribute name.
20970
20971 Clients of the attribute code should use get_attribute_namespace
20972 and get_attribute_name to get the actual namespace and name of
20973 attributes, regardless of their being GNU or C++11 attributes.
20974
20975 attribute:
20976 attribute-token attribute-argument-clause [opt]
20977
20978 attribute-token:
20979 identifier
20980 attribute-scoped-token
20981
20982 attribute-scoped-token:
20983 attribute-namespace :: identifier
20984
20985 attribute-namespace:
20986 identifier
20987
20988 attribute-argument-clause:
20989 ( balanced-token-seq )
20990
20991 balanced-token-seq:
20992 balanced-token [opt]
20993 balanced-token-seq balanced-token
20994
20995 balanced-token:
20996 ( balanced-token-seq )
20997 [ balanced-token-seq ]
20998 { balanced-token-seq }. */
20999
21000 static tree
21001 cp_parser_std_attribute (cp_parser *parser)
21002 {
21003 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21004 cp_token *token;
21005
21006 /* First, parse name of the the attribute, a.k.a
21007 attribute-token. */
21008
21009 token = cp_lexer_peek_token (parser->lexer);
21010 if (token->type == CPP_NAME)
21011 attr_id = token->u.value;
21012 else if (token->type == CPP_KEYWORD)
21013 attr_id = ridpointers[(int) token->keyword];
21014 else if (token->flags & NAMED_OP)
21015 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21016
21017 if (attr_id == NULL_TREE)
21018 return NULL_TREE;
21019
21020 cp_lexer_consume_token (parser->lexer);
21021
21022 token = cp_lexer_peek_token (parser->lexer);
21023 if (token->type == CPP_SCOPE)
21024 {
21025 /* We are seeing a scoped attribute token. */
21026
21027 cp_lexer_consume_token (parser->lexer);
21028 attr_ns = attr_id;
21029
21030 token = cp_lexer_consume_token (parser->lexer);
21031 if (token->type == CPP_NAME)
21032 attr_id = token->u.value;
21033 else if (token->type == CPP_KEYWORD)
21034 attr_id = ridpointers[(int) token->keyword];
21035 else
21036 {
21037 error_at (token->location,
21038 "expected an identifier for the attribute name");
21039 return error_mark_node;
21040 }
21041 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21042 NULL_TREE);
21043 token = cp_lexer_peek_token (parser->lexer);
21044 }
21045 else
21046 {
21047 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21048 NULL_TREE);
21049 /* C++11 noreturn attribute is equivalent to GNU's. */
21050 if (is_attribute_p ("noreturn", attr_id))
21051 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21052 }
21053
21054 /* Now parse the optional argument clause of the attribute. */
21055
21056 if (token->type != CPP_OPEN_PAREN)
21057 return attribute;
21058
21059 {
21060 vec<tree, va_gc> *vec;
21061 int attr_flag = normal_attr;
21062
21063 if (attr_ns == get_identifier ("gnu")
21064 && attribute_takes_identifier_p (attr_id))
21065 /* A GNU attribute that takes an identifier in parameter. */
21066 attr_flag = id_attr;
21067
21068 vec = cp_parser_parenthesized_expression_list
21069 (parser, attr_flag, /*cast_p=*/false,
21070 /*allow_expansion_p=*/true,
21071 /*non_constant_p=*/NULL);
21072 if (vec == NULL)
21073 arguments = error_mark_node;
21074 else
21075 {
21076 arguments = build_tree_list_vec (vec);
21077 release_tree_vector (vec);
21078 }
21079
21080 if (arguments == error_mark_node)
21081 attribute = error_mark_node;
21082 else
21083 TREE_VALUE (attribute) = arguments;
21084 }
21085
21086 return attribute;
21087 }
21088
21089 /* Parse a list of standard C++-11 attributes.
21090
21091 attribute-list:
21092 attribute [opt]
21093 attribute-list , attribute[opt]
21094 attribute ...
21095 attribute-list , attribute ...
21096 */
21097
21098 static tree
21099 cp_parser_std_attribute_list (cp_parser *parser)
21100 {
21101 tree attributes = NULL_TREE, attribute = NULL_TREE;
21102 cp_token *token = NULL;
21103
21104 while (true)
21105 {
21106 attribute = cp_parser_std_attribute (parser);
21107 if (attribute == error_mark_node)
21108 break;
21109 if (attribute != NULL_TREE)
21110 {
21111 TREE_CHAIN (attribute) = attributes;
21112 attributes = attribute;
21113 }
21114 token = cp_lexer_peek_token (parser->lexer);
21115 if (token->type != CPP_COMMA)
21116 break;
21117 cp_lexer_consume_token (parser->lexer);
21118 }
21119 attributes = nreverse (attributes);
21120 return attributes;
21121 }
21122
21123 /* Parse a standard C++-11 attribute specifier.
21124
21125 attribute-specifier:
21126 [ [ attribute-list ] ]
21127 alignment-specifier
21128
21129 alignment-specifier:
21130 alignas ( type-id ... [opt] )
21131 alignas ( alignment-expression ... [opt] ). */
21132
21133 static tree
21134 cp_parser_std_attribute_spec (cp_parser *parser)
21135 {
21136 tree attributes = NULL_TREE;
21137 cp_token *token = cp_lexer_peek_token (parser->lexer);
21138
21139 if (token->type == CPP_OPEN_SQUARE
21140 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21141 {
21142 cp_lexer_consume_token (parser->lexer);
21143 cp_lexer_consume_token (parser->lexer);
21144
21145 attributes = cp_parser_std_attribute_list (parser);
21146
21147 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21148 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21149 cp_parser_skip_to_end_of_statement (parser);
21150 else
21151 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21152 when we are sure that we have actually parsed them. */
21153 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21154 }
21155 else
21156 {
21157 tree alignas_expr;
21158
21159 /* Look for an alignment-specifier. */
21160
21161 token = cp_lexer_peek_token (parser->lexer);
21162
21163 if (token->type != CPP_KEYWORD
21164 || token->keyword != RID_ALIGNAS)
21165 return NULL_TREE;
21166
21167 cp_lexer_consume_token (parser->lexer);
21168 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21169
21170 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21171 {
21172 cp_parser_error (parser, "expected %<(%>");
21173 return error_mark_node;
21174 }
21175
21176 cp_parser_parse_tentatively (parser);
21177 alignas_expr = cp_parser_type_id (parser);
21178
21179 if (!cp_parser_parse_definitely (parser))
21180 {
21181 gcc_assert (alignas_expr == error_mark_node
21182 || alignas_expr == NULL_TREE);
21183
21184 alignas_expr =
21185 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21186 /**cp_id_kind=*/NULL);
21187 if (alignas_expr == NULL_TREE
21188 || alignas_expr == error_mark_node)
21189 return alignas_expr;
21190 }
21191
21192 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
21193 {
21194 cp_parser_error (parser, "expected %<)%>");
21195 return error_mark_node;
21196 }
21197
21198 alignas_expr = cxx_alignas_expr (alignas_expr);
21199
21200 /* Build the C++-11 representation of an 'aligned'
21201 attribute. */
21202 attributes =
21203 build_tree_list (build_tree_list (get_identifier ("gnu"),
21204 get_identifier ("aligned")),
21205 build_tree_list (NULL_TREE, alignas_expr));
21206 }
21207
21208 return attributes;
21209 }
21210
21211 /* Parse a standard C++-11 attribute-specifier-seq.
21212
21213 attribute-specifier-seq:
21214 attribute-specifier-seq [opt] attribute-specifier
21215 */
21216
21217 static tree
21218 cp_parser_std_attribute_spec_seq (cp_parser *parser)
21219 {
21220 tree attr_specs = NULL;
21221
21222 while (true)
21223 {
21224 tree attr_spec = cp_parser_std_attribute_spec (parser);
21225 if (attr_spec == NULL_TREE)
21226 break;
21227 if (attr_spec == error_mark_node)
21228 return error_mark_node;
21229
21230 TREE_CHAIN (attr_spec) = attr_specs;
21231 attr_specs = attr_spec;
21232 }
21233
21234 attr_specs = nreverse (attr_specs);
21235 return attr_specs;
21236 }
21237
21238 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21239 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21240 current value of the PEDANTIC flag, regardless of whether or not
21241 the `__extension__' keyword is present. The caller is responsible
21242 for restoring the value of the PEDANTIC flag. */
21243
21244 static bool
21245 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21246 {
21247 /* Save the old value of the PEDANTIC flag. */
21248 *saved_pedantic = pedantic;
21249
21250 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21251 {
21252 /* Consume the `__extension__' token. */
21253 cp_lexer_consume_token (parser->lexer);
21254 /* We're not being pedantic while the `__extension__' keyword is
21255 in effect. */
21256 pedantic = 0;
21257
21258 return true;
21259 }
21260
21261 return false;
21262 }
21263
21264 /* Parse a label declaration.
21265
21266 label-declaration:
21267 __label__ label-declarator-seq ;
21268
21269 label-declarator-seq:
21270 identifier , label-declarator-seq
21271 identifier */
21272
21273 static void
21274 cp_parser_label_declaration (cp_parser* parser)
21275 {
21276 /* Look for the `__label__' keyword. */
21277 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21278
21279 while (true)
21280 {
21281 tree identifier;
21282
21283 /* Look for an identifier. */
21284 identifier = cp_parser_identifier (parser);
21285 /* If we failed, stop. */
21286 if (identifier == error_mark_node)
21287 break;
21288 /* Declare it as a label. */
21289 finish_label_decl (identifier);
21290 /* If the next token is a `;', stop. */
21291 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21292 break;
21293 /* Look for the `,' separating the label declarations. */
21294 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21295 }
21296
21297 /* Look for the final `;'. */
21298 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21299 }
21300
21301 /* Support Functions */
21302
21303 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21304 NAME should have one of the representations used for an
21305 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21306 is returned. If PARSER->SCOPE is a dependent type, then a
21307 SCOPE_REF is returned.
21308
21309 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21310 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21311 was formed. Abstractly, such entities should not be passed to this
21312 function, because they do not need to be looked up, but it is
21313 simpler to check for this special case here, rather than at the
21314 call-sites.
21315
21316 In cases not explicitly covered above, this function returns a
21317 DECL, OVERLOAD, or baselink representing the result of the lookup.
21318 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21319 is returned.
21320
21321 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21322 (e.g., "struct") that was used. In that case bindings that do not
21323 refer to types are ignored.
21324
21325 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21326 ignored.
21327
21328 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21329 are ignored.
21330
21331 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21332 types.
21333
21334 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21335 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21336 NULL_TREE otherwise. */
21337
21338 static tree
21339 cp_parser_lookup_name (cp_parser *parser, tree name,
21340 enum tag_types tag_type,
21341 bool is_template,
21342 bool is_namespace,
21343 bool check_dependency,
21344 tree *ambiguous_decls,
21345 location_t name_location)
21346 {
21347 tree decl;
21348 tree object_type = parser->context->object_type;
21349
21350 /* Assume that the lookup will be unambiguous. */
21351 if (ambiguous_decls)
21352 *ambiguous_decls = NULL_TREE;
21353
21354 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21355 no longer valid. Note that if we are parsing tentatively, and
21356 the parse fails, OBJECT_TYPE will be automatically restored. */
21357 parser->context->object_type = NULL_TREE;
21358
21359 if (name == error_mark_node)
21360 return error_mark_node;
21361
21362 /* A template-id has already been resolved; there is no lookup to
21363 do. */
21364 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21365 return name;
21366 if (BASELINK_P (name))
21367 {
21368 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21369 == TEMPLATE_ID_EXPR);
21370 return name;
21371 }
21372
21373 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21374 it should already have been checked to make sure that the name
21375 used matches the type being destroyed. */
21376 if (TREE_CODE (name) == BIT_NOT_EXPR)
21377 {
21378 tree type;
21379
21380 /* Figure out to which type this destructor applies. */
21381 if (parser->scope)
21382 type = parser->scope;
21383 else if (object_type)
21384 type = object_type;
21385 else
21386 type = current_class_type;
21387 /* If that's not a class type, there is no destructor. */
21388 if (!type || !CLASS_TYPE_P (type))
21389 return error_mark_node;
21390 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21391 lazily_declare_fn (sfk_destructor, type);
21392 if (!CLASSTYPE_DESTRUCTORS (type))
21393 return error_mark_node;
21394 /* If it was a class type, return the destructor. */
21395 return CLASSTYPE_DESTRUCTORS (type);
21396 }
21397
21398 /* By this point, the NAME should be an ordinary identifier. If
21399 the id-expression was a qualified name, the qualifying scope is
21400 stored in PARSER->SCOPE at this point. */
21401 gcc_assert (identifier_p (name));
21402
21403 /* Perform the lookup. */
21404 if (parser->scope)
21405 {
21406 bool dependent_p;
21407
21408 if (parser->scope == error_mark_node)
21409 return error_mark_node;
21410
21411 /* If the SCOPE is dependent, the lookup must be deferred until
21412 the template is instantiated -- unless we are explicitly
21413 looking up names in uninstantiated templates. Even then, we
21414 cannot look up the name if the scope is not a class type; it
21415 might, for example, be a template type parameter. */
21416 dependent_p = (TYPE_P (parser->scope)
21417 && dependent_scope_p (parser->scope));
21418 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21419 && dependent_p)
21420 /* Defer lookup. */
21421 decl = error_mark_node;
21422 else
21423 {
21424 tree pushed_scope = NULL_TREE;
21425
21426 /* If PARSER->SCOPE is a dependent type, then it must be a
21427 class type, and we must not be checking dependencies;
21428 otherwise, we would have processed this lookup above. So
21429 that PARSER->SCOPE is not considered a dependent base by
21430 lookup_member, we must enter the scope here. */
21431 if (dependent_p)
21432 pushed_scope = push_scope (parser->scope);
21433
21434 /* If the PARSER->SCOPE is a template specialization, it
21435 may be instantiated during name lookup. In that case,
21436 errors may be issued. Even if we rollback the current
21437 tentative parse, those errors are valid. */
21438 decl = lookup_qualified_name (parser->scope, name,
21439 tag_type != none_type,
21440 /*complain=*/true);
21441
21442 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21443 lookup result and the nested-name-specifier nominates a class C:
21444 * if the name specified after the nested-name-specifier, when
21445 looked up in C, is the injected-class-name of C (Clause 9), or
21446 * if the name specified after the nested-name-specifier is the
21447 same as the identifier or the simple-template-id's template-
21448 name in the last component of the nested-name-specifier,
21449 the name is instead considered to name the constructor of
21450 class C. [ Note: for example, the constructor is not an
21451 acceptable lookup result in an elaborated-type-specifier so
21452 the constructor would not be used in place of the
21453 injected-class-name. --end note ] Such a constructor name
21454 shall be used only in the declarator-id of a declaration that
21455 names a constructor or in a using-declaration. */
21456 if (tag_type == none_type
21457 && DECL_SELF_REFERENCE_P (decl)
21458 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21459 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21460 tag_type != none_type,
21461 /*complain=*/true);
21462
21463 /* If we have a single function from a using decl, pull it out. */
21464 if (TREE_CODE (decl) == OVERLOAD
21465 && !really_overloaded_fn (decl))
21466 decl = OVL_FUNCTION (decl);
21467
21468 if (pushed_scope)
21469 pop_scope (pushed_scope);
21470 }
21471
21472 /* If the scope is a dependent type and either we deferred lookup or
21473 we did lookup but didn't find the name, rememeber the name. */
21474 if (decl == error_mark_node && TYPE_P (parser->scope)
21475 && dependent_type_p (parser->scope))
21476 {
21477 if (tag_type)
21478 {
21479 tree type;
21480
21481 /* The resolution to Core Issue 180 says that `struct
21482 A::B' should be considered a type-name, even if `A'
21483 is dependent. */
21484 type = make_typename_type (parser->scope, name, tag_type,
21485 /*complain=*/tf_error);
21486 decl = TYPE_NAME (type);
21487 }
21488 else if (is_template
21489 && (cp_parser_next_token_ends_template_argument_p (parser)
21490 || cp_lexer_next_token_is (parser->lexer,
21491 CPP_CLOSE_PAREN)))
21492 decl = make_unbound_class_template (parser->scope,
21493 name, NULL_TREE,
21494 /*complain=*/tf_error);
21495 else
21496 decl = build_qualified_name (/*type=*/NULL_TREE,
21497 parser->scope, name,
21498 is_template);
21499 }
21500 parser->qualifying_scope = parser->scope;
21501 parser->object_scope = NULL_TREE;
21502 }
21503 else if (object_type)
21504 {
21505 tree object_decl = NULL_TREE;
21506 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21507 OBJECT_TYPE is not a class. */
21508 if (CLASS_TYPE_P (object_type))
21509 /* If the OBJECT_TYPE is a template specialization, it may
21510 be instantiated during name lookup. In that case, errors
21511 may be issued. Even if we rollback the current tentative
21512 parse, those errors are valid. */
21513 object_decl = lookup_member (object_type,
21514 name,
21515 /*protect=*/0,
21516 tag_type != none_type,
21517 tf_warning_or_error);
21518 /* Look it up in the enclosing context, too. */
21519 decl = lookup_name_real (name, tag_type != none_type,
21520 /*nonclass=*/0,
21521 /*block_p=*/true, is_namespace, 0);
21522 parser->object_scope = object_type;
21523 parser->qualifying_scope = NULL_TREE;
21524 if (object_decl)
21525 decl = object_decl;
21526 }
21527 else
21528 {
21529 decl = lookup_name_real (name, tag_type != none_type,
21530 /*nonclass=*/0,
21531 /*block_p=*/true, is_namespace, 0);
21532 parser->qualifying_scope = NULL_TREE;
21533 parser->object_scope = NULL_TREE;
21534 }
21535
21536 /* If the lookup failed, let our caller know. */
21537 if (!decl || decl == error_mark_node)
21538 return error_mark_node;
21539
21540 /* Pull out the template from an injected-class-name (or multiple). */
21541 if (is_template)
21542 decl = maybe_get_template_decl_from_type_decl (decl);
21543
21544 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21545 if (TREE_CODE (decl) == TREE_LIST)
21546 {
21547 if (ambiguous_decls)
21548 *ambiguous_decls = decl;
21549 /* The error message we have to print is too complicated for
21550 cp_parser_error, so we incorporate its actions directly. */
21551 if (!cp_parser_simulate_error (parser))
21552 {
21553 error_at (name_location, "reference to %qD is ambiguous",
21554 name);
21555 print_candidates (decl);
21556 }
21557 return error_mark_node;
21558 }
21559
21560 gcc_assert (DECL_P (decl)
21561 || TREE_CODE (decl) == OVERLOAD
21562 || TREE_CODE (decl) == SCOPE_REF
21563 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21564 || BASELINK_P (decl));
21565
21566 /* If we have resolved the name of a member declaration, check to
21567 see if the declaration is accessible. When the name resolves to
21568 set of overloaded functions, accessibility is checked when
21569 overload resolution is done.
21570
21571 During an explicit instantiation, access is not checked at all,
21572 as per [temp.explicit]. */
21573 if (DECL_P (decl))
21574 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21575
21576 maybe_record_typedef_use (decl);
21577
21578 return decl;
21579 }
21580
21581 /* Like cp_parser_lookup_name, but for use in the typical case where
21582 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21583 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21584
21585 static tree
21586 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21587 {
21588 return cp_parser_lookup_name (parser, name,
21589 none_type,
21590 /*is_template=*/false,
21591 /*is_namespace=*/false,
21592 /*check_dependency=*/true,
21593 /*ambiguous_decls=*/NULL,
21594 location);
21595 }
21596
21597 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21598 the current context, return the TYPE_DECL. If TAG_NAME_P is
21599 true, the DECL indicates the class being defined in a class-head,
21600 or declared in an elaborated-type-specifier.
21601
21602 Otherwise, return DECL. */
21603
21604 static tree
21605 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21606 {
21607 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21608 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21609
21610 struct A {
21611 template <typename T> struct B;
21612 };
21613
21614 template <typename T> struct A::B {};
21615
21616 Similarly, in an elaborated-type-specifier:
21617
21618 namespace N { struct X{}; }
21619
21620 struct A {
21621 template <typename T> friend struct N::X;
21622 };
21623
21624 However, if the DECL refers to a class type, and we are in
21625 the scope of the class, then the name lookup automatically
21626 finds the TYPE_DECL created by build_self_reference rather
21627 than a TEMPLATE_DECL. For example, in:
21628
21629 template <class T> struct S {
21630 S s;
21631 };
21632
21633 there is no need to handle such case. */
21634
21635 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21636 return DECL_TEMPLATE_RESULT (decl);
21637
21638 return decl;
21639 }
21640
21641 /* If too many, or too few, template-parameter lists apply to the
21642 declarator, issue an error message. Returns TRUE if all went well,
21643 and FALSE otherwise. */
21644
21645 static bool
21646 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21647 cp_declarator *declarator,
21648 location_t declarator_location)
21649 {
21650 switch (declarator->kind)
21651 {
21652 case cdk_id:
21653 {
21654 unsigned num_templates = 0;
21655 tree scope = declarator->u.id.qualifying_scope;
21656
21657 if (scope)
21658 num_templates = num_template_headers_for_class (scope);
21659 else if (TREE_CODE (declarator->u.id.unqualified_name)
21660 == TEMPLATE_ID_EXPR)
21661 /* If the DECLARATOR has the form `X<y>' then it uses one
21662 additional level of template parameters. */
21663 ++num_templates;
21664
21665 return cp_parser_check_template_parameters
21666 (parser, num_templates, declarator_location, declarator);
21667 }
21668
21669 case cdk_function:
21670 case cdk_array:
21671 case cdk_pointer:
21672 case cdk_reference:
21673 case cdk_ptrmem:
21674 return (cp_parser_check_declarator_template_parameters
21675 (parser, declarator->declarator, declarator_location));
21676
21677 case cdk_error:
21678 return true;
21679
21680 default:
21681 gcc_unreachable ();
21682 }
21683 return false;
21684 }
21685
21686 /* NUM_TEMPLATES were used in the current declaration. If that is
21687 invalid, return FALSE and issue an error messages. Otherwise,
21688 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21689 declarator and we can print more accurate diagnostics. */
21690
21691 static bool
21692 cp_parser_check_template_parameters (cp_parser* parser,
21693 unsigned num_templates,
21694 location_t location,
21695 cp_declarator *declarator)
21696 {
21697 /* If there are the same number of template classes and parameter
21698 lists, that's OK. */
21699 if (parser->num_template_parameter_lists == num_templates)
21700 return true;
21701 /* If there are more, but only one more, then we are referring to a
21702 member template. That's OK too. */
21703 if (parser->num_template_parameter_lists == num_templates + 1)
21704 return true;
21705 /* If there are more template classes than parameter lists, we have
21706 something like:
21707
21708 template <class T> void S<T>::R<T>::f (); */
21709 if (parser->num_template_parameter_lists < num_templates)
21710 {
21711 if (declarator && !current_function_decl)
21712 error_at (location, "specializing member %<%T::%E%> "
21713 "requires %<template<>%> syntax",
21714 declarator->u.id.qualifying_scope,
21715 declarator->u.id.unqualified_name);
21716 else if (declarator)
21717 error_at (location, "invalid declaration of %<%T::%E%>",
21718 declarator->u.id.qualifying_scope,
21719 declarator->u.id.unqualified_name);
21720 else
21721 error_at (location, "too few template-parameter-lists");
21722 return false;
21723 }
21724 /* Otherwise, there are too many template parameter lists. We have
21725 something like:
21726
21727 template <class T> template <class U> void S::f(); */
21728 error_at (location, "too many template-parameter-lists");
21729 return false;
21730 }
21731
21732 /* Parse an optional `::' token indicating that the following name is
21733 from the global namespace. If so, PARSER->SCOPE is set to the
21734 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21735 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21736 Returns the new value of PARSER->SCOPE, if the `::' token is
21737 present, and NULL_TREE otherwise. */
21738
21739 static tree
21740 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21741 {
21742 cp_token *token;
21743
21744 /* Peek at the next token. */
21745 token = cp_lexer_peek_token (parser->lexer);
21746 /* If we're looking at a `::' token then we're starting from the
21747 global namespace, not our current location. */
21748 if (token->type == CPP_SCOPE)
21749 {
21750 /* Consume the `::' token. */
21751 cp_lexer_consume_token (parser->lexer);
21752 /* Set the SCOPE so that we know where to start the lookup. */
21753 parser->scope = global_namespace;
21754 parser->qualifying_scope = global_namespace;
21755 parser->object_scope = NULL_TREE;
21756
21757 return parser->scope;
21758 }
21759 else if (!current_scope_valid_p)
21760 {
21761 parser->scope = NULL_TREE;
21762 parser->qualifying_scope = NULL_TREE;
21763 parser->object_scope = NULL_TREE;
21764 }
21765
21766 return NULL_TREE;
21767 }
21768
21769 /* Returns TRUE if the upcoming token sequence is the start of a
21770 constructor declarator. If FRIEND_P is true, the declarator is
21771 preceded by the `friend' specifier. */
21772
21773 static bool
21774 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21775 {
21776 bool constructor_p;
21777 tree nested_name_specifier;
21778 cp_token *next_token;
21779
21780 /* The common case is that this is not a constructor declarator, so
21781 try to avoid doing lots of work if at all possible. It's not
21782 valid declare a constructor at function scope. */
21783 if (parser->in_function_body)
21784 return false;
21785 /* And only certain tokens can begin a constructor declarator. */
21786 next_token = cp_lexer_peek_token (parser->lexer);
21787 if (next_token->type != CPP_NAME
21788 && next_token->type != CPP_SCOPE
21789 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21790 && next_token->type != CPP_TEMPLATE_ID)
21791 return false;
21792
21793 /* Parse tentatively; we are going to roll back all of the tokens
21794 consumed here. */
21795 cp_parser_parse_tentatively (parser);
21796 /* Assume that we are looking at a constructor declarator. */
21797 constructor_p = true;
21798
21799 /* Look for the optional `::' operator. */
21800 cp_parser_global_scope_opt (parser,
21801 /*current_scope_valid_p=*/false);
21802 /* Look for the nested-name-specifier. */
21803 nested_name_specifier
21804 = (cp_parser_nested_name_specifier_opt (parser,
21805 /*typename_keyword_p=*/false,
21806 /*check_dependency_p=*/false,
21807 /*type_p=*/false,
21808 /*is_declaration=*/false));
21809 /* Outside of a class-specifier, there must be a
21810 nested-name-specifier. */
21811 if (!nested_name_specifier &&
21812 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21813 || friend_p))
21814 constructor_p = false;
21815 else if (nested_name_specifier == error_mark_node)
21816 constructor_p = false;
21817
21818 /* If we have a class scope, this is easy; DR 147 says that S::S always
21819 names the constructor, and no other qualified name could. */
21820 if (constructor_p && nested_name_specifier
21821 && CLASS_TYPE_P (nested_name_specifier))
21822 {
21823 tree id = cp_parser_unqualified_id (parser,
21824 /*template_keyword_p=*/false,
21825 /*check_dependency_p=*/false,
21826 /*declarator_p=*/true,
21827 /*optional_p=*/false);
21828 if (is_overloaded_fn (id))
21829 id = DECL_NAME (get_first_fn (id));
21830 if (!constructor_name_p (id, nested_name_specifier))
21831 constructor_p = false;
21832 }
21833 /* If we still think that this might be a constructor-declarator,
21834 look for a class-name. */
21835 else if (constructor_p)
21836 {
21837 /* If we have:
21838
21839 template <typename T> struct S {
21840 S();
21841 };
21842
21843 we must recognize that the nested `S' names a class. */
21844 tree type_decl;
21845 type_decl = cp_parser_class_name (parser,
21846 /*typename_keyword_p=*/false,
21847 /*template_keyword_p=*/false,
21848 none_type,
21849 /*check_dependency_p=*/false,
21850 /*class_head_p=*/false,
21851 /*is_declaration=*/false);
21852 /* If there was no class-name, then this is not a constructor. */
21853 constructor_p = !cp_parser_error_occurred (parser);
21854
21855 /* If we're still considering a constructor, we have to see a `(',
21856 to begin the parameter-declaration-clause, followed by either a
21857 `)', an `...', or a decl-specifier. We need to check for a
21858 type-specifier to avoid being fooled into thinking that:
21859
21860 S (f) (int);
21861
21862 is a constructor. (It is actually a function named `f' that
21863 takes one parameter (of type `int') and returns a value of type
21864 `S'. */
21865 if (constructor_p
21866 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21867 constructor_p = false;
21868
21869 if (constructor_p
21870 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21871 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21872 /* A parameter declaration begins with a decl-specifier,
21873 which is either the "attribute" keyword, a storage class
21874 specifier, or (usually) a type-specifier. */
21875 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21876 {
21877 tree type;
21878 tree pushed_scope = NULL_TREE;
21879 unsigned saved_num_template_parameter_lists;
21880
21881 /* Names appearing in the type-specifier should be looked up
21882 in the scope of the class. */
21883 if (current_class_type)
21884 type = NULL_TREE;
21885 else
21886 {
21887 type = TREE_TYPE (type_decl);
21888 if (TREE_CODE (type) == TYPENAME_TYPE)
21889 {
21890 type = resolve_typename_type (type,
21891 /*only_current_p=*/false);
21892 if (TREE_CODE (type) == TYPENAME_TYPE)
21893 {
21894 cp_parser_abort_tentative_parse (parser);
21895 return false;
21896 }
21897 }
21898 pushed_scope = push_scope (type);
21899 }
21900
21901 /* Inside the constructor parameter list, surrounding
21902 template-parameter-lists do not apply. */
21903 saved_num_template_parameter_lists
21904 = parser->num_template_parameter_lists;
21905 parser->num_template_parameter_lists = 0;
21906
21907 /* Look for the type-specifier. */
21908 cp_parser_type_specifier (parser,
21909 CP_PARSER_FLAGS_NONE,
21910 /*decl_specs=*/NULL,
21911 /*is_declarator=*/true,
21912 /*declares_class_or_enum=*/NULL,
21913 /*is_cv_qualifier=*/NULL);
21914
21915 parser->num_template_parameter_lists
21916 = saved_num_template_parameter_lists;
21917
21918 /* Leave the scope of the class. */
21919 if (pushed_scope)
21920 pop_scope (pushed_scope);
21921
21922 constructor_p = !cp_parser_error_occurred (parser);
21923 }
21924 }
21925
21926 /* We did not really want to consume any tokens. */
21927 cp_parser_abort_tentative_parse (parser);
21928
21929 return constructor_p;
21930 }
21931
21932 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21933 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21934 they must be performed once we are in the scope of the function.
21935
21936 Returns the function defined. */
21937
21938 static tree
21939 cp_parser_function_definition_from_specifiers_and_declarator
21940 (cp_parser* parser,
21941 cp_decl_specifier_seq *decl_specifiers,
21942 tree attributes,
21943 const cp_declarator *declarator)
21944 {
21945 tree fn;
21946 bool success_p;
21947
21948 /* Begin the function-definition. */
21949 success_p = start_function (decl_specifiers, declarator, attributes);
21950
21951 /* The things we're about to see are not directly qualified by any
21952 template headers we've seen thus far. */
21953 reset_specialization ();
21954
21955 /* If there were names looked up in the decl-specifier-seq that we
21956 did not check, check them now. We must wait until we are in the
21957 scope of the function to perform the checks, since the function
21958 might be a friend. */
21959 perform_deferred_access_checks (tf_warning_or_error);
21960
21961 if (!success_p)
21962 {
21963 /* Skip the entire function. */
21964 cp_parser_skip_to_end_of_block_or_statement (parser);
21965 fn = error_mark_node;
21966 }
21967 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21968 {
21969 /* Seen already, skip it. An error message has already been output. */
21970 cp_parser_skip_to_end_of_block_or_statement (parser);
21971 fn = current_function_decl;
21972 current_function_decl = NULL_TREE;
21973 /* If this is a function from a class, pop the nested class. */
21974 if (current_class_name)
21975 pop_nested_class ();
21976 }
21977 else
21978 {
21979 timevar_id_t tv;
21980 if (DECL_DECLARED_INLINE_P (current_function_decl))
21981 tv = TV_PARSE_INLINE;
21982 else
21983 tv = TV_PARSE_FUNC;
21984 timevar_push (tv);
21985 fn = cp_parser_function_definition_after_declarator (parser,
21986 /*inline_p=*/false);
21987 timevar_pop (tv);
21988 }
21989
21990 return fn;
21991 }
21992
21993 /* Parse the part of a function-definition that follows the
21994 declarator. INLINE_P is TRUE iff this function is an inline
21995 function defined within a class-specifier.
21996
21997 Returns the function defined. */
21998
21999 static tree
22000 cp_parser_function_definition_after_declarator (cp_parser* parser,
22001 bool inline_p)
22002 {
22003 tree fn;
22004 bool ctor_initializer_p = false;
22005 bool saved_in_unbraced_linkage_specification_p;
22006 bool saved_in_function_body;
22007 unsigned saved_num_template_parameter_lists;
22008 cp_token *token;
22009
22010 saved_in_function_body = parser->in_function_body;
22011 parser->in_function_body = true;
22012 /* If the next token is `return', then the code may be trying to
22013 make use of the "named return value" extension that G++ used to
22014 support. */
22015 token = cp_lexer_peek_token (parser->lexer);
22016 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22017 {
22018 /* Consume the `return' keyword. */
22019 cp_lexer_consume_token (parser->lexer);
22020 /* Look for the identifier that indicates what value is to be
22021 returned. */
22022 cp_parser_identifier (parser);
22023 /* Issue an error message. */
22024 error_at (token->location,
22025 "named return values are no longer supported");
22026 /* Skip tokens until we reach the start of the function body. */
22027 while (true)
22028 {
22029 cp_token *token = cp_lexer_peek_token (parser->lexer);
22030 if (token->type == CPP_OPEN_BRACE
22031 || token->type == CPP_EOF
22032 || token->type == CPP_PRAGMA_EOL)
22033 break;
22034 cp_lexer_consume_token (parser->lexer);
22035 }
22036 }
22037 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22038 anything declared inside `f'. */
22039 saved_in_unbraced_linkage_specification_p
22040 = parser->in_unbraced_linkage_specification_p;
22041 parser->in_unbraced_linkage_specification_p = false;
22042 /* Inside the function, surrounding template-parameter-lists do not
22043 apply. */
22044 saved_num_template_parameter_lists
22045 = parser->num_template_parameter_lists;
22046 parser->num_template_parameter_lists = 0;
22047
22048 start_lambda_scope (current_function_decl);
22049
22050 /* If the next token is `try', `__transaction_atomic', or
22051 `__transaction_relaxed`, then we are looking at either function-try-block
22052 or function-transaction-block. Note that all of these include the
22053 function-body. */
22054 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22055 ctor_initializer_p = cp_parser_function_transaction (parser,
22056 RID_TRANSACTION_ATOMIC);
22057 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22058 RID_TRANSACTION_RELAXED))
22059 ctor_initializer_p = cp_parser_function_transaction (parser,
22060 RID_TRANSACTION_RELAXED);
22061 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22062 ctor_initializer_p = cp_parser_function_try_block (parser);
22063 else
22064 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22065 (parser, /*in_function_try_block=*/false);
22066
22067 finish_lambda_scope ();
22068
22069 /* Finish the function. */
22070 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22071 (inline_p ? 2 : 0));
22072 /* Generate code for it, if necessary. */
22073 expand_or_defer_fn (fn);
22074 /* Restore the saved values. */
22075 parser->in_unbraced_linkage_specification_p
22076 = saved_in_unbraced_linkage_specification_p;
22077 parser->num_template_parameter_lists
22078 = saved_num_template_parameter_lists;
22079 parser->in_function_body = saved_in_function_body;
22080
22081 return fn;
22082 }
22083
22084 /* Parse a template-declaration, assuming that the `export' (and
22085 `extern') keywords, if present, has already been scanned. MEMBER_P
22086 is as for cp_parser_template_declaration. */
22087
22088 static void
22089 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22090 {
22091 tree decl = NULL_TREE;
22092 vec<deferred_access_check, va_gc> *checks;
22093 tree parameter_list;
22094 bool friend_p = false;
22095 bool need_lang_pop;
22096 cp_token *token;
22097
22098 /* Look for the `template' keyword. */
22099 token = cp_lexer_peek_token (parser->lexer);
22100 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22101 return;
22102
22103 /* And the `<'. */
22104 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22105 return;
22106 if (at_class_scope_p () && current_function_decl)
22107 {
22108 /* 14.5.2.2 [temp.mem]
22109
22110 A local class shall not have member templates. */
22111 error_at (token->location,
22112 "invalid declaration of member template in local class");
22113 cp_parser_skip_to_end_of_block_or_statement (parser);
22114 return;
22115 }
22116 /* [temp]
22117
22118 A template ... shall not have C linkage. */
22119 if (current_lang_name == lang_name_c)
22120 {
22121 error_at (token->location, "template with C linkage");
22122 /* Give it C++ linkage to avoid confusing other parts of the
22123 front end. */
22124 push_lang_context (lang_name_cplusplus);
22125 need_lang_pop = true;
22126 }
22127 else
22128 need_lang_pop = false;
22129
22130 /* We cannot perform access checks on the template parameter
22131 declarations until we know what is being declared, just as we
22132 cannot check the decl-specifier list. */
22133 push_deferring_access_checks (dk_deferred);
22134
22135 /* If the next token is `>', then we have an invalid
22136 specialization. Rather than complain about an invalid template
22137 parameter, issue an error message here. */
22138 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
22139 {
22140 cp_parser_error (parser, "invalid explicit specialization");
22141 begin_specialization ();
22142 parameter_list = NULL_TREE;
22143 }
22144 else
22145 {
22146 /* Parse the template parameters. */
22147 parameter_list = cp_parser_template_parameter_list (parser);
22148 }
22149
22150 /* Get the deferred access checks from the parameter list. These
22151 will be checked once we know what is being declared, as for a
22152 member template the checks must be performed in the scope of the
22153 class containing the member. */
22154 checks = get_deferred_access_checks ();
22155
22156 /* Look for the `>'. */
22157 cp_parser_skip_to_end_of_template_parameter_list (parser);
22158 /* We just processed one more parameter list. */
22159 ++parser->num_template_parameter_lists;
22160 /* If the next token is `template', there are more template
22161 parameters. */
22162 if (cp_lexer_next_token_is_keyword (parser->lexer,
22163 RID_TEMPLATE))
22164 cp_parser_template_declaration_after_export (parser, member_p);
22165 else if (cxx_dialect >= cxx0x
22166 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22167 decl = cp_parser_alias_declaration (parser);
22168 else
22169 {
22170 /* There are no access checks when parsing a template, as we do not
22171 know if a specialization will be a friend. */
22172 push_deferring_access_checks (dk_no_check);
22173 token = cp_lexer_peek_token (parser->lexer);
22174 decl = cp_parser_single_declaration (parser,
22175 checks,
22176 member_p,
22177 /*explicit_specialization_p=*/false,
22178 &friend_p);
22179 pop_deferring_access_checks ();
22180
22181 /* If this is a member template declaration, let the front
22182 end know. */
22183 if (member_p && !friend_p && decl)
22184 {
22185 if (TREE_CODE (decl) == TYPE_DECL)
22186 cp_parser_check_access_in_redeclaration (decl, token->location);
22187
22188 decl = finish_member_template_decl (decl);
22189 }
22190 else if (friend_p && decl
22191 && DECL_DECLARES_TYPE_P (decl))
22192 make_friend_class (current_class_type, TREE_TYPE (decl),
22193 /*complain=*/true);
22194 }
22195 /* We are done with the current parameter list. */
22196 --parser->num_template_parameter_lists;
22197
22198 pop_deferring_access_checks ();
22199
22200 /* Finish up. */
22201 finish_template_decl (parameter_list);
22202
22203 /* Check the template arguments for a literal operator template. */
22204 if (decl
22205 && DECL_DECLARES_FUNCTION_P (decl)
22206 && UDLIT_OPER_P (DECL_NAME (decl)))
22207 {
22208 bool ok = true;
22209 if (parameter_list == NULL_TREE)
22210 ok = false;
22211 else
22212 {
22213 int num_parms = TREE_VEC_LENGTH (parameter_list);
22214 if (num_parms == 1)
22215 {
22216 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
22217 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22218 if (TREE_TYPE (parm) != char_type_node
22219 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22220 ok = false;
22221 }
22222 else if (num_parms == 2 && cxx_dialect >= cxx1y)
22223 {
22224 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
22225 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
22226 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
22227 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22228 if (TREE_TYPE (parm) != TREE_TYPE (type)
22229 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22230 ok = false;
22231 }
22232 else
22233 ok = false;
22234 }
22235 if (!ok)
22236 error ("literal operator template %qD has invalid parameter list."
22237 " Expected non-type template argument pack <char...>"
22238 " or <typename CharT, CharT...>",
22239 decl);
22240 }
22241 /* Register member declarations. */
22242 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22243 finish_member_declaration (decl);
22244 /* For the erroneous case of a template with C linkage, we pushed an
22245 implicit C++ linkage scope; exit that scope now. */
22246 if (need_lang_pop)
22247 pop_lang_context ();
22248 /* If DECL is a function template, we must return to parse it later.
22249 (Even though there is no definition, there might be default
22250 arguments that need handling.) */
22251 if (member_p && decl
22252 && DECL_DECLARES_FUNCTION_P (decl))
22253 vec_safe_push (unparsed_funs_with_definitions, decl);
22254 }
22255
22256 /* Perform the deferred access checks from a template-parameter-list.
22257 CHECKS is a TREE_LIST of access checks, as returned by
22258 get_deferred_access_checks. */
22259
22260 static void
22261 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22262 {
22263 ++processing_template_parmlist;
22264 perform_access_checks (checks, tf_warning_or_error);
22265 --processing_template_parmlist;
22266 }
22267
22268 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22269 `function-definition' sequence that follows a template header.
22270 If MEMBER_P is true, this declaration appears in a class scope.
22271
22272 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22273 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22274
22275 static tree
22276 cp_parser_single_declaration (cp_parser* parser,
22277 vec<deferred_access_check, va_gc> *checks,
22278 bool member_p,
22279 bool explicit_specialization_p,
22280 bool* friend_p)
22281 {
22282 int declares_class_or_enum;
22283 tree decl = NULL_TREE;
22284 cp_decl_specifier_seq decl_specifiers;
22285 bool function_definition_p = false;
22286 cp_token *decl_spec_token_start;
22287
22288 /* This function is only used when processing a template
22289 declaration. */
22290 gcc_assert (innermost_scope_kind () == sk_template_parms
22291 || innermost_scope_kind () == sk_template_spec);
22292
22293 /* Defer access checks until we know what is being declared. */
22294 push_deferring_access_checks (dk_deferred);
22295
22296 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22297 alternative. */
22298 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22299 cp_parser_decl_specifier_seq (parser,
22300 CP_PARSER_FLAGS_OPTIONAL,
22301 &decl_specifiers,
22302 &declares_class_or_enum);
22303 if (friend_p)
22304 *friend_p = cp_parser_friend_p (&decl_specifiers);
22305
22306 /* There are no template typedefs. */
22307 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22308 {
22309 error_at (decl_spec_token_start->location,
22310 "template declaration of %<typedef%>");
22311 decl = error_mark_node;
22312 }
22313
22314 /* Gather up the access checks that occurred the
22315 decl-specifier-seq. */
22316 stop_deferring_access_checks ();
22317
22318 /* Check for the declaration of a template class. */
22319 if (declares_class_or_enum)
22320 {
22321 if (cp_parser_declares_only_class_p (parser))
22322 {
22323 decl = shadow_tag (&decl_specifiers);
22324
22325 /* In this case:
22326
22327 struct C {
22328 friend template <typename T> struct A<T>::B;
22329 };
22330
22331 A<T>::B will be represented by a TYPENAME_TYPE, and
22332 therefore not recognized by shadow_tag. */
22333 if (friend_p && *friend_p
22334 && !decl
22335 && decl_specifiers.type
22336 && TYPE_P (decl_specifiers.type))
22337 decl = decl_specifiers.type;
22338
22339 if (decl && decl != error_mark_node)
22340 decl = TYPE_NAME (decl);
22341 else
22342 decl = error_mark_node;
22343
22344 /* Perform access checks for template parameters. */
22345 cp_parser_perform_template_parameter_access_checks (checks);
22346 }
22347 }
22348
22349 /* Complain about missing 'typename' or other invalid type names. */
22350 if (!decl_specifiers.any_type_specifiers_p
22351 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22352 {
22353 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22354 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22355 the rest of this declaration. */
22356 decl = error_mark_node;
22357 goto out;
22358 }
22359
22360 /* If it's not a template class, try for a template function. If
22361 the next token is a `;', then this declaration does not declare
22362 anything. But, if there were errors in the decl-specifiers, then
22363 the error might well have come from an attempted class-specifier.
22364 In that case, there's no need to warn about a missing declarator. */
22365 if (!decl
22366 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22367 || decl_specifiers.type != error_mark_node))
22368 {
22369 decl = cp_parser_init_declarator (parser,
22370 &decl_specifiers,
22371 checks,
22372 /*function_definition_allowed_p=*/true,
22373 member_p,
22374 declares_class_or_enum,
22375 &function_definition_p,
22376 NULL);
22377
22378 /* 7.1.1-1 [dcl.stc]
22379
22380 A storage-class-specifier shall not be specified in an explicit
22381 specialization... */
22382 if (decl
22383 && explicit_specialization_p
22384 && decl_specifiers.storage_class != sc_none)
22385 {
22386 error_at (decl_spec_token_start->location,
22387 "explicit template specialization cannot have a storage class");
22388 decl = error_mark_node;
22389 }
22390
22391 if (decl && VAR_P (decl))
22392 check_template_variable (decl);
22393 }
22394
22395 /* Look for a trailing `;' after the declaration. */
22396 if (!function_definition_p
22397 && (decl == error_mark_node
22398 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22399 cp_parser_skip_to_end_of_block_or_statement (parser);
22400
22401 out:
22402 pop_deferring_access_checks ();
22403
22404 /* Clear any current qualification; whatever comes next is the start
22405 of something new. */
22406 parser->scope = NULL_TREE;
22407 parser->qualifying_scope = NULL_TREE;
22408 parser->object_scope = NULL_TREE;
22409
22410 return decl;
22411 }
22412
22413 /* Parse a cast-expression that is not the operand of a unary "&". */
22414
22415 static tree
22416 cp_parser_simple_cast_expression (cp_parser *parser)
22417 {
22418 return cp_parser_cast_expression (parser, /*address_p=*/false,
22419 /*cast_p=*/false, /*decltype*/false, NULL);
22420 }
22421
22422 /* Parse a functional cast to TYPE. Returns an expression
22423 representing the cast. */
22424
22425 static tree
22426 cp_parser_functional_cast (cp_parser* parser, tree type)
22427 {
22428 vec<tree, va_gc> *vec;
22429 tree expression_list;
22430 tree cast;
22431 bool nonconst_p;
22432
22433 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22434 {
22435 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22436 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22437 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22438 if (TREE_CODE (type) == TYPE_DECL)
22439 type = TREE_TYPE (type);
22440 return finish_compound_literal (type, expression_list,
22441 tf_warning_or_error);
22442 }
22443
22444
22445 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22446 /*cast_p=*/true,
22447 /*allow_expansion_p=*/true,
22448 /*non_constant_p=*/NULL);
22449 if (vec == NULL)
22450 expression_list = error_mark_node;
22451 else
22452 {
22453 expression_list = build_tree_list_vec (vec);
22454 release_tree_vector (vec);
22455 }
22456
22457 cast = build_functional_cast (type, expression_list,
22458 tf_warning_or_error);
22459 /* [expr.const]/1: In an integral constant expression "only type
22460 conversions to integral or enumeration type can be used". */
22461 if (TREE_CODE (type) == TYPE_DECL)
22462 type = TREE_TYPE (type);
22463 if (cast != error_mark_node
22464 && !cast_valid_in_integral_constant_expression_p (type)
22465 && cp_parser_non_integral_constant_expression (parser,
22466 NIC_CONSTRUCTOR))
22467 return error_mark_node;
22468 return cast;
22469 }
22470
22471 /* Save the tokens that make up the body of a member function defined
22472 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22473 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22474 specifiers applied to the declaration. Returns the FUNCTION_DECL
22475 for the member function. */
22476
22477 static tree
22478 cp_parser_save_member_function_body (cp_parser* parser,
22479 cp_decl_specifier_seq *decl_specifiers,
22480 cp_declarator *declarator,
22481 tree attributes)
22482 {
22483 cp_token *first;
22484 cp_token *last;
22485 tree fn;
22486
22487 /* Create the FUNCTION_DECL. */
22488 fn = grokmethod (decl_specifiers, declarator, attributes);
22489 /* If something went badly wrong, bail out now. */
22490 if (fn == error_mark_node)
22491 {
22492 /* If there's a function-body, skip it. */
22493 if (cp_parser_token_starts_function_definition_p
22494 (cp_lexer_peek_token (parser->lexer)))
22495 cp_parser_skip_to_end_of_block_or_statement (parser);
22496 return error_mark_node;
22497 }
22498
22499 /* Remember it, if there default args to post process. */
22500 cp_parser_save_default_args (parser, fn);
22501
22502 /* Save away the tokens that make up the body of the
22503 function. */
22504 first = parser->lexer->next_token;
22505 /* We can have braced-init-list mem-initializers before the fn body. */
22506 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22507 {
22508 cp_lexer_consume_token (parser->lexer);
22509 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22510 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22511 {
22512 /* cache_group will stop after an un-nested { } pair, too. */
22513 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22514 break;
22515
22516 /* variadic mem-inits have ... after the ')'. */
22517 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22518 cp_lexer_consume_token (parser->lexer);
22519 }
22520 }
22521 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22522 /* Handle function try blocks. */
22523 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22524 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22525 last = parser->lexer->next_token;
22526
22527 /* Save away the inline definition; we will process it when the
22528 class is complete. */
22529 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22530 DECL_PENDING_INLINE_P (fn) = 1;
22531
22532 /* We need to know that this was defined in the class, so that
22533 friend templates are handled correctly. */
22534 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22535
22536 /* Add FN to the queue of functions to be parsed later. */
22537 vec_safe_push (unparsed_funs_with_definitions, fn);
22538
22539 return fn;
22540 }
22541
22542 /* Save the tokens that make up the in-class initializer for a non-static
22543 data member. Returns a DEFAULT_ARG. */
22544
22545 static tree
22546 cp_parser_save_nsdmi (cp_parser* parser)
22547 {
22548 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22549 }
22550
22551 /* Parse a template-argument-list, as well as the trailing ">" (but
22552 not the opening "<"). See cp_parser_template_argument_list for the
22553 return value. */
22554
22555 static tree
22556 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22557 {
22558 tree arguments;
22559 tree saved_scope;
22560 tree saved_qualifying_scope;
22561 tree saved_object_scope;
22562 bool saved_greater_than_is_operator_p;
22563 int saved_unevaluated_operand;
22564 int saved_inhibit_evaluation_warnings;
22565
22566 /* [temp.names]
22567
22568 When parsing a template-id, the first non-nested `>' is taken as
22569 the end of the template-argument-list rather than a greater-than
22570 operator. */
22571 saved_greater_than_is_operator_p
22572 = parser->greater_than_is_operator_p;
22573 parser->greater_than_is_operator_p = false;
22574 /* Parsing the argument list may modify SCOPE, so we save it
22575 here. */
22576 saved_scope = parser->scope;
22577 saved_qualifying_scope = parser->qualifying_scope;
22578 saved_object_scope = parser->object_scope;
22579 /* We need to evaluate the template arguments, even though this
22580 template-id may be nested within a "sizeof". */
22581 saved_unevaluated_operand = cp_unevaluated_operand;
22582 cp_unevaluated_operand = 0;
22583 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22584 c_inhibit_evaluation_warnings = 0;
22585 /* Parse the template-argument-list itself. */
22586 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22587 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22588 arguments = NULL_TREE;
22589 else
22590 arguments = cp_parser_template_argument_list (parser);
22591 /* Look for the `>' that ends the template-argument-list. If we find
22592 a '>>' instead, it's probably just a typo. */
22593 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22594 {
22595 if (cxx_dialect != cxx98)
22596 {
22597 /* In C++0x, a `>>' in a template argument list or cast
22598 expression is considered to be two separate `>'
22599 tokens. So, change the current token to a `>', but don't
22600 consume it: it will be consumed later when the outer
22601 template argument list (or cast expression) is parsed.
22602 Note that this replacement of `>' for `>>' is necessary
22603 even if we are parsing tentatively: in the tentative
22604 case, after calling
22605 cp_parser_enclosed_template_argument_list we will always
22606 throw away all of the template arguments and the first
22607 closing `>', either because the template argument list
22608 was erroneous or because we are replacing those tokens
22609 with a CPP_TEMPLATE_ID token. The second `>' (which will
22610 not have been thrown away) is needed either to close an
22611 outer template argument list or to complete a new-style
22612 cast. */
22613 cp_token *token = cp_lexer_peek_token (parser->lexer);
22614 token->type = CPP_GREATER;
22615 }
22616 else if (!saved_greater_than_is_operator_p)
22617 {
22618 /* If we're in a nested template argument list, the '>>' has
22619 to be a typo for '> >'. We emit the error message, but we
22620 continue parsing and we push a '>' as next token, so that
22621 the argument list will be parsed correctly. Note that the
22622 global source location is still on the token before the
22623 '>>', so we need to say explicitly where we want it. */
22624 cp_token *token = cp_lexer_peek_token (parser->lexer);
22625 error_at (token->location, "%<>>%> should be %<> >%> "
22626 "within a nested template argument list");
22627
22628 token->type = CPP_GREATER;
22629 }
22630 else
22631 {
22632 /* If this is not a nested template argument list, the '>>'
22633 is a typo for '>'. Emit an error message and continue.
22634 Same deal about the token location, but here we can get it
22635 right by consuming the '>>' before issuing the diagnostic. */
22636 cp_token *token = cp_lexer_consume_token (parser->lexer);
22637 error_at (token->location,
22638 "spurious %<>>%>, use %<>%> to terminate "
22639 "a template argument list");
22640 }
22641 }
22642 else
22643 cp_parser_skip_to_end_of_template_parameter_list (parser);
22644 /* The `>' token might be a greater-than operator again now. */
22645 parser->greater_than_is_operator_p
22646 = saved_greater_than_is_operator_p;
22647 /* Restore the SAVED_SCOPE. */
22648 parser->scope = saved_scope;
22649 parser->qualifying_scope = saved_qualifying_scope;
22650 parser->object_scope = saved_object_scope;
22651 cp_unevaluated_operand = saved_unevaluated_operand;
22652 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22653
22654 return arguments;
22655 }
22656
22657 /* MEMBER_FUNCTION is a member function, or a friend. If default
22658 arguments, or the body of the function have not yet been parsed,
22659 parse them now. */
22660
22661 static void
22662 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22663 {
22664 timevar_push (TV_PARSE_INMETH);
22665 /* If this member is a template, get the underlying
22666 FUNCTION_DECL. */
22667 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22668 member_function = DECL_TEMPLATE_RESULT (member_function);
22669
22670 /* There should not be any class definitions in progress at this
22671 point; the bodies of members are only parsed outside of all class
22672 definitions. */
22673 gcc_assert (parser->num_classes_being_defined == 0);
22674 /* While we're parsing the member functions we might encounter more
22675 classes. We want to handle them right away, but we don't want
22676 them getting mixed up with functions that are currently in the
22677 queue. */
22678 push_unparsed_function_queues (parser);
22679
22680 /* Make sure that any template parameters are in scope. */
22681 maybe_begin_member_template_processing (member_function);
22682
22683 /* If the body of the function has not yet been parsed, parse it
22684 now. */
22685 if (DECL_PENDING_INLINE_P (member_function))
22686 {
22687 tree function_scope;
22688 cp_token_cache *tokens;
22689
22690 /* The function is no longer pending; we are processing it. */
22691 tokens = DECL_PENDING_INLINE_INFO (member_function);
22692 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22693 DECL_PENDING_INLINE_P (member_function) = 0;
22694
22695 /* If this is a local class, enter the scope of the containing
22696 function. */
22697 function_scope = current_function_decl;
22698 if (function_scope)
22699 push_function_context ();
22700
22701 /* Push the body of the function onto the lexer stack. */
22702 cp_parser_push_lexer_for_tokens (parser, tokens);
22703
22704 /* Let the front end know that we going to be defining this
22705 function. */
22706 start_preparsed_function (member_function, NULL_TREE,
22707 SF_PRE_PARSED | SF_INCLASS_INLINE);
22708
22709 /* Don't do access checking if it is a templated function. */
22710 if (processing_template_decl)
22711 push_deferring_access_checks (dk_no_check);
22712
22713 /* Now, parse the body of the function. */
22714 cp_parser_function_definition_after_declarator (parser,
22715 /*inline_p=*/true);
22716
22717 if (processing_template_decl)
22718 pop_deferring_access_checks ();
22719
22720 /* Leave the scope of the containing function. */
22721 if (function_scope)
22722 pop_function_context ();
22723 cp_parser_pop_lexer (parser);
22724 }
22725
22726 /* Remove any template parameters from the symbol table. */
22727 maybe_end_member_template_processing ();
22728
22729 /* Restore the queue. */
22730 pop_unparsed_function_queues (parser);
22731 timevar_pop (TV_PARSE_INMETH);
22732 }
22733
22734 /* If DECL contains any default args, remember it on the unparsed
22735 functions queue. */
22736
22737 static void
22738 cp_parser_save_default_args (cp_parser* parser, tree decl)
22739 {
22740 tree probe;
22741
22742 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22743 probe;
22744 probe = TREE_CHAIN (probe))
22745 if (TREE_PURPOSE (probe))
22746 {
22747 cp_default_arg_entry entry = {current_class_type, decl};
22748 vec_safe_push (unparsed_funs_with_default_args, entry);
22749 break;
22750 }
22751 }
22752
22753 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22754 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22755 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22756 from the parameter-type-list. */
22757
22758 static tree
22759 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22760 tree default_arg, tree parmtype)
22761 {
22762 cp_token_cache *tokens;
22763 tree parsed_arg;
22764 bool dummy;
22765
22766 if (default_arg == error_mark_node)
22767 return error_mark_node;
22768
22769 /* Push the saved tokens for the default argument onto the parser's
22770 lexer stack. */
22771 tokens = DEFARG_TOKENS (default_arg);
22772 cp_parser_push_lexer_for_tokens (parser, tokens);
22773
22774 start_lambda_scope (decl);
22775
22776 /* Parse the default argument. */
22777 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22778 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22779 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22780
22781 finish_lambda_scope ();
22782
22783 if (parsed_arg == error_mark_node)
22784 cp_parser_skip_to_end_of_statement (parser);
22785
22786 if (!processing_template_decl)
22787 {
22788 /* In a non-template class, check conversions now. In a template,
22789 we'll wait and instantiate these as needed. */
22790 if (TREE_CODE (decl) == PARM_DECL)
22791 parsed_arg = check_default_argument (parmtype, parsed_arg);
22792 else
22793 {
22794 int flags = LOOKUP_IMPLICIT;
22795 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22796 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22797 flags = LOOKUP_NORMAL;
22798 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22799 }
22800 }
22801
22802 /* If the token stream has not been completely used up, then
22803 there was extra junk after the end of the default
22804 argument. */
22805 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22806 {
22807 if (TREE_CODE (decl) == PARM_DECL)
22808 cp_parser_error (parser, "expected %<,%>");
22809 else
22810 cp_parser_error (parser, "expected %<;%>");
22811 }
22812
22813 /* Revert to the main lexer. */
22814 cp_parser_pop_lexer (parser);
22815
22816 return parsed_arg;
22817 }
22818
22819 /* FIELD is a non-static data member with an initializer which we saved for
22820 later; parse it now. */
22821
22822 static void
22823 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22824 {
22825 tree def;
22826
22827 push_unparsed_function_queues (parser);
22828 def = cp_parser_late_parse_one_default_arg (parser, field,
22829 DECL_INITIAL (field),
22830 NULL_TREE);
22831 pop_unparsed_function_queues (parser);
22832
22833 DECL_INITIAL (field) = def;
22834 }
22835
22836 /* FN is a FUNCTION_DECL which may contains a parameter with an
22837 unparsed DEFAULT_ARG. Parse the default args now. This function
22838 assumes that the current scope is the scope in which the default
22839 argument should be processed. */
22840
22841 static void
22842 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22843 {
22844 bool saved_local_variables_forbidden_p;
22845 tree parm, parmdecl;
22846
22847 /* While we're parsing the default args, we might (due to the
22848 statement expression extension) encounter more classes. We want
22849 to handle them right away, but we don't want them getting mixed
22850 up with default args that are currently in the queue. */
22851 push_unparsed_function_queues (parser);
22852
22853 /* Local variable names (and the `this' keyword) may not appear
22854 in a default argument. */
22855 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22856 parser->local_variables_forbidden_p = true;
22857
22858 push_defarg_context (fn);
22859
22860 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22861 parmdecl = DECL_ARGUMENTS (fn);
22862 parm && parm != void_list_node;
22863 parm = TREE_CHAIN (parm),
22864 parmdecl = DECL_CHAIN (parmdecl))
22865 {
22866 tree default_arg = TREE_PURPOSE (parm);
22867 tree parsed_arg;
22868 vec<tree, va_gc> *insts;
22869 tree copy;
22870 unsigned ix;
22871
22872 if (!default_arg)
22873 continue;
22874
22875 if (TREE_CODE (default_arg) != DEFAULT_ARG)
22876 /* This can happen for a friend declaration for a function
22877 already declared with default arguments. */
22878 continue;
22879
22880 parsed_arg
22881 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22882 default_arg,
22883 TREE_VALUE (parm));
22884 if (parsed_arg == error_mark_node)
22885 {
22886 continue;
22887 }
22888
22889 TREE_PURPOSE (parm) = parsed_arg;
22890
22891 /* Update any instantiations we've already created. */
22892 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22893 vec_safe_iterate (insts, ix, &copy); ix++)
22894 TREE_PURPOSE (copy) = parsed_arg;
22895 }
22896
22897 pop_defarg_context ();
22898
22899 /* Make sure no default arg is missing. */
22900 check_default_args (fn);
22901
22902 /* Restore the state of local_variables_forbidden_p. */
22903 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22904
22905 /* Restore the queue. */
22906 pop_unparsed_function_queues (parser);
22907 }
22908
22909 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
22910
22911 sizeof ... ( identifier )
22912
22913 where the 'sizeof' token has already been consumed. */
22914
22915 static tree
22916 cp_parser_sizeof_pack (cp_parser *parser)
22917 {
22918 /* Consume the `...'. */
22919 cp_lexer_consume_token (parser->lexer);
22920 maybe_warn_variadic_templates ();
22921
22922 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
22923 if (paren)
22924 cp_lexer_consume_token (parser->lexer);
22925 else
22926 permerror (cp_lexer_peek_token (parser->lexer)->location,
22927 "%<sizeof...%> argument must be surrounded by parentheses");
22928
22929 cp_token *token = cp_lexer_peek_token (parser->lexer);
22930 tree name = cp_parser_identifier (parser);
22931 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
22932 if (expr == error_mark_node)
22933 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
22934 token->location);
22935 if (TREE_CODE (expr) == TYPE_DECL)
22936 expr = TREE_TYPE (expr);
22937 else if (TREE_CODE (expr) == CONST_DECL)
22938 expr = DECL_INITIAL (expr);
22939 expr = make_pack_expansion (expr);
22940
22941 if (paren)
22942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22943
22944 return expr;
22945 }
22946
22947 /* Parse the operand of `sizeof' (or a similar operator). Returns
22948 either a TYPE or an expression, depending on the form of the
22949 input. The KEYWORD indicates which kind of expression we have
22950 encountered. */
22951
22952 static tree
22953 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22954 {
22955 tree expr = NULL_TREE;
22956 const char *saved_message;
22957 char *tmp;
22958 bool saved_integral_constant_expression_p;
22959 bool saved_non_integral_constant_expression_p;
22960
22961 /* If it's a `...', then we are computing the length of a parameter
22962 pack. */
22963 if (keyword == RID_SIZEOF
22964 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22965 return cp_parser_sizeof_pack (parser);
22966
22967 /* Types cannot be defined in a `sizeof' expression. Save away the
22968 old message. */
22969 saved_message = parser->type_definition_forbidden_message;
22970 /* And create the new one. */
22971 tmp = concat ("types may not be defined in %<",
22972 IDENTIFIER_POINTER (ridpointers[keyword]),
22973 "%> expressions", NULL);
22974 parser->type_definition_forbidden_message = tmp;
22975
22976 /* The restrictions on constant-expressions do not apply inside
22977 sizeof expressions. */
22978 saved_integral_constant_expression_p
22979 = parser->integral_constant_expression_p;
22980 saved_non_integral_constant_expression_p
22981 = parser->non_integral_constant_expression_p;
22982 parser->integral_constant_expression_p = false;
22983
22984 /* Do not actually evaluate the expression. */
22985 ++cp_unevaluated_operand;
22986 ++c_inhibit_evaluation_warnings;
22987 /* If it's a `(', then we might be looking at the type-id
22988 construction. */
22989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22990 {
22991 tree type;
22992 bool saved_in_type_id_in_expr_p;
22993
22994 /* We can't be sure yet whether we're looking at a type-id or an
22995 expression. */
22996 cp_parser_parse_tentatively (parser);
22997 /* Consume the `('. */
22998 cp_lexer_consume_token (parser->lexer);
22999 /* Parse the type-id. */
23000 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23001 parser->in_type_id_in_expr_p = true;
23002 type = cp_parser_type_id (parser);
23003 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23004 /* Now, look for the trailing `)'. */
23005 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23006 /* If all went well, then we're done. */
23007 if (cp_parser_parse_definitely (parser))
23008 {
23009 cp_decl_specifier_seq decl_specs;
23010
23011 /* Build a trivial decl-specifier-seq. */
23012 clear_decl_specs (&decl_specs);
23013 decl_specs.type = type;
23014
23015 /* Call grokdeclarator to figure out what type this is. */
23016 expr = grokdeclarator (NULL,
23017 &decl_specs,
23018 TYPENAME,
23019 /*initialized=*/0,
23020 /*attrlist=*/NULL);
23021 }
23022 }
23023
23024 /* If the type-id production did not work out, then we must be
23025 looking at the unary-expression production. */
23026 if (!expr)
23027 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23028 /*cast_p=*/false, NULL);
23029
23030 /* Go back to evaluating expressions. */
23031 --cp_unevaluated_operand;
23032 --c_inhibit_evaluation_warnings;
23033
23034 /* Free the message we created. */
23035 free (tmp);
23036 /* And restore the old one. */
23037 parser->type_definition_forbidden_message = saved_message;
23038 parser->integral_constant_expression_p
23039 = saved_integral_constant_expression_p;
23040 parser->non_integral_constant_expression_p
23041 = saved_non_integral_constant_expression_p;
23042
23043 return expr;
23044 }
23045
23046 /* If the current declaration has no declarator, return true. */
23047
23048 static bool
23049 cp_parser_declares_only_class_p (cp_parser *parser)
23050 {
23051 /* If the next token is a `;' or a `,' then there is no
23052 declarator. */
23053 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23054 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23055 }
23056
23057 /* Update the DECL_SPECS to reflect the storage class indicated by
23058 KEYWORD. */
23059
23060 static void
23061 cp_parser_set_storage_class (cp_parser *parser,
23062 cp_decl_specifier_seq *decl_specs,
23063 enum rid keyword,
23064 cp_token *token)
23065 {
23066 cp_storage_class storage_class;
23067
23068 if (parser->in_unbraced_linkage_specification_p)
23069 {
23070 error_at (token->location, "invalid use of %qD in linkage specification",
23071 ridpointers[keyword]);
23072 return;
23073 }
23074 else if (decl_specs->storage_class != sc_none)
23075 {
23076 decl_specs->conflicting_specifiers_p = true;
23077 return;
23078 }
23079
23080 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
23081 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
23082 && decl_specs->gnu_thread_keyword_p)
23083 {
23084 pedwarn (decl_specs->locations[ds_thread], 0,
23085 "%<__thread%> before %qD", ridpointers[keyword]);
23086 }
23087
23088 switch (keyword)
23089 {
23090 case RID_AUTO:
23091 storage_class = sc_auto;
23092 break;
23093 case RID_REGISTER:
23094 storage_class = sc_register;
23095 break;
23096 case RID_STATIC:
23097 storage_class = sc_static;
23098 break;
23099 case RID_EXTERN:
23100 storage_class = sc_extern;
23101 break;
23102 case RID_MUTABLE:
23103 storage_class = sc_mutable;
23104 break;
23105 default:
23106 gcc_unreachable ();
23107 }
23108 decl_specs->storage_class = storage_class;
23109 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
23110
23111 /* A storage class specifier cannot be applied alongside a typedef
23112 specifier. If there is a typedef specifier present then set
23113 conflicting_specifiers_p which will trigger an error later
23114 on in grokdeclarator. */
23115 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
23116 decl_specs->conflicting_specifiers_p = true;
23117 }
23118
23119 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
23120 is true, the type is a class or enum definition. */
23121
23122 static void
23123 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
23124 tree type_spec,
23125 cp_token *token,
23126 bool type_definition_p)
23127 {
23128 decl_specs->any_specifiers_p = true;
23129
23130 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
23131 (with, for example, in "typedef int wchar_t;") we remember that
23132 this is what happened. In system headers, we ignore these
23133 declarations so that G++ can work with system headers that are not
23134 C++-safe. */
23135 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
23136 && !type_definition_p
23137 && (type_spec == boolean_type_node
23138 || type_spec == char16_type_node
23139 || type_spec == char32_type_node
23140 || type_spec == wchar_type_node)
23141 && (decl_specs->type
23142 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
23143 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
23144 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
23145 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
23146 {
23147 decl_specs->redefined_builtin_type = type_spec;
23148 set_and_check_decl_spec_loc (decl_specs,
23149 ds_redefined_builtin_type_spec,
23150 token);
23151 if (!decl_specs->type)
23152 {
23153 decl_specs->type = type_spec;
23154 decl_specs->type_definition_p = false;
23155 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
23156 }
23157 }
23158 else if (decl_specs->type)
23159 decl_specs->multiple_types_p = true;
23160 else
23161 {
23162 decl_specs->type = type_spec;
23163 decl_specs->type_definition_p = type_definition_p;
23164 decl_specs->redefined_builtin_type = NULL_TREE;
23165 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
23166 }
23167 }
23168
23169 /* True iff TOKEN is the GNU keyword __thread. */
23170
23171 static bool
23172 token_is__thread (cp_token *token)
23173 {
23174 gcc_assert (token->keyword == RID_THREAD);
23175 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
23176 }
23177
23178 /* Set the location for a declarator specifier and check if it is
23179 duplicated.
23180
23181 DECL_SPECS is the sequence of declarator specifiers onto which to
23182 set the location.
23183
23184 DS is the single declarator specifier to set which location is to
23185 be set onto the existing sequence of declarators.
23186
23187 LOCATION is the location for the declarator specifier to
23188 consider. */
23189
23190 static void
23191 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
23192 cp_decl_spec ds, cp_token *token)
23193 {
23194 gcc_assert (ds < ds_last);
23195
23196 if (decl_specs == NULL)
23197 return;
23198
23199 source_location location = token->location;
23200
23201 if (decl_specs->locations[ds] == 0)
23202 {
23203 decl_specs->locations[ds] = location;
23204 if (ds == ds_thread)
23205 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
23206 }
23207 else
23208 {
23209 if (ds == ds_long)
23210 {
23211 if (decl_specs->locations[ds_long_long] != 0)
23212 error_at (location,
23213 "%<long long long%> is too long for GCC");
23214 else
23215 {
23216 decl_specs->locations[ds_long_long] = location;
23217 pedwarn_cxx98 (location,
23218 OPT_Wlong_long,
23219 "ISO C++ 1998 does not support %<long long%>");
23220 }
23221 }
23222 else if (ds == ds_thread)
23223 {
23224 bool gnu = token_is__thread (token);
23225 if (gnu != decl_specs->gnu_thread_keyword_p)
23226 error_at (location,
23227 "both %<__thread%> and %<thread_local%> specified");
23228 else
23229 error_at (location, "duplicate %qD", token->u.value);
23230 }
23231 else
23232 {
23233 static const char *const decl_spec_names[] = {
23234 "signed",
23235 "unsigned",
23236 "short",
23237 "long",
23238 "const",
23239 "volatile",
23240 "restrict",
23241 "inline",
23242 "virtual",
23243 "explicit",
23244 "friend",
23245 "typedef",
23246 "using",
23247 "constexpr",
23248 "__complex"
23249 };
23250 error_at (location,
23251 "duplicate %qs", decl_spec_names[ds]);
23252 }
23253 }
23254 }
23255
23256 /* Return true iff the declarator specifier DS is present in the
23257 sequence of declarator specifiers DECL_SPECS. */
23258
23259 bool
23260 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23261 cp_decl_spec ds)
23262 {
23263 gcc_assert (ds < ds_last);
23264
23265 if (decl_specs == NULL)
23266 return false;
23267
23268 return decl_specs->locations[ds] != 0;
23269 }
23270
23271 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23272 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23273
23274 static bool
23275 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23276 {
23277 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23278 }
23279
23280 /* Issue an error message indicating that TOKEN_DESC was expected.
23281 If KEYWORD is true, it indicated this function is called by
23282 cp_parser_require_keword and the required token can only be
23283 a indicated keyword. */
23284
23285 static void
23286 cp_parser_required_error (cp_parser *parser,
23287 required_token token_desc,
23288 bool keyword)
23289 {
23290 switch (token_desc)
23291 {
23292 case RT_NEW:
23293 cp_parser_error (parser, "expected %<new%>");
23294 return;
23295 case RT_DELETE:
23296 cp_parser_error (parser, "expected %<delete%>");
23297 return;
23298 case RT_RETURN:
23299 cp_parser_error (parser, "expected %<return%>");
23300 return;
23301 case RT_WHILE:
23302 cp_parser_error (parser, "expected %<while%>");
23303 return;
23304 case RT_EXTERN:
23305 cp_parser_error (parser, "expected %<extern%>");
23306 return;
23307 case RT_STATIC_ASSERT:
23308 cp_parser_error (parser, "expected %<static_assert%>");
23309 return;
23310 case RT_DECLTYPE:
23311 cp_parser_error (parser, "expected %<decltype%>");
23312 return;
23313 case RT_OPERATOR:
23314 cp_parser_error (parser, "expected %<operator%>");
23315 return;
23316 case RT_CLASS:
23317 cp_parser_error (parser, "expected %<class%>");
23318 return;
23319 case RT_TEMPLATE:
23320 cp_parser_error (parser, "expected %<template%>");
23321 return;
23322 case RT_NAMESPACE:
23323 cp_parser_error (parser, "expected %<namespace%>");
23324 return;
23325 case RT_USING:
23326 cp_parser_error (parser, "expected %<using%>");
23327 return;
23328 case RT_ASM:
23329 cp_parser_error (parser, "expected %<asm%>");
23330 return;
23331 case RT_TRY:
23332 cp_parser_error (parser, "expected %<try%>");
23333 return;
23334 case RT_CATCH:
23335 cp_parser_error (parser, "expected %<catch%>");
23336 return;
23337 case RT_THROW:
23338 cp_parser_error (parser, "expected %<throw%>");
23339 return;
23340 case RT_LABEL:
23341 cp_parser_error (parser, "expected %<__label__%>");
23342 return;
23343 case RT_AT_TRY:
23344 cp_parser_error (parser, "expected %<@try%>");
23345 return;
23346 case RT_AT_SYNCHRONIZED:
23347 cp_parser_error (parser, "expected %<@synchronized%>");
23348 return;
23349 case RT_AT_THROW:
23350 cp_parser_error (parser, "expected %<@throw%>");
23351 return;
23352 case RT_TRANSACTION_ATOMIC:
23353 cp_parser_error (parser, "expected %<__transaction_atomic%>");
23354 return;
23355 case RT_TRANSACTION_RELAXED:
23356 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23357 return;
23358 default:
23359 break;
23360 }
23361 if (!keyword)
23362 {
23363 switch (token_desc)
23364 {
23365 case RT_SEMICOLON:
23366 cp_parser_error (parser, "expected %<;%>");
23367 return;
23368 case RT_OPEN_PAREN:
23369 cp_parser_error (parser, "expected %<(%>");
23370 return;
23371 case RT_CLOSE_BRACE:
23372 cp_parser_error (parser, "expected %<}%>");
23373 return;
23374 case RT_OPEN_BRACE:
23375 cp_parser_error (parser, "expected %<{%>");
23376 return;
23377 case RT_CLOSE_SQUARE:
23378 cp_parser_error (parser, "expected %<]%>");
23379 return;
23380 case RT_OPEN_SQUARE:
23381 cp_parser_error (parser, "expected %<[%>");
23382 return;
23383 case RT_COMMA:
23384 cp_parser_error (parser, "expected %<,%>");
23385 return;
23386 case RT_SCOPE:
23387 cp_parser_error (parser, "expected %<::%>");
23388 return;
23389 case RT_LESS:
23390 cp_parser_error (parser, "expected %<<%>");
23391 return;
23392 case RT_GREATER:
23393 cp_parser_error (parser, "expected %<>%>");
23394 return;
23395 case RT_EQ:
23396 cp_parser_error (parser, "expected %<=%>");
23397 return;
23398 case RT_ELLIPSIS:
23399 cp_parser_error (parser, "expected %<...%>");
23400 return;
23401 case RT_MULT:
23402 cp_parser_error (parser, "expected %<*%>");
23403 return;
23404 case RT_COMPL:
23405 cp_parser_error (parser, "expected %<~%>");
23406 return;
23407 case RT_COLON:
23408 cp_parser_error (parser, "expected %<:%>");
23409 return;
23410 case RT_COLON_SCOPE:
23411 cp_parser_error (parser, "expected %<:%> or %<::%>");
23412 return;
23413 case RT_CLOSE_PAREN:
23414 cp_parser_error (parser, "expected %<)%>");
23415 return;
23416 case RT_COMMA_CLOSE_PAREN:
23417 cp_parser_error (parser, "expected %<,%> or %<)%>");
23418 return;
23419 case RT_PRAGMA_EOL:
23420 cp_parser_error (parser, "expected end of line");
23421 return;
23422 case RT_NAME:
23423 cp_parser_error (parser, "expected identifier");
23424 return;
23425 case RT_SELECT:
23426 cp_parser_error (parser, "expected selection-statement");
23427 return;
23428 case RT_INTERATION:
23429 cp_parser_error (parser, "expected iteration-statement");
23430 return;
23431 case RT_JUMP:
23432 cp_parser_error (parser, "expected jump-statement");
23433 return;
23434 case RT_CLASS_KEY:
23435 cp_parser_error (parser, "expected class-key");
23436 return;
23437 case RT_CLASS_TYPENAME_TEMPLATE:
23438 cp_parser_error (parser,
23439 "expected %<class%>, %<typename%>, or %<template%>");
23440 return;
23441 default:
23442 gcc_unreachable ();
23443 }
23444 }
23445 else
23446 gcc_unreachable ();
23447 }
23448
23449
23450
23451 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23452 issue an error message indicating that TOKEN_DESC was expected.
23453
23454 Returns the token consumed, if the token had the appropriate type.
23455 Otherwise, returns NULL. */
23456
23457 static cp_token *
23458 cp_parser_require (cp_parser* parser,
23459 enum cpp_ttype type,
23460 required_token token_desc)
23461 {
23462 if (cp_lexer_next_token_is (parser->lexer, type))
23463 return cp_lexer_consume_token (parser->lexer);
23464 else
23465 {
23466 /* Output the MESSAGE -- unless we're parsing tentatively. */
23467 if (!cp_parser_simulate_error (parser))
23468 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23469 return NULL;
23470 }
23471 }
23472
23473 /* An error message is produced if the next token is not '>'.
23474 All further tokens are skipped until the desired token is
23475 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23476
23477 static void
23478 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23479 {
23480 /* Current level of '< ... >'. */
23481 unsigned level = 0;
23482 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23483 unsigned nesting_depth = 0;
23484
23485 /* Are we ready, yet? If not, issue error message. */
23486 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23487 return;
23488
23489 /* Skip tokens until the desired token is found. */
23490 while (true)
23491 {
23492 /* Peek at the next token. */
23493 switch (cp_lexer_peek_token (parser->lexer)->type)
23494 {
23495 case CPP_LESS:
23496 if (!nesting_depth)
23497 ++level;
23498 break;
23499
23500 case CPP_RSHIFT:
23501 if (cxx_dialect == cxx98)
23502 /* C++0x views the `>>' operator as two `>' tokens, but
23503 C++98 does not. */
23504 break;
23505 else if (!nesting_depth && level-- == 0)
23506 {
23507 /* We've hit a `>>' where the first `>' closes the
23508 template argument list, and the second `>' is
23509 spurious. Just consume the `>>' and stop; we've
23510 already produced at least one error. */
23511 cp_lexer_consume_token (parser->lexer);
23512 return;
23513 }
23514 /* Fall through for C++0x, so we handle the second `>' in
23515 the `>>'. */
23516
23517 case CPP_GREATER:
23518 if (!nesting_depth && level-- == 0)
23519 {
23520 /* We've reached the token we want, consume it and stop. */
23521 cp_lexer_consume_token (parser->lexer);
23522 return;
23523 }
23524 break;
23525
23526 case CPP_OPEN_PAREN:
23527 case CPP_OPEN_SQUARE:
23528 ++nesting_depth;
23529 break;
23530
23531 case CPP_CLOSE_PAREN:
23532 case CPP_CLOSE_SQUARE:
23533 if (nesting_depth-- == 0)
23534 return;
23535 break;
23536
23537 case CPP_EOF:
23538 case CPP_PRAGMA_EOL:
23539 case CPP_SEMICOLON:
23540 case CPP_OPEN_BRACE:
23541 case CPP_CLOSE_BRACE:
23542 /* The '>' was probably forgotten, don't look further. */
23543 return;
23544
23545 default:
23546 break;
23547 }
23548
23549 /* Consume this token. */
23550 cp_lexer_consume_token (parser->lexer);
23551 }
23552 }
23553
23554 /* If the next token is the indicated keyword, consume it. Otherwise,
23555 issue an error message indicating that TOKEN_DESC was expected.
23556
23557 Returns the token consumed, if the token had the appropriate type.
23558 Otherwise, returns NULL. */
23559
23560 static cp_token *
23561 cp_parser_require_keyword (cp_parser* parser,
23562 enum rid keyword,
23563 required_token token_desc)
23564 {
23565 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23566
23567 if (token && token->keyword != keyword)
23568 {
23569 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23570 return NULL;
23571 }
23572
23573 return token;
23574 }
23575
23576 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23577 function-definition. */
23578
23579 static bool
23580 cp_parser_token_starts_function_definition_p (cp_token* token)
23581 {
23582 return (/* An ordinary function-body begins with an `{'. */
23583 token->type == CPP_OPEN_BRACE
23584 /* A ctor-initializer begins with a `:'. */
23585 || token->type == CPP_COLON
23586 /* A function-try-block begins with `try'. */
23587 || token->keyword == RID_TRY
23588 /* A function-transaction-block begins with `__transaction_atomic'
23589 or `__transaction_relaxed'. */
23590 || token->keyword == RID_TRANSACTION_ATOMIC
23591 || token->keyword == RID_TRANSACTION_RELAXED
23592 /* The named return value extension begins with `return'. */
23593 || token->keyword == RID_RETURN);
23594 }
23595
23596 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23597 definition. */
23598
23599 static bool
23600 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23601 {
23602 cp_token *token;
23603
23604 token = cp_lexer_peek_token (parser->lexer);
23605 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23606 }
23607
23608 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23609 C++0x) ending a template-argument. */
23610
23611 static bool
23612 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23613 {
23614 cp_token *token;
23615
23616 token = cp_lexer_peek_token (parser->lexer);
23617 return (token->type == CPP_COMMA
23618 || token->type == CPP_GREATER
23619 || token->type == CPP_ELLIPSIS
23620 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23621 }
23622
23623 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23624 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23625
23626 static bool
23627 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23628 size_t n)
23629 {
23630 cp_token *token;
23631
23632 token = cp_lexer_peek_nth_token (parser->lexer, n);
23633 if (token->type == CPP_LESS)
23634 return true;
23635 /* Check for the sequence `<::' in the original code. It would be lexed as
23636 `[:', where `[' is a digraph, and there is no whitespace before
23637 `:'. */
23638 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23639 {
23640 cp_token *token2;
23641 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23642 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23643 return true;
23644 }
23645 return false;
23646 }
23647
23648 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23649 or none_type otherwise. */
23650
23651 static enum tag_types
23652 cp_parser_token_is_class_key (cp_token* token)
23653 {
23654 switch (token->keyword)
23655 {
23656 case RID_CLASS:
23657 return class_type;
23658 case RID_STRUCT:
23659 return record_type;
23660 case RID_UNION:
23661 return union_type;
23662
23663 default:
23664 return none_type;
23665 }
23666 }
23667
23668 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23669
23670 static void
23671 cp_parser_check_class_key (enum tag_types class_key, tree type)
23672 {
23673 if (type == error_mark_node)
23674 return;
23675 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23676 {
23677 if (permerror (input_location, "%qs tag used in naming %q#T",
23678 class_key == union_type ? "union"
23679 : class_key == record_type ? "struct" : "class",
23680 type))
23681 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23682 "%q#T was previously declared here", type);
23683 }
23684 }
23685
23686 /* Issue an error message if DECL is redeclared with different
23687 access than its original declaration [class.access.spec/3].
23688 This applies to nested classes and nested class templates.
23689 [class.mem/1]. */
23690
23691 static void
23692 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23693 {
23694 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23695 return;
23696
23697 if ((TREE_PRIVATE (decl)
23698 != (current_access_specifier == access_private_node))
23699 || (TREE_PROTECTED (decl)
23700 != (current_access_specifier == access_protected_node)))
23701 error_at (location, "%qD redeclared with different access", decl);
23702 }
23703
23704 /* Look for the `template' keyword, as a syntactic disambiguator.
23705 Return TRUE iff it is present, in which case it will be
23706 consumed. */
23707
23708 static bool
23709 cp_parser_optional_template_keyword (cp_parser *parser)
23710 {
23711 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23712 {
23713 /* In C++98 the `template' keyword can only be used within templates;
23714 outside templates the parser can always figure out what is a
23715 template and what is not. In C++11, per the resolution of DR 468,
23716 `template' is allowed in cases where it is not strictly necessary. */
23717 if (!processing_template_decl
23718 && pedantic && cxx_dialect == cxx98)
23719 {
23720 cp_token *token = cp_lexer_peek_token (parser->lexer);
23721 pedwarn (token->location, OPT_Wpedantic,
23722 "in C++98 %<template%> (as a disambiguator) is only "
23723 "allowed within templates");
23724 /* If this part of the token stream is rescanned, the same
23725 error message would be generated. So, we purge the token
23726 from the stream. */
23727 cp_lexer_purge_token (parser->lexer);
23728 return false;
23729 }
23730 else
23731 {
23732 /* Consume the `template' keyword. */
23733 cp_lexer_consume_token (parser->lexer);
23734 return true;
23735 }
23736 }
23737 return false;
23738 }
23739
23740 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23741 set PARSER->SCOPE, and perform other related actions. */
23742
23743 static void
23744 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23745 {
23746 int i;
23747 struct tree_check *check_value;
23748 deferred_access_check *chk;
23749 vec<deferred_access_check, va_gc> *checks;
23750
23751 /* Get the stored value. */
23752 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23753 /* Perform any access checks that were deferred. */
23754 checks = check_value->checks;
23755 if (checks)
23756 {
23757 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
23758 perform_or_defer_access_check (chk->binfo,
23759 chk->decl,
23760 chk->diag_decl, tf_warning_or_error);
23761 }
23762 /* Set the scope from the stored value. */
23763 parser->scope = check_value->value;
23764 parser->qualifying_scope = check_value->qualifying_scope;
23765 parser->object_scope = NULL_TREE;
23766 }
23767
23768 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23769 encounter the end of a block before what we were looking for. */
23770
23771 static bool
23772 cp_parser_cache_group (cp_parser *parser,
23773 enum cpp_ttype end,
23774 unsigned depth)
23775 {
23776 while (true)
23777 {
23778 cp_token *token = cp_lexer_peek_token (parser->lexer);
23779
23780 /* Abort a parenthesized expression if we encounter a semicolon. */
23781 if ((end == CPP_CLOSE_PAREN || depth == 0)
23782 && token->type == CPP_SEMICOLON)
23783 return true;
23784 /* If we've reached the end of the file, stop. */
23785 if (token->type == CPP_EOF
23786 || (end != CPP_PRAGMA_EOL
23787 && token->type == CPP_PRAGMA_EOL))
23788 return true;
23789 if (token->type == CPP_CLOSE_BRACE && depth == 0)
23790 /* We've hit the end of an enclosing block, so there's been some
23791 kind of syntax error. */
23792 return true;
23793
23794 /* Consume the token. */
23795 cp_lexer_consume_token (parser->lexer);
23796 /* See if it starts a new group. */
23797 if (token->type == CPP_OPEN_BRACE)
23798 {
23799 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23800 /* In theory this should probably check end == '}', but
23801 cp_parser_save_member_function_body needs it to exit
23802 after either '}' or ')' when called with ')'. */
23803 if (depth == 0)
23804 return false;
23805 }
23806 else if (token->type == CPP_OPEN_PAREN)
23807 {
23808 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23809 if (depth == 0 && end == CPP_CLOSE_PAREN)
23810 return false;
23811 }
23812 else if (token->type == CPP_PRAGMA)
23813 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23814 else if (token->type == end)
23815 return false;
23816 }
23817 }
23818
23819 /* Like above, for caching a default argument or NSDMI. Both of these are
23820 terminated by a non-nested comma, but it can be unclear whether or not a
23821 comma is nested in a template argument list unless we do more parsing.
23822 In order to handle this ambiguity, when we encounter a ',' after a '<'
23823 we try to parse what follows as a parameter-declaration-list (in the
23824 case of a default argument) or a member-declarator (in the case of an
23825 NSDMI). If that succeeds, then we stop caching. */
23826
23827 static tree
23828 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23829 {
23830 unsigned depth = 0;
23831 int maybe_template_id = 0;
23832 cp_token *first_token;
23833 cp_token *token;
23834 tree default_argument;
23835
23836 /* Add tokens until we have processed the entire default
23837 argument. We add the range [first_token, token). */
23838 first_token = cp_lexer_peek_token (parser->lexer);
23839 if (first_token->type == CPP_OPEN_BRACE)
23840 {
23841 /* For list-initialization, this is straightforward. */
23842 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23843 token = cp_lexer_peek_token (parser->lexer);
23844 }
23845 else while (true)
23846 {
23847 bool done = false;
23848
23849 /* Peek at the next token. */
23850 token = cp_lexer_peek_token (parser->lexer);
23851 /* What we do depends on what token we have. */
23852 switch (token->type)
23853 {
23854 /* In valid code, a default argument must be
23855 immediately followed by a `,' `)', or `...'. */
23856 case CPP_COMMA:
23857 if (depth == 0 && maybe_template_id)
23858 {
23859 /* If we've seen a '<', we might be in a
23860 template-argument-list. Until Core issue 325 is
23861 resolved, we don't know how this situation ought
23862 to be handled, so try to DTRT. We check whether
23863 what comes after the comma is a valid parameter
23864 declaration list. If it is, then the comma ends
23865 the default argument; otherwise the default
23866 argument continues. */
23867 bool error = false;
23868 tree t;
23869
23870 /* Set ITALP so cp_parser_parameter_declaration_list
23871 doesn't decide to commit to this parse. */
23872 bool saved_italp = parser->in_template_argument_list_p;
23873 parser->in_template_argument_list_p = true;
23874
23875 cp_parser_parse_tentatively (parser);
23876 cp_lexer_consume_token (parser->lexer);
23877
23878 if (nsdmi)
23879 {
23880 int ctor_dtor_or_conv_p;
23881 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23882 &ctor_dtor_or_conv_p,
23883 /*parenthesized_p=*/NULL,
23884 /*member_p=*/true);
23885 }
23886 else
23887 {
23888 begin_scope (sk_function_parms, NULL_TREE);
23889 cp_parser_parameter_declaration_list (parser, &error);
23890 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23891 pop_binding (DECL_NAME (t), t);
23892 leave_scope ();
23893 }
23894 if (!cp_parser_error_occurred (parser) && !error)
23895 done = true;
23896 cp_parser_abort_tentative_parse (parser);
23897
23898 parser->in_template_argument_list_p = saved_italp;
23899 break;
23900 }
23901 case CPP_CLOSE_PAREN:
23902 case CPP_ELLIPSIS:
23903 /* If we run into a non-nested `;', `}', or `]',
23904 then the code is invalid -- but the default
23905 argument is certainly over. */
23906 case CPP_SEMICOLON:
23907 case CPP_CLOSE_BRACE:
23908 case CPP_CLOSE_SQUARE:
23909 if (depth == 0)
23910 done = true;
23911 /* Update DEPTH, if necessary. */
23912 else if (token->type == CPP_CLOSE_PAREN
23913 || token->type == CPP_CLOSE_BRACE
23914 || token->type == CPP_CLOSE_SQUARE)
23915 --depth;
23916 break;
23917
23918 case CPP_OPEN_PAREN:
23919 case CPP_OPEN_SQUARE:
23920 case CPP_OPEN_BRACE:
23921 ++depth;
23922 break;
23923
23924 case CPP_LESS:
23925 if (depth == 0)
23926 /* This might be the comparison operator, or it might
23927 start a template argument list. */
23928 ++maybe_template_id;
23929 break;
23930
23931 case CPP_RSHIFT:
23932 if (cxx_dialect == cxx98)
23933 break;
23934 /* Fall through for C++0x, which treats the `>>'
23935 operator like two `>' tokens in certain
23936 cases. */
23937
23938 case CPP_GREATER:
23939 if (depth == 0)
23940 {
23941 /* This might be an operator, or it might close a
23942 template argument list. But if a previous '<'
23943 started a template argument list, this will have
23944 closed it, so we can't be in one anymore. */
23945 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23946 if (maybe_template_id < 0)
23947 maybe_template_id = 0;
23948 }
23949 break;
23950
23951 /* If we run out of tokens, issue an error message. */
23952 case CPP_EOF:
23953 case CPP_PRAGMA_EOL:
23954 error_at (token->location, "file ends in default argument");
23955 done = true;
23956 break;
23957
23958 case CPP_NAME:
23959 case CPP_SCOPE:
23960 /* In these cases, we should look for template-ids.
23961 For example, if the default argument is
23962 `X<int, double>()', we need to do name lookup to
23963 figure out whether or not `X' is a template; if
23964 so, the `,' does not end the default argument.
23965
23966 That is not yet done. */
23967 break;
23968
23969 default:
23970 break;
23971 }
23972
23973 /* If we've reached the end, stop. */
23974 if (done)
23975 break;
23976
23977 /* Add the token to the token block. */
23978 token = cp_lexer_consume_token (parser->lexer);
23979 }
23980
23981 /* Create a DEFAULT_ARG to represent the unparsed default
23982 argument. */
23983 default_argument = make_node (DEFAULT_ARG);
23984 DEFARG_TOKENS (default_argument)
23985 = cp_token_cache_new (first_token, token);
23986 DEFARG_INSTANTIATIONS (default_argument) = NULL;
23987
23988 return default_argument;
23989 }
23990
23991 /* Begin parsing tentatively. We always save tokens while parsing
23992 tentatively so that if the tentative parsing fails we can restore the
23993 tokens. */
23994
23995 static void
23996 cp_parser_parse_tentatively (cp_parser* parser)
23997 {
23998 /* Enter a new parsing context. */
23999 parser->context = cp_parser_context_new (parser->context);
24000 /* Begin saving tokens. */
24001 cp_lexer_save_tokens (parser->lexer);
24002 /* In order to avoid repetitive access control error messages,
24003 access checks are queued up until we are no longer parsing
24004 tentatively. */
24005 push_deferring_access_checks (dk_deferred);
24006 }
24007
24008 /* Commit to the currently active tentative parse. */
24009
24010 static void
24011 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24012 {
24013 cp_parser_context *context;
24014 cp_lexer *lexer;
24015
24016 /* Mark all of the levels as committed. */
24017 lexer = parser->lexer;
24018 for (context = parser->context; context->next; context = context->next)
24019 {
24020 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24021 break;
24022 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24023 while (!cp_lexer_saving_tokens (lexer))
24024 lexer = lexer->next;
24025 cp_lexer_commit_tokens (lexer);
24026 }
24027 }
24028
24029 /* Abort the currently active tentative parse. All consumed tokens
24030 will be rolled back, and no diagnostics will be issued. */
24031
24032 static void
24033 cp_parser_abort_tentative_parse (cp_parser* parser)
24034 {
24035 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24036 || errorcount > 0);
24037 cp_parser_simulate_error (parser);
24038 /* Now, pretend that we want to see if the construct was
24039 successfully parsed. */
24040 cp_parser_parse_definitely (parser);
24041 }
24042
24043 /* Stop parsing tentatively. If a parse error has occurred, restore the
24044 token stream. Otherwise, commit to the tokens we have consumed.
24045 Returns true if no error occurred; false otherwise. */
24046
24047 static bool
24048 cp_parser_parse_definitely (cp_parser* parser)
24049 {
24050 bool error_occurred;
24051 cp_parser_context *context;
24052
24053 /* Remember whether or not an error occurred, since we are about to
24054 destroy that information. */
24055 error_occurred = cp_parser_error_occurred (parser);
24056 /* Remove the topmost context from the stack. */
24057 context = parser->context;
24058 parser->context = context->next;
24059 /* If no parse errors occurred, commit to the tentative parse. */
24060 if (!error_occurred)
24061 {
24062 /* Commit to the tokens read tentatively, unless that was
24063 already done. */
24064 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
24065 cp_lexer_commit_tokens (parser->lexer);
24066
24067 pop_to_parent_deferring_access_checks ();
24068 }
24069 /* Otherwise, if errors occurred, roll back our state so that things
24070 are just as they were before we began the tentative parse. */
24071 else
24072 {
24073 cp_lexer_rollback_tokens (parser->lexer);
24074 pop_deferring_access_checks ();
24075 }
24076 /* Add the context to the front of the free list. */
24077 context->next = cp_parser_context_free_list;
24078 cp_parser_context_free_list = context;
24079
24080 return !error_occurred;
24081 }
24082
24083 /* Returns true if we are parsing tentatively and are not committed to
24084 this tentative parse. */
24085
24086 static bool
24087 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
24088 {
24089 return (cp_parser_parsing_tentatively (parser)
24090 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
24091 }
24092
24093 /* Returns nonzero iff an error has occurred during the most recent
24094 tentative parse. */
24095
24096 static bool
24097 cp_parser_error_occurred (cp_parser* parser)
24098 {
24099 return (cp_parser_parsing_tentatively (parser)
24100 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
24101 }
24102
24103 /* Returns nonzero if GNU extensions are allowed. */
24104
24105 static bool
24106 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
24107 {
24108 return parser->allow_gnu_extensions_p;
24109 }
24110 \f
24111 /* Objective-C++ Productions */
24112
24113
24114 /* Parse an Objective-C expression, which feeds into a primary-expression
24115 above.
24116
24117 objc-expression:
24118 objc-message-expression
24119 objc-string-literal
24120 objc-encode-expression
24121 objc-protocol-expression
24122 objc-selector-expression
24123
24124 Returns a tree representation of the expression. */
24125
24126 static tree
24127 cp_parser_objc_expression (cp_parser* parser)
24128 {
24129 /* Try to figure out what kind of declaration is present. */
24130 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24131
24132 switch (kwd->type)
24133 {
24134 case CPP_OPEN_SQUARE:
24135 return cp_parser_objc_message_expression (parser);
24136
24137 case CPP_OBJC_STRING:
24138 kwd = cp_lexer_consume_token (parser->lexer);
24139 return objc_build_string_object (kwd->u.value);
24140
24141 case CPP_KEYWORD:
24142 switch (kwd->keyword)
24143 {
24144 case RID_AT_ENCODE:
24145 return cp_parser_objc_encode_expression (parser);
24146
24147 case RID_AT_PROTOCOL:
24148 return cp_parser_objc_protocol_expression (parser);
24149
24150 case RID_AT_SELECTOR:
24151 return cp_parser_objc_selector_expression (parser);
24152
24153 default:
24154 break;
24155 }
24156 default:
24157 error_at (kwd->location,
24158 "misplaced %<@%D%> Objective-C++ construct",
24159 kwd->u.value);
24160 cp_parser_skip_to_end_of_block_or_statement (parser);
24161 }
24162
24163 return error_mark_node;
24164 }
24165
24166 /* Parse an Objective-C message expression.
24167
24168 objc-message-expression:
24169 [ objc-message-receiver objc-message-args ]
24170
24171 Returns a representation of an Objective-C message. */
24172
24173 static tree
24174 cp_parser_objc_message_expression (cp_parser* parser)
24175 {
24176 tree receiver, messageargs;
24177
24178 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
24179 receiver = cp_parser_objc_message_receiver (parser);
24180 messageargs = cp_parser_objc_message_args (parser);
24181 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24182
24183 return objc_build_message_expr (receiver, messageargs);
24184 }
24185
24186 /* Parse an objc-message-receiver.
24187
24188 objc-message-receiver:
24189 expression
24190 simple-type-specifier
24191
24192 Returns a representation of the type or expression. */
24193
24194 static tree
24195 cp_parser_objc_message_receiver (cp_parser* parser)
24196 {
24197 tree rcv;
24198
24199 /* An Objective-C message receiver may be either (1) a type
24200 or (2) an expression. */
24201 cp_parser_parse_tentatively (parser);
24202 rcv = cp_parser_expression (parser, false, NULL);
24203
24204 if (cp_parser_parse_definitely (parser))
24205 return rcv;
24206
24207 rcv = cp_parser_simple_type_specifier (parser,
24208 /*decl_specs=*/NULL,
24209 CP_PARSER_FLAGS_NONE);
24210
24211 return objc_get_class_reference (rcv);
24212 }
24213
24214 /* Parse the arguments and selectors comprising an Objective-C message.
24215
24216 objc-message-args:
24217 objc-selector
24218 objc-selector-args
24219 objc-selector-args , objc-comma-args
24220
24221 objc-selector-args:
24222 objc-selector [opt] : assignment-expression
24223 objc-selector-args objc-selector [opt] : assignment-expression
24224
24225 objc-comma-args:
24226 assignment-expression
24227 objc-comma-args , assignment-expression
24228
24229 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24230 selector arguments and TREE_VALUE containing a list of comma
24231 arguments. */
24232
24233 static tree
24234 cp_parser_objc_message_args (cp_parser* parser)
24235 {
24236 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
24237 bool maybe_unary_selector_p = true;
24238 cp_token *token = cp_lexer_peek_token (parser->lexer);
24239
24240 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24241 {
24242 tree selector = NULL_TREE, arg;
24243
24244 if (token->type != CPP_COLON)
24245 selector = cp_parser_objc_selector (parser);
24246
24247 /* Detect if we have a unary selector. */
24248 if (maybe_unary_selector_p
24249 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24250 return build_tree_list (selector, NULL_TREE);
24251
24252 maybe_unary_selector_p = false;
24253 cp_parser_require (parser, CPP_COLON, RT_COLON);
24254 arg = cp_parser_assignment_expression (parser, false, NULL);
24255
24256 sel_args
24257 = chainon (sel_args,
24258 build_tree_list (selector, arg));
24259
24260 token = cp_lexer_peek_token (parser->lexer);
24261 }
24262
24263 /* Handle non-selector arguments, if any. */
24264 while (token->type == CPP_COMMA)
24265 {
24266 tree arg;
24267
24268 cp_lexer_consume_token (parser->lexer);
24269 arg = cp_parser_assignment_expression (parser, false, NULL);
24270
24271 addl_args
24272 = chainon (addl_args,
24273 build_tree_list (NULL_TREE, arg));
24274
24275 token = cp_lexer_peek_token (parser->lexer);
24276 }
24277
24278 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24279 {
24280 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24281 return build_tree_list (error_mark_node, error_mark_node);
24282 }
24283
24284 return build_tree_list (sel_args, addl_args);
24285 }
24286
24287 /* Parse an Objective-C encode expression.
24288
24289 objc-encode-expression:
24290 @encode objc-typename
24291
24292 Returns an encoded representation of the type argument. */
24293
24294 static tree
24295 cp_parser_objc_encode_expression (cp_parser* parser)
24296 {
24297 tree type;
24298 cp_token *token;
24299
24300 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
24301 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24302 token = cp_lexer_peek_token (parser->lexer);
24303 type = complete_type (cp_parser_type_id (parser));
24304 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24305
24306 if (!type)
24307 {
24308 error_at (token->location,
24309 "%<@encode%> must specify a type as an argument");
24310 return error_mark_node;
24311 }
24312
24313 /* This happens if we find @encode(T) (where T is a template
24314 typename or something dependent on a template typename) when
24315 parsing a template. In that case, we can't compile it
24316 immediately, but we rather create an AT_ENCODE_EXPR which will
24317 need to be instantiated when the template is used.
24318 */
24319 if (dependent_type_p (type))
24320 {
24321 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24322 TREE_READONLY (value) = 1;
24323 return value;
24324 }
24325
24326 return objc_build_encode_expr (type);
24327 }
24328
24329 /* Parse an Objective-C @defs expression. */
24330
24331 static tree
24332 cp_parser_objc_defs_expression (cp_parser *parser)
24333 {
24334 tree name;
24335
24336 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
24337 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24338 name = cp_parser_identifier (parser);
24339 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24340
24341 return objc_get_class_ivars (name);
24342 }
24343
24344 /* Parse an Objective-C protocol expression.
24345
24346 objc-protocol-expression:
24347 @protocol ( identifier )
24348
24349 Returns a representation of the protocol expression. */
24350
24351 static tree
24352 cp_parser_objc_protocol_expression (cp_parser* parser)
24353 {
24354 tree proto;
24355
24356 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24357 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24358 proto = cp_parser_identifier (parser);
24359 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24360
24361 return objc_build_protocol_expr (proto);
24362 }
24363
24364 /* Parse an Objective-C selector expression.
24365
24366 objc-selector-expression:
24367 @selector ( objc-method-signature )
24368
24369 objc-method-signature:
24370 objc-selector
24371 objc-selector-seq
24372
24373 objc-selector-seq:
24374 objc-selector :
24375 objc-selector-seq objc-selector :
24376
24377 Returns a representation of the method selector. */
24378
24379 static tree
24380 cp_parser_objc_selector_expression (cp_parser* parser)
24381 {
24382 tree sel_seq = NULL_TREE;
24383 bool maybe_unary_selector_p = true;
24384 cp_token *token;
24385 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24386
24387 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
24388 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24389 token = cp_lexer_peek_token (parser->lexer);
24390
24391 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
24392 || token->type == CPP_SCOPE)
24393 {
24394 tree selector = NULL_TREE;
24395
24396 if (token->type != CPP_COLON
24397 || token->type == CPP_SCOPE)
24398 selector = cp_parser_objc_selector (parser);
24399
24400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
24401 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
24402 {
24403 /* Detect if we have a unary selector. */
24404 if (maybe_unary_selector_p)
24405 {
24406 sel_seq = selector;
24407 goto finish_selector;
24408 }
24409 else
24410 {
24411 cp_parser_error (parser, "expected %<:%>");
24412 }
24413 }
24414 maybe_unary_selector_p = false;
24415 token = cp_lexer_consume_token (parser->lexer);
24416
24417 if (token->type == CPP_SCOPE)
24418 {
24419 sel_seq
24420 = chainon (sel_seq,
24421 build_tree_list (selector, NULL_TREE));
24422 sel_seq
24423 = chainon (sel_seq,
24424 build_tree_list (NULL_TREE, NULL_TREE));
24425 }
24426 else
24427 sel_seq
24428 = chainon (sel_seq,
24429 build_tree_list (selector, NULL_TREE));
24430
24431 token = cp_lexer_peek_token (parser->lexer);
24432 }
24433
24434 finish_selector:
24435 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24436
24437 return objc_build_selector_expr (loc, sel_seq);
24438 }
24439
24440 /* Parse a list of identifiers.
24441
24442 objc-identifier-list:
24443 identifier
24444 objc-identifier-list , identifier
24445
24446 Returns a TREE_LIST of identifier nodes. */
24447
24448 static tree
24449 cp_parser_objc_identifier_list (cp_parser* parser)
24450 {
24451 tree identifier;
24452 tree list;
24453 cp_token *sep;
24454
24455 identifier = cp_parser_identifier (parser);
24456 if (identifier == error_mark_node)
24457 return error_mark_node;
24458
24459 list = build_tree_list (NULL_TREE, identifier);
24460 sep = cp_lexer_peek_token (parser->lexer);
24461
24462 while (sep->type == CPP_COMMA)
24463 {
24464 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24465 identifier = cp_parser_identifier (parser);
24466 if (identifier == error_mark_node)
24467 return list;
24468
24469 list = chainon (list, build_tree_list (NULL_TREE,
24470 identifier));
24471 sep = cp_lexer_peek_token (parser->lexer);
24472 }
24473
24474 return list;
24475 }
24476
24477 /* Parse an Objective-C alias declaration.
24478
24479 objc-alias-declaration:
24480 @compatibility_alias identifier identifier ;
24481
24482 This function registers the alias mapping with the Objective-C front end.
24483 It returns nothing. */
24484
24485 static void
24486 cp_parser_objc_alias_declaration (cp_parser* parser)
24487 {
24488 tree alias, orig;
24489
24490 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24491 alias = cp_parser_identifier (parser);
24492 orig = cp_parser_identifier (parser);
24493 objc_declare_alias (alias, orig);
24494 cp_parser_consume_semicolon_at_end_of_statement (parser);
24495 }
24496
24497 /* Parse an Objective-C class forward-declaration.
24498
24499 objc-class-declaration:
24500 @class objc-identifier-list ;
24501
24502 The function registers the forward declarations with the Objective-C
24503 front end. It returns nothing. */
24504
24505 static void
24506 cp_parser_objc_class_declaration (cp_parser* parser)
24507 {
24508 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24509 while (true)
24510 {
24511 tree id;
24512
24513 id = cp_parser_identifier (parser);
24514 if (id == error_mark_node)
24515 break;
24516
24517 objc_declare_class (id);
24518
24519 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24520 cp_lexer_consume_token (parser->lexer);
24521 else
24522 break;
24523 }
24524 cp_parser_consume_semicolon_at_end_of_statement (parser);
24525 }
24526
24527 /* Parse a list of Objective-C protocol references.
24528
24529 objc-protocol-refs-opt:
24530 objc-protocol-refs [opt]
24531
24532 objc-protocol-refs:
24533 < objc-identifier-list >
24534
24535 Returns a TREE_LIST of identifiers, if any. */
24536
24537 static tree
24538 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24539 {
24540 tree protorefs = NULL_TREE;
24541
24542 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24543 {
24544 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24545 protorefs = cp_parser_objc_identifier_list (parser);
24546 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24547 }
24548
24549 return protorefs;
24550 }
24551
24552 /* Parse a Objective-C visibility specification. */
24553
24554 static void
24555 cp_parser_objc_visibility_spec (cp_parser* parser)
24556 {
24557 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24558
24559 switch (vis->keyword)
24560 {
24561 case RID_AT_PRIVATE:
24562 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24563 break;
24564 case RID_AT_PROTECTED:
24565 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24566 break;
24567 case RID_AT_PUBLIC:
24568 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24569 break;
24570 case RID_AT_PACKAGE:
24571 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24572 break;
24573 default:
24574 return;
24575 }
24576
24577 /* Eat '@private'/'@protected'/'@public'. */
24578 cp_lexer_consume_token (parser->lexer);
24579 }
24580
24581 /* Parse an Objective-C method type. Return 'true' if it is a class
24582 (+) method, and 'false' if it is an instance (-) method. */
24583
24584 static inline bool
24585 cp_parser_objc_method_type (cp_parser* parser)
24586 {
24587 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24588 return true;
24589 else
24590 return false;
24591 }
24592
24593 /* Parse an Objective-C protocol qualifier. */
24594
24595 static tree
24596 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24597 {
24598 tree quals = NULL_TREE, node;
24599 cp_token *token = cp_lexer_peek_token (parser->lexer);
24600
24601 node = token->u.value;
24602
24603 while (node && identifier_p (node)
24604 && (node == ridpointers [(int) RID_IN]
24605 || node == ridpointers [(int) RID_OUT]
24606 || node == ridpointers [(int) RID_INOUT]
24607 || node == ridpointers [(int) RID_BYCOPY]
24608 || node == ridpointers [(int) RID_BYREF]
24609 || node == ridpointers [(int) RID_ONEWAY]))
24610 {
24611 quals = tree_cons (NULL_TREE, node, quals);
24612 cp_lexer_consume_token (parser->lexer);
24613 token = cp_lexer_peek_token (parser->lexer);
24614 node = token->u.value;
24615 }
24616
24617 return quals;
24618 }
24619
24620 /* Parse an Objective-C typename. */
24621
24622 static tree
24623 cp_parser_objc_typename (cp_parser* parser)
24624 {
24625 tree type_name = NULL_TREE;
24626
24627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24628 {
24629 tree proto_quals, cp_type = NULL_TREE;
24630
24631 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24632 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24633
24634 /* An ObjC type name may consist of just protocol qualifiers, in which
24635 case the type shall default to 'id'. */
24636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24637 {
24638 cp_type = cp_parser_type_id (parser);
24639
24640 /* If the type could not be parsed, an error has already
24641 been produced. For error recovery, behave as if it had
24642 not been specified, which will use the default type
24643 'id'. */
24644 if (cp_type == error_mark_node)
24645 {
24646 cp_type = NULL_TREE;
24647 /* We need to skip to the closing parenthesis as
24648 cp_parser_type_id() does not seem to do it for
24649 us. */
24650 cp_parser_skip_to_closing_parenthesis (parser,
24651 /*recovering=*/true,
24652 /*or_comma=*/false,
24653 /*consume_paren=*/false);
24654 }
24655 }
24656
24657 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24658 type_name = build_tree_list (proto_quals, cp_type);
24659 }
24660
24661 return type_name;
24662 }
24663
24664 /* Check to see if TYPE refers to an Objective-C selector name. */
24665
24666 static bool
24667 cp_parser_objc_selector_p (enum cpp_ttype type)
24668 {
24669 return (type == CPP_NAME || type == CPP_KEYWORD
24670 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24671 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24672 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24673 || type == CPP_XOR || type == CPP_XOR_EQ);
24674 }
24675
24676 /* Parse an Objective-C selector. */
24677
24678 static tree
24679 cp_parser_objc_selector (cp_parser* parser)
24680 {
24681 cp_token *token = cp_lexer_consume_token (parser->lexer);
24682
24683 if (!cp_parser_objc_selector_p (token->type))
24684 {
24685 error_at (token->location, "invalid Objective-C++ selector name");
24686 return error_mark_node;
24687 }
24688
24689 /* C++ operator names are allowed to appear in ObjC selectors. */
24690 switch (token->type)
24691 {
24692 case CPP_AND_AND: return get_identifier ("and");
24693 case CPP_AND_EQ: return get_identifier ("and_eq");
24694 case CPP_AND: return get_identifier ("bitand");
24695 case CPP_OR: return get_identifier ("bitor");
24696 case CPP_COMPL: return get_identifier ("compl");
24697 case CPP_NOT: return get_identifier ("not");
24698 case CPP_NOT_EQ: return get_identifier ("not_eq");
24699 case CPP_OR_OR: return get_identifier ("or");
24700 case CPP_OR_EQ: return get_identifier ("or_eq");
24701 case CPP_XOR: return get_identifier ("xor");
24702 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24703 default: return token->u.value;
24704 }
24705 }
24706
24707 /* Parse an Objective-C params list. */
24708
24709 static tree
24710 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24711 {
24712 tree params = NULL_TREE;
24713 bool maybe_unary_selector_p = true;
24714 cp_token *token = cp_lexer_peek_token (parser->lexer);
24715
24716 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24717 {
24718 tree selector = NULL_TREE, type_name, identifier;
24719 tree parm_attr = NULL_TREE;
24720
24721 if (token->keyword == RID_ATTRIBUTE)
24722 break;
24723
24724 if (token->type != CPP_COLON)
24725 selector = cp_parser_objc_selector (parser);
24726
24727 /* Detect if we have a unary selector. */
24728 if (maybe_unary_selector_p
24729 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24730 {
24731 params = selector; /* Might be followed by attributes. */
24732 break;
24733 }
24734
24735 maybe_unary_selector_p = false;
24736 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24737 {
24738 /* Something went quite wrong. There should be a colon
24739 here, but there is not. Stop parsing parameters. */
24740 break;
24741 }
24742 type_name = cp_parser_objc_typename (parser);
24743 /* New ObjC allows attributes on parameters too. */
24744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24745 parm_attr = cp_parser_attributes_opt (parser);
24746 identifier = cp_parser_identifier (parser);
24747
24748 params
24749 = chainon (params,
24750 objc_build_keyword_decl (selector,
24751 type_name,
24752 identifier,
24753 parm_attr));
24754
24755 token = cp_lexer_peek_token (parser->lexer);
24756 }
24757
24758 if (params == NULL_TREE)
24759 {
24760 cp_parser_error (parser, "objective-c++ method declaration is expected");
24761 return error_mark_node;
24762 }
24763
24764 /* We allow tail attributes for the method. */
24765 if (token->keyword == RID_ATTRIBUTE)
24766 {
24767 *attributes = cp_parser_attributes_opt (parser);
24768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24769 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24770 return params;
24771 cp_parser_error (parser,
24772 "method attributes must be specified at the end");
24773 return error_mark_node;
24774 }
24775
24776 if (params == NULL_TREE)
24777 {
24778 cp_parser_error (parser, "objective-c++ method declaration is expected");
24779 return error_mark_node;
24780 }
24781 return params;
24782 }
24783
24784 /* Parse the non-keyword Objective-C params. */
24785
24786 static tree
24787 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
24788 tree* attributes)
24789 {
24790 tree params = make_node (TREE_LIST);
24791 cp_token *token = cp_lexer_peek_token (parser->lexer);
24792 *ellipsisp = false; /* Initially, assume no ellipsis. */
24793
24794 while (token->type == CPP_COMMA)
24795 {
24796 cp_parameter_declarator *parmdecl;
24797 tree parm;
24798
24799 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24800 token = cp_lexer_peek_token (parser->lexer);
24801
24802 if (token->type == CPP_ELLIPSIS)
24803 {
24804 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
24805 *ellipsisp = true;
24806 token = cp_lexer_peek_token (parser->lexer);
24807 break;
24808 }
24809
24810 /* TODO: parse attributes for tail parameters. */
24811 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24812 parm = grokdeclarator (parmdecl->declarator,
24813 &parmdecl->decl_specifiers,
24814 PARM, /*initialized=*/0,
24815 /*attrlist=*/NULL);
24816
24817 chainon (params, build_tree_list (NULL_TREE, parm));
24818 token = cp_lexer_peek_token (parser->lexer);
24819 }
24820
24821 /* We allow tail attributes for the method. */
24822 if (token->keyword == RID_ATTRIBUTE)
24823 {
24824 if (*attributes == NULL_TREE)
24825 {
24826 *attributes = cp_parser_attributes_opt (parser);
24827 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24828 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24829 return params;
24830 }
24831 else
24832 /* We have an error, but parse the attributes, so that we can
24833 carry on. */
24834 *attributes = cp_parser_attributes_opt (parser);
24835
24836 cp_parser_error (parser,
24837 "method attributes must be specified at the end");
24838 return error_mark_node;
24839 }
24840
24841 return params;
24842 }
24843
24844 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24845
24846 static void
24847 cp_parser_objc_interstitial_code (cp_parser* parser)
24848 {
24849 cp_token *token = cp_lexer_peek_token (parser->lexer);
24850
24851 /* If the next token is `extern' and the following token is a string
24852 literal, then we have a linkage specification. */
24853 if (token->keyword == RID_EXTERN
24854 && cp_parser_is_pure_string_literal
24855 (cp_lexer_peek_nth_token (parser->lexer, 2)))
24856 cp_parser_linkage_specification (parser);
24857 /* Handle #pragma, if any. */
24858 else if (token->type == CPP_PRAGMA)
24859 cp_parser_pragma (parser, pragma_external);
24860 /* Allow stray semicolons. */
24861 else if (token->type == CPP_SEMICOLON)
24862 cp_lexer_consume_token (parser->lexer);
24863 /* Mark methods as optional or required, when building protocols. */
24864 else if (token->keyword == RID_AT_OPTIONAL)
24865 {
24866 cp_lexer_consume_token (parser->lexer);
24867 objc_set_method_opt (true);
24868 }
24869 else if (token->keyword == RID_AT_REQUIRED)
24870 {
24871 cp_lexer_consume_token (parser->lexer);
24872 objc_set_method_opt (false);
24873 }
24874 else if (token->keyword == RID_NAMESPACE)
24875 cp_parser_namespace_definition (parser);
24876 /* Other stray characters must generate errors. */
24877 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24878 {
24879 cp_lexer_consume_token (parser->lexer);
24880 error ("stray %qs between Objective-C++ methods",
24881 token->type == CPP_OPEN_BRACE ? "{" : "}");
24882 }
24883 /* Finally, try to parse a block-declaration, or a function-definition. */
24884 else
24885 cp_parser_block_declaration (parser, /*statement_p=*/false);
24886 }
24887
24888 /* Parse a method signature. */
24889
24890 static tree
24891 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24892 {
24893 tree rettype, kwdparms, optparms;
24894 bool ellipsis = false;
24895 bool is_class_method;
24896
24897 is_class_method = cp_parser_objc_method_type (parser);
24898 rettype = cp_parser_objc_typename (parser);
24899 *attributes = NULL_TREE;
24900 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24901 if (kwdparms == error_mark_node)
24902 return error_mark_node;
24903 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24904 if (optparms == error_mark_node)
24905 return error_mark_node;
24906
24907 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24908 }
24909
24910 static bool
24911 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24912 {
24913 tree tattr;
24914 cp_lexer_save_tokens (parser->lexer);
24915 tattr = cp_parser_attributes_opt (parser);
24916 gcc_assert (tattr) ;
24917
24918 /* If the attributes are followed by a method introducer, this is not allowed.
24919 Dump the attributes and flag the situation. */
24920 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24921 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24922 return true;
24923
24924 /* Otherwise, the attributes introduce some interstitial code, possibly so
24925 rewind to allow that check. */
24926 cp_lexer_rollback_tokens (parser->lexer);
24927 return false;
24928 }
24929
24930 /* Parse an Objective-C method prototype list. */
24931
24932 static void
24933 cp_parser_objc_method_prototype_list (cp_parser* parser)
24934 {
24935 cp_token *token = cp_lexer_peek_token (parser->lexer);
24936
24937 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24938 {
24939 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24940 {
24941 tree attributes, sig;
24942 bool is_class_method;
24943 if (token->type == CPP_PLUS)
24944 is_class_method = true;
24945 else
24946 is_class_method = false;
24947 sig = cp_parser_objc_method_signature (parser, &attributes);
24948 if (sig == error_mark_node)
24949 {
24950 cp_parser_skip_to_end_of_block_or_statement (parser);
24951 token = cp_lexer_peek_token (parser->lexer);
24952 continue;
24953 }
24954 objc_add_method_declaration (is_class_method, sig, attributes);
24955 cp_parser_consume_semicolon_at_end_of_statement (parser);
24956 }
24957 else if (token->keyword == RID_AT_PROPERTY)
24958 cp_parser_objc_at_property_declaration (parser);
24959 else if (token->keyword == RID_ATTRIBUTE
24960 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24961 warning_at (cp_lexer_peek_token (parser->lexer)->location,
24962 OPT_Wattributes,
24963 "prefix attributes are ignored for methods");
24964 else
24965 /* Allow for interspersed non-ObjC++ code. */
24966 cp_parser_objc_interstitial_code (parser);
24967
24968 token = cp_lexer_peek_token (parser->lexer);
24969 }
24970
24971 if (token->type != CPP_EOF)
24972 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24973 else
24974 cp_parser_error (parser, "expected %<@end%>");
24975
24976 objc_finish_interface ();
24977 }
24978
24979 /* Parse an Objective-C method definition list. */
24980
24981 static void
24982 cp_parser_objc_method_definition_list (cp_parser* parser)
24983 {
24984 cp_token *token = cp_lexer_peek_token (parser->lexer);
24985
24986 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24987 {
24988 tree meth;
24989
24990 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24991 {
24992 cp_token *ptk;
24993 tree sig, attribute;
24994 bool is_class_method;
24995 if (token->type == CPP_PLUS)
24996 is_class_method = true;
24997 else
24998 is_class_method = false;
24999 push_deferring_access_checks (dk_deferred);
25000 sig = cp_parser_objc_method_signature (parser, &attribute);
25001 if (sig == error_mark_node)
25002 {
25003 cp_parser_skip_to_end_of_block_or_statement (parser);
25004 token = cp_lexer_peek_token (parser->lexer);
25005 continue;
25006 }
25007 objc_start_method_definition (is_class_method, sig, attribute,
25008 NULL_TREE);
25009
25010 /* For historical reasons, we accept an optional semicolon. */
25011 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25012 cp_lexer_consume_token (parser->lexer);
25013
25014 ptk = cp_lexer_peek_token (parser->lexer);
25015 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25016 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25017 {
25018 perform_deferred_access_checks (tf_warning_or_error);
25019 stop_deferring_access_checks ();
25020 meth = cp_parser_function_definition_after_declarator (parser,
25021 false);
25022 pop_deferring_access_checks ();
25023 objc_finish_method_definition (meth);
25024 }
25025 }
25026 /* The following case will be removed once @synthesize is
25027 completely implemented. */
25028 else if (token->keyword == RID_AT_PROPERTY)
25029 cp_parser_objc_at_property_declaration (parser);
25030 else if (token->keyword == RID_AT_SYNTHESIZE)
25031 cp_parser_objc_at_synthesize_declaration (parser);
25032 else if (token->keyword == RID_AT_DYNAMIC)
25033 cp_parser_objc_at_dynamic_declaration (parser);
25034 else if (token->keyword == RID_ATTRIBUTE
25035 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25036 warning_at (token->location, OPT_Wattributes,
25037 "prefix attributes are ignored for methods");
25038 else
25039 /* Allow for interspersed non-ObjC++ code. */
25040 cp_parser_objc_interstitial_code (parser);
25041
25042 token = cp_lexer_peek_token (parser->lexer);
25043 }
25044
25045 if (token->type != CPP_EOF)
25046 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25047 else
25048 cp_parser_error (parser, "expected %<@end%>");
25049
25050 objc_finish_implementation ();
25051 }
25052
25053 /* Parse Objective-C ivars. */
25054
25055 static void
25056 cp_parser_objc_class_ivars (cp_parser* parser)
25057 {
25058 cp_token *token = cp_lexer_peek_token (parser->lexer);
25059
25060 if (token->type != CPP_OPEN_BRACE)
25061 return; /* No ivars specified. */
25062
25063 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
25064 token = cp_lexer_peek_token (parser->lexer);
25065
25066 while (token->type != CPP_CLOSE_BRACE
25067 && token->keyword != RID_AT_END && token->type != CPP_EOF)
25068 {
25069 cp_decl_specifier_seq declspecs;
25070 int decl_class_or_enum_p;
25071 tree prefix_attributes;
25072
25073 cp_parser_objc_visibility_spec (parser);
25074
25075 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25076 break;
25077
25078 cp_parser_decl_specifier_seq (parser,
25079 CP_PARSER_FLAGS_OPTIONAL,
25080 &declspecs,
25081 &decl_class_or_enum_p);
25082
25083 /* auto, register, static, extern, mutable. */
25084 if (declspecs.storage_class != sc_none)
25085 {
25086 cp_parser_error (parser, "invalid type for instance variable");
25087 declspecs.storage_class = sc_none;
25088 }
25089
25090 /* thread_local. */
25091 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25092 {
25093 cp_parser_error (parser, "invalid type for instance variable");
25094 declspecs.locations[ds_thread] = 0;
25095 }
25096
25097 /* typedef. */
25098 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25099 {
25100 cp_parser_error (parser, "invalid type for instance variable");
25101 declspecs.locations[ds_typedef] = 0;
25102 }
25103
25104 prefix_attributes = declspecs.attributes;
25105 declspecs.attributes = NULL_TREE;
25106
25107 /* Keep going until we hit the `;' at the end of the
25108 declaration. */
25109 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25110 {
25111 tree width = NULL_TREE, attributes, first_attribute, decl;
25112 cp_declarator *declarator = NULL;
25113 int ctor_dtor_or_conv_p;
25114
25115 /* Check for a (possibly unnamed) bitfield declaration. */
25116 token = cp_lexer_peek_token (parser->lexer);
25117 if (token->type == CPP_COLON)
25118 goto eat_colon;
25119
25120 if (token->type == CPP_NAME
25121 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25122 == CPP_COLON))
25123 {
25124 /* Get the name of the bitfield. */
25125 declarator = make_id_declarator (NULL_TREE,
25126 cp_parser_identifier (parser),
25127 sfk_none);
25128
25129 eat_colon:
25130 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25131 /* Get the width of the bitfield. */
25132 width
25133 = cp_parser_constant_expression (parser,
25134 /*allow_non_constant=*/false,
25135 NULL);
25136 }
25137 else
25138 {
25139 /* Parse the declarator. */
25140 declarator
25141 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25142 &ctor_dtor_or_conv_p,
25143 /*parenthesized_p=*/NULL,
25144 /*member_p=*/false);
25145 }
25146
25147 /* Look for attributes that apply to the ivar. */
25148 attributes = cp_parser_attributes_opt (parser);
25149 /* Remember which attributes are prefix attributes and
25150 which are not. */
25151 first_attribute = attributes;
25152 /* Combine the attributes. */
25153 attributes = chainon (prefix_attributes, attributes);
25154
25155 if (width)
25156 /* Create the bitfield declaration. */
25157 decl = grokbitfield (declarator, &declspecs,
25158 width,
25159 attributes);
25160 else
25161 decl = grokfield (declarator, &declspecs,
25162 NULL_TREE, /*init_const_expr_p=*/false,
25163 NULL_TREE, attributes);
25164
25165 /* Add the instance variable. */
25166 if (decl != error_mark_node && decl != NULL_TREE)
25167 objc_add_instance_variable (decl);
25168
25169 /* Reset PREFIX_ATTRIBUTES. */
25170 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25171 attributes = TREE_CHAIN (attributes);
25172 if (attributes)
25173 TREE_CHAIN (attributes) = NULL_TREE;
25174
25175 token = cp_lexer_peek_token (parser->lexer);
25176
25177 if (token->type == CPP_COMMA)
25178 {
25179 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25180 continue;
25181 }
25182 break;
25183 }
25184
25185 cp_parser_consume_semicolon_at_end_of_statement (parser);
25186 token = cp_lexer_peek_token (parser->lexer);
25187 }
25188
25189 if (token->keyword == RID_AT_END)
25190 cp_parser_error (parser, "expected %<}%>");
25191
25192 /* Do not consume the RID_AT_END, so it will be read again as terminating
25193 the @interface of @implementation. */
25194 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
25195 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
25196
25197 /* For historical reasons, we accept an optional semicolon. */
25198 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25199 cp_lexer_consume_token (parser->lexer);
25200 }
25201
25202 /* Parse an Objective-C protocol declaration. */
25203
25204 static void
25205 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
25206 {
25207 tree proto, protorefs;
25208 cp_token *tok;
25209
25210 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
25212 {
25213 tok = cp_lexer_peek_token (parser->lexer);
25214 error_at (tok->location, "identifier expected after %<@protocol%>");
25215 cp_parser_consume_semicolon_at_end_of_statement (parser);
25216 return;
25217 }
25218
25219 /* See if we have a forward declaration or a definition. */
25220 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
25221
25222 /* Try a forward declaration first. */
25223 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
25224 {
25225 while (true)
25226 {
25227 tree id;
25228
25229 id = cp_parser_identifier (parser);
25230 if (id == error_mark_node)
25231 break;
25232
25233 objc_declare_protocol (id, attributes);
25234
25235 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25236 cp_lexer_consume_token (parser->lexer);
25237 else
25238 break;
25239 }
25240 cp_parser_consume_semicolon_at_end_of_statement (parser);
25241 }
25242
25243 /* Ok, we got a full-fledged definition (or at least should). */
25244 else
25245 {
25246 proto = cp_parser_identifier (parser);
25247 protorefs = cp_parser_objc_protocol_refs_opt (parser);
25248 objc_start_protocol (proto, protorefs, attributes);
25249 cp_parser_objc_method_prototype_list (parser);
25250 }
25251 }
25252
25253 /* Parse an Objective-C superclass or category. */
25254
25255 static void
25256 cp_parser_objc_superclass_or_category (cp_parser *parser,
25257 bool iface_p,
25258 tree *super,
25259 tree *categ, bool *is_class_extension)
25260 {
25261 cp_token *next = cp_lexer_peek_token (parser->lexer);
25262
25263 *super = *categ = NULL_TREE;
25264 *is_class_extension = false;
25265 if (next->type == CPP_COLON)
25266 {
25267 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25268 *super = cp_parser_identifier (parser);
25269 }
25270 else if (next->type == CPP_OPEN_PAREN)
25271 {
25272 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25273
25274 /* If there is no category name, and this is an @interface, we
25275 have a class extension. */
25276 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25277 {
25278 *categ = NULL_TREE;
25279 *is_class_extension = true;
25280 }
25281 else
25282 *categ = cp_parser_identifier (parser);
25283
25284 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25285 }
25286 }
25287
25288 /* Parse an Objective-C class interface. */
25289
25290 static void
25291 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25292 {
25293 tree name, super, categ, protos;
25294 bool is_class_extension;
25295
25296 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
25297 name = cp_parser_identifier (parser);
25298 if (name == error_mark_node)
25299 {
25300 /* It's hard to recover because even if valid @interface stuff
25301 is to follow, we can't compile it (or validate it) if we
25302 don't even know which class it refers to. Let's assume this
25303 was a stray '@interface' token in the stream and skip it.
25304 */
25305 return;
25306 }
25307 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25308 &is_class_extension);
25309 protos = cp_parser_objc_protocol_refs_opt (parser);
25310
25311 /* We have either a class or a category on our hands. */
25312 if (categ || is_class_extension)
25313 objc_start_category_interface (name, categ, protos, attributes);
25314 else
25315 {
25316 objc_start_class_interface (name, super, protos, attributes);
25317 /* Handle instance variable declarations, if any. */
25318 cp_parser_objc_class_ivars (parser);
25319 objc_continue_interface ();
25320 }
25321
25322 cp_parser_objc_method_prototype_list (parser);
25323 }
25324
25325 /* Parse an Objective-C class implementation. */
25326
25327 static void
25328 cp_parser_objc_class_implementation (cp_parser* parser)
25329 {
25330 tree name, super, categ;
25331 bool is_class_extension;
25332
25333 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
25334 name = cp_parser_identifier (parser);
25335 if (name == error_mark_node)
25336 {
25337 /* It's hard to recover because even if valid @implementation
25338 stuff is to follow, we can't compile it (or validate it) if
25339 we don't even know which class it refers to. Let's assume
25340 this was a stray '@implementation' token in the stream and
25341 skip it.
25342 */
25343 return;
25344 }
25345 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25346 &is_class_extension);
25347
25348 /* We have either a class or a category on our hands. */
25349 if (categ)
25350 objc_start_category_implementation (name, categ);
25351 else
25352 {
25353 objc_start_class_implementation (name, super);
25354 /* Handle instance variable declarations, if any. */
25355 cp_parser_objc_class_ivars (parser);
25356 objc_continue_implementation ();
25357 }
25358
25359 cp_parser_objc_method_definition_list (parser);
25360 }
25361
25362 /* Consume the @end token and finish off the implementation. */
25363
25364 static void
25365 cp_parser_objc_end_implementation (cp_parser* parser)
25366 {
25367 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25368 objc_finish_implementation ();
25369 }
25370
25371 /* Parse an Objective-C declaration. */
25372
25373 static void
25374 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
25375 {
25376 /* Try to figure out what kind of declaration is present. */
25377 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25378
25379 if (attributes)
25380 switch (kwd->keyword)
25381 {
25382 case RID_AT_ALIAS:
25383 case RID_AT_CLASS:
25384 case RID_AT_END:
25385 error_at (kwd->location, "attributes may not be specified before"
25386 " the %<@%D%> Objective-C++ keyword",
25387 kwd->u.value);
25388 attributes = NULL;
25389 break;
25390 case RID_AT_IMPLEMENTATION:
25391 warning_at (kwd->location, OPT_Wattributes,
25392 "prefix attributes are ignored before %<@%D%>",
25393 kwd->u.value);
25394 attributes = NULL;
25395 default:
25396 break;
25397 }
25398
25399 switch (kwd->keyword)
25400 {
25401 case RID_AT_ALIAS:
25402 cp_parser_objc_alias_declaration (parser);
25403 break;
25404 case RID_AT_CLASS:
25405 cp_parser_objc_class_declaration (parser);
25406 break;
25407 case RID_AT_PROTOCOL:
25408 cp_parser_objc_protocol_declaration (parser, attributes);
25409 break;
25410 case RID_AT_INTERFACE:
25411 cp_parser_objc_class_interface (parser, attributes);
25412 break;
25413 case RID_AT_IMPLEMENTATION:
25414 cp_parser_objc_class_implementation (parser);
25415 break;
25416 case RID_AT_END:
25417 cp_parser_objc_end_implementation (parser);
25418 break;
25419 default:
25420 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25421 kwd->u.value);
25422 cp_parser_skip_to_end_of_block_or_statement (parser);
25423 }
25424 }
25425
25426 /* Parse an Objective-C try-catch-finally statement.
25427
25428 objc-try-catch-finally-stmt:
25429 @try compound-statement objc-catch-clause-seq [opt]
25430 objc-finally-clause [opt]
25431
25432 objc-catch-clause-seq:
25433 objc-catch-clause objc-catch-clause-seq [opt]
25434
25435 objc-catch-clause:
25436 @catch ( objc-exception-declaration ) compound-statement
25437
25438 objc-finally-clause:
25439 @finally compound-statement
25440
25441 objc-exception-declaration:
25442 parameter-declaration
25443 '...'
25444
25445 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25446
25447 Returns NULL_TREE.
25448
25449 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25450 for C. Keep them in sync. */
25451
25452 static tree
25453 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25454 {
25455 location_t location;
25456 tree stmt;
25457
25458 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25459 location = cp_lexer_peek_token (parser->lexer)->location;
25460 objc_maybe_warn_exceptions (location);
25461 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25462 node, lest it get absorbed into the surrounding block. */
25463 stmt = push_stmt_list ();
25464 cp_parser_compound_statement (parser, NULL, false, false);
25465 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25466
25467 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25468 {
25469 cp_parameter_declarator *parm;
25470 tree parameter_declaration = error_mark_node;
25471 bool seen_open_paren = false;
25472
25473 cp_lexer_consume_token (parser->lexer);
25474 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25475 seen_open_paren = true;
25476 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25477 {
25478 /* We have "@catch (...)" (where the '...' are literally
25479 what is in the code). Skip the '...'.
25480 parameter_declaration is set to NULL_TREE, and
25481 objc_being_catch_clauses() knows that that means
25482 '...'. */
25483 cp_lexer_consume_token (parser->lexer);
25484 parameter_declaration = NULL_TREE;
25485 }
25486 else
25487 {
25488 /* We have "@catch (NSException *exception)" or something
25489 like that. Parse the parameter declaration. */
25490 parm = cp_parser_parameter_declaration (parser, false, NULL);
25491 if (parm == NULL)
25492 parameter_declaration = error_mark_node;
25493 else
25494 parameter_declaration = grokdeclarator (parm->declarator,
25495 &parm->decl_specifiers,
25496 PARM, /*initialized=*/0,
25497 /*attrlist=*/NULL);
25498 }
25499 if (seen_open_paren)
25500 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25501 else
25502 {
25503 /* If there was no open parenthesis, we are recovering from
25504 an error, and we are trying to figure out what mistake
25505 the user has made. */
25506
25507 /* If there is an immediate closing parenthesis, the user
25508 probably forgot the opening one (ie, they typed "@catch
25509 NSException *e)". Parse the closing parenthesis and keep
25510 going. */
25511 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25512 cp_lexer_consume_token (parser->lexer);
25513
25514 /* If these is no immediate closing parenthesis, the user
25515 probably doesn't know that parenthesis are required at
25516 all (ie, they typed "@catch NSException *e"). So, just
25517 forget about the closing parenthesis and keep going. */
25518 }
25519 objc_begin_catch_clause (parameter_declaration);
25520 cp_parser_compound_statement (parser, NULL, false, false);
25521 objc_finish_catch_clause ();
25522 }
25523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25524 {
25525 cp_lexer_consume_token (parser->lexer);
25526 location = cp_lexer_peek_token (parser->lexer)->location;
25527 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25528 node, lest it get absorbed into the surrounding block. */
25529 stmt = push_stmt_list ();
25530 cp_parser_compound_statement (parser, NULL, false, false);
25531 objc_build_finally_clause (location, pop_stmt_list (stmt));
25532 }
25533
25534 return objc_finish_try_stmt ();
25535 }
25536
25537 /* Parse an Objective-C synchronized statement.
25538
25539 objc-synchronized-stmt:
25540 @synchronized ( expression ) compound-statement
25541
25542 Returns NULL_TREE. */
25543
25544 static tree
25545 cp_parser_objc_synchronized_statement (cp_parser *parser)
25546 {
25547 location_t location;
25548 tree lock, stmt;
25549
25550 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25551
25552 location = cp_lexer_peek_token (parser->lexer)->location;
25553 objc_maybe_warn_exceptions (location);
25554 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25555 lock = cp_parser_expression (parser, false, NULL);
25556 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25557
25558 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25559 node, lest it get absorbed into the surrounding block. */
25560 stmt = push_stmt_list ();
25561 cp_parser_compound_statement (parser, NULL, false, false);
25562
25563 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25564 }
25565
25566 /* Parse an Objective-C throw statement.
25567
25568 objc-throw-stmt:
25569 @throw assignment-expression [opt] ;
25570
25571 Returns a constructed '@throw' statement. */
25572
25573 static tree
25574 cp_parser_objc_throw_statement (cp_parser *parser)
25575 {
25576 tree expr = NULL_TREE;
25577 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25578
25579 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25580
25581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25582 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25583
25584 cp_parser_consume_semicolon_at_end_of_statement (parser);
25585
25586 return objc_build_throw_stmt (loc, expr);
25587 }
25588
25589 /* Parse an Objective-C statement. */
25590
25591 static tree
25592 cp_parser_objc_statement (cp_parser * parser)
25593 {
25594 /* Try to figure out what kind of declaration is present. */
25595 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25596
25597 switch (kwd->keyword)
25598 {
25599 case RID_AT_TRY:
25600 return cp_parser_objc_try_catch_finally_statement (parser);
25601 case RID_AT_SYNCHRONIZED:
25602 return cp_parser_objc_synchronized_statement (parser);
25603 case RID_AT_THROW:
25604 return cp_parser_objc_throw_statement (parser);
25605 default:
25606 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25607 kwd->u.value);
25608 cp_parser_skip_to_end_of_block_or_statement (parser);
25609 }
25610
25611 return error_mark_node;
25612 }
25613
25614 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25615 look ahead to see if an objc keyword follows the attributes. This
25616 is to detect the use of prefix attributes on ObjC @interface and
25617 @protocol. */
25618
25619 static bool
25620 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25621 {
25622 cp_lexer_save_tokens (parser->lexer);
25623 *attrib = cp_parser_attributes_opt (parser);
25624 gcc_assert (*attrib);
25625 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25626 {
25627 cp_lexer_commit_tokens (parser->lexer);
25628 return true;
25629 }
25630 cp_lexer_rollback_tokens (parser->lexer);
25631 return false;
25632 }
25633
25634 /* This routine is a minimal replacement for
25635 c_parser_struct_declaration () used when parsing the list of
25636 types/names or ObjC++ properties. For example, when parsing the
25637 code
25638
25639 @property (readonly) int a, b, c;
25640
25641 this function is responsible for parsing "int a, int b, int c" and
25642 returning the declarations as CHAIN of DECLs.
25643
25644 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25645 similar parsing. */
25646 static tree
25647 cp_parser_objc_struct_declaration (cp_parser *parser)
25648 {
25649 tree decls = NULL_TREE;
25650 cp_decl_specifier_seq declspecs;
25651 int decl_class_or_enum_p;
25652 tree prefix_attributes;
25653
25654 cp_parser_decl_specifier_seq (parser,
25655 CP_PARSER_FLAGS_NONE,
25656 &declspecs,
25657 &decl_class_or_enum_p);
25658
25659 if (declspecs.type == error_mark_node)
25660 return error_mark_node;
25661
25662 /* auto, register, static, extern, mutable. */
25663 if (declspecs.storage_class != sc_none)
25664 {
25665 cp_parser_error (parser, "invalid type for property");
25666 declspecs.storage_class = sc_none;
25667 }
25668
25669 /* thread_local. */
25670 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25671 {
25672 cp_parser_error (parser, "invalid type for property");
25673 declspecs.locations[ds_thread] = 0;
25674 }
25675
25676 /* typedef. */
25677 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25678 {
25679 cp_parser_error (parser, "invalid type for property");
25680 declspecs.locations[ds_typedef] = 0;
25681 }
25682
25683 prefix_attributes = declspecs.attributes;
25684 declspecs.attributes = NULL_TREE;
25685
25686 /* Keep going until we hit the `;' at the end of the declaration. */
25687 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25688 {
25689 tree attributes, first_attribute, decl;
25690 cp_declarator *declarator;
25691 cp_token *token;
25692
25693 /* Parse the declarator. */
25694 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25695 NULL, NULL, false);
25696
25697 /* Look for attributes that apply to the ivar. */
25698 attributes = cp_parser_attributes_opt (parser);
25699 /* Remember which attributes are prefix attributes and
25700 which are not. */
25701 first_attribute = attributes;
25702 /* Combine the attributes. */
25703 attributes = chainon (prefix_attributes, attributes);
25704
25705 decl = grokfield (declarator, &declspecs,
25706 NULL_TREE, /*init_const_expr_p=*/false,
25707 NULL_TREE, attributes);
25708
25709 if (decl == error_mark_node || decl == NULL_TREE)
25710 return error_mark_node;
25711
25712 /* Reset PREFIX_ATTRIBUTES. */
25713 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25714 attributes = TREE_CHAIN (attributes);
25715 if (attributes)
25716 TREE_CHAIN (attributes) = NULL_TREE;
25717
25718 DECL_CHAIN (decl) = decls;
25719 decls = decl;
25720
25721 token = cp_lexer_peek_token (parser->lexer);
25722 if (token->type == CPP_COMMA)
25723 {
25724 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25725 continue;
25726 }
25727 else
25728 break;
25729 }
25730 return decls;
25731 }
25732
25733 /* Parse an Objective-C @property declaration. The syntax is:
25734
25735 objc-property-declaration:
25736 '@property' objc-property-attributes[opt] struct-declaration ;
25737
25738 objc-property-attributes:
25739 '(' objc-property-attribute-list ')'
25740
25741 objc-property-attribute-list:
25742 objc-property-attribute
25743 objc-property-attribute-list, objc-property-attribute
25744
25745 objc-property-attribute
25746 'getter' = identifier
25747 'setter' = identifier
25748 'readonly'
25749 'readwrite'
25750 'assign'
25751 'retain'
25752 'copy'
25753 'nonatomic'
25754
25755 For example:
25756 @property NSString *name;
25757 @property (readonly) id object;
25758 @property (retain, nonatomic, getter=getTheName) id name;
25759 @property int a, b, c;
25760
25761 PS: This function is identical to
25762 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25763 static void
25764 cp_parser_objc_at_property_declaration (cp_parser *parser)
25765 {
25766 /* The following variables hold the attributes of the properties as
25767 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25768 seen. When we see an attribute, we set them to 'true' (if they
25769 are boolean properties) or to the identifier (if they have an
25770 argument, ie, for getter and setter). Note that here we only
25771 parse the list of attributes, check the syntax and accumulate the
25772 attributes that we find. objc_add_property_declaration() will
25773 then process the information. */
25774 bool property_assign = false;
25775 bool property_copy = false;
25776 tree property_getter_ident = NULL_TREE;
25777 bool property_nonatomic = false;
25778 bool property_readonly = false;
25779 bool property_readwrite = false;
25780 bool property_retain = false;
25781 tree property_setter_ident = NULL_TREE;
25782
25783 /* 'properties' is the list of properties that we read. Usually a
25784 single one, but maybe more (eg, in "@property int a, b, c;" there
25785 are three). */
25786 tree properties;
25787 location_t loc;
25788
25789 loc = cp_lexer_peek_token (parser->lexer)->location;
25790
25791 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
25792
25793 /* Parse the optional attribute list... */
25794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25795 {
25796 /* Eat the '('. */
25797 cp_lexer_consume_token (parser->lexer);
25798
25799 while (true)
25800 {
25801 bool syntax_error = false;
25802 cp_token *token = cp_lexer_peek_token (parser->lexer);
25803 enum rid keyword;
25804
25805 if (token->type != CPP_NAME)
25806 {
25807 cp_parser_error (parser, "expected identifier");
25808 break;
25809 }
25810 keyword = C_RID_CODE (token->u.value);
25811 cp_lexer_consume_token (parser->lexer);
25812 switch (keyword)
25813 {
25814 case RID_ASSIGN: property_assign = true; break;
25815 case RID_COPY: property_copy = true; break;
25816 case RID_NONATOMIC: property_nonatomic = true; break;
25817 case RID_READONLY: property_readonly = true; break;
25818 case RID_READWRITE: property_readwrite = true; break;
25819 case RID_RETAIN: property_retain = true; break;
25820
25821 case RID_GETTER:
25822 case RID_SETTER:
25823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25824 {
25825 if (keyword == RID_GETTER)
25826 cp_parser_error (parser,
25827 "missing %<=%> (after %<getter%> attribute)");
25828 else
25829 cp_parser_error (parser,
25830 "missing %<=%> (after %<setter%> attribute)");
25831 syntax_error = true;
25832 break;
25833 }
25834 cp_lexer_consume_token (parser->lexer); /* eat the = */
25835 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25836 {
25837 cp_parser_error (parser, "expected identifier");
25838 syntax_error = true;
25839 break;
25840 }
25841 if (keyword == RID_SETTER)
25842 {
25843 if (property_setter_ident != NULL_TREE)
25844 {
25845 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25846 cp_lexer_consume_token (parser->lexer);
25847 }
25848 else
25849 property_setter_ident = cp_parser_objc_selector (parser);
25850 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25851 cp_parser_error (parser, "setter name must terminate with %<:%>");
25852 else
25853 cp_lexer_consume_token (parser->lexer);
25854 }
25855 else
25856 {
25857 if (property_getter_ident != NULL_TREE)
25858 {
25859 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25860 cp_lexer_consume_token (parser->lexer);
25861 }
25862 else
25863 property_getter_ident = cp_parser_objc_selector (parser);
25864 }
25865 break;
25866 default:
25867 cp_parser_error (parser, "unknown property attribute");
25868 syntax_error = true;
25869 break;
25870 }
25871
25872 if (syntax_error)
25873 break;
25874
25875 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25876 cp_lexer_consume_token (parser->lexer);
25877 else
25878 break;
25879 }
25880
25881 /* FIXME: "@property (setter, assign);" will generate a spurious
25882 "error: expected ‘)’ before ‘,’ token". This is because
25883 cp_parser_require, unlike the C counterpart, will produce an
25884 error even if we are in error recovery. */
25885 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25886 {
25887 cp_parser_skip_to_closing_parenthesis (parser,
25888 /*recovering=*/true,
25889 /*or_comma=*/false,
25890 /*consume_paren=*/true);
25891 }
25892 }
25893
25894 /* ... and the property declaration(s). */
25895 properties = cp_parser_objc_struct_declaration (parser);
25896
25897 if (properties == error_mark_node)
25898 {
25899 cp_parser_skip_to_end_of_statement (parser);
25900 /* If the next token is now a `;', consume it. */
25901 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25902 cp_lexer_consume_token (parser->lexer);
25903 return;
25904 }
25905
25906 if (properties == NULL_TREE)
25907 cp_parser_error (parser, "expected identifier");
25908 else
25909 {
25910 /* Comma-separated properties are chained together in
25911 reverse order; add them one by one. */
25912 properties = nreverse (properties);
25913
25914 for (; properties; properties = TREE_CHAIN (properties))
25915 objc_add_property_declaration (loc, copy_node (properties),
25916 property_readonly, property_readwrite,
25917 property_assign, property_retain,
25918 property_copy, property_nonatomic,
25919 property_getter_ident, property_setter_ident);
25920 }
25921
25922 cp_parser_consume_semicolon_at_end_of_statement (parser);
25923 }
25924
25925 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25926
25927 objc-synthesize-declaration:
25928 @synthesize objc-synthesize-identifier-list ;
25929
25930 objc-synthesize-identifier-list:
25931 objc-synthesize-identifier
25932 objc-synthesize-identifier-list, objc-synthesize-identifier
25933
25934 objc-synthesize-identifier
25935 identifier
25936 identifier = identifier
25937
25938 For example:
25939 @synthesize MyProperty;
25940 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25941
25942 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25943 for C. Keep them in sync.
25944 */
25945 static void
25946 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25947 {
25948 tree list = NULL_TREE;
25949 location_t loc;
25950 loc = cp_lexer_peek_token (parser->lexer)->location;
25951
25952 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
25953 while (true)
25954 {
25955 tree property, ivar;
25956 property = cp_parser_identifier (parser);
25957 if (property == error_mark_node)
25958 {
25959 cp_parser_consume_semicolon_at_end_of_statement (parser);
25960 return;
25961 }
25962 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25963 {
25964 cp_lexer_consume_token (parser->lexer);
25965 ivar = cp_parser_identifier (parser);
25966 if (ivar == error_mark_node)
25967 {
25968 cp_parser_consume_semicolon_at_end_of_statement (parser);
25969 return;
25970 }
25971 }
25972 else
25973 ivar = NULL_TREE;
25974 list = chainon (list, build_tree_list (ivar, property));
25975 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25976 cp_lexer_consume_token (parser->lexer);
25977 else
25978 break;
25979 }
25980 cp_parser_consume_semicolon_at_end_of_statement (parser);
25981 objc_add_synthesize_declaration (loc, list);
25982 }
25983
25984 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25985
25986 objc-dynamic-declaration:
25987 @dynamic identifier-list ;
25988
25989 For example:
25990 @dynamic MyProperty;
25991 @dynamic MyProperty, AnotherProperty;
25992
25993 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25994 for C. Keep them in sync.
25995 */
25996 static void
25997 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25998 {
25999 tree list = NULL_TREE;
26000 location_t loc;
26001 loc = cp_lexer_peek_token (parser->lexer)->location;
26002
26003 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26004 while (true)
26005 {
26006 tree property;
26007 property = cp_parser_identifier (parser);
26008 if (property == error_mark_node)
26009 {
26010 cp_parser_consume_semicolon_at_end_of_statement (parser);
26011 return;
26012 }
26013 list = chainon (list, build_tree_list (NULL, property));
26014 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26015 cp_lexer_consume_token (parser->lexer);
26016 else
26017 break;
26018 }
26019 cp_parser_consume_semicolon_at_end_of_statement (parser);
26020 objc_add_dynamic_declaration (loc, list);
26021 }
26022
26023 \f
26024 /* OpenMP 2.5 parsing routines. */
26025
26026 /* Returns name of the next clause.
26027 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26028 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26029 returned and the token is consumed. */
26030
26031 static pragma_omp_clause
26032 cp_parser_omp_clause_name (cp_parser *parser)
26033 {
26034 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26035
26036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26037 result = PRAGMA_OMP_CLAUSE_IF;
26038 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26039 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26040 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26041 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26042 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26043 {
26044 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26045 const char *p = IDENTIFIER_POINTER (id);
26046
26047 switch (p[0])
26048 {
26049 case 'c':
26050 if (!strcmp ("collapse", p))
26051 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
26052 else if (!strcmp ("copyin", p))
26053 result = PRAGMA_OMP_CLAUSE_COPYIN;
26054 else if (!strcmp ("copyprivate", p))
26055 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
26056 break;
26057 case 'f':
26058 if (!strcmp ("final", p))
26059 result = PRAGMA_OMP_CLAUSE_FINAL;
26060 else if (!strcmp ("firstprivate", p))
26061 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
26062 break;
26063 case 'l':
26064 if (!strcmp ("lastprivate", p))
26065 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
26066 break;
26067 case 'm':
26068 if (!strcmp ("mergeable", p))
26069 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
26070 break;
26071 case 'n':
26072 if (!strcmp ("nowait", p))
26073 result = PRAGMA_OMP_CLAUSE_NOWAIT;
26074 else if (!strcmp ("num_threads", p))
26075 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
26076 break;
26077 case 'o':
26078 if (!strcmp ("ordered", p))
26079 result = PRAGMA_OMP_CLAUSE_ORDERED;
26080 break;
26081 case 'r':
26082 if (!strcmp ("reduction", p))
26083 result = PRAGMA_OMP_CLAUSE_REDUCTION;
26084 break;
26085 case 's':
26086 if (!strcmp ("schedule", p))
26087 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
26088 else if (!strcmp ("shared", p))
26089 result = PRAGMA_OMP_CLAUSE_SHARED;
26090 break;
26091 case 'u':
26092 if (!strcmp ("untied", p))
26093 result = PRAGMA_OMP_CLAUSE_UNTIED;
26094 break;
26095 }
26096 }
26097
26098 if (result != PRAGMA_OMP_CLAUSE_NONE)
26099 cp_lexer_consume_token (parser->lexer);
26100
26101 return result;
26102 }
26103
26104 /* Validate that a clause of the given type does not already exist. */
26105
26106 static void
26107 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
26108 const char *name, location_t location)
26109 {
26110 tree c;
26111
26112 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26113 if (OMP_CLAUSE_CODE (c) == code)
26114 {
26115 error_at (location, "too many %qs clauses", name);
26116 break;
26117 }
26118 }
26119
26120 /* OpenMP 2.5:
26121 variable-list:
26122 identifier
26123 variable-list , identifier
26124
26125 In addition, we match a closing parenthesis. An opening parenthesis
26126 will have been consumed by the caller.
26127
26128 If KIND is nonzero, create the appropriate node and install the decl
26129 in OMP_CLAUSE_DECL and add the node to the head of the list.
26130
26131 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
26132 return the list created. */
26133
26134 static tree
26135 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
26136 tree list)
26137 {
26138 cp_token *token;
26139 while (1)
26140 {
26141 tree name, decl;
26142
26143 token = cp_lexer_peek_token (parser->lexer);
26144 name = cp_parser_id_expression (parser, /*template_p=*/false,
26145 /*check_dependency_p=*/true,
26146 /*template_p=*/NULL,
26147 /*declarator_p=*/false,
26148 /*optional_p=*/false);
26149 if (name == error_mark_node)
26150 goto skip_comma;
26151
26152 decl = cp_parser_lookup_name_simple (parser, name, token->location);
26153 if (decl == error_mark_node)
26154 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
26155 token->location);
26156 else if (kind != 0)
26157 {
26158 tree u = build_omp_clause (token->location, kind);
26159 OMP_CLAUSE_DECL (u) = decl;
26160 OMP_CLAUSE_CHAIN (u) = list;
26161 list = u;
26162 }
26163 else
26164 list = tree_cons (decl, NULL_TREE, list);
26165
26166 get_comma:
26167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26168 break;
26169 cp_lexer_consume_token (parser->lexer);
26170 }
26171
26172 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26173 {
26174 int ending;
26175
26176 /* Try to resync to an unnested comma. Copied from
26177 cp_parser_parenthesized_expression_list. */
26178 skip_comma:
26179 ending = cp_parser_skip_to_closing_parenthesis (parser,
26180 /*recovering=*/true,
26181 /*or_comma=*/true,
26182 /*consume_paren=*/true);
26183 if (ending < 0)
26184 goto get_comma;
26185 }
26186
26187 return list;
26188 }
26189
26190 /* Similarly, but expect leading and trailing parenthesis. This is a very
26191 common case for omp clauses. */
26192
26193 static tree
26194 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
26195 {
26196 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26197 return cp_parser_omp_var_list_no_open (parser, kind, list);
26198 return list;
26199 }
26200
26201 /* OpenMP 3.0:
26202 collapse ( constant-expression ) */
26203
26204 static tree
26205 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
26206 {
26207 tree c, num;
26208 location_t loc;
26209 HOST_WIDE_INT n;
26210
26211 loc = cp_lexer_peek_token (parser->lexer)->location;
26212 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26213 return list;
26214
26215 num = cp_parser_constant_expression (parser, false, NULL);
26216
26217 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26218 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26219 /*or_comma=*/false,
26220 /*consume_paren=*/true);
26221
26222 if (num == error_mark_node)
26223 return list;
26224 num = fold_non_dependent_expr (num);
26225 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
26226 || !host_integerp (num, 0)
26227 || (n = tree_low_cst (num, 0)) <= 0
26228 || (int) n != n)
26229 {
26230 error_at (loc, "collapse argument needs positive constant integer expression");
26231 return list;
26232 }
26233
26234 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
26235 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
26236 OMP_CLAUSE_CHAIN (c) = list;
26237 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
26238
26239 return c;
26240 }
26241
26242 /* OpenMP 2.5:
26243 default ( shared | none ) */
26244
26245 static tree
26246 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
26247 {
26248 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
26249 tree c;
26250
26251 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26252 return list;
26253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26254 {
26255 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26256 const char *p = IDENTIFIER_POINTER (id);
26257
26258 switch (p[0])
26259 {
26260 case 'n':
26261 if (strcmp ("none", p) != 0)
26262 goto invalid_kind;
26263 kind = OMP_CLAUSE_DEFAULT_NONE;
26264 break;
26265
26266 case 's':
26267 if (strcmp ("shared", p) != 0)
26268 goto invalid_kind;
26269 kind = OMP_CLAUSE_DEFAULT_SHARED;
26270 break;
26271
26272 default:
26273 goto invalid_kind;
26274 }
26275
26276 cp_lexer_consume_token (parser->lexer);
26277 }
26278 else
26279 {
26280 invalid_kind:
26281 cp_parser_error (parser, "expected %<none%> or %<shared%>");
26282 }
26283
26284 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26285 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26286 /*or_comma=*/false,
26287 /*consume_paren=*/true);
26288
26289 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
26290 return list;
26291
26292 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
26293 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
26294 OMP_CLAUSE_CHAIN (c) = list;
26295 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
26296
26297 return c;
26298 }
26299
26300 /* OpenMP 3.1:
26301 final ( expression ) */
26302
26303 static tree
26304 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
26305 {
26306 tree t, c;
26307
26308 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26309 return list;
26310
26311 t = cp_parser_condition (parser);
26312
26313 if (t == error_mark_node
26314 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26315 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26316 /*or_comma=*/false,
26317 /*consume_paren=*/true);
26318
26319 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
26320
26321 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
26322 OMP_CLAUSE_FINAL_EXPR (c) = t;
26323 OMP_CLAUSE_CHAIN (c) = list;
26324
26325 return c;
26326 }
26327
26328 /* OpenMP 2.5:
26329 if ( expression ) */
26330
26331 static tree
26332 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
26333 {
26334 tree t, c;
26335
26336 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26337 return list;
26338
26339 t = cp_parser_condition (parser);
26340
26341 if (t == error_mark_node
26342 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26344 /*or_comma=*/false,
26345 /*consume_paren=*/true);
26346
26347 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
26348
26349 c = build_omp_clause (location, OMP_CLAUSE_IF);
26350 OMP_CLAUSE_IF_EXPR (c) = t;
26351 OMP_CLAUSE_CHAIN (c) = list;
26352
26353 return c;
26354 }
26355
26356 /* OpenMP 3.1:
26357 mergeable */
26358
26359 static tree
26360 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
26361 tree list, location_t location)
26362 {
26363 tree c;
26364
26365 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
26366 location);
26367
26368 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
26369 OMP_CLAUSE_CHAIN (c) = list;
26370 return c;
26371 }
26372
26373 /* OpenMP 2.5:
26374 nowait */
26375
26376 static tree
26377 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
26378 tree list, location_t location)
26379 {
26380 tree c;
26381
26382 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
26383
26384 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
26385 OMP_CLAUSE_CHAIN (c) = list;
26386 return c;
26387 }
26388
26389 /* OpenMP 2.5:
26390 num_threads ( expression ) */
26391
26392 static tree
26393 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
26394 location_t location)
26395 {
26396 tree t, c;
26397
26398 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26399 return list;
26400
26401 t = cp_parser_expression (parser, false, NULL);
26402
26403 if (t == error_mark_node
26404 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26406 /*or_comma=*/false,
26407 /*consume_paren=*/true);
26408
26409 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
26410 "num_threads", location);
26411
26412 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
26413 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
26414 OMP_CLAUSE_CHAIN (c) = list;
26415
26416 return c;
26417 }
26418
26419 /* OpenMP 2.5:
26420 ordered */
26421
26422 static tree
26423 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
26424 tree list, location_t location)
26425 {
26426 tree c;
26427
26428 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
26429 "ordered", location);
26430
26431 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
26432 OMP_CLAUSE_CHAIN (c) = list;
26433 return c;
26434 }
26435
26436 /* OpenMP 2.5:
26437 reduction ( reduction-operator : variable-list )
26438
26439 reduction-operator:
26440 One of: + * - & ^ | && ||
26441
26442 OpenMP 3.1:
26443
26444 reduction-operator:
26445 One of: + * - & ^ | && || min max */
26446
26447 static tree
26448 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
26449 {
26450 enum tree_code code;
26451 tree nlist, c;
26452
26453 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26454 return list;
26455
26456 switch (cp_lexer_peek_token (parser->lexer)->type)
26457 {
26458 case CPP_PLUS:
26459 code = PLUS_EXPR;
26460 break;
26461 case CPP_MULT:
26462 code = MULT_EXPR;
26463 break;
26464 case CPP_MINUS:
26465 code = MINUS_EXPR;
26466 break;
26467 case CPP_AND:
26468 code = BIT_AND_EXPR;
26469 break;
26470 case CPP_XOR:
26471 code = BIT_XOR_EXPR;
26472 break;
26473 case CPP_OR:
26474 code = BIT_IOR_EXPR;
26475 break;
26476 case CPP_AND_AND:
26477 code = TRUTH_ANDIF_EXPR;
26478 break;
26479 case CPP_OR_OR:
26480 code = TRUTH_ORIF_EXPR;
26481 break;
26482 case CPP_NAME:
26483 {
26484 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26485 const char *p = IDENTIFIER_POINTER (id);
26486
26487 if (strcmp (p, "min") == 0)
26488 {
26489 code = MIN_EXPR;
26490 break;
26491 }
26492 if (strcmp (p, "max") == 0)
26493 {
26494 code = MAX_EXPR;
26495 break;
26496 }
26497 }
26498 /* FALLTHROUGH */
26499 default:
26500 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26501 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26502 resync_fail:
26503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26504 /*or_comma=*/false,
26505 /*consume_paren=*/true);
26506 return list;
26507 }
26508 cp_lexer_consume_token (parser->lexer);
26509
26510 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26511 goto resync_fail;
26512
26513 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26514 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26515 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26516
26517 return nlist;
26518 }
26519
26520 /* OpenMP 2.5:
26521 schedule ( schedule-kind )
26522 schedule ( schedule-kind , expression )
26523
26524 schedule-kind:
26525 static | dynamic | guided | runtime | auto */
26526
26527 static tree
26528 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26529 {
26530 tree c, t;
26531
26532 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26533 return list;
26534
26535 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26536
26537 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26538 {
26539 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26540 const char *p = IDENTIFIER_POINTER (id);
26541
26542 switch (p[0])
26543 {
26544 case 'd':
26545 if (strcmp ("dynamic", p) != 0)
26546 goto invalid_kind;
26547 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26548 break;
26549
26550 case 'g':
26551 if (strcmp ("guided", p) != 0)
26552 goto invalid_kind;
26553 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26554 break;
26555
26556 case 'r':
26557 if (strcmp ("runtime", p) != 0)
26558 goto invalid_kind;
26559 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26560 break;
26561
26562 default:
26563 goto invalid_kind;
26564 }
26565 }
26566 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26567 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26568 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26569 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26570 else
26571 goto invalid_kind;
26572 cp_lexer_consume_token (parser->lexer);
26573
26574 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26575 {
26576 cp_token *token;
26577 cp_lexer_consume_token (parser->lexer);
26578
26579 token = cp_lexer_peek_token (parser->lexer);
26580 t = cp_parser_assignment_expression (parser, false, NULL);
26581
26582 if (t == error_mark_node)
26583 goto resync_fail;
26584 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26585 error_at (token->location, "schedule %<runtime%> does not take "
26586 "a %<chunk_size%> parameter");
26587 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26588 error_at (token->location, "schedule %<auto%> does not take "
26589 "a %<chunk_size%> parameter");
26590 else
26591 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26592
26593 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26594 goto resync_fail;
26595 }
26596 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26597 goto resync_fail;
26598
26599 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26600 OMP_CLAUSE_CHAIN (c) = list;
26601 return c;
26602
26603 invalid_kind:
26604 cp_parser_error (parser, "invalid schedule kind");
26605 resync_fail:
26606 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26607 /*or_comma=*/false,
26608 /*consume_paren=*/true);
26609 return list;
26610 }
26611
26612 /* OpenMP 3.0:
26613 untied */
26614
26615 static tree
26616 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26617 tree list, location_t location)
26618 {
26619 tree c;
26620
26621 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26622
26623 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26624 OMP_CLAUSE_CHAIN (c) = list;
26625 return c;
26626 }
26627
26628 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26629 is a bitmask in MASK. Return the list of clauses found; the result
26630 of clause default goes in *pdefault. */
26631
26632 static tree
26633 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26634 const char *where, cp_token *pragma_tok)
26635 {
26636 tree clauses = NULL;
26637 bool first = true;
26638 cp_token *token = NULL;
26639
26640 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26641 {
26642 pragma_omp_clause c_kind;
26643 const char *c_name;
26644 tree prev = clauses;
26645
26646 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26647 cp_lexer_consume_token (parser->lexer);
26648
26649 token = cp_lexer_peek_token (parser->lexer);
26650 c_kind = cp_parser_omp_clause_name (parser);
26651 first = false;
26652
26653 switch (c_kind)
26654 {
26655 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26656 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26657 token->location);
26658 c_name = "collapse";
26659 break;
26660 case PRAGMA_OMP_CLAUSE_COPYIN:
26661 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26662 c_name = "copyin";
26663 break;
26664 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26665 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26666 clauses);
26667 c_name = "copyprivate";
26668 break;
26669 case PRAGMA_OMP_CLAUSE_DEFAULT:
26670 clauses = cp_parser_omp_clause_default (parser, clauses,
26671 token->location);
26672 c_name = "default";
26673 break;
26674 case PRAGMA_OMP_CLAUSE_FINAL:
26675 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26676 c_name = "final";
26677 break;
26678 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26679 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26680 clauses);
26681 c_name = "firstprivate";
26682 break;
26683 case PRAGMA_OMP_CLAUSE_IF:
26684 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26685 c_name = "if";
26686 break;
26687 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26688 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26689 clauses);
26690 c_name = "lastprivate";
26691 break;
26692 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26693 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26694 token->location);
26695 c_name = "mergeable";
26696 break;
26697 case PRAGMA_OMP_CLAUSE_NOWAIT:
26698 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26699 c_name = "nowait";
26700 break;
26701 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26702 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26703 token->location);
26704 c_name = "num_threads";
26705 break;
26706 case PRAGMA_OMP_CLAUSE_ORDERED:
26707 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26708 token->location);
26709 c_name = "ordered";
26710 break;
26711 case PRAGMA_OMP_CLAUSE_PRIVATE:
26712 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26713 clauses);
26714 c_name = "private";
26715 break;
26716 case PRAGMA_OMP_CLAUSE_REDUCTION:
26717 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26718 c_name = "reduction";
26719 break;
26720 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26721 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26722 token->location);
26723 c_name = "schedule";
26724 break;
26725 case PRAGMA_OMP_CLAUSE_SHARED:
26726 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26727 clauses);
26728 c_name = "shared";
26729 break;
26730 case PRAGMA_OMP_CLAUSE_UNTIED:
26731 clauses = cp_parser_omp_clause_untied (parser, clauses,
26732 token->location);
26733 c_name = "nowait";
26734 break;
26735 default:
26736 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26737 goto saw_error;
26738 }
26739
26740 if (((mask >> c_kind) & 1) == 0)
26741 {
26742 /* Remove the invalid clause(s) from the list to avoid
26743 confusing the rest of the compiler. */
26744 clauses = prev;
26745 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26746 }
26747 }
26748 saw_error:
26749 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26750 return finish_omp_clauses (clauses);
26751 }
26752
26753 /* OpenMP 2.5:
26754 structured-block:
26755 statement
26756
26757 In practice, we're also interested in adding the statement to an
26758 outer node. So it is convenient if we work around the fact that
26759 cp_parser_statement calls add_stmt. */
26760
26761 static unsigned
26762 cp_parser_begin_omp_structured_block (cp_parser *parser)
26763 {
26764 unsigned save = parser->in_statement;
26765
26766 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26767 This preserves the "not within loop or switch" style error messages
26768 for nonsense cases like
26769 void foo() {
26770 #pragma omp single
26771 break;
26772 }
26773 */
26774 if (parser->in_statement)
26775 parser->in_statement = IN_OMP_BLOCK;
26776
26777 return save;
26778 }
26779
26780 static void
26781 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26782 {
26783 parser->in_statement = save;
26784 }
26785
26786 static tree
26787 cp_parser_omp_structured_block (cp_parser *parser)
26788 {
26789 tree stmt = begin_omp_structured_block ();
26790 unsigned int save = cp_parser_begin_omp_structured_block (parser);
26791
26792 cp_parser_statement (parser, NULL_TREE, false, NULL);
26793
26794 cp_parser_end_omp_structured_block (parser, save);
26795 return finish_omp_structured_block (stmt);
26796 }
26797
26798 /* OpenMP 2.5:
26799 # pragma omp atomic new-line
26800 expression-stmt
26801
26802 expression-stmt:
26803 x binop= expr | x++ | ++x | x-- | --x
26804 binop:
26805 +, *, -, /, &, ^, |, <<, >>
26806
26807 where x is an lvalue expression with scalar type.
26808
26809 OpenMP 3.1:
26810 # pragma omp atomic new-line
26811 update-stmt
26812
26813 # pragma omp atomic read new-line
26814 read-stmt
26815
26816 # pragma omp atomic write new-line
26817 write-stmt
26818
26819 # pragma omp atomic update new-line
26820 update-stmt
26821
26822 # pragma omp atomic capture new-line
26823 capture-stmt
26824
26825 # pragma omp atomic capture new-line
26826 capture-block
26827
26828 read-stmt:
26829 v = x
26830 write-stmt:
26831 x = expr
26832 update-stmt:
26833 expression-stmt | x = x binop expr
26834 capture-stmt:
26835 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26836 capture-block:
26837 { v = x; update-stmt; } | { update-stmt; v = x; }
26838
26839 where x and v are lvalue expressions with scalar type. */
26840
26841 static void
26842 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26843 {
26844 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26845 tree rhs1 = NULL_TREE, orig_lhs;
26846 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26847 bool structured_block = false;
26848
26849 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26850 {
26851 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26852 const char *p = IDENTIFIER_POINTER (id);
26853
26854 if (!strcmp (p, "read"))
26855 code = OMP_ATOMIC_READ;
26856 else if (!strcmp (p, "write"))
26857 code = NOP_EXPR;
26858 else if (!strcmp (p, "update"))
26859 code = OMP_ATOMIC;
26860 else if (!strcmp (p, "capture"))
26861 code = OMP_ATOMIC_CAPTURE_NEW;
26862 else
26863 p = NULL;
26864 if (p)
26865 cp_lexer_consume_token (parser->lexer);
26866 }
26867 cp_parser_require_pragma_eol (parser, pragma_tok);
26868
26869 switch (code)
26870 {
26871 case OMP_ATOMIC_READ:
26872 case NOP_EXPR: /* atomic write */
26873 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26874 /*cast_p=*/false, NULL);
26875 if (v == error_mark_node)
26876 goto saw_error;
26877 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26878 goto saw_error;
26879 if (code == NOP_EXPR)
26880 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26881 else
26882 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26883 /*cast_p=*/false, NULL);
26884 if (lhs == error_mark_node)
26885 goto saw_error;
26886 if (code == NOP_EXPR)
26887 {
26888 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26889 opcode. */
26890 code = OMP_ATOMIC;
26891 rhs = lhs;
26892 lhs = v;
26893 v = NULL_TREE;
26894 }
26895 goto done;
26896 case OMP_ATOMIC_CAPTURE_NEW:
26897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26898 {
26899 cp_lexer_consume_token (parser->lexer);
26900 structured_block = true;
26901 }
26902 else
26903 {
26904 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26905 /*cast_p=*/false, NULL);
26906 if (v == error_mark_node)
26907 goto saw_error;
26908 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26909 goto saw_error;
26910 }
26911 default:
26912 break;
26913 }
26914
26915 restart:
26916 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26917 /*cast_p=*/false, NULL);
26918 orig_lhs = lhs;
26919 switch (TREE_CODE (lhs))
26920 {
26921 case ERROR_MARK:
26922 goto saw_error;
26923
26924 case POSTINCREMENT_EXPR:
26925 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26926 code = OMP_ATOMIC_CAPTURE_OLD;
26927 /* FALLTHROUGH */
26928 case PREINCREMENT_EXPR:
26929 lhs = TREE_OPERAND (lhs, 0);
26930 opcode = PLUS_EXPR;
26931 rhs = integer_one_node;
26932 break;
26933
26934 case POSTDECREMENT_EXPR:
26935 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26936 code = OMP_ATOMIC_CAPTURE_OLD;
26937 /* FALLTHROUGH */
26938 case PREDECREMENT_EXPR:
26939 lhs = TREE_OPERAND (lhs, 0);
26940 opcode = MINUS_EXPR;
26941 rhs = integer_one_node;
26942 break;
26943
26944 case COMPOUND_EXPR:
26945 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26946 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26947 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26948 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26949 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26950 (TREE_OPERAND (lhs, 1), 0), 0)))
26951 == BOOLEAN_TYPE)
26952 /* Undo effects of boolean_increment for post {in,de}crement. */
26953 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26954 /* FALLTHRU */
26955 case MODIFY_EXPR:
26956 if (TREE_CODE (lhs) == MODIFY_EXPR
26957 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26958 {
26959 /* Undo effects of boolean_increment. */
26960 if (integer_onep (TREE_OPERAND (lhs, 1)))
26961 {
26962 /* This is pre or post increment. */
26963 rhs = TREE_OPERAND (lhs, 1);
26964 lhs = TREE_OPERAND (lhs, 0);
26965 opcode = NOP_EXPR;
26966 if (code == OMP_ATOMIC_CAPTURE_NEW
26967 && !structured_block
26968 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26969 code = OMP_ATOMIC_CAPTURE_OLD;
26970 break;
26971 }
26972 }
26973 /* FALLTHRU */
26974 default:
26975 switch (cp_lexer_peek_token (parser->lexer)->type)
26976 {
26977 case CPP_MULT_EQ:
26978 opcode = MULT_EXPR;
26979 break;
26980 case CPP_DIV_EQ:
26981 opcode = TRUNC_DIV_EXPR;
26982 break;
26983 case CPP_PLUS_EQ:
26984 opcode = PLUS_EXPR;
26985 break;
26986 case CPP_MINUS_EQ:
26987 opcode = MINUS_EXPR;
26988 break;
26989 case CPP_LSHIFT_EQ:
26990 opcode = LSHIFT_EXPR;
26991 break;
26992 case CPP_RSHIFT_EQ:
26993 opcode = RSHIFT_EXPR;
26994 break;
26995 case CPP_AND_EQ:
26996 opcode = BIT_AND_EXPR;
26997 break;
26998 case CPP_OR_EQ:
26999 opcode = BIT_IOR_EXPR;
27000 break;
27001 case CPP_XOR_EQ:
27002 opcode = BIT_XOR_EXPR;
27003 break;
27004 case CPP_EQ:
27005 if (structured_block || code == OMP_ATOMIC)
27006 {
27007 enum cp_parser_prec oprec;
27008 cp_token *token;
27009 cp_lexer_consume_token (parser->lexer);
27010 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
27011 /*cast_p=*/false, NULL);
27012 if (rhs1 == error_mark_node)
27013 goto saw_error;
27014 token = cp_lexer_peek_token (parser->lexer);
27015 switch (token->type)
27016 {
27017 case CPP_SEMICOLON:
27018 if (code == OMP_ATOMIC_CAPTURE_NEW)
27019 {
27020 code = OMP_ATOMIC_CAPTURE_OLD;
27021 v = lhs;
27022 lhs = NULL_TREE;
27023 lhs1 = rhs1;
27024 rhs1 = NULL_TREE;
27025 cp_lexer_consume_token (parser->lexer);
27026 goto restart;
27027 }
27028 cp_parser_error (parser,
27029 "invalid form of %<#pragma omp atomic%>");
27030 goto saw_error;
27031 case CPP_MULT:
27032 opcode = MULT_EXPR;
27033 break;
27034 case CPP_DIV:
27035 opcode = TRUNC_DIV_EXPR;
27036 break;
27037 case CPP_PLUS:
27038 opcode = PLUS_EXPR;
27039 break;
27040 case CPP_MINUS:
27041 opcode = MINUS_EXPR;
27042 break;
27043 case CPP_LSHIFT:
27044 opcode = LSHIFT_EXPR;
27045 break;
27046 case CPP_RSHIFT:
27047 opcode = RSHIFT_EXPR;
27048 break;
27049 case CPP_AND:
27050 opcode = BIT_AND_EXPR;
27051 break;
27052 case CPP_OR:
27053 opcode = BIT_IOR_EXPR;
27054 break;
27055 case CPP_XOR:
27056 opcode = BIT_XOR_EXPR;
27057 break;
27058 default:
27059 cp_parser_error (parser,
27060 "invalid operator for %<#pragma omp atomic%>");
27061 goto saw_error;
27062 }
27063 oprec = TOKEN_PRECEDENCE (token);
27064 gcc_assert (oprec != PREC_NOT_OPERATOR);
27065 if (commutative_tree_code (opcode))
27066 oprec = (enum cp_parser_prec) (oprec - 1);
27067 cp_lexer_consume_token (parser->lexer);
27068 rhs = cp_parser_binary_expression (parser, false, false,
27069 oprec, NULL);
27070 if (rhs == error_mark_node)
27071 goto saw_error;
27072 goto stmt_done;
27073 }
27074 /* FALLTHROUGH */
27075 default:
27076 cp_parser_error (parser,
27077 "invalid operator for %<#pragma omp atomic%>");
27078 goto saw_error;
27079 }
27080 cp_lexer_consume_token (parser->lexer);
27081
27082 rhs = cp_parser_expression (parser, false, NULL);
27083 if (rhs == error_mark_node)
27084 goto saw_error;
27085 break;
27086 }
27087 stmt_done:
27088 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
27089 {
27090 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
27091 goto saw_error;
27092 v = cp_parser_unary_expression (parser, /*address_p=*/false,
27093 /*cast_p=*/false, NULL);
27094 if (v == error_mark_node)
27095 goto saw_error;
27096 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27097 goto saw_error;
27098 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
27099 /*cast_p=*/false, NULL);
27100 if (lhs1 == error_mark_node)
27101 goto saw_error;
27102 }
27103 if (structured_block)
27104 {
27105 cp_parser_consume_semicolon_at_end_of_statement (parser);
27106 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27107 }
27108 done:
27109 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
27110 if (!structured_block)
27111 cp_parser_consume_semicolon_at_end_of_statement (parser);
27112 return;
27113
27114 saw_error:
27115 cp_parser_skip_to_end_of_block_or_statement (parser);
27116 if (structured_block)
27117 {
27118 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27119 cp_lexer_consume_token (parser->lexer);
27120 else if (code == OMP_ATOMIC_CAPTURE_NEW)
27121 {
27122 cp_parser_skip_to_end_of_block_or_statement (parser);
27123 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27124 cp_lexer_consume_token (parser->lexer);
27125 }
27126 }
27127 }
27128
27129
27130 /* OpenMP 2.5:
27131 # pragma omp barrier new-line */
27132
27133 static void
27134 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
27135 {
27136 cp_parser_require_pragma_eol (parser, pragma_tok);
27137 finish_omp_barrier ();
27138 }
27139
27140 /* OpenMP 2.5:
27141 # pragma omp critical [(name)] new-line
27142 structured-block */
27143
27144 static tree
27145 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
27146 {
27147 tree stmt, name = NULL;
27148
27149 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27150 {
27151 cp_lexer_consume_token (parser->lexer);
27152
27153 name = cp_parser_identifier (parser);
27154
27155 if (name == error_mark_node
27156 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27157 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27158 /*or_comma=*/false,
27159 /*consume_paren=*/true);
27160 if (name == error_mark_node)
27161 name = NULL;
27162 }
27163 cp_parser_require_pragma_eol (parser, pragma_tok);
27164
27165 stmt = cp_parser_omp_structured_block (parser);
27166 return c_finish_omp_critical (input_location, stmt, name);
27167 }
27168
27169 /* OpenMP 2.5:
27170 # pragma omp flush flush-vars[opt] new-line
27171
27172 flush-vars:
27173 ( variable-list ) */
27174
27175 static void
27176 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
27177 {
27178 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27179 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27180 cp_parser_require_pragma_eol (parser, pragma_tok);
27181
27182 finish_omp_flush ();
27183 }
27184
27185 /* Helper function, to parse omp for increment expression. */
27186
27187 static tree
27188 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
27189 {
27190 tree cond = cp_parser_binary_expression (parser, false, true,
27191 PREC_NOT_OPERATOR, NULL);
27192 if (cond == error_mark_node
27193 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27194 {
27195 cp_parser_skip_to_end_of_statement (parser);
27196 return error_mark_node;
27197 }
27198
27199 switch (TREE_CODE (cond))
27200 {
27201 case GT_EXPR:
27202 case GE_EXPR:
27203 case LT_EXPR:
27204 case LE_EXPR:
27205 break;
27206 default:
27207 return error_mark_node;
27208 }
27209
27210 /* If decl is an iterator, preserve LHS and RHS of the relational
27211 expr until finish_omp_for. */
27212 if (decl
27213 && (type_dependent_expression_p (decl)
27214 || CLASS_TYPE_P (TREE_TYPE (decl))))
27215 return cond;
27216
27217 return build_x_binary_op (input_location, TREE_CODE (cond),
27218 TREE_OPERAND (cond, 0), ERROR_MARK,
27219 TREE_OPERAND (cond, 1), ERROR_MARK,
27220 /*overload=*/NULL, tf_warning_or_error);
27221 }
27222
27223 /* Helper function, to parse omp for increment expression. */
27224
27225 static tree
27226 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
27227 {
27228 cp_token *token = cp_lexer_peek_token (parser->lexer);
27229 enum tree_code op;
27230 tree lhs, rhs;
27231 cp_id_kind idk;
27232 bool decl_first;
27233
27234 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
27235 {
27236 op = (token->type == CPP_PLUS_PLUS
27237 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
27238 cp_lexer_consume_token (parser->lexer);
27239 lhs = cp_parser_simple_cast_expression (parser);
27240 if (lhs != decl)
27241 return error_mark_node;
27242 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27243 }
27244
27245 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
27246 if (lhs != decl)
27247 return error_mark_node;
27248
27249 token = cp_lexer_peek_token (parser->lexer);
27250 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
27251 {
27252 op = (token->type == CPP_PLUS_PLUS
27253 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
27254 cp_lexer_consume_token (parser->lexer);
27255 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27256 }
27257
27258 op = cp_parser_assignment_operator_opt (parser);
27259 if (op == ERROR_MARK)
27260 return error_mark_node;
27261
27262 if (op != NOP_EXPR)
27263 {
27264 rhs = cp_parser_assignment_expression (parser, false, NULL);
27265 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
27266 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27267 }
27268
27269 lhs = cp_parser_binary_expression (parser, false, false,
27270 PREC_ADDITIVE_EXPRESSION, NULL);
27271 token = cp_lexer_peek_token (parser->lexer);
27272 decl_first = lhs == decl;
27273 if (decl_first)
27274 lhs = NULL_TREE;
27275 if (token->type != CPP_PLUS
27276 && token->type != CPP_MINUS)
27277 return error_mark_node;
27278
27279 do
27280 {
27281 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
27282 cp_lexer_consume_token (parser->lexer);
27283 rhs = cp_parser_binary_expression (parser, false, false,
27284 PREC_ADDITIVE_EXPRESSION, NULL);
27285 token = cp_lexer_peek_token (parser->lexer);
27286 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
27287 {
27288 if (lhs == NULL_TREE)
27289 {
27290 if (op == PLUS_EXPR)
27291 lhs = rhs;
27292 else
27293 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
27294 tf_warning_or_error);
27295 }
27296 else
27297 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
27298 ERROR_MARK, NULL, tf_warning_or_error);
27299 }
27300 }
27301 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
27302
27303 if (!decl_first)
27304 {
27305 if (rhs != decl || op == MINUS_EXPR)
27306 return error_mark_node;
27307 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
27308 }
27309 else
27310 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
27311
27312 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27313 }
27314
27315 /* Parse the restricted form of the for statement allowed by OpenMP. */
27316
27317 static tree
27318 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
27319 {
27320 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
27321 tree real_decl, initv, condv, incrv, declv;
27322 tree this_pre_body, cl;
27323 location_t loc_first;
27324 bool collapse_err = false;
27325 int i, collapse = 1, nbraces = 0;
27326 vec<tree, va_gc> *for_block = make_tree_vector ();
27327
27328 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
27329 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
27330 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
27331
27332 gcc_assert (collapse >= 1);
27333
27334 declv = make_tree_vec (collapse);
27335 initv = make_tree_vec (collapse);
27336 condv = make_tree_vec (collapse);
27337 incrv = make_tree_vec (collapse);
27338
27339 loc_first = cp_lexer_peek_token (parser->lexer)->location;
27340
27341 for (i = 0; i < collapse; i++)
27342 {
27343 int bracecount = 0;
27344 bool add_private_clause = false;
27345 location_t loc;
27346
27347 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27348 {
27349 cp_parser_error (parser, "for statement expected");
27350 return NULL;
27351 }
27352 loc = cp_lexer_consume_token (parser->lexer)->location;
27353
27354 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27355 return NULL;
27356
27357 init = decl = real_decl = NULL;
27358 this_pre_body = push_stmt_list ();
27359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27360 {
27361 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
27362
27363 init-expr:
27364 var = lb
27365 integer-type var = lb
27366 random-access-iterator-type var = lb
27367 pointer-type var = lb
27368 */
27369 cp_decl_specifier_seq type_specifiers;
27370
27371 /* First, try to parse as an initialized declaration. See
27372 cp_parser_condition, from whence the bulk of this is copied. */
27373
27374 cp_parser_parse_tentatively (parser);
27375 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
27376 /*is_trailing_return=*/false,
27377 &type_specifiers);
27378 if (cp_parser_parse_definitely (parser))
27379 {
27380 /* If parsing a type specifier seq succeeded, then this
27381 MUST be a initialized declaration. */
27382 tree asm_specification, attributes;
27383 cp_declarator *declarator;
27384
27385 declarator = cp_parser_declarator (parser,
27386 CP_PARSER_DECLARATOR_NAMED,
27387 /*ctor_dtor_or_conv_p=*/NULL,
27388 /*parenthesized_p=*/NULL,
27389 /*member_p=*/false);
27390 attributes = cp_parser_attributes_opt (parser);
27391 asm_specification = cp_parser_asm_specification_opt (parser);
27392
27393 if (declarator == cp_error_declarator)
27394 cp_parser_skip_to_end_of_statement (parser);
27395
27396 else
27397 {
27398 tree pushed_scope, auto_node;
27399
27400 decl = start_decl (declarator, &type_specifiers,
27401 SD_INITIALIZED, attributes,
27402 /*prefix_attributes=*/NULL_TREE,
27403 &pushed_scope);
27404
27405 auto_node = type_uses_auto (TREE_TYPE (decl));
27406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27407 {
27408 if (cp_lexer_next_token_is (parser->lexer,
27409 CPP_OPEN_PAREN))
27410 error ("parenthesized initialization is not allowed in "
27411 "OpenMP %<for%> loop");
27412 else
27413 /* Trigger an error. */
27414 cp_parser_require (parser, CPP_EQ, RT_EQ);
27415
27416 init = error_mark_node;
27417 cp_parser_skip_to_end_of_statement (parser);
27418 }
27419 else if (CLASS_TYPE_P (TREE_TYPE (decl))
27420 || type_dependent_expression_p (decl)
27421 || auto_node)
27422 {
27423 bool is_direct_init, is_non_constant_init;
27424
27425 init = cp_parser_initializer (parser,
27426 &is_direct_init,
27427 &is_non_constant_init);
27428
27429 if (auto_node)
27430 {
27431 TREE_TYPE (decl)
27432 = do_auto_deduction (TREE_TYPE (decl), init,
27433 auto_node);
27434
27435 if (!CLASS_TYPE_P (TREE_TYPE (decl))
27436 && !type_dependent_expression_p (decl))
27437 goto non_class;
27438 }
27439
27440 cp_finish_decl (decl, init, !is_non_constant_init,
27441 asm_specification,
27442 LOOKUP_ONLYCONVERTING);
27443 if (CLASS_TYPE_P (TREE_TYPE (decl)))
27444 {
27445 vec_safe_push (for_block, this_pre_body);
27446 init = NULL_TREE;
27447 }
27448 else
27449 init = pop_stmt_list (this_pre_body);
27450 this_pre_body = NULL_TREE;
27451 }
27452 else
27453 {
27454 /* Consume '='. */
27455 cp_lexer_consume_token (parser->lexer);
27456 init = cp_parser_assignment_expression (parser, false, NULL);
27457
27458 non_class:
27459 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27460 init = error_mark_node;
27461 else
27462 cp_finish_decl (decl, NULL_TREE,
27463 /*init_const_expr_p=*/false,
27464 asm_specification,
27465 LOOKUP_ONLYCONVERTING);
27466 }
27467
27468 if (pushed_scope)
27469 pop_scope (pushed_scope);
27470 }
27471 }
27472 else
27473 {
27474 cp_id_kind idk;
27475 /* If parsing a type specifier sequence failed, then
27476 this MUST be a simple expression. */
27477 cp_parser_parse_tentatively (parser);
27478 decl = cp_parser_primary_expression (parser, false, false,
27479 false, &idk);
27480 if (!cp_parser_error_occurred (parser)
27481 && decl
27482 && DECL_P (decl)
27483 && CLASS_TYPE_P (TREE_TYPE (decl)))
27484 {
27485 tree rhs;
27486
27487 cp_parser_parse_definitely (parser);
27488 cp_parser_require (parser, CPP_EQ, RT_EQ);
27489 rhs = cp_parser_assignment_expression (parser, false, NULL);
27490 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27491 decl, NOP_EXPR,
27492 rhs,
27493 tf_warning_or_error));
27494 add_private_clause = true;
27495 }
27496 else
27497 {
27498 decl = NULL;
27499 cp_parser_abort_tentative_parse (parser);
27500 init = cp_parser_expression (parser, false, NULL);
27501 if (init)
27502 {
27503 if (TREE_CODE (init) == MODIFY_EXPR
27504 || TREE_CODE (init) == MODOP_EXPR)
27505 real_decl = TREE_OPERAND (init, 0);
27506 }
27507 }
27508 }
27509 }
27510 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27511 if (this_pre_body)
27512 {
27513 this_pre_body = pop_stmt_list (this_pre_body);
27514 if (pre_body)
27515 {
27516 tree t = pre_body;
27517 pre_body = push_stmt_list ();
27518 add_stmt (t);
27519 add_stmt (this_pre_body);
27520 pre_body = pop_stmt_list (pre_body);
27521 }
27522 else
27523 pre_body = this_pre_body;
27524 }
27525
27526 if (decl)
27527 real_decl = decl;
27528 if (par_clauses != NULL && real_decl != NULL_TREE)
27529 {
27530 tree *c;
27531 for (c = par_clauses; *c ; )
27532 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27533 && OMP_CLAUSE_DECL (*c) == real_decl)
27534 {
27535 error_at (loc, "iteration variable %qD"
27536 " should not be firstprivate", real_decl);
27537 *c = OMP_CLAUSE_CHAIN (*c);
27538 }
27539 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27540 && OMP_CLAUSE_DECL (*c) == real_decl)
27541 {
27542 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27543 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27544 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27545 OMP_CLAUSE_DECL (l) = real_decl;
27546 OMP_CLAUSE_CHAIN (l) = clauses;
27547 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27548 clauses = l;
27549 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27550 CP_OMP_CLAUSE_INFO (*c) = NULL;
27551 add_private_clause = false;
27552 }
27553 else
27554 {
27555 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27556 && OMP_CLAUSE_DECL (*c) == real_decl)
27557 add_private_clause = false;
27558 c = &OMP_CLAUSE_CHAIN (*c);
27559 }
27560 }
27561
27562 if (add_private_clause)
27563 {
27564 tree c;
27565 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27566 {
27567 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27568 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27569 && OMP_CLAUSE_DECL (c) == decl)
27570 break;
27571 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27572 && OMP_CLAUSE_DECL (c) == decl)
27573 error_at (loc, "iteration variable %qD "
27574 "should not be firstprivate",
27575 decl);
27576 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27577 && OMP_CLAUSE_DECL (c) == decl)
27578 error_at (loc, "iteration variable %qD should not be reduction",
27579 decl);
27580 }
27581 if (c == NULL)
27582 {
27583 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27584 OMP_CLAUSE_DECL (c) = decl;
27585 c = finish_omp_clauses (c);
27586 if (c)
27587 {
27588 OMP_CLAUSE_CHAIN (c) = clauses;
27589 clauses = c;
27590 }
27591 }
27592 }
27593
27594 cond = NULL;
27595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27596 cond = cp_parser_omp_for_cond (parser, decl);
27597 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27598
27599 incr = NULL;
27600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27601 {
27602 /* If decl is an iterator, preserve the operator on decl
27603 until finish_omp_for. */
27604 if (real_decl
27605 && ((processing_template_decl
27606 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27607 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27608 incr = cp_parser_omp_for_incr (parser, real_decl);
27609 else
27610 incr = cp_parser_expression (parser, false, NULL);
27611 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27612 SET_EXPR_LOCATION (incr, input_location);
27613 }
27614
27615 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27616 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27617 /*or_comma=*/false,
27618 /*consume_paren=*/true);
27619
27620 TREE_VEC_ELT (declv, i) = decl;
27621 TREE_VEC_ELT (initv, i) = init;
27622 TREE_VEC_ELT (condv, i) = cond;
27623 TREE_VEC_ELT (incrv, i) = incr;
27624
27625 if (i == collapse - 1)
27626 break;
27627
27628 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27629 in between the collapsed for loops to be still considered perfectly
27630 nested. Hopefully the final version clarifies this.
27631 For now handle (multiple) {'s and empty statements. */
27632 cp_parser_parse_tentatively (parser);
27633 do
27634 {
27635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27636 break;
27637 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27638 {
27639 cp_lexer_consume_token (parser->lexer);
27640 bracecount++;
27641 }
27642 else if (bracecount
27643 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27644 cp_lexer_consume_token (parser->lexer);
27645 else
27646 {
27647 loc = cp_lexer_peek_token (parser->lexer)->location;
27648 error_at (loc, "not enough collapsed for loops");
27649 collapse_err = true;
27650 cp_parser_abort_tentative_parse (parser);
27651 declv = NULL_TREE;
27652 break;
27653 }
27654 }
27655 while (1);
27656
27657 if (declv)
27658 {
27659 cp_parser_parse_definitely (parser);
27660 nbraces += bracecount;
27661 }
27662 }
27663
27664 /* Note that we saved the original contents of this flag when we entered
27665 the structured block, and so we don't need to re-save it here. */
27666 parser->in_statement = IN_OMP_FOR;
27667
27668 /* Note that the grammar doesn't call for a structured block here,
27669 though the loop as a whole is a structured block. */
27670 body = push_stmt_list ();
27671 cp_parser_statement (parser, NULL_TREE, false, NULL);
27672 body = pop_stmt_list (body);
27673
27674 if (declv == NULL_TREE)
27675 ret = NULL_TREE;
27676 else
27677 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27678 pre_body, clauses);
27679
27680 while (nbraces)
27681 {
27682 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27683 {
27684 cp_lexer_consume_token (parser->lexer);
27685 nbraces--;
27686 }
27687 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27688 cp_lexer_consume_token (parser->lexer);
27689 else
27690 {
27691 if (!collapse_err)
27692 {
27693 error_at (cp_lexer_peek_token (parser->lexer)->location,
27694 "collapsed loops not perfectly nested");
27695 }
27696 collapse_err = true;
27697 cp_parser_statement_seq_opt (parser, NULL);
27698 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27699 break;
27700 }
27701 }
27702
27703 while (!for_block->is_empty ())
27704 add_stmt (pop_stmt_list (for_block->pop ()));
27705 release_tree_vector (for_block);
27706
27707 return ret;
27708 }
27709
27710 /* OpenMP 2.5:
27711 #pragma omp for for-clause[optseq] new-line
27712 for-loop */
27713
27714 #define OMP_FOR_CLAUSE_MASK \
27715 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27716 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27717 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27718 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27719 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27720 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27721 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27722 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27723
27724 static tree
27725 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27726 {
27727 tree clauses, sb, ret;
27728 unsigned int save;
27729
27730 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27731 "#pragma omp for", pragma_tok);
27732
27733 sb = begin_omp_structured_block ();
27734 save = cp_parser_begin_omp_structured_block (parser);
27735
27736 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27737
27738 cp_parser_end_omp_structured_block (parser, save);
27739 add_stmt (finish_omp_structured_block (sb));
27740
27741 return ret;
27742 }
27743
27744 /* OpenMP 2.5:
27745 # pragma omp master new-line
27746 structured-block */
27747
27748 static tree
27749 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27750 {
27751 cp_parser_require_pragma_eol (parser, pragma_tok);
27752 return c_finish_omp_master (input_location,
27753 cp_parser_omp_structured_block (parser));
27754 }
27755
27756 /* OpenMP 2.5:
27757 # pragma omp ordered new-line
27758 structured-block */
27759
27760 static tree
27761 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27762 {
27763 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27764 cp_parser_require_pragma_eol (parser, pragma_tok);
27765 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27766 }
27767
27768 /* OpenMP 2.5:
27769
27770 section-scope:
27771 { section-sequence }
27772
27773 section-sequence:
27774 section-directive[opt] structured-block
27775 section-sequence section-directive structured-block */
27776
27777 static tree
27778 cp_parser_omp_sections_scope (cp_parser *parser)
27779 {
27780 tree stmt, substmt;
27781 bool error_suppress = false;
27782 cp_token *tok;
27783
27784 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27785 return NULL_TREE;
27786
27787 stmt = push_stmt_list ();
27788
27789 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27790 {
27791 unsigned save;
27792
27793 substmt = begin_omp_structured_block ();
27794 save = cp_parser_begin_omp_structured_block (parser);
27795
27796 while (1)
27797 {
27798 cp_parser_statement (parser, NULL_TREE, false, NULL);
27799
27800 tok = cp_lexer_peek_token (parser->lexer);
27801 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27802 break;
27803 if (tok->type == CPP_CLOSE_BRACE)
27804 break;
27805 if (tok->type == CPP_EOF)
27806 break;
27807 }
27808
27809 cp_parser_end_omp_structured_block (parser, save);
27810 substmt = finish_omp_structured_block (substmt);
27811 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27812 add_stmt (substmt);
27813 }
27814
27815 while (1)
27816 {
27817 tok = cp_lexer_peek_token (parser->lexer);
27818 if (tok->type == CPP_CLOSE_BRACE)
27819 break;
27820 if (tok->type == CPP_EOF)
27821 break;
27822
27823 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27824 {
27825 cp_lexer_consume_token (parser->lexer);
27826 cp_parser_require_pragma_eol (parser, tok);
27827 error_suppress = false;
27828 }
27829 else if (!error_suppress)
27830 {
27831 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27832 error_suppress = true;
27833 }
27834
27835 substmt = cp_parser_omp_structured_block (parser);
27836 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27837 add_stmt (substmt);
27838 }
27839 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27840
27841 substmt = pop_stmt_list (stmt);
27842
27843 stmt = make_node (OMP_SECTIONS);
27844 TREE_TYPE (stmt) = void_type_node;
27845 OMP_SECTIONS_BODY (stmt) = substmt;
27846
27847 add_stmt (stmt);
27848 return stmt;
27849 }
27850
27851 /* OpenMP 2.5:
27852 # pragma omp sections sections-clause[optseq] newline
27853 sections-scope */
27854
27855 #define OMP_SECTIONS_CLAUSE_MASK \
27856 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27857 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27858 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27859 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27860 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27861
27862 static tree
27863 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27864 {
27865 tree clauses, ret;
27866
27867 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27868 "#pragma omp sections", pragma_tok);
27869
27870 ret = cp_parser_omp_sections_scope (parser);
27871 if (ret)
27872 OMP_SECTIONS_CLAUSES (ret) = clauses;
27873
27874 return ret;
27875 }
27876
27877 /* OpenMP 2.5:
27878 # pragma parallel parallel-clause new-line
27879 # pragma parallel for parallel-for-clause new-line
27880 # pragma parallel sections parallel-sections-clause new-line */
27881
27882 #define OMP_PARALLEL_CLAUSE_MASK \
27883 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27884 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27885 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27886 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27887 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27888 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27889 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27890 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27891
27892 static tree
27893 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27894 {
27895 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27896 const char *p_name = "#pragma omp parallel";
27897 tree stmt, clauses, par_clause, ws_clause, block;
27898 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27899 unsigned int save;
27900 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27901
27902 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27903 {
27904 cp_lexer_consume_token (parser->lexer);
27905 p_kind = PRAGMA_OMP_PARALLEL_FOR;
27906 p_name = "#pragma omp parallel for";
27907 mask |= OMP_FOR_CLAUSE_MASK;
27908 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27909 }
27910 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27911 {
27912 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27913 const char *p = IDENTIFIER_POINTER (id);
27914 if (strcmp (p, "sections") == 0)
27915 {
27916 cp_lexer_consume_token (parser->lexer);
27917 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27918 p_name = "#pragma omp parallel sections";
27919 mask |= OMP_SECTIONS_CLAUSE_MASK;
27920 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27921 }
27922 }
27923
27924 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27925 block = begin_omp_parallel ();
27926 save = cp_parser_begin_omp_structured_block (parser);
27927
27928 switch (p_kind)
27929 {
27930 case PRAGMA_OMP_PARALLEL:
27931 cp_parser_statement (parser, NULL_TREE, false, NULL);
27932 par_clause = clauses;
27933 break;
27934
27935 case PRAGMA_OMP_PARALLEL_FOR:
27936 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27937 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27938 break;
27939
27940 case PRAGMA_OMP_PARALLEL_SECTIONS:
27941 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27942 stmt = cp_parser_omp_sections_scope (parser);
27943 if (stmt)
27944 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27945 break;
27946
27947 default:
27948 gcc_unreachable ();
27949 }
27950
27951 cp_parser_end_omp_structured_block (parser, save);
27952 stmt = finish_omp_parallel (par_clause, block);
27953 if (p_kind != PRAGMA_OMP_PARALLEL)
27954 OMP_PARALLEL_COMBINED (stmt) = 1;
27955 return stmt;
27956 }
27957
27958 /* OpenMP 2.5:
27959 # pragma omp single single-clause[optseq] new-line
27960 structured-block */
27961
27962 #define OMP_SINGLE_CLAUSE_MASK \
27963 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27964 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27965 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27966 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27967
27968 static tree
27969 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27970 {
27971 tree stmt = make_node (OMP_SINGLE);
27972 TREE_TYPE (stmt) = void_type_node;
27973
27974 OMP_SINGLE_CLAUSES (stmt)
27975 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27976 "#pragma omp single", pragma_tok);
27977 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27978
27979 return add_stmt (stmt);
27980 }
27981
27982 /* OpenMP 3.0:
27983 # pragma omp task task-clause[optseq] new-line
27984 structured-block */
27985
27986 #define OMP_TASK_CLAUSE_MASK \
27987 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27988 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27989 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27990 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27991 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27992 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27993 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27994 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27995
27996 static tree
27997 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27998 {
27999 tree clauses, block;
28000 unsigned int save;
28001
28002 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
28003 "#pragma omp task", pragma_tok);
28004 block = begin_omp_task ();
28005 save = cp_parser_begin_omp_structured_block (parser);
28006 cp_parser_statement (parser, NULL_TREE, false, NULL);
28007 cp_parser_end_omp_structured_block (parser, save);
28008 return finish_omp_task (clauses, block);
28009 }
28010
28011 /* OpenMP 3.0:
28012 # pragma omp taskwait new-line */
28013
28014 static void
28015 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
28016 {
28017 cp_parser_require_pragma_eol (parser, pragma_tok);
28018 finish_omp_taskwait ();
28019 }
28020
28021 /* OpenMP 3.1:
28022 # pragma omp taskyield new-line */
28023
28024 static void
28025 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
28026 {
28027 cp_parser_require_pragma_eol (parser, pragma_tok);
28028 finish_omp_taskyield ();
28029 }
28030
28031 /* OpenMP 2.5:
28032 # pragma omp threadprivate (variable-list) */
28033
28034 static void
28035 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
28036 {
28037 tree vars;
28038
28039 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28040 cp_parser_require_pragma_eol (parser, pragma_tok);
28041
28042 finish_omp_threadprivate (vars);
28043 }
28044
28045 /* Main entry point to OpenMP statement pragmas. */
28046
28047 static void
28048 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
28049 {
28050 tree stmt;
28051
28052 switch (pragma_tok->pragma_kind)
28053 {
28054 case PRAGMA_OMP_ATOMIC:
28055 cp_parser_omp_atomic (parser, pragma_tok);
28056 return;
28057 case PRAGMA_OMP_CRITICAL:
28058 stmt = cp_parser_omp_critical (parser, pragma_tok);
28059 break;
28060 case PRAGMA_OMP_FOR:
28061 stmt = cp_parser_omp_for (parser, pragma_tok);
28062 break;
28063 case PRAGMA_OMP_MASTER:
28064 stmt = cp_parser_omp_master (parser, pragma_tok);
28065 break;
28066 case PRAGMA_OMP_ORDERED:
28067 stmt = cp_parser_omp_ordered (parser, pragma_tok);
28068 break;
28069 case PRAGMA_OMP_PARALLEL:
28070 stmt = cp_parser_omp_parallel (parser, pragma_tok);
28071 break;
28072 case PRAGMA_OMP_SECTIONS:
28073 stmt = cp_parser_omp_sections (parser, pragma_tok);
28074 break;
28075 case PRAGMA_OMP_SINGLE:
28076 stmt = cp_parser_omp_single (parser, pragma_tok);
28077 break;
28078 case PRAGMA_OMP_TASK:
28079 stmt = cp_parser_omp_task (parser, pragma_tok);
28080 break;
28081 default:
28082 gcc_unreachable ();
28083 }
28084
28085 if (stmt)
28086 SET_EXPR_LOCATION (stmt, pragma_tok->location);
28087 }
28088 \f
28089 /* Transactional Memory parsing routines. */
28090
28091 /* Parse a transaction attribute.
28092
28093 txn-attribute:
28094 attribute
28095 [ [ identifier ] ]
28096
28097 ??? Simplify this when C++0x bracket attributes are
28098 implemented properly. */
28099
28100 static tree
28101 cp_parser_txn_attribute_opt (cp_parser *parser)
28102 {
28103 cp_token *token;
28104 tree attr_name, attr = NULL;
28105
28106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28107 return cp_parser_attributes_opt (parser);
28108
28109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
28110 return NULL_TREE;
28111 cp_lexer_consume_token (parser->lexer);
28112 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
28113 goto error1;
28114
28115 token = cp_lexer_peek_token (parser->lexer);
28116 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
28117 {
28118 token = cp_lexer_consume_token (parser->lexer);
28119
28120 attr_name = (token->type == CPP_KEYWORD
28121 /* For keywords, use the canonical spelling,
28122 not the parsed identifier. */
28123 ? ridpointers[(int) token->keyword]
28124 : token->u.value);
28125 attr = build_tree_list (attr_name, NULL_TREE);
28126 }
28127 else
28128 cp_parser_error (parser, "expected identifier");
28129
28130 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28131 error1:
28132 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28133 return attr;
28134 }
28135
28136 /* Parse a __transaction_atomic or __transaction_relaxed statement.
28137
28138 transaction-statement:
28139 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
28140 compound-statement
28141 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
28142 */
28143
28144 static tree
28145 cp_parser_transaction (cp_parser *parser, enum rid keyword)
28146 {
28147 unsigned char old_in = parser->in_transaction;
28148 unsigned char this_in = 1, new_in;
28149 cp_token *token;
28150 tree stmt, attrs, noex;
28151
28152 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28153 || keyword == RID_TRANSACTION_RELAXED);
28154 token = cp_parser_require_keyword (parser, keyword,
28155 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28156 : RT_TRANSACTION_RELAXED));
28157 gcc_assert (token != NULL);
28158
28159 if (keyword == RID_TRANSACTION_RELAXED)
28160 this_in |= TM_STMT_ATTR_RELAXED;
28161 else
28162 {
28163 attrs = cp_parser_txn_attribute_opt (parser);
28164 if (attrs)
28165 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
28166 }
28167
28168 /* Parse a noexcept specification. */
28169 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
28170
28171 /* Keep track if we're in the lexical scope of an outer transaction. */
28172 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
28173
28174 stmt = begin_transaction_stmt (token->location, NULL, this_in);
28175
28176 parser->in_transaction = new_in;
28177 cp_parser_compound_statement (parser, NULL, false, false);
28178 parser->in_transaction = old_in;
28179
28180 finish_transaction_stmt (stmt, NULL, this_in, noex);
28181
28182 return stmt;
28183 }
28184
28185 /* Parse a __transaction_atomic or __transaction_relaxed expression.
28186
28187 transaction-expression:
28188 __transaction_atomic txn-noexcept-spec[opt] ( expression )
28189 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
28190 */
28191
28192 static tree
28193 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
28194 {
28195 unsigned char old_in = parser->in_transaction;
28196 unsigned char this_in = 1;
28197 cp_token *token;
28198 tree expr, noex;
28199 bool noex_expr;
28200
28201 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28202 || keyword == RID_TRANSACTION_RELAXED);
28203
28204 if (!flag_tm)
28205 error (keyword == RID_TRANSACTION_RELAXED
28206 ? G_("%<__transaction_relaxed%> without transactional memory "
28207 "support enabled")
28208 : G_("%<__transaction_atomic%> without transactional memory "
28209 "support enabled"));
28210
28211 token = cp_parser_require_keyword (parser, keyword,
28212 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28213 : RT_TRANSACTION_RELAXED));
28214 gcc_assert (token != NULL);
28215
28216 if (keyword == RID_TRANSACTION_RELAXED)
28217 this_in |= TM_STMT_ATTR_RELAXED;
28218
28219 /* Set this early. This might mean that we allow transaction_cancel in
28220 an expression that we find out later actually has to be a constexpr.
28221 However, we expect that cxx_constant_value will be able to deal with
28222 this; also, if the noexcept has no constexpr, then what we parse next
28223 really is a transaction's body. */
28224 parser->in_transaction = this_in;
28225
28226 /* Parse a noexcept specification. */
28227 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
28228 true);
28229
28230 if (!noex || !noex_expr
28231 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28232 {
28233 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28234
28235 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28236 expr = finish_parenthesized_expr (expr);
28237
28238 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28239 }
28240 else
28241 {
28242 /* The only expression that is available got parsed for the noexcept
28243 already. noexcept is true then. */
28244 expr = noex;
28245 noex = boolean_true_node;
28246 }
28247
28248 expr = build_transaction_expr (token->location, expr, this_in, noex);
28249 parser->in_transaction = old_in;
28250
28251 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
28252 return error_mark_node;
28253
28254 return (flag_tm ? expr : error_mark_node);
28255 }
28256
28257 /* Parse a function-transaction-block.
28258
28259 function-transaction-block:
28260 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
28261 function-body
28262 __transaction_atomic txn-attribute[opt] function-try-block
28263 __transaction_relaxed ctor-initializer[opt] function-body
28264 __transaction_relaxed function-try-block
28265 */
28266
28267 static bool
28268 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
28269 {
28270 unsigned char old_in = parser->in_transaction;
28271 unsigned char new_in = 1;
28272 tree compound_stmt, stmt, attrs;
28273 bool ctor_initializer_p;
28274 cp_token *token;
28275
28276 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28277 || keyword == RID_TRANSACTION_RELAXED);
28278 token = cp_parser_require_keyword (parser, keyword,
28279 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28280 : RT_TRANSACTION_RELAXED));
28281 gcc_assert (token != NULL);
28282
28283 if (keyword == RID_TRANSACTION_RELAXED)
28284 new_in |= TM_STMT_ATTR_RELAXED;
28285 else
28286 {
28287 attrs = cp_parser_txn_attribute_opt (parser);
28288 if (attrs)
28289 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
28290 }
28291
28292 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
28293
28294 parser->in_transaction = new_in;
28295
28296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28297 ctor_initializer_p = cp_parser_function_try_block (parser);
28298 else
28299 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
28300 (parser, /*in_function_try_block=*/false);
28301
28302 parser->in_transaction = old_in;
28303
28304 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
28305
28306 return ctor_initializer_p;
28307 }
28308
28309 /* Parse a __transaction_cancel statement.
28310
28311 cancel-statement:
28312 __transaction_cancel txn-attribute[opt] ;
28313 __transaction_cancel txn-attribute[opt] throw-expression ;
28314
28315 ??? Cancel and throw is not yet implemented. */
28316
28317 static tree
28318 cp_parser_transaction_cancel (cp_parser *parser)
28319 {
28320 cp_token *token;
28321 bool is_outer = false;
28322 tree stmt, attrs;
28323
28324 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
28325 RT_TRANSACTION_CANCEL);
28326 gcc_assert (token != NULL);
28327
28328 attrs = cp_parser_txn_attribute_opt (parser);
28329 if (attrs)
28330 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
28331
28332 /* ??? Parse cancel-and-throw here. */
28333
28334 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28335
28336 if (!flag_tm)
28337 {
28338 error_at (token->location, "%<__transaction_cancel%> without "
28339 "transactional memory support enabled");
28340 return error_mark_node;
28341 }
28342 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
28343 {
28344 error_at (token->location, "%<__transaction_cancel%> within a "
28345 "%<__transaction_relaxed%>");
28346 return error_mark_node;
28347 }
28348 else if (is_outer)
28349 {
28350 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
28351 && !is_tm_may_cancel_outer (current_function_decl))
28352 {
28353 error_at (token->location, "outer %<__transaction_cancel%> not "
28354 "within outer %<__transaction_atomic%>");
28355 error_at (token->location,
28356 " or a %<transaction_may_cancel_outer%> function");
28357 return error_mark_node;
28358 }
28359 }
28360 else if (parser->in_transaction == 0)
28361 {
28362 error_at (token->location, "%<__transaction_cancel%> not within "
28363 "%<__transaction_atomic%>");
28364 return error_mark_node;
28365 }
28366
28367 stmt = build_tm_abort_call (token->location, is_outer);
28368 add_stmt (stmt);
28369 finish_stmt ();
28370
28371 return stmt;
28372 }
28373 \f
28374 /* The parser. */
28375
28376 static GTY (()) cp_parser *the_parser;
28377
28378 \f
28379 /* Special handling for the first token or line in the file. The first
28380 thing in the file might be #pragma GCC pch_preprocess, which loads a
28381 PCH file, which is a GC collection point. So we need to handle this
28382 first pragma without benefit of an existing lexer structure.
28383
28384 Always returns one token to the caller in *FIRST_TOKEN. This is
28385 either the true first token of the file, or the first token after
28386 the initial pragma. */
28387
28388 static void
28389 cp_parser_initial_pragma (cp_token *first_token)
28390 {
28391 tree name = NULL;
28392
28393 cp_lexer_get_preprocessor_token (NULL, first_token);
28394 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
28395 return;
28396
28397 cp_lexer_get_preprocessor_token (NULL, first_token);
28398 if (first_token->type == CPP_STRING)
28399 {
28400 name = first_token->u.value;
28401
28402 cp_lexer_get_preprocessor_token (NULL, first_token);
28403 if (first_token->type != CPP_PRAGMA_EOL)
28404 error_at (first_token->location,
28405 "junk at end of %<#pragma GCC pch_preprocess%>");
28406 }
28407 else
28408 error_at (first_token->location, "expected string literal");
28409
28410 /* Skip to the end of the pragma. */
28411 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
28412 cp_lexer_get_preprocessor_token (NULL, first_token);
28413
28414 /* Now actually load the PCH file. */
28415 if (name)
28416 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
28417
28418 /* Read one more token to return to our caller. We have to do this
28419 after reading the PCH file in, since its pointers have to be
28420 live. */
28421 cp_lexer_get_preprocessor_token (NULL, first_token);
28422 }
28423
28424 /* Normal parsing of a pragma token. Here we can (and must) use the
28425 regular lexer. */
28426
28427 static bool
28428 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
28429 {
28430 cp_token *pragma_tok;
28431 unsigned int id;
28432
28433 pragma_tok = cp_lexer_consume_token (parser->lexer);
28434 gcc_assert (pragma_tok->type == CPP_PRAGMA);
28435 parser->lexer->in_pragma = true;
28436
28437 id = pragma_tok->pragma_kind;
28438 switch (id)
28439 {
28440 case PRAGMA_GCC_PCH_PREPROCESS:
28441 error_at (pragma_tok->location,
28442 "%<#pragma GCC pch_preprocess%> must be first");
28443 break;
28444
28445 case PRAGMA_OMP_BARRIER:
28446 switch (context)
28447 {
28448 case pragma_compound:
28449 cp_parser_omp_barrier (parser, pragma_tok);
28450 return false;
28451 case pragma_stmt:
28452 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
28453 "used in compound statements");
28454 break;
28455 default:
28456 goto bad_stmt;
28457 }
28458 break;
28459
28460 case PRAGMA_OMP_FLUSH:
28461 switch (context)
28462 {
28463 case pragma_compound:
28464 cp_parser_omp_flush (parser, pragma_tok);
28465 return false;
28466 case pragma_stmt:
28467 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28468 "used in compound statements");
28469 break;
28470 default:
28471 goto bad_stmt;
28472 }
28473 break;
28474
28475 case PRAGMA_OMP_TASKWAIT:
28476 switch (context)
28477 {
28478 case pragma_compound:
28479 cp_parser_omp_taskwait (parser, pragma_tok);
28480 return false;
28481 case pragma_stmt:
28482 error_at (pragma_tok->location,
28483 "%<#pragma omp taskwait%> may only be "
28484 "used in compound statements");
28485 break;
28486 default:
28487 goto bad_stmt;
28488 }
28489 break;
28490
28491 case PRAGMA_OMP_TASKYIELD:
28492 switch (context)
28493 {
28494 case pragma_compound:
28495 cp_parser_omp_taskyield (parser, pragma_tok);
28496 return false;
28497 case pragma_stmt:
28498 error_at (pragma_tok->location,
28499 "%<#pragma omp taskyield%> may only be "
28500 "used in compound statements");
28501 break;
28502 default:
28503 goto bad_stmt;
28504 }
28505 break;
28506
28507 case PRAGMA_OMP_THREADPRIVATE:
28508 cp_parser_omp_threadprivate (parser, pragma_tok);
28509 return false;
28510
28511 case PRAGMA_OMP_ATOMIC:
28512 case PRAGMA_OMP_CRITICAL:
28513 case PRAGMA_OMP_FOR:
28514 case PRAGMA_OMP_MASTER:
28515 case PRAGMA_OMP_ORDERED:
28516 case PRAGMA_OMP_PARALLEL:
28517 case PRAGMA_OMP_SECTIONS:
28518 case PRAGMA_OMP_SINGLE:
28519 case PRAGMA_OMP_TASK:
28520 if (context == pragma_external)
28521 goto bad_stmt;
28522 cp_parser_omp_construct (parser, pragma_tok);
28523 return true;
28524
28525 case PRAGMA_OMP_SECTION:
28526 error_at (pragma_tok->location,
28527 "%<#pragma omp section%> may only be used in "
28528 "%<#pragma omp sections%> construct");
28529 break;
28530
28531 default:
28532 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28533 c_invoke_pragma_handler (id);
28534 break;
28535
28536 bad_stmt:
28537 cp_parser_error (parser, "expected declaration specifiers");
28538 break;
28539 }
28540
28541 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28542 return false;
28543 }
28544
28545 /* The interface the pragma parsers have to the lexer. */
28546
28547 enum cpp_ttype
28548 pragma_lex (tree *value)
28549 {
28550 cp_token *tok;
28551 enum cpp_ttype ret;
28552
28553 tok = cp_lexer_peek_token (the_parser->lexer);
28554
28555 ret = tok->type;
28556 *value = tok->u.value;
28557
28558 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28559 ret = CPP_EOF;
28560 else if (ret == CPP_STRING)
28561 *value = cp_parser_string_literal (the_parser, false, false);
28562 else
28563 {
28564 cp_lexer_consume_token (the_parser->lexer);
28565 if (ret == CPP_KEYWORD)
28566 ret = CPP_NAME;
28567 }
28568
28569 return ret;
28570 }
28571
28572 \f
28573 /* External interface. */
28574
28575 /* Parse one entire translation unit. */
28576
28577 void
28578 c_parse_file (void)
28579 {
28580 static bool already_called = false;
28581
28582 if (already_called)
28583 {
28584 sorry ("inter-module optimizations not implemented for C++");
28585 return;
28586 }
28587 already_called = true;
28588
28589 the_parser = cp_parser_new ();
28590 push_deferring_access_checks (flag_access_control
28591 ? dk_no_deferred : dk_no_check);
28592 cp_parser_translation_unit (the_parser);
28593 the_parser = NULL;
28594 }
28595
28596 #include "gt-cp-parser.h"