/cp
[official-gcc.git] / gcc / cp / parser.c
blob85e0322e43f2671b67d0d269afbde8b87623b685
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>.
6 This file is part of GCC.
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.
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.
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/>. */
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"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token =
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
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;
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;
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 */
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 */
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;
181 /* Prototypes. */
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;
226 static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
229 static void cp_parser_initial_pragma
230 (cp_token *);
232 static tree cp_literal_operator_id
233 (const char *);
235 /* Manifest constants. */
236 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
237 #define CP_SAVED_TOKEN_STACK 5
239 /* Variables. */
241 /* The stream to which debugging output should be written. */
242 static FILE *cp_lexer_debug_stream;
244 /* Nonzero if we are parsing an unevaluated operand: an operand to
245 sizeof, typeof, or alignof. */
246 int cp_unevaluated_operand;
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 [[ ]]. */
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)
259 unsigned i, nprinted;
260 cp_token *token;
261 bool do_print;
263 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265 if (buffer == NULL)
266 return;
268 if (num == 0)
269 num = VEC_length (cp_token, buffer);
271 if (start_token == NULL)
272 start_token = VEC_address (cp_token, buffer);
274 if (start_token > VEC_address (cp_token, buffer))
276 cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
277 fprintf (file, " ... ");
280 do_print = false;
281 nprinted = 0;
282 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284 if (token == start_token)
285 do_print = true;
287 if (!do_print)
288 continue;
290 nprinted++;
291 if (token == curr_token)
292 fprintf (file, "[[");
294 cp_lexer_print_token (file, token);
296 if (token == curr_token)
297 fprintf (file, "]]");
299 switch (token->type)
301 case CPP_SEMICOLON:
302 case CPP_OPEN_BRACE:
303 case CPP_CLOSE_BRACE:
304 case CPP_EOF:
305 fputc ('\n', file);
306 break;
308 default:
309 fputc (' ', file);
313 if (i == num && i < VEC_length (cp_token, buffer))
315 fprintf (file, " ... ");
316 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
317 VEC_length (cp_token, buffer) - 1));
320 fprintf (file, "\n");
324 /* Dump all tokens in BUFFER to stderr. */
326 void
327 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
329 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
333 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
334 description for T. */
336 static void
337 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
339 if (t)
341 fprintf (file, "%s: ", desc);
342 print_node_brief (file, "", t, 0);
347 /* Dump parser context C to FILE. */
349 static void
350 cp_debug_print_context (FILE *file, cp_parser_context *c)
352 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
353 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
354 print_node_brief (file, "", c->object_type, 0);
355 fprintf (file, "}\n");
359 /* Print the stack of parsing contexts to FILE starting with FIRST. */
361 static void
362 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
364 unsigned i;
365 cp_parser_context *c;
367 fprintf (file, "Parsing context stack:\n");
368 for (i = 0, c = first; c; c = c->next, i++)
370 fprintf (file, "\t#%u: ", i);
371 cp_debug_print_context (file, c);
376 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
378 static void
379 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
381 if (flag)
382 fprintf (file, "%s: true\n", desc);
386 /* Print an unparsed function entry UF to FILE. */
388 static void
389 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
391 unsigned i;
392 cp_default_arg_entry *default_arg_fn;
393 tree fn;
395 fprintf (file, "\tFunctions with default args:\n");
396 for (i = 0;
397 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
398 default_arg_fn);
399 i++)
401 fprintf (file, "\t\tClass type: ");
402 print_node_brief (file, "", default_arg_fn->class_type, 0);
403 fprintf (file, "\t\tDeclaration: ");
404 print_node_brief (file, "", default_arg_fn->decl, 0);
405 fprintf (file, "\n");
408 fprintf (file, "\n\tFunctions with definitions that require "
409 "post-processing\n\t\t");
410 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
412 print_node_brief (file, "", fn, 0);
413 fprintf (file, " ");
415 fprintf (file, "\n");
417 fprintf (file, "\n\tNon-static data members with initializers that require "
418 "post-processing\n\t\t");
419 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
421 print_node_brief (file, "", fn, 0);
422 fprintf (file, " ");
424 fprintf (file, "\n");
428 /* Print the stack of unparsed member functions S to FILE. */
430 static void
431 cp_debug_print_unparsed_queues (FILE *file,
432 VEC(cp_unparsed_functions_entry, gc) *s)
434 unsigned i;
435 cp_unparsed_functions_entry *uf;
437 fprintf (file, "Unparsed functions\n");
438 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
440 fprintf (file, "#%u:\n", i);
441 cp_debug_print_unparsed_function (file, uf);
446 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
447 the given PARSER. If FILE is NULL, the output is printed on stderr. */
449 static void
450 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
452 cp_token *next_token, *first_token, *start_token;
454 if (file == NULL)
455 file = stderr;
457 next_token = parser->lexer->next_token;
458 first_token = VEC_address (cp_token, parser->lexer->buffer);
459 start_token = (next_token > first_token + window_size / 2)
460 ? next_token - window_size / 2
461 : first_token;
462 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
463 next_token);
467 /* Dump debugging information for the given PARSER. If FILE is NULL,
468 the output is printed on stderr. */
470 void
471 cp_debug_parser (FILE *file, cp_parser *parser)
473 const size_t window_size = 20;
474 cp_token *token;
475 expanded_location eloc;
477 if (file == NULL)
478 file = stderr;
480 fprintf (file, "Parser state\n\n");
481 fprintf (file, "Number of tokens: %u\n",
482 VEC_length (cp_token, parser->lexer->buffer));
483 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
484 cp_debug_print_tree_if_set (file, "Object scope",
485 parser->object_scope);
486 cp_debug_print_tree_if_set (file, "Qualifying scope",
487 parser->qualifying_scope);
488 cp_debug_print_context_stack (file, parser->context);
489 cp_debug_print_flag (file, "Allow GNU extensions",
490 parser->allow_gnu_extensions_p);
491 cp_debug_print_flag (file, "'>' token is greater-than",
492 parser->greater_than_is_operator_p);
493 cp_debug_print_flag (file, "Default args allowed in current "
494 "parameter list", parser->default_arg_ok_p);
495 cp_debug_print_flag (file, "Parsing integral constant-expression",
496 parser->integral_constant_expression_p);
497 cp_debug_print_flag (file, "Allow non-constant expression in current "
498 "constant-expression",
499 parser->allow_non_integral_constant_expression_p);
500 cp_debug_print_flag (file, "Seen non-constant expression",
501 parser->non_integral_constant_expression_p);
502 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
503 "current context",
504 parser->local_variables_forbidden_p);
505 cp_debug_print_flag (file, "In unbraced linkage specification",
506 parser->in_unbraced_linkage_specification_p);
507 cp_debug_print_flag (file, "Parsing a declarator",
508 parser->in_declarator_p);
509 cp_debug_print_flag (file, "In template argument list",
510 parser->in_template_argument_list_p);
511 cp_debug_print_flag (file, "Parsing an iteration statement",
512 parser->in_statement & IN_ITERATION_STMT);
513 cp_debug_print_flag (file, "Parsing a switch statement",
514 parser->in_statement & IN_SWITCH_STMT);
515 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
516 parser->in_statement & IN_OMP_BLOCK);
517 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
518 parser->in_statement & IN_OMP_FOR);
519 cp_debug_print_flag (file, "Parsing an if statement",
520 parser->in_statement & IN_IF_STMT);
521 cp_debug_print_flag (file, "Parsing a type-id in an expression "
522 "context", parser->in_type_id_in_expr_p);
523 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
524 parser->implicit_extern_c);
525 cp_debug_print_flag (file, "String expressions should be translated "
526 "to execution character set",
527 parser->translate_strings_p);
528 cp_debug_print_flag (file, "Parsing function body outside of a "
529 "local class", parser->in_function_body);
530 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
531 parser->colon_corrects_to_scope_p);
532 if (parser->type_definition_forbidden_message)
533 fprintf (file, "Error message for forbidden type definitions: %s\n",
534 parser->type_definition_forbidden_message);
535 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
536 fprintf (file, "Number of class definitions in progress: %u\n",
537 parser->num_classes_being_defined);
538 fprintf (file, "Number of template parameter lists for the current "
539 "declaration: %u\n", parser->num_template_parameter_lists);
540 cp_debug_parser_tokens (file, parser, window_size);
541 token = parser->lexer->next_token;
542 fprintf (file, "Next token to parse:\n");
543 fprintf (file, "\tToken: ");
544 cp_lexer_print_token (file, token);
545 eloc = expand_location (token->location);
546 fprintf (file, "\n\tFile: %s\n", eloc.file);
547 fprintf (file, "\tLine: %d\n", eloc.line);
548 fprintf (file, "\tColumn: %d\n", eloc.column);
552 /* Allocate memory for a new lexer object and return it. */
554 static cp_lexer *
555 cp_lexer_alloc (void)
557 cp_lexer *lexer;
559 c_common_no_more_pch ();
561 /* Allocate the memory. */
562 lexer = ggc_alloc_cleared_cp_lexer ();
564 /* Initially we are not debugging. */
565 lexer->debugging_p = false;
567 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
568 CP_SAVED_TOKEN_STACK);
570 /* Create the buffer. */
571 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
573 return lexer;
577 /* Create a new main C++ lexer, the lexer that gets tokens from the
578 preprocessor. */
580 static cp_lexer *
581 cp_lexer_new_main (void)
583 cp_lexer *lexer;
584 cp_token token;
586 /* It's possible that parsing the first pragma will load a PCH file,
587 which is a GC collection point. So we have to do that before
588 allocating any memory. */
589 cp_parser_initial_pragma (&token);
591 lexer = cp_lexer_alloc ();
593 /* Put the first token in the buffer. */
594 VEC_quick_push (cp_token, lexer->buffer, &token);
596 /* Get the remaining tokens from the preprocessor. */
597 while (token.type != CPP_EOF)
599 cp_lexer_get_preprocessor_token (lexer, &token);
600 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
603 lexer->last_token = VEC_address (cp_token, lexer->buffer)
604 + VEC_length (cp_token, lexer->buffer)
605 - 1;
606 lexer->next_token = VEC_length (cp_token, lexer->buffer)
607 ? VEC_address (cp_token, lexer->buffer)
608 : &eof_token;
610 /* Subsequent preprocessor diagnostics should use compiler
611 diagnostic functions to get the compiler source location. */
612 done_lexing = true;
614 gcc_assert (!lexer->next_token->purged_p);
615 return lexer;
618 /* Create a new lexer whose token stream is primed with the tokens in
619 CACHE. When these tokens are exhausted, no new tokens will be read. */
621 static cp_lexer *
622 cp_lexer_new_from_tokens (cp_token_cache *cache)
624 cp_token *first = cache->first;
625 cp_token *last = cache->last;
626 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
628 /* We do not own the buffer. */
629 lexer->buffer = NULL;
630 lexer->next_token = first == last ? &eof_token : first;
631 lexer->last_token = last;
633 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
634 CP_SAVED_TOKEN_STACK);
636 /* Initially we are not debugging. */
637 lexer->debugging_p = false;
639 gcc_assert (!lexer->next_token->purged_p);
640 return lexer;
643 /* Frees all resources associated with LEXER. */
645 static void
646 cp_lexer_destroy (cp_lexer *lexer)
648 VEC_free (cp_token, gc, lexer->buffer);
649 VEC_free (cp_token_position, heap, lexer->saved_tokens);
650 ggc_free (lexer);
653 /* Returns nonzero if debugging information should be output. */
655 static inline bool
656 cp_lexer_debugging_p (cp_lexer *lexer)
658 return lexer->debugging_p;
662 static inline cp_token_position
663 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
665 gcc_assert (!previous_p || lexer->next_token != &eof_token);
667 return lexer->next_token - previous_p;
670 static inline cp_token *
671 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
673 return pos;
676 static inline void
677 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
679 lexer->next_token = cp_lexer_token_at (lexer, pos);
682 static inline cp_token_position
683 cp_lexer_previous_token_position (cp_lexer *lexer)
685 if (lexer->next_token == &eof_token)
686 return lexer->last_token - 1;
687 else
688 return cp_lexer_token_position (lexer, true);
691 static inline cp_token *
692 cp_lexer_previous_token (cp_lexer *lexer)
694 cp_token_position tp = cp_lexer_previous_token_position (lexer);
696 return cp_lexer_token_at (lexer, tp);
699 /* nonzero if we are presently saving tokens. */
701 static inline int
702 cp_lexer_saving_tokens (const cp_lexer* lexer)
704 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
707 /* Store the next token from the preprocessor in *TOKEN. Return true
708 if we reach EOF. If LEXER is NULL, assume we are handling an
709 initial #pragma pch_preprocess, and thus want the lexer to return
710 processed strings. */
712 static void
713 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
715 static int is_extern_c = 0;
717 /* Get a new token from the preprocessor. */
718 token->type
719 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
720 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
721 token->keyword = RID_MAX;
722 token->pragma_kind = PRAGMA_NONE;
723 token->purged_p = false;
725 /* On some systems, some header files are surrounded by an
726 implicit extern "C" block. Set a flag in the token if it
727 comes from such a header. */
728 is_extern_c += pending_lang_change;
729 pending_lang_change = 0;
730 token->implicit_extern_c = is_extern_c > 0;
732 /* Check to see if this token is a keyword. */
733 if (token->type == CPP_NAME)
735 if (C_IS_RESERVED_WORD (token->u.value))
737 /* Mark this token as a keyword. */
738 token->type = CPP_KEYWORD;
739 /* Record which keyword. */
740 token->keyword = C_RID_CODE (token->u.value);
742 else
744 if (warn_cxx0x_compat
745 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
746 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
748 /* Warn about the C++0x keyword (but still treat it as
749 an identifier). */
750 warning (OPT_Wc__0x_compat,
751 "identifier %qE is a keyword in C++11",
752 token->u.value);
754 /* Clear out the C_RID_CODE so we don't warn about this
755 particular identifier-turned-keyword again. */
756 C_SET_RID_CODE (token->u.value, RID_MAX);
759 token->ambiguous_p = false;
760 token->keyword = RID_MAX;
763 else if (token->type == CPP_AT_NAME)
765 /* This only happens in Objective-C++; it must be a keyword. */
766 token->type = CPP_KEYWORD;
767 switch (C_RID_CODE (token->u.value))
769 /* Replace 'class' with '@class', 'private' with '@private',
770 etc. This prevents confusion with the C++ keyword
771 'class', and makes the tokens consistent with other
772 Objective-C 'AT' keywords. For example '@class' is
773 reported as RID_AT_CLASS which is consistent with
774 '@synchronized', which is reported as
775 RID_AT_SYNCHRONIZED.
777 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
778 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
779 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
780 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
781 case RID_THROW: token->keyword = RID_AT_THROW; break;
782 case RID_TRY: token->keyword = RID_AT_TRY; break;
783 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
784 default: token->keyword = C_RID_CODE (token->u.value);
787 else if (token->type == CPP_PRAGMA)
789 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
790 token->pragma_kind = ((enum pragma_kind)
791 TREE_INT_CST_LOW (token->u.value));
792 token->u.value = NULL_TREE;
796 /* Update the globals input_location and the input file stack from TOKEN. */
797 static inline void
798 cp_lexer_set_source_position_from_token (cp_token *token)
800 if (token->type != CPP_EOF)
802 input_location = token->location;
806 /* Return a pointer to the next token in the token stream, but do not
807 consume it. */
809 static inline cp_token *
810 cp_lexer_peek_token (cp_lexer *lexer)
812 if (cp_lexer_debugging_p (lexer))
814 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
815 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
816 putc ('\n', cp_lexer_debug_stream);
818 return lexer->next_token;
821 /* Return true if the next token has the indicated TYPE. */
823 static inline bool
824 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
826 return cp_lexer_peek_token (lexer)->type == type;
829 /* Return true if the next token does not have the indicated TYPE. */
831 static inline bool
832 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
834 return !cp_lexer_next_token_is (lexer, type);
837 /* Return true if the next token is the indicated KEYWORD. */
839 static inline bool
840 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
842 return cp_lexer_peek_token (lexer)->keyword == keyword;
845 /* Return true if the next token is not the indicated KEYWORD. */
847 static inline bool
848 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
850 return cp_lexer_peek_token (lexer)->keyword != keyword;
853 /* Return true if the next token is a keyword for a decl-specifier. */
855 static bool
856 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
858 cp_token *token;
860 token = cp_lexer_peek_token (lexer);
861 switch (token->keyword)
863 /* auto specifier: storage-class-specifier in C++,
864 simple-type-specifier in C++0x. */
865 case RID_AUTO:
866 /* Storage classes. */
867 case RID_REGISTER:
868 case RID_STATIC:
869 case RID_EXTERN:
870 case RID_MUTABLE:
871 case RID_THREAD:
872 /* Elaborated type specifiers. */
873 case RID_ENUM:
874 case RID_CLASS:
875 case RID_STRUCT:
876 case RID_UNION:
877 case RID_TYPENAME:
878 /* Simple type specifiers. */
879 case RID_CHAR:
880 case RID_CHAR16:
881 case RID_CHAR32:
882 case RID_WCHAR:
883 case RID_BOOL:
884 case RID_SHORT:
885 case RID_INT:
886 case RID_LONG:
887 case RID_INT128:
888 case RID_SIGNED:
889 case RID_UNSIGNED:
890 case RID_FLOAT:
891 case RID_DOUBLE:
892 case RID_VOID:
893 /* GNU extensions. */
894 case RID_ATTRIBUTE:
895 case RID_TYPEOF:
896 /* C++0x extensions. */
897 case RID_DECLTYPE:
898 case RID_UNDERLYING_TYPE:
899 return true;
901 default:
902 return false;
906 /* Returns TRUE iff the token T begins a decltype type. */
908 static bool
909 token_is_decltype (cp_token *t)
911 return (t->keyword == RID_DECLTYPE
912 || t->type == CPP_DECLTYPE);
915 /* Returns TRUE iff the next token begins a decltype type. */
917 static bool
918 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
920 cp_token *t = cp_lexer_peek_token (lexer);
921 return token_is_decltype (t);
924 /* Return a pointer to the Nth token in the token stream. If N is 1,
925 then this is precisely equivalent to cp_lexer_peek_token (except
926 that it is not inline). One would like to disallow that case, but
927 there is one case (cp_parser_nth_token_starts_template_id) where
928 the caller passes a variable for N and it might be 1. */
930 static cp_token *
931 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
933 cp_token *token;
935 /* N is 1-based, not zero-based. */
936 gcc_assert (n > 0);
938 if (cp_lexer_debugging_p (lexer))
939 fprintf (cp_lexer_debug_stream,
940 "cp_lexer: peeking ahead %ld at token: ", (long)n);
942 --n;
943 token = lexer->next_token;
944 gcc_assert (!n || token != &eof_token);
945 while (n != 0)
947 ++token;
948 if (token == lexer->last_token)
950 token = &eof_token;
951 break;
954 if (!token->purged_p)
955 --n;
958 if (cp_lexer_debugging_p (lexer))
960 cp_lexer_print_token (cp_lexer_debug_stream, token);
961 putc ('\n', cp_lexer_debug_stream);
964 return token;
967 /* Return the next token, and advance the lexer's next_token pointer
968 to point to the next non-purged token. */
970 static cp_token *
971 cp_lexer_consume_token (cp_lexer* lexer)
973 cp_token *token = lexer->next_token;
975 gcc_assert (token != &eof_token);
976 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
980 lexer->next_token++;
981 if (lexer->next_token == lexer->last_token)
983 lexer->next_token = &eof_token;
984 break;
988 while (lexer->next_token->purged_p);
990 cp_lexer_set_source_position_from_token (token);
992 /* Provide debugging output. */
993 if (cp_lexer_debugging_p (lexer))
995 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
996 cp_lexer_print_token (cp_lexer_debug_stream, token);
997 putc ('\n', cp_lexer_debug_stream);
1000 return token;
1003 /* Permanently remove the next token from the token stream, and
1004 advance the next_token pointer to refer to the next non-purged
1005 token. */
1007 static void
1008 cp_lexer_purge_token (cp_lexer *lexer)
1010 cp_token *tok = lexer->next_token;
1012 gcc_assert (tok != &eof_token);
1013 tok->purged_p = true;
1014 tok->location = UNKNOWN_LOCATION;
1015 tok->u.value = NULL_TREE;
1016 tok->keyword = RID_MAX;
1020 tok++;
1021 if (tok == lexer->last_token)
1023 tok = &eof_token;
1024 break;
1027 while (tok->purged_p);
1028 lexer->next_token = tok;
1031 /* Permanently remove all tokens after TOK, up to, but not
1032 including, the token that will be returned next by
1033 cp_lexer_peek_token. */
1035 static void
1036 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1038 cp_token *peek = lexer->next_token;
1040 if (peek == &eof_token)
1041 peek = lexer->last_token;
1043 gcc_assert (tok < peek);
1045 for ( tok += 1; tok != peek; tok += 1)
1047 tok->purged_p = true;
1048 tok->location = UNKNOWN_LOCATION;
1049 tok->u.value = NULL_TREE;
1050 tok->keyword = RID_MAX;
1054 /* Begin saving tokens. All tokens consumed after this point will be
1055 preserved. */
1057 static void
1058 cp_lexer_save_tokens (cp_lexer* lexer)
1060 /* Provide debugging output. */
1061 if (cp_lexer_debugging_p (lexer))
1062 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1064 VEC_safe_push (cp_token_position, heap,
1065 lexer->saved_tokens, lexer->next_token);
1068 /* Commit to the portion of the token stream most recently saved. */
1070 static void
1071 cp_lexer_commit_tokens (cp_lexer* lexer)
1073 /* Provide debugging output. */
1074 if (cp_lexer_debugging_p (lexer))
1075 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1077 VEC_pop (cp_token_position, lexer->saved_tokens);
1080 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1081 to the token stream. Stop saving tokens. */
1083 static void
1084 cp_lexer_rollback_tokens (cp_lexer* lexer)
1086 /* Provide debugging output. */
1087 if (cp_lexer_debugging_p (lexer))
1088 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1090 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1093 /* Print a representation of the TOKEN on the STREAM. */
1095 static void
1096 cp_lexer_print_token (FILE * stream, cp_token *token)
1098 /* We don't use cpp_type2name here because the parser defines
1099 a few tokens of its own. */
1100 static const char *const token_names[] = {
1101 /* cpplib-defined token types */
1102 #define OP(e, s) #e,
1103 #define TK(e, s) #e,
1104 TTYPE_TABLE
1105 #undef OP
1106 #undef TK
1107 /* C++ parser token types - see "Manifest constants", above. */
1108 "KEYWORD",
1109 "TEMPLATE_ID",
1110 "NESTED_NAME_SPECIFIER",
1113 /* For some tokens, print the associated data. */
1114 switch (token->type)
1116 case CPP_KEYWORD:
1117 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1118 For example, `struct' is mapped to an INTEGER_CST. */
1119 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1120 break;
1121 /* else fall through */
1122 case CPP_NAME:
1123 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1124 break;
1126 case CPP_STRING:
1127 case CPP_STRING16:
1128 case CPP_STRING32:
1129 case CPP_WSTRING:
1130 case CPP_UTF8STRING:
1131 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1132 break;
1134 case CPP_NUMBER:
1135 print_generic_expr (stream, token->u.value, 0);
1136 break;
1138 default:
1139 /* If we have a name for the token, print it out. Otherwise, we
1140 simply give the numeric code. */
1141 if (token->type < ARRAY_SIZE(token_names))
1142 fputs (token_names[token->type], stream);
1143 else
1144 fprintf (stream, "[%d]", token->type);
1145 break;
1149 /* Start emitting debugging information. */
1151 static void
1152 cp_lexer_start_debugging (cp_lexer* lexer)
1154 lexer->debugging_p = true;
1155 cp_lexer_debug_stream = stderr;
1158 /* Stop emitting debugging information. */
1160 static void
1161 cp_lexer_stop_debugging (cp_lexer* lexer)
1163 lexer->debugging_p = false;
1164 cp_lexer_debug_stream = NULL;
1167 /* Create a new cp_token_cache, representing a range of tokens. */
1169 static cp_token_cache *
1170 cp_token_cache_new (cp_token *first, cp_token *last)
1172 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1173 cache->first = first;
1174 cache->last = last;
1175 return cache;
1179 /* Decl-specifiers. */
1181 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1183 static void
1184 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1186 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1189 /* Declarators. */
1191 /* Nothing other than the parser should be creating declarators;
1192 declarators are a semi-syntactic representation of C++ entities.
1193 Other parts of the front end that need to create entities (like
1194 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1196 static cp_declarator *make_call_declarator
1197 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1198 static cp_declarator *make_array_declarator
1199 (cp_declarator *, tree);
1200 static cp_declarator *make_pointer_declarator
1201 (cp_cv_quals, cp_declarator *);
1202 static cp_declarator *make_reference_declarator
1203 (cp_cv_quals, cp_declarator *, bool);
1204 static cp_parameter_declarator *make_parameter_declarator
1205 (cp_decl_specifier_seq *, cp_declarator *, tree);
1206 static cp_declarator *make_ptrmem_declarator
1207 (cp_cv_quals, tree, cp_declarator *);
1209 /* An erroneous declarator. */
1210 static cp_declarator *cp_error_declarator;
1212 /* The obstack on which declarators and related data structures are
1213 allocated. */
1214 static struct obstack declarator_obstack;
1216 /* Alloc BYTES from the declarator memory pool. */
1218 static inline void *
1219 alloc_declarator (size_t bytes)
1221 return obstack_alloc (&declarator_obstack, bytes);
1224 /* Allocate a declarator of the indicated KIND. Clear fields that are
1225 common to all declarators. */
1227 static cp_declarator *
1228 make_declarator (cp_declarator_kind kind)
1230 cp_declarator *declarator;
1232 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1233 declarator->kind = kind;
1234 declarator->attributes = NULL_TREE;
1235 declarator->declarator = NULL;
1236 declarator->parameter_pack_p = false;
1237 declarator->id_loc = UNKNOWN_LOCATION;
1239 return declarator;
1242 /* Make a declarator for a generalized identifier. If
1243 QUALIFYING_SCOPE is non-NULL, the identifier is
1244 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1245 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1246 is, if any. */
1248 static cp_declarator *
1249 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1250 special_function_kind sfk)
1252 cp_declarator *declarator;
1254 /* It is valid to write:
1256 class C { void f(); };
1257 typedef C D;
1258 void D::f();
1260 The standard is not clear about whether `typedef const C D' is
1261 legal; as of 2002-09-15 the committee is considering that
1262 question. EDG 3.0 allows that syntax. Therefore, we do as
1263 well. */
1264 if (qualifying_scope && TYPE_P (qualifying_scope))
1265 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1268 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1269 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271 declarator = make_declarator (cdk_id);
1272 declarator->u.id.qualifying_scope = qualifying_scope;
1273 declarator->u.id.unqualified_name = unqualified_name;
1274 declarator->u.id.sfk = sfk;
1276 return declarator;
1279 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1280 of modifiers such as const or volatile to apply to the pointer
1281 type, represented as identifiers. */
1283 cp_declarator *
1284 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1286 cp_declarator *declarator;
1288 declarator = make_declarator (cdk_pointer);
1289 declarator->declarator = target;
1290 declarator->u.pointer.qualifiers = cv_qualifiers;
1291 declarator->u.pointer.class_type = NULL_TREE;
1292 if (target)
1294 declarator->id_loc = target->id_loc;
1295 declarator->parameter_pack_p = target->parameter_pack_p;
1296 target->parameter_pack_p = false;
1298 else
1299 declarator->parameter_pack_p = false;
1301 return declarator;
1304 /* Like make_pointer_declarator -- but for references. */
1306 cp_declarator *
1307 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1308 bool rvalue_ref)
1310 cp_declarator *declarator;
1312 declarator = make_declarator (cdk_reference);
1313 declarator->declarator = target;
1314 declarator->u.reference.qualifiers = cv_qualifiers;
1315 declarator->u.reference.rvalue_ref = rvalue_ref;
1316 if (target)
1318 declarator->id_loc = target->id_loc;
1319 declarator->parameter_pack_p = target->parameter_pack_p;
1320 target->parameter_pack_p = false;
1322 else
1323 declarator->parameter_pack_p = false;
1325 return declarator;
1328 /* Like make_pointer_declarator -- but for a pointer to a non-static
1329 member of CLASS_TYPE. */
1331 cp_declarator *
1332 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1333 cp_declarator *pointee)
1335 cp_declarator *declarator;
1337 declarator = make_declarator (cdk_ptrmem);
1338 declarator->declarator = pointee;
1339 declarator->u.pointer.qualifiers = cv_qualifiers;
1340 declarator->u.pointer.class_type = class_type;
1342 if (pointee)
1344 declarator->parameter_pack_p = pointee->parameter_pack_p;
1345 pointee->parameter_pack_p = false;
1347 else
1348 declarator->parameter_pack_p = false;
1350 return declarator;
1353 /* Make a declarator for the function given by TARGET, with the
1354 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1355 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1356 indicates what exceptions can be thrown. */
1358 cp_declarator *
1359 make_call_declarator (cp_declarator *target,
1360 tree parms,
1361 cp_cv_quals cv_qualifiers,
1362 cp_virt_specifiers virt_specifiers,
1363 tree exception_specification,
1364 tree late_return_type)
1366 cp_declarator *declarator;
1368 declarator = make_declarator (cdk_function);
1369 declarator->declarator = target;
1370 declarator->u.function.parameters = parms;
1371 declarator->u.function.qualifiers = cv_qualifiers;
1372 declarator->u.function.virt_specifiers = virt_specifiers;
1373 declarator->u.function.exception_specification = exception_specification;
1374 declarator->u.function.late_return_type = late_return_type;
1375 if (target)
1377 declarator->id_loc = target->id_loc;
1378 declarator->parameter_pack_p = target->parameter_pack_p;
1379 target->parameter_pack_p = false;
1381 else
1382 declarator->parameter_pack_p = false;
1384 return declarator;
1387 /* Make a declarator for an array of BOUNDS elements, each of which is
1388 defined by ELEMENT. */
1390 cp_declarator *
1391 make_array_declarator (cp_declarator *element, tree bounds)
1393 cp_declarator *declarator;
1395 declarator = make_declarator (cdk_array);
1396 declarator->declarator = element;
1397 declarator->u.array.bounds = bounds;
1398 if (element)
1400 declarator->id_loc = element->id_loc;
1401 declarator->parameter_pack_p = element->parameter_pack_p;
1402 element->parameter_pack_p = false;
1404 else
1405 declarator->parameter_pack_p = false;
1407 return declarator;
1410 /* Determine whether the declarator we've seen so far can be a
1411 parameter pack, when followed by an ellipsis. */
1412 static bool
1413 declarator_can_be_parameter_pack (cp_declarator *declarator)
1415 /* Search for a declarator name, or any other declarator that goes
1416 after the point where the ellipsis could appear in a parameter
1417 pack. If we find any of these, then this declarator can not be
1418 made into a parameter pack. */
1419 bool found = false;
1420 while (declarator && !found)
1422 switch ((int)declarator->kind)
1424 case cdk_id:
1425 case cdk_array:
1426 found = true;
1427 break;
1429 case cdk_error:
1430 return true;
1432 default:
1433 declarator = declarator->declarator;
1434 break;
1438 return !found;
1441 cp_parameter_declarator *no_parameters;
1443 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1444 DECLARATOR and DEFAULT_ARGUMENT. */
1446 cp_parameter_declarator *
1447 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1448 cp_declarator *declarator,
1449 tree default_argument)
1451 cp_parameter_declarator *parameter;
1453 parameter = ((cp_parameter_declarator *)
1454 alloc_declarator (sizeof (cp_parameter_declarator)));
1455 parameter->next = NULL;
1456 if (decl_specifiers)
1457 parameter->decl_specifiers = *decl_specifiers;
1458 else
1459 clear_decl_specs (&parameter->decl_specifiers);
1460 parameter->declarator = declarator;
1461 parameter->default_argument = default_argument;
1462 parameter->ellipsis_p = false;
1464 return parameter;
1467 /* Returns true iff DECLARATOR is a declaration for a function. */
1469 static bool
1470 function_declarator_p (const cp_declarator *declarator)
1472 while (declarator)
1474 if (declarator->kind == cdk_function
1475 && declarator->declarator->kind == cdk_id)
1476 return true;
1477 if (declarator->kind == cdk_id
1478 || declarator->kind == cdk_error)
1479 return false;
1480 declarator = declarator->declarator;
1482 return false;
1485 /* The parser. */
1487 /* Overview
1488 --------
1490 A cp_parser parses the token stream as specified by the C++
1491 grammar. Its job is purely parsing, not semantic analysis. For
1492 example, the parser breaks the token stream into declarators,
1493 expressions, statements, and other similar syntactic constructs.
1494 It does not check that the types of the expressions on either side
1495 of an assignment-statement are compatible, or that a function is
1496 not declared with a parameter of type `void'.
1498 The parser invokes routines elsewhere in the compiler to perform
1499 semantic analysis and to build up the abstract syntax tree for the
1500 code processed.
1502 The parser (and the template instantiation code, which is, in a
1503 way, a close relative of parsing) are the only parts of the
1504 compiler that should be calling push_scope and pop_scope, or
1505 related functions. The parser (and template instantiation code)
1506 keeps track of what scope is presently active; everything else
1507 should simply honor that. (The code that generates static
1508 initializers may also need to set the scope, in order to check
1509 access control correctly when emitting the initializers.)
1511 Methodology
1512 -----------
1514 The parser is of the standard recursive-descent variety. Upcoming
1515 tokens in the token stream are examined in order to determine which
1516 production to use when parsing a non-terminal. Some C++ constructs
1517 require arbitrary look ahead to disambiguate. For example, it is
1518 impossible, in the general case, to tell whether a statement is an
1519 expression or declaration without scanning the entire statement.
1520 Therefore, the parser is capable of "parsing tentatively." When the
1521 parser is not sure what construct comes next, it enters this mode.
1522 Then, while we attempt to parse the construct, the parser queues up
1523 error messages, rather than issuing them immediately, and saves the
1524 tokens it consumes. If the construct is parsed successfully, the
1525 parser "commits", i.e., it issues any queued error messages and
1526 the tokens that were being preserved are permanently discarded.
1527 If, however, the construct is not parsed successfully, the parser
1528 rolls back its state completely so that it can resume parsing using
1529 a different alternative.
1531 Future Improvements
1532 -------------------
1534 The performance of the parser could probably be improved substantially.
1535 We could often eliminate the need to parse tentatively by looking ahead
1536 a little bit. In some places, this approach might not entirely eliminate
1537 the need to parse tentatively, but it might still speed up the average
1538 case. */
1540 /* Flags that are passed to some parsing functions. These values can
1541 be bitwise-ored together. */
1543 enum
1545 /* No flags. */
1546 CP_PARSER_FLAGS_NONE = 0x0,
1547 /* The construct is optional. If it is not present, then no error
1548 should be issued. */
1549 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1550 /* When parsing a type-specifier, treat user-defined type-names
1551 as non-type identifiers. */
1552 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1553 /* When parsing a type-specifier, do not try to parse a class-specifier
1554 or enum-specifier. */
1555 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1556 /* When parsing a decl-specifier-seq, only allow type-specifier or
1557 constexpr. */
1558 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1561 /* This type is used for parameters and variables which hold
1562 combinations of the above flags. */
1563 typedef int cp_parser_flags;
1565 /* The different kinds of declarators we want to parse. */
1567 typedef enum cp_parser_declarator_kind
1569 /* We want an abstract declarator. */
1570 CP_PARSER_DECLARATOR_ABSTRACT,
1571 /* We want a named declarator. */
1572 CP_PARSER_DECLARATOR_NAMED,
1573 /* We don't mind, but the name must be an unqualified-id. */
1574 CP_PARSER_DECLARATOR_EITHER
1575 } cp_parser_declarator_kind;
1577 /* The precedence values used to parse binary expressions. The minimum value
1578 of PREC must be 1, because zero is reserved to quickly discriminate
1579 binary operators from other tokens. */
1581 enum cp_parser_prec
1583 PREC_NOT_OPERATOR,
1584 PREC_LOGICAL_OR_EXPRESSION,
1585 PREC_LOGICAL_AND_EXPRESSION,
1586 PREC_INCLUSIVE_OR_EXPRESSION,
1587 PREC_EXCLUSIVE_OR_EXPRESSION,
1588 PREC_AND_EXPRESSION,
1589 PREC_EQUALITY_EXPRESSION,
1590 PREC_RELATIONAL_EXPRESSION,
1591 PREC_SHIFT_EXPRESSION,
1592 PREC_ADDITIVE_EXPRESSION,
1593 PREC_MULTIPLICATIVE_EXPRESSION,
1594 PREC_PM_EXPRESSION,
1595 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1598 /* A mapping from a token type to a corresponding tree node type, with a
1599 precedence value. */
1601 typedef struct cp_parser_binary_operations_map_node
1603 /* The token type. */
1604 enum cpp_ttype token_type;
1605 /* The corresponding tree code. */
1606 enum tree_code tree_type;
1607 /* The precedence of this operator. */
1608 enum cp_parser_prec prec;
1609 } cp_parser_binary_operations_map_node;
1611 typedef struct cp_parser_expression_stack_entry
1613 /* Left hand side of the binary operation we are currently
1614 parsing. */
1615 tree lhs;
1616 /* Original tree code for left hand side, if it was a binary
1617 expression itself (used for -Wparentheses). */
1618 enum tree_code lhs_type;
1619 /* Tree code for the binary operation we are parsing. */
1620 enum tree_code tree_type;
1621 /* Precedence of the binary operation we are parsing. */
1622 enum cp_parser_prec prec;
1623 /* Location of the binary operation we are parsing. */
1624 location_t loc;
1625 } cp_parser_expression_stack_entry;
1627 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1628 entries because precedence levels on the stack are monotonically
1629 increasing. */
1630 typedef struct cp_parser_expression_stack_entry
1631 cp_parser_expression_stack[NUM_PREC_VALUES];
1633 /* Prototypes. */
1635 /* Constructors and destructors. */
1637 static cp_parser_context *cp_parser_context_new
1638 (cp_parser_context *);
1640 /* Class variables. */
1642 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1644 /* The operator-precedence table used by cp_parser_binary_expression.
1645 Transformed into an associative array (binops_by_token) by
1646 cp_parser_new. */
1648 static const cp_parser_binary_operations_map_node binops[] = {
1649 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1650 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1652 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1656 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1659 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1662 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1664 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1667 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1668 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1670 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1672 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1674 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1676 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1678 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1681 /* The same as binops, but initialized by cp_parser_new so that
1682 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1683 for speed. */
1684 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1686 /* Constructors and destructors. */
1688 /* Construct a new context. The context below this one on the stack
1689 is given by NEXT. */
1691 static cp_parser_context *
1692 cp_parser_context_new (cp_parser_context* next)
1694 cp_parser_context *context;
1696 /* Allocate the storage. */
1697 if (cp_parser_context_free_list != NULL)
1699 /* Pull the first entry from the free list. */
1700 context = cp_parser_context_free_list;
1701 cp_parser_context_free_list = context->next;
1702 memset (context, 0, sizeof (*context));
1704 else
1705 context = ggc_alloc_cleared_cp_parser_context ();
1707 /* No errors have occurred yet in this context. */
1708 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1709 /* If this is not the bottommost context, copy information that we
1710 need from the previous context. */
1711 if (next)
1713 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1714 expression, then we are parsing one in this context, too. */
1715 context->object_type = next->object_type;
1716 /* Thread the stack. */
1717 context->next = next;
1720 return context;
1723 /* Managing the unparsed function queues. */
1725 #define unparsed_funs_with_default_args \
1726 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1727 #define unparsed_funs_with_definitions \
1728 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1729 #define unparsed_nsdmis \
1730 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1732 static void
1733 push_unparsed_function_queues (cp_parser *parser)
1735 VEC_safe_push (cp_unparsed_functions_entry, gc,
1736 parser->unparsed_queues, NULL);
1737 unparsed_funs_with_default_args = NULL;
1738 unparsed_funs_with_definitions = make_tree_vector ();
1739 unparsed_nsdmis = NULL;
1742 static void
1743 pop_unparsed_function_queues (cp_parser *parser)
1745 release_tree_vector (unparsed_funs_with_definitions);
1746 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1749 /* Prototypes. */
1751 /* Constructors and destructors. */
1753 static cp_parser *cp_parser_new
1754 (void);
1756 /* Routines to parse various constructs.
1758 Those that return `tree' will return the error_mark_node (rather
1759 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1760 Sometimes, they will return an ordinary node if error-recovery was
1761 attempted, even though a parse error occurred. So, to check
1762 whether or not a parse error occurred, you should always use
1763 cp_parser_error_occurred. If the construct is optional (indicated
1764 either by an `_opt' in the name of the function that does the
1765 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1766 the construct is not present. */
1768 /* Lexical conventions [gram.lex] */
1770 static tree cp_parser_identifier
1771 (cp_parser *);
1772 static tree cp_parser_string_literal
1773 (cp_parser *, bool, bool);
1774 static tree cp_parser_userdef_char_literal
1775 (cp_parser *);
1776 static tree cp_parser_userdef_string_literal
1777 (cp_token *);
1778 static tree cp_parser_userdef_numeric_literal
1779 (cp_parser *);
1781 /* Basic concepts [gram.basic] */
1783 static bool cp_parser_translation_unit
1784 (cp_parser *);
1786 /* Expressions [gram.expr] */
1788 static tree cp_parser_primary_expression
1789 (cp_parser *, bool, bool, bool, cp_id_kind *);
1790 static tree cp_parser_id_expression
1791 (cp_parser *, bool, bool, bool *, bool, bool);
1792 static tree cp_parser_unqualified_id
1793 (cp_parser *, bool, bool, bool, bool);
1794 static tree cp_parser_nested_name_specifier_opt
1795 (cp_parser *, bool, bool, bool, bool);
1796 static tree cp_parser_nested_name_specifier
1797 (cp_parser *, bool, bool, bool, bool);
1798 static tree cp_parser_qualifying_entity
1799 (cp_parser *, bool, bool, bool, bool, bool);
1800 static tree cp_parser_postfix_expression
1801 (cp_parser *, bool, bool, bool, cp_id_kind *);
1802 static tree cp_parser_postfix_open_square_expression
1803 (cp_parser *, tree, bool);
1804 static tree cp_parser_postfix_dot_deref_expression
1805 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1806 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1807 (cp_parser *, int, bool, bool, bool *);
1808 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1809 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1810 static void cp_parser_pseudo_destructor_name
1811 (cp_parser *, tree *, tree *);
1812 static tree cp_parser_unary_expression
1813 (cp_parser *, bool, bool, cp_id_kind *);
1814 static enum tree_code cp_parser_unary_operator
1815 (cp_token *);
1816 static tree cp_parser_new_expression
1817 (cp_parser *);
1818 static VEC(tree,gc) *cp_parser_new_placement
1819 (cp_parser *);
1820 static tree cp_parser_new_type_id
1821 (cp_parser *, tree *);
1822 static cp_declarator *cp_parser_new_declarator_opt
1823 (cp_parser *);
1824 static cp_declarator *cp_parser_direct_new_declarator
1825 (cp_parser *);
1826 static VEC(tree,gc) *cp_parser_new_initializer
1827 (cp_parser *);
1828 static tree cp_parser_delete_expression
1829 (cp_parser *);
1830 static tree cp_parser_cast_expression
1831 (cp_parser *, bool, bool, cp_id_kind *);
1832 static tree cp_parser_binary_expression
1833 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1834 static tree cp_parser_question_colon_clause
1835 (cp_parser *, tree);
1836 static tree cp_parser_assignment_expression
1837 (cp_parser *, bool, cp_id_kind *);
1838 static enum tree_code cp_parser_assignment_operator_opt
1839 (cp_parser *);
1840 static tree cp_parser_expression
1841 (cp_parser *, bool, cp_id_kind *);
1842 static tree cp_parser_constant_expression
1843 (cp_parser *, bool, bool *);
1844 static tree cp_parser_builtin_offsetof
1845 (cp_parser *);
1846 static tree cp_parser_lambda_expression
1847 (cp_parser *);
1848 static void cp_parser_lambda_introducer
1849 (cp_parser *, tree);
1850 static bool cp_parser_lambda_declarator_opt
1851 (cp_parser *, tree);
1852 static void cp_parser_lambda_body
1853 (cp_parser *, tree);
1855 /* Statements [gram.stmt.stmt] */
1857 static void cp_parser_statement
1858 (cp_parser *, tree, bool, bool *);
1859 static void cp_parser_label_for_labeled_statement
1860 (cp_parser *);
1861 static tree cp_parser_expression_statement
1862 (cp_parser *, tree);
1863 static tree cp_parser_compound_statement
1864 (cp_parser *, tree, bool, bool);
1865 static void cp_parser_statement_seq_opt
1866 (cp_parser *, tree);
1867 static tree cp_parser_selection_statement
1868 (cp_parser *, bool *);
1869 static tree cp_parser_condition
1870 (cp_parser *);
1871 static tree cp_parser_iteration_statement
1872 (cp_parser *);
1873 static bool cp_parser_for_init_statement
1874 (cp_parser *, tree *decl);
1875 static tree cp_parser_for
1876 (cp_parser *);
1877 static tree cp_parser_c_for
1878 (cp_parser *, tree, tree);
1879 static tree cp_parser_range_for
1880 (cp_parser *, tree, tree, tree);
1881 static void do_range_for_auto_deduction
1882 (tree, tree);
1883 static tree cp_parser_perform_range_for_lookup
1884 (tree, tree *, tree *);
1885 static tree cp_parser_range_for_member_function
1886 (tree, tree);
1887 static tree cp_parser_jump_statement
1888 (cp_parser *);
1889 static void cp_parser_declaration_statement
1890 (cp_parser *);
1892 static tree cp_parser_implicitly_scoped_statement
1893 (cp_parser *, bool *);
1894 static void cp_parser_already_scoped_statement
1895 (cp_parser *);
1897 /* Declarations [gram.dcl.dcl] */
1899 static void cp_parser_declaration_seq_opt
1900 (cp_parser *);
1901 static void cp_parser_declaration
1902 (cp_parser *);
1903 static void cp_parser_block_declaration
1904 (cp_parser *, bool);
1905 static void cp_parser_simple_declaration
1906 (cp_parser *, bool, tree *);
1907 static void cp_parser_decl_specifier_seq
1908 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1909 static tree cp_parser_storage_class_specifier_opt
1910 (cp_parser *);
1911 static tree cp_parser_function_specifier_opt
1912 (cp_parser *, cp_decl_specifier_seq *);
1913 static tree cp_parser_type_specifier
1914 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1915 int *, bool *);
1916 static tree cp_parser_simple_type_specifier
1917 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1918 static tree cp_parser_type_name
1919 (cp_parser *);
1920 static tree cp_parser_nonclass_name
1921 (cp_parser* parser);
1922 static tree cp_parser_elaborated_type_specifier
1923 (cp_parser *, bool, bool);
1924 static tree cp_parser_enum_specifier
1925 (cp_parser *);
1926 static void cp_parser_enumerator_list
1927 (cp_parser *, tree);
1928 static void cp_parser_enumerator_definition
1929 (cp_parser *, tree);
1930 static tree cp_parser_namespace_name
1931 (cp_parser *);
1932 static void cp_parser_namespace_definition
1933 (cp_parser *);
1934 static void cp_parser_namespace_body
1935 (cp_parser *);
1936 static tree cp_parser_qualified_namespace_specifier
1937 (cp_parser *);
1938 static void cp_parser_namespace_alias_definition
1939 (cp_parser *);
1940 static bool cp_parser_using_declaration
1941 (cp_parser *, bool);
1942 static void cp_parser_using_directive
1943 (cp_parser *);
1944 static tree cp_parser_alias_declaration
1945 (cp_parser *);
1946 static void cp_parser_asm_definition
1947 (cp_parser *);
1948 static void cp_parser_linkage_specification
1949 (cp_parser *);
1950 static void cp_parser_static_assert
1951 (cp_parser *, bool);
1952 static tree cp_parser_decltype
1953 (cp_parser *);
1955 /* Declarators [gram.dcl.decl] */
1957 static tree cp_parser_init_declarator
1958 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1959 static cp_declarator *cp_parser_declarator
1960 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1961 static cp_declarator *cp_parser_direct_declarator
1962 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1963 static enum tree_code cp_parser_ptr_operator
1964 (cp_parser *, tree *, cp_cv_quals *);
1965 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1966 (cp_parser *);
1967 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1968 (cp_parser *);
1969 static tree cp_parser_late_return_type_opt
1970 (cp_parser *, cp_cv_quals);
1971 static tree cp_parser_declarator_id
1972 (cp_parser *, bool);
1973 static tree cp_parser_type_id
1974 (cp_parser *);
1975 static tree cp_parser_template_type_arg
1976 (cp_parser *);
1977 static tree cp_parser_trailing_type_id (cp_parser *);
1978 static tree cp_parser_type_id_1
1979 (cp_parser *, bool, bool);
1980 static void cp_parser_type_specifier_seq
1981 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1982 static tree cp_parser_parameter_declaration_clause
1983 (cp_parser *);
1984 static tree cp_parser_parameter_declaration_list
1985 (cp_parser *, bool *);
1986 static cp_parameter_declarator *cp_parser_parameter_declaration
1987 (cp_parser *, bool, bool *);
1988 static tree cp_parser_default_argument
1989 (cp_parser *, bool);
1990 static void cp_parser_function_body
1991 (cp_parser *, bool);
1992 static tree cp_parser_initializer
1993 (cp_parser *, bool *, bool *);
1994 static tree cp_parser_initializer_clause
1995 (cp_parser *, bool *);
1996 static tree cp_parser_braced_list
1997 (cp_parser*, bool*);
1998 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1999 (cp_parser *, bool *);
2001 static bool cp_parser_ctor_initializer_opt_and_function_body
2002 (cp_parser *, bool);
2004 /* Classes [gram.class] */
2006 static tree cp_parser_class_name
2007 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2008 static tree cp_parser_class_specifier
2009 (cp_parser *);
2010 static tree cp_parser_class_head
2011 (cp_parser *, bool *, tree *);
2012 static enum tag_types cp_parser_class_key
2013 (cp_parser *);
2014 static void cp_parser_member_specification_opt
2015 (cp_parser *);
2016 static void cp_parser_member_declaration
2017 (cp_parser *);
2018 static tree cp_parser_pure_specifier
2019 (cp_parser *);
2020 static tree cp_parser_constant_initializer
2021 (cp_parser *);
2023 /* Derived classes [gram.class.derived] */
2025 static tree cp_parser_base_clause
2026 (cp_parser *);
2027 static tree cp_parser_base_specifier
2028 (cp_parser *);
2030 /* Special member functions [gram.special] */
2032 static tree cp_parser_conversion_function_id
2033 (cp_parser *);
2034 static tree cp_parser_conversion_type_id
2035 (cp_parser *);
2036 static cp_declarator *cp_parser_conversion_declarator_opt
2037 (cp_parser *);
2038 static bool cp_parser_ctor_initializer_opt
2039 (cp_parser *);
2040 static void cp_parser_mem_initializer_list
2041 (cp_parser *);
2042 static tree cp_parser_mem_initializer
2043 (cp_parser *);
2044 static tree cp_parser_mem_initializer_id
2045 (cp_parser *);
2047 /* Overloading [gram.over] */
2049 static tree cp_parser_operator_function_id
2050 (cp_parser *);
2051 static tree cp_parser_operator
2052 (cp_parser *);
2054 /* Templates [gram.temp] */
2056 static void cp_parser_template_declaration
2057 (cp_parser *, bool);
2058 static tree cp_parser_template_parameter_list
2059 (cp_parser *);
2060 static tree cp_parser_template_parameter
2061 (cp_parser *, bool *, bool *);
2062 static tree cp_parser_type_parameter
2063 (cp_parser *, bool *);
2064 static tree cp_parser_template_id
2065 (cp_parser *, bool, bool, bool);
2066 static tree cp_parser_template_name
2067 (cp_parser *, bool, bool, bool, bool *);
2068 static tree cp_parser_template_argument_list
2069 (cp_parser *);
2070 static tree cp_parser_template_argument
2071 (cp_parser *);
2072 static void cp_parser_explicit_instantiation
2073 (cp_parser *);
2074 static void cp_parser_explicit_specialization
2075 (cp_parser *);
2077 /* Exception handling [gram.exception] */
2079 static tree cp_parser_try_block
2080 (cp_parser *);
2081 static bool cp_parser_function_try_block
2082 (cp_parser *);
2083 static void cp_parser_handler_seq
2084 (cp_parser *);
2085 static void cp_parser_handler
2086 (cp_parser *);
2087 static tree cp_parser_exception_declaration
2088 (cp_parser *);
2089 static tree cp_parser_throw_expression
2090 (cp_parser *);
2091 static tree cp_parser_exception_specification_opt
2092 (cp_parser *);
2093 static tree cp_parser_type_id_list
2094 (cp_parser *);
2096 /* GNU Extensions */
2098 static tree cp_parser_asm_specification_opt
2099 (cp_parser *);
2100 static tree cp_parser_asm_operand_list
2101 (cp_parser *);
2102 static tree cp_parser_asm_clobber_list
2103 (cp_parser *);
2104 static tree cp_parser_asm_label_list
2105 (cp_parser *);
2106 static tree cp_parser_attributes_opt
2107 (cp_parser *);
2108 static tree cp_parser_attribute_list
2109 (cp_parser *);
2110 static bool cp_parser_extension_opt
2111 (cp_parser *, int *);
2112 static void cp_parser_label_declaration
2113 (cp_parser *);
2115 /* Transactional Memory Extensions */
2117 static tree cp_parser_transaction
2118 (cp_parser *, enum rid);
2119 static tree cp_parser_transaction_expression
2120 (cp_parser *, enum rid);
2121 static bool cp_parser_function_transaction
2122 (cp_parser *, enum rid);
2123 static tree cp_parser_transaction_cancel
2124 (cp_parser *);
2126 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2127 static bool cp_parser_pragma
2128 (cp_parser *, enum pragma_context);
2130 /* Objective-C++ Productions */
2132 static tree cp_parser_objc_message_receiver
2133 (cp_parser *);
2134 static tree cp_parser_objc_message_args
2135 (cp_parser *);
2136 static tree cp_parser_objc_message_expression
2137 (cp_parser *);
2138 static tree cp_parser_objc_encode_expression
2139 (cp_parser *);
2140 static tree cp_parser_objc_defs_expression
2141 (cp_parser *);
2142 static tree cp_parser_objc_protocol_expression
2143 (cp_parser *);
2144 static tree cp_parser_objc_selector_expression
2145 (cp_parser *);
2146 static tree cp_parser_objc_expression
2147 (cp_parser *);
2148 static bool cp_parser_objc_selector_p
2149 (enum cpp_ttype);
2150 static tree cp_parser_objc_selector
2151 (cp_parser *);
2152 static tree cp_parser_objc_protocol_refs_opt
2153 (cp_parser *);
2154 static void cp_parser_objc_declaration
2155 (cp_parser *, tree);
2156 static tree cp_parser_objc_statement
2157 (cp_parser *);
2158 static bool cp_parser_objc_valid_prefix_attributes
2159 (cp_parser *, tree *);
2160 static void cp_parser_objc_at_property_declaration
2161 (cp_parser *) ;
2162 static void cp_parser_objc_at_synthesize_declaration
2163 (cp_parser *) ;
2164 static void cp_parser_objc_at_dynamic_declaration
2165 (cp_parser *) ;
2166 static tree cp_parser_objc_struct_declaration
2167 (cp_parser *) ;
2169 /* Utility Routines */
2171 static tree cp_parser_lookup_name
2172 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2173 static tree cp_parser_lookup_name_simple
2174 (cp_parser *, tree, location_t);
2175 static tree cp_parser_maybe_treat_template_as_class
2176 (tree, bool);
2177 static bool cp_parser_check_declarator_template_parameters
2178 (cp_parser *, cp_declarator *, location_t);
2179 static bool cp_parser_check_template_parameters
2180 (cp_parser *, unsigned, location_t, cp_declarator *);
2181 static tree cp_parser_simple_cast_expression
2182 (cp_parser *);
2183 static tree cp_parser_global_scope_opt
2184 (cp_parser *, bool);
2185 static bool cp_parser_constructor_declarator_p
2186 (cp_parser *, bool);
2187 static tree cp_parser_function_definition_from_specifiers_and_declarator
2188 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2189 static tree cp_parser_function_definition_after_declarator
2190 (cp_parser *, bool);
2191 static void cp_parser_template_declaration_after_export
2192 (cp_parser *, bool);
2193 static void cp_parser_perform_template_parameter_access_checks
2194 (VEC (deferred_access_check,gc)*);
2195 static tree cp_parser_single_declaration
2196 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2197 static tree cp_parser_functional_cast
2198 (cp_parser *, tree);
2199 static tree cp_parser_save_member_function_body
2200 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2201 static tree cp_parser_save_nsdmi
2202 (cp_parser *);
2203 static tree cp_parser_enclosed_template_argument_list
2204 (cp_parser *);
2205 static void cp_parser_save_default_args
2206 (cp_parser *, tree);
2207 static void cp_parser_late_parsing_for_member
2208 (cp_parser *, tree);
2209 static tree cp_parser_late_parse_one_default_arg
2210 (cp_parser *, tree, tree, tree);
2211 static void cp_parser_late_parsing_nsdmi
2212 (cp_parser *, tree);
2213 static void cp_parser_late_parsing_default_args
2214 (cp_parser *, tree);
2215 static tree cp_parser_sizeof_operand
2216 (cp_parser *, enum rid);
2217 static tree cp_parser_trait_expr
2218 (cp_parser *, enum rid);
2219 static bool cp_parser_declares_only_class_p
2220 (cp_parser *);
2221 static void cp_parser_set_storage_class
2222 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2223 static void cp_parser_set_decl_spec_type
2224 (cp_decl_specifier_seq *, tree, location_t, bool);
2225 static void set_and_check_decl_spec_loc
2226 (cp_decl_specifier_seq *decl_specs,
2227 cp_decl_spec ds, source_location location);
2228 static bool cp_parser_friend_p
2229 (const cp_decl_specifier_seq *);
2230 static void cp_parser_required_error
2231 (cp_parser *, required_token, bool);
2232 static cp_token *cp_parser_require
2233 (cp_parser *, enum cpp_ttype, required_token);
2234 static cp_token *cp_parser_require_keyword
2235 (cp_parser *, enum rid, required_token);
2236 static bool cp_parser_token_starts_function_definition_p
2237 (cp_token *);
2238 static bool cp_parser_next_token_starts_class_definition_p
2239 (cp_parser *);
2240 static bool cp_parser_next_token_ends_template_argument_p
2241 (cp_parser *);
2242 static bool cp_parser_nth_token_starts_template_argument_list_p
2243 (cp_parser *, size_t);
2244 static enum tag_types cp_parser_token_is_class_key
2245 (cp_token *);
2246 static void cp_parser_check_class_key
2247 (enum tag_types, tree type);
2248 static void cp_parser_check_access_in_redeclaration
2249 (tree type, location_t location);
2250 static bool cp_parser_optional_template_keyword
2251 (cp_parser *);
2252 static void cp_parser_pre_parsed_nested_name_specifier
2253 (cp_parser *);
2254 static bool cp_parser_cache_group
2255 (cp_parser *, enum cpp_ttype, unsigned);
2256 static tree cp_parser_cache_defarg
2257 (cp_parser *parser, bool nsdmi);
2258 static void cp_parser_parse_tentatively
2259 (cp_parser *);
2260 static void cp_parser_commit_to_tentative_parse
2261 (cp_parser *);
2262 static void cp_parser_abort_tentative_parse
2263 (cp_parser *);
2264 static bool cp_parser_parse_definitely
2265 (cp_parser *);
2266 static inline bool cp_parser_parsing_tentatively
2267 (cp_parser *);
2268 static bool cp_parser_uncommitted_to_tentative_parse_p
2269 (cp_parser *);
2270 static void cp_parser_error
2271 (cp_parser *, const char *);
2272 static void cp_parser_name_lookup_error
2273 (cp_parser *, tree, tree, name_lookup_error, location_t);
2274 static bool cp_parser_simulate_error
2275 (cp_parser *);
2276 static bool cp_parser_check_type_definition
2277 (cp_parser *);
2278 static void cp_parser_check_for_definition_in_return_type
2279 (cp_declarator *, tree, location_t type_location);
2280 static void cp_parser_check_for_invalid_template_id
2281 (cp_parser *, tree, location_t location);
2282 static bool cp_parser_non_integral_constant_expression
2283 (cp_parser *, non_integral_constant);
2284 static void cp_parser_diagnose_invalid_type_name
2285 (cp_parser *, tree, tree, location_t);
2286 static bool cp_parser_parse_and_diagnose_invalid_type_name
2287 (cp_parser *);
2288 static int cp_parser_skip_to_closing_parenthesis
2289 (cp_parser *, bool, bool, bool);
2290 static void cp_parser_skip_to_end_of_statement
2291 (cp_parser *);
2292 static void cp_parser_consume_semicolon_at_end_of_statement
2293 (cp_parser *);
2294 static void cp_parser_skip_to_end_of_block_or_statement
2295 (cp_parser *);
2296 static bool cp_parser_skip_to_closing_brace
2297 (cp_parser *);
2298 static void cp_parser_skip_to_end_of_template_parameter_list
2299 (cp_parser *);
2300 static void cp_parser_skip_to_pragma_eol
2301 (cp_parser*, cp_token *);
2302 static bool cp_parser_error_occurred
2303 (cp_parser *);
2304 static bool cp_parser_allow_gnu_extensions_p
2305 (cp_parser *);
2306 static bool cp_parser_is_pure_string_literal
2307 (cp_token *);
2308 static bool cp_parser_is_string_literal
2309 (cp_token *);
2310 static bool cp_parser_is_keyword
2311 (cp_token *, enum rid);
2312 static tree cp_parser_make_typename_type
2313 (cp_parser *, tree, tree, location_t location);
2314 static cp_declarator * cp_parser_make_indirect_declarator
2315 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2317 /* Returns nonzero if we are parsing tentatively. */
2319 static inline bool
2320 cp_parser_parsing_tentatively (cp_parser* parser)
2322 return parser->context->next != NULL;
2325 /* Returns nonzero if TOKEN is a string literal. */
2327 static bool
2328 cp_parser_is_pure_string_literal (cp_token* token)
2330 return (token->type == CPP_STRING ||
2331 token->type == CPP_STRING16 ||
2332 token->type == CPP_STRING32 ||
2333 token->type == CPP_WSTRING ||
2334 token->type == CPP_UTF8STRING);
2337 /* Returns nonzero if TOKEN is a string literal
2338 of a user-defined string literal. */
2340 static bool
2341 cp_parser_is_string_literal (cp_token* token)
2343 return (cp_parser_is_pure_string_literal (token) ||
2344 token->type == CPP_STRING_USERDEF ||
2345 token->type == CPP_STRING16_USERDEF ||
2346 token->type == CPP_STRING32_USERDEF ||
2347 token->type == CPP_WSTRING_USERDEF ||
2348 token->type == CPP_UTF8STRING_USERDEF);
2351 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2353 static bool
2354 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2356 return token->keyword == keyword;
2359 /* If not parsing tentatively, issue a diagnostic of the form
2360 FILE:LINE: MESSAGE before TOKEN
2361 where TOKEN is the next token in the input stream. MESSAGE
2362 (specified by the caller) is usually of the form "expected
2363 OTHER-TOKEN". */
2365 static void
2366 cp_parser_error (cp_parser* parser, const char* gmsgid)
2368 if (!cp_parser_simulate_error (parser))
2370 cp_token *token = cp_lexer_peek_token (parser->lexer);
2371 /* This diagnostic makes more sense if it is tagged to the line
2372 of the token we just peeked at. */
2373 cp_lexer_set_source_position_from_token (token);
2375 if (token->type == CPP_PRAGMA)
2377 error_at (token->location,
2378 "%<#pragma%> is not allowed here");
2379 cp_parser_skip_to_pragma_eol (parser, token);
2380 return;
2383 c_parse_error (gmsgid,
2384 /* Because c_parser_error does not understand
2385 CPP_KEYWORD, keywords are treated like
2386 identifiers. */
2387 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2388 token->u.value, token->flags);
2392 /* Issue an error about name-lookup failing. NAME is the
2393 IDENTIFIER_NODE DECL is the result of
2394 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2395 the thing that we hoped to find. */
2397 static void
2398 cp_parser_name_lookup_error (cp_parser* parser,
2399 tree name,
2400 tree decl,
2401 name_lookup_error desired,
2402 location_t location)
2404 /* If name lookup completely failed, tell the user that NAME was not
2405 declared. */
2406 if (decl == error_mark_node)
2408 if (parser->scope && parser->scope != global_namespace)
2409 error_at (location, "%<%E::%E%> has not been declared",
2410 parser->scope, name);
2411 else if (parser->scope == global_namespace)
2412 error_at (location, "%<::%E%> has not been declared", name);
2413 else if (parser->object_scope
2414 && !CLASS_TYPE_P (parser->object_scope))
2415 error_at (location, "request for member %qE in non-class type %qT",
2416 name, parser->object_scope);
2417 else if (parser->object_scope)
2418 error_at (location, "%<%T::%E%> has not been declared",
2419 parser->object_scope, name);
2420 else
2421 error_at (location, "%qE has not been declared", name);
2423 else if (parser->scope && parser->scope != global_namespace)
2425 switch (desired)
2427 case NLE_TYPE:
2428 error_at (location, "%<%E::%E%> is not a type",
2429 parser->scope, name);
2430 break;
2431 case NLE_CXX98:
2432 error_at (location, "%<%E::%E%> is not a class or namespace",
2433 parser->scope, name);
2434 break;
2435 case NLE_NOT_CXX98:
2436 error_at (location,
2437 "%<%E::%E%> is not a class, namespace, or enumeration",
2438 parser->scope, name);
2439 break;
2440 default:
2441 gcc_unreachable ();
2445 else if (parser->scope == global_namespace)
2447 switch (desired)
2449 case NLE_TYPE:
2450 error_at (location, "%<::%E%> is not a type", name);
2451 break;
2452 case NLE_CXX98:
2453 error_at (location, "%<::%E%> is not a class or namespace", name);
2454 break;
2455 case NLE_NOT_CXX98:
2456 error_at (location,
2457 "%<::%E%> is not a class, namespace, or enumeration",
2458 name);
2459 break;
2460 default:
2461 gcc_unreachable ();
2464 else
2466 switch (desired)
2468 case NLE_TYPE:
2469 error_at (location, "%qE is not a type", name);
2470 break;
2471 case NLE_CXX98:
2472 error_at (location, "%qE is not a class or namespace", name);
2473 break;
2474 case NLE_NOT_CXX98:
2475 error_at (location,
2476 "%qE is not a class, namespace, or enumeration", name);
2477 break;
2478 default:
2479 gcc_unreachable ();
2484 /* If we are parsing tentatively, remember that an error has occurred
2485 during this tentative parse. Returns true if the error was
2486 simulated; false if a message should be issued by the caller. */
2488 static bool
2489 cp_parser_simulate_error (cp_parser* parser)
2491 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2493 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2494 return true;
2496 return false;
2499 /* This function is called when a type is defined. If type
2500 definitions are forbidden at this point, an error message is
2501 issued. */
2503 static bool
2504 cp_parser_check_type_definition (cp_parser* parser)
2506 /* If types are forbidden here, issue a message. */
2507 if (parser->type_definition_forbidden_message)
2509 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2510 in the message need to be interpreted. */
2511 error (parser->type_definition_forbidden_message);
2512 return false;
2514 return true;
2517 /* This function is called when the DECLARATOR is processed. The TYPE
2518 was a type defined in the decl-specifiers. If it is invalid to
2519 define a type in the decl-specifiers for DECLARATOR, an error is
2520 issued. TYPE_LOCATION is the location of TYPE and is used
2521 for error reporting. */
2523 static void
2524 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2525 tree type, location_t type_location)
2527 /* [dcl.fct] forbids type definitions in return types.
2528 Unfortunately, it's not easy to know whether or not we are
2529 processing a return type until after the fact. */
2530 while (declarator
2531 && (declarator->kind == cdk_pointer
2532 || declarator->kind == cdk_reference
2533 || declarator->kind == cdk_ptrmem))
2534 declarator = declarator->declarator;
2535 if (declarator
2536 && declarator->kind == cdk_function)
2538 error_at (type_location,
2539 "new types may not be defined in a return type");
2540 inform (type_location,
2541 "(perhaps a semicolon is missing after the definition of %qT)",
2542 type);
2546 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2547 "<" in any valid C++ program. If the next token is indeed "<",
2548 issue a message warning the user about what appears to be an
2549 invalid attempt to form a template-id. LOCATION is the location
2550 of the type-specifier (TYPE) */
2552 static void
2553 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2554 tree type, location_t location)
2556 cp_token_position start = 0;
2558 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2560 if (TYPE_P (type))
2561 error_at (location, "%qT is not a template", type);
2562 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2563 error_at (location, "%qE is not a template", type);
2564 else
2565 error_at (location, "invalid template-id");
2566 /* Remember the location of the invalid "<". */
2567 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2568 start = cp_lexer_token_position (parser->lexer, true);
2569 /* Consume the "<". */
2570 cp_lexer_consume_token (parser->lexer);
2571 /* Parse the template arguments. */
2572 cp_parser_enclosed_template_argument_list (parser);
2573 /* Permanently remove the invalid template arguments so that
2574 this error message is not issued again. */
2575 if (start)
2576 cp_lexer_purge_tokens_after (parser->lexer, start);
2580 /* If parsing an integral constant-expression, issue an error message
2581 about the fact that THING appeared and return true. Otherwise,
2582 return false. In either case, set
2583 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2585 static bool
2586 cp_parser_non_integral_constant_expression (cp_parser *parser,
2587 non_integral_constant thing)
2589 parser->non_integral_constant_expression_p = true;
2590 if (parser->integral_constant_expression_p)
2592 if (!parser->allow_non_integral_constant_expression_p)
2594 const char *msg = NULL;
2595 switch (thing)
2597 case NIC_FLOAT:
2598 error ("floating-point literal "
2599 "cannot appear in a constant-expression");
2600 return true;
2601 case NIC_CAST:
2602 error ("a cast to a type other than an integral or "
2603 "enumeration type cannot appear in a "
2604 "constant-expression");
2605 return true;
2606 case NIC_TYPEID:
2607 error ("%<typeid%> operator "
2608 "cannot appear in a constant-expression");
2609 return true;
2610 case NIC_NCC:
2611 error ("non-constant compound literals "
2612 "cannot appear in a constant-expression");
2613 return true;
2614 case NIC_FUNC_CALL:
2615 error ("a function call "
2616 "cannot appear in a constant-expression");
2617 return true;
2618 case NIC_INC:
2619 error ("an increment "
2620 "cannot appear in a constant-expression");
2621 return true;
2622 case NIC_DEC:
2623 error ("an decrement "
2624 "cannot appear in a constant-expression");
2625 return true;
2626 case NIC_ARRAY_REF:
2627 error ("an array reference "
2628 "cannot appear in a constant-expression");
2629 return true;
2630 case NIC_ADDR_LABEL:
2631 error ("the address of a label "
2632 "cannot appear in a constant-expression");
2633 return true;
2634 case NIC_OVERLOADED:
2635 error ("calls to overloaded operators "
2636 "cannot appear in a constant-expression");
2637 return true;
2638 case NIC_ASSIGNMENT:
2639 error ("an assignment cannot appear in a constant-expression");
2640 return true;
2641 case NIC_COMMA:
2642 error ("a comma operator "
2643 "cannot appear in a constant-expression");
2644 return true;
2645 case NIC_CONSTRUCTOR:
2646 error ("a call to a constructor "
2647 "cannot appear in a constant-expression");
2648 return true;
2649 case NIC_TRANSACTION:
2650 error ("a transaction expression "
2651 "cannot appear in a constant-expression");
2652 return true;
2653 case NIC_THIS:
2654 msg = "this";
2655 break;
2656 case NIC_FUNC_NAME:
2657 msg = "__FUNCTION__";
2658 break;
2659 case NIC_PRETTY_FUNC:
2660 msg = "__PRETTY_FUNCTION__";
2661 break;
2662 case NIC_C99_FUNC:
2663 msg = "__func__";
2664 break;
2665 case NIC_VA_ARG:
2666 msg = "va_arg";
2667 break;
2668 case NIC_ARROW:
2669 msg = "->";
2670 break;
2671 case NIC_POINT:
2672 msg = ".";
2673 break;
2674 case NIC_STAR:
2675 msg = "*";
2676 break;
2677 case NIC_ADDR:
2678 msg = "&";
2679 break;
2680 case NIC_PREINCREMENT:
2681 msg = "++";
2682 break;
2683 case NIC_PREDECREMENT:
2684 msg = "--";
2685 break;
2686 case NIC_NEW:
2687 msg = "new";
2688 break;
2689 case NIC_DEL:
2690 msg = "delete";
2691 break;
2692 default:
2693 gcc_unreachable ();
2695 if (msg)
2696 error ("%qs cannot appear in a constant-expression", msg);
2697 return true;
2700 return false;
2703 /* Emit a diagnostic for an invalid type name. SCOPE is the
2704 qualifying scope (or NULL, if none) for ID. This function commits
2705 to the current active tentative parse, if any. (Otherwise, the
2706 problematic construct might be encountered again later, resulting
2707 in duplicate error messages.) LOCATION is the location of ID. */
2709 static void
2710 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2711 tree scope, tree id,
2712 location_t location)
2714 tree decl, old_scope;
2715 cp_parser_commit_to_tentative_parse (parser);
2716 /* Try to lookup the identifier. */
2717 old_scope = parser->scope;
2718 parser->scope = scope;
2719 decl = cp_parser_lookup_name_simple (parser, id, location);
2720 parser->scope = old_scope;
2721 /* If the lookup found a template-name, it means that the user forgot
2722 to specify an argument list. Emit a useful error message. */
2723 if (TREE_CODE (decl) == TEMPLATE_DECL)
2724 error_at (location,
2725 "invalid use of template-name %qE without an argument list",
2726 decl);
2727 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2728 error_at (location, "invalid use of destructor %qD as a type", id);
2729 else if (TREE_CODE (decl) == TYPE_DECL)
2730 /* Something like 'unsigned A a;' */
2731 error_at (location, "invalid combination of multiple type-specifiers");
2732 else if (!parser->scope)
2734 /* Issue an error message. */
2735 error_at (location, "%qE does not name a type", id);
2736 /* If we're in a template class, it's possible that the user was
2737 referring to a type from a base class. For example:
2739 template <typename T> struct A { typedef T X; };
2740 template <typename T> struct B : public A<T> { X x; };
2742 The user should have said "typename A<T>::X". */
2743 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2744 inform (location, "C++11 %<constexpr%> only available with "
2745 "-std=c++11 or -std=gnu++11");
2746 else if (processing_template_decl && current_class_type
2747 && TYPE_BINFO (current_class_type))
2749 tree b;
2751 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2753 b = TREE_CHAIN (b))
2755 tree base_type = BINFO_TYPE (b);
2756 if (CLASS_TYPE_P (base_type)
2757 && dependent_type_p (base_type))
2759 tree field;
2760 /* Go from a particular instantiation of the
2761 template (which will have an empty TYPE_FIELDs),
2762 to the main version. */
2763 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2764 for (field = TYPE_FIELDS (base_type);
2765 field;
2766 field = DECL_CHAIN (field))
2767 if (TREE_CODE (field) == TYPE_DECL
2768 && DECL_NAME (field) == id)
2770 inform (location,
2771 "(perhaps %<typename %T::%E%> was intended)",
2772 BINFO_TYPE (b), id);
2773 break;
2775 if (field)
2776 break;
2781 /* Here we diagnose qualified-ids where the scope is actually correct,
2782 but the identifier does not resolve to a valid type name. */
2783 else if (parser->scope != error_mark_node)
2785 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2786 error_at (location, "%qE in namespace %qE does not name a type",
2787 id, parser->scope);
2788 else if (CLASS_TYPE_P (parser->scope)
2789 && constructor_name_p (id, parser->scope))
2791 /* A<T>::A<T>() */
2792 error_at (location, "%<%T::%E%> names the constructor, not"
2793 " the type", parser->scope, id);
2794 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2795 error_at (location, "and %qT has no template constructors",
2796 parser->scope);
2798 else if (TYPE_P (parser->scope)
2799 && dependent_scope_p (parser->scope))
2800 error_at (location, "need %<typename%> before %<%T::%E%> because "
2801 "%qT is a dependent scope",
2802 parser->scope, id, parser->scope);
2803 else if (TYPE_P (parser->scope))
2804 error_at (location, "%qE in %q#T does not name a type",
2805 id, parser->scope);
2806 else
2807 gcc_unreachable ();
2811 /* Check for a common situation where a type-name should be present,
2812 but is not, and issue a sensible error message. Returns true if an
2813 invalid type-name was detected.
2815 The situation handled by this function are variable declarations of the
2816 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2817 Usually, `ID' should name a type, but if we got here it means that it
2818 does not. We try to emit the best possible error message depending on
2819 how exactly the id-expression looks like. */
2821 static bool
2822 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2824 tree id;
2825 cp_token *token = cp_lexer_peek_token (parser->lexer);
2827 /* Avoid duplicate error about ambiguous lookup. */
2828 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2830 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2831 if (next->type == CPP_NAME && next->ambiguous_p)
2832 goto out;
2835 cp_parser_parse_tentatively (parser);
2836 id = cp_parser_id_expression (parser,
2837 /*template_keyword_p=*/false,
2838 /*check_dependency_p=*/true,
2839 /*template_p=*/NULL,
2840 /*declarator_p=*/true,
2841 /*optional_p=*/false);
2842 /* If the next token is a (, this is a function with no explicit return
2843 type, i.e. constructor, destructor or conversion op. */
2844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2845 || TREE_CODE (id) == TYPE_DECL)
2847 cp_parser_abort_tentative_parse (parser);
2848 return false;
2850 if (!cp_parser_parse_definitely (parser))
2851 return false;
2853 /* Emit a diagnostic for the invalid type. */
2854 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2855 id, token->location);
2856 out:
2857 /* If we aren't in the middle of a declarator (i.e. in a
2858 parameter-declaration-clause), skip to the end of the declaration;
2859 there's no point in trying to process it. */
2860 if (!parser->in_declarator_p)
2861 cp_parser_skip_to_end_of_block_or_statement (parser);
2862 return true;
2865 /* Consume tokens up to, and including, the next non-nested closing `)'.
2866 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2867 are doing error recovery. Returns -1 if OR_COMMA is true and we
2868 found an unnested comma. */
2870 static int
2871 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2872 bool recovering,
2873 bool or_comma,
2874 bool consume_paren)
2876 unsigned paren_depth = 0;
2877 unsigned brace_depth = 0;
2878 unsigned square_depth = 0;
2880 if (recovering && !or_comma
2881 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2882 return 0;
2884 while (true)
2886 cp_token * token = cp_lexer_peek_token (parser->lexer);
2888 switch (token->type)
2890 case CPP_EOF:
2891 case CPP_PRAGMA_EOL:
2892 /* If we've run out of tokens, then there is no closing `)'. */
2893 return 0;
2895 /* This is good for lambda expression capture-lists. */
2896 case CPP_OPEN_SQUARE:
2897 ++square_depth;
2898 break;
2899 case CPP_CLOSE_SQUARE:
2900 if (!square_depth--)
2901 return 0;
2902 break;
2904 case CPP_SEMICOLON:
2905 /* This matches the processing in skip_to_end_of_statement. */
2906 if (!brace_depth)
2907 return 0;
2908 break;
2910 case CPP_OPEN_BRACE:
2911 ++brace_depth;
2912 break;
2913 case CPP_CLOSE_BRACE:
2914 if (!brace_depth--)
2915 return 0;
2916 break;
2918 case CPP_COMMA:
2919 if (recovering && or_comma && !brace_depth && !paren_depth
2920 && !square_depth)
2921 return -1;
2922 break;
2924 case CPP_OPEN_PAREN:
2925 if (!brace_depth)
2926 ++paren_depth;
2927 break;
2929 case CPP_CLOSE_PAREN:
2930 if (!brace_depth && !paren_depth--)
2932 if (consume_paren)
2933 cp_lexer_consume_token (parser->lexer);
2934 return 1;
2936 break;
2938 default:
2939 break;
2942 /* Consume the token. */
2943 cp_lexer_consume_token (parser->lexer);
2947 /* Consume tokens until we reach the end of the current statement.
2948 Normally, that will be just before consuming a `;'. However, if a
2949 non-nested `}' comes first, then we stop before consuming that. */
2951 static void
2952 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2954 unsigned nesting_depth = 0;
2956 while (true)
2958 cp_token *token = cp_lexer_peek_token (parser->lexer);
2960 switch (token->type)
2962 case CPP_EOF:
2963 case CPP_PRAGMA_EOL:
2964 /* If we've run out of tokens, stop. */
2965 return;
2967 case CPP_SEMICOLON:
2968 /* If the next token is a `;', we have reached the end of the
2969 statement. */
2970 if (!nesting_depth)
2971 return;
2972 break;
2974 case CPP_CLOSE_BRACE:
2975 /* If this is a non-nested '}', stop before consuming it.
2976 That way, when confronted with something like:
2978 { 3 + }
2980 we stop before consuming the closing '}', even though we
2981 have not yet reached a `;'. */
2982 if (nesting_depth == 0)
2983 return;
2985 /* If it is the closing '}' for a block that we have
2986 scanned, stop -- but only after consuming the token.
2987 That way given:
2989 void f g () { ... }
2990 typedef int I;
2992 we will stop after the body of the erroneously declared
2993 function, but before consuming the following `typedef'
2994 declaration. */
2995 if (--nesting_depth == 0)
2997 cp_lexer_consume_token (parser->lexer);
2998 return;
3001 case CPP_OPEN_BRACE:
3002 ++nesting_depth;
3003 break;
3005 default:
3006 break;
3009 /* Consume the token. */
3010 cp_lexer_consume_token (parser->lexer);
3014 /* This function is called at the end of a statement or declaration.
3015 If the next token is a semicolon, it is consumed; otherwise, error
3016 recovery is attempted. */
3018 static void
3019 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3021 /* Look for the trailing `;'. */
3022 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3024 /* If there is additional (erroneous) input, skip to the end of
3025 the statement. */
3026 cp_parser_skip_to_end_of_statement (parser);
3027 /* If the next token is now a `;', consume it. */
3028 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3029 cp_lexer_consume_token (parser->lexer);
3033 /* Skip tokens until we have consumed an entire block, or until we
3034 have consumed a non-nested `;'. */
3036 static void
3037 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3039 int nesting_depth = 0;
3041 while (nesting_depth >= 0)
3043 cp_token *token = cp_lexer_peek_token (parser->lexer);
3045 switch (token->type)
3047 case CPP_EOF:
3048 case CPP_PRAGMA_EOL:
3049 /* If we've run out of tokens, stop. */
3050 return;
3052 case CPP_SEMICOLON:
3053 /* Stop if this is an unnested ';'. */
3054 if (!nesting_depth)
3055 nesting_depth = -1;
3056 break;
3058 case CPP_CLOSE_BRACE:
3059 /* Stop if this is an unnested '}', or closes the outermost
3060 nesting level. */
3061 nesting_depth--;
3062 if (nesting_depth < 0)
3063 return;
3064 if (!nesting_depth)
3065 nesting_depth = -1;
3066 break;
3068 case CPP_OPEN_BRACE:
3069 /* Nest. */
3070 nesting_depth++;
3071 break;
3073 default:
3074 break;
3077 /* Consume the token. */
3078 cp_lexer_consume_token (parser->lexer);
3082 /* Skip tokens until a non-nested closing curly brace is the next
3083 token, or there are no more tokens. Return true in the first case,
3084 false otherwise. */
3086 static bool
3087 cp_parser_skip_to_closing_brace (cp_parser *parser)
3089 unsigned nesting_depth = 0;
3091 while (true)
3093 cp_token *token = cp_lexer_peek_token (parser->lexer);
3095 switch (token->type)
3097 case CPP_EOF:
3098 case CPP_PRAGMA_EOL:
3099 /* If we've run out of tokens, stop. */
3100 return false;
3102 case CPP_CLOSE_BRACE:
3103 /* If the next token is a non-nested `}', then we have reached
3104 the end of the current block. */
3105 if (nesting_depth-- == 0)
3106 return true;
3107 break;
3109 case CPP_OPEN_BRACE:
3110 /* If it the next token is a `{', then we are entering a new
3111 block. Consume the entire block. */
3112 ++nesting_depth;
3113 break;
3115 default:
3116 break;
3119 /* Consume the token. */
3120 cp_lexer_consume_token (parser->lexer);
3124 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3125 parameter is the PRAGMA token, allowing us to purge the entire pragma
3126 sequence. */
3128 static void
3129 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3131 cp_token *token;
3133 parser->lexer->in_pragma = false;
3136 token = cp_lexer_consume_token (parser->lexer);
3137 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3139 /* Ensure that the pragma is not parsed again. */
3140 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3143 /* Require pragma end of line, resyncing with it as necessary. The
3144 arguments are as for cp_parser_skip_to_pragma_eol. */
3146 static void
3147 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3149 parser->lexer->in_pragma = false;
3150 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3151 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3154 /* This is a simple wrapper around make_typename_type. When the id is
3155 an unresolved identifier node, we can provide a superior diagnostic
3156 using cp_parser_diagnose_invalid_type_name. */
3158 static tree
3159 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3160 tree id, location_t id_location)
3162 tree result;
3163 if (TREE_CODE (id) == IDENTIFIER_NODE)
3165 result = make_typename_type (scope, id, typename_type,
3166 /*complain=*/tf_none);
3167 if (result == error_mark_node)
3168 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3169 return result;
3171 return make_typename_type (scope, id, typename_type, tf_error);
3174 /* This is a wrapper around the
3175 make_{pointer,ptrmem,reference}_declarator functions that decides
3176 which one to call based on the CODE and CLASS_TYPE arguments. The
3177 CODE argument should be one of the values returned by
3178 cp_parser_ptr_operator. */
3179 static cp_declarator *
3180 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3181 cp_cv_quals cv_qualifiers,
3182 cp_declarator *target)
3184 if (code == ERROR_MARK)
3185 return cp_error_declarator;
3187 if (code == INDIRECT_REF)
3188 if (class_type == NULL_TREE)
3189 return make_pointer_declarator (cv_qualifiers, target);
3190 else
3191 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3192 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3193 return make_reference_declarator (cv_qualifiers, target, false);
3194 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3195 return make_reference_declarator (cv_qualifiers, target, true);
3196 gcc_unreachable ();
3199 /* Create a new C++ parser. */
3201 static cp_parser *
3202 cp_parser_new (void)
3204 cp_parser *parser;
3205 cp_lexer *lexer;
3206 unsigned i;
3208 /* cp_lexer_new_main is called before doing GC allocation because
3209 cp_lexer_new_main might load a PCH file. */
3210 lexer = cp_lexer_new_main ();
3212 /* Initialize the binops_by_token so that we can get the tree
3213 directly from the token. */
3214 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3215 binops_by_token[binops[i].token_type] = binops[i];
3217 parser = ggc_alloc_cleared_cp_parser ();
3218 parser->lexer = lexer;
3219 parser->context = cp_parser_context_new (NULL);
3221 /* For now, we always accept GNU extensions. */
3222 parser->allow_gnu_extensions_p = 1;
3224 /* The `>' token is a greater-than operator, not the end of a
3225 template-id. */
3226 parser->greater_than_is_operator_p = true;
3228 parser->default_arg_ok_p = true;
3230 /* We are not parsing a constant-expression. */
3231 parser->integral_constant_expression_p = false;
3232 parser->allow_non_integral_constant_expression_p = false;
3233 parser->non_integral_constant_expression_p = false;
3235 /* Local variable names are not forbidden. */
3236 parser->local_variables_forbidden_p = false;
3238 /* We are not processing an `extern "C"' declaration. */
3239 parser->in_unbraced_linkage_specification_p = false;
3241 /* We are not processing a declarator. */
3242 parser->in_declarator_p = false;
3244 /* We are not processing a template-argument-list. */
3245 parser->in_template_argument_list_p = false;
3247 /* We are not in an iteration statement. */
3248 parser->in_statement = 0;
3250 /* We are not in a switch statement. */
3251 parser->in_switch_statement_p = false;
3253 /* We are not parsing a type-id inside an expression. */
3254 parser->in_type_id_in_expr_p = false;
3256 /* Declarations aren't implicitly extern "C". */
3257 parser->implicit_extern_c = false;
3259 /* String literals should be translated to the execution character set. */
3260 parser->translate_strings_p = true;
3262 /* We are not parsing a function body. */
3263 parser->in_function_body = false;
3265 /* We can correct until told otherwise. */
3266 parser->colon_corrects_to_scope_p = true;
3268 /* The unparsed function queue is empty. */
3269 push_unparsed_function_queues (parser);
3271 /* There are no classes being defined. */
3272 parser->num_classes_being_defined = 0;
3274 /* No template parameters apply. */
3275 parser->num_template_parameter_lists = 0;
3277 return parser;
3280 /* Create a cp_lexer structure which will emit the tokens in CACHE
3281 and push it onto the parser's lexer stack. This is used for delayed
3282 parsing of in-class method bodies and default arguments, and should
3283 not be confused with tentative parsing. */
3284 static void
3285 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3287 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3288 lexer->next = parser->lexer;
3289 parser->lexer = lexer;
3291 /* Move the current source position to that of the first token in the
3292 new lexer. */
3293 cp_lexer_set_source_position_from_token (lexer->next_token);
3296 /* Pop the top lexer off the parser stack. This is never used for the
3297 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3298 static void
3299 cp_parser_pop_lexer (cp_parser *parser)
3301 cp_lexer *lexer = parser->lexer;
3302 parser->lexer = lexer->next;
3303 cp_lexer_destroy (lexer);
3305 /* Put the current source position back where it was before this
3306 lexer was pushed. */
3307 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3310 /* Lexical conventions [gram.lex] */
3312 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3313 identifier. */
3315 static tree
3316 cp_parser_identifier (cp_parser* parser)
3318 cp_token *token;
3320 /* Look for the identifier. */
3321 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3322 /* Return the value. */
3323 return token ? token->u.value : error_mark_node;
3326 /* Parse a sequence of adjacent string constants. Returns a
3327 TREE_STRING representing the combined, nul-terminated string
3328 constant. If TRANSLATE is true, translate the string to the
3329 execution character set. If WIDE_OK is true, a wide string is
3330 invalid here.
3332 C++98 [lex.string] says that if a narrow string literal token is
3333 adjacent to a wide string literal token, the behavior is undefined.
3334 However, C99 6.4.5p4 says that this results in a wide string literal.
3335 We follow C99 here, for consistency with the C front end.
3337 This code is largely lifted from lex_string() in c-lex.c.
3339 FUTURE: ObjC++ will need to handle @-strings here. */
3340 static tree
3341 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3343 tree value;
3344 size_t count;
3345 struct obstack str_ob;
3346 cpp_string str, istr, *strs;
3347 cp_token *tok;
3348 enum cpp_ttype type, curr_type;
3349 int have_suffix_p = 0;
3350 tree string_tree;
3351 tree suffix_id = NULL_TREE;
3352 bool curr_tok_is_userdef_p = false;
3354 tok = cp_lexer_peek_token (parser->lexer);
3355 if (!cp_parser_is_string_literal (tok))
3357 cp_parser_error (parser, "expected string-literal");
3358 return error_mark_node;
3361 if (cpp_userdef_string_p (tok->type))
3363 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3364 curr_type = cpp_userdef_string_remove_type (tok->type);
3365 curr_tok_is_userdef_p = true;
3367 else
3369 string_tree = tok->u.value;
3370 curr_type = tok->type;
3372 type = curr_type;
3374 /* Try to avoid the overhead of creating and destroying an obstack
3375 for the common case of just one string. */
3376 if (!cp_parser_is_string_literal
3377 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3379 cp_lexer_consume_token (parser->lexer);
3381 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3382 str.len = TREE_STRING_LENGTH (string_tree);
3383 count = 1;
3385 if (curr_tok_is_userdef_p)
3387 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3388 have_suffix_p = 1;
3389 curr_type = cpp_userdef_string_remove_type (tok->type);
3391 else
3392 curr_type = tok->type;
3394 strs = &str;
3396 else
3398 gcc_obstack_init (&str_ob);
3399 count = 0;
3403 cp_lexer_consume_token (parser->lexer);
3404 count++;
3405 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3406 str.len = TREE_STRING_LENGTH (string_tree);
3408 if (curr_tok_is_userdef_p)
3410 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3411 if (have_suffix_p == 0)
3413 suffix_id = curr_suffix_id;
3414 have_suffix_p = 1;
3416 else if (have_suffix_p == 1
3417 && curr_suffix_id != suffix_id)
3419 error ("inconsistent user-defined literal suffixes"
3420 " %qD and %qD in string literal",
3421 suffix_id, curr_suffix_id);
3422 have_suffix_p = -1;
3424 curr_type = cpp_userdef_string_remove_type (tok->type);
3426 else
3427 curr_type = tok->type;
3429 if (type != curr_type)
3431 if (type == CPP_STRING)
3432 type = curr_type;
3433 else if (curr_type != CPP_STRING)
3434 error_at (tok->location,
3435 "unsupported non-standard concatenation "
3436 "of string literals");
3439 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3441 tok = cp_lexer_peek_token (parser->lexer);
3442 if (cpp_userdef_string_p (tok->type))
3444 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3445 curr_type = cpp_userdef_string_remove_type (tok->type);
3446 curr_tok_is_userdef_p = true;
3448 else
3450 string_tree = tok->u.value;
3451 curr_type = tok->type;
3452 curr_tok_is_userdef_p = false;
3455 while (cp_parser_is_string_literal (tok));
3457 strs = (cpp_string *) obstack_finish (&str_ob);
3460 if (type != CPP_STRING && !wide_ok)
3462 cp_parser_error (parser, "a wide string is invalid in this context");
3463 type = CPP_STRING;
3466 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3467 (parse_in, strs, count, &istr, type))
3469 value = build_string (istr.len, (const char *)istr.text);
3470 free (CONST_CAST (unsigned char *, istr.text));
3472 switch (type)
3474 default:
3475 case CPP_STRING:
3476 case CPP_UTF8STRING:
3477 TREE_TYPE (value) = char_array_type_node;
3478 break;
3479 case CPP_STRING16:
3480 TREE_TYPE (value) = char16_array_type_node;
3481 break;
3482 case CPP_STRING32:
3483 TREE_TYPE (value) = char32_array_type_node;
3484 break;
3485 case CPP_WSTRING:
3486 TREE_TYPE (value) = wchar_array_type_node;
3487 break;
3490 value = fix_string_type (value);
3492 if (have_suffix_p)
3494 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3495 tok->u.value = literal;
3496 return cp_parser_userdef_string_literal (tok);
3499 else
3500 /* cpp_interpret_string has issued an error. */
3501 value = error_mark_node;
3503 if (count > 1)
3504 obstack_free (&str_ob, 0);
3506 return value;
3509 /* Look up a literal operator with the name and the exact arguments. */
3511 static tree
3512 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3514 tree decl, fns;
3515 decl = lookup_name (name);
3516 if (!decl || !is_overloaded_fn (decl))
3517 return error_mark_node;
3519 for (fns = decl; fns; fns = OVL_NEXT (fns))
3521 unsigned int ix;
3522 bool found = true;
3523 tree fn = OVL_CURRENT (fns);
3524 tree argtypes = NULL_TREE;
3525 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3526 if (argtypes != NULL_TREE)
3528 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3529 ++ix, argtypes = TREE_CHAIN (argtypes))
3531 tree targ = TREE_VALUE (argtypes);
3532 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3533 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3534 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3535 if ((ptr || arr || !same_type_p (targ, tparm))
3536 && (!ptr || !arr
3537 || !same_type_p (TREE_TYPE (targ),
3538 TREE_TYPE (tparm))))
3539 found = false;
3541 if (found
3542 && ix == VEC_length (tree, args)
3543 /* May be this should be sufficient_parms_p instead,
3544 depending on how exactly should user-defined literals
3545 work in presence of default arguments on the literal
3546 operator parameters. */
3547 && argtypes == void_list_node)
3548 return fn;
3552 return error_mark_node;
3555 /* Parse a user-defined char constant. Returns a call to a user-defined
3556 literal operator taking the character as an argument. */
3558 static tree
3559 cp_parser_userdef_char_literal (cp_parser *parser)
3561 cp_token *token = cp_lexer_consume_token (parser->lexer);
3562 tree literal = token->u.value;
3563 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3564 tree value = USERDEF_LITERAL_VALUE (literal);
3565 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3566 tree decl, result;
3568 /* Build up a call to the user-defined operator */
3569 /* Lookup the name we got back from the id-expression. */
3570 VEC(tree,gc) *args = make_tree_vector ();
3571 VEC_safe_push (tree, gc, args, value);
3572 decl = lookup_literal_operator (name, args);
3573 if (!decl || decl == error_mark_node)
3575 error ("unable to find character literal operator %qD with %qT argument",
3576 name, TREE_TYPE (value));
3577 release_tree_vector (args);
3578 return error_mark_node;
3580 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3581 release_tree_vector (args);
3582 if (result != error_mark_node)
3583 return result;
3585 error ("unable to find character literal operator %qD with %qT argument",
3586 name, TREE_TYPE (value));
3587 return error_mark_node;
3590 /* A subroutine of cp_parser_userdef_numeric_literal to
3591 create a char... template parameter pack from a string node. */
3593 static tree
3594 make_char_string_pack (tree value)
3596 tree charvec;
3597 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3598 const char *str = TREE_STRING_POINTER (value);
3599 int i, len = TREE_STRING_LENGTH (value) - 1;
3600 tree argvec = make_tree_vec (1);
3602 /* Fill in CHARVEC with all of the parameters. */
3603 charvec = make_tree_vec (len);
3604 for (i = 0; i < len; ++i)
3605 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3607 /* Build the argument packs. */
3608 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3609 TREE_TYPE (argpack) = char_type_node;
3611 TREE_VEC_ELT (argvec, 0) = argpack;
3613 return argvec;
3616 /* Parse a user-defined numeric constant. returns a call to a user-defined
3617 literal operator. */
3619 static tree
3620 cp_parser_userdef_numeric_literal (cp_parser *parser)
3622 cp_token *token = cp_lexer_consume_token (parser->lexer);
3623 tree literal = token->u.value;
3624 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3625 tree value = USERDEF_LITERAL_VALUE (literal);
3626 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3627 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3628 tree decl, result;
3629 VEC(tree,gc) *args;
3631 /* Look for a literal operator taking the exact type of numeric argument
3632 as the literal value. */
3633 args = make_tree_vector ();
3634 VEC_safe_push (tree, gc, args, value);
3635 decl = lookup_literal_operator (name, args);
3636 if (decl && decl != error_mark_node)
3638 result = finish_call_expr (decl, &args, false, true, tf_none);
3639 if (result != error_mark_node)
3641 release_tree_vector (args);
3642 return result;
3645 release_tree_vector (args);
3647 /* If the numeric argument didn't work, look for a raw literal
3648 operator taking a const char* argument consisting of the number
3649 in string format. */
3650 args = make_tree_vector ();
3651 VEC_safe_push (tree, gc, args, num_string);
3652 decl = lookup_literal_operator (name, args);
3653 if (decl && decl != error_mark_node)
3655 result = finish_call_expr (decl, &args, false, true, tf_none);
3656 if (result != error_mark_node)
3658 release_tree_vector (args);
3659 return result;
3662 release_tree_vector (args);
3664 /* If the raw literal didn't work, look for a non-type template
3665 function with parameter pack char.... Call the function with
3666 template parameter characters representing the number. */
3667 args = make_tree_vector ();
3668 decl = lookup_literal_operator (name, args);
3669 if (decl && decl != error_mark_node)
3671 tree tmpl_args = make_char_string_pack (num_string);
3672 decl = lookup_template_function (decl, tmpl_args);
3673 result = finish_call_expr (decl, &args, false, true, tf_none);
3674 if (result != error_mark_node)
3676 release_tree_vector (args);
3677 return result;
3680 release_tree_vector (args);
3682 error ("unable to find numeric literal operator %qD", name);
3683 return error_mark_node;
3686 /* Parse a user-defined string constant. Returns a call to a user-defined
3687 literal operator taking a character pointer and the length of the string
3688 as arguments. */
3690 static tree
3691 cp_parser_userdef_string_literal (cp_token *token)
3693 tree literal = token->u.value;
3694 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3695 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3696 tree value = USERDEF_LITERAL_VALUE (literal);
3697 int len = TREE_STRING_LENGTH (value)
3698 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3699 tree decl, result;
3701 /* Build up a call to the user-defined operator */
3702 /* Lookup the name we got back from the id-expression. */
3703 VEC(tree,gc) *args = make_tree_vector ();
3704 VEC_safe_push (tree, gc, args, value);
3705 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3706 decl = lookup_name (name);
3707 if (!decl || decl == error_mark_node)
3709 error ("unable to find string literal operator %qD", name);
3710 release_tree_vector (args);
3711 return error_mark_node;
3713 result = finish_call_expr (decl, &args, false, true, tf_none);
3714 release_tree_vector (args);
3715 if (result != error_mark_node)
3716 return result;
3718 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3719 name, TREE_TYPE (value), size_type_node);
3720 return error_mark_node;
3724 /* Basic concepts [gram.basic] */
3726 /* Parse a translation-unit.
3728 translation-unit:
3729 declaration-seq [opt]
3731 Returns TRUE if all went well. */
3733 static bool
3734 cp_parser_translation_unit (cp_parser* parser)
3736 /* The address of the first non-permanent object on the declarator
3737 obstack. */
3738 static void *declarator_obstack_base;
3740 bool success;
3742 /* Create the declarator obstack, if necessary. */
3743 if (!cp_error_declarator)
3745 gcc_obstack_init (&declarator_obstack);
3746 /* Create the error declarator. */
3747 cp_error_declarator = make_declarator (cdk_error);
3748 /* Create the empty parameter list. */
3749 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3750 /* Remember where the base of the declarator obstack lies. */
3751 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3754 cp_parser_declaration_seq_opt (parser);
3756 /* If there are no tokens left then all went well. */
3757 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3759 /* Get rid of the token array; we don't need it any more. */
3760 cp_lexer_destroy (parser->lexer);
3761 parser->lexer = NULL;
3763 /* This file might have been a context that's implicitly extern
3764 "C". If so, pop the lang context. (Only relevant for PCH.) */
3765 if (parser->implicit_extern_c)
3767 pop_lang_context ();
3768 parser->implicit_extern_c = false;
3771 /* Finish up. */
3772 finish_translation_unit ();
3774 success = true;
3776 else
3778 cp_parser_error (parser, "expected declaration");
3779 success = false;
3782 /* Make sure the declarator obstack was fully cleaned up. */
3783 gcc_assert (obstack_next_free (&declarator_obstack)
3784 == declarator_obstack_base);
3786 /* All went well. */
3787 return success;
3790 /* Expressions [gram.expr] */
3792 /* Parse a primary-expression.
3794 primary-expression:
3795 literal
3796 this
3797 ( expression )
3798 id-expression
3800 GNU Extensions:
3802 primary-expression:
3803 ( compound-statement )
3804 __builtin_va_arg ( assignment-expression , type-id )
3805 __builtin_offsetof ( type-id , offsetof-expression )
3807 C++ Extensions:
3808 __has_nothrow_assign ( type-id )
3809 __has_nothrow_constructor ( type-id )
3810 __has_nothrow_copy ( type-id )
3811 __has_trivial_assign ( type-id )
3812 __has_trivial_constructor ( type-id )
3813 __has_trivial_copy ( type-id )
3814 __has_trivial_destructor ( type-id )
3815 __has_virtual_destructor ( type-id )
3816 __is_abstract ( type-id )
3817 __is_base_of ( type-id , type-id )
3818 __is_class ( type-id )
3819 __is_convertible_to ( type-id , type-id )
3820 __is_empty ( type-id )
3821 __is_enum ( type-id )
3822 __is_final ( type-id )
3823 __is_literal_type ( type-id )
3824 __is_pod ( type-id )
3825 __is_polymorphic ( type-id )
3826 __is_std_layout ( type-id )
3827 __is_trivial ( type-id )
3828 __is_union ( type-id )
3830 Objective-C++ Extension:
3832 primary-expression:
3833 objc-expression
3835 literal:
3836 __null
3838 ADDRESS_P is true iff this expression was immediately preceded by
3839 "&" and therefore might denote a pointer-to-member. CAST_P is true
3840 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3841 true iff this expression is a template argument.
3843 Returns a representation of the expression. Upon return, *IDK
3844 indicates what kind of id-expression (if any) was present. */
3846 static tree
3847 cp_parser_primary_expression (cp_parser *parser,
3848 bool address_p,
3849 bool cast_p,
3850 bool template_arg_p,
3851 cp_id_kind *idk)
3853 cp_token *token = NULL;
3855 /* Assume the primary expression is not an id-expression. */
3856 *idk = CP_ID_KIND_NONE;
3858 /* Peek at the next token. */
3859 token = cp_lexer_peek_token (parser->lexer);
3860 switch (token->type)
3862 /* literal:
3863 integer-literal
3864 character-literal
3865 floating-literal
3866 string-literal
3867 boolean-literal
3868 pointer-literal
3869 user-defined-literal */
3870 case CPP_CHAR:
3871 case CPP_CHAR16:
3872 case CPP_CHAR32:
3873 case CPP_WCHAR:
3874 case CPP_NUMBER:
3875 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3876 return cp_parser_userdef_numeric_literal (parser);
3877 token = cp_lexer_consume_token (parser->lexer);
3878 if (TREE_CODE (token->u.value) == FIXED_CST)
3880 error_at (token->location,
3881 "fixed-point types not supported in C++");
3882 return error_mark_node;
3884 /* Floating-point literals are only allowed in an integral
3885 constant expression if they are cast to an integral or
3886 enumeration type. */
3887 if (TREE_CODE (token->u.value) == REAL_CST
3888 && parser->integral_constant_expression_p
3889 && pedantic)
3891 /* CAST_P will be set even in invalid code like "int(2.7 +
3892 ...)". Therefore, we have to check that the next token
3893 is sure to end the cast. */
3894 if (cast_p)
3896 cp_token *next_token;
3898 next_token = cp_lexer_peek_token (parser->lexer);
3899 if (/* The comma at the end of an
3900 enumerator-definition. */
3901 next_token->type != CPP_COMMA
3902 /* The curly brace at the end of an enum-specifier. */
3903 && next_token->type != CPP_CLOSE_BRACE
3904 /* The end of a statement. */
3905 && next_token->type != CPP_SEMICOLON
3906 /* The end of the cast-expression. */
3907 && next_token->type != CPP_CLOSE_PAREN
3908 /* The end of an array bound. */
3909 && next_token->type != CPP_CLOSE_SQUARE
3910 /* The closing ">" in a template-argument-list. */
3911 && (next_token->type != CPP_GREATER
3912 || parser->greater_than_is_operator_p)
3913 /* C++0x only: A ">>" treated like two ">" tokens,
3914 in a template-argument-list. */
3915 && (next_token->type != CPP_RSHIFT
3916 || (cxx_dialect == cxx98)
3917 || parser->greater_than_is_operator_p))
3918 cast_p = false;
3921 /* If we are within a cast, then the constraint that the
3922 cast is to an integral or enumeration type will be
3923 checked at that point. If we are not within a cast, then
3924 this code is invalid. */
3925 if (!cast_p)
3926 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3928 return token->u.value;
3930 case CPP_CHAR_USERDEF:
3931 case CPP_CHAR16_USERDEF:
3932 case CPP_CHAR32_USERDEF:
3933 case CPP_WCHAR_USERDEF:
3934 return cp_parser_userdef_char_literal (parser);
3936 case CPP_STRING:
3937 case CPP_STRING16:
3938 case CPP_STRING32:
3939 case CPP_WSTRING:
3940 case CPP_UTF8STRING:
3941 case CPP_STRING_USERDEF:
3942 case CPP_STRING16_USERDEF:
3943 case CPP_STRING32_USERDEF:
3944 case CPP_WSTRING_USERDEF:
3945 case CPP_UTF8STRING_USERDEF:
3946 /* ??? Should wide strings be allowed when parser->translate_strings_p
3947 is false (i.e. in attributes)? If not, we can kill the third
3948 argument to cp_parser_string_literal. */
3949 return cp_parser_string_literal (parser,
3950 parser->translate_strings_p,
3951 true);
3953 case CPP_OPEN_PAREN:
3955 tree expr;
3956 bool saved_greater_than_is_operator_p;
3958 /* Consume the `('. */
3959 cp_lexer_consume_token (parser->lexer);
3960 /* Within a parenthesized expression, a `>' token is always
3961 the greater-than operator. */
3962 saved_greater_than_is_operator_p
3963 = parser->greater_than_is_operator_p;
3964 parser->greater_than_is_operator_p = true;
3965 /* If we see `( { ' then we are looking at the beginning of
3966 a GNU statement-expression. */
3967 if (cp_parser_allow_gnu_extensions_p (parser)
3968 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3970 /* Statement-expressions are not allowed by the standard. */
3971 pedwarn (token->location, OPT_Wpedantic,
3972 "ISO C++ forbids braced-groups within expressions");
3974 /* And they're not allowed outside of a function-body; you
3975 cannot, for example, write:
3977 int i = ({ int j = 3; j + 1; });
3979 at class or namespace scope. */
3980 if (!parser->in_function_body
3981 || parser->in_template_argument_list_p)
3983 error_at (token->location,
3984 "statement-expressions are not allowed outside "
3985 "functions nor in template-argument lists");
3986 cp_parser_skip_to_end_of_block_or_statement (parser);
3987 expr = error_mark_node;
3989 else
3991 /* Start the statement-expression. */
3992 expr = begin_stmt_expr ();
3993 /* Parse the compound-statement. */
3994 cp_parser_compound_statement (parser, expr, false, false);
3995 /* Finish up. */
3996 expr = finish_stmt_expr (expr, false);
3999 else
4001 /* Parse the parenthesized expression. */
4002 expr = cp_parser_expression (parser, cast_p, idk);
4003 /* Let the front end know that this expression was
4004 enclosed in parentheses. This matters in case, for
4005 example, the expression is of the form `A::B', since
4006 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4007 not. */
4008 finish_parenthesized_expr (expr);
4009 /* DR 705: Wrapping an unqualified name in parentheses
4010 suppresses arg-dependent lookup. We want to pass back
4011 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4012 (c++/37862), but none of the others. */
4013 if (*idk != CP_ID_KIND_QUALIFIED)
4014 *idk = CP_ID_KIND_NONE;
4016 /* The `>' token might be the end of a template-id or
4017 template-parameter-list now. */
4018 parser->greater_than_is_operator_p
4019 = saved_greater_than_is_operator_p;
4020 /* Consume the `)'. */
4021 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4022 cp_parser_skip_to_end_of_statement (parser);
4024 return expr;
4027 case CPP_OPEN_SQUARE:
4028 if (c_dialect_objc ())
4029 /* We have an Objective-C++ message. */
4030 return cp_parser_objc_expression (parser);
4032 tree lam = cp_parser_lambda_expression (parser);
4033 /* Don't warn about a failed tentative parse. */
4034 if (cp_parser_error_occurred (parser))
4035 return error_mark_node;
4036 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4037 return lam;
4040 case CPP_OBJC_STRING:
4041 if (c_dialect_objc ())
4042 /* We have an Objective-C++ string literal. */
4043 return cp_parser_objc_expression (parser);
4044 cp_parser_error (parser, "expected primary-expression");
4045 return error_mark_node;
4047 case CPP_KEYWORD:
4048 switch (token->keyword)
4050 /* These two are the boolean literals. */
4051 case RID_TRUE:
4052 cp_lexer_consume_token (parser->lexer);
4053 return boolean_true_node;
4054 case RID_FALSE:
4055 cp_lexer_consume_token (parser->lexer);
4056 return boolean_false_node;
4058 /* The `__null' literal. */
4059 case RID_NULL:
4060 cp_lexer_consume_token (parser->lexer);
4061 return null_node;
4063 /* The `nullptr' literal. */
4064 case RID_NULLPTR:
4065 cp_lexer_consume_token (parser->lexer);
4066 return nullptr_node;
4068 /* Recognize the `this' keyword. */
4069 case RID_THIS:
4070 cp_lexer_consume_token (parser->lexer);
4071 if (parser->local_variables_forbidden_p)
4073 error_at (token->location,
4074 "%<this%> may not be used in this context");
4075 return error_mark_node;
4077 /* Pointers cannot appear in constant-expressions. */
4078 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4079 return error_mark_node;
4080 return finish_this_expr ();
4082 /* The `operator' keyword can be the beginning of an
4083 id-expression. */
4084 case RID_OPERATOR:
4085 goto id_expression;
4087 case RID_FUNCTION_NAME:
4088 case RID_PRETTY_FUNCTION_NAME:
4089 case RID_C99_FUNCTION_NAME:
4091 non_integral_constant name;
4093 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4094 __func__ are the names of variables -- but they are
4095 treated specially. Therefore, they are handled here,
4096 rather than relying on the generic id-expression logic
4097 below. Grammatically, these names are id-expressions.
4099 Consume the token. */
4100 token = cp_lexer_consume_token (parser->lexer);
4102 switch (token->keyword)
4104 case RID_FUNCTION_NAME:
4105 name = NIC_FUNC_NAME;
4106 break;
4107 case RID_PRETTY_FUNCTION_NAME:
4108 name = NIC_PRETTY_FUNC;
4109 break;
4110 case RID_C99_FUNCTION_NAME:
4111 name = NIC_C99_FUNC;
4112 break;
4113 default:
4114 gcc_unreachable ();
4117 if (cp_parser_non_integral_constant_expression (parser, name))
4118 return error_mark_node;
4120 /* Look up the name. */
4121 return finish_fname (token->u.value);
4124 case RID_VA_ARG:
4126 tree expression;
4127 tree type;
4128 source_location type_location;
4130 /* The `__builtin_va_arg' construct is used to handle
4131 `va_arg'. Consume the `__builtin_va_arg' token. */
4132 cp_lexer_consume_token (parser->lexer);
4133 /* Look for the opening `('. */
4134 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4135 /* Now, parse the assignment-expression. */
4136 expression = cp_parser_assignment_expression (parser,
4137 /*cast_p=*/false, NULL);
4138 /* Look for the `,'. */
4139 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4140 type_location = cp_lexer_peek_token (parser->lexer)->location;
4141 /* Parse the type-id. */
4142 type = cp_parser_type_id (parser);
4143 /* Look for the closing `)'. */
4144 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4145 /* Using `va_arg' in a constant-expression is not
4146 allowed. */
4147 if (cp_parser_non_integral_constant_expression (parser,
4148 NIC_VA_ARG))
4149 return error_mark_node;
4150 return build_x_va_arg (type_location, expression, type);
4153 case RID_OFFSETOF:
4154 return cp_parser_builtin_offsetof (parser);
4156 case RID_HAS_NOTHROW_ASSIGN:
4157 case RID_HAS_NOTHROW_CONSTRUCTOR:
4158 case RID_HAS_NOTHROW_COPY:
4159 case RID_HAS_TRIVIAL_ASSIGN:
4160 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4161 case RID_HAS_TRIVIAL_COPY:
4162 case RID_HAS_TRIVIAL_DESTRUCTOR:
4163 case RID_HAS_VIRTUAL_DESTRUCTOR:
4164 case RID_IS_ABSTRACT:
4165 case RID_IS_BASE_OF:
4166 case RID_IS_CLASS:
4167 case RID_IS_CONVERTIBLE_TO:
4168 case RID_IS_EMPTY:
4169 case RID_IS_ENUM:
4170 case RID_IS_FINAL:
4171 case RID_IS_LITERAL_TYPE:
4172 case RID_IS_POD:
4173 case RID_IS_POLYMORPHIC:
4174 case RID_IS_STD_LAYOUT:
4175 case RID_IS_TRIVIAL:
4176 case RID_IS_UNION:
4177 return cp_parser_trait_expr (parser, token->keyword);
4179 /* Objective-C++ expressions. */
4180 case RID_AT_ENCODE:
4181 case RID_AT_PROTOCOL:
4182 case RID_AT_SELECTOR:
4183 return cp_parser_objc_expression (parser);
4185 case RID_TEMPLATE:
4186 if (parser->in_function_body
4187 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4188 == CPP_LESS))
4190 error_at (token->location,
4191 "a template declaration cannot appear at block scope");
4192 cp_parser_skip_to_end_of_block_or_statement (parser);
4193 return error_mark_node;
4195 default:
4196 cp_parser_error (parser, "expected primary-expression");
4197 return error_mark_node;
4200 /* An id-expression can start with either an identifier, a
4201 `::' as the beginning of a qualified-id, or the "operator"
4202 keyword. */
4203 case CPP_NAME:
4204 case CPP_SCOPE:
4205 case CPP_TEMPLATE_ID:
4206 case CPP_NESTED_NAME_SPECIFIER:
4208 tree id_expression;
4209 tree decl;
4210 const char *error_msg;
4211 bool template_p;
4212 bool done;
4213 cp_token *id_expr_token;
4215 id_expression:
4216 /* Parse the id-expression. */
4217 id_expression
4218 = cp_parser_id_expression (parser,
4219 /*template_keyword_p=*/false,
4220 /*check_dependency_p=*/true,
4221 &template_p,
4222 /*declarator_p=*/false,
4223 /*optional_p=*/false);
4224 if (id_expression == error_mark_node)
4225 return error_mark_node;
4226 id_expr_token = token;
4227 token = cp_lexer_peek_token (parser->lexer);
4228 done = (token->type != CPP_OPEN_SQUARE
4229 && token->type != CPP_OPEN_PAREN
4230 && token->type != CPP_DOT
4231 && token->type != CPP_DEREF
4232 && token->type != CPP_PLUS_PLUS
4233 && token->type != CPP_MINUS_MINUS);
4234 /* If we have a template-id, then no further lookup is
4235 required. If the template-id was for a template-class, we
4236 will sometimes have a TYPE_DECL at this point. */
4237 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4238 || TREE_CODE (id_expression) == TYPE_DECL)
4239 decl = id_expression;
4240 /* Look up the name. */
4241 else
4243 tree ambiguous_decls;
4245 /* If we already know that this lookup is ambiguous, then
4246 we've already issued an error message; there's no reason
4247 to check again. */
4248 if (id_expr_token->type == CPP_NAME
4249 && id_expr_token->ambiguous_p)
4251 cp_parser_simulate_error (parser);
4252 return error_mark_node;
4255 decl = cp_parser_lookup_name (parser, id_expression,
4256 none_type,
4257 template_p,
4258 /*is_namespace=*/false,
4259 /*check_dependency=*/true,
4260 &ambiguous_decls,
4261 id_expr_token->location);
4262 /* If the lookup was ambiguous, an error will already have
4263 been issued. */
4264 if (ambiguous_decls)
4265 return error_mark_node;
4267 /* In Objective-C++, we may have an Objective-C 2.0
4268 dot-syntax for classes here. */
4269 if (c_dialect_objc ()
4270 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4271 && TREE_CODE (decl) == TYPE_DECL
4272 && objc_is_class_name (decl))
4274 tree component;
4275 cp_lexer_consume_token (parser->lexer);
4276 component = cp_parser_identifier (parser);
4277 if (component == error_mark_node)
4278 return error_mark_node;
4280 return objc_build_class_component_ref (id_expression, component);
4283 /* In Objective-C++, an instance variable (ivar) may be preferred
4284 to whatever cp_parser_lookup_name() found. */
4285 decl = objc_lookup_ivar (decl, id_expression);
4287 /* If name lookup gives us a SCOPE_REF, then the
4288 qualifying scope was dependent. */
4289 if (TREE_CODE (decl) == SCOPE_REF)
4291 /* At this point, we do not know if DECL is a valid
4292 integral constant expression. We assume that it is
4293 in fact such an expression, so that code like:
4295 template <int N> struct A {
4296 int a[B<N>::i];
4299 is accepted. At template-instantiation time, we
4300 will check that B<N>::i is actually a constant. */
4301 return decl;
4303 /* Check to see if DECL is a local variable in a context
4304 where that is forbidden. */
4305 if (parser->local_variables_forbidden_p
4306 && local_variable_p (decl))
4308 /* It might be that we only found DECL because we are
4309 trying to be generous with pre-ISO scoping rules.
4310 For example, consider:
4312 int i;
4313 void g() {
4314 for (int i = 0; i < 10; ++i) {}
4315 extern void f(int j = i);
4318 Here, name look up will originally find the out
4319 of scope `i'. We need to issue a warning message,
4320 but then use the global `i'. */
4321 decl = check_for_out_of_scope_variable (decl);
4322 if (local_variable_p (decl))
4324 error_at (id_expr_token->location,
4325 "local variable %qD may not appear in this context",
4326 decl);
4327 return error_mark_node;
4332 decl = (finish_id_expression
4333 (id_expression, decl, parser->scope,
4334 idk,
4335 parser->integral_constant_expression_p,
4336 parser->allow_non_integral_constant_expression_p,
4337 &parser->non_integral_constant_expression_p,
4338 template_p, done, address_p,
4339 template_arg_p,
4340 &error_msg,
4341 id_expr_token->location));
4342 if (error_msg)
4343 cp_parser_error (parser, error_msg);
4344 return decl;
4347 /* Anything else is an error. */
4348 default:
4349 cp_parser_error (parser, "expected primary-expression");
4350 return error_mark_node;
4354 /* Parse an id-expression.
4356 id-expression:
4357 unqualified-id
4358 qualified-id
4360 qualified-id:
4361 :: [opt] nested-name-specifier template [opt] unqualified-id
4362 :: identifier
4363 :: operator-function-id
4364 :: template-id
4366 Return a representation of the unqualified portion of the
4367 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4368 a `::' or nested-name-specifier.
4370 Often, if the id-expression was a qualified-id, the caller will
4371 want to make a SCOPE_REF to represent the qualified-id. This
4372 function does not do this in order to avoid wastefully creating
4373 SCOPE_REFs when they are not required.
4375 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4376 `template' keyword.
4378 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4379 uninstantiated templates.
4381 If *TEMPLATE_P is non-NULL, it is set to true iff the
4382 `template' keyword is used to explicitly indicate that the entity
4383 named is a template.
4385 If DECLARATOR_P is true, the id-expression is appearing as part of
4386 a declarator, rather than as part of an expression. */
4388 static tree
4389 cp_parser_id_expression (cp_parser *parser,
4390 bool template_keyword_p,
4391 bool check_dependency_p,
4392 bool *template_p,
4393 bool declarator_p,
4394 bool optional_p)
4396 bool global_scope_p;
4397 bool nested_name_specifier_p;
4399 /* Assume the `template' keyword was not used. */
4400 if (template_p)
4401 *template_p = template_keyword_p;
4403 /* Look for the optional `::' operator. */
4404 global_scope_p
4405 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4406 != NULL_TREE);
4407 /* Look for the optional nested-name-specifier. */
4408 nested_name_specifier_p
4409 = (cp_parser_nested_name_specifier_opt (parser,
4410 /*typename_keyword_p=*/false,
4411 check_dependency_p,
4412 /*type_p=*/false,
4413 declarator_p)
4414 != NULL_TREE);
4415 /* If there is a nested-name-specifier, then we are looking at
4416 the first qualified-id production. */
4417 if (nested_name_specifier_p)
4419 tree saved_scope;
4420 tree saved_object_scope;
4421 tree saved_qualifying_scope;
4422 tree unqualified_id;
4423 bool is_template;
4425 /* See if the next token is the `template' keyword. */
4426 if (!template_p)
4427 template_p = &is_template;
4428 *template_p = cp_parser_optional_template_keyword (parser);
4429 /* Name lookup we do during the processing of the
4430 unqualified-id might obliterate SCOPE. */
4431 saved_scope = parser->scope;
4432 saved_object_scope = parser->object_scope;
4433 saved_qualifying_scope = parser->qualifying_scope;
4434 /* Process the final unqualified-id. */
4435 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4436 check_dependency_p,
4437 declarator_p,
4438 /*optional_p=*/false);
4439 /* Restore the SAVED_SCOPE for our caller. */
4440 parser->scope = saved_scope;
4441 parser->object_scope = saved_object_scope;
4442 parser->qualifying_scope = saved_qualifying_scope;
4444 return unqualified_id;
4446 /* Otherwise, if we are in global scope, then we are looking at one
4447 of the other qualified-id productions. */
4448 else if (global_scope_p)
4450 cp_token *token;
4451 tree id;
4453 /* Peek at the next token. */
4454 token = cp_lexer_peek_token (parser->lexer);
4456 /* If it's an identifier, and the next token is not a "<", then
4457 we can avoid the template-id case. This is an optimization
4458 for this common case. */
4459 if (token->type == CPP_NAME
4460 && !cp_parser_nth_token_starts_template_argument_list_p
4461 (parser, 2))
4462 return cp_parser_identifier (parser);
4464 cp_parser_parse_tentatively (parser);
4465 /* Try a template-id. */
4466 id = cp_parser_template_id (parser,
4467 /*template_keyword_p=*/false,
4468 /*check_dependency_p=*/true,
4469 declarator_p);
4470 /* If that worked, we're done. */
4471 if (cp_parser_parse_definitely (parser))
4472 return id;
4474 /* Peek at the next token. (Changes in the token buffer may
4475 have invalidated the pointer obtained above.) */
4476 token = cp_lexer_peek_token (parser->lexer);
4478 switch (token->type)
4480 case CPP_NAME:
4481 return cp_parser_identifier (parser);
4483 case CPP_KEYWORD:
4484 if (token->keyword == RID_OPERATOR)
4485 return cp_parser_operator_function_id (parser);
4486 /* Fall through. */
4488 default:
4489 cp_parser_error (parser, "expected id-expression");
4490 return error_mark_node;
4493 else
4494 return cp_parser_unqualified_id (parser, template_keyword_p,
4495 /*check_dependency_p=*/true,
4496 declarator_p,
4497 optional_p);
4500 /* Parse an unqualified-id.
4502 unqualified-id:
4503 identifier
4504 operator-function-id
4505 conversion-function-id
4506 ~ class-name
4507 template-id
4509 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4510 keyword, in a construct like `A::template ...'.
4512 Returns a representation of unqualified-id. For the `identifier'
4513 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4514 production a BIT_NOT_EXPR is returned; the operand of the
4515 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4516 other productions, see the documentation accompanying the
4517 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4518 names are looked up in uninstantiated templates. If DECLARATOR_P
4519 is true, the unqualified-id is appearing as part of a declarator,
4520 rather than as part of an expression. */
4522 static tree
4523 cp_parser_unqualified_id (cp_parser* parser,
4524 bool template_keyword_p,
4525 bool check_dependency_p,
4526 bool declarator_p,
4527 bool optional_p)
4529 cp_token *token;
4531 /* Peek at the next token. */
4532 token = cp_lexer_peek_token (parser->lexer);
4534 switch (token->type)
4536 case CPP_NAME:
4538 tree id;
4540 /* We don't know yet whether or not this will be a
4541 template-id. */
4542 cp_parser_parse_tentatively (parser);
4543 /* Try a template-id. */
4544 id = cp_parser_template_id (parser, template_keyword_p,
4545 check_dependency_p,
4546 declarator_p);
4547 /* If it worked, we're done. */
4548 if (cp_parser_parse_definitely (parser))
4549 return id;
4550 /* Otherwise, it's an ordinary identifier. */
4551 return cp_parser_identifier (parser);
4554 case CPP_TEMPLATE_ID:
4555 return cp_parser_template_id (parser, template_keyword_p,
4556 check_dependency_p,
4557 declarator_p);
4559 case CPP_COMPL:
4561 tree type_decl;
4562 tree qualifying_scope;
4563 tree object_scope;
4564 tree scope;
4565 bool done;
4567 /* Consume the `~' token. */
4568 cp_lexer_consume_token (parser->lexer);
4569 /* Parse the class-name. The standard, as written, seems to
4570 say that:
4572 template <typename T> struct S { ~S (); };
4573 template <typename T> S<T>::~S() {}
4575 is invalid, since `~' must be followed by a class-name, but
4576 `S<T>' is dependent, and so not known to be a class.
4577 That's not right; we need to look in uninstantiated
4578 templates. A further complication arises from:
4580 template <typename T> void f(T t) {
4581 t.T::~T();
4584 Here, it is not possible to look up `T' in the scope of `T'
4585 itself. We must look in both the current scope, and the
4586 scope of the containing complete expression.
4588 Yet another issue is:
4590 struct S {
4591 int S;
4592 ~S();
4595 S::~S() {}
4597 The standard does not seem to say that the `S' in `~S'
4598 should refer to the type `S' and not the data member
4599 `S::S'. */
4601 /* DR 244 says that we look up the name after the "~" in the
4602 same scope as we looked up the qualifying name. That idea
4603 isn't fully worked out; it's more complicated than that. */
4604 scope = parser->scope;
4605 object_scope = parser->object_scope;
4606 qualifying_scope = parser->qualifying_scope;
4608 /* Check for invalid scopes. */
4609 if (scope == error_mark_node)
4611 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4612 cp_lexer_consume_token (parser->lexer);
4613 return error_mark_node;
4615 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4617 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4618 error_at (token->location,
4619 "scope %qT before %<~%> is not a class-name",
4620 scope);
4621 cp_parser_simulate_error (parser);
4622 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4623 cp_lexer_consume_token (parser->lexer);
4624 return error_mark_node;
4626 gcc_assert (!scope || TYPE_P (scope));
4628 /* If the name is of the form "X::~X" it's OK even if X is a
4629 typedef. */
4630 token = cp_lexer_peek_token (parser->lexer);
4631 if (scope
4632 && token->type == CPP_NAME
4633 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4634 != CPP_LESS)
4635 && (token->u.value == TYPE_IDENTIFIER (scope)
4636 || (CLASS_TYPE_P (scope)
4637 && constructor_name_p (token->u.value, scope))))
4639 cp_lexer_consume_token (parser->lexer);
4640 return build_nt (BIT_NOT_EXPR, scope);
4643 /* If there was an explicit qualification (S::~T), first look
4644 in the scope given by the qualification (i.e., S).
4646 Note: in the calls to cp_parser_class_name below we pass
4647 typename_type so that lookup finds the injected-class-name
4648 rather than the constructor. */
4649 done = false;
4650 type_decl = NULL_TREE;
4651 if (scope)
4653 cp_parser_parse_tentatively (parser);
4654 type_decl = cp_parser_class_name (parser,
4655 /*typename_keyword_p=*/false,
4656 /*template_keyword_p=*/false,
4657 typename_type,
4658 /*check_dependency=*/false,
4659 /*class_head_p=*/false,
4660 declarator_p);
4661 if (cp_parser_parse_definitely (parser))
4662 done = true;
4664 /* In "N::S::~S", look in "N" as well. */
4665 if (!done && scope && qualifying_scope)
4667 cp_parser_parse_tentatively (parser);
4668 parser->scope = qualifying_scope;
4669 parser->object_scope = NULL_TREE;
4670 parser->qualifying_scope = NULL_TREE;
4671 type_decl
4672 = cp_parser_class_name (parser,
4673 /*typename_keyword_p=*/false,
4674 /*template_keyword_p=*/false,
4675 typename_type,
4676 /*check_dependency=*/false,
4677 /*class_head_p=*/false,
4678 declarator_p);
4679 if (cp_parser_parse_definitely (parser))
4680 done = true;
4682 /* In "p->S::~T", look in the scope given by "*p" as well. */
4683 else if (!done && object_scope)
4685 cp_parser_parse_tentatively (parser);
4686 parser->scope = object_scope;
4687 parser->object_scope = NULL_TREE;
4688 parser->qualifying_scope = NULL_TREE;
4689 type_decl
4690 = cp_parser_class_name (parser,
4691 /*typename_keyword_p=*/false,
4692 /*template_keyword_p=*/false,
4693 typename_type,
4694 /*check_dependency=*/false,
4695 /*class_head_p=*/false,
4696 declarator_p);
4697 if (cp_parser_parse_definitely (parser))
4698 done = true;
4700 /* Look in the surrounding context. */
4701 if (!done)
4703 parser->scope = NULL_TREE;
4704 parser->object_scope = NULL_TREE;
4705 parser->qualifying_scope = NULL_TREE;
4706 if (processing_template_decl)
4707 cp_parser_parse_tentatively (parser);
4708 type_decl
4709 = cp_parser_class_name (parser,
4710 /*typename_keyword_p=*/false,
4711 /*template_keyword_p=*/false,
4712 typename_type,
4713 /*check_dependency=*/false,
4714 /*class_head_p=*/false,
4715 declarator_p);
4716 if (processing_template_decl
4717 && ! cp_parser_parse_definitely (parser))
4719 /* We couldn't find a type with this name, so just accept
4720 it and check for a match at instantiation time. */
4721 type_decl = cp_parser_identifier (parser);
4722 if (type_decl != error_mark_node)
4723 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4724 return type_decl;
4727 /* If an error occurred, assume that the name of the
4728 destructor is the same as the name of the qualifying
4729 class. That allows us to keep parsing after running
4730 into ill-formed destructor names. */
4731 if (type_decl == error_mark_node && scope)
4732 return build_nt (BIT_NOT_EXPR, scope);
4733 else if (type_decl == error_mark_node)
4734 return error_mark_node;
4736 /* Check that destructor name and scope match. */
4737 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4739 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4740 error_at (token->location,
4741 "declaration of %<~%T%> as member of %qT",
4742 type_decl, scope);
4743 cp_parser_simulate_error (parser);
4744 return error_mark_node;
4747 /* [class.dtor]
4749 A typedef-name that names a class shall not be used as the
4750 identifier in the declarator for a destructor declaration. */
4751 if (declarator_p
4752 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4753 && !DECL_SELF_REFERENCE_P (type_decl)
4754 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4755 error_at (token->location,
4756 "typedef-name %qD used as destructor declarator",
4757 type_decl);
4759 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4762 case CPP_KEYWORD:
4763 if (token->keyword == RID_OPERATOR)
4765 tree id;
4767 /* This could be a template-id, so we try that first. */
4768 cp_parser_parse_tentatively (parser);
4769 /* Try a template-id. */
4770 id = cp_parser_template_id (parser, template_keyword_p,
4771 /*check_dependency_p=*/true,
4772 declarator_p);
4773 /* If that worked, we're done. */
4774 if (cp_parser_parse_definitely (parser))
4775 return id;
4776 /* We still don't know whether we're looking at an
4777 operator-function-id or a conversion-function-id. */
4778 cp_parser_parse_tentatively (parser);
4779 /* Try an operator-function-id. */
4780 id = cp_parser_operator_function_id (parser);
4781 /* If that didn't work, try a conversion-function-id. */
4782 if (!cp_parser_parse_definitely (parser))
4783 id = cp_parser_conversion_function_id (parser);
4784 else if (UDLIT_OPER_P (id))
4786 /* 17.6.3.3.5 */
4787 const char *name = UDLIT_OP_SUFFIX (id);
4788 if (name[0] != '_' && !in_system_header)
4789 warning (0, "literal operator suffixes not preceded by %<_%>"
4790 " are reserved for future standardization");
4793 return id;
4795 /* Fall through. */
4797 default:
4798 if (optional_p)
4799 return NULL_TREE;
4800 cp_parser_error (parser, "expected unqualified-id");
4801 return error_mark_node;
4805 /* Parse an (optional) nested-name-specifier.
4807 nested-name-specifier: [C++98]
4808 class-or-namespace-name :: nested-name-specifier [opt]
4809 class-or-namespace-name :: template nested-name-specifier [opt]
4811 nested-name-specifier: [C++0x]
4812 type-name ::
4813 namespace-name ::
4814 nested-name-specifier identifier ::
4815 nested-name-specifier template [opt] simple-template-id ::
4817 PARSER->SCOPE should be set appropriately before this function is
4818 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4819 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4820 in name lookups.
4822 Sets PARSER->SCOPE to the class (TYPE) or namespace
4823 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4824 it unchanged if there is no nested-name-specifier. Returns the new
4825 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4827 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4828 part of a declaration and/or decl-specifier. */
4830 static tree
4831 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4832 bool typename_keyword_p,
4833 bool check_dependency_p,
4834 bool type_p,
4835 bool is_declaration)
4837 bool success = false;
4838 cp_token_position start = 0;
4839 cp_token *token;
4841 /* Remember where the nested-name-specifier starts. */
4842 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4844 start = cp_lexer_token_position (parser->lexer, false);
4845 push_deferring_access_checks (dk_deferred);
4848 while (true)
4850 tree new_scope;
4851 tree old_scope;
4852 tree saved_qualifying_scope;
4853 bool template_keyword_p;
4855 /* Spot cases that cannot be the beginning of a
4856 nested-name-specifier. */
4857 token = cp_lexer_peek_token (parser->lexer);
4859 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4860 the already parsed nested-name-specifier. */
4861 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4863 /* Grab the nested-name-specifier and continue the loop. */
4864 cp_parser_pre_parsed_nested_name_specifier (parser);
4865 /* If we originally encountered this nested-name-specifier
4866 with IS_DECLARATION set to false, we will not have
4867 resolved TYPENAME_TYPEs, so we must do so here. */
4868 if (is_declaration
4869 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4871 new_scope = resolve_typename_type (parser->scope,
4872 /*only_current_p=*/false);
4873 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4874 parser->scope = new_scope;
4876 success = true;
4877 continue;
4880 /* Spot cases that cannot be the beginning of a
4881 nested-name-specifier. On the second and subsequent times
4882 through the loop, we look for the `template' keyword. */
4883 if (success && token->keyword == RID_TEMPLATE)
4885 /* A template-id can start a nested-name-specifier. */
4886 else if (token->type == CPP_TEMPLATE_ID)
4888 /* DR 743: decltype can be used in a nested-name-specifier. */
4889 else if (token_is_decltype (token))
4891 else
4893 /* If the next token is not an identifier, then it is
4894 definitely not a type-name or namespace-name. */
4895 if (token->type != CPP_NAME)
4896 break;
4897 /* If the following token is neither a `<' (to begin a
4898 template-id), nor a `::', then we are not looking at a
4899 nested-name-specifier. */
4900 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4902 if (token->type == CPP_COLON
4903 && parser->colon_corrects_to_scope_p
4904 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4906 error_at (token->location,
4907 "found %<:%> in nested-name-specifier, expected %<::%>");
4908 token->type = CPP_SCOPE;
4911 if (token->type != CPP_SCOPE
4912 && !cp_parser_nth_token_starts_template_argument_list_p
4913 (parser, 2))
4914 break;
4917 /* The nested-name-specifier is optional, so we parse
4918 tentatively. */
4919 cp_parser_parse_tentatively (parser);
4921 /* Look for the optional `template' keyword, if this isn't the
4922 first time through the loop. */
4923 if (success)
4924 template_keyword_p = cp_parser_optional_template_keyword (parser);
4925 else
4926 template_keyword_p = false;
4928 /* Save the old scope since the name lookup we are about to do
4929 might destroy it. */
4930 old_scope = parser->scope;
4931 saved_qualifying_scope = parser->qualifying_scope;
4932 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4933 look up names in "X<T>::I" in order to determine that "Y" is
4934 a template. So, if we have a typename at this point, we make
4935 an effort to look through it. */
4936 if (is_declaration
4937 && !typename_keyword_p
4938 && parser->scope
4939 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4940 parser->scope = resolve_typename_type (parser->scope,
4941 /*only_current_p=*/false);
4942 /* Parse the qualifying entity. */
4943 new_scope
4944 = cp_parser_qualifying_entity (parser,
4945 typename_keyword_p,
4946 template_keyword_p,
4947 check_dependency_p,
4948 type_p,
4949 is_declaration);
4950 /* Look for the `::' token. */
4951 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4953 /* If we found what we wanted, we keep going; otherwise, we're
4954 done. */
4955 if (!cp_parser_parse_definitely (parser))
4957 bool error_p = false;
4959 /* Restore the OLD_SCOPE since it was valid before the
4960 failed attempt at finding the last
4961 class-or-namespace-name. */
4962 parser->scope = old_scope;
4963 parser->qualifying_scope = saved_qualifying_scope;
4965 /* If the next token is a decltype, and the one after that is a
4966 `::', then the decltype has failed to resolve to a class or
4967 enumeration type. Give this error even when parsing
4968 tentatively since it can't possibly be valid--and we're going
4969 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4970 won't get another chance.*/
4971 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4972 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4973 == CPP_SCOPE))
4975 token = cp_lexer_consume_token (parser->lexer);
4976 error_at (token->location, "decltype evaluates to %qT, "
4977 "which is not a class or enumeration type",
4978 token->u.value);
4979 parser->scope = error_mark_node;
4980 error_p = true;
4981 /* As below. */
4982 success = true;
4983 cp_lexer_consume_token (parser->lexer);
4986 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4987 break;
4988 /* If the next token is an identifier, and the one after
4989 that is a `::', then any valid interpretation would have
4990 found a class-or-namespace-name. */
4991 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4992 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4993 == CPP_SCOPE)
4994 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4995 != CPP_COMPL))
4997 token = cp_lexer_consume_token (parser->lexer);
4998 if (!error_p)
5000 if (!token->ambiguous_p)
5002 tree decl;
5003 tree ambiguous_decls;
5005 decl = cp_parser_lookup_name (parser, token->u.value,
5006 none_type,
5007 /*is_template=*/false,
5008 /*is_namespace=*/false,
5009 /*check_dependency=*/true,
5010 &ambiguous_decls,
5011 token->location);
5012 if (TREE_CODE (decl) == TEMPLATE_DECL)
5013 error_at (token->location,
5014 "%qD used without template parameters",
5015 decl);
5016 else if (ambiguous_decls)
5018 error_at (token->location,
5019 "reference to %qD is ambiguous",
5020 token->u.value);
5021 print_candidates (ambiguous_decls);
5022 decl = error_mark_node;
5024 else
5026 if (cxx_dialect != cxx98)
5027 cp_parser_name_lookup_error
5028 (parser, token->u.value, decl, NLE_NOT_CXX98,
5029 token->location);
5030 else
5031 cp_parser_name_lookup_error
5032 (parser, token->u.value, decl, NLE_CXX98,
5033 token->location);
5036 parser->scope = error_mark_node;
5037 error_p = true;
5038 /* Treat this as a successful nested-name-specifier
5039 due to:
5041 [basic.lookup.qual]
5043 If the name found is not a class-name (clause
5044 _class_) or namespace-name (_namespace.def_), the
5045 program is ill-formed. */
5046 success = true;
5048 cp_lexer_consume_token (parser->lexer);
5050 break;
5052 /* We've found one valid nested-name-specifier. */
5053 success = true;
5054 /* Name lookup always gives us a DECL. */
5055 if (TREE_CODE (new_scope) == TYPE_DECL)
5056 new_scope = TREE_TYPE (new_scope);
5057 /* Uses of "template" must be followed by actual templates. */
5058 if (template_keyword_p
5059 && !(CLASS_TYPE_P (new_scope)
5060 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5061 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5062 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5063 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5064 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5065 == TEMPLATE_ID_EXPR)))
5066 permerror (input_location, TYPE_P (new_scope)
5067 ? G_("%qT is not a template")
5068 : G_("%qD is not a template"),
5069 new_scope);
5070 /* If it is a class scope, try to complete it; we are about to
5071 be looking up names inside the class. */
5072 if (TYPE_P (new_scope)
5073 /* Since checking types for dependency can be expensive,
5074 avoid doing it if the type is already complete. */
5075 && !COMPLETE_TYPE_P (new_scope)
5076 /* Do not try to complete dependent types. */
5077 && !dependent_type_p (new_scope))
5079 new_scope = complete_type (new_scope);
5080 /* If it is a typedef to current class, use the current
5081 class instead, as the typedef won't have any names inside
5082 it yet. */
5083 if (!COMPLETE_TYPE_P (new_scope)
5084 && currently_open_class (new_scope))
5085 new_scope = TYPE_MAIN_VARIANT (new_scope);
5087 /* Make sure we look in the right scope the next time through
5088 the loop. */
5089 parser->scope = new_scope;
5092 /* If parsing tentatively, replace the sequence of tokens that makes
5093 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5094 token. That way, should we re-parse the token stream, we will
5095 not have to repeat the effort required to do the parse, nor will
5096 we issue duplicate error messages. */
5097 if (success && start)
5099 cp_token *token;
5101 token = cp_lexer_token_at (parser->lexer, start);
5102 /* Reset the contents of the START token. */
5103 token->type = CPP_NESTED_NAME_SPECIFIER;
5104 /* Retrieve any deferred checks. Do not pop this access checks yet
5105 so the memory will not be reclaimed during token replacing below. */
5106 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5107 token->u.tree_check_value->value = parser->scope;
5108 token->u.tree_check_value->checks = get_deferred_access_checks ();
5109 token->u.tree_check_value->qualifying_scope =
5110 parser->qualifying_scope;
5111 token->keyword = RID_MAX;
5113 /* Purge all subsequent tokens. */
5114 cp_lexer_purge_tokens_after (parser->lexer, start);
5117 if (start)
5118 pop_to_parent_deferring_access_checks ();
5120 return success ? parser->scope : NULL_TREE;
5123 /* Parse a nested-name-specifier. See
5124 cp_parser_nested_name_specifier_opt for details. This function
5125 behaves identically, except that it will an issue an error if no
5126 nested-name-specifier is present. */
5128 static tree
5129 cp_parser_nested_name_specifier (cp_parser *parser,
5130 bool typename_keyword_p,
5131 bool check_dependency_p,
5132 bool type_p,
5133 bool is_declaration)
5135 tree scope;
5137 /* Look for the nested-name-specifier. */
5138 scope = cp_parser_nested_name_specifier_opt (parser,
5139 typename_keyword_p,
5140 check_dependency_p,
5141 type_p,
5142 is_declaration);
5143 /* If it was not present, issue an error message. */
5144 if (!scope)
5146 cp_parser_error (parser, "expected nested-name-specifier");
5147 parser->scope = NULL_TREE;
5150 return scope;
5153 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5154 this is either a class-name or a namespace-name (which corresponds
5155 to the class-or-namespace-name production in the grammar). For
5156 C++0x, it can also be a type-name that refers to an enumeration
5157 type or a simple-template-id.
5159 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5160 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5161 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5162 TYPE_P is TRUE iff the next name should be taken as a class-name,
5163 even the same name is declared to be another entity in the same
5164 scope.
5166 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5167 specified by the class-or-namespace-name. If neither is found the
5168 ERROR_MARK_NODE is returned. */
5170 static tree
5171 cp_parser_qualifying_entity (cp_parser *parser,
5172 bool typename_keyword_p,
5173 bool template_keyword_p,
5174 bool check_dependency_p,
5175 bool type_p,
5176 bool is_declaration)
5178 tree saved_scope;
5179 tree saved_qualifying_scope;
5180 tree saved_object_scope;
5181 tree scope;
5182 bool only_class_p;
5183 bool successful_parse_p;
5185 /* DR 743: decltype can appear in a nested-name-specifier. */
5186 if (cp_lexer_next_token_is_decltype (parser->lexer))
5188 scope = cp_parser_decltype (parser);
5189 if (TREE_CODE (scope) != ENUMERAL_TYPE
5190 && !MAYBE_CLASS_TYPE_P (scope))
5192 cp_parser_simulate_error (parser);
5193 return error_mark_node;
5195 if (TYPE_NAME (scope))
5196 scope = TYPE_NAME (scope);
5197 return scope;
5200 /* Before we try to parse the class-name, we must save away the
5201 current PARSER->SCOPE since cp_parser_class_name will destroy
5202 it. */
5203 saved_scope = parser->scope;
5204 saved_qualifying_scope = parser->qualifying_scope;
5205 saved_object_scope = parser->object_scope;
5206 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5207 there is no need to look for a namespace-name. */
5208 only_class_p = template_keyword_p
5209 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5210 if (!only_class_p)
5211 cp_parser_parse_tentatively (parser);
5212 scope = cp_parser_class_name (parser,
5213 typename_keyword_p,
5214 template_keyword_p,
5215 type_p ? class_type : none_type,
5216 check_dependency_p,
5217 /*class_head_p=*/false,
5218 is_declaration);
5219 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5220 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5221 if (!only_class_p
5222 && cxx_dialect != cxx98
5223 && !successful_parse_p)
5225 /* Restore the saved scope. */
5226 parser->scope = saved_scope;
5227 parser->qualifying_scope = saved_qualifying_scope;
5228 parser->object_scope = saved_object_scope;
5230 /* Parse tentatively. */
5231 cp_parser_parse_tentatively (parser);
5233 /* Parse a type-name */
5234 scope = cp_parser_type_name (parser);
5236 /* "If the name found does not designate a namespace or a class,
5237 enumeration, or dependent type, the program is ill-formed."
5239 We cover classes and dependent types above and namespaces below,
5240 so this code is only looking for enums. */
5241 if (!scope || TREE_CODE (scope) != TYPE_DECL
5242 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5243 cp_parser_simulate_error (parser);
5245 successful_parse_p = cp_parser_parse_definitely (parser);
5247 /* If that didn't work, try for a namespace-name. */
5248 if (!only_class_p && !successful_parse_p)
5250 /* Restore the saved scope. */
5251 parser->scope = saved_scope;
5252 parser->qualifying_scope = saved_qualifying_scope;
5253 parser->object_scope = saved_object_scope;
5254 /* If we are not looking at an identifier followed by the scope
5255 resolution operator, then this is not part of a
5256 nested-name-specifier. (Note that this function is only used
5257 to parse the components of a nested-name-specifier.) */
5258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5259 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5260 return error_mark_node;
5261 scope = cp_parser_namespace_name (parser);
5264 return scope;
5267 /* Parse a postfix-expression.
5269 postfix-expression:
5270 primary-expression
5271 postfix-expression [ expression ]
5272 postfix-expression ( expression-list [opt] )
5273 simple-type-specifier ( expression-list [opt] )
5274 typename :: [opt] nested-name-specifier identifier
5275 ( expression-list [opt] )
5276 typename :: [opt] nested-name-specifier template [opt] template-id
5277 ( expression-list [opt] )
5278 postfix-expression . template [opt] id-expression
5279 postfix-expression -> template [opt] id-expression
5280 postfix-expression . pseudo-destructor-name
5281 postfix-expression -> pseudo-destructor-name
5282 postfix-expression ++
5283 postfix-expression --
5284 dynamic_cast < type-id > ( expression )
5285 static_cast < type-id > ( expression )
5286 reinterpret_cast < type-id > ( expression )
5287 const_cast < type-id > ( expression )
5288 typeid ( expression )
5289 typeid ( type-id )
5291 GNU Extension:
5293 postfix-expression:
5294 ( type-id ) { initializer-list , [opt] }
5296 This extension is a GNU version of the C99 compound-literal
5297 construct. (The C99 grammar uses `type-name' instead of `type-id',
5298 but they are essentially the same concept.)
5300 If ADDRESS_P is true, the postfix expression is the operand of the
5301 `&' operator. CAST_P is true if this expression is the target of a
5302 cast.
5304 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5305 class member access expressions [expr.ref].
5307 Returns a representation of the expression. */
5309 static tree
5310 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5311 bool member_access_only_p,
5312 cp_id_kind * pidk_return)
5314 cp_token *token;
5315 enum rid keyword;
5316 cp_id_kind idk = CP_ID_KIND_NONE;
5317 tree postfix_expression = NULL_TREE;
5318 bool is_member_access = false;
5320 /* Peek at the next token. */
5321 token = cp_lexer_peek_token (parser->lexer);
5322 /* Some of the productions are determined by keywords. */
5323 keyword = token->keyword;
5324 switch (keyword)
5326 case RID_DYNCAST:
5327 case RID_STATCAST:
5328 case RID_REINTCAST:
5329 case RID_CONSTCAST:
5331 tree type;
5332 tree expression;
5333 const char *saved_message;
5335 /* All of these can be handled in the same way from the point
5336 of view of parsing. Begin by consuming the token
5337 identifying the cast. */
5338 cp_lexer_consume_token (parser->lexer);
5340 /* New types cannot be defined in the cast. */
5341 saved_message = parser->type_definition_forbidden_message;
5342 parser->type_definition_forbidden_message
5343 = G_("types may not be defined in casts");
5345 /* Look for the opening `<'. */
5346 cp_parser_require (parser, CPP_LESS, RT_LESS);
5347 /* Parse the type to which we are casting. */
5348 type = cp_parser_type_id (parser);
5349 /* Look for the closing `>'. */
5350 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5351 /* Restore the old message. */
5352 parser->type_definition_forbidden_message = saved_message;
5354 /* And the expression which is being cast. */
5355 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5356 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5357 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5359 /* Only type conversions to integral or enumeration types
5360 can be used in constant-expressions. */
5361 if (!cast_valid_in_integral_constant_expression_p (type)
5362 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5363 return error_mark_node;
5365 switch (keyword)
5367 case RID_DYNCAST:
5368 postfix_expression
5369 = build_dynamic_cast (type, expression, tf_warning_or_error);
5370 break;
5371 case RID_STATCAST:
5372 postfix_expression
5373 = build_static_cast (type, expression, tf_warning_or_error);
5374 break;
5375 case RID_REINTCAST:
5376 postfix_expression
5377 = build_reinterpret_cast (type, expression,
5378 tf_warning_or_error);
5379 break;
5380 case RID_CONSTCAST:
5381 postfix_expression
5382 = build_const_cast (type, expression, tf_warning_or_error);
5383 break;
5384 default:
5385 gcc_unreachable ();
5388 break;
5390 case RID_TYPEID:
5392 tree type;
5393 const char *saved_message;
5394 bool saved_in_type_id_in_expr_p;
5396 /* Consume the `typeid' token. */
5397 cp_lexer_consume_token (parser->lexer);
5398 /* Look for the `(' token. */
5399 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5400 /* Types cannot be defined in a `typeid' expression. */
5401 saved_message = parser->type_definition_forbidden_message;
5402 parser->type_definition_forbidden_message
5403 = G_("types may not be defined in a %<typeid%> expression");
5404 /* We can't be sure yet whether we're looking at a type-id or an
5405 expression. */
5406 cp_parser_parse_tentatively (parser);
5407 /* Try a type-id first. */
5408 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5409 parser->in_type_id_in_expr_p = true;
5410 type = cp_parser_type_id (parser);
5411 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5412 /* Look for the `)' token. Otherwise, we can't be sure that
5413 we're not looking at an expression: consider `typeid (int
5414 (3))', for example. */
5415 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5416 /* If all went well, simply lookup the type-id. */
5417 if (cp_parser_parse_definitely (parser))
5418 postfix_expression = get_typeid (type);
5419 /* Otherwise, fall back to the expression variant. */
5420 else
5422 tree expression;
5424 /* Look for an expression. */
5425 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5426 /* Compute its typeid. */
5427 postfix_expression = build_typeid (expression);
5428 /* Look for the `)' token. */
5429 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5431 /* Restore the saved message. */
5432 parser->type_definition_forbidden_message = saved_message;
5433 /* `typeid' may not appear in an integral constant expression. */
5434 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5435 return error_mark_node;
5437 break;
5439 case RID_TYPENAME:
5441 tree type;
5442 /* The syntax permitted here is the same permitted for an
5443 elaborated-type-specifier. */
5444 type = cp_parser_elaborated_type_specifier (parser,
5445 /*is_friend=*/false,
5446 /*is_declaration=*/false);
5447 postfix_expression = cp_parser_functional_cast (parser, type);
5449 break;
5451 default:
5453 tree type;
5455 /* If the next thing is a simple-type-specifier, we may be
5456 looking at a functional cast. We could also be looking at
5457 an id-expression. So, we try the functional cast, and if
5458 that doesn't work we fall back to the primary-expression. */
5459 cp_parser_parse_tentatively (parser);
5460 /* Look for the simple-type-specifier. */
5461 type = cp_parser_simple_type_specifier (parser,
5462 /*decl_specs=*/NULL,
5463 CP_PARSER_FLAGS_NONE);
5464 /* Parse the cast itself. */
5465 if (!cp_parser_error_occurred (parser))
5466 postfix_expression
5467 = cp_parser_functional_cast (parser, type);
5468 /* If that worked, we're done. */
5469 if (cp_parser_parse_definitely (parser))
5470 break;
5472 /* If the functional-cast didn't work out, try a
5473 compound-literal. */
5474 if (cp_parser_allow_gnu_extensions_p (parser)
5475 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5477 VEC(constructor_elt,gc) *initializer_list = NULL;
5478 bool saved_in_type_id_in_expr_p;
5480 cp_parser_parse_tentatively (parser);
5481 /* Consume the `('. */
5482 cp_lexer_consume_token (parser->lexer);
5483 /* Parse the type. */
5484 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5485 parser->in_type_id_in_expr_p = true;
5486 type = cp_parser_type_id (parser);
5487 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5488 /* Look for the `)'. */
5489 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5490 /* Look for the `{'. */
5491 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5492 /* If things aren't going well, there's no need to
5493 keep going. */
5494 if (!cp_parser_error_occurred (parser))
5496 bool non_constant_p;
5497 /* Parse the initializer-list. */
5498 initializer_list
5499 = cp_parser_initializer_list (parser, &non_constant_p);
5500 /* Allow a trailing `,'. */
5501 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5502 cp_lexer_consume_token (parser->lexer);
5503 /* Look for the final `}'. */
5504 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5506 /* If that worked, we're definitely looking at a
5507 compound-literal expression. */
5508 if (cp_parser_parse_definitely (parser))
5510 /* Warn the user that a compound literal is not
5511 allowed in standard C++. */
5512 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5513 /* For simplicity, we disallow compound literals in
5514 constant-expressions. We could
5515 allow compound literals of integer type, whose
5516 initializer was a constant, in constant
5517 expressions. Permitting that usage, as a further
5518 extension, would not change the meaning of any
5519 currently accepted programs. (Of course, as
5520 compound literals are not part of ISO C++, the
5521 standard has nothing to say.) */
5522 if (cp_parser_non_integral_constant_expression (parser,
5523 NIC_NCC))
5525 postfix_expression = error_mark_node;
5526 break;
5528 /* Form the representation of the compound-literal. */
5529 postfix_expression
5530 = (finish_compound_literal
5531 (type, build_constructor (init_list_type_node,
5532 initializer_list),
5533 tf_warning_or_error));
5534 break;
5538 /* It must be a primary-expression. */
5539 postfix_expression
5540 = cp_parser_primary_expression (parser, address_p, cast_p,
5541 /*template_arg_p=*/false,
5542 &idk);
5544 break;
5547 /* Keep looping until the postfix-expression is complete. */
5548 while (true)
5550 if (idk == CP_ID_KIND_UNQUALIFIED
5551 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5552 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5553 /* It is not a Koenig lookup function call. */
5554 postfix_expression
5555 = unqualified_name_lookup_error (postfix_expression);
5557 /* Peek at the next token. */
5558 token = cp_lexer_peek_token (parser->lexer);
5560 switch (token->type)
5562 case CPP_OPEN_SQUARE:
5563 postfix_expression
5564 = cp_parser_postfix_open_square_expression (parser,
5565 postfix_expression,
5566 false);
5567 idk = CP_ID_KIND_NONE;
5568 is_member_access = false;
5569 break;
5571 case CPP_OPEN_PAREN:
5572 /* postfix-expression ( expression-list [opt] ) */
5574 bool koenig_p;
5575 bool is_builtin_constant_p;
5576 bool saved_integral_constant_expression_p = false;
5577 bool saved_non_integral_constant_expression_p = false;
5578 VEC(tree,gc) *args;
5580 is_member_access = false;
5582 is_builtin_constant_p
5583 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5584 if (is_builtin_constant_p)
5586 /* The whole point of __builtin_constant_p is to allow
5587 non-constant expressions to appear as arguments. */
5588 saved_integral_constant_expression_p
5589 = parser->integral_constant_expression_p;
5590 saved_non_integral_constant_expression_p
5591 = parser->non_integral_constant_expression_p;
5592 parser->integral_constant_expression_p = false;
5594 args = (cp_parser_parenthesized_expression_list
5595 (parser, non_attr,
5596 /*cast_p=*/false, /*allow_expansion_p=*/true,
5597 /*non_constant_p=*/NULL));
5598 if (is_builtin_constant_p)
5600 parser->integral_constant_expression_p
5601 = saved_integral_constant_expression_p;
5602 parser->non_integral_constant_expression_p
5603 = saved_non_integral_constant_expression_p;
5606 if (args == NULL)
5608 postfix_expression = error_mark_node;
5609 break;
5612 /* Function calls are not permitted in
5613 constant-expressions. */
5614 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5615 && cp_parser_non_integral_constant_expression (parser,
5616 NIC_FUNC_CALL))
5618 postfix_expression = error_mark_node;
5619 release_tree_vector (args);
5620 break;
5623 koenig_p = false;
5624 if (idk == CP_ID_KIND_UNQUALIFIED
5625 || idk == CP_ID_KIND_TEMPLATE_ID)
5627 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5629 if (!VEC_empty (tree, args))
5631 koenig_p = true;
5632 if (!any_type_dependent_arguments_p (args))
5633 postfix_expression
5634 = perform_koenig_lookup (postfix_expression, args,
5635 /*include_std=*/false,
5636 tf_warning_or_error);
5638 else
5639 postfix_expression
5640 = unqualified_fn_lookup_error (postfix_expression);
5642 /* We do not perform argument-dependent lookup if
5643 normal lookup finds a non-function, in accordance
5644 with the expected resolution of DR 218. */
5645 else if (!VEC_empty (tree, args)
5646 && is_overloaded_fn (postfix_expression))
5648 tree fn = get_first_fn (postfix_expression);
5649 fn = STRIP_TEMPLATE (fn);
5651 /* Do not do argument dependent lookup if regular
5652 lookup finds a member function or a block-scope
5653 function declaration. [basic.lookup.argdep]/3 */
5654 if (!DECL_FUNCTION_MEMBER_P (fn)
5655 && !DECL_LOCAL_FUNCTION_P (fn))
5657 koenig_p = true;
5658 if (!any_type_dependent_arguments_p (args))
5659 postfix_expression
5660 = perform_koenig_lookup (postfix_expression, args,
5661 /*include_std=*/false,
5662 tf_warning_or_error);
5667 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5669 tree instance = TREE_OPERAND (postfix_expression, 0);
5670 tree fn = TREE_OPERAND (postfix_expression, 1);
5672 if (processing_template_decl
5673 && (type_dependent_expression_p (instance)
5674 || (!BASELINK_P (fn)
5675 && TREE_CODE (fn) != FIELD_DECL)
5676 || type_dependent_expression_p (fn)
5677 || any_type_dependent_arguments_p (args)))
5679 postfix_expression
5680 = build_nt_call_vec (postfix_expression, args);
5681 release_tree_vector (args);
5682 break;
5685 if (BASELINK_P (fn))
5687 postfix_expression
5688 = (build_new_method_call
5689 (instance, fn, &args, NULL_TREE,
5690 (idk == CP_ID_KIND_QUALIFIED
5691 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5692 : LOOKUP_NORMAL),
5693 /*fn_p=*/NULL,
5694 tf_warning_or_error));
5696 else
5697 postfix_expression
5698 = finish_call_expr (postfix_expression, &args,
5699 /*disallow_virtual=*/false,
5700 /*koenig_p=*/false,
5701 tf_warning_or_error);
5703 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5704 || TREE_CODE (postfix_expression) == MEMBER_REF
5705 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5706 postfix_expression = (build_offset_ref_call_from_tree
5707 (postfix_expression, &args));
5708 else if (idk == CP_ID_KIND_QUALIFIED)
5709 /* A call to a static class member, or a namespace-scope
5710 function. */
5711 postfix_expression
5712 = finish_call_expr (postfix_expression, &args,
5713 /*disallow_virtual=*/true,
5714 koenig_p,
5715 tf_warning_or_error);
5716 else
5717 /* All other function calls. */
5718 postfix_expression
5719 = finish_call_expr (postfix_expression, &args,
5720 /*disallow_virtual=*/false,
5721 koenig_p,
5722 tf_warning_or_error);
5724 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5725 idk = CP_ID_KIND_NONE;
5727 release_tree_vector (args);
5729 break;
5731 case CPP_DOT:
5732 case CPP_DEREF:
5733 /* postfix-expression . template [opt] id-expression
5734 postfix-expression . pseudo-destructor-name
5735 postfix-expression -> template [opt] id-expression
5736 postfix-expression -> pseudo-destructor-name */
5738 /* Consume the `.' or `->' operator. */
5739 cp_lexer_consume_token (parser->lexer);
5741 postfix_expression
5742 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5743 postfix_expression,
5744 false, &idk,
5745 token->location);
5747 is_member_access = true;
5748 break;
5750 case CPP_PLUS_PLUS:
5751 /* postfix-expression ++ */
5752 /* Consume the `++' token. */
5753 cp_lexer_consume_token (parser->lexer);
5754 /* Generate a representation for the complete expression. */
5755 postfix_expression
5756 = finish_increment_expr (postfix_expression,
5757 POSTINCREMENT_EXPR);
5758 /* Increments may not appear in constant-expressions. */
5759 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5760 postfix_expression = error_mark_node;
5761 idk = CP_ID_KIND_NONE;
5762 is_member_access = false;
5763 break;
5765 case CPP_MINUS_MINUS:
5766 /* postfix-expression -- */
5767 /* Consume the `--' token. */
5768 cp_lexer_consume_token (parser->lexer);
5769 /* Generate a representation for the complete expression. */
5770 postfix_expression
5771 = finish_increment_expr (postfix_expression,
5772 POSTDECREMENT_EXPR);
5773 /* Decrements may not appear in constant-expressions. */
5774 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5775 postfix_expression = error_mark_node;
5776 idk = CP_ID_KIND_NONE;
5777 is_member_access = false;
5778 break;
5780 default:
5781 if (pidk_return != NULL)
5782 * pidk_return = idk;
5783 if (member_access_only_p)
5784 return is_member_access? postfix_expression : error_mark_node;
5785 else
5786 return postfix_expression;
5790 /* We should never get here. */
5791 gcc_unreachable ();
5792 return error_mark_node;
5795 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5796 by cp_parser_builtin_offsetof. We're looking for
5798 postfix-expression [ expression ]
5799 postfix-expression [ braced-init-list ] (C++11)
5801 FOR_OFFSETOF is set if we're being called in that context, which
5802 changes how we deal with integer constant expressions. */
5804 static tree
5805 cp_parser_postfix_open_square_expression (cp_parser *parser,
5806 tree postfix_expression,
5807 bool for_offsetof)
5809 tree index;
5810 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5812 /* Consume the `[' token. */
5813 cp_lexer_consume_token (parser->lexer);
5815 /* Parse the index expression. */
5816 /* ??? For offsetof, there is a question of what to allow here. If
5817 offsetof is not being used in an integral constant expression context,
5818 then we *could* get the right answer by computing the value at runtime.
5819 If we are in an integral constant expression context, then we might
5820 could accept any constant expression; hard to say without analysis.
5821 Rather than open the barn door too wide right away, allow only integer
5822 constant expressions here. */
5823 if (for_offsetof)
5824 index = cp_parser_constant_expression (parser, false, NULL);
5825 else
5827 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5829 bool expr_nonconst_p;
5830 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5831 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5833 else
5834 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5837 /* Look for the closing `]'. */
5838 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5840 /* Build the ARRAY_REF. */
5841 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5843 /* When not doing offsetof, array references are not permitted in
5844 constant-expressions. */
5845 if (!for_offsetof
5846 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5847 postfix_expression = error_mark_node;
5849 return postfix_expression;
5852 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5853 by cp_parser_builtin_offsetof. We're looking for
5855 postfix-expression . template [opt] id-expression
5856 postfix-expression . pseudo-destructor-name
5857 postfix-expression -> template [opt] id-expression
5858 postfix-expression -> pseudo-destructor-name
5860 FOR_OFFSETOF is set if we're being called in that context. That sorta
5861 limits what of the above we'll actually accept, but nevermind.
5862 TOKEN_TYPE is the "." or "->" token, which will already have been
5863 removed from the stream. */
5865 static tree
5866 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5867 enum cpp_ttype token_type,
5868 tree postfix_expression,
5869 bool for_offsetof, cp_id_kind *idk,
5870 location_t location)
5872 tree name;
5873 bool dependent_p;
5874 bool pseudo_destructor_p;
5875 tree scope = NULL_TREE;
5877 /* If this is a `->' operator, dereference the pointer. */
5878 if (token_type == CPP_DEREF)
5879 postfix_expression = build_x_arrow (location, postfix_expression,
5880 tf_warning_or_error);
5881 /* Check to see whether or not the expression is type-dependent. */
5882 dependent_p = type_dependent_expression_p (postfix_expression);
5883 /* The identifier following the `->' or `.' is not qualified. */
5884 parser->scope = NULL_TREE;
5885 parser->qualifying_scope = NULL_TREE;
5886 parser->object_scope = NULL_TREE;
5887 *idk = CP_ID_KIND_NONE;
5889 /* Enter the scope corresponding to the type of the object
5890 given by the POSTFIX_EXPRESSION. */
5891 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5893 scope = TREE_TYPE (postfix_expression);
5894 /* According to the standard, no expression should ever have
5895 reference type. Unfortunately, we do not currently match
5896 the standard in this respect in that our internal representation
5897 of an expression may have reference type even when the standard
5898 says it does not. Therefore, we have to manually obtain the
5899 underlying type here. */
5900 scope = non_reference (scope);
5901 /* The type of the POSTFIX_EXPRESSION must be complete. */
5902 if (scope == unknown_type_node)
5904 error_at (location, "%qE does not have class type",
5905 postfix_expression);
5906 scope = NULL_TREE;
5908 /* Unlike the object expression in other contexts, *this is not
5909 required to be of complete type for purposes of class member
5910 access (5.2.5) outside the member function body. */
5911 else if (scope != current_class_ref
5912 && !(processing_template_decl && scope == current_class_type))
5913 scope = complete_type_or_else (scope, NULL_TREE);
5914 /* Let the name lookup machinery know that we are processing a
5915 class member access expression. */
5916 parser->context->object_type = scope;
5917 /* If something went wrong, we want to be able to discern that case,
5918 as opposed to the case where there was no SCOPE due to the type
5919 of expression being dependent. */
5920 if (!scope)
5921 scope = error_mark_node;
5922 /* If the SCOPE was erroneous, make the various semantic analysis
5923 functions exit quickly -- and without issuing additional error
5924 messages. */
5925 if (scope == error_mark_node)
5926 postfix_expression = error_mark_node;
5929 /* Assume this expression is not a pseudo-destructor access. */
5930 pseudo_destructor_p = false;
5932 /* If the SCOPE is a scalar type, then, if this is a valid program,
5933 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5934 is type dependent, it can be pseudo-destructor-name or something else.
5935 Try to parse it as pseudo-destructor-name first. */
5936 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5938 tree s;
5939 tree type;
5941 cp_parser_parse_tentatively (parser);
5942 /* Parse the pseudo-destructor-name. */
5943 s = NULL_TREE;
5944 cp_parser_pseudo_destructor_name (parser, &s, &type);
5945 if (dependent_p
5946 && (cp_parser_error_occurred (parser)
5947 || TREE_CODE (type) != TYPE_DECL
5948 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5949 cp_parser_abort_tentative_parse (parser);
5950 else if (cp_parser_parse_definitely (parser))
5952 pseudo_destructor_p = true;
5953 postfix_expression
5954 = finish_pseudo_destructor_expr (postfix_expression,
5955 s, TREE_TYPE (type));
5959 if (!pseudo_destructor_p)
5961 /* If the SCOPE is not a scalar type, we are looking at an
5962 ordinary class member access expression, rather than a
5963 pseudo-destructor-name. */
5964 bool template_p;
5965 cp_token *token = cp_lexer_peek_token (parser->lexer);
5966 /* Parse the id-expression. */
5967 name = (cp_parser_id_expression
5968 (parser,
5969 cp_parser_optional_template_keyword (parser),
5970 /*check_dependency_p=*/true,
5971 &template_p,
5972 /*declarator_p=*/false,
5973 /*optional_p=*/false));
5974 /* In general, build a SCOPE_REF if the member name is qualified.
5975 However, if the name was not dependent and has already been
5976 resolved; there is no need to build the SCOPE_REF. For example;
5978 struct X { void f(); };
5979 template <typename T> void f(T* t) { t->X::f(); }
5981 Even though "t" is dependent, "X::f" is not and has been resolved
5982 to a BASELINK; there is no need to include scope information. */
5984 /* But we do need to remember that there was an explicit scope for
5985 virtual function calls. */
5986 if (parser->scope)
5987 *idk = CP_ID_KIND_QUALIFIED;
5989 /* If the name is a template-id that names a type, we will get a
5990 TYPE_DECL here. That is invalid code. */
5991 if (TREE_CODE (name) == TYPE_DECL)
5993 error_at (token->location, "invalid use of %qD", name);
5994 postfix_expression = error_mark_node;
5996 else
5998 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6000 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6002 error_at (token->location, "%<%D::%D%> is not a class member",
6003 parser->scope, name);
6004 postfix_expression = error_mark_node;
6006 else
6007 name = build_qualified_name (/*type=*/NULL_TREE,
6008 parser->scope,
6009 name,
6010 template_p);
6011 parser->scope = NULL_TREE;
6012 parser->qualifying_scope = NULL_TREE;
6013 parser->object_scope = NULL_TREE;
6015 if (parser->scope && name && BASELINK_P (name))
6016 adjust_result_of_qualified_name_lookup
6017 (name, parser->scope, scope);
6018 postfix_expression
6019 = finish_class_member_access_expr (postfix_expression, name,
6020 template_p,
6021 tf_warning_or_error);
6025 /* We no longer need to look up names in the scope of the object on
6026 the left-hand side of the `.' or `->' operator. */
6027 parser->context->object_type = NULL_TREE;
6029 /* Outside of offsetof, these operators may not appear in
6030 constant-expressions. */
6031 if (!for_offsetof
6032 && (cp_parser_non_integral_constant_expression
6033 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6034 postfix_expression = error_mark_node;
6036 return postfix_expression;
6039 /* Parse a parenthesized expression-list.
6041 expression-list:
6042 assignment-expression
6043 expression-list, assignment-expression
6045 attribute-list:
6046 expression-list
6047 identifier
6048 identifier, expression-list
6050 CAST_P is true if this expression is the target of a cast.
6052 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6053 argument pack.
6055 Returns a vector of trees. Each element is a representation of an
6056 assignment-expression. NULL is returned if the ( and or ) are
6057 missing. An empty, but allocated, vector is returned on no
6058 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6059 if we are parsing an attribute list for an attribute that wants a
6060 plain identifier argument, normal_attr for an attribute that wants
6061 an expression, or non_attr if we aren't parsing an attribute list. If
6062 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6063 not all of the expressions in the list were constant. */
6065 static VEC(tree,gc) *
6066 cp_parser_parenthesized_expression_list (cp_parser* parser,
6067 int is_attribute_list,
6068 bool cast_p,
6069 bool allow_expansion_p,
6070 bool *non_constant_p)
6072 VEC(tree,gc) *expression_list;
6073 bool fold_expr_p = is_attribute_list != non_attr;
6074 tree identifier = NULL_TREE;
6075 bool saved_greater_than_is_operator_p;
6077 /* Assume all the expressions will be constant. */
6078 if (non_constant_p)
6079 *non_constant_p = false;
6081 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6082 return NULL;
6084 expression_list = make_tree_vector ();
6086 /* Within a parenthesized expression, a `>' token is always
6087 the greater-than operator. */
6088 saved_greater_than_is_operator_p
6089 = parser->greater_than_is_operator_p;
6090 parser->greater_than_is_operator_p = true;
6092 /* Consume expressions until there are no more. */
6093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6094 while (true)
6096 tree expr;
6098 /* At the beginning of attribute lists, check to see if the
6099 next token is an identifier. */
6100 if (is_attribute_list == id_attr
6101 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6103 cp_token *token;
6105 /* Consume the identifier. */
6106 token = cp_lexer_consume_token (parser->lexer);
6107 /* Save the identifier. */
6108 identifier = token->u.value;
6110 else
6112 bool expr_non_constant_p;
6114 /* Parse the next assignment-expression. */
6115 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6117 /* A braced-init-list. */
6118 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6119 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6120 if (non_constant_p && expr_non_constant_p)
6121 *non_constant_p = true;
6123 else if (non_constant_p)
6125 expr = (cp_parser_constant_expression
6126 (parser, /*allow_non_constant_p=*/true,
6127 &expr_non_constant_p));
6128 if (expr_non_constant_p)
6129 *non_constant_p = true;
6131 else
6132 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6134 if (fold_expr_p)
6135 expr = fold_non_dependent_expr (expr);
6137 /* If we have an ellipsis, then this is an expression
6138 expansion. */
6139 if (allow_expansion_p
6140 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6142 /* Consume the `...'. */
6143 cp_lexer_consume_token (parser->lexer);
6145 /* Build the argument pack. */
6146 expr = make_pack_expansion (expr);
6149 /* Add it to the list. We add error_mark_node
6150 expressions to the list, so that we can still tell if
6151 the correct form for a parenthesized expression-list
6152 is found. That gives better errors. */
6153 VEC_safe_push (tree, gc, expression_list, expr);
6155 if (expr == error_mark_node)
6156 goto skip_comma;
6159 /* After the first item, attribute lists look the same as
6160 expression lists. */
6161 is_attribute_list = non_attr;
6163 get_comma:;
6164 /* If the next token isn't a `,', then we are done. */
6165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6166 break;
6168 /* Otherwise, consume the `,' and keep going. */
6169 cp_lexer_consume_token (parser->lexer);
6172 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6174 int ending;
6176 skip_comma:;
6177 /* We try and resync to an unnested comma, as that will give the
6178 user better diagnostics. */
6179 ending = cp_parser_skip_to_closing_parenthesis (parser,
6180 /*recovering=*/true,
6181 /*or_comma=*/true,
6182 /*consume_paren=*/true);
6183 if (ending < 0)
6184 goto get_comma;
6185 if (!ending)
6187 parser->greater_than_is_operator_p
6188 = saved_greater_than_is_operator_p;
6189 return NULL;
6193 parser->greater_than_is_operator_p
6194 = saved_greater_than_is_operator_p;
6196 if (identifier)
6197 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6199 return expression_list;
6202 /* Parse a pseudo-destructor-name.
6204 pseudo-destructor-name:
6205 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6206 :: [opt] nested-name-specifier template template-id :: ~ type-name
6207 :: [opt] nested-name-specifier [opt] ~ type-name
6209 If either of the first two productions is used, sets *SCOPE to the
6210 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6211 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6212 or ERROR_MARK_NODE if the parse fails. */
6214 static void
6215 cp_parser_pseudo_destructor_name (cp_parser* parser,
6216 tree* scope,
6217 tree* type)
6219 bool nested_name_specifier_p;
6221 /* Assume that things will not work out. */
6222 *type = error_mark_node;
6224 /* Look for the optional `::' operator. */
6225 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6226 /* Look for the optional nested-name-specifier. */
6227 nested_name_specifier_p
6228 = (cp_parser_nested_name_specifier_opt (parser,
6229 /*typename_keyword_p=*/false,
6230 /*check_dependency_p=*/true,
6231 /*type_p=*/false,
6232 /*is_declaration=*/false)
6233 != NULL_TREE);
6234 /* Now, if we saw a nested-name-specifier, we might be doing the
6235 second production. */
6236 if (nested_name_specifier_p
6237 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6239 /* Consume the `template' keyword. */
6240 cp_lexer_consume_token (parser->lexer);
6241 /* Parse the template-id. */
6242 cp_parser_template_id (parser,
6243 /*template_keyword_p=*/true,
6244 /*check_dependency_p=*/false,
6245 /*is_declaration=*/true);
6246 /* Look for the `::' token. */
6247 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6249 /* If the next token is not a `~', then there might be some
6250 additional qualification. */
6251 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6253 /* At this point, we're looking for "type-name :: ~". The type-name
6254 must not be a class-name, since this is a pseudo-destructor. So,
6255 it must be either an enum-name, or a typedef-name -- both of which
6256 are just identifiers. So, we peek ahead to check that the "::"
6257 and "~" tokens are present; if they are not, then we can avoid
6258 calling type_name. */
6259 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6260 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6261 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6263 cp_parser_error (parser, "non-scalar type");
6264 return;
6267 /* Look for the type-name. */
6268 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6269 if (*scope == error_mark_node)
6270 return;
6272 /* Look for the `::' token. */
6273 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6275 else
6276 *scope = NULL_TREE;
6278 /* Look for the `~'. */
6279 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6281 /* Once we see the ~, this has to be a pseudo-destructor. */
6282 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6283 cp_parser_commit_to_tentative_parse (parser);
6285 /* Look for the type-name again. We are not responsible for
6286 checking that it matches the first type-name. */
6287 *type = cp_parser_nonclass_name (parser);
6290 /* Parse a unary-expression.
6292 unary-expression:
6293 postfix-expression
6294 ++ cast-expression
6295 -- cast-expression
6296 unary-operator cast-expression
6297 sizeof unary-expression
6298 sizeof ( type-id )
6299 alignof ( type-id ) [C++0x]
6300 new-expression
6301 delete-expression
6303 GNU Extensions:
6305 unary-expression:
6306 __extension__ cast-expression
6307 __alignof__ unary-expression
6308 __alignof__ ( type-id )
6309 alignof unary-expression [C++0x]
6310 __real__ cast-expression
6311 __imag__ cast-expression
6312 && identifier
6314 ADDRESS_P is true iff the unary-expression is appearing as the
6315 operand of the `&' operator. CAST_P is true if this expression is
6316 the target of a cast.
6318 Returns a representation of the expression. */
6320 static tree
6321 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6322 cp_id_kind * pidk)
6324 cp_token *token;
6325 enum tree_code unary_operator;
6327 /* Peek at the next token. */
6328 token = cp_lexer_peek_token (parser->lexer);
6329 /* Some keywords give away the kind of expression. */
6330 if (token->type == CPP_KEYWORD)
6332 enum rid keyword = token->keyword;
6334 switch (keyword)
6336 case RID_ALIGNOF:
6337 case RID_SIZEOF:
6339 tree operand;
6340 enum tree_code op;
6342 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6343 /* Consume the token. */
6344 cp_lexer_consume_token (parser->lexer);
6345 /* Parse the operand. */
6346 operand = cp_parser_sizeof_operand (parser, keyword);
6348 if (TYPE_P (operand))
6349 return cxx_sizeof_or_alignof_type (operand, op, true);
6350 else
6352 /* ISO C++ defines alignof only with types, not with
6353 expressions. So pedwarn if alignof is used with a non-
6354 type expression. However, __alignof__ is ok. */
6355 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6356 pedwarn (token->location, OPT_Wpedantic,
6357 "ISO C++ does not allow %<alignof%> "
6358 "with a non-type");
6360 return cxx_sizeof_or_alignof_expr (operand, op, true);
6364 case RID_NEW:
6365 return cp_parser_new_expression (parser);
6367 case RID_DELETE:
6368 return cp_parser_delete_expression (parser);
6370 case RID_EXTENSION:
6372 /* The saved value of the PEDANTIC flag. */
6373 int saved_pedantic;
6374 tree expr;
6376 /* Save away the PEDANTIC flag. */
6377 cp_parser_extension_opt (parser, &saved_pedantic);
6378 /* Parse the cast-expression. */
6379 expr = cp_parser_simple_cast_expression (parser);
6380 /* Restore the PEDANTIC flag. */
6381 pedantic = saved_pedantic;
6383 return expr;
6386 case RID_REALPART:
6387 case RID_IMAGPART:
6389 tree expression;
6391 /* Consume the `__real__' or `__imag__' token. */
6392 cp_lexer_consume_token (parser->lexer);
6393 /* Parse the cast-expression. */
6394 expression = cp_parser_simple_cast_expression (parser);
6395 /* Create the complete representation. */
6396 return build_x_unary_op (token->location,
6397 (keyword == RID_REALPART
6398 ? REALPART_EXPR : IMAGPART_EXPR),
6399 expression,
6400 tf_warning_or_error);
6402 break;
6404 case RID_TRANSACTION_ATOMIC:
6405 case RID_TRANSACTION_RELAXED:
6406 return cp_parser_transaction_expression (parser, keyword);
6408 case RID_NOEXCEPT:
6410 tree expr;
6411 const char *saved_message;
6412 bool saved_integral_constant_expression_p;
6413 bool saved_non_integral_constant_expression_p;
6414 bool saved_greater_than_is_operator_p;
6416 cp_lexer_consume_token (parser->lexer);
6417 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6419 saved_message = parser->type_definition_forbidden_message;
6420 parser->type_definition_forbidden_message
6421 = G_("types may not be defined in %<noexcept%> expressions");
6423 saved_integral_constant_expression_p
6424 = parser->integral_constant_expression_p;
6425 saved_non_integral_constant_expression_p
6426 = parser->non_integral_constant_expression_p;
6427 parser->integral_constant_expression_p = false;
6429 saved_greater_than_is_operator_p
6430 = parser->greater_than_is_operator_p;
6431 parser->greater_than_is_operator_p = true;
6433 ++cp_unevaluated_operand;
6434 ++c_inhibit_evaluation_warnings;
6435 expr = cp_parser_expression (parser, false, NULL);
6436 --c_inhibit_evaluation_warnings;
6437 --cp_unevaluated_operand;
6439 parser->greater_than_is_operator_p
6440 = saved_greater_than_is_operator_p;
6442 parser->integral_constant_expression_p
6443 = saved_integral_constant_expression_p;
6444 parser->non_integral_constant_expression_p
6445 = saved_non_integral_constant_expression_p;
6447 parser->type_definition_forbidden_message = saved_message;
6449 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6450 return finish_noexcept_expr (expr, tf_warning_or_error);
6453 default:
6454 break;
6458 /* Look for the `:: new' and `:: delete', which also signal the
6459 beginning of a new-expression, or delete-expression,
6460 respectively. If the next token is `::', then it might be one of
6461 these. */
6462 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6464 enum rid keyword;
6466 /* See if the token after the `::' is one of the keywords in
6467 which we're interested. */
6468 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6469 /* If it's `new', we have a new-expression. */
6470 if (keyword == RID_NEW)
6471 return cp_parser_new_expression (parser);
6472 /* Similarly, for `delete'. */
6473 else if (keyword == RID_DELETE)
6474 return cp_parser_delete_expression (parser);
6477 /* Look for a unary operator. */
6478 unary_operator = cp_parser_unary_operator (token);
6479 /* The `++' and `--' operators can be handled similarly, even though
6480 they are not technically unary-operators in the grammar. */
6481 if (unary_operator == ERROR_MARK)
6483 if (token->type == CPP_PLUS_PLUS)
6484 unary_operator = PREINCREMENT_EXPR;
6485 else if (token->type == CPP_MINUS_MINUS)
6486 unary_operator = PREDECREMENT_EXPR;
6487 /* Handle the GNU address-of-label extension. */
6488 else if (cp_parser_allow_gnu_extensions_p (parser)
6489 && token->type == CPP_AND_AND)
6491 tree identifier;
6492 tree expression;
6493 location_t loc = token->location;
6495 /* Consume the '&&' token. */
6496 cp_lexer_consume_token (parser->lexer);
6497 /* Look for the identifier. */
6498 identifier = cp_parser_identifier (parser);
6499 /* Create an expression representing the address. */
6500 expression = finish_label_address_expr (identifier, loc);
6501 if (cp_parser_non_integral_constant_expression (parser,
6502 NIC_ADDR_LABEL))
6503 expression = error_mark_node;
6504 return expression;
6507 if (unary_operator != ERROR_MARK)
6509 tree cast_expression;
6510 tree expression = error_mark_node;
6511 non_integral_constant non_constant_p = NIC_NONE;
6512 location_t loc = token->location;
6514 /* Consume the operator token. */
6515 token = cp_lexer_consume_token (parser->lexer);
6516 /* Parse the cast-expression. */
6517 cast_expression
6518 = cp_parser_cast_expression (parser,
6519 unary_operator == ADDR_EXPR,
6520 /*cast_p=*/false, pidk);
6521 /* Now, build an appropriate representation. */
6522 switch (unary_operator)
6524 case INDIRECT_REF:
6525 non_constant_p = NIC_STAR;
6526 expression = build_x_indirect_ref (loc, cast_expression,
6527 RO_UNARY_STAR,
6528 tf_warning_or_error);
6529 break;
6531 case ADDR_EXPR:
6532 non_constant_p = NIC_ADDR;
6533 /* Fall through. */
6534 case BIT_NOT_EXPR:
6535 expression = build_x_unary_op (loc, unary_operator,
6536 cast_expression,
6537 tf_warning_or_error);
6538 break;
6540 case PREINCREMENT_EXPR:
6541 case PREDECREMENT_EXPR:
6542 non_constant_p = unary_operator == PREINCREMENT_EXPR
6543 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6544 /* Fall through. */
6545 case UNARY_PLUS_EXPR:
6546 case NEGATE_EXPR:
6547 case TRUTH_NOT_EXPR:
6548 expression = finish_unary_op_expr (loc, unary_operator,
6549 cast_expression);
6550 break;
6552 default:
6553 gcc_unreachable ();
6556 if (non_constant_p != NIC_NONE
6557 && cp_parser_non_integral_constant_expression (parser,
6558 non_constant_p))
6559 expression = error_mark_node;
6561 return expression;
6564 return cp_parser_postfix_expression (parser, address_p, cast_p,
6565 /*member_access_only_p=*/false,
6566 pidk);
6569 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6570 unary-operator, the corresponding tree code is returned. */
6572 static enum tree_code
6573 cp_parser_unary_operator (cp_token* token)
6575 switch (token->type)
6577 case CPP_MULT:
6578 return INDIRECT_REF;
6580 case CPP_AND:
6581 return ADDR_EXPR;
6583 case CPP_PLUS:
6584 return UNARY_PLUS_EXPR;
6586 case CPP_MINUS:
6587 return NEGATE_EXPR;
6589 case CPP_NOT:
6590 return TRUTH_NOT_EXPR;
6592 case CPP_COMPL:
6593 return BIT_NOT_EXPR;
6595 default:
6596 return ERROR_MARK;
6600 /* Parse a new-expression.
6602 new-expression:
6603 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6604 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6606 Returns a representation of the expression. */
6608 static tree
6609 cp_parser_new_expression (cp_parser* parser)
6611 bool global_scope_p;
6612 VEC(tree,gc) *placement;
6613 tree type;
6614 VEC(tree,gc) *initializer;
6615 tree nelts = NULL_TREE;
6616 tree ret;
6618 /* Look for the optional `::' operator. */
6619 global_scope_p
6620 = (cp_parser_global_scope_opt (parser,
6621 /*current_scope_valid_p=*/false)
6622 != NULL_TREE);
6623 /* Look for the `new' operator. */
6624 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6625 /* There's no easy way to tell a new-placement from the
6626 `( type-id )' construct. */
6627 cp_parser_parse_tentatively (parser);
6628 /* Look for a new-placement. */
6629 placement = cp_parser_new_placement (parser);
6630 /* If that didn't work out, there's no new-placement. */
6631 if (!cp_parser_parse_definitely (parser))
6633 if (placement != NULL)
6634 release_tree_vector (placement);
6635 placement = NULL;
6638 /* If the next token is a `(', then we have a parenthesized
6639 type-id. */
6640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6642 cp_token *token;
6643 const char *saved_message = parser->type_definition_forbidden_message;
6645 /* Consume the `('. */
6646 cp_lexer_consume_token (parser->lexer);
6648 /* Parse the type-id. */
6649 parser->type_definition_forbidden_message
6650 = G_("types may not be defined in a new-expression");
6651 type = cp_parser_type_id (parser);
6652 parser->type_definition_forbidden_message = saved_message;
6654 /* Look for the closing `)'. */
6655 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6656 token = cp_lexer_peek_token (parser->lexer);
6657 /* There should not be a direct-new-declarator in this production,
6658 but GCC used to allowed this, so we check and emit a sensible error
6659 message for this case. */
6660 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6662 error_at (token->location,
6663 "array bound forbidden after parenthesized type-id");
6664 inform (token->location,
6665 "try removing the parentheses around the type-id");
6666 cp_parser_direct_new_declarator (parser);
6669 /* Otherwise, there must be a new-type-id. */
6670 else
6671 type = cp_parser_new_type_id (parser, &nelts);
6673 /* If the next token is a `(' or '{', then we have a new-initializer. */
6674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6675 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6676 initializer = cp_parser_new_initializer (parser);
6677 else
6678 initializer = NULL;
6680 /* A new-expression may not appear in an integral constant
6681 expression. */
6682 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6683 ret = error_mark_node;
6684 else
6686 /* Create a representation of the new-expression. */
6687 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6688 tf_warning_or_error);
6691 if (placement != NULL)
6692 release_tree_vector (placement);
6693 if (initializer != NULL)
6694 release_tree_vector (initializer);
6696 return ret;
6699 /* Parse a new-placement.
6701 new-placement:
6702 ( expression-list )
6704 Returns the same representation as for an expression-list. */
6706 static VEC(tree,gc) *
6707 cp_parser_new_placement (cp_parser* parser)
6709 VEC(tree,gc) *expression_list;
6711 /* Parse the expression-list. */
6712 expression_list = (cp_parser_parenthesized_expression_list
6713 (parser, non_attr, /*cast_p=*/false,
6714 /*allow_expansion_p=*/true,
6715 /*non_constant_p=*/NULL));
6717 return expression_list;
6720 /* Parse a new-type-id.
6722 new-type-id:
6723 type-specifier-seq new-declarator [opt]
6725 Returns the TYPE allocated. If the new-type-id indicates an array
6726 type, *NELTS is set to the number of elements in the last array
6727 bound; the TYPE will not include the last array bound. */
6729 static tree
6730 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6732 cp_decl_specifier_seq type_specifier_seq;
6733 cp_declarator *new_declarator;
6734 cp_declarator *declarator;
6735 cp_declarator *outer_declarator;
6736 const char *saved_message;
6738 /* The type-specifier sequence must not contain type definitions.
6739 (It cannot contain declarations of new types either, but if they
6740 are not definitions we will catch that because they are not
6741 complete.) */
6742 saved_message = parser->type_definition_forbidden_message;
6743 parser->type_definition_forbidden_message
6744 = G_("types may not be defined in a new-type-id");
6745 /* Parse the type-specifier-seq. */
6746 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6747 /*is_trailing_return=*/false,
6748 &type_specifier_seq);
6749 /* Restore the old message. */
6750 parser->type_definition_forbidden_message = saved_message;
6752 if (type_specifier_seq.type == error_mark_node)
6753 return error_mark_node;
6755 /* Parse the new-declarator. */
6756 new_declarator = cp_parser_new_declarator_opt (parser);
6758 /* Determine the number of elements in the last array dimension, if
6759 any. */
6760 *nelts = NULL_TREE;
6761 /* Skip down to the last array dimension. */
6762 declarator = new_declarator;
6763 outer_declarator = NULL;
6764 while (declarator && (declarator->kind == cdk_pointer
6765 || declarator->kind == cdk_ptrmem))
6767 outer_declarator = declarator;
6768 declarator = declarator->declarator;
6770 while (declarator
6771 && declarator->kind == cdk_array
6772 && declarator->declarator
6773 && declarator->declarator->kind == cdk_array)
6775 outer_declarator = declarator;
6776 declarator = declarator->declarator;
6779 if (declarator && declarator->kind == cdk_array)
6781 *nelts = declarator->u.array.bounds;
6782 if (*nelts == error_mark_node)
6783 *nelts = integer_one_node;
6785 if (outer_declarator)
6786 outer_declarator->declarator = declarator->declarator;
6787 else
6788 new_declarator = NULL;
6791 return groktypename (&type_specifier_seq, new_declarator, false);
6794 /* Parse an (optional) new-declarator.
6796 new-declarator:
6797 ptr-operator new-declarator [opt]
6798 direct-new-declarator
6800 Returns the declarator. */
6802 static cp_declarator *
6803 cp_parser_new_declarator_opt (cp_parser* parser)
6805 enum tree_code code;
6806 tree type;
6807 cp_cv_quals cv_quals;
6809 /* We don't know if there's a ptr-operator next, or not. */
6810 cp_parser_parse_tentatively (parser);
6811 /* Look for a ptr-operator. */
6812 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6813 /* If that worked, look for more new-declarators. */
6814 if (cp_parser_parse_definitely (parser))
6816 cp_declarator *declarator;
6818 /* Parse another optional declarator. */
6819 declarator = cp_parser_new_declarator_opt (parser);
6821 return cp_parser_make_indirect_declarator
6822 (code, type, cv_quals, declarator);
6825 /* If the next token is a `[', there is a direct-new-declarator. */
6826 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6827 return cp_parser_direct_new_declarator (parser);
6829 return NULL;
6832 /* Parse a direct-new-declarator.
6834 direct-new-declarator:
6835 [ expression ]
6836 direct-new-declarator [constant-expression]
6840 static cp_declarator *
6841 cp_parser_direct_new_declarator (cp_parser* parser)
6843 cp_declarator *declarator = NULL;
6845 while (true)
6847 tree expression;
6849 /* Look for the opening `['. */
6850 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6851 /* The first expression is not required to be constant. */
6852 if (!declarator)
6854 cp_token *token = cp_lexer_peek_token (parser->lexer);
6855 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6856 /* The standard requires that the expression have integral
6857 type. DR 74 adds enumeration types. We believe that the
6858 real intent is that these expressions be handled like the
6859 expression in a `switch' condition, which also allows
6860 classes with a single conversion to integral or
6861 enumeration type. */
6862 if (!processing_template_decl)
6864 expression
6865 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6866 expression,
6867 /*complain=*/true);
6868 if (!expression)
6870 error_at (token->location,
6871 "expression in new-declarator must have integral "
6872 "or enumeration type");
6873 expression = error_mark_node;
6877 /* But all the other expressions must be. */
6878 else
6879 expression
6880 = cp_parser_constant_expression (parser,
6881 /*allow_non_constant=*/false,
6882 NULL);
6883 /* Look for the closing `]'. */
6884 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6886 /* Add this bound to the declarator. */
6887 declarator = make_array_declarator (declarator, expression);
6889 /* If the next token is not a `[', then there are no more
6890 bounds. */
6891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6892 break;
6895 return declarator;
6898 /* Parse a new-initializer.
6900 new-initializer:
6901 ( expression-list [opt] )
6902 braced-init-list
6904 Returns a representation of the expression-list. */
6906 static VEC(tree,gc) *
6907 cp_parser_new_initializer (cp_parser* parser)
6909 VEC(tree,gc) *expression_list;
6911 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6913 tree t;
6914 bool expr_non_constant_p;
6915 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6916 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6917 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6918 expression_list = make_tree_vector_single (t);
6920 else
6921 expression_list = (cp_parser_parenthesized_expression_list
6922 (parser, non_attr, /*cast_p=*/false,
6923 /*allow_expansion_p=*/true,
6924 /*non_constant_p=*/NULL));
6926 return expression_list;
6929 /* Parse a delete-expression.
6931 delete-expression:
6932 :: [opt] delete cast-expression
6933 :: [opt] delete [ ] cast-expression
6935 Returns a representation of the expression. */
6937 static tree
6938 cp_parser_delete_expression (cp_parser* parser)
6940 bool global_scope_p;
6941 bool array_p;
6942 tree expression;
6944 /* Look for the optional `::' operator. */
6945 global_scope_p
6946 = (cp_parser_global_scope_opt (parser,
6947 /*current_scope_valid_p=*/false)
6948 != NULL_TREE);
6949 /* Look for the `delete' keyword. */
6950 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6951 /* See if the array syntax is in use. */
6952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6954 /* Consume the `[' token. */
6955 cp_lexer_consume_token (parser->lexer);
6956 /* Look for the `]' token. */
6957 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6958 /* Remember that this is the `[]' construct. */
6959 array_p = true;
6961 else
6962 array_p = false;
6964 /* Parse the cast-expression. */
6965 expression = cp_parser_simple_cast_expression (parser);
6967 /* A delete-expression may not appear in an integral constant
6968 expression. */
6969 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6970 return error_mark_node;
6972 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6973 tf_warning_or_error);
6976 /* Returns true if TOKEN may start a cast-expression and false
6977 otherwise. */
6979 static bool
6980 cp_parser_token_starts_cast_expression (cp_token *token)
6982 switch (token->type)
6984 case CPP_COMMA:
6985 case CPP_SEMICOLON:
6986 case CPP_QUERY:
6987 case CPP_COLON:
6988 case CPP_CLOSE_SQUARE:
6989 case CPP_CLOSE_PAREN:
6990 case CPP_CLOSE_BRACE:
6991 case CPP_DOT:
6992 case CPP_DOT_STAR:
6993 case CPP_DEREF:
6994 case CPP_DEREF_STAR:
6995 case CPP_DIV:
6996 case CPP_MOD:
6997 case CPP_LSHIFT:
6998 case CPP_RSHIFT:
6999 case CPP_LESS:
7000 case CPP_GREATER:
7001 case CPP_LESS_EQ:
7002 case CPP_GREATER_EQ:
7003 case CPP_EQ_EQ:
7004 case CPP_NOT_EQ:
7005 case CPP_EQ:
7006 case CPP_MULT_EQ:
7007 case CPP_DIV_EQ:
7008 case CPP_MOD_EQ:
7009 case CPP_PLUS_EQ:
7010 case CPP_MINUS_EQ:
7011 case CPP_RSHIFT_EQ:
7012 case CPP_LSHIFT_EQ:
7013 case CPP_AND_EQ:
7014 case CPP_XOR_EQ:
7015 case CPP_OR_EQ:
7016 case CPP_XOR:
7017 case CPP_OR:
7018 case CPP_OR_OR:
7019 case CPP_EOF:
7020 return false;
7022 /* '[' may start a primary-expression in obj-c++. */
7023 case CPP_OPEN_SQUARE:
7024 return c_dialect_objc ();
7026 default:
7027 return true;
7031 /* Parse a cast-expression.
7033 cast-expression:
7034 unary-expression
7035 ( type-id ) cast-expression
7037 ADDRESS_P is true iff the unary-expression is appearing as the
7038 operand of the `&' operator. CAST_P is true if this expression is
7039 the target of a cast.
7041 Returns a representation of the expression. */
7043 static tree
7044 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7045 cp_id_kind * pidk)
7047 /* If it's a `(', then we might be looking at a cast. */
7048 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7050 tree type = NULL_TREE;
7051 tree expr = NULL_TREE;
7052 bool compound_literal_p;
7053 const char *saved_message;
7055 /* There's no way to know yet whether or not this is a cast.
7056 For example, `(int (3))' is a unary-expression, while `(int)
7057 3' is a cast. So, we resort to parsing tentatively. */
7058 cp_parser_parse_tentatively (parser);
7059 /* Types may not be defined in a cast. */
7060 saved_message = parser->type_definition_forbidden_message;
7061 parser->type_definition_forbidden_message
7062 = G_("types may not be defined in casts");
7063 /* Consume the `('. */
7064 cp_lexer_consume_token (parser->lexer);
7065 /* A very tricky bit is that `(struct S) { 3 }' is a
7066 compound-literal (which we permit in C++ as an extension).
7067 But, that construct is not a cast-expression -- it is a
7068 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7069 is legal; if the compound-literal were a cast-expression,
7070 you'd need an extra set of parentheses.) But, if we parse
7071 the type-id, and it happens to be a class-specifier, then we
7072 will commit to the parse at that point, because we cannot
7073 undo the action that is done when creating a new class. So,
7074 then we cannot back up and do a postfix-expression.
7076 Therefore, we scan ahead to the closing `)', and check to see
7077 if the token after the `)' is a `{'. If so, we are not
7078 looking at a cast-expression.
7080 Save tokens so that we can put them back. */
7081 cp_lexer_save_tokens (parser->lexer);
7082 /* Skip tokens until the next token is a closing parenthesis.
7083 If we find the closing `)', and the next token is a `{', then
7084 we are looking at a compound-literal. */
7085 compound_literal_p
7086 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7087 /*consume_paren=*/true)
7088 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7089 /* Roll back the tokens we skipped. */
7090 cp_lexer_rollback_tokens (parser->lexer);
7091 /* If we were looking at a compound-literal, simulate an error
7092 so that the call to cp_parser_parse_definitely below will
7093 fail. */
7094 if (compound_literal_p)
7095 cp_parser_simulate_error (parser);
7096 else
7098 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7099 parser->in_type_id_in_expr_p = true;
7100 /* Look for the type-id. */
7101 type = cp_parser_type_id (parser);
7102 /* Look for the closing `)'. */
7103 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7104 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7107 /* Restore the saved message. */
7108 parser->type_definition_forbidden_message = saved_message;
7110 /* At this point this can only be either a cast or a
7111 parenthesized ctor such as `(T ())' that looks like a cast to
7112 function returning T. */
7113 if (!cp_parser_error_occurred (parser)
7114 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7115 (parser->lexer)))
7117 cp_parser_parse_definitely (parser);
7118 expr = cp_parser_cast_expression (parser,
7119 /*address_p=*/false,
7120 /*cast_p=*/true, pidk);
7122 /* Warn about old-style casts, if so requested. */
7123 if (warn_old_style_cast
7124 && !in_system_header
7125 && !VOID_TYPE_P (type)
7126 && current_lang_name != lang_name_c)
7127 warning (OPT_Wold_style_cast, "use of old-style cast");
7129 /* Only type conversions to integral or enumeration types
7130 can be used in constant-expressions. */
7131 if (!cast_valid_in_integral_constant_expression_p (type)
7132 && cp_parser_non_integral_constant_expression (parser,
7133 NIC_CAST))
7134 return error_mark_node;
7136 /* Perform the cast. */
7137 expr = build_c_cast (input_location, type, expr);
7138 return expr;
7140 else
7141 cp_parser_abort_tentative_parse (parser);
7144 /* If we get here, then it's not a cast, so it must be a
7145 unary-expression. */
7146 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7149 /* Parse a binary expression of the general form:
7151 pm-expression:
7152 cast-expression
7153 pm-expression .* cast-expression
7154 pm-expression ->* cast-expression
7156 multiplicative-expression:
7157 pm-expression
7158 multiplicative-expression * pm-expression
7159 multiplicative-expression / pm-expression
7160 multiplicative-expression % pm-expression
7162 additive-expression:
7163 multiplicative-expression
7164 additive-expression + multiplicative-expression
7165 additive-expression - multiplicative-expression
7167 shift-expression:
7168 additive-expression
7169 shift-expression << additive-expression
7170 shift-expression >> additive-expression
7172 relational-expression:
7173 shift-expression
7174 relational-expression < shift-expression
7175 relational-expression > shift-expression
7176 relational-expression <= shift-expression
7177 relational-expression >= shift-expression
7179 GNU Extension:
7181 relational-expression:
7182 relational-expression <? shift-expression
7183 relational-expression >? shift-expression
7185 equality-expression:
7186 relational-expression
7187 equality-expression == relational-expression
7188 equality-expression != relational-expression
7190 and-expression:
7191 equality-expression
7192 and-expression & equality-expression
7194 exclusive-or-expression:
7195 and-expression
7196 exclusive-or-expression ^ and-expression
7198 inclusive-or-expression:
7199 exclusive-or-expression
7200 inclusive-or-expression | exclusive-or-expression
7202 logical-and-expression:
7203 inclusive-or-expression
7204 logical-and-expression && inclusive-or-expression
7206 logical-or-expression:
7207 logical-and-expression
7208 logical-or-expression || logical-and-expression
7210 All these are implemented with a single function like:
7212 binary-expression:
7213 simple-cast-expression
7214 binary-expression <token> binary-expression
7216 CAST_P is true if this expression is the target of a cast.
7218 The binops_by_token map is used to get the tree codes for each <token> type.
7219 binary-expressions are associated according to a precedence table. */
7221 #define TOKEN_PRECEDENCE(token) \
7222 (((token->type == CPP_GREATER \
7223 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7224 && !parser->greater_than_is_operator_p) \
7225 ? PREC_NOT_OPERATOR \
7226 : binops_by_token[token->type].prec)
7228 static tree
7229 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7230 bool no_toplevel_fold_p,
7231 enum cp_parser_prec prec,
7232 cp_id_kind * pidk)
7234 cp_parser_expression_stack stack;
7235 cp_parser_expression_stack_entry *sp = &stack[0];
7236 cp_parser_expression_stack_entry current;
7237 tree rhs;
7238 cp_token *token;
7239 enum tree_code rhs_type;
7240 enum cp_parser_prec new_prec, lookahead_prec;
7241 tree overload;
7243 /* Parse the first expression. */
7244 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7245 cast_p, pidk);
7246 current.lhs_type = ERROR_MARK;
7247 current.prec = prec;
7249 if (cp_parser_error_occurred (parser))
7250 return error_mark_node;
7252 for (;;)
7254 /* Get an operator token. */
7255 token = cp_lexer_peek_token (parser->lexer);
7257 if (warn_cxx0x_compat
7258 && token->type == CPP_RSHIFT
7259 && !parser->greater_than_is_operator_p)
7261 if (warning_at (token->location, OPT_Wc__0x_compat,
7262 "%<>>%> operator is treated"
7263 " as two right angle brackets in C++11"))
7264 inform (token->location,
7265 "suggest parentheses around %<>>%> expression");
7268 new_prec = TOKEN_PRECEDENCE (token);
7270 /* Popping an entry off the stack means we completed a subexpression:
7271 - either we found a token which is not an operator (`>' where it is not
7272 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7273 will happen repeatedly;
7274 - or, we found an operator which has lower priority. This is the case
7275 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7276 parsing `3 * 4'. */
7277 if (new_prec <= current.prec)
7279 if (sp == stack)
7280 break;
7281 else
7282 goto pop;
7285 get_rhs:
7286 current.tree_type = binops_by_token[token->type].tree_type;
7287 current.loc = token->location;
7289 /* We used the operator token. */
7290 cp_lexer_consume_token (parser->lexer);
7292 /* For "false && x" or "true || x", x will never be executed;
7293 disable warnings while evaluating it. */
7294 if (current.tree_type == TRUTH_ANDIF_EXPR)
7295 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7296 else if (current.tree_type == TRUTH_ORIF_EXPR)
7297 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7299 /* Extract another operand. It may be the RHS of this expression
7300 or the LHS of a new, higher priority expression. */
7301 rhs = cp_parser_simple_cast_expression (parser);
7302 rhs_type = ERROR_MARK;
7304 /* Get another operator token. Look up its precedence to avoid
7305 building a useless (immediately popped) stack entry for common
7306 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7307 token = cp_lexer_peek_token (parser->lexer);
7308 lookahead_prec = TOKEN_PRECEDENCE (token);
7309 if (lookahead_prec > new_prec)
7311 /* ... and prepare to parse the RHS of the new, higher priority
7312 expression. Since precedence levels on the stack are
7313 monotonically increasing, we do not have to care about
7314 stack overflows. */
7315 *sp = current;
7316 ++sp;
7317 current.lhs = rhs;
7318 current.lhs_type = rhs_type;
7319 current.prec = new_prec;
7320 new_prec = lookahead_prec;
7321 goto get_rhs;
7323 pop:
7324 lookahead_prec = new_prec;
7325 /* If the stack is not empty, we have parsed into LHS the right side
7326 (`4' in the example above) of an expression we had suspended.
7327 We can use the information on the stack to recover the LHS (`3')
7328 from the stack together with the tree code (`MULT_EXPR'), and
7329 the precedence of the higher level subexpression
7330 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7331 which will be used to actually build the additive expression. */
7332 rhs = current.lhs;
7333 rhs_type = current.lhs_type;
7334 --sp;
7335 current = *sp;
7338 /* Undo the disabling of warnings done above. */
7339 if (current.tree_type == TRUTH_ANDIF_EXPR)
7340 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7341 else if (current.tree_type == TRUTH_ORIF_EXPR)
7342 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7344 overload = NULL;
7345 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7346 ERROR_MARK for everything that is not a binary expression.
7347 This makes warn_about_parentheses miss some warnings that
7348 involve unary operators. For unary expressions we should
7349 pass the correct tree_code unless the unary expression was
7350 surrounded by parentheses.
7352 if (no_toplevel_fold_p
7353 && lookahead_prec <= current.prec
7354 && sp == stack
7355 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7356 current.lhs = build2 (current.tree_type, boolean_type_node,
7357 current.lhs, rhs);
7358 else
7359 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7360 current.lhs, current.lhs_type,
7361 rhs, rhs_type, &overload,
7362 tf_warning_or_error);
7363 current.lhs_type = current.tree_type;
7365 /* If the binary operator required the use of an overloaded operator,
7366 then this expression cannot be an integral constant-expression.
7367 An overloaded operator can be used even if both operands are
7368 otherwise permissible in an integral constant-expression if at
7369 least one of the operands is of enumeration type. */
7371 if (overload
7372 && cp_parser_non_integral_constant_expression (parser,
7373 NIC_OVERLOADED))
7374 return error_mark_node;
7377 return current.lhs;
7381 /* Parse the `? expression : assignment-expression' part of a
7382 conditional-expression. The LOGICAL_OR_EXPR is the
7383 logical-or-expression that started the conditional-expression.
7384 Returns a representation of the entire conditional-expression.
7386 This routine is used by cp_parser_assignment_expression.
7388 ? expression : assignment-expression
7390 GNU Extensions:
7392 ? : assignment-expression */
7394 static tree
7395 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7397 tree expr;
7398 tree assignment_expr;
7399 struct cp_token *token;
7400 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7402 /* Consume the `?' token. */
7403 cp_lexer_consume_token (parser->lexer);
7404 token = cp_lexer_peek_token (parser->lexer);
7405 if (cp_parser_allow_gnu_extensions_p (parser)
7406 && token->type == CPP_COLON)
7408 pedwarn (token->location, OPT_Wpedantic,
7409 "ISO C++ does not allow ?: with omitted middle operand");
7410 /* Implicit true clause. */
7411 expr = NULL_TREE;
7412 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7413 warn_for_omitted_condop (token->location, logical_or_expr);
7415 else
7417 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7418 parser->colon_corrects_to_scope_p = false;
7419 /* Parse the expression. */
7420 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7421 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7422 c_inhibit_evaluation_warnings +=
7423 ((logical_or_expr == truthvalue_true_node)
7424 - (logical_or_expr == truthvalue_false_node));
7425 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7428 /* The next token should be a `:'. */
7429 cp_parser_require (parser, CPP_COLON, RT_COLON);
7430 /* Parse the assignment-expression. */
7431 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7432 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7434 /* Build the conditional-expression. */
7435 return build_x_conditional_expr (loc, logical_or_expr,
7436 expr,
7437 assignment_expr,
7438 tf_warning_or_error);
7441 /* Parse an assignment-expression.
7443 assignment-expression:
7444 conditional-expression
7445 logical-or-expression assignment-operator assignment_expression
7446 throw-expression
7448 CAST_P is true if this expression is the target of a cast.
7450 Returns a representation for the expression. */
7452 static tree
7453 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7454 cp_id_kind * pidk)
7456 tree expr;
7458 /* If the next token is the `throw' keyword, then we're looking at
7459 a throw-expression. */
7460 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7461 expr = cp_parser_throw_expression (parser);
7462 /* Otherwise, it must be that we are looking at a
7463 logical-or-expression. */
7464 else
7466 /* Parse the binary expressions (logical-or-expression). */
7467 expr = cp_parser_binary_expression (parser, cast_p, false,
7468 PREC_NOT_OPERATOR, pidk);
7469 /* If the next token is a `?' then we're actually looking at a
7470 conditional-expression. */
7471 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7472 return cp_parser_question_colon_clause (parser, expr);
7473 else
7475 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7477 /* If it's an assignment-operator, we're using the second
7478 production. */
7479 enum tree_code assignment_operator
7480 = cp_parser_assignment_operator_opt (parser);
7481 if (assignment_operator != ERROR_MARK)
7483 bool non_constant_p;
7484 location_t saved_input_location;
7486 /* Parse the right-hand side of the assignment. */
7487 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7489 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7490 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7492 /* An assignment may not appear in a
7493 constant-expression. */
7494 if (cp_parser_non_integral_constant_expression (parser,
7495 NIC_ASSIGNMENT))
7496 return error_mark_node;
7497 /* Build the assignment expression. Its default
7498 location is the location of the '=' token. */
7499 saved_input_location = input_location;
7500 input_location = loc;
7501 expr = build_x_modify_expr (loc, expr,
7502 assignment_operator,
7503 rhs,
7504 tf_warning_or_error);
7505 input_location = saved_input_location;
7510 return expr;
7513 /* Parse an (optional) assignment-operator.
7515 assignment-operator: one of
7516 = *= /= %= += -= >>= <<= &= ^= |=
7518 GNU Extension:
7520 assignment-operator: one of
7521 <?= >?=
7523 If the next token is an assignment operator, the corresponding tree
7524 code is returned, and the token is consumed. For example, for
7525 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7526 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7527 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7528 operator, ERROR_MARK is returned. */
7530 static enum tree_code
7531 cp_parser_assignment_operator_opt (cp_parser* parser)
7533 enum tree_code op;
7534 cp_token *token;
7536 /* Peek at the next token. */
7537 token = cp_lexer_peek_token (parser->lexer);
7539 switch (token->type)
7541 case CPP_EQ:
7542 op = NOP_EXPR;
7543 break;
7545 case CPP_MULT_EQ:
7546 op = MULT_EXPR;
7547 break;
7549 case CPP_DIV_EQ:
7550 op = TRUNC_DIV_EXPR;
7551 break;
7553 case CPP_MOD_EQ:
7554 op = TRUNC_MOD_EXPR;
7555 break;
7557 case CPP_PLUS_EQ:
7558 op = PLUS_EXPR;
7559 break;
7561 case CPP_MINUS_EQ:
7562 op = MINUS_EXPR;
7563 break;
7565 case CPP_RSHIFT_EQ:
7566 op = RSHIFT_EXPR;
7567 break;
7569 case CPP_LSHIFT_EQ:
7570 op = LSHIFT_EXPR;
7571 break;
7573 case CPP_AND_EQ:
7574 op = BIT_AND_EXPR;
7575 break;
7577 case CPP_XOR_EQ:
7578 op = BIT_XOR_EXPR;
7579 break;
7581 case CPP_OR_EQ:
7582 op = BIT_IOR_EXPR;
7583 break;
7585 default:
7586 /* Nothing else is an assignment operator. */
7587 op = ERROR_MARK;
7590 /* If it was an assignment operator, consume it. */
7591 if (op != ERROR_MARK)
7592 cp_lexer_consume_token (parser->lexer);
7594 return op;
7597 /* Parse an expression.
7599 expression:
7600 assignment-expression
7601 expression , assignment-expression
7603 CAST_P is true if this expression is the target of a cast.
7605 Returns a representation of the expression. */
7607 static tree
7608 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7610 tree expression = NULL_TREE;
7611 location_t loc = UNKNOWN_LOCATION;
7613 while (true)
7615 tree assignment_expression;
7617 /* Parse the next assignment-expression. */
7618 assignment_expression
7619 = cp_parser_assignment_expression (parser, cast_p, pidk);
7620 /* If this is the first assignment-expression, we can just
7621 save it away. */
7622 if (!expression)
7623 expression = assignment_expression;
7624 else
7625 expression = build_x_compound_expr (loc, expression,
7626 assignment_expression,
7627 tf_warning_or_error);
7628 /* If the next token is not a comma, then we are done with the
7629 expression. */
7630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7631 break;
7632 /* Consume the `,'. */
7633 loc = cp_lexer_peek_token (parser->lexer)->location;
7634 cp_lexer_consume_token (parser->lexer);
7635 /* A comma operator cannot appear in a constant-expression. */
7636 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7637 expression = error_mark_node;
7640 return expression;
7643 /* Parse a constant-expression.
7645 constant-expression:
7646 conditional-expression
7648 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7649 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7650 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7651 is false, NON_CONSTANT_P should be NULL. */
7653 static tree
7654 cp_parser_constant_expression (cp_parser* parser,
7655 bool allow_non_constant_p,
7656 bool *non_constant_p)
7658 bool saved_integral_constant_expression_p;
7659 bool saved_allow_non_integral_constant_expression_p;
7660 bool saved_non_integral_constant_expression_p;
7661 tree expression;
7663 /* It might seem that we could simply parse the
7664 conditional-expression, and then check to see if it were
7665 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7666 one that the compiler can figure out is constant, possibly after
7667 doing some simplifications or optimizations. The standard has a
7668 precise definition of constant-expression, and we must honor
7669 that, even though it is somewhat more restrictive.
7671 For example:
7673 int i[(2, 3)];
7675 is not a legal declaration, because `(2, 3)' is not a
7676 constant-expression. The `,' operator is forbidden in a
7677 constant-expression. However, GCC's constant-folding machinery
7678 will fold this operation to an INTEGER_CST for `3'. */
7680 /* Save the old settings. */
7681 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7682 saved_allow_non_integral_constant_expression_p
7683 = parser->allow_non_integral_constant_expression_p;
7684 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7685 /* We are now parsing a constant-expression. */
7686 parser->integral_constant_expression_p = true;
7687 parser->allow_non_integral_constant_expression_p
7688 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7689 parser->non_integral_constant_expression_p = false;
7690 /* Although the grammar says "conditional-expression", we parse an
7691 "assignment-expression", which also permits "throw-expression"
7692 and the use of assignment operators. In the case that
7693 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7694 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7695 actually essential that we look for an assignment-expression.
7696 For example, cp_parser_initializer_clauses uses this function to
7697 determine whether a particular assignment-expression is in fact
7698 constant. */
7699 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7700 /* Restore the old settings. */
7701 parser->integral_constant_expression_p
7702 = saved_integral_constant_expression_p;
7703 parser->allow_non_integral_constant_expression_p
7704 = saved_allow_non_integral_constant_expression_p;
7705 if (cxx_dialect >= cxx0x)
7707 /* Require an rvalue constant expression here; that's what our
7708 callers expect. Reference constant expressions are handled
7709 separately in e.g. cp_parser_template_argument. */
7710 bool is_const = potential_rvalue_constant_expression (expression);
7711 parser->non_integral_constant_expression_p = !is_const;
7712 if (!is_const && !allow_non_constant_p)
7713 require_potential_rvalue_constant_expression (expression);
7715 if (allow_non_constant_p)
7716 *non_constant_p = parser->non_integral_constant_expression_p;
7717 parser->non_integral_constant_expression_p
7718 = saved_non_integral_constant_expression_p;
7720 return expression;
7723 /* Parse __builtin_offsetof.
7725 offsetof-expression:
7726 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7728 offsetof-member-designator:
7729 id-expression
7730 | offsetof-member-designator "." id-expression
7731 | offsetof-member-designator "[" expression "]"
7732 | offsetof-member-designator "->" id-expression */
7734 static tree
7735 cp_parser_builtin_offsetof (cp_parser *parser)
7737 int save_ice_p, save_non_ice_p;
7738 tree type, expr;
7739 cp_id_kind dummy;
7740 cp_token *token;
7742 /* We're about to accept non-integral-constant things, but will
7743 definitely yield an integral constant expression. Save and
7744 restore these values around our local parsing. */
7745 save_ice_p = parser->integral_constant_expression_p;
7746 save_non_ice_p = parser->non_integral_constant_expression_p;
7748 /* Consume the "__builtin_offsetof" token. */
7749 cp_lexer_consume_token (parser->lexer);
7750 /* Consume the opening `('. */
7751 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7752 /* Parse the type-id. */
7753 type = cp_parser_type_id (parser);
7754 /* Look for the `,'. */
7755 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7756 token = cp_lexer_peek_token (parser->lexer);
7758 /* Build the (type *)null that begins the traditional offsetof macro. */
7759 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7760 tf_warning_or_error);
7762 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7763 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7764 true, &dummy, token->location);
7765 while (true)
7767 token = cp_lexer_peek_token (parser->lexer);
7768 switch (token->type)
7770 case CPP_OPEN_SQUARE:
7771 /* offsetof-member-designator "[" expression "]" */
7772 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7773 break;
7775 case CPP_DEREF:
7776 /* offsetof-member-designator "->" identifier */
7777 expr = grok_array_decl (token->location, expr, integer_zero_node);
7778 /* FALLTHRU */
7780 case CPP_DOT:
7781 /* offsetof-member-designator "." identifier */
7782 cp_lexer_consume_token (parser->lexer);
7783 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7784 expr, true, &dummy,
7785 token->location);
7786 break;
7788 case CPP_CLOSE_PAREN:
7789 /* Consume the ")" token. */
7790 cp_lexer_consume_token (parser->lexer);
7791 goto success;
7793 default:
7794 /* Error. We know the following require will fail, but
7795 that gives the proper error message. */
7796 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7797 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7798 expr = error_mark_node;
7799 goto failure;
7803 success:
7804 /* If we're processing a template, we can't finish the semantics yet.
7805 Otherwise we can fold the entire expression now. */
7806 if (processing_template_decl)
7807 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7808 else
7809 expr = finish_offsetof (expr);
7811 failure:
7812 parser->integral_constant_expression_p = save_ice_p;
7813 parser->non_integral_constant_expression_p = save_non_ice_p;
7815 return expr;
7818 /* Parse a trait expression.
7820 Returns a representation of the expression, the underlying type
7821 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7823 static tree
7824 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7826 cp_trait_kind kind;
7827 tree type1, type2 = NULL_TREE;
7828 bool binary = false;
7829 cp_decl_specifier_seq decl_specs;
7831 switch (keyword)
7833 case RID_HAS_NOTHROW_ASSIGN:
7834 kind = CPTK_HAS_NOTHROW_ASSIGN;
7835 break;
7836 case RID_HAS_NOTHROW_CONSTRUCTOR:
7837 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7838 break;
7839 case RID_HAS_NOTHROW_COPY:
7840 kind = CPTK_HAS_NOTHROW_COPY;
7841 break;
7842 case RID_HAS_TRIVIAL_ASSIGN:
7843 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7844 break;
7845 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7846 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7847 break;
7848 case RID_HAS_TRIVIAL_COPY:
7849 kind = CPTK_HAS_TRIVIAL_COPY;
7850 break;
7851 case RID_HAS_TRIVIAL_DESTRUCTOR:
7852 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7853 break;
7854 case RID_HAS_VIRTUAL_DESTRUCTOR:
7855 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7856 break;
7857 case RID_IS_ABSTRACT:
7858 kind = CPTK_IS_ABSTRACT;
7859 break;
7860 case RID_IS_BASE_OF:
7861 kind = CPTK_IS_BASE_OF;
7862 binary = true;
7863 break;
7864 case RID_IS_CLASS:
7865 kind = CPTK_IS_CLASS;
7866 break;
7867 case RID_IS_CONVERTIBLE_TO:
7868 kind = CPTK_IS_CONVERTIBLE_TO;
7869 binary = true;
7870 break;
7871 case RID_IS_EMPTY:
7872 kind = CPTK_IS_EMPTY;
7873 break;
7874 case RID_IS_ENUM:
7875 kind = CPTK_IS_ENUM;
7876 break;
7877 case RID_IS_FINAL:
7878 kind = CPTK_IS_FINAL;
7879 break;
7880 case RID_IS_LITERAL_TYPE:
7881 kind = CPTK_IS_LITERAL_TYPE;
7882 break;
7883 case RID_IS_POD:
7884 kind = CPTK_IS_POD;
7885 break;
7886 case RID_IS_POLYMORPHIC:
7887 kind = CPTK_IS_POLYMORPHIC;
7888 break;
7889 case RID_IS_STD_LAYOUT:
7890 kind = CPTK_IS_STD_LAYOUT;
7891 break;
7892 case RID_IS_TRIVIAL:
7893 kind = CPTK_IS_TRIVIAL;
7894 break;
7895 case RID_IS_UNION:
7896 kind = CPTK_IS_UNION;
7897 break;
7898 case RID_UNDERLYING_TYPE:
7899 kind = CPTK_UNDERLYING_TYPE;
7900 break;
7901 case RID_BASES:
7902 kind = CPTK_BASES;
7903 break;
7904 case RID_DIRECT_BASES:
7905 kind = CPTK_DIRECT_BASES;
7906 break;
7907 default:
7908 gcc_unreachable ();
7911 /* Consume the token. */
7912 cp_lexer_consume_token (parser->lexer);
7914 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7916 type1 = cp_parser_type_id (parser);
7918 if (type1 == error_mark_node)
7919 return error_mark_node;
7921 /* Build a trivial decl-specifier-seq. */
7922 clear_decl_specs (&decl_specs);
7923 decl_specs.type = type1;
7925 /* Call grokdeclarator to figure out what type this is. */
7926 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7927 /*initialized=*/0, /*attrlist=*/NULL);
7929 if (binary)
7931 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7933 type2 = cp_parser_type_id (parser);
7935 if (type2 == error_mark_node)
7936 return error_mark_node;
7938 /* Build a trivial decl-specifier-seq. */
7939 clear_decl_specs (&decl_specs);
7940 decl_specs.type = type2;
7942 /* Call grokdeclarator to figure out what type this is. */
7943 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7944 /*initialized=*/0, /*attrlist=*/NULL);
7947 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7949 /* Complete the trait expression, which may mean either processing
7950 the trait expr now or saving it for template instantiation. */
7951 switch(kind)
7953 case CPTK_UNDERLYING_TYPE:
7954 return finish_underlying_type (type1);
7955 case CPTK_BASES:
7956 return finish_bases (type1, false);
7957 case CPTK_DIRECT_BASES:
7958 return finish_bases (type1, true);
7959 default:
7960 return finish_trait_expr (kind, type1, type2);
7964 /* Lambdas that appear in variable initializer or default argument scope
7965 get that in their mangling, so we need to record it. We might as well
7966 use the count for function and namespace scopes as well. */
7967 static GTY(()) tree lambda_scope;
7968 static GTY(()) int lambda_count;
7969 typedef struct GTY(()) tree_int
7971 tree t;
7972 int i;
7973 } tree_int;
7974 DEF_VEC_O(tree_int);
7975 DEF_VEC_ALLOC_O(tree_int,gc);
7976 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7978 static void
7979 start_lambda_scope (tree decl)
7981 tree_int ti;
7982 gcc_assert (decl);
7983 /* Once we're inside a function, we ignore other scopes and just push
7984 the function again so that popping works properly. */
7985 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7986 decl = current_function_decl;
7987 ti.t = lambda_scope;
7988 ti.i = lambda_count;
7989 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7990 if (lambda_scope != decl)
7992 /* Don't reset the count if we're still in the same function. */
7993 lambda_scope = decl;
7994 lambda_count = 0;
7998 static void
7999 record_lambda_scope (tree lambda)
8001 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8002 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8005 static void
8006 finish_lambda_scope (void)
8008 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8009 if (lambda_scope != p->t)
8011 lambda_scope = p->t;
8012 lambda_count = p->i;
8014 VEC_pop (tree_int, lambda_scope_stack);
8017 /* Parse a lambda expression.
8019 lambda-expression:
8020 lambda-introducer lambda-declarator [opt] compound-statement
8022 Returns a representation of the expression. */
8024 static tree
8025 cp_parser_lambda_expression (cp_parser* parser)
8027 tree lambda_expr = build_lambda_expr ();
8028 tree type;
8029 bool ok;
8031 LAMBDA_EXPR_LOCATION (lambda_expr)
8032 = cp_lexer_peek_token (parser->lexer)->location;
8034 if (cp_unevaluated_operand)
8035 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8036 "lambda-expression in unevaluated context");
8038 /* We may be in the middle of deferred access check. Disable
8039 it now. */
8040 push_deferring_access_checks (dk_no_deferred);
8042 cp_parser_lambda_introducer (parser, lambda_expr);
8044 type = begin_lambda_type (lambda_expr);
8045 if (type == error_mark_node)
8046 return error_mark_node;
8048 record_lambda_scope (lambda_expr);
8050 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8051 determine_visibility (TYPE_NAME (type));
8053 /* Now that we've started the type, add the capture fields for any
8054 explicit captures. */
8055 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8058 /* Inside the class, surrounding template-parameter-lists do not apply. */
8059 unsigned int saved_num_template_parameter_lists
8060 = parser->num_template_parameter_lists;
8061 unsigned char in_statement = parser->in_statement;
8062 bool in_switch_statement_p = parser->in_switch_statement_p;
8064 parser->num_template_parameter_lists = 0;
8065 parser->in_statement = 0;
8066 parser->in_switch_statement_p = false;
8068 /* By virtue of defining a local class, a lambda expression has access to
8069 the private variables of enclosing classes. */
8071 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8073 if (ok)
8074 cp_parser_lambda_body (parser, lambda_expr);
8075 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8076 cp_parser_skip_to_end_of_block_or_statement (parser);
8078 /* The capture list was built up in reverse order; fix that now. */
8080 tree newlist = NULL_TREE;
8081 tree elt, next;
8083 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8084 elt; elt = next)
8086 next = TREE_CHAIN (elt);
8087 TREE_CHAIN (elt) = newlist;
8088 newlist = elt;
8090 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8093 if (ok)
8094 maybe_add_lambda_conv_op (type);
8096 type = finish_struct (type, /*attributes=*/NULL_TREE);
8098 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8099 parser->in_statement = in_statement;
8100 parser->in_switch_statement_p = in_switch_statement_p;
8103 pop_deferring_access_checks ();
8105 /* This field is only used during parsing of the lambda. */
8106 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8108 /* This lambda shouldn't have any proxies left at this point. */
8109 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8110 /* And now that we're done, push proxies for an enclosing lambda. */
8111 insert_pending_capture_proxies ();
8113 if (ok)
8114 return build_lambda_object (lambda_expr);
8115 else
8116 return error_mark_node;
8119 /* Parse the beginning of a lambda expression.
8121 lambda-introducer:
8122 [ lambda-capture [opt] ]
8124 LAMBDA_EXPR is the current representation of the lambda expression. */
8126 static void
8127 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8129 /* Need commas after the first capture. */
8130 bool first = true;
8132 /* Eat the leading `['. */
8133 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8135 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8136 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8137 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8138 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8139 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8140 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8142 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8144 cp_lexer_consume_token (parser->lexer);
8145 first = false;
8148 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8150 cp_token* capture_token;
8151 tree capture_id;
8152 tree capture_init_expr;
8153 cp_id_kind idk = CP_ID_KIND_NONE;
8154 bool explicit_init_p = false;
8156 enum capture_kind_type
8158 BY_COPY,
8159 BY_REFERENCE
8161 enum capture_kind_type capture_kind = BY_COPY;
8163 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8165 error ("expected end of capture-list");
8166 return;
8169 if (first)
8170 first = false;
8171 else
8172 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8174 /* Possibly capture `this'. */
8175 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8177 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8178 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8179 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8180 "with by-copy capture default");
8181 cp_lexer_consume_token (parser->lexer);
8182 add_capture (lambda_expr,
8183 /*id=*/this_identifier,
8184 /*initializer=*/finish_this_expr(),
8185 /*by_reference_p=*/false,
8186 explicit_init_p);
8187 continue;
8190 /* Remember whether we want to capture as a reference or not. */
8191 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8193 capture_kind = BY_REFERENCE;
8194 cp_lexer_consume_token (parser->lexer);
8197 /* Get the identifier. */
8198 capture_token = cp_lexer_peek_token (parser->lexer);
8199 capture_id = cp_parser_identifier (parser);
8201 if (capture_id == error_mark_node)
8202 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8203 delimiters, but I modified this to stop on unnested ']' as well. It
8204 was already changed to stop on unnested '}', so the
8205 "closing_parenthesis" name is no more misleading with my change. */
8207 cp_parser_skip_to_closing_parenthesis (parser,
8208 /*recovering=*/true,
8209 /*or_comma=*/true,
8210 /*consume_paren=*/true);
8211 break;
8214 /* Find the initializer for this capture. */
8215 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8217 /* An explicit expression exists. */
8218 cp_lexer_consume_token (parser->lexer);
8219 pedwarn (input_location, OPT_Wpedantic,
8220 "ISO C++ does not allow initializers "
8221 "in lambda expression capture lists");
8222 capture_init_expr = cp_parser_assignment_expression (parser,
8223 /*cast_p=*/true,
8224 &idk);
8225 explicit_init_p = true;
8227 else
8229 const char* error_msg;
8231 /* Turn the identifier into an id-expression. */
8232 capture_init_expr
8233 = cp_parser_lookup_name
8234 (parser,
8235 capture_id,
8236 none_type,
8237 /*is_template=*/false,
8238 /*is_namespace=*/false,
8239 /*check_dependency=*/true,
8240 /*ambiguous_decls=*/NULL,
8241 capture_token->location);
8243 if (capture_init_expr == error_mark_node)
8245 unqualified_name_lookup_error (capture_id);
8246 continue;
8248 else if (DECL_P (capture_init_expr)
8249 && (TREE_CODE (capture_init_expr) != VAR_DECL
8250 && TREE_CODE (capture_init_expr) != PARM_DECL))
8252 error_at (capture_token->location,
8253 "capture of non-variable %qD ",
8254 capture_init_expr);
8255 inform (0, "%q+#D declared here", capture_init_expr);
8256 continue;
8258 if (TREE_CODE (capture_init_expr) == VAR_DECL
8259 && decl_storage_duration (capture_init_expr) != dk_auto)
8261 pedwarn (capture_token->location, 0, "capture of variable "
8262 "%qD with non-automatic storage duration",
8263 capture_init_expr);
8264 inform (0, "%q+#D declared here", capture_init_expr);
8265 continue;
8268 capture_init_expr
8269 = finish_id_expression
8270 (capture_id,
8271 capture_init_expr,
8272 parser->scope,
8273 &idk,
8274 /*integral_constant_expression_p=*/false,
8275 /*allow_non_integral_constant_expression_p=*/false,
8276 /*non_integral_constant_expression_p=*/NULL,
8277 /*template_p=*/false,
8278 /*done=*/true,
8279 /*address_p=*/false,
8280 /*template_arg_p=*/false,
8281 &error_msg,
8282 capture_token->location);
8285 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8286 && !explicit_init_p)
8288 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8289 && capture_kind == BY_COPY)
8290 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8291 "of %qD redundant with by-copy capture default",
8292 capture_id);
8293 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8294 && capture_kind == BY_REFERENCE)
8295 pedwarn (capture_token->location, 0, "explicit by-reference "
8296 "capture of %qD redundant with by-reference capture "
8297 "default", capture_id);
8300 add_capture (lambda_expr,
8301 capture_id,
8302 capture_init_expr,
8303 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8304 explicit_init_p);
8307 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8310 /* Parse the (optional) middle of a lambda expression.
8312 lambda-declarator:
8313 ( parameter-declaration-clause [opt] )
8314 attribute-specifier [opt]
8315 mutable [opt]
8316 exception-specification [opt]
8317 lambda-return-type-clause [opt]
8319 LAMBDA_EXPR is the current representation of the lambda expression. */
8321 static bool
8322 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8324 /* 5.1.1.4 of the standard says:
8325 If a lambda-expression does not include a lambda-declarator, it is as if
8326 the lambda-declarator were ().
8327 This means an empty parameter list, no attributes, and no exception
8328 specification. */
8329 tree param_list = void_list_node;
8330 tree attributes = NULL_TREE;
8331 tree exception_spec = NULL_TREE;
8332 tree t;
8334 /* The lambda-declarator is optional, but must begin with an opening
8335 parenthesis if present. */
8336 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8338 cp_lexer_consume_token (parser->lexer);
8340 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8342 /* Parse parameters. */
8343 param_list = cp_parser_parameter_declaration_clause (parser);
8345 /* Default arguments shall not be specified in the
8346 parameter-declaration-clause of a lambda-declarator. */
8347 for (t = param_list; t; t = TREE_CHAIN (t))
8348 if (TREE_PURPOSE (t))
8349 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8350 "default argument specified for lambda parameter");
8352 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8354 attributes = cp_parser_attributes_opt (parser);
8356 /* Parse optional `mutable' keyword. */
8357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8359 cp_lexer_consume_token (parser->lexer);
8360 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8363 /* Parse optional exception specification. */
8364 exception_spec = cp_parser_exception_specification_opt (parser);
8366 /* Parse optional trailing return type. */
8367 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8369 cp_lexer_consume_token (parser->lexer);
8370 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8373 /* The function parameters must be in scope all the way until after the
8374 trailing-return-type in case of decltype. */
8375 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8376 pop_binding (DECL_NAME (t), t);
8378 leave_scope ();
8381 /* Create the function call operator.
8383 Messing with declarators like this is no uglier than building up the
8384 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8385 other code. */
8387 cp_decl_specifier_seq return_type_specs;
8388 cp_declarator* declarator;
8389 tree fco;
8390 int quals;
8391 void *p;
8393 clear_decl_specs (&return_type_specs);
8394 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8395 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8396 else
8397 /* Maybe we will deduce the return type later. */
8398 return_type_specs.type = make_auto ();
8400 p = obstack_alloc (&declarator_obstack, 0);
8402 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8403 sfk_none);
8405 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8406 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8407 declarator = make_call_declarator (declarator, param_list, quals,
8408 VIRT_SPEC_UNSPECIFIED,
8409 exception_spec,
8410 /*late_return_type=*/NULL_TREE);
8411 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8413 fco = grokmethod (&return_type_specs,
8414 declarator,
8415 attributes);
8416 if (fco != error_mark_node)
8418 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8419 DECL_ARTIFICIAL (fco) = 1;
8420 /* Give the object parameter a different name. */
8421 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8424 finish_member_declaration (fco);
8426 obstack_free (&declarator_obstack, p);
8428 return (fco != error_mark_node);
8432 /* Parse the body of a lambda expression, which is simply
8434 compound-statement
8436 but which requires special handling.
8437 LAMBDA_EXPR is the current representation of the lambda expression. */
8439 static void
8440 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8442 bool nested = (current_function_decl != NULL_TREE);
8443 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8444 if (nested)
8445 push_function_context ();
8446 else
8447 /* Still increment function_depth so that we don't GC in the
8448 middle of an expression. */
8449 ++function_depth;
8450 /* Clear this in case we're in the middle of a default argument. */
8451 parser->local_variables_forbidden_p = false;
8453 /* Finish the function call operator
8454 - class_specifier
8455 + late_parsing_for_member
8456 + function_definition_after_declarator
8457 + ctor_initializer_opt_and_function_body */
8459 tree fco = lambda_function (lambda_expr);
8460 tree body;
8461 bool done = false;
8462 tree compound_stmt;
8463 tree cap;
8465 /* Let the front end know that we are going to be defining this
8466 function. */
8467 start_preparsed_function (fco,
8468 NULL_TREE,
8469 SF_PRE_PARSED | SF_INCLASS_INLINE);
8471 start_lambda_scope (fco);
8472 body = begin_function_body ();
8474 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8475 goto out;
8477 /* Push the proxies for any explicit captures. */
8478 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8479 cap = TREE_CHAIN (cap))
8480 build_capture_proxy (TREE_PURPOSE (cap));
8482 compound_stmt = begin_compound_stmt (0);
8484 /* 5.1.1.4 of the standard says:
8485 If a lambda-expression does not include a trailing-return-type, it
8486 is as if the trailing-return-type denotes the following type:
8487 * if the compound-statement is of the form
8488 { return attribute-specifier [opt] expression ; }
8489 the type of the returned expression after lvalue-to-rvalue
8490 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8491 (_conv.array_ 4.2), and function-to-pointer conversion
8492 (_conv.func_ 4.3);
8493 * otherwise, void. */
8495 /* In a lambda that has neither a lambda-return-type-clause
8496 nor a deducible form, errors should be reported for return statements
8497 in the body. Since we used void as the placeholder return type, parsing
8498 the body as usual will give such desired behavior. */
8499 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8500 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8501 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8503 tree expr = NULL_TREE;
8504 cp_id_kind idk = CP_ID_KIND_NONE;
8506 /* Parse tentatively in case there's more after the initial return
8507 statement. */
8508 cp_parser_parse_tentatively (parser);
8510 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8512 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8514 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8515 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8517 if (cp_parser_parse_definitely (parser))
8519 if (!processing_template_decl)
8520 apply_deduced_return_type (fco, lambda_return_type (expr));
8522 /* Will get error here if type not deduced yet. */
8523 finish_return_stmt (expr);
8525 done = true;
8529 if (!done)
8531 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8532 cp_parser_label_declaration (parser);
8533 cp_parser_statement_seq_opt (parser, NULL_TREE);
8534 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8537 finish_compound_stmt (compound_stmt);
8539 out:
8540 finish_function_body (body);
8541 finish_lambda_scope ();
8543 /* Finish the function and generate code for it if necessary. */
8544 expand_or_defer_fn (finish_function (/*inline*/2));
8547 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8548 if (nested)
8549 pop_function_context();
8550 else
8551 --function_depth;
8554 /* Statements [gram.stmt.stmt] */
8556 /* Parse a statement.
8558 statement:
8559 labeled-statement
8560 expression-statement
8561 compound-statement
8562 selection-statement
8563 iteration-statement
8564 jump-statement
8565 declaration-statement
8566 try-block
8568 TM Extension:
8570 statement:
8571 atomic-statement
8573 IN_COMPOUND is true when the statement is nested inside a
8574 cp_parser_compound_statement; this matters for certain pragmas.
8576 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8577 is a (possibly labeled) if statement which is not enclosed in braces
8578 and has an else clause. This is used to implement -Wparentheses. */
8580 static void
8581 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8582 bool in_compound, bool *if_p)
8584 tree statement;
8585 cp_token *token;
8586 location_t statement_location;
8588 restart:
8589 if (if_p != NULL)
8590 *if_p = false;
8591 /* There is no statement yet. */
8592 statement = NULL_TREE;
8593 /* Peek at the next token. */
8594 token = cp_lexer_peek_token (parser->lexer);
8595 /* Remember the location of the first token in the statement. */
8596 statement_location = token->location;
8597 /* If this is a keyword, then that will often determine what kind of
8598 statement we have. */
8599 if (token->type == CPP_KEYWORD)
8601 enum rid keyword = token->keyword;
8603 switch (keyword)
8605 case RID_CASE:
8606 case RID_DEFAULT:
8607 /* Looks like a labeled-statement with a case label.
8608 Parse the label, and then use tail recursion to parse
8609 the statement. */
8610 cp_parser_label_for_labeled_statement (parser);
8611 goto restart;
8613 case RID_IF:
8614 case RID_SWITCH:
8615 statement = cp_parser_selection_statement (parser, if_p);
8616 break;
8618 case RID_WHILE:
8619 case RID_DO:
8620 case RID_FOR:
8621 statement = cp_parser_iteration_statement (parser);
8622 break;
8624 case RID_BREAK:
8625 case RID_CONTINUE:
8626 case RID_RETURN:
8627 case RID_GOTO:
8628 statement = cp_parser_jump_statement (parser);
8629 break;
8631 /* Objective-C++ exception-handling constructs. */
8632 case RID_AT_TRY:
8633 case RID_AT_CATCH:
8634 case RID_AT_FINALLY:
8635 case RID_AT_SYNCHRONIZED:
8636 case RID_AT_THROW:
8637 statement = cp_parser_objc_statement (parser);
8638 break;
8640 case RID_TRY:
8641 statement = cp_parser_try_block (parser);
8642 break;
8644 case RID_NAMESPACE:
8645 /* This must be a namespace alias definition. */
8646 cp_parser_declaration_statement (parser);
8647 return;
8649 case RID_TRANSACTION_ATOMIC:
8650 case RID_TRANSACTION_RELAXED:
8651 statement = cp_parser_transaction (parser, keyword);
8652 break;
8653 case RID_TRANSACTION_CANCEL:
8654 statement = cp_parser_transaction_cancel (parser);
8655 break;
8657 default:
8658 /* It might be a keyword like `int' that can start a
8659 declaration-statement. */
8660 break;
8663 else if (token->type == CPP_NAME)
8665 /* If the next token is a `:', then we are looking at a
8666 labeled-statement. */
8667 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8668 if (token->type == CPP_COLON)
8670 /* Looks like a labeled-statement with an ordinary label.
8671 Parse the label, and then use tail recursion to parse
8672 the statement. */
8673 cp_parser_label_for_labeled_statement (parser);
8674 goto restart;
8677 /* Anything that starts with a `{' must be a compound-statement. */
8678 else if (token->type == CPP_OPEN_BRACE)
8679 statement = cp_parser_compound_statement (parser, NULL, false, false);
8680 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8681 a statement all its own. */
8682 else if (token->type == CPP_PRAGMA)
8684 /* Only certain OpenMP pragmas are attached to statements, and thus
8685 are considered statements themselves. All others are not. In
8686 the context of a compound, accept the pragma as a "statement" and
8687 return so that we can check for a close brace. Otherwise we
8688 require a real statement and must go back and read one. */
8689 if (in_compound)
8690 cp_parser_pragma (parser, pragma_compound);
8691 else if (!cp_parser_pragma (parser, pragma_stmt))
8692 goto restart;
8693 return;
8695 else if (token->type == CPP_EOF)
8697 cp_parser_error (parser, "expected statement");
8698 return;
8701 /* Everything else must be a declaration-statement or an
8702 expression-statement. Try for the declaration-statement
8703 first, unless we are looking at a `;', in which case we know that
8704 we have an expression-statement. */
8705 if (!statement)
8707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8709 cp_parser_parse_tentatively (parser);
8710 /* Try to parse the declaration-statement. */
8711 cp_parser_declaration_statement (parser);
8712 /* If that worked, we're done. */
8713 if (cp_parser_parse_definitely (parser))
8714 return;
8716 /* Look for an expression-statement instead. */
8717 statement = cp_parser_expression_statement (parser, in_statement_expr);
8720 /* Set the line number for the statement. */
8721 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8722 SET_EXPR_LOCATION (statement, statement_location);
8725 /* Parse the label for a labeled-statement, i.e.
8727 identifier :
8728 case constant-expression :
8729 default :
8731 GNU Extension:
8732 case constant-expression ... constant-expression : statement
8734 When a label is parsed without errors, the label is added to the
8735 parse tree by the finish_* functions, so this function doesn't
8736 have to return the label. */
8738 static void
8739 cp_parser_label_for_labeled_statement (cp_parser* parser)
8741 cp_token *token;
8742 tree label = NULL_TREE;
8743 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8745 /* The next token should be an identifier. */
8746 token = cp_lexer_peek_token (parser->lexer);
8747 if (token->type != CPP_NAME
8748 && token->type != CPP_KEYWORD)
8750 cp_parser_error (parser, "expected labeled-statement");
8751 return;
8754 parser->colon_corrects_to_scope_p = false;
8755 switch (token->keyword)
8757 case RID_CASE:
8759 tree expr, expr_hi;
8760 cp_token *ellipsis;
8762 /* Consume the `case' token. */
8763 cp_lexer_consume_token (parser->lexer);
8764 /* Parse the constant-expression. */
8765 expr = cp_parser_constant_expression (parser,
8766 /*allow_non_constant_p=*/false,
8767 NULL);
8769 ellipsis = cp_lexer_peek_token (parser->lexer);
8770 if (ellipsis->type == CPP_ELLIPSIS)
8772 /* Consume the `...' token. */
8773 cp_lexer_consume_token (parser->lexer);
8774 expr_hi =
8775 cp_parser_constant_expression (parser,
8776 /*allow_non_constant_p=*/false,
8777 NULL);
8778 /* We don't need to emit warnings here, as the common code
8779 will do this for us. */
8781 else
8782 expr_hi = NULL_TREE;
8784 if (parser->in_switch_statement_p)
8785 finish_case_label (token->location, expr, expr_hi);
8786 else
8787 error_at (token->location,
8788 "case label %qE not within a switch statement",
8789 expr);
8791 break;
8793 case RID_DEFAULT:
8794 /* Consume the `default' token. */
8795 cp_lexer_consume_token (parser->lexer);
8797 if (parser->in_switch_statement_p)
8798 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8799 else
8800 error_at (token->location, "case label not within a switch statement");
8801 break;
8803 default:
8804 /* Anything else must be an ordinary label. */
8805 label = finish_label_stmt (cp_parser_identifier (parser));
8806 break;
8809 /* Require the `:' token. */
8810 cp_parser_require (parser, CPP_COLON, RT_COLON);
8812 /* An ordinary label may optionally be followed by attributes.
8813 However, this is only permitted if the attributes are then
8814 followed by a semicolon. This is because, for backward
8815 compatibility, when parsing
8816 lab: __attribute__ ((unused)) int i;
8817 we want the attribute to attach to "i", not "lab". */
8818 if (label != NULL_TREE
8819 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8821 tree attrs;
8823 cp_parser_parse_tentatively (parser);
8824 attrs = cp_parser_attributes_opt (parser);
8825 if (attrs == NULL_TREE
8826 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8827 cp_parser_abort_tentative_parse (parser);
8828 else if (!cp_parser_parse_definitely (parser))
8830 else
8831 cplus_decl_attributes (&label, attrs, 0);
8834 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8837 /* Parse an expression-statement.
8839 expression-statement:
8840 expression [opt] ;
8842 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8843 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8844 indicates whether this expression-statement is part of an
8845 expression statement. */
8847 static tree
8848 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8850 tree statement = NULL_TREE;
8851 cp_token *token = cp_lexer_peek_token (parser->lexer);
8853 /* If the next token is a ';', then there is no expression
8854 statement. */
8855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8856 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8858 /* Give a helpful message for "A<T>::type t;" and the like. */
8859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8860 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8862 if (TREE_CODE (statement) == SCOPE_REF)
8863 error_at (token->location, "need %<typename%> before %qE because "
8864 "%qT is a dependent scope",
8865 statement, TREE_OPERAND (statement, 0));
8866 else if (is_overloaded_fn (statement)
8867 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8869 /* A::A a; */
8870 tree fn = get_first_fn (statement);
8871 error_at (token->location,
8872 "%<%T::%D%> names the constructor, not the type",
8873 DECL_CONTEXT (fn), DECL_NAME (fn));
8877 /* Consume the final `;'. */
8878 cp_parser_consume_semicolon_at_end_of_statement (parser);
8880 if (in_statement_expr
8881 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8882 /* This is the final expression statement of a statement
8883 expression. */
8884 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8885 else if (statement)
8886 statement = finish_expr_stmt (statement);
8887 else
8888 finish_stmt ();
8890 return statement;
8893 /* Parse a compound-statement.
8895 compound-statement:
8896 { statement-seq [opt] }
8898 GNU extension:
8900 compound-statement:
8901 { label-declaration-seq [opt] statement-seq [opt] }
8903 label-declaration-seq:
8904 label-declaration
8905 label-declaration-seq label-declaration
8907 Returns a tree representing the statement. */
8909 static tree
8910 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8911 bool in_try, bool function_body)
8913 tree compound_stmt;
8915 /* Consume the `{'. */
8916 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8917 return error_mark_node;
8918 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8919 && !function_body)
8920 pedwarn (input_location, OPT_Wpedantic,
8921 "compound-statement in constexpr function");
8922 /* Begin the compound-statement. */
8923 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8924 /* If the next keyword is `__label__' we have a label declaration. */
8925 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8926 cp_parser_label_declaration (parser);
8927 /* Parse an (optional) statement-seq. */
8928 cp_parser_statement_seq_opt (parser, in_statement_expr);
8929 /* Finish the compound-statement. */
8930 finish_compound_stmt (compound_stmt);
8931 /* Consume the `}'. */
8932 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8934 return compound_stmt;
8937 /* Parse an (optional) statement-seq.
8939 statement-seq:
8940 statement
8941 statement-seq [opt] statement */
8943 static void
8944 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8946 /* Scan statements until there aren't any more. */
8947 while (true)
8949 cp_token *token = cp_lexer_peek_token (parser->lexer);
8951 /* If we are looking at a `}', then we have run out of
8952 statements; the same is true if we have reached the end
8953 of file, or have stumbled upon a stray '@end'. */
8954 if (token->type == CPP_CLOSE_BRACE
8955 || token->type == CPP_EOF
8956 || token->type == CPP_PRAGMA_EOL
8957 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8958 break;
8960 /* If we are in a compound statement and find 'else' then
8961 something went wrong. */
8962 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8964 if (parser->in_statement & IN_IF_STMT)
8965 break;
8966 else
8968 token = cp_lexer_consume_token (parser->lexer);
8969 error_at (token->location, "%<else%> without a previous %<if%>");
8973 /* Parse the statement. */
8974 cp_parser_statement (parser, in_statement_expr, true, NULL);
8978 /* Parse a selection-statement.
8980 selection-statement:
8981 if ( condition ) statement
8982 if ( condition ) statement else statement
8983 switch ( condition ) statement
8985 Returns the new IF_STMT or SWITCH_STMT.
8987 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8988 is a (possibly labeled) if statement which is not enclosed in
8989 braces and has an else clause. This is used to implement
8990 -Wparentheses. */
8992 static tree
8993 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8995 cp_token *token;
8996 enum rid keyword;
8998 if (if_p != NULL)
8999 *if_p = false;
9001 /* Peek at the next token. */
9002 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9004 /* See what kind of keyword it is. */
9005 keyword = token->keyword;
9006 switch (keyword)
9008 case RID_IF:
9009 case RID_SWITCH:
9011 tree statement;
9012 tree condition;
9014 /* Look for the `('. */
9015 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9017 cp_parser_skip_to_end_of_statement (parser);
9018 return error_mark_node;
9021 /* Begin the selection-statement. */
9022 if (keyword == RID_IF)
9023 statement = begin_if_stmt ();
9024 else
9025 statement = begin_switch_stmt ();
9027 /* Parse the condition. */
9028 condition = cp_parser_condition (parser);
9029 /* Look for the `)'. */
9030 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9031 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9032 /*consume_paren=*/true);
9034 if (keyword == RID_IF)
9036 bool nested_if;
9037 unsigned char in_statement;
9039 /* Add the condition. */
9040 finish_if_stmt_cond (condition, statement);
9042 /* Parse the then-clause. */
9043 in_statement = parser->in_statement;
9044 parser->in_statement |= IN_IF_STMT;
9045 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9047 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9048 add_stmt (build_empty_stmt (loc));
9049 cp_lexer_consume_token (parser->lexer);
9050 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9051 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9052 "empty body in an %<if%> statement");
9053 nested_if = false;
9055 else
9056 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9057 parser->in_statement = in_statement;
9059 finish_then_clause (statement);
9061 /* If the next token is `else', parse the else-clause. */
9062 if (cp_lexer_next_token_is_keyword (parser->lexer,
9063 RID_ELSE))
9065 /* Consume the `else' keyword. */
9066 cp_lexer_consume_token (parser->lexer);
9067 begin_else_clause (statement);
9068 /* Parse the else-clause. */
9069 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9071 location_t loc;
9072 loc = cp_lexer_peek_token (parser->lexer)->location;
9073 warning_at (loc,
9074 OPT_Wempty_body, "suggest braces around "
9075 "empty body in an %<else%> statement");
9076 add_stmt (build_empty_stmt (loc));
9077 cp_lexer_consume_token (parser->lexer);
9079 else
9080 cp_parser_implicitly_scoped_statement (parser, NULL);
9082 finish_else_clause (statement);
9084 /* If we are currently parsing a then-clause, then
9085 IF_P will not be NULL. We set it to true to
9086 indicate that this if statement has an else clause.
9087 This may trigger the Wparentheses warning below
9088 when we get back up to the parent if statement. */
9089 if (if_p != NULL)
9090 *if_p = true;
9092 else
9094 /* This if statement does not have an else clause. If
9095 NESTED_IF is true, then the then-clause is an if
9096 statement which does have an else clause. We warn
9097 about the potential ambiguity. */
9098 if (nested_if)
9099 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9100 "suggest explicit braces to avoid ambiguous"
9101 " %<else%>");
9104 /* Now we're all done with the if-statement. */
9105 finish_if_stmt (statement);
9107 else
9109 bool in_switch_statement_p;
9110 unsigned char in_statement;
9112 /* Add the condition. */
9113 finish_switch_cond (condition, statement);
9115 /* Parse the body of the switch-statement. */
9116 in_switch_statement_p = parser->in_switch_statement_p;
9117 in_statement = parser->in_statement;
9118 parser->in_switch_statement_p = true;
9119 parser->in_statement |= IN_SWITCH_STMT;
9120 cp_parser_implicitly_scoped_statement (parser, NULL);
9121 parser->in_switch_statement_p = in_switch_statement_p;
9122 parser->in_statement = in_statement;
9124 /* Now we're all done with the switch-statement. */
9125 finish_switch_stmt (statement);
9128 return statement;
9130 break;
9132 default:
9133 cp_parser_error (parser, "expected selection-statement");
9134 return error_mark_node;
9138 /* Parse a condition.
9140 condition:
9141 expression
9142 type-specifier-seq declarator = initializer-clause
9143 type-specifier-seq declarator braced-init-list
9145 GNU Extension:
9147 condition:
9148 type-specifier-seq declarator asm-specification [opt]
9149 attributes [opt] = assignment-expression
9151 Returns the expression that should be tested. */
9153 static tree
9154 cp_parser_condition (cp_parser* parser)
9156 cp_decl_specifier_seq type_specifiers;
9157 const char *saved_message;
9158 int declares_class_or_enum;
9160 /* Try the declaration first. */
9161 cp_parser_parse_tentatively (parser);
9162 /* New types are not allowed in the type-specifier-seq for a
9163 condition. */
9164 saved_message = parser->type_definition_forbidden_message;
9165 parser->type_definition_forbidden_message
9166 = G_("types may not be defined in conditions");
9167 /* Parse the type-specifier-seq. */
9168 cp_parser_decl_specifier_seq (parser,
9169 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9170 &type_specifiers,
9171 &declares_class_or_enum);
9172 /* Restore the saved message. */
9173 parser->type_definition_forbidden_message = saved_message;
9174 /* If all is well, we might be looking at a declaration. */
9175 if (!cp_parser_error_occurred (parser))
9177 tree decl;
9178 tree asm_specification;
9179 tree attributes;
9180 cp_declarator *declarator;
9181 tree initializer = NULL_TREE;
9183 /* Parse the declarator. */
9184 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9185 /*ctor_dtor_or_conv_p=*/NULL,
9186 /*parenthesized_p=*/NULL,
9187 /*member_p=*/false);
9188 /* Parse the attributes. */
9189 attributes = cp_parser_attributes_opt (parser);
9190 /* Parse the asm-specification. */
9191 asm_specification = cp_parser_asm_specification_opt (parser);
9192 /* If the next token is not an `=' or '{', then we might still be
9193 looking at an expression. For example:
9195 if (A(a).x)
9197 looks like a decl-specifier-seq and a declarator -- but then
9198 there is no `=', so this is an expression. */
9199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9200 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9201 cp_parser_simulate_error (parser);
9203 /* If we did see an `=' or '{', then we are looking at a declaration
9204 for sure. */
9205 if (cp_parser_parse_definitely (parser))
9207 tree pushed_scope;
9208 bool non_constant_p;
9209 bool flags = LOOKUP_ONLYCONVERTING;
9211 /* Create the declaration. */
9212 decl = start_decl (declarator, &type_specifiers,
9213 /*initialized_p=*/true,
9214 attributes, /*prefix_attributes=*/NULL_TREE,
9215 &pushed_scope);
9217 /* Parse the initializer. */
9218 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9220 initializer = cp_parser_braced_list (parser, &non_constant_p);
9221 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9222 flags = 0;
9224 else
9226 /* Consume the `='. */
9227 cp_parser_require (parser, CPP_EQ, RT_EQ);
9228 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9230 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9231 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9233 /* Process the initializer. */
9234 cp_finish_decl (decl,
9235 initializer, !non_constant_p,
9236 asm_specification,
9237 flags);
9239 if (pushed_scope)
9240 pop_scope (pushed_scope);
9242 return convert_from_reference (decl);
9245 /* If we didn't even get past the declarator successfully, we are
9246 definitely not looking at a declaration. */
9247 else
9248 cp_parser_abort_tentative_parse (parser);
9250 /* Otherwise, we are looking at an expression. */
9251 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9254 /* Parses a for-statement or range-for-statement until the closing ')',
9255 not included. */
9257 static tree
9258 cp_parser_for (cp_parser *parser)
9260 tree init, scope, decl;
9261 bool is_range_for;
9263 /* Begin the for-statement. */
9264 scope = begin_for_scope (&init);
9266 /* Parse the initialization. */
9267 is_range_for = cp_parser_for_init_statement (parser, &decl);
9269 if (is_range_for)
9270 return cp_parser_range_for (parser, scope, init, decl);
9271 else
9272 return cp_parser_c_for (parser, scope, init);
9275 static tree
9276 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9278 /* Normal for loop */
9279 tree condition = NULL_TREE;
9280 tree expression = NULL_TREE;
9281 tree stmt;
9283 stmt = begin_for_stmt (scope, init);
9284 /* The for-init-statement has already been parsed in
9285 cp_parser_for_init_statement, so no work is needed here. */
9286 finish_for_init_stmt (stmt);
9288 /* If there's a condition, process it. */
9289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9290 condition = cp_parser_condition (parser);
9291 finish_for_cond (condition, stmt);
9292 /* Look for the `;'. */
9293 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9295 /* If there's an expression, process it. */
9296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9297 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9298 finish_for_expr (expression, stmt);
9300 return stmt;
9303 /* Tries to parse a range-based for-statement:
9305 range-based-for:
9306 decl-specifier-seq declarator : expression
9308 The decl-specifier-seq declarator and the `:' are already parsed by
9309 cp_parser_for_init_statement. If processing_template_decl it returns a
9310 newly created RANGE_FOR_STMT; if not, it is converted to a
9311 regular FOR_STMT. */
9313 static tree
9314 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9316 tree stmt, range_expr;
9318 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9320 bool expr_non_constant_p;
9321 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9323 else
9324 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9326 /* If in template, STMT is converted to a normal for-statement
9327 at instantiation. If not, it is done just ahead. */
9328 if (processing_template_decl)
9330 if (check_for_bare_parameter_packs (range_expr))
9331 range_expr = error_mark_node;
9332 stmt = begin_range_for_stmt (scope, init);
9333 finish_range_for_decl (stmt, range_decl, range_expr);
9334 if (!type_dependent_expression_p (range_expr)
9335 /* do_auto_deduction doesn't mess with template init-lists. */
9336 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9337 do_range_for_auto_deduction (range_decl, range_expr);
9339 else
9341 stmt = begin_for_stmt (scope, init);
9342 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9344 return stmt;
9347 /* Subroutine of cp_convert_range_for: given the initializer expression,
9348 builds up the range temporary. */
9350 static tree
9351 build_range_temp (tree range_expr)
9353 tree range_type, range_temp;
9355 /* Find out the type deduced by the declaration
9356 `auto &&__range = range_expr'. */
9357 range_type = cp_build_reference_type (make_auto (), true);
9358 range_type = do_auto_deduction (range_type, range_expr,
9359 type_uses_auto (range_type));
9361 /* Create the __range variable. */
9362 range_temp = build_decl (input_location, VAR_DECL,
9363 get_identifier ("__for_range"), range_type);
9364 TREE_USED (range_temp) = 1;
9365 DECL_ARTIFICIAL (range_temp) = 1;
9367 return range_temp;
9370 /* Used by cp_parser_range_for in template context: we aren't going to
9371 do a full conversion yet, but we still need to resolve auto in the
9372 type of the for-range-declaration if present. This is basically
9373 a shortcut version of cp_convert_range_for. */
9375 static void
9376 do_range_for_auto_deduction (tree decl, tree range_expr)
9378 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9379 if (auto_node)
9381 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9382 range_temp = convert_from_reference (build_range_temp (range_expr));
9383 iter_type = (cp_parser_perform_range_for_lookup
9384 (range_temp, &begin_dummy, &end_dummy));
9385 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9386 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9387 tf_warning_or_error);
9388 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9389 iter_decl, auto_node);
9393 /* Converts a range-based for-statement into a normal
9394 for-statement, as per the definition.
9396 for (RANGE_DECL : RANGE_EXPR)
9397 BLOCK
9399 should be equivalent to:
9402 auto &&__range = RANGE_EXPR;
9403 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9404 __begin != __end;
9405 ++__begin)
9407 RANGE_DECL = *__begin;
9408 BLOCK
9412 If RANGE_EXPR is an array:
9413 BEGIN_EXPR = __range
9414 END_EXPR = __range + ARRAY_SIZE(__range)
9415 Else if RANGE_EXPR has a member 'begin' or 'end':
9416 BEGIN_EXPR = __range.begin()
9417 END_EXPR = __range.end()
9418 Else:
9419 BEGIN_EXPR = begin(__range)
9420 END_EXPR = end(__range);
9422 If __range has a member 'begin' but not 'end', or vice versa, we must
9423 still use the second alternative (it will surely fail, however).
9424 When calling begin()/end() in the third alternative we must use
9425 argument dependent lookup, but always considering 'std' as an associated
9426 namespace. */
9428 tree
9429 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9431 tree begin, end;
9432 tree iter_type, begin_expr, end_expr;
9433 tree condition, expression;
9435 if (range_decl == error_mark_node || range_expr == error_mark_node)
9436 /* If an error happened previously do nothing or else a lot of
9437 unhelpful errors would be issued. */
9438 begin_expr = end_expr = iter_type = error_mark_node;
9439 else
9441 tree range_temp = build_range_temp (range_expr);
9442 pushdecl (range_temp);
9443 cp_finish_decl (range_temp, range_expr,
9444 /*is_constant_init*/false, NULL_TREE,
9445 LOOKUP_ONLYCONVERTING);
9447 range_temp = convert_from_reference (range_temp);
9448 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9449 &begin_expr, &end_expr);
9452 /* The new for initialization statement. */
9453 begin = build_decl (input_location, VAR_DECL,
9454 get_identifier ("__for_begin"), iter_type);
9455 TREE_USED (begin) = 1;
9456 DECL_ARTIFICIAL (begin) = 1;
9457 pushdecl (begin);
9458 cp_finish_decl (begin, begin_expr,
9459 /*is_constant_init*/false, NULL_TREE,
9460 LOOKUP_ONLYCONVERTING);
9462 end = build_decl (input_location, VAR_DECL,
9463 get_identifier ("__for_end"), iter_type);
9464 TREE_USED (end) = 1;
9465 DECL_ARTIFICIAL (end) = 1;
9466 pushdecl (end);
9467 cp_finish_decl (end, end_expr,
9468 /*is_constant_init*/false, NULL_TREE,
9469 LOOKUP_ONLYCONVERTING);
9471 finish_for_init_stmt (statement);
9473 /* The new for condition. */
9474 condition = build_x_binary_op (input_location, NE_EXPR,
9475 begin, ERROR_MARK,
9476 end, ERROR_MARK,
9477 NULL, tf_warning_or_error);
9478 finish_for_cond (condition, statement);
9480 /* The new increment expression. */
9481 expression = finish_unary_op_expr (input_location,
9482 PREINCREMENT_EXPR, begin);
9483 finish_for_expr (expression, statement);
9485 /* The declaration is initialized with *__begin inside the loop body. */
9486 cp_finish_decl (range_decl,
9487 build_x_indirect_ref (input_location, begin, RO_NULL,
9488 tf_warning_or_error),
9489 /*is_constant_init*/false, NULL_TREE,
9490 LOOKUP_ONLYCONVERTING);
9492 return statement;
9495 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9496 We need to solve both at the same time because the method used
9497 depends on the existence of members begin or end.
9498 Returns the type deduced for the iterator expression. */
9500 static tree
9501 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9503 if (error_operand_p (range))
9505 *begin = *end = error_mark_node;
9506 return error_mark_node;
9509 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9511 error ("range-based %<for%> expression of type %qT "
9512 "has incomplete type", TREE_TYPE (range));
9513 *begin = *end = error_mark_node;
9514 return error_mark_node;
9516 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9518 /* If RANGE is an array, we will use pointer arithmetic. */
9519 *begin = range;
9520 *end = build_binary_op (input_location, PLUS_EXPR,
9521 range,
9522 array_type_nelts_top (TREE_TYPE (range)),
9524 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9526 else
9528 /* If it is not an array, we must do a bit of magic. */
9529 tree id_begin, id_end;
9530 tree member_begin, member_end;
9532 *begin = *end = error_mark_node;
9534 id_begin = get_identifier ("begin");
9535 id_end = get_identifier ("end");
9536 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9537 /*protect=*/2, /*want_type=*/false,
9538 tf_warning_or_error);
9539 member_end = lookup_member (TREE_TYPE (range), id_end,
9540 /*protect=*/2, /*want_type=*/false,
9541 tf_warning_or_error);
9543 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9545 /* Use the member functions. */
9546 if (member_begin != NULL_TREE)
9547 *begin = cp_parser_range_for_member_function (range, id_begin);
9548 else
9549 error ("range-based %<for%> expression of type %qT has an "
9550 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9552 if (member_end != NULL_TREE)
9553 *end = cp_parser_range_for_member_function (range, id_end);
9554 else
9555 error ("range-based %<for%> expression of type %qT has a "
9556 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9558 else
9560 /* Use global functions with ADL. */
9561 VEC(tree,gc) *vec;
9562 vec = make_tree_vector ();
9564 VEC_safe_push (tree, gc, vec, range);
9566 member_begin = perform_koenig_lookup (id_begin, vec,
9567 /*include_std=*/true,
9568 tf_warning_or_error);
9569 *begin = finish_call_expr (member_begin, &vec, false, true,
9570 tf_warning_or_error);
9571 member_end = perform_koenig_lookup (id_end, vec,
9572 /*include_std=*/true,
9573 tf_warning_or_error);
9574 *end = finish_call_expr (member_end, &vec, false, true,
9575 tf_warning_or_error);
9577 release_tree_vector (vec);
9580 /* Last common checks. */
9581 if (*begin == error_mark_node || *end == error_mark_node)
9583 /* If one of the expressions is an error do no more checks. */
9584 *begin = *end = error_mark_node;
9585 return error_mark_node;
9587 else
9589 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9590 /* The unqualified type of the __begin and __end temporaries should
9591 be the same, as required by the multiple auto declaration. */
9592 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9593 error ("inconsistent begin/end types in range-based %<for%> "
9594 "statement: %qT and %qT",
9595 TREE_TYPE (*begin), TREE_TYPE (*end));
9596 return iter_type;
9601 /* Helper function for cp_parser_perform_range_for_lookup.
9602 Builds a tree for RANGE.IDENTIFIER(). */
9604 static tree
9605 cp_parser_range_for_member_function (tree range, tree identifier)
9607 tree member, res;
9608 VEC(tree,gc) *vec;
9610 member = finish_class_member_access_expr (range, identifier,
9611 false, tf_warning_or_error);
9612 if (member == error_mark_node)
9613 return error_mark_node;
9615 vec = make_tree_vector ();
9616 res = finish_call_expr (member, &vec,
9617 /*disallow_virtual=*/false,
9618 /*koenig_p=*/false,
9619 tf_warning_or_error);
9620 release_tree_vector (vec);
9621 return res;
9624 /* Parse an iteration-statement.
9626 iteration-statement:
9627 while ( condition ) statement
9628 do statement while ( expression ) ;
9629 for ( for-init-statement condition [opt] ; expression [opt] )
9630 statement
9632 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9634 static tree
9635 cp_parser_iteration_statement (cp_parser* parser)
9637 cp_token *token;
9638 enum rid keyword;
9639 tree statement;
9640 unsigned char in_statement;
9642 /* Peek at the next token. */
9643 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9644 if (!token)
9645 return error_mark_node;
9647 /* Remember whether or not we are already within an iteration
9648 statement. */
9649 in_statement = parser->in_statement;
9651 /* See what kind of keyword it is. */
9652 keyword = token->keyword;
9653 switch (keyword)
9655 case RID_WHILE:
9657 tree condition;
9659 /* Begin the while-statement. */
9660 statement = begin_while_stmt ();
9661 /* Look for the `('. */
9662 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9663 /* Parse the condition. */
9664 condition = cp_parser_condition (parser);
9665 finish_while_stmt_cond (condition, statement);
9666 /* Look for the `)'. */
9667 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9668 /* Parse the dependent statement. */
9669 parser->in_statement = IN_ITERATION_STMT;
9670 cp_parser_already_scoped_statement (parser);
9671 parser->in_statement = in_statement;
9672 /* We're done with the while-statement. */
9673 finish_while_stmt (statement);
9675 break;
9677 case RID_DO:
9679 tree expression;
9681 /* Begin the do-statement. */
9682 statement = begin_do_stmt ();
9683 /* Parse the body of the do-statement. */
9684 parser->in_statement = IN_ITERATION_STMT;
9685 cp_parser_implicitly_scoped_statement (parser, NULL);
9686 parser->in_statement = in_statement;
9687 finish_do_body (statement);
9688 /* Look for the `while' keyword. */
9689 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9690 /* Look for the `('. */
9691 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9692 /* Parse the expression. */
9693 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9694 /* We're done with the do-statement. */
9695 finish_do_stmt (expression, statement);
9696 /* Look for the `)'. */
9697 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9698 /* Look for the `;'. */
9699 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9701 break;
9703 case RID_FOR:
9705 /* Look for the `('. */
9706 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9708 statement = cp_parser_for (parser);
9710 /* Look for the `)'. */
9711 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9713 /* Parse the body of the for-statement. */
9714 parser->in_statement = IN_ITERATION_STMT;
9715 cp_parser_already_scoped_statement (parser);
9716 parser->in_statement = in_statement;
9718 /* We're done with the for-statement. */
9719 finish_for_stmt (statement);
9721 break;
9723 default:
9724 cp_parser_error (parser, "expected iteration-statement");
9725 statement = error_mark_node;
9726 break;
9729 return statement;
9732 /* Parse a for-init-statement or the declarator of a range-based-for.
9733 Returns true if a range-based-for declaration is seen.
9735 for-init-statement:
9736 expression-statement
9737 simple-declaration */
9739 static bool
9740 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9742 /* If the next token is a `;', then we have an empty
9743 expression-statement. Grammatically, this is also a
9744 simple-declaration, but an invalid one, because it does not
9745 declare anything. Therefore, if we did not handle this case
9746 specially, we would issue an error message about an invalid
9747 declaration. */
9748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9750 bool is_range_for = false;
9751 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9753 parser->colon_corrects_to_scope_p = false;
9755 /* We're going to speculatively look for a declaration, falling back
9756 to an expression, if necessary. */
9757 cp_parser_parse_tentatively (parser);
9758 /* Parse the declaration. */
9759 cp_parser_simple_declaration (parser,
9760 /*function_definition_allowed_p=*/false,
9761 decl);
9762 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9763 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9765 /* It is a range-for, consume the ':' */
9766 cp_lexer_consume_token (parser->lexer);
9767 is_range_for = true;
9768 if (cxx_dialect < cxx0x)
9770 error_at (cp_lexer_peek_token (parser->lexer)->location,
9771 "range-based %<for%> loops are not allowed "
9772 "in C++98 mode");
9773 *decl = error_mark_node;
9776 else
9777 /* The ';' is not consumed yet because we told
9778 cp_parser_simple_declaration not to. */
9779 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9781 if (cp_parser_parse_definitely (parser))
9782 return is_range_for;
9783 /* If the tentative parse failed, then we shall need to look for an
9784 expression-statement. */
9786 /* If we are here, it is an expression-statement. */
9787 cp_parser_expression_statement (parser, NULL_TREE);
9788 return false;
9791 /* Parse a jump-statement.
9793 jump-statement:
9794 break ;
9795 continue ;
9796 return expression [opt] ;
9797 return braced-init-list ;
9798 goto identifier ;
9800 GNU extension:
9802 jump-statement:
9803 goto * expression ;
9805 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9807 static tree
9808 cp_parser_jump_statement (cp_parser* parser)
9810 tree statement = error_mark_node;
9811 cp_token *token;
9812 enum rid keyword;
9813 unsigned char in_statement;
9815 /* Peek at the next token. */
9816 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9817 if (!token)
9818 return error_mark_node;
9820 /* See what kind of keyword it is. */
9821 keyword = token->keyword;
9822 switch (keyword)
9824 case RID_BREAK:
9825 in_statement = parser->in_statement & ~IN_IF_STMT;
9826 switch (in_statement)
9828 case 0:
9829 error_at (token->location, "break statement not within loop or switch");
9830 break;
9831 default:
9832 gcc_assert ((in_statement & IN_SWITCH_STMT)
9833 || in_statement == IN_ITERATION_STMT);
9834 statement = finish_break_stmt ();
9835 break;
9836 case IN_OMP_BLOCK:
9837 error_at (token->location, "invalid exit from OpenMP structured block");
9838 break;
9839 case IN_OMP_FOR:
9840 error_at (token->location, "break statement used with OpenMP for loop");
9841 break;
9843 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9844 break;
9846 case RID_CONTINUE:
9847 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9849 case 0:
9850 error_at (token->location, "continue statement not within a loop");
9851 break;
9852 case IN_ITERATION_STMT:
9853 case IN_OMP_FOR:
9854 statement = finish_continue_stmt ();
9855 break;
9856 case IN_OMP_BLOCK:
9857 error_at (token->location, "invalid exit from OpenMP structured block");
9858 break;
9859 default:
9860 gcc_unreachable ();
9862 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9863 break;
9865 case RID_RETURN:
9867 tree expr;
9868 bool expr_non_constant_p;
9870 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9872 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9873 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9875 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9876 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9877 else
9878 /* If the next token is a `;', then there is no
9879 expression. */
9880 expr = NULL_TREE;
9881 /* Build the return-statement. */
9882 statement = finish_return_stmt (expr);
9883 /* Look for the final `;'. */
9884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9886 break;
9888 case RID_GOTO:
9889 /* Create the goto-statement. */
9890 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9892 /* Issue a warning about this use of a GNU extension. */
9893 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
9894 /* Consume the '*' token. */
9895 cp_lexer_consume_token (parser->lexer);
9896 /* Parse the dependent expression. */
9897 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9899 else
9900 finish_goto_stmt (cp_parser_identifier (parser));
9901 /* Look for the final `;'. */
9902 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9903 break;
9905 default:
9906 cp_parser_error (parser, "expected jump-statement");
9907 break;
9910 return statement;
9913 /* Parse a declaration-statement.
9915 declaration-statement:
9916 block-declaration */
9918 static void
9919 cp_parser_declaration_statement (cp_parser* parser)
9921 void *p;
9923 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9924 p = obstack_alloc (&declarator_obstack, 0);
9926 /* Parse the block-declaration. */
9927 cp_parser_block_declaration (parser, /*statement_p=*/true);
9929 /* Free any declarators allocated. */
9930 obstack_free (&declarator_obstack, p);
9932 /* Finish off the statement. */
9933 finish_stmt ();
9936 /* Some dependent statements (like `if (cond) statement'), are
9937 implicitly in their own scope. In other words, if the statement is
9938 a single statement (as opposed to a compound-statement), it is
9939 none-the-less treated as if it were enclosed in braces. Any
9940 declarations appearing in the dependent statement are out of scope
9941 after control passes that point. This function parses a statement,
9942 but ensures that is in its own scope, even if it is not a
9943 compound-statement.
9945 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9946 is a (possibly labeled) if statement which is not enclosed in
9947 braces and has an else clause. This is used to implement
9948 -Wparentheses.
9950 Returns the new statement. */
9952 static tree
9953 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9955 tree statement;
9957 if (if_p != NULL)
9958 *if_p = false;
9960 /* Mark if () ; with a special NOP_EXPR. */
9961 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9963 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9964 cp_lexer_consume_token (parser->lexer);
9965 statement = add_stmt (build_empty_stmt (loc));
9967 /* if a compound is opened, we simply parse the statement directly. */
9968 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9969 statement = cp_parser_compound_statement (parser, NULL, false, false);
9970 /* If the token is not a `{', then we must take special action. */
9971 else
9973 /* Create a compound-statement. */
9974 statement = begin_compound_stmt (0);
9975 /* Parse the dependent-statement. */
9976 cp_parser_statement (parser, NULL_TREE, false, if_p);
9977 /* Finish the dummy compound-statement. */
9978 finish_compound_stmt (statement);
9981 /* Return the statement. */
9982 return statement;
9985 /* For some dependent statements (like `while (cond) statement'), we
9986 have already created a scope. Therefore, even if the dependent
9987 statement is a compound-statement, we do not want to create another
9988 scope. */
9990 static void
9991 cp_parser_already_scoped_statement (cp_parser* parser)
9993 /* If the token is a `{', then we must take special action. */
9994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9995 cp_parser_statement (parser, NULL_TREE, false, NULL);
9996 else
9998 /* Avoid calling cp_parser_compound_statement, so that we
9999 don't create a new scope. Do everything else by hand. */
10000 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10001 /* If the next keyword is `__label__' we have a label declaration. */
10002 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10003 cp_parser_label_declaration (parser);
10004 /* Parse an (optional) statement-seq. */
10005 cp_parser_statement_seq_opt (parser, NULL_TREE);
10006 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10010 /* Declarations [gram.dcl.dcl] */
10012 /* Parse an optional declaration-sequence.
10014 declaration-seq:
10015 declaration
10016 declaration-seq declaration */
10018 static void
10019 cp_parser_declaration_seq_opt (cp_parser* parser)
10021 while (true)
10023 cp_token *token;
10025 token = cp_lexer_peek_token (parser->lexer);
10027 if (token->type == CPP_CLOSE_BRACE
10028 || token->type == CPP_EOF
10029 || token->type == CPP_PRAGMA_EOL)
10030 break;
10032 if (token->type == CPP_SEMICOLON)
10034 /* A declaration consisting of a single semicolon is
10035 invalid. Allow it unless we're being pedantic. */
10036 cp_lexer_consume_token (parser->lexer);
10037 if (!in_system_header)
10038 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10039 continue;
10042 /* If we're entering or exiting a region that's implicitly
10043 extern "C", modify the lang context appropriately. */
10044 if (!parser->implicit_extern_c && token->implicit_extern_c)
10046 push_lang_context (lang_name_c);
10047 parser->implicit_extern_c = true;
10049 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10051 pop_lang_context ();
10052 parser->implicit_extern_c = false;
10055 if (token->type == CPP_PRAGMA)
10057 /* A top-level declaration can consist solely of a #pragma.
10058 A nested declaration cannot, so this is done here and not
10059 in cp_parser_declaration. (A #pragma at block scope is
10060 handled in cp_parser_statement.) */
10061 cp_parser_pragma (parser, pragma_external);
10062 continue;
10065 /* Parse the declaration itself. */
10066 cp_parser_declaration (parser);
10070 /* Parse a declaration.
10072 declaration:
10073 block-declaration
10074 function-definition
10075 template-declaration
10076 explicit-instantiation
10077 explicit-specialization
10078 linkage-specification
10079 namespace-definition
10081 GNU extension:
10083 declaration:
10084 __extension__ declaration */
10086 static void
10087 cp_parser_declaration (cp_parser* parser)
10089 cp_token token1;
10090 cp_token token2;
10091 int saved_pedantic;
10092 void *p;
10093 tree attributes = NULL_TREE;
10095 /* Check for the `__extension__' keyword. */
10096 if (cp_parser_extension_opt (parser, &saved_pedantic))
10098 /* Parse the qualified declaration. */
10099 cp_parser_declaration (parser);
10100 /* Restore the PEDANTIC flag. */
10101 pedantic = saved_pedantic;
10103 return;
10106 /* Try to figure out what kind of declaration is present. */
10107 token1 = *cp_lexer_peek_token (parser->lexer);
10109 if (token1.type != CPP_EOF)
10110 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10111 else
10113 token2.type = CPP_EOF;
10114 token2.keyword = RID_MAX;
10117 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10118 p = obstack_alloc (&declarator_obstack, 0);
10120 /* If the next token is `extern' and the following token is a string
10121 literal, then we have a linkage specification. */
10122 if (token1.keyword == RID_EXTERN
10123 && cp_parser_is_pure_string_literal (&token2))
10124 cp_parser_linkage_specification (parser);
10125 /* If the next token is `template', then we have either a template
10126 declaration, an explicit instantiation, or an explicit
10127 specialization. */
10128 else if (token1.keyword == RID_TEMPLATE)
10130 /* `template <>' indicates a template specialization. */
10131 if (token2.type == CPP_LESS
10132 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10133 cp_parser_explicit_specialization (parser);
10134 /* `template <' indicates a template declaration. */
10135 else if (token2.type == CPP_LESS)
10136 cp_parser_template_declaration (parser, /*member_p=*/false);
10137 /* Anything else must be an explicit instantiation. */
10138 else
10139 cp_parser_explicit_instantiation (parser);
10141 /* If the next token is `export', then we have a template
10142 declaration. */
10143 else if (token1.keyword == RID_EXPORT)
10144 cp_parser_template_declaration (parser, /*member_p=*/false);
10145 /* If the next token is `extern', 'static' or 'inline' and the one
10146 after that is `template', we have a GNU extended explicit
10147 instantiation directive. */
10148 else if (cp_parser_allow_gnu_extensions_p (parser)
10149 && (token1.keyword == RID_EXTERN
10150 || token1.keyword == RID_STATIC
10151 || token1.keyword == RID_INLINE)
10152 && token2.keyword == RID_TEMPLATE)
10153 cp_parser_explicit_instantiation (parser);
10154 /* If the next token is `namespace', check for a named or unnamed
10155 namespace definition. */
10156 else if (token1.keyword == RID_NAMESPACE
10157 && (/* A named namespace definition. */
10158 (token2.type == CPP_NAME
10159 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10160 != CPP_EQ))
10161 /* An unnamed namespace definition. */
10162 || token2.type == CPP_OPEN_BRACE
10163 || token2.keyword == RID_ATTRIBUTE))
10164 cp_parser_namespace_definition (parser);
10165 /* An inline (associated) namespace definition. */
10166 else if (token1.keyword == RID_INLINE
10167 && token2.keyword == RID_NAMESPACE)
10168 cp_parser_namespace_definition (parser);
10169 /* Objective-C++ declaration/definition. */
10170 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10171 cp_parser_objc_declaration (parser, NULL_TREE);
10172 else if (c_dialect_objc ()
10173 && token1.keyword == RID_ATTRIBUTE
10174 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10175 cp_parser_objc_declaration (parser, attributes);
10176 /* We must have either a block declaration or a function
10177 definition. */
10178 else
10179 /* Try to parse a block-declaration, or a function-definition. */
10180 cp_parser_block_declaration (parser, /*statement_p=*/false);
10182 /* Free any declarators allocated. */
10183 obstack_free (&declarator_obstack, p);
10186 /* Parse a block-declaration.
10188 block-declaration:
10189 simple-declaration
10190 asm-definition
10191 namespace-alias-definition
10192 using-declaration
10193 using-directive
10195 GNU Extension:
10197 block-declaration:
10198 __extension__ block-declaration
10200 C++0x Extension:
10202 block-declaration:
10203 static_assert-declaration
10205 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10206 part of a declaration-statement. */
10208 static void
10209 cp_parser_block_declaration (cp_parser *parser,
10210 bool statement_p)
10212 cp_token *token1;
10213 int saved_pedantic;
10215 /* Check for the `__extension__' keyword. */
10216 if (cp_parser_extension_opt (parser, &saved_pedantic))
10218 /* Parse the qualified declaration. */
10219 cp_parser_block_declaration (parser, statement_p);
10220 /* Restore the PEDANTIC flag. */
10221 pedantic = saved_pedantic;
10223 return;
10226 /* Peek at the next token to figure out which kind of declaration is
10227 present. */
10228 token1 = cp_lexer_peek_token (parser->lexer);
10230 /* If the next keyword is `asm', we have an asm-definition. */
10231 if (token1->keyword == RID_ASM)
10233 if (statement_p)
10234 cp_parser_commit_to_tentative_parse (parser);
10235 cp_parser_asm_definition (parser);
10237 /* If the next keyword is `namespace', we have a
10238 namespace-alias-definition. */
10239 else if (token1->keyword == RID_NAMESPACE)
10240 cp_parser_namespace_alias_definition (parser);
10241 /* If the next keyword is `using', we have a
10242 using-declaration, a using-directive, or an alias-declaration. */
10243 else if (token1->keyword == RID_USING)
10245 cp_token *token2;
10247 if (statement_p)
10248 cp_parser_commit_to_tentative_parse (parser);
10249 /* If the token after `using' is `namespace', then we have a
10250 using-directive. */
10251 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10252 if (token2->keyword == RID_NAMESPACE)
10253 cp_parser_using_directive (parser);
10254 /* If the second token after 'using' is '=', then we have an
10255 alias-declaration. */
10256 else if (cxx_dialect >= cxx0x
10257 && token2->type == CPP_NAME
10258 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10259 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10260 == RID_ATTRIBUTE)))
10261 cp_parser_alias_declaration (parser);
10262 /* Otherwise, it's a using-declaration. */
10263 else
10264 cp_parser_using_declaration (parser,
10265 /*access_declaration_p=*/false);
10267 /* If the next keyword is `__label__' we have a misplaced label
10268 declaration. */
10269 else if (token1->keyword == RID_LABEL)
10271 cp_lexer_consume_token (parser->lexer);
10272 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10273 cp_parser_skip_to_end_of_statement (parser);
10274 /* If the next token is now a `;', consume it. */
10275 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10276 cp_lexer_consume_token (parser->lexer);
10278 /* If the next token is `static_assert' we have a static assertion. */
10279 else if (token1->keyword == RID_STATIC_ASSERT)
10280 cp_parser_static_assert (parser, /*member_p=*/false);
10281 /* Anything else must be a simple-declaration. */
10282 else
10283 cp_parser_simple_declaration (parser, !statement_p,
10284 /*maybe_range_for_decl*/NULL);
10287 /* Parse a simple-declaration.
10289 simple-declaration:
10290 decl-specifier-seq [opt] init-declarator-list [opt] ;
10292 init-declarator-list:
10293 init-declarator
10294 init-declarator-list , init-declarator
10296 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10297 function-definition as a simple-declaration.
10299 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10300 parsed declaration if it is an uninitialized single declarator not followed
10301 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10302 if present, will not be consumed. */
10304 static void
10305 cp_parser_simple_declaration (cp_parser* parser,
10306 bool function_definition_allowed_p,
10307 tree *maybe_range_for_decl)
10309 cp_decl_specifier_seq decl_specifiers;
10310 int declares_class_or_enum;
10311 bool saw_declarator;
10313 if (maybe_range_for_decl)
10314 *maybe_range_for_decl = NULL_TREE;
10316 /* Defer access checks until we know what is being declared; the
10317 checks for names appearing in the decl-specifier-seq should be
10318 done as if we were in the scope of the thing being declared. */
10319 push_deferring_access_checks (dk_deferred);
10321 /* Parse the decl-specifier-seq. We have to keep track of whether
10322 or not the decl-specifier-seq declares a named class or
10323 enumeration type, since that is the only case in which the
10324 init-declarator-list is allowed to be empty.
10326 [dcl.dcl]
10328 In a simple-declaration, the optional init-declarator-list can be
10329 omitted only when declaring a class or enumeration, that is when
10330 the decl-specifier-seq contains either a class-specifier, an
10331 elaborated-type-specifier, or an enum-specifier. */
10332 cp_parser_decl_specifier_seq (parser,
10333 CP_PARSER_FLAGS_OPTIONAL,
10334 &decl_specifiers,
10335 &declares_class_or_enum);
10336 /* We no longer need to defer access checks. */
10337 stop_deferring_access_checks ();
10339 /* In a block scope, a valid declaration must always have a
10340 decl-specifier-seq. By not trying to parse declarators, we can
10341 resolve the declaration/expression ambiguity more quickly. */
10342 if (!function_definition_allowed_p
10343 && !decl_specifiers.any_specifiers_p)
10345 cp_parser_error (parser, "expected declaration");
10346 goto done;
10349 /* If the next two tokens are both identifiers, the code is
10350 erroneous. The usual cause of this situation is code like:
10352 T t;
10354 where "T" should name a type -- but does not. */
10355 if (!decl_specifiers.any_type_specifiers_p
10356 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10358 /* If parsing tentatively, we should commit; we really are
10359 looking at a declaration. */
10360 cp_parser_commit_to_tentative_parse (parser);
10361 /* Give up. */
10362 goto done;
10365 /* If we have seen at least one decl-specifier, and the next token
10366 is not a parenthesis, then we must be looking at a declaration.
10367 (After "int (" we might be looking at a functional cast.) */
10368 if (decl_specifiers.any_specifiers_p
10369 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10370 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10371 && !cp_parser_error_occurred (parser))
10372 cp_parser_commit_to_tentative_parse (parser);
10374 /* Keep going until we hit the `;' at the end of the simple
10375 declaration. */
10376 saw_declarator = false;
10377 while (cp_lexer_next_token_is_not (parser->lexer,
10378 CPP_SEMICOLON))
10380 cp_token *token;
10381 bool function_definition_p;
10382 tree decl;
10384 if (saw_declarator)
10386 /* If we are processing next declarator, coma is expected */
10387 token = cp_lexer_peek_token (parser->lexer);
10388 gcc_assert (token->type == CPP_COMMA);
10389 cp_lexer_consume_token (parser->lexer);
10390 if (maybe_range_for_decl)
10391 *maybe_range_for_decl = error_mark_node;
10393 else
10394 saw_declarator = true;
10396 /* Parse the init-declarator. */
10397 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10398 /*checks=*/NULL,
10399 function_definition_allowed_p,
10400 /*member_p=*/false,
10401 declares_class_or_enum,
10402 &function_definition_p,
10403 maybe_range_for_decl);
10404 /* If an error occurred while parsing tentatively, exit quickly.
10405 (That usually happens when in the body of a function; each
10406 statement is treated as a declaration-statement until proven
10407 otherwise.) */
10408 if (cp_parser_error_occurred (parser))
10409 goto done;
10410 /* Handle function definitions specially. */
10411 if (function_definition_p)
10413 /* If the next token is a `,', then we are probably
10414 processing something like:
10416 void f() {}, *p;
10418 which is erroneous. */
10419 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10421 cp_token *token = cp_lexer_peek_token (parser->lexer);
10422 error_at (token->location,
10423 "mixing"
10424 " declarations and function-definitions is forbidden");
10426 /* Otherwise, we're done with the list of declarators. */
10427 else
10429 pop_deferring_access_checks ();
10430 return;
10433 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10434 *maybe_range_for_decl = decl;
10435 /* The next token should be either a `,' or a `;'. */
10436 token = cp_lexer_peek_token (parser->lexer);
10437 /* If it's a `,', there are more declarators to come. */
10438 if (token->type == CPP_COMMA)
10439 /* will be consumed next time around */;
10440 /* If it's a `;', we are done. */
10441 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10442 break;
10443 /* Anything else is an error. */
10444 else
10446 /* If we have already issued an error message we don't need
10447 to issue another one. */
10448 if (decl != error_mark_node
10449 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10450 cp_parser_error (parser, "expected %<,%> or %<;%>");
10451 /* Skip tokens until we reach the end of the statement. */
10452 cp_parser_skip_to_end_of_statement (parser);
10453 /* If the next token is now a `;', consume it. */
10454 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10455 cp_lexer_consume_token (parser->lexer);
10456 goto done;
10458 /* After the first time around, a function-definition is not
10459 allowed -- even if it was OK at first. For example:
10461 int i, f() {}
10463 is not valid. */
10464 function_definition_allowed_p = false;
10467 /* Issue an error message if no declarators are present, and the
10468 decl-specifier-seq does not itself declare a class or
10469 enumeration. */
10470 if (!saw_declarator)
10472 if (cp_parser_declares_only_class_p (parser))
10473 shadow_tag (&decl_specifiers);
10474 /* Perform any deferred access checks. */
10475 perform_deferred_access_checks ();
10478 /* Consume the `;'. */
10479 if (!maybe_range_for_decl)
10480 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10482 done:
10483 pop_deferring_access_checks ();
10486 /* Parse a decl-specifier-seq.
10488 decl-specifier-seq:
10489 decl-specifier-seq [opt] decl-specifier
10491 decl-specifier:
10492 storage-class-specifier
10493 type-specifier
10494 function-specifier
10495 friend
10496 typedef
10498 GNU Extension:
10500 decl-specifier:
10501 attributes
10503 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10505 The parser flags FLAGS is used to control type-specifier parsing.
10507 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10508 flags:
10510 1: one of the decl-specifiers is an elaborated-type-specifier
10511 (i.e., a type declaration)
10512 2: one of the decl-specifiers is an enum-specifier or a
10513 class-specifier (i.e., a type definition)
10517 static void
10518 cp_parser_decl_specifier_seq (cp_parser* parser,
10519 cp_parser_flags flags,
10520 cp_decl_specifier_seq *decl_specs,
10521 int* declares_class_or_enum)
10523 bool constructor_possible_p = !parser->in_declarator_p;
10524 cp_token *start_token = NULL;
10525 cp_decl_spec ds;
10527 /* Clear DECL_SPECS. */
10528 clear_decl_specs (decl_specs);
10530 /* Assume no class or enumeration type is declared. */
10531 *declares_class_or_enum = 0;
10533 /* Keep reading specifiers until there are no more to read. */
10534 while (true)
10536 bool constructor_p;
10537 bool found_decl_spec;
10538 cp_token *token;
10539 ds = ds_last;
10541 /* Peek at the next token. */
10542 token = cp_lexer_peek_token (parser->lexer);
10544 /* Save the first token of the decl spec list for error
10545 reporting. */
10546 if (!start_token)
10547 start_token = token;
10548 /* Handle attributes. */
10549 if (token->keyword == RID_ATTRIBUTE)
10551 /* Parse the attributes. */
10552 decl_specs->attributes
10553 = chainon (decl_specs->attributes,
10554 cp_parser_attributes_opt (parser));
10555 if (decl_specs->locations[ds_attribute] == 0)
10556 decl_specs->locations[ds_attribute] = token->location;
10557 continue;
10559 /* Assume we will find a decl-specifier keyword. */
10560 found_decl_spec = true;
10561 /* If the next token is an appropriate keyword, we can simply
10562 add it to the list. */
10563 switch (token->keyword)
10565 /* decl-specifier:
10566 friend
10567 constexpr */
10568 case RID_FRIEND:
10569 if (!at_class_scope_p ())
10571 error_at (token->location, "%<friend%> used outside of class");
10572 cp_lexer_purge_token (parser->lexer);
10574 else
10576 ds = ds_friend;
10577 /* Consume the token. */
10578 cp_lexer_consume_token (parser->lexer);
10580 break;
10582 case RID_CONSTEXPR:
10583 ds = ds_constexpr;
10584 cp_lexer_consume_token (parser->lexer);
10585 break;
10587 /* function-specifier:
10588 inline
10589 virtual
10590 explicit */
10591 case RID_INLINE:
10592 case RID_VIRTUAL:
10593 case RID_EXPLICIT:
10594 cp_parser_function_specifier_opt (parser, decl_specs);
10595 break;
10597 /* decl-specifier:
10598 typedef */
10599 case RID_TYPEDEF:
10600 ds = ds_typedef;
10601 /* Consume the token. */
10602 cp_lexer_consume_token (parser->lexer);
10603 /* A constructor declarator cannot appear in a typedef. */
10604 constructor_possible_p = false;
10605 /* The "typedef" keyword can only occur in a declaration; we
10606 may as well commit at this point. */
10607 cp_parser_commit_to_tentative_parse (parser);
10609 if (decl_specs->storage_class != sc_none)
10610 decl_specs->conflicting_specifiers_p = true;
10611 break;
10613 /* storage-class-specifier:
10614 auto
10615 register
10616 static
10617 extern
10618 mutable
10620 GNU Extension:
10621 thread */
10622 case RID_AUTO:
10623 if (cxx_dialect == cxx98)
10625 /* Consume the token. */
10626 cp_lexer_consume_token (parser->lexer);
10628 /* Complain about `auto' as a storage specifier, if
10629 we're complaining about C++0x compatibility. */
10630 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10631 " changes meaning in C++11; please remove it");
10633 /* Set the storage class anyway. */
10634 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10635 token->location);
10637 else
10638 /* C++0x auto type-specifier. */
10639 found_decl_spec = false;
10640 break;
10642 case RID_REGISTER:
10643 case RID_STATIC:
10644 case RID_EXTERN:
10645 case RID_MUTABLE:
10646 /* Consume the token. */
10647 cp_lexer_consume_token (parser->lexer);
10648 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10649 token->location);
10650 break;
10651 case RID_THREAD:
10652 /* Consume the token. */
10653 ds = ds_thread;
10654 cp_lexer_consume_token (parser->lexer);
10655 break;
10657 default:
10658 /* We did not yet find a decl-specifier yet. */
10659 found_decl_spec = false;
10660 break;
10663 if (found_decl_spec
10664 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10665 && token->keyword != RID_CONSTEXPR)
10666 error ("decl-specifier invalid in condition");
10668 if (ds != ds_last)
10669 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
10671 /* Constructors are a special case. The `S' in `S()' is not a
10672 decl-specifier; it is the beginning of the declarator. */
10673 constructor_p
10674 = (!found_decl_spec
10675 && constructor_possible_p
10676 && (cp_parser_constructor_declarator_p
10677 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10679 /* If we don't have a DECL_SPEC yet, then we must be looking at
10680 a type-specifier. */
10681 if (!found_decl_spec && !constructor_p)
10683 int decl_spec_declares_class_or_enum;
10684 bool is_cv_qualifier;
10685 tree type_spec;
10687 type_spec
10688 = cp_parser_type_specifier (parser, flags,
10689 decl_specs,
10690 /*is_declaration=*/true,
10691 &decl_spec_declares_class_or_enum,
10692 &is_cv_qualifier);
10693 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10695 /* If this type-specifier referenced a user-defined type
10696 (a typedef, class-name, etc.), then we can't allow any
10697 more such type-specifiers henceforth.
10699 [dcl.spec]
10701 The longest sequence of decl-specifiers that could
10702 possibly be a type name is taken as the
10703 decl-specifier-seq of a declaration. The sequence shall
10704 be self-consistent as described below.
10706 [dcl.type]
10708 As a general rule, at most one type-specifier is allowed
10709 in the complete decl-specifier-seq of a declaration. The
10710 only exceptions are the following:
10712 -- const or volatile can be combined with any other
10713 type-specifier.
10715 -- signed or unsigned can be combined with char, long,
10716 short, or int.
10718 -- ..
10720 Example:
10722 typedef char* Pc;
10723 void g (const int Pc);
10725 Here, Pc is *not* part of the decl-specifier seq; it's
10726 the declarator. Therefore, once we see a type-specifier
10727 (other than a cv-qualifier), we forbid any additional
10728 user-defined types. We *do* still allow things like `int
10729 int' to be considered a decl-specifier-seq, and issue the
10730 error message later. */
10731 if (type_spec && !is_cv_qualifier)
10732 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10733 /* A constructor declarator cannot follow a type-specifier. */
10734 if (type_spec)
10736 constructor_possible_p = false;
10737 found_decl_spec = true;
10738 if (!is_cv_qualifier)
10739 decl_specs->any_type_specifiers_p = true;
10743 /* If we still do not have a DECL_SPEC, then there are no more
10744 decl-specifiers. */
10745 if (!found_decl_spec)
10746 break;
10748 decl_specs->any_specifiers_p = true;
10749 /* After we see one decl-specifier, further decl-specifiers are
10750 always optional. */
10751 flags |= CP_PARSER_FLAGS_OPTIONAL;
10754 /* Don't allow a friend specifier with a class definition. */
10755 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10756 && (*declares_class_or_enum & 2))
10757 error_at (decl_specs->locations[ds_friend],
10758 "class definition may not be declared a friend");
10761 /* Parse an (optional) storage-class-specifier.
10763 storage-class-specifier:
10764 auto
10765 register
10766 static
10767 extern
10768 mutable
10770 GNU Extension:
10772 storage-class-specifier:
10773 thread
10775 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10777 static tree
10778 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10780 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10782 case RID_AUTO:
10783 if (cxx_dialect != cxx98)
10784 return NULL_TREE;
10785 /* Fall through for C++98. */
10787 case RID_REGISTER:
10788 case RID_STATIC:
10789 case RID_EXTERN:
10790 case RID_MUTABLE:
10791 case RID_THREAD:
10792 /* Consume the token. */
10793 return cp_lexer_consume_token (parser->lexer)->u.value;
10795 default:
10796 return NULL_TREE;
10800 /* Parse an (optional) function-specifier.
10802 function-specifier:
10803 inline
10804 virtual
10805 explicit
10807 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10808 Updates DECL_SPECS, if it is non-NULL. */
10810 static tree
10811 cp_parser_function_specifier_opt (cp_parser* parser,
10812 cp_decl_specifier_seq *decl_specs)
10814 cp_token *token = cp_lexer_peek_token (parser->lexer);
10815 switch (token->keyword)
10817 case RID_INLINE:
10818 set_and_check_decl_spec_loc (decl_specs, ds_inline, token->location);
10819 break;
10821 case RID_VIRTUAL:
10822 /* 14.5.2.3 [temp.mem]
10824 A member function template shall not be virtual. */
10825 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10826 error_at (token->location, "templates may not be %<virtual%>");
10827 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token->location);
10828 break;
10830 case RID_EXPLICIT:
10831 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token->location);
10832 break;
10834 default:
10835 return NULL_TREE;
10838 /* Consume the token. */
10839 return cp_lexer_consume_token (parser->lexer)->u.value;
10842 /* Parse a linkage-specification.
10844 linkage-specification:
10845 extern string-literal { declaration-seq [opt] }
10846 extern string-literal declaration */
10848 static void
10849 cp_parser_linkage_specification (cp_parser* parser)
10851 tree linkage;
10853 /* Look for the `extern' keyword. */
10854 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10856 /* Look for the string-literal. */
10857 linkage = cp_parser_string_literal (parser, false, false);
10859 /* Transform the literal into an identifier. If the literal is a
10860 wide-character string, or contains embedded NULs, then we can't
10861 handle it as the user wants. */
10862 if (strlen (TREE_STRING_POINTER (linkage))
10863 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10865 cp_parser_error (parser, "invalid linkage-specification");
10866 /* Assume C++ linkage. */
10867 linkage = lang_name_cplusplus;
10869 else
10870 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10872 /* We're now using the new linkage. */
10873 push_lang_context (linkage);
10875 /* If the next token is a `{', then we're using the first
10876 production. */
10877 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10879 /* Consume the `{' token. */
10880 cp_lexer_consume_token (parser->lexer);
10881 /* Parse the declarations. */
10882 cp_parser_declaration_seq_opt (parser);
10883 /* Look for the closing `}'. */
10884 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10886 /* Otherwise, there's just one declaration. */
10887 else
10889 bool saved_in_unbraced_linkage_specification_p;
10891 saved_in_unbraced_linkage_specification_p
10892 = parser->in_unbraced_linkage_specification_p;
10893 parser->in_unbraced_linkage_specification_p = true;
10894 cp_parser_declaration (parser);
10895 parser->in_unbraced_linkage_specification_p
10896 = saved_in_unbraced_linkage_specification_p;
10899 /* We're done with the linkage-specification. */
10900 pop_lang_context ();
10903 /* Parse a static_assert-declaration.
10905 static_assert-declaration:
10906 static_assert ( constant-expression , string-literal ) ;
10908 If MEMBER_P, this static_assert is a class member. */
10910 static void
10911 cp_parser_static_assert(cp_parser *parser, bool member_p)
10913 tree condition;
10914 tree message;
10915 cp_token *token;
10916 location_t saved_loc;
10917 bool dummy;
10919 /* Peek at the `static_assert' token so we can keep track of exactly
10920 where the static assertion started. */
10921 token = cp_lexer_peek_token (parser->lexer);
10922 saved_loc = token->location;
10924 /* Look for the `static_assert' keyword. */
10925 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10926 RT_STATIC_ASSERT))
10927 return;
10929 /* We know we are in a static assertion; commit to any tentative
10930 parse. */
10931 if (cp_parser_parsing_tentatively (parser))
10932 cp_parser_commit_to_tentative_parse (parser);
10934 /* Parse the `(' starting the static assertion condition. */
10935 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10937 /* Parse the constant-expression. Allow a non-constant expression
10938 here in order to give better diagnostics in finish_static_assert. */
10939 condition =
10940 cp_parser_constant_expression (parser,
10941 /*allow_non_constant_p=*/true,
10942 /*non_constant_p=*/&dummy);
10944 /* Parse the separating `,'. */
10945 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10947 /* Parse the string-literal message. */
10948 message = cp_parser_string_literal (parser,
10949 /*translate=*/false,
10950 /*wide_ok=*/true);
10952 /* A `)' completes the static assertion. */
10953 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10954 cp_parser_skip_to_closing_parenthesis (parser,
10955 /*recovering=*/true,
10956 /*or_comma=*/false,
10957 /*consume_paren=*/true);
10959 /* A semicolon terminates the declaration. */
10960 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10962 /* Complete the static assertion, which may mean either processing
10963 the static assert now or saving it for template instantiation. */
10964 finish_static_assert (condition, message, saved_loc, member_p);
10967 /* Parse a `decltype' type. Returns the type.
10969 simple-type-specifier:
10970 decltype ( expression ) */
10972 static tree
10973 cp_parser_decltype (cp_parser *parser)
10975 tree expr;
10976 bool id_expression_or_member_access_p = false;
10977 const char *saved_message;
10978 bool saved_integral_constant_expression_p;
10979 bool saved_non_integral_constant_expression_p;
10980 cp_token *id_expr_start_token;
10981 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10983 if (start_token->type == CPP_DECLTYPE)
10985 /* Already parsed. */
10986 cp_lexer_consume_token (parser->lexer);
10987 return start_token->u.value;
10990 /* Look for the `decltype' token. */
10991 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10992 return error_mark_node;
10994 /* Types cannot be defined in a `decltype' expression. Save away the
10995 old message. */
10996 saved_message = parser->type_definition_forbidden_message;
10998 /* And create the new one. */
10999 parser->type_definition_forbidden_message
11000 = G_("types may not be defined in %<decltype%> expressions");
11002 /* The restrictions on constant-expressions do not apply inside
11003 decltype expressions. */
11004 saved_integral_constant_expression_p
11005 = parser->integral_constant_expression_p;
11006 saved_non_integral_constant_expression_p
11007 = parser->non_integral_constant_expression_p;
11008 parser->integral_constant_expression_p = false;
11010 /* Do not actually evaluate the expression. */
11011 ++cp_unevaluated_operand;
11013 /* Do not warn about problems with the expression. */
11014 ++c_inhibit_evaluation_warnings;
11016 /* Parse the opening `('. */
11017 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11018 return error_mark_node;
11020 /* First, try parsing an id-expression. */
11021 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11022 cp_parser_parse_tentatively (parser);
11023 expr = cp_parser_id_expression (parser,
11024 /*template_keyword_p=*/false,
11025 /*check_dependency_p=*/true,
11026 /*template_p=*/NULL,
11027 /*declarator_p=*/false,
11028 /*optional_p=*/false);
11030 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11032 bool non_integral_constant_expression_p = false;
11033 tree id_expression = expr;
11034 cp_id_kind idk;
11035 const char *error_msg;
11037 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11038 /* Lookup the name we got back from the id-expression. */
11039 expr = cp_parser_lookup_name (parser, expr,
11040 none_type,
11041 /*is_template=*/false,
11042 /*is_namespace=*/false,
11043 /*check_dependency=*/true,
11044 /*ambiguous_decls=*/NULL,
11045 id_expr_start_token->location);
11047 if (expr
11048 && expr != error_mark_node
11049 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11050 && TREE_CODE (expr) != TYPE_DECL
11051 && (TREE_CODE (expr) != BIT_NOT_EXPR
11052 || !TYPE_P (TREE_OPERAND (expr, 0)))
11053 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11055 /* Complete lookup of the id-expression. */
11056 expr = (finish_id_expression
11057 (id_expression, expr, parser->scope, &idk,
11058 /*integral_constant_expression_p=*/false,
11059 /*allow_non_integral_constant_expression_p=*/true,
11060 &non_integral_constant_expression_p,
11061 /*template_p=*/false,
11062 /*done=*/true,
11063 /*address_p=*/false,
11064 /*template_arg_p=*/false,
11065 &error_msg,
11066 id_expr_start_token->location));
11068 if (expr == error_mark_node)
11069 /* We found an id-expression, but it was something that we
11070 should not have found. This is an error, not something
11071 we can recover from, so note that we found an
11072 id-expression and we'll recover as gracefully as
11073 possible. */
11074 id_expression_or_member_access_p = true;
11077 if (expr
11078 && expr != error_mark_node
11079 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11080 /* We have an id-expression. */
11081 id_expression_or_member_access_p = true;
11084 if (!id_expression_or_member_access_p)
11086 /* Abort the id-expression parse. */
11087 cp_parser_abort_tentative_parse (parser);
11089 /* Parsing tentatively, again. */
11090 cp_parser_parse_tentatively (parser);
11092 /* Parse a class member access. */
11093 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11094 /*cast_p=*/false,
11095 /*member_access_only_p=*/true, NULL);
11097 if (expr
11098 && expr != error_mark_node
11099 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11100 /* We have an id-expression. */
11101 id_expression_or_member_access_p = true;
11104 if (id_expression_or_member_access_p)
11105 /* We have parsed the complete id-expression or member access. */
11106 cp_parser_parse_definitely (parser);
11107 else
11109 bool saved_greater_than_is_operator_p;
11111 /* Abort our attempt to parse an id-expression or member access
11112 expression. */
11113 cp_parser_abort_tentative_parse (parser);
11115 /* Within a parenthesized expression, a `>' token is always
11116 the greater-than operator. */
11117 saved_greater_than_is_operator_p
11118 = parser->greater_than_is_operator_p;
11119 parser->greater_than_is_operator_p = true;
11121 /* Parse a full expression. */
11122 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11124 /* The `>' token might be the end of a template-id or
11125 template-parameter-list now. */
11126 parser->greater_than_is_operator_p
11127 = saved_greater_than_is_operator_p;
11130 /* Go back to evaluating expressions. */
11131 --cp_unevaluated_operand;
11132 --c_inhibit_evaluation_warnings;
11134 /* Restore the old message and the integral constant expression
11135 flags. */
11136 parser->type_definition_forbidden_message = saved_message;
11137 parser->integral_constant_expression_p
11138 = saved_integral_constant_expression_p;
11139 parser->non_integral_constant_expression_p
11140 = saved_non_integral_constant_expression_p;
11142 /* Parse to the closing `)'. */
11143 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11145 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11146 /*consume_paren=*/true);
11147 return error_mark_node;
11150 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11151 tf_warning_or_error);
11153 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11154 it again. */
11155 start_token->type = CPP_DECLTYPE;
11156 start_token->u.value = expr;
11157 start_token->keyword = RID_MAX;
11158 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11160 return expr;
11163 /* Special member functions [gram.special] */
11165 /* Parse a conversion-function-id.
11167 conversion-function-id:
11168 operator conversion-type-id
11170 Returns an IDENTIFIER_NODE representing the operator. */
11172 static tree
11173 cp_parser_conversion_function_id (cp_parser* parser)
11175 tree type;
11176 tree saved_scope;
11177 tree saved_qualifying_scope;
11178 tree saved_object_scope;
11179 tree pushed_scope = NULL_TREE;
11181 /* Look for the `operator' token. */
11182 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11183 return error_mark_node;
11184 /* When we parse the conversion-type-id, the current scope will be
11185 reset. However, we need that information in able to look up the
11186 conversion function later, so we save it here. */
11187 saved_scope = parser->scope;
11188 saved_qualifying_scope = parser->qualifying_scope;
11189 saved_object_scope = parser->object_scope;
11190 /* We must enter the scope of the class so that the names of
11191 entities declared within the class are available in the
11192 conversion-type-id. For example, consider:
11194 struct S {
11195 typedef int I;
11196 operator I();
11199 S::operator I() { ... }
11201 In order to see that `I' is a type-name in the definition, we
11202 must be in the scope of `S'. */
11203 if (saved_scope)
11204 pushed_scope = push_scope (saved_scope);
11205 /* Parse the conversion-type-id. */
11206 type = cp_parser_conversion_type_id (parser);
11207 /* Leave the scope of the class, if any. */
11208 if (pushed_scope)
11209 pop_scope (pushed_scope);
11210 /* Restore the saved scope. */
11211 parser->scope = saved_scope;
11212 parser->qualifying_scope = saved_qualifying_scope;
11213 parser->object_scope = saved_object_scope;
11214 /* If the TYPE is invalid, indicate failure. */
11215 if (type == error_mark_node)
11216 return error_mark_node;
11217 return mangle_conv_op_name_for_type (type);
11220 /* Parse a conversion-type-id:
11222 conversion-type-id:
11223 type-specifier-seq conversion-declarator [opt]
11225 Returns the TYPE specified. */
11227 static tree
11228 cp_parser_conversion_type_id (cp_parser* parser)
11230 tree attributes;
11231 cp_decl_specifier_seq type_specifiers;
11232 cp_declarator *declarator;
11233 tree type_specified;
11235 /* Parse the attributes. */
11236 attributes = cp_parser_attributes_opt (parser);
11237 /* Parse the type-specifiers. */
11238 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11239 /*is_trailing_return=*/false,
11240 &type_specifiers);
11241 /* If that didn't work, stop. */
11242 if (type_specifiers.type == error_mark_node)
11243 return error_mark_node;
11244 /* Parse the conversion-declarator. */
11245 declarator = cp_parser_conversion_declarator_opt (parser);
11247 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11248 /*initialized=*/0, &attributes);
11249 if (attributes)
11250 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11252 /* Don't give this error when parsing tentatively. This happens to
11253 work because we always parse this definitively once. */
11254 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11255 && type_uses_auto (type_specified))
11257 if (cxx_dialect < cxx1y)
11259 error ("invalid use of %<auto%> in conversion operator");
11260 return error_mark_node;
11262 else if (template_parm_scope_p ())
11263 warning (0, "use of %<auto%> in member template "
11264 "conversion operator can never be deduced");
11267 return type_specified;
11270 /* Parse an (optional) conversion-declarator.
11272 conversion-declarator:
11273 ptr-operator conversion-declarator [opt]
11277 static cp_declarator *
11278 cp_parser_conversion_declarator_opt (cp_parser* parser)
11280 enum tree_code code;
11281 tree class_type;
11282 cp_cv_quals cv_quals;
11284 /* We don't know if there's a ptr-operator next, or not. */
11285 cp_parser_parse_tentatively (parser);
11286 /* Try the ptr-operator. */
11287 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11288 /* If it worked, look for more conversion-declarators. */
11289 if (cp_parser_parse_definitely (parser))
11291 cp_declarator *declarator;
11293 /* Parse another optional declarator. */
11294 declarator = cp_parser_conversion_declarator_opt (parser);
11296 return cp_parser_make_indirect_declarator
11297 (code, class_type, cv_quals, declarator);
11300 return NULL;
11303 /* Parse an (optional) ctor-initializer.
11305 ctor-initializer:
11306 : mem-initializer-list
11308 Returns TRUE iff the ctor-initializer was actually present. */
11310 static bool
11311 cp_parser_ctor_initializer_opt (cp_parser* parser)
11313 /* If the next token is not a `:', then there is no
11314 ctor-initializer. */
11315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11317 /* Do default initialization of any bases and members. */
11318 if (DECL_CONSTRUCTOR_P (current_function_decl))
11319 finish_mem_initializers (NULL_TREE);
11321 return false;
11324 /* Consume the `:' token. */
11325 cp_lexer_consume_token (parser->lexer);
11326 /* And the mem-initializer-list. */
11327 cp_parser_mem_initializer_list (parser);
11329 return true;
11332 /* Parse a mem-initializer-list.
11334 mem-initializer-list:
11335 mem-initializer ... [opt]
11336 mem-initializer ... [opt] , mem-initializer-list */
11338 static void
11339 cp_parser_mem_initializer_list (cp_parser* parser)
11341 tree mem_initializer_list = NULL_TREE;
11342 tree target_ctor = error_mark_node;
11343 cp_token *token = cp_lexer_peek_token (parser->lexer);
11345 /* Let the semantic analysis code know that we are starting the
11346 mem-initializer-list. */
11347 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11348 error_at (token->location,
11349 "only constructors take member initializers");
11351 /* Loop through the list. */
11352 while (true)
11354 tree mem_initializer;
11356 token = cp_lexer_peek_token (parser->lexer);
11357 /* Parse the mem-initializer. */
11358 mem_initializer = cp_parser_mem_initializer (parser);
11359 /* If the next token is a `...', we're expanding member initializers. */
11360 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11362 /* Consume the `...'. */
11363 cp_lexer_consume_token (parser->lexer);
11365 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11366 can be expanded but members cannot. */
11367 if (mem_initializer != error_mark_node
11368 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11370 error_at (token->location,
11371 "cannot expand initializer for member %<%D%>",
11372 TREE_PURPOSE (mem_initializer));
11373 mem_initializer = error_mark_node;
11376 /* Construct the pack expansion type. */
11377 if (mem_initializer != error_mark_node)
11378 mem_initializer = make_pack_expansion (mem_initializer);
11380 if (target_ctor != error_mark_node
11381 && mem_initializer != error_mark_node)
11383 error ("mem-initializer for %qD follows constructor delegation",
11384 TREE_PURPOSE (mem_initializer));
11385 mem_initializer = error_mark_node;
11387 /* Look for a target constructor. */
11388 if (mem_initializer != error_mark_node
11389 && TYPE_P (TREE_PURPOSE (mem_initializer))
11390 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11392 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11393 if (mem_initializer_list)
11395 error ("constructor delegation follows mem-initializer for %qD",
11396 TREE_PURPOSE (mem_initializer_list));
11397 mem_initializer = error_mark_node;
11399 target_ctor = mem_initializer;
11401 /* Add it to the list, unless it was erroneous. */
11402 if (mem_initializer != error_mark_node)
11404 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11405 mem_initializer_list = mem_initializer;
11407 /* If the next token is not a `,', we're done. */
11408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11409 break;
11410 /* Consume the `,' token. */
11411 cp_lexer_consume_token (parser->lexer);
11414 /* Perform semantic analysis. */
11415 if (DECL_CONSTRUCTOR_P (current_function_decl))
11416 finish_mem_initializers (mem_initializer_list);
11419 /* Parse a mem-initializer.
11421 mem-initializer:
11422 mem-initializer-id ( expression-list [opt] )
11423 mem-initializer-id braced-init-list
11425 GNU extension:
11427 mem-initializer:
11428 ( expression-list [opt] )
11430 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11431 class) or FIELD_DECL (for a non-static data member) to initialize;
11432 the TREE_VALUE is the expression-list. An empty initialization
11433 list is represented by void_list_node. */
11435 static tree
11436 cp_parser_mem_initializer (cp_parser* parser)
11438 tree mem_initializer_id;
11439 tree expression_list;
11440 tree member;
11441 cp_token *token = cp_lexer_peek_token (parser->lexer);
11443 /* Find out what is being initialized. */
11444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11446 permerror (token->location,
11447 "anachronistic old-style base class initializer");
11448 mem_initializer_id = NULL_TREE;
11450 else
11452 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11453 if (mem_initializer_id == error_mark_node)
11454 return mem_initializer_id;
11456 member = expand_member_init (mem_initializer_id);
11457 if (member && !DECL_P (member))
11458 in_base_initializer = 1;
11460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11462 bool expr_non_constant_p;
11463 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11464 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11465 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11466 expression_list = build_tree_list (NULL_TREE, expression_list);
11468 else
11470 VEC(tree,gc)* vec;
11471 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11472 /*cast_p=*/false,
11473 /*allow_expansion_p=*/true,
11474 /*non_constant_p=*/NULL);
11475 if (vec == NULL)
11476 return error_mark_node;
11477 expression_list = build_tree_list_vec (vec);
11478 release_tree_vector (vec);
11481 if (expression_list == error_mark_node)
11482 return error_mark_node;
11483 if (!expression_list)
11484 expression_list = void_type_node;
11486 in_base_initializer = 0;
11488 return member ? build_tree_list (member, expression_list) : error_mark_node;
11491 /* Parse a mem-initializer-id.
11493 mem-initializer-id:
11494 :: [opt] nested-name-specifier [opt] class-name
11495 identifier
11497 Returns a TYPE indicating the class to be initializer for the first
11498 production. Returns an IDENTIFIER_NODE indicating the data member
11499 to be initialized for the second production. */
11501 static tree
11502 cp_parser_mem_initializer_id (cp_parser* parser)
11504 bool global_scope_p;
11505 bool nested_name_specifier_p;
11506 bool template_p = false;
11507 tree id;
11509 cp_token *token = cp_lexer_peek_token (parser->lexer);
11511 /* `typename' is not allowed in this context ([temp.res]). */
11512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11514 error_at (token->location,
11515 "keyword %<typename%> not allowed in this context (a qualified "
11516 "member initializer is implicitly a type)");
11517 cp_lexer_consume_token (parser->lexer);
11519 /* Look for the optional `::' operator. */
11520 global_scope_p
11521 = (cp_parser_global_scope_opt (parser,
11522 /*current_scope_valid_p=*/false)
11523 != NULL_TREE);
11524 /* Look for the optional nested-name-specifier. The simplest way to
11525 implement:
11527 [temp.res]
11529 The keyword `typename' is not permitted in a base-specifier or
11530 mem-initializer; in these contexts a qualified name that
11531 depends on a template-parameter is implicitly assumed to be a
11532 type name.
11534 is to assume that we have seen the `typename' keyword at this
11535 point. */
11536 nested_name_specifier_p
11537 = (cp_parser_nested_name_specifier_opt (parser,
11538 /*typename_keyword_p=*/true,
11539 /*check_dependency_p=*/true,
11540 /*type_p=*/true,
11541 /*is_declaration=*/true)
11542 != NULL_TREE);
11543 if (nested_name_specifier_p)
11544 template_p = cp_parser_optional_template_keyword (parser);
11545 /* If there is a `::' operator or a nested-name-specifier, then we
11546 are definitely looking for a class-name. */
11547 if (global_scope_p || nested_name_specifier_p)
11548 return cp_parser_class_name (parser,
11549 /*typename_keyword_p=*/true,
11550 /*template_keyword_p=*/template_p,
11551 typename_type,
11552 /*check_dependency_p=*/true,
11553 /*class_head_p=*/false,
11554 /*is_declaration=*/true);
11555 /* Otherwise, we could also be looking for an ordinary identifier. */
11556 cp_parser_parse_tentatively (parser);
11557 /* Try a class-name. */
11558 id = cp_parser_class_name (parser,
11559 /*typename_keyword_p=*/true,
11560 /*template_keyword_p=*/false,
11561 none_type,
11562 /*check_dependency_p=*/true,
11563 /*class_head_p=*/false,
11564 /*is_declaration=*/true);
11565 /* If we found one, we're done. */
11566 if (cp_parser_parse_definitely (parser))
11567 return id;
11568 /* Otherwise, look for an ordinary identifier. */
11569 return cp_parser_identifier (parser);
11572 /* Overloading [gram.over] */
11574 /* Parse an operator-function-id.
11576 operator-function-id:
11577 operator operator
11579 Returns an IDENTIFIER_NODE for the operator which is a
11580 human-readable spelling of the identifier, e.g., `operator +'. */
11582 static tree
11583 cp_parser_operator_function_id (cp_parser* parser)
11585 /* Look for the `operator' keyword. */
11586 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11587 return error_mark_node;
11588 /* And then the name of the operator itself. */
11589 return cp_parser_operator (parser);
11592 /* Return an identifier node for a user-defined literal operator.
11593 The suffix identifier is chained to the operator name identifier. */
11595 static tree
11596 cp_literal_operator_id (const char* name)
11598 tree identifier;
11599 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11600 + strlen (name) + 10);
11601 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11602 identifier = get_identifier (buffer);
11603 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11605 return identifier;
11608 /* Parse an operator.
11610 operator:
11611 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11612 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11613 || ++ -- , ->* -> () []
11615 GNU Extensions:
11617 operator:
11618 <? >? <?= >?=
11620 Returns an IDENTIFIER_NODE for the operator which is a
11621 human-readable spelling of the identifier, e.g., `operator +'. */
11623 static tree
11624 cp_parser_operator (cp_parser* parser)
11626 tree id = NULL_TREE;
11627 cp_token *token;
11629 /* Peek at the next token. */
11630 token = cp_lexer_peek_token (parser->lexer);
11631 /* Figure out which operator we have. */
11632 switch (token->type)
11634 case CPP_KEYWORD:
11636 enum tree_code op;
11638 /* The keyword should be either `new' or `delete'. */
11639 if (token->keyword == RID_NEW)
11640 op = NEW_EXPR;
11641 else if (token->keyword == RID_DELETE)
11642 op = DELETE_EXPR;
11643 else
11644 break;
11646 /* Consume the `new' or `delete' token. */
11647 cp_lexer_consume_token (parser->lexer);
11649 /* Peek at the next token. */
11650 token = cp_lexer_peek_token (parser->lexer);
11651 /* If it's a `[' token then this is the array variant of the
11652 operator. */
11653 if (token->type == CPP_OPEN_SQUARE)
11655 /* Consume the `[' token. */
11656 cp_lexer_consume_token (parser->lexer);
11657 /* Look for the `]' token. */
11658 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11659 id = ansi_opname (op == NEW_EXPR
11660 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11662 /* Otherwise, we have the non-array variant. */
11663 else
11664 id = ansi_opname (op);
11666 return id;
11669 case CPP_PLUS:
11670 id = ansi_opname (PLUS_EXPR);
11671 break;
11673 case CPP_MINUS:
11674 id = ansi_opname (MINUS_EXPR);
11675 break;
11677 case CPP_MULT:
11678 id = ansi_opname (MULT_EXPR);
11679 break;
11681 case CPP_DIV:
11682 id = ansi_opname (TRUNC_DIV_EXPR);
11683 break;
11685 case CPP_MOD:
11686 id = ansi_opname (TRUNC_MOD_EXPR);
11687 break;
11689 case CPP_XOR:
11690 id = ansi_opname (BIT_XOR_EXPR);
11691 break;
11693 case CPP_AND:
11694 id = ansi_opname (BIT_AND_EXPR);
11695 break;
11697 case CPP_OR:
11698 id = ansi_opname (BIT_IOR_EXPR);
11699 break;
11701 case CPP_COMPL:
11702 id = ansi_opname (BIT_NOT_EXPR);
11703 break;
11705 case CPP_NOT:
11706 id = ansi_opname (TRUTH_NOT_EXPR);
11707 break;
11709 case CPP_EQ:
11710 id = ansi_assopname (NOP_EXPR);
11711 break;
11713 case CPP_LESS:
11714 id = ansi_opname (LT_EXPR);
11715 break;
11717 case CPP_GREATER:
11718 id = ansi_opname (GT_EXPR);
11719 break;
11721 case CPP_PLUS_EQ:
11722 id = ansi_assopname (PLUS_EXPR);
11723 break;
11725 case CPP_MINUS_EQ:
11726 id = ansi_assopname (MINUS_EXPR);
11727 break;
11729 case CPP_MULT_EQ:
11730 id = ansi_assopname (MULT_EXPR);
11731 break;
11733 case CPP_DIV_EQ:
11734 id = ansi_assopname (TRUNC_DIV_EXPR);
11735 break;
11737 case CPP_MOD_EQ:
11738 id = ansi_assopname (TRUNC_MOD_EXPR);
11739 break;
11741 case CPP_XOR_EQ:
11742 id = ansi_assopname (BIT_XOR_EXPR);
11743 break;
11745 case CPP_AND_EQ:
11746 id = ansi_assopname (BIT_AND_EXPR);
11747 break;
11749 case CPP_OR_EQ:
11750 id = ansi_assopname (BIT_IOR_EXPR);
11751 break;
11753 case CPP_LSHIFT:
11754 id = ansi_opname (LSHIFT_EXPR);
11755 break;
11757 case CPP_RSHIFT:
11758 id = ansi_opname (RSHIFT_EXPR);
11759 break;
11761 case CPP_LSHIFT_EQ:
11762 id = ansi_assopname (LSHIFT_EXPR);
11763 break;
11765 case CPP_RSHIFT_EQ:
11766 id = ansi_assopname (RSHIFT_EXPR);
11767 break;
11769 case CPP_EQ_EQ:
11770 id = ansi_opname (EQ_EXPR);
11771 break;
11773 case CPP_NOT_EQ:
11774 id = ansi_opname (NE_EXPR);
11775 break;
11777 case CPP_LESS_EQ:
11778 id = ansi_opname (LE_EXPR);
11779 break;
11781 case CPP_GREATER_EQ:
11782 id = ansi_opname (GE_EXPR);
11783 break;
11785 case CPP_AND_AND:
11786 id = ansi_opname (TRUTH_ANDIF_EXPR);
11787 break;
11789 case CPP_OR_OR:
11790 id = ansi_opname (TRUTH_ORIF_EXPR);
11791 break;
11793 case CPP_PLUS_PLUS:
11794 id = ansi_opname (POSTINCREMENT_EXPR);
11795 break;
11797 case CPP_MINUS_MINUS:
11798 id = ansi_opname (PREDECREMENT_EXPR);
11799 break;
11801 case CPP_COMMA:
11802 id = ansi_opname (COMPOUND_EXPR);
11803 break;
11805 case CPP_DEREF_STAR:
11806 id = ansi_opname (MEMBER_REF);
11807 break;
11809 case CPP_DEREF:
11810 id = ansi_opname (COMPONENT_REF);
11811 break;
11813 case CPP_OPEN_PAREN:
11814 /* Consume the `('. */
11815 cp_lexer_consume_token (parser->lexer);
11816 /* Look for the matching `)'. */
11817 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11818 return ansi_opname (CALL_EXPR);
11820 case CPP_OPEN_SQUARE:
11821 /* Consume the `['. */
11822 cp_lexer_consume_token (parser->lexer);
11823 /* Look for the matching `]'. */
11824 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11825 return ansi_opname (ARRAY_REF);
11827 case CPP_STRING:
11828 if (cxx_dialect == cxx98)
11829 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11830 if (TREE_STRING_LENGTH (token->u.value) > 2)
11832 error ("expected empty string after %<operator%> keyword");
11833 return error_mark_node;
11835 /* Consume the string. */
11836 cp_lexer_consume_token (parser->lexer);
11837 /* Look for the suffix identifier. */
11838 token = cp_lexer_peek_token (parser->lexer);
11839 if (token->type == CPP_NAME)
11841 id = cp_parser_identifier (parser);
11842 if (id != error_mark_node)
11844 const char *name = IDENTIFIER_POINTER (id);
11845 return cp_literal_operator_id (name);
11848 else
11850 error ("expected suffix identifier");
11851 return error_mark_node;
11854 case CPP_STRING_USERDEF:
11855 error ("missing space between %<\"\"%> and suffix identifier");
11856 return error_mark_node;
11858 default:
11859 /* Anything else is an error. */
11860 break;
11863 /* If we have selected an identifier, we need to consume the
11864 operator token. */
11865 if (id)
11866 cp_lexer_consume_token (parser->lexer);
11867 /* Otherwise, no valid operator name was present. */
11868 else
11870 cp_parser_error (parser, "expected operator");
11871 id = error_mark_node;
11874 return id;
11877 /* Parse a template-declaration.
11879 template-declaration:
11880 export [opt] template < template-parameter-list > declaration
11882 If MEMBER_P is TRUE, this template-declaration occurs within a
11883 class-specifier.
11885 The grammar rule given by the standard isn't correct. What
11886 is really meant is:
11888 template-declaration:
11889 export [opt] template-parameter-list-seq
11890 decl-specifier-seq [opt] init-declarator [opt] ;
11891 export [opt] template-parameter-list-seq
11892 function-definition
11894 template-parameter-list-seq:
11895 template-parameter-list-seq [opt]
11896 template < template-parameter-list > */
11898 static void
11899 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11901 /* Check for `export'. */
11902 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11904 /* Consume the `export' token. */
11905 cp_lexer_consume_token (parser->lexer);
11906 /* Warn that we do not support `export'. */
11907 warning (0, "keyword %<export%> not implemented, and will be ignored");
11910 cp_parser_template_declaration_after_export (parser, member_p);
11913 /* Parse a template-parameter-list.
11915 template-parameter-list:
11916 template-parameter
11917 template-parameter-list , template-parameter
11919 Returns a TREE_LIST. Each node represents a template parameter.
11920 The nodes are connected via their TREE_CHAINs. */
11922 static tree
11923 cp_parser_template_parameter_list (cp_parser* parser)
11925 tree parameter_list = NULL_TREE;
11927 begin_template_parm_list ();
11929 /* The loop below parses the template parms. We first need to know
11930 the total number of template parms to be able to compute proper
11931 canonical types of each dependent type. So after the loop, when
11932 we know the total number of template parms,
11933 end_template_parm_list computes the proper canonical types and
11934 fixes up the dependent types accordingly. */
11935 while (true)
11937 tree parameter;
11938 bool is_non_type;
11939 bool is_parameter_pack;
11940 location_t parm_loc;
11942 /* Parse the template-parameter. */
11943 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11944 parameter = cp_parser_template_parameter (parser,
11945 &is_non_type,
11946 &is_parameter_pack);
11947 /* Add it to the list. */
11948 if (parameter != error_mark_node)
11949 parameter_list = process_template_parm (parameter_list,
11950 parm_loc,
11951 parameter,
11952 is_non_type,
11953 is_parameter_pack,
11955 else
11957 tree err_parm = build_tree_list (parameter, parameter);
11958 parameter_list = chainon (parameter_list, err_parm);
11961 /* If the next token is not a `,', we're done. */
11962 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11963 break;
11964 /* Otherwise, consume the `,' token. */
11965 cp_lexer_consume_token (parser->lexer);
11968 return end_template_parm_list (parameter_list);
11971 /* Parse a template-parameter.
11973 template-parameter:
11974 type-parameter
11975 parameter-declaration
11977 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11978 the parameter. The TREE_PURPOSE is the default value, if any.
11979 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11980 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11981 set to true iff this parameter is a parameter pack. */
11983 static tree
11984 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11985 bool *is_parameter_pack)
11987 cp_token *token;
11988 cp_parameter_declarator *parameter_declarator;
11989 cp_declarator *id_declarator;
11990 tree parm;
11992 /* Assume it is a type parameter or a template parameter. */
11993 *is_non_type = false;
11994 /* Assume it not a parameter pack. */
11995 *is_parameter_pack = false;
11996 /* Peek at the next token. */
11997 token = cp_lexer_peek_token (parser->lexer);
11998 /* If it is `class' or `template', we have a type-parameter. */
11999 if (token->keyword == RID_TEMPLATE)
12000 return cp_parser_type_parameter (parser, is_parameter_pack);
12001 /* If it is `class' or `typename' we do not know yet whether it is a
12002 type parameter or a non-type parameter. Consider:
12004 template <typename T, typename T::X X> ...
12008 template <class C, class D*> ...
12010 Here, the first parameter is a type parameter, and the second is
12011 a non-type parameter. We can tell by looking at the token after
12012 the identifier -- if it is a `,', `=', or `>' then we have a type
12013 parameter. */
12014 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12016 /* Peek at the token after `class' or `typename'. */
12017 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12018 /* If it's an ellipsis, we have a template type parameter
12019 pack. */
12020 if (token->type == CPP_ELLIPSIS)
12021 return cp_parser_type_parameter (parser, is_parameter_pack);
12022 /* If it's an identifier, skip it. */
12023 if (token->type == CPP_NAME)
12024 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12025 /* Now, see if the token looks like the end of a template
12026 parameter. */
12027 if (token->type == CPP_COMMA
12028 || token->type == CPP_EQ
12029 || token->type == CPP_GREATER)
12030 return cp_parser_type_parameter (parser, is_parameter_pack);
12033 /* Otherwise, it is a non-type parameter.
12035 [temp.param]
12037 When parsing a default template-argument for a non-type
12038 template-parameter, the first non-nested `>' is taken as the end
12039 of the template parameter-list rather than a greater-than
12040 operator. */
12041 *is_non_type = true;
12042 parameter_declarator
12043 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12044 /*parenthesized_p=*/NULL);
12046 /* If the parameter declaration is marked as a parameter pack, set
12047 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12048 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12049 grokdeclarator. */
12050 if (parameter_declarator
12051 && parameter_declarator->declarator
12052 && parameter_declarator->declarator->parameter_pack_p)
12054 *is_parameter_pack = true;
12055 parameter_declarator->declarator->parameter_pack_p = false;
12058 /* If the next token is an ellipsis, and we don't already have it
12059 marked as a parameter pack, then we have a parameter pack (that
12060 has no declarator). */
12061 if (!*is_parameter_pack
12062 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12063 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12065 /* Consume the `...'. */
12066 cp_lexer_consume_token (parser->lexer);
12067 maybe_warn_variadic_templates ();
12069 *is_parameter_pack = true;
12071 /* We might end up with a pack expansion as the type of the non-type
12072 template parameter, in which case this is a non-type template
12073 parameter pack. */
12074 else if (parameter_declarator
12075 && parameter_declarator->decl_specifiers.type
12076 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12078 *is_parameter_pack = true;
12079 parameter_declarator->decl_specifiers.type =
12080 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12083 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12085 /* Parameter packs cannot have default arguments. However, a
12086 user may try to do so, so we'll parse them and give an
12087 appropriate diagnostic here. */
12089 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12091 /* Find the name of the parameter pack. */
12092 id_declarator = parameter_declarator->declarator;
12093 while (id_declarator && id_declarator->kind != cdk_id)
12094 id_declarator = id_declarator->declarator;
12096 if (id_declarator && id_declarator->kind == cdk_id)
12097 error_at (start_token->location,
12098 "template parameter pack %qD cannot have a default argument",
12099 id_declarator->u.id.unqualified_name);
12100 else
12101 error_at (start_token->location,
12102 "template parameter pack cannot have a default argument");
12104 /* Parse the default argument, but throw away the result. */
12105 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12108 parm = grokdeclarator (parameter_declarator->declarator,
12109 &parameter_declarator->decl_specifiers,
12110 TPARM, /*initialized=*/0,
12111 /*attrlist=*/NULL);
12112 if (parm == error_mark_node)
12113 return error_mark_node;
12115 return build_tree_list (parameter_declarator->default_argument, parm);
12118 /* Parse a type-parameter.
12120 type-parameter:
12121 class identifier [opt]
12122 class identifier [opt] = type-id
12123 typename identifier [opt]
12124 typename identifier [opt] = type-id
12125 template < template-parameter-list > class identifier [opt]
12126 template < template-parameter-list > class identifier [opt]
12127 = id-expression
12129 GNU Extension (variadic templates):
12131 type-parameter:
12132 class ... identifier [opt]
12133 typename ... identifier [opt]
12135 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12136 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12137 the declaration of the parameter.
12139 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12141 static tree
12142 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12144 cp_token *token;
12145 tree parameter;
12147 /* Look for a keyword to tell us what kind of parameter this is. */
12148 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12149 if (!token)
12150 return error_mark_node;
12152 switch (token->keyword)
12154 case RID_CLASS:
12155 case RID_TYPENAME:
12157 tree identifier;
12158 tree default_argument;
12160 /* If the next token is an ellipsis, we have a template
12161 argument pack. */
12162 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12164 /* Consume the `...' token. */
12165 cp_lexer_consume_token (parser->lexer);
12166 maybe_warn_variadic_templates ();
12168 *is_parameter_pack = true;
12171 /* If the next token is an identifier, then it names the
12172 parameter. */
12173 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12174 identifier = cp_parser_identifier (parser);
12175 else
12176 identifier = NULL_TREE;
12178 /* Create the parameter. */
12179 parameter = finish_template_type_parm (class_type_node, identifier);
12181 /* If the next token is an `=', we have a default argument. */
12182 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12184 /* Consume the `=' token. */
12185 cp_lexer_consume_token (parser->lexer);
12186 /* Parse the default-argument. */
12187 push_deferring_access_checks (dk_no_deferred);
12188 default_argument = cp_parser_type_id (parser);
12190 /* Template parameter packs cannot have default
12191 arguments. */
12192 if (*is_parameter_pack)
12194 if (identifier)
12195 error_at (token->location,
12196 "template parameter pack %qD cannot have a "
12197 "default argument", identifier);
12198 else
12199 error_at (token->location,
12200 "template parameter packs cannot have "
12201 "default arguments");
12202 default_argument = NULL_TREE;
12204 pop_deferring_access_checks ();
12206 else
12207 default_argument = NULL_TREE;
12209 /* Create the combined representation of the parameter and the
12210 default argument. */
12211 parameter = build_tree_list (default_argument, parameter);
12213 break;
12215 case RID_TEMPLATE:
12217 tree identifier;
12218 tree default_argument;
12220 /* Look for the `<'. */
12221 cp_parser_require (parser, CPP_LESS, RT_LESS);
12222 /* Parse the template-parameter-list. */
12223 cp_parser_template_parameter_list (parser);
12224 /* Look for the `>'. */
12225 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12226 /* Look for the `class' keyword. */
12227 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12228 /* If the next token is an ellipsis, we have a template
12229 argument pack. */
12230 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12232 /* Consume the `...' token. */
12233 cp_lexer_consume_token (parser->lexer);
12234 maybe_warn_variadic_templates ();
12236 *is_parameter_pack = true;
12238 /* If the next token is an `=', then there is a
12239 default-argument. If the next token is a `>', we are at
12240 the end of the parameter-list. If the next token is a `,',
12241 then we are at the end of this parameter. */
12242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12243 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12244 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12246 identifier = cp_parser_identifier (parser);
12247 /* Treat invalid names as if the parameter were nameless. */
12248 if (identifier == error_mark_node)
12249 identifier = NULL_TREE;
12251 else
12252 identifier = NULL_TREE;
12254 /* Create the template parameter. */
12255 parameter = finish_template_template_parm (class_type_node,
12256 identifier);
12258 /* If the next token is an `=', then there is a
12259 default-argument. */
12260 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12262 bool is_template;
12264 /* Consume the `='. */
12265 cp_lexer_consume_token (parser->lexer);
12266 /* Parse the id-expression. */
12267 push_deferring_access_checks (dk_no_deferred);
12268 /* save token before parsing the id-expression, for error
12269 reporting */
12270 token = cp_lexer_peek_token (parser->lexer);
12271 default_argument
12272 = cp_parser_id_expression (parser,
12273 /*template_keyword_p=*/false,
12274 /*check_dependency_p=*/true,
12275 /*template_p=*/&is_template,
12276 /*declarator_p=*/false,
12277 /*optional_p=*/false);
12278 if (TREE_CODE (default_argument) == TYPE_DECL)
12279 /* If the id-expression was a template-id that refers to
12280 a template-class, we already have the declaration here,
12281 so no further lookup is needed. */
12283 else
12284 /* Look up the name. */
12285 default_argument
12286 = cp_parser_lookup_name (parser, default_argument,
12287 none_type,
12288 /*is_template=*/is_template,
12289 /*is_namespace=*/false,
12290 /*check_dependency=*/true,
12291 /*ambiguous_decls=*/NULL,
12292 token->location);
12293 /* See if the default argument is valid. */
12294 default_argument
12295 = check_template_template_default_arg (default_argument);
12297 /* Template parameter packs cannot have default
12298 arguments. */
12299 if (*is_parameter_pack)
12301 if (identifier)
12302 error_at (token->location,
12303 "template parameter pack %qD cannot "
12304 "have a default argument",
12305 identifier);
12306 else
12307 error_at (token->location, "template parameter packs cannot "
12308 "have default arguments");
12309 default_argument = NULL_TREE;
12311 pop_deferring_access_checks ();
12313 else
12314 default_argument = NULL_TREE;
12316 /* Create the combined representation of the parameter and the
12317 default argument. */
12318 parameter = build_tree_list (default_argument, parameter);
12320 break;
12322 default:
12323 gcc_unreachable ();
12324 break;
12327 return parameter;
12330 /* Parse a template-id.
12332 template-id:
12333 template-name < template-argument-list [opt] >
12335 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12336 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12337 returned. Otherwise, if the template-name names a function, or set
12338 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12339 names a class, returns a TYPE_DECL for the specialization.
12341 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12342 uninstantiated templates. */
12344 static tree
12345 cp_parser_template_id (cp_parser *parser,
12346 bool template_keyword_p,
12347 bool check_dependency_p,
12348 bool is_declaration)
12350 int i;
12351 tree templ;
12352 tree arguments;
12353 tree template_id;
12354 cp_token_position start_of_id = 0;
12355 deferred_access_check *chk;
12356 VEC (deferred_access_check,gc) *access_check;
12357 cp_token *next_token = NULL, *next_token_2 = NULL;
12358 bool is_identifier;
12360 /* If the next token corresponds to a template-id, there is no need
12361 to reparse it. */
12362 next_token = cp_lexer_peek_token (parser->lexer);
12363 if (next_token->type == CPP_TEMPLATE_ID)
12365 struct tree_check *check_value;
12367 /* Get the stored value. */
12368 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12369 /* Perform any access checks that were deferred. */
12370 access_check = check_value->checks;
12371 if (access_check)
12373 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12374 perform_or_defer_access_check (chk->binfo,
12375 chk->decl,
12376 chk->diag_decl);
12378 /* Return the stored value. */
12379 return check_value->value;
12382 /* Avoid performing name lookup if there is no possibility of
12383 finding a template-id. */
12384 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12385 || (next_token->type == CPP_NAME
12386 && !cp_parser_nth_token_starts_template_argument_list_p
12387 (parser, 2)))
12389 cp_parser_error (parser, "expected template-id");
12390 return error_mark_node;
12393 /* Remember where the template-id starts. */
12394 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12395 start_of_id = cp_lexer_token_position (parser->lexer, false);
12397 push_deferring_access_checks (dk_deferred);
12399 /* Parse the template-name. */
12400 is_identifier = false;
12401 templ = cp_parser_template_name (parser, template_keyword_p,
12402 check_dependency_p,
12403 is_declaration,
12404 &is_identifier);
12405 if (templ == error_mark_node || is_identifier)
12407 pop_deferring_access_checks ();
12408 return templ;
12411 /* If we find the sequence `[:' after a template-name, it's probably
12412 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12413 parse correctly the argument list. */
12414 next_token = cp_lexer_peek_token (parser->lexer);
12415 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12416 if (next_token->type == CPP_OPEN_SQUARE
12417 && next_token->flags & DIGRAPH
12418 && next_token_2->type == CPP_COLON
12419 && !(next_token_2->flags & PREV_WHITE))
12421 cp_parser_parse_tentatively (parser);
12422 /* Change `:' into `::'. */
12423 next_token_2->type = CPP_SCOPE;
12424 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12425 CPP_LESS. */
12426 cp_lexer_consume_token (parser->lexer);
12428 /* Parse the arguments. */
12429 arguments = cp_parser_enclosed_template_argument_list (parser);
12430 if (!cp_parser_parse_definitely (parser))
12432 /* If we couldn't parse an argument list, then we revert our changes
12433 and return simply an error. Maybe this is not a template-id
12434 after all. */
12435 next_token_2->type = CPP_COLON;
12436 cp_parser_error (parser, "expected %<<%>");
12437 pop_deferring_access_checks ();
12438 return error_mark_node;
12440 /* Otherwise, emit an error about the invalid digraph, but continue
12441 parsing because we got our argument list. */
12442 if (permerror (next_token->location,
12443 "%<<::%> cannot begin a template-argument list"))
12445 static bool hint = false;
12446 inform (next_token->location,
12447 "%<<:%> is an alternate spelling for %<[%>."
12448 " Insert whitespace between %<<%> and %<::%>");
12449 if (!hint && !flag_permissive)
12451 inform (next_token->location, "(if you use %<-fpermissive%>"
12452 " G++ will accept your code)");
12453 hint = true;
12457 else
12459 /* Look for the `<' that starts the template-argument-list. */
12460 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12462 pop_deferring_access_checks ();
12463 return error_mark_node;
12465 /* Parse the arguments. */
12466 arguments = cp_parser_enclosed_template_argument_list (parser);
12469 /* Build a representation of the specialization. */
12470 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12471 template_id = build_min_nt_loc (next_token->location,
12472 TEMPLATE_ID_EXPR,
12473 templ, arguments);
12474 else if (DECL_TYPE_TEMPLATE_P (templ)
12475 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12477 bool entering_scope;
12478 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12479 template (rather than some instantiation thereof) only if
12480 is not nested within some other construct. For example, in
12481 "template <typename T> void f(T) { A<T>::", A<T> is just an
12482 instantiation of A. */
12483 entering_scope = (template_parm_scope_p ()
12484 && cp_lexer_next_token_is (parser->lexer,
12485 CPP_SCOPE));
12486 template_id
12487 = finish_template_type (templ, arguments, entering_scope);
12489 else
12491 /* If it's not a class-template or a template-template, it should be
12492 a function-template. */
12493 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12494 || TREE_CODE (templ) == OVERLOAD
12495 || BASELINK_P (templ)));
12497 template_id = lookup_template_function (templ, arguments);
12500 /* If parsing tentatively, replace the sequence of tokens that makes
12501 up the template-id with a CPP_TEMPLATE_ID token. That way,
12502 should we re-parse the token stream, we will not have to repeat
12503 the effort required to do the parse, nor will we issue duplicate
12504 error messages about problems during instantiation of the
12505 template. */
12506 if (start_of_id)
12508 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12510 /* Reset the contents of the START_OF_ID token. */
12511 token->type = CPP_TEMPLATE_ID;
12512 /* Retrieve any deferred checks. Do not pop this access checks yet
12513 so the memory will not be reclaimed during token replacing below. */
12514 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12515 token->u.tree_check_value->value = template_id;
12516 token->u.tree_check_value->checks = get_deferred_access_checks ();
12517 token->keyword = RID_MAX;
12519 /* Purge all subsequent tokens. */
12520 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12522 /* ??? Can we actually assume that, if template_id ==
12523 error_mark_node, we will have issued a diagnostic to the
12524 user, as opposed to simply marking the tentative parse as
12525 failed? */
12526 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12527 error_at (token->location, "parse error in template argument list");
12530 pop_deferring_access_checks ();
12531 return template_id;
12534 /* Parse a template-name.
12536 template-name:
12537 identifier
12539 The standard should actually say:
12541 template-name:
12542 identifier
12543 operator-function-id
12545 A defect report has been filed about this issue.
12547 A conversion-function-id cannot be a template name because they cannot
12548 be part of a template-id. In fact, looking at this code:
12550 a.operator K<int>()
12552 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12553 It is impossible to call a templated conversion-function-id with an
12554 explicit argument list, since the only allowed template parameter is
12555 the type to which it is converting.
12557 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12558 `template' keyword, in a construction like:
12560 T::template f<3>()
12562 In that case `f' is taken to be a template-name, even though there
12563 is no way of knowing for sure.
12565 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12566 name refers to a set of overloaded functions, at least one of which
12567 is a template, or an IDENTIFIER_NODE with the name of the template,
12568 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12569 names are looked up inside uninstantiated templates. */
12571 static tree
12572 cp_parser_template_name (cp_parser* parser,
12573 bool template_keyword_p,
12574 bool check_dependency_p,
12575 bool is_declaration,
12576 bool *is_identifier)
12578 tree identifier;
12579 tree decl;
12580 tree fns;
12581 cp_token *token = cp_lexer_peek_token (parser->lexer);
12583 /* If the next token is `operator', then we have either an
12584 operator-function-id or a conversion-function-id. */
12585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12587 /* We don't know whether we're looking at an
12588 operator-function-id or a conversion-function-id. */
12589 cp_parser_parse_tentatively (parser);
12590 /* Try an operator-function-id. */
12591 identifier = cp_parser_operator_function_id (parser);
12592 /* If that didn't work, try a conversion-function-id. */
12593 if (!cp_parser_parse_definitely (parser))
12595 cp_parser_error (parser, "expected template-name");
12596 return error_mark_node;
12599 /* Look for the identifier. */
12600 else
12601 identifier = cp_parser_identifier (parser);
12603 /* If we didn't find an identifier, we don't have a template-id. */
12604 if (identifier == error_mark_node)
12605 return error_mark_node;
12607 /* If the name immediately followed the `template' keyword, then it
12608 is a template-name. However, if the next token is not `<', then
12609 we do not treat it as a template-name, since it is not being used
12610 as part of a template-id. This enables us to handle constructs
12611 like:
12613 template <typename T> struct S { S(); };
12614 template <typename T> S<T>::S();
12616 correctly. We would treat `S' as a template -- if it were `S<T>'
12617 -- but we do not if there is no `<'. */
12619 if (processing_template_decl
12620 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12622 /* In a declaration, in a dependent context, we pretend that the
12623 "template" keyword was present in order to improve error
12624 recovery. For example, given:
12626 template <typename T> void f(T::X<int>);
12628 we want to treat "X<int>" as a template-id. */
12629 if (is_declaration
12630 && !template_keyword_p
12631 && parser->scope && TYPE_P (parser->scope)
12632 && check_dependency_p
12633 && dependent_scope_p (parser->scope)
12634 /* Do not do this for dtors (or ctors), since they never
12635 need the template keyword before their name. */
12636 && !constructor_name_p (identifier, parser->scope))
12638 cp_token_position start = 0;
12640 /* Explain what went wrong. */
12641 error_at (token->location, "non-template %qD used as template",
12642 identifier);
12643 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12644 parser->scope, identifier);
12645 /* If parsing tentatively, find the location of the "<" token. */
12646 if (cp_parser_simulate_error (parser))
12647 start = cp_lexer_token_position (parser->lexer, true);
12648 /* Parse the template arguments so that we can issue error
12649 messages about them. */
12650 cp_lexer_consume_token (parser->lexer);
12651 cp_parser_enclosed_template_argument_list (parser);
12652 /* Skip tokens until we find a good place from which to
12653 continue parsing. */
12654 cp_parser_skip_to_closing_parenthesis (parser,
12655 /*recovering=*/true,
12656 /*or_comma=*/true,
12657 /*consume_paren=*/false);
12658 /* If parsing tentatively, permanently remove the
12659 template argument list. That will prevent duplicate
12660 error messages from being issued about the missing
12661 "template" keyword. */
12662 if (start)
12663 cp_lexer_purge_tokens_after (parser->lexer, start);
12664 if (is_identifier)
12665 *is_identifier = true;
12666 return identifier;
12669 /* If the "template" keyword is present, then there is generally
12670 no point in doing name-lookup, so we just return IDENTIFIER.
12671 But, if the qualifying scope is non-dependent then we can
12672 (and must) do name-lookup normally. */
12673 if (template_keyword_p
12674 && (!parser->scope
12675 || (TYPE_P (parser->scope)
12676 && dependent_type_p (parser->scope))))
12677 return identifier;
12680 /* Look up the name. */
12681 decl = cp_parser_lookup_name (parser, identifier,
12682 none_type,
12683 /*is_template=*/true,
12684 /*is_namespace=*/false,
12685 check_dependency_p,
12686 /*ambiguous_decls=*/NULL,
12687 token->location);
12689 /* If DECL is a template, then the name was a template-name. */
12690 if (TREE_CODE (decl) == TEMPLATE_DECL)
12692 else
12694 tree fn = NULL_TREE;
12696 /* The standard does not explicitly indicate whether a name that
12697 names a set of overloaded declarations, some of which are
12698 templates, is a template-name. However, such a name should
12699 be a template-name; otherwise, there is no way to form a
12700 template-id for the overloaded templates. */
12701 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12702 if (TREE_CODE (fns) == OVERLOAD)
12703 for (fn = fns; fn; fn = OVL_NEXT (fn))
12704 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12705 break;
12707 if (!fn)
12709 /* The name does not name a template. */
12710 cp_parser_error (parser, "expected template-name");
12711 return error_mark_node;
12715 /* If DECL is dependent, and refers to a function, then just return
12716 its name; we will look it up again during template instantiation. */
12717 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12719 tree scope = ovl_scope (decl);
12720 if (TYPE_P (scope) && dependent_type_p (scope))
12721 return identifier;
12724 return decl;
12727 /* Parse a template-argument-list.
12729 template-argument-list:
12730 template-argument ... [opt]
12731 template-argument-list , template-argument ... [opt]
12733 Returns a TREE_VEC containing the arguments. */
12735 static tree
12736 cp_parser_template_argument_list (cp_parser* parser)
12738 tree fixed_args[10];
12739 unsigned n_args = 0;
12740 unsigned alloced = 10;
12741 tree *arg_ary = fixed_args;
12742 tree vec;
12743 bool saved_in_template_argument_list_p;
12744 bool saved_ice_p;
12745 bool saved_non_ice_p;
12747 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12748 parser->in_template_argument_list_p = true;
12749 /* Even if the template-id appears in an integral
12750 constant-expression, the contents of the argument list do
12751 not. */
12752 saved_ice_p = parser->integral_constant_expression_p;
12753 parser->integral_constant_expression_p = false;
12754 saved_non_ice_p = parser->non_integral_constant_expression_p;
12755 parser->non_integral_constant_expression_p = false;
12757 /* Parse the arguments. */
12760 tree argument;
12762 if (n_args)
12763 /* Consume the comma. */
12764 cp_lexer_consume_token (parser->lexer);
12766 /* Parse the template-argument. */
12767 argument = cp_parser_template_argument (parser);
12769 /* If the next token is an ellipsis, we're expanding a template
12770 argument pack. */
12771 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12773 if (argument == error_mark_node)
12775 cp_token *token = cp_lexer_peek_token (parser->lexer);
12776 error_at (token->location,
12777 "expected parameter pack before %<...%>");
12779 /* Consume the `...' token. */
12780 cp_lexer_consume_token (parser->lexer);
12782 /* Make the argument into a TYPE_PACK_EXPANSION or
12783 EXPR_PACK_EXPANSION. */
12784 argument = make_pack_expansion (argument);
12787 if (n_args == alloced)
12789 alloced *= 2;
12791 if (arg_ary == fixed_args)
12793 arg_ary = XNEWVEC (tree, alloced);
12794 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12796 else
12797 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12799 arg_ary[n_args++] = argument;
12801 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12803 vec = make_tree_vec (n_args);
12805 while (n_args--)
12806 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12808 if (arg_ary != fixed_args)
12809 free (arg_ary);
12810 parser->non_integral_constant_expression_p = saved_non_ice_p;
12811 parser->integral_constant_expression_p = saved_ice_p;
12812 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12813 #ifdef ENABLE_CHECKING
12814 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12815 #endif
12816 return vec;
12819 /* Parse a template-argument.
12821 template-argument:
12822 assignment-expression
12823 type-id
12824 id-expression
12826 The representation is that of an assignment-expression, type-id, or
12827 id-expression -- except that the qualified id-expression is
12828 evaluated, so that the value returned is either a DECL or an
12829 OVERLOAD.
12831 Although the standard says "assignment-expression", it forbids
12832 throw-expressions or assignments in the template argument.
12833 Therefore, we use "conditional-expression" instead. */
12835 static tree
12836 cp_parser_template_argument (cp_parser* parser)
12838 tree argument;
12839 bool template_p;
12840 bool address_p;
12841 bool maybe_type_id = false;
12842 cp_token *token = NULL, *argument_start_token = NULL;
12843 location_t loc = 0;
12844 cp_id_kind idk;
12846 /* There's really no way to know what we're looking at, so we just
12847 try each alternative in order.
12849 [temp.arg]
12851 In a template-argument, an ambiguity between a type-id and an
12852 expression is resolved to a type-id, regardless of the form of
12853 the corresponding template-parameter.
12855 Therefore, we try a type-id first. */
12856 cp_parser_parse_tentatively (parser);
12857 argument = cp_parser_template_type_arg (parser);
12858 /* If there was no error parsing the type-id but the next token is a
12859 '>>', our behavior depends on which dialect of C++ we're
12860 parsing. In C++98, we probably found a typo for '> >'. But there
12861 are type-id which are also valid expressions. For instance:
12863 struct X { int operator >> (int); };
12864 template <int V> struct Foo {};
12865 Foo<X () >> 5> r;
12867 Here 'X()' is a valid type-id of a function type, but the user just
12868 wanted to write the expression "X() >> 5". Thus, we remember that we
12869 found a valid type-id, but we still try to parse the argument as an
12870 expression to see what happens.
12872 In C++0x, the '>>' will be considered two separate '>'
12873 tokens. */
12874 if (!cp_parser_error_occurred (parser)
12875 && cxx_dialect == cxx98
12876 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12878 maybe_type_id = true;
12879 cp_parser_abort_tentative_parse (parser);
12881 else
12883 /* If the next token isn't a `,' or a `>', then this argument wasn't
12884 really finished. This means that the argument is not a valid
12885 type-id. */
12886 if (!cp_parser_next_token_ends_template_argument_p (parser))
12887 cp_parser_error (parser, "expected template-argument");
12888 /* If that worked, we're done. */
12889 if (cp_parser_parse_definitely (parser))
12890 return argument;
12892 /* We're still not sure what the argument will be. */
12893 cp_parser_parse_tentatively (parser);
12894 /* Try a template. */
12895 argument_start_token = cp_lexer_peek_token (parser->lexer);
12896 argument = cp_parser_id_expression (parser,
12897 /*template_keyword_p=*/false,
12898 /*check_dependency_p=*/true,
12899 &template_p,
12900 /*declarator_p=*/false,
12901 /*optional_p=*/false);
12902 /* If the next token isn't a `,' or a `>', then this argument wasn't
12903 really finished. */
12904 if (!cp_parser_next_token_ends_template_argument_p (parser))
12905 cp_parser_error (parser, "expected template-argument");
12906 if (!cp_parser_error_occurred (parser))
12908 /* Figure out what is being referred to. If the id-expression
12909 was for a class template specialization, then we will have a
12910 TYPE_DECL at this point. There is no need to do name lookup
12911 at this point in that case. */
12912 if (TREE_CODE (argument) != TYPE_DECL)
12913 argument = cp_parser_lookup_name (parser, argument,
12914 none_type,
12915 /*is_template=*/template_p,
12916 /*is_namespace=*/false,
12917 /*check_dependency=*/true,
12918 /*ambiguous_decls=*/NULL,
12919 argument_start_token->location);
12920 if (TREE_CODE (argument) != TEMPLATE_DECL
12921 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12922 cp_parser_error (parser, "expected template-name");
12924 if (cp_parser_parse_definitely (parser))
12925 return argument;
12926 /* It must be a non-type argument. There permitted cases are given
12927 in [temp.arg.nontype]:
12929 -- an integral constant-expression of integral or enumeration
12930 type; or
12932 -- the name of a non-type template-parameter; or
12934 -- the name of an object or function with external linkage...
12936 -- the address of an object or function with external linkage...
12938 -- a pointer to member... */
12939 /* Look for a non-type template parameter. */
12940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12942 cp_parser_parse_tentatively (parser);
12943 argument = cp_parser_primary_expression (parser,
12944 /*address_p=*/false,
12945 /*cast_p=*/false,
12946 /*template_arg_p=*/true,
12947 &idk);
12948 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12949 || !cp_parser_next_token_ends_template_argument_p (parser))
12950 cp_parser_simulate_error (parser);
12951 if (cp_parser_parse_definitely (parser))
12952 return argument;
12955 /* If the next token is "&", the argument must be the address of an
12956 object or function with external linkage. */
12957 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12958 if (address_p)
12960 loc = cp_lexer_peek_token (parser->lexer)->location;
12961 cp_lexer_consume_token (parser->lexer);
12963 /* See if we might have an id-expression. */
12964 token = cp_lexer_peek_token (parser->lexer);
12965 if (token->type == CPP_NAME
12966 || token->keyword == RID_OPERATOR
12967 || token->type == CPP_SCOPE
12968 || token->type == CPP_TEMPLATE_ID
12969 || token->type == CPP_NESTED_NAME_SPECIFIER)
12971 cp_parser_parse_tentatively (parser);
12972 argument = cp_parser_primary_expression (parser,
12973 address_p,
12974 /*cast_p=*/false,
12975 /*template_arg_p=*/true,
12976 &idk);
12977 if (cp_parser_error_occurred (parser)
12978 || !cp_parser_next_token_ends_template_argument_p (parser))
12979 cp_parser_abort_tentative_parse (parser);
12980 else
12982 tree probe;
12984 if (TREE_CODE (argument) == INDIRECT_REF)
12986 gcc_assert (REFERENCE_REF_P (argument));
12987 argument = TREE_OPERAND (argument, 0);
12990 /* If we're in a template, we represent a qualified-id referring
12991 to a static data member as a SCOPE_REF even if the scope isn't
12992 dependent so that we can check access control later. */
12993 probe = argument;
12994 if (TREE_CODE (probe) == SCOPE_REF)
12995 probe = TREE_OPERAND (probe, 1);
12996 if (TREE_CODE (probe) == VAR_DECL)
12998 /* A variable without external linkage might still be a
12999 valid constant-expression, so no error is issued here
13000 if the external-linkage check fails. */
13001 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13002 cp_parser_simulate_error (parser);
13004 else if (is_overloaded_fn (argument))
13005 /* All overloaded functions are allowed; if the external
13006 linkage test does not pass, an error will be issued
13007 later. */
13009 else if (address_p
13010 && (TREE_CODE (argument) == OFFSET_REF
13011 || TREE_CODE (argument) == SCOPE_REF))
13012 /* A pointer-to-member. */
13014 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13016 else
13017 cp_parser_simulate_error (parser);
13019 if (cp_parser_parse_definitely (parser))
13021 if (address_p)
13022 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13023 tf_warning_or_error);
13024 return argument;
13028 /* If the argument started with "&", there are no other valid
13029 alternatives at this point. */
13030 if (address_p)
13032 cp_parser_error (parser, "invalid non-type template argument");
13033 return error_mark_node;
13036 /* If the argument wasn't successfully parsed as a type-id followed
13037 by '>>', the argument can only be a constant expression now.
13038 Otherwise, we try parsing the constant-expression tentatively,
13039 because the argument could really be a type-id. */
13040 if (maybe_type_id)
13041 cp_parser_parse_tentatively (parser);
13042 argument = cp_parser_constant_expression (parser,
13043 /*allow_non_constant_p=*/false,
13044 /*non_constant_p=*/NULL);
13045 argument = fold_non_dependent_expr (argument);
13046 if (!maybe_type_id)
13047 return argument;
13048 if (!cp_parser_next_token_ends_template_argument_p (parser))
13049 cp_parser_error (parser, "expected template-argument");
13050 if (cp_parser_parse_definitely (parser))
13051 return argument;
13052 /* We did our best to parse the argument as a non type-id, but that
13053 was the only alternative that matched (albeit with a '>' after
13054 it). We can assume it's just a typo from the user, and a
13055 diagnostic will then be issued. */
13056 return cp_parser_template_type_arg (parser);
13059 /* Parse an explicit-instantiation.
13061 explicit-instantiation:
13062 template declaration
13064 Although the standard says `declaration', what it really means is:
13066 explicit-instantiation:
13067 template decl-specifier-seq [opt] declarator [opt] ;
13069 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13070 supposed to be allowed. A defect report has been filed about this
13071 issue.
13073 GNU Extension:
13075 explicit-instantiation:
13076 storage-class-specifier template
13077 decl-specifier-seq [opt] declarator [opt] ;
13078 function-specifier template
13079 decl-specifier-seq [opt] declarator [opt] ; */
13081 static void
13082 cp_parser_explicit_instantiation (cp_parser* parser)
13084 int declares_class_or_enum;
13085 cp_decl_specifier_seq decl_specifiers;
13086 tree extension_specifier = NULL_TREE;
13088 timevar_push (TV_TEMPLATE_INST);
13090 /* Look for an (optional) storage-class-specifier or
13091 function-specifier. */
13092 if (cp_parser_allow_gnu_extensions_p (parser))
13094 extension_specifier
13095 = cp_parser_storage_class_specifier_opt (parser);
13096 if (!extension_specifier)
13097 extension_specifier
13098 = cp_parser_function_specifier_opt (parser,
13099 /*decl_specs=*/NULL);
13102 /* Look for the `template' keyword. */
13103 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13104 /* Let the front end know that we are processing an explicit
13105 instantiation. */
13106 begin_explicit_instantiation ();
13107 /* [temp.explicit] says that we are supposed to ignore access
13108 control while processing explicit instantiation directives. */
13109 push_deferring_access_checks (dk_no_check);
13110 /* Parse a decl-specifier-seq. */
13111 cp_parser_decl_specifier_seq (parser,
13112 CP_PARSER_FLAGS_OPTIONAL,
13113 &decl_specifiers,
13114 &declares_class_or_enum);
13115 /* If there was exactly one decl-specifier, and it declared a class,
13116 and there's no declarator, then we have an explicit type
13117 instantiation. */
13118 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13120 tree type;
13122 type = check_tag_decl (&decl_specifiers);
13123 /* Turn access control back on for names used during
13124 template instantiation. */
13125 pop_deferring_access_checks ();
13126 if (type)
13127 do_type_instantiation (type, extension_specifier,
13128 /*complain=*/tf_error);
13130 else
13132 cp_declarator *declarator;
13133 tree decl;
13135 /* Parse the declarator. */
13136 declarator
13137 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13138 /*ctor_dtor_or_conv_p=*/NULL,
13139 /*parenthesized_p=*/NULL,
13140 /*member_p=*/false);
13141 if (declares_class_or_enum & 2)
13142 cp_parser_check_for_definition_in_return_type (declarator,
13143 decl_specifiers.type,
13144 decl_specifiers.locations[ds_type_spec]);
13145 if (declarator != cp_error_declarator)
13147 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13148 permerror (decl_specifiers.locations[ds_inline],
13149 "explicit instantiation shall not use"
13150 " %<inline%> specifier");
13151 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13152 permerror (decl_specifiers.locations[ds_constexpr],
13153 "explicit instantiation shall not use"
13154 " %<constexpr%> specifier");
13156 decl = grokdeclarator (declarator, &decl_specifiers,
13157 NORMAL, 0, &decl_specifiers.attributes);
13158 /* Turn access control back on for names used during
13159 template instantiation. */
13160 pop_deferring_access_checks ();
13161 /* Do the explicit instantiation. */
13162 do_decl_instantiation (decl, extension_specifier);
13164 else
13166 pop_deferring_access_checks ();
13167 /* Skip the body of the explicit instantiation. */
13168 cp_parser_skip_to_end_of_statement (parser);
13171 /* We're done with the instantiation. */
13172 end_explicit_instantiation ();
13174 cp_parser_consume_semicolon_at_end_of_statement (parser);
13176 timevar_pop (TV_TEMPLATE_INST);
13179 /* Parse an explicit-specialization.
13181 explicit-specialization:
13182 template < > declaration
13184 Although the standard says `declaration', what it really means is:
13186 explicit-specialization:
13187 template <> decl-specifier [opt] init-declarator [opt] ;
13188 template <> function-definition
13189 template <> explicit-specialization
13190 template <> template-declaration */
13192 static void
13193 cp_parser_explicit_specialization (cp_parser* parser)
13195 bool need_lang_pop;
13196 cp_token *token = cp_lexer_peek_token (parser->lexer);
13198 /* Look for the `template' keyword. */
13199 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13200 /* Look for the `<'. */
13201 cp_parser_require (parser, CPP_LESS, RT_LESS);
13202 /* Look for the `>'. */
13203 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13204 /* We have processed another parameter list. */
13205 ++parser->num_template_parameter_lists;
13206 /* [temp]
13208 A template ... explicit specialization ... shall not have C
13209 linkage. */
13210 if (current_lang_name == lang_name_c)
13212 error_at (token->location, "template specialization with C linkage");
13213 /* Give it C++ linkage to avoid confusing other parts of the
13214 front end. */
13215 push_lang_context (lang_name_cplusplus);
13216 need_lang_pop = true;
13218 else
13219 need_lang_pop = false;
13220 /* Let the front end know that we are beginning a specialization. */
13221 if (!begin_specialization ())
13223 end_specialization ();
13224 return;
13227 /* If the next keyword is `template', we need to figure out whether
13228 or not we're looking a template-declaration. */
13229 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13231 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13232 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13233 cp_parser_template_declaration_after_export (parser,
13234 /*member_p=*/false);
13235 else
13236 cp_parser_explicit_specialization (parser);
13238 else
13239 /* Parse the dependent declaration. */
13240 cp_parser_single_declaration (parser,
13241 /*checks=*/NULL,
13242 /*member_p=*/false,
13243 /*explicit_specialization_p=*/true,
13244 /*friend_p=*/NULL);
13245 /* We're done with the specialization. */
13246 end_specialization ();
13247 /* For the erroneous case of a template with C linkage, we pushed an
13248 implicit C++ linkage scope; exit that scope now. */
13249 if (need_lang_pop)
13250 pop_lang_context ();
13251 /* We're done with this parameter list. */
13252 --parser->num_template_parameter_lists;
13255 /* Parse a type-specifier.
13257 type-specifier:
13258 simple-type-specifier
13259 class-specifier
13260 enum-specifier
13261 elaborated-type-specifier
13262 cv-qualifier
13264 GNU Extension:
13266 type-specifier:
13267 __complex__
13269 Returns a representation of the type-specifier. For a
13270 class-specifier, enum-specifier, or elaborated-type-specifier, a
13271 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13273 The parser flags FLAGS is used to control type-specifier parsing.
13275 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13276 in a decl-specifier-seq.
13278 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13279 class-specifier, enum-specifier, or elaborated-type-specifier, then
13280 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13281 if a type is declared; 2 if it is defined. Otherwise, it is set to
13282 zero.
13284 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13285 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13286 is set to FALSE. */
13288 static tree
13289 cp_parser_type_specifier (cp_parser* parser,
13290 cp_parser_flags flags,
13291 cp_decl_specifier_seq *decl_specs,
13292 bool is_declaration,
13293 int* declares_class_or_enum,
13294 bool* is_cv_qualifier)
13296 tree type_spec = NULL_TREE;
13297 cp_token *token;
13298 enum rid keyword;
13299 cp_decl_spec ds = ds_last;
13301 /* Assume this type-specifier does not declare a new type. */
13302 if (declares_class_or_enum)
13303 *declares_class_or_enum = 0;
13304 /* And that it does not specify a cv-qualifier. */
13305 if (is_cv_qualifier)
13306 *is_cv_qualifier = false;
13307 /* Peek at the next token. */
13308 token = cp_lexer_peek_token (parser->lexer);
13310 /* If we're looking at a keyword, we can use that to guide the
13311 production we choose. */
13312 keyword = token->keyword;
13313 switch (keyword)
13315 case RID_ENUM:
13316 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13317 goto elaborated_type_specifier;
13319 /* Look for the enum-specifier. */
13320 type_spec = cp_parser_enum_specifier (parser);
13321 /* If that worked, we're done. */
13322 if (type_spec)
13324 if (declares_class_or_enum)
13325 *declares_class_or_enum = 2;
13326 if (decl_specs)
13327 cp_parser_set_decl_spec_type (decl_specs,
13328 type_spec,
13329 token->location,
13330 /*type_definition_p=*/true);
13331 return type_spec;
13333 else
13334 goto elaborated_type_specifier;
13336 /* Any of these indicate either a class-specifier, or an
13337 elaborated-type-specifier. */
13338 case RID_CLASS:
13339 case RID_STRUCT:
13340 case RID_UNION:
13341 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13342 goto elaborated_type_specifier;
13344 /* Parse tentatively so that we can back up if we don't find a
13345 class-specifier. */
13346 cp_parser_parse_tentatively (parser);
13347 /* Look for the class-specifier. */
13348 type_spec = cp_parser_class_specifier (parser);
13349 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13350 /* If that worked, we're done. */
13351 if (cp_parser_parse_definitely (parser))
13353 if (declares_class_or_enum)
13354 *declares_class_or_enum = 2;
13355 if (decl_specs)
13356 cp_parser_set_decl_spec_type (decl_specs,
13357 type_spec,
13358 token->location,
13359 /*type_definition_p=*/true);
13360 return type_spec;
13363 /* Fall through. */
13364 elaborated_type_specifier:
13365 /* We're declaring (not defining) a class or enum. */
13366 if (declares_class_or_enum)
13367 *declares_class_or_enum = 1;
13369 /* Fall through. */
13370 case RID_TYPENAME:
13371 /* Look for an elaborated-type-specifier. */
13372 type_spec
13373 = (cp_parser_elaborated_type_specifier
13374 (parser,
13375 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13376 is_declaration));
13377 if (decl_specs)
13378 cp_parser_set_decl_spec_type (decl_specs,
13379 type_spec,
13380 token->location,
13381 /*type_definition_p=*/false);
13382 return type_spec;
13384 case RID_CONST:
13385 ds = ds_const;
13386 if (is_cv_qualifier)
13387 *is_cv_qualifier = true;
13388 break;
13390 case RID_VOLATILE:
13391 ds = ds_volatile;
13392 if (is_cv_qualifier)
13393 *is_cv_qualifier = true;
13394 break;
13396 case RID_RESTRICT:
13397 ds = ds_restrict;
13398 if (is_cv_qualifier)
13399 *is_cv_qualifier = true;
13400 break;
13402 case RID_COMPLEX:
13403 /* The `__complex__' keyword is a GNU extension. */
13404 ds = ds_complex;
13405 break;
13407 default:
13408 break;
13411 /* Handle simple keywords. */
13412 if (ds != ds_last)
13414 if (decl_specs)
13416 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
13417 decl_specs->any_specifiers_p = true;
13419 return cp_lexer_consume_token (parser->lexer)->u.value;
13422 /* If we do not already have a type-specifier, assume we are looking
13423 at a simple-type-specifier. */
13424 type_spec = cp_parser_simple_type_specifier (parser,
13425 decl_specs,
13426 flags);
13428 /* If we didn't find a type-specifier, and a type-specifier was not
13429 optional in this context, issue an error message. */
13430 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13432 cp_parser_error (parser, "expected type specifier");
13433 return error_mark_node;
13436 return type_spec;
13439 /* Parse a simple-type-specifier.
13441 simple-type-specifier:
13442 :: [opt] nested-name-specifier [opt] type-name
13443 :: [opt] nested-name-specifier template template-id
13444 char
13445 wchar_t
13446 bool
13447 short
13449 long
13450 signed
13451 unsigned
13452 float
13453 double
13454 void
13456 C++0x Extension:
13458 simple-type-specifier:
13459 auto
13460 decltype ( expression )
13461 char16_t
13462 char32_t
13463 __underlying_type ( type-id )
13465 GNU Extension:
13467 simple-type-specifier:
13468 __int128
13469 __typeof__ unary-expression
13470 __typeof__ ( type-id )
13472 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13473 appropriately updated. */
13475 static tree
13476 cp_parser_simple_type_specifier (cp_parser* parser,
13477 cp_decl_specifier_seq *decl_specs,
13478 cp_parser_flags flags)
13480 tree type = NULL_TREE;
13481 cp_token *token;
13483 /* Peek at the next token. */
13484 token = cp_lexer_peek_token (parser->lexer);
13486 /* If we're looking at a keyword, things are easy. */
13487 switch (token->keyword)
13489 case RID_CHAR:
13490 if (decl_specs)
13491 decl_specs->explicit_char_p = true;
13492 type = char_type_node;
13493 break;
13494 case RID_CHAR16:
13495 type = char16_type_node;
13496 break;
13497 case RID_CHAR32:
13498 type = char32_type_node;
13499 break;
13500 case RID_WCHAR:
13501 type = wchar_type_node;
13502 break;
13503 case RID_BOOL:
13504 type = boolean_type_node;
13505 break;
13506 case RID_SHORT:
13507 set_and_check_decl_spec_loc (decl_specs, ds_short, token->location);
13508 type = short_integer_type_node;
13509 break;
13510 case RID_INT:
13511 if (decl_specs)
13512 decl_specs->explicit_int_p = true;
13513 type = integer_type_node;
13514 break;
13515 case RID_INT128:
13516 if (!int128_integer_type_node)
13517 break;
13518 if (decl_specs)
13519 decl_specs->explicit_int128_p = true;
13520 type = int128_integer_type_node;
13521 break;
13522 case RID_LONG:
13523 if (decl_specs)
13524 set_and_check_decl_spec_loc (decl_specs, ds_long, token->location);
13525 type = long_integer_type_node;
13526 break;
13527 case RID_SIGNED:
13528 set_and_check_decl_spec_loc (decl_specs, ds_signed, token->location);
13529 type = integer_type_node;
13530 break;
13531 case RID_UNSIGNED:
13532 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token->location);
13533 type = unsigned_type_node;
13534 break;
13535 case RID_FLOAT:
13536 type = float_type_node;
13537 break;
13538 case RID_DOUBLE:
13539 type = double_type_node;
13540 break;
13541 case RID_VOID:
13542 type = void_type_node;
13543 break;
13545 case RID_AUTO:
13546 maybe_warn_cpp0x (CPP0X_AUTO);
13547 type = make_auto ();
13548 break;
13550 case RID_DECLTYPE:
13551 /* Since DR 743, decltype can either be a simple-type-specifier by
13552 itself or begin a nested-name-specifier. Parsing it will replace
13553 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13554 handling below decide what to do. */
13555 cp_parser_decltype (parser);
13556 cp_lexer_set_token_position (parser->lexer, token);
13557 break;
13559 case RID_TYPEOF:
13560 /* Consume the `typeof' token. */
13561 cp_lexer_consume_token (parser->lexer);
13562 /* Parse the operand to `typeof'. */
13563 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13564 /* If it is not already a TYPE, take its type. */
13565 if (!TYPE_P (type))
13566 type = finish_typeof (type);
13568 if (decl_specs)
13569 cp_parser_set_decl_spec_type (decl_specs, type,
13570 token->location,
13571 /*type_definition_p=*/false);
13573 return type;
13575 case RID_UNDERLYING_TYPE:
13576 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13577 if (decl_specs)
13578 cp_parser_set_decl_spec_type (decl_specs, type,
13579 token->location,
13580 /*type_definition_p=*/false);
13582 return type;
13584 case RID_BASES:
13585 case RID_DIRECT_BASES:
13586 type = cp_parser_trait_expr (parser, token->keyword);
13587 if (decl_specs)
13588 cp_parser_set_decl_spec_type (decl_specs, type,
13589 token->location,
13590 /*type_definition_p=*/false);
13591 return type;
13592 default:
13593 break;
13596 /* If token is an already-parsed decltype not followed by ::,
13597 it's a simple-type-specifier. */
13598 if (token->type == CPP_DECLTYPE
13599 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13601 type = token->u.value;
13602 if (decl_specs)
13603 cp_parser_set_decl_spec_type (decl_specs, type,
13604 token->location,
13605 /*type_definition_p=*/false);
13606 cp_lexer_consume_token (parser->lexer);
13607 return type;
13610 /* If the type-specifier was for a built-in type, we're done. */
13611 if (type)
13613 /* Record the type. */
13614 if (decl_specs
13615 && (token->keyword != RID_SIGNED
13616 && token->keyword != RID_UNSIGNED
13617 && token->keyword != RID_SHORT
13618 && token->keyword != RID_LONG))
13619 cp_parser_set_decl_spec_type (decl_specs,
13620 type,
13621 token->location,
13622 /*type_definition_p=*/false);
13623 if (decl_specs)
13624 decl_specs->any_specifiers_p = true;
13626 /* Consume the token. */
13627 cp_lexer_consume_token (parser->lexer);
13629 /* There is no valid C++ program where a non-template type is
13630 followed by a "<". That usually indicates that the user thought
13631 that the type was a template. */
13632 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13634 return TYPE_NAME (type);
13637 /* The type-specifier must be a user-defined type. */
13638 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13640 bool qualified_p;
13641 bool global_p;
13643 /* Don't gobble tokens or issue error messages if this is an
13644 optional type-specifier. */
13645 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13646 cp_parser_parse_tentatively (parser);
13648 /* Look for the optional `::' operator. */
13649 global_p
13650 = (cp_parser_global_scope_opt (parser,
13651 /*current_scope_valid_p=*/false)
13652 != NULL_TREE);
13653 /* Look for the nested-name specifier. */
13654 qualified_p
13655 = (cp_parser_nested_name_specifier_opt (parser,
13656 /*typename_keyword_p=*/false,
13657 /*check_dependency_p=*/true,
13658 /*type_p=*/false,
13659 /*is_declaration=*/false)
13660 != NULL_TREE);
13661 token = cp_lexer_peek_token (parser->lexer);
13662 /* If we have seen a nested-name-specifier, and the next token
13663 is `template', then we are using the template-id production. */
13664 if (parser->scope
13665 && cp_parser_optional_template_keyword (parser))
13667 /* Look for the template-id. */
13668 type = cp_parser_template_id (parser,
13669 /*template_keyword_p=*/true,
13670 /*check_dependency_p=*/true,
13671 /*is_declaration=*/false);
13672 /* If the template-id did not name a type, we are out of
13673 luck. */
13674 if (TREE_CODE (type) != TYPE_DECL)
13676 cp_parser_error (parser, "expected template-id for type");
13677 type = NULL_TREE;
13680 /* Otherwise, look for a type-name. */
13681 else
13682 type = cp_parser_type_name (parser);
13683 /* Keep track of all name-lookups performed in class scopes. */
13684 if (type
13685 && !global_p
13686 && !qualified_p
13687 && TREE_CODE (type) == TYPE_DECL
13688 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13689 maybe_note_name_used_in_class (DECL_NAME (type), type);
13690 /* If it didn't work out, we don't have a TYPE. */
13691 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13692 && !cp_parser_parse_definitely (parser))
13693 type = NULL_TREE;
13694 if (type && decl_specs)
13695 cp_parser_set_decl_spec_type (decl_specs, type,
13696 token->location,
13697 /*type_definition_p=*/false);
13700 /* If we didn't get a type-name, issue an error message. */
13701 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13703 cp_parser_error (parser, "expected type-name");
13704 return error_mark_node;
13707 if (type && type != error_mark_node)
13709 /* See if TYPE is an Objective-C type, and if so, parse and
13710 accept any protocol references following it. Do this before
13711 the cp_parser_check_for_invalid_template_id() call, because
13712 Objective-C types can be followed by '<...>' which would
13713 enclose protocol names rather than template arguments, and so
13714 everything is fine. */
13715 if (c_dialect_objc () && !parser->scope
13716 && (objc_is_id (type) || objc_is_class_name (type)))
13718 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13719 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13721 /* Clobber the "unqualified" type previously entered into
13722 DECL_SPECS with the new, improved protocol-qualified version. */
13723 if (decl_specs)
13724 decl_specs->type = qual_type;
13726 return qual_type;
13729 /* There is no valid C++ program where a non-template type is
13730 followed by a "<". That usually indicates that the user
13731 thought that the type was a template. */
13732 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13733 token->location);
13736 return type;
13739 /* Parse a type-name.
13741 type-name:
13742 class-name
13743 enum-name
13744 typedef-name
13745 simple-template-id [in c++0x]
13747 enum-name:
13748 identifier
13750 typedef-name:
13751 identifier
13753 Returns a TYPE_DECL for the type. */
13755 static tree
13756 cp_parser_type_name (cp_parser* parser)
13758 tree type_decl;
13760 /* We can't know yet whether it is a class-name or not. */
13761 cp_parser_parse_tentatively (parser);
13762 /* Try a class-name. */
13763 type_decl = cp_parser_class_name (parser,
13764 /*typename_keyword_p=*/false,
13765 /*template_keyword_p=*/false,
13766 none_type,
13767 /*check_dependency_p=*/true,
13768 /*class_head_p=*/false,
13769 /*is_declaration=*/false);
13770 /* If it's not a class-name, keep looking. */
13771 if (!cp_parser_parse_definitely (parser))
13773 if (cxx_dialect < cxx0x)
13774 /* It must be a typedef-name or an enum-name. */
13775 return cp_parser_nonclass_name (parser);
13777 cp_parser_parse_tentatively (parser);
13778 /* It is either a simple-template-id representing an
13779 instantiation of an alias template... */
13780 type_decl = cp_parser_template_id (parser,
13781 /*template_keyword_p=*/false,
13782 /*check_dependency_p=*/false,
13783 /*is_declaration=*/false);
13784 /* Note that this must be an instantiation of an alias template
13785 because [temp.names]/6 says:
13787 A template-id that names an alias template specialization
13788 is a type-name.
13790 Whereas [temp.names]/7 says:
13792 A simple-template-id that names a class template
13793 specialization is a class-name. */
13794 if (type_decl != NULL_TREE
13795 && TREE_CODE (type_decl) == TYPE_DECL
13796 && TYPE_DECL_ALIAS_P (type_decl))
13797 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13798 else
13799 cp_parser_simulate_error (parser);
13801 if (!cp_parser_parse_definitely (parser))
13802 /* ... Or a typedef-name or an enum-name. */
13803 return cp_parser_nonclass_name (parser);
13806 return type_decl;
13809 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13811 enum-name:
13812 identifier
13814 typedef-name:
13815 identifier
13817 Returns a TYPE_DECL for the type. */
13819 static tree
13820 cp_parser_nonclass_name (cp_parser* parser)
13822 tree type_decl;
13823 tree identifier;
13825 cp_token *token = cp_lexer_peek_token (parser->lexer);
13826 identifier = cp_parser_identifier (parser);
13827 if (identifier == error_mark_node)
13828 return error_mark_node;
13830 /* Look up the type-name. */
13831 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13833 if (TREE_CODE (type_decl) == USING_DECL)
13835 if (!DECL_DEPENDENT_P (type_decl))
13836 type_decl = strip_using_decl (type_decl);
13837 else if (USING_DECL_TYPENAME_P (type_decl))
13839 /* We have found a type introduced by a using
13840 declaration at class scope that refers to a dependent
13841 type.
13843 using typename :: [opt] nested-name-specifier unqualified-id ;
13845 type_decl = make_typename_type (TREE_TYPE (type_decl),
13846 DECL_NAME (type_decl),
13847 typename_type, tf_error);
13848 if (type_decl != error_mark_node)
13849 type_decl = TYPE_NAME (type_decl);
13853 if (TREE_CODE (type_decl) != TYPE_DECL
13854 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13856 /* See if this is an Objective-C type. */
13857 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13858 tree type = objc_get_protocol_qualified_type (identifier, protos);
13859 if (type)
13860 type_decl = TYPE_NAME (type);
13863 /* Issue an error if we did not find a type-name. */
13864 if (TREE_CODE (type_decl) != TYPE_DECL
13865 /* In Objective-C, we have the complication that class names are
13866 normally type names and start declarations (eg, the
13867 "NSObject" in "NSObject *object;"), but can be used in an
13868 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13869 is an expression. So, a classname followed by a dot is not a
13870 valid type-name. */
13871 || (objc_is_class_name (TREE_TYPE (type_decl))
13872 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13874 if (!cp_parser_simulate_error (parser))
13875 cp_parser_name_lookup_error (parser, identifier, type_decl,
13876 NLE_TYPE, token->location);
13877 return error_mark_node;
13879 /* Remember that the name was used in the definition of the
13880 current class so that we can check later to see if the
13881 meaning would have been different after the class was
13882 entirely defined. */
13883 else if (type_decl != error_mark_node
13884 && !parser->scope)
13885 maybe_note_name_used_in_class (identifier, type_decl);
13887 return type_decl;
13890 /* Parse an elaborated-type-specifier. Note that the grammar given
13891 here incorporates the resolution to DR68.
13893 elaborated-type-specifier:
13894 class-key :: [opt] nested-name-specifier [opt] identifier
13895 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13896 enum-key :: [opt] nested-name-specifier [opt] identifier
13897 typename :: [opt] nested-name-specifier identifier
13898 typename :: [opt] nested-name-specifier template [opt]
13899 template-id
13901 GNU extension:
13903 elaborated-type-specifier:
13904 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13905 class-key attributes :: [opt] nested-name-specifier [opt]
13906 template [opt] template-id
13907 enum attributes :: [opt] nested-name-specifier [opt] identifier
13909 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13910 declared `friend'. If IS_DECLARATION is TRUE, then this
13911 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13912 something is being declared.
13914 Returns the TYPE specified. */
13916 static tree
13917 cp_parser_elaborated_type_specifier (cp_parser* parser,
13918 bool is_friend,
13919 bool is_declaration)
13921 enum tag_types tag_type;
13922 tree identifier;
13923 tree type = NULL_TREE;
13924 tree attributes = NULL_TREE;
13925 tree globalscope;
13926 cp_token *token = NULL;
13928 /* See if we're looking at the `enum' keyword. */
13929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13931 /* Consume the `enum' token. */
13932 cp_lexer_consume_token (parser->lexer);
13933 /* Remember that it's an enumeration type. */
13934 tag_type = enum_type;
13935 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13936 enums) is used here. */
13937 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13938 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13940 pedwarn (input_location, 0, "elaborated-type-specifier "
13941 "for a scoped enum must not use the %<%D%> keyword",
13942 cp_lexer_peek_token (parser->lexer)->u.value);
13943 /* Consume the `struct' or `class' and parse it anyway. */
13944 cp_lexer_consume_token (parser->lexer);
13946 /* Parse the attributes. */
13947 attributes = cp_parser_attributes_opt (parser);
13949 /* Or, it might be `typename'. */
13950 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13951 RID_TYPENAME))
13953 /* Consume the `typename' token. */
13954 cp_lexer_consume_token (parser->lexer);
13955 /* Remember that it's a `typename' type. */
13956 tag_type = typename_type;
13958 /* Otherwise it must be a class-key. */
13959 else
13961 tag_type = cp_parser_class_key (parser);
13962 if (tag_type == none_type)
13963 return error_mark_node;
13964 /* Parse the attributes. */
13965 attributes = cp_parser_attributes_opt (parser);
13968 /* Look for the `::' operator. */
13969 globalscope = cp_parser_global_scope_opt (parser,
13970 /*current_scope_valid_p=*/false);
13971 /* Look for the nested-name-specifier. */
13972 if (tag_type == typename_type && !globalscope)
13974 if (!cp_parser_nested_name_specifier (parser,
13975 /*typename_keyword_p=*/true,
13976 /*check_dependency_p=*/true,
13977 /*type_p=*/true,
13978 is_declaration))
13979 return error_mark_node;
13981 else
13982 /* Even though `typename' is not present, the proposed resolution
13983 to Core Issue 180 says that in `class A<T>::B', `B' should be
13984 considered a type-name, even if `A<T>' is dependent. */
13985 cp_parser_nested_name_specifier_opt (parser,
13986 /*typename_keyword_p=*/true,
13987 /*check_dependency_p=*/true,
13988 /*type_p=*/true,
13989 is_declaration);
13990 /* For everything but enumeration types, consider a template-id.
13991 For an enumeration type, consider only a plain identifier. */
13992 if (tag_type != enum_type)
13994 bool template_p = false;
13995 tree decl;
13997 /* Allow the `template' keyword. */
13998 template_p = cp_parser_optional_template_keyword (parser);
13999 /* If we didn't see `template', we don't know if there's a
14000 template-id or not. */
14001 if (!template_p)
14002 cp_parser_parse_tentatively (parser);
14003 /* Parse the template-id. */
14004 token = cp_lexer_peek_token (parser->lexer);
14005 decl = cp_parser_template_id (parser, template_p,
14006 /*check_dependency_p=*/true,
14007 is_declaration);
14008 /* If we didn't find a template-id, look for an ordinary
14009 identifier. */
14010 if (!template_p && !cp_parser_parse_definitely (parser))
14012 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14013 in effect, then we must assume that, upon instantiation, the
14014 template will correspond to a class. */
14015 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14016 && tag_type == typename_type)
14017 type = make_typename_type (parser->scope, decl,
14018 typename_type,
14019 /*complain=*/tf_error);
14020 /* If the `typename' keyword is in effect and DECL is not a type
14021 decl. Then type is non existant. */
14022 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14023 type = NULL_TREE;
14024 else
14025 type = check_elaborated_type_specifier (tag_type, decl,
14026 /*allow_template_p=*/true);
14029 if (!type)
14031 token = cp_lexer_peek_token (parser->lexer);
14032 identifier = cp_parser_identifier (parser);
14034 if (identifier == error_mark_node)
14036 parser->scope = NULL_TREE;
14037 return error_mark_node;
14040 /* For a `typename', we needn't call xref_tag. */
14041 if (tag_type == typename_type
14042 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14043 return cp_parser_make_typename_type (parser, parser->scope,
14044 identifier,
14045 token->location);
14046 /* Look up a qualified name in the usual way. */
14047 if (parser->scope)
14049 tree decl;
14050 tree ambiguous_decls;
14052 decl = cp_parser_lookup_name (parser, identifier,
14053 tag_type,
14054 /*is_template=*/false,
14055 /*is_namespace=*/false,
14056 /*check_dependency=*/true,
14057 &ambiguous_decls,
14058 token->location);
14060 /* If the lookup was ambiguous, an error will already have been
14061 issued. */
14062 if (ambiguous_decls)
14063 return error_mark_node;
14065 /* If we are parsing friend declaration, DECL may be a
14066 TEMPLATE_DECL tree node here. However, we need to check
14067 whether this TEMPLATE_DECL results in valid code. Consider
14068 the following example:
14070 namespace N {
14071 template <class T> class C {};
14073 class X {
14074 template <class T> friend class N::C; // #1, valid code
14076 template <class T> class Y {
14077 friend class N::C; // #2, invalid code
14080 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14081 name lookup of `N::C'. We see that friend declaration must
14082 be template for the code to be valid. Note that
14083 processing_template_decl does not work here since it is
14084 always 1 for the above two cases. */
14086 decl = (cp_parser_maybe_treat_template_as_class
14087 (decl, /*tag_name_p=*/is_friend
14088 && parser->num_template_parameter_lists));
14090 if (TREE_CODE (decl) != TYPE_DECL)
14092 cp_parser_diagnose_invalid_type_name (parser,
14093 parser->scope,
14094 identifier,
14095 token->location);
14096 return error_mark_node;
14099 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14101 bool allow_template = (parser->num_template_parameter_lists
14102 || DECL_SELF_REFERENCE_P (decl));
14103 type = check_elaborated_type_specifier (tag_type, decl,
14104 allow_template);
14106 if (type == error_mark_node)
14107 return error_mark_node;
14110 /* Forward declarations of nested types, such as
14112 class C1::C2;
14113 class C1::C2::C3;
14115 are invalid unless all components preceding the final '::'
14116 are complete. If all enclosing types are complete, these
14117 declarations become merely pointless.
14119 Invalid forward declarations of nested types are errors
14120 caught elsewhere in parsing. Those that are pointless arrive
14121 here. */
14123 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14124 && !is_friend && !processing_explicit_instantiation)
14125 warning (0, "declaration %qD does not declare anything", decl);
14127 type = TREE_TYPE (decl);
14129 else
14131 /* An elaborated-type-specifier sometimes introduces a new type and
14132 sometimes names an existing type. Normally, the rule is that it
14133 introduces a new type only if there is not an existing type of
14134 the same name already in scope. For example, given:
14136 struct S {};
14137 void f() { struct S s; }
14139 the `struct S' in the body of `f' is the same `struct S' as in
14140 the global scope; the existing definition is used. However, if
14141 there were no global declaration, this would introduce a new
14142 local class named `S'.
14144 An exception to this rule applies to the following code:
14146 namespace N { struct S; }
14148 Here, the elaborated-type-specifier names a new type
14149 unconditionally; even if there is already an `S' in the
14150 containing scope this declaration names a new type.
14151 This exception only applies if the elaborated-type-specifier
14152 forms the complete declaration:
14154 [class.name]
14156 A declaration consisting solely of `class-key identifier ;' is
14157 either a redeclaration of the name in the current scope or a
14158 forward declaration of the identifier as a class name. It
14159 introduces the name into the current scope.
14161 We are in this situation precisely when the next token is a `;'.
14163 An exception to the exception is that a `friend' declaration does
14164 *not* name a new type; i.e., given:
14166 struct S { friend struct T; };
14168 `T' is not a new type in the scope of `S'.
14170 Also, `new struct S' or `sizeof (struct S)' never results in the
14171 definition of a new type; a new type can only be declared in a
14172 declaration context. */
14174 tag_scope ts;
14175 bool template_p;
14177 if (is_friend)
14178 /* Friends have special name lookup rules. */
14179 ts = ts_within_enclosing_non_class;
14180 else if (is_declaration
14181 && cp_lexer_next_token_is (parser->lexer,
14182 CPP_SEMICOLON))
14183 /* This is a `class-key identifier ;' */
14184 ts = ts_current;
14185 else
14186 ts = ts_global;
14188 template_p =
14189 (parser->num_template_parameter_lists
14190 && (cp_parser_next_token_starts_class_definition_p (parser)
14191 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14192 /* An unqualified name was used to reference this type, so
14193 there were no qualifying templates. */
14194 if (!cp_parser_check_template_parameters (parser,
14195 /*num_templates=*/0,
14196 token->location,
14197 /*declarator=*/NULL))
14198 return error_mark_node;
14199 type = xref_tag (tag_type, identifier, ts, template_p);
14203 if (type == error_mark_node)
14204 return error_mark_node;
14206 /* Allow attributes on forward declarations of classes. */
14207 if (attributes)
14209 if (TREE_CODE (type) == TYPENAME_TYPE)
14210 warning (OPT_Wattributes,
14211 "attributes ignored on uninstantiated type");
14212 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14213 && ! processing_explicit_instantiation)
14214 warning (OPT_Wattributes,
14215 "attributes ignored on template instantiation");
14216 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14217 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14218 else
14219 warning (OPT_Wattributes,
14220 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14223 if (tag_type != enum_type)
14225 /* Indicate whether this class was declared as a `class' or as a
14226 `struct'. */
14227 if (TREE_CODE (type) == RECORD_TYPE)
14228 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14229 cp_parser_check_class_key (tag_type, type);
14232 /* A "<" cannot follow an elaborated type specifier. If that
14233 happens, the user was probably trying to form a template-id. */
14234 cp_parser_check_for_invalid_template_id (parser, type, token->location);
14236 return type;
14239 /* Parse an enum-specifier.
14241 enum-specifier:
14242 enum-head { enumerator-list [opt] }
14243 enum-head { enumerator-list , } [C++0x]
14245 enum-head:
14246 enum-key identifier [opt] enum-base [opt]
14247 enum-key nested-name-specifier identifier enum-base [opt]
14249 enum-key:
14250 enum
14251 enum class [C++0x]
14252 enum struct [C++0x]
14254 enum-base: [C++0x]
14255 : type-specifier-seq
14257 opaque-enum-specifier:
14258 enum-key identifier enum-base [opt] ;
14260 GNU Extensions:
14261 enum-key attributes[opt] identifier [opt] enum-base [opt]
14262 { enumerator-list [opt] }attributes[opt]
14263 enum-key attributes[opt] identifier [opt] enum-base [opt]
14264 { enumerator-list, }attributes[opt] [C++0x]
14266 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14267 if the token stream isn't an enum-specifier after all. */
14269 static tree
14270 cp_parser_enum_specifier (cp_parser* parser)
14272 tree identifier;
14273 tree type = NULL_TREE;
14274 tree prev_scope;
14275 tree nested_name_specifier = NULL_TREE;
14276 tree attributes;
14277 bool scoped_enum_p = false;
14278 bool has_underlying_type = false;
14279 bool nested_being_defined = false;
14280 bool new_value_list = false;
14281 bool is_new_type = false;
14282 bool is_anonymous = false;
14283 tree underlying_type = NULL_TREE;
14284 cp_token *type_start_token = NULL;
14285 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14287 parser->colon_corrects_to_scope_p = false;
14289 /* Parse tentatively so that we can back up if we don't find a
14290 enum-specifier. */
14291 cp_parser_parse_tentatively (parser);
14293 /* Caller guarantees that the current token is 'enum', an identifier
14294 possibly follows, and the token after that is an opening brace.
14295 If we don't have an identifier, fabricate an anonymous name for
14296 the enumeration being defined. */
14297 cp_lexer_consume_token (parser->lexer);
14299 /* Parse the "class" or "struct", which indicates a scoped
14300 enumeration type in C++0x. */
14301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14302 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14304 if (cxx_dialect < cxx0x)
14305 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14307 /* Consume the `struct' or `class' token. */
14308 cp_lexer_consume_token (parser->lexer);
14310 scoped_enum_p = true;
14313 attributes = cp_parser_attributes_opt (parser);
14315 /* Clear the qualification. */
14316 parser->scope = NULL_TREE;
14317 parser->qualifying_scope = NULL_TREE;
14318 parser->object_scope = NULL_TREE;
14320 /* Figure out in what scope the declaration is being placed. */
14321 prev_scope = current_scope ();
14323 type_start_token = cp_lexer_peek_token (parser->lexer);
14325 push_deferring_access_checks (dk_no_check);
14326 nested_name_specifier
14327 = cp_parser_nested_name_specifier_opt (parser,
14328 /*typename_keyword_p=*/true,
14329 /*check_dependency_p=*/false,
14330 /*type_p=*/false,
14331 /*is_declaration=*/false);
14333 if (nested_name_specifier)
14335 tree name;
14337 identifier = cp_parser_identifier (parser);
14338 name = cp_parser_lookup_name (parser, identifier,
14339 enum_type,
14340 /*is_template=*/false,
14341 /*is_namespace=*/false,
14342 /*check_dependency=*/true,
14343 /*ambiguous_decls=*/NULL,
14344 input_location);
14345 if (name)
14347 type = TREE_TYPE (name);
14348 if (TREE_CODE (type) == TYPENAME_TYPE)
14350 /* Are template enums allowed in ISO? */
14351 if (template_parm_scope_p ())
14352 pedwarn (type_start_token->location, OPT_Wpedantic,
14353 "%qD is an enumeration template", name);
14354 /* ignore a typename reference, for it will be solved by name
14355 in start_enum. */
14356 type = NULL_TREE;
14359 else
14360 error_at (type_start_token->location,
14361 "%qD is not an enumerator-name", identifier);
14363 else
14365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14366 identifier = cp_parser_identifier (parser);
14367 else
14369 identifier = make_anon_name ();
14370 is_anonymous = true;
14373 pop_deferring_access_checks ();
14375 /* Check for the `:' that denotes a specified underlying type in C++0x.
14376 Note that a ':' could also indicate a bitfield width, however. */
14377 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14379 cp_decl_specifier_seq type_specifiers;
14381 /* Consume the `:'. */
14382 cp_lexer_consume_token (parser->lexer);
14384 /* Parse the type-specifier-seq. */
14385 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14386 /*is_trailing_return=*/false,
14387 &type_specifiers);
14389 /* At this point this is surely not elaborated type specifier. */
14390 if (!cp_parser_parse_definitely (parser))
14391 return NULL_TREE;
14393 if (cxx_dialect < cxx0x)
14394 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14396 has_underlying_type = true;
14398 /* If that didn't work, stop. */
14399 if (type_specifiers.type != error_mark_node)
14401 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14402 /*initialized=*/0, NULL);
14403 if (underlying_type == error_mark_node)
14404 underlying_type = NULL_TREE;
14408 /* Look for the `{' but don't consume it yet. */
14409 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14411 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14413 cp_parser_error (parser, "expected %<{%>");
14414 if (has_underlying_type)
14416 type = NULL_TREE;
14417 goto out;
14420 /* An opaque-enum-specifier must have a ';' here. */
14421 if ((scoped_enum_p || underlying_type)
14422 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14424 cp_parser_error (parser, "expected %<;%> or %<{%>");
14425 if (has_underlying_type)
14427 type = NULL_TREE;
14428 goto out;
14433 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14434 return NULL_TREE;
14436 if (nested_name_specifier)
14438 if (CLASS_TYPE_P (nested_name_specifier))
14440 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14441 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14442 push_scope (nested_name_specifier);
14444 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14446 push_nested_namespace (nested_name_specifier);
14450 /* Issue an error message if type-definitions are forbidden here. */
14451 if (!cp_parser_check_type_definition (parser))
14452 type = error_mark_node;
14453 else
14454 /* Create the new type. We do this before consuming the opening
14455 brace so the enum will be recorded as being on the line of its
14456 tag (or the 'enum' keyword, if there is no tag). */
14457 type = start_enum (identifier, type, underlying_type,
14458 scoped_enum_p, &is_new_type);
14460 /* If the next token is not '{' it is an opaque-enum-specifier or an
14461 elaborated-type-specifier. */
14462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14464 timevar_push (TV_PARSE_ENUM);
14465 if (nested_name_specifier)
14467 /* The following catches invalid code such as:
14468 enum class S<int>::E { A, B, C }; */
14469 if (!processing_specialization
14470 && CLASS_TYPE_P (nested_name_specifier)
14471 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14472 error_at (type_start_token->location, "cannot add an enumerator "
14473 "list to a template instantiation");
14475 /* If that scope does not contain the scope in which the
14476 class was originally declared, the program is invalid. */
14477 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14479 if (at_namespace_scope_p ())
14480 error_at (type_start_token->location,
14481 "declaration of %qD in namespace %qD which does not "
14482 "enclose %qD",
14483 type, prev_scope, nested_name_specifier);
14484 else
14485 error_at (type_start_token->location,
14486 "declaration of %qD in %qD which does not enclose %qD",
14487 type, prev_scope, nested_name_specifier);
14488 type = error_mark_node;
14492 if (scoped_enum_p)
14493 begin_scope (sk_scoped_enum, type);
14495 /* Consume the opening brace. */
14496 cp_lexer_consume_token (parser->lexer);
14498 if (type == error_mark_node)
14499 ; /* Nothing to add */
14500 else if (OPAQUE_ENUM_P (type)
14501 || (cxx_dialect > cxx98 && processing_specialization))
14503 new_value_list = true;
14504 SET_OPAQUE_ENUM_P (type, false);
14505 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14507 else
14509 error_at (type_start_token->location, "multiple definition of %q#T", type);
14510 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14511 "previous definition here");
14512 type = error_mark_node;
14515 if (type == error_mark_node)
14516 cp_parser_skip_to_end_of_block_or_statement (parser);
14517 /* If the next token is not '}', then there are some enumerators. */
14518 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14519 cp_parser_enumerator_list (parser, type);
14521 /* Consume the final '}'. */
14522 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14524 if (scoped_enum_p)
14525 finish_scope ();
14526 timevar_pop (TV_PARSE_ENUM);
14528 else
14530 /* If a ';' follows, then it is an opaque-enum-specifier
14531 and additional restrictions apply. */
14532 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14534 if (is_anonymous)
14535 error_at (type_start_token->location,
14536 "opaque-enum-specifier without name");
14537 else if (nested_name_specifier)
14538 error_at (type_start_token->location,
14539 "opaque-enum-specifier must use a simple identifier");
14543 /* Look for trailing attributes to apply to this enumeration, and
14544 apply them if appropriate. */
14545 if (cp_parser_allow_gnu_extensions_p (parser))
14547 tree trailing_attr = cp_parser_attributes_opt (parser);
14548 trailing_attr = chainon (trailing_attr, attributes);
14549 cplus_decl_attributes (&type,
14550 trailing_attr,
14551 (int) ATTR_FLAG_TYPE_IN_PLACE);
14554 /* Finish up the enumeration. */
14555 if (type != error_mark_node)
14557 if (new_value_list)
14558 finish_enum_value_list (type);
14559 if (is_new_type)
14560 finish_enum (type);
14563 if (nested_name_specifier)
14565 if (CLASS_TYPE_P (nested_name_specifier))
14567 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14568 pop_scope (nested_name_specifier);
14570 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14572 pop_nested_namespace (nested_name_specifier);
14575 out:
14576 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14577 return type;
14580 /* Parse an enumerator-list. The enumerators all have the indicated
14581 TYPE.
14583 enumerator-list:
14584 enumerator-definition
14585 enumerator-list , enumerator-definition */
14587 static void
14588 cp_parser_enumerator_list (cp_parser* parser, tree type)
14590 while (true)
14592 /* Parse an enumerator-definition. */
14593 cp_parser_enumerator_definition (parser, type);
14595 /* If the next token is not a ',', we've reached the end of
14596 the list. */
14597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14598 break;
14599 /* Otherwise, consume the `,' and keep going. */
14600 cp_lexer_consume_token (parser->lexer);
14601 /* If the next token is a `}', there is a trailing comma. */
14602 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14604 if (cxx_dialect < cxx0x && !in_system_header)
14605 pedwarn (input_location, OPT_Wpedantic,
14606 "comma at end of enumerator list");
14607 break;
14612 /* Parse an enumerator-definition. The enumerator has the indicated
14613 TYPE.
14615 enumerator-definition:
14616 enumerator
14617 enumerator = constant-expression
14619 enumerator:
14620 identifier */
14622 static void
14623 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14625 tree identifier;
14626 tree value;
14627 location_t loc;
14629 /* Save the input location because we are interested in the location
14630 of the identifier and not the location of the explicit value. */
14631 loc = cp_lexer_peek_token (parser->lexer)->location;
14633 /* Look for the identifier. */
14634 identifier = cp_parser_identifier (parser);
14635 if (identifier == error_mark_node)
14636 return;
14638 /* If the next token is an '=', then there is an explicit value. */
14639 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14641 /* Consume the `=' token. */
14642 cp_lexer_consume_token (parser->lexer);
14643 /* Parse the value. */
14644 value = cp_parser_constant_expression (parser,
14645 /*allow_non_constant_p=*/false,
14646 NULL);
14648 else
14649 value = NULL_TREE;
14651 /* If we are processing a template, make sure the initializer of the
14652 enumerator doesn't contain any bare template parameter pack. */
14653 if (check_for_bare_parameter_packs (value))
14654 value = error_mark_node;
14656 /* integral_constant_value will pull out this expression, so make sure
14657 it's folded as appropriate. */
14658 value = fold_non_dependent_expr (value);
14660 /* Create the enumerator. */
14661 build_enumerator (identifier, value, type, loc);
14664 /* Parse a namespace-name.
14666 namespace-name:
14667 original-namespace-name
14668 namespace-alias
14670 Returns the NAMESPACE_DECL for the namespace. */
14672 static tree
14673 cp_parser_namespace_name (cp_parser* parser)
14675 tree identifier;
14676 tree namespace_decl;
14678 cp_token *token = cp_lexer_peek_token (parser->lexer);
14680 /* Get the name of the namespace. */
14681 identifier = cp_parser_identifier (parser);
14682 if (identifier == error_mark_node)
14683 return error_mark_node;
14685 /* Look up the identifier in the currently active scope. Look only
14686 for namespaces, due to:
14688 [basic.lookup.udir]
14690 When looking up a namespace-name in a using-directive or alias
14691 definition, only namespace names are considered.
14693 And:
14695 [basic.lookup.qual]
14697 During the lookup of a name preceding the :: scope resolution
14698 operator, object, function, and enumerator names are ignored.
14700 (Note that cp_parser_qualifying_entity only calls this
14701 function if the token after the name is the scope resolution
14702 operator.) */
14703 namespace_decl = cp_parser_lookup_name (parser, identifier,
14704 none_type,
14705 /*is_template=*/false,
14706 /*is_namespace=*/true,
14707 /*check_dependency=*/true,
14708 /*ambiguous_decls=*/NULL,
14709 token->location);
14710 /* If it's not a namespace, issue an error. */
14711 if (namespace_decl == error_mark_node
14712 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14714 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14715 error_at (token->location, "%qD is not a namespace-name", identifier);
14716 cp_parser_error (parser, "expected namespace-name");
14717 namespace_decl = error_mark_node;
14720 return namespace_decl;
14723 /* Parse a namespace-definition.
14725 namespace-definition:
14726 named-namespace-definition
14727 unnamed-namespace-definition
14729 named-namespace-definition:
14730 original-namespace-definition
14731 extension-namespace-definition
14733 original-namespace-definition:
14734 namespace identifier { namespace-body }
14736 extension-namespace-definition:
14737 namespace original-namespace-name { namespace-body }
14739 unnamed-namespace-definition:
14740 namespace { namespace-body } */
14742 static void
14743 cp_parser_namespace_definition (cp_parser* parser)
14745 tree identifier, attribs;
14746 bool has_visibility;
14747 bool is_inline;
14749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14751 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14752 is_inline = true;
14753 cp_lexer_consume_token (parser->lexer);
14755 else
14756 is_inline = false;
14758 /* Look for the `namespace' keyword. */
14759 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14761 /* Get the name of the namespace. We do not attempt to distinguish
14762 between an original-namespace-definition and an
14763 extension-namespace-definition at this point. The semantic
14764 analysis routines are responsible for that. */
14765 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14766 identifier = cp_parser_identifier (parser);
14767 else
14768 identifier = NULL_TREE;
14770 /* Parse any specified attributes. */
14771 attribs = cp_parser_attributes_opt (parser);
14773 /* Look for the `{' to start the namespace. */
14774 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14775 /* Start the namespace. */
14776 push_namespace (identifier);
14778 /* "inline namespace" is equivalent to a stub namespace definition
14779 followed by a strong using directive. */
14780 if (is_inline)
14782 tree name_space = current_namespace;
14783 /* Set up namespace association. */
14784 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14785 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14786 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14787 /* Import the contents of the inline namespace. */
14788 pop_namespace ();
14789 do_using_directive (name_space);
14790 push_namespace (identifier);
14793 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14795 /* Parse the body of the namespace. */
14796 cp_parser_namespace_body (parser);
14798 if (has_visibility)
14799 pop_visibility (1);
14801 /* Finish the namespace. */
14802 pop_namespace ();
14803 /* Look for the final `}'. */
14804 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14807 /* Parse a namespace-body.
14809 namespace-body:
14810 declaration-seq [opt] */
14812 static void
14813 cp_parser_namespace_body (cp_parser* parser)
14815 cp_parser_declaration_seq_opt (parser);
14818 /* Parse a namespace-alias-definition.
14820 namespace-alias-definition:
14821 namespace identifier = qualified-namespace-specifier ; */
14823 static void
14824 cp_parser_namespace_alias_definition (cp_parser* parser)
14826 tree identifier;
14827 tree namespace_specifier;
14829 cp_token *token = cp_lexer_peek_token (parser->lexer);
14831 /* Look for the `namespace' keyword. */
14832 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14833 /* Look for the identifier. */
14834 identifier = cp_parser_identifier (parser);
14835 if (identifier == error_mark_node)
14836 return;
14837 /* Look for the `=' token. */
14838 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14839 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14841 error_at (token->location, "%<namespace%> definition is not allowed here");
14842 /* Skip the definition. */
14843 cp_lexer_consume_token (parser->lexer);
14844 if (cp_parser_skip_to_closing_brace (parser))
14845 cp_lexer_consume_token (parser->lexer);
14846 return;
14848 cp_parser_require (parser, CPP_EQ, RT_EQ);
14849 /* Look for the qualified-namespace-specifier. */
14850 namespace_specifier
14851 = cp_parser_qualified_namespace_specifier (parser);
14852 /* Look for the `;' token. */
14853 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14855 /* Register the alias in the symbol table. */
14856 do_namespace_alias (identifier, namespace_specifier);
14859 /* Parse a qualified-namespace-specifier.
14861 qualified-namespace-specifier:
14862 :: [opt] nested-name-specifier [opt] namespace-name
14864 Returns a NAMESPACE_DECL corresponding to the specified
14865 namespace. */
14867 static tree
14868 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14870 /* Look for the optional `::'. */
14871 cp_parser_global_scope_opt (parser,
14872 /*current_scope_valid_p=*/false);
14874 /* Look for the optional nested-name-specifier. */
14875 cp_parser_nested_name_specifier_opt (parser,
14876 /*typename_keyword_p=*/false,
14877 /*check_dependency_p=*/true,
14878 /*type_p=*/false,
14879 /*is_declaration=*/true);
14881 return cp_parser_namespace_name (parser);
14884 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14885 access declaration.
14887 using-declaration:
14888 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14889 using :: unqualified-id ;
14891 access-declaration:
14892 qualified-id ;
14896 static bool
14897 cp_parser_using_declaration (cp_parser* parser,
14898 bool access_declaration_p)
14900 cp_token *token;
14901 bool typename_p = false;
14902 bool global_scope_p;
14903 tree decl;
14904 tree identifier;
14905 tree qscope;
14906 int oldcount = errorcount;
14907 cp_token *diag_token = NULL;
14909 if (access_declaration_p)
14911 diag_token = cp_lexer_peek_token (parser->lexer);
14912 cp_parser_parse_tentatively (parser);
14914 else
14916 /* Look for the `using' keyword. */
14917 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14919 /* Peek at the next token. */
14920 token = cp_lexer_peek_token (parser->lexer);
14921 /* See if it's `typename'. */
14922 if (token->keyword == RID_TYPENAME)
14924 /* Remember that we've seen it. */
14925 typename_p = true;
14926 /* Consume the `typename' token. */
14927 cp_lexer_consume_token (parser->lexer);
14931 /* Look for the optional global scope qualification. */
14932 global_scope_p
14933 = (cp_parser_global_scope_opt (parser,
14934 /*current_scope_valid_p=*/false)
14935 != NULL_TREE);
14937 /* If we saw `typename', or didn't see `::', then there must be a
14938 nested-name-specifier present. */
14939 if (typename_p || !global_scope_p)
14940 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14941 /*check_dependency_p=*/true,
14942 /*type_p=*/false,
14943 /*is_declaration=*/true);
14944 /* Otherwise, we could be in either of the two productions. In that
14945 case, treat the nested-name-specifier as optional. */
14946 else
14947 qscope = cp_parser_nested_name_specifier_opt (parser,
14948 /*typename_keyword_p=*/false,
14949 /*check_dependency_p=*/true,
14950 /*type_p=*/false,
14951 /*is_declaration=*/true);
14952 if (!qscope)
14953 qscope = global_namespace;
14955 if (access_declaration_p && cp_parser_error_occurred (parser))
14956 /* Something has already gone wrong; there's no need to parse
14957 further. Since an error has occurred, the return value of
14958 cp_parser_parse_definitely will be false, as required. */
14959 return cp_parser_parse_definitely (parser);
14961 token = cp_lexer_peek_token (parser->lexer);
14962 /* Parse the unqualified-id. */
14963 identifier = cp_parser_unqualified_id (parser,
14964 /*template_keyword_p=*/false,
14965 /*check_dependency_p=*/true,
14966 /*declarator_p=*/true,
14967 /*optional_p=*/false);
14969 if (access_declaration_p)
14971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14972 cp_parser_simulate_error (parser);
14973 if (!cp_parser_parse_definitely (parser))
14974 return false;
14977 /* The function we call to handle a using-declaration is different
14978 depending on what scope we are in. */
14979 if (qscope == error_mark_node || identifier == error_mark_node)
14981 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14982 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14983 /* [namespace.udecl]
14985 A using declaration shall not name a template-id. */
14986 error_at (token->location,
14987 "a template-id may not appear in a using-declaration");
14988 else
14990 if (at_class_scope_p ())
14992 /* Create the USING_DECL. */
14993 decl = do_class_using_decl (parser->scope, identifier);
14995 if (decl && typename_p)
14996 USING_DECL_TYPENAME_P (decl) = 1;
14998 if (check_for_bare_parameter_packs (decl))
14999 return false;
15000 else
15001 /* Add it to the list of members in this class. */
15002 finish_member_declaration (decl);
15004 else
15006 decl = cp_parser_lookup_name_simple (parser,
15007 identifier,
15008 token->location);
15009 if (decl == error_mark_node)
15010 cp_parser_name_lookup_error (parser, identifier,
15011 decl, NLE_NULL,
15012 token->location);
15013 else if (check_for_bare_parameter_packs (decl))
15014 return false;
15015 else if (!at_namespace_scope_p ())
15016 do_local_using_decl (decl, qscope, identifier);
15017 else
15018 do_toplevel_using_decl (decl, qscope, identifier);
15022 /* Look for the final `;'. */
15023 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15025 if (access_declaration_p && errorcount == oldcount)
15026 warning_at (diag_token->location, OPT_Wdeprecated,
15027 "access declarations are deprecated "
15028 "in favour of using-declarations; "
15029 "suggestion: add the %<using%> keyword");
15031 return true;
15034 /* Parse an alias-declaration.
15036 alias-declaration:
15037 using identifier attribute-specifier-seq [opt] = type-id */
15039 static tree
15040 cp_parser_alias_declaration (cp_parser* parser)
15042 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15043 location_t id_location, using_location, attrs_location = 0;
15044 cp_declarator *declarator;
15045 cp_decl_specifier_seq decl_specs;
15046 bool member_p;
15047 const char *saved_message = NULL;
15049 /* Look for the `using' keyword. */
15050 using_location = cp_lexer_peek_token (parser->lexer)->location;
15051 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15052 id_location = cp_lexer_peek_token (parser->lexer)->location;
15053 id = cp_parser_identifier (parser);
15054 if (id == error_mark_node)
15055 return error_mark_node;
15057 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
15058 attributes = cp_parser_attributes_opt (parser);
15059 if (attributes == error_mark_node)
15060 return error_mark_node;
15062 cp_parser_require (parser, CPP_EQ, RT_EQ);
15064 if (cp_parser_error_occurred (parser))
15065 return error_mark_node;
15067 /* Now we are going to parse the type-id of the declaration. */
15070 [dcl.type]/3 says:
15072 "A type-specifier-seq shall not define a class or enumeration
15073 unless it appears in the type-id of an alias-declaration (7.1.3) that
15074 is not the declaration of a template-declaration."
15076 In other words, if we currently are in an alias template, the
15077 type-id should not define a type.
15079 So let's set parser->type_definition_forbidden_message in that
15080 case; cp_parser_check_type_definition (called by
15081 cp_parser_class_specifier) will then emit an error if a type is
15082 defined in the type-id. */
15083 if (parser->num_template_parameter_lists)
15085 saved_message = parser->type_definition_forbidden_message;
15086 parser->type_definition_forbidden_message =
15087 G_("types may not be defined in alias template declarations");
15090 type = cp_parser_type_id (parser);
15092 /* Restore the error message if need be. */
15093 if (parser->num_template_parameter_lists)
15094 parser->type_definition_forbidden_message = saved_message;
15096 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15098 if (cp_parser_error_occurred (parser))
15099 return error_mark_node;
15101 /* A typedef-name can also be introduced by an alias-declaration. The
15102 identifier following the using keyword becomes a typedef-name. It has
15103 the same semantics as if it were introduced by the typedef
15104 specifier. In particular, it does not define a new type and it shall
15105 not appear in the type-id. */
15107 clear_decl_specs (&decl_specs);
15108 decl_specs.type = type;
15109 if (attributes != NULL_TREE)
15111 decl_specs.attributes = attributes;
15112 set_and_check_decl_spec_loc (&decl_specs,
15113 ds_attribute,
15114 attrs_location);
15116 set_and_check_decl_spec_loc (&decl_specs,
15117 ds_typedef,
15118 using_location);
15119 set_and_check_decl_spec_loc (&decl_specs,
15120 ds_alias,
15121 using_location);
15123 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15124 declarator->id_loc = id_location;
15126 member_p = at_class_scope_p ();
15127 if (member_p)
15128 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15129 NULL_TREE, attributes);
15130 else
15131 decl = start_decl (declarator, &decl_specs, 0,
15132 attributes, NULL_TREE, &pushed_scope);
15133 if (decl == error_mark_node)
15134 return decl;
15136 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15138 if (pushed_scope)
15139 pop_scope (pushed_scope);
15141 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15142 added into the symbol table; otherwise, return the TYPE_DECL. */
15143 if (DECL_LANG_SPECIFIC (decl)
15144 && DECL_TEMPLATE_INFO (decl)
15145 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15147 decl = DECL_TI_TEMPLATE (decl);
15148 if (member_p)
15149 check_member_template (decl);
15152 return decl;
15155 /* Parse a using-directive.
15157 using-directive:
15158 using namespace :: [opt] nested-name-specifier [opt]
15159 namespace-name ; */
15161 static void
15162 cp_parser_using_directive (cp_parser* parser)
15164 tree namespace_decl;
15165 tree attribs;
15167 /* Look for the `using' keyword. */
15168 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15169 /* And the `namespace' keyword. */
15170 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15171 /* Look for the optional `::' operator. */
15172 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15173 /* And the optional nested-name-specifier. */
15174 cp_parser_nested_name_specifier_opt (parser,
15175 /*typename_keyword_p=*/false,
15176 /*check_dependency_p=*/true,
15177 /*type_p=*/false,
15178 /*is_declaration=*/true);
15179 /* Get the namespace being used. */
15180 namespace_decl = cp_parser_namespace_name (parser);
15181 /* And any specified attributes. */
15182 attribs = cp_parser_attributes_opt (parser);
15183 /* Update the symbol table. */
15184 parse_using_directive (namespace_decl, attribs);
15185 /* Look for the final `;'. */
15186 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15189 /* Parse an asm-definition.
15191 asm-definition:
15192 asm ( string-literal ) ;
15194 GNU Extension:
15196 asm-definition:
15197 asm volatile [opt] ( string-literal ) ;
15198 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15199 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15200 : asm-operand-list [opt] ) ;
15201 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15202 : asm-operand-list [opt]
15203 : asm-clobber-list [opt] ) ;
15204 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15205 : asm-clobber-list [opt]
15206 : asm-goto-list ) ; */
15208 static void
15209 cp_parser_asm_definition (cp_parser* parser)
15211 tree string;
15212 tree outputs = NULL_TREE;
15213 tree inputs = NULL_TREE;
15214 tree clobbers = NULL_TREE;
15215 tree labels = NULL_TREE;
15216 tree asm_stmt;
15217 bool volatile_p = false;
15218 bool extended_p = false;
15219 bool invalid_inputs_p = false;
15220 bool invalid_outputs_p = false;
15221 bool goto_p = false;
15222 required_token missing = RT_NONE;
15224 /* Look for the `asm' keyword. */
15225 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15226 /* See if the next token is `volatile'. */
15227 if (cp_parser_allow_gnu_extensions_p (parser)
15228 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15230 /* Remember that we saw the `volatile' keyword. */
15231 volatile_p = true;
15232 /* Consume the token. */
15233 cp_lexer_consume_token (parser->lexer);
15235 if (cp_parser_allow_gnu_extensions_p (parser)
15236 && parser->in_function_body
15237 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15239 /* Remember that we saw the `goto' keyword. */
15240 goto_p = true;
15241 /* Consume the token. */
15242 cp_lexer_consume_token (parser->lexer);
15244 /* Look for the opening `('. */
15245 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15246 return;
15247 /* Look for the string. */
15248 string = cp_parser_string_literal (parser, false, false);
15249 if (string == error_mark_node)
15251 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15252 /*consume_paren=*/true);
15253 return;
15256 /* If we're allowing GNU extensions, check for the extended assembly
15257 syntax. Unfortunately, the `:' tokens need not be separated by
15258 a space in C, and so, for compatibility, we tolerate that here
15259 too. Doing that means that we have to treat the `::' operator as
15260 two `:' tokens. */
15261 if (cp_parser_allow_gnu_extensions_p (parser)
15262 && parser->in_function_body
15263 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15264 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15266 bool inputs_p = false;
15267 bool clobbers_p = false;
15268 bool labels_p = false;
15270 /* The extended syntax was used. */
15271 extended_p = true;
15273 /* Look for outputs. */
15274 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15276 /* Consume the `:'. */
15277 cp_lexer_consume_token (parser->lexer);
15278 /* Parse the output-operands. */
15279 if (cp_lexer_next_token_is_not (parser->lexer,
15280 CPP_COLON)
15281 && cp_lexer_next_token_is_not (parser->lexer,
15282 CPP_SCOPE)
15283 && cp_lexer_next_token_is_not (parser->lexer,
15284 CPP_CLOSE_PAREN)
15285 && !goto_p)
15286 outputs = cp_parser_asm_operand_list (parser);
15288 if (outputs == error_mark_node)
15289 invalid_outputs_p = true;
15291 /* If the next token is `::', there are no outputs, and the
15292 next token is the beginning of the inputs. */
15293 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15294 /* The inputs are coming next. */
15295 inputs_p = true;
15297 /* Look for inputs. */
15298 if (inputs_p
15299 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15301 /* Consume the `:' or `::'. */
15302 cp_lexer_consume_token (parser->lexer);
15303 /* Parse the output-operands. */
15304 if (cp_lexer_next_token_is_not (parser->lexer,
15305 CPP_COLON)
15306 && cp_lexer_next_token_is_not (parser->lexer,
15307 CPP_SCOPE)
15308 && cp_lexer_next_token_is_not (parser->lexer,
15309 CPP_CLOSE_PAREN))
15310 inputs = cp_parser_asm_operand_list (parser);
15312 if (inputs == error_mark_node)
15313 invalid_inputs_p = true;
15315 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15316 /* The clobbers are coming next. */
15317 clobbers_p = true;
15319 /* Look for clobbers. */
15320 if (clobbers_p
15321 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15323 clobbers_p = true;
15324 /* Consume the `:' or `::'. */
15325 cp_lexer_consume_token (parser->lexer);
15326 /* Parse the clobbers. */
15327 if (cp_lexer_next_token_is_not (parser->lexer,
15328 CPP_COLON)
15329 && cp_lexer_next_token_is_not (parser->lexer,
15330 CPP_CLOSE_PAREN))
15331 clobbers = cp_parser_asm_clobber_list (parser);
15333 else if (goto_p
15334 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15335 /* The labels are coming next. */
15336 labels_p = true;
15338 /* Look for labels. */
15339 if (labels_p
15340 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15342 labels_p = true;
15343 /* Consume the `:' or `::'. */
15344 cp_lexer_consume_token (parser->lexer);
15345 /* Parse the labels. */
15346 labels = cp_parser_asm_label_list (parser);
15349 if (goto_p && !labels_p)
15350 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15352 else if (goto_p)
15353 missing = RT_COLON_SCOPE;
15355 /* Look for the closing `)'. */
15356 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15357 missing ? missing : RT_CLOSE_PAREN))
15358 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15359 /*consume_paren=*/true);
15360 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15362 if (!invalid_inputs_p && !invalid_outputs_p)
15364 /* Create the ASM_EXPR. */
15365 if (parser->in_function_body)
15367 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15368 inputs, clobbers, labels);
15369 /* If the extended syntax was not used, mark the ASM_EXPR. */
15370 if (!extended_p)
15372 tree temp = asm_stmt;
15373 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15374 temp = TREE_OPERAND (temp, 0);
15376 ASM_INPUT_P (temp) = 1;
15379 else
15380 add_asm_node (string);
15384 /* Declarators [gram.dcl.decl] */
15386 /* Parse an init-declarator.
15388 init-declarator:
15389 declarator initializer [opt]
15391 GNU Extension:
15393 init-declarator:
15394 declarator asm-specification [opt] attributes [opt] initializer [opt]
15396 function-definition:
15397 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15398 function-body
15399 decl-specifier-seq [opt] declarator function-try-block
15401 GNU Extension:
15403 function-definition:
15404 __extension__ function-definition
15406 TM Extension:
15408 function-definition:
15409 decl-specifier-seq [opt] declarator function-transaction-block
15411 The DECL_SPECIFIERS apply to this declarator. Returns a
15412 representation of the entity declared. If MEMBER_P is TRUE, then
15413 this declarator appears in a class scope. The new DECL created by
15414 this declarator is returned.
15416 The CHECKS are access checks that should be performed once we know
15417 what entity is being declared (and, therefore, what classes have
15418 befriended it).
15420 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15421 for a function-definition here as well. If the declarator is a
15422 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15423 be TRUE upon return. By that point, the function-definition will
15424 have been completely parsed.
15426 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15427 is FALSE.
15429 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15430 parsed declaration if it is an uninitialized single declarator not followed
15431 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15432 if present, will not be consumed. If returned, this declarator will be
15433 created with SD_INITIALIZED but will not call cp_finish_decl. */
15435 static tree
15436 cp_parser_init_declarator (cp_parser* parser,
15437 cp_decl_specifier_seq *decl_specifiers,
15438 VEC (deferred_access_check,gc)* checks,
15439 bool function_definition_allowed_p,
15440 bool member_p,
15441 int declares_class_or_enum,
15442 bool* function_definition_p,
15443 tree* maybe_range_for_decl)
15445 cp_token *token = NULL, *asm_spec_start_token = NULL,
15446 *attributes_start_token = NULL;
15447 cp_declarator *declarator;
15448 tree prefix_attributes;
15449 tree attributes;
15450 tree asm_specification;
15451 tree initializer;
15452 tree decl = NULL_TREE;
15453 tree scope;
15454 int is_initialized;
15455 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15456 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15457 "(...)". */
15458 enum cpp_ttype initialization_kind;
15459 bool is_direct_init = false;
15460 bool is_non_constant_init;
15461 int ctor_dtor_or_conv_p;
15462 bool friend_p;
15463 tree pushed_scope = NULL_TREE;
15464 bool range_for_decl_p = false;
15466 /* Gather the attributes that were provided with the
15467 decl-specifiers. */
15468 prefix_attributes = decl_specifiers->attributes;
15470 /* Assume that this is not the declarator for a function
15471 definition. */
15472 if (function_definition_p)
15473 *function_definition_p = false;
15475 /* Defer access checks while parsing the declarator; we cannot know
15476 what names are accessible until we know what is being
15477 declared. */
15478 resume_deferring_access_checks ();
15480 /* Parse the declarator. */
15481 token = cp_lexer_peek_token (parser->lexer);
15482 declarator
15483 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15484 &ctor_dtor_or_conv_p,
15485 /*parenthesized_p=*/NULL,
15486 member_p);
15487 /* Gather up the deferred checks. */
15488 stop_deferring_access_checks ();
15490 /* If the DECLARATOR was erroneous, there's no need to go
15491 further. */
15492 if (declarator == cp_error_declarator)
15493 return error_mark_node;
15495 /* Check that the number of template-parameter-lists is OK. */
15496 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15497 token->location))
15498 return error_mark_node;
15500 if (declares_class_or_enum & 2)
15501 cp_parser_check_for_definition_in_return_type (declarator,
15502 decl_specifiers->type,
15503 decl_specifiers->locations[ds_type_spec]);
15505 /* Figure out what scope the entity declared by the DECLARATOR is
15506 located in. `grokdeclarator' sometimes changes the scope, so
15507 we compute it now. */
15508 scope = get_scope_of_declarator (declarator);
15510 /* Perform any lookups in the declared type which were thought to be
15511 dependent, but are not in the scope of the declarator. */
15512 decl_specifiers->type
15513 = maybe_update_decl_type (decl_specifiers->type, scope);
15515 /* If we're allowing GNU extensions, look for an asm-specification
15516 and attributes. */
15517 if (cp_parser_allow_gnu_extensions_p (parser))
15519 /* Look for an asm-specification. */
15520 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15521 asm_specification = cp_parser_asm_specification_opt (parser);
15522 /* And attributes. */
15523 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15524 attributes = cp_parser_attributes_opt (parser);
15526 else
15528 asm_specification = NULL_TREE;
15529 attributes = NULL_TREE;
15532 /* Peek at the next token. */
15533 token = cp_lexer_peek_token (parser->lexer);
15534 /* Check to see if the token indicates the start of a
15535 function-definition. */
15536 if (function_declarator_p (declarator)
15537 && cp_parser_token_starts_function_definition_p (token))
15539 if (!function_definition_allowed_p)
15541 /* If a function-definition should not appear here, issue an
15542 error message. */
15543 cp_parser_error (parser,
15544 "a function-definition is not allowed here");
15545 return error_mark_node;
15547 else
15549 location_t func_brace_location
15550 = cp_lexer_peek_token (parser->lexer)->location;
15552 /* Neither attributes nor an asm-specification are allowed
15553 on a function-definition. */
15554 if (asm_specification)
15555 error_at (asm_spec_start_token->location,
15556 "an asm-specification is not allowed "
15557 "on a function-definition");
15558 if (attributes)
15559 error_at (attributes_start_token->location,
15560 "attributes are not allowed on a function-definition");
15561 /* This is a function-definition. */
15562 *function_definition_p = true;
15564 /* Parse the function definition. */
15565 if (member_p)
15566 decl = cp_parser_save_member_function_body (parser,
15567 decl_specifiers,
15568 declarator,
15569 prefix_attributes);
15570 else
15571 decl
15572 = (cp_parser_function_definition_from_specifiers_and_declarator
15573 (parser, decl_specifiers, prefix_attributes, declarator));
15575 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15577 /* This is where the prologue starts... */
15578 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15579 = func_brace_location;
15582 return decl;
15586 /* [dcl.dcl]
15588 Only in function declarations for constructors, destructors, and
15589 type conversions can the decl-specifier-seq be omitted.
15591 We explicitly postpone this check past the point where we handle
15592 function-definitions because we tolerate function-definitions
15593 that are missing their return types in some modes. */
15594 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15596 cp_parser_error (parser,
15597 "expected constructor, destructor, or type conversion");
15598 return error_mark_node;
15601 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15602 if (token->type == CPP_EQ
15603 || token->type == CPP_OPEN_PAREN
15604 || token->type == CPP_OPEN_BRACE)
15606 is_initialized = SD_INITIALIZED;
15607 initialization_kind = token->type;
15608 if (maybe_range_for_decl)
15609 *maybe_range_for_decl = error_mark_node;
15611 if (token->type == CPP_EQ
15612 && function_declarator_p (declarator))
15614 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15615 if (t2->keyword == RID_DEFAULT)
15616 is_initialized = SD_DEFAULTED;
15617 else if (t2->keyword == RID_DELETE)
15618 is_initialized = SD_DELETED;
15621 else
15623 /* If the init-declarator isn't initialized and isn't followed by a
15624 `,' or `;', it's not a valid init-declarator. */
15625 if (token->type != CPP_COMMA
15626 && token->type != CPP_SEMICOLON)
15628 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15629 range_for_decl_p = true;
15630 else
15632 cp_parser_error (parser, "expected initializer");
15633 return error_mark_node;
15636 is_initialized = SD_UNINITIALIZED;
15637 initialization_kind = CPP_EOF;
15640 /* Because start_decl has side-effects, we should only call it if we
15641 know we're going ahead. By this point, we know that we cannot
15642 possibly be looking at any other construct. */
15643 cp_parser_commit_to_tentative_parse (parser);
15645 /* If the decl specifiers were bad, issue an error now that we're
15646 sure this was intended to be a declarator. Then continue
15647 declaring the variable(s), as int, to try to cut down on further
15648 errors. */
15649 if (decl_specifiers->any_specifiers_p
15650 && decl_specifiers->type == error_mark_node)
15652 cp_parser_error (parser, "invalid type in declaration");
15653 decl_specifiers->type = integer_type_node;
15656 /* Check to see whether or not this declaration is a friend. */
15657 friend_p = cp_parser_friend_p (decl_specifiers);
15659 /* Enter the newly declared entry in the symbol table. If we're
15660 processing a declaration in a class-specifier, we wait until
15661 after processing the initializer. */
15662 if (!member_p)
15664 if (parser->in_unbraced_linkage_specification_p)
15665 decl_specifiers->storage_class = sc_extern;
15666 decl = start_decl (declarator, decl_specifiers,
15667 range_for_decl_p? SD_INITIALIZED : is_initialized,
15668 attributes, prefix_attributes,
15669 &pushed_scope);
15670 /* Adjust location of decl if declarator->id_loc is more appropriate:
15671 set, and decl wasn't merged with another decl, in which case its
15672 location would be different from input_location, and more accurate. */
15673 if (DECL_P (decl)
15674 && declarator->id_loc != UNKNOWN_LOCATION
15675 && DECL_SOURCE_LOCATION (decl) == input_location)
15676 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15678 else if (scope)
15679 /* Enter the SCOPE. That way unqualified names appearing in the
15680 initializer will be looked up in SCOPE. */
15681 pushed_scope = push_scope (scope);
15683 /* Perform deferred access control checks, now that we know in which
15684 SCOPE the declared entity resides. */
15685 if (!member_p && decl)
15687 tree saved_current_function_decl = NULL_TREE;
15689 /* If the entity being declared is a function, pretend that we
15690 are in its scope. If it is a `friend', it may have access to
15691 things that would not otherwise be accessible. */
15692 if (TREE_CODE (decl) == FUNCTION_DECL)
15694 saved_current_function_decl = current_function_decl;
15695 current_function_decl = decl;
15698 /* Perform access checks for template parameters. */
15699 cp_parser_perform_template_parameter_access_checks (checks);
15701 /* Perform the access control checks for the declarator and the
15702 decl-specifiers. */
15703 perform_deferred_access_checks ();
15705 /* Restore the saved value. */
15706 if (TREE_CODE (decl) == FUNCTION_DECL)
15707 current_function_decl = saved_current_function_decl;
15710 /* Parse the initializer. */
15711 initializer = NULL_TREE;
15712 is_direct_init = false;
15713 is_non_constant_init = true;
15714 if (is_initialized)
15716 if (function_declarator_p (declarator))
15718 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15719 if (initialization_kind == CPP_EQ)
15720 initializer = cp_parser_pure_specifier (parser);
15721 else
15723 /* If the declaration was erroneous, we don't really
15724 know what the user intended, so just silently
15725 consume the initializer. */
15726 if (decl != error_mark_node)
15727 error_at (initializer_start_token->location,
15728 "initializer provided for function");
15729 cp_parser_skip_to_closing_parenthesis (parser,
15730 /*recovering=*/true,
15731 /*or_comma=*/false,
15732 /*consume_paren=*/true);
15735 else
15737 /* We want to record the extra mangling scope for in-class
15738 initializers of class members and initializers of static data
15739 member templates. The former involves deferring
15740 parsing of the initializer until end of class as with default
15741 arguments. So right here we only handle the latter. */
15742 if (!member_p && processing_template_decl)
15743 start_lambda_scope (decl);
15744 initializer = cp_parser_initializer (parser,
15745 &is_direct_init,
15746 &is_non_constant_init);
15747 if (!member_p && processing_template_decl)
15748 finish_lambda_scope ();
15749 if (initializer == error_mark_node)
15750 cp_parser_skip_to_end_of_statement (parser);
15754 /* The old parser allows attributes to appear after a parenthesized
15755 initializer. Mark Mitchell proposed removing this functionality
15756 on the GCC mailing lists on 2002-08-13. This parser accepts the
15757 attributes -- but ignores them. */
15758 if (cp_parser_allow_gnu_extensions_p (parser)
15759 && initialization_kind == CPP_OPEN_PAREN)
15760 if (cp_parser_attributes_opt (parser))
15761 warning (OPT_Wattributes,
15762 "attributes after parenthesized initializer ignored");
15764 /* For an in-class declaration, use `grokfield' to create the
15765 declaration. */
15766 if (member_p)
15768 if (pushed_scope)
15770 pop_scope (pushed_scope);
15771 pushed_scope = NULL_TREE;
15773 decl = grokfield (declarator, decl_specifiers,
15774 initializer, !is_non_constant_init,
15775 /*asmspec=*/NULL_TREE,
15776 prefix_attributes);
15777 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15778 cp_parser_save_default_args (parser, decl);
15781 /* Finish processing the declaration. But, skip member
15782 declarations. */
15783 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15785 cp_finish_decl (decl,
15786 initializer, !is_non_constant_init,
15787 asm_specification,
15788 /* If the initializer is in parentheses, then this is
15789 a direct-initialization, which means that an
15790 `explicit' constructor is OK. Otherwise, an
15791 `explicit' constructor cannot be used. */
15792 ((is_direct_init || !is_initialized)
15793 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15795 else if ((cxx_dialect != cxx98) && friend_p
15796 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15797 /* Core issue #226 (C++0x only): A default template-argument
15798 shall not be specified in a friend class template
15799 declaration. */
15800 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15801 /*is_partial=*/0, /*is_friend_decl=*/1);
15803 if (!friend_p && pushed_scope)
15804 pop_scope (pushed_scope);
15806 return decl;
15809 /* Parse a declarator.
15811 declarator:
15812 direct-declarator
15813 ptr-operator declarator
15815 abstract-declarator:
15816 ptr-operator abstract-declarator [opt]
15817 direct-abstract-declarator
15819 GNU Extensions:
15821 declarator:
15822 attributes [opt] direct-declarator
15823 attributes [opt] ptr-operator declarator
15825 abstract-declarator:
15826 attributes [opt] ptr-operator abstract-declarator [opt]
15827 attributes [opt] direct-abstract-declarator
15829 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15830 detect constructor, destructor or conversion operators. It is set
15831 to -1 if the declarator is a name, and +1 if it is a
15832 function. Otherwise it is set to zero. Usually you just want to
15833 test for >0, but internally the negative value is used.
15835 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15836 a decl-specifier-seq unless it declares a constructor, destructor,
15837 or conversion. It might seem that we could check this condition in
15838 semantic analysis, rather than parsing, but that makes it difficult
15839 to handle something like `f()'. We want to notice that there are
15840 no decl-specifiers, and therefore realize that this is an
15841 expression, not a declaration.)
15843 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15844 the declarator is a direct-declarator of the form "(...)".
15846 MEMBER_P is true iff this declarator is a member-declarator. */
15848 static cp_declarator *
15849 cp_parser_declarator (cp_parser* parser,
15850 cp_parser_declarator_kind dcl_kind,
15851 int* ctor_dtor_or_conv_p,
15852 bool* parenthesized_p,
15853 bool member_p)
15855 cp_declarator *declarator;
15856 enum tree_code code;
15857 cp_cv_quals cv_quals;
15858 tree class_type;
15859 tree attributes = NULL_TREE;
15861 /* Assume this is not a constructor, destructor, or type-conversion
15862 operator. */
15863 if (ctor_dtor_or_conv_p)
15864 *ctor_dtor_or_conv_p = 0;
15866 if (cp_parser_allow_gnu_extensions_p (parser))
15867 attributes = cp_parser_attributes_opt (parser);
15869 /* Check for the ptr-operator production. */
15870 cp_parser_parse_tentatively (parser);
15871 /* Parse the ptr-operator. */
15872 code = cp_parser_ptr_operator (parser,
15873 &class_type,
15874 &cv_quals);
15875 /* If that worked, then we have a ptr-operator. */
15876 if (cp_parser_parse_definitely (parser))
15878 /* If a ptr-operator was found, then this declarator was not
15879 parenthesized. */
15880 if (parenthesized_p)
15881 *parenthesized_p = true;
15882 /* The dependent declarator is optional if we are parsing an
15883 abstract-declarator. */
15884 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15885 cp_parser_parse_tentatively (parser);
15887 /* Parse the dependent declarator. */
15888 declarator = cp_parser_declarator (parser, dcl_kind,
15889 /*ctor_dtor_or_conv_p=*/NULL,
15890 /*parenthesized_p=*/NULL,
15891 /*member_p=*/false);
15893 /* If we are parsing an abstract-declarator, we must handle the
15894 case where the dependent declarator is absent. */
15895 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15896 && !cp_parser_parse_definitely (parser))
15897 declarator = NULL;
15899 declarator = cp_parser_make_indirect_declarator
15900 (code, class_type, cv_quals, declarator);
15902 /* Everything else is a direct-declarator. */
15903 else
15905 if (parenthesized_p)
15906 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15907 CPP_OPEN_PAREN);
15908 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15909 ctor_dtor_or_conv_p,
15910 member_p);
15913 if (attributes && declarator && declarator != cp_error_declarator)
15914 declarator->attributes = attributes;
15916 return declarator;
15919 /* Parse a direct-declarator or direct-abstract-declarator.
15921 direct-declarator:
15922 declarator-id
15923 direct-declarator ( parameter-declaration-clause )
15924 cv-qualifier-seq [opt]
15925 exception-specification [opt]
15926 direct-declarator [ constant-expression [opt] ]
15927 ( declarator )
15929 direct-abstract-declarator:
15930 direct-abstract-declarator [opt]
15931 ( parameter-declaration-clause )
15932 cv-qualifier-seq [opt]
15933 exception-specification [opt]
15934 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15935 ( abstract-declarator )
15937 Returns a representation of the declarator. DCL_KIND is
15938 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15939 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15940 we are parsing a direct-declarator. It is
15941 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15942 of ambiguity we prefer an abstract declarator, as per
15943 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15944 cp_parser_declarator. */
15946 static cp_declarator *
15947 cp_parser_direct_declarator (cp_parser* parser,
15948 cp_parser_declarator_kind dcl_kind,
15949 int* ctor_dtor_or_conv_p,
15950 bool member_p)
15952 cp_token *token;
15953 cp_declarator *declarator = NULL;
15954 tree scope = NULL_TREE;
15955 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15956 bool saved_in_declarator_p = parser->in_declarator_p;
15957 bool first = true;
15958 tree pushed_scope = NULL_TREE;
15960 while (true)
15962 /* Peek at the next token. */
15963 token = cp_lexer_peek_token (parser->lexer);
15964 if (token->type == CPP_OPEN_PAREN)
15966 /* This is either a parameter-declaration-clause, or a
15967 parenthesized declarator. When we know we are parsing a
15968 named declarator, it must be a parenthesized declarator
15969 if FIRST is true. For instance, `(int)' is a
15970 parameter-declaration-clause, with an omitted
15971 direct-abstract-declarator. But `((*))', is a
15972 parenthesized abstract declarator. Finally, when T is a
15973 template parameter `(T)' is a
15974 parameter-declaration-clause, and not a parenthesized
15975 named declarator.
15977 We first try and parse a parameter-declaration-clause,
15978 and then try a nested declarator (if FIRST is true).
15980 It is not an error for it not to be a
15981 parameter-declaration-clause, even when FIRST is
15982 false. Consider,
15984 int i (int);
15985 int i (3);
15987 The first is the declaration of a function while the
15988 second is the definition of a variable, including its
15989 initializer.
15991 Having seen only the parenthesis, we cannot know which of
15992 these two alternatives should be selected. Even more
15993 complex are examples like:
15995 int i (int (a));
15996 int i (int (3));
15998 The former is a function-declaration; the latter is a
15999 variable initialization.
16001 Thus again, we try a parameter-declaration-clause, and if
16002 that fails, we back out and return. */
16004 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16006 tree params;
16007 unsigned saved_num_template_parameter_lists;
16008 bool is_declarator = false;
16009 tree t;
16011 /* In a member-declarator, the only valid interpretation
16012 of a parenthesis is the start of a
16013 parameter-declaration-clause. (It is invalid to
16014 initialize a static data member with a parenthesized
16015 initializer; only the "=" form of initialization is
16016 permitted.) */
16017 if (!member_p)
16018 cp_parser_parse_tentatively (parser);
16020 /* Consume the `('. */
16021 cp_lexer_consume_token (parser->lexer);
16022 if (first)
16024 /* If this is going to be an abstract declarator, we're
16025 in a declarator and we can't have default args. */
16026 parser->default_arg_ok_p = false;
16027 parser->in_declarator_p = true;
16030 /* Inside the function parameter list, surrounding
16031 template-parameter-lists do not apply. */
16032 saved_num_template_parameter_lists
16033 = parser->num_template_parameter_lists;
16034 parser->num_template_parameter_lists = 0;
16036 begin_scope (sk_function_parms, NULL_TREE);
16038 /* Parse the parameter-declaration-clause. */
16039 params = cp_parser_parameter_declaration_clause (parser);
16041 parser->num_template_parameter_lists
16042 = saved_num_template_parameter_lists;
16044 /* Consume the `)'. */
16045 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16047 /* If all went well, parse the cv-qualifier-seq and the
16048 exception-specification. */
16049 if (member_p || cp_parser_parse_definitely (parser))
16051 cp_cv_quals cv_quals;
16052 cp_virt_specifiers virt_specifiers;
16053 tree exception_specification;
16054 tree late_return;
16056 is_declarator = true;
16058 if (ctor_dtor_or_conv_p)
16059 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16060 first = false;
16062 /* Parse the cv-qualifier-seq. */
16063 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16064 /* And the exception-specification. */
16065 exception_specification
16066 = cp_parser_exception_specification_opt (parser);
16067 /* Parse the virt-specifier-seq. */
16068 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16070 late_return = (cp_parser_late_return_type_opt
16071 (parser, member_p ? cv_quals : -1));
16073 /* Create the function-declarator. */
16074 declarator = make_call_declarator (declarator,
16075 params,
16076 cv_quals,
16077 virt_specifiers,
16078 exception_specification,
16079 late_return);
16080 /* Any subsequent parameter lists are to do with
16081 return type, so are not those of the declared
16082 function. */
16083 parser->default_arg_ok_p = false;
16086 /* Remove the function parms from scope. */
16087 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16088 pop_binding (DECL_NAME (t), t);
16089 leave_scope();
16091 if (is_declarator)
16092 /* Repeat the main loop. */
16093 continue;
16096 /* If this is the first, we can try a parenthesized
16097 declarator. */
16098 if (first)
16100 bool saved_in_type_id_in_expr_p;
16102 parser->default_arg_ok_p = saved_default_arg_ok_p;
16103 parser->in_declarator_p = saved_in_declarator_p;
16105 /* Consume the `('. */
16106 cp_lexer_consume_token (parser->lexer);
16107 /* Parse the nested declarator. */
16108 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16109 parser->in_type_id_in_expr_p = true;
16110 declarator
16111 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16112 /*parenthesized_p=*/NULL,
16113 member_p);
16114 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16115 first = false;
16116 /* Expect a `)'. */
16117 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16118 declarator = cp_error_declarator;
16119 if (declarator == cp_error_declarator)
16120 break;
16122 goto handle_declarator;
16124 /* Otherwise, we must be done. */
16125 else
16126 break;
16128 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16129 && token->type == CPP_OPEN_SQUARE)
16131 /* Parse an array-declarator. */
16132 tree bounds;
16134 if (ctor_dtor_or_conv_p)
16135 *ctor_dtor_or_conv_p = 0;
16137 first = false;
16138 parser->default_arg_ok_p = false;
16139 parser->in_declarator_p = true;
16140 /* Consume the `['. */
16141 cp_lexer_consume_token (parser->lexer);
16142 /* Peek at the next token. */
16143 token = cp_lexer_peek_token (parser->lexer);
16144 /* If the next token is `]', then there is no
16145 constant-expression. */
16146 if (token->type != CPP_CLOSE_SQUARE)
16148 bool non_constant_p;
16150 bounds
16151 = cp_parser_constant_expression (parser,
16152 /*allow_non_constant=*/true,
16153 &non_constant_p);
16154 if (!non_constant_p)
16155 /* OK */;
16156 else if (error_operand_p (bounds))
16157 /* Already gave an error. */;
16158 else if (!parser->in_function_body
16159 || current_binding_level->kind == sk_function_parms)
16161 /* Normally, the array bound must be an integral constant
16162 expression. However, as an extension, we allow VLAs
16163 in function scopes as long as they aren't part of a
16164 parameter declaration. */
16165 cp_parser_error (parser,
16166 "array bound is not an integer constant");
16167 bounds = error_mark_node;
16169 else if (processing_template_decl)
16171 /* Remember this wasn't a constant-expression. */
16172 bounds = build_nop (TREE_TYPE (bounds), bounds);
16173 TREE_SIDE_EFFECTS (bounds) = 1;
16176 else
16177 bounds = NULL_TREE;
16178 /* Look for the closing `]'. */
16179 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16181 declarator = cp_error_declarator;
16182 break;
16185 declarator = make_array_declarator (declarator, bounds);
16187 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16190 tree qualifying_scope;
16191 tree unqualified_name;
16192 special_function_kind sfk;
16193 bool abstract_ok;
16194 bool pack_expansion_p = false;
16195 cp_token *declarator_id_start_token;
16197 /* Parse a declarator-id */
16198 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16199 if (abstract_ok)
16201 cp_parser_parse_tentatively (parser);
16203 /* If we see an ellipsis, we should be looking at a
16204 parameter pack. */
16205 if (token->type == CPP_ELLIPSIS)
16207 /* Consume the `...' */
16208 cp_lexer_consume_token (parser->lexer);
16210 pack_expansion_p = true;
16214 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16215 unqualified_name
16216 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16217 qualifying_scope = parser->scope;
16218 if (abstract_ok)
16220 bool okay = false;
16222 if (!unqualified_name && pack_expansion_p)
16224 /* Check whether an error occurred. */
16225 okay = !cp_parser_error_occurred (parser);
16227 /* We already consumed the ellipsis to mark a
16228 parameter pack, but we have no way to report it,
16229 so abort the tentative parse. We will be exiting
16230 immediately anyway. */
16231 cp_parser_abort_tentative_parse (parser);
16233 else
16234 okay = cp_parser_parse_definitely (parser);
16236 if (!okay)
16237 unqualified_name = error_mark_node;
16238 else if (unqualified_name
16239 && (qualifying_scope
16240 || (TREE_CODE (unqualified_name)
16241 != IDENTIFIER_NODE)))
16243 cp_parser_error (parser, "expected unqualified-id");
16244 unqualified_name = error_mark_node;
16248 if (!unqualified_name)
16249 return NULL;
16250 if (unqualified_name == error_mark_node)
16252 declarator = cp_error_declarator;
16253 pack_expansion_p = false;
16254 declarator->parameter_pack_p = false;
16255 break;
16258 if (qualifying_scope && at_namespace_scope_p ()
16259 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16261 /* In the declaration of a member of a template class
16262 outside of the class itself, the SCOPE will sometimes
16263 be a TYPENAME_TYPE. For example, given:
16265 template <typename T>
16266 int S<T>::R::i = 3;
16268 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16269 this context, we must resolve S<T>::R to an ordinary
16270 type, rather than a typename type.
16272 The reason we normally avoid resolving TYPENAME_TYPEs
16273 is that a specialization of `S' might render
16274 `S<T>::R' not a type. However, if `S' is
16275 specialized, then this `i' will not be used, so there
16276 is no harm in resolving the types here. */
16277 tree type;
16279 /* Resolve the TYPENAME_TYPE. */
16280 type = resolve_typename_type (qualifying_scope,
16281 /*only_current_p=*/false);
16282 /* If that failed, the declarator is invalid. */
16283 if (TREE_CODE (type) == TYPENAME_TYPE)
16285 if (typedef_variant_p (type))
16286 error_at (declarator_id_start_token->location,
16287 "cannot define member of dependent typedef "
16288 "%qT", type);
16289 else
16290 error_at (declarator_id_start_token->location,
16291 "%<%T::%E%> is not a type",
16292 TYPE_CONTEXT (qualifying_scope),
16293 TYPE_IDENTIFIER (qualifying_scope));
16295 qualifying_scope = type;
16298 sfk = sfk_none;
16300 if (unqualified_name)
16302 tree class_type;
16304 if (qualifying_scope
16305 && CLASS_TYPE_P (qualifying_scope))
16306 class_type = qualifying_scope;
16307 else
16308 class_type = current_class_type;
16310 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16312 tree name_type = TREE_TYPE (unqualified_name);
16313 if (class_type && same_type_p (name_type, class_type))
16315 if (qualifying_scope
16316 && CLASSTYPE_USE_TEMPLATE (name_type))
16318 error_at (declarator_id_start_token->location,
16319 "invalid use of constructor as a template");
16320 inform (declarator_id_start_token->location,
16321 "use %<%T::%D%> instead of %<%T::%D%> to "
16322 "name the constructor in a qualified name",
16323 class_type,
16324 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16325 class_type, name_type);
16326 declarator = cp_error_declarator;
16327 break;
16329 else
16330 unqualified_name = constructor_name (class_type);
16332 else
16334 /* We do not attempt to print the declarator
16335 here because we do not have enough
16336 information about its original syntactic
16337 form. */
16338 cp_parser_error (parser, "invalid declarator");
16339 declarator = cp_error_declarator;
16340 break;
16344 if (class_type)
16346 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16347 sfk = sfk_destructor;
16348 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16349 sfk = sfk_conversion;
16350 else if (/* There's no way to declare a constructor
16351 for an anonymous type, even if the type
16352 got a name for linkage purposes. */
16353 !TYPE_WAS_ANONYMOUS (class_type)
16354 && constructor_name_p (unqualified_name,
16355 class_type))
16357 unqualified_name = constructor_name (class_type);
16358 sfk = sfk_constructor;
16360 else if (is_overloaded_fn (unqualified_name)
16361 && DECL_CONSTRUCTOR_P (get_first_fn
16362 (unqualified_name)))
16363 sfk = sfk_constructor;
16365 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16366 *ctor_dtor_or_conv_p = -1;
16369 declarator = make_id_declarator (qualifying_scope,
16370 unqualified_name,
16371 sfk);
16372 declarator->id_loc = token->location;
16373 declarator->parameter_pack_p = pack_expansion_p;
16375 if (pack_expansion_p)
16376 maybe_warn_variadic_templates ();
16379 handle_declarator:;
16380 scope = get_scope_of_declarator (declarator);
16381 if (scope)
16382 /* Any names that appear after the declarator-id for a
16383 member are looked up in the containing scope. */
16384 pushed_scope = push_scope (scope);
16385 parser->in_declarator_p = true;
16386 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16387 || (declarator && declarator->kind == cdk_id))
16388 /* Default args are only allowed on function
16389 declarations. */
16390 parser->default_arg_ok_p = saved_default_arg_ok_p;
16391 else
16392 parser->default_arg_ok_p = false;
16394 first = false;
16396 /* We're done. */
16397 else
16398 break;
16401 /* For an abstract declarator, we might wind up with nothing at this
16402 point. That's an error; the declarator is not optional. */
16403 if (!declarator)
16404 cp_parser_error (parser, "expected declarator");
16406 /* If we entered a scope, we must exit it now. */
16407 if (pushed_scope)
16408 pop_scope (pushed_scope);
16410 parser->default_arg_ok_p = saved_default_arg_ok_p;
16411 parser->in_declarator_p = saved_in_declarator_p;
16413 return declarator;
16416 /* Parse a ptr-operator.
16418 ptr-operator:
16419 * cv-qualifier-seq [opt]
16421 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16423 GNU Extension:
16425 ptr-operator:
16426 & cv-qualifier-seq [opt]
16428 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16429 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16430 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16431 filled in with the TYPE containing the member. *CV_QUALS is
16432 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16433 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16434 Note that the tree codes returned by this function have nothing
16435 to do with the types of trees that will be eventually be created
16436 to represent the pointer or reference type being parsed. They are
16437 just constants with suggestive names. */
16438 static enum tree_code
16439 cp_parser_ptr_operator (cp_parser* parser,
16440 tree* type,
16441 cp_cv_quals *cv_quals)
16443 enum tree_code code = ERROR_MARK;
16444 cp_token *token;
16446 /* Assume that it's not a pointer-to-member. */
16447 *type = NULL_TREE;
16448 /* And that there are no cv-qualifiers. */
16449 *cv_quals = TYPE_UNQUALIFIED;
16451 /* Peek at the next token. */
16452 token = cp_lexer_peek_token (parser->lexer);
16454 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16455 if (token->type == CPP_MULT)
16456 code = INDIRECT_REF;
16457 else if (token->type == CPP_AND)
16458 code = ADDR_EXPR;
16459 else if ((cxx_dialect != cxx98) &&
16460 token->type == CPP_AND_AND) /* C++0x only */
16461 code = NON_LVALUE_EXPR;
16463 if (code != ERROR_MARK)
16465 /* Consume the `*', `&' or `&&'. */
16466 cp_lexer_consume_token (parser->lexer);
16468 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16469 `&', if we are allowing GNU extensions. (The only qualifier
16470 that can legally appear after `&' is `restrict', but that is
16471 enforced during semantic analysis. */
16472 if (code == INDIRECT_REF
16473 || cp_parser_allow_gnu_extensions_p (parser))
16474 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16476 else
16478 /* Try the pointer-to-member case. */
16479 cp_parser_parse_tentatively (parser);
16480 /* Look for the optional `::' operator. */
16481 cp_parser_global_scope_opt (parser,
16482 /*current_scope_valid_p=*/false);
16483 /* Look for the nested-name specifier. */
16484 token = cp_lexer_peek_token (parser->lexer);
16485 cp_parser_nested_name_specifier (parser,
16486 /*typename_keyword_p=*/false,
16487 /*check_dependency_p=*/true,
16488 /*type_p=*/false,
16489 /*is_declaration=*/false);
16490 /* If we found it, and the next token is a `*', then we are
16491 indeed looking at a pointer-to-member operator. */
16492 if (!cp_parser_error_occurred (parser)
16493 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16495 /* Indicate that the `*' operator was used. */
16496 code = INDIRECT_REF;
16498 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16499 error_at (token->location, "%qD is a namespace", parser->scope);
16500 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16501 error_at (token->location, "cannot form pointer to member of "
16502 "non-class %q#T", parser->scope);
16503 else
16505 /* The type of which the member is a member is given by the
16506 current SCOPE. */
16507 *type = parser->scope;
16508 /* The next name will not be qualified. */
16509 parser->scope = NULL_TREE;
16510 parser->qualifying_scope = NULL_TREE;
16511 parser->object_scope = NULL_TREE;
16512 /* Look for the optional cv-qualifier-seq. */
16513 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16516 /* If that didn't work we don't have a ptr-operator. */
16517 if (!cp_parser_parse_definitely (parser))
16518 cp_parser_error (parser, "expected ptr-operator");
16521 return code;
16524 /* Parse an (optional) cv-qualifier-seq.
16526 cv-qualifier-seq:
16527 cv-qualifier cv-qualifier-seq [opt]
16529 cv-qualifier:
16530 const
16531 volatile
16533 GNU Extension:
16535 cv-qualifier:
16536 __restrict__
16538 Returns a bitmask representing the cv-qualifiers. */
16540 static cp_cv_quals
16541 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16543 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16545 while (true)
16547 cp_token *token;
16548 cp_cv_quals cv_qualifier;
16550 /* Peek at the next token. */
16551 token = cp_lexer_peek_token (parser->lexer);
16552 /* See if it's a cv-qualifier. */
16553 switch (token->keyword)
16555 case RID_CONST:
16556 cv_qualifier = TYPE_QUAL_CONST;
16557 break;
16559 case RID_VOLATILE:
16560 cv_qualifier = TYPE_QUAL_VOLATILE;
16561 break;
16563 case RID_RESTRICT:
16564 cv_qualifier = TYPE_QUAL_RESTRICT;
16565 break;
16567 default:
16568 cv_qualifier = TYPE_UNQUALIFIED;
16569 break;
16572 if (!cv_qualifier)
16573 break;
16575 if (cv_quals & cv_qualifier)
16577 error_at (token->location, "duplicate cv-qualifier");
16578 cp_lexer_purge_token (parser->lexer);
16580 else
16582 cp_lexer_consume_token (parser->lexer);
16583 cv_quals |= cv_qualifier;
16587 return cv_quals;
16590 /* Parse an (optional) virt-specifier-seq.
16592 virt-specifier-seq:
16593 virt-specifier virt-specifier-seq [opt]
16595 virt-specifier:
16596 override
16597 final
16599 Returns a bitmask representing the virt-specifiers. */
16601 static cp_virt_specifiers
16602 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16604 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16606 while (true)
16608 cp_token *token;
16609 cp_virt_specifiers virt_specifier;
16611 /* Peek at the next token. */
16612 token = cp_lexer_peek_token (parser->lexer);
16613 /* See if it's a virt-specifier-qualifier. */
16614 if (token->type != CPP_NAME)
16615 break;
16616 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16618 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16619 virt_specifier = VIRT_SPEC_OVERRIDE;
16621 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16623 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16624 virt_specifier = VIRT_SPEC_FINAL;
16626 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16628 virt_specifier = VIRT_SPEC_FINAL;
16630 else
16631 break;
16633 if (virt_specifiers & virt_specifier)
16635 error_at (token->location, "duplicate virt-specifier");
16636 cp_lexer_purge_token (parser->lexer);
16638 else
16640 cp_lexer_consume_token (parser->lexer);
16641 virt_specifiers |= virt_specifier;
16644 return virt_specifiers;
16647 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16648 is in scope even though it isn't real. */
16650 static void
16651 inject_this_parameter (tree ctype, cp_cv_quals quals)
16653 tree this_parm;
16655 if (current_class_ptr)
16657 /* We don't clear this between NSDMIs. Is it already what we want? */
16658 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16659 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16660 && cp_type_quals (type) == quals)
16661 return;
16664 this_parm = build_this_parm (ctype, quals);
16665 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16666 current_class_ptr = NULL_TREE;
16667 current_class_ref
16668 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16669 current_class_ptr = this_parm;
16672 /* Parse a late-specified return type, if any. This is not a separate
16673 non-terminal, but part of a function declarator, which looks like
16675 -> trailing-type-specifier-seq abstract-declarator(opt)
16677 Returns the type indicated by the type-id.
16679 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16680 function. */
16682 static tree
16683 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16685 cp_token *token;
16686 tree type;
16688 /* Peek at the next token. */
16689 token = cp_lexer_peek_token (parser->lexer);
16690 /* A late-specified return type is indicated by an initial '->'. */
16691 if (token->type != CPP_DEREF)
16692 return NULL_TREE;
16694 /* Consume the ->. */
16695 cp_lexer_consume_token (parser->lexer);
16697 if (quals >= 0)
16699 /* DR 1207: 'this' is in scope in the trailing return type. */
16700 gcc_assert (current_class_ptr == NULL_TREE);
16701 inject_this_parameter (current_class_type, quals);
16704 type = cp_parser_trailing_type_id (parser);
16706 if (quals >= 0)
16707 current_class_ptr = current_class_ref = NULL_TREE;
16709 return type;
16712 /* Parse a declarator-id.
16714 declarator-id:
16715 id-expression
16716 :: [opt] nested-name-specifier [opt] type-name
16718 In the `id-expression' case, the value returned is as for
16719 cp_parser_id_expression if the id-expression was an unqualified-id.
16720 If the id-expression was a qualified-id, then a SCOPE_REF is
16721 returned. The first operand is the scope (either a NAMESPACE_DECL
16722 or TREE_TYPE), but the second is still just a representation of an
16723 unqualified-id. */
16725 static tree
16726 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16728 tree id;
16729 /* The expression must be an id-expression. Assume that qualified
16730 names are the names of types so that:
16732 template <class T>
16733 int S<T>::R::i = 3;
16735 will work; we must treat `S<T>::R' as the name of a type.
16736 Similarly, assume that qualified names are templates, where
16737 required, so that:
16739 template <class T>
16740 int S<T>::R<T>::i = 3;
16742 will work, too. */
16743 id = cp_parser_id_expression (parser,
16744 /*template_keyword_p=*/false,
16745 /*check_dependency_p=*/false,
16746 /*template_p=*/NULL,
16747 /*declarator_p=*/true,
16748 optional_p);
16749 if (id && BASELINK_P (id))
16750 id = BASELINK_FUNCTIONS (id);
16751 return id;
16754 /* Parse a type-id.
16756 type-id:
16757 type-specifier-seq abstract-declarator [opt]
16759 Returns the TYPE specified. */
16761 static tree
16762 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16763 bool is_trailing_return)
16765 cp_decl_specifier_seq type_specifier_seq;
16766 cp_declarator *abstract_declarator;
16768 /* Parse the type-specifier-seq. */
16769 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16770 is_trailing_return,
16771 &type_specifier_seq);
16772 if (type_specifier_seq.type == error_mark_node)
16773 return error_mark_node;
16775 /* There might or might not be an abstract declarator. */
16776 cp_parser_parse_tentatively (parser);
16777 /* Look for the declarator. */
16778 abstract_declarator
16779 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16780 /*parenthesized_p=*/NULL,
16781 /*member_p=*/false);
16782 /* Check to see if there really was a declarator. */
16783 if (!cp_parser_parse_definitely (parser))
16784 abstract_declarator = NULL;
16786 if (type_specifier_seq.type
16787 && type_uses_auto (type_specifier_seq.type))
16789 /* A type-id with type 'auto' is only ok if the abstract declarator
16790 is a function declarator with a late-specified return type. */
16791 if (abstract_declarator
16792 && abstract_declarator->kind == cdk_function
16793 && abstract_declarator->u.function.late_return_type)
16794 /* OK */;
16795 else
16797 error ("invalid use of %<auto%>");
16798 return error_mark_node;
16802 return groktypename (&type_specifier_seq, abstract_declarator,
16803 is_template_arg);
16806 static tree cp_parser_type_id (cp_parser *parser)
16808 return cp_parser_type_id_1 (parser, false, false);
16811 static tree cp_parser_template_type_arg (cp_parser *parser)
16813 tree r;
16814 const char *saved_message = parser->type_definition_forbidden_message;
16815 parser->type_definition_forbidden_message
16816 = G_("types may not be defined in template arguments");
16817 r = cp_parser_type_id_1 (parser, true, false);
16818 parser->type_definition_forbidden_message = saved_message;
16819 return r;
16822 static tree cp_parser_trailing_type_id (cp_parser *parser)
16824 return cp_parser_type_id_1 (parser, false, true);
16827 /* Parse a type-specifier-seq.
16829 type-specifier-seq:
16830 type-specifier type-specifier-seq [opt]
16832 GNU extension:
16834 type-specifier-seq:
16835 attributes type-specifier-seq [opt]
16837 If IS_DECLARATION is true, we are at the start of a "condition" or
16838 exception-declaration, so we might be followed by a declarator-id.
16840 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16841 i.e. we've just seen "->".
16843 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16845 static void
16846 cp_parser_type_specifier_seq (cp_parser* parser,
16847 bool is_declaration,
16848 bool is_trailing_return,
16849 cp_decl_specifier_seq *type_specifier_seq)
16851 bool seen_type_specifier = false;
16852 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16853 cp_token *start_token = NULL;
16855 /* Clear the TYPE_SPECIFIER_SEQ. */
16856 clear_decl_specs (type_specifier_seq);
16858 /* In the context of a trailing return type, enum E { } is an
16859 elaborated-type-specifier followed by a function-body, not an
16860 enum-specifier. */
16861 if (is_trailing_return)
16862 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16864 /* Parse the type-specifiers and attributes. */
16865 while (true)
16867 tree type_specifier;
16868 bool is_cv_qualifier;
16870 /* Check for attributes first. */
16871 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16873 type_specifier_seq->attributes =
16874 chainon (type_specifier_seq->attributes,
16875 cp_parser_attributes_opt (parser));
16876 continue;
16879 /* record the token of the beginning of the type specifier seq,
16880 for error reporting purposes*/
16881 if (!start_token)
16882 start_token = cp_lexer_peek_token (parser->lexer);
16884 /* Look for the type-specifier. */
16885 type_specifier = cp_parser_type_specifier (parser,
16886 flags,
16887 type_specifier_seq,
16888 /*is_declaration=*/false,
16889 NULL,
16890 &is_cv_qualifier);
16891 if (!type_specifier)
16893 /* If the first type-specifier could not be found, this is not a
16894 type-specifier-seq at all. */
16895 if (!seen_type_specifier)
16897 cp_parser_error (parser, "expected type-specifier");
16898 type_specifier_seq->type = error_mark_node;
16899 return;
16901 /* If subsequent type-specifiers could not be found, the
16902 type-specifier-seq is complete. */
16903 break;
16906 seen_type_specifier = true;
16907 /* The standard says that a condition can be:
16909 type-specifier-seq declarator = assignment-expression
16911 However, given:
16913 struct S {};
16914 if (int S = ...)
16916 we should treat the "S" as a declarator, not as a
16917 type-specifier. The standard doesn't say that explicitly for
16918 type-specifier-seq, but it does say that for
16919 decl-specifier-seq in an ordinary declaration. Perhaps it
16920 would be clearer just to allow a decl-specifier-seq here, and
16921 then add a semantic restriction that if any decl-specifiers
16922 that are not type-specifiers appear, the program is invalid. */
16923 if (is_declaration && !is_cv_qualifier)
16924 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16928 /* Parse a parameter-declaration-clause.
16930 parameter-declaration-clause:
16931 parameter-declaration-list [opt] ... [opt]
16932 parameter-declaration-list , ...
16934 Returns a representation for the parameter declarations. A return
16935 value of NULL indicates a parameter-declaration-clause consisting
16936 only of an ellipsis. */
16938 static tree
16939 cp_parser_parameter_declaration_clause (cp_parser* parser)
16941 tree parameters;
16942 cp_token *token;
16943 bool ellipsis_p;
16944 bool is_error;
16946 /* Peek at the next token. */
16947 token = cp_lexer_peek_token (parser->lexer);
16948 /* Check for trivial parameter-declaration-clauses. */
16949 if (token->type == CPP_ELLIPSIS)
16951 /* Consume the `...' token. */
16952 cp_lexer_consume_token (parser->lexer);
16953 return NULL_TREE;
16955 else if (token->type == CPP_CLOSE_PAREN)
16956 /* There are no parameters. */
16958 #ifndef NO_IMPLICIT_EXTERN_C
16959 if (in_system_header && current_class_type == NULL
16960 && current_lang_name == lang_name_c)
16961 return NULL_TREE;
16962 else
16963 #endif
16964 return void_list_node;
16966 /* Check for `(void)', too, which is a special case. */
16967 else if (token->keyword == RID_VOID
16968 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16969 == CPP_CLOSE_PAREN))
16971 /* Consume the `void' token. */
16972 cp_lexer_consume_token (parser->lexer);
16973 /* There are no parameters. */
16974 return void_list_node;
16977 /* Parse the parameter-declaration-list. */
16978 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16979 /* If a parse error occurred while parsing the
16980 parameter-declaration-list, then the entire
16981 parameter-declaration-clause is erroneous. */
16982 if (is_error)
16983 return NULL;
16985 /* Peek at the next token. */
16986 token = cp_lexer_peek_token (parser->lexer);
16987 /* If it's a `,', the clause should terminate with an ellipsis. */
16988 if (token->type == CPP_COMMA)
16990 /* Consume the `,'. */
16991 cp_lexer_consume_token (parser->lexer);
16992 /* Expect an ellipsis. */
16993 ellipsis_p
16994 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16996 /* It might also be `...' if the optional trailing `,' was
16997 omitted. */
16998 else if (token->type == CPP_ELLIPSIS)
17000 /* Consume the `...' token. */
17001 cp_lexer_consume_token (parser->lexer);
17002 /* And remember that we saw it. */
17003 ellipsis_p = true;
17005 else
17006 ellipsis_p = false;
17008 /* Finish the parameter list. */
17009 if (!ellipsis_p)
17010 parameters = chainon (parameters, void_list_node);
17012 return parameters;
17015 /* Parse a parameter-declaration-list.
17017 parameter-declaration-list:
17018 parameter-declaration
17019 parameter-declaration-list , parameter-declaration
17021 Returns a representation of the parameter-declaration-list, as for
17022 cp_parser_parameter_declaration_clause. However, the
17023 `void_list_node' is never appended to the list. Upon return,
17024 *IS_ERROR will be true iff an error occurred. */
17026 static tree
17027 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17029 tree parameters = NULL_TREE;
17030 tree *tail = &parameters;
17031 bool saved_in_unbraced_linkage_specification_p;
17032 int index = 0;
17034 /* Assume all will go well. */
17035 *is_error = false;
17036 /* The special considerations that apply to a function within an
17037 unbraced linkage specifications do not apply to the parameters
17038 to the function. */
17039 saved_in_unbraced_linkage_specification_p
17040 = parser->in_unbraced_linkage_specification_p;
17041 parser->in_unbraced_linkage_specification_p = false;
17043 /* Look for more parameters. */
17044 while (true)
17046 cp_parameter_declarator *parameter;
17047 tree decl = error_mark_node;
17048 bool parenthesized_p = false;
17049 /* Parse the parameter. */
17050 parameter
17051 = cp_parser_parameter_declaration (parser,
17052 /*template_parm_p=*/false,
17053 &parenthesized_p);
17055 /* We don't know yet if the enclosing context is deprecated, so wait
17056 and warn in grokparms if appropriate. */
17057 deprecated_state = DEPRECATED_SUPPRESS;
17059 if (parameter)
17060 decl = grokdeclarator (parameter->declarator,
17061 &parameter->decl_specifiers,
17062 PARM,
17063 parameter->default_argument != NULL_TREE,
17064 &parameter->decl_specifiers.attributes);
17066 deprecated_state = DEPRECATED_NORMAL;
17068 /* If a parse error occurred parsing the parameter declaration,
17069 then the entire parameter-declaration-list is erroneous. */
17070 if (decl == error_mark_node)
17072 *is_error = true;
17073 parameters = error_mark_node;
17074 break;
17077 if (parameter->decl_specifiers.attributes)
17078 cplus_decl_attributes (&decl,
17079 parameter->decl_specifiers.attributes,
17081 if (DECL_NAME (decl))
17082 decl = pushdecl (decl);
17084 if (decl != error_mark_node)
17086 retrofit_lang_decl (decl);
17087 DECL_PARM_INDEX (decl) = ++index;
17088 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17091 /* Add the new parameter to the list. */
17092 *tail = build_tree_list (parameter->default_argument, decl);
17093 tail = &TREE_CHAIN (*tail);
17095 /* Peek at the next token. */
17096 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17097 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17098 /* These are for Objective-C++ */
17099 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17100 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17101 /* The parameter-declaration-list is complete. */
17102 break;
17103 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17105 cp_token *token;
17107 /* Peek at the next token. */
17108 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17109 /* If it's an ellipsis, then the list is complete. */
17110 if (token->type == CPP_ELLIPSIS)
17111 break;
17112 /* Otherwise, there must be more parameters. Consume the
17113 `,'. */
17114 cp_lexer_consume_token (parser->lexer);
17115 /* When parsing something like:
17117 int i(float f, double d)
17119 we can tell after seeing the declaration for "f" that we
17120 are not looking at an initialization of a variable "i",
17121 but rather at the declaration of a function "i".
17123 Due to the fact that the parsing of template arguments
17124 (as specified to a template-id) requires backtracking we
17125 cannot use this technique when inside a template argument
17126 list. */
17127 if (!parser->in_template_argument_list_p
17128 && !parser->in_type_id_in_expr_p
17129 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17130 /* However, a parameter-declaration of the form
17131 "foat(f)" (which is a valid declaration of a
17132 parameter "f") can also be interpreted as an
17133 expression (the conversion of "f" to "float"). */
17134 && !parenthesized_p)
17135 cp_parser_commit_to_tentative_parse (parser);
17137 else
17139 cp_parser_error (parser, "expected %<,%> or %<...%>");
17140 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17141 cp_parser_skip_to_closing_parenthesis (parser,
17142 /*recovering=*/true,
17143 /*or_comma=*/false,
17144 /*consume_paren=*/false);
17145 break;
17149 parser->in_unbraced_linkage_specification_p
17150 = saved_in_unbraced_linkage_specification_p;
17152 return parameters;
17155 /* Parse a parameter declaration.
17157 parameter-declaration:
17158 decl-specifier-seq ... [opt] declarator
17159 decl-specifier-seq declarator = assignment-expression
17160 decl-specifier-seq ... [opt] abstract-declarator [opt]
17161 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17163 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17164 declares a template parameter. (In that case, a non-nested `>'
17165 token encountered during the parsing of the assignment-expression
17166 is not interpreted as a greater-than operator.)
17168 Returns a representation of the parameter, or NULL if an error
17169 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17170 true iff the declarator is of the form "(p)". */
17172 static cp_parameter_declarator *
17173 cp_parser_parameter_declaration (cp_parser *parser,
17174 bool template_parm_p,
17175 bool *parenthesized_p)
17177 int declares_class_or_enum;
17178 cp_decl_specifier_seq decl_specifiers;
17179 cp_declarator *declarator;
17180 tree default_argument;
17181 cp_token *token = NULL, *declarator_token_start = NULL;
17182 const char *saved_message;
17184 /* In a template parameter, `>' is not an operator.
17186 [temp.param]
17188 When parsing a default template-argument for a non-type
17189 template-parameter, the first non-nested `>' is taken as the end
17190 of the template parameter-list rather than a greater-than
17191 operator. */
17193 /* Type definitions may not appear in parameter types. */
17194 saved_message = parser->type_definition_forbidden_message;
17195 parser->type_definition_forbidden_message
17196 = G_("types may not be defined in parameter types");
17198 /* Parse the declaration-specifiers. */
17199 cp_parser_decl_specifier_seq (parser,
17200 CP_PARSER_FLAGS_NONE,
17201 &decl_specifiers,
17202 &declares_class_or_enum);
17204 /* Complain about missing 'typename' or other invalid type names. */
17205 if (!decl_specifiers.any_type_specifiers_p)
17206 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17208 /* If an error occurred, there's no reason to attempt to parse the
17209 rest of the declaration. */
17210 if (cp_parser_error_occurred (parser))
17212 parser->type_definition_forbidden_message = saved_message;
17213 return NULL;
17216 /* Peek at the next token. */
17217 token = cp_lexer_peek_token (parser->lexer);
17219 /* If the next token is a `)', `,', `=', `>', or `...', then there
17220 is no declarator. However, when variadic templates are enabled,
17221 there may be a declarator following `...'. */
17222 if (token->type == CPP_CLOSE_PAREN
17223 || token->type == CPP_COMMA
17224 || token->type == CPP_EQ
17225 || token->type == CPP_GREATER)
17227 declarator = NULL;
17228 if (parenthesized_p)
17229 *parenthesized_p = false;
17231 /* Otherwise, there should be a declarator. */
17232 else
17234 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17235 parser->default_arg_ok_p = false;
17237 /* After seeing a decl-specifier-seq, if the next token is not a
17238 "(", there is no possibility that the code is a valid
17239 expression. Therefore, if parsing tentatively, we commit at
17240 this point. */
17241 if (!parser->in_template_argument_list_p
17242 /* In an expression context, having seen:
17244 (int((char ...
17246 we cannot be sure whether we are looking at a
17247 function-type (taking a "char" as a parameter) or a cast
17248 of some object of type "char" to "int". */
17249 && !parser->in_type_id_in_expr_p
17250 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17251 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17252 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17253 cp_parser_commit_to_tentative_parse (parser);
17254 /* Parse the declarator. */
17255 declarator_token_start = token;
17256 declarator = cp_parser_declarator (parser,
17257 CP_PARSER_DECLARATOR_EITHER,
17258 /*ctor_dtor_or_conv_p=*/NULL,
17259 parenthesized_p,
17260 /*member_p=*/false);
17261 parser->default_arg_ok_p = saved_default_arg_ok_p;
17262 /* After the declarator, allow more attributes. */
17263 decl_specifiers.attributes
17264 = chainon (decl_specifiers.attributes,
17265 cp_parser_attributes_opt (parser));
17268 /* If the next token is an ellipsis, and we have not seen a
17269 declarator name, and the type of the declarator contains parameter
17270 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17271 a parameter pack expansion expression. Otherwise, leave the
17272 ellipsis for a C-style variadic function. */
17273 token = cp_lexer_peek_token (parser->lexer);
17274 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17276 tree type = decl_specifiers.type;
17278 if (type && DECL_P (type))
17279 type = TREE_TYPE (type);
17281 if (type
17282 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17283 && declarator_can_be_parameter_pack (declarator)
17284 && (!declarator || !declarator->parameter_pack_p)
17285 && uses_parameter_packs (type))
17287 /* Consume the `...'. */
17288 cp_lexer_consume_token (parser->lexer);
17289 maybe_warn_variadic_templates ();
17291 /* Build a pack expansion type */
17292 if (declarator)
17293 declarator->parameter_pack_p = true;
17294 else
17295 decl_specifiers.type = make_pack_expansion (type);
17299 /* The restriction on defining new types applies only to the type
17300 of the parameter, not to the default argument. */
17301 parser->type_definition_forbidden_message = saved_message;
17303 /* If the next token is `=', then process a default argument. */
17304 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17306 token = cp_lexer_peek_token (parser->lexer);
17307 /* If we are defining a class, then the tokens that make up the
17308 default argument must be saved and processed later. */
17309 if (!template_parm_p && at_class_scope_p ()
17310 && TYPE_BEING_DEFINED (current_class_type)
17311 && !LAMBDA_TYPE_P (current_class_type))
17312 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17313 /* Outside of a class definition, we can just parse the
17314 assignment-expression. */
17315 else
17316 default_argument
17317 = cp_parser_default_argument (parser, template_parm_p);
17319 if (!parser->default_arg_ok_p)
17321 if (flag_permissive)
17322 warning (0, "deprecated use of default argument for parameter of non-function");
17323 else
17325 error_at (token->location,
17326 "default arguments are only "
17327 "permitted for function parameters");
17328 default_argument = NULL_TREE;
17331 else if ((declarator && declarator->parameter_pack_p)
17332 || (decl_specifiers.type
17333 && PACK_EXPANSION_P (decl_specifiers.type)))
17335 /* Find the name of the parameter pack. */
17336 cp_declarator *id_declarator = declarator;
17337 while (id_declarator && id_declarator->kind != cdk_id)
17338 id_declarator = id_declarator->declarator;
17340 if (id_declarator && id_declarator->kind == cdk_id)
17341 error_at (declarator_token_start->location,
17342 template_parm_p
17343 ? G_("template parameter pack %qD "
17344 "cannot have a default argument")
17345 : G_("parameter pack %qD cannot have "
17346 "a default argument"),
17347 id_declarator->u.id.unqualified_name);
17348 else
17349 error_at (declarator_token_start->location,
17350 template_parm_p
17351 ? G_("template parameter pack cannot have "
17352 "a default argument")
17353 : G_("parameter pack cannot have a "
17354 "default argument"));
17356 default_argument = NULL_TREE;
17359 else
17360 default_argument = NULL_TREE;
17362 return make_parameter_declarator (&decl_specifiers,
17363 declarator,
17364 default_argument);
17367 /* Parse a default argument and return it.
17369 TEMPLATE_PARM_P is true if this is a default argument for a
17370 non-type template parameter. */
17371 static tree
17372 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17374 tree default_argument = NULL_TREE;
17375 bool saved_greater_than_is_operator_p;
17376 bool saved_local_variables_forbidden_p;
17377 bool non_constant_p, is_direct_init;
17379 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17380 set correctly. */
17381 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17382 parser->greater_than_is_operator_p = !template_parm_p;
17383 /* Local variable names (and the `this' keyword) may not
17384 appear in a default argument. */
17385 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17386 parser->local_variables_forbidden_p = true;
17387 /* Parse the assignment-expression. */
17388 if (template_parm_p)
17389 push_deferring_access_checks (dk_no_deferred);
17390 default_argument
17391 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17392 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17393 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17394 if (template_parm_p)
17395 pop_deferring_access_checks ();
17396 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17397 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17399 return default_argument;
17402 /* Parse a function-body.
17404 function-body:
17405 compound_statement */
17407 static void
17408 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17410 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17413 /* Parse a ctor-initializer-opt followed by a function-body. Return
17414 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17415 is true we are parsing a function-try-block. */
17417 static bool
17418 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17419 bool in_function_try_block)
17421 tree body, list;
17422 bool ctor_initializer_p;
17423 const bool check_body_p =
17424 DECL_CONSTRUCTOR_P (current_function_decl)
17425 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17426 tree last = NULL;
17428 /* Begin the function body. */
17429 body = begin_function_body ();
17430 /* Parse the optional ctor-initializer. */
17431 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17433 /* If we're parsing a constexpr constructor definition, we need
17434 to check that the constructor body is indeed empty. However,
17435 before we get to cp_parser_function_body lot of junk has been
17436 generated, so we can't just check that we have an empty block.
17437 Rather we take a snapshot of the outermost block, and check whether
17438 cp_parser_function_body changed its state. */
17439 if (check_body_p)
17441 list = cur_stmt_list;
17442 if (STATEMENT_LIST_TAIL (list))
17443 last = STATEMENT_LIST_TAIL (list)->stmt;
17445 /* Parse the function-body. */
17446 cp_parser_function_body (parser, in_function_try_block);
17447 if (check_body_p)
17448 check_constexpr_ctor_body (last, list);
17449 /* Finish the function body. */
17450 finish_function_body (body);
17452 return ctor_initializer_p;
17455 /* Parse an initializer.
17457 initializer:
17458 = initializer-clause
17459 ( expression-list )
17461 Returns an expression representing the initializer. If no
17462 initializer is present, NULL_TREE is returned.
17464 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17465 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17466 set to TRUE if there is no initializer present. If there is an
17467 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17468 is set to true; otherwise it is set to false. */
17470 static tree
17471 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17472 bool* non_constant_p)
17474 cp_token *token;
17475 tree init;
17477 /* Peek at the next token. */
17478 token = cp_lexer_peek_token (parser->lexer);
17480 /* Let our caller know whether or not this initializer was
17481 parenthesized. */
17482 *is_direct_init = (token->type != CPP_EQ);
17483 /* Assume that the initializer is constant. */
17484 *non_constant_p = false;
17486 if (token->type == CPP_EQ)
17488 /* Consume the `='. */
17489 cp_lexer_consume_token (parser->lexer);
17490 /* Parse the initializer-clause. */
17491 init = cp_parser_initializer_clause (parser, non_constant_p);
17493 else if (token->type == CPP_OPEN_PAREN)
17495 VEC(tree,gc) *vec;
17496 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17497 /*cast_p=*/false,
17498 /*allow_expansion_p=*/true,
17499 non_constant_p);
17500 if (vec == NULL)
17501 return error_mark_node;
17502 init = build_tree_list_vec (vec);
17503 release_tree_vector (vec);
17505 else if (token->type == CPP_OPEN_BRACE)
17507 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17508 init = cp_parser_braced_list (parser, non_constant_p);
17509 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17511 else
17513 /* Anything else is an error. */
17514 cp_parser_error (parser, "expected initializer");
17515 init = error_mark_node;
17518 return init;
17521 /* Parse an initializer-clause.
17523 initializer-clause:
17524 assignment-expression
17525 braced-init-list
17527 Returns an expression representing the initializer.
17529 If the `assignment-expression' production is used the value
17530 returned is simply a representation for the expression.
17532 Otherwise, calls cp_parser_braced_list. */
17534 static tree
17535 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17537 tree initializer;
17539 /* Assume the expression is constant. */
17540 *non_constant_p = false;
17542 /* If it is not a `{', then we are looking at an
17543 assignment-expression. */
17544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17546 initializer
17547 = cp_parser_constant_expression (parser,
17548 /*allow_non_constant_p=*/true,
17549 non_constant_p);
17551 else
17552 initializer = cp_parser_braced_list (parser, non_constant_p);
17554 return initializer;
17557 /* Parse a brace-enclosed initializer list.
17559 braced-init-list:
17560 { initializer-list , [opt] }
17563 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17564 the elements of the initializer-list (or NULL, if the last
17565 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17566 NULL_TREE. There is no way to detect whether or not the optional
17567 trailing `,' was provided. NON_CONSTANT_P is as for
17568 cp_parser_initializer. */
17570 static tree
17571 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17573 tree initializer;
17575 /* Consume the `{' token. */
17576 cp_lexer_consume_token (parser->lexer);
17577 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17578 initializer = make_node (CONSTRUCTOR);
17579 /* If it's not a `}', then there is a non-trivial initializer. */
17580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17582 /* Parse the initializer list. */
17583 CONSTRUCTOR_ELTS (initializer)
17584 = cp_parser_initializer_list (parser, non_constant_p);
17585 /* A trailing `,' token is allowed. */
17586 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17587 cp_lexer_consume_token (parser->lexer);
17589 /* Now, there should be a trailing `}'. */
17590 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17591 TREE_TYPE (initializer) = init_list_type_node;
17592 return initializer;
17595 /* Parse an initializer-list.
17597 initializer-list:
17598 initializer-clause ... [opt]
17599 initializer-list , initializer-clause ... [opt]
17601 GNU Extension:
17603 initializer-list:
17604 designation initializer-clause ...[opt]
17605 initializer-list , designation initializer-clause ...[opt]
17607 designation:
17608 . identifier =
17609 identifier :
17610 [ constant-expression ] =
17612 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17613 for the initializer. If the INDEX of the elt is non-NULL, it is the
17614 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17615 as for cp_parser_initializer. */
17617 static VEC(constructor_elt,gc) *
17618 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17620 VEC(constructor_elt,gc) *v = NULL;
17622 /* Assume all of the expressions are constant. */
17623 *non_constant_p = false;
17625 /* Parse the rest of the list. */
17626 while (true)
17628 cp_token *token;
17629 tree designator;
17630 tree initializer;
17631 bool clause_non_constant_p;
17633 /* If the next token is an identifier and the following one is a
17634 colon, we are looking at the GNU designated-initializer
17635 syntax. */
17636 if (cp_parser_allow_gnu_extensions_p (parser)
17637 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17638 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17640 /* Warn the user that they are using an extension. */
17641 pedwarn (input_location, OPT_Wpedantic,
17642 "ISO C++ does not allow designated initializers");
17643 /* Consume the identifier. */
17644 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17645 /* Consume the `:'. */
17646 cp_lexer_consume_token (parser->lexer);
17648 /* Also handle the C99 syntax, '. id ='. */
17649 else if (cp_parser_allow_gnu_extensions_p (parser)
17650 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17651 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17652 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17654 /* Warn the user that they are using an extension. */
17655 pedwarn (input_location, OPT_Wpedantic,
17656 "ISO C++ does not allow C99 designated initializers");
17657 /* Consume the `.'. */
17658 cp_lexer_consume_token (parser->lexer);
17659 /* Consume the identifier. */
17660 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17661 /* Consume the `='. */
17662 cp_lexer_consume_token (parser->lexer);
17664 /* Also handle C99 array designators, '[ const ] ='. */
17665 else if (cp_parser_allow_gnu_extensions_p (parser)
17666 && !c_dialect_objc ()
17667 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17669 /* In C++11, [ could start a lambda-introducer. */
17670 cp_parser_parse_tentatively (parser);
17671 cp_lexer_consume_token (parser->lexer);
17672 designator = cp_parser_constant_expression (parser, false, NULL);
17673 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17674 cp_parser_require (parser, CPP_EQ, RT_EQ);
17675 if (!cp_parser_parse_definitely (parser))
17676 designator = NULL_TREE;
17678 else
17679 designator = NULL_TREE;
17681 /* Parse the initializer. */
17682 initializer = cp_parser_initializer_clause (parser,
17683 &clause_non_constant_p);
17684 /* If any clause is non-constant, so is the entire initializer. */
17685 if (clause_non_constant_p)
17686 *non_constant_p = true;
17688 /* If we have an ellipsis, this is an initializer pack
17689 expansion. */
17690 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17692 /* Consume the `...'. */
17693 cp_lexer_consume_token (parser->lexer);
17695 /* Turn the initializer into an initializer expansion. */
17696 initializer = make_pack_expansion (initializer);
17699 /* Add it to the vector. */
17700 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17702 /* If the next token is not a comma, we have reached the end of
17703 the list. */
17704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17705 break;
17707 /* Peek at the next token. */
17708 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17709 /* If the next token is a `}', then we're still done. An
17710 initializer-clause can have a trailing `,' after the
17711 initializer-list and before the closing `}'. */
17712 if (token->type == CPP_CLOSE_BRACE)
17713 break;
17715 /* Consume the `,' token. */
17716 cp_lexer_consume_token (parser->lexer);
17719 return v;
17722 /* Classes [gram.class] */
17724 /* Parse a class-name.
17726 class-name:
17727 identifier
17728 template-id
17730 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17731 to indicate that names looked up in dependent types should be
17732 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17733 keyword has been used to indicate that the name that appears next
17734 is a template. TAG_TYPE indicates the explicit tag given before
17735 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17736 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17737 is the class being defined in a class-head.
17739 Returns the TYPE_DECL representing the class. */
17741 static tree
17742 cp_parser_class_name (cp_parser *parser,
17743 bool typename_keyword_p,
17744 bool template_keyword_p,
17745 enum tag_types tag_type,
17746 bool check_dependency_p,
17747 bool class_head_p,
17748 bool is_declaration)
17750 tree decl;
17751 tree scope;
17752 bool typename_p;
17753 cp_token *token;
17754 tree identifier = NULL_TREE;
17756 /* All class-names start with an identifier. */
17757 token = cp_lexer_peek_token (parser->lexer);
17758 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17760 cp_parser_error (parser, "expected class-name");
17761 return error_mark_node;
17764 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17765 to a template-id, so we save it here. */
17766 scope = parser->scope;
17767 if (scope == error_mark_node)
17768 return error_mark_node;
17770 /* Any name names a type if we're following the `typename' keyword
17771 in a qualified name where the enclosing scope is type-dependent. */
17772 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17773 && dependent_type_p (scope));
17774 /* Handle the common case (an identifier, but not a template-id)
17775 efficiently. */
17776 if (token->type == CPP_NAME
17777 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17779 cp_token *identifier_token;
17780 bool ambiguous_p;
17782 /* Look for the identifier. */
17783 identifier_token = cp_lexer_peek_token (parser->lexer);
17784 ambiguous_p = identifier_token->ambiguous_p;
17785 identifier = cp_parser_identifier (parser);
17786 /* If the next token isn't an identifier, we are certainly not
17787 looking at a class-name. */
17788 if (identifier == error_mark_node)
17789 decl = error_mark_node;
17790 /* If we know this is a type-name, there's no need to look it
17791 up. */
17792 else if (typename_p)
17793 decl = identifier;
17794 else
17796 tree ambiguous_decls;
17797 /* If we already know that this lookup is ambiguous, then
17798 we've already issued an error message; there's no reason
17799 to check again. */
17800 if (ambiguous_p)
17802 cp_parser_simulate_error (parser);
17803 return error_mark_node;
17805 /* If the next token is a `::', then the name must be a type
17806 name.
17808 [basic.lookup.qual]
17810 During the lookup for a name preceding the :: scope
17811 resolution operator, object, function, and enumerator
17812 names are ignored. */
17813 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17814 tag_type = typename_type;
17815 /* Look up the name. */
17816 decl = cp_parser_lookup_name (parser, identifier,
17817 tag_type,
17818 /*is_template=*/false,
17819 /*is_namespace=*/false,
17820 check_dependency_p,
17821 &ambiguous_decls,
17822 identifier_token->location);
17823 if (ambiguous_decls)
17825 if (cp_parser_parsing_tentatively (parser))
17826 cp_parser_simulate_error (parser);
17827 return error_mark_node;
17831 else
17833 /* Try a template-id. */
17834 decl = cp_parser_template_id (parser, template_keyword_p,
17835 check_dependency_p,
17836 is_declaration);
17837 if (decl == error_mark_node)
17838 return error_mark_node;
17841 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17843 /* If this is a typename, create a TYPENAME_TYPE. */
17844 if (typename_p && decl != error_mark_node)
17846 decl = make_typename_type (scope, decl, typename_type,
17847 /*complain=*/tf_error);
17848 if (decl != error_mark_node)
17849 decl = TYPE_NAME (decl);
17852 decl = strip_using_decl (decl);
17854 /* Check to see that it is really the name of a class. */
17855 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17856 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17857 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17858 /* Situations like this:
17860 template <typename T> struct A {
17861 typename T::template X<int>::I i;
17864 are problematic. Is `T::template X<int>' a class-name? The
17865 standard does not seem to be definitive, but there is no other
17866 valid interpretation of the following `::'. Therefore, those
17867 names are considered class-names. */
17869 decl = make_typename_type (scope, decl, tag_type, tf_error);
17870 if (decl != error_mark_node)
17871 decl = TYPE_NAME (decl);
17873 else if (TREE_CODE (decl) != TYPE_DECL
17874 || TREE_TYPE (decl) == error_mark_node
17875 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17876 /* In Objective-C 2.0, a classname followed by '.' starts a
17877 dot-syntax expression, and it's not a type-name. */
17878 || (c_dialect_objc ()
17879 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17880 && objc_is_class_name (decl)))
17881 decl = error_mark_node;
17883 if (decl == error_mark_node)
17884 cp_parser_error (parser, "expected class-name");
17885 else if (identifier && !parser->scope)
17886 maybe_note_name_used_in_class (identifier, decl);
17888 return decl;
17891 /* Parse a class-specifier.
17893 class-specifier:
17894 class-head { member-specification [opt] }
17896 Returns the TREE_TYPE representing the class. */
17898 static tree
17899 cp_parser_class_specifier_1 (cp_parser* parser)
17901 tree type;
17902 tree attributes = NULL_TREE;
17903 bool nested_name_specifier_p;
17904 unsigned saved_num_template_parameter_lists;
17905 bool saved_in_function_body;
17906 unsigned char in_statement;
17907 bool in_switch_statement_p;
17908 bool saved_in_unbraced_linkage_specification_p;
17909 tree old_scope = NULL_TREE;
17910 tree scope = NULL_TREE;
17911 tree bases;
17912 cp_token *closing_brace;
17914 push_deferring_access_checks (dk_no_deferred);
17916 /* Parse the class-head. */
17917 type = cp_parser_class_head (parser,
17918 &nested_name_specifier_p,
17919 &bases);
17920 /* If the class-head was a semantic disaster, skip the entire body
17921 of the class. */
17922 if (!type)
17924 cp_parser_skip_to_end_of_block_or_statement (parser);
17925 pop_deferring_access_checks ();
17926 return error_mark_node;
17929 /* Look for the `{'. */
17930 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17932 pop_deferring_access_checks ();
17933 return error_mark_node;
17936 /* Process the base classes. If they're invalid, skip the
17937 entire class body. */
17938 if (!xref_basetypes (type, bases))
17940 /* Consuming the closing brace yields better error messages
17941 later on. */
17942 if (cp_parser_skip_to_closing_brace (parser))
17943 cp_lexer_consume_token (parser->lexer);
17944 pop_deferring_access_checks ();
17945 return error_mark_node;
17948 /* Issue an error message if type-definitions are forbidden here. */
17949 cp_parser_check_type_definition (parser);
17950 /* Remember that we are defining one more class. */
17951 ++parser->num_classes_being_defined;
17952 /* Inside the class, surrounding template-parameter-lists do not
17953 apply. */
17954 saved_num_template_parameter_lists
17955 = parser->num_template_parameter_lists;
17956 parser->num_template_parameter_lists = 0;
17957 /* We are not in a function body. */
17958 saved_in_function_body = parser->in_function_body;
17959 parser->in_function_body = false;
17960 /* Or in a loop. */
17961 in_statement = parser->in_statement;
17962 parser->in_statement = 0;
17963 /* Or in a switch. */
17964 in_switch_statement_p = parser->in_switch_statement_p;
17965 parser->in_switch_statement_p = false;
17966 /* We are not immediately inside an extern "lang" block. */
17967 saved_in_unbraced_linkage_specification_p
17968 = parser->in_unbraced_linkage_specification_p;
17969 parser->in_unbraced_linkage_specification_p = false;
17971 /* Start the class. */
17972 if (nested_name_specifier_p)
17974 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17975 old_scope = push_inner_scope (scope);
17977 type = begin_class_definition (type);
17979 if (type == error_mark_node)
17980 /* If the type is erroneous, skip the entire body of the class. */
17981 cp_parser_skip_to_closing_brace (parser);
17982 else
17983 /* Parse the member-specification. */
17984 cp_parser_member_specification_opt (parser);
17986 /* Look for the trailing `}'. */
17987 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17988 /* Look for trailing attributes to apply to this class. */
17989 if (cp_parser_allow_gnu_extensions_p (parser))
17990 attributes = cp_parser_attributes_opt (parser);
17991 if (type != error_mark_node)
17992 type = finish_struct (type, attributes);
17993 if (nested_name_specifier_p)
17994 pop_inner_scope (old_scope, scope);
17996 /* We've finished a type definition. Check for the common syntax
17997 error of forgetting a semicolon after the definition. We need to
17998 be careful, as we can't just check for not-a-semicolon and be done
17999 with it; the user might have typed:
18001 class X { } c = ...;
18002 class X { } *p = ...;
18004 and so forth. Instead, enumerate all the possible tokens that
18005 might follow this production; if we don't see one of them, then
18006 complain and silently insert the semicolon. */
18008 cp_token *token = cp_lexer_peek_token (parser->lexer);
18009 bool want_semicolon = true;
18011 switch (token->type)
18013 case CPP_NAME:
18014 case CPP_SEMICOLON:
18015 case CPP_MULT:
18016 case CPP_AND:
18017 case CPP_OPEN_PAREN:
18018 case CPP_CLOSE_PAREN:
18019 case CPP_COMMA:
18020 want_semicolon = false;
18021 break;
18023 /* While it's legal for type qualifiers and storage class
18024 specifiers to follow type definitions in the grammar, only
18025 compiler testsuites contain code like that. Assume that if
18026 we see such code, then what we're really seeing is a case
18027 like:
18029 class X { }
18030 const <type> var = ...;
18034 class Y { }
18035 static <type> func (...) ...
18037 i.e. the qualifier or specifier applies to the next
18038 declaration. To do so, however, we need to look ahead one
18039 more token to see if *that* token is a type specifier.
18041 This code could be improved to handle:
18043 class Z { }
18044 static const <type> var = ...; */
18045 case CPP_KEYWORD:
18046 if (keyword_is_decl_specifier (token->keyword))
18048 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18050 /* Handling user-defined types here would be nice, but very
18051 tricky. */
18052 want_semicolon
18053 = (lookahead->type == CPP_KEYWORD
18054 && keyword_begins_type_specifier (lookahead->keyword));
18056 break;
18057 default:
18058 break;
18061 /* If we don't have a type, then something is very wrong and we
18062 shouldn't try to do anything clever. Likewise for not seeing the
18063 closing brace. */
18064 if (closing_brace && TYPE_P (type) && want_semicolon)
18066 cp_token_position prev
18067 = cp_lexer_previous_token_position (parser->lexer);
18068 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18069 location_t loc = prev_token->location;
18071 if (CLASSTYPE_DECLARED_CLASS (type))
18072 error_at (loc, "expected %<;%> after class definition");
18073 else if (TREE_CODE (type) == RECORD_TYPE)
18074 error_at (loc, "expected %<;%> after struct definition");
18075 else if (TREE_CODE (type) == UNION_TYPE)
18076 error_at (loc, "expected %<;%> after union definition");
18077 else
18078 gcc_unreachable ();
18080 /* Unget one token and smash it to look as though we encountered
18081 a semicolon in the input stream. */
18082 cp_lexer_set_token_position (parser->lexer, prev);
18083 token = cp_lexer_peek_token (parser->lexer);
18084 token->type = CPP_SEMICOLON;
18085 token->keyword = RID_MAX;
18089 /* If this class is not itself within the scope of another class,
18090 then we need to parse the bodies of all of the queued function
18091 definitions. Note that the queued functions defined in a class
18092 are not always processed immediately following the
18093 class-specifier for that class. Consider:
18095 struct A {
18096 struct B { void f() { sizeof (A); } };
18099 If `f' were processed before the processing of `A' were
18100 completed, there would be no way to compute the size of `A'.
18101 Note that the nesting we are interested in here is lexical --
18102 not the semantic nesting given by TYPE_CONTEXT. In particular,
18103 for:
18105 struct A { struct B; };
18106 struct A::B { void f() { } };
18108 there is no need to delay the parsing of `A::B::f'. */
18109 if (--parser->num_classes_being_defined == 0)
18111 tree decl;
18112 tree class_type = NULL_TREE;
18113 tree pushed_scope = NULL_TREE;
18114 unsigned ix;
18115 cp_default_arg_entry *e;
18116 tree save_ccp, save_ccr;
18118 /* In a first pass, parse default arguments to the functions.
18119 Then, in a second pass, parse the bodies of the functions.
18120 This two-phased approach handles cases like:
18122 struct S {
18123 void f() { g(); }
18124 void g(int i = 3);
18128 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18129 ix, e)
18131 decl = e->decl;
18132 /* If there are default arguments that have not yet been processed,
18133 take care of them now. */
18134 if (class_type != e->class_type)
18136 if (pushed_scope)
18137 pop_scope (pushed_scope);
18138 class_type = e->class_type;
18139 pushed_scope = push_scope (class_type);
18141 /* Make sure that any template parameters are in scope. */
18142 maybe_begin_member_template_processing (decl);
18143 /* Parse the default argument expressions. */
18144 cp_parser_late_parsing_default_args (parser, decl);
18145 /* Remove any template parameters from the symbol table. */
18146 maybe_end_member_template_processing ();
18148 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18149 /* Now parse any NSDMIs. */
18150 save_ccp = current_class_ptr;
18151 save_ccr = current_class_ref;
18152 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18154 if (class_type != DECL_CONTEXT (decl))
18156 if (pushed_scope)
18157 pop_scope (pushed_scope);
18158 class_type = DECL_CONTEXT (decl);
18159 pushed_scope = push_scope (class_type);
18161 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18162 cp_parser_late_parsing_nsdmi (parser, decl);
18164 VEC_truncate (tree, unparsed_nsdmis, 0);
18165 current_class_ptr = save_ccp;
18166 current_class_ref = save_ccr;
18167 if (pushed_scope)
18168 pop_scope (pushed_scope);
18169 /* Now parse the body of the functions. */
18170 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18171 cp_parser_late_parsing_for_member (parser, decl);
18172 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18175 /* Put back any saved access checks. */
18176 pop_deferring_access_checks ();
18178 /* Restore saved state. */
18179 parser->in_switch_statement_p = in_switch_statement_p;
18180 parser->in_statement = in_statement;
18181 parser->in_function_body = saved_in_function_body;
18182 parser->num_template_parameter_lists
18183 = saved_num_template_parameter_lists;
18184 parser->in_unbraced_linkage_specification_p
18185 = saved_in_unbraced_linkage_specification_p;
18187 return type;
18190 static tree
18191 cp_parser_class_specifier (cp_parser* parser)
18193 tree ret;
18194 timevar_push (TV_PARSE_STRUCT);
18195 ret = cp_parser_class_specifier_1 (parser);
18196 timevar_pop (TV_PARSE_STRUCT);
18197 return ret;
18200 /* Parse a class-head.
18202 class-head:
18203 class-key identifier [opt] base-clause [opt]
18204 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18205 class-key nested-name-specifier [opt] template-id
18206 base-clause [opt]
18208 class-virt-specifier:
18209 final
18211 GNU Extensions:
18212 class-key attributes identifier [opt] base-clause [opt]
18213 class-key attributes nested-name-specifier identifier base-clause [opt]
18214 class-key attributes nested-name-specifier [opt] template-id
18215 base-clause [opt]
18217 Upon return BASES is initialized to the list of base classes (or
18218 NULL, if there are none) in the same form returned by
18219 cp_parser_base_clause.
18221 Returns the TYPE of the indicated class. Sets
18222 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18223 involving a nested-name-specifier was used, and FALSE otherwise.
18225 Returns error_mark_node if this is not a class-head.
18227 Returns NULL_TREE if the class-head is syntactically valid, but
18228 semantically invalid in a way that means we should skip the entire
18229 body of the class. */
18231 static tree
18232 cp_parser_class_head (cp_parser* parser,
18233 bool* nested_name_specifier_p,
18234 tree *bases)
18236 tree nested_name_specifier;
18237 enum tag_types class_key;
18238 tree id = NULL_TREE;
18239 tree type = NULL_TREE;
18240 tree attributes;
18241 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18242 bool template_id_p = false;
18243 bool qualified_p = false;
18244 bool invalid_nested_name_p = false;
18245 bool invalid_explicit_specialization_p = false;
18246 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18247 tree pushed_scope = NULL_TREE;
18248 unsigned num_templates;
18249 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18250 /* Assume no nested-name-specifier will be present. */
18251 *nested_name_specifier_p = false;
18252 /* Assume no template parameter lists will be used in defining the
18253 type. */
18254 num_templates = 0;
18255 parser->colon_corrects_to_scope_p = false;
18257 *bases = NULL_TREE;
18259 /* Look for the class-key. */
18260 class_key = cp_parser_class_key (parser);
18261 if (class_key == none_type)
18262 return error_mark_node;
18264 /* Parse the attributes. */
18265 attributes = cp_parser_attributes_opt (parser);
18267 /* If the next token is `::', that is invalid -- but sometimes
18268 people do try to write:
18270 struct ::S {};
18272 Handle this gracefully by accepting the extra qualifier, and then
18273 issuing an error about it later if this really is a
18274 class-head. If it turns out just to be an elaborated type
18275 specifier, remain silent. */
18276 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18277 qualified_p = true;
18279 push_deferring_access_checks (dk_no_check);
18281 /* Determine the name of the class. Begin by looking for an
18282 optional nested-name-specifier. */
18283 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18284 nested_name_specifier
18285 = cp_parser_nested_name_specifier_opt (parser,
18286 /*typename_keyword_p=*/false,
18287 /*check_dependency_p=*/false,
18288 /*type_p=*/false,
18289 /*is_declaration=*/false);
18290 /* If there was a nested-name-specifier, then there *must* be an
18291 identifier. */
18292 if (nested_name_specifier)
18294 type_start_token = cp_lexer_peek_token (parser->lexer);
18295 /* Although the grammar says `identifier', it really means
18296 `class-name' or `template-name'. You are only allowed to
18297 define a class that has already been declared with this
18298 syntax.
18300 The proposed resolution for Core Issue 180 says that wherever
18301 you see `class T::X' you should treat `X' as a type-name.
18303 It is OK to define an inaccessible class; for example:
18305 class A { class B; };
18306 class A::B {};
18308 We do not know if we will see a class-name, or a
18309 template-name. We look for a class-name first, in case the
18310 class-name is a template-id; if we looked for the
18311 template-name first we would stop after the template-name. */
18312 cp_parser_parse_tentatively (parser);
18313 type = cp_parser_class_name (parser,
18314 /*typename_keyword_p=*/false,
18315 /*template_keyword_p=*/false,
18316 class_type,
18317 /*check_dependency_p=*/false,
18318 /*class_head_p=*/true,
18319 /*is_declaration=*/false);
18320 /* If that didn't work, ignore the nested-name-specifier. */
18321 if (!cp_parser_parse_definitely (parser))
18323 invalid_nested_name_p = true;
18324 type_start_token = cp_lexer_peek_token (parser->lexer);
18325 id = cp_parser_identifier (parser);
18326 if (id == error_mark_node)
18327 id = NULL_TREE;
18329 /* If we could not find a corresponding TYPE, treat this
18330 declaration like an unqualified declaration. */
18331 if (type == error_mark_node)
18332 nested_name_specifier = NULL_TREE;
18333 /* Otherwise, count the number of templates used in TYPE and its
18334 containing scopes. */
18335 else
18337 tree scope;
18339 for (scope = TREE_TYPE (type);
18340 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18341 scope = (TYPE_P (scope)
18342 ? TYPE_CONTEXT (scope)
18343 : DECL_CONTEXT (scope)))
18344 if (TYPE_P (scope)
18345 && CLASS_TYPE_P (scope)
18346 && CLASSTYPE_TEMPLATE_INFO (scope)
18347 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18348 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18349 ++num_templates;
18352 /* Otherwise, the identifier is optional. */
18353 else
18355 /* We don't know whether what comes next is a template-id,
18356 an identifier, or nothing at all. */
18357 cp_parser_parse_tentatively (parser);
18358 /* Check for a template-id. */
18359 type_start_token = cp_lexer_peek_token (parser->lexer);
18360 id = cp_parser_template_id (parser,
18361 /*template_keyword_p=*/false,
18362 /*check_dependency_p=*/true,
18363 /*is_declaration=*/true);
18364 /* If that didn't work, it could still be an identifier. */
18365 if (!cp_parser_parse_definitely (parser))
18367 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18369 type_start_token = cp_lexer_peek_token (parser->lexer);
18370 id = cp_parser_identifier (parser);
18372 else
18373 id = NULL_TREE;
18375 else
18377 template_id_p = true;
18378 ++num_templates;
18382 pop_deferring_access_checks ();
18384 if (id)
18386 cp_parser_check_for_invalid_template_id (parser, id,
18387 type_start_token->location);
18389 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18391 /* If it's not a `:' or a `{' then we can't really be looking at a
18392 class-head, since a class-head only appears as part of a
18393 class-specifier. We have to detect this situation before calling
18394 xref_tag, since that has irreversible side-effects. */
18395 if (!cp_parser_next_token_starts_class_definition_p (parser))
18397 cp_parser_error (parser, "expected %<{%> or %<:%>");
18398 type = error_mark_node;
18399 goto out;
18402 /* At this point, we're going ahead with the class-specifier, even
18403 if some other problem occurs. */
18404 cp_parser_commit_to_tentative_parse (parser);
18405 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18407 cp_parser_error (parser,
18408 "cannot specify %<override%> for a class");
18409 type = error_mark_node;
18410 goto out;
18412 /* Issue the error about the overly-qualified name now. */
18413 if (qualified_p)
18415 cp_parser_error (parser,
18416 "global qualification of class name is invalid");
18417 type = error_mark_node;
18418 goto out;
18420 else if (invalid_nested_name_p)
18422 cp_parser_error (parser,
18423 "qualified name does not name a class");
18424 type = error_mark_node;
18425 goto out;
18427 else if (nested_name_specifier)
18429 tree scope;
18431 /* Reject typedef-names in class heads. */
18432 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18434 error_at (type_start_token->location,
18435 "invalid class name in declaration of %qD",
18436 type);
18437 type = NULL_TREE;
18438 goto done;
18441 /* Figure out in what scope the declaration is being placed. */
18442 scope = current_scope ();
18443 /* If that scope does not contain the scope in which the
18444 class was originally declared, the program is invalid. */
18445 if (scope && !is_ancestor (scope, nested_name_specifier))
18447 if (at_namespace_scope_p ())
18448 error_at (type_start_token->location,
18449 "declaration of %qD in namespace %qD which does not "
18450 "enclose %qD",
18451 type, scope, nested_name_specifier);
18452 else
18453 error_at (type_start_token->location,
18454 "declaration of %qD in %qD which does not enclose %qD",
18455 type, scope, nested_name_specifier);
18456 type = NULL_TREE;
18457 goto done;
18459 /* [dcl.meaning]
18461 A declarator-id shall not be qualified except for the
18462 definition of a ... nested class outside of its class
18463 ... [or] the definition or explicit instantiation of a
18464 class member of a namespace outside of its namespace. */
18465 if (scope == nested_name_specifier)
18467 permerror (nested_name_specifier_token_start->location,
18468 "extra qualification not allowed");
18469 nested_name_specifier = NULL_TREE;
18470 num_templates = 0;
18473 /* An explicit-specialization must be preceded by "template <>". If
18474 it is not, try to recover gracefully. */
18475 if (at_namespace_scope_p ()
18476 && parser->num_template_parameter_lists == 0
18477 && template_id_p)
18479 error_at (type_start_token->location,
18480 "an explicit specialization must be preceded by %<template <>%>");
18481 invalid_explicit_specialization_p = true;
18482 /* Take the same action that would have been taken by
18483 cp_parser_explicit_specialization. */
18484 ++parser->num_template_parameter_lists;
18485 begin_specialization ();
18487 /* There must be no "return" statements between this point and the
18488 end of this function; set "type "to the correct return value and
18489 use "goto done;" to return. */
18490 /* Make sure that the right number of template parameters were
18491 present. */
18492 if (!cp_parser_check_template_parameters (parser, num_templates,
18493 type_start_token->location,
18494 /*declarator=*/NULL))
18496 /* If something went wrong, there is no point in even trying to
18497 process the class-definition. */
18498 type = NULL_TREE;
18499 goto done;
18502 /* Look up the type. */
18503 if (template_id_p)
18505 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18506 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18507 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18509 error_at (type_start_token->location,
18510 "function template %qD redeclared as a class template", id);
18511 type = error_mark_node;
18513 else
18515 type = TREE_TYPE (id);
18516 type = maybe_process_partial_specialization (type);
18518 if (nested_name_specifier)
18519 pushed_scope = push_scope (nested_name_specifier);
18521 else if (nested_name_specifier)
18523 tree class_type;
18525 /* Given:
18527 template <typename T> struct S { struct T };
18528 template <typename T> struct S<T>::T { };
18530 we will get a TYPENAME_TYPE when processing the definition of
18531 `S::T'. We need to resolve it to the actual type before we
18532 try to define it. */
18533 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18535 class_type = resolve_typename_type (TREE_TYPE (type),
18536 /*only_current_p=*/false);
18537 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18538 type = TYPE_NAME (class_type);
18539 else
18541 cp_parser_error (parser, "could not resolve typename type");
18542 type = error_mark_node;
18546 if (maybe_process_partial_specialization (TREE_TYPE (type))
18547 == error_mark_node)
18549 type = NULL_TREE;
18550 goto done;
18553 class_type = current_class_type;
18554 /* Enter the scope indicated by the nested-name-specifier. */
18555 pushed_scope = push_scope (nested_name_specifier);
18556 /* Get the canonical version of this type. */
18557 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18558 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18559 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18561 type = push_template_decl (type);
18562 if (type == error_mark_node)
18564 type = NULL_TREE;
18565 goto done;
18569 type = TREE_TYPE (type);
18570 *nested_name_specifier_p = true;
18572 else /* The name is not a nested name. */
18574 /* If the class was unnamed, create a dummy name. */
18575 if (!id)
18576 id = make_anon_name ();
18577 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18578 parser->num_template_parameter_lists);
18581 /* Indicate whether this class was declared as a `class' or as a
18582 `struct'. */
18583 if (TREE_CODE (type) == RECORD_TYPE)
18584 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18585 cp_parser_check_class_key (class_key, type);
18587 /* If this type was already complete, and we see another definition,
18588 that's an error. */
18589 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18591 error_at (type_start_token->location, "redefinition of %q#T",
18592 type);
18593 error_at (type_start_token->location, "previous definition of %q+#T",
18594 type);
18595 type = NULL_TREE;
18596 goto done;
18598 else if (type == error_mark_node)
18599 type = NULL_TREE;
18601 if (type)
18603 /* Apply attributes now, before any use of the class as a template
18604 argument in its base list. */
18605 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18606 fixup_attribute_variants (type);
18609 /* We will have entered the scope containing the class; the names of
18610 base classes should be looked up in that context. For example:
18612 struct A { struct B {}; struct C; };
18613 struct A::C : B {};
18615 is valid. */
18617 /* Get the list of base-classes, if there is one. */
18618 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18619 *bases = cp_parser_base_clause (parser);
18621 done:
18622 /* Leave the scope given by the nested-name-specifier. We will
18623 enter the class scope itself while processing the members. */
18624 if (pushed_scope)
18625 pop_scope (pushed_scope);
18627 if (invalid_explicit_specialization_p)
18629 end_specialization ();
18630 --parser->num_template_parameter_lists;
18633 if (type)
18634 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18635 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18636 CLASSTYPE_FINAL (type) = 1;
18637 out:
18638 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18639 return type;
18642 /* Parse a class-key.
18644 class-key:
18645 class
18646 struct
18647 union
18649 Returns the kind of class-key specified, or none_type to indicate
18650 error. */
18652 static enum tag_types
18653 cp_parser_class_key (cp_parser* parser)
18655 cp_token *token;
18656 enum tag_types tag_type;
18658 /* Look for the class-key. */
18659 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18660 if (!token)
18661 return none_type;
18663 /* Check to see if the TOKEN is a class-key. */
18664 tag_type = cp_parser_token_is_class_key (token);
18665 if (!tag_type)
18666 cp_parser_error (parser, "expected class-key");
18667 return tag_type;
18670 /* Parse an (optional) member-specification.
18672 member-specification:
18673 member-declaration member-specification [opt]
18674 access-specifier : member-specification [opt] */
18676 static void
18677 cp_parser_member_specification_opt (cp_parser* parser)
18679 while (true)
18681 cp_token *token;
18682 enum rid keyword;
18684 /* Peek at the next token. */
18685 token = cp_lexer_peek_token (parser->lexer);
18686 /* If it's a `}', or EOF then we've seen all the members. */
18687 if (token->type == CPP_CLOSE_BRACE
18688 || token->type == CPP_EOF
18689 || token->type == CPP_PRAGMA_EOL)
18690 break;
18692 /* See if this token is a keyword. */
18693 keyword = token->keyword;
18694 switch (keyword)
18696 case RID_PUBLIC:
18697 case RID_PROTECTED:
18698 case RID_PRIVATE:
18699 /* Consume the access-specifier. */
18700 cp_lexer_consume_token (parser->lexer);
18701 /* Remember which access-specifier is active. */
18702 current_access_specifier = token->u.value;
18703 /* Look for the `:'. */
18704 cp_parser_require (parser, CPP_COLON, RT_COLON);
18705 break;
18707 default:
18708 /* Accept #pragmas at class scope. */
18709 if (token->type == CPP_PRAGMA)
18711 cp_parser_pragma (parser, pragma_external);
18712 break;
18715 /* Otherwise, the next construction must be a
18716 member-declaration. */
18717 cp_parser_member_declaration (parser);
18722 /* Parse a member-declaration.
18724 member-declaration:
18725 decl-specifier-seq [opt] member-declarator-list [opt] ;
18726 function-definition ; [opt]
18727 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18728 using-declaration
18729 template-declaration
18730 alias-declaration
18732 member-declarator-list:
18733 member-declarator
18734 member-declarator-list , member-declarator
18736 member-declarator:
18737 declarator pure-specifier [opt]
18738 declarator constant-initializer [opt]
18739 identifier [opt] : constant-expression
18741 GNU Extensions:
18743 member-declaration:
18744 __extension__ member-declaration
18746 member-declarator:
18747 declarator attributes [opt] pure-specifier [opt]
18748 declarator attributes [opt] constant-initializer [opt]
18749 identifier [opt] attributes [opt] : constant-expression
18751 C++0x Extensions:
18753 member-declaration:
18754 static_assert-declaration */
18756 static void
18757 cp_parser_member_declaration (cp_parser* parser)
18759 cp_decl_specifier_seq decl_specifiers;
18760 tree prefix_attributes;
18761 tree decl;
18762 int declares_class_or_enum;
18763 bool friend_p;
18764 cp_token *token = NULL;
18765 cp_token *decl_spec_token_start = NULL;
18766 cp_token *initializer_token_start = NULL;
18767 int saved_pedantic;
18768 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18770 /* Check for the `__extension__' keyword. */
18771 if (cp_parser_extension_opt (parser, &saved_pedantic))
18773 /* Recurse. */
18774 cp_parser_member_declaration (parser);
18775 /* Restore the old value of the PEDANTIC flag. */
18776 pedantic = saved_pedantic;
18778 return;
18781 /* Check for a template-declaration. */
18782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18784 /* An explicit specialization here is an error condition, and we
18785 expect the specialization handler to detect and report this. */
18786 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18787 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18788 cp_parser_explicit_specialization (parser);
18789 else
18790 cp_parser_template_declaration (parser, /*member_p=*/true);
18792 return;
18795 /* Check for a using-declaration. */
18796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18798 if (cxx_dialect < cxx0x)
18800 /* Parse the using-declaration. */
18801 cp_parser_using_declaration (parser,
18802 /*access_declaration_p=*/false);
18803 return;
18805 else
18807 tree decl;
18808 cp_parser_parse_tentatively (parser);
18809 decl = cp_parser_alias_declaration (parser);
18810 if (cp_parser_parse_definitely (parser))
18811 finish_member_declaration (decl);
18812 else
18813 cp_parser_using_declaration (parser,
18814 /*access_declaration_p=*/false);
18815 return;
18819 /* Check for @defs. */
18820 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18822 tree ivar, member;
18823 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18824 ivar = ivar_chains;
18825 while (ivar)
18827 member = ivar;
18828 ivar = TREE_CHAIN (member);
18829 TREE_CHAIN (member) = NULL_TREE;
18830 finish_member_declaration (member);
18832 return;
18835 /* If the next token is `static_assert' we have a static assertion. */
18836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18838 cp_parser_static_assert (parser, /*member_p=*/true);
18839 return;
18842 parser->colon_corrects_to_scope_p = false;
18844 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18845 goto out;
18847 /* Parse the decl-specifier-seq. */
18848 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18849 cp_parser_decl_specifier_seq (parser,
18850 CP_PARSER_FLAGS_OPTIONAL,
18851 &decl_specifiers,
18852 &declares_class_or_enum);
18853 prefix_attributes = decl_specifiers.attributes;
18854 decl_specifiers.attributes = NULL_TREE;
18855 /* Check for an invalid type-name. */
18856 if (!decl_specifiers.any_type_specifiers_p
18857 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18858 goto out;
18859 /* If there is no declarator, then the decl-specifier-seq should
18860 specify a type. */
18861 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18863 /* If there was no decl-specifier-seq, and the next token is a
18864 `;', then we have something like:
18866 struct S { ; };
18868 [class.mem]
18870 Each member-declaration shall declare at least one member
18871 name of the class. */
18872 if (!decl_specifiers.any_specifiers_p)
18874 cp_token *token = cp_lexer_peek_token (parser->lexer);
18875 if (!in_system_header_at (token->location))
18876 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
18878 else
18880 tree type;
18882 /* See if this declaration is a friend. */
18883 friend_p = cp_parser_friend_p (&decl_specifiers);
18884 /* If there were decl-specifiers, check to see if there was
18885 a class-declaration. */
18886 type = check_tag_decl (&decl_specifiers);
18887 /* Nested classes have already been added to the class, but
18888 a `friend' needs to be explicitly registered. */
18889 if (friend_p)
18891 /* If the `friend' keyword was present, the friend must
18892 be introduced with a class-key. */
18893 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18894 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
18895 "in C++03 a class-key must be used "
18896 "when declaring a friend");
18897 /* In this case:
18899 template <typename T> struct A {
18900 friend struct A<T>::B;
18903 A<T>::B will be represented by a TYPENAME_TYPE, and
18904 therefore not recognized by check_tag_decl. */
18905 if (!type)
18907 type = decl_specifiers.type;
18908 if (type && TREE_CODE (type) == TYPE_DECL)
18909 type = TREE_TYPE (type);
18911 if (!type || !TYPE_P (type))
18912 error_at (decl_spec_token_start->location,
18913 "friend declaration does not name a class or "
18914 "function");
18915 else
18916 make_friend_class (current_class_type, type,
18917 /*complain=*/true);
18919 /* If there is no TYPE, an error message will already have
18920 been issued. */
18921 else if (!type || type == error_mark_node)
18923 /* An anonymous aggregate has to be handled specially; such
18924 a declaration really declares a data member (with a
18925 particular type), as opposed to a nested class. */
18926 else if (ANON_AGGR_TYPE_P (type))
18928 /* C++11 9.5/6. */
18929 if (decl_specifiers.storage_class != sc_none)
18930 error_at (decl_spec_token_start->location,
18931 "a storage class on an anonymous aggregate "
18932 "in class scope is not allowed");
18934 /* Remove constructors and such from TYPE, now that we
18935 know it is an anonymous aggregate. */
18936 fixup_anonymous_aggr (type);
18937 /* And make the corresponding data member. */
18938 decl = build_decl (decl_spec_token_start->location,
18939 FIELD_DECL, NULL_TREE, type);
18940 /* Add it to the class. */
18941 finish_member_declaration (decl);
18943 else
18944 cp_parser_check_access_in_redeclaration
18945 (TYPE_NAME (type),
18946 decl_spec_token_start->location);
18949 else
18951 bool assume_semicolon = false;
18953 /* See if these declarations will be friends. */
18954 friend_p = cp_parser_friend_p (&decl_specifiers);
18956 /* Keep going until we hit the `;' at the end of the
18957 declaration. */
18958 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18960 tree attributes = NULL_TREE;
18961 tree first_attribute;
18963 /* Peek at the next token. */
18964 token = cp_lexer_peek_token (parser->lexer);
18966 /* Check for a bitfield declaration. */
18967 if (token->type == CPP_COLON
18968 || (token->type == CPP_NAME
18969 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18970 == CPP_COLON))
18972 tree identifier;
18973 tree width;
18975 /* Get the name of the bitfield. Note that we cannot just
18976 check TOKEN here because it may have been invalidated by
18977 the call to cp_lexer_peek_nth_token above. */
18978 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18979 identifier = cp_parser_identifier (parser);
18980 else
18981 identifier = NULL_TREE;
18983 /* Consume the `:' token. */
18984 cp_lexer_consume_token (parser->lexer);
18985 /* Get the width of the bitfield. */
18986 width
18987 = cp_parser_constant_expression (parser,
18988 /*allow_non_constant=*/false,
18989 NULL);
18991 /* Look for attributes that apply to the bitfield. */
18992 attributes = cp_parser_attributes_opt (parser);
18993 /* Remember which attributes are prefix attributes and
18994 which are not. */
18995 first_attribute = attributes;
18996 /* Combine the attributes. */
18997 attributes = chainon (prefix_attributes, attributes);
18999 /* Create the bitfield declaration. */
19000 decl = grokbitfield (identifier
19001 ? make_id_declarator (NULL_TREE,
19002 identifier,
19003 sfk_none)
19004 : NULL,
19005 &decl_specifiers,
19006 width,
19007 attributes);
19009 else
19011 cp_declarator *declarator;
19012 tree initializer;
19013 tree asm_specification;
19014 int ctor_dtor_or_conv_p;
19016 /* Parse the declarator. */
19017 declarator
19018 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19019 &ctor_dtor_or_conv_p,
19020 /*parenthesized_p=*/NULL,
19021 /*member_p=*/true);
19023 /* If something went wrong parsing the declarator, make sure
19024 that we at least consume some tokens. */
19025 if (declarator == cp_error_declarator)
19027 /* Skip to the end of the statement. */
19028 cp_parser_skip_to_end_of_statement (parser);
19029 /* If the next token is not a semicolon, that is
19030 probably because we just skipped over the body of
19031 a function. So, we consume a semicolon if
19032 present, but do not issue an error message if it
19033 is not present. */
19034 if (cp_lexer_next_token_is (parser->lexer,
19035 CPP_SEMICOLON))
19036 cp_lexer_consume_token (parser->lexer);
19037 goto out;
19040 if (declares_class_or_enum & 2)
19041 cp_parser_check_for_definition_in_return_type
19042 (declarator, decl_specifiers.type,
19043 decl_specifiers.locations[ds_type_spec]);
19045 /* Look for an asm-specification. */
19046 asm_specification = cp_parser_asm_specification_opt (parser);
19047 /* Look for attributes that apply to the declaration. */
19048 attributes = cp_parser_attributes_opt (parser);
19049 /* Remember which attributes are prefix attributes and
19050 which are not. */
19051 first_attribute = attributes;
19052 /* Combine the attributes. */
19053 attributes = chainon (prefix_attributes, attributes);
19055 /* If it's an `=', then we have a constant-initializer or a
19056 pure-specifier. It is not correct to parse the
19057 initializer before registering the member declaration
19058 since the member declaration should be in scope while
19059 its initializer is processed. However, the rest of the
19060 front end does not yet provide an interface that allows
19061 us to handle this correctly. */
19062 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19064 /* In [class.mem]:
19066 A pure-specifier shall be used only in the declaration of
19067 a virtual function.
19069 A member-declarator can contain a constant-initializer
19070 only if it declares a static member of integral or
19071 enumeration type.
19073 Therefore, if the DECLARATOR is for a function, we look
19074 for a pure-specifier; otherwise, we look for a
19075 constant-initializer. When we call `grokfield', it will
19076 perform more stringent semantics checks. */
19077 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19078 if (function_declarator_p (declarator)
19079 || (decl_specifiers.type
19080 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19081 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19082 == FUNCTION_TYPE)))
19083 initializer = cp_parser_pure_specifier (parser);
19084 else if (decl_specifiers.storage_class != sc_static)
19085 initializer = cp_parser_save_nsdmi (parser);
19086 else if (cxx_dialect >= cxx0x)
19088 bool nonconst;
19089 /* Don't require a constant rvalue in C++11, since we
19090 might want a reference constant. We'll enforce
19091 constancy later. */
19092 cp_lexer_consume_token (parser->lexer);
19093 /* Parse the initializer. */
19094 initializer = cp_parser_initializer_clause (parser,
19095 &nonconst);
19097 else
19098 /* Parse the initializer. */
19099 initializer = cp_parser_constant_initializer (parser);
19101 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19102 && !function_declarator_p (declarator))
19104 bool x;
19105 if (decl_specifiers.storage_class != sc_static)
19106 initializer = cp_parser_save_nsdmi (parser);
19107 else
19108 initializer = cp_parser_initializer (parser, &x, &x);
19110 /* Otherwise, there is no initializer. */
19111 else
19112 initializer = NULL_TREE;
19114 /* See if we are probably looking at a function
19115 definition. We are certainly not looking at a
19116 member-declarator. Calling `grokfield' has
19117 side-effects, so we must not do it unless we are sure
19118 that we are looking at a member-declarator. */
19119 if (cp_parser_token_starts_function_definition_p
19120 (cp_lexer_peek_token (parser->lexer)))
19122 /* The grammar does not allow a pure-specifier to be
19123 used when a member function is defined. (It is
19124 possible that this fact is an oversight in the
19125 standard, since a pure function may be defined
19126 outside of the class-specifier. */
19127 if (initializer && initializer_token_start)
19128 error_at (initializer_token_start->location,
19129 "pure-specifier on function-definition");
19130 decl = cp_parser_save_member_function_body (parser,
19131 &decl_specifiers,
19132 declarator,
19133 attributes);
19134 /* If the member was not a friend, declare it here. */
19135 if (!friend_p)
19136 finish_member_declaration (decl);
19137 /* Peek at the next token. */
19138 token = cp_lexer_peek_token (parser->lexer);
19139 /* If the next token is a semicolon, consume it. */
19140 if (token->type == CPP_SEMICOLON)
19141 cp_lexer_consume_token (parser->lexer);
19142 goto out;
19144 else
19145 if (declarator->kind == cdk_function)
19146 declarator->id_loc = token->location;
19147 /* Create the declaration. */
19148 decl = grokfield (declarator, &decl_specifiers,
19149 initializer, /*init_const_expr_p=*/true,
19150 asm_specification,
19151 attributes);
19154 /* Reset PREFIX_ATTRIBUTES. */
19155 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19156 attributes = TREE_CHAIN (attributes);
19157 if (attributes)
19158 TREE_CHAIN (attributes) = NULL_TREE;
19160 /* If there is any qualification still in effect, clear it
19161 now; we will be starting fresh with the next declarator. */
19162 parser->scope = NULL_TREE;
19163 parser->qualifying_scope = NULL_TREE;
19164 parser->object_scope = NULL_TREE;
19165 /* If it's a `,', then there are more declarators. */
19166 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19167 cp_lexer_consume_token (parser->lexer);
19168 /* If the next token isn't a `;', then we have a parse error. */
19169 else if (cp_lexer_next_token_is_not (parser->lexer,
19170 CPP_SEMICOLON))
19172 /* The next token might be a ways away from where the
19173 actual semicolon is missing. Find the previous token
19174 and use that for our error position. */
19175 cp_token *token = cp_lexer_previous_token (parser->lexer);
19176 error_at (token->location,
19177 "expected %<;%> at end of member declaration");
19179 /* Assume that the user meant to provide a semicolon. If
19180 we were to cp_parser_skip_to_end_of_statement, we might
19181 skip to a semicolon inside a member function definition
19182 and issue nonsensical error messages. */
19183 assume_semicolon = true;
19186 if (decl)
19188 /* Add DECL to the list of members. */
19189 if (!friend_p)
19190 finish_member_declaration (decl);
19192 if (TREE_CODE (decl) == FUNCTION_DECL)
19193 cp_parser_save_default_args (parser, decl);
19194 else if (TREE_CODE (decl) == FIELD_DECL
19195 && !DECL_C_BIT_FIELD (decl)
19196 && DECL_INITIAL (decl))
19197 /* Add DECL to the queue of NSDMI to be parsed later. */
19198 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19201 if (assume_semicolon)
19202 goto out;
19206 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19207 out:
19208 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19211 /* Parse a pure-specifier.
19213 pure-specifier:
19216 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19217 Otherwise, ERROR_MARK_NODE is returned. */
19219 static tree
19220 cp_parser_pure_specifier (cp_parser* parser)
19222 cp_token *token;
19224 /* Look for the `=' token. */
19225 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19226 return error_mark_node;
19227 /* Look for the `0' token. */
19228 token = cp_lexer_peek_token (parser->lexer);
19230 if (token->type == CPP_EOF
19231 || token->type == CPP_PRAGMA_EOL)
19232 return error_mark_node;
19234 cp_lexer_consume_token (parser->lexer);
19236 /* Accept = default or = delete in c++0x mode. */
19237 if (token->keyword == RID_DEFAULT
19238 || token->keyword == RID_DELETE)
19240 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19241 return token->u.value;
19244 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19245 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19247 cp_parser_error (parser,
19248 "invalid pure specifier (only %<= 0%> is allowed)");
19249 cp_parser_skip_to_end_of_statement (parser);
19250 return error_mark_node;
19252 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19254 error_at (token->location, "templates may not be %<virtual%>");
19255 return error_mark_node;
19258 return integer_zero_node;
19261 /* Parse a constant-initializer.
19263 constant-initializer:
19264 = constant-expression
19266 Returns a representation of the constant-expression. */
19268 static tree
19269 cp_parser_constant_initializer (cp_parser* parser)
19271 /* Look for the `=' token. */
19272 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19273 return error_mark_node;
19275 /* It is invalid to write:
19277 struct S { static const int i = { 7 }; };
19280 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19282 cp_parser_error (parser,
19283 "a brace-enclosed initializer is not allowed here");
19284 /* Consume the opening brace. */
19285 cp_lexer_consume_token (parser->lexer);
19286 /* Skip the initializer. */
19287 cp_parser_skip_to_closing_brace (parser);
19288 /* Look for the trailing `}'. */
19289 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19291 return error_mark_node;
19294 return cp_parser_constant_expression (parser,
19295 /*allow_non_constant=*/false,
19296 NULL);
19299 /* Derived classes [gram.class.derived] */
19301 /* Parse a base-clause.
19303 base-clause:
19304 : base-specifier-list
19306 base-specifier-list:
19307 base-specifier ... [opt]
19308 base-specifier-list , base-specifier ... [opt]
19310 Returns a TREE_LIST representing the base-classes, in the order in
19311 which they were declared. The representation of each node is as
19312 described by cp_parser_base_specifier.
19314 In the case that no bases are specified, this function will return
19315 NULL_TREE, not ERROR_MARK_NODE. */
19317 static tree
19318 cp_parser_base_clause (cp_parser* parser)
19320 tree bases = NULL_TREE;
19322 /* Look for the `:' that begins the list. */
19323 cp_parser_require (parser, CPP_COLON, RT_COLON);
19325 /* Scan the base-specifier-list. */
19326 while (true)
19328 cp_token *token;
19329 tree base;
19330 bool pack_expansion_p = false;
19332 /* Look for the base-specifier. */
19333 base = cp_parser_base_specifier (parser);
19334 /* Look for the (optional) ellipsis. */
19335 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19337 /* Consume the `...'. */
19338 cp_lexer_consume_token (parser->lexer);
19340 pack_expansion_p = true;
19343 /* Add BASE to the front of the list. */
19344 if (base && base != error_mark_node)
19346 if (pack_expansion_p)
19347 /* Make this a pack expansion type. */
19348 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19350 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19352 TREE_CHAIN (base) = bases;
19353 bases = base;
19356 /* Peek at the next token. */
19357 token = cp_lexer_peek_token (parser->lexer);
19358 /* If it's not a comma, then the list is complete. */
19359 if (token->type != CPP_COMMA)
19360 break;
19361 /* Consume the `,'. */
19362 cp_lexer_consume_token (parser->lexer);
19365 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19366 base class had a qualified name. However, the next name that
19367 appears is certainly not qualified. */
19368 parser->scope = NULL_TREE;
19369 parser->qualifying_scope = NULL_TREE;
19370 parser->object_scope = NULL_TREE;
19372 return nreverse (bases);
19375 /* Parse a base-specifier.
19377 base-specifier:
19378 :: [opt] nested-name-specifier [opt] class-name
19379 virtual access-specifier [opt] :: [opt] nested-name-specifier
19380 [opt] class-name
19381 access-specifier virtual [opt] :: [opt] nested-name-specifier
19382 [opt] class-name
19384 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19385 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19386 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19387 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19389 static tree
19390 cp_parser_base_specifier (cp_parser* parser)
19392 cp_token *token;
19393 bool done = false;
19394 bool virtual_p = false;
19395 bool duplicate_virtual_error_issued_p = false;
19396 bool duplicate_access_error_issued_p = false;
19397 bool class_scope_p, template_p;
19398 tree access = access_default_node;
19399 tree type;
19401 /* Process the optional `virtual' and `access-specifier'. */
19402 while (!done)
19404 /* Peek at the next token. */
19405 token = cp_lexer_peek_token (parser->lexer);
19406 /* Process `virtual'. */
19407 switch (token->keyword)
19409 case RID_VIRTUAL:
19410 /* If `virtual' appears more than once, issue an error. */
19411 if (virtual_p && !duplicate_virtual_error_issued_p)
19413 cp_parser_error (parser,
19414 "%<virtual%> specified more than once in base-specified");
19415 duplicate_virtual_error_issued_p = true;
19418 virtual_p = true;
19420 /* Consume the `virtual' token. */
19421 cp_lexer_consume_token (parser->lexer);
19423 break;
19425 case RID_PUBLIC:
19426 case RID_PROTECTED:
19427 case RID_PRIVATE:
19428 /* If more than one access specifier appears, issue an
19429 error. */
19430 if (access != access_default_node
19431 && !duplicate_access_error_issued_p)
19433 cp_parser_error (parser,
19434 "more than one access specifier in base-specified");
19435 duplicate_access_error_issued_p = true;
19438 access = ridpointers[(int) token->keyword];
19440 /* Consume the access-specifier. */
19441 cp_lexer_consume_token (parser->lexer);
19443 break;
19445 default:
19446 done = true;
19447 break;
19450 /* It is not uncommon to see programs mechanically, erroneously, use
19451 the 'typename' keyword to denote (dependent) qualified types
19452 as base classes. */
19453 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19455 token = cp_lexer_peek_token (parser->lexer);
19456 if (!processing_template_decl)
19457 error_at (token->location,
19458 "keyword %<typename%> not allowed outside of templates");
19459 else
19460 error_at (token->location,
19461 "keyword %<typename%> not allowed in this context "
19462 "(the base class is implicitly a type)");
19463 cp_lexer_consume_token (parser->lexer);
19466 /* Look for the optional `::' operator. */
19467 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19468 /* Look for the nested-name-specifier. The simplest way to
19469 implement:
19471 [temp.res]
19473 The keyword `typename' is not permitted in a base-specifier or
19474 mem-initializer; in these contexts a qualified name that
19475 depends on a template-parameter is implicitly assumed to be a
19476 type name.
19478 is to pretend that we have seen the `typename' keyword at this
19479 point. */
19480 cp_parser_nested_name_specifier_opt (parser,
19481 /*typename_keyword_p=*/true,
19482 /*check_dependency_p=*/true,
19483 typename_type,
19484 /*is_declaration=*/true);
19485 /* If the base class is given by a qualified name, assume that names
19486 we see are type names or templates, as appropriate. */
19487 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19488 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19490 if (!parser->scope
19491 && cp_lexer_next_token_is_decltype (parser->lexer))
19492 /* DR 950 allows decltype as a base-specifier. */
19493 type = cp_parser_decltype (parser);
19494 else
19496 /* Otherwise, look for the class-name. */
19497 type = cp_parser_class_name (parser,
19498 class_scope_p,
19499 template_p,
19500 typename_type,
19501 /*check_dependency_p=*/true,
19502 /*class_head_p=*/false,
19503 /*is_declaration=*/true);
19504 type = TREE_TYPE (type);
19507 if (type == error_mark_node)
19508 return error_mark_node;
19510 return finish_base_specifier (type, access, virtual_p);
19513 /* Exception handling [gram.exception] */
19515 /* Parse an (optional) noexcept-specification.
19517 noexcept-specification:
19518 noexcept ( constant-expression ) [opt]
19520 If no noexcept-specification is present, returns NULL_TREE.
19521 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19522 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19523 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19524 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19525 in which case a boolean condition is returned instead. */
19527 static tree
19528 cp_parser_noexcept_specification_opt (cp_parser* parser,
19529 bool require_constexpr,
19530 bool* consumed_expr,
19531 bool return_cond)
19533 cp_token *token;
19534 const char *saved_message;
19536 /* Peek at the next token. */
19537 token = cp_lexer_peek_token (parser->lexer);
19539 /* Is it a noexcept-specification? */
19540 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19542 tree expr;
19543 cp_lexer_consume_token (parser->lexer);
19545 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19547 cp_lexer_consume_token (parser->lexer);
19549 if (require_constexpr)
19551 /* Types may not be defined in an exception-specification. */
19552 saved_message = parser->type_definition_forbidden_message;
19553 parser->type_definition_forbidden_message
19554 = G_("types may not be defined in an exception-specification");
19556 expr = cp_parser_constant_expression (parser, false, NULL);
19558 /* Restore the saved message. */
19559 parser->type_definition_forbidden_message = saved_message;
19561 else
19563 expr = cp_parser_expression (parser, false, NULL);
19564 *consumed_expr = true;
19567 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19569 else
19571 expr = boolean_true_node;
19572 if (!require_constexpr)
19573 *consumed_expr = false;
19576 /* We cannot build a noexcept-spec right away because this will check
19577 that expr is a constexpr. */
19578 if (!return_cond)
19579 return build_noexcept_spec (expr, tf_warning_or_error);
19580 else
19581 return expr;
19583 else
19584 return NULL_TREE;
19587 /* Parse an (optional) exception-specification.
19589 exception-specification:
19590 throw ( type-id-list [opt] )
19592 Returns a TREE_LIST representing the exception-specification. The
19593 TREE_VALUE of each node is a type. */
19595 static tree
19596 cp_parser_exception_specification_opt (cp_parser* parser)
19598 cp_token *token;
19599 tree type_id_list;
19600 const char *saved_message;
19602 /* Peek at the next token. */
19603 token = cp_lexer_peek_token (parser->lexer);
19605 /* Is it a noexcept-specification? */
19606 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19607 false);
19608 if (type_id_list != NULL_TREE)
19609 return type_id_list;
19611 /* If it's not `throw', then there's no exception-specification. */
19612 if (!cp_parser_is_keyword (token, RID_THROW))
19613 return NULL_TREE;
19615 #if 0
19616 /* Enable this once a lot of code has transitioned to noexcept? */
19617 if (cxx_dialect >= cxx0x && !in_system_header)
19618 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19619 "deprecated in C++0x; use %<noexcept%> instead");
19620 #endif
19622 /* Consume the `throw'. */
19623 cp_lexer_consume_token (parser->lexer);
19625 /* Look for the `('. */
19626 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19628 /* Peek at the next token. */
19629 token = cp_lexer_peek_token (parser->lexer);
19630 /* If it's not a `)', then there is a type-id-list. */
19631 if (token->type != CPP_CLOSE_PAREN)
19633 /* Types may not be defined in an exception-specification. */
19634 saved_message = parser->type_definition_forbidden_message;
19635 parser->type_definition_forbidden_message
19636 = G_("types may not be defined in an exception-specification");
19637 /* Parse the type-id-list. */
19638 type_id_list = cp_parser_type_id_list (parser);
19639 /* Restore the saved message. */
19640 parser->type_definition_forbidden_message = saved_message;
19642 else
19643 type_id_list = empty_except_spec;
19645 /* Look for the `)'. */
19646 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19648 return type_id_list;
19651 /* Parse an (optional) type-id-list.
19653 type-id-list:
19654 type-id ... [opt]
19655 type-id-list , type-id ... [opt]
19657 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19658 in the order that the types were presented. */
19660 static tree
19661 cp_parser_type_id_list (cp_parser* parser)
19663 tree types = NULL_TREE;
19665 while (true)
19667 cp_token *token;
19668 tree type;
19670 /* Get the next type-id. */
19671 type = cp_parser_type_id (parser);
19672 /* Parse the optional ellipsis. */
19673 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19675 /* Consume the `...'. */
19676 cp_lexer_consume_token (parser->lexer);
19678 /* Turn the type into a pack expansion expression. */
19679 type = make_pack_expansion (type);
19681 /* Add it to the list. */
19682 types = add_exception_specifier (types, type, /*complain=*/1);
19683 /* Peek at the next token. */
19684 token = cp_lexer_peek_token (parser->lexer);
19685 /* If it is not a `,', we are done. */
19686 if (token->type != CPP_COMMA)
19687 break;
19688 /* Consume the `,'. */
19689 cp_lexer_consume_token (parser->lexer);
19692 return nreverse (types);
19695 /* Parse a try-block.
19697 try-block:
19698 try compound-statement handler-seq */
19700 static tree
19701 cp_parser_try_block (cp_parser* parser)
19703 tree try_block;
19705 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19706 try_block = begin_try_block ();
19707 cp_parser_compound_statement (parser, NULL, true, false);
19708 finish_try_block (try_block);
19709 cp_parser_handler_seq (parser);
19710 finish_handler_sequence (try_block);
19712 return try_block;
19715 /* Parse a function-try-block.
19717 function-try-block:
19718 try ctor-initializer [opt] function-body handler-seq */
19720 static bool
19721 cp_parser_function_try_block (cp_parser* parser)
19723 tree compound_stmt;
19724 tree try_block;
19725 bool ctor_initializer_p;
19727 /* Look for the `try' keyword. */
19728 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19729 return false;
19730 /* Let the rest of the front end know where we are. */
19731 try_block = begin_function_try_block (&compound_stmt);
19732 /* Parse the function-body. */
19733 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19734 (parser, /*in_function_try_block=*/true);
19735 /* We're done with the `try' part. */
19736 finish_function_try_block (try_block);
19737 /* Parse the handlers. */
19738 cp_parser_handler_seq (parser);
19739 /* We're done with the handlers. */
19740 finish_function_handler_sequence (try_block, compound_stmt);
19742 return ctor_initializer_p;
19745 /* Parse a handler-seq.
19747 handler-seq:
19748 handler handler-seq [opt] */
19750 static void
19751 cp_parser_handler_seq (cp_parser* parser)
19753 while (true)
19755 cp_token *token;
19757 /* Parse the handler. */
19758 cp_parser_handler (parser);
19759 /* Peek at the next token. */
19760 token = cp_lexer_peek_token (parser->lexer);
19761 /* If it's not `catch' then there are no more handlers. */
19762 if (!cp_parser_is_keyword (token, RID_CATCH))
19763 break;
19767 /* Parse a handler.
19769 handler:
19770 catch ( exception-declaration ) compound-statement */
19772 static void
19773 cp_parser_handler (cp_parser* parser)
19775 tree handler;
19776 tree declaration;
19778 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19779 handler = begin_handler ();
19780 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19781 declaration = cp_parser_exception_declaration (parser);
19782 finish_handler_parms (declaration, handler);
19783 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19784 cp_parser_compound_statement (parser, NULL, false, false);
19785 finish_handler (handler);
19788 /* Parse an exception-declaration.
19790 exception-declaration:
19791 type-specifier-seq declarator
19792 type-specifier-seq abstract-declarator
19793 type-specifier-seq
19796 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19797 ellipsis variant is used. */
19799 static tree
19800 cp_parser_exception_declaration (cp_parser* parser)
19802 cp_decl_specifier_seq type_specifiers;
19803 cp_declarator *declarator;
19804 const char *saved_message;
19806 /* If it's an ellipsis, it's easy to handle. */
19807 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19809 /* Consume the `...' token. */
19810 cp_lexer_consume_token (parser->lexer);
19811 return NULL_TREE;
19814 /* Types may not be defined in exception-declarations. */
19815 saved_message = parser->type_definition_forbidden_message;
19816 parser->type_definition_forbidden_message
19817 = G_("types may not be defined in exception-declarations");
19819 /* Parse the type-specifier-seq. */
19820 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19821 /*is_trailing_return=*/false,
19822 &type_specifiers);
19823 /* If it's a `)', then there is no declarator. */
19824 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19825 declarator = NULL;
19826 else
19827 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19828 /*ctor_dtor_or_conv_p=*/NULL,
19829 /*parenthesized_p=*/NULL,
19830 /*member_p=*/false);
19832 /* Restore the saved message. */
19833 parser->type_definition_forbidden_message = saved_message;
19835 if (!type_specifiers.any_specifiers_p)
19836 return error_mark_node;
19838 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19841 /* Parse a throw-expression.
19843 throw-expression:
19844 throw assignment-expression [opt]
19846 Returns a THROW_EXPR representing the throw-expression. */
19848 static tree
19849 cp_parser_throw_expression (cp_parser* parser)
19851 tree expression;
19852 cp_token* token;
19854 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19855 token = cp_lexer_peek_token (parser->lexer);
19856 /* Figure out whether or not there is an assignment-expression
19857 following the "throw" keyword. */
19858 if (token->type == CPP_COMMA
19859 || token->type == CPP_SEMICOLON
19860 || token->type == CPP_CLOSE_PAREN
19861 || token->type == CPP_CLOSE_SQUARE
19862 || token->type == CPP_CLOSE_BRACE
19863 || token->type == CPP_COLON)
19864 expression = NULL_TREE;
19865 else
19866 expression = cp_parser_assignment_expression (parser,
19867 /*cast_p=*/false, NULL);
19869 return build_throw (expression);
19872 /* GNU Extensions */
19874 /* Parse an (optional) asm-specification.
19876 asm-specification:
19877 asm ( string-literal )
19879 If the asm-specification is present, returns a STRING_CST
19880 corresponding to the string-literal. Otherwise, returns
19881 NULL_TREE. */
19883 static tree
19884 cp_parser_asm_specification_opt (cp_parser* parser)
19886 cp_token *token;
19887 tree asm_specification;
19889 /* Peek at the next token. */
19890 token = cp_lexer_peek_token (parser->lexer);
19891 /* If the next token isn't the `asm' keyword, then there's no
19892 asm-specification. */
19893 if (!cp_parser_is_keyword (token, RID_ASM))
19894 return NULL_TREE;
19896 /* Consume the `asm' token. */
19897 cp_lexer_consume_token (parser->lexer);
19898 /* Look for the `('. */
19899 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19901 /* Look for the string-literal. */
19902 asm_specification = cp_parser_string_literal (parser, false, false);
19904 /* Look for the `)'. */
19905 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19907 return asm_specification;
19910 /* Parse an asm-operand-list.
19912 asm-operand-list:
19913 asm-operand
19914 asm-operand-list , asm-operand
19916 asm-operand:
19917 string-literal ( expression )
19918 [ string-literal ] string-literal ( expression )
19920 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19921 each node is the expression. The TREE_PURPOSE is itself a
19922 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19923 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19924 is a STRING_CST for the string literal before the parenthesis. Returns
19925 ERROR_MARK_NODE if any of the operands are invalid. */
19927 static tree
19928 cp_parser_asm_operand_list (cp_parser* parser)
19930 tree asm_operands = NULL_TREE;
19931 bool invalid_operands = false;
19933 while (true)
19935 tree string_literal;
19936 tree expression;
19937 tree name;
19939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19941 /* Consume the `[' token. */
19942 cp_lexer_consume_token (parser->lexer);
19943 /* Read the operand name. */
19944 name = cp_parser_identifier (parser);
19945 if (name != error_mark_node)
19946 name = build_string (IDENTIFIER_LENGTH (name),
19947 IDENTIFIER_POINTER (name));
19948 /* Look for the closing `]'. */
19949 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19951 else
19952 name = NULL_TREE;
19953 /* Look for the string-literal. */
19954 string_literal = cp_parser_string_literal (parser, false, false);
19956 /* Look for the `('. */
19957 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19958 /* Parse the expression. */
19959 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19960 /* Look for the `)'. */
19961 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19963 if (name == error_mark_node
19964 || string_literal == error_mark_node
19965 || expression == error_mark_node)
19966 invalid_operands = true;
19968 /* Add this operand to the list. */
19969 asm_operands = tree_cons (build_tree_list (name, string_literal),
19970 expression,
19971 asm_operands);
19972 /* If the next token is not a `,', there are no more
19973 operands. */
19974 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19975 break;
19976 /* Consume the `,'. */
19977 cp_lexer_consume_token (parser->lexer);
19980 return invalid_operands ? error_mark_node : nreverse (asm_operands);
19983 /* Parse an asm-clobber-list.
19985 asm-clobber-list:
19986 string-literal
19987 asm-clobber-list , string-literal
19989 Returns a TREE_LIST, indicating the clobbers in the order that they
19990 appeared. The TREE_VALUE of each node is a STRING_CST. */
19992 static tree
19993 cp_parser_asm_clobber_list (cp_parser* parser)
19995 tree clobbers = NULL_TREE;
19997 while (true)
19999 tree string_literal;
20001 /* Look for the string literal. */
20002 string_literal = cp_parser_string_literal (parser, false, false);
20003 /* Add it to the list. */
20004 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20005 /* If the next token is not a `,', then the list is
20006 complete. */
20007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20008 break;
20009 /* Consume the `,' token. */
20010 cp_lexer_consume_token (parser->lexer);
20013 return clobbers;
20016 /* Parse an asm-label-list.
20018 asm-label-list:
20019 identifier
20020 asm-label-list , identifier
20022 Returns a TREE_LIST, indicating the labels in the order that they
20023 appeared. The TREE_VALUE of each node is a label. */
20025 static tree
20026 cp_parser_asm_label_list (cp_parser* parser)
20028 tree labels = NULL_TREE;
20030 while (true)
20032 tree identifier, label, name;
20034 /* Look for the identifier. */
20035 identifier = cp_parser_identifier (parser);
20036 if (!error_operand_p (identifier))
20038 label = lookup_label (identifier);
20039 if (TREE_CODE (label) == LABEL_DECL)
20041 TREE_USED (label) = 1;
20042 check_goto (label);
20043 name = build_string (IDENTIFIER_LENGTH (identifier),
20044 IDENTIFIER_POINTER (identifier));
20045 labels = tree_cons (name, label, labels);
20048 /* If the next token is not a `,', then the list is
20049 complete. */
20050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20051 break;
20052 /* Consume the `,' token. */
20053 cp_lexer_consume_token (parser->lexer);
20056 return nreverse (labels);
20059 /* Parse an (optional) series of attributes.
20061 attributes:
20062 attributes attribute
20064 attribute:
20065 __attribute__ (( attribute-list [opt] ))
20067 The return value is as for cp_parser_attribute_list. */
20069 static tree
20070 cp_parser_attributes_opt (cp_parser* parser)
20072 tree attributes = NULL_TREE;
20074 while (true)
20076 cp_token *token;
20077 tree attribute_list;
20078 bool ok = true;
20080 /* Peek at the next token. */
20081 token = cp_lexer_peek_token (parser->lexer);
20082 /* If it's not `__attribute__', then we're done. */
20083 if (token->keyword != RID_ATTRIBUTE)
20084 break;
20086 /* Consume the `__attribute__' keyword. */
20087 cp_lexer_consume_token (parser->lexer);
20088 /* Look for the two `(' tokens. */
20089 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20090 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20092 /* Peek at the next token. */
20093 token = cp_lexer_peek_token (parser->lexer);
20094 if (token->type != CPP_CLOSE_PAREN)
20095 /* Parse the attribute-list. */
20096 attribute_list = cp_parser_attribute_list (parser);
20097 else
20098 /* If the next token is a `)', then there is no attribute
20099 list. */
20100 attribute_list = NULL;
20102 /* Look for the two `)' tokens. */
20103 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20104 ok = false;
20105 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20106 ok = false;
20107 if (!ok)
20108 cp_parser_skip_to_end_of_statement (parser);
20110 /* Add these new attributes to the list. */
20111 attributes = chainon (attributes, attribute_list);
20114 return attributes;
20117 /* Parse an attribute-list.
20119 attribute-list:
20120 attribute
20121 attribute-list , attribute
20123 attribute:
20124 identifier
20125 identifier ( identifier )
20126 identifier ( identifier , expression-list )
20127 identifier ( expression-list )
20129 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20130 to an attribute. The TREE_PURPOSE of each node is the identifier
20131 indicating which attribute is in use. The TREE_VALUE represents
20132 the arguments, if any. */
20134 static tree
20135 cp_parser_attribute_list (cp_parser* parser)
20137 tree attribute_list = NULL_TREE;
20138 bool save_translate_strings_p = parser->translate_strings_p;
20140 parser->translate_strings_p = false;
20141 while (true)
20143 cp_token *token;
20144 tree identifier;
20145 tree attribute;
20147 /* Look for the identifier. We also allow keywords here; for
20148 example `__attribute__ ((const))' is legal. */
20149 token = cp_lexer_peek_token (parser->lexer);
20150 if (token->type == CPP_NAME
20151 || token->type == CPP_KEYWORD)
20153 tree arguments = NULL_TREE;
20155 /* Consume the token. */
20156 token = cp_lexer_consume_token (parser->lexer);
20158 /* Save away the identifier that indicates which attribute
20159 this is. */
20160 identifier = (token->type == CPP_KEYWORD)
20161 /* For keywords, use the canonical spelling, not the
20162 parsed identifier. */
20163 ? ridpointers[(int) token->keyword]
20164 : token->u.value;
20166 attribute = build_tree_list (identifier, NULL_TREE);
20168 /* Peek at the next token. */
20169 token = cp_lexer_peek_token (parser->lexer);
20170 /* If it's an `(', then parse the attribute arguments. */
20171 if (token->type == CPP_OPEN_PAREN)
20173 VEC(tree,gc) *vec;
20174 int attr_flag = (attribute_takes_identifier_p (identifier)
20175 ? id_attr : normal_attr);
20176 vec = cp_parser_parenthesized_expression_list
20177 (parser, attr_flag, /*cast_p=*/false,
20178 /*allow_expansion_p=*/false,
20179 /*non_constant_p=*/NULL);
20180 if (vec == NULL)
20181 arguments = error_mark_node;
20182 else
20184 arguments = build_tree_list_vec (vec);
20185 release_tree_vector (vec);
20187 /* Save the arguments away. */
20188 TREE_VALUE (attribute) = arguments;
20191 if (arguments != error_mark_node)
20193 /* Add this attribute to the list. */
20194 TREE_CHAIN (attribute) = attribute_list;
20195 attribute_list = attribute;
20198 token = cp_lexer_peek_token (parser->lexer);
20200 /* Now, look for more attributes. If the next token isn't a
20201 `,', we're done. */
20202 if (token->type != CPP_COMMA)
20203 break;
20205 /* Consume the comma and keep going. */
20206 cp_lexer_consume_token (parser->lexer);
20208 parser->translate_strings_p = save_translate_strings_p;
20210 /* We built up the list in reverse order. */
20211 return nreverse (attribute_list);
20214 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20215 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20216 current value of the PEDANTIC flag, regardless of whether or not
20217 the `__extension__' keyword is present. The caller is responsible
20218 for restoring the value of the PEDANTIC flag. */
20220 static bool
20221 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20223 /* Save the old value of the PEDANTIC flag. */
20224 *saved_pedantic = pedantic;
20226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20228 /* Consume the `__extension__' token. */
20229 cp_lexer_consume_token (parser->lexer);
20230 /* We're not being pedantic while the `__extension__' keyword is
20231 in effect. */
20232 pedantic = 0;
20234 return true;
20237 return false;
20240 /* Parse a label declaration.
20242 label-declaration:
20243 __label__ label-declarator-seq ;
20245 label-declarator-seq:
20246 identifier , label-declarator-seq
20247 identifier */
20249 static void
20250 cp_parser_label_declaration (cp_parser* parser)
20252 /* Look for the `__label__' keyword. */
20253 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20255 while (true)
20257 tree identifier;
20259 /* Look for an identifier. */
20260 identifier = cp_parser_identifier (parser);
20261 /* If we failed, stop. */
20262 if (identifier == error_mark_node)
20263 break;
20264 /* Declare it as a label. */
20265 finish_label_decl (identifier);
20266 /* If the next token is a `;', stop. */
20267 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20268 break;
20269 /* Look for the `,' separating the label declarations. */
20270 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20273 /* Look for the final `;'. */
20274 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20277 /* Support Functions */
20279 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20280 NAME should have one of the representations used for an
20281 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20282 is returned. If PARSER->SCOPE is a dependent type, then a
20283 SCOPE_REF is returned.
20285 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20286 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20287 was formed. Abstractly, such entities should not be passed to this
20288 function, because they do not need to be looked up, but it is
20289 simpler to check for this special case here, rather than at the
20290 call-sites.
20292 In cases not explicitly covered above, this function returns a
20293 DECL, OVERLOAD, or baselink representing the result of the lookup.
20294 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20295 is returned.
20297 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20298 (e.g., "struct") that was used. In that case bindings that do not
20299 refer to types are ignored.
20301 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20302 ignored.
20304 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20305 are ignored.
20307 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20308 types.
20310 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20311 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20312 NULL_TREE otherwise. */
20314 static tree
20315 cp_parser_lookup_name (cp_parser *parser, tree name,
20316 enum tag_types tag_type,
20317 bool is_template,
20318 bool is_namespace,
20319 bool check_dependency,
20320 tree *ambiguous_decls,
20321 location_t name_location)
20323 tree decl;
20324 tree object_type = parser->context->object_type;
20326 /* Assume that the lookup will be unambiguous. */
20327 if (ambiguous_decls)
20328 *ambiguous_decls = NULL_TREE;
20330 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20331 no longer valid. Note that if we are parsing tentatively, and
20332 the parse fails, OBJECT_TYPE will be automatically restored. */
20333 parser->context->object_type = NULL_TREE;
20335 if (name == error_mark_node)
20336 return error_mark_node;
20338 /* A template-id has already been resolved; there is no lookup to
20339 do. */
20340 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20341 return name;
20342 if (BASELINK_P (name))
20344 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20345 == TEMPLATE_ID_EXPR);
20346 return name;
20349 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20350 it should already have been checked to make sure that the name
20351 used matches the type being destroyed. */
20352 if (TREE_CODE (name) == BIT_NOT_EXPR)
20354 tree type;
20356 /* Figure out to which type this destructor applies. */
20357 if (parser->scope)
20358 type = parser->scope;
20359 else if (object_type)
20360 type = object_type;
20361 else
20362 type = current_class_type;
20363 /* If that's not a class type, there is no destructor. */
20364 if (!type || !CLASS_TYPE_P (type))
20365 return error_mark_node;
20366 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20367 lazily_declare_fn (sfk_destructor, type);
20368 if (!CLASSTYPE_DESTRUCTORS (type))
20369 return error_mark_node;
20370 /* If it was a class type, return the destructor. */
20371 return CLASSTYPE_DESTRUCTORS (type);
20374 /* By this point, the NAME should be an ordinary identifier. If
20375 the id-expression was a qualified name, the qualifying scope is
20376 stored in PARSER->SCOPE at this point. */
20377 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20379 /* Perform the lookup. */
20380 if (parser->scope)
20382 bool dependent_p;
20384 if (parser->scope == error_mark_node)
20385 return error_mark_node;
20387 /* If the SCOPE is dependent, the lookup must be deferred until
20388 the template is instantiated -- unless we are explicitly
20389 looking up names in uninstantiated templates. Even then, we
20390 cannot look up the name if the scope is not a class type; it
20391 might, for example, be a template type parameter. */
20392 dependent_p = (TYPE_P (parser->scope)
20393 && dependent_scope_p (parser->scope));
20394 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20395 && dependent_p)
20396 /* Defer lookup. */
20397 decl = error_mark_node;
20398 else
20400 tree pushed_scope = NULL_TREE;
20402 /* If PARSER->SCOPE is a dependent type, then it must be a
20403 class type, and we must not be checking dependencies;
20404 otherwise, we would have processed this lookup above. So
20405 that PARSER->SCOPE is not considered a dependent base by
20406 lookup_member, we must enter the scope here. */
20407 if (dependent_p)
20408 pushed_scope = push_scope (parser->scope);
20410 /* If the PARSER->SCOPE is a template specialization, it
20411 may be instantiated during name lookup. In that case,
20412 errors may be issued. Even if we rollback the current
20413 tentative parse, those errors are valid. */
20414 decl = lookup_qualified_name (parser->scope, name,
20415 tag_type != none_type,
20416 /*complain=*/true);
20418 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20419 lookup result and the nested-name-specifier nominates a class C:
20420 * if the name specified after the nested-name-specifier, when
20421 looked up in C, is the injected-class-name of C (Clause 9), or
20422 * if the name specified after the nested-name-specifier is the
20423 same as the identifier or the simple-template-id's template-
20424 name in the last component of the nested-name-specifier,
20425 the name is instead considered to name the constructor of
20426 class C. [ Note: for example, the constructor is not an
20427 acceptable lookup result in an elaborated-type-specifier so
20428 the constructor would not be used in place of the
20429 injected-class-name. --end note ] Such a constructor name
20430 shall be used only in the declarator-id of a declaration that
20431 names a constructor or in a using-declaration. */
20432 if (tag_type == none_type
20433 && DECL_SELF_REFERENCE_P (decl)
20434 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20435 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20436 tag_type != none_type,
20437 /*complain=*/true);
20439 /* If we have a single function from a using decl, pull it out. */
20440 if (TREE_CODE (decl) == OVERLOAD
20441 && !really_overloaded_fn (decl))
20442 decl = OVL_FUNCTION (decl);
20444 if (pushed_scope)
20445 pop_scope (pushed_scope);
20448 /* If the scope is a dependent type and either we deferred lookup or
20449 we did lookup but didn't find the name, rememeber the name. */
20450 if (decl == error_mark_node && TYPE_P (parser->scope)
20451 && dependent_type_p (parser->scope))
20453 if (tag_type)
20455 tree type;
20457 /* The resolution to Core Issue 180 says that `struct
20458 A::B' should be considered a type-name, even if `A'
20459 is dependent. */
20460 type = make_typename_type (parser->scope, name, tag_type,
20461 /*complain=*/tf_error);
20462 decl = TYPE_NAME (type);
20464 else if (is_template
20465 && (cp_parser_next_token_ends_template_argument_p (parser)
20466 || cp_lexer_next_token_is (parser->lexer,
20467 CPP_CLOSE_PAREN)))
20468 decl = make_unbound_class_template (parser->scope,
20469 name, NULL_TREE,
20470 /*complain=*/tf_error);
20471 else
20472 decl = build_qualified_name (/*type=*/NULL_TREE,
20473 parser->scope, name,
20474 is_template);
20476 parser->qualifying_scope = parser->scope;
20477 parser->object_scope = NULL_TREE;
20479 else if (object_type)
20481 tree object_decl = NULL_TREE;
20482 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20483 OBJECT_TYPE is not a class. */
20484 if (CLASS_TYPE_P (object_type))
20485 /* If the OBJECT_TYPE is a template specialization, it may
20486 be instantiated during name lookup. In that case, errors
20487 may be issued. Even if we rollback the current tentative
20488 parse, those errors are valid. */
20489 object_decl = lookup_member (object_type,
20490 name,
20491 /*protect=*/0,
20492 tag_type != none_type,
20493 tf_warning_or_error);
20494 /* Look it up in the enclosing context, too. */
20495 decl = lookup_name_real (name, tag_type != none_type,
20496 /*nonclass=*/0,
20497 /*block_p=*/true, is_namespace, 0);
20498 parser->object_scope = object_type;
20499 parser->qualifying_scope = NULL_TREE;
20500 if (object_decl)
20501 decl = object_decl;
20503 else
20505 decl = lookup_name_real (name, tag_type != none_type,
20506 /*nonclass=*/0,
20507 /*block_p=*/true, is_namespace, 0);
20508 parser->qualifying_scope = NULL_TREE;
20509 parser->object_scope = NULL_TREE;
20512 /* If the lookup failed, let our caller know. */
20513 if (!decl || decl == error_mark_node)
20514 return error_mark_node;
20516 /* Pull out the template from an injected-class-name (or multiple). */
20517 if (is_template)
20518 decl = maybe_get_template_decl_from_type_decl (decl);
20520 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20521 if (TREE_CODE (decl) == TREE_LIST)
20523 if (ambiguous_decls)
20524 *ambiguous_decls = decl;
20525 /* The error message we have to print is too complicated for
20526 cp_parser_error, so we incorporate its actions directly. */
20527 if (!cp_parser_simulate_error (parser))
20529 error_at (name_location, "reference to %qD is ambiguous",
20530 name);
20531 print_candidates (decl);
20533 return error_mark_node;
20536 gcc_assert (DECL_P (decl)
20537 || TREE_CODE (decl) == OVERLOAD
20538 || TREE_CODE (decl) == SCOPE_REF
20539 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20540 || BASELINK_P (decl));
20542 /* If we have resolved the name of a member declaration, check to
20543 see if the declaration is accessible. When the name resolves to
20544 set of overloaded functions, accessibility is checked when
20545 overload resolution is done.
20547 During an explicit instantiation, access is not checked at all,
20548 as per [temp.explicit]. */
20549 if (DECL_P (decl))
20550 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20552 maybe_record_typedef_use (decl);
20554 return decl;
20557 /* Like cp_parser_lookup_name, but for use in the typical case where
20558 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20559 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20561 static tree
20562 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20564 return cp_parser_lookup_name (parser, name,
20565 none_type,
20566 /*is_template=*/false,
20567 /*is_namespace=*/false,
20568 /*check_dependency=*/true,
20569 /*ambiguous_decls=*/NULL,
20570 location);
20573 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20574 the current context, return the TYPE_DECL. If TAG_NAME_P is
20575 true, the DECL indicates the class being defined in a class-head,
20576 or declared in an elaborated-type-specifier.
20578 Otherwise, return DECL. */
20580 static tree
20581 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20583 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20584 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20586 struct A {
20587 template <typename T> struct B;
20590 template <typename T> struct A::B {};
20592 Similarly, in an elaborated-type-specifier:
20594 namespace N { struct X{}; }
20596 struct A {
20597 template <typename T> friend struct N::X;
20600 However, if the DECL refers to a class type, and we are in
20601 the scope of the class, then the name lookup automatically
20602 finds the TYPE_DECL created by build_self_reference rather
20603 than a TEMPLATE_DECL. For example, in:
20605 template <class T> struct S {
20606 S s;
20609 there is no need to handle such case. */
20611 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20612 return DECL_TEMPLATE_RESULT (decl);
20614 return decl;
20617 /* If too many, or too few, template-parameter lists apply to the
20618 declarator, issue an error message. Returns TRUE if all went well,
20619 and FALSE otherwise. */
20621 static bool
20622 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20623 cp_declarator *declarator,
20624 location_t declarator_location)
20626 unsigned num_templates;
20628 /* We haven't seen any classes that involve template parameters yet. */
20629 num_templates = 0;
20631 switch (declarator->kind)
20633 case cdk_id:
20634 if (declarator->u.id.qualifying_scope)
20636 tree scope;
20638 scope = declarator->u.id.qualifying_scope;
20640 while (scope && CLASS_TYPE_P (scope))
20642 /* You're supposed to have one `template <...>'
20643 for every template class, but you don't need one
20644 for a full specialization. For example:
20646 template <class T> struct S{};
20647 template <> struct S<int> { void f(); };
20648 void S<int>::f () {}
20650 is correct; there shouldn't be a `template <>' for
20651 the definition of `S<int>::f'. */
20652 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20653 /* If SCOPE does not have template information of any
20654 kind, then it is not a template, nor is it nested
20655 within a template. */
20656 break;
20657 if (explicit_class_specialization_p (scope))
20658 break;
20659 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20660 ++num_templates;
20662 scope = TYPE_CONTEXT (scope);
20665 else if (TREE_CODE (declarator->u.id.unqualified_name)
20666 == TEMPLATE_ID_EXPR)
20667 /* If the DECLARATOR has the form `X<y>' then it uses one
20668 additional level of template parameters. */
20669 ++num_templates;
20671 return cp_parser_check_template_parameters
20672 (parser, num_templates, declarator_location, declarator);
20675 case cdk_function:
20676 case cdk_array:
20677 case cdk_pointer:
20678 case cdk_reference:
20679 case cdk_ptrmem:
20680 return (cp_parser_check_declarator_template_parameters
20681 (parser, declarator->declarator, declarator_location));
20683 case cdk_error:
20684 return true;
20686 default:
20687 gcc_unreachable ();
20689 return false;
20692 /* NUM_TEMPLATES were used in the current declaration. If that is
20693 invalid, return FALSE and issue an error messages. Otherwise,
20694 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20695 declarator and we can print more accurate diagnostics. */
20697 static bool
20698 cp_parser_check_template_parameters (cp_parser* parser,
20699 unsigned num_templates,
20700 location_t location,
20701 cp_declarator *declarator)
20703 /* If there are the same number of template classes and parameter
20704 lists, that's OK. */
20705 if (parser->num_template_parameter_lists == num_templates)
20706 return true;
20707 /* If there are more, but only one more, then we are referring to a
20708 member template. That's OK too. */
20709 if (parser->num_template_parameter_lists == num_templates + 1)
20710 return true;
20711 /* If there are more template classes than parameter lists, we have
20712 something like:
20714 template <class T> void S<T>::R<T>::f (); */
20715 if (parser->num_template_parameter_lists < num_templates)
20717 if (declarator && !current_function_decl)
20718 error_at (location, "specializing member %<%T::%E%> "
20719 "requires %<template<>%> syntax",
20720 declarator->u.id.qualifying_scope,
20721 declarator->u.id.unqualified_name);
20722 else if (declarator)
20723 error_at (location, "invalid declaration of %<%T::%E%>",
20724 declarator->u.id.qualifying_scope,
20725 declarator->u.id.unqualified_name);
20726 else
20727 error_at (location, "too few template-parameter-lists");
20728 return false;
20730 /* Otherwise, there are too many template parameter lists. We have
20731 something like:
20733 template <class T> template <class U> void S::f(); */
20734 error_at (location, "too many template-parameter-lists");
20735 return false;
20738 /* Parse an optional `::' token indicating that the following name is
20739 from the global namespace. If so, PARSER->SCOPE is set to the
20740 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20741 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20742 Returns the new value of PARSER->SCOPE, if the `::' token is
20743 present, and NULL_TREE otherwise. */
20745 static tree
20746 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20748 cp_token *token;
20750 /* Peek at the next token. */
20751 token = cp_lexer_peek_token (parser->lexer);
20752 /* If we're looking at a `::' token then we're starting from the
20753 global namespace, not our current location. */
20754 if (token->type == CPP_SCOPE)
20756 /* Consume the `::' token. */
20757 cp_lexer_consume_token (parser->lexer);
20758 /* Set the SCOPE so that we know where to start the lookup. */
20759 parser->scope = global_namespace;
20760 parser->qualifying_scope = global_namespace;
20761 parser->object_scope = NULL_TREE;
20763 return parser->scope;
20765 else if (!current_scope_valid_p)
20767 parser->scope = NULL_TREE;
20768 parser->qualifying_scope = NULL_TREE;
20769 parser->object_scope = NULL_TREE;
20772 return NULL_TREE;
20775 /* Returns TRUE if the upcoming token sequence is the start of a
20776 constructor declarator. If FRIEND_P is true, the declarator is
20777 preceded by the `friend' specifier. */
20779 static bool
20780 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20782 bool constructor_p;
20783 tree nested_name_specifier;
20784 cp_token *next_token;
20786 /* The common case is that this is not a constructor declarator, so
20787 try to avoid doing lots of work if at all possible. It's not
20788 valid declare a constructor at function scope. */
20789 if (parser->in_function_body)
20790 return false;
20791 /* And only certain tokens can begin a constructor declarator. */
20792 next_token = cp_lexer_peek_token (parser->lexer);
20793 if (next_token->type != CPP_NAME
20794 && next_token->type != CPP_SCOPE
20795 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20796 && next_token->type != CPP_TEMPLATE_ID)
20797 return false;
20799 /* Parse tentatively; we are going to roll back all of the tokens
20800 consumed here. */
20801 cp_parser_parse_tentatively (parser);
20802 /* Assume that we are looking at a constructor declarator. */
20803 constructor_p = true;
20805 /* Look for the optional `::' operator. */
20806 cp_parser_global_scope_opt (parser,
20807 /*current_scope_valid_p=*/false);
20808 /* Look for the nested-name-specifier. */
20809 nested_name_specifier
20810 = (cp_parser_nested_name_specifier_opt (parser,
20811 /*typename_keyword_p=*/false,
20812 /*check_dependency_p=*/false,
20813 /*type_p=*/false,
20814 /*is_declaration=*/false));
20815 /* Outside of a class-specifier, there must be a
20816 nested-name-specifier. */
20817 if (!nested_name_specifier &&
20818 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20819 || friend_p))
20820 constructor_p = false;
20821 else if (nested_name_specifier == error_mark_node)
20822 constructor_p = false;
20824 /* If we have a class scope, this is easy; DR 147 says that S::S always
20825 names the constructor, and no other qualified name could. */
20826 if (constructor_p && nested_name_specifier
20827 && CLASS_TYPE_P (nested_name_specifier))
20829 tree id = cp_parser_unqualified_id (parser,
20830 /*template_keyword_p=*/false,
20831 /*check_dependency_p=*/false,
20832 /*declarator_p=*/true,
20833 /*optional_p=*/false);
20834 if (is_overloaded_fn (id))
20835 id = DECL_NAME (get_first_fn (id));
20836 if (!constructor_name_p (id, nested_name_specifier))
20837 constructor_p = false;
20839 /* If we still think that this might be a constructor-declarator,
20840 look for a class-name. */
20841 else if (constructor_p)
20843 /* If we have:
20845 template <typename T> struct S {
20846 S();
20849 we must recognize that the nested `S' names a class. */
20850 tree type_decl;
20851 type_decl = cp_parser_class_name (parser,
20852 /*typename_keyword_p=*/false,
20853 /*template_keyword_p=*/false,
20854 none_type,
20855 /*check_dependency_p=*/false,
20856 /*class_head_p=*/false,
20857 /*is_declaration=*/false);
20858 /* If there was no class-name, then this is not a constructor. */
20859 constructor_p = !cp_parser_error_occurred (parser);
20861 /* If we're still considering a constructor, we have to see a `(',
20862 to begin the parameter-declaration-clause, followed by either a
20863 `)', an `...', or a decl-specifier. We need to check for a
20864 type-specifier to avoid being fooled into thinking that:
20866 S (f) (int);
20868 is a constructor. (It is actually a function named `f' that
20869 takes one parameter (of type `int') and returns a value of type
20870 `S'. */
20871 if (constructor_p
20872 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20873 constructor_p = false;
20875 if (constructor_p
20876 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20877 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20878 /* A parameter declaration begins with a decl-specifier,
20879 which is either the "attribute" keyword, a storage class
20880 specifier, or (usually) a type-specifier. */
20881 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20883 tree type;
20884 tree pushed_scope = NULL_TREE;
20885 unsigned saved_num_template_parameter_lists;
20887 /* Names appearing in the type-specifier should be looked up
20888 in the scope of the class. */
20889 if (current_class_type)
20890 type = NULL_TREE;
20891 else
20893 type = TREE_TYPE (type_decl);
20894 if (TREE_CODE (type) == TYPENAME_TYPE)
20896 type = resolve_typename_type (type,
20897 /*only_current_p=*/false);
20898 if (TREE_CODE (type) == TYPENAME_TYPE)
20900 cp_parser_abort_tentative_parse (parser);
20901 return false;
20904 pushed_scope = push_scope (type);
20907 /* Inside the constructor parameter list, surrounding
20908 template-parameter-lists do not apply. */
20909 saved_num_template_parameter_lists
20910 = parser->num_template_parameter_lists;
20911 parser->num_template_parameter_lists = 0;
20913 /* Look for the type-specifier. */
20914 cp_parser_type_specifier (parser,
20915 CP_PARSER_FLAGS_NONE,
20916 /*decl_specs=*/NULL,
20917 /*is_declarator=*/true,
20918 /*declares_class_or_enum=*/NULL,
20919 /*is_cv_qualifier=*/NULL);
20921 parser->num_template_parameter_lists
20922 = saved_num_template_parameter_lists;
20924 /* Leave the scope of the class. */
20925 if (pushed_scope)
20926 pop_scope (pushed_scope);
20928 constructor_p = !cp_parser_error_occurred (parser);
20932 /* We did not really want to consume any tokens. */
20933 cp_parser_abort_tentative_parse (parser);
20935 return constructor_p;
20938 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20939 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20940 they must be performed once we are in the scope of the function.
20942 Returns the function defined. */
20944 static tree
20945 cp_parser_function_definition_from_specifiers_and_declarator
20946 (cp_parser* parser,
20947 cp_decl_specifier_seq *decl_specifiers,
20948 tree attributes,
20949 const cp_declarator *declarator)
20951 tree fn;
20952 bool success_p;
20954 /* Begin the function-definition. */
20955 success_p = start_function (decl_specifiers, declarator, attributes);
20957 /* The things we're about to see are not directly qualified by any
20958 template headers we've seen thus far. */
20959 reset_specialization ();
20961 /* If there were names looked up in the decl-specifier-seq that we
20962 did not check, check them now. We must wait until we are in the
20963 scope of the function to perform the checks, since the function
20964 might be a friend. */
20965 perform_deferred_access_checks ();
20967 if (!success_p)
20969 /* Skip the entire function. */
20970 cp_parser_skip_to_end_of_block_or_statement (parser);
20971 fn = error_mark_node;
20973 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20975 /* Seen already, skip it. An error message has already been output. */
20976 cp_parser_skip_to_end_of_block_or_statement (parser);
20977 fn = current_function_decl;
20978 current_function_decl = NULL_TREE;
20979 /* If this is a function from a class, pop the nested class. */
20980 if (current_class_name)
20981 pop_nested_class ();
20983 else
20985 timevar_id_t tv;
20986 if (DECL_DECLARED_INLINE_P (current_function_decl))
20987 tv = TV_PARSE_INLINE;
20988 else
20989 tv = TV_PARSE_FUNC;
20990 timevar_push (tv);
20991 fn = cp_parser_function_definition_after_declarator (parser,
20992 /*inline_p=*/false);
20993 timevar_pop (tv);
20996 return fn;
20999 /* Parse the part of a function-definition that follows the
21000 declarator. INLINE_P is TRUE iff this function is an inline
21001 function defined within a class-specifier.
21003 Returns the function defined. */
21005 static tree
21006 cp_parser_function_definition_after_declarator (cp_parser* parser,
21007 bool inline_p)
21009 tree fn;
21010 bool ctor_initializer_p = false;
21011 bool saved_in_unbraced_linkage_specification_p;
21012 bool saved_in_function_body;
21013 unsigned saved_num_template_parameter_lists;
21014 cp_token *token;
21016 saved_in_function_body = parser->in_function_body;
21017 parser->in_function_body = true;
21018 /* If the next token is `return', then the code may be trying to
21019 make use of the "named return value" extension that G++ used to
21020 support. */
21021 token = cp_lexer_peek_token (parser->lexer);
21022 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21024 /* Consume the `return' keyword. */
21025 cp_lexer_consume_token (parser->lexer);
21026 /* Look for the identifier that indicates what value is to be
21027 returned. */
21028 cp_parser_identifier (parser);
21029 /* Issue an error message. */
21030 error_at (token->location,
21031 "named return values are no longer supported");
21032 /* Skip tokens until we reach the start of the function body. */
21033 while (true)
21035 cp_token *token = cp_lexer_peek_token (parser->lexer);
21036 if (token->type == CPP_OPEN_BRACE
21037 || token->type == CPP_EOF
21038 || token->type == CPP_PRAGMA_EOL)
21039 break;
21040 cp_lexer_consume_token (parser->lexer);
21043 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21044 anything declared inside `f'. */
21045 saved_in_unbraced_linkage_specification_p
21046 = parser->in_unbraced_linkage_specification_p;
21047 parser->in_unbraced_linkage_specification_p = false;
21048 /* Inside the function, surrounding template-parameter-lists do not
21049 apply. */
21050 saved_num_template_parameter_lists
21051 = parser->num_template_parameter_lists;
21052 parser->num_template_parameter_lists = 0;
21054 start_lambda_scope (current_function_decl);
21056 /* If the next token is `try', `__transaction_atomic', or
21057 `__transaction_relaxed`, then we are looking at either function-try-block
21058 or function-transaction-block. Note that all of these include the
21059 function-body. */
21060 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21061 ctor_initializer_p = cp_parser_function_transaction (parser,
21062 RID_TRANSACTION_ATOMIC);
21063 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21064 RID_TRANSACTION_RELAXED))
21065 ctor_initializer_p = cp_parser_function_transaction (parser,
21066 RID_TRANSACTION_RELAXED);
21067 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21068 ctor_initializer_p = cp_parser_function_try_block (parser);
21069 else
21070 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21071 (parser, /*in_function_try_block=*/false);
21073 finish_lambda_scope ();
21075 /* Finish the function. */
21076 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21077 (inline_p ? 2 : 0));
21078 /* Generate code for it, if necessary. */
21079 expand_or_defer_fn (fn);
21080 /* Restore the saved values. */
21081 parser->in_unbraced_linkage_specification_p
21082 = saved_in_unbraced_linkage_specification_p;
21083 parser->num_template_parameter_lists
21084 = saved_num_template_parameter_lists;
21085 parser->in_function_body = saved_in_function_body;
21087 return fn;
21090 /* Parse a template-declaration, assuming that the `export' (and
21091 `extern') keywords, if present, has already been scanned. MEMBER_P
21092 is as for cp_parser_template_declaration. */
21094 static void
21095 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21097 tree decl = NULL_TREE;
21098 VEC (deferred_access_check,gc) *checks;
21099 tree parameter_list;
21100 bool friend_p = false;
21101 bool need_lang_pop;
21102 cp_token *token;
21104 /* Look for the `template' keyword. */
21105 token = cp_lexer_peek_token (parser->lexer);
21106 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21107 return;
21109 /* And the `<'. */
21110 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21111 return;
21112 if (at_class_scope_p () && current_function_decl)
21114 /* 14.5.2.2 [temp.mem]
21116 A local class shall not have member templates. */
21117 error_at (token->location,
21118 "invalid declaration of member template in local class");
21119 cp_parser_skip_to_end_of_block_or_statement (parser);
21120 return;
21122 /* [temp]
21124 A template ... shall not have C linkage. */
21125 if (current_lang_name == lang_name_c)
21127 error_at (token->location, "template with C linkage");
21128 /* Give it C++ linkage to avoid confusing other parts of the
21129 front end. */
21130 push_lang_context (lang_name_cplusplus);
21131 need_lang_pop = true;
21133 else
21134 need_lang_pop = false;
21136 /* We cannot perform access checks on the template parameter
21137 declarations until we know what is being declared, just as we
21138 cannot check the decl-specifier list. */
21139 push_deferring_access_checks (dk_deferred);
21141 /* If the next token is `>', then we have an invalid
21142 specialization. Rather than complain about an invalid template
21143 parameter, issue an error message here. */
21144 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21146 cp_parser_error (parser, "invalid explicit specialization");
21147 begin_specialization ();
21148 parameter_list = NULL_TREE;
21150 else
21152 /* Parse the template parameters. */
21153 parameter_list = cp_parser_template_parameter_list (parser);
21154 fixup_template_parms ();
21157 /* Get the deferred access checks from the parameter list. These
21158 will be checked once we know what is being declared, as for a
21159 member template the checks must be performed in the scope of the
21160 class containing the member. */
21161 checks = get_deferred_access_checks ();
21163 /* Look for the `>'. */
21164 cp_parser_skip_to_end_of_template_parameter_list (parser);
21165 /* We just processed one more parameter list. */
21166 ++parser->num_template_parameter_lists;
21167 /* If the next token is `template', there are more template
21168 parameters. */
21169 if (cp_lexer_next_token_is_keyword (parser->lexer,
21170 RID_TEMPLATE))
21171 cp_parser_template_declaration_after_export (parser, member_p);
21172 else if (cxx_dialect >= cxx0x
21173 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21174 decl = cp_parser_alias_declaration (parser);
21175 else
21177 /* There are no access checks when parsing a template, as we do not
21178 know if a specialization will be a friend. */
21179 push_deferring_access_checks (dk_no_check);
21180 token = cp_lexer_peek_token (parser->lexer);
21181 decl = cp_parser_single_declaration (parser,
21182 checks,
21183 member_p,
21184 /*explicit_specialization_p=*/false,
21185 &friend_p);
21186 pop_deferring_access_checks ();
21188 /* If this is a member template declaration, let the front
21189 end know. */
21190 if (member_p && !friend_p && decl)
21192 if (TREE_CODE (decl) == TYPE_DECL)
21193 cp_parser_check_access_in_redeclaration (decl, token->location);
21195 decl = finish_member_template_decl (decl);
21197 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21198 make_friend_class (current_class_type, TREE_TYPE (decl),
21199 /*complain=*/true);
21201 /* We are done with the current parameter list. */
21202 --parser->num_template_parameter_lists;
21204 pop_deferring_access_checks ();
21206 /* Finish up. */
21207 finish_template_decl (parameter_list);
21209 /* Check the template arguments for a literal operator template. */
21210 if (decl
21211 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21212 && UDLIT_OPER_P (DECL_NAME (decl)))
21214 bool ok = true;
21215 if (parameter_list == NULL_TREE)
21216 ok = false;
21217 else
21219 int num_parms = TREE_VEC_LENGTH (parameter_list);
21220 if (num_parms != 1)
21221 ok = false;
21222 else
21224 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21225 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21226 if (TREE_TYPE (parm) != char_type_node
21227 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21228 ok = false;
21231 if (!ok)
21232 error ("literal operator template %qD has invalid parameter list."
21233 " Expected non-type template argument pack <char...>",
21234 decl);
21236 /* Register member declarations. */
21237 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21238 finish_member_declaration (decl);
21239 /* For the erroneous case of a template with C linkage, we pushed an
21240 implicit C++ linkage scope; exit that scope now. */
21241 if (need_lang_pop)
21242 pop_lang_context ();
21243 /* If DECL is a function template, we must return to parse it later.
21244 (Even though there is no definition, there might be default
21245 arguments that need handling.) */
21246 if (member_p && decl
21247 && (TREE_CODE (decl) == FUNCTION_DECL
21248 || DECL_FUNCTION_TEMPLATE_P (decl)))
21249 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21252 /* Perform the deferred access checks from a template-parameter-list.
21253 CHECKS is a TREE_LIST of access checks, as returned by
21254 get_deferred_access_checks. */
21256 static void
21257 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21259 ++processing_template_parmlist;
21260 perform_access_checks (checks);
21261 --processing_template_parmlist;
21264 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21265 `function-definition' sequence. MEMBER_P is true, this declaration
21266 appears in a class scope.
21268 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21269 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21271 static tree
21272 cp_parser_single_declaration (cp_parser* parser,
21273 VEC (deferred_access_check,gc)* checks,
21274 bool member_p,
21275 bool explicit_specialization_p,
21276 bool* friend_p)
21278 int declares_class_or_enum;
21279 tree decl = NULL_TREE;
21280 cp_decl_specifier_seq decl_specifiers;
21281 bool function_definition_p = false;
21282 cp_token *decl_spec_token_start;
21284 /* This function is only used when processing a template
21285 declaration. */
21286 gcc_assert (innermost_scope_kind () == sk_template_parms
21287 || innermost_scope_kind () == sk_template_spec);
21289 /* Defer access checks until we know what is being declared. */
21290 push_deferring_access_checks (dk_deferred);
21292 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21293 alternative. */
21294 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21295 cp_parser_decl_specifier_seq (parser,
21296 CP_PARSER_FLAGS_OPTIONAL,
21297 &decl_specifiers,
21298 &declares_class_or_enum);
21299 if (friend_p)
21300 *friend_p = cp_parser_friend_p (&decl_specifiers);
21302 /* There are no template typedefs. */
21303 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21305 error_at (decl_spec_token_start->location,
21306 "template declaration of %<typedef%>");
21307 decl = error_mark_node;
21310 /* Gather up the access checks that occurred the
21311 decl-specifier-seq. */
21312 stop_deferring_access_checks ();
21314 /* Check for the declaration of a template class. */
21315 if (declares_class_or_enum)
21317 if (cp_parser_declares_only_class_p (parser))
21319 decl = shadow_tag (&decl_specifiers);
21321 /* In this case:
21323 struct C {
21324 friend template <typename T> struct A<T>::B;
21327 A<T>::B will be represented by a TYPENAME_TYPE, and
21328 therefore not recognized by shadow_tag. */
21329 if (friend_p && *friend_p
21330 && !decl
21331 && decl_specifiers.type
21332 && TYPE_P (decl_specifiers.type))
21333 decl = decl_specifiers.type;
21335 if (decl && decl != error_mark_node)
21336 decl = TYPE_NAME (decl);
21337 else
21338 decl = error_mark_node;
21340 /* Perform access checks for template parameters. */
21341 cp_parser_perform_template_parameter_access_checks (checks);
21345 /* Complain about missing 'typename' or other invalid type names. */
21346 if (!decl_specifiers.any_type_specifiers_p
21347 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21349 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21350 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21351 the rest of this declaration. */
21352 decl = error_mark_node;
21353 goto out;
21356 /* If it's not a template class, try for a template function. If
21357 the next token is a `;', then this declaration does not declare
21358 anything. But, if there were errors in the decl-specifiers, then
21359 the error might well have come from an attempted class-specifier.
21360 In that case, there's no need to warn about a missing declarator. */
21361 if (!decl
21362 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21363 || decl_specifiers.type != error_mark_node))
21365 decl = cp_parser_init_declarator (parser,
21366 &decl_specifiers,
21367 checks,
21368 /*function_definition_allowed_p=*/true,
21369 member_p,
21370 declares_class_or_enum,
21371 &function_definition_p,
21372 NULL);
21374 /* 7.1.1-1 [dcl.stc]
21376 A storage-class-specifier shall not be specified in an explicit
21377 specialization... */
21378 if (decl
21379 && explicit_specialization_p
21380 && decl_specifiers.storage_class != sc_none)
21382 error_at (decl_spec_token_start->location,
21383 "explicit template specialization cannot have a storage class");
21384 decl = error_mark_node;
21388 /* Look for a trailing `;' after the declaration. */
21389 if (!function_definition_p
21390 && (decl == error_mark_node
21391 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21392 cp_parser_skip_to_end_of_block_or_statement (parser);
21394 out:
21395 pop_deferring_access_checks ();
21397 /* Clear any current qualification; whatever comes next is the start
21398 of something new. */
21399 parser->scope = NULL_TREE;
21400 parser->qualifying_scope = NULL_TREE;
21401 parser->object_scope = NULL_TREE;
21403 return decl;
21406 /* Parse a cast-expression that is not the operand of a unary "&". */
21408 static tree
21409 cp_parser_simple_cast_expression (cp_parser *parser)
21411 return cp_parser_cast_expression (parser, /*address_p=*/false,
21412 /*cast_p=*/false, NULL);
21415 /* Parse a functional cast to TYPE. Returns an expression
21416 representing the cast. */
21418 static tree
21419 cp_parser_functional_cast (cp_parser* parser, tree type)
21421 VEC(tree,gc) *vec;
21422 tree expression_list;
21423 tree cast;
21424 bool nonconst_p;
21426 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21429 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21430 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21431 if (TREE_CODE (type) == TYPE_DECL)
21432 type = TREE_TYPE (type);
21433 return finish_compound_literal (type, expression_list,
21434 tf_warning_or_error);
21438 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21439 /*cast_p=*/true,
21440 /*allow_expansion_p=*/true,
21441 /*non_constant_p=*/NULL);
21442 if (vec == NULL)
21443 expression_list = error_mark_node;
21444 else
21446 expression_list = build_tree_list_vec (vec);
21447 release_tree_vector (vec);
21450 cast = build_functional_cast (type, expression_list,
21451 tf_warning_or_error);
21452 /* [expr.const]/1: In an integral constant expression "only type
21453 conversions to integral or enumeration type can be used". */
21454 if (TREE_CODE (type) == TYPE_DECL)
21455 type = TREE_TYPE (type);
21456 if (cast != error_mark_node
21457 && !cast_valid_in_integral_constant_expression_p (type)
21458 && cp_parser_non_integral_constant_expression (parser,
21459 NIC_CONSTRUCTOR))
21460 return error_mark_node;
21461 return cast;
21464 /* Save the tokens that make up the body of a member function defined
21465 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21466 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21467 specifiers applied to the declaration. Returns the FUNCTION_DECL
21468 for the member function. */
21470 static tree
21471 cp_parser_save_member_function_body (cp_parser* parser,
21472 cp_decl_specifier_seq *decl_specifiers,
21473 cp_declarator *declarator,
21474 tree attributes)
21476 cp_token *first;
21477 cp_token *last;
21478 tree fn;
21480 /* Create the FUNCTION_DECL. */
21481 fn = grokmethod (decl_specifiers, declarator, attributes);
21482 /* If something went badly wrong, bail out now. */
21483 if (fn == error_mark_node)
21485 /* If there's a function-body, skip it. */
21486 if (cp_parser_token_starts_function_definition_p
21487 (cp_lexer_peek_token (parser->lexer)))
21488 cp_parser_skip_to_end_of_block_or_statement (parser);
21489 return error_mark_node;
21492 /* Remember it, if there default args to post process. */
21493 cp_parser_save_default_args (parser, fn);
21495 /* Save away the tokens that make up the body of the
21496 function. */
21497 first = parser->lexer->next_token;
21498 /* We can have braced-init-list mem-initializers before the fn body. */
21499 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21501 cp_lexer_consume_token (parser->lexer);
21502 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21503 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21505 /* cache_group will stop after an un-nested { } pair, too. */
21506 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21507 break;
21509 /* variadic mem-inits have ... after the ')'. */
21510 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21511 cp_lexer_consume_token (parser->lexer);
21514 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21515 /* Handle function try blocks. */
21516 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21517 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21518 last = parser->lexer->next_token;
21520 /* Save away the inline definition; we will process it when the
21521 class is complete. */
21522 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21523 DECL_PENDING_INLINE_P (fn) = 1;
21525 /* We need to know that this was defined in the class, so that
21526 friend templates are handled correctly. */
21527 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21529 /* Add FN to the queue of functions to be parsed later. */
21530 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21532 return fn;
21535 /* Save the tokens that make up the in-class initializer for a non-static
21536 data member. Returns a DEFAULT_ARG. */
21538 static tree
21539 cp_parser_save_nsdmi (cp_parser* parser)
21541 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21544 /* Parse a template-argument-list, as well as the trailing ">" (but
21545 not the opening "<"). See cp_parser_template_argument_list for the
21546 return value. */
21548 static tree
21549 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21551 tree arguments;
21552 tree saved_scope;
21553 tree saved_qualifying_scope;
21554 tree saved_object_scope;
21555 bool saved_greater_than_is_operator_p;
21556 int saved_unevaluated_operand;
21557 int saved_inhibit_evaluation_warnings;
21559 /* [temp.names]
21561 When parsing a template-id, the first non-nested `>' is taken as
21562 the end of the template-argument-list rather than a greater-than
21563 operator. */
21564 saved_greater_than_is_operator_p
21565 = parser->greater_than_is_operator_p;
21566 parser->greater_than_is_operator_p = false;
21567 /* Parsing the argument list may modify SCOPE, so we save it
21568 here. */
21569 saved_scope = parser->scope;
21570 saved_qualifying_scope = parser->qualifying_scope;
21571 saved_object_scope = parser->object_scope;
21572 /* We need to evaluate the template arguments, even though this
21573 template-id may be nested within a "sizeof". */
21574 saved_unevaluated_operand = cp_unevaluated_operand;
21575 cp_unevaluated_operand = 0;
21576 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21577 c_inhibit_evaluation_warnings = 0;
21578 /* Parse the template-argument-list itself. */
21579 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21580 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21581 arguments = NULL_TREE;
21582 else
21583 arguments = cp_parser_template_argument_list (parser);
21584 /* Look for the `>' that ends the template-argument-list. If we find
21585 a '>>' instead, it's probably just a typo. */
21586 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21588 if (cxx_dialect != cxx98)
21590 /* In C++0x, a `>>' in a template argument list or cast
21591 expression is considered to be two separate `>'
21592 tokens. So, change the current token to a `>', but don't
21593 consume it: it will be consumed later when the outer
21594 template argument list (or cast expression) is parsed.
21595 Note that this replacement of `>' for `>>' is necessary
21596 even if we are parsing tentatively: in the tentative
21597 case, after calling
21598 cp_parser_enclosed_template_argument_list we will always
21599 throw away all of the template arguments and the first
21600 closing `>', either because the template argument list
21601 was erroneous or because we are replacing those tokens
21602 with a CPP_TEMPLATE_ID token. The second `>' (which will
21603 not have been thrown away) is needed either to close an
21604 outer template argument list or to complete a new-style
21605 cast. */
21606 cp_token *token = cp_lexer_peek_token (parser->lexer);
21607 token->type = CPP_GREATER;
21609 else if (!saved_greater_than_is_operator_p)
21611 /* If we're in a nested template argument list, the '>>' has
21612 to be a typo for '> >'. We emit the error message, but we
21613 continue parsing and we push a '>' as next token, so that
21614 the argument list will be parsed correctly. Note that the
21615 global source location is still on the token before the
21616 '>>', so we need to say explicitly where we want it. */
21617 cp_token *token = cp_lexer_peek_token (parser->lexer);
21618 error_at (token->location, "%<>>%> should be %<> >%> "
21619 "within a nested template argument list");
21621 token->type = CPP_GREATER;
21623 else
21625 /* If this is not a nested template argument list, the '>>'
21626 is a typo for '>'. Emit an error message and continue.
21627 Same deal about the token location, but here we can get it
21628 right by consuming the '>>' before issuing the diagnostic. */
21629 cp_token *token = cp_lexer_consume_token (parser->lexer);
21630 error_at (token->location,
21631 "spurious %<>>%>, use %<>%> to terminate "
21632 "a template argument list");
21635 else
21636 cp_parser_skip_to_end_of_template_parameter_list (parser);
21637 /* The `>' token might be a greater-than operator again now. */
21638 parser->greater_than_is_operator_p
21639 = saved_greater_than_is_operator_p;
21640 /* Restore the SAVED_SCOPE. */
21641 parser->scope = saved_scope;
21642 parser->qualifying_scope = saved_qualifying_scope;
21643 parser->object_scope = saved_object_scope;
21644 cp_unevaluated_operand = saved_unevaluated_operand;
21645 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21647 return arguments;
21650 /* MEMBER_FUNCTION is a member function, or a friend. If default
21651 arguments, or the body of the function have not yet been parsed,
21652 parse them now. */
21654 static void
21655 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21657 timevar_push (TV_PARSE_INMETH);
21658 /* If this member is a template, get the underlying
21659 FUNCTION_DECL. */
21660 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21661 member_function = DECL_TEMPLATE_RESULT (member_function);
21663 /* There should not be any class definitions in progress at this
21664 point; the bodies of members are only parsed outside of all class
21665 definitions. */
21666 gcc_assert (parser->num_classes_being_defined == 0);
21667 /* While we're parsing the member functions we might encounter more
21668 classes. We want to handle them right away, but we don't want
21669 them getting mixed up with functions that are currently in the
21670 queue. */
21671 push_unparsed_function_queues (parser);
21673 /* Make sure that any template parameters are in scope. */
21674 maybe_begin_member_template_processing (member_function);
21676 /* If the body of the function has not yet been parsed, parse it
21677 now. */
21678 if (DECL_PENDING_INLINE_P (member_function))
21680 tree function_scope;
21681 cp_token_cache *tokens;
21683 /* The function is no longer pending; we are processing it. */
21684 tokens = DECL_PENDING_INLINE_INFO (member_function);
21685 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21686 DECL_PENDING_INLINE_P (member_function) = 0;
21688 /* If this is a local class, enter the scope of the containing
21689 function. */
21690 function_scope = current_function_decl;
21691 if (function_scope)
21692 push_function_context ();
21694 /* Push the body of the function onto the lexer stack. */
21695 cp_parser_push_lexer_for_tokens (parser, tokens);
21697 /* Let the front end know that we going to be defining this
21698 function. */
21699 start_preparsed_function (member_function, NULL_TREE,
21700 SF_PRE_PARSED | SF_INCLASS_INLINE);
21702 /* Don't do access checking if it is a templated function. */
21703 if (processing_template_decl)
21704 push_deferring_access_checks (dk_no_check);
21706 /* Now, parse the body of the function. */
21707 cp_parser_function_definition_after_declarator (parser,
21708 /*inline_p=*/true);
21710 if (processing_template_decl)
21711 pop_deferring_access_checks ();
21713 /* Leave the scope of the containing function. */
21714 if (function_scope)
21715 pop_function_context ();
21716 cp_parser_pop_lexer (parser);
21719 /* Remove any template parameters from the symbol table. */
21720 maybe_end_member_template_processing ();
21722 /* Restore the queue. */
21723 pop_unparsed_function_queues (parser);
21724 timevar_pop (TV_PARSE_INMETH);
21727 /* If DECL contains any default args, remember it on the unparsed
21728 functions queue. */
21730 static void
21731 cp_parser_save_default_args (cp_parser* parser, tree decl)
21733 tree probe;
21735 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21736 probe;
21737 probe = TREE_CHAIN (probe))
21738 if (TREE_PURPOSE (probe))
21740 cp_default_arg_entry *entry
21741 = VEC_safe_push (cp_default_arg_entry, gc,
21742 unparsed_funs_with_default_args, NULL);
21743 entry->class_type = current_class_type;
21744 entry->decl = decl;
21745 break;
21749 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21750 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21751 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21752 from the parameter-type-list. */
21754 static tree
21755 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21756 tree default_arg, tree parmtype)
21758 cp_token_cache *tokens;
21759 tree parsed_arg;
21760 bool dummy;
21762 if (default_arg == error_mark_node)
21763 return error_mark_node;
21765 /* Push the saved tokens for the default argument onto the parser's
21766 lexer stack. */
21767 tokens = DEFARG_TOKENS (default_arg);
21768 cp_parser_push_lexer_for_tokens (parser, tokens);
21770 start_lambda_scope (decl);
21772 /* Parse the default argument. */
21773 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21774 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21775 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21777 finish_lambda_scope ();
21779 if (parsed_arg == error_mark_node)
21780 cp_parser_skip_to_end_of_statement (parser);
21782 if (!processing_template_decl)
21784 /* In a non-template class, check conversions now. In a template,
21785 we'll wait and instantiate these as needed. */
21786 if (TREE_CODE (decl) == PARM_DECL)
21787 parsed_arg = check_default_argument (parmtype, parsed_arg);
21788 else
21790 int flags = LOOKUP_IMPLICIT;
21791 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21792 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21793 flags = LOOKUP_NORMAL;
21794 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21798 /* If the token stream has not been completely used up, then
21799 there was extra junk after the end of the default
21800 argument. */
21801 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21803 if (TREE_CODE (decl) == PARM_DECL)
21804 cp_parser_error (parser, "expected %<,%>");
21805 else
21806 cp_parser_error (parser, "expected %<;%>");
21809 /* Revert to the main lexer. */
21810 cp_parser_pop_lexer (parser);
21812 return parsed_arg;
21815 /* FIELD is a non-static data member with an initializer which we saved for
21816 later; parse it now. */
21818 static void
21819 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21821 tree def;
21823 push_unparsed_function_queues (parser);
21824 def = cp_parser_late_parse_one_default_arg (parser, field,
21825 DECL_INITIAL (field),
21826 NULL_TREE);
21827 pop_unparsed_function_queues (parser);
21829 DECL_INITIAL (field) = def;
21832 /* FN is a FUNCTION_DECL which may contains a parameter with an
21833 unparsed DEFAULT_ARG. Parse the default args now. This function
21834 assumes that the current scope is the scope in which the default
21835 argument should be processed. */
21837 static void
21838 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21840 bool saved_local_variables_forbidden_p;
21841 tree parm, parmdecl;
21843 /* While we're parsing the default args, we might (due to the
21844 statement expression extension) encounter more classes. We want
21845 to handle them right away, but we don't want them getting mixed
21846 up with default args that are currently in the queue. */
21847 push_unparsed_function_queues (parser);
21849 /* Local variable names (and the `this' keyword) may not appear
21850 in a default argument. */
21851 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21852 parser->local_variables_forbidden_p = true;
21854 push_defarg_context (fn);
21856 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21857 parmdecl = DECL_ARGUMENTS (fn);
21858 parm && parm != void_list_node;
21859 parm = TREE_CHAIN (parm),
21860 parmdecl = DECL_CHAIN (parmdecl))
21862 tree default_arg = TREE_PURPOSE (parm);
21863 tree parsed_arg;
21864 VEC(tree,gc) *insts;
21865 tree copy;
21866 unsigned ix;
21868 if (!default_arg)
21869 continue;
21871 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21872 /* This can happen for a friend declaration for a function
21873 already declared with default arguments. */
21874 continue;
21876 parsed_arg
21877 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21878 default_arg,
21879 TREE_VALUE (parm));
21880 if (parsed_arg == error_mark_node)
21882 continue;
21885 TREE_PURPOSE (parm) = parsed_arg;
21887 /* Update any instantiations we've already created. */
21888 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21889 VEC_iterate (tree, insts, ix, copy); ix++)
21890 TREE_PURPOSE (copy) = parsed_arg;
21893 pop_defarg_context ();
21895 /* Make sure no default arg is missing. */
21896 check_default_args (fn);
21898 /* Restore the state of local_variables_forbidden_p. */
21899 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21901 /* Restore the queue. */
21902 pop_unparsed_function_queues (parser);
21905 /* Parse the operand of `sizeof' (or a similar operator). Returns
21906 either a TYPE or an expression, depending on the form of the
21907 input. The KEYWORD indicates which kind of expression we have
21908 encountered. */
21910 static tree
21911 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21913 tree expr = NULL_TREE;
21914 const char *saved_message;
21915 char *tmp;
21916 bool saved_integral_constant_expression_p;
21917 bool saved_non_integral_constant_expression_p;
21918 bool pack_expansion_p = false;
21920 /* Types cannot be defined in a `sizeof' expression. Save away the
21921 old message. */
21922 saved_message = parser->type_definition_forbidden_message;
21923 /* And create the new one. */
21924 tmp = concat ("types may not be defined in %<",
21925 IDENTIFIER_POINTER (ridpointers[keyword]),
21926 "%> expressions", NULL);
21927 parser->type_definition_forbidden_message = tmp;
21929 /* The restrictions on constant-expressions do not apply inside
21930 sizeof expressions. */
21931 saved_integral_constant_expression_p
21932 = parser->integral_constant_expression_p;
21933 saved_non_integral_constant_expression_p
21934 = parser->non_integral_constant_expression_p;
21935 parser->integral_constant_expression_p = false;
21937 /* If it's a `...', then we are computing the length of a parameter
21938 pack. */
21939 if (keyword == RID_SIZEOF
21940 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21942 /* Consume the `...'. */
21943 cp_lexer_consume_token (parser->lexer);
21944 maybe_warn_variadic_templates ();
21946 /* Note that this is an expansion. */
21947 pack_expansion_p = true;
21950 /* Do not actually evaluate the expression. */
21951 ++cp_unevaluated_operand;
21952 ++c_inhibit_evaluation_warnings;
21953 /* If it's a `(', then we might be looking at the type-id
21954 construction. */
21955 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21957 tree type;
21958 bool saved_in_type_id_in_expr_p;
21960 /* We can't be sure yet whether we're looking at a type-id or an
21961 expression. */
21962 cp_parser_parse_tentatively (parser);
21963 /* Consume the `('. */
21964 cp_lexer_consume_token (parser->lexer);
21965 /* Parse the type-id. */
21966 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21967 parser->in_type_id_in_expr_p = true;
21968 type = cp_parser_type_id (parser);
21969 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21970 /* Now, look for the trailing `)'. */
21971 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21972 /* If all went well, then we're done. */
21973 if (cp_parser_parse_definitely (parser))
21975 cp_decl_specifier_seq decl_specs;
21977 /* Build a trivial decl-specifier-seq. */
21978 clear_decl_specs (&decl_specs);
21979 decl_specs.type = type;
21981 /* Call grokdeclarator to figure out what type this is. */
21982 expr = grokdeclarator (NULL,
21983 &decl_specs,
21984 TYPENAME,
21985 /*initialized=*/0,
21986 /*attrlist=*/NULL);
21989 else if (pack_expansion_p)
21990 permerror (cp_lexer_peek_token (parser->lexer)->location,
21991 "%<sizeof...%> argument must be surrounded by parentheses");
21993 /* If the type-id production did not work out, then we must be
21994 looking at the unary-expression production. */
21995 if (!expr)
21996 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21997 /*cast_p=*/false, NULL);
21999 if (pack_expansion_p)
22000 /* Build a pack expansion. */
22001 expr = make_pack_expansion (expr);
22003 /* Go back to evaluating expressions. */
22004 --cp_unevaluated_operand;
22005 --c_inhibit_evaluation_warnings;
22007 /* Free the message we created. */
22008 free (tmp);
22009 /* And restore the old one. */
22010 parser->type_definition_forbidden_message = saved_message;
22011 parser->integral_constant_expression_p
22012 = saved_integral_constant_expression_p;
22013 parser->non_integral_constant_expression_p
22014 = saved_non_integral_constant_expression_p;
22016 return expr;
22019 /* If the current declaration has no declarator, return true. */
22021 static bool
22022 cp_parser_declares_only_class_p (cp_parser *parser)
22024 /* If the next token is a `;' or a `,' then there is no
22025 declarator. */
22026 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22027 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22030 /* Update the DECL_SPECS to reflect the storage class indicated by
22031 KEYWORD. */
22033 static void
22034 cp_parser_set_storage_class (cp_parser *parser,
22035 cp_decl_specifier_seq *decl_specs,
22036 enum rid keyword,
22037 location_t location)
22039 cp_storage_class storage_class;
22041 if (parser->in_unbraced_linkage_specification_p)
22043 error_at (location, "invalid use of %qD in linkage specification",
22044 ridpointers[keyword]);
22045 return;
22047 else if (decl_specs->storage_class != sc_none)
22049 decl_specs->conflicting_specifiers_p = true;
22050 return;
22053 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22054 && decl_spec_seq_has_spec_p (decl_specs, ds_thread))
22056 error_at (decl_specs->locations[ds_thread],
22057 "%<__thread%> before %qD", ridpointers[keyword]);
22058 decl_specs->locations[ds_thread] = 0;
22061 switch (keyword)
22063 case RID_AUTO:
22064 storage_class = sc_auto;
22065 break;
22066 case RID_REGISTER:
22067 storage_class = sc_register;
22068 break;
22069 case RID_STATIC:
22070 storage_class = sc_static;
22071 break;
22072 case RID_EXTERN:
22073 storage_class = sc_extern;
22074 break;
22075 case RID_MUTABLE:
22076 storage_class = sc_mutable;
22077 break;
22078 default:
22079 gcc_unreachable ();
22081 decl_specs->storage_class = storage_class;
22082 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, location);
22084 /* A storage class specifier cannot be applied alongside a typedef
22085 specifier. If there is a typedef specifier present then set
22086 conflicting_specifiers_p which will trigger an error later
22087 on in grokdeclarator. */
22088 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22089 decl_specs->conflicting_specifiers_p = true;
22092 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22093 is true, the type is a class or enum definition. */
22095 static void
22096 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22097 tree type_spec,
22098 location_t location,
22099 bool type_definition_p)
22101 decl_specs->any_specifiers_p = true;
22103 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22104 (with, for example, in "typedef int wchar_t;") we remember that
22105 this is what happened. In system headers, we ignore these
22106 declarations so that G++ can work with system headers that are not
22107 C++-safe. */
22108 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22109 && !type_definition_p
22110 && (type_spec == boolean_type_node
22111 || type_spec == char16_type_node
22112 || type_spec == char32_type_node
22113 || type_spec == wchar_type_node)
22114 && (decl_specs->type
22115 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22116 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22117 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22118 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22120 decl_specs->redefined_builtin_type = type_spec;
22121 set_and_check_decl_spec_loc (decl_specs,
22122 ds_redefined_builtin_type_spec,
22123 location);
22124 if (!decl_specs->type)
22126 decl_specs->type = type_spec;
22127 decl_specs->type_definition_p = false;
22128 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, location);
22131 else if (decl_specs->type)
22132 decl_specs->multiple_types_p = true;
22133 else
22135 decl_specs->type = type_spec;
22136 decl_specs->type_definition_p = type_definition_p;
22137 decl_specs->redefined_builtin_type = NULL_TREE;
22138 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, location);
22142 /* Set the location for a declarator specifier and check if it is
22143 duplicated.
22145 DECL_SPECS is the sequence of declarator specifiers onto which to
22146 set the location.
22148 DS is the single declarator specifier to set which location is to
22149 be set onto the existing sequence of declarators.
22151 LOCATION is the location for the declarator specifier to
22152 consider. */
22154 static void
22155 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22156 cp_decl_spec ds, source_location location)
22158 gcc_assert (ds < ds_last);
22160 if (decl_specs == NULL)
22161 return;
22163 if (decl_specs->locations[ds] == 0)
22164 decl_specs->locations[ds] = location;
22165 else
22167 if (ds == ds_long)
22169 if (decl_specs->locations[ds_long_long] != 0)
22170 error_at (location,
22171 "%<long long long%> is too long for GCC");
22172 else
22174 decl_specs->locations[ds_long_long] = location;
22175 pedwarn_cxx98 (location,
22176 OPT_Wlong_long,
22177 "ISO C++ 1998 does not support %<long long%>");
22180 else
22182 static const char *const decl_spec_names[] = {
22183 "signed",
22184 "unsigned",
22185 "short",
22186 "long",
22187 "const",
22188 "volatile",
22189 "restrict",
22190 "inline",
22191 "virtual",
22192 "explicit",
22193 "friend",
22194 "typedef",
22195 "using",
22196 "constexpr",
22197 "__complex",
22198 "__thread"
22200 error_at (location,
22201 "duplicate %qs", decl_spec_names[ds]);
22206 /* Return true iff the declarator specifier DS is present in the
22207 sequence of declarator specifiers DECL_SPECS. */
22209 bool
22210 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22211 cp_decl_spec ds)
22213 gcc_assert (ds < ds_last);
22215 if (decl_specs == NULL)
22216 return false;
22218 return decl_specs->locations[ds] != 0;
22221 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22222 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22224 static bool
22225 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22227 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22230 /* Issue an error message indicating that TOKEN_DESC was expected.
22231 If KEYWORD is true, it indicated this function is called by
22232 cp_parser_require_keword and the required token can only be
22233 a indicated keyword. */
22235 static void
22236 cp_parser_required_error (cp_parser *parser,
22237 required_token token_desc,
22238 bool keyword)
22240 switch (token_desc)
22242 case RT_NEW:
22243 cp_parser_error (parser, "expected %<new%>");
22244 return;
22245 case RT_DELETE:
22246 cp_parser_error (parser, "expected %<delete%>");
22247 return;
22248 case RT_RETURN:
22249 cp_parser_error (parser, "expected %<return%>");
22250 return;
22251 case RT_WHILE:
22252 cp_parser_error (parser, "expected %<while%>");
22253 return;
22254 case RT_EXTERN:
22255 cp_parser_error (parser, "expected %<extern%>");
22256 return;
22257 case RT_STATIC_ASSERT:
22258 cp_parser_error (parser, "expected %<static_assert%>");
22259 return;
22260 case RT_DECLTYPE:
22261 cp_parser_error (parser, "expected %<decltype%>");
22262 return;
22263 case RT_OPERATOR:
22264 cp_parser_error (parser, "expected %<operator%>");
22265 return;
22266 case RT_CLASS:
22267 cp_parser_error (parser, "expected %<class%>");
22268 return;
22269 case RT_TEMPLATE:
22270 cp_parser_error (parser, "expected %<template%>");
22271 return;
22272 case RT_NAMESPACE:
22273 cp_parser_error (parser, "expected %<namespace%>");
22274 return;
22275 case RT_USING:
22276 cp_parser_error (parser, "expected %<using%>");
22277 return;
22278 case RT_ASM:
22279 cp_parser_error (parser, "expected %<asm%>");
22280 return;
22281 case RT_TRY:
22282 cp_parser_error (parser, "expected %<try%>");
22283 return;
22284 case RT_CATCH:
22285 cp_parser_error (parser, "expected %<catch%>");
22286 return;
22287 case RT_THROW:
22288 cp_parser_error (parser, "expected %<throw%>");
22289 return;
22290 case RT_LABEL:
22291 cp_parser_error (parser, "expected %<__label__%>");
22292 return;
22293 case RT_AT_TRY:
22294 cp_parser_error (parser, "expected %<@try%>");
22295 return;
22296 case RT_AT_SYNCHRONIZED:
22297 cp_parser_error (parser, "expected %<@synchronized%>");
22298 return;
22299 case RT_AT_THROW:
22300 cp_parser_error (parser, "expected %<@throw%>");
22301 return;
22302 case RT_TRANSACTION_ATOMIC:
22303 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22304 return;
22305 case RT_TRANSACTION_RELAXED:
22306 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22307 return;
22308 default:
22309 break;
22311 if (!keyword)
22313 switch (token_desc)
22315 case RT_SEMICOLON:
22316 cp_parser_error (parser, "expected %<;%>");
22317 return;
22318 case RT_OPEN_PAREN:
22319 cp_parser_error (parser, "expected %<(%>");
22320 return;
22321 case RT_CLOSE_BRACE:
22322 cp_parser_error (parser, "expected %<}%>");
22323 return;
22324 case RT_OPEN_BRACE:
22325 cp_parser_error (parser, "expected %<{%>");
22326 return;
22327 case RT_CLOSE_SQUARE:
22328 cp_parser_error (parser, "expected %<]%>");
22329 return;
22330 case RT_OPEN_SQUARE:
22331 cp_parser_error (parser, "expected %<[%>");
22332 return;
22333 case RT_COMMA:
22334 cp_parser_error (parser, "expected %<,%>");
22335 return;
22336 case RT_SCOPE:
22337 cp_parser_error (parser, "expected %<::%>");
22338 return;
22339 case RT_LESS:
22340 cp_parser_error (parser, "expected %<<%>");
22341 return;
22342 case RT_GREATER:
22343 cp_parser_error (parser, "expected %<>%>");
22344 return;
22345 case RT_EQ:
22346 cp_parser_error (parser, "expected %<=%>");
22347 return;
22348 case RT_ELLIPSIS:
22349 cp_parser_error (parser, "expected %<...%>");
22350 return;
22351 case RT_MULT:
22352 cp_parser_error (parser, "expected %<*%>");
22353 return;
22354 case RT_COMPL:
22355 cp_parser_error (parser, "expected %<~%>");
22356 return;
22357 case RT_COLON:
22358 cp_parser_error (parser, "expected %<:%>");
22359 return;
22360 case RT_COLON_SCOPE:
22361 cp_parser_error (parser, "expected %<:%> or %<::%>");
22362 return;
22363 case RT_CLOSE_PAREN:
22364 cp_parser_error (parser, "expected %<)%>");
22365 return;
22366 case RT_COMMA_CLOSE_PAREN:
22367 cp_parser_error (parser, "expected %<,%> or %<)%>");
22368 return;
22369 case RT_PRAGMA_EOL:
22370 cp_parser_error (parser, "expected end of line");
22371 return;
22372 case RT_NAME:
22373 cp_parser_error (parser, "expected identifier");
22374 return;
22375 case RT_SELECT:
22376 cp_parser_error (parser, "expected selection-statement");
22377 return;
22378 case RT_INTERATION:
22379 cp_parser_error (parser, "expected iteration-statement");
22380 return;
22381 case RT_JUMP:
22382 cp_parser_error (parser, "expected jump-statement");
22383 return;
22384 case RT_CLASS_KEY:
22385 cp_parser_error (parser, "expected class-key");
22386 return;
22387 case RT_CLASS_TYPENAME_TEMPLATE:
22388 cp_parser_error (parser,
22389 "expected %<class%>, %<typename%>, or %<template%>");
22390 return;
22391 default:
22392 gcc_unreachable ();
22395 else
22396 gcc_unreachable ();
22401 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22402 issue an error message indicating that TOKEN_DESC was expected.
22404 Returns the token consumed, if the token had the appropriate type.
22405 Otherwise, returns NULL. */
22407 static cp_token *
22408 cp_parser_require (cp_parser* parser,
22409 enum cpp_ttype type,
22410 required_token token_desc)
22412 if (cp_lexer_next_token_is (parser->lexer, type))
22413 return cp_lexer_consume_token (parser->lexer);
22414 else
22416 /* Output the MESSAGE -- unless we're parsing tentatively. */
22417 if (!cp_parser_simulate_error (parser))
22418 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22419 return NULL;
22423 /* An error message is produced if the next token is not '>'.
22424 All further tokens are skipped until the desired token is
22425 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22427 static void
22428 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22430 /* Current level of '< ... >'. */
22431 unsigned level = 0;
22432 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22433 unsigned nesting_depth = 0;
22435 /* Are we ready, yet? If not, issue error message. */
22436 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22437 return;
22439 /* Skip tokens until the desired token is found. */
22440 while (true)
22442 /* Peek at the next token. */
22443 switch (cp_lexer_peek_token (parser->lexer)->type)
22445 case CPP_LESS:
22446 if (!nesting_depth)
22447 ++level;
22448 break;
22450 case CPP_RSHIFT:
22451 if (cxx_dialect == cxx98)
22452 /* C++0x views the `>>' operator as two `>' tokens, but
22453 C++98 does not. */
22454 break;
22455 else if (!nesting_depth && level-- == 0)
22457 /* We've hit a `>>' where the first `>' closes the
22458 template argument list, and the second `>' is
22459 spurious. Just consume the `>>' and stop; we've
22460 already produced at least one error. */
22461 cp_lexer_consume_token (parser->lexer);
22462 return;
22464 /* Fall through for C++0x, so we handle the second `>' in
22465 the `>>'. */
22467 case CPP_GREATER:
22468 if (!nesting_depth && level-- == 0)
22470 /* We've reached the token we want, consume it and stop. */
22471 cp_lexer_consume_token (parser->lexer);
22472 return;
22474 break;
22476 case CPP_OPEN_PAREN:
22477 case CPP_OPEN_SQUARE:
22478 ++nesting_depth;
22479 break;
22481 case CPP_CLOSE_PAREN:
22482 case CPP_CLOSE_SQUARE:
22483 if (nesting_depth-- == 0)
22484 return;
22485 break;
22487 case CPP_EOF:
22488 case CPP_PRAGMA_EOL:
22489 case CPP_SEMICOLON:
22490 case CPP_OPEN_BRACE:
22491 case CPP_CLOSE_BRACE:
22492 /* The '>' was probably forgotten, don't look further. */
22493 return;
22495 default:
22496 break;
22499 /* Consume this token. */
22500 cp_lexer_consume_token (parser->lexer);
22504 /* If the next token is the indicated keyword, consume it. Otherwise,
22505 issue an error message indicating that TOKEN_DESC was expected.
22507 Returns the token consumed, if the token had the appropriate type.
22508 Otherwise, returns NULL. */
22510 static cp_token *
22511 cp_parser_require_keyword (cp_parser* parser,
22512 enum rid keyword,
22513 required_token token_desc)
22515 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22517 if (token && token->keyword != keyword)
22519 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22520 return NULL;
22523 return token;
22526 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22527 function-definition. */
22529 static bool
22530 cp_parser_token_starts_function_definition_p (cp_token* token)
22532 return (/* An ordinary function-body begins with an `{'. */
22533 token->type == CPP_OPEN_BRACE
22534 /* A ctor-initializer begins with a `:'. */
22535 || token->type == CPP_COLON
22536 /* A function-try-block begins with `try'. */
22537 || token->keyword == RID_TRY
22538 /* A function-transaction-block begins with `__transaction_atomic'
22539 or `__transaction_relaxed'. */
22540 || token->keyword == RID_TRANSACTION_ATOMIC
22541 || token->keyword == RID_TRANSACTION_RELAXED
22542 /* The named return value extension begins with `return'. */
22543 || token->keyword == RID_RETURN);
22546 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22547 definition. */
22549 static bool
22550 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22552 cp_token *token;
22554 token = cp_lexer_peek_token (parser->lexer);
22555 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22558 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22559 C++0x) ending a template-argument. */
22561 static bool
22562 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22564 cp_token *token;
22566 token = cp_lexer_peek_token (parser->lexer);
22567 return (token->type == CPP_COMMA
22568 || token->type == CPP_GREATER
22569 || token->type == CPP_ELLIPSIS
22570 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22573 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22574 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22576 static bool
22577 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22578 size_t n)
22580 cp_token *token;
22582 token = cp_lexer_peek_nth_token (parser->lexer, n);
22583 if (token->type == CPP_LESS)
22584 return true;
22585 /* Check for the sequence `<::' in the original code. It would be lexed as
22586 `[:', where `[' is a digraph, and there is no whitespace before
22587 `:'. */
22588 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22590 cp_token *token2;
22591 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22592 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22593 return true;
22595 return false;
22598 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22599 or none_type otherwise. */
22601 static enum tag_types
22602 cp_parser_token_is_class_key (cp_token* token)
22604 switch (token->keyword)
22606 case RID_CLASS:
22607 return class_type;
22608 case RID_STRUCT:
22609 return record_type;
22610 case RID_UNION:
22611 return union_type;
22613 default:
22614 return none_type;
22618 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22620 static void
22621 cp_parser_check_class_key (enum tag_types class_key, tree type)
22623 if (type == error_mark_node)
22624 return;
22625 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22627 permerror (input_location, "%qs tag used in naming %q#T",
22628 class_key == union_type ? "union"
22629 : class_key == record_type ? "struct" : "class",
22630 type);
22631 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22632 "%q#T was previously declared here", type);
22636 /* Issue an error message if DECL is redeclared with different
22637 access than its original declaration [class.access.spec/3].
22638 This applies to nested classes and nested class templates.
22639 [class.mem/1]. */
22641 static void
22642 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22644 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22645 return;
22647 if ((TREE_PRIVATE (decl)
22648 != (current_access_specifier == access_private_node))
22649 || (TREE_PROTECTED (decl)
22650 != (current_access_specifier == access_protected_node)))
22651 error_at (location, "%qD redeclared with different access", decl);
22654 /* Look for the `template' keyword, as a syntactic disambiguator.
22655 Return TRUE iff it is present, in which case it will be
22656 consumed. */
22658 static bool
22659 cp_parser_optional_template_keyword (cp_parser *parser)
22661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22663 /* The `template' keyword can only be used within templates;
22664 outside templates the parser can always figure out what is a
22665 template and what is not. */
22666 if (!processing_template_decl)
22668 cp_token *token = cp_lexer_peek_token (parser->lexer);
22669 error_at (token->location,
22670 "%<template%> (as a disambiguator) is only allowed "
22671 "within templates");
22672 /* If this part of the token stream is rescanned, the same
22673 error message would be generated. So, we purge the token
22674 from the stream. */
22675 cp_lexer_purge_token (parser->lexer);
22676 return false;
22678 else
22680 /* Consume the `template' keyword. */
22681 cp_lexer_consume_token (parser->lexer);
22682 return true;
22686 return false;
22689 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22690 set PARSER->SCOPE, and perform other related actions. */
22692 static void
22693 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22695 int i;
22696 struct tree_check *check_value;
22697 deferred_access_check *chk;
22698 VEC (deferred_access_check,gc) *checks;
22700 /* Get the stored value. */
22701 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22702 /* Perform any access checks that were deferred. */
22703 checks = check_value->checks;
22704 if (checks)
22706 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22707 perform_or_defer_access_check (chk->binfo,
22708 chk->decl,
22709 chk->diag_decl);
22711 /* Set the scope from the stored value. */
22712 parser->scope = check_value->value;
22713 parser->qualifying_scope = check_value->qualifying_scope;
22714 parser->object_scope = NULL_TREE;
22717 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22718 encounter the end of a block before what we were looking for. */
22720 static bool
22721 cp_parser_cache_group (cp_parser *parser,
22722 enum cpp_ttype end,
22723 unsigned depth)
22725 while (true)
22727 cp_token *token = cp_lexer_peek_token (parser->lexer);
22729 /* Abort a parenthesized expression if we encounter a semicolon. */
22730 if ((end == CPP_CLOSE_PAREN || depth == 0)
22731 && token->type == CPP_SEMICOLON)
22732 return true;
22733 /* If we've reached the end of the file, stop. */
22734 if (token->type == CPP_EOF
22735 || (end != CPP_PRAGMA_EOL
22736 && token->type == CPP_PRAGMA_EOL))
22737 return true;
22738 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22739 /* We've hit the end of an enclosing block, so there's been some
22740 kind of syntax error. */
22741 return true;
22743 /* Consume the token. */
22744 cp_lexer_consume_token (parser->lexer);
22745 /* See if it starts a new group. */
22746 if (token->type == CPP_OPEN_BRACE)
22748 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22749 /* In theory this should probably check end == '}', but
22750 cp_parser_save_member_function_body needs it to exit
22751 after either '}' or ')' when called with ')'. */
22752 if (depth == 0)
22753 return false;
22755 else if (token->type == CPP_OPEN_PAREN)
22757 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22758 if (depth == 0 && end == CPP_CLOSE_PAREN)
22759 return false;
22761 else if (token->type == CPP_PRAGMA)
22762 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22763 else if (token->type == end)
22764 return false;
22768 /* Like above, for caching a default argument or NSDMI. Both of these are
22769 terminated by a non-nested comma, but it can be unclear whether or not a
22770 comma is nested in a template argument list unless we do more parsing.
22771 In order to handle this ambiguity, when we encounter a ',' after a '<'
22772 we try to parse what follows as a parameter-declaration-list (in the
22773 case of a default argument) or a member-declarator (in the case of an
22774 NSDMI). If that succeeds, then we stop caching. */
22776 static tree
22777 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22779 unsigned depth = 0;
22780 int maybe_template_id = 0;
22781 cp_token *first_token;
22782 cp_token *token;
22783 tree default_argument;
22785 /* Add tokens until we have processed the entire default
22786 argument. We add the range [first_token, token). */
22787 first_token = cp_lexer_peek_token (parser->lexer);
22788 if (first_token->type == CPP_OPEN_BRACE)
22790 /* For list-initialization, this is straightforward. */
22791 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22792 token = cp_lexer_peek_token (parser->lexer);
22794 else while (true)
22796 bool done = false;
22798 /* Peek at the next token. */
22799 token = cp_lexer_peek_token (parser->lexer);
22800 /* What we do depends on what token we have. */
22801 switch (token->type)
22803 /* In valid code, a default argument must be
22804 immediately followed by a `,' `)', or `...'. */
22805 case CPP_COMMA:
22806 if (depth == 0 && maybe_template_id)
22808 /* If we've seen a '<', we might be in a
22809 template-argument-list. Until Core issue 325 is
22810 resolved, we don't know how this situation ought
22811 to be handled, so try to DTRT. We check whether
22812 what comes after the comma is a valid parameter
22813 declaration list. If it is, then the comma ends
22814 the default argument; otherwise the default
22815 argument continues. */
22816 bool error = false;
22817 tree t;
22819 /* Set ITALP so cp_parser_parameter_declaration_list
22820 doesn't decide to commit to this parse. */
22821 bool saved_italp = parser->in_template_argument_list_p;
22822 parser->in_template_argument_list_p = true;
22824 cp_parser_parse_tentatively (parser);
22825 cp_lexer_consume_token (parser->lexer);
22827 if (nsdmi)
22829 int ctor_dtor_or_conv_p;
22830 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22831 &ctor_dtor_or_conv_p,
22832 /*parenthesized_p=*/NULL,
22833 /*member_p=*/true);
22835 else
22837 begin_scope (sk_function_parms, NULL_TREE);
22838 cp_parser_parameter_declaration_list (parser, &error);
22839 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22840 pop_binding (DECL_NAME (t), t);
22841 leave_scope ();
22843 if (!cp_parser_error_occurred (parser) && !error)
22844 done = true;
22845 cp_parser_abort_tentative_parse (parser);
22847 parser->in_template_argument_list_p = saved_italp;
22848 break;
22850 case CPP_CLOSE_PAREN:
22851 case CPP_ELLIPSIS:
22852 /* If we run into a non-nested `;', `}', or `]',
22853 then the code is invalid -- but the default
22854 argument is certainly over. */
22855 case CPP_SEMICOLON:
22856 case CPP_CLOSE_BRACE:
22857 case CPP_CLOSE_SQUARE:
22858 if (depth == 0)
22859 done = true;
22860 /* Update DEPTH, if necessary. */
22861 else if (token->type == CPP_CLOSE_PAREN
22862 || token->type == CPP_CLOSE_BRACE
22863 || token->type == CPP_CLOSE_SQUARE)
22864 --depth;
22865 break;
22867 case CPP_OPEN_PAREN:
22868 case CPP_OPEN_SQUARE:
22869 case CPP_OPEN_BRACE:
22870 ++depth;
22871 break;
22873 case CPP_LESS:
22874 if (depth == 0)
22875 /* This might be the comparison operator, or it might
22876 start a template argument list. */
22877 ++maybe_template_id;
22878 break;
22880 case CPP_RSHIFT:
22881 if (cxx_dialect == cxx98)
22882 break;
22883 /* Fall through for C++0x, which treats the `>>'
22884 operator like two `>' tokens in certain
22885 cases. */
22887 case CPP_GREATER:
22888 if (depth == 0)
22890 /* This might be an operator, or it might close a
22891 template argument list. But if a previous '<'
22892 started a template argument list, this will have
22893 closed it, so we can't be in one anymore. */
22894 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22895 if (maybe_template_id < 0)
22896 maybe_template_id = 0;
22898 break;
22900 /* If we run out of tokens, issue an error message. */
22901 case CPP_EOF:
22902 case CPP_PRAGMA_EOL:
22903 error_at (token->location, "file ends in default argument");
22904 done = true;
22905 break;
22907 case CPP_NAME:
22908 case CPP_SCOPE:
22909 /* In these cases, we should look for template-ids.
22910 For example, if the default argument is
22911 `X<int, double>()', we need to do name lookup to
22912 figure out whether or not `X' is a template; if
22913 so, the `,' does not end the default argument.
22915 That is not yet done. */
22916 break;
22918 default:
22919 break;
22922 /* If we've reached the end, stop. */
22923 if (done)
22924 break;
22926 /* Add the token to the token block. */
22927 token = cp_lexer_consume_token (parser->lexer);
22930 /* Create a DEFAULT_ARG to represent the unparsed default
22931 argument. */
22932 default_argument = make_node (DEFAULT_ARG);
22933 DEFARG_TOKENS (default_argument)
22934 = cp_token_cache_new (first_token, token);
22935 DEFARG_INSTANTIATIONS (default_argument) = NULL;
22937 return default_argument;
22940 /* Begin parsing tentatively. We always save tokens while parsing
22941 tentatively so that if the tentative parsing fails we can restore the
22942 tokens. */
22944 static void
22945 cp_parser_parse_tentatively (cp_parser* parser)
22947 /* Enter a new parsing context. */
22948 parser->context = cp_parser_context_new (parser->context);
22949 /* Begin saving tokens. */
22950 cp_lexer_save_tokens (parser->lexer);
22951 /* In order to avoid repetitive access control error messages,
22952 access checks are queued up until we are no longer parsing
22953 tentatively. */
22954 push_deferring_access_checks (dk_deferred);
22957 /* Commit to the currently active tentative parse. */
22959 static void
22960 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22962 cp_parser_context *context;
22963 cp_lexer *lexer;
22965 /* Mark all of the levels as committed. */
22966 lexer = parser->lexer;
22967 for (context = parser->context; context->next; context = context->next)
22969 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22970 break;
22971 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22972 while (!cp_lexer_saving_tokens (lexer))
22973 lexer = lexer->next;
22974 cp_lexer_commit_tokens (lexer);
22978 /* Abort the currently active tentative parse. All consumed tokens
22979 will be rolled back, and no diagnostics will be issued. */
22981 static void
22982 cp_parser_abort_tentative_parse (cp_parser* parser)
22984 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22985 || errorcount > 0);
22986 cp_parser_simulate_error (parser);
22987 /* Now, pretend that we want to see if the construct was
22988 successfully parsed. */
22989 cp_parser_parse_definitely (parser);
22992 /* Stop parsing tentatively. If a parse error has occurred, restore the
22993 token stream. Otherwise, commit to the tokens we have consumed.
22994 Returns true if no error occurred; false otherwise. */
22996 static bool
22997 cp_parser_parse_definitely (cp_parser* parser)
22999 bool error_occurred;
23000 cp_parser_context *context;
23002 /* Remember whether or not an error occurred, since we are about to
23003 destroy that information. */
23004 error_occurred = cp_parser_error_occurred (parser);
23005 /* Remove the topmost context from the stack. */
23006 context = parser->context;
23007 parser->context = context->next;
23008 /* If no parse errors occurred, commit to the tentative parse. */
23009 if (!error_occurred)
23011 /* Commit to the tokens read tentatively, unless that was
23012 already done. */
23013 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23014 cp_lexer_commit_tokens (parser->lexer);
23016 pop_to_parent_deferring_access_checks ();
23018 /* Otherwise, if errors occurred, roll back our state so that things
23019 are just as they were before we began the tentative parse. */
23020 else
23022 cp_lexer_rollback_tokens (parser->lexer);
23023 pop_deferring_access_checks ();
23025 /* Add the context to the front of the free list. */
23026 context->next = cp_parser_context_free_list;
23027 cp_parser_context_free_list = context;
23029 return !error_occurred;
23032 /* Returns true if we are parsing tentatively and are not committed to
23033 this tentative parse. */
23035 static bool
23036 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23038 return (cp_parser_parsing_tentatively (parser)
23039 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23042 /* Returns nonzero iff an error has occurred during the most recent
23043 tentative parse. */
23045 static bool
23046 cp_parser_error_occurred (cp_parser* parser)
23048 return (cp_parser_parsing_tentatively (parser)
23049 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23052 /* Returns nonzero if GNU extensions are allowed. */
23054 static bool
23055 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23057 return parser->allow_gnu_extensions_p;
23060 /* Objective-C++ Productions */
23063 /* Parse an Objective-C expression, which feeds into a primary-expression
23064 above.
23066 objc-expression:
23067 objc-message-expression
23068 objc-string-literal
23069 objc-encode-expression
23070 objc-protocol-expression
23071 objc-selector-expression
23073 Returns a tree representation of the expression. */
23075 static tree
23076 cp_parser_objc_expression (cp_parser* parser)
23078 /* Try to figure out what kind of declaration is present. */
23079 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23081 switch (kwd->type)
23083 case CPP_OPEN_SQUARE:
23084 return cp_parser_objc_message_expression (parser);
23086 case CPP_OBJC_STRING:
23087 kwd = cp_lexer_consume_token (parser->lexer);
23088 return objc_build_string_object (kwd->u.value);
23090 case CPP_KEYWORD:
23091 switch (kwd->keyword)
23093 case RID_AT_ENCODE:
23094 return cp_parser_objc_encode_expression (parser);
23096 case RID_AT_PROTOCOL:
23097 return cp_parser_objc_protocol_expression (parser);
23099 case RID_AT_SELECTOR:
23100 return cp_parser_objc_selector_expression (parser);
23102 default:
23103 break;
23105 default:
23106 error_at (kwd->location,
23107 "misplaced %<@%D%> Objective-C++ construct",
23108 kwd->u.value);
23109 cp_parser_skip_to_end_of_block_or_statement (parser);
23112 return error_mark_node;
23115 /* Parse an Objective-C message expression.
23117 objc-message-expression:
23118 [ objc-message-receiver objc-message-args ]
23120 Returns a representation of an Objective-C message. */
23122 static tree
23123 cp_parser_objc_message_expression (cp_parser* parser)
23125 tree receiver, messageargs;
23127 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23128 receiver = cp_parser_objc_message_receiver (parser);
23129 messageargs = cp_parser_objc_message_args (parser);
23130 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23132 return objc_build_message_expr (receiver, messageargs);
23135 /* Parse an objc-message-receiver.
23137 objc-message-receiver:
23138 expression
23139 simple-type-specifier
23141 Returns a representation of the type or expression. */
23143 static tree
23144 cp_parser_objc_message_receiver (cp_parser* parser)
23146 tree rcv;
23148 /* An Objective-C message receiver may be either (1) a type
23149 or (2) an expression. */
23150 cp_parser_parse_tentatively (parser);
23151 rcv = cp_parser_expression (parser, false, NULL);
23153 if (cp_parser_parse_definitely (parser))
23154 return rcv;
23156 rcv = cp_parser_simple_type_specifier (parser,
23157 /*decl_specs=*/NULL,
23158 CP_PARSER_FLAGS_NONE);
23160 return objc_get_class_reference (rcv);
23163 /* Parse the arguments and selectors comprising an Objective-C message.
23165 objc-message-args:
23166 objc-selector
23167 objc-selector-args
23168 objc-selector-args , objc-comma-args
23170 objc-selector-args:
23171 objc-selector [opt] : assignment-expression
23172 objc-selector-args objc-selector [opt] : assignment-expression
23174 objc-comma-args:
23175 assignment-expression
23176 objc-comma-args , assignment-expression
23178 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23179 selector arguments and TREE_VALUE containing a list of comma
23180 arguments. */
23182 static tree
23183 cp_parser_objc_message_args (cp_parser* parser)
23185 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23186 bool maybe_unary_selector_p = true;
23187 cp_token *token = cp_lexer_peek_token (parser->lexer);
23189 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23191 tree selector = NULL_TREE, arg;
23193 if (token->type != CPP_COLON)
23194 selector = cp_parser_objc_selector (parser);
23196 /* Detect if we have a unary selector. */
23197 if (maybe_unary_selector_p
23198 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23199 return build_tree_list (selector, NULL_TREE);
23201 maybe_unary_selector_p = false;
23202 cp_parser_require (parser, CPP_COLON, RT_COLON);
23203 arg = cp_parser_assignment_expression (parser, false, NULL);
23205 sel_args
23206 = chainon (sel_args,
23207 build_tree_list (selector, arg));
23209 token = cp_lexer_peek_token (parser->lexer);
23212 /* Handle non-selector arguments, if any. */
23213 while (token->type == CPP_COMMA)
23215 tree arg;
23217 cp_lexer_consume_token (parser->lexer);
23218 arg = cp_parser_assignment_expression (parser, false, NULL);
23220 addl_args
23221 = chainon (addl_args,
23222 build_tree_list (NULL_TREE, arg));
23224 token = cp_lexer_peek_token (parser->lexer);
23227 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23229 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23230 return build_tree_list (error_mark_node, error_mark_node);
23233 return build_tree_list (sel_args, addl_args);
23236 /* Parse an Objective-C encode expression.
23238 objc-encode-expression:
23239 @encode objc-typename
23241 Returns an encoded representation of the type argument. */
23243 static tree
23244 cp_parser_objc_encode_expression (cp_parser* parser)
23246 tree type;
23247 cp_token *token;
23249 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23250 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23251 token = cp_lexer_peek_token (parser->lexer);
23252 type = complete_type (cp_parser_type_id (parser));
23253 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23255 if (!type)
23257 error_at (token->location,
23258 "%<@encode%> must specify a type as an argument");
23259 return error_mark_node;
23262 /* This happens if we find @encode(T) (where T is a template
23263 typename or something dependent on a template typename) when
23264 parsing a template. In that case, we can't compile it
23265 immediately, but we rather create an AT_ENCODE_EXPR which will
23266 need to be instantiated when the template is used.
23268 if (dependent_type_p (type))
23270 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23271 TREE_READONLY (value) = 1;
23272 return value;
23275 return objc_build_encode_expr (type);
23278 /* Parse an Objective-C @defs expression. */
23280 static tree
23281 cp_parser_objc_defs_expression (cp_parser *parser)
23283 tree name;
23285 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23286 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23287 name = cp_parser_identifier (parser);
23288 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23290 return objc_get_class_ivars (name);
23293 /* Parse an Objective-C protocol expression.
23295 objc-protocol-expression:
23296 @protocol ( identifier )
23298 Returns a representation of the protocol expression. */
23300 static tree
23301 cp_parser_objc_protocol_expression (cp_parser* parser)
23303 tree proto;
23305 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23306 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23307 proto = cp_parser_identifier (parser);
23308 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23310 return objc_build_protocol_expr (proto);
23313 /* Parse an Objective-C selector expression.
23315 objc-selector-expression:
23316 @selector ( objc-method-signature )
23318 objc-method-signature:
23319 objc-selector
23320 objc-selector-seq
23322 objc-selector-seq:
23323 objc-selector :
23324 objc-selector-seq objc-selector :
23326 Returns a representation of the method selector. */
23328 static tree
23329 cp_parser_objc_selector_expression (cp_parser* parser)
23331 tree sel_seq = NULL_TREE;
23332 bool maybe_unary_selector_p = true;
23333 cp_token *token;
23334 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23336 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23337 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23338 token = cp_lexer_peek_token (parser->lexer);
23340 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23341 || token->type == CPP_SCOPE)
23343 tree selector = NULL_TREE;
23345 if (token->type != CPP_COLON
23346 || token->type == CPP_SCOPE)
23347 selector = cp_parser_objc_selector (parser);
23349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23350 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23352 /* Detect if we have a unary selector. */
23353 if (maybe_unary_selector_p)
23355 sel_seq = selector;
23356 goto finish_selector;
23358 else
23360 cp_parser_error (parser, "expected %<:%>");
23363 maybe_unary_selector_p = false;
23364 token = cp_lexer_consume_token (parser->lexer);
23366 if (token->type == CPP_SCOPE)
23368 sel_seq
23369 = chainon (sel_seq,
23370 build_tree_list (selector, NULL_TREE));
23371 sel_seq
23372 = chainon (sel_seq,
23373 build_tree_list (NULL_TREE, NULL_TREE));
23375 else
23376 sel_seq
23377 = chainon (sel_seq,
23378 build_tree_list (selector, NULL_TREE));
23380 token = cp_lexer_peek_token (parser->lexer);
23383 finish_selector:
23384 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23386 return objc_build_selector_expr (loc, sel_seq);
23389 /* Parse a list of identifiers.
23391 objc-identifier-list:
23392 identifier
23393 objc-identifier-list , identifier
23395 Returns a TREE_LIST of identifier nodes. */
23397 static tree
23398 cp_parser_objc_identifier_list (cp_parser* parser)
23400 tree identifier;
23401 tree list;
23402 cp_token *sep;
23404 identifier = cp_parser_identifier (parser);
23405 if (identifier == error_mark_node)
23406 return error_mark_node;
23408 list = build_tree_list (NULL_TREE, identifier);
23409 sep = cp_lexer_peek_token (parser->lexer);
23411 while (sep->type == CPP_COMMA)
23413 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23414 identifier = cp_parser_identifier (parser);
23415 if (identifier == error_mark_node)
23416 return list;
23418 list = chainon (list, build_tree_list (NULL_TREE,
23419 identifier));
23420 sep = cp_lexer_peek_token (parser->lexer);
23423 return list;
23426 /* Parse an Objective-C alias declaration.
23428 objc-alias-declaration:
23429 @compatibility_alias identifier identifier ;
23431 This function registers the alias mapping with the Objective-C front end.
23432 It returns nothing. */
23434 static void
23435 cp_parser_objc_alias_declaration (cp_parser* parser)
23437 tree alias, orig;
23439 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23440 alias = cp_parser_identifier (parser);
23441 orig = cp_parser_identifier (parser);
23442 objc_declare_alias (alias, orig);
23443 cp_parser_consume_semicolon_at_end_of_statement (parser);
23446 /* Parse an Objective-C class forward-declaration.
23448 objc-class-declaration:
23449 @class objc-identifier-list ;
23451 The function registers the forward declarations with the Objective-C
23452 front end. It returns nothing. */
23454 static void
23455 cp_parser_objc_class_declaration (cp_parser* parser)
23457 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23458 while (true)
23460 tree id;
23462 id = cp_parser_identifier (parser);
23463 if (id == error_mark_node)
23464 break;
23466 objc_declare_class (id);
23468 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23469 cp_lexer_consume_token (parser->lexer);
23470 else
23471 break;
23473 cp_parser_consume_semicolon_at_end_of_statement (parser);
23476 /* Parse a list of Objective-C protocol references.
23478 objc-protocol-refs-opt:
23479 objc-protocol-refs [opt]
23481 objc-protocol-refs:
23482 < objc-identifier-list >
23484 Returns a TREE_LIST of identifiers, if any. */
23486 static tree
23487 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23489 tree protorefs = NULL_TREE;
23491 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23493 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23494 protorefs = cp_parser_objc_identifier_list (parser);
23495 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23498 return protorefs;
23501 /* Parse a Objective-C visibility specification. */
23503 static void
23504 cp_parser_objc_visibility_spec (cp_parser* parser)
23506 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23508 switch (vis->keyword)
23510 case RID_AT_PRIVATE:
23511 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23512 break;
23513 case RID_AT_PROTECTED:
23514 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23515 break;
23516 case RID_AT_PUBLIC:
23517 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23518 break;
23519 case RID_AT_PACKAGE:
23520 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23521 break;
23522 default:
23523 return;
23526 /* Eat '@private'/'@protected'/'@public'. */
23527 cp_lexer_consume_token (parser->lexer);
23530 /* Parse an Objective-C method type. Return 'true' if it is a class
23531 (+) method, and 'false' if it is an instance (-) method. */
23533 static inline bool
23534 cp_parser_objc_method_type (cp_parser* parser)
23536 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23537 return true;
23538 else
23539 return false;
23542 /* Parse an Objective-C protocol qualifier. */
23544 static tree
23545 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23547 tree quals = NULL_TREE, node;
23548 cp_token *token = cp_lexer_peek_token (parser->lexer);
23550 node = token->u.value;
23552 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23553 && (node == ridpointers [(int) RID_IN]
23554 || node == ridpointers [(int) RID_OUT]
23555 || node == ridpointers [(int) RID_INOUT]
23556 || node == ridpointers [(int) RID_BYCOPY]
23557 || node == ridpointers [(int) RID_BYREF]
23558 || node == ridpointers [(int) RID_ONEWAY]))
23560 quals = tree_cons (NULL_TREE, node, quals);
23561 cp_lexer_consume_token (parser->lexer);
23562 token = cp_lexer_peek_token (parser->lexer);
23563 node = token->u.value;
23566 return quals;
23569 /* Parse an Objective-C typename. */
23571 static tree
23572 cp_parser_objc_typename (cp_parser* parser)
23574 tree type_name = NULL_TREE;
23576 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23578 tree proto_quals, cp_type = NULL_TREE;
23580 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23581 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23583 /* An ObjC type name may consist of just protocol qualifiers, in which
23584 case the type shall default to 'id'. */
23585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23587 cp_type = cp_parser_type_id (parser);
23589 /* If the type could not be parsed, an error has already
23590 been produced. For error recovery, behave as if it had
23591 not been specified, which will use the default type
23592 'id'. */
23593 if (cp_type == error_mark_node)
23595 cp_type = NULL_TREE;
23596 /* We need to skip to the closing parenthesis as
23597 cp_parser_type_id() does not seem to do it for
23598 us. */
23599 cp_parser_skip_to_closing_parenthesis (parser,
23600 /*recovering=*/true,
23601 /*or_comma=*/false,
23602 /*consume_paren=*/false);
23606 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23607 type_name = build_tree_list (proto_quals, cp_type);
23610 return type_name;
23613 /* Check to see if TYPE refers to an Objective-C selector name. */
23615 static bool
23616 cp_parser_objc_selector_p (enum cpp_ttype type)
23618 return (type == CPP_NAME || type == CPP_KEYWORD
23619 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23620 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23621 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23622 || type == CPP_XOR || type == CPP_XOR_EQ);
23625 /* Parse an Objective-C selector. */
23627 static tree
23628 cp_parser_objc_selector (cp_parser* parser)
23630 cp_token *token = cp_lexer_consume_token (parser->lexer);
23632 if (!cp_parser_objc_selector_p (token->type))
23634 error_at (token->location, "invalid Objective-C++ selector name");
23635 return error_mark_node;
23638 /* C++ operator names are allowed to appear in ObjC selectors. */
23639 switch (token->type)
23641 case CPP_AND_AND: return get_identifier ("and");
23642 case CPP_AND_EQ: return get_identifier ("and_eq");
23643 case CPP_AND: return get_identifier ("bitand");
23644 case CPP_OR: return get_identifier ("bitor");
23645 case CPP_COMPL: return get_identifier ("compl");
23646 case CPP_NOT: return get_identifier ("not");
23647 case CPP_NOT_EQ: return get_identifier ("not_eq");
23648 case CPP_OR_OR: return get_identifier ("or");
23649 case CPP_OR_EQ: return get_identifier ("or_eq");
23650 case CPP_XOR: return get_identifier ("xor");
23651 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23652 default: return token->u.value;
23656 /* Parse an Objective-C params list. */
23658 static tree
23659 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23661 tree params = NULL_TREE;
23662 bool maybe_unary_selector_p = true;
23663 cp_token *token = cp_lexer_peek_token (parser->lexer);
23665 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23667 tree selector = NULL_TREE, type_name, identifier;
23668 tree parm_attr = NULL_TREE;
23670 if (token->keyword == RID_ATTRIBUTE)
23671 break;
23673 if (token->type != CPP_COLON)
23674 selector = cp_parser_objc_selector (parser);
23676 /* Detect if we have a unary selector. */
23677 if (maybe_unary_selector_p
23678 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23680 params = selector; /* Might be followed by attributes. */
23681 break;
23684 maybe_unary_selector_p = false;
23685 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23687 /* Something went quite wrong. There should be a colon
23688 here, but there is not. Stop parsing parameters. */
23689 break;
23691 type_name = cp_parser_objc_typename (parser);
23692 /* New ObjC allows attributes on parameters too. */
23693 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23694 parm_attr = cp_parser_attributes_opt (parser);
23695 identifier = cp_parser_identifier (parser);
23697 params
23698 = chainon (params,
23699 objc_build_keyword_decl (selector,
23700 type_name,
23701 identifier,
23702 parm_attr));
23704 token = cp_lexer_peek_token (parser->lexer);
23707 if (params == NULL_TREE)
23709 cp_parser_error (parser, "objective-c++ method declaration is expected");
23710 return error_mark_node;
23713 /* We allow tail attributes for the method. */
23714 if (token->keyword == RID_ATTRIBUTE)
23716 *attributes = cp_parser_attributes_opt (parser);
23717 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23718 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23719 return params;
23720 cp_parser_error (parser,
23721 "method attributes must be specified at the end");
23722 return error_mark_node;
23725 if (params == NULL_TREE)
23727 cp_parser_error (parser, "objective-c++ method declaration is expected");
23728 return error_mark_node;
23730 return params;
23733 /* Parse the non-keyword Objective-C params. */
23735 static tree
23736 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23737 tree* attributes)
23739 tree params = make_node (TREE_LIST);
23740 cp_token *token = cp_lexer_peek_token (parser->lexer);
23741 *ellipsisp = false; /* Initially, assume no ellipsis. */
23743 while (token->type == CPP_COMMA)
23745 cp_parameter_declarator *parmdecl;
23746 tree parm;
23748 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23749 token = cp_lexer_peek_token (parser->lexer);
23751 if (token->type == CPP_ELLIPSIS)
23753 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23754 *ellipsisp = true;
23755 token = cp_lexer_peek_token (parser->lexer);
23756 break;
23759 /* TODO: parse attributes for tail parameters. */
23760 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23761 parm = grokdeclarator (parmdecl->declarator,
23762 &parmdecl->decl_specifiers,
23763 PARM, /*initialized=*/0,
23764 /*attrlist=*/NULL);
23766 chainon (params, build_tree_list (NULL_TREE, parm));
23767 token = cp_lexer_peek_token (parser->lexer);
23770 /* We allow tail attributes for the method. */
23771 if (token->keyword == RID_ATTRIBUTE)
23773 if (*attributes == NULL_TREE)
23775 *attributes = cp_parser_attributes_opt (parser);
23776 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23777 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23778 return params;
23780 else
23781 /* We have an error, but parse the attributes, so that we can
23782 carry on. */
23783 *attributes = cp_parser_attributes_opt (parser);
23785 cp_parser_error (parser,
23786 "method attributes must be specified at the end");
23787 return error_mark_node;
23790 return params;
23793 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23795 static void
23796 cp_parser_objc_interstitial_code (cp_parser* parser)
23798 cp_token *token = cp_lexer_peek_token (parser->lexer);
23800 /* If the next token is `extern' and the following token is a string
23801 literal, then we have a linkage specification. */
23802 if (token->keyword == RID_EXTERN
23803 && cp_parser_is_pure_string_literal
23804 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23805 cp_parser_linkage_specification (parser);
23806 /* Handle #pragma, if any. */
23807 else if (token->type == CPP_PRAGMA)
23808 cp_parser_pragma (parser, pragma_external);
23809 /* Allow stray semicolons. */
23810 else if (token->type == CPP_SEMICOLON)
23811 cp_lexer_consume_token (parser->lexer);
23812 /* Mark methods as optional or required, when building protocols. */
23813 else if (token->keyword == RID_AT_OPTIONAL)
23815 cp_lexer_consume_token (parser->lexer);
23816 objc_set_method_opt (true);
23818 else if (token->keyword == RID_AT_REQUIRED)
23820 cp_lexer_consume_token (parser->lexer);
23821 objc_set_method_opt (false);
23823 else if (token->keyword == RID_NAMESPACE)
23824 cp_parser_namespace_definition (parser);
23825 /* Other stray characters must generate errors. */
23826 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23828 cp_lexer_consume_token (parser->lexer);
23829 error ("stray %qs between Objective-C++ methods",
23830 token->type == CPP_OPEN_BRACE ? "{" : "}");
23832 /* Finally, try to parse a block-declaration, or a function-definition. */
23833 else
23834 cp_parser_block_declaration (parser, /*statement_p=*/false);
23837 /* Parse a method signature. */
23839 static tree
23840 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23842 tree rettype, kwdparms, optparms;
23843 bool ellipsis = false;
23844 bool is_class_method;
23846 is_class_method = cp_parser_objc_method_type (parser);
23847 rettype = cp_parser_objc_typename (parser);
23848 *attributes = NULL_TREE;
23849 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23850 if (kwdparms == error_mark_node)
23851 return error_mark_node;
23852 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23853 if (optparms == error_mark_node)
23854 return error_mark_node;
23856 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23859 static bool
23860 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23862 tree tattr;
23863 cp_lexer_save_tokens (parser->lexer);
23864 tattr = cp_parser_attributes_opt (parser);
23865 gcc_assert (tattr) ;
23867 /* If the attributes are followed by a method introducer, this is not allowed.
23868 Dump the attributes and flag the situation. */
23869 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23870 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23871 return true;
23873 /* Otherwise, the attributes introduce some interstitial code, possibly so
23874 rewind to allow that check. */
23875 cp_lexer_rollback_tokens (parser->lexer);
23876 return false;
23879 /* Parse an Objective-C method prototype list. */
23881 static void
23882 cp_parser_objc_method_prototype_list (cp_parser* parser)
23884 cp_token *token = cp_lexer_peek_token (parser->lexer);
23886 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23888 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23890 tree attributes, sig;
23891 bool is_class_method;
23892 if (token->type == CPP_PLUS)
23893 is_class_method = true;
23894 else
23895 is_class_method = false;
23896 sig = cp_parser_objc_method_signature (parser, &attributes);
23897 if (sig == error_mark_node)
23899 cp_parser_skip_to_end_of_block_or_statement (parser);
23900 token = cp_lexer_peek_token (parser->lexer);
23901 continue;
23903 objc_add_method_declaration (is_class_method, sig, attributes);
23904 cp_parser_consume_semicolon_at_end_of_statement (parser);
23906 else if (token->keyword == RID_AT_PROPERTY)
23907 cp_parser_objc_at_property_declaration (parser);
23908 else if (token->keyword == RID_ATTRIBUTE
23909 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23910 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23911 OPT_Wattributes,
23912 "prefix attributes are ignored for methods");
23913 else
23914 /* Allow for interspersed non-ObjC++ code. */
23915 cp_parser_objc_interstitial_code (parser);
23917 token = cp_lexer_peek_token (parser->lexer);
23920 if (token->type != CPP_EOF)
23921 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23922 else
23923 cp_parser_error (parser, "expected %<@end%>");
23925 objc_finish_interface ();
23928 /* Parse an Objective-C method definition list. */
23930 static void
23931 cp_parser_objc_method_definition_list (cp_parser* parser)
23933 cp_token *token = cp_lexer_peek_token (parser->lexer);
23935 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23937 tree meth;
23939 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23941 cp_token *ptk;
23942 tree sig, attribute;
23943 bool is_class_method;
23944 if (token->type == CPP_PLUS)
23945 is_class_method = true;
23946 else
23947 is_class_method = false;
23948 push_deferring_access_checks (dk_deferred);
23949 sig = cp_parser_objc_method_signature (parser, &attribute);
23950 if (sig == error_mark_node)
23952 cp_parser_skip_to_end_of_block_or_statement (parser);
23953 token = cp_lexer_peek_token (parser->lexer);
23954 continue;
23956 objc_start_method_definition (is_class_method, sig, attribute,
23957 NULL_TREE);
23959 /* For historical reasons, we accept an optional semicolon. */
23960 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23961 cp_lexer_consume_token (parser->lexer);
23963 ptk = cp_lexer_peek_token (parser->lexer);
23964 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23965 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23967 perform_deferred_access_checks ();
23968 stop_deferring_access_checks ();
23969 meth = cp_parser_function_definition_after_declarator (parser,
23970 false);
23971 pop_deferring_access_checks ();
23972 objc_finish_method_definition (meth);
23975 /* The following case will be removed once @synthesize is
23976 completely implemented. */
23977 else if (token->keyword == RID_AT_PROPERTY)
23978 cp_parser_objc_at_property_declaration (parser);
23979 else if (token->keyword == RID_AT_SYNTHESIZE)
23980 cp_parser_objc_at_synthesize_declaration (parser);
23981 else if (token->keyword == RID_AT_DYNAMIC)
23982 cp_parser_objc_at_dynamic_declaration (parser);
23983 else if (token->keyword == RID_ATTRIBUTE
23984 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23985 warning_at (token->location, OPT_Wattributes,
23986 "prefix attributes are ignored for methods");
23987 else
23988 /* Allow for interspersed non-ObjC++ code. */
23989 cp_parser_objc_interstitial_code (parser);
23991 token = cp_lexer_peek_token (parser->lexer);
23994 if (token->type != CPP_EOF)
23995 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23996 else
23997 cp_parser_error (parser, "expected %<@end%>");
23999 objc_finish_implementation ();
24002 /* Parse Objective-C ivars. */
24004 static void
24005 cp_parser_objc_class_ivars (cp_parser* parser)
24007 cp_token *token = cp_lexer_peek_token (parser->lexer);
24009 if (token->type != CPP_OPEN_BRACE)
24010 return; /* No ivars specified. */
24012 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24013 token = cp_lexer_peek_token (parser->lexer);
24015 while (token->type != CPP_CLOSE_BRACE
24016 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24018 cp_decl_specifier_seq declspecs;
24019 int decl_class_or_enum_p;
24020 tree prefix_attributes;
24022 cp_parser_objc_visibility_spec (parser);
24024 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24025 break;
24027 cp_parser_decl_specifier_seq (parser,
24028 CP_PARSER_FLAGS_OPTIONAL,
24029 &declspecs,
24030 &decl_class_or_enum_p);
24032 /* auto, register, static, extern, mutable. */
24033 if (declspecs.storage_class != sc_none)
24035 cp_parser_error (parser, "invalid type for instance variable");
24036 declspecs.storage_class = sc_none;
24039 /* __thread. */
24040 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24042 cp_parser_error (parser, "invalid type for instance variable");
24043 declspecs.locations[ds_thread] = 0;
24046 /* typedef. */
24047 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24049 cp_parser_error (parser, "invalid type for instance variable");
24050 declspecs.locations[ds_thread] = 0;
24053 prefix_attributes = declspecs.attributes;
24054 declspecs.attributes = NULL_TREE;
24056 /* Keep going until we hit the `;' at the end of the
24057 declaration. */
24058 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24060 tree width = NULL_TREE, attributes, first_attribute, decl;
24061 cp_declarator *declarator = NULL;
24062 int ctor_dtor_or_conv_p;
24064 /* Check for a (possibly unnamed) bitfield declaration. */
24065 token = cp_lexer_peek_token (parser->lexer);
24066 if (token->type == CPP_COLON)
24067 goto eat_colon;
24069 if (token->type == CPP_NAME
24070 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24071 == CPP_COLON))
24073 /* Get the name of the bitfield. */
24074 declarator = make_id_declarator (NULL_TREE,
24075 cp_parser_identifier (parser),
24076 sfk_none);
24078 eat_colon:
24079 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24080 /* Get the width of the bitfield. */
24081 width
24082 = cp_parser_constant_expression (parser,
24083 /*allow_non_constant=*/false,
24084 NULL);
24086 else
24088 /* Parse the declarator. */
24089 declarator
24090 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24091 &ctor_dtor_or_conv_p,
24092 /*parenthesized_p=*/NULL,
24093 /*member_p=*/false);
24096 /* Look for attributes that apply to the ivar. */
24097 attributes = cp_parser_attributes_opt (parser);
24098 /* Remember which attributes are prefix attributes and
24099 which are not. */
24100 first_attribute = attributes;
24101 /* Combine the attributes. */
24102 attributes = chainon (prefix_attributes, attributes);
24104 if (width)
24105 /* Create the bitfield declaration. */
24106 decl = grokbitfield (declarator, &declspecs,
24107 width,
24108 attributes);
24109 else
24110 decl = grokfield (declarator, &declspecs,
24111 NULL_TREE, /*init_const_expr_p=*/false,
24112 NULL_TREE, attributes);
24114 /* Add the instance variable. */
24115 if (decl != error_mark_node && decl != NULL_TREE)
24116 objc_add_instance_variable (decl);
24118 /* Reset PREFIX_ATTRIBUTES. */
24119 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24120 attributes = TREE_CHAIN (attributes);
24121 if (attributes)
24122 TREE_CHAIN (attributes) = NULL_TREE;
24124 token = cp_lexer_peek_token (parser->lexer);
24126 if (token->type == CPP_COMMA)
24128 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24129 continue;
24131 break;
24134 cp_parser_consume_semicolon_at_end_of_statement (parser);
24135 token = cp_lexer_peek_token (parser->lexer);
24138 if (token->keyword == RID_AT_END)
24139 cp_parser_error (parser, "expected %<}%>");
24141 /* Do not consume the RID_AT_END, so it will be read again as terminating
24142 the @interface of @implementation. */
24143 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24144 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24146 /* For historical reasons, we accept an optional semicolon. */
24147 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24148 cp_lexer_consume_token (parser->lexer);
24151 /* Parse an Objective-C protocol declaration. */
24153 static void
24154 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24156 tree proto, protorefs;
24157 cp_token *tok;
24159 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24162 tok = cp_lexer_peek_token (parser->lexer);
24163 error_at (tok->location, "identifier expected after %<@protocol%>");
24164 cp_parser_consume_semicolon_at_end_of_statement (parser);
24165 return;
24168 /* See if we have a forward declaration or a definition. */
24169 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24171 /* Try a forward declaration first. */
24172 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24174 while (true)
24176 tree id;
24178 id = cp_parser_identifier (parser);
24179 if (id == error_mark_node)
24180 break;
24182 objc_declare_protocol (id, attributes);
24184 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24185 cp_lexer_consume_token (parser->lexer);
24186 else
24187 break;
24189 cp_parser_consume_semicolon_at_end_of_statement (parser);
24192 /* Ok, we got a full-fledged definition (or at least should). */
24193 else
24195 proto = cp_parser_identifier (parser);
24196 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24197 objc_start_protocol (proto, protorefs, attributes);
24198 cp_parser_objc_method_prototype_list (parser);
24202 /* Parse an Objective-C superclass or category. */
24204 static void
24205 cp_parser_objc_superclass_or_category (cp_parser *parser,
24206 bool iface_p,
24207 tree *super,
24208 tree *categ, bool *is_class_extension)
24210 cp_token *next = cp_lexer_peek_token (parser->lexer);
24212 *super = *categ = NULL_TREE;
24213 *is_class_extension = false;
24214 if (next->type == CPP_COLON)
24216 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24217 *super = cp_parser_identifier (parser);
24219 else if (next->type == CPP_OPEN_PAREN)
24221 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24223 /* If there is no category name, and this is an @interface, we
24224 have a class extension. */
24225 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24227 *categ = NULL_TREE;
24228 *is_class_extension = true;
24230 else
24231 *categ = cp_parser_identifier (parser);
24233 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24237 /* Parse an Objective-C class interface. */
24239 static void
24240 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24242 tree name, super, categ, protos;
24243 bool is_class_extension;
24245 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24246 name = cp_parser_identifier (parser);
24247 if (name == error_mark_node)
24249 /* It's hard to recover because even if valid @interface stuff
24250 is to follow, we can't compile it (or validate it) if we
24251 don't even know which class it refers to. Let's assume this
24252 was a stray '@interface' token in the stream and skip it.
24254 return;
24256 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24257 &is_class_extension);
24258 protos = cp_parser_objc_protocol_refs_opt (parser);
24260 /* We have either a class or a category on our hands. */
24261 if (categ || is_class_extension)
24262 objc_start_category_interface (name, categ, protos, attributes);
24263 else
24265 objc_start_class_interface (name, super, protos, attributes);
24266 /* Handle instance variable declarations, if any. */
24267 cp_parser_objc_class_ivars (parser);
24268 objc_continue_interface ();
24271 cp_parser_objc_method_prototype_list (parser);
24274 /* Parse an Objective-C class implementation. */
24276 static void
24277 cp_parser_objc_class_implementation (cp_parser* parser)
24279 tree name, super, categ;
24280 bool is_class_extension;
24282 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24283 name = cp_parser_identifier (parser);
24284 if (name == error_mark_node)
24286 /* It's hard to recover because even if valid @implementation
24287 stuff is to follow, we can't compile it (or validate it) if
24288 we don't even know which class it refers to. Let's assume
24289 this was a stray '@implementation' token in the stream and
24290 skip it.
24292 return;
24294 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24295 &is_class_extension);
24297 /* We have either a class or a category on our hands. */
24298 if (categ)
24299 objc_start_category_implementation (name, categ);
24300 else
24302 objc_start_class_implementation (name, super);
24303 /* Handle instance variable declarations, if any. */
24304 cp_parser_objc_class_ivars (parser);
24305 objc_continue_implementation ();
24308 cp_parser_objc_method_definition_list (parser);
24311 /* Consume the @end token and finish off the implementation. */
24313 static void
24314 cp_parser_objc_end_implementation (cp_parser* parser)
24316 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24317 objc_finish_implementation ();
24320 /* Parse an Objective-C declaration. */
24322 static void
24323 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24325 /* Try to figure out what kind of declaration is present. */
24326 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24328 if (attributes)
24329 switch (kwd->keyword)
24331 case RID_AT_ALIAS:
24332 case RID_AT_CLASS:
24333 case RID_AT_END:
24334 error_at (kwd->location, "attributes may not be specified before"
24335 " the %<@%D%> Objective-C++ keyword",
24336 kwd->u.value);
24337 attributes = NULL;
24338 break;
24339 case RID_AT_IMPLEMENTATION:
24340 warning_at (kwd->location, OPT_Wattributes,
24341 "prefix attributes are ignored before %<@%D%>",
24342 kwd->u.value);
24343 attributes = NULL;
24344 default:
24345 break;
24348 switch (kwd->keyword)
24350 case RID_AT_ALIAS:
24351 cp_parser_objc_alias_declaration (parser);
24352 break;
24353 case RID_AT_CLASS:
24354 cp_parser_objc_class_declaration (parser);
24355 break;
24356 case RID_AT_PROTOCOL:
24357 cp_parser_objc_protocol_declaration (parser, attributes);
24358 break;
24359 case RID_AT_INTERFACE:
24360 cp_parser_objc_class_interface (parser, attributes);
24361 break;
24362 case RID_AT_IMPLEMENTATION:
24363 cp_parser_objc_class_implementation (parser);
24364 break;
24365 case RID_AT_END:
24366 cp_parser_objc_end_implementation (parser);
24367 break;
24368 default:
24369 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24370 kwd->u.value);
24371 cp_parser_skip_to_end_of_block_or_statement (parser);
24375 /* Parse an Objective-C try-catch-finally statement.
24377 objc-try-catch-finally-stmt:
24378 @try compound-statement objc-catch-clause-seq [opt]
24379 objc-finally-clause [opt]
24381 objc-catch-clause-seq:
24382 objc-catch-clause objc-catch-clause-seq [opt]
24384 objc-catch-clause:
24385 @catch ( objc-exception-declaration ) compound-statement
24387 objc-finally-clause:
24388 @finally compound-statement
24390 objc-exception-declaration:
24391 parameter-declaration
24392 '...'
24394 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24396 Returns NULL_TREE.
24398 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24399 for C. Keep them in sync. */
24401 static tree
24402 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24404 location_t location;
24405 tree stmt;
24407 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24408 location = cp_lexer_peek_token (parser->lexer)->location;
24409 objc_maybe_warn_exceptions (location);
24410 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24411 node, lest it get absorbed into the surrounding block. */
24412 stmt = push_stmt_list ();
24413 cp_parser_compound_statement (parser, NULL, false, false);
24414 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24416 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24418 cp_parameter_declarator *parm;
24419 tree parameter_declaration = error_mark_node;
24420 bool seen_open_paren = false;
24422 cp_lexer_consume_token (parser->lexer);
24423 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24424 seen_open_paren = true;
24425 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24427 /* We have "@catch (...)" (where the '...' are literally
24428 what is in the code). Skip the '...'.
24429 parameter_declaration is set to NULL_TREE, and
24430 objc_being_catch_clauses() knows that that means
24431 '...'. */
24432 cp_lexer_consume_token (parser->lexer);
24433 parameter_declaration = NULL_TREE;
24435 else
24437 /* We have "@catch (NSException *exception)" or something
24438 like that. Parse the parameter declaration. */
24439 parm = cp_parser_parameter_declaration (parser, false, NULL);
24440 if (parm == NULL)
24441 parameter_declaration = error_mark_node;
24442 else
24443 parameter_declaration = grokdeclarator (parm->declarator,
24444 &parm->decl_specifiers,
24445 PARM, /*initialized=*/0,
24446 /*attrlist=*/NULL);
24448 if (seen_open_paren)
24449 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24450 else
24452 /* If there was no open parenthesis, we are recovering from
24453 an error, and we are trying to figure out what mistake
24454 the user has made. */
24456 /* If there is an immediate closing parenthesis, the user
24457 probably forgot the opening one (ie, they typed "@catch
24458 NSException *e)". Parse the closing parenthesis and keep
24459 going. */
24460 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24461 cp_lexer_consume_token (parser->lexer);
24463 /* If these is no immediate closing parenthesis, the user
24464 probably doesn't know that parenthesis are required at
24465 all (ie, they typed "@catch NSException *e"). So, just
24466 forget about the closing parenthesis and keep going. */
24468 objc_begin_catch_clause (parameter_declaration);
24469 cp_parser_compound_statement (parser, NULL, false, false);
24470 objc_finish_catch_clause ();
24472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24474 cp_lexer_consume_token (parser->lexer);
24475 location = cp_lexer_peek_token (parser->lexer)->location;
24476 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24477 node, lest it get absorbed into the surrounding block. */
24478 stmt = push_stmt_list ();
24479 cp_parser_compound_statement (parser, NULL, false, false);
24480 objc_build_finally_clause (location, pop_stmt_list (stmt));
24483 return objc_finish_try_stmt ();
24486 /* Parse an Objective-C synchronized statement.
24488 objc-synchronized-stmt:
24489 @synchronized ( expression ) compound-statement
24491 Returns NULL_TREE. */
24493 static tree
24494 cp_parser_objc_synchronized_statement (cp_parser *parser)
24496 location_t location;
24497 tree lock, stmt;
24499 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24501 location = cp_lexer_peek_token (parser->lexer)->location;
24502 objc_maybe_warn_exceptions (location);
24503 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24504 lock = cp_parser_expression (parser, false, NULL);
24505 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24507 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24508 node, lest it get absorbed into the surrounding block. */
24509 stmt = push_stmt_list ();
24510 cp_parser_compound_statement (parser, NULL, false, false);
24512 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24515 /* Parse an Objective-C throw statement.
24517 objc-throw-stmt:
24518 @throw assignment-expression [opt] ;
24520 Returns a constructed '@throw' statement. */
24522 static tree
24523 cp_parser_objc_throw_statement (cp_parser *parser)
24525 tree expr = NULL_TREE;
24526 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24528 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24530 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24531 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24533 cp_parser_consume_semicolon_at_end_of_statement (parser);
24535 return objc_build_throw_stmt (loc, expr);
24538 /* Parse an Objective-C statement. */
24540 static tree
24541 cp_parser_objc_statement (cp_parser * parser)
24543 /* Try to figure out what kind of declaration is present. */
24544 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24546 switch (kwd->keyword)
24548 case RID_AT_TRY:
24549 return cp_parser_objc_try_catch_finally_statement (parser);
24550 case RID_AT_SYNCHRONIZED:
24551 return cp_parser_objc_synchronized_statement (parser);
24552 case RID_AT_THROW:
24553 return cp_parser_objc_throw_statement (parser);
24554 default:
24555 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24556 kwd->u.value);
24557 cp_parser_skip_to_end_of_block_or_statement (parser);
24560 return error_mark_node;
24563 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24564 look ahead to see if an objc keyword follows the attributes. This
24565 is to detect the use of prefix attributes on ObjC @interface and
24566 @protocol. */
24568 static bool
24569 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24571 cp_lexer_save_tokens (parser->lexer);
24572 *attrib = cp_parser_attributes_opt (parser);
24573 gcc_assert (*attrib);
24574 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24576 cp_lexer_commit_tokens (parser->lexer);
24577 return true;
24579 cp_lexer_rollback_tokens (parser->lexer);
24580 return false;
24583 /* This routine is a minimal replacement for
24584 c_parser_struct_declaration () used when parsing the list of
24585 types/names or ObjC++ properties. For example, when parsing the
24586 code
24588 @property (readonly) int a, b, c;
24590 this function is responsible for parsing "int a, int b, int c" and
24591 returning the declarations as CHAIN of DECLs.
24593 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24594 similar parsing. */
24595 static tree
24596 cp_parser_objc_struct_declaration (cp_parser *parser)
24598 tree decls = NULL_TREE;
24599 cp_decl_specifier_seq declspecs;
24600 int decl_class_or_enum_p;
24601 tree prefix_attributes;
24603 cp_parser_decl_specifier_seq (parser,
24604 CP_PARSER_FLAGS_NONE,
24605 &declspecs,
24606 &decl_class_or_enum_p);
24608 if (declspecs.type == error_mark_node)
24609 return error_mark_node;
24611 /* auto, register, static, extern, mutable. */
24612 if (declspecs.storage_class != sc_none)
24614 cp_parser_error (parser, "invalid type for property");
24615 declspecs.storage_class = sc_none;
24618 /* __thread. */
24619 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24621 cp_parser_error (parser, "invalid type for property");
24622 declspecs.locations[ds_thread] = 0;
24625 /* typedef. */
24626 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24628 cp_parser_error (parser, "invalid type for property");
24629 declspecs.locations[ds_typedef] = 0;
24632 prefix_attributes = declspecs.attributes;
24633 declspecs.attributes = NULL_TREE;
24635 /* Keep going until we hit the `;' at the end of the declaration. */
24636 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24638 tree attributes, first_attribute, decl;
24639 cp_declarator *declarator;
24640 cp_token *token;
24642 /* Parse the declarator. */
24643 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24644 NULL, NULL, false);
24646 /* Look for attributes that apply to the ivar. */
24647 attributes = cp_parser_attributes_opt (parser);
24648 /* Remember which attributes are prefix attributes and
24649 which are not. */
24650 first_attribute = attributes;
24651 /* Combine the attributes. */
24652 attributes = chainon (prefix_attributes, attributes);
24654 decl = grokfield (declarator, &declspecs,
24655 NULL_TREE, /*init_const_expr_p=*/false,
24656 NULL_TREE, attributes);
24658 if (decl == error_mark_node || decl == NULL_TREE)
24659 return error_mark_node;
24661 /* Reset PREFIX_ATTRIBUTES. */
24662 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24663 attributes = TREE_CHAIN (attributes);
24664 if (attributes)
24665 TREE_CHAIN (attributes) = NULL_TREE;
24667 DECL_CHAIN (decl) = decls;
24668 decls = decl;
24670 token = cp_lexer_peek_token (parser->lexer);
24671 if (token->type == CPP_COMMA)
24673 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24674 continue;
24676 else
24677 break;
24679 return decls;
24682 /* Parse an Objective-C @property declaration. The syntax is:
24684 objc-property-declaration:
24685 '@property' objc-property-attributes[opt] struct-declaration ;
24687 objc-property-attributes:
24688 '(' objc-property-attribute-list ')'
24690 objc-property-attribute-list:
24691 objc-property-attribute
24692 objc-property-attribute-list, objc-property-attribute
24694 objc-property-attribute
24695 'getter' = identifier
24696 'setter' = identifier
24697 'readonly'
24698 'readwrite'
24699 'assign'
24700 'retain'
24701 'copy'
24702 'nonatomic'
24704 For example:
24705 @property NSString *name;
24706 @property (readonly) id object;
24707 @property (retain, nonatomic, getter=getTheName) id name;
24708 @property int a, b, c;
24710 PS: This function is identical to
24711 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24712 static void
24713 cp_parser_objc_at_property_declaration (cp_parser *parser)
24715 /* The following variables hold the attributes of the properties as
24716 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24717 seen. When we see an attribute, we set them to 'true' (if they
24718 are boolean properties) or to the identifier (if they have an
24719 argument, ie, for getter and setter). Note that here we only
24720 parse the list of attributes, check the syntax and accumulate the
24721 attributes that we find. objc_add_property_declaration() will
24722 then process the information. */
24723 bool property_assign = false;
24724 bool property_copy = false;
24725 tree property_getter_ident = NULL_TREE;
24726 bool property_nonatomic = false;
24727 bool property_readonly = false;
24728 bool property_readwrite = false;
24729 bool property_retain = false;
24730 tree property_setter_ident = NULL_TREE;
24732 /* 'properties' is the list of properties that we read. Usually a
24733 single one, but maybe more (eg, in "@property int a, b, c;" there
24734 are three). */
24735 tree properties;
24736 location_t loc;
24738 loc = cp_lexer_peek_token (parser->lexer)->location;
24740 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24742 /* Parse the optional attribute list... */
24743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24745 /* Eat the '('. */
24746 cp_lexer_consume_token (parser->lexer);
24748 while (true)
24750 bool syntax_error = false;
24751 cp_token *token = cp_lexer_peek_token (parser->lexer);
24752 enum rid keyword;
24754 if (token->type != CPP_NAME)
24756 cp_parser_error (parser, "expected identifier");
24757 break;
24759 keyword = C_RID_CODE (token->u.value);
24760 cp_lexer_consume_token (parser->lexer);
24761 switch (keyword)
24763 case RID_ASSIGN: property_assign = true; break;
24764 case RID_COPY: property_copy = true; break;
24765 case RID_NONATOMIC: property_nonatomic = true; break;
24766 case RID_READONLY: property_readonly = true; break;
24767 case RID_READWRITE: property_readwrite = true; break;
24768 case RID_RETAIN: property_retain = true; break;
24770 case RID_GETTER:
24771 case RID_SETTER:
24772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24774 if (keyword == RID_GETTER)
24775 cp_parser_error (parser,
24776 "missing %<=%> (after %<getter%> attribute)");
24777 else
24778 cp_parser_error (parser,
24779 "missing %<=%> (after %<setter%> attribute)");
24780 syntax_error = true;
24781 break;
24783 cp_lexer_consume_token (parser->lexer); /* eat the = */
24784 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24786 cp_parser_error (parser, "expected identifier");
24787 syntax_error = true;
24788 break;
24790 if (keyword == RID_SETTER)
24792 if (property_setter_ident != NULL_TREE)
24794 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24795 cp_lexer_consume_token (parser->lexer);
24797 else
24798 property_setter_ident = cp_parser_objc_selector (parser);
24799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24800 cp_parser_error (parser, "setter name must terminate with %<:%>");
24801 else
24802 cp_lexer_consume_token (parser->lexer);
24804 else
24806 if (property_getter_ident != NULL_TREE)
24808 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24809 cp_lexer_consume_token (parser->lexer);
24811 else
24812 property_getter_ident = cp_parser_objc_selector (parser);
24814 break;
24815 default:
24816 cp_parser_error (parser, "unknown property attribute");
24817 syntax_error = true;
24818 break;
24821 if (syntax_error)
24822 break;
24824 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24825 cp_lexer_consume_token (parser->lexer);
24826 else
24827 break;
24830 /* FIXME: "@property (setter, assign);" will generate a spurious
24831 "error: expected ‘)’ before ‘,’ token". This is because
24832 cp_parser_require, unlike the C counterpart, will produce an
24833 error even if we are in error recovery. */
24834 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24836 cp_parser_skip_to_closing_parenthesis (parser,
24837 /*recovering=*/true,
24838 /*or_comma=*/false,
24839 /*consume_paren=*/true);
24843 /* ... and the property declaration(s). */
24844 properties = cp_parser_objc_struct_declaration (parser);
24846 if (properties == error_mark_node)
24848 cp_parser_skip_to_end_of_statement (parser);
24849 /* If the next token is now a `;', consume it. */
24850 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24851 cp_lexer_consume_token (parser->lexer);
24852 return;
24855 if (properties == NULL_TREE)
24856 cp_parser_error (parser, "expected identifier");
24857 else
24859 /* Comma-separated properties are chained together in
24860 reverse order; add them one by one. */
24861 properties = nreverse (properties);
24863 for (; properties; properties = TREE_CHAIN (properties))
24864 objc_add_property_declaration (loc, copy_node (properties),
24865 property_readonly, property_readwrite,
24866 property_assign, property_retain,
24867 property_copy, property_nonatomic,
24868 property_getter_ident, property_setter_ident);
24871 cp_parser_consume_semicolon_at_end_of_statement (parser);
24874 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24876 objc-synthesize-declaration:
24877 @synthesize objc-synthesize-identifier-list ;
24879 objc-synthesize-identifier-list:
24880 objc-synthesize-identifier
24881 objc-synthesize-identifier-list, objc-synthesize-identifier
24883 objc-synthesize-identifier
24884 identifier
24885 identifier = identifier
24887 For example:
24888 @synthesize MyProperty;
24889 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24891 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24892 for C. Keep them in sync.
24894 static void
24895 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24897 tree list = NULL_TREE;
24898 location_t loc;
24899 loc = cp_lexer_peek_token (parser->lexer)->location;
24901 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24902 while (true)
24904 tree property, ivar;
24905 property = cp_parser_identifier (parser);
24906 if (property == error_mark_node)
24908 cp_parser_consume_semicolon_at_end_of_statement (parser);
24909 return;
24911 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24913 cp_lexer_consume_token (parser->lexer);
24914 ivar = cp_parser_identifier (parser);
24915 if (ivar == error_mark_node)
24917 cp_parser_consume_semicolon_at_end_of_statement (parser);
24918 return;
24921 else
24922 ivar = NULL_TREE;
24923 list = chainon (list, build_tree_list (ivar, property));
24924 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24925 cp_lexer_consume_token (parser->lexer);
24926 else
24927 break;
24929 cp_parser_consume_semicolon_at_end_of_statement (parser);
24930 objc_add_synthesize_declaration (loc, list);
24933 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24935 objc-dynamic-declaration:
24936 @dynamic identifier-list ;
24938 For example:
24939 @dynamic MyProperty;
24940 @dynamic MyProperty, AnotherProperty;
24942 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24943 for C. Keep them in sync.
24945 static void
24946 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24948 tree list = NULL_TREE;
24949 location_t loc;
24950 loc = cp_lexer_peek_token (parser->lexer)->location;
24952 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24953 while (true)
24955 tree property;
24956 property = cp_parser_identifier (parser);
24957 if (property == error_mark_node)
24959 cp_parser_consume_semicolon_at_end_of_statement (parser);
24960 return;
24962 list = chainon (list, build_tree_list (NULL, property));
24963 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24964 cp_lexer_consume_token (parser->lexer);
24965 else
24966 break;
24968 cp_parser_consume_semicolon_at_end_of_statement (parser);
24969 objc_add_dynamic_declaration (loc, list);
24973 /* OpenMP 2.5 parsing routines. */
24975 /* Returns name of the next clause.
24976 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24977 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24978 returned and the token is consumed. */
24980 static pragma_omp_clause
24981 cp_parser_omp_clause_name (cp_parser *parser)
24983 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24986 result = PRAGMA_OMP_CLAUSE_IF;
24987 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24988 result = PRAGMA_OMP_CLAUSE_DEFAULT;
24989 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24990 result = PRAGMA_OMP_CLAUSE_PRIVATE;
24991 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24993 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24994 const char *p = IDENTIFIER_POINTER (id);
24996 switch (p[0])
24998 case 'c':
24999 if (!strcmp ("collapse", p))
25000 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25001 else if (!strcmp ("copyin", p))
25002 result = PRAGMA_OMP_CLAUSE_COPYIN;
25003 else if (!strcmp ("copyprivate", p))
25004 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25005 break;
25006 case 'f':
25007 if (!strcmp ("final", p))
25008 result = PRAGMA_OMP_CLAUSE_FINAL;
25009 else if (!strcmp ("firstprivate", p))
25010 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25011 break;
25012 case 'l':
25013 if (!strcmp ("lastprivate", p))
25014 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25015 break;
25016 case 'm':
25017 if (!strcmp ("mergeable", p))
25018 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25019 break;
25020 case 'n':
25021 if (!strcmp ("nowait", p))
25022 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25023 else if (!strcmp ("num_threads", p))
25024 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25025 break;
25026 case 'o':
25027 if (!strcmp ("ordered", p))
25028 result = PRAGMA_OMP_CLAUSE_ORDERED;
25029 break;
25030 case 'r':
25031 if (!strcmp ("reduction", p))
25032 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25033 break;
25034 case 's':
25035 if (!strcmp ("schedule", p))
25036 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25037 else if (!strcmp ("shared", p))
25038 result = PRAGMA_OMP_CLAUSE_SHARED;
25039 break;
25040 case 'u':
25041 if (!strcmp ("untied", p))
25042 result = PRAGMA_OMP_CLAUSE_UNTIED;
25043 break;
25047 if (result != PRAGMA_OMP_CLAUSE_NONE)
25048 cp_lexer_consume_token (parser->lexer);
25050 return result;
25053 /* Validate that a clause of the given type does not already exist. */
25055 static void
25056 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25057 const char *name, location_t location)
25059 tree c;
25061 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25062 if (OMP_CLAUSE_CODE (c) == code)
25064 error_at (location, "too many %qs clauses", name);
25065 break;
25069 /* OpenMP 2.5:
25070 variable-list:
25071 identifier
25072 variable-list , identifier
25074 In addition, we match a closing parenthesis. An opening parenthesis
25075 will have been consumed by the caller.
25077 If KIND is nonzero, create the appropriate node and install the decl
25078 in OMP_CLAUSE_DECL and add the node to the head of the list.
25080 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25081 return the list created. */
25083 static tree
25084 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25085 tree list)
25087 cp_token *token;
25088 while (1)
25090 tree name, decl;
25092 token = cp_lexer_peek_token (parser->lexer);
25093 name = cp_parser_id_expression (parser, /*template_p=*/false,
25094 /*check_dependency_p=*/true,
25095 /*template_p=*/NULL,
25096 /*declarator_p=*/false,
25097 /*optional_p=*/false);
25098 if (name == error_mark_node)
25099 goto skip_comma;
25101 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25102 if (decl == error_mark_node)
25103 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25104 token->location);
25105 else if (kind != 0)
25107 tree u = build_omp_clause (token->location, kind);
25108 OMP_CLAUSE_DECL (u) = decl;
25109 OMP_CLAUSE_CHAIN (u) = list;
25110 list = u;
25112 else
25113 list = tree_cons (decl, NULL_TREE, list);
25115 get_comma:
25116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25117 break;
25118 cp_lexer_consume_token (parser->lexer);
25121 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25123 int ending;
25125 /* Try to resync to an unnested comma. Copied from
25126 cp_parser_parenthesized_expression_list. */
25127 skip_comma:
25128 ending = cp_parser_skip_to_closing_parenthesis (parser,
25129 /*recovering=*/true,
25130 /*or_comma=*/true,
25131 /*consume_paren=*/true);
25132 if (ending < 0)
25133 goto get_comma;
25136 return list;
25139 /* Similarly, but expect leading and trailing parenthesis. This is a very
25140 common case for omp clauses. */
25142 static tree
25143 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25145 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25146 return cp_parser_omp_var_list_no_open (parser, kind, list);
25147 return list;
25150 /* OpenMP 3.0:
25151 collapse ( constant-expression ) */
25153 static tree
25154 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25156 tree c, num;
25157 location_t loc;
25158 HOST_WIDE_INT n;
25160 loc = cp_lexer_peek_token (parser->lexer)->location;
25161 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25162 return list;
25164 num = cp_parser_constant_expression (parser, false, NULL);
25166 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25167 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25168 /*or_comma=*/false,
25169 /*consume_paren=*/true);
25171 if (num == error_mark_node)
25172 return list;
25173 num = fold_non_dependent_expr (num);
25174 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25175 || !host_integerp (num, 0)
25176 || (n = tree_low_cst (num, 0)) <= 0
25177 || (int) n != n)
25179 error_at (loc, "collapse argument needs positive constant integer expression");
25180 return list;
25183 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25184 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25185 OMP_CLAUSE_CHAIN (c) = list;
25186 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25188 return c;
25191 /* OpenMP 2.5:
25192 default ( shared | none ) */
25194 static tree
25195 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25197 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25198 tree c;
25200 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25201 return list;
25202 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25204 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25205 const char *p = IDENTIFIER_POINTER (id);
25207 switch (p[0])
25209 case 'n':
25210 if (strcmp ("none", p) != 0)
25211 goto invalid_kind;
25212 kind = OMP_CLAUSE_DEFAULT_NONE;
25213 break;
25215 case 's':
25216 if (strcmp ("shared", p) != 0)
25217 goto invalid_kind;
25218 kind = OMP_CLAUSE_DEFAULT_SHARED;
25219 break;
25221 default:
25222 goto invalid_kind;
25225 cp_lexer_consume_token (parser->lexer);
25227 else
25229 invalid_kind:
25230 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25233 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25234 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25235 /*or_comma=*/false,
25236 /*consume_paren=*/true);
25238 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25239 return list;
25241 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25242 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25243 OMP_CLAUSE_CHAIN (c) = list;
25244 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25246 return c;
25249 /* OpenMP 3.1:
25250 final ( expression ) */
25252 static tree
25253 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25255 tree t, c;
25257 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25258 return list;
25260 t = cp_parser_condition (parser);
25262 if (t == error_mark_node
25263 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25264 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25265 /*or_comma=*/false,
25266 /*consume_paren=*/true);
25268 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25270 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25271 OMP_CLAUSE_FINAL_EXPR (c) = t;
25272 OMP_CLAUSE_CHAIN (c) = list;
25274 return c;
25277 /* OpenMP 2.5:
25278 if ( expression ) */
25280 static tree
25281 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25283 tree t, c;
25285 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25286 return list;
25288 t = cp_parser_condition (parser);
25290 if (t == error_mark_node
25291 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25292 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25293 /*or_comma=*/false,
25294 /*consume_paren=*/true);
25296 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25298 c = build_omp_clause (location, OMP_CLAUSE_IF);
25299 OMP_CLAUSE_IF_EXPR (c) = t;
25300 OMP_CLAUSE_CHAIN (c) = list;
25302 return c;
25305 /* OpenMP 3.1:
25306 mergeable */
25308 static tree
25309 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25310 tree list, location_t location)
25312 tree c;
25314 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25315 location);
25317 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25318 OMP_CLAUSE_CHAIN (c) = list;
25319 return c;
25322 /* OpenMP 2.5:
25323 nowait */
25325 static tree
25326 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25327 tree list, location_t location)
25329 tree c;
25331 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25333 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25334 OMP_CLAUSE_CHAIN (c) = list;
25335 return c;
25338 /* OpenMP 2.5:
25339 num_threads ( expression ) */
25341 static tree
25342 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25343 location_t location)
25345 tree t, c;
25347 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25348 return list;
25350 t = cp_parser_expression (parser, false, NULL);
25352 if (t == error_mark_node
25353 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25354 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25355 /*or_comma=*/false,
25356 /*consume_paren=*/true);
25358 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25359 "num_threads", location);
25361 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25362 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25363 OMP_CLAUSE_CHAIN (c) = list;
25365 return c;
25368 /* OpenMP 2.5:
25369 ordered */
25371 static tree
25372 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25373 tree list, location_t location)
25375 tree c;
25377 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25378 "ordered", location);
25380 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25381 OMP_CLAUSE_CHAIN (c) = list;
25382 return c;
25385 /* OpenMP 2.5:
25386 reduction ( reduction-operator : variable-list )
25388 reduction-operator:
25389 One of: + * - & ^ | && ||
25391 OpenMP 3.1:
25393 reduction-operator:
25394 One of: + * - & ^ | && || min max */
25396 static tree
25397 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25399 enum tree_code code;
25400 tree nlist, c;
25402 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25403 return list;
25405 switch (cp_lexer_peek_token (parser->lexer)->type)
25407 case CPP_PLUS:
25408 code = PLUS_EXPR;
25409 break;
25410 case CPP_MULT:
25411 code = MULT_EXPR;
25412 break;
25413 case CPP_MINUS:
25414 code = MINUS_EXPR;
25415 break;
25416 case CPP_AND:
25417 code = BIT_AND_EXPR;
25418 break;
25419 case CPP_XOR:
25420 code = BIT_XOR_EXPR;
25421 break;
25422 case CPP_OR:
25423 code = BIT_IOR_EXPR;
25424 break;
25425 case CPP_AND_AND:
25426 code = TRUTH_ANDIF_EXPR;
25427 break;
25428 case CPP_OR_OR:
25429 code = TRUTH_ORIF_EXPR;
25430 break;
25431 case CPP_NAME:
25433 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25434 const char *p = IDENTIFIER_POINTER (id);
25436 if (strcmp (p, "min") == 0)
25438 code = MIN_EXPR;
25439 break;
25441 if (strcmp (p, "max") == 0)
25443 code = MAX_EXPR;
25444 break;
25447 /* FALLTHROUGH */
25448 default:
25449 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25450 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25451 resync_fail:
25452 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25453 /*or_comma=*/false,
25454 /*consume_paren=*/true);
25455 return list;
25457 cp_lexer_consume_token (parser->lexer);
25459 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25460 goto resync_fail;
25462 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25463 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25464 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25466 return nlist;
25469 /* OpenMP 2.5:
25470 schedule ( schedule-kind )
25471 schedule ( schedule-kind , expression )
25473 schedule-kind:
25474 static | dynamic | guided | runtime | auto */
25476 static tree
25477 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25479 tree c, t;
25481 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25482 return list;
25484 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25486 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25488 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25489 const char *p = IDENTIFIER_POINTER (id);
25491 switch (p[0])
25493 case 'd':
25494 if (strcmp ("dynamic", p) != 0)
25495 goto invalid_kind;
25496 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25497 break;
25499 case 'g':
25500 if (strcmp ("guided", p) != 0)
25501 goto invalid_kind;
25502 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25503 break;
25505 case 'r':
25506 if (strcmp ("runtime", p) != 0)
25507 goto invalid_kind;
25508 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25509 break;
25511 default:
25512 goto invalid_kind;
25515 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25516 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25517 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25518 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25519 else
25520 goto invalid_kind;
25521 cp_lexer_consume_token (parser->lexer);
25523 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25525 cp_token *token;
25526 cp_lexer_consume_token (parser->lexer);
25528 token = cp_lexer_peek_token (parser->lexer);
25529 t = cp_parser_assignment_expression (parser, false, NULL);
25531 if (t == error_mark_node)
25532 goto resync_fail;
25533 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25534 error_at (token->location, "schedule %<runtime%> does not take "
25535 "a %<chunk_size%> parameter");
25536 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25537 error_at (token->location, "schedule %<auto%> does not take "
25538 "a %<chunk_size%> parameter");
25539 else
25540 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25542 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25543 goto resync_fail;
25545 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25546 goto resync_fail;
25548 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25549 OMP_CLAUSE_CHAIN (c) = list;
25550 return c;
25552 invalid_kind:
25553 cp_parser_error (parser, "invalid schedule kind");
25554 resync_fail:
25555 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25556 /*or_comma=*/false,
25557 /*consume_paren=*/true);
25558 return list;
25561 /* OpenMP 3.0:
25562 untied */
25564 static tree
25565 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25566 tree list, location_t location)
25568 tree c;
25570 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25572 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25573 OMP_CLAUSE_CHAIN (c) = list;
25574 return c;
25577 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25578 is a bitmask in MASK. Return the list of clauses found; the result
25579 of clause default goes in *pdefault. */
25581 static tree
25582 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25583 const char *where, cp_token *pragma_tok)
25585 tree clauses = NULL;
25586 bool first = true;
25587 cp_token *token = NULL;
25589 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25591 pragma_omp_clause c_kind;
25592 const char *c_name;
25593 tree prev = clauses;
25595 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25596 cp_lexer_consume_token (parser->lexer);
25598 token = cp_lexer_peek_token (parser->lexer);
25599 c_kind = cp_parser_omp_clause_name (parser);
25600 first = false;
25602 switch (c_kind)
25604 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25605 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25606 token->location);
25607 c_name = "collapse";
25608 break;
25609 case PRAGMA_OMP_CLAUSE_COPYIN:
25610 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25611 c_name = "copyin";
25612 break;
25613 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25614 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25615 clauses);
25616 c_name = "copyprivate";
25617 break;
25618 case PRAGMA_OMP_CLAUSE_DEFAULT:
25619 clauses = cp_parser_omp_clause_default (parser, clauses,
25620 token->location);
25621 c_name = "default";
25622 break;
25623 case PRAGMA_OMP_CLAUSE_FINAL:
25624 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25625 c_name = "final";
25626 break;
25627 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25628 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25629 clauses);
25630 c_name = "firstprivate";
25631 break;
25632 case PRAGMA_OMP_CLAUSE_IF:
25633 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25634 c_name = "if";
25635 break;
25636 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25637 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25638 clauses);
25639 c_name = "lastprivate";
25640 break;
25641 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25642 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25643 token->location);
25644 c_name = "mergeable";
25645 break;
25646 case PRAGMA_OMP_CLAUSE_NOWAIT:
25647 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25648 c_name = "nowait";
25649 break;
25650 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25651 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25652 token->location);
25653 c_name = "num_threads";
25654 break;
25655 case PRAGMA_OMP_CLAUSE_ORDERED:
25656 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25657 token->location);
25658 c_name = "ordered";
25659 break;
25660 case PRAGMA_OMP_CLAUSE_PRIVATE:
25661 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25662 clauses);
25663 c_name = "private";
25664 break;
25665 case PRAGMA_OMP_CLAUSE_REDUCTION:
25666 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25667 c_name = "reduction";
25668 break;
25669 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25670 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25671 token->location);
25672 c_name = "schedule";
25673 break;
25674 case PRAGMA_OMP_CLAUSE_SHARED:
25675 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25676 clauses);
25677 c_name = "shared";
25678 break;
25679 case PRAGMA_OMP_CLAUSE_UNTIED:
25680 clauses = cp_parser_omp_clause_untied (parser, clauses,
25681 token->location);
25682 c_name = "nowait";
25683 break;
25684 default:
25685 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25686 goto saw_error;
25689 if (((mask >> c_kind) & 1) == 0)
25691 /* Remove the invalid clause(s) from the list to avoid
25692 confusing the rest of the compiler. */
25693 clauses = prev;
25694 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25697 saw_error:
25698 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25699 return finish_omp_clauses (clauses);
25702 /* OpenMP 2.5:
25703 structured-block:
25704 statement
25706 In practice, we're also interested in adding the statement to an
25707 outer node. So it is convenient if we work around the fact that
25708 cp_parser_statement calls add_stmt. */
25710 static unsigned
25711 cp_parser_begin_omp_structured_block (cp_parser *parser)
25713 unsigned save = parser->in_statement;
25715 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25716 This preserves the "not within loop or switch" style error messages
25717 for nonsense cases like
25718 void foo() {
25719 #pragma omp single
25720 break;
25723 if (parser->in_statement)
25724 parser->in_statement = IN_OMP_BLOCK;
25726 return save;
25729 static void
25730 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25732 parser->in_statement = save;
25735 static tree
25736 cp_parser_omp_structured_block (cp_parser *parser)
25738 tree stmt = begin_omp_structured_block ();
25739 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25741 cp_parser_statement (parser, NULL_TREE, false, NULL);
25743 cp_parser_end_omp_structured_block (parser, save);
25744 return finish_omp_structured_block (stmt);
25747 /* OpenMP 2.5:
25748 # pragma omp atomic new-line
25749 expression-stmt
25751 expression-stmt:
25752 x binop= expr | x++ | ++x | x-- | --x
25753 binop:
25754 +, *, -, /, &, ^, |, <<, >>
25756 where x is an lvalue expression with scalar type.
25758 OpenMP 3.1:
25759 # pragma omp atomic new-line
25760 update-stmt
25762 # pragma omp atomic read new-line
25763 read-stmt
25765 # pragma omp atomic write new-line
25766 write-stmt
25768 # pragma omp atomic update new-line
25769 update-stmt
25771 # pragma omp atomic capture new-line
25772 capture-stmt
25774 # pragma omp atomic capture new-line
25775 capture-block
25777 read-stmt:
25778 v = x
25779 write-stmt:
25780 x = expr
25781 update-stmt:
25782 expression-stmt | x = x binop expr
25783 capture-stmt:
25784 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25785 capture-block:
25786 { v = x; update-stmt; } | { update-stmt; v = x; }
25788 where x and v are lvalue expressions with scalar type. */
25790 static void
25791 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25793 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25794 tree rhs1 = NULL_TREE, orig_lhs;
25795 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25796 bool structured_block = false;
25798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25800 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25801 const char *p = IDENTIFIER_POINTER (id);
25803 if (!strcmp (p, "read"))
25804 code = OMP_ATOMIC_READ;
25805 else if (!strcmp (p, "write"))
25806 code = NOP_EXPR;
25807 else if (!strcmp (p, "update"))
25808 code = OMP_ATOMIC;
25809 else if (!strcmp (p, "capture"))
25810 code = OMP_ATOMIC_CAPTURE_NEW;
25811 else
25812 p = NULL;
25813 if (p)
25814 cp_lexer_consume_token (parser->lexer);
25816 cp_parser_require_pragma_eol (parser, pragma_tok);
25818 switch (code)
25820 case OMP_ATOMIC_READ:
25821 case NOP_EXPR: /* atomic write */
25822 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25823 /*cast_p=*/false, NULL);
25824 if (v == error_mark_node)
25825 goto saw_error;
25826 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25827 goto saw_error;
25828 if (code == NOP_EXPR)
25829 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25830 else
25831 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25832 /*cast_p=*/false, NULL);
25833 if (lhs == error_mark_node)
25834 goto saw_error;
25835 if (code == NOP_EXPR)
25837 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25838 opcode. */
25839 code = OMP_ATOMIC;
25840 rhs = lhs;
25841 lhs = v;
25842 v = NULL_TREE;
25844 goto done;
25845 case OMP_ATOMIC_CAPTURE_NEW:
25846 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25848 cp_lexer_consume_token (parser->lexer);
25849 structured_block = true;
25851 else
25853 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25854 /*cast_p=*/false, NULL);
25855 if (v == error_mark_node)
25856 goto saw_error;
25857 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25858 goto saw_error;
25860 default:
25861 break;
25864 restart:
25865 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25866 /*cast_p=*/false, NULL);
25867 orig_lhs = lhs;
25868 switch (TREE_CODE (lhs))
25870 case ERROR_MARK:
25871 goto saw_error;
25873 case POSTINCREMENT_EXPR:
25874 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25875 code = OMP_ATOMIC_CAPTURE_OLD;
25876 /* FALLTHROUGH */
25877 case PREINCREMENT_EXPR:
25878 lhs = TREE_OPERAND (lhs, 0);
25879 opcode = PLUS_EXPR;
25880 rhs = integer_one_node;
25881 break;
25883 case POSTDECREMENT_EXPR:
25884 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25885 code = OMP_ATOMIC_CAPTURE_OLD;
25886 /* FALLTHROUGH */
25887 case PREDECREMENT_EXPR:
25888 lhs = TREE_OPERAND (lhs, 0);
25889 opcode = MINUS_EXPR;
25890 rhs = integer_one_node;
25891 break;
25893 case COMPOUND_EXPR:
25894 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25895 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25896 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25897 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25898 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25899 (TREE_OPERAND (lhs, 1), 0), 0)))
25900 == BOOLEAN_TYPE)
25901 /* Undo effects of boolean_increment for post {in,de}crement. */
25902 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25903 /* FALLTHRU */
25904 case MODIFY_EXPR:
25905 if (TREE_CODE (lhs) == MODIFY_EXPR
25906 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25908 /* Undo effects of boolean_increment. */
25909 if (integer_onep (TREE_OPERAND (lhs, 1)))
25911 /* This is pre or post increment. */
25912 rhs = TREE_OPERAND (lhs, 1);
25913 lhs = TREE_OPERAND (lhs, 0);
25914 opcode = NOP_EXPR;
25915 if (code == OMP_ATOMIC_CAPTURE_NEW
25916 && !structured_block
25917 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25918 code = OMP_ATOMIC_CAPTURE_OLD;
25919 break;
25922 /* FALLTHRU */
25923 default:
25924 switch (cp_lexer_peek_token (parser->lexer)->type)
25926 case CPP_MULT_EQ:
25927 opcode = MULT_EXPR;
25928 break;
25929 case CPP_DIV_EQ:
25930 opcode = TRUNC_DIV_EXPR;
25931 break;
25932 case CPP_PLUS_EQ:
25933 opcode = PLUS_EXPR;
25934 break;
25935 case CPP_MINUS_EQ:
25936 opcode = MINUS_EXPR;
25937 break;
25938 case CPP_LSHIFT_EQ:
25939 opcode = LSHIFT_EXPR;
25940 break;
25941 case CPP_RSHIFT_EQ:
25942 opcode = RSHIFT_EXPR;
25943 break;
25944 case CPP_AND_EQ:
25945 opcode = BIT_AND_EXPR;
25946 break;
25947 case CPP_OR_EQ:
25948 opcode = BIT_IOR_EXPR;
25949 break;
25950 case CPP_XOR_EQ:
25951 opcode = BIT_XOR_EXPR;
25952 break;
25953 case CPP_EQ:
25954 if (structured_block || code == OMP_ATOMIC)
25956 enum cp_parser_prec oprec;
25957 cp_token *token;
25958 cp_lexer_consume_token (parser->lexer);
25959 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25960 /*cast_p=*/false, NULL);
25961 if (rhs1 == error_mark_node)
25962 goto saw_error;
25963 token = cp_lexer_peek_token (parser->lexer);
25964 switch (token->type)
25966 case CPP_SEMICOLON:
25967 if (code == OMP_ATOMIC_CAPTURE_NEW)
25969 code = OMP_ATOMIC_CAPTURE_OLD;
25970 v = lhs;
25971 lhs = NULL_TREE;
25972 lhs1 = rhs1;
25973 rhs1 = NULL_TREE;
25974 cp_lexer_consume_token (parser->lexer);
25975 goto restart;
25977 cp_parser_error (parser,
25978 "invalid form of %<#pragma omp atomic%>");
25979 goto saw_error;
25980 case CPP_MULT:
25981 opcode = MULT_EXPR;
25982 break;
25983 case CPP_DIV:
25984 opcode = TRUNC_DIV_EXPR;
25985 break;
25986 case CPP_PLUS:
25987 opcode = PLUS_EXPR;
25988 break;
25989 case CPP_MINUS:
25990 opcode = MINUS_EXPR;
25991 break;
25992 case CPP_LSHIFT:
25993 opcode = LSHIFT_EXPR;
25994 break;
25995 case CPP_RSHIFT:
25996 opcode = RSHIFT_EXPR;
25997 break;
25998 case CPP_AND:
25999 opcode = BIT_AND_EXPR;
26000 break;
26001 case CPP_OR:
26002 opcode = BIT_IOR_EXPR;
26003 break;
26004 case CPP_XOR:
26005 opcode = BIT_XOR_EXPR;
26006 break;
26007 default:
26008 cp_parser_error (parser,
26009 "invalid operator for %<#pragma omp atomic%>");
26010 goto saw_error;
26012 oprec = TOKEN_PRECEDENCE (token);
26013 gcc_assert (oprec != PREC_NOT_OPERATOR);
26014 if (commutative_tree_code (opcode))
26015 oprec = (enum cp_parser_prec) (oprec - 1);
26016 cp_lexer_consume_token (parser->lexer);
26017 rhs = cp_parser_binary_expression (parser, false, false,
26018 oprec, NULL);
26019 if (rhs == error_mark_node)
26020 goto saw_error;
26021 goto stmt_done;
26023 /* FALLTHROUGH */
26024 default:
26025 cp_parser_error (parser,
26026 "invalid operator for %<#pragma omp atomic%>");
26027 goto saw_error;
26029 cp_lexer_consume_token (parser->lexer);
26031 rhs = cp_parser_expression (parser, false, NULL);
26032 if (rhs == error_mark_node)
26033 goto saw_error;
26034 break;
26036 stmt_done:
26037 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26039 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26040 goto saw_error;
26041 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26042 /*cast_p=*/false, NULL);
26043 if (v == error_mark_node)
26044 goto saw_error;
26045 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26046 goto saw_error;
26047 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26048 /*cast_p=*/false, NULL);
26049 if (lhs1 == error_mark_node)
26050 goto saw_error;
26052 if (structured_block)
26054 cp_parser_consume_semicolon_at_end_of_statement (parser);
26055 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26057 done:
26058 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26059 if (!structured_block)
26060 cp_parser_consume_semicolon_at_end_of_statement (parser);
26061 return;
26063 saw_error:
26064 cp_parser_skip_to_end_of_block_or_statement (parser);
26065 if (structured_block)
26067 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26068 cp_lexer_consume_token (parser->lexer);
26069 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26071 cp_parser_skip_to_end_of_block_or_statement (parser);
26072 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26073 cp_lexer_consume_token (parser->lexer);
26079 /* OpenMP 2.5:
26080 # pragma omp barrier new-line */
26082 static void
26083 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26085 cp_parser_require_pragma_eol (parser, pragma_tok);
26086 finish_omp_barrier ();
26089 /* OpenMP 2.5:
26090 # pragma omp critical [(name)] new-line
26091 structured-block */
26093 static tree
26094 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26096 tree stmt, name = NULL;
26098 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26100 cp_lexer_consume_token (parser->lexer);
26102 name = cp_parser_identifier (parser);
26104 if (name == error_mark_node
26105 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26106 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26107 /*or_comma=*/false,
26108 /*consume_paren=*/true);
26109 if (name == error_mark_node)
26110 name = NULL;
26112 cp_parser_require_pragma_eol (parser, pragma_tok);
26114 stmt = cp_parser_omp_structured_block (parser);
26115 return c_finish_omp_critical (input_location, stmt, name);
26118 /* OpenMP 2.5:
26119 # pragma omp flush flush-vars[opt] new-line
26121 flush-vars:
26122 ( variable-list ) */
26124 static void
26125 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26127 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26128 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26129 cp_parser_require_pragma_eol (parser, pragma_tok);
26131 finish_omp_flush ();
26134 /* Helper function, to parse omp for increment expression. */
26136 static tree
26137 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26139 tree cond = cp_parser_binary_expression (parser, false, true,
26140 PREC_NOT_OPERATOR, NULL);
26141 if (cond == error_mark_node
26142 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26144 cp_parser_skip_to_end_of_statement (parser);
26145 return error_mark_node;
26148 switch (TREE_CODE (cond))
26150 case GT_EXPR:
26151 case GE_EXPR:
26152 case LT_EXPR:
26153 case LE_EXPR:
26154 break;
26155 default:
26156 return error_mark_node;
26159 /* If decl is an iterator, preserve LHS and RHS of the relational
26160 expr until finish_omp_for. */
26161 if (decl
26162 && (type_dependent_expression_p (decl)
26163 || CLASS_TYPE_P (TREE_TYPE (decl))))
26164 return cond;
26166 return build_x_binary_op (input_location, TREE_CODE (cond),
26167 TREE_OPERAND (cond, 0), ERROR_MARK,
26168 TREE_OPERAND (cond, 1), ERROR_MARK,
26169 /*overload=*/NULL, tf_warning_or_error);
26172 /* Helper function, to parse omp for increment expression. */
26174 static tree
26175 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26177 cp_token *token = cp_lexer_peek_token (parser->lexer);
26178 enum tree_code op;
26179 tree lhs, rhs;
26180 cp_id_kind idk;
26181 bool decl_first;
26183 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26185 op = (token->type == CPP_PLUS_PLUS
26186 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26187 cp_lexer_consume_token (parser->lexer);
26188 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26189 if (lhs != decl)
26190 return error_mark_node;
26191 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26194 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26195 if (lhs != decl)
26196 return error_mark_node;
26198 token = cp_lexer_peek_token (parser->lexer);
26199 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26201 op = (token->type == CPP_PLUS_PLUS
26202 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26203 cp_lexer_consume_token (parser->lexer);
26204 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26207 op = cp_parser_assignment_operator_opt (parser);
26208 if (op == ERROR_MARK)
26209 return error_mark_node;
26211 if (op != NOP_EXPR)
26213 rhs = cp_parser_assignment_expression (parser, false, NULL);
26214 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26215 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26218 lhs = cp_parser_binary_expression (parser, false, false,
26219 PREC_ADDITIVE_EXPRESSION, NULL);
26220 token = cp_lexer_peek_token (parser->lexer);
26221 decl_first = lhs == decl;
26222 if (decl_first)
26223 lhs = NULL_TREE;
26224 if (token->type != CPP_PLUS
26225 && token->type != CPP_MINUS)
26226 return error_mark_node;
26230 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26231 cp_lexer_consume_token (parser->lexer);
26232 rhs = cp_parser_binary_expression (parser, false, false,
26233 PREC_ADDITIVE_EXPRESSION, NULL);
26234 token = cp_lexer_peek_token (parser->lexer);
26235 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26237 if (lhs == NULL_TREE)
26239 if (op == PLUS_EXPR)
26240 lhs = rhs;
26241 else
26242 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26243 tf_warning_or_error);
26245 else
26246 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26247 ERROR_MARK, NULL, tf_warning_or_error);
26250 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26252 if (!decl_first)
26254 if (rhs != decl || op == MINUS_EXPR)
26255 return error_mark_node;
26256 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26258 else
26259 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26261 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26264 /* Parse the restricted form of the for statement allowed by OpenMP. */
26266 static tree
26267 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26269 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26270 tree real_decl, initv, condv, incrv, declv;
26271 tree this_pre_body, cl;
26272 location_t loc_first;
26273 bool collapse_err = false;
26274 int i, collapse = 1, nbraces = 0;
26275 VEC(tree,gc) *for_block = make_tree_vector ();
26277 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26278 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26279 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26281 gcc_assert (collapse >= 1);
26283 declv = make_tree_vec (collapse);
26284 initv = make_tree_vec (collapse);
26285 condv = make_tree_vec (collapse);
26286 incrv = make_tree_vec (collapse);
26288 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26290 for (i = 0; i < collapse; i++)
26292 int bracecount = 0;
26293 bool add_private_clause = false;
26294 location_t loc;
26296 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26298 cp_parser_error (parser, "for statement expected");
26299 return NULL;
26301 loc = cp_lexer_consume_token (parser->lexer)->location;
26303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26304 return NULL;
26306 init = decl = real_decl = NULL;
26307 this_pre_body = push_stmt_list ();
26308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26310 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26312 init-expr:
26313 var = lb
26314 integer-type var = lb
26315 random-access-iterator-type var = lb
26316 pointer-type var = lb
26318 cp_decl_specifier_seq type_specifiers;
26320 /* First, try to parse as an initialized declaration. See
26321 cp_parser_condition, from whence the bulk of this is copied. */
26323 cp_parser_parse_tentatively (parser);
26324 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26325 /*is_trailing_return=*/false,
26326 &type_specifiers);
26327 if (cp_parser_parse_definitely (parser))
26329 /* If parsing a type specifier seq succeeded, then this
26330 MUST be a initialized declaration. */
26331 tree asm_specification, attributes;
26332 cp_declarator *declarator;
26334 declarator = cp_parser_declarator (parser,
26335 CP_PARSER_DECLARATOR_NAMED,
26336 /*ctor_dtor_or_conv_p=*/NULL,
26337 /*parenthesized_p=*/NULL,
26338 /*member_p=*/false);
26339 attributes = cp_parser_attributes_opt (parser);
26340 asm_specification = cp_parser_asm_specification_opt (parser);
26342 if (declarator == cp_error_declarator)
26343 cp_parser_skip_to_end_of_statement (parser);
26345 else
26347 tree pushed_scope, auto_node;
26349 decl = start_decl (declarator, &type_specifiers,
26350 SD_INITIALIZED, attributes,
26351 /*prefix_attributes=*/NULL_TREE,
26352 &pushed_scope);
26354 auto_node = type_uses_auto (TREE_TYPE (decl));
26355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26357 if (cp_lexer_next_token_is (parser->lexer,
26358 CPP_OPEN_PAREN))
26359 error ("parenthesized initialization is not allowed in "
26360 "OpenMP %<for%> loop");
26361 else
26362 /* Trigger an error. */
26363 cp_parser_require (parser, CPP_EQ, RT_EQ);
26365 init = error_mark_node;
26366 cp_parser_skip_to_end_of_statement (parser);
26368 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26369 || type_dependent_expression_p (decl)
26370 || auto_node)
26372 bool is_direct_init, is_non_constant_init;
26374 init = cp_parser_initializer (parser,
26375 &is_direct_init,
26376 &is_non_constant_init);
26378 if (auto_node)
26380 TREE_TYPE (decl)
26381 = do_auto_deduction (TREE_TYPE (decl), init,
26382 auto_node);
26384 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26385 && !type_dependent_expression_p (decl))
26386 goto non_class;
26389 cp_finish_decl (decl, init, !is_non_constant_init,
26390 asm_specification,
26391 LOOKUP_ONLYCONVERTING);
26392 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26394 VEC_safe_push (tree, gc, for_block, this_pre_body);
26395 init = NULL_TREE;
26397 else
26398 init = pop_stmt_list (this_pre_body);
26399 this_pre_body = NULL_TREE;
26401 else
26403 /* Consume '='. */
26404 cp_lexer_consume_token (parser->lexer);
26405 init = cp_parser_assignment_expression (parser, false, NULL);
26407 non_class:
26408 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26409 init = error_mark_node;
26410 else
26411 cp_finish_decl (decl, NULL_TREE,
26412 /*init_const_expr_p=*/false,
26413 asm_specification,
26414 LOOKUP_ONLYCONVERTING);
26417 if (pushed_scope)
26418 pop_scope (pushed_scope);
26421 else
26423 cp_id_kind idk;
26424 /* If parsing a type specifier sequence failed, then
26425 this MUST be a simple expression. */
26426 cp_parser_parse_tentatively (parser);
26427 decl = cp_parser_primary_expression (parser, false, false,
26428 false, &idk);
26429 if (!cp_parser_error_occurred (parser)
26430 && decl
26431 && DECL_P (decl)
26432 && CLASS_TYPE_P (TREE_TYPE (decl)))
26434 tree rhs;
26436 cp_parser_parse_definitely (parser);
26437 cp_parser_require (parser, CPP_EQ, RT_EQ);
26438 rhs = cp_parser_assignment_expression (parser, false, NULL);
26439 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
26440 decl, NOP_EXPR,
26441 rhs,
26442 tf_warning_or_error));
26443 add_private_clause = true;
26445 else
26447 decl = NULL;
26448 cp_parser_abort_tentative_parse (parser);
26449 init = cp_parser_expression (parser, false, NULL);
26450 if (init)
26452 if (TREE_CODE (init) == MODIFY_EXPR
26453 || TREE_CODE (init) == MODOP_EXPR)
26454 real_decl = TREE_OPERAND (init, 0);
26459 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26460 if (this_pre_body)
26462 this_pre_body = pop_stmt_list (this_pre_body);
26463 if (pre_body)
26465 tree t = pre_body;
26466 pre_body = push_stmt_list ();
26467 add_stmt (t);
26468 add_stmt (this_pre_body);
26469 pre_body = pop_stmt_list (pre_body);
26471 else
26472 pre_body = this_pre_body;
26475 if (decl)
26476 real_decl = decl;
26477 if (par_clauses != NULL && real_decl != NULL_TREE)
26479 tree *c;
26480 for (c = par_clauses; *c ; )
26481 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26482 && OMP_CLAUSE_DECL (*c) == real_decl)
26484 error_at (loc, "iteration variable %qD"
26485 " should not be firstprivate", real_decl);
26486 *c = OMP_CLAUSE_CHAIN (*c);
26488 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26489 && OMP_CLAUSE_DECL (*c) == real_decl)
26491 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26492 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26493 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26494 OMP_CLAUSE_DECL (l) = real_decl;
26495 OMP_CLAUSE_CHAIN (l) = clauses;
26496 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26497 clauses = l;
26498 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26499 CP_OMP_CLAUSE_INFO (*c) = NULL;
26500 add_private_clause = false;
26502 else
26504 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26505 && OMP_CLAUSE_DECL (*c) == real_decl)
26506 add_private_clause = false;
26507 c = &OMP_CLAUSE_CHAIN (*c);
26511 if (add_private_clause)
26513 tree c;
26514 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26516 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26517 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26518 && OMP_CLAUSE_DECL (c) == decl)
26519 break;
26520 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26521 && OMP_CLAUSE_DECL (c) == decl)
26522 error_at (loc, "iteration variable %qD "
26523 "should not be firstprivate",
26524 decl);
26525 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26526 && OMP_CLAUSE_DECL (c) == decl)
26527 error_at (loc, "iteration variable %qD should not be reduction",
26528 decl);
26530 if (c == NULL)
26532 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26533 OMP_CLAUSE_DECL (c) = decl;
26534 c = finish_omp_clauses (c);
26535 if (c)
26537 OMP_CLAUSE_CHAIN (c) = clauses;
26538 clauses = c;
26543 cond = NULL;
26544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26545 cond = cp_parser_omp_for_cond (parser, decl);
26546 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26548 incr = NULL;
26549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26551 /* If decl is an iterator, preserve the operator on decl
26552 until finish_omp_for. */
26553 if (real_decl
26554 && ((processing_template_decl
26555 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26556 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26557 incr = cp_parser_omp_for_incr (parser, real_decl);
26558 else
26559 incr = cp_parser_expression (parser, false, NULL);
26562 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26563 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26564 /*or_comma=*/false,
26565 /*consume_paren=*/true);
26567 TREE_VEC_ELT (declv, i) = decl;
26568 TREE_VEC_ELT (initv, i) = init;
26569 TREE_VEC_ELT (condv, i) = cond;
26570 TREE_VEC_ELT (incrv, i) = incr;
26572 if (i == collapse - 1)
26573 break;
26575 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26576 in between the collapsed for loops to be still considered perfectly
26577 nested. Hopefully the final version clarifies this.
26578 For now handle (multiple) {'s and empty statements. */
26579 cp_parser_parse_tentatively (parser);
26582 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26583 break;
26584 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26586 cp_lexer_consume_token (parser->lexer);
26587 bracecount++;
26589 else if (bracecount
26590 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26591 cp_lexer_consume_token (parser->lexer);
26592 else
26594 loc = cp_lexer_peek_token (parser->lexer)->location;
26595 error_at (loc, "not enough collapsed for loops");
26596 collapse_err = true;
26597 cp_parser_abort_tentative_parse (parser);
26598 declv = NULL_TREE;
26599 break;
26602 while (1);
26604 if (declv)
26606 cp_parser_parse_definitely (parser);
26607 nbraces += bracecount;
26611 /* Note that we saved the original contents of this flag when we entered
26612 the structured block, and so we don't need to re-save it here. */
26613 parser->in_statement = IN_OMP_FOR;
26615 /* Note that the grammar doesn't call for a structured block here,
26616 though the loop as a whole is a structured block. */
26617 body = push_stmt_list ();
26618 cp_parser_statement (parser, NULL_TREE, false, NULL);
26619 body = pop_stmt_list (body);
26621 if (declv == NULL_TREE)
26622 ret = NULL_TREE;
26623 else
26624 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26625 pre_body, clauses);
26627 while (nbraces)
26629 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26631 cp_lexer_consume_token (parser->lexer);
26632 nbraces--;
26634 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26635 cp_lexer_consume_token (parser->lexer);
26636 else
26638 if (!collapse_err)
26640 error_at (cp_lexer_peek_token (parser->lexer)->location,
26641 "collapsed loops not perfectly nested");
26643 collapse_err = true;
26644 cp_parser_statement_seq_opt (parser, NULL);
26645 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26646 break;
26650 while (!VEC_empty (tree, for_block))
26651 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26652 release_tree_vector (for_block);
26654 return ret;
26657 /* OpenMP 2.5:
26658 #pragma omp for for-clause[optseq] new-line
26659 for-loop */
26661 #define OMP_FOR_CLAUSE_MASK \
26662 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26663 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26664 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26665 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26666 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26667 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26668 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26669 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26671 static tree
26672 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26674 tree clauses, sb, ret;
26675 unsigned int save;
26677 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26678 "#pragma omp for", pragma_tok);
26680 sb = begin_omp_structured_block ();
26681 save = cp_parser_begin_omp_structured_block (parser);
26683 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26685 cp_parser_end_omp_structured_block (parser, save);
26686 add_stmt (finish_omp_structured_block (sb));
26688 return ret;
26691 /* OpenMP 2.5:
26692 # pragma omp master new-line
26693 structured-block */
26695 static tree
26696 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26698 cp_parser_require_pragma_eol (parser, pragma_tok);
26699 return c_finish_omp_master (input_location,
26700 cp_parser_omp_structured_block (parser));
26703 /* OpenMP 2.5:
26704 # pragma omp ordered new-line
26705 structured-block */
26707 static tree
26708 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26711 cp_parser_require_pragma_eol (parser, pragma_tok);
26712 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26715 /* OpenMP 2.5:
26717 section-scope:
26718 { section-sequence }
26720 section-sequence:
26721 section-directive[opt] structured-block
26722 section-sequence section-directive structured-block */
26724 static tree
26725 cp_parser_omp_sections_scope (cp_parser *parser)
26727 tree stmt, substmt;
26728 bool error_suppress = false;
26729 cp_token *tok;
26731 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26732 return NULL_TREE;
26734 stmt = push_stmt_list ();
26736 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26738 unsigned save;
26740 substmt = begin_omp_structured_block ();
26741 save = cp_parser_begin_omp_structured_block (parser);
26743 while (1)
26745 cp_parser_statement (parser, NULL_TREE, false, NULL);
26747 tok = cp_lexer_peek_token (parser->lexer);
26748 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26749 break;
26750 if (tok->type == CPP_CLOSE_BRACE)
26751 break;
26752 if (tok->type == CPP_EOF)
26753 break;
26756 cp_parser_end_omp_structured_block (parser, save);
26757 substmt = finish_omp_structured_block (substmt);
26758 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26759 add_stmt (substmt);
26762 while (1)
26764 tok = cp_lexer_peek_token (parser->lexer);
26765 if (tok->type == CPP_CLOSE_BRACE)
26766 break;
26767 if (tok->type == CPP_EOF)
26768 break;
26770 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26772 cp_lexer_consume_token (parser->lexer);
26773 cp_parser_require_pragma_eol (parser, tok);
26774 error_suppress = false;
26776 else if (!error_suppress)
26778 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26779 error_suppress = true;
26782 substmt = cp_parser_omp_structured_block (parser);
26783 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26784 add_stmt (substmt);
26786 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26788 substmt = pop_stmt_list (stmt);
26790 stmt = make_node (OMP_SECTIONS);
26791 TREE_TYPE (stmt) = void_type_node;
26792 OMP_SECTIONS_BODY (stmt) = substmt;
26794 add_stmt (stmt);
26795 return stmt;
26798 /* OpenMP 2.5:
26799 # pragma omp sections sections-clause[optseq] newline
26800 sections-scope */
26802 #define OMP_SECTIONS_CLAUSE_MASK \
26803 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26804 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26805 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26806 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26807 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26809 static tree
26810 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26812 tree clauses, ret;
26814 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26815 "#pragma omp sections", pragma_tok);
26817 ret = cp_parser_omp_sections_scope (parser);
26818 if (ret)
26819 OMP_SECTIONS_CLAUSES (ret) = clauses;
26821 return ret;
26824 /* OpenMP 2.5:
26825 # pragma parallel parallel-clause new-line
26826 # pragma parallel for parallel-for-clause new-line
26827 # pragma parallel sections parallel-sections-clause new-line */
26829 #define OMP_PARALLEL_CLAUSE_MASK \
26830 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26831 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26832 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26833 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26834 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26835 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26836 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26837 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26839 static tree
26840 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26842 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26843 const char *p_name = "#pragma omp parallel";
26844 tree stmt, clauses, par_clause, ws_clause, block;
26845 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26846 unsigned int save;
26847 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26849 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26851 cp_lexer_consume_token (parser->lexer);
26852 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26853 p_name = "#pragma omp parallel for";
26854 mask |= OMP_FOR_CLAUSE_MASK;
26855 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26857 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26859 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26860 const char *p = IDENTIFIER_POINTER (id);
26861 if (strcmp (p, "sections") == 0)
26863 cp_lexer_consume_token (parser->lexer);
26864 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26865 p_name = "#pragma omp parallel sections";
26866 mask |= OMP_SECTIONS_CLAUSE_MASK;
26867 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26871 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26872 block = begin_omp_parallel ();
26873 save = cp_parser_begin_omp_structured_block (parser);
26875 switch (p_kind)
26877 case PRAGMA_OMP_PARALLEL:
26878 cp_parser_statement (parser, NULL_TREE, false, NULL);
26879 par_clause = clauses;
26880 break;
26882 case PRAGMA_OMP_PARALLEL_FOR:
26883 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26884 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26885 break;
26887 case PRAGMA_OMP_PARALLEL_SECTIONS:
26888 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26889 stmt = cp_parser_omp_sections_scope (parser);
26890 if (stmt)
26891 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26892 break;
26894 default:
26895 gcc_unreachable ();
26898 cp_parser_end_omp_structured_block (parser, save);
26899 stmt = finish_omp_parallel (par_clause, block);
26900 if (p_kind != PRAGMA_OMP_PARALLEL)
26901 OMP_PARALLEL_COMBINED (stmt) = 1;
26902 return stmt;
26905 /* OpenMP 2.5:
26906 # pragma omp single single-clause[optseq] new-line
26907 structured-block */
26909 #define OMP_SINGLE_CLAUSE_MASK \
26910 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26911 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26912 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26913 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26915 static tree
26916 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26918 tree stmt = make_node (OMP_SINGLE);
26919 TREE_TYPE (stmt) = void_type_node;
26921 OMP_SINGLE_CLAUSES (stmt)
26922 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26923 "#pragma omp single", pragma_tok);
26924 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26926 return add_stmt (stmt);
26929 /* OpenMP 3.0:
26930 # pragma omp task task-clause[optseq] new-line
26931 structured-block */
26933 #define OMP_TASK_CLAUSE_MASK \
26934 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26935 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26936 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26937 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26938 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26939 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26940 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26941 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26943 static tree
26944 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26946 tree clauses, block;
26947 unsigned int save;
26949 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26950 "#pragma omp task", pragma_tok);
26951 block = begin_omp_task ();
26952 save = cp_parser_begin_omp_structured_block (parser);
26953 cp_parser_statement (parser, NULL_TREE, false, NULL);
26954 cp_parser_end_omp_structured_block (parser, save);
26955 return finish_omp_task (clauses, block);
26958 /* OpenMP 3.0:
26959 # pragma omp taskwait new-line */
26961 static void
26962 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26964 cp_parser_require_pragma_eol (parser, pragma_tok);
26965 finish_omp_taskwait ();
26968 /* OpenMP 3.1:
26969 # pragma omp taskyield new-line */
26971 static void
26972 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26974 cp_parser_require_pragma_eol (parser, pragma_tok);
26975 finish_omp_taskyield ();
26978 /* OpenMP 2.5:
26979 # pragma omp threadprivate (variable-list) */
26981 static void
26982 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26984 tree vars;
26986 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26987 cp_parser_require_pragma_eol (parser, pragma_tok);
26989 finish_omp_threadprivate (vars);
26992 /* Main entry point to OpenMP statement pragmas. */
26994 static void
26995 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26997 tree stmt;
26999 switch (pragma_tok->pragma_kind)
27001 case PRAGMA_OMP_ATOMIC:
27002 cp_parser_omp_atomic (parser, pragma_tok);
27003 return;
27004 case PRAGMA_OMP_CRITICAL:
27005 stmt = cp_parser_omp_critical (parser, pragma_tok);
27006 break;
27007 case PRAGMA_OMP_FOR:
27008 stmt = cp_parser_omp_for (parser, pragma_tok);
27009 break;
27010 case PRAGMA_OMP_MASTER:
27011 stmt = cp_parser_omp_master (parser, pragma_tok);
27012 break;
27013 case PRAGMA_OMP_ORDERED:
27014 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27015 break;
27016 case PRAGMA_OMP_PARALLEL:
27017 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27018 break;
27019 case PRAGMA_OMP_SECTIONS:
27020 stmt = cp_parser_omp_sections (parser, pragma_tok);
27021 break;
27022 case PRAGMA_OMP_SINGLE:
27023 stmt = cp_parser_omp_single (parser, pragma_tok);
27024 break;
27025 case PRAGMA_OMP_TASK:
27026 stmt = cp_parser_omp_task (parser, pragma_tok);
27027 break;
27028 default:
27029 gcc_unreachable ();
27032 if (stmt)
27033 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27036 /* Transactional Memory parsing routines. */
27038 /* Parse a transaction attribute.
27040 txn-attribute:
27041 attribute
27042 [ [ identifier ] ]
27044 ??? Simplify this when C++0x bracket attributes are
27045 implemented properly. */
27047 static tree
27048 cp_parser_txn_attribute_opt (cp_parser *parser)
27050 cp_token *token;
27051 tree attr_name, attr = NULL;
27053 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27054 return cp_parser_attributes_opt (parser);
27056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27057 return NULL_TREE;
27058 cp_lexer_consume_token (parser->lexer);
27059 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27060 goto error1;
27062 token = cp_lexer_peek_token (parser->lexer);
27063 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27065 token = cp_lexer_consume_token (parser->lexer);
27067 attr_name = (token->type == CPP_KEYWORD
27068 /* For keywords, use the canonical spelling,
27069 not the parsed identifier. */
27070 ? ridpointers[(int) token->keyword]
27071 : token->u.value);
27072 attr = build_tree_list (attr_name, NULL_TREE);
27074 else
27075 cp_parser_error (parser, "expected identifier");
27077 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27078 error1:
27079 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27080 return attr;
27083 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27085 transaction-statement:
27086 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27087 compound-statement
27088 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27091 static tree
27092 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27094 unsigned char old_in = parser->in_transaction;
27095 unsigned char this_in = 1, new_in;
27096 cp_token *token;
27097 tree stmt, attrs, noex;
27099 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27100 || keyword == RID_TRANSACTION_RELAXED);
27101 token = cp_parser_require_keyword (parser, keyword,
27102 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27103 : RT_TRANSACTION_RELAXED));
27104 gcc_assert (token != NULL);
27106 if (keyword == RID_TRANSACTION_RELAXED)
27107 this_in |= TM_STMT_ATTR_RELAXED;
27108 else
27110 attrs = cp_parser_txn_attribute_opt (parser);
27111 if (attrs)
27112 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27115 /* Parse a noexcept specification. */
27116 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27118 /* Keep track if we're in the lexical scope of an outer transaction. */
27119 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27121 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27123 parser->in_transaction = new_in;
27124 cp_parser_compound_statement (parser, NULL, false, false);
27125 parser->in_transaction = old_in;
27127 finish_transaction_stmt (stmt, NULL, this_in, noex);
27129 return stmt;
27132 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27134 transaction-expression:
27135 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27136 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27139 static tree
27140 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27142 unsigned char old_in = parser->in_transaction;
27143 unsigned char this_in = 1;
27144 cp_token *token;
27145 tree expr, noex;
27146 bool noex_expr;
27148 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27149 || keyword == RID_TRANSACTION_RELAXED);
27151 if (!flag_tm)
27152 error (keyword == RID_TRANSACTION_RELAXED
27153 ? G_("%<__transaction_relaxed%> without transactional memory "
27154 "support enabled")
27155 : G_("%<__transaction_atomic%> without transactional memory "
27156 "support enabled"));
27158 token = cp_parser_require_keyword (parser, keyword,
27159 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27160 : RT_TRANSACTION_RELAXED));
27161 gcc_assert (token != NULL);
27163 if (keyword == RID_TRANSACTION_RELAXED)
27164 this_in |= TM_STMT_ATTR_RELAXED;
27166 /* Set this early. This might mean that we allow transaction_cancel in
27167 an expression that we find out later actually has to be a constexpr.
27168 However, we expect that cxx_constant_value will be able to deal with
27169 this; also, if the noexcept has no constexpr, then what we parse next
27170 really is a transaction's body. */
27171 parser->in_transaction = this_in;
27173 /* Parse a noexcept specification. */
27174 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27175 true);
27177 if (!noex || !noex_expr
27178 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27180 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27182 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27183 finish_parenthesized_expr (expr);
27185 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27187 else
27189 /* The only expression that is available got parsed for the noexcept
27190 already. noexcept is true then. */
27191 expr = noex;
27192 noex = boolean_true_node;
27195 expr = build_transaction_expr (token->location, expr, this_in, noex);
27196 parser->in_transaction = old_in;
27198 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27199 return error_mark_node;
27201 return (flag_tm ? expr : error_mark_node);
27204 /* Parse a function-transaction-block.
27206 function-transaction-block:
27207 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27208 function-body
27209 __transaction_atomic txn-attribute[opt] function-try-block
27210 __transaction_relaxed ctor-initializer[opt] function-body
27211 __transaction_relaxed function-try-block
27214 static bool
27215 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27217 unsigned char old_in = parser->in_transaction;
27218 unsigned char new_in = 1;
27219 tree compound_stmt, stmt, attrs;
27220 bool ctor_initializer_p;
27221 cp_token *token;
27223 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27224 || keyword == RID_TRANSACTION_RELAXED);
27225 token = cp_parser_require_keyword (parser, keyword,
27226 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27227 : RT_TRANSACTION_RELAXED));
27228 gcc_assert (token != NULL);
27230 if (keyword == RID_TRANSACTION_RELAXED)
27231 new_in |= TM_STMT_ATTR_RELAXED;
27232 else
27234 attrs = cp_parser_txn_attribute_opt (parser);
27235 if (attrs)
27236 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27239 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27241 parser->in_transaction = new_in;
27243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27244 ctor_initializer_p = cp_parser_function_try_block (parser);
27245 else
27246 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27247 (parser, /*in_function_try_block=*/false);
27249 parser->in_transaction = old_in;
27251 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27253 return ctor_initializer_p;
27256 /* Parse a __transaction_cancel statement.
27258 cancel-statement:
27259 __transaction_cancel txn-attribute[opt] ;
27260 __transaction_cancel txn-attribute[opt] throw-expression ;
27262 ??? Cancel and throw is not yet implemented. */
27264 static tree
27265 cp_parser_transaction_cancel (cp_parser *parser)
27267 cp_token *token;
27268 bool is_outer = false;
27269 tree stmt, attrs;
27271 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27272 RT_TRANSACTION_CANCEL);
27273 gcc_assert (token != NULL);
27275 attrs = cp_parser_txn_attribute_opt (parser);
27276 if (attrs)
27277 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27279 /* ??? Parse cancel-and-throw here. */
27281 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27283 if (!flag_tm)
27285 error_at (token->location, "%<__transaction_cancel%> without "
27286 "transactional memory support enabled");
27287 return error_mark_node;
27289 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27291 error_at (token->location, "%<__transaction_cancel%> within a "
27292 "%<__transaction_relaxed%>");
27293 return error_mark_node;
27295 else if (is_outer)
27297 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27298 && !is_tm_may_cancel_outer (current_function_decl))
27300 error_at (token->location, "outer %<__transaction_cancel%> not "
27301 "within outer %<__transaction_atomic%>");
27302 error_at (token->location,
27303 " or a %<transaction_may_cancel_outer%> function");
27304 return error_mark_node;
27307 else if (parser->in_transaction == 0)
27309 error_at (token->location, "%<__transaction_cancel%> not within "
27310 "%<__transaction_atomic%>");
27311 return error_mark_node;
27314 stmt = build_tm_abort_call (token->location, is_outer);
27315 add_stmt (stmt);
27316 finish_stmt ();
27318 return stmt;
27321 /* The parser. */
27323 static GTY (()) cp_parser *the_parser;
27326 /* Special handling for the first token or line in the file. The first
27327 thing in the file might be #pragma GCC pch_preprocess, which loads a
27328 PCH file, which is a GC collection point. So we need to handle this
27329 first pragma without benefit of an existing lexer structure.
27331 Always returns one token to the caller in *FIRST_TOKEN. This is
27332 either the true first token of the file, or the first token after
27333 the initial pragma. */
27335 static void
27336 cp_parser_initial_pragma (cp_token *first_token)
27338 tree name = NULL;
27340 cp_lexer_get_preprocessor_token (NULL, first_token);
27341 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27342 return;
27344 cp_lexer_get_preprocessor_token (NULL, first_token);
27345 if (first_token->type == CPP_STRING)
27347 name = first_token->u.value;
27349 cp_lexer_get_preprocessor_token (NULL, first_token);
27350 if (first_token->type != CPP_PRAGMA_EOL)
27351 error_at (first_token->location,
27352 "junk at end of %<#pragma GCC pch_preprocess%>");
27354 else
27355 error_at (first_token->location, "expected string literal");
27357 /* Skip to the end of the pragma. */
27358 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27359 cp_lexer_get_preprocessor_token (NULL, first_token);
27361 /* Now actually load the PCH file. */
27362 if (name)
27363 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27365 /* Read one more token to return to our caller. We have to do this
27366 after reading the PCH file in, since its pointers have to be
27367 live. */
27368 cp_lexer_get_preprocessor_token (NULL, first_token);
27371 /* Normal parsing of a pragma token. Here we can (and must) use the
27372 regular lexer. */
27374 static bool
27375 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27377 cp_token *pragma_tok;
27378 unsigned int id;
27380 pragma_tok = cp_lexer_consume_token (parser->lexer);
27381 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27382 parser->lexer->in_pragma = true;
27384 id = pragma_tok->pragma_kind;
27385 switch (id)
27387 case PRAGMA_GCC_PCH_PREPROCESS:
27388 error_at (pragma_tok->location,
27389 "%<#pragma GCC pch_preprocess%> must be first");
27390 break;
27392 case PRAGMA_OMP_BARRIER:
27393 switch (context)
27395 case pragma_compound:
27396 cp_parser_omp_barrier (parser, pragma_tok);
27397 return false;
27398 case pragma_stmt:
27399 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27400 "used in compound statements");
27401 break;
27402 default:
27403 goto bad_stmt;
27405 break;
27407 case PRAGMA_OMP_FLUSH:
27408 switch (context)
27410 case pragma_compound:
27411 cp_parser_omp_flush (parser, pragma_tok);
27412 return false;
27413 case pragma_stmt:
27414 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27415 "used in compound statements");
27416 break;
27417 default:
27418 goto bad_stmt;
27420 break;
27422 case PRAGMA_OMP_TASKWAIT:
27423 switch (context)
27425 case pragma_compound:
27426 cp_parser_omp_taskwait (parser, pragma_tok);
27427 return false;
27428 case pragma_stmt:
27429 error_at (pragma_tok->location,
27430 "%<#pragma omp taskwait%> may only be "
27431 "used in compound statements");
27432 break;
27433 default:
27434 goto bad_stmt;
27436 break;
27438 case PRAGMA_OMP_TASKYIELD:
27439 switch (context)
27441 case pragma_compound:
27442 cp_parser_omp_taskyield (parser, pragma_tok);
27443 return false;
27444 case pragma_stmt:
27445 error_at (pragma_tok->location,
27446 "%<#pragma omp taskyield%> may only be "
27447 "used in compound statements");
27448 break;
27449 default:
27450 goto bad_stmt;
27452 break;
27454 case PRAGMA_OMP_THREADPRIVATE:
27455 cp_parser_omp_threadprivate (parser, pragma_tok);
27456 return false;
27458 case PRAGMA_OMP_ATOMIC:
27459 case PRAGMA_OMP_CRITICAL:
27460 case PRAGMA_OMP_FOR:
27461 case PRAGMA_OMP_MASTER:
27462 case PRAGMA_OMP_ORDERED:
27463 case PRAGMA_OMP_PARALLEL:
27464 case PRAGMA_OMP_SECTIONS:
27465 case PRAGMA_OMP_SINGLE:
27466 case PRAGMA_OMP_TASK:
27467 if (context == pragma_external)
27468 goto bad_stmt;
27469 cp_parser_omp_construct (parser, pragma_tok);
27470 return true;
27472 case PRAGMA_OMP_SECTION:
27473 error_at (pragma_tok->location,
27474 "%<#pragma omp section%> may only be used in "
27475 "%<#pragma omp sections%> construct");
27476 break;
27478 default:
27479 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27480 c_invoke_pragma_handler (id);
27481 break;
27483 bad_stmt:
27484 cp_parser_error (parser, "expected declaration specifiers");
27485 break;
27488 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27489 return false;
27492 /* The interface the pragma parsers have to the lexer. */
27494 enum cpp_ttype
27495 pragma_lex (tree *value)
27497 cp_token *tok;
27498 enum cpp_ttype ret;
27500 tok = cp_lexer_peek_token (the_parser->lexer);
27502 ret = tok->type;
27503 *value = tok->u.value;
27505 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27506 ret = CPP_EOF;
27507 else if (ret == CPP_STRING)
27508 *value = cp_parser_string_literal (the_parser, false, false);
27509 else
27511 cp_lexer_consume_token (the_parser->lexer);
27512 if (ret == CPP_KEYWORD)
27513 ret = CPP_NAME;
27516 return ret;
27520 /* External interface. */
27522 /* Parse one entire translation unit. */
27524 void
27525 c_parse_file (void)
27527 static bool already_called = false;
27529 if (already_called)
27531 sorry ("inter-module optimizations not implemented for C++");
27532 return;
27534 already_called = true;
27536 the_parser = cp_parser_new ();
27537 push_deferring_access_checks (flag_access_control
27538 ? dk_no_deferred : dk_no_check);
27539 cp_parser_translation_unit (the_parser);
27540 the_parser = NULL;
27543 #include "gt-cp-parser.h"