]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
Remove unnecessary VEC function overloads.
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "target.h"
36 #include "cgraph.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "plugin.h"
40 #include "tree-pretty-print.h"
41 #include "parser.h"
42
43 \f
44 /* The lexer. */
45
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
48
49 static cp_token eof_token =
50 {
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
52 };
53
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR,
109 /* a transaction expression */
110 NIC_TRANSACTION
111 } non_integral_constant;
112
113 /* The various kinds of errors about name-lookup failing. */
114 typedef enum name_lookup_error {
115 /* NULL */
116 NLE_NULL,
117 /* is not a type */
118 NLE_TYPE,
119 /* is not a class or namespace */
120 NLE_CXX98,
121 /* is not a class, namespace, or enumeration */
122 NLE_NOT_CXX98
123 } name_lookup_error;
124
125 /* The various kinds of required token */
126 typedef enum required_token {
127 RT_NONE,
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
134 RT_COMMA, /* ',' */
135 RT_SCOPE, /* '::' */
136 RT_LESS, /* '<' */
137 RT_GREATER, /* '>' */
138 RT_EQ, /* '=' */
139 RT_ELLIPSIS, /* '...' */
140 RT_MULT, /* '*' */
141 RT_COMPL, /* '~' */
142 RT_COLON, /* ':' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
148
149 /* The type is CPP_KEYWORD */
150 RT_NEW, /* new */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
162 RT_ASM, /* asm */
163 RT_TRY, /* try */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
170
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
179 } required_token;
180
181 /* Prototypes. */
182
183 static cp_lexer *cp_lexer_new_main
184 (void);
185 static cp_lexer *cp_lexer_new_from_tokens
186 (cp_token_cache *tokens);
187 static void cp_lexer_destroy
188 (cp_lexer *);
189 static int cp_lexer_saving_tokens
190 (const cp_lexer *);
191 static cp_token *cp_lexer_token_at
192 (cp_lexer *, cp_token_position);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer *, cp_token *);
195 static inline cp_token *cp_lexer_peek_token
196 (cp_lexer *);
197 static cp_token *cp_lexer_peek_nth_token
198 (cp_lexer *, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer *, enum cpp_ttype);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer *, enum cpp_ttype);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer *, enum rid);
205 static cp_token *cp_lexer_consume_token
206 (cp_lexer *);
207 static void cp_lexer_purge_token
208 (cp_lexer *);
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer *, cp_token_position);
211 static void cp_lexer_save_tokens
212 (cp_lexer *);
213 static void cp_lexer_commit_tokens
214 (cp_lexer *);
215 static void cp_lexer_rollback_tokens
216 (cp_lexer *);
217 static void cp_lexer_print_token
218 (FILE *, cp_token *);
219 static inline bool cp_lexer_debugging_p
220 (cp_lexer *);
221 static void cp_lexer_start_debugging
222 (cp_lexer *) ATTRIBUTE_UNUSED;
223 static void cp_lexer_stop_debugging
224 (cp_lexer *) ATTRIBUTE_UNUSED;
225
226 static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
228
229 static void cp_parser_initial_pragma
230 (cp_token *);
231
232 static tree cp_literal_operator_id
233 (const char *);
234
235 /* Manifest constants. */
236 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
237 #define CP_SAVED_TOKEN_STACK 5
238
239 /* Variables. */
240
241 /* The stream to which debugging output should be written. */
242 static FILE *cp_lexer_debug_stream;
243
244 /* Nonzero if we are parsing an unevaluated operand: an operand to
245 sizeof, typeof, or alignof. */
246 int cp_unevaluated_operand;
247
248 /* Dump up to NUM tokens in BUFFER to FILE starting with token
249 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
250 first token in BUFFER. If NUM is 0, dump all the tokens. If
251 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
252 highlighted by surrounding it in [[ ]]. */
253
254 static void
255 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
256 cp_token *start_token, unsigned num,
257 cp_token *curr_token)
258 {
259 unsigned i, nprinted;
260 cp_token *token;
261 bool do_print;
262
263 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264
265 if (buffer == NULL)
266 return;
267
268 if (num == 0)
269 num = VEC_length (cp_token, buffer);
270
271 if (start_token == NULL)
272 start_token = VEC_address (cp_token, buffer);
273
274 if (start_token > VEC_address (cp_token, buffer))
275 {
276 cp_lexer_print_token (file, &VEC_index (cp_token, buffer, 0));
277 fprintf (file, " ... ");
278 }
279
280 do_print = false;
281 nprinted = 0;
282 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
283 {
284 if (token == start_token)
285 do_print = true;
286
287 if (!do_print)
288 continue;
289
290 nprinted++;
291 if (token == curr_token)
292 fprintf (file, "[[");
293
294 cp_lexer_print_token (file, token);
295
296 if (token == curr_token)
297 fprintf (file, "]]");
298
299 switch (token->type)
300 {
301 case CPP_SEMICOLON:
302 case CPP_OPEN_BRACE:
303 case CPP_CLOSE_BRACE:
304 case CPP_EOF:
305 fputc ('\n', file);
306 break;
307
308 default:
309 fputc (' ', file);
310 }
311 }
312
313 if (i == num && i < VEC_length (cp_token, buffer))
314 {
315 fprintf (file, " ... ");
316 cp_lexer_print_token (file, &VEC_last (cp_token, buffer));
317 }
318
319 fprintf (file, "\n");
320 }
321
322
323 /* Dump all tokens in BUFFER to stderr. */
324
325 void
326 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
327 {
328 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
329 }
330
331
332 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
333 description for T. */
334
335 static void
336 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
337 {
338 if (t)
339 {
340 fprintf (file, "%s: ", desc);
341 print_node_brief (file, "", t, 0);
342 }
343 }
344
345
346 /* Dump parser context C to FILE. */
347
348 static void
349 cp_debug_print_context (FILE *file, cp_parser_context *c)
350 {
351 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
352 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
353 print_node_brief (file, "", c->object_type, 0);
354 fprintf (file, "}\n");
355 }
356
357
358 /* Print the stack of parsing contexts to FILE starting with FIRST. */
359
360 static void
361 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
362 {
363 unsigned i;
364 cp_parser_context *c;
365
366 fprintf (file, "Parsing context stack:\n");
367 for (i = 0, c = first; c; c = c->next, i++)
368 {
369 fprintf (file, "\t#%u: ", i);
370 cp_debug_print_context (file, c);
371 }
372 }
373
374
375 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
376
377 static void
378 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
379 {
380 if (flag)
381 fprintf (file, "%s: true\n", desc);
382 }
383
384
385 /* Print an unparsed function entry UF to FILE. */
386
387 static void
388 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
389 {
390 unsigned i;
391 cp_default_arg_entry *default_arg_fn;
392 tree fn;
393
394 fprintf (file, "\tFunctions with default args:\n");
395 for (i = 0;
396 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
397 default_arg_fn);
398 i++)
399 {
400 fprintf (file, "\t\tClass type: ");
401 print_node_brief (file, "", default_arg_fn->class_type, 0);
402 fprintf (file, "\t\tDeclaration: ");
403 print_node_brief (file, "", default_arg_fn->decl, 0);
404 fprintf (file, "\n");
405 }
406
407 fprintf (file, "\n\tFunctions with definitions that require "
408 "post-processing\n\t\t");
409 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
410 {
411 print_node_brief (file, "", fn, 0);
412 fprintf (file, " ");
413 }
414 fprintf (file, "\n");
415
416 fprintf (file, "\n\tNon-static data members with initializers that require "
417 "post-processing\n\t\t");
418 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
419 {
420 print_node_brief (file, "", fn, 0);
421 fprintf (file, " ");
422 }
423 fprintf (file, "\n");
424 }
425
426
427 /* Print the stack of unparsed member functions S to FILE. */
428
429 static void
430 cp_debug_print_unparsed_queues (FILE *file,
431 VEC(cp_unparsed_functions_entry, gc) *s)
432 {
433 unsigned i;
434 cp_unparsed_functions_entry *uf;
435
436 fprintf (file, "Unparsed functions\n");
437 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
438 {
439 fprintf (file, "#%u:\n", i);
440 cp_debug_print_unparsed_function (file, uf);
441 }
442 }
443
444
445 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
446 the given PARSER. If FILE is NULL, the output is printed on stderr. */
447
448 static void
449 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
450 {
451 cp_token *next_token, *first_token, *start_token;
452
453 if (file == NULL)
454 file = stderr;
455
456 next_token = parser->lexer->next_token;
457 first_token = VEC_address (cp_token, parser->lexer->buffer);
458 start_token = (next_token > first_token + window_size / 2)
459 ? next_token - window_size / 2
460 : first_token;
461 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
462 next_token);
463 }
464
465
466 /* Dump debugging information for the given PARSER. If FILE is NULL,
467 the output is printed on stderr. */
468
469 void
470 cp_debug_parser (FILE *file, cp_parser *parser)
471 {
472 const size_t window_size = 20;
473 cp_token *token;
474 expanded_location eloc;
475
476 if (file == NULL)
477 file = stderr;
478
479 fprintf (file, "Parser state\n\n");
480 fprintf (file, "Number of tokens: %u\n",
481 VEC_length (cp_token, parser->lexer->buffer));
482 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
483 cp_debug_print_tree_if_set (file, "Object scope",
484 parser->object_scope);
485 cp_debug_print_tree_if_set (file, "Qualifying scope",
486 parser->qualifying_scope);
487 cp_debug_print_context_stack (file, parser->context);
488 cp_debug_print_flag (file, "Allow GNU extensions",
489 parser->allow_gnu_extensions_p);
490 cp_debug_print_flag (file, "'>' token is greater-than",
491 parser->greater_than_is_operator_p);
492 cp_debug_print_flag (file, "Default args allowed in current "
493 "parameter list", parser->default_arg_ok_p);
494 cp_debug_print_flag (file, "Parsing integral constant-expression",
495 parser->integral_constant_expression_p);
496 cp_debug_print_flag (file, "Allow non-constant expression in current "
497 "constant-expression",
498 parser->allow_non_integral_constant_expression_p);
499 cp_debug_print_flag (file, "Seen non-constant expression",
500 parser->non_integral_constant_expression_p);
501 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
502 "current context",
503 parser->local_variables_forbidden_p);
504 cp_debug_print_flag (file, "In unbraced linkage specification",
505 parser->in_unbraced_linkage_specification_p);
506 cp_debug_print_flag (file, "Parsing a declarator",
507 parser->in_declarator_p);
508 cp_debug_print_flag (file, "In template argument list",
509 parser->in_template_argument_list_p);
510 cp_debug_print_flag (file, "Parsing an iteration statement",
511 parser->in_statement & IN_ITERATION_STMT);
512 cp_debug_print_flag (file, "Parsing a switch statement",
513 parser->in_statement & IN_SWITCH_STMT);
514 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
515 parser->in_statement & IN_OMP_BLOCK);
516 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
517 parser->in_statement & IN_OMP_FOR);
518 cp_debug_print_flag (file, "Parsing an if statement",
519 parser->in_statement & IN_IF_STMT);
520 cp_debug_print_flag (file, "Parsing a type-id in an expression "
521 "context", parser->in_type_id_in_expr_p);
522 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
523 parser->implicit_extern_c);
524 cp_debug_print_flag (file, "String expressions should be translated "
525 "to execution character set",
526 parser->translate_strings_p);
527 cp_debug_print_flag (file, "Parsing function body outside of a "
528 "local class", parser->in_function_body);
529 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
530 parser->colon_corrects_to_scope_p);
531 if (parser->type_definition_forbidden_message)
532 fprintf (file, "Error message for forbidden type definitions: %s\n",
533 parser->type_definition_forbidden_message);
534 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
535 fprintf (file, "Number of class definitions in progress: %u\n",
536 parser->num_classes_being_defined);
537 fprintf (file, "Number of template parameter lists for the current "
538 "declaration: %u\n", parser->num_template_parameter_lists);
539 cp_debug_parser_tokens (file, parser, window_size);
540 token = parser->lexer->next_token;
541 fprintf (file, "Next token to parse:\n");
542 fprintf (file, "\tToken: ");
543 cp_lexer_print_token (file, token);
544 eloc = expand_location (token->location);
545 fprintf (file, "\n\tFile: %s\n", eloc.file);
546 fprintf (file, "\tLine: %d\n", eloc.line);
547 fprintf (file, "\tColumn: %d\n", eloc.column);
548 }
549
550
551 /* Allocate memory for a new lexer object and return it. */
552
553 static cp_lexer *
554 cp_lexer_alloc (void)
555 {
556 cp_lexer *lexer;
557
558 c_common_no_more_pch ();
559
560 /* Allocate the memory. */
561 lexer = ggc_alloc_cleared_cp_lexer ();
562
563 /* Initially we are not debugging. */
564 lexer->debugging_p = false;
565
566 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
567 CP_SAVED_TOKEN_STACK);
568
569 /* Create the buffer. */
570 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
571
572 return lexer;
573 }
574
575
576 /* Create a new main C++ lexer, the lexer that gets tokens from the
577 preprocessor. */
578
579 static cp_lexer *
580 cp_lexer_new_main (void)
581 {
582 cp_lexer *lexer;
583 cp_token token;
584
585 /* It's possible that parsing the first pragma will load a PCH file,
586 which is a GC collection point. So we have to do that before
587 allocating any memory. */
588 cp_parser_initial_pragma (&token);
589
590 lexer = cp_lexer_alloc ();
591
592 /* Put the first token in the buffer. */
593 VEC_quick_push (cp_token, lexer->buffer, token);
594
595 /* Get the remaining tokens from the preprocessor. */
596 while (token.type != CPP_EOF)
597 {
598 cp_lexer_get_preprocessor_token (lexer, &token);
599 VEC_safe_push (cp_token, gc, lexer->buffer, token);
600 }
601
602 lexer->last_token = VEC_address (cp_token, lexer->buffer)
603 + VEC_length (cp_token, lexer->buffer)
604 - 1;
605 lexer->next_token = VEC_length (cp_token, lexer->buffer)
606 ? VEC_address (cp_token, lexer->buffer)
607 : &eof_token;
608
609 /* Subsequent preprocessor diagnostics should use compiler
610 diagnostic functions to get the compiler source location. */
611 done_lexing = true;
612
613 gcc_assert (!lexer->next_token->purged_p);
614 return lexer;
615 }
616
617 /* Create a new lexer whose token stream is primed with the tokens in
618 CACHE. When these tokens are exhausted, no new tokens will be read. */
619
620 static cp_lexer *
621 cp_lexer_new_from_tokens (cp_token_cache *cache)
622 {
623 cp_token *first = cache->first;
624 cp_token *last = cache->last;
625 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
626
627 /* We do not own the buffer. */
628 lexer->buffer = NULL;
629 lexer->next_token = first == last ? &eof_token : first;
630 lexer->last_token = last;
631
632 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
633 CP_SAVED_TOKEN_STACK);
634
635 /* Initially we are not debugging. */
636 lexer->debugging_p = false;
637
638 gcc_assert (!lexer->next_token->purged_p);
639 return lexer;
640 }
641
642 /* Frees all resources associated with LEXER. */
643
644 static void
645 cp_lexer_destroy (cp_lexer *lexer)
646 {
647 VEC_free (cp_token, gc, lexer->buffer);
648 VEC_free (cp_token_position, heap, lexer->saved_tokens);
649 ggc_free (lexer);
650 }
651
652 /* Returns nonzero if debugging information should be output. */
653
654 static inline bool
655 cp_lexer_debugging_p (cp_lexer *lexer)
656 {
657 return lexer->debugging_p;
658 }
659
660
661 static inline cp_token_position
662 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
663 {
664 gcc_assert (!previous_p || lexer->next_token != &eof_token);
665
666 return lexer->next_token - previous_p;
667 }
668
669 static inline cp_token *
670 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
671 {
672 return pos;
673 }
674
675 static inline void
676 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
677 {
678 lexer->next_token = cp_lexer_token_at (lexer, pos);
679 }
680
681 static inline cp_token_position
682 cp_lexer_previous_token_position (cp_lexer *lexer)
683 {
684 if (lexer->next_token == &eof_token)
685 return lexer->last_token - 1;
686 else
687 return cp_lexer_token_position (lexer, true);
688 }
689
690 static inline cp_token *
691 cp_lexer_previous_token (cp_lexer *lexer)
692 {
693 cp_token_position tp = cp_lexer_previous_token_position (lexer);
694
695 return cp_lexer_token_at (lexer, tp);
696 }
697
698 /* nonzero if we are presently saving tokens. */
699
700 static inline int
701 cp_lexer_saving_tokens (const cp_lexer* lexer)
702 {
703 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
704 }
705
706 /* Store the next token from the preprocessor in *TOKEN. Return true
707 if we reach EOF. If LEXER is NULL, assume we are handling an
708 initial #pragma pch_preprocess, and thus want the lexer to return
709 processed strings. */
710
711 static void
712 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
713 {
714 static int is_extern_c = 0;
715
716 /* Get a new token from the preprocessor. */
717 token->type
718 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
719 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
720 token->keyword = RID_MAX;
721 token->pragma_kind = PRAGMA_NONE;
722 token->purged_p = false;
723
724 /* On some systems, some header files are surrounded by an
725 implicit extern "C" block. Set a flag in the token if it
726 comes from such a header. */
727 is_extern_c += pending_lang_change;
728 pending_lang_change = 0;
729 token->implicit_extern_c = is_extern_c > 0;
730
731 /* Check to see if this token is a keyword. */
732 if (token->type == CPP_NAME)
733 {
734 if (C_IS_RESERVED_WORD (token->u.value))
735 {
736 /* Mark this token as a keyword. */
737 token->type = CPP_KEYWORD;
738 /* Record which keyword. */
739 token->keyword = C_RID_CODE (token->u.value);
740 }
741 else
742 {
743 if (warn_cxx0x_compat
744 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
745 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
746 {
747 /* Warn about the C++0x keyword (but still treat it as
748 an identifier). */
749 warning (OPT_Wc__0x_compat,
750 "identifier %qE is a keyword in C++11",
751 token->u.value);
752
753 /* Clear out the C_RID_CODE so we don't warn about this
754 particular identifier-turned-keyword again. */
755 C_SET_RID_CODE (token->u.value, RID_MAX);
756 }
757
758 token->ambiguous_p = false;
759 token->keyword = RID_MAX;
760 }
761 }
762 else if (token->type == CPP_AT_NAME)
763 {
764 /* This only happens in Objective-C++; it must be a keyword. */
765 token->type = CPP_KEYWORD;
766 switch (C_RID_CODE (token->u.value))
767 {
768 /* Replace 'class' with '@class', 'private' with '@private',
769 etc. This prevents confusion with the C++ keyword
770 'class', and makes the tokens consistent with other
771 Objective-C 'AT' keywords. For example '@class' is
772 reported as RID_AT_CLASS which is consistent with
773 '@synchronized', which is reported as
774 RID_AT_SYNCHRONIZED.
775 */
776 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
777 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
778 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
779 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
780 case RID_THROW: token->keyword = RID_AT_THROW; break;
781 case RID_TRY: token->keyword = RID_AT_TRY; break;
782 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
783 default: token->keyword = C_RID_CODE (token->u.value);
784 }
785 }
786 else if (token->type == CPP_PRAGMA)
787 {
788 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
789 token->pragma_kind = ((enum pragma_kind)
790 TREE_INT_CST_LOW (token->u.value));
791 token->u.value = NULL_TREE;
792 }
793 }
794
795 /* Update the globals input_location and the input file stack from TOKEN. */
796 static inline void
797 cp_lexer_set_source_position_from_token (cp_token *token)
798 {
799 if (token->type != CPP_EOF)
800 {
801 input_location = token->location;
802 }
803 }
804
805 /* Return a pointer to the next token in the token stream, but do not
806 consume it. */
807
808 static inline cp_token *
809 cp_lexer_peek_token (cp_lexer *lexer)
810 {
811 if (cp_lexer_debugging_p (lexer))
812 {
813 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
814 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
815 putc ('\n', cp_lexer_debug_stream);
816 }
817 return lexer->next_token;
818 }
819
820 /* Return true if the next token has the indicated TYPE. */
821
822 static inline bool
823 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
824 {
825 return cp_lexer_peek_token (lexer)->type == type;
826 }
827
828 /* Return true if the next token does not have the indicated TYPE. */
829
830 static inline bool
831 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
832 {
833 return !cp_lexer_next_token_is (lexer, type);
834 }
835
836 /* Return true if the next token is the indicated KEYWORD. */
837
838 static inline bool
839 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
840 {
841 return cp_lexer_peek_token (lexer)->keyword == keyword;
842 }
843
844 /* Return true if the next token is not the indicated KEYWORD. */
845
846 static inline bool
847 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
848 {
849 return cp_lexer_peek_token (lexer)->keyword != keyword;
850 }
851
852 /* Return true if the next token is a keyword for a decl-specifier. */
853
854 static bool
855 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
856 {
857 cp_token *token;
858
859 token = cp_lexer_peek_token (lexer);
860 switch (token->keyword)
861 {
862 /* auto specifier: storage-class-specifier in C++,
863 simple-type-specifier in C++0x. */
864 case RID_AUTO:
865 /* Storage classes. */
866 case RID_REGISTER:
867 case RID_STATIC:
868 case RID_EXTERN:
869 case RID_MUTABLE:
870 case RID_THREAD:
871 /* Elaborated type specifiers. */
872 case RID_ENUM:
873 case RID_CLASS:
874 case RID_STRUCT:
875 case RID_UNION:
876 case RID_TYPENAME:
877 /* Simple type specifiers. */
878 case RID_CHAR:
879 case RID_CHAR16:
880 case RID_CHAR32:
881 case RID_WCHAR:
882 case RID_BOOL:
883 case RID_SHORT:
884 case RID_INT:
885 case RID_LONG:
886 case RID_INT128:
887 case RID_SIGNED:
888 case RID_UNSIGNED:
889 case RID_FLOAT:
890 case RID_DOUBLE:
891 case RID_VOID:
892 /* GNU extensions. */
893 case RID_ATTRIBUTE:
894 case RID_TYPEOF:
895 /* C++0x extensions. */
896 case RID_DECLTYPE:
897 case RID_UNDERLYING_TYPE:
898 return true;
899
900 default:
901 return false;
902 }
903 }
904
905 /* Returns TRUE iff the token T begins a decltype type. */
906
907 static bool
908 token_is_decltype (cp_token *t)
909 {
910 return (t->keyword == RID_DECLTYPE
911 || t->type == CPP_DECLTYPE);
912 }
913
914 /* Returns TRUE iff the next token begins a decltype type. */
915
916 static bool
917 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
918 {
919 cp_token *t = cp_lexer_peek_token (lexer);
920 return token_is_decltype (t);
921 }
922
923 /* Return a pointer to the Nth token in the token stream. If N is 1,
924 then this is precisely equivalent to cp_lexer_peek_token (except
925 that it is not inline). One would like to disallow that case, but
926 there is one case (cp_parser_nth_token_starts_template_id) where
927 the caller passes a variable for N and it might be 1. */
928
929 static cp_token *
930 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
931 {
932 cp_token *token;
933
934 /* N is 1-based, not zero-based. */
935 gcc_assert (n > 0);
936
937 if (cp_lexer_debugging_p (lexer))
938 fprintf (cp_lexer_debug_stream,
939 "cp_lexer: peeking ahead %ld at token: ", (long)n);
940
941 --n;
942 token = lexer->next_token;
943 gcc_assert (!n || token != &eof_token);
944 while (n != 0)
945 {
946 ++token;
947 if (token == lexer->last_token)
948 {
949 token = &eof_token;
950 break;
951 }
952
953 if (!token->purged_p)
954 --n;
955 }
956
957 if (cp_lexer_debugging_p (lexer))
958 {
959 cp_lexer_print_token (cp_lexer_debug_stream, token);
960 putc ('\n', cp_lexer_debug_stream);
961 }
962
963 return token;
964 }
965
966 /* Return the next token, and advance the lexer's next_token pointer
967 to point to the next non-purged token. */
968
969 static cp_token *
970 cp_lexer_consume_token (cp_lexer* lexer)
971 {
972 cp_token *token = lexer->next_token;
973
974 gcc_assert (token != &eof_token);
975 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
976
977 do
978 {
979 lexer->next_token++;
980 if (lexer->next_token == lexer->last_token)
981 {
982 lexer->next_token = &eof_token;
983 break;
984 }
985
986 }
987 while (lexer->next_token->purged_p);
988
989 cp_lexer_set_source_position_from_token (token);
990
991 /* Provide debugging output. */
992 if (cp_lexer_debugging_p (lexer))
993 {
994 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
995 cp_lexer_print_token (cp_lexer_debug_stream, token);
996 putc ('\n', cp_lexer_debug_stream);
997 }
998
999 return token;
1000 }
1001
1002 /* Permanently remove the next token from the token stream, and
1003 advance the next_token pointer to refer to the next non-purged
1004 token. */
1005
1006 static void
1007 cp_lexer_purge_token (cp_lexer *lexer)
1008 {
1009 cp_token *tok = lexer->next_token;
1010
1011 gcc_assert (tok != &eof_token);
1012 tok->purged_p = true;
1013 tok->location = UNKNOWN_LOCATION;
1014 tok->u.value = NULL_TREE;
1015 tok->keyword = RID_MAX;
1016
1017 do
1018 {
1019 tok++;
1020 if (tok == lexer->last_token)
1021 {
1022 tok = &eof_token;
1023 break;
1024 }
1025 }
1026 while (tok->purged_p);
1027 lexer->next_token = tok;
1028 }
1029
1030 /* Permanently remove all tokens after TOK, up to, but not
1031 including, the token that will be returned next by
1032 cp_lexer_peek_token. */
1033
1034 static void
1035 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1036 {
1037 cp_token *peek = lexer->next_token;
1038
1039 if (peek == &eof_token)
1040 peek = lexer->last_token;
1041
1042 gcc_assert (tok < peek);
1043
1044 for ( tok += 1; tok != peek; tok += 1)
1045 {
1046 tok->purged_p = true;
1047 tok->location = UNKNOWN_LOCATION;
1048 tok->u.value = NULL_TREE;
1049 tok->keyword = RID_MAX;
1050 }
1051 }
1052
1053 /* Begin saving tokens. All tokens consumed after this point will be
1054 preserved. */
1055
1056 static void
1057 cp_lexer_save_tokens (cp_lexer* lexer)
1058 {
1059 /* Provide debugging output. */
1060 if (cp_lexer_debugging_p (lexer))
1061 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1062
1063 VEC_safe_push (cp_token_position, heap,
1064 lexer->saved_tokens, lexer->next_token);
1065 }
1066
1067 /* Commit to the portion of the token stream most recently saved. */
1068
1069 static void
1070 cp_lexer_commit_tokens (cp_lexer* lexer)
1071 {
1072 /* Provide debugging output. */
1073 if (cp_lexer_debugging_p (lexer))
1074 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1075
1076 VEC_pop (cp_token_position, lexer->saved_tokens);
1077 }
1078
1079 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1080 to the token stream. Stop saving tokens. */
1081
1082 static void
1083 cp_lexer_rollback_tokens (cp_lexer* lexer)
1084 {
1085 /* Provide debugging output. */
1086 if (cp_lexer_debugging_p (lexer))
1087 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1088
1089 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1090 }
1091
1092 /* Print a representation of the TOKEN on the STREAM. */
1093
1094 static void
1095 cp_lexer_print_token (FILE * stream, cp_token *token)
1096 {
1097 /* We don't use cpp_type2name here because the parser defines
1098 a few tokens of its own. */
1099 static const char *const token_names[] = {
1100 /* cpplib-defined token types */
1101 #define OP(e, s) #e,
1102 #define TK(e, s) #e,
1103 TTYPE_TABLE
1104 #undef OP
1105 #undef TK
1106 /* C++ parser token types - see "Manifest constants", above. */
1107 "KEYWORD",
1108 "TEMPLATE_ID",
1109 "NESTED_NAME_SPECIFIER",
1110 };
1111
1112 /* For some tokens, print the associated data. */
1113 switch (token->type)
1114 {
1115 case CPP_KEYWORD:
1116 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1117 For example, `struct' is mapped to an INTEGER_CST. */
1118 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1119 break;
1120 /* else fall through */
1121 case CPP_NAME:
1122 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1123 break;
1124
1125 case CPP_STRING:
1126 case CPP_STRING16:
1127 case CPP_STRING32:
1128 case CPP_WSTRING:
1129 case CPP_UTF8STRING:
1130 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1131 break;
1132
1133 case CPP_NUMBER:
1134 print_generic_expr (stream, token->u.value, 0);
1135 break;
1136
1137 default:
1138 /* If we have a name for the token, print it out. Otherwise, we
1139 simply give the numeric code. */
1140 if (token->type < ARRAY_SIZE(token_names))
1141 fputs (token_names[token->type], stream);
1142 else
1143 fprintf (stream, "[%d]", token->type);
1144 break;
1145 }
1146 }
1147
1148 /* Start emitting debugging information. */
1149
1150 static void
1151 cp_lexer_start_debugging (cp_lexer* lexer)
1152 {
1153 lexer->debugging_p = true;
1154 cp_lexer_debug_stream = stderr;
1155 }
1156
1157 /* Stop emitting debugging information. */
1158
1159 static void
1160 cp_lexer_stop_debugging (cp_lexer* lexer)
1161 {
1162 lexer->debugging_p = false;
1163 cp_lexer_debug_stream = NULL;
1164 }
1165
1166 /* Create a new cp_token_cache, representing a range of tokens. */
1167
1168 static cp_token_cache *
1169 cp_token_cache_new (cp_token *first, cp_token *last)
1170 {
1171 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1172 cache->first = first;
1173 cache->last = last;
1174 return cache;
1175 }
1176
1177 \f
1178 /* Decl-specifiers. */
1179
1180 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1181
1182 static void
1183 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1184 {
1185 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1186 }
1187
1188 /* Declarators. */
1189
1190 /* Nothing other than the parser should be creating declarators;
1191 declarators are a semi-syntactic representation of C++ entities.
1192 Other parts of the front end that need to create entities (like
1193 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1194
1195 static cp_declarator *make_call_declarator
1196 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1197 static cp_declarator *make_array_declarator
1198 (cp_declarator *, tree);
1199 static cp_declarator *make_pointer_declarator
1200 (cp_cv_quals, cp_declarator *);
1201 static cp_declarator *make_reference_declarator
1202 (cp_cv_quals, cp_declarator *, bool);
1203 static cp_parameter_declarator *make_parameter_declarator
1204 (cp_decl_specifier_seq *, cp_declarator *, tree);
1205 static cp_declarator *make_ptrmem_declarator
1206 (cp_cv_quals, tree, cp_declarator *);
1207
1208 /* An erroneous declarator. */
1209 static cp_declarator *cp_error_declarator;
1210
1211 /* The obstack on which declarators and related data structures are
1212 allocated. */
1213 static struct obstack declarator_obstack;
1214
1215 /* Alloc BYTES from the declarator memory pool. */
1216
1217 static inline void *
1218 alloc_declarator (size_t bytes)
1219 {
1220 return obstack_alloc (&declarator_obstack, bytes);
1221 }
1222
1223 /* Allocate a declarator of the indicated KIND. Clear fields that are
1224 common to all declarators. */
1225
1226 static cp_declarator *
1227 make_declarator (cp_declarator_kind kind)
1228 {
1229 cp_declarator *declarator;
1230
1231 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1232 declarator->kind = kind;
1233 declarator->attributes = NULL_TREE;
1234 declarator->declarator = NULL;
1235 declarator->parameter_pack_p = false;
1236 declarator->id_loc = UNKNOWN_LOCATION;
1237
1238 return declarator;
1239 }
1240
1241 /* Make a declarator for a generalized identifier. If
1242 QUALIFYING_SCOPE is non-NULL, the identifier is
1243 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1244 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1245 is, if any. */
1246
1247 static cp_declarator *
1248 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1249 special_function_kind sfk)
1250 {
1251 cp_declarator *declarator;
1252
1253 /* It is valid to write:
1254
1255 class C { void f(); };
1256 typedef C D;
1257 void D::f();
1258
1259 The standard is not clear about whether `typedef const C D' is
1260 legal; as of 2002-09-15 the committee is considering that
1261 question. EDG 3.0 allows that syntax. Therefore, we do as
1262 well. */
1263 if (qualifying_scope && TYPE_P (qualifying_scope))
1264 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1265
1266 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1267 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1268 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1269
1270 declarator = make_declarator (cdk_id);
1271 declarator->u.id.qualifying_scope = qualifying_scope;
1272 declarator->u.id.unqualified_name = unqualified_name;
1273 declarator->u.id.sfk = sfk;
1274
1275 return declarator;
1276 }
1277
1278 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1279 of modifiers such as const or volatile to apply to the pointer
1280 type, represented as identifiers. */
1281
1282 cp_declarator *
1283 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1284 {
1285 cp_declarator *declarator;
1286
1287 declarator = make_declarator (cdk_pointer);
1288 declarator->declarator = target;
1289 declarator->u.pointer.qualifiers = cv_qualifiers;
1290 declarator->u.pointer.class_type = NULL_TREE;
1291 if (target)
1292 {
1293 declarator->id_loc = target->id_loc;
1294 declarator->parameter_pack_p = target->parameter_pack_p;
1295 target->parameter_pack_p = false;
1296 }
1297 else
1298 declarator->parameter_pack_p = false;
1299
1300 return declarator;
1301 }
1302
1303 /* Like make_pointer_declarator -- but for references. */
1304
1305 cp_declarator *
1306 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1307 bool rvalue_ref)
1308 {
1309 cp_declarator *declarator;
1310
1311 declarator = make_declarator (cdk_reference);
1312 declarator->declarator = target;
1313 declarator->u.reference.qualifiers = cv_qualifiers;
1314 declarator->u.reference.rvalue_ref = rvalue_ref;
1315 if (target)
1316 {
1317 declarator->id_loc = target->id_loc;
1318 declarator->parameter_pack_p = target->parameter_pack_p;
1319 target->parameter_pack_p = false;
1320 }
1321 else
1322 declarator->parameter_pack_p = false;
1323
1324 return declarator;
1325 }
1326
1327 /* Like make_pointer_declarator -- but for a pointer to a non-static
1328 member of CLASS_TYPE. */
1329
1330 cp_declarator *
1331 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1332 cp_declarator *pointee)
1333 {
1334 cp_declarator *declarator;
1335
1336 declarator = make_declarator (cdk_ptrmem);
1337 declarator->declarator = pointee;
1338 declarator->u.pointer.qualifiers = cv_qualifiers;
1339 declarator->u.pointer.class_type = class_type;
1340
1341 if (pointee)
1342 {
1343 declarator->parameter_pack_p = pointee->parameter_pack_p;
1344 pointee->parameter_pack_p = false;
1345 }
1346 else
1347 declarator->parameter_pack_p = false;
1348
1349 return declarator;
1350 }
1351
1352 /* Make a declarator for the function given by TARGET, with the
1353 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1354 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1355 indicates what exceptions can be thrown. */
1356
1357 cp_declarator *
1358 make_call_declarator (cp_declarator *target,
1359 tree parms,
1360 cp_cv_quals cv_qualifiers,
1361 cp_virt_specifiers virt_specifiers,
1362 tree exception_specification,
1363 tree late_return_type)
1364 {
1365 cp_declarator *declarator;
1366
1367 declarator = make_declarator (cdk_function);
1368 declarator->declarator = target;
1369 declarator->u.function.parameters = parms;
1370 declarator->u.function.qualifiers = cv_qualifiers;
1371 declarator->u.function.virt_specifiers = virt_specifiers;
1372 declarator->u.function.exception_specification = exception_specification;
1373 declarator->u.function.late_return_type = late_return_type;
1374 if (target)
1375 {
1376 declarator->id_loc = target->id_loc;
1377 declarator->parameter_pack_p = target->parameter_pack_p;
1378 target->parameter_pack_p = false;
1379 }
1380 else
1381 declarator->parameter_pack_p = false;
1382
1383 return declarator;
1384 }
1385
1386 /* Make a declarator for an array of BOUNDS elements, each of which is
1387 defined by ELEMENT. */
1388
1389 cp_declarator *
1390 make_array_declarator (cp_declarator *element, tree bounds)
1391 {
1392 cp_declarator *declarator;
1393
1394 declarator = make_declarator (cdk_array);
1395 declarator->declarator = element;
1396 declarator->u.array.bounds = bounds;
1397 if (element)
1398 {
1399 declarator->id_loc = element->id_loc;
1400 declarator->parameter_pack_p = element->parameter_pack_p;
1401 element->parameter_pack_p = false;
1402 }
1403 else
1404 declarator->parameter_pack_p = false;
1405
1406 return declarator;
1407 }
1408
1409 /* Determine whether the declarator we've seen so far can be a
1410 parameter pack, when followed by an ellipsis. */
1411 static bool
1412 declarator_can_be_parameter_pack (cp_declarator *declarator)
1413 {
1414 /* Search for a declarator name, or any other declarator that goes
1415 after the point where the ellipsis could appear in a parameter
1416 pack. If we find any of these, then this declarator can not be
1417 made into a parameter pack. */
1418 bool found = false;
1419 while (declarator && !found)
1420 {
1421 switch ((int)declarator->kind)
1422 {
1423 case cdk_id:
1424 case cdk_array:
1425 found = true;
1426 break;
1427
1428 case cdk_error:
1429 return true;
1430
1431 default:
1432 declarator = declarator->declarator;
1433 break;
1434 }
1435 }
1436
1437 return !found;
1438 }
1439
1440 cp_parameter_declarator *no_parameters;
1441
1442 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1443 DECLARATOR and DEFAULT_ARGUMENT. */
1444
1445 cp_parameter_declarator *
1446 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1447 cp_declarator *declarator,
1448 tree default_argument)
1449 {
1450 cp_parameter_declarator *parameter;
1451
1452 parameter = ((cp_parameter_declarator *)
1453 alloc_declarator (sizeof (cp_parameter_declarator)));
1454 parameter->next = NULL;
1455 if (decl_specifiers)
1456 parameter->decl_specifiers = *decl_specifiers;
1457 else
1458 clear_decl_specs (&parameter->decl_specifiers);
1459 parameter->declarator = declarator;
1460 parameter->default_argument = default_argument;
1461 parameter->ellipsis_p = false;
1462
1463 return parameter;
1464 }
1465
1466 /* Returns true iff DECLARATOR is a declaration for a function. */
1467
1468 static bool
1469 function_declarator_p (const cp_declarator *declarator)
1470 {
1471 while (declarator)
1472 {
1473 if (declarator->kind == cdk_function
1474 && declarator->declarator->kind == cdk_id)
1475 return true;
1476 if (declarator->kind == cdk_id
1477 || declarator->kind == cdk_error)
1478 return false;
1479 declarator = declarator->declarator;
1480 }
1481 return false;
1482 }
1483
1484 /* The parser. */
1485
1486 /* Overview
1487 --------
1488
1489 A cp_parser parses the token stream as specified by the C++
1490 grammar. Its job is purely parsing, not semantic analysis. For
1491 example, the parser breaks the token stream into declarators,
1492 expressions, statements, and other similar syntactic constructs.
1493 It does not check that the types of the expressions on either side
1494 of an assignment-statement are compatible, or that a function is
1495 not declared with a parameter of type `void'.
1496
1497 The parser invokes routines elsewhere in the compiler to perform
1498 semantic analysis and to build up the abstract syntax tree for the
1499 code processed.
1500
1501 The parser (and the template instantiation code, which is, in a
1502 way, a close relative of parsing) are the only parts of the
1503 compiler that should be calling push_scope and pop_scope, or
1504 related functions. The parser (and template instantiation code)
1505 keeps track of what scope is presently active; everything else
1506 should simply honor that. (The code that generates static
1507 initializers may also need to set the scope, in order to check
1508 access control correctly when emitting the initializers.)
1509
1510 Methodology
1511 -----------
1512
1513 The parser is of the standard recursive-descent variety. Upcoming
1514 tokens in the token stream are examined in order to determine which
1515 production to use when parsing a non-terminal. Some C++ constructs
1516 require arbitrary look ahead to disambiguate. For example, it is
1517 impossible, in the general case, to tell whether a statement is an
1518 expression or declaration without scanning the entire statement.
1519 Therefore, the parser is capable of "parsing tentatively." When the
1520 parser is not sure what construct comes next, it enters this mode.
1521 Then, while we attempt to parse the construct, the parser queues up
1522 error messages, rather than issuing them immediately, and saves the
1523 tokens it consumes. If the construct is parsed successfully, the
1524 parser "commits", i.e., it issues any queued error messages and
1525 the tokens that were being preserved are permanently discarded.
1526 If, however, the construct is not parsed successfully, the parser
1527 rolls back its state completely so that it can resume parsing using
1528 a different alternative.
1529
1530 Future Improvements
1531 -------------------
1532
1533 The performance of the parser could probably be improved substantially.
1534 We could often eliminate the need to parse tentatively by looking ahead
1535 a little bit. In some places, this approach might not entirely eliminate
1536 the need to parse tentatively, but it might still speed up the average
1537 case. */
1538
1539 /* Flags that are passed to some parsing functions. These values can
1540 be bitwise-ored together. */
1541
1542 enum
1543 {
1544 /* No flags. */
1545 CP_PARSER_FLAGS_NONE = 0x0,
1546 /* The construct is optional. If it is not present, then no error
1547 should be issued. */
1548 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1549 /* When parsing a type-specifier, treat user-defined type-names
1550 as non-type identifiers. */
1551 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1552 /* When parsing a type-specifier, do not try to parse a class-specifier
1553 or enum-specifier. */
1554 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1555 /* When parsing a decl-specifier-seq, only allow type-specifier or
1556 constexpr. */
1557 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1558 };
1559
1560 /* This type is used for parameters and variables which hold
1561 combinations of the above flags. */
1562 typedef int cp_parser_flags;
1563
1564 /* The different kinds of declarators we want to parse. */
1565
1566 typedef enum cp_parser_declarator_kind
1567 {
1568 /* We want an abstract declarator. */
1569 CP_PARSER_DECLARATOR_ABSTRACT,
1570 /* We want a named declarator. */
1571 CP_PARSER_DECLARATOR_NAMED,
1572 /* We don't mind, but the name must be an unqualified-id. */
1573 CP_PARSER_DECLARATOR_EITHER
1574 } cp_parser_declarator_kind;
1575
1576 /* The precedence values used to parse binary expressions. The minimum value
1577 of PREC must be 1, because zero is reserved to quickly discriminate
1578 binary operators from other tokens. */
1579
1580 enum cp_parser_prec
1581 {
1582 PREC_NOT_OPERATOR,
1583 PREC_LOGICAL_OR_EXPRESSION,
1584 PREC_LOGICAL_AND_EXPRESSION,
1585 PREC_INCLUSIVE_OR_EXPRESSION,
1586 PREC_EXCLUSIVE_OR_EXPRESSION,
1587 PREC_AND_EXPRESSION,
1588 PREC_EQUALITY_EXPRESSION,
1589 PREC_RELATIONAL_EXPRESSION,
1590 PREC_SHIFT_EXPRESSION,
1591 PREC_ADDITIVE_EXPRESSION,
1592 PREC_MULTIPLICATIVE_EXPRESSION,
1593 PREC_PM_EXPRESSION,
1594 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1595 };
1596
1597 /* A mapping from a token type to a corresponding tree node type, with a
1598 precedence value. */
1599
1600 typedef struct cp_parser_binary_operations_map_node
1601 {
1602 /* The token type. */
1603 enum cpp_ttype token_type;
1604 /* The corresponding tree code. */
1605 enum tree_code tree_type;
1606 /* The precedence of this operator. */
1607 enum cp_parser_prec prec;
1608 } cp_parser_binary_operations_map_node;
1609
1610 typedef struct cp_parser_expression_stack_entry
1611 {
1612 /* Left hand side of the binary operation we are currently
1613 parsing. */
1614 tree lhs;
1615 /* Original tree code for left hand side, if it was a binary
1616 expression itself (used for -Wparentheses). */
1617 enum tree_code lhs_type;
1618 /* Tree code for the binary operation we are parsing. */
1619 enum tree_code tree_type;
1620 /* Precedence of the binary operation we are parsing. */
1621 enum cp_parser_prec prec;
1622 /* Location of the binary operation we are parsing. */
1623 location_t loc;
1624 } cp_parser_expression_stack_entry;
1625
1626 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1627 entries because precedence levels on the stack are monotonically
1628 increasing. */
1629 typedef struct cp_parser_expression_stack_entry
1630 cp_parser_expression_stack[NUM_PREC_VALUES];
1631
1632 /* Prototypes. */
1633
1634 /* Constructors and destructors. */
1635
1636 static cp_parser_context *cp_parser_context_new
1637 (cp_parser_context *);
1638
1639 /* Class variables. */
1640
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1642
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644 Transformed into an associative array (binops_by_token) by
1645 cp_parser_new. */
1646
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1650
1651 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654
1655 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657
1658 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660
1661 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665
1666 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1668
1669 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1670
1671 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1672
1673 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1674
1675 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1676
1677 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1678 };
1679
1680 /* The same as binops, but initialized by cp_parser_new so that
1681 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1682 for speed. */
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1684
1685 /* Constructors and destructors. */
1686
1687 /* Construct a new context. The context below this one on the stack
1688 is given by NEXT. */
1689
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1692 {
1693 cp_parser_context *context;
1694
1695 /* Allocate the storage. */
1696 if (cp_parser_context_free_list != NULL)
1697 {
1698 /* Pull the first entry from the free list. */
1699 context = cp_parser_context_free_list;
1700 cp_parser_context_free_list = context->next;
1701 memset (context, 0, sizeof (*context));
1702 }
1703 else
1704 context = ggc_alloc_cleared_cp_parser_context ();
1705
1706 /* No errors have occurred yet in this context. */
1707 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708 /* If this is not the bottommost context, copy information that we
1709 need from the previous context. */
1710 if (next)
1711 {
1712 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713 expression, then we are parsing one in this context, too. */
1714 context->object_type = next->object_type;
1715 /* Thread the stack. */
1716 context->next = next;
1717 }
1718
1719 return context;
1720 }
1721
1722 /* Managing the unparsed function queues. */
1723
1724 #define unparsed_funs_with_default_args \
1725 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_definitions
1728 #define unparsed_nsdmis \
1729 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).nsdmis
1730
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1733 {
1734 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1735 VEC_safe_push (cp_unparsed_functions_entry, gc, parser->unparsed_queues, e);
1736 }
1737
1738 static void
1739 pop_unparsed_function_queues (cp_parser *parser)
1740 {
1741 release_tree_vector (unparsed_funs_with_definitions);
1742 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1743 }
1744
1745 /* Prototypes. */
1746
1747 /* Constructors and destructors. */
1748
1749 static cp_parser *cp_parser_new
1750 (void);
1751
1752 /* Routines to parse various constructs.
1753
1754 Those that return `tree' will return the error_mark_node (rather
1755 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1756 Sometimes, they will return an ordinary node if error-recovery was
1757 attempted, even though a parse error occurred. So, to check
1758 whether or not a parse error occurred, you should always use
1759 cp_parser_error_occurred. If the construct is optional (indicated
1760 either by an `_opt' in the name of the function that does the
1761 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1762 the construct is not present. */
1763
1764 /* Lexical conventions [gram.lex] */
1765
1766 static tree cp_parser_identifier
1767 (cp_parser *);
1768 static tree cp_parser_string_literal
1769 (cp_parser *, bool, bool);
1770 static tree cp_parser_userdef_char_literal
1771 (cp_parser *);
1772 static tree cp_parser_userdef_string_literal
1773 (cp_token *);
1774 static tree cp_parser_userdef_numeric_literal
1775 (cp_parser *);
1776
1777 /* Basic concepts [gram.basic] */
1778
1779 static bool cp_parser_translation_unit
1780 (cp_parser *);
1781
1782 /* Expressions [gram.expr] */
1783
1784 static tree cp_parser_primary_expression
1785 (cp_parser *, bool, bool, bool, cp_id_kind *);
1786 static tree cp_parser_id_expression
1787 (cp_parser *, bool, bool, bool *, bool, bool);
1788 static tree cp_parser_unqualified_id
1789 (cp_parser *, bool, bool, bool, bool);
1790 static tree cp_parser_nested_name_specifier_opt
1791 (cp_parser *, bool, bool, bool, bool);
1792 static tree cp_parser_nested_name_specifier
1793 (cp_parser *, bool, bool, bool, bool);
1794 static tree cp_parser_qualifying_entity
1795 (cp_parser *, bool, bool, bool, bool, bool);
1796 static tree cp_parser_postfix_expression
1797 (cp_parser *, bool, bool, bool, cp_id_kind *);
1798 static tree cp_parser_postfix_open_square_expression
1799 (cp_parser *, tree, bool);
1800 static tree cp_parser_postfix_dot_deref_expression
1801 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1802 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1803 (cp_parser *, int, bool, bool, bool *);
1804 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1805 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1806 static void cp_parser_pseudo_destructor_name
1807 (cp_parser *, tree *, tree *);
1808 static tree cp_parser_unary_expression
1809 (cp_parser *, bool, bool, cp_id_kind *);
1810 static enum tree_code cp_parser_unary_operator
1811 (cp_token *);
1812 static tree cp_parser_new_expression
1813 (cp_parser *);
1814 static VEC(tree,gc) *cp_parser_new_placement
1815 (cp_parser *);
1816 static tree cp_parser_new_type_id
1817 (cp_parser *, tree *);
1818 static cp_declarator *cp_parser_new_declarator_opt
1819 (cp_parser *);
1820 static cp_declarator *cp_parser_direct_new_declarator
1821 (cp_parser *);
1822 static VEC(tree,gc) *cp_parser_new_initializer
1823 (cp_parser *);
1824 static tree cp_parser_delete_expression
1825 (cp_parser *);
1826 static tree cp_parser_cast_expression
1827 (cp_parser *, bool, bool, cp_id_kind *);
1828 static tree cp_parser_binary_expression
1829 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1830 static tree cp_parser_question_colon_clause
1831 (cp_parser *, tree);
1832 static tree cp_parser_assignment_expression
1833 (cp_parser *, bool, cp_id_kind *);
1834 static enum tree_code cp_parser_assignment_operator_opt
1835 (cp_parser *);
1836 static tree cp_parser_expression
1837 (cp_parser *, bool, cp_id_kind *);
1838 static tree cp_parser_constant_expression
1839 (cp_parser *, bool, bool *);
1840 static tree cp_parser_builtin_offsetof
1841 (cp_parser *);
1842 static tree cp_parser_lambda_expression
1843 (cp_parser *);
1844 static void cp_parser_lambda_introducer
1845 (cp_parser *, tree);
1846 static bool cp_parser_lambda_declarator_opt
1847 (cp_parser *, tree);
1848 static void cp_parser_lambda_body
1849 (cp_parser *, tree);
1850
1851 /* Statements [gram.stmt.stmt] */
1852
1853 static void cp_parser_statement
1854 (cp_parser *, tree, bool, bool *);
1855 static void cp_parser_label_for_labeled_statement
1856 (cp_parser *);
1857 static tree cp_parser_expression_statement
1858 (cp_parser *, tree);
1859 static tree cp_parser_compound_statement
1860 (cp_parser *, tree, bool, bool);
1861 static void cp_parser_statement_seq_opt
1862 (cp_parser *, tree);
1863 static tree cp_parser_selection_statement
1864 (cp_parser *, bool *);
1865 static tree cp_parser_condition
1866 (cp_parser *);
1867 static tree cp_parser_iteration_statement
1868 (cp_parser *);
1869 static bool cp_parser_for_init_statement
1870 (cp_parser *, tree *decl);
1871 static tree cp_parser_for
1872 (cp_parser *);
1873 static tree cp_parser_c_for
1874 (cp_parser *, tree, tree);
1875 static tree cp_parser_range_for
1876 (cp_parser *, tree, tree, tree);
1877 static void do_range_for_auto_deduction
1878 (tree, tree);
1879 static tree cp_parser_perform_range_for_lookup
1880 (tree, tree *, tree *);
1881 static tree cp_parser_range_for_member_function
1882 (tree, tree);
1883 static tree cp_parser_jump_statement
1884 (cp_parser *);
1885 static void cp_parser_declaration_statement
1886 (cp_parser *);
1887
1888 static tree cp_parser_implicitly_scoped_statement
1889 (cp_parser *, bool *);
1890 static void cp_parser_already_scoped_statement
1891 (cp_parser *);
1892
1893 /* Declarations [gram.dcl.dcl] */
1894
1895 static void cp_parser_declaration_seq_opt
1896 (cp_parser *);
1897 static void cp_parser_declaration
1898 (cp_parser *);
1899 static void cp_parser_block_declaration
1900 (cp_parser *, bool);
1901 static void cp_parser_simple_declaration
1902 (cp_parser *, bool, tree *);
1903 static void cp_parser_decl_specifier_seq
1904 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1905 static tree cp_parser_storage_class_specifier_opt
1906 (cp_parser *);
1907 static tree cp_parser_function_specifier_opt
1908 (cp_parser *, cp_decl_specifier_seq *);
1909 static tree cp_parser_type_specifier
1910 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1911 int *, bool *);
1912 static tree cp_parser_simple_type_specifier
1913 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1914 static tree cp_parser_type_name
1915 (cp_parser *);
1916 static tree cp_parser_nonclass_name
1917 (cp_parser* parser);
1918 static tree cp_parser_elaborated_type_specifier
1919 (cp_parser *, bool, bool);
1920 static tree cp_parser_enum_specifier
1921 (cp_parser *);
1922 static void cp_parser_enumerator_list
1923 (cp_parser *, tree);
1924 static void cp_parser_enumerator_definition
1925 (cp_parser *, tree);
1926 static tree cp_parser_namespace_name
1927 (cp_parser *);
1928 static void cp_parser_namespace_definition
1929 (cp_parser *);
1930 static void cp_parser_namespace_body
1931 (cp_parser *);
1932 static tree cp_parser_qualified_namespace_specifier
1933 (cp_parser *);
1934 static void cp_parser_namespace_alias_definition
1935 (cp_parser *);
1936 static bool cp_parser_using_declaration
1937 (cp_parser *, bool);
1938 static void cp_parser_using_directive
1939 (cp_parser *);
1940 static tree cp_parser_alias_declaration
1941 (cp_parser *);
1942 static void cp_parser_asm_definition
1943 (cp_parser *);
1944 static void cp_parser_linkage_specification
1945 (cp_parser *);
1946 static void cp_parser_static_assert
1947 (cp_parser *, bool);
1948 static tree cp_parser_decltype
1949 (cp_parser *);
1950
1951 /* Declarators [gram.dcl.decl] */
1952
1953 static tree cp_parser_init_declarator
1954 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1955 static cp_declarator *cp_parser_declarator
1956 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1957 static cp_declarator *cp_parser_direct_declarator
1958 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1959 static enum tree_code cp_parser_ptr_operator
1960 (cp_parser *, tree *, cp_cv_quals *);
1961 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1962 (cp_parser *);
1963 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1964 (cp_parser *);
1965 static tree cp_parser_late_return_type_opt
1966 (cp_parser *, cp_cv_quals);
1967 static tree cp_parser_declarator_id
1968 (cp_parser *, bool);
1969 static tree cp_parser_type_id
1970 (cp_parser *);
1971 static tree cp_parser_template_type_arg
1972 (cp_parser *);
1973 static tree cp_parser_trailing_type_id (cp_parser *);
1974 static tree cp_parser_type_id_1
1975 (cp_parser *, bool, bool);
1976 static void cp_parser_type_specifier_seq
1977 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1978 static tree cp_parser_parameter_declaration_clause
1979 (cp_parser *);
1980 static tree cp_parser_parameter_declaration_list
1981 (cp_parser *, bool *);
1982 static cp_parameter_declarator *cp_parser_parameter_declaration
1983 (cp_parser *, bool, bool *);
1984 static tree cp_parser_default_argument
1985 (cp_parser *, bool);
1986 static void cp_parser_function_body
1987 (cp_parser *, bool);
1988 static tree cp_parser_initializer
1989 (cp_parser *, bool *, bool *);
1990 static tree cp_parser_initializer_clause
1991 (cp_parser *, bool *);
1992 static tree cp_parser_braced_list
1993 (cp_parser*, bool*);
1994 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1995 (cp_parser *, bool *);
1996
1997 static bool cp_parser_ctor_initializer_opt_and_function_body
1998 (cp_parser *, bool);
1999
2000 /* Classes [gram.class] */
2001
2002 static tree cp_parser_class_name
2003 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2004 static tree cp_parser_class_specifier
2005 (cp_parser *);
2006 static tree cp_parser_class_head
2007 (cp_parser *, bool *);
2008 static enum tag_types cp_parser_class_key
2009 (cp_parser *);
2010 static void cp_parser_member_specification_opt
2011 (cp_parser *);
2012 static void cp_parser_member_declaration
2013 (cp_parser *);
2014 static tree cp_parser_pure_specifier
2015 (cp_parser *);
2016 static tree cp_parser_constant_initializer
2017 (cp_parser *);
2018
2019 /* Derived classes [gram.class.derived] */
2020
2021 static tree cp_parser_base_clause
2022 (cp_parser *);
2023 static tree cp_parser_base_specifier
2024 (cp_parser *);
2025
2026 /* Special member functions [gram.special] */
2027
2028 static tree cp_parser_conversion_function_id
2029 (cp_parser *);
2030 static tree cp_parser_conversion_type_id
2031 (cp_parser *);
2032 static cp_declarator *cp_parser_conversion_declarator_opt
2033 (cp_parser *);
2034 static bool cp_parser_ctor_initializer_opt
2035 (cp_parser *);
2036 static void cp_parser_mem_initializer_list
2037 (cp_parser *);
2038 static tree cp_parser_mem_initializer
2039 (cp_parser *);
2040 static tree cp_parser_mem_initializer_id
2041 (cp_parser *);
2042
2043 /* Overloading [gram.over] */
2044
2045 static tree cp_parser_operator_function_id
2046 (cp_parser *);
2047 static tree cp_parser_operator
2048 (cp_parser *);
2049
2050 /* Templates [gram.temp] */
2051
2052 static void cp_parser_template_declaration
2053 (cp_parser *, bool);
2054 static tree cp_parser_template_parameter_list
2055 (cp_parser *);
2056 static tree cp_parser_template_parameter
2057 (cp_parser *, bool *, bool *);
2058 static tree cp_parser_type_parameter
2059 (cp_parser *, bool *);
2060 static tree cp_parser_template_id
2061 (cp_parser *, bool, bool, enum tag_types, bool);
2062 static tree cp_parser_template_name
2063 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2064 static tree cp_parser_template_argument_list
2065 (cp_parser *);
2066 static tree cp_parser_template_argument
2067 (cp_parser *);
2068 static void cp_parser_explicit_instantiation
2069 (cp_parser *);
2070 static void cp_parser_explicit_specialization
2071 (cp_parser *);
2072
2073 /* Exception handling [gram.exception] */
2074
2075 static tree cp_parser_try_block
2076 (cp_parser *);
2077 static bool cp_parser_function_try_block
2078 (cp_parser *);
2079 static void cp_parser_handler_seq
2080 (cp_parser *);
2081 static void cp_parser_handler
2082 (cp_parser *);
2083 static tree cp_parser_exception_declaration
2084 (cp_parser *);
2085 static tree cp_parser_throw_expression
2086 (cp_parser *);
2087 static tree cp_parser_exception_specification_opt
2088 (cp_parser *);
2089 static tree cp_parser_type_id_list
2090 (cp_parser *);
2091
2092 /* GNU Extensions */
2093
2094 static tree cp_parser_asm_specification_opt
2095 (cp_parser *);
2096 static tree cp_parser_asm_operand_list
2097 (cp_parser *);
2098 static tree cp_parser_asm_clobber_list
2099 (cp_parser *);
2100 static tree cp_parser_asm_label_list
2101 (cp_parser *);
2102 static tree cp_parser_attributes_opt
2103 (cp_parser *);
2104 static tree cp_parser_attribute_list
2105 (cp_parser *);
2106 static bool cp_parser_extension_opt
2107 (cp_parser *, int *);
2108 static void cp_parser_label_declaration
2109 (cp_parser *);
2110
2111 /* Transactional Memory Extensions */
2112
2113 static tree cp_parser_transaction
2114 (cp_parser *, enum rid);
2115 static tree cp_parser_transaction_expression
2116 (cp_parser *, enum rid);
2117 static bool cp_parser_function_transaction
2118 (cp_parser *, enum rid);
2119 static tree cp_parser_transaction_cancel
2120 (cp_parser *);
2121
2122 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2123 static bool cp_parser_pragma
2124 (cp_parser *, enum pragma_context);
2125
2126 /* Objective-C++ Productions */
2127
2128 static tree cp_parser_objc_message_receiver
2129 (cp_parser *);
2130 static tree cp_parser_objc_message_args
2131 (cp_parser *);
2132 static tree cp_parser_objc_message_expression
2133 (cp_parser *);
2134 static tree cp_parser_objc_encode_expression
2135 (cp_parser *);
2136 static tree cp_parser_objc_defs_expression
2137 (cp_parser *);
2138 static tree cp_parser_objc_protocol_expression
2139 (cp_parser *);
2140 static tree cp_parser_objc_selector_expression
2141 (cp_parser *);
2142 static tree cp_parser_objc_expression
2143 (cp_parser *);
2144 static bool cp_parser_objc_selector_p
2145 (enum cpp_ttype);
2146 static tree cp_parser_objc_selector
2147 (cp_parser *);
2148 static tree cp_parser_objc_protocol_refs_opt
2149 (cp_parser *);
2150 static void cp_parser_objc_declaration
2151 (cp_parser *, tree);
2152 static tree cp_parser_objc_statement
2153 (cp_parser *);
2154 static bool cp_parser_objc_valid_prefix_attributes
2155 (cp_parser *, tree *);
2156 static void cp_parser_objc_at_property_declaration
2157 (cp_parser *) ;
2158 static void cp_parser_objc_at_synthesize_declaration
2159 (cp_parser *) ;
2160 static void cp_parser_objc_at_dynamic_declaration
2161 (cp_parser *) ;
2162 static tree cp_parser_objc_struct_declaration
2163 (cp_parser *) ;
2164
2165 /* Utility Routines */
2166
2167 static tree cp_parser_lookup_name
2168 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2169 static tree cp_parser_lookup_name_simple
2170 (cp_parser *, tree, location_t);
2171 static tree cp_parser_maybe_treat_template_as_class
2172 (tree, bool);
2173 static bool cp_parser_check_declarator_template_parameters
2174 (cp_parser *, cp_declarator *, location_t);
2175 static bool cp_parser_check_template_parameters
2176 (cp_parser *, unsigned, location_t, cp_declarator *);
2177 static tree cp_parser_simple_cast_expression
2178 (cp_parser *);
2179 static tree cp_parser_global_scope_opt
2180 (cp_parser *, bool);
2181 static bool cp_parser_constructor_declarator_p
2182 (cp_parser *, bool);
2183 static tree cp_parser_function_definition_from_specifiers_and_declarator
2184 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2185 static tree cp_parser_function_definition_after_declarator
2186 (cp_parser *, bool);
2187 static void cp_parser_template_declaration_after_export
2188 (cp_parser *, bool);
2189 static void cp_parser_perform_template_parameter_access_checks
2190 (VEC (deferred_access_check,gc)*);
2191 static tree cp_parser_single_declaration
2192 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2193 static tree cp_parser_functional_cast
2194 (cp_parser *, tree);
2195 static tree cp_parser_save_member_function_body
2196 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2197 static tree cp_parser_save_nsdmi
2198 (cp_parser *);
2199 static tree cp_parser_enclosed_template_argument_list
2200 (cp_parser *);
2201 static void cp_parser_save_default_args
2202 (cp_parser *, tree);
2203 static void cp_parser_late_parsing_for_member
2204 (cp_parser *, tree);
2205 static tree cp_parser_late_parse_one_default_arg
2206 (cp_parser *, tree, tree, tree);
2207 static void cp_parser_late_parsing_nsdmi
2208 (cp_parser *, tree);
2209 static void cp_parser_late_parsing_default_args
2210 (cp_parser *, tree);
2211 static tree cp_parser_sizeof_operand
2212 (cp_parser *, enum rid);
2213 static tree cp_parser_trait_expr
2214 (cp_parser *, enum rid);
2215 static bool cp_parser_declares_only_class_p
2216 (cp_parser *);
2217 static void cp_parser_set_storage_class
2218 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2219 static void cp_parser_set_decl_spec_type
2220 (cp_decl_specifier_seq *, tree, location_t, bool);
2221 static void set_and_check_decl_spec_loc
2222 (cp_decl_specifier_seq *decl_specs,
2223 cp_decl_spec ds, source_location location);
2224 static bool cp_parser_friend_p
2225 (const cp_decl_specifier_seq *);
2226 static void cp_parser_required_error
2227 (cp_parser *, required_token, bool);
2228 static cp_token *cp_parser_require
2229 (cp_parser *, enum cpp_ttype, required_token);
2230 static cp_token *cp_parser_require_keyword
2231 (cp_parser *, enum rid, required_token);
2232 static bool cp_parser_token_starts_function_definition_p
2233 (cp_token *);
2234 static bool cp_parser_next_token_starts_class_definition_p
2235 (cp_parser *);
2236 static bool cp_parser_next_token_ends_template_argument_p
2237 (cp_parser *);
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239 (cp_parser *, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2241 (cp_token *);
2242 static void cp_parser_check_class_key
2243 (enum tag_types, tree type);
2244 static void cp_parser_check_access_in_redeclaration
2245 (tree type, location_t location);
2246 static bool cp_parser_optional_template_keyword
2247 (cp_parser *);
2248 static void cp_parser_pre_parsed_nested_name_specifier
2249 (cp_parser *);
2250 static bool cp_parser_cache_group
2251 (cp_parser *, enum cpp_ttype, unsigned);
2252 static tree cp_parser_cache_defarg
2253 (cp_parser *parser, bool nsdmi);
2254 static void cp_parser_parse_tentatively
2255 (cp_parser *);
2256 static void cp_parser_commit_to_tentative_parse
2257 (cp_parser *);
2258 static void cp_parser_abort_tentative_parse
2259 (cp_parser *);
2260 static bool cp_parser_parse_definitely
2261 (cp_parser *);
2262 static inline bool cp_parser_parsing_tentatively
2263 (cp_parser *);
2264 static bool cp_parser_uncommitted_to_tentative_parse_p
2265 (cp_parser *);
2266 static void cp_parser_error
2267 (cp_parser *, const char *);
2268 static void cp_parser_name_lookup_error
2269 (cp_parser *, tree, tree, name_lookup_error, location_t);
2270 static bool cp_parser_simulate_error
2271 (cp_parser *);
2272 static bool cp_parser_check_type_definition
2273 (cp_parser *);
2274 static void cp_parser_check_for_definition_in_return_type
2275 (cp_declarator *, tree, location_t type_location);
2276 static void cp_parser_check_for_invalid_template_id
2277 (cp_parser *, tree, enum tag_types, location_t location);
2278 static bool cp_parser_non_integral_constant_expression
2279 (cp_parser *, non_integral_constant);
2280 static void cp_parser_diagnose_invalid_type_name
2281 (cp_parser *, tree, tree, location_t);
2282 static bool cp_parser_parse_and_diagnose_invalid_type_name
2283 (cp_parser *);
2284 static int cp_parser_skip_to_closing_parenthesis
2285 (cp_parser *, bool, bool, bool);
2286 static void cp_parser_skip_to_end_of_statement
2287 (cp_parser *);
2288 static void cp_parser_consume_semicolon_at_end_of_statement
2289 (cp_parser *);
2290 static void cp_parser_skip_to_end_of_block_or_statement
2291 (cp_parser *);
2292 static bool cp_parser_skip_to_closing_brace
2293 (cp_parser *);
2294 static void cp_parser_skip_to_end_of_template_parameter_list
2295 (cp_parser *);
2296 static void cp_parser_skip_to_pragma_eol
2297 (cp_parser*, cp_token *);
2298 static bool cp_parser_error_occurred
2299 (cp_parser *);
2300 static bool cp_parser_allow_gnu_extensions_p
2301 (cp_parser *);
2302 static bool cp_parser_is_pure_string_literal
2303 (cp_token *);
2304 static bool cp_parser_is_string_literal
2305 (cp_token *);
2306 static bool cp_parser_is_keyword
2307 (cp_token *, enum rid);
2308 static tree cp_parser_make_typename_type
2309 (cp_parser *, tree, tree, location_t location);
2310 static cp_declarator * cp_parser_make_indirect_declarator
2311 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2312
2313 /* Returns nonzero if we are parsing tentatively. */
2314
2315 static inline bool
2316 cp_parser_parsing_tentatively (cp_parser* parser)
2317 {
2318 return parser->context->next != NULL;
2319 }
2320
2321 /* Returns nonzero if TOKEN is a string literal. */
2322
2323 static bool
2324 cp_parser_is_pure_string_literal (cp_token* token)
2325 {
2326 return (token->type == CPP_STRING ||
2327 token->type == CPP_STRING16 ||
2328 token->type == CPP_STRING32 ||
2329 token->type == CPP_WSTRING ||
2330 token->type == CPP_UTF8STRING);
2331 }
2332
2333 /* Returns nonzero if TOKEN is a string literal
2334 of a user-defined string literal. */
2335
2336 static bool
2337 cp_parser_is_string_literal (cp_token* token)
2338 {
2339 return (cp_parser_is_pure_string_literal (token) ||
2340 token->type == CPP_STRING_USERDEF ||
2341 token->type == CPP_STRING16_USERDEF ||
2342 token->type == CPP_STRING32_USERDEF ||
2343 token->type == CPP_WSTRING_USERDEF ||
2344 token->type == CPP_UTF8STRING_USERDEF);
2345 }
2346
2347 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2348
2349 static bool
2350 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2351 {
2352 return token->keyword == keyword;
2353 }
2354
2355 /* If not parsing tentatively, issue a diagnostic of the form
2356 FILE:LINE: MESSAGE before TOKEN
2357 where TOKEN is the next token in the input stream. MESSAGE
2358 (specified by the caller) is usually of the form "expected
2359 OTHER-TOKEN". */
2360
2361 static void
2362 cp_parser_error (cp_parser* parser, const char* gmsgid)
2363 {
2364 if (!cp_parser_simulate_error (parser))
2365 {
2366 cp_token *token = cp_lexer_peek_token (parser->lexer);
2367 /* This diagnostic makes more sense if it is tagged to the line
2368 of the token we just peeked at. */
2369 cp_lexer_set_source_position_from_token (token);
2370
2371 if (token->type == CPP_PRAGMA)
2372 {
2373 error_at (token->location,
2374 "%<#pragma%> is not allowed here");
2375 cp_parser_skip_to_pragma_eol (parser, token);
2376 return;
2377 }
2378
2379 c_parse_error (gmsgid,
2380 /* Because c_parser_error does not understand
2381 CPP_KEYWORD, keywords are treated like
2382 identifiers. */
2383 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2384 token->u.value, token->flags);
2385 }
2386 }
2387
2388 /* Issue an error about name-lookup failing. NAME is the
2389 IDENTIFIER_NODE DECL is the result of
2390 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2391 the thing that we hoped to find. */
2392
2393 static void
2394 cp_parser_name_lookup_error (cp_parser* parser,
2395 tree name,
2396 tree decl,
2397 name_lookup_error desired,
2398 location_t location)
2399 {
2400 /* If name lookup completely failed, tell the user that NAME was not
2401 declared. */
2402 if (decl == error_mark_node)
2403 {
2404 if (parser->scope && parser->scope != global_namespace)
2405 error_at (location, "%<%E::%E%> has not been declared",
2406 parser->scope, name);
2407 else if (parser->scope == global_namespace)
2408 error_at (location, "%<::%E%> has not been declared", name);
2409 else if (parser->object_scope
2410 && !CLASS_TYPE_P (parser->object_scope))
2411 error_at (location, "request for member %qE in non-class type %qT",
2412 name, parser->object_scope);
2413 else if (parser->object_scope)
2414 error_at (location, "%<%T::%E%> has not been declared",
2415 parser->object_scope, name);
2416 else
2417 error_at (location, "%qE has not been declared", name);
2418 }
2419 else if (parser->scope && parser->scope != global_namespace)
2420 {
2421 switch (desired)
2422 {
2423 case NLE_TYPE:
2424 error_at (location, "%<%E::%E%> is not a type",
2425 parser->scope, name);
2426 break;
2427 case NLE_CXX98:
2428 error_at (location, "%<%E::%E%> is not a class or namespace",
2429 parser->scope, name);
2430 break;
2431 case NLE_NOT_CXX98:
2432 error_at (location,
2433 "%<%E::%E%> is not a class, namespace, or enumeration",
2434 parser->scope, name);
2435 break;
2436 default:
2437 gcc_unreachable ();
2438
2439 }
2440 }
2441 else if (parser->scope == global_namespace)
2442 {
2443 switch (desired)
2444 {
2445 case NLE_TYPE:
2446 error_at (location, "%<::%E%> is not a type", name);
2447 break;
2448 case NLE_CXX98:
2449 error_at (location, "%<::%E%> is not a class or namespace", name);
2450 break;
2451 case NLE_NOT_CXX98:
2452 error_at (location,
2453 "%<::%E%> is not a class, namespace, or enumeration",
2454 name);
2455 break;
2456 default:
2457 gcc_unreachable ();
2458 }
2459 }
2460 else
2461 {
2462 switch (desired)
2463 {
2464 case NLE_TYPE:
2465 error_at (location, "%qE is not a type", name);
2466 break;
2467 case NLE_CXX98:
2468 error_at (location, "%qE is not a class or namespace", name);
2469 break;
2470 case NLE_NOT_CXX98:
2471 error_at (location,
2472 "%qE is not a class, namespace, or enumeration", name);
2473 break;
2474 default:
2475 gcc_unreachable ();
2476 }
2477 }
2478 }
2479
2480 /* If we are parsing tentatively, remember that an error has occurred
2481 during this tentative parse. Returns true if the error was
2482 simulated; false if a message should be issued by the caller. */
2483
2484 static bool
2485 cp_parser_simulate_error (cp_parser* parser)
2486 {
2487 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2488 {
2489 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2490 return true;
2491 }
2492 return false;
2493 }
2494
2495 /* This function is called when a type is defined. If type
2496 definitions are forbidden at this point, an error message is
2497 issued. */
2498
2499 static bool
2500 cp_parser_check_type_definition (cp_parser* parser)
2501 {
2502 /* If types are forbidden here, issue a message. */
2503 if (parser->type_definition_forbidden_message)
2504 {
2505 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2506 in the message need to be interpreted. */
2507 error (parser->type_definition_forbidden_message);
2508 return false;
2509 }
2510 return true;
2511 }
2512
2513 /* This function is called when the DECLARATOR is processed. The TYPE
2514 was a type defined in the decl-specifiers. If it is invalid to
2515 define a type in the decl-specifiers for DECLARATOR, an error is
2516 issued. TYPE_LOCATION is the location of TYPE and is used
2517 for error reporting. */
2518
2519 static void
2520 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2521 tree type, location_t type_location)
2522 {
2523 /* [dcl.fct] forbids type definitions in return types.
2524 Unfortunately, it's not easy to know whether or not we are
2525 processing a return type until after the fact. */
2526 while (declarator
2527 && (declarator->kind == cdk_pointer
2528 || declarator->kind == cdk_reference
2529 || declarator->kind == cdk_ptrmem))
2530 declarator = declarator->declarator;
2531 if (declarator
2532 && declarator->kind == cdk_function)
2533 {
2534 error_at (type_location,
2535 "new types may not be defined in a return type");
2536 inform (type_location,
2537 "(perhaps a semicolon is missing after the definition of %qT)",
2538 type);
2539 }
2540 }
2541
2542 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2543 "<" in any valid C++ program. If the next token is indeed "<",
2544 issue a message warning the user about what appears to be an
2545 invalid attempt to form a template-id. LOCATION is the location
2546 of the type-specifier (TYPE) */
2547
2548 static void
2549 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2550 tree type,
2551 enum tag_types tag_type,
2552 location_t location)
2553 {
2554 cp_token_position start = 0;
2555
2556 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2557 {
2558 if (TYPE_P (type))
2559 error_at (location, "%qT is not a template", type);
2560 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2561 {
2562 if (tag_type != none_type)
2563 error_at (location, "%qE is not a class template", type);
2564 else
2565 error_at (location, "%qE is not a template", type);
2566 }
2567 else
2568 error_at (location, "invalid template-id");
2569 /* Remember the location of the invalid "<". */
2570 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2571 start = cp_lexer_token_position (parser->lexer, true);
2572 /* Consume the "<". */
2573 cp_lexer_consume_token (parser->lexer);
2574 /* Parse the template arguments. */
2575 cp_parser_enclosed_template_argument_list (parser);
2576 /* Permanently remove the invalid template arguments so that
2577 this error message is not issued again. */
2578 if (start)
2579 cp_lexer_purge_tokens_after (parser->lexer, start);
2580 }
2581 }
2582
2583 /* If parsing an integral constant-expression, issue an error message
2584 about the fact that THING appeared and return true. Otherwise,
2585 return false. In either case, set
2586 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2587
2588 static bool
2589 cp_parser_non_integral_constant_expression (cp_parser *parser,
2590 non_integral_constant thing)
2591 {
2592 parser->non_integral_constant_expression_p = true;
2593 if (parser->integral_constant_expression_p)
2594 {
2595 if (!parser->allow_non_integral_constant_expression_p)
2596 {
2597 const char *msg = NULL;
2598 switch (thing)
2599 {
2600 case NIC_FLOAT:
2601 error ("floating-point literal "
2602 "cannot appear in a constant-expression");
2603 return true;
2604 case NIC_CAST:
2605 error ("a cast to a type other than an integral or "
2606 "enumeration type cannot appear in a "
2607 "constant-expression");
2608 return true;
2609 case NIC_TYPEID:
2610 error ("%<typeid%> operator "
2611 "cannot appear in a constant-expression");
2612 return true;
2613 case NIC_NCC:
2614 error ("non-constant compound literals "
2615 "cannot appear in a constant-expression");
2616 return true;
2617 case NIC_FUNC_CALL:
2618 error ("a function call "
2619 "cannot appear in a constant-expression");
2620 return true;
2621 case NIC_INC:
2622 error ("an increment "
2623 "cannot appear in a constant-expression");
2624 return true;
2625 case NIC_DEC:
2626 error ("an decrement "
2627 "cannot appear in a constant-expression");
2628 return true;
2629 case NIC_ARRAY_REF:
2630 error ("an array reference "
2631 "cannot appear in a constant-expression");
2632 return true;
2633 case NIC_ADDR_LABEL:
2634 error ("the address of a label "
2635 "cannot appear in a constant-expression");
2636 return true;
2637 case NIC_OVERLOADED:
2638 error ("calls to overloaded operators "
2639 "cannot appear in a constant-expression");
2640 return true;
2641 case NIC_ASSIGNMENT:
2642 error ("an assignment cannot appear in a constant-expression");
2643 return true;
2644 case NIC_COMMA:
2645 error ("a comma operator "
2646 "cannot appear in a constant-expression");
2647 return true;
2648 case NIC_CONSTRUCTOR:
2649 error ("a call to a constructor "
2650 "cannot appear in a constant-expression");
2651 return true;
2652 case NIC_TRANSACTION:
2653 error ("a transaction expression "
2654 "cannot appear in a constant-expression");
2655 return true;
2656 case NIC_THIS:
2657 msg = "this";
2658 break;
2659 case NIC_FUNC_NAME:
2660 msg = "__FUNCTION__";
2661 break;
2662 case NIC_PRETTY_FUNC:
2663 msg = "__PRETTY_FUNCTION__";
2664 break;
2665 case NIC_C99_FUNC:
2666 msg = "__func__";
2667 break;
2668 case NIC_VA_ARG:
2669 msg = "va_arg";
2670 break;
2671 case NIC_ARROW:
2672 msg = "->";
2673 break;
2674 case NIC_POINT:
2675 msg = ".";
2676 break;
2677 case NIC_STAR:
2678 msg = "*";
2679 break;
2680 case NIC_ADDR:
2681 msg = "&";
2682 break;
2683 case NIC_PREINCREMENT:
2684 msg = "++";
2685 break;
2686 case NIC_PREDECREMENT:
2687 msg = "--";
2688 break;
2689 case NIC_NEW:
2690 msg = "new";
2691 break;
2692 case NIC_DEL:
2693 msg = "delete";
2694 break;
2695 default:
2696 gcc_unreachable ();
2697 }
2698 if (msg)
2699 error ("%qs cannot appear in a constant-expression", msg);
2700 return true;
2701 }
2702 }
2703 return false;
2704 }
2705
2706 /* Emit a diagnostic for an invalid type name. SCOPE is the
2707 qualifying scope (or NULL, if none) for ID. This function commits
2708 to the current active tentative parse, if any. (Otherwise, the
2709 problematic construct might be encountered again later, resulting
2710 in duplicate error messages.) LOCATION is the location of ID. */
2711
2712 static void
2713 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2714 tree scope, tree id,
2715 location_t location)
2716 {
2717 tree decl, old_scope;
2718 cp_parser_commit_to_tentative_parse (parser);
2719 /* Try to lookup the identifier. */
2720 old_scope = parser->scope;
2721 parser->scope = scope;
2722 decl = cp_parser_lookup_name_simple (parser, id, location);
2723 parser->scope = old_scope;
2724 /* If the lookup found a template-name, it means that the user forgot
2725 to specify an argument list. Emit a useful error message. */
2726 if (TREE_CODE (decl) == TEMPLATE_DECL)
2727 error_at (location,
2728 "invalid use of template-name %qE without an argument list",
2729 decl);
2730 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2731 error_at (location, "invalid use of destructor %qD as a type", id);
2732 else if (TREE_CODE (decl) == TYPE_DECL)
2733 /* Something like 'unsigned A a;' */
2734 error_at (location, "invalid combination of multiple type-specifiers");
2735 else if (!parser->scope)
2736 {
2737 /* Issue an error message. */
2738 error_at (location, "%qE does not name a type", id);
2739 /* If we're in a template class, it's possible that the user was
2740 referring to a type from a base class. For example:
2741
2742 template <typename T> struct A { typedef T X; };
2743 template <typename T> struct B : public A<T> { X x; };
2744
2745 The user should have said "typename A<T>::X". */
2746 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2747 inform (location, "C++11 %<constexpr%> only available with "
2748 "-std=c++11 or -std=gnu++11");
2749 else if (processing_template_decl && current_class_type
2750 && TYPE_BINFO (current_class_type))
2751 {
2752 tree b;
2753
2754 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2755 b;
2756 b = TREE_CHAIN (b))
2757 {
2758 tree base_type = BINFO_TYPE (b);
2759 if (CLASS_TYPE_P (base_type)
2760 && dependent_type_p (base_type))
2761 {
2762 tree field;
2763 /* Go from a particular instantiation of the
2764 template (which will have an empty TYPE_FIELDs),
2765 to the main version. */
2766 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2767 for (field = TYPE_FIELDS (base_type);
2768 field;
2769 field = DECL_CHAIN (field))
2770 if (TREE_CODE (field) == TYPE_DECL
2771 && DECL_NAME (field) == id)
2772 {
2773 inform (location,
2774 "(perhaps %<typename %T::%E%> was intended)",
2775 BINFO_TYPE (b), id);
2776 break;
2777 }
2778 if (field)
2779 break;
2780 }
2781 }
2782 }
2783 }
2784 /* Here we diagnose qualified-ids where the scope is actually correct,
2785 but the identifier does not resolve to a valid type name. */
2786 else if (parser->scope != error_mark_node)
2787 {
2788 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2789 error_at (location, "%qE in namespace %qE does not name a type",
2790 id, parser->scope);
2791 else if (CLASS_TYPE_P (parser->scope)
2792 && constructor_name_p (id, parser->scope))
2793 {
2794 /* A<T>::A<T>() */
2795 error_at (location, "%<%T::%E%> names the constructor, not"
2796 " the type", parser->scope, id);
2797 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2798 error_at (location, "and %qT has no template constructors",
2799 parser->scope);
2800 }
2801 else if (TYPE_P (parser->scope)
2802 && dependent_scope_p (parser->scope))
2803 error_at (location, "need %<typename%> before %<%T::%E%> because "
2804 "%qT is a dependent scope",
2805 parser->scope, id, parser->scope);
2806 else if (TYPE_P (parser->scope))
2807 error_at (location, "%qE in %q#T does not name a type",
2808 id, parser->scope);
2809 else
2810 gcc_unreachable ();
2811 }
2812 }
2813
2814 /* Check for a common situation where a type-name should be present,
2815 but is not, and issue a sensible error message. Returns true if an
2816 invalid type-name was detected.
2817
2818 The situation handled by this function are variable declarations of the
2819 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2820 Usually, `ID' should name a type, but if we got here it means that it
2821 does not. We try to emit the best possible error message depending on
2822 how exactly the id-expression looks like. */
2823
2824 static bool
2825 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2826 {
2827 tree id;
2828 cp_token *token = cp_lexer_peek_token (parser->lexer);
2829
2830 /* Avoid duplicate error about ambiguous lookup. */
2831 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2832 {
2833 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2834 if (next->type == CPP_NAME && next->ambiguous_p)
2835 goto out;
2836 }
2837
2838 cp_parser_parse_tentatively (parser);
2839 id = cp_parser_id_expression (parser,
2840 /*template_keyword_p=*/false,
2841 /*check_dependency_p=*/true,
2842 /*template_p=*/NULL,
2843 /*declarator_p=*/true,
2844 /*optional_p=*/false);
2845 /* If the next token is a (, this is a function with no explicit return
2846 type, i.e. constructor, destructor or conversion op. */
2847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2848 || TREE_CODE (id) == TYPE_DECL)
2849 {
2850 cp_parser_abort_tentative_parse (parser);
2851 return false;
2852 }
2853 if (!cp_parser_parse_definitely (parser))
2854 return false;
2855
2856 /* Emit a diagnostic for the invalid type. */
2857 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2858 id, token->location);
2859 out:
2860 /* If we aren't in the middle of a declarator (i.e. in a
2861 parameter-declaration-clause), skip to the end of the declaration;
2862 there's no point in trying to process it. */
2863 if (!parser->in_declarator_p)
2864 cp_parser_skip_to_end_of_block_or_statement (parser);
2865 return true;
2866 }
2867
2868 /* Consume tokens up to, and including, the next non-nested closing `)'.
2869 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2870 are doing error recovery. Returns -1 if OR_COMMA is true and we
2871 found an unnested comma. */
2872
2873 static int
2874 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2875 bool recovering,
2876 bool or_comma,
2877 bool consume_paren)
2878 {
2879 unsigned paren_depth = 0;
2880 unsigned brace_depth = 0;
2881 unsigned square_depth = 0;
2882
2883 if (recovering && !or_comma
2884 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2885 return 0;
2886
2887 while (true)
2888 {
2889 cp_token * token = cp_lexer_peek_token (parser->lexer);
2890
2891 switch (token->type)
2892 {
2893 case CPP_EOF:
2894 case CPP_PRAGMA_EOL:
2895 /* If we've run out of tokens, then there is no closing `)'. */
2896 return 0;
2897
2898 /* This is good for lambda expression capture-lists. */
2899 case CPP_OPEN_SQUARE:
2900 ++square_depth;
2901 break;
2902 case CPP_CLOSE_SQUARE:
2903 if (!square_depth--)
2904 return 0;
2905 break;
2906
2907 case CPP_SEMICOLON:
2908 /* This matches the processing in skip_to_end_of_statement. */
2909 if (!brace_depth)
2910 return 0;
2911 break;
2912
2913 case CPP_OPEN_BRACE:
2914 ++brace_depth;
2915 break;
2916 case CPP_CLOSE_BRACE:
2917 if (!brace_depth--)
2918 return 0;
2919 break;
2920
2921 case CPP_COMMA:
2922 if (recovering && or_comma && !brace_depth && !paren_depth
2923 && !square_depth)
2924 return -1;
2925 break;
2926
2927 case CPP_OPEN_PAREN:
2928 if (!brace_depth)
2929 ++paren_depth;
2930 break;
2931
2932 case CPP_CLOSE_PAREN:
2933 if (!brace_depth && !paren_depth--)
2934 {
2935 if (consume_paren)
2936 cp_lexer_consume_token (parser->lexer);
2937 return 1;
2938 }
2939 break;
2940
2941 default:
2942 break;
2943 }
2944
2945 /* Consume the token. */
2946 cp_lexer_consume_token (parser->lexer);
2947 }
2948 }
2949
2950 /* Consume tokens until we reach the end of the current statement.
2951 Normally, that will be just before consuming a `;'. However, if a
2952 non-nested `}' comes first, then we stop before consuming that. */
2953
2954 static void
2955 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2956 {
2957 unsigned nesting_depth = 0;
2958
2959 while (true)
2960 {
2961 cp_token *token = cp_lexer_peek_token (parser->lexer);
2962
2963 switch (token->type)
2964 {
2965 case CPP_EOF:
2966 case CPP_PRAGMA_EOL:
2967 /* If we've run out of tokens, stop. */
2968 return;
2969
2970 case CPP_SEMICOLON:
2971 /* If the next token is a `;', we have reached the end of the
2972 statement. */
2973 if (!nesting_depth)
2974 return;
2975 break;
2976
2977 case CPP_CLOSE_BRACE:
2978 /* If this is a non-nested '}', stop before consuming it.
2979 That way, when confronted with something like:
2980
2981 { 3 + }
2982
2983 we stop before consuming the closing '}', even though we
2984 have not yet reached a `;'. */
2985 if (nesting_depth == 0)
2986 return;
2987
2988 /* If it is the closing '}' for a block that we have
2989 scanned, stop -- but only after consuming the token.
2990 That way given:
2991
2992 void f g () { ... }
2993 typedef int I;
2994
2995 we will stop after the body of the erroneously declared
2996 function, but before consuming the following `typedef'
2997 declaration. */
2998 if (--nesting_depth == 0)
2999 {
3000 cp_lexer_consume_token (parser->lexer);
3001 return;
3002 }
3003
3004 case CPP_OPEN_BRACE:
3005 ++nesting_depth;
3006 break;
3007
3008 default:
3009 break;
3010 }
3011
3012 /* Consume the token. */
3013 cp_lexer_consume_token (parser->lexer);
3014 }
3015 }
3016
3017 /* This function is called at the end of a statement or declaration.
3018 If the next token is a semicolon, it is consumed; otherwise, error
3019 recovery is attempted. */
3020
3021 static void
3022 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3023 {
3024 /* Look for the trailing `;'. */
3025 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3026 {
3027 /* If there is additional (erroneous) input, skip to the end of
3028 the statement. */
3029 cp_parser_skip_to_end_of_statement (parser);
3030 /* If the next token is now a `;', consume it. */
3031 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3032 cp_lexer_consume_token (parser->lexer);
3033 }
3034 }
3035
3036 /* Skip tokens until we have consumed an entire block, or until we
3037 have consumed a non-nested `;'. */
3038
3039 static void
3040 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3041 {
3042 int nesting_depth = 0;
3043
3044 while (nesting_depth >= 0)
3045 {
3046 cp_token *token = cp_lexer_peek_token (parser->lexer);
3047
3048 switch (token->type)
3049 {
3050 case CPP_EOF:
3051 case CPP_PRAGMA_EOL:
3052 /* If we've run out of tokens, stop. */
3053 return;
3054
3055 case CPP_SEMICOLON:
3056 /* Stop if this is an unnested ';'. */
3057 if (!nesting_depth)
3058 nesting_depth = -1;
3059 break;
3060
3061 case CPP_CLOSE_BRACE:
3062 /* Stop if this is an unnested '}', or closes the outermost
3063 nesting level. */
3064 nesting_depth--;
3065 if (nesting_depth < 0)
3066 return;
3067 if (!nesting_depth)
3068 nesting_depth = -1;
3069 break;
3070
3071 case CPP_OPEN_BRACE:
3072 /* Nest. */
3073 nesting_depth++;
3074 break;
3075
3076 default:
3077 break;
3078 }
3079
3080 /* Consume the token. */
3081 cp_lexer_consume_token (parser->lexer);
3082 }
3083 }
3084
3085 /* Skip tokens until a non-nested closing curly brace is the next
3086 token, or there are no more tokens. Return true in the first case,
3087 false otherwise. */
3088
3089 static bool
3090 cp_parser_skip_to_closing_brace (cp_parser *parser)
3091 {
3092 unsigned nesting_depth = 0;
3093
3094 while (true)
3095 {
3096 cp_token *token = cp_lexer_peek_token (parser->lexer);
3097
3098 switch (token->type)
3099 {
3100 case CPP_EOF:
3101 case CPP_PRAGMA_EOL:
3102 /* If we've run out of tokens, stop. */
3103 return false;
3104
3105 case CPP_CLOSE_BRACE:
3106 /* If the next token is a non-nested `}', then we have reached
3107 the end of the current block. */
3108 if (nesting_depth-- == 0)
3109 return true;
3110 break;
3111
3112 case CPP_OPEN_BRACE:
3113 /* If it the next token is a `{', then we are entering a new
3114 block. Consume the entire block. */
3115 ++nesting_depth;
3116 break;
3117
3118 default:
3119 break;
3120 }
3121
3122 /* Consume the token. */
3123 cp_lexer_consume_token (parser->lexer);
3124 }
3125 }
3126
3127 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3128 parameter is the PRAGMA token, allowing us to purge the entire pragma
3129 sequence. */
3130
3131 static void
3132 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3133 {
3134 cp_token *token;
3135
3136 parser->lexer->in_pragma = false;
3137
3138 do
3139 token = cp_lexer_consume_token (parser->lexer);
3140 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3141
3142 /* Ensure that the pragma is not parsed again. */
3143 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3144 }
3145
3146 /* Require pragma end of line, resyncing with it as necessary. The
3147 arguments are as for cp_parser_skip_to_pragma_eol. */
3148
3149 static void
3150 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3151 {
3152 parser->lexer->in_pragma = false;
3153 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3154 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3155 }
3156
3157 /* This is a simple wrapper around make_typename_type. When the id is
3158 an unresolved identifier node, we can provide a superior diagnostic
3159 using cp_parser_diagnose_invalid_type_name. */
3160
3161 static tree
3162 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3163 tree id, location_t id_location)
3164 {
3165 tree result;
3166 if (TREE_CODE (id) == IDENTIFIER_NODE)
3167 {
3168 result = make_typename_type (scope, id, typename_type,
3169 /*complain=*/tf_none);
3170 if (result == error_mark_node)
3171 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3172 return result;
3173 }
3174 return make_typename_type (scope, id, typename_type, tf_error);
3175 }
3176
3177 /* This is a wrapper around the
3178 make_{pointer,ptrmem,reference}_declarator functions that decides
3179 which one to call based on the CODE and CLASS_TYPE arguments. The
3180 CODE argument should be one of the values returned by
3181 cp_parser_ptr_operator. */
3182 static cp_declarator *
3183 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3184 cp_cv_quals cv_qualifiers,
3185 cp_declarator *target)
3186 {
3187 if (code == ERROR_MARK)
3188 return cp_error_declarator;
3189
3190 if (code == INDIRECT_REF)
3191 if (class_type == NULL_TREE)
3192 return make_pointer_declarator (cv_qualifiers, target);
3193 else
3194 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3195 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3196 return make_reference_declarator (cv_qualifiers, target, false);
3197 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3198 return make_reference_declarator (cv_qualifiers, target, true);
3199 gcc_unreachable ();
3200 }
3201
3202 /* Create a new C++ parser. */
3203
3204 static cp_parser *
3205 cp_parser_new (void)
3206 {
3207 cp_parser *parser;
3208 cp_lexer *lexer;
3209 unsigned i;
3210
3211 /* cp_lexer_new_main is called before doing GC allocation because
3212 cp_lexer_new_main might load a PCH file. */
3213 lexer = cp_lexer_new_main ();
3214
3215 /* Initialize the binops_by_token so that we can get the tree
3216 directly from the token. */
3217 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3218 binops_by_token[binops[i].token_type] = binops[i];
3219
3220 parser = ggc_alloc_cleared_cp_parser ();
3221 parser->lexer = lexer;
3222 parser->context = cp_parser_context_new (NULL);
3223
3224 /* For now, we always accept GNU extensions. */
3225 parser->allow_gnu_extensions_p = 1;
3226
3227 /* The `>' token is a greater-than operator, not the end of a
3228 template-id. */
3229 parser->greater_than_is_operator_p = true;
3230
3231 parser->default_arg_ok_p = true;
3232
3233 /* We are not parsing a constant-expression. */
3234 parser->integral_constant_expression_p = false;
3235 parser->allow_non_integral_constant_expression_p = false;
3236 parser->non_integral_constant_expression_p = false;
3237
3238 /* Local variable names are not forbidden. */
3239 parser->local_variables_forbidden_p = false;
3240
3241 /* We are not processing an `extern "C"' declaration. */
3242 parser->in_unbraced_linkage_specification_p = false;
3243
3244 /* We are not processing a declarator. */
3245 parser->in_declarator_p = false;
3246
3247 /* We are not processing a template-argument-list. */
3248 parser->in_template_argument_list_p = false;
3249
3250 /* We are not in an iteration statement. */
3251 parser->in_statement = 0;
3252
3253 /* We are not in a switch statement. */
3254 parser->in_switch_statement_p = false;
3255
3256 /* We are not parsing a type-id inside an expression. */
3257 parser->in_type_id_in_expr_p = false;
3258
3259 /* Declarations aren't implicitly extern "C". */
3260 parser->implicit_extern_c = false;
3261
3262 /* String literals should be translated to the execution character set. */
3263 parser->translate_strings_p = true;
3264
3265 /* We are not parsing a function body. */
3266 parser->in_function_body = false;
3267
3268 /* We can correct until told otherwise. */
3269 parser->colon_corrects_to_scope_p = true;
3270
3271 /* The unparsed function queue is empty. */
3272 push_unparsed_function_queues (parser);
3273
3274 /* There are no classes being defined. */
3275 parser->num_classes_being_defined = 0;
3276
3277 /* No template parameters apply. */
3278 parser->num_template_parameter_lists = 0;
3279
3280 return parser;
3281 }
3282
3283 /* Create a cp_lexer structure which will emit the tokens in CACHE
3284 and push it onto the parser's lexer stack. This is used for delayed
3285 parsing of in-class method bodies and default arguments, and should
3286 not be confused with tentative parsing. */
3287 static void
3288 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3289 {
3290 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3291 lexer->next = parser->lexer;
3292 parser->lexer = lexer;
3293
3294 /* Move the current source position to that of the first token in the
3295 new lexer. */
3296 cp_lexer_set_source_position_from_token (lexer->next_token);
3297 }
3298
3299 /* Pop the top lexer off the parser stack. This is never used for the
3300 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3301 static void
3302 cp_parser_pop_lexer (cp_parser *parser)
3303 {
3304 cp_lexer *lexer = parser->lexer;
3305 parser->lexer = lexer->next;
3306 cp_lexer_destroy (lexer);
3307
3308 /* Put the current source position back where it was before this
3309 lexer was pushed. */
3310 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3311 }
3312
3313 /* Lexical conventions [gram.lex] */
3314
3315 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3316 identifier. */
3317
3318 static tree
3319 cp_parser_identifier (cp_parser* parser)
3320 {
3321 cp_token *token;
3322
3323 /* Look for the identifier. */
3324 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3325 /* Return the value. */
3326 return token ? token->u.value : error_mark_node;
3327 }
3328
3329 /* Parse a sequence of adjacent string constants. Returns a
3330 TREE_STRING representing the combined, nul-terminated string
3331 constant. If TRANSLATE is true, translate the string to the
3332 execution character set. If WIDE_OK is true, a wide string is
3333 invalid here.
3334
3335 C++98 [lex.string] says that if a narrow string literal token is
3336 adjacent to a wide string literal token, the behavior is undefined.
3337 However, C99 6.4.5p4 says that this results in a wide string literal.
3338 We follow C99 here, for consistency with the C front end.
3339
3340 This code is largely lifted from lex_string() in c-lex.c.
3341
3342 FUTURE: ObjC++ will need to handle @-strings here. */
3343 static tree
3344 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3345 {
3346 tree value;
3347 size_t count;
3348 struct obstack str_ob;
3349 cpp_string str, istr, *strs;
3350 cp_token *tok;
3351 enum cpp_ttype type, curr_type;
3352 int have_suffix_p = 0;
3353 tree string_tree;
3354 tree suffix_id = NULL_TREE;
3355 bool curr_tok_is_userdef_p = false;
3356
3357 tok = cp_lexer_peek_token (parser->lexer);
3358 if (!cp_parser_is_string_literal (tok))
3359 {
3360 cp_parser_error (parser, "expected string-literal");
3361 return error_mark_node;
3362 }
3363
3364 if (cpp_userdef_string_p (tok->type))
3365 {
3366 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3367 curr_type = cpp_userdef_string_remove_type (tok->type);
3368 curr_tok_is_userdef_p = true;
3369 }
3370 else
3371 {
3372 string_tree = tok->u.value;
3373 curr_type = tok->type;
3374 }
3375 type = curr_type;
3376
3377 /* Try to avoid the overhead of creating and destroying an obstack
3378 for the common case of just one string. */
3379 if (!cp_parser_is_string_literal
3380 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3381 {
3382 cp_lexer_consume_token (parser->lexer);
3383
3384 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3385 str.len = TREE_STRING_LENGTH (string_tree);
3386 count = 1;
3387
3388 if (curr_tok_is_userdef_p)
3389 {
3390 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3391 have_suffix_p = 1;
3392 curr_type = cpp_userdef_string_remove_type (tok->type);
3393 }
3394 else
3395 curr_type = tok->type;
3396
3397 strs = &str;
3398 }
3399 else
3400 {
3401 gcc_obstack_init (&str_ob);
3402 count = 0;
3403
3404 do
3405 {
3406 cp_lexer_consume_token (parser->lexer);
3407 count++;
3408 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3409 str.len = TREE_STRING_LENGTH (string_tree);
3410
3411 if (curr_tok_is_userdef_p)
3412 {
3413 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3414 if (have_suffix_p == 0)
3415 {
3416 suffix_id = curr_suffix_id;
3417 have_suffix_p = 1;
3418 }
3419 else if (have_suffix_p == 1
3420 && curr_suffix_id != suffix_id)
3421 {
3422 error ("inconsistent user-defined literal suffixes"
3423 " %qD and %qD in string literal",
3424 suffix_id, curr_suffix_id);
3425 have_suffix_p = -1;
3426 }
3427 curr_type = cpp_userdef_string_remove_type (tok->type);
3428 }
3429 else
3430 curr_type = tok->type;
3431
3432 if (type != curr_type)
3433 {
3434 if (type == CPP_STRING)
3435 type = curr_type;
3436 else if (curr_type != CPP_STRING)
3437 error_at (tok->location,
3438 "unsupported non-standard concatenation "
3439 "of string literals");
3440 }
3441
3442 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3443
3444 tok = cp_lexer_peek_token (parser->lexer);
3445 if (cpp_userdef_string_p (tok->type))
3446 {
3447 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3448 curr_type = cpp_userdef_string_remove_type (tok->type);
3449 curr_tok_is_userdef_p = true;
3450 }
3451 else
3452 {
3453 string_tree = tok->u.value;
3454 curr_type = tok->type;
3455 curr_tok_is_userdef_p = false;
3456 }
3457 }
3458 while (cp_parser_is_string_literal (tok));
3459
3460 strs = (cpp_string *) obstack_finish (&str_ob);
3461 }
3462
3463 if (type != CPP_STRING && !wide_ok)
3464 {
3465 cp_parser_error (parser, "a wide string is invalid in this context");
3466 type = CPP_STRING;
3467 }
3468
3469 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3470 (parse_in, strs, count, &istr, type))
3471 {
3472 value = build_string (istr.len, (const char *)istr.text);
3473 free (CONST_CAST (unsigned char *, istr.text));
3474
3475 switch (type)
3476 {
3477 default:
3478 case CPP_STRING:
3479 case CPP_UTF8STRING:
3480 TREE_TYPE (value) = char_array_type_node;
3481 break;
3482 case CPP_STRING16:
3483 TREE_TYPE (value) = char16_array_type_node;
3484 break;
3485 case CPP_STRING32:
3486 TREE_TYPE (value) = char32_array_type_node;
3487 break;
3488 case CPP_WSTRING:
3489 TREE_TYPE (value) = wchar_array_type_node;
3490 break;
3491 }
3492
3493 value = fix_string_type (value);
3494
3495 if (have_suffix_p)
3496 {
3497 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3498 tok->u.value = literal;
3499 return cp_parser_userdef_string_literal (tok);
3500 }
3501 }
3502 else
3503 /* cpp_interpret_string has issued an error. */
3504 value = error_mark_node;
3505
3506 if (count > 1)
3507 obstack_free (&str_ob, 0);
3508
3509 return value;
3510 }
3511
3512 /* Look up a literal operator with the name and the exact arguments. */
3513
3514 static tree
3515 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3516 {
3517 tree decl, fns;
3518 decl = lookup_name (name);
3519 if (!decl || !is_overloaded_fn (decl))
3520 return error_mark_node;
3521
3522 for (fns = decl; fns; fns = OVL_NEXT (fns))
3523 {
3524 unsigned int ix;
3525 bool found = true;
3526 tree fn = OVL_CURRENT (fns);
3527 tree argtypes = NULL_TREE;
3528 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3529 if (argtypes != NULL_TREE)
3530 {
3531 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3532 ++ix, argtypes = TREE_CHAIN (argtypes))
3533 {
3534 tree targ = TREE_VALUE (argtypes);
3535 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3536 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3537 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3538 if ((ptr || arr || !same_type_p (targ, tparm))
3539 && (!ptr || !arr
3540 || !same_type_p (TREE_TYPE (targ),
3541 TREE_TYPE (tparm))))
3542 found = false;
3543 }
3544 if (found
3545 && ix == VEC_length (tree, args)
3546 /* May be this should be sufficient_parms_p instead,
3547 depending on how exactly should user-defined literals
3548 work in presence of default arguments on the literal
3549 operator parameters. */
3550 && argtypes == void_list_node)
3551 return fn;
3552 }
3553 }
3554
3555 return error_mark_node;
3556 }
3557
3558 /* Parse a user-defined char constant. Returns a call to a user-defined
3559 literal operator taking the character as an argument. */
3560
3561 static tree
3562 cp_parser_userdef_char_literal (cp_parser *parser)
3563 {
3564 cp_token *token = cp_lexer_consume_token (parser->lexer);
3565 tree literal = token->u.value;
3566 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3567 tree value = USERDEF_LITERAL_VALUE (literal);
3568 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3569 tree decl, result;
3570
3571 /* Build up a call to the user-defined operator */
3572 /* Lookup the name we got back from the id-expression. */
3573 VEC(tree,gc) *args = make_tree_vector ();
3574 VEC_safe_push (tree, gc, args, value);
3575 decl = lookup_literal_operator (name, args);
3576 if (!decl || decl == error_mark_node)
3577 {
3578 error ("unable to find character literal operator %qD with %qT argument",
3579 name, TREE_TYPE (value));
3580 release_tree_vector (args);
3581 return error_mark_node;
3582 }
3583 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3584 release_tree_vector (args);
3585 if (result != error_mark_node)
3586 return result;
3587
3588 error ("unable to find character literal operator %qD with %qT argument",
3589 name, TREE_TYPE (value));
3590 return error_mark_node;
3591 }
3592
3593 /* A subroutine of cp_parser_userdef_numeric_literal to
3594 create a char... template parameter pack from a string node. */
3595
3596 static tree
3597 make_char_string_pack (tree value)
3598 {
3599 tree charvec;
3600 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3601 const char *str = TREE_STRING_POINTER (value);
3602 int i, len = TREE_STRING_LENGTH (value) - 1;
3603 tree argvec = make_tree_vec (1);
3604
3605 /* Fill in CHARVEC with all of the parameters. */
3606 charvec = make_tree_vec (len);
3607 for (i = 0; i < len; ++i)
3608 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3609
3610 /* Build the argument packs. */
3611 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3612 TREE_TYPE (argpack) = char_type_node;
3613
3614 TREE_VEC_ELT (argvec, 0) = argpack;
3615
3616 return argvec;
3617 }
3618
3619 /* Parse a user-defined numeric constant. returns a call to a user-defined
3620 literal operator. */
3621
3622 static tree
3623 cp_parser_userdef_numeric_literal (cp_parser *parser)
3624 {
3625 cp_token *token = cp_lexer_consume_token (parser->lexer);
3626 tree literal = token->u.value;
3627 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3628 tree value = USERDEF_LITERAL_VALUE (literal);
3629 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3630 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3631 tree decl, result;
3632 VEC(tree,gc) *args;
3633
3634 /* Look for a literal operator taking the exact type of numeric argument
3635 as the literal value. */
3636 args = make_tree_vector ();
3637 VEC_safe_push (tree, gc, args, value);
3638 decl = lookup_literal_operator (name, args);
3639 if (decl && decl != error_mark_node)
3640 {
3641 result = finish_call_expr (decl, &args, false, true, tf_none);
3642 if (result != error_mark_node)
3643 {
3644 release_tree_vector (args);
3645 return result;
3646 }
3647 }
3648 release_tree_vector (args);
3649
3650 /* If the numeric argument didn't work, look for a raw literal
3651 operator taking a const char* argument consisting of the number
3652 in string format. */
3653 args = make_tree_vector ();
3654 VEC_safe_push (tree, gc, args, num_string);
3655 decl = lookup_literal_operator (name, args);
3656 if (decl && decl != error_mark_node)
3657 {
3658 result = finish_call_expr (decl, &args, false, true, tf_none);
3659 if (result != error_mark_node)
3660 {
3661 release_tree_vector (args);
3662 return result;
3663 }
3664 }
3665 release_tree_vector (args);
3666
3667 /* If the raw literal didn't work, look for a non-type template
3668 function with parameter pack char.... Call the function with
3669 template parameter characters representing the number. */
3670 args = make_tree_vector ();
3671 decl = lookup_literal_operator (name, args);
3672 if (decl && decl != error_mark_node)
3673 {
3674 tree tmpl_args = make_char_string_pack (num_string);
3675 decl = lookup_template_function (decl, tmpl_args);
3676 result = finish_call_expr (decl, &args, false, true, tf_none);
3677 if (result != error_mark_node)
3678 {
3679 release_tree_vector (args);
3680 return result;
3681 }
3682 }
3683 release_tree_vector (args);
3684
3685 error ("unable to find numeric literal operator %qD", name);
3686 return error_mark_node;
3687 }
3688
3689 /* Parse a user-defined string constant. Returns a call to a user-defined
3690 literal operator taking a character pointer and the length of the string
3691 as arguments. */
3692
3693 static tree
3694 cp_parser_userdef_string_literal (cp_token *token)
3695 {
3696 tree literal = token->u.value;
3697 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3698 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3699 tree value = USERDEF_LITERAL_VALUE (literal);
3700 int len = TREE_STRING_LENGTH (value)
3701 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3702 tree decl, result;
3703
3704 /* Build up a call to the user-defined operator */
3705 /* Lookup the name we got back from the id-expression. */
3706 VEC(tree,gc) *args = make_tree_vector ();
3707 VEC_safe_push (tree, gc, args, value);
3708 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3709 decl = lookup_name (name);
3710 if (!decl || decl == error_mark_node)
3711 {
3712 error ("unable to find string literal operator %qD", name);
3713 release_tree_vector (args);
3714 return error_mark_node;
3715 }
3716 result = finish_call_expr (decl, &args, false, true, tf_none);
3717 release_tree_vector (args);
3718 if (result != error_mark_node)
3719 return result;
3720
3721 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3722 name, TREE_TYPE (value), size_type_node);
3723 return error_mark_node;
3724 }
3725
3726
3727 /* Basic concepts [gram.basic] */
3728
3729 /* Parse a translation-unit.
3730
3731 translation-unit:
3732 declaration-seq [opt]
3733
3734 Returns TRUE if all went well. */
3735
3736 static bool
3737 cp_parser_translation_unit (cp_parser* parser)
3738 {
3739 /* The address of the first non-permanent object on the declarator
3740 obstack. */
3741 static void *declarator_obstack_base;
3742
3743 bool success;
3744
3745 /* Create the declarator obstack, if necessary. */
3746 if (!cp_error_declarator)
3747 {
3748 gcc_obstack_init (&declarator_obstack);
3749 /* Create the error declarator. */
3750 cp_error_declarator = make_declarator (cdk_error);
3751 /* Create the empty parameter list. */
3752 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3753 /* Remember where the base of the declarator obstack lies. */
3754 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3755 }
3756
3757 cp_parser_declaration_seq_opt (parser);
3758
3759 /* If there are no tokens left then all went well. */
3760 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3761 {
3762 /* Get rid of the token array; we don't need it any more. */
3763 cp_lexer_destroy (parser->lexer);
3764 parser->lexer = NULL;
3765
3766 /* This file might have been a context that's implicitly extern
3767 "C". If so, pop the lang context. (Only relevant for PCH.) */
3768 if (parser->implicit_extern_c)
3769 {
3770 pop_lang_context ();
3771 parser->implicit_extern_c = false;
3772 }
3773
3774 /* Finish up. */
3775 finish_translation_unit ();
3776
3777 success = true;
3778 }
3779 else
3780 {
3781 cp_parser_error (parser, "expected declaration");
3782 success = false;
3783 }
3784
3785 /* Make sure the declarator obstack was fully cleaned up. */
3786 gcc_assert (obstack_next_free (&declarator_obstack)
3787 == declarator_obstack_base);
3788
3789 /* All went well. */
3790 return success;
3791 }
3792
3793 /* Expressions [gram.expr] */
3794
3795 /* Parse a primary-expression.
3796
3797 primary-expression:
3798 literal
3799 this
3800 ( expression )
3801 id-expression
3802
3803 GNU Extensions:
3804
3805 primary-expression:
3806 ( compound-statement )
3807 __builtin_va_arg ( assignment-expression , type-id )
3808 __builtin_offsetof ( type-id , offsetof-expression )
3809
3810 C++ Extensions:
3811 __has_nothrow_assign ( type-id )
3812 __has_nothrow_constructor ( type-id )
3813 __has_nothrow_copy ( type-id )
3814 __has_trivial_assign ( type-id )
3815 __has_trivial_constructor ( type-id )
3816 __has_trivial_copy ( type-id )
3817 __has_trivial_destructor ( type-id )
3818 __has_virtual_destructor ( type-id )
3819 __is_abstract ( type-id )
3820 __is_base_of ( type-id , type-id )
3821 __is_class ( type-id )
3822 __is_convertible_to ( type-id , type-id )
3823 __is_empty ( type-id )
3824 __is_enum ( type-id )
3825 __is_final ( type-id )
3826 __is_literal_type ( type-id )
3827 __is_pod ( type-id )
3828 __is_polymorphic ( type-id )
3829 __is_std_layout ( type-id )
3830 __is_trivial ( type-id )
3831 __is_union ( type-id )
3832
3833 Objective-C++ Extension:
3834
3835 primary-expression:
3836 objc-expression
3837
3838 literal:
3839 __null
3840
3841 ADDRESS_P is true iff this expression was immediately preceded by
3842 "&" and therefore might denote a pointer-to-member. CAST_P is true
3843 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3844 true iff this expression is a template argument.
3845
3846 Returns a representation of the expression. Upon return, *IDK
3847 indicates what kind of id-expression (if any) was present. */
3848
3849 static tree
3850 cp_parser_primary_expression (cp_parser *parser,
3851 bool address_p,
3852 bool cast_p,
3853 bool template_arg_p,
3854 cp_id_kind *idk)
3855 {
3856 cp_token *token = NULL;
3857
3858 /* Assume the primary expression is not an id-expression. */
3859 *idk = CP_ID_KIND_NONE;
3860
3861 /* Peek at the next token. */
3862 token = cp_lexer_peek_token (parser->lexer);
3863 switch (token->type)
3864 {
3865 /* literal:
3866 integer-literal
3867 character-literal
3868 floating-literal
3869 string-literal
3870 boolean-literal
3871 pointer-literal
3872 user-defined-literal */
3873 case CPP_CHAR:
3874 case CPP_CHAR16:
3875 case CPP_CHAR32:
3876 case CPP_WCHAR:
3877 case CPP_NUMBER:
3878 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3879 return cp_parser_userdef_numeric_literal (parser);
3880 token = cp_lexer_consume_token (parser->lexer);
3881 if (TREE_CODE (token->u.value) == FIXED_CST)
3882 {
3883 error_at (token->location,
3884 "fixed-point types not supported in C++");
3885 return error_mark_node;
3886 }
3887 /* Floating-point literals are only allowed in an integral
3888 constant expression if they are cast to an integral or
3889 enumeration type. */
3890 if (TREE_CODE (token->u.value) == REAL_CST
3891 && parser->integral_constant_expression_p
3892 && pedantic)
3893 {
3894 /* CAST_P will be set even in invalid code like "int(2.7 +
3895 ...)". Therefore, we have to check that the next token
3896 is sure to end the cast. */
3897 if (cast_p)
3898 {
3899 cp_token *next_token;
3900
3901 next_token = cp_lexer_peek_token (parser->lexer);
3902 if (/* The comma at the end of an
3903 enumerator-definition. */
3904 next_token->type != CPP_COMMA
3905 /* The curly brace at the end of an enum-specifier. */
3906 && next_token->type != CPP_CLOSE_BRACE
3907 /* The end of a statement. */
3908 && next_token->type != CPP_SEMICOLON
3909 /* The end of the cast-expression. */
3910 && next_token->type != CPP_CLOSE_PAREN
3911 /* The end of an array bound. */
3912 && next_token->type != CPP_CLOSE_SQUARE
3913 /* The closing ">" in a template-argument-list. */
3914 && (next_token->type != CPP_GREATER
3915 || parser->greater_than_is_operator_p)
3916 /* C++0x only: A ">>" treated like two ">" tokens,
3917 in a template-argument-list. */
3918 && (next_token->type != CPP_RSHIFT
3919 || (cxx_dialect == cxx98)
3920 || parser->greater_than_is_operator_p))
3921 cast_p = false;
3922 }
3923
3924 /* If we are within a cast, then the constraint that the
3925 cast is to an integral or enumeration type will be
3926 checked at that point. If we are not within a cast, then
3927 this code is invalid. */
3928 if (!cast_p)
3929 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3930 }
3931 return token->u.value;
3932
3933 case CPP_CHAR_USERDEF:
3934 case CPP_CHAR16_USERDEF:
3935 case CPP_CHAR32_USERDEF:
3936 case CPP_WCHAR_USERDEF:
3937 return cp_parser_userdef_char_literal (parser);
3938
3939 case CPP_STRING:
3940 case CPP_STRING16:
3941 case CPP_STRING32:
3942 case CPP_WSTRING:
3943 case CPP_UTF8STRING:
3944 case CPP_STRING_USERDEF:
3945 case CPP_STRING16_USERDEF:
3946 case CPP_STRING32_USERDEF:
3947 case CPP_WSTRING_USERDEF:
3948 case CPP_UTF8STRING_USERDEF:
3949 /* ??? Should wide strings be allowed when parser->translate_strings_p
3950 is false (i.e. in attributes)? If not, we can kill the third
3951 argument to cp_parser_string_literal. */
3952 return cp_parser_string_literal (parser,
3953 parser->translate_strings_p,
3954 true);
3955
3956 case CPP_OPEN_PAREN:
3957 {
3958 tree expr;
3959 bool saved_greater_than_is_operator_p;
3960
3961 /* Consume the `('. */
3962 cp_lexer_consume_token (parser->lexer);
3963 /* Within a parenthesized expression, a `>' token is always
3964 the greater-than operator. */
3965 saved_greater_than_is_operator_p
3966 = parser->greater_than_is_operator_p;
3967 parser->greater_than_is_operator_p = true;
3968 /* If we see `( { ' then we are looking at the beginning of
3969 a GNU statement-expression. */
3970 if (cp_parser_allow_gnu_extensions_p (parser)
3971 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3972 {
3973 /* Statement-expressions are not allowed by the standard. */
3974 pedwarn (token->location, OPT_Wpedantic,
3975 "ISO C++ forbids braced-groups within expressions");
3976
3977 /* And they're not allowed outside of a function-body; you
3978 cannot, for example, write:
3979
3980 int i = ({ int j = 3; j + 1; });
3981
3982 at class or namespace scope. */
3983 if (!parser->in_function_body
3984 || parser->in_template_argument_list_p)
3985 {
3986 error_at (token->location,
3987 "statement-expressions are not allowed outside "
3988 "functions nor in template-argument lists");
3989 cp_parser_skip_to_end_of_block_or_statement (parser);
3990 expr = error_mark_node;
3991 }
3992 else
3993 {
3994 /* Start the statement-expression. */
3995 expr = begin_stmt_expr ();
3996 /* Parse the compound-statement. */
3997 cp_parser_compound_statement (parser, expr, false, false);
3998 /* Finish up. */
3999 expr = finish_stmt_expr (expr, false);
4000 }
4001 }
4002 else
4003 {
4004 /* Parse the parenthesized expression. */
4005 expr = cp_parser_expression (parser, cast_p, idk);
4006 /* Let the front end know that this expression was
4007 enclosed in parentheses. This matters in case, for
4008 example, the expression is of the form `A::B', since
4009 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4010 not. */
4011 finish_parenthesized_expr (expr);
4012 /* DR 705: Wrapping an unqualified name in parentheses
4013 suppresses arg-dependent lookup. We want to pass back
4014 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4015 (c++/37862), but none of the others. */
4016 if (*idk != CP_ID_KIND_QUALIFIED)
4017 *idk = CP_ID_KIND_NONE;
4018 }
4019 /* The `>' token might be the end of a template-id or
4020 template-parameter-list now. */
4021 parser->greater_than_is_operator_p
4022 = saved_greater_than_is_operator_p;
4023 /* Consume the `)'. */
4024 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4025 cp_parser_skip_to_end_of_statement (parser);
4026
4027 return expr;
4028 }
4029
4030 case CPP_OPEN_SQUARE:
4031 if (c_dialect_objc ())
4032 /* We have an Objective-C++ message. */
4033 return cp_parser_objc_expression (parser);
4034 {
4035 tree lam = cp_parser_lambda_expression (parser);
4036 /* Don't warn about a failed tentative parse. */
4037 if (cp_parser_error_occurred (parser))
4038 return error_mark_node;
4039 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4040 return lam;
4041 }
4042
4043 case CPP_OBJC_STRING:
4044 if (c_dialect_objc ())
4045 /* We have an Objective-C++ string literal. */
4046 return cp_parser_objc_expression (parser);
4047 cp_parser_error (parser, "expected primary-expression");
4048 return error_mark_node;
4049
4050 case CPP_KEYWORD:
4051 switch (token->keyword)
4052 {
4053 /* These two are the boolean literals. */
4054 case RID_TRUE:
4055 cp_lexer_consume_token (parser->lexer);
4056 return boolean_true_node;
4057 case RID_FALSE:
4058 cp_lexer_consume_token (parser->lexer);
4059 return boolean_false_node;
4060
4061 /* The `__null' literal. */
4062 case RID_NULL:
4063 cp_lexer_consume_token (parser->lexer);
4064 return null_node;
4065
4066 /* The `nullptr' literal. */
4067 case RID_NULLPTR:
4068 cp_lexer_consume_token (parser->lexer);
4069 return nullptr_node;
4070
4071 /* Recognize the `this' keyword. */
4072 case RID_THIS:
4073 cp_lexer_consume_token (parser->lexer);
4074 if (parser->local_variables_forbidden_p)
4075 {
4076 error_at (token->location,
4077 "%<this%> may not be used in this context");
4078 return error_mark_node;
4079 }
4080 /* Pointers cannot appear in constant-expressions. */
4081 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4082 return error_mark_node;
4083 return finish_this_expr ();
4084
4085 /* The `operator' keyword can be the beginning of an
4086 id-expression. */
4087 case RID_OPERATOR:
4088 goto id_expression;
4089
4090 case RID_FUNCTION_NAME:
4091 case RID_PRETTY_FUNCTION_NAME:
4092 case RID_C99_FUNCTION_NAME:
4093 {
4094 non_integral_constant name;
4095
4096 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4097 __func__ are the names of variables -- but they are
4098 treated specially. Therefore, they are handled here,
4099 rather than relying on the generic id-expression logic
4100 below. Grammatically, these names are id-expressions.
4101
4102 Consume the token. */
4103 token = cp_lexer_consume_token (parser->lexer);
4104
4105 switch (token->keyword)
4106 {
4107 case RID_FUNCTION_NAME:
4108 name = NIC_FUNC_NAME;
4109 break;
4110 case RID_PRETTY_FUNCTION_NAME:
4111 name = NIC_PRETTY_FUNC;
4112 break;
4113 case RID_C99_FUNCTION_NAME:
4114 name = NIC_C99_FUNC;
4115 break;
4116 default:
4117 gcc_unreachable ();
4118 }
4119
4120 if (cp_parser_non_integral_constant_expression (parser, name))
4121 return error_mark_node;
4122
4123 /* Look up the name. */
4124 return finish_fname (token->u.value);
4125 }
4126
4127 case RID_VA_ARG:
4128 {
4129 tree expression;
4130 tree type;
4131 source_location type_location;
4132
4133 /* The `__builtin_va_arg' construct is used to handle
4134 `va_arg'. Consume the `__builtin_va_arg' token. */
4135 cp_lexer_consume_token (parser->lexer);
4136 /* Look for the opening `('. */
4137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4138 /* Now, parse the assignment-expression. */
4139 expression = cp_parser_assignment_expression (parser,
4140 /*cast_p=*/false, NULL);
4141 /* Look for the `,'. */
4142 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4143 type_location = cp_lexer_peek_token (parser->lexer)->location;
4144 /* Parse the type-id. */
4145 type = cp_parser_type_id (parser);
4146 /* Look for the closing `)'. */
4147 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4148 /* Using `va_arg' in a constant-expression is not
4149 allowed. */
4150 if (cp_parser_non_integral_constant_expression (parser,
4151 NIC_VA_ARG))
4152 return error_mark_node;
4153 return build_x_va_arg (type_location, expression, type);
4154 }
4155
4156 case RID_OFFSETOF:
4157 return cp_parser_builtin_offsetof (parser);
4158
4159 case RID_HAS_NOTHROW_ASSIGN:
4160 case RID_HAS_NOTHROW_CONSTRUCTOR:
4161 case RID_HAS_NOTHROW_COPY:
4162 case RID_HAS_TRIVIAL_ASSIGN:
4163 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4164 case RID_HAS_TRIVIAL_COPY:
4165 case RID_HAS_TRIVIAL_DESTRUCTOR:
4166 case RID_HAS_VIRTUAL_DESTRUCTOR:
4167 case RID_IS_ABSTRACT:
4168 case RID_IS_BASE_OF:
4169 case RID_IS_CLASS:
4170 case RID_IS_CONVERTIBLE_TO:
4171 case RID_IS_EMPTY:
4172 case RID_IS_ENUM:
4173 case RID_IS_FINAL:
4174 case RID_IS_LITERAL_TYPE:
4175 case RID_IS_POD:
4176 case RID_IS_POLYMORPHIC:
4177 case RID_IS_STD_LAYOUT:
4178 case RID_IS_TRIVIAL:
4179 case RID_IS_UNION:
4180 return cp_parser_trait_expr (parser, token->keyword);
4181
4182 /* Objective-C++ expressions. */
4183 case RID_AT_ENCODE:
4184 case RID_AT_PROTOCOL:
4185 case RID_AT_SELECTOR:
4186 return cp_parser_objc_expression (parser);
4187
4188 case RID_TEMPLATE:
4189 if (parser->in_function_body
4190 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4191 == CPP_LESS))
4192 {
4193 error_at (token->location,
4194 "a template declaration cannot appear at block scope");
4195 cp_parser_skip_to_end_of_block_or_statement (parser);
4196 return error_mark_node;
4197 }
4198 default:
4199 cp_parser_error (parser, "expected primary-expression");
4200 return error_mark_node;
4201 }
4202
4203 /* An id-expression can start with either an identifier, a
4204 `::' as the beginning of a qualified-id, or the "operator"
4205 keyword. */
4206 case CPP_NAME:
4207 case CPP_SCOPE:
4208 case CPP_TEMPLATE_ID:
4209 case CPP_NESTED_NAME_SPECIFIER:
4210 {
4211 tree id_expression;
4212 tree decl;
4213 const char *error_msg;
4214 bool template_p;
4215 bool done;
4216 cp_token *id_expr_token;
4217
4218 id_expression:
4219 /* Parse the id-expression. */
4220 id_expression
4221 = cp_parser_id_expression (parser,
4222 /*template_keyword_p=*/false,
4223 /*check_dependency_p=*/true,
4224 &template_p,
4225 /*declarator_p=*/false,
4226 /*optional_p=*/false);
4227 if (id_expression == error_mark_node)
4228 return error_mark_node;
4229 id_expr_token = token;
4230 token = cp_lexer_peek_token (parser->lexer);
4231 done = (token->type != CPP_OPEN_SQUARE
4232 && token->type != CPP_OPEN_PAREN
4233 && token->type != CPP_DOT
4234 && token->type != CPP_DEREF
4235 && token->type != CPP_PLUS_PLUS
4236 && token->type != CPP_MINUS_MINUS);
4237 /* If we have a template-id, then no further lookup is
4238 required. If the template-id was for a template-class, we
4239 will sometimes have a TYPE_DECL at this point. */
4240 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4241 || TREE_CODE (id_expression) == TYPE_DECL)
4242 decl = id_expression;
4243 /* Look up the name. */
4244 else
4245 {
4246 tree ambiguous_decls;
4247
4248 /* If we already know that this lookup is ambiguous, then
4249 we've already issued an error message; there's no reason
4250 to check again. */
4251 if (id_expr_token->type == CPP_NAME
4252 && id_expr_token->ambiguous_p)
4253 {
4254 cp_parser_simulate_error (parser);
4255 return error_mark_node;
4256 }
4257
4258 decl = cp_parser_lookup_name (parser, id_expression,
4259 none_type,
4260 template_p,
4261 /*is_namespace=*/false,
4262 /*check_dependency=*/true,
4263 &ambiguous_decls,
4264 id_expr_token->location);
4265 /* If the lookup was ambiguous, an error will already have
4266 been issued. */
4267 if (ambiguous_decls)
4268 return error_mark_node;
4269
4270 /* In Objective-C++, we may have an Objective-C 2.0
4271 dot-syntax for classes here. */
4272 if (c_dialect_objc ()
4273 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4274 && TREE_CODE (decl) == TYPE_DECL
4275 && objc_is_class_name (decl))
4276 {
4277 tree component;
4278 cp_lexer_consume_token (parser->lexer);
4279 component = cp_parser_identifier (parser);
4280 if (component == error_mark_node)
4281 return error_mark_node;
4282
4283 return objc_build_class_component_ref (id_expression, component);
4284 }
4285
4286 /* In Objective-C++, an instance variable (ivar) may be preferred
4287 to whatever cp_parser_lookup_name() found. */
4288 decl = objc_lookup_ivar (decl, id_expression);
4289
4290 /* If name lookup gives us a SCOPE_REF, then the
4291 qualifying scope was dependent. */
4292 if (TREE_CODE (decl) == SCOPE_REF)
4293 {
4294 /* At this point, we do not know if DECL is a valid
4295 integral constant expression. We assume that it is
4296 in fact such an expression, so that code like:
4297
4298 template <int N> struct A {
4299 int a[B<N>::i];
4300 };
4301
4302 is accepted. At template-instantiation time, we
4303 will check that B<N>::i is actually a constant. */
4304 return decl;
4305 }
4306 /* Check to see if DECL is a local variable in a context
4307 where that is forbidden. */
4308 if (parser->local_variables_forbidden_p
4309 && local_variable_p (decl))
4310 {
4311 /* It might be that we only found DECL because we are
4312 trying to be generous with pre-ISO scoping rules.
4313 For example, consider:
4314
4315 int i;
4316 void g() {
4317 for (int i = 0; i < 10; ++i) {}
4318 extern void f(int j = i);
4319 }
4320
4321 Here, name look up will originally find the out
4322 of scope `i'. We need to issue a warning message,
4323 but then use the global `i'. */
4324 decl = check_for_out_of_scope_variable (decl);
4325 if (local_variable_p (decl))
4326 {
4327 error_at (id_expr_token->location,
4328 "local variable %qD may not appear in this context",
4329 decl);
4330 return error_mark_node;
4331 }
4332 }
4333 }
4334
4335 decl = (finish_id_expression
4336 (id_expression, decl, parser->scope,
4337 idk,
4338 parser->integral_constant_expression_p,
4339 parser->allow_non_integral_constant_expression_p,
4340 &parser->non_integral_constant_expression_p,
4341 template_p, done, address_p,
4342 template_arg_p,
4343 &error_msg,
4344 id_expr_token->location));
4345 if (error_msg)
4346 cp_parser_error (parser, error_msg);
4347 return decl;
4348 }
4349
4350 /* Anything else is an error. */
4351 default:
4352 cp_parser_error (parser, "expected primary-expression");
4353 return error_mark_node;
4354 }
4355 }
4356
4357 /* Parse an id-expression.
4358
4359 id-expression:
4360 unqualified-id
4361 qualified-id
4362
4363 qualified-id:
4364 :: [opt] nested-name-specifier template [opt] unqualified-id
4365 :: identifier
4366 :: operator-function-id
4367 :: template-id
4368
4369 Return a representation of the unqualified portion of the
4370 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4371 a `::' or nested-name-specifier.
4372
4373 Often, if the id-expression was a qualified-id, the caller will
4374 want to make a SCOPE_REF to represent the qualified-id. This
4375 function does not do this in order to avoid wastefully creating
4376 SCOPE_REFs when they are not required.
4377
4378 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4379 `template' keyword.
4380
4381 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4382 uninstantiated templates.
4383
4384 If *TEMPLATE_P is non-NULL, it is set to true iff the
4385 `template' keyword is used to explicitly indicate that the entity
4386 named is a template.
4387
4388 If DECLARATOR_P is true, the id-expression is appearing as part of
4389 a declarator, rather than as part of an expression. */
4390
4391 static tree
4392 cp_parser_id_expression (cp_parser *parser,
4393 bool template_keyword_p,
4394 bool check_dependency_p,
4395 bool *template_p,
4396 bool declarator_p,
4397 bool optional_p)
4398 {
4399 bool global_scope_p;
4400 bool nested_name_specifier_p;
4401
4402 /* Assume the `template' keyword was not used. */
4403 if (template_p)
4404 *template_p = template_keyword_p;
4405
4406 /* Look for the optional `::' operator. */
4407 global_scope_p
4408 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4409 != NULL_TREE);
4410 /* Look for the optional nested-name-specifier. */
4411 nested_name_specifier_p
4412 = (cp_parser_nested_name_specifier_opt (parser,
4413 /*typename_keyword_p=*/false,
4414 check_dependency_p,
4415 /*type_p=*/false,
4416 declarator_p)
4417 != NULL_TREE);
4418 /* If there is a nested-name-specifier, then we are looking at
4419 the first qualified-id production. */
4420 if (nested_name_specifier_p)
4421 {
4422 tree saved_scope;
4423 tree saved_object_scope;
4424 tree saved_qualifying_scope;
4425 tree unqualified_id;
4426 bool is_template;
4427
4428 /* See if the next token is the `template' keyword. */
4429 if (!template_p)
4430 template_p = &is_template;
4431 *template_p = cp_parser_optional_template_keyword (parser);
4432 /* Name lookup we do during the processing of the
4433 unqualified-id might obliterate SCOPE. */
4434 saved_scope = parser->scope;
4435 saved_object_scope = parser->object_scope;
4436 saved_qualifying_scope = parser->qualifying_scope;
4437 /* Process the final unqualified-id. */
4438 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4439 check_dependency_p,
4440 declarator_p,
4441 /*optional_p=*/false);
4442 /* Restore the SAVED_SCOPE for our caller. */
4443 parser->scope = saved_scope;
4444 parser->object_scope = saved_object_scope;
4445 parser->qualifying_scope = saved_qualifying_scope;
4446
4447 return unqualified_id;
4448 }
4449 /* Otherwise, if we are in global scope, then we are looking at one
4450 of the other qualified-id productions. */
4451 else if (global_scope_p)
4452 {
4453 cp_token *token;
4454 tree id;
4455
4456 /* Peek at the next token. */
4457 token = cp_lexer_peek_token (parser->lexer);
4458
4459 /* If it's an identifier, and the next token is not a "<", then
4460 we can avoid the template-id case. This is an optimization
4461 for this common case. */
4462 if (token->type == CPP_NAME
4463 && !cp_parser_nth_token_starts_template_argument_list_p
4464 (parser, 2))
4465 return cp_parser_identifier (parser);
4466
4467 cp_parser_parse_tentatively (parser);
4468 /* Try a template-id. */
4469 id = cp_parser_template_id (parser,
4470 /*template_keyword_p=*/false,
4471 /*check_dependency_p=*/true,
4472 none_type,
4473 declarator_p);
4474 /* If that worked, we're done. */
4475 if (cp_parser_parse_definitely (parser))
4476 return id;
4477
4478 /* Peek at the next token. (Changes in the token buffer may
4479 have invalidated the pointer obtained above.) */
4480 token = cp_lexer_peek_token (parser->lexer);
4481
4482 switch (token->type)
4483 {
4484 case CPP_NAME:
4485 return cp_parser_identifier (parser);
4486
4487 case CPP_KEYWORD:
4488 if (token->keyword == RID_OPERATOR)
4489 return cp_parser_operator_function_id (parser);
4490 /* Fall through. */
4491
4492 default:
4493 cp_parser_error (parser, "expected id-expression");
4494 return error_mark_node;
4495 }
4496 }
4497 else
4498 return cp_parser_unqualified_id (parser, template_keyword_p,
4499 /*check_dependency_p=*/true,
4500 declarator_p,
4501 optional_p);
4502 }
4503
4504 /* Parse an unqualified-id.
4505
4506 unqualified-id:
4507 identifier
4508 operator-function-id
4509 conversion-function-id
4510 ~ class-name
4511 template-id
4512
4513 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4514 keyword, in a construct like `A::template ...'.
4515
4516 Returns a representation of unqualified-id. For the `identifier'
4517 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4518 production a BIT_NOT_EXPR is returned; the operand of the
4519 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4520 other productions, see the documentation accompanying the
4521 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4522 names are looked up in uninstantiated templates. If DECLARATOR_P
4523 is true, the unqualified-id is appearing as part of a declarator,
4524 rather than as part of an expression. */
4525
4526 static tree
4527 cp_parser_unqualified_id (cp_parser* parser,
4528 bool template_keyword_p,
4529 bool check_dependency_p,
4530 bool declarator_p,
4531 bool optional_p)
4532 {
4533 cp_token *token;
4534
4535 /* Peek at the next token. */
4536 token = cp_lexer_peek_token (parser->lexer);
4537
4538 switch (token->type)
4539 {
4540 case CPP_NAME:
4541 {
4542 tree id;
4543
4544 /* We don't know yet whether or not this will be a
4545 template-id. */
4546 cp_parser_parse_tentatively (parser);
4547 /* Try a template-id. */
4548 id = cp_parser_template_id (parser, template_keyword_p,
4549 check_dependency_p,
4550 none_type,
4551 declarator_p);
4552 /* If it worked, we're done. */
4553 if (cp_parser_parse_definitely (parser))
4554 return id;
4555 /* Otherwise, it's an ordinary identifier. */
4556 return cp_parser_identifier (parser);
4557 }
4558
4559 case CPP_TEMPLATE_ID:
4560 return cp_parser_template_id (parser, template_keyword_p,
4561 check_dependency_p,
4562 none_type,
4563 declarator_p);
4564
4565 case CPP_COMPL:
4566 {
4567 tree type_decl;
4568 tree qualifying_scope;
4569 tree object_scope;
4570 tree scope;
4571 bool done;
4572
4573 /* Consume the `~' token. */
4574 cp_lexer_consume_token (parser->lexer);
4575 /* Parse the class-name. The standard, as written, seems to
4576 say that:
4577
4578 template <typename T> struct S { ~S (); };
4579 template <typename T> S<T>::~S() {}
4580
4581 is invalid, since `~' must be followed by a class-name, but
4582 `S<T>' is dependent, and so not known to be a class.
4583 That's not right; we need to look in uninstantiated
4584 templates. A further complication arises from:
4585
4586 template <typename T> void f(T t) {
4587 t.T::~T();
4588 }
4589
4590 Here, it is not possible to look up `T' in the scope of `T'
4591 itself. We must look in both the current scope, and the
4592 scope of the containing complete expression.
4593
4594 Yet another issue is:
4595
4596 struct S {
4597 int S;
4598 ~S();
4599 };
4600
4601 S::~S() {}
4602
4603 The standard does not seem to say that the `S' in `~S'
4604 should refer to the type `S' and not the data member
4605 `S::S'. */
4606
4607 /* DR 244 says that we look up the name after the "~" in the
4608 same scope as we looked up the qualifying name. That idea
4609 isn't fully worked out; it's more complicated than that. */
4610 scope = parser->scope;
4611 object_scope = parser->object_scope;
4612 qualifying_scope = parser->qualifying_scope;
4613
4614 /* Check for invalid scopes. */
4615 if (scope == error_mark_node)
4616 {
4617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4618 cp_lexer_consume_token (parser->lexer);
4619 return error_mark_node;
4620 }
4621 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4622 {
4623 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4624 error_at (token->location,
4625 "scope %qT before %<~%> is not a class-name",
4626 scope);
4627 cp_parser_simulate_error (parser);
4628 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4629 cp_lexer_consume_token (parser->lexer);
4630 return error_mark_node;
4631 }
4632 gcc_assert (!scope || TYPE_P (scope));
4633
4634 /* If the name is of the form "X::~X" it's OK even if X is a
4635 typedef. */
4636 token = cp_lexer_peek_token (parser->lexer);
4637 if (scope
4638 && token->type == CPP_NAME
4639 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4640 != CPP_LESS)
4641 && (token->u.value == TYPE_IDENTIFIER (scope)
4642 || (CLASS_TYPE_P (scope)
4643 && constructor_name_p (token->u.value, scope))))
4644 {
4645 cp_lexer_consume_token (parser->lexer);
4646 return build_nt (BIT_NOT_EXPR, scope);
4647 }
4648
4649 /* If there was an explicit qualification (S::~T), first look
4650 in the scope given by the qualification (i.e., S).
4651
4652 Note: in the calls to cp_parser_class_name below we pass
4653 typename_type so that lookup finds the injected-class-name
4654 rather than the constructor. */
4655 done = false;
4656 type_decl = NULL_TREE;
4657 if (scope)
4658 {
4659 cp_parser_parse_tentatively (parser);
4660 type_decl = cp_parser_class_name (parser,
4661 /*typename_keyword_p=*/false,
4662 /*template_keyword_p=*/false,
4663 typename_type,
4664 /*check_dependency=*/false,
4665 /*class_head_p=*/false,
4666 declarator_p);
4667 if (cp_parser_parse_definitely (parser))
4668 done = true;
4669 }
4670 /* In "N::S::~S", look in "N" as well. */
4671 if (!done && scope && qualifying_scope)
4672 {
4673 cp_parser_parse_tentatively (parser);
4674 parser->scope = qualifying_scope;
4675 parser->object_scope = NULL_TREE;
4676 parser->qualifying_scope = NULL_TREE;
4677 type_decl
4678 = cp_parser_class_name (parser,
4679 /*typename_keyword_p=*/false,
4680 /*template_keyword_p=*/false,
4681 typename_type,
4682 /*check_dependency=*/false,
4683 /*class_head_p=*/false,
4684 declarator_p);
4685 if (cp_parser_parse_definitely (parser))
4686 done = true;
4687 }
4688 /* In "p->S::~T", look in the scope given by "*p" as well. */
4689 else if (!done && object_scope)
4690 {
4691 cp_parser_parse_tentatively (parser);
4692 parser->scope = object_scope;
4693 parser->object_scope = NULL_TREE;
4694 parser->qualifying_scope = NULL_TREE;
4695 type_decl
4696 = cp_parser_class_name (parser,
4697 /*typename_keyword_p=*/false,
4698 /*template_keyword_p=*/false,
4699 typename_type,
4700 /*check_dependency=*/false,
4701 /*class_head_p=*/false,
4702 declarator_p);
4703 if (cp_parser_parse_definitely (parser))
4704 done = true;
4705 }
4706 /* Look in the surrounding context. */
4707 if (!done)
4708 {
4709 parser->scope = NULL_TREE;
4710 parser->object_scope = NULL_TREE;
4711 parser->qualifying_scope = NULL_TREE;
4712 if (processing_template_decl)
4713 cp_parser_parse_tentatively (parser);
4714 type_decl
4715 = cp_parser_class_name (parser,
4716 /*typename_keyword_p=*/false,
4717 /*template_keyword_p=*/false,
4718 typename_type,
4719 /*check_dependency=*/false,
4720 /*class_head_p=*/false,
4721 declarator_p);
4722 if (processing_template_decl
4723 && ! cp_parser_parse_definitely (parser))
4724 {
4725 /* We couldn't find a type with this name, so just accept
4726 it and check for a match at instantiation time. */
4727 type_decl = cp_parser_identifier (parser);
4728 if (type_decl != error_mark_node)
4729 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4730 return type_decl;
4731 }
4732 }
4733 /* If an error occurred, assume that the name of the
4734 destructor is the same as the name of the qualifying
4735 class. That allows us to keep parsing after running
4736 into ill-formed destructor names. */
4737 if (type_decl == error_mark_node && scope)
4738 return build_nt (BIT_NOT_EXPR, scope);
4739 else if (type_decl == error_mark_node)
4740 return error_mark_node;
4741
4742 /* Check that destructor name and scope match. */
4743 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4744 {
4745 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4746 error_at (token->location,
4747 "declaration of %<~%T%> as member of %qT",
4748 type_decl, scope);
4749 cp_parser_simulate_error (parser);
4750 return error_mark_node;
4751 }
4752
4753 /* [class.dtor]
4754
4755 A typedef-name that names a class shall not be used as the
4756 identifier in the declarator for a destructor declaration. */
4757 if (declarator_p
4758 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4759 && !DECL_SELF_REFERENCE_P (type_decl)
4760 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4761 error_at (token->location,
4762 "typedef-name %qD used as destructor declarator",
4763 type_decl);
4764
4765 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4766 }
4767
4768 case CPP_KEYWORD:
4769 if (token->keyword == RID_OPERATOR)
4770 {
4771 tree id;
4772
4773 /* This could be a template-id, so we try that first. */
4774 cp_parser_parse_tentatively (parser);
4775 /* Try a template-id. */
4776 id = cp_parser_template_id (parser, template_keyword_p,
4777 /*check_dependency_p=*/true,
4778 none_type,
4779 declarator_p);
4780 /* If that worked, we're done. */
4781 if (cp_parser_parse_definitely (parser))
4782 return id;
4783 /* We still don't know whether we're looking at an
4784 operator-function-id or a conversion-function-id. */
4785 cp_parser_parse_tentatively (parser);
4786 /* Try an operator-function-id. */
4787 id = cp_parser_operator_function_id (parser);
4788 /* If that didn't work, try a conversion-function-id. */
4789 if (!cp_parser_parse_definitely (parser))
4790 id = cp_parser_conversion_function_id (parser);
4791 else if (UDLIT_OPER_P (id))
4792 {
4793 /* 17.6.3.3.5 */
4794 const char *name = UDLIT_OP_SUFFIX (id);
4795 if (name[0] != '_' && !in_system_header)
4796 warning (0, "literal operator suffixes not preceded by %<_%>"
4797 " are reserved for future standardization");
4798 }
4799
4800 return id;
4801 }
4802 /* Fall through. */
4803
4804 default:
4805 if (optional_p)
4806 return NULL_TREE;
4807 cp_parser_error (parser, "expected unqualified-id");
4808 return error_mark_node;
4809 }
4810 }
4811
4812 /* Parse an (optional) nested-name-specifier.
4813
4814 nested-name-specifier: [C++98]
4815 class-or-namespace-name :: nested-name-specifier [opt]
4816 class-or-namespace-name :: template nested-name-specifier [opt]
4817
4818 nested-name-specifier: [C++0x]
4819 type-name ::
4820 namespace-name ::
4821 nested-name-specifier identifier ::
4822 nested-name-specifier template [opt] simple-template-id ::
4823
4824 PARSER->SCOPE should be set appropriately before this function is
4825 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4826 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4827 in name lookups.
4828
4829 Sets PARSER->SCOPE to the class (TYPE) or namespace
4830 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4831 it unchanged if there is no nested-name-specifier. Returns the new
4832 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4833
4834 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4835 part of a declaration and/or decl-specifier. */
4836
4837 static tree
4838 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4839 bool typename_keyword_p,
4840 bool check_dependency_p,
4841 bool type_p,
4842 bool is_declaration)
4843 {
4844 bool success = false;
4845 cp_token_position start = 0;
4846 cp_token *token;
4847
4848 /* Remember where the nested-name-specifier starts. */
4849 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4850 {
4851 start = cp_lexer_token_position (parser->lexer, false);
4852 push_deferring_access_checks (dk_deferred);
4853 }
4854
4855 while (true)
4856 {
4857 tree new_scope;
4858 tree old_scope;
4859 tree saved_qualifying_scope;
4860 bool template_keyword_p;
4861
4862 /* Spot cases that cannot be the beginning of a
4863 nested-name-specifier. */
4864 token = cp_lexer_peek_token (parser->lexer);
4865
4866 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4867 the already parsed nested-name-specifier. */
4868 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4869 {
4870 /* Grab the nested-name-specifier and continue the loop. */
4871 cp_parser_pre_parsed_nested_name_specifier (parser);
4872 /* If we originally encountered this nested-name-specifier
4873 with IS_DECLARATION set to false, we will not have
4874 resolved TYPENAME_TYPEs, so we must do so here. */
4875 if (is_declaration
4876 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4877 {
4878 new_scope = resolve_typename_type (parser->scope,
4879 /*only_current_p=*/false);
4880 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4881 parser->scope = new_scope;
4882 }
4883 success = true;
4884 continue;
4885 }
4886
4887 /* Spot cases that cannot be the beginning of a
4888 nested-name-specifier. On the second and subsequent times
4889 through the loop, we look for the `template' keyword. */
4890 if (success && token->keyword == RID_TEMPLATE)
4891 ;
4892 /* A template-id can start a nested-name-specifier. */
4893 else if (token->type == CPP_TEMPLATE_ID)
4894 ;
4895 /* DR 743: decltype can be used in a nested-name-specifier. */
4896 else if (token_is_decltype (token))
4897 ;
4898 else
4899 {
4900 /* If the next token is not an identifier, then it is
4901 definitely not a type-name or namespace-name. */
4902 if (token->type != CPP_NAME)
4903 break;
4904 /* If the following token is neither a `<' (to begin a
4905 template-id), nor a `::', then we are not looking at a
4906 nested-name-specifier. */
4907 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4908
4909 if (token->type == CPP_COLON
4910 && parser->colon_corrects_to_scope_p
4911 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4912 {
4913 error_at (token->location,
4914 "found %<:%> in nested-name-specifier, expected %<::%>");
4915 token->type = CPP_SCOPE;
4916 }
4917
4918 if (token->type != CPP_SCOPE
4919 && !cp_parser_nth_token_starts_template_argument_list_p
4920 (parser, 2))
4921 break;
4922 }
4923
4924 /* The nested-name-specifier is optional, so we parse
4925 tentatively. */
4926 cp_parser_parse_tentatively (parser);
4927
4928 /* Look for the optional `template' keyword, if this isn't the
4929 first time through the loop. */
4930 if (success)
4931 template_keyword_p = cp_parser_optional_template_keyword (parser);
4932 else
4933 template_keyword_p = false;
4934
4935 /* Save the old scope since the name lookup we are about to do
4936 might destroy it. */
4937 old_scope = parser->scope;
4938 saved_qualifying_scope = parser->qualifying_scope;
4939 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4940 look up names in "X<T>::I" in order to determine that "Y" is
4941 a template. So, if we have a typename at this point, we make
4942 an effort to look through it. */
4943 if (is_declaration
4944 && !typename_keyword_p
4945 && parser->scope
4946 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4947 parser->scope = resolve_typename_type (parser->scope,
4948 /*only_current_p=*/false);
4949 /* Parse the qualifying entity. */
4950 new_scope
4951 = cp_parser_qualifying_entity (parser,
4952 typename_keyword_p,
4953 template_keyword_p,
4954 check_dependency_p,
4955 type_p,
4956 is_declaration);
4957 /* Look for the `::' token. */
4958 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4959
4960 /* If we found what we wanted, we keep going; otherwise, we're
4961 done. */
4962 if (!cp_parser_parse_definitely (parser))
4963 {
4964 bool error_p = false;
4965
4966 /* Restore the OLD_SCOPE since it was valid before the
4967 failed attempt at finding the last
4968 class-or-namespace-name. */
4969 parser->scope = old_scope;
4970 parser->qualifying_scope = saved_qualifying_scope;
4971
4972 /* If the next token is a decltype, and the one after that is a
4973 `::', then the decltype has failed to resolve to a class or
4974 enumeration type. Give this error even when parsing
4975 tentatively since it can't possibly be valid--and we're going
4976 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4977 won't get another chance.*/
4978 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4979 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4980 == CPP_SCOPE))
4981 {
4982 token = cp_lexer_consume_token (parser->lexer);
4983 error_at (token->location, "decltype evaluates to %qT, "
4984 "which is not a class or enumeration type",
4985 token->u.value);
4986 parser->scope = error_mark_node;
4987 error_p = true;
4988 /* As below. */
4989 success = true;
4990 cp_lexer_consume_token (parser->lexer);
4991 }
4992
4993 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4994 break;
4995 /* If the next token is an identifier, and the one after
4996 that is a `::', then any valid interpretation would have
4997 found a class-or-namespace-name. */
4998 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4999 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5000 == CPP_SCOPE)
5001 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5002 != CPP_COMPL))
5003 {
5004 token = cp_lexer_consume_token (parser->lexer);
5005 if (!error_p)
5006 {
5007 if (!token->ambiguous_p)
5008 {
5009 tree decl;
5010 tree ambiguous_decls;
5011
5012 decl = cp_parser_lookup_name (parser, token->u.value,
5013 none_type,
5014 /*is_template=*/false,
5015 /*is_namespace=*/false,
5016 /*check_dependency=*/true,
5017 &ambiguous_decls,
5018 token->location);
5019 if (TREE_CODE (decl) == TEMPLATE_DECL)
5020 error_at (token->location,
5021 "%qD used without template parameters",
5022 decl);
5023 else if (ambiguous_decls)
5024 {
5025 error_at (token->location,
5026 "reference to %qD is ambiguous",
5027 token->u.value);
5028 print_candidates (ambiguous_decls);
5029 decl = error_mark_node;
5030 }
5031 else
5032 {
5033 if (cxx_dialect != cxx98)
5034 cp_parser_name_lookup_error
5035 (parser, token->u.value, decl, NLE_NOT_CXX98,
5036 token->location);
5037 else
5038 cp_parser_name_lookup_error
5039 (parser, token->u.value, decl, NLE_CXX98,
5040 token->location);
5041 }
5042 }
5043 parser->scope = error_mark_node;
5044 error_p = true;
5045 /* Treat this as a successful nested-name-specifier
5046 due to:
5047
5048 [basic.lookup.qual]
5049
5050 If the name found is not a class-name (clause
5051 _class_) or namespace-name (_namespace.def_), the
5052 program is ill-formed. */
5053 success = true;
5054 }
5055 cp_lexer_consume_token (parser->lexer);
5056 }
5057 break;
5058 }
5059 /* We've found one valid nested-name-specifier. */
5060 success = true;
5061 /* Name lookup always gives us a DECL. */
5062 if (TREE_CODE (new_scope) == TYPE_DECL)
5063 new_scope = TREE_TYPE (new_scope);
5064 /* Uses of "template" must be followed by actual templates. */
5065 if (template_keyword_p
5066 && !(CLASS_TYPE_P (new_scope)
5067 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5068 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5069 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5070 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5071 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5072 == TEMPLATE_ID_EXPR)))
5073 permerror (input_location, TYPE_P (new_scope)
5074 ? G_("%qT is not a template")
5075 : G_("%qD is not a template"),
5076 new_scope);
5077 /* If it is a class scope, try to complete it; we are about to
5078 be looking up names inside the class. */
5079 if (TYPE_P (new_scope)
5080 /* Since checking types for dependency can be expensive,
5081 avoid doing it if the type is already complete. */
5082 && !COMPLETE_TYPE_P (new_scope)
5083 /* Do not try to complete dependent types. */
5084 && !dependent_type_p (new_scope))
5085 {
5086 new_scope = complete_type (new_scope);
5087 /* If it is a typedef to current class, use the current
5088 class instead, as the typedef won't have any names inside
5089 it yet. */
5090 if (!COMPLETE_TYPE_P (new_scope)
5091 && currently_open_class (new_scope))
5092 new_scope = TYPE_MAIN_VARIANT (new_scope);
5093 }
5094 /* Make sure we look in the right scope the next time through
5095 the loop. */
5096 parser->scope = new_scope;
5097 }
5098
5099 /* If parsing tentatively, replace the sequence of tokens that makes
5100 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5101 token. That way, should we re-parse the token stream, we will
5102 not have to repeat the effort required to do the parse, nor will
5103 we issue duplicate error messages. */
5104 if (success && start)
5105 {
5106 cp_token *token;
5107
5108 token = cp_lexer_token_at (parser->lexer, start);
5109 /* Reset the contents of the START token. */
5110 token->type = CPP_NESTED_NAME_SPECIFIER;
5111 /* Retrieve any deferred checks. Do not pop this access checks yet
5112 so the memory will not be reclaimed during token replacing below. */
5113 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5114 token->u.tree_check_value->value = parser->scope;
5115 token->u.tree_check_value->checks = get_deferred_access_checks ();
5116 token->u.tree_check_value->qualifying_scope =
5117 parser->qualifying_scope;
5118 token->keyword = RID_MAX;
5119
5120 /* Purge all subsequent tokens. */
5121 cp_lexer_purge_tokens_after (parser->lexer, start);
5122 }
5123
5124 if (start)
5125 pop_to_parent_deferring_access_checks ();
5126
5127 return success ? parser->scope : NULL_TREE;
5128 }
5129
5130 /* Parse a nested-name-specifier. See
5131 cp_parser_nested_name_specifier_opt for details. This function
5132 behaves identically, except that it will an issue an error if no
5133 nested-name-specifier is present. */
5134
5135 static tree
5136 cp_parser_nested_name_specifier (cp_parser *parser,
5137 bool typename_keyword_p,
5138 bool check_dependency_p,
5139 bool type_p,
5140 bool is_declaration)
5141 {
5142 tree scope;
5143
5144 /* Look for the nested-name-specifier. */
5145 scope = cp_parser_nested_name_specifier_opt (parser,
5146 typename_keyword_p,
5147 check_dependency_p,
5148 type_p,
5149 is_declaration);
5150 /* If it was not present, issue an error message. */
5151 if (!scope)
5152 {
5153 cp_parser_error (parser, "expected nested-name-specifier");
5154 parser->scope = NULL_TREE;
5155 }
5156
5157 return scope;
5158 }
5159
5160 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5161 this is either a class-name or a namespace-name (which corresponds
5162 to the class-or-namespace-name production in the grammar). For
5163 C++0x, it can also be a type-name that refers to an enumeration
5164 type or a simple-template-id.
5165
5166 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5167 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5168 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5169 TYPE_P is TRUE iff the next name should be taken as a class-name,
5170 even the same name is declared to be another entity in the same
5171 scope.
5172
5173 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5174 specified by the class-or-namespace-name. If neither is found the
5175 ERROR_MARK_NODE is returned. */
5176
5177 static tree
5178 cp_parser_qualifying_entity (cp_parser *parser,
5179 bool typename_keyword_p,
5180 bool template_keyword_p,
5181 bool check_dependency_p,
5182 bool type_p,
5183 bool is_declaration)
5184 {
5185 tree saved_scope;
5186 tree saved_qualifying_scope;
5187 tree saved_object_scope;
5188 tree scope;
5189 bool only_class_p;
5190 bool successful_parse_p;
5191
5192 /* DR 743: decltype can appear in a nested-name-specifier. */
5193 if (cp_lexer_next_token_is_decltype (parser->lexer))
5194 {
5195 scope = cp_parser_decltype (parser);
5196 if (TREE_CODE (scope) != ENUMERAL_TYPE
5197 && !MAYBE_CLASS_TYPE_P (scope))
5198 {
5199 cp_parser_simulate_error (parser);
5200 return error_mark_node;
5201 }
5202 if (TYPE_NAME (scope))
5203 scope = TYPE_NAME (scope);
5204 return scope;
5205 }
5206
5207 /* Before we try to parse the class-name, we must save away the
5208 current PARSER->SCOPE since cp_parser_class_name will destroy
5209 it. */
5210 saved_scope = parser->scope;
5211 saved_qualifying_scope = parser->qualifying_scope;
5212 saved_object_scope = parser->object_scope;
5213 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5214 there is no need to look for a namespace-name. */
5215 only_class_p = template_keyword_p
5216 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5217 if (!only_class_p)
5218 cp_parser_parse_tentatively (parser);
5219 scope = cp_parser_class_name (parser,
5220 typename_keyword_p,
5221 template_keyword_p,
5222 type_p ? class_type : none_type,
5223 check_dependency_p,
5224 /*class_head_p=*/false,
5225 is_declaration);
5226 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5227 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5228 if (!only_class_p
5229 && cxx_dialect != cxx98
5230 && !successful_parse_p)
5231 {
5232 /* Restore the saved scope. */
5233 parser->scope = saved_scope;
5234 parser->qualifying_scope = saved_qualifying_scope;
5235 parser->object_scope = saved_object_scope;
5236
5237 /* Parse tentatively. */
5238 cp_parser_parse_tentatively (parser);
5239
5240 /* Parse a type-name */
5241 scope = cp_parser_type_name (parser);
5242
5243 /* "If the name found does not designate a namespace or a class,
5244 enumeration, or dependent type, the program is ill-formed."
5245
5246 We cover classes and dependent types above and namespaces below,
5247 so this code is only looking for enums. */
5248 if (!scope || TREE_CODE (scope) != TYPE_DECL
5249 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5250 cp_parser_simulate_error (parser);
5251
5252 successful_parse_p = cp_parser_parse_definitely (parser);
5253 }
5254 /* If that didn't work, try for a namespace-name. */
5255 if (!only_class_p && !successful_parse_p)
5256 {
5257 /* Restore the saved scope. */
5258 parser->scope = saved_scope;
5259 parser->qualifying_scope = saved_qualifying_scope;
5260 parser->object_scope = saved_object_scope;
5261 /* If we are not looking at an identifier followed by the scope
5262 resolution operator, then this is not part of a
5263 nested-name-specifier. (Note that this function is only used
5264 to parse the components of a nested-name-specifier.) */
5265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5266 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5267 return error_mark_node;
5268 scope = cp_parser_namespace_name (parser);
5269 }
5270
5271 return scope;
5272 }
5273
5274 /* Parse a postfix-expression.
5275
5276 postfix-expression:
5277 primary-expression
5278 postfix-expression [ expression ]
5279 postfix-expression ( expression-list [opt] )
5280 simple-type-specifier ( expression-list [opt] )
5281 typename :: [opt] nested-name-specifier identifier
5282 ( expression-list [opt] )
5283 typename :: [opt] nested-name-specifier template [opt] template-id
5284 ( expression-list [opt] )
5285 postfix-expression . template [opt] id-expression
5286 postfix-expression -> template [opt] id-expression
5287 postfix-expression . pseudo-destructor-name
5288 postfix-expression -> pseudo-destructor-name
5289 postfix-expression ++
5290 postfix-expression --
5291 dynamic_cast < type-id > ( expression )
5292 static_cast < type-id > ( expression )
5293 reinterpret_cast < type-id > ( expression )
5294 const_cast < type-id > ( expression )
5295 typeid ( expression )
5296 typeid ( type-id )
5297
5298 GNU Extension:
5299
5300 postfix-expression:
5301 ( type-id ) { initializer-list , [opt] }
5302
5303 This extension is a GNU version of the C99 compound-literal
5304 construct. (The C99 grammar uses `type-name' instead of `type-id',
5305 but they are essentially the same concept.)
5306
5307 If ADDRESS_P is true, the postfix expression is the operand of the
5308 `&' operator. CAST_P is true if this expression is the target of a
5309 cast.
5310
5311 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5312 class member access expressions [expr.ref].
5313
5314 Returns a representation of the expression. */
5315
5316 static tree
5317 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5318 bool member_access_only_p,
5319 cp_id_kind * pidk_return)
5320 {
5321 cp_token *token;
5322 enum rid keyword;
5323 cp_id_kind idk = CP_ID_KIND_NONE;
5324 tree postfix_expression = NULL_TREE;
5325 bool is_member_access = false;
5326
5327 /* Peek at the next token. */
5328 token = cp_lexer_peek_token (parser->lexer);
5329 /* Some of the productions are determined by keywords. */
5330 keyword = token->keyword;
5331 switch (keyword)
5332 {
5333 case RID_DYNCAST:
5334 case RID_STATCAST:
5335 case RID_REINTCAST:
5336 case RID_CONSTCAST:
5337 {
5338 tree type;
5339 tree expression;
5340 const char *saved_message;
5341
5342 /* All of these can be handled in the same way from the point
5343 of view of parsing. Begin by consuming the token
5344 identifying the cast. */
5345 cp_lexer_consume_token (parser->lexer);
5346
5347 /* New types cannot be defined in the cast. */
5348 saved_message = parser->type_definition_forbidden_message;
5349 parser->type_definition_forbidden_message
5350 = G_("types may not be defined in casts");
5351
5352 /* Look for the opening `<'. */
5353 cp_parser_require (parser, CPP_LESS, RT_LESS);
5354 /* Parse the type to which we are casting. */
5355 type = cp_parser_type_id (parser);
5356 /* Look for the closing `>'. */
5357 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5358 /* Restore the old message. */
5359 parser->type_definition_forbidden_message = saved_message;
5360
5361 /* And the expression which is being cast. */
5362 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5363 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5364 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5365
5366 /* Only type conversions to integral or enumeration types
5367 can be used in constant-expressions. */
5368 if (!cast_valid_in_integral_constant_expression_p (type)
5369 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5370 return error_mark_node;
5371
5372 switch (keyword)
5373 {
5374 case RID_DYNCAST:
5375 postfix_expression
5376 = build_dynamic_cast (type, expression, tf_warning_or_error);
5377 break;
5378 case RID_STATCAST:
5379 postfix_expression
5380 = build_static_cast (type, expression, tf_warning_or_error);
5381 break;
5382 case RID_REINTCAST:
5383 postfix_expression
5384 = build_reinterpret_cast (type, expression,
5385 tf_warning_or_error);
5386 break;
5387 case RID_CONSTCAST:
5388 postfix_expression
5389 = build_const_cast (type, expression, tf_warning_or_error);
5390 break;
5391 default:
5392 gcc_unreachable ();
5393 }
5394 }
5395 break;
5396
5397 case RID_TYPEID:
5398 {
5399 tree type;
5400 const char *saved_message;
5401 bool saved_in_type_id_in_expr_p;
5402
5403 /* Consume the `typeid' token. */
5404 cp_lexer_consume_token (parser->lexer);
5405 /* Look for the `(' token. */
5406 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5407 /* Types cannot be defined in a `typeid' expression. */
5408 saved_message = parser->type_definition_forbidden_message;
5409 parser->type_definition_forbidden_message
5410 = G_("types may not be defined in a %<typeid%> expression");
5411 /* We can't be sure yet whether we're looking at a type-id or an
5412 expression. */
5413 cp_parser_parse_tentatively (parser);
5414 /* Try a type-id first. */
5415 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5416 parser->in_type_id_in_expr_p = true;
5417 type = cp_parser_type_id (parser);
5418 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5419 /* Look for the `)' token. Otherwise, we can't be sure that
5420 we're not looking at an expression: consider `typeid (int
5421 (3))', for example. */
5422 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5423 /* If all went well, simply lookup the type-id. */
5424 if (cp_parser_parse_definitely (parser))
5425 postfix_expression = get_typeid (type);
5426 /* Otherwise, fall back to the expression variant. */
5427 else
5428 {
5429 tree expression;
5430
5431 /* Look for an expression. */
5432 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5433 /* Compute its typeid. */
5434 postfix_expression = build_typeid (expression);
5435 /* Look for the `)' token. */
5436 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5437 }
5438 /* Restore the saved message. */
5439 parser->type_definition_forbidden_message = saved_message;
5440 /* `typeid' may not appear in an integral constant expression. */
5441 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5442 return error_mark_node;
5443 }
5444 break;
5445
5446 case RID_TYPENAME:
5447 {
5448 tree type;
5449 /* The syntax permitted here is the same permitted for an
5450 elaborated-type-specifier. */
5451 type = cp_parser_elaborated_type_specifier (parser,
5452 /*is_friend=*/false,
5453 /*is_declaration=*/false);
5454 postfix_expression = cp_parser_functional_cast (parser, type);
5455 }
5456 break;
5457
5458 case RID_BUILTIN_SHUFFLE:
5459 {
5460 VEC(tree,gc)* vec;
5461 unsigned int i;
5462 tree p;
5463 location_t loc = token->location;
5464
5465 cp_lexer_consume_token (parser->lexer);
5466 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5467 /*cast_p=*/false, /*allow_expansion_p=*/true,
5468 /*non_constant_p=*/NULL);
5469 if (vec == NULL)
5470 return error_mark_node;
5471
5472 FOR_EACH_VEC_ELT (tree, vec, i, p)
5473 mark_exp_read (p);
5474
5475 if (VEC_length (tree, vec) == 2)
5476 return
5477 c_build_vec_perm_expr
5478 (loc, VEC_index (tree, vec, 0),
5479 NULL_TREE, VEC_index (tree, vec, 1));
5480
5481 else if (VEC_length (tree, vec) == 3)
5482 return
5483 c_build_vec_perm_expr
5484 (loc, VEC_index (tree, vec, 0),
5485 VEC_index (tree, vec, 1),
5486 VEC_index (tree, vec, 2));
5487 else
5488 {
5489 error_at (loc, "wrong number of arguments to "
5490 "%<__builtin_shuffle%>");
5491 return error_mark_node;
5492 }
5493 break;
5494 }
5495
5496 default:
5497 {
5498 tree type;
5499
5500 /* If the next thing is a simple-type-specifier, we may be
5501 looking at a functional cast. We could also be looking at
5502 an id-expression. So, we try the functional cast, and if
5503 that doesn't work we fall back to the primary-expression. */
5504 cp_parser_parse_tentatively (parser);
5505 /* Look for the simple-type-specifier. */
5506 type = cp_parser_simple_type_specifier (parser,
5507 /*decl_specs=*/NULL,
5508 CP_PARSER_FLAGS_NONE);
5509 /* Parse the cast itself. */
5510 if (!cp_parser_error_occurred (parser))
5511 postfix_expression
5512 = cp_parser_functional_cast (parser, type);
5513 /* If that worked, we're done. */
5514 if (cp_parser_parse_definitely (parser))
5515 break;
5516
5517 /* If the functional-cast didn't work out, try a
5518 compound-literal. */
5519 if (cp_parser_allow_gnu_extensions_p (parser)
5520 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5521 {
5522 VEC(constructor_elt,gc) *initializer_list = NULL;
5523 bool saved_in_type_id_in_expr_p;
5524
5525 cp_parser_parse_tentatively (parser);
5526 /* Consume the `('. */
5527 cp_lexer_consume_token (parser->lexer);
5528 /* Parse the type. */
5529 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5530 parser->in_type_id_in_expr_p = true;
5531 type = cp_parser_type_id (parser);
5532 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5533 /* Look for the `)'. */
5534 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5535 /* Look for the `{'. */
5536 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5537 /* If things aren't going well, there's no need to
5538 keep going. */
5539 if (!cp_parser_error_occurred (parser))
5540 {
5541 bool non_constant_p;
5542 /* Parse the initializer-list. */
5543 initializer_list
5544 = cp_parser_initializer_list (parser, &non_constant_p);
5545 /* Allow a trailing `,'. */
5546 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5547 cp_lexer_consume_token (parser->lexer);
5548 /* Look for the final `}'. */
5549 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5550 }
5551 /* If that worked, we're definitely looking at a
5552 compound-literal expression. */
5553 if (cp_parser_parse_definitely (parser))
5554 {
5555 /* Warn the user that a compound literal is not
5556 allowed in standard C++. */
5557 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5558 /* For simplicity, we disallow compound literals in
5559 constant-expressions. We could
5560 allow compound literals of integer type, whose
5561 initializer was a constant, in constant
5562 expressions. Permitting that usage, as a further
5563 extension, would not change the meaning of any
5564 currently accepted programs. (Of course, as
5565 compound literals are not part of ISO C++, the
5566 standard has nothing to say.) */
5567 if (cp_parser_non_integral_constant_expression (parser,
5568 NIC_NCC))
5569 {
5570 postfix_expression = error_mark_node;
5571 break;
5572 }
5573 /* Form the representation of the compound-literal. */
5574 postfix_expression
5575 = (finish_compound_literal
5576 (type, build_constructor (init_list_type_node,
5577 initializer_list),
5578 tf_warning_or_error));
5579 break;
5580 }
5581 }
5582
5583 /* It must be a primary-expression. */
5584 postfix_expression
5585 = cp_parser_primary_expression (parser, address_p, cast_p,
5586 /*template_arg_p=*/false,
5587 &idk);
5588 }
5589 break;
5590 }
5591
5592 /* Keep looping until the postfix-expression is complete. */
5593 while (true)
5594 {
5595 if (idk == CP_ID_KIND_UNQUALIFIED
5596 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5597 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5598 /* It is not a Koenig lookup function call. */
5599 postfix_expression
5600 = unqualified_name_lookup_error (postfix_expression);
5601
5602 /* Peek at the next token. */
5603 token = cp_lexer_peek_token (parser->lexer);
5604
5605 switch (token->type)
5606 {
5607 case CPP_OPEN_SQUARE:
5608 postfix_expression
5609 = cp_parser_postfix_open_square_expression (parser,
5610 postfix_expression,
5611 false);
5612 idk = CP_ID_KIND_NONE;
5613 is_member_access = false;
5614 break;
5615
5616 case CPP_OPEN_PAREN:
5617 /* postfix-expression ( expression-list [opt] ) */
5618 {
5619 bool koenig_p;
5620 bool is_builtin_constant_p;
5621 bool saved_integral_constant_expression_p = false;
5622 bool saved_non_integral_constant_expression_p = false;
5623 VEC(tree,gc) *args;
5624
5625 is_member_access = false;
5626
5627 is_builtin_constant_p
5628 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5629 if (is_builtin_constant_p)
5630 {
5631 /* The whole point of __builtin_constant_p is to allow
5632 non-constant expressions to appear as arguments. */
5633 saved_integral_constant_expression_p
5634 = parser->integral_constant_expression_p;
5635 saved_non_integral_constant_expression_p
5636 = parser->non_integral_constant_expression_p;
5637 parser->integral_constant_expression_p = false;
5638 }
5639 args = (cp_parser_parenthesized_expression_list
5640 (parser, non_attr,
5641 /*cast_p=*/false, /*allow_expansion_p=*/true,
5642 /*non_constant_p=*/NULL));
5643 if (is_builtin_constant_p)
5644 {
5645 parser->integral_constant_expression_p
5646 = saved_integral_constant_expression_p;
5647 parser->non_integral_constant_expression_p
5648 = saved_non_integral_constant_expression_p;
5649 }
5650
5651 if (args == NULL)
5652 {
5653 postfix_expression = error_mark_node;
5654 break;
5655 }
5656
5657 /* Function calls are not permitted in
5658 constant-expressions. */
5659 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5660 && cp_parser_non_integral_constant_expression (parser,
5661 NIC_FUNC_CALL))
5662 {
5663 postfix_expression = error_mark_node;
5664 release_tree_vector (args);
5665 break;
5666 }
5667
5668 koenig_p = false;
5669 if (idk == CP_ID_KIND_UNQUALIFIED
5670 || idk == CP_ID_KIND_TEMPLATE_ID)
5671 {
5672 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5673 {
5674 if (!VEC_empty (tree, args))
5675 {
5676 koenig_p = true;
5677 if (!any_type_dependent_arguments_p (args))
5678 postfix_expression
5679 = perform_koenig_lookup (postfix_expression, args,
5680 /*include_std=*/false,
5681 tf_warning_or_error);
5682 }
5683 else
5684 postfix_expression
5685 = unqualified_fn_lookup_error (postfix_expression);
5686 }
5687 /* We do not perform argument-dependent lookup if
5688 normal lookup finds a non-function, in accordance
5689 with the expected resolution of DR 218. */
5690 else if (!VEC_empty (tree, args)
5691 && is_overloaded_fn (postfix_expression))
5692 {
5693 tree fn = get_first_fn (postfix_expression);
5694 fn = STRIP_TEMPLATE (fn);
5695
5696 /* Do not do argument dependent lookup if regular
5697 lookup finds a member function or a block-scope
5698 function declaration. [basic.lookup.argdep]/3 */
5699 if (!DECL_FUNCTION_MEMBER_P (fn)
5700 && !DECL_LOCAL_FUNCTION_P (fn))
5701 {
5702 koenig_p = true;
5703 if (!any_type_dependent_arguments_p (args))
5704 postfix_expression
5705 = perform_koenig_lookup (postfix_expression, args,
5706 /*include_std=*/false,
5707 tf_warning_or_error);
5708 }
5709 }
5710 }
5711
5712 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5713 {
5714 tree instance = TREE_OPERAND (postfix_expression, 0);
5715 tree fn = TREE_OPERAND (postfix_expression, 1);
5716
5717 if (processing_template_decl
5718 && (type_dependent_expression_p (instance)
5719 || (!BASELINK_P (fn)
5720 && TREE_CODE (fn) != FIELD_DECL)
5721 || type_dependent_expression_p (fn)
5722 || any_type_dependent_arguments_p (args)))
5723 {
5724 postfix_expression
5725 = build_nt_call_vec (postfix_expression, args);
5726 release_tree_vector (args);
5727 break;
5728 }
5729
5730 if (BASELINK_P (fn))
5731 {
5732 postfix_expression
5733 = (build_new_method_call
5734 (instance, fn, &args, NULL_TREE,
5735 (idk == CP_ID_KIND_QUALIFIED
5736 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5737 : LOOKUP_NORMAL),
5738 /*fn_p=*/NULL,
5739 tf_warning_or_error));
5740 }
5741 else
5742 postfix_expression
5743 = finish_call_expr (postfix_expression, &args,
5744 /*disallow_virtual=*/false,
5745 /*koenig_p=*/false,
5746 tf_warning_or_error);
5747 }
5748 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5749 || TREE_CODE (postfix_expression) == MEMBER_REF
5750 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5751 postfix_expression = (build_offset_ref_call_from_tree
5752 (postfix_expression, &args));
5753 else if (idk == CP_ID_KIND_QUALIFIED)
5754 /* A call to a static class member, or a namespace-scope
5755 function. */
5756 postfix_expression
5757 = finish_call_expr (postfix_expression, &args,
5758 /*disallow_virtual=*/true,
5759 koenig_p,
5760 tf_warning_or_error);
5761 else
5762 /* All other function calls. */
5763 postfix_expression
5764 = finish_call_expr (postfix_expression, &args,
5765 /*disallow_virtual=*/false,
5766 koenig_p,
5767 tf_warning_or_error);
5768
5769 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5770 idk = CP_ID_KIND_NONE;
5771
5772 release_tree_vector (args);
5773 }
5774 break;
5775
5776 case CPP_DOT:
5777 case CPP_DEREF:
5778 /* postfix-expression . template [opt] id-expression
5779 postfix-expression . pseudo-destructor-name
5780 postfix-expression -> template [opt] id-expression
5781 postfix-expression -> pseudo-destructor-name */
5782
5783 /* Consume the `.' or `->' operator. */
5784 cp_lexer_consume_token (parser->lexer);
5785
5786 postfix_expression
5787 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5788 postfix_expression,
5789 false, &idk,
5790 token->location);
5791
5792 is_member_access = true;
5793 break;
5794
5795 case CPP_PLUS_PLUS:
5796 /* postfix-expression ++ */
5797 /* Consume the `++' token. */
5798 cp_lexer_consume_token (parser->lexer);
5799 /* Generate a representation for the complete expression. */
5800 postfix_expression
5801 = finish_increment_expr (postfix_expression,
5802 POSTINCREMENT_EXPR);
5803 /* Increments may not appear in constant-expressions. */
5804 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5805 postfix_expression = error_mark_node;
5806 idk = CP_ID_KIND_NONE;
5807 is_member_access = false;
5808 break;
5809
5810 case CPP_MINUS_MINUS:
5811 /* postfix-expression -- */
5812 /* Consume the `--' token. */
5813 cp_lexer_consume_token (parser->lexer);
5814 /* Generate a representation for the complete expression. */
5815 postfix_expression
5816 = finish_increment_expr (postfix_expression,
5817 POSTDECREMENT_EXPR);
5818 /* Decrements may not appear in constant-expressions. */
5819 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5820 postfix_expression = error_mark_node;
5821 idk = CP_ID_KIND_NONE;
5822 is_member_access = false;
5823 break;
5824
5825 default:
5826 if (pidk_return != NULL)
5827 * pidk_return = idk;
5828 if (member_access_only_p)
5829 return is_member_access? postfix_expression : error_mark_node;
5830 else
5831 return postfix_expression;
5832 }
5833 }
5834
5835 /* We should never get here. */
5836 gcc_unreachable ();
5837 return error_mark_node;
5838 }
5839
5840 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5841 by cp_parser_builtin_offsetof. We're looking for
5842
5843 postfix-expression [ expression ]
5844 postfix-expression [ braced-init-list ] (C++11)
5845
5846 FOR_OFFSETOF is set if we're being called in that context, which
5847 changes how we deal with integer constant expressions. */
5848
5849 static tree
5850 cp_parser_postfix_open_square_expression (cp_parser *parser,
5851 tree postfix_expression,
5852 bool for_offsetof)
5853 {
5854 tree index;
5855 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5856
5857 /* Consume the `[' token. */
5858 cp_lexer_consume_token (parser->lexer);
5859
5860 /* Parse the index expression. */
5861 /* ??? For offsetof, there is a question of what to allow here. If
5862 offsetof is not being used in an integral constant expression context,
5863 then we *could* get the right answer by computing the value at runtime.
5864 If we are in an integral constant expression context, then we might
5865 could accept any constant expression; hard to say without analysis.
5866 Rather than open the barn door too wide right away, allow only integer
5867 constant expressions here. */
5868 if (for_offsetof)
5869 index = cp_parser_constant_expression (parser, false, NULL);
5870 else
5871 {
5872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5873 {
5874 bool expr_nonconst_p;
5875 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5876 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5877 }
5878 else
5879 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5880 }
5881
5882 /* Look for the closing `]'. */
5883 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5884
5885 /* Build the ARRAY_REF. */
5886 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5887
5888 /* When not doing offsetof, array references are not permitted in
5889 constant-expressions. */
5890 if (!for_offsetof
5891 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5892 postfix_expression = error_mark_node;
5893
5894 return postfix_expression;
5895 }
5896
5897 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5898 by cp_parser_builtin_offsetof. We're looking for
5899
5900 postfix-expression . template [opt] id-expression
5901 postfix-expression . pseudo-destructor-name
5902 postfix-expression -> template [opt] id-expression
5903 postfix-expression -> pseudo-destructor-name
5904
5905 FOR_OFFSETOF is set if we're being called in that context. That sorta
5906 limits what of the above we'll actually accept, but nevermind.
5907 TOKEN_TYPE is the "." or "->" token, which will already have been
5908 removed from the stream. */
5909
5910 static tree
5911 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5912 enum cpp_ttype token_type,
5913 tree postfix_expression,
5914 bool for_offsetof, cp_id_kind *idk,
5915 location_t location)
5916 {
5917 tree name;
5918 bool dependent_p;
5919 bool pseudo_destructor_p;
5920 tree scope = NULL_TREE;
5921
5922 /* If this is a `->' operator, dereference the pointer. */
5923 if (token_type == CPP_DEREF)
5924 postfix_expression = build_x_arrow (location, postfix_expression,
5925 tf_warning_or_error);
5926 /* Check to see whether or not the expression is type-dependent. */
5927 dependent_p = type_dependent_expression_p (postfix_expression);
5928 /* The identifier following the `->' or `.' is not qualified. */
5929 parser->scope = NULL_TREE;
5930 parser->qualifying_scope = NULL_TREE;
5931 parser->object_scope = NULL_TREE;
5932 *idk = CP_ID_KIND_NONE;
5933
5934 /* Enter the scope corresponding to the type of the object
5935 given by the POSTFIX_EXPRESSION. */
5936 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5937 {
5938 scope = TREE_TYPE (postfix_expression);
5939 /* According to the standard, no expression should ever have
5940 reference type. Unfortunately, we do not currently match
5941 the standard in this respect in that our internal representation
5942 of an expression may have reference type even when the standard
5943 says it does not. Therefore, we have to manually obtain the
5944 underlying type here. */
5945 scope = non_reference (scope);
5946 /* The type of the POSTFIX_EXPRESSION must be complete. */
5947 if (scope == unknown_type_node)
5948 {
5949 error_at (location, "%qE does not have class type",
5950 postfix_expression);
5951 scope = NULL_TREE;
5952 }
5953 /* Unlike the object expression in other contexts, *this is not
5954 required to be of complete type for purposes of class member
5955 access (5.2.5) outside the member function body. */
5956 else if (scope != current_class_ref
5957 && !(processing_template_decl && scope == current_class_type))
5958 scope = complete_type_or_else (scope, NULL_TREE);
5959 /* Let the name lookup machinery know that we are processing a
5960 class member access expression. */
5961 parser->context->object_type = scope;
5962 /* If something went wrong, we want to be able to discern that case,
5963 as opposed to the case where there was no SCOPE due to the type
5964 of expression being dependent. */
5965 if (!scope)
5966 scope = error_mark_node;
5967 /* If the SCOPE was erroneous, make the various semantic analysis
5968 functions exit quickly -- and without issuing additional error
5969 messages. */
5970 if (scope == error_mark_node)
5971 postfix_expression = error_mark_node;
5972 }
5973
5974 /* Assume this expression is not a pseudo-destructor access. */
5975 pseudo_destructor_p = false;
5976
5977 /* If the SCOPE is a scalar type, then, if this is a valid program,
5978 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5979 is type dependent, it can be pseudo-destructor-name or something else.
5980 Try to parse it as pseudo-destructor-name first. */
5981 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5982 {
5983 tree s;
5984 tree type;
5985
5986 cp_parser_parse_tentatively (parser);
5987 /* Parse the pseudo-destructor-name. */
5988 s = NULL_TREE;
5989 cp_parser_pseudo_destructor_name (parser, &s, &type);
5990 if (dependent_p
5991 && (cp_parser_error_occurred (parser)
5992 || TREE_CODE (type) != TYPE_DECL
5993 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5994 cp_parser_abort_tentative_parse (parser);
5995 else if (cp_parser_parse_definitely (parser))
5996 {
5997 pseudo_destructor_p = true;
5998 postfix_expression
5999 = finish_pseudo_destructor_expr (postfix_expression,
6000 s, TREE_TYPE (type));
6001 }
6002 }
6003
6004 if (!pseudo_destructor_p)
6005 {
6006 /* If the SCOPE is not a scalar type, we are looking at an
6007 ordinary class member access expression, rather than a
6008 pseudo-destructor-name. */
6009 bool template_p;
6010 cp_token *token = cp_lexer_peek_token (parser->lexer);
6011 /* Parse the id-expression. */
6012 name = (cp_parser_id_expression
6013 (parser,
6014 cp_parser_optional_template_keyword (parser),
6015 /*check_dependency_p=*/true,
6016 &template_p,
6017 /*declarator_p=*/false,
6018 /*optional_p=*/false));
6019 /* In general, build a SCOPE_REF if the member name is qualified.
6020 However, if the name was not dependent and has already been
6021 resolved; there is no need to build the SCOPE_REF. For example;
6022
6023 struct X { void f(); };
6024 template <typename T> void f(T* t) { t->X::f(); }
6025
6026 Even though "t" is dependent, "X::f" is not and has been resolved
6027 to a BASELINK; there is no need to include scope information. */
6028
6029 /* But we do need to remember that there was an explicit scope for
6030 virtual function calls. */
6031 if (parser->scope)
6032 *idk = CP_ID_KIND_QUALIFIED;
6033
6034 /* If the name is a template-id that names a type, we will get a
6035 TYPE_DECL here. That is invalid code. */
6036 if (TREE_CODE (name) == TYPE_DECL)
6037 {
6038 error_at (token->location, "invalid use of %qD", name);
6039 postfix_expression = error_mark_node;
6040 }
6041 else
6042 {
6043 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6044 {
6045 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6046 {
6047 error_at (token->location, "%<%D::%D%> is not a class member",
6048 parser->scope, name);
6049 postfix_expression = error_mark_node;
6050 }
6051 else
6052 name = build_qualified_name (/*type=*/NULL_TREE,
6053 parser->scope,
6054 name,
6055 template_p);
6056 parser->scope = NULL_TREE;
6057 parser->qualifying_scope = NULL_TREE;
6058 parser->object_scope = NULL_TREE;
6059 }
6060 if (parser->scope && name && BASELINK_P (name))
6061 adjust_result_of_qualified_name_lookup
6062 (name, parser->scope, scope);
6063 postfix_expression
6064 = finish_class_member_access_expr (postfix_expression, name,
6065 template_p,
6066 tf_warning_or_error);
6067 }
6068 }
6069
6070 /* We no longer need to look up names in the scope of the object on
6071 the left-hand side of the `.' or `->' operator. */
6072 parser->context->object_type = NULL_TREE;
6073
6074 /* Outside of offsetof, these operators may not appear in
6075 constant-expressions. */
6076 if (!for_offsetof
6077 && (cp_parser_non_integral_constant_expression
6078 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6079 postfix_expression = error_mark_node;
6080
6081 return postfix_expression;
6082 }
6083
6084 /* Parse a parenthesized expression-list.
6085
6086 expression-list:
6087 assignment-expression
6088 expression-list, assignment-expression
6089
6090 attribute-list:
6091 expression-list
6092 identifier
6093 identifier, expression-list
6094
6095 CAST_P is true if this expression is the target of a cast.
6096
6097 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6098 argument pack.
6099
6100 Returns a vector of trees. Each element is a representation of an
6101 assignment-expression. NULL is returned if the ( and or ) are
6102 missing. An empty, but allocated, vector is returned on no
6103 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6104 if we are parsing an attribute list for an attribute that wants a
6105 plain identifier argument, normal_attr for an attribute that wants
6106 an expression, or non_attr if we aren't parsing an attribute list. If
6107 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6108 not all of the expressions in the list were constant. */
6109
6110 static VEC(tree,gc) *
6111 cp_parser_parenthesized_expression_list (cp_parser* parser,
6112 int is_attribute_list,
6113 bool cast_p,
6114 bool allow_expansion_p,
6115 bool *non_constant_p)
6116 {
6117 VEC(tree,gc) *expression_list;
6118 bool fold_expr_p = is_attribute_list != non_attr;
6119 tree identifier = NULL_TREE;
6120 bool saved_greater_than_is_operator_p;
6121
6122 /* Assume all the expressions will be constant. */
6123 if (non_constant_p)
6124 *non_constant_p = false;
6125
6126 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6127 return NULL;
6128
6129 expression_list = make_tree_vector ();
6130
6131 /* Within a parenthesized expression, a `>' token is always
6132 the greater-than operator. */
6133 saved_greater_than_is_operator_p
6134 = parser->greater_than_is_operator_p;
6135 parser->greater_than_is_operator_p = true;
6136
6137 /* Consume expressions until there are no more. */
6138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6139 while (true)
6140 {
6141 tree expr;
6142
6143 /* At the beginning of attribute lists, check to see if the
6144 next token is an identifier. */
6145 if (is_attribute_list == id_attr
6146 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6147 {
6148 cp_token *token;
6149
6150 /* Consume the identifier. */
6151 token = cp_lexer_consume_token (parser->lexer);
6152 /* Save the identifier. */
6153 identifier = token->u.value;
6154 }
6155 else
6156 {
6157 bool expr_non_constant_p;
6158
6159 /* Parse the next assignment-expression. */
6160 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6161 {
6162 /* A braced-init-list. */
6163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6164 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6165 if (non_constant_p && expr_non_constant_p)
6166 *non_constant_p = true;
6167 }
6168 else if (non_constant_p)
6169 {
6170 expr = (cp_parser_constant_expression
6171 (parser, /*allow_non_constant_p=*/true,
6172 &expr_non_constant_p));
6173 if (expr_non_constant_p)
6174 *non_constant_p = true;
6175 }
6176 else
6177 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6178
6179 if (fold_expr_p)
6180 expr = fold_non_dependent_expr (expr);
6181
6182 /* If we have an ellipsis, then this is an expression
6183 expansion. */
6184 if (allow_expansion_p
6185 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6186 {
6187 /* Consume the `...'. */
6188 cp_lexer_consume_token (parser->lexer);
6189
6190 /* Build the argument pack. */
6191 expr = make_pack_expansion (expr);
6192 }
6193
6194 /* Add it to the list. We add error_mark_node
6195 expressions to the list, so that we can still tell if
6196 the correct form for a parenthesized expression-list
6197 is found. That gives better errors. */
6198 VEC_safe_push (tree, gc, expression_list, expr);
6199
6200 if (expr == error_mark_node)
6201 goto skip_comma;
6202 }
6203
6204 /* After the first item, attribute lists look the same as
6205 expression lists. */
6206 is_attribute_list = non_attr;
6207
6208 get_comma:;
6209 /* If the next token isn't a `,', then we are done. */
6210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6211 break;
6212
6213 /* Otherwise, consume the `,' and keep going. */
6214 cp_lexer_consume_token (parser->lexer);
6215 }
6216
6217 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6218 {
6219 int ending;
6220
6221 skip_comma:;
6222 /* We try and resync to an unnested comma, as that will give the
6223 user better diagnostics. */
6224 ending = cp_parser_skip_to_closing_parenthesis (parser,
6225 /*recovering=*/true,
6226 /*or_comma=*/true,
6227 /*consume_paren=*/true);
6228 if (ending < 0)
6229 goto get_comma;
6230 if (!ending)
6231 {
6232 parser->greater_than_is_operator_p
6233 = saved_greater_than_is_operator_p;
6234 return NULL;
6235 }
6236 }
6237
6238 parser->greater_than_is_operator_p
6239 = saved_greater_than_is_operator_p;
6240
6241 if (identifier)
6242 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6243
6244 return expression_list;
6245 }
6246
6247 /* Parse a pseudo-destructor-name.
6248
6249 pseudo-destructor-name:
6250 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6251 :: [opt] nested-name-specifier template template-id :: ~ type-name
6252 :: [opt] nested-name-specifier [opt] ~ type-name
6253
6254 If either of the first two productions is used, sets *SCOPE to the
6255 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6256 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6257 or ERROR_MARK_NODE if the parse fails. */
6258
6259 static void
6260 cp_parser_pseudo_destructor_name (cp_parser* parser,
6261 tree* scope,
6262 tree* type)
6263 {
6264 bool nested_name_specifier_p;
6265
6266 /* Assume that things will not work out. */
6267 *type = error_mark_node;
6268
6269 /* Look for the optional `::' operator. */
6270 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6271 /* Look for the optional nested-name-specifier. */
6272 nested_name_specifier_p
6273 = (cp_parser_nested_name_specifier_opt (parser,
6274 /*typename_keyword_p=*/false,
6275 /*check_dependency_p=*/true,
6276 /*type_p=*/false,
6277 /*is_declaration=*/false)
6278 != NULL_TREE);
6279 /* Now, if we saw a nested-name-specifier, we might be doing the
6280 second production. */
6281 if (nested_name_specifier_p
6282 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6283 {
6284 /* Consume the `template' keyword. */
6285 cp_lexer_consume_token (parser->lexer);
6286 /* Parse the template-id. */
6287 cp_parser_template_id (parser,
6288 /*template_keyword_p=*/true,
6289 /*check_dependency_p=*/false,
6290 class_type,
6291 /*is_declaration=*/true);
6292 /* Look for the `::' token. */
6293 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6294 }
6295 /* If the next token is not a `~', then there might be some
6296 additional qualification. */
6297 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6298 {
6299 /* At this point, we're looking for "type-name :: ~". The type-name
6300 must not be a class-name, since this is a pseudo-destructor. So,
6301 it must be either an enum-name, or a typedef-name -- both of which
6302 are just identifiers. So, we peek ahead to check that the "::"
6303 and "~" tokens are present; if they are not, then we can avoid
6304 calling type_name. */
6305 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6306 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6307 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6308 {
6309 cp_parser_error (parser, "non-scalar type");
6310 return;
6311 }
6312
6313 /* Look for the type-name. */
6314 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6315 if (*scope == error_mark_node)
6316 return;
6317
6318 /* Look for the `::' token. */
6319 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6320 }
6321 else
6322 *scope = NULL_TREE;
6323
6324 /* Look for the `~'. */
6325 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6326
6327 /* Once we see the ~, this has to be a pseudo-destructor. */
6328 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6329 cp_parser_commit_to_tentative_parse (parser);
6330
6331 /* Look for the type-name again. We are not responsible for
6332 checking that it matches the first type-name. */
6333 *type = cp_parser_nonclass_name (parser);
6334 }
6335
6336 /* Parse a unary-expression.
6337
6338 unary-expression:
6339 postfix-expression
6340 ++ cast-expression
6341 -- cast-expression
6342 unary-operator cast-expression
6343 sizeof unary-expression
6344 sizeof ( type-id )
6345 alignof ( type-id ) [C++0x]
6346 new-expression
6347 delete-expression
6348
6349 GNU Extensions:
6350
6351 unary-expression:
6352 __extension__ cast-expression
6353 __alignof__ unary-expression
6354 __alignof__ ( type-id )
6355 alignof unary-expression [C++0x]
6356 __real__ cast-expression
6357 __imag__ cast-expression
6358 && identifier
6359
6360 ADDRESS_P is true iff the unary-expression is appearing as the
6361 operand of the `&' operator. CAST_P is true if this expression is
6362 the target of a cast.
6363
6364 Returns a representation of the expression. */
6365
6366 static tree
6367 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6368 cp_id_kind * pidk)
6369 {
6370 cp_token *token;
6371 enum tree_code unary_operator;
6372
6373 /* Peek at the next token. */
6374 token = cp_lexer_peek_token (parser->lexer);
6375 /* Some keywords give away the kind of expression. */
6376 if (token->type == CPP_KEYWORD)
6377 {
6378 enum rid keyword = token->keyword;
6379
6380 switch (keyword)
6381 {
6382 case RID_ALIGNOF:
6383 case RID_SIZEOF:
6384 {
6385 tree operand;
6386 enum tree_code op;
6387
6388 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6389 /* Consume the token. */
6390 cp_lexer_consume_token (parser->lexer);
6391 /* Parse the operand. */
6392 operand = cp_parser_sizeof_operand (parser, keyword);
6393
6394 if (TYPE_P (operand))
6395 return cxx_sizeof_or_alignof_type (operand, op, true);
6396 else
6397 {
6398 /* ISO C++ defines alignof only with types, not with
6399 expressions. So pedwarn if alignof is used with a non-
6400 type expression. However, __alignof__ is ok. */
6401 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6402 pedwarn (token->location, OPT_Wpedantic,
6403 "ISO C++ does not allow %<alignof%> "
6404 "with a non-type");
6405
6406 return cxx_sizeof_or_alignof_expr (operand, op, true);
6407 }
6408 }
6409
6410 case RID_NEW:
6411 return cp_parser_new_expression (parser);
6412
6413 case RID_DELETE:
6414 return cp_parser_delete_expression (parser);
6415
6416 case RID_EXTENSION:
6417 {
6418 /* The saved value of the PEDANTIC flag. */
6419 int saved_pedantic;
6420 tree expr;
6421
6422 /* Save away the PEDANTIC flag. */
6423 cp_parser_extension_opt (parser, &saved_pedantic);
6424 /* Parse the cast-expression. */
6425 expr = cp_parser_simple_cast_expression (parser);
6426 /* Restore the PEDANTIC flag. */
6427 pedantic = saved_pedantic;
6428
6429 return expr;
6430 }
6431
6432 case RID_REALPART:
6433 case RID_IMAGPART:
6434 {
6435 tree expression;
6436
6437 /* Consume the `__real__' or `__imag__' token. */
6438 cp_lexer_consume_token (parser->lexer);
6439 /* Parse the cast-expression. */
6440 expression = cp_parser_simple_cast_expression (parser);
6441 /* Create the complete representation. */
6442 return build_x_unary_op (token->location,
6443 (keyword == RID_REALPART
6444 ? REALPART_EXPR : IMAGPART_EXPR),
6445 expression,
6446 tf_warning_or_error);
6447 }
6448 break;
6449
6450 case RID_TRANSACTION_ATOMIC:
6451 case RID_TRANSACTION_RELAXED:
6452 return cp_parser_transaction_expression (parser, keyword);
6453
6454 case RID_NOEXCEPT:
6455 {
6456 tree expr;
6457 const char *saved_message;
6458 bool saved_integral_constant_expression_p;
6459 bool saved_non_integral_constant_expression_p;
6460 bool saved_greater_than_is_operator_p;
6461
6462 cp_lexer_consume_token (parser->lexer);
6463 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6464
6465 saved_message = parser->type_definition_forbidden_message;
6466 parser->type_definition_forbidden_message
6467 = G_("types may not be defined in %<noexcept%> expressions");
6468
6469 saved_integral_constant_expression_p
6470 = parser->integral_constant_expression_p;
6471 saved_non_integral_constant_expression_p
6472 = parser->non_integral_constant_expression_p;
6473 parser->integral_constant_expression_p = false;
6474
6475 saved_greater_than_is_operator_p
6476 = parser->greater_than_is_operator_p;
6477 parser->greater_than_is_operator_p = true;
6478
6479 ++cp_unevaluated_operand;
6480 ++c_inhibit_evaluation_warnings;
6481 expr = cp_parser_expression (parser, false, NULL);
6482 --c_inhibit_evaluation_warnings;
6483 --cp_unevaluated_operand;
6484
6485 parser->greater_than_is_operator_p
6486 = saved_greater_than_is_operator_p;
6487
6488 parser->integral_constant_expression_p
6489 = saved_integral_constant_expression_p;
6490 parser->non_integral_constant_expression_p
6491 = saved_non_integral_constant_expression_p;
6492
6493 parser->type_definition_forbidden_message = saved_message;
6494
6495 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6496 return finish_noexcept_expr (expr, tf_warning_or_error);
6497 }
6498
6499 default:
6500 break;
6501 }
6502 }
6503
6504 /* Look for the `:: new' and `:: delete', which also signal the
6505 beginning of a new-expression, or delete-expression,
6506 respectively. If the next token is `::', then it might be one of
6507 these. */
6508 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6509 {
6510 enum rid keyword;
6511
6512 /* See if the token after the `::' is one of the keywords in
6513 which we're interested. */
6514 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6515 /* If it's `new', we have a new-expression. */
6516 if (keyword == RID_NEW)
6517 return cp_parser_new_expression (parser);
6518 /* Similarly, for `delete'. */
6519 else if (keyword == RID_DELETE)
6520 return cp_parser_delete_expression (parser);
6521 }
6522
6523 /* Look for a unary operator. */
6524 unary_operator = cp_parser_unary_operator (token);
6525 /* The `++' and `--' operators can be handled similarly, even though
6526 they are not technically unary-operators in the grammar. */
6527 if (unary_operator == ERROR_MARK)
6528 {
6529 if (token->type == CPP_PLUS_PLUS)
6530 unary_operator = PREINCREMENT_EXPR;
6531 else if (token->type == CPP_MINUS_MINUS)
6532 unary_operator = PREDECREMENT_EXPR;
6533 /* Handle the GNU address-of-label extension. */
6534 else if (cp_parser_allow_gnu_extensions_p (parser)
6535 && token->type == CPP_AND_AND)
6536 {
6537 tree identifier;
6538 tree expression;
6539 location_t loc = token->location;
6540
6541 /* Consume the '&&' token. */
6542 cp_lexer_consume_token (parser->lexer);
6543 /* Look for the identifier. */
6544 identifier = cp_parser_identifier (parser);
6545 /* Create an expression representing the address. */
6546 expression = finish_label_address_expr (identifier, loc);
6547 if (cp_parser_non_integral_constant_expression (parser,
6548 NIC_ADDR_LABEL))
6549 expression = error_mark_node;
6550 return expression;
6551 }
6552 }
6553 if (unary_operator != ERROR_MARK)
6554 {
6555 tree cast_expression;
6556 tree expression = error_mark_node;
6557 non_integral_constant non_constant_p = NIC_NONE;
6558 location_t loc = token->location;
6559
6560 /* Consume the operator token. */
6561 token = cp_lexer_consume_token (parser->lexer);
6562 /* Parse the cast-expression. */
6563 cast_expression
6564 = cp_parser_cast_expression (parser,
6565 unary_operator == ADDR_EXPR,
6566 /*cast_p=*/false, pidk);
6567 /* Now, build an appropriate representation. */
6568 switch (unary_operator)
6569 {
6570 case INDIRECT_REF:
6571 non_constant_p = NIC_STAR;
6572 expression = build_x_indirect_ref (loc, cast_expression,
6573 RO_UNARY_STAR,
6574 tf_warning_or_error);
6575 break;
6576
6577 case ADDR_EXPR:
6578 non_constant_p = NIC_ADDR;
6579 /* Fall through. */
6580 case BIT_NOT_EXPR:
6581 expression = build_x_unary_op (loc, unary_operator,
6582 cast_expression,
6583 tf_warning_or_error);
6584 break;
6585
6586 case PREINCREMENT_EXPR:
6587 case PREDECREMENT_EXPR:
6588 non_constant_p = unary_operator == PREINCREMENT_EXPR
6589 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6590 /* Fall through. */
6591 case UNARY_PLUS_EXPR:
6592 case NEGATE_EXPR:
6593 case TRUTH_NOT_EXPR:
6594 expression = finish_unary_op_expr (loc, unary_operator,
6595 cast_expression);
6596 break;
6597
6598 default:
6599 gcc_unreachable ();
6600 }
6601
6602 if (non_constant_p != NIC_NONE
6603 && cp_parser_non_integral_constant_expression (parser,
6604 non_constant_p))
6605 expression = error_mark_node;
6606
6607 return expression;
6608 }
6609
6610 return cp_parser_postfix_expression (parser, address_p, cast_p,
6611 /*member_access_only_p=*/false,
6612 pidk);
6613 }
6614
6615 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6616 unary-operator, the corresponding tree code is returned. */
6617
6618 static enum tree_code
6619 cp_parser_unary_operator (cp_token* token)
6620 {
6621 switch (token->type)
6622 {
6623 case CPP_MULT:
6624 return INDIRECT_REF;
6625
6626 case CPP_AND:
6627 return ADDR_EXPR;
6628
6629 case CPP_PLUS:
6630 return UNARY_PLUS_EXPR;
6631
6632 case CPP_MINUS:
6633 return NEGATE_EXPR;
6634
6635 case CPP_NOT:
6636 return TRUTH_NOT_EXPR;
6637
6638 case CPP_COMPL:
6639 return BIT_NOT_EXPR;
6640
6641 default:
6642 return ERROR_MARK;
6643 }
6644 }
6645
6646 /* Parse a new-expression.
6647
6648 new-expression:
6649 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6650 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6651
6652 Returns a representation of the expression. */
6653
6654 static tree
6655 cp_parser_new_expression (cp_parser* parser)
6656 {
6657 bool global_scope_p;
6658 VEC(tree,gc) *placement;
6659 tree type;
6660 VEC(tree,gc) *initializer;
6661 tree nelts = NULL_TREE;
6662 tree ret;
6663
6664 /* Look for the optional `::' operator. */
6665 global_scope_p
6666 = (cp_parser_global_scope_opt (parser,
6667 /*current_scope_valid_p=*/false)
6668 != NULL_TREE);
6669 /* Look for the `new' operator. */
6670 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6671 /* There's no easy way to tell a new-placement from the
6672 `( type-id )' construct. */
6673 cp_parser_parse_tentatively (parser);
6674 /* Look for a new-placement. */
6675 placement = cp_parser_new_placement (parser);
6676 /* If that didn't work out, there's no new-placement. */
6677 if (!cp_parser_parse_definitely (parser))
6678 {
6679 if (placement != NULL)
6680 release_tree_vector (placement);
6681 placement = NULL;
6682 }
6683
6684 /* If the next token is a `(', then we have a parenthesized
6685 type-id. */
6686 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6687 {
6688 cp_token *token;
6689 const char *saved_message = parser->type_definition_forbidden_message;
6690
6691 /* Consume the `('. */
6692 cp_lexer_consume_token (parser->lexer);
6693
6694 /* Parse the type-id. */
6695 parser->type_definition_forbidden_message
6696 = G_("types may not be defined in a new-expression");
6697 type = cp_parser_type_id (parser);
6698 parser->type_definition_forbidden_message = saved_message;
6699
6700 /* Look for the closing `)'. */
6701 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6702 token = cp_lexer_peek_token (parser->lexer);
6703 /* There should not be a direct-new-declarator in this production,
6704 but GCC used to allowed this, so we check and emit a sensible error
6705 message for this case. */
6706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6707 {
6708 error_at (token->location,
6709 "array bound forbidden after parenthesized type-id");
6710 inform (token->location,
6711 "try removing the parentheses around the type-id");
6712 cp_parser_direct_new_declarator (parser);
6713 }
6714 }
6715 /* Otherwise, there must be a new-type-id. */
6716 else
6717 type = cp_parser_new_type_id (parser, &nelts);
6718
6719 /* If the next token is a `(' or '{', then we have a new-initializer. */
6720 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6721 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6722 initializer = cp_parser_new_initializer (parser);
6723 else
6724 initializer = NULL;
6725
6726 /* A new-expression may not appear in an integral constant
6727 expression. */
6728 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6729 ret = error_mark_node;
6730 else
6731 {
6732 /* Create a representation of the new-expression. */
6733 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6734 tf_warning_or_error);
6735 }
6736
6737 if (placement != NULL)
6738 release_tree_vector (placement);
6739 if (initializer != NULL)
6740 release_tree_vector (initializer);
6741
6742 return ret;
6743 }
6744
6745 /* Parse a new-placement.
6746
6747 new-placement:
6748 ( expression-list )
6749
6750 Returns the same representation as for an expression-list. */
6751
6752 static VEC(tree,gc) *
6753 cp_parser_new_placement (cp_parser* parser)
6754 {
6755 VEC(tree,gc) *expression_list;
6756
6757 /* Parse the expression-list. */
6758 expression_list = (cp_parser_parenthesized_expression_list
6759 (parser, non_attr, /*cast_p=*/false,
6760 /*allow_expansion_p=*/true,
6761 /*non_constant_p=*/NULL));
6762
6763 return expression_list;
6764 }
6765
6766 /* Parse a new-type-id.
6767
6768 new-type-id:
6769 type-specifier-seq new-declarator [opt]
6770
6771 Returns the TYPE allocated. If the new-type-id indicates an array
6772 type, *NELTS is set to the number of elements in the last array
6773 bound; the TYPE will not include the last array bound. */
6774
6775 static tree
6776 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6777 {
6778 cp_decl_specifier_seq type_specifier_seq;
6779 cp_declarator *new_declarator;
6780 cp_declarator *declarator;
6781 cp_declarator *outer_declarator;
6782 const char *saved_message;
6783
6784 /* The type-specifier sequence must not contain type definitions.
6785 (It cannot contain declarations of new types either, but if they
6786 are not definitions we will catch that because they are not
6787 complete.) */
6788 saved_message = parser->type_definition_forbidden_message;
6789 parser->type_definition_forbidden_message
6790 = G_("types may not be defined in a new-type-id");
6791 /* Parse the type-specifier-seq. */
6792 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6793 /*is_trailing_return=*/false,
6794 &type_specifier_seq);
6795 /* Restore the old message. */
6796 parser->type_definition_forbidden_message = saved_message;
6797
6798 if (type_specifier_seq.type == error_mark_node)
6799 return error_mark_node;
6800
6801 /* Parse the new-declarator. */
6802 new_declarator = cp_parser_new_declarator_opt (parser);
6803
6804 /* Determine the number of elements in the last array dimension, if
6805 any. */
6806 *nelts = NULL_TREE;
6807 /* Skip down to the last array dimension. */
6808 declarator = new_declarator;
6809 outer_declarator = NULL;
6810 while (declarator && (declarator->kind == cdk_pointer
6811 || declarator->kind == cdk_ptrmem))
6812 {
6813 outer_declarator = declarator;
6814 declarator = declarator->declarator;
6815 }
6816 while (declarator
6817 && declarator->kind == cdk_array
6818 && declarator->declarator
6819 && declarator->declarator->kind == cdk_array)
6820 {
6821 outer_declarator = declarator;
6822 declarator = declarator->declarator;
6823 }
6824
6825 if (declarator && declarator->kind == cdk_array)
6826 {
6827 *nelts = declarator->u.array.bounds;
6828 if (*nelts == error_mark_node)
6829 *nelts = integer_one_node;
6830
6831 if (outer_declarator)
6832 outer_declarator->declarator = declarator->declarator;
6833 else
6834 new_declarator = NULL;
6835 }
6836
6837 return groktypename (&type_specifier_seq, new_declarator, false);
6838 }
6839
6840 /* Parse an (optional) new-declarator.
6841
6842 new-declarator:
6843 ptr-operator new-declarator [opt]
6844 direct-new-declarator
6845
6846 Returns the declarator. */
6847
6848 static cp_declarator *
6849 cp_parser_new_declarator_opt (cp_parser* parser)
6850 {
6851 enum tree_code code;
6852 tree type;
6853 cp_cv_quals cv_quals;
6854
6855 /* We don't know if there's a ptr-operator next, or not. */
6856 cp_parser_parse_tentatively (parser);
6857 /* Look for a ptr-operator. */
6858 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6859 /* If that worked, look for more new-declarators. */
6860 if (cp_parser_parse_definitely (parser))
6861 {
6862 cp_declarator *declarator;
6863
6864 /* Parse another optional declarator. */
6865 declarator = cp_parser_new_declarator_opt (parser);
6866
6867 return cp_parser_make_indirect_declarator
6868 (code, type, cv_quals, declarator);
6869 }
6870
6871 /* If the next token is a `[', there is a direct-new-declarator. */
6872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6873 return cp_parser_direct_new_declarator (parser);
6874
6875 return NULL;
6876 }
6877
6878 /* Parse a direct-new-declarator.
6879
6880 direct-new-declarator:
6881 [ expression ]
6882 direct-new-declarator [constant-expression]
6883
6884 */
6885
6886 static cp_declarator *
6887 cp_parser_direct_new_declarator (cp_parser* parser)
6888 {
6889 cp_declarator *declarator = NULL;
6890
6891 while (true)
6892 {
6893 tree expression;
6894 cp_token *token;
6895
6896 /* Look for the opening `['. */
6897 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6898
6899 token = cp_lexer_peek_token (parser->lexer);
6900 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6901 /* The standard requires that the expression have integral
6902 type. DR 74 adds enumeration types. We believe that the
6903 real intent is that these expressions be handled like the
6904 expression in a `switch' condition, which also allows
6905 classes with a single conversion to integral or
6906 enumeration type. */
6907 if (!processing_template_decl)
6908 {
6909 expression
6910 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6911 expression,
6912 /*complain=*/true);
6913 if (!expression)
6914 {
6915 error_at (token->location,
6916 "expression in new-declarator must have integral "
6917 "or enumeration type");
6918 expression = error_mark_node;
6919 }
6920 }
6921
6922 /* Look for the closing `]'. */
6923 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6924
6925 /* Add this bound to the declarator. */
6926 declarator = make_array_declarator (declarator, expression);
6927
6928 /* If the next token is not a `[', then there are no more
6929 bounds. */
6930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6931 break;
6932 }
6933
6934 return declarator;
6935 }
6936
6937 /* Parse a new-initializer.
6938
6939 new-initializer:
6940 ( expression-list [opt] )
6941 braced-init-list
6942
6943 Returns a representation of the expression-list. */
6944
6945 static VEC(tree,gc) *
6946 cp_parser_new_initializer (cp_parser* parser)
6947 {
6948 VEC(tree,gc) *expression_list;
6949
6950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6951 {
6952 tree t;
6953 bool expr_non_constant_p;
6954 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6955 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6956 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6957 expression_list = make_tree_vector_single (t);
6958 }
6959 else
6960 expression_list = (cp_parser_parenthesized_expression_list
6961 (parser, non_attr, /*cast_p=*/false,
6962 /*allow_expansion_p=*/true,
6963 /*non_constant_p=*/NULL));
6964
6965 return expression_list;
6966 }
6967
6968 /* Parse a delete-expression.
6969
6970 delete-expression:
6971 :: [opt] delete cast-expression
6972 :: [opt] delete [ ] cast-expression
6973
6974 Returns a representation of the expression. */
6975
6976 static tree
6977 cp_parser_delete_expression (cp_parser* parser)
6978 {
6979 bool global_scope_p;
6980 bool array_p;
6981 tree expression;
6982
6983 /* Look for the optional `::' operator. */
6984 global_scope_p
6985 = (cp_parser_global_scope_opt (parser,
6986 /*current_scope_valid_p=*/false)
6987 != NULL_TREE);
6988 /* Look for the `delete' keyword. */
6989 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6990 /* See if the array syntax is in use. */
6991 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6992 {
6993 /* Consume the `[' token. */
6994 cp_lexer_consume_token (parser->lexer);
6995 /* Look for the `]' token. */
6996 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6997 /* Remember that this is the `[]' construct. */
6998 array_p = true;
6999 }
7000 else
7001 array_p = false;
7002
7003 /* Parse the cast-expression. */
7004 expression = cp_parser_simple_cast_expression (parser);
7005
7006 /* A delete-expression may not appear in an integral constant
7007 expression. */
7008 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7009 return error_mark_node;
7010
7011 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7012 tf_warning_or_error);
7013 }
7014
7015 /* Returns true if TOKEN may start a cast-expression and false
7016 otherwise. */
7017
7018 static bool
7019 cp_parser_token_starts_cast_expression (cp_token *token)
7020 {
7021 switch (token->type)
7022 {
7023 case CPP_COMMA:
7024 case CPP_SEMICOLON:
7025 case CPP_QUERY:
7026 case CPP_COLON:
7027 case CPP_CLOSE_SQUARE:
7028 case CPP_CLOSE_PAREN:
7029 case CPP_CLOSE_BRACE:
7030 case CPP_DOT:
7031 case CPP_DOT_STAR:
7032 case CPP_DEREF:
7033 case CPP_DEREF_STAR:
7034 case CPP_DIV:
7035 case CPP_MOD:
7036 case CPP_LSHIFT:
7037 case CPP_RSHIFT:
7038 case CPP_LESS:
7039 case CPP_GREATER:
7040 case CPP_LESS_EQ:
7041 case CPP_GREATER_EQ:
7042 case CPP_EQ_EQ:
7043 case CPP_NOT_EQ:
7044 case CPP_EQ:
7045 case CPP_MULT_EQ:
7046 case CPP_DIV_EQ:
7047 case CPP_MOD_EQ:
7048 case CPP_PLUS_EQ:
7049 case CPP_MINUS_EQ:
7050 case CPP_RSHIFT_EQ:
7051 case CPP_LSHIFT_EQ:
7052 case CPP_AND_EQ:
7053 case CPP_XOR_EQ:
7054 case CPP_OR_EQ:
7055 case CPP_XOR:
7056 case CPP_OR:
7057 case CPP_OR_OR:
7058 case CPP_EOF:
7059 return false;
7060
7061 /* '[' may start a primary-expression in obj-c++. */
7062 case CPP_OPEN_SQUARE:
7063 return c_dialect_objc ();
7064
7065 default:
7066 return true;
7067 }
7068 }
7069
7070 /* Parse a cast-expression.
7071
7072 cast-expression:
7073 unary-expression
7074 ( type-id ) cast-expression
7075
7076 ADDRESS_P is true iff the unary-expression is appearing as the
7077 operand of the `&' operator. CAST_P is true if this expression is
7078 the target of a cast.
7079
7080 Returns a representation of the expression. */
7081
7082 static tree
7083 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7084 cp_id_kind * pidk)
7085 {
7086 /* If it's a `(', then we might be looking at a cast. */
7087 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7088 {
7089 tree type = NULL_TREE;
7090 tree expr = NULL_TREE;
7091 bool compound_literal_p;
7092 const char *saved_message;
7093
7094 /* There's no way to know yet whether or not this is a cast.
7095 For example, `(int (3))' is a unary-expression, while `(int)
7096 3' is a cast. So, we resort to parsing tentatively. */
7097 cp_parser_parse_tentatively (parser);
7098 /* Types may not be defined in a cast. */
7099 saved_message = parser->type_definition_forbidden_message;
7100 parser->type_definition_forbidden_message
7101 = G_("types may not be defined in casts");
7102 /* Consume the `('. */
7103 cp_lexer_consume_token (parser->lexer);
7104 /* A very tricky bit is that `(struct S) { 3 }' is a
7105 compound-literal (which we permit in C++ as an extension).
7106 But, that construct is not a cast-expression -- it is a
7107 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7108 is legal; if the compound-literal were a cast-expression,
7109 you'd need an extra set of parentheses.) But, if we parse
7110 the type-id, and it happens to be a class-specifier, then we
7111 will commit to the parse at that point, because we cannot
7112 undo the action that is done when creating a new class. So,
7113 then we cannot back up and do a postfix-expression.
7114
7115 Therefore, we scan ahead to the closing `)', and check to see
7116 if the token after the `)' is a `{'. If so, we are not
7117 looking at a cast-expression.
7118
7119 Save tokens so that we can put them back. */
7120 cp_lexer_save_tokens (parser->lexer);
7121 /* Skip tokens until the next token is a closing parenthesis.
7122 If we find the closing `)', and the next token is a `{', then
7123 we are looking at a compound-literal. */
7124 compound_literal_p
7125 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7126 /*consume_paren=*/true)
7127 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7128 /* Roll back the tokens we skipped. */
7129 cp_lexer_rollback_tokens (parser->lexer);
7130 /* If we were looking at a compound-literal, simulate an error
7131 so that the call to cp_parser_parse_definitely below will
7132 fail. */
7133 if (compound_literal_p)
7134 cp_parser_simulate_error (parser);
7135 else
7136 {
7137 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7138 parser->in_type_id_in_expr_p = true;
7139 /* Look for the type-id. */
7140 type = cp_parser_type_id (parser);
7141 /* Look for the closing `)'. */
7142 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7143 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7144 }
7145
7146 /* Restore the saved message. */
7147 parser->type_definition_forbidden_message = saved_message;
7148
7149 /* At this point this can only be either a cast or a
7150 parenthesized ctor such as `(T ())' that looks like a cast to
7151 function returning T. */
7152 if (!cp_parser_error_occurred (parser)
7153 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7154 (parser->lexer)))
7155 {
7156 cp_parser_parse_definitely (parser);
7157 expr = cp_parser_cast_expression (parser,
7158 /*address_p=*/false,
7159 /*cast_p=*/true, pidk);
7160
7161 /* Warn about old-style casts, if so requested. */
7162 if (warn_old_style_cast
7163 && !in_system_header
7164 && !VOID_TYPE_P (type)
7165 && current_lang_name != lang_name_c)
7166 warning (OPT_Wold_style_cast, "use of old-style cast");
7167
7168 /* Only type conversions to integral or enumeration types
7169 can be used in constant-expressions. */
7170 if (!cast_valid_in_integral_constant_expression_p (type)
7171 && cp_parser_non_integral_constant_expression (parser,
7172 NIC_CAST))
7173 return error_mark_node;
7174
7175 /* Perform the cast. */
7176 expr = build_c_cast (input_location, type, expr);
7177 return expr;
7178 }
7179 else
7180 cp_parser_abort_tentative_parse (parser);
7181 }
7182
7183 /* If we get here, then it's not a cast, so it must be a
7184 unary-expression. */
7185 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7186 }
7187
7188 /* Parse a binary expression of the general form:
7189
7190 pm-expression:
7191 cast-expression
7192 pm-expression .* cast-expression
7193 pm-expression ->* cast-expression
7194
7195 multiplicative-expression:
7196 pm-expression
7197 multiplicative-expression * pm-expression
7198 multiplicative-expression / pm-expression
7199 multiplicative-expression % pm-expression
7200
7201 additive-expression:
7202 multiplicative-expression
7203 additive-expression + multiplicative-expression
7204 additive-expression - multiplicative-expression
7205
7206 shift-expression:
7207 additive-expression
7208 shift-expression << additive-expression
7209 shift-expression >> additive-expression
7210
7211 relational-expression:
7212 shift-expression
7213 relational-expression < shift-expression
7214 relational-expression > shift-expression
7215 relational-expression <= shift-expression
7216 relational-expression >= shift-expression
7217
7218 GNU Extension:
7219
7220 relational-expression:
7221 relational-expression <? shift-expression
7222 relational-expression >? shift-expression
7223
7224 equality-expression:
7225 relational-expression
7226 equality-expression == relational-expression
7227 equality-expression != relational-expression
7228
7229 and-expression:
7230 equality-expression
7231 and-expression & equality-expression
7232
7233 exclusive-or-expression:
7234 and-expression
7235 exclusive-or-expression ^ and-expression
7236
7237 inclusive-or-expression:
7238 exclusive-or-expression
7239 inclusive-or-expression | exclusive-or-expression
7240
7241 logical-and-expression:
7242 inclusive-or-expression
7243 logical-and-expression && inclusive-or-expression
7244
7245 logical-or-expression:
7246 logical-and-expression
7247 logical-or-expression || logical-and-expression
7248
7249 All these are implemented with a single function like:
7250
7251 binary-expression:
7252 simple-cast-expression
7253 binary-expression <token> binary-expression
7254
7255 CAST_P is true if this expression is the target of a cast.
7256
7257 The binops_by_token map is used to get the tree codes for each <token> type.
7258 binary-expressions are associated according to a precedence table. */
7259
7260 #define TOKEN_PRECEDENCE(token) \
7261 (((token->type == CPP_GREATER \
7262 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7263 && !parser->greater_than_is_operator_p) \
7264 ? PREC_NOT_OPERATOR \
7265 : binops_by_token[token->type].prec)
7266
7267 static tree
7268 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7269 bool no_toplevel_fold_p,
7270 enum cp_parser_prec prec,
7271 cp_id_kind * pidk)
7272 {
7273 cp_parser_expression_stack stack;
7274 cp_parser_expression_stack_entry *sp = &stack[0];
7275 cp_parser_expression_stack_entry current;
7276 tree rhs;
7277 cp_token *token;
7278 enum tree_code rhs_type;
7279 enum cp_parser_prec new_prec, lookahead_prec;
7280 tree overload;
7281
7282 /* Parse the first expression. */
7283 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7284 cast_p, pidk);
7285 current.lhs_type = ERROR_MARK;
7286 current.prec = prec;
7287
7288 if (cp_parser_error_occurred (parser))
7289 return error_mark_node;
7290
7291 for (;;)
7292 {
7293 /* Get an operator token. */
7294 token = cp_lexer_peek_token (parser->lexer);
7295
7296 if (warn_cxx0x_compat
7297 && token->type == CPP_RSHIFT
7298 && !parser->greater_than_is_operator_p)
7299 {
7300 if (warning_at (token->location, OPT_Wc__0x_compat,
7301 "%<>>%> operator is treated"
7302 " as two right angle brackets in C++11"))
7303 inform (token->location,
7304 "suggest parentheses around %<>>%> expression");
7305 }
7306
7307 new_prec = TOKEN_PRECEDENCE (token);
7308
7309 /* Popping an entry off the stack means we completed a subexpression:
7310 - either we found a token which is not an operator (`>' where it is not
7311 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7312 will happen repeatedly;
7313 - or, we found an operator which has lower priority. This is the case
7314 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7315 parsing `3 * 4'. */
7316 if (new_prec <= current.prec)
7317 {
7318 if (sp == stack)
7319 break;
7320 else
7321 goto pop;
7322 }
7323
7324 get_rhs:
7325 current.tree_type = binops_by_token[token->type].tree_type;
7326 current.loc = token->location;
7327
7328 /* We used the operator token. */
7329 cp_lexer_consume_token (parser->lexer);
7330
7331 /* For "false && x" or "true || x", x will never be executed;
7332 disable warnings while evaluating it. */
7333 if (current.tree_type == TRUTH_ANDIF_EXPR)
7334 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7335 else if (current.tree_type == TRUTH_ORIF_EXPR)
7336 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7337
7338 /* Extract another operand. It may be the RHS of this expression
7339 or the LHS of a new, higher priority expression. */
7340 rhs = cp_parser_simple_cast_expression (parser);
7341 rhs_type = ERROR_MARK;
7342
7343 /* Get another operator token. Look up its precedence to avoid
7344 building a useless (immediately popped) stack entry for common
7345 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7346 token = cp_lexer_peek_token (parser->lexer);
7347 lookahead_prec = TOKEN_PRECEDENCE (token);
7348 if (lookahead_prec > new_prec)
7349 {
7350 /* ... and prepare to parse the RHS of the new, higher priority
7351 expression. Since precedence levels on the stack are
7352 monotonically increasing, we do not have to care about
7353 stack overflows. */
7354 *sp = current;
7355 ++sp;
7356 current.lhs = rhs;
7357 current.lhs_type = rhs_type;
7358 current.prec = new_prec;
7359 new_prec = lookahead_prec;
7360 goto get_rhs;
7361
7362 pop:
7363 lookahead_prec = new_prec;
7364 /* If the stack is not empty, we have parsed into LHS the right side
7365 (`4' in the example above) of an expression we had suspended.
7366 We can use the information on the stack to recover the LHS (`3')
7367 from the stack together with the tree code (`MULT_EXPR'), and
7368 the precedence of the higher level subexpression
7369 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7370 which will be used to actually build the additive expression. */
7371 rhs = current.lhs;
7372 rhs_type = current.lhs_type;
7373 --sp;
7374 current = *sp;
7375 }
7376
7377 /* Undo the disabling of warnings done above. */
7378 if (current.tree_type == TRUTH_ANDIF_EXPR)
7379 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7380 else if (current.tree_type == TRUTH_ORIF_EXPR)
7381 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7382
7383 overload = NULL;
7384 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7385 ERROR_MARK for everything that is not a binary expression.
7386 This makes warn_about_parentheses miss some warnings that
7387 involve unary operators. For unary expressions we should
7388 pass the correct tree_code unless the unary expression was
7389 surrounded by parentheses.
7390 */
7391 if (no_toplevel_fold_p
7392 && lookahead_prec <= current.prec
7393 && sp == stack
7394 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7395 current.lhs = build2 (current.tree_type, boolean_type_node,
7396 current.lhs, rhs);
7397 else
7398 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7399 current.lhs, current.lhs_type,
7400 rhs, rhs_type, &overload,
7401 tf_warning_or_error);
7402 current.lhs_type = current.tree_type;
7403
7404 /* If the binary operator required the use of an overloaded operator,
7405 then this expression cannot be an integral constant-expression.
7406 An overloaded operator can be used even if both operands are
7407 otherwise permissible in an integral constant-expression if at
7408 least one of the operands is of enumeration type. */
7409
7410 if (overload
7411 && cp_parser_non_integral_constant_expression (parser,
7412 NIC_OVERLOADED))
7413 return error_mark_node;
7414 }
7415
7416 return current.lhs;
7417 }
7418
7419
7420 /* Parse the `? expression : assignment-expression' part of a
7421 conditional-expression. The LOGICAL_OR_EXPR is the
7422 logical-or-expression that started the conditional-expression.
7423 Returns a representation of the entire conditional-expression.
7424
7425 This routine is used by cp_parser_assignment_expression.
7426
7427 ? expression : assignment-expression
7428
7429 GNU Extensions:
7430
7431 ? : assignment-expression */
7432
7433 static tree
7434 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7435 {
7436 tree expr;
7437 tree assignment_expr;
7438 struct cp_token *token;
7439 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7440
7441 /* Consume the `?' token. */
7442 cp_lexer_consume_token (parser->lexer);
7443 token = cp_lexer_peek_token (parser->lexer);
7444 if (cp_parser_allow_gnu_extensions_p (parser)
7445 && token->type == CPP_COLON)
7446 {
7447 pedwarn (token->location, OPT_Wpedantic,
7448 "ISO C++ does not allow ?: with omitted middle operand");
7449 /* Implicit true clause. */
7450 expr = NULL_TREE;
7451 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7452 warn_for_omitted_condop (token->location, logical_or_expr);
7453 }
7454 else
7455 {
7456 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7457 parser->colon_corrects_to_scope_p = false;
7458 /* Parse the expression. */
7459 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7460 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7461 c_inhibit_evaluation_warnings +=
7462 ((logical_or_expr == truthvalue_true_node)
7463 - (logical_or_expr == truthvalue_false_node));
7464 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7465 }
7466
7467 /* The next token should be a `:'. */
7468 cp_parser_require (parser, CPP_COLON, RT_COLON);
7469 /* Parse the assignment-expression. */
7470 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7471 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7472
7473 /* Build the conditional-expression. */
7474 return build_x_conditional_expr (loc, logical_or_expr,
7475 expr,
7476 assignment_expr,
7477 tf_warning_or_error);
7478 }
7479
7480 /* Parse an assignment-expression.
7481
7482 assignment-expression:
7483 conditional-expression
7484 logical-or-expression assignment-operator assignment_expression
7485 throw-expression
7486
7487 CAST_P is true if this expression is the target of a cast.
7488
7489 Returns a representation for the expression. */
7490
7491 static tree
7492 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7493 cp_id_kind * pidk)
7494 {
7495 tree expr;
7496
7497 /* If the next token is the `throw' keyword, then we're looking at
7498 a throw-expression. */
7499 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7500 expr = cp_parser_throw_expression (parser);
7501 /* Otherwise, it must be that we are looking at a
7502 logical-or-expression. */
7503 else
7504 {
7505 /* Parse the binary expressions (logical-or-expression). */
7506 expr = cp_parser_binary_expression (parser, cast_p, false,
7507 PREC_NOT_OPERATOR, pidk);
7508 /* If the next token is a `?' then we're actually looking at a
7509 conditional-expression. */
7510 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7511 return cp_parser_question_colon_clause (parser, expr);
7512 else
7513 {
7514 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7515
7516 /* If it's an assignment-operator, we're using the second
7517 production. */
7518 enum tree_code assignment_operator
7519 = cp_parser_assignment_operator_opt (parser);
7520 if (assignment_operator != ERROR_MARK)
7521 {
7522 bool non_constant_p;
7523 location_t saved_input_location;
7524
7525 /* Parse the right-hand side of the assignment. */
7526 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7527
7528 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7529 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7530
7531 /* An assignment may not appear in a
7532 constant-expression. */
7533 if (cp_parser_non_integral_constant_expression (parser,
7534 NIC_ASSIGNMENT))
7535 return error_mark_node;
7536 /* Build the assignment expression. Its default
7537 location is the location of the '=' token. */
7538 saved_input_location = input_location;
7539 input_location = loc;
7540 expr = build_x_modify_expr (loc, expr,
7541 assignment_operator,
7542 rhs,
7543 tf_warning_or_error);
7544 input_location = saved_input_location;
7545 }
7546 }
7547 }
7548
7549 return expr;
7550 }
7551
7552 /* Parse an (optional) assignment-operator.
7553
7554 assignment-operator: one of
7555 = *= /= %= += -= >>= <<= &= ^= |=
7556
7557 GNU Extension:
7558
7559 assignment-operator: one of
7560 <?= >?=
7561
7562 If the next token is an assignment operator, the corresponding tree
7563 code is returned, and the token is consumed. For example, for
7564 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7565 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7566 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7567 operator, ERROR_MARK is returned. */
7568
7569 static enum tree_code
7570 cp_parser_assignment_operator_opt (cp_parser* parser)
7571 {
7572 enum tree_code op;
7573 cp_token *token;
7574
7575 /* Peek at the next token. */
7576 token = cp_lexer_peek_token (parser->lexer);
7577
7578 switch (token->type)
7579 {
7580 case CPP_EQ:
7581 op = NOP_EXPR;
7582 break;
7583
7584 case CPP_MULT_EQ:
7585 op = MULT_EXPR;
7586 break;
7587
7588 case CPP_DIV_EQ:
7589 op = TRUNC_DIV_EXPR;
7590 break;
7591
7592 case CPP_MOD_EQ:
7593 op = TRUNC_MOD_EXPR;
7594 break;
7595
7596 case CPP_PLUS_EQ:
7597 op = PLUS_EXPR;
7598 break;
7599
7600 case CPP_MINUS_EQ:
7601 op = MINUS_EXPR;
7602 break;
7603
7604 case CPP_RSHIFT_EQ:
7605 op = RSHIFT_EXPR;
7606 break;
7607
7608 case CPP_LSHIFT_EQ:
7609 op = LSHIFT_EXPR;
7610 break;
7611
7612 case CPP_AND_EQ:
7613 op = BIT_AND_EXPR;
7614 break;
7615
7616 case CPP_XOR_EQ:
7617 op = BIT_XOR_EXPR;
7618 break;
7619
7620 case CPP_OR_EQ:
7621 op = BIT_IOR_EXPR;
7622 break;
7623
7624 default:
7625 /* Nothing else is an assignment operator. */
7626 op = ERROR_MARK;
7627 }
7628
7629 /* If it was an assignment operator, consume it. */
7630 if (op != ERROR_MARK)
7631 cp_lexer_consume_token (parser->lexer);
7632
7633 return op;
7634 }
7635
7636 /* Parse an expression.
7637
7638 expression:
7639 assignment-expression
7640 expression , assignment-expression
7641
7642 CAST_P is true if this expression is the target of a cast.
7643
7644 Returns a representation of the expression. */
7645
7646 static tree
7647 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7648 {
7649 tree expression = NULL_TREE;
7650 location_t loc = UNKNOWN_LOCATION;
7651
7652 while (true)
7653 {
7654 tree assignment_expression;
7655
7656 /* Parse the next assignment-expression. */
7657 assignment_expression
7658 = cp_parser_assignment_expression (parser, cast_p, pidk);
7659 /* If this is the first assignment-expression, we can just
7660 save it away. */
7661 if (!expression)
7662 expression = assignment_expression;
7663 else
7664 expression = build_x_compound_expr (loc, expression,
7665 assignment_expression,
7666 tf_warning_or_error);
7667 /* If the next token is not a comma, then we are done with the
7668 expression. */
7669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7670 break;
7671 /* Consume the `,'. */
7672 loc = cp_lexer_peek_token (parser->lexer)->location;
7673 cp_lexer_consume_token (parser->lexer);
7674 /* A comma operator cannot appear in a constant-expression. */
7675 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7676 expression = error_mark_node;
7677 }
7678
7679 return expression;
7680 }
7681
7682 /* Parse a constant-expression.
7683
7684 constant-expression:
7685 conditional-expression
7686
7687 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7688 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7689 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7690 is false, NON_CONSTANT_P should be NULL. */
7691
7692 static tree
7693 cp_parser_constant_expression (cp_parser* parser,
7694 bool allow_non_constant_p,
7695 bool *non_constant_p)
7696 {
7697 bool saved_integral_constant_expression_p;
7698 bool saved_allow_non_integral_constant_expression_p;
7699 bool saved_non_integral_constant_expression_p;
7700 tree expression;
7701
7702 /* It might seem that we could simply parse the
7703 conditional-expression, and then check to see if it were
7704 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7705 one that the compiler can figure out is constant, possibly after
7706 doing some simplifications or optimizations. The standard has a
7707 precise definition of constant-expression, and we must honor
7708 that, even though it is somewhat more restrictive.
7709
7710 For example:
7711
7712 int i[(2, 3)];
7713
7714 is not a legal declaration, because `(2, 3)' is not a
7715 constant-expression. The `,' operator is forbidden in a
7716 constant-expression. However, GCC's constant-folding machinery
7717 will fold this operation to an INTEGER_CST for `3'. */
7718
7719 /* Save the old settings. */
7720 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7721 saved_allow_non_integral_constant_expression_p
7722 = parser->allow_non_integral_constant_expression_p;
7723 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7724 /* We are now parsing a constant-expression. */
7725 parser->integral_constant_expression_p = true;
7726 parser->allow_non_integral_constant_expression_p
7727 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7728 parser->non_integral_constant_expression_p = false;
7729 /* Although the grammar says "conditional-expression", we parse an
7730 "assignment-expression", which also permits "throw-expression"
7731 and the use of assignment operators. In the case that
7732 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7733 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7734 actually essential that we look for an assignment-expression.
7735 For example, cp_parser_initializer_clauses uses this function to
7736 determine whether a particular assignment-expression is in fact
7737 constant. */
7738 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7739 /* Restore the old settings. */
7740 parser->integral_constant_expression_p
7741 = saved_integral_constant_expression_p;
7742 parser->allow_non_integral_constant_expression_p
7743 = saved_allow_non_integral_constant_expression_p;
7744 if (cxx_dialect >= cxx0x)
7745 {
7746 /* Require an rvalue constant expression here; that's what our
7747 callers expect. Reference constant expressions are handled
7748 separately in e.g. cp_parser_template_argument. */
7749 bool is_const = potential_rvalue_constant_expression (expression);
7750 parser->non_integral_constant_expression_p = !is_const;
7751 if (!is_const && !allow_non_constant_p)
7752 require_potential_rvalue_constant_expression (expression);
7753 }
7754 if (allow_non_constant_p)
7755 *non_constant_p = parser->non_integral_constant_expression_p;
7756 parser->non_integral_constant_expression_p
7757 = saved_non_integral_constant_expression_p;
7758
7759 return expression;
7760 }
7761
7762 /* Parse __builtin_offsetof.
7763
7764 offsetof-expression:
7765 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7766
7767 offsetof-member-designator:
7768 id-expression
7769 | offsetof-member-designator "." id-expression
7770 | offsetof-member-designator "[" expression "]"
7771 | offsetof-member-designator "->" id-expression */
7772
7773 static tree
7774 cp_parser_builtin_offsetof (cp_parser *parser)
7775 {
7776 int save_ice_p, save_non_ice_p;
7777 tree type, expr;
7778 cp_id_kind dummy;
7779 cp_token *token;
7780
7781 /* We're about to accept non-integral-constant things, but will
7782 definitely yield an integral constant expression. Save and
7783 restore these values around our local parsing. */
7784 save_ice_p = parser->integral_constant_expression_p;
7785 save_non_ice_p = parser->non_integral_constant_expression_p;
7786
7787 /* Consume the "__builtin_offsetof" token. */
7788 cp_lexer_consume_token (parser->lexer);
7789 /* Consume the opening `('. */
7790 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7791 /* Parse the type-id. */
7792 type = cp_parser_type_id (parser);
7793 /* Look for the `,'. */
7794 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7795 token = cp_lexer_peek_token (parser->lexer);
7796
7797 /* Build the (type *)null that begins the traditional offsetof macro. */
7798 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7799 tf_warning_or_error);
7800
7801 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7802 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7803 true, &dummy, token->location);
7804 while (true)
7805 {
7806 token = cp_lexer_peek_token (parser->lexer);
7807 switch (token->type)
7808 {
7809 case CPP_OPEN_SQUARE:
7810 /* offsetof-member-designator "[" expression "]" */
7811 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7812 break;
7813
7814 case CPP_DEREF:
7815 /* offsetof-member-designator "->" identifier */
7816 expr = grok_array_decl (token->location, expr, integer_zero_node);
7817 /* FALLTHRU */
7818
7819 case CPP_DOT:
7820 /* offsetof-member-designator "." identifier */
7821 cp_lexer_consume_token (parser->lexer);
7822 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7823 expr, true, &dummy,
7824 token->location);
7825 break;
7826
7827 case CPP_CLOSE_PAREN:
7828 /* Consume the ")" token. */
7829 cp_lexer_consume_token (parser->lexer);
7830 goto success;
7831
7832 default:
7833 /* Error. We know the following require will fail, but
7834 that gives the proper error message. */
7835 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7836 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7837 expr = error_mark_node;
7838 goto failure;
7839 }
7840 }
7841
7842 success:
7843 /* If we're processing a template, we can't finish the semantics yet.
7844 Otherwise we can fold the entire expression now. */
7845 if (processing_template_decl)
7846 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7847 else
7848 expr = finish_offsetof (expr);
7849
7850 failure:
7851 parser->integral_constant_expression_p = save_ice_p;
7852 parser->non_integral_constant_expression_p = save_non_ice_p;
7853
7854 return expr;
7855 }
7856
7857 /* Parse a trait expression.
7858
7859 Returns a representation of the expression, the underlying type
7860 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7861
7862 static tree
7863 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7864 {
7865 cp_trait_kind kind;
7866 tree type1, type2 = NULL_TREE;
7867 bool binary = false;
7868 cp_decl_specifier_seq decl_specs;
7869
7870 switch (keyword)
7871 {
7872 case RID_HAS_NOTHROW_ASSIGN:
7873 kind = CPTK_HAS_NOTHROW_ASSIGN;
7874 break;
7875 case RID_HAS_NOTHROW_CONSTRUCTOR:
7876 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7877 break;
7878 case RID_HAS_NOTHROW_COPY:
7879 kind = CPTK_HAS_NOTHROW_COPY;
7880 break;
7881 case RID_HAS_TRIVIAL_ASSIGN:
7882 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7883 break;
7884 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7885 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7886 break;
7887 case RID_HAS_TRIVIAL_COPY:
7888 kind = CPTK_HAS_TRIVIAL_COPY;
7889 break;
7890 case RID_HAS_TRIVIAL_DESTRUCTOR:
7891 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7892 break;
7893 case RID_HAS_VIRTUAL_DESTRUCTOR:
7894 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7895 break;
7896 case RID_IS_ABSTRACT:
7897 kind = CPTK_IS_ABSTRACT;
7898 break;
7899 case RID_IS_BASE_OF:
7900 kind = CPTK_IS_BASE_OF;
7901 binary = true;
7902 break;
7903 case RID_IS_CLASS:
7904 kind = CPTK_IS_CLASS;
7905 break;
7906 case RID_IS_CONVERTIBLE_TO:
7907 kind = CPTK_IS_CONVERTIBLE_TO;
7908 binary = true;
7909 break;
7910 case RID_IS_EMPTY:
7911 kind = CPTK_IS_EMPTY;
7912 break;
7913 case RID_IS_ENUM:
7914 kind = CPTK_IS_ENUM;
7915 break;
7916 case RID_IS_FINAL:
7917 kind = CPTK_IS_FINAL;
7918 break;
7919 case RID_IS_LITERAL_TYPE:
7920 kind = CPTK_IS_LITERAL_TYPE;
7921 break;
7922 case RID_IS_POD:
7923 kind = CPTK_IS_POD;
7924 break;
7925 case RID_IS_POLYMORPHIC:
7926 kind = CPTK_IS_POLYMORPHIC;
7927 break;
7928 case RID_IS_STD_LAYOUT:
7929 kind = CPTK_IS_STD_LAYOUT;
7930 break;
7931 case RID_IS_TRIVIAL:
7932 kind = CPTK_IS_TRIVIAL;
7933 break;
7934 case RID_IS_UNION:
7935 kind = CPTK_IS_UNION;
7936 break;
7937 case RID_UNDERLYING_TYPE:
7938 kind = CPTK_UNDERLYING_TYPE;
7939 break;
7940 case RID_BASES:
7941 kind = CPTK_BASES;
7942 break;
7943 case RID_DIRECT_BASES:
7944 kind = CPTK_DIRECT_BASES;
7945 break;
7946 default:
7947 gcc_unreachable ();
7948 }
7949
7950 /* Consume the token. */
7951 cp_lexer_consume_token (parser->lexer);
7952
7953 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7954
7955 type1 = cp_parser_type_id (parser);
7956
7957 if (type1 == error_mark_node)
7958 return error_mark_node;
7959
7960 /* Build a trivial decl-specifier-seq. */
7961 clear_decl_specs (&decl_specs);
7962 decl_specs.type = type1;
7963
7964 /* Call grokdeclarator to figure out what type this is. */
7965 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7966 /*initialized=*/0, /*attrlist=*/NULL);
7967
7968 if (binary)
7969 {
7970 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7971
7972 type2 = cp_parser_type_id (parser);
7973
7974 if (type2 == error_mark_node)
7975 return error_mark_node;
7976
7977 /* Build a trivial decl-specifier-seq. */
7978 clear_decl_specs (&decl_specs);
7979 decl_specs.type = type2;
7980
7981 /* Call grokdeclarator to figure out what type this is. */
7982 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7983 /*initialized=*/0, /*attrlist=*/NULL);
7984 }
7985
7986 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7987
7988 /* Complete the trait expression, which may mean either processing
7989 the trait expr now or saving it for template instantiation. */
7990 switch(kind)
7991 {
7992 case CPTK_UNDERLYING_TYPE:
7993 return finish_underlying_type (type1);
7994 case CPTK_BASES:
7995 return finish_bases (type1, false);
7996 case CPTK_DIRECT_BASES:
7997 return finish_bases (type1, true);
7998 default:
7999 return finish_trait_expr (kind, type1, type2);
8000 }
8001 }
8002
8003 /* Lambdas that appear in variable initializer or default argument scope
8004 get that in their mangling, so we need to record it. We might as well
8005 use the count for function and namespace scopes as well. */
8006 static GTY(()) tree lambda_scope;
8007 static GTY(()) int lambda_count;
8008 typedef struct GTY(()) tree_int
8009 {
8010 tree t;
8011 int i;
8012 } tree_int;
8013 DEF_VEC_O(tree_int);
8014 DEF_VEC_ALLOC_O(tree_int,gc);
8015 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8016
8017 static void
8018 start_lambda_scope (tree decl)
8019 {
8020 tree_int ti;
8021 gcc_assert (decl);
8022 /* Once we're inside a function, we ignore other scopes and just push
8023 the function again so that popping works properly. */
8024 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8025 decl = current_function_decl;
8026 ti.t = lambda_scope;
8027 ti.i = lambda_count;
8028 VEC_safe_push (tree_int, gc, lambda_scope_stack, ti);
8029 if (lambda_scope != decl)
8030 {
8031 /* Don't reset the count if we're still in the same function. */
8032 lambda_scope = decl;
8033 lambda_count = 0;
8034 }
8035 }
8036
8037 static void
8038 record_lambda_scope (tree lambda)
8039 {
8040 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8041 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8042 }
8043
8044 static void
8045 finish_lambda_scope (void)
8046 {
8047 tree_int *p = &VEC_last (tree_int, lambda_scope_stack);
8048 if (lambda_scope != p->t)
8049 {
8050 lambda_scope = p->t;
8051 lambda_count = p->i;
8052 }
8053 VEC_pop (tree_int, lambda_scope_stack);
8054 }
8055
8056 /* Parse a lambda expression.
8057
8058 lambda-expression:
8059 lambda-introducer lambda-declarator [opt] compound-statement
8060
8061 Returns a representation of the expression. */
8062
8063 static tree
8064 cp_parser_lambda_expression (cp_parser* parser)
8065 {
8066 tree lambda_expr = build_lambda_expr ();
8067 tree type;
8068 bool ok;
8069
8070 LAMBDA_EXPR_LOCATION (lambda_expr)
8071 = cp_lexer_peek_token (parser->lexer)->location;
8072
8073 if (cp_unevaluated_operand)
8074 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8075 "lambda-expression in unevaluated context");
8076
8077 /* We may be in the middle of deferred access check. Disable
8078 it now. */
8079 push_deferring_access_checks (dk_no_deferred);
8080
8081 cp_parser_lambda_introducer (parser, lambda_expr);
8082
8083 type = begin_lambda_type (lambda_expr);
8084 if (type == error_mark_node)
8085 return error_mark_node;
8086
8087 record_lambda_scope (lambda_expr);
8088
8089 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8090 determine_visibility (TYPE_NAME (type));
8091
8092 /* Now that we've started the type, add the capture fields for any
8093 explicit captures. */
8094 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8095
8096 {
8097 /* Inside the class, surrounding template-parameter-lists do not apply. */
8098 unsigned int saved_num_template_parameter_lists
8099 = parser->num_template_parameter_lists;
8100 unsigned char in_statement = parser->in_statement;
8101 bool in_switch_statement_p = parser->in_switch_statement_p;
8102
8103 parser->num_template_parameter_lists = 0;
8104 parser->in_statement = 0;
8105 parser->in_switch_statement_p = false;
8106
8107 /* By virtue of defining a local class, a lambda expression has access to
8108 the private variables of enclosing classes. */
8109
8110 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8111
8112 if (ok)
8113 cp_parser_lambda_body (parser, lambda_expr);
8114 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8115 cp_parser_skip_to_end_of_block_or_statement (parser);
8116
8117 /* The capture list was built up in reverse order; fix that now. */
8118 {
8119 tree newlist = NULL_TREE;
8120 tree elt, next;
8121
8122 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8123 elt; elt = next)
8124 {
8125 next = TREE_CHAIN (elt);
8126 TREE_CHAIN (elt) = newlist;
8127 newlist = elt;
8128 }
8129 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8130 }
8131
8132 if (ok)
8133 maybe_add_lambda_conv_op (type);
8134
8135 type = finish_struct (type, /*attributes=*/NULL_TREE);
8136
8137 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8138 parser->in_statement = in_statement;
8139 parser->in_switch_statement_p = in_switch_statement_p;
8140 }
8141
8142 pop_deferring_access_checks ();
8143
8144 /* This field is only used during parsing of the lambda. */
8145 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8146
8147 /* This lambda shouldn't have any proxies left at this point. */
8148 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8149 /* And now that we're done, push proxies for an enclosing lambda. */
8150 insert_pending_capture_proxies ();
8151
8152 if (ok)
8153 return build_lambda_object (lambda_expr);
8154 else
8155 return error_mark_node;
8156 }
8157
8158 /* Parse the beginning of a lambda expression.
8159
8160 lambda-introducer:
8161 [ lambda-capture [opt] ]
8162
8163 LAMBDA_EXPR is the current representation of the lambda expression. */
8164
8165 static void
8166 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8167 {
8168 /* Need commas after the first capture. */
8169 bool first = true;
8170
8171 /* Eat the leading `['. */
8172 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8173
8174 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8175 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8176 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8177 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8178 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8179 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8180
8181 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8182 {
8183 cp_lexer_consume_token (parser->lexer);
8184 first = false;
8185 }
8186
8187 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8188 {
8189 cp_token* capture_token;
8190 tree capture_id;
8191 tree capture_init_expr;
8192 cp_id_kind idk = CP_ID_KIND_NONE;
8193 bool explicit_init_p = false;
8194
8195 enum capture_kind_type
8196 {
8197 BY_COPY,
8198 BY_REFERENCE
8199 };
8200 enum capture_kind_type capture_kind = BY_COPY;
8201
8202 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8203 {
8204 error ("expected end of capture-list");
8205 return;
8206 }
8207
8208 if (first)
8209 first = false;
8210 else
8211 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8212
8213 /* Possibly capture `this'. */
8214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8215 {
8216 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8217 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8218 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8219 "with by-copy capture default");
8220 cp_lexer_consume_token (parser->lexer);
8221 add_capture (lambda_expr,
8222 /*id=*/this_identifier,
8223 /*initializer=*/finish_this_expr(),
8224 /*by_reference_p=*/false,
8225 explicit_init_p);
8226 continue;
8227 }
8228
8229 /* Remember whether we want to capture as a reference or not. */
8230 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8231 {
8232 capture_kind = BY_REFERENCE;
8233 cp_lexer_consume_token (parser->lexer);
8234 }
8235
8236 /* Get the identifier. */
8237 capture_token = cp_lexer_peek_token (parser->lexer);
8238 capture_id = cp_parser_identifier (parser);
8239
8240 if (capture_id == error_mark_node)
8241 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8242 delimiters, but I modified this to stop on unnested ']' as well. It
8243 was already changed to stop on unnested '}', so the
8244 "closing_parenthesis" name is no more misleading with my change. */
8245 {
8246 cp_parser_skip_to_closing_parenthesis (parser,
8247 /*recovering=*/true,
8248 /*or_comma=*/true,
8249 /*consume_paren=*/true);
8250 break;
8251 }
8252
8253 /* Find the initializer for this capture. */
8254 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8255 {
8256 /* An explicit expression exists. */
8257 cp_lexer_consume_token (parser->lexer);
8258 pedwarn (input_location, OPT_Wpedantic,
8259 "ISO C++ does not allow initializers "
8260 "in lambda expression capture lists");
8261 capture_init_expr = cp_parser_assignment_expression (parser,
8262 /*cast_p=*/true,
8263 &idk);
8264 explicit_init_p = true;
8265 }
8266 else
8267 {
8268 const char* error_msg;
8269
8270 /* Turn the identifier into an id-expression. */
8271 capture_init_expr
8272 = cp_parser_lookup_name
8273 (parser,
8274 capture_id,
8275 none_type,
8276 /*is_template=*/false,
8277 /*is_namespace=*/false,
8278 /*check_dependency=*/true,
8279 /*ambiguous_decls=*/NULL,
8280 capture_token->location);
8281
8282 if (capture_init_expr == error_mark_node)
8283 {
8284 unqualified_name_lookup_error (capture_id);
8285 continue;
8286 }
8287 else if (DECL_P (capture_init_expr)
8288 && (TREE_CODE (capture_init_expr) != VAR_DECL
8289 && TREE_CODE (capture_init_expr) != PARM_DECL))
8290 {
8291 error_at (capture_token->location,
8292 "capture of non-variable %qD ",
8293 capture_init_expr);
8294 inform (0, "%q+#D declared here", capture_init_expr);
8295 continue;
8296 }
8297 if (TREE_CODE (capture_init_expr) == VAR_DECL
8298 && decl_storage_duration (capture_init_expr) != dk_auto)
8299 {
8300 pedwarn (capture_token->location, 0, "capture of variable "
8301 "%qD with non-automatic storage duration",
8302 capture_init_expr);
8303 inform (0, "%q+#D declared here", capture_init_expr);
8304 continue;
8305 }
8306
8307 capture_init_expr
8308 = finish_id_expression
8309 (capture_id,
8310 capture_init_expr,
8311 parser->scope,
8312 &idk,
8313 /*integral_constant_expression_p=*/false,
8314 /*allow_non_integral_constant_expression_p=*/false,
8315 /*non_integral_constant_expression_p=*/NULL,
8316 /*template_p=*/false,
8317 /*done=*/true,
8318 /*address_p=*/false,
8319 /*template_arg_p=*/false,
8320 &error_msg,
8321 capture_token->location);
8322 }
8323
8324 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8325 && !explicit_init_p)
8326 {
8327 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8328 && capture_kind == BY_COPY)
8329 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8330 "of %qD redundant with by-copy capture default",
8331 capture_id);
8332 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8333 && capture_kind == BY_REFERENCE)
8334 pedwarn (capture_token->location, 0, "explicit by-reference "
8335 "capture of %qD redundant with by-reference capture "
8336 "default", capture_id);
8337 }
8338
8339 add_capture (lambda_expr,
8340 capture_id,
8341 capture_init_expr,
8342 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8343 explicit_init_p);
8344 }
8345
8346 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8347 }
8348
8349 /* Parse the (optional) middle of a lambda expression.
8350
8351 lambda-declarator:
8352 ( parameter-declaration-clause [opt] )
8353 attribute-specifier [opt]
8354 mutable [opt]
8355 exception-specification [opt]
8356 lambda-return-type-clause [opt]
8357
8358 LAMBDA_EXPR is the current representation of the lambda expression. */
8359
8360 static bool
8361 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8362 {
8363 /* 5.1.1.4 of the standard says:
8364 If a lambda-expression does not include a lambda-declarator, it is as if
8365 the lambda-declarator were ().
8366 This means an empty parameter list, no attributes, and no exception
8367 specification. */
8368 tree param_list = void_list_node;
8369 tree attributes = NULL_TREE;
8370 tree exception_spec = NULL_TREE;
8371 tree t;
8372
8373 /* The lambda-declarator is optional, but must begin with an opening
8374 parenthesis if present. */
8375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8376 {
8377 cp_lexer_consume_token (parser->lexer);
8378
8379 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8380
8381 /* Parse parameters. */
8382 param_list = cp_parser_parameter_declaration_clause (parser);
8383
8384 /* Default arguments shall not be specified in the
8385 parameter-declaration-clause of a lambda-declarator. */
8386 for (t = param_list; t; t = TREE_CHAIN (t))
8387 if (TREE_PURPOSE (t))
8388 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8389 "default argument specified for lambda parameter");
8390
8391 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8392
8393 attributes = cp_parser_attributes_opt (parser);
8394
8395 /* Parse optional `mutable' keyword. */
8396 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8397 {
8398 cp_lexer_consume_token (parser->lexer);
8399 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8400 }
8401
8402 /* Parse optional exception specification. */
8403 exception_spec = cp_parser_exception_specification_opt (parser);
8404
8405 /* Parse optional trailing return type. */
8406 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8407 {
8408 cp_lexer_consume_token (parser->lexer);
8409 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8410 }
8411
8412 /* The function parameters must be in scope all the way until after the
8413 trailing-return-type in case of decltype. */
8414 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8415 pop_binding (DECL_NAME (t), t);
8416
8417 leave_scope ();
8418 }
8419
8420 /* Create the function call operator.
8421
8422 Messing with declarators like this is no uglier than building up the
8423 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8424 other code. */
8425 {
8426 cp_decl_specifier_seq return_type_specs;
8427 cp_declarator* declarator;
8428 tree fco;
8429 int quals;
8430 void *p;
8431
8432 clear_decl_specs (&return_type_specs);
8433 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8434 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8435 else
8436 /* Maybe we will deduce the return type later. */
8437 return_type_specs.type = make_auto ();
8438
8439 p = obstack_alloc (&declarator_obstack, 0);
8440
8441 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8442 sfk_none);
8443
8444 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8445 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8446 declarator = make_call_declarator (declarator, param_list, quals,
8447 VIRT_SPEC_UNSPECIFIED,
8448 exception_spec,
8449 /*late_return_type=*/NULL_TREE);
8450 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8451
8452 fco = grokmethod (&return_type_specs,
8453 declarator,
8454 attributes);
8455 if (fco != error_mark_node)
8456 {
8457 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8458 DECL_ARTIFICIAL (fco) = 1;
8459 /* Give the object parameter a different name. */
8460 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8461 }
8462
8463 finish_member_declaration (fco);
8464
8465 obstack_free (&declarator_obstack, p);
8466
8467 return (fco != error_mark_node);
8468 }
8469 }
8470
8471 /* Parse the body of a lambda expression, which is simply
8472
8473 compound-statement
8474
8475 but which requires special handling.
8476 LAMBDA_EXPR is the current representation of the lambda expression. */
8477
8478 static void
8479 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8480 {
8481 bool nested = (current_function_decl != NULL_TREE);
8482 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8483 if (nested)
8484 push_function_context ();
8485 else
8486 /* Still increment function_depth so that we don't GC in the
8487 middle of an expression. */
8488 ++function_depth;
8489 /* Clear this in case we're in the middle of a default argument. */
8490 parser->local_variables_forbidden_p = false;
8491
8492 /* Finish the function call operator
8493 - class_specifier
8494 + late_parsing_for_member
8495 + function_definition_after_declarator
8496 + ctor_initializer_opt_and_function_body */
8497 {
8498 tree fco = lambda_function (lambda_expr);
8499 tree body;
8500 bool done = false;
8501 tree compound_stmt;
8502 tree cap;
8503
8504 /* Let the front end know that we are going to be defining this
8505 function. */
8506 start_preparsed_function (fco,
8507 NULL_TREE,
8508 SF_PRE_PARSED | SF_INCLASS_INLINE);
8509
8510 start_lambda_scope (fco);
8511 body = begin_function_body ();
8512
8513 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8514 goto out;
8515
8516 /* Push the proxies for any explicit captures. */
8517 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8518 cap = TREE_CHAIN (cap))
8519 build_capture_proxy (TREE_PURPOSE (cap));
8520
8521 compound_stmt = begin_compound_stmt (0);
8522
8523 /* 5.1.1.4 of the standard says:
8524 If a lambda-expression does not include a trailing-return-type, it
8525 is as if the trailing-return-type denotes the following type:
8526 * if the compound-statement is of the form
8527 { return attribute-specifier [opt] expression ; }
8528 the type of the returned expression after lvalue-to-rvalue
8529 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8530 (_conv.array_ 4.2), and function-to-pointer conversion
8531 (_conv.func_ 4.3);
8532 * otherwise, void. */
8533
8534 /* In a lambda that has neither a lambda-return-type-clause
8535 nor a deducible form, errors should be reported for return statements
8536 in the body. Since we used void as the placeholder return type, parsing
8537 the body as usual will give such desired behavior. */
8538 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8539 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8540 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8541 {
8542 tree expr = NULL_TREE;
8543 cp_id_kind idk = CP_ID_KIND_NONE;
8544
8545 /* Parse tentatively in case there's more after the initial return
8546 statement. */
8547 cp_parser_parse_tentatively (parser);
8548
8549 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8550
8551 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8552
8553 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8554 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8555
8556 if (cp_parser_parse_definitely (parser))
8557 {
8558 if (!processing_template_decl)
8559 apply_deduced_return_type (fco, lambda_return_type (expr));
8560
8561 /* Will get error here if type not deduced yet. */
8562 finish_return_stmt (expr);
8563
8564 done = true;
8565 }
8566 }
8567
8568 if (!done)
8569 {
8570 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8571 cp_parser_label_declaration (parser);
8572 cp_parser_statement_seq_opt (parser, NULL_TREE);
8573 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8574 }
8575
8576 finish_compound_stmt (compound_stmt);
8577
8578 out:
8579 finish_function_body (body);
8580 finish_lambda_scope ();
8581
8582 /* Finish the function and generate code for it if necessary. */
8583 expand_or_defer_fn (finish_function (/*inline*/2));
8584 }
8585
8586 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8587 if (nested)
8588 pop_function_context();
8589 else
8590 --function_depth;
8591 }
8592
8593 /* Statements [gram.stmt.stmt] */
8594
8595 /* Parse a statement.
8596
8597 statement:
8598 labeled-statement
8599 expression-statement
8600 compound-statement
8601 selection-statement
8602 iteration-statement
8603 jump-statement
8604 declaration-statement
8605 try-block
8606
8607 TM Extension:
8608
8609 statement:
8610 atomic-statement
8611
8612 IN_COMPOUND is true when the statement is nested inside a
8613 cp_parser_compound_statement; this matters for certain pragmas.
8614
8615 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8616 is a (possibly labeled) if statement which is not enclosed in braces
8617 and has an else clause. This is used to implement -Wparentheses. */
8618
8619 static void
8620 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8621 bool in_compound, bool *if_p)
8622 {
8623 tree statement;
8624 cp_token *token;
8625 location_t statement_location;
8626
8627 restart:
8628 if (if_p != NULL)
8629 *if_p = false;
8630 /* There is no statement yet. */
8631 statement = NULL_TREE;
8632 /* Peek at the next token. */
8633 token = cp_lexer_peek_token (parser->lexer);
8634 /* Remember the location of the first token in the statement. */
8635 statement_location = token->location;
8636 /* If this is a keyword, then that will often determine what kind of
8637 statement we have. */
8638 if (token->type == CPP_KEYWORD)
8639 {
8640 enum rid keyword = token->keyword;
8641
8642 switch (keyword)
8643 {
8644 case RID_CASE:
8645 case RID_DEFAULT:
8646 /* Looks like a labeled-statement with a case label.
8647 Parse the label, and then use tail recursion to parse
8648 the statement. */
8649 cp_parser_label_for_labeled_statement (parser);
8650 goto restart;
8651
8652 case RID_IF:
8653 case RID_SWITCH:
8654 statement = cp_parser_selection_statement (parser, if_p);
8655 break;
8656
8657 case RID_WHILE:
8658 case RID_DO:
8659 case RID_FOR:
8660 statement = cp_parser_iteration_statement (parser);
8661 break;
8662
8663 case RID_BREAK:
8664 case RID_CONTINUE:
8665 case RID_RETURN:
8666 case RID_GOTO:
8667 statement = cp_parser_jump_statement (parser);
8668 break;
8669
8670 /* Objective-C++ exception-handling constructs. */
8671 case RID_AT_TRY:
8672 case RID_AT_CATCH:
8673 case RID_AT_FINALLY:
8674 case RID_AT_SYNCHRONIZED:
8675 case RID_AT_THROW:
8676 statement = cp_parser_objc_statement (parser);
8677 break;
8678
8679 case RID_TRY:
8680 statement = cp_parser_try_block (parser);
8681 break;
8682
8683 case RID_NAMESPACE:
8684 /* This must be a namespace alias definition. */
8685 cp_parser_declaration_statement (parser);
8686 return;
8687
8688 case RID_TRANSACTION_ATOMIC:
8689 case RID_TRANSACTION_RELAXED:
8690 statement = cp_parser_transaction (parser, keyword);
8691 break;
8692 case RID_TRANSACTION_CANCEL:
8693 statement = cp_parser_transaction_cancel (parser);
8694 break;
8695
8696 default:
8697 /* It might be a keyword like `int' that can start a
8698 declaration-statement. */
8699 break;
8700 }
8701 }
8702 else if (token->type == CPP_NAME)
8703 {
8704 /* If the next token is a `:', then we are looking at a
8705 labeled-statement. */
8706 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8707 if (token->type == CPP_COLON)
8708 {
8709 /* Looks like a labeled-statement with an ordinary label.
8710 Parse the label, and then use tail recursion to parse
8711 the statement. */
8712 cp_parser_label_for_labeled_statement (parser);
8713 goto restart;
8714 }
8715 }
8716 /* Anything that starts with a `{' must be a compound-statement. */
8717 else if (token->type == CPP_OPEN_BRACE)
8718 statement = cp_parser_compound_statement (parser, NULL, false, false);
8719 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8720 a statement all its own. */
8721 else if (token->type == CPP_PRAGMA)
8722 {
8723 /* Only certain OpenMP pragmas are attached to statements, and thus
8724 are considered statements themselves. All others are not. In
8725 the context of a compound, accept the pragma as a "statement" and
8726 return so that we can check for a close brace. Otherwise we
8727 require a real statement and must go back and read one. */
8728 if (in_compound)
8729 cp_parser_pragma (parser, pragma_compound);
8730 else if (!cp_parser_pragma (parser, pragma_stmt))
8731 goto restart;
8732 return;
8733 }
8734 else if (token->type == CPP_EOF)
8735 {
8736 cp_parser_error (parser, "expected statement");
8737 return;
8738 }
8739
8740 /* Everything else must be a declaration-statement or an
8741 expression-statement. Try for the declaration-statement
8742 first, unless we are looking at a `;', in which case we know that
8743 we have an expression-statement. */
8744 if (!statement)
8745 {
8746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8747 {
8748 cp_parser_parse_tentatively (parser);
8749 /* Try to parse the declaration-statement. */
8750 cp_parser_declaration_statement (parser);
8751 /* If that worked, we're done. */
8752 if (cp_parser_parse_definitely (parser))
8753 return;
8754 }
8755 /* Look for an expression-statement instead. */
8756 statement = cp_parser_expression_statement (parser, in_statement_expr);
8757 }
8758
8759 /* Set the line number for the statement. */
8760 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8761 SET_EXPR_LOCATION (statement, statement_location);
8762 }
8763
8764 /* Parse the label for a labeled-statement, i.e.
8765
8766 identifier :
8767 case constant-expression :
8768 default :
8769
8770 GNU Extension:
8771 case constant-expression ... constant-expression : statement
8772
8773 When a label is parsed without errors, the label is added to the
8774 parse tree by the finish_* functions, so this function doesn't
8775 have to return the label. */
8776
8777 static void
8778 cp_parser_label_for_labeled_statement (cp_parser* parser)
8779 {
8780 cp_token *token;
8781 tree label = NULL_TREE;
8782 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8783
8784 /* The next token should be an identifier. */
8785 token = cp_lexer_peek_token (parser->lexer);
8786 if (token->type != CPP_NAME
8787 && token->type != CPP_KEYWORD)
8788 {
8789 cp_parser_error (parser, "expected labeled-statement");
8790 return;
8791 }
8792
8793 parser->colon_corrects_to_scope_p = false;
8794 switch (token->keyword)
8795 {
8796 case RID_CASE:
8797 {
8798 tree expr, expr_hi;
8799 cp_token *ellipsis;
8800
8801 /* Consume the `case' token. */
8802 cp_lexer_consume_token (parser->lexer);
8803 /* Parse the constant-expression. */
8804 expr = cp_parser_constant_expression (parser,
8805 /*allow_non_constant_p=*/false,
8806 NULL);
8807
8808 ellipsis = cp_lexer_peek_token (parser->lexer);
8809 if (ellipsis->type == CPP_ELLIPSIS)
8810 {
8811 /* Consume the `...' token. */
8812 cp_lexer_consume_token (parser->lexer);
8813 expr_hi =
8814 cp_parser_constant_expression (parser,
8815 /*allow_non_constant_p=*/false,
8816 NULL);
8817 /* We don't need to emit warnings here, as the common code
8818 will do this for us. */
8819 }
8820 else
8821 expr_hi = NULL_TREE;
8822
8823 if (parser->in_switch_statement_p)
8824 finish_case_label (token->location, expr, expr_hi);
8825 else
8826 error_at (token->location,
8827 "case label %qE not within a switch statement",
8828 expr);
8829 }
8830 break;
8831
8832 case RID_DEFAULT:
8833 /* Consume the `default' token. */
8834 cp_lexer_consume_token (parser->lexer);
8835
8836 if (parser->in_switch_statement_p)
8837 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8838 else
8839 error_at (token->location, "case label not within a switch statement");
8840 break;
8841
8842 default:
8843 /* Anything else must be an ordinary label. */
8844 label = finish_label_stmt (cp_parser_identifier (parser));
8845 break;
8846 }
8847
8848 /* Require the `:' token. */
8849 cp_parser_require (parser, CPP_COLON, RT_COLON);
8850
8851 /* An ordinary label may optionally be followed by attributes.
8852 However, this is only permitted if the attributes are then
8853 followed by a semicolon. This is because, for backward
8854 compatibility, when parsing
8855 lab: __attribute__ ((unused)) int i;
8856 we want the attribute to attach to "i", not "lab". */
8857 if (label != NULL_TREE
8858 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8859 {
8860 tree attrs;
8861
8862 cp_parser_parse_tentatively (parser);
8863 attrs = cp_parser_attributes_opt (parser);
8864 if (attrs == NULL_TREE
8865 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8866 cp_parser_abort_tentative_parse (parser);
8867 else if (!cp_parser_parse_definitely (parser))
8868 ;
8869 else
8870 cplus_decl_attributes (&label, attrs, 0);
8871 }
8872
8873 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8874 }
8875
8876 /* Parse an expression-statement.
8877
8878 expression-statement:
8879 expression [opt] ;
8880
8881 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8882 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8883 indicates whether this expression-statement is part of an
8884 expression statement. */
8885
8886 static tree
8887 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8888 {
8889 tree statement = NULL_TREE;
8890 cp_token *token = cp_lexer_peek_token (parser->lexer);
8891
8892 /* If the next token is a ';', then there is no expression
8893 statement. */
8894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8895 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8896
8897 /* Give a helpful message for "A<T>::type t;" and the like. */
8898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8899 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8900 {
8901 if (TREE_CODE (statement) == SCOPE_REF)
8902 error_at (token->location, "need %<typename%> before %qE because "
8903 "%qT is a dependent scope",
8904 statement, TREE_OPERAND (statement, 0));
8905 else if (is_overloaded_fn (statement)
8906 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8907 {
8908 /* A::A a; */
8909 tree fn = get_first_fn (statement);
8910 error_at (token->location,
8911 "%<%T::%D%> names the constructor, not the type",
8912 DECL_CONTEXT (fn), DECL_NAME (fn));
8913 }
8914 }
8915
8916 /* Consume the final `;'. */
8917 cp_parser_consume_semicolon_at_end_of_statement (parser);
8918
8919 if (in_statement_expr
8920 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8921 /* This is the final expression statement of a statement
8922 expression. */
8923 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8924 else if (statement)
8925 statement = finish_expr_stmt (statement);
8926 else
8927 finish_stmt ();
8928
8929 return statement;
8930 }
8931
8932 /* Parse a compound-statement.
8933
8934 compound-statement:
8935 { statement-seq [opt] }
8936
8937 GNU extension:
8938
8939 compound-statement:
8940 { label-declaration-seq [opt] statement-seq [opt] }
8941
8942 label-declaration-seq:
8943 label-declaration
8944 label-declaration-seq label-declaration
8945
8946 Returns a tree representing the statement. */
8947
8948 static tree
8949 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8950 bool in_try, bool function_body)
8951 {
8952 tree compound_stmt;
8953
8954 /* Consume the `{'. */
8955 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8956 return error_mark_node;
8957 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8958 && !function_body)
8959 pedwarn (input_location, OPT_Wpedantic,
8960 "compound-statement in constexpr function");
8961 /* Begin the compound-statement. */
8962 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8963 /* If the next keyword is `__label__' we have a label declaration. */
8964 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8965 cp_parser_label_declaration (parser);
8966 /* Parse an (optional) statement-seq. */
8967 cp_parser_statement_seq_opt (parser, in_statement_expr);
8968 /* Finish the compound-statement. */
8969 finish_compound_stmt (compound_stmt);
8970 /* Consume the `}'. */
8971 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8972
8973 return compound_stmt;
8974 }
8975
8976 /* Parse an (optional) statement-seq.
8977
8978 statement-seq:
8979 statement
8980 statement-seq [opt] statement */
8981
8982 static void
8983 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8984 {
8985 /* Scan statements until there aren't any more. */
8986 while (true)
8987 {
8988 cp_token *token = cp_lexer_peek_token (parser->lexer);
8989
8990 /* If we are looking at a `}', then we have run out of
8991 statements; the same is true if we have reached the end
8992 of file, or have stumbled upon a stray '@end'. */
8993 if (token->type == CPP_CLOSE_BRACE
8994 || token->type == CPP_EOF
8995 || token->type == CPP_PRAGMA_EOL
8996 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8997 break;
8998
8999 /* If we are in a compound statement and find 'else' then
9000 something went wrong. */
9001 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9002 {
9003 if (parser->in_statement & IN_IF_STMT)
9004 break;
9005 else
9006 {
9007 token = cp_lexer_consume_token (parser->lexer);
9008 error_at (token->location, "%<else%> without a previous %<if%>");
9009 }
9010 }
9011
9012 /* Parse the statement. */
9013 cp_parser_statement (parser, in_statement_expr, true, NULL);
9014 }
9015 }
9016
9017 /* Parse a selection-statement.
9018
9019 selection-statement:
9020 if ( condition ) statement
9021 if ( condition ) statement else statement
9022 switch ( condition ) statement
9023
9024 Returns the new IF_STMT or SWITCH_STMT.
9025
9026 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9027 is a (possibly labeled) if statement which is not enclosed in
9028 braces and has an else clause. This is used to implement
9029 -Wparentheses. */
9030
9031 static tree
9032 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9033 {
9034 cp_token *token;
9035 enum rid keyword;
9036
9037 if (if_p != NULL)
9038 *if_p = false;
9039
9040 /* Peek at the next token. */
9041 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9042
9043 /* See what kind of keyword it is. */
9044 keyword = token->keyword;
9045 switch (keyword)
9046 {
9047 case RID_IF:
9048 case RID_SWITCH:
9049 {
9050 tree statement;
9051 tree condition;
9052
9053 /* Look for the `('. */
9054 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9055 {
9056 cp_parser_skip_to_end_of_statement (parser);
9057 return error_mark_node;
9058 }
9059
9060 /* Begin the selection-statement. */
9061 if (keyword == RID_IF)
9062 statement = begin_if_stmt ();
9063 else
9064 statement = begin_switch_stmt ();
9065
9066 /* Parse the condition. */
9067 condition = cp_parser_condition (parser);
9068 /* Look for the `)'. */
9069 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9070 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9071 /*consume_paren=*/true);
9072
9073 if (keyword == RID_IF)
9074 {
9075 bool nested_if;
9076 unsigned char in_statement;
9077
9078 /* Add the condition. */
9079 finish_if_stmt_cond (condition, statement);
9080
9081 /* Parse the then-clause. */
9082 in_statement = parser->in_statement;
9083 parser->in_statement |= IN_IF_STMT;
9084 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9085 {
9086 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9087 add_stmt (build_empty_stmt (loc));
9088 cp_lexer_consume_token (parser->lexer);
9089 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9090 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9091 "empty body in an %<if%> statement");
9092 nested_if = false;
9093 }
9094 else
9095 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9096 parser->in_statement = in_statement;
9097
9098 finish_then_clause (statement);
9099
9100 /* If the next token is `else', parse the else-clause. */
9101 if (cp_lexer_next_token_is_keyword (parser->lexer,
9102 RID_ELSE))
9103 {
9104 /* Consume the `else' keyword. */
9105 cp_lexer_consume_token (parser->lexer);
9106 begin_else_clause (statement);
9107 /* Parse the else-clause. */
9108 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9109 {
9110 location_t loc;
9111 loc = cp_lexer_peek_token (parser->lexer)->location;
9112 warning_at (loc,
9113 OPT_Wempty_body, "suggest braces around "
9114 "empty body in an %<else%> statement");
9115 add_stmt (build_empty_stmt (loc));
9116 cp_lexer_consume_token (parser->lexer);
9117 }
9118 else
9119 cp_parser_implicitly_scoped_statement (parser, NULL);
9120
9121 finish_else_clause (statement);
9122
9123 /* If we are currently parsing a then-clause, then
9124 IF_P will not be NULL. We set it to true to
9125 indicate that this if statement has an else clause.
9126 This may trigger the Wparentheses warning below
9127 when we get back up to the parent if statement. */
9128 if (if_p != NULL)
9129 *if_p = true;
9130 }
9131 else
9132 {
9133 /* This if statement does not have an else clause. If
9134 NESTED_IF is true, then the then-clause is an if
9135 statement which does have an else clause. We warn
9136 about the potential ambiguity. */
9137 if (nested_if)
9138 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9139 "suggest explicit braces to avoid ambiguous"
9140 " %<else%>");
9141 }
9142
9143 /* Now we're all done with the if-statement. */
9144 finish_if_stmt (statement);
9145 }
9146 else
9147 {
9148 bool in_switch_statement_p;
9149 unsigned char in_statement;
9150
9151 /* Add the condition. */
9152 finish_switch_cond (condition, statement);
9153
9154 /* Parse the body of the switch-statement. */
9155 in_switch_statement_p = parser->in_switch_statement_p;
9156 in_statement = parser->in_statement;
9157 parser->in_switch_statement_p = true;
9158 parser->in_statement |= IN_SWITCH_STMT;
9159 cp_parser_implicitly_scoped_statement (parser, NULL);
9160 parser->in_switch_statement_p = in_switch_statement_p;
9161 parser->in_statement = in_statement;
9162
9163 /* Now we're all done with the switch-statement. */
9164 finish_switch_stmt (statement);
9165 }
9166
9167 return statement;
9168 }
9169 break;
9170
9171 default:
9172 cp_parser_error (parser, "expected selection-statement");
9173 return error_mark_node;
9174 }
9175 }
9176
9177 /* Parse a condition.
9178
9179 condition:
9180 expression
9181 type-specifier-seq declarator = initializer-clause
9182 type-specifier-seq declarator braced-init-list
9183
9184 GNU Extension:
9185
9186 condition:
9187 type-specifier-seq declarator asm-specification [opt]
9188 attributes [opt] = assignment-expression
9189
9190 Returns the expression that should be tested. */
9191
9192 static tree
9193 cp_parser_condition (cp_parser* parser)
9194 {
9195 cp_decl_specifier_seq type_specifiers;
9196 const char *saved_message;
9197 int declares_class_or_enum;
9198
9199 /* Try the declaration first. */
9200 cp_parser_parse_tentatively (parser);
9201 /* New types are not allowed in the type-specifier-seq for a
9202 condition. */
9203 saved_message = parser->type_definition_forbidden_message;
9204 parser->type_definition_forbidden_message
9205 = G_("types may not be defined in conditions");
9206 /* Parse the type-specifier-seq. */
9207 cp_parser_decl_specifier_seq (parser,
9208 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9209 &type_specifiers,
9210 &declares_class_or_enum);
9211 /* Restore the saved message. */
9212 parser->type_definition_forbidden_message = saved_message;
9213 /* If all is well, we might be looking at a declaration. */
9214 if (!cp_parser_error_occurred (parser))
9215 {
9216 tree decl;
9217 tree asm_specification;
9218 tree attributes;
9219 cp_declarator *declarator;
9220 tree initializer = NULL_TREE;
9221
9222 /* Parse the declarator. */
9223 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9224 /*ctor_dtor_or_conv_p=*/NULL,
9225 /*parenthesized_p=*/NULL,
9226 /*member_p=*/false);
9227 /* Parse the attributes. */
9228 attributes = cp_parser_attributes_opt (parser);
9229 /* Parse the asm-specification. */
9230 asm_specification = cp_parser_asm_specification_opt (parser);
9231 /* If the next token is not an `=' or '{', then we might still be
9232 looking at an expression. For example:
9233
9234 if (A(a).x)
9235
9236 looks like a decl-specifier-seq and a declarator -- but then
9237 there is no `=', so this is an expression. */
9238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9239 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9240 cp_parser_simulate_error (parser);
9241
9242 /* If we did see an `=' or '{', then we are looking at a declaration
9243 for sure. */
9244 if (cp_parser_parse_definitely (parser))
9245 {
9246 tree pushed_scope;
9247 bool non_constant_p;
9248 bool flags = LOOKUP_ONLYCONVERTING;
9249
9250 /* Create the declaration. */
9251 decl = start_decl (declarator, &type_specifiers,
9252 /*initialized_p=*/true,
9253 attributes, /*prefix_attributes=*/NULL_TREE,
9254 &pushed_scope);
9255
9256 /* Parse the initializer. */
9257 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9258 {
9259 initializer = cp_parser_braced_list (parser, &non_constant_p);
9260 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9261 flags = 0;
9262 }
9263 else
9264 {
9265 /* Consume the `='. */
9266 cp_parser_require (parser, CPP_EQ, RT_EQ);
9267 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9268 }
9269 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9270 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9271
9272 /* Process the initializer. */
9273 cp_finish_decl (decl,
9274 initializer, !non_constant_p,
9275 asm_specification,
9276 flags);
9277
9278 if (pushed_scope)
9279 pop_scope (pushed_scope);
9280
9281 return convert_from_reference (decl);
9282 }
9283 }
9284 /* If we didn't even get past the declarator successfully, we are
9285 definitely not looking at a declaration. */
9286 else
9287 cp_parser_abort_tentative_parse (parser);
9288
9289 /* Otherwise, we are looking at an expression. */
9290 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9291 }
9292
9293 /* Parses a for-statement or range-for-statement until the closing ')',
9294 not included. */
9295
9296 static tree
9297 cp_parser_for (cp_parser *parser)
9298 {
9299 tree init, scope, decl;
9300 bool is_range_for;
9301
9302 /* Begin the for-statement. */
9303 scope = begin_for_scope (&init);
9304
9305 /* Parse the initialization. */
9306 is_range_for = cp_parser_for_init_statement (parser, &decl);
9307
9308 if (is_range_for)
9309 return cp_parser_range_for (parser, scope, init, decl);
9310 else
9311 return cp_parser_c_for (parser, scope, init);
9312 }
9313
9314 static tree
9315 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9316 {
9317 /* Normal for loop */
9318 tree condition = NULL_TREE;
9319 tree expression = NULL_TREE;
9320 tree stmt;
9321
9322 stmt = begin_for_stmt (scope, init);
9323 /* The for-init-statement has already been parsed in
9324 cp_parser_for_init_statement, so no work is needed here. */
9325 finish_for_init_stmt (stmt);
9326
9327 /* If there's a condition, process it. */
9328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9329 condition = cp_parser_condition (parser);
9330 finish_for_cond (condition, stmt);
9331 /* Look for the `;'. */
9332 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9333
9334 /* If there's an expression, process it. */
9335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9336 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9337 finish_for_expr (expression, stmt);
9338
9339 return stmt;
9340 }
9341
9342 /* Tries to parse a range-based for-statement:
9343
9344 range-based-for:
9345 decl-specifier-seq declarator : expression
9346
9347 The decl-specifier-seq declarator and the `:' are already parsed by
9348 cp_parser_for_init_statement. If processing_template_decl it returns a
9349 newly created RANGE_FOR_STMT; if not, it is converted to a
9350 regular FOR_STMT. */
9351
9352 static tree
9353 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9354 {
9355 tree stmt, range_expr;
9356
9357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9358 {
9359 bool expr_non_constant_p;
9360 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9361 }
9362 else
9363 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9364
9365 /* If in template, STMT is converted to a normal for-statement
9366 at instantiation. If not, it is done just ahead. */
9367 if (processing_template_decl)
9368 {
9369 if (check_for_bare_parameter_packs (range_expr))
9370 range_expr = error_mark_node;
9371 stmt = begin_range_for_stmt (scope, init);
9372 finish_range_for_decl (stmt, range_decl, range_expr);
9373 if (!type_dependent_expression_p (range_expr)
9374 /* do_auto_deduction doesn't mess with template init-lists. */
9375 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9376 do_range_for_auto_deduction (range_decl, range_expr);
9377 }
9378 else
9379 {
9380 stmt = begin_for_stmt (scope, init);
9381 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9382 }
9383 return stmt;
9384 }
9385
9386 /* Subroutine of cp_convert_range_for: given the initializer expression,
9387 builds up the range temporary. */
9388
9389 static tree
9390 build_range_temp (tree range_expr)
9391 {
9392 tree range_type, range_temp;
9393
9394 /* Find out the type deduced by the declaration
9395 `auto &&__range = range_expr'. */
9396 range_type = cp_build_reference_type (make_auto (), true);
9397 range_type = do_auto_deduction (range_type, range_expr,
9398 type_uses_auto (range_type));
9399
9400 /* Create the __range variable. */
9401 range_temp = build_decl (input_location, VAR_DECL,
9402 get_identifier ("__for_range"), range_type);
9403 TREE_USED (range_temp) = 1;
9404 DECL_ARTIFICIAL (range_temp) = 1;
9405
9406 return range_temp;
9407 }
9408
9409 /* Used by cp_parser_range_for in template context: we aren't going to
9410 do a full conversion yet, but we still need to resolve auto in the
9411 type of the for-range-declaration if present. This is basically
9412 a shortcut version of cp_convert_range_for. */
9413
9414 static void
9415 do_range_for_auto_deduction (tree decl, tree range_expr)
9416 {
9417 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9418 if (auto_node)
9419 {
9420 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9421 range_temp = convert_from_reference (build_range_temp (range_expr));
9422 iter_type = (cp_parser_perform_range_for_lookup
9423 (range_temp, &begin_dummy, &end_dummy));
9424 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9425 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9426 tf_warning_or_error);
9427 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9428 iter_decl, auto_node);
9429 }
9430 }
9431
9432 /* Converts a range-based for-statement into a normal
9433 for-statement, as per the definition.
9434
9435 for (RANGE_DECL : RANGE_EXPR)
9436 BLOCK
9437
9438 should be equivalent to:
9439
9440 {
9441 auto &&__range = RANGE_EXPR;
9442 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9443 __begin != __end;
9444 ++__begin)
9445 {
9446 RANGE_DECL = *__begin;
9447 BLOCK
9448 }
9449 }
9450
9451 If RANGE_EXPR is an array:
9452 BEGIN_EXPR = __range
9453 END_EXPR = __range + ARRAY_SIZE(__range)
9454 Else if RANGE_EXPR has a member 'begin' or 'end':
9455 BEGIN_EXPR = __range.begin()
9456 END_EXPR = __range.end()
9457 Else:
9458 BEGIN_EXPR = begin(__range)
9459 END_EXPR = end(__range);
9460
9461 If __range has a member 'begin' but not 'end', or vice versa, we must
9462 still use the second alternative (it will surely fail, however).
9463 When calling begin()/end() in the third alternative we must use
9464 argument dependent lookup, but always considering 'std' as an associated
9465 namespace. */
9466
9467 tree
9468 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9469 {
9470 tree begin, end;
9471 tree iter_type, begin_expr, end_expr;
9472 tree condition, expression;
9473
9474 if (range_decl == error_mark_node || range_expr == error_mark_node)
9475 /* If an error happened previously do nothing or else a lot of
9476 unhelpful errors would be issued. */
9477 begin_expr = end_expr = iter_type = error_mark_node;
9478 else
9479 {
9480 tree range_temp = build_range_temp (range_expr);
9481 pushdecl (range_temp);
9482 cp_finish_decl (range_temp, range_expr,
9483 /*is_constant_init*/false, NULL_TREE,
9484 LOOKUP_ONLYCONVERTING);
9485
9486 range_temp = convert_from_reference (range_temp);
9487 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9488 &begin_expr, &end_expr);
9489 }
9490
9491 /* The new for initialization statement. */
9492 begin = build_decl (input_location, VAR_DECL,
9493 get_identifier ("__for_begin"), iter_type);
9494 TREE_USED (begin) = 1;
9495 DECL_ARTIFICIAL (begin) = 1;
9496 pushdecl (begin);
9497 cp_finish_decl (begin, begin_expr,
9498 /*is_constant_init*/false, NULL_TREE,
9499 LOOKUP_ONLYCONVERTING);
9500
9501 end = build_decl (input_location, VAR_DECL,
9502 get_identifier ("__for_end"), iter_type);
9503 TREE_USED (end) = 1;
9504 DECL_ARTIFICIAL (end) = 1;
9505 pushdecl (end);
9506 cp_finish_decl (end, end_expr,
9507 /*is_constant_init*/false, NULL_TREE,
9508 LOOKUP_ONLYCONVERTING);
9509
9510 finish_for_init_stmt (statement);
9511
9512 /* The new for condition. */
9513 condition = build_x_binary_op (input_location, NE_EXPR,
9514 begin, ERROR_MARK,
9515 end, ERROR_MARK,
9516 NULL, tf_warning_or_error);
9517 finish_for_cond (condition, statement);
9518
9519 /* The new increment expression. */
9520 expression = finish_unary_op_expr (input_location,
9521 PREINCREMENT_EXPR, begin);
9522 finish_for_expr (expression, statement);
9523
9524 /* The declaration is initialized with *__begin inside the loop body. */
9525 cp_finish_decl (range_decl,
9526 build_x_indirect_ref (input_location, begin, RO_NULL,
9527 tf_warning_or_error),
9528 /*is_constant_init*/false, NULL_TREE,
9529 LOOKUP_ONLYCONVERTING);
9530
9531 return statement;
9532 }
9533
9534 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9535 We need to solve both at the same time because the method used
9536 depends on the existence of members begin or end.
9537 Returns the type deduced for the iterator expression. */
9538
9539 static tree
9540 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9541 {
9542 if (error_operand_p (range))
9543 {
9544 *begin = *end = error_mark_node;
9545 return error_mark_node;
9546 }
9547
9548 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9549 {
9550 error ("range-based %<for%> expression of type %qT "
9551 "has incomplete type", TREE_TYPE (range));
9552 *begin = *end = error_mark_node;
9553 return error_mark_node;
9554 }
9555 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9556 {
9557 /* If RANGE is an array, we will use pointer arithmetic. */
9558 *begin = range;
9559 *end = build_binary_op (input_location, PLUS_EXPR,
9560 range,
9561 array_type_nelts_top (TREE_TYPE (range)),
9562 0);
9563 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9564 }
9565 else
9566 {
9567 /* If it is not an array, we must do a bit of magic. */
9568 tree id_begin, id_end;
9569 tree member_begin, member_end;
9570
9571 *begin = *end = error_mark_node;
9572
9573 id_begin = get_identifier ("begin");
9574 id_end = get_identifier ("end");
9575 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9576 /*protect=*/2, /*want_type=*/false,
9577 tf_warning_or_error);
9578 member_end = lookup_member (TREE_TYPE (range), id_end,
9579 /*protect=*/2, /*want_type=*/false,
9580 tf_warning_or_error);
9581
9582 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9583 {
9584 /* Use the member functions. */
9585 if (member_begin != NULL_TREE)
9586 *begin = cp_parser_range_for_member_function (range, id_begin);
9587 else
9588 error ("range-based %<for%> expression of type %qT has an "
9589 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9590
9591 if (member_end != NULL_TREE)
9592 *end = cp_parser_range_for_member_function (range, id_end);
9593 else
9594 error ("range-based %<for%> expression of type %qT has a "
9595 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9596 }
9597 else
9598 {
9599 /* Use global functions with ADL. */
9600 VEC(tree,gc) *vec;
9601 vec = make_tree_vector ();
9602
9603 VEC_safe_push (tree, gc, vec, range);
9604
9605 member_begin = perform_koenig_lookup (id_begin, vec,
9606 /*include_std=*/true,
9607 tf_warning_or_error);
9608 *begin = finish_call_expr (member_begin, &vec, false, true,
9609 tf_warning_or_error);
9610 member_end = perform_koenig_lookup (id_end, vec,
9611 /*include_std=*/true,
9612 tf_warning_or_error);
9613 *end = finish_call_expr (member_end, &vec, false, true,
9614 tf_warning_or_error);
9615
9616 release_tree_vector (vec);
9617 }
9618
9619 /* Last common checks. */
9620 if (*begin == error_mark_node || *end == error_mark_node)
9621 {
9622 /* If one of the expressions is an error do no more checks. */
9623 *begin = *end = error_mark_node;
9624 return error_mark_node;
9625 }
9626 else
9627 {
9628 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9629 /* The unqualified type of the __begin and __end temporaries should
9630 be the same, as required by the multiple auto declaration. */
9631 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9632 error ("inconsistent begin/end types in range-based %<for%> "
9633 "statement: %qT and %qT",
9634 TREE_TYPE (*begin), TREE_TYPE (*end));
9635 return iter_type;
9636 }
9637 }
9638 }
9639
9640 /* Helper function for cp_parser_perform_range_for_lookup.
9641 Builds a tree for RANGE.IDENTIFIER(). */
9642
9643 static tree
9644 cp_parser_range_for_member_function (tree range, tree identifier)
9645 {
9646 tree member, res;
9647 VEC(tree,gc) *vec;
9648
9649 member = finish_class_member_access_expr (range, identifier,
9650 false, tf_warning_or_error);
9651 if (member == error_mark_node)
9652 return error_mark_node;
9653
9654 vec = make_tree_vector ();
9655 res = finish_call_expr (member, &vec,
9656 /*disallow_virtual=*/false,
9657 /*koenig_p=*/false,
9658 tf_warning_or_error);
9659 release_tree_vector (vec);
9660 return res;
9661 }
9662
9663 /* Parse an iteration-statement.
9664
9665 iteration-statement:
9666 while ( condition ) statement
9667 do statement while ( expression ) ;
9668 for ( for-init-statement condition [opt] ; expression [opt] )
9669 statement
9670
9671 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9672
9673 static tree
9674 cp_parser_iteration_statement (cp_parser* parser)
9675 {
9676 cp_token *token;
9677 enum rid keyword;
9678 tree statement;
9679 unsigned char in_statement;
9680
9681 /* Peek at the next token. */
9682 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9683 if (!token)
9684 return error_mark_node;
9685
9686 /* Remember whether or not we are already within an iteration
9687 statement. */
9688 in_statement = parser->in_statement;
9689
9690 /* See what kind of keyword it is. */
9691 keyword = token->keyword;
9692 switch (keyword)
9693 {
9694 case RID_WHILE:
9695 {
9696 tree condition;
9697
9698 /* Begin the while-statement. */
9699 statement = begin_while_stmt ();
9700 /* Look for the `('. */
9701 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9702 /* Parse the condition. */
9703 condition = cp_parser_condition (parser);
9704 finish_while_stmt_cond (condition, statement);
9705 /* Look for the `)'. */
9706 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9707 /* Parse the dependent statement. */
9708 parser->in_statement = IN_ITERATION_STMT;
9709 cp_parser_already_scoped_statement (parser);
9710 parser->in_statement = in_statement;
9711 /* We're done with the while-statement. */
9712 finish_while_stmt (statement);
9713 }
9714 break;
9715
9716 case RID_DO:
9717 {
9718 tree expression;
9719
9720 /* Begin the do-statement. */
9721 statement = begin_do_stmt ();
9722 /* Parse the body of the do-statement. */
9723 parser->in_statement = IN_ITERATION_STMT;
9724 cp_parser_implicitly_scoped_statement (parser, NULL);
9725 parser->in_statement = in_statement;
9726 finish_do_body (statement);
9727 /* Look for the `while' keyword. */
9728 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9729 /* Look for the `('. */
9730 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9731 /* Parse the expression. */
9732 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9733 /* We're done with the do-statement. */
9734 finish_do_stmt (expression, statement);
9735 /* Look for the `)'. */
9736 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9737 /* Look for the `;'. */
9738 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9739 }
9740 break;
9741
9742 case RID_FOR:
9743 {
9744 /* Look for the `('. */
9745 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9746
9747 statement = cp_parser_for (parser);
9748
9749 /* Look for the `)'. */
9750 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9751
9752 /* Parse the body of the for-statement. */
9753 parser->in_statement = IN_ITERATION_STMT;
9754 cp_parser_already_scoped_statement (parser);
9755 parser->in_statement = in_statement;
9756
9757 /* We're done with the for-statement. */
9758 finish_for_stmt (statement);
9759 }
9760 break;
9761
9762 default:
9763 cp_parser_error (parser, "expected iteration-statement");
9764 statement = error_mark_node;
9765 break;
9766 }
9767
9768 return statement;
9769 }
9770
9771 /* Parse a for-init-statement or the declarator of a range-based-for.
9772 Returns true if a range-based-for declaration is seen.
9773
9774 for-init-statement:
9775 expression-statement
9776 simple-declaration */
9777
9778 static bool
9779 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9780 {
9781 /* If the next token is a `;', then we have an empty
9782 expression-statement. Grammatically, this is also a
9783 simple-declaration, but an invalid one, because it does not
9784 declare anything. Therefore, if we did not handle this case
9785 specially, we would issue an error message about an invalid
9786 declaration. */
9787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9788 {
9789 bool is_range_for = false;
9790 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9791
9792 parser->colon_corrects_to_scope_p = false;
9793
9794 /* We're going to speculatively look for a declaration, falling back
9795 to an expression, if necessary. */
9796 cp_parser_parse_tentatively (parser);
9797 /* Parse the declaration. */
9798 cp_parser_simple_declaration (parser,
9799 /*function_definition_allowed_p=*/false,
9800 decl);
9801 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9802 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9803 {
9804 /* It is a range-for, consume the ':' */
9805 cp_lexer_consume_token (parser->lexer);
9806 is_range_for = true;
9807 if (cxx_dialect < cxx0x)
9808 {
9809 error_at (cp_lexer_peek_token (parser->lexer)->location,
9810 "range-based %<for%> loops are not allowed "
9811 "in C++98 mode");
9812 *decl = error_mark_node;
9813 }
9814 }
9815 else
9816 /* The ';' is not consumed yet because we told
9817 cp_parser_simple_declaration not to. */
9818 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9819
9820 if (cp_parser_parse_definitely (parser))
9821 return is_range_for;
9822 /* If the tentative parse failed, then we shall need to look for an
9823 expression-statement. */
9824 }
9825 /* If we are here, it is an expression-statement. */
9826 cp_parser_expression_statement (parser, NULL_TREE);
9827 return false;
9828 }
9829
9830 /* Parse a jump-statement.
9831
9832 jump-statement:
9833 break ;
9834 continue ;
9835 return expression [opt] ;
9836 return braced-init-list ;
9837 goto identifier ;
9838
9839 GNU extension:
9840
9841 jump-statement:
9842 goto * expression ;
9843
9844 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9845
9846 static tree
9847 cp_parser_jump_statement (cp_parser* parser)
9848 {
9849 tree statement = error_mark_node;
9850 cp_token *token;
9851 enum rid keyword;
9852 unsigned char in_statement;
9853
9854 /* Peek at the next token. */
9855 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9856 if (!token)
9857 return error_mark_node;
9858
9859 /* See what kind of keyword it is. */
9860 keyword = token->keyword;
9861 switch (keyword)
9862 {
9863 case RID_BREAK:
9864 in_statement = parser->in_statement & ~IN_IF_STMT;
9865 switch (in_statement)
9866 {
9867 case 0:
9868 error_at (token->location, "break statement not within loop or switch");
9869 break;
9870 default:
9871 gcc_assert ((in_statement & IN_SWITCH_STMT)
9872 || in_statement == IN_ITERATION_STMT);
9873 statement = finish_break_stmt ();
9874 break;
9875 case IN_OMP_BLOCK:
9876 error_at (token->location, "invalid exit from OpenMP structured block");
9877 break;
9878 case IN_OMP_FOR:
9879 error_at (token->location, "break statement used with OpenMP for loop");
9880 break;
9881 }
9882 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9883 break;
9884
9885 case RID_CONTINUE:
9886 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9887 {
9888 case 0:
9889 error_at (token->location, "continue statement not within a loop");
9890 break;
9891 case IN_ITERATION_STMT:
9892 case IN_OMP_FOR:
9893 statement = finish_continue_stmt ();
9894 break;
9895 case IN_OMP_BLOCK:
9896 error_at (token->location, "invalid exit from OpenMP structured block");
9897 break;
9898 default:
9899 gcc_unreachable ();
9900 }
9901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9902 break;
9903
9904 case RID_RETURN:
9905 {
9906 tree expr;
9907 bool expr_non_constant_p;
9908
9909 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9910 {
9911 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9912 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9913 }
9914 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9915 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9916 else
9917 /* If the next token is a `;', then there is no
9918 expression. */
9919 expr = NULL_TREE;
9920 /* Build the return-statement. */
9921 statement = finish_return_stmt (expr);
9922 /* Look for the final `;'. */
9923 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9924 }
9925 break;
9926
9927 case RID_GOTO:
9928 /* Create the goto-statement. */
9929 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9930 {
9931 /* Issue a warning about this use of a GNU extension. */
9932 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
9933 /* Consume the '*' token. */
9934 cp_lexer_consume_token (parser->lexer);
9935 /* Parse the dependent expression. */
9936 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9937 }
9938 else
9939 finish_goto_stmt (cp_parser_identifier (parser));
9940 /* Look for the final `;'. */
9941 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9942 break;
9943
9944 default:
9945 cp_parser_error (parser, "expected jump-statement");
9946 break;
9947 }
9948
9949 return statement;
9950 }
9951
9952 /* Parse a declaration-statement.
9953
9954 declaration-statement:
9955 block-declaration */
9956
9957 static void
9958 cp_parser_declaration_statement (cp_parser* parser)
9959 {
9960 void *p;
9961
9962 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9963 p = obstack_alloc (&declarator_obstack, 0);
9964
9965 /* Parse the block-declaration. */
9966 cp_parser_block_declaration (parser, /*statement_p=*/true);
9967
9968 /* Free any declarators allocated. */
9969 obstack_free (&declarator_obstack, p);
9970
9971 /* Finish off the statement. */
9972 finish_stmt ();
9973 }
9974
9975 /* Some dependent statements (like `if (cond) statement'), are
9976 implicitly in their own scope. In other words, if the statement is
9977 a single statement (as opposed to a compound-statement), it is
9978 none-the-less treated as if it were enclosed in braces. Any
9979 declarations appearing in the dependent statement are out of scope
9980 after control passes that point. This function parses a statement,
9981 but ensures that is in its own scope, even if it is not a
9982 compound-statement.
9983
9984 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9985 is a (possibly labeled) if statement which is not enclosed in
9986 braces and has an else clause. This is used to implement
9987 -Wparentheses.
9988
9989 Returns the new statement. */
9990
9991 static tree
9992 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9993 {
9994 tree statement;
9995
9996 if (if_p != NULL)
9997 *if_p = false;
9998
9999 /* Mark if () ; with a special NOP_EXPR. */
10000 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10001 {
10002 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10003 cp_lexer_consume_token (parser->lexer);
10004 statement = add_stmt (build_empty_stmt (loc));
10005 }
10006 /* if a compound is opened, we simply parse the statement directly. */
10007 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10008 statement = cp_parser_compound_statement (parser, NULL, false, false);
10009 /* If the token is not a `{', then we must take special action. */
10010 else
10011 {
10012 /* Create a compound-statement. */
10013 statement = begin_compound_stmt (0);
10014 /* Parse the dependent-statement. */
10015 cp_parser_statement (parser, NULL_TREE, false, if_p);
10016 /* Finish the dummy compound-statement. */
10017 finish_compound_stmt (statement);
10018 }
10019
10020 /* Return the statement. */
10021 return statement;
10022 }
10023
10024 /* For some dependent statements (like `while (cond) statement'), we
10025 have already created a scope. Therefore, even if the dependent
10026 statement is a compound-statement, we do not want to create another
10027 scope. */
10028
10029 static void
10030 cp_parser_already_scoped_statement (cp_parser* parser)
10031 {
10032 /* If the token is a `{', then we must take special action. */
10033 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10034 cp_parser_statement (parser, NULL_TREE, false, NULL);
10035 else
10036 {
10037 /* Avoid calling cp_parser_compound_statement, so that we
10038 don't create a new scope. Do everything else by hand. */
10039 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10040 /* If the next keyword is `__label__' we have a label declaration. */
10041 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10042 cp_parser_label_declaration (parser);
10043 /* Parse an (optional) statement-seq. */
10044 cp_parser_statement_seq_opt (parser, NULL_TREE);
10045 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10046 }
10047 }
10048
10049 /* Declarations [gram.dcl.dcl] */
10050
10051 /* Parse an optional declaration-sequence.
10052
10053 declaration-seq:
10054 declaration
10055 declaration-seq declaration */
10056
10057 static void
10058 cp_parser_declaration_seq_opt (cp_parser* parser)
10059 {
10060 while (true)
10061 {
10062 cp_token *token;
10063
10064 token = cp_lexer_peek_token (parser->lexer);
10065
10066 if (token->type == CPP_CLOSE_BRACE
10067 || token->type == CPP_EOF
10068 || token->type == CPP_PRAGMA_EOL)
10069 break;
10070
10071 if (token->type == CPP_SEMICOLON)
10072 {
10073 /* A declaration consisting of a single semicolon is
10074 invalid. Allow it unless we're being pedantic. */
10075 cp_lexer_consume_token (parser->lexer);
10076 if (!in_system_header)
10077 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10078 continue;
10079 }
10080
10081 /* If we're entering or exiting a region that's implicitly
10082 extern "C", modify the lang context appropriately. */
10083 if (!parser->implicit_extern_c && token->implicit_extern_c)
10084 {
10085 push_lang_context (lang_name_c);
10086 parser->implicit_extern_c = true;
10087 }
10088 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10089 {
10090 pop_lang_context ();
10091 parser->implicit_extern_c = false;
10092 }
10093
10094 if (token->type == CPP_PRAGMA)
10095 {
10096 /* A top-level declaration can consist solely of a #pragma.
10097 A nested declaration cannot, so this is done here and not
10098 in cp_parser_declaration. (A #pragma at block scope is
10099 handled in cp_parser_statement.) */
10100 cp_parser_pragma (parser, pragma_external);
10101 continue;
10102 }
10103
10104 /* Parse the declaration itself. */
10105 cp_parser_declaration (parser);
10106 }
10107 }
10108
10109 /* Parse a declaration.
10110
10111 declaration:
10112 block-declaration
10113 function-definition
10114 template-declaration
10115 explicit-instantiation
10116 explicit-specialization
10117 linkage-specification
10118 namespace-definition
10119
10120 GNU extension:
10121
10122 declaration:
10123 __extension__ declaration */
10124
10125 static void
10126 cp_parser_declaration (cp_parser* parser)
10127 {
10128 cp_token token1;
10129 cp_token token2;
10130 int saved_pedantic;
10131 void *p;
10132 tree attributes = NULL_TREE;
10133
10134 /* Check for the `__extension__' keyword. */
10135 if (cp_parser_extension_opt (parser, &saved_pedantic))
10136 {
10137 /* Parse the qualified declaration. */
10138 cp_parser_declaration (parser);
10139 /* Restore the PEDANTIC flag. */
10140 pedantic = saved_pedantic;
10141
10142 return;
10143 }
10144
10145 /* Try to figure out what kind of declaration is present. */
10146 token1 = *cp_lexer_peek_token (parser->lexer);
10147
10148 if (token1.type != CPP_EOF)
10149 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10150 else
10151 {
10152 token2.type = CPP_EOF;
10153 token2.keyword = RID_MAX;
10154 }
10155
10156 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10157 p = obstack_alloc (&declarator_obstack, 0);
10158
10159 /* If the next token is `extern' and the following token is a string
10160 literal, then we have a linkage specification. */
10161 if (token1.keyword == RID_EXTERN
10162 && cp_parser_is_pure_string_literal (&token2))
10163 cp_parser_linkage_specification (parser);
10164 /* If the next token is `template', then we have either a template
10165 declaration, an explicit instantiation, or an explicit
10166 specialization. */
10167 else if (token1.keyword == RID_TEMPLATE)
10168 {
10169 /* `template <>' indicates a template specialization. */
10170 if (token2.type == CPP_LESS
10171 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10172 cp_parser_explicit_specialization (parser);
10173 /* `template <' indicates a template declaration. */
10174 else if (token2.type == CPP_LESS)
10175 cp_parser_template_declaration (parser, /*member_p=*/false);
10176 /* Anything else must be an explicit instantiation. */
10177 else
10178 cp_parser_explicit_instantiation (parser);
10179 }
10180 /* If the next token is `export', then we have a template
10181 declaration. */
10182 else if (token1.keyword == RID_EXPORT)
10183 cp_parser_template_declaration (parser, /*member_p=*/false);
10184 /* If the next token is `extern', 'static' or 'inline' and the one
10185 after that is `template', we have a GNU extended explicit
10186 instantiation directive. */
10187 else if (cp_parser_allow_gnu_extensions_p (parser)
10188 && (token1.keyword == RID_EXTERN
10189 || token1.keyword == RID_STATIC
10190 || token1.keyword == RID_INLINE)
10191 && token2.keyword == RID_TEMPLATE)
10192 cp_parser_explicit_instantiation (parser);
10193 /* If the next token is `namespace', check for a named or unnamed
10194 namespace definition. */
10195 else if (token1.keyword == RID_NAMESPACE
10196 && (/* A named namespace definition. */
10197 (token2.type == CPP_NAME
10198 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10199 != CPP_EQ))
10200 /* An unnamed namespace definition. */
10201 || token2.type == CPP_OPEN_BRACE
10202 || token2.keyword == RID_ATTRIBUTE))
10203 cp_parser_namespace_definition (parser);
10204 /* An inline (associated) namespace definition. */
10205 else if (token1.keyword == RID_INLINE
10206 && token2.keyword == RID_NAMESPACE)
10207 cp_parser_namespace_definition (parser);
10208 /* Objective-C++ declaration/definition. */
10209 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10210 cp_parser_objc_declaration (parser, NULL_TREE);
10211 else if (c_dialect_objc ()
10212 && token1.keyword == RID_ATTRIBUTE
10213 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10214 cp_parser_objc_declaration (parser, attributes);
10215 /* We must have either a block declaration or a function
10216 definition. */
10217 else
10218 /* Try to parse a block-declaration, or a function-definition. */
10219 cp_parser_block_declaration (parser, /*statement_p=*/false);
10220
10221 /* Free any declarators allocated. */
10222 obstack_free (&declarator_obstack, p);
10223 }
10224
10225 /* Parse a block-declaration.
10226
10227 block-declaration:
10228 simple-declaration
10229 asm-definition
10230 namespace-alias-definition
10231 using-declaration
10232 using-directive
10233
10234 GNU Extension:
10235
10236 block-declaration:
10237 __extension__ block-declaration
10238
10239 C++0x Extension:
10240
10241 block-declaration:
10242 static_assert-declaration
10243
10244 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10245 part of a declaration-statement. */
10246
10247 static void
10248 cp_parser_block_declaration (cp_parser *parser,
10249 bool statement_p)
10250 {
10251 cp_token *token1;
10252 int saved_pedantic;
10253
10254 /* Check for the `__extension__' keyword. */
10255 if (cp_parser_extension_opt (parser, &saved_pedantic))
10256 {
10257 /* Parse the qualified declaration. */
10258 cp_parser_block_declaration (parser, statement_p);
10259 /* Restore the PEDANTIC flag. */
10260 pedantic = saved_pedantic;
10261
10262 return;
10263 }
10264
10265 /* Peek at the next token to figure out which kind of declaration is
10266 present. */
10267 token1 = cp_lexer_peek_token (parser->lexer);
10268
10269 /* If the next keyword is `asm', we have an asm-definition. */
10270 if (token1->keyword == RID_ASM)
10271 {
10272 if (statement_p)
10273 cp_parser_commit_to_tentative_parse (parser);
10274 cp_parser_asm_definition (parser);
10275 }
10276 /* If the next keyword is `namespace', we have a
10277 namespace-alias-definition. */
10278 else if (token1->keyword == RID_NAMESPACE)
10279 cp_parser_namespace_alias_definition (parser);
10280 /* If the next keyword is `using', we have a
10281 using-declaration, a using-directive, or an alias-declaration. */
10282 else if (token1->keyword == RID_USING)
10283 {
10284 cp_token *token2;
10285
10286 if (statement_p)
10287 cp_parser_commit_to_tentative_parse (parser);
10288 /* If the token after `using' is `namespace', then we have a
10289 using-directive. */
10290 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10291 if (token2->keyword == RID_NAMESPACE)
10292 cp_parser_using_directive (parser);
10293 /* If the second token after 'using' is '=', then we have an
10294 alias-declaration. */
10295 else if (cxx_dialect >= cxx0x
10296 && token2->type == CPP_NAME
10297 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10298 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10299 == RID_ATTRIBUTE)))
10300 cp_parser_alias_declaration (parser);
10301 /* Otherwise, it's a using-declaration. */
10302 else
10303 cp_parser_using_declaration (parser,
10304 /*access_declaration_p=*/false);
10305 }
10306 /* If the next keyword is `__label__' we have a misplaced label
10307 declaration. */
10308 else if (token1->keyword == RID_LABEL)
10309 {
10310 cp_lexer_consume_token (parser->lexer);
10311 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10312 cp_parser_skip_to_end_of_statement (parser);
10313 /* If the next token is now a `;', consume it. */
10314 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10315 cp_lexer_consume_token (parser->lexer);
10316 }
10317 /* If the next token is `static_assert' we have a static assertion. */
10318 else if (token1->keyword == RID_STATIC_ASSERT)
10319 cp_parser_static_assert (parser, /*member_p=*/false);
10320 /* Anything else must be a simple-declaration. */
10321 else
10322 cp_parser_simple_declaration (parser, !statement_p,
10323 /*maybe_range_for_decl*/NULL);
10324 }
10325
10326 /* Parse a simple-declaration.
10327
10328 simple-declaration:
10329 decl-specifier-seq [opt] init-declarator-list [opt] ;
10330
10331 init-declarator-list:
10332 init-declarator
10333 init-declarator-list , init-declarator
10334
10335 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10336 function-definition as a simple-declaration.
10337
10338 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10339 parsed declaration if it is an uninitialized single declarator not followed
10340 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10341 if present, will not be consumed. */
10342
10343 static void
10344 cp_parser_simple_declaration (cp_parser* parser,
10345 bool function_definition_allowed_p,
10346 tree *maybe_range_for_decl)
10347 {
10348 cp_decl_specifier_seq decl_specifiers;
10349 int declares_class_or_enum;
10350 bool saw_declarator;
10351
10352 if (maybe_range_for_decl)
10353 *maybe_range_for_decl = NULL_TREE;
10354
10355 /* Defer access checks until we know what is being declared; the
10356 checks for names appearing in the decl-specifier-seq should be
10357 done as if we were in the scope of the thing being declared. */
10358 push_deferring_access_checks (dk_deferred);
10359
10360 /* Parse the decl-specifier-seq. We have to keep track of whether
10361 or not the decl-specifier-seq declares a named class or
10362 enumeration type, since that is the only case in which the
10363 init-declarator-list is allowed to be empty.
10364
10365 [dcl.dcl]
10366
10367 In a simple-declaration, the optional init-declarator-list can be
10368 omitted only when declaring a class or enumeration, that is when
10369 the decl-specifier-seq contains either a class-specifier, an
10370 elaborated-type-specifier, or an enum-specifier. */
10371 cp_parser_decl_specifier_seq (parser,
10372 CP_PARSER_FLAGS_OPTIONAL,
10373 &decl_specifiers,
10374 &declares_class_or_enum);
10375 /* We no longer need to defer access checks. */
10376 stop_deferring_access_checks ();
10377
10378 /* In a block scope, a valid declaration must always have a
10379 decl-specifier-seq. By not trying to parse declarators, we can
10380 resolve the declaration/expression ambiguity more quickly. */
10381 if (!function_definition_allowed_p
10382 && !decl_specifiers.any_specifiers_p)
10383 {
10384 cp_parser_error (parser, "expected declaration");
10385 goto done;
10386 }
10387
10388 /* If the next two tokens are both identifiers, the code is
10389 erroneous. The usual cause of this situation is code like:
10390
10391 T t;
10392
10393 where "T" should name a type -- but does not. */
10394 if (!decl_specifiers.any_type_specifiers_p
10395 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10396 {
10397 /* If parsing tentatively, we should commit; we really are
10398 looking at a declaration. */
10399 cp_parser_commit_to_tentative_parse (parser);
10400 /* Give up. */
10401 goto done;
10402 }
10403
10404 /* If we have seen at least one decl-specifier, and the next token
10405 is not a parenthesis, then we must be looking at a declaration.
10406 (After "int (" we might be looking at a functional cast.) */
10407 if (decl_specifiers.any_specifiers_p
10408 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10409 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10410 && !cp_parser_error_occurred (parser))
10411 cp_parser_commit_to_tentative_parse (parser);
10412
10413 /* Keep going until we hit the `;' at the end of the simple
10414 declaration. */
10415 saw_declarator = false;
10416 while (cp_lexer_next_token_is_not (parser->lexer,
10417 CPP_SEMICOLON))
10418 {
10419 cp_token *token;
10420 bool function_definition_p;
10421 tree decl;
10422
10423 if (saw_declarator)
10424 {
10425 /* If we are processing next declarator, coma is expected */
10426 token = cp_lexer_peek_token (parser->lexer);
10427 gcc_assert (token->type == CPP_COMMA);
10428 cp_lexer_consume_token (parser->lexer);
10429 if (maybe_range_for_decl)
10430 *maybe_range_for_decl = error_mark_node;
10431 }
10432 else
10433 saw_declarator = true;
10434
10435 /* Parse the init-declarator. */
10436 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10437 /*checks=*/NULL,
10438 function_definition_allowed_p,
10439 /*member_p=*/false,
10440 declares_class_or_enum,
10441 &function_definition_p,
10442 maybe_range_for_decl);
10443 /* If an error occurred while parsing tentatively, exit quickly.
10444 (That usually happens when in the body of a function; each
10445 statement is treated as a declaration-statement until proven
10446 otherwise.) */
10447 if (cp_parser_error_occurred (parser))
10448 goto done;
10449 /* Handle function definitions specially. */
10450 if (function_definition_p)
10451 {
10452 /* If the next token is a `,', then we are probably
10453 processing something like:
10454
10455 void f() {}, *p;
10456
10457 which is erroneous. */
10458 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10459 {
10460 cp_token *token = cp_lexer_peek_token (parser->lexer);
10461 error_at (token->location,
10462 "mixing"
10463 " declarations and function-definitions is forbidden");
10464 }
10465 /* Otherwise, we're done with the list of declarators. */
10466 else
10467 {
10468 pop_deferring_access_checks ();
10469 return;
10470 }
10471 }
10472 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10473 *maybe_range_for_decl = decl;
10474 /* The next token should be either a `,' or a `;'. */
10475 token = cp_lexer_peek_token (parser->lexer);
10476 /* If it's a `,', there are more declarators to come. */
10477 if (token->type == CPP_COMMA)
10478 /* will be consumed next time around */;
10479 /* If it's a `;', we are done. */
10480 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10481 break;
10482 /* Anything else is an error. */
10483 else
10484 {
10485 /* If we have already issued an error message we don't need
10486 to issue another one. */
10487 if (decl != error_mark_node
10488 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10489 cp_parser_error (parser, "expected %<,%> or %<;%>");
10490 /* Skip tokens until we reach the end of the statement. */
10491 cp_parser_skip_to_end_of_statement (parser);
10492 /* If the next token is now a `;', consume it. */
10493 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10494 cp_lexer_consume_token (parser->lexer);
10495 goto done;
10496 }
10497 /* After the first time around, a function-definition is not
10498 allowed -- even if it was OK at first. For example:
10499
10500 int i, f() {}
10501
10502 is not valid. */
10503 function_definition_allowed_p = false;
10504 }
10505
10506 /* Issue an error message if no declarators are present, and the
10507 decl-specifier-seq does not itself declare a class or
10508 enumeration. */
10509 if (!saw_declarator)
10510 {
10511 if (cp_parser_declares_only_class_p (parser))
10512 shadow_tag (&decl_specifiers);
10513 /* Perform any deferred access checks. */
10514 perform_deferred_access_checks (tf_warning_or_error);
10515 }
10516
10517 /* Consume the `;'. */
10518 if (!maybe_range_for_decl)
10519 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10520
10521 done:
10522 pop_deferring_access_checks ();
10523 }
10524
10525 /* Parse a decl-specifier-seq.
10526
10527 decl-specifier-seq:
10528 decl-specifier-seq [opt] decl-specifier
10529
10530 decl-specifier:
10531 storage-class-specifier
10532 type-specifier
10533 function-specifier
10534 friend
10535 typedef
10536
10537 GNU Extension:
10538
10539 decl-specifier:
10540 attributes
10541
10542 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10543
10544 The parser flags FLAGS is used to control type-specifier parsing.
10545
10546 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10547 flags:
10548
10549 1: one of the decl-specifiers is an elaborated-type-specifier
10550 (i.e., a type declaration)
10551 2: one of the decl-specifiers is an enum-specifier or a
10552 class-specifier (i.e., a type definition)
10553
10554 */
10555
10556 static void
10557 cp_parser_decl_specifier_seq (cp_parser* parser,
10558 cp_parser_flags flags,
10559 cp_decl_specifier_seq *decl_specs,
10560 int* declares_class_or_enum)
10561 {
10562 bool constructor_possible_p = !parser->in_declarator_p;
10563 cp_token *start_token = NULL;
10564 cp_decl_spec ds;
10565
10566 /* Clear DECL_SPECS. */
10567 clear_decl_specs (decl_specs);
10568
10569 /* Assume no class or enumeration type is declared. */
10570 *declares_class_or_enum = 0;
10571
10572 /* Keep reading specifiers until there are no more to read. */
10573 while (true)
10574 {
10575 bool constructor_p;
10576 bool found_decl_spec;
10577 cp_token *token;
10578 ds = ds_last;
10579
10580 /* Peek at the next token. */
10581 token = cp_lexer_peek_token (parser->lexer);
10582
10583 /* Save the first token of the decl spec list for error
10584 reporting. */
10585 if (!start_token)
10586 start_token = token;
10587 /* Handle attributes. */
10588 if (token->keyword == RID_ATTRIBUTE)
10589 {
10590 /* Parse the attributes. */
10591 decl_specs->attributes
10592 = chainon (decl_specs->attributes,
10593 cp_parser_attributes_opt (parser));
10594 if (decl_specs->locations[ds_attribute] == 0)
10595 decl_specs->locations[ds_attribute] = token->location;
10596 continue;
10597 }
10598 /* Assume we will find a decl-specifier keyword. */
10599 found_decl_spec = true;
10600 /* If the next token is an appropriate keyword, we can simply
10601 add it to the list. */
10602 switch (token->keyword)
10603 {
10604 /* decl-specifier:
10605 friend
10606 constexpr */
10607 case RID_FRIEND:
10608 if (!at_class_scope_p ())
10609 {
10610 error_at (token->location, "%<friend%> used outside of class");
10611 cp_lexer_purge_token (parser->lexer);
10612 }
10613 else
10614 {
10615 ds = ds_friend;
10616 /* Consume the token. */
10617 cp_lexer_consume_token (parser->lexer);
10618 }
10619 break;
10620
10621 case RID_CONSTEXPR:
10622 ds = ds_constexpr;
10623 cp_lexer_consume_token (parser->lexer);
10624 break;
10625
10626 /* function-specifier:
10627 inline
10628 virtual
10629 explicit */
10630 case RID_INLINE:
10631 case RID_VIRTUAL:
10632 case RID_EXPLICIT:
10633 cp_parser_function_specifier_opt (parser, decl_specs);
10634 break;
10635
10636 /* decl-specifier:
10637 typedef */
10638 case RID_TYPEDEF:
10639 ds = ds_typedef;
10640 /* Consume the token. */
10641 cp_lexer_consume_token (parser->lexer);
10642 /* A constructor declarator cannot appear in a typedef. */
10643 constructor_possible_p = false;
10644 /* The "typedef" keyword can only occur in a declaration; we
10645 may as well commit at this point. */
10646 cp_parser_commit_to_tentative_parse (parser);
10647
10648 if (decl_specs->storage_class != sc_none)
10649 decl_specs->conflicting_specifiers_p = true;
10650 break;
10651
10652 /* storage-class-specifier:
10653 auto
10654 register
10655 static
10656 extern
10657 mutable
10658
10659 GNU Extension:
10660 thread */
10661 case RID_AUTO:
10662 if (cxx_dialect == cxx98)
10663 {
10664 /* Consume the token. */
10665 cp_lexer_consume_token (parser->lexer);
10666
10667 /* Complain about `auto' as a storage specifier, if
10668 we're complaining about C++0x compatibility. */
10669 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10670 " changes meaning in C++11; please remove it");
10671
10672 /* Set the storage class anyway. */
10673 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10674 token->location);
10675 }
10676 else
10677 /* C++0x auto type-specifier. */
10678 found_decl_spec = false;
10679 break;
10680
10681 case RID_REGISTER:
10682 case RID_STATIC:
10683 case RID_EXTERN:
10684 case RID_MUTABLE:
10685 /* Consume the token. */
10686 cp_lexer_consume_token (parser->lexer);
10687 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10688 token->location);
10689 break;
10690 case RID_THREAD:
10691 /* Consume the token. */
10692 ds = ds_thread;
10693 cp_lexer_consume_token (parser->lexer);
10694 break;
10695
10696 default:
10697 /* We did not yet find a decl-specifier yet. */
10698 found_decl_spec = false;
10699 break;
10700 }
10701
10702 if (found_decl_spec
10703 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10704 && token->keyword != RID_CONSTEXPR)
10705 error ("decl-specifier invalid in condition");
10706
10707 if (ds != ds_last)
10708 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
10709
10710 /* Constructors are a special case. The `S' in `S()' is not a
10711 decl-specifier; it is the beginning of the declarator. */
10712 constructor_p
10713 = (!found_decl_spec
10714 && constructor_possible_p
10715 && (cp_parser_constructor_declarator_p
10716 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10717
10718 /* If we don't have a DECL_SPEC yet, then we must be looking at
10719 a type-specifier. */
10720 if (!found_decl_spec && !constructor_p)
10721 {
10722 int decl_spec_declares_class_or_enum;
10723 bool is_cv_qualifier;
10724 tree type_spec;
10725
10726 type_spec
10727 = cp_parser_type_specifier (parser, flags,
10728 decl_specs,
10729 /*is_declaration=*/true,
10730 &decl_spec_declares_class_or_enum,
10731 &is_cv_qualifier);
10732 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10733
10734 /* If this type-specifier referenced a user-defined type
10735 (a typedef, class-name, etc.), then we can't allow any
10736 more such type-specifiers henceforth.
10737
10738 [dcl.spec]
10739
10740 The longest sequence of decl-specifiers that could
10741 possibly be a type name is taken as the
10742 decl-specifier-seq of a declaration. The sequence shall
10743 be self-consistent as described below.
10744
10745 [dcl.type]
10746
10747 As a general rule, at most one type-specifier is allowed
10748 in the complete decl-specifier-seq of a declaration. The
10749 only exceptions are the following:
10750
10751 -- const or volatile can be combined with any other
10752 type-specifier.
10753
10754 -- signed or unsigned can be combined with char, long,
10755 short, or int.
10756
10757 -- ..
10758
10759 Example:
10760
10761 typedef char* Pc;
10762 void g (const int Pc);
10763
10764 Here, Pc is *not* part of the decl-specifier seq; it's
10765 the declarator. Therefore, once we see a type-specifier
10766 (other than a cv-qualifier), we forbid any additional
10767 user-defined types. We *do* still allow things like `int
10768 int' to be considered a decl-specifier-seq, and issue the
10769 error message later. */
10770 if (type_spec && !is_cv_qualifier)
10771 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10772 /* A constructor declarator cannot follow a type-specifier. */
10773 if (type_spec)
10774 {
10775 constructor_possible_p = false;
10776 found_decl_spec = true;
10777 if (!is_cv_qualifier)
10778 decl_specs->any_type_specifiers_p = true;
10779 }
10780 }
10781
10782 /* If we still do not have a DECL_SPEC, then there are no more
10783 decl-specifiers. */
10784 if (!found_decl_spec)
10785 break;
10786
10787 decl_specs->any_specifiers_p = true;
10788 /* After we see one decl-specifier, further decl-specifiers are
10789 always optional. */
10790 flags |= CP_PARSER_FLAGS_OPTIONAL;
10791 }
10792
10793 /* Don't allow a friend specifier with a class definition. */
10794 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10795 && (*declares_class_or_enum & 2))
10796 error_at (decl_specs->locations[ds_friend],
10797 "class definition may not be declared a friend");
10798 }
10799
10800 /* Parse an (optional) storage-class-specifier.
10801
10802 storage-class-specifier:
10803 auto
10804 register
10805 static
10806 extern
10807 mutable
10808
10809 GNU Extension:
10810
10811 storage-class-specifier:
10812 thread
10813
10814 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10815
10816 static tree
10817 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10818 {
10819 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10820 {
10821 case RID_AUTO:
10822 if (cxx_dialect != cxx98)
10823 return NULL_TREE;
10824 /* Fall through for C++98. */
10825
10826 case RID_REGISTER:
10827 case RID_STATIC:
10828 case RID_EXTERN:
10829 case RID_MUTABLE:
10830 case RID_THREAD:
10831 /* Consume the token. */
10832 return cp_lexer_consume_token (parser->lexer)->u.value;
10833
10834 default:
10835 return NULL_TREE;
10836 }
10837 }
10838
10839 /* Parse an (optional) function-specifier.
10840
10841 function-specifier:
10842 inline
10843 virtual
10844 explicit
10845
10846 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10847 Updates DECL_SPECS, if it is non-NULL. */
10848
10849 static tree
10850 cp_parser_function_specifier_opt (cp_parser* parser,
10851 cp_decl_specifier_seq *decl_specs)
10852 {
10853 cp_token *token = cp_lexer_peek_token (parser->lexer);
10854 switch (token->keyword)
10855 {
10856 case RID_INLINE:
10857 set_and_check_decl_spec_loc (decl_specs, ds_inline, token->location);
10858 break;
10859
10860 case RID_VIRTUAL:
10861 /* 14.5.2.3 [temp.mem]
10862
10863 A member function template shall not be virtual. */
10864 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10865 error_at (token->location, "templates may not be %<virtual%>");
10866 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token->location);
10867 break;
10868
10869 case RID_EXPLICIT:
10870 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token->location);
10871 break;
10872
10873 default:
10874 return NULL_TREE;
10875 }
10876
10877 /* Consume the token. */
10878 return cp_lexer_consume_token (parser->lexer)->u.value;
10879 }
10880
10881 /* Parse a linkage-specification.
10882
10883 linkage-specification:
10884 extern string-literal { declaration-seq [opt] }
10885 extern string-literal declaration */
10886
10887 static void
10888 cp_parser_linkage_specification (cp_parser* parser)
10889 {
10890 tree linkage;
10891
10892 /* Look for the `extern' keyword. */
10893 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10894
10895 /* Look for the string-literal. */
10896 linkage = cp_parser_string_literal (parser, false, false);
10897
10898 /* Transform the literal into an identifier. If the literal is a
10899 wide-character string, or contains embedded NULs, then we can't
10900 handle it as the user wants. */
10901 if (strlen (TREE_STRING_POINTER (linkage))
10902 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10903 {
10904 cp_parser_error (parser, "invalid linkage-specification");
10905 /* Assume C++ linkage. */
10906 linkage = lang_name_cplusplus;
10907 }
10908 else
10909 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10910
10911 /* We're now using the new linkage. */
10912 push_lang_context (linkage);
10913
10914 /* If the next token is a `{', then we're using the first
10915 production. */
10916 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10917 {
10918 /* Consume the `{' token. */
10919 cp_lexer_consume_token (parser->lexer);
10920 /* Parse the declarations. */
10921 cp_parser_declaration_seq_opt (parser);
10922 /* Look for the closing `}'. */
10923 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10924 }
10925 /* Otherwise, there's just one declaration. */
10926 else
10927 {
10928 bool saved_in_unbraced_linkage_specification_p;
10929
10930 saved_in_unbraced_linkage_specification_p
10931 = parser->in_unbraced_linkage_specification_p;
10932 parser->in_unbraced_linkage_specification_p = true;
10933 cp_parser_declaration (parser);
10934 parser->in_unbraced_linkage_specification_p
10935 = saved_in_unbraced_linkage_specification_p;
10936 }
10937
10938 /* We're done with the linkage-specification. */
10939 pop_lang_context ();
10940 }
10941
10942 /* Parse a static_assert-declaration.
10943
10944 static_assert-declaration:
10945 static_assert ( constant-expression , string-literal ) ;
10946
10947 If MEMBER_P, this static_assert is a class member. */
10948
10949 static void
10950 cp_parser_static_assert(cp_parser *parser, bool member_p)
10951 {
10952 tree condition;
10953 tree message;
10954 cp_token *token;
10955 location_t saved_loc;
10956 bool dummy;
10957
10958 /* Peek at the `static_assert' token so we can keep track of exactly
10959 where the static assertion started. */
10960 token = cp_lexer_peek_token (parser->lexer);
10961 saved_loc = token->location;
10962
10963 /* Look for the `static_assert' keyword. */
10964 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10965 RT_STATIC_ASSERT))
10966 return;
10967
10968 /* We know we are in a static assertion; commit to any tentative
10969 parse. */
10970 if (cp_parser_parsing_tentatively (parser))
10971 cp_parser_commit_to_tentative_parse (parser);
10972
10973 /* Parse the `(' starting the static assertion condition. */
10974 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10975
10976 /* Parse the constant-expression. Allow a non-constant expression
10977 here in order to give better diagnostics in finish_static_assert. */
10978 condition =
10979 cp_parser_constant_expression (parser,
10980 /*allow_non_constant_p=*/true,
10981 /*non_constant_p=*/&dummy);
10982
10983 /* Parse the separating `,'. */
10984 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10985
10986 /* Parse the string-literal message. */
10987 message = cp_parser_string_literal (parser,
10988 /*translate=*/false,
10989 /*wide_ok=*/true);
10990
10991 /* A `)' completes the static assertion. */
10992 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10993 cp_parser_skip_to_closing_parenthesis (parser,
10994 /*recovering=*/true,
10995 /*or_comma=*/false,
10996 /*consume_paren=*/true);
10997
10998 /* A semicolon terminates the declaration. */
10999 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11000
11001 /* Complete the static assertion, which may mean either processing
11002 the static assert now or saving it for template instantiation. */
11003 finish_static_assert (condition, message, saved_loc, member_p);
11004 }
11005
11006 /* Parse a `decltype' type. Returns the type.
11007
11008 simple-type-specifier:
11009 decltype ( expression ) */
11010
11011 static tree
11012 cp_parser_decltype (cp_parser *parser)
11013 {
11014 tree expr;
11015 bool id_expression_or_member_access_p = false;
11016 const char *saved_message;
11017 bool saved_integral_constant_expression_p;
11018 bool saved_non_integral_constant_expression_p;
11019 cp_token *id_expr_start_token;
11020 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11021
11022 if (start_token->type == CPP_DECLTYPE)
11023 {
11024 /* Already parsed. */
11025 cp_lexer_consume_token (parser->lexer);
11026 return start_token->u.value;
11027 }
11028
11029 /* Look for the `decltype' token. */
11030 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11031 return error_mark_node;
11032
11033 /* Types cannot be defined in a `decltype' expression. Save away the
11034 old message. */
11035 saved_message = parser->type_definition_forbidden_message;
11036
11037 /* And create the new one. */
11038 parser->type_definition_forbidden_message
11039 = G_("types may not be defined in %<decltype%> expressions");
11040
11041 /* The restrictions on constant-expressions do not apply inside
11042 decltype expressions. */
11043 saved_integral_constant_expression_p
11044 = parser->integral_constant_expression_p;
11045 saved_non_integral_constant_expression_p
11046 = parser->non_integral_constant_expression_p;
11047 parser->integral_constant_expression_p = false;
11048
11049 /* Do not actually evaluate the expression. */
11050 ++cp_unevaluated_operand;
11051
11052 /* Do not warn about problems with the expression. */
11053 ++c_inhibit_evaluation_warnings;
11054
11055 /* Parse the opening `('. */
11056 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11057 return error_mark_node;
11058
11059 /* First, try parsing an id-expression. */
11060 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11061 cp_parser_parse_tentatively (parser);
11062 expr = cp_parser_id_expression (parser,
11063 /*template_keyword_p=*/false,
11064 /*check_dependency_p=*/true,
11065 /*template_p=*/NULL,
11066 /*declarator_p=*/false,
11067 /*optional_p=*/false);
11068
11069 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11070 {
11071 bool non_integral_constant_expression_p = false;
11072 tree id_expression = expr;
11073 cp_id_kind idk;
11074 const char *error_msg;
11075
11076 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11077 /* Lookup the name we got back from the id-expression. */
11078 expr = cp_parser_lookup_name (parser, expr,
11079 none_type,
11080 /*is_template=*/false,
11081 /*is_namespace=*/false,
11082 /*check_dependency=*/true,
11083 /*ambiguous_decls=*/NULL,
11084 id_expr_start_token->location);
11085
11086 if (expr
11087 && expr != error_mark_node
11088 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11089 && TREE_CODE (expr) != TYPE_DECL
11090 && (TREE_CODE (expr) != BIT_NOT_EXPR
11091 || !TYPE_P (TREE_OPERAND (expr, 0)))
11092 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11093 {
11094 /* Complete lookup of the id-expression. */
11095 expr = (finish_id_expression
11096 (id_expression, expr, parser->scope, &idk,
11097 /*integral_constant_expression_p=*/false,
11098 /*allow_non_integral_constant_expression_p=*/true,
11099 &non_integral_constant_expression_p,
11100 /*template_p=*/false,
11101 /*done=*/true,
11102 /*address_p=*/false,
11103 /*template_arg_p=*/false,
11104 &error_msg,
11105 id_expr_start_token->location));
11106
11107 if (expr == error_mark_node)
11108 /* We found an id-expression, but it was something that we
11109 should not have found. This is an error, not something
11110 we can recover from, so note that we found an
11111 id-expression and we'll recover as gracefully as
11112 possible. */
11113 id_expression_or_member_access_p = true;
11114 }
11115
11116 if (expr
11117 && expr != error_mark_node
11118 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11119 /* We have an id-expression. */
11120 id_expression_or_member_access_p = true;
11121 }
11122
11123 if (!id_expression_or_member_access_p)
11124 {
11125 /* Abort the id-expression parse. */
11126 cp_parser_abort_tentative_parse (parser);
11127
11128 /* Parsing tentatively, again. */
11129 cp_parser_parse_tentatively (parser);
11130
11131 /* Parse a class member access. */
11132 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11133 /*cast_p=*/false,
11134 /*member_access_only_p=*/true, NULL);
11135
11136 if (expr
11137 && expr != error_mark_node
11138 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11139 /* We have an id-expression. */
11140 id_expression_or_member_access_p = true;
11141 }
11142
11143 if (id_expression_or_member_access_p)
11144 /* We have parsed the complete id-expression or member access. */
11145 cp_parser_parse_definitely (parser);
11146 else
11147 {
11148 bool saved_greater_than_is_operator_p;
11149
11150 /* Abort our attempt to parse an id-expression or member access
11151 expression. */
11152 cp_parser_abort_tentative_parse (parser);
11153
11154 /* Within a parenthesized expression, a `>' token is always
11155 the greater-than operator. */
11156 saved_greater_than_is_operator_p
11157 = parser->greater_than_is_operator_p;
11158 parser->greater_than_is_operator_p = true;
11159
11160 /* Parse a full expression. */
11161 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11162
11163 /* The `>' token might be the end of a template-id or
11164 template-parameter-list now. */
11165 parser->greater_than_is_operator_p
11166 = saved_greater_than_is_operator_p;
11167 }
11168
11169 /* Go back to evaluating expressions. */
11170 --cp_unevaluated_operand;
11171 --c_inhibit_evaluation_warnings;
11172
11173 /* Restore the old message and the integral constant expression
11174 flags. */
11175 parser->type_definition_forbidden_message = saved_message;
11176 parser->integral_constant_expression_p
11177 = saved_integral_constant_expression_p;
11178 parser->non_integral_constant_expression_p
11179 = saved_non_integral_constant_expression_p;
11180
11181 /* Parse to the closing `)'. */
11182 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11183 {
11184 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11185 /*consume_paren=*/true);
11186 return error_mark_node;
11187 }
11188
11189 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11190 tf_warning_or_error);
11191
11192 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11193 it again. */
11194 start_token->type = CPP_DECLTYPE;
11195 start_token->u.value = expr;
11196 start_token->keyword = RID_MAX;
11197 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11198
11199 return expr;
11200 }
11201
11202 /* Special member functions [gram.special] */
11203
11204 /* Parse a conversion-function-id.
11205
11206 conversion-function-id:
11207 operator conversion-type-id
11208
11209 Returns an IDENTIFIER_NODE representing the operator. */
11210
11211 static tree
11212 cp_parser_conversion_function_id (cp_parser* parser)
11213 {
11214 tree type;
11215 tree saved_scope;
11216 tree saved_qualifying_scope;
11217 tree saved_object_scope;
11218 tree pushed_scope = NULL_TREE;
11219
11220 /* Look for the `operator' token. */
11221 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11222 return error_mark_node;
11223 /* When we parse the conversion-type-id, the current scope will be
11224 reset. However, we need that information in able to look up the
11225 conversion function later, so we save it here. */
11226 saved_scope = parser->scope;
11227 saved_qualifying_scope = parser->qualifying_scope;
11228 saved_object_scope = parser->object_scope;
11229 /* We must enter the scope of the class so that the names of
11230 entities declared within the class are available in the
11231 conversion-type-id. For example, consider:
11232
11233 struct S {
11234 typedef int I;
11235 operator I();
11236 };
11237
11238 S::operator I() { ... }
11239
11240 In order to see that `I' is a type-name in the definition, we
11241 must be in the scope of `S'. */
11242 if (saved_scope)
11243 pushed_scope = push_scope (saved_scope);
11244 /* Parse the conversion-type-id. */
11245 type = cp_parser_conversion_type_id (parser);
11246 /* Leave the scope of the class, if any. */
11247 if (pushed_scope)
11248 pop_scope (pushed_scope);
11249 /* Restore the saved scope. */
11250 parser->scope = saved_scope;
11251 parser->qualifying_scope = saved_qualifying_scope;
11252 parser->object_scope = saved_object_scope;
11253 /* If the TYPE is invalid, indicate failure. */
11254 if (type == error_mark_node)
11255 return error_mark_node;
11256 return mangle_conv_op_name_for_type (type);
11257 }
11258
11259 /* Parse a conversion-type-id:
11260
11261 conversion-type-id:
11262 type-specifier-seq conversion-declarator [opt]
11263
11264 Returns the TYPE specified. */
11265
11266 static tree
11267 cp_parser_conversion_type_id (cp_parser* parser)
11268 {
11269 tree attributes;
11270 cp_decl_specifier_seq type_specifiers;
11271 cp_declarator *declarator;
11272 tree type_specified;
11273
11274 /* Parse the attributes. */
11275 attributes = cp_parser_attributes_opt (parser);
11276 /* Parse the type-specifiers. */
11277 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11278 /*is_trailing_return=*/false,
11279 &type_specifiers);
11280 /* If that didn't work, stop. */
11281 if (type_specifiers.type == error_mark_node)
11282 return error_mark_node;
11283 /* Parse the conversion-declarator. */
11284 declarator = cp_parser_conversion_declarator_opt (parser);
11285
11286 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11287 /*initialized=*/0, &attributes);
11288 if (attributes)
11289 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11290
11291 /* Don't give this error when parsing tentatively. This happens to
11292 work because we always parse this definitively once. */
11293 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11294 && type_uses_auto (type_specified))
11295 {
11296 if (cxx_dialect < cxx1y)
11297 {
11298 error ("invalid use of %<auto%> in conversion operator");
11299 return error_mark_node;
11300 }
11301 else if (template_parm_scope_p ())
11302 warning (0, "use of %<auto%> in member template "
11303 "conversion operator can never be deduced");
11304 }
11305
11306 return type_specified;
11307 }
11308
11309 /* Parse an (optional) conversion-declarator.
11310
11311 conversion-declarator:
11312 ptr-operator conversion-declarator [opt]
11313
11314 */
11315
11316 static cp_declarator *
11317 cp_parser_conversion_declarator_opt (cp_parser* parser)
11318 {
11319 enum tree_code code;
11320 tree class_type;
11321 cp_cv_quals cv_quals;
11322
11323 /* We don't know if there's a ptr-operator next, or not. */
11324 cp_parser_parse_tentatively (parser);
11325 /* Try the ptr-operator. */
11326 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11327 /* If it worked, look for more conversion-declarators. */
11328 if (cp_parser_parse_definitely (parser))
11329 {
11330 cp_declarator *declarator;
11331
11332 /* Parse another optional declarator. */
11333 declarator = cp_parser_conversion_declarator_opt (parser);
11334
11335 return cp_parser_make_indirect_declarator
11336 (code, class_type, cv_quals, declarator);
11337 }
11338
11339 return NULL;
11340 }
11341
11342 /* Parse an (optional) ctor-initializer.
11343
11344 ctor-initializer:
11345 : mem-initializer-list
11346
11347 Returns TRUE iff the ctor-initializer was actually present. */
11348
11349 static bool
11350 cp_parser_ctor_initializer_opt (cp_parser* parser)
11351 {
11352 /* If the next token is not a `:', then there is no
11353 ctor-initializer. */
11354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11355 {
11356 /* Do default initialization of any bases and members. */
11357 if (DECL_CONSTRUCTOR_P (current_function_decl))
11358 finish_mem_initializers (NULL_TREE);
11359
11360 return false;
11361 }
11362
11363 /* Consume the `:' token. */
11364 cp_lexer_consume_token (parser->lexer);
11365 /* And the mem-initializer-list. */
11366 cp_parser_mem_initializer_list (parser);
11367
11368 return true;
11369 }
11370
11371 /* Parse a mem-initializer-list.
11372
11373 mem-initializer-list:
11374 mem-initializer ... [opt]
11375 mem-initializer ... [opt] , mem-initializer-list */
11376
11377 static void
11378 cp_parser_mem_initializer_list (cp_parser* parser)
11379 {
11380 tree mem_initializer_list = NULL_TREE;
11381 tree target_ctor = error_mark_node;
11382 cp_token *token = cp_lexer_peek_token (parser->lexer);
11383
11384 /* Let the semantic analysis code know that we are starting the
11385 mem-initializer-list. */
11386 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11387 error_at (token->location,
11388 "only constructors take member initializers");
11389
11390 /* Loop through the list. */
11391 while (true)
11392 {
11393 tree mem_initializer;
11394
11395 token = cp_lexer_peek_token (parser->lexer);
11396 /* Parse the mem-initializer. */
11397 mem_initializer = cp_parser_mem_initializer (parser);
11398 /* If the next token is a `...', we're expanding member initializers. */
11399 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11400 {
11401 /* Consume the `...'. */
11402 cp_lexer_consume_token (parser->lexer);
11403
11404 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11405 can be expanded but members cannot. */
11406 if (mem_initializer != error_mark_node
11407 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11408 {
11409 error_at (token->location,
11410 "cannot expand initializer for member %<%D%>",
11411 TREE_PURPOSE (mem_initializer));
11412 mem_initializer = error_mark_node;
11413 }
11414
11415 /* Construct the pack expansion type. */
11416 if (mem_initializer != error_mark_node)
11417 mem_initializer = make_pack_expansion (mem_initializer);
11418 }
11419 if (target_ctor != error_mark_node
11420 && mem_initializer != error_mark_node)
11421 {
11422 error ("mem-initializer for %qD follows constructor delegation",
11423 TREE_PURPOSE (mem_initializer));
11424 mem_initializer = error_mark_node;
11425 }
11426 /* Look for a target constructor. */
11427 if (mem_initializer != error_mark_node
11428 && TYPE_P (TREE_PURPOSE (mem_initializer))
11429 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11430 {
11431 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11432 if (mem_initializer_list)
11433 {
11434 error ("constructor delegation follows mem-initializer for %qD",
11435 TREE_PURPOSE (mem_initializer_list));
11436 mem_initializer = error_mark_node;
11437 }
11438 target_ctor = mem_initializer;
11439 }
11440 /* Add it to the list, unless it was erroneous. */
11441 if (mem_initializer != error_mark_node)
11442 {
11443 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11444 mem_initializer_list = mem_initializer;
11445 }
11446 /* If the next token is not a `,', we're done. */
11447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11448 break;
11449 /* Consume the `,' token. */
11450 cp_lexer_consume_token (parser->lexer);
11451 }
11452
11453 /* Perform semantic analysis. */
11454 if (DECL_CONSTRUCTOR_P (current_function_decl))
11455 finish_mem_initializers (mem_initializer_list);
11456 }
11457
11458 /* Parse a mem-initializer.
11459
11460 mem-initializer:
11461 mem-initializer-id ( expression-list [opt] )
11462 mem-initializer-id braced-init-list
11463
11464 GNU extension:
11465
11466 mem-initializer:
11467 ( expression-list [opt] )
11468
11469 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11470 class) or FIELD_DECL (for a non-static data member) to initialize;
11471 the TREE_VALUE is the expression-list. An empty initialization
11472 list is represented by void_list_node. */
11473
11474 static tree
11475 cp_parser_mem_initializer (cp_parser* parser)
11476 {
11477 tree mem_initializer_id;
11478 tree expression_list;
11479 tree member;
11480 cp_token *token = cp_lexer_peek_token (parser->lexer);
11481
11482 /* Find out what is being initialized. */
11483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11484 {
11485 permerror (token->location,
11486 "anachronistic old-style base class initializer");
11487 mem_initializer_id = NULL_TREE;
11488 }
11489 else
11490 {
11491 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11492 if (mem_initializer_id == error_mark_node)
11493 return mem_initializer_id;
11494 }
11495 member = expand_member_init (mem_initializer_id);
11496 if (member && !DECL_P (member))
11497 in_base_initializer = 1;
11498
11499 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11500 {
11501 bool expr_non_constant_p;
11502 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11503 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11504 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11505 expression_list = build_tree_list (NULL_TREE, expression_list);
11506 }
11507 else
11508 {
11509 VEC(tree,gc)* vec;
11510 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11511 /*cast_p=*/false,
11512 /*allow_expansion_p=*/true,
11513 /*non_constant_p=*/NULL);
11514 if (vec == NULL)
11515 return error_mark_node;
11516 expression_list = build_tree_list_vec (vec);
11517 release_tree_vector (vec);
11518 }
11519
11520 if (expression_list == error_mark_node)
11521 return error_mark_node;
11522 if (!expression_list)
11523 expression_list = void_type_node;
11524
11525 in_base_initializer = 0;
11526
11527 return member ? build_tree_list (member, expression_list) : error_mark_node;
11528 }
11529
11530 /* Parse a mem-initializer-id.
11531
11532 mem-initializer-id:
11533 :: [opt] nested-name-specifier [opt] class-name
11534 identifier
11535
11536 Returns a TYPE indicating the class to be initializer for the first
11537 production. Returns an IDENTIFIER_NODE indicating the data member
11538 to be initialized for the second production. */
11539
11540 static tree
11541 cp_parser_mem_initializer_id (cp_parser* parser)
11542 {
11543 bool global_scope_p;
11544 bool nested_name_specifier_p;
11545 bool template_p = false;
11546 tree id;
11547
11548 cp_token *token = cp_lexer_peek_token (parser->lexer);
11549
11550 /* `typename' is not allowed in this context ([temp.res]). */
11551 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11552 {
11553 error_at (token->location,
11554 "keyword %<typename%> not allowed in this context (a qualified "
11555 "member initializer is implicitly a type)");
11556 cp_lexer_consume_token (parser->lexer);
11557 }
11558 /* Look for the optional `::' operator. */
11559 global_scope_p
11560 = (cp_parser_global_scope_opt (parser,
11561 /*current_scope_valid_p=*/false)
11562 != NULL_TREE);
11563 /* Look for the optional nested-name-specifier. The simplest way to
11564 implement:
11565
11566 [temp.res]
11567
11568 The keyword `typename' is not permitted in a base-specifier or
11569 mem-initializer; in these contexts a qualified name that
11570 depends on a template-parameter is implicitly assumed to be a
11571 type name.
11572
11573 is to assume that we have seen the `typename' keyword at this
11574 point. */
11575 nested_name_specifier_p
11576 = (cp_parser_nested_name_specifier_opt (parser,
11577 /*typename_keyword_p=*/true,
11578 /*check_dependency_p=*/true,
11579 /*type_p=*/true,
11580 /*is_declaration=*/true)
11581 != NULL_TREE);
11582 if (nested_name_specifier_p)
11583 template_p = cp_parser_optional_template_keyword (parser);
11584 /* If there is a `::' operator or a nested-name-specifier, then we
11585 are definitely looking for a class-name. */
11586 if (global_scope_p || nested_name_specifier_p)
11587 return cp_parser_class_name (parser,
11588 /*typename_keyword_p=*/true,
11589 /*template_keyword_p=*/template_p,
11590 typename_type,
11591 /*check_dependency_p=*/true,
11592 /*class_head_p=*/false,
11593 /*is_declaration=*/true);
11594 /* Otherwise, we could also be looking for an ordinary identifier. */
11595 cp_parser_parse_tentatively (parser);
11596 /* Try a class-name. */
11597 id = cp_parser_class_name (parser,
11598 /*typename_keyword_p=*/true,
11599 /*template_keyword_p=*/false,
11600 none_type,
11601 /*check_dependency_p=*/true,
11602 /*class_head_p=*/false,
11603 /*is_declaration=*/true);
11604 /* If we found one, we're done. */
11605 if (cp_parser_parse_definitely (parser))
11606 return id;
11607 /* Otherwise, look for an ordinary identifier. */
11608 return cp_parser_identifier (parser);
11609 }
11610
11611 /* Overloading [gram.over] */
11612
11613 /* Parse an operator-function-id.
11614
11615 operator-function-id:
11616 operator operator
11617
11618 Returns an IDENTIFIER_NODE for the operator which is a
11619 human-readable spelling of the identifier, e.g., `operator +'. */
11620
11621 static tree
11622 cp_parser_operator_function_id (cp_parser* parser)
11623 {
11624 /* Look for the `operator' keyword. */
11625 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11626 return error_mark_node;
11627 /* And then the name of the operator itself. */
11628 return cp_parser_operator (parser);
11629 }
11630
11631 /* Return an identifier node for a user-defined literal operator.
11632 The suffix identifier is chained to the operator name identifier. */
11633
11634 static tree
11635 cp_literal_operator_id (const char* name)
11636 {
11637 tree identifier;
11638 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11639 + strlen (name) + 10);
11640 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11641 identifier = get_identifier (buffer);
11642 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11643
11644 return identifier;
11645 }
11646
11647 /* Parse an operator.
11648
11649 operator:
11650 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11651 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11652 || ++ -- , ->* -> () []
11653
11654 GNU Extensions:
11655
11656 operator:
11657 <? >? <?= >?=
11658
11659 Returns an IDENTIFIER_NODE for the operator which is a
11660 human-readable spelling of the identifier, e.g., `operator +'. */
11661
11662 static tree
11663 cp_parser_operator (cp_parser* parser)
11664 {
11665 tree id = NULL_TREE;
11666 cp_token *token;
11667
11668 /* Peek at the next token. */
11669 token = cp_lexer_peek_token (parser->lexer);
11670 /* Figure out which operator we have. */
11671 switch (token->type)
11672 {
11673 case CPP_KEYWORD:
11674 {
11675 enum tree_code op;
11676
11677 /* The keyword should be either `new' or `delete'. */
11678 if (token->keyword == RID_NEW)
11679 op = NEW_EXPR;
11680 else if (token->keyword == RID_DELETE)
11681 op = DELETE_EXPR;
11682 else
11683 break;
11684
11685 /* Consume the `new' or `delete' token. */
11686 cp_lexer_consume_token (parser->lexer);
11687
11688 /* Peek at the next token. */
11689 token = cp_lexer_peek_token (parser->lexer);
11690 /* If it's a `[' token then this is the array variant of the
11691 operator. */
11692 if (token->type == CPP_OPEN_SQUARE)
11693 {
11694 /* Consume the `[' token. */
11695 cp_lexer_consume_token (parser->lexer);
11696 /* Look for the `]' token. */
11697 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11698 id = ansi_opname (op == NEW_EXPR
11699 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11700 }
11701 /* Otherwise, we have the non-array variant. */
11702 else
11703 id = ansi_opname (op);
11704
11705 return id;
11706 }
11707
11708 case CPP_PLUS:
11709 id = ansi_opname (PLUS_EXPR);
11710 break;
11711
11712 case CPP_MINUS:
11713 id = ansi_opname (MINUS_EXPR);
11714 break;
11715
11716 case CPP_MULT:
11717 id = ansi_opname (MULT_EXPR);
11718 break;
11719
11720 case CPP_DIV:
11721 id = ansi_opname (TRUNC_DIV_EXPR);
11722 break;
11723
11724 case CPP_MOD:
11725 id = ansi_opname (TRUNC_MOD_EXPR);
11726 break;
11727
11728 case CPP_XOR:
11729 id = ansi_opname (BIT_XOR_EXPR);
11730 break;
11731
11732 case CPP_AND:
11733 id = ansi_opname (BIT_AND_EXPR);
11734 break;
11735
11736 case CPP_OR:
11737 id = ansi_opname (BIT_IOR_EXPR);
11738 break;
11739
11740 case CPP_COMPL:
11741 id = ansi_opname (BIT_NOT_EXPR);
11742 break;
11743
11744 case CPP_NOT:
11745 id = ansi_opname (TRUTH_NOT_EXPR);
11746 break;
11747
11748 case CPP_EQ:
11749 id = ansi_assopname (NOP_EXPR);
11750 break;
11751
11752 case CPP_LESS:
11753 id = ansi_opname (LT_EXPR);
11754 break;
11755
11756 case CPP_GREATER:
11757 id = ansi_opname (GT_EXPR);
11758 break;
11759
11760 case CPP_PLUS_EQ:
11761 id = ansi_assopname (PLUS_EXPR);
11762 break;
11763
11764 case CPP_MINUS_EQ:
11765 id = ansi_assopname (MINUS_EXPR);
11766 break;
11767
11768 case CPP_MULT_EQ:
11769 id = ansi_assopname (MULT_EXPR);
11770 break;
11771
11772 case CPP_DIV_EQ:
11773 id = ansi_assopname (TRUNC_DIV_EXPR);
11774 break;
11775
11776 case CPP_MOD_EQ:
11777 id = ansi_assopname (TRUNC_MOD_EXPR);
11778 break;
11779
11780 case CPP_XOR_EQ:
11781 id = ansi_assopname (BIT_XOR_EXPR);
11782 break;
11783
11784 case CPP_AND_EQ:
11785 id = ansi_assopname (BIT_AND_EXPR);
11786 break;
11787
11788 case CPP_OR_EQ:
11789 id = ansi_assopname (BIT_IOR_EXPR);
11790 break;
11791
11792 case CPP_LSHIFT:
11793 id = ansi_opname (LSHIFT_EXPR);
11794 break;
11795
11796 case CPP_RSHIFT:
11797 id = ansi_opname (RSHIFT_EXPR);
11798 break;
11799
11800 case CPP_LSHIFT_EQ:
11801 id = ansi_assopname (LSHIFT_EXPR);
11802 break;
11803
11804 case CPP_RSHIFT_EQ:
11805 id = ansi_assopname (RSHIFT_EXPR);
11806 break;
11807
11808 case CPP_EQ_EQ:
11809 id = ansi_opname (EQ_EXPR);
11810 break;
11811
11812 case CPP_NOT_EQ:
11813 id = ansi_opname (NE_EXPR);
11814 break;
11815
11816 case CPP_LESS_EQ:
11817 id = ansi_opname (LE_EXPR);
11818 break;
11819
11820 case CPP_GREATER_EQ:
11821 id = ansi_opname (GE_EXPR);
11822 break;
11823
11824 case CPP_AND_AND:
11825 id = ansi_opname (TRUTH_ANDIF_EXPR);
11826 break;
11827
11828 case CPP_OR_OR:
11829 id = ansi_opname (TRUTH_ORIF_EXPR);
11830 break;
11831
11832 case CPP_PLUS_PLUS:
11833 id = ansi_opname (POSTINCREMENT_EXPR);
11834 break;
11835
11836 case CPP_MINUS_MINUS:
11837 id = ansi_opname (PREDECREMENT_EXPR);
11838 break;
11839
11840 case CPP_COMMA:
11841 id = ansi_opname (COMPOUND_EXPR);
11842 break;
11843
11844 case CPP_DEREF_STAR:
11845 id = ansi_opname (MEMBER_REF);
11846 break;
11847
11848 case CPP_DEREF:
11849 id = ansi_opname (COMPONENT_REF);
11850 break;
11851
11852 case CPP_OPEN_PAREN:
11853 /* Consume the `('. */
11854 cp_lexer_consume_token (parser->lexer);
11855 /* Look for the matching `)'. */
11856 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11857 return ansi_opname (CALL_EXPR);
11858
11859 case CPP_OPEN_SQUARE:
11860 /* Consume the `['. */
11861 cp_lexer_consume_token (parser->lexer);
11862 /* Look for the matching `]'. */
11863 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11864 return ansi_opname (ARRAY_REF);
11865
11866 case CPP_STRING:
11867 if (cxx_dialect == cxx98)
11868 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11869 if (TREE_STRING_LENGTH (token->u.value) > 2)
11870 {
11871 error ("expected empty string after %<operator%> keyword");
11872 return error_mark_node;
11873 }
11874 /* Consume the string. */
11875 cp_lexer_consume_token (parser->lexer);
11876 /* Look for the suffix identifier. */
11877 token = cp_lexer_peek_token (parser->lexer);
11878 if (token->type == CPP_NAME)
11879 {
11880 id = cp_parser_identifier (parser);
11881 if (id != error_mark_node)
11882 {
11883 const char *name = IDENTIFIER_POINTER (id);
11884 return cp_literal_operator_id (name);
11885 }
11886 }
11887 else
11888 {
11889 error ("expected suffix identifier");
11890 return error_mark_node;
11891 }
11892
11893 case CPP_STRING_USERDEF:
11894 error ("missing space between %<\"\"%> and suffix identifier");
11895 return error_mark_node;
11896
11897 default:
11898 /* Anything else is an error. */
11899 break;
11900 }
11901
11902 /* If we have selected an identifier, we need to consume the
11903 operator token. */
11904 if (id)
11905 cp_lexer_consume_token (parser->lexer);
11906 /* Otherwise, no valid operator name was present. */
11907 else
11908 {
11909 cp_parser_error (parser, "expected operator");
11910 id = error_mark_node;
11911 }
11912
11913 return id;
11914 }
11915
11916 /* Parse a template-declaration.
11917
11918 template-declaration:
11919 export [opt] template < template-parameter-list > declaration
11920
11921 If MEMBER_P is TRUE, this template-declaration occurs within a
11922 class-specifier.
11923
11924 The grammar rule given by the standard isn't correct. What
11925 is really meant is:
11926
11927 template-declaration:
11928 export [opt] template-parameter-list-seq
11929 decl-specifier-seq [opt] init-declarator [opt] ;
11930 export [opt] template-parameter-list-seq
11931 function-definition
11932
11933 template-parameter-list-seq:
11934 template-parameter-list-seq [opt]
11935 template < template-parameter-list > */
11936
11937 static void
11938 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11939 {
11940 /* Check for `export'. */
11941 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11942 {
11943 /* Consume the `export' token. */
11944 cp_lexer_consume_token (parser->lexer);
11945 /* Warn that we do not support `export'. */
11946 warning (0, "keyword %<export%> not implemented, and will be ignored");
11947 }
11948
11949 cp_parser_template_declaration_after_export (parser, member_p);
11950 }
11951
11952 /* Parse a template-parameter-list.
11953
11954 template-parameter-list:
11955 template-parameter
11956 template-parameter-list , template-parameter
11957
11958 Returns a TREE_LIST. Each node represents a template parameter.
11959 The nodes are connected via their TREE_CHAINs. */
11960
11961 static tree
11962 cp_parser_template_parameter_list (cp_parser* parser)
11963 {
11964 tree parameter_list = NULL_TREE;
11965
11966 begin_template_parm_list ();
11967
11968 /* The loop below parses the template parms. We first need to know
11969 the total number of template parms to be able to compute proper
11970 canonical types of each dependent type. So after the loop, when
11971 we know the total number of template parms,
11972 end_template_parm_list computes the proper canonical types and
11973 fixes up the dependent types accordingly. */
11974 while (true)
11975 {
11976 tree parameter;
11977 bool is_non_type;
11978 bool is_parameter_pack;
11979 location_t parm_loc;
11980
11981 /* Parse the template-parameter. */
11982 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11983 parameter = cp_parser_template_parameter (parser,
11984 &is_non_type,
11985 &is_parameter_pack);
11986 /* Add it to the list. */
11987 if (parameter != error_mark_node)
11988 parameter_list = process_template_parm (parameter_list,
11989 parm_loc,
11990 parameter,
11991 is_non_type,
11992 is_parameter_pack);
11993 else
11994 {
11995 tree err_parm = build_tree_list (parameter, parameter);
11996 parameter_list = chainon (parameter_list, err_parm);
11997 }
11998
11999 /* If the next token is not a `,', we're done. */
12000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12001 break;
12002 /* Otherwise, consume the `,' token. */
12003 cp_lexer_consume_token (parser->lexer);
12004 }
12005
12006 return end_template_parm_list (parameter_list);
12007 }
12008
12009 /* Parse a template-parameter.
12010
12011 template-parameter:
12012 type-parameter
12013 parameter-declaration
12014
12015 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12016 the parameter. The TREE_PURPOSE is the default value, if any.
12017 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12018 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12019 set to true iff this parameter is a parameter pack. */
12020
12021 static tree
12022 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12023 bool *is_parameter_pack)
12024 {
12025 cp_token *token;
12026 cp_parameter_declarator *parameter_declarator;
12027 cp_declarator *id_declarator;
12028 tree parm;
12029
12030 /* Assume it is a type parameter or a template parameter. */
12031 *is_non_type = false;
12032 /* Assume it not a parameter pack. */
12033 *is_parameter_pack = false;
12034 /* Peek at the next token. */
12035 token = cp_lexer_peek_token (parser->lexer);
12036 /* If it is `class' or `template', we have a type-parameter. */
12037 if (token->keyword == RID_TEMPLATE)
12038 return cp_parser_type_parameter (parser, is_parameter_pack);
12039 /* If it is `class' or `typename' we do not know yet whether it is a
12040 type parameter or a non-type parameter. Consider:
12041
12042 template <typename T, typename T::X X> ...
12043
12044 or:
12045
12046 template <class C, class D*> ...
12047
12048 Here, the first parameter is a type parameter, and the second is
12049 a non-type parameter. We can tell by looking at the token after
12050 the identifier -- if it is a `,', `=', or `>' then we have a type
12051 parameter. */
12052 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12053 {
12054 /* Peek at the token after `class' or `typename'. */
12055 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12056 /* If it's an ellipsis, we have a template type parameter
12057 pack. */
12058 if (token->type == CPP_ELLIPSIS)
12059 return cp_parser_type_parameter (parser, is_parameter_pack);
12060 /* If it's an identifier, skip it. */
12061 if (token->type == CPP_NAME)
12062 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12063 /* Now, see if the token looks like the end of a template
12064 parameter. */
12065 if (token->type == CPP_COMMA
12066 || token->type == CPP_EQ
12067 || token->type == CPP_GREATER)
12068 return cp_parser_type_parameter (parser, is_parameter_pack);
12069 }
12070
12071 /* Otherwise, it is a non-type parameter.
12072
12073 [temp.param]
12074
12075 When parsing a default template-argument for a non-type
12076 template-parameter, the first non-nested `>' is taken as the end
12077 of the template parameter-list rather than a greater-than
12078 operator. */
12079 *is_non_type = true;
12080 parameter_declarator
12081 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12082 /*parenthesized_p=*/NULL);
12083
12084 /* If the parameter declaration is marked as a parameter pack, set
12085 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12086 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12087 grokdeclarator. */
12088 if (parameter_declarator
12089 && parameter_declarator->declarator
12090 && parameter_declarator->declarator->parameter_pack_p)
12091 {
12092 *is_parameter_pack = true;
12093 parameter_declarator->declarator->parameter_pack_p = false;
12094 }
12095
12096 /* If the next token is an ellipsis, and we don't already have it
12097 marked as a parameter pack, then we have a parameter pack (that
12098 has no declarator). */
12099 if (!*is_parameter_pack
12100 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12101 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12102 {
12103 /* Consume the `...'. */
12104 cp_lexer_consume_token (parser->lexer);
12105 maybe_warn_variadic_templates ();
12106
12107 *is_parameter_pack = true;
12108 }
12109 /* We might end up with a pack expansion as the type of the non-type
12110 template parameter, in which case this is a non-type template
12111 parameter pack. */
12112 else if (parameter_declarator
12113 && parameter_declarator->decl_specifiers.type
12114 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12115 {
12116 *is_parameter_pack = true;
12117 parameter_declarator->decl_specifiers.type =
12118 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12119 }
12120
12121 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12122 {
12123 /* Parameter packs cannot have default arguments. However, a
12124 user may try to do so, so we'll parse them and give an
12125 appropriate diagnostic here. */
12126
12127 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12128
12129 /* Find the name of the parameter pack. */
12130 id_declarator = parameter_declarator->declarator;
12131 while (id_declarator && id_declarator->kind != cdk_id)
12132 id_declarator = id_declarator->declarator;
12133
12134 if (id_declarator && id_declarator->kind == cdk_id)
12135 error_at (start_token->location,
12136 "template parameter pack %qD cannot have a default argument",
12137 id_declarator->u.id.unqualified_name);
12138 else
12139 error_at (start_token->location,
12140 "template parameter pack cannot have a default argument");
12141
12142 /* Parse the default argument, but throw away the result. */
12143 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12144 }
12145
12146 parm = grokdeclarator (parameter_declarator->declarator,
12147 &parameter_declarator->decl_specifiers,
12148 TPARM, /*initialized=*/0,
12149 /*attrlist=*/NULL);
12150 if (parm == error_mark_node)
12151 return error_mark_node;
12152
12153 return build_tree_list (parameter_declarator->default_argument, parm);
12154 }
12155
12156 /* Parse a type-parameter.
12157
12158 type-parameter:
12159 class identifier [opt]
12160 class identifier [opt] = type-id
12161 typename identifier [opt]
12162 typename identifier [opt] = type-id
12163 template < template-parameter-list > class identifier [opt]
12164 template < template-parameter-list > class identifier [opt]
12165 = id-expression
12166
12167 GNU Extension (variadic templates):
12168
12169 type-parameter:
12170 class ... identifier [opt]
12171 typename ... identifier [opt]
12172
12173 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12174 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12175 the declaration of the parameter.
12176
12177 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12178
12179 static tree
12180 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12181 {
12182 cp_token *token;
12183 tree parameter;
12184
12185 /* Look for a keyword to tell us what kind of parameter this is. */
12186 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12187 if (!token)
12188 return error_mark_node;
12189
12190 switch (token->keyword)
12191 {
12192 case RID_CLASS:
12193 case RID_TYPENAME:
12194 {
12195 tree identifier;
12196 tree default_argument;
12197
12198 /* If the next token is an ellipsis, we have a template
12199 argument pack. */
12200 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12201 {
12202 /* Consume the `...' token. */
12203 cp_lexer_consume_token (parser->lexer);
12204 maybe_warn_variadic_templates ();
12205
12206 *is_parameter_pack = true;
12207 }
12208
12209 /* If the next token is an identifier, then it names the
12210 parameter. */
12211 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12212 identifier = cp_parser_identifier (parser);
12213 else
12214 identifier = NULL_TREE;
12215
12216 /* Create the parameter. */
12217 parameter = finish_template_type_parm (class_type_node, identifier);
12218
12219 /* If the next token is an `=', we have a default argument. */
12220 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12221 {
12222 /* Consume the `=' token. */
12223 cp_lexer_consume_token (parser->lexer);
12224 /* Parse the default-argument. */
12225 push_deferring_access_checks (dk_no_deferred);
12226 default_argument = cp_parser_type_id (parser);
12227
12228 /* Template parameter packs cannot have default
12229 arguments. */
12230 if (*is_parameter_pack)
12231 {
12232 if (identifier)
12233 error_at (token->location,
12234 "template parameter pack %qD cannot have a "
12235 "default argument", identifier);
12236 else
12237 error_at (token->location,
12238 "template parameter packs cannot have "
12239 "default arguments");
12240 default_argument = NULL_TREE;
12241 }
12242 pop_deferring_access_checks ();
12243 }
12244 else
12245 default_argument = NULL_TREE;
12246
12247 /* Create the combined representation of the parameter and the
12248 default argument. */
12249 parameter = build_tree_list (default_argument, parameter);
12250 }
12251 break;
12252
12253 case RID_TEMPLATE:
12254 {
12255 tree identifier;
12256 tree default_argument;
12257
12258 /* Look for the `<'. */
12259 cp_parser_require (parser, CPP_LESS, RT_LESS);
12260 /* Parse the template-parameter-list. */
12261 cp_parser_template_parameter_list (parser);
12262 /* Look for the `>'. */
12263 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12264 /* Look for the `class' keyword. */
12265 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12266 /* If the next token is an ellipsis, we have a template
12267 argument pack. */
12268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12269 {
12270 /* Consume the `...' token. */
12271 cp_lexer_consume_token (parser->lexer);
12272 maybe_warn_variadic_templates ();
12273
12274 *is_parameter_pack = true;
12275 }
12276 /* If the next token is an `=', then there is a
12277 default-argument. If the next token is a `>', we are at
12278 the end of the parameter-list. If the next token is a `,',
12279 then we are at the end of this parameter. */
12280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12281 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12282 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12283 {
12284 identifier = cp_parser_identifier (parser);
12285 /* Treat invalid names as if the parameter were nameless. */
12286 if (identifier == error_mark_node)
12287 identifier = NULL_TREE;
12288 }
12289 else
12290 identifier = NULL_TREE;
12291
12292 /* Create the template parameter. */
12293 parameter = finish_template_template_parm (class_type_node,
12294 identifier);
12295
12296 /* If the next token is an `=', then there is a
12297 default-argument. */
12298 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12299 {
12300 bool is_template;
12301
12302 /* Consume the `='. */
12303 cp_lexer_consume_token (parser->lexer);
12304 /* Parse the id-expression. */
12305 push_deferring_access_checks (dk_no_deferred);
12306 /* save token before parsing the id-expression, for error
12307 reporting */
12308 token = cp_lexer_peek_token (parser->lexer);
12309 default_argument
12310 = cp_parser_id_expression (parser,
12311 /*template_keyword_p=*/false,
12312 /*check_dependency_p=*/true,
12313 /*template_p=*/&is_template,
12314 /*declarator_p=*/false,
12315 /*optional_p=*/false);
12316 if (TREE_CODE (default_argument) == TYPE_DECL)
12317 /* If the id-expression was a template-id that refers to
12318 a template-class, we already have the declaration here,
12319 so no further lookup is needed. */
12320 ;
12321 else
12322 /* Look up the name. */
12323 default_argument
12324 = cp_parser_lookup_name (parser, default_argument,
12325 none_type,
12326 /*is_template=*/is_template,
12327 /*is_namespace=*/false,
12328 /*check_dependency=*/true,
12329 /*ambiguous_decls=*/NULL,
12330 token->location);
12331 /* See if the default argument is valid. */
12332 default_argument
12333 = check_template_template_default_arg (default_argument);
12334
12335 /* Template parameter packs cannot have default
12336 arguments. */
12337 if (*is_parameter_pack)
12338 {
12339 if (identifier)
12340 error_at (token->location,
12341 "template parameter pack %qD cannot "
12342 "have a default argument",
12343 identifier);
12344 else
12345 error_at (token->location, "template parameter packs cannot "
12346 "have default arguments");
12347 default_argument = NULL_TREE;
12348 }
12349 pop_deferring_access_checks ();
12350 }
12351 else
12352 default_argument = NULL_TREE;
12353
12354 /* Create the combined representation of the parameter and the
12355 default argument. */
12356 parameter = build_tree_list (default_argument, parameter);
12357 }
12358 break;
12359
12360 default:
12361 gcc_unreachable ();
12362 break;
12363 }
12364
12365 return parameter;
12366 }
12367
12368 /* Parse a template-id.
12369
12370 template-id:
12371 template-name < template-argument-list [opt] >
12372
12373 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12374 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12375 returned. Otherwise, if the template-name names a function, or set
12376 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12377 names a class, returns a TYPE_DECL for the specialization.
12378
12379 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12380 uninstantiated templates. */
12381
12382 static tree
12383 cp_parser_template_id (cp_parser *parser,
12384 bool template_keyword_p,
12385 bool check_dependency_p,
12386 enum tag_types tag_type,
12387 bool is_declaration)
12388 {
12389 int i;
12390 tree templ;
12391 tree arguments;
12392 tree template_id;
12393 cp_token_position start_of_id = 0;
12394 deferred_access_check *chk;
12395 VEC (deferred_access_check,gc) *access_check;
12396 cp_token *next_token = NULL, *next_token_2 = NULL;
12397 bool is_identifier;
12398
12399 /* If the next token corresponds to a template-id, there is no need
12400 to reparse it. */
12401 next_token = cp_lexer_peek_token (parser->lexer);
12402 if (next_token->type == CPP_TEMPLATE_ID)
12403 {
12404 struct tree_check *check_value;
12405
12406 /* Get the stored value. */
12407 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12408 /* Perform any access checks that were deferred. */
12409 access_check = check_value->checks;
12410 if (access_check)
12411 {
12412 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12413 perform_or_defer_access_check (chk->binfo,
12414 chk->decl,
12415 chk->diag_decl,
12416 tf_warning_or_error);
12417 }
12418 /* Return the stored value. */
12419 return check_value->value;
12420 }
12421
12422 /* Avoid performing name lookup if there is no possibility of
12423 finding a template-id. */
12424 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12425 || (next_token->type == CPP_NAME
12426 && !cp_parser_nth_token_starts_template_argument_list_p
12427 (parser, 2)))
12428 {
12429 cp_parser_error (parser, "expected template-id");
12430 return error_mark_node;
12431 }
12432
12433 /* Remember where the template-id starts. */
12434 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12435 start_of_id = cp_lexer_token_position (parser->lexer, false);
12436
12437 push_deferring_access_checks (dk_deferred);
12438
12439 /* Parse the template-name. */
12440 is_identifier = false;
12441 templ = cp_parser_template_name (parser, template_keyword_p,
12442 check_dependency_p,
12443 is_declaration,
12444 tag_type,
12445 &is_identifier);
12446 if (templ == error_mark_node || is_identifier)
12447 {
12448 pop_deferring_access_checks ();
12449 return templ;
12450 }
12451
12452 /* If we find the sequence `[:' after a template-name, it's probably
12453 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12454 parse correctly the argument list. */
12455 next_token = cp_lexer_peek_token (parser->lexer);
12456 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12457 if (next_token->type == CPP_OPEN_SQUARE
12458 && next_token->flags & DIGRAPH
12459 && next_token_2->type == CPP_COLON
12460 && !(next_token_2->flags & PREV_WHITE))
12461 {
12462 cp_parser_parse_tentatively (parser);
12463 /* Change `:' into `::'. */
12464 next_token_2->type = CPP_SCOPE;
12465 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12466 CPP_LESS. */
12467 cp_lexer_consume_token (parser->lexer);
12468
12469 /* Parse the arguments. */
12470 arguments = cp_parser_enclosed_template_argument_list (parser);
12471 if (!cp_parser_parse_definitely (parser))
12472 {
12473 /* If we couldn't parse an argument list, then we revert our changes
12474 and return simply an error. Maybe this is not a template-id
12475 after all. */
12476 next_token_2->type = CPP_COLON;
12477 cp_parser_error (parser, "expected %<<%>");
12478 pop_deferring_access_checks ();
12479 return error_mark_node;
12480 }
12481 /* Otherwise, emit an error about the invalid digraph, but continue
12482 parsing because we got our argument list. */
12483 if (permerror (next_token->location,
12484 "%<<::%> cannot begin a template-argument list"))
12485 {
12486 static bool hint = false;
12487 inform (next_token->location,
12488 "%<<:%> is an alternate spelling for %<[%>."
12489 " Insert whitespace between %<<%> and %<::%>");
12490 if (!hint && !flag_permissive)
12491 {
12492 inform (next_token->location, "(if you use %<-fpermissive%>"
12493 " G++ will accept your code)");
12494 hint = true;
12495 }
12496 }
12497 }
12498 else
12499 {
12500 /* Look for the `<' that starts the template-argument-list. */
12501 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12502 {
12503 pop_deferring_access_checks ();
12504 return error_mark_node;
12505 }
12506 /* Parse the arguments. */
12507 arguments = cp_parser_enclosed_template_argument_list (parser);
12508 }
12509
12510 /* Build a representation of the specialization. */
12511 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12512 template_id = build_min_nt_loc (next_token->location,
12513 TEMPLATE_ID_EXPR,
12514 templ, arguments);
12515 else if (DECL_TYPE_TEMPLATE_P (templ)
12516 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12517 {
12518 bool entering_scope;
12519 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12520 template (rather than some instantiation thereof) only if
12521 is not nested within some other construct. For example, in
12522 "template <typename T> void f(T) { A<T>::", A<T> is just an
12523 instantiation of A. */
12524 entering_scope = (template_parm_scope_p ()
12525 && cp_lexer_next_token_is (parser->lexer,
12526 CPP_SCOPE));
12527 template_id
12528 = finish_template_type (templ, arguments, entering_scope);
12529 }
12530 else
12531 {
12532 /* If it's not a class-template or a template-template, it should be
12533 a function-template. */
12534 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12535 || TREE_CODE (templ) == OVERLOAD
12536 || BASELINK_P (templ)));
12537
12538 template_id = lookup_template_function (templ, arguments);
12539 }
12540
12541 /* If parsing tentatively, replace the sequence of tokens that makes
12542 up the template-id with a CPP_TEMPLATE_ID token. That way,
12543 should we re-parse the token stream, we will not have to repeat
12544 the effort required to do the parse, nor will we issue duplicate
12545 error messages about problems during instantiation of the
12546 template. */
12547 if (start_of_id)
12548 {
12549 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12550
12551 /* Reset the contents of the START_OF_ID token. */
12552 token->type = CPP_TEMPLATE_ID;
12553 /* Retrieve any deferred checks. Do not pop this access checks yet
12554 so the memory will not be reclaimed during token replacing below. */
12555 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12556 token->u.tree_check_value->value = template_id;
12557 token->u.tree_check_value->checks = get_deferred_access_checks ();
12558 token->keyword = RID_MAX;
12559
12560 /* Purge all subsequent tokens. */
12561 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12562
12563 /* ??? Can we actually assume that, if template_id ==
12564 error_mark_node, we will have issued a diagnostic to the
12565 user, as opposed to simply marking the tentative parse as
12566 failed? */
12567 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12568 error_at (token->location, "parse error in template argument list");
12569 }
12570
12571 pop_deferring_access_checks ();
12572 return template_id;
12573 }
12574
12575 /* Parse a template-name.
12576
12577 template-name:
12578 identifier
12579
12580 The standard should actually say:
12581
12582 template-name:
12583 identifier
12584 operator-function-id
12585
12586 A defect report has been filed about this issue.
12587
12588 A conversion-function-id cannot be a template name because they cannot
12589 be part of a template-id. In fact, looking at this code:
12590
12591 a.operator K<int>()
12592
12593 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12594 It is impossible to call a templated conversion-function-id with an
12595 explicit argument list, since the only allowed template parameter is
12596 the type to which it is converting.
12597
12598 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12599 `template' keyword, in a construction like:
12600
12601 T::template f<3>()
12602
12603 In that case `f' is taken to be a template-name, even though there
12604 is no way of knowing for sure.
12605
12606 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12607 name refers to a set of overloaded functions, at least one of which
12608 is a template, or an IDENTIFIER_NODE with the name of the template,
12609 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12610 names are looked up inside uninstantiated templates. */
12611
12612 static tree
12613 cp_parser_template_name (cp_parser* parser,
12614 bool template_keyword_p,
12615 bool check_dependency_p,
12616 bool is_declaration,
12617 enum tag_types tag_type,
12618 bool *is_identifier)
12619 {
12620 tree identifier;
12621 tree decl;
12622 tree fns;
12623 cp_token *token = cp_lexer_peek_token (parser->lexer);
12624
12625 /* If the next token is `operator', then we have either an
12626 operator-function-id or a conversion-function-id. */
12627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12628 {
12629 /* We don't know whether we're looking at an
12630 operator-function-id or a conversion-function-id. */
12631 cp_parser_parse_tentatively (parser);
12632 /* Try an operator-function-id. */
12633 identifier = cp_parser_operator_function_id (parser);
12634 /* If that didn't work, try a conversion-function-id. */
12635 if (!cp_parser_parse_definitely (parser))
12636 {
12637 cp_parser_error (parser, "expected template-name");
12638 return error_mark_node;
12639 }
12640 }
12641 /* Look for the identifier. */
12642 else
12643 identifier = cp_parser_identifier (parser);
12644
12645 /* If we didn't find an identifier, we don't have a template-id. */
12646 if (identifier == error_mark_node)
12647 return error_mark_node;
12648
12649 /* If the name immediately followed the `template' keyword, then it
12650 is a template-name. However, if the next token is not `<', then
12651 we do not treat it as a template-name, since it is not being used
12652 as part of a template-id. This enables us to handle constructs
12653 like:
12654
12655 template <typename T> struct S { S(); };
12656 template <typename T> S<T>::S();
12657
12658 correctly. We would treat `S' as a template -- if it were `S<T>'
12659 -- but we do not if there is no `<'. */
12660
12661 if (processing_template_decl
12662 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12663 {
12664 /* In a declaration, in a dependent context, we pretend that the
12665 "template" keyword was present in order to improve error
12666 recovery. For example, given:
12667
12668 template <typename T> void f(T::X<int>);
12669
12670 we want to treat "X<int>" as a template-id. */
12671 if (is_declaration
12672 && !template_keyword_p
12673 && parser->scope && TYPE_P (parser->scope)
12674 && check_dependency_p
12675 && dependent_scope_p (parser->scope)
12676 /* Do not do this for dtors (or ctors), since they never
12677 need the template keyword before their name. */
12678 && !constructor_name_p (identifier, parser->scope))
12679 {
12680 cp_token_position start = 0;
12681
12682 /* Explain what went wrong. */
12683 error_at (token->location, "non-template %qD used as template",
12684 identifier);
12685 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12686 parser->scope, identifier);
12687 /* If parsing tentatively, find the location of the "<" token. */
12688 if (cp_parser_simulate_error (parser))
12689 start = cp_lexer_token_position (parser->lexer, true);
12690 /* Parse the template arguments so that we can issue error
12691 messages about them. */
12692 cp_lexer_consume_token (parser->lexer);
12693 cp_parser_enclosed_template_argument_list (parser);
12694 /* Skip tokens until we find a good place from which to
12695 continue parsing. */
12696 cp_parser_skip_to_closing_parenthesis (parser,
12697 /*recovering=*/true,
12698 /*or_comma=*/true,
12699 /*consume_paren=*/false);
12700 /* If parsing tentatively, permanently remove the
12701 template argument list. That will prevent duplicate
12702 error messages from being issued about the missing
12703 "template" keyword. */
12704 if (start)
12705 cp_lexer_purge_tokens_after (parser->lexer, start);
12706 if (is_identifier)
12707 *is_identifier = true;
12708 return identifier;
12709 }
12710
12711 /* If the "template" keyword is present, then there is generally
12712 no point in doing name-lookup, so we just return IDENTIFIER.
12713 But, if the qualifying scope is non-dependent then we can
12714 (and must) do name-lookup normally. */
12715 if (template_keyword_p
12716 && (!parser->scope
12717 || (TYPE_P (parser->scope)
12718 && dependent_type_p (parser->scope))))
12719 return identifier;
12720 }
12721
12722 /* Look up the name. */
12723 decl = cp_parser_lookup_name (parser, identifier,
12724 tag_type,
12725 /*is_template=*/true,
12726 /*is_namespace=*/false,
12727 check_dependency_p,
12728 /*ambiguous_decls=*/NULL,
12729 token->location);
12730
12731 /* If DECL is a template, then the name was a template-name. */
12732 if (TREE_CODE (decl) == TEMPLATE_DECL)
12733 ;
12734 else
12735 {
12736 tree fn = NULL_TREE;
12737
12738 /* The standard does not explicitly indicate whether a name that
12739 names a set of overloaded declarations, some of which are
12740 templates, is a template-name. However, such a name should
12741 be a template-name; otherwise, there is no way to form a
12742 template-id for the overloaded templates. */
12743 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12744 if (TREE_CODE (fns) == OVERLOAD)
12745 for (fn = fns; fn; fn = OVL_NEXT (fn))
12746 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12747 break;
12748
12749 if (!fn)
12750 {
12751 /* The name does not name a template. */
12752 cp_parser_error (parser, "expected template-name");
12753 return error_mark_node;
12754 }
12755 }
12756
12757 /* If DECL is dependent, and refers to a function, then just return
12758 its name; we will look it up again during template instantiation. */
12759 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12760 {
12761 tree scope = ovl_scope (decl);
12762 if (TYPE_P (scope) && dependent_type_p (scope))
12763 return identifier;
12764 }
12765
12766 return decl;
12767 }
12768
12769 /* Parse a template-argument-list.
12770
12771 template-argument-list:
12772 template-argument ... [opt]
12773 template-argument-list , template-argument ... [opt]
12774
12775 Returns a TREE_VEC containing the arguments. */
12776
12777 static tree
12778 cp_parser_template_argument_list (cp_parser* parser)
12779 {
12780 tree fixed_args[10];
12781 unsigned n_args = 0;
12782 unsigned alloced = 10;
12783 tree *arg_ary = fixed_args;
12784 tree vec;
12785 bool saved_in_template_argument_list_p;
12786 bool saved_ice_p;
12787 bool saved_non_ice_p;
12788
12789 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12790 parser->in_template_argument_list_p = true;
12791 /* Even if the template-id appears in an integral
12792 constant-expression, the contents of the argument list do
12793 not. */
12794 saved_ice_p = parser->integral_constant_expression_p;
12795 parser->integral_constant_expression_p = false;
12796 saved_non_ice_p = parser->non_integral_constant_expression_p;
12797 parser->non_integral_constant_expression_p = false;
12798
12799 /* Parse the arguments. */
12800 do
12801 {
12802 tree argument;
12803
12804 if (n_args)
12805 /* Consume the comma. */
12806 cp_lexer_consume_token (parser->lexer);
12807
12808 /* Parse the template-argument. */
12809 argument = cp_parser_template_argument (parser);
12810
12811 /* If the next token is an ellipsis, we're expanding a template
12812 argument pack. */
12813 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12814 {
12815 if (argument == error_mark_node)
12816 {
12817 cp_token *token = cp_lexer_peek_token (parser->lexer);
12818 error_at (token->location,
12819 "expected parameter pack before %<...%>");
12820 }
12821 /* Consume the `...' token. */
12822 cp_lexer_consume_token (parser->lexer);
12823
12824 /* Make the argument into a TYPE_PACK_EXPANSION or
12825 EXPR_PACK_EXPANSION. */
12826 argument = make_pack_expansion (argument);
12827 }
12828
12829 if (n_args == alloced)
12830 {
12831 alloced *= 2;
12832
12833 if (arg_ary == fixed_args)
12834 {
12835 arg_ary = XNEWVEC (tree, alloced);
12836 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12837 }
12838 else
12839 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12840 }
12841 arg_ary[n_args++] = argument;
12842 }
12843 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12844
12845 vec = make_tree_vec (n_args);
12846
12847 while (n_args--)
12848 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12849
12850 if (arg_ary != fixed_args)
12851 free (arg_ary);
12852 parser->non_integral_constant_expression_p = saved_non_ice_p;
12853 parser->integral_constant_expression_p = saved_ice_p;
12854 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12855 #ifdef ENABLE_CHECKING
12856 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12857 #endif
12858 return vec;
12859 }
12860
12861 /* Parse a template-argument.
12862
12863 template-argument:
12864 assignment-expression
12865 type-id
12866 id-expression
12867
12868 The representation is that of an assignment-expression, type-id, or
12869 id-expression -- except that the qualified id-expression is
12870 evaluated, so that the value returned is either a DECL or an
12871 OVERLOAD.
12872
12873 Although the standard says "assignment-expression", it forbids
12874 throw-expressions or assignments in the template argument.
12875 Therefore, we use "conditional-expression" instead. */
12876
12877 static tree
12878 cp_parser_template_argument (cp_parser* parser)
12879 {
12880 tree argument;
12881 bool template_p;
12882 bool address_p;
12883 bool maybe_type_id = false;
12884 cp_token *token = NULL, *argument_start_token = NULL;
12885 location_t loc = 0;
12886 cp_id_kind idk;
12887
12888 /* There's really no way to know what we're looking at, so we just
12889 try each alternative in order.
12890
12891 [temp.arg]
12892
12893 In a template-argument, an ambiguity between a type-id and an
12894 expression is resolved to a type-id, regardless of the form of
12895 the corresponding template-parameter.
12896
12897 Therefore, we try a type-id first. */
12898 cp_parser_parse_tentatively (parser);
12899 argument = cp_parser_template_type_arg (parser);
12900 /* If there was no error parsing the type-id but the next token is a
12901 '>>', our behavior depends on which dialect of C++ we're
12902 parsing. In C++98, we probably found a typo for '> >'. But there
12903 are type-id which are also valid expressions. For instance:
12904
12905 struct X { int operator >> (int); };
12906 template <int V> struct Foo {};
12907 Foo<X () >> 5> r;
12908
12909 Here 'X()' is a valid type-id of a function type, but the user just
12910 wanted to write the expression "X() >> 5". Thus, we remember that we
12911 found a valid type-id, but we still try to parse the argument as an
12912 expression to see what happens.
12913
12914 In C++0x, the '>>' will be considered two separate '>'
12915 tokens. */
12916 if (!cp_parser_error_occurred (parser)
12917 && cxx_dialect == cxx98
12918 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12919 {
12920 maybe_type_id = true;
12921 cp_parser_abort_tentative_parse (parser);
12922 }
12923 else
12924 {
12925 /* If the next token isn't a `,' or a `>', then this argument wasn't
12926 really finished. This means that the argument is not a valid
12927 type-id. */
12928 if (!cp_parser_next_token_ends_template_argument_p (parser))
12929 cp_parser_error (parser, "expected template-argument");
12930 /* If that worked, we're done. */
12931 if (cp_parser_parse_definitely (parser))
12932 return argument;
12933 }
12934 /* We're still not sure what the argument will be. */
12935 cp_parser_parse_tentatively (parser);
12936 /* Try a template. */
12937 argument_start_token = cp_lexer_peek_token (parser->lexer);
12938 argument = cp_parser_id_expression (parser,
12939 /*template_keyword_p=*/false,
12940 /*check_dependency_p=*/true,
12941 &template_p,
12942 /*declarator_p=*/false,
12943 /*optional_p=*/false);
12944 /* If the next token isn't a `,' or a `>', then this argument wasn't
12945 really finished. */
12946 if (!cp_parser_next_token_ends_template_argument_p (parser))
12947 cp_parser_error (parser, "expected template-argument");
12948 if (!cp_parser_error_occurred (parser))
12949 {
12950 /* Figure out what is being referred to. If the id-expression
12951 was for a class template specialization, then we will have a
12952 TYPE_DECL at this point. There is no need to do name lookup
12953 at this point in that case. */
12954 if (TREE_CODE (argument) != TYPE_DECL)
12955 argument = cp_parser_lookup_name (parser, argument,
12956 none_type,
12957 /*is_template=*/template_p,
12958 /*is_namespace=*/false,
12959 /*check_dependency=*/true,
12960 /*ambiguous_decls=*/NULL,
12961 argument_start_token->location);
12962 if (TREE_CODE (argument) != TEMPLATE_DECL
12963 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12964 cp_parser_error (parser, "expected template-name");
12965 }
12966 if (cp_parser_parse_definitely (parser))
12967 return argument;
12968 /* It must be a non-type argument. There permitted cases are given
12969 in [temp.arg.nontype]:
12970
12971 -- an integral constant-expression of integral or enumeration
12972 type; or
12973
12974 -- the name of a non-type template-parameter; or
12975
12976 -- the name of an object or function with external linkage...
12977
12978 -- the address of an object or function with external linkage...
12979
12980 -- a pointer to member... */
12981 /* Look for a non-type template parameter. */
12982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12983 {
12984 cp_parser_parse_tentatively (parser);
12985 argument = cp_parser_primary_expression (parser,
12986 /*address_p=*/false,
12987 /*cast_p=*/false,
12988 /*template_arg_p=*/true,
12989 &idk);
12990 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12991 || !cp_parser_next_token_ends_template_argument_p (parser))
12992 cp_parser_simulate_error (parser);
12993 if (cp_parser_parse_definitely (parser))
12994 return argument;
12995 }
12996
12997 /* If the next token is "&", the argument must be the address of an
12998 object or function with external linkage. */
12999 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13000 if (address_p)
13001 {
13002 loc = cp_lexer_peek_token (parser->lexer)->location;
13003 cp_lexer_consume_token (parser->lexer);
13004 }
13005 /* See if we might have an id-expression. */
13006 token = cp_lexer_peek_token (parser->lexer);
13007 if (token->type == CPP_NAME
13008 || token->keyword == RID_OPERATOR
13009 || token->type == CPP_SCOPE
13010 || token->type == CPP_TEMPLATE_ID
13011 || token->type == CPP_NESTED_NAME_SPECIFIER)
13012 {
13013 cp_parser_parse_tentatively (parser);
13014 argument = cp_parser_primary_expression (parser,
13015 address_p,
13016 /*cast_p=*/false,
13017 /*template_arg_p=*/true,
13018 &idk);
13019 if (cp_parser_error_occurred (parser)
13020 || !cp_parser_next_token_ends_template_argument_p (parser))
13021 cp_parser_abort_tentative_parse (parser);
13022 else
13023 {
13024 tree probe;
13025
13026 if (TREE_CODE (argument) == INDIRECT_REF)
13027 {
13028 gcc_assert (REFERENCE_REF_P (argument));
13029 argument = TREE_OPERAND (argument, 0);
13030 }
13031
13032 /* If we're in a template, we represent a qualified-id referring
13033 to a static data member as a SCOPE_REF even if the scope isn't
13034 dependent so that we can check access control later. */
13035 probe = argument;
13036 if (TREE_CODE (probe) == SCOPE_REF)
13037 probe = TREE_OPERAND (probe, 1);
13038 if (TREE_CODE (probe) == VAR_DECL)
13039 {
13040 /* A variable without external linkage might still be a
13041 valid constant-expression, so no error is issued here
13042 if the external-linkage check fails. */
13043 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13044 cp_parser_simulate_error (parser);
13045 }
13046 else if (is_overloaded_fn (argument))
13047 /* All overloaded functions are allowed; if the external
13048 linkage test does not pass, an error will be issued
13049 later. */
13050 ;
13051 else if (address_p
13052 && (TREE_CODE (argument) == OFFSET_REF
13053 || TREE_CODE (argument) == SCOPE_REF))
13054 /* A pointer-to-member. */
13055 ;
13056 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13057 ;
13058 else
13059 cp_parser_simulate_error (parser);
13060
13061 if (cp_parser_parse_definitely (parser))
13062 {
13063 if (address_p)
13064 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13065 tf_warning_or_error);
13066 return argument;
13067 }
13068 }
13069 }
13070 /* If the argument started with "&", there are no other valid
13071 alternatives at this point. */
13072 if (address_p)
13073 {
13074 cp_parser_error (parser, "invalid non-type template argument");
13075 return error_mark_node;
13076 }
13077
13078 /* If the argument wasn't successfully parsed as a type-id followed
13079 by '>>', the argument can only be a constant expression now.
13080 Otherwise, we try parsing the constant-expression tentatively,
13081 because the argument could really be a type-id. */
13082 if (maybe_type_id)
13083 cp_parser_parse_tentatively (parser);
13084 argument = cp_parser_constant_expression (parser,
13085 /*allow_non_constant_p=*/false,
13086 /*non_constant_p=*/NULL);
13087 argument = fold_non_dependent_expr (argument);
13088 if (!maybe_type_id)
13089 return argument;
13090 if (!cp_parser_next_token_ends_template_argument_p (parser))
13091 cp_parser_error (parser, "expected template-argument");
13092 if (cp_parser_parse_definitely (parser))
13093 return argument;
13094 /* We did our best to parse the argument as a non type-id, but that
13095 was the only alternative that matched (albeit with a '>' after
13096 it). We can assume it's just a typo from the user, and a
13097 diagnostic will then be issued. */
13098 return cp_parser_template_type_arg (parser);
13099 }
13100
13101 /* Parse an explicit-instantiation.
13102
13103 explicit-instantiation:
13104 template declaration
13105
13106 Although the standard says `declaration', what it really means is:
13107
13108 explicit-instantiation:
13109 template decl-specifier-seq [opt] declarator [opt] ;
13110
13111 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13112 supposed to be allowed. A defect report has been filed about this
13113 issue.
13114
13115 GNU Extension:
13116
13117 explicit-instantiation:
13118 storage-class-specifier template
13119 decl-specifier-seq [opt] declarator [opt] ;
13120 function-specifier template
13121 decl-specifier-seq [opt] declarator [opt] ; */
13122
13123 static void
13124 cp_parser_explicit_instantiation (cp_parser* parser)
13125 {
13126 int declares_class_or_enum;
13127 cp_decl_specifier_seq decl_specifiers;
13128 tree extension_specifier = NULL_TREE;
13129
13130 timevar_push (TV_TEMPLATE_INST);
13131
13132 /* Look for an (optional) storage-class-specifier or
13133 function-specifier. */
13134 if (cp_parser_allow_gnu_extensions_p (parser))
13135 {
13136 extension_specifier
13137 = cp_parser_storage_class_specifier_opt (parser);
13138 if (!extension_specifier)
13139 extension_specifier
13140 = cp_parser_function_specifier_opt (parser,
13141 /*decl_specs=*/NULL);
13142 }
13143
13144 /* Look for the `template' keyword. */
13145 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13146 /* Let the front end know that we are processing an explicit
13147 instantiation. */
13148 begin_explicit_instantiation ();
13149 /* [temp.explicit] says that we are supposed to ignore access
13150 control while processing explicit instantiation directives. */
13151 push_deferring_access_checks (dk_no_check);
13152 /* Parse a decl-specifier-seq. */
13153 cp_parser_decl_specifier_seq (parser,
13154 CP_PARSER_FLAGS_OPTIONAL,
13155 &decl_specifiers,
13156 &declares_class_or_enum);
13157 /* If there was exactly one decl-specifier, and it declared a class,
13158 and there's no declarator, then we have an explicit type
13159 instantiation. */
13160 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13161 {
13162 tree type;
13163
13164 type = check_tag_decl (&decl_specifiers);
13165 /* Turn access control back on for names used during
13166 template instantiation. */
13167 pop_deferring_access_checks ();
13168 if (type)
13169 do_type_instantiation (type, extension_specifier,
13170 /*complain=*/tf_error);
13171 }
13172 else
13173 {
13174 cp_declarator *declarator;
13175 tree decl;
13176
13177 /* Parse the declarator. */
13178 declarator
13179 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13180 /*ctor_dtor_or_conv_p=*/NULL,
13181 /*parenthesized_p=*/NULL,
13182 /*member_p=*/false);
13183 if (declares_class_or_enum & 2)
13184 cp_parser_check_for_definition_in_return_type (declarator,
13185 decl_specifiers.type,
13186 decl_specifiers.locations[ds_type_spec]);
13187 if (declarator != cp_error_declarator)
13188 {
13189 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13190 permerror (decl_specifiers.locations[ds_inline],
13191 "explicit instantiation shall not use"
13192 " %<inline%> specifier");
13193 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13194 permerror (decl_specifiers.locations[ds_constexpr],
13195 "explicit instantiation shall not use"
13196 " %<constexpr%> specifier");
13197
13198 decl = grokdeclarator (declarator, &decl_specifiers,
13199 NORMAL, 0, &decl_specifiers.attributes);
13200 /* Turn access control back on for names used during
13201 template instantiation. */
13202 pop_deferring_access_checks ();
13203 /* Do the explicit instantiation. */
13204 do_decl_instantiation (decl, extension_specifier);
13205 }
13206 else
13207 {
13208 pop_deferring_access_checks ();
13209 /* Skip the body of the explicit instantiation. */
13210 cp_parser_skip_to_end_of_statement (parser);
13211 }
13212 }
13213 /* We're done with the instantiation. */
13214 end_explicit_instantiation ();
13215
13216 cp_parser_consume_semicolon_at_end_of_statement (parser);
13217
13218 timevar_pop (TV_TEMPLATE_INST);
13219 }
13220
13221 /* Parse an explicit-specialization.
13222
13223 explicit-specialization:
13224 template < > declaration
13225
13226 Although the standard says `declaration', what it really means is:
13227
13228 explicit-specialization:
13229 template <> decl-specifier [opt] init-declarator [opt] ;
13230 template <> function-definition
13231 template <> explicit-specialization
13232 template <> template-declaration */
13233
13234 static void
13235 cp_parser_explicit_specialization (cp_parser* parser)
13236 {
13237 bool need_lang_pop;
13238 cp_token *token = cp_lexer_peek_token (parser->lexer);
13239
13240 /* Look for the `template' keyword. */
13241 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13242 /* Look for the `<'. */
13243 cp_parser_require (parser, CPP_LESS, RT_LESS);
13244 /* Look for the `>'. */
13245 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13246 /* We have processed another parameter list. */
13247 ++parser->num_template_parameter_lists;
13248 /* [temp]
13249
13250 A template ... explicit specialization ... shall not have C
13251 linkage. */
13252 if (current_lang_name == lang_name_c)
13253 {
13254 error_at (token->location, "template specialization with C linkage");
13255 /* Give it C++ linkage to avoid confusing other parts of the
13256 front end. */
13257 push_lang_context (lang_name_cplusplus);
13258 need_lang_pop = true;
13259 }
13260 else
13261 need_lang_pop = false;
13262 /* Let the front end know that we are beginning a specialization. */
13263 if (!begin_specialization ())
13264 {
13265 end_specialization ();
13266 return;
13267 }
13268
13269 /* If the next keyword is `template', we need to figure out whether
13270 or not we're looking a template-declaration. */
13271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13272 {
13273 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13274 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13275 cp_parser_template_declaration_after_export (parser,
13276 /*member_p=*/false);
13277 else
13278 cp_parser_explicit_specialization (parser);
13279 }
13280 else
13281 /* Parse the dependent declaration. */
13282 cp_parser_single_declaration (parser,
13283 /*checks=*/NULL,
13284 /*member_p=*/false,
13285 /*explicit_specialization_p=*/true,
13286 /*friend_p=*/NULL);
13287 /* We're done with the specialization. */
13288 end_specialization ();
13289 /* For the erroneous case of a template with C linkage, we pushed an
13290 implicit C++ linkage scope; exit that scope now. */
13291 if (need_lang_pop)
13292 pop_lang_context ();
13293 /* We're done with this parameter list. */
13294 --parser->num_template_parameter_lists;
13295 }
13296
13297 /* Parse a type-specifier.
13298
13299 type-specifier:
13300 simple-type-specifier
13301 class-specifier
13302 enum-specifier
13303 elaborated-type-specifier
13304 cv-qualifier
13305
13306 GNU Extension:
13307
13308 type-specifier:
13309 __complex__
13310
13311 Returns a representation of the type-specifier. For a
13312 class-specifier, enum-specifier, or elaborated-type-specifier, a
13313 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13314
13315 The parser flags FLAGS is used to control type-specifier parsing.
13316
13317 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13318 in a decl-specifier-seq.
13319
13320 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13321 class-specifier, enum-specifier, or elaborated-type-specifier, then
13322 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13323 if a type is declared; 2 if it is defined. Otherwise, it is set to
13324 zero.
13325
13326 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13327 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13328 is set to FALSE. */
13329
13330 static tree
13331 cp_parser_type_specifier (cp_parser* parser,
13332 cp_parser_flags flags,
13333 cp_decl_specifier_seq *decl_specs,
13334 bool is_declaration,
13335 int* declares_class_or_enum,
13336 bool* is_cv_qualifier)
13337 {
13338 tree type_spec = NULL_TREE;
13339 cp_token *token;
13340 enum rid keyword;
13341 cp_decl_spec ds = ds_last;
13342
13343 /* Assume this type-specifier does not declare a new type. */
13344 if (declares_class_or_enum)
13345 *declares_class_or_enum = 0;
13346 /* And that it does not specify a cv-qualifier. */
13347 if (is_cv_qualifier)
13348 *is_cv_qualifier = false;
13349 /* Peek at the next token. */
13350 token = cp_lexer_peek_token (parser->lexer);
13351
13352 /* If we're looking at a keyword, we can use that to guide the
13353 production we choose. */
13354 keyword = token->keyword;
13355 switch (keyword)
13356 {
13357 case RID_ENUM:
13358 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13359 goto elaborated_type_specifier;
13360
13361 /* Look for the enum-specifier. */
13362 type_spec = cp_parser_enum_specifier (parser);
13363 /* If that worked, we're done. */
13364 if (type_spec)
13365 {
13366 if (declares_class_or_enum)
13367 *declares_class_or_enum = 2;
13368 if (decl_specs)
13369 cp_parser_set_decl_spec_type (decl_specs,
13370 type_spec,
13371 token->location,
13372 /*type_definition_p=*/true);
13373 return type_spec;
13374 }
13375 else
13376 goto elaborated_type_specifier;
13377
13378 /* Any of these indicate either a class-specifier, or an
13379 elaborated-type-specifier. */
13380 case RID_CLASS:
13381 case RID_STRUCT:
13382 case RID_UNION:
13383 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13384 goto elaborated_type_specifier;
13385
13386 /* Parse tentatively so that we can back up if we don't find a
13387 class-specifier. */
13388 cp_parser_parse_tentatively (parser);
13389 /* Look for the class-specifier. */
13390 type_spec = cp_parser_class_specifier (parser);
13391 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13392 /* If that worked, we're done. */
13393 if (cp_parser_parse_definitely (parser))
13394 {
13395 if (declares_class_or_enum)
13396 *declares_class_or_enum = 2;
13397 if (decl_specs)
13398 cp_parser_set_decl_spec_type (decl_specs,
13399 type_spec,
13400 token->location,
13401 /*type_definition_p=*/true);
13402 return type_spec;
13403 }
13404
13405 /* Fall through. */
13406 elaborated_type_specifier:
13407 /* We're declaring (not defining) a class or enum. */
13408 if (declares_class_or_enum)
13409 *declares_class_or_enum = 1;
13410
13411 /* Fall through. */
13412 case RID_TYPENAME:
13413 /* Look for an elaborated-type-specifier. */
13414 type_spec
13415 = (cp_parser_elaborated_type_specifier
13416 (parser,
13417 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13418 is_declaration));
13419 if (decl_specs)
13420 cp_parser_set_decl_spec_type (decl_specs,
13421 type_spec,
13422 token->location,
13423 /*type_definition_p=*/false);
13424 return type_spec;
13425
13426 case RID_CONST:
13427 ds = ds_const;
13428 if (is_cv_qualifier)
13429 *is_cv_qualifier = true;
13430 break;
13431
13432 case RID_VOLATILE:
13433 ds = ds_volatile;
13434 if (is_cv_qualifier)
13435 *is_cv_qualifier = true;
13436 break;
13437
13438 case RID_RESTRICT:
13439 ds = ds_restrict;
13440 if (is_cv_qualifier)
13441 *is_cv_qualifier = true;
13442 break;
13443
13444 case RID_COMPLEX:
13445 /* The `__complex__' keyword is a GNU extension. */
13446 ds = ds_complex;
13447 break;
13448
13449 default:
13450 break;
13451 }
13452
13453 /* Handle simple keywords. */
13454 if (ds != ds_last)
13455 {
13456 if (decl_specs)
13457 {
13458 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
13459 decl_specs->any_specifiers_p = true;
13460 }
13461 return cp_lexer_consume_token (parser->lexer)->u.value;
13462 }
13463
13464 /* If we do not already have a type-specifier, assume we are looking
13465 at a simple-type-specifier. */
13466 type_spec = cp_parser_simple_type_specifier (parser,
13467 decl_specs,
13468 flags);
13469
13470 /* If we didn't find a type-specifier, and a type-specifier was not
13471 optional in this context, issue an error message. */
13472 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13473 {
13474 cp_parser_error (parser, "expected type specifier");
13475 return error_mark_node;
13476 }
13477
13478 return type_spec;
13479 }
13480
13481 /* Parse a simple-type-specifier.
13482
13483 simple-type-specifier:
13484 :: [opt] nested-name-specifier [opt] type-name
13485 :: [opt] nested-name-specifier template template-id
13486 char
13487 wchar_t
13488 bool
13489 short
13490 int
13491 long
13492 signed
13493 unsigned
13494 float
13495 double
13496 void
13497
13498 C++0x Extension:
13499
13500 simple-type-specifier:
13501 auto
13502 decltype ( expression )
13503 char16_t
13504 char32_t
13505 __underlying_type ( type-id )
13506
13507 GNU Extension:
13508
13509 simple-type-specifier:
13510 __int128
13511 __typeof__ unary-expression
13512 __typeof__ ( type-id )
13513
13514 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13515 appropriately updated. */
13516
13517 static tree
13518 cp_parser_simple_type_specifier (cp_parser* parser,
13519 cp_decl_specifier_seq *decl_specs,
13520 cp_parser_flags flags)
13521 {
13522 tree type = NULL_TREE;
13523 cp_token *token;
13524
13525 /* Peek at the next token. */
13526 token = cp_lexer_peek_token (parser->lexer);
13527
13528 /* If we're looking at a keyword, things are easy. */
13529 switch (token->keyword)
13530 {
13531 case RID_CHAR:
13532 if (decl_specs)
13533 decl_specs->explicit_char_p = true;
13534 type = char_type_node;
13535 break;
13536 case RID_CHAR16:
13537 type = char16_type_node;
13538 break;
13539 case RID_CHAR32:
13540 type = char32_type_node;
13541 break;
13542 case RID_WCHAR:
13543 type = wchar_type_node;
13544 break;
13545 case RID_BOOL:
13546 type = boolean_type_node;
13547 break;
13548 case RID_SHORT:
13549 set_and_check_decl_spec_loc (decl_specs, ds_short, token->location);
13550 type = short_integer_type_node;
13551 break;
13552 case RID_INT:
13553 if (decl_specs)
13554 decl_specs->explicit_int_p = true;
13555 type = integer_type_node;
13556 break;
13557 case RID_INT128:
13558 if (!int128_integer_type_node)
13559 break;
13560 if (decl_specs)
13561 decl_specs->explicit_int128_p = true;
13562 type = int128_integer_type_node;
13563 break;
13564 case RID_LONG:
13565 if (decl_specs)
13566 set_and_check_decl_spec_loc (decl_specs, ds_long, token->location);
13567 type = long_integer_type_node;
13568 break;
13569 case RID_SIGNED:
13570 set_and_check_decl_spec_loc (decl_specs, ds_signed, token->location);
13571 type = integer_type_node;
13572 break;
13573 case RID_UNSIGNED:
13574 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token->location);
13575 type = unsigned_type_node;
13576 break;
13577 case RID_FLOAT:
13578 type = float_type_node;
13579 break;
13580 case RID_DOUBLE:
13581 type = double_type_node;
13582 break;
13583 case RID_VOID:
13584 type = void_type_node;
13585 break;
13586
13587 case RID_AUTO:
13588 maybe_warn_cpp0x (CPP0X_AUTO);
13589 type = make_auto ();
13590 break;
13591
13592 case RID_DECLTYPE:
13593 /* Since DR 743, decltype can either be a simple-type-specifier by
13594 itself or begin a nested-name-specifier. Parsing it will replace
13595 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13596 handling below decide what to do. */
13597 cp_parser_decltype (parser);
13598 cp_lexer_set_token_position (parser->lexer, token);
13599 break;
13600
13601 case RID_TYPEOF:
13602 /* Consume the `typeof' token. */
13603 cp_lexer_consume_token (parser->lexer);
13604 /* Parse the operand to `typeof'. */
13605 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13606 /* If it is not already a TYPE, take its type. */
13607 if (!TYPE_P (type))
13608 type = finish_typeof (type);
13609
13610 if (decl_specs)
13611 cp_parser_set_decl_spec_type (decl_specs, type,
13612 token->location,
13613 /*type_definition_p=*/false);
13614
13615 return type;
13616
13617 case RID_UNDERLYING_TYPE:
13618 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13619 if (decl_specs)
13620 cp_parser_set_decl_spec_type (decl_specs, type,
13621 token->location,
13622 /*type_definition_p=*/false);
13623
13624 return type;
13625
13626 case RID_BASES:
13627 case RID_DIRECT_BASES:
13628 type = cp_parser_trait_expr (parser, token->keyword);
13629 if (decl_specs)
13630 cp_parser_set_decl_spec_type (decl_specs, type,
13631 token->location,
13632 /*type_definition_p=*/false);
13633 return type;
13634 default:
13635 break;
13636 }
13637
13638 /* If token is an already-parsed decltype not followed by ::,
13639 it's a simple-type-specifier. */
13640 if (token->type == CPP_DECLTYPE
13641 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13642 {
13643 type = token->u.value;
13644 if (decl_specs)
13645 cp_parser_set_decl_spec_type (decl_specs, type,
13646 token->location,
13647 /*type_definition_p=*/false);
13648 cp_lexer_consume_token (parser->lexer);
13649 return type;
13650 }
13651
13652 /* If the type-specifier was for a built-in type, we're done. */
13653 if (type)
13654 {
13655 /* Record the type. */
13656 if (decl_specs
13657 && (token->keyword != RID_SIGNED
13658 && token->keyword != RID_UNSIGNED
13659 && token->keyword != RID_SHORT
13660 && token->keyword != RID_LONG))
13661 cp_parser_set_decl_spec_type (decl_specs,
13662 type,
13663 token->location,
13664 /*type_definition_p=*/false);
13665 if (decl_specs)
13666 decl_specs->any_specifiers_p = true;
13667
13668 /* Consume the token. */
13669 cp_lexer_consume_token (parser->lexer);
13670
13671 /* There is no valid C++ program where a non-template type is
13672 followed by a "<". That usually indicates that the user thought
13673 that the type was a template. */
13674 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13675 token->location);
13676
13677 return TYPE_NAME (type);
13678 }
13679
13680 /* The type-specifier must be a user-defined type. */
13681 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13682 {
13683 bool qualified_p;
13684 bool global_p;
13685
13686 /* Don't gobble tokens or issue error messages if this is an
13687 optional type-specifier. */
13688 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13689 cp_parser_parse_tentatively (parser);
13690
13691 /* Look for the optional `::' operator. */
13692 global_p
13693 = (cp_parser_global_scope_opt (parser,
13694 /*current_scope_valid_p=*/false)
13695 != NULL_TREE);
13696 /* Look for the nested-name specifier. */
13697 qualified_p
13698 = (cp_parser_nested_name_specifier_opt (parser,
13699 /*typename_keyword_p=*/false,
13700 /*check_dependency_p=*/true,
13701 /*type_p=*/false,
13702 /*is_declaration=*/false)
13703 != NULL_TREE);
13704 token = cp_lexer_peek_token (parser->lexer);
13705 /* If we have seen a nested-name-specifier, and the next token
13706 is `template', then we are using the template-id production. */
13707 if (parser->scope
13708 && cp_parser_optional_template_keyword (parser))
13709 {
13710 /* Look for the template-id. */
13711 type = cp_parser_template_id (parser,
13712 /*template_keyword_p=*/true,
13713 /*check_dependency_p=*/true,
13714 none_type,
13715 /*is_declaration=*/false);
13716 /* If the template-id did not name a type, we are out of
13717 luck. */
13718 if (TREE_CODE (type) != TYPE_DECL)
13719 {
13720 cp_parser_error (parser, "expected template-id for type");
13721 type = NULL_TREE;
13722 }
13723 }
13724 /* Otherwise, look for a type-name. */
13725 else
13726 type = cp_parser_type_name (parser);
13727 /* Keep track of all name-lookups performed in class scopes. */
13728 if (type
13729 && !global_p
13730 && !qualified_p
13731 && TREE_CODE (type) == TYPE_DECL
13732 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13733 maybe_note_name_used_in_class (DECL_NAME (type), type);
13734 /* If it didn't work out, we don't have a TYPE. */
13735 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13736 && !cp_parser_parse_definitely (parser))
13737 type = NULL_TREE;
13738 if (type && decl_specs)
13739 cp_parser_set_decl_spec_type (decl_specs, type,
13740 token->location,
13741 /*type_definition_p=*/false);
13742 }
13743
13744 /* If we didn't get a type-name, issue an error message. */
13745 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13746 {
13747 cp_parser_error (parser, "expected type-name");
13748 return error_mark_node;
13749 }
13750
13751 if (type && type != error_mark_node)
13752 {
13753 /* See if TYPE is an Objective-C type, and if so, parse and
13754 accept any protocol references following it. Do this before
13755 the cp_parser_check_for_invalid_template_id() call, because
13756 Objective-C types can be followed by '<...>' which would
13757 enclose protocol names rather than template arguments, and so
13758 everything is fine. */
13759 if (c_dialect_objc () && !parser->scope
13760 && (objc_is_id (type) || objc_is_class_name (type)))
13761 {
13762 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13763 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13764
13765 /* Clobber the "unqualified" type previously entered into
13766 DECL_SPECS with the new, improved protocol-qualified version. */
13767 if (decl_specs)
13768 decl_specs->type = qual_type;
13769
13770 return qual_type;
13771 }
13772
13773 /* There is no valid C++ program where a non-template type is
13774 followed by a "<". That usually indicates that the user
13775 thought that the type was a template. */
13776 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13777 none_type,
13778 token->location);
13779 }
13780
13781 return type;
13782 }
13783
13784 /* Parse a type-name.
13785
13786 type-name:
13787 class-name
13788 enum-name
13789 typedef-name
13790 simple-template-id [in c++0x]
13791
13792 enum-name:
13793 identifier
13794
13795 typedef-name:
13796 identifier
13797
13798 Returns a TYPE_DECL for the type. */
13799
13800 static tree
13801 cp_parser_type_name (cp_parser* parser)
13802 {
13803 tree type_decl;
13804
13805 /* We can't know yet whether it is a class-name or not. */
13806 cp_parser_parse_tentatively (parser);
13807 /* Try a class-name. */
13808 type_decl = cp_parser_class_name (parser,
13809 /*typename_keyword_p=*/false,
13810 /*template_keyword_p=*/false,
13811 none_type,
13812 /*check_dependency_p=*/true,
13813 /*class_head_p=*/false,
13814 /*is_declaration=*/false);
13815 /* If it's not a class-name, keep looking. */
13816 if (!cp_parser_parse_definitely (parser))
13817 {
13818 if (cxx_dialect < cxx0x)
13819 /* It must be a typedef-name or an enum-name. */
13820 return cp_parser_nonclass_name (parser);
13821
13822 cp_parser_parse_tentatively (parser);
13823 /* It is either a simple-template-id representing an
13824 instantiation of an alias template... */
13825 type_decl = cp_parser_template_id (parser,
13826 /*template_keyword_p=*/false,
13827 /*check_dependency_p=*/false,
13828 none_type,
13829 /*is_declaration=*/false);
13830 /* Note that this must be an instantiation of an alias template
13831 because [temp.names]/6 says:
13832
13833 A template-id that names an alias template specialization
13834 is a type-name.
13835
13836 Whereas [temp.names]/7 says:
13837
13838 A simple-template-id that names a class template
13839 specialization is a class-name. */
13840 if (type_decl != NULL_TREE
13841 && TREE_CODE (type_decl) == TYPE_DECL
13842 && TYPE_DECL_ALIAS_P (type_decl))
13843 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13844 else
13845 cp_parser_simulate_error (parser);
13846
13847 if (!cp_parser_parse_definitely (parser))
13848 /* ... Or a typedef-name or an enum-name. */
13849 return cp_parser_nonclass_name (parser);
13850 }
13851
13852 return type_decl;
13853 }
13854
13855 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13856
13857 enum-name:
13858 identifier
13859
13860 typedef-name:
13861 identifier
13862
13863 Returns a TYPE_DECL for the type. */
13864
13865 static tree
13866 cp_parser_nonclass_name (cp_parser* parser)
13867 {
13868 tree type_decl;
13869 tree identifier;
13870
13871 cp_token *token = cp_lexer_peek_token (parser->lexer);
13872 identifier = cp_parser_identifier (parser);
13873 if (identifier == error_mark_node)
13874 return error_mark_node;
13875
13876 /* Look up the type-name. */
13877 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13878
13879 if (TREE_CODE (type_decl) == USING_DECL)
13880 {
13881 if (!DECL_DEPENDENT_P (type_decl))
13882 type_decl = strip_using_decl (type_decl);
13883 else if (USING_DECL_TYPENAME_P (type_decl))
13884 {
13885 /* We have found a type introduced by a using
13886 declaration at class scope that refers to a dependent
13887 type.
13888
13889 using typename :: [opt] nested-name-specifier unqualified-id ;
13890 */
13891 type_decl = make_typename_type (TREE_TYPE (type_decl),
13892 DECL_NAME (type_decl),
13893 typename_type, tf_error);
13894 if (type_decl != error_mark_node)
13895 type_decl = TYPE_NAME (type_decl);
13896 }
13897 }
13898
13899 if (TREE_CODE (type_decl) != TYPE_DECL
13900 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13901 {
13902 /* See if this is an Objective-C type. */
13903 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13904 tree type = objc_get_protocol_qualified_type (identifier, protos);
13905 if (type)
13906 type_decl = TYPE_NAME (type);
13907 }
13908
13909 /* Issue an error if we did not find a type-name. */
13910 if (TREE_CODE (type_decl) != TYPE_DECL
13911 /* In Objective-C, we have the complication that class names are
13912 normally type names and start declarations (eg, the
13913 "NSObject" in "NSObject *object;"), but can be used in an
13914 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13915 is an expression. So, a classname followed by a dot is not a
13916 valid type-name. */
13917 || (objc_is_class_name (TREE_TYPE (type_decl))
13918 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13919 {
13920 if (!cp_parser_simulate_error (parser))
13921 cp_parser_name_lookup_error (parser, identifier, type_decl,
13922 NLE_TYPE, token->location);
13923 return error_mark_node;
13924 }
13925 /* Remember that the name was used in the definition of the
13926 current class so that we can check later to see if the
13927 meaning would have been different after the class was
13928 entirely defined. */
13929 else if (type_decl != error_mark_node
13930 && !parser->scope)
13931 maybe_note_name_used_in_class (identifier, type_decl);
13932
13933 return type_decl;
13934 }
13935
13936 /* Parse an elaborated-type-specifier. Note that the grammar given
13937 here incorporates the resolution to DR68.
13938
13939 elaborated-type-specifier:
13940 class-key :: [opt] nested-name-specifier [opt] identifier
13941 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13942 enum-key :: [opt] nested-name-specifier [opt] identifier
13943 typename :: [opt] nested-name-specifier identifier
13944 typename :: [opt] nested-name-specifier template [opt]
13945 template-id
13946
13947 GNU extension:
13948
13949 elaborated-type-specifier:
13950 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13951 class-key attributes :: [opt] nested-name-specifier [opt]
13952 template [opt] template-id
13953 enum attributes :: [opt] nested-name-specifier [opt] identifier
13954
13955 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13956 declared `friend'. If IS_DECLARATION is TRUE, then this
13957 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13958 something is being declared.
13959
13960 Returns the TYPE specified. */
13961
13962 static tree
13963 cp_parser_elaborated_type_specifier (cp_parser* parser,
13964 bool is_friend,
13965 bool is_declaration)
13966 {
13967 enum tag_types tag_type;
13968 tree identifier;
13969 tree type = NULL_TREE;
13970 tree attributes = NULL_TREE;
13971 tree globalscope;
13972 cp_token *token = NULL;
13973
13974 /* See if we're looking at the `enum' keyword. */
13975 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13976 {
13977 /* Consume the `enum' token. */
13978 cp_lexer_consume_token (parser->lexer);
13979 /* Remember that it's an enumeration type. */
13980 tag_type = enum_type;
13981 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13982 enums) is used here. */
13983 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13984 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13985 {
13986 pedwarn (input_location, 0, "elaborated-type-specifier "
13987 "for a scoped enum must not use the %<%D%> keyword",
13988 cp_lexer_peek_token (parser->lexer)->u.value);
13989 /* Consume the `struct' or `class' and parse it anyway. */
13990 cp_lexer_consume_token (parser->lexer);
13991 }
13992 /* Parse the attributes. */
13993 attributes = cp_parser_attributes_opt (parser);
13994 }
13995 /* Or, it might be `typename'. */
13996 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13997 RID_TYPENAME))
13998 {
13999 /* Consume the `typename' token. */
14000 cp_lexer_consume_token (parser->lexer);
14001 /* Remember that it's a `typename' type. */
14002 tag_type = typename_type;
14003 }
14004 /* Otherwise it must be a class-key. */
14005 else
14006 {
14007 tag_type = cp_parser_class_key (parser);
14008 if (tag_type == none_type)
14009 return error_mark_node;
14010 /* Parse the attributes. */
14011 attributes = cp_parser_attributes_opt (parser);
14012 }
14013
14014 /* Look for the `::' operator. */
14015 globalscope = cp_parser_global_scope_opt (parser,
14016 /*current_scope_valid_p=*/false);
14017 /* Look for the nested-name-specifier. */
14018 if (tag_type == typename_type && !globalscope)
14019 {
14020 if (!cp_parser_nested_name_specifier (parser,
14021 /*typename_keyword_p=*/true,
14022 /*check_dependency_p=*/true,
14023 /*type_p=*/true,
14024 is_declaration))
14025 return error_mark_node;
14026 }
14027 else
14028 /* Even though `typename' is not present, the proposed resolution
14029 to Core Issue 180 says that in `class A<T>::B', `B' should be
14030 considered a type-name, even if `A<T>' is dependent. */
14031 cp_parser_nested_name_specifier_opt (parser,
14032 /*typename_keyword_p=*/true,
14033 /*check_dependency_p=*/true,
14034 /*type_p=*/true,
14035 is_declaration);
14036 /* For everything but enumeration types, consider a template-id.
14037 For an enumeration type, consider only a plain identifier. */
14038 if (tag_type != enum_type)
14039 {
14040 bool template_p = false;
14041 tree decl;
14042
14043 /* Allow the `template' keyword. */
14044 template_p = cp_parser_optional_template_keyword (parser);
14045 /* If we didn't see `template', we don't know if there's a
14046 template-id or not. */
14047 if (!template_p)
14048 cp_parser_parse_tentatively (parser);
14049 /* Parse the template-id. */
14050 token = cp_lexer_peek_token (parser->lexer);
14051 decl = cp_parser_template_id (parser, template_p,
14052 /*check_dependency_p=*/true,
14053 tag_type,
14054 is_declaration);
14055 /* If we didn't find a template-id, look for an ordinary
14056 identifier. */
14057 if (!template_p && !cp_parser_parse_definitely (parser))
14058 ;
14059 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14060 in effect, then we must assume that, upon instantiation, the
14061 template will correspond to a class. */
14062 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14063 && tag_type == typename_type)
14064 type = make_typename_type (parser->scope, decl,
14065 typename_type,
14066 /*complain=*/tf_error);
14067 /* If the `typename' keyword is in effect and DECL is not a type
14068 decl. Then type is non existant. */
14069 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14070 type = NULL_TREE;
14071 else
14072 type = check_elaborated_type_specifier (tag_type, decl,
14073 /*allow_template_p=*/true);
14074 }
14075
14076 if (!type)
14077 {
14078 token = cp_lexer_peek_token (parser->lexer);
14079 identifier = cp_parser_identifier (parser);
14080
14081 if (identifier == error_mark_node)
14082 {
14083 parser->scope = NULL_TREE;
14084 return error_mark_node;
14085 }
14086
14087 /* For a `typename', we needn't call xref_tag. */
14088 if (tag_type == typename_type
14089 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14090 return cp_parser_make_typename_type (parser, parser->scope,
14091 identifier,
14092 token->location);
14093 /* Look up a qualified name in the usual way. */
14094 if (parser->scope)
14095 {
14096 tree decl;
14097 tree ambiguous_decls;
14098
14099 decl = cp_parser_lookup_name (parser, identifier,
14100 tag_type,
14101 /*is_template=*/false,
14102 /*is_namespace=*/false,
14103 /*check_dependency=*/true,
14104 &ambiguous_decls,
14105 token->location);
14106
14107 /* If the lookup was ambiguous, an error will already have been
14108 issued. */
14109 if (ambiguous_decls)
14110 return error_mark_node;
14111
14112 /* If we are parsing friend declaration, DECL may be a
14113 TEMPLATE_DECL tree node here. However, we need to check
14114 whether this TEMPLATE_DECL results in valid code. Consider
14115 the following example:
14116
14117 namespace N {
14118 template <class T> class C {};
14119 }
14120 class X {
14121 template <class T> friend class N::C; // #1, valid code
14122 };
14123 template <class T> class Y {
14124 friend class N::C; // #2, invalid code
14125 };
14126
14127 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14128 name lookup of `N::C'. We see that friend declaration must
14129 be template for the code to be valid. Note that
14130 processing_template_decl does not work here since it is
14131 always 1 for the above two cases. */
14132
14133 decl = (cp_parser_maybe_treat_template_as_class
14134 (decl, /*tag_name_p=*/is_friend
14135 && parser->num_template_parameter_lists));
14136
14137 if (TREE_CODE (decl) != TYPE_DECL)
14138 {
14139 cp_parser_diagnose_invalid_type_name (parser,
14140 parser->scope,
14141 identifier,
14142 token->location);
14143 return error_mark_node;
14144 }
14145
14146 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14147 {
14148 bool allow_template = (parser->num_template_parameter_lists
14149 || DECL_SELF_REFERENCE_P (decl));
14150 type = check_elaborated_type_specifier (tag_type, decl,
14151 allow_template);
14152
14153 if (type == error_mark_node)
14154 return error_mark_node;
14155 }
14156
14157 /* Forward declarations of nested types, such as
14158
14159 class C1::C2;
14160 class C1::C2::C3;
14161
14162 are invalid unless all components preceding the final '::'
14163 are complete. If all enclosing types are complete, these
14164 declarations become merely pointless.
14165
14166 Invalid forward declarations of nested types are errors
14167 caught elsewhere in parsing. Those that are pointless arrive
14168 here. */
14169
14170 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14171 && !is_friend && !processing_explicit_instantiation)
14172 warning (0, "declaration %qD does not declare anything", decl);
14173
14174 type = TREE_TYPE (decl);
14175 }
14176 else
14177 {
14178 /* An elaborated-type-specifier sometimes introduces a new type and
14179 sometimes names an existing type. Normally, the rule is that it
14180 introduces a new type only if there is not an existing type of
14181 the same name already in scope. For example, given:
14182
14183 struct S {};
14184 void f() { struct S s; }
14185
14186 the `struct S' in the body of `f' is the same `struct S' as in
14187 the global scope; the existing definition is used. However, if
14188 there were no global declaration, this would introduce a new
14189 local class named `S'.
14190
14191 An exception to this rule applies to the following code:
14192
14193 namespace N { struct S; }
14194
14195 Here, the elaborated-type-specifier names a new type
14196 unconditionally; even if there is already an `S' in the
14197 containing scope this declaration names a new type.
14198 This exception only applies if the elaborated-type-specifier
14199 forms the complete declaration:
14200
14201 [class.name]
14202
14203 A declaration consisting solely of `class-key identifier ;' is
14204 either a redeclaration of the name in the current scope or a
14205 forward declaration of the identifier as a class name. It
14206 introduces the name into the current scope.
14207
14208 We are in this situation precisely when the next token is a `;'.
14209
14210 An exception to the exception is that a `friend' declaration does
14211 *not* name a new type; i.e., given:
14212
14213 struct S { friend struct T; };
14214
14215 `T' is not a new type in the scope of `S'.
14216
14217 Also, `new struct S' or `sizeof (struct S)' never results in the
14218 definition of a new type; a new type can only be declared in a
14219 declaration context. */
14220
14221 tag_scope ts;
14222 bool template_p;
14223
14224 if (is_friend)
14225 /* Friends have special name lookup rules. */
14226 ts = ts_within_enclosing_non_class;
14227 else if (is_declaration
14228 && cp_lexer_next_token_is (parser->lexer,
14229 CPP_SEMICOLON))
14230 /* This is a `class-key identifier ;' */
14231 ts = ts_current;
14232 else
14233 ts = ts_global;
14234
14235 template_p =
14236 (parser->num_template_parameter_lists
14237 && (cp_parser_next_token_starts_class_definition_p (parser)
14238 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14239 /* An unqualified name was used to reference this type, so
14240 there were no qualifying templates. */
14241 if (!cp_parser_check_template_parameters (parser,
14242 /*num_templates=*/0,
14243 token->location,
14244 /*declarator=*/NULL))
14245 return error_mark_node;
14246 type = xref_tag (tag_type, identifier, ts, template_p);
14247 }
14248 }
14249
14250 if (type == error_mark_node)
14251 return error_mark_node;
14252
14253 /* Allow attributes on forward declarations of classes. */
14254 if (attributes)
14255 {
14256 if (TREE_CODE (type) == TYPENAME_TYPE)
14257 warning (OPT_Wattributes,
14258 "attributes ignored on uninstantiated type");
14259 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14260 && ! processing_explicit_instantiation)
14261 warning (OPT_Wattributes,
14262 "attributes ignored on template instantiation");
14263 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14264 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14265 else
14266 warning (OPT_Wattributes,
14267 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14268 }
14269
14270 if (tag_type != enum_type)
14271 {
14272 /* Indicate whether this class was declared as a `class' or as a
14273 `struct'. */
14274 if (TREE_CODE (type) == RECORD_TYPE)
14275 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14276 cp_parser_check_class_key (tag_type, type);
14277 }
14278
14279 /* A "<" cannot follow an elaborated type specifier. If that
14280 happens, the user was probably trying to form a template-id. */
14281 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14282 token->location);
14283
14284 return type;
14285 }
14286
14287 /* Parse an enum-specifier.
14288
14289 enum-specifier:
14290 enum-head { enumerator-list [opt] }
14291 enum-head { enumerator-list , } [C++0x]
14292
14293 enum-head:
14294 enum-key identifier [opt] enum-base [opt]
14295 enum-key nested-name-specifier identifier enum-base [opt]
14296
14297 enum-key:
14298 enum
14299 enum class [C++0x]
14300 enum struct [C++0x]
14301
14302 enum-base: [C++0x]
14303 : type-specifier-seq
14304
14305 opaque-enum-specifier:
14306 enum-key identifier enum-base [opt] ;
14307
14308 GNU Extensions:
14309 enum-key attributes[opt] identifier [opt] enum-base [opt]
14310 { enumerator-list [opt] }attributes[opt]
14311 enum-key attributes[opt] identifier [opt] enum-base [opt]
14312 { enumerator-list, }attributes[opt] [C++0x]
14313
14314 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14315 if the token stream isn't an enum-specifier after all. */
14316
14317 static tree
14318 cp_parser_enum_specifier (cp_parser* parser)
14319 {
14320 tree identifier;
14321 tree type = NULL_TREE;
14322 tree prev_scope;
14323 tree nested_name_specifier = NULL_TREE;
14324 tree attributes;
14325 bool scoped_enum_p = false;
14326 bool has_underlying_type = false;
14327 bool nested_being_defined = false;
14328 bool new_value_list = false;
14329 bool is_new_type = false;
14330 bool is_anonymous = false;
14331 tree underlying_type = NULL_TREE;
14332 cp_token *type_start_token = NULL;
14333 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14334
14335 parser->colon_corrects_to_scope_p = false;
14336
14337 /* Parse tentatively so that we can back up if we don't find a
14338 enum-specifier. */
14339 cp_parser_parse_tentatively (parser);
14340
14341 /* Caller guarantees that the current token is 'enum', an identifier
14342 possibly follows, and the token after that is an opening brace.
14343 If we don't have an identifier, fabricate an anonymous name for
14344 the enumeration being defined. */
14345 cp_lexer_consume_token (parser->lexer);
14346
14347 /* Parse the "class" or "struct", which indicates a scoped
14348 enumeration type in C++0x. */
14349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14350 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14351 {
14352 if (cxx_dialect < cxx0x)
14353 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14354
14355 /* Consume the `struct' or `class' token. */
14356 cp_lexer_consume_token (parser->lexer);
14357
14358 scoped_enum_p = true;
14359 }
14360
14361 attributes = cp_parser_attributes_opt (parser);
14362
14363 /* Clear the qualification. */
14364 parser->scope = NULL_TREE;
14365 parser->qualifying_scope = NULL_TREE;
14366 parser->object_scope = NULL_TREE;
14367
14368 /* Figure out in what scope the declaration is being placed. */
14369 prev_scope = current_scope ();
14370
14371 type_start_token = cp_lexer_peek_token (parser->lexer);
14372
14373 push_deferring_access_checks (dk_no_check);
14374 nested_name_specifier
14375 = cp_parser_nested_name_specifier_opt (parser,
14376 /*typename_keyword_p=*/true,
14377 /*check_dependency_p=*/false,
14378 /*type_p=*/false,
14379 /*is_declaration=*/false);
14380
14381 if (nested_name_specifier)
14382 {
14383 tree name;
14384
14385 identifier = cp_parser_identifier (parser);
14386 name = cp_parser_lookup_name (parser, identifier,
14387 enum_type,
14388 /*is_template=*/false,
14389 /*is_namespace=*/false,
14390 /*check_dependency=*/true,
14391 /*ambiguous_decls=*/NULL,
14392 input_location);
14393 if (name)
14394 {
14395 type = TREE_TYPE (name);
14396 if (TREE_CODE (type) == TYPENAME_TYPE)
14397 {
14398 /* Are template enums allowed in ISO? */
14399 if (template_parm_scope_p ())
14400 pedwarn (type_start_token->location, OPT_Wpedantic,
14401 "%qD is an enumeration template", name);
14402 /* ignore a typename reference, for it will be solved by name
14403 in start_enum. */
14404 type = NULL_TREE;
14405 }
14406 }
14407 else
14408 error_at (type_start_token->location,
14409 "%qD is not an enumerator-name", identifier);
14410 }
14411 else
14412 {
14413 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14414 identifier = cp_parser_identifier (parser);
14415 else
14416 {
14417 identifier = make_anon_name ();
14418 is_anonymous = true;
14419 }
14420 }
14421 pop_deferring_access_checks ();
14422
14423 /* Check for the `:' that denotes a specified underlying type in C++0x.
14424 Note that a ':' could also indicate a bitfield width, however. */
14425 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14426 {
14427 cp_decl_specifier_seq type_specifiers;
14428
14429 /* Consume the `:'. */
14430 cp_lexer_consume_token (parser->lexer);
14431
14432 /* Parse the type-specifier-seq. */
14433 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14434 /*is_trailing_return=*/false,
14435 &type_specifiers);
14436
14437 /* At this point this is surely not elaborated type specifier. */
14438 if (!cp_parser_parse_definitely (parser))
14439 return NULL_TREE;
14440
14441 if (cxx_dialect < cxx0x)
14442 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14443
14444 has_underlying_type = true;
14445
14446 /* If that didn't work, stop. */
14447 if (type_specifiers.type != error_mark_node)
14448 {
14449 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14450 /*initialized=*/0, NULL);
14451 if (underlying_type == error_mark_node)
14452 underlying_type = NULL_TREE;
14453 }
14454 }
14455
14456 /* Look for the `{' but don't consume it yet. */
14457 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14458 {
14459 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14460 {
14461 cp_parser_error (parser, "expected %<{%>");
14462 if (has_underlying_type)
14463 {
14464 type = NULL_TREE;
14465 goto out;
14466 }
14467 }
14468 /* An opaque-enum-specifier must have a ';' here. */
14469 if ((scoped_enum_p || underlying_type)
14470 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14471 {
14472 cp_parser_error (parser, "expected %<;%> or %<{%>");
14473 if (has_underlying_type)
14474 {
14475 type = NULL_TREE;
14476 goto out;
14477 }
14478 }
14479 }
14480
14481 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14482 return NULL_TREE;
14483
14484 if (nested_name_specifier)
14485 {
14486 if (CLASS_TYPE_P (nested_name_specifier))
14487 {
14488 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14489 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14490 push_scope (nested_name_specifier);
14491 }
14492 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14493 {
14494 push_nested_namespace (nested_name_specifier);
14495 }
14496 }
14497
14498 /* Issue an error message if type-definitions are forbidden here. */
14499 if (!cp_parser_check_type_definition (parser))
14500 type = error_mark_node;
14501 else
14502 /* Create the new type. We do this before consuming the opening
14503 brace so the enum will be recorded as being on the line of its
14504 tag (or the 'enum' keyword, if there is no tag). */
14505 type = start_enum (identifier, type, underlying_type,
14506 scoped_enum_p, &is_new_type);
14507
14508 /* If the next token is not '{' it is an opaque-enum-specifier or an
14509 elaborated-type-specifier. */
14510 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14511 {
14512 timevar_push (TV_PARSE_ENUM);
14513 if (nested_name_specifier)
14514 {
14515 /* The following catches invalid code such as:
14516 enum class S<int>::E { A, B, C }; */
14517 if (!processing_specialization
14518 && CLASS_TYPE_P (nested_name_specifier)
14519 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14520 error_at (type_start_token->location, "cannot add an enumerator "
14521 "list to a template instantiation");
14522
14523 /* If that scope does not contain the scope in which the
14524 class was originally declared, the program is invalid. */
14525 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14526 {
14527 if (at_namespace_scope_p ())
14528 error_at (type_start_token->location,
14529 "declaration of %qD in namespace %qD which does not "
14530 "enclose %qD",
14531 type, prev_scope, nested_name_specifier);
14532 else
14533 error_at (type_start_token->location,
14534 "declaration of %qD in %qD which does not enclose %qD",
14535 type, prev_scope, nested_name_specifier);
14536 type = error_mark_node;
14537 }
14538 }
14539
14540 if (scoped_enum_p)
14541 begin_scope (sk_scoped_enum, type);
14542
14543 /* Consume the opening brace. */
14544 cp_lexer_consume_token (parser->lexer);
14545
14546 if (type == error_mark_node)
14547 ; /* Nothing to add */
14548 else if (OPAQUE_ENUM_P (type)
14549 || (cxx_dialect > cxx98 && processing_specialization))
14550 {
14551 new_value_list = true;
14552 SET_OPAQUE_ENUM_P (type, false);
14553 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14554 }
14555 else
14556 {
14557 error_at (type_start_token->location, "multiple definition of %q#T", type);
14558 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14559 "previous definition here");
14560 type = error_mark_node;
14561 }
14562
14563 if (type == error_mark_node)
14564 cp_parser_skip_to_end_of_block_or_statement (parser);
14565 /* If the next token is not '}', then there are some enumerators. */
14566 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14567 cp_parser_enumerator_list (parser, type);
14568
14569 /* Consume the final '}'. */
14570 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14571
14572 if (scoped_enum_p)
14573 finish_scope ();
14574 timevar_pop (TV_PARSE_ENUM);
14575 }
14576 else
14577 {
14578 /* If a ';' follows, then it is an opaque-enum-specifier
14579 and additional restrictions apply. */
14580 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14581 {
14582 if (is_anonymous)
14583 error_at (type_start_token->location,
14584 "opaque-enum-specifier without name");
14585 else if (nested_name_specifier)
14586 error_at (type_start_token->location,
14587 "opaque-enum-specifier must use a simple identifier");
14588 }
14589 }
14590
14591 /* Look for trailing attributes to apply to this enumeration, and
14592 apply them if appropriate. */
14593 if (cp_parser_allow_gnu_extensions_p (parser))
14594 {
14595 tree trailing_attr = cp_parser_attributes_opt (parser);
14596 trailing_attr = chainon (trailing_attr, attributes);
14597 cplus_decl_attributes (&type,
14598 trailing_attr,
14599 (int) ATTR_FLAG_TYPE_IN_PLACE);
14600 }
14601
14602 /* Finish up the enumeration. */
14603 if (type != error_mark_node)
14604 {
14605 if (new_value_list)
14606 finish_enum_value_list (type);
14607 if (is_new_type)
14608 finish_enum (type);
14609 }
14610
14611 if (nested_name_specifier)
14612 {
14613 if (CLASS_TYPE_P (nested_name_specifier))
14614 {
14615 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14616 pop_scope (nested_name_specifier);
14617 }
14618 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14619 {
14620 pop_nested_namespace (nested_name_specifier);
14621 }
14622 }
14623 out:
14624 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14625 return type;
14626 }
14627
14628 /* Parse an enumerator-list. The enumerators all have the indicated
14629 TYPE.
14630
14631 enumerator-list:
14632 enumerator-definition
14633 enumerator-list , enumerator-definition */
14634
14635 static void
14636 cp_parser_enumerator_list (cp_parser* parser, tree type)
14637 {
14638 while (true)
14639 {
14640 /* Parse an enumerator-definition. */
14641 cp_parser_enumerator_definition (parser, type);
14642
14643 /* If the next token is not a ',', we've reached the end of
14644 the list. */
14645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14646 break;
14647 /* Otherwise, consume the `,' and keep going. */
14648 cp_lexer_consume_token (parser->lexer);
14649 /* If the next token is a `}', there is a trailing comma. */
14650 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14651 {
14652 if (cxx_dialect < cxx0x && !in_system_header)
14653 pedwarn (input_location, OPT_Wpedantic,
14654 "comma at end of enumerator list");
14655 break;
14656 }
14657 }
14658 }
14659
14660 /* Parse an enumerator-definition. The enumerator has the indicated
14661 TYPE.
14662
14663 enumerator-definition:
14664 enumerator
14665 enumerator = constant-expression
14666
14667 enumerator:
14668 identifier */
14669
14670 static void
14671 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14672 {
14673 tree identifier;
14674 tree value;
14675 location_t loc;
14676
14677 /* Save the input location because we are interested in the location
14678 of the identifier and not the location of the explicit value. */
14679 loc = cp_lexer_peek_token (parser->lexer)->location;
14680
14681 /* Look for the identifier. */
14682 identifier = cp_parser_identifier (parser);
14683 if (identifier == error_mark_node)
14684 return;
14685
14686 /* If the next token is an '=', then there is an explicit value. */
14687 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14688 {
14689 /* Consume the `=' token. */
14690 cp_lexer_consume_token (parser->lexer);
14691 /* Parse the value. */
14692 value = cp_parser_constant_expression (parser,
14693 /*allow_non_constant_p=*/false,
14694 NULL);
14695 }
14696 else
14697 value = NULL_TREE;
14698
14699 /* If we are processing a template, make sure the initializer of the
14700 enumerator doesn't contain any bare template parameter pack. */
14701 if (check_for_bare_parameter_packs (value))
14702 value = error_mark_node;
14703
14704 /* integral_constant_value will pull out this expression, so make sure
14705 it's folded as appropriate. */
14706 value = fold_non_dependent_expr (value);
14707
14708 /* Create the enumerator. */
14709 build_enumerator (identifier, value, type, loc);
14710 }
14711
14712 /* Parse a namespace-name.
14713
14714 namespace-name:
14715 original-namespace-name
14716 namespace-alias
14717
14718 Returns the NAMESPACE_DECL for the namespace. */
14719
14720 static tree
14721 cp_parser_namespace_name (cp_parser* parser)
14722 {
14723 tree identifier;
14724 tree namespace_decl;
14725
14726 cp_token *token = cp_lexer_peek_token (parser->lexer);
14727
14728 /* Get the name of the namespace. */
14729 identifier = cp_parser_identifier (parser);
14730 if (identifier == error_mark_node)
14731 return error_mark_node;
14732
14733 /* Look up the identifier in the currently active scope. Look only
14734 for namespaces, due to:
14735
14736 [basic.lookup.udir]
14737
14738 When looking up a namespace-name in a using-directive or alias
14739 definition, only namespace names are considered.
14740
14741 And:
14742
14743 [basic.lookup.qual]
14744
14745 During the lookup of a name preceding the :: scope resolution
14746 operator, object, function, and enumerator names are ignored.
14747
14748 (Note that cp_parser_qualifying_entity only calls this
14749 function if the token after the name is the scope resolution
14750 operator.) */
14751 namespace_decl = cp_parser_lookup_name (parser, identifier,
14752 none_type,
14753 /*is_template=*/false,
14754 /*is_namespace=*/true,
14755 /*check_dependency=*/true,
14756 /*ambiguous_decls=*/NULL,
14757 token->location);
14758 /* If it's not a namespace, issue an error. */
14759 if (namespace_decl == error_mark_node
14760 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14761 {
14762 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14763 error_at (token->location, "%qD is not a namespace-name", identifier);
14764 cp_parser_error (parser, "expected namespace-name");
14765 namespace_decl = error_mark_node;
14766 }
14767
14768 return namespace_decl;
14769 }
14770
14771 /* Parse a namespace-definition.
14772
14773 namespace-definition:
14774 named-namespace-definition
14775 unnamed-namespace-definition
14776
14777 named-namespace-definition:
14778 original-namespace-definition
14779 extension-namespace-definition
14780
14781 original-namespace-definition:
14782 namespace identifier { namespace-body }
14783
14784 extension-namespace-definition:
14785 namespace original-namespace-name { namespace-body }
14786
14787 unnamed-namespace-definition:
14788 namespace { namespace-body } */
14789
14790 static void
14791 cp_parser_namespace_definition (cp_parser* parser)
14792 {
14793 tree identifier, attribs;
14794 bool has_visibility;
14795 bool is_inline;
14796
14797 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14798 {
14799 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14800 is_inline = true;
14801 cp_lexer_consume_token (parser->lexer);
14802 }
14803 else
14804 is_inline = false;
14805
14806 /* Look for the `namespace' keyword. */
14807 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14808
14809 /* Get the name of the namespace. We do not attempt to distinguish
14810 between an original-namespace-definition and an
14811 extension-namespace-definition at this point. The semantic
14812 analysis routines are responsible for that. */
14813 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14814 identifier = cp_parser_identifier (parser);
14815 else
14816 identifier = NULL_TREE;
14817
14818 /* Parse any specified attributes. */
14819 attribs = cp_parser_attributes_opt (parser);
14820
14821 /* Look for the `{' to start the namespace. */
14822 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14823 /* Start the namespace. */
14824 push_namespace (identifier);
14825
14826 /* "inline namespace" is equivalent to a stub namespace definition
14827 followed by a strong using directive. */
14828 if (is_inline)
14829 {
14830 tree name_space = current_namespace;
14831 /* Set up namespace association. */
14832 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14833 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14834 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14835 /* Import the contents of the inline namespace. */
14836 pop_namespace ();
14837 do_using_directive (name_space);
14838 push_namespace (identifier);
14839 }
14840
14841 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14842
14843 /* Parse the body of the namespace. */
14844 cp_parser_namespace_body (parser);
14845
14846 if (has_visibility)
14847 pop_visibility (1);
14848
14849 /* Finish the namespace. */
14850 pop_namespace ();
14851 /* Look for the final `}'. */
14852 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14853 }
14854
14855 /* Parse a namespace-body.
14856
14857 namespace-body:
14858 declaration-seq [opt] */
14859
14860 static void
14861 cp_parser_namespace_body (cp_parser* parser)
14862 {
14863 cp_parser_declaration_seq_opt (parser);
14864 }
14865
14866 /* Parse a namespace-alias-definition.
14867
14868 namespace-alias-definition:
14869 namespace identifier = qualified-namespace-specifier ; */
14870
14871 static void
14872 cp_parser_namespace_alias_definition (cp_parser* parser)
14873 {
14874 tree identifier;
14875 tree namespace_specifier;
14876
14877 cp_token *token = cp_lexer_peek_token (parser->lexer);
14878
14879 /* Look for the `namespace' keyword. */
14880 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14881 /* Look for the identifier. */
14882 identifier = cp_parser_identifier (parser);
14883 if (identifier == error_mark_node)
14884 return;
14885 /* Look for the `=' token. */
14886 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14887 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14888 {
14889 error_at (token->location, "%<namespace%> definition is not allowed here");
14890 /* Skip the definition. */
14891 cp_lexer_consume_token (parser->lexer);
14892 if (cp_parser_skip_to_closing_brace (parser))
14893 cp_lexer_consume_token (parser->lexer);
14894 return;
14895 }
14896 cp_parser_require (parser, CPP_EQ, RT_EQ);
14897 /* Look for the qualified-namespace-specifier. */
14898 namespace_specifier
14899 = cp_parser_qualified_namespace_specifier (parser);
14900 /* Look for the `;' token. */
14901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14902
14903 /* Register the alias in the symbol table. */
14904 do_namespace_alias (identifier, namespace_specifier);
14905 }
14906
14907 /* Parse a qualified-namespace-specifier.
14908
14909 qualified-namespace-specifier:
14910 :: [opt] nested-name-specifier [opt] namespace-name
14911
14912 Returns a NAMESPACE_DECL corresponding to the specified
14913 namespace. */
14914
14915 static tree
14916 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14917 {
14918 /* Look for the optional `::'. */
14919 cp_parser_global_scope_opt (parser,
14920 /*current_scope_valid_p=*/false);
14921
14922 /* Look for the optional nested-name-specifier. */
14923 cp_parser_nested_name_specifier_opt (parser,
14924 /*typename_keyword_p=*/false,
14925 /*check_dependency_p=*/true,
14926 /*type_p=*/false,
14927 /*is_declaration=*/true);
14928
14929 return cp_parser_namespace_name (parser);
14930 }
14931
14932 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14933 access declaration.
14934
14935 using-declaration:
14936 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14937 using :: unqualified-id ;
14938
14939 access-declaration:
14940 qualified-id ;
14941
14942 */
14943
14944 static bool
14945 cp_parser_using_declaration (cp_parser* parser,
14946 bool access_declaration_p)
14947 {
14948 cp_token *token;
14949 bool typename_p = false;
14950 bool global_scope_p;
14951 tree decl;
14952 tree identifier;
14953 tree qscope;
14954 int oldcount = errorcount;
14955 cp_token *diag_token = NULL;
14956
14957 if (access_declaration_p)
14958 {
14959 diag_token = cp_lexer_peek_token (parser->lexer);
14960 cp_parser_parse_tentatively (parser);
14961 }
14962 else
14963 {
14964 /* Look for the `using' keyword. */
14965 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14966
14967 /* Peek at the next token. */
14968 token = cp_lexer_peek_token (parser->lexer);
14969 /* See if it's `typename'. */
14970 if (token->keyword == RID_TYPENAME)
14971 {
14972 /* Remember that we've seen it. */
14973 typename_p = true;
14974 /* Consume the `typename' token. */
14975 cp_lexer_consume_token (parser->lexer);
14976 }
14977 }
14978
14979 /* Look for the optional global scope qualification. */
14980 global_scope_p
14981 = (cp_parser_global_scope_opt (parser,
14982 /*current_scope_valid_p=*/false)
14983 != NULL_TREE);
14984
14985 /* If we saw `typename', or didn't see `::', then there must be a
14986 nested-name-specifier present. */
14987 if (typename_p || !global_scope_p)
14988 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14989 /*check_dependency_p=*/true,
14990 /*type_p=*/false,
14991 /*is_declaration=*/true);
14992 /* Otherwise, we could be in either of the two productions. In that
14993 case, treat the nested-name-specifier as optional. */
14994 else
14995 qscope = cp_parser_nested_name_specifier_opt (parser,
14996 /*typename_keyword_p=*/false,
14997 /*check_dependency_p=*/true,
14998 /*type_p=*/false,
14999 /*is_declaration=*/true);
15000 if (!qscope)
15001 qscope = global_namespace;
15002
15003 if (access_declaration_p && cp_parser_error_occurred (parser))
15004 /* Something has already gone wrong; there's no need to parse
15005 further. Since an error has occurred, the return value of
15006 cp_parser_parse_definitely will be false, as required. */
15007 return cp_parser_parse_definitely (parser);
15008
15009 token = cp_lexer_peek_token (parser->lexer);
15010 /* Parse the unqualified-id. */
15011 identifier = cp_parser_unqualified_id (parser,
15012 /*template_keyword_p=*/false,
15013 /*check_dependency_p=*/true,
15014 /*declarator_p=*/true,
15015 /*optional_p=*/false);
15016
15017 if (access_declaration_p)
15018 {
15019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15020 cp_parser_simulate_error (parser);
15021 if (!cp_parser_parse_definitely (parser))
15022 return false;
15023 }
15024
15025 /* The function we call to handle a using-declaration is different
15026 depending on what scope we are in. */
15027 if (qscope == error_mark_node || identifier == error_mark_node)
15028 ;
15029 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15030 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15031 /* [namespace.udecl]
15032
15033 A using declaration shall not name a template-id. */
15034 error_at (token->location,
15035 "a template-id may not appear in a using-declaration");
15036 else
15037 {
15038 if (at_class_scope_p ())
15039 {
15040 /* Create the USING_DECL. */
15041 decl = do_class_using_decl (parser->scope, identifier);
15042
15043 if (decl && typename_p)
15044 USING_DECL_TYPENAME_P (decl) = 1;
15045
15046 if (check_for_bare_parameter_packs (decl))
15047 return false;
15048 else
15049 /* Add it to the list of members in this class. */
15050 finish_member_declaration (decl);
15051 }
15052 else
15053 {
15054 decl = cp_parser_lookup_name_simple (parser,
15055 identifier,
15056 token->location);
15057 if (decl == error_mark_node)
15058 cp_parser_name_lookup_error (parser, identifier,
15059 decl, NLE_NULL,
15060 token->location);
15061 else if (check_for_bare_parameter_packs (decl))
15062 return false;
15063 else if (!at_namespace_scope_p ())
15064 do_local_using_decl (decl, qscope, identifier);
15065 else
15066 do_toplevel_using_decl (decl, qscope, identifier);
15067 }
15068 }
15069
15070 /* Look for the final `;'. */
15071 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15072
15073 if (access_declaration_p && errorcount == oldcount)
15074 warning_at (diag_token->location, OPT_Wdeprecated,
15075 "access declarations are deprecated "
15076 "in favour of using-declarations; "
15077 "suggestion: add the %<using%> keyword");
15078
15079 return true;
15080 }
15081
15082 /* Parse an alias-declaration.
15083
15084 alias-declaration:
15085 using identifier attribute-specifier-seq [opt] = type-id */
15086
15087 static tree
15088 cp_parser_alias_declaration (cp_parser* parser)
15089 {
15090 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15091 location_t id_location, using_location, attrs_location = 0;
15092 cp_declarator *declarator;
15093 cp_decl_specifier_seq decl_specs;
15094 bool member_p;
15095 const char *saved_message = NULL;
15096
15097 /* Look for the `using' keyword. */
15098 using_location = cp_lexer_peek_token (parser->lexer)->location;
15099 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15100 id_location = cp_lexer_peek_token (parser->lexer)->location;
15101 id = cp_parser_identifier (parser);
15102 if (id == error_mark_node)
15103 return error_mark_node;
15104
15105 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
15106 attributes = cp_parser_attributes_opt (parser);
15107 if (attributes == error_mark_node)
15108 return error_mark_node;
15109
15110 cp_parser_require (parser, CPP_EQ, RT_EQ);
15111
15112 if (cp_parser_error_occurred (parser))
15113 return error_mark_node;
15114
15115 /* Now we are going to parse the type-id of the declaration. */
15116
15117 /*
15118 [dcl.type]/3 says:
15119
15120 "A type-specifier-seq shall not define a class or enumeration
15121 unless it appears in the type-id of an alias-declaration (7.1.3) that
15122 is not the declaration of a template-declaration."
15123
15124 In other words, if we currently are in an alias template, the
15125 type-id should not define a type.
15126
15127 So let's set parser->type_definition_forbidden_message in that
15128 case; cp_parser_check_type_definition (called by
15129 cp_parser_class_specifier) will then emit an error if a type is
15130 defined in the type-id. */
15131 if (parser->num_template_parameter_lists)
15132 {
15133 saved_message = parser->type_definition_forbidden_message;
15134 parser->type_definition_forbidden_message =
15135 G_("types may not be defined in alias template declarations");
15136 }
15137
15138 type = cp_parser_type_id (parser);
15139
15140 /* Restore the error message if need be. */
15141 if (parser->num_template_parameter_lists)
15142 parser->type_definition_forbidden_message = saved_message;
15143
15144 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15145
15146 if (cp_parser_error_occurred (parser))
15147 return error_mark_node;
15148
15149 /* A typedef-name can also be introduced by an alias-declaration. The
15150 identifier following the using keyword becomes a typedef-name. It has
15151 the same semantics as if it were introduced by the typedef
15152 specifier. In particular, it does not define a new type and it shall
15153 not appear in the type-id. */
15154
15155 clear_decl_specs (&decl_specs);
15156 decl_specs.type = type;
15157 if (attributes != NULL_TREE)
15158 {
15159 decl_specs.attributes = attributes;
15160 set_and_check_decl_spec_loc (&decl_specs,
15161 ds_attribute,
15162 attrs_location);
15163 }
15164 set_and_check_decl_spec_loc (&decl_specs,
15165 ds_typedef,
15166 using_location);
15167 set_and_check_decl_spec_loc (&decl_specs,
15168 ds_alias,
15169 using_location);
15170
15171 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15172 declarator->id_loc = id_location;
15173
15174 member_p = at_class_scope_p ();
15175 if (member_p)
15176 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15177 NULL_TREE, attributes);
15178 else
15179 decl = start_decl (declarator, &decl_specs, 0,
15180 attributes, NULL_TREE, &pushed_scope);
15181 if (decl == error_mark_node)
15182 return decl;
15183
15184 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15185
15186 if (pushed_scope)
15187 pop_scope (pushed_scope);
15188
15189 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15190 added into the symbol table; otherwise, return the TYPE_DECL. */
15191 if (DECL_LANG_SPECIFIC (decl)
15192 && DECL_TEMPLATE_INFO (decl)
15193 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15194 {
15195 decl = DECL_TI_TEMPLATE (decl);
15196 if (member_p)
15197 check_member_template (decl);
15198 }
15199
15200 return decl;
15201 }
15202
15203 /* Parse a using-directive.
15204
15205 using-directive:
15206 using namespace :: [opt] nested-name-specifier [opt]
15207 namespace-name ; */
15208
15209 static void
15210 cp_parser_using_directive (cp_parser* parser)
15211 {
15212 tree namespace_decl;
15213 tree attribs;
15214
15215 /* Look for the `using' keyword. */
15216 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15217 /* And the `namespace' keyword. */
15218 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15219 /* Look for the optional `::' operator. */
15220 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15221 /* And the optional nested-name-specifier. */
15222 cp_parser_nested_name_specifier_opt (parser,
15223 /*typename_keyword_p=*/false,
15224 /*check_dependency_p=*/true,
15225 /*type_p=*/false,
15226 /*is_declaration=*/true);
15227 /* Get the namespace being used. */
15228 namespace_decl = cp_parser_namespace_name (parser);
15229 /* And any specified attributes. */
15230 attribs = cp_parser_attributes_opt (parser);
15231 /* Update the symbol table. */
15232 parse_using_directive (namespace_decl, attribs);
15233 /* Look for the final `;'. */
15234 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15235 }
15236
15237 /* Parse an asm-definition.
15238
15239 asm-definition:
15240 asm ( string-literal ) ;
15241
15242 GNU Extension:
15243
15244 asm-definition:
15245 asm volatile [opt] ( string-literal ) ;
15246 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15247 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15248 : asm-operand-list [opt] ) ;
15249 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15250 : asm-operand-list [opt]
15251 : asm-clobber-list [opt] ) ;
15252 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15253 : asm-clobber-list [opt]
15254 : asm-goto-list ) ; */
15255
15256 static void
15257 cp_parser_asm_definition (cp_parser* parser)
15258 {
15259 tree string;
15260 tree outputs = NULL_TREE;
15261 tree inputs = NULL_TREE;
15262 tree clobbers = NULL_TREE;
15263 tree labels = NULL_TREE;
15264 tree asm_stmt;
15265 bool volatile_p = false;
15266 bool extended_p = false;
15267 bool invalid_inputs_p = false;
15268 bool invalid_outputs_p = false;
15269 bool goto_p = false;
15270 required_token missing = RT_NONE;
15271
15272 /* Look for the `asm' keyword. */
15273 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15274 /* See if the next token is `volatile'. */
15275 if (cp_parser_allow_gnu_extensions_p (parser)
15276 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15277 {
15278 /* Remember that we saw the `volatile' keyword. */
15279 volatile_p = true;
15280 /* Consume the token. */
15281 cp_lexer_consume_token (parser->lexer);
15282 }
15283 if (cp_parser_allow_gnu_extensions_p (parser)
15284 && parser->in_function_body
15285 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15286 {
15287 /* Remember that we saw the `goto' keyword. */
15288 goto_p = true;
15289 /* Consume the token. */
15290 cp_lexer_consume_token (parser->lexer);
15291 }
15292 /* Look for the opening `('. */
15293 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15294 return;
15295 /* Look for the string. */
15296 string = cp_parser_string_literal (parser, false, false);
15297 if (string == error_mark_node)
15298 {
15299 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15300 /*consume_paren=*/true);
15301 return;
15302 }
15303
15304 /* If we're allowing GNU extensions, check for the extended assembly
15305 syntax. Unfortunately, the `:' tokens need not be separated by
15306 a space in C, and so, for compatibility, we tolerate that here
15307 too. Doing that means that we have to treat the `::' operator as
15308 two `:' tokens. */
15309 if (cp_parser_allow_gnu_extensions_p (parser)
15310 && parser->in_function_body
15311 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15312 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15313 {
15314 bool inputs_p = false;
15315 bool clobbers_p = false;
15316 bool labels_p = false;
15317
15318 /* The extended syntax was used. */
15319 extended_p = true;
15320
15321 /* Look for outputs. */
15322 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15323 {
15324 /* Consume the `:'. */
15325 cp_lexer_consume_token (parser->lexer);
15326 /* Parse the output-operands. */
15327 if (cp_lexer_next_token_is_not (parser->lexer,
15328 CPP_COLON)
15329 && cp_lexer_next_token_is_not (parser->lexer,
15330 CPP_SCOPE)
15331 && cp_lexer_next_token_is_not (parser->lexer,
15332 CPP_CLOSE_PAREN)
15333 && !goto_p)
15334 outputs = cp_parser_asm_operand_list (parser);
15335
15336 if (outputs == error_mark_node)
15337 invalid_outputs_p = true;
15338 }
15339 /* If the next token is `::', there are no outputs, and the
15340 next token is the beginning of the inputs. */
15341 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15342 /* The inputs are coming next. */
15343 inputs_p = true;
15344
15345 /* Look for inputs. */
15346 if (inputs_p
15347 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15348 {
15349 /* Consume the `:' or `::'. */
15350 cp_lexer_consume_token (parser->lexer);
15351 /* Parse the output-operands. */
15352 if (cp_lexer_next_token_is_not (parser->lexer,
15353 CPP_COLON)
15354 && cp_lexer_next_token_is_not (parser->lexer,
15355 CPP_SCOPE)
15356 && cp_lexer_next_token_is_not (parser->lexer,
15357 CPP_CLOSE_PAREN))
15358 inputs = cp_parser_asm_operand_list (parser);
15359
15360 if (inputs == error_mark_node)
15361 invalid_inputs_p = true;
15362 }
15363 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15364 /* The clobbers are coming next. */
15365 clobbers_p = true;
15366
15367 /* Look for clobbers. */
15368 if (clobbers_p
15369 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15370 {
15371 clobbers_p = true;
15372 /* Consume the `:' or `::'. */
15373 cp_lexer_consume_token (parser->lexer);
15374 /* Parse the clobbers. */
15375 if (cp_lexer_next_token_is_not (parser->lexer,
15376 CPP_COLON)
15377 && cp_lexer_next_token_is_not (parser->lexer,
15378 CPP_CLOSE_PAREN))
15379 clobbers = cp_parser_asm_clobber_list (parser);
15380 }
15381 else if (goto_p
15382 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15383 /* The labels are coming next. */
15384 labels_p = true;
15385
15386 /* Look for labels. */
15387 if (labels_p
15388 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15389 {
15390 labels_p = true;
15391 /* Consume the `:' or `::'. */
15392 cp_lexer_consume_token (parser->lexer);
15393 /* Parse the labels. */
15394 labels = cp_parser_asm_label_list (parser);
15395 }
15396
15397 if (goto_p && !labels_p)
15398 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15399 }
15400 else if (goto_p)
15401 missing = RT_COLON_SCOPE;
15402
15403 /* Look for the closing `)'. */
15404 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15405 missing ? missing : RT_CLOSE_PAREN))
15406 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15407 /*consume_paren=*/true);
15408 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15409
15410 if (!invalid_inputs_p && !invalid_outputs_p)
15411 {
15412 /* Create the ASM_EXPR. */
15413 if (parser->in_function_body)
15414 {
15415 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15416 inputs, clobbers, labels);
15417 /* If the extended syntax was not used, mark the ASM_EXPR. */
15418 if (!extended_p)
15419 {
15420 tree temp = asm_stmt;
15421 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15422 temp = TREE_OPERAND (temp, 0);
15423
15424 ASM_INPUT_P (temp) = 1;
15425 }
15426 }
15427 else
15428 add_asm_node (string);
15429 }
15430 }
15431
15432 /* Declarators [gram.dcl.decl] */
15433
15434 /* Parse an init-declarator.
15435
15436 init-declarator:
15437 declarator initializer [opt]
15438
15439 GNU Extension:
15440
15441 init-declarator:
15442 declarator asm-specification [opt] attributes [opt] initializer [opt]
15443
15444 function-definition:
15445 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15446 function-body
15447 decl-specifier-seq [opt] declarator function-try-block
15448
15449 GNU Extension:
15450
15451 function-definition:
15452 __extension__ function-definition
15453
15454 TM Extension:
15455
15456 function-definition:
15457 decl-specifier-seq [opt] declarator function-transaction-block
15458
15459 The DECL_SPECIFIERS apply to this declarator. Returns a
15460 representation of the entity declared. If MEMBER_P is TRUE, then
15461 this declarator appears in a class scope. The new DECL created by
15462 this declarator is returned.
15463
15464 The CHECKS are access checks that should be performed once we know
15465 what entity is being declared (and, therefore, what classes have
15466 befriended it).
15467
15468 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15469 for a function-definition here as well. If the declarator is a
15470 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15471 be TRUE upon return. By that point, the function-definition will
15472 have been completely parsed.
15473
15474 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15475 is FALSE.
15476
15477 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15478 parsed declaration if it is an uninitialized single declarator not followed
15479 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15480 if present, will not be consumed. If returned, this declarator will be
15481 created with SD_INITIALIZED but will not call cp_finish_decl. */
15482
15483 static tree
15484 cp_parser_init_declarator (cp_parser* parser,
15485 cp_decl_specifier_seq *decl_specifiers,
15486 VEC (deferred_access_check,gc)* checks,
15487 bool function_definition_allowed_p,
15488 bool member_p,
15489 int declares_class_or_enum,
15490 bool* function_definition_p,
15491 tree* maybe_range_for_decl)
15492 {
15493 cp_token *token = NULL, *asm_spec_start_token = NULL,
15494 *attributes_start_token = NULL;
15495 cp_declarator *declarator;
15496 tree prefix_attributes;
15497 tree attributes;
15498 tree asm_specification;
15499 tree initializer;
15500 tree decl = NULL_TREE;
15501 tree scope;
15502 int is_initialized;
15503 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15504 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15505 "(...)". */
15506 enum cpp_ttype initialization_kind;
15507 bool is_direct_init = false;
15508 bool is_non_constant_init;
15509 int ctor_dtor_or_conv_p;
15510 bool friend_p;
15511 tree pushed_scope = NULL_TREE;
15512 bool range_for_decl_p = false;
15513
15514 /* Gather the attributes that were provided with the
15515 decl-specifiers. */
15516 prefix_attributes = decl_specifiers->attributes;
15517
15518 /* Assume that this is not the declarator for a function
15519 definition. */
15520 if (function_definition_p)
15521 *function_definition_p = false;
15522
15523 /* Defer access checks while parsing the declarator; we cannot know
15524 what names are accessible until we know what is being
15525 declared. */
15526 resume_deferring_access_checks ();
15527
15528 /* Parse the declarator. */
15529 token = cp_lexer_peek_token (parser->lexer);
15530 declarator
15531 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15532 &ctor_dtor_or_conv_p,
15533 /*parenthesized_p=*/NULL,
15534 member_p);
15535 /* Gather up the deferred checks. */
15536 stop_deferring_access_checks ();
15537
15538 /* If the DECLARATOR was erroneous, there's no need to go
15539 further. */
15540 if (declarator == cp_error_declarator)
15541 return error_mark_node;
15542
15543 /* Check that the number of template-parameter-lists is OK. */
15544 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15545 token->location))
15546 return error_mark_node;
15547
15548 if (declares_class_or_enum & 2)
15549 cp_parser_check_for_definition_in_return_type (declarator,
15550 decl_specifiers->type,
15551 decl_specifiers->locations[ds_type_spec]);
15552
15553 /* Figure out what scope the entity declared by the DECLARATOR is
15554 located in. `grokdeclarator' sometimes changes the scope, so
15555 we compute it now. */
15556 scope = get_scope_of_declarator (declarator);
15557
15558 /* Perform any lookups in the declared type which were thought to be
15559 dependent, but are not in the scope of the declarator. */
15560 decl_specifiers->type
15561 = maybe_update_decl_type (decl_specifiers->type, scope);
15562
15563 /* If we're allowing GNU extensions, look for an asm-specification
15564 and attributes. */
15565 if (cp_parser_allow_gnu_extensions_p (parser))
15566 {
15567 /* Look for an asm-specification. */
15568 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15569 asm_specification = cp_parser_asm_specification_opt (parser);
15570 /* And attributes. */
15571 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15572 attributes = cp_parser_attributes_opt (parser);
15573 }
15574 else
15575 {
15576 asm_specification = NULL_TREE;
15577 attributes = NULL_TREE;
15578 }
15579
15580 /* Peek at the next token. */
15581 token = cp_lexer_peek_token (parser->lexer);
15582 /* Check to see if the token indicates the start of a
15583 function-definition. */
15584 if (function_declarator_p (declarator)
15585 && cp_parser_token_starts_function_definition_p (token))
15586 {
15587 if (!function_definition_allowed_p)
15588 {
15589 /* If a function-definition should not appear here, issue an
15590 error message. */
15591 cp_parser_error (parser,
15592 "a function-definition is not allowed here");
15593 return error_mark_node;
15594 }
15595 else
15596 {
15597 location_t func_brace_location
15598 = cp_lexer_peek_token (parser->lexer)->location;
15599
15600 /* Neither attributes nor an asm-specification are allowed
15601 on a function-definition. */
15602 if (asm_specification)
15603 error_at (asm_spec_start_token->location,
15604 "an asm-specification is not allowed "
15605 "on a function-definition");
15606 if (attributes)
15607 error_at (attributes_start_token->location,
15608 "attributes are not allowed on a function-definition");
15609 /* This is a function-definition. */
15610 *function_definition_p = true;
15611
15612 /* Parse the function definition. */
15613 if (member_p)
15614 decl = cp_parser_save_member_function_body (parser,
15615 decl_specifiers,
15616 declarator,
15617 prefix_attributes);
15618 else
15619 decl
15620 = (cp_parser_function_definition_from_specifiers_and_declarator
15621 (parser, decl_specifiers, prefix_attributes, declarator));
15622
15623 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15624 {
15625 /* This is where the prologue starts... */
15626 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15627 = func_brace_location;
15628 }
15629
15630 return decl;
15631 }
15632 }
15633
15634 /* [dcl.dcl]
15635
15636 Only in function declarations for constructors, destructors, and
15637 type conversions can the decl-specifier-seq be omitted.
15638
15639 We explicitly postpone this check past the point where we handle
15640 function-definitions because we tolerate function-definitions
15641 that are missing their return types in some modes. */
15642 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15643 {
15644 cp_parser_error (parser,
15645 "expected constructor, destructor, or type conversion");
15646 return error_mark_node;
15647 }
15648
15649 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15650 if (token->type == CPP_EQ
15651 || token->type == CPP_OPEN_PAREN
15652 || token->type == CPP_OPEN_BRACE)
15653 {
15654 is_initialized = SD_INITIALIZED;
15655 initialization_kind = token->type;
15656 if (maybe_range_for_decl)
15657 *maybe_range_for_decl = error_mark_node;
15658
15659 if (token->type == CPP_EQ
15660 && function_declarator_p (declarator))
15661 {
15662 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15663 if (t2->keyword == RID_DEFAULT)
15664 is_initialized = SD_DEFAULTED;
15665 else if (t2->keyword == RID_DELETE)
15666 is_initialized = SD_DELETED;
15667 }
15668 }
15669 else
15670 {
15671 /* If the init-declarator isn't initialized and isn't followed by a
15672 `,' or `;', it's not a valid init-declarator. */
15673 if (token->type != CPP_COMMA
15674 && token->type != CPP_SEMICOLON)
15675 {
15676 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15677 range_for_decl_p = true;
15678 else
15679 {
15680 cp_parser_error (parser, "expected initializer");
15681 return error_mark_node;
15682 }
15683 }
15684 is_initialized = SD_UNINITIALIZED;
15685 initialization_kind = CPP_EOF;
15686 }
15687
15688 /* Because start_decl has side-effects, we should only call it if we
15689 know we're going ahead. By this point, we know that we cannot
15690 possibly be looking at any other construct. */
15691 cp_parser_commit_to_tentative_parse (parser);
15692
15693 /* If the decl specifiers were bad, issue an error now that we're
15694 sure this was intended to be a declarator. Then continue
15695 declaring the variable(s), as int, to try to cut down on further
15696 errors. */
15697 if (decl_specifiers->any_specifiers_p
15698 && decl_specifiers->type == error_mark_node)
15699 {
15700 cp_parser_error (parser, "invalid type in declaration");
15701 decl_specifiers->type = integer_type_node;
15702 }
15703
15704 /* Check to see whether or not this declaration is a friend. */
15705 friend_p = cp_parser_friend_p (decl_specifiers);
15706
15707 /* Enter the newly declared entry in the symbol table. If we're
15708 processing a declaration in a class-specifier, we wait until
15709 after processing the initializer. */
15710 if (!member_p)
15711 {
15712 if (parser->in_unbraced_linkage_specification_p)
15713 decl_specifiers->storage_class = sc_extern;
15714 decl = start_decl (declarator, decl_specifiers,
15715 range_for_decl_p? SD_INITIALIZED : is_initialized,
15716 attributes, prefix_attributes,
15717 &pushed_scope);
15718 /* Adjust location of decl if declarator->id_loc is more appropriate:
15719 set, and decl wasn't merged with another decl, in which case its
15720 location would be different from input_location, and more accurate. */
15721 if (DECL_P (decl)
15722 && declarator->id_loc != UNKNOWN_LOCATION
15723 && DECL_SOURCE_LOCATION (decl) == input_location)
15724 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15725 }
15726 else if (scope)
15727 /* Enter the SCOPE. That way unqualified names appearing in the
15728 initializer will be looked up in SCOPE. */
15729 pushed_scope = push_scope (scope);
15730
15731 /* Perform deferred access control checks, now that we know in which
15732 SCOPE the declared entity resides. */
15733 if (!member_p && decl)
15734 {
15735 tree saved_current_function_decl = NULL_TREE;
15736
15737 /* If the entity being declared is a function, pretend that we
15738 are in its scope. If it is a `friend', it may have access to
15739 things that would not otherwise be accessible. */
15740 if (TREE_CODE (decl) == FUNCTION_DECL)
15741 {
15742 saved_current_function_decl = current_function_decl;
15743 current_function_decl = decl;
15744 }
15745
15746 /* Perform access checks for template parameters. */
15747 cp_parser_perform_template_parameter_access_checks (checks);
15748
15749 /* Perform the access control checks for the declarator and the
15750 decl-specifiers. */
15751 perform_deferred_access_checks (tf_warning_or_error);
15752
15753 /* Restore the saved value. */
15754 if (TREE_CODE (decl) == FUNCTION_DECL)
15755 current_function_decl = saved_current_function_decl;
15756 }
15757
15758 /* Parse the initializer. */
15759 initializer = NULL_TREE;
15760 is_direct_init = false;
15761 is_non_constant_init = true;
15762 if (is_initialized)
15763 {
15764 if (function_declarator_p (declarator))
15765 {
15766 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15767 if (initialization_kind == CPP_EQ)
15768 initializer = cp_parser_pure_specifier (parser);
15769 else
15770 {
15771 /* If the declaration was erroneous, we don't really
15772 know what the user intended, so just silently
15773 consume the initializer. */
15774 if (decl != error_mark_node)
15775 error_at (initializer_start_token->location,
15776 "initializer provided for function");
15777 cp_parser_skip_to_closing_parenthesis (parser,
15778 /*recovering=*/true,
15779 /*or_comma=*/false,
15780 /*consume_paren=*/true);
15781 }
15782 }
15783 else
15784 {
15785 /* We want to record the extra mangling scope for in-class
15786 initializers of class members and initializers of static data
15787 member templates. The former involves deferring
15788 parsing of the initializer until end of class as with default
15789 arguments. So right here we only handle the latter. */
15790 if (!member_p && processing_template_decl)
15791 start_lambda_scope (decl);
15792 initializer = cp_parser_initializer (parser,
15793 &is_direct_init,
15794 &is_non_constant_init);
15795 if (!member_p && processing_template_decl)
15796 finish_lambda_scope ();
15797 if (initializer == error_mark_node)
15798 cp_parser_skip_to_end_of_statement (parser);
15799 }
15800 }
15801
15802 /* The old parser allows attributes to appear after a parenthesized
15803 initializer. Mark Mitchell proposed removing this functionality
15804 on the GCC mailing lists on 2002-08-13. This parser accepts the
15805 attributes -- but ignores them. */
15806 if (cp_parser_allow_gnu_extensions_p (parser)
15807 && initialization_kind == CPP_OPEN_PAREN)
15808 if (cp_parser_attributes_opt (parser))
15809 warning (OPT_Wattributes,
15810 "attributes after parenthesized initializer ignored");
15811
15812 /* For an in-class declaration, use `grokfield' to create the
15813 declaration. */
15814 if (member_p)
15815 {
15816 if (pushed_scope)
15817 {
15818 pop_scope (pushed_scope);
15819 pushed_scope = NULL_TREE;
15820 }
15821 decl = grokfield (declarator, decl_specifiers,
15822 initializer, !is_non_constant_init,
15823 /*asmspec=*/NULL_TREE,
15824 prefix_attributes);
15825 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15826 cp_parser_save_default_args (parser, decl);
15827 }
15828
15829 /* Finish processing the declaration. But, skip member
15830 declarations. */
15831 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15832 {
15833 cp_finish_decl (decl,
15834 initializer, !is_non_constant_init,
15835 asm_specification,
15836 /* If the initializer is in parentheses, then this is
15837 a direct-initialization, which means that an
15838 `explicit' constructor is OK. Otherwise, an
15839 `explicit' constructor cannot be used. */
15840 ((is_direct_init || !is_initialized)
15841 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15842 }
15843 else if ((cxx_dialect != cxx98) && friend_p
15844 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15845 /* Core issue #226 (C++0x only): A default template-argument
15846 shall not be specified in a friend class template
15847 declaration. */
15848 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
15849 /*is_partial=*/false, /*is_friend_decl=*/1);
15850
15851 if (!friend_p && pushed_scope)
15852 pop_scope (pushed_scope);
15853
15854 return decl;
15855 }
15856
15857 /* Parse a declarator.
15858
15859 declarator:
15860 direct-declarator
15861 ptr-operator declarator
15862
15863 abstract-declarator:
15864 ptr-operator abstract-declarator [opt]
15865 direct-abstract-declarator
15866
15867 GNU Extensions:
15868
15869 declarator:
15870 attributes [opt] direct-declarator
15871 attributes [opt] ptr-operator declarator
15872
15873 abstract-declarator:
15874 attributes [opt] ptr-operator abstract-declarator [opt]
15875 attributes [opt] direct-abstract-declarator
15876
15877 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15878 detect constructor, destructor or conversion operators. It is set
15879 to -1 if the declarator is a name, and +1 if it is a
15880 function. Otherwise it is set to zero. Usually you just want to
15881 test for >0, but internally the negative value is used.
15882
15883 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15884 a decl-specifier-seq unless it declares a constructor, destructor,
15885 or conversion. It might seem that we could check this condition in
15886 semantic analysis, rather than parsing, but that makes it difficult
15887 to handle something like `f()'. We want to notice that there are
15888 no decl-specifiers, and therefore realize that this is an
15889 expression, not a declaration.)
15890
15891 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15892 the declarator is a direct-declarator of the form "(...)".
15893
15894 MEMBER_P is true iff this declarator is a member-declarator. */
15895
15896 static cp_declarator *
15897 cp_parser_declarator (cp_parser* parser,
15898 cp_parser_declarator_kind dcl_kind,
15899 int* ctor_dtor_or_conv_p,
15900 bool* parenthesized_p,
15901 bool member_p)
15902 {
15903 cp_declarator *declarator;
15904 enum tree_code code;
15905 cp_cv_quals cv_quals;
15906 tree class_type;
15907 tree attributes = NULL_TREE;
15908
15909 /* Assume this is not a constructor, destructor, or type-conversion
15910 operator. */
15911 if (ctor_dtor_or_conv_p)
15912 *ctor_dtor_or_conv_p = 0;
15913
15914 if (cp_parser_allow_gnu_extensions_p (parser))
15915 attributes = cp_parser_attributes_opt (parser);
15916
15917 /* Check for the ptr-operator production. */
15918 cp_parser_parse_tentatively (parser);
15919 /* Parse the ptr-operator. */
15920 code = cp_parser_ptr_operator (parser,
15921 &class_type,
15922 &cv_quals);
15923 /* If that worked, then we have a ptr-operator. */
15924 if (cp_parser_parse_definitely (parser))
15925 {
15926 /* If a ptr-operator was found, then this declarator was not
15927 parenthesized. */
15928 if (parenthesized_p)
15929 *parenthesized_p = true;
15930 /* The dependent declarator is optional if we are parsing an
15931 abstract-declarator. */
15932 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15933 cp_parser_parse_tentatively (parser);
15934
15935 /* Parse the dependent declarator. */
15936 declarator = cp_parser_declarator (parser, dcl_kind,
15937 /*ctor_dtor_or_conv_p=*/NULL,
15938 /*parenthesized_p=*/NULL,
15939 /*member_p=*/false);
15940
15941 /* If we are parsing an abstract-declarator, we must handle the
15942 case where the dependent declarator is absent. */
15943 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15944 && !cp_parser_parse_definitely (parser))
15945 declarator = NULL;
15946
15947 declarator = cp_parser_make_indirect_declarator
15948 (code, class_type, cv_quals, declarator);
15949 }
15950 /* Everything else is a direct-declarator. */
15951 else
15952 {
15953 if (parenthesized_p)
15954 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15955 CPP_OPEN_PAREN);
15956 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15957 ctor_dtor_or_conv_p,
15958 member_p);
15959 }
15960
15961 if (attributes && declarator && declarator != cp_error_declarator)
15962 declarator->attributes = attributes;
15963
15964 return declarator;
15965 }
15966
15967 /* Parse a direct-declarator or direct-abstract-declarator.
15968
15969 direct-declarator:
15970 declarator-id
15971 direct-declarator ( parameter-declaration-clause )
15972 cv-qualifier-seq [opt]
15973 exception-specification [opt]
15974 direct-declarator [ constant-expression [opt] ]
15975 ( declarator )
15976
15977 direct-abstract-declarator:
15978 direct-abstract-declarator [opt]
15979 ( parameter-declaration-clause )
15980 cv-qualifier-seq [opt]
15981 exception-specification [opt]
15982 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15983 ( abstract-declarator )
15984
15985 Returns a representation of the declarator. DCL_KIND is
15986 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15987 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15988 we are parsing a direct-declarator. It is
15989 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15990 of ambiguity we prefer an abstract declarator, as per
15991 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15992 cp_parser_declarator. */
15993
15994 static cp_declarator *
15995 cp_parser_direct_declarator (cp_parser* parser,
15996 cp_parser_declarator_kind dcl_kind,
15997 int* ctor_dtor_or_conv_p,
15998 bool member_p)
15999 {
16000 cp_token *token;
16001 cp_declarator *declarator = NULL;
16002 tree scope = NULL_TREE;
16003 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16004 bool saved_in_declarator_p = parser->in_declarator_p;
16005 bool first = true;
16006 tree pushed_scope = NULL_TREE;
16007
16008 while (true)
16009 {
16010 /* Peek at the next token. */
16011 token = cp_lexer_peek_token (parser->lexer);
16012 if (token->type == CPP_OPEN_PAREN)
16013 {
16014 /* This is either a parameter-declaration-clause, or a
16015 parenthesized declarator. When we know we are parsing a
16016 named declarator, it must be a parenthesized declarator
16017 if FIRST is true. For instance, `(int)' is a
16018 parameter-declaration-clause, with an omitted
16019 direct-abstract-declarator. But `((*))', is a
16020 parenthesized abstract declarator. Finally, when T is a
16021 template parameter `(T)' is a
16022 parameter-declaration-clause, and not a parenthesized
16023 named declarator.
16024
16025 We first try and parse a parameter-declaration-clause,
16026 and then try a nested declarator (if FIRST is true).
16027
16028 It is not an error for it not to be a
16029 parameter-declaration-clause, even when FIRST is
16030 false. Consider,
16031
16032 int i (int);
16033 int i (3);
16034
16035 The first is the declaration of a function while the
16036 second is the definition of a variable, including its
16037 initializer.
16038
16039 Having seen only the parenthesis, we cannot know which of
16040 these two alternatives should be selected. Even more
16041 complex are examples like:
16042
16043 int i (int (a));
16044 int i (int (3));
16045
16046 The former is a function-declaration; the latter is a
16047 variable initialization.
16048
16049 Thus again, we try a parameter-declaration-clause, and if
16050 that fails, we back out and return. */
16051
16052 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16053 {
16054 tree params;
16055 unsigned saved_num_template_parameter_lists;
16056 bool is_declarator = false;
16057 tree t;
16058
16059 /* In a member-declarator, the only valid interpretation
16060 of a parenthesis is the start of a
16061 parameter-declaration-clause. (It is invalid to
16062 initialize a static data member with a parenthesized
16063 initializer; only the "=" form of initialization is
16064 permitted.) */
16065 if (!member_p)
16066 cp_parser_parse_tentatively (parser);
16067
16068 /* Consume the `('. */
16069 cp_lexer_consume_token (parser->lexer);
16070 if (first)
16071 {
16072 /* If this is going to be an abstract declarator, we're
16073 in a declarator and we can't have default args. */
16074 parser->default_arg_ok_p = false;
16075 parser->in_declarator_p = true;
16076 }
16077
16078 /* Inside the function parameter list, surrounding
16079 template-parameter-lists do not apply. */
16080 saved_num_template_parameter_lists
16081 = parser->num_template_parameter_lists;
16082 parser->num_template_parameter_lists = 0;
16083
16084 begin_scope (sk_function_parms, NULL_TREE);
16085
16086 /* Parse the parameter-declaration-clause. */
16087 params = cp_parser_parameter_declaration_clause (parser);
16088
16089 parser->num_template_parameter_lists
16090 = saved_num_template_parameter_lists;
16091
16092 /* Consume the `)'. */
16093 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16094
16095 /* If all went well, parse the cv-qualifier-seq and the
16096 exception-specification. */
16097 if (member_p || cp_parser_parse_definitely (parser))
16098 {
16099 cp_cv_quals cv_quals;
16100 cp_virt_specifiers virt_specifiers;
16101 tree exception_specification;
16102 tree late_return;
16103
16104 is_declarator = true;
16105
16106 if (ctor_dtor_or_conv_p)
16107 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16108 first = false;
16109
16110 /* Parse the cv-qualifier-seq. */
16111 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16112 /* And the exception-specification. */
16113 exception_specification
16114 = cp_parser_exception_specification_opt (parser);
16115
16116 late_return = (cp_parser_late_return_type_opt
16117 (parser, member_p ? cv_quals : -1));
16118
16119 /* Parse the virt-specifier-seq. */
16120 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16121
16122 /* Create the function-declarator. */
16123 declarator = make_call_declarator (declarator,
16124 params,
16125 cv_quals,
16126 virt_specifiers,
16127 exception_specification,
16128 late_return);
16129 /* Any subsequent parameter lists are to do with
16130 return type, so are not those of the declared
16131 function. */
16132 parser->default_arg_ok_p = false;
16133 }
16134
16135 /* Remove the function parms from scope. */
16136 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16137 pop_binding (DECL_NAME (t), t);
16138 leave_scope();
16139
16140 if (is_declarator)
16141 /* Repeat the main loop. */
16142 continue;
16143 }
16144
16145 /* If this is the first, we can try a parenthesized
16146 declarator. */
16147 if (first)
16148 {
16149 bool saved_in_type_id_in_expr_p;
16150
16151 parser->default_arg_ok_p = saved_default_arg_ok_p;
16152 parser->in_declarator_p = saved_in_declarator_p;
16153
16154 /* Consume the `('. */
16155 cp_lexer_consume_token (parser->lexer);
16156 /* Parse the nested declarator. */
16157 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16158 parser->in_type_id_in_expr_p = true;
16159 declarator
16160 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16161 /*parenthesized_p=*/NULL,
16162 member_p);
16163 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16164 first = false;
16165 /* Expect a `)'. */
16166 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16167 declarator = cp_error_declarator;
16168 if (declarator == cp_error_declarator)
16169 break;
16170
16171 goto handle_declarator;
16172 }
16173 /* Otherwise, we must be done. */
16174 else
16175 break;
16176 }
16177 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16178 && token->type == CPP_OPEN_SQUARE)
16179 {
16180 /* Parse an array-declarator. */
16181 tree bounds;
16182
16183 if (ctor_dtor_or_conv_p)
16184 *ctor_dtor_or_conv_p = 0;
16185
16186 first = false;
16187 parser->default_arg_ok_p = false;
16188 parser->in_declarator_p = true;
16189 /* Consume the `['. */
16190 cp_lexer_consume_token (parser->lexer);
16191 /* Peek at the next token. */
16192 token = cp_lexer_peek_token (parser->lexer);
16193 /* If the next token is `]', then there is no
16194 constant-expression. */
16195 if (token->type != CPP_CLOSE_SQUARE)
16196 {
16197 bool non_constant_p;
16198
16199 bounds
16200 = cp_parser_constant_expression (parser,
16201 /*allow_non_constant=*/true,
16202 &non_constant_p);
16203 if (!non_constant_p)
16204 /* OK */;
16205 else if (error_operand_p (bounds))
16206 /* Already gave an error. */;
16207 else if (!parser->in_function_body
16208 || current_binding_level->kind == sk_function_parms)
16209 {
16210 /* Normally, the array bound must be an integral constant
16211 expression. However, as an extension, we allow VLAs
16212 in function scopes as long as they aren't part of a
16213 parameter declaration. */
16214 cp_parser_error (parser,
16215 "array bound is not an integer constant");
16216 bounds = error_mark_node;
16217 }
16218 else if (processing_template_decl)
16219 {
16220 /* Remember this wasn't a constant-expression. */
16221 bounds = build_nop (TREE_TYPE (bounds), bounds);
16222 TREE_SIDE_EFFECTS (bounds) = 1;
16223 }
16224 }
16225 else
16226 bounds = NULL_TREE;
16227 /* Look for the closing `]'. */
16228 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16229 {
16230 declarator = cp_error_declarator;
16231 break;
16232 }
16233
16234 declarator = make_array_declarator (declarator, bounds);
16235 }
16236 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16237 {
16238 {
16239 tree qualifying_scope;
16240 tree unqualified_name;
16241 special_function_kind sfk;
16242 bool abstract_ok;
16243 bool pack_expansion_p = false;
16244 cp_token *declarator_id_start_token;
16245
16246 /* Parse a declarator-id */
16247 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16248 if (abstract_ok)
16249 {
16250 cp_parser_parse_tentatively (parser);
16251
16252 /* If we see an ellipsis, we should be looking at a
16253 parameter pack. */
16254 if (token->type == CPP_ELLIPSIS)
16255 {
16256 /* Consume the `...' */
16257 cp_lexer_consume_token (parser->lexer);
16258
16259 pack_expansion_p = true;
16260 }
16261 }
16262
16263 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16264 unqualified_name
16265 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16266 qualifying_scope = parser->scope;
16267 if (abstract_ok)
16268 {
16269 bool okay = false;
16270
16271 if (!unqualified_name && pack_expansion_p)
16272 {
16273 /* Check whether an error occurred. */
16274 okay = !cp_parser_error_occurred (parser);
16275
16276 /* We already consumed the ellipsis to mark a
16277 parameter pack, but we have no way to report it,
16278 so abort the tentative parse. We will be exiting
16279 immediately anyway. */
16280 cp_parser_abort_tentative_parse (parser);
16281 }
16282 else
16283 okay = cp_parser_parse_definitely (parser);
16284
16285 if (!okay)
16286 unqualified_name = error_mark_node;
16287 else if (unqualified_name
16288 && (qualifying_scope
16289 || (TREE_CODE (unqualified_name)
16290 != IDENTIFIER_NODE)))
16291 {
16292 cp_parser_error (parser, "expected unqualified-id");
16293 unqualified_name = error_mark_node;
16294 }
16295 }
16296
16297 if (!unqualified_name)
16298 return NULL;
16299 if (unqualified_name == error_mark_node)
16300 {
16301 declarator = cp_error_declarator;
16302 pack_expansion_p = false;
16303 declarator->parameter_pack_p = false;
16304 break;
16305 }
16306
16307 if (qualifying_scope && at_namespace_scope_p ()
16308 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16309 {
16310 /* In the declaration of a member of a template class
16311 outside of the class itself, the SCOPE will sometimes
16312 be a TYPENAME_TYPE. For example, given:
16313
16314 template <typename T>
16315 int S<T>::R::i = 3;
16316
16317 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16318 this context, we must resolve S<T>::R to an ordinary
16319 type, rather than a typename type.
16320
16321 The reason we normally avoid resolving TYPENAME_TYPEs
16322 is that a specialization of `S' might render
16323 `S<T>::R' not a type. However, if `S' is
16324 specialized, then this `i' will not be used, so there
16325 is no harm in resolving the types here. */
16326 tree type;
16327
16328 /* Resolve the TYPENAME_TYPE. */
16329 type = resolve_typename_type (qualifying_scope,
16330 /*only_current_p=*/false);
16331 /* If that failed, the declarator is invalid. */
16332 if (TREE_CODE (type) == TYPENAME_TYPE)
16333 {
16334 if (typedef_variant_p (type))
16335 error_at (declarator_id_start_token->location,
16336 "cannot define member of dependent typedef "
16337 "%qT", type);
16338 else
16339 error_at (declarator_id_start_token->location,
16340 "%<%T::%E%> is not a type",
16341 TYPE_CONTEXT (qualifying_scope),
16342 TYPE_IDENTIFIER (qualifying_scope));
16343 }
16344 qualifying_scope = type;
16345 }
16346
16347 sfk = sfk_none;
16348
16349 if (unqualified_name)
16350 {
16351 tree class_type;
16352
16353 if (qualifying_scope
16354 && CLASS_TYPE_P (qualifying_scope))
16355 class_type = qualifying_scope;
16356 else
16357 class_type = current_class_type;
16358
16359 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16360 {
16361 tree name_type = TREE_TYPE (unqualified_name);
16362 if (class_type && same_type_p (name_type, class_type))
16363 {
16364 if (qualifying_scope
16365 && CLASSTYPE_USE_TEMPLATE (name_type))
16366 {
16367 error_at (declarator_id_start_token->location,
16368 "invalid use of constructor as a template");
16369 inform (declarator_id_start_token->location,
16370 "use %<%T::%D%> instead of %<%T::%D%> to "
16371 "name the constructor in a qualified name",
16372 class_type,
16373 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16374 class_type, name_type);
16375 declarator = cp_error_declarator;
16376 break;
16377 }
16378 else
16379 unqualified_name = constructor_name (class_type);
16380 }
16381 else
16382 {
16383 /* We do not attempt to print the declarator
16384 here because we do not have enough
16385 information about its original syntactic
16386 form. */
16387 cp_parser_error (parser, "invalid declarator");
16388 declarator = cp_error_declarator;
16389 break;
16390 }
16391 }
16392
16393 if (class_type)
16394 {
16395 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16396 sfk = sfk_destructor;
16397 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16398 sfk = sfk_conversion;
16399 else if (/* There's no way to declare a constructor
16400 for an anonymous type, even if the type
16401 got a name for linkage purposes. */
16402 !TYPE_WAS_ANONYMOUS (class_type)
16403 && constructor_name_p (unqualified_name,
16404 class_type))
16405 {
16406 unqualified_name = constructor_name (class_type);
16407 sfk = sfk_constructor;
16408 }
16409 else if (is_overloaded_fn (unqualified_name)
16410 && DECL_CONSTRUCTOR_P (get_first_fn
16411 (unqualified_name)))
16412 sfk = sfk_constructor;
16413
16414 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16415 *ctor_dtor_or_conv_p = -1;
16416 }
16417 }
16418 declarator = make_id_declarator (qualifying_scope,
16419 unqualified_name,
16420 sfk);
16421 declarator->id_loc = token->location;
16422 declarator->parameter_pack_p = pack_expansion_p;
16423
16424 if (pack_expansion_p)
16425 maybe_warn_variadic_templates ();
16426 }
16427
16428 handle_declarator:;
16429 scope = get_scope_of_declarator (declarator);
16430 if (scope)
16431 /* Any names that appear after the declarator-id for a
16432 member are looked up in the containing scope. */
16433 pushed_scope = push_scope (scope);
16434 parser->in_declarator_p = true;
16435 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16436 || (declarator && declarator->kind == cdk_id))
16437 /* Default args are only allowed on function
16438 declarations. */
16439 parser->default_arg_ok_p = saved_default_arg_ok_p;
16440 else
16441 parser->default_arg_ok_p = false;
16442
16443 first = false;
16444 }
16445 /* We're done. */
16446 else
16447 break;
16448 }
16449
16450 /* For an abstract declarator, we might wind up with nothing at this
16451 point. That's an error; the declarator is not optional. */
16452 if (!declarator)
16453 cp_parser_error (parser, "expected declarator");
16454
16455 /* If we entered a scope, we must exit it now. */
16456 if (pushed_scope)
16457 pop_scope (pushed_scope);
16458
16459 parser->default_arg_ok_p = saved_default_arg_ok_p;
16460 parser->in_declarator_p = saved_in_declarator_p;
16461
16462 return declarator;
16463 }
16464
16465 /* Parse a ptr-operator.
16466
16467 ptr-operator:
16468 * cv-qualifier-seq [opt]
16469 &
16470 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16471
16472 GNU Extension:
16473
16474 ptr-operator:
16475 & cv-qualifier-seq [opt]
16476
16477 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16478 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16479 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16480 filled in with the TYPE containing the member. *CV_QUALS is
16481 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16482 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16483 Note that the tree codes returned by this function have nothing
16484 to do with the types of trees that will be eventually be created
16485 to represent the pointer or reference type being parsed. They are
16486 just constants with suggestive names. */
16487 static enum tree_code
16488 cp_parser_ptr_operator (cp_parser* parser,
16489 tree* type,
16490 cp_cv_quals *cv_quals)
16491 {
16492 enum tree_code code = ERROR_MARK;
16493 cp_token *token;
16494
16495 /* Assume that it's not a pointer-to-member. */
16496 *type = NULL_TREE;
16497 /* And that there are no cv-qualifiers. */
16498 *cv_quals = TYPE_UNQUALIFIED;
16499
16500 /* Peek at the next token. */
16501 token = cp_lexer_peek_token (parser->lexer);
16502
16503 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16504 if (token->type == CPP_MULT)
16505 code = INDIRECT_REF;
16506 else if (token->type == CPP_AND)
16507 code = ADDR_EXPR;
16508 else if ((cxx_dialect != cxx98) &&
16509 token->type == CPP_AND_AND) /* C++0x only */
16510 code = NON_LVALUE_EXPR;
16511
16512 if (code != ERROR_MARK)
16513 {
16514 /* Consume the `*', `&' or `&&'. */
16515 cp_lexer_consume_token (parser->lexer);
16516
16517 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16518 `&', if we are allowing GNU extensions. (The only qualifier
16519 that can legally appear after `&' is `restrict', but that is
16520 enforced during semantic analysis. */
16521 if (code == INDIRECT_REF
16522 || cp_parser_allow_gnu_extensions_p (parser))
16523 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16524 }
16525 else
16526 {
16527 /* Try the pointer-to-member case. */
16528 cp_parser_parse_tentatively (parser);
16529 /* Look for the optional `::' operator. */
16530 cp_parser_global_scope_opt (parser,
16531 /*current_scope_valid_p=*/false);
16532 /* Look for the nested-name specifier. */
16533 token = cp_lexer_peek_token (parser->lexer);
16534 cp_parser_nested_name_specifier (parser,
16535 /*typename_keyword_p=*/false,
16536 /*check_dependency_p=*/true,
16537 /*type_p=*/false,
16538 /*is_declaration=*/false);
16539 /* If we found it, and the next token is a `*', then we are
16540 indeed looking at a pointer-to-member operator. */
16541 if (!cp_parser_error_occurred (parser)
16542 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16543 {
16544 /* Indicate that the `*' operator was used. */
16545 code = INDIRECT_REF;
16546
16547 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16548 error_at (token->location, "%qD is a namespace", parser->scope);
16549 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16550 error_at (token->location, "cannot form pointer to member of "
16551 "non-class %q#T", parser->scope);
16552 else
16553 {
16554 /* The type of which the member is a member is given by the
16555 current SCOPE. */
16556 *type = parser->scope;
16557 /* The next name will not be qualified. */
16558 parser->scope = NULL_TREE;
16559 parser->qualifying_scope = NULL_TREE;
16560 parser->object_scope = NULL_TREE;
16561 /* Look for the optional cv-qualifier-seq. */
16562 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16563 }
16564 }
16565 /* If that didn't work we don't have a ptr-operator. */
16566 if (!cp_parser_parse_definitely (parser))
16567 cp_parser_error (parser, "expected ptr-operator");
16568 }
16569
16570 return code;
16571 }
16572
16573 /* Parse an (optional) cv-qualifier-seq.
16574
16575 cv-qualifier-seq:
16576 cv-qualifier cv-qualifier-seq [opt]
16577
16578 cv-qualifier:
16579 const
16580 volatile
16581
16582 GNU Extension:
16583
16584 cv-qualifier:
16585 __restrict__
16586
16587 Returns a bitmask representing the cv-qualifiers. */
16588
16589 static cp_cv_quals
16590 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16591 {
16592 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16593
16594 while (true)
16595 {
16596 cp_token *token;
16597 cp_cv_quals cv_qualifier;
16598
16599 /* Peek at the next token. */
16600 token = cp_lexer_peek_token (parser->lexer);
16601 /* See if it's a cv-qualifier. */
16602 switch (token->keyword)
16603 {
16604 case RID_CONST:
16605 cv_qualifier = TYPE_QUAL_CONST;
16606 break;
16607
16608 case RID_VOLATILE:
16609 cv_qualifier = TYPE_QUAL_VOLATILE;
16610 break;
16611
16612 case RID_RESTRICT:
16613 cv_qualifier = TYPE_QUAL_RESTRICT;
16614 break;
16615
16616 default:
16617 cv_qualifier = TYPE_UNQUALIFIED;
16618 break;
16619 }
16620
16621 if (!cv_qualifier)
16622 break;
16623
16624 if (cv_quals & cv_qualifier)
16625 {
16626 error_at (token->location, "duplicate cv-qualifier");
16627 cp_lexer_purge_token (parser->lexer);
16628 }
16629 else
16630 {
16631 cp_lexer_consume_token (parser->lexer);
16632 cv_quals |= cv_qualifier;
16633 }
16634 }
16635
16636 return cv_quals;
16637 }
16638
16639 /* Parse an (optional) virt-specifier-seq.
16640
16641 virt-specifier-seq:
16642 virt-specifier virt-specifier-seq [opt]
16643
16644 virt-specifier:
16645 override
16646 final
16647
16648 Returns a bitmask representing the virt-specifiers. */
16649
16650 static cp_virt_specifiers
16651 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16652 {
16653 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16654
16655 while (true)
16656 {
16657 cp_token *token;
16658 cp_virt_specifiers virt_specifier;
16659
16660 /* Peek at the next token. */
16661 token = cp_lexer_peek_token (parser->lexer);
16662 /* See if it's a virt-specifier-qualifier. */
16663 if (token->type != CPP_NAME)
16664 break;
16665 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16666 {
16667 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16668 virt_specifier = VIRT_SPEC_OVERRIDE;
16669 }
16670 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16671 {
16672 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16673 virt_specifier = VIRT_SPEC_FINAL;
16674 }
16675 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16676 {
16677 virt_specifier = VIRT_SPEC_FINAL;
16678 }
16679 else
16680 break;
16681
16682 if (virt_specifiers & virt_specifier)
16683 {
16684 error_at (token->location, "duplicate virt-specifier");
16685 cp_lexer_purge_token (parser->lexer);
16686 }
16687 else
16688 {
16689 cp_lexer_consume_token (parser->lexer);
16690 virt_specifiers |= virt_specifier;
16691 }
16692 }
16693 return virt_specifiers;
16694 }
16695
16696 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16697 is in scope even though it isn't real. */
16698
16699 static void
16700 inject_this_parameter (tree ctype, cp_cv_quals quals)
16701 {
16702 tree this_parm;
16703
16704 if (current_class_ptr)
16705 {
16706 /* We don't clear this between NSDMIs. Is it already what we want? */
16707 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16708 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16709 && cp_type_quals (type) == quals)
16710 return;
16711 }
16712
16713 this_parm = build_this_parm (ctype, quals);
16714 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16715 current_class_ptr = NULL_TREE;
16716 current_class_ref
16717 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16718 current_class_ptr = this_parm;
16719 }
16720
16721 /* Parse a late-specified return type, if any. This is not a separate
16722 non-terminal, but part of a function declarator, which looks like
16723
16724 -> trailing-type-specifier-seq abstract-declarator(opt)
16725
16726 Returns the type indicated by the type-id.
16727
16728 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16729 function. */
16730
16731 static tree
16732 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16733 {
16734 cp_token *token;
16735 tree type;
16736
16737 /* Peek at the next token. */
16738 token = cp_lexer_peek_token (parser->lexer);
16739 /* A late-specified return type is indicated by an initial '->'. */
16740 if (token->type != CPP_DEREF)
16741 return NULL_TREE;
16742
16743 /* Consume the ->. */
16744 cp_lexer_consume_token (parser->lexer);
16745
16746 if (quals >= 0)
16747 {
16748 /* DR 1207: 'this' is in scope in the trailing return type. */
16749 gcc_assert (current_class_ptr == NULL_TREE);
16750 inject_this_parameter (current_class_type, quals);
16751 }
16752
16753 type = cp_parser_trailing_type_id (parser);
16754
16755 if (quals >= 0)
16756 current_class_ptr = current_class_ref = NULL_TREE;
16757
16758 return type;
16759 }
16760
16761 /* Parse a declarator-id.
16762
16763 declarator-id:
16764 id-expression
16765 :: [opt] nested-name-specifier [opt] type-name
16766
16767 In the `id-expression' case, the value returned is as for
16768 cp_parser_id_expression if the id-expression was an unqualified-id.
16769 If the id-expression was a qualified-id, then a SCOPE_REF is
16770 returned. The first operand is the scope (either a NAMESPACE_DECL
16771 or TREE_TYPE), but the second is still just a representation of an
16772 unqualified-id. */
16773
16774 static tree
16775 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16776 {
16777 tree id;
16778 /* The expression must be an id-expression. Assume that qualified
16779 names are the names of types so that:
16780
16781 template <class T>
16782 int S<T>::R::i = 3;
16783
16784 will work; we must treat `S<T>::R' as the name of a type.
16785 Similarly, assume that qualified names are templates, where
16786 required, so that:
16787
16788 template <class T>
16789 int S<T>::R<T>::i = 3;
16790
16791 will work, too. */
16792 id = cp_parser_id_expression (parser,
16793 /*template_keyword_p=*/false,
16794 /*check_dependency_p=*/false,
16795 /*template_p=*/NULL,
16796 /*declarator_p=*/true,
16797 optional_p);
16798 if (id && BASELINK_P (id))
16799 id = BASELINK_FUNCTIONS (id);
16800 return id;
16801 }
16802
16803 /* Parse a type-id.
16804
16805 type-id:
16806 type-specifier-seq abstract-declarator [opt]
16807
16808 Returns the TYPE specified. */
16809
16810 static tree
16811 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16812 bool is_trailing_return)
16813 {
16814 cp_decl_specifier_seq type_specifier_seq;
16815 cp_declarator *abstract_declarator;
16816
16817 /* Parse the type-specifier-seq. */
16818 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16819 is_trailing_return,
16820 &type_specifier_seq);
16821 if (type_specifier_seq.type == error_mark_node)
16822 return error_mark_node;
16823
16824 /* There might or might not be an abstract declarator. */
16825 cp_parser_parse_tentatively (parser);
16826 /* Look for the declarator. */
16827 abstract_declarator
16828 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16829 /*parenthesized_p=*/NULL,
16830 /*member_p=*/false);
16831 /* Check to see if there really was a declarator. */
16832 if (!cp_parser_parse_definitely (parser))
16833 abstract_declarator = NULL;
16834
16835 if (type_specifier_seq.type
16836 && type_uses_auto (type_specifier_seq.type))
16837 {
16838 /* A type-id with type 'auto' is only ok if the abstract declarator
16839 is a function declarator with a late-specified return type. */
16840 if (abstract_declarator
16841 && abstract_declarator->kind == cdk_function
16842 && abstract_declarator->u.function.late_return_type)
16843 /* OK */;
16844 else
16845 {
16846 error ("invalid use of %<auto%>");
16847 return error_mark_node;
16848 }
16849 }
16850
16851 return groktypename (&type_specifier_seq, abstract_declarator,
16852 is_template_arg);
16853 }
16854
16855 static tree cp_parser_type_id (cp_parser *parser)
16856 {
16857 return cp_parser_type_id_1 (parser, false, false);
16858 }
16859
16860 static tree cp_parser_template_type_arg (cp_parser *parser)
16861 {
16862 tree r;
16863 const char *saved_message = parser->type_definition_forbidden_message;
16864 parser->type_definition_forbidden_message
16865 = G_("types may not be defined in template arguments");
16866 r = cp_parser_type_id_1 (parser, true, false);
16867 parser->type_definition_forbidden_message = saved_message;
16868 return r;
16869 }
16870
16871 static tree cp_parser_trailing_type_id (cp_parser *parser)
16872 {
16873 return cp_parser_type_id_1 (parser, false, true);
16874 }
16875
16876 /* Parse a type-specifier-seq.
16877
16878 type-specifier-seq:
16879 type-specifier type-specifier-seq [opt]
16880
16881 GNU extension:
16882
16883 type-specifier-seq:
16884 attributes type-specifier-seq [opt]
16885
16886 If IS_DECLARATION is true, we are at the start of a "condition" or
16887 exception-declaration, so we might be followed by a declarator-id.
16888
16889 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16890 i.e. we've just seen "->".
16891
16892 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16893
16894 static void
16895 cp_parser_type_specifier_seq (cp_parser* parser,
16896 bool is_declaration,
16897 bool is_trailing_return,
16898 cp_decl_specifier_seq *type_specifier_seq)
16899 {
16900 bool seen_type_specifier = false;
16901 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16902 cp_token *start_token = NULL;
16903
16904 /* Clear the TYPE_SPECIFIER_SEQ. */
16905 clear_decl_specs (type_specifier_seq);
16906
16907 /* In the context of a trailing return type, enum E { } is an
16908 elaborated-type-specifier followed by a function-body, not an
16909 enum-specifier. */
16910 if (is_trailing_return)
16911 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16912
16913 /* Parse the type-specifiers and attributes. */
16914 while (true)
16915 {
16916 tree type_specifier;
16917 bool is_cv_qualifier;
16918
16919 /* Check for attributes first. */
16920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16921 {
16922 type_specifier_seq->attributes =
16923 chainon (type_specifier_seq->attributes,
16924 cp_parser_attributes_opt (parser));
16925 continue;
16926 }
16927
16928 /* record the token of the beginning of the type specifier seq,
16929 for error reporting purposes*/
16930 if (!start_token)
16931 start_token = cp_lexer_peek_token (parser->lexer);
16932
16933 /* Look for the type-specifier. */
16934 type_specifier = cp_parser_type_specifier (parser,
16935 flags,
16936 type_specifier_seq,
16937 /*is_declaration=*/false,
16938 NULL,
16939 &is_cv_qualifier);
16940 if (!type_specifier)
16941 {
16942 /* If the first type-specifier could not be found, this is not a
16943 type-specifier-seq at all. */
16944 if (!seen_type_specifier)
16945 {
16946 cp_parser_error (parser, "expected type-specifier");
16947 type_specifier_seq->type = error_mark_node;
16948 return;
16949 }
16950 /* If subsequent type-specifiers could not be found, the
16951 type-specifier-seq is complete. */
16952 break;
16953 }
16954
16955 seen_type_specifier = true;
16956 /* The standard says that a condition can be:
16957
16958 type-specifier-seq declarator = assignment-expression
16959
16960 However, given:
16961
16962 struct S {};
16963 if (int S = ...)
16964
16965 we should treat the "S" as a declarator, not as a
16966 type-specifier. The standard doesn't say that explicitly for
16967 type-specifier-seq, but it does say that for
16968 decl-specifier-seq in an ordinary declaration. Perhaps it
16969 would be clearer just to allow a decl-specifier-seq here, and
16970 then add a semantic restriction that if any decl-specifiers
16971 that are not type-specifiers appear, the program is invalid. */
16972 if (is_declaration && !is_cv_qualifier)
16973 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16974 }
16975 }
16976
16977 /* Parse a parameter-declaration-clause.
16978
16979 parameter-declaration-clause:
16980 parameter-declaration-list [opt] ... [opt]
16981 parameter-declaration-list , ...
16982
16983 Returns a representation for the parameter declarations. A return
16984 value of NULL indicates a parameter-declaration-clause consisting
16985 only of an ellipsis. */
16986
16987 static tree
16988 cp_parser_parameter_declaration_clause (cp_parser* parser)
16989 {
16990 tree parameters;
16991 cp_token *token;
16992 bool ellipsis_p;
16993 bool is_error;
16994
16995 /* Peek at the next token. */
16996 token = cp_lexer_peek_token (parser->lexer);
16997 /* Check for trivial parameter-declaration-clauses. */
16998 if (token->type == CPP_ELLIPSIS)
16999 {
17000 /* Consume the `...' token. */
17001 cp_lexer_consume_token (parser->lexer);
17002 return NULL_TREE;
17003 }
17004 else if (token->type == CPP_CLOSE_PAREN)
17005 /* There are no parameters. */
17006 {
17007 #ifndef NO_IMPLICIT_EXTERN_C
17008 if (in_system_header && current_class_type == NULL
17009 && current_lang_name == lang_name_c)
17010 return NULL_TREE;
17011 else
17012 #endif
17013 return void_list_node;
17014 }
17015 /* Check for `(void)', too, which is a special case. */
17016 else if (token->keyword == RID_VOID
17017 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17018 == CPP_CLOSE_PAREN))
17019 {
17020 /* Consume the `void' token. */
17021 cp_lexer_consume_token (parser->lexer);
17022 /* There are no parameters. */
17023 return void_list_node;
17024 }
17025
17026 /* Parse the parameter-declaration-list. */
17027 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17028 /* If a parse error occurred while parsing the
17029 parameter-declaration-list, then the entire
17030 parameter-declaration-clause is erroneous. */
17031 if (is_error)
17032 return NULL;
17033
17034 /* Peek at the next token. */
17035 token = cp_lexer_peek_token (parser->lexer);
17036 /* If it's a `,', the clause should terminate with an ellipsis. */
17037 if (token->type == CPP_COMMA)
17038 {
17039 /* Consume the `,'. */
17040 cp_lexer_consume_token (parser->lexer);
17041 /* Expect an ellipsis. */
17042 ellipsis_p
17043 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17044 }
17045 /* It might also be `...' if the optional trailing `,' was
17046 omitted. */
17047 else if (token->type == CPP_ELLIPSIS)
17048 {
17049 /* Consume the `...' token. */
17050 cp_lexer_consume_token (parser->lexer);
17051 /* And remember that we saw it. */
17052 ellipsis_p = true;
17053 }
17054 else
17055 ellipsis_p = false;
17056
17057 /* Finish the parameter list. */
17058 if (!ellipsis_p)
17059 parameters = chainon (parameters, void_list_node);
17060
17061 return parameters;
17062 }
17063
17064 /* Parse a parameter-declaration-list.
17065
17066 parameter-declaration-list:
17067 parameter-declaration
17068 parameter-declaration-list , parameter-declaration
17069
17070 Returns a representation of the parameter-declaration-list, as for
17071 cp_parser_parameter_declaration_clause. However, the
17072 `void_list_node' is never appended to the list. Upon return,
17073 *IS_ERROR will be true iff an error occurred. */
17074
17075 static tree
17076 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17077 {
17078 tree parameters = NULL_TREE;
17079 tree *tail = &parameters;
17080 bool saved_in_unbraced_linkage_specification_p;
17081 int index = 0;
17082
17083 /* Assume all will go well. */
17084 *is_error = false;
17085 /* The special considerations that apply to a function within an
17086 unbraced linkage specifications do not apply to the parameters
17087 to the function. */
17088 saved_in_unbraced_linkage_specification_p
17089 = parser->in_unbraced_linkage_specification_p;
17090 parser->in_unbraced_linkage_specification_p = false;
17091
17092 /* Look for more parameters. */
17093 while (true)
17094 {
17095 cp_parameter_declarator *parameter;
17096 tree decl = error_mark_node;
17097 bool parenthesized_p = false;
17098 /* Parse the parameter. */
17099 parameter
17100 = cp_parser_parameter_declaration (parser,
17101 /*template_parm_p=*/false,
17102 &parenthesized_p);
17103
17104 /* We don't know yet if the enclosing context is deprecated, so wait
17105 and warn in grokparms if appropriate. */
17106 deprecated_state = DEPRECATED_SUPPRESS;
17107
17108 if (parameter)
17109 decl = grokdeclarator (parameter->declarator,
17110 &parameter->decl_specifiers,
17111 PARM,
17112 parameter->default_argument != NULL_TREE,
17113 &parameter->decl_specifiers.attributes);
17114
17115 deprecated_state = DEPRECATED_NORMAL;
17116
17117 /* If a parse error occurred parsing the parameter declaration,
17118 then the entire parameter-declaration-list is erroneous. */
17119 if (decl == error_mark_node)
17120 {
17121 *is_error = true;
17122 parameters = error_mark_node;
17123 break;
17124 }
17125
17126 if (parameter->decl_specifiers.attributes)
17127 cplus_decl_attributes (&decl,
17128 parameter->decl_specifiers.attributes,
17129 0);
17130 if (DECL_NAME (decl))
17131 decl = pushdecl (decl);
17132
17133 if (decl != error_mark_node)
17134 {
17135 retrofit_lang_decl (decl);
17136 DECL_PARM_INDEX (decl) = ++index;
17137 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17138 }
17139
17140 /* Add the new parameter to the list. */
17141 *tail = build_tree_list (parameter->default_argument, decl);
17142 tail = &TREE_CHAIN (*tail);
17143
17144 /* Peek at the next token. */
17145 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17146 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17147 /* These are for Objective-C++ */
17148 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17149 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17150 /* The parameter-declaration-list is complete. */
17151 break;
17152 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17153 {
17154 cp_token *token;
17155
17156 /* Peek at the next token. */
17157 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17158 /* If it's an ellipsis, then the list is complete. */
17159 if (token->type == CPP_ELLIPSIS)
17160 break;
17161 /* Otherwise, there must be more parameters. Consume the
17162 `,'. */
17163 cp_lexer_consume_token (parser->lexer);
17164 /* When parsing something like:
17165
17166 int i(float f, double d)
17167
17168 we can tell after seeing the declaration for "f" that we
17169 are not looking at an initialization of a variable "i",
17170 but rather at the declaration of a function "i".
17171
17172 Due to the fact that the parsing of template arguments
17173 (as specified to a template-id) requires backtracking we
17174 cannot use this technique when inside a template argument
17175 list. */
17176 if (!parser->in_template_argument_list_p
17177 && !parser->in_type_id_in_expr_p
17178 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17179 /* However, a parameter-declaration of the form
17180 "foat(f)" (which is a valid declaration of a
17181 parameter "f") can also be interpreted as an
17182 expression (the conversion of "f" to "float"). */
17183 && !parenthesized_p)
17184 cp_parser_commit_to_tentative_parse (parser);
17185 }
17186 else
17187 {
17188 cp_parser_error (parser, "expected %<,%> or %<...%>");
17189 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17190 cp_parser_skip_to_closing_parenthesis (parser,
17191 /*recovering=*/true,
17192 /*or_comma=*/false,
17193 /*consume_paren=*/false);
17194 break;
17195 }
17196 }
17197
17198 parser->in_unbraced_linkage_specification_p
17199 = saved_in_unbraced_linkage_specification_p;
17200
17201 return parameters;
17202 }
17203
17204 /* Parse a parameter declaration.
17205
17206 parameter-declaration:
17207 decl-specifier-seq ... [opt] declarator
17208 decl-specifier-seq declarator = assignment-expression
17209 decl-specifier-seq ... [opt] abstract-declarator [opt]
17210 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17211
17212 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17213 declares a template parameter. (In that case, a non-nested `>'
17214 token encountered during the parsing of the assignment-expression
17215 is not interpreted as a greater-than operator.)
17216
17217 Returns a representation of the parameter, or NULL if an error
17218 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17219 true iff the declarator is of the form "(p)". */
17220
17221 static cp_parameter_declarator *
17222 cp_parser_parameter_declaration (cp_parser *parser,
17223 bool template_parm_p,
17224 bool *parenthesized_p)
17225 {
17226 int declares_class_or_enum;
17227 cp_decl_specifier_seq decl_specifiers;
17228 cp_declarator *declarator;
17229 tree default_argument;
17230 cp_token *token = NULL, *declarator_token_start = NULL;
17231 const char *saved_message;
17232
17233 /* In a template parameter, `>' is not an operator.
17234
17235 [temp.param]
17236
17237 When parsing a default template-argument for a non-type
17238 template-parameter, the first non-nested `>' is taken as the end
17239 of the template parameter-list rather than a greater-than
17240 operator. */
17241
17242 /* Type definitions may not appear in parameter types. */
17243 saved_message = parser->type_definition_forbidden_message;
17244 parser->type_definition_forbidden_message
17245 = G_("types may not be defined in parameter types");
17246
17247 /* Parse the declaration-specifiers. */
17248 cp_parser_decl_specifier_seq (parser,
17249 CP_PARSER_FLAGS_NONE,
17250 &decl_specifiers,
17251 &declares_class_or_enum);
17252
17253 /* Complain about missing 'typename' or other invalid type names. */
17254 if (!decl_specifiers.any_type_specifiers_p)
17255 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17256
17257 /* If an error occurred, there's no reason to attempt to parse the
17258 rest of the declaration. */
17259 if (cp_parser_error_occurred (parser))
17260 {
17261 parser->type_definition_forbidden_message = saved_message;
17262 return NULL;
17263 }
17264
17265 /* Peek at the next token. */
17266 token = cp_lexer_peek_token (parser->lexer);
17267
17268 /* If the next token is a `)', `,', `=', `>', or `...', then there
17269 is no declarator. However, when variadic templates are enabled,
17270 there may be a declarator following `...'. */
17271 if (token->type == CPP_CLOSE_PAREN
17272 || token->type == CPP_COMMA
17273 || token->type == CPP_EQ
17274 || token->type == CPP_GREATER)
17275 {
17276 declarator = NULL;
17277 if (parenthesized_p)
17278 *parenthesized_p = false;
17279 }
17280 /* Otherwise, there should be a declarator. */
17281 else
17282 {
17283 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17284 parser->default_arg_ok_p = false;
17285
17286 /* After seeing a decl-specifier-seq, if the next token is not a
17287 "(", there is no possibility that the code is a valid
17288 expression. Therefore, if parsing tentatively, we commit at
17289 this point. */
17290 if (!parser->in_template_argument_list_p
17291 /* In an expression context, having seen:
17292
17293 (int((char ...
17294
17295 we cannot be sure whether we are looking at a
17296 function-type (taking a "char" as a parameter) or a cast
17297 of some object of type "char" to "int". */
17298 && !parser->in_type_id_in_expr_p
17299 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17300 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17301 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17302 cp_parser_commit_to_tentative_parse (parser);
17303 /* Parse the declarator. */
17304 declarator_token_start = token;
17305 declarator = cp_parser_declarator (parser,
17306 CP_PARSER_DECLARATOR_EITHER,
17307 /*ctor_dtor_or_conv_p=*/NULL,
17308 parenthesized_p,
17309 /*member_p=*/false);
17310 parser->default_arg_ok_p = saved_default_arg_ok_p;
17311 /* After the declarator, allow more attributes. */
17312 decl_specifiers.attributes
17313 = chainon (decl_specifiers.attributes,
17314 cp_parser_attributes_opt (parser));
17315 }
17316
17317 /* If the next token is an ellipsis, and we have not seen a
17318 declarator name, and the type of the declarator contains parameter
17319 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17320 a parameter pack expansion expression. Otherwise, leave the
17321 ellipsis for a C-style variadic function. */
17322 token = cp_lexer_peek_token (parser->lexer);
17323 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17324 {
17325 tree type = decl_specifiers.type;
17326
17327 if (type && DECL_P (type))
17328 type = TREE_TYPE (type);
17329
17330 if (type
17331 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17332 && declarator_can_be_parameter_pack (declarator)
17333 && (!declarator || !declarator->parameter_pack_p)
17334 && uses_parameter_packs (type))
17335 {
17336 /* Consume the `...'. */
17337 cp_lexer_consume_token (parser->lexer);
17338 maybe_warn_variadic_templates ();
17339
17340 /* Build a pack expansion type */
17341 if (declarator)
17342 declarator->parameter_pack_p = true;
17343 else
17344 decl_specifiers.type = make_pack_expansion (type);
17345 }
17346 }
17347
17348 /* The restriction on defining new types applies only to the type
17349 of the parameter, not to the default argument. */
17350 parser->type_definition_forbidden_message = saved_message;
17351
17352 /* If the next token is `=', then process a default argument. */
17353 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17354 {
17355 token = cp_lexer_peek_token (parser->lexer);
17356 /* If we are defining a class, then the tokens that make up the
17357 default argument must be saved and processed later. */
17358 if (!template_parm_p && at_class_scope_p ()
17359 && TYPE_BEING_DEFINED (current_class_type)
17360 && !LAMBDA_TYPE_P (current_class_type))
17361 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17362 /* Outside of a class definition, we can just parse the
17363 assignment-expression. */
17364 else
17365 default_argument
17366 = cp_parser_default_argument (parser, template_parm_p);
17367
17368 if (!parser->default_arg_ok_p)
17369 {
17370 if (flag_permissive)
17371 warning (0, "deprecated use of default argument for parameter of non-function");
17372 else
17373 {
17374 error_at (token->location,
17375 "default arguments are only "
17376 "permitted for function parameters");
17377 default_argument = NULL_TREE;
17378 }
17379 }
17380 else if ((declarator && declarator->parameter_pack_p)
17381 || (decl_specifiers.type
17382 && PACK_EXPANSION_P (decl_specifiers.type)))
17383 {
17384 /* Find the name of the parameter pack. */
17385 cp_declarator *id_declarator = declarator;
17386 while (id_declarator && id_declarator->kind != cdk_id)
17387 id_declarator = id_declarator->declarator;
17388
17389 if (id_declarator && id_declarator->kind == cdk_id)
17390 error_at (declarator_token_start->location,
17391 template_parm_p
17392 ? G_("template parameter pack %qD "
17393 "cannot have a default argument")
17394 : G_("parameter pack %qD cannot have "
17395 "a default argument"),
17396 id_declarator->u.id.unqualified_name);
17397 else
17398 error_at (declarator_token_start->location,
17399 template_parm_p
17400 ? G_("template parameter pack cannot have "
17401 "a default argument")
17402 : G_("parameter pack cannot have a "
17403 "default argument"));
17404
17405 default_argument = NULL_TREE;
17406 }
17407 }
17408 else
17409 default_argument = NULL_TREE;
17410
17411 return make_parameter_declarator (&decl_specifiers,
17412 declarator,
17413 default_argument);
17414 }
17415
17416 /* Parse a default argument and return it.
17417
17418 TEMPLATE_PARM_P is true if this is a default argument for a
17419 non-type template parameter. */
17420 static tree
17421 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17422 {
17423 tree default_argument = NULL_TREE;
17424 bool saved_greater_than_is_operator_p;
17425 bool saved_local_variables_forbidden_p;
17426 bool non_constant_p, is_direct_init;
17427
17428 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17429 set correctly. */
17430 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17431 parser->greater_than_is_operator_p = !template_parm_p;
17432 /* Local variable names (and the `this' keyword) may not
17433 appear in a default argument. */
17434 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17435 parser->local_variables_forbidden_p = true;
17436 /* Parse the assignment-expression. */
17437 if (template_parm_p)
17438 push_deferring_access_checks (dk_no_deferred);
17439 default_argument
17440 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17441 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17442 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17443 if (template_parm_p)
17444 pop_deferring_access_checks ();
17445 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17446 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17447
17448 return default_argument;
17449 }
17450
17451 /* Parse a function-body.
17452
17453 function-body:
17454 compound_statement */
17455
17456 static void
17457 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17458 {
17459 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17460 }
17461
17462 /* Parse a ctor-initializer-opt followed by a function-body. Return
17463 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17464 is true we are parsing a function-try-block. */
17465
17466 static bool
17467 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17468 bool in_function_try_block)
17469 {
17470 tree body, list;
17471 bool ctor_initializer_p;
17472 const bool check_body_p =
17473 DECL_CONSTRUCTOR_P (current_function_decl)
17474 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17475 tree last = NULL;
17476
17477 /* Begin the function body. */
17478 body = begin_function_body ();
17479 /* Parse the optional ctor-initializer. */
17480 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17481
17482 /* If we're parsing a constexpr constructor definition, we need
17483 to check that the constructor body is indeed empty. However,
17484 before we get to cp_parser_function_body lot of junk has been
17485 generated, so we can't just check that we have an empty block.
17486 Rather we take a snapshot of the outermost block, and check whether
17487 cp_parser_function_body changed its state. */
17488 if (check_body_p)
17489 {
17490 list = cur_stmt_list;
17491 if (STATEMENT_LIST_TAIL (list))
17492 last = STATEMENT_LIST_TAIL (list)->stmt;
17493 }
17494 /* Parse the function-body. */
17495 cp_parser_function_body (parser, in_function_try_block);
17496 if (check_body_p)
17497 check_constexpr_ctor_body (last, list);
17498 /* Finish the function body. */
17499 finish_function_body (body);
17500
17501 return ctor_initializer_p;
17502 }
17503
17504 /* Parse an initializer.
17505
17506 initializer:
17507 = initializer-clause
17508 ( expression-list )
17509
17510 Returns an expression representing the initializer. If no
17511 initializer is present, NULL_TREE is returned.
17512
17513 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17514 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17515 set to TRUE if there is no initializer present. If there is an
17516 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17517 is set to true; otherwise it is set to false. */
17518
17519 static tree
17520 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17521 bool* non_constant_p)
17522 {
17523 cp_token *token;
17524 tree init;
17525
17526 /* Peek at the next token. */
17527 token = cp_lexer_peek_token (parser->lexer);
17528
17529 /* Let our caller know whether or not this initializer was
17530 parenthesized. */
17531 *is_direct_init = (token->type != CPP_EQ);
17532 /* Assume that the initializer is constant. */
17533 *non_constant_p = false;
17534
17535 if (token->type == CPP_EQ)
17536 {
17537 /* Consume the `='. */
17538 cp_lexer_consume_token (parser->lexer);
17539 /* Parse the initializer-clause. */
17540 init = cp_parser_initializer_clause (parser, non_constant_p);
17541 }
17542 else if (token->type == CPP_OPEN_PAREN)
17543 {
17544 VEC(tree,gc) *vec;
17545 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17546 /*cast_p=*/false,
17547 /*allow_expansion_p=*/true,
17548 non_constant_p);
17549 if (vec == NULL)
17550 return error_mark_node;
17551 init = build_tree_list_vec (vec);
17552 release_tree_vector (vec);
17553 }
17554 else if (token->type == CPP_OPEN_BRACE)
17555 {
17556 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17557 init = cp_parser_braced_list (parser, non_constant_p);
17558 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17559 }
17560 else
17561 {
17562 /* Anything else is an error. */
17563 cp_parser_error (parser, "expected initializer");
17564 init = error_mark_node;
17565 }
17566
17567 return init;
17568 }
17569
17570 /* Parse an initializer-clause.
17571
17572 initializer-clause:
17573 assignment-expression
17574 braced-init-list
17575
17576 Returns an expression representing the initializer.
17577
17578 If the `assignment-expression' production is used the value
17579 returned is simply a representation for the expression.
17580
17581 Otherwise, calls cp_parser_braced_list. */
17582
17583 static tree
17584 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17585 {
17586 tree initializer;
17587
17588 /* Assume the expression is constant. */
17589 *non_constant_p = false;
17590
17591 /* If it is not a `{', then we are looking at an
17592 assignment-expression. */
17593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17594 {
17595 initializer
17596 = cp_parser_constant_expression (parser,
17597 /*allow_non_constant_p=*/true,
17598 non_constant_p);
17599 }
17600 else
17601 initializer = cp_parser_braced_list (parser, non_constant_p);
17602
17603 return initializer;
17604 }
17605
17606 /* Parse a brace-enclosed initializer list.
17607
17608 braced-init-list:
17609 { initializer-list , [opt] }
17610 { }
17611
17612 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17613 the elements of the initializer-list (or NULL, if the last
17614 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17615 NULL_TREE. There is no way to detect whether or not the optional
17616 trailing `,' was provided. NON_CONSTANT_P is as for
17617 cp_parser_initializer. */
17618
17619 static tree
17620 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17621 {
17622 tree initializer;
17623
17624 /* Consume the `{' token. */
17625 cp_lexer_consume_token (parser->lexer);
17626 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17627 initializer = make_node (CONSTRUCTOR);
17628 /* If it's not a `}', then there is a non-trivial initializer. */
17629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17630 {
17631 /* Parse the initializer list. */
17632 CONSTRUCTOR_ELTS (initializer)
17633 = cp_parser_initializer_list (parser, non_constant_p);
17634 /* A trailing `,' token is allowed. */
17635 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17636 cp_lexer_consume_token (parser->lexer);
17637 }
17638 /* Now, there should be a trailing `}'. */
17639 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17640 TREE_TYPE (initializer) = init_list_type_node;
17641 return initializer;
17642 }
17643
17644 /* Parse an initializer-list.
17645
17646 initializer-list:
17647 initializer-clause ... [opt]
17648 initializer-list , initializer-clause ... [opt]
17649
17650 GNU Extension:
17651
17652 initializer-list:
17653 designation initializer-clause ...[opt]
17654 initializer-list , designation initializer-clause ...[opt]
17655
17656 designation:
17657 . identifier =
17658 identifier :
17659 [ constant-expression ] =
17660
17661 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17662 for the initializer. If the INDEX of the elt is non-NULL, it is the
17663 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17664 as for cp_parser_initializer. */
17665
17666 static VEC(constructor_elt,gc) *
17667 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17668 {
17669 VEC(constructor_elt,gc) *v = NULL;
17670
17671 /* Assume all of the expressions are constant. */
17672 *non_constant_p = false;
17673
17674 /* Parse the rest of the list. */
17675 while (true)
17676 {
17677 cp_token *token;
17678 tree designator;
17679 tree initializer;
17680 bool clause_non_constant_p;
17681
17682 /* If the next token is an identifier and the following one is a
17683 colon, we are looking at the GNU designated-initializer
17684 syntax. */
17685 if (cp_parser_allow_gnu_extensions_p (parser)
17686 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17687 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17688 {
17689 /* Warn the user that they are using an extension. */
17690 pedwarn (input_location, OPT_Wpedantic,
17691 "ISO C++ does not allow designated initializers");
17692 /* Consume the identifier. */
17693 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17694 /* Consume the `:'. */
17695 cp_lexer_consume_token (parser->lexer);
17696 }
17697 /* Also handle the C99 syntax, '. id ='. */
17698 else if (cp_parser_allow_gnu_extensions_p (parser)
17699 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17700 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17701 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17702 {
17703 /* Warn the user that they are using an extension. */
17704 pedwarn (input_location, OPT_Wpedantic,
17705 "ISO C++ does not allow C99 designated initializers");
17706 /* Consume the `.'. */
17707 cp_lexer_consume_token (parser->lexer);
17708 /* Consume the identifier. */
17709 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17710 /* Consume the `='. */
17711 cp_lexer_consume_token (parser->lexer);
17712 }
17713 /* Also handle C99 array designators, '[ const ] ='. */
17714 else if (cp_parser_allow_gnu_extensions_p (parser)
17715 && !c_dialect_objc ()
17716 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17717 {
17718 /* In C++11, [ could start a lambda-introducer. */
17719 cp_parser_parse_tentatively (parser);
17720 cp_lexer_consume_token (parser->lexer);
17721 designator = cp_parser_constant_expression (parser, false, NULL);
17722 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17723 cp_parser_require (parser, CPP_EQ, RT_EQ);
17724 if (!cp_parser_parse_definitely (parser))
17725 designator = NULL_TREE;
17726 }
17727 else
17728 designator = NULL_TREE;
17729
17730 /* Parse the initializer. */
17731 initializer = cp_parser_initializer_clause (parser,
17732 &clause_non_constant_p);
17733 /* If any clause is non-constant, so is the entire initializer. */
17734 if (clause_non_constant_p)
17735 *non_constant_p = true;
17736
17737 /* If we have an ellipsis, this is an initializer pack
17738 expansion. */
17739 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17740 {
17741 /* Consume the `...'. */
17742 cp_lexer_consume_token (parser->lexer);
17743
17744 /* Turn the initializer into an initializer expansion. */
17745 initializer = make_pack_expansion (initializer);
17746 }
17747
17748 /* Add it to the vector. */
17749 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17750
17751 /* If the next token is not a comma, we have reached the end of
17752 the list. */
17753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17754 break;
17755
17756 /* Peek at the next token. */
17757 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17758 /* If the next token is a `}', then we're still done. An
17759 initializer-clause can have a trailing `,' after the
17760 initializer-list and before the closing `}'. */
17761 if (token->type == CPP_CLOSE_BRACE)
17762 break;
17763
17764 /* Consume the `,' token. */
17765 cp_lexer_consume_token (parser->lexer);
17766 }
17767
17768 return v;
17769 }
17770
17771 /* Classes [gram.class] */
17772
17773 /* Parse a class-name.
17774
17775 class-name:
17776 identifier
17777 template-id
17778
17779 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17780 to indicate that names looked up in dependent types should be
17781 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17782 keyword has been used to indicate that the name that appears next
17783 is a template. TAG_TYPE indicates the explicit tag given before
17784 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17785 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17786 is the class being defined in a class-head.
17787
17788 Returns the TYPE_DECL representing the class. */
17789
17790 static tree
17791 cp_parser_class_name (cp_parser *parser,
17792 bool typename_keyword_p,
17793 bool template_keyword_p,
17794 enum tag_types tag_type,
17795 bool check_dependency_p,
17796 bool class_head_p,
17797 bool is_declaration)
17798 {
17799 tree decl;
17800 tree scope;
17801 bool typename_p;
17802 cp_token *token;
17803 tree identifier = NULL_TREE;
17804
17805 /* All class-names start with an identifier. */
17806 token = cp_lexer_peek_token (parser->lexer);
17807 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17808 {
17809 cp_parser_error (parser, "expected class-name");
17810 return error_mark_node;
17811 }
17812
17813 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17814 to a template-id, so we save it here. */
17815 scope = parser->scope;
17816 if (scope == error_mark_node)
17817 return error_mark_node;
17818
17819 /* Any name names a type if we're following the `typename' keyword
17820 in a qualified name where the enclosing scope is type-dependent. */
17821 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17822 && dependent_type_p (scope));
17823 /* Handle the common case (an identifier, but not a template-id)
17824 efficiently. */
17825 if (token->type == CPP_NAME
17826 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17827 {
17828 cp_token *identifier_token;
17829 bool ambiguous_p;
17830
17831 /* Look for the identifier. */
17832 identifier_token = cp_lexer_peek_token (parser->lexer);
17833 ambiguous_p = identifier_token->ambiguous_p;
17834 identifier = cp_parser_identifier (parser);
17835 /* If the next token isn't an identifier, we are certainly not
17836 looking at a class-name. */
17837 if (identifier == error_mark_node)
17838 decl = error_mark_node;
17839 /* If we know this is a type-name, there's no need to look it
17840 up. */
17841 else if (typename_p)
17842 decl = identifier;
17843 else
17844 {
17845 tree ambiguous_decls;
17846 /* If we already know that this lookup is ambiguous, then
17847 we've already issued an error message; there's no reason
17848 to check again. */
17849 if (ambiguous_p)
17850 {
17851 cp_parser_simulate_error (parser);
17852 return error_mark_node;
17853 }
17854 /* If the next token is a `::', then the name must be a type
17855 name.
17856
17857 [basic.lookup.qual]
17858
17859 During the lookup for a name preceding the :: scope
17860 resolution operator, object, function, and enumerator
17861 names are ignored. */
17862 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17863 tag_type = typename_type;
17864 /* Look up the name. */
17865 decl = cp_parser_lookup_name (parser, identifier,
17866 tag_type,
17867 /*is_template=*/false,
17868 /*is_namespace=*/false,
17869 check_dependency_p,
17870 &ambiguous_decls,
17871 identifier_token->location);
17872 if (ambiguous_decls)
17873 {
17874 if (cp_parser_parsing_tentatively (parser))
17875 cp_parser_simulate_error (parser);
17876 return error_mark_node;
17877 }
17878 }
17879 }
17880 else
17881 {
17882 /* Try a template-id. */
17883 decl = cp_parser_template_id (parser, template_keyword_p,
17884 check_dependency_p,
17885 tag_type,
17886 is_declaration);
17887 if (decl == error_mark_node)
17888 return error_mark_node;
17889 }
17890
17891 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17892
17893 /* If this is a typename, create a TYPENAME_TYPE. */
17894 if (typename_p && decl != error_mark_node)
17895 {
17896 decl = make_typename_type (scope, decl, typename_type,
17897 /*complain=*/tf_error);
17898 if (decl != error_mark_node)
17899 decl = TYPE_NAME (decl);
17900 }
17901
17902 decl = strip_using_decl (decl);
17903
17904 /* Check to see that it is really the name of a class. */
17905 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17906 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17907 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17908 /* Situations like this:
17909
17910 template <typename T> struct A {
17911 typename T::template X<int>::I i;
17912 };
17913
17914 are problematic. Is `T::template X<int>' a class-name? The
17915 standard does not seem to be definitive, but there is no other
17916 valid interpretation of the following `::'. Therefore, those
17917 names are considered class-names. */
17918 {
17919 decl = make_typename_type (scope, decl, tag_type, tf_error);
17920 if (decl != error_mark_node)
17921 decl = TYPE_NAME (decl);
17922 }
17923 else if (TREE_CODE (decl) != TYPE_DECL
17924 || TREE_TYPE (decl) == error_mark_node
17925 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17926 /* In Objective-C 2.0, a classname followed by '.' starts a
17927 dot-syntax expression, and it's not a type-name. */
17928 || (c_dialect_objc ()
17929 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17930 && objc_is_class_name (decl)))
17931 decl = error_mark_node;
17932
17933 if (decl == error_mark_node)
17934 cp_parser_error (parser, "expected class-name");
17935 else if (identifier && !parser->scope)
17936 maybe_note_name_used_in_class (identifier, decl);
17937
17938 return decl;
17939 }
17940
17941 /* Parse a class-specifier.
17942
17943 class-specifier:
17944 class-head { member-specification [opt] }
17945
17946 Returns the TREE_TYPE representing the class. */
17947
17948 static tree
17949 cp_parser_class_specifier_1 (cp_parser* parser)
17950 {
17951 tree type;
17952 tree attributes = NULL_TREE;
17953 bool nested_name_specifier_p;
17954 unsigned saved_num_template_parameter_lists;
17955 bool saved_in_function_body;
17956 unsigned char in_statement;
17957 bool in_switch_statement_p;
17958 bool saved_in_unbraced_linkage_specification_p;
17959 tree old_scope = NULL_TREE;
17960 tree scope = NULL_TREE;
17961 cp_token *closing_brace;
17962
17963 push_deferring_access_checks (dk_no_deferred);
17964
17965 /* Parse the class-head. */
17966 type = cp_parser_class_head (parser,
17967 &nested_name_specifier_p);
17968 /* If the class-head was a semantic disaster, skip the entire body
17969 of the class. */
17970 if (!type)
17971 {
17972 cp_parser_skip_to_end_of_block_or_statement (parser);
17973 pop_deferring_access_checks ();
17974 return error_mark_node;
17975 }
17976
17977 /* Look for the `{'. */
17978 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17979 {
17980 pop_deferring_access_checks ();
17981 return error_mark_node;
17982 }
17983
17984 /* Issue an error message if type-definitions are forbidden here. */
17985 cp_parser_check_type_definition (parser);
17986 /* Remember that we are defining one more class. */
17987 ++parser->num_classes_being_defined;
17988 /* Inside the class, surrounding template-parameter-lists do not
17989 apply. */
17990 saved_num_template_parameter_lists
17991 = parser->num_template_parameter_lists;
17992 parser->num_template_parameter_lists = 0;
17993 /* We are not in a function body. */
17994 saved_in_function_body = parser->in_function_body;
17995 parser->in_function_body = false;
17996 /* Or in a loop. */
17997 in_statement = parser->in_statement;
17998 parser->in_statement = 0;
17999 /* Or in a switch. */
18000 in_switch_statement_p = parser->in_switch_statement_p;
18001 parser->in_switch_statement_p = false;
18002 /* We are not immediately inside an extern "lang" block. */
18003 saved_in_unbraced_linkage_specification_p
18004 = parser->in_unbraced_linkage_specification_p;
18005 parser->in_unbraced_linkage_specification_p = false;
18006
18007 /* Start the class. */
18008 if (nested_name_specifier_p)
18009 {
18010 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18011 old_scope = push_inner_scope (scope);
18012 }
18013 type = begin_class_definition (type);
18014
18015 if (type == error_mark_node)
18016 /* If the type is erroneous, skip the entire body of the class. */
18017 cp_parser_skip_to_closing_brace (parser);
18018 else
18019 /* Parse the member-specification. */
18020 cp_parser_member_specification_opt (parser);
18021
18022 /* Look for the trailing `}'. */
18023 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18024 /* Look for trailing attributes to apply to this class. */
18025 if (cp_parser_allow_gnu_extensions_p (parser))
18026 attributes = cp_parser_attributes_opt (parser);
18027 if (type != error_mark_node)
18028 type = finish_struct (type, attributes);
18029 if (nested_name_specifier_p)
18030 pop_inner_scope (old_scope, scope);
18031
18032 /* We've finished a type definition. Check for the common syntax
18033 error of forgetting a semicolon after the definition. We need to
18034 be careful, as we can't just check for not-a-semicolon and be done
18035 with it; the user might have typed:
18036
18037 class X { } c = ...;
18038 class X { } *p = ...;
18039
18040 and so forth. Instead, enumerate all the possible tokens that
18041 might follow this production; if we don't see one of them, then
18042 complain and silently insert the semicolon. */
18043 {
18044 cp_token *token = cp_lexer_peek_token (parser->lexer);
18045 bool want_semicolon = true;
18046
18047 switch (token->type)
18048 {
18049 case CPP_NAME:
18050 case CPP_SEMICOLON:
18051 case CPP_MULT:
18052 case CPP_AND:
18053 case CPP_OPEN_PAREN:
18054 case CPP_CLOSE_PAREN:
18055 case CPP_COMMA:
18056 want_semicolon = false;
18057 break;
18058
18059 /* While it's legal for type qualifiers and storage class
18060 specifiers to follow type definitions in the grammar, only
18061 compiler testsuites contain code like that. Assume that if
18062 we see such code, then what we're really seeing is a case
18063 like:
18064
18065 class X { }
18066 const <type> var = ...;
18067
18068 or
18069
18070 class Y { }
18071 static <type> func (...) ...
18072
18073 i.e. the qualifier or specifier applies to the next
18074 declaration. To do so, however, we need to look ahead one
18075 more token to see if *that* token is a type specifier.
18076
18077 This code could be improved to handle:
18078
18079 class Z { }
18080 static const <type> var = ...; */
18081 case CPP_KEYWORD:
18082 if (keyword_is_decl_specifier (token->keyword))
18083 {
18084 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18085
18086 /* Handling user-defined types here would be nice, but very
18087 tricky. */
18088 want_semicolon
18089 = (lookahead->type == CPP_KEYWORD
18090 && keyword_begins_type_specifier (lookahead->keyword));
18091 }
18092 break;
18093 default:
18094 break;
18095 }
18096
18097 /* If we don't have a type, then something is very wrong and we
18098 shouldn't try to do anything clever. Likewise for not seeing the
18099 closing brace. */
18100 if (closing_brace && TYPE_P (type) && want_semicolon)
18101 {
18102 cp_token_position prev
18103 = cp_lexer_previous_token_position (parser->lexer);
18104 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18105 location_t loc = prev_token->location;
18106
18107 if (CLASSTYPE_DECLARED_CLASS (type))
18108 error_at (loc, "expected %<;%> after class definition");
18109 else if (TREE_CODE (type) == RECORD_TYPE)
18110 error_at (loc, "expected %<;%> after struct definition");
18111 else if (TREE_CODE (type) == UNION_TYPE)
18112 error_at (loc, "expected %<;%> after union definition");
18113 else
18114 gcc_unreachable ();
18115
18116 /* Unget one token and smash it to look as though we encountered
18117 a semicolon in the input stream. */
18118 cp_lexer_set_token_position (parser->lexer, prev);
18119 token = cp_lexer_peek_token (parser->lexer);
18120 token->type = CPP_SEMICOLON;
18121 token->keyword = RID_MAX;
18122 }
18123 }
18124
18125 /* If this class is not itself within the scope of another class,
18126 then we need to parse the bodies of all of the queued function
18127 definitions. Note that the queued functions defined in a class
18128 are not always processed immediately following the
18129 class-specifier for that class. Consider:
18130
18131 struct A {
18132 struct B { void f() { sizeof (A); } };
18133 };
18134
18135 If `f' were processed before the processing of `A' were
18136 completed, there would be no way to compute the size of `A'.
18137 Note that the nesting we are interested in here is lexical --
18138 not the semantic nesting given by TYPE_CONTEXT. In particular,
18139 for:
18140
18141 struct A { struct B; };
18142 struct A::B { void f() { } };
18143
18144 there is no need to delay the parsing of `A::B::f'. */
18145 if (--parser->num_classes_being_defined == 0)
18146 {
18147 tree decl;
18148 tree class_type = NULL_TREE;
18149 tree pushed_scope = NULL_TREE;
18150 unsigned ix;
18151 cp_default_arg_entry *e;
18152 tree save_ccp, save_ccr;
18153
18154 /* In a first pass, parse default arguments to the functions.
18155 Then, in a second pass, parse the bodies of the functions.
18156 This two-phased approach handles cases like:
18157
18158 struct S {
18159 void f() { g(); }
18160 void g(int i = 3);
18161 };
18162
18163 */
18164 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18165 ix, e)
18166 {
18167 decl = e->decl;
18168 /* If there are default arguments that have not yet been processed,
18169 take care of them now. */
18170 if (class_type != e->class_type)
18171 {
18172 if (pushed_scope)
18173 pop_scope (pushed_scope);
18174 class_type = e->class_type;
18175 pushed_scope = push_scope (class_type);
18176 }
18177 /* Make sure that any template parameters are in scope. */
18178 maybe_begin_member_template_processing (decl);
18179 /* Parse the default argument expressions. */
18180 cp_parser_late_parsing_default_args (parser, decl);
18181 /* Remove any template parameters from the symbol table. */
18182 maybe_end_member_template_processing ();
18183 }
18184 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18185 /* Now parse any NSDMIs. */
18186 save_ccp = current_class_ptr;
18187 save_ccr = current_class_ref;
18188 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18189 {
18190 if (class_type != DECL_CONTEXT (decl))
18191 {
18192 if (pushed_scope)
18193 pop_scope (pushed_scope);
18194 class_type = DECL_CONTEXT (decl);
18195 pushed_scope = push_scope (class_type);
18196 }
18197 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18198 cp_parser_late_parsing_nsdmi (parser, decl);
18199 }
18200 VEC_truncate (tree, unparsed_nsdmis, 0);
18201 current_class_ptr = save_ccp;
18202 current_class_ref = save_ccr;
18203 if (pushed_scope)
18204 pop_scope (pushed_scope);
18205 /* Now parse the body of the functions. */
18206 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18207 cp_parser_late_parsing_for_member (parser, decl);
18208 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18209 }
18210
18211 /* Put back any saved access checks. */
18212 pop_deferring_access_checks ();
18213
18214 /* Restore saved state. */
18215 parser->in_switch_statement_p = in_switch_statement_p;
18216 parser->in_statement = in_statement;
18217 parser->in_function_body = saved_in_function_body;
18218 parser->num_template_parameter_lists
18219 = saved_num_template_parameter_lists;
18220 parser->in_unbraced_linkage_specification_p
18221 = saved_in_unbraced_linkage_specification_p;
18222
18223 return type;
18224 }
18225
18226 static tree
18227 cp_parser_class_specifier (cp_parser* parser)
18228 {
18229 tree ret;
18230 timevar_push (TV_PARSE_STRUCT);
18231 ret = cp_parser_class_specifier_1 (parser);
18232 timevar_pop (TV_PARSE_STRUCT);
18233 return ret;
18234 }
18235
18236 /* Parse a class-head.
18237
18238 class-head:
18239 class-key identifier [opt] base-clause [opt]
18240 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18241 class-key nested-name-specifier [opt] template-id
18242 base-clause [opt]
18243
18244 class-virt-specifier:
18245 final
18246
18247 GNU Extensions:
18248 class-key attributes identifier [opt] base-clause [opt]
18249 class-key attributes nested-name-specifier identifier base-clause [opt]
18250 class-key attributes nested-name-specifier [opt] template-id
18251 base-clause [opt]
18252
18253 Upon return BASES is initialized to the list of base classes (or
18254 NULL, if there are none) in the same form returned by
18255 cp_parser_base_clause.
18256
18257 Returns the TYPE of the indicated class. Sets
18258 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18259 involving a nested-name-specifier was used, and FALSE otherwise.
18260
18261 Returns error_mark_node if this is not a class-head.
18262
18263 Returns NULL_TREE if the class-head is syntactically valid, but
18264 semantically invalid in a way that means we should skip the entire
18265 body of the class. */
18266
18267 static tree
18268 cp_parser_class_head (cp_parser* parser,
18269 bool* nested_name_specifier_p)
18270 {
18271 tree nested_name_specifier;
18272 enum tag_types class_key;
18273 tree id = NULL_TREE;
18274 tree type = NULL_TREE;
18275 tree attributes;
18276 tree bases;
18277 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18278 bool template_id_p = false;
18279 bool qualified_p = false;
18280 bool invalid_nested_name_p = false;
18281 bool invalid_explicit_specialization_p = false;
18282 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18283 tree pushed_scope = NULL_TREE;
18284 unsigned num_templates;
18285 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18286 /* Assume no nested-name-specifier will be present. */
18287 *nested_name_specifier_p = false;
18288 /* Assume no template parameter lists will be used in defining the
18289 type. */
18290 num_templates = 0;
18291 parser->colon_corrects_to_scope_p = false;
18292
18293 /* Look for the class-key. */
18294 class_key = cp_parser_class_key (parser);
18295 if (class_key == none_type)
18296 return error_mark_node;
18297
18298 /* Parse the attributes. */
18299 attributes = cp_parser_attributes_opt (parser);
18300
18301 /* If the next token is `::', that is invalid -- but sometimes
18302 people do try to write:
18303
18304 struct ::S {};
18305
18306 Handle this gracefully by accepting the extra qualifier, and then
18307 issuing an error about it later if this really is a
18308 class-head. If it turns out just to be an elaborated type
18309 specifier, remain silent. */
18310 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18311 qualified_p = true;
18312
18313 push_deferring_access_checks (dk_no_check);
18314
18315 /* Determine the name of the class. Begin by looking for an
18316 optional nested-name-specifier. */
18317 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18318 nested_name_specifier
18319 = cp_parser_nested_name_specifier_opt (parser,
18320 /*typename_keyword_p=*/false,
18321 /*check_dependency_p=*/false,
18322 /*type_p=*/true,
18323 /*is_declaration=*/false);
18324 /* If there was a nested-name-specifier, then there *must* be an
18325 identifier. */
18326 if (nested_name_specifier)
18327 {
18328 type_start_token = cp_lexer_peek_token (parser->lexer);
18329 /* Although the grammar says `identifier', it really means
18330 `class-name' or `template-name'. You are only allowed to
18331 define a class that has already been declared with this
18332 syntax.
18333
18334 The proposed resolution for Core Issue 180 says that wherever
18335 you see `class T::X' you should treat `X' as a type-name.
18336
18337 It is OK to define an inaccessible class; for example:
18338
18339 class A { class B; };
18340 class A::B {};
18341
18342 We do not know if we will see a class-name, or a
18343 template-name. We look for a class-name first, in case the
18344 class-name is a template-id; if we looked for the
18345 template-name first we would stop after the template-name. */
18346 cp_parser_parse_tentatively (parser);
18347 type = cp_parser_class_name (parser,
18348 /*typename_keyword_p=*/false,
18349 /*template_keyword_p=*/false,
18350 class_type,
18351 /*check_dependency_p=*/false,
18352 /*class_head_p=*/true,
18353 /*is_declaration=*/false);
18354 /* If that didn't work, ignore the nested-name-specifier. */
18355 if (!cp_parser_parse_definitely (parser))
18356 {
18357 invalid_nested_name_p = true;
18358 type_start_token = cp_lexer_peek_token (parser->lexer);
18359 id = cp_parser_identifier (parser);
18360 if (id == error_mark_node)
18361 id = NULL_TREE;
18362 }
18363 /* If we could not find a corresponding TYPE, treat this
18364 declaration like an unqualified declaration. */
18365 if (type == error_mark_node)
18366 nested_name_specifier = NULL_TREE;
18367 /* Otherwise, count the number of templates used in TYPE and its
18368 containing scopes. */
18369 else
18370 {
18371 tree scope;
18372
18373 for (scope = TREE_TYPE (type);
18374 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18375 scope = (TYPE_P (scope)
18376 ? TYPE_CONTEXT (scope)
18377 : DECL_CONTEXT (scope)))
18378 if (TYPE_P (scope)
18379 && CLASS_TYPE_P (scope)
18380 && CLASSTYPE_TEMPLATE_INFO (scope)
18381 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18382 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18383 ++num_templates;
18384 }
18385 }
18386 /* Otherwise, the identifier is optional. */
18387 else
18388 {
18389 /* We don't know whether what comes next is a template-id,
18390 an identifier, or nothing at all. */
18391 cp_parser_parse_tentatively (parser);
18392 /* Check for a template-id. */
18393 type_start_token = cp_lexer_peek_token (parser->lexer);
18394 id = cp_parser_template_id (parser,
18395 /*template_keyword_p=*/false,
18396 /*check_dependency_p=*/true,
18397 class_key,
18398 /*is_declaration=*/true);
18399 /* If that didn't work, it could still be an identifier. */
18400 if (!cp_parser_parse_definitely (parser))
18401 {
18402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18403 {
18404 type_start_token = cp_lexer_peek_token (parser->lexer);
18405 id = cp_parser_identifier (parser);
18406 }
18407 else
18408 id = NULL_TREE;
18409 }
18410 else
18411 {
18412 template_id_p = true;
18413 ++num_templates;
18414 }
18415 }
18416
18417 pop_deferring_access_checks ();
18418
18419 if (id)
18420 {
18421 cp_parser_check_for_invalid_template_id (parser, id,
18422 class_key,
18423 type_start_token->location);
18424 }
18425 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18426
18427 /* If it's not a `:' or a `{' then we can't really be looking at a
18428 class-head, since a class-head only appears as part of a
18429 class-specifier. We have to detect this situation before calling
18430 xref_tag, since that has irreversible side-effects. */
18431 if (!cp_parser_next_token_starts_class_definition_p (parser))
18432 {
18433 cp_parser_error (parser, "expected %<{%> or %<:%>");
18434 type = error_mark_node;
18435 goto out;
18436 }
18437
18438 /* At this point, we're going ahead with the class-specifier, even
18439 if some other problem occurs. */
18440 cp_parser_commit_to_tentative_parse (parser);
18441 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18442 {
18443 cp_parser_error (parser,
18444 "cannot specify %<override%> for a class");
18445 type = error_mark_node;
18446 goto out;
18447 }
18448 /* Issue the error about the overly-qualified name now. */
18449 if (qualified_p)
18450 {
18451 cp_parser_error (parser,
18452 "global qualification of class name is invalid");
18453 type = error_mark_node;
18454 goto out;
18455 }
18456 else if (invalid_nested_name_p)
18457 {
18458 cp_parser_error (parser,
18459 "qualified name does not name a class");
18460 type = error_mark_node;
18461 goto out;
18462 }
18463 else if (nested_name_specifier)
18464 {
18465 tree scope;
18466
18467 /* Reject typedef-names in class heads. */
18468 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18469 {
18470 error_at (type_start_token->location,
18471 "invalid class name in declaration of %qD",
18472 type);
18473 type = NULL_TREE;
18474 goto done;
18475 }
18476
18477 /* Figure out in what scope the declaration is being placed. */
18478 scope = current_scope ();
18479 /* If that scope does not contain the scope in which the
18480 class was originally declared, the program is invalid. */
18481 if (scope && !is_ancestor (scope, nested_name_specifier))
18482 {
18483 if (at_namespace_scope_p ())
18484 error_at (type_start_token->location,
18485 "declaration of %qD in namespace %qD which does not "
18486 "enclose %qD",
18487 type, scope, nested_name_specifier);
18488 else
18489 error_at (type_start_token->location,
18490 "declaration of %qD in %qD which does not enclose %qD",
18491 type, scope, nested_name_specifier);
18492 type = NULL_TREE;
18493 goto done;
18494 }
18495 /* [dcl.meaning]
18496
18497 A declarator-id shall not be qualified except for the
18498 definition of a ... nested class outside of its class
18499 ... [or] the definition or explicit instantiation of a
18500 class member of a namespace outside of its namespace. */
18501 if (scope == nested_name_specifier)
18502 {
18503 permerror (nested_name_specifier_token_start->location,
18504 "extra qualification not allowed");
18505 nested_name_specifier = NULL_TREE;
18506 num_templates = 0;
18507 }
18508 }
18509 /* An explicit-specialization must be preceded by "template <>". If
18510 it is not, try to recover gracefully. */
18511 if (at_namespace_scope_p ()
18512 && parser->num_template_parameter_lists == 0
18513 && template_id_p)
18514 {
18515 error_at (type_start_token->location,
18516 "an explicit specialization must be preceded by %<template <>%>");
18517 invalid_explicit_specialization_p = true;
18518 /* Take the same action that would have been taken by
18519 cp_parser_explicit_specialization. */
18520 ++parser->num_template_parameter_lists;
18521 begin_specialization ();
18522 }
18523 /* There must be no "return" statements between this point and the
18524 end of this function; set "type "to the correct return value and
18525 use "goto done;" to return. */
18526 /* Make sure that the right number of template parameters were
18527 present. */
18528 if (!cp_parser_check_template_parameters (parser, num_templates,
18529 type_start_token->location,
18530 /*declarator=*/NULL))
18531 {
18532 /* If something went wrong, there is no point in even trying to
18533 process the class-definition. */
18534 type = NULL_TREE;
18535 goto done;
18536 }
18537
18538 /* Look up the type. */
18539 if (template_id_p)
18540 {
18541 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18542 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18543 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18544 {
18545 error_at (type_start_token->location,
18546 "function template %qD redeclared as a class template", id);
18547 type = error_mark_node;
18548 }
18549 else
18550 {
18551 type = TREE_TYPE (id);
18552 type = maybe_process_partial_specialization (type);
18553 }
18554 if (nested_name_specifier)
18555 pushed_scope = push_scope (nested_name_specifier);
18556 }
18557 else if (nested_name_specifier)
18558 {
18559 tree class_type;
18560
18561 /* Given:
18562
18563 template <typename T> struct S { struct T };
18564 template <typename T> struct S<T>::T { };
18565
18566 we will get a TYPENAME_TYPE when processing the definition of
18567 `S::T'. We need to resolve it to the actual type before we
18568 try to define it. */
18569 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18570 {
18571 class_type = resolve_typename_type (TREE_TYPE (type),
18572 /*only_current_p=*/false);
18573 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18574 type = TYPE_NAME (class_type);
18575 else
18576 {
18577 cp_parser_error (parser, "could not resolve typename type");
18578 type = error_mark_node;
18579 }
18580 }
18581
18582 if (maybe_process_partial_specialization (TREE_TYPE (type))
18583 == error_mark_node)
18584 {
18585 type = NULL_TREE;
18586 goto done;
18587 }
18588
18589 class_type = current_class_type;
18590 /* Enter the scope indicated by the nested-name-specifier. */
18591 pushed_scope = push_scope (nested_name_specifier);
18592 /* Get the canonical version of this type. */
18593 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18594 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18595 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18596 {
18597 type = push_template_decl (type);
18598 if (type == error_mark_node)
18599 {
18600 type = NULL_TREE;
18601 goto done;
18602 }
18603 }
18604
18605 type = TREE_TYPE (type);
18606 *nested_name_specifier_p = true;
18607 }
18608 else /* The name is not a nested name. */
18609 {
18610 /* If the class was unnamed, create a dummy name. */
18611 if (!id)
18612 id = make_anon_name ();
18613 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18614 parser->num_template_parameter_lists);
18615 }
18616
18617 /* Indicate whether this class was declared as a `class' or as a
18618 `struct'. */
18619 if (TREE_CODE (type) == RECORD_TYPE)
18620 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18621 cp_parser_check_class_key (class_key, type);
18622
18623 /* If this type was already complete, and we see another definition,
18624 that's an error. */
18625 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18626 {
18627 error_at (type_start_token->location, "redefinition of %q#T",
18628 type);
18629 error_at (type_start_token->location, "previous definition of %q+#T",
18630 type);
18631 type = NULL_TREE;
18632 goto done;
18633 }
18634 else if (type == error_mark_node)
18635 type = NULL_TREE;
18636
18637 if (type)
18638 {
18639 /* Apply attributes now, before any use of the class as a template
18640 argument in its base list. */
18641 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18642 fixup_attribute_variants (type);
18643 }
18644
18645 /* We will have entered the scope containing the class; the names of
18646 base classes should be looked up in that context. For example:
18647
18648 struct A { struct B {}; struct C; };
18649 struct A::C : B {};
18650
18651 is valid. */
18652
18653 /* Get the list of base-classes, if there is one. */
18654 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18655 bases = cp_parser_base_clause (parser);
18656 else
18657 bases = NULL_TREE;
18658
18659 /* If we're really defining a class, process the base classes.
18660 If they're invalid, fail. */
18661 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18662 && !xref_basetypes (type, bases))
18663 type = NULL_TREE;
18664
18665 done:
18666 /* Leave the scope given by the nested-name-specifier. We will
18667 enter the class scope itself while processing the members. */
18668 if (pushed_scope)
18669 pop_scope (pushed_scope);
18670
18671 if (invalid_explicit_specialization_p)
18672 {
18673 end_specialization ();
18674 --parser->num_template_parameter_lists;
18675 }
18676
18677 if (type)
18678 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18679 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18680 CLASSTYPE_FINAL (type) = 1;
18681 out:
18682 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18683 return type;
18684 }
18685
18686 /* Parse a class-key.
18687
18688 class-key:
18689 class
18690 struct
18691 union
18692
18693 Returns the kind of class-key specified, or none_type to indicate
18694 error. */
18695
18696 static enum tag_types
18697 cp_parser_class_key (cp_parser* parser)
18698 {
18699 cp_token *token;
18700 enum tag_types tag_type;
18701
18702 /* Look for the class-key. */
18703 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18704 if (!token)
18705 return none_type;
18706
18707 /* Check to see if the TOKEN is a class-key. */
18708 tag_type = cp_parser_token_is_class_key (token);
18709 if (!tag_type)
18710 cp_parser_error (parser, "expected class-key");
18711 return tag_type;
18712 }
18713
18714 /* Parse an (optional) member-specification.
18715
18716 member-specification:
18717 member-declaration member-specification [opt]
18718 access-specifier : member-specification [opt] */
18719
18720 static void
18721 cp_parser_member_specification_opt (cp_parser* parser)
18722 {
18723 while (true)
18724 {
18725 cp_token *token;
18726 enum rid keyword;
18727
18728 /* Peek at the next token. */
18729 token = cp_lexer_peek_token (parser->lexer);
18730 /* If it's a `}', or EOF then we've seen all the members. */
18731 if (token->type == CPP_CLOSE_BRACE
18732 || token->type == CPP_EOF
18733 || token->type == CPP_PRAGMA_EOL)
18734 break;
18735
18736 /* See if this token is a keyword. */
18737 keyword = token->keyword;
18738 switch (keyword)
18739 {
18740 case RID_PUBLIC:
18741 case RID_PROTECTED:
18742 case RID_PRIVATE:
18743 /* Consume the access-specifier. */
18744 cp_lexer_consume_token (parser->lexer);
18745 /* Remember which access-specifier is active. */
18746 current_access_specifier = token->u.value;
18747 /* Look for the `:'. */
18748 cp_parser_require (parser, CPP_COLON, RT_COLON);
18749 break;
18750
18751 default:
18752 /* Accept #pragmas at class scope. */
18753 if (token->type == CPP_PRAGMA)
18754 {
18755 cp_parser_pragma (parser, pragma_external);
18756 break;
18757 }
18758
18759 /* Otherwise, the next construction must be a
18760 member-declaration. */
18761 cp_parser_member_declaration (parser);
18762 }
18763 }
18764 }
18765
18766 /* Parse a member-declaration.
18767
18768 member-declaration:
18769 decl-specifier-seq [opt] member-declarator-list [opt] ;
18770 function-definition ; [opt]
18771 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18772 using-declaration
18773 template-declaration
18774 alias-declaration
18775
18776 member-declarator-list:
18777 member-declarator
18778 member-declarator-list , member-declarator
18779
18780 member-declarator:
18781 declarator pure-specifier [opt]
18782 declarator constant-initializer [opt]
18783 identifier [opt] : constant-expression
18784
18785 GNU Extensions:
18786
18787 member-declaration:
18788 __extension__ member-declaration
18789
18790 member-declarator:
18791 declarator attributes [opt] pure-specifier [opt]
18792 declarator attributes [opt] constant-initializer [opt]
18793 identifier [opt] attributes [opt] : constant-expression
18794
18795 C++0x Extensions:
18796
18797 member-declaration:
18798 static_assert-declaration */
18799
18800 static void
18801 cp_parser_member_declaration (cp_parser* parser)
18802 {
18803 cp_decl_specifier_seq decl_specifiers;
18804 tree prefix_attributes;
18805 tree decl;
18806 int declares_class_or_enum;
18807 bool friend_p;
18808 cp_token *token = NULL;
18809 cp_token *decl_spec_token_start = NULL;
18810 cp_token *initializer_token_start = NULL;
18811 int saved_pedantic;
18812 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18813
18814 /* Check for the `__extension__' keyword. */
18815 if (cp_parser_extension_opt (parser, &saved_pedantic))
18816 {
18817 /* Recurse. */
18818 cp_parser_member_declaration (parser);
18819 /* Restore the old value of the PEDANTIC flag. */
18820 pedantic = saved_pedantic;
18821
18822 return;
18823 }
18824
18825 /* Check for a template-declaration. */
18826 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18827 {
18828 /* An explicit specialization here is an error condition, and we
18829 expect the specialization handler to detect and report this. */
18830 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18831 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18832 cp_parser_explicit_specialization (parser);
18833 else
18834 cp_parser_template_declaration (parser, /*member_p=*/true);
18835
18836 return;
18837 }
18838
18839 /* Check for a using-declaration. */
18840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18841 {
18842 if (cxx_dialect < cxx0x)
18843 {
18844 /* Parse the using-declaration. */
18845 cp_parser_using_declaration (parser,
18846 /*access_declaration_p=*/false);
18847 return;
18848 }
18849 else
18850 {
18851 tree decl;
18852 cp_parser_parse_tentatively (parser);
18853 decl = cp_parser_alias_declaration (parser);
18854 if (cp_parser_parse_definitely (parser))
18855 finish_member_declaration (decl);
18856 else
18857 cp_parser_using_declaration (parser,
18858 /*access_declaration_p=*/false);
18859 return;
18860 }
18861 }
18862
18863 /* Check for @defs. */
18864 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18865 {
18866 tree ivar, member;
18867 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18868 ivar = ivar_chains;
18869 while (ivar)
18870 {
18871 member = ivar;
18872 ivar = TREE_CHAIN (member);
18873 TREE_CHAIN (member) = NULL_TREE;
18874 finish_member_declaration (member);
18875 }
18876 return;
18877 }
18878
18879 /* If the next token is `static_assert' we have a static assertion. */
18880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18881 {
18882 cp_parser_static_assert (parser, /*member_p=*/true);
18883 return;
18884 }
18885
18886 parser->colon_corrects_to_scope_p = false;
18887
18888 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18889 goto out;
18890
18891 /* Parse the decl-specifier-seq. */
18892 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18893 cp_parser_decl_specifier_seq (parser,
18894 CP_PARSER_FLAGS_OPTIONAL,
18895 &decl_specifiers,
18896 &declares_class_or_enum);
18897 prefix_attributes = decl_specifiers.attributes;
18898 decl_specifiers.attributes = NULL_TREE;
18899 /* Check for an invalid type-name. */
18900 if (!decl_specifiers.any_type_specifiers_p
18901 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18902 goto out;
18903 /* If there is no declarator, then the decl-specifier-seq should
18904 specify a type. */
18905 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18906 {
18907 /* If there was no decl-specifier-seq, and the next token is a
18908 `;', then we have something like:
18909
18910 struct S { ; };
18911
18912 [class.mem]
18913
18914 Each member-declaration shall declare at least one member
18915 name of the class. */
18916 if (!decl_specifiers.any_specifiers_p)
18917 {
18918 cp_token *token = cp_lexer_peek_token (parser->lexer);
18919 if (!in_system_header_at (token->location))
18920 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
18921 }
18922 else
18923 {
18924 tree type;
18925
18926 /* See if this declaration is a friend. */
18927 friend_p = cp_parser_friend_p (&decl_specifiers);
18928 /* If there were decl-specifiers, check to see if there was
18929 a class-declaration. */
18930 type = check_tag_decl (&decl_specifiers);
18931 /* Nested classes have already been added to the class, but
18932 a `friend' needs to be explicitly registered. */
18933 if (friend_p)
18934 {
18935 /* If the `friend' keyword was present, the friend must
18936 be introduced with a class-key. */
18937 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18938 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
18939 "in C++03 a class-key must be used "
18940 "when declaring a friend");
18941 /* In this case:
18942
18943 template <typename T> struct A {
18944 friend struct A<T>::B;
18945 };
18946
18947 A<T>::B will be represented by a TYPENAME_TYPE, and
18948 therefore not recognized by check_tag_decl. */
18949 if (!type)
18950 {
18951 type = decl_specifiers.type;
18952 if (type && TREE_CODE (type) == TYPE_DECL)
18953 type = TREE_TYPE (type);
18954 }
18955 if (!type || !TYPE_P (type))
18956 error_at (decl_spec_token_start->location,
18957 "friend declaration does not name a class or "
18958 "function");
18959 else
18960 make_friend_class (current_class_type, type,
18961 /*complain=*/true);
18962 }
18963 /* If there is no TYPE, an error message will already have
18964 been issued. */
18965 else if (!type || type == error_mark_node)
18966 ;
18967 /* An anonymous aggregate has to be handled specially; such
18968 a declaration really declares a data member (with a
18969 particular type), as opposed to a nested class. */
18970 else if (ANON_AGGR_TYPE_P (type))
18971 {
18972 /* C++11 9.5/6. */
18973 if (decl_specifiers.storage_class != sc_none)
18974 error_at (decl_spec_token_start->location,
18975 "a storage class on an anonymous aggregate "
18976 "in class scope is not allowed");
18977
18978 /* Remove constructors and such from TYPE, now that we
18979 know it is an anonymous aggregate. */
18980 fixup_anonymous_aggr (type);
18981 /* And make the corresponding data member. */
18982 decl = build_decl (decl_spec_token_start->location,
18983 FIELD_DECL, NULL_TREE, type);
18984 /* Add it to the class. */
18985 finish_member_declaration (decl);
18986 }
18987 else
18988 cp_parser_check_access_in_redeclaration
18989 (TYPE_NAME (type),
18990 decl_spec_token_start->location);
18991 }
18992 }
18993 else
18994 {
18995 bool assume_semicolon = false;
18996
18997 /* See if these declarations will be friends. */
18998 friend_p = cp_parser_friend_p (&decl_specifiers);
18999
19000 /* Keep going until we hit the `;' at the end of the
19001 declaration. */
19002 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19003 {
19004 tree attributes = NULL_TREE;
19005 tree first_attribute;
19006
19007 /* Peek at the next token. */
19008 token = cp_lexer_peek_token (parser->lexer);
19009
19010 /* Check for a bitfield declaration. */
19011 if (token->type == CPP_COLON
19012 || (token->type == CPP_NAME
19013 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19014 == CPP_COLON))
19015 {
19016 tree identifier;
19017 tree width;
19018
19019 /* Get the name of the bitfield. Note that we cannot just
19020 check TOKEN here because it may have been invalidated by
19021 the call to cp_lexer_peek_nth_token above. */
19022 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19023 identifier = cp_parser_identifier (parser);
19024 else
19025 identifier = NULL_TREE;
19026
19027 /* Consume the `:' token. */
19028 cp_lexer_consume_token (parser->lexer);
19029 /* Get the width of the bitfield. */
19030 width
19031 = cp_parser_constant_expression (parser,
19032 /*allow_non_constant=*/false,
19033 NULL);
19034
19035 /* Look for attributes that apply to the bitfield. */
19036 attributes = cp_parser_attributes_opt (parser);
19037 /* Remember which attributes are prefix attributes and
19038 which are not. */
19039 first_attribute = attributes;
19040 /* Combine the attributes. */
19041 attributes = chainon (prefix_attributes, attributes);
19042
19043 /* Create the bitfield declaration. */
19044 decl = grokbitfield (identifier
19045 ? make_id_declarator (NULL_TREE,
19046 identifier,
19047 sfk_none)
19048 : NULL,
19049 &decl_specifiers,
19050 width,
19051 attributes);
19052 }
19053 else
19054 {
19055 cp_declarator *declarator;
19056 tree initializer;
19057 tree asm_specification;
19058 int ctor_dtor_or_conv_p;
19059
19060 /* Parse the declarator. */
19061 declarator
19062 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19063 &ctor_dtor_or_conv_p,
19064 /*parenthesized_p=*/NULL,
19065 /*member_p=*/true);
19066
19067 /* If something went wrong parsing the declarator, make sure
19068 that we at least consume some tokens. */
19069 if (declarator == cp_error_declarator)
19070 {
19071 /* Skip to the end of the statement. */
19072 cp_parser_skip_to_end_of_statement (parser);
19073 /* If the next token is not a semicolon, that is
19074 probably because we just skipped over the body of
19075 a function. So, we consume a semicolon if
19076 present, but do not issue an error message if it
19077 is not present. */
19078 if (cp_lexer_next_token_is (parser->lexer,
19079 CPP_SEMICOLON))
19080 cp_lexer_consume_token (parser->lexer);
19081 goto out;
19082 }
19083
19084 if (declares_class_or_enum & 2)
19085 cp_parser_check_for_definition_in_return_type
19086 (declarator, decl_specifiers.type,
19087 decl_specifiers.locations[ds_type_spec]);
19088
19089 /* Look for an asm-specification. */
19090 asm_specification = cp_parser_asm_specification_opt (parser);
19091 /* Look for attributes that apply to the declaration. */
19092 attributes = cp_parser_attributes_opt (parser);
19093 /* Remember which attributes are prefix attributes and
19094 which are not. */
19095 first_attribute = attributes;
19096 /* Combine the attributes. */
19097 attributes = chainon (prefix_attributes, attributes);
19098
19099 /* If it's an `=', then we have a constant-initializer or a
19100 pure-specifier. It is not correct to parse the
19101 initializer before registering the member declaration
19102 since the member declaration should be in scope while
19103 its initializer is processed. However, the rest of the
19104 front end does not yet provide an interface that allows
19105 us to handle this correctly. */
19106 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19107 {
19108 /* In [class.mem]:
19109
19110 A pure-specifier shall be used only in the declaration of
19111 a virtual function.
19112
19113 A member-declarator can contain a constant-initializer
19114 only if it declares a static member of integral or
19115 enumeration type.
19116
19117 Therefore, if the DECLARATOR is for a function, we look
19118 for a pure-specifier; otherwise, we look for a
19119 constant-initializer. When we call `grokfield', it will
19120 perform more stringent semantics checks. */
19121 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19122 if (function_declarator_p (declarator)
19123 || (decl_specifiers.type
19124 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19125 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19126 == FUNCTION_TYPE)))
19127 initializer = cp_parser_pure_specifier (parser);
19128 else if (decl_specifiers.storage_class != sc_static)
19129 initializer = cp_parser_save_nsdmi (parser);
19130 else if (cxx_dialect >= cxx0x)
19131 {
19132 bool nonconst;
19133 /* Don't require a constant rvalue in C++11, since we
19134 might want a reference constant. We'll enforce
19135 constancy later. */
19136 cp_lexer_consume_token (parser->lexer);
19137 /* Parse the initializer. */
19138 initializer = cp_parser_initializer_clause (parser,
19139 &nonconst);
19140 }
19141 else
19142 /* Parse the initializer. */
19143 initializer = cp_parser_constant_initializer (parser);
19144 }
19145 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19146 && !function_declarator_p (declarator))
19147 {
19148 bool x;
19149 if (decl_specifiers.storage_class != sc_static)
19150 initializer = cp_parser_save_nsdmi (parser);
19151 else
19152 initializer = cp_parser_initializer (parser, &x, &x);
19153 }
19154 /* Otherwise, there is no initializer. */
19155 else
19156 initializer = NULL_TREE;
19157
19158 /* See if we are probably looking at a function
19159 definition. We are certainly not looking at a
19160 member-declarator. Calling `grokfield' has
19161 side-effects, so we must not do it unless we are sure
19162 that we are looking at a member-declarator. */
19163 if (cp_parser_token_starts_function_definition_p
19164 (cp_lexer_peek_token (parser->lexer)))
19165 {
19166 /* The grammar does not allow a pure-specifier to be
19167 used when a member function is defined. (It is
19168 possible that this fact is an oversight in the
19169 standard, since a pure function may be defined
19170 outside of the class-specifier. */
19171 if (initializer && initializer_token_start)
19172 error_at (initializer_token_start->location,
19173 "pure-specifier on function-definition");
19174 decl = cp_parser_save_member_function_body (parser,
19175 &decl_specifiers,
19176 declarator,
19177 attributes);
19178 /* If the member was not a friend, declare it here. */
19179 if (!friend_p)
19180 finish_member_declaration (decl);
19181 /* Peek at the next token. */
19182 token = cp_lexer_peek_token (parser->lexer);
19183 /* If the next token is a semicolon, consume it. */
19184 if (token->type == CPP_SEMICOLON)
19185 cp_lexer_consume_token (parser->lexer);
19186 goto out;
19187 }
19188 else
19189 if (declarator->kind == cdk_function)
19190 declarator->id_loc = token->location;
19191 /* Create the declaration. */
19192 decl = grokfield (declarator, &decl_specifiers,
19193 initializer, /*init_const_expr_p=*/true,
19194 asm_specification,
19195 attributes);
19196 }
19197
19198 /* Reset PREFIX_ATTRIBUTES. */
19199 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19200 attributes = TREE_CHAIN (attributes);
19201 if (attributes)
19202 TREE_CHAIN (attributes) = NULL_TREE;
19203
19204 /* If there is any qualification still in effect, clear it
19205 now; we will be starting fresh with the next declarator. */
19206 parser->scope = NULL_TREE;
19207 parser->qualifying_scope = NULL_TREE;
19208 parser->object_scope = NULL_TREE;
19209 /* If it's a `,', then there are more declarators. */
19210 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19211 cp_lexer_consume_token (parser->lexer);
19212 /* If the next token isn't a `;', then we have a parse error. */
19213 else if (cp_lexer_next_token_is_not (parser->lexer,
19214 CPP_SEMICOLON))
19215 {
19216 /* The next token might be a ways away from where the
19217 actual semicolon is missing. Find the previous token
19218 and use that for our error position. */
19219 cp_token *token = cp_lexer_previous_token (parser->lexer);
19220 error_at (token->location,
19221 "expected %<;%> at end of member declaration");
19222
19223 /* Assume that the user meant to provide a semicolon. If
19224 we were to cp_parser_skip_to_end_of_statement, we might
19225 skip to a semicolon inside a member function definition
19226 and issue nonsensical error messages. */
19227 assume_semicolon = true;
19228 }
19229
19230 if (decl)
19231 {
19232 /* Add DECL to the list of members. */
19233 if (!friend_p)
19234 finish_member_declaration (decl);
19235
19236 if (TREE_CODE (decl) == FUNCTION_DECL)
19237 cp_parser_save_default_args (parser, decl);
19238 else if (TREE_CODE (decl) == FIELD_DECL
19239 && !DECL_C_BIT_FIELD (decl)
19240 && DECL_INITIAL (decl))
19241 /* Add DECL to the queue of NSDMI to be parsed later. */
19242 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19243 }
19244
19245 if (assume_semicolon)
19246 goto out;
19247 }
19248 }
19249
19250 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19251 out:
19252 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19253 }
19254
19255 /* Parse a pure-specifier.
19256
19257 pure-specifier:
19258 = 0
19259
19260 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19261 Otherwise, ERROR_MARK_NODE is returned. */
19262
19263 static tree
19264 cp_parser_pure_specifier (cp_parser* parser)
19265 {
19266 cp_token *token;
19267
19268 /* Look for the `=' token. */
19269 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19270 return error_mark_node;
19271 /* Look for the `0' token. */
19272 token = cp_lexer_peek_token (parser->lexer);
19273
19274 if (token->type == CPP_EOF
19275 || token->type == CPP_PRAGMA_EOL)
19276 return error_mark_node;
19277
19278 cp_lexer_consume_token (parser->lexer);
19279
19280 /* Accept = default or = delete in c++0x mode. */
19281 if (token->keyword == RID_DEFAULT
19282 || token->keyword == RID_DELETE)
19283 {
19284 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19285 return token->u.value;
19286 }
19287
19288 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19289 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19290 {
19291 cp_parser_error (parser,
19292 "invalid pure specifier (only %<= 0%> is allowed)");
19293 cp_parser_skip_to_end_of_statement (parser);
19294 return error_mark_node;
19295 }
19296 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19297 {
19298 error_at (token->location, "templates may not be %<virtual%>");
19299 return error_mark_node;
19300 }
19301
19302 return integer_zero_node;
19303 }
19304
19305 /* Parse a constant-initializer.
19306
19307 constant-initializer:
19308 = constant-expression
19309
19310 Returns a representation of the constant-expression. */
19311
19312 static tree
19313 cp_parser_constant_initializer (cp_parser* parser)
19314 {
19315 /* Look for the `=' token. */
19316 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19317 return error_mark_node;
19318
19319 /* It is invalid to write:
19320
19321 struct S { static const int i = { 7 }; };
19322
19323 */
19324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19325 {
19326 cp_parser_error (parser,
19327 "a brace-enclosed initializer is not allowed here");
19328 /* Consume the opening brace. */
19329 cp_lexer_consume_token (parser->lexer);
19330 /* Skip the initializer. */
19331 cp_parser_skip_to_closing_brace (parser);
19332 /* Look for the trailing `}'. */
19333 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19334
19335 return error_mark_node;
19336 }
19337
19338 return cp_parser_constant_expression (parser,
19339 /*allow_non_constant=*/false,
19340 NULL);
19341 }
19342
19343 /* Derived classes [gram.class.derived] */
19344
19345 /* Parse a base-clause.
19346
19347 base-clause:
19348 : base-specifier-list
19349
19350 base-specifier-list:
19351 base-specifier ... [opt]
19352 base-specifier-list , base-specifier ... [opt]
19353
19354 Returns a TREE_LIST representing the base-classes, in the order in
19355 which they were declared. The representation of each node is as
19356 described by cp_parser_base_specifier.
19357
19358 In the case that no bases are specified, this function will return
19359 NULL_TREE, not ERROR_MARK_NODE. */
19360
19361 static tree
19362 cp_parser_base_clause (cp_parser* parser)
19363 {
19364 tree bases = NULL_TREE;
19365
19366 /* Look for the `:' that begins the list. */
19367 cp_parser_require (parser, CPP_COLON, RT_COLON);
19368
19369 /* Scan the base-specifier-list. */
19370 while (true)
19371 {
19372 cp_token *token;
19373 tree base;
19374 bool pack_expansion_p = false;
19375
19376 /* Look for the base-specifier. */
19377 base = cp_parser_base_specifier (parser);
19378 /* Look for the (optional) ellipsis. */
19379 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19380 {
19381 /* Consume the `...'. */
19382 cp_lexer_consume_token (parser->lexer);
19383
19384 pack_expansion_p = true;
19385 }
19386
19387 /* Add BASE to the front of the list. */
19388 if (base && base != error_mark_node)
19389 {
19390 if (pack_expansion_p)
19391 /* Make this a pack expansion type. */
19392 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19393
19394 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19395 {
19396 TREE_CHAIN (base) = bases;
19397 bases = base;
19398 }
19399 }
19400 /* Peek at the next token. */
19401 token = cp_lexer_peek_token (parser->lexer);
19402 /* If it's not a comma, then the list is complete. */
19403 if (token->type != CPP_COMMA)
19404 break;
19405 /* Consume the `,'. */
19406 cp_lexer_consume_token (parser->lexer);
19407 }
19408
19409 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19410 base class had a qualified name. However, the next name that
19411 appears is certainly not qualified. */
19412 parser->scope = NULL_TREE;
19413 parser->qualifying_scope = NULL_TREE;
19414 parser->object_scope = NULL_TREE;
19415
19416 return nreverse (bases);
19417 }
19418
19419 /* Parse a base-specifier.
19420
19421 base-specifier:
19422 :: [opt] nested-name-specifier [opt] class-name
19423 virtual access-specifier [opt] :: [opt] nested-name-specifier
19424 [opt] class-name
19425 access-specifier virtual [opt] :: [opt] nested-name-specifier
19426 [opt] class-name
19427
19428 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19429 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19430 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19431 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19432
19433 static tree
19434 cp_parser_base_specifier (cp_parser* parser)
19435 {
19436 cp_token *token;
19437 bool done = false;
19438 bool virtual_p = false;
19439 bool duplicate_virtual_error_issued_p = false;
19440 bool duplicate_access_error_issued_p = false;
19441 bool class_scope_p, template_p;
19442 tree access = access_default_node;
19443 tree type;
19444
19445 /* Process the optional `virtual' and `access-specifier'. */
19446 while (!done)
19447 {
19448 /* Peek at the next token. */
19449 token = cp_lexer_peek_token (parser->lexer);
19450 /* Process `virtual'. */
19451 switch (token->keyword)
19452 {
19453 case RID_VIRTUAL:
19454 /* If `virtual' appears more than once, issue an error. */
19455 if (virtual_p && !duplicate_virtual_error_issued_p)
19456 {
19457 cp_parser_error (parser,
19458 "%<virtual%> specified more than once in base-specified");
19459 duplicate_virtual_error_issued_p = true;
19460 }
19461
19462 virtual_p = true;
19463
19464 /* Consume the `virtual' token. */
19465 cp_lexer_consume_token (parser->lexer);
19466
19467 break;
19468
19469 case RID_PUBLIC:
19470 case RID_PROTECTED:
19471 case RID_PRIVATE:
19472 /* If more than one access specifier appears, issue an
19473 error. */
19474 if (access != access_default_node
19475 && !duplicate_access_error_issued_p)
19476 {
19477 cp_parser_error (parser,
19478 "more than one access specifier in base-specified");
19479 duplicate_access_error_issued_p = true;
19480 }
19481
19482 access = ridpointers[(int) token->keyword];
19483
19484 /* Consume the access-specifier. */
19485 cp_lexer_consume_token (parser->lexer);
19486
19487 break;
19488
19489 default:
19490 done = true;
19491 break;
19492 }
19493 }
19494 /* It is not uncommon to see programs mechanically, erroneously, use
19495 the 'typename' keyword to denote (dependent) qualified types
19496 as base classes. */
19497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19498 {
19499 token = cp_lexer_peek_token (parser->lexer);
19500 if (!processing_template_decl)
19501 error_at (token->location,
19502 "keyword %<typename%> not allowed outside of templates");
19503 else
19504 error_at (token->location,
19505 "keyword %<typename%> not allowed in this context "
19506 "(the base class is implicitly a type)");
19507 cp_lexer_consume_token (parser->lexer);
19508 }
19509
19510 /* Look for the optional `::' operator. */
19511 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19512 /* Look for the nested-name-specifier. The simplest way to
19513 implement:
19514
19515 [temp.res]
19516
19517 The keyword `typename' is not permitted in a base-specifier or
19518 mem-initializer; in these contexts a qualified name that
19519 depends on a template-parameter is implicitly assumed to be a
19520 type name.
19521
19522 is to pretend that we have seen the `typename' keyword at this
19523 point. */
19524 cp_parser_nested_name_specifier_opt (parser,
19525 /*typename_keyword_p=*/true,
19526 /*check_dependency_p=*/true,
19527 typename_type,
19528 /*is_declaration=*/true);
19529 /* If the base class is given by a qualified name, assume that names
19530 we see are type names or templates, as appropriate. */
19531 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19532 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19533
19534 if (!parser->scope
19535 && cp_lexer_next_token_is_decltype (parser->lexer))
19536 /* DR 950 allows decltype as a base-specifier. */
19537 type = cp_parser_decltype (parser);
19538 else
19539 {
19540 /* Otherwise, look for the class-name. */
19541 type = cp_parser_class_name (parser,
19542 class_scope_p,
19543 template_p,
19544 typename_type,
19545 /*check_dependency_p=*/true,
19546 /*class_head_p=*/false,
19547 /*is_declaration=*/true);
19548 type = TREE_TYPE (type);
19549 }
19550
19551 if (type == error_mark_node)
19552 return error_mark_node;
19553
19554 return finish_base_specifier (type, access, virtual_p);
19555 }
19556
19557 /* Exception handling [gram.exception] */
19558
19559 /* Parse an (optional) noexcept-specification.
19560
19561 noexcept-specification:
19562 noexcept ( constant-expression ) [opt]
19563
19564 If no noexcept-specification is present, returns NULL_TREE.
19565 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19566 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19567 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19568 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19569 in which case a boolean condition is returned instead. */
19570
19571 static tree
19572 cp_parser_noexcept_specification_opt (cp_parser* parser,
19573 bool require_constexpr,
19574 bool* consumed_expr,
19575 bool return_cond)
19576 {
19577 cp_token *token;
19578 const char *saved_message;
19579
19580 /* Peek at the next token. */
19581 token = cp_lexer_peek_token (parser->lexer);
19582
19583 /* Is it a noexcept-specification? */
19584 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19585 {
19586 tree expr;
19587 cp_lexer_consume_token (parser->lexer);
19588
19589 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19590 {
19591 cp_lexer_consume_token (parser->lexer);
19592
19593 if (require_constexpr)
19594 {
19595 /* Types may not be defined in an exception-specification. */
19596 saved_message = parser->type_definition_forbidden_message;
19597 parser->type_definition_forbidden_message
19598 = G_("types may not be defined in an exception-specification");
19599
19600 expr = cp_parser_constant_expression (parser, false, NULL);
19601
19602 /* Restore the saved message. */
19603 parser->type_definition_forbidden_message = saved_message;
19604 }
19605 else
19606 {
19607 expr = cp_parser_expression (parser, false, NULL);
19608 *consumed_expr = true;
19609 }
19610
19611 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19612 }
19613 else
19614 {
19615 expr = boolean_true_node;
19616 if (!require_constexpr)
19617 *consumed_expr = false;
19618 }
19619
19620 /* We cannot build a noexcept-spec right away because this will check
19621 that expr is a constexpr. */
19622 if (!return_cond)
19623 return build_noexcept_spec (expr, tf_warning_or_error);
19624 else
19625 return expr;
19626 }
19627 else
19628 return NULL_TREE;
19629 }
19630
19631 /* Parse an (optional) exception-specification.
19632
19633 exception-specification:
19634 throw ( type-id-list [opt] )
19635
19636 Returns a TREE_LIST representing the exception-specification. The
19637 TREE_VALUE of each node is a type. */
19638
19639 static tree
19640 cp_parser_exception_specification_opt (cp_parser* parser)
19641 {
19642 cp_token *token;
19643 tree type_id_list;
19644 const char *saved_message;
19645
19646 /* Peek at the next token. */
19647 token = cp_lexer_peek_token (parser->lexer);
19648
19649 /* Is it a noexcept-specification? */
19650 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19651 false);
19652 if (type_id_list != NULL_TREE)
19653 return type_id_list;
19654
19655 /* If it's not `throw', then there's no exception-specification. */
19656 if (!cp_parser_is_keyword (token, RID_THROW))
19657 return NULL_TREE;
19658
19659 #if 0
19660 /* Enable this once a lot of code has transitioned to noexcept? */
19661 if (cxx_dialect >= cxx0x && !in_system_header)
19662 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19663 "deprecated in C++0x; use %<noexcept%> instead");
19664 #endif
19665
19666 /* Consume the `throw'. */
19667 cp_lexer_consume_token (parser->lexer);
19668
19669 /* Look for the `('. */
19670 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19671
19672 /* Peek at the next token. */
19673 token = cp_lexer_peek_token (parser->lexer);
19674 /* If it's not a `)', then there is a type-id-list. */
19675 if (token->type != CPP_CLOSE_PAREN)
19676 {
19677 /* Types may not be defined in an exception-specification. */
19678 saved_message = parser->type_definition_forbidden_message;
19679 parser->type_definition_forbidden_message
19680 = G_("types may not be defined in an exception-specification");
19681 /* Parse the type-id-list. */
19682 type_id_list = cp_parser_type_id_list (parser);
19683 /* Restore the saved message. */
19684 parser->type_definition_forbidden_message = saved_message;
19685 }
19686 else
19687 type_id_list = empty_except_spec;
19688
19689 /* Look for the `)'. */
19690 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19691
19692 return type_id_list;
19693 }
19694
19695 /* Parse an (optional) type-id-list.
19696
19697 type-id-list:
19698 type-id ... [opt]
19699 type-id-list , type-id ... [opt]
19700
19701 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19702 in the order that the types were presented. */
19703
19704 static tree
19705 cp_parser_type_id_list (cp_parser* parser)
19706 {
19707 tree types = NULL_TREE;
19708
19709 while (true)
19710 {
19711 cp_token *token;
19712 tree type;
19713
19714 /* Get the next type-id. */
19715 type = cp_parser_type_id (parser);
19716 /* Parse the optional ellipsis. */
19717 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19718 {
19719 /* Consume the `...'. */
19720 cp_lexer_consume_token (parser->lexer);
19721
19722 /* Turn the type into a pack expansion expression. */
19723 type = make_pack_expansion (type);
19724 }
19725 /* Add it to the list. */
19726 types = add_exception_specifier (types, type, /*complain=*/1);
19727 /* Peek at the next token. */
19728 token = cp_lexer_peek_token (parser->lexer);
19729 /* If it is not a `,', we are done. */
19730 if (token->type != CPP_COMMA)
19731 break;
19732 /* Consume the `,'. */
19733 cp_lexer_consume_token (parser->lexer);
19734 }
19735
19736 return nreverse (types);
19737 }
19738
19739 /* Parse a try-block.
19740
19741 try-block:
19742 try compound-statement handler-seq */
19743
19744 static tree
19745 cp_parser_try_block (cp_parser* parser)
19746 {
19747 tree try_block;
19748
19749 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19750 try_block = begin_try_block ();
19751 cp_parser_compound_statement (parser, NULL, true, false);
19752 finish_try_block (try_block);
19753 cp_parser_handler_seq (parser);
19754 finish_handler_sequence (try_block);
19755
19756 return try_block;
19757 }
19758
19759 /* Parse a function-try-block.
19760
19761 function-try-block:
19762 try ctor-initializer [opt] function-body handler-seq */
19763
19764 static bool
19765 cp_parser_function_try_block (cp_parser* parser)
19766 {
19767 tree compound_stmt;
19768 tree try_block;
19769 bool ctor_initializer_p;
19770
19771 /* Look for the `try' keyword. */
19772 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19773 return false;
19774 /* Let the rest of the front end know where we are. */
19775 try_block = begin_function_try_block (&compound_stmt);
19776 /* Parse the function-body. */
19777 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19778 (parser, /*in_function_try_block=*/true);
19779 /* We're done with the `try' part. */
19780 finish_function_try_block (try_block);
19781 /* Parse the handlers. */
19782 cp_parser_handler_seq (parser);
19783 /* We're done with the handlers. */
19784 finish_function_handler_sequence (try_block, compound_stmt);
19785
19786 return ctor_initializer_p;
19787 }
19788
19789 /* Parse a handler-seq.
19790
19791 handler-seq:
19792 handler handler-seq [opt] */
19793
19794 static void
19795 cp_parser_handler_seq (cp_parser* parser)
19796 {
19797 while (true)
19798 {
19799 cp_token *token;
19800
19801 /* Parse the handler. */
19802 cp_parser_handler (parser);
19803 /* Peek at the next token. */
19804 token = cp_lexer_peek_token (parser->lexer);
19805 /* If it's not `catch' then there are no more handlers. */
19806 if (!cp_parser_is_keyword (token, RID_CATCH))
19807 break;
19808 }
19809 }
19810
19811 /* Parse a handler.
19812
19813 handler:
19814 catch ( exception-declaration ) compound-statement */
19815
19816 static void
19817 cp_parser_handler (cp_parser* parser)
19818 {
19819 tree handler;
19820 tree declaration;
19821
19822 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19823 handler = begin_handler ();
19824 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19825 declaration = cp_parser_exception_declaration (parser);
19826 finish_handler_parms (declaration, handler);
19827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19828 cp_parser_compound_statement (parser, NULL, false, false);
19829 finish_handler (handler);
19830 }
19831
19832 /* Parse an exception-declaration.
19833
19834 exception-declaration:
19835 type-specifier-seq declarator
19836 type-specifier-seq abstract-declarator
19837 type-specifier-seq
19838 ...
19839
19840 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19841 ellipsis variant is used. */
19842
19843 static tree
19844 cp_parser_exception_declaration (cp_parser* parser)
19845 {
19846 cp_decl_specifier_seq type_specifiers;
19847 cp_declarator *declarator;
19848 const char *saved_message;
19849
19850 /* If it's an ellipsis, it's easy to handle. */
19851 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19852 {
19853 /* Consume the `...' token. */
19854 cp_lexer_consume_token (parser->lexer);
19855 return NULL_TREE;
19856 }
19857
19858 /* Types may not be defined in exception-declarations. */
19859 saved_message = parser->type_definition_forbidden_message;
19860 parser->type_definition_forbidden_message
19861 = G_("types may not be defined in exception-declarations");
19862
19863 /* Parse the type-specifier-seq. */
19864 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19865 /*is_trailing_return=*/false,
19866 &type_specifiers);
19867 /* If it's a `)', then there is no declarator. */
19868 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19869 declarator = NULL;
19870 else
19871 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19872 /*ctor_dtor_or_conv_p=*/NULL,
19873 /*parenthesized_p=*/NULL,
19874 /*member_p=*/false);
19875
19876 /* Restore the saved message. */
19877 parser->type_definition_forbidden_message = saved_message;
19878
19879 if (!type_specifiers.any_specifiers_p)
19880 return error_mark_node;
19881
19882 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19883 }
19884
19885 /* Parse a throw-expression.
19886
19887 throw-expression:
19888 throw assignment-expression [opt]
19889
19890 Returns a THROW_EXPR representing the throw-expression. */
19891
19892 static tree
19893 cp_parser_throw_expression (cp_parser* parser)
19894 {
19895 tree expression;
19896 cp_token* token;
19897
19898 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19899 token = cp_lexer_peek_token (parser->lexer);
19900 /* Figure out whether or not there is an assignment-expression
19901 following the "throw" keyword. */
19902 if (token->type == CPP_COMMA
19903 || token->type == CPP_SEMICOLON
19904 || token->type == CPP_CLOSE_PAREN
19905 || token->type == CPP_CLOSE_SQUARE
19906 || token->type == CPP_CLOSE_BRACE
19907 || token->type == CPP_COLON)
19908 expression = NULL_TREE;
19909 else
19910 expression = cp_parser_assignment_expression (parser,
19911 /*cast_p=*/false, NULL);
19912
19913 return build_throw (expression);
19914 }
19915
19916 /* GNU Extensions */
19917
19918 /* Parse an (optional) asm-specification.
19919
19920 asm-specification:
19921 asm ( string-literal )
19922
19923 If the asm-specification is present, returns a STRING_CST
19924 corresponding to the string-literal. Otherwise, returns
19925 NULL_TREE. */
19926
19927 static tree
19928 cp_parser_asm_specification_opt (cp_parser* parser)
19929 {
19930 cp_token *token;
19931 tree asm_specification;
19932
19933 /* Peek at the next token. */
19934 token = cp_lexer_peek_token (parser->lexer);
19935 /* If the next token isn't the `asm' keyword, then there's no
19936 asm-specification. */
19937 if (!cp_parser_is_keyword (token, RID_ASM))
19938 return NULL_TREE;
19939
19940 /* Consume the `asm' token. */
19941 cp_lexer_consume_token (parser->lexer);
19942 /* Look for the `('. */
19943 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19944
19945 /* Look for the string-literal. */
19946 asm_specification = cp_parser_string_literal (parser, false, false);
19947
19948 /* Look for the `)'. */
19949 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19950
19951 return asm_specification;
19952 }
19953
19954 /* Parse an asm-operand-list.
19955
19956 asm-operand-list:
19957 asm-operand
19958 asm-operand-list , asm-operand
19959
19960 asm-operand:
19961 string-literal ( expression )
19962 [ string-literal ] string-literal ( expression )
19963
19964 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19965 each node is the expression. The TREE_PURPOSE is itself a
19966 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19967 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19968 is a STRING_CST for the string literal before the parenthesis. Returns
19969 ERROR_MARK_NODE if any of the operands are invalid. */
19970
19971 static tree
19972 cp_parser_asm_operand_list (cp_parser* parser)
19973 {
19974 tree asm_operands = NULL_TREE;
19975 bool invalid_operands = false;
19976
19977 while (true)
19978 {
19979 tree string_literal;
19980 tree expression;
19981 tree name;
19982
19983 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19984 {
19985 /* Consume the `[' token. */
19986 cp_lexer_consume_token (parser->lexer);
19987 /* Read the operand name. */
19988 name = cp_parser_identifier (parser);
19989 if (name != error_mark_node)
19990 name = build_string (IDENTIFIER_LENGTH (name),
19991 IDENTIFIER_POINTER (name));
19992 /* Look for the closing `]'. */
19993 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19994 }
19995 else
19996 name = NULL_TREE;
19997 /* Look for the string-literal. */
19998 string_literal = cp_parser_string_literal (parser, false, false);
19999
20000 /* Look for the `('. */
20001 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20002 /* Parse the expression. */
20003 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20004 /* Look for the `)'. */
20005 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20006
20007 if (name == error_mark_node
20008 || string_literal == error_mark_node
20009 || expression == error_mark_node)
20010 invalid_operands = true;
20011
20012 /* Add this operand to the list. */
20013 asm_operands = tree_cons (build_tree_list (name, string_literal),
20014 expression,
20015 asm_operands);
20016 /* If the next token is not a `,', there are no more
20017 operands. */
20018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20019 break;
20020 /* Consume the `,'. */
20021 cp_lexer_consume_token (parser->lexer);
20022 }
20023
20024 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20025 }
20026
20027 /* Parse an asm-clobber-list.
20028
20029 asm-clobber-list:
20030 string-literal
20031 asm-clobber-list , string-literal
20032
20033 Returns a TREE_LIST, indicating the clobbers in the order that they
20034 appeared. The TREE_VALUE of each node is a STRING_CST. */
20035
20036 static tree
20037 cp_parser_asm_clobber_list (cp_parser* parser)
20038 {
20039 tree clobbers = NULL_TREE;
20040
20041 while (true)
20042 {
20043 tree string_literal;
20044
20045 /* Look for the string literal. */
20046 string_literal = cp_parser_string_literal (parser, false, false);
20047 /* Add it to the list. */
20048 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20049 /* If the next token is not a `,', then the list is
20050 complete. */
20051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20052 break;
20053 /* Consume the `,' token. */
20054 cp_lexer_consume_token (parser->lexer);
20055 }
20056
20057 return clobbers;
20058 }
20059
20060 /* Parse an asm-label-list.
20061
20062 asm-label-list:
20063 identifier
20064 asm-label-list , identifier
20065
20066 Returns a TREE_LIST, indicating the labels in the order that they
20067 appeared. The TREE_VALUE of each node is a label. */
20068
20069 static tree
20070 cp_parser_asm_label_list (cp_parser* parser)
20071 {
20072 tree labels = NULL_TREE;
20073
20074 while (true)
20075 {
20076 tree identifier, label, name;
20077
20078 /* Look for the identifier. */
20079 identifier = cp_parser_identifier (parser);
20080 if (!error_operand_p (identifier))
20081 {
20082 label = lookup_label (identifier);
20083 if (TREE_CODE (label) == LABEL_DECL)
20084 {
20085 TREE_USED (label) = 1;
20086 check_goto (label);
20087 name = build_string (IDENTIFIER_LENGTH (identifier),
20088 IDENTIFIER_POINTER (identifier));
20089 labels = tree_cons (name, label, labels);
20090 }
20091 }
20092 /* If the next token is not a `,', then the list is
20093 complete. */
20094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20095 break;
20096 /* Consume the `,' token. */
20097 cp_lexer_consume_token (parser->lexer);
20098 }
20099
20100 return nreverse (labels);
20101 }
20102
20103 /* Parse an (optional) series of attributes.
20104
20105 attributes:
20106 attributes attribute
20107
20108 attribute:
20109 __attribute__ (( attribute-list [opt] ))
20110
20111 The return value is as for cp_parser_attribute_list. */
20112
20113 static tree
20114 cp_parser_attributes_opt (cp_parser* parser)
20115 {
20116 tree attributes = NULL_TREE;
20117
20118 while (true)
20119 {
20120 cp_token *token;
20121 tree attribute_list;
20122 bool ok = true;
20123
20124 /* Peek at the next token. */
20125 token = cp_lexer_peek_token (parser->lexer);
20126 /* If it's not `__attribute__', then we're done. */
20127 if (token->keyword != RID_ATTRIBUTE)
20128 break;
20129
20130 /* Consume the `__attribute__' keyword. */
20131 cp_lexer_consume_token (parser->lexer);
20132 /* Look for the two `(' tokens. */
20133 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20134 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20135
20136 /* Peek at the next token. */
20137 token = cp_lexer_peek_token (parser->lexer);
20138 if (token->type != CPP_CLOSE_PAREN)
20139 /* Parse the attribute-list. */
20140 attribute_list = cp_parser_attribute_list (parser);
20141 else
20142 /* If the next token is a `)', then there is no attribute
20143 list. */
20144 attribute_list = NULL;
20145
20146 /* Look for the two `)' tokens. */
20147 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20148 ok = false;
20149 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20150 ok = false;
20151 if (!ok)
20152 cp_parser_skip_to_end_of_statement (parser);
20153
20154 /* Add these new attributes to the list. */
20155 attributes = chainon (attributes, attribute_list);
20156 }
20157
20158 return attributes;
20159 }
20160
20161 /* Parse an attribute-list.
20162
20163 attribute-list:
20164 attribute
20165 attribute-list , attribute
20166
20167 attribute:
20168 identifier
20169 identifier ( identifier )
20170 identifier ( identifier , expression-list )
20171 identifier ( expression-list )
20172
20173 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20174 to an attribute. The TREE_PURPOSE of each node is the identifier
20175 indicating which attribute is in use. The TREE_VALUE represents
20176 the arguments, if any. */
20177
20178 static tree
20179 cp_parser_attribute_list (cp_parser* parser)
20180 {
20181 tree attribute_list = NULL_TREE;
20182 bool save_translate_strings_p = parser->translate_strings_p;
20183
20184 parser->translate_strings_p = false;
20185 while (true)
20186 {
20187 cp_token *token;
20188 tree identifier;
20189 tree attribute;
20190
20191 /* Look for the identifier. We also allow keywords here; for
20192 example `__attribute__ ((const))' is legal. */
20193 token = cp_lexer_peek_token (parser->lexer);
20194 if (token->type == CPP_NAME
20195 || token->type == CPP_KEYWORD)
20196 {
20197 tree arguments = NULL_TREE;
20198
20199 /* Consume the token. */
20200 token = cp_lexer_consume_token (parser->lexer);
20201
20202 /* Save away the identifier that indicates which attribute
20203 this is. */
20204 identifier = (token->type == CPP_KEYWORD)
20205 /* For keywords, use the canonical spelling, not the
20206 parsed identifier. */
20207 ? ridpointers[(int) token->keyword]
20208 : token->u.value;
20209
20210 attribute = build_tree_list (identifier, NULL_TREE);
20211
20212 /* Peek at the next token. */
20213 token = cp_lexer_peek_token (parser->lexer);
20214 /* If it's an `(', then parse the attribute arguments. */
20215 if (token->type == CPP_OPEN_PAREN)
20216 {
20217 VEC(tree,gc) *vec;
20218 int attr_flag = (attribute_takes_identifier_p (identifier)
20219 ? id_attr : normal_attr);
20220 vec = cp_parser_parenthesized_expression_list
20221 (parser, attr_flag, /*cast_p=*/false,
20222 /*allow_expansion_p=*/false,
20223 /*non_constant_p=*/NULL);
20224 if (vec == NULL)
20225 arguments = error_mark_node;
20226 else
20227 {
20228 arguments = build_tree_list_vec (vec);
20229 release_tree_vector (vec);
20230 }
20231 /* Save the arguments away. */
20232 TREE_VALUE (attribute) = arguments;
20233 }
20234
20235 if (arguments != error_mark_node)
20236 {
20237 /* Add this attribute to the list. */
20238 TREE_CHAIN (attribute) = attribute_list;
20239 attribute_list = attribute;
20240 }
20241
20242 token = cp_lexer_peek_token (parser->lexer);
20243 }
20244 /* Now, look for more attributes. If the next token isn't a
20245 `,', we're done. */
20246 if (token->type != CPP_COMMA)
20247 break;
20248
20249 /* Consume the comma and keep going. */
20250 cp_lexer_consume_token (parser->lexer);
20251 }
20252 parser->translate_strings_p = save_translate_strings_p;
20253
20254 /* We built up the list in reverse order. */
20255 return nreverse (attribute_list);
20256 }
20257
20258 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20259 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20260 current value of the PEDANTIC flag, regardless of whether or not
20261 the `__extension__' keyword is present. The caller is responsible
20262 for restoring the value of the PEDANTIC flag. */
20263
20264 static bool
20265 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20266 {
20267 /* Save the old value of the PEDANTIC flag. */
20268 *saved_pedantic = pedantic;
20269
20270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20271 {
20272 /* Consume the `__extension__' token. */
20273 cp_lexer_consume_token (parser->lexer);
20274 /* We're not being pedantic while the `__extension__' keyword is
20275 in effect. */
20276 pedantic = 0;
20277
20278 return true;
20279 }
20280
20281 return false;
20282 }
20283
20284 /* Parse a label declaration.
20285
20286 label-declaration:
20287 __label__ label-declarator-seq ;
20288
20289 label-declarator-seq:
20290 identifier , label-declarator-seq
20291 identifier */
20292
20293 static void
20294 cp_parser_label_declaration (cp_parser* parser)
20295 {
20296 /* Look for the `__label__' keyword. */
20297 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20298
20299 while (true)
20300 {
20301 tree identifier;
20302
20303 /* Look for an identifier. */
20304 identifier = cp_parser_identifier (parser);
20305 /* If we failed, stop. */
20306 if (identifier == error_mark_node)
20307 break;
20308 /* Declare it as a label. */
20309 finish_label_decl (identifier);
20310 /* If the next token is a `;', stop. */
20311 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20312 break;
20313 /* Look for the `,' separating the label declarations. */
20314 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20315 }
20316
20317 /* Look for the final `;'. */
20318 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20319 }
20320
20321 /* Support Functions */
20322
20323 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20324 NAME should have one of the representations used for an
20325 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20326 is returned. If PARSER->SCOPE is a dependent type, then a
20327 SCOPE_REF is returned.
20328
20329 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20330 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20331 was formed. Abstractly, such entities should not be passed to this
20332 function, because they do not need to be looked up, but it is
20333 simpler to check for this special case here, rather than at the
20334 call-sites.
20335
20336 In cases not explicitly covered above, this function returns a
20337 DECL, OVERLOAD, or baselink representing the result of the lookup.
20338 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20339 is returned.
20340
20341 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20342 (e.g., "struct") that was used. In that case bindings that do not
20343 refer to types are ignored.
20344
20345 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20346 ignored.
20347
20348 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20349 are ignored.
20350
20351 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20352 types.
20353
20354 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20355 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20356 NULL_TREE otherwise. */
20357
20358 static tree
20359 cp_parser_lookup_name (cp_parser *parser, tree name,
20360 enum tag_types tag_type,
20361 bool is_template,
20362 bool is_namespace,
20363 bool check_dependency,
20364 tree *ambiguous_decls,
20365 location_t name_location)
20366 {
20367 tree decl;
20368 tree object_type = parser->context->object_type;
20369
20370 /* Assume that the lookup will be unambiguous. */
20371 if (ambiguous_decls)
20372 *ambiguous_decls = NULL_TREE;
20373
20374 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20375 no longer valid. Note that if we are parsing tentatively, and
20376 the parse fails, OBJECT_TYPE will be automatically restored. */
20377 parser->context->object_type = NULL_TREE;
20378
20379 if (name == error_mark_node)
20380 return error_mark_node;
20381
20382 /* A template-id has already been resolved; there is no lookup to
20383 do. */
20384 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20385 return name;
20386 if (BASELINK_P (name))
20387 {
20388 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20389 == TEMPLATE_ID_EXPR);
20390 return name;
20391 }
20392
20393 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20394 it should already have been checked to make sure that the name
20395 used matches the type being destroyed. */
20396 if (TREE_CODE (name) == BIT_NOT_EXPR)
20397 {
20398 tree type;
20399
20400 /* Figure out to which type this destructor applies. */
20401 if (parser->scope)
20402 type = parser->scope;
20403 else if (object_type)
20404 type = object_type;
20405 else
20406 type = current_class_type;
20407 /* If that's not a class type, there is no destructor. */
20408 if (!type || !CLASS_TYPE_P (type))
20409 return error_mark_node;
20410 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20411 lazily_declare_fn (sfk_destructor, type);
20412 if (!CLASSTYPE_DESTRUCTORS (type))
20413 return error_mark_node;
20414 /* If it was a class type, return the destructor. */
20415 return CLASSTYPE_DESTRUCTORS (type);
20416 }
20417
20418 /* By this point, the NAME should be an ordinary identifier. If
20419 the id-expression was a qualified name, the qualifying scope is
20420 stored in PARSER->SCOPE at this point. */
20421 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20422
20423 /* Perform the lookup. */
20424 if (parser->scope)
20425 {
20426 bool dependent_p;
20427
20428 if (parser->scope == error_mark_node)
20429 return error_mark_node;
20430
20431 /* If the SCOPE is dependent, the lookup must be deferred until
20432 the template is instantiated -- unless we are explicitly
20433 looking up names in uninstantiated templates. Even then, we
20434 cannot look up the name if the scope is not a class type; it
20435 might, for example, be a template type parameter. */
20436 dependent_p = (TYPE_P (parser->scope)
20437 && dependent_scope_p (parser->scope));
20438 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20439 && dependent_p)
20440 /* Defer lookup. */
20441 decl = error_mark_node;
20442 else
20443 {
20444 tree pushed_scope = NULL_TREE;
20445
20446 /* If PARSER->SCOPE is a dependent type, then it must be a
20447 class type, and we must not be checking dependencies;
20448 otherwise, we would have processed this lookup above. So
20449 that PARSER->SCOPE is not considered a dependent base by
20450 lookup_member, we must enter the scope here. */
20451 if (dependent_p)
20452 pushed_scope = push_scope (parser->scope);
20453
20454 /* If the PARSER->SCOPE is a template specialization, it
20455 may be instantiated during name lookup. In that case,
20456 errors may be issued. Even if we rollback the current
20457 tentative parse, those errors are valid. */
20458 decl = lookup_qualified_name (parser->scope, name,
20459 tag_type != none_type,
20460 /*complain=*/true);
20461
20462 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20463 lookup result and the nested-name-specifier nominates a class C:
20464 * if the name specified after the nested-name-specifier, when
20465 looked up in C, is the injected-class-name of C (Clause 9), or
20466 * if the name specified after the nested-name-specifier is the
20467 same as the identifier or the simple-template-id's template-
20468 name in the last component of the nested-name-specifier,
20469 the name is instead considered to name the constructor of
20470 class C. [ Note: for example, the constructor is not an
20471 acceptable lookup result in an elaborated-type-specifier so
20472 the constructor would not be used in place of the
20473 injected-class-name. --end note ] Such a constructor name
20474 shall be used only in the declarator-id of a declaration that
20475 names a constructor or in a using-declaration. */
20476 if (tag_type == none_type
20477 && DECL_SELF_REFERENCE_P (decl)
20478 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20479 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20480 tag_type != none_type,
20481 /*complain=*/true);
20482
20483 /* If we have a single function from a using decl, pull it out. */
20484 if (TREE_CODE (decl) == OVERLOAD
20485 && !really_overloaded_fn (decl))
20486 decl = OVL_FUNCTION (decl);
20487
20488 if (pushed_scope)
20489 pop_scope (pushed_scope);
20490 }
20491
20492 /* If the scope is a dependent type and either we deferred lookup or
20493 we did lookup but didn't find the name, rememeber the name. */
20494 if (decl == error_mark_node && TYPE_P (parser->scope)
20495 && dependent_type_p (parser->scope))
20496 {
20497 if (tag_type)
20498 {
20499 tree type;
20500
20501 /* The resolution to Core Issue 180 says that `struct
20502 A::B' should be considered a type-name, even if `A'
20503 is dependent. */
20504 type = make_typename_type (parser->scope, name, tag_type,
20505 /*complain=*/tf_error);
20506 decl = TYPE_NAME (type);
20507 }
20508 else if (is_template
20509 && (cp_parser_next_token_ends_template_argument_p (parser)
20510 || cp_lexer_next_token_is (parser->lexer,
20511 CPP_CLOSE_PAREN)))
20512 decl = make_unbound_class_template (parser->scope,
20513 name, NULL_TREE,
20514 /*complain=*/tf_error);
20515 else
20516 decl = build_qualified_name (/*type=*/NULL_TREE,
20517 parser->scope, name,
20518 is_template);
20519 }
20520 parser->qualifying_scope = parser->scope;
20521 parser->object_scope = NULL_TREE;
20522 }
20523 else if (object_type)
20524 {
20525 tree object_decl = NULL_TREE;
20526 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20527 OBJECT_TYPE is not a class. */
20528 if (CLASS_TYPE_P (object_type))
20529 /* If the OBJECT_TYPE is a template specialization, it may
20530 be instantiated during name lookup. In that case, errors
20531 may be issued. Even if we rollback the current tentative
20532 parse, those errors are valid. */
20533 object_decl = lookup_member (object_type,
20534 name,
20535 /*protect=*/0,
20536 tag_type != none_type,
20537 tf_warning_or_error);
20538 /* Look it up in the enclosing context, too. */
20539 decl = lookup_name_real (name, tag_type != none_type,
20540 /*nonclass=*/0,
20541 /*block_p=*/true, is_namespace, 0);
20542 parser->object_scope = object_type;
20543 parser->qualifying_scope = NULL_TREE;
20544 if (object_decl)
20545 decl = object_decl;
20546 }
20547 else
20548 {
20549 decl = lookup_name_real (name, tag_type != none_type,
20550 /*nonclass=*/0,
20551 /*block_p=*/true, is_namespace, 0);
20552 parser->qualifying_scope = NULL_TREE;
20553 parser->object_scope = NULL_TREE;
20554 }
20555
20556 /* If the lookup failed, let our caller know. */
20557 if (!decl || decl == error_mark_node)
20558 return error_mark_node;
20559
20560 /* Pull out the template from an injected-class-name (or multiple). */
20561 if (is_template)
20562 decl = maybe_get_template_decl_from_type_decl (decl);
20563
20564 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20565 if (TREE_CODE (decl) == TREE_LIST)
20566 {
20567 if (ambiguous_decls)
20568 *ambiguous_decls = decl;
20569 /* The error message we have to print is too complicated for
20570 cp_parser_error, so we incorporate its actions directly. */
20571 if (!cp_parser_simulate_error (parser))
20572 {
20573 error_at (name_location, "reference to %qD is ambiguous",
20574 name);
20575 print_candidates (decl);
20576 }
20577 return error_mark_node;
20578 }
20579
20580 gcc_assert (DECL_P (decl)
20581 || TREE_CODE (decl) == OVERLOAD
20582 || TREE_CODE (decl) == SCOPE_REF
20583 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20584 || BASELINK_P (decl));
20585
20586 /* If we have resolved the name of a member declaration, check to
20587 see if the declaration is accessible. When the name resolves to
20588 set of overloaded functions, accessibility is checked when
20589 overload resolution is done.
20590
20591 During an explicit instantiation, access is not checked at all,
20592 as per [temp.explicit]. */
20593 if (DECL_P (decl))
20594 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20595
20596 maybe_record_typedef_use (decl);
20597
20598 return decl;
20599 }
20600
20601 /* Like cp_parser_lookup_name, but for use in the typical case where
20602 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20603 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20604
20605 static tree
20606 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20607 {
20608 return cp_parser_lookup_name (parser, name,
20609 none_type,
20610 /*is_template=*/false,
20611 /*is_namespace=*/false,
20612 /*check_dependency=*/true,
20613 /*ambiguous_decls=*/NULL,
20614 location);
20615 }
20616
20617 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20618 the current context, return the TYPE_DECL. If TAG_NAME_P is
20619 true, the DECL indicates the class being defined in a class-head,
20620 or declared in an elaborated-type-specifier.
20621
20622 Otherwise, return DECL. */
20623
20624 static tree
20625 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20626 {
20627 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20628 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20629
20630 struct A {
20631 template <typename T> struct B;
20632 };
20633
20634 template <typename T> struct A::B {};
20635
20636 Similarly, in an elaborated-type-specifier:
20637
20638 namespace N { struct X{}; }
20639
20640 struct A {
20641 template <typename T> friend struct N::X;
20642 };
20643
20644 However, if the DECL refers to a class type, and we are in
20645 the scope of the class, then the name lookup automatically
20646 finds the TYPE_DECL created by build_self_reference rather
20647 than a TEMPLATE_DECL. For example, in:
20648
20649 template <class T> struct S {
20650 S s;
20651 };
20652
20653 there is no need to handle such case. */
20654
20655 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20656 return DECL_TEMPLATE_RESULT (decl);
20657
20658 return decl;
20659 }
20660
20661 /* If too many, or too few, template-parameter lists apply to the
20662 declarator, issue an error message. Returns TRUE if all went well,
20663 and FALSE otherwise. */
20664
20665 static bool
20666 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20667 cp_declarator *declarator,
20668 location_t declarator_location)
20669 {
20670 switch (declarator->kind)
20671 {
20672 case cdk_id:
20673 {
20674 unsigned num_templates = 0;
20675 tree scope = declarator->u.id.qualifying_scope;
20676
20677 if (scope)
20678 num_templates = num_template_headers_for_class (scope);
20679 else if (TREE_CODE (declarator->u.id.unqualified_name)
20680 == TEMPLATE_ID_EXPR)
20681 /* If the DECLARATOR has the form `X<y>' then it uses one
20682 additional level of template parameters. */
20683 ++num_templates;
20684
20685 return cp_parser_check_template_parameters
20686 (parser, num_templates, declarator_location, declarator);
20687 }
20688
20689 case cdk_function:
20690 case cdk_array:
20691 case cdk_pointer:
20692 case cdk_reference:
20693 case cdk_ptrmem:
20694 return (cp_parser_check_declarator_template_parameters
20695 (parser, declarator->declarator, declarator_location));
20696
20697 case cdk_error:
20698 return true;
20699
20700 default:
20701 gcc_unreachable ();
20702 }
20703 return false;
20704 }
20705
20706 /* NUM_TEMPLATES were used in the current declaration. If that is
20707 invalid, return FALSE and issue an error messages. Otherwise,
20708 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20709 declarator and we can print more accurate diagnostics. */
20710
20711 static bool
20712 cp_parser_check_template_parameters (cp_parser* parser,
20713 unsigned num_templates,
20714 location_t location,
20715 cp_declarator *declarator)
20716 {
20717 /* If there are the same number of template classes and parameter
20718 lists, that's OK. */
20719 if (parser->num_template_parameter_lists == num_templates)
20720 return true;
20721 /* If there are more, but only one more, then we are referring to a
20722 member template. That's OK too. */
20723 if (parser->num_template_parameter_lists == num_templates + 1)
20724 return true;
20725 /* If there are more template classes than parameter lists, we have
20726 something like:
20727
20728 template <class T> void S<T>::R<T>::f (); */
20729 if (parser->num_template_parameter_lists < num_templates)
20730 {
20731 if (declarator && !current_function_decl)
20732 error_at (location, "specializing member %<%T::%E%> "
20733 "requires %<template<>%> syntax",
20734 declarator->u.id.qualifying_scope,
20735 declarator->u.id.unqualified_name);
20736 else if (declarator)
20737 error_at (location, "invalid declaration of %<%T::%E%>",
20738 declarator->u.id.qualifying_scope,
20739 declarator->u.id.unqualified_name);
20740 else
20741 error_at (location, "too few template-parameter-lists");
20742 return false;
20743 }
20744 /* Otherwise, there are too many template parameter lists. We have
20745 something like:
20746
20747 template <class T> template <class U> void S::f(); */
20748 error_at (location, "too many template-parameter-lists");
20749 return false;
20750 }
20751
20752 /* Parse an optional `::' token indicating that the following name is
20753 from the global namespace. If so, PARSER->SCOPE is set to the
20754 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20755 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20756 Returns the new value of PARSER->SCOPE, if the `::' token is
20757 present, and NULL_TREE otherwise. */
20758
20759 static tree
20760 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20761 {
20762 cp_token *token;
20763
20764 /* Peek at the next token. */
20765 token = cp_lexer_peek_token (parser->lexer);
20766 /* If we're looking at a `::' token then we're starting from the
20767 global namespace, not our current location. */
20768 if (token->type == CPP_SCOPE)
20769 {
20770 /* Consume the `::' token. */
20771 cp_lexer_consume_token (parser->lexer);
20772 /* Set the SCOPE so that we know where to start the lookup. */
20773 parser->scope = global_namespace;
20774 parser->qualifying_scope = global_namespace;
20775 parser->object_scope = NULL_TREE;
20776
20777 return parser->scope;
20778 }
20779 else if (!current_scope_valid_p)
20780 {
20781 parser->scope = NULL_TREE;
20782 parser->qualifying_scope = NULL_TREE;
20783 parser->object_scope = NULL_TREE;
20784 }
20785
20786 return NULL_TREE;
20787 }
20788
20789 /* Returns TRUE if the upcoming token sequence is the start of a
20790 constructor declarator. If FRIEND_P is true, the declarator is
20791 preceded by the `friend' specifier. */
20792
20793 static bool
20794 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20795 {
20796 bool constructor_p;
20797 tree nested_name_specifier;
20798 cp_token *next_token;
20799
20800 /* The common case is that this is not a constructor declarator, so
20801 try to avoid doing lots of work if at all possible. It's not
20802 valid declare a constructor at function scope. */
20803 if (parser->in_function_body)
20804 return false;
20805 /* And only certain tokens can begin a constructor declarator. */
20806 next_token = cp_lexer_peek_token (parser->lexer);
20807 if (next_token->type != CPP_NAME
20808 && next_token->type != CPP_SCOPE
20809 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20810 && next_token->type != CPP_TEMPLATE_ID)
20811 return false;
20812
20813 /* Parse tentatively; we are going to roll back all of the tokens
20814 consumed here. */
20815 cp_parser_parse_tentatively (parser);
20816 /* Assume that we are looking at a constructor declarator. */
20817 constructor_p = true;
20818
20819 /* Look for the optional `::' operator. */
20820 cp_parser_global_scope_opt (parser,
20821 /*current_scope_valid_p=*/false);
20822 /* Look for the nested-name-specifier. */
20823 nested_name_specifier
20824 = (cp_parser_nested_name_specifier_opt (parser,
20825 /*typename_keyword_p=*/false,
20826 /*check_dependency_p=*/false,
20827 /*type_p=*/false,
20828 /*is_declaration=*/false));
20829 /* Outside of a class-specifier, there must be a
20830 nested-name-specifier. */
20831 if (!nested_name_specifier &&
20832 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20833 || friend_p))
20834 constructor_p = false;
20835 else if (nested_name_specifier == error_mark_node)
20836 constructor_p = false;
20837
20838 /* If we have a class scope, this is easy; DR 147 says that S::S always
20839 names the constructor, and no other qualified name could. */
20840 if (constructor_p && nested_name_specifier
20841 && CLASS_TYPE_P (nested_name_specifier))
20842 {
20843 tree id = cp_parser_unqualified_id (parser,
20844 /*template_keyword_p=*/false,
20845 /*check_dependency_p=*/false,
20846 /*declarator_p=*/true,
20847 /*optional_p=*/false);
20848 if (is_overloaded_fn (id))
20849 id = DECL_NAME (get_first_fn (id));
20850 if (!constructor_name_p (id, nested_name_specifier))
20851 constructor_p = false;
20852 }
20853 /* If we still think that this might be a constructor-declarator,
20854 look for a class-name. */
20855 else if (constructor_p)
20856 {
20857 /* If we have:
20858
20859 template <typename T> struct S {
20860 S();
20861 };
20862
20863 we must recognize that the nested `S' names a class. */
20864 tree type_decl;
20865 type_decl = cp_parser_class_name (parser,
20866 /*typename_keyword_p=*/false,
20867 /*template_keyword_p=*/false,
20868 none_type,
20869 /*check_dependency_p=*/false,
20870 /*class_head_p=*/false,
20871 /*is_declaration=*/false);
20872 /* If there was no class-name, then this is not a constructor. */
20873 constructor_p = !cp_parser_error_occurred (parser);
20874
20875 /* If we're still considering a constructor, we have to see a `(',
20876 to begin the parameter-declaration-clause, followed by either a
20877 `)', an `...', or a decl-specifier. We need to check for a
20878 type-specifier to avoid being fooled into thinking that:
20879
20880 S (f) (int);
20881
20882 is a constructor. (It is actually a function named `f' that
20883 takes one parameter (of type `int') and returns a value of type
20884 `S'. */
20885 if (constructor_p
20886 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20887 constructor_p = false;
20888
20889 if (constructor_p
20890 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20891 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20892 /* A parameter declaration begins with a decl-specifier,
20893 which is either the "attribute" keyword, a storage class
20894 specifier, or (usually) a type-specifier. */
20895 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20896 {
20897 tree type;
20898 tree pushed_scope = NULL_TREE;
20899 unsigned saved_num_template_parameter_lists;
20900
20901 /* Names appearing in the type-specifier should be looked up
20902 in the scope of the class. */
20903 if (current_class_type)
20904 type = NULL_TREE;
20905 else
20906 {
20907 type = TREE_TYPE (type_decl);
20908 if (TREE_CODE (type) == TYPENAME_TYPE)
20909 {
20910 type = resolve_typename_type (type,
20911 /*only_current_p=*/false);
20912 if (TREE_CODE (type) == TYPENAME_TYPE)
20913 {
20914 cp_parser_abort_tentative_parse (parser);
20915 return false;
20916 }
20917 }
20918 pushed_scope = push_scope (type);
20919 }
20920
20921 /* Inside the constructor parameter list, surrounding
20922 template-parameter-lists do not apply. */
20923 saved_num_template_parameter_lists
20924 = parser->num_template_parameter_lists;
20925 parser->num_template_parameter_lists = 0;
20926
20927 /* Look for the type-specifier. */
20928 cp_parser_type_specifier (parser,
20929 CP_PARSER_FLAGS_NONE,
20930 /*decl_specs=*/NULL,
20931 /*is_declarator=*/true,
20932 /*declares_class_or_enum=*/NULL,
20933 /*is_cv_qualifier=*/NULL);
20934
20935 parser->num_template_parameter_lists
20936 = saved_num_template_parameter_lists;
20937
20938 /* Leave the scope of the class. */
20939 if (pushed_scope)
20940 pop_scope (pushed_scope);
20941
20942 constructor_p = !cp_parser_error_occurred (parser);
20943 }
20944 }
20945
20946 /* We did not really want to consume any tokens. */
20947 cp_parser_abort_tentative_parse (parser);
20948
20949 return constructor_p;
20950 }
20951
20952 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20953 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20954 they must be performed once we are in the scope of the function.
20955
20956 Returns the function defined. */
20957
20958 static tree
20959 cp_parser_function_definition_from_specifiers_and_declarator
20960 (cp_parser* parser,
20961 cp_decl_specifier_seq *decl_specifiers,
20962 tree attributes,
20963 const cp_declarator *declarator)
20964 {
20965 tree fn;
20966 bool success_p;
20967
20968 /* Begin the function-definition. */
20969 success_p = start_function (decl_specifiers, declarator, attributes);
20970
20971 /* The things we're about to see are not directly qualified by any
20972 template headers we've seen thus far. */
20973 reset_specialization ();
20974
20975 /* If there were names looked up in the decl-specifier-seq that we
20976 did not check, check them now. We must wait until we are in the
20977 scope of the function to perform the checks, since the function
20978 might be a friend. */
20979 perform_deferred_access_checks (tf_warning_or_error);
20980
20981 if (!success_p)
20982 {
20983 /* Skip the entire function. */
20984 cp_parser_skip_to_end_of_block_or_statement (parser);
20985 fn = error_mark_node;
20986 }
20987 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20988 {
20989 /* Seen already, skip it. An error message has already been output. */
20990 cp_parser_skip_to_end_of_block_or_statement (parser);
20991 fn = current_function_decl;
20992 current_function_decl = NULL_TREE;
20993 /* If this is a function from a class, pop the nested class. */
20994 if (current_class_name)
20995 pop_nested_class ();
20996 }
20997 else
20998 {
20999 timevar_id_t tv;
21000 if (DECL_DECLARED_INLINE_P (current_function_decl))
21001 tv = TV_PARSE_INLINE;
21002 else
21003 tv = TV_PARSE_FUNC;
21004 timevar_push (tv);
21005 fn = cp_parser_function_definition_after_declarator (parser,
21006 /*inline_p=*/false);
21007 timevar_pop (tv);
21008 }
21009
21010 return fn;
21011 }
21012
21013 /* Parse the part of a function-definition that follows the
21014 declarator. INLINE_P is TRUE iff this function is an inline
21015 function defined within a class-specifier.
21016
21017 Returns the function defined. */
21018
21019 static tree
21020 cp_parser_function_definition_after_declarator (cp_parser* parser,
21021 bool inline_p)
21022 {
21023 tree fn;
21024 bool ctor_initializer_p = false;
21025 bool saved_in_unbraced_linkage_specification_p;
21026 bool saved_in_function_body;
21027 unsigned saved_num_template_parameter_lists;
21028 cp_token *token;
21029
21030 saved_in_function_body = parser->in_function_body;
21031 parser->in_function_body = true;
21032 /* If the next token is `return', then the code may be trying to
21033 make use of the "named return value" extension that G++ used to
21034 support. */
21035 token = cp_lexer_peek_token (parser->lexer);
21036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21037 {
21038 /* Consume the `return' keyword. */
21039 cp_lexer_consume_token (parser->lexer);
21040 /* Look for the identifier that indicates what value is to be
21041 returned. */
21042 cp_parser_identifier (parser);
21043 /* Issue an error message. */
21044 error_at (token->location,
21045 "named return values are no longer supported");
21046 /* Skip tokens until we reach the start of the function body. */
21047 while (true)
21048 {
21049 cp_token *token = cp_lexer_peek_token (parser->lexer);
21050 if (token->type == CPP_OPEN_BRACE
21051 || token->type == CPP_EOF
21052 || token->type == CPP_PRAGMA_EOL)
21053 break;
21054 cp_lexer_consume_token (parser->lexer);
21055 }
21056 }
21057 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21058 anything declared inside `f'. */
21059 saved_in_unbraced_linkage_specification_p
21060 = parser->in_unbraced_linkage_specification_p;
21061 parser->in_unbraced_linkage_specification_p = false;
21062 /* Inside the function, surrounding template-parameter-lists do not
21063 apply. */
21064 saved_num_template_parameter_lists
21065 = parser->num_template_parameter_lists;
21066 parser->num_template_parameter_lists = 0;
21067
21068 start_lambda_scope (current_function_decl);
21069
21070 /* If the next token is `try', `__transaction_atomic', or
21071 `__transaction_relaxed`, then we are looking at either function-try-block
21072 or function-transaction-block. Note that all of these include the
21073 function-body. */
21074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21075 ctor_initializer_p = cp_parser_function_transaction (parser,
21076 RID_TRANSACTION_ATOMIC);
21077 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21078 RID_TRANSACTION_RELAXED))
21079 ctor_initializer_p = cp_parser_function_transaction (parser,
21080 RID_TRANSACTION_RELAXED);
21081 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21082 ctor_initializer_p = cp_parser_function_try_block (parser);
21083 else
21084 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21085 (parser, /*in_function_try_block=*/false);
21086
21087 finish_lambda_scope ();
21088
21089 /* Finish the function. */
21090 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21091 (inline_p ? 2 : 0));
21092 /* Generate code for it, if necessary. */
21093 expand_or_defer_fn (fn);
21094 /* Restore the saved values. */
21095 parser->in_unbraced_linkage_specification_p
21096 = saved_in_unbraced_linkage_specification_p;
21097 parser->num_template_parameter_lists
21098 = saved_num_template_parameter_lists;
21099 parser->in_function_body = saved_in_function_body;
21100
21101 return fn;
21102 }
21103
21104 /* Parse a template-declaration, assuming that the `export' (and
21105 `extern') keywords, if present, has already been scanned. MEMBER_P
21106 is as for cp_parser_template_declaration. */
21107
21108 static void
21109 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21110 {
21111 tree decl = NULL_TREE;
21112 VEC (deferred_access_check,gc) *checks;
21113 tree parameter_list;
21114 bool friend_p = false;
21115 bool need_lang_pop;
21116 cp_token *token;
21117
21118 /* Look for the `template' keyword. */
21119 token = cp_lexer_peek_token (parser->lexer);
21120 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21121 return;
21122
21123 /* And the `<'. */
21124 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21125 return;
21126 if (at_class_scope_p () && current_function_decl)
21127 {
21128 /* 14.5.2.2 [temp.mem]
21129
21130 A local class shall not have member templates. */
21131 error_at (token->location,
21132 "invalid declaration of member template in local class");
21133 cp_parser_skip_to_end_of_block_or_statement (parser);
21134 return;
21135 }
21136 /* [temp]
21137
21138 A template ... shall not have C linkage. */
21139 if (current_lang_name == lang_name_c)
21140 {
21141 error_at (token->location, "template with C linkage");
21142 /* Give it C++ linkage to avoid confusing other parts of the
21143 front end. */
21144 push_lang_context (lang_name_cplusplus);
21145 need_lang_pop = true;
21146 }
21147 else
21148 need_lang_pop = false;
21149
21150 /* We cannot perform access checks on the template parameter
21151 declarations until we know what is being declared, just as we
21152 cannot check the decl-specifier list. */
21153 push_deferring_access_checks (dk_deferred);
21154
21155 /* If the next token is `>', then we have an invalid
21156 specialization. Rather than complain about an invalid template
21157 parameter, issue an error message here. */
21158 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21159 {
21160 cp_parser_error (parser, "invalid explicit specialization");
21161 begin_specialization ();
21162 parameter_list = NULL_TREE;
21163 }
21164 else
21165 {
21166 /* Parse the template parameters. */
21167 parameter_list = cp_parser_template_parameter_list (parser);
21168 }
21169
21170 /* Get the deferred access checks from the parameter list. These
21171 will be checked once we know what is being declared, as for a
21172 member template the checks must be performed in the scope of the
21173 class containing the member. */
21174 checks = get_deferred_access_checks ();
21175
21176 /* Look for the `>'. */
21177 cp_parser_skip_to_end_of_template_parameter_list (parser);
21178 /* We just processed one more parameter list. */
21179 ++parser->num_template_parameter_lists;
21180 /* If the next token is `template', there are more template
21181 parameters. */
21182 if (cp_lexer_next_token_is_keyword (parser->lexer,
21183 RID_TEMPLATE))
21184 cp_parser_template_declaration_after_export (parser, member_p);
21185 else if (cxx_dialect >= cxx0x
21186 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21187 decl = cp_parser_alias_declaration (parser);
21188 else
21189 {
21190 /* There are no access checks when parsing a template, as we do not
21191 know if a specialization will be a friend. */
21192 push_deferring_access_checks (dk_no_check);
21193 token = cp_lexer_peek_token (parser->lexer);
21194 decl = cp_parser_single_declaration (parser,
21195 checks,
21196 member_p,
21197 /*explicit_specialization_p=*/false,
21198 &friend_p);
21199 pop_deferring_access_checks ();
21200
21201 /* If this is a member template declaration, let the front
21202 end know. */
21203 if (member_p && !friend_p && decl)
21204 {
21205 if (TREE_CODE (decl) == TYPE_DECL)
21206 cp_parser_check_access_in_redeclaration (decl, token->location);
21207
21208 decl = finish_member_template_decl (decl);
21209 }
21210 else if (friend_p && decl
21211 && (TREE_CODE (decl) == TYPE_DECL
21212 || DECL_TYPE_TEMPLATE_P (decl)))
21213 make_friend_class (current_class_type, TREE_TYPE (decl),
21214 /*complain=*/true);
21215 }
21216 /* We are done with the current parameter list. */
21217 --parser->num_template_parameter_lists;
21218
21219 pop_deferring_access_checks ();
21220
21221 /* Finish up. */
21222 finish_template_decl (parameter_list);
21223
21224 /* Check the template arguments for a literal operator template. */
21225 if (decl
21226 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21227 && UDLIT_OPER_P (DECL_NAME (decl)))
21228 {
21229 bool ok = true;
21230 if (parameter_list == NULL_TREE)
21231 ok = false;
21232 else
21233 {
21234 int num_parms = TREE_VEC_LENGTH (parameter_list);
21235 if (num_parms != 1)
21236 ok = false;
21237 else
21238 {
21239 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21240 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21241 if (TREE_TYPE (parm) != char_type_node
21242 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21243 ok = false;
21244 }
21245 }
21246 if (!ok)
21247 error ("literal operator template %qD has invalid parameter list."
21248 " Expected non-type template argument pack <char...>",
21249 decl);
21250 }
21251 /* Register member declarations. */
21252 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21253 finish_member_declaration (decl);
21254 /* For the erroneous case of a template with C linkage, we pushed an
21255 implicit C++ linkage scope; exit that scope now. */
21256 if (need_lang_pop)
21257 pop_lang_context ();
21258 /* If DECL is a function template, we must return to parse it later.
21259 (Even though there is no definition, there might be default
21260 arguments that need handling.) */
21261 if (member_p && decl
21262 && (TREE_CODE (decl) == FUNCTION_DECL
21263 || DECL_FUNCTION_TEMPLATE_P (decl)))
21264 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21265 }
21266
21267 /* Perform the deferred access checks from a template-parameter-list.
21268 CHECKS is a TREE_LIST of access checks, as returned by
21269 get_deferred_access_checks. */
21270
21271 static void
21272 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21273 {
21274 ++processing_template_parmlist;
21275 perform_access_checks (checks, tf_warning_or_error);
21276 --processing_template_parmlist;
21277 }
21278
21279 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21280 `function-definition' sequence that follows a template header.
21281 If MEMBER_P is true, this declaration appears in a class scope.
21282
21283 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21284 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21285
21286 static tree
21287 cp_parser_single_declaration (cp_parser* parser,
21288 VEC (deferred_access_check,gc)* checks,
21289 bool member_p,
21290 bool explicit_specialization_p,
21291 bool* friend_p)
21292 {
21293 int declares_class_or_enum;
21294 tree decl = NULL_TREE;
21295 cp_decl_specifier_seq decl_specifiers;
21296 bool function_definition_p = false;
21297 cp_token *decl_spec_token_start;
21298
21299 /* This function is only used when processing a template
21300 declaration. */
21301 gcc_assert (innermost_scope_kind () == sk_template_parms
21302 || innermost_scope_kind () == sk_template_spec);
21303
21304 /* Defer access checks until we know what is being declared. */
21305 push_deferring_access_checks (dk_deferred);
21306
21307 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21308 alternative. */
21309 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21310 cp_parser_decl_specifier_seq (parser,
21311 CP_PARSER_FLAGS_OPTIONAL,
21312 &decl_specifiers,
21313 &declares_class_or_enum);
21314 if (friend_p)
21315 *friend_p = cp_parser_friend_p (&decl_specifiers);
21316
21317 /* There are no template typedefs. */
21318 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21319 {
21320 error_at (decl_spec_token_start->location,
21321 "template declaration of %<typedef%>");
21322 decl = error_mark_node;
21323 }
21324
21325 /* Gather up the access checks that occurred the
21326 decl-specifier-seq. */
21327 stop_deferring_access_checks ();
21328
21329 /* Check for the declaration of a template class. */
21330 if (declares_class_or_enum)
21331 {
21332 if (cp_parser_declares_only_class_p (parser))
21333 {
21334 decl = shadow_tag (&decl_specifiers);
21335
21336 /* In this case:
21337
21338 struct C {
21339 friend template <typename T> struct A<T>::B;
21340 };
21341
21342 A<T>::B will be represented by a TYPENAME_TYPE, and
21343 therefore not recognized by shadow_tag. */
21344 if (friend_p && *friend_p
21345 && !decl
21346 && decl_specifiers.type
21347 && TYPE_P (decl_specifiers.type))
21348 decl = decl_specifiers.type;
21349
21350 if (decl && decl != error_mark_node)
21351 decl = TYPE_NAME (decl);
21352 else
21353 decl = error_mark_node;
21354
21355 /* Perform access checks for template parameters. */
21356 cp_parser_perform_template_parameter_access_checks (checks);
21357 }
21358 }
21359
21360 /* Complain about missing 'typename' or other invalid type names. */
21361 if (!decl_specifiers.any_type_specifiers_p
21362 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21363 {
21364 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21365 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21366 the rest of this declaration. */
21367 decl = error_mark_node;
21368 goto out;
21369 }
21370
21371 /* If it's not a template class, try for a template function. If
21372 the next token is a `;', then this declaration does not declare
21373 anything. But, if there were errors in the decl-specifiers, then
21374 the error might well have come from an attempted class-specifier.
21375 In that case, there's no need to warn about a missing declarator. */
21376 if (!decl
21377 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21378 || decl_specifiers.type != error_mark_node))
21379 {
21380 decl = cp_parser_init_declarator (parser,
21381 &decl_specifiers,
21382 checks,
21383 /*function_definition_allowed_p=*/true,
21384 member_p,
21385 declares_class_or_enum,
21386 &function_definition_p,
21387 NULL);
21388
21389 /* 7.1.1-1 [dcl.stc]
21390
21391 A storage-class-specifier shall not be specified in an explicit
21392 specialization... */
21393 if (decl
21394 && explicit_specialization_p
21395 && decl_specifiers.storage_class != sc_none)
21396 {
21397 error_at (decl_spec_token_start->location,
21398 "explicit template specialization cannot have a storage class");
21399 decl = error_mark_node;
21400 }
21401
21402 if (decl && TREE_CODE (decl) == VAR_DECL)
21403 check_template_variable (decl);
21404 }
21405
21406 /* Look for a trailing `;' after the declaration. */
21407 if (!function_definition_p
21408 && (decl == error_mark_node
21409 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21410 cp_parser_skip_to_end_of_block_or_statement (parser);
21411
21412 out:
21413 pop_deferring_access_checks ();
21414
21415 /* Clear any current qualification; whatever comes next is the start
21416 of something new. */
21417 parser->scope = NULL_TREE;
21418 parser->qualifying_scope = NULL_TREE;
21419 parser->object_scope = NULL_TREE;
21420
21421 return decl;
21422 }
21423
21424 /* Parse a cast-expression that is not the operand of a unary "&". */
21425
21426 static tree
21427 cp_parser_simple_cast_expression (cp_parser *parser)
21428 {
21429 return cp_parser_cast_expression (parser, /*address_p=*/false,
21430 /*cast_p=*/false, NULL);
21431 }
21432
21433 /* Parse a functional cast to TYPE. Returns an expression
21434 representing the cast. */
21435
21436 static tree
21437 cp_parser_functional_cast (cp_parser* parser, tree type)
21438 {
21439 VEC(tree,gc) *vec;
21440 tree expression_list;
21441 tree cast;
21442 bool nonconst_p;
21443
21444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21445 {
21446 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21447 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21448 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21449 if (TREE_CODE (type) == TYPE_DECL)
21450 type = TREE_TYPE (type);
21451 return finish_compound_literal (type, expression_list,
21452 tf_warning_or_error);
21453 }
21454
21455
21456 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21457 /*cast_p=*/true,
21458 /*allow_expansion_p=*/true,
21459 /*non_constant_p=*/NULL);
21460 if (vec == NULL)
21461 expression_list = error_mark_node;
21462 else
21463 {
21464 expression_list = build_tree_list_vec (vec);
21465 release_tree_vector (vec);
21466 }
21467
21468 cast = build_functional_cast (type, expression_list,
21469 tf_warning_or_error);
21470 /* [expr.const]/1: In an integral constant expression "only type
21471 conversions to integral or enumeration type can be used". */
21472 if (TREE_CODE (type) == TYPE_DECL)
21473 type = TREE_TYPE (type);
21474 if (cast != error_mark_node
21475 && !cast_valid_in_integral_constant_expression_p (type)
21476 && cp_parser_non_integral_constant_expression (parser,
21477 NIC_CONSTRUCTOR))
21478 return error_mark_node;
21479 return cast;
21480 }
21481
21482 /* Save the tokens that make up the body of a member function defined
21483 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21484 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21485 specifiers applied to the declaration. Returns the FUNCTION_DECL
21486 for the member function. */
21487
21488 static tree
21489 cp_parser_save_member_function_body (cp_parser* parser,
21490 cp_decl_specifier_seq *decl_specifiers,
21491 cp_declarator *declarator,
21492 tree attributes)
21493 {
21494 cp_token *first;
21495 cp_token *last;
21496 tree fn;
21497
21498 /* Create the FUNCTION_DECL. */
21499 fn = grokmethod (decl_specifiers, declarator, attributes);
21500 /* If something went badly wrong, bail out now. */
21501 if (fn == error_mark_node)
21502 {
21503 /* If there's a function-body, skip it. */
21504 if (cp_parser_token_starts_function_definition_p
21505 (cp_lexer_peek_token (parser->lexer)))
21506 cp_parser_skip_to_end_of_block_or_statement (parser);
21507 return error_mark_node;
21508 }
21509
21510 /* Remember it, if there default args to post process. */
21511 cp_parser_save_default_args (parser, fn);
21512
21513 /* Save away the tokens that make up the body of the
21514 function. */
21515 first = parser->lexer->next_token;
21516 /* We can have braced-init-list mem-initializers before the fn body. */
21517 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21518 {
21519 cp_lexer_consume_token (parser->lexer);
21520 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21521 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21522 {
21523 /* cache_group will stop after an un-nested { } pair, too. */
21524 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21525 break;
21526
21527 /* variadic mem-inits have ... after the ')'. */
21528 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21529 cp_lexer_consume_token (parser->lexer);
21530 }
21531 }
21532 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21533 /* Handle function try blocks. */
21534 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21535 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21536 last = parser->lexer->next_token;
21537
21538 /* Save away the inline definition; we will process it when the
21539 class is complete. */
21540 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21541 DECL_PENDING_INLINE_P (fn) = 1;
21542
21543 /* We need to know that this was defined in the class, so that
21544 friend templates are handled correctly. */
21545 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21546
21547 /* Add FN to the queue of functions to be parsed later. */
21548 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21549
21550 return fn;
21551 }
21552
21553 /* Save the tokens that make up the in-class initializer for a non-static
21554 data member. Returns a DEFAULT_ARG. */
21555
21556 static tree
21557 cp_parser_save_nsdmi (cp_parser* parser)
21558 {
21559 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21560 }
21561
21562 /* Parse a template-argument-list, as well as the trailing ">" (but
21563 not the opening "<"). See cp_parser_template_argument_list for the
21564 return value. */
21565
21566 static tree
21567 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21568 {
21569 tree arguments;
21570 tree saved_scope;
21571 tree saved_qualifying_scope;
21572 tree saved_object_scope;
21573 bool saved_greater_than_is_operator_p;
21574 int saved_unevaluated_operand;
21575 int saved_inhibit_evaluation_warnings;
21576
21577 /* [temp.names]
21578
21579 When parsing a template-id, the first non-nested `>' is taken as
21580 the end of the template-argument-list rather than a greater-than
21581 operator. */
21582 saved_greater_than_is_operator_p
21583 = parser->greater_than_is_operator_p;
21584 parser->greater_than_is_operator_p = false;
21585 /* Parsing the argument list may modify SCOPE, so we save it
21586 here. */
21587 saved_scope = parser->scope;
21588 saved_qualifying_scope = parser->qualifying_scope;
21589 saved_object_scope = parser->object_scope;
21590 /* We need to evaluate the template arguments, even though this
21591 template-id may be nested within a "sizeof". */
21592 saved_unevaluated_operand = cp_unevaluated_operand;
21593 cp_unevaluated_operand = 0;
21594 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21595 c_inhibit_evaluation_warnings = 0;
21596 /* Parse the template-argument-list itself. */
21597 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21598 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21599 arguments = NULL_TREE;
21600 else
21601 arguments = cp_parser_template_argument_list (parser);
21602 /* Look for the `>' that ends the template-argument-list. If we find
21603 a '>>' instead, it's probably just a typo. */
21604 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21605 {
21606 if (cxx_dialect != cxx98)
21607 {
21608 /* In C++0x, a `>>' in a template argument list or cast
21609 expression is considered to be two separate `>'
21610 tokens. So, change the current token to a `>', but don't
21611 consume it: it will be consumed later when the outer
21612 template argument list (or cast expression) is parsed.
21613 Note that this replacement of `>' for `>>' is necessary
21614 even if we are parsing tentatively: in the tentative
21615 case, after calling
21616 cp_parser_enclosed_template_argument_list we will always
21617 throw away all of the template arguments and the first
21618 closing `>', either because the template argument list
21619 was erroneous or because we are replacing those tokens
21620 with a CPP_TEMPLATE_ID token. The second `>' (which will
21621 not have been thrown away) is needed either to close an
21622 outer template argument list or to complete a new-style
21623 cast. */
21624 cp_token *token = cp_lexer_peek_token (parser->lexer);
21625 token->type = CPP_GREATER;
21626 }
21627 else if (!saved_greater_than_is_operator_p)
21628 {
21629 /* If we're in a nested template argument list, the '>>' has
21630 to be a typo for '> >'. We emit the error message, but we
21631 continue parsing and we push a '>' as next token, so that
21632 the argument list will be parsed correctly. Note that the
21633 global source location is still on the token before the
21634 '>>', so we need to say explicitly where we want it. */
21635 cp_token *token = cp_lexer_peek_token (parser->lexer);
21636 error_at (token->location, "%<>>%> should be %<> >%> "
21637 "within a nested template argument list");
21638
21639 token->type = CPP_GREATER;
21640 }
21641 else
21642 {
21643 /* If this is not a nested template argument list, the '>>'
21644 is a typo for '>'. Emit an error message and continue.
21645 Same deal about the token location, but here we can get it
21646 right by consuming the '>>' before issuing the diagnostic. */
21647 cp_token *token = cp_lexer_consume_token (parser->lexer);
21648 error_at (token->location,
21649 "spurious %<>>%>, use %<>%> to terminate "
21650 "a template argument list");
21651 }
21652 }
21653 else
21654 cp_parser_skip_to_end_of_template_parameter_list (parser);
21655 /* The `>' token might be a greater-than operator again now. */
21656 parser->greater_than_is_operator_p
21657 = saved_greater_than_is_operator_p;
21658 /* Restore the SAVED_SCOPE. */
21659 parser->scope = saved_scope;
21660 parser->qualifying_scope = saved_qualifying_scope;
21661 parser->object_scope = saved_object_scope;
21662 cp_unevaluated_operand = saved_unevaluated_operand;
21663 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21664
21665 return arguments;
21666 }
21667
21668 /* MEMBER_FUNCTION is a member function, or a friend. If default
21669 arguments, or the body of the function have not yet been parsed,
21670 parse them now. */
21671
21672 static void
21673 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21674 {
21675 timevar_push (TV_PARSE_INMETH);
21676 /* If this member is a template, get the underlying
21677 FUNCTION_DECL. */
21678 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21679 member_function = DECL_TEMPLATE_RESULT (member_function);
21680
21681 /* There should not be any class definitions in progress at this
21682 point; the bodies of members are only parsed outside of all class
21683 definitions. */
21684 gcc_assert (parser->num_classes_being_defined == 0);
21685 /* While we're parsing the member functions we might encounter more
21686 classes. We want to handle them right away, but we don't want
21687 them getting mixed up with functions that are currently in the
21688 queue. */
21689 push_unparsed_function_queues (parser);
21690
21691 /* Make sure that any template parameters are in scope. */
21692 maybe_begin_member_template_processing (member_function);
21693
21694 /* If the body of the function has not yet been parsed, parse it
21695 now. */
21696 if (DECL_PENDING_INLINE_P (member_function))
21697 {
21698 tree function_scope;
21699 cp_token_cache *tokens;
21700
21701 /* The function is no longer pending; we are processing it. */
21702 tokens = DECL_PENDING_INLINE_INFO (member_function);
21703 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21704 DECL_PENDING_INLINE_P (member_function) = 0;
21705
21706 /* If this is a local class, enter the scope of the containing
21707 function. */
21708 function_scope = current_function_decl;
21709 if (function_scope)
21710 push_function_context ();
21711
21712 /* Push the body of the function onto the lexer stack. */
21713 cp_parser_push_lexer_for_tokens (parser, tokens);
21714
21715 /* Let the front end know that we going to be defining this
21716 function. */
21717 start_preparsed_function (member_function, NULL_TREE,
21718 SF_PRE_PARSED | SF_INCLASS_INLINE);
21719
21720 /* Don't do access checking if it is a templated function. */
21721 if (processing_template_decl)
21722 push_deferring_access_checks (dk_no_check);
21723
21724 /* Now, parse the body of the function. */
21725 cp_parser_function_definition_after_declarator (parser,
21726 /*inline_p=*/true);
21727
21728 if (processing_template_decl)
21729 pop_deferring_access_checks ();
21730
21731 /* Leave the scope of the containing function. */
21732 if (function_scope)
21733 pop_function_context ();
21734 cp_parser_pop_lexer (parser);
21735 }
21736
21737 /* Remove any template parameters from the symbol table. */
21738 maybe_end_member_template_processing ();
21739
21740 /* Restore the queue. */
21741 pop_unparsed_function_queues (parser);
21742 timevar_pop (TV_PARSE_INMETH);
21743 }
21744
21745 /* If DECL contains any default args, remember it on the unparsed
21746 functions queue. */
21747
21748 static void
21749 cp_parser_save_default_args (cp_parser* parser, tree decl)
21750 {
21751 tree probe;
21752
21753 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21754 probe;
21755 probe = TREE_CHAIN (probe))
21756 if (TREE_PURPOSE (probe))
21757 {
21758 cp_default_arg_entry entry = {current_class_type, decl};
21759 VEC_safe_push (cp_default_arg_entry, gc,
21760 unparsed_funs_with_default_args, entry);
21761 break;
21762 }
21763 }
21764
21765 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21766 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21767 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21768 from the parameter-type-list. */
21769
21770 static tree
21771 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21772 tree default_arg, tree parmtype)
21773 {
21774 cp_token_cache *tokens;
21775 tree parsed_arg;
21776 bool dummy;
21777
21778 if (default_arg == error_mark_node)
21779 return error_mark_node;
21780
21781 /* Push the saved tokens for the default argument onto the parser's
21782 lexer stack. */
21783 tokens = DEFARG_TOKENS (default_arg);
21784 cp_parser_push_lexer_for_tokens (parser, tokens);
21785
21786 start_lambda_scope (decl);
21787
21788 /* Parse the default argument. */
21789 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21790 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21791 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21792
21793 finish_lambda_scope ();
21794
21795 if (parsed_arg == error_mark_node)
21796 cp_parser_skip_to_end_of_statement (parser);
21797
21798 if (!processing_template_decl)
21799 {
21800 /* In a non-template class, check conversions now. In a template,
21801 we'll wait and instantiate these as needed. */
21802 if (TREE_CODE (decl) == PARM_DECL)
21803 parsed_arg = check_default_argument (parmtype, parsed_arg);
21804 else
21805 {
21806 int flags = LOOKUP_IMPLICIT;
21807 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21808 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21809 flags = LOOKUP_NORMAL;
21810 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21811 }
21812 }
21813
21814 /* If the token stream has not been completely used up, then
21815 there was extra junk after the end of the default
21816 argument. */
21817 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21818 {
21819 if (TREE_CODE (decl) == PARM_DECL)
21820 cp_parser_error (parser, "expected %<,%>");
21821 else
21822 cp_parser_error (parser, "expected %<;%>");
21823 }
21824
21825 /* Revert to the main lexer. */
21826 cp_parser_pop_lexer (parser);
21827
21828 return parsed_arg;
21829 }
21830
21831 /* FIELD is a non-static data member with an initializer which we saved for
21832 later; parse it now. */
21833
21834 static void
21835 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21836 {
21837 tree def;
21838
21839 push_unparsed_function_queues (parser);
21840 def = cp_parser_late_parse_one_default_arg (parser, field,
21841 DECL_INITIAL (field),
21842 NULL_TREE);
21843 pop_unparsed_function_queues (parser);
21844
21845 DECL_INITIAL (field) = def;
21846 }
21847
21848 /* FN is a FUNCTION_DECL which may contains a parameter with an
21849 unparsed DEFAULT_ARG. Parse the default args now. This function
21850 assumes that the current scope is the scope in which the default
21851 argument should be processed. */
21852
21853 static void
21854 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21855 {
21856 bool saved_local_variables_forbidden_p;
21857 tree parm, parmdecl;
21858
21859 /* While we're parsing the default args, we might (due to the
21860 statement expression extension) encounter more classes. We want
21861 to handle them right away, but we don't want them getting mixed
21862 up with default args that are currently in the queue. */
21863 push_unparsed_function_queues (parser);
21864
21865 /* Local variable names (and the `this' keyword) may not appear
21866 in a default argument. */
21867 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21868 parser->local_variables_forbidden_p = true;
21869
21870 push_defarg_context (fn);
21871
21872 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21873 parmdecl = DECL_ARGUMENTS (fn);
21874 parm && parm != void_list_node;
21875 parm = TREE_CHAIN (parm),
21876 parmdecl = DECL_CHAIN (parmdecl))
21877 {
21878 tree default_arg = TREE_PURPOSE (parm);
21879 tree parsed_arg;
21880 VEC(tree,gc) *insts;
21881 tree copy;
21882 unsigned ix;
21883
21884 if (!default_arg)
21885 continue;
21886
21887 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21888 /* This can happen for a friend declaration for a function
21889 already declared with default arguments. */
21890 continue;
21891
21892 parsed_arg
21893 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21894 default_arg,
21895 TREE_VALUE (parm));
21896 if (parsed_arg == error_mark_node)
21897 {
21898 continue;
21899 }
21900
21901 TREE_PURPOSE (parm) = parsed_arg;
21902
21903 /* Update any instantiations we've already created. */
21904 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21905 VEC_iterate (tree, insts, ix, copy); ix++)
21906 TREE_PURPOSE (copy) = parsed_arg;
21907 }
21908
21909 pop_defarg_context ();
21910
21911 /* Make sure no default arg is missing. */
21912 check_default_args (fn);
21913
21914 /* Restore the state of local_variables_forbidden_p. */
21915 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21916
21917 /* Restore the queue. */
21918 pop_unparsed_function_queues (parser);
21919 }
21920
21921 /* Parse the operand of `sizeof' (or a similar operator). Returns
21922 either a TYPE or an expression, depending on the form of the
21923 input. The KEYWORD indicates which kind of expression we have
21924 encountered. */
21925
21926 static tree
21927 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21928 {
21929 tree expr = NULL_TREE;
21930 const char *saved_message;
21931 char *tmp;
21932 bool saved_integral_constant_expression_p;
21933 bool saved_non_integral_constant_expression_p;
21934 bool pack_expansion_p = false;
21935
21936 /* Types cannot be defined in a `sizeof' expression. Save away the
21937 old message. */
21938 saved_message = parser->type_definition_forbidden_message;
21939 /* And create the new one. */
21940 tmp = concat ("types may not be defined in %<",
21941 IDENTIFIER_POINTER (ridpointers[keyword]),
21942 "%> expressions", NULL);
21943 parser->type_definition_forbidden_message = tmp;
21944
21945 /* The restrictions on constant-expressions do not apply inside
21946 sizeof expressions. */
21947 saved_integral_constant_expression_p
21948 = parser->integral_constant_expression_p;
21949 saved_non_integral_constant_expression_p
21950 = parser->non_integral_constant_expression_p;
21951 parser->integral_constant_expression_p = false;
21952
21953 /* If it's a `...', then we are computing the length of a parameter
21954 pack. */
21955 if (keyword == RID_SIZEOF
21956 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21957 {
21958 /* Consume the `...'. */
21959 cp_lexer_consume_token (parser->lexer);
21960 maybe_warn_variadic_templates ();
21961
21962 /* Note that this is an expansion. */
21963 pack_expansion_p = true;
21964 }
21965
21966 /* Do not actually evaluate the expression. */
21967 ++cp_unevaluated_operand;
21968 ++c_inhibit_evaluation_warnings;
21969 /* If it's a `(', then we might be looking at the type-id
21970 construction. */
21971 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21972 {
21973 tree type;
21974 bool saved_in_type_id_in_expr_p;
21975
21976 /* We can't be sure yet whether we're looking at a type-id or an
21977 expression. */
21978 cp_parser_parse_tentatively (parser);
21979 /* Consume the `('. */
21980 cp_lexer_consume_token (parser->lexer);
21981 /* Parse the type-id. */
21982 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21983 parser->in_type_id_in_expr_p = true;
21984 type = cp_parser_type_id (parser);
21985 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21986 /* Now, look for the trailing `)'. */
21987 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21988 /* If all went well, then we're done. */
21989 if (cp_parser_parse_definitely (parser))
21990 {
21991 cp_decl_specifier_seq decl_specs;
21992
21993 /* Build a trivial decl-specifier-seq. */
21994 clear_decl_specs (&decl_specs);
21995 decl_specs.type = type;
21996
21997 /* Call grokdeclarator to figure out what type this is. */
21998 expr = grokdeclarator (NULL,
21999 &decl_specs,
22000 TYPENAME,
22001 /*initialized=*/0,
22002 /*attrlist=*/NULL);
22003 }
22004 }
22005 else if (pack_expansion_p)
22006 permerror (cp_lexer_peek_token (parser->lexer)->location,
22007 "%<sizeof...%> argument must be surrounded by parentheses");
22008
22009 /* If the type-id production did not work out, then we must be
22010 looking at the unary-expression production. */
22011 if (!expr)
22012 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22013 /*cast_p=*/false, NULL);
22014
22015 if (pack_expansion_p)
22016 /* Build a pack expansion. */
22017 expr = make_pack_expansion (expr);
22018
22019 /* Go back to evaluating expressions. */
22020 --cp_unevaluated_operand;
22021 --c_inhibit_evaluation_warnings;
22022
22023 /* Free the message we created. */
22024 free (tmp);
22025 /* And restore the old one. */
22026 parser->type_definition_forbidden_message = saved_message;
22027 parser->integral_constant_expression_p
22028 = saved_integral_constant_expression_p;
22029 parser->non_integral_constant_expression_p
22030 = saved_non_integral_constant_expression_p;
22031
22032 return expr;
22033 }
22034
22035 /* If the current declaration has no declarator, return true. */
22036
22037 static bool
22038 cp_parser_declares_only_class_p (cp_parser *parser)
22039 {
22040 /* If the next token is a `;' or a `,' then there is no
22041 declarator. */
22042 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22043 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22044 }
22045
22046 /* Update the DECL_SPECS to reflect the storage class indicated by
22047 KEYWORD. */
22048
22049 static void
22050 cp_parser_set_storage_class (cp_parser *parser,
22051 cp_decl_specifier_seq *decl_specs,
22052 enum rid keyword,
22053 location_t location)
22054 {
22055 cp_storage_class storage_class;
22056
22057 if (parser->in_unbraced_linkage_specification_p)
22058 {
22059 error_at (location, "invalid use of %qD in linkage specification",
22060 ridpointers[keyword]);
22061 return;
22062 }
22063 else if (decl_specs->storage_class != sc_none)
22064 {
22065 decl_specs->conflicting_specifiers_p = true;
22066 return;
22067 }
22068
22069 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22070 && decl_spec_seq_has_spec_p (decl_specs, ds_thread))
22071 {
22072 error_at (decl_specs->locations[ds_thread],
22073 "%<__thread%> before %qD", ridpointers[keyword]);
22074 decl_specs->locations[ds_thread] = 0;
22075 }
22076
22077 switch (keyword)
22078 {
22079 case RID_AUTO:
22080 storage_class = sc_auto;
22081 break;
22082 case RID_REGISTER:
22083 storage_class = sc_register;
22084 break;
22085 case RID_STATIC:
22086 storage_class = sc_static;
22087 break;
22088 case RID_EXTERN:
22089 storage_class = sc_extern;
22090 break;
22091 case RID_MUTABLE:
22092 storage_class = sc_mutable;
22093 break;
22094 default:
22095 gcc_unreachable ();
22096 }
22097 decl_specs->storage_class = storage_class;
22098 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, location);
22099
22100 /* A storage class specifier cannot be applied alongside a typedef
22101 specifier. If there is a typedef specifier present then set
22102 conflicting_specifiers_p which will trigger an error later
22103 on in grokdeclarator. */
22104 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22105 decl_specs->conflicting_specifiers_p = true;
22106 }
22107
22108 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22109 is true, the type is a class or enum definition. */
22110
22111 static void
22112 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22113 tree type_spec,
22114 location_t location,
22115 bool type_definition_p)
22116 {
22117 decl_specs->any_specifiers_p = true;
22118
22119 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22120 (with, for example, in "typedef int wchar_t;") we remember that
22121 this is what happened. In system headers, we ignore these
22122 declarations so that G++ can work with system headers that are not
22123 C++-safe. */
22124 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22125 && !type_definition_p
22126 && (type_spec == boolean_type_node
22127 || type_spec == char16_type_node
22128 || type_spec == char32_type_node
22129 || type_spec == wchar_type_node)
22130 && (decl_specs->type
22131 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22132 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22133 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22134 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22135 {
22136 decl_specs->redefined_builtin_type = type_spec;
22137 set_and_check_decl_spec_loc (decl_specs,
22138 ds_redefined_builtin_type_spec,
22139 location);
22140 if (!decl_specs->type)
22141 {
22142 decl_specs->type = type_spec;
22143 decl_specs->type_definition_p = false;
22144 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, location);
22145 }
22146 }
22147 else if (decl_specs->type)
22148 decl_specs->multiple_types_p = true;
22149 else
22150 {
22151 decl_specs->type = type_spec;
22152 decl_specs->type_definition_p = type_definition_p;
22153 decl_specs->redefined_builtin_type = NULL_TREE;
22154 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, location);
22155 }
22156 }
22157
22158 /* Set the location for a declarator specifier and check if it is
22159 duplicated.
22160
22161 DECL_SPECS is the sequence of declarator specifiers onto which to
22162 set the location.
22163
22164 DS is the single declarator specifier to set which location is to
22165 be set onto the existing sequence of declarators.
22166
22167 LOCATION is the location for the declarator specifier to
22168 consider. */
22169
22170 static void
22171 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22172 cp_decl_spec ds, source_location location)
22173 {
22174 gcc_assert (ds < ds_last);
22175
22176 if (decl_specs == NULL)
22177 return;
22178
22179 if (decl_specs->locations[ds] == 0)
22180 decl_specs->locations[ds] = location;
22181 else
22182 {
22183 if (ds == ds_long)
22184 {
22185 if (decl_specs->locations[ds_long_long] != 0)
22186 error_at (location,
22187 "%<long long long%> is too long for GCC");
22188 else
22189 {
22190 decl_specs->locations[ds_long_long] = location;
22191 pedwarn_cxx98 (location,
22192 OPT_Wlong_long,
22193 "ISO C++ 1998 does not support %<long long%>");
22194 }
22195 }
22196 else
22197 {
22198 static const char *const decl_spec_names[] = {
22199 "signed",
22200 "unsigned",
22201 "short",
22202 "long",
22203 "const",
22204 "volatile",
22205 "restrict",
22206 "inline",
22207 "virtual",
22208 "explicit",
22209 "friend",
22210 "typedef",
22211 "using",
22212 "constexpr",
22213 "__complex",
22214 "__thread"
22215 };
22216 error_at (location,
22217 "duplicate %qs", decl_spec_names[ds]);
22218 }
22219 }
22220 }
22221
22222 /* Return true iff the declarator specifier DS is present in the
22223 sequence of declarator specifiers DECL_SPECS. */
22224
22225 bool
22226 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22227 cp_decl_spec ds)
22228 {
22229 gcc_assert (ds < ds_last);
22230
22231 if (decl_specs == NULL)
22232 return false;
22233
22234 return decl_specs->locations[ds] != 0;
22235 }
22236
22237 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22238 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22239
22240 static bool
22241 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22242 {
22243 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22244 }
22245
22246 /* Issue an error message indicating that TOKEN_DESC was expected.
22247 If KEYWORD is true, it indicated this function is called by
22248 cp_parser_require_keword and the required token can only be
22249 a indicated keyword. */
22250
22251 static void
22252 cp_parser_required_error (cp_parser *parser,
22253 required_token token_desc,
22254 bool keyword)
22255 {
22256 switch (token_desc)
22257 {
22258 case RT_NEW:
22259 cp_parser_error (parser, "expected %<new%>");
22260 return;
22261 case RT_DELETE:
22262 cp_parser_error (parser, "expected %<delete%>");
22263 return;
22264 case RT_RETURN:
22265 cp_parser_error (parser, "expected %<return%>");
22266 return;
22267 case RT_WHILE:
22268 cp_parser_error (parser, "expected %<while%>");
22269 return;
22270 case RT_EXTERN:
22271 cp_parser_error (parser, "expected %<extern%>");
22272 return;
22273 case RT_STATIC_ASSERT:
22274 cp_parser_error (parser, "expected %<static_assert%>");
22275 return;
22276 case RT_DECLTYPE:
22277 cp_parser_error (parser, "expected %<decltype%>");
22278 return;
22279 case RT_OPERATOR:
22280 cp_parser_error (parser, "expected %<operator%>");
22281 return;
22282 case RT_CLASS:
22283 cp_parser_error (parser, "expected %<class%>");
22284 return;
22285 case RT_TEMPLATE:
22286 cp_parser_error (parser, "expected %<template%>");
22287 return;
22288 case RT_NAMESPACE:
22289 cp_parser_error (parser, "expected %<namespace%>");
22290 return;
22291 case RT_USING:
22292 cp_parser_error (parser, "expected %<using%>");
22293 return;
22294 case RT_ASM:
22295 cp_parser_error (parser, "expected %<asm%>");
22296 return;
22297 case RT_TRY:
22298 cp_parser_error (parser, "expected %<try%>");
22299 return;
22300 case RT_CATCH:
22301 cp_parser_error (parser, "expected %<catch%>");
22302 return;
22303 case RT_THROW:
22304 cp_parser_error (parser, "expected %<throw%>");
22305 return;
22306 case RT_LABEL:
22307 cp_parser_error (parser, "expected %<__label__%>");
22308 return;
22309 case RT_AT_TRY:
22310 cp_parser_error (parser, "expected %<@try%>");
22311 return;
22312 case RT_AT_SYNCHRONIZED:
22313 cp_parser_error (parser, "expected %<@synchronized%>");
22314 return;
22315 case RT_AT_THROW:
22316 cp_parser_error (parser, "expected %<@throw%>");
22317 return;
22318 case RT_TRANSACTION_ATOMIC:
22319 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22320 return;
22321 case RT_TRANSACTION_RELAXED:
22322 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22323 return;
22324 default:
22325 break;
22326 }
22327 if (!keyword)
22328 {
22329 switch (token_desc)
22330 {
22331 case RT_SEMICOLON:
22332 cp_parser_error (parser, "expected %<;%>");
22333 return;
22334 case RT_OPEN_PAREN:
22335 cp_parser_error (parser, "expected %<(%>");
22336 return;
22337 case RT_CLOSE_BRACE:
22338 cp_parser_error (parser, "expected %<}%>");
22339 return;
22340 case RT_OPEN_BRACE:
22341 cp_parser_error (parser, "expected %<{%>");
22342 return;
22343 case RT_CLOSE_SQUARE:
22344 cp_parser_error (parser, "expected %<]%>");
22345 return;
22346 case RT_OPEN_SQUARE:
22347 cp_parser_error (parser, "expected %<[%>");
22348 return;
22349 case RT_COMMA:
22350 cp_parser_error (parser, "expected %<,%>");
22351 return;
22352 case RT_SCOPE:
22353 cp_parser_error (parser, "expected %<::%>");
22354 return;
22355 case RT_LESS:
22356 cp_parser_error (parser, "expected %<<%>");
22357 return;
22358 case RT_GREATER:
22359 cp_parser_error (parser, "expected %<>%>");
22360 return;
22361 case RT_EQ:
22362 cp_parser_error (parser, "expected %<=%>");
22363 return;
22364 case RT_ELLIPSIS:
22365 cp_parser_error (parser, "expected %<...%>");
22366 return;
22367 case RT_MULT:
22368 cp_parser_error (parser, "expected %<*%>");
22369 return;
22370 case RT_COMPL:
22371 cp_parser_error (parser, "expected %<~%>");
22372 return;
22373 case RT_COLON:
22374 cp_parser_error (parser, "expected %<:%>");
22375 return;
22376 case RT_COLON_SCOPE:
22377 cp_parser_error (parser, "expected %<:%> or %<::%>");
22378 return;
22379 case RT_CLOSE_PAREN:
22380 cp_parser_error (parser, "expected %<)%>");
22381 return;
22382 case RT_COMMA_CLOSE_PAREN:
22383 cp_parser_error (parser, "expected %<,%> or %<)%>");
22384 return;
22385 case RT_PRAGMA_EOL:
22386 cp_parser_error (parser, "expected end of line");
22387 return;
22388 case RT_NAME:
22389 cp_parser_error (parser, "expected identifier");
22390 return;
22391 case RT_SELECT:
22392 cp_parser_error (parser, "expected selection-statement");
22393 return;
22394 case RT_INTERATION:
22395 cp_parser_error (parser, "expected iteration-statement");
22396 return;
22397 case RT_JUMP:
22398 cp_parser_error (parser, "expected jump-statement");
22399 return;
22400 case RT_CLASS_KEY:
22401 cp_parser_error (parser, "expected class-key");
22402 return;
22403 case RT_CLASS_TYPENAME_TEMPLATE:
22404 cp_parser_error (parser,
22405 "expected %<class%>, %<typename%>, or %<template%>");
22406 return;
22407 default:
22408 gcc_unreachable ();
22409 }
22410 }
22411 else
22412 gcc_unreachable ();
22413 }
22414
22415
22416
22417 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22418 issue an error message indicating that TOKEN_DESC was expected.
22419
22420 Returns the token consumed, if the token had the appropriate type.
22421 Otherwise, returns NULL. */
22422
22423 static cp_token *
22424 cp_parser_require (cp_parser* parser,
22425 enum cpp_ttype type,
22426 required_token token_desc)
22427 {
22428 if (cp_lexer_next_token_is (parser->lexer, type))
22429 return cp_lexer_consume_token (parser->lexer);
22430 else
22431 {
22432 /* Output the MESSAGE -- unless we're parsing tentatively. */
22433 if (!cp_parser_simulate_error (parser))
22434 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22435 return NULL;
22436 }
22437 }
22438
22439 /* An error message is produced if the next token is not '>'.
22440 All further tokens are skipped until the desired token is
22441 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22442
22443 static void
22444 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22445 {
22446 /* Current level of '< ... >'. */
22447 unsigned level = 0;
22448 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22449 unsigned nesting_depth = 0;
22450
22451 /* Are we ready, yet? If not, issue error message. */
22452 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22453 return;
22454
22455 /* Skip tokens until the desired token is found. */
22456 while (true)
22457 {
22458 /* Peek at the next token. */
22459 switch (cp_lexer_peek_token (parser->lexer)->type)
22460 {
22461 case CPP_LESS:
22462 if (!nesting_depth)
22463 ++level;
22464 break;
22465
22466 case CPP_RSHIFT:
22467 if (cxx_dialect == cxx98)
22468 /* C++0x views the `>>' operator as two `>' tokens, but
22469 C++98 does not. */
22470 break;
22471 else if (!nesting_depth && level-- == 0)
22472 {
22473 /* We've hit a `>>' where the first `>' closes the
22474 template argument list, and the second `>' is
22475 spurious. Just consume the `>>' and stop; we've
22476 already produced at least one error. */
22477 cp_lexer_consume_token (parser->lexer);
22478 return;
22479 }
22480 /* Fall through for C++0x, so we handle the second `>' in
22481 the `>>'. */
22482
22483 case CPP_GREATER:
22484 if (!nesting_depth && level-- == 0)
22485 {
22486 /* We've reached the token we want, consume it and stop. */
22487 cp_lexer_consume_token (parser->lexer);
22488 return;
22489 }
22490 break;
22491
22492 case CPP_OPEN_PAREN:
22493 case CPP_OPEN_SQUARE:
22494 ++nesting_depth;
22495 break;
22496
22497 case CPP_CLOSE_PAREN:
22498 case CPP_CLOSE_SQUARE:
22499 if (nesting_depth-- == 0)
22500 return;
22501 break;
22502
22503 case CPP_EOF:
22504 case CPP_PRAGMA_EOL:
22505 case CPP_SEMICOLON:
22506 case CPP_OPEN_BRACE:
22507 case CPP_CLOSE_BRACE:
22508 /* The '>' was probably forgotten, don't look further. */
22509 return;
22510
22511 default:
22512 break;
22513 }
22514
22515 /* Consume this token. */
22516 cp_lexer_consume_token (parser->lexer);
22517 }
22518 }
22519
22520 /* If the next token is the indicated keyword, consume it. Otherwise,
22521 issue an error message indicating that TOKEN_DESC was expected.
22522
22523 Returns the token consumed, if the token had the appropriate type.
22524 Otherwise, returns NULL. */
22525
22526 static cp_token *
22527 cp_parser_require_keyword (cp_parser* parser,
22528 enum rid keyword,
22529 required_token token_desc)
22530 {
22531 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22532
22533 if (token && token->keyword != keyword)
22534 {
22535 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22536 return NULL;
22537 }
22538
22539 return token;
22540 }
22541
22542 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22543 function-definition. */
22544
22545 static bool
22546 cp_parser_token_starts_function_definition_p (cp_token* token)
22547 {
22548 return (/* An ordinary function-body begins with an `{'. */
22549 token->type == CPP_OPEN_BRACE
22550 /* A ctor-initializer begins with a `:'. */
22551 || token->type == CPP_COLON
22552 /* A function-try-block begins with `try'. */
22553 || token->keyword == RID_TRY
22554 /* A function-transaction-block begins with `__transaction_atomic'
22555 or `__transaction_relaxed'. */
22556 || token->keyword == RID_TRANSACTION_ATOMIC
22557 || token->keyword == RID_TRANSACTION_RELAXED
22558 /* The named return value extension begins with `return'. */
22559 || token->keyword == RID_RETURN);
22560 }
22561
22562 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22563 definition. */
22564
22565 static bool
22566 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22567 {
22568 cp_token *token;
22569
22570 token = cp_lexer_peek_token (parser->lexer);
22571 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22572 }
22573
22574 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22575 C++0x) ending a template-argument. */
22576
22577 static bool
22578 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22579 {
22580 cp_token *token;
22581
22582 token = cp_lexer_peek_token (parser->lexer);
22583 return (token->type == CPP_COMMA
22584 || token->type == CPP_GREATER
22585 || token->type == CPP_ELLIPSIS
22586 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22587 }
22588
22589 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22590 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22591
22592 static bool
22593 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22594 size_t n)
22595 {
22596 cp_token *token;
22597
22598 token = cp_lexer_peek_nth_token (parser->lexer, n);
22599 if (token->type == CPP_LESS)
22600 return true;
22601 /* Check for the sequence `<::' in the original code. It would be lexed as
22602 `[:', where `[' is a digraph, and there is no whitespace before
22603 `:'. */
22604 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22605 {
22606 cp_token *token2;
22607 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22608 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22609 return true;
22610 }
22611 return false;
22612 }
22613
22614 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22615 or none_type otherwise. */
22616
22617 static enum tag_types
22618 cp_parser_token_is_class_key (cp_token* token)
22619 {
22620 switch (token->keyword)
22621 {
22622 case RID_CLASS:
22623 return class_type;
22624 case RID_STRUCT:
22625 return record_type;
22626 case RID_UNION:
22627 return union_type;
22628
22629 default:
22630 return none_type;
22631 }
22632 }
22633
22634 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22635
22636 static void
22637 cp_parser_check_class_key (enum tag_types class_key, tree type)
22638 {
22639 if (type == error_mark_node)
22640 return;
22641 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22642 {
22643 permerror (input_location, "%qs tag used in naming %q#T",
22644 class_key == union_type ? "union"
22645 : class_key == record_type ? "struct" : "class",
22646 type);
22647 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22648 "%q#T was previously declared here", type);
22649 }
22650 }
22651
22652 /* Issue an error message if DECL is redeclared with different
22653 access than its original declaration [class.access.spec/3].
22654 This applies to nested classes and nested class templates.
22655 [class.mem/1]. */
22656
22657 static void
22658 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22659 {
22660 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22661 return;
22662
22663 if ((TREE_PRIVATE (decl)
22664 != (current_access_specifier == access_private_node))
22665 || (TREE_PROTECTED (decl)
22666 != (current_access_specifier == access_protected_node)))
22667 error_at (location, "%qD redeclared with different access", decl);
22668 }
22669
22670 /* Look for the `template' keyword, as a syntactic disambiguator.
22671 Return TRUE iff it is present, in which case it will be
22672 consumed. */
22673
22674 static bool
22675 cp_parser_optional_template_keyword (cp_parser *parser)
22676 {
22677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22678 {
22679 /* The `template' keyword can only be used within templates;
22680 outside templates the parser can always figure out what is a
22681 template and what is not. */
22682 if (!processing_template_decl)
22683 {
22684 cp_token *token = cp_lexer_peek_token (parser->lexer);
22685 error_at (token->location,
22686 "%<template%> (as a disambiguator) is only allowed "
22687 "within templates");
22688 /* If this part of the token stream is rescanned, the same
22689 error message would be generated. So, we purge the token
22690 from the stream. */
22691 cp_lexer_purge_token (parser->lexer);
22692 return false;
22693 }
22694 else
22695 {
22696 /* Consume the `template' keyword. */
22697 cp_lexer_consume_token (parser->lexer);
22698 return true;
22699 }
22700 }
22701
22702 return false;
22703 }
22704
22705 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22706 set PARSER->SCOPE, and perform other related actions. */
22707
22708 static void
22709 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22710 {
22711 int i;
22712 struct tree_check *check_value;
22713 deferred_access_check *chk;
22714 VEC (deferred_access_check,gc) *checks;
22715
22716 /* Get the stored value. */
22717 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22718 /* Perform any access checks that were deferred. */
22719 checks = check_value->checks;
22720 if (checks)
22721 {
22722 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22723 perform_or_defer_access_check (chk->binfo,
22724 chk->decl,
22725 chk->diag_decl, tf_warning_or_error);
22726 }
22727 /* Set the scope from the stored value. */
22728 parser->scope = check_value->value;
22729 parser->qualifying_scope = check_value->qualifying_scope;
22730 parser->object_scope = NULL_TREE;
22731 }
22732
22733 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22734 encounter the end of a block before what we were looking for. */
22735
22736 static bool
22737 cp_parser_cache_group (cp_parser *parser,
22738 enum cpp_ttype end,
22739 unsigned depth)
22740 {
22741 while (true)
22742 {
22743 cp_token *token = cp_lexer_peek_token (parser->lexer);
22744
22745 /* Abort a parenthesized expression if we encounter a semicolon. */
22746 if ((end == CPP_CLOSE_PAREN || depth == 0)
22747 && token->type == CPP_SEMICOLON)
22748 return true;
22749 /* If we've reached the end of the file, stop. */
22750 if (token->type == CPP_EOF
22751 || (end != CPP_PRAGMA_EOL
22752 && token->type == CPP_PRAGMA_EOL))
22753 return true;
22754 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22755 /* We've hit the end of an enclosing block, so there's been some
22756 kind of syntax error. */
22757 return true;
22758
22759 /* Consume the token. */
22760 cp_lexer_consume_token (parser->lexer);
22761 /* See if it starts a new group. */
22762 if (token->type == CPP_OPEN_BRACE)
22763 {
22764 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22765 /* In theory this should probably check end == '}', but
22766 cp_parser_save_member_function_body needs it to exit
22767 after either '}' or ')' when called with ')'. */
22768 if (depth == 0)
22769 return false;
22770 }
22771 else if (token->type == CPP_OPEN_PAREN)
22772 {
22773 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22774 if (depth == 0 && end == CPP_CLOSE_PAREN)
22775 return false;
22776 }
22777 else if (token->type == CPP_PRAGMA)
22778 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22779 else if (token->type == end)
22780 return false;
22781 }
22782 }
22783
22784 /* Like above, for caching a default argument or NSDMI. Both of these are
22785 terminated by a non-nested comma, but it can be unclear whether or not a
22786 comma is nested in a template argument list unless we do more parsing.
22787 In order to handle this ambiguity, when we encounter a ',' after a '<'
22788 we try to parse what follows as a parameter-declaration-list (in the
22789 case of a default argument) or a member-declarator (in the case of an
22790 NSDMI). If that succeeds, then we stop caching. */
22791
22792 static tree
22793 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22794 {
22795 unsigned depth = 0;
22796 int maybe_template_id = 0;
22797 cp_token *first_token;
22798 cp_token *token;
22799 tree default_argument;
22800
22801 /* Add tokens until we have processed the entire default
22802 argument. We add the range [first_token, token). */
22803 first_token = cp_lexer_peek_token (parser->lexer);
22804 if (first_token->type == CPP_OPEN_BRACE)
22805 {
22806 /* For list-initialization, this is straightforward. */
22807 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22808 token = cp_lexer_peek_token (parser->lexer);
22809 }
22810 else while (true)
22811 {
22812 bool done = false;
22813
22814 /* Peek at the next token. */
22815 token = cp_lexer_peek_token (parser->lexer);
22816 /* What we do depends on what token we have. */
22817 switch (token->type)
22818 {
22819 /* In valid code, a default argument must be
22820 immediately followed by a `,' `)', or `...'. */
22821 case CPP_COMMA:
22822 if (depth == 0 && maybe_template_id)
22823 {
22824 /* If we've seen a '<', we might be in a
22825 template-argument-list. Until Core issue 325 is
22826 resolved, we don't know how this situation ought
22827 to be handled, so try to DTRT. We check whether
22828 what comes after the comma is a valid parameter
22829 declaration list. If it is, then the comma ends
22830 the default argument; otherwise the default
22831 argument continues. */
22832 bool error = false;
22833 tree t;
22834
22835 /* Set ITALP so cp_parser_parameter_declaration_list
22836 doesn't decide to commit to this parse. */
22837 bool saved_italp = parser->in_template_argument_list_p;
22838 parser->in_template_argument_list_p = true;
22839
22840 cp_parser_parse_tentatively (parser);
22841 cp_lexer_consume_token (parser->lexer);
22842
22843 if (nsdmi)
22844 {
22845 int ctor_dtor_or_conv_p;
22846 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22847 &ctor_dtor_or_conv_p,
22848 /*parenthesized_p=*/NULL,
22849 /*member_p=*/true);
22850 }
22851 else
22852 {
22853 begin_scope (sk_function_parms, NULL_TREE);
22854 cp_parser_parameter_declaration_list (parser, &error);
22855 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22856 pop_binding (DECL_NAME (t), t);
22857 leave_scope ();
22858 }
22859 if (!cp_parser_error_occurred (parser) && !error)
22860 done = true;
22861 cp_parser_abort_tentative_parse (parser);
22862
22863 parser->in_template_argument_list_p = saved_italp;
22864 break;
22865 }
22866 case CPP_CLOSE_PAREN:
22867 case CPP_ELLIPSIS:
22868 /* If we run into a non-nested `;', `}', or `]',
22869 then the code is invalid -- but the default
22870 argument is certainly over. */
22871 case CPP_SEMICOLON:
22872 case CPP_CLOSE_BRACE:
22873 case CPP_CLOSE_SQUARE:
22874 if (depth == 0)
22875 done = true;
22876 /* Update DEPTH, if necessary. */
22877 else if (token->type == CPP_CLOSE_PAREN
22878 || token->type == CPP_CLOSE_BRACE
22879 || token->type == CPP_CLOSE_SQUARE)
22880 --depth;
22881 break;
22882
22883 case CPP_OPEN_PAREN:
22884 case CPP_OPEN_SQUARE:
22885 case CPP_OPEN_BRACE:
22886 ++depth;
22887 break;
22888
22889 case CPP_LESS:
22890 if (depth == 0)
22891 /* This might be the comparison operator, or it might
22892 start a template argument list. */
22893 ++maybe_template_id;
22894 break;
22895
22896 case CPP_RSHIFT:
22897 if (cxx_dialect == cxx98)
22898 break;
22899 /* Fall through for C++0x, which treats the `>>'
22900 operator like two `>' tokens in certain
22901 cases. */
22902
22903 case CPP_GREATER:
22904 if (depth == 0)
22905 {
22906 /* This might be an operator, or it might close a
22907 template argument list. But if a previous '<'
22908 started a template argument list, this will have
22909 closed it, so we can't be in one anymore. */
22910 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22911 if (maybe_template_id < 0)
22912 maybe_template_id = 0;
22913 }
22914 break;
22915
22916 /* If we run out of tokens, issue an error message. */
22917 case CPP_EOF:
22918 case CPP_PRAGMA_EOL:
22919 error_at (token->location, "file ends in default argument");
22920 done = true;
22921 break;
22922
22923 case CPP_NAME:
22924 case CPP_SCOPE:
22925 /* In these cases, we should look for template-ids.
22926 For example, if the default argument is
22927 `X<int, double>()', we need to do name lookup to
22928 figure out whether or not `X' is a template; if
22929 so, the `,' does not end the default argument.
22930
22931 That is not yet done. */
22932 break;
22933
22934 default:
22935 break;
22936 }
22937
22938 /* If we've reached the end, stop. */
22939 if (done)
22940 break;
22941
22942 /* Add the token to the token block. */
22943 token = cp_lexer_consume_token (parser->lexer);
22944 }
22945
22946 /* Create a DEFAULT_ARG to represent the unparsed default
22947 argument. */
22948 default_argument = make_node (DEFAULT_ARG);
22949 DEFARG_TOKENS (default_argument)
22950 = cp_token_cache_new (first_token, token);
22951 DEFARG_INSTANTIATIONS (default_argument) = NULL;
22952
22953 return default_argument;
22954 }
22955
22956 /* Begin parsing tentatively. We always save tokens while parsing
22957 tentatively so that if the tentative parsing fails we can restore the
22958 tokens. */
22959
22960 static void
22961 cp_parser_parse_tentatively (cp_parser* parser)
22962 {
22963 /* Enter a new parsing context. */
22964 parser->context = cp_parser_context_new (parser->context);
22965 /* Begin saving tokens. */
22966 cp_lexer_save_tokens (parser->lexer);
22967 /* In order to avoid repetitive access control error messages,
22968 access checks are queued up until we are no longer parsing
22969 tentatively. */
22970 push_deferring_access_checks (dk_deferred);
22971 }
22972
22973 /* Commit to the currently active tentative parse. */
22974
22975 static void
22976 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22977 {
22978 cp_parser_context *context;
22979 cp_lexer *lexer;
22980
22981 /* Mark all of the levels as committed. */
22982 lexer = parser->lexer;
22983 for (context = parser->context; context->next; context = context->next)
22984 {
22985 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22986 break;
22987 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22988 while (!cp_lexer_saving_tokens (lexer))
22989 lexer = lexer->next;
22990 cp_lexer_commit_tokens (lexer);
22991 }
22992 }
22993
22994 /* Abort the currently active tentative parse. All consumed tokens
22995 will be rolled back, and no diagnostics will be issued. */
22996
22997 static void
22998 cp_parser_abort_tentative_parse (cp_parser* parser)
22999 {
23000 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23001 || errorcount > 0);
23002 cp_parser_simulate_error (parser);
23003 /* Now, pretend that we want to see if the construct was
23004 successfully parsed. */
23005 cp_parser_parse_definitely (parser);
23006 }
23007
23008 /* Stop parsing tentatively. If a parse error has occurred, restore the
23009 token stream. Otherwise, commit to the tokens we have consumed.
23010 Returns true if no error occurred; false otherwise. */
23011
23012 static bool
23013 cp_parser_parse_definitely (cp_parser* parser)
23014 {
23015 bool error_occurred;
23016 cp_parser_context *context;
23017
23018 /* Remember whether or not an error occurred, since we are about to
23019 destroy that information. */
23020 error_occurred = cp_parser_error_occurred (parser);
23021 /* Remove the topmost context from the stack. */
23022 context = parser->context;
23023 parser->context = context->next;
23024 /* If no parse errors occurred, commit to the tentative parse. */
23025 if (!error_occurred)
23026 {
23027 /* Commit to the tokens read tentatively, unless that was
23028 already done. */
23029 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23030 cp_lexer_commit_tokens (parser->lexer);
23031
23032 pop_to_parent_deferring_access_checks ();
23033 }
23034 /* Otherwise, if errors occurred, roll back our state so that things
23035 are just as they were before we began the tentative parse. */
23036 else
23037 {
23038 cp_lexer_rollback_tokens (parser->lexer);
23039 pop_deferring_access_checks ();
23040 }
23041 /* Add the context to the front of the free list. */
23042 context->next = cp_parser_context_free_list;
23043 cp_parser_context_free_list = context;
23044
23045 return !error_occurred;
23046 }
23047
23048 /* Returns true if we are parsing tentatively and are not committed to
23049 this tentative parse. */
23050
23051 static bool
23052 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23053 {
23054 return (cp_parser_parsing_tentatively (parser)
23055 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23056 }
23057
23058 /* Returns nonzero iff an error has occurred during the most recent
23059 tentative parse. */
23060
23061 static bool
23062 cp_parser_error_occurred (cp_parser* parser)
23063 {
23064 return (cp_parser_parsing_tentatively (parser)
23065 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23066 }
23067
23068 /* Returns nonzero if GNU extensions are allowed. */
23069
23070 static bool
23071 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23072 {
23073 return parser->allow_gnu_extensions_p;
23074 }
23075 \f
23076 /* Objective-C++ Productions */
23077
23078
23079 /* Parse an Objective-C expression, which feeds into a primary-expression
23080 above.
23081
23082 objc-expression:
23083 objc-message-expression
23084 objc-string-literal
23085 objc-encode-expression
23086 objc-protocol-expression
23087 objc-selector-expression
23088
23089 Returns a tree representation of the expression. */
23090
23091 static tree
23092 cp_parser_objc_expression (cp_parser* parser)
23093 {
23094 /* Try to figure out what kind of declaration is present. */
23095 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23096
23097 switch (kwd->type)
23098 {
23099 case CPP_OPEN_SQUARE:
23100 return cp_parser_objc_message_expression (parser);
23101
23102 case CPP_OBJC_STRING:
23103 kwd = cp_lexer_consume_token (parser->lexer);
23104 return objc_build_string_object (kwd->u.value);
23105
23106 case CPP_KEYWORD:
23107 switch (kwd->keyword)
23108 {
23109 case RID_AT_ENCODE:
23110 return cp_parser_objc_encode_expression (parser);
23111
23112 case RID_AT_PROTOCOL:
23113 return cp_parser_objc_protocol_expression (parser);
23114
23115 case RID_AT_SELECTOR:
23116 return cp_parser_objc_selector_expression (parser);
23117
23118 default:
23119 break;
23120 }
23121 default:
23122 error_at (kwd->location,
23123 "misplaced %<@%D%> Objective-C++ construct",
23124 kwd->u.value);
23125 cp_parser_skip_to_end_of_block_or_statement (parser);
23126 }
23127
23128 return error_mark_node;
23129 }
23130
23131 /* Parse an Objective-C message expression.
23132
23133 objc-message-expression:
23134 [ objc-message-receiver objc-message-args ]
23135
23136 Returns a representation of an Objective-C message. */
23137
23138 static tree
23139 cp_parser_objc_message_expression (cp_parser* parser)
23140 {
23141 tree receiver, messageargs;
23142
23143 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23144 receiver = cp_parser_objc_message_receiver (parser);
23145 messageargs = cp_parser_objc_message_args (parser);
23146 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23147
23148 return objc_build_message_expr (receiver, messageargs);
23149 }
23150
23151 /* Parse an objc-message-receiver.
23152
23153 objc-message-receiver:
23154 expression
23155 simple-type-specifier
23156
23157 Returns a representation of the type or expression. */
23158
23159 static tree
23160 cp_parser_objc_message_receiver (cp_parser* parser)
23161 {
23162 tree rcv;
23163
23164 /* An Objective-C message receiver may be either (1) a type
23165 or (2) an expression. */
23166 cp_parser_parse_tentatively (parser);
23167 rcv = cp_parser_expression (parser, false, NULL);
23168
23169 if (cp_parser_parse_definitely (parser))
23170 return rcv;
23171
23172 rcv = cp_parser_simple_type_specifier (parser,
23173 /*decl_specs=*/NULL,
23174 CP_PARSER_FLAGS_NONE);
23175
23176 return objc_get_class_reference (rcv);
23177 }
23178
23179 /* Parse the arguments and selectors comprising an Objective-C message.
23180
23181 objc-message-args:
23182 objc-selector
23183 objc-selector-args
23184 objc-selector-args , objc-comma-args
23185
23186 objc-selector-args:
23187 objc-selector [opt] : assignment-expression
23188 objc-selector-args objc-selector [opt] : assignment-expression
23189
23190 objc-comma-args:
23191 assignment-expression
23192 objc-comma-args , assignment-expression
23193
23194 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23195 selector arguments and TREE_VALUE containing a list of comma
23196 arguments. */
23197
23198 static tree
23199 cp_parser_objc_message_args (cp_parser* parser)
23200 {
23201 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23202 bool maybe_unary_selector_p = true;
23203 cp_token *token = cp_lexer_peek_token (parser->lexer);
23204
23205 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23206 {
23207 tree selector = NULL_TREE, arg;
23208
23209 if (token->type != CPP_COLON)
23210 selector = cp_parser_objc_selector (parser);
23211
23212 /* Detect if we have a unary selector. */
23213 if (maybe_unary_selector_p
23214 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23215 return build_tree_list (selector, NULL_TREE);
23216
23217 maybe_unary_selector_p = false;
23218 cp_parser_require (parser, CPP_COLON, RT_COLON);
23219 arg = cp_parser_assignment_expression (parser, false, NULL);
23220
23221 sel_args
23222 = chainon (sel_args,
23223 build_tree_list (selector, arg));
23224
23225 token = cp_lexer_peek_token (parser->lexer);
23226 }
23227
23228 /* Handle non-selector arguments, if any. */
23229 while (token->type == CPP_COMMA)
23230 {
23231 tree arg;
23232
23233 cp_lexer_consume_token (parser->lexer);
23234 arg = cp_parser_assignment_expression (parser, false, NULL);
23235
23236 addl_args
23237 = chainon (addl_args,
23238 build_tree_list (NULL_TREE, arg));
23239
23240 token = cp_lexer_peek_token (parser->lexer);
23241 }
23242
23243 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23244 {
23245 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23246 return build_tree_list (error_mark_node, error_mark_node);
23247 }
23248
23249 return build_tree_list (sel_args, addl_args);
23250 }
23251
23252 /* Parse an Objective-C encode expression.
23253
23254 objc-encode-expression:
23255 @encode objc-typename
23256
23257 Returns an encoded representation of the type argument. */
23258
23259 static tree
23260 cp_parser_objc_encode_expression (cp_parser* parser)
23261 {
23262 tree type;
23263 cp_token *token;
23264
23265 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23266 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23267 token = cp_lexer_peek_token (parser->lexer);
23268 type = complete_type (cp_parser_type_id (parser));
23269 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23270
23271 if (!type)
23272 {
23273 error_at (token->location,
23274 "%<@encode%> must specify a type as an argument");
23275 return error_mark_node;
23276 }
23277
23278 /* This happens if we find @encode(T) (where T is a template
23279 typename or something dependent on a template typename) when
23280 parsing a template. In that case, we can't compile it
23281 immediately, but we rather create an AT_ENCODE_EXPR which will
23282 need to be instantiated when the template is used.
23283 */
23284 if (dependent_type_p (type))
23285 {
23286 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23287 TREE_READONLY (value) = 1;
23288 return value;
23289 }
23290
23291 return objc_build_encode_expr (type);
23292 }
23293
23294 /* Parse an Objective-C @defs expression. */
23295
23296 static tree
23297 cp_parser_objc_defs_expression (cp_parser *parser)
23298 {
23299 tree name;
23300
23301 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23302 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23303 name = cp_parser_identifier (parser);
23304 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23305
23306 return objc_get_class_ivars (name);
23307 }
23308
23309 /* Parse an Objective-C protocol expression.
23310
23311 objc-protocol-expression:
23312 @protocol ( identifier )
23313
23314 Returns a representation of the protocol expression. */
23315
23316 static tree
23317 cp_parser_objc_protocol_expression (cp_parser* parser)
23318 {
23319 tree proto;
23320
23321 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23322 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23323 proto = cp_parser_identifier (parser);
23324 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23325
23326 return objc_build_protocol_expr (proto);
23327 }
23328
23329 /* Parse an Objective-C selector expression.
23330
23331 objc-selector-expression:
23332 @selector ( objc-method-signature )
23333
23334 objc-method-signature:
23335 objc-selector
23336 objc-selector-seq
23337
23338 objc-selector-seq:
23339 objc-selector :
23340 objc-selector-seq objc-selector :
23341
23342 Returns a representation of the method selector. */
23343
23344 static tree
23345 cp_parser_objc_selector_expression (cp_parser* parser)
23346 {
23347 tree sel_seq = NULL_TREE;
23348 bool maybe_unary_selector_p = true;
23349 cp_token *token;
23350 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23351
23352 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23353 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23354 token = cp_lexer_peek_token (parser->lexer);
23355
23356 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23357 || token->type == CPP_SCOPE)
23358 {
23359 tree selector = NULL_TREE;
23360
23361 if (token->type != CPP_COLON
23362 || token->type == CPP_SCOPE)
23363 selector = cp_parser_objc_selector (parser);
23364
23365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23366 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23367 {
23368 /* Detect if we have a unary selector. */
23369 if (maybe_unary_selector_p)
23370 {
23371 sel_seq = selector;
23372 goto finish_selector;
23373 }
23374 else
23375 {
23376 cp_parser_error (parser, "expected %<:%>");
23377 }
23378 }
23379 maybe_unary_selector_p = false;
23380 token = cp_lexer_consume_token (parser->lexer);
23381
23382 if (token->type == CPP_SCOPE)
23383 {
23384 sel_seq
23385 = chainon (sel_seq,
23386 build_tree_list (selector, NULL_TREE));
23387 sel_seq
23388 = chainon (sel_seq,
23389 build_tree_list (NULL_TREE, NULL_TREE));
23390 }
23391 else
23392 sel_seq
23393 = chainon (sel_seq,
23394 build_tree_list (selector, NULL_TREE));
23395
23396 token = cp_lexer_peek_token (parser->lexer);
23397 }
23398
23399 finish_selector:
23400 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23401
23402 return objc_build_selector_expr (loc, sel_seq);
23403 }
23404
23405 /* Parse a list of identifiers.
23406
23407 objc-identifier-list:
23408 identifier
23409 objc-identifier-list , identifier
23410
23411 Returns a TREE_LIST of identifier nodes. */
23412
23413 static tree
23414 cp_parser_objc_identifier_list (cp_parser* parser)
23415 {
23416 tree identifier;
23417 tree list;
23418 cp_token *sep;
23419
23420 identifier = cp_parser_identifier (parser);
23421 if (identifier == error_mark_node)
23422 return error_mark_node;
23423
23424 list = build_tree_list (NULL_TREE, identifier);
23425 sep = cp_lexer_peek_token (parser->lexer);
23426
23427 while (sep->type == CPP_COMMA)
23428 {
23429 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23430 identifier = cp_parser_identifier (parser);
23431 if (identifier == error_mark_node)
23432 return list;
23433
23434 list = chainon (list, build_tree_list (NULL_TREE,
23435 identifier));
23436 sep = cp_lexer_peek_token (parser->lexer);
23437 }
23438
23439 return list;
23440 }
23441
23442 /* Parse an Objective-C alias declaration.
23443
23444 objc-alias-declaration:
23445 @compatibility_alias identifier identifier ;
23446
23447 This function registers the alias mapping with the Objective-C front end.
23448 It returns nothing. */
23449
23450 static void
23451 cp_parser_objc_alias_declaration (cp_parser* parser)
23452 {
23453 tree alias, orig;
23454
23455 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23456 alias = cp_parser_identifier (parser);
23457 orig = cp_parser_identifier (parser);
23458 objc_declare_alias (alias, orig);
23459 cp_parser_consume_semicolon_at_end_of_statement (parser);
23460 }
23461
23462 /* Parse an Objective-C class forward-declaration.
23463
23464 objc-class-declaration:
23465 @class objc-identifier-list ;
23466
23467 The function registers the forward declarations with the Objective-C
23468 front end. It returns nothing. */
23469
23470 static void
23471 cp_parser_objc_class_declaration (cp_parser* parser)
23472 {
23473 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23474 while (true)
23475 {
23476 tree id;
23477
23478 id = cp_parser_identifier (parser);
23479 if (id == error_mark_node)
23480 break;
23481
23482 objc_declare_class (id);
23483
23484 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23485 cp_lexer_consume_token (parser->lexer);
23486 else
23487 break;
23488 }
23489 cp_parser_consume_semicolon_at_end_of_statement (parser);
23490 }
23491
23492 /* Parse a list of Objective-C protocol references.
23493
23494 objc-protocol-refs-opt:
23495 objc-protocol-refs [opt]
23496
23497 objc-protocol-refs:
23498 < objc-identifier-list >
23499
23500 Returns a TREE_LIST of identifiers, if any. */
23501
23502 static tree
23503 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23504 {
23505 tree protorefs = NULL_TREE;
23506
23507 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23508 {
23509 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23510 protorefs = cp_parser_objc_identifier_list (parser);
23511 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23512 }
23513
23514 return protorefs;
23515 }
23516
23517 /* Parse a Objective-C visibility specification. */
23518
23519 static void
23520 cp_parser_objc_visibility_spec (cp_parser* parser)
23521 {
23522 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23523
23524 switch (vis->keyword)
23525 {
23526 case RID_AT_PRIVATE:
23527 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23528 break;
23529 case RID_AT_PROTECTED:
23530 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23531 break;
23532 case RID_AT_PUBLIC:
23533 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23534 break;
23535 case RID_AT_PACKAGE:
23536 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23537 break;
23538 default:
23539 return;
23540 }
23541
23542 /* Eat '@private'/'@protected'/'@public'. */
23543 cp_lexer_consume_token (parser->lexer);
23544 }
23545
23546 /* Parse an Objective-C method type. Return 'true' if it is a class
23547 (+) method, and 'false' if it is an instance (-) method. */
23548
23549 static inline bool
23550 cp_parser_objc_method_type (cp_parser* parser)
23551 {
23552 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23553 return true;
23554 else
23555 return false;
23556 }
23557
23558 /* Parse an Objective-C protocol qualifier. */
23559
23560 static tree
23561 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23562 {
23563 tree quals = NULL_TREE, node;
23564 cp_token *token = cp_lexer_peek_token (parser->lexer);
23565
23566 node = token->u.value;
23567
23568 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23569 && (node == ridpointers [(int) RID_IN]
23570 || node == ridpointers [(int) RID_OUT]
23571 || node == ridpointers [(int) RID_INOUT]
23572 || node == ridpointers [(int) RID_BYCOPY]
23573 || node == ridpointers [(int) RID_BYREF]
23574 || node == ridpointers [(int) RID_ONEWAY]))
23575 {
23576 quals = tree_cons (NULL_TREE, node, quals);
23577 cp_lexer_consume_token (parser->lexer);
23578 token = cp_lexer_peek_token (parser->lexer);
23579 node = token->u.value;
23580 }
23581
23582 return quals;
23583 }
23584
23585 /* Parse an Objective-C typename. */
23586
23587 static tree
23588 cp_parser_objc_typename (cp_parser* parser)
23589 {
23590 tree type_name = NULL_TREE;
23591
23592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23593 {
23594 tree proto_quals, cp_type = NULL_TREE;
23595
23596 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23597 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23598
23599 /* An ObjC type name may consist of just protocol qualifiers, in which
23600 case the type shall default to 'id'. */
23601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23602 {
23603 cp_type = cp_parser_type_id (parser);
23604
23605 /* If the type could not be parsed, an error has already
23606 been produced. For error recovery, behave as if it had
23607 not been specified, which will use the default type
23608 'id'. */
23609 if (cp_type == error_mark_node)
23610 {
23611 cp_type = NULL_TREE;
23612 /* We need to skip to the closing parenthesis as
23613 cp_parser_type_id() does not seem to do it for
23614 us. */
23615 cp_parser_skip_to_closing_parenthesis (parser,
23616 /*recovering=*/true,
23617 /*or_comma=*/false,
23618 /*consume_paren=*/false);
23619 }
23620 }
23621
23622 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23623 type_name = build_tree_list (proto_quals, cp_type);
23624 }
23625
23626 return type_name;
23627 }
23628
23629 /* Check to see if TYPE refers to an Objective-C selector name. */
23630
23631 static bool
23632 cp_parser_objc_selector_p (enum cpp_ttype type)
23633 {
23634 return (type == CPP_NAME || type == CPP_KEYWORD
23635 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23636 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23637 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23638 || type == CPP_XOR || type == CPP_XOR_EQ);
23639 }
23640
23641 /* Parse an Objective-C selector. */
23642
23643 static tree
23644 cp_parser_objc_selector (cp_parser* parser)
23645 {
23646 cp_token *token = cp_lexer_consume_token (parser->lexer);
23647
23648 if (!cp_parser_objc_selector_p (token->type))
23649 {
23650 error_at (token->location, "invalid Objective-C++ selector name");
23651 return error_mark_node;
23652 }
23653
23654 /* C++ operator names are allowed to appear in ObjC selectors. */
23655 switch (token->type)
23656 {
23657 case CPP_AND_AND: return get_identifier ("and");
23658 case CPP_AND_EQ: return get_identifier ("and_eq");
23659 case CPP_AND: return get_identifier ("bitand");
23660 case CPP_OR: return get_identifier ("bitor");
23661 case CPP_COMPL: return get_identifier ("compl");
23662 case CPP_NOT: return get_identifier ("not");
23663 case CPP_NOT_EQ: return get_identifier ("not_eq");
23664 case CPP_OR_OR: return get_identifier ("or");
23665 case CPP_OR_EQ: return get_identifier ("or_eq");
23666 case CPP_XOR: return get_identifier ("xor");
23667 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23668 default: return token->u.value;
23669 }
23670 }
23671
23672 /* Parse an Objective-C params list. */
23673
23674 static tree
23675 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23676 {
23677 tree params = NULL_TREE;
23678 bool maybe_unary_selector_p = true;
23679 cp_token *token = cp_lexer_peek_token (parser->lexer);
23680
23681 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23682 {
23683 tree selector = NULL_TREE, type_name, identifier;
23684 tree parm_attr = NULL_TREE;
23685
23686 if (token->keyword == RID_ATTRIBUTE)
23687 break;
23688
23689 if (token->type != CPP_COLON)
23690 selector = cp_parser_objc_selector (parser);
23691
23692 /* Detect if we have a unary selector. */
23693 if (maybe_unary_selector_p
23694 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23695 {
23696 params = selector; /* Might be followed by attributes. */
23697 break;
23698 }
23699
23700 maybe_unary_selector_p = false;
23701 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23702 {
23703 /* Something went quite wrong. There should be a colon
23704 here, but there is not. Stop parsing parameters. */
23705 break;
23706 }
23707 type_name = cp_parser_objc_typename (parser);
23708 /* New ObjC allows attributes on parameters too. */
23709 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23710 parm_attr = cp_parser_attributes_opt (parser);
23711 identifier = cp_parser_identifier (parser);
23712
23713 params
23714 = chainon (params,
23715 objc_build_keyword_decl (selector,
23716 type_name,
23717 identifier,
23718 parm_attr));
23719
23720 token = cp_lexer_peek_token (parser->lexer);
23721 }
23722
23723 if (params == NULL_TREE)
23724 {
23725 cp_parser_error (parser, "objective-c++ method declaration is expected");
23726 return error_mark_node;
23727 }
23728
23729 /* We allow tail attributes for the method. */
23730 if (token->keyword == RID_ATTRIBUTE)
23731 {
23732 *attributes = cp_parser_attributes_opt (parser);
23733 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23734 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23735 return params;
23736 cp_parser_error (parser,
23737 "method attributes must be specified at the end");
23738 return error_mark_node;
23739 }
23740
23741 if (params == NULL_TREE)
23742 {
23743 cp_parser_error (parser, "objective-c++ method declaration is expected");
23744 return error_mark_node;
23745 }
23746 return params;
23747 }
23748
23749 /* Parse the non-keyword Objective-C params. */
23750
23751 static tree
23752 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23753 tree* attributes)
23754 {
23755 tree params = make_node (TREE_LIST);
23756 cp_token *token = cp_lexer_peek_token (parser->lexer);
23757 *ellipsisp = false; /* Initially, assume no ellipsis. */
23758
23759 while (token->type == CPP_COMMA)
23760 {
23761 cp_parameter_declarator *parmdecl;
23762 tree parm;
23763
23764 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23765 token = cp_lexer_peek_token (parser->lexer);
23766
23767 if (token->type == CPP_ELLIPSIS)
23768 {
23769 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23770 *ellipsisp = true;
23771 token = cp_lexer_peek_token (parser->lexer);
23772 break;
23773 }
23774
23775 /* TODO: parse attributes for tail parameters. */
23776 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23777 parm = grokdeclarator (parmdecl->declarator,
23778 &parmdecl->decl_specifiers,
23779 PARM, /*initialized=*/0,
23780 /*attrlist=*/NULL);
23781
23782 chainon (params, build_tree_list (NULL_TREE, parm));
23783 token = cp_lexer_peek_token (parser->lexer);
23784 }
23785
23786 /* We allow tail attributes for the method. */
23787 if (token->keyword == RID_ATTRIBUTE)
23788 {
23789 if (*attributes == NULL_TREE)
23790 {
23791 *attributes = cp_parser_attributes_opt (parser);
23792 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23793 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23794 return params;
23795 }
23796 else
23797 /* We have an error, but parse the attributes, so that we can
23798 carry on. */
23799 *attributes = cp_parser_attributes_opt (parser);
23800
23801 cp_parser_error (parser,
23802 "method attributes must be specified at the end");
23803 return error_mark_node;
23804 }
23805
23806 return params;
23807 }
23808
23809 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23810
23811 static void
23812 cp_parser_objc_interstitial_code (cp_parser* parser)
23813 {
23814 cp_token *token = cp_lexer_peek_token (parser->lexer);
23815
23816 /* If the next token is `extern' and the following token is a string
23817 literal, then we have a linkage specification. */
23818 if (token->keyword == RID_EXTERN
23819 && cp_parser_is_pure_string_literal
23820 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23821 cp_parser_linkage_specification (parser);
23822 /* Handle #pragma, if any. */
23823 else if (token->type == CPP_PRAGMA)
23824 cp_parser_pragma (parser, pragma_external);
23825 /* Allow stray semicolons. */
23826 else if (token->type == CPP_SEMICOLON)
23827 cp_lexer_consume_token (parser->lexer);
23828 /* Mark methods as optional or required, when building protocols. */
23829 else if (token->keyword == RID_AT_OPTIONAL)
23830 {
23831 cp_lexer_consume_token (parser->lexer);
23832 objc_set_method_opt (true);
23833 }
23834 else if (token->keyword == RID_AT_REQUIRED)
23835 {
23836 cp_lexer_consume_token (parser->lexer);
23837 objc_set_method_opt (false);
23838 }
23839 else if (token->keyword == RID_NAMESPACE)
23840 cp_parser_namespace_definition (parser);
23841 /* Other stray characters must generate errors. */
23842 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23843 {
23844 cp_lexer_consume_token (parser->lexer);
23845 error ("stray %qs between Objective-C++ methods",
23846 token->type == CPP_OPEN_BRACE ? "{" : "}");
23847 }
23848 /* Finally, try to parse a block-declaration, or a function-definition. */
23849 else
23850 cp_parser_block_declaration (parser, /*statement_p=*/false);
23851 }
23852
23853 /* Parse a method signature. */
23854
23855 static tree
23856 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23857 {
23858 tree rettype, kwdparms, optparms;
23859 bool ellipsis = false;
23860 bool is_class_method;
23861
23862 is_class_method = cp_parser_objc_method_type (parser);
23863 rettype = cp_parser_objc_typename (parser);
23864 *attributes = NULL_TREE;
23865 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23866 if (kwdparms == error_mark_node)
23867 return error_mark_node;
23868 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23869 if (optparms == error_mark_node)
23870 return error_mark_node;
23871
23872 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23873 }
23874
23875 static bool
23876 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23877 {
23878 tree tattr;
23879 cp_lexer_save_tokens (parser->lexer);
23880 tattr = cp_parser_attributes_opt (parser);
23881 gcc_assert (tattr) ;
23882
23883 /* If the attributes are followed by a method introducer, this is not allowed.
23884 Dump the attributes and flag the situation. */
23885 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23886 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23887 return true;
23888
23889 /* Otherwise, the attributes introduce some interstitial code, possibly so
23890 rewind to allow that check. */
23891 cp_lexer_rollback_tokens (parser->lexer);
23892 return false;
23893 }
23894
23895 /* Parse an Objective-C method prototype list. */
23896
23897 static void
23898 cp_parser_objc_method_prototype_list (cp_parser* parser)
23899 {
23900 cp_token *token = cp_lexer_peek_token (parser->lexer);
23901
23902 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23903 {
23904 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23905 {
23906 tree attributes, sig;
23907 bool is_class_method;
23908 if (token->type == CPP_PLUS)
23909 is_class_method = true;
23910 else
23911 is_class_method = false;
23912 sig = cp_parser_objc_method_signature (parser, &attributes);
23913 if (sig == error_mark_node)
23914 {
23915 cp_parser_skip_to_end_of_block_or_statement (parser);
23916 token = cp_lexer_peek_token (parser->lexer);
23917 continue;
23918 }
23919 objc_add_method_declaration (is_class_method, sig, attributes);
23920 cp_parser_consume_semicolon_at_end_of_statement (parser);
23921 }
23922 else if (token->keyword == RID_AT_PROPERTY)
23923 cp_parser_objc_at_property_declaration (parser);
23924 else if (token->keyword == RID_ATTRIBUTE
23925 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23926 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23927 OPT_Wattributes,
23928 "prefix attributes are ignored for methods");
23929 else
23930 /* Allow for interspersed non-ObjC++ code. */
23931 cp_parser_objc_interstitial_code (parser);
23932
23933 token = cp_lexer_peek_token (parser->lexer);
23934 }
23935
23936 if (token->type != CPP_EOF)
23937 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23938 else
23939 cp_parser_error (parser, "expected %<@end%>");
23940
23941 objc_finish_interface ();
23942 }
23943
23944 /* Parse an Objective-C method definition list. */
23945
23946 static void
23947 cp_parser_objc_method_definition_list (cp_parser* parser)
23948 {
23949 cp_token *token = cp_lexer_peek_token (parser->lexer);
23950
23951 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23952 {
23953 tree meth;
23954
23955 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23956 {
23957 cp_token *ptk;
23958 tree sig, attribute;
23959 bool is_class_method;
23960 if (token->type == CPP_PLUS)
23961 is_class_method = true;
23962 else
23963 is_class_method = false;
23964 push_deferring_access_checks (dk_deferred);
23965 sig = cp_parser_objc_method_signature (parser, &attribute);
23966 if (sig == error_mark_node)
23967 {
23968 cp_parser_skip_to_end_of_block_or_statement (parser);
23969 token = cp_lexer_peek_token (parser->lexer);
23970 continue;
23971 }
23972 objc_start_method_definition (is_class_method, sig, attribute,
23973 NULL_TREE);
23974
23975 /* For historical reasons, we accept an optional semicolon. */
23976 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23977 cp_lexer_consume_token (parser->lexer);
23978
23979 ptk = cp_lexer_peek_token (parser->lexer);
23980 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23981 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23982 {
23983 perform_deferred_access_checks (tf_warning_or_error);
23984 stop_deferring_access_checks ();
23985 meth = cp_parser_function_definition_after_declarator (parser,
23986 false);
23987 pop_deferring_access_checks ();
23988 objc_finish_method_definition (meth);
23989 }
23990 }
23991 /* The following case will be removed once @synthesize is
23992 completely implemented. */
23993 else if (token->keyword == RID_AT_PROPERTY)
23994 cp_parser_objc_at_property_declaration (parser);
23995 else if (token->keyword == RID_AT_SYNTHESIZE)
23996 cp_parser_objc_at_synthesize_declaration (parser);
23997 else if (token->keyword == RID_AT_DYNAMIC)
23998 cp_parser_objc_at_dynamic_declaration (parser);
23999 else if (token->keyword == RID_ATTRIBUTE
24000 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24001 warning_at (token->location, OPT_Wattributes,
24002 "prefix attributes are ignored for methods");
24003 else
24004 /* Allow for interspersed non-ObjC++ code. */
24005 cp_parser_objc_interstitial_code (parser);
24006
24007 token = cp_lexer_peek_token (parser->lexer);
24008 }
24009
24010 if (token->type != CPP_EOF)
24011 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24012 else
24013 cp_parser_error (parser, "expected %<@end%>");
24014
24015 objc_finish_implementation ();
24016 }
24017
24018 /* Parse Objective-C ivars. */
24019
24020 static void
24021 cp_parser_objc_class_ivars (cp_parser* parser)
24022 {
24023 cp_token *token = cp_lexer_peek_token (parser->lexer);
24024
24025 if (token->type != CPP_OPEN_BRACE)
24026 return; /* No ivars specified. */
24027
24028 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24029 token = cp_lexer_peek_token (parser->lexer);
24030
24031 while (token->type != CPP_CLOSE_BRACE
24032 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24033 {
24034 cp_decl_specifier_seq declspecs;
24035 int decl_class_or_enum_p;
24036 tree prefix_attributes;
24037
24038 cp_parser_objc_visibility_spec (parser);
24039
24040 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24041 break;
24042
24043 cp_parser_decl_specifier_seq (parser,
24044 CP_PARSER_FLAGS_OPTIONAL,
24045 &declspecs,
24046 &decl_class_or_enum_p);
24047
24048 /* auto, register, static, extern, mutable. */
24049 if (declspecs.storage_class != sc_none)
24050 {
24051 cp_parser_error (parser, "invalid type for instance variable");
24052 declspecs.storage_class = sc_none;
24053 }
24054
24055 /* __thread. */
24056 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24057 {
24058 cp_parser_error (parser, "invalid type for instance variable");
24059 declspecs.locations[ds_thread] = 0;
24060 }
24061
24062 /* typedef. */
24063 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24064 {
24065 cp_parser_error (parser, "invalid type for instance variable");
24066 declspecs.locations[ds_thread] = 0;
24067 }
24068
24069 prefix_attributes = declspecs.attributes;
24070 declspecs.attributes = NULL_TREE;
24071
24072 /* Keep going until we hit the `;' at the end of the
24073 declaration. */
24074 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24075 {
24076 tree width = NULL_TREE, attributes, first_attribute, decl;
24077 cp_declarator *declarator = NULL;
24078 int ctor_dtor_or_conv_p;
24079
24080 /* Check for a (possibly unnamed) bitfield declaration. */
24081 token = cp_lexer_peek_token (parser->lexer);
24082 if (token->type == CPP_COLON)
24083 goto eat_colon;
24084
24085 if (token->type == CPP_NAME
24086 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24087 == CPP_COLON))
24088 {
24089 /* Get the name of the bitfield. */
24090 declarator = make_id_declarator (NULL_TREE,
24091 cp_parser_identifier (parser),
24092 sfk_none);
24093
24094 eat_colon:
24095 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24096 /* Get the width of the bitfield. */
24097 width
24098 = cp_parser_constant_expression (parser,
24099 /*allow_non_constant=*/false,
24100 NULL);
24101 }
24102 else
24103 {
24104 /* Parse the declarator. */
24105 declarator
24106 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24107 &ctor_dtor_or_conv_p,
24108 /*parenthesized_p=*/NULL,
24109 /*member_p=*/false);
24110 }
24111
24112 /* Look for attributes that apply to the ivar. */
24113 attributes = cp_parser_attributes_opt (parser);
24114 /* Remember which attributes are prefix attributes and
24115 which are not. */
24116 first_attribute = attributes;
24117 /* Combine the attributes. */
24118 attributes = chainon (prefix_attributes, attributes);
24119
24120 if (width)
24121 /* Create the bitfield declaration. */
24122 decl = grokbitfield (declarator, &declspecs,
24123 width,
24124 attributes);
24125 else
24126 decl = grokfield (declarator, &declspecs,
24127 NULL_TREE, /*init_const_expr_p=*/false,
24128 NULL_TREE, attributes);
24129
24130 /* Add the instance variable. */
24131 if (decl != error_mark_node && decl != NULL_TREE)
24132 objc_add_instance_variable (decl);
24133
24134 /* Reset PREFIX_ATTRIBUTES. */
24135 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24136 attributes = TREE_CHAIN (attributes);
24137 if (attributes)
24138 TREE_CHAIN (attributes) = NULL_TREE;
24139
24140 token = cp_lexer_peek_token (parser->lexer);
24141
24142 if (token->type == CPP_COMMA)
24143 {
24144 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24145 continue;
24146 }
24147 break;
24148 }
24149
24150 cp_parser_consume_semicolon_at_end_of_statement (parser);
24151 token = cp_lexer_peek_token (parser->lexer);
24152 }
24153
24154 if (token->keyword == RID_AT_END)
24155 cp_parser_error (parser, "expected %<}%>");
24156
24157 /* Do not consume the RID_AT_END, so it will be read again as terminating
24158 the @interface of @implementation. */
24159 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24160 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24161
24162 /* For historical reasons, we accept an optional semicolon. */
24163 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24164 cp_lexer_consume_token (parser->lexer);
24165 }
24166
24167 /* Parse an Objective-C protocol declaration. */
24168
24169 static void
24170 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24171 {
24172 tree proto, protorefs;
24173 cp_token *tok;
24174
24175 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24177 {
24178 tok = cp_lexer_peek_token (parser->lexer);
24179 error_at (tok->location, "identifier expected after %<@protocol%>");
24180 cp_parser_consume_semicolon_at_end_of_statement (parser);
24181 return;
24182 }
24183
24184 /* See if we have a forward declaration or a definition. */
24185 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24186
24187 /* Try a forward declaration first. */
24188 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24189 {
24190 while (true)
24191 {
24192 tree id;
24193
24194 id = cp_parser_identifier (parser);
24195 if (id == error_mark_node)
24196 break;
24197
24198 objc_declare_protocol (id, attributes);
24199
24200 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24201 cp_lexer_consume_token (parser->lexer);
24202 else
24203 break;
24204 }
24205 cp_parser_consume_semicolon_at_end_of_statement (parser);
24206 }
24207
24208 /* Ok, we got a full-fledged definition (or at least should). */
24209 else
24210 {
24211 proto = cp_parser_identifier (parser);
24212 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24213 objc_start_protocol (proto, protorefs, attributes);
24214 cp_parser_objc_method_prototype_list (parser);
24215 }
24216 }
24217
24218 /* Parse an Objective-C superclass or category. */
24219
24220 static void
24221 cp_parser_objc_superclass_or_category (cp_parser *parser,
24222 bool iface_p,
24223 tree *super,
24224 tree *categ, bool *is_class_extension)
24225 {
24226 cp_token *next = cp_lexer_peek_token (parser->lexer);
24227
24228 *super = *categ = NULL_TREE;
24229 *is_class_extension = false;
24230 if (next->type == CPP_COLON)
24231 {
24232 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24233 *super = cp_parser_identifier (parser);
24234 }
24235 else if (next->type == CPP_OPEN_PAREN)
24236 {
24237 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24238
24239 /* If there is no category name, and this is an @interface, we
24240 have a class extension. */
24241 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24242 {
24243 *categ = NULL_TREE;
24244 *is_class_extension = true;
24245 }
24246 else
24247 *categ = cp_parser_identifier (parser);
24248
24249 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24250 }
24251 }
24252
24253 /* Parse an Objective-C class interface. */
24254
24255 static void
24256 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24257 {
24258 tree name, super, categ, protos;
24259 bool is_class_extension;
24260
24261 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24262 name = cp_parser_identifier (parser);
24263 if (name == error_mark_node)
24264 {
24265 /* It's hard to recover because even if valid @interface stuff
24266 is to follow, we can't compile it (or validate it) if we
24267 don't even know which class it refers to. Let's assume this
24268 was a stray '@interface' token in the stream and skip it.
24269 */
24270 return;
24271 }
24272 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24273 &is_class_extension);
24274 protos = cp_parser_objc_protocol_refs_opt (parser);
24275
24276 /* We have either a class or a category on our hands. */
24277 if (categ || is_class_extension)
24278 objc_start_category_interface (name, categ, protos, attributes);
24279 else
24280 {
24281 objc_start_class_interface (name, super, protos, attributes);
24282 /* Handle instance variable declarations, if any. */
24283 cp_parser_objc_class_ivars (parser);
24284 objc_continue_interface ();
24285 }
24286
24287 cp_parser_objc_method_prototype_list (parser);
24288 }
24289
24290 /* Parse an Objective-C class implementation. */
24291
24292 static void
24293 cp_parser_objc_class_implementation (cp_parser* parser)
24294 {
24295 tree name, super, categ;
24296 bool is_class_extension;
24297
24298 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24299 name = cp_parser_identifier (parser);
24300 if (name == error_mark_node)
24301 {
24302 /* It's hard to recover because even if valid @implementation
24303 stuff is to follow, we can't compile it (or validate it) if
24304 we don't even know which class it refers to. Let's assume
24305 this was a stray '@implementation' token in the stream and
24306 skip it.
24307 */
24308 return;
24309 }
24310 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24311 &is_class_extension);
24312
24313 /* We have either a class or a category on our hands. */
24314 if (categ)
24315 objc_start_category_implementation (name, categ);
24316 else
24317 {
24318 objc_start_class_implementation (name, super);
24319 /* Handle instance variable declarations, if any. */
24320 cp_parser_objc_class_ivars (parser);
24321 objc_continue_implementation ();
24322 }
24323
24324 cp_parser_objc_method_definition_list (parser);
24325 }
24326
24327 /* Consume the @end token and finish off the implementation. */
24328
24329 static void
24330 cp_parser_objc_end_implementation (cp_parser* parser)
24331 {
24332 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24333 objc_finish_implementation ();
24334 }
24335
24336 /* Parse an Objective-C declaration. */
24337
24338 static void
24339 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24340 {
24341 /* Try to figure out what kind of declaration is present. */
24342 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24343
24344 if (attributes)
24345 switch (kwd->keyword)
24346 {
24347 case RID_AT_ALIAS:
24348 case RID_AT_CLASS:
24349 case RID_AT_END:
24350 error_at (kwd->location, "attributes may not be specified before"
24351 " the %<@%D%> Objective-C++ keyword",
24352 kwd->u.value);
24353 attributes = NULL;
24354 break;
24355 case RID_AT_IMPLEMENTATION:
24356 warning_at (kwd->location, OPT_Wattributes,
24357 "prefix attributes are ignored before %<@%D%>",
24358 kwd->u.value);
24359 attributes = NULL;
24360 default:
24361 break;
24362 }
24363
24364 switch (kwd->keyword)
24365 {
24366 case RID_AT_ALIAS:
24367 cp_parser_objc_alias_declaration (parser);
24368 break;
24369 case RID_AT_CLASS:
24370 cp_parser_objc_class_declaration (parser);
24371 break;
24372 case RID_AT_PROTOCOL:
24373 cp_parser_objc_protocol_declaration (parser, attributes);
24374 break;
24375 case RID_AT_INTERFACE:
24376 cp_parser_objc_class_interface (parser, attributes);
24377 break;
24378 case RID_AT_IMPLEMENTATION:
24379 cp_parser_objc_class_implementation (parser);
24380 break;
24381 case RID_AT_END:
24382 cp_parser_objc_end_implementation (parser);
24383 break;
24384 default:
24385 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24386 kwd->u.value);
24387 cp_parser_skip_to_end_of_block_or_statement (parser);
24388 }
24389 }
24390
24391 /* Parse an Objective-C try-catch-finally statement.
24392
24393 objc-try-catch-finally-stmt:
24394 @try compound-statement objc-catch-clause-seq [opt]
24395 objc-finally-clause [opt]
24396
24397 objc-catch-clause-seq:
24398 objc-catch-clause objc-catch-clause-seq [opt]
24399
24400 objc-catch-clause:
24401 @catch ( objc-exception-declaration ) compound-statement
24402
24403 objc-finally-clause:
24404 @finally compound-statement
24405
24406 objc-exception-declaration:
24407 parameter-declaration
24408 '...'
24409
24410 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24411
24412 Returns NULL_TREE.
24413
24414 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24415 for C. Keep them in sync. */
24416
24417 static tree
24418 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24419 {
24420 location_t location;
24421 tree stmt;
24422
24423 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24424 location = cp_lexer_peek_token (parser->lexer)->location;
24425 objc_maybe_warn_exceptions (location);
24426 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24427 node, lest it get absorbed into the surrounding block. */
24428 stmt = push_stmt_list ();
24429 cp_parser_compound_statement (parser, NULL, false, false);
24430 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24431
24432 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24433 {
24434 cp_parameter_declarator *parm;
24435 tree parameter_declaration = error_mark_node;
24436 bool seen_open_paren = false;
24437
24438 cp_lexer_consume_token (parser->lexer);
24439 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24440 seen_open_paren = true;
24441 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24442 {
24443 /* We have "@catch (...)" (where the '...' are literally
24444 what is in the code). Skip the '...'.
24445 parameter_declaration is set to NULL_TREE, and
24446 objc_being_catch_clauses() knows that that means
24447 '...'. */
24448 cp_lexer_consume_token (parser->lexer);
24449 parameter_declaration = NULL_TREE;
24450 }
24451 else
24452 {
24453 /* We have "@catch (NSException *exception)" or something
24454 like that. Parse the parameter declaration. */
24455 parm = cp_parser_parameter_declaration (parser, false, NULL);
24456 if (parm == NULL)
24457 parameter_declaration = error_mark_node;
24458 else
24459 parameter_declaration = grokdeclarator (parm->declarator,
24460 &parm->decl_specifiers,
24461 PARM, /*initialized=*/0,
24462 /*attrlist=*/NULL);
24463 }
24464 if (seen_open_paren)
24465 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24466 else
24467 {
24468 /* If there was no open parenthesis, we are recovering from
24469 an error, and we are trying to figure out what mistake
24470 the user has made. */
24471
24472 /* If there is an immediate closing parenthesis, the user
24473 probably forgot the opening one (ie, they typed "@catch
24474 NSException *e)". Parse the closing parenthesis and keep
24475 going. */
24476 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24477 cp_lexer_consume_token (parser->lexer);
24478
24479 /* If these is no immediate closing parenthesis, the user
24480 probably doesn't know that parenthesis are required at
24481 all (ie, they typed "@catch NSException *e"). So, just
24482 forget about the closing parenthesis and keep going. */
24483 }
24484 objc_begin_catch_clause (parameter_declaration);
24485 cp_parser_compound_statement (parser, NULL, false, false);
24486 objc_finish_catch_clause ();
24487 }
24488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24489 {
24490 cp_lexer_consume_token (parser->lexer);
24491 location = cp_lexer_peek_token (parser->lexer)->location;
24492 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24493 node, lest it get absorbed into the surrounding block. */
24494 stmt = push_stmt_list ();
24495 cp_parser_compound_statement (parser, NULL, false, false);
24496 objc_build_finally_clause (location, pop_stmt_list (stmt));
24497 }
24498
24499 return objc_finish_try_stmt ();
24500 }
24501
24502 /* Parse an Objective-C synchronized statement.
24503
24504 objc-synchronized-stmt:
24505 @synchronized ( expression ) compound-statement
24506
24507 Returns NULL_TREE. */
24508
24509 static tree
24510 cp_parser_objc_synchronized_statement (cp_parser *parser)
24511 {
24512 location_t location;
24513 tree lock, stmt;
24514
24515 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24516
24517 location = cp_lexer_peek_token (parser->lexer)->location;
24518 objc_maybe_warn_exceptions (location);
24519 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24520 lock = cp_parser_expression (parser, false, NULL);
24521 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24522
24523 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24524 node, lest it get absorbed into the surrounding block. */
24525 stmt = push_stmt_list ();
24526 cp_parser_compound_statement (parser, NULL, false, false);
24527
24528 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24529 }
24530
24531 /* Parse an Objective-C throw statement.
24532
24533 objc-throw-stmt:
24534 @throw assignment-expression [opt] ;
24535
24536 Returns a constructed '@throw' statement. */
24537
24538 static tree
24539 cp_parser_objc_throw_statement (cp_parser *parser)
24540 {
24541 tree expr = NULL_TREE;
24542 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24543
24544 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24545
24546 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24547 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24548
24549 cp_parser_consume_semicolon_at_end_of_statement (parser);
24550
24551 return objc_build_throw_stmt (loc, expr);
24552 }
24553
24554 /* Parse an Objective-C statement. */
24555
24556 static tree
24557 cp_parser_objc_statement (cp_parser * parser)
24558 {
24559 /* Try to figure out what kind of declaration is present. */
24560 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24561
24562 switch (kwd->keyword)
24563 {
24564 case RID_AT_TRY:
24565 return cp_parser_objc_try_catch_finally_statement (parser);
24566 case RID_AT_SYNCHRONIZED:
24567 return cp_parser_objc_synchronized_statement (parser);
24568 case RID_AT_THROW:
24569 return cp_parser_objc_throw_statement (parser);
24570 default:
24571 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24572 kwd->u.value);
24573 cp_parser_skip_to_end_of_block_or_statement (parser);
24574 }
24575
24576 return error_mark_node;
24577 }
24578
24579 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24580 look ahead to see if an objc keyword follows the attributes. This
24581 is to detect the use of prefix attributes on ObjC @interface and
24582 @protocol. */
24583
24584 static bool
24585 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24586 {
24587 cp_lexer_save_tokens (parser->lexer);
24588 *attrib = cp_parser_attributes_opt (parser);
24589 gcc_assert (*attrib);
24590 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24591 {
24592 cp_lexer_commit_tokens (parser->lexer);
24593 return true;
24594 }
24595 cp_lexer_rollback_tokens (parser->lexer);
24596 return false;
24597 }
24598
24599 /* This routine is a minimal replacement for
24600 c_parser_struct_declaration () used when parsing the list of
24601 types/names or ObjC++ properties. For example, when parsing the
24602 code
24603
24604 @property (readonly) int a, b, c;
24605
24606 this function is responsible for parsing "int a, int b, int c" and
24607 returning the declarations as CHAIN of DECLs.
24608
24609 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24610 similar parsing. */
24611 static tree
24612 cp_parser_objc_struct_declaration (cp_parser *parser)
24613 {
24614 tree decls = NULL_TREE;
24615 cp_decl_specifier_seq declspecs;
24616 int decl_class_or_enum_p;
24617 tree prefix_attributes;
24618
24619 cp_parser_decl_specifier_seq (parser,
24620 CP_PARSER_FLAGS_NONE,
24621 &declspecs,
24622 &decl_class_or_enum_p);
24623
24624 if (declspecs.type == error_mark_node)
24625 return error_mark_node;
24626
24627 /* auto, register, static, extern, mutable. */
24628 if (declspecs.storage_class != sc_none)
24629 {
24630 cp_parser_error (parser, "invalid type for property");
24631 declspecs.storage_class = sc_none;
24632 }
24633
24634 /* __thread. */
24635 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24636 {
24637 cp_parser_error (parser, "invalid type for property");
24638 declspecs.locations[ds_thread] = 0;
24639 }
24640
24641 /* typedef. */
24642 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24643 {
24644 cp_parser_error (parser, "invalid type for property");
24645 declspecs.locations[ds_typedef] = 0;
24646 }
24647
24648 prefix_attributes = declspecs.attributes;
24649 declspecs.attributes = NULL_TREE;
24650
24651 /* Keep going until we hit the `;' at the end of the declaration. */
24652 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24653 {
24654 tree attributes, first_attribute, decl;
24655 cp_declarator *declarator;
24656 cp_token *token;
24657
24658 /* Parse the declarator. */
24659 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24660 NULL, NULL, false);
24661
24662 /* Look for attributes that apply to the ivar. */
24663 attributes = cp_parser_attributes_opt (parser);
24664 /* Remember which attributes are prefix attributes and
24665 which are not. */
24666 first_attribute = attributes;
24667 /* Combine the attributes. */
24668 attributes = chainon (prefix_attributes, attributes);
24669
24670 decl = grokfield (declarator, &declspecs,
24671 NULL_TREE, /*init_const_expr_p=*/false,
24672 NULL_TREE, attributes);
24673
24674 if (decl == error_mark_node || decl == NULL_TREE)
24675 return error_mark_node;
24676
24677 /* Reset PREFIX_ATTRIBUTES. */
24678 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24679 attributes = TREE_CHAIN (attributes);
24680 if (attributes)
24681 TREE_CHAIN (attributes) = NULL_TREE;
24682
24683 DECL_CHAIN (decl) = decls;
24684 decls = decl;
24685
24686 token = cp_lexer_peek_token (parser->lexer);
24687 if (token->type == CPP_COMMA)
24688 {
24689 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24690 continue;
24691 }
24692 else
24693 break;
24694 }
24695 return decls;
24696 }
24697
24698 /* Parse an Objective-C @property declaration. The syntax is:
24699
24700 objc-property-declaration:
24701 '@property' objc-property-attributes[opt] struct-declaration ;
24702
24703 objc-property-attributes:
24704 '(' objc-property-attribute-list ')'
24705
24706 objc-property-attribute-list:
24707 objc-property-attribute
24708 objc-property-attribute-list, objc-property-attribute
24709
24710 objc-property-attribute
24711 'getter' = identifier
24712 'setter' = identifier
24713 'readonly'
24714 'readwrite'
24715 'assign'
24716 'retain'
24717 'copy'
24718 'nonatomic'
24719
24720 For example:
24721 @property NSString *name;
24722 @property (readonly) id object;
24723 @property (retain, nonatomic, getter=getTheName) id name;
24724 @property int a, b, c;
24725
24726 PS: This function is identical to
24727 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24728 static void
24729 cp_parser_objc_at_property_declaration (cp_parser *parser)
24730 {
24731 /* The following variables hold the attributes of the properties as
24732 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24733 seen. When we see an attribute, we set them to 'true' (if they
24734 are boolean properties) or to the identifier (if they have an
24735 argument, ie, for getter and setter). Note that here we only
24736 parse the list of attributes, check the syntax and accumulate the
24737 attributes that we find. objc_add_property_declaration() will
24738 then process the information. */
24739 bool property_assign = false;
24740 bool property_copy = false;
24741 tree property_getter_ident = NULL_TREE;
24742 bool property_nonatomic = false;
24743 bool property_readonly = false;
24744 bool property_readwrite = false;
24745 bool property_retain = false;
24746 tree property_setter_ident = NULL_TREE;
24747
24748 /* 'properties' is the list of properties that we read. Usually a
24749 single one, but maybe more (eg, in "@property int a, b, c;" there
24750 are three). */
24751 tree properties;
24752 location_t loc;
24753
24754 loc = cp_lexer_peek_token (parser->lexer)->location;
24755
24756 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24757
24758 /* Parse the optional attribute list... */
24759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24760 {
24761 /* Eat the '('. */
24762 cp_lexer_consume_token (parser->lexer);
24763
24764 while (true)
24765 {
24766 bool syntax_error = false;
24767 cp_token *token = cp_lexer_peek_token (parser->lexer);
24768 enum rid keyword;
24769
24770 if (token->type != CPP_NAME)
24771 {
24772 cp_parser_error (parser, "expected identifier");
24773 break;
24774 }
24775 keyword = C_RID_CODE (token->u.value);
24776 cp_lexer_consume_token (parser->lexer);
24777 switch (keyword)
24778 {
24779 case RID_ASSIGN: property_assign = true; break;
24780 case RID_COPY: property_copy = true; break;
24781 case RID_NONATOMIC: property_nonatomic = true; break;
24782 case RID_READONLY: property_readonly = true; break;
24783 case RID_READWRITE: property_readwrite = true; break;
24784 case RID_RETAIN: property_retain = true; break;
24785
24786 case RID_GETTER:
24787 case RID_SETTER:
24788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24789 {
24790 if (keyword == RID_GETTER)
24791 cp_parser_error (parser,
24792 "missing %<=%> (after %<getter%> attribute)");
24793 else
24794 cp_parser_error (parser,
24795 "missing %<=%> (after %<setter%> attribute)");
24796 syntax_error = true;
24797 break;
24798 }
24799 cp_lexer_consume_token (parser->lexer); /* eat the = */
24800 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24801 {
24802 cp_parser_error (parser, "expected identifier");
24803 syntax_error = true;
24804 break;
24805 }
24806 if (keyword == RID_SETTER)
24807 {
24808 if (property_setter_ident != NULL_TREE)
24809 {
24810 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24811 cp_lexer_consume_token (parser->lexer);
24812 }
24813 else
24814 property_setter_ident = cp_parser_objc_selector (parser);
24815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24816 cp_parser_error (parser, "setter name must terminate with %<:%>");
24817 else
24818 cp_lexer_consume_token (parser->lexer);
24819 }
24820 else
24821 {
24822 if (property_getter_ident != NULL_TREE)
24823 {
24824 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24825 cp_lexer_consume_token (parser->lexer);
24826 }
24827 else
24828 property_getter_ident = cp_parser_objc_selector (parser);
24829 }
24830 break;
24831 default:
24832 cp_parser_error (parser, "unknown property attribute");
24833 syntax_error = true;
24834 break;
24835 }
24836
24837 if (syntax_error)
24838 break;
24839
24840 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24841 cp_lexer_consume_token (parser->lexer);
24842 else
24843 break;
24844 }
24845
24846 /* FIXME: "@property (setter, assign);" will generate a spurious
24847 "error: expected ‘)’ before ‘,’ token". This is because
24848 cp_parser_require, unlike the C counterpart, will produce an
24849 error even if we are in error recovery. */
24850 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24851 {
24852 cp_parser_skip_to_closing_parenthesis (parser,
24853 /*recovering=*/true,
24854 /*or_comma=*/false,
24855 /*consume_paren=*/true);
24856 }
24857 }
24858
24859 /* ... and the property declaration(s). */
24860 properties = cp_parser_objc_struct_declaration (parser);
24861
24862 if (properties == error_mark_node)
24863 {
24864 cp_parser_skip_to_end_of_statement (parser);
24865 /* If the next token is now a `;', consume it. */
24866 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24867 cp_lexer_consume_token (parser->lexer);
24868 return;
24869 }
24870
24871 if (properties == NULL_TREE)
24872 cp_parser_error (parser, "expected identifier");
24873 else
24874 {
24875 /* Comma-separated properties are chained together in
24876 reverse order; add them one by one. */
24877 properties = nreverse (properties);
24878
24879 for (; properties; properties = TREE_CHAIN (properties))
24880 objc_add_property_declaration (loc, copy_node (properties),
24881 property_readonly, property_readwrite,
24882 property_assign, property_retain,
24883 property_copy, property_nonatomic,
24884 property_getter_ident, property_setter_ident);
24885 }
24886
24887 cp_parser_consume_semicolon_at_end_of_statement (parser);
24888 }
24889
24890 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24891
24892 objc-synthesize-declaration:
24893 @synthesize objc-synthesize-identifier-list ;
24894
24895 objc-synthesize-identifier-list:
24896 objc-synthesize-identifier
24897 objc-synthesize-identifier-list, objc-synthesize-identifier
24898
24899 objc-synthesize-identifier
24900 identifier
24901 identifier = identifier
24902
24903 For example:
24904 @synthesize MyProperty;
24905 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24906
24907 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24908 for C. Keep them in sync.
24909 */
24910 static void
24911 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24912 {
24913 tree list = NULL_TREE;
24914 location_t loc;
24915 loc = cp_lexer_peek_token (parser->lexer)->location;
24916
24917 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24918 while (true)
24919 {
24920 tree property, ivar;
24921 property = cp_parser_identifier (parser);
24922 if (property == error_mark_node)
24923 {
24924 cp_parser_consume_semicolon_at_end_of_statement (parser);
24925 return;
24926 }
24927 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24928 {
24929 cp_lexer_consume_token (parser->lexer);
24930 ivar = cp_parser_identifier (parser);
24931 if (ivar == error_mark_node)
24932 {
24933 cp_parser_consume_semicolon_at_end_of_statement (parser);
24934 return;
24935 }
24936 }
24937 else
24938 ivar = NULL_TREE;
24939 list = chainon (list, build_tree_list (ivar, property));
24940 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24941 cp_lexer_consume_token (parser->lexer);
24942 else
24943 break;
24944 }
24945 cp_parser_consume_semicolon_at_end_of_statement (parser);
24946 objc_add_synthesize_declaration (loc, list);
24947 }
24948
24949 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24950
24951 objc-dynamic-declaration:
24952 @dynamic identifier-list ;
24953
24954 For example:
24955 @dynamic MyProperty;
24956 @dynamic MyProperty, AnotherProperty;
24957
24958 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24959 for C. Keep them in sync.
24960 */
24961 static void
24962 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24963 {
24964 tree list = NULL_TREE;
24965 location_t loc;
24966 loc = cp_lexer_peek_token (parser->lexer)->location;
24967
24968 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24969 while (true)
24970 {
24971 tree property;
24972 property = cp_parser_identifier (parser);
24973 if (property == error_mark_node)
24974 {
24975 cp_parser_consume_semicolon_at_end_of_statement (parser);
24976 return;
24977 }
24978 list = chainon (list, build_tree_list (NULL, property));
24979 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24980 cp_lexer_consume_token (parser->lexer);
24981 else
24982 break;
24983 }
24984 cp_parser_consume_semicolon_at_end_of_statement (parser);
24985 objc_add_dynamic_declaration (loc, list);
24986 }
24987
24988 \f
24989 /* OpenMP 2.5 parsing routines. */
24990
24991 /* Returns name of the next clause.
24992 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24993 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24994 returned and the token is consumed. */
24995
24996 static pragma_omp_clause
24997 cp_parser_omp_clause_name (cp_parser *parser)
24998 {
24999 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25000
25001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25002 result = PRAGMA_OMP_CLAUSE_IF;
25003 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25004 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25005 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25006 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25007 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25008 {
25009 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25010 const char *p = IDENTIFIER_POINTER (id);
25011
25012 switch (p[0])
25013 {
25014 case 'c':
25015 if (!strcmp ("collapse", p))
25016 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25017 else if (!strcmp ("copyin", p))
25018 result = PRAGMA_OMP_CLAUSE_COPYIN;
25019 else if (!strcmp ("copyprivate", p))
25020 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25021 break;
25022 case 'f':
25023 if (!strcmp ("final", p))
25024 result = PRAGMA_OMP_CLAUSE_FINAL;
25025 else if (!strcmp ("firstprivate", p))
25026 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25027 break;
25028 case 'l':
25029 if (!strcmp ("lastprivate", p))
25030 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25031 break;
25032 case 'm':
25033 if (!strcmp ("mergeable", p))
25034 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25035 break;
25036 case 'n':
25037 if (!strcmp ("nowait", p))
25038 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25039 else if (!strcmp ("num_threads", p))
25040 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25041 break;
25042 case 'o':
25043 if (!strcmp ("ordered", p))
25044 result = PRAGMA_OMP_CLAUSE_ORDERED;
25045 break;
25046 case 'r':
25047 if (!strcmp ("reduction", p))
25048 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25049 break;
25050 case 's':
25051 if (!strcmp ("schedule", p))
25052 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25053 else if (!strcmp ("shared", p))
25054 result = PRAGMA_OMP_CLAUSE_SHARED;
25055 break;
25056 case 'u':
25057 if (!strcmp ("untied", p))
25058 result = PRAGMA_OMP_CLAUSE_UNTIED;
25059 break;
25060 }
25061 }
25062
25063 if (result != PRAGMA_OMP_CLAUSE_NONE)
25064 cp_lexer_consume_token (parser->lexer);
25065
25066 return result;
25067 }
25068
25069 /* Validate that a clause of the given type does not already exist. */
25070
25071 static void
25072 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25073 const char *name, location_t location)
25074 {
25075 tree c;
25076
25077 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25078 if (OMP_CLAUSE_CODE (c) == code)
25079 {
25080 error_at (location, "too many %qs clauses", name);
25081 break;
25082 }
25083 }
25084
25085 /* OpenMP 2.5:
25086 variable-list:
25087 identifier
25088 variable-list , identifier
25089
25090 In addition, we match a closing parenthesis. An opening parenthesis
25091 will have been consumed by the caller.
25092
25093 If KIND is nonzero, create the appropriate node and install the decl
25094 in OMP_CLAUSE_DECL and add the node to the head of the list.
25095
25096 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25097 return the list created. */
25098
25099 static tree
25100 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25101 tree list)
25102 {
25103 cp_token *token;
25104 while (1)
25105 {
25106 tree name, decl;
25107
25108 token = cp_lexer_peek_token (parser->lexer);
25109 name = cp_parser_id_expression (parser, /*template_p=*/false,
25110 /*check_dependency_p=*/true,
25111 /*template_p=*/NULL,
25112 /*declarator_p=*/false,
25113 /*optional_p=*/false);
25114 if (name == error_mark_node)
25115 goto skip_comma;
25116
25117 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25118 if (decl == error_mark_node)
25119 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25120 token->location);
25121 else if (kind != 0)
25122 {
25123 tree u = build_omp_clause (token->location, kind);
25124 OMP_CLAUSE_DECL (u) = decl;
25125 OMP_CLAUSE_CHAIN (u) = list;
25126 list = u;
25127 }
25128 else
25129 list = tree_cons (decl, NULL_TREE, list);
25130
25131 get_comma:
25132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25133 break;
25134 cp_lexer_consume_token (parser->lexer);
25135 }
25136
25137 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25138 {
25139 int ending;
25140
25141 /* Try to resync to an unnested comma. Copied from
25142 cp_parser_parenthesized_expression_list. */
25143 skip_comma:
25144 ending = cp_parser_skip_to_closing_parenthesis (parser,
25145 /*recovering=*/true,
25146 /*or_comma=*/true,
25147 /*consume_paren=*/true);
25148 if (ending < 0)
25149 goto get_comma;
25150 }
25151
25152 return list;
25153 }
25154
25155 /* Similarly, but expect leading and trailing parenthesis. This is a very
25156 common case for omp clauses. */
25157
25158 static tree
25159 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25160 {
25161 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25162 return cp_parser_omp_var_list_no_open (parser, kind, list);
25163 return list;
25164 }
25165
25166 /* OpenMP 3.0:
25167 collapse ( constant-expression ) */
25168
25169 static tree
25170 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25171 {
25172 tree c, num;
25173 location_t loc;
25174 HOST_WIDE_INT n;
25175
25176 loc = cp_lexer_peek_token (parser->lexer)->location;
25177 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25178 return list;
25179
25180 num = cp_parser_constant_expression (parser, false, NULL);
25181
25182 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25183 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25184 /*or_comma=*/false,
25185 /*consume_paren=*/true);
25186
25187 if (num == error_mark_node)
25188 return list;
25189 num = fold_non_dependent_expr (num);
25190 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25191 || !host_integerp (num, 0)
25192 || (n = tree_low_cst (num, 0)) <= 0
25193 || (int) n != n)
25194 {
25195 error_at (loc, "collapse argument needs positive constant integer expression");
25196 return list;
25197 }
25198
25199 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25200 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25201 OMP_CLAUSE_CHAIN (c) = list;
25202 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25203
25204 return c;
25205 }
25206
25207 /* OpenMP 2.5:
25208 default ( shared | none ) */
25209
25210 static tree
25211 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25212 {
25213 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25214 tree c;
25215
25216 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25217 return list;
25218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25219 {
25220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25221 const char *p = IDENTIFIER_POINTER (id);
25222
25223 switch (p[0])
25224 {
25225 case 'n':
25226 if (strcmp ("none", p) != 0)
25227 goto invalid_kind;
25228 kind = OMP_CLAUSE_DEFAULT_NONE;
25229 break;
25230
25231 case 's':
25232 if (strcmp ("shared", p) != 0)
25233 goto invalid_kind;
25234 kind = OMP_CLAUSE_DEFAULT_SHARED;
25235 break;
25236
25237 default:
25238 goto invalid_kind;
25239 }
25240
25241 cp_lexer_consume_token (parser->lexer);
25242 }
25243 else
25244 {
25245 invalid_kind:
25246 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25247 }
25248
25249 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25250 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25251 /*or_comma=*/false,
25252 /*consume_paren=*/true);
25253
25254 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25255 return list;
25256
25257 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25258 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25259 OMP_CLAUSE_CHAIN (c) = list;
25260 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25261
25262 return c;
25263 }
25264
25265 /* OpenMP 3.1:
25266 final ( expression ) */
25267
25268 static tree
25269 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25270 {
25271 tree t, c;
25272
25273 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25274 return list;
25275
25276 t = cp_parser_condition (parser);
25277
25278 if (t == error_mark_node
25279 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25280 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25281 /*or_comma=*/false,
25282 /*consume_paren=*/true);
25283
25284 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25285
25286 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25287 OMP_CLAUSE_FINAL_EXPR (c) = t;
25288 OMP_CLAUSE_CHAIN (c) = list;
25289
25290 return c;
25291 }
25292
25293 /* OpenMP 2.5:
25294 if ( expression ) */
25295
25296 static tree
25297 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25298 {
25299 tree t, c;
25300
25301 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25302 return list;
25303
25304 t = cp_parser_condition (parser);
25305
25306 if (t == error_mark_node
25307 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25308 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25309 /*or_comma=*/false,
25310 /*consume_paren=*/true);
25311
25312 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25313
25314 c = build_omp_clause (location, OMP_CLAUSE_IF);
25315 OMP_CLAUSE_IF_EXPR (c) = t;
25316 OMP_CLAUSE_CHAIN (c) = list;
25317
25318 return c;
25319 }
25320
25321 /* OpenMP 3.1:
25322 mergeable */
25323
25324 static tree
25325 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25326 tree list, location_t location)
25327 {
25328 tree c;
25329
25330 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25331 location);
25332
25333 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25334 OMP_CLAUSE_CHAIN (c) = list;
25335 return c;
25336 }
25337
25338 /* OpenMP 2.5:
25339 nowait */
25340
25341 static tree
25342 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25343 tree list, location_t location)
25344 {
25345 tree c;
25346
25347 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25348
25349 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25350 OMP_CLAUSE_CHAIN (c) = list;
25351 return c;
25352 }
25353
25354 /* OpenMP 2.5:
25355 num_threads ( expression ) */
25356
25357 static tree
25358 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25359 location_t location)
25360 {
25361 tree t, c;
25362
25363 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25364 return list;
25365
25366 t = cp_parser_expression (parser, false, NULL);
25367
25368 if (t == error_mark_node
25369 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25370 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25371 /*or_comma=*/false,
25372 /*consume_paren=*/true);
25373
25374 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25375 "num_threads", location);
25376
25377 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25378 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25379 OMP_CLAUSE_CHAIN (c) = list;
25380
25381 return c;
25382 }
25383
25384 /* OpenMP 2.5:
25385 ordered */
25386
25387 static tree
25388 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25389 tree list, location_t location)
25390 {
25391 tree c;
25392
25393 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25394 "ordered", location);
25395
25396 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25397 OMP_CLAUSE_CHAIN (c) = list;
25398 return c;
25399 }
25400
25401 /* OpenMP 2.5:
25402 reduction ( reduction-operator : variable-list )
25403
25404 reduction-operator:
25405 One of: + * - & ^ | && ||
25406
25407 OpenMP 3.1:
25408
25409 reduction-operator:
25410 One of: + * - & ^ | && || min max */
25411
25412 static tree
25413 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25414 {
25415 enum tree_code code;
25416 tree nlist, c;
25417
25418 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25419 return list;
25420
25421 switch (cp_lexer_peek_token (parser->lexer)->type)
25422 {
25423 case CPP_PLUS:
25424 code = PLUS_EXPR;
25425 break;
25426 case CPP_MULT:
25427 code = MULT_EXPR;
25428 break;
25429 case CPP_MINUS:
25430 code = MINUS_EXPR;
25431 break;
25432 case CPP_AND:
25433 code = BIT_AND_EXPR;
25434 break;
25435 case CPP_XOR:
25436 code = BIT_XOR_EXPR;
25437 break;
25438 case CPP_OR:
25439 code = BIT_IOR_EXPR;
25440 break;
25441 case CPP_AND_AND:
25442 code = TRUTH_ANDIF_EXPR;
25443 break;
25444 case CPP_OR_OR:
25445 code = TRUTH_ORIF_EXPR;
25446 break;
25447 case CPP_NAME:
25448 {
25449 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25450 const char *p = IDENTIFIER_POINTER (id);
25451
25452 if (strcmp (p, "min") == 0)
25453 {
25454 code = MIN_EXPR;
25455 break;
25456 }
25457 if (strcmp (p, "max") == 0)
25458 {
25459 code = MAX_EXPR;
25460 break;
25461 }
25462 }
25463 /* FALLTHROUGH */
25464 default:
25465 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25466 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25467 resync_fail:
25468 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25469 /*or_comma=*/false,
25470 /*consume_paren=*/true);
25471 return list;
25472 }
25473 cp_lexer_consume_token (parser->lexer);
25474
25475 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25476 goto resync_fail;
25477
25478 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25479 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25480 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25481
25482 return nlist;
25483 }
25484
25485 /* OpenMP 2.5:
25486 schedule ( schedule-kind )
25487 schedule ( schedule-kind , expression )
25488
25489 schedule-kind:
25490 static | dynamic | guided | runtime | auto */
25491
25492 static tree
25493 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25494 {
25495 tree c, t;
25496
25497 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25498 return list;
25499
25500 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25501
25502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25503 {
25504 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25505 const char *p = IDENTIFIER_POINTER (id);
25506
25507 switch (p[0])
25508 {
25509 case 'd':
25510 if (strcmp ("dynamic", p) != 0)
25511 goto invalid_kind;
25512 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25513 break;
25514
25515 case 'g':
25516 if (strcmp ("guided", p) != 0)
25517 goto invalid_kind;
25518 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25519 break;
25520
25521 case 'r':
25522 if (strcmp ("runtime", p) != 0)
25523 goto invalid_kind;
25524 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25525 break;
25526
25527 default:
25528 goto invalid_kind;
25529 }
25530 }
25531 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25532 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25533 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25534 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25535 else
25536 goto invalid_kind;
25537 cp_lexer_consume_token (parser->lexer);
25538
25539 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25540 {
25541 cp_token *token;
25542 cp_lexer_consume_token (parser->lexer);
25543
25544 token = cp_lexer_peek_token (parser->lexer);
25545 t = cp_parser_assignment_expression (parser, false, NULL);
25546
25547 if (t == error_mark_node)
25548 goto resync_fail;
25549 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25550 error_at (token->location, "schedule %<runtime%> does not take "
25551 "a %<chunk_size%> parameter");
25552 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25553 error_at (token->location, "schedule %<auto%> does not take "
25554 "a %<chunk_size%> parameter");
25555 else
25556 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25557
25558 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25559 goto resync_fail;
25560 }
25561 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25562 goto resync_fail;
25563
25564 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25565 OMP_CLAUSE_CHAIN (c) = list;
25566 return c;
25567
25568 invalid_kind:
25569 cp_parser_error (parser, "invalid schedule kind");
25570 resync_fail:
25571 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25572 /*or_comma=*/false,
25573 /*consume_paren=*/true);
25574 return list;
25575 }
25576
25577 /* OpenMP 3.0:
25578 untied */
25579
25580 static tree
25581 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25582 tree list, location_t location)
25583 {
25584 tree c;
25585
25586 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25587
25588 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25589 OMP_CLAUSE_CHAIN (c) = list;
25590 return c;
25591 }
25592
25593 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25594 is a bitmask in MASK. Return the list of clauses found; the result
25595 of clause default goes in *pdefault. */
25596
25597 static tree
25598 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25599 const char *where, cp_token *pragma_tok)
25600 {
25601 tree clauses = NULL;
25602 bool first = true;
25603 cp_token *token = NULL;
25604
25605 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25606 {
25607 pragma_omp_clause c_kind;
25608 const char *c_name;
25609 tree prev = clauses;
25610
25611 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25612 cp_lexer_consume_token (parser->lexer);
25613
25614 token = cp_lexer_peek_token (parser->lexer);
25615 c_kind = cp_parser_omp_clause_name (parser);
25616 first = false;
25617
25618 switch (c_kind)
25619 {
25620 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25621 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25622 token->location);
25623 c_name = "collapse";
25624 break;
25625 case PRAGMA_OMP_CLAUSE_COPYIN:
25626 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25627 c_name = "copyin";
25628 break;
25629 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25630 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25631 clauses);
25632 c_name = "copyprivate";
25633 break;
25634 case PRAGMA_OMP_CLAUSE_DEFAULT:
25635 clauses = cp_parser_omp_clause_default (parser, clauses,
25636 token->location);
25637 c_name = "default";
25638 break;
25639 case PRAGMA_OMP_CLAUSE_FINAL:
25640 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25641 c_name = "final";
25642 break;
25643 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25644 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25645 clauses);
25646 c_name = "firstprivate";
25647 break;
25648 case PRAGMA_OMP_CLAUSE_IF:
25649 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25650 c_name = "if";
25651 break;
25652 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25653 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25654 clauses);
25655 c_name = "lastprivate";
25656 break;
25657 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25658 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25659 token->location);
25660 c_name = "mergeable";
25661 break;
25662 case PRAGMA_OMP_CLAUSE_NOWAIT:
25663 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25664 c_name = "nowait";
25665 break;
25666 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25667 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25668 token->location);
25669 c_name = "num_threads";
25670 break;
25671 case PRAGMA_OMP_CLAUSE_ORDERED:
25672 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25673 token->location);
25674 c_name = "ordered";
25675 break;
25676 case PRAGMA_OMP_CLAUSE_PRIVATE:
25677 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25678 clauses);
25679 c_name = "private";
25680 break;
25681 case PRAGMA_OMP_CLAUSE_REDUCTION:
25682 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25683 c_name = "reduction";
25684 break;
25685 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25686 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25687 token->location);
25688 c_name = "schedule";
25689 break;
25690 case PRAGMA_OMP_CLAUSE_SHARED:
25691 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25692 clauses);
25693 c_name = "shared";
25694 break;
25695 case PRAGMA_OMP_CLAUSE_UNTIED:
25696 clauses = cp_parser_omp_clause_untied (parser, clauses,
25697 token->location);
25698 c_name = "nowait";
25699 break;
25700 default:
25701 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25702 goto saw_error;
25703 }
25704
25705 if (((mask >> c_kind) & 1) == 0)
25706 {
25707 /* Remove the invalid clause(s) from the list to avoid
25708 confusing the rest of the compiler. */
25709 clauses = prev;
25710 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25711 }
25712 }
25713 saw_error:
25714 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25715 return finish_omp_clauses (clauses);
25716 }
25717
25718 /* OpenMP 2.5:
25719 structured-block:
25720 statement
25721
25722 In practice, we're also interested in adding the statement to an
25723 outer node. So it is convenient if we work around the fact that
25724 cp_parser_statement calls add_stmt. */
25725
25726 static unsigned
25727 cp_parser_begin_omp_structured_block (cp_parser *parser)
25728 {
25729 unsigned save = parser->in_statement;
25730
25731 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25732 This preserves the "not within loop or switch" style error messages
25733 for nonsense cases like
25734 void foo() {
25735 #pragma omp single
25736 break;
25737 }
25738 */
25739 if (parser->in_statement)
25740 parser->in_statement = IN_OMP_BLOCK;
25741
25742 return save;
25743 }
25744
25745 static void
25746 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25747 {
25748 parser->in_statement = save;
25749 }
25750
25751 static tree
25752 cp_parser_omp_structured_block (cp_parser *parser)
25753 {
25754 tree stmt = begin_omp_structured_block ();
25755 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25756
25757 cp_parser_statement (parser, NULL_TREE, false, NULL);
25758
25759 cp_parser_end_omp_structured_block (parser, save);
25760 return finish_omp_structured_block (stmt);
25761 }
25762
25763 /* OpenMP 2.5:
25764 # pragma omp atomic new-line
25765 expression-stmt
25766
25767 expression-stmt:
25768 x binop= expr | x++ | ++x | x-- | --x
25769 binop:
25770 +, *, -, /, &, ^, |, <<, >>
25771
25772 where x is an lvalue expression with scalar type.
25773
25774 OpenMP 3.1:
25775 # pragma omp atomic new-line
25776 update-stmt
25777
25778 # pragma omp atomic read new-line
25779 read-stmt
25780
25781 # pragma omp atomic write new-line
25782 write-stmt
25783
25784 # pragma omp atomic update new-line
25785 update-stmt
25786
25787 # pragma omp atomic capture new-line
25788 capture-stmt
25789
25790 # pragma omp atomic capture new-line
25791 capture-block
25792
25793 read-stmt:
25794 v = x
25795 write-stmt:
25796 x = expr
25797 update-stmt:
25798 expression-stmt | x = x binop expr
25799 capture-stmt:
25800 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25801 capture-block:
25802 { v = x; update-stmt; } | { update-stmt; v = x; }
25803
25804 where x and v are lvalue expressions with scalar type. */
25805
25806 static void
25807 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25808 {
25809 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25810 tree rhs1 = NULL_TREE, orig_lhs;
25811 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25812 bool structured_block = false;
25813
25814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25815 {
25816 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25817 const char *p = IDENTIFIER_POINTER (id);
25818
25819 if (!strcmp (p, "read"))
25820 code = OMP_ATOMIC_READ;
25821 else if (!strcmp (p, "write"))
25822 code = NOP_EXPR;
25823 else if (!strcmp (p, "update"))
25824 code = OMP_ATOMIC;
25825 else if (!strcmp (p, "capture"))
25826 code = OMP_ATOMIC_CAPTURE_NEW;
25827 else
25828 p = NULL;
25829 if (p)
25830 cp_lexer_consume_token (parser->lexer);
25831 }
25832 cp_parser_require_pragma_eol (parser, pragma_tok);
25833
25834 switch (code)
25835 {
25836 case OMP_ATOMIC_READ:
25837 case NOP_EXPR: /* atomic write */
25838 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25839 /*cast_p=*/false, NULL);
25840 if (v == error_mark_node)
25841 goto saw_error;
25842 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25843 goto saw_error;
25844 if (code == NOP_EXPR)
25845 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25846 else
25847 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25848 /*cast_p=*/false, NULL);
25849 if (lhs == error_mark_node)
25850 goto saw_error;
25851 if (code == NOP_EXPR)
25852 {
25853 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25854 opcode. */
25855 code = OMP_ATOMIC;
25856 rhs = lhs;
25857 lhs = v;
25858 v = NULL_TREE;
25859 }
25860 goto done;
25861 case OMP_ATOMIC_CAPTURE_NEW:
25862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25863 {
25864 cp_lexer_consume_token (parser->lexer);
25865 structured_block = true;
25866 }
25867 else
25868 {
25869 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25870 /*cast_p=*/false, NULL);
25871 if (v == error_mark_node)
25872 goto saw_error;
25873 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25874 goto saw_error;
25875 }
25876 default:
25877 break;
25878 }
25879
25880 restart:
25881 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25882 /*cast_p=*/false, NULL);
25883 orig_lhs = lhs;
25884 switch (TREE_CODE (lhs))
25885 {
25886 case ERROR_MARK:
25887 goto saw_error;
25888
25889 case POSTINCREMENT_EXPR:
25890 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25891 code = OMP_ATOMIC_CAPTURE_OLD;
25892 /* FALLTHROUGH */
25893 case PREINCREMENT_EXPR:
25894 lhs = TREE_OPERAND (lhs, 0);
25895 opcode = PLUS_EXPR;
25896 rhs = integer_one_node;
25897 break;
25898
25899 case POSTDECREMENT_EXPR:
25900 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25901 code = OMP_ATOMIC_CAPTURE_OLD;
25902 /* FALLTHROUGH */
25903 case PREDECREMENT_EXPR:
25904 lhs = TREE_OPERAND (lhs, 0);
25905 opcode = MINUS_EXPR;
25906 rhs = integer_one_node;
25907 break;
25908
25909 case COMPOUND_EXPR:
25910 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25911 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25912 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25913 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25914 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25915 (TREE_OPERAND (lhs, 1), 0), 0)))
25916 == BOOLEAN_TYPE)
25917 /* Undo effects of boolean_increment for post {in,de}crement. */
25918 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25919 /* FALLTHRU */
25920 case MODIFY_EXPR:
25921 if (TREE_CODE (lhs) == MODIFY_EXPR
25922 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25923 {
25924 /* Undo effects of boolean_increment. */
25925 if (integer_onep (TREE_OPERAND (lhs, 1)))
25926 {
25927 /* This is pre or post increment. */
25928 rhs = TREE_OPERAND (lhs, 1);
25929 lhs = TREE_OPERAND (lhs, 0);
25930 opcode = NOP_EXPR;
25931 if (code == OMP_ATOMIC_CAPTURE_NEW
25932 && !structured_block
25933 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25934 code = OMP_ATOMIC_CAPTURE_OLD;
25935 break;
25936 }
25937 }
25938 /* FALLTHRU */
25939 default:
25940 switch (cp_lexer_peek_token (parser->lexer)->type)
25941 {
25942 case CPP_MULT_EQ:
25943 opcode = MULT_EXPR;
25944 break;
25945 case CPP_DIV_EQ:
25946 opcode = TRUNC_DIV_EXPR;
25947 break;
25948 case CPP_PLUS_EQ:
25949 opcode = PLUS_EXPR;
25950 break;
25951 case CPP_MINUS_EQ:
25952 opcode = MINUS_EXPR;
25953 break;
25954 case CPP_LSHIFT_EQ:
25955 opcode = LSHIFT_EXPR;
25956 break;
25957 case CPP_RSHIFT_EQ:
25958 opcode = RSHIFT_EXPR;
25959 break;
25960 case CPP_AND_EQ:
25961 opcode = BIT_AND_EXPR;
25962 break;
25963 case CPP_OR_EQ:
25964 opcode = BIT_IOR_EXPR;
25965 break;
25966 case CPP_XOR_EQ:
25967 opcode = BIT_XOR_EXPR;
25968 break;
25969 case CPP_EQ:
25970 if (structured_block || code == OMP_ATOMIC)
25971 {
25972 enum cp_parser_prec oprec;
25973 cp_token *token;
25974 cp_lexer_consume_token (parser->lexer);
25975 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25976 /*cast_p=*/false, NULL);
25977 if (rhs1 == error_mark_node)
25978 goto saw_error;
25979 token = cp_lexer_peek_token (parser->lexer);
25980 switch (token->type)
25981 {
25982 case CPP_SEMICOLON:
25983 if (code == OMP_ATOMIC_CAPTURE_NEW)
25984 {
25985 code = OMP_ATOMIC_CAPTURE_OLD;
25986 v = lhs;
25987 lhs = NULL_TREE;
25988 lhs1 = rhs1;
25989 rhs1 = NULL_TREE;
25990 cp_lexer_consume_token (parser->lexer);
25991 goto restart;
25992 }
25993 cp_parser_error (parser,
25994 "invalid form of %<#pragma omp atomic%>");
25995 goto saw_error;
25996 case CPP_MULT:
25997 opcode = MULT_EXPR;
25998 break;
25999 case CPP_DIV:
26000 opcode = TRUNC_DIV_EXPR;
26001 break;
26002 case CPP_PLUS:
26003 opcode = PLUS_EXPR;
26004 break;
26005 case CPP_MINUS:
26006 opcode = MINUS_EXPR;
26007 break;
26008 case CPP_LSHIFT:
26009 opcode = LSHIFT_EXPR;
26010 break;
26011 case CPP_RSHIFT:
26012 opcode = RSHIFT_EXPR;
26013 break;
26014 case CPP_AND:
26015 opcode = BIT_AND_EXPR;
26016 break;
26017 case CPP_OR:
26018 opcode = BIT_IOR_EXPR;
26019 break;
26020 case CPP_XOR:
26021 opcode = BIT_XOR_EXPR;
26022 break;
26023 default:
26024 cp_parser_error (parser,
26025 "invalid operator for %<#pragma omp atomic%>");
26026 goto saw_error;
26027 }
26028 oprec = TOKEN_PRECEDENCE (token);
26029 gcc_assert (oprec != PREC_NOT_OPERATOR);
26030 if (commutative_tree_code (opcode))
26031 oprec = (enum cp_parser_prec) (oprec - 1);
26032 cp_lexer_consume_token (parser->lexer);
26033 rhs = cp_parser_binary_expression (parser, false, false,
26034 oprec, NULL);
26035 if (rhs == error_mark_node)
26036 goto saw_error;
26037 goto stmt_done;
26038 }
26039 /* FALLTHROUGH */
26040 default:
26041 cp_parser_error (parser,
26042 "invalid operator for %<#pragma omp atomic%>");
26043 goto saw_error;
26044 }
26045 cp_lexer_consume_token (parser->lexer);
26046
26047 rhs = cp_parser_expression (parser, false, NULL);
26048 if (rhs == error_mark_node)
26049 goto saw_error;
26050 break;
26051 }
26052 stmt_done:
26053 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26054 {
26055 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26056 goto saw_error;
26057 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26058 /*cast_p=*/false, NULL);
26059 if (v == error_mark_node)
26060 goto saw_error;
26061 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26062 goto saw_error;
26063 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26064 /*cast_p=*/false, NULL);
26065 if (lhs1 == error_mark_node)
26066 goto saw_error;
26067 }
26068 if (structured_block)
26069 {
26070 cp_parser_consume_semicolon_at_end_of_statement (parser);
26071 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26072 }
26073 done:
26074 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26075 if (!structured_block)
26076 cp_parser_consume_semicolon_at_end_of_statement (parser);
26077 return;
26078
26079 saw_error:
26080 cp_parser_skip_to_end_of_block_or_statement (parser);
26081 if (structured_block)
26082 {
26083 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26084 cp_lexer_consume_token (parser->lexer);
26085 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26086 {
26087 cp_parser_skip_to_end_of_block_or_statement (parser);
26088 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26089 cp_lexer_consume_token (parser->lexer);
26090 }
26091 }
26092 }
26093
26094
26095 /* OpenMP 2.5:
26096 # pragma omp barrier new-line */
26097
26098 static void
26099 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26100 {
26101 cp_parser_require_pragma_eol (parser, pragma_tok);
26102 finish_omp_barrier ();
26103 }
26104
26105 /* OpenMP 2.5:
26106 # pragma omp critical [(name)] new-line
26107 structured-block */
26108
26109 static tree
26110 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26111 {
26112 tree stmt, name = NULL;
26113
26114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26115 {
26116 cp_lexer_consume_token (parser->lexer);
26117
26118 name = cp_parser_identifier (parser);
26119
26120 if (name == error_mark_node
26121 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26122 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26123 /*or_comma=*/false,
26124 /*consume_paren=*/true);
26125 if (name == error_mark_node)
26126 name = NULL;
26127 }
26128 cp_parser_require_pragma_eol (parser, pragma_tok);
26129
26130 stmt = cp_parser_omp_structured_block (parser);
26131 return c_finish_omp_critical (input_location, stmt, name);
26132 }
26133
26134 /* OpenMP 2.5:
26135 # pragma omp flush flush-vars[opt] new-line
26136
26137 flush-vars:
26138 ( variable-list ) */
26139
26140 static void
26141 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26142 {
26143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26144 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26145 cp_parser_require_pragma_eol (parser, pragma_tok);
26146
26147 finish_omp_flush ();
26148 }
26149
26150 /* Helper function, to parse omp for increment expression. */
26151
26152 static tree
26153 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26154 {
26155 tree cond = cp_parser_binary_expression (parser, false, true,
26156 PREC_NOT_OPERATOR, NULL);
26157 if (cond == error_mark_node
26158 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26159 {
26160 cp_parser_skip_to_end_of_statement (parser);
26161 return error_mark_node;
26162 }
26163
26164 switch (TREE_CODE (cond))
26165 {
26166 case GT_EXPR:
26167 case GE_EXPR:
26168 case LT_EXPR:
26169 case LE_EXPR:
26170 break;
26171 default:
26172 return error_mark_node;
26173 }
26174
26175 /* If decl is an iterator, preserve LHS and RHS of the relational
26176 expr until finish_omp_for. */
26177 if (decl
26178 && (type_dependent_expression_p (decl)
26179 || CLASS_TYPE_P (TREE_TYPE (decl))))
26180 return cond;
26181
26182 return build_x_binary_op (input_location, TREE_CODE (cond),
26183 TREE_OPERAND (cond, 0), ERROR_MARK,
26184 TREE_OPERAND (cond, 1), ERROR_MARK,
26185 /*overload=*/NULL, tf_warning_or_error);
26186 }
26187
26188 /* Helper function, to parse omp for increment expression. */
26189
26190 static tree
26191 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26192 {
26193 cp_token *token = cp_lexer_peek_token (parser->lexer);
26194 enum tree_code op;
26195 tree lhs, rhs;
26196 cp_id_kind idk;
26197 bool decl_first;
26198
26199 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26200 {
26201 op = (token->type == CPP_PLUS_PLUS
26202 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26203 cp_lexer_consume_token (parser->lexer);
26204 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26205 if (lhs != decl)
26206 return error_mark_node;
26207 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26208 }
26209
26210 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26211 if (lhs != decl)
26212 return error_mark_node;
26213
26214 token = cp_lexer_peek_token (parser->lexer);
26215 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26216 {
26217 op = (token->type == CPP_PLUS_PLUS
26218 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26219 cp_lexer_consume_token (parser->lexer);
26220 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26221 }
26222
26223 op = cp_parser_assignment_operator_opt (parser);
26224 if (op == ERROR_MARK)
26225 return error_mark_node;
26226
26227 if (op != NOP_EXPR)
26228 {
26229 rhs = cp_parser_assignment_expression (parser, false, NULL);
26230 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26231 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26232 }
26233
26234 lhs = cp_parser_binary_expression (parser, false, false,
26235 PREC_ADDITIVE_EXPRESSION, NULL);
26236 token = cp_lexer_peek_token (parser->lexer);
26237 decl_first = lhs == decl;
26238 if (decl_first)
26239 lhs = NULL_TREE;
26240 if (token->type != CPP_PLUS
26241 && token->type != CPP_MINUS)
26242 return error_mark_node;
26243
26244 do
26245 {
26246 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26247 cp_lexer_consume_token (parser->lexer);
26248 rhs = cp_parser_binary_expression (parser, false, false,
26249 PREC_ADDITIVE_EXPRESSION, NULL);
26250 token = cp_lexer_peek_token (parser->lexer);
26251 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26252 {
26253 if (lhs == NULL_TREE)
26254 {
26255 if (op == PLUS_EXPR)
26256 lhs = rhs;
26257 else
26258 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26259 tf_warning_or_error);
26260 }
26261 else
26262 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26263 ERROR_MARK, NULL, tf_warning_or_error);
26264 }
26265 }
26266 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26267
26268 if (!decl_first)
26269 {
26270 if (rhs != decl || op == MINUS_EXPR)
26271 return error_mark_node;
26272 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26273 }
26274 else
26275 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26276
26277 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26278 }
26279
26280 /* Parse the restricted form of the for statement allowed by OpenMP. */
26281
26282 static tree
26283 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26284 {
26285 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26286 tree real_decl, initv, condv, incrv, declv;
26287 tree this_pre_body, cl;
26288 location_t loc_first;
26289 bool collapse_err = false;
26290 int i, collapse = 1, nbraces = 0;
26291 VEC(tree,gc) *for_block = make_tree_vector ();
26292
26293 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26294 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26295 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26296
26297 gcc_assert (collapse >= 1);
26298
26299 declv = make_tree_vec (collapse);
26300 initv = make_tree_vec (collapse);
26301 condv = make_tree_vec (collapse);
26302 incrv = make_tree_vec (collapse);
26303
26304 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26305
26306 for (i = 0; i < collapse; i++)
26307 {
26308 int bracecount = 0;
26309 bool add_private_clause = false;
26310 location_t loc;
26311
26312 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26313 {
26314 cp_parser_error (parser, "for statement expected");
26315 return NULL;
26316 }
26317 loc = cp_lexer_consume_token (parser->lexer)->location;
26318
26319 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26320 return NULL;
26321
26322 init = decl = real_decl = NULL;
26323 this_pre_body = push_stmt_list ();
26324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26325 {
26326 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26327
26328 init-expr:
26329 var = lb
26330 integer-type var = lb
26331 random-access-iterator-type var = lb
26332 pointer-type var = lb
26333 */
26334 cp_decl_specifier_seq type_specifiers;
26335
26336 /* First, try to parse as an initialized declaration. See
26337 cp_parser_condition, from whence the bulk of this is copied. */
26338
26339 cp_parser_parse_tentatively (parser);
26340 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26341 /*is_trailing_return=*/false,
26342 &type_specifiers);
26343 if (cp_parser_parse_definitely (parser))
26344 {
26345 /* If parsing a type specifier seq succeeded, then this
26346 MUST be a initialized declaration. */
26347 tree asm_specification, attributes;
26348 cp_declarator *declarator;
26349
26350 declarator = cp_parser_declarator (parser,
26351 CP_PARSER_DECLARATOR_NAMED,
26352 /*ctor_dtor_or_conv_p=*/NULL,
26353 /*parenthesized_p=*/NULL,
26354 /*member_p=*/false);
26355 attributes = cp_parser_attributes_opt (parser);
26356 asm_specification = cp_parser_asm_specification_opt (parser);
26357
26358 if (declarator == cp_error_declarator)
26359 cp_parser_skip_to_end_of_statement (parser);
26360
26361 else
26362 {
26363 tree pushed_scope, auto_node;
26364
26365 decl = start_decl (declarator, &type_specifiers,
26366 SD_INITIALIZED, attributes,
26367 /*prefix_attributes=*/NULL_TREE,
26368 &pushed_scope);
26369
26370 auto_node = type_uses_auto (TREE_TYPE (decl));
26371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26372 {
26373 if (cp_lexer_next_token_is (parser->lexer,
26374 CPP_OPEN_PAREN))
26375 error ("parenthesized initialization is not allowed in "
26376 "OpenMP %<for%> loop");
26377 else
26378 /* Trigger an error. */
26379 cp_parser_require (parser, CPP_EQ, RT_EQ);
26380
26381 init = error_mark_node;
26382 cp_parser_skip_to_end_of_statement (parser);
26383 }
26384 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26385 || type_dependent_expression_p (decl)
26386 || auto_node)
26387 {
26388 bool is_direct_init, is_non_constant_init;
26389
26390 init = cp_parser_initializer (parser,
26391 &is_direct_init,
26392 &is_non_constant_init);
26393
26394 if (auto_node)
26395 {
26396 TREE_TYPE (decl)
26397 = do_auto_deduction (TREE_TYPE (decl), init,
26398 auto_node);
26399
26400 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26401 && !type_dependent_expression_p (decl))
26402 goto non_class;
26403 }
26404
26405 cp_finish_decl (decl, init, !is_non_constant_init,
26406 asm_specification,
26407 LOOKUP_ONLYCONVERTING);
26408 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26409 {
26410 VEC_safe_push (tree, gc, for_block, this_pre_body);
26411 init = NULL_TREE;
26412 }
26413 else
26414 init = pop_stmt_list (this_pre_body);
26415 this_pre_body = NULL_TREE;
26416 }
26417 else
26418 {
26419 /* Consume '='. */
26420 cp_lexer_consume_token (parser->lexer);
26421 init = cp_parser_assignment_expression (parser, false, NULL);
26422
26423 non_class:
26424 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26425 init = error_mark_node;
26426 else
26427 cp_finish_decl (decl, NULL_TREE,
26428 /*init_const_expr_p=*/false,
26429 asm_specification,
26430 LOOKUP_ONLYCONVERTING);
26431 }
26432
26433 if (pushed_scope)
26434 pop_scope (pushed_scope);
26435 }
26436 }
26437 else
26438 {
26439 cp_id_kind idk;
26440 /* If parsing a type specifier sequence failed, then
26441 this MUST be a simple expression. */
26442 cp_parser_parse_tentatively (parser);
26443 decl = cp_parser_primary_expression (parser, false, false,
26444 false, &idk);
26445 if (!cp_parser_error_occurred (parser)
26446 && decl
26447 && DECL_P (decl)
26448 && CLASS_TYPE_P (TREE_TYPE (decl)))
26449 {
26450 tree rhs;
26451
26452 cp_parser_parse_definitely (parser);
26453 cp_parser_require (parser, CPP_EQ, RT_EQ);
26454 rhs = cp_parser_assignment_expression (parser, false, NULL);
26455 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
26456 decl, NOP_EXPR,
26457 rhs,
26458 tf_warning_or_error));
26459 add_private_clause = true;
26460 }
26461 else
26462 {
26463 decl = NULL;
26464 cp_parser_abort_tentative_parse (parser);
26465 init = cp_parser_expression (parser, false, NULL);
26466 if (init)
26467 {
26468 if (TREE_CODE (init) == MODIFY_EXPR
26469 || TREE_CODE (init) == MODOP_EXPR)
26470 real_decl = TREE_OPERAND (init, 0);
26471 }
26472 }
26473 }
26474 }
26475 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26476 if (this_pre_body)
26477 {
26478 this_pre_body = pop_stmt_list (this_pre_body);
26479 if (pre_body)
26480 {
26481 tree t = pre_body;
26482 pre_body = push_stmt_list ();
26483 add_stmt (t);
26484 add_stmt (this_pre_body);
26485 pre_body = pop_stmt_list (pre_body);
26486 }
26487 else
26488 pre_body = this_pre_body;
26489 }
26490
26491 if (decl)
26492 real_decl = decl;
26493 if (par_clauses != NULL && real_decl != NULL_TREE)
26494 {
26495 tree *c;
26496 for (c = par_clauses; *c ; )
26497 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26498 && OMP_CLAUSE_DECL (*c) == real_decl)
26499 {
26500 error_at (loc, "iteration variable %qD"
26501 " should not be firstprivate", real_decl);
26502 *c = OMP_CLAUSE_CHAIN (*c);
26503 }
26504 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26505 && OMP_CLAUSE_DECL (*c) == real_decl)
26506 {
26507 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26508 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26509 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26510 OMP_CLAUSE_DECL (l) = real_decl;
26511 OMP_CLAUSE_CHAIN (l) = clauses;
26512 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26513 clauses = l;
26514 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26515 CP_OMP_CLAUSE_INFO (*c) = NULL;
26516 add_private_clause = false;
26517 }
26518 else
26519 {
26520 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26521 && OMP_CLAUSE_DECL (*c) == real_decl)
26522 add_private_clause = false;
26523 c = &OMP_CLAUSE_CHAIN (*c);
26524 }
26525 }
26526
26527 if (add_private_clause)
26528 {
26529 tree c;
26530 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26531 {
26532 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26533 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26534 && OMP_CLAUSE_DECL (c) == decl)
26535 break;
26536 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26537 && OMP_CLAUSE_DECL (c) == decl)
26538 error_at (loc, "iteration variable %qD "
26539 "should not be firstprivate",
26540 decl);
26541 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26542 && OMP_CLAUSE_DECL (c) == decl)
26543 error_at (loc, "iteration variable %qD should not be reduction",
26544 decl);
26545 }
26546 if (c == NULL)
26547 {
26548 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26549 OMP_CLAUSE_DECL (c) = decl;
26550 c = finish_omp_clauses (c);
26551 if (c)
26552 {
26553 OMP_CLAUSE_CHAIN (c) = clauses;
26554 clauses = c;
26555 }
26556 }
26557 }
26558
26559 cond = NULL;
26560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26561 cond = cp_parser_omp_for_cond (parser, decl);
26562 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26563
26564 incr = NULL;
26565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26566 {
26567 /* If decl is an iterator, preserve the operator on decl
26568 until finish_omp_for. */
26569 if (real_decl
26570 && ((processing_template_decl
26571 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26572 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26573 incr = cp_parser_omp_for_incr (parser, real_decl);
26574 else
26575 incr = cp_parser_expression (parser, false, NULL);
26576 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
26577 SET_EXPR_LOCATION (incr, input_location);
26578 }
26579
26580 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26581 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26582 /*or_comma=*/false,
26583 /*consume_paren=*/true);
26584
26585 TREE_VEC_ELT (declv, i) = decl;
26586 TREE_VEC_ELT (initv, i) = init;
26587 TREE_VEC_ELT (condv, i) = cond;
26588 TREE_VEC_ELT (incrv, i) = incr;
26589
26590 if (i == collapse - 1)
26591 break;
26592
26593 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26594 in between the collapsed for loops to be still considered perfectly
26595 nested. Hopefully the final version clarifies this.
26596 For now handle (multiple) {'s and empty statements. */
26597 cp_parser_parse_tentatively (parser);
26598 do
26599 {
26600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26601 break;
26602 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26603 {
26604 cp_lexer_consume_token (parser->lexer);
26605 bracecount++;
26606 }
26607 else if (bracecount
26608 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26609 cp_lexer_consume_token (parser->lexer);
26610 else
26611 {
26612 loc = cp_lexer_peek_token (parser->lexer)->location;
26613 error_at (loc, "not enough collapsed for loops");
26614 collapse_err = true;
26615 cp_parser_abort_tentative_parse (parser);
26616 declv = NULL_TREE;
26617 break;
26618 }
26619 }
26620 while (1);
26621
26622 if (declv)
26623 {
26624 cp_parser_parse_definitely (parser);
26625 nbraces += bracecount;
26626 }
26627 }
26628
26629 /* Note that we saved the original contents of this flag when we entered
26630 the structured block, and so we don't need to re-save it here. */
26631 parser->in_statement = IN_OMP_FOR;
26632
26633 /* Note that the grammar doesn't call for a structured block here,
26634 though the loop as a whole is a structured block. */
26635 body = push_stmt_list ();
26636 cp_parser_statement (parser, NULL_TREE, false, NULL);
26637 body = pop_stmt_list (body);
26638
26639 if (declv == NULL_TREE)
26640 ret = NULL_TREE;
26641 else
26642 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26643 pre_body, clauses);
26644
26645 while (nbraces)
26646 {
26647 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26648 {
26649 cp_lexer_consume_token (parser->lexer);
26650 nbraces--;
26651 }
26652 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26653 cp_lexer_consume_token (parser->lexer);
26654 else
26655 {
26656 if (!collapse_err)
26657 {
26658 error_at (cp_lexer_peek_token (parser->lexer)->location,
26659 "collapsed loops not perfectly nested");
26660 }
26661 collapse_err = true;
26662 cp_parser_statement_seq_opt (parser, NULL);
26663 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26664 break;
26665 }
26666 }
26667
26668 while (!VEC_empty (tree, for_block))
26669 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26670 release_tree_vector (for_block);
26671
26672 return ret;
26673 }
26674
26675 /* OpenMP 2.5:
26676 #pragma omp for for-clause[optseq] new-line
26677 for-loop */
26678
26679 #define OMP_FOR_CLAUSE_MASK \
26680 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26681 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26682 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26683 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26684 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26685 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26686 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26687 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26688
26689 static tree
26690 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26691 {
26692 tree clauses, sb, ret;
26693 unsigned int save;
26694
26695 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26696 "#pragma omp for", pragma_tok);
26697
26698 sb = begin_omp_structured_block ();
26699 save = cp_parser_begin_omp_structured_block (parser);
26700
26701 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26702
26703 cp_parser_end_omp_structured_block (parser, save);
26704 add_stmt (finish_omp_structured_block (sb));
26705
26706 return ret;
26707 }
26708
26709 /* OpenMP 2.5:
26710 # pragma omp master new-line
26711 structured-block */
26712
26713 static tree
26714 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26715 {
26716 cp_parser_require_pragma_eol (parser, pragma_tok);
26717 return c_finish_omp_master (input_location,
26718 cp_parser_omp_structured_block (parser));
26719 }
26720
26721 /* OpenMP 2.5:
26722 # pragma omp ordered new-line
26723 structured-block */
26724
26725 static tree
26726 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26727 {
26728 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26729 cp_parser_require_pragma_eol (parser, pragma_tok);
26730 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26731 }
26732
26733 /* OpenMP 2.5:
26734
26735 section-scope:
26736 { section-sequence }
26737
26738 section-sequence:
26739 section-directive[opt] structured-block
26740 section-sequence section-directive structured-block */
26741
26742 static tree
26743 cp_parser_omp_sections_scope (cp_parser *parser)
26744 {
26745 tree stmt, substmt;
26746 bool error_suppress = false;
26747 cp_token *tok;
26748
26749 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26750 return NULL_TREE;
26751
26752 stmt = push_stmt_list ();
26753
26754 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26755 {
26756 unsigned save;
26757
26758 substmt = begin_omp_structured_block ();
26759 save = cp_parser_begin_omp_structured_block (parser);
26760
26761 while (1)
26762 {
26763 cp_parser_statement (parser, NULL_TREE, false, NULL);
26764
26765 tok = cp_lexer_peek_token (parser->lexer);
26766 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26767 break;
26768 if (tok->type == CPP_CLOSE_BRACE)
26769 break;
26770 if (tok->type == CPP_EOF)
26771 break;
26772 }
26773
26774 cp_parser_end_omp_structured_block (parser, save);
26775 substmt = finish_omp_structured_block (substmt);
26776 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26777 add_stmt (substmt);
26778 }
26779
26780 while (1)
26781 {
26782 tok = cp_lexer_peek_token (parser->lexer);
26783 if (tok->type == CPP_CLOSE_BRACE)
26784 break;
26785 if (tok->type == CPP_EOF)
26786 break;
26787
26788 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26789 {
26790 cp_lexer_consume_token (parser->lexer);
26791 cp_parser_require_pragma_eol (parser, tok);
26792 error_suppress = false;
26793 }
26794 else if (!error_suppress)
26795 {
26796 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26797 error_suppress = true;
26798 }
26799
26800 substmt = cp_parser_omp_structured_block (parser);
26801 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26802 add_stmt (substmt);
26803 }
26804 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26805
26806 substmt = pop_stmt_list (stmt);
26807
26808 stmt = make_node (OMP_SECTIONS);
26809 TREE_TYPE (stmt) = void_type_node;
26810 OMP_SECTIONS_BODY (stmt) = substmt;
26811
26812 add_stmt (stmt);
26813 return stmt;
26814 }
26815
26816 /* OpenMP 2.5:
26817 # pragma omp sections sections-clause[optseq] newline
26818 sections-scope */
26819
26820 #define OMP_SECTIONS_CLAUSE_MASK \
26821 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26822 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26823 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26824 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26825 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26826
26827 static tree
26828 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26829 {
26830 tree clauses, ret;
26831
26832 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26833 "#pragma omp sections", pragma_tok);
26834
26835 ret = cp_parser_omp_sections_scope (parser);
26836 if (ret)
26837 OMP_SECTIONS_CLAUSES (ret) = clauses;
26838
26839 return ret;
26840 }
26841
26842 /* OpenMP 2.5:
26843 # pragma parallel parallel-clause new-line
26844 # pragma parallel for parallel-for-clause new-line
26845 # pragma parallel sections parallel-sections-clause new-line */
26846
26847 #define OMP_PARALLEL_CLAUSE_MASK \
26848 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26849 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26850 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26851 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26852 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26853 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26854 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26855 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26856
26857 static tree
26858 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26859 {
26860 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26861 const char *p_name = "#pragma omp parallel";
26862 tree stmt, clauses, par_clause, ws_clause, block;
26863 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26864 unsigned int save;
26865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26866
26867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26868 {
26869 cp_lexer_consume_token (parser->lexer);
26870 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26871 p_name = "#pragma omp parallel for";
26872 mask |= OMP_FOR_CLAUSE_MASK;
26873 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26874 }
26875 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26876 {
26877 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26878 const char *p = IDENTIFIER_POINTER (id);
26879 if (strcmp (p, "sections") == 0)
26880 {
26881 cp_lexer_consume_token (parser->lexer);
26882 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26883 p_name = "#pragma omp parallel sections";
26884 mask |= OMP_SECTIONS_CLAUSE_MASK;
26885 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26886 }
26887 }
26888
26889 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26890 block = begin_omp_parallel ();
26891 save = cp_parser_begin_omp_structured_block (parser);
26892
26893 switch (p_kind)
26894 {
26895 case PRAGMA_OMP_PARALLEL:
26896 cp_parser_statement (parser, NULL_TREE, false, NULL);
26897 par_clause = clauses;
26898 break;
26899
26900 case PRAGMA_OMP_PARALLEL_FOR:
26901 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26902 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26903 break;
26904
26905 case PRAGMA_OMP_PARALLEL_SECTIONS:
26906 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26907 stmt = cp_parser_omp_sections_scope (parser);
26908 if (stmt)
26909 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26910 break;
26911
26912 default:
26913 gcc_unreachable ();
26914 }
26915
26916 cp_parser_end_omp_structured_block (parser, save);
26917 stmt = finish_omp_parallel (par_clause, block);
26918 if (p_kind != PRAGMA_OMP_PARALLEL)
26919 OMP_PARALLEL_COMBINED (stmt) = 1;
26920 return stmt;
26921 }
26922
26923 /* OpenMP 2.5:
26924 # pragma omp single single-clause[optseq] new-line
26925 structured-block */
26926
26927 #define OMP_SINGLE_CLAUSE_MASK \
26928 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26929 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26930 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26931 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26932
26933 static tree
26934 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26935 {
26936 tree stmt = make_node (OMP_SINGLE);
26937 TREE_TYPE (stmt) = void_type_node;
26938
26939 OMP_SINGLE_CLAUSES (stmt)
26940 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26941 "#pragma omp single", pragma_tok);
26942 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26943
26944 return add_stmt (stmt);
26945 }
26946
26947 /* OpenMP 3.0:
26948 # pragma omp task task-clause[optseq] new-line
26949 structured-block */
26950
26951 #define OMP_TASK_CLAUSE_MASK \
26952 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26953 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26954 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26955 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26956 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26957 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26958 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26959 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26960
26961 static tree
26962 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26963 {
26964 tree clauses, block;
26965 unsigned int save;
26966
26967 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26968 "#pragma omp task", pragma_tok);
26969 block = begin_omp_task ();
26970 save = cp_parser_begin_omp_structured_block (parser);
26971 cp_parser_statement (parser, NULL_TREE, false, NULL);
26972 cp_parser_end_omp_structured_block (parser, save);
26973 return finish_omp_task (clauses, block);
26974 }
26975
26976 /* OpenMP 3.0:
26977 # pragma omp taskwait new-line */
26978
26979 static void
26980 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26981 {
26982 cp_parser_require_pragma_eol (parser, pragma_tok);
26983 finish_omp_taskwait ();
26984 }
26985
26986 /* OpenMP 3.1:
26987 # pragma omp taskyield new-line */
26988
26989 static void
26990 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26991 {
26992 cp_parser_require_pragma_eol (parser, pragma_tok);
26993 finish_omp_taskyield ();
26994 }
26995
26996 /* OpenMP 2.5:
26997 # pragma omp threadprivate (variable-list) */
26998
26999 static void
27000 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27001 {
27002 tree vars;
27003
27004 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27005 cp_parser_require_pragma_eol (parser, pragma_tok);
27006
27007 finish_omp_threadprivate (vars);
27008 }
27009
27010 /* Main entry point to OpenMP statement pragmas. */
27011
27012 static void
27013 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27014 {
27015 tree stmt;
27016
27017 switch (pragma_tok->pragma_kind)
27018 {
27019 case PRAGMA_OMP_ATOMIC:
27020 cp_parser_omp_atomic (parser, pragma_tok);
27021 return;
27022 case PRAGMA_OMP_CRITICAL:
27023 stmt = cp_parser_omp_critical (parser, pragma_tok);
27024 break;
27025 case PRAGMA_OMP_FOR:
27026 stmt = cp_parser_omp_for (parser, pragma_tok);
27027 break;
27028 case PRAGMA_OMP_MASTER:
27029 stmt = cp_parser_omp_master (parser, pragma_tok);
27030 break;
27031 case PRAGMA_OMP_ORDERED:
27032 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27033 break;
27034 case PRAGMA_OMP_PARALLEL:
27035 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27036 break;
27037 case PRAGMA_OMP_SECTIONS:
27038 stmt = cp_parser_omp_sections (parser, pragma_tok);
27039 break;
27040 case PRAGMA_OMP_SINGLE:
27041 stmt = cp_parser_omp_single (parser, pragma_tok);
27042 break;
27043 case PRAGMA_OMP_TASK:
27044 stmt = cp_parser_omp_task (parser, pragma_tok);
27045 break;
27046 default:
27047 gcc_unreachable ();
27048 }
27049
27050 if (stmt)
27051 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27052 }
27053 \f
27054 /* Transactional Memory parsing routines. */
27055
27056 /* Parse a transaction attribute.
27057
27058 txn-attribute:
27059 attribute
27060 [ [ identifier ] ]
27061
27062 ??? Simplify this when C++0x bracket attributes are
27063 implemented properly. */
27064
27065 static tree
27066 cp_parser_txn_attribute_opt (cp_parser *parser)
27067 {
27068 cp_token *token;
27069 tree attr_name, attr = NULL;
27070
27071 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27072 return cp_parser_attributes_opt (parser);
27073
27074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27075 return NULL_TREE;
27076 cp_lexer_consume_token (parser->lexer);
27077 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27078 goto error1;
27079
27080 token = cp_lexer_peek_token (parser->lexer);
27081 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27082 {
27083 token = cp_lexer_consume_token (parser->lexer);
27084
27085 attr_name = (token->type == CPP_KEYWORD
27086 /* For keywords, use the canonical spelling,
27087 not the parsed identifier. */
27088 ? ridpointers[(int) token->keyword]
27089 : token->u.value);
27090 attr = build_tree_list (attr_name, NULL_TREE);
27091 }
27092 else
27093 cp_parser_error (parser, "expected identifier");
27094
27095 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27096 error1:
27097 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27098 return attr;
27099 }
27100
27101 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27102
27103 transaction-statement:
27104 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27105 compound-statement
27106 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27107 */
27108
27109 static tree
27110 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27111 {
27112 unsigned char old_in = parser->in_transaction;
27113 unsigned char this_in = 1, new_in;
27114 cp_token *token;
27115 tree stmt, attrs, noex;
27116
27117 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27118 || keyword == RID_TRANSACTION_RELAXED);
27119 token = cp_parser_require_keyword (parser, keyword,
27120 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27121 : RT_TRANSACTION_RELAXED));
27122 gcc_assert (token != NULL);
27123
27124 if (keyword == RID_TRANSACTION_RELAXED)
27125 this_in |= TM_STMT_ATTR_RELAXED;
27126 else
27127 {
27128 attrs = cp_parser_txn_attribute_opt (parser);
27129 if (attrs)
27130 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27131 }
27132
27133 /* Parse a noexcept specification. */
27134 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27135
27136 /* Keep track if we're in the lexical scope of an outer transaction. */
27137 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27138
27139 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27140
27141 parser->in_transaction = new_in;
27142 cp_parser_compound_statement (parser, NULL, false, false);
27143 parser->in_transaction = old_in;
27144
27145 finish_transaction_stmt (stmt, NULL, this_in, noex);
27146
27147 return stmt;
27148 }
27149
27150 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27151
27152 transaction-expression:
27153 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27154 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27155 */
27156
27157 static tree
27158 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27159 {
27160 unsigned char old_in = parser->in_transaction;
27161 unsigned char this_in = 1;
27162 cp_token *token;
27163 tree expr, noex;
27164 bool noex_expr;
27165
27166 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27167 || keyword == RID_TRANSACTION_RELAXED);
27168
27169 if (!flag_tm)
27170 error (keyword == RID_TRANSACTION_RELAXED
27171 ? G_("%<__transaction_relaxed%> without transactional memory "
27172 "support enabled")
27173 : G_("%<__transaction_atomic%> without transactional memory "
27174 "support enabled"));
27175
27176 token = cp_parser_require_keyword (parser, keyword,
27177 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27178 : RT_TRANSACTION_RELAXED));
27179 gcc_assert (token != NULL);
27180
27181 if (keyword == RID_TRANSACTION_RELAXED)
27182 this_in |= TM_STMT_ATTR_RELAXED;
27183
27184 /* Set this early. This might mean that we allow transaction_cancel in
27185 an expression that we find out later actually has to be a constexpr.
27186 However, we expect that cxx_constant_value will be able to deal with
27187 this; also, if the noexcept has no constexpr, then what we parse next
27188 really is a transaction's body. */
27189 parser->in_transaction = this_in;
27190
27191 /* Parse a noexcept specification. */
27192 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27193 true);
27194
27195 if (!noex || !noex_expr
27196 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27197 {
27198 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27199
27200 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27201 finish_parenthesized_expr (expr);
27202
27203 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27204 }
27205 else
27206 {
27207 /* The only expression that is available got parsed for the noexcept
27208 already. noexcept is true then. */
27209 expr = noex;
27210 noex = boolean_true_node;
27211 }
27212
27213 expr = build_transaction_expr (token->location, expr, this_in, noex);
27214 parser->in_transaction = old_in;
27215
27216 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27217 return error_mark_node;
27218
27219 return (flag_tm ? expr : error_mark_node);
27220 }
27221
27222 /* Parse a function-transaction-block.
27223
27224 function-transaction-block:
27225 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27226 function-body
27227 __transaction_atomic txn-attribute[opt] function-try-block
27228 __transaction_relaxed ctor-initializer[opt] function-body
27229 __transaction_relaxed function-try-block
27230 */
27231
27232 static bool
27233 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27234 {
27235 unsigned char old_in = parser->in_transaction;
27236 unsigned char new_in = 1;
27237 tree compound_stmt, stmt, attrs;
27238 bool ctor_initializer_p;
27239 cp_token *token;
27240
27241 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27242 || keyword == RID_TRANSACTION_RELAXED);
27243 token = cp_parser_require_keyword (parser, keyword,
27244 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27245 : RT_TRANSACTION_RELAXED));
27246 gcc_assert (token != NULL);
27247
27248 if (keyword == RID_TRANSACTION_RELAXED)
27249 new_in |= TM_STMT_ATTR_RELAXED;
27250 else
27251 {
27252 attrs = cp_parser_txn_attribute_opt (parser);
27253 if (attrs)
27254 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27255 }
27256
27257 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27258
27259 parser->in_transaction = new_in;
27260
27261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27262 ctor_initializer_p = cp_parser_function_try_block (parser);
27263 else
27264 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27265 (parser, /*in_function_try_block=*/false);
27266
27267 parser->in_transaction = old_in;
27268
27269 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27270
27271 return ctor_initializer_p;
27272 }
27273
27274 /* Parse a __transaction_cancel statement.
27275
27276 cancel-statement:
27277 __transaction_cancel txn-attribute[opt] ;
27278 __transaction_cancel txn-attribute[opt] throw-expression ;
27279
27280 ??? Cancel and throw is not yet implemented. */
27281
27282 static tree
27283 cp_parser_transaction_cancel (cp_parser *parser)
27284 {
27285 cp_token *token;
27286 bool is_outer = false;
27287 tree stmt, attrs;
27288
27289 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27290 RT_TRANSACTION_CANCEL);
27291 gcc_assert (token != NULL);
27292
27293 attrs = cp_parser_txn_attribute_opt (parser);
27294 if (attrs)
27295 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27296
27297 /* ??? Parse cancel-and-throw here. */
27298
27299 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27300
27301 if (!flag_tm)
27302 {
27303 error_at (token->location, "%<__transaction_cancel%> without "
27304 "transactional memory support enabled");
27305 return error_mark_node;
27306 }
27307 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27308 {
27309 error_at (token->location, "%<__transaction_cancel%> within a "
27310 "%<__transaction_relaxed%>");
27311 return error_mark_node;
27312 }
27313 else if (is_outer)
27314 {
27315 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27316 && !is_tm_may_cancel_outer (current_function_decl))
27317 {
27318 error_at (token->location, "outer %<__transaction_cancel%> not "
27319 "within outer %<__transaction_atomic%>");
27320 error_at (token->location,
27321 " or a %<transaction_may_cancel_outer%> function");
27322 return error_mark_node;
27323 }
27324 }
27325 else if (parser->in_transaction == 0)
27326 {
27327 error_at (token->location, "%<__transaction_cancel%> not within "
27328 "%<__transaction_atomic%>");
27329 return error_mark_node;
27330 }
27331
27332 stmt = build_tm_abort_call (token->location, is_outer);
27333 add_stmt (stmt);
27334 finish_stmt ();
27335
27336 return stmt;
27337 }
27338 \f
27339 /* The parser. */
27340
27341 static GTY (()) cp_parser *the_parser;
27342
27343 \f
27344 /* Special handling for the first token or line in the file. The first
27345 thing in the file might be #pragma GCC pch_preprocess, which loads a
27346 PCH file, which is a GC collection point. So we need to handle this
27347 first pragma without benefit of an existing lexer structure.
27348
27349 Always returns one token to the caller in *FIRST_TOKEN. This is
27350 either the true first token of the file, or the first token after
27351 the initial pragma. */
27352
27353 static void
27354 cp_parser_initial_pragma (cp_token *first_token)
27355 {
27356 tree name = NULL;
27357
27358 cp_lexer_get_preprocessor_token (NULL, first_token);
27359 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27360 return;
27361
27362 cp_lexer_get_preprocessor_token (NULL, first_token);
27363 if (first_token->type == CPP_STRING)
27364 {
27365 name = first_token->u.value;
27366
27367 cp_lexer_get_preprocessor_token (NULL, first_token);
27368 if (first_token->type != CPP_PRAGMA_EOL)
27369 error_at (first_token->location,
27370 "junk at end of %<#pragma GCC pch_preprocess%>");
27371 }
27372 else
27373 error_at (first_token->location, "expected string literal");
27374
27375 /* Skip to the end of the pragma. */
27376 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27377 cp_lexer_get_preprocessor_token (NULL, first_token);
27378
27379 /* Now actually load the PCH file. */
27380 if (name)
27381 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27382
27383 /* Read one more token to return to our caller. We have to do this
27384 after reading the PCH file in, since its pointers have to be
27385 live. */
27386 cp_lexer_get_preprocessor_token (NULL, first_token);
27387 }
27388
27389 /* Normal parsing of a pragma token. Here we can (and must) use the
27390 regular lexer. */
27391
27392 static bool
27393 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27394 {
27395 cp_token *pragma_tok;
27396 unsigned int id;
27397
27398 pragma_tok = cp_lexer_consume_token (parser->lexer);
27399 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27400 parser->lexer->in_pragma = true;
27401
27402 id = pragma_tok->pragma_kind;
27403 switch (id)
27404 {
27405 case PRAGMA_GCC_PCH_PREPROCESS:
27406 error_at (pragma_tok->location,
27407 "%<#pragma GCC pch_preprocess%> must be first");
27408 break;
27409
27410 case PRAGMA_OMP_BARRIER:
27411 switch (context)
27412 {
27413 case pragma_compound:
27414 cp_parser_omp_barrier (parser, pragma_tok);
27415 return false;
27416 case pragma_stmt:
27417 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27418 "used in compound statements");
27419 break;
27420 default:
27421 goto bad_stmt;
27422 }
27423 break;
27424
27425 case PRAGMA_OMP_FLUSH:
27426 switch (context)
27427 {
27428 case pragma_compound:
27429 cp_parser_omp_flush (parser, pragma_tok);
27430 return false;
27431 case pragma_stmt:
27432 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27433 "used in compound statements");
27434 break;
27435 default:
27436 goto bad_stmt;
27437 }
27438 break;
27439
27440 case PRAGMA_OMP_TASKWAIT:
27441 switch (context)
27442 {
27443 case pragma_compound:
27444 cp_parser_omp_taskwait (parser, pragma_tok);
27445 return false;
27446 case pragma_stmt:
27447 error_at (pragma_tok->location,
27448 "%<#pragma omp taskwait%> may only be "
27449 "used in compound statements");
27450 break;
27451 default:
27452 goto bad_stmt;
27453 }
27454 break;
27455
27456 case PRAGMA_OMP_TASKYIELD:
27457 switch (context)
27458 {
27459 case pragma_compound:
27460 cp_parser_omp_taskyield (parser, pragma_tok);
27461 return false;
27462 case pragma_stmt:
27463 error_at (pragma_tok->location,
27464 "%<#pragma omp taskyield%> may only be "
27465 "used in compound statements");
27466 break;
27467 default:
27468 goto bad_stmt;
27469 }
27470 break;
27471
27472 case PRAGMA_OMP_THREADPRIVATE:
27473 cp_parser_omp_threadprivate (parser, pragma_tok);
27474 return false;
27475
27476 case PRAGMA_OMP_ATOMIC:
27477 case PRAGMA_OMP_CRITICAL:
27478 case PRAGMA_OMP_FOR:
27479 case PRAGMA_OMP_MASTER:
27480 case PRAGMA_OMP_ORDERED:
27481 case PRAGMA_OMP_PARALLEL:
27482 case PRAGMA_OMP_SECTIONS:
27483 case PRAGMA_OMP_SINGLE:
27484 case PRAGMA_OMP_TASK:
27485 if (context == pragma_external)
27486 goto bad_stmt;
27487 cp_parser_omp_construct (parser, pragma_tok);
27488 return true;
27489
27490 case PRAGMA_OMP_SECTION:
27491 error_at (pragma_tok->location,
27492 "%<#pragma omp section%> may only be used in "
27493 "%<#pragma omp sections%> construct");
27494 break;
27495
27496 default:
27497 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27498 c_invoke_pragma_handler (id);
27499 break;
27500
27501 bad_stmt:
27502 cp_parser_error (parser, "expected declaration specifiers");
27503 break;
27504 }
27505
27506 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27507 return false;
27508 }
27509
27510 /* The interface the pragma parsers have to the lexer. */
27511
27512 enum cpp_ttype
27513 pragma_lex (tree *value)
27514 {
27515 cp_token *tok;
27516 enum cpp_ttype ret;
27517
27518 tok = cp_lexer_peek_token (the_parser->lexer);
27519
27520 ret = tok->type;
27521 *value = tok->u.value;
27522
27523 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27524 ret = CPP_EOF;
27525 else if (ret == CPP_STRING)
27526 *value = cp_parser_string_literal (the_parser, false, false);
27527 else
27528 {
27529 cp_lexer_consume_token (the_parser->lexer);
27530 if (ret == CPP_KEYWORD)
27531 ret = CPP_NAME;
27532 }
27533
27534 return ret;
27535 }
27536
27537 \f
27538 /* External interface. */
27539
27540 /* Parse one entire translation unit. */
27541
27542 void
27543 c_parse_file (void)
27544 {
27545 static bool already_called = false;
27546
27547 if (already_called)
27548 {
27549 sorry ("inter-module optimizations not implemented for C++");
27550 return;
27551 }
27552 already_called = true;
27553
27554 the_parser = cp_parser_new ();
27555 push_deferring_access_checks (flag_access_control
27556 ? dk_no_deferred : dk_no_check);
27557 cp_parser_translation_unit (the_parser);
27558 the_parser = NULL;
27559 }
27560
27561 #include "gt-cp-parser.h"